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