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