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