]>
Commit | Line | Data |
---|---|---|
4fe74b1c PB |
1 | /* |
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 | ||
ba06d1e1 WG |
16 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
17 | ||
4fe74b1c PB |
18 | #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 | |
365a7150 | 30 | #define VIRTIO_SCSI_EVENT_LEN 8 |
4fe74b1c PB |
31 | |
32 | /* Command queue element */ | |
33 | struct 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 | ||
365a7150 CM |
49 | struct virtio_scsi_event_node { |
50 | struct virtio_scsi *vscsi; | |
51 | struct virtio_scsi_event event; | |
52 | struct work_struct work; | |
53 | }; | |
54 | ||
139fe45a PB |
55 | struct virtio_scsi_vq { |
56 | /* Protects vq */ | |
57 | spinlock_t vq_lock; | |
58 | ||
59 | struct virtqueue *vq; | |
60 | }; | |
61 | ||
2bd37f0f PB |
62 | /* Per-target queue state */ |
63 | struct virtio_scsi_target_state { | |
682993b4 | 64 | /* Never held at the same time as vq_lock. */ |
2bd37f0f | 65 | spinlock_t tgt_lock; |
2bd37f0f PB |
66 | }; |
67 | ||
4fe74b1c PB |
68 | /* Driver instance state */ |
69 | struct virtio_scsi { | |
4fe74b1c | 70 | struct virtio_device *vdev; |
2bd37f0f | 71 | |
139fe45a PB |
72 | struct virtio_scsi_vq ctrl_vq; |
73 | struct virtio_scsi_vq event_vq; | |
74 | struct virtio_scsi_vq req_vq; | |
4fe74b1c | 75 | |
365a7150 CM |
76 | /* Get some buffers ready for event vq */ |
77 | struct virtio_scsi_event_node event_list[VIRTIO_SCSI_EVENT_LEN]; | |
4fe74b1c PB |
78 | }; |
79 | ||
80 | static struct kmem_cache *virtscsi_cmd_cache; | |
81 | static mempool_t *virtscsi_cmd_pool; | |
82 | ||
83 | static inline struct Scsi_Host *virtio_scsi_host(struct virtio_device *vdev) | |
84 | { | |
85 | return vdev->priv; | |
86 | } | |
87 | ||
88 | static void virtscsi_compute_resid(struct scsi_cmnd *sc, u32 resid) | |
89 | { | |
90 | if (!resid) | |
91 | return; | |
92 | ||
93 | if (!scsi_bidi_cmnd(sc)) { | |
94 | scsi_set_resid(sc, resid); | |
95 | return; | |
96 | } | |
97 | ||
98 | scsi_in(sc)->resid = min(resid, scsi_in(sc)->length); | |
99 | scsi_out(sc)->resid = resid - scsi_in(sc)->resid; | |
100 | } | |
101 | ||
102 | /** | |
103 | * virtscsi_complete_cmd - finish a scsi_cmd and invoke scsi_done | |
104 | * | |
105 | * Called with vq_lock held. | |
106 | */ | |
7f82b3c9 | 107 | static void virtscsi_complete_cmd(struct virtio_scsi *vscsi, void *buf) |
4fe74b1c PB |
108 | { |
109 | struct virtio_scsi_cmd *cmd = buf; | |
110 | struct scsi_cmnd *sc = cmd->sc; | |
111 | struct virtio_scsi_cmd_resp *resp = &cmd->resp.cmd; | |
112 | ||
113 | dev_dbg(&sc->device->sdev_gendev, | |
114 | "cmd %p response %u status %#02x sense_len %u\n", | |
115 | sc, resp->response, resp->status, resp->sense_len); | |
116 | ||
117 | sc->result = resp->status; | |
118 | virtscsi_compute_resid(sc, resp->resid); | |
119 | switch (resp->response) { | |
120 | case VIRTIO_SCSI_S_OK: | |
121 | set_host_byte(sc, DID_OK); | |
122 | break; | |
123 | case VIRTIO_SCSI_S_OVERRUN: | |
124 | set_host_byte(sc, DID_ERROR); | |
125 | break; | |
126 | case VIRTIO_SCSI_S_ABORTED: | |
127 | set_host_byte(sc, DID_ABORT); | |
128 | break; | |
129 | case VIRTIO_SCSI_S_BAD_TARGET: | |
130 | set_host_byte(sc, DID_BAD_TARGET); | |
131 | break; | |
132 | case VIRTIO_SCSI_S_RESET: | |
133 | set_host_byte(sc, DID_RESET); | |
134 | break; | |
135 | case VIRTIO_SCSI_S_BUSY: | |
136 | set_host_byte(sc, DID_BUS_BUSY); | |
137 | break; | |
138 | case VIRTIO_SCSI_S_TRANSPORT_FAILURE: | |
139 | set_host_byte(sc, DID_TRANSPORT_DISRUPTED); | |
140 | break; | |
141 | case VIRTIO_SCSI_S_TARGET_FAILURE: | |
142 | set_host_byte(sc, DID_TARGET_FAILURE); | |
143 | break; | |
144 | case VIRTIO_SCSI_S_NEXUS_FAILURE: | |
145 | set_host_byte(sc, DID_NEXUS_FAILURE); | |
146 | break; | |
147 | default: | |
148 | scmd_printk(KERN_WARNING, sc, "Unknown response %d", | |
149 | resp->response); | |
150 | /* fall through */ | |
151 | case VIRTIO_SCSI_S_FAILURE: | |
152 | set_host_byte(sc, DID_ERROR); | |
153 | break; | |
154 | } | |
155 | ||
156 | WARN_ON(resp->sense_len > VIRTIO_SCSI_SENSE_SIZE); | |
157 | if (sc->sense_buffer) { | |
158 | memcpy(sc->sense_buffer, resp->sense, | |
159 | min_t(u32, resp->sense_len, VIRTIO_SCSI_SENSE_SIZE)); | |
160 | if (resp->sense_len) | |
161 | set_driver_byte(sc, DRIVER_SENSE); | |
162 | } | |
163 | ||
164 | mempool_free(cmd, virtscsi_cmd_pool); | |
165 | sc->scsi_done(sc); | |
166 | } | |
167 | ||
7f82b3c9 PB |
168 | static void virtscsi_vq_done(struct virtio_scsi *vscsi, struct virtqueue *vq, |
169 | void (*fn)(struct virtio_scsi *vscsi, void *buf)) | |
4fe74b1c | 170 | { |
4fe74b1c | 171 | void *buf; |
4fe74b1c PB |
172 | unsigned int len; |
173 | ||
4fe74b1c PB |
174 | do { |
175 | virtqueue_disable_cb(vq); | |
176 | while ((buf = virtqueue_get_buf(vq, &len)) != NULL) | |
7f82b3c9 | 177 | fn(vscsi, buf); |
4fe74b1c | 178 | } while (!virtqueue_enable_cb(vq)); |
4fe74b1c PB |
179 | } |
180 | ||
181 | static void virtscsi_req_done(struct virtqueue *vq) | |
182 | { | |
139fe45a PB |
183 | struct Scsi_Host *sh = virtio_scsi_host(vq->vdev); |
184 | struct virtio_scsi *vscsi = shost_priv(sh); | |
185 | unsigned long flags; | |
186 | ||
187 | spin_lock_irqsave(&vscsi->req_vq.vq_lock, flags); | |
7f82b3c9 | 188 | virtscsi_vq_done(vscsi, vq, virtscsi_complete_cmd); |
139fe45a | 189 | spin_unlock_irqrestore(&vscsi->req_vq.vq_lock, flags); |
4fe74b1c PB |
190 | }; |
191 | ||
7f82b3c9 | 192 | static void virtscsi_complete_free(struct virtio_scsi *vscsi, void *buf) |
4fe74b1c PB |
193 | { |
194 | struct virtio_scsi_cmd *cmd = buf; | |
195 | ||
196 | if (cmd->comp) | |
197 | complete_all(cmd->comp); | |
e4594bb5 PB |
198 | else |
199 | mempool_free(cmd, virtscsi_cmd_pool); | |
4fe74b1c PB |
200 | } |
201 | ||
202 | static void virtscsi_ctrl_done(struct virtqueue *vq) | |
203 | { | |
139fe45a PB |
204 | struct Scsi_Host *sh = virtio_scsi_host(vq->vdev); |
205 | struct virtio_scsi *vscsi = shost_priv(sh); | |
206 | unsigned long flags; | |
207 | ||
208 | spin_lock_irqsave(&vscsi->ctrl_vq.vq_lock, flags); | |
7f82b3c9 | 209 | virtscsi_vq_done(vscsi, vq, virtscsi_complete_free); |
139fe45a | 210 | spin_unlock_irqrestore(&vscsi->ctrl_vq.vq_lock, flags); |
4fe74b1c PB |
211 | }; |
212 | ||
365a7150 CM |
213 | static int virtscsi_kick_event(struct virtio_scsi *vscsi, |
214 | struct virtio_scsi_event_node *event_node) | |
215 | { | |
4614e51c | 216 | int err; |
365a7150 CM |
217 | struct scatterlist sg; |
218 | unsigned long flags; | |
219 | ||
2e9c9dfd | 220 | sg_init_one(&sg, &event_node->event, sizeof(struct virtio_scsi_event)); |
365a7150 CM |
221 | |
222 | spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags); | |
223 | ||
bf958291 RR |
224 | err = virtqueue_add_inbuf(vscsi->event_vq.vq, &sg, 1, event_node, |
225 | GFP_ATOMIC); | |
4614e51c | 226 | if (!err) |
365a7150 CM |
227 | virtqueue_kick(vscsi->event_vq.vq); |
228 | ||
229 | spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags); | |
230 | ||
4614e51c | 231 | return err; |
365a7150 CM |
232 | } |
233 | ||
234 | static int virtscsi_kick_event_all(struct virtio_scsi *vscsi) | |
235 | { | |
236 | int i; | |
237 | ||
238 | for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) { | |
239 | vscsi->event_list[i].vscsi = vscsi; | |
240 | virtscsi_kick_event(vscsi, &vscsi->event_list[i]); | |
241 | } | |
242 | ||
243 | return 0; | |
244 | } | |
245 | ||
246 | static void virtscsi_cancel_event_work(struct virtio_scsi *vscsi) | |
247 | { | |
248 | int i; | |
249 | ||
250 | for (i = 0; i < VIRTIO_SCSI_EVENT_LEN; i++) | |
251 | cancel_work_sync(&vscsi->event_list[i].work); | |
252 | } | |
253 | ||
254 | static void virtscsi_handle_transport_reset(struct virtio_scsi *vscsi, | |
255 | struct virtio_scsi_event *event) | |
256 | { | |
257 | struct scsi_device *sdev; | |
258 | struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev); | |
259 | unsigned int target = event->lun[1]; | |
260 | unsigned int lun = (event->lun[2] << 8) | event->lun[3]; | |
261 | ||
262 | switch (event->reason) { | |
263 | case VIRTIO_SCSI_EVT_RESET_RESCAN: | |
264 | scsi_add_device(shost, 0, target, lun); | |
265 | break; | |
266 | case VIRTIO_SCSI_EVT_RESET_REMOVED: | |
267 | sdev = scsi_device_lookup(shost, 0, target, lun); | |
268 | if (sdev) { | |
269 | scsi_remove_device(sdev); | |
270 | scsi_device_put(sdev); | |
271 | } else { | |
272 | pr_err("SCSI device %d 0 %d %d not found\n", | |
273 | shost->host_no, target, lun); | |
274 | } | |
275 | break; | |
276 | default: | |
277 | pr_info("Unsupport virtio scsi event reason %x\n", event->reason); | |
278 | } | |
279 | } | |
280 | ||
865b58c0 PB |
281 | static void virtscsi_handle_param_change(struct virtio_scsi *vscsi, |
282 | struct virtio_scsi_event *event) | |
283 | { | |
284 | struct scsi_device *sdev; | |
285 | struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev); | |
286 | unsigned int target = event->lun[1]; | |
287 | unsigned int lun = (event->lun[2] << 8) | event->lun[3]; | |
288 | u8 asc = event->reason & 255; | |
289 | u8 ascq = event->reason >> 8; | |
290 | ||
291 | sdev = scsi_device_lookup(shost, 0, target, lun); | |
292 | if (!sdev) { | |
293 | pr_err("SCSI device %d 0 %d %d not found\n", | |
294 | shost->host_no, target, lun); | |
295 | return; | |
296 | } | |
297 | ||
298 | /* Handle "Parameters changed", "Mode parameters changed", and | |
299 | "Capacity data has changed". */ | |
300 | if (asc == 0x2a && (ascq == 0x00 || ascq == 0x01 || ascq == 0x09)) | |
301 | scsi_rescan_device(&sdev->sdev_gendev); | |
302 | ||
303 | scsi_device_put(sdev); | |
304 | } | |
305 | ||
365a7150 CM |
306 | static void virtscsi_handle_event(struct work_struct *work) |
307 | { | |
308 | struct virtio_scsi_event_node *event_node = | |
309 | container_of(work, struct virtio_scsi_event_node, work); | |
310 | struct virtio_scsi *vscsi = event_node->vscsi; | |
311 | struct virtio_scsi_event *event = &event_node->event; | |
312 | ||
313 | if (event->event & VIRTIO_SCSI_T_EVENTS_MISSED) { | |
314 | event->event &= ~VIRTIO_SCSI_T_EVENTS_MISSED; | |
315 | scsi_scan_host(virtio_scsi_host(vscsi->vdev)); | |
316 | } | |
317 | ||
318 | switch (event->event) { | |
319 | case VIRTIO_SCSI_T_NO_EVENT: | |
320 | break; | |
321 | case VIRTIO_SCSI_T_TRANSPORT_RESET: | |
322 | virtscsi_handle_transport_reset(vscsi, event); | |
323 | break; | |
865b58c0 PB |
324 | case VIRTIO_SCSI_T_PARAM_CHANGE: |
325 | virtscsi_handle_param_change(vscsi, event); | |
326 | break; | |
365a7150 CM |
327 | default: |
328 | pr_err("Unsupport virtio scsi event %x\n", event->event); | |
329 | } | |
330 | virtscsi_kick_event(vscsi, event_node); | |
331 | } | |
332 | ||
7f82b3c9 | 333 | static void virtscsi_complete_event(struct virtio_scsi *vscsi, void *buf) |
365a7150 CM |
334 | { |
335 | struct virtio_scsi_event_node *event_node = buf; | |
336 | ||
337 | INIT_WORK(&event_node->work, virtscsi_handle_event); | |
338 | schedule_work(&event_node->work); | |
339 | } | |
340 | ||
4fe74b1c PB |
341 | static void virtscsi_event_done(struct virtqueue *vq) |
342 | { | |
139fe45a PB |
343 | struct Scsi_Host *sh = virtio_scsi_host(vq->vdev); |
344 | struct virtio_scsi *vscsi = shost_priv(sh); | |
345 | unsigned long flags; | |
346 | ||
347 | spin_lock_irqsave(&vscsi->event_vq.vq_lock, flags); | |
7f82b3c9 | 348 | virtscsi_vq_done(vscsi, vq, virtscsi_complete_event); |
139fe45a | 349 | spin_unlock_irqrestore(&vscsi->event_vq.vq_lock, flags); |
4fe74b1c PB |
350 | }; |
351 | ||
4fe74b1c | 352 | /** |
682993b4 WG |
353 | * virtscsi_add_cmd - add a virtio_scsi_cmd to a virtqueue |
354 | * @vq : the struct virtqueue we're talking about | |
4fe74b1c | 355 | * @cmd : command structure |
4fe74b1c PB |
356 | * @req_size : size of the request buffer |
357 | * @resp_size : size of the response buffer | |
682993b4 | 358 | * @gfp : flags to use for memory allocations |
4fe74b1c | 359 | */ |
682993b4 WG |
360 | static int virtscsi_add_cmd(struct virtqueue *vq, |
361 | struct virtio_scsi_cmd *cmd, | |
362 | size_t req_size, size_t resp_size, gfp_t gfp) | |
4fe74b1c PB |
363 | { |
364 | struct scsi_cmnd *sc = cmd->sc; | |
682993b4 WG |
365 | struct scatterlist *sgs[4], req, resp; |
366 | struct sg_table *out, *in; | |
367 | unsigned out_num = 0, in_num = 0; | |
368 | ||
369 | out = in = NULL; | |
370 | ||
371 | if (sc && sc->sc_data_direction != DMA_NONE) { | |
372 | if (sc->sc_data_direction != DMA_FROM_DEVICE) | |
373 | out = &scsi_out(sc)->table; | |
374 | if (sc->sc_data_direction != DMA_TO_DEVICE) | |
375 | in = &scsi_in(sc)->table; | |
376 | } | |
4fe74b1c | 377 | |
4fe74b1c | 378 | /* Request header. */ |
682993b4 WG |
379 | sg_init_one(&req, &cmd->req, req_size); |
380 | sgs[out_num++] = &req; | |
4fe74b1c PB |
381 | |
382 | /* Data-out buffer. */ | |
682993b4 WG |
383 | if (out) |
384 | sgs[out_num++] = out->sgl; | |
4fe74b1c PB |
385 | |
386 | /* Response header. */ | |
682993b4 WG |
387 | sg_init_one(&resp, &cmd->resp, resp_size); |
388 | sgs[out_num + in_num++] = &resp; | |
4fe74b1c PB |
389 | |
390 | /* Data-in buffer */ | |
682993b4 WG |
391 | if (in) |
392 | sgs[out_num + in_num++] = in->sgl; | |
4fe74b1c | 393 | |
682993b4 | 394 | return virtqueue_add_sgs(vq, sgs, out_num, in_num, cmd, gfp); |
4fe74b1c PB |
395 | } |
396 | ||
682993b4 | 397 | static int virtscsi_kick_cmd(struct virtio_scsi_vq *vq, |
4fe74b1c PB |
398 | struct virtio_scsi_cmd *cmd, |
399 | size_t req_size, size_t resp_size, gfp_t gfp) | |
400 | { | |
4fe74b1c | 401 | unsigned long flags; |
4614e51c RR |
402 | int err; |
403 | bool needs_kick = false; | |
4fe74b1c | 404 | |
682993b4 WG |
405 | spin_lock_irqsave(&vq->vq_lock, flags); |
406 | err = virtscsi_add_cmd(vq->vq, cmd, req_size, resp_size, gfp); | |
4614e51c RR |
407 | if (!err) |
408 | needs_kick = virtqueue_kick_prepare(vq->vq); | |
139fe45a | 409 | |
bce750b1 | 410 | spin_unlock_irqrestore(&vq->vq_lock, flags); |
4fe74b1c | 411 | |
4614e51c | 412 | if (needs_kick) |
139fe45a | 413 | virtqueue_notify(vq->vq); |
4614e51c | 414 | return err; |
4fe74b1c PB |
415 | } |
416 | ||
417 | static int virtscsi_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *sc) | |
418 | { | |
419 | struct virtio_scsi *vscsi = shost_priv(sh); | |
420 | struct virtio_scsi_cmd *cmd; | |
421 | int ret; | |
422 | ||
2bd37f0f PB |
423 | struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev); |
424 | BUG_ON(scsi_sg_count(sc) > shost->sg_tablesize); | |
425 | ||
426 | /* TODO: check feature bit and fail if unsupported? */ | |
427 | BUG_ON(sc->sc_data_direction == DMA_BIDIRECTIONAL); | |
428 | ||
4fe74b1c PB |
429 | dev_dbg(&sc->device->sdev_gendev, |
430 | "cmd %p CDB: %#02x\n", sc, sc->cmnd[0]); | |
431 | ||
432 | ret = SCSI_MLQUEUE_HOST_BUSY; | |
433 | cmd = mempool_alloc(virtscsi_cmd_pool, GFP_ATOMIC); | |
434 | if (!cmd) | |
435 | goto out; | |
436 | ||
437 | memset(cmd, 0, sizeof(*cmd)); | |
438 | cmd->sc = sc; | |
439 | cmd->req.cmd = (struct virtio_scsi_cmd_req){ | |
440 | .lun[0] = 1, | |
441 | .lun[1] = sc->device->id, | |
442 | .lun[2] = (sc->device->lun >> 8) | 0x40, | |
443 | .lun[3] = sc->device->lun & 0xff, | |
444 | .tag = (unsigned long)sc, | |
445 | .task_attr = VIRTIO_SCSI_S_SIMPLE, | |
446 | .prio = 0, | |
447 | .crn = 0, | |
448 | }; | |
449 | ||
450 | BUG_ON(sc->cmd_len > VIRTIO_SCSI_CDB_SIZE); | |
451 | memcpy(cmd->req.cmd.cdb, sc->cmnd, sc->cmd_len); | |
452 | ||
682993b4 | 453 | if (virtscsi_kick_cmd(&vscsi->req_vq, cmd, |
4fe74b1c | 454 | sizeof cmd->req.cmd, sizeof cmd->resp.cmd, |
4614e51c | 455 | GFP_ATOMIC) == 0) |
4fe74b1c | 456 | ret = 0; |
b56d1003 EN |
457 | else |
458 | mempool_free(cmd, virtscsi_cmd_pool); | |
4fe74b1c PB |
459 | |
460 | out: | |
461 | return ret; | |
462 | } | |
463 | ||
464 | static int virtscsi_tmf(struct virtio_scsi *vscsi, struct virtio_scsi_cmd *cmd) | |
465 | { | |
466 | DECLARE_COMPLETION_ONSTACK(comp); | |
e4594bb5 | 467 | int ret = FAILED; |
4fe74b1c PB |
468 | |
469 | cmd->comp = ∁ | |
682993b4 | 470 | if (virtscsi_kick_cmd(&vscsi->ctrl_vq, cmd, |
e4594bb5 PB |
471 | sizeof cmd->req.tmf, sizeof cmd->resp.tmf, |
472 | GFP_NOIO) < 0) | |
473 | goto out; | |
4fe74b1c PB |
474 | |
475 | wait_for_completion(&comp); | |
e4594bb5 PB |
476 | if (cmd->resp.tmf.response == VIRTIO_SCSI_S_OK || |
477 | cmd->resp.tmf.response == VIRTIO_SCSI_S_FUNCTION_SUCCEEDED) | |
478 | ret = SUCCESS; | |
4fe74b1c | 479 | |
e4594bb5 PB |
480 | out: |
481 | mempool_free(cmd, virtscsi_cmd_pool); | |
482 | return ret; | |
4fe74b1c PB |
483 | } |
484 | ||
485 | static int virtscsi_device_reset(struct scsi_cmnd *sc) | |
486 | { | |
487 | struct virtio_scsi *vscsi = shost_priv(sc->device->host); | |
488 | struct virtio_scsi_cmd *cmd; | |
489 | ||
490 | sdev_printk(KERN_INFO, sc->device, "device reset\n"); | |
491 | cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO); | |
492 | if (!cmd) | |
493 | return FAILED; | |
494 | ||
495 | memset(cmd, 0, sizeof(*cmd)); | |
496 | cmd->sc = sc; | |
497 | cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){ | |
498 | .type = VIRTIO_SCSI_T_TMF, | |
499 | .subtype = VIRTIO_SCSI_T_TMF_LOGICAL_UNIT_RESET, | |
500 | .lun[0] = 1, | |
501 | .lun[1] = sc->device->id, | |
502 | .lun[2] = (sc->device->lun >> 8) | 0x40, | |
503 | .lun[3] = sc->device->lun & 0xff, | |
504 | }; | |
505 | return virtscsi_tmf(vscsi, cmd); | |
506 | } | |
507 | ||
508 | static int virtscsi_abort(struct scsi_cmnd *sc) | |
509 | { | |
510 | struct virtio_scsi *vscsi = shost_priv(sc->device->host); | |
511 | struct virtio_scsi_cmd *cmd; | |
512 | ||
513 | scmd_printk(KERN_INFO, sc, "abort\n"); | |
514 | cmd = mempool_alloc(virtscsi_cmd_pool, GFP_NOIO); | |
515 | if (!cmd) | |
516 | return FAILED; | |
517 | ||
518 | memset(cmd, 0, sizeof(*cmd)); | |
519 | cmd->sc = sc; | |
520 | cmd->req.tmf = (struct virtio_scsi_ctrl_tmf_req){ | |
521 | .type = VIRTIO_SCSI_T_TMF, | |
522 | .subtype = VIRTIO_SCSI_T_TMF_ABORT_TASK, | |
523 | .lun[0] = 1, | |
524 | .lun[1] = sc->device->id, | |
525 | .lun[2] = (sc->device->lun >> 8) | 0x40, | |
526 | .lun[3] = sc->device->lun & 0xff, | |
527 | .tag = (unsigned long)sc, | |
528 | }; | |
529 | return virtscsi_tmf(vscsi, cmd); | |
530 | } | |
531 | ||
5c370194 WG |
532 | static int virtscsi_target_alloc(struct scsi_target *starget) |
533 | { | |
534 | struct virtio_scsi_target_state *tgt = | |
535 | kmalloc(sizeof(*tgt), GFP_KERNEL); | |
536 | if (!tgt) | |
537 | return -ENOMEM; | |
538 | ||
539 | spin_lock_init(&tgt->tgt_lock); | |
540 | ||
541 | starget->hostdata = tgt; | |
542 | return 0; | |
543 | } | |
544 | ||
545 | static void virtscsi_target_destroy(struct scsi_target *starget) | |
546 | { | |
547 | struct virtio_scsi_target_state *tgt = starget->hostdata; | |
548 | kfree(tgt); | |
549 | } | |
550 | ||
4fe74b1c PB |
551 | static struct scsi_host_template virtscsi_host_template = { |
552 | .module = THIS_MODULE, | |
553 | .name = "Virtio SCSI HBA", | |
554 | .proc_name = "virtio_scsi", | |
555 | .queuecommand = virtscsi_queuecommand, | |
556 | .this_id = -1, | |
557 | .eh_abort_handler = virtscsi_abort, | |
558 | .eh_device_reset_handler = virtscsi_device_reset, | |
559 | ||
560 | .can_queue = 1024, | |
561 | .dma_boundary = UINT_MAX, | |
562 | .use_clustering = ENABLE_CLUSTERING, | |
5c370194 WG |
563 | .target_alloc = virtscsi_target_alloc, |
564 | .target_destroy = virtscsi_target_destroy, | |
4fe74b1c PB |
565 | }; |
566 | ||
567 | #define virtscsi_config_get(vdev, fld) \ | |
568 | ({ \ | |
569 | typeof(((struct virtio_scsi_config *)0)->fld) __val; \ | |
570 | vdev->config->get(vdev, \ | |
571 | offsetof(struct virtio_scsi_config, fld), \ | |
572 | &__val, sizeof(__val)); \ | |
573 | __val; \ | |
574 | }) | |
575 | ||
576 | #define virtscsi_config_set(vdev, fld, val) \ | |
577 | (void)({ \ | |
578 | typeof(((struct virtio_scsi_config *)0)->fld) __val = (val); \ | |
579 | vdev->config->set(vdev, \ | |
580 | offsetof(struct virtio_scsi_config, fld), \ | |
581 | &__val, sizeof(__val)); \ | |
582 | }) | |
583 | ||
139fe45a PB |
584 | static void virtscsi_init_vq(struct virtio_scsi_vq *virtscsi_vq, |
585 | struct virtqueue *vq) | |
586 | { | |
587 | spin_lock_init(&virtscsi_vq->vq_lock); | |
588 | virtscsi_vq->vq = vq; | |
589 | } | |
590 | ||
59057fbc NB |
591 | static void virtscsi_scan(struct virtio_device *vdev) |
592 | { | |
593 | struct Scsi_Host *shost = (struct Scsi_Host *)vdev->priv; | |
594 | ||
595 | scsi_scan_host(shost); | |
596 | } | |
597 | ||
2bd37f0f PB |
598 | static void virtscsi_remove_vqs(struct virtio_device *vdev) |
599 | { | |
2bd37f0f PB |
600 | /* Stop all the virtqueues. */ |
601 | vdev->config->reset(vdev); | |
602 | ||
2bd37f0f PB |
603 | vdev->config->del_vqs(vdev); |
604 | } | |
605 | ||
4fe74b1c | 606 | static int virtscsi_init(struct virtio_device *vdev, |
5c370194 | 607 | struct virtio_scsi *vscsi) |
4fe74b1c PB |
608 | { |
609 | int err; | |
610 | struct virtqueue *vqs[3]; | |
2bd37f0f | 611 | |
4fe74b1c PB |
612 | vq_callback_t *callbacks[] = { |
613 | virtscsi_ctrl_done, | |
614 | virtscsi_event_done, | |
615 | virtscsi_req_done | |
616 | }; | |
617 | const char *names[] = { | |
618 | "control", | |
619 | "event", | |
620 | "request" | |
621 | }; | |
622 | ||
623 | /* Discover virtqueues and write information to configuration. */ | |
624 | err = vdev->config->find_vqs(vdev, 3, vqs, callbacks, names); | |
625 | if (err) | |
626 | return err; | |
627 | ||
139fe45a PB |
628 | virtscsi_init_vq(&vscsi->ctrl_vq, vqs[0]); |
629 | virtscsi_init_vq(&vscsi->event_vq, vqs[1]); | |
630 | virtscsi_init_vq(&vscsi->req_vq, vqs[2]); | |
4fe74b1c PB |
631 | |
632 | virtscsi_config_set(vdev, cdb_size, VIRTIO_SCSI_CDB_SIZE); | |
633 | virtscsi_config_set(vdev, sense_size, VIRTIO_SCSI_SENSE_SIZE); | |
2bd37f0f | 634 | |
365a7150 CM |
635 | if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) |
636 | virtscsi_kick_event_all(vscsi); | |
637 | ||
2bd37f0f | 638 | return err; |
4fe74b1c PB |
639 | } |
640 | ||
6f039790 | 641 | static int virtscsi_probe(struct virtio_device *vdev) |
4fe74b1c PB |
642 | { |
643 | struct Scsi_Host *shost; | |
644 | struct virtio_scsi *vscsi; | |
645 | int err; | |
2bd37f0f | 646 | u32 sg_elems, num_targets; |
4fe74b1c PB |
647 | u32 cmd_per_lun; |
648 | ||
2bd37f0f | 649 | num_targets = virtscsi_config_get(vdev, max_target) + 1; |
4fe74b1c | 650 | |
5c370194 | 651 | shost = scsi_host_alloc(&virtscsi_host_template, sizeof(*vscsi)); |
4fe74b1c PB |
652 | if (!shost) |
653 | return -ENOMEM; | |
654 | ||
2bd37f0f | 655 | sg_elems = virtscsi_config_get(vdev, seg_max) ?: 1; |
4fe74b1c PB |
656 | shost->sg_tablesize = sg_elems; |
657 | vscsi = shost_priv(shost); | |
658 | vscsi->vdev = vdev; | |
659 | vdev->priv = shost; | |
660 | ||
5c370194 | 661 | err = virtscsi_init(vdev, vscsi); |
4fe74b1c PB |
662 | if (err) |
663 | goto virtscsi_init_failed; | |
664 | ||
665 | cmd_per_lun = virtscsi_config_get(vdev, cmd_per_lun) ?: 1; | |
666 | shost->cmd_per_lun = min_t(u32, cmd_per_lun, shost->can_queue); | |
667 | shost->max_sectors = virtscsi_config_get(vdev, max_sectors) ?: 0xFFFF; | |
9da5f5ac PB |
668 | |
669 | /* LUNs > 256 are reported with format 1, so they go in the range | |
670 | * 16640-32767. | |
671 | */ | |
672 | shost->max_lun = virtscsi_config_get(vdev, max_lun) + 1 + 0x4000; | |
2bd37f0f | 673 | shost->max_id = num_targets; |
4fe74b1c PB |
674 | shost->max_channel = 0; |
675 | shost->max_cmd_len = VIRTIO_SCSI_CDB_SIZE; | |
676 | err = scsi_add_host(shost, &vdev->dev); | |
677 | if (err) | |
678 | goto scsi_add_host_failed; | |
59057fbc NB |
679 | /* |
680 | * scsi_scan_host() happens in virtscsi_scan() via virtio_driver->scan() | |
681 | * after VIRTIO_CONFIG_S_DRIVER_OK has been set.. | |
682 | */ | |
4fe74b1c PB |
683 | return 0; |
684 | ||
685 | scsi_add_host_failed: | |
686 | vdev->config->del_vqs(vdev); | |
687 | virtscsi_init_failed: | |
688 | scsi_host_put(shost); | |
689 | return err; | |
690 | } | |
691 | ||
6f039790 | 692 | static void virtscsi_remove(struct virtio_device *vdev) |
4fe74b1c PB |
693 | { |
694 | struct Scsi_Host *shost = virtio_scsi_host(vdev); | |
365a7150 CM |
695 | struct virtio_scsi *vscsi = shost_priv(shost); |
696 | ||
697 | if (virtio_has_feature(vdev, VIRTIO_SCSI_F_HOTPLUG)) | |
698 | virtscsi_cancel_event_work(vscsi); | |
4fe74b1c PB |
699 | |
700 | scsi_remove_host(shost); | |
701 | ||
702 | virtscsi_remove_vqs(vdev); | |
703 | scsi_host_put(shost); | |
704 | } | |
705 | ||
706 | #ifdef CONFIG_PM | |
707 | static int virtscsi_freeze(struct virtio_device *vdev) | |
708 | { | |
709 | virtscsi_remove_vqs(vdev); | |
710 | return 0; | |
711 | } | |
712 | ||
713 | static int virtscsi_restore(struct virtio_device *vdev) | |
714 | { | |
715 | struct Scsi_Host *sh = virtio_scsi_host(vdev); | |
716 | struct virtio_scsi *vscsi = shost_priv(sh); | |
717 | ||
5c370194 | 718 | return virtscsi_init(vdev, vscsi); |
4fe74b1c PB |
719 | } |
720 | #endif | |
721 | ||
722 | static struct virtio_device_id id_table[] = { | |
723 | { VIRTIO_ID_SCSI, VIRTIO_DEV_ANY_ID }, | |
724 | { 0 }, | |
725 | }; | |
726 | ||
365a7150 | 727 | static unsigned int features[] = { |
865b58c0 PB |
728 | VIRTIO_SCSI_F_HOTPLUG, |
729 | VIRTIO_SCSI_F_CHANGE, | |
365a7150 CM |
730 | }; |
731 | ||
4fe74b1c | 732 | static struct virtio_driver virtio_scsi_driver = { |
365a7150 CM |
733 | .feature_table = features, |
734 | .feature_table_size = ARRAY_SIZE(features), | |
4fe74b1c PB |
735 | .driver.name = KBUILD_MODNAME, |
736 | .driver.owner = THIS_MODULE, | |
737 | .id_table = id_table, | |
738 | .probe = virtscsi_probe, | |
59057fbc | 739 | .scan = virtscsi_scan, |
4fe74b1c PB |
740 | #ifdef CONFIG_PM |
741 | .freeze = virtscsi_freeze, | |
742 | .restore = virtscsi_restore, | |
743 | #endif | |
6f039790 | 744 | .remove = virtscsi_remove, |
4fe74b1c PB |
745 | }; |
746 | ||
747 | static int __init init(void) | |
748 | { | |
749 | int ret = -ENOMEM; | |
750 | ||
751 | virtscsi_cmd_cache = KMEM_CACHE(virtio_scsi_cmd, 0); | |
752 | if (!virtscsi_cmd_cache) { | |
ba06d1e1 | 753 | pr_err("kmem_cache_create() for virtscsi_cmd_cache failed\n"); |
4fe74b1c PB |
754 | goto error; |
755 | } | |
756 | ||
757 | ||
758 | virtscsi_cmd_pool = | |
759 | mempool_create_slab_pool(VIRTIO_SCSI_MEMPOOL_SZ, | |
760 | virtscsi_cmd_cache); | |
761 | if (!virtscsi_cmd_pool) { | |
ba06d1e1 | 762 | pr_err("mempool_create() for virtscsi_cmd_pool failed\n"); |
4fe74b1c PB |
763 | goto error; |
764 | } | |
765 | ret = register_virtio_driver(&virtio_scsi_driver); | |
766 | if (ret < 0) | |
767 | goto error; | |
768 | ||
769 | return 0; | |
770 | ||
771 | error: | |
772 | if (virtscsi_cmd_pool) { | |
773 | mempool_destroy(virtscsi_cmd_pool); | |
774 | virtscsi_cmd_pool = NULL; | |
775 | } | |
776 | if (virtscsi_cmd_cache) { | |
777 | kmem_cache_destroy(virtscsi_cmd_cache); | |
778 | virtscsi_cmd_cache = NULL; | |
779 | } | |
780 | return ret; | |
781 | } | |
782 | ||
783 | static void __exit fini(void) | |
784 | { | |
785 | unregister_virtio_driver(&virtio_scsi_driver); | |
786 | mempool_destroy(virtscsi_cmd_pool); | |
787 | kmem_cache_destroy(virtscsi_cmd_cache); | |
788 | } | |
789 | module_init(init); | |
790 | module_exit(fini); | |
791 | ||
792 | MODULE_DEVICE_TABLE(virtio, id_table); | |
793 | MODULE_DESCRIPTION("Virtio SCSI HBA driver"); | |
794 | MODULE_LICENSE("GPL"); |