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