Blame view

drivers/block/viodasd.c 21.7 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
  /* -*- linux-c -*-
   * viodasd.c
   *  Authors: Dave Boutcher <boutcher@us.ibm.com>
   *           Ryan Arnold <ryanarn@us.ibm.com>
   *           Colin Devilbiss <devilbis@us.ibm.com>
8962cadbe   Stephen Rothwell   [POWERPC] iSeries...
6
   *           Stephen Rothwell
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
   *
   * (C) Copyright 2000-2004 IBM Corporation
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License as
   * published by the Free Software Foundation; either version 2 of the
   * License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
   *
   * This routine provides access to disk space (termed "DASD" in historical
   * IBM terms) owned and managed by an OS/400 partition running on the
   * same box as this Linux partition.
   *
   * All disk operations are performed by sending messages back and forth to
   * the OS/400 partition.
   */
  #include <linux/major.h>
  #include <linux/fs.h>
  #include <linux/module.h>
  #include <linux/kernel.h>
  #include <linux/blkdev.h>
  #include <linux/genhd.h>
  #include <linux/hdreg.h>
  #include <linux/errno.h>
  #include <linux/init.h>
  #include <linux/string.h>
  #include <linux/dma-mapping.h>
  #include <linux/completion.h>
  #include <linux/device.h>
45711f1af   Jens Axboe   [SG] Update drive...
44
  #include <linux/scatterlist.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
47
  
  #include <asm/uaccess.h>
  #include <asm/vio.h>
1ec65d76f   Kelly Daly   merge filename an...
48
  #include <asm/iseries/hv_types.h>
e45423eac   Kelly Daly   merge filename an...
49
  #include <asm/iseries/hv_lp_event.h>
15b171894   Kelly Daly   merge filename an...
50
  #include <asm/iseries/hv_lp_config.h>
b42067787   Kelly Daly   merge filename an...
51
  #include <asm/iseries/vio.h>
fb8b50078   Stephen Rothwell   [POWERPC] iSeries...
52
  #include <asm/firmware.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53
54
55
56
57
58
59
60
61
62
  
  MODULE_DESCRIPTION("iSeries Virtual DASD");
  MODULE_AUTHOR("Dave Boutcher");
  MODULE_LICENSE("GPL");
  
  /*
   * We only support 7 partitions per physical disk....so with minor
   * numbers 0-255 we get a maximum of 32 disks.
   */
  #define VIOD_GENHD_NAME		"iseries/vd"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
63
64
65
66
67
68
69
70
71
  
  #define VIOD_VERS		"1.64"
  
  #define VIOD_KERN_WARNING	KERN_WARNING "viod: "
  #define VIOD_KERN_INFO		KERN_INFO "viod: "
  
  enum {
  	PARTITION_SHIFT = 3,
  	MAX_DISKNO = HVMAXARCHITECTEDVIRTUALDISKS,
ea6728c11   Julia Lawall   [POWERPC] Use FIE...
72
  	MAX_DISK_NAME = FIELD_SIZEOF(struct gendisk, disk_name)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
73
74
75
76
77
  };
  
  static DEFINE_SPINLOCK(viodasd_spinlock);
  
  #define VIOMAXREQ		16
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78
79
  
  #define DEVICE_NO(cell)	((struct viodasd_device *)(cell) - &viodasd_devices[0])
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  struct viodasd_waitevent {
  	struct completion	com;
  	int			rc;
  	u16			sub_result;
  	int			max_disk;	/* open */
  };
  
  static const struct vio_error_entry viodasd_err_table[] = {
  	{ 0x0201, EINVAL, "Invalid Range" },
  	{ 0x0202, EINVAL, "Invalid Token" },
  	{ 0x0203, EIO, "DMA Error" },
  	{ 0x0204, EIO, "Use Error" },
  	{ 0x0205, EIO, "Release Error" },
  	{ 0x0206, EINVAL, "Invalid Disk" },
  	{ 0x0207, EBUSY, "Cant Lock" },
  	{ 0x0208, EIO, "Already Locked" },
  	{ 0x0209, EIO, "Already Unlocked" },
  	{ 0x020A, EIO, "Invalid Arg" },
  	{ 0x020B, EIO, "Bad IFS File" },
  	{ 0x020C, EROFS, "Read Only Device" },
  	{ 0x02FF, EIO, "Internal Error" },
  	{ 0x0000, 0, NULL },
  };
  
  /*
   * Figure out the biggest I/O request (in sectors) we can accept
   */
  #define VIODASD_MAXSECTORS (4096 / 512 * VIOMAXBLOCKDMA)
  
  /*
   * Number of disk I/O requests we've sent to OS/400
   */
  static int num_req_outstanding;
  
  /*
   * This is our internal structure for keeping track of disk devices
   */
  struct viodasd_device {
  	u16		cylinders;
  	u16		tracks;
  	u16		sectors;
  	u16		bytes_per_sector;
  	u64		size;
  	int		read_only;
  	spinlock_t	q_lock;
  	struct gendisk	*disk;
  	struct device	*dev;
  } viodasd_devices[MAX_DISKNO];
  
  /*
   * External open entry point.
   */
