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