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