blob: ffa03e8cb19c5a4aff96a29522d96611ac4598cc [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 */
107static void virtscsi_complete_cmd(void *buf)
108{
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
168static void virtscsi_vq_done(struct virtqueue *vq, void (*fn)(void *buf))
169{
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100170 void *buf;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100171 unsigned int len;
172
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100173 do {
174 virtqueue_disable_cb(vq);
175 while ((buf = virtqueue_get_buf(vq, &len)) != NULL)
176 fn(buf);
177 } while (!virtqueue_enable_cb(vq));
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100178}
179
180static void virtscsi_req_done(struct virtqueue *vq)
181{
Paolo Bonzini139fe452012-06-13 16:56:32 +0200182 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
183 struct virtio_scsi *vscsi = shost_priv(sh);
184 unsigned long flags;
185
186 spin_lock_irqsave(&vscsi->req_vq.vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100187 virtscsi_vq_done(vq, virtscsi_complete_cmd);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200188 spin_unlock_irqrestore(&vscsi->req_vq.vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100189};
190
191static void virtscsi_complete_free(void *buf)
192{
193 struct virtio_scsi_cmd *cmd = buf;
194
195 if (cmd->comp)
196 complete_all(cmd->comp);
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200197 else
198 mempool_free(cmd, virtscsi_cmd_pool);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100199}
200
201static void virtscsi_ctrl_done(struct virtqueue *vq)
202{
Paolo Bonzini139fe452012-06-13 16:56:32 +0200203 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
204 struct virtio_scsi *vscsi = shost_priv(sh);
205 unsigned long flags;
206
207 spin_lock_irqsave(&vscsi->ctrl_vq.vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100208 virtscsi_vq_done(vq, virtscsi_complete_free);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200209 spin_unlock_irqrestore(&vscsi->ctrl_vq.vq_lock, flags);
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
332static void virtscsi_complete_event(void *buf)
333{
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);
344 unsigned long flags;
345
346 spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
Cong Meng365a7152012-07-05 17:06:43 +0800347 virtscsi_vq_done(vq, virtscsi_complete_event);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200348 spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100349};
350
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100351/**
Wanlong Gao682993b2013-03-20 15:44:28 +1030352 * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue
353 * @vq : the struct virtqueue we're talking about
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100354 * @cmd : command structure
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100355 * @req_size : size of the request buffer
356 * @resp_size : size of the response buffer
Wanlong Gao682993b2013-03-20 15:44:28 +1030357 * @gfp : flags to use for memory allocations
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100358 */
Wanlong Gao682993b2013-03-20 15:44:28 +1030359static int virtscsi_add_cmd(struct virtqueue *vq,
360 struct virtio_scsi_cmd *cmd,
361 size_t req_size, size_t resp_size, gfp_t gfp)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100362{
363 struct scsi_cmnd *sc = cmd->sc;
Wanlong Gao682993b2013-03-20 15:44:28 +1030364 struct scatterlist *sgs[4], req, resp;
365 struct sg_table *out, *in;
366 unsigned out_num = 0, in_num = 0;
367
368 out = in = NULL;
369
370 if (sc && sc->sc_data_direction != DMA_NONE) {
371 if (sc->sc_data_direction != DMA_FROM_DEVICE)
372 out = &scsi_out(sc)->table;
373 if (sc->sc_data_direction != DMA_TO_DEVICE)
374 in = &scsi_in(sc)->table;
375 }
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100376
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100377 /* Request header. */
Wanlong Gao682993b2013-03-20 15:44:28 +1030378 sg_init_one(&req, &cmd->req, req_size);
379 sgs[out_num++] = &req;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100380
381 /* Data-out buffer. */
Wanlong Gao682993b2013-03-20 15:44:28 +1030382 if (out)
383 sgs[out_num++] = out->sgl;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100384
385 /* Response header. */
Wanlong Gao682993b2013-03-20 15:44:28 +1030386 sg_init_one(&resp, &cmd->resp, resp_size);
387 sgs[out_num + in_num++] = &resp;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100388
389 /* Data-in buffer */
Wanlong Gao682993b2013-03-20 15:44:28 +1030390 if (in)
391 sgs[out_num + in_num++] = in->sgl;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100392
Wanlong Gao682993b2013-03-20 15:44:28 +1030393 return virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, gfp);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100394}
395
Wanlong Gao682993b2013-03-20 15:44:28 +1030396static int virtscsi_kick_cmd(struct virtio_scsi_vq *vq,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100397 struct virtio_scsi_cmd *cmd,
398 size_t req_size, size_t resp_size, gfp_t gfp)
399{
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100400 unsigned long flags;
Rusty Russell4614e512012-10-16 23:56:16 +1030401 int err;
402 bool needs_kick = false;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100403
Wanlong Gao682993b2013-03-20 15:44:28 +1030404 spin_lock_irqsave(&vq->vq_lock, flags);
405 err = virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size, gfp);
Rusty Russell4614e512012-10-16 23:56:16 +1030406 if (!err)
407 needs_kick = virtqueue_kick_prepare(vq->vq);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100408
Paolo Bonzinibce750b2012-06-13 16:56:33 +0200409 spin_unlock_irqrestore(&vq->vq_lock, flags);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200410
Rusty Russell4614e512012-10-16 23:56:16 +1030411 if (needs_kick)
Paolo Bonzini139fe452012-06-13 16:56:32 +0200412 virtqueue_notify(vq->vq);
Rusty Russell4614e512012-10-16 23:56:16 +1030413 return err;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100414}
415
416static int virtscsi_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
417{
418 struct virtio_scsi *vscsi = shost_priv(sh);
419 struct virtio_scsi_cmd *cmd;
420 int ret;
421
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200422 struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
423 BUG_ON(scsi_sg_count(sc) > shost->sg_tablesize);
424
425 /* TODO: check feature bit and fail if unsupported? */
426 BUG_ON(sc->sc_data_direction == DMA_BIDIRECTIONAL);
427
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100428 dev_dbg(&sc->device->sdev_gendev,
429 "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]);
430
431 ret = SCSI_MLQUEUE_HOST_BUSY;
432 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_ATOMIC);
433 if (!cmd)
434 goto out;
435
436 memset(cmd, 0, sizeof(*cmd));
437 cmd->sc = sc;
438 cmd->req.cmd = (struct virtio_scsi_cmd_req){
439 .lun[0] = 1,
440 .lun[1] = sc->device->id,
441 .lun[2] = (sc->device->lun >> 8) | 0x40,
442 .lun[3] = sc->device->lun & 0xff,
443 .tag = (unsigned long)sc,
444 .task_attr = VIRTIO_SCSI_S_SIMPLE,
445 .prio = 0,
446 .crn = 0,
447 };
448
449 BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE);
450 memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len);
451
Wanlong Gao682993b2013-03-20 15:44:28 +1030452 if (virtscsi_kick_cmd(&vscsi->req_vq, cmd,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100453 sizeof cmd->req.cmd, sizeof cmd->resp.cmd,
Rusty Russell4614e512012-10-16 23:56:16 +1030454 GFP_ATOMIC) == 0)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100455 ret = 0;
Eric Northupb56d1002012-11-08 01:55:50 -0800456 else
457 mempool_free(cmd, virtscsi_cmd_pool);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100458
459out:
460 return ret;
461}
462
463static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd)
464{
465 DECLARE_COMPLETION_ONSTACK(comp);
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200466 int ret = FAILED;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100467
468 cmd->comp = &comp;
Wanlong Gao682993b2013-03-20 15:44:28 +1030469 if (virtscsi_kick_cmd(&vscsi->ctrl_vq, cmd,
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200470 sizeof cmd->req.tmf, sizeof cmd->resp.tmf,
471 GFP_NOIO) < 0)
472 goto out;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100473
474 wait_for_completion(&comp);
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200475 if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK ||
476 cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED)
477 ret = SUCCESS;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100478
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200479out:
480 mempool_free(cmd, virtscsi_cmd_pool);
481 return ret;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100482}
483
484static int virtscsi_device_reset(struct scsi_cmnd *sc)
485{
486 struct virtio_scsi *vscsi = shost_priv(sc->device->host);
487 struct virtio_scsi_cmd *cmd;
488
489 sdev_printk(KERN_INFO, sc->device, "device reset\n");
490 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
491 if (!cmd)
492 return FAILED;
493
494 memset(cmd, 0, sizeof(*cmd));
495 cmd->sc = sc;
496 cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
497 .type = VIRTIO_SCSI_T_TMF,
498 .subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET,
499 .lun[0] = 1,
500 .lun[1] = sc->device->id,
501 .lun[2] = (sc->device->lun >> 8) | 0x40,
502 .lun[3] = sc->device->lun & 0xff,
503 };
504 return virtscsi_tmf(vscsi, cmd);
505}
506
507static int virtscsi_abort(struct scsi_cmnd *sc)
508{
509 struct virtio_scsi *vscsi = shost_priv(sc->device->host);
510 struct virtio_scsi_cmd *cmd;
511
512 scmd_printk(KERN_INFO, sc, "abort\n");
513 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
514 if (!cmd)
515 return FAILED;
516
517 memset(cmd, 0, sizeof(*cmd));
518 cmd->sc = sc;
519 cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
520 .type = VIRTIO_SCSI_T_TMF,
521 .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK,
522 .lun[0] = 1,
523 .lun[1] = sc->device->id,
524 .lun[2] = (sc->device->lun >> 8) | 0x40,
525 .lun[3] = sc->device->lun & 0xff,
526 .tag = (unsigned long)sc,
527 };
528 return virtscsi_tmf(vscsi, cmd);
529}
530
Wanlong Gao5c370192013-04-08 23:01:16 +0930531static int virtscsi_target_alloc(struct scsi_target *starget)
532{
533 struct virtio_scsi_target_state *tgt =
534 kmalloc(sizeof(*tgt), GFP_KERNEL);
535 if (!tgt)
536 return -ENOMEM;
537
538 spin_lock_init(&tgt->tgt_lock);
539
540 starget->hostdata = tgt;
541 return 0;
542}
543
544static void virtscsi_target_destroy(struct scsi_target *starget)
545{
546 struct virtio_scsi_target_state *tgt = starget->hostdata;
547 kfree(tgt);
548}
549
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100550static struct scsi_host_template virtscsi_host_template = {
551 .module = THIS_MODULE,
552 .name = "Virtio SCSI HBA",
553 .proc_name = "virtio_scsi",
554 .queuecommand = virtscsi_queuecommand,
555 .this_id = -1,
556 .eh_abort_handler = virtscsi_abort,
557 .eh_device_reset_handler = virtscsi_device_reset,
558
559 .can_queue = 1024,
560 .dma_boundary = UINT_MAX,
561 .use_clustering = ENABLE_CLUSTERING,
Wanlong Gao5c370192013-04-08 23:01:16 +0930562 .target_alloc = virtscsi_target_alloc,
563 .target_destroy = virtscsi_target_destroy,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100564};
565
566#define virtscsi_config_get(vdev, fld) \
567 ({ \
568 typeof(((struct virtio_scsi_config *)0)->fld) __val; \
569 vdev->config->get(vdev, \
570 offsetof(struct virtio_scsi_config, fld), \
571 &__val, sizeof(__val)); \
572 __val; \
573 })
574
575#define virtscsi_config_set(vdev, fld, val) \
576 (void)({ \
577 typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
578 vdev->config->set(vdev, \
579 offsetof(struct virtio_scsi_config, fld), \
580 &__val, sizeof(__val)); \
581 })
582
Paolo Bonzini139fe452012-06-13 16:56:32 +0200583static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq,
584 struct virtqueue *vq)
585{
586 spin_lock_init(&virtscsi_vq->vq_lock);
587 virtscsi_vq->vq = vq;
588}
589
Nicholas Bellinger59057fb2012-07-11 21:22:16 +0000590static void virtscsi_scan(struct virtio_device *vdev)
591{
592 struct Scsi_Host *shost = (struct Scsi_Host *)vdev->priv;
593
594 scsi_scan_host(shost);
595}
596
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200597static void virtscsi_remove_vqs(struct virtio_device *vdev)
598{
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200599 /* Stop all the virtqueues. */
600 vdev->config->reset(vdev);
601
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200602 vdev->config->del_vqs(vdev);
603}
604
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100605static int virtscsi_init(struct virtio_device *vdev,
Wanlong Gao5c370192013-04-08 23:01:16 +0930606 struct virtio_scsi *vscsi)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100607{
608 int err;
609 struct virtqueue *vqs[3];
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200610
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100611 vq_callback_t *callbacks[] = {
612 virtscsi_ctrl_done,
613 virtscsi_event_done,
614 virtscsi_req_done
615 };
616 const char *names[] = {
617 "control",
618 "event",
619 "request"
620 };
621
622 /* Discover virtqueues and write information to configuration. */
623 err = vdev->config->find_vqs(vdev, 3, vqs, callbacks, names);
624 if (err)
625 return err;
626
Paolo Bonzini139fe452012-06-13 16:56:32 +0200627 virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]);
628 virtscsi_init_vq(&vscsi->event_vq, vqs[1]);
629 virtscsi_init_vq(&vscsi->req_vq, vqs[2]);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100630
631 virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
632 virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200633
Cong Meng365a7152012-07-05 17:06:43 +0800634 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
635 virtscsi_kick_event_all(vscsi);
636
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200637 return err;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100638}
639
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800640static int virtscsi_probe(struct virtio_device *vdev)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100641{
642 struct Scsi_Host *shost;
643 struct virtio_scsi *vscsi;
644 int err;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200645 u32 sg_elems, num_targets;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100646 u32 cmd_per_lun;
647
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200648 num_targets = virtscsi_config_get(vdev, max_target) + 1;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100649
Wanlong Gao5c370192013-04-08 23:01:16 +0930650 shost = scsi_host_alloc(&virtscsi_host_template, sizeof(*vscsi));
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100651 if (!shost)
652 return -ENOMEM;
653
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200654 sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100655 shost->sg_tablesize = sg_elems;
656 vscsi = shost_priv(shost);
657 vscsi->vdev = vdev;
658 vdev->priv = shost;
659
Wanlong Gao5c370192013-04-08 23:01:16 +0930660 err = virtscsi_init(vdev, vscsi);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100661 if (err)
662 goto virtscsi_init_failed;
663
664 cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1;
665 shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue);
666 shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF;
Paolo Bonzini9da5f5a2012-10-02 17:25:47 +0200667
668 /* LUNs > 256 are reported with format 1, so they go in the range
669 * 16640-32767.
670 */
671 shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1 + 0x4000;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200672 shost->max_id = num_targets;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100673 shost->max_channel = 0;
674 shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE;
675 err = scsi_add_host(shost, &vdev->dev);
676 if (err)
677 goto scsi_add_host_failed;
Nicholas Bellinger59057fb2012-07-11 21:22:16 +0000678 /*
679 * scsi_scan_host() happens in virtscsi_scan() via virtio_driver->scan()
680 * after VIRTIO_CONFIG_S_DRIVER_OK has been set..
681 */
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100682 return 0;
683
684scsi_add_host_failed:
685 vdev->config->del_vqs(vdev);
686virtscsi_init_failed:
687 scsi_host_put(shost);
688 return err;
689}
690
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800691static void virtscsi_remove(struct virtio_device *vdev)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100692{
693 struct Scsi_Host *shost = virtio_scsi_host(vdev);
Cong Meng365a7152012-07-05 17:06:43 +0800694 struct virtio_scsi *vscsi = shost_priv(shost);
695
696 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
697 virtscsi_cancel_event_work(vscsi);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100698
699 scsi_remove_host(shost);
700
701 virtscsi_remove_vqs(vdev);
702 scsi_host_put(shost);
703}
704
705#ifdef CONFIG_PM
706static int virtscsi_freeze(struct virtio_device *vdev)
707{
708 virtscsi_remove_vqs(vdev);
709 return 0;
710}
711
712static int virtscsi_restore(struct virtio_device *vdev)
713{
714 struct Scsi_Host *sh = virtio_scsi_host(vdev);
715 struct virtio_scsi *vscsi = shost_priv(sh);
716
Wanlong Gao5c370192013-04-08 23:01:16 +0930717 return virtscsi_init(vdev, vscsi);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100718}
719#endif
720
721static struct virtio_device_id id_table[] = {
722 { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID },
723 { 0 },
724};
725
Cong Meng365a7152012-07-05 17:06:43 +0800726static unsigned int features[] = {
Paolo Bonzini865b58c2012-10-02 17:25:48 +0200727 VIRTIO_SCSI_F_HOTPLUG,
728 VIRTIO_SCSI_F_CHANGE,
Cong Meng365a7152012-07-05 17:06:43 +0800729};
730
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100731static struct virtio_driver virtio_scsi_driver = {
Cong Meng365a7152012-07-05 17:06:43 +0800732 .feature_table = features,
733 .feature_table_size = ARRAY_SIZE(features),
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100734 .driver.name = KBUILD_MODNAME,
735 .driver.owner = THIS_MODULE,
736 .id_table = id_table,
737 .probe = virtscsi_probe,
Nicholas Bellinger59057fb2012-07-11 21:22:16 +0000738 .scan = virtscsi_scan,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100739#ifdef CONFIG_PM
740 .freeze = virtscsi_freeze,
741 .restore = virtscsi_restore,
742#endif
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800743 .remove = virtscsi_remove,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100744};
745
746static int __init init(void)
747{
748 int ret = -ENOMEM;
749
750 virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0);
751 if (!virtscsi_cmd_cache) {
Wanlong Gaoba06d1e2013-03-12 15:34:40 +1030752 pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n");
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100753 goto error;
754 }
755
756
757 virtscsi_cmd_pool =
758 mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ,
759 virtscsi_cmd_cache);
760 if (!virtscsi_cmd_pool) {
Wanlong Gaoba06d1e2013-03-12 15:34:40 +1030761 pr_err("mempool_create() for virtscsi_cmd_pool failed\n");
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100762 goto error;
763 }
764 ret = register_virtio_driver(&virtio_scsi_driver);
765 if (ret < 0)
766 goto error;
767
768 return 0;
769
770error:
771 if (virtscsi_cmd_pool) {
772 mempool_destroy(virtscsi_cmd_pool);
773 virtscsi_cmd_pool = NULL;
774 }
775 if (virtscsi_cmd_cache) {
776 kmem_cache_destroy(virtscsi_cmd_cache);
777 virtscsi_cmd_cache = NULL;
778 }
779 return ret;
780}
781
782static void __exit fini(void)
783{
784 unregister_virtio_driver(&virtio_scsi_driver);
785 mempool_destroy(virtscsi_cmd_pool);
786 kmem_cache_destroy(virtscsi_cmd_cache);
787}
788module_init(init);
789module_exit(fini);
790
791MODULE_DEVICE_TABLE(virtio, id_table);
792MODULE_DESCRIPTION("Virtio SCSI HBA driver");
793MODULE_LICENSE("GPL");