Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/drivers/block/ps3disk.c
49156 views
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3
* PS3 Disk Storage Driver
4
*
5
* Copyright (C) 2007 Sony Computer Entertainment Inc.
6
* Copyright 2007 Sony Corp.
7
*/
8
9
#include <linux/ata.h>
10
#include <linux/blk-mq.h>
11
#include <linux/slab.h>
12
#include <linux/module.h>
13
14
#include <asm/lv1call.h>
15
#include <asm/ps3stor.h>
16
#include <asm/firmware.h>
17
18
19
#define DEVICE_NAME "ps3disk"
20
21
#define BOUNCE_SIZE (64*1024)
22
23
#define PS3DISK_MAX_DISKS 16
24
#define PS3DISK_MINORS 16
25
26
27
#define PS3DISK_NAME "ps3d%c"
28
29
30
struct ps3disk_private {
31
spinlock_t lock; /* Request queue spinlock */
32
struct blk_mq_tag_set tag_set;
33
struct gendisk *gendisk;
34
unsigned int blocking_factor;
35
struct request *req;
36
u64 raw_capacity;
37
unsigned char model[ATA_ID_PROD_LEN+1];
38
};
39
40
41
#define LV1_STORAGE_SEND_ATA_COMMAND (2)
42
#define LV1_STORAGE_ATA_HDDOUT (0x23)
43
44
struct lv1_ata_cmnd_block {
45
u16 features;
46
u16 sector_count;
47
u16 LBA_low;
48
u16 LBA_mid;
49
u16 LBA_high;
50
u8 device;
51
u8 command;
52
u32 is_ext;
53
u32 proto;
54
u32 in_out;
55
u32 size;
56
u64 buffer;
57
u32 arglen;
58
};
59
60
enum lv1_ata_proto {
61
NON_DATA_PROTO = 0,
62
PIO_DATA_IN_PROTO = 1,
63
PIO_DATA_OUT_PROTO = 2,
64
DMA_PROTO = 3
65
};
66
67
enum lv1_ata_in_out {
68
DIR_WRITE = 0, /* memory -> device */
69
DIR_READ = 1 /* device -> memory */
70
};
71
72
static int ps3disk_major;
73
74
75
static const struct block_device_operations ps3disk_fops = {
76
.owner = THIS_MODULE,
77
};
78
79
80
static void ps3disk_scatter_gather(struct ps3_storage_device *dev,
81
struct request *req, int gather)
82
{
83
unsigned int offset = 0;
84
struct req_iterator iter;
85
struct bio_vec bvec;
86
87
rq_for_each_segment(bvec, req, iter) {
88
dev_dbg(&dev->sbd.core, "%s:%u: %u sectors from %llu\n",
89
__func__, __LINE__, bio_sectors(iter.bio),
90
iter.bio->bi_iter.bi_sector);
91
if (gather)
92
memcpy_from_bvec(dev->bounce_buf + offset, &bvec);
93
else
94
memcpy_to_bvec(&bvec, dev->bounce_buf + offset);
95
offset += bvec.bv_len;
96
}
97
}
98
99
static blk_status_t ps3disk_submit_request_sg(struct ps3_storage_device *dev,
100
struct request *req)
101
{
102
struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
103
int write = rq_data_dir(req), res;
104
const char *op = write ? "write" : "read";
105
u64 start_sector, sectors;
106
unsigned int region_id = dev->regions[dev->region_idx].id;
107
108
#ifdef DEBUG
109
unsigned int n = 0;
110
struct bio_vec bv;
111
struct req_iterator iter;
112
113
rq_for_each_segment(bv, req, iter)
114
n++;
115
dev_dbg(&dev->sbd.core,
116
"%s:%u: %s req has %u bvecs for %u sectors\n",
117
__func__, __LINE__, op, n, blk_rq_sectors(req));
118
#endif
119
120
start_sector = blk_rq_pos(req) * priv->blocking_factor;
121
sectors = blk_rq_sectors(req) * priv->blocking_factor;
122
dev_dbg(&dev->sbd.core, "%s:%u: %s %llu sectors starting at %llu\n",
123
__func__, __LINE__, op, sectors, start_sector);
124
125
if (write) {
126
ps3disk_scatter_gather(dev, req, 1);
127
128
res = lv1_storage_write(dev->sbd.dev_id, region_id,
129
start_sector, sectors, 0,
130
dev->bounce_lpar, &dev->tag);
131
} else {
132
res = lv1_storage_read(dev->sbd.dev_id, region_id,
133
start_sector, sectors, 0,
134
dev->bounce_lpar, &dev->tag);
135
}
136
if (res) {
137
dev_err(&dev->sbd.core, "%s:%u: %s failed %d\n", __func__,
138
__LINE__, op, res);
139
return BLK_STS_IOERR;
140
}
141
142
priv->req = req;
143
return BLK_STS_OK;
144
}
145
146
static blk_status_t ps3disk_submit_flush_request(struct ps3_storage_device *dev,
147
struct request *req)
148
{
149
struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
150
u64 res;
151
152
dev_dbg(&dev->sbd.core, "%s:%u: flush request\n", __func__, __LINE__);
153
154
res = lv1_storage_send_device_command(dev->sbd.dev_id,
155
LV1_STORAGE_ATA_HDDOUT, 0, 0, 0,
156
0, &dev->tag);
157
if (res) {
158
dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%llx\n",
159
__func__, __LINE__, res);
160
return BLK_STS_IOERR;
161
}
162
163
priv->req = req;
164
return BLK_STS_OK;
165
}
166
167
static blk_status_t ps3disk_do_request(struct ps3_storage_device *dev,
168
struct request *req)
169
{
170
dev_dbg(&dev->sbd.core, "%s:%u\n", __func__, __LINE__);
171
172
switch (req_op(req)) {
173
case REQ_OP_FLUSH:
174
return ps3disk_submit_flush_request(dev, req);
175
case REQ_OP_READ:
176
case REQ_OP_WRITE:
177
return ps3disk_submit_request_sg(dev, req);
178
default:
179
blk_dump_rq_flags(req, DEVICE_NAME " bad request");
180
return BLK_STS_IOERR;
181
}
182
}
183
184
static blk_status_t ps3disk_queue_rq(struct blk_mq_hw_ctx *hctx,
185
const struct blk_mq_queue_data *bd)
186
{
187
struct request_queue *q = hctx->queue;
188
struct ps3_storage_device *dev = q->queuedata;
189
struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
190
blk_status_t ret;
191
192
blk_mq_start_request(bd->rq);
193
194
spin_lock_irq(&priv->lock);
195
ret = ps3disk_do_request(dev, bd->rq);
196
spin_unlock_irq(&priv->lock);
197
198
return ret;
199
}
200
201
static irqreturn_t ps3disk_interrupt(int irq, void *data)
202
{
203
struct ps3_storage_device *dev = data;
204
struct ps3disk_private *priv;
205
struct request *req;
206
int res, read;
207
blk_status_t error;
208
u64 tag, status;
209
const char *op;
210
211
res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
212
213
if (tag != dev->tag)
214
dev_err(&dev->sbd.core,
215
"%s:%u: tag mismatch, got %llx, expected %llx\n",
216
__func__, __LINE__, tag, dev->tag);
217
218
if (res) {
219
dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%llx\n",
220
__func__, __LINE__, res, status);
221
return IRQ_HANDLED;
222
}
223
224
priv = ps3_system_bus_get_drvdata(&dev->sbd);
225
req = priv->req;
226
if (!req) {
227
dev_dbg(&dev->sbd.core,
228
"%s:%u non-block layer request completed\n", __func__,
229
__LINE__);
230
dev->lv1_status = status;
231
complete(&dev->done);
232
return IRQ_HANDLED;
233
}
234
235
if (req_op(req) == REQ_OP_FLUSH) {
236
read = 0;
237
op = "flush";
238
} else {
239
read = !rq_data_dir(req);
240
op = read ? "read" : "write";
241
}
242
if (status) {
243
dev_dbg(&dev->sbd.core, "%s:%u: %s failed 0x%llx\n", __func__,
244
__LINE__, op, status);
245
error = BLK_STS_IOERR;
246
} else {
247
dev_dbg(&dev->sbd.core, "%s:%u: %s completed\n", __func__,
248
__LINE__, op);
249
error = 0;
250
if (read)
251
ps3disk_scatter_gather(dev, req, 0);
252
}
253
254
spin_lock(&priv->lock);
255
priv->req = NULL;
256
blk_mq_end_request(req, error);
257
spin_unlock(&priv->lock);
258
259
blk_mq_run_hw_queues(priv->gendisk->queue, true);
260
return IRQ_HANDLED;
261
}
262
263
static int ps3disk_sync_cache(struct ps3_storage_device *dev)
264
{
265
u64 res;
266
267
dev_dbg(&dev->sbd.core, "%s:%u: sync cache\n", __func__, __LINE__);
268
269
res = ps3stor_send_command(dev, LV1_STORAGE_ATA_HDDOUT, 0, 0, 0, 0);
270
if (res) {
271
dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%llx\n",
272
__func__, __LINE__, res);
273
return -EIO;
274
}
275
return 0;
276
}
277
278
279
/* ATA helpers copied from drivers/ata/libata-core.c */
280
281
static void swap_buf_le16(u16 *buf, unsigned int buf_words)
282
{
283
#ifdef __BIG_ENDIAN
284
unsigned int i;
285
286
for (i = 0; i < buf_words; i++)
287
buf[i] = le16_to_cpu(buf[i]);
288
#endif /* __BIG_ENDIAN */
289
}
290
291
static u64 ata_id_n_sectors(const u16 *id)
292
{
293
if (ata_id_has_lba(id)) {
294
if (ata_id_has_lba48(id))
295
return ata_id_u64(id, 100);
296
else
297
return ata_id_u32(id, 60);
298
} else {
299
if (ata_id_current_chs_valid(id))
300
return ata_id_u32(id, 57);
301
else
302
return id[1] * id[3] * id[6];
303
}
304
}
305
306
static void ata_id_string(const u16 *id, unsigned char *s, unsigned int ofs,
307
unsigned int len)
308
{
309
unsigned int c;
310
311
while (len > 0) {
312
c = id[ofs] >> 8;
313
*s = c;
314
s++;
315
316
c = id[ofs] & 0xff;
317
*s = c;
318
s++;
319
320
ofs++;
321
len -= 2;
322
}
323
}
324
325
static void ata_id_c_string(const u16 *id, unsigned char *s, unsigned int ofs,
326
unsigned int len)
327
{
328
unsigned char *p;
329
330
WARN_ON(!(len & 1));
331
332
ata_id_string(id, s, ofs, len - 1);
333
334
p = s + strnlen(s, len - 1);
335
while (p > s && p[-1] == ' ')
336
p--;
337
*p = '\0';
338
}
339
340
static int ps3disk_identify(struct ps3_storage_device *dev)
341
{
342
struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
343
struct lv1_ata_cmnd_block ata_cmnd;
344
u16 *id = dev->bounce_buf;
345
u64 res;
346
347
dev_dbg(&dev->sbd.core, "%s:%u: identify disk\n", __func__, __LINE__);
348
349
memset(&ata_cmnd, 0, sizeof(struct lv1_ata_cmnd_block));
350
ata_cmnd.command = ATA_CMD_ID_ATA;
351
ata_cmnd.sector_count = 1;
352
ata_cmnd.size = ata_cmnd.arglen = ATA_ID_WORDS * 2;
353
ata_cmnd.buffer = dev->bounce_lpar;
354
ata_cmnd.proto = PIO_DATA_IN_PROTO;
355
ata_cmnd.in_out = DIR_READ;
356
357
res = ps3stor_send_command(dev, LV1_STORAGE_SEND_ATA_COMMAND,
358
ps3_mm_phys_to_lpar(__pa(&ata_cmnd)),
359
sizeof(ata_cmnd), ata_cmnd.buffer,
360
ata_cmnd.arglen);
361
if (res) {
362
dev_err(&dev->sbd.core, "%s:%u: identify disk failed 0x%llx\n",
363
__func__, __LINE__, res);
364
return -EIO;
365
}
366
367
swap_buf_le16(id, ATA_ID_WORDS);
368
369
/* All we're interested in are raw capacity and model name */
370
priv->raw_capacity = ata_id_n_sectors(id);
371
ata_id_c_string(id, priv->model, ATA_ID_PROD, sizeof(priv->model));
372
return 0;
373
}
374
375
static unsigned long ps3disk_mask;
376
377
static DEFINE_MUTEX(ps3disk_mask_mutex);
378
379
static const struct blk_mq_ops ps3disk_mq_ops = {
380
.queue_rq = ps3disk_queue_rq,
381
};
382
383
static int ps3disk_probe(struct ps3_system_bus_device *_dev)
384
{
385
struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
386
struct ps3disk_private *priv;
387
int error;
388
unsigned int devidx;
389
struct queue_limits lim = {
390
.logical_block_size = dev->blk_size,
391
.max_hw_sectors = BOUNCE_SIZE >> 9,
392
.max_segments = -1,
393
.max_segment_size = BOUNCE_SIZE,
394
.dma_alignment = dev->blk_size - 1,
395
.features = BLK_FEAT_WRITE_CACHE |
396
BLK_FEAT_ROTATIONAL,
397
};
398
struct gendisk *gendisk;
399
400
if (dev->blk_size < 512) {
401
dev_err(&dev->sbd.core,
402
"%s:%u: cannot handle block size %llu\n", __func__,
403
__LINE__, dev->blk_size);
404
return -EINVAL;
405
}
406
407
BUILD_BUG_ON(PS3DISK_MAX_DISKS > BITS_PER_LONG);
408
mutex_lock(&ps3disk_mask_mutex);
409
devidx = find_first_zero_bit(&ps3disk_mask, PS3DISK_MAX_DISKS);
410
if (devidx >= PS3DISK_MAX_DISKS) {
411
dev_err(&dev->sbd.core, "%s:%u: Too many disks\n", __func__,
412
__LINE__);
413
mutex_unlock(&ps3disk_mask_mutex);
414
return -ENOSPC;
415
}
416
__set_bit(devidx, &ps3disk_mask);
417
mutex_unlock(&ps3disk_mask_mutex);
418
419
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
420
if (!priv) {
421
error = -ENOMEM;
422
goto fail;
423
}
424
425
ps3_system_bus_set_drvdata(_dev, priv);
426
spin_lock_init(&priv->lock);
427
428
dev->bounce_size = BOUNCE_SIZE;
429
dev->bounce_buf = kmalloc(BOUNCE_SIZE, GFP_DMA);
430
if (!dev->bounce_buf) {
431
error = -ENOMEM;
432
goto fail_free_priv;
433
}
434
435
error = ps3stor_setup(dev, ps3disk_interrupt);
436
if (error)
437
goto fail_free_bounce;
438
439
ps3disk_identify(dev);
440
441
error = blk_mq_alloc_sq_tag_set(&priv->tag_set, &ps3disk_mq_ops, 1, 0);
442
if (error)
443
goto fail_teardown;
444
445
gendisk = blk_mq_alloc_disk(&priv->tag_set, &lim, dev);
446
if (IS_ERR(gendisk)) {
447
dev_err(&dev->sbd.core, "%s:%u: blk_mq_alloc_disk failed\n",
448
__func__, __LINE__);
449
error = PTR_ERR(gendisk);
450
goto fail_free_tag_set;
451
}
452
453
priv->gendisk = gendisk;
454
gendisk->major = ps3disk_major;
455
gendisk->first_minor = devidx * PS3DISK_MINORS;
456
gendisk->minors = PS3DISK_MINORS;
457
gendisk->fops = &ps3disk_fops;
458
gendisk->private_data = dev;
459
snprintf(gendisk->disk_name, sizeof(gendisk->disk_name), PS3DISK_NAME,
460
devidx+'a');
461
priv->blocking_factor = dev->blk_size >> 9;
462
set_capacity(gendisk,
463
dev->regions[dev->region_idx].size*priv->blocking_factor);
464
465
dev_info(&dev->sbd.core,
466
"%s is a %s (%llu MiB total, %llu MiB for OtherOS)\n",
467
gendisk->disk_name, priv->model, priv->raw_capacity >> 11,
468
get_capacity(gendisk) >> 11);
469
470
error = device_add_disk(&dev->sbd.core, gendisk, NULL);
471
if (error)
472
goto fail_cleanup_disk;
473
474
return 0;
475
fail_cleanup_disk:
476
put_disk(gendisk);
477
fail_free_tag_set:
478
blk_mq_free_tag_set(&priv->tag_set);
479
fail_teardown:
480
ps3stor_teardown(dev);
481
fail_free_bounce:
482
kfree(dev->bounce_buf);
483
fail_free_priv:
484
kfree(priv);
485
ps3_system_bus_set_drvdata(_dev, NULL);
486
fail:
487
mutex_lock(&ps3disk_mask_mutex);
488
__clear_bit(devidx, &ps3disk_mask);
489
mutex_unlock(&ps3disk_mask_mutex);
490
return error;
491
}
492
493
static void ps3disk_remove(struct ps3_system_bus_device *_dev)
494
{
495
struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
496
struct ps3disk_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
497
498
mutex_lock(&ps3disk_mask_mutex);
499
__clear_bit(MINOR(disk_devt(priv->gendisk)) / PS3DISK_MINORS,
500
&ps3disk_mask);
501
mutex_unlock(&ps3disk_mask_mutex);
502
del_gendisk(priv->gendisk);
503
put_disk(priv->gendisk);
504
blk_mq_free_tag_set(&priv->tag_set);
505
dev_notice(&dev->sbd.core, "Synchronizing disk cache\n");
506
ps3disk_sync_cache(dev);
507
ps3stor_teardown(dev);
508
kfree(dev->bounce_buf);
509
kfree(priv);
510
ps3_system_bus_set_drvdata(_dev, NULL);
511
}
512
513
static struct ps3_system_bus_driver ps3disk = {
514
.match_id = PS3_MATCH_ID_STOR_DISK,
515
.core.name = DEVICE_NAME,
516
.core.owner = THIS_MODULE,
517
.probe = ps3disk_probe,
518
.remove = ps3disk_remove,
519
.shutdown = ps3disk_remove,
520
};
521
522
523
static int __init ps3disk_init(void)
524
{
525
int error;
526
527
if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
528
return -ENODEV;
529
530
error = register_blkdev(0, DEVICE_NAME);
531
if (error <= 0) {
532
printk(KERN_ERR "%s:%u: register_blkdev failed %d\n", __func__,
533
__LINE__, error);
534
return error;
535
}
536
ps3disk_major = error;
537
538
pr_info("%s:%u: registered block device major %d\n", __func__,
539
__LINE__, ps3disk_major);
540
541
error = ps3_system_bus_driver_register(&ps3disk);
542
if (error)
543
unregister_blkdev(ps3disk_major, DEVICE_NAME);
544
545
return error;
546
}
547
548
static void __exit ps3disk_exit(void)
549
{
550
ps3_system_bus_driver_unregister(&ps3disk);
551
unregister_blkdev(ps3disk_major, DEVICE_NAME);
552
}
553
554
module_init(ps3disk_init);
555
module_exit(ps3disk_exit);
556
557
MODULE_LICENSE("GPL");
558
MODULE_DESCRIPTION("PS3 Disk Storage Driver");
559
MODULE_AUTHOR("Sony Corporation");
560
MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_DISK);
561
562