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