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