blob: 77206d0eb6a908143f3dcb9ec51e7251699be265 [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];
78
Paolo Bonzini2bd37f02012-06-13 16:56:34 +020079 struct virtio_scsi_target_state *tgt[];
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010080};
81
82static struct kmem_cache *virtscsi_cmd_cache;
83static mempool_t *virtscsi_cmd_pool;
84
85static inline struct Scsi_Host *virtio_scsi_host(struct virtio_device *vdev)
86{
87 return vdev->priv;
88}
89
90static void virtscsi_compute_resid(struct scsi_cmnd *sc, u32 resid)
91{
92 if (!resid)
93 return;
94
95 if (!scsi_bidi_cmnd(sc)) {
96 scsi_set_resid(sc, resid);
97 return;
98 }
99
100 scsi_in(sc)->resid = min(resid, scsi_in(sc)->length);
101 scsi_out(sc)->resid = resid - scsi_in(sc)->resid;
102}
103
104/**
105 * virtscsi_complete_cmd - finish a scsi_cmd and invoke scsi_done
106 *
107 * Called with vq_lock held.
108 */
109static void virtscsi_complete_cmd(void *buf)
110{
111 struct virtio_scsi_cmd *cmd = buf;
112 struct scsi_cmnd *sc = cmd->sc;
113 struct virtio_scsi_cmd_resp *resp = &cmd->resp.cmd;
114
115 dev_dbg(&sc->device->sdev_gendev,
116 "cmd %p response %u status %#02x sense_len %u\n",
117 sc, resp->response, resp->status, resp->sense_len);
118
119 sc->result = resp->status;
120 virtscsi_compute_resid(sc, resp->resid);
121 switch (resp->response) {
122 case VIRTIO_SCSI_S_OK:
123 set_host_byte(sc, DID_OK);
124 break;
125 case VIRTIO_SCSI_S_OVERRUN:
126 set_host_byte(sc, DID_ERROR);
127 break;
128 case VIRTIO_SCSI_S_ABORTED:
129 set_host_byte(sc, DID_ABORT);
130 break;
131 case VIRTIO_SCSI_S_BAD_TARGET:
132 set_host_byte(sc, DID_BAD_TARGET);
133 break;
134 case VIRTIO_SCSI_S_RESET:
135 set_host_byte(sc, DID_RESET);
136 break;
137 case VIRTIO_SCSI_S_BUSY:
138 set_host_byte(sc, DID_BUS_BUSY);
139 break;
140 case VIRTIO_SCSI_S_TRANSPORT_FAILURE:
141 set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
142 break;
143 case VIRTIO_SCSI_S_TARGET_FAILURE:
144 set_host_byte(sc, DID_TARGET_FAILURE);
145 break;
146 case VIRTIO_SCSI_S_NEXUS_FAILURE:
147 set_host_byte(sc, DID_NEXUS_FAILURE);
148 break;
149 default:
150 scmd_printk(KERN_WARNING, sc, "Unknown response %d",
151 resp->response);
152 /* fall through */
153 case VIRTIO_SCSI_S_FAILURE:
154 set_host_byte(sc, DID_ERROR);
155 break;
156 }
157
158 WARN_ON(resp->sense_len > VIRTIO_SCSI_SENSE_SIZE);
159 if (sc->sense_buffer) {
160 memcpy(sc->sense_buffer, resp->sense,
161 min_t(u32, resp->sense_len, VIRTIO_SCSI_SENSE_SIZE));
162 if (resp->sense_len)
163 set_driver_byte(sc, DRIVER_SENSE);
164 }
165
166 mempool_free(cmd, virtscsi_cmd_pool);
167 sc->scsi_done(sc);
168}
169
170static void virtscsi_vq_done(struct virtqueue *vq, void (*fn)(void *buf))
171{
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100172 void *buf;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100173 unsigned int len;
174
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100175 do {
176 virtqueue_disable_cb(vq);
177 while ((buf = virtqueue_get_buf(vq, &len)) != NULL)
178 fn(buf);
179 } while (!virtqueue_enable_cb(vq));
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100180}
181
182static void virtscsi_req_done(struct virtqueue *vq)
183{
Paolo Bonzini139fe452012-06-13 16:56:32 +0200184 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
185 struct virtio_scsi *vscsi = shost_priv(sh);
186 unsigned long flags;
187
188 spin_lock_irqsave(&vscsi->req_vq.vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100189 virtscsi_vq_done(vq, virtscsi_complete_cmd);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200190 spin_unlock_irqrestore(&vscsi->req_vq.vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100191};
192
193static void virtscsi_complete_free(void *buf)
194{
195 struct virtio_scsi_cmd *cmd = buf;
196
197 if (cmd->comp)
198 complete_all(cmd->comp);
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200199 else
200 mempool_free(cmd, virtscsi_cmd_pool);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100201}
202
203static void virtscsi_ctrl_done(struct virtqueue *vq)
204{
Paolo Bonzini139fe452012-06-13 16:56:32 +0200205 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
206 struct virtio_scsi *vscsi = shost_priv(sh);
207 unsigned long flags;
208
209 spin_lock_irqsave(&vscsi->ctrl_vq.vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100210 virtscsi_vq_done(vq, virtscsi_complete_free);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200211 spin_unlock_irqrestore(&vscsi->ctrl_vq.vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100212};
213
Cong Meng365a7152012-07-05 17:06:43 +0800214static int virtscsi_kick_event(struct virtio_scsi *vscsi,
215 struct virtio_scsi_event_node *event_node)
216{
Rusty Russell4614e512012-10-16 23:56:16 +1030217 int err;
Cong Meng365a7152012-07-05 17:06:43 +0800218 struct scatterlist sg;
219 unsigned long flags;
220
Richard W.M. Jones2e9c9df2012-10-02 17:25:46 +0200221 sg_init_one(&sg, &event_node->event, sizeof(struct virtio_scsi_event));
Cong Meng365a7152012-07-05 17:06:43 +0800222
223 spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
224
Rusty Russell4614e512012-10-16 23:56:16 +1030225 err = virtqueue_add_buf(vscsi->event_vq.vq, &sg, 0, 1, event_node,
226 GFP_ATOMIC);
227 if (!err)
Cong Meng365a7152012-07-05 17:06:43 +0800228 virtqueue_kick(vscsi->event_vq.vq);
229
230 spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
231
Rusty Russell4614e512012-10-16 23:56:16 +1030232 return err;
Cong Meng365a7152012-07-05 17:06:43 +0800233}
234
235static int virtscsi_kick_event_all(struct virtio_scsi *vscsi)
236{
237 int i;
238
239 for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) {
240 vscsi->event_list[i].vscsi = vscsi;
241 virtscsi_kick_event(vscsi, &vscsi->event_list[i]);
242 }
243
244 return 0;
245}
246
247static void virtscsi_cancel_event_work(struct virtio_scsi *vscsi)
248{
249 int i;
250
251 for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++)
252 cancel_work_sync(&vscsi->event_list[i].work);
253}
254
255static void virtscsi_handle_transport_reset(struct virtio_scsi *vscsi,
256 struct virtio_scsi_event *event)
257{
258 struct scsi_device *sdev;
259 struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
260 unsigned int target = event->lun[1];
261 unsigned int lun = (event->lun[2] << 8) | event->lun[3];
262
263 switch (event->reason) {
264 case VIRTIO_SCSI_EVT_RESET_RESCAN:
265 scsi_add_device(shost, 0, target, lun);
266 break;
267 case VIRTIO_SCSI_EVT_RESET_REMOVED:
268 sdev = scsi_device_lookup(shost, 0, target, lun);
269 if (sdev) {
270 scsi_remove_device(sdev);
271 scsi_device_put(sdev);
272 } else {
273 pr_err("SCSI device %d 0 %d %d not found\n",
274 shost->host_no, target, lun);
275 }
276 break;
277 default:
278 pr_info("Unsupport virtio scsi event reason %x\n", event->reason);
279 }
280}
281
Paolo Bonzini865b58c2012-10-02 17:25:48 +0200282static void virtscsi_handle_param_change(struct virtio_scsi *vscsi,
283 struct virtio_scsi_event *event)
284{
285 struct scsi_device *sdev;
286 struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
287 unsigned int target = event->lun[1];
288 unsigned int lun = (event->lun[2] << 8) | event->lun[3];
289 u8 asc = event->reason & 255;
290 u8 ascq = event->reason >> 8;
291
292 sdev = scsi_device_lookup(shost, 0, target, lun);
293 if (!sdev) {
294 pr_err("SCSI device %d 0 %d %d not found\n",
295 shost->host_no, target, lun);
296 return;
297 }
298
299 /* Handle "Parameters changed", "Mode parameters changed", and
300 "Capacity data has changed". */
301 if (asc == 0x2a && (ascq == 0x00 || ascq == 0x01 || ascq == 0x09))
302 scsi_rescan_device(&sdev->sdev_gendev);
303
304 scsi_device_put(sdev);
305}
306
Cong Meng365a7152012-07-05 17:06:43 +0800307static void virtscsi_handle_event(struct work_struct *work)
308{
309 struct virtio_scsi_event_node *event_node =
310 container_of(work, struct virtio_scsi_event_node, work);
311 struct virtio_scsi *vscsi = event_node->vscsi;
312 struct virtio_scsi_event *event = &event_node->event;
313
314 if (event->event & VIRTIO_SCSI_T_EVENTS_MISSED) {
315 event->event &= ~VIRTIO_SCSI_T_EVENTS_MISSED;
316 scsi_scan_host(virtio_scsi_host(vscsi->vdev));
317 }
318
319 switch (event->event) {
320 case VIRTIO_SCSI_T_NO_EVENT:
321 break;
322 case VIRTIO_SCSI_T_TRANSPORT_RESET:
323 virtscsi_handle_transport_reset(vscsi, event);
324 break;
Paolo Bonzini865b58c2012-10-02 17:25:48 +0200325 case VIRTIO_SCSI_T_PARAM_CHANGE:
326 virtscsi_handle_param_change(vscsi, event);
327 break;
Cong Meng365a7152012-07-05 17:06:43 +0800328 default:
329 pr_err("Unsupport virtio scsi event %x\n", event->event);
330 }
331 virtscsi_kick_event(vscsi, event_node);
332}
333
334static void virtscsi_complete_event(void *buf)
335{
336 struct virtio_scsi_event_node *event_node = buf;
337
338 INIT_WORK(&event_node->work, virtscsi_handle_event);
339 schedule_work(&event_node->work);
340}
341
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100342static void virtscsi_event_done(struct virtqueue *vq)
343{
Paolo Bonzini139fe452012-06-13 16:56:32 +0200344 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
345 struct virtio_scsi *vscsi = shost_priv(sh);
346 unsigned long flags;
347
348 spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
Cong Meng365a7152012-07-05 17:06:43 +0800349 virtscsi_vq_done(vq, virtscsi_complete_event);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200350 spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100351};
352
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100353/**
Wanlong Gao682993b2013-03-20 15:44:28 +1030354 * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue
355 * @vq : the struct virtqueue we're talking about
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100356 * @cmd : command structure
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100357 * @req_size : size of the request buffer
358 * @resp_size : size of the response buffer
Wanlong Gao682993b2013-03-20 15:44:28 +1030359 * @gfp : flags to use for memory allocations
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100360 */
Wanlong Gao682993b2013-03-20 15:44:28 +1030361static int virtscsi_add_cmd(struct virtqueue *vq,
362 struct virtio_scsi_cmd *cmd,
363 size_t req_size, size_t resp_size, gfp_t gfp)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100364{
365 struct scsi_cmnd *sc = cmd->sc;
Wanlong Gao682993b2013-03-20 15:44:28 +1030366 struct scatterlist *sgs[4], req, resp;
367 struct sg_table *out, *in;
368 unsigned out_num = 0, in_num = 0;
369
370 out = in = NULL;
371
372 if (sc && sc->sc_data_direction != DMA_NONE) {
373 if (sc->sc_data_direction != DMA_FROM_DEVICE)
374 out = &scsi_out(sc)->table;
375 if (sc->sc_data_direction != DMA_TO_DEVICE)
376 in = &scsi_in(sc)->table;
377 }
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100378
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100379 /* Request header. */
Wanlong Gao682993b2013-03-20 15:44:28 +1030380 sg_init_one(&req, &cmd->req, req_size);
381 sgs[out_num++] = &req;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100382
383 /* Data-out buffer. */
Wanlong Gao682993b2013-03-20 15:44:28 +1030384 if (out)
385 sgs[out_num++] = out->sgl;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100386
387 /* Response header. */
Wanlong Gao682993b2013-03-20 15:44:28 +1030388 sg_init_one(&resp, &cmd->resp, resp_size);
389 sgs[out_num + in_num++] = &resp;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100390
391 /* Data-in buffer */
Wanlong Gao682993b2013-03-20 15:44:28 +1030392 if (in)
393 sgs[out_num + in_num++] = in->sgl;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100394
Wanlong Gao682993b2013-03-20 15:44:28 +1030395 return virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, gfp);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100396}
397
Wanlong Gao682993b2013-03-20 15:44:28 +1030398static int virtscsi_kick_cmd(struct virtio_scsi_vq *vq,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100399 struct virtio_scsi_cmd *cmd,
400 size_t req_size, size_t resp_size, gfp_t gfp)
401{
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100402 unsigned long flags;
Rusty Russell4614e512012-10-16 23:56:16 +1030403 int err;
404 bool needs_kick = false;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100405
Wanlong Gao682993b2013-03-20 15:44:28 +1030406 spin_lock_irqsave(&vq->vq_lock, flags);
407 err = virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size, gfp);
Rusty Russell4614e512012-10-16 23:56:16 +1030408 if (!err)
409 needs_kick = virtqueue_kick_prepare(vq->vq);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100410
Paolo Bonzinibce750b2012-06-13 16:56:33 +0200411 spin_unlock_irqrestore(&vq->vq_lock, flags);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200412
Rusty Russell4614e512012-10-16 23:56:16 +1030413 if (needs_kick)
Paolo Bonzini139fe452012-06-13 16:56:32 +0200414 virtqueue_notify(vq->vq);
Rusty Russell4614e512012-10-16 23:56:16 +1030415 return err;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100416}
417
418static int virtscsi_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
419{
420 struct virtio_scsi *vscsi = shost_priv(sh);
421 struct virtio_scsi_cmd *cmd;
422 int ret;
423
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200424 struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
425 BUG_ON(scsi_sg_count(sc) > shost->sg_tablesize);
426
427 /* TODO: check feature bit and fail if unsupported? */
428 BUG_ON(sc->sc_data_direction == DMA_BIDIRECTIONAL);
429
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100430 dev_dbg(&sc->device->sdev_gendev,
431 "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]);
432
433 ret = SCSI_MLQUEUE_HOST_BUSY;
434 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_ATOMIC);
435 if (!cmd)
436 goto out;
437
438 memset(cmd, 0, sizeof(*cmd));
439 cmd->sc = sc;
440 cmd->req.cmd = (struct virtio_scsi_cmd_req){
441 .lun[0] = 1,
442 .lun[1] = sc->device->id,
443 .lun[2] = (sc->device->lun >> 8) | 0x40,
444 .lun[3] = sc->device->lun & 0xff,
445 .tag = (unsigned long)sc,
446 .task_attr = VIRTIO_SCSI_S_SIMPLE,
447 .prio = 0,
448 .crn = 0,
449 };
450
451 BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE);
452 memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len);
453
Wanlong Gao682993b2013-03-20 15:44:28 +1030454 if (virtscsi_kick_cmd(&vscsi->req_vq, cmd,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100455 sizeof cmd->req.cmd, sizeof cmd->resp.cmd,
Rusty Russell4614e512012-10-16 23:56:16 +1030456 GFP_ATOMIC) == 0)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100457 ret = 0;
Eric Northupb56d1002012-11-08 01:55:50 -0800458 else
459 mempool_free(cmd, virtscsi_cmd_pool);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100460
461out:
462 return ret;
463}
464
465static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd)
466{
467 DECLARE_COMPLETION_ONSTACK(comp);
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200468 int ret = FAILED;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100469
470 cmd->comp = &comp;
Wanlong Gao682993b2013-03-20 15:44:28 +1030471 if (virtscsi_kick_cmd(&vscsi->ctrl_vq, cmd,
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200472 sizeof cmd->req.tmf, sizeof cmd->resp.tmf,
473 GFP_NOIO) < 0)
474 goto out;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100475
476 wait_for_completion(&comp);
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200477 if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK ||
478 cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED)
479 ret = SUCCESS;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100480
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200481out:
482 mempool_free(cmd, virtscsi_cmd_pool);
483 return ret;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100484}
485
486static int virtscsi_device_reset(struct scsi_cmnd *sc)
487{
488 struct virtio_scsi *vscsi = shost_priv(sc->device->host);
489 struct virtio_scsi_cmd *cmd;
490
491 sdev_printk(KERN_INFO, sc->device, "device reset\n");
492 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
493 if (!cmd)
494 return FAILED;
495
496 memset(cmd, 0, sizeof(*cmd));
497 cmd->sc = sc;
498 cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
499 .type = VIRTIO_SCSI_T_TMF,
500 .subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET,
501 .lun[0] = 1,
502 .lun[1] = sc->device->id,
503 .lun[2] = (sc->device->lun >> 8) | 0x40,
504 .lun[3] = sc->device->lun & 0xff,
505 };
506 return virtscsi_tmf(vscsi, cmd);
507}
508
509static int virtscsi_abort(struct scsi_cmnd *sc)
510{
511 struct virtio_scsi *vscsi = shost_priv(sc->device->host);
512 struct virtio_scsi_cmd *cmd;
513
514 scmd_printk(KERN_INFO, sc, "abort\n");
515 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
516 if (!cmd)
517 return FAILED;
518
519 memset(cmd, 0, sizeof(*cmd));
520 cmd->sc = sc;
521 cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
522 .type = VIRTIO_SCSI_T_TMF,
523 .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK,
524 .lun[0] = 1,
525 .lun[1] = sc->device->id,
526 .lun[2] = (sc->device->lun >> 8) | 0x40,
527 .lun[3] = sc->device->lun & 0xff,
528 .tag = (unsigned long)sc,
529 };
530 return virtscsi_tmf(vscsi, cmd);
531}
532
533static struct scsi_host_template virtscsi_host_template = {
534 .module = THIS_MODULE,
535 .name = "Virtio SCSI HBA",
536 .proc_name = "virtio_scsi",
537 .queuecommand = virtscsi_queuecommand,
538 .this_id = -1,
539 .eh_abort_handler = virtscsi_abort,
540 .eh_device_reset_handler = virtscsi_device_reset,
541
542 .can_queue = 1024,
543 .dma_boundary = UINT_MAX,
544 .use_clustering = ENABLE_CLUSTERING,
545};
546
547#define virtscsi_config_get(vdev, fld) \
548 ({ \
549 typeof(((struct virtio_scsi_config *)0)->fld) __val; \
550 vdev->config->get(vdev, \
551 offsetof(struct virtio_scsi_config, fld), \
552 &__val, sizeof(__val)); \
553 __val; \
554 })
555
556#define virtscsi_config_set(vdev, fld, val) \
557 (void)({ \
558 typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
559 vdev->config->set(vdev, \
560 offsetof(struct virtio_scsi_config, fld), \
561 &__val, sizeof(__val)); \
562 })
563
Paolo Bonzini139fe452012-06-13 16:56:32 +0200564static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq,
565 struct virtqueue *vq)
566{
567 spin_lock_init(&virtscsi_vq->vq_lock);
568 virtscsi_vq->vq = vq;
569}
570
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200571static struct virtio_scsi_target_state *virtscsi_alloc_tgt(
Wanlong Gao682993b2013-03-20 15:44:28 +1030572 struct virtio_device *vdev)
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200573{
574 struct virtio_scsi_target_state *tgt;
575 gfp_t gfp_mask = GFP_KERNEL;
576
Wanlong Gao682993b2013-03-20 15:44:28 +1030577 tgt = kmalloc(sizeof(*tgt), gfp_mask);
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200578 if (!tgt)
579 return NULL;
580
581 spin_lock_init(&tgt->tgt_lock);
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200582 return tgt;
583}
584
Nicholas Bellinger59057fb2012-07-11 21:22:16 +0000585static void virtscsi_scan(struct virtio_device *vdev)
586{
587 struct Scsi_Host *shost = (struct Scsi_Host *)vdev->priv;
588
589 scsi_scan_host(shost);
590}
591
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200592static void virtscsi_remove_vqs(struct virtio_device *vdev)
593{
594 struct Scsi_Host *sh = virtio_scsi_host(vdev);
595 struct virtio_scsi *vscsi = shost_priv(sh);
596 u32 i, num_targets;
597
598 /* Stop all the virtqueues. */
599 vdev->config->reset(vdev);
600
601 num_targets = sh->max_id;
602 for (i = 0; i < num_targets; i++) {
603 kfree(vscsi->tgt[i]);
604 vscsi->tgt[i] = NULL;
605 }
606
607 vdev->config->del_vqs(vdev);
608}
609
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100610static int virtscsi_init(struct virtio_device *vdev,
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200611 struct virtio_scsi *vscsi, int num_targets)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100612{
613 int err;
614 struct virtqueue *vqs[3];
Wanlong Gao682993b2013-03-20 15:44:28 +1030615 u32 i;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200616
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100617 vq_callback_t *callbacks[] = {
618 virtscsi_ctrl_done,
619 virtscsi_event_done,
620 virtscsi_req_done
621 };
622 const char *names[] = {
623 "control",
624 "event",
625 "request"
626 };
627
628 /* Discover virtqueues and write information to configuration. */
629 err = vdev->config->find_vqs(vdev, 3, vqs, callbacks, names);
630 if (err)
631 return err;
632
Paolo Bonzini139fe452012-06-13 16:56:32 +0200633 virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]);
634 virtscsi_init_vq(&vscsi->event_vq, vqs[1]);
635 virtscsi_init_vq(&vscsi->req_vq, vqs[2]);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100636
637 virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
638 virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200639
Cong Meng365a7152012-07-05 17:06:43 +0800640 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
641 virtscsi_kick_event_all(vscsi);
642
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200643 for (i = 0; i < num_targets; i++) {
Wanlong Gao682993b2013-03-20 15:44:28 +1030644 vscsi->tgt[i] = virtscsi_alloc_tgt(vdev);
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200645 if (!vscsi->tgt[i]) {
646 err = -ENOMEM;
647 goto out;
648 }
649 }
650 err = 0;
651
652out:
653 if (err)
654 virtscsi_remove_vqs(vdev);
655 return err;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100656}
657
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800658static int virtscsi_probe(struct virtio_device *vdev)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100659{
660 struct Scsi_Host *shost;
661 struct virtio_scsi *vscsi;
662 int err;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200663 u32 sg_elems, num_targets;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100664 u32 cmd_per_lun;
665
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100666 /* Allocate memory and link the structs together. */
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200667 num_targets = virtscsi_config_get(vdev, max_target) + 1;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100668 shost = scsi_host_alloc(&virtscsi_host_template,
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200669 sizeof(*vscsi)
670 + num_targets * sizeof(struct virtio_scsi_target_state));
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100671
672 if (!shost)
673 return -ENOMEM;
674
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200675 sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100676 shost->sg_tablesize = sg_elems;
677 vscsi = shost_priv(shost);
678 vscsi->vdev = vdev;
679 vdev->priv = shost;
680
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200681 err = virtscsi_init(vdev, vscsi, num_targets);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100682 if (err)
683 goto virtscsi_init_failed;
684
685 cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1;
686 shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue);
687 shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF;
Paolo Bonzini9da5f5a2012-10-02 17:25:47 +0200688
689 /* LUNs > 256 are reported with format 1, so they go in the range
690 * 16640-32767.
691 */
692 shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1 + 0x4000;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200693 shost->max_id = num_targets;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100694 shost->max_channel = 0;
695 shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE;
696 err = scsi_add_host(shost, &vdev->dev);
697 if (err)
698 goto scsi_add_host_failed;
Nicholas Bellinger59057fb2012-07-11 21:22:16 +0000699 /*
700 * scsi_scan_host() happens in virtscsi_scan() via virtio_driver->scan()
701 * after VIRTIO_CONFIG_S_DRIVER_OK has been set..
702 */
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100703 return 0;
704
705scsi_add_host_failed:
706 vdev->config->del_vqs(vdev);
707virtscsi_init_failed:
708 scsi_host_put(shost);
709 return err;
710}
711
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800712static void virtscsi_remove(struct virtio_device *vdev)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100713{
714 struct Scsi_Host *shost = virtio_scsi_host(vdev);
Cong Meng365a7152012-07-05 17:06:43 +0800715 struct virtio_scsi *vscsi = shost_priv(shost);
716
717 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
718 virtscsi_cancel_event_work(vscsi);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100719
720 scsi_remove_host(shost);
721
722 virtscsi_remove_vqs(vdev);
723 scsi_host_put(shost);
724}
725
726#ifdef CONFIG_PM
727static int virtscsi_freeze(struct virtio_device *vdev)
728{
729 virtscsi_remove_vqs(vdev);
730 return 0;
731}
732
733static int virtscsi_restore(struct virtio_device *vdev)
734{
735 struct Scsi_Host *sh = virtio_scsi_host(vdev);
736 struct virtio_scsi *vscsi = shost_priv(sh);
737
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200738 return virtscsi_init(vdev, vscsi, sh->max_id);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100739}
740#endif
741
742static struct virtio_device_id id_table[] = {
743 { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID },
744 { 0 },
745};
746
Cong Meng365a7152012-07-05 17:06:43 +0800747static unsigned int features[] = {
Paolo Bonzini865b58c2012-10-02 17:25:48 +0200748 VIRTIO_SCSI_F_HOTPLUG,
749 VIRTIO_SCSI_F_CHANGE,
Cong Meng365a7152012-07-05 17:06:43 +0800750};
751
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100752static struct virtio_driver virtio_scsi_driver = {
Cong Meng365a7152012-07-05 17:06:43 +0800753 .feature_table = features,
754 .feature_table_size = ARRAY_SIZE(features),
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100755 .driver.name = KBUILD_MODNAME,
756 .driver.owner = THIS_MODULE,
757 .id_table = id_table,
758 .probe = virtscsi_probe,
Nicholas Bellinger59057fb2012-07-11 21:22:16 +0000759 .scan = virtscsi_scan,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100760#ifdef CONFIG_PM
761 .freeze = virtscsi_freeze,
762 .restore = virtscsi_restore,
763#endif
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800764 .remove = virtscsi_remove,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100765};
766
767static int __init init(void)
768{
769 int ret = -ENOMEM;
770
771 virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0);
772 if (!virtscsi_cmd_cache) {
Wanlong Gaoba06d1e2013-03-12 15:34:40 +1030773 pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n");
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100774 goto error;
775 }
776
777
778 virtscsi_cmd_pool =
779 mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ,
780 virtscsi_cmd_cache);
781 if (!virtscsi_cmd_pool) {
Wanlong Gaoba06d1e2013-03-12 15:34:40 +1030782 pr_err("mempool_create() for virtscsi_cmd_pool failed\n");
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100783 goto error;
784 }
785 ret = register_virtio_driver(&virtio_scsi_driver);
786 if (ret < 0)
787 goto error;
788
789 return 0;
790
791error:
792 if (virtscsi_cmd_pool) {
793 mempool_destroy(virtscsi_cmd_pool);
794 virtscsi_cmd_pool = NULL;
795 }
796 if (virtscsi_cmd_cache) {
797 kmem_cache_destroy(virtscsi_cmd_cache);
798 virtscsi_cmd_cache = NULL;
799 }
800 return ret;
801}
802
803static void __exit fini(void)
804{
805 unregister_virtio_driver(&virtio_scsi_driver);
806 mempool_destroy(virtscsi_cmd_pool);
807 kmem_cache_destroy(virtscsi_cmd_cache);
808}
809module_init(init);
810module_exit(fini);
811
812MODULE_DEVICE_TABLE(virtio, id_table);
813MODULE_DESCRIPTION("Virtio SCSI HBA driver");
814MODULE_LICENSE("GPL");