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