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