Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1 | /* |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 2 | * Copyright (c) 2009, Microsoft Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 15 | * Place - Suite 330, Boston, MA 02111-1307 USA. |
| 16 | * |
| 17 | * Authors: |
| 18 | * Haiyang Zhang <haiyangz@microsoft.com> |
| 19 | * Hank Janssen <hjanssen@microsoft.com> |
K. Y. Srinivasan | 972621c | 2011-05-10 07:54:19 -0700 | [diff] [blame] | 20 | * K. Y. Srinivasan <kys@microsoft.com> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 21 | */ |
K. Y. Srinivasan | a1be170 | 2011-08-27 11:31:23 -0700 | [diff] [blame] | 22 | |
| 23 | #include <linux/kernel.h> |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 24 | #include <linux/wait.h> |
K. Y. Srinivasan | a1be170 | 2011-08-27 11:31:23 -0700 | [diff] [blame] | 25 | #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 Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 30 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/slab.h> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 32 | #include <linux/module.h> |
| 33 | #include <linux/device.h> |
Greg Kroah-Hartman | 46a9719 | 2011-10-04 12:29:52 -0700 | [diff] [blame] | 34 | #include <linux/hyperv.h> |
K. Y. Srinivasan | 4e03e69 | 2011-11-08 09:01:40 -0800 | [diff] [blame] | 35 | #include <linux/mempool.h> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 36 | #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 Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 43 | #include <scsi/scsi_dbg.h> |
K. Y. Srinivasan | 3f335ea | 2011-05-12 19:34:15 -0700 | [diff] [blame] | 44 | |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 45 | |
K. Y. Srinivasan | 4e03e69 | 2011-11-08 09:01:40 -0800 | [diff] [blame] | 46 | #define STORVSC_MIN_BUF_NR 64 |
K. Y. Srinivasan | c1b3d06 | 2011-08-27 11:31:25 -0700 | [diff] [blame] | 47 | #define STORVSC_RING_BUFFER_SIZE (20*PAGE_SIZE) |
| 48 | static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE; |
| 49 | |
| 50 | module_param(storvsc_ringbuffer_size, int, S_IRUGO); |
| 51 | MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)"); |
| 52 | |
K. Y. Srinivasan | 09f0355 | 2012-01-12 12:37:54 -0800 | [diff] [blame] | 53 | /* |
| 54 | * To alert the user that structure sizes may be mismatched even though the |
| 55 | * protocol versions match. |
| 56 | */ |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 57 | |
| 58 | |
| 59 | #define REVISION_STRING(REVISION_) #REVISION_ |
| 60 | #define FILL_VMSTOR_REVISION(RESULT_LVALUE_) \ |
| 61 | do { \ |
| 62 | char *revision_string \ |
| 63 | = REVISION_STRING($Rev : 6 $) + 6; \ |
| 64 | RESULT_LVALUE_ = 0; \ |
| 65 | while (*revision_string >= '0' \ |
| 66 | && *revision_string <= '9') { \ |
| 67 | RESULT_LVALUE_ *= 10; \ |
| 68 | RESULT_LVALUE_ += *revision_string - '0'; \ |
| 69 | revision_string++; \ |
| 70 | } \ |
| 71 | } while (0) |
| 72 | |
K. Y. Srinivasan | 09f0355 | 2012-01-12 12:37:54 -0800 | [diff] [blame] | 73 | /* |
| 74 | * Major/minor macros. Minor version is in LSB, meaning that earlier flat |
| 75 | * version numbers will be interpreted as "0.x" (i.e., 1 becomes 0.1). |
| 76 | */ |
| 77 | |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 78 | #define VMSTOR_PROTOCOL_MAJOR(VERSION_) (((VERSION_) >> 8) & 0xff) |
| 79 | #define VMSTOR_PROTOCOL_MINOR(VERSION_) (((VERSION_)) & 0xff) |
| 80 | #define VMSTOR_PROTOCOL_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \ |
| 81 | (((MINOR_) & 0xff))) |
| 82 | #define VMSTOR_INVALID_PROTOCOL_VERSION (-1) |
| 83 | |
K. Y. Srinivasan | 09f0355 | 2012-01-12 12:37:54 -0800 | [diff] [blame] | 84 | /* |
| 85 | * Version history: |
| 86 | * V1 Beta: 0.1 |
| 87 | * V1 RC < 2008/1/31: 1.0 |
| 88 | * V1 RC > 2008/1/31: 2.0 |
| 89 | * Win7: 4.2 |
| 90 | */ |
| 91 | |
K. Y. Srinivasan | 2b9525f | 2011-11-08 09:01:48 -0800 | [diff] [blame] | 92 | #define VMSTOR_PROTOCOL_VERSION_CURRENT VMSTOR_PROTOCOL_VERSION(4, 2) |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 93 | |
| 94 | |
| 95 | |
| 96 | |
K. Y. Srinivasan | 09f0355 | 2012-01-12 12:37:54 -0800 | [diff] [blame] | 97 | /* |
| 98 | * This will get replaced with the max transfer length that is possible on |
| 99 | * the host adapter. |
| 100 | * The max transfer length will be published when we offer a vmbus channel. |
| 101 | */ |
| 102 | |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 103 | #define MAX_TRANSFER_LENGTH 0x40000 |
| 104 | #define DEFAULT_PACKET_SIZE (sizeof(struct vmdata_gpa_direct) + \ |
| 105 | sizeof(struct vstor_packet) + \ |
| 106 | sizesizeof(u64) * (MAX_TRANSFER_LENGTH / PAGE_SIZE))) |
| 107 | |
| 108 | |
| 109 | /* Packet structure describing virtual storage requests. */ |
| 110 | enum vstor_packet_operation { |
| 111 | VSTOR_OPERATION_COMPLETE_IO = 1, |
| 112 | VSTOR_OPERATION_REMOVE_DEVICE = 2, |
| 113 | VSTOR_OPERATION_EXECUTE_SRB = 3, |
| 114 | VSTOR_OPERATION_RESET_LUN = 4, |
| 115 | VSTOR_OPERATION_RESET_ADAPTER = 5, |
| 116 | VSTOR_OPERATION_RESET_BUS = 6, |
| 117 | VSTOR_OPERATION_BEGIN_INITIALIZATION = 7, |
| 118 | VSTOR_OPERATION_END_INITIALIZATION = 8, |
| 119 | VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9, |
| 120 | VSTOR_OPERATION_QUERY_PROPERTIES = 10, |
K. Y. Srinivasan | 2b9525f | 2011-11-08 09:01:48 -0800 | [diff] [blame] | 121 | VSTOR_OPERATION_ENUMERATE_BUS = 11, |
| 122 | VSTOR_OPERATION_MAXIMUM = 11 |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | /* |
| 126 | * Platform neutral description of a scsi request - |
| 127 | * this remains the same across the write regardless of 32/64 bit |
| 128 | * note: it's patterned off the SCSI_PASS_THROUGH structure |
| 129 | */ |
| 130 | #define CDB16GENERIC_LENGTH 0x10 |
| 131 | |
| 132 | #ifndef SENSE_BUFFER_SIZE |
| 133 | #define SENSE_BUFFER_SIZE 0x12 |
| 134 | #endif |
| 135 | |
| 136 | #define MAX_DATA_BUF_LEN_WITH_PADDING 0x14 |
| 137 | |
| 138 | struct vmscsi_request { |
| 139 | unsigned short length; |
| 140 | unsigned char srb_status; |
| 141 | unsigned char scsi_status; |
| 142 | |
| 143 | unsigned char port_number; |
| 144 | unsigned char path_id; |
| 145 | unsigned char target_id; |
| 146 | unsigned char lun; |
| 147 | |
| 148 | unsigned char cdb_length; |
| 149 | unsigned char sense_info_length; |
| 150 | unsigned char data_in; |
| 151 | unsigned char reserved; |
| 152 | |
| 153 | unsigned int data_transfer_length; |
| 154 | |
| 155 | union { |
| 156 | unsigned char cdb[CDB16GENERIC_LENGTH]; |
| 157 | unsigned char sense_data[SENSE_BUFFER_SIZE]; |
| 158 | unsigned char reserved_array[MAX_DATA_BUF_LEN_WITH_PADDING]; |
| 159 | }; |
| 160 | } __attribute((packed)); |
| 161 | |
| 162 | |
| 163 | /* |
| 164 | * This structure is sent during the intialization phase to get the different |
| 165 | * properties of the channel. |
| 166 | */ |
| 167 | struct vmstorage_channel_properties { |
| 168 | unsigned short protocol_version; |
| 169 | unsigned char path_id; |
| 170 | unsigned char target_id; |
| 171 | |
| 172 | /* Note: port number is only really known on the client side */ |
| 173 | unsigned int port_number; |
| 174 | unsigned int flags; |
| 175 | unsigned int max_transfer_bytes; |
| 176 | |
| 177 | /* This id is unique for each channel and will correspond with */ |
| 178 | /* vendor specific data in the inquirydata */ |
| 179 | unsigned long long unique_id; |
| 180 | } __packed; |
| 181 | |
| 182 | /* This structure is sent during the storage protocol negotiations. */ |
| 183 | struct vmstorage_protocol_version { |
| 184 | /* Major (MSW) and minor (LSW) version numbers. */ |
| 185 | unsigned short major_minor; |
| 186 | |
| 187 | /* |
| 188 | * Revision number is auto-incremented whenever this file is changed |
| 189 | * (See FILL_VMSTOR_REVISION macro above). Mismatch does not |
| 190 | * definitely indicate incompatibility--but it does indicate mismatched |
| 191 | * builds. |
| 192 | */ |
| 193 | unsigned short revision; |
| 194 | } __packed; |
| 195 | |
| 196 | /* Channel Property Flags */ |
| 197 | #define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1 |
| 198 | #define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2 |
| 199 | |
| 200 | struct vstor_packet { |
| 201 | /* Requested operation type */ |
| 202 | enum vstor_packet_operation operation; |
| 203 | |
| 204 | /* Flags - see below for values */ |
| 205 | unsigned int flags; |
| 206 | |
| 207 | /* Status of the request returned from the server side. */ |
| 208 | unsigned int status; |
| 209 | |
| 210 | /* Data payload area */ |
| 211 | union { |
| 212 | /* |
| 213 | * Structure used to forward SCSI commands from the |
| 214 | * client to the server. |
| 215 | */ |
| 216 | struct vmscsi_request vm_srb; |
| 217 | |
| 218 | /* Structure used to query channel properties. */ |
| 219 | struct vmstorage_channel_properties storage_channel_properties; |
| 220 | |
| 221 | /* Used during version negotiations. */ |
| 222 | struct vmstorage_protocol_version version; |
| 223 | }; |
| 224 | } __packed; |
| 225 | |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 226 | /* |
K. Y. Srinivasan | 09f0355 | 2012-01-12 12:37:54 -0800 | [diff] [blame] | 227 | * Packet Flags: |
| 228 | * |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 229 | * This flag indicates that the server should send back a completion for this |
| 230 | * packet. |
| 231 | */ |
K. Y. Srinivasan | 09f0355 | 2012-01-12 12:37:54 -0800 | [diff] [blame] | 232 | |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 233 | #define REQUEST_COMPLETION_FLAG 0x1 |
| 234 | |
| 235 | /* This is the set of flags that the vsc can set in any packets it sends */ |
| 236 | #define VSC_LEGAL_FLAGS (REQUEST_COMPLETION_FLAG) |
| 237 | |
| 238 | |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 239 | |
| 240 | #define STORVSC_MAX_IO_REQUESTS 128 |
| 241 | |
| 242 | /* |
| 243 | * In Hyper-V, each port/path/target maps to 1 scsi host adapter. In |
| 244 | * reality, the path/target is not used (ie always set to 0) so our |
| 245 | * scsi host adapter essentially has 1 bus with 1 target that contains |
| 246 | * up to 256 luns. |
| 247 | */ |
| 248 | #define STORVSC_MAX_LUNS_PER_TARGET 64 |
| 249 | #define STORVSC_MAX_TARGETS 1 |
| 250 | #define STORVSC_MAX_CHANNELS 1 |
Mike Sterling | cf55f4a | 2011-09-06 16:10:55 -0700 | [diff] [blame] | 251 | #define STORVSC_MAX_CMD_LEN 16 |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 252 | |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 253 | /* Matches Windows-end */ |
| 254 | enum storvsc_request_type { |
| 255 | WRITE_TYPE, |
| 256 | READ_TYPE, |
| 257 | UNKNOWN_TYPE, |
| 258 | }; |
| 259 | |
| 260 | |
| 261 | struct hv_storvsc_request { |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 262 | struct hv_device *device; |
| 263 | |
| 264 | /* Synchronize the request/response if needed */ |
| 265 | struct completion wait_event; |
| 266 | |
| 267 | unsigned char *sense_buffer; |
| 268 | void *context; |
| 269 | void (*on_io_completion)(struct hv_storvsc_request *request); |
| 270 | struct hv_multipage_buffer data_buffer; |
| 271 | |
| 272 | struct vstor_packet vstor_packet; |
| 273 | }; |
| 274 | |
| 275 | |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 276 | /* A storvsc device is a device object that contains a vmbus channel */ |
| 277 | struct storvsc_device { |
| 278 | struct hv_device *device; |
| 279 | |
| 280 | bool destroy; |
| 281 | bool drain_notify; |
| 282 | atomic_t num_outstanding_req; |
K. Y. Srinivasan | cd654ea | 2011-09-13 10:59:48 -0700 | [diff] [blame] | 283 | struct Scsi_Host *host; |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 284 | |
| 285 | wait_queue_head_t waiting_to_drain; |
| 286 | |
| 287 | /* |
| 288 | * Each unique Port/Path/Target represents 1 channel ie scsi |
| 289 | * controller. In reality, the pathid, targetid is always 0 |
| 290 | * and the port is set by us |
| 291 | */ |
| 292 | unsigned int port_number; |
| 293 | unsigned char path_id; |
| 294 | unsigned char target_id; |
| 295 | |
| 296 | /* Used for vsc/vsp channel reset process */ |
| 297 | struct hv_storvsc_request init_request; |
| 298 | struct hv_storvsc_request reset_request; |
| 299 | }; |
| 300 | |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 301 | struct stor_mem_pools { |
K. Y. Srinivasan | c1b3d06 | 2011-08-27 11:31:25 -0700 | [diff] [blame] | 302 | struct kmem_cache *request_pool; |
K. Y. Srinivasan | 4e03e69 | 2011-11-08 09:01:40 -0800 | [diff] [blame] | 303 | mempool_t *request_mempool; |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 304 | }; |
| 305 | |
| 306 | struct hv_host_device { |
| 307 | struct hv_device *dev; |
K. Y. Srinivasan | c1b3d06 | 2011-08-27 11:31:25 -0700 | [diff] [blame] | 308 | unsigned int port; |
| 309 | unsigned char path; |
| 310 | unsigned char target; |
| 311 | }; |
| 312 | |
| 313 | struct storvsc_cmd_request { |
| 314 | struct list_head entry; |
| 315 | struct scsi_cmnd *cmd; |
| 316 | |
| 317 | unsigned int bounce_sgl_count; |
| 318 | struct scatterlist *bounce_sgl; |
| 319 | |
| 320 | struct hv_storvsc_request request; |
| 321 | }; |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 322 | |
K. Y. Srinivasan | 1267579 | 2011-11-08 09:01:49 -0800 | [diff] [blame] | 323 | struct storvsc_scan_work { |
| 324 | struct work_struct work; |
| 325 | struct Scsi_Host *host; |
| 326 | uint lun; |
| 327 | }; |
| 328 | |
| 329 | static void storvsc_bus_scan(struct work_struct *work) |
| 330 | { |
| 331 | struct storvsc_scan_work *wrk; |
| 332 | int id, order_id; |
| 333 | |
| 334 | wrk = container_of(work, struct storvsc_scan_work, work); |
| 335 | for (id = 0; id < wrk->host->max_id; ++id) { |
| 336 | if (wrk->host->reverse_ordering) |
| 337 | order_id = wrk->host->max_id - id - 1; |
| 338 | else |
| 339 | order_id = id; |
| 340 | |
| 341 | scsi_scan_target(&wrk->host->shost_gendev, 0, |
| 342 | order_id, SCAN_WILD_CARD, 1); |
| 343 | } |
| 344 | kfree(wrk); |
| 345 | } |
| 346 | |
K. Y. Srinivasan | b401731 | 2011-11-08 09:01:50 -0800 | [diff] [blame] | 347 | static void storvsc_remove_lun(struct work_struct *work) |
| 348 | { |
| 349 | struct storvsc_scan_work *wrk; |
| 350 | struct scsi_device *sdev; |
| 351 | |
| 352 | wrk = container_of(work, struct storvsc_scan_work, work); |
| 353 | if (!scsi_host_get(wrk->host)) |
| 354 | goto done; |
| 355 | |
| 356 | sdev = scsi_device_lookup(wrk->host, 0, 0, wrk->lun); |
| 357 | |
| 358 | if (sdev) { |
| 359 | scsi_remove_device(sdev); |
| 360 | scsi_device_put(sdev); |
| 361 | } |
| 362 | scsi_host_put(wrk->host); |
| 363 | |
| 364 | done: |
| 365 | kfree(wrk); |
| 366 | } |
| 367 | |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 368 | static inline struct storvsc_device *get_out_stor_device( |
| 369 | struct hv_device *device) |
| 370 | { |
| 371 | struct storvsc_device *stor_device; |
| 372 | |
K. Y. Srinivasan | cd654ea | 2011-09-13 10:59:48 -0700 | [diff] [blame] | 373 | stor_device = hv_get_drvdata(device); |
K. Y. Srinivasan | f0d79fe | 2011-08-27 11:31:24 -0700 | [diff] [blame] | 374 | |
| 375 | if (stor_device && stor_device->destroy) |
| 376 | stor_device = NULL; |
| 377 | |
| 378 | return stor_device; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | static inline void storvsc_wait_to_drain(struct storvsc_device *dev) |
| 383 | { |
| 384 | dev->drain_notify = true; |
| 385 | wait_event(dev->waiting_to_drain, |
| 386 | atomic_read(&dev->num_outstanding_req) == 0); |
| 387 | dev->drain_notify = false; |
| 388 | } |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 389 | |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 390 | static inline struct storvsc_device *get_in_stor_device( |
| 391 | struct hv_device *device) |
| 392 | { |
| 393 | struct storvsc_device *stor_device; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 394 | |
K. Y. Srinivasan | cd654ea | 2011-09-13 10:59:48 -0700 | [diff] [blame] | 395 | stor_device = hv_get_drvdata(device); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 396 | |
| 397 | if (!stor_device) |
| 398 | goto get_in_err; |
| 399 | |
| 400 | /* |
| 401 | * If the device is being destroyed; allow incoming |
| 402 | * traffic only to cleanup outstanding requests. |
| 403 | */ |
| 404 | |
| 405 | if (stor_device->destroy && |
| 406 | (atomic_read(&stor_device->num_outstanding_req) == 0)) |
| 407 | stor_device = NULL; |
| 408 | |
| 409 | get_in_err: |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 410 | return stor_device; |
| 411 | |
| 412 | } |
| 413 | |
| 414 | static int storvsc_channel_init(struct hv_device *device) |
| 415 | { |
| 416 | struct storvsc_device *stor_device; |
| 417 | struct hv_storvsc_request *request; |
| 418 | struct vstor_packet *vstor_packet; |
| 419 | int ret, t; |
| 420 | |
| 421 | stor_device = get_out_stor_device(device); |
| 422 | if (!stor_device) |
| 423 | return -ENODEV; |
| 424 | |
| 425 | request = &stor_device->init_request; |
| 426 | vstor_packet = &request->vstor_packet; |
| 427 | |
| 428 | /* |
| 429 | * Now, initiate the vsc/vsp initialization protocol on the open |
| 430 | * channel |
| 431 | */ |
| 432 | memset(request, 0, sizeof(struct hv_storvsc_request)); |
| 433 | init_completion(&request->wait_event); |
| 434 | vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION; |
| 435 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; |
| 436 | |
| 437 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
| 438 | sizeof(struct vstor_packet), |
| 439 | (unsigned long)request, |
| 440 | VM_PKT_DATA_INBAND, |
| 441 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 442 | if (ret != 0) |
| 443 | goto cleanup; |
| 444 | |
| 445 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); |
| 446 | if (t == 0) { |
| 447 | ret = -ETIMEDOUT; |
| 448 | goto cleanup; |
| 449 | } |
| 450 | |
| 451 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || |
| 452 | vstor_packet->status != 0) |
| 453 | goto cleanup; |
| 454 | |
| 455 | |
| 456 | /* reuse the packet for version range supported */ |
| 457 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); |
| 458 | vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION; |
| 459 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; |
| 460 | |
| 461 | vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT; |
| 462 | FILL_VMSTOR_REVISION(vstor_packet->version.revision); |
| 463 | |
| 464 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
| 465 | sizeof(struct vstor_packet), |
| 466 | (unsigned long)request, |
| 467 | VM_PKT_DATA_INBAND, |
| 468 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 469 | if (ret != 0) |
| 470 | goto cleanup; |
| 471 | |
| 472 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); |
| 473 | if (t == 0) { |
| 474 | ret = -ETIMEDOUT; |
| 475 | goto cleanup; |
| 476 | } |
| 477 | |
| 478 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || |
| 479 | vstor_packet->status != 0) |
| 480 | goto cleanup; |
| 481 | |
| 482 | |
| 483 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); |
| 484 | vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES; |
| 485 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; |
| 486 | vstor_packet->storage_channel_properties.port_number = |
| 487 | stor_device->port_number; |
| 488 | |
| 489 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
| 490 | sizeof(struct vstor_packet), |
| 491 | (unsigned long)request, |
| 492 | VM_PKT_DATA_INBAND, |
| 493 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 494 | |
| 495 | if (ret != 0) |
| 496 | goto cleanup; |
| 497 | |
| 498 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); |
| 499 | if (t == 0) { |
| 500 | ret = -ETIMEDOUT; |
| 501 | goto cleanup; |
| 502 | } |
| 503 | |
| 504 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || |
| 505 | vstor_packet->status != 0) |
| 506 | goto cleanup; |
| 507 | |
| 508 | stor_device->path_id = vstor_packet->storage_channel_properties.path_id; |
| 509 | stor_device->target_id |
| 510 | = vstor_packet->storage_channel_properties.target_id; |
| 511 | |
| 512 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); |
| 513 | vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION; |
| 514 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; |
| 515 | |
| 516 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
| 517 | sizeof(struct vstor_packet), |
| 518 | (unsigned long)request, |
| 519 | VM_PKT_DATA_INBAND, |
| 520 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 521 | |
| 522 | if (ret != 0) |
| 523 | goto cleanup; |
| 524 | |
| 525 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); |
| 526 | if (t == 0) { |
| 527 | ret = -ETIMEDOUT; |
| 528 | goto cleanup; |
| 529 | } |
| 530 | |
| 531 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || |
| 532 | vstor_packet->status != 0) |
| 533 | goto cleanup; |
| 534 | |
| 535 | |
| 536 | cleanup: |
| 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | static void storvsc_on_io_completion(struct hv_device *device, |
| 541 | struct vstor_packet *vstor_packet, |
| 542 | struct hv_storvsc_request *request) |
| 543 | { |
| 544 | struct storvsc_device *stor_device; |
| 545 | struct vstor_packet *stor_pkt; |
| 546 | |
K. Y. Srinivasan | cd654ea | 2011-09-13 10:59:48 -0700 | [diff] [blame] | 547 | stor_device = hv_get_drvdata(device); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 548 | stor_pkt = &request->vstor_packet; |
| 549 | |
K. Y. Srinivasan | 4ed51a2 | 2011-08-27 11:31:26 -0700 | [diff] [blame] | 550 | /* |
| 551 | * The current SCSI handling on the host side does |
| 552 | * not correctly handle: |
| 553 | * INQUIRY command with page code parameter set to 0x80 |
| 554 | * MODE_SENSE command with cmd[2] == 0x1c |
| 555 | * |
| 556 | * Setup srb and scsi status so this won't be fatal. |
| 557 | * We do this so we can distinguish truly fatal failues |
| 558 | * (srb status == 0x4) and off-line the device in that case. |
| 559 | */ |
| 560 | |
| 561 | if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) || |
| 562 | (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) { |
| 563 | vstor_packet->vm_srb.scsi_status = 0; |
| 564 | vstor_packet->vm_srb.srb_status = 0x1; |
| 565 | } |
| 566 | |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 567 | |
| 568 | /* Copy over the status...etc */ |
| 569 | stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status; |
| 570 | stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status; |
| 571 | stor_pkt->vm_srb.sense_info_length = |
| 572 | vstor_packet->vm_srb.sense_info_length; |
| 573 | |
| 574 | if (vstor_packet->vm_srb.scsi_status != 0 || |
| 575 | vstor_packet->vm_srb.srb_status != 1){ |
Greg Kroah-Hartman | d181daa | 2011-10-11 09:20:31 -0600 | [diff] [blame] | 576 | dev_warn(&device->device, |
| 577 | "cmd 0x%x scsi status 0x%x srb status 0x%x\n", |
| 578 | stor_pkt->vm_srb.cdb[0], |
| 579 | vstor_packet->vm_srb.scsi_status, |
| 580 | vstor_packet->vm_srb.srb_status); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) { |
| 584 | /* CHECK_CONDITION */ |
| 585 | if (vstor_packet->vm_srb.srb_status & 0x80) { |
| 586 | /* autosense data available */ |
Greg Kroah-Hartman | d181daa | 2011-10-11 09:20:31 -0600 | [diff] [blame] | 587 | dev_warn(&device->device, |
K. Y. Srinivasan | 41098f8 | 2011-10-14 21:31:57 -0700 | [diff] [blame] | 588 | "stor pkt %p autosense data valid - len %d\n", |
| 589 | request, |
| 590 | vstor_packet->vm_srb.sense_info_length); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 591 | |
| 592 | memcpy(request->sense_buffer, |
| 593 | vstor_packet->vm_srb.sense_data, |
| 594 | vstor_packet->vm_srb.sense_info_length); |
| 595 | |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | stor_pkt->vm_srb.data_transfer_length = |
| 600 | vstor_packet->vm_srb.data_transfer_length; |
| 601 | |
| 602 | request->on_io_completion(request); |
| 603 | |
| 604 | if (atomic_dec_and_test(&stor_device->num_outstanding_req) && |
| 605 | stor_device->drain_notify) |
| 606 | wake_up(&stor_device->waiting_to_drain); |
| 607 | |
| 608 | |
| 609 | } |
| 610 | |
| 611 | static void storvsc_on_receive(struct hv_device *device, |
| 612 | struct vstor_packet *vstor_packet, |
| 613 | struct hv_storvsc_request *request) |
| 614 | { |
K. Y. Srinivasan | 1267579 | 2011-11-08 09:01:49 -0800 | [diff] [blame] | 615 | struct storvsc_scan_work *work; |
| 616 | struct storvsc_device *stor_device; |
| 617 | |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 618 | switch (vstor_packet->operation) { |
| 619 | case VSTOR_OPERATION_COMPLETE_IO: |
| 620 | storvsc_on_io_completion(device, vstor_packet, request); |
| 621 | break; |
K. Y. Srinivasan | 1267579 | 2011-11-08 09:01:49 -0800 | [diff] [blame] | 622 | |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 623 | case VSTOR_OPERATION_REMOVE_DEVICE: |
K. Y. Srinivasan | 1267579 | 2011-11-08 09:01:49 -0800 | [diff] [blame] | 624 | case VSTOR_OPERATION_ENUMERATE_BUS: |
| 625 | stor_device = get_in_stor_device(device); |
| 626 | work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC); |
| 627 | if (!work) |
| 628 | return; |
| 629 | |
| 630 | INIT_WORK(&work->work, storvsc_bus_scan); |
| 631 | work->host = stor_device->host; |
| 632 | schedule_work(&work->work); |
| 633 | break; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 634 | |
| 635 | default: |
| 636 | break; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | static void storvsc_on_channel_callback(void *context) |
| 641 | { |
| 642 | struct hv_device *device = (struct hv_device *)context; |
| 643 | struct storvsc_device *stor_device; |
| 644 | u32 bytes_recvd; |
| 645 | u64 request_id; |
| 646 | unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)]; |
| 647 | struct hv_storvsc_request *request; |
| 648 | int ret; |
| 649 | |
| 650 | |
| 651 | stor_device = get_in_stor_device(device); |
| 652 | if (!stor_device) |
| 653 | return; |
| 654 | |
| 655 | do { |
| 656 | ret = vmbus_recvpacket(device->channel, packet, |
| 657 | ALIGN(sizeof(struct vstor_packet), 8), |
| 658 | &bytes_recvd, &request_id); |
| 659 | if (ret == 0 && bytes_recvd > 0) { |
| 660 | |
| 661 | request = (struct hv_storvsc_request *) |
| 662 | (unsigned long)request_id; |
| 663 | |
| 664 | if ((request == &stor_device->init_request) || |
| 665 | (request == &stor_device->reset_request)) { |
| 666 | |
| 667 | memcpy(&request->vstor_packet, packet, |
| 668 | sizeof(struct vstor_packet)); |
| 669 | complete(&request->wait_event); |
| 670 | } else { |
| 671 | storvsc_on_receive(device, |
| 672 | (struct vstor_packet *)packet, |
| 673 | request); |
| 674 | } |
| 675 | } else { |
| 676 | break; |
| 677 | } |
| 678 | } while (1); |
| 679 | |
| 680 | return; |
| 681 | } |
| 682 | |
| 683 | static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size) |
| 684 | { |
| 685 | struct vmstorage_channel_properties props; |
| 686 | int ret; |
| 687 | |
| 688 | memset(&props, 0, sizeof(struct vmstorage_channel_properties)); |
| 689 | |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 690 | ret = vmbus_open(device->channel, |
| 691 | ring_size, |
| 692 | ring_size, |
| 693 | (void *)&props, |
| 694 | sizeof(struct vmstorage_channel_properties), |
| 695 | storvsc_on_channel_callback, device); |
| 696 | |
| 697 | if (ret != 0) |
| 698 | return ret; |
| 699 | |
| 700 | ret = storvsc_channel_init(device); |
| 701 | |
| 702 | return ret; |
| 703 | } |
| 704 | |
K. Y. Srinivasan | c1b3d06 | 2011-08-27 11:31:25 -0700 | [diff] [blame] | 705 | static int storvsc_dev_remove(struct hv_device *device) |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 706 | { |
| 707 | struct storvsc_device *stor_device; |
| 708 | unsigned long flags; |
| 709 | |
K. Y. Srinivasan | cd654ea | 2011-09-13 10:59:48 -0700 | [diff] [blame] | 710 | stor_device = hv_get_drvdata(device); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 711 | |
| 712 | spin_lock_irqsave(&device->channel->inbound_lock, flags); |
| 713 | stor_device->destroy = true; |
| 714 | spin_unlock_irqrestore(&device->channel->inbound_lock, flags); |
| 715 | |
| 716 | /* |
| 717 | * At this point, all outbound traffic should be disable. We |
| 718 | * only allow inbound traffic (responses) to proceed so that |
| 719 | * outstanding requests can be completed. |
| 720 | */ |
| 721 | |
| 722 | storvsc_wait_to_drain(stor_device); |
| 723 | |
| 724 | /* |
| 725 | * Since we have already drained, we don't need to busy wait |
| 726 | * as was done in final_release_stor_device() |
| 727 | * Note that we cannot set the ext pointer to NULL until |
| 728 | * we have drained - to drain the outgoing packets, we need to |
| 729 | * allow incoming packets. |
| 730 | */ |
| 731 | spin_lock_irqsave(&device->channel->inbound_lock, flags); |
K. Y. Srinivasan | cd654ea | 2011-09-13 10:59:48 -0700 | [diff] [blame] | 732 | hv_set_drvdata(device, NULL); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 733 | spin_unlock_irqrestore(&device->channel->inbound_lock, flags); |
| 734 | |
| 735 | /* Close the channel */ |
| 736 | vmbus_close(device->channel); |
| 737 | |
| 738 | kfree(stor_device); |
| 739 | return 0; |
| 740 | } |
| 741 | |
K. Y. Srinivasan | c1b3d06 | 2011-08-27 11:31:25 -0700 | [diff] [blame] | 742 | static int storvsc_do_io(struct hv_device *device, |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 743 | struct hv_storvsc_request *request) |
| 744 | { |
| 745 | struct storvsc_device *stor_device; |
| 746 | struct vstor_packet *vstor_packet; |
| 747 | int ret = 0; |
| 748 | |
| 749 | vstor_packet = &request->vstor_packet; |
| 750 | stor_device = get_out_stor_device(device); |
| 751 | |
| 752 | if (!stor_device) |
| 753 | return -ENODEV; |
| 754 | |
| 755 | |
| 756 | request->device = device; |
| 757 | |
| 758 | |
| 759 | vstor_packet->flags |= REQUEST_COMPLETION_FLAG; |
| 760 | |
| 761 | vstor_packet->vm_srb.length = sizeof(struct vmscsi_request); |
| 762 | |
| 763 | |
| 764 | vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE; |
| 765 | |
| 766 | |
| 767 | vstor_packet->vm_srb.data_transfer_length = |
| 768 | request->data_buffer.len; |
| 769 | |
| 770 | vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB; |
| 771 | |
| 772 | if (request->data_buffer.len) { |
| 773 | ret = vmbus_sendpacket_multipagebuffer(device->channel, |
| 774 | &request->data_buffer, |
| 775 | vstor_packet, |
| 776 | sizeof(struct vstor_packet), |
| 777 | (unsigned long)request); |
| 778 | } else { |
| 779 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
| 780 | sizeof(struct vstor_packet), |
| 781 | (unsigned long)request, |
| 782 | VM_PKT_DATA_INBAND, |
| 783 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 784 | } |
| 785 | |
| 786 | if (ret != 0) |
| 787 | return ret; |
| 788 | |
| 789 | atomic_inc(&stor_device->num_outstanding_req); |
| 790 | |
| 791 | return ret; |
| 792 | } |
| 793 | |
K. Y. Srinivasan | 5b60ace | 2011-05-10 07:54:25 -0700 | [diff] [blame] | 794 | static int storvsc_device_alloc(struct scsi_device *sdevice) |
| 795 | { |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 796 | struct stor_mem_pools *memp; |
| 797 | int number = STORVSC_MIN_BUF_NR; |
| 798 | |
| 799 | memp = kzalloc(sizeof(struct stor_mem_pools), GFP_KERNEL); |
| 800 | if (!memp) |
| 801 | return -ENOMEM; |
| 802 | |
| 803 | memp->request_pool = |
| 804 | kmem_cache_create(dev_name(&sdevice->sdev_dev), |
| 805 | sizeof(struct storvsc_cmd_request), 0, |
| 806 | SLAB_HWCACHE_ALIGN, NULL); |
| 807 | |
| 808 | if (!memp->request_pool) |
| 809 | goto err0; |
| 810 | |
| 811 | memp->request_mempool = mempool_create(number, mempool_alloc_slab, |
| 812 | mempool_free_slab, |
| 813 | memp->request_pool); |
| 814 | |
| 815 | if (!memp->request_mempool) |
| 816 | goto err1; |
| 817 | |
| 818 | sdevice->hostdata = memp; |
| 819 | |
K. Y. Srinivasan | 5b60ace | 2011-05-10 07:54:25 -0700 | [diff] [blame] | 820 | return 0; |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 821 | |
| 822 | err1: |
| 823 | kmem_cache_destroy(memp->request_pool); |
| 824 | |
| 825 | err0: |
| 826 | kfree(memp); |
| 827 | return -ENOMEM; |
| 828 | } |
| 829 | |
| 830 | static void storvsc_device_destroy(struct scsi_device *sdevice) |
| 831 | { |
| 832 | struct stor_mem_pools *memp = sdevice->hostdata; |
| 833 | |
| 834 | mempool_destroy(memp->request_mempool); |
| 835 | kmem_cache_destroy(memp->request_pool); |
| 836 | kfree(memp); |
| 837 | sdevice->hostdata = NULL; |
K. Y. Srinivasan | 5b60ace | 2011-05-10 07:54:25 -0700 | [diff] [blame] | 838 | } |
| 839 | |
K. Y. Srinivasan | 419f2d0 | 2011-05-10 07:54:27 -0700 | [diff] [blame] | 840 | static int storvsc_device_configure(struct scsi_device *sdevice) |
| 841 | { |
| 842 | scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG, |
| 843 | STORVSC_MAX_IO_REQUESTS); |
| 844 | |
K. Y. Srinivasan | 419f2d0 | 2011-05-10 07:54:27 -0700 | [diff] [blame] | 845 | blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE); |
| 846 | |
K. Y. Srinivasan | 419f2d0 | 2011-05-10 07:54:27 -0700 | [diff] [blame] | 847 | blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY); |
K. Y. Srinivasan | 419f2d0 | 2011-05-10 07:54:27 -0700 | [diff] [blame] | 848 | |
| 849 | return 0; |
| 850 | } |
| 851 | |
K. Y. Srinivasan | 49a3c7a | 2011-05-10 07:54:28 -0700 | [diff] [blame] | 852 | static void destroy_bounce_buffer(struct scatterlist *sgl, |
| 853 | unsigned int sg_count) |
| 854 | { |
| 855 | int i; |
| 856 | struct page *page_buf; |
| 857 | |
| 858 | for (i = 0; i < sg_count; i++) { |
| 859 | page_buf = sg_page((&sgl[i])); |
| 860 | if (page_buf != NULL) |
| 861 | __free_page(page_buf); |
| 862 | } |
| 863 | |
| 864 | kfree(sgl); |
| 865 | } |
| 866 | |
K. Y. Srinivasan | 3862ef3 | 2011-05-10 07:54:29 -0700 | [diff] [blame] | 867 | static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count) |
| 868 | { |
| 869 | int i; |
| 870 | |
| 871 | /* No need to check */ |
| 872 | if (sg_count < 2) |
| 873 | return -1; |
| 874 | |
| 875 | /* We have at least 2 sg entries */ |
| 876 | for (i = 0; i < sg_count; i++) { |
| 877 | if (i == 0) { |
| 878 | /* make sure 1st one does not have hole */ |
| 879 | if (sgl[i].offset + sgl[i].length != PAGE_SIZE) |
| 880 | return i; |
| 881 | } else if (i == sg_count - 1) { |
| 882 | /* make sure last one does not have hole */ |
| 883 | if (sgl[i].offset != 0) |
| 884 | return i; |
| 885 | } else { |
| 886 | /* make sure no hole in the middle */ |
| 887 | if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0) |
| 888 | return i; |
| 889 | } |
| 890 | } |
| 891 | return -1; |
| 892 | } |
| 893 | |
K. Y. Srinivasan | a9753cb | 2011-05-10 07:54:30 -0700 | [diff] [blame] | 894 | static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl, |
| 895 | unsigned int sg_count, |
K. Y. Srinivasan | 6e8087a | 2011-12-07 07:15:52 -0800 | [diff] [blame] | 896 | unsigned int len, |
| 897 | int write) |
K. Y. Srinivasan | a9753cb | 2011-05-10 07:54:30 -0700 | [diff] [blame] | 898 | { |
| 899 | int i; |
| 900 | int num_pages; |
| 901 | struct scatterlist *bounce_sgl; |
| 902 | struct page *page_buf; |
K. Y. Srinivasan | 6e8087a | 2011-12-07 07:15:52 -0800 | [diff] [blame] | 903 | unsigned int buf_len = ((write == WRITE_TYPE) ? 0 : PAGE_SIZE); |
K. Y. Srinivasan | a9753cb | 2011-05-10 07:54:30 -0700 | [diff] [blame] | 904 | |
| 905 | num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT; |
| 906 | |
| 907 | bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC); |
| 908 | if (!bounce_sgl) |
| 909 | return NULL; |
| 910 | |
| 911 | for (i = 0; i < num_pages; i++) { |
| 912 | page_buf = alloc_page(GFP_ATOMIC); |
| 913 | if (!page_buf) |
| 914 | goto cleanup; |
K. Y. Srinivasan | 6e8087a | 2011-12-07 07:15:52 -0800 | [diff] [blame] | 915 | sg_set_page(&bounce_sgl[i], page_buf, buf_len, 0); |
K. Y. Srinivasan | a9753cb | 2011-05-10 07:54:30 -0700 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | return bounce_sgl; |
| 919 | |
| 920 | cleanup: |
| 921 | destroy_bounce_buffer(bounce_sgl, num_pages); |
| 922 | return NULL; |
| 923 | } |
| 924 | |
K. Y. Srinivasan | 29fe2c9 | 2011-05-10 07:54:31 -0700 | [diff] [blame] | 925 | |
| 926 | /* Assume the original sgl has enough room */ |
| 927 | static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl, |
| 928 | struct scatterlist *bounce_sgl, |
K. Y. Srinivasan | 10c43dd4 | 2011-12-01 04:59:19 -0800 | [diff] [blame] | 929 | unsigned int orig_sgl_count, |
| 930 | unsigned int bounce_sgl_count) |
K. Y. Srinivasan | 29fe2c9 | 2011-05-10 07:54:31 -0700 | [diff] [blame] | 931 | { |
| 932 | int i; |
| 933 | int j = 0; |
| 934 | unsigned long src, dest; |
| 935 | unsigned int srclen, destlen, copylen; |
| 936 | unsigned int total_copied = 0; |
| 937 | unsigned long bounce_addr = 0; |
| 938 | unsigned long dest_addr = 0; |
| 939 | unsigned long flags; |
| 940 | |
| 941 | local_irq_save(flags); |
| 942 | |
| 943 | for (i = 0; i < orig_sgl_count; i++) { |
| 944 | dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), |
| 945 | KM_IRQ0) + orig_sgl[i].offset; |
| 946 | dest = dest_addr; |
| 947 | destlen = orig_sgl[i].length; |
| 948 | |
| 949 | if (bounce_addr == 0) |
| 950 | bounce_addr = |
| 951 | (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), |
| 952 | KM_IRQ0); |
| 953 | |
| 954 | while (destlen) { |
| 955 | src = bounce_addr + bounce_sgl[j].offset; |
| 956 | srclen = bounce_sgl[j].length - bounce_sgl[j].offset; |
| 957 | |
| 958 | copylen = min(srclen, destlen); |
| 959 | memcpy((void *)dest, (void *)src, copylen); |
| 960 | |
| 961 | total_copied += copylen; |
| 962 | bounce_sgl[j].offset += copylen; |
| 963 | destlen -= copylen; |
| 964 | dest += copylen; |
| 965 | |
| 966 | if (bounce_sgl[j].offset == bounce_sgl[j].length) { |
| 967 | /* full */ |
| 968 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); |
| 969 | j++; |
| 970 | |
K. Y. Srinivasan | 10c43dd4 | 2011-12-01 04:59:19 -0800 | [diff] [blame] | 971 | /* |
| 972 | * It is possible that the number of elements |
| 973 | * in the bounce buffer may not be equal to |
| 974 | * the number of elements in the original |
| 975 | * scatter list. Handle this correctly. |
| 976 | */ |
| 977 | |
| 978 | if (j == bounce_sgl_count) { |
| 979 | /* |
| 980 | * We are done; cleanup and return. |
| 981 | */ |
| 982 | kunmap_atomic((void *)(dest_addr - |
| 983 | orig_sgl[i].offset), |
| 984 | KM_IRQ0); |
| 985 | local_irq_restore(flags); |
| 986 | return total_copied; |
| 987 | } |
| 988 | |
K. Y. Srinivasan | 29fe2c9 | 2011-05-10 07:54:31 -0700 | [diff] [blame] | 989 | /* if we need to use another bounce buffer */ |
| 990 | if (destlen || i != orig_sgl_count - 1) |
| 991 | bounce_addr = |
| 992 | (unsigned long)kmap_atomic( |
| 993 | sg_page((&bounce_sgl[j])), KM_IRQ0); |
| 994 | } else if (destlen == 0 && i == orig_sgl_count - 1) { |
| 995 | /* unmap the last bounce that is < PAGE_SIZE */ |
| 996 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset), |
| 1001 | KM_IRQ0); |
| 1002 | } |
| 1003 | |
| 1004 | local_irq_restore(flags); |
| 1005 | |
| 1006 | return total_copied; |
| 1007 | } |
| 1008 | |
K. Y. Srinivasan | 6a8ff44 | 2011-05-10 07:54:32 -0700 | [diff] [blame] | 1009 | |
| 1010 | /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */ |
| 1011 | static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl, |
| 1012 | struct scatterlist *bounce_sgl, |
| 1013 | unsigned int orig_sgl_count) |
| 1014 | { |
| 1015 | int i; |
| 1016 | int j = 0; |
| 1017 | unsigned long src, dest; |
| 1018 | unsigned int srclen, destlen, copylen; |
| 1019 | unsigned int total_copied = 0; |
| 1020 | unsigned long bounce_addr = 0; |
| 1021 | unsigned long src_addr = 0; |
| 1022 | unsigned long flags; |
| 1023 | |
| 1024 | local_irq_save(flags); |
| 1025 | |
| 1026 | for (i = 0; i < orig_sgl_count; i++) { |
| 1027 | src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), |
| 1028 | KM_IRQ0) + orig_sgl[i].offset; |
| 1029 | src = src_addr; |
| 1030 | srclen = orig_sgl[i].length; |
| 1031 | |
| 1032 | if (bounce_addr == 0) |
| 1033 | bounce_addr = |
| 1034 | (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), |
| 1035 | KM_IRQ0); |
| 1036 | |
| 1037 | while (srclen) { |
| 1038 | /* assume bounce offset always == 0 */ |
| 1039 | dest = bounce_addr + bounce_sgl[j].length; |
| 1040 | destlen = PAGE_SIZE - bounce_sgl[j].length; |
| 1041 | |
| 1042 | copylen = min(srclen, destlen); |
| 1043 | memcpy((void *)dest, (void *)src, copylen); |
| 1044 | |
| 1045 | total_copied += copylen; |
| 1046 | bounce_sgl[j].length += copylen; |
| 1047 | srclen -= copylen; |
| 1048 | src += copylen; |
| 1049 | |
| 1050 | if (bounce_sgl[j].length == PAGE_SIZE) { |
| 1051 | /* full..move to next entry */ |
| 1052 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); |
| 1053 | j++; |
| 1054 | |
| 1055 | /* if we need to use another bounce buffer */ |
| 1056 | if (srclen || i != orig_sgl_count - 1) |
| 1057 | bounce_addr = |
| 1058 | (unsigned long)kmap_atomic( |
| 1059 | sg_page((&bounce_sgl[j])), KM_IRQ0); |
| 1060 | |
| 1061 | } else if (srclen == 0 && i == orig_sgl_count - 1) { |
| 1062 | /* unmap the last bounce that is < PAGE_SIZE */ |
| 1063 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0); |
| 1068 | } |
| 1069 | |
| 1070 | local_irq_restore(flags); |
| 1071 | |
| 1072 | return total_copied; |
| 1073 | } |
| 1074 | |
K. Y. Srinivasan | 5c5c023 | 2011-05-10 07:54:33 -0700 | [diff] [blame] | 1075 | |
K. Y. Srinivasan | 5c5c023 | 2011-05-10 07:54:33 -0700 | [diff] [blame] | 1076 | static int storvsc_remove(struct hv_device *dev) |
| 1077 | { |
K. Y. Srinivasan | cd654ea | 2011-09-13 10:59:48 -0700 | [diff] [blame] | 1078 | struct storvsc_device *stor_device = hv_get_drvdata(dev); |
| 1079 | struct Scsi_Host *host = stor_device->host; |
K. Y. Srinivasan | 5c5c023 | 2011-05-10 07:54:33 -0700 | [diff] [blame] | 1080 | |
K. Y. Srinivasan | 5c5c023 | 2011-05-10 07:54:33 -0700 | [diff] [blame] | 1081 | scsi_remove_host(host); |
| 1082 | |
K. Y. Srinivasan | 5c5c023 | 2011-05-10 07:54:33 -0700 | [diff] [blame] | 1083 | scsi_host_put(host); |
K. Y. Srinivasan | d36b0a0 | 2011-06-06 15:49:29 -0700 | [diff] [blame] | 1084 | |
K. Y. Srinivasan | 2935a40 | 2011-06-06 15:49:28 -0700 | [diff] [blame] | 1085 | storvsc_dev_remove(dev); |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 1086 | |
K. Y. Srinivasan | 5c5c023 | 2011-05-10 07:54:33 -0700 | [diff] [blame] | 1087 | return 0; |
| 1088 | } |
| 1089 | |
K. Y. Srinivasan | 62838ce | 2011-05-10 07:54:34 -0700 | [diff] [blame] | 1090 | |
| 1091 | static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev, |
| 1092 | sector_t capacity, int *info) |
| 1093 | { |
K. Y. Srinivasan | 5326fd5 | 2011-05-10 07:54:52 -0700 | [diff] [blame] | 1094 | sector_t nsect = capacity; |
| 1095 | sector_t cylinders = nsect; |
| 1096 | int heads, sectors_pt; |
K. Y. Srinivasan | 62838ce | 2011-05-10 07:54:34 -0700 | [diff] [blame] | 1097 | |
K. Y. Srinivasan | 5326fd5 | 2011-05-10 07:54:52 -0700 | [diff] [blame] | 1098 | /* |
| 1099 | * We are making up these values; let us keep it simple. |
| 1100 | */ |
| 1101 | heads = 0xff; |
| 1102 | sectors_pt = 0x3f; /* Sectors per track */ |
| 1103 | sector_div(cylinders, heads * sectors_pt); |
| 1104 | if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect) |
| 1105 | cylinders = 0xffff; |
K. Y. Srinivasan | 62838ce | 2011-05-10 07:54:34 -0700 | [diff] [blame] | 1106 | |
| 1107 | info[0] = heads; |
K. Y. Srinivasan | 5326fd5 | 2011-05-10 07:54:52 -0700 | [diff] [blame] | 1108 | info[1] = sectors_pt; |
| 1109 | info[2] = (int)cylinders; |
K. Y. Srinivasan | 62838ce | 2011-05-10 07:54:34 -0700 | [diff] [blame] | 1110 | |
K. Y. Srinivasan | 62838ce | 2011-05-10 07:54:34 -0700 | [diff] [blame] | 1111 | return 0; |
| 1112 | } |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1113 | |
| 1114 | static int storvsc_host_reset(struct hv_device *device) |
| 1115 | { |
| 1116 | struct storvsc_device *stor_device; |
| 1117 | struct hv_storvsc_request *request; |
| 1118 | struct vstor_packet *vstor_packet; |
| 1119 | int ret, t; |
| 1120 | |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1121 | |
K. Y. Srinivasan | 1eaaddf | 2011-08-27 11:31:03 -0700 | [diff] [blame] | 1122 | stor_device = get_out_stor_device(device); |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1123 | if (!stor_device) |
K. Y. Srinivasan | a00e822 | 2011-11-08 09:01:43 -0800 | [diff] [blame] | 1124 | return FAILED; |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1125 | |
| 1126 | request = &stor_device->reset_request; |
| 1127 | vstor_packet = &request->vstor_packet; |
| 1128 | |
| 1129 | init_completion(&request->wait_event); |
| 1130 | |
| 1131 | vstor_packet->operation = VSTOR_OPERATION_RESET_BUS; |
| 1132 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; |
| 1133 | vstor_packet->vm_srb.path_id = stor_device->path_id; |
| 1134 | |
| 1135 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
| 1136 | sizeof(struct vstor_packet), |
| 1137 | (unsigned long)&stor_device->reset_request, |
| 1138 | VM_PKT_DATA_INBAND, |
| 1139 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 1140 | if (ret != 0) |
K. Y. Srinivasan | a00e822 | 2011-11-08 09:01:43 -0800 | [diff] [blame] | 1141 | return FAILED; |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1142 | |
K. Y. Srinivasan | 46d2eb6 | 2011-06-16 13:16:36 -0700 | [diff] [blame] | 1143 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); |
K. Y. Srinivasan | a00e822 | 2011-11-08 09:01:43 -0800 | [diff] [blame] | 1144 | if (t == 0) |
| 1145 | return TIMEOUT_ERROR; |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1146 | |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1147 | |
| 1148 | /* |
| 1149 | * At this point, all outstanding requests in the adapter |
| 1150 | * should have been flushed out and return to us |
| 1151 | */ |
| 1152 | |
K. Y. Srinivasan | a00e822 | 2011-11-08 09:01:43 -0800 | [diff] [blame] | 1153 | return SUCCESS; |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | |
K. Y. Srinivasan | 96e690be | 2011-05-10 07:54:38 -0700 | [diff] [blame] | 1157 | static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd) |
| 1158 | { |
K. Y. Srinivasan | 7f33f30 | 2011-11-08 09:01:44 -0800 | [diff] [blame] | 1159 | struct hv_host_device *host_dev = shost_priv(scmnd->device->host); |
K. Y. Srinivasan | 96e690be | 2011-05-10 07:54:38 -0700 | [diff] [blame] | 1160 | struct hv_device *dev = host_dev->dev; |
| 1161 | |
K. Y. Srinivasan | a00e822 | 2011-11-08 09:01:43 -0800 | [diff] [blame] | 1162 | return storvsc_host_reset(dev); |
K. Y. Srinivasan | 96e690be | 2011-05-10 07:54:38 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 1165 | |
K. Y. Srinivasan | b8de73d | 2011-08-27 11:31:27 -0700 | [diff] [blame] | 1166 | static void storvsc_command_completion(struct hv_storvsc_request *request) |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 1167 | { |
| 1168 | struct storvsc_cmd_request *cmd_request = |
| 1169 | (struct storvsc_cmd_request *)request->context; |
| 1170 | struct scsi_cmnd *scmnd = cmd_request->cmd; |
K. Y. Srinivasan | 7f33f30 | 2011-11-08 09:01:44 -0800 | [diff] [blame] | 1171 | struct hv_host_device *host_dev = shost_priv(scmnd->device->host); |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 1172 | void (*scsi_done_fn)(struct scsi_cmnd *); |
| 1173 | struct scsi_sense_hdr sense_hdr; |
| 1174 | struct vmscsi_request *vm_srb; |
K. Y. Srinivasan | b401731 | 2011-11-08 09:01:50 -0800 | [diff] [blame] | 1175 | struct storvsc_scan_work *wrk; |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 1176 | struct stor_mem_pools *memp = scmnd->device->hostdata; |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 1177 | |
K. Y. Srinivasan | 3612ef9 | 2011-08-27 11:31:20 -0700 | [diff] [blame] | 1178 | vm_srb = &request->vstor_packet.vm_srb; |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 1179 | if (cmd_request->bounce_sgl_count) { |
K. Y. Srinivasan | a768a76 | 2011-12-01 04:59:18 -0800 | [diff] [blame] | 1180 | if (vm_srb->data_in == READ_TYPE) |
K. Y. Srinivasan | 3612ef9 | 2011-08-27 11:31:20 -0700 | [diff] [blame] | 1181 | copy_from_bounce_buffer(scsi_sglist(scmnd), |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 1182 | cmd_request->bounce_sgl, |
K. Y. Srinivasan | 10c43dd4 | 2011-12-01 04:59:19 -0800 | [diff] [blame] | 1183 | scsi_sg_count(scmnd), |
| 1184 | cmd_request->bounce_sgl_count); |
K. Y. Srinivasan | a768a76 | 2011-12-01 04:59:18 -0800 | [diff] [blame] | 1185 | destroy_bounce_buffer(cmd_request->bounce_sgl, |
K. Y. Srinivasan | 3612ef9 | 2011-08-27 11:31:20 -0700 | [diff] [blame] | 1186 | cmd_request->bounce_sgl_count); |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
K. Y. Srinivasan | 2544b79 | 2011-08-27 11:31:28 -0700 | [diff] [blame] | 1189 | /* |
| 1190 | * If there is an error; offline the device since all |
| 1191 | * error recovery strategies would have already been |
| 1192 | * deployed on the host side. |
| 1193 | */ |
| 1194 | if (vm_srb->srb_status == 0x4) |
| 1195 | scmnd->result = DID_TARGET_FAILURE << 16; |
| 1196 | else |
| 1197 | scmnd->result = vm_srb->scsi_status; |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 1198 | |
K. Y. Srinivasan | b401731 | 2011-11-08 09:01:50 -0800 | [diff] [blame] | 1199 | /* |
| 1200 | * If the LUN is invalid; remove the device. |
| 1201 | */ |
| 1202 | if (vm_srb->srb_status == 0x20) { |
| 1203 | struct storvsc_device *stor_dev; |
| 1204 | struct hv_device *dev = host_dev->dev; |
| 1205 | struct Scsi_Host *host; |
| 1206 | |
| 1207 | stor_dev = get_in_stor_device(dev); |
| 1208 | host = stor_dev->host; |
| 1209 | |
| 1210 | wrk = kmalloc(sizeof(struct storvsc_scan_work), |
| 1211 | GFP_ATOMIC); |
| 1212 | if (!wrk) { |
| 1213 | scmnd->result = DID_TARGET_FAILURE << 16; |
| 1214 | } else { |
| 1215 | wrk->host = host; |
| 1216 | wrk->lun = vm_srb->lun; |
| 1217 | INIT_WORK(&wrk->work, storvsc_remove_lun); |
| 1218 | schedule_work(&wrk->work); |
| 1219 | } |
| 1220 | } |
| 1221 | |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 1222 | if (scmnd->result) { |
| 1223 | if (scsi_normalize_sense(scmnd->sense_buffer, |
| 1224 | SCSI_SENSE_BUFFERSIZE, &sense_hdr)) |
| 1225 | scsi_print_sense_hdr("storvsc", &sense_hdr); |
| 1226 | } |
| 1227 | |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 1228 | scsi_set_resid(scmnd, |
| 1229 | request->data_buffer.len - |
| 1230 | vm_srb->data_transfer_length); |
| 1231 | |
| 1232 | scsi_done_fn = scmnd->scsi_done; |
| 1233 | |
| 1234 | scmnd->host_scribble = NULL; |
| 1235 | scmnd->scsi_done = NULL; |
| 1236 | |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 1237 | scsi_done_fn(scmnd); |
| 1238 | |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 1239 | mempool_free(cmd_request, memp->request_mempool); |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Olaf Hering | 92ae4eb | 2011-10-10 09:37:39 +0200 | [diff] [blame] | 1242 | static bool storvsc_check_scsi_cmd(struct scsi_cmnd *scmnd) |
| 1243 | { |
| 1244 | bool allowed = true; |
| 1245 | u8 scsi_op = scmnd->cmnd[0]; |
| 1246 | |
| 1247 | switch (scsi_op) { |
K. Y. Srinivasan | 41098f8 | 2011-10-14 21:31:57 -0700 | [diff] [blame] | 1248 | /* smartd sends this command, which will offline the device */ |
| 1249 | case SET_WINDOW: |
K. Y. Srinivasan | 59e00e7 | 2011-11-08 09:01:42 -0800 | [diff] [blame] | 1250 | scmnd->result = ILLEGAL_REQUEST << 16; |
K. Y. Srinivasan | 41098f8 | 2011-10-14 21:31:57 -0700 | [diff] [blame] | 1251 | allowed = false; |
| 1252 | break; |
| 1253 | default: |
| 1254 | break; |
Olaf Hering | 92ae4eb | 2011-10-10 09:37:39 +0200 | [diff] [blame] | 1255 | } |
| 1256 | return allowed; |
| 1257 | } |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1258 | |
K. Y. Srinivasan | bab445e | 2011-11-08 09:01:45 -0800 | [diff] [blame] | 1259 | static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd) |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1260 | { |
| 1261 | int ret; |
K. Y. Srinivasan | bab445e | 2011-11-08 09:01:45 -0800 | [diff] [blame] | 1262 | struct hv_host_device *host_dev = shost_priv(host); |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1263 | struct hv_device *dev = host_dev->dev; |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1264 | struct hv_storvsc_request *request; |
| 1265 | struct storvsc_cmd_request *cmd_request; |
| 1266 | unsigned int request_size = 0; |
| 1267 | int i; |
| 1268 | struct scatterlist *sgl; |
| 1269 | unsigned int sg_count = 0; |
| 1270 | struct vmscsi_request *vm_srb; |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 1271 | struct stor_mem_pools *memp = scmnd->device->hostdata; |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1272 | |
Olaf Hering | 92ae4eb | 2011-10-10 09:37:39 +0200 | [diff] [blame] | 1273 | if (storvsc_check_scsi_cmd(scmnd) == false) { |
K. Y. Srinivasan | bab445e | 2011-11-08 09:01:45 -0800 | [diff] [blame] | 1274 | scmnd->scsi_done(scmnd); |
Olaf Hering | 92ae4eb | 2011-10-10 09:37:39 +0200 | [diff] [blame] | 1275 | return 0; |
| 1276 | } |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1277 | |
| 1278 | /* If retrying, no need to prep the cmd */ |
| 1279 | if (scmnd->host_scribble) { |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1280 | |
| 1281 | cmd_request = |
| 1282 | (struct storvsc_cmd_request *)scmnd->host_scribble; |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1283 | |
| 1284 | goto retry_request; |
| 1285 | } |
| 1286 | |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1287 | request_size = sizeof(struct storvsc_cmd_request); |
| 1288 | |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 1289 | cmd_request = mempool_alloc(memp->request_mempool, |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1290 | GFP_ATOMIC); |
K. Y. Srinivasan | bab445e | 2011-11-08 09:01:45 -0800 | [diff] [blame] | 1291 | if (!cmd_request) |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1292 | return SCSI_MLQUEUE_DEVICE_BUSY; |
K. Y. Srinivasan | bab445e | 2011-11-08 09:01:45 -0800 | [diff] [blame] | 1293 | |
K. Y. Srinivasan | 4e03e69 | 2011-11-08 09:01:40 -0800 | [diff] [blame] | 1294 | memset(cmd_request, 0, sizeof(struct storvsc_cmd_request)); |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1295 | |
| 1296 | /* Setup the cmd request */ |
| 1297 | cmd_request->bounce_sgl_count = 0; |
| 1298 | cmd_request->bounce_sgl = NULL; |
| 1299 | cmd_request->cmd = scmnd; |
| 1300 | |
| 1301 | scmnd->host_scribble = (unsigned char *)cmd_request; |
| 1302 | |
| 1303 | request = &cmd_request->request; |
| 1304 | vm_srb = &request->vstor_packet.vm_srb; |
| 1305 | |
| 1306 | |
| 1307 | /* Build the SRB */ |
| 1308 | switch (scmnd->sc_data_direction) { |
| 1309 | case DMA_TO_DEVICE: |
| 1310 | vm_srb->data_in = WRITE_TYPE; |
| 1311 | break; |
| 1312 | case DMA_FROM_DEVICE: |
| 1313 | vm_srb->data_in = READ_TYPE; |
| 1314 | break; |
| 1315 | default: |
| 1316 | vm_srb->data_in = UNKNOWN_TYPE; |
| 1317 | break; |
| 1318 | } |
| 1319 | |
K. Y. Srinivasan | b8de73d | 2011-08-27 11:31:27 -0700 | [diff] [blame] | 1320 | request->on_io_completion = storvsc_command_completion; |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1321 | request->context = cmd_request;/* scmnd; */ |
| 1322 | |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1323 | vm_srb->port_number = host_dev->port; |
| 1324 | vm_srb->path_id = scmnd->device->channel; |
| 1325 | vm_srb->target_id = scmnd->device->id; |
| 1326 | vm_srb->lun = scmnd->device->lun; |
| 1327 | |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1328 | vm_srb->cdb_length = scmnd->cmd_len; |
| 1329 | |
| 1330 | memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length); |
| 1331 | |
| 1332 | request->sense_buffer = scmnd->sense_buffer; |
| 1333 | |
| 1334 | |
| 1335 | request->data_buffer.len = scsi_bufflen(scmnd); |
| 1336 | if (scsi_sg_count(scmnd)) { |
| 1337 | sgl = (struct scatterlist *)scsi_sglist(scmnd); |
| 1338 | sg_count = scsi_sg_count(scmnd); |
| 1339 | |
| 1340 | /* check if we need to bounce the sgl */ |
| 1341 | if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) { |
| 1342 | cmd_request->bounce_sgl = |
| 1343 | create_bounce_buffer(sgl, scsi_sg_count(scmnd), |
K. Y. Srinivasan | 6e8087a | 2011-12-07 07:15:52 -0800 | [diff] [blame] | 1344 | scsi_bufflen(scmnd), |
| 1345 | vm_srb->data_in); |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1346 | if (!cmd_request->bounce_sgl) { |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1347 | scmnd->host_scribble = NULL; |
K. Y. Srinivasan | 4e03e69 | 2011-11-08 09:01:40 -0800 | [diff] [blame] | 1348 | mempool_free(cmd_request, |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 1349 | memp->request_mempool); |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1350 | |
| 1351 | return SCSI_MLQUEUE_HOST_BUSY; |
| 1352 | } |
| 1353 | |
| 1354 | cmd_request->bounce_sgl_count = |
| 1355 | ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >> |
| 1356 | PAGE_SHIFT; |
| 1357 | |
K. Y. Srinivasan | fa23b8c | 2011-08-27 11:31:21 -0700 | [diff] [blame] | 1358 | if (vm_srb->data_in == WRITE_TYPE) |
| 1359 | copy_to_bounce_buffer(sgl, |
| 1360 | cmd_request->bounce_sgl, |
| 1361 | scsi_sg_count(scmnd)); |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1362 | |
| 1363 | sgl = cmd_request->bounce_sgl; |
| 1364 | sg_count = cmd_request->bounce_sgl_count; |
| 1365 | } |
| 1366 | |
| 1367 | request->data_buffer.offset = sgl[0].offset; |
| 1368 | |
| 1369 | for (i = 0; i < sg_count; i++) |
| 1370 | request->data_buffer.pfn_array[i] = |
| 1371 | page_to_pfn(sg_page((&sgl[i]))); |
| 1372 | |
| 1373 | } else if (scsi_sglist(scmnd)) { |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1374 | request->data_buffer.offset = |
| 1375 | virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1); |
| 1376 | request->data_buffer.pfn_array[0] = |
| 1377 | virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT; |
| 1378 | } |
| 1379 | |
| 1380 | retry_request: |
| 1381 | /* Invokes the vsc to start an IO */ |
K. Y. Srinivasan | 636f0fd | 2011-05-10 07:54:50 -0700 | [diff] [blame] | 1382 | ret = storvsc_do_io(dev, &cmd_request->request); |
| 1383 | |
K. Y. Srinivasan | d2598f0 | 2011-08-25 09:48:58 -0700 | [diff] [blame] | 1384 | if (ret == -EAGAIN) { |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1385 | /* no more space */ |
| 1386 | |
K. Y. Srinivasan | 70691ec | 2011-08-27 11:31:29 -0700 | [diff] [blame] | 1387 | if (cmd_request->bounce_sgl_count) |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1388 | destroy_bounce_buffer(cmd_request->bounce_sgl, |
K. Y. Srinivasan | 70691ec | 2011-08-27 11:31:29 -0700 | [diff] [blame] | 1389 | cmd_request->bounce_sgl_count); |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1390 | |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 1391 | mempool_free(cmd_request, memp->request_mempool); |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1392 | |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1393 | scmnd->host_scribble = NULL; |
| 1394 | |
| 1395 | ret = SCSI_MLQUEUE_DEVICE_BUSY; |
| 1396 | } |
| 1397 | |
| 1398 | return ret; |
| 1399 | } |
| 1400 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1401 | static struct scsi_host_template scsi_driver = { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 1402 | .module = THIS_MODULE, |
| 1403 | .name = "storvsc_host_t", |
| 1404 | .bios_param = storvsc_get_chs, |
| 1405 | .queuecommand = storvsc_queuecommand, |
| 1406 | .eh_host_reset_handler = storvsc_host_reset_handler, |
| 1407 | .slave_alloc = storvsc_device_alloc, |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 1408 | .slave_destroy = storvsc_device_destroy, |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 1409 | .slave_configure = storvsc_device_configure, |
| 1410 | .cmd_per_lun = 1, |
| 1411 | /* 64 max_queue * 1 target */ |
Lars Lindley | 0686e4f | 2010-03-11 23:51:23 +0100 | [diff] [blame] | 1412 | .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS, |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 1413 | .this_id = -1, |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 1414 | /* no use setting to 0 since ll_blk_rw reset it to 1 */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 1415 | /* currently 32 */ |
| 1416 | .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT, |
K. Y. Srinivasan | 039db52 | 2011-12-01 04:59:16 -0800 | [diff] [blame] | 1417 | .use_clustering = DISABLE_CLUSTERING, |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 1418 | /* Make sure we dont get a sg segment crosses a page boundary */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 1419 | .dma_boundary = PAGE_SIZE-1, |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1420 | }; |
| 1421 | |
K. Y. Srinivasan | ef52a81 | 2011-09-13 10:59:39 -0700 | [diff] [blame] | 1422 | enum { |
| 1423 | SCSI_GUID, |
| 1424 | IDE_GUID, |
| 1425 | }; |
| 1426 | |
K. Y. Srinivasan | d847b5f | 2011-08-25 09:48:33 -0700 | [diff] [blame] | 1427 | static const struct hv_vmbus_device_id id_table[] = { |
Greg Kroah-Hartman | c45cf2d | 2011-08-25 11:41:33 -0700 | [diff] [blame] | 1428 | /* SCSI guid */ |
| 1429 | { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d, |
K. Y. Srinivasan | ef52a81 | 2011-09-13 10:59:39 -0700 | [diff] [blame] | 1430 | 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f) |
| 1431 | .driver_data = SCSI_GUID }, |
K. Y. Srinivasan | 21e3774 | 2011-08-27 11:31:18 -0700 | [diff] [blame] | 1432 | /* IDE guid */ |
| 1433 | { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44, |
K. Y. Srinivasan | ef52a81 | 2011-09-13 10:59:39 -0700 | [diff] [blame] | 1434 | 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5) |
| 1435 | .driver_data = IDE_GUID }, |
Greg Kroah-Hartman | c45cf2d | 2011-08-25 11:41:33 -0700 | [diff] [blame] | 1436 | { }, |
K. Y. Srinivasan | d847b5f | 2011-08-25 09:48:33 -0700 | [diff] [blame] | 1437 | }; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1438 | |
K. Y. Srinivasan | d847b5f | 2011-08-25 09:48:33 -0700 | [diff] [blame] | 1439 | MODULE_DEVICE_TABLE(vmbus, id_table); |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 1440 | |
K. Y. Srinivasan | 8494689 | 2011-09-13 10:59:38 -0700 | [diff] [blame] | 1441 | static int storvsc_probe(struct hv_device *device, |
| 1442 | const struct hv_vmbus_device_id *dev_id) |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1443 | { |
| 1444 | int ret; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1445 | struct Scsi_Host *host; |
| 1446 | struct hv_host_device *host_dev; |
K. Y. Srinivasan | ef52a81 | 2011-09-13 10:59:39 -0700 | [diff] [blame] | 1447 | bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false); |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 1448 | int target = 0; |
K. Y. Srinivasan | 6e4198c | 2011-09-13 10:59:45 -0700 | [diff] [blame] | 1449 | struct storvsc_device *stor_device; |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 1450 | |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1451 | host = scsi_host_alloc(&scsi_driver, |
| 1452 | sizeof(struct hv_host_device)); |
| 1453 | if (!host) |
| 1454 | return -ENOMEM; |
| 1455 | |
K. Y. Srinivasan | 7f33f30 | 2011-11-08 09:01:44 -0800 | [diff] [blame] | 1456 | host_dev = shost_priv(host); |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1457 | memset(host_dev, 0, sizeof(struct hv_host_device)); |
| 1458 | |
| 1459 | host_dev->port = host->host_no; |
| 1460 | host_dev->dev = device; |
| 1461 | |
K. Y. Srinivasan | 4e03e69 | 2011-11-08 09:01:40 -0800 | [diff] [blame] | 1462 | |
K. Y. Srinivasan | a13d35a | 2011-09-13 10:59:46 -0700 | [diff] [blame] | 1463 | stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL); |
K. Y. Srinivasan | 6e4198c | 2011-09-13 10:59:45 -0700 | [diff] [blame] | 1464 | if (!stor_device) { |
K. Y. Srinivasan | 225ce6e | 2011-11-08 09:01:41 -0800 | [diff] [blame] | 1465 | ret = -ENOMEM; |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 1466 | goto err_out0; |
K. Y. Srinivasan | 6e4198c | 2011-09-13 10:59:45 -0700 | [diff] [blame] | 1467 | } |
| 1468 | |
K. Y. Srinivasan | a13d35a | 2011-09-13 10:59:46 -0700 | [diff] [blame] | 1469 | stor_device->destroy = false; |
| 1470 | init_waitqueue_head(&stor_device->waiting_to_drain); |
| 1471 | stor_device->device = device; |
K. Y. Srinivasan | cd654ea | 2011-09-13 10:59:48 -0700 | [diff] [blame] | 1472 | stor_device->host = host; |
| 1473 | hv_set_drvdata(device, stor_device); |
K. Y. Srinivasan | a13d35a | 2011-09-13 10:59:46 -0700 | [diff] [blame] | 1474 | |
K. Y. Srinivasan | 6e4198c | 2011-09-13 10:59:45 -0700 | [diff] [blame] | 1475 | stor_device->port_number = host->host_no; |
| 1476 | ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size); |
K. Y. Srinivasan | 225ce6e | 2011-11-08 09:01:41 -0800 | [diff] [blame] | 1477 | if (ret) |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 1478 | goto err_out1; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1479 | |
K. Y. Srinivasan | 6e4198c | 2011-09-13 10:59:45 -0700 | [diff] [blame] | 1480 | host_dev->path = stor_device->path_id; |
| 1481 | host_dev->target = stor_device->target_id; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1482 | |
| 1483 | /* max # of devices per target */ |
| 1484 | host->max_lun = STORVSC_MAX_LUNS_PER_TARGET; |
| 1485 | /* max # of targets per channel */ |
| 1486 | host->max_id = STORVSC_MAX_TARGETS; |
| 1487 | /* max # of channels */ |
| 1488 | host->max_channel = STORVSC_MAX_CHANNELS - 1; |
Mike Sterling | cf55f4a | 2011-09-06 16:10:55 -0700 | [diff] [blame] | 1489 | /* max cmd length */ |
| 1490 | host->max_cmd_len = STORVSC_MAX_CMD_LEN; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1491 | |
| 1492 | /* Register the HBA and start the scsi bus scan */ |
| 1493 | ret = scsi_add_host(host, &device->device); |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 1494 | if (ret != 0) |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 1495 | goto err_out2; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1496 | |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 1497 | if (!dev_is_ide) { |
| 1498 | scsi_scan_host(host); |
K. Y. Srinivasan | 59d2295 | 2012-01-12 12:37:55 -0800 | [diff] [blame^] | 1499 | } else { |
| 1500 | target = (device->dev_instance.b[5] << 8 | |
| 1501 | device->dev_instance.b[4]); |
| 1502 | ret = scsi_add_device(host, 0, target, 0); |
| 1503 | if (ret) { |
| 1504 | scsi_remove_host(host); |
| 1505 | goto err_out2; |
| 1506 | } |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 1507 | } |
| 1508 | return 0; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1509 | |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 1510 | err_out2: |
K. Y. Srinivasan | 225ce6e | 2011-11-08 09:01:41 -0800 | [diff] [blame] | 1511 | /* |
| 1512 | * Once we have connected with the host, we would need to |
| 1513 | * to invoke storvsc_dev_remove() to rollback this state and |
| 1514 | * this call also frees up the stor_device; hence the jump around |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 1515 | * err_out1 label. |
K. Y. Srinivasan | 225ce6e | 2011-11-08 09:01:41 -0800 | [diff] [blame] | 1516 | */ |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 1517 | storvsc_dev_remove(device); |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 1518 | goto err_out0; |
K. Y. Srinivasan | 225ce6e | 2011-11-08 09:01:41 -0800 | [diff] [blame] | 1519 | |
| 1520 | err_out1: |
K. Y. Srinivasan | ce3e301 | 2011-12-01 04:59:20 -0800 | [diff] [blame] | 1521 | kfree(stor_device); |
K. Y. Srinivasan | 225ce6e | 2011-11-08 09:01:41 -0800 | [diff] [blame] | 1522 | |
| 1523 | err_out0: |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 1524 | scsi_host_put(host); |
K. Y. Srinivasan | 225ce6e | 2011-11-08 09:01:41 -0800 | [diff] [blame] | 1525 | return ret; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1526 | } |
| 1527 | |
K. Y. Srinivasan | 40bf63e | 2011-05-10 07:56:09 -0700 | [diff] [blame] | 1528 | static struct hv_driver storvsc_drv = { |
K. Y. Srinivasan | fafb0ef | 2011-11-08 09:01:46 -0800 | [diff] [blame] | 1529 | .name = KBUILD_MODNAME, |
K. Y. Srinivasan | d847b5f | 2011-08-25 09:48:33 -0700 | [diff] [blame] | 1530 | .id_table = id_table, |
K. Y. Srinivasan | 40bf63e | 2011-05-10 07:56:09 -0700 | [diff] [blame] | 1531 | .probe = storvsc_probe, |
| 1532 | .remove = storvsc_remove, |
K. Y. Srinivasan | 39ae6fa | 2011-05-10 07:54:46 -0700 | [diff] [blame] | 1533 | }; |
K. Y. Srinivasan | 7bd05b9 | 2011-05-10 07:54:45 -0700 | [diff] [blame] | 1534 | |
K. Y. Srinivasan | d9bbae8 | 2011-06-06 15:49:27 -0700 | [diff] [blame] | 1535 | static int __init storvsc_drv_init(void) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1536 | { |
K. Y. Srinivasan | 01415ab3 | 2011-05-10 07:55:58 -0700 | [diff] [blame] | 1537 | u32 max_outstanding_req_per_channel; |
| 1538 | |
| 1539 | /* |
| 1540 | * Divide the ring buffer data size (which is 1 page less |
| 1541 | * than the ring buffer size since that page is reserved for |
| 1542 | * the ring buffer indices) by the max request size (which is |
| 1543 | * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64) |
| 1544 | */ |
K. Y. Srinivasan | 01415ab3 | 2011-05-10 07:55:58 -0700 | [diff] [blame] | 1545 | max_outstanding_req_per_channel = |
Greg Kroah-Hartman | 768fa21 | 2011-08-25 15:07:32 -0700 | [diff] [blame] | 1546 | ((storvsc_ringbuffer_size - PAGE_SIZE) / |
| 1547 | ALIGN(MAX_MULTIPAGE_BUFFER_PACKET + |
| 1548 | sizeof(struct vstor_packet) + sizeof(u64), |
| 1549 | sizeof(u64))); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1550 | |
K. Y. Srinivasan | 01415ab3 | 2011-05-10 07:55:58 -0700 | [diff] [blame] | 1551 | if (max_outstanding_req_per_channel < |
K. Y. Srinivasan | f8feed0 | 2011-05-10 07:54:24 -0700 | [diff] [blame] | 1552 | STORVSC_MAX_IO_REQUESTS) |
K. Y. Srinivasan | b06efc1 | 2011-08-25 09:49:10 -0700 | [diff] [blame] | 1553 | return -EINVAL; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1554 | |
Greg Kroah-Hartman | 768fa21 | 2011-08-25 15:07:32 -0700 | [diff] [blame] | 1555 | return vmbus_driver_register(&storvsc_drv); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1556 | } |
| 1557 | |
K. Y. Srinivasan | c63ba9e | 2011-06-06 15:49:26 -0700 | [diff] [blame] | 1558 | static void __exit storvsc_drv_exit(void) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1559 | { |
Greg Kroah-Hartman | 768fa21 | 2011-08-25 15:07:32 -0700 | [diff] [blame] | 1560 | vmbus_driver_unregister(&storvsc_drv); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1561 | } |
| 1562 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 1563 | MODULE_LICENSE("GPL"); |
Hank Janssen | 26c14cc | 2010-02-11 23:02:42 +0000 | [diff] [blame] | 1564 | MODULE_VERSION(HV_DRV_VERSION); |
Stephen Hemminger | 3afc7cc3 | 2010-05-06 21:44:45 -0700 | [diff] [blame] | 1565 | MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver"); |
K. Y. Srinivasan | d9bbae8 | 2011-06-06 15:49:27 -0700 | [diff] [blame] | 1566 | module_init(storvsc_drv_init); |
K. Y. Srinivasan | c63ba9e | 2011-06-06 15:49:26 -0700 | [diff] [blame] | 1567 | module_exit(storvsc_drv_exit); |