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