blob: 4196d6ad0cd8e8d0d9b72b6a6caef56af19e9906 [file] [log] [blame]
Hank Janssenbef4a342009-07-13 16:01:31 -07001/*
Hank Janssenbef4a342009-07-13 16:01:31 -07002 * 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>
K. Y. Srinivasan972621c2011-05-10 07:54:19 -070020 * K. Y. Srinivasan <kys@microsoft.com>
Hank Janssenbef4a342009-07-13 16:01:31 -070021 */
Hank Janssenbef4a342009-07-13 16:01:31 -070022#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070024#include <linux/module.h>
25#include <linux/device.h>
26#include <linux/blkdev.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070027#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>
Hank Janssenbef4a342009-07-13 16:01:31 -070034#include <scsi/scsi_dbg.h>
K. Y. Srinivasane3fe0bb2011-02-11 10:00:12 -080035#include "hv_api.h"
Greg Kroah-Hartman645954c2009-08-28 16:22:59 -070036#include "logging.h"
Greg Kroah-Hartman2d82f6c2010-05-05 22:52:28 -070037#include "version_info.h"
Greg Kroah-Hartman870cde82009-08-19 16:21:28 -070038#include "vmbus.h"
Greg Kroah-Hartmanbb969792010-05-05 22:40:43 -070039#include "storvsc_api.h"
K. Y. Srinivasanaf3043c2011-03-23 10:50:33 -070040#include "vstorage.h"
41#include "channel.h"
Hank Janssenbef4a342009-07-13 16:01:31 -070042
Hank Janssenbef4a342009-07-13 16:01:31 -070043
K. Y. Srinivasan0f0cdc62011-05-10 07:54:18 -070044static const char *driver_name = "storvsc";
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070045
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
K. Y. Srinivasan972621c2011-05-10 07:54:19 -070054struct hv_host_device {
K. Y. Srinivasan97c15292011-05-10 07:54:21 -070055 struct hv_device *dev;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070056 struct kmem_cache *request_pool;
57 unsigned int port;
58 unsigned char path;
59 unsigned char target;
Hank Janssenbef4a342009-07-13 16:01:31 -070060};
61
62struct storvsc_cmd_request {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070063 struct list_head entry;
64 struct scsi_cmnd *cmd;
Hank Janssenbef4a342009-07-13 16:01:31 -070065
66 unsigned int bounce_sgl_count;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -070067 struct scatterlist *bounce_sgl;
Hank Janssenbef4a342009-07-13 16:01:31 -070068
Nicolas Palix0b3f6832009-07-29 14:10:19 +020069 struct hv_storvsc_request request;
Hank Janssenbef4a342009-07-13 16:01:31 -070070};
71
Hank Janssenbef4a342009-07-13 16:01:31 -070072
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070073/*
74 * stor_vsc_initialize - Main entry point
75 */
K. Y. Srinivasan2b8b3582011-03-23 10:50:26 -070076static int stor_vsc_initialize(struct hv_driver *driver)
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070077{
K. Y. Srinivasan2e795052011-05-10 07:53:52 -070078 struct storvsc_driver *stor_driver;
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070079
K. Y. Srinivasanced01b02011-03-28 09:33:29 -070080 stor_driver = hvdr_to_stordr(driver);
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070081
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070082
83 /* Make sure we are at least 2 pages since 1 page is used for control */
84
K. Y. Srinivasan0f0cdc62011-05-10 07:54:18 -070085 driver->name = driver_name;
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070086 memcpy(&driver->dev_type, &gStorVscDeviceType,
87 sizeof(struct hv_guid));
88
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -070089
90 /*
91 * Divide the ring buffer data size (which is 1 page less
92 * than the ring buffer size since that page is reserved for
93 * the ring buffer indices) by the max request size (which is
94 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
95 */
96 stor_driver->max_outstanding_req_per_channel =
97 ((stor_driver->ring_buffer_size - PAGE_SIZE) /
98 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
99 sizeof(struct vstor_packet) + sizeof(u64),
100 sizeof(u64)));
101
102 DPRINT_INFO(STORVSC, "max io %u, currently %u\n",
103 stor_driver->max_outstanding_req_per_channel,
104 STORVSC_MAX_IO_REQUESTS);
105
106 /* Setup the dispatch table */
K. Y. Srinivasan2ac5dad2011-05-10 07:53:57 -0700107 stor_driver->base.dev_add = storvsc_dev_add;
K. Y. Srinivasancb706b042011-05-10 07:53:58 -0700108 stor_driver->base.dev_rm = storvsc_dev_remove;
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -0700109
K. Y. Srinivasanbb465922011-05-10 07:54:02 -0700110 stor_driver->on_io_request = storvsc_do_io;
K. Y. Srinivasan6dec2442011-03-23 10:50:25 -0700111
112 return 0;
113}
114
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -0700115static int storvsc_device_alloc(struct scsi_device *sdevice)
116{
117 /*
118 * This enables luns to be located sparsely. Otherwise, we may not
119 * discovered them.
120 */
121 sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN;
122 return 0;
123}
124
K. Y. Srinivasana1ebfea2011-05-10 07:54:26 -0700125static int storvsc_merge_bvec(struct request_queue *q,
126 struct bvec_merge_data *bmd, struct bio_vec *bvec)
127{
128 /* checking done by caller. */
129 return bvec->bv_len;
130}
131
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -0700132static int storvsc_device_configure(struct scsi_device *sdevice)
133{
134 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
135 STORVSC_MAX_IO_REQUESTS);
136
137 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld",
138 sdevice, PAGE_SIZE);
139 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
140
141 DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine",
142 sdevice);
143 blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec);
144
145 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
146 /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */
147
148 return 0;
149}
150
Bill Pemberton454f18a2009-07-27 16:47:24 -0400151/* Static decl */
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700152static int storvsc_probe(struct hv_device *dev);
Jeff Garzikf2812332010-11-16 02:10:29 -0500153static int storvsc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd);
Hank Janssenbef4a342009-07-13 16:01:31 -0700154static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd);
K. Y. Srinivasan415b0232011-04-29 13:45:12 -0700155static int storvsc_remove(struct hv_device *dev);
Hank Janssenbef4a342009-07-13 16:01:31 -0700156
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700157static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
158 unsigned int sg_count,
159 unsigned int len);
160static void destroy_bounce_buffer(struct scatterlist *sgl,
161 unsigned int sg_count);
Hank Janssenbef4a342009-07-13 16:01:31 -0700162static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700163static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
164 struct scatterlist *bounce_sgl,
165 unsigned int orig_sgl_count);
166static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
167 struct scatterlist *bounce_sgl,
168 unsigned int orig_sgl_count);
Hank Janssenbef4a342009-07-13 16:01:31 -0700169
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700170static int storvsc_get_chs(struct scsi_device *sdev, struct block_device *bdev,
171 sector_t capacity, int *info);
Hank Janssenbef4a342009-07-13 16:01:31 -0700172
173
174static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE;
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -0700175module_param(storvsc_ringbuffer_size, int, S_IRUGO);
176MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
Hank Janssenbef4a342009-07-13 16:01:31 -0700177
Bill Pemberton454f18a2009-07-27 16:47:24 -0400178/* The one and only one */
K. Y. Srinivasan2e795052011-05-10 07:53:52 -0700179static struct storvsc_driver g_storvsc_drv;
Hank Janssenbef4a342009-07-13 16:01:31 -0700180
Bill Pemberton454f18a2009-07-27 16:47:24 -0400181/* Scsi driver */
Hank Janssenbef4a342009-07-13 16:01:31 -0700182static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700183 .module = THIS_MODULE,
184 .name = "storvsc_host_t",
185 .bios_param = storvsc_get_chs,
186 .queuecommand = storvsc_queuecommand,
187 .eh_host_reset_handler = storvsc_host_reset_handler,
188 .slave_alloc = storvsc_device_alloc,
189 .slave_configure = storvsc_device_configure,
190 .cmd_per_lun = 1,
191 /* 64 max_queue * 1 target */
Lars Lindley0686e4f2010-03-11 23:51:23 +0100192 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700193 .this_id = -1,
Bill Pemberton454f18a2009-07-27 16:47:24 -0400194 /* no use setting to 0 since ll_blk_rw reset it to 1 */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700195 /* currently 32 */
196 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
197 /*
198 * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge
199 * into 1 sg element. If set, we must limit the max_segment_size to
200 * PAGE_SIZE, otherwise we may get 1 sg element that represents
201 * multiple
202 */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400203 /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700204 .use_clustering = ENABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -0400205 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700206 .dma_boundary = PAGE_SIZE-1,
Hank Janssenbef4a342009-07-13 16:01:31 -0700207};
208
209
Hank Janssen3e189512010-03-04 22:11:00 +0000210/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700211 * storvsc_drv_init - StorVsc driver initialization.
212 */
K. Y. Srinivasandb085772011-03-23 10:50:27 -0700213static int storvsc_drv_init(void)
Hank Janssenbef4a342009-07-13 16:01:31 -0700214{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700215 int ret;
K. Y. Srinivasan2e795052011-05-10 07:53:52 -0700216 struct storvsc_driver *storvsc_drv_obj = &g_storvsc_drv;
K. Y. Srinivasan4af27d72011-03-07 13:34:01 -0800217 struct hv_driver *drv = &g_storvsc_drv.base;
Hank Janssenbef4a342009-07-13 16:01:31 -0700218
Hank Janssen8a046022010-12-06 12:26:45 -0800219 storvsc_drv_obj->ring_buffer_size = storvsc_ringbuffer_size;
Hank Janssenbef4a342009-07-13 16:01:31 -0700220
Bill Pemberton454f18a2009-07-27 16:47:24 -0400221 /* Callback to client driver to complete the initialization */
K. Y. Srinivasandb085772011-03-23 10:50:27 -0700222 stor_vsc_initialize(&storvsc_drv_obj->base);
Hank Janssenbef4a342009-07-13 16:01:31 -0700223
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700224 DPRINT_INFO(STORVSC_DRV,
K. Y. Srinivasan1e05d882011-03-28 09:33:28 -0700225 "max outstanding reqs %u",
Hank Janssen8a046022010-12-06 12:26:45 -0800226 storvsc_drv_obj->max_outstanding_req_per_channel);
Hank Janssenbef4a342009-07-13 16:01:31 -0700227
Hank Janssen8a046022010-12-06 12:26:45 -0800228 if (storvsc_drv_obj->max_outstanding_req_per_channel <
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -0700229 STORVSC_MAX_IO_REQUESTS)
Hank Janssenbef4a342009-07-13 16:01:31 -0700230 return -1;
Hank Janssenbef4a342009-07-13 16:01:31 -0700231
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800232 drv->driver.name = storvsc_drv_obj->base.name;
Hank Janssenbef4a342009-07-13 16:01:31 -0700233
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700234 drv->probe = storvsc_probe;
K. Y. Srinivasan415b0232011-04-29 13:45:12 -0700235 drv->remove = storvsc_remove;
Hank Janssenbef4a342009-07-13 16:01:31 -0700236
Bill Pemberton454f18a2009-07-27 16:47:24 -0400237 /* The driver belongs to vmbus */
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800238 ret = vmbus_child_driver_register(&drv->driver);
Hank Janssenbef4a342009-07-13 16:01:31 -0700239
Hank Janssenbef4a342009-07-13 16:01:31 -0700240 return ret;
241}
242
K. Y. Srinivasanaf3043c2011-03-23 10:50:33 -0700243
K. Y. Srinivasan12cb12e2011-03-23 10:50:34 -0700244static int stor_vsc_on_host_reset(struct hv_device *device)
K. Y. Srinivasanaf3043c2011-03-23 10:50:33 -0700245{
246 struct storvsc_device *stor_device;
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700247 struct hv_storvsc_request *request;
K. Y. Srinivasanaf3043c2011-03-23 10:50:33 -0700248 struct vstor_packet *vstor_packet;
K. Y. Srinivasan939584652011-03-28 09:33:30 -0700249 int ret, t;
K. Y. Srinivasanaf3043c2011-03-23 10:50:33 -0700250
251 DPRINT_INFO(STORVSC, "resetting host adapter...");
252
253 stor_device = get_stor_device(device);
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -0700254 if (!stor_device)
K. Y. Srinivasanaf3043c2011-03-23 10:50:33 -0700255 return -1;
K. Y. Srinivasanaf3043c2011-03-23 10:50:33 -0700256
257 request = &stor_device->reset_request;
258 vstor_packet = &request->vstor_packet;
259
K. Y. Srinivasan939584652011-03-28 09:33:30 -0700260 init_completion(&request->wait_event);
K. Y. Srinivasanaf3043c2011-03-23 10:50:33 -0700261
262 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
263 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
264 vstor_packet->vm_srb.path_id = stor_device->path_id;
265
K. Y. Srinivasanaf3043c2011-03-23 10:50:33 -0700266 ret = vmbus_sendpacket(device->channel, vstor_packet,
267 sizeof(struct vstor_packet),
268 (unsigned long)&stor_device->reset_request,
269 VM_PKT_DATA_INBAND,
270 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -0700271 if (ret != 0)
K. Y. Srinivasanaf3043c2011-03-23 10:50:33 -0700272 goto cleanup;
K. Y. Srinivasanaf3043c2011-03-23 10:50:33 -0700273
K. Y. Srinivasan939584652011-03-28 09:33:30 -0700274 t = wait_for_completion_timeout(&request->wait_event, HZ);
275 if (t == 0) {
K. Y. Srinivasanaf3043c2011-03-23 10:50:33 -0700276 ret = -ETIMEDOUT;
277 goto cleanup;
278 }
279
280 DPRINT_INFO(STORVSC, "host adapter reset completed");
281
282 /*
283 * At this point, all outstanding requests in the adapter
284 * should have been flushed out and return to us
285 */
286
287cleanup:
288 put_stor_device(device);
289 return ret;
290}
291
Hank Janssenbef4a342009-07-13 16:01:31 -0700292static int storvsc_drv_exit_cb(struct device *dev, void *data)
293{
294 struct device **curr = (struct device **)data;
295 *curr = dev;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400296 return 1; /* stop iterating */
Hank Janssenbef4a342009-07-13 16:01:31 -0700297}
298
Greg Kroah-Hartmanbd1de702009-07-29 09:04:51 -0700299static void storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -0700300{
K. Y. Srinivasan2e795052011-05-10 07:53:52 -0700301 struct storvsc_driver *storvsc_drv_obj = &g_storvsc_drv;
K. Y. Srinivasan4af27d72011-03-07 13:34:01 -0800302 struct hv_driver *drv = &g_storvsc_drv.base;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700303 struct device *current_dev = NULL;
Bill Pemberton2295ba22009-07-28 13:46:22 -0400304 int ret;
Hank Janssenbef4a342009-07-13 16:01:31 -0700305
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700306 while (1) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700307 current_dev = NULL;
308
Bill Pemberton454f18a2009-07-27 16:47:24 -0400309 /* Get the device */
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800310 ret = driver_for_each_device(&drv->driver, NULL,
Bill Pemberton2295ba22009-07-28 13:46:22 -0400311 (void *) &current_dev,
312 storvsc_drv_exit_cb);
313
Hank Janssenbef4a342009-07-13 16:01:31 -0700314
315 if (current_dev == NULL)
316 break;
317
Bill Pemberton454f18a2009-07-27 16:47:24 -0400318 /* Initiate removal from the top-down */
Hank Janssenbef4a342009-07-13 16:01:31 -0700319 device_unregister(current_dev);
320 }
321
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800322 if (storvsc_drv_obj->base.cleanup)
323 storvsc_drv_obj->base.cleanup(&storvsc_drv_obj->base);
Hank Janssenbef4a342009-07-13 16:01:31 -0700324
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800325 vmbus_child_driver_unregister(&drv->driver);
Hank Janssenbef4a342009-07-13 16:01:31 -0700326 return;
327}
328
Hank Janssen3e189512010-03-04 22:11:00 +0000329/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700330 * storvsc_probe - Add a new device for this driver
331 */
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700332static int storvsc_probe(struct hv_device *device)
Hank Janssenbef4a342009-07-13 16:01:31 -0700333{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700334 int ret;
K. Y. Srinivasan2e795052011-05-10 07:53:52 -0700335 struct storvsc_driver *storvsc_drv_obj =
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700336 drv_to_stordrv(device->device.driver);
Hank Janssenbef4a342009-07-13 16:01:31 -0700337 struct Scsi_Host *host;
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700338 struct hv_host_device *host_dev;
Greg Kroah-Hartman9f0c7d22009-08-28 16:20:07 -0700339 struct storvsc_device_info device_info;
Hank Janssenbef4a342009-07-13 16:01:31 -0700340
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800341 if (!storvsc_drv_obj->base.dev_add)
Hank Janssenbef4a342009-07-13 16:01:31 -0700342 return -1;
343
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700344 host = scsi_host_alloc(&scsi_driver,
K. Y. Srinivasan972621c2011-05-10 07:54:19 -0700345 sizeof(struct hv_host_device));
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -0700346 if (!host)
Hank Janssenbef4a342009-07-13 16:01:31 -0700347 return -ENOMEM;
Hank Janssenbef4a342009-07-13 16:01:31 -0700348
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700349 dev_set_drvdata(&device->device, host);
Hank Janssenbef4a342009-07-13 16:01:31 -0700350
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700351 host_dev = (struct hv_host_device *)host->hostdata;
352 memset(host_dev, 0, sizeof(struct hv_host_device));
Hank Janssenbef4a342009-07-13 16:01:31 -0700353
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700354 host_dev->port = host->host_no;
K. Y. Srinivasan97c15292011-05-10 07:54:21 -0700355 host_dev->dev = device;
Hank Janssenbef4a342009-07-13 16:01:31 -0700356
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700357 host_dev->request_pool =
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700358 kmem_cache_create(dev_name(&device->device),
K. Y. Srinivasan1e05d882011-03-28 09:33:28 -0700359 sizeof(struct storvsc_cmd_request), 0,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700360 SLAB_HWCACHE_ALIGN, NULL);
Hank Janssenbef4a342009-07-13 16:01:31 -0700361
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700362 if (!host_dev->request_pool) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700363 scsi_host_put(host);
Hank Janssenbef4a342009-07-13 16:01:31 -0700364 return -ENOMEM;
365 }
366
Hank Janssen8a046022010-12-06 12:26:45 -0800367 device_info.port_number = host->host_no;
Bill Pemberton454f18a2009-07-27 16:47:24 -0400368 /* Call to the vsc driver to add the device */
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700369 ret = storvsc_drv_obj->base.dev_add(device, (void *)&device_info);
370
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700371 if (ret != 0) {
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700372 kmem_cache_destroy(host_dev->request_pool);
Hank Janssenbef4a342009-07-13 16:01:31 -0700373 scsi_host_put(host);
Hank Janssenbef4a342009-07-13 16:01:31 -0700374 return -1;
375 }
376
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700377 host_dev->path = device_info.path_id;
378 host_dev->target = device_info.target_id;
Hank Janssenbef4a342009-07-13 16:01:31 -0700379
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700380 /* max # of devices per target */
381 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
382 /* max # of targets per channel */
383 host->max_id = STORVSC_MAX_TARGETS;
384 /* max # of channels */
385 host->max_channel = STORVSC_MAX_CHANNELS - 1;
Hank Janssenbef4a342009-07-13 16:01:31 -0700386
Bill Pemberton454f18a2009-07-27 16:47:24 -0400387 /* Register the HBA and start the scsi bus scan */
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700388 ret = scsi_add_host(host, &device->device);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700389 if (ret != 0) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700390
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700391 storvsc_drv_obj->base.dev_rm(device);
Hank Janssenbef4a342009-07-13 16:01:31 -0700392
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700393 kmem_cache_destroy(host_dev->request_pool);
Hank Janssenbef4a342009-07-13 16:01:31 -0700394 scsi_host_put(host);
Hank Janssenbef4a342009-07-13 16:01:31 -0700395 return -1;
396 }
397
398 scsi_scan_host(host);
Hank Janssenbef4a342009-07-13 16:01:31 -0700399 return ret;
400}
401
Hank Janssen3e189512010-03-04 22:11:00 +0000402/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700403 * storvsc_remove - Callback when our device is removed
404 */
K. Y. Srinivasan415b0232011-04-29 13:45:12 -0700405static int storvsc_remove(struct hv_device *dev)
Hank Janssenbef4a342009-07-13 16:01:31 -0700406{
K. Y. Srinivasan2e795052011-05-10 07:53:52 -0700407 struct storvsc_driver *storvsc_drv_obj =
K. Y. Srinivasan415b0232011-04-29 13:45:12 -0700408 drv_to_stordrv(dev->device.driver);
409 struct Scsi_Host *host = dev_get_drvdata(&dev->device);
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700410 struct hv_host_device *host_dev =
K. Y. Srinivasan972621c2011-05-10 07:54:19 -0700411 (struct hv_host_device *)host->hostdata;
Hank Janssenbef4a342009-07-13 16:01:31 -0700412
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700413 /*
414 * Call to the vsc driver to let it know that the device is being
415 * removed
416 */
K. Y. Srinivasan415b0232011-04-29 13:45:12 -0700417 storvsc_drv_obj->base.dev_rm(dev);
Hank Janssenbef4a342009-07-13 16:01:31 -0700418
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700419 if (host_dev->request_pool) {
420 kmem_cache_destroy(host_dev->request_pool);
421 host_dev->request_pool = NULL;
Hank Janssenbef4a342009-07-13 16:01:31 -0700422 }
423
424 DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host);
425 scsi_remove_host(host);
426
427 DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host);
428 scsi_host_put(host);
K. Y. Srinivasan1c8d0b72011-04-22 14:55:18 -0700429 return 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700430}
431
Hank Janssen3e189512010-03-04 22:11:00 +0000432/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700433 * storvsc_commmand_completion - Command completion processing
434 */
Nicolas Palix0b3f6832009-07-29 14:10:19 +0200435static void storvsc_commmand_completion(struct hv_storvsc_request *request)
Hank Janssenbef4a342009-07-13 16:01:31 -0700436{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700437 struct storvsc_cmd_request *cmd_request =
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700438 (struct storvsc_cmd_request *)request->context;
Hank Janssenbef4a342009-07-13 16:01:31 -0700439 struct scsi_cmnd *scmnd = cmd_request->cmd;
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700440 struct hv_host_device *host_dev =
K. Y. Srinivasan972621c2011-05-10 07:54:19 -0700441 (struct hv_host_device *)scmnd->device->host->hostdata;
Hank Janssenbef4a342009-07-13 16:01:31 -0700442 void (*scsi_done_fn)(struct scsi_cmnd *);
Hank Janssenbef4a342009-07-13 16:01:31 -0700443 struct scsi_sense_hdr sense_hdr;
K. Y. Srinivasan6dc3f0a2011-03-28 09:33:42 -0700444 struct vmscsi_request *vm_srb;
Hank Janssenbef4a342009-07-13 16:01:31 -0700445
Bill Pembertonb856e732010-05-05 15:27:41 -0400446 /* ASSERT(request == &cmd_request->request); */
447 /* ASSERT(scmnd); */
448 /* ASSERT((unsigned long)scmnd->host_scribble == */
449 /* (unsigned long)cmd_request); */
450 /* ASSERT(scmnd->scsi_done); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700451
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700452 if (cmd_request->bounce_sgl_count) {
453 /* using bounce buffer */
Bill Pemberton454f18a2009-07-27 16:47:24 -0400454 /* printk("copy_from_bounce_buffer\n"); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700455
Bill Pemberton454f18a2009-07-27 16:47:24 -0400456 /* FIXME: We can optimize on writes by just skipping this */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700457 copy_from_bounce_buffer(scsi_sglist(scmnd),
458 cmd_request->bounce_sgl,
459 scsi_sg_count(scmnd));
460 destroy_bounce_buffer(cmd_request->bounce_sgl,
461 cmd_request->bounce_sgl_count);
Hank Janssenbef4a342009-07-13 16:01:31 -0700462 }
463
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700464 vm_srb = &request->vstor_packet.vm_srb;
K. Y. Srinivasan6dc3f0a2011-03-28 09:33:42 -0700465 scmnd->result = vm_srb->scsi_status;
Hank Janssenbef4a342009-07-13 16:01:31 -0700466
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700467 if (scmnd->result) {
468 if (scsi_normalize_sense(scmnd->sense_buffer,
K. Y. Srinivasan6f461cc2011-03-28 09:33:38 -0700469 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
Hank Janssenbef4a342009-07-13 16:01:31 -0700470 scsi_print_sense_hdr("storvsc", &sense_hdr);
Hank Janssenbef4a342009-07-13 16:01:31 -0700471 }
472
Hank Janssen8a046022010-12-06 12:26:45 -0800473 /* ASSERT(request->BytesXfer <= request->data_buffer.Length); */
474 scsi_set_resid(scmnd,
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700475 request->data_buffer.len -
K. Y. Srinivasane9e936c2011-03-28 09:33:43 -0700476 vm_srb->data_transfer_length);
Hank Janssenbef4a342009-07-13 16:01:31 -0700477
478 scsi_done_fn = scmnd->scsi_done;
479
480 scmnd->host_scribble = NULL;
481 scmnd->scsi_done = NULL;
482
Bill Pemberton454f18a2009-07-27 16:47:24 -0400483 /* !!DO NOT MODIFY the scmnd after this call */
Hank Janssenbef4a342009-07-13 16:01:31 -0700484 scsi_done_fn(scmnd);
485
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700486 kmem_cache_free(host_dev->request_pool, cmd_request);
Hank Janssenbef4a342009-07-13 16:01:31 -0700487}
488
489static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
490{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700491 int i;
Hank Janssenbef4a342009-07-13 16:01:31 -0700492
Bill Pemberton454f18a2009-07-27 16:47:24 -0400493 /* No need to check */
Hank Janssenbef4a342009-07-13 16:01:31 -0700494 if (sg_count < 2)
495 return -1;
496
Bill Pemberton454f18a2009-07-27 16:47:24 -0400497 /* We have at least 2 sg entries */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700498 for (i = 0; i < sg_count; i++) {
499 if (i == 0) {
500 /* make sure 1st one does not have hole */
Hank Janssenbef4a342009-07-13 16:01:31 -0700501 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
502 return i;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700503 } else if (i == sg_count - 1) {
504 /* make sure last one does not have hole */
Hank Janssenbef4a342009-07-13 16:01:31 -0700505 if (sgl[i].offset != 0)
506 return i;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700507 } else {
508 /* make sure no hole in the middle */
Hank Janssenbef4a342009-07-13 16:01:31 -0700509 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
Hank Janssenbef4a342009-07-13 16:01:31 -0700510 return i;
Hank Janssenbef4a342009-07-13 16:01:31 -0700511 }
512 }
513 return -1;
514}
515
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700516static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
517 unsigned int sg_count,
518 unsigned int len)
Hank Janssenbef4a342009-07-13 16:01:31 -0700519{
520 int i;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700521 int num_pages;
522 struct scatterlist *bounce_sgl;
Hank Janssenbef4a342009-07-13 16:01:31 -0700523 struct page *page_buf;
524
Uwe Kleine-König73509682011-01-20 09:32:01 +0100525 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
Hank Janssenbef4a342009-07-13 16:01:31 -0700526
Nicolas Palix06da0bc2009-07-22 15:39:13 +0200527 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
Hank Janssenbef4a342009-07-13 16:01:31 -0700528 if (!bounce_sgl)
Hank Janssenbef4a342009-07-13 16:01:31 -0700529 return NULL;
Hank Janssenbef4a342009-07-13 16:01:31 -0700530
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700531 for (i = 0; i < num_pages; i++) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700532 page_buf = alloc_page(GFP_ATOMIC);
533 if (!page_buf)
Hank Janssenbef4a342009-07-13 16:01:31 -0700534 goto cleanup;
Hank Janssenbef4a342009-07-13 16:01:31 -0700535 sg_set_page(&bounce_sgl[i], page_buf, 0, 0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700536 }
537
538 return bounce_sgl;
539
540cleanup:
541 destroy_bounce_buffer(bounce_sgl, num_pages);
542 return NULL;
543}
544
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700545static void destroy_bounce_buffer(struct scatterlist *sgl,
546 unsigned int sg_count)
Hank Janssenbef4a342009-07-13 16:01:31 -0700547{
548 int i;
549 struct page *page_buf;
550
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700551 for (i = 0; i < sg_count; i++) {
552 page_buf = sg_page((&sgl[i]));
553 if (page_buf != NULL)
Hank Janssenbef4a342009-07-13 16:01:31 -0700554 __free_page(page_buf);
Hank Janssenbef4a342009-07-13 16:01:31 -0700555 }
556
557 kfree(sgl);
558}
559
Bill Pemberton454f18a2009-07-27 16:47:24 -0400560/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700561static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
562 struct scatterlist *bounce_sgl,
563 unsigned int orig_sgl_count)
Hank Janssenbef4a342009-07-13 16:01:31 -0700564{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700565 int i;
566 int j = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700567 unsigned long src, dest;
568 unsigned int srclen, destlen, copylen;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700569 unsigned int total_copied = 0;
570 unsigned long bounce_addr = 0;
571 unsigned long src_addr = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700572 unsigned long flags;
573
574 local_irq_save(flags);
575
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700576 for (i = 0; i < orig_sgl_count; i++) {
577 src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
578 KM_IRQ0) + orig_sgl[i].offset;
Hank Janssenbef4a342009-07-13 16:01:31 -0700579 src = src_addr;
580 srclen = orig_sgl[i].length;
581
Bill Pembertonb856e732010-05-05 15:27:41 -0400582 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700583
Hank Janssen0c47a702010-08-05 19:29:44 +0000584 if (bounce_addr == 0)
Hank Janssenbef4a342009-07-13 16:01:31 -0700585 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700586
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700587 while (srclen) {
Bill Pemberton454f18a2009-07-27 16:47:24 -0400588 /* assume bounce offset always == 0 */
Hank Janssenbef4a342009-07-13 16:01:31 -0700589 dest = bounce_addr + bounce_sgl[j].length;
590 destlen = PAGE_SIZE - bounce_sgl[j].length;
591
Greg Kroah-Hartmanfc6a4b22009-07-15 11:05:14 -0700592 copylen = min(srclen, destlen);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700593 memcpy((void *)dest, (void *)src, copylen);
Hank Janssenbef4a342009-07-13 16:01:31 -0700594
595 total_copied += copylen;
596 bounce_sgl[j].length += copylen;
597 srclen -= copylen;
598 src += copylen;
599
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700600 if (bounce_sgl[j].length == PAGE_SIZE) {
601 /* full..move to next entry */
602 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700603 j++;
604
Bill Pemberton454f18a2009-07-27 16:47:24 -0400605 /* if we need to use another bounce buffer */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700606 if (srclen || i != orig_sgl_count - 1)
Hank Janssenbef4a342009-07-13 16:01:31 -0700607 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700608 } else if (srclen == 0 && i == orig_sgl_count - 1) {
609 /* unmap the last bounce that is < PAGE_SIZE */
610 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700611 }
612 }
613
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700614 kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700615 }
616
617 local_irq_restore(flags);
618
619 return total_copied;
620}
621
Bill Pemberton454f18a2009-07-27 16:47:24 -0400622/* Assume the original sgl has enough room */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700623static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
624 struct scatterlist *bounce_sgl,
625 unsigned int orig_sgl_count)
Hank Janssenbef4a342009-07-13 16:01:31 -0700626{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700627 int i;
628 int j = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700629 unsigned long src, dest;
630 unsigned int srclen, destlen, copylen;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700631 unsigned int total_copied = 0;
632 unsigned long bounce_addr = 0;
633 unsigned long dest_addr = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700634 unsigned long flags;
635
636 local_irq_save(flags);
637
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700638 for (i = 0; i < orig_sgl_count; i++) {
639 dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])),
640 KM_IRQ0) + orig_sgl[i].offset;
Hank Janssenbef4a342009-07-13 16:01:31 -0700641 dest = dest_addr;
642 destlen = orig_sgl[i].length;
Bill Pembertonb856e732010-05-05 15:27:41 -0400643 /* ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700644
Hank Janssen0c47a702010-08-05 19:29:44 +0000645 if (bounce_addr == 0)
Hank Janssenbef4a342009-07-13 16:01:31 -0700646 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700647
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700648 while (destlen) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700649 src = bounce_addr + bounce_sgl[j].offset;
650 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
651
Greg Kroah-Hartmanfc6a4b22009-07-15 11:05:14 -0700652 copylen = min(srclen, destlen);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700653 memcpy((void *)dest, (void *)src, copylen);
Hank Janssenbef4a342009-07-13 16:01:31 -0700654
655 total_copied += copylen;
656 bounce_sgl[j].offset += copylen;
657 destlen -= copylen;
658 dest += copylen;
659
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700660 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
661 /* full */
662 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700663 j++;
664
Bill Pemberton454f18a2009-07-27 16:47:24 -0400665 /* if we need to use another bounce buffer */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700666 if (destlen || i != orig_sgl_count - 1)
Hank Janssenbef4a342009-07-13 16:01:31 -0700667 bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700668 } else if (destlen == 0 && i == orig_sgl_count - 1) {
669 /* unmap the last bounce that is < PAGE_SIZE */
670 kunmap_atomic((void *)bounce_addr, KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700671 }
672 }
673
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700674 kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset),
675 KM_IRQ0);
Hank Janssenbef4a342009-07-13 16:01:31 -0700676 }
677
678 local_irq_restore(flags);
679
680 return total_copied;
681}
682
Hank Janssen3e189512010-03-04 22:11:00 +0000683/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700684 * storvsc_queuecommand - Initiate command processing
685 */
Jeff Garzikf2812332010-11-16 02:10:29 -0500686static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700687 void (*done)(struct scsi_cmnd *))
Hank Janssenbef4a342009-07-13 16:01:31 -0700688{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700689 int ret;
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700690 struct hv_host_device *host_dev =
K. Y. Srinivasan972621c2011-05-10 07:54:19 -0700691 (struct hv_host_device *)scmnd->device->host->hostdata;
K. Y. Srinivasanc2ad4832011-05-10 07:54:22 -0700692 struct hv_device *dev = host_dev->dev;
K. Y. Srinivasan2e795052011-05-10 07:53:52 -0700693 struct storvsc_driver *storvsc_drv_obj =
K. Y. Srinivasanc2ad4832011-05-10 07:54:22 -0700694 drv_to_stordrv(dev->device.driver);
Nicolas Palix0b3f6832009-07-29 14:10:19 +0200695 struct hv_storvsc_request *request;
Hank Janssenbef4a342009-07-13 16:01:31 -0700696 struct storvsc_cmd_request *cmd_request;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700697 unsigned int request_size = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700698 int i;
699 struct scatterlist *sgl;
Hank Janssen77c5cea2010-09-01 11:10:41 -0700700 unsigned int sg_count = 0;
K. Y. Srinivasan12fbd412011-03-28 09:33:31 -0700701 struct vmscsi_request *vm_srb;
Hank Janssenbef4a342009-07-13 16:01:31 -0700702
Hank Janssenbef4a342009-07-13 16:01:31 -0700703
Bill Pemberton454f18a2009-07-27 16:47:24 -0400704 /* If retrying, no need to prep the cmd */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700705 if (scmnd->host_scribble) {
Bill Pembertonb856e732010-05-05 15:27:41 -0400706 /* ASSERT(scmnd->scsi_done != NULL); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700707
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700708 cmd_request =
709 (struct storvsc_cmd_request *)scmnd->host_scribble;
710 DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p",
711 scmnd, cmd_request);
Hank Janssenbef4a342009-07-13 16:01:31 -0700712
713 goto retry_request;
714 }
715
Bill Pembertonb856e732010-05-05 15:27:41 -0400716 /* ASSERT(scmnd->scsi_done == NULL); */
717 /* ASSERT(scmnd->host_scribble == NULL); */
Hank Janssenbef4a342009-07-13 16:01:31 -0700718
719 scmnd->scsi_done = done;
720
721 request_size = sizeof(struct storvsc_cmd_request);
722
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700723 cmd_request = kmem_cache_zalloc(host_dev->request_pool,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700724 GFP_ATOMIC);
725 if (!cmd_request) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700726 scmnd->scsi_done = NULL;
727 return SCSI_MLQUEUE_DEVICE_BUSY;
728 }
729
Bill Pemberton454f18a2009-07-27 16:47:24 -0400730 /* Setup the cmd request */
Hank Janssenbef4a342009-07-13 16:01:31 -0700731 cmd_request->bounce_sgl_count = 0;
732 cmd_request->bounce_sgl = NULL;
733 cmd_request->cmd = scmnd;
734
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700735 scmnd->host_scribble = (unsigned char *)cmd_request;
Hank Janssenbef4a342009-07-13 16:01:31 -0700736
737 request = &cmd_request->request;
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700738 vm_srb = &request->vstor_packet.vm_srb;
Hank Janssenbef4a342009-07-13 16:01:31 -0700739
Hank Janssenbef4a342009-07-13 16:01:31 -0700740
Bill Pemberton454f18a2009-07-27 16:47:24 -0400741 /* Build the SRB */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700742 switch (scmnd->sc_data_direction) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700743 case DMA_TO_DEVICE:
K. Y. Srinivasan12fbd412011-03-28 09:33:31 -0700744 vm_srb->data_in = WRITE_TYPE;
Hank Janssenbef4a342009-07-13 16:01:31 -0700745 break;
746 case DMA_FROM_DEVICE:
K. Y. Srinivasan12fbd412011-03-28 09:33:31 -0700747 vm_srb->data_in = READ_TYPE;
Hank Janssenbef4a342009-07-13 16:01:31 -0700748 break;
749 default:
K. Y. Srinivasan12fbd412011-03-28 09:33:31 -0700750 vm_srb->data_in = UNKNOWN_TYPE;
Hank Janssenbef4a342009-07-13 16:01:31 -0700751 break;
752 }
753
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700754 request->on_io_completion = storvsc_commmand_completion;
755 request->context = cmd_request;/* scmnd; */
Hank Janssenbef4a342009-07-13 16:01:31 -0700756
Bill Pemberton454f18a2009-07-27 16:47:24 -0400757 /* request->PortId = scmnd->device->channel; */
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700758 vm_srb->port_number = host_dev->port;
K. Y. Srinivasan735625f2011-03-28 09:33:33 -0700759 vm_srb->path_id = scmnd->device->channel;
K. Y. Srinivasanb4dba0a2011-03-28 09:33:34 -0700760 vm_srb->target_id = scmnd->device->id;
K. Y. Srinivasanfc3967b2011-03-28 09:33:35 -0700761 vm_srb->lun = scmnd->device->lun;
Hank Janssenbef4a342009-07-13 16:01:31 -0700762
Bill Pembertonb856e732010-05-05 15:27:41 -0400763 /* ASSERT(scmnd->cmd_len <= 16); */
K. Y. Srinivasan473f9402011-03-28 09:33:36 -0700764 vm_srb->cdb_length = scmnd->cmd_len;
K. Y. Srinivasan373dd8a2011-03-28 09:33:37 -0700765
766 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
Hank Janssenbef4a342009-07-13 16:01:31 -0700767
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700768 request->sense_buffer = scmnd->sense_buffer;
Hank Janssenbef4a342009-07-13 16:01:31 -0700769
770
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700771 request->data_buffer.len = scsi_bufflen(scmnd);
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700772 if (scsi_sg_count(scmnd)) {
773 sgl = (struct scatterlist *)scsi_sglist(scmnd);
Hank Janssen77c5cea2010-09-01 11:10:41 -0700774 sg_count = scsi_sg_count(scmnd);
Hank Janssenbef4a342009-07-13 16:01:31 -0700775
Bill Pemberton454f18a2009-07-27 16:47:24 -0400776 /* check if we need to bounce the sgl */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700777 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700778 cmd_request->bounce_sgl =
779 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
780 scsi_bufflen(scmnd));
781 if (!cmd_request->bounce_sgl) {
Hank Janssenbef4a342009-07-13 16:01:31 -0700782 scmnd->scsi_done = NULL;
783 scmnd->host_scribble = NULL;
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700784 kmem_cache_free(host_dev->request_pool,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700785 cmd_request);
Hank Janssenbef4a342009-07-13 16:01:31 -0700786
787 return SCSI_MLQUEUE_HOST_BUSY;
788 }
789
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700790 cmd_request->bounce_sgl_count =
Uwe Kleine-König73509682011-01-20 09:32:01 +0100791 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700792 PAGE_SHIFT;
Hank Janssenbef4a342009-07-13 16:01:31 -0700793
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700794 /*
795 * FIXME: We can optimize on reads by just skipping
796 * this
797 */
798 copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl,
799 scsi_sg_count(scmnd));
Hank Janssenbef4a342009-07-13 16:01:31 -0700800
801 sgl = cmd_request->bounce_sgl;
Hank Janssen77c5cea2010-09-01 11:10:41 -0700802 sg_count = cmd_request->bounce_sgl_count;
Hank Janssenbef4a342009-07-13 16:01:31 -0700803 }
804
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700805 request->data_buffer.offset = sgl[0].offset;
Hank Janssenbef4a342009-07-13 16:01:31 -0700806
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -0700807 for (i = 0; i < sg_count; i++)
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700808 request->data_buffer.pfn_array[i] =
Hank Janssen77c5cea2010-09-01 11:10:41 -0700809 page_to_pfn(sg_page((&sgl[i])));
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -0700810
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700811 } else if (scsi_sglist(scmnd)) {
Bill Pembertonb856e732010-05-05 15:27:41 -0400812 /* ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); */
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700813 request->data_buffer.offset =
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700814 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
K. Y. Srinivasand7a1bdb2011-03-28 09:33:44 -0700815 request->data_buffer.pfn_array[0] =
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700816 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
Hank Janssenbef4a342009-07-13 16:01:31 -0700817 }
Hank Janssenbef4a342009-07-13 16:01:31 -0700818
819retry_request:
Bill Pemberton454f18a2009-07-27 16:47:24 -0400820 /* Invokes the vsc to start an IO */
K. Y. Srinivasanc2ad4832011-05-10 07:54:22 -0700821 ret = storvsc_drv_obj->on_io_request(dev,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700822 &cmd_request->request);
823 if (ret == -1) {
824 /* no more space */
Hank Janssenbef4a342009-07-13 16:01:31 -0700825
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700826 if (cmd_request->bounce_sgl_count) {
827 /*
828 * FIXME: We can optimize on writes by just skipping
829 * this
830 */
831 copy_from_bounce_buffer(scsi_sglist(scmnd),
832 cmd_request->bounce_sgl,
833 scsi_sg_count(scmnd));
834 destroy_bounce_buffer(cmd_request->bounce_sgl,
835 cmd_request->bounce_sgl_count);
Hank Janssenbef4a342009-07-13 16:01:31 -0700836 }
837
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700838 kmem_cache_free(host_dev->request_pool, cmd_request);
Hank Janssenbef4a342009-07-13 16:01:31 -0700839
840 scmnd->scsi_done = NULL;
841 scmnd->host_scribble = NULL;
842
843 ret = SCSI_MLQUEUE_DEVICE_BUSY;
844 }
845
Hank Janssenbef4a342009-07-13 16:01:31 -0700846 return ret;
847}
848
Jeff Garzikf2812332010-11-16 02:10:29 -0500849static DEF_SCSI_QCMD(storvsc_queuecommand)
850
Hank Janssen3e189512010-03-04 22:11:00 +0000851/*
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700852 * storvsc_host_reset_handler - Reset the scsi HBA
853 */
Hank Janssenbef4a342009-07-13 16:01:31 -0700854static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
855{
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700856 int ret;
K. Y. Srinivasan795b6132011-05-10 07:54:20 -0700857 struct hv_host_device *host_dev =
K. Y. Srinivasan972621c2011-05-10 07:54:19 -0700858 (struct hv_host_device *)scmnd->device->host->hostdata;
K. Y. Srinivasanc2ad4832011-05-10 07:54:22 -0700859 struct hv_device *dev = host_dev->dev;
Hank Janssenbef4a342009-07-13 16:01:31 -0700860
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700861 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...",
K. Y. Srinivasanc2ad4832011-05-10 07:54:22 -0700862 scmnd->device, dev);
Hank Janssenbef4a342009-07-13 16:01:31 -0700863
Bill Pemberton454f18a2009-07-27 16:47:24 -0400864 /* Invokes the vsc to reset the host/bus */
K. Y. Srinivasanc2ad4832011-05-10 07:54:22 -0700865 ret = stor_vsc_on_host_reset(dev);
Greg Kroah-Hartman83c720e2010-07-22 15:14:04 -0700866 if (ret != 0)
Hank Janssenbef4a342009-07-13 16:01:31 -0700867 return ret;
Hank Janssenbef4a342009-07-13 16:01:31 -0700868
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700869 DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted",
K. Y. Srinivasanc2ad4832011-05-10 07:54:22 -0700870 scmnd->device, dev);
Hank Janssenbef4a342009-07-13 16:01:31 -0700871
Hank Janssenbef4a342009-07-13 16:01:31 -0700872 return ret;
873}
874
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700875static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
876 sector_t capacity, int *info)
Hank Janssenbef4a342009-07-13 16:01:31 -0700877{
878 sector_t total_sectors = capacity;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700879 sector_t cylinder_times_heads = 0;
880 sector_t temp = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700881
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700882 int sectors_per_track = 0;
883 int heads = 0;
884 int cylinders = 0;
885 int rem = 0;
Hank Janssenbef4a342009-07-13 16:01:31 -0700886
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700887 if (total_sectors > (65535 * 16 * 255))
888 total_sectors = (65535 * 16 * 255);
Hank Janssenbef4a342009-07-13 16:01:31 -0700889
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700890 if (total_sectors >= (65535 * 16 * 63)) {
891 sectors_per_track = 255;
892 heads = 16;
Hank Janssenbef4a342009-07-13 16:01:31 -0700893
894 cylinder_times_heads = total_sectors;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700895 /* sector_div stores the quotient in cylinder_times_heads */
896 rem = sector_div(cylinder_times_heads, sectors_per_track);
897 } else {
898 sectors_per_track = 17;
Hank Janssenbef4a342009-07-13 16:01:31 -0700899
900 cylinder_times_heads = total_sectors;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700901 /* sector_div stores the quotient in cylinder_times_heads */
902 rem = sector_div(cylinder_times_heads, sectors_per_track);
Hank Janssenbef4a342009-07-13 16:01:31 -0700903
904 temp = cylinder_times_heads + 1023;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700905 /* sector_div stores the quotient in temp */
906 rem = sector_div(temp, 1024);
Hank Janssenbef4a342009-07-13 16:01:31 -0700907
908 heads = temp;
909
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700910 if (heads < 4)
911 heads = 4;
Hank Janssenbef4a342009-07-13 16:01:31 -0700912
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700913 if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) {
914 sectors_per_track = 31;
915 heads = 16;
Hank Janssenbef4a342009-07-13 16:01:31 -0700916
917 cylinder_times_heads = total_sectors;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700918 /*
919 * sector_div stores the quotient in
920 * cylinder_times_heads
921 */
922 rem = sector_div(cylinder_times_heads,
923 sectors_per_track);
924 }
Hank Janssenbef4a342009-07-13 16:01:31 -0700925
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700926 if (cylinder_times_heads >= (heads * 1024)) {
927 sectors_per_track = 63;
928 heads = 16;
Hank Janssenbef4a342009-07-13 16:01:31 -0700929
930 cylinder_times_heads = total_sectors;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700931 /*
932 * sector_div stores the quotient in
933 * cylinder_times_heads
934 */
935 rem = sector_div(cylinder_times_heads,
936 sectors_per_track);
937 }
938 }
Hank Janssenbef4a342009-07-13 16:01:31 -0700939
940 temp = cylinder_times_heads;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700941 /* sector_div stores the quotient in temp */
942 rem = sector_div(temp, heads);
Hank Janssenbef4a342009-07-13 16:01:31 -0700943 cylinders = temp;
944
945 info[0] = heads;
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700946 info[1] = sectors_per_track;
947 info[2] = cylinders;
Hank Janssenbef4a342009-07-13 16:01:31 -0700948
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700949 DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads,
950 sectors_per_track);
Hank Janssenbef4a342009-07-13 16:01:31 -0700951
952 return 0;
953}
954
Hank Janssenbef4a342009-07-13 16:01:31 -0700955static int __init storvsc_init(void)
956{
957 int ret;
958
Hank Janssenbef4a342009-07-13 16:01:31 -0700959 DPRINT_INFO(STORVSC_DRV, "Storvsc initializing....");
K. Y. Srinivasandb085772011-03-23 10:50:27 -0700960 ret = storvsc_drv_init();
Hank Janssenbef4a342009-07-13 16:01:31 -0700961 return ret;
962}
963
964static void __exit storvsc_exit(void)
965{
Hank Janssenbef4a342009-07-13 16:01:31 -0700966 storvsc_drv_exit();
Hank Janssenbef4a342009-07-13 16:01:31 -0700967}
968
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -0700969MODULE_LICENSE("GPL");
Hank Janssen26c14cc2010-02-11 23:02:42 +0000970MODULE_VERSION(HV_DRV_VERSION);
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -0700971MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
Hank Janssenbef4a342009-07-13 16:01:31 -0700972module_init(storvsc_init);
973module_exit(storvsc_exit);