Introduce rq_for_each_segment replacing rq_for_each_bio
[linux-2.6.git] / drivers / block / ps3disk.c
1 /*
2  * PS3 Disk Storage Driver
3  *
4  * Copyright (C) 2007 Sony Computer Entertainment Inc.
5  * Copyright 2007 Sony Corp.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published
9  * by the Free Software Foundation; version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <linux/ata.h>
22 #include <linux/blkdev.h>
23
24 #include <asm/lv1call.h>
25 #include <asm/ps3stor.h>
26 #include <asm/firmware.h>
27
28
29 #define DEVICE_NAME             "ps3disk"
30
31 #define BOUNCE_SIZE             (64*1024)
32
33 #define PS3DISK_MAX_DISKS       16
34 #define PS3DISK_MINORS          16
35
36
37 #define PS3DISK_NAME            "ps3d%c"
38
39
40 struct ps3disk_private {
41         spinlock_t lock;                /* Request queue spinlock */
42         struct request_queue *queue;
43         struct gendisk *gendisk;
44         unsigned int blocking_factor;
45         struct request *req;
46         u64 raw_capacity;
47         unsigned char model[ATA_ID_PROD_LEN+1];
48 };
49
50
51 #define LV1_STORAGE_SEND_ATA_COMMAND    (2)
52 #define LV1_STORAGE_ATA_HDDOUT          (0x23)
53
54 struct lv1_ata_cmnd_block {
55         u16     features;
56         u16     sector_count;
57         u16     LBA_low;
58         u16     LBA_mid;
59         u16     LBA_high;
60         u8      device;
61         u8      command;
62         u32     is_ext;
63         u32     proto;
64         u32     in_out;
65         u32     size;
66         u64     buffer;
67         u32     arglen;
68 };
69
70 enum lv1_ata_proto {
71         NON_DATA_PROTO     = 0,
72         PIO_DATA_IN_PROTO  = 1,
73         PIO_DATA_OUT_PROTO = 2,
74         DMA_PROTO = 3
75 };
76
77 enum lv1_ata_in_out {
78         DIR_WRITE = 0,                  /* memory -> device */
79         DIR_READ = 1                    /* device -> memory */
80 };
81
82 static int ps3disk_major;
83
84
85 static struct block_device_operations ps3disk_fops = {
86         .owner          = THIS_MODULE,
87 };
88
89
90 static void ps3disk_scatter_gather(struct ps3_storage_device *dev,
91                                    struct request *req, int gather)
92 {
93         unsigned int offset = 0;
94         struct req_iterator iter;
95         struct bio_vec *bvec;
96         unsigned int i = 0;
97         size_t size;
98         void *buf;
99
100         rq_for_each_segment(bvec, req, iter) {
101                 unsigned long flags;
102                 dev_dbg(&dev->sbd.core,
103                         "%s:%u: bio %u: %u segs %u sectors from %lu\n",
104                         __func__, __LINE__, i, bio_segments(iter.bio),
105                         bio_sectors(iter.bio),
106                         (unsigned long)iter.bio->bi_sector);
107
108                         size = bvec->bv_len;
109                         buf = bvec_kmap_irq(bvec, &flags);
110                         if (gather)
111                                 memcpy(dev->bounce_buf+offset, buf, size);
112                         else
113                                 memcpy(buf, dev->bounce_buf+offset, size);
114                         offset += size;
115                         flush_kernel_dcache_page(bvec->bv_page);
116                         bvec_kunmap_irq(bvec, &flags);
117
118                 i++;
119         }
120 }
121
122 static int ps3disk_submit_request_sg(struct ps3_storage_device *dev,
123                                      struct request *req)
124 {
125         struct ps3disk_private *priv = dev->sbd.core.driver_data;
126         int write = rq_data_dir(req), res;
127         const char *op = write ? "write" : "read";
128         u64 start_sector, sectors;
129         unsigned int region_id = dev->regions[dev->region_idx].id;
130
131 #ifdef DEBUG
132         unsigned int n = 0;
133         struct bio_vec *bv;
134         struct req_iterator iter;
135
136         rq_for_each_segment(bv, req, iter)
137                 n++;
138         dev_dbg(&dev->sbd.core,
139                 "%s:%u: %s req has %u bvecs for %lu sectors %lu hard sectors\n",
140                 __func__, __LINE__, op, n, req->nr_sectors,
141                 req->hard_nr_sectors);
142 #endif
143
144         start_sector = req->sector * priv->blocking_factor;
145         sectors = req->nr_sectors * priv->blocking_factor;
146         dev_dbg(&dev->sbd.core, "%s:%u: %s %lu sectors starting at %lu\n",
147                 __func__, __LINE__, op, sectors, start_sector);
148
149         if (write) {
150                 ps3disk_scatter_gather(dev, req, 1);
151
152                 res = lv1_storage_write(dev->sbd.dev_id, region_id,
153                                         start_sector, sectors, 0,
154                                         dev->bounce_lpar, &dev->tag);
155         } else {
156                 res = lv1_storage_read(dev->sbd.dev_id, region_id,
157                                        start_sector, sectors, 0,
158                                        dev->bounce_lpar, &dev->tag);
159         }
160         if (res) {
161                 dev_err(&dev->sbd.core, "%s:%u: %s failed %d\n", __func__,
162                         __LINE__, op, res);
163                 end_request(req, 0);
164                 return 0;
165         }
166
167         priv->req = req;
168         return 1;
169 }
170
171 static int ps3disk_submit_flush_request(struct ps3_storage_device *dev,
172                                         struct request *req)
173 {
174         struct ps3disk_private *priv = dev->sbd.core.driver_data;
175         u64 res;
176
177         dev_dbg(&dev->sbd.core, "%s:%u: flush request\n", __func__, __LINE__);
178
179         res = lv1_storage_send_device_command(dev->sbd.dev_id,
180                                               LV1_STORAGE_ATA_HDDOUT, 0, 0, 0,
181                                               0, &dev->tag);
182         if (res) {
183                 dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%lx\n",
184                         __func__, __LINE__, res);
185                 end_request(req, 0);
186                 return 0;
187         }
188
189         priv->req = req;
190         return 1;
191 }
192
193 static void ps3disk_do_request(struct ps3_storage_device *dev,
194                                struct request_queue *q)
195 {
196         struct request *req;
197
198         dev_dbg(&dev->sbd.core, "%s:%u\n", __func__, __LINE__);
199
200         while ((req = elv_next_request(q))) {
201                 if (blk_fs_request(req)) {
202                         if (ps3disk_submit_request_sg(dev, req))
203                                 break;
204                 } else if (req->cmd_type == REQ_TYPE_FLUSH) {
205                         if (ps3disk_submit_flush_request(dev, req))
206                                 break;
207                 } else {
208                         blk_dump_rq_flags(req, DEVICE_NAME " bad request");
209                         end_request(req, 0);
210                         continue;
211                 }
212         }
213 }
214
215 static void ps3disk_request(struct request_queue *q)
216 {
217         struct ps3_storage_device *dev = q->queuedata;
218         struct ps3disk_private *priv = dev->sbd.core.driver_data;
219
220         if (priv->req) {
221                 dev_dbg(&dev->sbd.core, "%s:%u busy\n", __func__, __LINE__);
222                 return;
223         }
224
225         ps3disk_do_request(dev, q);
226 }
227
228 static irqreturn_t ps3disk_interrupt(int irq, void *data)
229 {
230         struct ps3_storage_device *dev = data;
231         struct ps3disk_private *priv;
232         struct request *req;
233         int res, read, uptodate;
234         u64 tag, status;
235         unsigned long num_sectors;
236         const char *op;
237
238         res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
239
240         if (tag != dev->tag)
241                 dev_err(&dev->sbd.core,
242                         "%s:%u: tag mismatch, got %lx, expected %lx\n",
243                         __func__, __LINE__, tag, dev->tag);
244
245         if (res) {
246                 dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%lx\n",
247                         __func__, __LINE__, res, status);
248                 return IRQ_HANDLED;
249         }
250
251         priv = dev->sbd.core.driver_data;
252         req = priv->req;
253         if (!req) {
254                 dev_dbg(&dev->sbd.core,
255                         "%s:%u non-block layer request completed\n", __func__,
256                         __LINE__);
257                 dev->lv1_status = status;
258                 complete(&dev->done);
259                 return IRQ_HANDLED;
260         }
261
262         if (req->cmd_type == REQ_TYPE_FLUSH) {
263                 read = 0;
264                 num_sectors = req->hard_cur_sectors;
265                 op = "flush";
266         } else {
267                 read = !rq_data_dir(req);
268                 num_sectors = req->nr_sectors;
269                 op = read ? "read" : "write";
270         }
271         if (status) {
272                 dev_dbg(&dev->sbd.core, "%s:%u: %s failed 0x%lx\n", __func__,
273                         __LINE__, op, status);
274                 uptodate = 0;
275         } else {
276                 dev_dbg(&dev->sbd.core, "%s:%u: %s completed\n", __func__,
277                         __LINE__, op);
278                 uptodate = 1;
279                 if (read)
280                         ps3disk_scatter_gather(dev, req, 0);
281         }
282
283         spin_lock(&priv->lock);
284         if (!end_that_request_first(req, uptodate, num_sectors)) {
285                 add_disk_randomness(req->rq_disk);
286                 blkdev_dequeue_request(req);
287                 end_that_request_last(req, uptodate);
288         }
289         priv->req = NULL;
290         ps3disk_do_request(dev, priv->queue);
291         spin_unlock(&priv->lock);
292
293         return IRQ_HANDLED;
294 }
295
296 static int ps3disk_sync_cache(struct ps3_storage_device *dev)
297 {
298         u64 res;
299
300         dev_dbg(&dev->sbd.core, "%s:%u: sync cache\n", __func__, __LINE__);
301
302         res = ps3stor_send_command(dev, LV1_STORAGE_ATA_HDDOUT, 0, 0, 0, 0);
303         if (res) {
304                 dev_err(&dev->sbd.core, "%s:%u: sync cache failed 0x%lx\n",
305                         __func__, __LINE__, res);
306                 return -EIO;
307         }
308         return 0;
309 }
310
311
312 /* ATA helpers copied from drivers/ata/libata-core.c */
313
314 static void swap_buf_le16(u16 *buf, unsigned int buf_words)
315 {
316 #ifdef __BIG_ENDIAN
317         unsigned int i;
318
319         for (i = 0; i < buf_words; i++)
320                 buf[i] = le16_to_cpu(buf[i]);
321 #endif /* __BIG_ENDIAN */
322 }
323
324 static u64 ata_id_n_sectors(const u16 *id)
325 {
326         if (ata_id_has_lba(id)) {
327                 if (ata_id_has_lba48(id))
328                         return ata_id_u64(id, 100);
329                 else
330                         return ata_id_u32(id, 60);
331         } else {
332                 if (ata_id_current_chs_valid(id))
333                         return ata_id_u32(id, 57);
334                 else
335                         return id[1] * id[3] * id[6];
336         }
337 }
338
339 static void ata_id_string(const u16 *id, unsigned char *s, unsigned int ofs,
340                           unsigned int len)
341 {
342         unsigned int c;
343
344         while (len > 0) {
345                 c = id[ofs] >> 8;
346                 *s = c;
347                 s++;
348
349                 c = id[ofs] & 0xff;
350                 *s = c;
351                 s++;
352
353                 ofs++;
354                 len -= 2;
355         }
356 }
357
358 static void ata_id_c_string(const u16 *id, unsigned char *s, unsigned int ofs,
359                             unsigned int len)
360 {
361         unsigned char *p;
362
363         WARN_ON(!(len & 1));
364
365         ata_id_string(id, s, ofs, len - 1);
366
367         p = s + strnlen(s, len - 1);
368         while (p > s && p[-1] == ' ')
369                 p--;
370         *p = '\0';
371 }
372
373 static int ps3disk_identify(struct ps3_storage_device *dev)
374 {
375         struct ps3disk_private *priv = dev->sbd.core.driver_data;
376         struct lv1_ata_cmnd_block ata_cmnd;
377         u16 *id = dev->bounce_buf;
378         u64 res;
379
380         dev_dbg(&dev->sbd.core, "%s:%u: identify disk\n", __func__, __LINE__);
381
382         memset(&ata_cmnd, 0, sizeof(struct lv1_ata_cmnd_block));
383         ata_cmnd.command = ATA_CMD_ID_ATA;
384         ata_cmnd.sector_count = 1;
385         ata_cmnd.size = ata_cmnd.arglen = ATA_ID_WORDS * 2;
386         ata_cmnd.buffer = dev->bounce_lpar;
387         ata_cmnd.proto = PIO_DATA_IN_PROTO;
388         ata_cmnd.in_out = DIR_READ;
389
390         res = ps3stor_send_command(dev, LV1_STORAGE_SEND_ATA_COMMAND,
391                                    ps3_mm_phys_to_lpar(__pa(&ata_cmnd)),
392                                    sizeof(ata_cmnd), ata_cmnd.buffer,
393                                    ata_cmnd.arglen);
394         if (res) {
395                 dev_err(&dev->sbd.core, "%s:%u: identify disk failed 0x%lx\n",
396                         __func__, __LINE__, res);
397                 return -EIO;
398         }
399
400         swap_buf_le16(id, ATA_ID_WORDS);
401
402         /* All we're interested in are raw capacity and model name */
403         priv->raw_capacity = ata_id_n_sectors(id);
404         ata_id_c_string(id, priv->model, ATA_ID_PROD, sizeof(priv->model));
405         return 0;
406 }
407
408 static void ps3disk_prepare_flush(struct request_queue *q, struct request *req)
409 {
410         struct ps3_storage_device *dev = q->queuedata;
411
412         dev_dbg(&dev->sbd.core, "%s:%u\n", __func__, __LINE__);
413
414         memset(req->cmd, 0, sizeof(req->cmd));
415         req->cmd_type = REQ_TYPE_FLUSH;
416 }
417
418 static int ps3disk_issue_flush(struct request_queue *q, struct gendisk *gendisk,
419                                sector_t *sector)
420 {
421         struct ps3_storage_device *dev = q->queuedata;
422         struct request *req;
423         int res;
424
425         dev_dbg(&dev->sbd.core, "%s:%u\n", __func__, __LINE__);
426
427         req = blk_get_request(q, WRITE, __GFP_WAIT);
428         ps3disk_prepare_flush(q, req);
429         res = blk_execute_rq(q, gendisk, req, 0);
430         if (res)
431                 dev_err(&dev->sbd.core, "%s:%u: flush request failed %d\n",
432                         __func__, __LINE__, res);
433         blk_put_request(req);
434         return res;
435 }
436
437
438 static unsigned long ps3disk_mask;
439
440 static DEFINE_MUTEX(ps3disk_mask_mutex);
441
442 static int __devinit ps3disk_probe(struct ps3_system_bus_device *_dev)
443 {
444         struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
445         struct ps3disk_private *priv;
446         int error;
447         unsigned int devidx;
448         struct request_queue *queue;
449         struct gendisk *gendisk;
450
451         if (dev->blk_size < 512) {
452                 dev_err(&dev->sbd.core,
453                         "%s:%u: cannot handle block size %lu\n", __func__,
454                         __LINE__, dev->blk_size);
455                 return -EINVAL;
456         }
457
458         BUILD_BUG_ON(PS3DISK_MAX_DISKS > BITS_PER_LONG);
459         mutex_lock(&ps3disk_mask_mutex);
460         devidx = find_first_zero_bit(&ps3disk_mask, PS3DISK_MAX_DISKS);
461         if (devidx >= PS3DISK_MAX_DISKS) {
462                 dev_err(&dev->sbd.core, "%s:%u: Too many disks\n", __func__,
463                         __LINE__);
464                 mutex_unlock(&ps3disk_mask_mutex);
465                 return -ENOSPC;
466         }
467         __set_bit(devidx, &ps3disk_mask);
468         mutex_unlock(&ps3disk_mask_mutex);
469
470         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
471         if (!priv) {
472                 error = -ENOMEM;
473                 goto fail;
474         }
475
476         dev->sbd.core.driver_data = priv;
477         spin_lock_init(&priv->lock);
478
479         dev->bounce_size = BOUNCE_SIZE;
480         dev->bounce_buf = kmalloc(BOUNCE_SIZE, GFP_DMA);
481         if (!dev->bounce_buf) {
482                 error = -ENOMEM;
483                 goto fail_free_priv;
484         }
485
486         error = ps3stor_setup(dev, ps3disk_interrupt);
487         if (error)
488                 goto fail_free_bounce;
489
490         ps3disk_identify(dev);
491
492         queue = blk_init_queue(ps3disk_request, &priv->lock);
493         if (!queue) {
494                 dev_err(&dev->sbd.core, "%s:%u: blk_init_queue failed\n",
495                         __func__, __LINE__);
496                 error = -ENOMEM;
497                 goto fail_teardown;
498         }
499
500         priv->queue = queue;
501         queue->queuedata = dev;
502
503         blk_queue_bounce_limit(queue, BLK_BOUNCE_HIGH);
504
505         blk_queue_max_sectors(queue, dev->bounce_size >> 9);
506         blk_queue_segment_boundary(queue, -1UL);
507         blk_queue_dma_alignment(queue, dev->blk_size-1);
508         blk_queue_hardsect_size(queue, dev->blk_size);
509
510         blk_queue_issue_flush_fn(queue, ps3disk_issue_flush);
511         blk_queue_ordered(queue, QUEUE_ORDERED_DRAIN_FLUSH,
512                           ps3disk_prepare_flush);
513
514         blk_queue_max_phys_segments(queue, -1);
515         blk_queue_max_hw_segments(queue, -1);
516         blk_queue_max_segment_size(queue, dev->bounce_size);
517
518         gendisk = alloc_disk(PS3DISK_MINORS);
519         if (!gendisk) {
520                 dev_err(&dev->sbd.core, "%s:%u: alloc_disk failed\n", __func__,
521                         __LINE__);
522                 error = -ENOMEM;
523                 goto fail_cleanup_queue;
524         }
525
526         priv->gendisk = gendisk;
527         gendisk->major = ps3disk_major;
528         gendisk->first_minor = devidx * PS3DISK_MINORS;
529         gendisk->fops = &ps3disk_fops;
530         gendisk->queue = queue;
531         gendisk->private_data = dev;
532         gendisk->driverfs_dev = &dev->sbd.core;
533         snprintf(gendisk->disk_name, sizeof(gendisk->disk_name), PS3DISK_NAME,
534                  devidx+'a');
535         priv->blocking_factor = dev->blk_size >> 9;
536         set_capacity(gendisk,
537                      dev->regions[dev->region_idx].size*priv->blocking_factor);
538
539         dev_info(&dev->sbd.core,
540                  "%s is a %s (%lu MiB total, %lu MiB for OtherOS)\n",
541                  gendisk->disk_name, priv->model, priv->raw_capacity >> 11,
542                  get_capacity(gendisk) >> 11);
543
544         add_disk(gendisk);
545         return 0;
546
547 fail_cleanup_queue:
548         blk_cleanup_queue(queue);
549 fail_teardown:
550         ps3stor_teardown(dev);
551 fail_free_bounce:
552         kfree(dev->bounce_buf);
553 fail_free_priv:
554         kfree(priv);
555         dev->sbd.core.driver_data = NULL;
556 fail:
557         mutex_lock(&ps3disk_mask_mutex);
558         __clear_bit(devidx, &ps3disk_mask);
559         mutex_unlock(&ps3disk_mask_mutex);
560         return error;
561 }
562
563 static int ps3disk_remove(struct ps3_system_bus_device *_dev)
564 {
565         struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
566         struct ps3disk_private *priv = dev->sbd.core.driver_data;
567
568         mutex_lock(&ps3disk_mask_mutex);
569         __clear_bit(priv->gendisk->first_minor / PS3DISK_MINORS,
570                     &ps3disk_mask);
571         mutex_unlock(&ps3disk_mask_mutex);
572         del_gendisk(priv->gendisk);
573         blk_cleanup_queue(priv->queue);
574         put_disk(priv->gendisk);
575         dev_notice(&dev->sbd.core, "Synchronizing disk cache\n");
576         ps3disk_sync_cache(dev);
577         ps3stor_teardown(dev);
578         kfree(dev->bounce_buf);
579         kfree(priv);
580         dev->sbd.core.driver_data = NULL;
581         return 0;
582 }
583
584 static struct ps3_system_bus_driver ps3disk = {
585         .match_id       = PS3_MATCH_ID_STOR_DISK,
586         .core.name      = DEVICE_NAME,
587         .core.owner     = THIS_MODULE,
588         .probe          = ps3disk_probe,
589         .remove         = ps3disk_remove,
590         .shutdown       = ps3disk_remove,
591 };
592
593
594 static int __init ps3disk_init(void)
595 {
596         int error;
597
598         if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
599                 return -ENODEV;
600
601         error = register_blkdev(0, DEVICE_NAME);
602         if (error <= 0) {
603                 printk(KERN_ERR "%s:%u: register_blkdev failed %d\n", __func__,
604                        __LINE__, error);
605                 return error;
606         }
607         ps3disk_major = error;
608
609         pr_info("%s:%u: registered block device major %d\n", __func__,
610                 __LINE__, ps3disk_major);
611
612         error = ps3_system_bus_driver_register(&ps3disk);
613         if (error)
614                 unregister_blkdev(ps3disk_major, DEVICE_NAME);
615
616         return error;
617 }
618
619 static void __exit ps3disk_exit(void)
620 {
621         ps3_system_bus_driver_unregister(&ps3disk);
622         unregister_blkdev(ps3disk_major, DEVICE_NAME);
623 }
624
625 module_init(ps3disk_init);
626 module_exit(ps3disk_exit);
627
628 MODULE_LICENSE("GPL");
629 MODULE_DESCRIPTION("PS3 Disk Storage Driver");
630 MODULE_AUTHOR("Sony Corporation");
631 MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_DISK);