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