blob: dc2daec9a10da7d805a652f58c5a097c6d79cc49 [file] [log] [blame]
Paolo Bonzini4fe74b12012-02-05 12:16:00 +01001/*
2 * Virtio SCSI HBA driver
3 *
4 * Copyright IBM Corp. 2010
5 * Copyright Red Hat, Inc. 2011
6 *
7 * Authors:
8 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
9 * Paolo Bonzini <pbonzini@redhat.com>
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
13 *
14 */
15
Wanlong Gaoba06d1e2013-03-12 15:34:40 +103016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010018#include <linux/module.h>
19#include <linux/slab.h>
20#include <linux/mempool.h>
21#include <linux/virtio.h>
22#include <linux/virtio_ids.h>
23#include <linux/virtio_config.h>
24#include <linux/virtio_scsi.h>
25#include <scsi/scsi_host.h>
26#include <scsi/scsi_device.h>
27#include <scsi/scsi_cmnd.h>
28
29#define VIRTIO_SCSI_MEMPOOL_SZ 64
Cong Meng365a7152012-07-05 17:06:43 +080030#define VIRTIO_SCSI_EVENT_LEN 8
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010031
32/* Command queue element */
33struct virtio_scsi_cmd {
34 struct scsi_cmnd *sc;
35 struct completion *comp;
36 union {
37 struct virtio_scsi_cmd_req cmd;
38 struct virtio_scsi_ctrl_tmf_req tmf;
39 struct virtio_scsi_ctrl_an_req an;
40 } req;
41 union {
42 struct virtio_scsi_cmd_resp cmd;
43 struct virtio_scsi_ctrl_tmf_resp tmf;
44 struct virtio_scsi_ctrl_an_resp an;
45 struct virtio_scsi_event evt;
46 } resp;
47} ____cacheline_aligned_in_smp;
48
Cong Meng365a7152012-07-05 17:06:43 +080049struct virtio_scsi_event_node {
50 struct virtio_scsi *vscsi;
51 struct virtio_scsi_event event;
52 struct work_struct work;
53};
54
Paolo Bonzini139fe452012-06-13 16:56:32 +020055struct virtio_scsi_vq {
56 /* Protects vq */
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010057 spinlock_t vq_lock;
58
Paolo Bonzini139fe452012-06-13 16:56:32 +020059 struct virtqueue *vq;
60};
61
Paolo Bonzini2bd37f02012-06-13 16:56:34 +020062/* Per-target queue state */
63struct virtio_scsi_target_state {
Wanlong Gao682993b2013-03-20 15:44:28 +103064 /* Never held at the same time as vq_lock. */
Paolo Bonzini2bd37f02012-06-13 16:56:34 +020065 spinlock_t tgt_lock;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +020066};
67
Paolo Bonzini139fe452012-06-13 16:56:32 +020068/* Driver instance state */
69struct virtio_scsi {
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010070 struct virtio_device *vdev;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +020071
Paolo Bonzini139fe452012-06-13 16:56:32 +020072 struct virtio_scsi_vq ctrl_vq;
73 struct virtio_scsi_vq event_vq;
74 struct virtio_scsi_vq req_vq;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010075
Cong Meng365a7152012-07-05 17:06:43 +080076 /* Get some buffers ready for event vq */
77 struct virtio_scsi_event_node event_list[VIRTIO_SCSI_EVENT_LEN];
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010078};
79
80static struct kmem_cache *virtscsi_cmd_cache;
81static mempool_t *virtscsi_cmd_pool;
82
83static inline struct Scsi_Host *virtio_scsi_host(struct virtio_device *vdev)
84{
85 return vdev->priv;
86}
87
88static void virtscsi_compute_resid(struct scsi_cmnd *sc, u32 resid)
89{
90 if (!resid)
91 return;
92
93 if (!scsi_bidi_cmnd(sc)) {
94 scsi_set_resid(sc, resid);
95 return;
96 }
97
98 scsi_in(sc)->resid = min(resid, scsi_in(sc)->length);
99 scsi_out(sc)->resid = resid - scsi_in(sc)->resid;
100}
101
102/**
103 * virtscsi_complete_cmd - finish a scsi_cmd and invoke scsi_done
104 *
105 * Called with vq_lock held.
106 */
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930107static void virtscsi_complete_cmd(struct virtio_scsi *vscsi, void *buf)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100108{
109 struct virtio_scsi_cmd *cmd = buf;
110 struct scsi_cmnd *sc = cmd->sc;
111 struct virtio_scsi_cmd_resp *resp = &cmd->resp.cmd;
112
113 dev_dbg(&sc->device->sdev_gendev,
114 "cmd %p response %u status %#02x sense_len %u\n",
115 sc, resp->response, resp->status, resp->sense_len);
116
117 sc->result = resp->status;
118 virtscsi_compute_resid(sc, resp->resid);
119 switch (resp->response) {
120 case VIRTIO_SCSI_S_OK:
121 set_host_byte(sc, DID_OK);
122 break;
123 case VIRTIO_SCSI_S_OVERRUN:
124 set_host_byte(sc, DID_ERROR);
125 break;
126 case VIRTIO_SCSI_S_ABORTED:
127 set_host_byte(sc, DID_ABORT);
128 break;
129 case VIRTIO_SCSI_S_BAD_TARGET:
130 set_host_byte(sc, DID_BAD_TARGET);
131 break;
132 case VIRTIO_SCSI_S_RESET:
133 set_host_byte(sc, DID_RESET);
134 break;
135 case VIRTIO_SCSI_S_BUSY:
136 set_host_byte(sc, DID_BUS_BUSY);
137 break;
138 case VIRTIO_SCSI_S_TRANSPORT_FAILURE:
139 set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
140 break;
141 case VIRTIO_SCSI_S_TARGET_FAILURE:
142 set_host_byte(sc, DID_TARGET_FAILURE);
143 break;
144 case VIRTIO_SCSI_S_NEXUS_FAILURE:
145 set_host_byte(sc, DID_NEXUS_FAILURE);
146 break;
147 default:
148 scmd_printk(KERN_WARNING, sc, "Unknown response %d",
149 resp->response);
150 /* fall through */
151 case VIRTIO_SCSI_S_FAILURE:
152 set_host_byte(sc, DID_ERROR);
153 break;
154 }
155
156 WARN_ON(resp->sense_len > VIRTIO_SCSI_SENSE_SIZE);
157 if (sc->sense_buffer) {
158 memcpy(sc->sense_buffer, resp->sense,
159 min_t(u32, resp->sense_len, VIRTIO_SCSI_SENSE_SIZE));
160 if (resp->sense_len)
161 set_driver_byte(sc, DRIVER_SENSE);
162 }
163
164 mempool_free(cmd, virtscsi_cmd_pool);
165 sc->scsi_done(sc);
166}
167
Paolo Bonzini10f34f62013-04-08 23:02:07 +0930168static void virtscsi_vq_done(struct virtio_scsi *vscsi,
169 struct virtio_scsi_vq *virtscsi_vq,
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930170 void (*fn)(struct virtio_scsi *vscsi, void *buf))
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100171{
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100172 void *buf;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100173 unsigned int len;
Paolo Bonzini10f34f62013-04-08 23:02:07 +0930174 unsigned long flags;
175 struct virtqueue *vq = virtscsi_vq->vq;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100176
Paolo Bonzini10f34f62013-04-08 23:02:07 +0930177 spin_lock_irqsave(&virtscsi_vq->vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100178 do {
179 virtqueue_disable_cb(vq);
180 while ((buf = virtqueue_get_buf(vq, &len)) != NULL)
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930181 fn(vscsi, buf);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100182 } while (!virtqueue_enable_cb(vq));
Paolo Bonzini10f34f62013-04-08 23:02:07 +0930183 spin_unlock_irqrestore(&virtscsi_vq->vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100184}
185
186static void virtscsi_req_done(struct virtqueue *vq)
187{
Paolo Bonzini139fe452012-06-13 16:56:32 +0200188 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
189 struct virtio_scsi *vscsi = shost_priv(sh);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200190
Paolo Bonzini10f34f62013-04-08 23:02:07 +0930191 virtscsi_vq_done(vscsi, &vscsi->req_vq, virtscsi_complete_cmd);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100192};
193
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930194static void virtscsi_complete_free(struct virtio_scsi *vscsi, void *buf)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100195{
196 struct virtio_scsi_cmd *cmd = buf;
197
198 if (cmd->comp)
199 complete_all(cmd->comp);
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200200 else
201 mempool_free(cmd, virtscsi_cmd_pool);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100202}
203
204static void virtscsi_ctrl_done(struct virtqueue *vq)
205{
Paolo Bonzini139fe452012-06-13 16:56:32 +0200206 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
207 struct virtio_scsi *vscsi = shost_priv(sh);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200208
Paolo Bonzini10f34f62013-04-08 23:02:07 +0930209 virtscsi_vq_done(vscsi, &vscsi->ctrl_vq, virtscsi_complete_free);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100210};
211
Cong Meng365a7152012-07-05 17:06:43 +0800212static int virtscsi_kick_event(struct virtio_scsi *vscsi,
213 struct virtio_scsi_event_node *event_node)
214{
Rusty Russell4614e512012-10-16 23:56:16 +1030215 int err;
Cong Meng365a7152012-07-05 17:06:43 +0800216 struct scatterlist sg;
217 unsigned long flags;
218
Richard W.M. Jones2e9c9df2012-10-02 17:25:46 +0200219 sg_init_one(&sg, &event_node->event, sizeof(struct virtio_scsi_event));
Cong Meng365a7152012-07-05 17:06:43 +0800220
221 spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
222
Rusty Russellbf958292013-03-20 15:44:28 +1030223 err = virtqueue_add_inbuf(vscsi->event_vq.vq, &sg, 1, event_node,
224 GFP_ATOMIC);
Rusty Russell4614e512012-10-16 23:56:16 +1030225 if (!err)
Cong Meng365a7152012-07-05 17:06:43 +0800226 virtqueue_kick(vscsi->event_vq.vq);
227
228 spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
229
Rusty Russell4614e512012-10-16 23:56:16 +1030230 return err;
Cong Meng365a7152012-07-05 17:06:43 +0800231}
232
233static int virtscsi_kick_event_all(struct virtio_scsi *vscsi)
234{
235 int i;
236
237 for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) {
238 vscsi->event_list[i].vscsi = vscsi;
239 virtscsi_kick_event(vscsi, &vscsi->event_list[i]);
240 }
241
242 return 0;
243}
244
245static void virtscsi_cancel_event_work(struct virtio_scsi *vscsi)
246{
247 int i;
248
249 for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++)
250 cancel_work_sync(&vscsi->event_list[i].work);
251}
252
253static void virtscsi_handle_transport_reset(struct virtio_scsi *vscsi,
254 struct virtio_scsi_event *event)
255{
256 struct scsi_device *sdev;
257 struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
258 unsigned int target = event->lun[1];
259 unsigned int lun = (event->lun[2] << 8) | event->lun[3];
260
261 switch (event->reason) {
262 case VIRTIO_SCSI_EVT_RESET_RESCAN:
263 scsi_add_device(shost, 0, target, lun);
264 break;
265 case VIRTIO_SCSI_EVT_RESET_REMOVED:
266 sdev = scsi_device_lookup(shost, 0, target, lun);
267 if (sdev) {
268 scsi_remove_device(sdev);
269 scsi_device_put(sdev);
270 } else {
271 pr_err("SCSI device %d 0 %d %d not found\n",
272 shost->host_no, target, lun);
273 }
274 break;
275 default:
276 pr_info("Unsupport virtio scsi event reason %x\n", event->reason);
277 }
278}
279
Paolo Bonzini865b58c2012-10-02 17:25:48 +0200280static void virtscsi_handle_param_change(struct virtio_scsi *vscsi,
281 struct virtio_scsi_event *event)
282{
283 struct scsi_device *sdev;
284 struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
285 unsigned int target = event->lun[1];
286 unsigned int lun = (event->lun[2] << 8) | event->lun[3];
287 u8 asc = event->reason & 255;
288 u8 ascq = event->reason >> 8;
289
290 sdev = scsi_device_lookup(shost, 0, target, lun);
291 if (!sdev) {
292 pr_err("SCSI device %d 0 %d %d not found\n",
293 shost->host_no, target, lun);
294 return;
295 }
296
297 /* Handle "Parameters changed", "Mode parameters changed", and
298 "Capacity data has changed". */
299 if (asc == 0x2a && (ascq == 0x00 || ascq == 0x01 || ascq == 0x09))
300 scsi_rescan_device(&sdev->sdev_gendev);
301
302 scsi_device_put(sdev);
303}
304
Cong Meng365a7152012-07-05 17:06:43 +0800305static void virtscsi_handle_event(struct work_struct *work)
306{
307 struct virtio_scsi_event_node *event_node =
308 container_of(work, struct virtio_scsi_event_node, work);
309 struct virtio_scsi *vscsi = event_node->vscsi;
310 struct virtio_scsi_event *event = &event_node->event;
311
312 if (event->event & VIRTIO_SCSI_T_EVENTS_MISSED) {
313 event->event &= ~VIRTIO_SCSI_T_EVENTS_MISSED;
314 scsi_scan_host(virtio_scsi_host(vscsi->vdev));
315 }
316
317 switch (event->event) {
318 case VIRTIO_SCSI_T_NO_EVENT:
319 break;
320 case VIRTIO_SCSI_T_TRANSPORT_RESET:
321 virtscsi_handle_transport_reset(vscsi, event);
322 break;
Paolo Bonzini865b58c2012-10-02 17:25:48 +0200323 case VIRTIO_SCSI_T_PARAM_CHANGE:
324 virtscsi_handle_param_change(vscsi, event);
325 break;
Cong Meng365a7152012-07-05 17:06:43 +0800326 default:
327 pr_err("Unsupport virtio scsi event %x\n", event->event);
328 }
329 virtscsi_kick_event(vscsi, event_node);
330}
331
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930332static void virtscsi_complete_event(struct virtio_scsi *vscsi, void *buf)
Cong Meng365a7152012-07-05 17:06:43 +0800333{
334 struct virtio_scsi_event_node *event_node = buf;
335
336 INIT_WORK(&event_node->work, virtscsi_handle_event);
337 schedule_work(&event_node->work);
338}
339
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100340static void virtscsi_event_done(struct virtqueue *vq)
341{
Paolo Bonzini139fe452012-06-13 16:56:32 +0200342 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
343 struct virtio_scsi *vscsi = shost_priv(sh);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200344
Paolo Bonzini10f34f62013-04-08 23:02:07 +0930345 virtscsi_vq_done(vscsi, &vscsi->event_vq, virtscsi_complete_event);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100346};
347
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100348/**
Wanlong Gao682993b2013-03-20 15:44:28 +1030349 * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue
350 * @vq : the struct virtqueue we're talking about
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100351 * @cmd : command structure
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100352 * @req_size : size of the request buffer
353 * @resp_size : size of the response buffer
Wanlong Gao682993b2013-03-20 15:44:28 +1030354 * @gfp : flags to use for memory allocations
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100355 */
Wanlong Gao682993b2013-03-20 15:44:28 +1030356static int virtscsi_add_cmd(struct virtqueue *vq,
357 struct virtio_scsi_cmd *cmd,
358 size_t req_size, size_t resp_size, gfp_t gfp)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100359{
360 struct scsi_cmnd *sc = cmd->sc;
Wanlong Gao682993b2013-03-20 15:44:28 +1030361 struct scatterlist *sgs[4], req, resp;
362 struct sg_table *out, *in;
363 unsigned out_num = 0, in_num = 0;
364
365 out = in = NULL;
366
367 if (sc && sc->sc_data_direction != DMA_NONE) {
368 if (sc->sc_data_direction != DMA_FROM_DEVICE)
369 out = &scsi_out(sc)->table;
370 if (sc->sc_data_direction != DMA_TO_DEVICE)
371 in = &scsi_in(sc)->table;
372 }
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100373
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100374 /* Request header. */
Wanlong Gao682993b2013-03-20 15:44:28 +1030375 sg_init_one(&req, &cmd->req, req_size);
376 sgs[out_num++] = &req;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100377
378 /* Data-out buffer. */
Wanlong Gao682993b2013-03-20 15:44:28 +1030379 if (out)
380 sgs[out_num++] = out->sgl;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100381
382 /* Response header. */
Wanlong Gao682993b2013-03-20 15:44:28 +1030383 sg_init_one(&resp, &cmd->resp, resp_size);
384 sgs[out_num + in_num++] = &resp;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100385
386 /* Data-in buffer */
Wanlong Gao682993b2013-03-20 15:44:28 +1030387 if (in)
388 sgs[out_num + in_num++] = in->sgl;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100389
Wanlong Gao682993b2013-03-20 15:44:28 +1030390 return virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, gfp);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100391}
392
Wanlong Gao682993b2013-03-20 15:44:28 +1030393static int virtscsi_kick_cmd(struct virtio_scsi_vq *vq,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100394 struct virtio_scsi_cmd *cmd,
395 size_t req_size, size_t resp_size, gfp_t gfp)
396{
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100397 unsigned long flags;
Rusty Russell4614e512012-10-16 23:56:16 +1030398 int err;
399 bool needs_kick = false;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100400
Wanlong Gao682993b2013-03-20 15:44:28 +1030401 spin_lock_irqsave(&vq->vq_lock, flags);
402 err = virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size, gfp);
Rusty Russell4614e512012-10-16 23:56:16 +1030403 if (!err)
404 needs_kick = virtqueue_kick_prepare(vq->vq);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100405
Paolo Bonzinibce750b2012-06-13 16:56:33 +0200406 spin_unlock_irqrestore(&vq->vq_lock, flags);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200407
Rusty Russell4614e512012-10-16 23:56:16 +1030408 if (needs_kick)
Paolo Bonzini139fe452012-06-13 16:56:32 +0200409 virtqueue_notify(vq->vq);
Rusty Russell4614e512012-10-16 23:56:16 +1030410 return err;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100411}
412
413static int virtscsi_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
414{
415 struct virtio_scsi *vscsi = shost_priv(sh);
416 struct virtio_scsi_cmd *cmd;
417 int ret;
418
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200419 struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
420 BUG_ON(scsi_sg_count(sc) > shost->sg_tablesize);
421
422 /* TODO: check feature bit and fail if unsupported? */
423 BUG_ON(sc->sc_data_direction == DMA_BIDIRECTIONAL);
424
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100425 dev_dbg(&sc->device->sdev_gendev,
426 "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]);
427
428 ret = SCSI_MLQUEUE_HOST_BUSY;
429 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_ATOMIC);
430 if (!cmd)
431 goto out;
432
433 memset(cmd, 0, sizeof(*cmd));
434 cmd->sc = sc;
435 cmd->req.cmd = (struct virtio_scsi_cmd_req){
436 .lun[0] = 1,
437 .lun[1] = sc->device->id,
438 .lun[2] = (sc->device->lun >> 8) | 0x40,
439 .lun[3] = sc->device->lun & 0xff,
440 .tag = (unsigned long)sc,
441 .task_attr = VIRTIO_SCSI_S_SIMPLE,
442 .prio = 0,
443 .crn = 0,
444 };
445
446 BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE);
447 memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len);
448
Wanlong Gao682993b2013-03-20 15:44:28 +1030449 if (virtscsi_kick_cmd(&vscsi->req_vq, cmd,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100450 sizeof cmd->req.cmd, sizeof cmd->resp.cmd,
Rusty Russell4614e512012-10-16 23:56:16 +1030451 GFP_ATOMIC) == 0)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100452 ret = 0;
Eric Northupb56d1002012-11-08 01:55:50 -0800453 else
454 mempool_free(cmd, virtscsi_cmd_pool);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100455
456out:
457 return ret;
458}
459
460static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd)
461{
462 DECLARE_COMPLETION_ONSTACK(comp);
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200463 int ret = FAILED;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100464
465 cmd->comp = &comp;
Wanlong Gao682993b2013-03-20 15:44:28 +1030466 if (virtscsi_kick_cmd(&vscsi->ctrl_vq, cmd,
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200467 sizeof cmd->req.tmf, sizeof cmd->resp.tmf,
468 GFP_NOIO) < 0)
469 goto out;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100470
471 wait_for_completion(&comp);
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200472 if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK ||
473 cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED)
474 ret = SUCCESS;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100475
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200476out:
477 mempool_free(cmd, virtscsi_cmd_pool);
478 return ret;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100479}
480
481static int virtscsi_device_reset(struct scsi_cmnd *sc)
482{
483 struct virtio_scsi *vscsi = shost_priv(sc->device->host);
484 struct virtio_scsi_cmd *cmd;
485
486 sdev_printk(KERN_INFO, sc->device, "device reset\n");
487 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
488 if (!cmd)
489 return FAILED;
490
491 memset(cmd, 0, sizeof(*cmd));
492 cmd->sc = sc;
493 cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
494 .type = VIRTIO_SCSI_T_TMF,
495 .subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET,
496 .lun[0] = 1,
497 .lun[1] = sc->device->id,
498 .lun[2] = (sc->device->lun >> 8) | 0x40,
499 .lun[3] = sc->device->lun & 0xff,
500 };
501 return virtscsi_tmf(vscsi, cmd);
502}
503
504static int virtscsi_abort(struct scsi_cmnd *sc)
505{
506 struct virtio_scsi *vscsi = shost_priv(sc->device->host);
507 struct virtio_scsi_cmd *cmd;
508
509 scmd_printk(KERN_INFO, sc, "abort\n");
510 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
511 if (!cmd)
512 return FAILED;
513
514 memset(cmd, 0, sizeof(*cmd));
515 cmd->sc = sc;
516 cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
517 .type = VIRTIO_SCSI_T_TMF,
518 .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK,
519 .lun[0] = 1,
520 .lun[1] = sc->device->id,
521 .lun[2] = (sc->device->lun >> 8) | 0x40,
522 .lun[3] = sc->device->lun & 0xff,
523 .tag = (unsigned long)sc,
524 };
525 return virtscsi_tmf(vscsi, cmd);
526}
527
Wanlong Gao5c370192013-04-08 23:01:16 +0930528static int virtscsi_target_alloc(struct scsi_target *starget)
529{
530 struct virtio_scsi_target_state *tgt =
531 kmalloc(sizeof(*tgt), GFP_KERNEL);
532 if (!tgt)
533 return -ENOMEM;
534
535 spin_lock_init(&tgt->tgt_lock);
536
537 starget->hostdata = tgt;
538 return 0;
539}
540
541static void virtscsi_target_destroy(struct scsi_target *starget)
542{
543 struct virtio_scsi_target_state *tgt = starget->hostdata;
544 kfree(tgt);
545}
546
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100547static struct scsi_host_template virtscsi_host_template = {
548 .module = THIS_MODULE,
549 .name = "Virtio SCSI HBA",
550 .proc_name = "virtio_scsi",
551 .queuecommand = virtscsi_queuecommand,
552 .this_id = -1,
553 .eh_abort_handler = virtscsi_abort,
554 .eh_device_reset_handler = virtscsi_device_reset,
555
556 .can_queue = 1024,
557 .dma_boundary = UINT_MAX,
558 .use_clustering = ENABLE_CLUSTERING,
Wanlong Gao5c370192013-04-08 23:01:16 +0930559 .target_alloc = virtscsi_target_alloc,
560 .target_destroy = virtscsi_target_destroy,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100561};
562
563#define virtscsi_config_get(vdev, fld) \
564 ({ \
565 typeof(((struct virtio_scsi_config *)0)->fld) __val; \
566 vdev->config->get(vdev, \
567 offsetof(struct virtio_scsi_config, fld), \
568 &__val, sizeof(__val)); \
569 __val; \
570 })
571
572#define virtscsi_config_set(vdev, fld, val) \
573 (void)({ \
574 typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
575 vdev->config->set(vdev, \
576 offsetof(struct virtio_scsi_config, fld), \
577 &__val, sizeof(__val)); \
578 })
579
Paolo Bonzini139fe452012-06-13 16:56:32 +0200580static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq,
581 struct virtqueue *vq)
582{
583 spin_lock_init(&virtscsi_vq->vq_lock);
584 virtscsi_vq->vq = vq;
585}
586
Nicholas Bellinger59057fb2012-07-11 21:22:16 +0000587static void virtscsi_scan(struct virtio_device *vdev)
588{
589 struct Scsi_Host *shost = (struct Scsi_Host *)vdev->priv;
590
591 scsi_scan_host(shost);
592}
593
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200594static void virtscsi_remove_vqs(struct virtio_device *vdev)
595{
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200596 /* Stop all the virtqueues. */
597 vdev->config->reset(vdev);
598
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200599 vdev->config->del_vqs(vdev);
600}
601
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100602static int virtscsi_init(struct virtio_device *vdev,
Wanlong Gao5c370192013-04-08 23:01:16 +0930603 struct virtio_scsi *vscsi)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100604{
605 int err;
606 struct virtqueue *vqs[3];
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200607
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100608 vq_callback_t *callbacks[] = {
609 virtscsi_ctrl_done,
610 virtscsi_event_done,
611 virtscsi_req_done
612 };
613 const char *names[] = {
614 "control",
615 "event",
616 "request"
617 };
618
619 /* Discover virtqueues and write information to configuration. */
620 err = vdev->config->find_vqs(vdev, 3, vqs, callbacks, names);
621 if (err)
622 return err;
623
Paolo Bonzini139fe452012-06-13 16:56:32 +0200624 virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]);
625 virtscsi_init_vq(&vscsi->event_vq, vqs[1]);
626 virtscsi_init_vq(&vscsi->req_vq, vqs[2]);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100627
628 virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
629 virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200630
Cong Meng365a7152012-07-05 17:06:43 +0800631 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
632 virtscsi_kick_event_all(vscsi);
633
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200634 return err;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100635}
636
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800637static int virtscsi_probe(struct virtio_device *vdev)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100638{
639 struct Scsi_Host *shost;
640 struct virtio_scsi *vscsi;
641 int err;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200642 u32 sg_elems, num_targets;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100643 u32 cmd_per_lun;
644
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200645 num_targets = virtscsi_config_get(vdev, max_target) + 1;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100646
Wanlong Gao5c370192013-04-08 23:01:16 +0930647 shost = scsi_host_alloc(&virtscsi_host_template, sizeof(*vscsi));
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100648 if (!shost)
649 return -ENOMEM;
650
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200651 sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100652 shost->sg_tablesize = sg_elems;
653 vscsi = shost_priv(shost);
654 vscsi->vdev = vdev;
655 vdev->priv = shost;
656
Wanlong Gao5c370192013-04-08 23:01:16 +0930657 err = virtscsi_init(vdev, vscsi);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100658 if (err)
659 goto virtscsi_init_failed;
660
661 cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1;
662 shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue);
663 shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF;
Paolo Bonzini9da5f5a2012-10-02 17:25:47 +0200664
665 /* LUNs > 256 are reported with format 1, so they go in the range
666 * 16640-32767.
667 */
668 shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1 + 0x4000;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200669 shost->max_id = num_targets;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100670 shost->max_channel = 0;
671 shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE;
672 err = scsi_add_host(shost, &vdev->dev);
673 if (err)
674 goto scsi_add_host_failed;
Nicholas Bellinger59057fb2012-07-11 21:22:16 +0000675 /*
676 * scsi_scan_host() happens in virtscsi_scan() via virtio_driver->scan()
677 * after VIRTIO_CONFIG_S_DRIVER_OK has been set..
678 */
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100679 return 0;
680
681scsi_add_host_failed:
682 vdev->config->del_vqs(vdev);
683virtscsi_init_failed:
684 scsi_host_put(shost);
685 return err;
686}
687
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800688static void virtscsi_remove(struct virtio_device *vdev)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100689{
690 struct Scsi_Host *shost = virtio_scsi_host(vdev);
Cong Meng365a7152012-07-05 17:06:43 +0800691 struct virtio_scsi *vscsi = shost_priv(shost);
692
693 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
694 virtscsi_cancel_event_work(vscsi);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100695
696 scsi_remove_host(shost);
697
698 virtscsi_remove_vqs(vdev);
699 scsi_host_put(shost);
700}
701
702#ifdef CONFIG_PM
703static int virtscsi_freeze(struct virtio_device *vdev)
704{
705 virtscsi_remove_vqs(vdev);
706 return 0;
707}
708
709static int virtscsi_restore(struct virtio_device *vdev)
710{
711 struct Scsi_Host *sh = virtio_scsi_host(vdev);
712 struct virtio_scsi *vscsi = shost_priv(sh);
713
Wanlong Gao5c370192013-04-08 23:01:16 +0930714 return virtscsi_init(vdev, vscsi);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100715}
716#endif
717
718static struct virtio_device_id id_table[] = {
719 { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID },
720 { 0 },
721};
722
Cong Meng365a7152012-07-05 17:06:43 +0800723static unsigned int features[] = {
Paolo Bonzini865b58c2012-10-02 17:25:48 +0200724 VIRTIO_SCSI_F_HOTPLUG,
725 VIRTIO_SCSI_F_CHANGE,
Cong Meng365a7152012-07-05 17:06:43 +0800726};
727
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100728static struct virtio_driver virtio_scsi_driver = {
Cong Meng365a7152012-07-05 17:06:43 +0800729 .feature_table = features,
730 .feature_table_size = ARRAY_SIZE(features),
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100731 .driver.name = KBUILD_MODNAME,
732 .driver.owner = THIS_MODULE,
733 .id_table = id_table,
734 .probe = virtscsi_probe,
Nicholas Bellinger59057fb2012-07-11 21:22:16 +0000735 .scan = virtscsi_scan,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100736#ifdef CONFIG_PM
737 .freeze = virtscsi_freeze,
738 .restore = virtscsi_restore,
739#endif
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800740 .remove = virtscsi_remove,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100741};
742
743static int __init init(void)
744{
745 int ret = -ENOMEM;
746
747 virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0);
748 if (!virtscsi_cmd_cache) {
Wanlong Gaoba06d1e2013-03-12 15:34:40 +1030749 pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n");
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100750 goto error;
751 }
752
753
754 virtscsi_cmd_pool =
755 mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ,
756 virtscsi_cmd_cache);
757 if (!virtscsi_cmd_pool) {
Wanlong Gaoba06d1e2013-03-12 15:34:40 +1030758 pr_err("mempool_create() for virtscsi_cmd_pool failed\n");
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100759 goto error;
760 }
761 ret = register_virtio_driver(&virtio_scsi_driver);
762 if (ret < 0)
763 goto error;
764
765 return 0;
766
767error:
768 if (virtscsi_cmd_pool) {
769 mempool_destroy(virtscsi_cmd_pool);
770 virtscsi_cmd_pool = NULL;
771 }
772 if (virtscsi_cmd_cache) {
773 kmem_cache_destroy(virtscsi_cmd_cache);
774 virtscsi_cmd_cache = NULL;
775 }
776 return ret;
777}
778
779static void __exit fini(void)
780{
781 unregister_virtio_driver(&virtio_scsi_driver);
782 mempool_destroy(virtscsi_cmd_pool);
783 kmem_cache_destroy(virtscsi_cmd_cache);
784}
785module_init(init);
786module_exit(fini);
787
788MODULE_DEVICE_TABLE(virtio, id_table);
789MODULE_DESCRIPTION("Virtio SCSI HBA driver");
790MODULE_LICENSE("GPL");