f115a14ae   Al Viro   [PATCH] switch vi...
132
  static int viodasd_open(struct block_device *bdev, fmode_t mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133
  {
f115a14ae   Al Viro   [PATCH] switch vi...
134
  	struct viodasd_device *d = bdev->bd_disk->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
135
136
137
138
139
  	HvLpEvent_Rc hvrc;
  	struct viodasd_waitevent we;
  	u16 flags = 0;
  
  	if (d->read_only) {
f115a14ae   Al Viro   [PATCH] switch vi...
140
  		if (mode & FMODE_WRITE)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  			return -EROFS;
  		flags = vioblockflags_ro;
  	}
  
  	init_completion(&we.com);
  
  	/* Send the open event to OS/400 */
  	hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  			HvLpEvent_Type_VirtualIo,
  			viomajorsubtype_blockio | vioblockopen,
  			HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  			viopath_sourceinst(viopath_hostLp),
  			viopath_targetinst(viopath_hostLp),
  			(u64)(unsigned long)&we, VIOVERSION << 16,
  			((u64)DEVICE_NO(d) << 48) | ((u64)flags << 32),
  			0, 0, 0);
  	if (hvrc != 0) {
  		printk(VIOD_KERN_WARNING "HV open failed %d
  ", (int)hvrc);
  		return -EIO;
  	}
  
  	wait_for_completion(&we.com);
  
  	/* Check the return code */
  	if (we.rc != 0) {
  		const struct vio_error_entry *err =
  			vio_lookup_rc(viodasd_err_table, we.sub_result);
  
  		printk(VIOD_KERN_WARNING
  				"bad rc opening disk: %d:0x%04x (%s)
  ",
  				(int)we.rc, we.sub_result, err->msg);
  		return -EIO;
  	}
  
  	return 0;
  }
  
  /*
   * External release entry point.
   */
f115a14ae   Al Viro   [PATCH] switch vi...
183
  static int viodasd_release(struct gendisk *disk, fmode_t mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184
  {
f115a14ae   Al Viro   [PATCH] switch vi...
185
  	struct viodasd_device *d = disk->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  	HvLpEvent_Rc hvrc;
  
  	/* Send the event to OS/400.  We DON'T expect a response */
  	hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  			HvLpEvent_Type_VirtualIo,
  			viomajorsubtype_blockio | vioblockclose,
  			HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
  			viopath_sourceinst(viopath_hostLp),
  			viopath_targetinst(viopath_hostLp),
  			0, VIOVERSION << 16,
  			((u64)DEVICE_NO(d) << 48) /* | ((u64)flags << 32) */,
  			0, 0, 0);
  	if (hvrc != 0)
  		printk(VIOD_KERN_WARNING "HV close call failed %d
  ",
  				(int)hvrc);
  	return 0;
  }
  
  
  /* External ioctl entry point.
   */
a885c8c43   Christoph Hellwig   [PATCH] Add block...
208
  static int viodasd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
209
  {
a885c8c43   Christoph Hellwig   [PATCH] Add block...
210
211
  	struct gendisk *disk = bdev->bd_disk;
  	struct viodasd_device *d = disk->private_data;
36a700307   Stephen Rothwell   [POWERPC] Fix vio...
212
  	geo->sectors = d->sectors ? d->sectors : 32;
a885c8c43   Christoph Hellwig   [PATCH] Add block...
213
214
  	geo->heads = d->tracks ? d->tracks  : 64;
  	geo->cylinders = d->cylinders ? d->cylinders :
36a700307   Stephen Rothwell   [POWERPC] Fix vio...
215
  		get_capacity(disk) / (geo->sectors * geo->heads);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
216

a885c8c43   Christoph Hellwig   [PATCH] Add block...
217
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218
219
220
221
222
223
224
  }
  
  /*
   * Our file operations table
   */
  static struct block_device_operations viodasd_fops = {
  	.owner = THIS_MODULE,
f115a14ae   Al Viro   [PATCH] switch vi...
225
226
  	.open = viodasd_open,
  	.release = viodasd_release,
a885c8c43   Christoph Hellwig   [PATCH] Add block...
227
  	.getgeo = viodasd_getgeo,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
228
229
230
231
232
  };
  
  /*
   * End a request
   */
b2aec24ea   Kiyoshi Ueda   blk_end_request: ...
233
  static void viodasd_end_request(struct request *req, int error,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234
235
  		int num_sectors)
  {
b2aec24ea   Kiyoshi Ueda   blk_end_request: ...
236
  	__blk_end_request(req, error, num_sectors << 9);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
237
238
239
240
241
242
243
244
245
246
247
248
249
  }
  
  /*
   * Send an actual I/O request to OS/400
   */
  static int send_request(struct request *req)
  {
  	u64 start;
  	int direction;
  	int nsg;
  	u16 viocmd;
  	HvLpEvent_Rc hvrc;
  	struct vioblocklpevent *bevent;
677f8c0d0   Stephen Rothwell   [PATCH] powerpc: ...
250
  	struct HvLpEvent *hev;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
251
252
  	struct scatterlist sg[VIOMAXBLOCKDMA];
  	int sgindex;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
253
254
255
256
257
258
259
260
  	struct viodasd_device *d;
  	unsigned long flags;
  
  	start = (u64)req->sector << 9;
  
  	if (rq_data_dir(req) == READ) {
  		direction = DMA_FROM_DEVICE;
  		viocmd = viomajorsubtype_blockio | vioblockread;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
261
262
263
  	} else {
  		direction = DMA_TO_DEVICE;
  		viocmd = viomajorsubtype_blockio | vioblockwrite;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
264
265
266
267
268
  	}
  
          d = req->rq_disk->private_data;
  
  	/* Now build the scatter-gather list */
45711f1af   Jens Axboe   [SG] Update drive...
269
  	sg_init_table(sg, VIOMAXBLOCKDMA);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
  	nsg = blk_rq_map_sg(req->q, req, sg);
  	nsg = dma_map_sg(d->dev, sg, nsg, direction);
  
  	spin_lock_irqsave(&viodasd_spinlock, flags);
  	num_req_outstanding++;
  
  	/* This optimization handles a single DMA block */
  	if (nsg == 1)
  		hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  				HvLpEvent_Type_VirtualIo, viocmd,
  				HvLpEvent_AckInd_DoAck,
  				HvLpEvent_AckType_ImmediateAck,
  				viopath_sourceinst(viopath_hostLp),
  				viopath_targetinst(viopath_hostLp),
  				(u64)(unsigned long)req, VIOVERSION << 16,
  				((u64)DEVICE_NO(d) << 48), start,
  				((u64)sg_dma_address(&sg[0])) << 32,
  				sg_dma_len(&sg[0]));
  	else {
  		bevent = (struct vioblocklpevent *)
  			vio_get_event_buffer(viomajorsubtype_blockio);
  		if (bevent == NULL) {
  			printk(VIOD_KERN_WARNING
  			       "error allocating disk event buffer
  ");
  			goto error_ret;
  		}
  
  		/*
  		 * Now build up the actual request.  Note that we store
  		 * the pointer to the request in the correlation
  		 * token so we can match the response up later
  		 */
  		memset(bevent, 0, sizeof(struct vioblocklpevent));
677f8c0d0   Stephen Rothwell   [PATCH] powerpc: ...
304
305
306
307
308
309
310
311
  		hev = &bevent->event;
  		hev->flags = HV_LP_EVENT_VALID | HV_LP_EVENT_DO_ACK |
  			HV_LP_EVENT_INT;
  		hev->xType = HvLpEvent_Type_VirtualIo;
  		hev->xSubtype = viocmd;
  		hev->xSourceLp = HvLpConfig_getLpIndex();
  		hev->xTargetLp = viopath_hostLp;
  		hev->xSizeMinus1 =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
312
313
  			offsetof(struct vioblocklpevent, u.rw_data.dma_info) +
  			(sizeof(bevent->u.rw_data.dma_info[0]) * nsg) - 1;
677f8c0d0   Stephen Rothwell   [PATCH] powerpc: ...
314
315
316
  		hev->xSourceInstanceId = viopath_sourceinst(viopath_hostLp);
  		hev->xTargetInstanceId = viopath_targetinst(viopath_hostLp);
  		hev->xCorrelationToken = (u64)req;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
  		bevent->version = VIOVERSION;
  		bevent->disk = DEVICE_NO(d);
  		bevent->u.rw_data.offset = start;
  
  		/*
  		 * Copy just the dma information from the sg list
  		 * into the request
  		 */
  		for (sgindex = 0; sgindex < nsg; sgindex++) {
  			bevent->u.rw_data.dma_info[sgindex].token =
  				sg_dma_address(&sg[sgindex]);
  			bevent->u.rw_data.dma_info[sgindex].len =
  				sg_dma_len(&sg[sgindex]);
  		}
  
  		/* Send the request */
  		hvrc = HvCallEvent_signalLpEvent(&bevent->event);
  		vio_free_event_buffer(viomajorsubtype_blockio, bevent);
  	}
  
  	if (hvrc != HvLpEvent_Rc_Good) {
  		printk(VIOD_KERN_WARNING
  		       "error sending disk event to OS/400 (rc %d)
  ",
  		       (int)hvrc);
  		goto error_ret;
  	}
  	spin_unlock_irqrestore(&viodasd_spinlock, flags);
  	return 0;
  
  error_ret:
  	num_req_outstanding--;
  	spin_unlock_irqrestore(&viodasd_spinlock, flags);
  	dma_unmap_sg(d->dev, sg, nsg, direction);
  	return -1;
  }
  
  /*
   * This is the external request processing routine
   */
165125e1e   Jens Axboe   [BLOCK] Get rid o...
357
  static void do_viodasd_request(struct request_queue *q)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
  {
  	struct request *req;
  
  	/*
  	 * If we already have the maximum number of requests
  	 * outstanding to OS/400 just bail out. We'll come
  	 * back later.
  	 */
  	while (num_req_outstanding < VIOMAXREQ) {
  		req = elv_next_request(q);
  		if (req == NULL)
  			return;
  		/* dequeue the current request from the queue */
  		blkdev_dequeue_request(req);
  		/* check that request contains a valid command */
  		if (!blk_fs_request(req)) {
b2aec24ea   Kiyoshi Ueda   blk_end_request: ...
374
  			viodasd_end_request(req, -EIO, req->hard_nr_sectors);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
375
376
377
378
  			continue;
  		}
  		/* Try sending the request */
  		if (send_request(req) != 0)
b2aec24ea   Kiyoshi Ueda   blk_end_request: ...
379
  			viodasd_end_request(req, -EIO, req->hard_nr_sectors);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
380
381
382
383
384
385
386
  	}
  }
  
  /*
   * Probe a single disk and fill in the viodasd_device structure
   * for it.
   */
8251b4c48   Stephen Rothwell   [POWERPC] iSeries...
387
  static int probe_disk(struct viodasd_device *d)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
  {
  	HvLpEvent_Rc hvrc;
  	struct viodasd_waitevent we;
  	int dev_no = DEVICE_NO(d);
  	struct gendisk *g;
  	struct request_queue *q;
  	u16 flags = 0;
  
  retry:
  	init_completion(&we.com);
  
  	/* Send the open event to OS/400 */
  	hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  			HvLpEvent_Type_VirtualIo,
  			viomajorsubtype_blockio | vioblockopen,
  			HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
  			viopath_sourceinst(viopath_hostLp),
  			viopath_targetinst(viopath_hostLp),
  			(u64)(unsigned long)&we, VIOVERSION << 16,
  			((u64)dev_no << 48) | ((u64)flags<< 32),
  			0, 0, 0);
  	if (hvrc != 0) {
  		printk(VIOD_KERN_WARNING "bad rc on HV open %d
  ", (int)hvrc);
8251b4c48   Stephen Rothwell   [POWERPC] iSeries...
412
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
413
414
415
416
417
418
  	}
  
  	wait_for_completion(&we.com);
  
  	if (we.rc != 0) {
  		if (flags != 0)
8251b4c48   Stephen Rothwell   [POWERPC] iSeries...
419
  			return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
  		/* try again with read only flag set */
  		flags = vioblockflags_ro;
  		goto retry;
  	}
  	if (we.max_disk > (MAX_DISKNO - 1)) {
  		static int warned;
  
  		if (warned == 0) {
  			warned++;
  			printk(VIOD_KERN_INFO
  				"Only examining the first %d "
  				"of %d disks connected
  ",
  				MAX_DISKNO, we.max_disk + 1);
  		}
  	}
  
  	/* Send the close event to OS/400.  We DON'T expect a response */
  	hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
  			HvLpEvent_Type_VirtualIo,
  			viomajorsubtype_blockio | vioblockclose,
  			HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
  			viopath_sourceinst(viopath_hostLp),
  			viopath_targetinst(viopath_hostLp),
  			0, VIOVERSION << 16,
  			((u64)dev_no << 48) | ((u64)flags << 32),
  			0, 0, 0);
  	if (hvrc != 0) {
  		printk(VIOD_KERN_WARNING
  		       "bad rc sending event to OS/400 %d
  ", (int)hvrc);
8251b4c48   Stephen Rothwell   [POWERPC] iSeries...
451
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
452
  	}
8251b4c48   Stephen Rothwell   [POWERPC] iSeries...
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
  
  	if (d->dev == NULL) {
  		/* this is when we reprobe for new disks */
  		if (vio_create_viodasd(dev_no) == NULL) {
  			printk(VIOD_KERN_WARNING
  				"cannot allocate virtual device for disk %d
  ",
  				dev_no);
  			return 0;
  		}
  		/*
  		 * The vio_create_viodasd will have recursed into this
  		 * routine with d->dev set to the new vio device and
  		 * will finish the setup of the disk below.
  		 */
  		return 1;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
470
471
472
473
474
475
476
  	/* create the request queue for the disk */
  	spin_lock_init(&d->q_lock);
  	q = blk_init_queue(do_viodasd_request, &d->q_lock);
  	if (q == NULL) {
  		printk(VIOD_KERN_WARNING "cannot allocate queue for disk %d
  ",
  				dev_no);
8251b4c48   Stephen Rothwell   [POWERPC] iSeries...
477
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
478
479
480
481
482
483
484
485
  	}
  	g = alloc_disk(1 << PARTITION_SHIFT);
  	if (g == NULL) {
  		printk(VIOD_KERN_WARNING
  				"cannot allocate disk structure for disk %d
  ",
  				dev_no);
  		blk_cleanup_queue(q);
8251b4c48   Stephen Rothwell   [POWERPC] iSeries...
486
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
  	}
  
  	d->disk = g;
  	blk_queue_max_hw_segments(q, VIOMAXBLOCKDMA);
  	blk_queue_max_phys_segments(q, VIOMAXBLOCKDMA);
  	blk_queue_max_sectors(q, VIODASD_MAXSECTORS);
  	g->major = VIODASD_MAJOR;
  	g->first_minor = dev_no << PARTITION_SHIFT;
  	if (dev_no >= 26)
  		snprintf(g->disk_name, sizeof(g->disk_name),
  				VIOD_GENHD_NAME "%c%c",
  				'a' + (dev_no / 26) - 1, 'a' + (dev_no % 26));
  	else
  		snprintf(g->disk_name, sizeof(g->disk_name),
  				VIOD_GENHD_NAME "%c", 'a' + (dev_no % 26));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
  	g->fops = &viodasd_fops;
  	g->queue = q;
  	g->private_data = d;
  	g->driverfs_dev = d->dev;
  	set_capacity(g, d->size >> 9);
  
  	printk(VIOD_KERN_INFO "disk %d: %lu sectors (%lu MB) "
  			"CHS=%d/%d/%d sector size %d%s
  ",
  			dev_no, (unsigned long)(d->size >> 9),
  			(unsigned long)(d->size >> 20),
  			(int)d->cylinders, (int)d->tracks,
  			(int)d->sectors, (int)d->bytes_per_sector,
  			d->read_only ? " (RO)" : "");
  
  	/* register us in the global list */
  	add_disk(g);
8251b4c48   Stephen Rothwell   [POWERPC] iSeries...
519
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
  }
  
  /* returns the total number of scatterlist elements converted */
  static int block_event_to_scatterlist(const struct vioblocklpevent *bevent,
  		struct scatterlist *sg, int *total_len)
  {
  	int i, numsg;
  	const struct rw_data *rw_data = &bevent->u.rw_data;
  	static const int offset =
  		offsetof(struct vioblocklpevent, u.rw_data.dma_info);
  	static const int element_size = sizeof(rw_data->dma_info[0]);
  
  	numsg = ((bevent->event.xSizeMinus1 + 1) - offset) / element_size;
  	if (numsg > VIOMAXBLOCKDMA)
  		numsg = VIOMAXBLOCKDMA;
  
  	*total_len = 0;
25c0a7b83   Benjamin Herrenschmidt   [POWERPC] Fix vio...
537
  	sg_init_table(sg, VIOMAXBLOCKDMA);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
  	for (i = 0; (i < numsg) && (rw_data->dma_info[i].len > 0); ++i) {
  		sg_dma_address(&sg[i]) = rw_data->dma_info[i].token;
  		sg_dma_len(&sg[i]) = rw_data->dma_info[i].len;
  		*total_len += rw_data->dma_info[i].len;
  	}
  	return i;
  }
  
  /*
   * Restart all queues, starting with the one _after_ the disk given,
   * thus reducing the chance of starvation of higher numbered disks.
   */
  static void viodasd_restart_all_queues_starting_from(int first_index)
  {
  	int i;
  
  	for (i = first_index + 1; i < MAX_DISKNO; ++i)
  		if (viodasd_devices[i].disk)
  			blk_run_queue(viodasd_devices[i].disk->queue);
  	for (i = 0; i <= first_index; ++i)
  		if (viodasd_devices[i].disk)
  			blk_run_queue(viodasd_devices[i].disk->queue);
  }
  
  /*
   * For read and write requests, decrement the number of outstanding requests,
   * Free the DMA buffers we allocated.
   */
  static int viodasd_handle_read_write(struct vioblocklpevent *bevent)
  {
  	int num_sg, num_sect, pci_direction, total_len;
  	struct request *req;
  	struct scatterlist sg[VIOMAXBLOCKDMA];
  	struct HvLpEvent *event = &bevent->event;
  	unsigned long irq_flags;
  	struct viodasd_device *d;
  	int error;
  	spinlock_t *qlock;
  
  	num_sg = block_event_to_scatterlist(bevent, sg, &total_len);
  	num_sect = total_len >> 9;
  	if (event->xSubtype == (viomajorsubtype_blockio | vioblockread))
  		pci_direction = DMA_FROM_DEVICE;
  	else
  		pci_direction = DMA_TO_DEVICE;
  	req = (struct request *)bevent->event.xCorrelationToken;
  	d = req->rq_disk->private_data;
  
  	dma_unmap_sg(d->dev, sg, num_sg, pci_direction);
  
  	/*
  	 * Since this is running in interrupt mode, we need to make sure
  	 * we're not stepping on any global I/O operations
  	 */
  	spin_lock_irqsave(&viodasd_spinlock, irq_flags);
  	num_req_outstanding--;
  	spin_unlock_irqrestore(&viodasd_spinlock, irq_flags);
b2aec24ea   Kiyoshi Ueda   blk_end_request: ...
595
  	error = (event->xRc == HvLpEvent_Rc_Good) ? 0 : -EIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
596
597
598
599
600
601
602
603
604
605
  	if (error) {
  		const struct vio_error_entry *err;
  		err = vio_lookup_rc(viodasd_err_table, bevent->sub_result);
  		printk(VIOD_KERN_WARNING "read/write error %d:0x%04x (%s)
  ",
  				event->xRc, bevent->sub_result, err->msg);
  		num_sect = req->hard_nr_sectors;
  	}
  	qlock = req->q->queue_lock;
  	spin_lock_irqsave(qlock, irq_flags);
b2aec24ea   Kiyoshi Ueda   blk_end_request: ...
606
  	viodasd_end_request(req, error, num_sect);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
  	spin_unlock_irqrestore(qlock, irq_flags);
  
  	/* Finally, try to get more requests off of this device's queue */
  	viodasd_restart_all_queues_starting_from(DEVICE_NO(d));
  
  	return 0;
  }
  
  /* This routine handles incoming block LP events */
  static void handle_block_event(struct HvLpEvent *event)
  {
  	struct vioblocklpevent *bevent = (struct vioblocklpevent *)event;
  	struct viodasd_waitevent *pwe;
  
  	if (event == NULL)
  		/* Notification that a partition went away! */
  		return;
  	/* First, we should NEVER get an int here...only acks */
677f8c0d0   Stephen Rothwell   [PATCH] powerpc: ...
625
  	if (hvlpevent_is_int(event)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
626
627
628
  		printk(VIOD_KERN_WARNING
  		       "Yikes! got an int in viodasd event handler!
  ");
677f8c0d0   Stephen Rothwell   [PATCH] powerpc: ...
629
  		if (hvlpevent_need_ack(event)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
  			event->xRc = HvLpEvent_Rc_InvalidSubtype;
  			HvCallEvent_ackLpEvent(event);
  		}
  	}
  
  	switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
  	case vioblockopen:
  		/*
  		 * Handle a response to an open request.  We get all the
  		 * disk information in the response, so update it.  The
  		 * correlation token contains a pointer to a waitevent
  		 * structure that has a completion in it.  update the
  		 * return code in the waitevent structure and post the
  		 * completion to wake up the guy who sent the request
  		 */
  		pwe = (struct viodasd_waitevent *)event->xCorrelationToken;
  		pwe->rc = event->xRc;
  		pwe->sub_result = bevent->sub_result;
  		if (event->xRc == HvLpEvent_Rc_Good) {
  			const struct open_data *data = &bevent->u.open_data;
  			struct viodasd_device *device =
  				&viodasd_devices[bevent->disk];
  			device->read_only =
  				bevent->flags & vioblockflags_ro;
  			device->size = data->disk_size;
  			device->cylinders = data->cylinders;
  			device->tracks = data->tracks;
  			device->sectors = data->sectors;
  			device->bytes_per_sector = data->bytes_per_sector;
  			pwe->max_disk = data->max_disk;
  		}
  		complete(&pwe->com);
  		break;
  	case vioblockclose:
  		break;
  	case vioblockread:
  	case vioblockwrite:
  		viodasd_handle_read_write(bevent);
  		break;
  
  	default:
  		printk(VIOD_KERN_WARNING "invalid subtype!");
677f8c0d0   Stephen Rothwell   [PATCH] powerpc: ...
672
  		if (hvlpevent_need_ack(event)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
  			event->xRc = HvLpEvent_Rc_InvalidSubtype;
  			HvCallEvent_ackLpEvent(event);
  		}
  	}
  }
  
  /*
   * Get the driver to reprobe for more disks.
   */
  static ssize_t probe_disks(struct device_driver *drv, const char *buf,
  		size_t count)
  {
  	struct viodasd_device *d;
  
  	for (d = viodasd_devices; d < &viodasd_devices[MAX_DISKNO]; d++) {
  		if (d->disk == NULL)
  			probe_disk(d);
  	}
  	return count;
  }
  static DRIVER_ATTR(probe, S_IWUSR, NULL, probe_disks);
  
  static int viodasd_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  {
  	struct viodasd_device *d = &viodasd_devices[vdev->unit_address];
  
  	d->dev = &vdev->dev;
8251b4c48   Stephen Rothwell   [POWERPC] iSeries...
700
  	if (!probe_disk(d))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
  		return -ENODEV;
  	return 0;
  }
  
  static int viodasd_remove(struct vio_dev *vdev)
  {
  	struct viodasd_device *d;
  
  	d = &viodasd_devices[vdev->unit_address];
  	if (d->disk) {
  		del_gendisk(d->disk);
  		blk_cleanup_queue(d->disk->queue);
  		put_disk(d->disk);
  		d->disk = NULL;
  	}
  	d->dev = NULL;
  	return 0;
  }
  
  /**
   * viodasd_device_table: Used by vio.c to match devices that we
   * support.
   */
  static struct vio_device_id viodasd_device_table[] __devinitdata = {
fbabeb60b   Stephen Rothwell   [PATCH] powerpc: ...
725
  	{ "block", "IBM,iSeries-viodasd" },
fb120da67   Stephen Rothwell   [PATCH] Make MODU...
726
  	{ "", "" }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
727
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
728
  MODULE_DEVICE_TABLE(vio, viodasd_device_table);
915124d81   Stephen Rothwell   powerpc: set the ...
729

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
730
  static struct vio_driver viodasd_driver = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
731
732
  	.id_table = viodasd_device_table,
  	.probe = viodasd_probe,
6fdf5392c   Stephen Rothwell   powerpc: don't du...
733
734
735
  	.remove = viodasd_remove,
  	.driver = {
  		.name = "viodasd",
915124d81   Stephen Rothwell   powerpc: set the ...
736
  		.owner = THIS_MODULE,
6fdf5392c   Stephen Rothwell   powerpc: don't du...
737
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
738
  };
f9df68ec7   Stephen Rothwell   [POWERPC] iSeries...
739
  static int need_delete_probe;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
740
741
742
743
744
745
746
  /*
   * Initialize the whole device driver.  Handle module and non-module
   * versions
   */
  static int __init viodasd_init(void)
  {
  	int rc;
fb8b50078   Stephen Rothwell   [POWERPC] iSeries...
747
748
749
750
  	if (!firmware_has_feature(FW_FEATURE_ISERIES)) {
  		rc = -ENODEV;
  		goto early_fail;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
751
752
753
754
755
756
757
  	/* Try to open to our host lp */
  	if (viopath_hostLp == HvLpIndexInvalid)
  		vio_set_hostlp();
  
  	if (viopath_hostLp == HvLpIndexInvalid) {
  		printk(VIOD_KERN_WARNING "invalid hosting partition
  ");
f9df68ec7   Stephen Rothwell   [POWERPC] iSeries...
758
759
  		rc = -EIO;
  		goto early_fail;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
760
761
762
763
764
765
766
  	}
  
  	printk(VIOD_KERN_INFO "vers " VIOD_VERS ", hosting partition %d
  ",
  			viopath_hostLp);
  
          /* register the block device */
f9df68ec7   Stephen Rothwell   [POWERPC] iSeries...
767
768
  	rc =  register_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
  	if (rc) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
769
770
771
772
  		printk(VIOD_KERN_WARNING
  				"Unable to get major number %d for %s
  ",
  				VIODASD_MAJOR, VIOD_GENHD_NAME);
f9df68ec7   Stephen Rothwell   [POWERPC] iSeries...
773
  		goto early_fail;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
774
775
  	}
  	/* Actually open the path to the hosting partition */
f9df68ec7   Stephen Rothwell   [POWERPC] iSeries...
776
777
778
  	rc = viopath_open(viopath_hostLp, viomajorsubtype_blockio,
  				VIOMAXREQ + 2);
  	if (rc) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
779
780
781
782
  		printk(VIOD_KERN_WARNING
  		       "error opening path to host partition %d
  ",
  		       viopath_hostLp);
f9df68ec7   Stephen Rothwell   [POWERPC] iSeries...
783
  		goto unregister_blk;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
784
785
786
787
788
789
  	}
  
  	/* Initialize our request handler */
  	vio_setHandler(viomajorsubtype_blockio, handle_block_event);
  
  	rc = vio_register_driver(&viodasd_driver);
f9df68ec7   Stephen Rothwell   [POWERPC] iSeries...
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
  	if (rc) {
  		printk(VIOD_KERN_WARNING "vio_register_driver failed
  ");
  		goto unset_handler;
  	}
  
  	/*
  	 * If this call fails, it just means that we cannot dynamically
  	 * add virtual disks, but the driver will still work fine for
  	 * all existing disk, so ignore the failure.
  	 */
  	if (!driver_create_file(&viodasd_driver.driver, &driver_attr_probe))
  		need_delete_probe = 1;
  
  	return 0;
  
  unset_handler:
  	vio_clearHandler(viomajorsubtype_blockio);
  	viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
  unregister_blk:
  	unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
  early_fail:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
812
813
814
  	return rc;
  }
  module_init(viodasd_init);
f9df68ec7   Stephen Rothwell   [POWERPC] iSeries...
815
  void __exit viodasd_exit(void)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
816
  {
f9df68ec7   Stephen Rothwell   [POWERPC] iSeries...
817
818
  	if (need_delete_probe)
  		driver_remove_file(&viodasd_driver.driver, &driver_attr_probe);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
819
820
  	vio_unregister_driver(&viodasd_driver);
  	vio_clearHandler(viomajorsubtype_blockio);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
821
  	viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
f9df68ec7   Stephen Rothwell   [POWERPC] iSeries...
822
  	unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
823
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
824
  module_exit(viodasd_exit);