blob: ede8694781d6449478d96fe8330361f047275b65 [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 */
K. Y. Srinivasana1be1702011-08-27 11:31:23 -070022
23#include <linux/kernel.h>
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070024#include <linux/wait.h>
K. Y. Srinivasana1be1702011-08-27 11:31:23 -070025#include <linux/sched.h>
26#include <linux/completion.h>
27#include <linux/string.h>
28#include <linux/mm.h>
29#include <linux/delay.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070030#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070032#include <linux/module.h>
33#include <linux/device.h>
Greg Kroah-Hartman46a97192011-10-04 12:29:52 -070034#include <linux/hyperv.h>
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -080035#include <linux/mempool.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070036#include <scsi/scsi.h>
37#include <scsi/scsi_cmnd.h>
38#include <scsi/scsi_host.h>
39#include <scsi/scsi_device.h>
40#include <scsi/scsi_tcq.h>
41#include <scsi/scsi_eh.h>
42#include <scsi/scsi_devinfo.h>
Hank Janssenbef4a342009-07-13 16:01:31 -070043#include <scsi/scsi_dbg.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070044
K. Y. Srinivasanc6491142012-01-12 12:38:03 -080045/*
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -080046 * All wire protocol details (storage protocol between the guest and the host)
47 * are consolidated here.
48 *
49 * Begin protocol definitions.
K. Y. Srinivasanc6491142012-01-12 12:38:03 -080050 */
51
K. Y. Srinivasan09f03552012-01-12 12:37:54 -080052/*
53 * Version history:
54 * V1 Beta: 0.1
55 * V1 RC < 2008/1/31: 1.0
56 * V1 RC > 2008/1/31: 2.0
57 * Win7: 4.2
58 */
59
K. Y. Srinivasan85904a52012-01-12 12:38:04 -080060#define VMSTOR_CURRENT_MAJOR 4
61#define VMSTOR_CURRENT_MINOR 2
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070062
63
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070064/* Packet structure describing virtual storage requests. */
65enum vstor_packet_operation {
66 VSTOR_OPERATION_COMPLETE_IO = 1,
67 VSTOR_OPERATION_REMOVE_DEVICE = 2,
68 VSTOR_OPERATION_EXECUTE_SRB = 3,
69 VSTOR_OPERATION_RESET_LUN = 4,
70 VSTOR_OPERATION_RESET_ADAPTER = 5,
71 VSTOR_OPERATION_RESET_BUS = 6,
72 VSTOR_OPERATION_BEGIN_INITIALIZATION = 7,
73 VSTOR_OPERATION_END_INITIALIZATION = 8,
74 VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9,
75 VSTOR_OPERATION_QUERY_PROPERTIES = 10,
K. Y. Srinivasan2b9525f2011-11-08 09:01:48 -080076 VSTOR_OPERATION_ENUMERATE_BUS = 11,
77 VSTOR_OPERATION_MAXIMUM = 11
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070078};
79
80/*
81 * Platform neutral description of a scsi request -
82 * this remains the same across the write regardless of 32/64 bit
83 * note: it's patterned off the SCSI_PASS_THROUGH structure
84 */
K. Y. Srinivasan6b2f9492012-01-12 12:38:05 -080085#define STORVSC_MAX_CMD_LEN 0x10
86#define STORVSC_SENSE_BUFFER_SIZE 0x12
87#define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070088
89struct vmscsi_request {
K. Y. Srinivasanc6491142012-01-12 12:38:03 -080090 u16 length;
91 u8 srb_status;
92 u8 scsi_status;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070093
K. Y. Srinivasanc6491142012-01-12 12:38:03 -080094 u8 port_number;
95 u8 path_id;
96 u8 target_id;
97 u8 lun;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -070098
K. Y. Srinivasanc6491142012-01-12 12:38:03 -080099 u8 cdb_length;
100 u8 sense_info_length;
101 u8 data_in;
102 u8 reserved;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700103
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800104 u32 data_transfer_length;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700105
106 union {
K. Y. Srinivasan6b2f9492012-01-12 12:38:05 -0800107 u8 cdb[STORVSC_MAX_CMD_LEN];
108 u8 sense_data[STORVSC_SENSE_BUFFER_SIZE];
109 u8 reserved_array[STORVSC_MAX_BUF_LEN_WITH_PADDING];
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700110 };
111} __attribute((packed));
112
113
114/*
115 * This structure is sent during the intialization phase to get the different
116 * properties of the channel.
117 */
118struct vmstorage_channel_properties {
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800119 u16 protocol_version;
120 u8 path_id;
121 u8 target_id;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700122
123 /* Note: port number is only really known on the client side */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800124 u32 port_number;
125 u32 flags;
126 u32 max_transfer_bytes;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700127
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800128 /*
129 * This id is unique for each channel and will correspond with
130 * vendor specific data in the inquiry data.
131 */
132
133 u64 unique_id;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700134} __packed;
135
136/* This structure is sent during the storage protocol negotiations. */
137struct vmstorage_protocol_version {
138 /* Major (MSW) and minor (LSW) version numbers. */
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800139 u16 major_minor;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700140
141 /*
142 * Revision number is auto-incremented whenever this file is changed
143 * (See FILL_VMSTOR_REVISION macro above). Mismatch does not
144 * definitely indicate incompatibility--but it does indicate mismatched
145 * builds.
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800146 * This is only used on the windows side. Just set it to 0.
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700147 */
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800148 u16 revision;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700149} __packed;
150
151/* Channel Property Flags */
152#define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1
153#define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2
154
155struct vstor_packet {
156 /* Requested operation type */
157 enum vstor_packet_operation operation;
158
159 /* Flags - see below for values */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800160 u32 flags;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700161
162 /* Status of the request returned from the server side. */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800163 u32 status;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700164
165 /* Data payload area */
166 union {
167 /*
168 * Structure used to forward SCSI commands from the
169 * client to the server.
170 */
171 struct vmscsi_request vm_srb;
172
173 /* Structure used to query channel properties. */
174 struct vmstorage_channel_properties storage_channel_properties;
175
176 /* Used during version negotiations. */
177 struct vmstorage_protocol_version version;
178 };
179} __packed;
180
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700181/*
K. Y. Srinivasan09f03552012-01-12 12:37:54 -0800182 * Packet Flags:
183 *
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700184 * This flag indicates that the server should send back a completion for this
185 * packet.
186 */
K. Y. Srinivasan09f03552012-01-12 12:37:54 -0800187
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700188#define REQUEST_COMPLETION_FLAG 0x1
189
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700190/* Matches Windows-end */
191enum storvsc_request_type {
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800192 WRITE_TYPE = 0,
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700193 READ_TYPE,
194 UNKNOWN_TYPE,
195};
196
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800197/*
198 * SRB status codes and masks; a subset of the codes used here.
199 */
200
201#define SRB_STATUS_AUTOSENSE_VALID 0x80
202#define SRB_STATUS_INVALID_LUN 0x20
203#define SRB_STATUS_SUCCESS 0x01
K. Y. Srinivasan67812092013-02-21 12:04:53 -0800204#define SRB_STATUS_ABORTED 0x02
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800205#define SRB_STATUS_ERROR 0x04
206
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800207/*
208 * This is the end of Protocol specific defines.
209 */
210
211
212/*
213 * We setup a mempool to allocate request structures for this driver
214 * on a per-lun basis. The following define specifies the number of
215 * elements in the pool.
216 */
217
218#define STORVSC_MIN_BUF_NR 64
219static int storvsc_ringbuffer_size = (20 * PAGE_SIZE);
220
221module_param(storvsc_ringbuffer_size, int, S_IRUGO);
222MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)");
223
K. Y. Srinivasan893def32013-06-04 12:05:05 -0700224/*
225 * Timeout in seconds for all devices managed by this driver.
226 */
227static int storvsc_timeout = 180;
228
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800229#define STORVSC_MAX_IO_REQUESTS 128
230
231/*
232 * In Hyper-V, each port/path/target maps to 1 scsi host adapter. In
233 * reality, the path/target is not used (ie always set to 0) so our
234 * scsi host adapter essentially has 1 bus with 1 target that contains
235 * up to 256 luns.
236 */
237#define STORVSC_MAX_LUNS_PER_TARGET 64
238#define STORVSC_MAX_TARGETS 1
239#define STORVSC_MAX_CHANNELS 1
240
241
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800242
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800243struct storvsc_cmd_request {
244 struct list_head entry;
245 struct scsi_cmnd *cmd;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700246
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800247 unsigned int bounce_sgl_count;
248 struct scatterlist *bounce_sgl;
249
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700250 struct hv_device *device;
251
252 /* Synchronize the request/response if needed */
253 struct completion wait_event;
254
255 unsigned char *sense_buffer;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700256 struct hv_multipage_buffer data_buffer;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700257 struct vstor_packet vstor_packet;
258};
259
260
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700261/* A storvsc device is a device object that contains a vmbus channel */
262struct storvsc_device {
263 struct hv_device *device;
264
265 bool destroy;
266 bool drain_notify;
267 atomic_t num_outstanding_req;
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700268 struct Scsi_Host *host;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700269
270 wait_queue_head_t waiting_to_drain;
271
272 /*
273 * Each unique Port/Path/Target represents 1 channel ie scsi
274 * controller. In reality, the pathid, targetid is always 0
275 * and the port is set by us
276 */
277 unsigned int port_number;
278 unsigned char path_id;
279 unsigned char target_id;
280
281 /* Used for vsc/vsp channel reset process */
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800282 struct storvsc_cmd_request init_request;
283 struct storvsc_cmd_request reset_request;
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700284};
285
K. Y. Srinivasance3e3012011-12-01 04:59:20 -0800286struct stor_mem_pools {
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700287 struct kmem_cache *request_pool;
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -0800288 mempool_t *request_mempool;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -0800289};
290
291struct hv_host_device {
292 struct hv_device *dev;
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -0700293 unsigned int port;
294 unsigned char path;
295 unsigned char target;
296};
297
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800298struct storvsc_scan_work {
299 struct work_struct work;
300 struct Scsi_Host *host;
301 uint lun;
302};
303
K. Y. Srinivasan67812092013-02-21 12:04:53 -0800304static void storvsc_device_scan(struct work_struct *work)
305{
306 struct storvsc_scan_work *wrk;
307 uint lun;
308 struct scsi_device *sdev;
309
310 wrk = container_of(work, struct storvsc_scan_work, work);
311 lun = wrk->lun;
312
313 sdev = scsi_device_lookup(wrk->host, 0, 0, lun);
314 if (!sdev)
315 goto done;
316 scsi_rescan_device(&sdev->sdev_gendev);
317 scsi_device_put(sdev);
318
319done:
320 kfree(wrk);
321}
322
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800323static void storvsc_bus_scan(struct work_struct *work)
324{
325 struct storvsc_scan_work *wrk;
326 int id, order_id;
327
328 wrk = container_of(work, struct storvsc_scan_work, work);
329 for (id = 0; id < wrk->host->max_id; ++id) {
330 if (wrk->host->reverse_ordering)
331 order_id = wrk->host->max_id - id - 1;
332 else
333 order_id = id;
334
335 scsi_scan_target(&wrk->host->shost_gendev, 0,
336 order_id, SCAN_WILD_CARD, 1);
337 }
338 kfree(wrk);
339}
340
K. Y. Srinivasanb4017312011-11-08 09:01:50 -0800341static void storvsc_remove_lun(struct work_struct *work)
342{
343 struct storvsc_scan_work *wrk;
344 struct scsi_device *sdev;
345
346 wrk = container_of(work, struct storvsc_scan_work, work);
347 if (!scsi_host_get(wrk->host))
348 goto done;
349
350 sdev = scsi_device_lookup(wrk->host, 0, 0, wrk->lun);
351
352 if (sdev) {
353 scsi_remove_device(sdev);
354 scsi_device_put(sdev);
355 }
356 scsi_host_put(wrk->host);
357
358done:
359 kfree(wrk);
360}
361
K. Y. Srinivasana8c18c52012-01-12 12:38:00 -0800362/*
K. Y. Srinivasanaf9584b2012-01-12 12:38:08 -0800363 * Major/minor macros. Minor version is in LSB, meaning that earlier flat
364 * version numbers will be interpreted as "0.x" (i.e., 1 becomes 0.1).
365 */
366
367static inline u16 storvsc_get_version(u8 major, u8 minor)
368{
369 u16 version;
370
371 version = ((major << 8) | minor);
372 return version;
373}
374
375/*
K. Y. Srinivasana8c18c52012-01-12 12:38:00 -0800376 * We can get incoming messages from the host that are not in response to
377 * messages that we have sent out. An example of this would be messages
378 * received by the guest to notify dynamic addition/removal of LUNs. To
379 * deal with potential race conditions where the driver may be in the
380 * midst of being unloaded when we might receive an unsolicited message
381 * from the host, we have implemented a mechanism to gurantee sequential
382 * consistency:
383 *
384 * 1) Once the device is marked as being destroyed, we will fail all
385 * outgoing messages.
386 * 2) We permit incoming messages when the device is being destroyed,
387 * only to properly account for messages already sent out.
388 */
389
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700390static inline struct storvsc_device *get_out_stor_device(
391 struct hv_device *device)
392{
393 struct storvsc_device *stor_device;
394
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700395 stor_device = hv_get_drvdata(device);
K. Y. Srinivasanf0d79fe2011-08-27 11:31:24 -0700396
397 if (stor_device && stor_device->destroy)
398 stor_device = NULL;
399
400 return stor_device;
401}
402
403
404static inline void storvsc_wait_to_drain(struct storvsc_device *dev)
405{
406 dev->drain_notify = true;
407 wait_event(dev->waiting_to_drain,
408 atomic_read(&dev->num_outstanding_req) == 0);
409 dev->drain_notify = false;
410}
Hank Janssenbef4a342009-07-13 16:01:31 -0700411
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700412static inline struct storvsc_device *get_in_stor_device(
413 struct hv_device *device)
414{
415 struct storvsc_device *stor_device;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700416
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700417 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700418
419 if (!stor_device)
420 goto get_in_err;
421
422 /*
423 * If the device is being destroyed; allow incoming
424 * traffic only to cleanup outstanding requests.
425 */
426
427 if (stor_device->destroy &&
428 (atomic_read(&stor_device->num_outstanding_req) == 0))
429 stor_device = NULL;
430
431get_in_err:
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700432 return stor_device;
433
434}
435
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800436static void destroy_bounce_buffer(struct scatterlist *sgl,
437 unsigned int sg_count)
438{
439 int i;
440 struct page *page_buf;
441
442 for (i = 0; i < sg_count; i++) {
443 page_buf = sg_page((&sgl[i]));
444 if (page_buf != NULL)
445 __free_page(page_buf);
446 }
447
448 kfree(sgl);
449}
450
451static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count)
452{
453 int i;
454
455 /* No need to check */
456 if (sg_count < 2)
457 return -1;
458
459 /* We have at least 2 sg entries */
460 for (i = 0; i < sg_count; i++) {
461 if (i == 0) {
462 /* make sure 1st one does not have hole */
463 if (sgl[i].offset + sgl[i].length != PAGE_SIZE)
464 return i;
465 } else if (i == sg_count - 1) {
466 /* make sure last one does not have hole */
467 if (sgl[i].offset != 0)
468 return i;
469 } else {
470 /* make sure no hole in the middle */
471 if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0)
472 return i;
473 }
474 }
475 return -1;
476}
477
478static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl,
479 unsigned int sg_count,
480 unsigned int len,
481 int write)
482{
483 int i;
484 int num_pages;
485 struct scatterlist *bounce_sgl;
486 struct page *page_buf;
487 unsigned int buf_len = ((write == WRITE_TYPE) ? 0 : PAGE_SIZE);
488
489 num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT;
490
491 bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC);
492 if (!bounce_sgl)
493 return NULL;
494
K. Y. Srinivasan9d2696e2013-02-06 05:15:28 -0800495 sg_init_table(bounce_sgl, num_pages);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800496 for (i = 0; i < num_pages; i++) {
497 page_buf = alloc_page(GFP_ATOMIC);
498 if (!page_buf)
499 goto cleanup;
500 sg_set_page(&bounce_sgl[i], page_buf, buf_len, 0);
501 }
502
503 return bounce_sgl;
504
505cleanup:
506 destroy_bounce_buffer(bounce_sgl, num_pages);
507 return NULL;
508}
509
Linus Torvalds9f393832012-03-21 09:40:26 -0700510/* Disgusting wrapper functions */
511static inline unsigned long sg_kmap_atomic(struct scatterlist *sgl, int idx)
512{
513 void *addr = kmap_atomic(sg_page(sgl + idx));
514 return (unsigned long)addr;
515}
516
517static inline void sg_kunmap_atomic(unsigned long addr)
518{
519 kunmap_atomic((void *)addr);
520}
521
522
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800523/* Assume the original sgl has enough room */
524static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl,
525 struct scatterlist *bounce_sgl,
526 unsigned int orig_sgl_count,
527 unsigned int bounce_sgl_count)
528{
529 int i;
530 int j = 0;
531 unsigned long src, dest;
532 unsigned int srclen, destlen, copylen;
533 unsigned int total_copied = 0;
534 unsigned long bounce_addr = 0;
535 unsigned long dest_addr = 0;
536 unsigned long flags;
537
538 local_irq_save(flags);
539
540 for (i = 0; i < orig_sgl_count; i++) {
Linus Torvalds9f393832012-03-21 09:40:26 -0700541 dest_addr = sg_kmap_atomic(orig_sgl,i) + orig_sgl[i].offset;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800542 dest = dest_addr;
543 destlen = orig_sgl[i].length;
544
545 if (bounce_addr == 0)
Linus Torvalds9f393832012-03-21 09:40:26 -0700546 bounce_addr = sg_kmap_atomic(bounce_sgl,j);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800547
548 while (destlen) {
549 src = bounce_addr + bounce_sgl[j].offset;
550 srclen = bounce_sgl[j].length - bounce_sgl[j].offset;
551
552 copylen = min(srclen, destlen);
553 memcpy((void *)dest, (void *)src, copylen);
554
555 total_copied += copylen;
556 bounce_sgl[j].offset += copylen;
557 destlen -= copylen;
558 dest += copylen;
559
560 if (bounce_sgl[j].offset == bounce_sgl[j].length) {
561 /* full */
Linus Torvalds9f393832012-03-21 09:40:26 -0700562 sg_kunmap_atomic(bounce_addr);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800563 j++;
564
565 /*
566 * It is possible that the number of elements
567 * in the bounce buffer may not be equal to
568 * the number of elements in the original
569 * scatter list. Handle this correctly.
570 */
571
572 if (j == bounce_sgl_count) {
573 /*
574 * We are done; cleanup and return.
575 */
Linus Torvalds9f393832012-03-21 09:40:26 -0700576 sg_kunmap_atomic(dest_addr - orig_sgl[i].offset);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800577 local_irq_restore(flags);
578 return total_copied;
579 }
580
581 /* if we need to use another bounce buffer */
582 if (destlen || i != orig_sgl_count - 1)
Linus Torvalds9f393832012-03-21 09:40:26 -0700583 bounce_addr = sg_kmap_atomic(bounce_sgl,j);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800584 } else if (destlen == 0 && i == orig_sgl_count - 1) {
585 /* unmap the last bounce that is < PAGE_SIZE */
Linus Torvalds9f393832012-03-21 09:40:26 -0700586 sg_kunmap_atomic(bounce_addr);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800587 }
588 }
589
Linus Torvalds9f393832012-03-21 09:40:26 -0700590 sg_kunmap_atomic(dest_addr - orig_sgl[i].offset);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800591 }
592
593 local_irq_restore(flags);
594
595 return total_copied;
596}
597
598/* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */
599static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl,
600 struct scatterlist *bounce_sgl,
601 unsigned int orig_sgl_count)
602{
603 int i;
604 int j = 0;
605 unsigned long src, dest;
606 unsigned int srclen, destlen, copylen;
607 unsigned int total_copied = 0;
608 unsigned long bounce_addr = 0;
609 unsigned long src_addr = 0;
610 unsigned long flags;
611
612 local_irq_save(flags);
613
614 for (i = 0; i < orig_sgl_count; i++) {
Linus Torvalds9f393832012-03-21 09:40:26 -0700615 src_addr = sg_kmap_atomic(orig_sgl,i) + orig_sgl[i].offset;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800616 src = src_addr;
617 srclen = orig_sgl[i].length;
618
619 if (bounce_addr == 0)
Linus Torvalds9f393832012-03-21 09:40:26 -0700620 bounce_addr = sg_kmap_atomic(bounce_sgl,j);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800621
622 while (srclen) {
623 /* assume bounce offset always == 0 */
624 dest = bounce_addr + bounce_sgl[j].length;
625 destlen = PAGE_SIZE - bounce_sgl[j].length;
626
627 copylen = min(srclen, destlen);
628 memcpy((void *)dest, (void *)src, copylen);
629
630 total_copied += copylen;
631 bounce_sgl[j].length += copylen;
632 srclen -= copylen;
633 src += copylen;
634
635 if (bounce_sgl[j].length == PAGE_SIZE) {
636 /* full..move to next entry */
Linus Torvalds9f393832012-03-21 09:40:26 -0700637 sg_kunmap_atomic(bounce_addr);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800638 j++;
639
640 /* if we need to use another bounce buffer */
641 if (srclen || i != orig_sgl_count - 1)
Linus Torvalds9f393832012-03-21 09:40:26 -0700642 bounce_addr = sg_kmap_atomic(bounce_sgl,j);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800643
644 } else if (srclen == 0 && i == orig_sgl_count - 1) {
645 /* unmap the last bounce that is < PAGE_SIZE */
Linus Torvalds9f393832012-03-21 09:40:26 -0700646 sg_kunmap_atomic(bounce_addr);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800647 }
648 }
649
Linus Torvalds9f393832012-03-21 09:40:26 -0700650 sg_kunmap_atomic(src_addr - orig_sgl[i].offset);
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800651 }
652
653 local_irq_restore(flags);
654
655 return total_copied;
656}
657
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700658static int storvsc_channel_init(struct hv_device *device)
659{
660 struct storvsc_device *stor_device;
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800661 struct storvsc_cmd_request *request;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700662 struct vstor_packet *vstor_packet;
663 int ret, t;
664
665 stor_device = get_out_stor_device(device);
666 if (!stor_device)
667 return -ENODEV;
668
669 request = &stor_device->init_request;
670 vstor_packet = &request->vstor_packet;
671
672 /*
673 * Now, initiate the vsc/vsp initialization protocol on the open
674 * channel
675 */
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800676 memset(request, 0, sizeof(struct storvsc_cmd_request));
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700677 init_completion(&request->wait_event);
678 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
679 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
680
681 ret = vmbus_sendpacket(device->channel, vstor_packet,
682 sizeof(struct vstor_packet),
683 (unsigned long)request,
684 VM_PKT_DATA_INBAND,
685 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
686 if (ret != 0)
687 goto cleanup;
688
689 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
690 if (t == 0) {
691 ret = -ETIMEDOUT;
692 goto cleanup;
693 }
694
695 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
696 vstor_packet->status != 0)
697 goto cleanup;
698
699
700 /* reuse the packet for version range supported */
701 memset(vstor_packet, 0, sizeof(struct vstor_packet));
702 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
703 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
704
K. Y. Srinivasan85904a52012-01-12 12:38:04 -0800705 vstor_packet->version.major_minor =
706 storvsc_get_version(VMSTOR_CURRENT_MAJOR, VMSTOR_CURRENT_MINOR);
707
708 /*
709 * The revision number is only used in Windows; set it to 0.
710 */
K. Y. Srinivasanc6491142012-01-12 12:38:03 -0800711 vstor_packet->version.revision = 0;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700712
713 ret = vmbus_sendpacket(device->channel, vstor_packet,
714 sizeof(struct vstor_packet),
715 (unsigned long)request,
716 VM_PKT_DATA_INBAND,
717 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
718 if (ret != 0)
719 goto cleanup;
720
721 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
722 if (t == 0) {
723 ret = -ETIMEDOUT;
724 goto cleanup;
725 }
726
727 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
728 vstor_packet->status != 0)
729 goto cleanup;
730
731
732 memset(vstor_packet, 0, sizeof(struct vstor_packet));
733 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
734 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
735 vstor_packet->storage_channel_properties.port_number =
736 stor_device->port_number;
737
738 ret = vmbus_sendpacket(device->channel, vstor_packet,
739 sizeof(struct vstor_packet),
740 (unsigned long)request,
741 VM_PKT_DATA_INBAND,
742 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
743
744 if (ret != 0)
745 goto cleanup;
746
747 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
748 if (t == 0) {
749 ret = -ETIMEDOUT;
750 goto cleanup;
751 }
752
753 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
754 vstor_packet->status != 0)
755 goto cleanup;
756
757 stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
758 stor_device->target_id
759 = vstor_packet->storage_channel_properties.target_id;
760
761 memset(vstor_packet, 0, sizeof(struct vstor_packet));
762 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
763 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
764
765 ret = vmbus_sendpacket(device->channel, vstor_packet,
766 sizeof(struct vstor_packet),
767 (unsigned long)request,
768 VM_PKT_DATA_INBAND,
769 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
770
771 if (ret != 0)
772 goto cleanup;
773
774 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
775 if (t == 0) {
776 ret = -ETIMEDOUT;
777 goto cleanup;
778 }
779
780 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
781 vstor_packet->status != 0)
782 goto cleanup;
783
784
785cleanup:
786 return ret;
787}
788
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -0800789static void storvsc_handle_error(struct vmscsi_request *vm_srb,
790 struct scsi_cmnd *scmnd,
791 struct Scsi_Host *host,
792 u8 asc, u8 ascq)
793{
794 struct storvsc_scan_work *wrk;
795 void (*process_err_fn)(struct work_struct *work);
796 bool do_work = false;
797
798 switch (vm_srb->srb_status) {
799 case SRB_STATUS_ERROR:
800 /*
801 * If there is an error; offline the device since all
802 * error recovery strategies would have already been
803 * deployed on the host side. However, if the command
804 * were a pass-through command deal with it appropriately.
805 */
806 switch (scmnd->cmnd[0]) {
807 case ATA_16:
808 case ATA_12:
809 set_host_byte(scmnd, DID_PASSTHROUGH);
810 break;
811 default:
812 set_host_byte(scmnd, DID_TARGET_FAILURE);
813 }
814 break;
815 case SRB_STATUS_INVALID_LUN:
816 do_work = true;
817 process_err_fn = storvsc_remove_lun;
818 break;
K. Y. Srinivasan67812092013-02-21 12:04:53 -0800819 case (SRB_STATUS_ABORTED | SRB_STATUS_AUTOSENSE_VALID):
820 if ((asc == 0x2a) && (ascq == 0x9)) {
821 do_work = true;
822 process_err_fn = storvsc_device_scan;
823 /*
824 * Retry the I/O that trigerred this.
825 */
826 set_host_byte(scmnd, DID_REQUEUE);
827 }
828 break;
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -0800829 }
K. Y. Srinivasan67812092013-02-21 12:04:53 -0800830
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -0800831 if (!do_work)
832 return;
833
834 /*
835 * We need to schedule work to process this error; schedule it.
836 */
837 wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
838 if (!wrk) {
839 set_host_byte(scmnd, DID_TARGET_FAILURE);
840 return;
841 }
842
843 wrk->host = host;
844 wrk->lun = vm_srb->lun;
845 INIT_WORK(&wrk->work, process_err_fn);
846 schedule_work(&wrk->work);
847}
848
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800849
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800850static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request)
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800851{
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800852 struct scsi_cmnd *scmnd = cmd_request->cmd;
853 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
854 void (*scsi_done_fn)(struct scsi_cmnd *);
855 struct scsi_sense_hdr sense_hdr;
856 struct vmscsi_request *vm_srb;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800857 struct stor_mem_pools *memp = scmnd->device->hostdata;
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -0800858 struct Scsi_Host *host;
859 struct storvsc_device *stor_dev;
860 struct hv_device *dev = host_dev->dev;
861
862 stor_dev = get_in_stor_device(dev);
863 host = stor_dev->host;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800864
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800865 vm_srb = &cmd_request->vstor_packet.vm_srb;
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800866 if (cmd_request->bounce_sgl_count) {
867 if (vm_srb->data_in == READ_TYPE)
868 copy_from_bounce_buffer(scsi_sglist(scmnd),
869 cmd_request->bounce_sgl,
870 scsi_sg_count(scmnd),
871 cmd_request->bounce_sgl_count);
872 destroy_bounce_buffer(cmd_request->bounce_sgl,
873 cmd_request->bounce_sgl_count);
874 }
875
K. Y. Srinivasan42e22ca2012-04-05 12:26:52 -0700876 scmnd->result = vm_srb->scsi_status;
877
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800878 if (scmnd->result) {
879 if (scsi_normalize_sense(scmnd->sense_buffer,
880 SCSI_SENSE_BUFFERSIZE, &sense_hdr))
881 scsi_print_sense_hdr("storvsc", &sense_hdr);
882 }
883
K. Y. Srinivasanc50bd442013-02-21 12:04:52 -0800884 if (vm_srb->srb_status != SRB_STATUS_SUCCESS)
885 storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc,
886 sense_hdr.ascq);
887
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800888 scsi_set_resid(scmnd,
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800889 cmd_request->data_buffer.len -
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800890 vm_srb->data_transfer_length);
891
892 scsi_done_fn = scmnd->scsi_done;
893
894 scmnd->host_scribble = NULL;
895 scmnd->scsi_done = NULL;
896
897 scsi_done_fn(scmnd);
898
899 mempool_free(cmd_request, memp->request_mempool);
900}
901
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700902static void storvsc_on_io_completion(struct hv_device *device,
903 struct vstor_packet *vstor_packet,
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800904 struct storvsc_cmd_request *request)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700905{
906 struct storvsc_device *stor_device;
907 struct vstor_packet *stor_pkt;
908
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -0700909 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700910 stor_pkt = &request->vstor_packet;
911
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -0700912 /*
913 * The current SCSI handling on the host side does
914 * not correctly handle:
915 * INQUIRY command with page code parameter set to 0x80
916 * MODE_SENSE command with cmd[2] == 0x1c
917 *
918 * Setup srb and scsi status so this won't be fatal.
919 * We do this so we can distinguish truly fatal failues
920 * (srb status == 0x4) and off-line the device in that case.
921 */
922
923 if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) ||
K. Y. Srinivasana8c18c52012-01-12 12:38:00 -0800924 (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) {
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -0700925 vstor_packet->vm_srb.scsi_status = 0;
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800926 vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS;
K. Y. Srinivasan4ed51a22011-08-27 11:31:26 -0700927 }
928
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700929
930 /* Copy over the status...etc */
931 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
932 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
933 stor_pkt->vm_srb.sense_info_length =
934 vstor_packet->vm_srb.sense_info_length;
935
936 if (vstor_packet->vm_srb.scsi_status != 0 ||
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800937 vstor_packet->vm_srb.srb_status != SRB_STATUS_SUCCESS){
Greg Kroah-Hartmand181daa2011-10-11 09:20:31 -0600938 dev_warn(&device->device,
939 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
940 stor_pkt->vm_srb.cdb[0],
941 vstor_packet->vm_srb.scsi_status,
942 vstor_packet->vm_srb.srb_status);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700943 }
944
945 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
946 /* CHECK_CONDITION */
K. Y. Srinivasan16046322012-01-12 12:37:57 -0800947 if (vstor_packet->vm_srb.srb_status &
948 SRB_STATUS_AUTOSENSE_VALID) {
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700949 /* autosense data available */
Greg Kroah-Hartmand181daa2011-10-11 09:20:31 -0600950 dev_warn(&device->device,
K. Y. Srinivasan41098f82011-10-14 21:31:57 -0700951 "stor pkt %p autosense data valid - len %d\n",
952 request,
953 vstor_packet->vm_srb.sense_info_length);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700954
955 memcpy(request->sense_buffer,
956 vstor_packet->vm_srb.sense_data,
957 vstor_packet->vm_srb.sense_info_length);
958
959 }
960 }
961
962 stor_pkt->vm_srb.data_transfer_length =
963 vstor_packet->vm_srb.data_transfer_length;
964
K. Y. Srinivasan27073882012-01-12 12:38:01 -0800965 storvsc_command_completion(request);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700966
967 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
968 stor_device->drain_notify)
969 wake_up(&stor_device->waiting_to_drain);
970
971
972}
973
974static void storvsc_on_receive(struct hv_device *device,
975 struct vstor_packet *vstor_packet,
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -0800976 struct storvsc_cmd_request *request)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700977{
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800978 struct storvsc_scan_work *work;
979 struct storvsc_device *stor_device;
980
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700981 switch (vstor_packet->operation) {
982 case VSTOR_OPERATION_COMPLETE_IO:
983 storvsc_on_io_completion(device, vstor_packet, request);
984 break;
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800985
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700986 case VSTOR_OPERATION_REMOVE_DEVICE:
K. Y. Srinivasan12675792011-11-08 09:01:49 -0800987 case VSTOR_OPERATION_ENUMERATE_BUS:
988 stor_device = get_in_stor_device(device);
989 work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC);
990 if (!work)
991 return;
992
993 INIT_WORK(&work->work, storvsc_bus_scan);
994 work->host = stor_device->host;
995 schedule_work(&work->work);
996 break;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -0700997
998 default:
999 break;
1000 }
1001}
1002
1003static void storvsc_on_channel_callback(void *context)
1004{
1005 struct hv_device *device = (struct hv_device *)context;
1006 struct storvsc_device *stor_device;
1007 u32 bytes_recvd;
1008 u64 request_id;
1009 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001010 struct storvsc_cmd_request *request;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001011 int ret;
1012
1013
1014 stor_device = get_in_stor_device(device);
1015 if (!stor_device)
1016 return;
1017
1018 do {
1019 ret = vmbus_recvpacket(device->channel, packet,
1020 ALIGN(sizeof(struct vstor_packet), 8),
1021 &bytes_recvd, &request_id);
1022 if (ret == 0 && bytes_recvd > 0) {
1023
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001024 request = (struct storvsc_cmd_request *)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001025 (unsigned long)request_id;
1026
1027 if ((request == &stor_device->init_request) ||
1028 (request == &stor_device->reset_request)) {
1029
1030 memcpy(&request->vstor_packet, packet,
1031 sizeof(struct vstor_packet));
1032 complete(&request->wait_event);
1033 } else {
1034 storvsc_on_receive(device,
1035 (struct vstor_packet *)packet,
1036 request);
1037 }
1038 } else {
1039 break;
1040 }
1041 } while (1);
1042
1043 return;
1044}
1045
1046static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size)
1047{
1048 struct vmstorage_channel_properties props;
1049 int ret;
1050
1051 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
1052
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001053 ret = vmbus_open(device->channel,
1054 ring_size,
1055 ring_size,
1056 (void *)&props,
1057 sizeof(struct vmstorage_channel_properties),
1058 storvsc_on_channel_callback, device);
1059
1060 if (ret != 0)
1061 return ret;
1062
1063 ret = storvsc_channel_init(device);
1064
1065 return ret;
1066}
1067
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -07001068static int storvsc_dev_remove(struct hv_device *device)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001069{
1070 struct storvsc_device *stor_device;
1071 unsigned long flags;
1072
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001073 stor_device = hv_get_drvdata(device);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001074
1075 spin_lock_irqsave(&device->channel->inbound_lock, flags);
1076 stor_device->destroy = true;
1077 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
1078
1079 /*
1080 * At this point, all outbound traffic should be disable. We
1081 * only allow inbound traffic (responses) to proceed so that
1082 * outstanding requests can be completed.
1083 */
1084
1085 storvsc_wait_to_drain(stor_device);
1086
1087 /*
1088 * Since we have already drained, we don't need to busy wait
1089 * as was done in final_release_stor_device()
1090 * Note that we cannot set the ext pointer to NULL until
1091 * we have drained - to drain the outgoing packets, we need to
1092 * allow incoming packets.
1093 */
1094 spin_lock_irqsave(&device->channel->inbound_lock, flags);
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001095 hv_set_drvdata(device, NULL);
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001096 spin_unlock_irqrestore(&device->channel->inbound_lock, flags);
1097
1098 /* Close the channel */
1099 vmbus_close(device->channel);
1100
1101 kfree(stor_device);
1102 return 0;
1103}
1104
K. Y. Srinivasanc1b3d062011-08-27 11:31:25 -07001105static int storvsc_do_io(struct hv_device *device,
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001106 struct storvsc_cmd_request *request)
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001107{
1108 struct storvsc_device *stor_device;
1109 struct vstor_packet *vstor_packet;
1110 int ret = 0;
1111
1112 vstor_packet = &request->vstor_packet;
1113 stor_device = get_out_stor_device(device);
1114
1115 if (!stor_device)
1116 return -ENODEV;
1117
1118
1119 request->device = device;
1120
1121
1122 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
1123
1124 vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
1125
1126
K. Y. Srinivasan6b2f9492012-01-12 12:38:05 -08001127 vstor_packet->vm_srb.sense_info_length = STORVSC_SENSE_BUFFER_SIZE;
K. Y. Srinivasan8dcf37d2011-08-27 11:31:22 -07001128
1129
1130 vstor_packet->vm_srb.data_transfer_length =
1131 request->data_buffer.len;
1132
1133 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
1134
1135 if (request->data_buffer.len) {
1136 ret = vmbus_sendpacket_multipagebuffer(device->channel,
1137 &request->data_buffer,
1138 vstor_packet,
1139 sizeof(struct vstor_packet),
1140 (unsigned long)request);
1141 } else {
1142 ret = vmbus_sendpacket(device->channel, vstor_packet,
1143 sizeof(struct vstor_packet),
1144 (unsigned long)request,
1145 VM_PKT_DATA_INBAND,
1146 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1147 }
1148
1149 if (ret != 0)
1150 return ret;
1151
1152 atomic_inc(&stor_device->num_outstanding_req);
1153
1154 return ret;
1155}
1156
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -07001157static int storvsc_device_alloc(struct scsi_device *sdevice)
1158{
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001159 struct stor_mem_pools *memp;
1160 int number = STORVSC_MIN_BUF_NR;
1161
1162 memp = kzalloc(sizeof(struct stor_mem_pools), GFP_KERNEL);
1163 if (!memp)
1164 return -ENOMEM;
1165
1166 memp->request_pool =
1167 kmem_cache_create(dev_name(&sdevice->sdev_dev),
1168 sizeof(struct storvsc_cmd_request), 0,
1169 SLAB_HWCACHE_ALIGN, NULL);
1170
1171 if (!memp->request_pool)
1172 goto err0;
1173
1174 memp->request_mempool = mempool_create(number, mempool_alloc_slab,
1175 mempool_free_slab,
1176 memp->request_pool);
1177
1178 if (!memp->request_mempool)
1179 goto err1;
1180
1181 sdevice->hostdata = memp;
1182
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -07001183 return 0;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001184
1185err1:
1186 kmem_cache_destroy(memp->request_pool);
1187
1188err0:
1189 kfree(memp);
1190 return -ENOMEM;
1191}
1192
1193static void storvsc_device_destroy(struct scsi_device *sdevice)
1194{
1195 struct stor_mem_pools *memp = sdevice->hostdata;
1196
1197 mempool_destroy(memp->request_mempool);
1198 kmem_cache_destroy(memp->request_pool);
1199 kfree(memp);
1200 sdevice->hostdata = NULL;
K. Y. Srinivasan5b60ace2011-05-10 07:54:25 -07001201}
1202
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001203static int storvsc_device_configure(struct scsi_device *sdevice)
1204{
1205 scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG,
1206 STORVSC_MAX_IO_REQUESTS);
1207
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001208 blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE);
1209
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001210 blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY);
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001211
K. Y. Srinivasan893def32013-06-04 12:05:05 -07001212 blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ));
1213
Olaf Hering3e8f4f42013-02-21 12:04:51 -08001214 sdevice->no_write_same = 1;
1215
K. Y. Srinivasan419f2d02011-05-10 07:54:27 -07001216 return 0;
1217}
1218
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001219static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev,
1220 sector_t capacity, int *info)
1221{
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001222 sector_t nsect = capacity;
1223 sector_t cylinders = nsect;
1224 int heads, sectors_pt;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001225
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001226 /*
1227 * We are making up these values; let us keep it simple.
1228 */
1229 heads = 0xff;
1230 sectors_pt = 0x3f; /* Sectors per track */
1231 sector_div(cylinders, heads * sectors_pt);
1232 if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect)
1233 cylinders = 0xffff;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001234
1235 info[0] = heads;
K. Y. Srinivasan5326fd52011-05-10 07:54:52 -07001236 info[1] = sectors_pt;
1237 info[2] = (int)cylinders;
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001238
K. Y. Srinivasan62838ce2011-05-10 07:54:34 -07001239 return 0;
1240}
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001241
K. Y. Srinivasan4b270c82012-01-12 12:37:58 -08001242static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd)
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001243{
K. Y. Srinivasan4b270c82012-01-12 12:37:58 -08001244 struct hv_host_device *host_dev = shost_priv(scmnd->device->host);
1245 struct hv_device *device = host_dev->dev;
1246
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001247 struct storvsc_device *stor_device;
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001248 struct storvsc_cmd_request *request;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001249 struct vstor_packet *vstor_packet;
1250 int ret, t;
1251
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001252
K. Y. Srinivasan1eaaddf2011-08-27 11:31:03 -07001253 stor_device = get_out_stor_device(device);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001254 if (!stor_device)
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001255 return FAILED;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001256
1257 request = &stor_device->reset_request;
1258 vstor_packet = &request->vstor_packet;
1259
1260 init_completion(&request->wait_event);
1261
1262 vstor_packet->operation = VSTOR_OPERATION_RESET_BUS;
1263 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
1264 vstor_packet->vm_srb.path_id = stor_device->path_id;
1265
1266 ret = vmbus_sendpacket(device->channel, vstor_packet,
1267 sizeof(struct vstor_packet),
1268 (unsigned long)&stor_device->reset_request,
1269 VM_PKT_DATA_INBAND,
1270 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1271 if (ret != 0)
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001272 return FAILED;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001273
K. Y. Srinivasan46d2eb62011-06-16 13:16:36 -07001274 t = wait_for_completion_timeout(&request->wait_event, 5*HZ);
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001275 if (t == 0)
1276 return TIMEOUT_ERROR;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001277
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001278
1279 /*
1280 * At this point, all outstanding requests in the adapter
1281 * should have been flushed out and return to us
K. Y. Srinivasan5c1b10a2012-10-02 11:03:31 -07001282 * There is a potential race here where the host may be in
1283 * the process of responding when we return from here.
1284 * Just wait for all in-transit packets to be accounted for
1285 * before we return from here.
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001286 */
K. Y. Srinivasan5c1b10a2012-10-02 11:03:31 -07001287 storvsc_wait_to_drain(stor_device);
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001288
K. Y. Srinivasana00e8222011-11-08 09:01:43 -08001289 return SUCCESS;
K. Y. Srinivasanaa3d7892011-05-10 07:54:37 -07001290}
1291
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001292static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd)
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001293{
1294 bool allowed = true;
1295 u8 scsi_op = scmnd->cmnd[0];
1296
1297 switch (scsi_op) {
Olaf Hering3e8f4f42013-02-21 12:04:51 -08001298 /* the host does not handle WRITE_SAME, log accident usage */
1299 case WRITE_SAME:
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001300 /*
1301 * smartd sends this command and the host does not handle
1302 * this. So, don't send it.
1303 */
K. Y. Srinivasan41098f82011-10-14 21:31:57 -07001304 case SET_WINDOW:
K. Y. Srinivasan59e00e72011-11-08 09:01:42 -08001305 scmnd->result = ILLEGAL_REQUEST << 16;
K. Y. Srinivasan41098f82011-10-14 21:31:57 -07001306 allowed = false;
1307 break;
1308 default:
1309 break;
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001310 }
1311 return allowed;
1312}
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001313
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001314static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001315{
1316 int ret;
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001317 struct hv_host_device *host_dev = shost_priv(host);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001318 struct hv_device *dev = host_dev->dev;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001319 struct storvsc_cmd_request *cmd_request;
1320 unsigned int request_size = 0;
1321 int i;
1322 struct scatterlist *sgl;
1323 unsigned int sg_count = 0;
1324 struct vmscsi_request *vm_srb;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001325 struct stor_mem_pools *memp = scmnd->device->hostdata;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001326
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001327 if (!storvsc_scsi_cmd_ok(scmnd)) {
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001328 scmnd->scsi_done(scmnd);
Olaf Hering92ae4eb2011-10-10 09:37:39 +02001329 return 0;
1330 }
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001331
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001332 request_size = sizeof(struct storvsc_cmd_request);
1333
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001334 cmd_request = mempool_alloc(memp->request_mempool,
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001335 GFP_ATOMIC);
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001336
1337 /*
1338 * We might be invoked in an interrupt context; hence
1339 * mempool_alloc() can fail.
1340 */
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001341 if (!cmd_request)
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001342 return SCSI_MLQUEUE_DEVICE_BUSY;
K. Y. Srinivasanbab445e2011-11-08 09:01:45 -08001343
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001344 memset(cmd_request, 0, sizeof(struct storvsc_cmd_request));
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001345
1346 /* Setup the cmd request */
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001347 cmd_request->cmd = scmnd;
1348
1349 scmnd->host_scribble = (unsigned char *)cmd_request;
1350
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001351 vm_srb = &cmd_request->vstor_packet.vm_srb;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001352
1353
1354 /* Build the SRB */
1355 switch (scmnd->sc_data_direction) {
1356 case DMA_TO_DEVICE:
1357 vm_srb->data_in = WRITE_TYPE;
1358 break;
1359 case DMA_FROM_DEVICE:
1360 vm_srb->data_in = READ_TYPE;
1361 break;
1362 default:
1363 vm_srb->data_in = UNKNOWN_TYPE;
1364 break;
1365 }
1366
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001367
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001368 vm_srb->port_number = host_dev->port;
1369 vm_srb->path_id = scmnd->device->channel;
1370 vm_srb->target_id = scmnd->device->id;
1371 vm_srb->lun = scmnd->device->lun;
1372
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001373 vm_srb->cdb_length = scmnd->cmd_len;
1374
1375 memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
1376
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001377 cmd_request->sense_buffer = scmnd->sense_buffer;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001378
1379
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001380 cmd_request->data_buffer.len = scsi_bufflen(scmnd);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001381 if (scsi_sg_count(scmnd)) {
1382 sgl = (struct scatterlist *)scsi_sglist(scmnd);
1383 sg_count = scsi_sg_count(scmnd);
1384
1385 /* check if we need to bounce the sgl */
1386 if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) {
1387 cmd_request->bounce_sgl =
1388 create_bounce_buffer(sgl, scsi_sg_count(scmnd),
K. Y. Srinivasan6e8087a2011-12-07 07:15:52 -08001389 scsi_bufflen(scmnd),
1390 vm_srb->data_in);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001391 if (!cmd_request->bounce_sgl) {
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001392 ret = SCSI_MLQUEUE_HOST_BUSY;
1393 goto queue_error;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001394 }
1395
1396 cmd_request->bounce_sgl_count =
1397 ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >>
1398 PAGE_SHIFT;
1399
K. Y. Srinivasanfa23b8c2011-08-27 11:31:21 -07001400 if (vm_srb->data_in == WRITE_TYPE)
1401 copy_to_bounce_buffer(sgl,
1402 cmd_request->bounce_sgl,
1403 scsi_sg_count(scmnd));
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001404
1405 sgl = cmd_request->bounce_sgl;
1406 sg_count = cmd_request->bounce_sgl_count;
1407 }
1408
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001409 cmd_request->data_buffer.offset = sgl[0].offset;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001410
1411 for (i = 0; i < sg_count; i++)
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001412 cmd_request->data_buffer.pfn_array[i] =
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001413 page_to_pfn(sg_page((&sgl[i])));
1414
1415 } else if (scsi_sglist(scmnd)) {
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001416 cmd_request->data_buffer.offset =
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001417 virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1);
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001418 cmd_request->data_buffer.pfn_array[0] =
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001419 virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT;
1420 }
1421
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001422 /* Invokes the vsc to start an IO */
K. Y. Srinivasan61eaffc2012-01-12 12:38:07 -08001423 ret = storvsc_do_io(dev, cmd_request);
K. Y. Srinivasan636f0fd2011-05-10 07:54:50 -07001424
K. Y. Srinivasand2598f02011-08-25 09:48:58 -07001425 if (ret == -EAGAIN) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001426 /* no more space */
1427
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001428 if (cmd_request->bounce_sgl_count) {
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001429 destroy_bounce_buffer(cmd_request->bounce_sgl,
K. Y. Srinivasan70691ec2011-08-27 11:31:29 -07001430 cmd_request->bounce_sgl_count);
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001431
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001432 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1433 goto queue_error;
1434 }
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001435 }
1436
K. Y. Srinivasanc77b63b2012-01-12 12:37:56 -08001437 return 0;
1438
1439queue_error:
1440 mempool_free(cmd_request, memp->request_mempool);
1441 scmnd->host_scribble = NULL;
K. Y. Srinivasanc5b463a2011-05-10 07:54:42 -07001442 return ret;
1443}
1444
Hank Janssenbef4a342009-07-13 16:01:31 -07001445static struct scsi_host_template scsi_driver = {
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001446 .module = THIS_MODULE,
1447 .name = "storvsc_host_t",
1448 .bios_param = storvsc_get_chs,
1449 .queuecommand = storvsc_queuecommand,
1450 .eh_host_reset_handler = storvsc_host_reset_handler,
1451 .slave_alloc = storvsc_device_alloc,
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001452 .slave_destroy = storvsc_device_destroy,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001453 .slave_configure = storvsc_device_configure,
1454 .cmd_per_lun = 1,
1455 /* 64 max_queue * 1 target */
Lars Lindley0686e4f2010-03-11 23:51:23 +01001456 .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS,
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001457 .this_id = -1,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001458 /* no use setting to 0 since ll_blk_rw reset it to 1 */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001459 /* currently 32 */
1460 .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT,
K. Y. Srinivasan039db522011-12-01 04:59:16 -08001461 .use_clustering = DISABLE_CLUSTERING,
Bill Pemberton454f18a2009-07-27 16:47:24 -04001462 /* Make sure we dont get a sg segment crosses a page boundary */
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001463 .dma_boundary = PAGE_SIZE-1,
Hank Janssenbef4a342009-07-13 16:01:31 -07001464};
1465
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001466enum {
1467 SCSI_GUID,
1468 IDE_GUID,
1469};
1470
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001471static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001472 /* SCSI guid */
K. Y. Srinivasan35c3bc22013-01-23 17:42:43 -08001473 { HV_SCSI_GUID,
1474 .driver_data = SCSI_GUID
1475 },
K. Y. Srinivasan21e37742011-08-27 11:31:18 -07001476 /* IDE guid */
K. Y. Srinivasan35c3bc22013-01-23 17:42:43 -08001477 { HV_IDE_GUID,
1478 .driver_data = IDE_GUID
1479 },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -07001480 { },
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001481};
Hank Janssenbef4a342009-07-13 16:01:31 -07001482
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001483MODULE_DEVICE_TABLE(vmbus, id_table);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001484
K. Y. Srinivasan84946892011-09-13 10:59:38 -07001485static int storvsc_probe(struct hv_device *device,
1486 const struct hv_vmbus_device_id *dev_id)
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001487{
1488 int ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001489 struct Scsi_Host *host;
1490 struct hv_host_device *host_dev;
K. Y. Srinivasanef52a812011-09-13 10:59:39 -07001491 bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001492 int target = 0;
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001493 struct storvsc_device *stor_device;
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001494
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001495 host = scsi_host_alloc(&scsi_driver,
1496 sizeof(struct hv_host_device));
1497 if (!host)
1498 return -ENOMEM;
1499
K. Y. Srinivasan7f33f302011-11-08 09:01:44 -08001500 host_dev = shost_priv(host);
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001501 memset(host_dev, 0, sizeof(struct hv_host_device));
1502
1503 host_dev->port = host->host_no;
1504 host_dev->dev = device;
1505
K. Y. Srinivasan4e03e692011-11-08 09:01:40 -08001506
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001507 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001508 if (!stor_device) {
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001509 ret = -ENOMEM;
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001510 goto err_out0;
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001511 }
1512
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001513 stor_device->destroy = false;
1514 init_waitqueue_head(&stor_device->waiting_to_drain);
1515 stor_device->device = device;
K. Y. Srinivasancd654ea2011-09-13 10:59:48 -07001516 stor_device->host = host;
1517 hv_set_drvdata(device, stor_device);
K. Y. Srinivasana13d35a2011-09-13 10:59:46 -07001518
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001519 stor_device->port_number = host->host_no;
1520 ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001521 if (ret)
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001522 goto err_out1;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001523
K. Y. Srinivasan6e4198c2011-09-13 10:59:45 -07001524 host_dev->path = stor_device->path_id;
1525 host_dev->target = stor_device->target_id;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001526
1527 /* max # of devices per target */
1528 host->max_lun = STORVSC_MAX_LUNS_PER_TARGET;
1529 /* max # of targets per channel */
1530 host->max_id = STORVSC_MAX_TARGETS;
1531 /* max # of channels */
1532 host->max_channel = STORVSC_MAX_CHANNELS - 1;
Mike Sterlingcf55f4a2011-09-06 16:10:55 -07001533 /* max cmd length */
1534 host->max_cmd_len = STORVSC_MAX_CMD_LEN;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001535
1536 /* Register the HBA and start the scsi bus scan */
1537 ret = scsi_add_host(host, &device->device);
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001538 if (ret != 0)
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001539 goto err_out2;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001540
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001541 if (!dev_is_ide) {
1542 scsi_scan_host(host);
K. Y. Srinivasan59d22952012-01-12 12:37:55 -08001543 } else {
1544 target = (device->dev_instance.b[5] << 8 |
1545 device->dev_instance.b[4]);
1546 ret = scsi_add_device(host, 0, target, 0);
1547 if (ret) {
1548 scsi_remove_host(host);
1549 goto err_out2;
1550 }
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001551 }
1552 return 0;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001553
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001554err_out2:
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001555 /*
1556 * Once we have connected with the host, we would need to
1557 * to invoke storvsc_dev_remove() to rollback this state and
1558 * this call also frees up the stor_device; hence the jump around
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001559 * err_out1 label.
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001560 */
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001561 storvsc_dev_remove(device);
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001562 goto err_out0;
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001563
1564err_out1:
K. Y. Srinivasance3e3012011-12-01 04:59:20 -08001565 kfree(stor_device);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001566
1567err_out0:
K. Y. Srinivasanbd1f5d62011-08-27 11:31:17 -07001568 scsi_host_put(host);
K. Y. Srinivasan225ce6e2011-11-08 09:01:41 -08001569 return ret;
K. Y. Srinivasanf5c78872011-05-10 07:54:43 -07001570}
1571
K. Y. Srinivasanddcbf652012-01-12 12:37:59 -08001572static int storvsc_remove(struct hv_device *dev)
1573{
1574 struct storvsc_device *stor_device = hv_get_drvdata(dev);
1575 struct Scsi_Host *host = stor_device->host;
1576
1577 scsi_remove_host(host);
1578 storvsc_dev_remove(dev);
1579 scsi_host_put(host);
1580
1581 return 0;
1582}
1583
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001584static struct hv_driver storvsc_drv = {
K. Y. Srinivasanfafb0ef2011-11-08 09:01:46 -08001585 .name = KBUILD_MODNAME,
K. Y. Srinivasand847b5f2011-08-25 09:48:33 -07001586 .id_table = id_table,
K. Y. Srinivasan40bf63e2011-05-10 07:56:09 -07001587 .probe = storvsc_probe,
1588 .remove = storvsc_remove,
K. Y. Srinivasan39ae6fa2011-05-10 07:54:46 -07001589};
K. Y. Srinivasan7bd05b92011-05-10 07:54:45 -07001590
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001591static int __init storvsc_drv_init(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001592{
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001593 u32 max_outstanding_req_per_channel;
1594
1595 /*
1596 * Divide the ring buffer data size (which is 1 page less
1597 * than the ring buffer size since that page is reserved for
1598 * the ring buffer indices) by the max request size (which is
1599 * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64)
1600 */
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001601 max_outstanding_req_per_channel =
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001602 ((storvsc_ringbuffer_size - PAGE_SIZE) /
1603 ALIGN(MAX_MULTIPAGE_BUFFER_PACKET +
1604 sizeof(struct vstor_packet) + sizeof(u64),
1605 sizeof(u64)));
Hank Janssenbef4a342009-07-13 16:01:31 -07001606
K. Y. Srinivasan01415ab32011-05-10 07:55:58 -07001607 if (max_outstanding_req_per_channel <
K. Y. Srinivasanf8feed02011-05-10 07:54:24 -07001608 STORVSC_MAX_IO_REQUESTS)
K. Y. Srinivasanb06efc12011-08-25 09:49:10 -07001609 return -EINVAL;
Hank Janssenbef4a342009-07-13 16:01:31 -07001610
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001611 return vmbus_driver_register(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001612}
1613
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001614static void __exit storvsc_drv_exit(void)
Hank Janssenbef4a342009-07-13 16:01:31 -07001615{
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -07001616 vmbus_driver_unregister(&storvsc_drv);
Hank Janssenbef4a342009-07-13 16:01:31 -07001617}
1618
Greg Kroah-Hartmanff568d32009-09-02 08:37:47 -07001619MODULE_LICENSE("GPL");
Hank Janssen26c14cc2010-02-11 23:02:42 +00001620MODULE_VERSION(HV_DRV_VERSION);
Stephen Hemminger3afc7cc32010-05-06 21:44:45 -07001621MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver");
K. Y. Srinivasand9bbae82011-06-06 15:49:27 -07001622module_init(storvsc_drv_init);
K. Y. Srinivasanc63ba9e2011-06-06 15:49:26 -07001623module_exit(storvsc_drv_exit);