xref: /openbmc/linux/drivers/block/sunvdc.c (revision 53809828)
1 /* sunvdc.c: Sun LDOM Virtual Disk Client.
2  *
3  * Copyright (C) 2007, 2008 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/cdrom.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
15 #include <linux/completion.h>
16 #include <linux/delay.h>
17 #include <linux/init.h>
18 #include <linux/list.h>
19 #include <linux/scatterlist.h>
20 
21 #include <asm/vio.h>
22 #include <asm/ldc.h>
23 
24 #define DRV_MODULE_NAME		"sunvdc"
25 #define PFX DRV_MODULE_NAME	": "
26 #define DRV_MODULE_VERSION	"1.2"
27 #define DRV_MODULE_RELDATE	"November 24, 2014"
28 
29 static char version[] =
30 	DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
31 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
32 MODULE_DESCRIPTION("Sun LDOM virtual disk client driver");
33 MODULE_LICENSE("GPL");
34 MODULE_VERSION(DRV_MODULE_VERSION);
35 
36 #define VDC_TX_RING_SIZE	512
37 #define VDC_DEFAULT_BLK_SIZE	512
38 
39 #define MAX_XFER_BLKS		(128 * 1024)
40 #define MAX_XFER_SIZE		(MAX_XFER_BLKS / VDC_DEFAULT_BLK_SIZE)
41 #define MAX_RING_COOKIES	((MAX_XFER_BLKS / PAGE_SIZE) + 2)
42 
43 #define WAITING_FOR_LINK_UP	0x01
44 #define WAITING_FOR_TX_SPACE	0x02
45 #define WAITING_FOR_GEN_CMD	0x04
46 #define WAITING_FOR_ANY		-1
47 
48 static struct workqueue_struct *sunvdc_wq;
49 
50 struct vdc_req_entry {
51 	struct request		*req;
52 };
53 
54 struct vdc_port {
55 	struct vio_driver_state	vio;
56 
57 	struct gendisk		*disk;
58 
59 	struct vdc_completion	*cmp;
60 
61 	u64			req_id;
62 	u64			seq;
63 	struct vdc_req_entry	rq_arr[VDC_TX_RING_SIZE];
64 
65 	unsigned long		ring_cookies;
66 
67 	u64			max_xfer_size;
68 	u32			vdisk_block_size;
69 
70 	u64			ldc_timeout;
71 	struct timer_list	ldc_reset_timer;
72 	struct work_struct	ldc_reset_work;
73 
74 	/* The server fills these in for us in the disk attribute
75 	 * ACK packet.
76 	 */
77 	u64			operations;
78 	u32			vdisk_size;
79 	u8			vdisk_type;
80 	u8			vdisk_mtype;
81 	u32			vdisk_phys_blksz;
82 
83 	char			disk_name[32];
84 };
85 
86 static void vdc_ldc_reset(struct vdc_port *port);
87 static void vdc_ldc_reset_work(struct work_struct *work);
88 static void vdc_ldc_reset_timer(struct timer_list *t);
89 
90 static inline struct vdc_port *to_vdc_port(struct vio_driver_state *vio)
91 {
92 	return container_of(vio, struct vdc_port, vio);
93 }
94 
95 /* Ordered from largest major to lowest */
96 static struct vio_version vdc_versions[] = {
97 	{ .major = 1, .minor = 2 },
98 	{ .major = 1, .minor = 1 },
99 	{ .major = 1, .minor = 0 },
100 };
101 
102 static inline int vdc_version_supported(struct vdc_port *port,
103 					u16 major, u16 minor)
104 {
105 	return port->vio.ver.major == major && port->vio.ver.minor >= minor;
106 }
107 
108 #define VDCBLK_NAME	"vdisk"
109 static int vdc_major;
110 #define PARTITION_SHIFT	3
111 
112 static inline u32 vdc_tx_dring_avail(struct vio_dring_state *dr)
113 {
114 	return vio_dring_avail(dr, VDC_TX_RING_SIZE);
115 }
116 
117 static int vdc_getgeo(struct block_device *bdev, struct hd_geometry *geo)
118 {
119 	struct gendisk *disk = bdev->bd_disk;
120 	sector_t nsect = get_capacity(disk);
121 	sector_t cylinders = nsect;
122 
123 	geo->heads = 0xff;
124 	geo->sectors = 0x3f;
125 	sector_div(cylinders, geo->heads * geo->sectors);
126 	geo->cylinders = cylinders;
127 	if ((sector_t)(geo->cylinders + 1) * geo->heads * geo->sectors < nsect)
128 		geo->cylinders = 0xffff;
129 
130 	return 0;
131 }
132 
133 /* Add ioctl/CDROM_GET_CAPABILITY to support cdrom_id in udev
134  * when vdisk_mtype is VD_MEDIA_TYPE_CD or VD_MEDIA_TYPE_DVD.
135  * Needed to be able to install inside an ldom from an iso image.
136  */
137 static int vdc_ioctl(struct block_device *bdev, fmode_t mode,
138 		     unsigned command, unsigned long argument)
139 {
140 	int i;
141 	struct gendisk *disk;
142 
143 	switch (command) {
144 	case CDROMMULTISESSION:
145 		pr_debug(PFX "Multisession CDs not supported\n");
146 		for (i = 0; i < sizeof(struct cdrom_multisession); i++)
147 			if (put_user(0, (char __user *)(argument + i)))
148 				return -EFAULT;
149 		return 0;
150 
151 	case CDROM_GET_CAPABILITY:
152 		disk = bdev->bd_disk;
153 
154 		if (bdev->bd_disk && (disk->flags & GENHD_FL_CD))
155 			return 0;
156 		return -EINVAL;
157 
158 	default:
159 		pr_debug(PFX "ioctl %08x not supported\n", command);
160 		return -EINVAL;
161 	}
162 }
163 
164 static const struct block_device_operations vdc_fops = {
165 	.owner		= THIS_MODULE,
166 	.getgeo		= vdc_getgeo,
167 	.ioctl		= vdc_ioctl,
168 };
169 
170 static void vdc_blk_queue_start(struct vdc_port *port)
171 {
172 	struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
173 
174 	/* restart blk queue when ring is half emptied. also called after
175 	 * handshake completes, so check for initial handshake before we've
176 	 * allocated a disk.
177 	 */
178 	if (port->disk && blk_queue_stopped(port->disk->queue) &&
179 	    vdc_tx_dring_avail(dr) * 100 / VDC_TX_RING_SIZE >= 50) {
180 		blk_start_queue(port->disk->queue);
181 	}
182 
183 }
184 
185 static void vdc_finish(struct vio_driver_state *vio, int err, int waiting_for)
186 {
187 	if (vio->cmp &&
188 	    (waiting_for == -1 ||
189 	     vio->cmp->waiting_for == waiting_for)) {
190 		vio->cmp->err = err;
191 		complete(&vio->cmp->com);
192 		vio->cmp = NULL;
193 	}
194 }
195 
196 static void vdc_handshake_complete(struct vio_driver_state *vio)
197 {
198 	struct vdc_port *port = to_vdc_port(vio);
199 
200 	del_timer(&port->ldc_reset_timer);
201 	vdc_finish(vio, 0, WAITING_FOR_LINK_UP);
202 	vdc_blk_queue_start(port);
203 }
204 
205 static int vdc_handle_unknown(struct vdc_port *port, void *arg)
206 {
207 	struct vio_msg_tag *pkt = arg;
208 
209 	printk(KERN_ERR PFX "Received unknown msg [%02x:%02x:%04x:%08x]\n",
210 	       pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
211 	printk(KERN_ERR PFX "Resetting connection.\n");
212 
213 	ldc_disconnect(port->vio.lp);
214 
215 	return -ECONNRESET;
216 }
217 
218 static int vdc_send_attr(struct vio_driver_state *vio)
219 {
220 	struct vdc_port *port = to_vdc_port(vio);
221 	struct vio_disk_attr_info pkt;
222 
223 	memset(&pkt, 0, sizeof(pkt));
224 
225 	pkt.tag.type = VIO_TYPE_CTRL;
226 	pkt.tag.stype = VIO_SUBTYPE_INFO;
227 	pkt.tag.stype_env = VIO_ATTR_INFO;
228 	pkt.tag.sid = vio_send_sid(vio);
229 
230 	pkt.xfer_mode = VIO_DRING_MODE;
231 	pkt.vdisk_block_size = port->vdisk_block_size;
232 	pkt.max_xfer_size = port->max_xfer_size;
233 
234 	viodbg(HS, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
235 	       pkt.xfer_mode, pkt.vdisk_block_size, pkt.max_xfer_size);
236 
237 	return vio_ldc_send(&port->vio, &pkt, sizeof(pkt));
238 }
239 
240 static int vdc_handle_attr(struct vio_driver_state *vio, void *arg)
241 {
242 	struct vdc_port *port = to_vdc_port(vio);
243 	struct vio_disk_attr_info *pkt = arg;
244 
245 	viodbg(HS, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] "
246 	       "mtype[0x%x] xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
247 	       pkt->tag.stype, pkt->operations,
248 	       pkt->vdisk_size, pkt->vdisk_type, pkt->vdisk_mtype,
249 	       pkt->xfer_mode, pkt->vdisk_block_size,
250 	       pkt->max_xfer_size);
251 
252 	if (pkt->tag.stype == VIO_SUBTYPE_ACK) {
253 		switch (pkt->vdisk_type) {
254 		case VD_DISK_TYPE_DISK:
255 		case VD_DISK_TYPE_SLICE:
256 			break;
257 
258 		default:
259 			printk(KERN_ERR PFX "%s: Bogus vdisk_type 0x%x\n",
260 			       vio->name, pkt->vdisk_type);
261 			return -ECONNRESET;
262 		}
263 
264 		if (pkt->vdisk_block_size > port->vdisk_block_size) {
265 			printk(KERN_ERR PFX "%s: BLOCK size increased "
266 			       "%u --> %u\n",
267 			       vio->name,
268 			       port->vdisk_block_size, pkt->vdisk_block_size);
269 			return -ECONNRESET;
270 		}
271 
272 		port->operations = pkt->operations;
273 		port->vdisk_type = pkt->vdisk_type;
274 		if (vdc_version_supported(port, 1, 1)) {
275 			port->vdisk_size = pkt->vdisk_size;
276 			port->vdisk_mtype = pkt->vdisk_mtype;
277 		}
278 		if (pkt->max_xfer_size < port->max_xfer_size)
279 			port->max_xfer_size = pkt->max_xfer_size;
280 		port->vdisk_block_size = pkt->vdisk_block_size;
281 
282 		port->vdisk_phys_blksz = VDC_DEFAULT_BLK_SIZE;
283 		if (vdc_version_supported(port, 1, 2))
284 			port->vdisk_phys_blksz = pkt->phys_block_size;
285 
286 		return 0;
287 	} else {
288 		printk(KERN_ERR PFX "%s: Attribute NACK\n", vio->name);
289 
290 		return -ECONNRESET;
291 	}
292 }
293 
294 static void vdc_end_special(struct vdc_port *port, struct vio_disk_desc *desc)
295 {
296 	int err = desc->status;
297 
298 	vdc_finish(&port->vio, -err, WAITING_FOR_GEN_CMD);
299 }
300 
301 static void vdc_end_one(struct vdc_port *port, struct vio_dring_state *dr,
302 			unsigned int index)
303 {
304 	struct vio_disk_desc *desc = vio_dring_entry(dr, index);
305 	struct vdc_req_entry *rqe = &port->rq_arr[index];
306 	struct request *req;
307 
308 	if (unlikely(desc->hdr.state != VIO_DESC_DONE))
309 		return;
310 
311 	ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
312 	desc->hdr.state = VIO_DESC_FREE;
313 	dr->cons = vio_dring_next(dr, index);
314 
315 	req = rqe->req;
316 	if (req == NULL) {
317 		vdc_end_special(port, desc);
318 		return;
319 	}
320 
321 	rqe->req = NULL;
322 
323 	__blk_end_request(req, (desc->status ? BLK_STS_IOERR : 0), desc->size);
324 
325 	vdc_blk_queue_start(port);
326 }
327 
328 static int vdc_ack(struct vdc_port *port, void *msgbuf)
329 {
330 	struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
331 	struct vio_dring_data *pkt = msgbuf;
332 
333 	if (unlikely(pkt->dring_ident != dr->ident ||
334 		     pkt->start_idx != pkt->end_idx ||
335 		     pkt->start_idx >= VDC_TX_RING_SIZE))
336 		return 0;
337 
338 	vdc_end_one(port, dr, pkt->start_idx);
339 
340 	return 0;
341 }
342 
343 static int vdc_nack(struct vdc_port *port, void *msgbuf)
344 {
345 	/* XXX Implement me XXX */
346 	return 0;
347 }
348 
349 static void vdc_event(void *arg, int event)
350 {
351 	struct vdc_port *port = arg;
352 	struct vio_driver_state *vio = &port->vio;
353 	unsigned long flags;
354 	int err;
355 
356 	spin_lock_irqsave(&vio->lock, flags);
357 
358 	if (unlikely(event == LDC_EVENT_RESET)) {
359 		vio_link_state_change(vio, event);
360 		queue_work(sunvdc_wq, &port->ldc_reset_work);
361 		goto out;
362 	}
363 
364 	if (unlikely(event == LDC_EVENT_UP)) {
365 		vio_link_state_change(vio, event);
366 		goto out;
367 	}
368 
369 	if (unlikely(event != LDC_EVENT_DATA_READY)) {
370 		pr_warn(PFX "Unexpected LDC event %d\n", event);
371 		goto out;
372 	}
373 
374 	err = 0;
375 	while (1) {
376 		union {
377 			struct vio_msg_tag tag;
378 			u64 raw[8];
379 		} msgbuf;
380 
381 		err = ldc_read(vio->lp, &msgbuf, sizeof(msgbuf));
382 		if (unlikely(err < 0)) {
383 			if (err == -ECONNRESET)
384 				vio_conn_reset(vio);
385 			break;
386 		}
387 		if (err == 0)
388 			break;
389 		viodbg(DATA, "TAG [%02x:%02x:%04x:%08x]\n",
390 		       msgbuf.tag.type,
391 		       msgbuf.tag.stype,
392 		       msgbuf.tag.stype_env,
393 		       msgbuf.tag.sid);
394 		err = vio_validate_sid(vio, &msgbuf.tag);
395 		if (err < 0)
396 			break;
397 
398 		if (likely(msgbuf.tag.type == VIO_TYPE_DATA)) {
399 			if (msgbuf.tag.stype == VIO_SUBTYPE_ACK)
400 				err = vdc_ack(port, &msgbuf);
401 			else if (msgbuf.tag.stype == VIO_SUBTYPE_NACK)
402 				err = vdc_nack(port, &msgbuf);
403 			else
404 				err = vdc_handle_unknown(port, &msgbuf);
405 		} else if (msgbuf.tag.type == VIO_TYPE_CTRL) {
406 			err = vio_control_pkt_engine(vio, &msgbuf);
407 		} else {
408 			err = vdc_handle_unknown(port, &msgbuf);
409 		}
410 		if (err < 0)
411 			break;
412 	}
413 	if (err < 0)
414 		vdc_finish(&port->vio, err, WAITING_FOR_ANY);
415 out:
416 	spin_unlock_irqrestore(&vio->lock, flags);
417 }
418 
419 static int __vdc_tx_trigger(struct vdc_port *port)
420 {
421 	struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
422 	struct vio_dring_data hdr = {
423 		.tag = {
424 			.type		= VIO_TYPE_DATA,
425 			.stype		= VIO_SUBTYPE_INFO,
426 			.stype_env	= VIO_DRING_DATA,
427 			.sid		= vio_send_sid(&port->vio),
428 		},
429 		.dring_ident		= dr->ident,
430 		.start_idx		= dr->prod,
431 		.end_idx		= dr->prod,
432 	};
433 	int err, delay;
434 
435 	hdr.seq = dr->snd_nxt;
436 	delay = 1;
437 	do {
438 		err = vio_ldc_send(&port->vio, &hdr, sizeof(hdr));
439 		if (err > 0) {
440 			dr->snd_nxt++;
441 			break;
442 		}
443 		udelay(delay);
444 		if ((delay <<= 1) > 128)
445 			delay = 128;
446 	} while (err == -EAGAIN);
447 
448 	if (err == -ENOTCONN)
449 		vdc_ldc_reset(port);
450 	return err;
451 }
452 
453 static int __send_request(struct request *req)
454 {
455 	struct vdc_port *port = req->rq_disk->private_data;
456 	struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
457 	struct scatterlist sg[MAX_RING_COOKIES];
458 	struct vdc_req_entry *rqe;
459 	struct vio_disk_desc *desc;
460 	unsigned int map_perm;
461 	int nsg, err, i;
462 	u64 len;
463 	u8 op;
464 
465 	if (WARN_ON(port->ring_cookies > MAX_RING_COOKIES))
466 		return -EINVAL;
467 
468 	map_perm = LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
469 
470 	if (rq_data_dir(req) == READ) {
471 		map_perm |= LDC_MAP_W;
472 		op = VD_OP_BREAD;
473 	} else {
474 		map_perm |= LDC_MAP_R;
475 		op = VD_OP_BWRITE;
476 	}
477 
478 	sg_init_table(sg, port->ring_cookies);
479 	nsg = blk_rq_map_sg(req->q, req, sg);
480 
481 	len = 0;
482 	for (i = 0; i < nsg; i++)
483 		len += sg[i].length;
484 
485 	desc = vio_dring_cur(dr);
486 
487 	err = ldc_map_sg(port->vio.lp, sg, nsg,
488 			 desc->cookies, port->ring_cookies,
489 			 map_perm);
490 	if (err < 0) {
491 		printk(KERN_ERR PFX "ldc_map_sg() failure, err=%d.\n", err);
492 		return err;
493 	}
494 
495 	rqe = &port->rq_arr[dr->prod];
496 	rqe->req = req;
497 
498 	desc->hdr.ack = VIO_ACK_ENABLE;
499 	desc->req_id = port->req_id;
500 	desc->operation = op;
501 	if (port->vdisk_type == VD_DISK_TYPE_DISK) {
502 		desc->slice = 0xff;
503 	} else {
504 		desc->slice = 0;
505 	}
506 	desc->status = ~0;
507 	desc->offset = (blk_rq_pos(req) << 9) / port->vdisk_block_size;
508 	desc->size = len;
509 	desc->ncookies = err;
510 
511 	/* This has to be a non-SMP write barrier because we are writing
512 	 * to memory which is shared with the peer LDOM.
513 	 */
514 	wmb();
515 	desc->hdr.state = VIO_DESC_READY;
516 
517 	err = __vdc_tx_trigger(port);
518 	if (err < 0) {
519 		printk(KERN_ERR PFX "vdc_tx_trigger() failure, err=%d\n", err);
520 	} else {
521 		port->req_id++;
522 		dr->prod = vio_dring_next(dr, dr->prod);
523 	}
524 
525 	return err;
526 }
527 
528 static void do_vdc_request(struct request_queue *rq)
529 {
530 	struct request *req;
531 
532 	while ((req = blk_peek_request(rq)) != NULL) {
533 		struct vdc_port *port;
534 		struct vio_dring_state *dr;
535 
536 		port = req->rq_disk->private_data;
537 		dr = &port->vio.drings[VIO_DRIVER_TX_RING];
538 		if (unlikely(vdc_tx_dring_avail(dr) < 1))
539 			goto wait;
540 
541 		blk_start_request(req);
542 
543 		if (__send_request(req) < 0) {
544 			blk_requeue_request(rq, req);
545 wait:
546 			/* Avoid pointless unplugs. */
547 			blk_stop_queue(rq);
548 			break;
549 		}
550 	}
551 }
552 
553 static int generic_request(struct vdc_port *port, u8 op, void *buf, int len)
554 {
555 	struct vio_dring_state *dr;
556 	struct vio_completion comp;
557 	struct vio_disk_desc *desc;
558 	unsigned int map_perm;
559 	unsigned long flags;
560 	int op_len, err;
561 	void *req_buf;
562 
563 	if (!(((u64)1 << (u64)op) & port->operations))
564 		return -EOPNOTSUPP;
565 
566 	switch (op) {
567 	case VD_OP_BREAD:
568 	case VD_OP_BWRITE:
569 	default:
570 		return -EINVAL;
571 
572 	case VD_OP_FLUSH:
573 		op_len = 0;
574 		map_perm = 0;
575 		break;
576 
577 	case VD_OP_GET_WCE:
578 		op_len = sizeof(u32);
579 		map_perm = LDC_MAP_W;
580 		break;
581 
582 	case VD_OP_SET_WCE:
583 		op_len = sizeof(u32);
584 		map_perm = LDC_MAP_R;
585 		break;
586 
587 	case VD_OP_GET_VTOC:
588 		op_len = sizeof(struct vio_disk_vtoc);
589 		map_perm = LDC_MAP_W;
590 		break;
591 
592 	case VD_OP_SET_VTOC:
593 		op_len = sizeof(struct vio_disk_vtoc);
594 		map_perm = LDC_MAP_R;
595 		break;
596 
597 	case VD_OP_GET_DISKGEOM:
598 		op_len = sizeof(struct vio_disk_geom);
599 		map_perm = LDC_MAP_W;
600 		break;
601 
602 	case VD_OP_SET_DISKGEOM:
603 		op_len = sizeof(struct vio_disk_geom);
604 		map_perm = LDC_MAP_R;
605 		break;
606 
607 	case VD_OP_SCSICMD:
608 		op_len = 16;
609 		map_perm = LDC_MAP_RW;
610 		break;
611 
612 	case VD_OP_GET_DEVID:
613 		op_len = sizeof(struct vio_disk_devid);
614 		map_perm = LDC_MAP_W;
615 		break;
616 
617 	case VD_OP_GET_EFI:
618 	case VD_OP_SET_EFI:
619 		return -EOPNOTSUPP;
620 		break;
621 	};
622 
623 	map_perm |= LDC_MAP_SHADOW | LDC_MAP_DIRECT | LDC_MAP_IO;
624 
625 	op_len = (op_len + 7) & ~7;
626 	req_buf = kzalloc(op_len, GFP_KERNEL);
627 	if (!req_buf)
628 		return -ENOMEM;
629 
630 	if (len > op_len)
631 		len = op_len;
632 
633 	if (map_perm & LDC_MAP_R)
634 		memcpy(req_buf, buf, len);
635 
636 	spin_lock_irqsave(&port->vio.lock, flags);
637 
638 	dr = &port->vio.drings[VIO_DRIVER_TX_RING];
639 
640 	/* XXX If we want to use this code generically we have to
641 	 * XXX handle TX ring exhaustion etc.
642 	 */
643 	desc = vio_dring_cur(dr);
644 
645 	err = ldc_map_single(port->vio.lp, req_buf, op_len,
646 			     desc->cookies, port->ring_cookies,
647 			     map_perm);
648 	if (err < 0) {
649 		spin_unlock_irqrestore(&port->vio.lock, flags);
650 		kfree(req_buf);
651 		return err;
652 	}
653 
654 	init_completion(&comp.com);
655 	comp.waiting_for = WAITING_FOR_GEN_CMD;
656 	port->vio.cmp = &comp;
657 
658 	desc->hdr.ack = VIO_ACK_ENABLE;
659 	desc->req_id = port->req_id;
660 	desc->operation = op;
661 	desc->slice = 0;
662 	desc->status = ~0;
663 	desc->offset = 0;
664 	desc->size = op_len;
665 	desc->ncookies = err;
666 
667 	/* This has to be a non-SMP write barrier because we are writing
668 	 * to memory which is shared with the peer LDOM.
669 	 */
670 	wmb();
671 	desc->hdr.state = VIO_DESC_READY;
672 
673 	err = __vdc_tx_trigger(port);
674 	if (err >= 0) {
675 		port->req_id++;
676 		dr->prod = vio_dring_next(dr, dr->prod);
677 		spin_unlock_irqrestore(&port->vio.lock, flags);
678 
679 		wait_for_completion(&comp.com);
680 		err = comp.err;
681 	} else {
682 		port->vio.cmp = NULL;
683 		spin_unlock_irqrestore(&port->vio.lock, flags);
684 	}
685 
686 	if (map_perm & LDC_MAP_W)
687 		memcpy(buf, req_buf, len);
688 
689 	kfree(req_buf);
690 
691 	return err;
692 }
693 
694 static int vdc_alloc_tx_ring(struct vdc_port *port)
695 {
696 	struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
697 	unsigned long len, entry_size;
698 	int ncookies;
699 	void *dring;
700 
701 	entry_size = sizeof(struct vio_disk_desc) +
702 		(sizeof(struct ldc_trans_cookie) * port->ring_cookies);
703 	len = (VDC_TX_RING_SIZE * entry_size);
704 
705 	ncookies = VIO_MAX_RING_COOKIES;
706 	dring = ldc_alloc_exp_dring(port->vio.lp, len,
707 				    dr->cookies, &ncookies,
708 				    (LDC_MAP_SHADOW |
709 				     LDC_MAP_DIRECT |
710 				     LDC_MAP_RW));
711 	if (IS_ERR(dring))
712 		return PTR_ERR(dring);
713 
714 	dr->base = dring;
715 	dr->entry_size = entry_size;
716 	dr->num_entries = VDC_TX_RING_SIZE;
717 	dr->prod = dr->cons = 0;
718 	dr->pending = VDC_TX_RING_SIZE;
719 	dr->ncookies = ncookies;
720 
721 	return 0;
722 }
723 
724 static void vdc_free_tx_ring(struct vdc_port *port)
725 {
726 	struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
727 
728 	if (dr->base) {
729 		ldc_free_exp_dring(port->vio.lp, dr->base,
730 				   (dr->entry_size * dr->num_entries),
731 				   dr->cookies, dr->ncookies);
732 		dr->base = NULL;
733 		dr->entry_size = 0;
734 		dr->num_entries = 0;
735 		dr->pending = 0;
736 		dr->ncookies = 0;
737 	}
738 }
739 
740 static int vdc_port_up(struct vdc_port *port)
741 {
742 	struct vio_completion comp;
743 
744 	init_completion(&comp.com);
745 	comp.err = 0;
746 	comp.waiting_for = WAITING_FOR_LINK_UP;
747 	port->vio.cmp = &comp;
748 
749 	vio_port_up(&port->vio);
750 	wait_for_completion(&comp.com);
751 	return comp.err;
752 }
753 
754 static void vdc_port_down(struct vdc_port *port)
755 {
756 	ldc_disconnect(port->vio.lp);
757 	ldc_unbind(port->vio.lp);
758 	vdc_free_tx_ring(port);
759 	vio_ldc_free(&port->vio);
760 }
761 
762 static int probe_disk(struct vdc_port *port)
763 {
764 	struct request_queue *q;
765 	struct gendisk *g;
766 	int err;
767 
768 	err = vdc_port_up(port);
769 	if (err)
770 		return err;
771 
772 	/* Using version 1.2 means vdisk_phys_blksz should be set unless the
773 	 * disk is reserved by another system.
774 	 */
775 	if (vdc_version_supported(port, 1, 2) && !port->vdisk_phys_blksz)
776 		return -ENODEV;
777 
778 	if (vdc_version_supported(port, 1, 1)) {
779 		/* vdisk_size should be set during the handshake, if it wasn't
780 		 * then the underlying disk is reserved by another system
781 		 */
782 		if (port->vdisk_size == -1)
783 			return -ENODEV;
784 	} else {
785 		struct vio_disk_geom geom;
786 
787 		err = generic_request(port, VD_OP_GET_DISKGEOM,
788 				      &geom, sizeof(geom));
789 		if (err < 0) {
790 			printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns "
791 			       "error %d\n", err);
792 			return err;
793 		}
794 		port->vdisk_size = ((u64)geom.num_cyl *
795 				    (u64)geom.num_hd *
796 				    (u64)geom.num_sec);
797 	}
798 
799 	q = blk_init_queue(do_vdc_request, &port->vio.lock);
800 	if (!q) {
801 		printk(KERN_ERR PFX "%s: Could not allocate queue.\n",
802 		       port->vio.name);
803 		return -ENOMEM;
804 	}
805 	g = alloc_disk(1 << PARTITION_SHIFT);
806 	if (!g) {
807 		printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n",
808 		       port->vio.name);
809 		blk_cleanup_queue(q);
810 		return -ENOMEM;
811 	}
812 
813 	port->disk = g;
814 
815 	/* Each segment in a request is up to an aligned page in size. */
816 	blk_queue_segment_boundary(q, PAGE_SIZE - 1);
817 	blk_queue_max_segment_size(q, PAGE_SIZE);
818 
819 	blk_queue_max_segments(q, port->ring_cookies);
820 	blk_queue_max_hw_sectors(q, port->max_xfer_size);
821 	g->major = vdc_major;
822 	g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT;
823 	strcpy(g->disk_name, port->disk_name);
824 
825 	g->fops = &vdc_fops;
826 	g->queue = q;
827 	g->private_data = port;
828 
829 	set_capacity(g, port->vdisk_size);
830 
831 	if (vdc_version_supported(port, 1, 1)) {
832 		switch (port->vdisk_mtype) {
833 		case VD_MEDIA_TYPE_CD:
834 			pr_info(PFX "Virtual CDROM %s\n", port->disk_name);
835 			g->flags |= GENHD_FL_CD;
836 			g->flags |= GENHD_FL_REMOVABLE;
837 			set_disk_ro(g, 1);
838 			break;
839 
840 		case VD_MEDIA_TYPE_DVD:
841 			pr_info(PFX "Virtual DVD %s\n", port->disk_name);
842 			g->flags |= GENHD_FL_CD;
843 			g->flags |= GENHD_FL_REMOVABLE;
844 			set_disk_ro(g, 1);
845 			break;
846 
847 		case VD_MEDIA_TYPE_FIXED:
848 			pr_info(PFX "Virtual Hard disk %s\n", port->disk_name);
849 			break;
850 		}
851 	}
852 
853 	blk_queue_physical_block_size(q, port->vdisk_phys_blksz);
854 
855 	pr_info(PFX "%s: %u sectors (%u MB) protocol %d.%d\n",
856 	       g->disk_name,
857 	       port->vdisk_size, (port->vdisk_size >> (20 - 9)),
858 	       port->vio.ver.major, port->vio.ver.minor);
859 
860 	device_add_disk(&port->vio.vdev->dev, g, NULL);
861 
862 	return 0;
863 }
864 
865 static struct ldc_channel_config vdc_ldc_cfg = {
866 	.event		= vdc_event,
867 	.mtu		= 64,
868 	.mode		= LDC_MODE_UNRELIABLE,
869 };
870 
871 static struct vio_driver_ops vdc_vio_ops = {
872 	.send_attr		= vdc_send_attr,
873 	.handle_attr		= vdc_handle_attr,
874 	.handshake_complete	= vdc_handshake_complete,
875 };
876 
877 static void print_version(void)
878 {
879 	static int version_printed;
880 
881 	if (version_printed++ == 0)
882 		printk(KERN_INFO "%s", version);
883 }
884 
885 struct vdc_check_port_data {
886 	int	dev_no;
887 	char	*type;
888 };
889 
890 static int vdc_device_probed(struct device *dev, void *arg)
891 {
892 	struct vio_dev *vdev = to_vio_dev(dev);
893 	struct vdc_check_port_data *port_data;
894 
895 	port_data = (struct vdc_check_port_data *)arg;
896 
897 	if ((vdev->dev_no == port_data->dev_no) &&
898 	    (!(strcmp((char *)&vdev->type, port_data->type))) &&
899 		dev_get_drvdata(dev)) {
900 		/* This device has already been configured
901 		 * by vdc_port_probe()
902 		 */
903 		return 1;
904 	} else {
905 		return 0;
906 	}
907 }
908 
909 /* Determine whether the VIO device is part of an mpgroup
910  * by locating all the virtual-device-port nodes associated
911  * with the parent virtual-device node for the VIO device
912  * and checking whether any of these nodes are vdc-ports
913  * which have already been configured.
914  *
915  * Returns true if this device is part of an mpgroup and has
916  * already been probed.
917  */
918 static bool vdc_port_mpgroup_check(struct vio_dev *vdev)
919 {
920 	struct vdc_check_port_data port_data;
921 	struct device *dev;
922 
923 	port_data.dev_no = vdev->dev_no;
924 	port_data.type = (char *)&vdev->type;
925 
926 	dev = device_find_child(vdev->dev.parent, &port_data,
927 				vdc_device_probed);
928 
929 	if (dev)
930 		return true;
931 
932 	return false;
933 }
934 
935 static int vdc_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
936 {
937 	struct mdesc_handle *hp;
938 	struct vdc_port *port;
939 	int err;
940 	const u64 *ldc_timeout;
941 
942 	print_version();
943 
944 	hp = mdesc_grab();
945 
946 	err = -ENODEV;
947 	if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) {
948 		printk(KERN_ERR PFX "Port id [%llu] too large.\n",
949 		       vdev->dev_no);
950 		goto err_out_release_mdesc;
951 	}
952 
953 	/* Check if this device is part of an mpgroup */
954 	if (vdc_port_mpgroup_check(vdev)) {
955 		printk(KERN_WARNING
956 			"VIO: Ignoring extra vdisk port %s",
957 			dev_name(&vdev->dev));
958 		goto err_out_release_mdesc;
959 	}
960 
961 	port = kzalloc(sizeof(*port), GFP_KERNEL);
962 	err = -ENOMEM;
963 	if (!port) {
964 		printk(KERN_ERR PFX "Cannot allocate vdc_port.\n");
965 		goto err_out_release_mdesc;
966 	}
967 
968 	if (vdev->dev_no >= 26)
969 		snprintf(port->disk_name, sizeof(port->disk_name),
970 			 VDCBLK_NAME "%c%c",
971 			 'a' + ((int)vdev->dev_no / 26) - 1,
972 			 'a' + ((int)vdev->dev_no % 26));
973 	else
974 		snprintf(port->disk_name, sizeof(port->disk_name),
975 			 VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26));
976 	port->vdisk_size = -1;
977 
978 	/* Actual wall time may be double due to do_generic_file_read() doing
979 	 * a readahead I/O first, and once that fails it will try to read a
980 	 * single page.
981 	 */
982 	ldc_timeout = mdesc_get_property(hp, vdev->mp, "vdc-timeout", NULL);
983 	port->ldc_timeout = ldc_timeout ? *ldc_timeout : 0;
984 	timer_setup(&port->ldc_reset_timer, vdc_ldc_reset_timer, 0);
985 	INIT_WORK(&port->ldc_reset_work, vdc_ldc_reset_work);
986 
987 	err = vio_driver_init(&port->vio, vdev, VDEV_DISK,
988 			      vdc_versions, ARRAY_SIZE(vdc_versions),
989 			      &vdc_vio_ops, port->disk_name);
990 	if (err)
991 		goto err_out_free_port;
992 
993 	port->vdisk_block_size = VDC_DEFAULT_BLK_SIZE;
994 	port->max_xfer_size = MAX_XFER_SIZE;
995 	port->ring_cookies = MAX_RING_COOKIES;
996 
997 	err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
998 	if (err)
999 		goto err_out_free_port;
1000 
1001 	err = vdc_alloc_tx_ring(port);
1002 	if (err)
1003 		goto err_out_free_ldc;
1004 
1005 	err = probe_disk(port);
1006 	if (err)
1007 		goto err_out_free_tx_ring;
1008 
1009 	/* Note that the device driver_data is used to determine
1010 	 * whether the port has been probed.
1011 	 */
1012 	dev_set_drvdata(&vdev->dev, port);
1013 
1014 	mdesc_release(hp);
1015 
1016 	return 0;
1017 
1018 err_out_free_tx_ring:
1019 	vdc_free_tx_ring(port);
1020 
1021 err_out_free_ldc:
1022 	vio_ldc_free(&port->vio);
1023 
1024 err_out_free_port:
1025 	kfree(port);
1026 
1027 err_out_release_mdesc:
1028 	mdesc_release(hp);
1029 	return err;
1030 }
1031 
1032 static int vdc_port_remove(struct vio_dev *vdev)
1033 {
1034 	struct vdc_port *port = dev_get_drvdata(&vdev->dev);
1035 
1036 	if (port) {
1037 		unsigned long flags;
1038 
1039 		spin_lock_irqsave(&port->vio.lock, flags);
1040 		blk_stop_queue(port->disk->queue);
1041 		spin_unlock_irqrestore(&port->vio.lock, flags);
1042 
1043 		flush_work(&port->ldc_reset_work);
1044 		del_timer_sync(&port->ldc_reset_timer);
1045 		del_timer_sync(&port->vio.timer);
1046 
1047 		del_gendisk(port->disk);
1048 		blk_cleanup_queue(port->disk->queue);
1049 		put_disk(port->disk);
1050 		port->disk = NULL;
1051 
1052 		vdc_free_tx_ring(port);
1053 		vio_ldc_free(&port->vio);
1054 
1055 		dev_set_drvdata(&vdev->dev, NULL);
1056 
1057 		kfree(port);
1058 	}
1059 	return 0;
1060 }
1061 
1062 static void vdc_requeue_inflight(struct vdc_port *port)
1063 {
1064 	struct vio_dring_state *dr = &port->vio.drings[VIO_DRIVER_TX_RING];
1065 	u32 idx;
1066 
1067 	for (idx = dr->cons; idx != dr->prod; idx = vio_dring_next(dr, idx)) {
1068 		struct vio_disk_desc *desc = vio_dring_entry(dr, idx);
1069 		struct vdc_req_entry *rqe = &port->rq_arr[idx];
1070 		struct request *req;
1071 
1072 		ldc_unmap(port->vio.lp, desc->cookies, desc->ncookies);
1073 		desc->hdr.state = VIO_DESC_FREE;
1074 		dr->cons = vio_dring_next(dr, idx);
1075 
1076 		req = rqe->req;
1077 		if (req == NULL) {
1078 			vdc_end_special(port, desc);
1079 			continue;
1080 		}
1081 
1082 		rqe->req = NULL;
1083 		blk_requeue_request(port->disk->queue, req);
1084 	}
1085 }
1086 
1087 static void vdc_queue_drain(struct vdc_port *port)
1088 {
1089 	struct request *req;
1090 
1091 	while ((req = blk_fetch_request(port->disk->queue)) != NULL)
1092 		__blk_end_request_all(req, BLK_STS_IOERR);
1093 }
1094 
1095 static void vdc_ldc_reset_timer(struct timer_list *t)
1096 {
1097 	struct vdc_port *port = from_timer(port, t, ldc_reset_timer);
1098 	struct vio_driver_state *vio = &port->vio;
1099 	unsigned long flags;
1100 
1101 	spin_lock_irqsave(&vio->lock, flags);
1102 	if (!(port->vio.hs_state & VIO_HS_COMPLETE)) {
1103 		pr_warn(PFX "%s ldc down %llu seconds, draining queue\n",
1104 			port->disk_name, port->ldc_timeout);
1105 		vdc_queue_drain(port);
1106 		vdc_blk_queue_start(port);
1107 	}
1108 	spin_unlock_irqrestore(&vio->lock, flags);
1109 }
1110 
1111 static void vdc_ldc_reset_work(struct work_struct *work)
1112 {
1113 	struct vdc_port *port;
1114 	struct vio_driver_state *vio;
1115 	unsigned long flags;
1116 
1117 	port = container_of(work, struct vdc_port, ldc_reset_work);
1118 	vio = &port->vio;
1119 
1120 	spin_lock_irqsave(&vio->lock, flags);
1121 	vdc_ldc_reset(port);
1122 	spin_unlock_irqrestore(&vio->lock, flags);
1123 }
1124 
1125 static void vdc_ldc_reset(struct vdc_port *port)
1126 {
1127 	int err;
1128 
1129 	assert_spin_locked(&port->vio.lock);
1130 
1131 	pr_warn(PFX "%s ldc link reset\n", port->disk_name);
1132 	blk_stop_queue(port->disk->queue);
1133 	vdc_requeue_inflight(port);
1134 	vdc_port_down(port);
1135 
1136 	err = vio_ldc_alloc(&port->vio, &vdc_ldc_cfg, port);
1137 	if (err) {
1138 		pr_err(PFX "%s vio_ldc_alloc:%d\n", port->disk_name, err);
1139 		return;
1140 	}
1141 
1142 	err = vdc_alloc_tx_ring(port);
1143 	if (err) {
1144 		pr_err(PFX "%s vio_alloc_tx_ring:%d\n", port->disk_name, err);
1145 		goto err_free_ldc;
1146 	}
1147 
1148 	if (port->ldc_timeout)
1149 		mod_timer(&port->ldc_reset_timer,
1150 			  round_jiffies(jiffies + HZ * port->ldc_timeout));
1151 	mod_timer(&port->vio.timer, round_jiffies(jiffies + HZ));
1152 	return;
1153 
1154 err_free_ldc:
1155 	vio_ldc_free(&port->vio);
1156 }
1157 
1158 static const struct vio_device_id vdc_port_match[] = {
1159 	{
1160 		.type = "vdc-port",
1161 	},
1162 	{},
1163 };
1164 MODULE_DEVICE_TABLE(vio, vdc_port_match);
1165 
1166 static struct vio_driver vdc_port_driver = {
1167 	.id_table	= vdc_port_match,
1168 	.probe		= vdc_port_probe,
1169 	.remove		= vdc_port_remove,
1170 	.name		= "vdc_port",
1171 };
1172 
1173 static int __init vdc_init(void)
1174 {
1175 	int err;
1176 
1177 	sunvdc_wq = alloc_workqueue("sunvdc", 0, 0);
1178 	if (!sunvdc_wq)
1179 		return -ENOMEM;
1180 
1181 	err = register_blkdev(0, VDCBLK_NAME);
1182 	if (err < 0)
1183 		goto out_free_wq;
1184 
1185 	vdc_major = err;
1186 
1187 	err = vio_register_driver(&vdc_port_driver);
1188 	if (err)
1189 		goto out_unregister_blkdev;
1190 
1191 	return 0;
1192 
1193 out_unregister_blkdev:
1194 	unregister_blkdev(vdc_major, VDCBLK_NAME);
1195 	vdc_major = 0;
1196 
1197 out_free_wq:
1198 	destroy_workqueue(sunvdc_wq);
1199 	return err;
1200 }
1201 
1202 static void __exit vdc_exit(void)
1203 {
1204 	vio_unregister_driver(&vdc_port_driver);
1205 	unregister_blkdev(vdc_major, VDCBLK_NAME);
1206 	destroy_workqueue(sunvdc_wq);
1207 }
1208 
1209 module_init(vdc_init);
1210 module_exit(vdc_exit);
1211