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