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