]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/staging/hv/storvsc_drv.c
Staging: hv: storvsc_drv: Rename g_driver_name
[mirror_ubuntu-artful-kernel.git] / drivers / staging / hv / storvsc_drv.c
CommitLineData
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>
bef4a34a 20 */
bef4a34a 21#include <linux/init.h>
5a0e3ad6 22#include <linux/slab.h>
bef4a34a
HJ
23#include <linux/module.h>
24#include <linux/device.h>
25#include <linux/blkdev.h>
bef4a34a
HJ
26#include <scsi/scsi.h>
27#include <scsi/scsi_cmnd.h>
28#include <scsi/scsi_host.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_tcq.h>
31#include <scsi/scsi_eh.h>
32#include <scsi/scsi_devinfo.h>
bef4a34a 33#include <scsi/scsi_dbg.h>
e3fe0bb6 34#include "hv_api.h"
645954c5 35#include "logging.h"
2d82f6c7 36#include "version_info.h"
870cde80 37#include "vmbus.h"
bb969793 38#include "storvsc_api.h"
af3043c6
S
39#include "vstorage.h"
40#include "channel.h"
bef4a34a 41
bef4a34a 42
0f0cdc6a 43static const char *driver_name = "storvsc";
6dec2442
S
44
45/* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
46static const struct hv_guid gStorVscDeviceType = {
47 .data = {
48 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
49 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
50 }
51};
52
bef4a34a 53struct host_device_context {
ff568d3a
GKH
54 /* must be 1st field
55 * FIXME this is a bug */
ff568d3a 56 /* point back to our device context */
6bad88da 57 struct hv_device *device_ctx;
ff568d3a
GKH
58 struct kmem_cache *request_pool;
59 unsigned int port;
60 unsigned char path;
61 unsigned char target;
bef4a34a
HJ
62};
63
64struct storvsc_cmd_request {
ff568d3a
GKH
65 struct list_head entry;
66 struct scsi_cmnd *cmd;
bef4a34a
HJ
67
68 unsigned int bounce_sgl_count;
ff568d3a 69 struct scatterlist *bounce_sgl;
bef4a34a 70
0b3f6834 71 struct hv_storvsc_request request;
bef4a34a
HJ
72};
73
bef4a34a 74
6dec2442
S
75/*
76 * stor_vsc_initialize - Main entry point
77 */
2b8b3582 78static int stor_vsc_initialize(struct hv_driver *driver)
6dec2442 79{
2e79505d 80 struct storvsc_driver *stor_driver;
6dec2442 81
ced01b0d 82 stor_driver = hvdr_to_stordr(driver);
6dec2442 83
d7a1bdb9
S
84 DPRINT_DBG(STORVSC,
85 "sizeof(struct hv_storvsc_request)=%zd "
6dec2442
S
86 "sizeof(struct vstor_packet)=%zd, "
87 "sizeof(struct vmscsi_request)=%zd",
88 sizeof(struct hv_storvsc_request),
6dec2442
S
89 sizeof(struct vstor_packet),
90 sizeof(struct vmscsi_request));
91
92 /* Make sure we are at least 2 pages since 1 page is used for control */
93
0f0cdc6a 94 driver->name = driver_name;
6dec2442
S
95 memcpy(&driver->dev_type, &gStorVscDeviceType,
96 sizeof(struct hv_guid));
97
6dec2442
S
98
99 /*
100 * Divide the ring buffer data size (which is 1 page less
101 * than the ring buffer size since that page is reserved for
102 * the ring buffer indices) by the max request size (which is
103 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
104 */
105 stor_driver->max_outstanding_req_per_channel =
106 ((stor_driver->ring_buffer_size - PAGE_SIZE) /
107 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
108 sizeof(struct vstor_packet) + sizeof(u64),
109 sizeof(u64)));
110
111 DPRINT_INFO(STORVSC, "max io %u, currently %u\n",
112 stor_driver->max_outstanding_req_per_channel,
113 STORVSC_MAX_IO_REQUESTS);
114
115 /* Setup the dispatch table */
2ac5dad1 116 stor_driver->base.dev_add = storvsc_dev_add;
cb706b04 117 stor_driver->base.dev_rm = storvsc_dev_remove;
6dec2442 118
bb465926 119 stor_driver->on_io_request = storvsc_do_io;
6dec2442
S
120
121 return 0;
122}
123
454f18a9 124/* Static decl */
9efd21e1 125static int storvsc_probe(struct hv_device *dev);
f281233d 126static int storvsc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd);
bef4a34a
HJ
127static int storvsc_device_alloc(struct scsi_device *);
128static int storvsc_device_configure(struct scsi_device *);
129static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd);
415b023a 130static int storvsc_remove(struct hv_device *dev);
bef4a34a 131
ff568d3a
GKH
132static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
133 unsigned int sg_count,
134 unsigned int len);
135static void destroy_bounce_buffer(struct scatterlist *sgl,
136 unsigned int sg_count);
bef4a34a 137static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count);
ff568d3a
GKH
138static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
139 struct scatterlist *bounce_sgl,
140 unsigned int orig_sgl_count);
141static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
142 struct scatterlist *bounce_sgl,
143 unsigned int orig_sgl_count);
bef4a34a 144
ff568d3a
GKH
145static int storvsc_get_chs(struct scsi_device *sdev, struct block_device *bdev,
146 sector_t capacity, int *info);
bef4a34a
HJ
147
148
149static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
3afc7cc3
SH
150module_param(storvsc_ringbuffer_size, int, S_IRUGO);
151MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
bef4a34a 152
454f18a9 153/* The one and only one */
2e79505d 154static struct storvsc_driver g_storvsc_drv;
bef4a34a 155
454f18a9 156/* Scsi driver */
bef4a34a 157static struct scsi_host_template scsi_driver = {
ff568d3a
GKH
158 .module = THIS_MODULE,
159 .name = "storvsc_host_t",
160 .bios_param = storvsc_get_chs,
161 .queuecommand = storvsc_queuecommand,
162 .eh_host_reset_handler = storvsc_host_reset_handler,
163 .slave_alloc = storvsc_device_alloc,
164 .slave_configure = storvsc_device_configure,
165 .cmd_per_lun = 1,
166 /* 64 max_queue * 1 target */
0686e4f4 167 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
ff568d3a 168 .this_id = -1,
454f18a9 169 /* no use setting to 0 since ll_blk_rw reset it to 1 */
ff568d3a
GKH
170 /* currently 32 */
171 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
172 /*
173 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
174 * into 1 sg element. If set, we must limit the max_segment_size to
175 * PAGE_SIZE, otherwise we may get 1 sg element that represents
176 * multiple
177 */
454f18a9 178 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
ff568d3a 179 .use_clustering = ENABLE_CLUSTERING,
454f18a9 180 /* Make sure we dont get a sg segment crosses a page boundary */
ff568d3a 181 .dma_boundary = PAGE_SIZE-1,
bef4a34a
HJ
182};
183
184
3e189519 185/*
ff568d3a
GKH
186 * storvsc_drv_init - StorVsc driver initialization.
187 */
db085777 188static int storvsc_drv_init(void)
bef4a34a 189{
ff568d3a 190 int ret;
2e79505d 191 struct storvsc_driver *storvsc_drv_obj = &g_storvsc_drv;
4af27d70 192 struct hv_driver *drv = &g_storvsc_drv.base;
bef4a34a 193
8a046024 194 storvsc_drv_obj->ring_buffer_size = storvsc_ringbuffer_size;
bef4a34a 195
454f18a9 196 /* Callback to client driver to complete the initialization */
db085777 197 stor_vsc_initialize(&storvsc_drv_obj->base);
bef4a34a 198
ff568d3a 199 DPRINT_INFO(STORVSC_DRV,
1e05d88e 200 "max outstanding reqs %u",
8a046024 201 storvsc_drv_obj->max_outstanding_req_per_channel);
ff568d3a 202
8a046024 203 if (storvsc_drv_obj->max_outstanding_req_per_channel <
ff568d3a
GKH
204 STORVSC_MAX_IO_REQUESTS) {
205 DPRINT_ERR(STORVSC_DRV,
206 "The number of outstanding io requests (%d) "
207 "is larger than that supported (%d) internally.",
208 STORVSC_MAX_IO_REQUESTS,
8a046024 209 storvsc_drv_obj->max_outstanding_req_per_channel);
bef4a34a
HJ
210 return -1;
211 }
212
150f9398 213 drv->driver.name = storvsc_drv_obj->base.name;
bef4a34a 214
9efd21e1 215 drv->probe = storvsc_probe;
415b023a 216 drv->remove = storvsc_remove;
bef4a34a 217
454f18a9 218 /* The driver belongs to vmbus */
150f9398 219 ret = vmbus_child_driver_register(&drv->driver);
bef4a34a 220
bef4a34a
HJ
221 return ret;
222}
223
af3043c6 224
12cb12ef 225static int stor_vsc_on_host_reset(struct hv_device *device)
af3043c6
S
226{
227 struct storvsc_device *stor_device;
d7a1bdb9 228 struct hv_storvsc_request *request;
af3043c6 229 struct vstor_packet *vstor_packet;
93958465 230 int ret, t;
af3043c6
S
231
232 DPRINT_INFO(STORVSC, "resetting host adapter...");
233
234 stor_device = get_stor_device(device);
235 if (!stor_device) {
236 DPRINT_ERR(STORVSC, "unable to get stor device..."
237 "device being destroyed?");
238 return -1;
239 }
240
241 request = &stor_device->reset_request;
242 vstor_packet = &request->vstor_packet;
243
93958465 244 init_completion(&request->wait_event);
af3043c6
S
245
246 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
247 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
248 vstor_packet->vm_srb.path_id = stor_device->path_id;
249
af3043c6
S
250 ret = vmbus_sendpacket(device->channel, vstor_packet,
251 sizeof(struct vstor_packet),
252 (unsigned long)&stor_device->reset_request,
253 VM_PKT_DATA_INBAND,
254 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
255 if (ret != 0) {
256 DPRINT_ERR(STORVSC, "Unable to send reset packet %p ret %d",
257 vstor_packet, ret);
258 goto cleanup;
259 }
260
93958465
S
261 t = wait_for_completion_timeout(&request->wait_event, HZ);
262 if (t == 0) {
af3043c6
S
263 ret = -ETIMEDOUT;
264 goto cleanup;
265 }
266
267 DPRINT_INFO(STORVSC, "host adapter reset completed");
268
269 /*
270 * At this point, all outstanding requests in the adapter
271 * should have been flushed out and return to us
272 */
273
274cleanup:
275 put_stor_device(device);
276 return ret;
277}
278
bef4a34a
HJ
279static int storvsc_drv_exit_cb(struct device *dev, void *data)
280{
281 struct device **curr = (struct device **)data;
282 *curr = dev;
454f18a9 283 return 1; /* stop iterating */
bef4a34a
HJ
284}
285
bd1de709 286static void storvsc_drv_exit(void)
bef4a34a 287{
2e79505d 288 struct storvsc_driver *storvsc_drv_obj = &g_storvsc_drv;
4af27d70 289 struct hv_driver *drv = &g_storvsc_drv.base;
ff568d3a 290 struct device *current_dev = NULL;
2295ba2e 291 int ret;
bef4a34a 292
ff568d3a 293 while (1) {
bef4a34a
HJ
294 current_dev = NULL;
295
454f18a9 296 /* Get the device */
150f9398 297 ret = driver_for_each_device(&drv->driver, NULL,
2295ba2e
BP
298 (void *) &current_dev,
299 storvsc_drv_exit_cb);
300
301 if (ret)
302 DPRINT_WARN(STORVSC_DRV,
303 "driver_for_each_device returned %d", ret);
bef4a34a
HJ
304
305 if (current_dev == NULL)
306 break;
307
454f18a9 308 /* Initiate removal from the top-down */
bef4a34a
HJ
309 device_unregister(current_dev);
310 }
311
ca623ad3
HZ
312 if (storvsc_drv_obj->base.cleanup)
313 storvsc_drv_obj->base.cleanup(&storvsc_drv_obj->base);
bef4a34a 314
150f9398 315 vmbus_child_driver_unregister(&drv->driver);
bef4a34a
HJ
316 return;
317}
318
3e189519 319/*
ff568d3a
GKH
320 * storvsc_probe - Add a new device for this driver
321 */
9efd21e1 322static int storvsc_probe(struct hv_device *device)
bef4a34a 323{
ff568d3a 324 int ret;
2e79505d 325 struct storvsc_driver *storvsc_drv_obj =
9efd21e1 326 drv_to_stordrv(device->device.driver);
bef4a34a
HJ
327 struct Scsi_Host *host;
328 struct host_device_context *host_device_ctx;
9f0c7d2c 329 struct storvsc_device_info device_info;
bef4a34a 330
ca623ad3 331 if (!storvsc_drv_obj->base.dev_add)
bef4a34a
HJ
332 return -1;
333
ff568d3a
GKH
334 host = scsi_host_alloc(&scsi_driver,
335 sizeof(struct host_device_context));
336 if (!host) {
bef4a34a
HJ
337 DPRINT_ERR(STORVSC_DRV, "unable to allocate scsi host object");
338 return -ENOMEM;
339 }
340
9efd21e1 341 dev_set_drvdata(&device->device, host);
bef4a34a 342
ff568d3a 343 host_device_ctx = (struct host_device_context *)host->hostdata;
bef4a34a
HJ
344 memset(host_device_ctx, 0, sizeof(struct host_device_context));
345
346 host_device_ctx->port = host->host_no;
9efd21e1 347 host_device_ctx->device_ctx = device;
bef4a34a 348
bef4a34a 349 host_device_ctx->request_pool =
9efd21e1 350 kmem_cache_create(dev_name(&device->device),
1e05d88e 351 sizeof(struct storvsc_cmd_request), 0,
ff568d3a
GKH
352 SLAB_HWCACHE_ALIGN, NULL);
353
354 if (!host_device_ctx->request_pool) {
bef4a34a 355 scsi_host_put(host);
bef4a34a
HJ
356 return -ENOMEM;
357 }
358
8a046024 359 device_info.port_number = host->host_no;
454f18a9 360 /* Call to the vsc driver to add the device */
9efd21e1
S
361 ret = storvsc_drv_obj->base.dev_add(device, (void *)&device_info);
362
ff568d3a 363 if (ret != 0) {
bef4a34a
HJ
364 DPRINT_ERR(STORVSC_DRV, "unable to add scsi vsc device");
365 kmem_cache_destroy(host_device_ctx->request_pool);
366 scsi_host_put(host);
bef4a34a
HJ
367 return -1;
368 }
369
454f18a9 370 /* host_device_ctx->port = device_info.PortNumber; */
8a046024
HJ
371 host_device_ctx->path = device_info.path_id;
372 host_device_ctx->target = device_info.target_id;
bef4a34a 373
ff568d3a
GKH
374 /* max # of devices per target */
375 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
376 /* max # of targets per channel */
377 host->max_id = STORVSC_MAX_TARGETS;
378 /* max # of channels */
379 host->max_channel = STORVSC_MAX_CHANNELS - 1;
bef4a34a 380
454f18a9 381 /* Register the HBA and start the scsi bus scan */
9efd21e1 382 ret = scsi_add_host(host, &device->device);
ff568d3a 383 if (ret != 0) {
bef4a34a
HJ
384 DPRINT_ERR(STORVSC_DRV, "unable to add scsi host device");
385
9efd21e1 386 storvsc_drv_obj->base.dev_rm(device);
bef4a34a
HJ
387
388 kmem_cache_destroy(host_device_ctx->request_pool);
389 scsi_host_put(host);
bef4a34a
HJ
390 return -1;
391 }
392
393 scsi_scan_host(host);
bef4a34a
HJ
394 return ret;
395}
396
3e189519 397/*
ff568d3a
GKH
398 * storvsc_remove - Callback when our device is removed
399 */
415b023a 400static int storvsc_remove(struct hv_device *dev)
bef4a34a 401{
2e79505d 402 struct storvsc_driver *storvsc_drv_obj =
415b023a
S
403 drv_to_stordrv(dev->device.driver);
404 struct Scsi_Host *host = dev_get_drvdata(&dev->device);
ff568d3a
GKH
405 struct host_device_context *host_device_ctx =
406 (struct host_device_context *)host->hostdata;
bef4a34a 407
ff568d3a
GKH
408 /*
409 * Call to the vsc driver to let it know that the device is being
410 * removed
411 */
415b023a 412 storvsc_drv_obj->base.dev_rm(dev);
bef4a34a 413
ff568d3a 414 if (host_device_ctx->request_pool) {
bef4a34a
HJ
415 kmem_cache_destroy(host_device_ctx->request_pool);
416 host_device_ctx->request_pool = NULL;
417 }
418
419 DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
420 scsi_remove_host(host);
421
422 DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
423 scsi_host_put(host);
1c8d0b7d 424 return 0;
bef4a34a
HJ
425}
426
3e189519 427/*
ff568d3a
GKH
428 * storvsc_commmand_completion - Command completion processing
429 */
0b3f6834 430static void storvsc_commmand_completion(struct hv_storvsc_request *request)
bef4a34a 431{
ff568d3a 432 struct storvsc_cmd_request *cmd_request =
d7a1bdb9 433 (struct storvsc_cmd_request *)request->context;
bef4a34a 434 struct scsi_cmnd *scmnd = cmd_request->cmd;
ff568d3a
GKH
435 struct host_device_context *host_device_ctx =
436 (struct host_device_context *)scmnd->device->host->hostdata;
bef4a34a 437 void (*scsi_done_fn)(struct scsi_cmnd *);
bef4a34a 438 struct scsi_sense_hdr sense_hdr;
6dc3f0a7 439 struct vmscsi_request *vm_srb;
bef4a34a 440
b856e738
BP
441 /* ASSERT(request == &cmd_request->request); */
442 /* ASSERT(scmnd); */
443 /* ASSERT((unsigned long)scmnd->host_scribble == */
444 /* (unsigned long)cmd_request); */
445 /* ASSERT(scmnd->scsi_done); */
bef4a34a 446
ff568d3a
GKH
447 if (cmd_request->bounce_sgl_count) {
448 /* using bounce buffer */
454f18a9 449 /* printk("copy_from_bounce_buffer\n"); */
bef4a34a 450
454f18a9 451 /* FIXME: We can optimize on writes by just skipping this */
ff568d3a
GKH
452 copy_from_bounce_buffer(scsi_sglist(scmnd),
453 cmd_request->bounce_sgl,
454 scsi_sg_count(scmnd));
455 destroy_bounce_buffer(cmd_request->bounce_sgl,
456 cmd_request->bounce_sgl_count);
bef4a34a
HJ
457 }
458
d7a1bdb9 459 vm_srb = &request->vstor_packet.vm_srb;
6dc3f0a7 460 scmnd->result = vm_srb->scsi_status;
bef4a34a 461
ff568d3a
GKH
462 if (scmnd->result) {
463 if (scsi_normalize_sense(scmnd->sense_buffer,
6f461cc4 464 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
bef4a34a 465 scsi_print_sense_hdr("storvsc", &sense_hdr);
bef4a34a
HJ
466 }
467
8a046024
HJ
468 /* ASSERT(request->BytesXfer <= request->data_buffer.Length); */
469 scsi_set_resid(scmnd,
d7a1bdb9 470 request->data_buffer.len -
e9e936c6 471 vm_srb->data_transfer_length);
bef4a34a
HJ
472
473 scsi_done_fn = scmnd->scsi_done;
474
475 scmnd->host_scribble = NULL;
476 scmnd->scsi_done = NULL;
477
454f18a9 478 /* !!DO NOT MODIFY the scmnd after this call */
bef4a34a
HJ
479 scsi_done_fn(scmnd);
480
481 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
bef4a34a
HJ
482}
483
484static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
485{
ff568d3a 486 int i;
bef4a34a 487
454f18a9 488 /* No need to check */
bef4a34a
HJ
489 if (sg_count < 2)
490 return -1;
491
454f18a9 492 /* We have at least 2 sg entries */
ff568d3a
GKH
493 for (i = 0; i < sg_count; i++) {
494 if (i == 0) {
495 /* make sure 1st one does not have hole */
bef4a34a
HJ
496 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
497 return i;
ff568d3a
GKH
498 } else if (i == sg_count - 1) {
499 /* make sure last one does not have hole */
bef4a34a
HJ
500 if (sgl[i].offset != 0)
501 return i;
ff568d3a
GKH
502 } else {
503 /* make sure no hole in the middle */
bef4a34a 504 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
bef4a34a 505 return i;
bef4a34a
HJ
506 }
507 }
508 return -1;
509}
510
ff568d3a
GKH
511static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
512 unsigned int sg_count,
513 unsigned int len)
bef4a34a
HJ
514{
515 int i;
ff568d3a
GKH
516 int num_pages;
517 struct scatterlist *bounce_sgl;
bef4a34a
HJ
518 struct page *page_buf;
519
73509681 520 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
bef4a34a 521
06da0bc8 522 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
bef4a34a 523 if (!bounce_sgl)
bef4a34a 524 return NULL;
bef4a34a 525
ff568d3a 526 for (i = 0; i < num_pages; i++) {
bef4a34a
HJ
527 page_buf = alloc_page(GFP_ATOMIC);
528 if (!page_buf)
bef4a34a 529 goto cleanup;
bef4a34a 530 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
bef4a34a
HJ
531 }
532
533 return bounce_sgl;
534
535cleanup:
536 destroy_bounce_buffer(bounce_sgl, num_pages);
537 return NULL;
538}
539
ff568d3a
GKH
540static void destroy_bounce_buffer(struct scatterlist *sgl,
541 unsigned int sg_count)
bef4a34a
HJ
542{
543 int i;
544 struct page *page_buf;
545
ff568d3a
GKH
546 for (i = 0; i < sg_count; i++) {
547 page_buf = sg_page((&sgl[i]));
548 if (page_buf != NULL)
bef4a34a 549 __free_page(page_buf);
bef4a34a
HJ
550 }
551
552 kfree(sgl);
553}
554
454f18a9 555/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
ff568d3a
GKH
556static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
557 struct scatterlist *bounce_sgl,
558 unsigned int orig_sgl_count)
bef4a34a 559{
ff568d3a
GKH
560 int i;
561 int j = 0;
bef4a34a
HJ
562 unsigned long src, dest;
563 unsigned int srclen, destlen, copylen;
ff568d3a
GKH
564 unsigned int total_copied = 0;
565 unsigned long bounce_addr = 0;
566 unsigned long src_addr = 0;
bef4a34a
HJ
567 unsigned long flags;
568
569 local_irq_save(flags);
570
ff568d3a
GKH
571 for (i = 0; i < orig_sgl_count; i++) {
572 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
573 KM_IRQ0) + orig_sgl[i].offset;
bef4a34a
HJ
574 src = src_addr;
575 srclen = orig_sgl[i].length;
576
b856e738 577 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
bef4a34a 578
0c47a70a 579 if (bounce_addr == 0)
bef4a34a 580 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
bef4a34a 581
ff568d3a 582 while (srclen) {
454f18a9 583 /* assume bounce offset always == 0 */
bef4a34a
HJ
584 dest = bounce_addr + bounce_sgl[j].length;
585 destlen = PAGE_SIZE - bounce_sgl[j].length;
586
fc6a4b26 587 copylen = min(srclen, destlen);
ff568d3a 588 memcpy((void *)dest, (void *)src, copylen);
bef4a34a
HJ
589
590 total_copied += copylen;
591 bounce_sgl[j].length += copylen;
592 srclen -= copylen;
593 src += copylen;
594
ff568d3a
GKH
595 if (bounce_sgl[j].length == PAGE_SIZE) {
596 /* full..move to next entry */
597 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
bef4a34a
HJ
598 j++;
599
454f18a9 600 /* if we need to use another bounce buffer */
ff568d3a 601 if (srclen || i != orig_sgl_count - 1)
bef4a34a 602 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
ff568d3a
GKH
603 } else if (srclen == 0 && i == orig_sgl_count - 1) {
604 /* unmap the last bounce that is < PAGE_SIZE */
605 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
bef4a34a
HJ
606 }
607 }
608
ff568d3a 609 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
bef4a34a
HJ
610 }
611
612 local_irq_restore(flags);
613
614 return total_copied;
615}
616
454f18a9 617/* Assume the original sgl has enough room */
ff568d3a
GKH
618static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
619 struct scatterlist *bounce_sgl,
620 unsigned int orig_sgl_count)
bef4a34a 621{
ff568d3a
GKH
622 int i;
623 int j = 0;
bef4a34a
HJ
624 unsigned long src, dest;
625 unsigned int srclen, destlen, copylen;
ff568d3a
GKH
626 unsigned int total_copied = 0;
627 unsigned long bounce_addr = 0;
628 unsigned long dest_addr = 0;
bef4a34a
HJ
629 unsigned long flags;
630
631 local_irq_save(flags);
632
ff568d3a
GKH
633 for (i = 0; i < orig_sgl_count; i++) {
634 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
635 KM_IRQ0) + orig_sgl[i].offset;
bef4a34a
HJ
636 dest = dest_addr;
637 destlen = orig_sgl[i].length;
b856e738 638 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
bef4a34a 639
0c47a70a 640 if (bounce_addr == 0)
bef4a34a 641 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
bef4a34a 642
ff568d3a 643 while (destlen) {
bef4a34a
HJ
644 src = bounce_addr + bounce_sgl[j].offset;
645 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
646
fc6a4b26 647 copylen = min(srclen, destlen);
ff568d3a 648 memcpy((void *)dest, (void *)src, copylen);
bef4a34a
HJ
649
650 total_copied += copylen;
651 bounce_sgl[j].offset += copylen;
652 destlen -= copylen;
653 dest += copylen;
654
ff568d3a
GKH
655 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
656 /* full */
657 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
bef4a34a
HJ
658 j++;
659
454f18a9 660 /* if we need to use another bounce buffer */
ff568d3a 661 if (destlen || i != orig_sgl_count - 1)
bef4a34a 662 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
ff568d3a
GKH
663 } else if (destlen == 0 && i == orig_sgl_count - 1) {
664 /* unmap the last bounce that is < PAGE_SIZE */
665 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
bef4a34a
HJ
666 }
667 }
668
ff568d3a
GKH
669 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
670 KM_IRQ0);
bef4a34a
HJ
671 }
672
673 local_irq_restore(flags);
674
675 return total_copied;
676}
677
3e189519 678/*
ff568d3a
GKH
679 * storvsc_queuecommand - Initiate command processing
680 */
f281233d 681static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd,
ff568d3a 682 void (*done)(struct scsi_cmnd *))
bef4a34a 683{
ff568d3a
GKH
684 int ret;
685 struct host_device_context *host_device_ctx =
686 (struct host_device_context *)scmnd->device->host->hostdata;
6bad88da 687 struct hv_device *device_ctx = host_device_ctx->device_ctx;
2e79505d 688 struct storvsc_driver *storvsc_drv_obj =
28e7f2a8 689 drv_to_stordrv(device_ctx->device.driver);
0b3f6834 690 struct hv_storvsc_request *request;
bef4a34a 691 struct storvsc_cmd_request *cmd_request;
ff568d3a 692 unsigned int request_size = 0;
bef4a34a
HJ
693 int i;
694 struct scatterlist *sgl;
77c5ceaf 695 unsigned int sg_count = 0;
12fbd416 696 struct vmscsi_request *vm_srb;
bef4a34a 697
ff568d3a
GKH
698 DPRINT_DBG(STORVSC_DRV, "scmnd %p dir %d, use_sg %d buf %p len %d "
699 "queue depth %d tagged %d", scmnd, scmnd->sc_data_direction,
700 scsi_sg_count(scmnd), scsi_sglist(scmnd),
701 scsi_bufflen(scmnd), scmnd->device->queue_depth,
702 scmnd->device->tagged_supported);
bef4a34a 703
454f18a9 704 /* If retrying, no need to prep the cmd */
ff568d3a 705 if (scmnd->host_scribble) {
b856e738 706 /* ASSERT(scmnd->scsi_done != NULL); */
bef4a34a 707
ff568d3a
GKH
708 cmd_request =
709 (struct storvsc_cmd_request *)scmnd->host_scribble;
710 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p",
711 scmnd, cmd_request);
bef4a34a
HJ
712
713 goto retry_request;
714 }
715
b856e738
BP
716 /* ASSERT(scmnd->scsi_done == NULL); */
717 /* ASSERT(scmnd->host_scribble == NULL); */
bef4a34a
HJ
718
719 scmnd->scsi_done = done;
720
721 request_size = sizeof(struct storvsc_cmd_request);
722
c7a9a484 723 cmd_request = kmem_cache_zalloc(host_device_ctx->request_pool,
ff568d3a
GKH
724 GFP_ATOMIC);
725 if (!cmd_request) {
726 DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - unable to allocate "
727 "storvsc_cmd_request...marking queue busy", scmnd);
bef4a34a
HJ
728 scmnd->scsi_done = NULL;
729 return SCSI_MLQUEUE_DEVICE_BUSY;
730 }
731
454f18a9 732 /* Setup the cmd request */
bef4a34a
HJ
733 cmd_request->bounce_sgl_count = 0;
734 cmd_request->bounce_sgl = NULL;
735 cmd_request->cmd = scmnd;
736
ff568d3a 737 scmnd->host_scribble = (unsigned char *)cmd_request;
bef4a34a
HJ
738
739 request = &cmd_request->request;
d7a1bdb9 740 vm_srb = &request->vstor_packet.vm_srb;
bef4a34a 741
1e05d88e 742 DPRINT_DBG(STORVSC_DRV, "req %p size %d", request, request_size);
bef4a34a 743
454f18a9 744 /* Build the SRB */
ff568d3a 745 switch (scmnd->sc_data_direction) {
bef4a34a 746 case DMA_TO_DEVICE:
12fbd416 747 vm_srb->data_in = WRITE_TYPE;
bef4a34a
HJ
748 break;
749 case DMA_FROM_DEVICE:
12fbd416 750 vm_srb->data_in = READ_TYPE;
bef4a34a
HJ
751 break;
752 default:
12fbd416 753 vm_srb->data_in = UNKNOWN_TYPE;
bef4a34a
HJ
754 break;
755 }
756
d7a1bdb9
S
757 request->on_io_completion = storvsc_commmand_completion;
758 request->context = cmd_request;/* scmnd; */
bef4a34a 759
454f18a9 760 /* request->PortId = scmnd->device->channel; */
124661de 761 vm_srb->port_number = host_device_ctx->port;
735625fe 762 vm_srb->path_id = scmnd->device->channel;
b4dba0a3 763 vm_srb->target_id = scmnd->device->id;
fc3967b0 764 vm_srb->lun = scmnd->device->lun;
bef4a34a 765
b856e738 766 /* ASSERT(scmnd->cmd_len <= 16); */
473f9409 767 vm_srb->cdb_length = scmnd->cmd_len;
373dd8a9
S
768
769 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
bef4a34a 770
d7a1bdb9 771 request->sense_buffer = scmnd->sense_buffer;
bef4a34a
HJ
772
773
d7a1bdb9 774 request->data_buffer.len = scsi_bufflen(scmnd);
ff568d3a
GKH
775 if (scsi_sg_count(scmnd)) {
776 sgl = (struct scatterlist *)scsi_sglist(scmnd);
77c5ceaf 777 sg_count = scsi_sg_count(scmnd);
bef4a34a 778
454f18a9 779 /* check if we need to bounce the sgl */
ff568d3a
GKH
780 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
781 DPRINT_INFO(STORVSC_DRV,
782 "need to bounce buffer for this scmnd %p",
783 scmnd);
784 cmd_request->bounce_sgl =
785 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
786 scsi_bufflen(scmnd));
787 if (!cmd_request->bounce_sgl) {
788 DPRINT_ERR(STORVSC_DRV,
789 "unable to create bounce buffer for "
790 "this scmnd %p", scmnd);
bef4a34a
HJ
791
792 scmnd->scsi_done = NULL;
793 scmnd->host_scribble = NULL;
ff568d3a
GKH
794 kmem_cache_free(host_device_ctx->request_pool,
795 cmd_request);
bef4a34a
HJ
796
797 return SCSI_MLQUEUE_HOST_BUSY;
798 }
799
ff568d3a 800 cmd_request->bounce_sgl_count =
73509681 801 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
ff568d3a 802 PAGE_SHIFT;
bef4a34a 803
ff568d3a
GKH
804 /*
805 * FIXME: We can optimize on reads by just skipping
806 * this
807 */
808 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
809 scsi_sg_count(scmnd));
bef4a34a
HJ
810
811 sgl = cmd_request->bounce_sgl;
77c5ceaf 812 sg_count = cmd_request->bounce_sgl_count;
bef4a34a
HJ
813 }
814
d7a1bdb9 815 request->data_buffer.offset = sgl[0].offset;
bef4a34a 816
77c5ceaf 817 for (i = 0; i < sg_count; i++) {
0686e4f4 818 DPRINT_DBG(STORVSC_DRV, "sgl[%d] len %d offset %d\n",
ff568d3a 819 i, sgl[i].length, sgl[i].offset);
d7a1bdb9 820 request->data_buffer.pfn_array[i] =
77c5ceaf 821 page_to_pfn(sg_page((&sgl[i])));
bef4a34a 822 }
ff568d3a 823 } else if (scsi_sglist(scmnd)) {
b856e738 824 /* ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); */
d7a1bdb9 825 request->data_buffer.offset =
ff568d3a 826 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
d7a1bdb9 827 request->data_buffer.pfn_array[0] =
ff568d3a 828 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
bef4a34a 829 }
bef4a34a
HJ
830
831retry_request:
454f18a9 832 /* Invokes the vsc to start an IO */
6bad88da 833 ret = storvsc_drv_obj->on_io_request(device_ctx,
ff568d3a
GKH
834 &cmd_request->request);
835 if (ret == -1) {
836 /* no more space */
837 DPRINT_ERR(STORVSC_DRV,
838 "scmnd (%p) - queue FULL...marking queue busy",
839 scmnd);
840
841 if (cmd_request->bounce_sgl_count) {
842 /*
843 * FIXME: We can optimize on writes by just skipping
844 * this
845 */
846 copy_from_bounce_buffer(scsi_sglist(scmnd),
847 cmd_request->bounce_sgl,
848 scsi_sg_count(scmnd));
849 destroy_bounce_buffer(cmd_request->bounce_sgl,
850 cmd_request->bounce_sgl_count);
bef4a34a
HJ
851 }
852
853 kmem_cache_free(host_device_ctx->request_pool, cmd_request);
854
855 scmnd->scsi_done = NULL;
856 scmnd->host_scribble = NULL;
857
858 ret = SCSI_MLQUEUE_DEVICE_BUSY;
859 }
860
bef4a34a
HJ
861 return ret;
862}
863
f281233d
JG
864static DEF_SCSI_QCMD(storvsc_queuecommand)
865
ff568d3a
GKH
866static int storvsc_merge_bvec(struct request_queue *q,
867 struct bvec_merge_data *bmd, struct bio_vec *bvec)
bef4a34a 868{
ff568d3a
GKH
869 /* checking done by caller. */
870 return bvec->bv_len;
bef4a34a 871}
bef4a34a 872
3e189519 873/*
ff568d3a
GKH
874 * storvsc_device_configure - Configure the specified scsi device
875 */
bef4a34a
HJ
876static int storvsc_device_alloc(struct scsi_device *sdevice)
877{
ff568d3a
GKH
878 DPRINT_DBG(STORVSC_DRV, "sdev (%p) - setting device flag to %d",
879 sdevice, BLIST_SPARSELUN);
880 /*
881 * This enables luns to be located sparsely. Otherwise, we may not
882 * discovered them.
883 */
bef4a34a 884 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
bef4a34a
HJ
885 return 0;
886}
887
888static int storvsc_device_configure(struct scsi_device *sdevice)
889{
ff568d3a
GKH
890 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - curr queue depth %d", sdevice,
891 sdevice->queue_depth);
bef4a34a 892
ff568d3a
GKH
893 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting queue depth to %d",
894 sdevice, STORVSC_MAX_IO_REQUESTS);
895 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
896 STORVSC_MAX_IO_REQUESTS);
bef4a34a 897
ff568d3a
GKH
898 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld",
899 sdevice, PAGE_SIZE);
bef4a34a
HJ
900 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
901
ff568d3a
GKH
902 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine",
903 sdevice);
bef4a34a
HJ
904 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
905
906 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
454f18a9 907 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
bef4a34a
HJ
908
909 return 0;
910}
911
3e189519 912/*
ff568d3a
GKH
913 * storvsc_host_reset_handler - Reset the scsi HBA
914 */
bef4a34a
HJ
915static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
916{
ff568d3a
GKH
917 int ret;
918 struct host_device_context *host_device_ctx =
919 (struct host_device_context *)scmnd->device->host->hostdata;
6bad88da 920 struct hv_device *device_ctx = host_device_ctx->device_ctx;
bef4a34a 921
ff568d3a 922 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
6bad88da 923 scmnd->device, device_ctx);
bef4a34a 924
454f18a9 925 /* Invokes the vsc to reset the host/bus */
6bad88da 926 ret = stor_vsc_on_host_reset(device_ctx);
83c720ea 927 if (ret != 0)
bef4a34a 928 return ret;
bef4a34a 929
ff568d3a 930 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
6bad88da 931 scmnd->device, device_ctx);
bef4a34a 932
bef4a34a
HJ
933 return ret;
934}
935
ff568d3a
GKH
936static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
937 sector_t capacity, int *info)
bef4a34a
HJ
938{
939 sector_t total_sectors = capacity;
ff568d3a
GKH
940 sector_t cylinder_times_heads = 0;
941 sector_t temp = 0;
bef4a34a 942
ff568d3a
GKH
943 int sectors_per_track = 0;
944 int heads = 0;
945 int cylinders = 0;
946 int rem = 0;
bef4a34a 947
ff568d3a
GKH
948 if (total_sectors > (65535 * 16 * 255))
949 total_sectors = (65535 * 16 * 255);
bef4a34a 950
ff568d3a
GKH
951 if (total_sectors >= (65535 * 16 * 63)) {
952 sectors_per_track = 255;
953 heads = 16;
bef4a34a
HJ
954
955 cylinder_times_heads = total_sectors;
ff568d3a
GKH
956 /* sector_div stores the quotient in cylinder_times_heads */
957 rem = sector_div(cylinder_times_heads, sectors_per_track);
958 } else {
959 sectors_per_track = 17;
bef4a34a
HJ
960
961 cylinder_times_heads = total_sectors;
ff568d3a
GKH
962 /* sector_div stores the quotient in cylinder_times_heads */
963 rem = sector_div(cylinder_times_heads, sectors_per_track);
bef4a34a
HJ
964
965 temp = cylinder_times_heads + 1023;
ff568d3a
GKH
966 /* sector_div stores the quotient in temp */
967 rem = sector_div(temp, 1024);
bef4a34a
HJ
968
969 heads = temp;
970
ff568d3a
GKH
971 if (heads < 4)
972 heads = 4;
bef4a34a 973
ff568d3a
GKH
974 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
975 sectors_per_track = 31;
976 heads = 16;
bef4a34a
HJ
977
978 cylinder_times_heads = total_sectors;
ff568d3a
GKH
979 /*
980 * sector_div stores the quotient in
981 * cylinder_times_heads
982 */
983 rem = sector_div(cylinder_times_heads,
984 sectors_per_track);
985 }
bef4a34a 986
ff568d3a
GKH
987 if (cylinder_times_heads >= (heads * 1024)) {
988 sectors_per_track = 63;
989 heads = 16;
bef4a34a
HJ
990
991 cylinder_times_heads = total_sectors;
ff568d3a
GKH
992 /*
993 * sector_div stores the quotient in
994 * cylinder_times_heads
995 */
996 rem = sector_div(cylinder_times_heads,
997 sectors_per_track);
998 }
999 }
bef4a34a
HJ
1000
1001 temp = cylinder_times_heads;
ff568d3a
GKH
1002 /* sector_div stores the quotient in temp */
1003 rem = sector_div(temp, heads);
bef4a34a
HJ
1004 cylinders = temp;
1005
1006 info[0] = heads;
ff568d3a
GKH
1007 info[1] = sectors_per_track;
1008 info[2] = cylinders;
bef4a34a 1009
ff568d3a
GKH
1010 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
1011 sectors_per_track);
bef4a34a
HJ
1012
1013 return 0;
1014}
1015
bef4a34a
HJ
1016static int __init storvsc_init(void)
1017{
1018 int ret;
1019
bef4a34a 1020 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
db085777 1021 ret = storvsc_drv_init();
bef4a34a
HJ
1022 return ret;
1023}
1024
1025static void __exit storvsc_exit(void)
1026{
bef4a34a 1027 storvsc_drv_exit();
bef4a34a
HJ
1028}
1029
ff568d3a 1030MODULE_LICENSE("GPL");
26c14cc1 1031MODULE_VERSION(HV_DRV_VERSION);
3afc7cc3 1032MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
bef4a34a
HJ
1033module_init(storvsc_init);
1034module_exit(storvsc_exit);