Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1 | /* |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 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> |
K. Y. Srinivasan | 972621c | 2011-05-10 07:54:19 -0700 | [diff] [blame] | 20 | * K. Y. Srinivasan <kys@microsoft.com> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 21 | */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 22 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 23 | #include <linux/slab.h> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/device.h> |
| 26 | #include <linux/blkdev.h> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 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> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 34 | #include <scsi/scsi_dbg.h> |
K. Y. Srinivasan | 3f335ea | 2011-05-12 19:34:15 -0700 | [diff] [blame] | 35 | |
| 36 | #include "hyperv.h" |
K. Y. Srinivasan | cdee150 | 2011-05-12 19:34:34 -0700 | [diff] [blame] | 37 | #include "hyperv_storage.h" |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 38 | |
K. Y. Srinivasan | 2db2cab | 2011-05-10 07:54:39 -0700 | [diff] [blame] | 39 | static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 40 | |
K. Y. Srinivasan | 3d598ce | 2011-05-10 07:54:40 -0700 | [diff] [blame] | 41 | module_param(storvsc_ringbuffer_size, int, S_IRUGO); |
| 42 | MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)"); |
| 43 | |
K. Y. Srinivasan | 972621c | 2011-05-10 07:54:19 -0700 | [diff] [blame] | 44 | struct hv_host_device { |
K. Y. Srinivasan | 97c1529 | 2011-05-10 07:54:21 -0700 | [diff] [blame] | 45 | struct hv_device *dev; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 46 | struct kmem_cache *request_pool; |
| 47 | unsigned int port; |
| 48 | unsigned char path; |
| 49 | unsigned char target; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | struct storvsc_cmd_request { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 53 | struct list_head entry; |
| 54 | struct scsi_cmnd *cmd; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 55 | |
| 56 | unsigned int bounce_sgl_count; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 57 | struct scatterlist *bounce_sgl; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 58 | |
Nicolas Palix | 0b3f683 | 2009-07-29 14:10:19 +0200 | [diff] [blame] | 59 | struct hv_storvsc_request request; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 62 | |
K. Y. Srinivasan | 5b60ace | 2011-05-10 07:54:25 -0700 | [diff] [blame] | 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 | |
K. Y. Srinivasan | a1ebfea | 2011-05-10 07:54:26 -0700 | [diff] [blame] | 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 | |
K. Y. Srinivasan | 419f2d0 | 2011-05-10 07:54:27 -0700 | [diff] [blame] | 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 | |
K. Y. Srinivasan | 419f2d0 | 2011-05-10 07:54:27 -0700 | [diff] [blame] | 85 | blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE); |
| 86 | |
K. Y. Srinivasan | 419f2d0 | 2011-05-10 07:54:27 -0700 | [diff] [blame] | 87 | blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec); |
| 88 | |
| 89 | blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY); |
K. Y. Srinivasan | 419f2d0 | 2011-05-10 07:54:27 -0700 | [diff] [blame] | 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
K. Y. Srinivasan | 49a3c7a | 2011-05-10 07:54:28 -0700 | [diff] [blame] | 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 | |
K. Y. Srinivasan | 3862ef3 | 2011-05-10 07:54:29 -0700 | [diff] [blame] | 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 | |
K. Y. Srinivasan | a9753cb | 2011-05-10 07:54:30 -0700 | [diff] [blame] | 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 | |
K. Y. Srinivasan | 29fe2c9 | 2011-05-10 07:54:31 -0700 | [diff] [blame] | 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 | |
K. Y. Srinivasan | 6a8ff44 | 2011-05-10 07:54:32 -0700 | [diff] [blame] | 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 | |
K. Y. Srinivasan | 5c5c023 | 2011-05-10 07:54:33 -0700 | [diff] [blame] | 296 | |
K. Y. Srinivasan | 5c5c023 | 2011-05-10 07:54:33 -0700 | [diff] [blame] | 297 | static int storvsc_remove(struct hv_device *dev) |
| 298 | { |
K. Y. Srinivasan | 5c5c023 | 2011-05-10 07:54:33 -0700 | [diff] [blame] | 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 | |
K. Y. Srinivasan | 5c5c023 | 2011-05-10 07:54:33 -0700 | [diff] [blame] | 303 | scsi_remove_host(host); |
| 304 | |
K. Y. Srinivasan | 5c5c023 | 2011-05-10 07:54:33 -0700 | [diff] [blame] | 305 | scsi_host_put(host); |
K. Y. Srinivasan | d36b0a0 | 2011-06-06 15:49:29 -0700 | [diff] [blame] | 306 | |
K. Y. Srinivasan | 2935a40 | 2011-06-06 15:49:28 -0700 | [diff] [blame] | 307 | storvsc_dev_remove(dev); |
| 308 | if (host_dev->request_pool) { |
| 309 | kmem_cache_destroy(host_dev->request_pool); |
| 310 | host_dev->request_pool = NULL; |
| 311 | } |
K. Y. Srinivasan | 5c5c023 | 2011-05-10 07:54:33 -0700 | [diff] [blame] | 312 | return 0; |
| 313 | } |
| 314 | |
K. Y. Srinivasan | 62838ce | 2011-05-10 07:54:34 -0700 | [diff] [blame] | 315 | |
| 316 | static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev, |
| 317 | sector_t capacity, int *info) |
| 318 | { |
K. Y. Srinivasan | 5326fd5 | 2011-05-10 07:54:52 -0700 | [diff] [blame] | 319 | sector_t nsect = capacity; |
| 320 | sector_t cylinders = nsect; |
| 321 | int heads, sectors_pt; |
K. Y. Srinivasan | 62838ce | 2011-05-10 07:54:34 -0700 | [diff] [blame] | 322 | |
K. Y. Srinivasan | 5326fd5 | 2011-05-10 07:54:52 -0700 | [diff] [blame] | 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; |
K. Y. Srinivasan | 62838ce | 2011-05-10 07:54:34 -0700 | [diff] [blame] | 331 | |
| 332 | info[0] = heads; |
K. Y. Srinivasan | 5326fd5 | 2011-05-10 07:54:52 -0700 | [diff] [blame] | 333 | info[1] = sectors_pt; |
| 334 | info[2] = (int)cylinders; |
K. Y. Srinivasan | 62838ce | 2011-05-10 07:54:34 -0700 | [diff] [blame] | 335 | |
K. Y. Srinivasan | 62838ce | 2011-05-10 07:54:34 -0700 | [diff] [blame] | 336 | return 0; |
| 337 | } |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 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 | |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 346 | |
| 347 | stor_device = get_stor_device(device); |
| 348 | if (!stor_device) |
| 349 | return -1; |
| 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 | |
K. Y. Srinivasan | 46d2eb6 | 2011-06-16 13:16:36 -0700 | [diff] [blame] | 368 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 369 | if (t == 0) { |
| 370 | ret = -ETIMEDOUT; |
| 371 | goto cleanup; |
| 372 | } |
| 373 | |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 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 | |
K. Y. Srinivasan | 96e690be | 2011-05-10 07:54:38 -0700 | [diff] [blame] | 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 | |
K. Y. Srinivasan | 96e690be | 2011-05-10 07:54:38 -0700 | [diff] [blame] | 396 | ret = storvsc_host_reset(dev); |
| 397 | if (ret != 0) |
| 398 | return ret; |
| 399 | |
K. Y. Srinivasan | 96e690be | 2011-05-10 07:54:38 -0700 | [diff] [blame] | 400 | return ret; |
| 401 | } |
| 402 | |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 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 | |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 418 | if (cmd_request->bounce_sgl_count) { |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 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 | |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 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 | |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 446 | scsi_done_fn(scmnd); |
| 447 | |
| 448 | kmem_cache_free(host_dev->request_pool, cmd_request); |
| 449 | } |
| 450 | |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 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; |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 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) { |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 473 | |
| 474 | cmd_request = |
| 475 | (struct storvsc_cmd_request *)scmnd->host_scribble; |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 476 | |
| 477 | goto retry_request; |
| 478 | } |
| 479 | |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 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 | |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 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 | |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 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)) { |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 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 */ |
K. Y. Srinivasan | 636f0fd | 2011-05-10 07:54:50 -0700 | [diff] [blame] | 579 | ret = storvsc_do_io(dev, &cmd_request->request); |
| 580 | |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 581 | if (ret == -1) { |
| 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 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 609 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 610 | /* Scsi driver */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 611 | static struct scsi_host_template scsi_driver = { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 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 */ |
Lars Lindley | 0686e4f | 2010-03-11 23:51:23 +0100 | [diff] [blame] | 621 | .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS, |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 622 | .this_id = -1, |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 623 | /* no use setting to 0 since ll_blk_rw reset it to 1 */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 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 | */ |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 632 | /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 633 | .use_clustering = ENABLE_CLUSTERING, |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 634 | /* Make sure we dont get a sg segment crosses a page boundary */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 635 | .dma_boundary = PAGE_SIZE-1, |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 636 | }; |
| 637 | |
K. Y. Srinivasan | d847b5f | 2011-08-25 09:48:33 -0700 | [diff] [blame] | 638 | static const struct hv_vmbus_device_id id_table[] = { |
Greg Kroah-Hartman | c45cf2d | 2011-08-25 11:41:33 -0700 | [diff] [blame] | 639 | /* SCSI guid */ |
| 640 | { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d, |
| 641 | 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f) }, |
| 642 | { }, |
K. Y. Srinivasan | d847b5f | 2011-08-25 09:48:33 -0700 | [diff] [blame] | 643 | }; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 644 | |
K. Y. Srinivasan | d847b5f | 2011-08-25 09:48:33 -0700 | [diff] [blame] | 645 | MODULE_DEVICE_TABLE(vmbus, id_table); |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 646 | /* |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 647 | * storvsc_probe - Add a new device for this driver |
| 648 | */ |
| 649 | |
| 650 | static int storvsc_probe(struct hv_device *device) |
| 651 | { |
| 652 | int ret; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 653 | struct Scsi_Host *host; |
| 654 | struct hv_host_device *host_dev; |
| 655 | struct storvsc_device_info device_info; |
| 656 | |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 657 | host = scsi_host_alloc(&scsi_driver, |
| 658 | sizeof(struct hv_host_device)); |
| 659 | if (!host) |
| 660 | return -ENOMEM; |
| 661 | |
| 662 | dev_set_drvdata(&device->device, host); |
| 663 | |
| 664 | host_dev = (struct hv_host_device *)host->hostdata; |
| 665 | memset(host_dev, 0, sizeof(struct hv_host_device)); |
| 666 | |
| 667 | host_dev->port = host->host_no; |
| 668 | host_dev->dev = device; |
| 669 | |
| 670 | host_dev->request_pool = |
| 671 | kmem_cache_create(dev_name(&device->device), |
| 672 | sizeof(struct storvsc_cmd_request), 0, |
| 673 | SLAB_HWCACHE_ALIGN, NULL); |
| 674 | |
| 675 | if (!host_dev->request_pool) { |
| 676 | scsi_host_put(host); |
| 677 | return -ENOMEM; |
| 678 | } |
| 679 | |
| 680 | device_info.port_number = host->host_no; |
K. Y. Srinivasan | fa4d123 | 2011-05-10 07:56:01 -0700 | [diff] [blame] | 681 | device_info.ring_buffer_size = storvsc_ringbuffer_size; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 682 | /* Call to the vsc driver to add the device */ |
K. Y. Srinivasan | 58f1f5c | 2011-05-10 07:54:49 -0700 | [diff] [blame] | 683 | ret = storvsc_dev_add(device, (void *)&device_info); |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 684 | |
| 685 | if (ret != 0) { |
| 686 | kmem_cache_destroy(host_dev->request_pool); |
| 687 | scsi_host_put(host); |
| 688 | return -1; |
| 689 | } |
| 690 | |
| 691 | host_dev->path = device_info.path_id; |
| 692 | host_dev->target = device_info.target_id; |
| 693 | |
| 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; |
| 700 | |
| 701 | /* Register the HBA and start the scsi bus scan */ |
| 702 | ret = scsi_add_host(host, &device->device); |
| 703 | if (ret != 0) { |
| 704 | |
K. Y. Srinivasan | 6cdc57c | 2011-05-10 07:54:48 -0700 | [diff] [blame] | 705 | storvsc_dev_remove(device); |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 706 | |
| 707 | kmem_cache_destroy(host_dev->request_pool); |
| 708 | scsi_host_put(host); |
| 709 | return -1; |
| 710 | } |
| 711 | |
| 712 | scsi_scan_host(host); |
| 713 | return ret; |
| 714 | } |
| 715 | |
K. Y. Srinivasan | 7bd05b9 | 2011-05-10 07:54:45 -0700 | [diff] [blame] | 716 | /* The one and only one */ |
| 717 | |
K. Y. Srinivasan | 40bf63e | 2011-05-10 07:56:09 -0700 | [diff] [blame] | 718 | static struct hv_driver storvsc_drv = { |
Greg Kroah-Hartman | 768fa21 | 2011-08-25 15:07:32 -0700 | [diff] [blame^] | 719 | .name = "storvsc", |
K. Y. Srinivasan | d847b5f | 2011-08-25 09:48:33 -0700 | [diff] [blame] | 720 | .id_table = id_table, |
K. Y. Srinivasan | 40bf63e | 2011-05-10 07:56:09 -0700 | [diff] [blame] | 721 | .probe = storvsc_probe, |
| 722 | .remove = storvsc_remove, |
K. Y. Srinivasan | 39ae6fa | 2011-05-10 07:54:46 -0700 | [diff] [blame] | 723 | }; |
K. Y. Srinivasan | 7bd05b9 | 2011-05-10 07:54:45 -0700 | [diff] [blame] | 724 | |
K. Y. Srinivasan | d9bbae8 | 2011-06-06 15:49:27 -0700 | [diff] [blame] | 725 | static int __init storvsc_drv_init(void) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 726 | { |
K. Y. Srinivasan | 01415ab3 | 2011-05-10 07:55:58 -0700 | [diff] [blame] | 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 | */ |
K. Y. Srinivasan | 01415ab3 | 2011-05-10 07:55:58 -0700 | [diff] [blame] | 735 | max_outstanding_req_per_channel = |
Greg Kroah-Hartman | 768fa21 | 2011-08-25 15:07:32 -0700 | [diff] [blame^] | 736 | ((storvsc_ringbuffer_size - PAGE_SIZE) / |
| 737 | ALIGN(MAX_MULTIPAGE_BUFFER_PACKET + |
| 738 | sizeof(struct vstor_packet) + sizeof(u64), |
| 739 | sizeof(u64))); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 740 | |
K. Y. Srinivasan | 01415ab3 | 2011-05-10 07:55:58 -0700 | [diff] [blame] | 741 | if (max_outstanding_req_per_channel < |
K. Y. Srinivasan | f8feed0 | 2011-05-10 07:54:24 -0700 | [diff] [blame] | 742 | STORVSC_MAX_IO_REQUESTS) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 743 | return -1; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 744 | |
Greg Kroah-Hartman | 768fa21 | 2011-08-25 15:07:32 -0700 | [diff] [blame^] | 745 | return vmbus_driver_register(&storvsc_drv); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 746 | } |
| 747 | |
K. Y. Srinivasan | c63ba9e | 2011-06-06 15:49:26 -0700 | [diff] [blame] | 748 | static void __exit storvsc_drv_exit(void) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 749 | { |
Greg Kroah-Hartman | 768fa21 | 2011-08-25 15:07:32 -0700 | [diff] [blame^] | 750 | vmbus_driver_unregister(&storvsc_drv); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 751 | } |
| 752 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 753 | MODULE_LICENSE("GPL"); |
Hank Janssen | 26c14cc | 2010-02-11 23:02:42 +0000 | [diff] [blame] | 754 | MODULE_VERSION(HV_DRV_VERSION); |
Stephen Hemminger | 3afc7cc3 | 2010-05-06 21:44:45 -0700 | [diff] [blame] | 755 | MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver"); |
K. Y. Srinivasan | d9bbae8 | 2011-06-06 15:49:27 -0700 | [diff] [blame] | 756 | module_init(storvsc_drv_init); |
K. Y. Srinivasan | c63ba9e | 2011-06-06 15:49:26 -0700 | [diff] [blame] | 757 | module_exit(storvsc_drv_exit); |