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 S |
346 | |
347 | stor_device = get_stor_device(device); | |
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: | |
381 | put_stor_device(device); | |
382 | return ret; | |
383 | } | |
384 | ||
385 | ||
96e690be S |
386 | /* |
387 | * storvsc_host_reset_handler - Reset the scsi HBA | |
388 | */ | |
389 | static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd) | |
390 | { | |
391 | int ret; | |
392 | struct hv_host_device *host_dev = | |
393 | (struct hv_host_device *)scmnd->device->host->hostdata; | |
394 | struct hv_device *dev = host_dev->dev; | |
395 | ||
96e690be S |
396 | ret = storvsc_host_reset(dev); |
397 | if (ret != 0) | |
398 | return ret; | |
399 | ||
96e690be S |
400 | return ret; |
401 | } | |
402 | ||
8a411bad S |
403 | |
404 | /* | |
405 | * storvsc_commmand_completion - Command completion processing | |
406 | */ | |
407 | static void storvsc_commmand_completion(struct hv_storvsc_request *request) | |
408 | { | |
409 | struct storvsc_cmd_request *cmd_request = | |
410 | (struct storvsc_cmd_request *)request->context; | |
411 | struct scsi_cmnd *scmnd = cmd_request->cmd; | |
412 | struct hv_host_device *host_dev = | |
413 | (struct hv_host_device *)scmnd->device->host->hostdata; | |
414 | void (*scsi_done_fn)(struct scsi_cmnd *); | |
415 | struct scsi_sense_hdr sense_hdr; | |
416 | struct vmscsi_request *vm_srb; | |
417 | ||
8a411bad | 418 | if (cmd_request->bounce_sgl_count) { |
8a411bad S |
419 | |
420 | /* FIXME: We can optimize on writes by just skipping this */ | |
421 | copy_from_bounce_buffer(scsi_sglist(scmnd), | |
422 | cmd_request->bounce_sgl, | |
423 | scsi_sg_count(scmnd)); | |
424 | destroy_bounce_buffer(cmd_request->bounce_sgl, | |
425 | cmd_request->bounce_sgl_count); | |
426 | } | |
427 | ||
428 | vm_srb = &request->vstor_packet.vm_srb; | |
429 | scmnd->result = vm_srb->scsi_status; | |
430 | ||
431 | if (scmnd->result) { | |
432 | if (scsi_normalize_sense(scmnd->sense_buffer, | |
433 | SCSI_SENSE_BUFFERSIZE, &sense_hdr)) | |
434 | scsi_print_sense_hdr("storvsc", &sense_hdr); | |
435 | } | |
436 | ||
8a411bad S |
437 | scsi_set_resid(scmnd, |
438 | request->data_buffer.len - | |
439 | vm_srb->data_transfer_length); | |
440 | ||
441 | scsi_done_fn = scmnd->scsi_done; | |
442 | ||
443 | scmnd->host_scribble = NULL; | |
444 | scmnd->scsi_done = NULL; | |
445 | ||
8a411bad S |
446 | scsi_done_fn(scmnd); |
447 | ||
448 | kmem_cache_free(host_dev->request_pool, cmd_request); | |
449 | } | |
450 | ||
c5b463ae S |
451 | |
452 | /* | |
453 | * storvsc_queuecommand - Initiate command processing | |
454 | */ | |
455 | static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd, | |
456 | void (*done)(struct scsi_cmnd *)) | |
457 | { | |
458 | int ret; | |
459 | struct hv_host_device *host_dev = | |
460 | (struct hv_host_device *)scmnd->device->host->hostdata; | |
461 | struct hv_device *dev = host_dev->dev; | |
c5b463ae S |
462 | struct hv_storvsc_request *request; |
463 | struct storvsc_cmd_request *cmd_request; | |
464 | unsigned int request_size = 0; | |
465 | int i; | |
466 | struct scatterlist *sgl; | |
467 | unsigned int sg_count = 0; | |
468 | struct vmscsi_request *vm_srb; | |
469 | ||
470 | ||
471 | /* If retrying, no need to prep the cmd */ | |
472 | if (scmnd->host_scribble) { | |
c5b463ae S |
473 | |
474 | cmd_request = | |
475 | (struct storvsc_cmd_request *)scmnd->host_scribble; | |
c5b463ae S |
476 | |
477 | goto retry_request; | |
478 | } | |
479 | ||
c5b463ae S |
480 | scmnd->scsi_done = done; |
481 | ||
482 | request_size = sizeof(struct storvsc_cmd_request); | |
483 | ||
484 | cmd_request = kmem_cache_zalloc(host_dev->request_pool, | |
485 | GFP_ATOMIC); | |
486 | if (!cmd_request) { | |
487 | scmnd->scsi_done = NULL; | |
488 | return SCSI_MLQUEUE_DEVICE_BUSY; | |
489 | } | |
490 | ||
491 | /* Setup the cmd request */ | |
492 | cmd_request->bounce_sgl_count = 0; | |
493 | cmd_request->bounce_sgl = NULL; | |
494 | cmd_request->cmd = scmnd; | |
495 | ||
496 | scmnd->host_scribble = (unsigned char *)cmd_request; | |
497 | ||
498 | request = &cmd_request->request; | |
499 | vm_srb = &request->vstor_packet.vm_srb; | |
500 | ||
501 | ||
502 | /* Build the SRB */ | |
503 | switch (scmnd->sc_data_direction) { | |
504 | case DMA_TO_DEVICE: | |
505 | vm_srb->data_in = WRITE_TYPE; | |
506 | break; | |
507 | case DMA_FROM_DEVICE: | |
508 | vm_srb->data_in = READ_TYPE; | |
509 | break; | |
510 | default: | |
511 | vm_srb->data_in = UNKNOWN_TYPE; | |
512 | break; | |
513 | } | |
514 | ||
515 | request->on_io_completion = storvsc_commmand_completion; | |
516 | request->context = cmd_request;/* scmnd; */ | |
517 | ||
c5b463ae S |
518 | vm_srb->port_number = host_dev->port; |
519 | vm_srb->path_id = scmnd->device->channel; | |
520 | vm_srb->target_id = scmnd->device->id; | |
521 | vm_srb->lun = scmnd->device->lun; | |
522 | ||
c5b463ae S |
523 | vm_srb->cdb_length = scmnd->cmd_len; |
524 | ||
525 | memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length); | |
526 | ||
527 | request->sense_buffer = scmnd->sense_buffer; | |
528 | ||
529 | ||
530 | request->data_buffer.len = scsi_bufflen(scmnd); | |
531 | if (scsi_sg_count(scmnd)) { | |
532 | sgl = (struct scatterlist *)scsi_sglist(scmnd); | |
533 | sg_count = scsi_sg_count(scmnd); | |
534 | ||
535 | /* check if we need to bounce the sgl */ | |
536 | if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) { | |
537 | cmd_request->bounce_sgl = | |
538 | create_bounce_buffer(sgl, scsi_sg_count(scmnd), | |
539 | scsi_bufflen(scmnd)); | |
540 | if (!cmd_request->bounce_sgl) { | |
541 | scmnd->scsi_done = NULL; | |
542 | scmnd->host_scribble = NULL; | |
543 | kmem_cache_free(host_dev->request_pool, | |
544 | cmd_request); | |
545 | ||
546 | return SCSI_MLQUEUE_HOST_BUSY; | |
547 | } | |
548 | ||
549 | cmd_request->bounce_sgl_count = | |
550 | ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >> | |
551 | PAGE_SHIFT; | |
552 | ||
553 | /* | |
554 | * FIXME: We can optimize on reads by just skipping | |
555 | * this | |
556 | */ | |
557 | copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl, | |
558 | scsi_sg_count(scmnd)); | |
559 | ||
560 | sgl = cmd_request->bounce_sgl; | |
561 | sg_count = cmd_request->bounce_sgl_count; | |
562 | } | |
563 | ||
564 | request->data_buffer.offset = sgl[0].offset; | |
565 | ||
566 | for (i = 0; i < sg_count; i++) | |
567 | request->data_buffer.pfn_array[i] = | |
568 | page_to_pfn(sg_page((&sgl[i]))); | |
569 | ||
570 | } else if (scsi_sglist(scmnd)) { | |
c5b463ae S |
571 | request->data_buffer.offset = |
572 | virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1); | |
573 | request->data_buffer.pfn_array[0] = | |
574 | virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT; | |
575 | } | |
576 | ||
577 | retry_request: | |
578 | /* Invokes the vsc to start an IO */ | |
636f0fd1 S |
579 | ret = storvsc_do_io(dev, &cmd_request->request); |
580 | ||
d2598f01 | 581 | if (ret == -EAGAIN) { |
c5b463ae S |
582 | /* no more space */ |
583 | ||
584 | if (cmd_request->bounce_sgl_count) { | |
585 | /* | |
586 | * FIXME: We can optimize on writes by just skipping | |
587 | * this | |
588 | */ | |
589 | copy_from_bounce_buffer(scsi_sglist(scmnd), | |
590 | cmd_request->bounce_sgl, | |
591 | scsi_sg_count(scmnd)); | |
592 | destroy_bounce_buffer(cmd_request->bounce_sgl, | |
593 | cmd_request->bounce_sgl_count); | |
594 | } | |
595 | ||
596 | kmem_cache_free(host_dev->request_pool, cmd_request); | |
597 | ||
598 | scmnd->scsi_done = NULL; | |
599 | scmnd->host_scribble = NULL; | |
600 | ||
601 | ret = SCSI_MLQUEUE_DEVICE_BUSY; | |
602 | } | |
603 | ||
604 | return ret; | |
605 | } | |
606 | ||
607 | static DEF_SCSI_QCMD(storvsc_queuecommand) | |
608 | ||
bef4a34a | 609 | |
454f18a9 | 610 | /* Scsi driver */ |
bef4a34a | 611 | static struct scsi_host_template scsi_driver = { |
ff568d3a GKH |
612 | .module = THIS_MODULE, |
613 | .name = "storvsc_host_t", | |
614 | .bios_param = storvsc_get_chs, | |
615 | .queuecommand = storvsc_queuecommand, | |
616 | .eh_host_reset_handler = storvsc_host_reset_handler, | |
617 | .slave_alloc = storvsc_device_alloc, | |
618 | .slave_configure = storvsc_device_configure, | |
619 | .cmd_per_lun = 1, | |
620 | /* 64 max_queue * 1 target */ | |
0686e4f4 | 621 | .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS, |
ff568d3a | 622 | .this_id = -1, |
454f18a9 | 623 | /* no use setting to 0 since ll_blk_rw reset it to 1 */ |
ff568d3a GKH |
624 | /* currently 32 */ |
625 | .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT, | |
626 | /* | |
627 | * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge | |
628 | * into 1 sg element. If set, we must limit the max_segment_size to | |
629 | * PAGE_SIZE, otherwise we may get 1 sg element that represents | |
630 | * multiple | |
631 | */ | |
454f18a9 | 632 | /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */ |
ff568d3a | 633 | .use_clustering = ENABLE_CLUSTERING, |
454f18a9 | 634 | /* Make sure we dont get a sg segment crosses a page boundary */ |
ff568d3a | 635 | .dma_boundary = PAGE_SIZE-1, |
bef4a34a HJ |
636 | }; |
637 | ||
d847b5fe | 638 | static const struct hv_vmbus_device_id id_table[] = { |
c45cf2d4 GKH |
639 | /* SCSI guid */ |
640 | { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d, | |
641 | 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f) }, | |
642 | { }, | |
d847b5fe | 643 | }; |
bef4a34a | 644 | |
d847b5fe | 645 | MODULE_DEVICE_TABLE(vmbus, id_table); |
3e189519 | 646 | /* |
ff568d3a GKH |
647 | * storvsc_probe - Add a new device for this driver |
648 | */ | |
f5c78872 | 649 | |
9efd21e1 | 650 | static int storvsc_probe(struct hv_device *device) |
bef4a34a | 651 | { |
ff568d3a | 652 | int ret; |
bef4a34a | 653 | struct Scsi_Host *host; |
795b613d | 654 | struct hv_host_device *host_dev; |
9f0c7d2c | 655 | struct storvsc_device_info device_info; |
bef4a34a | 656 | |
ff568d3a | 657 | host = scsi_host_alloc(&scsi_driver, |
972621c9 | 658 | sizeof(struct hv_host_device)); |
f8feed06 | 659 | if (!host) |
bef4a34a | 660 | return -ENOMEM; |
bef4a34a | 661 | |
9efd21e1 | 662 | dev_set_drvdata(&device->device, host); |
bef4a34a | 663 | |
795b613d S |
664 | host_dev = (struct hv_host_device *)host->hostdata; |
665 | memset(host_dev, 0, sizeof(struct hv_host_device)); | |
bef4a34a | 666 | |
795b613d | 667 | host_dev->port = host->host_no; |
97c15296 | 668 | host_dev->dev = device; |
bef4a34a | 669 | |
795b613d | 670 | host_dev->request_pool = |
9efd21e1 | 671 | kmem_cache_create(dev_name(&device->device), |
1e05d88e | 672 | sizeof(struct storvsc_cmd_request), 0, |
ff568d3a GKH |
673 | SLAB_HWCACHE_ALIGN, NULL); |
674 | ||
795b613d | 675 | if (!host_dev->request_pool) { |
bef4a34a | 676 | scsi_host_put(host); |
bef4a34a HJ |
677 | return -ENOMEM; |
678 | } | |
679 | ||
8a046024 | 680 | device_info.port_number = host->host_no; |
fa4d123a | 681 | device_info.ring_buffer_size = storvsc_ringbuffer_size; |
454f18a9 | 682 | /* Call to the vsc driver to add the device */ |
58f1f5cb | 683 | ret = storvsc_dev_add(device, (void *)&device_info); |
9efd21e1 | 684 | |
ff568d3a | 685 | if (ret != 0) { |
795b613d | 686 | kmem_cache_destroy(host_dev->request_pool); |
bef4a34a | 687 | scsi_host_put(host); |
8e854680 | 688 | return -ENODEV; |
bef4a34a HJ |
689 | } |
690 | ||
795b613d S |
691 | host_dev->path = device_info.path_id; |
692 | host_dev->target = device_info.target_id; | |
bef4a34a | 693 | |
ff568d3a GKH |
694 | /* max # of devices per target */ |
695 | host->max_lun = STORVSC_MAX_LUNS_PER_TARGET; | |
696 | /* max # of targets per channel */ | |
697 | host->max_id = STORVSC_MAX_TARGETS; | |
698 | /* max # of channels */ | |
699 | host->max_channel = STORVSC_MAX_CHANNELS - 1; | |
bef4a34a | 700 | |
454f18a9 | 701 | /* Register the HBA and start the scsi bus scan */ |
9efd21e1 | 702 | ret = scsi_add_host(host, &device->device); |
ff568d3a | 703 | if (ret != 0) { |
bef4a34a | 704 | |
6cdc57c0 | 705 | storvsc_dev_remove(device); |
bef4a34a | 706 | |
795b613d | 707 | kmem_cache_destroy(host_dev->request_pool); |
bef4a34a | 708 | scsi_host_put(host); |
8e854680 | 709 | return -ENODEV; |
bef4a34a HJ |
710 | } |
711 | ||
712 | scsi_scan_host(host); | |
bef4a34a HJ |
713 | return ret; |
714 | } | |
715 | ||
7bd05b91 S |
716 | /* The one and only one */ |
717 | ||
40bf63ed | 718 | static struct hv_driver storvsc_drv = { |
768fa219 | 719 | .name = "storvsc", |
d847b5fe | 720 | .id_table = id_table, |
40bf63ed S |
721 | .probe = storvsc_probe, |
722 | .remove = storvsc_remove, | |
39ae6fae | 723 | }; |
7bd05b91 | 724 | |
d9bbae83 | 725 | static int __init storvsc_drv_init(void) |
f5c78872 | 726 | { |
01415ab3 S |
727 | u32 max_outstanding_req_per_channel; |
728 | ||
729 | /* | |
730 | * Divide the ring buffer data size (which is 1 page less | |
731 | * than the ring buffer size since that page is reserved for | |
732 | * the ring buffer indices) by the max request size (which is | |
733 | * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64) | |
734 | */ | |
01415ab3 | 735 | max_outstanding_req_per_channel = |
768fa219 GKH |
736 | ((storvsc_ringbuffer_size - PAGE_SIZE) / |
737 | ALIGN(MAX_MULTIPAGE_BUFFER_PACKET + | |
738 | sizeof(struct vstor_packet) + sizeof(u64), | |
739 | sizeof(u64))); | |
f5c78872 | 740 | |
01415ab3 | 741 | if (max_outstanding_req_per_channel < |
f5c78872 S |
742 | STORVSC_MAX_IO_REQUESTS) |
743 | return -1; | |
744 | ||
768fa219 | 745 | return vmbus_driver_register(&storvsc_drv); |
f5c78872 S |
746 | } |
747 | ||
c63ba9e1 | 748 | static void __exit storvsc_drv_exit(void) |
f5c78872 | 749 | { |
768fa219 | 750 | vmbus_driver_unregister(&storvsc_drv); |
f5c78872 S |
751 | } |
752 | ||
ff568d3a | 753 | MODULE_LICENSE("GPL"); |
26c14cc1 | 754 | MODULE_VERSION(HV_DRV_VERSION); |
3afc7cc3 | 755 | MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver"); |
d9bbae83 | 756 | module_init(storvsc_drv_init); |
c63ba9e1 | 757 | module_exit(storvsc_drv_exit); |