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