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 | ||
c5b463ae S |
600 | |
601 | /* | |
602 | * storvsc_queuecommand - Initiate command processing | |
603 | */ | |
604 | static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd, | |
605 | void (*done)(struct scsi_cmnd *)) | |
606 | { | |
607 | int ret; | |
608 | struct hv_host_device *host_dev = | |
609 | (struct hv_host_device *)scmnd->device->host->hostdata; | |
610 | struct hv_device *dev = host_dev->dev; | |
611 | struct storvsc_driver *storvsc_drv_obj = | |
612 | drv_to_stordrv(dev->device.driver); | |
613 | struct hv_storvsc_request *request; | |
614 | struct storvsc_cmd_request *cmd_request; | |
615 | unsigned int request_size = 0; | |
616 | int i; | |
617 | struct scatterlist *sgl; | |
618 | unsigned int sg_count = 0; | |
619 | struct vmscsi_request *vm_srb; | |
620 | ||
621 | ||
622 | /* If retrying, no need to prep the cmd */ | |
623 | if (scmnd->host_scribble) { | |
624 | /* ASSERT(scmnd->scsi_done != NULL); */ | |
625 | ||
626 | cmd_request = | |
627 | (struct storvsc_cmd_request *)scmnd->host_scribble; | |
628 | DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p", | |
629 | scmnd, cmd_request); | |
630 | ||
631 | goto retry_request; | |
632 | } | |
633 | ||
634 | /* ASSERT(scmnd->scsi_done == NULL); */ | |
635 | /* ASSERT(scmnd->host_scribble == NULL); */ | |
636 | ||
637 | scmnd->scsi_done = done; | |
638 | ||
639 | request_size = sizeof(struct storvsc_cmd_request); | |
640 | ||
641 | cmd_request = kmem_cache_zalloc(host_dev->request_pool, | |
642 | GFP_ATOMIC); | |
643 | if (!cmd_request) { | |
644 | scmnd->scsi_done = NULL; | |
645 | return SCSI_MLQUEUE_DEVICE_BUSY; | |
646 | } | |
647 | ||
648 | /* Setup the cmd request */ | |
649 | cmd_request->bounce_sgl_count = 0; | |
650 | cmd_request->bounce_sgl = NULL; | |
651 | cmd_request->cmd = scmnd; | |
652 | ||
653 | scmnd->host_scribble = (unsigned char *)cmd_request; | |
654 | ||
655 | request = &cmd_request->request; | |
656 | vm_srb = &request->vstor_packet.vm_srb; | |
657 | ||
658 | ||
659 | /* Build the SRB */ | |
660 | switch (scmnd->sc_data_direction) { | |
661 | case DMA_TO_DEVICE: | |
662 | vm_srb->data_in = WRITE_TYPE; | |
663 | break; | |
664 | case DMA_FROM_DEVICE: | |
665 | vm_srb->data_in = READ_TYPE; | |
666 | break; | |
667 | default: | |
668 | vm_srb->data_in = UNKNOWN_TYPE; | |
669 | break; | |
670 | } | |
671 | ||
672 | request->on_io_completion = storvsc_commmand_completion; | |
673 | request->context = cmd_request;/* scmnd; */ | |
674 | ||
675 | /* request->PortId = scmnd->device->channel; */ | |
676 | vm_srb->port_number = host_dev->port; | |
677 | vm_srb->path_id = scmnd->device->channel; | |
678 | vm_srb->target_id = scmnd->device->id; | |
679 | vm_srb->lun = scmnd->device->lun; | |
680 | ||
681 | /* ASSERT(scmnd->cmd_len <= 16); */ | |
682 | vm_srb->cdb_length = scmnd->cmd_len; | |
683 | ||
684 | memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length); | |
685 | ||
686 | request->sense_buffer = scmnd->sense_buffer; | |
687 | ||
688 | ||
689 | request->data_buffer.len = scsi_bufflen(scmnd); | |
690 | if (scsi_sg_count(scmnd)) { | |
691 | sgl = (struct scatterlist *)scsi_sglist(scmnd); | |
692 | sg_count = scsi_sg_count(scmnd); | |
693 | ||
694 | /* check if we need to bounce the sgl */ | |
695 | if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) { | |
696 | cmd_request->bounce_sgl = | |
697 | create_bounce_buffer(sgl, scsi_sg_count(scmnd), | |
698 | scsi_bufflen(scmnd)); | |
699 | if (!cmd_request->bounce_sgl) { | |
700 | scmnd->scsi_done = NULL; | |
701 | scmnd->host_scribble = NULL; | |
702 | kmem_cache_free(host_dev->request_pool, | |
703 | cmd_request); | |
704 | ||
705 | return SCSI_MLQUEUE_HOST_BUSY; | |
706 | } | |
707 | ||
708 | cmd_request->bounce_sgl_count = | |
709 | ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >> | |
710 | PAGE_SHIFT; | |
711 | ||
712 | /* | |
713 | * FIXME: We can optimize on reads by just skipping | |
714 | * this | |
715 | */ | |
716 | copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl, | |
717 | scsi_sg_count(scmnd)); | |
718 | ||
719 | sgl = cmd_request->bounce_sgl; | |
720 | sg_count = cmd_request->bounce_sgl_count; | |
721 | } | |
722 | ||
723 | request->data_buffer.offset = sgl[0].offset; | |
724 | ||
725 | for (i = 0; i < sg_count; i++) | |
726 | request->data_buffer.pfn_array[i] = | |
727 | page_to_pfn(sg_page((&sgl[i]))); | |
728 | ||
729 | } else if (scsi_sglist(scmnd)) { | |
730 | /* ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); */ | |
731 | request->data_buffer.offset = | |
732 | virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1); | |
733 | request->data_buffer.pfn_array[0] = | |
734 | virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT; | |
735 | } | |
736 | ||
737 | retry_request: | |
738 | /* Invokes the vsc to start an IO */ | |
739 | ret = storvsc_drv_obj->on_io_request(dev, | |
740 | &cmd_request->request); | |
741 | if (ret == -1) { | |
742 | /* no more space */ | |
743 | ||
744 | if (cmd_request->bounce_sgl_count) { | |
745 | /* | |
746 | * FIXME: We can optimize on writes by just skipping | |
747 | * this | |
748 | */ | |
749 | copy_from_bounce_buffer(scsi_sglist(scmnd), | |
750 | cmd_request->bounce_sgl, | |
751 | scsi_sg_count(scmnd)); | |
752 | destroy_bounce_buffer(cmd_request->bounce_sgl, | |
753 | cmd_request->bounce_sgl_count); | |
754 | } | |
755 | ||
756 | kmem_cache_free(host_dev->request_pool, cmd_request); | |
757 | ||
758 | scmnd->scsi_done = NULL; | |
759 | scmnd->host_scribble = NULL; | |
760 | ||
761 | ret = SCSI_MLQUEUE_DEVICE_BUSY; | |
762 | } | |
763 | ||
764 | return ret; | |
765 | } | |
766 | ||
767 | static DEF_SCSI_QCMD(storvsc_queuecommand) | |
768 | ||
454f18a9 | 769 | /* Static decl */ |
9efd21e1 | 770 | static int storvsc_probe(struct hv_device *dev); |
bef4a34a | 771 | |
454f18a9 | 772 | /* The one and only one */ |
2e79505d | 773 | static struct storvsc_driver g_storvsc_drv; |
bef4a34a | 774 | |
454f18a9 | 775 | /* Scsi driver */ |
bef4a34a | 776 | static struct scsi_host_template scsi_driver = { |
ff568d3a GKH |
777 | .module = THIS_MODULE, |
778 | .name = "storvsc_host_t", | |
779 | .bios_param = storvsc_get_chs, | |
780 | .queuecommand = storvsc_queuecommand, | |
781 | .eh_host_reset_handler = storvsc_host_reset_handler, | |
782 | .slave_alloc = storvsc_device_alloc, | |
783 | .slave_configure = storvsc_device_configure, | |
784 | .cmd_per_lun = 1, | |
785 | /* 64 max_queue * 1 target */ | |
0686e4f4 | 786 | .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS, |
ff568d3a | 787 | .this_id = -1, |
454f18a9 | 788 | /* no use setting to 0 since ll_blk_rw reset it to 1 */ |
ff568d3a GKH |
789 | /* currently 32 */ |
790 | .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT, | |
791 | /* | |
792 | * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge | |
793 | * into 1 sg element. If set, we must limit the max_segment_size to | |
794 | * PAGE_SIZE, otherwise we may get 1 sg element that represents | |
795 | * multiple | |
796 | */ | |
454f18a9 | 797 | /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */ |
ff568d3a | 798 | .use_clustering = ENABLE_CLUSTERING, |
454f18a9 | 799 | /* Make sure we dont get a sg segment crosses a page boundary */ |
ff568d3a | 800 | .dma_boundary = PAGE_SIZE-1, |
bef4a34a HJ |
801 | }; |
802 | ||
803 | ||
3e189519 | 804 | /* |
ff568d3a GKH |
805 | * storvsc_drv_init - StorVsc driver initialization. |
806 | */ | |
db085777 | 807 | static int storvsc_drv_init(void) |
bef4a34a | 808 | { |
ff568d3a | 809 | int ret; |
2e79505d | 810 | struct storvsc_driver *storvsc_drv_obj = &g_storvsc_drv; |
4af27d70 | 811 | struct hv_driver *drv = &g_storvsc_drv.base; |
bef4a34a | 812 | |
8a046024 | 813 | storvsc_drv_obj->ring_buffer_size = storvsc_ringbuffer_size; |
bef4a34a | 814 | |
454f18a9 | 815 | /* Callback to client driver to complete the initialization */ |
cf6fdfb3 | 816 | storvsc_initialize(&storvsc_drv_obj->base); |
bef4a34a | 817 | |
ff568d3a | 818 | DPRINT_INFO(STORVSC_DRV, |
1e05d88e | 819 | "max outstanding reqs %u", |
8a046024 | 820 | storvsc_drv_obj->max_outstanding_req_per_channel); |
ff568d3a | 821 | |
8a046024 | 822 | if (storvsc_drv_obj->max_outstanding_req_per_channel < |
f8feed06 | 823 | STORVSC_MAX_IO_REQUESTS) |
bef4a34a | 824 | return -1; |
bef4a34a | 825 | |
150f9398 | 826 | drv->driver.name = storvsc_drv_obj->base.name; |
bef4a34a | 827 | |
9efd21e1 | 828 | drv->probe = storvsc_probe; |
415b023a | 829 | drv->remove = storvsc_remove; |
bef4a34a | 830 | |
454f18a9 | 831 | /* The driver belongs to vmbus */ |
150f9398 | 832 | ret = vmbus_child_driver_register(&drv->driver); |
bef4a34a | 833 | |
bef4a34a HJ |
834 | return ret; |
835 | } | |
836 | ||
bef4a34a HJ |
837 | static int storvsc_drv_exit_cb(struct device *dev, void *data) |
838 | { | |
839 | struct device **curr = (struct device **)data; | |
840 | *curr = dev; | |
454f18a9 | 841 | return 1; /* stop iterating */ |
bef4a34a HJ |
842 | } |
843 | ||
bd1de709 | 844 | static void storvsc_drv_exit(void) |
bef4a34a | 845 | { |
2e79505d | 846 | struct storvsc_driver *storvsc_drv_obj = &g_storvsc_drv; |
4af27d70 | 847 | struct hv_driver *drv = &g_storvsc_drv.base; |
ff568d3a | 848 | struct device *current_dev = NULL; |
2295ba2e | 849 | int ret; |
bef4a34a | 850 | |
ff568d3a | 851 | while (1) { |
bef4a34a HJ |
852 | current_dev = NULL; |
853 | ||
454f18a9 | 854 | /* Get the device */ |
150f9398 | 855 | ret = driver_for_each_device(&drv->driver, NULL, |
2295ba2e BP |
856 | (void *) ¤t_dev, |
857 | storvsc_drv_exit_cb); | |
858 | ||
bef4a34a HJ |
859 | |
860 | if (current_dev == NULL) | |
861 | break; | |
862 | ||
454f18a9 | 863 | /* Initiate removal from the top-down */ |
bef4a34a HJ |
864 | device_unregister(current_dev); |
865 | } | |
866 | ||
ca623ad3 HZ |
867 | if (storvsc_drv_obj->base.cleanup) |
868 | storvsc_drv_obj->base.cleanup(&storvsc_drv_obj->base); | |
bef4a34a | 869 | |
150f9398 | 870 | vmbus_child_driver_unregister(&drv->driver); |
bef4a34a HJ |
871 | return; |
872 | } | |
873 | ||
3e189519 | 874 | /* |
ff568d3a GKH |
875 | * storvsc_probe - Add a new device for this driver |
876 | */ | |
9efd21e1 | 877 | static int storvsc_probe(struct hv_device *device) |
bef4a34a | 878 | { |
ff568d3a | 879 | int ret; |
2e79505d | 880 | struct storvsc_driver *storvsc_drv_obj = |
9efd21e1 | 881 | drv_to_stordrv(device->device.driver); |
bef4a34a | 882 | struct Scsi_Host *host; |
795b613d | 883 | struct hv_host_device *host_dev; |
9f0c7d2c | 884 | struct storvsc_device_info device_info; |
bef4a34a | 885 | |
ca623ad3 | 886 | if (!storvsc_drv_obj->base.dev_add) |
bef4a34a HJ |
887 | return -1; |
888 | ||
ff568d3a | 889 | host = scsi_host_alloc(&scsi_driver, |
972621c9 | 890 | sizeof(struct hv_host_device)); |
f8feed06 | 891 | if (!host) |
bef4a34a | 892 | return -ENOMEM; |
bef4a34a | 893 | |
9efd21e1 | 894 | dev_set_drvdata(&device->device, host); |
bef4a34a | 895 | |
795b613d S |
896 | host_dev = (struct hv_host_device *)host->hostdata; |
897 | memset(host_dev, 0, sizeof(struct hv_host_device)); | |
bef4a34a | 898 | |
795b613d | 899 | host_dev->port = host->host_no; |
97c15296 | 900 | host_dev->dev = device; |
bef4a34a | 901 | |
795b613d | 902 | host_dev->request_pool = |
9efd21e1 | 903 | kmem_cache_create(dev_name(&device->device), |
1e05d88e | 904 | sizeof(struct storvsc_cmd_request), 0, |
ff568d3a GKH |
905 | SLAB_HWCACHE_ALIGN, NULL); |
906 | ||
795b613d | 907 | if (!host_dev->request_pool) { |
bef4a34a | 908 | scsi_host_put(host); |
bef4a34a HJ |
909 | return -ENOMEM; |
910 | } | |
911 | ||
8a046024 | 912 | device_info.port_number = host->host_no; |
454f18a9 | 913 | /* Call to the vsc driver to add the device */ |
9efd21e1 S |
914 | ret = storvsc_drv_obj->base.dev_add(device, (void *)&device_info); |
915 | ||
ff568d3a | 916 | if (ret != 0) { |
795b613d | 917 | kmem_cache_destroy(host_dev->request_pool); |
bef4a34a | 918 | scsi_host_put(host); |
bef4a34a HJ |
919 | return -1; |
920 | } | |
921 | ||
795b613d S |
922 | host_dev->path = device_info.path_id; |
923 | host_dev->target = device_info.target_id; | |
bef4a34a | 924 | |
ff568d3a GKH |
925 | /* max # of devices per target */ |
926 | host->max_lun = STORVSC_MAX_LUNS_PER_TARGET; | |
927 | /* max # of targets per channel */ | |
928 | host->max_id = STORVSC_MAX_TARGETS; | |
929 | /* max # of channels */ | |
930 | host->max_channel = STORVSC_MAX_CHANNELS - 1; | |
bef4a34a | 931 | |
454f18a9 | 932 | /* Register the HBA and start the scsi bus scan */ |
9efd21e1 | 933 | ret = scsi_add_host(host, &device->device); |
ff568d3a | 934 | if (ret != 0) { |
bef4a34a | 935 | |
9efd21e1 | 936 | storvsc_drv_obj->base.dev_rm(device); |
bef4a34a | 937 | |
795b613d | 938 | kmem_cache_destroy(host_dev->request_pool); |
bef4a34a | 939 | scsi_host_put(host); |
bef4a34a HJ |
940 | return -1; |
941 | } | |
942 | ||
943 | scsi_scan_host(host); | |
bef4a34a HJ |
944 | return ret; |
945 | } | |
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); |