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