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