Commit | Line | Data |
---|---|---|
bef4a34a | 1 | /* |
bef4a34a HJ |
2 | * Copyright (c) 2009, Microsoft Corporation. |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms and conditions of the GNU General Public License, | |
6 | * version 2, as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple | |
15 | * Place - Suite 330, Boston, MA 02111-1307 USA. | |
16 | * | |
17 | * Authors: | |
18 | * Haiyang Zhang <haiyangz@microsoft.com> | |
19 | * Hank Janssen <hjanssen@microsoft.com> | |
972621c9 | 20 | * K. Y. Srinivasan <kys@microsoft.com> |
bef4a34a | 21 | */ |
bef4a34a | 22 | #include <linux/init.h> |
5a0e3ad6 | 23 | #include <linux/slab.h> |
bef4a34a HJ |
24 | #include <linux/module.h> |
25 | #include <linux/device.h> | |
26 | #include <linux/blkdev.h> | |
bef4a34a HJ |
27 | #include <scsi/scsi.h> |
28 | #include <scsi/scsi_cmnd.h> | |
29 | #include <scsi/scsi_host.h> | |
30 | #include <scsi/scsi_device.h> | |
31 | #include <scsi/scsi_tcq.h> | |
32 | #include <scsi/scsi_eh.h> | |
33 | #include <scsi/scsi_devinfo.h> | |
bef4a34a | 34 | #include <scsi/scsi_dbg.h> |
3f335ea2 S |
35 | |
36 | #include "hyperv.h" | |
cdee1504 | 37 | #include "hyperv_storage.h" |
bef4a34a | 38 | |
2db2cabe | 39 | static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE; |
bef4a34a | 40 | |
3d598ce1 S |
41 | module_param(storvsc_ringbuffer_size, int, S_IRUGO); |
42 | MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)"); | |
43 | ||
972621c9 | 44 | struct hv_host_device { |
97c15296 | 45 | struct hv_device *dev; |
ff568d3a GKH |
46 | struct kmem_cache *request_pool; |
47 | unsigned int port; | |
48 | unsigned char path; | |
49 | unsigned char target; | |
bef4a34a HJ |
50 | }; |
51 | ||
52 | struct storvsc_cmd_request { | |
ff568d3a GKH |
53 | struct list_head entry; |
54 | struct scsi_cmnd *cmd; | |
bef4a34a HJ |
55 | |
56 | unsigned int bounce_sgl_count; | |
ff568d3a | 57 | struct scatterlist *bounce_sgl; |
bef4a34a | 58 | |
0b3f6834 | 59 | struct hv_storvsc_request request; |
bef4a34a HJ |
60 | }; |
61 | ||
bd1f5d6a S |
62 | static void storvsc_get_ide_info(struct hv_device *dev, int *target, int *path) |
63 | { | |
64 | *target = | |
65 | dev->dev_instance.b[5] << 8 | dev->dev_instance.b[4]; | |
66 | ||
67 | *path = | |
68 | dev->dev_instance.b[3] << 24 | | |
69 | dev->dev_instance.b[2] << 16 | | |
70 | dev->dev_instance.b[1] << 8 | dev->dev_instance.b[0]; | |
71 | } | |
72 | ||
bef4a34a | 73 | |
5b60acee S |
74 | static int storvsc_device_alloc(struct scsi_device *sdevice) |
75 | { | |
76 | /* | |
77 | * This enables luns to be located sparsely. Otherwise, we may not | |
78 | * discovered them. | |
79 | */ | |
80 | sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN; | |
81 | return 0; | |
82 | } | |
83 | ||
a1ebfeae S |
84 | static int storvsc_merge_bvec(struct request_queue *q, |
85 | struct bvec_merge_data *bmd, struct bio_vec *bvec) | |
86 | { | |
87 | /* checking done by caller. */ | |
88 | return bvec->bv_len; | |
89 | } | |
90 | ||
419f2d03 S |
91 | static int storvsc_device_configure(struct scsi_device *sdevice) |
92 | { | |
93 | scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG, | |
94 | STORVSC_MAX_IO_REQUESTS); | |
95 | ||
419f2d03 S |
96 | blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE); |
97 | ||
419f2d03 S |
98 | blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec); |
99 | ||
100 | blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY); | |
419f2d03 S |
101 | |
102 | return 0; | |
103 | } | |
104 | ||
49a3c7a9 S |
105 | static void destroy_bounce_buffer(struct scatterlist *sgl, |
106 | unsigned int sg_count) | |
107 | { | |
108 | int i; | |
109 | struct page *page_buf; | |
110 | ||
111 | for (i = 0; i < sg_count; i++) { | |
112 | page_buf = sg_page((&sgl[i])); | |
113 | if (page_buf != NULL) | |
114 | __free_page(page_buf); | |
115 | } | |
116 | ||
117 | kfree(sgl); | |
118 | } | |
119 | ||
3862ef3e S |
120 | static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count) |
121 | { | |
122 | int i; | |
123 | ||
124 | /* No need to check */ | |
125 | if (sg_count < 2) | |
126 | return -1; | |
127 | ||
128 | /* We have at least 2 sg entries */ | |
129 | for (i = 0; i < sg_count; i++) { | |
130 | if (i == 0) { | |
131 | /* make sure 1st one does not have hole */ | |
132 | if (sgl[i].offset + sgl[i].length != PAGE_SIZE) | |
133 | return i; | |
134 | } else if (i == sg_count - 1) { | |
135 | /* make sure last one does not have hole */ | |
136 | if (sgl[i].offset != 0) | |
137 | return i; | |
138 | } else { | |
139 | /* make sure no hole in the middle */ | |
140 | if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0) | |
141 | return i; | |
142 | } | |
143 | } | |
144 | return -1; | |
145 | } | |
146 | ||
a9753cbd S |
147 | static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl, |
148 | unsigned int sg_count, | |
149 | unsigned int len) | |
150 | { | |
151 | int i; | |
152 | int num_pages; | |
153 | struct scatterlist *bounce_sgl; | |
154 | struct page *page_buf; | |
155 | ||
156 | num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT; | |
157 | ||
158 | bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC); | |
159 | if (!bounce_sgl) | |
160 | return NULL; | |
161 | ||
162 | for (i = 0; i < num_pages; i++) { | |
163 | page_buf = alloc_page(GFP_ATOMIC); | |
164 | if (!page_buf) | |
165 | goto cleanup; | |
166 | sg_set_page(&bounce_sgl[i], page_buf, 0, 0); | |
167 | } | |
168 | ||
169 | return bounce_sgl; | |
170 | ||
171 | cleanup: | |
172 | destroy_bounce_buffer(bounce_sgl, num_pages); | |
173 | return NULL; | |
174 | } | |
175 | ||
29fe2c9b S |
176 | |
177 | /* Assume the original sgl has enough room */ | |
178 | static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl, | |
179 | struct scatterlist *bounce_sgl, | |
180 | unsigned int orig_sgl_count) | |
181 | { | |
182 | int i; | |
183 | int j = 0; | |
184 | unsigned long src, dest; | |
185 | unsigned int srclen, destlen, copylen; | |
186 | unsigned int total_copied = 0; | |
187 | unsigned long bounce_addr = 0; | |
188 | unsigned long dest_addr = 0; | |
189 | unsigned long flags; | |
190 | ||
191 | local_irq_save(flags); | |
192 | ||
193 | for (i = 0; i < orig_sgl_count; i++) { | |
194 | dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), | |
195 | KM_IRQ0) + orig_sgl[i].offset; | |
196 | dest = dest_addr; | |
197 | destlen = orig_sgl[i].length; | |
198 | ||
199 | if (bounce_addr == 0) | |
200 | bounce_addr = | |
201 | (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), | |
202 | KM_IRQ0); | |
203 | ||
204 | while (destlen) { | |
205 | src = bounce_addr + bounce_sgl[j].offset; | |
206 | srclen = bounce_sgl[j].length - bounce_sgl[j].offset; | |
207 | ||
208 | copylen = min(srclen, destlen); | |
209 | memcpy((void *)dest, (void *)src, copylen); | |
210 | ||
211 | total_copied += copylen; | |
212 | bounce_sgl[j].offset += copylen; | |
213 | destlen -= copylen; | |
214 | dest += copylen; | |
215 | ||
216 | if (bounce_sgl[j].offset == bounce_sgl[j].length) { | |
217 | /* full */ | |
218 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); | |
219 | j++; | |
220 | ||
221 | /* if we need to use another bounce buffer */ | |
222 | if (destlen || i != orig_sgl_count - 1) | |
223 | bounce_addr = | |
224 | (unsigned long)kmap_atomic( | |
225 | sg_page((&bounce_sgl[j])), KM_IRQ0); | |
226 | } else if (destlen == 0 && i == orig_sgl_count - 1) { | |
227 | /* unmap the last bounce that is < PAGE_SIZE */ | |
228 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); | |
229 | } | |
230 | } | |
231 | ||
232 | kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset), | |
233 | KM_IRQ0); | |
234 | } | |
235 | ||
236 | local_irq_restore(flags); | |
237 | ||
238 | return total_copied; | |
239 | } | |
240 | ||
6a8ff44b S |
241 | |
242 | /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */ | |
243 | static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl, | |
244 | struct scatterlist *bounce_sgl, | |
245 | unsigned int orig_sgl_count) | |
246 | { | |
247 | int i; | |
248 | int j = 0; | |
249 | unsigned long src, dest; | |
250 | unsigned int srclen, destlen, copylen; | |
251 | unsigned int total_copied = 0; | |
252 | unsigned long bounce_addr = 0; | |
253 | unsigned long src_addr = 0; | |
254 | unsigned long flags; | |
255 | ||
256 | local_irq_save(flags); | |
257 | ||
258 | for (i = 0; i < orig_sgl_count; i++) { | |
259 | src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), | |
260 | KM_IRQ0) + orig_sgl[i].offset; | |
261 | src = src_addr; | |
262 | srclen = orig_sgl[i].length; | |
263 | ||
264 | if (bounce_addr == 0) | |
265 | bounce_addr = | |
266 | (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), | |
267 | KM_IRQ0); | |
268 | ||
269 | while (srclen) { | |
270 | /* assume bounce offset always == 0 */ | |
271 | dest = bounce_addr + bounce_sgl[j].length; | |
272 | destlen = PAGE_SIZE - bounce_sgl[j].length; | |
273 | ||
274 | copylen = min(srclen, destlen); | |
275 | memcpy((void *)dest, (void *)src, copylen); | |
276 | ||
277 | total_copied += copylen; | |
278 | bounce_sgl[j].length += copylen; | |
279 | srclen -= copylen; | |
280 | src += copylen; | |
281 | ||
282 | if (bounce_sgl[j].length == PAGE_SIZE) { | |
283 | /* full..move to next entry */ | |
284 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); | |
285 | j++; | |
286 | ||
287 | /* if we need to use another bounce buffer */ | |
288 | if (srclen || i != orig_sgl_count - 1) | |
289 | bounce_addr = | |
290 | (unsigned long)kmap_atomic( | |
291 | sg_page((&bounce_sgl[j])), KM_IRQ0); | |
292 | ||
293 | } else if (srclen == 0 && i == orig_sgl_count - 1) { | |
294 | /* unmap the last bounce that is < PAGE_SIZE */ | |
295 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); | |
296 | } | |
297 | } | |
298 | ||
299 | kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0); | |
300 | } | |
301 | ||
302 | local_irq_restore(flags); | |
303 | ||
304 | return total_copied; | |
305 | } | |
306 | ||
5c5c0234 | 307 | |
5c5c0234 S |
308 | static int storvsc_remove(struct hv_device *dev) |
309 | { | |
5c5c0234 S |
310 | struct Scsi_Host *host = dev_get_drvdata(&dev->device); |
311 | struct hv_host_device *host_dev = | |
312 | (struct hv_host_device *)host->hostdata; | |
313 | ||
2935a407 S |
314 | scsi_remove_host(host); |
315 | ||
2935a407 | 316 | scsi_host_put(host); |
d36b0a03 | 317 | |
6cdc57c0 | 318 | storvsc_dev_remove(dev); |
5c5c0234 S |
319 | if (host_dev->request_pool) { |
320 | kmem_cache_destroy(host_dev->request_pool); | |
321 | host_dev->request_pool = NULL; | |
322 | } | |
5c5c0234 S |
323 | return 0; |
324 | } | |
325 | ||
62838ce2 S |
326 | |
327 | static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev, | |
328 | sector_t capacity, int *info) | |
329 | { | |
5326fd5c S |
330 | sector_t nsect = capacity; |
331 | sector_t cylinders = nsect; | |
332 | int heads, sectors_pt; | |
62838ce2 | 333 | |
5326fd5c S |
334 | /* |
335 | * We are making up these values; let us keep it simple. | |
336 | */ | |
337 | heads = 0xff; | |
338 | sectors_pt = 0x3f; /* Sectors per track */ | |
339 | sector_div(cylinders, heads * sectors_pt); | |
340 | if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect) | |
341 | cylinders = 0xffff; | |
62838ce2 S |
342 | |
343 | info[0] = heads; | |
5326fd5c S |
344 | info[1] = sectors_pt; |
345 | info[2] = (int)cylinders; | |
62838ce2 | 346 | |
62838ce2 S |
347 | return 0; |
348 | } | |
aa3d789e S |
349 | |
350 | static int storvsc_host_reset(struct hv_device *device) | |
351 | { | |
352 | struct storvsc_device *stor_device; | |
353 | struct hv_storvsc_request *request; | |
354 | struct vstor_packet *vstor_packet; | |
355 | int ret, t; | |
356 | ||
aa3d789e | 357 | |
1eaaddf9 | 358 | stor_device = get_out_stor_device(device); |
aa3d789e | 359 | if (!stor_device) |
f3b74165 | 360 | return -ENODEV; |
aa3d789e S |
361 | |
362 | request = &stor_device->reset_request; | |
363 | vstor_packet = &request->vstor_packet; | |
364 | ||
365 | init_completion(&request->wait_event); | |
366 | ||
367 | vstor_packet->operation = VSTOR_OPERATION_RESET_BUS; | |
368 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; | |
369 | vstor_packet->vm_srb.path_id = stor_device->path_id; | |
370 | ||
371 | ret = vmbus_sendpacket(device->channel, vstor_packet, | |
372 | sizeof(struct vstor_packet), | |
373 | (unsigned long)&stor_device->reset_request, | |
374 | VM_PKT_DATA_INBAND, | |
375 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
376 | if (ret != 0) | |
377 | goto cleanup; | |
378 | ||
46d2eb6d | 379 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); |
aa3d789e S |
380 | if (t == 0) { |
381 | ret = -ETIMEDOUT; | |
382 | goto cleanup; | |
383 | } | |
384 | ||
aa3d789e S |
385 | |
386 | /* | |
387 | * At this point, all outstanding requests in the adapter | |
388 | * should have been flushed out and return to us | |
389 | */ | |
390 | ||
391 | cleanup: | |
aa3d789e S |
392 | return ret; |
393 | } | |
394 | ||
395 | ||
96e690be S |
396 | /* |
397 | * storvsc_host_reset_handler - Reset the scsi HBA | |
398 | */ | |
399 | static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd) | |
400 | { | |
401 | int ret; | |
402 | struct hv_host_device *host_dev = | |
403 | (struct hv_host_device *)scmnd->device->host->hostdata; | |
404 | struct hv_device *dev = host_dev->dev; | |
405 | ||
96e690be S |
406 | ret = storvsc_host_reset(dev); |
407 | if (ret != 0) | |
408 | return ret; | |
409 | ||
96e690be S |
410 | return ret; |
411 | } | |
412 | ||
8a411bad S |
413 | |
414 | /* | |
415 | * storvsc_commmand_completion - Command completion processing | |
416 | */ | |
417 | static void storvsc_commmand_completion(struct hv_storvsc_request *request) | |
418 | { | |
419 | struct storvsc_cmd_request *cmd_request = | |
420 | (struct storvsc_cmd_request *)request->context; | |
421 | struct scsi_cmnd *scmnd = cmd_request->cmd; | |
422 | struct hv_host_device *host_dev = | |
423 | (struct hv_host_device *)scmnd->device->host->hostdata; | |
424 | void (*scsi_done_fn)(struct scsi_cmnd *); | |
425 | struct scsi_sense_hdr sense_hdr; | |
426 | struct vmscsi_request *vm_srb; | |
427 | ||
3612ef99 | 428 | vm_srb = &request->vstor_packet.vm_srb; |
8a411bad | 429 | if (cmd_request->bounce_sgl_count) { |
3612ef99 S |
430 | if (vm_srb->data_in == READ_TYPE) { |
431 | copy_from_bounce_buffer(scsi_sglist(scmnd), | |
8a411bad S |
432 | cmd_request->bounce_sgl, |
433 | scsi_sg_count(scmnd)); | |
3612ef99 S |
434 | destroy_bounce_buffer(cmd_request->bounce_sgl, |
435 | cmd_request->bounce_sgl_count); | |
436 | } | |
8a411bad S |
437 | } |
438 | ||
8a411bad S |
439 | scmnd->result = vm_srb->scsi_status; |
440 | ||
441 | if (scmnd->result) { | |
442 | if (scsi_normalize_sense(scmnd->sense_buffer, | |
443 | SCSI_SENSE_BUFFERSIZE, &sense_hdr)) | |
444 | scsi_print_sense_hdr("storvsc", &sense_hdr); | |
445 | } | |
446 | ||
8a411bad S |
447 | scsi_set_resid(scmnd, |
448 | request->data_buffer.len - | |
449 | vm_srb->data_transfer_length); | |
450 | ||
451 | scsi_done_fn = scmnd->scsi_done; | |
452 | ||
453 | scmnd->host_scribble = NULL; | |
454 | scmnd->scsi_done = NULL; | |
455 | ||
8a411bad S |
456 | scsi_done_fn(scmnd); |
457 | ||
458 | kmem_cache_free(host_dev->request_pool, cmd_request); | |
459 | } | |
460 | ||
c5b463ae S |
461 | |
462 | /* | |
463 | * storvsc_queuecommand - Initiate command processing | |
464 | */ | |
465 | static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd, | |
466 | void (*done)(struct scsi_cmnd *)) | |
467 | { | |
468 | int ret; | |
469 | struct hv_host_device *host_dev = | |
470 | (struct hv_host_device *)scmnd->device->host->hostdata; | |
471 | struct hv_device *dev = host_dev->dev; | |
c5b463ae S |
472 | struct hv_storvsc_request *request; |
473 | struct storvsc_cmd_request *cmd_request; | |
474 | unsigned int request_size = 0; | |
475 | int i; | |
476 | struct scatterlist *sgl; | |
477 | unsigned int sg_count = 0; | |
478 | struct vmscsi_request *vm_srb; | |
479 | ||
480 | ||
481 | /* If retrying, no need to prep the cmd */ | |
482 | if (scmnd->host_scribble) { | |
c5b463ae S |
483 | |
484 | cmd_request = | |
485 | (struct storvsc_cmd_request *)scmnd->host_scribble; | |
c5b463ae S |
486 | |
487 | goto retry_request; | |
488 | } | |
489 | ||
c5b463ae S |
490 | scmnd->scsi_done = done; |
491 | ||
492 | request_size = sizeof(struct storvsc_cmd_request); | |
493 | ||
494 | cmd_request = kmem_cache_zalloc(host_dev->request_pool, | |
495 | GFP_ATOMIC); | |
496 | if (!cmd_request) { | |
497 | scmnd->scsi_done = NULL; | |
498 | return SCSI_MLQUEUE_DEVICE_BUSY; | |
499 | } | |
500 | ||
501 | /* Setup the cmd request */ | |
502 | cmd_request->bounce_sgl_count = 0; | |
503 | cmd_request->bounce_sgl = NULL; | |
504 | cmd_request->cmd = scmnd; | |
505 | ||
506 | scmnd->host_scribble = (unsigned char *)cmd_request; | |
507 | ||
508 | request = &cmd_request->request; | |
509 | vm_srb = &request->vstor_packet.vm_srb; | |
510 | ||
511 | ||
512 | /* Build the SRB */ | |
513 | switch (scmnd->sc_data_direction) { | |
514 | case DMA_TO_DEVICE: | |
515 | vm_srb->data_in = WRITE_TYPE; | |
516 | break; | |
517 | case DMA_FROM_DEVICE: | |
518 | vm_srb->data_in = READ_TYPE; | |
519 | break; | |
520 | default: | |
521 | vm_srb->data_in = UNKNOWN_TYPE; | |
522 | break; | |
523 | } | |
524 | ||
525 | request->on_io_completion = storvsc_commmand_completion; | |
526 | request->context = cmd_request;/* scmnd; */ | |
527 | ||
c5b463ae S |
528 | vm_srb->port_number = host_dev->port; |
529 | vm_srb->path_id = scmnd->device->channel; | |
530 | vm_srb->target_id = scmnd->device->id; | |
531 | vm_srb->lun = scmnd->device->lun; | |
532 | ||
c5b463ae S |
533 | vm_srb->cdb_length = scmnd->cmd_len; |
534 | ||
535 | memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length); | |
536 | ||
537 | request->sense_buffer = scmnd->sense_buffer; | |
538 | ||
539 | ||
540 | request->data_buffer.len = scsi_bufflen(scmnd); | |
541 | if (scsi_sg_count(scmnd)) { | |
542 | sgl = (struct scatterlist *)scsi_sglist(scmnd); | |
543 | sg_count = scsi_sg_count(scmnd); | |
544 | ||
545 | /* check if we need to bounce the sgl */ | |
546 | if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) { | |
547 | cmd_request->bounce_sgl = | |
548 | create_bounce_buffer(sgl, scsi_sg_count(scmnd), | |
549 | scsi_bufflen(scmnd)); | |
550 | if (!cmd_request->bounce_sgl) { | |
551 | scmnd->scsi_done = NULL; | |
552 | scmnd->host_scribble = NULL; | |
553 | kmem_cache_free(host_dev->request_pool, | |
554 | cmd_request); | |
555 | ||
556 | return SCSI_MLQUEUE_HOST_BUSY; | |
557 | } | |
558 | ||
559 | cmd_request->bounce_sgl_count = | |
560 | ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >> | |
561 | PAGE_SHIFT; | |
562 | ||
fa23b8c7 S |
563 | if (vm_srb->data_in == WRITE_TYPE) |
564 | copy_to_bounce_buffer(sgl, | |
565 | cmd_request->bounce_sgl, | |
566 | scsi_sg_count(scmnd)); | |
c5b463ae S |
567 | |
568 | sgl = cmd_request->bounce_sgl; | |
569 | sg_count = cmd_request->bounce_sgl_count; | |
570 | } | |
571 | ||
572 | request->data_buffer.offset = sgl[0].offset; | |
573 | ||
574 | for (i = 0; i < sg_count; i++) | |
575 | request->data_buffer.pfn_array[i] = | |
576 | page_to_pfn(sg_page((&sgl[i]))); | |
577 | ||
578 | } else if (scsi_sglist(scmnd)) { | |
c5b463ae S |
579 | request->data_buffer.offset = |
580 | virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1); | |
581 | request->data_buffer.pfn_array[0] = | |
582 | virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT; | |
583 | } | |
584 | ||
585 | retry_request: | |
586 | /* Invokes the vsc to start an IO */ | |
636f0fd1 S |
587 | ret = storvsc_do_io(dev, &cmd_request->request); |
588 | ||
d2598f01 | 589 | if (ret == -EAGAIN) { |
c5b463ae S |
590 | /* no more space */ |
591 | ||
592 | if (cmd_request->bounce_sgl_count) { | |
593 | /* | |
594 | * FIXME: We can optimize on writes by just skipping | |
595 | * this | |
596 | */ | |
597 | copy_from_bounce_buffer(scsi_sglist(scmnd), | |
598 | cmd_request->bounce_sgl, | |
599 | scsi_sg_count(scmnd)); | |
600 | destroy_bounce_buffer(cmd_request->bounce_sgl, | |
601 | cmd_request->bounce_sgl_count); | |
602 | } | |
603 | ||
604 | kmem_cache_free(host_dev->request_pool, cmd_request); | |
605 | ||
606 | scmnd->scsi_done = NULL; | |
607 | scmnd->host_scribble = NULL; | |
608 | ||
609 | ret = SCSI_MLQUEUE_DEVICE_BUSY; | |
610 | } | |
611 | ||
612 | return ret; | |
613 | } | |
614 | ||
615 | static DEF_SCSI_QCMD(storvsc_queuecommand) | |
616 | ||
bef4a34a | 617 | |
454f18a9 | 618 | /* Scsi driver */ |
bef4a34a | 619 | static struct scsi_host_template scsi_driver = { |
ff568d3a GKH |
620 | .module = THIS_MODULE, |
621 | .name = "storvsc_host_t", | |
622 | .bios_param = storvsc_get_chs, | |
623 | .queuecommand = storvsc_queuecommand, | |
624 | .eh_host_reset_handler = storvsc_host_reset_handler, | |
625 | .slave_alloc = storvsc_device_alloc, | |
626 | .slave_configure = storvsc_device_configure, | |
627 | .cmd_per_lun = 1, | |
628 | /* 64 max_queue * 1 target */ | |
0686e4f4 | 629 | .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS, |
ff568d3a | 630 | .this_id = -1, |
454f18a9 | 631 | /* no use setting to 0 since ll_blk_rw reset it to 1 */ |
ff568d3a GKH |
632 | /* currently 32 */ |
633 | .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT, | |
634 | /* | |
635 | * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge | |
636 | * into 1 sg element. If set, we must limit the max_segment_size to | |
637 | * PAGE_SIZE, otherwise we may get 1 sg element that represents | |
638 | * multiple | |
639 | */ | |
454f18a9 | 640 | /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */ |
ff568d3a | 641 | .use_clustering = ENABLE_CLUSTERING, |
454f18a9 | 642 | /* Make sure we dont get a sg segment crosses a page boundary */ |
ff568d3a | 643 | .dma_boundary = PAGE_SIZE-1, |
bef4a34a HJ |
644 | }; |
645 | ||
21e37742 S |
646 | /* |
647 | * The storvsc_probe function assumes that the IDE guid | |
648 | * is the second entry. | |
649 | */ | |
d847b5fe | 650 | static const struct hv_vmbus_device_id id_table[] = { |
c45cf2d4 GKH |
651 | /* SCSI guid */ |
652 | { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d, | |
653 | 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f) }, | |
21e37742 S |
654 | /* IDE guid */ |
655 | { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44, | |
656 | 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5) }, | |
c45cf2d4 | 657 | { }, |
d847b5fe | 658 | }; |
bef4a34a | 659 | |
d847b5fe | 660 | MODULE_DEVICE_TABLE(vmbus, id_table); |
bd1f5d6a | 661 | |
bd1f5d6a | 662 | |
3e189519 | 663 | /* |
ff568d3a GKH |
664 | * storvsc_probe - Add a new device for this driver |
665 | */ | |
f5c78872 | 666 | |
9efd21e1 | 667 | static int storvsc_probe(struct hv_device *device) |
bef4a34a | 668 | { |
ff568d3a | 669 | int ret; |
bef4a34a | 670 | struct Scsi_Host *host; |
795b613d | 671 | struct hv_host_device *host_dev; |
9f0c7d2c | 672 | struct storvsc_device_info device_info; |
bd1f5d6a S |
673 | bool dev_is_ide; |
674 | int path = 0; | |
675 | int target = 0; | |
676 | ||
21e37742 | 677 | if (!memcmp(&device->dev_type.b, id_table[1].guid, sizeof(uuid_le))) |
bd1f5d6a S |
678 | dev_is_ide = true; |
679 | else | |
680 | dev_is_ide = false; | |
bef4a34a | 681 | |
ff568d3a | 682 | host = scsi_host_alloc(&scsi_driver, |
972621c9 | 683 | sizeof(struct hv_host_device)); |
f8feed06 | 684 | if (!host) |
bef4a34a | 685 | return -ENOMEM; |
bef4a34a | 686 | |
9efd21e1 | 687 | dev_set_drvdata(&device->device, host); |
bef4a34a | 688 | |
795b613d S |
689 | host_dev = (struct hv_host_device *)host->hostdata; |
690 | memset(host_dev, 0, sizeof(struct hv_host_device)); | |
bef4a34a | 691 | |
795b613d | 692 | host_dev->port = host->host_no; |
97c15296 | 693 | host_dev->dev = device; |
bef4a34a | 694 | |
795b613d | 695 | host_dev->request_pool = |
9efd21e1 | 696 | kmem_cache_create(dev_name(&device->device), |
1e05d88e | 697 | sizeof(struct storvsc_cmd_request), 0, |
ff568d3a GKH |
698 | SLAB_HWCACHE_ALIGN, NULL); |
699 | ||
795b613d | 700 | if (!host_dev->request_pool) { |
bef4a34a | 701 | scsi_host_put(host); |
bef4a34a HJ |
702 | return -ENOMEM; |
703 | } | |
704 | ||
8a046024 | 705 | device_info.port_number = host->host_no; |
fa4d123a | 706 | device_info.ring_buffer_size = storvsc_ringbuffer_size; |
454f18a9 | 707 | /* Call to the vsc driver to add the device */ |
58f1f5cb | 708 | ret = storvsc_dev_add(device, (void *)&device_info); |
9efd21e1 | 709 | |
ff568d3a | 710 | if (ret != 0) { |
795b613d | 711 | kmem_cache_destroy(host_dev->request_pool); |
bef4a34a | 712 | scsi_host_put(host); |
8e854680 | 713 | return -ENODEV; |
bef4a34a HJ |
714 | } |
715 | ||
bd1f5d6a S |
716 | if (dev_is_ide) |
717 | storvsc_get_ide_info(device, &target, &path); | |
718 | ||
795b613d S |
719 | host_dev->path = device_info.path_id; |
720 | host_dev->target = device_info.target_id; | |
bef4a34a | 721 | |
ff568d3a GKH |
722 | /* max # of devices per target */ |
723 | host->max_lun = STORVSC_MAX_LUNS_PER_TARGET; | |
724 | /* max # of targets per channel */ | |
725 | host->max_id = STORVSC_MAX_TARGETS; | |
726 | /* max # of channels */ | |
727 | host->max_channel = STORVSC_MAX_CHANNELS - 1; | |
bef4a34a | 728 | |
454f18a9 | 729 | /* Register the HBA and start the scsi bus scan */ |
9efd21e1 | 730 | ret = scsi_add_host(host, &device->device); |
bd1f5d6a S |
731 | if (ret != 0) |
732 | goto err_out; | |
bef4a34a | 733 | |
bd1f5d6a S |
734 | if (!dev_is_ide) { |
735 | scsi_scan_host(host); | |
736 | return 0; | |
737 | } | |
738 | ret = scsi_add_device(host, 0, target, 0); | |
739 | if (ret) { | |
740 | scsi_remove_host(host); | |
741 | goto err_out; | |
bef4a34a | 742 | } |
bd1f5d6a | 743 | return 0; |
bef4a34a | 744 | |
bd1f5d6a S |
745 | err_out: |
746 | storvsc_dev_remove(device); | |
747 | kmem_cache_destroy(host_dev->request_pool); | |
748 | scsi_host_put(host); | |
749 | return -ENODEV; | |
bef4a34a HJ |
750 | } |
751 | ||
7bd05b91 S |
752 | /* The one and only one */ |
753 | ||
40bf63ed | 754 | static struct hv_driver storvsc_drv = { |
768fa219 | 755 | .name = "storvsc", |
d847b5fe | 756 | .id_table = id_table, |
40bf63ed S |
757 | .probe = storvsc_probe, |
758 | .remove = storvsc_remove, | |
39ae6fae | 759 | }; |
7bd05b91 | 760 | |
d9bbae83 | 761 | static int __init storvsc_drv_init(void) |
f5c78872 | 762 | { |
01415ab3 S |
763 | u32 max_outstanding_req_per_channel; |
764 | ||
765 | /* | |
766 | * Divide the ring buffer data size (which is 1 page less | |
767 | * than the ring buffer size since that page is reserved for | |
768 | * the ring buffer indices) by the max request size (which is | |
769 | * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64) | |
770 | */ | |
01415ab3 | 771 | max_outstanding_req_per_channel = |
768fa219 GKH |
772 | ((storvsc_ringbuffer_size - PAGE_SIZE) / |
773 | ALIGN(MAX_MULTIPAGE_BUFFER_PACKET + | |
774 | sizeof(struct vstor_packet) + sizeof(u64), | |
775 | sizeof(u64))); | |
f5c78872 | 776 | |
01415ab3 | 777 | if (max_outstanding_req_per_channel < |
f5c78872 | 778 | STORVSC_MAX_IO_REQUESTS) |
b06efc19 | 779 | return -EINVAL; |
f5c78872 | 780 | |
768fa219 | 781 | return vmbus_driver_register(&storvsc_drv); |
f5c78872 S |
782 | } |
783 | ||
c63ba9e1 | 784 | static void __exit storvsc_drv_exit(void) |
f5c78872 | 785 | { |
768fa219 | 786 | vmbus_driver_unregister(&storvsc_drv); |
f5c78872 S |
787 | } |
788 | ||
ff568d3a | 789 | MODULE_LICENSE("GPL"); |
26c14cc1 | 790 | MODULE_VERSION(HV_DRV_VERSION); |
3afc7cc3 | 791 | MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver"); |
d9bbae83 | 792 | module_init(storvsc_drv_init); |
c63ba9e1 | 793 | module_exit(storvsc_drv_exit); |