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