Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/roland...
[linux-2.6] / 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/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
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 DEFINE_SPINLOCK(viodasd_spinlock);
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_getgeo(struct block_device *bdev, struct hd_geometry *geo)
251 {
252         struct gendisk *disk = bdev->bd_disk;
253         struct viodasd_device *d = disk->private_data;
254
255         geo->sectors = d->sectors ? d->sectors : 0;
256         geo->heads = d->tracks ? d->tracks  : 64;
257         geo->cylinders = d->cylinders ? d->cylinders :
258                 get_capacity(disk) / (geo->cylinders * geo->heads);
259
260         return 0;
261 }
262
263 /*
264  * Our file operations table
265  */
266 static struct block_device_operations viodasd_fops = {
267         .owner = THIS_MODULE,
268         .open = viodasd_open,
269         .release = viodasd_release,
270         .getgeo = viodasd_getgeo,
271 };
272
273 /*
274  * End a request
275  */
276 static void viodasd_end_request(struct request *req, int uptodate,
277                 int num_sectors)
278 {
279         if (end_that_request_first(req, uptodate, num_sectors))
280                 return;
281         add_disk_randomness(req->rq_disk);
282         end_that_request_last(req, uptodate);
283 }
284
285 /*
286  * Send an actual I/O request to OS/400
287  */
288 static int send_request(struct request *req)
289 {
290         u64 start;
291         int direction;
292         int nsg;
293         u16 viocmd;
294         HvLpEvent_Rc hvrc;
295         struct vioblocklpevent *bevent;
296         struct scatterlist sg[VIOMAXBLOCKDMA];
297         int sgindex;
298         int statindex;
299         struct viodasd_device *d;
300         unsigned long flags;
301
302         start = (u64)req->sector << 9;
303
304         if (rq_data_dir(req) == READ) {
305                 direction = DMA_FROM_DEVICE;
306                 viocmd = viomajorsubtype_blockio | vioblockread;
307                 statindex = 0;
308         } else {
309                 direction = DMA_TO_DEVICE;
310                 viocmd = viomajorsubtype_blockio | vioblockwrite;
311                 statindex = 1;
312         }
313
314         d = req->rq_disk->private_data;
315
316         /* Now build the scatter-gather list */
317         nsg = blk_rq_map_sg(req->q, req, sg);
318         nsg = dma_map_sg(d->dev, sg, nsg, direction);
319
320         spin_lock_irqsave(&viodasd_spinlock, flags);
321         num_req_outstanding++;
322
323         /* This optimization handles a single DMA block */
324         if (nsg == 1)
325                 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
326                                 HvLpEvent_Type_VirtualIo, viocmd,
327                                 HvLpEvent_AckInd_DoAck,
328                                 HvLpEvent_AckType_ImmediateAck,
329                                 viopath_sourceinst(viopath_hostLp),
330                                 viopath_targetinst(viopath_hostLp),
331                                 (u64)(unsigned long)req, VIOVERSION << 16,
332                                 ((u64)DEVICE_NO(d) << 48), start,
333                                 ((u64)sg_dma_address(&sg[0])) << 32,
334                                 sg_dma_len(&sg[0]));
335         else {
336                 bevent = (struct vioblocklpevent *)
337                         vio_get_event_buffer(viomajorsubtype_blockio);
338                 if (bevent == NULL) {
339                         printk(VIOD_KERN_WARNING
340                                "error allocating disk event buffer\n");
341                         goto error_ret;
342                 }
343
344                 /*
345                  * Now build up the actual request.  Note that we store
346                  * the pointer to the request in the correlation
347                  * token so we can match the response up later
348                  */
349                 memset(bevent, 0, sizeof(struct vioblocklpevent));
350                 bevent->event.xFlags.xValid = 1;
351                 bevent->event.xFlags.xFunction = HvLpEvent_Function_Int;
352                 bevent->event.xFlags.xAckInd = HvLpEvent_AckInd_DoAck;
353                 bevent->event.xFlags.xAckType = HvLpEvent_AckType_ImmediateAck;
354                 bevent->event.xType = HvLpEvent_Type_VirtualIo;
355                 bevent->event.xSubtype = viocmd;
356                 bevent->event.xSourceLp = HvLpConfig_getLpIndex();
357                 bevent->event.xTargetLp = viopath_hostLp;
358                 bevent->event.xSizeMinus1 =
359                         offsetof(struct vioblocklpevent, u.rw_data.dma_info) +
360                         (sizeof(bevent->u.rw_data.dma_info[0]) * nsg) - 1;
361                 bevent->event.xSourceInstanceId =
362                         viopath_sourceinst(viopath_hostLp);
363                 bevent->event.xTargetInstanceId =
364                         viopath_targetinst(viopath_hostLp);
365                 bevent->event.xCorrelationToken = (u64)req;
366                 bevent->version = VIOVERSION;
367                 bevent->disk = DEVICE_NO(d);
368                 bevent->u.rw_data.offset = start;
369
370                 /*
371                  * Copy just the dma information from the sg list
372                  * into the request
373                  */
374                 for (sgindex = 0; sgindex < nsg; sgindex++) {
375                         bevent->u.rw_data.dma_info[sgindex].token =
376                                 sg_dma_address(&sg[sgindex]);
377                         bevent->u.rw_data.dma_info[sgindex].len =
378                                 sg_dma_len(&sg[sgindex]);
379                 }
380
381                 /* Send the request */
382                 hvrc = HvCallEvent_signalLpEvent(&bevent->event);
383                 vio_free_event_buffer(viomajorsubtype_blockio, bevent);
384         }
385
386         if (hvrc != HvLpEvent_Rc_Good) {
387                 printk(VIOD_KERN_WARNING
388                        "error sending disk event to OS/400 (rc %d)\n",
389                        (int)hvrc);
390                 goto error_ret;
391         }
392         spin_unlock_irqrestore(&viodasd_spinlock, flags);
393         return 0;
394
395 error_ret:
396         num_req_outstanding--;
397         spin_unlock_irqrestore(&viodasd_spinlock, flags);
398         dma_unmap_sg(d->dev, sg, nsg, direction);
399         return -1;
400 }
401
402 /*
403  * This is the external request processing routine
404  */
405 static void do_viodasd_request(request_queue_t *q)
406 {
407         struct request *req;
408
409         /*
410          * If we already have the maximum number of requests
411          * outstanding to OS/400 just bail out. We'll come
412          * back later.
413          */
414         while (num_req_outstanding < VIOMAXREQ) {
415                 req = elv_next_request(q);
416                 if (req == NULL)
417                         return;
418                 /* dequeue the current request from the queue */
419                 blkdev_dequeue_request(req);
420                 /* check that request contains a valid command */
421                 if (!blk_fs_request(req)) {
422                         viodasd_end_request(req, 0, req->hard_nr_sectors);
423                         continue;
424                 }
425                 /* Try sending the request */
426                 if (send_request(req) != 0)
427                         viodasd_end_request(req, 0, req->hard_nr_sectors);
428         }
429 }
430
431 /*
432  * Probe a single disk and fill in the viodasd_device structure
433  * for it.
434  */
435 static void probe_disk(struct viodasd_device *d)
436 {
437         HvLpEvent_Rc hvrc;
438         struct viodasd_waitevent we;
439         int dev_no = DEVICE_NO(d);
440         struct gendisk *g;
441         struct request_queue *q;
442         u16 flags = 0;
443
444 retry:
445         init_completion(&we.com);
446
447         /* Send the open event to OS/400 */
448         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
449                         HvLpEvent_Type_VirtualIo,
450                         viomajorsubtype_blockio | vioblockopen,
451                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
452                         viopath_sourceinst(viopath_hostLp),
453                         viopath_targetinst(viopath_hostLp),
454                         (u64)(unsigned long)&we, VIOVERSION << 16,
455                         ((u64)dev_no << 48) | ((u64)flags<< 32),
456                         0, 0, 0);
457         if (hvrc != 0) {
458                 printk(VIOD_KERN_WARNING "bad rc on HV open %d\n", (int)hvrc);
459                 return;
460         }
461
462         wait_for_completion(&we.com);
463
464         if (we.rc != 0) {
465                 if (flags != 0)
466                         return;
467                 /* try again with read only flag set */
468                 flags = vioblockflags_ro;
469                 goto retry;
470         }
471         if (we.max_disk > (MAX_DISKNO - 1)) {
472                 static int warned;
473
474                 if (warned == 0) {
475                         warned++;
476                         printk(VIOD_KERN_INFO
477                                 "Only examining the first %d "
478                                 "of %d disks connected\n",
479                                 MAX_DISKNO, we.max_disk + 1);
480                 }
481         }
482
483         /* Send the close event to OS/400.  We DON'T expect a response */
484         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
485                         HvLpEvent_Type_VirtualIo,
486                         viomajorsubtype_blockio | vioblockclose,
487                         HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
488                         viopath_sourceinst(viopath_hostLp),
489                         viopath_targetinst(viopath_hostLp),
490                         0, VIOVERSION << 16,
491                         ((u64)dev_no << 48) | ((u64)flags << 32),
492                         0, 0, 0);
493         if (hvrc != 0) {
494                 printk(VIOD_KERN_WARNING
495                        "bad rc sending event to OS/400 %d\n", (int)hvrc);
496                 return;
497         }
498         /* create the request queue for the disk */
499         spin_lock_init(&d->q_lock);
500         q = blk_init_queue(do_viodasd_request, &d->q_lock);
501         if (q == NULL) {
502                 printk(VIOD_KERN_WARNING "cannot allocate queue for disk %d\n",
503                                 dev_no);
504                 return;
505         }
506         g = alloc_disk(1 << PARTITION_SHIFT);
507         if (g == NULL) {
508                 printk(VIOD_KERN_WARNING
509                                 "cannot allocate disk structure for disk %d\n",
510                                 dev_no);
511                 blk_cleanup_queue(q);
512                 return;
513         }
514
515         d->disk = g;
516         blk_queue_max_hw_segments(q, VIOMAXBLOCKDMA);
517         blk_queue_max_phys_segments(q, VIOMAXBLOCKDMA);
518         blk_queue_max_sectors(q, VIODASD_MAXSECTORS);
519         g->major = VIODASD_MAJOR;
520         g->first_minor = dev_no << PARTITION_SHIFT;
521         if (dev_no >= 26)
522                 snprintf(g->disk_name, sizeof(g->disk_name),
523                                 VIOD_GENHD_NAME "%c%c",
524                                 'a' + (dev_no / 26) - 1, 'a' + (dev_no % 26));
525         else
526                 snprintf(g->disk_name, sizeof(g->disk_name),
527                                 VIOD_GENHD_NAME "%c", 'a' + (dev_no % 26));
528         snprintf(g->devfs_name, sizeof(g->devfs_name),
529                         "%s%d", VIOD_GENHD_DEVFS_NAME, dev_no);
530         g->fops = &viodasd_fops;
531         g->queue = q;
532         g->private_data = d;
533         g->driverfs_dev = d->dev;
534         set_capacity(g, d->size >> 9);
535
536         printk(VIOD_KERN_INFO "disk %d: %lu sectors (%lu MB) "
537                         "CHS=%d/%d/%d sector size %d%s\n",
538                         dev_no, (unsigned long)(d->size >> 9),
539                         (unsigned long)(d->size >> 20),
540                         (int)d->cylinders, (int)d->tracks,
541                         (int)d->sectors, (int)d->bytes_per_sector,
542                         d->read_only ? " (RO)" : "");
543
544         /* register us in the global list */
545         add_disk(g);
546 }
547
548 /* returns the total number of scatterlist elements converted */
549 static int block_event_to_scatterlist(const struct vioblocklpevent *bevent,
550                 struct scatterlist *sg, int *total_len)
551 {
552         int i, numsg;
553         const struct rw_data *rw_data = &bevent->u.rw_data;
554         static const int offset =
555                 offsetof(struct vioblocklpevent, u.rw_data.dma_info);
556         static const int element_size = sizeof(rw_data->dma_info[0]);
557
558         numsg = ((bevent->event.xSizeMinus1 + 1) - offset) / element_size;
559         if (numsg > VIOMAXBLOCKDMA)
560                 numsg = VIOMAXBLOCKDMA;
561
562         *total_len = 0;
563         memset(sg, 0, sizeof(sg[0]) * VIOMAXBLOCKDMA);
564
565         for (i = 0; (i < numsg) && (rw_data->dma_info[i].len > 0); ++i) {
566                 sg_dma_address(&sg[i]) = rw_data->dma_info[i].token;
567                 sg_dma_len(&sg[i]) = rw_data->dma_info[i].len;
568                 *total_len += rw_data->dma_info[i].len;
569         }
570         return i;
571 }
572
573 /*
574  * Restart all queues, starting with the one _after_ the disk given,
575  * thus reducing the chance of starvation of higher numbered disks.
576  */
577 static void viodasd_restart_all_queues_starting_from(int first_index)
578 {
579         int i;
580
581         for (i = first_index + 1; i < MAX_DISKNO; ++i)
582                 if (viodasd_devices[i].disk)
583                         blk_run_queue(viodasd_devices[i].disk->queue);
584         for (i = 0; i <= first_index; ++i)
585                 if (viodasd_devices[i].disk)
586                         blk_run_queue(viodasd_devices[i].disk->queue);
587 }
588
589 /*
590  * For read and write requests, decrement the number of outstanding requests,
591  * Free the DMA buffers we allocated.
592  */
593 static int viodasd_handle_read_write(struct vioblocklpevent *bevent)
594 {
595         int num_sg, num_sect, pci_direction, total_len;
596         struct request *req;
597         struct scatterlist sg[VIOMAXBLOCKDMA];
598         struct HvLpEvent *event = &bevent->event;
599         unsigned long irq_flags;
600         struct viodasd_device *d;
601         int error;
602         spinlock_t *qlock;
603
604         num_sg = block_event_to_scatterlist(bevent, sg, &total_len);
605         num_sect = total_len >> 9;
606         if (event->xSubtype == (viomajorsubtype_blockio | vioblockread))
607                 pci_direction = DMA_FROM_DEVICE;
608         else
609                 pci_direction = DMA_TO_DEVICE;
610         req = (struct request *)bevent->event.xCorrelationToken;
611         d = req->rq_disk->private_data;
612
613         dma_unmap_sg(d->dev, sg, num_sg, pci_direction);
614
615         /*
616          * Since this is running in interrupt mode, we need to make sure
617          * we're not stepping on any global I/O operations
618          */
619         spin_lock_irqsave(&viodasd_spinlock, irq_flags);
620         num_req_outstanding--;
621         spin_unlock_irqrestore(&viodasd_spinlock, irq_flags);
622
623         error = event->xRc != HvLpEvent_Rc_Good;
624         if (error) {
625                 const struct vio_error_entry *err;
626                 err = vio_lookup_rc(viodasd_err_table, bevent->sub_result);
627                 printk(VIOD_KERN_WARNING "read/write error %d:0x%04x (%s)\n",
628                                 event->xRc, bevent->sub_result, err->msg);
629                 num_sect = req->hard_nr_sectors;
630         }
631         qlock = req->q->queue_lock;
632         spin_lock_irqsave(qlock, irq_flags);
633         viodasd_end_request(req, !error, num_sect);
634         spin_unlock_irqrestore(qlock, irq_flags);
635
636         /* Finally, try to get more requests off of this device's queue */
637         viodasd_restart_all_queues_starting_from(DEVICE_NO(d));
638
639         return 0;
640 }
641
642 /* This routine handles incoming block LP events */
643 static void handle_block_event(struct HvLpEvent *event)
644 {
645         struct vioblocklpevent *bevent = (struct vioblocklpevent *)event;
646         struct viodasd_waitevent *pwe;
647
648         if (event == NULL)
649                 /* Notification that a partition went away! */
650                 return;
651         /* First, we should NEVER get an int here...only acks */
652         if (event->xFlags.xFunction == HvLpEvent_Function_Int) {
653                 printk(VIOD_KERN_WARNING
654                        "Yikes! got an int in viodasd event handler!\n");
655                 if (event->xFlags.xAckInd == HvLpEvent_AckInd_DoAck) {
656                         event->xRc = HvLpEvent_Rc_InvalidSubtype;
657                         HvCallEvent_ackLpEvent(event);
658                 }
659         }
660
661         switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
662         case vioblockopen:
663                 /*
664                  * Handle a response to an open request.  We get all the
665                  * disk information in the response, so update it.  The
666                  * correlation token contains a pointer to a waitevent
667                  * structure that has a completion in it.  update the
668                  * return code in the waitevent structure and post the
669                  * completion to wake up the guy who sent the request
670                  */
671                 pwe = (struct viodasd_waitevent *)event->xCorrelationToken;
672                 pwe->rc = event->xRc;
673                 pwe->sub_result = bevent->sub_result;
674                 if (event->xRc == HvLpEvent_Rc_Good) {
675                         const struct open_data *data = &bevent->u.open_data;
676                         struct viodasd_device *device =
677                                 &viodasd_devices[bevent->disk];
678                         device->read_only =
679                                 bevent->flags & vioblockflags_ro;
680                         device->size = data->disk_size;
681                         device->cylinders = data->cylinders;
682                         device->tracks = data->tracks;
683                         device->sectors = data->sectors;
684                         device->bytes_per_sector = data->bytes_per_sector;
685                         pwe->max_disk = data->max_disk;
686                 }
687                 complete(&pwe->com);
688                 break;
689         case vioblockclose:
690                 break;
691         case vioblockread:
692         case vioblockwrite:
693                 viodasd_handle_read_write(bevent);
694                 break;
695
696         default:
697                 printk(VIOD_KERN_WARNING "invalid subtype!");
698                 if (event->xFlags.xAckInd == HvLpEvent_AckInd_DoAck) {
699                         event->xRc = HvLpEvent_Rc_InvalidSubtype;
700                         HvCallEvent_ackLpEvent(event);
701                 }
702         }
703 }
704
705 /*
706  * Get the driver to reprobe for more disks.
707  */
708 static ssize_t probe_disks(struct device_driver *drv, const char *buf,
709                 size_t count)
710 {
711         struct viodasd_device *d;
712
713         for (d = viodasd_devices; d < &viodasd_devices[MAX_DISKNO]; d++) {
714                 if (d->disk == NULL)
715                         probe_disk(d);
716         }
717         return count;
718 }
719 static DRIVER_ATTR(probe, S_IWUSR, NULL, probe_disks);
720
721 static int viodasd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
722 {
723         struct viodasd_device *d = &viodasd_devices[vdev->unit_address];
724
725         d->dev = &vdev->dev;
726         probe_disk(d);
727         if (d->disk == NULL)
728                 return -ENODEV;
729         return 0;
730 }
731
732 static int viodasd_remove(struct vio_dev *vdev)
733 {
734         struct viodasd_device *d;
735
736         d = &viodasd_devices[vdev->unit_address];
737         if (d->disk) {
738                 del_gendisk(d->disk);
739                 blk_cleanup_queue(d->disk->queue);
740                 put_disk(d->disk);
741                 d->disk = NULL;
742         }
743         d->dev = NULL;
744         return 0;
745 }
746
747 /**
748  * viodasd_device_table: Used by vio.c to match devices that we
749  * support.
750  */
751 static struct vio_device_id viodasd_device_table[] __devinitdata = {
752         { "viodasd", "" },
753         { "", "" }
754 };
755 MODULE_DEVICE_TABLE(vio, viodasd_device_table);
756
757 static struct vio_driver viodasd_driver = {
758         .id_table = viodasd_device_table,
759         .probe = viodasd_probe,
760         .remove = viodasd_remove,
761         .driver = {
762                 .name = "viodasd",
763                 .owner = THIS_MODULE,
764         }
765 };
766
767 /*
768  * Initialize the whole device driver.  Handle module and non-module
769  * versions
770  */
771 static int __init viodasd_init(void)
772 {
773         int rc;
774
775         /* Try to open to our host lp */
776         if (viopath_hostLp == HvLpIndexInvalid)
777                 vio_set_hostlp();
778
779         if (viopath_hostLp == HvLpIndexInvalid) {
780                 printk(VIOD_KERN_WARNING "invalid hosting partition\n");
781                 return -EIO;
782         }
783
784         printk(VIOD_KERN_INFO "vers " VIOD_VERS ", hosting partition %d\n",
785                         viopath_hostLp);
786
787         /* register the block device */
788         if (register_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME)) {
789                 printk(VIOD_KERN_WARNING
790                                 "Unable to get major number %d for %s\n",
791                                 VIODASD_MAJOR, VIOD_GENHD_NAME);
792                 return -EIO;
793         }
794         /* Actually open the path to the hosting partition */
795         if (viopath_open(viopath_hostLp, viomajorsubtype_blockio,
796                                 VIOMAXREQ + 2)) {
797                 printk(VIOD_KERN_WARNING
798                        "error opening path to host partition %d\n",
799                        viopath_hostLp);
800                 unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
801                 return -EIO;
802         }
803
804         /* Initialize our request handler */
805         vio_setHandler(viomajorsubtype_blockio, handle_block_event);
806
807         rc = vio_register_driver(&viodasd_driver);
808         if (rc == 0)
809                 driver_create_file(&viodasd_driver.driver, &driver_attr_probe);
810         return rc;
811 }
812 module_init(viodasd_init);
813
814 void viodasd_exit(void)
815 {
816         driver_remove_file(&viodasd_driver.driver, &driver_attr_probe);
817         vio_unregister_driver(&viodasd_driver);
818         vio_clearHandler(viomajorsubtype_blockio);
819         unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
820         viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
821 }
822
823 module_exit(viodasd_exit);