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