blob: e1b32ed0aa205fbbc076eac91bbb4e6c15688d8e [file] [log] [blame]
Juergen Gross8beb8d42014-08-28 06:44:11 +02001/*
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
Juergen Gross85617dc2015-02-17 08:02:49 +010066#define VSCSIFRONT_OP_READD_LUN 3
Juergen Gross8beb8d42014-08-28 06:44:11 +020067
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 uint16_t rqid;
83
84 unsigned int nr_grants; /* number of grants in gref[] */
85 struct scsiif_request_segment *sg; /* scatter/gather elements */
86
87 /* Do reset or abort function. */
88 wait_queue_head_t wq_reset; /* reset work queue */
89 int wait_reset; /* reset work queue condition */
90 int32_t rslt_reset; /* reset response status: */
91 /* SUCCESS or FAILED or: */
92#define RSLT_RESET_WAITING 0
93#define RSLT_RESET_ERR -1
94
95 /* Requested struct scsi_cmnd is stored from kernel. */
96 struct scsi_cmnd *sc;
97 int gref[vscsiif_grants_sg(SG_ALL) + SG_ALL];
98};
99
100struct vscsifrnt_info {
101 struct xenbus_device *dev;
102
103 struct Scsi_Host *host;
104 int host_active;
105
106 unsigned int evtchn;
107 unsigned int irq;
108
109 grant_ref_t ring_ref;
110 struct vscsiif_front_ring ring;
111 struct vscsiif_response ring_rsp;
112
113 spinlock_t shadow_lock;
114 DECLARE_BITMAP(shadow_free_bitmap, VSCSIIF_MAX_REQS);
115 struct vscsifrnt_shadow *shadow[VSCSIIF_MAX_REQS];
116
Juergen Gross85617dc2015-02-17 08:02:49 +0100117 /* Following items are protected by the host lock. */
Juergen Gross8beb8d42014-08-28 06:44:11 +0200118 wait_queue_head_t wq_sync;
Juergen Gross85617dc2015-02-17 08:02:49 +0100119 wait_queue_head_t wq_pause;
Juergen Gross8beb8d42014-08-28 06:44:11 +0200120 unsigned int wait_ring_available:1;
Juergen Gross85617dc2015-02-17 08:02:49 +0100121 unsigned int waiting_pause:1;
122 unsigned int pause:1;
123 unsigned callers;
Juergen Gross8beb8d42014-08-28 06:44:11 +0200124
125 char dev_state_path[64];
126 struct task_struct *curr;
127};
128
129static DEFINE_MUTEX(scsifront_mutex);
130
131static void scsifront_wake_up(struct vscsifrnt_info *info)
132{
133 info->wait_ring_available = 0;
134 wake_up(&info->wq_sync);
135}
136
137static int scsifront_get_rqid(struct vscsifrnt_info *info)
138{
139 unsigned long flags;
140 int free;
141
142 spin_lock_irqsave(&info->shadow_lock, flags);
143
144 free = find_first_bit(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
145 __clear_bit(free, info->shadow_free_bitmap);
146
147 spin_unlock_irqrestore(&info->shadow_lock, flags);
148
149 return free;
150}
151
152static int _scsifront_put_rqid(struct vscsifrnt_info *info, uint32_t id)
153{
154 int empty = bitmap_empty(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
155
156 __set_bit(id, info->shadow_free_bitmap);
157 info->shadow[id] = NULL;
158
159 return empty || info->wait_ring_available;
160}
161
162static void scsifront_put_rqid(struct vscsifrnt_info *info, uint32_t id)
163{
164 unsigned long flags;
165 int kick;
166
167 spin_lock_irqsave(&info->shadow_lock, flags);
168 kick = _scsifront_put_rqid(info, id);
169 spin_unlock_irqrestore(&info->shadow_lock, flags);
170
171 if (kick)
172 scsifront_wake_up(info);
173}
174
175static struct vscsiif_request *scsifront_pre_req(struct vscsifrnt_info *info)
176{
177 struct vscsiif_front_ring *ring = &(info->ring);
178 struct vscsiif_request *ring_req;
179 uint32_t id;
180
181 id = scsifront_get_rqid(info); /* use id in response */
182 if (id >= VSCSIIF_MAX_REQS)
183 return NULL;
184
185 ring_req = RING_GET_REQUEST(&(info->ring), ring->req_prod_pvt);
186
187 ring->req_prod_pvt++;
188
189 ring_req->rqid = (uint16_t)id;
190
191 return ring_req;
192}
193
194static void scsifront_do_request(struct vscsifrnt_info *info)
195{
196 struct vscsiif_front_ring *ring = &(info->ring);
197 int notify;
198
199 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(ring, notify);
200 if (notify)
201 notify_remote_via_irq(info->irq);
202}
203
204static void scsifront_gnttab_done(struct vscsifrnt_info *info, uint32_t id)
205{
206 struct vscsifrnt_shadow *s = info->shadow[id];
207 int i;
208
209 if (s->sc->sc_data_direction == DMA_NONE)
210 return;
211
212 for (i = 0; i < s->nr_grants; i++) {
213 if (unlikely(gnttab_query_foreign_access(s->gref[i]) != 0)) {
214 shost_printk(KERN_ALERT, info->host, KBUILD_MODNAME
215 "grant still in use by backend\n");
216 BUG();
217 }
218 gnttab_end_foreign_access(s->gref[i], 0, 0UL);
219 }
220
221 kfree(s->sg);
222}
223
224static void scsifront_cdb_cmd_done(struct vscsifrnt_info *info,
225 struct vscsiif_response *ring_rsp)
226{
227 struct scsi_cmnd *sc;
228 uint32_t id;
229 uint8_t sense_len;
230
231 id = ring_rsp->rqid;
232 sc = info->shadow[id]->sc;
233
234 BUG_ON(sc == NULL);
235
236 scsifront_gnttab_done(info, id);
237 scsifront_put_rqid(info, id);
238
239 sc->result = ring_rsp->rslt;
240 scsi_set_resid(sc, ring_rsp->residual_len);
241
242 sense_len = min_t(uint8_t, VSCSIIF_SENSE_BUFFERSIZE,
243 ring_rsp->sense_len);
244
245 if (sense_len)
246 memcpy(sc->sense_buffer, ring_rsp->sense_buffer, sense_len);
247
248 sc->scsi_done(sc);
249}
250
251static void scsifront_sync_cmd_done(struct vscsifrnt_info *info,
252 struct vscsiif_response *ring_rsp)
253{
254 uint16_t id = ring_rsp->rqid;
255 unsigned long flags;
256 struct vscsifrnt_shadow *shadow = info->shadow[id];
257 int kick;
258
259 spin_lock_irqsave(&info->shadow_lock, flags);
260 shadow->wait_reset = 1;
261 switch (shadow->rslt_reset) {
262 case RSLT_RESET_WAITING:
263 shadow->rslt_reset = ring_rsp->rslt;
264 break;
265 case RSLT_RESET_ERR:
266 kick = _scsifront_put_rqid(info, id);
267 spin_unlock_irqrestore(&info->shadow_lock, flags);
268 kfree(shadow);
269 if (kick)
270 scsifront_wake_up(info);
271 return;
272 default:
273 shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
274 "bad reset state %d, possibly leaking %u\n",
275 shadow->rslt_reset, id);
276 break;
277 }
278 spin_unlock_irqrestore(&info->shadow_lock, flags);
279
280 wake_up(&shadow->wq_reset);
281}
282
Juergen Gross85617dc2015-02-17 08:02:49 +0100283static void scsifront_do_response(struct vscsifrnt_info *info,
284 struct vscsiif_response *ring_rsp)
285{
286 if (WARN(ring_rsp->rqid >= VSCSIIF_MAX_REQS ||
287 test_bit(ring_rsp->rqid, info->shadow_free_bitmap),
288 "illegal rqid %u returned by backend!\n", ring_rsp->rqid))
289 return;
290
291 if (info->shadow[ring_rsp->rqid]->act == VSCSIIF_ACT_SCSI_CDB)
292 scsifront_cdb_cmd_done(info, ring_rsp);
293 else
294 scsifront_sync_cmd_done(info, ring_rsp);
295}
296
297static int scsifront_ring_drain(struct vscsifrnt_info *info)
Juergen Gross8beb8d42014-08-28 06:44:11 +0200298{
299 struct vscsiif_response *ring_rsp;
300 RING_IDX i, rp;
301 int more_to_do = 0;
Juergen Gross8beb8d42014-08-28 06:44:11 +0200302
303 rp = info->ring.sring->rsp_prod;
304 rmb(); /* ordering required respective to dom0 */
305 for (i = info->ring.rsp_cons; i != rp; i++) {
Juergen Gross8beb8d42014-08-28 06:44:11 +0200306 ring_rsp = RING_GET_RESPONSE(&info->ring, i);
Juergen Gross85617dc2015-02-17 08:02:49 +0100307 scsifront_do_response(info, ring_rsp);
Juergen Gross8beb8d42014-08-28 06:44:11 +0200308 }
309
310 info->ring.rsp_cons = i;
311
312 if (i != info->ring.req_prod_pvt)
313 RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
314 else
315 info->ring.sring->rsp_event = i + 1;
316
Juergen Gross85617dc2015-02-17 08:02:49 +0100317 return more_to_do;
318}
319
320static int scsifront_cmd_done(struct vscsifrnt_info *info)
321{
322 int more_to_do;
323 unsigned long flags;
324
325 spin_lock_irqsave(info->host->host_lock, flags);
326
327 more_to_do = scsifront_ring_drain(info);
328
Juergen Gross8beb8d42014-08-28 06:44:11 +0200329 info->wait_ring_available = 0;
330
331 spin_unlock_irqrestore(info->host->host_lock, flags);
332
333 wake_up(&info->wq_sync);
334
335 return more_to_do;
336}
337
338static irqreturn_t scsifront_irq_fn(int irq, void *dev_id)
339{
340 struct vscsifrnt_info *info = dev_id;
341
342 while (scsifront_cmd_done(info))
343 /* Yield point for this unbounded loop. */
344 cond_resched();
345
346 return IRQ_HANDLED;
347}
348
Juergen Gross85617dc2015-02-17 08:02:49 +0100349static void scsifront_finish_all(struct vscsifrnt_info *info)
350{
351 unsigned i;
352 struct vscsiif_response resp;
353
354 scsifront_ring_drain(info);
355
356 for (i = 0; i < VSCSIIF_MAX_REQS; i++) {
357 if (test_bit(i, info->shadow_free_bitmap))
358 continue;
359 resp.rqid = i;
360 resp.sense_len = 0;
361 resp.rslt = DID_RESET << 16;
362 resp.residual_len = 0;
363 scsifront_do_response(info, &resp);
364 }
365}
366
Juergen Gross8beb8d42014-08-28 06:44:11 +0200367static int map_data_for_request(struct vscsifrnt_info *info,
368 struct scsi_cmnd *sc,
369 struct vscsiif_request *ring_req,
370 struct vscsifrnt_shadow *shadow)
371{
372 grant_ref_t gref_head;
373 struct page *page;
374 int err, ref, ref_cnt = 0;
375 int grant_ro = (sc->sc_data_direction == DMA_TO_DEVICE);
376 unsigned int i, off, len, bytes;
377 unsigned int data_len = scsi_bufflen(sc);
378 unsigned int data_grants = 0, seg_grants = 0;
379 struct scatterlist *sg;
Juergen Gross8beb8d42014-08-28 06:44:11 +0200380 struct scsiif_request_segment *seg;
381
382 ring_req->nr_segments = 0;
383 if (sc->sc_data_direction == DMA_NONE || !data_len)
384 return 0;
385
386 scsi_for_each_sg(sc, sg, scsi_sg_count(sc), i)
387 data_grants += PFN_UP(sg->offset + sg->length);
388
389 if (data_grants > VSCSIIF_SG_TABLESIZE) {
390 if (data_grants > info->host->sg_tablesize) {
391 shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
392 "Unable to map request_buffer for command!\n");
393 return -E2BIG;
394 }
395 seg_grants = vscsiif_grants_sg(data_grants);
396 shadow->sg = kcalloc(data_grants,
Dan Carpenterd9b1e632014-09-08 14:15:42 +0300397 sizeof(struct scsiif_request_segment), GFP_ATOMIC);
Juergen Gross8beb8d42014-08-28 06:44:11 +0200398 if (!shadow->sg)
399 return -ENOMEM;
400 }
401 seg = shadow->sg ? : ring_req->seg;
402
403 err = gnttab_alloc_grant_references(seg_grants + data_grants,
404 &gref_head);
405 if (err) {
406 kfree(shadow->sg);
407 shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
408 "gnttab_alloc_grant_references() error\n");
409 return -ENOMEM;
410 }
411
412 if (seg_grants) {
413 page = virt_to_page(seg);
414 off = (unsigned long)seg & ~PAGE_MASK;
415 len = sizeof(struct scsiif_request_segment) * data_grants;
416 while (len > 0) {
417 bytes = min_t(unsigned int, len, PAGE_SIZE - off);
418
419 ref = gnttab_claim_grant_reference(&gref_head);
420 BUG_ON(ref == -ENOSPC);
421
Juergen Gross8beb8d42014-08-28 06:44:11 +0200422 gnttab_grant_foreign_access_ref(ref,
Julien Grall0df4f262015-08-07 17:34:37 +0100423 info->dev->otherend_id,
424 xen_page_to_gfn(page), 1);
Juergen Gross8beb8d42014-08-28 06:44:11 +0200425 shadow->gref[ref_cnt] = ref;
426 ring_req->seg[ref_cnt].gref = ref;
427 ring_req->seg[ref_cnt].offset = (uint16_t)off;
428 ring_req->seg[ref_cnt].length = (uint16_t)bytes;
429
430 page++;
431 len -= bytes;
432 off = 0;
433 ref_cnt++;
434 }
435 BUG_ON(seg_grants < ref_cnt);
436 seg_grants = ref_cnt;
437 }
438
439 scsi_for_each_sg(sc, sg, scsi_sg_count(sc), i) {
440 page = sg_page(sg);
441 off = sg->offset;
442 len = sg->length;
443
444 while (len > 0 && data_len > 0) {
445 /*
446 * sg sends a scatterlist that is larger than
447 * the data_len it wants transferred for certain
448 * IO sizes.
449 */
450 bytes = min_t(unsigned int, len, PAGE_SIZE - off);
451 bytes = min(bytes, data_len);
452
453 ref = gnttab_claim_grant_reference(&gref_head);
454 BUG_ON(ref == -ENOSPC);
455
Juergen Gross8beb8d42014-08-28 06:44:11 +0200456 gnttab_grant_foreign_access_ref(ref,
Julien Grall0df4f262015-08-07 17:34:37 +0100457 info->dev->otherend_id,
458 xen_page_to_gfn(page),
459 grant_ro);
Juergen Gross8beb8d42014-08-28 06:44:11 +0200460
461 shadow->gref[ref_cnt] = ref;
462 seg->gref = ref;
463 seg->offset = (uint16_t)off;
464 seg->length = (uint16_t)bytes;
465
466 page++;
467 seg++;
468 len -= bytes;
469 data_len -= bytes;
470 off = 0;
471 ref_cnt++;
472 }
473 }
474
475 if (seg_grants)
476 ring_req->nr_segments = VSCSIIF_SG_GRANT | seg_grants;
477 else
478 ring_req->nr_segments = (uint8_t)ref_cnt;
479 shadow->nr_grants = ref_cnt;
480
481 return 0;
482}
483
484static struct vscsiif_request *scsifront_command2ring(
485 struct vscsifrnt_info *info, struct scsi_cmnd *sc,
486 struct vscsifrnt_shadow *shadow)
487{
488 struct vscsiif_request *ring_req;
489
490 memset(shadow, 0, sizeof(*shadow));
491
492 ring_req = scsifront_pre_req(info);
493 if (!ring_req)
494 return NULL;
495
496 info->shadow[ring_req->rqid] = shadow;
497 shadow->rqid = ring_req->rqid;
498
499 ring_req->id = sc->device->id;
500 ring_req->lun = sc->device->lun;
501 ring_req->channel = sc->device->channel;
502 ring_req->cmd_len = sc->cmd_len;
503
504 BUG_ON(sc->cmd_len > VSCSIIF_MAX_COMMAND_SIZE);
505
506 memcpy(ring_req->cmnd, sc->cmnd, sc->cmd_len);
507
508 ring_req->sc_data_direction = (uint8_t)sc->sc_data_direction;
509 ring_req->timeout_per_command = sc->request->timeout / HZ;
510
511 return ring_req;
512}
513
Juergen Gross85617dc2015-02-17 08:02:49 +0100514static int scsifront_enter(struct vscsifrnt_info *info)
515{
516 if (info->pause)
517 return 1;
518 info->callers++;
519 return 0;
520}
521
522static void scsifront_return(struct vscsifrnt_info *info)
523{
524 info->callers--;
525 if (info->callers)
526 return;
527
528 if (!info->waiting_pause)
529 return;
530
531 info->waiting_pause = 0;
532 wake_up(&info->wq_pause);
533}
534
Juergen Gross8beb8d42014-08-28 06:44:11 +0200535static int scsifront_queuecommand(struct Scsi_Host *shost,
536 struct scsi_cmnd *sc)
537{
538 struct vscsifrnt_info *info = shost_priv(shost);
539 struct vscsiif_request *ring_req;
540 struct vscsifrnt_shadow *shadow = scsi_cmd_priv(sc);
541 unsigned long flags;
542 int err;
543 uint16_t rqid;
544
545 spin_lock_irqsave(shost->host_lock, flags);
Juergen Gross85617dc2015-02-17 08:02:49 +0100546 if (scsifront_enter(info)) {
547 spin_unlock_irqrestore(shost->host_lock, flags);
548 return SCSI_MLQUEUE_HOST_BUSY;
549 }
Juergen Gross8beb8d42014-08-28 06:44:11 +0200550 if (RING_FULL(&info->ring))
551 goto busy;
552
553 ring_req = scsifront_command2ring(info, sc, shadow);
554 if (!ring_req)
555 goto busy;
556
557 sc->result = 0;
558
559 rqid = ring_req->rqid;
560 ring_req->act = VSCSIIF_ACT_SCSI_CDB;
561
562 shadow->sc = sc;
563 shadow->act = VSCSIIF_ACT_SCSI_CDB;
564
565 err = map_data_for_request(info, sc, ring_req, shadow);
566 if (err < 0) {
567 pr_debug("%s: err %d\n", __func__, err);
568 scsifront_put_rqid(info, rqid);
Juergen Gross85617dc2015-02-17 08:02:49 +0100569 scsifront_return(info);
Juergen Gross8beb8d42014-08-28 06:44:11 +0200570 spin_unlock_irqrestore(shost->host_lock, flags);
571 if (err == -ENOMEM)
572 return SCSI_MLQUEUE_HOST_BUSY;
573 sc->result = DID_ERROR << 16;
574 sc->scsi_done(sc);
575 return 0;
576 }
577
578 scsifront_do_request(info);
Juergen Gross85617dc2015-02-17 08:02:49 +0100579 scsifront_return(info);
Juergen Gross8beb8d42014-08-28 06:44:11 +0200580 spin_unlock_irqrestore(shost->host_lock, flags);
581
582 return 0;
583
584busy:
Juergen Gross85617dc2015-02-17 08:02:49 +0100585 scsifront_return(info);
Juergen Gross8beb8d42014-08-28 06:44:11 +0200586 spin_unlock_irqrestore(shost->host_lock, flags);
587 pr_debug("%s: busy\n", __func__);
588 return SCSI_MLQUEUE_HOST_BUSY;
589}
590
591/*
592 * Any exception handling (reset or abort) must be forwarded to the backend.
593 * We have to wait until an answer is returned. This answer contains the
594 * result to be returned to the requestor.
595 */
596static int scsifront_action_handler(struct scsi_cmnd *sc, uint8_t act)
597{
598 struct Scsi_Host *host = sc->device->host;
599 struct vscsifrnt_info *info = shost_priv(host);
600 struct vscsifrnt_shadow *shadow, *s = scsi_cmd_priv(sc);
601 struct vscsiif_request *ring_req;
602 int err = 0;
603
604 shadow = kmalloc(sizeof(*shadow), GFP_NOIO);
605 if (!shadow)
606 return FAILED;
607
David Vrabel7921a112014-09-11 14:20:04 +0100608 spin_lock_irq(host->host_lock);
609
Juergen Gross8beb8d42014-08-28 06:44:11 +0200610 for (;;) {
Juergen Gross8beb8d42014-08-28 06:44:11 +0200611 if (!RING_FULL(&info->ring)) {
612 ring_req = scsifront_command2ring(info, sc, shadow);
613 if (ring_req)
614 break;
615 }
Juergen Gross85617dc2015-02-17 08:02:49 +0100616 if (err || info->pause) {
Juergen Gross8beb8d42014-08-28 06:44:11 +0200617 spin_unlock_irq(host->host_lock);
618 kfree(shadow);
619 return FAILED;
620 }
621 info->wait_ring_available = 1;
622 spin_unlock_irq(host->host_lock);
623 err = wait_event_interruptible(info->wq_sync,
624 !info->wait_ring_available);
625 spin_lock_irq(host->host_lock);
626 }
627
Juergen Gross85617dc2015-02-17 08:02:49 +0100628 if (scsifront_enter(info)) {
629 spin_unlock_irq(host->host_lock);
630 return FAILED;
631 }
632
Juergen Gross8beb8d42014-08-28 06:44:11 +0200633 ring_req->act = act;
634 ring_req->ref_rqid = s->rqid;
635
636 shadow->act = act;
637 shadow->rslt_reset = RSLT_RESET_WAITING;
638 init_waitqueue_head(&shadow->wq_reset);
639
640 ring_req->nr_segments = 0;
641
642 scsifront_do_request(info);
643
644 spin_unlock_irq(host->host_lock);
645 err = wait_event_interruptible(shadow->wq_reset, shadow->wait_reset);
646 spin_lock_irq(host->host_lock);
647
648 if (!err) {
649 err = shadow->rslt_reset;
650 scsifront_put_rqid(info, shadow->rqid);
651 kfree(shadow);
652 } else {
653 spin_lock(&info->shadow_lock);
654 shadow->rslt_reset = RSLT_RESET_ERR;
655 spin_unlock(&info->shadow_lock);
656 err = FAILED;
657 }
658
Juergen Gross85617dc2015-02-17 08:02:49 +0100659 scsifront_return(info);
Juergen Gross8beb8d42014-08-28 06:44:11 +0200660 spin_unlock_irq(host->host_lock);
661 return err;
662}
663
664static int scsifront_eh_abort_handler(struct scsi_cmnd *sc)
665{
666 pr_debug("%s\n", __func__);
667 return scsifront_action_handler(sc, VSCSIIF_ACT_SCSI_ABORT);
668}
669
670static int scsifront_dev_reset_handler(struct scsi_cmnd *sc)
671{
672 pr_debug("%s\n", __func__);
673 return scsifront_action_handler(sc, VSCSIIF_ACT_SCSI_RESET);
674}
675
676static int scsifront_sdev_configure(struct scsi_device *sdev)
677{
678 struct vscsifrnt_info *info = shost_priv(sdev->host);
Zhouyang Jia2941d962018-06-16 01:05:01 +0800679 int err;
Juergen Gross8beb8d42014-08-28 06:44:11 +0200680
Zhouyang Jia2941d962018-06-16 01:05:01 +0800681 if (info && current == info->curr) {
682 err = xenbus_printf(XBT_NIL, info->dev->nodename,
Juergen Gross8beb8d42014-08-28 06:44:11 +0200683 info->dev_state_path, "%d", XenbusStateConnected);
Zhouyang Jia2941d962018-06-16 01:05:01 +0800684 if (err) {
685 xenbus_dev_error(info->dev, err,
686 "%s: writing dev_state_path", __func__);
687 return err;
688 }
689 }
Juergen Gross8beb8d42014-08-28 06:44:11 +0200690
691 return 0;
692}
693
694static void scsifront_sdev_destroy(struct scsi_device *sdev)
695{
696 struct vscsifrnt_info *info = shost_priv(sdev->host);
Zhouyang Jia2941d962018-06-16 01:05:01 +0800697 int err;
Juergen Gross8beb8d42014-08-28 06:44:11 +0200698
Zhouyang Jia2941d962018-06-16 01:05:01 +0800699 if (info && current == info->curr) {
700 err = xenbus_printf(XBT_NIL, info->dev->nodename,
Juergen Gross8beb8d42014-08-28 06:44:11 +0200701 info->dev_state_path, "%d", XenbusStateClosed);
Zhouyang Jia2941d962018-06-16 01:05:01 +0800702 if (err)
703 xenbus_dev_error(info->dev, err,
704 "%s: writing dev_state_path", __func__);
705 }
Juergen Gross8beb8d42014-08-28 06:44:11 +0200706}
707
708static struct scsi_host_template scsifront_sht = {
709 .module = THIS_MODULE,
710 .name = "Xen SCSI frontend driver",
711 .queuecommand = scsifront_queuecommand,
712 .eh_abort_handler = scsifront_eh_abort_handler,
713 .eh_device_reset_handler = scsifront_dev_reset_handler,
714 .slave_configure = scsifront_sdev_configure,
715 .slave_destroy = scsifront_sdev_destroy,
716 .cmd_per_lun = VSCSIIF_DEFAULT_CMD_PER_LUN,
717 .can_queue = VSCSIIF_MAX_REQS,
718 .this_id = -1,
719 .cmd_size = sizeof(struct vscsifrnt_shadow),
720 .sg_tablesize = VSCSIIF_SG_TABLESIZE,
721 .use_clustering = DISABLE_CLUSTERING,
722 .proc_name = "scsifront",
723};
724
725static int scsifront_alloc_ring(struct vscsifrnt_info *info)
726{
727 struct xenbus_device *dev = info->dev;
728 struct vscsiif_sring *sring;
Wei Liuccc9d902015-04-03 14:44:59 +0800729 grant_ref_t gref;
Juergen Gross8beb8d42014-08-28 06:44:11 +0200730 int err = -ENOMEM;
731
732 /***** Frontend to Backend ring start *****/
733 sring = (struct vscsiif_sring *)__get_free_page(GFP_KERNEL);
734 if (!sring) {
735 xenbus_dev_fatal(dev, err,
736 "fail to allocate shared ring (Front to Back)");
737 return err;
738 }
739 SHARED_RING_INIT(sring);
740 FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
741
Wei Liuccc9d902015-04-03 14:44:59 +0800742 err = xenbus_grant_ring(dev, sring, 1, &gref);
Juergen Gross8beb8d42014-08-28 06:44:11 +0200743 if (err < 0) {
744 free_page((unsigned long)sring);
745 xenbus_dev_fatal(dev, err,
746 "fail to grant shared ring (Front to Back)");
747 return err;
748 }
Wei Liuccc9d902015-04-03 14:44:59 +0800749 info->ring_ref = gref;
Juergen Gross8beb8d42014-08-28 06:44:11 +0200750
751 err = xenbus_alloc_evtchn(dev, &info->evtchn);
752 if (err) {
753 xenbus_dev_fatal(dev, err, "xenbus_alloc_evtchn");
754 goto free_gnttab;
755 }
756
757 err = bind_evtchn_to_irq(info->evtchn);
758 if (err <= 0) {
759 xenbus_dev_fatal(dev, err, "bind_evtchn_to_irq");
760 goto free_gnttab;
761 }
762
763 info->irq = err;
764
765 err = request_threaded_irq(info->irq, NULL, scsifront_irq_fn,
766 IRQF_ONESHOT, "scsifront", info);
767 if (err) {
768 xenbus_dev_fatal(dev, err, "request_threaded_irq");
769 goto free_irq;
770 }
771
772 return 0;
773
774/* free resource */
775free_irq:
776 unbind_from_irqhandler(info->irq, info);
777free_gnttab:
778 gnttab_end_foreign_access(info->ring_ref, 0,
779 (unsigned long)info->ring.sring);
780
781 return err;
782}
783
Juergen Gross85617dc2015-02-17 08:02:49 +0100784static void scsifront_free_ring(struct vscsifrnt_info *info)
785{
786 unbind_from_irqhandler(info->irq, info);
787 gnttab_end_foreign_access(info->ring_ref, 0,
788 (unsigned long)info->ring.sring);
789}
790
Juergen Gross8beb8d42014-08-28 06:44:11 +0200791static int scsifront_init_ring(struct vscsifrnt_info *info)
792{
793 struct xenbus_device *dev = info->dev;
794 struct xenbus_transaction xbt;
795 int err;
796
797 pr_debug("%s\n", __func__);
798
799 err = scsifront_alloc_ring(info);
800 if (err)
801 return err;
802 pr_debug("%s: %u %u\n", __func__, info->ring_ref, info->evtchn);
803
804again:
805 err = xenbus_transaction_start(&xbt);
806 if (err)
807 xenbus_dev_fatal(dev, err, "starting transaction");
808
809 err = xenbus_printf(xbt, dev->nodename, "ring-ref", "%u",
810 info->ring_ref);
811 if (err) {
812 xenbus_dev_fatal(dev, err, "%s", "writing ring-ref");
813 goto fail;
814 }
815
816 err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
817 info->evtchn);
818
819 if (err) {
820 xenbus_dev_fatal(dev, err, "%s", "writing event-channel");
821 goto fail;
822 }
823
824 err = xenbus_transaction_end(xbt, 0);
825 if (err) {
826 if (err == -EAGAIN)
827 goto again;
828 xenbus_dev_fatal(dev, err, "completing transaction");
829 goto free_sring;
830 }
831
832 return 0;
833
834fail:
835 xenbus_transaction_end(xbt, 1);
836free_sring:
Juergen Gross85617dc2015-02-17 08:02:49 +0100837 scsifront_free_ring(info);
Juergen Gross8beb8d42014-08-28 06:44:11 +0200838
839 return err;
840}
841
842
843static int scsifront_probe(struct xenbus_device *dev,
844 const struct xenbus_device_id *id)
845{
846 struct vscsifrnt_info *info;
847 struct Scsi_Host *host;
848 int err = -ENOMEM;
849 char name[TASK_COMM_LEN];
850
851 host = scsi_host_alloc(&scsifront_sht, sizeof(*info));
852 if (!host) {
853 xenbus_dev_fatal(dev, err, "fail to allocate scsi host");
854 return err;
855 }
856 info = (struct vscsifrnt_info *)host->hostdata;
857
858 dev_set_drvdata(&dev->dev, info);
859 info->dev = dev;
860
861 bitmap_fill(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
862
863 err = scsifront_init_ring(info);
864 if (err) {
865 scsi_host_put(host);
866 return err;
867 }
868
869 init_waitqueue_head(&info->wq_sync);
Juergen Gross85617dc2015-02-17 08:02:49 +0100870 init_waitqueue_head(&info->wq_pause);
Juergen Gross8beb8d42014-08-28 06:44:11 +0200871 spin_lock_init(&info->shadow_lock);
872
873 snprintf(name, TASK_COMM_LEN, "vscsiif.%d", host->host_no);
874
875 host->max_id = VSCSIIF_MAX_TARGET;
876 host->max_channel = 0;
877 host->max_lun = VSCSIIF_MAX_LUN;
878 host->max_sectors = (host->sg_tablesize - 1) * PAGE_SIZE / 512;
879 host->max_cmd_len = VSCSIIF_MAX_COMMAND_SIZE;
880
881 err = scsi_add_host(host, &dev->dev);
882 if (err) {
883 dev_err(&dev->dev, "fail to add scsi host %d\n", err);
884 goto free_sring;
885 }
886 info->host = host;
887 info->host_active = 1;
888
889 xenbus_switch_state(dev, XenbusStateInitialised);
890
891 return 0;
892
893free_sring:
Juergen Gross85617dc2015-02-17 08:02:49 +0100894 scsifront_free_ring(info);
Juergen Gross8beb8d42014-08-28 06:44:11 +0200895 scsi_host_put(host);
896 return err;
897}
898
Juergen Gross85617dc2015-02-17 08:02:49 +0100899static int scsifront_resume(struct xenbus_device *dev)
900{
901 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
902 struct Scsi_Host *host = info->host;
903 int err;
904
905 spin_lock_irq(host->host_lock);
906
907 /* Finish all still pending commands. */
908 scsifront_finish_all(info);
909
910 spin_unlock_irq(host->host_lock);
911
912 /* Reconnect to dom0. */
913 scsifront_free_ring(info);
914 err = scsifront_init_ring(info);
915 if (err) {
916 dev_err(&dev->dev, "fail to resume %d\n", err);
917 scsi_host_put(host);
918 return err;
919 }
920
921 xenbus_switch_state(dev, XenbusStateInitialised);
922
923 return 0;
924}
925
926static int scsifront_suspend(struct xenbus_device *dev)
927{
928 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
929 struct Scsi_Host *host = info->host;
930 int err = 0;
931
932 /* No new commands for the backend. */
933 spin_lock_irq(host->host_lock);
934 info->pause = 1;
935 while (info->callers && !err) {
936 info->waiting_pause = 1;
937 info->wait_ring_available = 0;
938 spin_unlock_irq(host->host_lock);
939 wake_up(&info->wq_sync);
940 err = wait_event_interruptible(info->wq_pause,
941 !info->waiting_pause);
942 spin_lock_irq(host->host_lock);
943 }
944 spin_unlock_irq(host->host_lock);
945 return err;
946}
947
Juergen Gross8beb8d42014-08-28 06:44:11 +0200948static int scsifront_remove(struct xenbus_device *dev)
949{
950 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
951
952 pr_debug("%s: %s removed\n", __func__, dev->nodename);
953
954 mutex_lock(&scsifront_mutex);
955 if (info->host_active) {
956 /* Scsi_host not yet removed */
957 scsi_remove_host(info->host);
958 info->host_active = 0;
959 }
960 mutex_unlock(&scsifront_mutex);
961
Juergen Gross85617dc2015-02-17 08:02:49 +0100962 scsifront_free_ring(info);
Juergen Gross8beb8d42014-08-28 06:44:11 +0200963 scsi_host_put(info->host);
964
965 return 0;
966}
967
968static void scsifront_disconnect(struct vscsifrnt_info *info)
969{
970 struct xenbus_device *dev = info->dev;
971 struct Scsi_Host *host = info->host;
972
973 pr_debug("%s: %s disconnect\n", __func__, dev->nodename);
974
975 /*
976 * When this function is executed, all devices of
977 * Frontend have been deleted.
978 * Therefore, it need not block I/O before remove_host.
979 */
980
981 mutex_lock(&scsifront_mutex);
982 if (info->host_active) {
983 scsi_remove_host(host);
984 info->host_active = 0;
985 }
986 mutex_unlock(&scsifront_mutex);
987
988 xenbus_frontend_closed(dev);
989}
990
991static void scsifront_do_lun_hotplug(struct vscsifrnt_info *info, int op)
992{
993 struct xenbus_device *dev = info->dev;
994 int i, err = 0;
995 char str[64];
996 char **dir;
997 unsigned int dir_n = 0;
998 unsigned int device_state;
999 unsigned int hst, chn, tgt, lun;
1000 struct scsi_device *sdev;
1001
1002 dir = xenbus_directory(XBT_NIL, dev->otherend, "vscsi-devs", &dir_n);
1003 if (IS_ERR(dir))
1004 return;
1005
1006 /* mark current task as the one allowed to modify device states */
1007 BUG_ON(info->curr);
1008 info->curr = current;
1009
1010 for (i = 0; i < dir_n; i++) {
1011 /* read status */
1012 snprintf(str, sizeof(str), "vscsi-devs/%s/state", dir[i]);
1013 err = xenbus_scanf(XBT_NIL, dev->otherend, str, "%u",
1014 &device_state);
1015 if (XENBUS_EXIST_ERR(err))
1016 continue;
1017
1018 /* virtual SCSI device */
1019 snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", dir[i]);
1020 err = xenbus_scanf(XBT_NIL, dev->otherend, str,
1021 "%u:%u:%u:%u", &hst, &chn, &tgt, &lun);
1022 if (XENBUS_EXIST_ERR(err))
1023 continue;
1024
1025 /*
1026 * Front device state path, used in slave_configure called
1027 * on successfull scsi_add_device, and in slave_destroy called
1028 * on remove of a device.
1029 */
1030 snprintf(info->dev_state_path, sizeof(info->dev_state_path),
1031 "vscsi-devs/%s/state", dir[i]);
1032
1033 switch (op) {
1034 case VSCSIFRONT_OP_ADD_LUN:
1035 if (device_state != XenbusStateInitialised)
1036 break;
1037
1038 if (scsi_add_device(info->host, chn, tgt, lun)) {
1039 dev_err(&dev->dev, "scsi_add_device\n");
Zhouyang Jia2941d962018-06-16 01:05:01 +08001040 err = xenbus_printf(XBT_NIL, dev->nodename,
Juergen Gross8beb8d42014-08-28 06:44:11 +02001041 info->dev_state_path,
1042 "%d", XenbusStateClosed);
Zhouyang Jia2941d962018-06-16 01:05:01 +08001043 if (err)
1044 xenbus_dev_error(dev, err,
1045 "%s: writing dev_state_path", __func__);
Juergen Gross8beb8d42014-08-28 06:44:11 +02001046 }
1047 break;
1048 case VSCSIFRONT_OP_DEL_LUN:
1049 if (device_state != XenbusStateClosing)
1050 break;
1051
1052 sdev = scsi_device_lookup(info->host, chn, tgt, lun);
1053 if (sdev) {
1054 scsi_remove_device(sdev);
1055 scsi_device_put(sdev);
1056 }
1057 break;
Juergen Gross85617dc2015-02-17 08:02:49 +01001058 case VSCSIFRONT_OP_READD_LUN:
Zhouyang Jia2941d962018-06-16 01:05:01 +08001059 if (device_state == XenbusStateConnected) {
1060 err = xenbus_printf(XBT_NIL, dev->nodename,
Juergen Gross85617dc2015-02-17 08:02:49 +01001061 info->dev_state_path,
1062 "%d", XenbusStateConnected);
Zhouyang Jia2941d962018-06-16 01:05:01 +08001063 if (err)
1064 xenbus_dev_error(dev, err,
1065 "%s: writing dev_state_path", __func__);
1066 }
Juergen Gross85617dc2015-02-17 08:02:49 +01001067 break;
Juergen Gross8beb8d42014-08-28 06:44:11 +02001068 default:
1069 break;
1070 }
1071 }
1072
1073 info->curr = NULL;
1074
1075 kfree(dir);
1076}
1077
1078static void scsifront_read_backend_params(struct xenbus_device *dev,
1079 struct vscsifrnt_info *info)
1080{
Juergen Gross85617dc2015-02-17 08:02:49 +01001081 unsigned int sg_grant, nr_segs;
Juergen Gross8beb8d42014-08-28 06:44:11 +02001082 int ret;
1083 struct Scsi_Host *host = info->host;
1084
1085 ret = xenbus_scanf(XBT_NIL, dev->otherend, "feature-sg-grant", "%u",
1086 &sg_grant);
Juergen Gross85617dc2015-02-17 08:02:49 +01001087 if (ret != 1)
1088 sg_grant = 0;
1089 nr_segs = min_t(unsigned int, sg_grant, SG_ALL);
1090 nr_segs = max_t(unsigned int, nr_segs, VSCSIIF_SG_TABLESIZE);
1091 nr_segs = min_t(unsigned int, nr_segs,
Juergen Gross8beb8d42014-08-28 06:44:11 +02001092 VSCSIIF_SG_TABLESIZE * PAGE_SIZE /
1093 sizeof(struct scsiif_request_segment));
Juergen Gross85617dc2015-02-17 08:02:49 +01001094
1095 if (!info->pause && sg_grant)
1096 dev_info(&dev->dev, "using up to %d SG entries\n", nr_segs);
1097 else if (info->pause && nr_segs < host->sg_tablesize)
1098 dev_warn(&dev->dev,
1099 "SG entries decreased from %d to %u - device may not work properly anymore\n",
1100 host->sg_tablesize, nr_segs);
1101
1102 host->sg_tablesize = nr_segs;
1103 host->max_sectors = (nr_segs - 1) * PAGE_SIZE / 512;
Juergen Gross8beb8d42014-08-28 06:44:11 +02001104}
1105
1106static void scsifront_backend_changed(struct xenbus_device *dev,
1107 enum xenbus_state backend_state)
1108{
1109 struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
1110
1111 pr_debug("%s: %p %u %u\n", __func__, dev, dev->state, backend_state);
1112
1113 switch (backend_state) {
1114 case XenbusStateUnknown:
1115 case XenbusStateInitialising:
1116 case XenbusStateInitWait:
1117 case XenbusStateInitialised:
1118 break;
1119
1120 case XenbusStateConnected:
1121 scsifront_read_backend_params(dev, info);
Juergen Gross85617dc2015-02-17 08:02:49 +01001122
1123 if (info->pause) {
1124 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_READD_LUN);
1125 xenbus_switch_state(dev, XenbusStateConnected);
1126 info->pause = 0;
1127 return;
1128 }
1129
Juergen Gross8beb8d42014-08-28 06:44:11 +02001130 if (xenbus_read_driver_state(dev->nodename) ==
1131 XenbusStateInitialised)
1132 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN);
1133
1134 if (dev->state != XenbusStateConnected)
1135 xenbus_switch_state(dev, XenbusStateConnected);
1136 break;
1137
1138 case XenbusStateClosed:
1139 if (dev->state == XenbusStateClosed)
1140 break;
1141 /* Missed the backend's Closing state -- fallthrough */
1142 case XenbusStateClosing:
1143 scsifront_disconnect(info);
1144 break;
1145
1146 case XenbusStateReconfiguring:
1147 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_DEL_LUN);
1148 xenbus_switch_state(dev, XenbusStateReconfiguring);
1149 break;
1150
1151 case XenbusStateReconfigured:
1152 scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN);
1153 xenbus_switch_state(dev, XenbusStateConnected);
1154 break;
1155 }
1156}
1157
1158static const struct xenbus_device_id scsifront_ids[] = {
1159 { "vscsi" },
1160 { "" }
1161};
1162
David Vrabel95afae42014-09-08 17:30:41 +01001163static struct xenbus_driver scsifront_driver = {
1164 .ids = scsifront_ids,
Juergen Gross8beb8d42014-08-28 06:44:11 +02001165 .probe = scsifront_probe,
1166 .remove = scsifront_remove,
Juergen Gross85617dc2015-02-17 08:02:49 +01001167 .resume = scsifront_resume,
1168 .suspend = scsifront_suspend,
Juergen Gross8beb8d42014-08-28 06:44:11 +02001169 .otherend_changed = scsifront_backend_changed,
David Vrabel95afae42014-09-08 17:30:41 +01001170};
Juergen Gross8beb8d42014-08-28 06:44:11 +02001171
1172static int __init scsifront_init(void)
1173{
1174 if (!xen_domain())
1175 return -ENODEV;
1176
1177 return xenbus_register_frontend(&scsifront_driver);
1178}
1179module_init(scsifront_init);
1180
1181static void __exit scsifront_exit(void)
1182{
1183 xenbus_unregister_driver(&scsifront_driver);
1184}
1185module_exit(scsifront_exit);
1186
1187MODULE_DESCRIPTION("Xen SCSI frontend driver");
1188MODULE_LICENSE("GPL");
1189MODULE_ALIAS("xen:vscsi");
1190MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");