blob: f68e9baffad764a72e31fdb652b9707dbaf8e46e [file] [log] [blame]
David S. Miller667ef3c2007-07-16 04:03:56 -07001/* sunvdc.c: Sun LDOM Virtual Disk Client.
2 *
David S. Miller3d452e52008-09-01 01:48:52 -07003 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
David S. Miller667ef3c2007-07-16 04:03:56 -07004 */
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>
Allen Pais9bce2182014-09-19 09:42:14 -040012#include <linux/cdrom.h>
David S. Miller667ef3c2007-07-16 04:03:56 -070013#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>
David Millerd91c5e82007-10-24 08:46:01 +020019#include <linux/scatterlist.h>
David S. Miller667ef3c2007-07-16 04:03:56 -070020
21#include <asm/vio.h>
22#include <asm/ldc.h>
23
24#define DRV_MODULE_NAME "sunvdc"
25#define PFX DRV_MODULE_NAME ": "
Dwight Engen76e74bb2014-12-11 12:26:17 -050026#define DRV_MODULE_VERSION "1.2"
27#define DRV_MODULE_RELDATE "November 24, 2014"
David S. Miller667ef3c2007-07-16 04:03:56 -070028
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -080029static char version[] =
David S. Miller667ef3c2007-07-16 04:03:56 -070030 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
31MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
32MODULE_DESCRIPTION("Sun LDOM virtual disk client driver");
33MODULE_LICENSE("GPL");
34MODULE_VERSION(DRV_MODULE_VERSION);
35
Dwight Engend0aedcd2014-09-19 09:43:02 -040036#define VDC_TX_RING_SIZE 512
Liam R. Howlettf41e5462017-02-01 14:09:26 -050037#define VDC_DEFAULT_BLK_SIZE 512
David S. Miller667ef3c2007-07-16 04:03:56 -070038
Kees Cookff5d1a42018-10-08 08:46:51 -070039#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
David S. Miller667ef3c2007-07-16 04:03:56 -070043#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
Dwight Engen76e74bb2014-12-11 12:26:17 -050048static struct workqueue_struct *sunvdc_wq;
49
David S. Miller667ef3c2007-07-16 04:03:56 -070050struct vdc_req_entry {
51 struct request *req;
52};
53
54struct vdc_port {
55 struct vio_driver_state vio;
56
David S. Miller667ef3c2007-07-16 04:03:56 -070057 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
Dwight Engen76e74bb2014-12-11 12:26:17 -050070 u64 ldc_timeout;
71 struct timer_list ldc_reset_timer;
72 struct work_struct ldc_reset_work;
73
David S. Miller667ef3c2007-07-16 04:03:56 -070074 /* 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;
Allen Pais9bce2182014-09-19 09:42:14 -040080 u8 vdisk_mtype;
Liam R. Howlettf41e5462017-02-01 14:09:26 -050081 u32 vdisk_phys_blksz;
David S. Miller667ef3c2007-07-16 04:03:56 -070082
83 char disk_name[32];
David S. Miller667ef3c2007-07-16 04:03:56 -070084};
85
Dwight Engen76e74bb2014-12-11 12:26:17 -050086static void vdc_ldc_reset(struct vdc_port *port);
87static void vdc_ldc_reset_work(struct work_struct *work);
Kees Cooke99e88a2017-10-16 14:43:17 -070088static void vdc_ldc_reset_timer(struct timer_list *t);
Dwight Engen76e74bb2014-12-11 12:26:17 -050089
David S. Miller667ef3c2007-07-16 04:03:56 -070090static inline struct vdc_port *to_vdc_port(struct vio_driver_state *vio)
91{
92 return container_of(vio, struct vdc_port, vio);
93}
94
David S. Miller667ef3c2007-07-16 04:03:56 -070095/* Ordered from largest major to lowest */
96static struct vio_version vdc_versions[] = {
Liam R. Howlettf41e5462017-02-01 14:09:26 -050097 { .major = 1, .minor = 2 },
Allen Pais9bce2182014-09-19 09:42:14 -040098 { .major = 1, .minor = 1 },
David S. Miller667ef3c2007-07-16 04:03:56 -070099 { .major = 1, .minor = 0 },
100};
101
Allen Pais9bce2182014-09-19 09:42:14 -0400102static 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
David S. Miller667ef3c2007-07-16 04:03:56 -0700108#define VDCBLK_NAME "vdisk"
109static int vdc_major;
110#define PARTITION_SHIFT 3
111
112static inline u32 vdc_tx_dring_avail(struct vio_dring_state *dr)
113{
114 return vio_dring_avail(dr, VDC_TX_RING_SIZE);
115}
116
117static int vdc_getgeo(struct block_device *bdev, struct hd_geometry *geo)
118{
119 struct gendisk *disk = bdev->bd_disk;
Allen Paisde5b73f2014-09-19 09:42:26 -0400120 sector_t nsect = get_capacity(disk);
121 sector_t cylinders = nsect;
David S. Miller667ef3c2007-07-16 04:03:56 -0700122
Allen Paisde5b73f2014-09-19 09:42:26 -0400123 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;
David S. Miller667ef3c2007-07-16 04:03:56 -0700129
130 return 0;
131}
132
Allen Pais9bce2182014-09-19 09:42:14 -0400133/* 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 */
137static 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
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700164static const struct block_device_operations vdc_fops = {
David S. Miller667ef3c2007-07-16 04:03:56 -0700165 .owner = THIS_MODULE,
166 .getgeo = vdc_getgeo,
Allen Pais9bce2182014-09-19 09:42:14 -0400167 .ioctl = vdc_ioctl,
David S. Miller667ef3c2007-07-16 04:03:56 -0700168};
169
Dwight Engen76e74bb2014-12-11 12:26:17 -0500170static 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
David S. Miller667ef3c2007-07-16 04:03:56 -0700185static 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
196static void vdc_handshake_complete(struct vio_driver_state *vio)
197{
Dwight Engen76e74bb2014-12-11 12:26:17 -0500198 struct vdc_port *port = to_vdc_port(vio);
199
200 del_timer(&port->ldc_reset_timer);
David S. Miller667ef3c2007-07-16 04:03:56 -0700201 vdc_finish(vio, 0, WAITING_FOR_LINK_UP);
Dwight Engen76e74bb2014-12-11 12:26:17 -0500202 vdc_blk_queue_start(port);
David S. Miller667ef3c2007-07-16 04:03:56 -0700203}
204
205static 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
218static 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
Sam Ravnborg3f4528d2009-01-06 13:20:38 -0800234 viodbg(HS, "SEND ATTR xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
David S. Miller667ef3c2007-07-16 04:03:56 -0700235 pkt.xfer_mode, pkt.vdisk_block_size, pkt.max_xfer_size);
236
237 return vio_ldc_send(&port->vio, &pkt, sizeof(pkt));
238}
239
240static 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
Sam Ravnborg3f4528d2009-01-06 13:20:38 -0800245 viodbg(HS, "GOT ATTR stype[0x%x] ops[%llx] disk_size[%llu] disk_type[%x] "
Allen Pais9bce2182014-09-19 09:42:14 -0400246 "mtype[0x%x] xfer_mode[0x%x] blksz[%u] max_xfer[%llu]\n",
David S. Miller667ef3c2007-07-16 04:03:56 -0700247 pkt->tag.stype, pkt->operations,
Allen Pais9bce2182014-09-19 09:42:14 -0400248 pkt->vdisk_size, pkt->vdisk_type, pkt->vdisk_mtype,
David S. Miller667ef3c2007-07-16 04:03:56 -0700249 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;
David S. Miller667ef3c2007-07-16 04:03:56 -0700273 port->vdisk_type = pkt->vdisk_type;
Allen Pais9bce2182014-09-19 09:42:14 -0400274 if (vdc_version_supported(port, 1, 1)) {
275 port->vdisk_size = pkt->vdisk_size;
276 port->vdisk_mtype = pkt->vdisk_mtype;
277 }
David S. Miller667ef3c2007-07-16 04:03:56 -0700278 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;
Liam R. Howlettf41e5462017-02-01 14:09:26 -0500281
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
David S. Miller667ef3c2007-07-16 04:03:56 -0700286 return 0;
287 } else {
288 printk(KERN_ERR PFX "%s: Attribute NACK\n", vio->name);
289
290 return -ECONNRESET;
291 }
292}
293
294static 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
David S. Miller667ef3c2007-07-16 04:03:56 -0700301static 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;
Dwight Engenfe47c3c2014-12-11 12:26:15 -0500313 dr->cons = vio_dring_next(dr, index);
David S. Miller667ef3c2007-07-16 04:03:56 -0700314
315 req = rqe->req;
316 if (req == NULL) {
317 vdc_end_special(port, desc);
318 return;
319 }
320
321 rqe->req = NULL;
322
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200323 __blk_end_request(req, (desc->status ? BLK_STS_IOERR : 0), desc->size);
David S. Miller667ef3c2007-07-16 04:03:56 -0700324
Dwight Engen76e74bb2014-12-11 12:26:17 -0500325 vdc_blk_queue_start(port);
David S. Miller667ef3c2007-07-16 04:03:56 -0700326}
327
328static 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
343static int vdc_nack(struct vdc_port *port, void *msgbuf)
344{
345 /* XXX Implement me XXX */
346 return 0;
347}
348
349static 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
Dwight Engen76e74bb2014-12-11 12:26:17 -0500358 if (unlikely(event == LDC_EVENT_RESET)) {
David S. Miller667ef3c2007-07-16 04:03:56 -0700359 vio_link_state_change(vio, event);
Dwight Engen76e74bb2014-12-11 12:26:17 -0500360 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;
David S. Miller667ef3c2007-07-16 04:03:56 -0700367 }
368
369 if (unlikely(event != LDC_EVENT_DATA_READY)) {
Dwight Engen76e74bb2014-12-11 12:26:17 -0500370 pr_warn(PFX "Unexpected LDC event %d\n", event);
371 goto out;
David S. Miller667ef3c2007-07-16 04:03:56 -0700372 }
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);
Dwight Engen76e74bb2014-12-11 12:26:17 -0500415out:
David S. Miller667ef3c2007-07-16 04:03:56 -0700416 spin_unlock_irqrestore(&vio->lock, flags);
417}
418
419static 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
Dwight Engen76e74bb2014-12-11 12:26:17 -0500448 if (err == -ENOTCONN)
449 vdc_ldc_reset(port);
David S. Miller667ef3c2007-07-16 04:03:56 -0700450 return err;
451}
452
453static 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];
Kees Cookff5d1a42018-10-08 08:46:51 -0700457 struct scatterlist sg[MAX_RING_COOKIES];
David S. Miller667ef3c2007-07-16 04:03:56 -0700458 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
Kees Cookff5d1a42018-10-08 08:46:51 -0700465 if (WARN_ON(port->ring_cookies > MAX_RING_COOKIES))
466 return -EINVAL;
467
David S. Miller667ef3c2007-07-16 04:03:56 -0700468 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
Jens Axboe45711f12007-10-22 21:19:53 +0200478 sg_init_table(sg, port->ring_cookies);
David S. Miller667ef3c2007-07-16 04:03:56 -0700479 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
David S. Miller667ef3c2007-07-16 04:03:56 -0700485 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) {
David S. Miller1bd4b282007-08-24 22:05:44 -0700502 desc->slice = 0xff;
David S. Miller667ef3c2007-07-16 04:03:56 -0700503 } else {
504 desc->slice = 0;
505 }
506 desc->status = ~0;
Tejun Heo83096eb2009-05-07 22:24:39 +0900507 desc->offset = (blk_rq_pos(req) << 9) / port->vdisk_block_size;
David S. Miller667ef3c2007-07-16 04:03:56 -0700508 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++;
Dwight Engenfe47c3c2014-12-11 12:26:15 -0500522 dr->prod = vio_dring_next(dr, dr->prod);
David S. Miller667ef3c2007-07-16 04:03:56 -0700523 }
David S. Miller667ef3c2007-07-16 04:03:56 -0700524
525 return err;
526}
527
Dwight Engend0aedcd2014-09-19 09:43:02 -0400528static void do_vdc_request(struct request_queue *rq)
David S. Miller667ef3c2007-07-16 04:03:56 -0700529{
Dwight Engend0aedcd2014-09-19 09:43:02 -0400530 struct request *req;
David S. Miller667ef3c2007-07-16 04:03:56 -0700531
Dwight Engend0aedcd2014-09-19 09:43:02 -0400532 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);
545wait:
546 /* Avoid pointless unplugs. */
547 blk_stop_queue(rq);
David S. Miller667ef3c2007-07-16 04:03:56 -0700548 break;
Dwight Engend0aedcd2014-09-19 09:43:02 -0400549 }
David S. Miller667ef3c2007-07-16 04:03:56 -0700550 }
551}
552
553static 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
David S. Millerf4d96052013-02-14 11:49:01 -0800563 if (!(((u64)1 << (u64)op) & port->operations))
David S. Miller667ef3c2007-07-16 04:03:56 -0700564 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++;
Dwight Engenfe47c3c2014-12-11 12:26:15 -0500676 dr->prod = vio_dring_next(dr, dr->prod);
David S. Miller667ef3c2007-07-16 04:03:56 -0700677 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
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800694static int vdc_alloc_tx_ring(struct vdc_port *port)
David S. Miller667ef3c2007-07-16 04:03:56 -0700695{
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
724static 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
Dwight Engen76e74bb2014-12-11 12:26:17 -0500740static int vdc_port_up(struct vdc_port *port)
David S. Miller667ef3c2007-07-16 04:03:56 -0700741{
742 struct vio_completion comp;
David S. Miller667ef3c2007-07-16 04:03:56 -0700743
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);
David S. Miller667ef3c2007-07-16 04:03:56 -0700750 wait_for_completion(&comp.com);
Dwight Engen76e74bb2014-12-11 12:26:17 -0500751 return comp.err;
752}
753
754static 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
762static 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;
David S. Miller667ef3c2007-07-16 04:03:56 -0700771
Liam R. Howlettf41e5462017-02-01 14:09:26 -0500772 /* 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
Allen Pais9bce2182014-09-19 09:42:14 -0400778 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 {
Allen Paisde5b73f2014-09-19 09:42:26 -0400785 struct vio_disk_geom geom;
786
Allen Pais9bce2182014-09-19 09:42:14 -0400787 err = generic_request(port, VD_OP_GET_DISKGEOM,
Allen Paisde5b73f2014-09-19 09:42:26 -0400788 &geom, sizeof(geom));
Allen Pais9bce2182014-09-19 09:42:14 -0400789 if (err < 0) {
790 printk(KERN_ERR PFX "VD_OP_GET_DISKGEOM returns "
791 "error %d\n", err);
792 return err;
793 }
Allen Paisde5b73f2014-09-19 09:42:26 -0400794 port->vdisk_size = ((u64)geom.num_cyl *
795 (u64)geom.num_hd *
796 (u64)geom.num_sec);
David S. Miller667ef3c2007-07-16 04:03:56 -0700797 }
798
David S. Miller667ef3c2007-07-16 04:03:56 -0700799 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
Dwight Engen5eed69f2014-09-19 09:42:53 -0400815 /* 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
Martin K. Petersen8a783622010-02-26 00:20:39 -0500819 blk_queue_max_segments(q, port->ring_cookies);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500820 blk_queue_max_hw_sectors(q, port->max_xfer_size);
David S. Miller667ef3c2007-07-16 04:03:56 -0700821 g->major = vdc_major;
David S. Miller91ba3c22007-07-18 15:15:45 -0700822 g->first_minor = port->vio.vdev->dev_no << PARTITION_SHIFT;
David S. Miller667ef3c2007-07-16 04:03:56 -0700823 strcpy(g->disk_name, port->disk_name);
824
825 g->fops = &vdc_fops;
826 g->queue = q;
827 g->private_data = port;
David S. Miller667ef3c2007-07-16 04:03:56 -0700828
829 set_capacity(g, port->vdisk_size);
830
Allen Pais9bce2182014-09-19 09:42:14 -0400831 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
Liam R. Howlettf41e5462017-02-01 14:09:26 -0500853 blk_queue_physical_block_size(q, port->vdisk_phys_blksz);
854
Allen Pais9bce2182014-09-19 09:42:14 -0400855 pr_info(PFX "%s: %u sectors (%u MB) protocol %d.%d\n",
David S. Miller667ef3c2007-07-16 04:03:56 -0700856 g->disk_name,
Allen Pais9bce2182014-09-19 09:42:14 -0400857 port->vdisk_size, (port->vdisk_size >> (20 - 9)),
858 port->vio.ver.major, port->vio.ver.minor);
David S. Miller667ef3c2007-07-16 04:03:56 -0700859
Dan Williams0d52c7562016-06-15 19:44:20 -0700860 device_add_disk(&port->vio.vdev->dev, g);
David S. Miller667ef3c2007-07-16 04:03:56 -0700861
862 return 0;
863}
864
865static struct ldc_channel_config vdc_ldc_cfg = {
866 .event = vdc_event,
867 .mtu = 64,
868 .mode = LDC_MODE_UNRELIABLE,
869};
870
871static 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
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800877static void print_version(void)
David S. Miller80dc35d2007-07-17 21:46:00 -0700878{
879 static int version_printed;
880
881 if (version_printed++ == 0)
882 printk(KERN_INFO "%s", version);
883}
884
Jim Quigley3ee70592017-07-21 09:20:15 -0400885struct vdc_check_port_data {
886 int dev_no;
887 char *type;
888};
889
890static 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 */
918static 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
Greg Kroah-Hartman8d85fce2012-12-21 15:13:49 -0800935static int vdc_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
David S. Miller667ef3c2007-07-16 04:03:56 -0700936{
David S. Miller43fdf272007-07-12 13:47:50 -0700937 struct mdesc_handle *hp;
David S. Miller667ef3c2007-07-16 04:03:56 -0700938 struct vdc_port *port;
David S. Miller667ef3c2007-07-16 04:03:56 -0700939 int err;
Dwight Engen76e74bb2014-12-11 12:26:17 -0500940 const u64 *ldc_timeout;
David S. Miller667ef3c2007-07-16 04:03:56 -0700941
David S. Miller80dc35d2007-07-17 21:46:00 -0700942 print_version();
David S. Miller667ef3c2007-07-16 04:03:56 -0700943
David S. Miller43fdf272007-07-12 13:47:50 -0700944 hp = mdesc_grab();
David S. Miller667ef3c2007-07-16 04:03:56 -0700945
David S. Miller43fdf272007-07-12 13:47:50 -0700946 err = -ENODEV;
David S. Miller91ba3c22007-07-18 15:15:45 -0700947 if ((vdev->dev_no << PARTITION_SHIFT) & ~(u64)MINORMASK) {
Sam Ravnborg3f4528d2009-01-06 13:20:38 -0800948 printk(KERN_ERR PFX "Port id [%llu] too large.\n",
David S. Miller91ba3c22007-07-18 15:15:45 -0700949 vdev->dev_no);
David S. Miller43fdf272007-07-12 13:47:50 -0700950 goto err_out_release_mdesc;
David S. Miller667ef3c2007-07-16 04:03:56 -0700951 }
952
Jim Quigley3ee70592017-07-21 09:20:15 -0400953 /* 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
David S. Miller667ef3c2007-07-16 04:03:56 -0700961 port = kzalloc(sizeof(*port), GFP_KERNEL);
David S. Miller43fdf272007-07-12 13:47:50 -0700962 err = -ENOMEM;
David S. Miller667ef3c2007-07-16 04:03:56 -0700963 if (!port) {
964 printk(KERN_ERR PFX "Cannot allocate vdc_port.\n");
David S. Miller43fdf272007-07-12 13:47:50 -0700965 goto err_out_release_mdesc;
David S. Miller667ef3c2007-07-16 04:03:56 -0700966 }
967
David S. Miller91ba3c22007-07-18 15:15:45 -0700968 if (vdev->dev_no >= 26)
David S. Miller667ef3c2007-07-16 04:03:56 -0700969 snprintf(port->disk_name, sizeof(port->disk_name),
970 VDCBLK_NAME "%c%c",
David S. Miller91ba3c22007-07-18 15:15:45 -0700971 'a' + ((int)vdev->dev_no / 26) - 1,
972 'a' + ((int)vdev->dev_no % 26));
David S. Miller667ef3c2007-07-16 04:03:56 -0700973 else
974 snprintf(port->disk_name, sizeof(port->disk_name),
David S. Miller91ba3c22007-07-18 15:15:45 -0700975 VDCBLK_NAME "%c", 'a' + ((int)vdev->dev_no % 26));
Allen Pais9bce2182014-09-19 09:42:14 -0400976 port->vdisk_size = -1;
David S. Miller667ef3c2007-07-16 04:03:56 -0700977
Dwight Engen76e74bb2014-12-11 12:26:17 -0500978 /* 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;
Kees Cooke99e88a2017-10-16 14:43:17 -0700984 timer_setup(&port->ldc_reset_timer, vdc_ldc_reset_timer, 0);
Dwight Engen76e74bb2014-12-11 12:26:17 -0500985 INIT_WORK(&port->ldc_reset_work, vdc_ldc_reset_work);
986
David S. Miller43fdf272007-07-12 13:47:50 -0700987 err = vio_driver_init(&port->vio, vdev, VDEV_DISK,
David S. Miller667ef3c2007-07-16 04:03:56 -0700988 vdc_versions, ARRAY_SIZE(vdc_versions),
989 &vdc_vio_ops, port->disk_name);
990 if (err)
991 goto err_out_free_port;
992
Liam R. Howlettf41e5462017-02-01 14:09:26 -0500993 port->vdisk_block_size = VDC_DEFAULT_BLK_SIZE;
Kees Cookff5d1a42018-10-08 08:46:51 -0700994 port->max_xfer_size = MAX_XFER_SIZE;
995 port->ring_cookies = MAX_RING_COOKIES;
David S. Miller667ef3c2007-07-16 04:03:56 -0700996
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
Jim Quigley3ee70592017-07-21 09:20:15 -04001009 /* Note that the device driver_data is used to determine
1010 * whether the port has been probed.
1011 */
David S. Miller667ef3c2007-07-16 04:03:56 -07001012 dev_set_drvdata(&vdev->dev, port);
1013
David S. Miller43fdf272007-07-12 13:47:50 -07001014 mdesc_release(hp);
1015
David S. Miller667ef3c2007-07-16 04:03:56 -07001016 return 0;
1017
1018err_out_free_tx_ring:
1019 vdc_free_tx_ring(port);
1020
1021err_out_free_ldc:
1022 vio_ldc_free(&port->vio);
1023
1024err_out_free_port:
1025 kfree(port);
1026
David S. Miller43fdf272007-07-12 13:47:50 -07001027err_out_release_mdesc:
1028 mdesc_release(hp);
David S. Miller667ef3c2007-07-16 04:03:56 -07001029 return err;
1030}
1031
1032static int vdc_port_remove(struct vio_dev *vdev)
1033{
1034 struct vdc_port *port = dev_get_drvdata(&vdev->dev);
1035
1036 if (port) {
Dwight Engen31f48882014-12-11 12:25:42 -05001037 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
Dwight Engen76e74bb2014-12-11 12:26:17 -05001043 flush_work(&port->ldc_reset_work);
1044 del_timer_sync(&port->ldc_reset_timer);
David S. Miller667ef3c2007-07-16 04:03:56 -07001045 del_timer_sync(&port->vio.timer);
1046
Dwight Engen31f48882014-12-11 12:25:42 -05001047 del_gendisk(port->disk);
1048 blk_cleanup_queue(port->disk->queue);
1049 put_disk(port->disk);
1050 port->disk = NULL;
1051
David S. Miller667ef3c2007-07-16 04:03:56 -07001052 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
Dwight Engen76e74bb2014-12-11 12:26:17 -05001062static 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
1087static void vdc_queue_drain(struct vdc_port *port)
1088{
1089 struct request *req;
1090
1091 while ((req = blk_fetch_request(port->disk->queue)) != NULL)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02001092 __blk_end_request_all(req, BLK_STS_IOERR);
Dwight Engen76e74bb2014-12-11 12:26:17 -05001093}
1094
Kees Cooke99e88a2017-10-16 14:43:17 -07001095static void vdc_ldc_reset_timer(struct timer_list *t)
Dwight Engen76e74bb2014-12-11 12:26:17 -05001096{
Kees Cooke99e88a2017-10-16 14:43:17 -07001097 struct vdc_port *port = from_timer(port, t, ldc_reset_timer);
Dwight Engen76e74bb2014-12-11 12:26:17 -05001098 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
1111static 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
1125static 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
1154err_free_ldc:
1155 vio_ldc_free(&port->vio);
1156}
1157
David S. Miller3d452e52008-09-01 01:48:52 -07001158static const struct vio_device_id vdc_port_match[] = {
David S. Miller667ef3c2007-07-16 04:03:56 -07001159 {
1160 .type = "vdc-port",
1161 },
1162 {},
1163};
Fabio Massimo Di Nittoda68e082007-07-18 14:35:23 -07001164MODULE_DEVICE_TABLE(vio, vdc_port_match);
David S. Miller667ef3c2007-07-16 04:03:56 -07001165
1166static struct vio_driver vdc_port_driver = {
1167 .id_table = vdc_port_match,
1168 .probe = vdc_port_probe,
1169 .remove = vdc_port_remove,
Benjamin Herrenschmidtcb52d892012-03-26 19:06:30 +00001170 .name = "vdc_port",
David S. Miller667ef3c2007-07-16 04:03:56 -07001171};
1172
David S. Miller667ef3c2007-07-16 04:03:56 -07001173static int __init vdc_init(void)
1174{
1175 int err;
1176
Dwight Engen76e74bb2014-12-11 12:26:17 -05001177 sunvdc_wq = alloc_workqueue("sunvdc", 0, 0);
1178 if (!sunvdc_wq)
1179 return -ENOMEM;
1180
David S. Miller667ef3c2007-07-16 04:03:56 -07001181 err = register_blkdev(0, VDCBLK_NAME);
1182 if (err < 0)
Dwight Engen76e74bb2014-12-11 12:26:17 -05001183 goto out_free_wq;
David S. Miller667ef3c2007-07-16 04:03:56 -07001184
1185 vdc_major = err;
David S. Miller667ef3c2007-07-16 04:03:56 -07001186
1187 err = vio_register_driver(&vdc_port_driver);
1188 if (err)
David S. Miller80dc35d2007-07-17 21:46:00 -07001189 goto out_unregister_blkdev;
David S. Miller667ef3c2007-07-16 04:03:56 -07001190
1191 return 0;
1192
David S. Miller667ef3c2007-07-16 04:03:56 -07001193out_unregister_blkdev:
1194 unregister_blkdev(vdc_major, VDCBLK_NAME);
1195 vdc_major = 0;
1196
Dwight Engen76e74bb2014-12-11 12:26:17 -05001197out_free_wq:
1198 destroy_workqueue(sunvdc_wq);
David S. Miller667ef3c2007-07-16 04:03:56 -07001199 return err;
1200}
1201
1202static void __exit vdc_exit(void)
1203{
1204 vio_unregister_driver(&vdc_port_driver);
David S. Miller667ef3c2007-07-16 04:03:56 -07001205 unregister_blkdev(vdc_major, VDCBLK_NAME);
Dwight Engen76e74bb2014-12-11 12:26:17 -05001206 destroy_workqueue(sunvdc_wq);
David S. Miller667ef3c2007-07-16 04:03:56 -07001207}
1208
1209module_init(vdc_init);
1210module_exit(vdc_exit);