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