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