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