Merge git://git.infradead.org/battery-2.6
[sfrench/cifs-2.6.git] / drivers / block / viodasd.c
1 /* -*- linux-c -*-
2  * viodasd.c
3  *  Authors: Dave Boutcher <boutcher@us.ibm.com>
4  *           Ryan Arnold <ryanarn@us.ibm.com>
5  *           Colin Devilbiss <devilbis@us.ibm.com>
6  *           Stephen Rothwell <sfr@au1.ibm.com>
7  *
8  * (C) Copyright 2000-2004 IBM Corporation
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of the
13  * License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  * This routine provides access to disk space (termed "DASD" in historical
25  * IBM terms) owned and managed by an OS/400 partition running on the
26  * same box as this Linux partition.
27  *
28  * All disk operations are performed by sending messages back and forth to
29  * the OS/400 partition.
30  */
31 #include <linux/major.h>
32 #include <linux/fs.h>
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/blkdev.h>
36 #include <linux/genhd.h>
37 #include <linux/hdreg.h>
38 #include <linux/errno.h>
39 #include <linux/init.h>
40 #include <linux/string.h>
41 #include <linux/dma-mapping.h>
42 #include <linux/completion.h>
43 #include <linux/device.h>
44 #include <linux/scatterlist.h>
45
46 #include <asm/uaccess.h>
47 #include <asm/vio.h>
48 #include <asm/iseries/hv_types.h>
49 #include <asm/iseries/hv_lp_event.h>
50 #include <asm/iseries/hv_lp_config.h>
51 #include <asm/iseries/vio.h>
52 #include <asm/firmware.h>
53
54 MODULE_DESCRIPTION("iSeries Virtual DASD");
55 MODULE_AUTHOR("Dave Boutcher");
56 MODULE_LICENSE("GPL");
57
58 /*
59  * We only support 7 partitions per physical disk....so with minor
60  * numbers 0-255 we get a maximum of 32 disks.
61  */
62 #define VIOD_GENHD_NAME         "iseries/vd"
63
64 #define VIOD_VERS               "1.64"
65
66 #define VIOD_KERN_WARNING       KERN_WARNING "viod: "
67 #define VIOD_KERN_INFO          KERN_INFO "viod: "
68
69 enum {
70         PARTITION_SHIFT = 3,
71         MAX_DISKNO = HVMAXARCHITECTEDVIRTUALDISKS,
72         MAX_DISK_NAME = sizeof(((struct gendisk *)0)->disk_name)
73 };
74
75 static DEFINE_SPINLOCK(viodasd_spinlock);
76
77 #define VIOMAXREQ               16
78
79 #define DEVICE_NO(cell) ((struct viodasd_device *)(cell) - &viodasd_devices[0])
80
81 struct viodasd_waitevent {
82         struct completion       com;
83         int                     rc;
84         u16                     sub_result;
85         int                     max_disk;       /* open */
86 };
87
88 static const struct vio_error_entry viodasd_err_table[] = {
89         { 0x0201, EINVAL, "Invalid Range" },
90         { 0x0202, EINVAL, "Invalid Token" },
91         { 0x0203, EIO, "DMA Error" },
92         { 0x0204, EIO, "Use Error" },
93         { 0x0205, EIO, "Release Error" },
94         { 0x0206, EINVAL, "Invalid Disk" },
95         { 0x0207, EBUSY, "Cant Lock" },
96         { 0x0208, EIO, "Already Locked" },
97         { 0x0209, EIO, "Already Unlocked" },
98         { 0x020A, EIO, "Invalid Arg" },
99         { 0x020B, EIO, "Bad IFS File" },
100         { 0x020C, EROFS, "Read Only Device" },
101         { 0x02FF, EIO, "Internal Error" },
102         { 0x0000, 0, NULL },
103 };
104
105 /*
106  * Figure out the biggest I/O request (in sectors) we can accept
107  */
108 #define VIODASD_MAXSECTORS (4096 / 512 * VIOMAXBLOCKDMA)
109
110 /*
111  * Number of disk I/O requests we've sent to OS/400
112  */
113 static int num_req_outstanding;
114
115 /*
116  * This is our internal structure for keeping track of disk devices
117  */
118 struct viodasd_device {
119         u16             cylinders;
120         u16             tracks;
121         u16             sectors;
122         u16             bytes_per_sector;
123         u64             size;
124         int             read_only;
125         spinlock_t      q_lock;
126         struct gendisk  *disk;
127         struct device   *dev;
128 } viodasd_devices[MAX_DISKNO];
129
130 /*
131  * External open entry point.
132  */
133 static int viodasd_open(struct inode *ino, struct file *fil)
134 {
135         struct viodasd_device *d = ino->i_bdev->bd_disk->private_data;
136         HvLpEvent_Rc hvrc;
137         struct viodasd_waitevent we;
138         u16 flags = 0;
139
140         if (d->read_only) {
141                 if ((fil != NULL) && (fil->f_mode & FMODE_WRITE))
142                         return -EROFS;
143                 flags = vioblockflags_ro;
144         }
145
146         init_completion(&we.com);
147
148         /* Send the open event to OS/400 */
149         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
150                         HvLpEvent_Type_VirtualIo,
151                         viomajorsubtype_blockio | vioblockopen,
152                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
153                         viopath_sourceinst(viopath_hostLp),
154                         viopath_targetinst(viopath_hostLp),
155                         (u64)(unsigned long)&we, VIOVERSION << 16,
156                         ((u64)DEVICE_NO(d) << 48) | ((u64)flags << 32),
157                         0, 0, 0);
158         if (hvrc != 0) {
159                 printk(VIOD_KERN_WARNING "HV open failed %d\n", (int)hvrc);
160                 return -EIO;
161         }
162
163         wait_for_completion(&we.com);
164
165         /* Check the return code */
166         if (we.rc != 0) {
167                 const struct vio_error_entry *err =
168                         vio_lookup_rc(viodasd_err_table, we.sub_result);
169
170                 printk(VIOD_KERN_WARNING
171                                 "bad rc opening disk: %d:0x%04x (%s)\n",
172                                 (int)we.rc, we.sub_result, err->msg);
173                 return -EIO;
174         }
175
176         return 0;
177 }
178
179 /*
180  * External release entry point.
181  */
182 static int viodasd_release(struct inode *ino, struct file *fil)
183 {
184         struct viodasd_device *d = ino->i_bdev->bd_disk->private_data;
185         HvLpEvent_Rc hvrc;
186
187         /* Send the event to OS/400.  We DON'T expect a response */
188         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
189                         HvLpEvent_Type_VirtualIo,
190                         viomajorsubtype_blockio | vioblockclose,
191                         HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
192                         viopath_sourceinst(viopath_hostLp),
193                         viopath_targetinst(viopath_hostLp),
194                         0, VIOVERSION << 16,
195                         ((u64)DEVICE_NO(d) << 48) /* | ((u64)flags << 32) */,
196                         0, 0, 0);
197         if (hvrc != 0)
198                 printk(VIOD_KERN_WARNING "HV close call failed %d\n",
199                                 (int)hvrc);
200         return 0;
201 }
202
203
204 /* External ioctl entry point.
205  */
206 static int viodasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
207 {
208         struct gendisk *disk = bdev->bd_disk;
209         struct viodasd_device *d = disk->private_data;
210
211         geo->sectors = d->sectors ? d->sectors : 32;
212         geo->heads = d->tracks ? d->tracks  : 64;
213         geo->cylinders = d->cylinders ? d->cylinders :
214                 get_capacity(disk) / (geo->sectors * geo->heads);
215
216         return 0;
217 }
218
219 /*
220  * Our file operations table
221  */
222 static struct block_device_operations viodasd_fops = {
223         .owner = THIS_MODULE,
224         .open = viodasd_open,
225         .release = viodasd_release,
226         .getgeo = viodasd_getgeo,
227 };
228
229 /*
230  * End a request
231  */
232 static void viodasd_end_request(struct request *req, int uptodate,
233                 int num_sectors)
234 {
235         if (end_that_request_first(req, uptodate, num_sectors))
236                 return;
237         add_disk_randomness(req->rq_disk);
238         end_that_request_last(req, uptodate);
239 }
240
241 /*
242  * Send an actual I/O request to OS/400
243  */
244 static int send_request(struct request *req)
245 {
246         u64 start;
247         int direction;
248         int nsg;
249         u16 viocmd;
250         HvLpEvent_Rc hvrc;
251         struct vioblocklpevent *bevent;
252         struct HvLpEvent *hev;
253         struct scatterlist sg[VIOMAXBLOCKDMA];
254         int sgindex;
255         int statindex;
256         struct viodasd_device *d;
257         unsigned long flags;
258
259         start = (u64)req->sector << 9;
260
261         if (rq_data_dir(req) == READ) {
262                 direction = DMA_FROM_DEVICE;
263                 viocmd = viomajorsubtype_blockio | vioblockread;
264                 statindex = 0;
265         } else {
266                 direction = DMA_TO_DEVICE;
267                 viocmd = viomajorsubtype_blockio | vioblockwrite;
268                 statindex = 1;
269         }
270
271         d = req->rq_disk->private_data;
272
273         /* Now build the scatter-gather list */
274         sg_init_table(sg, VIOMAXBLOCKDMA);
275         nsg = blk_rq_map_sg(req->q, req, sg);
276         nsg = dma_map_sg(d->dev, sg, nsg, direction);
277
278         spin_lock_irqsave(&viodasd_spinlock, flags);
279         num_req_outstanding++;
280
281         /* This optimization handles a single DMA block */
282         if (nsg == 1)
283                 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
284                                 HvLpEvent_Type_VirtualIo, viocmd,
285                                 HvLpEvent_AckInd_DoAck,
286                                 HvLpEvent_AckType_ImmediateAck,
287                                 viopath_sourceinst(viopath_hostLp),
288                                 viopath_targetinst(viopath_hostLp),
289                                 (u64)(unsigned long)req, VIOVERSION << 16,
290                                 ((u64)DEVICE_NO(d) << 48), start,
291                                 ((u64)sg_dma_address(&sg[0])) << 32,
292                                 sg_dma_len(&sg[0]));
293         else {
294                 bevent = (struct vioblocklpevent *)
295                         vio_get_event_buffer(viomajorsubtype_blockio);
296                 if (bevent == NULL) {
297                         printk(VIOD_KERN_WARNING
298                                "error allocating disk event buffer\n");
299                         goto error_ret;
300                 }
301
302                 /*
303                  * Now build up the actual request.  Note that we store
304                  * the pointer to the request in the correlation
305                  * token so we can match the response up later
306                  */
307                 memset(bevent, 0, sizeof(struct vioblocklpevent));
308                 hev = &bevent->event;
309                 hev->flags = HV_LP_EVENT_VALID | HV_LP_EVENT_DO_ACK |
310                         HV_LP_EVENT_INT;
311                 hev->xType = HvLpEvent_Type_VirtualIo;
312                 hev->xSubtype = viocmd;
313                 hev->xSourceLp = HvLpConfig_getLpIndex();
314                 hev->xTargetLp = viopath_hostLp;
315                 hev->xSizeMinus1 =
316                         offsetof(struct vioblocklpevent, u.rw_data.dma_info) +
317                         (sizeof(bevent->u.rw_data.dma_info[0]) * nsg) - 1;
318                 hev->xSourceInstanceId = viopath_sourceinst(viopath_hostLp);
319                 hev->xTargetInstanceId = viopath_targetinst(viopath_hostLp);
320                 hev->xCorrelationToken = (u64)req;
321                 bevent->version = VIOVERSION;
322                 bevent->disk = DEVICE_NO(d);
323                 bevent->u.rw_data.offset = start;
324
325                 /*
326                  * Copy just the dma information from the sg list
327                  * into the request
328                  */
329                 for (sgindex = 0; sgindex < nsg; sgindex++) {
330                         bevent->u.rw_data.dma_info[sgindex].token =
331                                 sg_dma_address(&sg[sgindex]);
332                         bevent->u.rw_data.dma_info[sgindex].len =
333                                 sg_dma_len(&sg[sgindex]);
334                 }
335
336                 /* Send the request */
337                 hvrc = HvCallEvent_signalLpEvent(&bevent->event);
338                 vio_free_event_buffer(viomajorsubtype_blockio, bevent);
339         }
340
341         if (hvrc != HvLpEvent_Rc_Good) {
342                 printk(VIOD_KERN_WARNING
343                        "error sending disk event to OS/400 (rc %d)\n",
344                        (int)hvrc);
345                 goto error_ret;
346         }
347         spin_unlock_irqrestore(&viodasd_spinlock, flags);
348         return 0;
349
350 error_ret:
351         num_req_outstanding--;
352         spin_unlock_irqrestore(&viodasd_spinlock, flags);
353         dma_unmap_sg(d->dev, sg, nsg, direction);
354         return -1;
355 }
356
357 /*
358  * This is the external request processing routine
359  */
360 static void do_viodasd_request(struct request_queue *q)
361 {
362         struct request *req;
363
364         /*
365          * If we already have the maximum number of requests
366          * outstanding to OS/400 just bail out. We'll come
367          * back later.
368          */
369         while (num_req_outstanding < VIOMAXREQ) {
370                 req = elv_next_request(q);
371                 if (req == NULL)
372                         return;
373                 /* dequeue the current request from the queue */
374                 blkdev_dequeue_request(req);
375                 /* check that request contains a valid command */
376                 if (!blk_fs_request(req)) {
377                         viodasd_end_request(req, 0, req->hard_nr_sectors);
378                         continue;
379                 }
380                 /* Try sending the request */
381                 if (send_request(req) != 0)
382                         viodasd_end_request(req, 0, req->hard_nr_sectors);
383         }
384 }
385
386 /*
387  * Probe a single disk and fill in the viodasd_device structure
388  * for it.
389  */
390 static int probe_disk(struct viodasd_device *d)
391 {
392         HvLpEvent_Rc hvrc;
393         struct viodasd_waitevent we;
394         int dev_no = DEVICE_NO(d);
395         struct gendisk *g;
396         struct request_queue *q;
397         u16 flags = 0;
398
399 retry:
400         init_completion(&we.com);
401
402         /* Send the open event to OS/400 */
403         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
404                         HvLpEvent_Type_VirtualIo,
405                         viomajorsubtype_blockio | vioblockopen,
406                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
407                         viopath_sourceinst(viopath_hostLp),
408                         viopath_targetinst(viopath_hostLp),
409                         (u64)(unsigned long)&we, VIOVERSION << 16,
410                         ((u64)dev_no << 48) | ((u64)flags<< 32),
411                         0, 0, 0);
412         if (hvrc != 0) {
413                 printk(VIOD_KERN_WARNING "bad rc on HV open %d\n", (int)hvrc);
414                 return 0;
415         }
416
417         wait_for_completion(&we.com);
418
419         if (we.rc != 0) {
420                 if (flags != 0)
421                         return 0;
422                 /* try again with read only flag set */
423                 flags = vioblockflags_ro;
424                 goto retry;
425         }
426         if (we.max_disk > (MAX_DISKNO - 1)) {
427                 static int warned;
428
429                 if (warned == 0) {
430                         warned++;
431                         printk(VIOD_KERN_INFO
432                                 "Only examining the first %d "
433                                 "of %d disks connected\n",
434                                 MAX_DISKNO, we.max_disk + 1);
435                 }
436         }
437
438         /* Send the close event to OS/400.  We DON'T expect a response */
439         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
440                         HvLpEvent_Type_VirtualIo,
441                         viomajorsubtype_blockio | vioblockclose,
442                         HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
443                         viopath_sourceinst(viopath_hostLp),
444                         viopath_targetinst(viopath_hostLp),
445                         0, VIOVERSION << 16,
446                         ((u64)dev_no << 48) | ((u64)flags << 32),
447                         0, 0, 0);
448         if (hvrc != 0) {
449                 printk(VIOD_KERN_WARNING
450                        "bad rc sending event to OS/400 %d\n", (int)hvrc);
451                 return 0;
452         }
453
454         if (d->dev == NULL) {
455                 /* this is when we reprobe for new disks */
456                 if (vio_create_viodasd(dev_no) == NULL) {
457                         printk(VIOD_KERN_WARNING
458                                 "cannot allocate virtual device for disk %d\n",
459                                 dev_no);
460                         return 0;
461                 }
462                 /*
463                  * The vio_create_viodasd will have recursed into this
464                  * routine with d->dev set to the new vio device and
465                  * will finish the setup of the disk below.
466                  */
467                 return 1;
468         }
469
470         /* create the request queue for the disk */
471         spin_lock_init(&d->q_lock);
472         q = blk_init_queue(do_viodasd_request, &d->q_lock);
473         if (q == NULL) {
474                 printk(VIOD_KERN_WARNING "cannot allocate queue for disk %d\n",
475                                 dev_no);
476                 return 0;
477         }
478         g = alloc_disk(1 << PARTITION_SHIFT);
479         if (g == NULL) {
480                 printk(VIOD_KERN_WARNING
481                                 "cannot allocate disk structure for disk %d\n",
482                                 dev_no);
483                 blk_cleanup_queue(q);
484                 return 0;
485         }
486
487         d->disk = g;
488         blk_queue_max_hw_segments(q, VIOMAXBLOCKDMA);
489         blk_queue_max_phys_segments(q, VIOMAXBLOCKDMA);
490         blk_queue_max_sectors(q, VIODASD_MAXSECTORS);
491         g->major = VIODASD_MAJOR;
492         g->first_minor = dev_no << PARTITION_SHIFT;
493         if (dev_no >= 26)
494                 snprintf(g->disk_name, sizeof(g->disk_name),
495                                 VIOD_GENHD_NAME "%c%c",
496                                 'a' + (dev_no / 26) - 1, 'a' + (dev_no % 26));
497         else
498                 snprintf(g->disk_name, sizeof(g->disk_name),
499                                 VIOD_GENHD_NAME "%c", 'a' + (dev_no % 26));
500         g->fops = &viodasd_fops;
501         g->queue = q;
502         g->private_data = d;
503         g->driverfs_dev = d->dev;
504         set_capacity(g, d->size >> 9);
505
506         printk(VIOD_KERN_INFO "disk %d: %lu sectors (%lu MB) "
507                         "CHS=%d/%d/%d sector size %d%s\n",
508                         dev_no, (unsigned long)(d->size >> 9),
509                         (unsigned long)(d->size >> 20),
510                         (int)d->cylinders, (int)d->tracks,
511                         (int)d->sectors, (int)d->bytes_per_sector,
512                         d->read_only ? " (RO)" : "");
513
514         /* register us in the global list */
515         add_disk(g);
516         return 1;
517 }
518
519 /* returns the total number of scatterlist elements converted */
520 static int block_event_to_scatterlist(const struct vioblocklpevent *bevent,
521                 struct scatterlist *sg, int *total_len)
522 {
523         int i, numsg;
524         const struct rw_data *rw_data = &bevent->u.rw_data;
525         static const int offset =
526                 offsetof(struct vioblocklpevent, u.rw_data.dma_info);
527         static const int element_size = sizeof(rw_data->dma_info[0]);
528
529         numsg = ((bevent->event.xSizeMinus1 + 1) - offset) / element_size;
530         if (numsg > VIOMAXBLOCKDMA)
531                 numsg = VIOMAXBLOCKDMA;
532
533         *total_len = 0;
534         memset(sg, 0, sizeof(sg[0]) * VIOMAXBLOCKDMA);
535
536         for (i = 0; (i < numsg) && (rw_data->dma_info[i].len > 0); ++i) {
537                 sg_dma_address(&sg[i]) = rw_data->dma_info[i].token;
538                 sg_dma_len(&sg[i]) = rw_data->dma_info[i].len;
539                 *total_len += rw_data->dma_info[i].len;
540         }
541         return i;
542 }
543
544 /*
545  * Restart all queues, starting with the one _after_ the disk given,
546  * thus reducing the chance of starvation of higher numbered disks.
547  */
548 static void viodasd_restart_all_queues_starting_from(int first_index)
549 {
550         int i;
551
552         for (i = first_index + 1; i < MAX_DISKNO; ++i)
553                 if (viodasd_devices[i].disk)
554                         blk_run_queue(viodasd_devices[i].disk->queue);
555         for (i = 0; i <= first_index; ++i)
556                 if (viodasd_devices[i].disk)
557                         blk_run_queue(viodasd_devices[i].disk->queue);
558 }
559
560 /*
561  * For read and write requests, decrement the number of outstanding requests,
562  * Free the DMA buffers we allocated.
563  */
564 static int viodasd_handle_read_write(struct vioblocklpevent *bevent)
565 {
566         int num_sg, num_sect, pci_direction, total_len;
567         struct request *req;
568         struct scatterlist sg[VIOMAXBLOCKDMA];
569         struct HvLpEvent *event = &bevent->event;
570         unsigned long irq_flags;
571         struct viodasd_device *d;
572         int error;
573         spinlock_t *qlock;
574
575         num_sg = block_event_to_scatterlist(bevent, sg, &total_len);
576         num_sect = total_len >> 9;
577         if (event->xSubtype == (viomajorsubtype_blockio | vioblockread))
578                 pci_direction = DMA_FROM_DEVICE;
579         else
580                 pci_direction = DMA_TO_DEVICE;
581         req = (struct request *)bevent->event.xCorrelationToken;
582         d = req->rq_disk->private_data;
583
584         dma_unmap_sg(d->dev, sg, num_sg, pci_direction);
585
586         /*
587          * Since this is running in interrupt mode, we need to make sure
588          * we're not stepping on any global I/O operations
589          */
590         spin_lock_irqsave(&viodasd_spinlock, irq_flags);
591         num_req_outstanding--;
592         spin_unlock_irqrestore(&viodasd_spinlock, irq_flags);
593
594         error = event->xRc != HvLpEvent_Rc_Good;
595         if (error) {
596                 const struct vio_error_entry *err;
597                 err = vio_lookup_rc(viodasd_err_table, bevent->sub_result);
598                 printk(VIOD_KERN_WARNING "read/write error %d:0x%04x (%s)\n",
599                                 event->xRc, bevent->sub_result, err->msg);
600                 num_sect = req->hard_nr_sectors;
601         }
602         qlock = req->q->queue_lock;
603         spin_lock_irqsave(qlock, irq_flags);
604         viodasd_end_request(req, !error, num_sect);
605         spin_unlock_irqrestore(qlock, irq_flags);
606
607         /* Finally, try to get more requests off of this device's queue */
608         viodasd_restart_all_queues_starting_from(DEVICE_NO(d));
609
610         return 0;
611 }
612
613 /* This routine handles incoming block LP events */
614 static void handle_block_event(struct HvLpEvent *event)
615 {
616         struct vioblocklpevent *bevent = (struct vioblocklpevent *)event;
617         struct viodasd_waitevent *pwe;
618
619         if (event == NULL)
620                 /* Notification that a partition went away! */
621                 return;
622         /* First, we should NEVER get an int here...only acks */
623         if (hvlpevent_is_int(event)) {
624                 printk(VIOD_KERN_WARNING
625                        "Yikes! got an int in viodasd event handler!\n");
626                 if (hvlpevent_need_ack(event)) {
627                         event->xRc = HvLpEvent_Rc_InvalidSubtype;
628                         HvCallEvent_ackLpEvent(event);
629                 }
630         }
631
632         switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
633         case vioblockopen:
634                 /*
635                  * Handle a response to an open request.  We get all the
636                  * disk information in the response, so update it.  The
637                  * correlation token contains a pointer to a waitevent
638                  * structure that has a completion in it.  update the
639                  * return code in the waitevent structure and post the
640                  * completion to wake up the guy who sent the request
641                  */
642                 pwe = (struct viodasd_waitevent *)event->xCorrelationToken;
643                 pwe->rc = event->xRc;
644                 pwe->sub_result = bevent->sub_result;
645                 if (event->xRc == HvLpEvent_Rc_Good) {
646                         const struct open_data *data = &bevent->u.open_data;
647                         struct viodasd_device *device =
648                                 &viodasd_devices[bevent->disk];
649                         device->read_only =
650                                 bevent->flags & vioblockflags_ro;
651                         device->size = data->disk_size;
652                         device->cylinders = data->cylinders;
653                         device->tracks = data->tracks;
654                         device->sectors = data->sectors;
655                         device->bytes_per_sector = data->bytes_per_sector;
656                         pwe->max_disk = data->max_disk;
657                 }
658                 complete(&pwe->com);
659                 break;
660         case vioblockclose:
661                 break;
662         case vioblockread:
663         case vioblockwrite:
664                 viodasd_handle_read_write(bevent);
665                 break;
666
667         default:
668                 printk(VIOD_KERN_WARNING "invalid subtype!");
669                 if (hvlpevent_need_ack(event)) {
670                         event->xRc = HvLpEvent_Rc_InvalidSubtype;
671                         HvCallEvent_ackLpEvent(event);
672                 }
673         }
674 }
675
676 /*
677  * Get the driver to reprobe for more disks.
678  */
679 static ssize_t probe_disks(struct device_driver *drv, const char *buf,
680                 size_t count)
681 {
682         struct viodasd_device *d;
683
684         for (d = viodasd_devices; d < &viodasd_devices[MAX_DISKNO]; d++) {
685                 if (d->disk == NULL)
686                         probe_disk(d);
687         }
688         return count;
689 }
690 static DRIVER_ATTR(probe, S_IWUSR, NULL, probe_disks);
691
692 static int viodasd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
693 {
694         struct viodasd_device *d = &viodasd_devices[vdev->unit_address];
695
696         d->dev = &vdev->dev;
697         if (!probe_disk(d))
698                 return -ENODEV;
699         return 0;
700 }
701
702 static int viodasd_remove(struct vio_dev *vdev)
703 {
704         struct viodasd_device *d;
705
706         d = &viodasd_devices[vdev->unit_address];
707         if (d->disk) {
708                 del_gendisk(d->disk);
709                 blk_cleanup_queue(d->disk->queue);
710                 put_disk(d->disk);
711                 d->disk = NULL;
712         }
713         d->dev = NULL;
714         return 0;
715 }
716
717 /**
718  * viodasd_device_table: Used by vio.c to match devices that we
719  * support.
720  */
721 static struct vio_device_id viodasd_device_table[] __devinitdata = {
722         { "block", "IBM,iSeries-viodasd" },
723         { "", "" }
724 };
725 MODULE_DEVICE_TABLE(vio, viodasd_device_table);
726
727 static struct vio_driver viodasd_driver = {
728         .id_table = viodasd_device_table,
729         .probe = viodasd_probe,
730         .remove = viodasd_remove,
731         .driver = {
732                 .name = "viodasd",
733                 .owner = THIS_MODULE,
734         }
735 };
736
737 static int need_delete_probe;
738
739 /*
740  * Initialize the whole device driver.  Handle module and non-module
741  * versions
742  */
743 static int __init viodasd_init(void)
744 {
745         int rc;
746
747         if (!firmware_has_feature(FW_FEATURE_ISERIES)) {
748                 rc = -ENODEV;
749                 goto early_fail;
750         }
751
752         /* Try to open to our host lp */
753         if (viopath_hostLp == HvLpIndexInvalid)
754                 vio_set_hostlp();
755
756         if (viopath_hostLp == HvLpIndexInvalid) {
757                 printk(VIOD_KERN_WARNING "invalid hosting partition\n");
758                 rc = -EIO;
759                 goto early_fail;
760         }
761
762         printk(VIOD_KERN_INFO "vers " VIOD_VERS ", hosting partition %d\n",
763                         viopath_hostLp);
764
765         /* register the block device */
766         rc =  register_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
767         if (rc) {
768                 printk(VIOD_KERN_WARNING
769                                 "Unable to get major number %d for %s\n",
770                                 VIODASD_MAJOR, VIOD_GENHD_NAME);
771                 goto early_fail;
772         }
773         /* Actually open the path to the hosting partition */
774         rc = viopath_open(viopath_hostLp, viomajorsubtype_blockio,
775                                 VIOMAXREQ + 2);
776         if (rc) {
777                 printk(VIOD_KERN_WARNING
778                        "error opening path to host partition %d\n",
779                        viopath_hostLp);
780                 goto unregister_blk;
781         }
782
783         /* Initialize our request handler */
784         vio_setHandler(viomajorsubtype_blockio, handle_block_event);
785
786         rc = vio_register_driver(&viodasd_driver);
787         if (rc) {
788                 printk(VIOD_KERN_WARNING "vio_register_driver failed\n");
789                 goto unset_handler;
790         }
791
792         /*
793          * If this call fails, it just means that we cannot dynamically
794          * add virtual disks, but the driver will still work fine for
795          * all existing disk, so ignore the failure.
796          */
797         if (!driver_create_file(&viodasd_driver.driver, &driver_attr_probe))
798                 need_delete_probe = 1;
799
800         return 0;
801
802 unset_handler:
803         vio_clearHandler(viomajorsubtype_blockio);
804         viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
805 unregister_blk:
806         unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
807 early_fail:
808         return rc;
809 }
810 module_init(viodasd_init);
811
812 void __exit viodasd_exit(void)
813 {
814         if (need_delete_probe)
815                 driver_remove_file(&viodasd_driver.driver, &driver_attr_probe);
816         vio_unregister_driver(&viodasd_driver);
817         vio_clearHandler(viomajorsubtype_blockio);
818         viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
819         unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
820 }
821 module_exit(viodasd_exit);