blob: a25c9386fdf785b3061a1c4a7550507c1010e076 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/*
2 * Xen SCSI frontend driver
3 *
4 * Copyright (c) 2008, FUJITSU Limited
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation; or, when distributed
9 * separately from the Linux kernel or incorporated into other
10 * software packages, subject to the following license:
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this source file (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use, copy, modify,
15 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 * and to permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 * IN THE SOFTWARE.
29 */
30
31#include <linux/module.h>
32#include <linux/kernel.h>
33#include <linux/device.h>
34#include <linux/wait.h>
35#include <linux/interrupt.h>
36#include <linux/mutex.h>
37#include <linux/spinlock.h>
38#include <linux/sched.h>
39#include <linux/blkdev.h>
40#include <linux/pfn.h>
41#include <linux/slab.h>
42#include <linux/bitops.h>
43
44#include <scsi/scsi_cmnd.h>
45#include <scsi/scsi_device.h>
46#include <scsi/scsi.h>
47#include <scsi/scsi_host.h>
48
49#include <xen/xen.h>
50#include <xen/xenbus.h>
51#include <xen/grant_table.h>
52#include <xen/events.h>
53#include <xen/page.h>
54
55#include <xen/interface/grant_table.h>
56#include <xen/interface/io/vscsiif.h>
57#include <xen/interface/io/protocols.h>
58
59#include <asm/xen/hypervisor.h>
60
61
62#define GRANT_INVALID_REF 0
63
64#define VSCSIFRONT_OP_ADD_LUN 1
65#define VSCSIFRONT_OP_DEL_LUN 2
66#define VSCSIFRONT_OP_READD_LUN 3
67
68/* Tuning point. */
69#define VSCSIIF_DEFAULT_CMD_PER_LUN 10
70#define VSCSIIF_MAX_TARGET 64
71#define VSCSIIF_MAX_LUN 255
72
73#define VSCSIIF_RING_SIZE __CONST_RING_SIZE(vscsiif, PAGE_SIZE)
74#define VSCSIIF_MAX_REQS VSCSIIF_RING_SIZE
75
76#define vscsiif_grants_sg(_sg) (PFN_UP((_sg) * \
77 sizeof(struct scsiif_request_segment)))
78
79struct vscsifrnt_shadow {
80 /* command between backend and frontend */
81 unsigned char act;
82 uint8_t nr_segments;
83 uint16_t rqid;
84 uint16_t ref_rqid;
85
86 unsigned int nr_grants; /* number of grants in gref[] */
87 struct scsiif_request_segment *sg; /* scatter/gather elements */
88 struct scsiif_request_segment seg[VSCSIIF_SG_TABLESIZE];
89
90 /* Do reset or abort function. */
91 wait_queue_head_t wq_reset; /* reset work queue */
92 int wait_reset; /* reset work queue condition */
93 int32_t rslt_reset; /* reset response status: */
94 /* SUCCESS or FAILED or: */
95#define RSLT_RESET_WAITING 0
96#define RSLT_RESET_ERR -1
97
98 /* Requested struct scsi_cmnd is stored from kernel. */
99 struct scsi_cmnd *sc;
100 int gref[vscsiif_grants_sg(SG_ALL) + SG_ALL];
101};
102
103struct vscsifrnt_info {
104 struct xenbus_device *dev;
105
106 struct Scsi_Host *host;
107 int host_active;
108
109 unsigned int evtchn;
110 unsigned int irq;
111
112 grant_ref_t ring_ref;
113 struct vscsiif_front_ring ring;
114 struct vscsiif_response ring_rsp;
115
116 spinlock_t shadow_lock;
117 DECLARE_BITMAP(shadow_free_bitmap, VSCSIIF_MAX_REQS);
118 struct vscsifrnt_shadow *shadow[VSCSIIF_MAX_REQS];
119
120 /* Following items are protected by the host lock. */
121 wait_queue_head_t wq_sync;
122 wait_queue_head_t wq_pause;
123 unsigned int wait_ring_available:1;
124 unsigned int waiting_pause:1;
125 unsigned int pause:1;
126 unsigned callers;
127
128 char dev_state_path[64];
129 struct task_struct *curr;
130};
131
132static DEFINE_MUTEX(scsifront_mutex);
133
134static void scsifront_wake_up(struct vscsifrnt_info *info)
135{
136 info->wait_ring_available = 0;
137 wake_up(&info->wq_sync);
138}
139
140static int scsifront_get_rqid(struct vscsifrnt_info *info)
141{
142 unsigned long flags;
143 int free;
144
145 spin_lock_irqsave(&info->shadow_lock, flags);
146
147 free = find_first_bit(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
148 __clear_bit(free, info->shadow_free_bitmap);
149
150 spin_unlock_irqrestore(&info->shadow_lock, flags);
151
152 return free;
153}
154
155static int _scsifront_put_rqid(struct vscsifrnt_info *info, uint32_t id)
156{
157 int empty = bitmap_empty(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
158
159 __set_bit(id, info->shadow_free_bitmap);
160 info->shadow[id] = NULL;
161
162 return empty || info->wait_ring_available;
163}
164
165static void scsifront_put_rqid(struct vscsifrnt_info *info, uint32_t id)
166{
167 unsigned long flags;
168 int kick;
169
170 spin_lock_irqsave(&info->shadow_lock, flags);
171 kick = _scsifront_put_rqid(info, id);
172 spin_unlock_irqrestore(&info->shadow_lock, flags);
173
174 if (kick)
175 scsifront_wake_up(info);
176}
177
178static int scsifront_do_request(struct vscsifrnt_info *info,
179 struct vscsifrnt_shadow *shadow)
180{
181 struct vscsiif_front_ring *ring = &(info->ring);
182 struct vscsiif_request *ring_req;
183 struct scsi_cmnd *sc = shadow->sc;
184 uint32_t id;
185 int i, notify;
186
187 if (RING_FULL(&info->ring))
188 return -EBUSY;
189
190 id = scsifront_get_rqid(info); /* use id in response */
191 if (id >= VSCSIIF_MAX_REQS)
192 return -EBUSY;
193
194 info->shadow[id] = shadow;
195 shadow->rqid = id;
196
197 ring_req = RING_GET_REQUEST(&(info->ring), ring->req_prod_pvt);
198 ring->req_prod_pvt++;
199
200 ring_req->rqid = id;
201 ring_req->act = shadow->act;
202 ring_req->ref_rqid = shadow->ref_rqid;
203 ring_req->nr_segments = shadow->nr_segments;
204
205 ring_req->id = sc->device->id;
206 ring_req->lun = sc->device->lun;
207 ring_req->channel = sc->device->channel;
208 ring_req->cmd_len = sc->cmd_len;
209
210 BUG_ON(sc->cmd_len > VSCSIIF_MAX_COMMAND_SIZE);
211
212 memcpy(ring_req->cmnd, sc->cmnd, sc->cmd_len);
213
214 ring_req->sc_data_direction = (uint8_t)sc->sc_data_direction;
215 ring_req->timeout_per_command = sc->request->timeout / HZ;
216
217 for (i = 0; i < (shadow->nr_segments & ~VSCSIIF_SG_GRANT); i++)
218 ring_req->seg[i] = shadow->seg[i];
219
220 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(ring, notify);
221 if (notify)
222 notify_remote_via_irq(info->irq);
223
224 return 0;
225}
226
227static void scsifront_gnttab_done(struct vscsifrnt_info *info,
228 struct vscsifrnt_shadow *shadow)
229{
230 int i;
231
232 if (shadow->sc->sc_data_direction == DMA_NONE)
233 return;
234
235 for (i = 0; i < shadow->nr_grants; i++) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200236 if (unlikely(!gnttab_try_end_foreign_access(shadow->gref[i]))) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000237 shost_printk(KERN_ALERT, info->host, KBUILD_MODNAME
238 "grant still in use by backend\n");
239 BUG();
240 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000241 }
242
243 kfree(shadow->sg);
244}
245
246static void scsifront_cdb_cmd_done(struct vscsifrnt_info *info,
247 struct vscsiif_response *ring_rsp)
248{
249 struct vscsifrnt_shadow *shadow;
250 struct scsi_cmnd *sc;
251 uint32_t id;
252 uint8_t sense_len;
253
254 id = ring_rsp->rqid;
255 shadow = info->shadow[id];
256 sc = shadow->sc;
257
258 BUG_ON(sc == NULL);
259
260 scsifront_gnttab_done(info, shadow);
261 scsifront_put_rqid(info, id);
262
263 sc->result = ring_rsp->rslt;
264 scsi_set_resid(sc, ring_rsp->residual_len);
265
266 sense_len = min_t(uint8_t, VSCSIIF_SENSE_BUFFERSIZE,
267 ring_rsp->sense_len);
268
269 if (sense_len)
270 memcpy(sc->sense_buffer, ring_rsp->sense_buffer, sense_len);
271
272 sc->scsi_done(sc);
273}
274
275static void scsifront_sync_cmd_done(struct vscsifrnt_info *info,
276 struct vscsiif_response *ring_rsp)
277{
278 uint16_t id = ring_rsp->rqid;
279 unsigned long flags;
280 struct vscsifrnt_shadow *shadow = info->shadow[id];
281 int kick;
282
283 spin_lock_irqsave(&info->shadow_lock, flags);
284 shadow->wait_reset = 1;
285 switch (shadow->rslt_reset) {
286 case RSLT_RESET_WAITING:
287 shadow->rslt_reset = ring_rsp->rslt;
288 break;
289 case RSLT_RESET_ERR:
290 kick = _scsifront_put_rqid(info, id);
291 spin_unlock_irqrestore(&info->shadow_lock, flags);
292 kfree(shadow);
293 if (kick)
294 scsifront_wake_up(info);
295 return;
296 default:
297 shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
298 "bad reset state %d, possibly leaking %u\n",
299 shadow->rslt_reset, id);
300 break;
301 }
302 spin_unlock_irqrestore(&info->shadow_lock, flags);
303
304 wake_up(&shadow->wq_reset);
305}
306
307static void scsifront_do_response(struct vscsifrnt_info *info,
308 struct vscsiif_response *ring_rsp)
309{
310 if (WARN(ring_rsp->rqid >= VSCSIIF_MAX_REQS ||
311 test_bit(ring_rsp->rqid, info->shadow_free_bitmap),
312 "illegal rqid %u returned by backend!\n", ring_rsp->rqid))
313 return;
314
315 if (info->shadow[ring_rsp->rqid]->act == VSCSIIF_ACT_SCSI_CDB)
316 scsifront_cdb_cmd_done(info, ring_rsp);
317 else
318 scsifront_sync_cmd_done(info, ring_rsp);
319}
320
321static int scsifront_ring_drain(struct vscsifrnt_info *info)
322{
323 struct vscsiif_response *ring_rsp;
324 RING_IDX i, rp;
325 int more_to_do = 0;
326
327 rp = info->ring.sring->rsp_prod;
328 rmb(); /* ordering required respective to dom0 */
329 for (i = info->ring.rsp_cons; i != rp; i++) {
330 ring_rsp = RING_GET_RESPONSE(&info->ring, i);
331 scsifront_do_response(info, ring_rsp);
332 }
333
334 info->ring.rsp_cons = i;
335
336 if (i != info->ring.req_prod_pvt)
337 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
338 else
339 info->ring.sring->rsp_event = i + 1;
340
341 return more_to_do;
342}
343
344static int scsifront_cmd_done(struct vscsifrnt_info *info)
345{
346 int more_to_do;
347 unsigned long flags;
348
349 spin_lock_irqsave(info->host->host_lock, flags);
350
351 more_to_do = scsifront_ring_drain(info);
352
353 info->wait_ring_available = 0;
354
355 spin_unlock_irqrestore(info->host->host_lock, flags);
356
357 wake_up(&info->wq_sync);
358
359 return more_to_do;
360}
361
362static irqreturn_t scsifront_irq_fn(int irq, void *dev_id)
363{
364 struct vscsifrnt_info *info = dev_id;
365
366 while (scsifront_cmd_done(info))
367 /* Yield point for this unbounded loop. */
368 cond_resched();
369
370 return IRQ_HANDLED;
371}
372
373static void scsifront_finish_all(struct vscsifrnt_info *info)
374{
375 unsigned i;
376 struct vscsiif_response resp;
377
378 scsifront_ring_drain(info);
379
380 for (i = 0; i < VSCSIIF_MAX_REQS; i++) {
381 if (test_bit(i, info->shadow_free_bitmap))
382 continue;
383 resp.rqid = i;
384 resp.sense_len = 0;
385 resp.rslt = DID_RESET << 16;
386 resp.residual_len = 0;
387 scsifront_do_response(info, &resp);
388 }
389}
390
391static int map_data_for_request(struct vscsifrnt_info *info,
392 struct scsi_cmnd *sc,
393 struct vscsifrnt_shadow *shadow)
394{
395 grant_ref_t gref_head;
396 struct page *page;
397 int err, ref, ref_cnt = 0;
398 int grant_ro = (sc->sc_data_direction == DMA_TO_DEVICE);
399 unsigned int i, off, len, bytes;
400 unsigned int data_len = scsi_bufflen(sc);
401 unsigned int data_grants = 0, seg_grants = 0;
402 struct scatterlist *sg;
403 struct scsiif_request_segment *seg;
404
405 if (sc->sc_data_direction == DMA_NONE || !data_len)
406 return 0;
407
408 scsi_for_each_sg(sc, sg, scsi_sg_count(sc), i)
409 data_grants += PFN_UP(sg->offset + sg->length);
410
411 if (data_grants > VSCSIIF_SG_TABLESIZE) {
412 if (data_grants > info->host->sg_tablesize) {
413 shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
414 "Unable to map request_buffer for command!\n");
415 return -E2BIG;
416 }
417 seg_grants = vscsiif_grants_sg(data_grants);
418 shadow->sg = kcalloc(data_grants,
419 sizeof(struct scsiif_request_segment), GFP_ATOMIC);
420 if (!shadow->sg)
421 return -ENOMEM;
422 }
423 seg = shadow->sg ? : shadow->seg;
424
425 err = gnttab_alloc_grant_references(seg_grants + data_grants,
426 &gref_head);
427 if (err) {
428 kfree(shadow->sg);
429 shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
430 "gnttab_alloc_grant_references() error\n");
431 return -ENOMEM;
432 }
433
434 if (seg_grants) {
435 page = virt_to_page(seg);
436 off = offset_in_page(seg);
437 len = sizeof(struct scsiif_request_segment) * data_grants;
438 while (len > 0) {
439 bytes = min_t(unsigned int, len, PAGE_SIZE - off);
440
441 ref = gnttab_claim_grant_reference(&gref_head);
442 BUG_ON(ref == -ENOSPC);
443
444 gnttab_grant_foreign_access_ref(ref,
445 info->dev->otherend_id,
446 xen_page_to_gfn(page), 1);
447 shadow->gref[ref_cnt] = ref;
448 shadow->seg[ref_cnt].gref = ref;
449 shadow->seg[ref_cnt].offset = (uint16_t)off;
450 shadow->seg[ref_cnt].length = (uint16_t)bytes;
451
452 page++;
453 len -= bytes;
454 off = 0;
455 ref_cnt++;
456 }
457 BUG_ON(seg_grants < ref_cnt);
458 seg_grants = ref_cnt;
459 }
460
461 scsi_for_each_sg(sc, sg, scsi_sg_count(sc), i) {
462 page = sg_page(sg);
463 off = sg->offset;
464 len = sg->length;
465
466 while (len > 0 && data_len > 0) {
467 /*
468 * sg sends a scatterlist that is larger than
469 * the data_len it wants transferred for certain
470 * IO sizes.
471 */
472 bytes = min_t(unsigned int, len, PAGE_SIZE - off);
473 bytes = min(bytes, data_len);
474
475 ref = gnttab_claim_grant_reference(&gref_head);
476 BUG_ON(ref == -ENOSPC);
477
478 gnttab_grant_foreign_access_ref(ref,
479 info->dev->otherend_id,
480 xen_page_to_gfn(page),
481 grant_ro);
482
483 shadow->gref[ref_cnt] = ref;
484 seg->gref = ref;
485 seg->offset = (uint16_t)off;
486 seg->length = (uint16_t)bytes;
487
488 page++;
489 seg++;
490 len -= bytes;
491 data_len -= bytes;
492 off = 0;
493 ref_cnt++;
494 }
495 }
496
497 if (seg_grants)
498 shadow->nr_segments = VSCSIIF_SG_GRANT | seg_grants;
499 else
500 shadow->nr_segments = (uint8_t)ref_cnt;
501 shadow->nr_grants = ref_cnt;
502
503 return 0;
504}
505
506static int scsifront_enter(struct vscsifrnt_info *info)
507{
508 if (info->pause)
509 return 1;
510 info->callers++;
511 return 0;
512}
513
514static void scsifront_return(struct vscsifrnt_info *info)
515{
516 info->callers--;
517 if (info->callers)
518 return;
519
520 if (!info->waiting_pause)
521 return;
522
523 info->waiting_pause = 0;
524 wake_up(&info->wq_pause);
525}
526
527static int scsifront_queuecommand(struct Scsi_Host *shost,
528 struct scsi_cmnd *sc)
529{
530 struct vscsifrnt_info *info = shost_priv(shost);
531 struct vscsifrnt_shadow *shadow = scsi_cmd_priv(sc);
532 unsigned long flags;
533 int err;
534
535 sc->result = 0;
536
537 shadow->sc = sc;
538 shadow->act = VSCSIIF_ACT_SCSI_CDB;
539
540 spin_lock_irqsave(shost->host_lock, flags);
541 if (scsifront_enter(info)) {
542 spin_unlock_irqrestore(shost->host_lock, flags);
543 return SCSI_MLQUEUE_HOST_BUSY;
544 }
545
546 err = map_data_for_request(info, sc, shadow);
547 if (err < 0) {
548 pr_debug("%s: err %d\n", __func__, err);
549 scsifront_return(info);
550 spin_unlock_irqrestore(shost->host_lock, flags);
551 if (err == -ENOMEM)
552 return SCSI_MLQUEUE_HOST_BUSY;
553 sc->result = DID_ERROR << 16;
554 sc->scsi_done(sc);
555 return 0;
556 }
557
558 if (scsifront_do_request(info, shadow)) {
559 scsifront_gnttab_done(info, shadow);
560 goto busy;
561 }
562
563 scsifront_return(info);
564 spin_unlock_irqrestore(shost->host_lock, flags);
565
566 return 0;
567
568busy:
569 scsifront_return(info);
570 spin_unlock_irqrestore(shost->host_lock, flags);
571 pr_debug("%s: busy\n", __func__);
572 return SCSI_MLQUEUE_HOST_BUSY;
573}
574
575/*
576 * Any exception handling (reset or abort) must be forwarded to the backend.
577 * We have to wait until an answer is returned. This answer contains the
578 * result to be returned to the requestor.
579 */
580static int scsifront_action_handler(struct scsi_cmnd *sc, uint8_t act)
581{
582 struct Scsi_Host *host = sc->device->host;
583 struct vscsifrnt_info *info = shost_priv(host);
584 struct vscsifrnt_shadow *shadow, *s = scsi_cmd_priv(sc);
585 int err = 0;
586
587 shadow = kzalloc(sizeof(*shadow), GFP_NOIO);
588 if (!shadow)
589 return FAILED;
590
591 shadow->act = act;
592 shadow->rslt_reset = RSLT_RESET_WAITING;
593 shadow->sc = sc;
594 shadow->ref_rqid = s->rqid;
595 init_waitqueue_head(&shadow->wq_reset);
596
597 spin_lock_irq(host->host_lock);
598
599 for (;;) {
600 if (scsifront_enter(info))
601 goto fail;
602
603 if (!scsifront_do_request(info, shadow))
604 break;
605
606 scsifront_return(info);
607 if (err)
608 goto fail;
609 info->wait_ring_available = 1;
610 spin_unlock_irq(host->host_lock);
611 err = wait_event_interruptible(info->wq_sync,
612 !info->wait_ring_available);
613 spin_lock_irq(host->host_lock);
614 }
615
616 spin_unlock_irq(host->host_lock);
617 err = wait_event_interruptible(shadow->wq_reset, shadow->wait_reset);
618 spin_lock_irq(host->host_lock);
619
620 if (!err) {
621 err = shadow->rslt_reset;
622 scsifront_put_rqid(info, shadow->rqid);
623 kfree(shadow);
624 } else {
625 spin_lock(&info->shadow_lock);
626 shadow->rslt_reset = RSLT_RESET_ERR;
627 spin_unlock(&info->shadow_lock);
628 err = FAILED;
629 }
630
631 scsifront_return(info);
632 spin_unlock_irq(host->host_lock);
633 return err;
634
635fail:
636 spin_unlock_irq(host->host_lock);
637 kfree(shadow);
638 return FAILED;
639}
640
641static int scsifront_eh_abort_handler(struct scsi_cmnd *sc)
642{
643 pr_debug("%s\n", __func__);
644 return scsifront_action_handler(sc, VSCSIIF_ACT_SCSI_ABORT);
645}
646
647static int scsifront_dev_reset_handler(struct scsi_cmnd *sc)
648{
649 pr_debug("%s\n", __func__);
650 return scsifront_action_handler(sc, VSCSIIF_ACT_SCSI_RESET);
651}
652
653static int scsifront_sdev_configure(struct scsi_device *sdev)
654{
655 struct vscsifrnt_info *info = shost_priv(sdev->host);
656 int err;
657
658 if (info && current == info->curr) {
659 err = xenbus_printf(XBT_NIL, info->dev->nodename,
660 info->dev_state_path, "%d", XenbusStateConnected);
661 if (err) {
662 xenbus_dev_error(info->dev, err,
663 "%s: writing dev_state_path", __func__);
664 return err;
665 }
666 }
667
668 return 0;
669}
670
671static void scsifront_sdev_destroy(struct scsi_device *sdev)
672{
673 struct vscsifrnt_info *info = shost_priv(sdev->host);
674 int err;
675
676 if (info && current == info->curr) {
677 err = xenbus_printf(XBT_NIL, info->dev->nodename,
678 info->dev_state_path, "%d", XenbusStateClosed);
679 if (err)
680 xenbus_dev_error(info->dev, err,
681 "%s: writing dev_state_path", __func__);
682 }
683}
684
685static struct scsi_host_template scsifront_sht = {
686 .module = THIS_MODULE,
687 .name = "Xen SCSI frontend driver",
688 .queuecommand = scsifront_queuecommand,
689 .eh_abort_handler = scsifront_eh_abort_handler,
690 .eh_device_reset_handler = scsifront_dev_reset_handler,
691 .slave_configure = scsifront_sdev_configure,
692 .slave_destroy = scsifront_sdev_destroy,
693 .cmd_per_lun = VSCSIIF_DEFAULT_CMD_PER_LUN,
694 .can_queue = VSCSIIF_MAX_REQS,
695 .this_id = -1,
696 .cmd_size = sizeof(struct vscsifrnt_shadow),
697 .sg_tablesize = VSCSIIF_SG_TABLESIZE,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000698 .proc_name = "scsifront",
699};
700
701static int scsifront_alloc_ring(struct vscsifrnt_info *info)
702{
703 struct xenbus_device *dev = info->dev;
704 struct vscsiif_sring *sring;
705 grant_ref_t gref;
706 int err = -ENOMEM;
707
708 /***** Frontend to Backend ring start *****/
709 sring = (struct vscsiif_sring *)__get_free_page(GFP_KERNEL);
710 if (!sring) {
711 xenbus_dev_fatal(dev, err,
712 "fail to allocate shared ring (Front to Back)");
713 return err;
714 }
715 SHARED_RING_INIT(sring);
716 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
717
718 err = xenbus_grant_ring(dev, sring, 1, &gref);
719 if (err < 0) {
720 free_page((unsigned long)sring);
721 xenbus_dev_fatal(dev, err,
722 "fail to grant shared ring (Front to Back)");
723 return err;
724 }
725 info->ring_ref = gref;
726
727 err = xenbus_alloc_evtchn(dev, &info->evtchn);
728 if (err) {
729 xenbus_dev_fatal(dev, err, "xenbus_alloc_evtchn");
730 goto free_gnttab;
731 }
732
733 err = bind_evtchn_to_irq(info->evtchn);
734 if (err <= 0) {
735 xenbus_dev_fatal(dev, err, "bind_evtchn_to_irq");
736 goto free_gnttab;
737 }
738
739 info->irq = err;
740
741 err = request_threaded_irq(info->irq, NULL, scsifront_irq_fn,
742 IRQF_ONESHOT, "scsifront", info);
743 if (err) {
744 xenbus_dev_fatal(dev, err, "request_threaded_irq");
745 goto free_irq;
746 }
747
748 return 0;
749
750/* free resource */
751free_irq:
752 unbind_from_irqhandler(info->irq, info);
753free_gnttab:
754 gnttab_end_foreign_access(info->ring_ref, 0,
755 (unsigned long)info->ring.sring);
756
757 return err;
758}
759
760static void scsifront_free_ring(struct vscsifrnt_info *info)
761{
762 unbind_from_irqhandler(info->irq, info);
763 gnttab_end_foreign_access(info->ring_ref, 0,
764 (unsigned long)info->ring.sring);
765}
766
767static int scsifront_init_ring(struct vscsifrnt_info *info)
768{
769 struct xenbus_device *dev = info->dev;
770 struct xenbus_transaction xbt;
771 int err;
772
773 pr_debug("%s\n", __func__);
774
775 err = scsifront_alloc_ring(info);
776 if (err)
777 return err;
778 pr_debug("%s: %u %u\n", __func__, info->ring_ref, info->evtchn);
779
780again:
781 err = xenbus_transaction_start(&xbt);
782 if (err)
783 xenbus_dev_fatal(dev, err, "starting transaction");
784
785 err = xenbus_printf(xbt, dev->nodename, "ring-ref", "%u",
786 info->ring_ref);
787 if (err) {
788 xenbus_dev_fatal(dev, err, "%s", "writing ring-ref");
789 goto fail;
790 }
791
792 err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
793 info->evtchn);
794
795 if (err) {
796 xenbus_dev_fatal(dev, err, "%s", "writing event-channel");
797 goto fail;
798 }
799
800 err = xenbus_transaction_end(xbt, 0);
801 if (err) {
802 if (err == -EAGAIN)
803 goto again;
804 xenbus_dev_fatal(dev, err, "completing transaction");
805 goto free_sring;
806 }
807
808 return 0;
809
810fail:
811 xenbus_transaction_end(xbt, 1);
812free_sring:
813 scsifront_free_ring(info);
814
815 return err;
816}
817
818
819static int scsifront_probe(struct xenbus_device *dev,
820 const struct xenbus_device_id *id)
821{
822 struct vscsifrnt_info *info;
823 struct Scsi_Host *host;
824 int err = -ENOMEM;
825 char name[TASK_COMM_LEN];
826
827 host = scsi_host_alloc(&scsifront_sht, sizeof(*info));
828 if (!host) {
829 xenbus_dev_fatal(dev, err, "fail to allocate scsi host");
830 return err;
831 }
832 info = (struct vscsifrnt_info *)host->hostdata;
833
834 dev_set_drvdata(&dev->dev, info);
835 info->dev = dev;
836
837 bitmap_fill(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
838
839 err = scsifront_init_ring(info);
840 if (err) {
841 scsi_host_put(host);
842 return err;
843 }
844
845 init_waitqueue_head(&info->wq_sync);
846 init_waitqueue_head(&info->wq_pause);
847 spin_lock_init(&info->shadow_lock);
848
849 snprintf(name, TASK_COMM_LEN, "vscsiif.%d", host->host_no);
850
851 host->max_id = VSCSIIF_MAX_TARGET;
852 host->max_channel = 0;
853 host->max_lun = VSCSIIF_MAX_LUN;
854 host->max_sectors = (host->sg_tablesize - 1) * PAGE_SIZE / 512;
855 host->max_cmd_len = VSCSIIF_MAX_COMMAND_SIZE;
856
857 err = scsi_add_host(host, &dev->dev);
858 if (err) {
859 dev_err(&dev->dev, "fail to add scsi host %d\n", err);
860 goto free_sring;
861 }
862 info->host = host;
863 info->host_active = 1;
864
865 xenbus_switch_state(dev, XenbusStateInitialised);
866
867 return 0;
868
869free_sring:
870 scsifront_free_ring(info);
871 scsi_host_put(host);
872 return err;
873}
874
875static int scsifront_resume(struct xenbus_device *dev)
876{
877 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
878 struct Scsi_Host *host = info->host;
879 int err;
880
881 spin_lock_irq(host->host_lock);
882
883 /* Finish all still pending commands. */
884 scsifront_finish_all(info);
885
886 spin_unlock_irq(host->host_lock);
887
888 /* Reconnect to dom0. */
889 scsifront_free_ring(info);
890 err = scsifront_init_ring(info);
891 if (err) {
892 dev_err(&dev->dev, "fail to resume %d\n", err);
893 scsi_host_put(host);
894 return err;
895 }
896
897 xenbus_switch_state(dev, XenbusStateInitialised);
898
899 return 0;
900}
901
902static int scsifront_suspend(struct xenbus_device *dev)
903{
904 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
905 struct Scsi_Host *host = info->host;
906 int err = 0;
907
908 /* No new commands for the backend. */
909 spin_lock_irq(host->host_lock);
910 info->pause = 1;
911 while (info->callers && !err) {
912 info->waiting_pause = 1;
913 info->wait_ring_available = 0;
914 spin_unlock_irq(host->host_lock);
915 wake_up(&info->wq_sync);
916 err = wait_event_interruptible(info->wq_pause,
917 !info->waiting_pause);
918 spin_lock_irq(host->host_lock);
919 }
920 spin_unlock_irq(host->host_lock);
921 return err;
922}
923
924static int scsifront_remove(struct xenbus_device *dev)
925{
926 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
927
928 pr_debug("%s: %s removed\n", __func__, dev->nodename);
929
930 mutex_lock(&scsifront_mutex);
931 if (info->host_active) {
932 /* Scsi_host not yet removed */
933 scsi_remove_host(info->host);
934 info->host_active = 0;
935 }
936 mutex_unlock(&scsifront_mutex);
937
938 scsifront_free_ring(info);
939 scsi_host_put(info->host);
940
941 return 0;
942}
943
944static void scsifront_disconnect(struct vscsifrnt_info *info)
945{
946 struct xenbus_device *dev = info->dev;
947 struct Scsi_Host *host = info->host;
948
949 pr_debug("%s: %s disconnect\n", __func__, dev->nodename);
950
951 /*
952 * When this function is executed, all devices of
953 * Frontend have been deleted.
954 * Therefore, it need not block I/O before remove_host.
955 */
956
957 mutex_lock(&scsifront_mutex);
958 if (info->host_active) {
959 scsi_remove_host(host);
960 info->host_active = 0;
961 }
962 mutex_unlock(&scsifront_mutex);
963
964 xenbus_frontend_closed(dev);
965}
966
967static void scsifront_do_lun_hotplug(struct vscsifrnt_info *info, int op)
968{
969 struct xenbus_device *dev = info->dev;
970 int i, err = 0;
971 char str[64];
972 char **dir;
973 unsigned int dir_n = 0;
974 unsigned int device_state;
975 unsigned int hst, chn, tgt, lun;
976 struct scsi_device *sdev;
977
978 dir = xenbus_directory(XBT_NIL, dev->otherend, "vscsi-devs", &dir_n);
979 if (IS_ERR(dir))
980 return;
981
982 /* mark current task as the one allowed to modify device states */
983 BUG_ON(info->curr);
984 info->curr = current;
985
986 for (i = 0; i < dir_n; i++) {
987 /* read status */
988 snprintf(str, sizeof(str), "vscsi-devs/%s/state", dir[i]);
989 err = xenbus_scanf(XBT_NIL, dev->otherend, str, "%u",
990 &device_state);
991 if (XENBUS_EXIST_ERR(err))
992 continue;
993
994 /* virtual SCSI device */
995 snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", dir[i]);
996 err = xenbus_scanf(XBT_NIL, dev->otherend, str,
997 "%u:%u:%u:%u", &hst, &chn, &tgt, &lun);
998 if (XENBUS_EXIST_ERR(err))
999 continue;
1000
1001 /*
1002 * Front device state path, used in slave_configure called
1003 * on successfull scsi_add_device, and in slave_destroy called
1004 * on remove of a device.
1005 */
1006 snprintf(info->dev_state_path, sizeof(info->dev_state_path),
1007 "vscsi-devs/%s/state", dir[i]);
1008
1009 switch (op) {
1010 case VSCSIFRONT_OP_ADD_LUN:
1011 if (device_state != XenbusStateInitialised)
1012 break;
1013
1014 if (scsi_add_device(info->host, chn, tgt, lun)) {
1015 dev_err(&dev->dev, "scsi_add_device\n");
1016 err = xenbus_printf(XBT_NIL, dev->nodename,
1017 info->dev_state_path,
1018 "%d", XenbusStateClosed);
1019 if (err)
1020 xenbus_dev_error(dev, err,
1021 "%s: writing dev_state_path", __func__);
1022 }
1023 break;
1024 case VSCSIFRONT_OP_DEL_LUN:
1025 if (device_state != XenbusStateClosing)
1026 break;
1027
1028 sdev = scsi_device_lookup(info->host, chn, tgt, lun);
1029 if (sdev) {
1030 scsi_remove_device(sdev);
1031 scsi_device_put(sdev);
1032 }
1033 break;
1034 case VSCSIFRONT_OP_READD_LUN:
1035 if (device_state == XenbusStateConnected) {
1036 err = xenbus_printf(XBT_NIL, dev->nodename,
1037 info->dev_state_path,
1038 "%d", XenbusStateConnected);
1039 if (err)
1040 xenbus_dev_error(dev, err,
1041 "%s: writing dev_state_path", __func__);
1042 }
1043 break;
1044 default:
1045 break;
1046 }
1047 }
1048
1049 info->curr = NULL;
1050
1051 kfree(dir);
1052}
1053
1054static void scsifront_read_backend_params(struct xenbus_device *dev,
1055 struct vscsifrnt_info *info)
1056{
1057 unsigned int sg_grant, nr_segs;
1058 struct Scsi_Host *host = info->host;
1059
1060 sg_grant = xenbus_read_unsigned(dev->otherend, "feature-sg-grant", 0);
1061 nr_segs = min_t(unsigned int, sg_grant, SG_ALL);
1062 nr_segs = max_t(unsigned int, nr_segs, VSCSIIF_SG_TABLESIZE);
1063 nr_segs = min_t(unsigned int, nr_segs,
1064 VSCSIIF_SG_TABLESIZE * PAGE_SIZE /
1065 sizeof(struct scsiif_request_segment));
1066
1067 if (!info->pause && sg_grant)
1068 dev_info(&dev->dev, "using up to %d SG entries\n", nr_segs);
1069 else if (info->pause && nr_segs < host->sg_tablesize)
1070 dev_warn(&dev->dev,
1071 "SG entries decreased from %d to %u - device may not work properly anymore\n",
1072 host->sg_tablesize, nr_segs);
1073
1074 host->sg_tablesize = nr_segs;
1075 host->max_sectors = (nr_segs - 1) * PAGE_SIZE / 512;
1076}
1077
1078static void scsifront_backend_changed(struct xenbus_device *dev,
1079 enum xenbus_state backend_state)
1080{
1081 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
1082
1083 pr_debug("%s: %p %u %u\n", __func__, dev, dev->state, backend_state);
1084
1085 switch (backend_state) {
1086 case XenbusStateUnknown:
1087 case XenbusStateInitialising:
1088 case XenbusStateInitWait:
1089 case XenbusStateInitialised:
1090 break;
1091
1092 case XenbusStateConnected:
1093 scsifront_read_backend_params(dev, info);
1094
1095 if (info->pause) {
1096 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_READD_LUN);
1097 xenbus_switch_state(dev, XenbusStateConnected);
1098 info->pause = 0;
1099 return;
1100 }
1101
1102 if (xenbus_read_driver_state(dev->nodename) ==
1103 XenbusStateInitialised)
1104 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN);
1105
1106 if (dev->state != XenbusStateConnected)
1107 xenbus_switch_state(dev, XenbusStateConnected);
1108 break;
1109
1110 case XenbusStateClosed:
1111 if (dev->state == XenbusStateClosed)
1112 break;
Olivier Deprez157378f2022-04-04 15:47:50 +02001113 fallthrough; /* Missed the backend's Closing state */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001114 case XenbusStateClosing:
1115 scsifront_disconnect(info);
1116 break;
1117
1118 case XenbusStateReconfiguring:
1119 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_DEL_LUN);
1120 xenbus_switch_state(dev, XenbusStateReconfiguring);
1121 break;
1122
1123 case XenbusStateReconfigured:
1124 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN);
1125 xenbus_switch_state(dev, XenbusStateConnected);
1126 break;
1127 }
1128}
1129
1130static const struct xenbus_device_id scsifront_ids[] = {
1131 { "vscsi" },
1132 { "" }
1133};
1134
1135static struct xenbus_driver scsifront_driver = {
1136 .ids = scsifront_ids,
1137 .probe = scsifront_probe,
1138 .remove = scsifront_remove,
1139 .resume = scsifront_resume,
1140 .suspend = scsifront_suspend,
1141 .otherend_changed = scsifront_backend_changed,
1142};
1143
1144static int __init scsifront_init(void)
1145{
1146 if (!xen_domain())
1147 return -ENODEV;
1148
1149 return xenbus_register_frontend(&scsifront_driver);
1150}
1151module_init(scsifront_init);
1152
1153static void __exit scsifront_exit(void)
1154{
1155 xenbus_unregister_driver(&scsifront_driver);
1156}
1157module_exit(scsifront_exit);
1158
1159MODULE_DESCRIPTION("Xen SCSI frontend driver");
1160MODULE_LICENSE("GPL");
1161MODULE_ALIAS("xen:vscsi");
1162MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");