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