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