blob: a7cf726bc747875fbd2d3515adba623b6dca81fd [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
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/mempool.h>
19#include <linux/virtio.h>
20#include <linux/virtio_ids.h>
21#include <linux/virtio_config.h>
22#include <linux/virtio_scsi.h>
23#include <scsi/scsi_host.h>
24#include <scsi/scsi_device.h>
25#include <scsi/scsi_cmnd.h>
26
27#define VIRTIO_SCSI_MEMPOOL_SZ 64
Cong Meng365a7152012-07-05 17:06:43 +080028#define VIRTIO_SCSI_EVENT_LEN 8
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010029
30/* Command queue element */
31struct virtio_scsi_cmd {
32 struct scsi_cmnd *sc;
33 struct completion *comp;
34 union {
35 struct virtio_scsi_cmd_req cmd;
36 struct virtio_scsi_ctrl_tmf_req tmf;
37 struct virtio_scsi_ctrl_an_req an;
38 } req;
39 union {
40 struct virtio_scsi_cmd_resp cmd;
41 struct virtio_scsi_ctrl_tmf_resp tmf;
42 struct virtio_scsi_ctrl_an_resp an;
43 struct virtio_scsi_event evt;
44 } resp;
45} ____cacheline_aligned_in_smp;
46
Cong Meng365a7152012-07-05 17:06:43 +080047struct virtio_scsi_event_node {
48 struct virtio_scsi *vscsi;
49 struct virtio_scsi_event event;
50 struct work_struct work;
51};
52
Paolo Bonzini139fe452012-06-13 16:56:32 +020053struct virtio_scsi_vq {
54 /* Protects vq */
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010055 spinlock_t vq_lock;
56
Paolo Bonzini139fe452012-06-13 16:56:32 +020057 struct virtqueue *vq;
58};
59
Paolo Bonzini2bd37f02012-06-13 16:56:34 +020060/* Per-target queue state */
61struct virtio_scsi_target_state {
62 /* Protects sg. Lock hierarchy is tgt_lock -> vq_lock. */
63 spinlock_t tgt_lock;
64
65 /* For sglist construction when adding commands to the virtqueue. */
66 struct scatterlist sg[];
67};
68
Paolo Bonzini139fe452012-06-13 16:56:32 +020069/* Driver instance state */
70struct virtio_scsi {
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010071 struct virtio_device *vdev;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +020072
Paolo Bonzini139fe452012-06-13 16:56:32 +020073 struct virtio_scsi_vq ctrl_vq;
74 struct virtio_scsi_vq event_vq;
75 struct virtio_scsi_vq req_vq;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010076
Cong Meng365a7152012-07-05 17:06:43 +080077 /* Get some buffers ready for event vq */
78 struct virtio_scsi_event_node event_list[VIRTIO_SCSI_EVENT_LEN];
79
Paolo Bonzini2bd37f02012-06-13 16:56:34 +020080 struct virtio_scsi_target_state *tgt[];
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010081};
82
83static struct kmem_cache *virtscsi_cmd_cache;
84static mempool_t *virtscsi_cmd_pool;
85
86static inline struct Scsi_Host *virtio_scsi_host(struct virtio_device *vdev)
87{
88 return vdev->priv;
89}
90
91static void virtscsi_compute_resid(struct scsi_cmnd *sc, u32 resid)
92{
93 if (!resid)
94 return;
95
96 if (!scsi_bidi_cmnd(sc)) {
97 scsi_set_resid(sc, resid);
98 return;
99 }
100
101 scsi_in(sc)->resid = min(resid, scsi_in(sc)->length);
102 scsi_out(sc)->resid = resid - scsi_in(sc)->resid;
103}
104
105/**
106 * virtscsi_complete_cmd - finish a scsi_cmd and invoke scsi_done
107 *
108 * Called with vq_lock held.
109 */
110static void virtscsi_complete_cmd(void *buf)
111{
112 struct virtio_scsi_cmd *cmd = buf;
113 struct scsi_cmnd *sc = cmd->sc;
114 struct virtio_scsi_cmd_resp *resp = &cmd->resp.cmd;
115
116 dev_dbg(&sc->device->sdev_gendev,
117 "cmd %p response %u status %#02x sense_len %u\n",
118 sc, resp->response, resp->status, resp->sense_len);
119
120 sc->result = resp->status;
121 virtscsi_compute_resid(sc, resp->resid);
122 switch (resp->response) {
123 case VIRTIO_SCSI_S_OK:
124 set_host_byte(sc, DID_OK);
125 break;
126 case VIRTIO_SCSI_S_OVERRUN:
127 set_host_byte(sc, DID_ERROR);
128 break;
129 case VIRTIO_SCSI_S_ABORTED:
130 set_host_byte(sc, DID_ABORT);
131 break;
132 case VIRTIO_SCSI_S_BAD_TARGET:
133 set_host_byte(sc, DID_BAD_TARGET);
134 break;
135 case VIRTIO_SCSI_S_RESET:
136 set_host_byte(sc, DID_RESET);
137 break;
138 case VIRTIO_SCSI_S_BUSY:
139 set_host_byte(sc, DID_BUS_BUSY);
140 break;
141 case VIRTIO_SCSI_S_TRANSPORT_FAILURE:
142 set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
143 break;
144 case VIRTIO_SCSI_S_TARGET_FAILURE:
145 set_host_byte(sc, DID_TARGET_FAILURE);
146 break;
147 case VIRTIO_SCSI_S_NEXUS_FAILURE:
148 set_host_byte(sc, DID_NEXUS_FAILURE);
149 break;
150 default:
151 scmd_printk(KERN_WARNING, sc, "Unknown response %d",
152 resp->response);
153 /* fall through */
154 case VIRTIO_SCSI_S_FAILURE:
155 set_host_byte(sc, DID_ERROR);
156 break;
157 }
158
159 WARN_ON(resp->sense_len > VIRTIO_SCSI_SENSE_SIZE);
160 if (sc->sense_buffer) {
161 memcpy(sc->sense_buffer, resp->sense,
162 min_t(u32, resp->sense_len, VIRTIO_SCSI_SENSE_SIZE));
163 if (resp->sense_len)
164 set_driver_byte(sc, DRIVER_SENSE);
165 }
166
167 mempool_free(cmd, virtscsi_cmd_pool);
168 sc->scsi_done(sc);
169}
170
171static void virtscsi_vq_done(struct virtqueue *vq, void (*fn)(void *buf))
172{
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100173 void *buf;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100174 unsigned int len;
175
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100176 do {
177 virtqueue_disable_cb(vq);
178 while ((buf = virtqueue_get_buf(vq, &len)) != NULL)
179 fn(buf);
180 } while (!virtqueue_enable_cb(vq));
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100181}
182
183static void virtscsi_req_done(struct virtqueue *vq)
184{
Paolo Bonzini139fe452012-06-13 16:56:32 +0200185 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
186 struct virtio_scsi *vscsi = shost_priv(sh);
187 unsigned long flags;
188
189 spin_lock_irqsave(&vscsi->req_vq.vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100190 virtscsi_vq_done(vq, virtscsi_complete_cmd);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200191 spin_unlock_irqrestore(&vscsi->req_vq.vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100192};
193
194static void virtscsi_complete_free(void *buf)
195{
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);
208 unsigned long flags;
209
210 spin_lock_irqsave(&vscsi->ctrl_vq.vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100211 virtscsi_vq_done(vq, virtscsi_complete_free);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200212 spin_unlock_irqrestore(&vscsi->ctrl_vq.vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100213};
214
Cong Meng365a7152012-07-05 17:06:43 +0800215static int virtscsi_kick_event(struct virtio_scsi *vscsi,
216 struct virtio_scsi_event_node *event_node)
217{
218 int ret;
219 struct scatterlist sg;
220 unsigned long flags;
221
Richard W.M. Jones2e9c9df2012-10-02 17:25:46 +0200222 sg_init_one(&sg, &event_node->event, sizeof(struct virtio_scsi_event));
Cong Meng365a7152012-07-05 17:06:43 +0800223
224 spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
225
226 ret = virtqueue_add_buf(vscsi->event_vq.vq, &sg, 0, 1, event_node, GFP_ATOMIC);
227 if (ret >= 0)
228 virtqueue_kick(vscsi->event_vq.vq);
229
230 spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
231
232 return ret;
233}
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
282static void virtscsi_handle_event(struct work_struct *work)
283{
284 struct virtio_scsi_event_node *event_node =
285 container_of(work, struct virtio_scsi_event_node, work);
286 struct virtio_scsi *vscsi = event_node->vscsi;
287 struct virtio_scsi_event *event = &event_node->event;
288
289 if (event->event & VIRTIO_SCSI_T_EVENTS_MISSED) {
290 event->event &= ~VIRTIO_SCSI_T_EVENTS_MISSED;
291 scsi_scan_host(virtio_scsi_host(vscsi->vdev));
292 }
293
294 switch (event->event) {
295 case VIRTIO_SCSI_T_NO_EVENT:
296 break;
297 case VIRTIO_SCSI_T_TRANSPORT_RESET:
298 virtscsi_handle_transport_reset(vscsi, event);
299 break;
300 default:
301 pr_err("Unsupport virtio scsi event %x\n", event->event);
302 }
303 virtscsi_kick_event(vscsi, event_node);
304}
305
306static void virtscsi_complete_event(void *buf)
307{
308 struct virtio_scsi_event_node *event_node = buf;
309
310 INIT_WORK(&event_node->work, virtscsi_handle_event);
311 schedule_work(&event_node->work);
312}
313
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100314static void virtscsi_event_done(struct virtqueue *vq)
315{
Paolo Bonzini139fe452012-06-13 16:56:32 +0200316 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
317 struct virtio_scsi *vscsi = shost_priv(sh);
318 unsigned long flags;
319
320 spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
Cong Meng365a7152012-07-05 17:06:43 +0800321 virtscsi_vq_done(vq, virtscsi_complete_event);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200322 spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100323};
324
325static void virtscsi_map_sgl(struct scatterlist *sg, unsigned int *p_idx,
326 struct scsi_data_buffer *sdb)
327{
328 struct sg_table *table = &sdb->table;
329 struct scatterlist *sg_elem;
330 unsigned int idx = *p_idx;
331 int i;
332
333 for_each_sg(table->sgl, sg_elem, table->nents, i)
Wang Sen27e99ad2012-07-30 14:25:06 +0800334 sg[idx++] = *sg_elem;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100335
336 *p_idx = idx;
337}
338
339/**
340 * virtscsi_map_cmd - map a scsi_cmd to a virtqueue scatterlist
341 * @vscsi : virtio_scsi state
342 * @cmd : command structure
343 * @out_num : number of read-only elements
344 * @in_num : number of write-only elements
345 * @req_size : size of the request buffer
346 * @resp_size : size of the response buffer
347 *
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200348 * Called with tgt_lock held.
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100349 */
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200350static void virtscsi_map_cmd(struct virtio_scsi_target_state *tgt,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100351 struct virtio_scsi_cmd *cmd,
352 unsigned *out_num, unsigned *in_num,
353 size_t req_size, size_t resp_size)
354{
355 struct scsi_cmnd *sc = cmd->sc;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200356 struct scatterlist *sg = tgt->sg;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100357 unsigned int idx = 0;
358
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100359 /* Request header. */
360 sg_set_buf(&sg[idx++], &cmd->req, req_size);
361
362 /* Data-out buffer. */
363 if (sc && sc->sc_data_direction != DMA_FROM_DEVICE)
364 virtscsi_map_sgl(sg, &idx, scsi_out(sc));
365
366 *out_num = idx;
367
368 /* Response header. */
369 sg_set_buf(&sg[idx++], &cmd->resp, resp_size);
370
371 /* Data-in buffer */
372 if (sc && sc->sc_data_direction != DMA_TO_DEVICE)
373 virtscsi_map_sgl(sg, &idx, scsi_in(sc));
374
375 *in_num = idx - *out_num;
376}
377
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200378static int virtscsi_kick_cmd(struct virtio_scsi_target_state *tgt,
379 struct virtio_scsi_vq *vq,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100380 struct virtio_scsi_cmd *cmd,
381 size_t req_size, size_t resp_size, gfp_t gfp)
382{
383 unsigned int out_num, in_num;
384 unsigned long flags;
385 int ret;
386
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200387 spin_lock_irqsave(&tgt->tgt_lock, flags);
388 virtscsi_map_cmd(tgt, cmd, &out_num, &in_num, req_size, resp_size);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100389
Paolo Bonzini139fe452012-06-13 16:56:32 +0200390 spin_lock(&vq->vq_lock);
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200391 ret = virtqueue_add_buf(vq->vq, tgt->sg, out_num, in_num, cmd, gfp);
392 spin_unlock(&tgt->tgt_lock);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100393 if (ret >= 0)
Paolo Bonzini139fe452012-06-13 16:56:32 +0200394 ret = virtqueue_kick_prepare(vq->vq);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100395
Paolo Bonzinibce750b2012-06-13 16:56:33 +0200396 spin_unlock_irqrestore(&vq->vq_lock, flags);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200397
Paolo Bonzinib5ee8f22012-06-13 16:56:31 +0200398 if (ret > 0)
Paolo Bonzini139fe452012-06-13 16:56:32 +0200399 virtqueue_notify(vq->vq);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100400 return ret;
401}
402
403static int virtscsi_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc)
404{
405 struct virtio_scsi *vscsi = shost_priv(sh);
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200406 struct virtio_scsi_target_state *tgt = vscsi->tgt[sc->device->id];
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100407 struct virtio_scsi_cmd *cmd;
408 int ret;
409
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200410 struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
411 BUG_ON(scsi_sg_count(sc) > shost->sg_tablesize);
412
413 /* TODO: check feature bit and fail if unsupported? */
414 BUG_ON(sc->sc_data_direction == DMA_BIDIRECTIONAL);
415
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100416 dev_dbg(&sc->device->sdev_gendev,
417 "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]);
418
419 ret = SCSI_MLQUEUE_HOST_BUSY;
420 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_ATOMIC);
421 if (!cmd)
422 goto out;
423
424 memset(cmd, 0, sizeof(*cmd));
425 cmd->sc = sc;
426 cmd->req.cmd = (struct virtio_scsi_cmd_req){
427 .lun[0] = 1,
428 .lun[1] = sc->device->id,
429 .lun[2] = (sc->device->lun >> 8) | 0x40,
430 .lun[3] = sc->device->lun & 0xff,
431 .tag = (unsigned long)sc,
432 .task_attr = VIRTIO_SCSI_S_SIMPLE,
433 .prio = 0,
434 .crn = 0,
435 };
436
437 BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE);
438 memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len);
439
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200440 if (virtscsi_kick_cmd(tgt, &vscsi->req_vq, cmd,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100441 sizeof cmd->req.cmd, sizeof cmd->resp.cmd,
442 GFP_ATOMIC) >= 0)
443 ret = 0;
444
445out:
446 return ret;
447}
448
449static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd)
450{
451 DECLARE_COMPLETION_ONSTACK(comp);
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200452 struct virtio_scsi_target_state *tgt = vscsi->tgt[cmd->sc->device->id];
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200453 int ret = FAILED;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100454
455 cmd->comp = &comp;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200456 if (virtscsi_kick_cmd(tgt, &vscsi->ctrl_vq, cmd,
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200457 sizeof cmd->req.tmf, sizeof cmd->resp.tmf,
458 GFP_NOIO) < 0)
459 goto out;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100460
461 wait_for_completion(&comp);
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200462 if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK ||
463 cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED)
464 ret = SUCCESS;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100465
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200466out:
467 mempool_free(cmd, virtscsi_cmd_pool);
468 return ret;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100469}
470
471static int virtscsi_device_reset(struct scsi_cmnd *sc)
472{
473 struct virtio_scsi *vscsi = shost_priv(sc->device->host);
474 struct virtio_scsi_cmd *cmd;
475
476 sdev_printk(KERN_INFO, sc->device, "device reset\n");
477 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
478 if (!cmd)
479 return FAILED;
480
481 memset(cmd, 0, sizeof(*cmd));
482 cmd->sc = sc;
483 cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
484 .type = VIRTIO_SCSI_T_TMF,
485 .subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET,
486 .lun[0] = 1,
487 .lun[1] = sc->device->id,
488 .lun[2] = (sc->device->lun >> 8) | 0x40,
489 .lun[3] = sc->device->lun & 0xff,
490 };
491 return virtscsi_tmf(vscsi, cmd);
492}
493
494static int virtscsi_abort(struct scsi_cmnd *sc)
495{
496 struct virtio_scsi *vscsi = shost_priv(sc->device->host);
497 struct virtio_scsi_cmd *cmd;
498
499 scmd_printk(KERN_INFO, sc, "abort\n");
500 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
501 if (!cmd)
502 return FAILED;
503
504 memset(cmd, 0, sizeof(*cmd));
505 cmd->sc = sc;
506 cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
507 .type = VIRTIO_SCSI_T_TMF,
508 .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK,
509 .lun[0] = 1,
510 .lun[1] = sc->device->id,
511 .lun[2] = (sc->device->lun >> 8) | 0x40,
512 .lun[3] = sc->device->lun & 0xff,
513 .tag = (unsigned long)sc,
514 };
515 return virtscsi_tmf(vscsi, cmd);
516}
517
518static struct scsi_host_template virtscsi_host_template = {
519 .module = THIS_MODULE,
520 .name = "Virtio SCSI HBA",
521 .proc_name = "virtio_scsi",
522 .queuecommand = virtscsi_queuecommand,
523 .this_id = -1,
524 .eh_abort_handler = virtscsi_abort,
525 .eh_device_reset_handler = virtscsi_device_reset,
526
527 .can_queue = 1024,
528 .dma_boundary = UINT_MAX,
529 .use_clustering = ENABLE_CLUSTERING,
530};
531
532#define virtscsi_config_get(vdev, fld) \
533 ({ \
534 typeof(((struct virtio_scsi_config *)0)->fld) __val; \
535 vdev->config->get(vdev, \
536 offsetof(struct virtio_scsi_config, fld), \
537 &__val, sizeof(__val)); \
538 __val; \
539 })
540
541#define virtscsi_config_set(vdev, fld, val) \
542 (void)({ \
543 typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
544 vdev->config->set(vdev, \
545 offsetof(struct virtio_scsi_config, fld), \
546 &__val, sizeof(__val)); \
547 })
548
Paolo Bonzini139fe452012-06-13 16:56:32 +0200549static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq,
550 struct virtqueue *vq)
551{
552 spin_lock_init(&virtscsi_vq->vq_lock);
553 virtscsi_vq->vq = vq;
554}
555
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200556static struct virtio_scsi_target_state *virtscsi_alloc_tgt(
557 struct virtio_device *vdev, int sg_elems)
558{
559 struct virtio_scsi_target_state *tgt;
560 gfp_t gfp_mask = GFP_KERNEL;
561
562 /* We need extra sg elements at head and tail. */
563 tgt = kmalloc(sizeof(*tgt) + sizeof(tgt->sg[0]) * (sg_elems + 2),
564 gfp_mask);
565
566 if (!tgt)
567 return NULL;
568
569 spin_lock_init(&tgt->tgt_lock);
570 sg_init_table(tgt->sg, sg_elems + 2);
571 return tgt;
572}
573
Nicholas Bellinger59057fb2012-07-11 21:22:16 +0000574static void virtscsi_scan(struct virtio_device *vdev)
575{
576 struct Scsi_Host *shost = (struct Scsi_Host *)vdev->priv;
577
578 scsi_scan_host(shost);
579}
580
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200581static void virtscsi_remove_vqs(struct virtio_device *vdev)
582{
583 struct Scsi_Host *sh = virtio_scsi_host(vdev);
584 struct virtio_scsi *vscsi = shost_priv(sh);
585 u32 i, num_targets;
586
587 /* Stop all the virtqueues. */
588 vdev->config->reset(vdev);
589
590 num_targets = sh->max_id;
591 for (i = 0; i < num_targets; i++) {
592 kfree(vscsi->tgt[i]);
593 vscsi->tgt[i] = NULL;
594 }
595
596 vdev->config->del_vqs(vdev);
597}
598
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100599static int virtscsi_init(struct virtio_device *vdev,
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200600 struct virtio_scsi *vscsi, int num_targets)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100601{
602 int err;
603 struct virtqueue *vqs[3];
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200604 u32 i, sg_elems;
605
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100606 vq_callback_t *callbacks[] = {
607 virtscsi_ctrl_done,
608 virtscsi_event_done,
609 virtscsi_req_done
610 };
611 const char *names[] = {
612 "control",
613 "event",
614 "request"
615 };
616
617 /* Discover virtqueues and write information to configuration. */
618 err = vdev->config->find_vqs(vdev, 3, vqs, callbacks, names);
619 if (err)
620 return err;
621
Paolo Bonzini139fe452012-06-13 16:56:32 +0200622 virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]);
623 virtscsi_init_vq(&vscsi->event_vq, vqs[1]);
624 virtscsi_init_vq(&vscsi->req_vq, vqs[2]);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100625
626 virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
627 virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200628
Cong Meng365a7152012-07-05 17:06:43 +0800629 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
630 virtscsi_kick_event_all(vscsi);
631
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200632 /* We need to know how many segments before we allocate. */
633 sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
634
635 for (i = 0; i < num_targets; i++) {
636 vscsi->tgt[i] = virtscsi_alloc_tgt(vdev, sg_elems);
637 if (!vscsi->tgt[i]) {
638 err = -ENOMEM;
639 goto out;
640 }
641 }
642 err = 0;
643
644out:
645 if (err)
646 virtscsi_remove_vqs(vdev);
647 return err;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100648}
649
650static int __devinit virtscsi_probe(struct virtio_device *vdev)
651{
652 struct Scsi_Host *shost;
653 struct virtio_scsi *vscsi;
654 int err;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200655 u32 sg_elems, num_targets;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100656 u32 cmd_per_lun;
657
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100658 /* Allocate memory and link the structs together. */
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200659 num_targets = virtscsi_config_get(vdev, max_target) + 1;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100660 shost = scsi_host_alloc(&virtscsi_host_template,
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200661 sizeof(*vscsi)
662 + num_targets * sizeof(struct virtio_scsi_target_state));
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100663
664 if (!shost)
665 return -ENOMEM;
666
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200667 sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100668 shost->sg_tablesize = sg_elems;
669 vscsi = shost_priv(shost);
670 vscsi->vdev = vdev;
671 vdev->priv = shost;
672
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200673 err = virtscsi_init(vdev, vscsi, num_targets);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100674 if (err)
675 goto virtscsi_init_failed;
676
677 cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1;
678 shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue);
679 shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF;
Paolo Bonzini9da5f5a2012-10-02 17:25:47 +0200680
681 /* LUNs > 256 are reported with format 1, so they go in the range
682 * 16640-32767.
683 */
684 shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1 + 0x4000;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200685 shost->max_id = num_targets;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100686 shost->max_channel = 0;
687 shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE;
688 err = scsi_add_host(shost, &vdev->dev);
689 if (err)
690 goto scsi_add_host_failed;
Nicholas Bellinger59057fb2012-07-11 21:22:16 +0000691 /*
692 * scsi_scan_host() happens in virtscsi_scan() via virtio_driver->scan()
693 * after VIRTIO_CONFIG_S_DRIVER_OK has been set..
694 */
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100695 return 0;
696
697scsi_add_host_failed:
698 vdev->config->del_vqs(vdev);
699virtscsi_init_failed:
700 scsi_host_put(shost);
701 return err;
702}
703
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100704static void __devexit virtscsi_remove(struct virtio_device *vdev)
705{
706 struct Scsi_Host *shost = virtio_scsi_host(vdev);
Cong Meng365a7152012-07-05 17:06:43 +0800707 struct virtio_scsi *vscsi = shost_priv(shost);
708
709 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
710 virtscsi_cancel_event_work(vscsi);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100711
712 scsi_remove_host(shost);
713
714 virtscsi_remove_vqs(vdev);
715 scsi_host_put(shost);
716}
717
718#ifdef CONFIG_PM
719static int virtscsi_freeze(struct virtio_device *vdev)
720{
721 virtscsi_remove_vqs(vdev);
722 return 0;
723}
724
725static int virtscsi_restore(struct virtio_device *vdev)
726{
727 struct Scsi_Host *sh = virtio_scsi_host(vdev);
728 struct virtio_scsi *vscsi = shost_priv(sh);
729
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200730 return virtscsi_init(vdev, vscsi, sh->max_id);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100731}
732#endif
733
734static struct virtio_device_id id_table[] = {
735 { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID },
736 { 0 },
737};
738
Cong Meng365a7152012-07-05 17:06:43 +0800739static unsigned int features[] = {
740 VIRTIO_SCSI_F_HOTPLUG
741};
742
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100743static struct virtio_driver virtio_scsi_driver = {
Cong Meng365a7152012-07-05 17:06:43 +0800744 .feature_table = features,
745 .feature_table_size = ARRAY_SIZE(features),
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100746 .driver.name = KBUILD_MODNAME,
747 .driver.owner = THIS_MODULE,
748 .id_table = id_table,
749 .probe = virtscsi_probe,
Nicholas Bellinger59057fb2012-07-11 21:22:16 +0000750 .scan = virtscsi_scan,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100751#ifdef CONFIG_PM
752 .freeze = virtscsi_freeze,
753 .restore = virtscsi_restore,
754#endif
755 .remove = __devexit_p(virtscsi_remove),
756};
757
758static int __init init(void)
759{
760 int ret = -ENOMEM;
761
762 virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0);
763 if (!virtscsi_cmd_cache) {
764 printk(KERN_ERR "kmem_cache_create() for "
765 "virtscsi_cmd_cache failed\n");
766 goto error;
767 }
768
769
770 virtscsi_cmd_pool =
771 mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ,
772 virtscsi_cmd_cache);
773 if (!virtscsi_cmd_pool) {
774 printk(KERN_ERR "mempool_create() for"
775 "virtscsi_cmd_pool failed\n");
776 goto error;
777 }
778 ret = register_virtio_driver(&virtio_scsi_driver);
779 if (ret < 0)
780 goto error;
781
782 return 0;
783
784error:
785 if (virtscsi_cmd_pool) {
786 mempool_destroy(virtscsi_cmd_pool);
787 virtscsi_cmd_pool = NULL;
788 }
789 if (virtscsi_cmd_cache) {
790 kmem_cache_destroy(virtscsi_cmd_cache);
791 virtscsi_cmd_cache = NULL;
792 }
793 return ret;
794}
795
796static void __exit fini(void)
797{
798 unregister_virtio_driver(&virtio_scsi_driver);
799 mempool_destroy(virtscsi_cmd_pool);
800 kmem_cache_destroy(virtscsi_cmd_cache);
801}
802module_init(init);
803module_exit(fini);
804
805MODULE_DEVICE_TABLE(virtio, id_table);
806MODULE_DESCRIPTION("Virtio SCSI HBA driver");
807MODULE_LICENSE("GPL");