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