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