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> |
e3fe0bb6 | 35 | #include "hv_api.h" |
645954c5 | 36 | #include "logging.h" |
2d82f6c7 | 37 | #include "version_info.h" |
870cde80 | 38 | #include "vmbus.h" |
bb969793 | 39 | #include "storvsc_api.h" |
af3043c6 S |
40 | #include "vstorage.h" |
41 | #include "channel.h" | |
bef4a34a | 42 | |
2db2cabe | 43 | static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE; |
bef4a34a | 44 | |
3d598ce1 S |
45 | module_param(storvsc_ringbuffer_size, int, S_IRUGO); |
46 | MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)"); | |
47 | ||
0f0cdc6a | 48 | static const char *driver_name = "storvsc"; |
6dec2442 S |
49 | |
50 | /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */ | |
51 | static const struct hv_guid gStorVscDeviceType = { | |
52 | .data = { | |
53 | 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d, | |
54 | 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f | |
55 | } | |
56 | }; | |
57 | ||
972621c9 | 58 | struct hv_host_device { |
97c15296 | 59 | struct hv_device *dev; |
ff568d3a GKH |
60 | struct kmem_cache *request_pool; |
61 | unsigned int port; | |
62 | unsigned char path; | |
63 | unsigned char target; | |
bef4a34a HJ |
64 | }; |
65 | ||
66 | struct storvsc_cmd_request { | |
ff568d3a GKH |
67 | struct list_head entry; |
68 | struct scsi_cmnd *cmd; | |
bef4a34a HJ |
69 | |
70 | unsigned int bounce_sgl_count; | |
ff568d3a | 71 | struct scatterlist *bounce_sgl; |
bef4a34a | 72 | |
0b3f6834 | 73 | struct hv_storvsc_request request; |
bef4a34a HJ |
74 | }; |
75 | ||
bef4a34a | 76 | |
6dec2442 | 77 | /* |
cf6fdfb3 | 78 | * storvsc_initialize - Main entry point |
6dec2442 | 79 | */ |
cf6fdfb3 | 80 | static int storvsc_initialize(struct hv_driver *driver) |
6dec2442 | 81 | { |
2e79505d | 82 | struct storvsc_driver *stor_driver; |
6dec2442 | 83 | |
ced01b0d | 84 | stor_driver = hvdr_to_stordr(driver); |
6dec2442 | 85 | |
6dec2442 S |
86 | |
87 | /* Make sure we are at least 2 pages since 1 page is used for control */ | |
88 | ||
0f0cdc6a | 89 | driver->name = driver_name; |
6dec2442 S |
90 | memcpy(&driver->dev_type, &gStorVscDeviceType, |
91 | sizeof(struct hv_guid)); | |
92 | ||
6dec2442 S |
93 | |
94 | /* | |
95 | * Divide the ring buffer data size (which is 1 page less | |
96 | * than the ring buffer size since that page is reserved for | |
97 | * the ring buffer indices) by the max request size (which is | |
98 | * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64) | |
99 | */ | |
100 | stor_driver->max_outstanding_req_per_channel = | |
101 | ((stor_driver->ring_buffer_size - PAGE_SIZE) / | |
102 | ALIGN(MAX_MULTIPAGE_BUFFER_PACKET + | |
103 | sizeof(struct vstor_packet) + sizeof(u64), | |
104 | sizeof(u64))); | |
105 | ||
106 | DPRINT_INFO(STORVSC, "max io %u, currently %u\n", | |
107 | stor_driver->max_outstanding_req_per_channel, | |
108 | STORVSC_MAX_IO_REQUESTS); | |
109 | ||
6dec2442 S |
110 | |
111 | return 0; | |
112 | } | |
113 | ||
5b60acee S |
114 | static int storvsc_device_alloc(struct scsi_device *sdevice) |
115 | { | |
116 | /* | |
117 | * This enables luns to be located sparsely. Otherwise, we may not | |
118 | * discovered them. | |
119 | */ | |
120 | sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN; | |
121 | return 0; | |
122 | } | |
123 | ||
a1ebfeae S |
124 | static int storvsc_merge_bvec(struct request_queue *q, |
125 | struct bvec_merge_data *bmd, struct bio_vec *bvec) | |
126 | { | |
127 | /* checking done by caller. */ | |
128 | return bvec->bv_len; | |
129 | } | |
130 | ||
419f2d03 S |
131 | static int storvsc_device_configure(struct scsi_device *sdevice) |
132 | { | |
133 | scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG, | |
134 | STORVSC_MAX_IO_REQUESTS); | |
135 | ||
136 | DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld", | |
137 | sdevice, PAGE_SIZE); | |
138 | blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE); | |
139 | ||
140 | DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine", | |
141 | sdevice); | |
142 | blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec); | |
143 | ||
144 | blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY); | |
419f2d03 S |
145 | |
146 | return 0; | |
147 | } | |
148 | ||
49a3c7a9 S |
149 | static void destroy_bounce_buffer(struct scatterlist *sgl, |
150 | unsigned int sg_count) | |
151 | { | |
152 | int i; | |
153 | struct page *page_buf; | |
154 | ||
155 | for (i = 0; i < sg_count; i++) { | |
156 | page_buf = sg_page((&sgl[i])); | |
157 | if (page_buf != NULL) | |
158 | __free_page(page_buf); | |
159 | } | |
160 | ||
161 | kfree(sgl); | |
162 | } | |
163 | ||
3862ef3e S |
164 | static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count) |
165 | { | |
166 | int i; | |
167 | ||
168 | /* No need to check */ | |
169 | if (sg_count < 2) | |
170 | return -1; | |
171 | ||
172 | /* We have at least 2 sg entries */ | |
173 | for (i = 0; i < sg_count; i++) { | |
174 | if (i == 0) { | |
175 | /* make sure 1st one does not have hole */ | |
176 | if (sgl[i].offset + sgl[i].length != PAGE_SIZE) | |
177 | return i; | |
178 | } else if (i == sg_count - 1) { | |
179 | /* make sure last one does not have hole */ | |
180 | if (sgl[i].offset != 0) | |
181 | return i; | |
182 | } else { | |
183 | /* make sure no hole in the middle */ | |
184 | if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0) | |
185 | return i; | |
186 | } | |
187 | } | |
188 | return -1; | |
189 | } | |
190 | ||
a9753cbd S |
191 | static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl, |
192 | unsigned int sg_count, | |
193 | unsigned int len) | |
194 | { | |
195 | int i; | |
196 | int num_pages; | |
197 | struct scatterlist *bounce_sgl; | |
198 | struct page *page_buf; | |
199 | ||
200 | num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT; | |
201 | ||
202 | bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC); | |
203 | if (!bounce_sgl) | |
204 | return NULL; | |
205 | ||
206 | for (i = 0; i < num_pages; i++) { | |
207 | page_buf = alloc_page(GFP_ATOMIC); | |
208 | if (!page_buf) | |
209 | goto cleanup; | |
210 | sg_set_page(&bounce_sgl[i], page_buf, 0, 0); | |
211 | } | |
212 | ||
213 | return bounce_sgl; | |
214 | ||
215 | cleanup: | |
216 | destroy_bounce_buffer(bounce_sgl, num_pages); | |
217 | return NULL; | |
218 | } | |
219 | ||
29fe2c9b S |
220 | |
221 | /* Assume the original sgl has enough room */ | |
222 | static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl, | |
223 | struct scatterlist *bounce_sgl, | |
224 | unsigned int orig_sgl_count) | |
225 | { | |
226 | int i; | |
227 | int j = 0; | |
228 | unsigned long src, dest; | |
229 | unsigned int srclen, destlen, copylen; | |
230 | unsigned int total_copied = 0; | |
231 | unsigned long bounce_addr = 0; | |
232 | unsigned long dest_addr = 0; | |
233 | unsigned long flags; | |
234 | ||
235 | local_irq_save(flags); | |
236 | ||
237 | for (i = 0; i < orig_sgl_count; i++) { | |
238 | dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), | |
239 | KM_IRQ0) + orig_sgl[i].offset; | |
240 | dest = dest_addr; | |
241 | destlen = orig_sgl[i].length; | |
242 | ||
243 | if (bounce_addr == 0) | |
244 | bounce_addr = | |
245 | (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), | |
246 | KM_IRQ0); | |
247 | ||
248 | while (destlen) { | |
249 | src = bounce_addr + bounce_sgl[j].offset; | |
250 | srclen = bounce_sgl[j].length - bounce_sgl[j].offset; | |
251 | ||
252 | copylen = min(srclen, destlen); | |
253 | memcpy((void *)dest, (void *)src, copylen); | |
254 | ||
255 | total_copied += copylen; | |
256 | bounce_sgl[j].offset += copylen; | |
257 | destlen -= copylen; | |
258 | dest += copylen; | |
259 | ||
260 | if (bounce_sgl[j].offset == bounce_sgl[j].length) { | |
261 | /* full */ | |
262 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); | |
263 | j++; | |
264 | ||
265 | /* if we need to use another bounce buffer */ | |
266 | if (destlen || i != orig_sgl_count - 1) | |
267 | bounce_addr = | |
268 | (unsigned long)kmap_atomic( | |
269 | sg_page((&bounce_sgl[j])), KM_IRQ0); | |
270 | } else if (destlen == 0 && i == orig_sgl_count - 1) { | |
271 | /* unmap the last bounce that is < PAGE_SIZE */ | |
272 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); | |
273 | } | |
274 | } | |
275 | ||
276 | kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset), | |
277 | KM_IRQ0); | |
278 | } | |
279 | ||
280 | local_irq_restore(flags); | |
281 | ||
282 | return total_copied; | |
283 | } | |
284 | ||
6a8ff44b S |
285 | |
286 | /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */ | |
287 | static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl, | |
288 | struct scatterlist *bounce_sgl, | |
289 | unsigned int orig_sgl_count) | |
290 | { | |
291 | int i; | |
292 | int j = 0; | |
293 | unsigned long src, dest; | |
294 | unsigned int srclen, destlen, copylen; | |
295 | unsigned int total_copied = 0; | |
296 | unsigned long bounce_addr = 0; | |
297 | unsigned long src_addr = 0; | |
298 | unsigned long flags; | |
299 | ||
300 | local_irq_save(flags); | |
301 | ||
302 | for (i = 0; i < orig_sgl_count; i++) { | |
303 | src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), | |
304 | KM_IRQ0) + orig_sgl[i].offset; | |
305 | src = src_addr; | |
306 | srclen = orig_sgl[i].length; | |
307 | ||
308 | if (bounce_addr == 0) | |
309 | bounce_addr = | |
310 | (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), | |
311 | KM_IRQ0); | |
312 | ||
313 | while (srclen) { | |
314 | /* assume bounce offset always == 0 */ | |
315 | dest = bounce_addr + bounce_sgl[j].length; | |
316 | destlen = PAGE_SIZE - bounce_sgl[j].length; | |
317 | ||
318 | copylen = min(srclen, destlen); | |
319 | memcpy((void *)dest, (void *)src, copylen); | |
320 | ||
321 | total_copied += copylen; | |
322 | bounce_sgl[j].length += copylen; | |
323 | srclen -= copylen; | |
324 | src += copylen; | |
325 | ||
326 | if (bounce_sgl[j].length == PAGE_SIZE) { | |
327 | /* full..move to next entry */ | |
328 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); | |
329 | j++; | |
330 | ||
331 | /* if we need to use another bounce buffer */ | |
332 | if (srclen || i != orig_sgl_count - 1) | |
333 | bounce_addr = | |
334 | (unsigned long)kmap_atomic( | |
335 | sg_page((&bounce_sgl[j])), KM_IRQ0); | |
336 | ||
337 | } else if (srclen == 0 && i == orig_sgl_count - 1) { | |
338 | /* unmap the last bounce that is < PAGE_SIZE */ | |
339 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); | |
340 | } | |
341 | } | |
342 | ||
343 | kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0); | |
344 | } | |
345 | ||
346 | local_irq_restore(flags); | |
347 | ||
348 | return total_copied; | |
349 | } | |
350 | ||
5c5c0234 S |
351 | |
352 | /* | |
353 | * storvsc_remove - Callback when our device is removed | |
354 | */ | |
355 | static int storvsc_remove(struct hv_device *dev) | |
356 | { | |
5c5c0234 S |
357 | struct Scsi_Host *host = dev_get_drvdata(&dev->device); |
358 | struct hv_host_device *host_dev = | |
359 | (struct hv_host_device *)host->hostdata; | |
360 | ||
361 | /* | |
362 | * Call to the vsc driver to let it know that the device is being | |
363 | * removed | |
364 | */ | |
6cdc57c0 | 365 | storvsc_dev_remove(dev); |
5c5c0234 S |
366 | |
367 | if (host_dev->request_pool) { | |
368 | kmem_cache_destroy(host_dev->request_pool); | |
369 | host_dev->request_pool = NULL; | |
370 | } | |
371 | ||
372 | DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host); | |
373 | scsi_remove_host(host); | |
374 | ||
375 | DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host); | |
376 | scsi_host_put(host); | |
377 | return 0; | |
378 | } | |
379 | ||
62838ce2 S |
380 | |
381 | static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev, | |
382 | sector_t capacity, int *info) | |
383 | { | |
384 | sector_t total_sectors = capacity; | |
385 | sector_t cylinder_times_heads = 0; | |
386 | sector_t temp = 0; | |
387 | ||
388 | int sectors_per_track = 0; | |
389 | int heads = 0; | |
390 | int cylinders = 0; | |
391 | int rem = 0; | |
392 | ||
393 | if (total_sectors > (65535 * 16 * 255)) | |
394 | total_sectors = (65535 * 16 * 255); | |
395 | ||
396 | if (total_sectors >= (65535 * 16 * 63)) { | |
397 | sectors_per_track = 255; | |
398 | heads = 16; | |
399 | ||
400 | cylinder_times_heads = total_sectors; | |
401 | /* sector_div stores the quotient in cylinder_times_heads */ | |
402 | rem = sector_div(cylinder_times_heads, sectors_per_track); | |
403 | } else { | |
404 | sectors_per_track = 17; | |
405 | ||
406 | cylinder_times_heads = total_sectors; | |
407 | /* sector_div stores the quotient in cylinder_times_heads */ | |
408 | rem = sector_div(cylinder_times_heads, sectors_per_track); | |
409 | ||
410 | temp = cylinder_times_heads + 1023; | |
411 | /* sector_div stores the quotient in temp */ | |
412 | rem = sector_div(temp, 1024); | |
413 | ||
414 | heads = temp; | |
415 | ||
416 | if (heads < 4) | |
417 | heads = 4; | |
418 | ||
419 | if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) { | |
420 | sectors_per_track = 31; | |
421 | heads = 16; | |
422 | ||
423 | cylinder_times_heads = total_sectors; | |
424 | /* | |
425 | * sector_div stores the quotient in | |
426 | * cylinder_times_heads | |
427 | */ | |
428 | rem = sector_div(cylinder_times_heads, | |
429 | sectors_per_track); | |
430 | } | |
431 | ||
432 | if (cylinder_times_heads >= (heads * 1024)) { | |
433 | sectors_per_track = 63; | |
434 | heads = 16; | |
435 | ||
436 | cylinder_times_heads = total_sectors; | |
437 | /* | |
438 | * sector_div stores the quotient in | |
439 | * cylinder_times_heads | |
440 | */ | |
441 | rem = sector_div(cylinder_times_heads, | |
442 | sectors_per_track); | |
443 | } | |
444 | } | |
445 | ||
446 | temp = cylinder_times_heads; | |
447 | /* sector_div stores the quotient in temp */ | |
448 | rem = sector_div(temp, heads); | |
449 | cylinders = temp; | |
450 | ||
451 | info[0] = heads; | |
452 | info[1] = sectors_per_track; | |
453 | info[2] = cylinders; | |
454 | ||
455 | DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads, | |
456 | sectors_per_track); | |
457 | ||
458 | return 0; | |
459 | } | |
aa3d789e S |
460 | |
461 | static int storvsc_host_reset(struct hv_device *device) | |
462 | { | |
463 | struct storvsc_device *stor_device; | |
464 | struct hv_storvsc_request *request; | |
465 | struct vstor_packet *vstor_packet; | |
466 | int ret, t; | |
467 | ||
468 | DPRINT_INFO(STORVSC, "resetting host adapter..."); | |
469 | ||
470 | stor_device = get_stor_device(device); | |
471 | if (!stor_device) | |
472 | return -1; | |
473 | ||
474 | request = &stor_device->reset_request; | |
475 | vstor_packet = &request->vstor_packet; | |
476 | ||
477 | init_completion(&request->wait_event); | |
478 | ||
479 | vstor_packet->operation = VSTOR_OPERATION_RESET_BUS; | |
480 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; | |
481 | vstor_packet->vm_srb.path_id = stor_device->path_id; | |
482 | ||
483 | ret = vmbus_sendpacket(device->channel, vstor_packet, | |
484 | sizeof(struct vstor_packet), | |
485 | (unsigned long)&stor_device->reset_request, | |
486 | VM_PKT_DATA_INBAND, | |
487 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
488 | if (ret != 0) | |
489 | goto cleanup; | |
490 | ||
491 | t = wait_for_completion_timeout(&request->wait_event, HZ); | |
492 | if (t == 0) { | |
493 | ret = -ETIMEDOUT; | |
494 | goto cleanup; | |
495 | } | |
496 | ||
497 | DPRINT_INFO(STORVSC, "host adapter reset completed"); | |
498 | ||
499 | /* | |
500 | * At this point, all outstanding requests in the adapter | |
501 | * should have been flushed out and return to us | |
502 | */ | |
503 | ||
504 | cleanup: | |
505 | put_stor_device(device); | |
506 | return ret; | |
507 | } | |
508 | ||
509 | ||
96e690be S |
510 | /* |
511 | * storvsc_host_reset_handler - Reset the scsi HBA | |
512 | */ | |
513 | static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd) | |
514 | { | |
515 | int ret; | |
516 | struct hv_host_device *host_dev = | |
517 | (struct hv_host_device *)scmnd->device->host->hostdata; | |
518 | struct hv_device *dev = host_dev->dev; | |
519 | ||
520 | DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...", | |
521 | scmnd->device, dev); | |
522 | ||
523 | /* Invokes the vsc to reset the host/bus */ | |
524 | ret = storvsc_host_reset(dev); | |
525 | if (ret != 0) | |
526 | return ret; | |
527 | ||
528 | DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted", | |
529 | scmnd->device, dev); | |
530 | ||
531 | return ret; | |
532 | } | |
533 | ||
8a411bad S |
534 | |
535 | /* | |
536 | * storvsc_commmand_completion - Command completion processing | |
537 | */ | |
538 | static void storvsc_commmand_completion(struct hv_storvsc_request *request) | |
539 | { | |
540 | struct storvsc_cmd_request *cmd_request = | |
541 | (struct storvsc_cmd_request *)request->context; | |
542 | struct scsi_cmnd *scmnd = cmd_request->cmd; | |
543 | struct hv_host_device *host_dev = | |
544 | (struct hv_host_device *)scmnd->device->host->hostdata; | |
545 | void (*scsi_done_fn)(struct scsi_cmnd *); | |
546 | struct scsi_sense_hdr sense_hdr; | |
547 | struct vmscsi_request *vm_srb; | |
548 | ||
8a411bad | 549 | if (cmd_request->bounce_sgl_count) { |
8a411bad S |
550 | |
551 | /* FIXME: We can optimize on writes by just skipping this */ | |
552 | copy_from_bounce_buffer(scsi_sglist(scmnd), | |
553 | cmd_request->bounce_sgl, | |
554 | scsi_sg_count(scmnd)); | |
555 | destroy_bounce_buffer(cmd_request->bounce_sgl, | |
556 | cmd_request->bounce_sgl_count); | |
557 | } | |
558 | ||
559 | vm_srb = &request->vstor_packet.vm_srb; | |
560 | scmnd->result = vm_srb->scsi_status; | |
561 | ||
562 | if (scmnd->result) { | |
563 | if (scsi_normalize_sense(scmnd->sense_buffer, | |
564 | SCSI_SENSE_BUFFERSIZE, &sense_hdr)) | |
565 | scsi_print_sense_hdr("storvsc", &sense_hdr); | |
566 | } | |
567 | ||
8a411bad S |
568 | scsi_set_resid(scmnd, |
569 | request->data_buffer.len - | |
570 | vm_srb->data_transfer_length); | |
571 | ||
572 | scsi_done_fn = scmnd->scsi_done; | |
573 | ||
574 | scmnd->host_scribble = NULL; | |
575 | scmnd->scsi_done = NULL; | |
576 | ||
577 | /* !!DO NOT MODIFY the scmnd after this call */ | |
578 | scsi_done_fn(scmnd); | |
579 | ||
580 | kmem_cache_free(host_dev->request_pool, cmd_request); | |
581 | } | |
582 | ||
c5b463ae S |
583 | |
584 | /* | |
585 | * storvsc_queuecommand - Initiate command processing | |
586 | */ | |
587 | static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd, | |
588 | void (*done)(struct scsi_cmnd *)) | |
589 | { | |
590 | int ret; | |
591 | struct hv_host_device *host_dev = | |
592 | (struct hv_host_device *)scmnd->device->host->hostdata; | |
593 | struct hv_device *dev = host_dev->dev; | |
c5b463ae S |
594 | struct hv_storvsc_request *request; |
595 | struct storvsc_cmd_request *cmd_request; | |
596 | unsigned int request_size = 0; | |
597 | int i; | |
598 | struct scatterlist *sgl; | |
599 | unsigned int sg_count = 0; | |
600 | struct vmscsi_request *vm_srb; | |
601 | ||
602 | ||
603 | /* If retrying, no need to prep the cmd */ | |
604 | if (scmnd->host_scribble) { | |
c5b463ae S |
605 | |
606 | cmd_request = | |
607 | (struct storvsc_cmd_request *)scmnd->host_scribble; | |
608 | DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p", | |
609 | scmnd, cmd_request); | |
610 | ||
611 | goto retry_request; | |
612 | } | |
613 | ||
c5b463ae S |
614 | scmnd->scsi_done = done; |
615 | ||
616 | request_size = sizeof(struct storvsc_cmd_request); | |
617 | ||
618 | cmd_request = kmem_cache_zalloc(host_dev->request_pool, | |
619 | GFP_ATOMIC); | |
620 | if (!cmd_request) { | |
621 | scmnd->scsi_done = NULL; | |
622 | return SCSI_MLQUEUE_DEVICE_BUSY; | |
623 | } | |
624 | ||
625 | /* Setup the cmd request */ | |
626 | cmd_request->bounce_sgl_count = 0; | |
627 | cmd_request->bounce_sgl = NULL; | |
628 | cmd_request->cmd = scmnd; | |
629 | ||
630 | scmnd->host_scribble = (unsigned char *)cmd_request; | |
631 | ||
632 | request = &cmd_request->request; | |
633 | vm_srb = &request->vstor_packet.vm_srb; | |
634 | ||
635 | ||
636 | /* Build the SRB */ | |
637 | switch (scmnd->sc_data_direction) { | |
638 | case DMA_TO_DEVICE: | |
639 | vm_srb->data_in = WRITE_TYPE; | |
640 | break; | |
641 | case DMA_FROM_DEVICE: | |
642 | vm_srb->data_in = READ_TYPE; | |
643 | break; | |
644 | default: | |
645 | vm_srb->data_in = UNKNOWN_TYPE; | |
646 | break; | |
647 | } | |
648 | ||
649 | request->on_io_completion = storvsc_commmand_completion; | |
650 | request->context = cmd_request;/* scmnd; */ | |
651 | ||
c5b463ae S |
652 | vm_srb->port_number = host_dev->port; |
653 | vm_srb->path_id = scmnd->device->channel; | |
654 | vm_srb->target_id = scmnd->device->id; | |
655 | vm_srb->lun = scmnd->device->lun; | |
656 | ||
c5b463ae S |
657 | vm_srb->cdb_length = scmnd->cmd_len; |
658 | ||
659 | memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length); | |
660 | ||
661 | request->sense_buffer = scmnd->sense_buffer; | |
662 | ||
663 | ||
664 | request->data_buffer.len = scsi_bufflen(scmnd); | |
665 | if (scsi_sg_count(scmnd)) { | |
666 | sgl = (struct scatterlist *)scsi_sglist(scmnd); | |
667 | sg_count = scsi_sg_count(scmnd); | |
668 | ||
669 | /* check if we need to bounce the sgl */ | |
670 | if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) { | |
671 | cmd_request->bounce_sgl = | |
672 | create_bounce_buffer(sgl, scsi_sg_count(scmnd), | |
673 | scsi_bufflen(scmnd)); | |
674 | if (!cmd_request->bounce_sgl) { | |
675 | scmnd->scsi_done = NULL; | |
676 | scmnd->host_scribble = NULL; | |
677 | kmem_cache_free(host_dev->request_pool, | |
678 | cmd_request); | |
679 | ||
680 | return SCSI_MLQUEUE_HOST_BUSY; | |
681 | } | |
682 | ||
683 | cmd_request->bounce_sgl_count = | |
684 | ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >> | |
685 | PAGE_SHIFT; | |
686 | ||
687 | /* | |
688 | * FIXME: We can optimize on reads by just skipping | |
689 | * this | |
690 | */ | |
691 | copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl, | |
692 | scsi_sg_count(scmnd)); | |
693 | ||
694 | sgl = cmd_request->bounce_sgl; | |
695 | sg_count = cmd_request->bounce_sgl_count; | |
696 | } | |
697 | ||
698 | request->data_buffer.offset = sgl[0].offset; | |
699 | ||
700 | for (i = 0; i < sg_count; i++) | |
701 | request->data_buffer.pfn_array[i] = | |
702 | page_to_pfn(sg_page((&sgl[i]))); | |
703 | ||
704 | } else if (scsi_sglist(scmnd)) { | |
c5b463ae S |
705 | request->data_buffer.offset = |
706 | virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1); | |
707 | request->data_buffer.pfn_array[0] = | |
708 | virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT; | |
709 | } | |
710 | ||
711 | retry_request: | |
712 | /* Invokes the vsc to start an IO */ | |
636f0fd1 S |
713 | ret = storvsc_do_io(dev, &cmd_request->request); |
714 | ||
c5b463ae S |
715 | if (ret == -1) { |
716 | /* no more space */ | |
717 | ||
718 | if (cmd_request->bounce_sgl_count) { | |
719 | /* | |
720 | * FIXME: We can optimize on writes by just skipping | |
721 | * this | |
722 | */ | |
723 | copy_from_bounce_buffer(scsi_sglist(scmnd), | |
724 | cmd_request->bounce_sgl, | |
725 | scsi_sg_count(scmnd)); | |
726 | destroy_bounce_buffer(cmd_request->bounce_sgl, | |
727 | cmd_request->bounce_sgl_count); | |
728 | } | |
729 | ||
730 | kmem_cache_free(host_dev->request_pool, cmd_request); | |
731 | ||
732 | scmnd->scsi_done = NULL; | |
733 | scmnd->host_scribble = NULL; | |
734 | ||
735 | ret = SCSI_MLQUEUE_DEVICE_BUSY; | |
736 | } | |
737 | ||
738 | return ret; | |
739 | } | |
740 | ||
741 | static DEF_SCSI_QCMD(storvsc_queuecommand) | |
742 | ||
bef4a34a | 743 | |
454f18a9 | 744 | /* Scsi driver */ |
bef4a34a | 745 | static struct scsi_host_template scsi_driver = { |
ff568d3a GKH |
746 | .module = THIS_MODULE, |
747 | .name = "storvsc_host_t", | |
748 | .bios_param = storvsc_get_chs, | |
749 | .queuecommand = storvsc_queuecommand, | |
750 | .eh_host_reset_handler = storvsc_host_reset_handler, | |
751 | .slave_alloc = storvsc_device_alloc, | |
752 | .slave_configure = storvsc_device_configure, | |
753 | .cmd_per_lun = 1, | |
754 | /* 64 max_queue * 1 target */ | |
0686e4f4 | 755 | .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS, |
ff568d3a | 756 | .this_id = -1, |
454f18a9 | 757 | /* no use setting to 0 since ll_blk_rw reset it to 1 */ |
ff568d3a GKH |
758 | /* currently 32 */ |
759 | .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT, | |
760 | /* | |
761 | * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge | |
762 | * into 1 sg element. If set, we must limit the max_segment_size to | |
763 | * PAGE_SIZE, otherwise we may get 1 sg element that represents | |
764 | * multiple | |
765 | */ | |
454f18a9 | 766 | /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */ |
ff568d3a | 767 | .use_clustering = ENABLE_CLUSTERING, |
454f18a9 | 768 | /* Make sure we dont get a sg segment crosses a page boundary */ |
ff568d3a | 769 | .dma_boundary = PAGE_SIZE-1, |
bef4a34a HJ |
770 | }; |
771 | ||
772 | ||
3e189519 | 773 | /* |
ff568d3a GKH |
774 | * storvsc_probe - Add a new device for this driver |
775 | */ | |
f5c78872 | 776 | |
9efd21e1 | 777 | static int storvsc_probe(struct hv_device *device) |
bef4a34a | 778 | { |
ff568d3a | 779 | int ret; |
bef4a34a | 780 | struct Scsi_Host *host; |
795b613d | 781 | struct hv_host_device *host_dev; |
9f0c7d2c | 782 | struct storvsc_device_info device_info; |
bef4a34a | 783 | |
ff568d3a | 784 | host = scsi_host_alloc(&scsi_driver, |
972621c9 | 785 | sizeof(struct hv_host_device)); |
f8feed06 | 786 | if (!host) |
bef4a34a | 787 | return -ENOMEM; |
bef4a34a | 788 | |
9efd21e1 | 789 | dev_set_drvdata(&device->device, host); |
bef4a34a | 790 | |
795b613d S |
791 | host_dev = (struct hv_host_device *)host->hostdata; |
792 | memset(host_dev, 0, sizeof(struct hv_host_device)); | |
bef4a34a | 793 | |
795b613d | 794 | host_dev->port = host->host_no; |
97c15296 | 795 | host_dev->dev = device; |
bef4a34a | 796 | |
795b613d | 797 | host_dev->request_pool = |
9efd21e1 | 798 | kmem_cache_create(dev_name(&device->device), |
1e05d88e | 799 | sizeof(struct storvsc_cmd_request), 0, |
ff568d3a GKH |
800 | SLAB_HWCACHE_ALIGN, NULL); |
801 | ||
795b613d | 802 | if (!host_dev->request_pool) { |
bef4a34a | 803 | scsi_host_put(host); |
bef4a34a HJ |
804 | return -ENOMEM; |
805 | } | |
806 | ||
8a046024 | 807 | device_info.port_number = host->host_no; |
454f18a9 | 808 | /* Call to the vsc driver to add the device */ |
58f1f5cb | 809 | ret = storvsc_dev_add(device, (void *)&device_info); |
9efd21e1 | 810 | |
ff568d3a | 811 | if (ret != 0) { |
795b613d | 812 | kmem_cache_destroy(host_dev->request_pool); |
bef4a34a | 813 | scsi_host_put(host); |
bef4a34a HJ |
814 | return -1; |
815 | } | |
816 | ||
795b613d S |
817 | host_dev->path = device_info.path_id; |
818 | host_dev->target = device_info.target_id; | |
bef4a34a | 819 | |
ff568d3a GKH |
820 | /* max # of devices per target */ |
821 | host->max_lun = STORVSC_MAX_LUNS_PER_TARGET; | |
822 | /* max # of targets per channel */ | |
823 | host->max_id = STORVSC_MAX_TARGETS; | |
824 | /* max # of channels */ | |
825 | host->max_channel = STORVSC_MAX_CHANNELS - 1; | |
bef4a34a | 826 | |
454f18a9 | 827 | /* Register the HBA and start the scsi bus scan */ |
9efd21e1 | 828 | ret = scsi_add_host(host, &device->device); |
ff568d3a | 829 | if (ret != 0) { |
bef4a34a | 830 | |
6cdc57c0 | 831 | storvsc_dev_remove(device); |
bef4a34a | 832 | |
795b613d | 833 | kmem_cache_destroy(host_dev->request_pool); |
bef4a34a | 834 | scsi_host_put(host); |
bef4a34a HJ |
835 | return -1; |
836 | } | |
837 | ||
838 | scsi_scan_host(host); | |
bef4a34a HJ |
839 | return ret; |
840 | } | |
841 | ||
7bd05b91 S |
842 | /* The one and only one */ |
843 | ||
39ae6fae S |
844 | static struct storvsc_driver storvsc_drv = { |
845 | .base.probe = storvsc_probe, | |
846 | .base.remove = storvsc_remove, | |
847 | }; | |
7bd05b91 S |
848 | |
849 | ||
f5c78872 S |
850 | /* |
851 | * storvsc_drv_init - StorVsc driver initialization. | |
852 | */ | |
853 | static int storvsc_drv_init(void) | |
854 | { | |
855 | int ret; | |
565c38de S |
856 | struct storvsc_driver *storvsc_drv_obj = &storvsc_drv; |
857 | struct hv_driver *drv = &storvsc_drv.base; | |
f5c78872 S |
858 | |
859 | storvsc_drv_obj->ring_buffer_size = storvsc_ringbuffer_size; | |
860 | ||
861 | /* Callback to client driver to complete the initialization */ | |
862 | storvsc_initialize(&storvsc_drv_obj->base); | |
863 | ||
864 | DPRINT_INFO(STORVSC_DRV, | |
865 | "max outstanding reqs %u", | |
866 | storvsc_drv_obj->max_outstanding_req_per_channel); | |
867 | ||
868 | if (storvsc_drv_obj->max_outstanding_req_per_channel < | |
869 | STORVSC_MAX_IO_REQUESTS) | |
870 | return -1; | |
871 | ||
872 | drv->driver.name = storvsc_drv_obj->base.name; | |
873 | ||
f5c78872 S |
874 | |
875 | /* The driver belongs to vmbus */ | |
876 | ret = vmbus_child_driver_register(&drv->driver); | |
877 | ||
878 | return ret; | |
879 | } | |
880 | ||
881 | static int storvsc_drv_exit_cb(struct device *dev, void *data) | |
882 | { | |
883 | struct device **curr = (struct device **)data; | |
884 | *curr = dev; | |
885 | return 1; /* stop iterating */ | |
886 | } | |
887 | ||
888 | static void storvsc_drv_exit(void) | |
889 | { | |
565c38de S |
890 | struct storvsc_driver *storvsc_drv_obj = &storvsc_drv; |
891 | struct hv_driver *drv = &storvsc_drv.base; | |
f5c78872 S |
892 | struct device *current_dev = NULL; |
893 | int ret; | |
894 | ||
895 | while (1) { | |
896 | current_dev = NULL; | |
897 | ||
898 | /* Get the device */ | |
899 | ret = driver_for_each_device(&drv->driver, NULL, | |
900 | (void *) ¤t_dev, | |
901 | storvsc_drv_exit_cb); | |
902 | ||
903 | ||
904 | if (current_dev == NULL) | |
905 | break; | |
906 | ||
907 | /* Initiate removal from the top-down */ | |
908 | device_unregister(current_dev); | |
909 | } | |
910 | ||
911 | if (storvsc_drv_obj->base.cleanup) | |
912 | storvsc_drv_obj->base.cleanup(&storvsc_drv_obj->base); | |
913 | ||
914 | vmbus_child_driver_unregister(&drv->driver); | |
915 | return; | |
916 | } | |
917 | ||
bef4a34a HJ |
918 | static int __init storvsc_init(void) |
919 | { | |
920 | int ret; | |
921 | ||
bef4a34a | 922 | DPRINT_INFO(STORVSC_DRV, "Storvsc initializing...."); |
db085777 | 923 | ret = storvsc_drv_init(); |
bef4a34a HJ |
924 | return ret; |
925 | } | |
926 | ||
927 | static void __exit storvsc_exit(void) | |
928 | { | |
bef4a34a | 929 | storvsc_drv_exit(); |
bef4a34a HJ |
930 | } |
931 | ||
ff568d3a | 932 | MODULE_LICENSE("GPL"); |
26c14cc1 | 933 | MODULE_VERSION(HV_DRV_VERSION); |
3afc7cc3 | 934 | MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver"); |
bef4a34a HJ |
935 | module_init(storvsc_init); |
936 | module_exit(storvsc_exit); |