Commit | Line | Data |
---|---|---|
667ef3c3 DM |
1 | /* sunvdc.c: Sun LDOM Virtual Disk Client. |
2 | * | |
3 | * Copyright (C) 2007 David S. Miller <davem@davemloft.net> | |
4 | */ | |
5 | ||
6 | #include <linux/module.h> | |
7 | #include <linux/kernel.h> | |
8 | #include <linux/types.h> | |
9 | #include <linux/blkdev.h> | |
10 | #include <linux/hdreg.h> | |
11 | #include <linux/genhd.h> | |
12 | #include <linux/slab.h> | |
13 | #include <linux/spinlock.h> | |
14 | #include <linux/completion.h> | |
15 | #include <linux/delay.h> | |
16 | #include <linux/init.h> | |
17 | #include <linux/list.h> | |
18 | ||
19 | #include <asm/vio.h> | |
20 | #include <asm/ldc.h> | |
21 | ||
22 | #define DRV_MODULE_NAME "sunvdc" | |
23 | #define PFX DRV_MODULE_NAME ": " | |
24 | #define DRV_MODULE_VERSION "1.0" | |
25 | #define DRV_MODULE_RELDATE "June 25, 2007" | |
26 | ||
27 | static char version[] __devinitdata = | |
28 | DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n"; | |
29 | MODULE_AUTHOR("David S. Miller (davem@davemloft.net)"); | |
30 | MODULE_DESCRIPTION("Sun LDOM virtual disk client driver"); | |
31 | MODULE_LICENSE("GPL"); | |
32 | MODULE_VERSION(DRV_MODULE_VERSION); | |
33 | ||
34 | #define VDC_TX_RING_SIZE 256 | |
35 | ||
36 | #define WAITING_FOR_LINK_UP 0x01 | |
37 | #define WAITING_FOR_TX_SPACE 0x02 | |
38 | #define WAITING_FOR_GEN_CMD 0x04 | |
39 | #define WAITING_FOR_ANY -1 | |
40 | ||
41 | struct vdc_req_entry { | |
42 | struct request *req; | |
43 | }; | |
44 | ||
45 | struct vdc_port { | |
46 | struct vio_driver_state vio; | |
47 | ||
667ef3c3 DM |
48 | struct gendisk *disk; |
49 | ||
50 | struct vdc_completion *cmp; | |
51 | ||
52 | u64 req_id; | |
53 | u64 seq; | |
54 | struct vdc_req_entry rq_arr[VDC_TX_RING_SIZE]; | |
55 | ||
56 | unsigned long ring_cookies; | |
57 | ||
58 | u64 max_xfer_size; | |
59 | u32 vdisk_block_size; | |
60 | ||
61 | /* The server fills these in for us in the disk attribute | |
62 | * ACK packet. | |
63 | */ | |
64 | u64 operations; | |
65 | u32 vdisk_size; | |
66 | u8 vdisk_type; | |
667ef3c3 DM |
67 | |
68 | char disk_name[32]; | |
69 | ||
70 | struct vio_disk_geom geom; | |
71 | struct vio_disk_vtoc label; | |
667ef3c3 DM |
72 | }; |
73 | ||
74 | static inline struct vdc_port *to_vdc_port(struct vio_driver_state *vio) | |
75 | { | |
76 | return container_of(vio, struct vdc_port, vio); | |
77 | } | |
78 | ||
667ef3c3 DM |
79 | /* Ordered from largest major to lowest */ |
80 | static struct vio_version vdc_versions[] = { | |
81 | { .major = 1, .minor = 0 }, | |
82 | }; | |
83 | ||
84 | #define VDCBLK_NAME "vdisk" | |
85 | static int vdc_major; | |
86 | #define PARTITION_SHIFT 3 | |
87 | ||
88 | static inline u32 vdc_tx_dring_avail(struct vio_dring_state *dr) | |
89 | { | |
90 | return vio_dring_avail(dr, VDC_TX_RING_SIZE); | |
91 | } | |
92 | ||
93 | static int vdc_getgeo(struct block_device *bdev, struct hd_geometry *geo) | |
94 | { | |
95 | struct gendisk *disk = bdev->bd_disk; | |
96 | struct vdc_port *port = disk->private_data; | |
97 | ||
98 | geo->heads = (u8) port->geom.num_hd; | |
99 | geo->sectors = (u8) port->geom.num_sec; | |
100 | geo->cylinders = port->geom.num_cyl; | |
101 | ||
102 | return 0; | |
103 | } | |
104 | ||
105 | static struct block_device_operations vdc_fops = { | |
106 | .owner = THIS_MODULE, | |
107 | .getgeo = vdc_getgeo, | |
108 | }; | |
109 | ||
110 | static void vdc_finish(struct vio_driver_state *vio, int err, int waiting_for) | |
111 | { | |
112 | if (vio->cmp && | |
113 | (waiting_for == -1 || | |
114 | vio->cmp->waiting_for == waiting_for)) { | |
115 | vio->cmp->err = err; | |
116 | complete(&vio->cmp->com); | |
117 | vio->cmp = NULL; | |
118 | } | |
119 | } | |
120 | ||
121 | static void vdc_handshake_complete(struct vio_driver_state *vio) | |
122 | { | |
123 | vdc_finish(vio, 0, WAITING_FOR_LINK_UP); | |
124 | } | |
125 | ||
126 | static int vdc_handle_unknown(struct vdc_port *port, void *arg) | |
127 | { | |
128 | struct vio_msg_tag *pkt = arg; | |
129 | ||
130 | printk(KERN_ERR PFX "Received unknown msg [%02x:%02x:%04x:%08x]\n", | |
131 | pkt->type, pkt->stype, pkt->stype_env, pkt->sid); | |
132 | printk(KERN_ERR PFX "Resetting connection.\n"); | |
133 | ||
134 | ldc_disconnect(port->vio.lp); | |
135 | ||
136 | return -ECONNRESET; | |
137 | } | |
138 | ||
139 | static int vdc_send_attr(struct vio_driver_state *vio) | |
140 | { | |
141 | struct vdc_port *port = to_vdc_port(vio); | |
142 | struct vio_disk_attr_info pkt; | |
143 | ||
144 | memset(&pkt, 0, sizeof(pkt)); | |
145 | ||
146 | pkt.tag.type = VIO_TYPE_CTRL; | |
147 | pkt.tag.stype = VIO_SUBTYPE_INFO; | |
148 | pkt.tag.stype_env = VIO_ATTR_INFO; | |
149 | pkt.tag.sid = vio_send_sid(vio); | |
150 | ||
151 | pkt.xfer_mode = VIO_DRING_MODE; | |
152 | pkt.vdisk_block_size = port->vdisk_block_size; | |
153 | pkt.max_xfer_size = port->max_xfer_size; | |
154 | ||
155 | viodbg(HS, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%lu]\n", | |
156 | pkt.xfer_mode, pkt.vdisk_block_size, pkt.max_xfer_size); | |
157 | ||
158 | return vio_ldc_send(&port->vio, &pkt, sizeof(pkt)); | |
159 | } | |
160 | ||
161 | static int vdc_handle_attr(struct vio_driver_state *vio, void *arg) | |
162 | { | |
163 | struct vdc_port *port = to_vdc_port(vio); | |
164 | struct vio_disk_attr_info *pkt = arg; | |
165 | ||
166 | viodbg(HS, "GOT ATTR stype[0x%x] ops[%lx] disk_size[%lu] disk_type[%x] " | |
167 | "xfer_mode[0x%x] blksz[%u] max_xfer[%lu]\n", | |
168 | pkt->tag.stype, pkt->operations, | |
169 | pkt->vdisk_size, pkt->vdisk_type, | |
170 | pkt->xfer_mode, pkt->vdisk_block_size, | |
171 | pkt->max_xfer_size); | |
172 | ||
173 | if (pkt->tag.stype == VIO_SUBTYPE_ACK) { | |
174 | switch (pkt->vdisk_type) { | |
175 | case VD_DISK_TYPE_DISK: | |
176 | case VD_DISK_TYPE_SLICE: | |
177 | break; | |
178 | ||
179 | default: | |
180 | printk(KERN_ERR PFX "%s: Bogus vdisk_type 0x%x\n", | |
181 | vio->name, pkt->vdisk_type); | |
182 | return -ECONNRESET; | |
183 | } | |
184 | ||
185 | if (pkt->vdisk_block_size > port->vdisk_block_size) { | |
186 | printk(KERN_ERR PFX "%s: BLOCK size increased " | |
187 | "%u --> %u\n", | |
188 | vio->name, | |
189 | port->vdisk_block_size, pkt->vdisk_block_size); | |
190 | return -ECONNRESET; | |
191 | } | |
192 | ||
193 | port->operations = pkt->operations; | |
194 | port->vdisk_size = pkt->vdisk_size; | |
195 | port->vdisk_type = pkt->vdisk_type; | |
196 | if (pkt->max_xfer_size < port->max_xfer_size) | |
197 | port->max_xfer_size = pkt->max_xfer_size; | |
198 | port->vdisk_block_size = pkt->vdisk_block_size; | |
199 | return 0; | |
200 | } else { | |
201 | printk(KERN_ERR PFX "%s: Attribute NACK\n", vio->name); | |
202 | ||
203 | return -ECONNRESET; | |
204 | } | |
205 | } | |
206 | ||
207 | static void vdc_end_special(struct vdc_port *port, struct vio_disk_desc *desc) | |
208 | { | |
209 | int err = desc->status; | |
210 | ||
211 | vdc_finish(&port->vio, -err, WAITING_FOR_GEN_CMD); | |
212 | } | |
213 | ||
214 | static void vdc_end_request(struct request *req, int uptodate, int num_sectors) | |
215 | { | |
216 | if (end_that_request_first(req, uptodate, num_sectors)) | |
217 | return; | |
218 | add_disk_randomness(req->rq_disk); | |
219 | end_that_request_last(req, uptodate); | |
220 | } | |
221 | ||
222 | static void vdc_end_one(struct vdc_port *port, struct vio_dring_state *dr, | |
223 | unsigned int index) | |
224 | { | |
225 | struct vio_disk_desc *desc = vio_dring_entry(dr, index); | |
226 | struct vdc_req_entry *rqe = &port->rq_arr[index]; | |
227 | struct request *req; | |
228 | ||
229 | if (unlikely(desc->hdr.state != VIO_DESC_DONE)) | |
230 | return; | |
231 | ||
232 | ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies); | |
233 | desc->hdr.state = VIO_DESC_FREE; | |
234 | dr->cons = (index + 1) & (VDC_TX_RING_SIZE - 1); | |
235 | ||
236 | req = rqe->req; | |
237 | if (req == NULL) { | |
238 | vdc_end_special(port, desc); | |
239 | return; | |
240 | } | |
241 | ||
242 | rqe->req = NULL; | |
243 | ||
244 | vdc_end_request(req, !desc->status, desc->size >> 9); | |
245 | ||
246 | if (blk_queue_stopped(port->disk->queue)) | |
247 | blk_start_queue(port->disk->queue); | |
248 | } | |
249 | ||
250 | static int vdc_ack(struct vdc_port *port, void *msgbuf) | |
251 | { | |
252 | struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING]; | |
253 | struct vio_dring_data *pkt = msgbuf; | |
254 | ||
255 | if (unlikely(pkt->dring_ident != dr->ident || | |
256 | pkt->start_idx != pkt->end_idx || | |
257 | pkt->start_idx >= VDC_TX_RING_SIZE)) | |
258 | return 0; | |
259 | ||
260 | vdc_end_one(port, dr, pkt->start_idx); | |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
265 | static int vdc_nack(struct vdc_port *port, void *msgbuf) | |
266 | { | |
267 | /* XXX Implement me XXX */ | |
268 | return 0; | |
269 | } | |
270 | ||
271 | static void vdc_event(void *arg, int event) | |
272 | { | |
273 | struct vdc_port *port = arg; | |
274 | struct vio_driver_state *vio = &port->vio; | |
275 | unsigned long flags; | |
276 | int err; | |
277 | ||
278 | spin_lock_irqsave(&vio->lock, flags); | |
279 | ||
280 | if (unlikely(event == LDC_EVENT_RESET || | |
281 | event == LDC_EVENT_UP)) { | |
282 | vio_link_state_change(vio, event); | |
283 | spin_unlock_irqrestore(&vio->lock, flags); | |
284 | return; | |
285 | } | |
286 | ||
287 | if (unlikely(event != LDC_EVENT_DATA_READY)) { | |
288 | printk(KERN_WARNING PFX "Unexpected LDC event %d\n", event); | |
289 | spin_unlock_irqrestore(&vio->lock, flags); | |
290 | return; | |
291 | } | |
292 | ||
293 | err = 0; | |
294 | while (1) { | |
295 | union { | |
296 | struct vio_msg_tag tag; | |
297 | u64 raw[8]; | |
298 | } msgbuf; | |
299 | ||
300 | err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf)); | |
301 | if (unlikely(err < 0)) { | |
302 | if (err == -ECONNRESET) | |
303 | vio_conn_reset(vio); | |
304 | break; | |
305 | } | |
306 | if (err == 0) | |
307 | break; | |
308 | viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n", | |
309 | msgbuf.tag.type, | |
310 | msgbuf.tag.stype, | |
311 | msgbuf.tag.stype_env, | |
312 | msgbuf.tag.sid); | |
313 | err = vio_validate_sid(vio, &msgbuf.tag); | |
314 | if (err < 0) | |
315 | break; | |
316 | ||
317 | if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) { | |
318 | if (msgbuf.tag.stype == VIO_SUBTYPE_ACK) | |
319 | err = vdc_ack(port, &msgbuf); | |
320 | else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK) | |
321 | err = vdc_nack(port, &msgbuf); | |
322 | else | |
323 | err = vdc_handle_unknown(port, &msgbuf); | |
324 | } else if (msgbuf.tag.type == VIO_TYPE_CTRL) { | |
325 | err = vio_control_pkt_engine(vio, &msgbuf); | |
326 | } else { | |
327 | err = vdc_handle_unknown(port, &msgbuf); | |
328 | } | |
329 | if (err < 0) | |
330 | break; | |
331 | } | |
332 | if (err < 0) | |
333 | vdc_finish(&port->vio, err, WAITING_FOR_ANY); | |
334 | spin_unlock_irqrestore(&vio->lock, flags); | |
335 | } | |
336 | ||
337 | static int __vdc_tx_trigger(struct vdc_port *port) | |
338 | { | |
339 | struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING]; | |
340 | struct vio_dring_data hdr = { | |
341 | .tag = { | |
342 | .type = VIO_TYPE_DATA, | |
343 | .stype = VIO_SUBTYPE_INFO, | |
344 | .stype_env = VIO_DRING_DATA, | |
345 | .sid = vio_send_sid(&port->vio), | |
346 | }, | |
347 | .dring_ident = dr->ident, | |
348 | .start_idx = dr->prod, | |
349 | .end_idx = dr->prod, | |
350 | }; | |
351 | int err, delay; | |
352 | ||
353 | hdr.seq = dr->snd_nxt; | |
354 | delay = 1; | |
355 | do { | |
356 | err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr)); | |
357 | if (err > 0) { | |
358 | dr->snd_nxt++; | |
359 | break; | |
360 | } | |
361 | udelay(delay); | |
362 | if ((delay <<= 1) > 128) | |
363 | delay = 128; | |
364 | } while (err == -EAGAIN); | |
365 | ||
366 | return err; | |
367 | } | |
368 | ||
369 | static int __send_request(struct request *req) | |
370 | { | |
371 | struct vdc_port *port = req->rq_disk->private_data; | |
372 | struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING]; | |
373 | struct scatterlist sg[port->ring_cookies]; | |
374 | struct vdc_req_entry *rqe; | |
375 | struct vio_disk_desc *desc; | |
376 | unsigned int map_perm; | |
377 | int nsg, err, i; | |
378 | u64 len; | |
379 | u8 op; | |
380 | ||
381 | map_perm = LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO; | |
382 | ||
383 | if (rq_data_dir(req) == READ) { | |
384 | map_perm |= LDC_MAP_W; | |
385 | op = VD_OP_BREAD; | |
386 | } else { | |
387 | map_perm |= LDC_MAP_R; | |
388 | op = VD_OP_BWRITE; | |
389 | } | |
390 | ||
391 | nsg = blk_rq_map_sg(req->q, req, sg); | |
392 | ||
393 | len = 0; | |
394 | for (i = 0; i < nsg; i++) | |
395 | len += sg[i].length; | |
396 | ||
397 | if (unlikely(vdc_tx_dring_avail(dr) < 1)) { | |
398 | blk_stop_queue(port->disk->queue); | |
399 | err = -ENOMEM; | |
400 | goto out; | |
401 | } | |
402 | ||
403 | desc = vio_dring_cur(dr); | |
404 | ||
405 | err = ldc_map_sg(port->vio.lp, sg, nsg, | |
406 | desc->cookies, port->ring_cookies, | |
407 | map_perm); | |
408 | if (err < 0) { | |
409 | printk(KERN_ERR PFX "ldc_map_sg() failure, err=%d.\n", err); | |
410 | return err; | |
411 | } | |
412 | ||
413 | rqe = &port->rq_arr[dr->prod]; | |
414 | rqe->req = req; | |
415 | ||
416 | desc->hdr.ack = VIO_ACK_ENABLE; | |
417 | desc->req_id = port->req_id; | |
418 | desc->operation = op; | |
419 | if (port->vdisk_type == VD_DISK_TYPE_DISK) { | |
1bd4b280 | 420 | desc->slice = 0xff; |
667ef3c3 DM |
421 | } else { |
422 | desc->slice = 0; | |
423 | } | |
424 | desc->status = ~0; | |
425 | desc->offset = (req->sector << 9) / port->vdisk_block_size; | |
426 | desc->size = len; | |
427 | desc->ncookies = err; | |
428 | ||
429 | /* This has to be a non-SMP write barrier because we are writing | |
430 | * to memory which is shared with the peer LDOM. | |
431 | */ | |
432 | wmb(); | |
433 | desc->hdr.state = VIO_DESC_READY; | |
434 | ||
435 | err = __vdc_tx_trigger(port); | |
436 | if (err < 0) { | |
437 | printk(KERN_ERR PFX "vdc_tx_trigger() failure, err=%d\n", err); | |
438 | } else { | |
439 | port->req_id++; | |
440 | dr->prod = (dr->prod + 1) & (VDC_TX_RING_SIZE - 1); | |
441 | } | |
442 | out: | |
443 | ||
444 | return err; | |
445 | } | |
446 | ||
165125e1 | 447 | static void do_vdc_request(struct request_queue *q) |
667ef3c3 DM |
448 | { |
449 | while (1) { | |
450 | struct request *req = elv_next_request(q); | |
451 | ||
452 | if (!req) | |
453 | break; | |
454 | ||
455 | blkdev_dequeue_request(req); | |
456 | if (__send_request(req) < 0) | |
457 | vdc_end_request(req, 0, req->hard_nr_sectors); | |
458 | } | |
459 | } | |
460 | ||
461 | static int generic_request(struct vdc_port *port, u8 op, void *buf, int len) | |
462 | { | |
463 | struct vio_dring_state *dr; | |
464 | struct vio_completion comp; | |
465 | struct vio_disk_desc *desc; | |
466 | unsigned int map_perm; | |
467 | unsigned long flags; | |
468 | int op_len, err; | |
469 | void *req_buf; | |
470 | ||
471 | if (!(((u64)1 << ((u64)op - 1)) & port->operations)) | |
472 | return -EOPNOTSUPP; | |
473 | ||
474 | switch (op) { | |
475 | case VD_OP_BREAD: | |
476 | case VD_OP_BWRITE: | |
477 | default: | |
478 | return -EINVAL; | |
479 | ||
480 | case VD_OP_FLUSH: | |
481 | op_len = 0; | |
482 | map_perm = 0; | |
483 | break; | |
484 | ||
485 | case VD_OP_GET_WCE: | |
486 | op_len = sizeof(u32); | |
487 | map_perm = LDC_MAP_W; | |
488 | break; | |
489 | ||
490 | case VD_OP_SET_WCE: | |
491 | op_len = sizeof(u32); | |
492 | map_perm = LDC_MAP_R; | |
493 | break; | |
494 | ||
495 | case VD_OP_GET_VTOC: | |
496 | op_len = sizeof(struct vio_disk_vtoc); | |
497 | map_perm = LDC_MAP_W; | |
498 | break; | |
499 | ||
500 | case VD_OP_SET_VTOC: | |
501 | op_len = sizeof(struct vio_disk_vtoc); | |
502 | map_perm = LDC_MAP_R; | |
503 | break; | |
504 | ||
505 | case VD_OP_GET_DISKGEOM: | |
506 | op_len = sizeof(struct vio_disk_geom); | |
507 | map_perm = LDC_MAP_W; | |
508 | break; | |
509 | ||
510 | case VD_OP_SET_DISKGEOM: | |
511 | op_len = sizeof(struct vio_disk_geom); | |
512 | map_perm = LDC_MAP_R; | |
513 | break; | |
514 | ||
515 | case VD_OP_SCSICMD: | |
516 | op_len = 16; | |
517 | map_perm = LDC_MAP_RW; | |
518 | break; | |
519 | ||
520 | case VD_OP_GET_DEVID: | |
521 | op_len = sizeof(struct vio_disk_devid); | |
522 | map_perm = LDC_MAP_W; | |
523 | break; | |
524 | ||
525 | case VD_OP_GET_EFI: | |
526 | case VD_OP_SET_EFI: | |
527 | return -EOPNOTSUPP; | |
528 | break; | |
529 | }; | |
530 | ||
531 | map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO; | |
532 | ||
533 | op_len = (op_len + 7) & ~7; | |
534 | req_buf = kzalloc(op_len, GFP_KERNEL); | |
535 | if (!req_buf) | |
536 | return -ENOMEM; | |
537 | ||
538 | if (len > op_len) | |
539 | len = op_len; | |
540 | ||
541 | if (map_perm & LDC_MAP_R) | |
542 | memcpy(req_buf, buf, len); | |
543 | ||
544 | spin_lock_irqsave(&port->vio.lock, flags); | |
545 | ||
546 | dr = &port->vio.drings[VIO_DRIVER_TX_RING]; | |
547 | ||
548 | /* XXX If we want to use this code generically we have to | |
549 | * XXX handle TX ring exhaustion etc. | |
550 | */ | |
551 | desc = vio_dring_cur(dr); | |
552 | ||
553 | err = ldc_map_single(port->vio.lp, req_buf, op_len, | |
554 | desc->cookies, port->ring_cookies, | |
555 | map_perm); | |
556 | if (err < 0) { | |
557 | spin_unlock_irqrestore(&port->vio.lock, flags); | |
558 | kfree(req_buf); | |
559 | return err; | |
560 | } | |
561 | ||
562 | init_completion(&comp.com); | |
563 | comp.waiting_for = WAITING_FOR_GEN_CMD; | |
564 | port->vio.cmp = ∁ | |
565 | ||
566 | desc->hdr.ack = VIO_ACK_ENABLE; | |
567 | desc->req_id = port->req_id; | |
568 | desc->operation = op; | |
569 | desc->slice = 0; | |
570 | desc->status = ~0; | |
571 | desc->offset = 0; | |
572 | desc->size = op_len; | |
573 | desc->ncookies = err; | |
574 | ||
575 | /* This has to be a non-SMP write barrier because we are writing | |
576 | * to memory which is shared with the peer LDOM. | |
577 | */ | |
578 | wmb(); | |
579 | desc->hdr.state = VIO_DESC_READY; | |
580 | ||
581 | err = __vdc_tx_trigger(port); | |
582 | if (err >= 0) { | |
583 | port->req_id++; | |
584 | dr->prod = (dr->prod + 1) & (VDC_TX_RING_SIZE - 1); | |
585 | spin_unlock_irqrestore(&port->vio.lock, flags); | |
586 | ||
587 | wait_for_completion(&comp.com); | |
588 | err = comp.err; | |
589 | } else { | |
590 | port->vio.cmp = NULL; | |
591 | spin_unlock_irqrestore(&port->vio.lock, flags); | |
592 | } | |
593 | ||
594 | if (map_perm & LDC_MAP_W) | |
595 | memcpy(buf, req_buf, len); | |
596 | ||
597 | kfree(req_buf); | |
598 | ||
599 | return err; | |
600 | } | |
601 | ||
602 | static int __devinit vdc_alloc_tx_ring(struct vdc_port *port) | |
603 | { | |
604 | struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING]; | |
605 | unsigned long len, entry_size; | |
606 | int ncookies; | |
607 | void *dring; | |
608 | ||
609 | entry_size = sizeof(struct vio_disk_desc) + | |
610 | (sizeof(struct ldc_trans_cookie) * port->ring_cookies); | |
611 | len = (VDC_TX_RING_SIZE * entry_size); | |
612 | ||
613 | ncookies = VIO_MAX_RING_COOKIES; | |
614 | dring = ldc_alloc_exp_dring(port->vio.lp, len, | |
615 | dr->cookies, &ncookies, | |
616 | (LDC_MAP_SHADOW | | |
617 | LDC_MAP_DIRECT | | |
618 | LDC_MAP_RW)); | |
619 | if (IS_ERR(dring)) | |
620 | return PTR_ERR(dring); | |
621 | ||
622 | dr->base = dring; | |
623 | dr->entry_size = entry_size; | |
624 | dr->num_entries = VDC_TX_RING_SIZE; | |
625 | dr->prod = dr->cons = 0; | |
626 | dr->pending = VDC_TX_RING_SIZE; | |
627 | dr->ncookies = ncookies; | |
628 | ||
629 | return 0; | |
630 | } | |
631 | ||
632 | static void vdc_free_tx_ring(struct vdc_port *port) | |
633 | { | |
634 | struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING]; | |
635 | ||
636 | if (dr->base) { | |
637 | ldc_free_exp_dring(port->vio.lp, dr->base, | |
638 | (dr->entry_size * dr->num_entries), | |
639 | dr->cookies, dr->ncookies); | |
640 | dr->base = NULL; | |
641 | dr->entry_size = 0; | |
642 | dr->num_entries = 0; | |
643 | dr->pending = 0; | |
644 | dr->ncookies = 0; | |
645 | } | |
646 | } | |
647 | ||
648 | static int probe_disk(struct vdc_port *port) | |
649 | { | |
650 | struct vio_completion comp; | |
651 | struct request_queue *q; | |
652 | struct gendisk *g; | |
653 | int err; | |
654 | ||
655 | init_completion(&comp.com); | |
656 | comp.err = 0; | |
657 | comp.waiting_for = WAITING_FOR_LINK_UP; | |
658 | port->vio.cmp = ∁ | |
659 | ||
660 | vio_port_up(&port->vio); | |
661 | ||
662 | wait_for_completion(&comp.com); | |
663 | if (comp.err) | |
664 | return comp.err; | |
665 | ||
666 | err = generic_request(port, VD_OP_GET_VTOC, | |
667 | &port->label, sizeof(port->label)); | |
668 | if (err < 0) { | |
669 | printk(KERN_ERR PFX "VD_OP_GET_VTOC returns error %d\n", err); | |
670 | return err; | |
671 | } | |
672 | ||
673 | err = generic_request(port, VD_OP_GET_DISKGEOM, | |
674 | &port->geom, sizeof(port->geom)); | |
675 | if (err < 0) { | |
676 | printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns " | |
677 | "error %d\n", err); | |
678 | return err; | |
679 | } | |
680 | ||
681 | port->vdisk_size = ((u64)port->geom.num_cyl * | |
682 | (u64)port->geom.num_hd * | |
683 | (u64)port->geom.num_sec); | |
684 | ||
685 | q = blk_init_queue(do_vdc_request, &port->vio.lock); | |
686 | if (!q) { | |
687 | printk(KERN_ERR PFX "%s: Could not allocate queue.\n", | |
688 | port->vio.name); | |
689 | return -ENOMEM; | |
690 | } | |
691 | g = alloc_disk(1 << PARTITION_SHIFT); | |
692 | if (!g) { | |
693 | printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n", | |
694 | port->vio.name); | |
695 | blk_cleanup_queue(q); | |
696 | return -ENOMEM; | |
697 | } | |
698 | ||
699 | port->disk = g; | |
700 | ||
701 | blk_queue_max_hw_segments(q, port->ring_cookies); | |
702 | blk_queue_max_phys_segments(q, port->ring_cookies); | |
703 | blk_queue_max_sectors(q, port->max_xfer_size); | |
704 | g->major = vdc_major; | |
91ba3c21 | 705 | g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT; |
667ef3c3 DM |
706 | strcpy(g->disk_name, port->disk_name); |
707 | ||
708 | g->fops = &vdc_fops; | |
709 | g->queue = q; | |
710 | g->private_data = port; | |
711 | g->driverfs_dev = &port->vio.vdev->dev; | |
712 | ||
713 | set_capacity(g, port->vdisk_size); | |
714 | ||
715 | printk(KERN_INFO PFX "%s: %u sectors (%u MB)\n", | |
716 | g->disk_name, | |
717 | port->vdisk_size, (port->vdisk_size >> (20 - 9))); | |
718 | ||
719 | add_disk(g); | |
720 | ||
721 | return 0; | |
722 | } | |
723 | ||
724 | static struct ldc_channel_config vdc_ldc_cfg = { | |
725 | .event = vdc_event, | |
726 | .mtu = 64, | |
727 | .mode = LDC_MODE_UNRELIABLE, | |
728 | }; | |
729 | ||
730 | static struct vio_driver_ops vdc_vio_ops = { | |
731 | .send_attr = vdc_send_attr, | |
732 | .handle_attr = vdc_handle_attr, | |
733 | .handshake_complete = vdc_handshake_complete, | |
734 | }; | |
735 | ||
80dc35df DM |
736 | static void print_version(void) |
737 | { | |
738 | static int version_printed; | |
739 | ||
740 | if (version_printed++ == 0) | |
741 | printk(KERN_INFO "%s", version); | |
742 | } | |
743 | ||
667ef3c3 DM |
744 | static int __devinit vdc_port_probe(struct vio_dev *vdev, |
745 | const struct vio_device_id *id) | |
746 | { | |
43fdf274 | 747 | struct mdesc_handle *hp; |
667ef3c3 | 748 | struct vdc_port *port; |
667ef3c3 DM |
749 | int err; |
750 | ||
80dc35df | 751 | print_version(); |
667ef3c3 | 752 | |
43fdf274 | 753 | hp = mdesc_grab(); |
667ef3c3 | 754 | |
43fdf274 | 755 | err = -ENODEV; |
91ba3c21 DM |
756 | if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) { |
757 | printk(KERN_ERR PFX "Port id [%lu] too large.\n", | |
758 | vdev->dev_no); | |
43fdf274 | 759 | goto err_out_release_mdesc; |
667ef3c3 DM |
760 | } |
761 | ||
762 | port = kzalloc(sizeof(*port), GFP_KERNEL); | |
43fdf274 | 763 | err = -ENOMEM; |
667ef3c3 DM |
764 | if (!port) { |
765 | printk(KERN_ERR PFX "Cannot allocate vdc_port.\n"); | |
43fdf274 | 766 | goto err_out_release_mdesc; |
667ef3c3 DM |
767 | } |
768 | ||
91ba3c21 | 769 | if (vdev->dev_no >= 26) |
667ef3c3 DM |
770 | snprintf(port->disk_name, sizeof(port->disk_name), |
771 | VDCBLK_NAME "%c%c", | |
91ba3c21 DM |
772 | 'a' + ((int)vdev->dev_no / 26) - 1, |
773 | 'a' + ((int)vdev->dev_no % 26)); | |
667ef3c3 DM |
774 | else |
775 | snprintf(port->disk_name, sizeof(port->disk_name), | |
91ba3c21 | 776 | VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26)); |
667ef3c3 | 777 | |
43fdf274 | 778 | err = vio_driver_init(&port->vio, vdev, VDEV_DISK, |
667ef3c3 DM |
779 | vdc_versions, ARRAY_SIZE(vdc_versions), |
780 | &vdc_vio_ops, port->disk_name); | |
781 | if (err) | |
782 | goto err_out_free_port; | |
783 | ||
784 | port->vdisk_block_size = 512; | |
785 | port->max_xfer_size = ((128 * 1024) / port->vdisk_block_size); | |
786 | port->ring_cookies = ((port->max_xfer_size * | |
787 | port->vdisk_block_size) / PAGE_SIZE) + 2; | |
788 | ||
789 | err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port); | |
790 | if (err) | |
791 | goto err_out_free_port; | |
792 | ||
793 | err = vdc_alloc_tx_ring(port); | |
794 | if (err) | |
795 | goto err_out_free_ldc; | |
796 | ||
797 | err = probe_disk(port); | |
798 | if (err) | |
799 | goto err_out_free_tx_ring; | |
800 | ||
667ef3c3 DM |
801 | dev_set_drvdata(&vdev->dev, port); |
802 | ||
43fdf274 DM |
803 | mdesc_release(hp); |
804 | ||
667ef3c3 DM |
805 | return 0; |
806 | ||
807 | err_out_free_tx_ring: | |
808 | vdc_free_tx_ring(port); | |
809 | ||
810 | err_out_free_ldc: | |
811 | vio_ldc_free(&port->vio); | |
812 | ||
813 | err_out_free_port: | |
814 | kfree(port); | |
815 | ||
43fdf274 DM |
816 | err_out_release_mdesc: |
817 | mdesc_release(hp); | |
667ef3c3 DM |
818 | return err; |
819 | } | |
820 | ||
821 | static int vdc_port_remove(struct vio_dev *vdev) | |
822 | { | |
823 | struct vdc_port *port = dev_get_drvdata(&vdev->dev); | |
824 | ||
825 | if (port) { | |
826 | del_timer_sync(&port->vio.timer); | |
827 | ||
828 | vdc_free_tx_ring(port); | |
829 | vio_ldc_free(&port->vio); | |
830 | ||
831 | dev_set_drvdata(&vdev->dev, NULL); | |
832 | ||
833 | kfree(port); | |
834 | } | |
835 | return 0; | |
836 | } | |
837 | ||
838 | static struct vio_device_id vdc_port_match[] = { | |
839 | { | |
840 | .type = "vdc-port", | |
841 | }, | |
842 | {}, | |
843 | }; | |
da68e081 | 844 | MODULE_DEVICE_TABLE(vio, vdc_port_match); |
667ef3c3 DM |
845 | |
846 | static struct vio_driver vdc_port_driver = { | |
847 | .id_table = vdc_port_match, | |
848 | .probe = vdc_port_probe, | |
849 | .remove = vdc_port_remove, | |
850 | .driver = { | |
851 | .name = "vdc_port", | |
852 | .owner = THIS_MODULE, | |
853 | } | |
854 | }; | |
855 | ||
667ef3c3 DM |
856 | static int __init vdc_init(void) |
857 | { | |
858 | int err; | |
859 | ||
860 | err = register_blkdev(0, VDCBLK_NAME); | |
861 | if (err < 0) | |
862 | goto out_err; | |
863 | ||
864 | vdc_major = err; | |
667ef3c3 DM |
865 | |
866 | err = vio_register_driver(&vdc_port_driver); | |
867 | if (err) | |
80dc35df | 868 | goto out_unregister_blkdev; |
667ef3c3 DM |
869 | |
870 | return 0; | |
871 | ||
667ef3c3 DM |
872 | out_unregister_blkdev: |
873 | unregister_blkdev(vdc_major, VDCBLK_NAME); | |
874 | vdc_major = 0; | |
875 | ||
876 | out_err: | |
877 | return err; | |
878 | } | |
879 | ||
880 | static void __exit vdc_exit(void) | |
881 | { | |
882 | vio_unregister_driver(&vdc_port_driver); | |
667ef3c3 DM |
883 | unregister_blkdev(vdc_major, VDCBLK_NAME); |
884 | } | |
885 | ||
886 | module_init(vdc_init); | |
887 | module_exit(vdc_exit); |