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