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