]>
Commit | Line | Data |
---|---|---|
bef4a34a | 1 | /* |
bef4a34a HJ |
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> | |
972621c9 | 20 | * K. Y. Srinivasan <kys@microsoft.com> |
bef4a34a | 21 | */ |
a1be1706 S |
22 | |
23 | #include <linux/kernel.h> | |
f0d79fe9 | 24 | #include <linux/wait.h> |
a1be1706 S |
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> | |
bef4a34a | 30 | #include <linux/init.h> |
5a0e3ad6 | 31 | #include <linux/slab.h> |
bef4a34a HJ |
32 | #include <linux/module.h> |
33 | #include <linux/device.h> | |
46a97191 | 34 | #include <linux/hyperv.h> |
56b26e69 | 35 | #include <linux/blkdev.h> |
bef4a34a HJ |
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> | |
bef4a34a | 43 | #include <scsi/scsi_dbg.h> |
3f335ea2 | 44 | |
09f0355f | 45 | /* |
af9584b8 S |
46 | * All wire protocol details (storage protocol between the guest and the host) |
47 | * are consolidated here. | |
48 | * | |
49 | * Begin protocol definitions. | |
09f0355f S |
50 | */ |
51 | ||
09f0355f S |
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 | |
8b612fa2 | 58 | * Win8: 5.1 |
1a363108 KM |
59 | * Win8.1: 6.0 |
60 | * Win10: 6.2 | |
09f0355f S |
61 | */ |
62 | ||
2492fd7a KM |
63 | #define VMSTOR_PROTO_VERSION(MAJOR_, MINOR_) ((((MAJOR_) & 0xff) << 8) | \ |
64 | (((MINOR_) & 0xff))) | |
8b612fa2 | 65 | |
1a363108 | 66 | #define VMSTOR_PROTO_VERSION_WIN6 VMSTOR_PROTO_VERSION(2, 0) |
2492fd7a KM |
67 | #define VMSTOR_PROTO_VERSION_WIN7 VMSTOR_PROTO_VERSION(4, 2) |
68 | #define VMSTOR_PROTO_VERSION_WIN8 VMSTOR_PROTO_VERSION(5, 1) | |
1a363108 KM |
69 | #define VMSTOR_PROTO_VERSION_WIN8_1 VMSTOR_PROTO_VERSION(6, 0) |
70 | #define VMSTOR_PROTO_VERSION_WIN10 VMSTOR_PROTO_VERSION(6, 2) | |
f0d79fe9 | 71 | |
f0d79fe9 S |
72 | /* Packet structure describing virtual storage requests. */ |
73 | enum vstor_packet_operation { | |
74 | VSTOR_OPERATION_COMPLETE_IO = 1, | |
75 | VSTOR_OPERATION_REMOVE_DEVICE = 2, | |
76 | VSTOR_OPERATION_EXECUTE_SRB = 3, | |
77 | VSTOR_OPERATION_RESET_LUN = 4, | |
78 | VSTOR_OPERATION_RESET_ADAPTER = 5, | |
79 | VSTOR_OPERATION_RESET_BUS = 6, | |
80 | VSTOR_OPERATION_BEGIN_INITIALIZATION = 7, | |
81 | VSTOR_OPERATION_END_INITIALIZATION = 8, | |
82 | VSTOR_OPERATION_QUERY_PROTOCOL_VERSION = 9, | |
83 | VSTOR_OPERATION_QUERY_PROPERTIES = 10, | |
2b9525f5 | 84 | VSTOR_OPERATION_ENUMERATE_BUS = 11, |
8b612fa2 S |
85 | VSTOR_OPERATION_FCHBA_DATA = 12, |
86 | VSTOR_OPERATION_CREATE_SUB_CHANNELS = 13, | |
87 | VSTOR_OPERATION_MAXIMUM = 13 | |
88 | }; | |
89 | ||
90 | /* | |
91 | * WWN packet for Fibre Channel HBA | |
92 | */ | |
93 | ||
94 | struct hv_fc_wwn_packet { | |
95 | bool primary_active; | |
96 | u8 reserved1; | |
97 | u8 reserved2; | |
98 | u8 primary_port_wwn[8]; | |
99 | u8 primary_node_wwn[8]; | |
100 | u8 secondary_port_wwn[8]; | |
101 | u8 secondary_node_wwn[8]; | |
f0d79fe9 S |
102 | }; |
103 | ||
8b612fa2 S |
104 | |
105 | ||
106 | /* | |
107 | * SRB Flag Bits | |
108 | */ | |
109 | ||
110 | #define SRB_FLAGS_QUEUE_ACTION_ENABLE 0x00000002 | |
111 | #define SRB_FLAGS_DISABLE_DISCONNECT 0x00000004 | |
112 | #define SRB_FLAGS_DISABLE_SYNCH_TRANSFER 0x00000008 | |
113 | #define SRB_FLAGS_BYPASS_FROZEN_QUEUE 0x00000010 | |
114 | #define SRB_FLAGS_DISABLE_AUTOSENSE 0x00000020 | |
115 | #define SRB_FLAGS_DATA_IN 0x00000040 | |
116 | #define SRB_FLAGS_DATA_OUT 0x00000080 | |
117 | #define SRB_FLAGS_NO_DATA_TRANSFER 0x00000000 | |
118 | #define SRB_FLAGS_UNSPECIFIED_DIRECTION (SRB_FLAGS_DATA_IN | SRB_FLAGS_DATA_OUT) | |
119 | #define SRB_FLAGS_NO_QUEUE_FREEZE 0x00000100 | |
120 | #define SRB_FLAGS_ADAPTER_CACHE_ENABLE 0x00000200 | |
121 | #define SRB_FLAGS_FREE_SENSE_BUFFER 0x00000400 | |
122 | ||
123 | /* | |
124 | * This flag indicates the request is part of the workflow for processing a D3. | |
125 | */ | |
126 | #define SRB_FLAGS_D3_PROCESSING 0x00000800 | |
127 | #define SRB_FLAGS_IS_ACTIVE 0x00010000 | |
128 | #define SRB_FLAGS_ALLOCATED_FROM_ZONE 0x00020000 | |
129 | #define SRB_FLAGS_SGLIST_FROM_POOL 0x00040000 | |
130 | #define SRB_FLAGS_BYPASS_LOCKED_QUEUE 0x00080000 | |
131 | #define SRB_FLAGS_NO_KEEP_AWAKE 0x00100000 | |
132 | #define SRB_FLAGS_PORT_DRIVER_ALLOCSENSE 0x00200000 | |
133 | #define SRB_FLAGS_PORT_DRIVER_SENSEHASPORT 0x00400000 | |
134 | #define SRB_FLAGS_DONT_START_NEXT_PACKET 0x00800000 | |
135 | #define SRB_FLAGS_PORT_DRIVER_RESERVED 0x0F000000 | |
136 | #define SRB_FLAGS_CLASS_DRIVER_RESERVED 0xF0000000 | |
137 | ||
138 | ||
f0d79fe9 S |
139 | /* |
140 | * Platform neutral description of a scsi request - | |
141 | * this remains the same across the write regardless of 32/64 bit | |
142 | * note: it's patterned off the SCSI_PASS_THROUGH structure | |
143 | */ | |
6b2f9495 | 144 | #define STORVSC_MAX_CMD_LEN 0x10 |
8b612fa2 S |
145 | |
146 | #define POST_WIN7_STORVSC_SENSE_BUFFER_SIZE 0x14 | |
147 | #define PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE 0x12 | |
148 | ||
149 | #define STORVSC_SENSE_BUFFER_SIZE 0x14 | |
6b2f9495 | 150 | #define STORVSC_MAX_BUF_LEN_WITH_PADDING 0x14 |
f0d79fe9 | 151 | |
8b612fa2 S |
152 | /* |
153 | * Sense buffer size changed in win8; have a run-time | |
cb11fead KM |
154 | * variable to track the size we should use. This value will |
155 | * likely change during protocol negotiation but it is valid | |
156 | * to start by assuming pre-Win8. | |
8b612fa2 | 157 | */ |
cb11fead | 158 | static int sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE; |
8b612fa2 S |
159 | |
160 | /* | |
cb11fead KM |
161 | * The storage protocol version is determined during the |
162 | * initial exchange with the host. It will indicate which | |
163 | * storage functionality is available in the host. | |
164 | */ | |
2492fd7a | 165 | static int vmstor_proto_version; |
8b612fa2 S |
166 | |
167 | struct vmscsi_win8_extension { | |
168 | /* | |
169 | * The following were added in Windows 8 | |
170 | */ | |
171 | u16 reserve; | |
172 | u8 queue_tag; | |
173 | u8 queue_action; | |
174 | u32 srb_flags; | |
175 | u32 time_out_value; | |
176 | u32 queue_sort_ey; | |
177 | } __packed; | |
178 | ||
f0d79fe9 | 179 | struct vmscsi_request { |
c649114a S |
180 | u16 length; |
181 | u8 srb_status; | |
182 | u8 scsi_status; | |
f0d79fe9 | 183 | |
c649114a S |
184 | u8 port_number; |
185 | u8 path_id; | |
186 | u8 target_id; | |
187 | u8 lun; | |
f0d79fe9 | 188 | |
c649114a S |
189 | u8 cdb_length; |
190 | u8 sense_info_length; | |
191 | u8 data_in; | |
192 | u8 reserved; | |
f0d79fe9 | 193 | |
c649114a | 194 | u32 data_transfer_length; |
f0d79fe9 S |
195 | |
196 | union { | |
6b2f9495 S |
197 | u8 cdb[STORVSC_MAX_CMD_LEN]; |
198 | u8 sense_data[STORVSC_SENSE_BUFFER_SIZE]; | |
199 | u8 reserved_array[STORVSC_MAX_BUF_LEN_WITH_PADDING]; | |
f0d79fe9 | 200 | }; |
8b612fa2 S |
201 | /* |
202 | * The following was added in win8. | |
203 | */ | |
204 | struct vmscsi_win8_extension win8_extension; | |
205 | ||
f0d79fe9 S |
206 | } __attribute((packed)); |
207 | ||
208 | ||
cb11fead KM |
209 | /* |
210 | * The size of the vmscsi_request has changed in win8. The | |
211 | * additional size is because of new elements added to the | |
212 | * structure. These elements are valid only when we are talking | |
213 | * to a win8 host. | |
214 | * Track the correction to size we need to apply. This value | |
215 | * will likely change during protocol negotiation but it is | |
216 | * valid to start by assuming pre-Win8. | |
217 | */ | |
218 | static int vmscsi_size_delta = sizeof(struct vmscsi_win8_extension); | |
219 | ||
1a363108 KM |
220 | /* |
221 | * The list of storage protocols in order of preference. | |
222 | */ | |
223 | struct vmstor_protocol { | |
224 | int protocol_version; | |
225 | int sense_buffer_size; | |
226 | int vmscsi_size_delta; | |
227 | }; | |
228 | ||
229 | ||
230 | static const struct vmstor_protocol vmstor_protocols[] = { | |
231 | { | |
232 | VMSTOR_PROTO_VERSION_WIN10, | |
233 | POST_WIN7_STORVSC_SENSE_BUFFER_SIZE, | |
234 | 0 | |
235 | }, | |
236 | { | |
237 | VMSTOR_PROTO_VERSION_WIN8_1, | |
238 | POST_WIN7_STORVSC_SENSE_BUFFER_SIZE, | |
239 | 0 | |
240 | }, | |
241 | { | |
242 | VMSTOR_PROTO_VERSION_WIN8, | |
243 | POST_WIN7_STORVSC_SENSE_BUFFER_SIZE, | |
244 | 0 | |
245 | }, | |
246 | { | |
247 | VMSTOR_PROTO_VERSION_WIN7, | |
248 | PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE, | |
249 | sizeof(struct vmscsi_win8_extension), | |
250 | }, | |
251 | { | |
252 | VMSTOR_PROTO_VERSION_WIN6, | |
253 | PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE, | |
254 | sizeof(struct vmscsi_win8_extension), | |
255 | } | |
256 | }; | |
257 | ||
258 | ||
f0d79fe9 S |
259 | /* |
260 | * This structure is sent during the intialization phase to get the different | |
261 | * properties of the channel. | |
262 | */ | |
8b612fa2 S |
263 | |
264 | #define STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL 0x1 | |
265 | ||
f0d79fe9 | 266 | struct vmstorage_channel_properties { |
8b612fa2 S |
267 | u32 reserved; |
268 | u16 max_channel_cnt; | |
269 | u16 reserved1; | |
f0d79fe9 | 270 | |
8b612fa2 | 271 | u32 flags; |
c649114a | 272 | u32 max_transfer_bytes; |
f0d79fe9 | 273 | |
8b612fa2 | 274 | u64 reserved2; |
f0d79fe9 S |
275 | } __packed; |
276 | ||
277 | /* This structure is sent during the storage protocol negotiations. */ | |
278 | struct vmstorage_protocol_version { | |
279 | /* Major (MSW) and minor (LSW) version numbers. */ | |
85904a5e | 280 | u16 major_minor; |
f0d79fe9 S |
281 | |
282 | /* | |
283 | * Revision number is auto-incremented whenever this file is changed | |
284 | * (See FILL_VMSTOR_REVISION macro above). Mismatch does not | |
285 | * definitely indicate incompatibility--but it does indicate mismatched | |
286 | * builds. | |
c649114a | 287 | * This is only used on the windows side. Just set it to 0. |
f0d79fe9 | 288 | */ |
85904a5e | 289 | u16 revision; |
f0d79fe9 S |
290 | } __packed; |
291 | ||
292 | /* Channel Property Flags */ | |
293 | #define STORAGE_CHANNEL_REMOVABLE_FLAG 0x1 | |
294 | #define STORAGE_CHANNEL_EMULATED_IDE_FLAG 0x2 | |
295 | ||
296 | struct vstor_packet { | |
297 | /* Requested operation type */ | |
298 | enum vstor_packet_operation operation; | |
299 | ||
300 | /* Flags - see below for values */ | |
c649114a | 301 | u32 flags; |
f0d79fe9 S |
302 | |
303 | /* Status of the request returned from the server side. */ | |
c649114a | 304 | u32 status; |
f0d79fe9 S |
305 | |
306 | /* Data payload area */ | |
307 | union { | |
308 | /* | |
309 | * Structure used to forward SCSI commands from the | |
310 | * client to the server. | |
311 | */ | |
312 | struct vmscsi_request vm_srb; | |
313 | ||
314 | /* Structure used to query channel properties. */ | |
315 | struct vmstorage_channel_properties storage_channel_properties; | |
316 | ||
317 | /* Used during version negotiations. */ | |
318 | struct vmstorage_protocol_version version; | |
8b612fa2 S |
319 | |
320 | /* Fibre channel address packet */ | |
321 | struct hv_fc_wwn_packet wwn_packet; | |
322 | ||
323 | /* Number of sub-channels to create */ | |
324 | u16 sub_channel_count; | |
325 | ||
326 | /* This will be the maximum of the union members */ | |
327 | u8 buffer[0x34]; | |
f0d79fe9 S |
328 | }; |
329 | } __packed; | |
330 | ||
f0d79fe9 | 331 | /* |
09f0355f S |
332 | * Packet Flags: |
333 | * | |
f0d79fe9 S |
334 | * This flag indicates that the server should send back a completion for this |
335 | * packet. | |
336 | */ | |
09f0355f | 337 | |
f0d79fe9 S |
338 | #define REQUEST_COMPLETION_FLAG 0x1 |
339 | ||
f0d79fe9 S |
340 | /* Matches Windows-end */ |
341 | enum storvsc_request_type { | |
c649114a | 342 | WRITE_TYPE = 0, |
f0d79fe9 S |
343 | READ_TYPE, |
344 | UNKNOWN_TYPE, | |
345 | }; | |
346 | ||
16046320 S |
347 | /* |
348 | * SRB status codes and masks; a subset of the codes used here. | |
349 | */ | |
350 | ||
351 | #define SRB_STATUS_AUTOSENSE_VALID 0x80 | |
352 | #define SRB_STATUS_INVALID_LUN 0x20 | |
353 | #define SRB_STATUS_SUCCESS 0x01 | |
6781209e | 354 | #define SRB_STATUS_ABORTED 0x02 |
16046320 S |
355 | #define SRB_STATUS_ERROR 0x04 |
356 | ||
af9584b8 S |
357 | /* |
358 | * This is the end of Protocol specific defines. | |
359 | */ | |
360 | ||
b9ec3a55 | 361 | static int storvsc_ringbuffer_size = (256 * PAGE_SIZE); |
f458aada S |
362 | static u32 max_outstanding_req_per_channel; |
363 | ||
364 | static int storvsc_vcpus_per_sub_channel = 4; | |
af9584b8 S |
365 | |
366 | module_param(storvsc_ringbuffer_size, int, S_IRUGO); | |
367 | MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)"); | |
368 | ||
f458aada S |
369 | module_param(storvsc_vcpus_per_sub_channel, int, S_IRUGO); |
370 | MODULE_PARM_DESC(vcpus_per_sub_channel, "Ratio of VCPUs to subchannels"); | |
893def38 S |
371 | /* |
372 | * Timeout in seconds for all devices managed by this driver. | |
373 | */ | |
374 | static int storvsc_timeout = 180; | |
375 | ||
f3cfabce S |
376 | static int msft_blist_flags = BLIST_TRY_VPD_PAGES; |
377 | ||
af9584b8 | 378 | |
6f94d5de S |
379 | static void storvsc_on_channel_callback(void *context); |
380 | ||
4cd83ecd S |
381 | #define STORVSC_MAX_LUNS_PER_TARGET 255 |
382 | #define STORVSC_MAX_TARGETS 2 | |
383 | #define STORVSC_MAX_CHANNELS 8 | |
af9584b8 | 384 | |
4cd83ecd S |
385 | #define STORVSC_FC_MAX_LUNS_PER_TARGET 255 |
386 | #define STORVSC_FC_MAX_TARGETS 128 | |
387 | #define STORVSC_FC_MAX_CHANNELS 8 | |
af9584b8 | 388 | |
4cd83ecd S |
389 | #define STORVSC_IDE_MAX_LUNS_PER_TARGET 64 |
390 | #define STORVSC_IDE_MAX_TARGETS 1 | |
391 | #define STORVSC_IDE_MAX_CHANNELS 1 | |
16046320 | 392 | |
61eaffc9 | 393 | struct storvsc_cmd_request { |
61eaffc9 S |
394 | struct scsi_cmnd *cmd; |
395 | ||
396 | unsigned int bounce_sgl_count; | |
397 | struct scatterlist *bounce_sgl; | |
f0d79fe9 | 398 | |
f0d79fe9 S |
399 | struct hv_device *device; |
400 | ||
401 | /* Synchronize the request/response if needed */ | |
402 | struct completion wait_event; | |
403 | ||
be0cf6ca S |
404 | struct vmbus_channel_packet_multipage_buffer mpb; |
405 | struct vmbus_packet_mpb_array *payload; | |
406 | u32 payload_sz; | |
407 | ||
f0d79fe9 S |
408 | struct vstor_packet vstor_packet; |
409 | }; | |
410 | ||
411 | ||
f0d79fe9 S |
412 | /* A storvsc device is a device object that contains a vmbus channel */ |
413 | struct storvsc_device { | |
414 | struct hv_device *device; | |
415 | ||
416 | bool destroy; | |
417 | bool drain_notify; | |
6f94d5de | 418 | bool open_sub_channel; |
f0d79fe9 | 419 | atomic_t num_outstanding_req; |
cd654ea1 | 420 | struct Scsi_Host *host; |
f0d79fe9 S |
421 | |
422 | wait_queue_head_t waiting_to_drain; | |
423 | ||
424 | /* | |
425 | * Each unique Port/Path/Target represents 1 channel ie scsi | |
426 | * controller. In reality, the pathid, targetid is always 0 | |
427 | * and the port is set by us | |
428 | */ | |
429 | unsigned int port_number; | |
430 | unsigned char path_id; | |
431 | unsigned char target_id; | |
432 | ||
5117b936 S |
433 | /* |
434 | * Max I/O, the device can support. | |
435 | */ | |
436 | u32 max_transfer_bytes; | |
f0d79fe9 | 437 | /* Used for vsc/vsp channel reset process */ |
61eaffc9 S |
438 | struct storvsc_cmd_request init_request; |
439 | struct storvsc_cmd_request reset_request; | |
f0d79fe9 S |
440 | }; |
441 | ||
ce3e301c S |
442 | struct hv_host_device { |
443 | struct hv_device *dev; | |
c1b3d067 S |
444 | unsigned int port; |
445 | unsigned char path; | |
446 | unsigned char target; | |
447 | }; | |
448 | ||
12675799 S |
449 | struct storvsc_scan_work { |
450 | struct work_struct work; | |
451 | struct Scsi_Host *host; | |
452 | uint lun; | |
453 | }; | |
454 | ||
6781209e S |
455 | static void storvsc_device_scan(struct work_struct *work) |
456 | { | |
457 | struct storvsc_scan_work *wrk; | |
458 | uint lun; | |
459 | struct scsi_device *sdev; | |
460 | ||
461 | wrk = container_of(work, struct storvsc_scan_work, work); | |
462 | lun = wrk->lun; | |
463 | ||
464 | sdev = scsi_device_lookup(wrk->host, 0, 0, lun); | |
465 | if (!sdev) | |
466 | goto done; | |
467 | scsi_rescan_device(&sdev->sdev_gendev); | |
468 | scsi_device_put(sdev); | |
469 | ||
470 | done: | |
471 | kfree(wrk); | |
472 | } | |
473 | ||
2a09ed3d | 474 | static void storvsc_host_scan(struct work_struct *work) |
12675799 S |
475 | { |
476 | struct storvsc_scan_work *wrk; | |
2a09ed3d | 477 | struct Scsi_Host *host; |
34a716bc | 478 | struct scsi_device *sdev; |
12675799 S |
479 | |
480 | wrk = container_of(work, struct storvsc_scan_work, work); | |
2a09ed3d S |
481 | host = wrk->host; |
482 | ||
34a716bc S |
483 | /* |
484 | * Before scanning the host, first check to see if any of the | |
485 | * currrently known devices have been hot removed. We issue a | |
486 | * "unit ready" command against all currently known devices. | |
487 | * This I/O will result in an error for devices that have been | |
488 | * removed. As part of handling the I/O error, we remove the device. | |
489 | * | |
490 | * When a LUN is added or removed, the host sends us a signal to | |
491 | * scan the host. Thus we are forced to discover the LUNs that | |
492 | * may have been removed this way. | |
493 | */ | |
494 | mutex_lock(&host->scan_mutex); | |
8d6a9f56 | 495 | shost_for_each_device(sdev, host) |
34a716bc | 496 | scsi_test_unit_ready(sdev, 1, 1, NULL); |
34a716bc S |
497 | mutex_unlock(&host->scan_mutex); |
498 | /* | |
499 | * Now scan the host to discover LUNs that may have been added. | |
500 | */ | |
2a09ed3d S |
501 | scsi_scan_host(host); |
502 | ||
12675799 S |
503 | kfree(wrk); |
504 | } | |
505 | ||
b4017319 S |
506 | static void storvsc_remove_lun(struct work_struct *work) |
507 | { | |
508 | struct storvsc_scan_work *wrk; | |
509 | struct scsi_device *sdev; | |
510 | ||
511 | wrk = container_of(work, struct storvsc_scan_work, work); | |
512 | if (!scsi_host_get(wrk->host)) | |
513 | goto done; | |
514 | ||
515 | sdev = scsi_device_lookup(wrk->host, 0, 0, wrk->lun); | |
516 | ||
517 | if (sdev) { | |
518 | scsi_remove_device(sdev); | |
519 | scsi_device_put(sdev); | |
520 | } | |
521 | scsi_host_put(wrk->host); | |
522 | ||
523 | done: | |
524 | kfree(wrk); | |
525 | } | |
526 | ||
af9584b8 | 527 | |
a8c18c57 S |
528 | /* |
529 | * We can get incoming messages from the host that are not in response to | |
530 | * messages that we have sent out. An example of this would be messages | |
531 | * received by the guest to notify dynamic addition/removal of LUNs. To | |
532 | * deal with potential race conditions where the driver may be in the | |
533 | * midst of being unloaded when we might receive an unsolicited message | |
534 | * from the host, we have implemented a mechanism to gurantee sequential | |
535 | * consistency: | |
536 | * | |
537 | * 1) Once the device is marked as being destroyed, we will fail all | |
538 | * outgoing messages. | |
539 | * 2) We permit incoming messages when the device is being destroyed, | |
540 | * only to properly account for messages already sent out. | |
541 | */ | |
542 | ||
f0d79fe9 S |
543 | static inline struct storvsc_device *get_out_stor_device( |
544 | struct hv_device *device) | |
545 | { | |
546 | struct storvsc_device *stor_device; | |
547 | ||
cd654ea1 | 548 | stor_device = hv_get_drvdata(device); |
f0d79fe9 S |
549 | |
550 | if (stor_device && stor_device->destroy) | |
551 | stor_device = NULL; | |
552 | ||
553 | return stor_device; | |
554 | } | |
555 | ||
556 | ||
557 | static inline void storvsc_wait_to_drain(struct storvsc_device *dev) | |
558 | { | |
559 | dev->drain_notify = true; | |
560 | wait_event(dev->waiting_to_drain, | |
561 | atomic_read(&dev->num_outstanding_req) == 0); | |
562 | dev->drain_notify = false; | |
563 | } | |
bef4a34a | 564 | |
8dcf37d4 S |
565 | static inline struct storvsc_device *get_in_stor_device( |
566 | struct hv_device *device) | |
567 | { | |
568 | struct storvsc_device *stor_device; | |
8dcf37d4 | 569 | |
cd654ea1 | 570 | stor_device = hv_get_drvdata(device); |
8dcf37d4 S |
571 | |
572 | if (!stor_device) | |
573 | goto get_in_err; | |
574 | ||
575 | /* | |
576 | * If the device is being destroyed; allow incoming | |
577 | * traffic only to cleanup outstanding requests. | |
578 | */ | |
579 | ||
580 | if (stor_device->destroy && | |
581 | (atomic_read(&stor_device->num_outstanding_req) == 0)) | |
582 | stor_device = NULL; | |
583 | ||
584 | get_in_err: | |
8dcf37d4 S |
585 | return stor_device; |
586 | ||
587 | } | |
588 | ||
2707388c S |
589 | static void destroy_bounce_buffer(struct scatterlist *sgl, |
590 | unsigned int sg_count) | |
591 | { | |
592 | int i; | |
593 | struct page *page_buf; | |
594 | ||
595 | for (i = 0; i < sg_count; i++) { | |
596 | page_buf = sg_page((&sgl[i])); | |
597 | if (page_buf != NULL) | |
598 | __free_page(page_buf); | |
599 | } | |
600 | ||
601 | kfree(sgl); | |
602 | } | |
603 | ||
604 | static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count) | |
605 | { | |
606 | int i; | |
607 | ||
608 | /* No need to check */ | |
609 | if (sg_count < 2) | |
610 | return -1; | |
611 | ||
612 | /* We have at least 2 sg entries */ | |
613 | for (i = 0; i < sg_count; i++) { | |
614 | if (i == 0) { | |
615 | /* make sure 1st one does not have hole */ | |
616 | if (sgl[i].offset + sgl[i].length != PAGE_SIZE) | |
617 | return i; | |
618 | } else if (i == sg_count - 1) { | |
619 | /* make sure last one does not have hole */ | |
620 | if (sgl[i].offset != 0) | |
621 | return i; | |
622 | } else { | |
623 | /* make sure no hole in the middle */ | |
624 | if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0) | |
625 | return i; | |
626 | } | |
627 | } | |
628 | return -1; | |
629 | } | |
630 | ||
631 | static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl, | |
632 | unsigned int sg_count, | |
633 | unsigned int len, | |
634 | int write) | |
635 | { | |
636 | int i; | |
637 | int num_pages; | |
638 | struct scatterlist *bounce_sgl; | |
639 | struct page *page_buf; | |
640 | unsigned int buf_len = ((write == WRITE_TYPE) ? 0 : PAGE_SIZE); | |
641 | ||
642 | num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT; | |
643 | ||
644 | bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC); | |
645 | if (!bounce_sgl) | |
646 | return NULL; | |
647 | ||
9d2696e6 | 648 | sg_init_table(bounce_sgl, num_pages); |
2707388c S |
649 | for (i = 0; i < num_pages; i++) { |
650 | page_buf = alloc_page(GFP_ATOMIC); | |
651 | if (!page_buf) | |
652 | goto cleanup; | |
653 | sg_set_page(&bounce_sgl[i], page_buf, buf_len, 0); | |
654 | } | |
655 | ||
656 | return bounce_sgl; | |
657 | ||
658 | cleanup: | |
659 | destroy_bounce_buffer(bounce_sgl, num_pages); | |
660 | return NULL; | |
661 | } | |
662 | ||
663 | /* Assume the original sgl has enough room */ | |
664 | static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl, | |
665 | struct scatterlist *bounce_sgl, | |
666 | unsigned int orig_sgl_count, | |
667 | unsigned int bounce_sgl_count) | |
668 | { | |
669 | int i; | |
670 | int j = 0; | |
671 | unsigned long src, dest; | |
672 | unsigned int srclen, destlen, copylen; | |
673 | unsigned int total_copied = 0; | |
674 | unsigned long bounce_addr = 0; | |
675 | unsigned long dest_addr = 0; | |
676 | unsigned long flags; | |
aaced993 S |
677 | struct scatterlist *cur_dest_sgl; |
678 | struct scatterlist *cur_src_sgl; | |
2707388c S |
679 | |
680 | local_irq_save(flags); | |
aaced993 S |
681 | cur_dest_sgl = orig_sgl; |
682 | cur_src_sgl = bounce_sgl; | |
2707388c | 683 | for (i = 0; i < orig_sgl_count; i++) { |
aaced993 S |
684 | dest_addr = (unsigned long) |
685 | kmap_atomic(sg_page(cur_dest_sgl)) + | |
686 | cur_dest_sgl->offset; | |
2707388c | 687 | dest = dest_addr; |
aaced993 | 688 | destlen = cur_dest_sgl->length; |
2707388c S |
689 | |
690 | if (bounce_addr == 0) | |
aaced993 S |
691 | bounce_addr = (unsigned long)kmap_atomic( |
692 | sg_page(cur_src_sgl)); | |
2707388c S |
693 | |
694 | while (destlen) { | |
aaced993 S |
695 | src = bounce_addr + cur_src_sgl->offset; |
696 | srclen = cur_src_sgl->length - cur_src_sgl->offset; | |
2707388c S |
697 | |
698 | copylen = min(srclen, destlen); | |
699 | memcpy((void *)dest, (void *)src, copylen); | |
700 | ||
701 | total_copied += copylen; | |
aaced993 | 702 | cur_src_sgl->offset += copylen; |
2707388c S |
703 | destlen -= copylen; |
704 | dest += copylen; | |
705 | ||
aaced993 | 706 | if (cur_src_sgl->offset == cur_src_sgl->length) { |
2707388c | 707 | /* full */ |
aaced993 | 708 | kunmap_atomic((void *)bounce_addr); |
2707388c S |
709 | j++; |
710 | ||
711 | /* | |
712 | * It is possible that the number of elements | |
713 | * in the bounce buffer may not be equal to | |
714 | * the number of elements in the original | |
715 | * scatter list. Handle this correctly. | |
716 | */ | |
717 | ||
718 | if (j == bounce_sgl_count) { | |
719 | /* | |
720 | * We are done; cleanup and return. | |
721 | */ | |
aaced993 S |
722 | kunmap_atomic((void *)(dest_addr - |
723 | cur_dest_sgl->offset)); | |
2707388c S |
724 | local_irq_restore(flags); |
725 | return total_copied; | |
726 | } | |
727 | ||
728 | /* if we need to use another bounce buffer */ | |
aaced993 S |
729 | if (destlen || i != orig_sgl_count - 1) { |
730 | cur_src_sgl = sg_next(cur_src_sgl); | |
731 | bounce_addr = (unsigned long) | |
732 | kmap_atomic( | |
733 | sg_page(cur_src_sgl)); | |
734 | } | |
2707388c S |
735 | } else if (destlen == 0 && i == orig_sgl_count - 1) { |
736 | /* unmap the last bounce that is < PAGE_SIZE */ | |
aaced993 | 737 | kunmap_atomic((void *)bounce_addr); |
2707388c S |
738 | } |
739 | } | |
740 | ||
aaced993 S |
741 | kunmap_atomic((void *)(dest_addr - cur_dest_sgl->offset)); |
742 | cur_dest_sgl = sg_next(cur_dest_sgl); | |
2707388c S |
743 | } |
744 | ||
745 | local_irq_restore(flags); | |
746 | ||
747 | return total_copied; | |
748 | } | |
749 | ||
750 | /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */ | |
751 | static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl, | |
752 | struct scatterlist *bounce_sgl, | |
753 | unsigned int orig_sgl_count) | |
754 | { | |
755 | int i; | |
756 | int j = 0; | |
757 | unsigned long src, dest; | |
758 | unsigned int srclen, destlen, copylen; | |
759 | unsigned int total_copied = 0; | |
760 | unsigned long bounce_addr = 0; | |
761 | unsigned long src_addr = 0; | |
762 | unsigned long flags; | |
aaced993 S |
763 | struct scatterlist *cur_src_sgl; |
764 | struct scatterlist *cur_dest_sgl; | |
2707388c S |
765 | |
766 | local_irq_save(flags); | |
767 | ||
aaced993 S |
768 | cur_src_sgl = orig_sgl; |
769 | cur_dest_sgl = bounce_sgl; | |
770 | ||
2707388c | 771 | for (i = 0; i < orig_sgl_count; i++) { |
aaced993 S |
772 | src_addr = (unsigned long) |
773 | kmap_atomic(sg_page(cur_src_sgl)) + | |
774 | cur_src_sgl->offset; | |
2707388c | 775 | src = src_addr; |
aaced993 | 776 | srclen = cur_src_sgl->length; |
2707388c S |
777 | |
778 | if (bounce_addr == 0) | |
aaced993 S |
779 | bounce_addr = (unsigned long) |
780 | kmap_atomic(sg_page(cur_dest_sgl)); | |
2707388c S |
781 | |
782 | while (srclen) { | |
783 | /* assume bounce offset always == 0 */ | |
aaced993 S |
784 | dest = bounce_addr + cur_dest_sgl->length; |
785 | destlen = PAGE_SIZE - cur_dest_sgl->length; | |
2707388c S |
786 | |
787 | copylen = min(srclen, destlen); | |
788 | memcpy((void *)dest, (void *)src, copylen); | |
789 | ||
790 | total_copied += copylen; | |
aaced993 | 791 | cur_dest_sgl->length += copylen; |
2707388c S |
792 | srclen -= copylen; |
793 | src += copylen; | |
794 | ||
aaced993 | 795 | if (cur_dest_sgl->length == PAGE_SIZE) { |
2707388c | 796 | /* full..move to next entry */ |
aaced993 | 797 | kunmap_atomic((void *)bounce_addr); |
8de58074 | 798 | bounce_addr = 0; |
2707388c | 799 | j++; |
8de58074 | 800 | } |
2707388c | 801 | |
8de58074 | 802 | /* if we need to use another bounce buffer */ |
aaced993 S |
803 | if (srclen && bounce_addr == 0) { |
804 | cur_dest_sgl = sg_next(cur_dest_sgl); | |
805 | bounce_addr = (unsigned long) | |
806 | kmap_atomic( | |
807 | sg_page(cur_dest_sgl)); | |
808 | } | |
2707388c | 809 | |
2707388c S |
810 | } |
811 | ||
aaced993 S |
812 | kunmap_atomic((void *)(src_addr - cur_src_sgl->offset)); |
813 | cur_src_sgl = sg_next(cur_src_sgl); | |
2707388c S |
814 | } |
815 | ||
8de58074 | 816 | if (bounce_addr) |
aaced993 | 817 | kunmap_atomic((void *)bounce_addr); |
8de58074 | 818 | |
2707388c S |
819 | local_irq_restore(flags); |
820 | ||
821 | return total_copied; | |
822 | } | |
823 | ||
6f94d5de S |
824 | static void handle_sc_creation(struct vmbus_channel *new_sc) |
825 | { | |
826 | struct hv_device *device = new_sc->primary_channel->device_obj; | |
827 | struct storvsc_device *stor_device; | |
828 | struct vmstorage_channel_properties props; | |
829 | ||
830 | stor_device = get_out_stor_device(device); | |
831 | if (!stor_device) | |
832 | return; | |
833 | ||
834 | if (stor_device->open_sub_channel == false) | |
835 | return; | |
836 | ||
837 | memset(&props, 0, sizeof(struct vmstorage_channel_properties)); | |
838 | ||
839 | vmbus_open(new_sc, | |
840 | storvsc_ringbuffer_size, | |
841 | storvsc_ringbuffer_size, | |
842 | (void *)&props, | |
843 | sizeof(struct vmstorage_channel_properties), | |
844 | storvsc_on_channel_callback, new_sc); | |
845 | } | |
846 | ||
847 | static void handle_multichannel_storage(struct hv_device *device, int max_chns) | |
848 | { | |
849 | struct storvsc_device *stor_device; | |
850 | int num_cpus = num_online_cpus(); | |
851 | int num_sc; | |
852 | struct storvsc_cmd_request *request; | |
853 | struct vstor_packet *vstor_packet; | |
854 | int ret, t; | |
855 | ||
856 | num_sc = ((max_chns > num_cpus) ? num_cpus : max_chns); | |
857 | stor_device = get_out_stor_device(device); | |
858 | if (!stor_device) | |
859 | return; | |
860 | ||
861 | request = &stor_device->init_request; | |
862 | vstor_packet = &request->vstor_packet; | |
863 | ||
864 | stor_device->open_sub_channel = true; | |
865 | /* | |
866 | * Establish a handler for dealing with subchannels. | |
867 | */ | |
868 | vmbus_set_sc_create_callback(device->channel, handle_sc_creation); | |
869 | ||
870 | /* | |
871 | * Check to see if sub-channels have already been created. This | |
872 | * can happen when this driver is re-loaded after unloading. | |
873 | */ | |
874 | ||
875 | if (vmbus_are_subchannels_present(device->channel)) | |
876 | return; | |
877 | ||
878 | stor_device->open_sub_channel = false; | |
879 | /* | |
880 | * Request the host to create sub-channels. | |
881 | */ | |
882 | memset(request, 0, sizeof(struct storvsc_cmd_request)); | |
883 | init_completion(&request->wait_event); | |
884 | vstor_packet->operation = VSTOR_OPERATION_CREATE_SUB_CHANNELS; | |
885 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; | |
886 | vstor_packet->sub_channel_count = num_sc; | |
887 | ||
888 | ret = vmbus_sendpacket(device->channel, vstor_packet, | |
889 | (sizeof(struct vstor_packet) - | |
890 | vmscsi_size_delta), | |
891 | (unsigned long)request, | |
892 | VM_PKT_DATA_INBAND, | |
893 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
894 | ||
895 | if (ret != 0) | |
896 | return; | |
897 | ||
898 | t = wait_for_completion_timeout(&request->wait_event, 10*HZ); | |
899 | if (t == 0) | |
900 | return; | |
901 | ||
902 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || | |
903 | vstor_packet->status != 0) | |
904 | return; | |
905 | ||
906 | /* | |
907 | * Now that we created the sub-channels, invoke the check; this | |
908 | * may trigger the callback. | |
909 | */ | |
910 | stor_device->open_sub_channel = true; | |
911 | vmbus_are_subchannels_present(device->channel); | |
912 | } | |
913 | ||
8dcf37d4 S |
914 | static int storvsc_channel_init(struct hv_device *device) |
915 | { | |
916 | struct storvsc_device *stor_device; | |
61eaffc9 | 917 | struct storvsc_cmd_request *request; |
8dcf37d4 | 918 | struct vstor_packet *vstor_packet; |
1a363108 | 919 | int ret, t, i; |
6f94d5de S |
920 | int max_chns; |
921 | bool process_sub_channels = false; | |
8dcf37d4 S |
922 | |
923 | stor_device = get_out_stor_device(device); | |
924 | if (!stor_device) | |
925 | return -ENODEV; | |
926 | ||
927 | request = &stor_device->init_request; | |
928 | vstor_packet = &request->vstor_packet; | |
929 | ||
930 | /* | |
931 | * Now, initiate the vsc/vsp initialization protocol on the open | |
932 | * channel | |
933 | */ | |
61eaffc9 | 934 | memset(request, 0, sizeof(struct storvsc_cmd_request)); |
8dcf37d4 S |
935 | init_completion(&request->wait_event); |
936 | vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION; | |
937 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; | |
938 | ||
939 | ret = vmbus_sendpacket(device->channel, vstor_packet, | |
8b612fa2 S |
940 | (sizeof(struct vstor_packet) - |
941 | vmscsi_size_delta), | |
8dcf37d4 S |
942 | (unsigned long)request, |
943 | VM_PKT_DATA_INBAND, | |
944 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
945 | if (ret != 0) | |
946 | goto cleanup; | |
947 | ||
948 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); | |
949 | if (t == 0) { | |
950 | ret = -ETIMEDOUT; | |
951 | goto cleanup; | |
952 | } | |
953 | ||
954 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || | |
955 | vstor_packet->status != 0) | |
956 | goto cleanup; | |
957 | ||
958 | ||
1a363108 KM |
959 | for (i = 0; i < ARRAY_SIZE(vmstor_protocols); i++) { |
960 | /* reuse the packet for version range supported */ | |
961 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); | |
962 | vstor_packet->operation = | |
963 | VSTOR_OPERATION_QUERY_PROTOCOL_VERSION; | |
964 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; | |
8dcf37d4 | 965 | |
1a363108 KM |
966 | vstor_packet->version.major_minor = |
967 | vmstor_protocols[i].protocol_version; | |
85904a5e | 968 | |
1a363108 KM |
969 | /* |
970 | * The revision number is only used in Windows; set it to 0. | |
971 | */ | |
972 | vstor_packet->version.revision = 0; | |
8dcf37d4 | 973 | |
1a363108 | 974 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
8b612fa2 S |
975 | (sizeof(struct vstor_packet) - |
976 | vmscsi_size_delta), | |
8dcf37d4 S |
977 | (unsigned long)request, |
978 | VM_PKT_DATA_INBAND, | |
979 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
1a363108 KM |
980 | if (ret != 0) |
981 | goto cleanup; | |
8dcf37d4 | 982 | |
1a363108 KM |
983 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); |
984 | if (t == 0) { | |
985 | ret = -ETIMEDOUT; | |
986 | goto cleanup; | |
987 | } | |
988 | ||
989 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO) { | |
990 | ret = -EINVAL; | |
991 | goto cleanup; | |
992 | } | |
993 | ||
994 | if (vstor_packet->status == 0) { | |
995 | vmstor_proto_version = | |
996 | vmstor_protocols[i].protocol_version; | |
997 | ||
998 | sense_buffer_size = | |
999 | vmstor_protocols[i].sense_buffer_size; | |
1000 | ||
1001 | vmscsi_size_delta = | |
1002 | vmstor_protocols[i].vmscsi_size_delta; | |
1003 | ||
1004 | break; | |
1005 | } | |
8dcf37d4 S |
1006 | } |
1007 | ||
1a363108 KM |
1008 | if (vstor_packet->status != 0) { |
1009 | ret = -EINVAL; | |
8dcf37d4 | 1010 | goto cleanup; |
1a363108 | 1011 | } |
8dcf37d4 S |
1012 | |
1013 | ||
1014 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); | |
1015 | vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES; | |
1016 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; | |
8dcf37d4 S |
1017 | |
1018 | ret = vmbus_sendpacket(device->channel, vstor_packet, | |
8b612fa2 S |
1019 | (sizeof(struct vstor_packet) - |
1020 | vmscsi_size_delta), | |
8dcf37d4 S |
1021 | (unsigned long)request, |
1022 | VM_PKT_DATA_INBAND, | |
1023 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
1024 | ||
1025 | if (ret != 0) | |
1026 | goto cleanup; | |
1027 | ||
1028 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); | |
1029 | if (t == 0) { | |
1030 | ret = -ETIMEDOUT; | |
1031 | goto cleanup; | |
1032 | } | |
1033 | ||
1034 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || | |
1035 | vstor_packet->status != 0) | |
1036 | goto cleanup; | |
1037 | ||
6f94d5de S |
1038 | /* |
1039 | * Check to see if multi-channel support is there. | |
1040 | * Hosts that implement protocol version of 5.1 and above | |
1041 | * support multi-channel. | |
1042 | */ | |
1043 | max_chns = vstor_packet->storage_channel_properties.max_channel_cnt; | |
6ee5c615 | 1044 | if (vmbus_proto_version >= VERSION_WIN8) { |
6f94d5de S |
1045 | if (vstor_packet->storage_channel_properties.flags & |
1046 | STORAGE_CHANNEL_SUPPORTS_MULTI_CHANNEL) | |
1047 | process_sub_channels = true; | |
1048 | } | |
5117b936 S |
1049 | stor_device->max_transfer_bytes = |
1050 | vstor_packet->storage_channel_properties.max_transfer_bytes; | |
6f94d5de | 1051 | |
8dcf37d4 S |
1052 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); |
1053 | vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION; | |
1054 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; | |
1055 | ||
1056 | ret = vmbus_sendpacket(device->channel, vstor_packet, | |
8b612fa2 S |
1057 | (sizeof(struct vstor_packet) - |
1058 | vmscsi_size_delta), | |
8dcf37d4 S |
1059 | (unsigned long)request, |
1060 | VM_PKT_DATA_INBAND, | |
1061 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
1062 | ||
1063 | if (ret != 0) | |
1064 | goto cleanup; | |
1065 | ||
1066 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); | |
1067 | if (t == 0) { | |
1068 | ret = -ETIMEDOUT; | |
1069 | goto cleanup; | |
1070 | } | |
1071 | ||
1072 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || | |
1073 | vstor_packet->status != 0) | |
1074 | goto cleanup; | |
1075 | ||
6f94d5de S |
1076 | if (process_sub_channels) |
1077 | handle_multichannel_storage(device, max_chns); | |
1078 | ||
8dcf37d4 S |
1079 | |
1080 | cleanup: | |
1081 | return ret; | |
1082 | } | |
1083 | ||
c50bd448 S |
1084 | static void storvsc_handle_error(struct vmscsi_request *vm_srb, |
1085 | struct scsi_cmnd *scmnd, | |
1086 | struct Scsi_Host *host, | |
1087 | u8 asc, u8 ascq) | |
1088 | { | |
1089 | struct storvsc_scan_work *wrk; | |
1090 | void (*process_err_fn)(struct work_struct *work); | |
1091 | bool do_work = false; | |
1092 | ||
1093 | switch (vm_srb->srb_status) { | |
1094 | case SRB_STATUS_ERROR: | |
1095 | /* | |
1096 | * If there is an error; offline the device since all | |
1097 | * error recovery strategies would have already been | |
1098 | * deployed on the host side. However, if the command | |
1099 | * were a pass-through command deal with it appropriately. | |
1100 | */ | |
1101 | switch (scmnd->cmnd[0]) { | |
1102 | case ATA_16: | |
1103 | case ATA_12: | |
1104 | set_host_byte(scmnd, DID_PASSTHROUGH); | |
1105 | break; | |
3533f860 S |
1106 | /* |
1107 | * On Some Windows hosts TEST_UNIT_READY command can return | |
1108 | * SRB_STATUS_ERROR, let the upper level code deal with it | |
1109 | * based on the sense information. | |
1110 | */ | |
1111 | case TEST_UNIT_READY: | |
1112 | break; | |
c50bd448 S |
1113 | default: |
1114 | set_host_byte(scmnd, DID_TARGET_FAILURE); | |
1115 | } | |
1116 | break; | |
1117 | case SRB_STATUS_INVALID_LUN: | |
1118 | do_work = true; | |
1119 | process_err_fn = storvsc_remove_lun; | |
1120 | break; | |
6781209e S |
1121 | case (SRB_STATUS_ABORTED | SRB_STATUS_AUTOSENSE_VALID): |
1122 | if ((asc == 0x2a) && (ascq == 0x9)) { | |
1123 | do_work = true; | |
1124 | process_err_fn = storvsc_device_scan; | |
1125 | /* | |
1126 | * Retry the I/O that trigerred this. | |
1127 | */ | |
1128 | set_host_byte(scmnd, DID_REQUEUE); | |
1129 | } | |
1130 | break; | |
c50bd448 | 1131 | } |
6781209e | 1132 | |
c50bd448 S |
1133 | if (!do_work) |
1134 | return; | |
1135 | ||
1136 | /* | |
1137 | * We need to schedule work to process this error; schedule it. | |
1138 | */ | |
1139 | wrk = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC); | |
1140 | if (!wrk) { | |
1141 | set_host_byte(scmnd, DID_TARGET_FAILURE); | |
1142 | return; | |
1143 | } | |
1144 | ||
1145 | wrk->host = host; | |
1146 | wrk->lun = vm_srb->lun; | |
1147 | INIT_WORK(&wrk->work, process_err_fn); | |
1148 | schedule_work(&wrk->work); | |
1149 | } | |
1150 | ||
2707388c | 1151 | |
61eaffc9 | 1152 | static void storvsc_command_completion(struct storvsc_cmd_request *cmd_request) |
8dcf37d4 | 1153 | { |
2707388c S |
1154 | struct scsi_cmnd *scmnd = cmd_request->cmd; |
1155 | struct hv_host_device *host_dev = shost_priv(scmnd->device->host); | |
2707388c S |
1156 | struct scsi_sense_hdr sense_hdr; |
1157 | struct vmscsi_request *vm_srb; | |
c50bd448 S |
1158 | struct Scsi_Host *host; |
1159 | struct storvsc_device *stor_dev; | |
1160 | struct hv_device *dev = host_dev->dev; | |
be0cf6ca S |
1161 | u32 payload_sz = cmd_request->payload_sz; |
1162 | void *payload = cmd_request->payload; | |
c50bd448 S |
1163 | |
1164 | stor_dev = get_in_stor_device(dev); | |
1165 | host = stor_dev->host; | |
8dcf37d4 | 1166 | |
61eaffc9 | 1167 | vm_srb = &cmd_request->vstor_packet.vm_srb; |
2707388c S |
1168 | if (cmd_request->bounce_sgl_count) { |
1169 | if (vm_srb->data_in == READ_TYPE) | |
1170 | copy_from_bounce_buffer(scsi_sglist(scmnd), | |
1171 | cmd_request->bounce_sgl, | |
1172 | scsi_sg_count(scmnd), | |
1173 | cmd_request->bounce_sgl_count); | |
1174 | destroy_bounce_buffer(cmd_request->bounce_sgl, | |
1175 | cmd_request->bounce_sgl_count); | |
1176 | } | |
8dcf37d4 | 1177 | |
42e22cac S |
1178 | scmnd->result = vm_srb->scsi_status; |
1179 | ||
2707388c S |
1180 | if (scmnd->result) { |
1181 | if (scsi_normalize_sense(scmnd->sense_buffer, | |
1182 | SCSI_SENSE_BUFFERSIZE, &sense_hdr)) | |
d811b848 HR |
1183 | scsi_print_sense_hdr(scmnd->device, "storvsc", |
1184 | &sense_hdr); | |
2707388c S |
1185 | } |
1186 | ||
c50bd448 S |
1187 | if (vm_srb->srb_status != SRB_STATUS_SUCCESS) |
1188 | storvsc_handle_error(vm_srb, scmnd, host, sense_hdr.asc, | |
1189 | sense_hdr.ascq); | |
1190 | ||
2707388c | 1191 | scsi_set_resid(scmnd, |
be0cf6ca | 1192 | cmd_request->payload->range.len - |
2707388c S |
1193 | vm_srb->data_transfer_length); |
1194 | ||
ead3700d | 1195 | scmnd->scsi_done(scmnd); |
be0cf6ca S |
1196 | |
1197 | if (payload_sz > | |
1198 | sizeof(struct vmbus_channel_packet_multipage_buffer)) | |
1199 | kfree(payload); | |
2707388c S |
1200 | } |
1201 | ||
1202 | static void storvsc_on_io_completion(struct hv_device *device, | |
1203 | struct vstor_packet *vstor_packet, | |
61eaffc9 | 1204 | struct storvsc_cmd_request *request) |
2707388c S |
1205 | { |
1206 | struct storvsc_device *stor_device; | |
1207 | struct vstor_packet *stor_pkt; | |
1208 | ||
1209 | stor_device = hv_get_drvdata(device); | |
1210 | stor_pkt = &request->vstor_packet; | |
1211 | ||
1212 | /* | |
1213 | * The current SCSI handling on the host side does | |
1214 | * not correctly handle: | |
1215 | * INQUIRY command with page code parameter set to 0x80 | |
1216 | * MODE_SENSE command with cmd[2] == 0x1c | |
1217 | * | |
1218 | * Setup srb and scsi status so this won't be fatal. | |
1219 | * We do this so we can distinguish truly fatal failues | |
4ed51a21 S |
1220 | * (srb status == 0x4) and off-line the device in that case. |
1221 | */ | |
1222 | ||
1223 | if ((stor_pkt->vm_srb.cdb[0] == INQUIRY) || | |
a8c18c57 | 1224 | (stor_pkt->vm_srb.cdb[0] == MODE_SENSE)) { |
4ed51a21 | 1225 | vstor_packet->vm_srb.scsi_status = 0; |
16046320 | 1226 | vstor_packet->vm_srb.srb_status = SRB_STATUS_SUCCESS; |
4ed51a21 S |
1227 | } |
1228 | ||
8dcf37d4 S |
1229 | |
1230 | /* Copy over the status...etc */ | |
1231 | stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status; | |
1232 | stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status; | |
1233 | stor_pkt->vm_srb.sense_info_length = | |
1234 | vstor_packet->vm_srb.sense_info_length; | |
1235 | ||
8dcf37d4 S |
1236 | |
1237 | if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) { | |
1238 | /* CHECK_CONDITION */ | |
16046320 S |
1239 | if (vstor_packet->vm_srb.srb_status & |
1240 | SRB_STATUS_AUTOSENSE_VALID) { | |
8dcf37d4 | 1241 | /* autosense data available */ |
8dcf37d4 | 1242 | |
ead3700d | 1243 | memcpy(request->cmd->sense_buffer, |
8dcf37d4 S |
1244 | vstor_packet->vm_srb.sense_data, |
1245 | vstor_packet->vm_srb.sense_info_length); | |
1246 | ||
1247 | } | |
1248 | } | |
1249 | ||
1250 | stor_pkt->vm_srb.data_transfer_length = | |
1251 | vstor_packet->vm_srb.data_transfer_length; | |
1252 | ||
2707388c | 1253 | storvsc_command_completion(request); |
8dcf37d4 S |
1254 | |
1255 | if (atomic_dec_and_test(&stor_device->num_outstanding_req) && | |
1256 | stor_device->drain_notify) | |
1257 | wake_up(&stor_device->waiting_to_drain); | |
1258 | ||
1259 | ||
1260 | } | |
1261 | ||
1262 | static void storvsc_on_receive(struct hv_device *device, | |
1263 | struct vstor_packet *vstor_packet, | |
61eaffc9 | 1264 | struct storvsc_cmd_request *request) |
8dcf37d4 | 1265 | { |
12675799 S |
1266 | struct storvsc_scan_work *work; |
1267 | struct storvsc_device *stor_device; | |
1268 | ||
8dcf37d4 S |
1269 | switch (vstor_packet->operation) { |
1270 | case VSTOR_OPERATION_COMPLETE_IO: | |
1271 | storvsc_on_io_completion(device, vstor_packet, request); | |
1272 | break; | |
12675799 | 1273 | |
8dcf37d4 | 1274 | case VSTOR_OPERATION_REMOVE_DEVICE: |
12675799 S |
1275 | case VSTOR_OPERATION_ENUMERATE_BUS: |
1276 | stor_device = get_in_stor_device(device); | |
1277 | work = kmalloc(sizeof(struct storvsc_scan_work), GFP_ATOMIC); | |
1278 | if (!work) | |
1279 | return; | |
1280 | ||
2a09ed3d | 1281 | INIT_WORK(&work->work, storvsc_host_scan); |
12675799 S |
1282 | work->host = stor_device->host; |
1283 | schedule_work(&work->work); | |
1284 | break; | |
8dcf37d4 S |
1285 | |
1286 | default: | |
1287 | break; | |
1288 | } | |
1289 | } | |
1290 | ||
1291 | static void storvsc_on_channel_callback(void *context) | |
1292 | { | |
6f94d5de S |
1293 | struct vmbus_channel *channel = (struct vmbus_channel *)context; |
1294 | struct hv_device *device; | |
8dcf37d4 S |
1295 | struct storvsc_device *stor_device; |
1296 | u32 bytes_recvd; | |
1297 | u64 request_id; | |
1298 | unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)]; | |
61eaffc9 | 1299 | struct storvsc_cmd_request *request; |
8dcf37d4 S |
1300 | int ret; |
1301 | ||
6f94d5de S |
1302 | if (channel->primary_channel != NULL) |
1303 | device = channel->primary_channel->device_obj; | |
1304 | else | |
1305 | device = channel->device_obj; | |
8dcf37d4 S |
1306 | |
1307 | stor_device = get_in_stor_device(device); | |
1308 | if (!stor_device) | |
1309 | return; | |
1310 | ||
1311 | do { | |
6f94d5de | 1312 | ret = vmbus_recvpacket(channel, packet, |
8b612fa2 S |
1313 | ALIGN((sizeof(struct vstor_packet) - |
1314 | vmscsi_size_delta), 8), | |
8dcf37d4 S |
1315 | &bytes_recvd, &request_id); |
1316 | if (ret == 0 && bytes_recvd > 0) { | |
1317 | ||
61eaffc9 | 1318 | request = (struct storvsc_cmd_request *) |
8dcf37d4 S |
1319 | (unsigned long)request_id; |
1320 | ||
1321 | if ((request == &stor_device->init_request) || | |
1322 | (request == &stor_device->reset_request)) { | |
1323 | ||
1324 | memcpy(&request->vstor_packet, packet, | |
8b612fa2 S |
1325 | (sizeof(struct vstor_packet) - |
1326 | vmscsi_size_delta)); | |
8dcf37d4 S |
1327 | complete(&request->wait_event); |
1328 | } else { | |
1329 | storvsc_on_receive(device, | |
1330 | (struct vstor_packet *)packet, | |
1331 | request); | |
1332 | } | |
1333 | } else { | |
1334 | break; | |
1335 | } | |
1336 | } while (1); | |
1337 | ||
1338 | return; | |
1339 | } | |
1340 | ||
1341 | static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size) | |
1342 | { | |
1343 | struct vmstorage_channel_properties props; | |
1344 | int ret; | |
1345 | ||
1346 | memset(&props, 0, sizeof(struct vmstorage_channel_properties)); | |
1347 | ||
8dcf37d4 S |
1348 | ret = vmbus_open(device->channel, |
1349 | ring_size, | |
1350 | ring_size, | |
1351 | (void *)&props, | |
1352 | sizeof(struct vmstorage_channel_properties), | |
6f94d5de | 1353 | storvsc_on_channel_callback, device->channel); |
8dcf37d4 S |
1354 | |
1355 | if (ret != 0) | |
1356 | return ret; | |
1357 | ||
1358 | ret = storvsc_channel_init(device); | |
1359 | ||
1360 | return ret; | |
1361 | } | |
1362 | ||
c1b3d067 | 1363 | static int storvsc_dev_remove(struct hv_device *device) |
8dcf37d4 S |
1364 | { |
1365 | struct storvsc_device *stor_device; | |
1366 | unsigned long flags; | |
1367 | ||
cd654ea1 | 1368 | stor_device = hv_get_drvdata(device); |
8dcf37d4 S |
1369 | |
1370 | spin_lock_irqsave(&device->channel->inbound_lock, flags); | |
1371 | stor_device->destroy = true; | |
1372 | spin_unlock_irqrestore(&device->channel->inbound_lock, flags); | |
1373 | ||
1374 | /* | |
1375 | * At this point, all outbound traffic should be disable. We | |
1376 | * only allow inbound traffic (responses) to proceed so that | |
1377 | * outstanding requests can be completed. | |
1378 | */ | |
1379 | ||
1380 | storvsc_wait_to_drain(stor_device); | |
1381 | ||
1382 | /* | |
1383 | * Since we have already drained, we don't need to busy wait | |
1384 | * as was done in final_release_stor_device() | |
1385 | * Note that we cannot set the ext pointer to NULL until | |
1386 | * we have drained - to drain the outgoing packets, we need to | |
1387 | * allow incoming packets. | |
1388 | */ | |
1389 | spin_lock_irqsave(&device->channel->inbound_lock, flags); | |
cd654ea1 | 1390 | hv_set_drvdata(device, NULL); |
8dcf37d4 S |
1391 | spin_unlock_irqrestore(&device->channel->inbound_lock, flags); |
1392 | ||
1393 | /* Close the channel */ | |
1394 | vmbus_close(device->channel); | |
1395 | ||
1396 | kfree(stor_device); | |
1397 | return 0; | |
1398 | } | |
1399 | ||
c1b3d067 | 1400 | static int storvsc_do_io(struct hv_device *device, |
be0cf6ca | 1401 | struct storvsc_cmd_request *request) |
8dcf37d4 S |
1402 | { |
1403 | struct storvsc_device *stor_device; | |
1404 | struct vstor_packet *vstor_packet; | |
6f94d5de | 1405 | struct vmbus_channel *outgoing_channel; |
8dcf37d4 S |
1406 | int ret = 0; |
1407 | ||
1408 | vstor_packet = &request->vstor_packet; | |
1409 | stor_device = get_out_stor_device(device); | |
1410 | ||
1411 | if (!stor_device) | |
1412 | return -ENODEV; | |
1413 | ||
1414 | ||
1415 | request->device = device; | |
6f94d5de S |
1416 | /* |
1417 | * Select an an appropriate channel to send the request out. | |
1418 | */ | |
1419 | ||
1420 | outgoing_channel = vmbus_get_outgoing_channel(device->channel); | |
8dcf37d4 S |
1421 | |
1422 | ||
1423 | vstor_packet->flags |= REQUEST_COMPLETION_FLAG; | |
1424 | ||
8b612fa2 S |
1425 | vstor_packet->vm_srb.length = (sizeof(struct vmscsi_request) - |
1426 | vmscsi_size_delta); | |
8dcf37d4 S |
1427 | |
1428 | ||
8b612fa2 | 1429 | vstor_packet->vm_srb.sense_info_length = sense_buffer_size; |
8dcf37d4 S |
1430 | |
1431 | ||
1432 | vstor_packet->vm_srb.data_transfer_length = | |
be0cf6ca | 1433 | request->payload->range.len; |
8dcf37d4 S |
1434 | |
1435 | vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB; | |
1436 | ||
be0cf6ca S |
1437 | if (request->payload->range.len) { |
1438 | ||
1439 | ret = vmbus_sendpacket_mpb_desc(outgoing_channel, | |
1440 | request->payload, request->payload_sz, | |
8dcf37d4 | 1441 | vstor_packet, |
8b612fa2 S |
1442 | (sizeof(struct vstor_packet) - |
1443 | vmscsi_size_delta), | |
8dcf37d4 S |
1444 | (unsigned long)request); |
1445 | } else { | |
0147dabc | 1446 | ret = vmbus_sendpacket(outgoing_channel, vstor_packet, |
8b612fa2 S |
1447 | (sizeof(struct vstor_packet) - |
1448 | vmscsi_size_delta), | |
8dcf37d4 S |
1449 | (unsigned long)request, |
1450 | VM_PKT_DATA_INBAND, | |
1451 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
1452 | } | |
1453 | ||
1454 | if (ret != 0) | |
1455 | return ret; | |
1456 | ||
1457 | atomic_inc(&stor_device->num_outstanding_req); | |
1458 | ||
1459 | return ret; | |
1460 | } | |
1461 | ||
419f2d03 S |
1462 | static int storvsc_device_configure(struct scsi_device *sdevice) |
1463 | { | |
419f2d03 | 1464 | |
419f2d03 S |
1465 | blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE); |
1466 | ||
419f2d03 | 1467 | blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY); |
419f2d03 | 1468 | |
893def38 S |
1469 | blk_queue_rq_timeout(sdevice->request_queue, (storvsc_timeout * HZ)); |
1470 | ||
3e8f4f40 OH |
1471 | sdevice->no_write_same = 1; |
1472 | ||
f3cfabce S |
1473 | /* |
1474 | * Add blist flags to permit the reading of the VPD pages even when | |
1475 | * the target may claim SPC-2 compliance. MSFT targets currently | |
1476 | * claim SPC-2 compliance while they implement post SPC-2 features. | |
1477 | * With this patch we can correctly handle WRITE_SAME_16 issues. | |
1478 | */ | |
1479 | sdevice->sdev_bflags |= msft_blist_flags; | |
1480 | ||
b0a93d96 S |
1481 | /* |
1482 | * If the host is WIN8 or WIN8 R2, claim conformance to SPC-3 | |
1483 | * if the device is a MSFT virtual device. | |
1484 | */ | |
1485 | if (!strncmp(sdevice->vendor, "Msft", 4)) { | |
1486 | switch (vmbus_proto_version) { | |
1487 | case VERSION_WIN8: | |
1488 | case VERSION_WIN8_1: | |
1489 | sdevice->scsi_level = SCSI_SPC_3; | |
1490 | break; | |
1491 | } | |
1492 | } | |
1493 | ||
419f2d03 S |
1494 | return 0; |
1495 | } | |
1496 | ||
62838ce2 S |
1497 | static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev, |
1498 | sector_t capacity, int *info) | |
1499 | { | |
5326fd5c S |
1500 | sector_t nsect = capacity; |
1501 | sector_t cylinders = nsect; | |
1502 | int heads, sectors_pt; | |
62838ce2 | 1503 | |
5326fd5c S |
1504 | /* |
1505 | * We are making up these values; let us keep it simple. | |
1506 | */ | |
1507 | heads = 0xff; | |
1508 | sectors_pt = 0x3f; /* Sectors per track */ | |
1509 | sector_div(cylinders, heads * sectors_pt); | |
1510 | if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect) | |
1511 | cylinders = 0xffff; | |
62838ce2 S |
1512 | |
1513 | info[0] = heads; | |
5326fd5c S |
1514 | info[1] = sectors_pt; |
1515 | info[2] = (int)cylinders; | |
62838ce2 | 1516 | |
62838ce2 S |
1517 | return 0; |
1518 | } | |
aa3d789e | 1519 | |
4b270c8b | 1520 | static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd) |
aa3d789e | 1521 | { |
4b270c8b S |
1522 | struct hv_host_device *host_dev = shost_priv(scmnd->device->host); |
1523 | struct hv_device *device = host_dev->dev; | |
1524 | ||
aa3d789e | 1525 | struct storvsc_device *stor_device; |
61eaffc9 | 1526 | struct storvsc_cmd_request *request; |
aa3d789e S |
1527 | struct vstor_packet *vstor_packet; |
1528 | int ret, t; | |
1529 | ||
aa3d789e | 1530 | |
1eaaddf9 | 1531 | stor_device = get_out_stor_device(device); |
aa3d789e | 1532 | if (!stor_device) |
a00e8224 | 1533 | return FAILED; |
aa3d789e S |
1534 | |
1535 | request = &stor_device->reset_request; | |
1536 | vstor_packet = &request->vstor_packet; | |
1537 | ||
1538 | init_completion(&request->wait_event); | |
1539 | ||
1540 | vstor_packet->operation = VSTOR_OPERATION_RESET_BUS; | |
1541 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; | |
1542 | vstor_packet->vm_srb.path_id = stor_device->path_id; | |
1543 | ||
1544 | ret = vmbus_sendpacket(device->channel, vstor_packet, | |
8b612fa2 S |
1545 | (sizeof(struct vstor_packet) - |
1546 | vmscsi_size_delta), | |
aa3d789e S |
1547 | (unsigned long)&stor_device->reset_request, |
1548 | VM_PKT_DATA_INBAND, | |
1549 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); | |
1550 | if (ret != 0) | |
a00e8224 | 1551 | return FAILED; |
aa3d789e | 1552 | |
46d2eb6d | 1553 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); |
a00e8224 S |
1554 | if (t == 0) |
1555 | return TIMEOUT_ERROR; | |
aa3d789e | 1556 | |
aa3d789e S |
1557 | |
1558 | /* | |
1559 | * At this point, all outstanding requests in the adapter | |
1560 | * should have been flushed out and return to us | |
5c1b10ab S |
1561 | * There is a potential race here where the host may be in |
1562 | * the process of responding when we return from here. | |
1563 | * Just wait for all in-transit packets to be accounted for | |
1564 | * before we return from here. | |
aa3d789e | 1565 | */ |
5c1b10ab | 1566 | storvsc_wait_to_drain(stor_device); |
aa3d789e | 1567 | |
a00e8224 | 1568 | return SUCCESS; |
aa3d789e S |
1569 | } |
1570 | ||
56b26e69 S |
1571 | /* |
1572 | * The host guarantees to respond to each command, although I/O latencies might | |
1573 | * be unbounded on Azure. Reset the timer unconditionally to give the host a | |
1574 | * chance to perform EH. | |
1575 | */ | |
1576 | static enum blk_eh_timer_return storvsc_eh_timed_out(struct scsi_cmnd *scmnd) | |
1577 | { | |
1578 | return BLK_EH_RESET_TIMER; | |
1579 | } | |
1580 | ||
c77b63b6 | 1581 | static bool storvsc_scsi_cmd_ok(struct scsi_cmnd *scmnd) |
92ae4ebd OH |
1582 | { |
1583 | bool allowed = true; | |
1584 | u8 scsi_op = scmnd->cmnd[0]; | |
1585 | ||
1586 | switch (scsi_op) { | |
3e8f4f40 OH |
1587 | /* the host does not handle WRITE_SAME, log accident usage */ |
1588 | case WRITE_SAME: | |
c77b63b6 S |
1589 | /* |
1590 | * smartd sends this command and the host does not handle | |
1591 | * this. So, don't send it. | |
1592 | */ | |
41098f8f | 1593 | case SET_WINDOW: |
59e00e74 | 1594 | scmnd->result = ILLEGAL_REQUEST << 16; |
41098f8f S |
1595 | allowed = false; |
1596 | break; | |
1597 | default: | |
1598 | break; | |
92ae4ebd OH |
1599 | } |
1600 | return allowed; | |
1601 | } | |
c5b463ae | 1602 | |
bab445e1 | 1603 | static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd) |
c5b463ae S |
1604 | { |
1605 | int ret; | |
bab445e1 | 1606 | struct hv_host_device *host_dev = shost_priv(host); |
c5b463ae | 1607 | struct hv_device *dev = host_dev->dev; |
ead3700d | 1608 | struct storvsc_cmd_request *cmd_request = scsi_cmd_priv(scmnd); |
c5b463ae S |
1609 | int i; |
1610 | struct scatterlist *sgl; | |
1611 | unsigned int sg_count = 0; | |
1612 | struct vmscsi_request *vm_srb; | |
aaced993 | 1613 | struct scatterlist *cur_sgl; |
be0cf6ca S |
1614 | struct vmbus_packet_mpb_array *payload; |
1615 | u32 payload_sz; | |
1616 | u32 length; | |
c5b463ae | 1617 | |
2492fd7a | 1618 | if (vmstor_proto_version <= VMSTOR_PROTO_VERSION_WIN8) { |
8caf92d8 S |
1619 | /* |
1620 | * On legacy hosts filter unimplemented commands. | |
1621 | * Future hosts are expected to correctly handle | |
1622 | * unsupported commands. Furthermore, it is | |
1623 | * possible that some of the currently | |
1624 | * unsupported commands maybe supported in | |
1625 | * future versions of the host. | |
1626 | */ | |
1627 | if (!storvsc_scsi_cmd_ok(scmnd)) { | |
1628 | scmnd->scsi_done(scmnd); | |
1629 | return 0; | |
1630 | } | |
92ae4ebd | 1631 | } |
c5b463ae | 1632 | |
c5b463ae | 1633 | /* Setup the cmd request */ |
c5b463ae S |
1634 | cmd_request->cmd = scmnd; |
1635 | ||
61eaffc9 | 1636 | vm_srb = &cmd_request->vstor_packet.vm_srb; |
8b612fa2 | 1637 | vm_srb->win8_extension.time_out_value = 60; |
c5b463ae | 1638 | |
f885fb73 S |
1639 | vm_srb->win8_extension.srb_flags |= |
1640 | (SRB_FLAGS_QUEUE_ACTION_ENABLE | | |
1641 | SRB_FLAGS_DISABLE_SYNCH_TRANSFER); | |
c5b463ae S |
1642 | |
1643 | /* Build the SRB */ | |
1644 | switch (scmnd->sc_data_direction) { | |
1645 | case DMA_TO_DEVICE: | |
1646 | vm_srb->data_in = WRITE_TYPE; | |
8b612fa2 | 1647 | vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_OUT; |
c5b463ae S |
1648 | break; |
1649 | case DMA_FROM_DEVICE: | |
1650 | vm_srb->data_in = READ_TYPE; | |
8b612fa2 | 1651 | vm_srb->win8_extension.srb_flags |= SRB_FLAGS_DATA_IN; |
c5b463ae | 1652 | break; |
cb1cf080 | 1653 | case DMA_NONE: |
c5b463ae | 1654 | vm_srb->data_in = UNKNOWN_TYPE; |
dc45708c | 1655 | vm_srb->win8_extension.srb_flags |= SRB_FLAGS_NO_DATA_TRANSFER; |
c5b463ae | 1656 | break; |
cb1cf080 VK |
1657 | default: |
1658 | /* | |
1659 | * This is DMA_BIDIRECTIONAL or something else we are never | |
1660 | * supposed to see here. | |
1661 | */ | |
1662 | WARN(1, "Unexpected data direction: %d\n", | |
1663 | scmnd->sc_data_direction); | |
1664 | return -EINVAL; | |
c5b463ae S |
1665 | } |
1666 | ||
c5b463ae | 1667 | |
c5b463ae S |
1668 | vm_srb->port_number = host_dev->port; |
1669 | vm_srb->path_id = scmnd->device->channel; | |
1670 | vm_srb->target_id = scmnd->device->id; | |
1671 | vm_srb->lun = scmnd->device->lun; | |
1672 | ||
c5b463ae S |
1673 | vm_srb->cdb_length = scmnd->cmd_len; |
1674 | ||
1675 | memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length); | |
1676 | ||
be0cf6ca S |
1677 | sgl = (struct scatterlist *)scsi_sglist(scmnd); |
1678 | sg_count = scsi_sg_count(scmnd); | |
c5b463ae | 1679 | |
be0cf6ca S |
1680 | length = scsi_bufflen(scmnd); |
1681 | payload = (struct vmbus_packet_mpb_array *)&cmd_request->mpb; | |
1682 | payload_sz = sizeof(cmd_request->mpb); | |
1683 | ||
1684 | if (sg_count) { | |
c5b463ae S |
1685 | /* check if we need to bounce the sgl */ |
1686 | if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) { | |
1687 | cmd_request->bounce_sgl = | |
be0cf6ca S |
1688 | create_bounce_buffer(sgl, sg_count, |
1689 | length, | |
6e8087a4 | 1690 | vm_srb->data_in); |
ead3700d CH |
1691 | if (!cmd_request->bounce_sgl) |
1692 | return SCSI_MLQUEUE_HOST_BUSY; | |
c5b463ae S |
1693 | |
1694 | cmd_request->bounce_sgl_count = | |
be0cf6ca | 1695 | ALIGN(length, PAGE_SIZE) >> PAGE_SHIFT; |
c5b463ae | 1696 | |
fa23b8c7 S |
1697 | if (vm_srb->data_in == WRITE_TYPE) |
1698 | copy_to_bounce_buffer(sgl, | |
be0cf6ca | 1699 | cmd_request->bounce_sgl, sg_count); |
c5b463ae S |
1700 | |
1701 | sgl = cmd_request->bounce_sgl; | |
1702 | sg_count = cmd_request->bounce_sgl_count; | |
1703 | } | |
1704 | ||
be0cf6ca S |
1705 | |
1706 | if (sg_count > MAX_PAGE_BUFFER_COUNT) { | |
1707 | ||
1708 | payload_sz = (sg_count * sizeof(void *) + | |
1709 | sizeof(struct vmbus_packet_mpb_array)); | |
1710 | payload = kmalloc(payload_sz, GFP_ATOMIC); | |
1711 | if (!payload) { | |
1712 | if (cmd_request->bounce_sgl_count) | |
1713 | destroy_bounce_buffer( | |
1714 | cmd_request->bounce_sgl, | |
1715 | cmd_request->bounce_sgl_count); | |
1716 | ||
1717 | return SCSI_MLQUEUE_DEVICE_BUSY; | |
1718 | } | |
1719 | } | |
1720 | ||
1721 | payload->range.len = length; | |
1722 | payload->range.offset = sgl[0].offset; | |
1723 | ||
aaced993 S |
1724 | cur_sgl = sgl; |
1725 | for (i = 0; i < sg_count; i++) { | |
be0cf6ca | 1726 | payload->range.pfn_array[i] = |
aaced993 S |
1727 | page_to_pfn(sg_page((cur_sgl))); |
1728 | cur_sgl = sg_next(cur_sgl); | |
1729 | } | |
c5b463ae S |
1730 | |
1731 | } else if (scsi_sglist(scmnd)) { | |
be0cf6ca S |
1732 | payload->range.len = length; |
1733 | payload->range.offset = | |
c5b463ae | 1734 | virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1); |
be0cf6ca | 1735 | payload->range.pfn_array[0] = |
c5b463ae S |
1736 | virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT; |
1737 | } | |
1738 | ||
be0cf6ca S |
1739 | cmd_request->payload = payload; |
1740 | cmd_request->payload_sz = payload_sz; | |
1741 | ||
c5b463ae | 1742 | /* Invokes the vsc to start an IO */ |
61eaffc9 | 1743 | ret = storvsc_do_io(dev, cmd_request); |
636f0fd1 | 1744 | |
d2598f01 | 1745 | if (ret == -EAGAIN) { |
c5b463ae S |
1746 | /* no more space */ |
1747 | ||
e86fb5e8 | 1748 | if (cmd_request->bounce_sgl_count) |
c5b463ae | 1749 | destroy_bounce_buffer(cmd_request->bounce_sgl, |
70691ec6 | 1750 | cmd_request->bounce_sgl_count); |
c5b463ae | 1751 | |
ead3700d | 1752 | return SCSI_MLQUEUE_DEVICE_BUSY; |
c5b463ae S |
1753 | } |
1754 | ||
c77b63b6 | 1755 | return 0; |
c5b463ae S |
1756 | } |
1757 | ||
bef4a34a | 1758 | static struct scsi_host_template scsi_driver = { |
ff568d3a GKH |
1759 | .module = THIS_MODULE, |
1760 | .name = "storvsc_host_t", | |
ead3700d | 1761 | .cmd_size = sizeof(struct storvsc_cmd_request), |
ff568d3a GKH |
1762 | .bios_param = storvsc_get_chs, |
1763 | .queuecommand = storvsc_queuecommand, | |
1764 | .eh_host_reset_handler = storvsc_host_reset_handler, | |
ead3700d | 1765 | .proc_name = "storvsc_host", |
56b26e69 | 1766 | .eh_timed_out = storvsc_eh_timed_out, |
ff568d3a | 1767 | .slave_configure = storvsc_device_configure, |
52f9614d | 1768 | .cmd_per_lun = 255, |
ff568d3a | 1769 | .this_id = -1, |
be0cf6ca | 1770 | .use_clustering = ENABLE_CLUSTERING, |
454f18a9 | 1771 | /* Make sure we dont get a sg segment crosses a page boundary */ |
ff568d3a | 1772 | .dma_boundary = PAGE_SIZE-1, |
54b2b50c | 1773 | .no_write_same = 1, |
bef4a34a HJ |
1774 | }; |
1775 | ||
ef52a81b S |
1776 | enum { |
1777 | SCSI_GUID, | |
1778 | IDE_GUID, | |
bde6d0f9 | 1779 | SFC_GUID, |
ef52a81b S |
1780 | }; |
1781 | ||
d847b5fe | 1782 | static const struct hv_vmbus_device_id id_table[] = { |
c45cf2d4 | 1783 | /* SCSI guid */ |
35c3bc20 S |
1784 | { HV_SCSI_GUID, |
1785 | .driver_data = SCSI_GUID | |
1786 | }, | |
21e37742 | 1787 | /* IDE guid */ |
35c3bc20 S |
1788 | { HV_IDE_GUID, |
1789 | .driver_data = IDE_GUID | |
1790 | }, | |
bde6d0f9 S |
1791 | /* Fibre Channel GUID */ |
1792 | { | |
1793 | HV_SYNTHFC_GUID, | |
1794 | .driver_data = SFC_GUID | |
1795 | }, | |
c45cf2d4 | 1796 | { }, |
d847b5fe | 1797 | }; |
bef4a34a | 1798 | |
d847b5fe | 1799 | MODULE_DEVICE_TABLE(vmbus, id_table); |
bd1f5d6a | 1800 | |
84946899 S |
1801 | static int storvsc_probe(struct hv_device *device, |
1802 | const struct hv_vmbus_device_id *dev_id) | |
bef4a34a | 1803 | { |
ff568d3a | 1804 | int ret; |
f458aada | 1805 | int num_cpus = num_online_cpus(); |
bef4a34a | 1806 | struct Scsi_Host *host; |
795b613d | 1807 | struct hv_host_device *host_dev; |
ef52a81b | 1808 | bool dev_is_ide = ((dev_id->driver_data == IDE_GUID) ? true : false); |
bd1f5d6a | 1809 | int target = 0; |
6e4198ce | 1810 | struct storvsc_device *stor_device; |
0fb8db29 S |
1811 | int max_luns_per_target; |
1812 | int max_targets; | |
1813 | int max_channels; | |
f458aada | 1814 | int max_sub_channels = 0; |
bd1f5d6a | 1815 | |
8b612fa2 S |
1816 | /* |
1817 | * Based on the windows host we are running on, | |
1818 | * set state to properly communicate with the host. | |
1819 | */ | |
1820 | ||
6ee5c615 | 1821 | if (vmbus_proto_version < VERSION_WIN8) { |
0fb8db29 S |
1822 | max_luns_per_target = STORVSC_IDE_MAX_LUNS_PER_TARGET; |
1823 | max_targets = STORVSC_IDE_MAX_TARGETS; | |
1824 | max_channels = STORVSC_IDE_MAX_CHANNELS; | |
6ee5c615 | 1825 | } else { |
0fb8db29 S |
1826 | max_luns_per_target = STORVSC_MAX_LUNS_PER_TARGET; |
1827 | max_targets = STORVSC_MAX_TARGETS; | |
1828 | max_channels = STORVSC_MAX_CHANNELS; | |
f458aada S |
1829 | /* |
1830 | * On Windows8 and above, we support sub-channels for storage. | |
1831 | * The number of sub-channels offerred is based on the number of | |
1832 | * VCPUs in the guest. | |
1833 | */ | |
1834 | max_sub_channels = (num_cpus / storvsc_vcpus_per_sub_channel); | |
8b612fa2 S |
1835 | } |
1836 | ||
f458aada S |
1837 | scsi_driver.can_queue = (max_outstanding_req_per_channel * |
1838 | (max_sub_channels + 1)); | |
1839 | ||
ff568d3a | 1840 | host = scsi_host_alloc(&scsi_driver, |
972621c9 | 1841 | sizeof(struct hv_host_device)); |
f8feed06 | 1842 | if (!host) |
bef4a34a | 1843 | return -ENOMEM; |
bef4a34a | 1844 | |
7f33f30a | 1845 | host_dev = shost_priv(host); |
795b613d | 1846 | memset(host_dev, 0, sizeof(struct hv_host_device)); |
bef4a34a | 1847 | |
795b613d | 1848 | host_dev->port = host->host_no; |
97c15296 | 1849 | host_dev->dev = device; |
bef4a34a | 1850 | |
4e03e697 | 1851 | |
a13d35ab | 1852 | stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL); |
6e4198ce | 1853 | if (!stor_device) { |
225ce6ea | 1854 | ret = -ENOMEM; |
ce3e301c | 1855 | goto err_out0; |
6e4198ce | 1856 | } |
9efd21e1 | 1857 | |
a13d35ab | 1858 | stor_device->destroy = false; |
6f94d5de | 1859 | stor_device->open_sub_channel = false; |
a13d35ab S |
1860 | init_waitqueue_head(&stor_device->waiting_to_drain); |
1861 | stor_device->device = device; | |
cd654ea1 S |
1862 | stor_device->host = host; |
1863 | hv_set_drvdata(device, stor_device); | |
a13d35ab | 1864 | |
6e4198ce S |
1865 | stor_device->port_number = host->host_no; |
1866 | ret = storvsc_connect_to_vsp(device, storvsc_ringbuffer_size); | |
225ce6ea | 1867 | if (ret) |
ce3e301c | 1868 | goto err_out1; |
bef4a34a | 1869 | |
6e4198ce S |
1870 | host_dev->path = stor_device->path_id; |
1871 | host_dev->target = stor_device->target_id; | |
bef4a34a | 1872 | |
4cd83ecd S |
1873 | switch (dev_id->driver_data) { |
1874 | case SFC_GUID: | |
1875 | host->max_lun = STORVSC_FC_MAX_LUNS_PER_TARGET; | |
1876 | host->max_id = STORVSC_FC_MAX_TARGETS; | |
1877 | host->max_channel = STORVSC_FC_MAX_CHANNELS - 1; | |
1878 | break; | |
1879 | ||
1880 | case SCSI_GUID: | |
0fb8db29 S |
1881 | host->max_lun = max_luns_per_target; |
1882 | host->max_id = max_targets; | |
1883 | host->max_channel = max_channels - 1; | |
4cd83ecd S |
1884 | break; |
1885 | ||
1886 | default: | |
1887 | host->max_lun = STORVSC_IDE_MAX_LUNS_PER_TARGET; | |
1888 | host->max_id = STORVSC_IDE_MAX_TARGETS; | |
1889 | host->max_channel = STORVSC_IDE_MAX_CHANNELS - 1; | |
1890 | break; | |
1891 | } | |
cf55f4a8 MS |
1892 | /* max cmd length */ |
1893 | host->max_cmd_len = STORVSC_MAX_CMD_LEN; | |
bef4a34a | 1894 | |
be0cf6ca S |
1895 | /* |
1896 | * set the table size based on the info we got | |
1897 | * from the host. | |
1898 | */ | |
1899 | host->sg_tablesize = (stor_device->max_transfer_bytes >> PAGE_SHIFT); | |
1900 | ||
454f18a9 | 1901 | /* Register the HBA and start the scsi bus scan */ |
9efd21e1 | 1902 | ret = scsi_add_host(host, &device->device); |
bd1f5d6a | 1903 | if (ret != 0) |
ce3e301c | 1904 | goto err_out2; |
bef4a34a | 1905 | |
bd1f5d6a S |
1906 | if (!dev_is_ide) { |
1907 | scsi_scan_host(host); | |
59d22950 S |
1908 | } else { |
1909 | target = (device->dev_instance.b[5] << 8 | | |
1910 | device->dev_instance.b[4]); | |
1911 | ret = scsi_add_device(host, 0, target, 0); | |
1912 | if (ret) { | |
1913 | scsi_remove_host(host); | |
1914 | goto err_out2; | |
1915 | } | |
bef4a34a | 1916 | } |
bd1f5d6a | 1917 | return 0; |
bef4a34a | 1918 | |
ce3e301c | 1919 | err_out2: |
225ce6ea S |
1920 | /* |
1921 | * Once we have connected with the host, we would need to | |
1922 | * to invoke storvsc_dev_remove() to rollback this state and | |
1923 | * this call also frees up the stor_device; hence the jump around | |
ce3e301c | 1924 | * err_out1 label. |
225ce6ea | 1925 | */ |
bd1f5d6a | 1926 | storvsc_dev_remove(device); |
ce3e301c | 1927 | goto err_out0; |
225ce6ea S |
1928 | |
1929 | err_out1: | |
ce3e301c | 1930 | kfree(stor_device); |
225ce6ea S |
1931 | |
1932 | err_out0: | |
bd1f5d6a | 1933 | scsi_host_put(host); |
225ce6ea | 1934 | return ret; |
bef4a34a HJ |
1935 | } |
1936 | ||
ddcbf65e S |
1937 | static int storvsc_remove(struct hv_device *dev) |
1938 | { | |
1939 | struct storvsc_device *stor_device = hv_get_drvdata(dev); | |
1940 | struct Scsi_Host *host = stor_device->host; | |
1941 | ||
1942 | scsi_remove_host(host); | |
1943 | storvsc_dev_remove(dev); | |
1944 | scsi_host_put(host); | |
1945 | ||
1946 | return 0; | |
1947 | } | |
1948 | ||
40bf63ed | 1949 | static struct hv_driver storvsc_drv = { |
fafb0efc | 1950 | .name = KBUILD_MODNAME, |
d847b5fe | 1951 | .id_table = id_table, |
40bf63ed S |
1952 | .probe = storvsc_probe, |
1953 | .remove = storvsc_remove, | |
39ae6fae | 1954 | }; |
7bd05b91 | 1955 | |
d9bbae83 | 1956 | static int __init storvsc_drv_init(void) |
f5c78872 | 1957 | { |
01415ab3 S |
1958 | |
1959 | /* | |
1960 | * Divide the ring buffer data size (which is 1 page less | |
1961 | * than the ring buffer size since that page is reserved for | |
1962 | * the ring buffer indices) by the max request size (which is | |
1963 | * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64) | |
1964 | */ | |
01415ab3 | 1965 | max_outstanding_req_per_channel = |
768fa219 GKH |
1966 | ((storvsc_ringbuffer_size - PAGE_SIZE) / |
1967 | ALIGN(MAX_MULTIPAGE_BUFFER_PACKET + | |
8b612fa2 S |
1968 | sizeof(struct vstor_packet) + sizeof(u64) - |
1969 | vmscsi_size_delta, | |
768fa219 | 1970 | sizeof(u64))); |
f5c78872 | 1971 | |
768fa219 | 1972 | return vmbus_driver_register(&storvsc_drv); |
f5c78872 S |
1973 | } |
1974 | ||
c63ba9e1 | 1975 | static void __exit storvsc_drv_exit(void) |
f5c78872 | 1976 | { |
768fa219 | 1977 | vmbus_driver_unregister(&storvsc_drv); |
f5c78872 S |
1978 | } |
1979 | ||
ff568d3a | 1980 | MODULE_LICENSE("GPL"); |
3afc7cc3 | 1981 | MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver"); |
d9bbae83 | 1982 | module_init(storvsc_drv_init); |
c63ba9e1 | 1983 | module_exit(storvsc_drv_exit); |