blob: c23560c6a32e95dfb53eb3097f885d86d9de8e0d [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 Bonzini7f82b3c2013-04-08 23:01:38 +0930168static void virtscsi_vq_done(struct virtio_scsi *vscsi, struct virtqueue *vq,
169 void (*fn)(struct virtio_scsi *vscsi, void *buf))
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100170{
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100171 void *buf;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100172 unsigned int len;
173
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100174 do {
175 virtqueue_disable_cb(vq);
176 while ((buf = virtqueue_get_buf(vq, &len)) != NULL)
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930177 fn(vscsi, buf);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100178 } while (!virtqueue_enable_cb(vq));
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100179}
180
181static void virtscsi_req_done(struct virtqueue *vq)
182{
Paolo Bonzini139fe452012-06-13 16:56:32 +0200183 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
184 struct virtio_scsi *vscsi = shost_priv(sh);
185 unsigned long flags;
186
187 spin_lock_irqsave(&vscsi->req_vq.vq_lock, flags);
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930188 virtscsi_vq_done(vscsi, vq, virtscsi_complete_cmd);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200189 spin_unlock_irqrestore(&vscsi->req_vq.vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100190};
191
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930192static void virtscsi_complete_free(struct virtio_scsi *vscsi, void *buf)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100193{
194 struct virtio_scsi_cmd *cmd = buf;
195
196 if (cmd->comp)
197 complete_all(cmd->comp);
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200198 else
199 mempool_free(cmd, virtscsi_cmd_pool);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100200}
201
202static void virtscsi_ctrl_done(struct virtqueue *vq)
203{
Paolo Bonzini139fe452012-06-13 16:56:32 +0200204 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
205 struct virtio_scsi *vscsi = shost_priv(sh);
206 unsigned long flags;
207
208 spin_lock_irqsave(&vscsi->ctrl_vq.vq_lock, flags);
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930209 virtscsi_vq_done(vscsi, vq, virtscsi_complete_free);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200210 spin_unlock_irqrestore(&vscsi->ctrl_vq.vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100211};
212
Cong Meng365a7152012-07-05 17:06:43 +0800213static int virtscsi_kick_event(struct virtio_scsi *vscsi,
214 struct virtio_scsi_event_node *event_node)
215{
Rusty Russell4614e512012-10-16 23:56:16 +1030216 int err;
Cong Meng365a7152012-07-05 17:06:43 +0800217 struct scatterlist sg;
218 unsigned long flags;
219
Richard W.M. Jones2e9c9df2012-10-02 17:25:46 +0200220 sg_init_one(&sg, &event_node->event, sizeof(struct virtio_scsi_event));
Cong Meng365a7152012-07-05 17:06:43 +0800221
222 spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
223
Rusty Russellbf958292013-03-20 15:44:28 +1030224 err = virtqueue_add_inbuf(vscsi->event_vq.vq, &sg, 1, event_node,
225 GFP_ATOMIC);
Rusty Russell4614e512012-10-16 23:56:16 +1030226 if (!err)
Cong Meng365a7152012-07-05 17:06:43 +0800227 virtqueue_kick(vscsi->event_vq.vq);
228
229 spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
230
Rusty Russell4614e512012-10-16 23:56:16 +1030231 return err;
Cong Meng365a7152012-07-05 17:06:43 +0800232}
233
234static int virtscsi_kick_event_all(struct virtio_scsi *vscsi)
235{
236 int i;
237
238 for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) {
239 vscsi->event_list[i].vscsi = vscsi;
240 virtscsi_kick_event(vscsi, &vscsi->event_list[i]);
241 }
242
243 return 0;
244}
245
246static void virtscsi_cancel_event_work(struct virtio_scsi *vscsi)
247{
248 int i;
249
250 for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++)
251 cancel_work_sync(&vscsi->event_list[i].work);
252}
253
254static void virtscsi_handle_transport_reset(struct virtio_scsi *vscsi,
255 struct virtio_scsi_event *event)
256{
257 struct scsi_device *sdev;
258 struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
259 unsigned int target = event->lun[1];
260 unsigned int lun = (event->lun[2] << 8) | event->lun[3];
261
262 switch (event->reason) {
263 case VIRTIO_SCSI_EVT_RESET_RESCAN:
264 scsi_add_device(shost, 0, target, lun);
265 break;
266 case VIRTIO_SCSI_EVT_RESET_REMOVED:
267 sdev = scsi_device_lookup(shost, 0, target, lun);
268 if (sdev) {
269 scsi_remove_device(sdev);
270 scsi_device_put(sdev);
271 } else {
272 pr_err("SCSI device %d 0 %d %d not found\n",
273 shost->host_no, target, lun);
274 }
275 break;
276 default:
277 pr_info("Unsupport virtio scsi event reason %x\n", event->reason);
278 }
279}
280
Paolo Bonzini865b58c2012-10-02 17:25:48 +0200281static void virtscsi_handle_param_change(struct virtio_scsi *vscsi,
282 struct virtio_scsi_event *event)
283{
284 struct scsi_device *sdev;
285 struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
286 unsigned int target = event->lun[1];
287 unsigned int lun = (event->lun[2] << 8) | event->lun[3];
288 u8 asc = event->reason & 255;
289 u8 ascq = event->reason >> 8;
290
291 sdev = scsi_device_lookup(shost, 0, target, lun);
292 if (!sdev) {
293 pr_err("SCSI device %d 0 %d %d not found\n",
294 shost->host_no, target, lun);
295 return;
296 }
297
298 /* Handle "Parameters changed", "Mode parameters changed", and
299 "Capacity data has changed". */
300 if (asc == 0x2a && (ascq == 0x00 || ascq == 0x01 || ascq == 0x09))
301 scsi_rescan_device(&sdev->sdev_gendev);
302
303 scsi_device_put(sdev);
304}
305
Cong Meng365a7152012-07-05 17:06:43 +0800306static void virtscsi_handle_event(struct work_struct *work)
307{
308 struct virtio_scsi_event_node *event_node =
309 container_of(work, struct virtio_scsi_event_node, work);
310 struct virtio_scsi *vscsi = event_node->vscsi;
311 struct virtio_scsi_event *event = &event_node->event;
312
313 if (event->event & VIRTIO_SCSI_T_EVENTS_MISSED) {
314 event->event &= ~VIRTIO_SCSI_T_EVENTS_MISSED;
315 scsi_scan_host(virtio_scsi_host(vscsi->vdev));
316 }
317
318 switch (event->event) {
319 case VIRTIO_SCSI_T_NO_EVENT:
320 break;
321 case VIRTIO_SCSI_T_TRANSPORT_RESET:
322 virtscsi_handle_transport_reset(vscsi, event);
323 break;
Paolo Bonzini865b58c2012-10-02 17:25:48 +0200324 case VIRTIO_SCSI_T_PARAM_CHANGE:
325 virtscsi_handle_param_change(vscsi, event);
326 break;
Cong Meng365a7152012-07-05 17:06:43 +0800327 default:
328 pr_err("Unsupport virtio scsi event %x\n", event->event);
329 }
330 virtscsi_kick_event(vscsi, event_node);
331}
332
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930333static void virtscsi_complete_event(struct virtio_scsi *vscsi, void *buf)
Cong Meng365a7152012-07-05 17:06:43 +0800334{
335 struct virtio_scsi_event_node *event_node = buf;
336
337 INIT_WORK(&event_node->work, virtscsi_handle_event);
338 schedule_work(&event_node->work);
339}
340
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100341static void virtscsi_event_done(struct virtqueue *vq)
342{
Paolo Bonzini139fe452012-06-13 16:56:32 +0200343 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
344 struct virtio_scsi *vscsi = shost_priv(sh);
345 unsigned long flags;
346
347 spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930348 virtscsi_vq_done(vscsi, vq, virtscsi_complete_event);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200349 spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100350};
351
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100352/**
Wanlong Gao682993b2013-03-20 15:44:28 +1030353 * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue
354 * @vq : the struct virtqueue we're talking about
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100355 * @cmd : command structure
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100356 * @req_size : size of the request buffer
357 * @resp_size : size of the response buffer
Wanlong Gao682993b2013-03-20 15:44:28 +1030358 * @gfp : flags to use for memory allocations
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100359 */
Wanlong Gao682993b2013-03-20 15:44:28 +1030360static int virtscsi_add_cmd(struct virtqueue *vq,
361 struct virtio_scsi_cmd *cmd,
362 size_t req_size, size_t resp_size, gfp_t gfp)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100363{
364 struct scsi_cmnd *sc = cmd->sc;
Wanlong Gao682993b2013-03-20 15:44:28 +1030365 struct scatterlist *sgs[4], req, resp;
366 struct sg_table *out, *in;
367 unsigned out_num = 0, in_num = 0;
368
369 out = in = NULL;
370
371 if (sc && sc->sc_data_direction != DMA_NONE) {
372 if (sc->sc_data_direction != DMA_FROM_DEVICE)
373 out = &scsi_out(sc)->table;
374 if (sc->sc_data_direction != DMA_TO_DEVICE)
375 in = &scsi_in(sc)->table;
376 }
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100377
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100378 /* Request header. */
Wanlong Gao682993b2013-03-20 15:44:28 +1030379 sg_init_one(&req, &cmd->req, req_size);
380 sgs[out_num++] = &req;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100381
382 /* Data-out buffer. */
Wanlong Gao682993b2013-03-20 15:44:28 +1030383 if (out)
384 sgs[out_num++] = out->sgl;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100385
386 /* Response header. */
Wanlong Gao682993b2013-03-20 15:44:28 +1030387 sg_init_one(&resp, &cmd->resp, resp_size);
388 sgs[out_num + in_num++] = &resp;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100389
390 /* Data-in buffer */
Wanlong Gao682993b2013-03-20 15:44:28 +1030391 if (in)
392 sgs[out_num + in_num++] = in->sgl;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100393
Wanlong Gao682993b2013-03-20 15:44:28 +1030394 return virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, gfp);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100395}
396
Wanlong Gao682993b2013-03-20 15:44:28 +1030397static int virtscsi_kick_cmd(struct virtio_scsi_vq *vq,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100398 struct virtio_scsi_cmd *cmd,
399 size_t req_size, size_t resp_size, gfp_t gfp)
400{
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100401 unsigned long flags;
Rusty Russell4614e512012-10-16 23:56:16 +1030402 int err;
403 bool needs_kick = false;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100404
Wanlong Gao682993b2013-03-20 15:44:28 +1030405 spin_lock_irqsave(&vq->vq_lock, flags);
406 err = virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size, gfp);
Rusty Russell4614e512012-10-16 23:56:16 +1030407 if (!err)
408 needs_kick = virtqueue_kick_prepare(vq->vq);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100409
Paolo Bonzinibce750b2012-06-13 16:56:33 +0200410 spin_unlock_irqrestore(&vq->vq_lock, flags);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200411
Rusty Russell4614e512012-10-16 23:56:16 +1030412 if (needs_kick)
Paolo Bonzini139fe452012-06-13 16:56:32 +0200413 virtqueue_notify(vq->vq);
Rusty Russell4614e512012-10-16 23:56:16 +1030414 return err;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100415}
416
417static int virtscsi_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
418{
419 struct virtio_scsi *vscsi = shost_priv(sh);
420 struct virtio_scsi_cmd *cmd;
421 int ret;
422
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200423 struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
424 BUG_ON(scsi_sg_count(sc) > shost->sg_tablesize);
425
426 /* TODO: check feature bit and fail if unsupported? */
427 BUG_ON(sc->sc_data_direction == DMA_BIDIRECTIONAL);
428
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100429 dev_dbg(&sc->device->sdev_gendev,
430 "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]);
431
432 ret = SCSI_MLQUEUE_HOST_BUSY;
433 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_ATOMIC);
434 if (!cmd)
435 goto out;
436
437 memset(cmd, 0, sizeof(*cmd));
438 cmd->sc = sc;
439 cmd->req.cmd = (struct virtio_scsi_cmd_req){
440 .lun[0] = 1,
441 .lun[1] = sc->device->id,
442 .lun[2] = (sc->device->lun >> 8) | 0x40,
443 .lun[3] = sc->device->lun & 0xff,
444 .tag = (unsigned long)sc,
445 .task_attr = VIRTIO_SCSI_S_SIMPLE,
446 .prio = 0,
447 .crn = 0,
448 };
449
450 BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE);
451 memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len);
452
Wanlong Gao682993b2013-03-20 15:44:28 +1030453 if (virtscsi_kick_cmd(&vscsi->req_vq, cmd,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100454 sizeof cmd->req.cmd, sizeof cmd->resp.cmd,
Rusty Russell4614e512012-10-16 23:56:16 +1030455 GFP_ATOMIC) == 0)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100456 ret = 0;
Eric Northupb56d1002012-11-08 01:55:50 -0800457 else
458 mempool_free(cmd, virtscsi_cmd_pool);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100459
460out:
461 return ret;
462}
463
464static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd)
465{
466 DECLARE_COMPLETION_ONSTACK(comp);
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200467 int ret = FAILED;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100468
469 cmd->comp = &comp;
Wanlong Gao682993b2013-03-20 15:44:28 +1030470 if (virtscsi_kick_cmd(&vscsi->ctrl_vq, cmd,
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200471 sizeof cmd->req.tmf, sizeof cmd->resp.tmf,
472 GFP_NOIO) < 0)
473 goto out;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100474
475 wait_for_completion(&comp);
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200476 if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK ||
477 cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED)
478 ret = SUCCESS;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100479
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200480out:
481 mempool_free(cmd, virtscsi_cmd_pool);
482 return ret;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100483}
484
485static int virtscsi_device_reset(struct scsi_cmnd *sc)
486{
487 struct virtio_scsi *vscsi = shost_priv(sc->device->host);
488 struct virtio_scsi_cmd *cmd;
489
490 sdev_printk(KERN_INFO, sc->device, "device reset\n");
491 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
492 if (!cmd)
493 return FAILED;
494
495 memset(cmd, 0, sizeof(*cmd));
496 cmd->sc = sc;
497 cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
498 .type = VIRTIO_SCSI_T_TMF,
499 .subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET,
500 .lun[0] = 1,
501 .lun[1] = sc->device->id,
502 .lun[2] = (sc->device->lun >> 8) | 0x40,
503 .lun[3] = sc->device->lun & 0xff,
504 };
505 return virtscsi_tmf(vscsi, cmd);
506}
507
508static int virtscsi_abort(struct scsi_cmnd *sc)
509{
510 struct virtio_scsi *vscsi = shost_priv(sc->device->host);
511 struct virtio_scsi_cmd *cmd;
512
513 scmd_printk(KERN_INFO, sc, "abort\n");
514 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
515 if (!cmd)
516 return FAILED;
517
518 memset(cmd, 0, sizeof(*cmd));
519 cmd->sc = sc;
520 cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
521 .type = VIRTIO_SCSI_T_TMF,
522 .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK,
523 .lun[0] = 1,
524 .lun[1] = sc->device->id,
525 .lun[2] = (sc->device->lun >> 8) | 0x40,
526 .lun[3] = sc->device->lun & 0xff,
527 .tag = (unsigned long)sc,
528 };
529 return virtscsi_tmf(vscsi, cmd);
530}
531
Wanlong Gao5c370192013-04-08 23:01:16 +0930532static int virtscsi_target_alloc(struct scsi_target *starget)
533{
534 struct virtio_scsi_target_state *tgt =
535 kmalloc(sizeof(*tgt), GFP_KERNEL);
536 if (!tgt)
537 return -ENOMEM;
538
539 spin_lock_init(&tgt->tgt_lock);
540
541 starget->hostdata = tgt;
542 return 0;
543}
544
545static void virtscsi_target_destroy(struct scsi_target *starget)
546{
547 struct virtio_scsi_target_state *tgt = starget->hostdata;
548 kfree(tgt);
549}
550
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100551static struct scsi_host_template virtscsi_host_template = {
552 .module = THIS_MODULE,
553 .name = "Virtio SCSI HBA",
554 .proc_name = "virtio_scsi",
555 .queuecommand = virtscsi_queuecommand,
556 .this_id = -1,
557 .eh_abort_handler = virtscsi_abort,
558 .eh_device_reset_handler = virtscsi_device_reset,
559
560 .can_queue = 1024,
561 .dma_boundary = UINT_MAX,
562 .use_clustering = ENABLE_CLUSTERING,
Wanlong Gao5c370192013-04-08 23:01:16 +0930563 .target_alloc = virtscsi_target_alloc,
564 .target_destroy = virtscsi_target_destroy,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100565};
566
567#define virtscsi_config_get(vdev, fld) \
568 ({ \
569 typeof(((struct virtio_scsi_config *)0)->fld) __val; \
570 vdev->config->get(vdev, \
571 offsetof(struct virtio_scsi_config, fld), \
572 &__val, sizeof(__val)); \
573 __val; \
574 })
575
576#define virtscsi_config_set(vdev, fld, val) \
577 (void)({ \
578 typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
579 vdev->config->set(vdev, \
580 offsetof(struct virtio_scsi_config, fld), \
581 &__val, sizeof(__val)); \
582 })
583
Paolo Bonzini139fe452012-06-13 16:56:32 +0200584static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq,
585 struct virtqueue *vq)
586{
587 spin_lock_init(&virtscsi_vq->vq_lock);
588 virtscsi_vq->vq = vq;
589}
590
Nicholas Bellinger59057fb2012-07-11 21:22:16 +0000591static void virtscsi_scan(struct virtio_device *vdev)
592{
593 struct Scsi_Host *shost = (struct Scsi_Host *)vdev->priv;
594
595 scsi_scan_host(shost);
596}
597
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200598static void virtscsi_remove_vqs(struct virtio_device *vdev)
599{
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200600 /* Stop all the virtqueues. */
601 vdev->config->reset(vdev);
602
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200603 vdev->config->del_vqs(vdev);
604}
605
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100606static int virtscsi_init(struct virtio_device *vdev,
Wanlong Gao5c370192013-04-08 23:01:16 +0930607 struct virtio_scsi *vscsi)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100608{
609 int err;
610 struct virtqueue *vqs[3];
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200611
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100612 vq_callback_t *callbacks[] = {
613 virtscsi_ctrl_done,
614 virtscsi_event_done,
615 virtscsi_req_done
616 };
617 const char *names[] = {
618 "control",
619 "event",
620 "request"
621 };
622
623 /* Discover virtqueues and write information to configuration. */
624 err = vdev->config->find_vqs(vdev, 3, vqs, callbacks, names);
625 if (err)
626 return err;
627
Paolo Bonzini139fe452012-06-13 16:56:32 +0200628 virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]);
629 virtscsi_init_vq(&vscsi->event_vq, vqs[1]);
630 virtscsi_init_vq(&vscsi->req_vq, vqs[2]);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100631
632 virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
633 virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200634
Cong Meng365a7152012-07-05 17:06:43 +0800635 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
636 virtscsi_kick_event_all(vscsi);
637
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200638 return err;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100639}
640
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800641static int virtscsi_probe(struct virtio_device *vdev)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100642{
643 struct Scsi_Host *shost;
644 struct virtio_scsi *vscsi;
645 int err;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200646 u32 sg_elems, num_targets;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100647 u32 cmd_per_lun;
648
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200649 num_targets = virtscsi_config_get(vdev, max_target) + 1;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100650
Wanlong Gao5c370192013-04-08 23:01:16 +0930651 shost = scsi_host_alloc(&virtscsi_host_template, sizeof(*vscsi));
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100652 if (!shost)
653 return -ENOMEM;
654
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200655 sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100656 shost->sg_tablesize = sg_elems;
657 vscsi = shost_priv(shost);
658 vscsi->vdev = vdev;
659 vdev->priv = shost;
660
Wanlong Gao5c370192013-04-08 23:01:16 +0930661 err = virtscsi_init(vdev, vscsi);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100662 if (err)
663 goto virtscsi_init_failed;
664
665 cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1;
666 shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue);
667 shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF;
Paolo Bonzini9da5f5a2012-10-02 17:25:47 +0200668
669 /* LUNs > 256 are reported with format 1, so they go in the range
670 * 16640-32767.
671 */
672 shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1 + 0x4000;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200673 shost->max_id = num_targets;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100674 shost->max_channel = 0;
675 shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE;
676 err = scsi_add_host(shost, &vdev->dev);
677 if (err)
678 goto scsi_add_host_failed;
Nicholas Bellinger59057fb2012-07-11 21:22:16 +0000679 /*
680 * scsi_scan_host() happens in virtscsi_scan() via virtio_driver->scan()
681 * after VIRTIO_CONFIG_S_DRIVER_OK has been set..
682 */
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100683 return 0;
684
685scsi_add_host_failed:
686 vdev->config->del_vqs(vdev);
687virtscsi_init_failed:
688 scsi_host_put(shost);
689 return err;
690}
691
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800692static void virtscsi_remove(struct virtio_device *vdev)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100693{
694 struct Scsi_Host *shost = virtio_scsi_host(vdev);
Cong Meng365a7152012-07-05 17:06:43 +0800695 struct virtio_scsi *vscsi = shost_priv(shost);
696
697 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
698 virtscsi_cancel_event_work(vscsi);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100699
700 scsi_remove_host(shost);
701
702 virtscsi_remove_vqs(vdev);
703 scsi_host_put(shost);
704}
705
706#ifdef CONFIG_PM
707static int virtscsi_freeze(struct virtio_device *vdev)
708{
709 virtscsi_remove_vqs(vdev);
710 return 0;
711}
712
713static int virtscsi_restore(struct virtio_device *vdev)
714{
715 struct Scsi_Host *sh = virtio_scsi_host(vdev);
716 struct virtio_scsi *vscsi = shost_priv(sh);
717
Wanlong Gao5c370192013-04-08 23:01:16 +0930718 return virtscsi_init(vdev, vscsi);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100719}
720#endif
721
722static struct virtio_device_id id_table[] = {
723 { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID },
724 { 0 },
725};
726
Cong Meng365a7152012-07-05 17:06:43 +0800727static unsigned int features[] = {
Paolo Bonzini865b58c2012-10-02 17:25:48 +0200728 VIRTIO_SCSI_F_HOTPLUG,
729 VIRTIO_SCSI_F_CHANGE,
Cong Meng365a7152012-07-05 17:06:43 +0800730};
731
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100732static struct virtio_driver virtio_scsi_driver = {
Cong Meng365a7152012-07-05 17:06:43 +0800733 .feature_table = features,
734 .feature_table_size = ARRAY_SIZE(features),
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100735 .driver.name = KBUILD_MODNAME,
736 .driver.owner = THIS_MODULE,
737 .id_table = id_table,
738 .probe = virtscsi_probe,
Nicholas Bellinger59057fb2012-07-11 21:22:16 +0000739 .scan = virtscsi_scan,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100740#ifdef CONFIG_PM
741 .freeze = virtscsi_freeze,
742 .restore = virtscsi_restore,
743#endif
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800744 .remove = virtscsi_remove,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100745};
746
747static int __init init(void)
748{
749 int ret = -ENOMEM;
750
751 virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0);
752 if (!virtscsi_cmd_cache) {
Wanlong Gaoba06d1e2013-03-12 15:34:40 +1030753 pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n");
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100754 goto error;
755 }
756
757
758 virtscsi_cmd_pool =
759 mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ,
760 virtscsi_cmd_cache);
761 if (!virtscsi_cmd_pool) {
Wanlong Gaoba06d1e2013-03-12 15:34:40 +1030762 pr_err("mempool_create() for virtscsi_cmd_pool failed\n");
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100763 goto error;
764 }
765 ret = register_virtio_driver(&virtio_scsi_driver);
766 if (ret < 0)
767 goto error;
768
769 return 0;
770
771error:
772 if (virtscsi_cmd_pool) {
773 mempool_destroy(virtscsi_cmd_pool);
774 virtscsi_cmd_pool = NULL;
775 }
776 if (virtscsi_cmd_cache) {
777 kmem_cache_destroy(virtscsi_cmd_cache);
778 virtscsi_cmd_cache = NULL;
779 }
780 return ret;
781}
782
783static void __exit fini(void)
784{
785 unregister_virtio_driver(&virtio_scsi_driver);
786 mempool_destroy(virtscsi_cmd_pool);
787 kmem_cache_destroy(virtscsi_cmd_cache);
788}
789module_init(init);
790module_exit(fini);
791
792MODULE_DEVICE_TABLE(virtio, id_table);
793MODULE_DESCRIPTION("Virtio SCSI HBA driver");
794MODULE_LICENSE("GPL");