blob: fc054935eb1f8c35c118dccc6f5e985c59522b0f [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>
Paolo Bonzini9141a4c2013-04-08 23:03:25 +093025#include <linux/cpu.h>
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010026#include <scsi/scsi_host.h>
27#include <scsi/scsi_device.h>
28#include <scsi/scsi_cmnd.h>
29
30#define VIRTIO_SCSI_MEMPOOL_SZ 64
Cong Meng365a7152012-07-05 17:06:43 +080031#define VIRTIO_SCSI_EVENT_LEN 8
Paolo Bonzini9141a4c2013-04-08 23:03:25 +093032#define VIRTIO_SCSI_VQ_BASE 2
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010033
34/* Command queue element */
35struct virtio_scsi_cmd {
36 struct scsi_cmnd *sc;
37 struct completion *comp;
38 union {
39 struct virtio_scsi_cmd_req cmd;
40 struct virtio_scsi_ctrl_tmf_req tmf;
41 struct virtio_scsi_ctrl_an_req an;
42 } req;
43 union {
44 struct virtio_scsi_cmd_resp cmd;
45 struct virtio_scsi_ctrl_tmf_resp tmf;
46 struct virtio_scsi_ctrl_an_resp an;
47 struct virtio_scsi_event evt;
48 } resp;
49} ____cacheline_aligned_in_smp;
50
Cong Meng365a7152012-07-05 17:06:43 +080051struct virtio_scsi_event_node {
52 struct virtio_scsi *vscsi;
53 struct virtio_scsi_event event;
54 struct work_struct work;
55};
56
Paolo Bonzini139fe452012-06-13 16:56:32 +020057struct virtio_scsi_vq {
58 /* Protects vq */
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010059 spinlock_t vq_lock;
60
Paolo Bonzini139fe452012-06-13 16:56:32 +020061 struct virtqueue *vq;
62};
63
Paolo Bonzini9141a4c2013-04-08 23:03:25 +093064/*
65 * Per-target queue state.
66 *
67 * This struct holds the data needed by the queue steering policy. When a
68 * target is sent multiple requests, we need to drive them to the same queue so
69 * that FIFO processing order is kept. However, if a target was idle, we can
70 * choose a queue arbitrarily. In this case the queue is chosen according to
71 * the current VCPU, so the driver expects the number of request queues to be
72 * equal to the number of VCPUs. This makes it easy and fast to select the
73 * queue, and also lets the driver optimize the IRQ affinity for the virtqueues
74 * (each virtqueue's affinity is set to the CPU that "owns" the queue).
75 *
Ming Leif259d9b2014-05-08 15:23:45 +080076 * tgt_lock is held to serialize reading and writing req_vq. Reading req_vq
77 * could be done locklessly, but we do not do it yet.
Paolo Bonzini9141a4c2013-04-08 23:03:25 +093078 *
Ming Leif259d9b2014-05-08 15:23:45 +080079 * Decrements of reqs are never concurrent with writes of req_vq: before the
80 * decrement reqs will be != 0; after the decrement the virtqueue completion
81 * routine will not use the req_vq so it can be changed by a new request.
Paolo Bonzini9141a4c2013-04-08 23:03:25 +093082 * Thus they can happen outside the tgt_lock, provided of course we make reqs
83 * an atomic_t.
84 */
Paolo Bonzini2bd37f02012-06-13 16:56:34 +020085struct virtio_scsi_target_state {
Paolo Bonzini9141a4c2013-04-08 23:03:25 +093086 /* This spinlock never held at the same time as vq_lock. */
Paolo Bonzini2bd37f02012-06-13 16:56:34 +020087 spinlock_t tgt_lock;
Paolo Bonzini9141a4c2013-04-08 23:03:25 +093088
89 /* Count of outstanding requests. */
90 atomic_t reqs;
91
92 /* Currently active virtqueue for requests sent to this target. */
93 struct virtio_scsi_vq *req_vq;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +020094};
95
Paolo Bonzini139fe452012-06-13 16:56:32 +020096/* Driver instance state */
97struct virtio_scsi {
Paolo Bonzini4fe74b12012-02-05 12:16:00 +010098 struct virtio_device *vdev;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +020099
Cong Meng365a7152012-07-05 17:06:43 +0800100 /* Get some buffers ready for event vq */
101 struct virtio_scsi_event_node event_list[VIRTIO_SCSI_EVENT_LEN];
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930102
103 u32 num_queues;
104
105 /* If the affinity hint is set for virtqueues */
106 bool affinity_hint_set;
107
Wanlong Gao285e71e2013-04-08 23:05:49 +0930108 /* CPU hotplug notifier */
109 struct notifier_block nb;
110
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930111 struct virtio_scsi_vq ctrl_vq;
112 struct virtio_scsi_vq event_vq;
113 struct virtio_scsi_vq req_vqs[];
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100114};
115
116static struct kmem_cache *virtscsi_cmd_cache;
117static mempool_t *virtscsi_cmd_pool;
118
119static inline struct Scsi_Host *virtio_scsi_host(struct virtio_device *vdev)
120{
121 return vdev->priv;
122}
123
124static void virtscsi_compute_resid(struct scsi_cmnd *sc, u32 resid)
125{
126 if (!resid)
127 return;
128
129 if (!scsi_bidi_cmnd(sc)) {
130 scsi_set_resid(sc, resid);
131 return;
132 }
133
134 scsi_in(sc)->resid = min(resid, scsi_in(sc)->length);
135 scsi_out(sc)->resid = resid - scsi_in(sc)->resid;
136}
137
138/**
139 * virtscsi_complete_cmd - finish a scsi_cmd and invoke scsi_done
140 *
141 * Called with vq_lock held.
142 */
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930143static void virtscsi_complete_cmd(struct virtio_scsi *vscsi, void *buf)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100144{
145 struct virtio_scsi_cmd *cmd = buf;
146 struct scsi_cmnd *sc = cmd->sc;
147 struct virtio_scsi_cmd_resp *resp = &cmd->resp.cmd;
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930148 struct virtio_scsi_target_state *tgt =
149 scsi_target(sc->device)->hostdata;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100150
151 dev_dbg(&sc->device->sdev_gendev,
152 "cmd %p response %u status %#02x sense_len %u\n",
153 sc, resp->response, resp->status, resp->sense_len);
154
155 sc->result = resp->status;
156 virtscsi_compute_resid(sc, resp->resid);
157 switch (resp->response) {
158 case VIRTIO_SCSI_S_OK:
159 set_host_byte(sc, DID_OK);
160 break;
161 case VIRTIO_SCSI_S_OVERRUN:
162 set_host_byte(sc, DID_ERROR);
163 break;
164 case VIRTIO_SCSI_S_ABORTED:
165 set_host_byte(sc, DID_ABORT);
166 break;
167 case VIRTIO_SCSI_S_BAD_TARGET:
168 set_host_byte(sc, DID_BAD_TARGET);
169 break;
170 case VIRTIO_SCSI_S_RESET:
171 set_host_byte(sc, DID_RESET);
172 break;
173 case VIRTIO_SCSI_S_BUSY:
174 set_host_byte(sc, DID_BUS_BUSY);
175 break;
176 case VIRTIO_SCSI_S_TRANSPORT_FAILURE:
177 set_host_byte(sc, DID_TRANSPORT_DISRUPTED);
178 break;
179 case VIRTIO_SCSI_S_TARGET_FAILURE:
180 set_host_byte(sc, DID_TARGET_FAILURE);
181 break;
182 case VIRTIO_SCSI_S_NEXUS_FAILURE:
183 set_host_byte(sc, DID_NEXUS_FAILURE);
184 break;
185 default:
186 scmd_printk(KERN_WARNING, sc, "Unknown response %d",
187 resp->response);
188 /* fall through */
189 case VIRTIO_SCSI_S_FAILURE:
190 set_host_byte(sc, DID_ERROR);
191 break;
192 }
193
194 WARN_ON(resp->sense_len > VIRTIO_SCSI_SENSE_SIZE);
195 if (sc->sense_buffer) {
196 memcpy(sc->sense_buffer, resp->sense,
197 min_t(u32, resp->sense_len, VIRTIO_SCSI_SENSE_SIZE));
198 if (resp->sense_len)
199 set_driver_byte(sc, DRIVER_SENSE);
200 }
201
202 mempool_free(cmd, virtscsi_cmd_pool);
203 sc->scsi_done(sc);
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930204
205 atomic_dec(&tgt->reqs);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100206}
207
Paolo Bonzini10f34f62013-04-08 23:02:07 +0930208static void virtscsi_vq_done(struct virtio_scsi *vscsi,
209 struct virtio_scsi_vq *virtscsi_vq,
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930210 void (*fn)(struct virtio_scsi *vscsi, void *buf))
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100211{
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100212 void *buf;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100213 unsigned int len;
Paolo Bonzini10f34f62013-04-08 23:02:07 +0930214 unsigned long flags;
215 struct virtqueue *vq = virtscsi_vq->vq;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100216
Paolo Bonzini10f34f62013-04-08 23:02:07 +0930217 spin_lock_irqsave(&virtscsi_vq->vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100218 do {
219 virtqueue_disable_cb(vq);
220 while ((buf = virtqueue_get_buf(vq, &len)) != NULL)
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930221 fn(vscsi, buf);
Heinz Graalfs2bf4fd32013-11-11 11:52:43 +1030222
223 if (unlikely(virtqueue_is_broken(vq)))
224 break;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100225 } while (!virtqueue_enable_cb(vq));
Paolo Bonzini10f34f62013-04-08 23:02:07 +0930226 spin_unlock_irqrestore(&virtscsi_vq->vq_lock, flags);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100227}
228
229static void virtscsi_req_done(struct virtqueue *vq)
230{
Paolo Bonzini139fe452012-06-13 16:56:32 +0200231 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
232 struct virtio_scsi *vscsi = shost_priv(sh);
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930233 int index = vq->index - VIRTIO_SCSI_VQ_BASE;
234 struct virtio_scsi_vq *req_vq = &vscsi->req_vqs[index];
Paolo Bonzini139fe452012-06-13 16:56:32 +0200235
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930236 virtscsi_vq_done(vscsi, req_vq, virtscsi_complete_cmd);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100237};
238
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930239static void virtscsi_complete_free(struct virtio_scsi *vscsi, void *buf)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100240{
241 struct virtio_scsi_cmd *cmd = buf;
242
243 if (cmd->comp)
244 complete_all(cmd->comp);
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200245 else
246 mempool_free(cmd, virtscsi_cmd_pool);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100247}
248
249static void virtscsi_ctrl_done(struct virtqueue *vq)
250{
Paolo Bonzini139fe452012-06-13 16:56:32 +0200251 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
252 struct virtio_scsi *vscsi = shost_priv(sh);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200253
Paolo Bonzini10f34f62013-04-08 23:02:07 +0930254 virtscsi_vq_done(vscsi, &vscsi->ctrl_vq, virtscsi_complete_free);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100255};
256
Cong Meng365a7152012-07-05 17:06:43 +0800257static int virtscsi_kick_event(struct virtio_scsi *vscsi,
258 struct virtio_scsi_event_node *event_node)
259{
Rusty Russell4614e512012-10-16 23:56:16 +1030260 int err;
Cong Meng365a7152012-07-05 17:06:43 +0800261 struct scatterlist sg;
262 unsigned long flags;
263
Richard W.M. Jones2e9c9df2012-10-02 17:25:46 +0200264 sg_init_one(&sg, &event_node->event, sizeof(struct virtio_scsi_event));
Cong Meng365a7152012-07-05 17:06:43 +0800265
266 spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags);
267
Rusty Russellbf958292013-03-20 15:44:28 +1030268 err = virtqueue_add_inbuf(vscsi->event_vq.vq, &sg, 1, event_node,
269 GFP_ATOMIC);
Rusty Russell4614e512012-10-16 23:56:16 +1030270 if (!err)
Cong Meng365a7152012-07-05 17:06:43 +0800271 virtqueue_kick(vscsi->event_vq.vq);
272
273 spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags);
274
Rusty Russell4614e512012-10-16 23:56:16 +1030275 return err;
Cong Meng365a7152012-07-05 17:06:43 +0800276}
277
278static int virtscsi_kick_event_all(struct virtio_scsi *vscsi)
279{
280 int i;
281
282 for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) {
283 vscsi->event_list[i].vscsi = vscsi;
284 virtscsi_kick_event(vscsi, &vscsi->event_list[i]);
285 }
286
287 return 0;
288}
289
290static void virtscsi_cancel_event_work(struct virtio_scsi *vscsi)
291{
292 int i;
293
294 for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++)
295 cancel_work_sync(&vscsi->event_list[i].work);
296}
297
298static void virtscsi_handle_transport_reset(struct virtio_scsi *vscsi,
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930299 struct virtio_scsi_event *event)
Cong Meng365a7152012-07-05 17:06:43 +0800300{
301 struct scsi_device *sdev;
302 struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
303 unsigned int target = event->lun[1];
304 unsigned int lun = (event->lun[2] << 8) | event->lun[3];
305
306 switch (event->reason) {
307 case VIRTIO_SCSI_EVT_RESET_RESCAN:
308 scsi_add_device(shost, 0, target, lun);
309 break;
310 case VIRTIO_SCSI_EVT_RESET_REMOVED:
311 sdev = scsi_device_lookup(shost, 0, target, lun);
312 if (sdev) {
313 scsi_remove_device(sdev);
314 scsi_device_put(sdev);
315 } else {
316 pr_err("SCSI device %d 0 %d %d not found\n",
317 shost->host_no, target, lun);
318 }
319 break;
320 default:
321 pr_info("Unsupport virtio scsi event reason %x\n", event->reason);
322 }
323}
324
Paolo Bonzini865b58c2012-10-02 17:25:48 +0200325static void virtscsi_handle_param_change(struct virtio_scsi *vscsi,
326 struct virtio_scsi_event *event)
327{
328 struct scsi_device *sdev;
329 struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
330 unsigned int target = event->lun[1];
331 unsigned int lun = (event->lun[2] << 8) | event->lun[3];
332 u8 asc = event->reason & 255;
333 u8 ascq = event->reason >> 8;
334
335 sdev = scsi_device_lookup(shost, 0, target, lun);
336 if (!sdev) {
337 pr_err("SCSI device %d 0 %d %d not found\n",
338 shost->host_no, target, lun);
339 return;
340 }
341
342 /* Handle "Parameters changed", "Mode parameters changed", and
343 "Capacity data has changed". */
344 if (asc == 0x2a && (ascq == 0x00 || ascq == 0x01 || ascq == 0x09))
345 scsi_rescan_device(&sdev->sdev_gendev);
346
347 scsi_device_put(sdev);
348}
349
Cong Meng365a7152012-07-05 17:06:43 +0800350static void virtscsi_handle_event(struct work_struct *work)
351{
352 struct virtio_scsi_event_node *event_node =
353 container_of(work, struct virtio_scsi_event_node, work);
354 struct virtio_scsi *vscsi = event_node->vscsi;
355 struct virtio_scsi_event *event = &event_node->event;
356
357 if (event->event & VIRTIO_SCSI_T_EVENTS_MISSED) {
358 event->event &= ~VIRTIO_SCSI_T_EVENTS_MISSED;
359 scsi_scan_host(virtio_scsi_host(vscsi->vdev));
360 }
361
362 switch (event->event) {
363 case VIRTIO_SCSI_T_NO_EVENT:
364 break;
365 case VIRTIO_SCSI_T_TRANSPORT_RESET:
366 virtscsi_handle_transport_reset(vscsi, event);
367 break;
Paolo Bonzini865b58c2012-10-02 17:25:48 +0200368 case VIRTIO_SCSI_T_PARAM_CHANGE:
369 virtscsi_handle_param_change(vscsi, event);
370 break;
Cong Meng365a7152012-07-05 17:06:43 +0800371 default:
372 pr_err("Unsupport virtio scsi event %x\n", event->event);
373 }
374 virtscsi_kick_event(vscsi, event_node);
375}
376
Paolo Bonzini7f82b3c2013-04-08 23:01:38 +0930377static void virtscsi_complete_event(struct virtio_scsi *vscsi, void *buf)
Cong Meng365a7152012-07-05 17:06:43 +0800378{
379 struct virtio_scsi_event_node *event_node = buf;
380
381 INIT_WORK(&event_node->work, virtscsi_handle_event);
382 schedule_work(&event_node->work);
383}
384
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100385static void virtscsi_event_done(struct virtqueue *vq)
386{
Paolo Bonzini139fe452012-06-13 16:56:32 +0200387 struct Scsi_Host *sh = virtio_scsi_host(vq->vdev);
388 struct virtio_scsi *vscsi = shost_priv(sh);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200389
Paolo Bonzini10f34f62013-04-08 23:02:07 +0930390 virtscsi_vq_done(vscsi, &vscsi->event_vq, virtscsi_complete_event);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100391};
392
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100393/**
Wanlong Gao682993b2013-03-20 15:44:28 +1030394 * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue
395 * @vq : the struct virtqueue we're talking about
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100396 * @cmd : command structure
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100397 * @req_size : size of the request buffer
398 * @resp_size : size of the response buffer
Wanlong Gao682993b2013-03-20 15:44:28 +1030399 * @gfp : flags to use for memory allocations
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100400 */
Wanlong Gao682993b2013-03-20 15:44:28 +1030401static int virtscsi_add_cmd(struct virtqueue *vq,
402 struct virtio_scsi_cmd *cmd,
403 size_t req_size, size_t resp_size, gfp_t gfp)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100404{
405 struct scsi_cmnd *sc = cmd->sc;
Wanlong Gao682993b2013-03-20 15:44:28 +1030406 struct scatterlist *sgs[4], req, resp;
407 struct sg_table *out, *in;
408 unsigned out_num = 0, in_num = 0;
409
410 out = in = NULL;
411
412 if (sc && sc->sc_data_direction != DMA_NONE) {
413 if (sc->sc_data_direction != DMA_FROM_DEVICE)
414 out = &scsi_out(sc)->table;
415 if (sc->sc_data_direction != DMA_TO_DEVICE)
416 in = &scsi_in(sc)->table;
417 }
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100418
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100419 /* Request header. */
Wanlong Gao682993b2013-03-20 15:44:28 +1030420 sg_init_one(&req, &cmd->req, req_size);
421 sgs[out_num++] = &req;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100422
423 /* Data-out buffer. */
Wanlong Gao682993b2013-03-20 15:44:28 +1030424 if (out)
425 sgs[out_num++] = out->sgl;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100426
427 /* Response header. */
Wanlong Gao682993b2013-03-20 15:44:28 +1030428 sg_init_one(&resp, &cmd->resp, resp_size);
429 sgs[out_num + in_num++] = &resp;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100430
431 /* Data-in buffer */
Wanlong Gao682993b2013-03-20 15:44:28 +1030432 if (in)
433 sgs[out_num + in_num++] = in->sgl;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100434
Wanlong Gao682993b2013-03-20 15:44:28 +1030435 return virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, gfp);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100436}
437
Wanlong Gao682993b2013-03-20 15:44:28 +1030438static int virtscsi_kick_cmd(struct virtio_scsi_vq *vq,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100439 struct virtio_scsi_cmd *cmd,
440 size_t req_size, size_t resp_size, gfp_t gfp)
441{
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100442 unsigned long flags;
Rusty Russell4614e512012-10-16 23:56:16 +1030443 int err;
444 bool needs_kick = false;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100445
Wanlong Gao682993b2013-03-20 15:44:28 +1030446 spin_lock_irqsave(&vq->vq_lock, flags);
447 err = virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size, gfp);
Rusty Russell4614e512012-10-16 23:56:16 +1030448 if (!err)
449 needs_kick = virtqueue_kick_prepare(vq->vq);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100450
Paolo Bonzinibce750b2012-06-13 16:56:33 +0200451 spin_unlock_irqrestore(&vq->vq_lock, flags);
Paolo Bonzini139fe452012-06-13 16:56:32 +0200452
Rusty Russell4614e512012-10-16 23:56:16 +1030453 if (needs_kick)
Paolo Bonzini139fe452012-06-13 16:56:32 +0200454 virtqueue_notify(vq->vq);
Rusty Russell4614e512012-10-16 23:56:16 +1030455 return err;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100456}
457
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930458static int virtscsi_queuecommand(struct virtio_scsi *vscsi,
459 struct virtio_scsi_vq *req_vq,
460 struct scsi_cmnd *sc)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100461{
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100462 struct virtio_scsi_cmd *cmd;
463 int ret;
464
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200465 struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
466 BUG_ON(scsi_sg_count(sc) > shost->sg_tablesize);
467
468 /* TODO: check feature bit and fail if unsupported? */
469 BUG_ON(sc->sc_data_direction == DMA_BIDIRECTIONAL);
470
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100471 dev_dbg(&sc->device->sdev_gendev,
472 "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]);
473
474 ret = SCSI_MLQUEUE_HOST_BUSY;
475 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_ATOMIC);
476 if (!cmd)
477 goto out;
478
479 memset(cmd, 0, sizeof(*cmd));
480 cmd->sc = sc;
481 cmd->req.cmd = (struct virtio_scsi_cmd_req){
482 .lun[0] = 1,
483 .lun[1] = sc->device->id,
484 .lun[2] = (sc->device->lun >> 8) | 0x40,
485 .lun[3] = sc->device->lun & 0xff,
486 .tag = (unsigned long)sc,
487 .task_attr = VIRTIO_SCSI_S_SIMPLE,
488 .prio = 0,
489 .crn = 0,
490 };
491
492 BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE);
493 memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len);
494
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930495 if (virtscsi_kick_cmd(req_vq, cmd,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100496 sizeof cmd->req.cmd, sizeof cmd->resp.cmd,
Rusty Russell4614e512012-10-16 23:56:16 +1030497 GFP_ATOMIC) == 0)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100498 ret = 0;
Eric Northupb56d1002012-11-08 01:55:50 -0800499 else
500 mempool_free(cmd, virtscsi_cmd_pool);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100501
502out:
503 return ret;
504}
505
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930506static int virtscsi_queuecommand_single(struct Scsi_Host *sh,
507 struct scsi_cmnd *sc)
508{
509 struct virtio_scsi *vscsi = shost_priv(sh);
510 struct virtio_scsi_target_state *tgt =
511 scsi_target(sc->device)->hostdata;
512
513 atomic_inc(&tgt->reqs);
514 return virtscsi_queuecommand(vscsi, &vscsi->req_vqs[0], sc);
515}
516
517static struct virtio_scsi_vq *virtscsi_pick_vq(struct virtio_scsi *vscsi,
518 struct virtio_scsi_target_state *tgt)
519{
520 struct virtio_scsi_vq *vq;
521 unsigned long flags;
522 u32 queue_num;
523
524 spin_lock_irqsave(&tgt->tgt_lock, flags);
525
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930526 if (atomic_inc_return(&tgt->reqs) > 1)
Ming Leif259d9b2014-05-08 15:23:45 +0800527 vq = tgt->req_vq;
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930528 else {
529 queue_num = smp_processor_id();
530 while (unlikely(queue_num >= vscsi->num_queues))
531 queue_num -= vscsi->num_queues;
532
533 tgt->req_vq = vq = &vscsi->req_vqs[queue_num];
534 }
535
536 spin_unlock_irqrestore(&tgt->tgt_lock, flags);
537 return vq;
538}
539
540static int virtscsi_queuecommand_multi(struct Scsi_Host *sh,
541 struct scsi_cmnd *sc)
542{
543 struct virtio_scsi *vscsi = shost_priv(sh);
544 struct virtio_scsi_target_state *tgt =
545 scsi_target(sc->device)->hostdata;
546 struct virtio_scsi_vq *req_vq = virtscsi_pick_vq(vscsi, tgt);
547
548 return virtscsi_queuecommand(vscsi, req_vq, sc);
549}
550
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100551static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd)
552{
553 DECLARE_COMPLETION_ONSTACK(comp);
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200554 int ret = FAILED;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100555
556 cmd->comp = &comp;
Wanlong Gao682993b2013-03-20 15:44:28 +1030557 if (virtscsi_kick_cmd(&vscsi->ctrl_vq, cmd,
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200558 sizeof cmd->req.tmf, sizeof cmd->resp.tmf,
559 GFP_NOIO) < 0)
560 goto out;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100561
562 wait_for_completion(&comp);
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200563 if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK ||
564 cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED)
565 ret = SUCCESS;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100566
Paolo Bonzinie4594bb2012-05-04 12:32:04 +0200567out:
568 mempool_free(cmd, virtscsi_cmd_pool);
569 return ret;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100570}
571
572static int virtscsi_device_reset(struct scsi_cmnd *sc)
573{
574 struct virtio_scsi *vscsi = shost_priv(sc->device->host);
575 struct virtio_scsi_cmd *cmd;
576
577 sdev_printk(KERN_INFO, sc->device, "device reset\n");
578 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
579 if (!cmd)
580 return FAILED;
581
582 memset(cmd, 0, sizeof(*cmd));
583 cmd->sc = sc;
584 cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
585 .type = VIRTIO_SCSI_T_TMF,
586 .subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET,
587 .lun[0] = 1,
588 .lun[1] = sc->device->id,
589 .lun[2] = (sc->device->lun >> 8) | 0x40,
590 .lun[3] = sc->device->lun & 0xff,
591 };
592 return virtscsi_tmf(vscsi, cmd);
593}
594
595static int virtscsi_abort(struct scsi_cmnd *sc)
596{
597 struct virtio_scsi *vscsi = shost_priv(sc->device->host);
598 struct virtio_scsi_cmd *cmd;
599
600 scmd_printk(KERN_INFO, sc, "abort\n");
601 cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO);
602 if (!cmd)
603 return FAILED;
604
605 memset(cmd, 0, sizeof(*cmd));
606 cmd->sc = sc;
607 cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){
608 .type = VIRTIO_SCSI_T_TMF,
609 .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK,
610 .lun[0] = 1,
611 .lun[1] = sc->device->id,
612 .lun[2] = (sc->device->lun >> 8) | 0x40,
613 .lun[3] = sc->device->lun & 0xff,
614 .tag = (unsigned long)sc,
615 };
616 return virtscsi_tmf(vscsi, cmd);
617}
618
Wanlong Gao5c370192013-04-08 23:01:16 +0930619static int virtscsi_target_alloc(struct scsi_target *starget)
620{
621 struct virtio_scsi_target_state *tgt =
622 kmalloc(sizeof(*tgt), GFP_KERNEL);
623 if (!tgt)
624 return -ENOMEM;
625
626 spin_lock_init(&tgt->tgt_lock);
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930627 atomic_set(&tgt->reqs, 0);
628 tgt->req_vq = NULL;
Wanlong Gao5c370192013-04-08 23:01:16 +0930629
630 starget->hostdata = tgt;
631 return 0;
632}
633
634static void virtscsi_target_destroy(struct scsi_target *starget)
635{
636 struct virtio_scsi_target_state *tgt = starget->hostdata;
637 kfree(tgt);
638}
639
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930640static struct scsi_host_template virtscsi_host_template_single = {
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100641 .module = THIS_MODULE,
642 .name = "Virtio SCSI HBA",
643 .proc_name = "virtio_scsi",
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100644 .this_id = -1,
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930645 .queuecommand = virtscsi_queuecommand_single,
646 .eh_abort_handler = virtscsi_abort,
647 .eh_device_reset_handler = virtscsi_device_reset,
648
649 .can_queue = 1024,
650 .dma_boundary = UINT_MAX,
651 .use_clustering = ENABLE_CLUSTERING,
652 .target_alloc = virtscsi_target_alloc,
653 .target_destroy = virtscsi_target_destroy,
654};
655
656static struct scsi_host_template virtscsi_host_template_multi = {
657 .module = THIS_MODULE,
658 .name = "Virtio SCSI HBA",
659 .proc_name = "virtio_scsi",
660 .this_id = -1,
661 .queuecommand = virtscsi_queuecommand_multi,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100662 .eh_abort_handler = virtscsi_abort,
663 .eh_device_reset_handler = virtscsi_device_reset,
664
665 .can_queue = 1024,
666 .dma_boundary = UINT_MAX,
667 .use_clustering = ENABLE_CLUSTERING,
Wanlong Gao5c370192013-04-08 23:01:16 +0930668 .target_alloc = virtscsi_target_alloc,
669 .target_destroy = virtscsi_target_destroy,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100670};
671
672#define virtscsi_config_get(vdev, fld) \
673 ({ \
674 typeof(((struct virtio_scsi_config *)0)->fld) __val; \
Rusty Russell855e0c52013-10-14 18:11:51 +1030675 virtio_cread(vdev, struct virtio_scsi_config, fld, &__val); \
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100676 __val; \
677 })
678
679#define virtscsi_config_set(vdev, fld, val) \
Rusty Russell855e0c52013-10-14 18:11:51 +1030680 do { \
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100681 typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \
Rusty Russell855e0c52013-10-14 18:11:51 +1030682 virtio_cwrite(vdev, struct virtio_scsi_config, fld, &__val); \
683 } while(0)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100684
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930685static void __virtscsi_set_affinity(struct virtio_scsi *vscsi, bool affinity)
686{
687 int i;
688 int cpu;
689
690 /* In multiqueue mode, when the number of cpu is equal
691 * to the number of request queues, we let the qeueues
692 * to be private to one cpu by setting the affinity hint
693 * to eliminate the contention.
694 */
695 if ((vscsi->num_queues == 1 ||
696 vscsi->num_queues != num_online_cpus()) && affinity) {
697 if (vscsi->affinity_hint_set)
698 affinity = false;
699 else
700 return;
701 }
702
703 if (affinity) {
704 i = 0;
705 for_each_online_cpu(cpu) {
706 virtqueue_set_affinity(vscsi->req_vqs[i].vq, cpu);
707 i++;
708 }
709
710 vscsi->affinity_hint_set = true;
711 } else {
Fam Zheng0c8482a2014-04-14 10:16:09 +0800712 for (i = 0; i < vscsi->num_queues; i++) {
713 if (!vscsi->req_vqs[i].vq)
714 continue;
715
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930716 virtqueue_set_affinity(vscsi->req_vqs[i].vq, -1);
Fam Zheng0c8482a2014-04-14 10:16:09 +0800717 }
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930718
719 vscsi->affinity_hint_set = false;
720 }
721}
722
723static void virtscsi_set_affinity(struct virtio_scsi *vscsi, bool affinity)
724{
725 get_online_cpus();
726 __virtscsi_set_affinity(vscsi, affinity);
727 put_online_cpus();
728}
729
Wanlong Gao285e71e2013-04-08 23:05:49 +0930730static int virtscsi_cpu_callback(struct notifier_block *nfb,
731 unsigned long action, void *hcpu)
732{
733 struct virtio_scsi *vscsi = container_of(nfb, struct virtio_scsi, nb);
734 switch(action) {
735 case CPU_ONLINE:
736 case CPU_ONLINE_FROZEN:
737 case CPU_DEAD:
738 case CPU_DEAD_FROZEN:
739 __virtscsi_set_affinity(vscsi, true);
740 break;
741 default:
742 break;
743 }
744 return NOTIFY_OK;
745}
746
Paolo Bonzini139fe452012-06-13 16:56:32 +0200747static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq,
748 struct virtqueue *vq)
749{
750 spin_lock_init(&virtscsi_vq->vq_lock);
751 virtscsi_vq->vq = vq;
752}
753
Nicholas Bellinger59057fb2012-07-11 21:22:16 +0000754static void virtscsi_scan(struct virtio_device *vdev)
755{
756 struct Scsi_Host *shost = (struct Scsi_Host *)vdev->priv;
757
758 scsi_scan_host(shost);
759}
760
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200761static void virtscsi_remove_vqs(struct virtio_device *vdev)
762{
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930763 struct Scsi_Host *sh = virtio_scsi_host(vdev);
764 struct virtio_scsi *vscsi = shost_priv(sh);
765
766 virtscsi_set_affinity(vscsi, false);
767
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200768 /* Stop all the virtqueues. */
769 vdev->config->reset(vdev);
770
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200771 vdev->config->del_vqs(vdev);
772}
773
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100774static int virtscsi_init(struct virtio_device *vdev,
Wanlong Gao5c370192013-04-08 23:01:16 +0930775 struct virtio_scsi *vscsi)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100776{
777 int err;
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930778 u32 i;
779 u32 num_vqs;
780 vq_callback_t **callbacks;
781 const char **names;
782 struct virtqueue **vqs;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200783
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930784 num_vqs = vscsi->num_queues + VIRTIO_SCSI_VQ_BASE;
785 vqs = kmalloc(num_vqs * sizeof(struct virtqueue *), GFP_KERNEL);
786 callbacks = kmalloc(num_vqs * sizeof(vq_callback_t *), GFP_KERNEL);
787 names = kmalloc(num_vqs * sizeof(char *), GFP_KERNEL);
788
789 if (!callbacks || !vqs || !names) {
790 err = -ENOMEM;
791 goto out;
792 }
793
794 callbacks[0] = virtscsi_ctrl_done;
795 callbacks[1] = virtscsi_event_done;
796 names[0] = "control";
797 names[1] = "event";
798 for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++) {
799 callbacks[i] = virtscsi_req_done;
800 names[i] = "request";
801 }
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100802
803 /* Discover virtqueues and write information to configuration. */
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930804 err = vdev->config->find_vqs(vdev, num_vqs, vqs, callbacks, names);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100805 if (err)
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930806 goto out;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100807
Paolo Bonzini139fe452012-06-13 16:56:32 +0200808 virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]);
809 virtscsi_init_vq(&vscsi->event_vq, vqs[1]);
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930810 for (i = VIRTIO_SCSI_VQ_BASE; i < num_vqs; i++)
811 virtscsi_init_vq(&vscsi->req_vqs[i - VIRTIO_SCSI_VQ_BASE],
812 vqs[i]);
813
814 virtscsi_set_affinity(vscsi, true);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100815
816 virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE);
817 virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE);
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200818
Cong Meng365a7152012-07-05 17:06:43 +0800819 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
820 virtscsi_kick_event_all(vscsi);
821
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930822 err = 0;
823
824out:
825 kfree(names);
826 kfree(callbacks);
827 kfree(vqs);
828 if (err)
829 virtscsi_remove_vqs(vdev);
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200830 return err;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100831}
832
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800833static int virtscsi_probe(struct virtio_device *vdev)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100834{
835 struct Scsi_Host *shost;
836 struct virtio_scsi *vscsi;
837 int err;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200838 u32 sg_elems, num_targets;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100839 u32 cmd_per_lun;
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930840 u32 num_queues;
841 struct scsi_host_template *hostt;
842
843 /* We need to know how many queues before we allocate. */
844 num_queues = virtscsi_config_get(vdev, num_queues) ? : 1;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100845
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200846 num_targets = virtscsi_config_get(vdev, max_target) + 1;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100847
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930848 if (num_queues == 1)
849 hostt = &virtscsi_host_template_single;
850 else
851 hostt = &virtscsi_host_template_multi;
852
853 shost = scsi_host_alloc(hostt,
854 sizeof(*vscsi) + sizeof(vscsi->req_vqs[0]) * num_queues);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100855 if (!shost)
856 return -ENOMEM;
857
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200858 sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100859 shost->sg_tablesize = sg_elems;
860 vscsi = shost_priv(shost);
861 vscsi->vdev = vdev;
Paolo Bonzini9141a4c2013-04-08 23:03:25 +0930862 vscsi->num_queues = num_queues;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100863 vdev->priv = shost;
864
Wanlong Gao5c370192013-04-08 23:01:16 +0930865 err = virtscsi_init(vdev, vscsi);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100866 if (err)
867 goto virtscsi_init_failed;
868
Wanlong Gao285e71e2013-04-08 23:05:49 +0930869 vscsi->nb.notifier_call = &virtscsi_cpu_callback;
870 err = register_hotcpu_notifier(&vscsi->nb);
871 if (err) {
872 pr_err("registering cpu notifier failed\n");
873 goto scsi_add_host_failed;
874 }
875
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100876 cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1;
877 shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue);
878 shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF;
Paolo Bonzini9da5f5a2012-10-02 17:25:47 +0200879
880 /* LUNs > 256 are reported with format 1, so they go in the range
881 * 16640-32767.
882 */
883 shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1 + 0x4000;
Paolo Bonzini2bd37f02012-06-13 16:56:34 +0200884 shost->max_id = num_targets;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100885 shost->max_channel = 0;
886 shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE;
887 err = scsi_add_host(shost, &vdev->dev);
888 if (err)
889 goto scsi_add_host_failed;
Nicholas Bellinger59057fb2012-07-11 21:22:16 +0000890 /*
891 * scsi_scan_host() happens in virtscsi_scan() via virtio_driver->scan()
892 * after VIRTIO_CONFIG_S_DRIVER_OK has been set..
893 */
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100894 return 0;
895
896scsi_add_host_failed:
897 vdev->config->del_vqs(vdev);
898virtscsi_init_failed:
899 scsi_host_put(shost);
900 return err;
901}
902
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800903static void virtscsi_remove(struct virtio_device *vdev)
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100904{
905 struct Scsi_Host *shost = virtio_scsi_host(vdev);
Cong Meng365a7152012-07-05 17:06:43 +0800906 struct virtio_scsi *vscsi = shost_priv(shost);
907
908 if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG))
909 virtscsi_cancel_event_work(vscsi);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100910
911 scsi_remove_host(shost);
912
Wanlong Gao285e71e2013-04-08 23:05:49 +0930913 unregister_hotcpu_notifier(&vscsi->nb);
914
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100915 virtscsi_remove_vqs(vdev);
916 scsi_host_put(shost);
917}
918
Aaron Lu89107002013-09-17 09:25:23 +0930919#ifdef CONFIG_PM_SLEEP
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100920static int virtscsi_freeze(struct virtio_device *vdev)
921{
Asias Hef466f752014-01-16 10:18:48 +1030922 struct Scsi_Host *sh = virtio_scsi_host(vdev);
923 struct virtio_scsi *vscsi = shost_priv(sh);
924
925 unregister_hotcpu_notifier(&vscsi->nb);
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100926 virtscsi_remove_vqs(vdev);
927 return 0;
928}
929
930static int virtscsi_restore(struct virtio_device *vdev)
931{
932 struct Scsi_Host *sh = virtio_scsi_host(vdev);
933 struct virtio_scsi *vscsi = shost_priv(sh);
Asias Hef466f752014-01-16 10:18:48 +1030934 int err;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100935
Asias Hef466f752014-01-16 10:18:48 +1030936 err = virtscsi_init(vdev, vscsi);
937 if (err)
938 return err;
939
940 err = register_hotcpu_notifier(&vscsi->nb);
941 if (err)
942 vdev->config->del_vqs(vdev);
943
944 return err;
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100945}
946#endif
947
948static struct virtio_device_id id_table[] = {
949 { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID },
950 { 0 },
951};
952
Cong Meng365a7152012-07-05 17:06:43 +0800953static unsigned int features[] = {
Paolo Bonzini865b58c2012-10-02 17:25:48 +0200954 VIRTIO_SCSI_F_HOTPLUG,
955 VIRTIO_SCSI_F_CHANGE,
Cong Meng365a7152012-07-05 17:06:43 +0800956};
957
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100958static struct virtio_driver virtio_scsi_driver = {
Cong Meng365a7152012-07-05 17:06:43 +0800959 .feature_table = features,
960 .feature_table_size = ARRAY_SIZE(features),
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100961 .driver.name = KBUILD_MODNAME,
962 .driver.owner = THIS_MODULE,
963 .id_table = id_table,
964 .probe = virtscsi_probe,
Nicholas Bellinger59057fb2012-07-11 21:22:16 +0000965 .scan = virtscsi_scan,
Aaron Lu89107002013-09-17 09:25:23 +0930966#ifdef CONFIG_PM_SLEEP
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100967 .freeze = virtscsi_freeze,
968 .restore = virtscsi_restore,
969#endif
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800970 .remove = virtscsi_remove,
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100971};
972
973static int __init init(void)
974{
975 int ret = -ENOMEM;
976
977 virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0);
978 if (!virtscsi_cmd_cache) {
Wanlong Gaoba06d1e2013-03-12 15:34:40 +1030979 pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n");
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100980 goto error;
981 }
982
983
984 virtscsi_cmd_pool =
985 mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ,
986 virtscsi_cmd_cache);
987 if (!virtscsi_cmd_pool) {
Wanlong Gaoba06d1e2013-03-12 15:34:40 +1030988 pr_err("mempool_create() for virtscsi_cmd_pool failed\n");
Paolo Bonzini4fe74b12012-02-05 12:16:00 +0100989 goto error;
990 }
991 ret = register_virtio_driver(&virtio_scsi_driver);
992 if (ret < 0)
993 goto error;
994
995 return 0;
996
997error:
998 if (virtscsi_cmd_pool) {
999 mempool_destroy(virtscsi_cmd_pool);
1000 virtscsi_cmd_pool = NULL;
1001 }
1002 if (virtscsi_cmd_cache) {
1003 kmem_cache_destroy(virtscsi_cmd_cache);
1004 virtscsi_cmd_cache = NULL;
1005 }
1006 return ret;
1007}
1008
1009static void __exit fini(void)
1010{
1011 unregister_virtio_driver(&virtio_scsi_driver);
1012 mempool_destroy(virtscsi_cmd_pool);
1013 kmem_cache_destroy(virtscsi_cmd_cache);
1014}
1015module_init(init);
1016module_exit(fini);
1017
1018MODULE_DEVICE_TABLE(virtio, id_table);
1019MODULE_DESCRIPTION("Virtio SCSI HBA driver");
1020MODULE_LICENSE("GPL");