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