]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/hyperv/storvsc/hv_storvsc_drv_freebsd.c
MFC 302882-302884
[FreeBSD/stable/10.git] / sys / dev / hyperv / storvsc / hv_storvsc_drv_freebsd.c
1 /*-
2  * Copyright (c) 2009-2012,2016 Microsoft Corp.
3  * Copyright (c) 2012 NetApp Inc.
4  * Copyright (c) 2012 Citrix Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 /**
30  * StorVSC driver for Hyper-V.  This driver presents a SCSI HBA interface
31  * to the Comman Access Method (CAM) layer.  CAM control blocks (CCBs) are
32  * converted into VSCSI protocol messages which are delivered to the parent
33  * partition StorVSP driver over the Hyper-V VMBUS.
34  */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/proc.h>
40 #include <sys/condvar.h>
41 #include <sys/time.h>
42 #include <sys/systm.h>
43 #include <sys/sockio.h>
44 #include <sys/mbuf.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47 #include <sys/kernel.h>
48 #include <sys/queue.h>
49 #include <sys/lock.h>
50 #include <sys/sx.h>
51 #include <sys/taskqueue.h>
52 #include <sys/bus.h>
53 #include <sys/mutex.h>
54 #include <sys/callout.h>
55 #include <vm/vm.h>
56 #include <vm/pmap.h>
57 #include <vm/uma.h>
58 #include <sys/lock.h>
59 #include <sys/sema.h>
60 #include <sys/sglist.h>
61 #include <machine/bus.h>
62 #include <sys/bus_dma.h>
63
64 #include <cam/cam.h>
65 #include <cam/cam_ccb.h>
66 #include <cam/cam_periph.h>
67 #include <cam/cam_sim.h>
68 #include <cam/cam_xpt_sim.h>
69 #include <cam/cam_xpt_internal.h>
70 #include <cam/cam_debug.h>
71 #include <cam/scsi/scsi_all.h>
72 #include <cam/scsi/scsi_message.h>
73
74 #include <dev/hyperv/include/hyperv.h>
75 #include <dev/hyperv/include/vmbus.h>
76
77 #include "hv_vstorage.h"
78 #include "vmbus_if.h"
79
80 #define STORVSC_RINGBUFFER_SIZE         (20*PAGE_SIZE)
81 #define STORVSC_MAX_LUNS_PER_TARGET     (64)
82 #define STORVSC_MAX_IO_REQUESTS         (STORVSC_MAX_LUNS_PER_TARGET * 2)
83 #define BLKVSC_MAX_IDE_DISKS_PER_TARGET (1)
84 #define BLKVSC_MAX_IO_REQUESTS          STORVSC_MAX_IO_REQUESTS
85 #define STORVSC_MAX_TARGETS             (2)
86
87 #define VSTOR_PKT_SIZE  (sizeof(struct vstor_packet) - vmscsi_size_delta)
88
89 #define HV_ALIGN(x, a) roundup2(x, a)
90
91 struct storvsc_softc;
92
93 struct hv_sgl_node {
94         LIST_ENTRY(hv_sgl_node) link;
95         struct sglist *sgl_data;
96 };
97
98 struct hv_sgl_page_pool{
99         LIST_HEAD(, hv_sgl_node) in_use_sgl_list;
100         LIST_HEAD(, hv_sgl_node) free_sgl_list;
101         boolean_t                is_init;
102 } g_hv_sgl_page_pool;
103
104 #define STORVSC_MAX_SG_PAGE_CNT STORVSC_MAX_IO_REQUESTS * VMBUS_CHAN_PRPLIST_MAX
105
106 enum storvsc_request_type {
107         WRITE_TYPE,
108         READ_TYPE,
109         UNKNOWN_TYPE
110 };
111
112 struct hvs_gpa_range {
113         struct vmbus_gpa_range  gpa_range;
114         uint64_t                gpa_page[VMBUS_CHAN_PRPLIST_MAX];
115 } __packed;
116
117 struct hv_storvsc_request {
118         LIST_ENTRY(hv_storvsc_request) link;
119         struct vstor_packet     vstor_packet;
120         int prp_cnt;
121         struct hvs_gpa_range prp_list;
122         void *sense_data;
123         uint8_t sense_info_len;
124         uint8_t retries;
125         union ccb *ccb;
126         struct storvsc_softc *softc;
127         struct callout callout;
128         struct sema synch_sema; /*Synchronize the request/response if needed */
129         struct sglist *bounce_sgl;
130         unsigned int bounce_sgl_count;
131         uint64_t not_aligned_seg_bits;
132 };
133
134 struct storvsc_softc {
135         struct hv_vmbus_channel         *hs_chan;
136         LIST_HEAD(, hv_storvsc_request) hs_free_list;
137         struct mtx                      hs_lock;
138         struct storvsc_driver_props     *hs_drv_props;
139         int                             hs_unit;
140         uint32_t                        hs_frozen;
141         struct cam_sim                  *hs_sim;
142         struct cam_path                 *hs_path;
143         uint32_t                        hs_num_out_reqs;
144         boolean_t                       hs_destroy;
145         boolean_t                       hs_drain_notify;
146         struct sema                     hs_drain_sema;  
147         struct hv_storvsc_request       hs_init_req;
148         struct hv_storvsc_request       hs_reset_req;
149         device_t                        hs_dev;
150 };
151
152
153 /**
154  * HyperV storvsc timeout testing cases:
155  * a. IO returned after first timeout;
156  * b. IO returned after second timeout and queue freeze;
157  * c. IO returned while timer handler is running
158  * The first can be tested by "sg_senddiag -vv /dev/daX",
159  * and the second and third can be done by
160  * "sg_wr_mode -v -p 08 -c 0,1a -m 0,ff /dev/daX".
161  */
162 #define HVS_TIMEOUT_TEST 0
163
164 /*
165  * Bus/adapter reset functionality on the Hyper-V host is
166  * buggy and it will be disabled until
167  * it can be further tested.
168  */
169 #define HVS_HOST_RESET 0
170
171 struct storvsc_driver_props {
172         char            *drv_name;
173         char            *drv_desc;
174         uint8_t         drv_max_luns_per_target;
175         uint8_t         drv_max_ios_per_target;
176         uint32_t        drv_ringbuffer_size;
177 };
178
179 enum hv_storage_type {
180         DRIVER_BLKVSC,
181         DRIVER_STORVSC,
182         DRIVER_UNKNOWN
183 };
184
185 #define HS_MAX_ADAPTERS 10
186
187 #define HV_STORAGE_SUPPORTS_MULTI_CHANNEL 0x1
188
189 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
190 static const struct hyperv_guid gStorVscDeviceType={
191         .hv_guid = {0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
192                  0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f}
193 };
194
195 /* {32412632-86cb-44a2-9b5c-50d1417354f5} */
196 static const struct hyperv_guid gBlkVscDeviceType={
197         .hv_guid = {0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
198                  0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5}
199 };
200
201 static struct storvsc_driver_props g_drv_props_table[] = {
202         {"blkvsc", "Hyper-V IDE Storage Interface",
203          BLKVSC_MAX_IDE_DISKS_PER_TARGET, BLKVSC_MAX_IO_REQUESTS,
204          STORVSC_RINGBUFFER_SIZE},
205         {"storvsc", "Hyper-V SCSI Storage Interface",
206          STORVSC_MAX_LUNS_PER_TARGET, STORVSC_MAX_IO_REQUESTS,
207          STORVSC_RINGBUFFER_SIZE}
208 };
209
210 /*
211  * Sense buffer size changed in win8; have a run-time
212  * variable to track the size we should use.
213  */
214 static int sense_buffer_size = PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE;
215
216 /*
217  * The size of the vmscsi_request has changed in win8. The
218  * additional size is for the newly added elements in the
219  * structure. These elements are valid only when we are talking
220  * to a win8 host.
221  * Track the correct size we need to apply.
222  */
223 static int vmscsi_size_delta;
224 /*
225  * The storage protocol version is determined during the
226  * initial exchange with the host.  It will indicate which
227  * storage functionality is available in the host.
228 */
229 static int vmstor_proto_version;
230
231 struct vmstor_proto {
232         int proto_version;
233         int sense_buffer_size;
234         int vmscsi_size_delta;
235 };
236
237 static const struct vmstor_proto vmstor_proto_list[] = {
238         {
239                 VMSTOR_PROTOCOL_VERSION_WIN10,
240                 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
241                 0
242         },
243         {
244                 VMSTOR_PROTOCOL_VERSION_WIN8_1,
245                 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
246                 0
247         },
248         {
249                 VMSTOR_PROTOCOL_VERSION_WIN8,
250                 POST_WIN7_STORVSC_SENSE_BUFFER_SIZE,
251                 0
252         },
253         {
254                 VMSTOR_PROTOCOL_VERSION_WIN7,
255                 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
256                 sizeof(struct vmscsi_win8_extension),
257         },
258         {
259                 VMSTOR_PROTOCOL_VERSION_WIN6,
260                 PRE_WIN8_STORVSC_SENSE_BUFFER_SIZE,
261                 sizeof(struct vmscsi_win8_extension),
262         }
263 };
264
265 /* static functions */
266 static int storvsc_probe(device_t dev);
267 static int storvsc_attach(device_t dev);
268 static int storvsc_detach(device_t dev);
269 static void storvsc_poll(struct cam_sim * sim);
270 static void storvsc_action(struct cam_sim * sim, union ccb * ccb);
271 static int create_storvsc_request(union ccb *ccb, struct hv_storvsc_request *reqp);
272 static void storvsc_free_request(struct storvsc_softc *sc, struct hv_storvsc_request *reqp);
273 static enum hv_storage_type storvsc_get_storage_type(device_t dev);
274 static void hv_storvsc_rescan_target(struct storvsc_softc *sc);
275 static void hv_storvsc_on_channel_callback(void *xchan);
276 static void hv_storvsc_on_iocompletion( struct storvsc_softc *sc,
277                                         struct vstor_packet *vstor_packet,
278                                         struct hv_storvsc_request *request);
279 static int hv_storvsc_connect_vsp(struct storvsc_softc *);
280 static void storvsc_io_done(struct hv_storvsc_request *reqp);
281 static void storvsc_copy_sgl_to_bounce_buf(struct sglist *bounce_sgl,
282                                 bus_dma_segment_t *orig_sgl,
283                                 unsigned int orig_sgl_count,
284                                 uint64_t seg_bits);
285 void storvsc_copy_from_bounce_buf_to_sgl(bus_dma_segment_t *dest_sgl,
286                                 unsigned int dest_sgl_count,
287                                 struct sglist* src_sgl,
288                                 uint64_t seg_bits);
289
290 static device_method_t storvsc_methods[] = {
291         /* Device interface */
292         DEVMETHOD(device_probe,         storvsc_probe),
293         DEVMETHOD(device_attach,        storvsc_attach),
294         DEVMETHOD(device_detach,        storvsc_detach),
295         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
296         DEVMETHOD_END
297 };
298
299 static driver_t storvsc_driver = {
300         "storvsc", storvsc_methods, sizeof(struct storvsc_softc),
301 };
302
303 static devclass_t storvsc_devclass;
304 DRIVER_MODULE(storvsc, vmbus, storvsc_driver, storvsc_devclass, 0, 0);
305 MODULE_VERSION(storvsc, 1);
306 MODULE_DEPEND(storvsc, vmbus, 1, 1, 1);
307
308 static void
309 storvsc_subchan_attach(struct storvsc_softc *sc,
310     struct hv_vmbus_channel *new_channel)
311 {
312         struct vmstor_chan_props props;
313         int ret = 0;
314
315         memset(&props, 0, sizeof(props));
316
317         new_channel->hv_chan_priv1 = sc;
318         vmbus_channel_cpu_rr(new_channel);
319         ret = hv_vmbus_channel_open(new_channel,
320             sc->hs_drv_props->drv_ringbuffer_size,
321             sc->hs_drv_props->drv_ringbuffer_size,
322             (void *)&props,
323             sizeof(struct vmstor_chan_props),
324             hv_storvsc_on_channel_callback,
325             new_channel);
326 }
327
328 /**
329  * @brief Send multi-channel creation request to host
330  *
331  * @param device  a Hyper-V device pointer
332  * @param max_chans  the max channels supported by vmbus
333  */
334 static void
335 storvsc_send_multichannel_request(struct storvsc_softc *sc, int max_chans)
336 {
337         struct hv_vmbus_channel **subchan;
338         struct hv_storvsc_request *request;
339         struct vstor_packet *vstor_packet;      
340         int request_channels_cnt = 0;
341         int ret, i;
342
343         /* get multichannels count that need to create */
344         request_channels_cnt = MIN(max_chans, mp_ncpus);
345
346         request = &sc->hs_init_req;
347
348         /* request the host to create multi-channel */
349         memset(request, 0, sizeof(struct hv_storvsc_request));
350         
351         sema_init(&request->synch_sema, 0, ("stor_synch_sema"));
352
353         vstor_packet = &request->vstor_packet;
354         
355         vstor_packet->operation = VSTOR_OPERATION_CREATE_MULTI_CHANNELS;
356         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
357         vstor_packet->u.multi_channels_cnt = request_channels_cnt;
358
359         ret = vmbus_chan_send(sc->hs_chan,
360             VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
361             vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
362
363         /* wait for 5 seconds */
364         ret = sema_timedwait(&request->synch_sema, 5 * hz);
365         if (ret != 0) {         
366                 printf("Storvsc_error: create multi-channel timeout, %d\n",
367                     ret);
368                 return;
369         }
370
371         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
372             vstor_packet->status != 0) {                
373                 printf("Storvsc_error: create multi-channel invalid operation "
374                     "(%d) or statue (%u)\n",
375                     vstor_packet->operation, vstor_packet->status);
376                 return;
377         }
378
379         /* Wait for sub-channels setup to complete. */
380         subchan = vmbus_get_subchan(sc->hs_chan, request_channels_cnt);
381
382         /* Attach the sub-channels. */
383         for (i = 0; i < request_channels_cnt; ++i)
384                 storvsc_subchan_attach(sc, subchan[i]);
385
386         /* Release the sub-channels. */
387         vmbus_rel_subchan(subchan, request_channels_cnt);
388
389         if (bootverbose)
390                 printf("Storvsc create multi-channel success!\n");
391 }
392
393 /**
394  * @brief initialize channel connection to parent partition
395  *
396  * @param dev  a Hyper-V device pointer
397  * @returns  0 on success, non-zero error on failure
398  */
399 static int
400 hv_storvsc_channel_init(struct storvsc_softc *sc)
401 {
402         int ret = 0, i;
403         struct hv_storvsc_request *request;
404         struct vstor_packet *vstor_packet;
405         uint16_t max_chans = 0;
406         boolean_t support_multichannel = FALSE;
407         uint32_t version;
408
409         max_chans = 0;
410         support_multichannel = FALSE;
411
412         request = &sc->hs_init_req;
413         memset(request, 0, sizeof(struct hv_storvsc_request));
414         vstor_packet = &request->vstor_packet;
415         request->softc = sc;
416
417         /**
418          * Initiate the vsc/vsp initialization protocol on the open channel
419          */
420         sema_init(&request->synch_sema, 0, ("stor_synch_sema"));
421
422         vstor_packet->operation = VSTOR_OPERATION_BEGININITIALIZATION;
423         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
424
425
426         ret = vmbus_chan_send(sc->hs_chan,
427             VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
428             vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
429
430         if (ret != 0)
431                 goto cleanup;
432
433         /* wait 5 seconds */
434         ret = sema_timedwait(&request->synch_sema, 5 * hz);
435         if (ret != 0)
436                 goto cleanup;
437
438         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
439                 vstor_packet->status != 0) {
440                 goto cleanup;
441         }
442
443         for (i = 0; i < nitems(vmstor_proto_list); i++) {
444                 /* reuse the packet for version range supported */
445
446                 memset(vstor_packet, 0, sizeof(struct vstor_packet));
447                 vstor_packet->operation = VSTOR_OPERATION_QUERYPROTOCOLVERSION;
448                 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
449
450                 vstor_packet->u.version.major_minor =
451                         vmstor_proto_list[i].proto_version;
452
453                 /* revision is only significant for Windows guests */
454                 vstor_packet->u.version.revision = 0;
455
456                 ret = vmbus_chan_send(sc->hs_chan,
457                     VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
458                     vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
459
460                 if (ret != 0)
461                         goto cleanup;
462
463                 /* wait 5 seconds */
464                 ret = sema_timedwait(&request->synch_sema, 5 * hz);
465
466                 if (ret)
467                         goto cleanup;
468
469                 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO) {
470                         ret = EINVAL;
471                         goto cleanup;   
472                 }
473                 if (vstor_packet->status == 0) {
474                         vmstor_proto_version =
475                                 vmstor_proto_list[i].proto_version;
476                         sense_buffer_size =
477                                 vmstor_proto_list[i].sense_buffer_size;
478                         vmscsi_size_delta =
479                                 vmstor_proto_list[i].vmscsi_size_delta;
480                         break;
481                 }
482         }
483
484         if (vstor_packet->status != 0) {
485                 ret = EINVAL;
486                 goto cleanup;
487         }
488         /**
489          * Query channel properties
490          */
491         memset(vstor_packet, 0, sizeof(struct vstor_packet));
492         vstor_packet->operation = VSTOR_OPERATION_QUERYPROPERTIES;
493         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
494
495         ret = vmbus_chan_send(sc->hs_chan,
496             VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
497             vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
498
499         if ( ret != 0)
500                 goto cleanup;
501
502         /* wait 5 seconds */
503         ret = sema_timedwait(&request->synch_sema, 5 * hz);
504
505         if (ret != 0)
506                 goto cleanup;
507
508         /* TODO: Check returned version */
509         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
510             vstor_packet->status != 0) {
511                 goto cleanup;
512         }
513
514         /* multi-channels feature is supported by WIN8 and above version */
515         max_chans = vstor_packet->u.chan_props.max_channel_cnt;
516         version = VMBUS_GET_VERSION(device_get_parent(sc->hs_dev), sc->hs_dev);
517         if (version != VMBUS_VERSION_WIN7 && version != VMBUS_VERSION_WS2008 &&
518             (vstor_packet->u.chan_props.flags &
519              HV_STORAGE_SUPPORTS_MULTI_CHANNEL)) {
520                 support_multichannel = TRUE;
521         }
522
523         memset(vstor_packet, 0, sizeof(struct vstor_packet));
524         vstor_packet->operation = VSTOR_OPERATION_ENDINITIALIZATION;
525         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
526
527         ret = vmbus_chan_send(sc->hs_chan,
528             VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
529             vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
530
531         if (ret != 0) {
532                 goto cleanup;
533         }
534
535         /* wait 5 seconds */
536         ret = sema_timedwait(&request->synch_sema, 5 * hz);
537
538         if (ret != 0)
539                 goto cleanup;
540
541         if (vstor_packet->operation != VSTOR_OPERATION_COMPLETEIO ||
542             vstor_packet->status != 0)
543                 goto cleanup;
544
545         /*
546          * If multi-channel is supported, send multichannel create
547          * request to host.
548          */
549         if (support_multichannel)
550                 storvsc_send_multichannel_request(sc, max_chans);
551
552 cleanup:
553         sema_destroy(&request->synch_sema);
554         return (ret);
555 }
556
557 /**
558  * @brief Open channel connection to paraent partition StorVSP driver
559  *
560  * Open and initialize channel connection to parent partition StorVSP driver.
561  *
562  * @param pointer to a Hyper-V device
563  * @returns 0 on success, non-zero error on failure
564  */
565 static int
566 hv_storvsc_connect_vsp(struct storvsc_softc *sc)
567 {       
568         int ret = 0;
569         struct vmstor_chan_props props;
570
571         memset(&props, 0, sizeof(struct vmstor_chan_props));
572
573         /*
574          * Open the channel
575          */
576         KASSERT(sc->hs_chan->hv_chan_priv1 == sc, ("invalid chan priv1"));
577         vmbus_channel_cpu_rr(sc->hs_chan);
578         ret = hv_vmbus_channel_open(
579                 sc->hs_chan,
580                 sc->hs_drv_props->drv_ringbuffer_size,
581                 sc->hs_drv_props->drv_ringbuffer_size,
582                 (void *)&props,
583                 sizeof(struct vmstor_chan_props),
584                 hv_storvsc_on_channel_callback,
585                 sc->hs_chan);
586
587         if (ret != 0) {
588                 return ret;
589         }
590
591         ret = hv_storvsc_channel_init(sc);
592
593         return (ret);
594 }
595
596 #if HVS_HOST_RESET
597 static int
598 hv_storvsc_host_reset(struct storvsc_softc *sc)
599 {
600         int ret = 0;
601
602         struct hv_storvsc_request *request;
603         struct vstor_packet *vstor_packet;
604
605         request = &sc->hs_reset_req;
606         request->softc = sc;
607         vstor_packet = &request->vstor_packet;
608
609         sema_init(&request->synch_sema, 0, "stor synch sema");
610
611         vstor_packet->operation = VSTOR_OPERATION_RESETBUS;
612         vstor_packet->flags = REQUEST_COMPLETION_FLAG;
613
614         ret = vmbus_chan_send(dev->channel,
615             VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
616             vstor_packet, VSTOR_PKT_SIZE,
617             (uint64_t)(uintptr_t)&sc->hs_reset_req);
618
619         if (ret != 0) {
620                 goto cleanup;
621         }
622
623         ret = sema_timedwait(&request->synch_sema, 5 * hz); /* KYS 5 seconds */
624
625         if (ret) {
626                 goto cleanup;
627         }
628
629
630         /*
631          * At this point, all outstanding requests in the adapter
632          * should have been flushed out and return to us
633          */
634
635 cleanup:
636         sema_destroy(&request->synch_sema);
637         return (ret);
638 }
639 #endif /* HVS_HOST_RESET */
640
641 /**
642  * @brief Function to initiate an I/O request
643  *
644  * @param device Hyper-V device pointer
645  * @param request pointer to a request structure
646  * @returns 0 on success, non-zero error on failure
647  */
648 static int
649 hv_storvsc_io_request(struct storvsc_softc *sc,
650                                           struct hv_storvsc_request *request)
651 {
652         struct vstor_packet *vstor_packet = &request->vstor_packet;
653         struct hv_vmbus_channel* outgoing_channel = NULL;
654         int ret = 0;
655
656         vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
657
658         vstor_packet->u.vm_srb.length =
659             sizeof(struct vmscsi_req) - vmscsi_size_delta;
660         
661         vstor_packet->u.vm_srb.sense_info_len = sense_buffer_size;
662
663         vstor_packet->u.vm_srb.transfer_len =
664             request->prp_list.gpa_range.gpa_len;
665
666         vstor_packet->operation = VSTOR_OPERATION_EXECUTESRB;
667
668         outgoing_channel = vmbus_select_outgoing_channel(sc->hs_chan);
669
670         mtx_unlock(&request->softc->hs_lock);
671         if (request->prp_list.gpa_range.gpa_len) {
672                 ret = vmbus_chan_send_prplist(outgoing_channel,
673                     &request->prp_list.gpa_range, request->prp_cnt,
674                     vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
675         } else {
676                 ret = vmbus_chan_send(outgoing_channel,
677                     VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
678                     vstor_packet, VSTOR_PKT_SIZE, (uint64_t)(uintptr_t)request);
679         }
680         mtx_lock(&request->softc->hs_lock);
681
682         if (ret != 0) {
683                 printf("Unable to send packet %p ret %d", vstor_packet, ret);
684         } else {
685                 atomic_add_int(&sc->hs_num_out_reqs, 1);
686         }
687
688         return (ret);
689 }
690
691
692 /**
693  * Process IO_COMPLETION_OPERATION and ready
694  * the result to be completed for upper layer
695  * processing by the CAM layer.
696  */
697 static void
698 hv_storvsc_on_iocompletion(struct storvsc_softc *sc,
699                            struct vstor_packet *vstor_packet,
700                            struct hv_storvsc_request *request)
701 {
702         struct vmscsi_req *vm_srb;
703
704         vm_srb = &vstor_packet->u.vm_srb;
705
706         /*
707          * Copy some fields of the host's response into the request structure,
708          * because the fields will be used later in storvsc_io_done().
709          */
710         request->vstor_packet.u.vm_srb.scsi_status = vm_srb->scsi_status;
711         request->vstor_packet.u.vm_srb.srb_status = vm_srb->srb_status;
712         request->vstor_packet.u.vm_srb.transfer_len = vm_srb->transfer_len;
713
714         if (((vm_srb->scsi_status & 0xFF) == SCSI_STATUS_CHECK_COND) &&
715                         (vm_srb->srb_status & SRB_STATUS_AUTOSENSE_VALID)) {
716                 /* Autosense data available */
717
718                 KASSERT(vm_srb->sense_info_len <= request->sense_info_len,
719                                 ("vm_srb->sense_info_len <= "
720                                  "request->sense_info_len"));
721
722                 memcpy(request->sense_data, vm_srb->u.sense_data,
723                         vm_srb->sense_info_len);
724
725                 request->sense_info_len = vm_srb->sense_info_len;
726         }
727
728         /* Complete request by passing to the CAM layer */
729         storvsc_io_done(request);
730         atomic_subtract_int(&sc->hs_num_out_reqs, 1);
731         if (sc->hs_drain_notify && (sc->hs_num_out_reqs == 0)) {
732                 sema_post(&sc->hs_drain_sema);
733         }
734 }
735
736 static void
737 hv_storvsc_rescan_target(struct storvsc_softc *sc)
738 {
739         path_id_t pathid;
740         target_id_t targetid;
741         union ccb *ccb;
742
743         pathid = cam_sim_path(sc->hs_sim);
744         targetid = CAM_TARGET_WILDCARD;
745
746         /*
747          * Allocate a CCB and schedule a rescan.
748          */
749         ccb = xpt_alloc_ccb_nowait();
750         if (ccb == NULL) {
751                 printf("unable to alloc CCB for rescan\n");
752                 return;
753         }
754
755         if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid, targetid,
756             CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
757                 printf("unable to create path for rescan, pathid: %u,"
758                     "targetid: %u\n", pathid, targetid);
759                 xpt_free_ccb(ccb);
760                 return;
761         }
762
763         if (targetid == CAM_TARGET_WILDCARD)
764                 ccb->ccb_h.func_code = XPT_SCAN_BUS;
765         else
766                 ccb->ccb_h.func_code = XPT_SCAN_TGT;
767
768         xpt_rescan(ccb);
769 }
770
771 static void
772 hv_storvsc_on_channel_callback(void *xchan)
773 {
774         int ret = 0;
775         hv_vmbus_channel *channel = xchan;
776         struct storvsc_softc *sc = channel->hv_chan_priv1;
777         uint32_t bytes_recvd;
778         uint64_t request_id;
779         uint8_t packet[roundup2(sizeof(struct vstor_packet), 8)];
780         struct hv_storvsc_request *request;
781         struct vstor_packet *vstor_packet;
782
783         ret = hv_vmbus_channel_recv_packet(
784                         channel,
785                         packet,
786                         roundup2(VSTOR_PKT_SIZE, 8),
787                         &bytes_recvd,
788                         &request_id);
789
790         while ((ret == 0) && (bytes_recvd > 0)) {
791                 request = (struct hv_storvsc_request *)(uintptr_t)request_id;
792
793                 if ((request == &sc->hs_init_req) ||
794                         (request == &sc->hs_reset_req)) {
795                         memcpy(&request->vstor_packet, packet,
796                                    sizeof(struct vstor_packet));
797                         sema_post(&request->synch_sema);
798                 } else {
799                         vstor_packet = (struct vstor_packet *)packet;
800                         switch(vstor_packet->operation) {
801                         case VSTOR_OPERATION_COMPLETEIO:
802                                 if (request == NULL)
803                                         panic("VMBUS: storvsc received a "
804                                             "packet with NULL request id in "
805                                             "COMPLETEIO operation.");
806
807                                 hv_storvsc_on_iocompletion(sc,
808                                                         vstor_packet, request);
809                                 break;
810                         case VSTOR_OPERATION_REMOVEDEVICE:
811                                 printf("VMBUS: storvsc operation %d not "
812                                     "implemented.\n", vstor_packet->operation);
813                                 /* TODO: implement */
814                                 break;
815                         case VSTOR_OPERATION_ENUMERATE_BUS:
816                                 hv_storvsc_rescan_target(sc);
817                                 break;
818                         default:
819                                 break;
820                         }                       
821                 }
822                 ret = hv_vmbus_channel_recv_packet(
823                                 channel,
824                                 packet,
825                                 roundup2(VSTOR_PKT_SIZE, 8),
826                                 &bytes_recvd,
827                                 &request_id);
828         }
829 }
830
831 /**
832  * @brief StorVSC probe function
833  *
834  * Device probe function.  Returns 0 if the input device is a StorVSC
835  * device.  Otherwise, a ENXIO is returned.  If the input device is
836  * for BlkVSC (paravirtual IDE) device and this support is disabled in
837  * favor of the emulated ATA/IDE device, return ENXIO.
838  *
839  * @param a device
840  * @returns 0 on success, ENXIO if not a matcing StorVSC device
841  */
842 static int
843 storvsc_probe(device_t dev)
844 {
845         int ata_disk_enable = 0;
846         int ret = ENXIO;
847         
848         switch (storvsc_get_storage_type(dev)) {
849         case DRIVER_BLKVSC:
850                 if(bootverbose)
851                         device_printf(dev, "DRIVER_BLKVSC-Emulated ATA/IDE probe\n");
852                 if (!getenv_int("hw.ata.disk_enable", &ata_disk_enable)) {
853                         if(bootverbose)
854                                 device_printf(dev,
855                                         "Enlightened ATA/IDE detected\n");
856                         device_set_desc(dev, g_drv_props_table[DRIVER_BLKVSC].drv_desc);
857                         ret = BUS_PROBE_DEFAULT;
858                 } else if(bootverbose)
859                         device_printf(dev, "Emulated ATA/IDE set (hw.ata.disk_enable set)\n");
860                 break;
861         case DRIVER_STORVSC:
862                 if(bootverbose)
863                         device_printf(dev, "Enlightened SCSI device detected\n");
864                 device_set_desc(dev, g_drv_props_table[DRIVER_STORVSC].drv_desc);
865                 ret = BUS_PROBE_DEFAULT;
866                 break;
867         default:
868                 ret = ENXIO;
869         }
870         return (ret);
871 }
872
873 /**
874  * @brief StorVSC attach function
875  *
876  * Function responsible for allocating per-device structures,
877  * setting up CAM interfaces and scanning for available LUNs to
878  * be used for SCSI device peripherals.
879  *
880  * @param a device
881  * @returns 0 on success or an error on failure
882  */
883 static int
884 storvsc_attach(device_t dev)
885 {
886         enum hv_storage_type stor_type;
887         struct storvsc_softc *sc;
888         struct cam_devq *devq;
889         int ret, i, j;
890         struct hv_storvsc_request *reqp;
891         struct root_hold_token *root_mount_token = NULL;
892         struct hv_sgl_node *sgl_node = NULL;
893         void *tmp_buff = NULL;
894
895         /*
896          * We need to serialize storvsc attach calls.
897          */
898         root_mount_token = root_mount_hold("storvsc");
899
900         sc = device_get_softc(dev);
901         sc->hs_chan = vmbus_get_channel(dev);
902         sc->hs_chan->hv_chan_priv1 = sc;
903
904         stor_type = storvsc_get_storage_type(dev);
905
906         if (stor_type == DRIVER_UNKNOWN) {
907                 ret = ENODEV;
908                 goto cleanup;
909         }
910
911         /* fill in driver specific properties */
912         sc->hs_drv_props = &g_drv_props_table[stor_type];
913
914         /* fill in device specific properties */
915         sc->hs_unit     = device_get_unit(dev);
916         sc->hs_dev      = dev;
917
918         LIST_INIT(&sc->hs_free_list);
919         mtx_init(&sc->hs_lock, "hvslck", NULL, MTX_DEF);
920
921         for (i = 0; i < sc->hs_drv_props->drv_max_ios_per_target; ++i) {
922                 reqp = malloc(sizeof(struct hv_storvsc_request),
923                                  M_DEVBUF, M_WAITOK|M_ZERO);
924                 reqp->softc = sc;
925
926                 LIST_INSERT_HEAD(&sc->hs_free_list, reqp, link);
927         }
928
929         /* create sg-list page pool */
930         if (FALSE == g_hv_sgl_page_pool.is_init) {
931                 g_hv_sgl_page_pool.is_init = TRUE;
932                 LIST_INIT(&g_hv_sgl_page_pool.in_use_sgl_list);
933                 LIST_INIT(&g_hv_sgl_page_pool.free_sgl_list);
934
935                 /*
936                  * Pre-create SG list, each SG list with
937                  * VMBUS_CHAN_PRPLIST_MAX segments, each
938                  * segment has one page buffer
939                  */
940                 for (i = 0; i < STORVSC_MAX_IO_REQUESTS; i++) {
941                         sgl_node = malloc(sizeof(struct hv_sgl_node),
942                             M_DEVBUF, M_WAITOK|M_ZERO);
943
944                         sgl_node->sgl_data =
945                             sglist_alloc(VMBUS_CHAN_PRPLIST_MAX,
946                             M_WAITOK|M_ZERO);
947
948                         for (j = 0; j < VMBUS_CHAN_PRPLIST_MAX; j++) {
949                                 tmp_buff = malloc(PAGE_SIZE,
950                                     M_DEVBUF, M_WAITOK|M_ZERO);
951
952                                 sgl_node->sgl_data->sg_segs[j].ss_paddr =
953                                     (vm_paddr_t)tmp_buff;
954                         }
955
956                         LIST_INSERT_HEAD(&g_hv_sgl_page_pool.free_sgl_list,
957                             sgl_node, link);
958                 }
959         }
960
961         sc->hs_destroy = FALSE;
962         sc->hs_drain_notify = FALSE;
963         sema_init(&sc->hs_drain_sema, 0, "Store Drain Sema");
964
965         ret = hv_storvsc_connect_vsp(sc);
966         if (ret != 0) {
967                 goto cleanup;
968         }
969
970         /*
971          * Create the device queue.
972          * Hyper-V maps each target to one SCSI HBA
973          */
974         devq = cam_simq_alloc(sc->hs_drv_props->drv_max_ios_per_target);
975         if (devq == NULL) {
976                 device_printf(dev, "Failed to alloc device queue\n");
977                 ret = ENOMEM;
978                 goto cleanup;
979         }
980
981         sc->hs_sim = cam_sim_alloc(storvsc_action,
982                                 storvsc_poll,
983                                 sc->hs_drv_props->drv_name,
984                                 sc,
985                                 sc->hs_unit,
986                                 &sc->hs_lock, 1,
987                                 sc->hs_drv_props->drv_max_ios_per_target,
988                                 devq);
989
990         if (sc->hs_sim == NULL) {
991                 device_printf(dev, "Failed to alloc sim\n");
992                 cam_simq_free(devq);
993                 ret = ENOMEM;
994                 goto cleanup;
995         }
996
997         mtx_lock(&sc->hs_lock);
998         /* bus_id is set to 0, need to get it from VMBUS channel query? */
999         if (xpt_bus_register(sc->hs_sim, dev, 0) != CAM_SUCCESS) {
1000                 cam_sim_free(sc->hs_sim, /*free_devq*/TRUE);
1001                 mtx_unlock(&sc->hs_lock);
1002                 device_printf(dev, "Unable to register SCSI bus\n");
1003                 ret = ENXIO;
1004                 goto cleanup;
1005         }
1006
1007         if (xpt_create_path(&sc->hs_path, /*periph*/NULL,
1008                  cam_sim_path(sc->hs_sim),
1009                 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1010                 xpt_bus_deregister(cam_sim_path(sc->hs_sim));
1011                 cam_sim_free(sc->hs_sim, /*free_devq*/TRUE);
1012                 mtx_unlock(&sc->hs_lock);
1013                 device_printf(dev, "Unable to create path\n");
1014                 ret = ENXIO;
1015                 goto cleanup;
1016         }
1017
1018         mtx_unlock(&sc->hs_lock);
1019
1020         root_mount_rel(root_mount_token);
1021         return (0);
1022
1023
1024 cleanup:
1025         root_mount_rel(root_mount_token);
1026         while (!LIST_EMPTY(&sc->hs_free_list)) {
1027                 reqp = LIST_FIRST(&sc->hs_free_list);
1028                 LIST_REMOVE(reqp, link);
1029                 free(reqp, M_DEVBUF);
1030         }
1031
1032         while (!LIST_EMPTY(&g_hv_sgl_page_pool.free_sgl_list)) {
1033                 sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.free_sgl_list);
1034                 LIST_REMOVE(sgl_node, link);
1035                 for (j = 0; j < VMBUS_CHAN_PRPLIST_MAX; j++) {
1036                         if (NULL !=
1037                             (void*)sgl_node->sgl_data->sg_segs[j].ss_paddr) {
1038                                 free((void*)sgl_node->sgl_data->sg_segs[j].ss_paddr, M_DEVBUF);
1039                         }
1040                 }
1041                 sglist_free(sgl_node->sgl_data);
1042                 free(sgl_node, M_DEVBUF);
1043         }
1044
1045         return (ret);
1046 }
1047
1048 /**
1049  * @brief StorVSC device detach function
1050  *
1051  * This function is responsible for safely detaching a
1052  * StorVSC device.  This includes waiting for inbound responses
1053  * to complete and freeing associated per-device structures.
1054  *
1055  * @param dev a device
1056  * returns 0 on success
1057  */
1058 static int
1059 storvsc_detach(device_t dev)
1060 {
1061         struct storvsc_softc *sc = device_get_softc(dev);
1062         struct hv_storvsc_request *reqp = NULL;
1063         struct hv_sgl_node *sgl_node = NULL;
1064         int j = 0;
1065
1066         sc->hs_destroy = TRUE;
1067
1068         /*
1069          * At this point, all outbound traffic should be disabled. We
1070          * only allow inbound traffic (responses) to proceed so that
1071          * outstanding requests can be completed.
1072          */
1073
1074         sc->hs_drain_notify = TRUE;
1075         sema_wait(&sc->hs_drain_sema);
1076         sc->hs_drain_notify = FALSE;
1077
1078         /*
1079          * Since we have already drained, we don't need to busy wait.
1080          * The call to close the channel will reset the callback
1081          * under the protection of the incoming channel lock.
1082          */
1083
1084         hv_vmbus_channel_close(sc->hs_chan);
1085
1086         mtx_lock(&sc->hs_lock);
1087         while (!LIST_EMPTY(&sc->hs_free_list)) {
1088                 reqp = LIST_FIRST(&sc->hs_free_list);
1089                 LIST_REMOVE(reqp, link);
1090
1091                 free(reqp, M_DEVBUF);
1092         }
1093         mtx_unlock(&sc->hs_lock);
1094
1095         while (!LIST_EMPTY(&g_hv_sgl_page_pool.free_sgl_list)) {
1096                 sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.free_sgl_list);
1097                 LIST_REMOVE(sgl_node, link);
1098                 for (j = 0; j < VMBUS_CHAN_PRPLIST_MAX; j++){
1099                         if (NULL !=
1100                             (void*)sgl_node->sgl_data->sg_segs[j].ss_paddr) {
1101                                 free((void*)sgl_node->sgl_data->sg_segs[j].ss_paddr, M_DEVBUF);
1102                         }
1103                 }
1104                 sglist_free(sgl_node->sgl_data);
1105                 free(sgl_node, M_DEVBUF);
1106         }
1107         
1108         return (0);
1109 }
1110
1111 #if HVS_TIMEOUT_TEST
1112 /**
1113  * @brief unit test for timed out operations
1114  *
1115  * This function provides unit testing capability to simulate
1116  * timed out operations.  Recompilation with HV_TIMEOUT_TEST=1
1117  * is required.
1118  *
1119  * @param reqp pointer to a request structure
1120  * @param opcode SCSI operation being performed
1121  * @param wait if 1, wait for I/O to complete
1122  */
1123 static void
1124 storvsc_timeout_test(struct hv_storvsc_request *reqp,
1125                 uint8_t opcode, int wait)
1126 {
1127         int ret;
1128         union ccb *ccb = reqp->ccb;
1129         struct storvsc_softc *sc = reqp->softc;
1130
1131         if (reqp->vstor_packet.vm_srb.cdb[0] != opcode) {
1132                 return;
1133         }
1134
1135         if (wait) {
1136                 mtx_lock(&reqp->event.mtx);
1137         }
1138         ret = hv_storvsc_io_request(sc, reqp);
1139         if (ret != 0) {
1140                 if (wait) {
1141                         mtx_unlock(&reqp->event.mtx);
1142                 }
1143                 printf("%s: io_request failed with %d.\n",
1144                                 __func__, ret);
1145                 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1146                 mtx_lock(&sc->hs_lock);
1147                 storvsc_free_request(sc, reqp);
1148                 xpt_done(ccb);
1149                 mtx_unlock(&sc->hs_lock);
1150                 return;
1151         }
1152
1153         if (wait) {
1154                 xpt_print(ccb->ccb_h.path,
1155                                 "%u: %s: waiting for IO return.\n",
1156                                 ticks, __func__);
1157                 ret = cv_timedwait(&reqp->event.cv, &reqp->event.mtx, 60*hz);
1158                 mtx_unlock(&reqp->event.mtx);
1159                 xpt_print(ccb->ccb_h.path, "%u: %s: %s.\n",
1160                                 ticks, __func__, (ret == 0)?
1161                                 "IO return detected" :
1162                                 "IO return not detected");
1163                 /*
1164                  * Now both the timer handler and io done are running
1165                  * simultaneously. We want to confirm the io done always
1166                  * finishes after the timer handler exits. So reqp used by
1167                  * timer handler is not freed or stale. Do busy loop for
1168                  * another 1/10 second to make sure io done does
1169                  * wait for the timer handler to complete.
1170                  */
1171                 DELAY(100*1000);
1172                 mtx_lock(&sc->hs_lock);
1173                 xpt_print(ccb->ccb_h.path,
1174                                 "%u: %s: finishing, queue frozen %d, "
1175                                 "ccb status 0x%x scsi_status 0x%x.\n",
1176                                 ticks, __func__, sc->hs_frozen,
1177                                 ccb->ccb_h.status,
1178                                 ccb->csio.scsi_status);
1179                 mtx_unlock(&sc->hs_lock);
1180         }
1181 }
1182 #endif /* HVS_TIMEOUT_TEST */
1183
1184 #ifdef notyet
1185 /**
1186  * @brief timeout handler for requests
1187  *
1188  * This function is called as a result of a callout expiring.
1189  *
1190  * @param arg pointer to a request
1191  */
1192 static void
1193 storvsc_timeout(void *arg)
1194 {
1195         struct hv_storvsc_request *reqp = arg;
1196         struct storvsc_softc *sc = reqp->softc;
1197         union ccb *ccb = reqp->ccb;
1198
1199         if (reqp->retries == 0) {
1200                 mtx_lock(&sc->hs_lock);
1201                 xpt_print(ccb->ccb_h.path,
1202                     "%u: IO timed out (req=0x%p), wait for another %u secs.\n",
1203                     ticks, reqp, ccb->ccb_h.timeout / 1000);
1204                 cam_error_print(ccb, CAM_ESF_ALL, CAM_EPF_ALL);
1205                 mtx_unlock(&sc->hs_lock);
1206
1207                 reqp->retries++;
1208                 callout_reset_sbt(&reqp->callout, SBT_1MS * ccb->ccb_h.timeout,
1209                     0, storvsc_timeout, reqp, 0);
1210 #if HVS_TIMEOUT_TEST
1211                 storvsc_timeout_test(reqp, SEND_DIAGNOSTIC, 0);
1212 #endif
1213                 return;
1214         }
1215
1216         mtx_lock(&sc->hs_lock);
1217         xpt_print(ccb->ccb_h.path,
1218                 "%u: IO (reqp = 0x%p) did not return for %u seconds, %s.\n",
1219                 ticks, reqp, ccb->ccb_h.timeout * (reqp->retries+1) / 1000,
1220                 (sc->hs_frozen == 0)?
1221                 "freezing the queue" : "the queue is already frozen");
1222         if (sc->hs_frozen == 0) {
1223                 sc->hs_frozen = 1;
1224                 xpt_freeze_simq(xpt_path_sim(ccb->ccb_h.path), 1);
1225         }
1226         mtx_unlock(&sc->hs_lock);
1227         
1228 #if HVS_TIMEOUT_TEST
1229         storvsc_timeout_test(reqp, MODE_SELECT_10, 1);
1230 #endif
1231 }
1232 #endif
1233
1234 /**
1235  * @brief StorVSC device poll function
1236  *
1237  * This function is responsible for servicing requests when
1238  * interrupts are disabled (i.e when we are dumping core.)
1239  *
1240  * @param sim a pointer to a CAM SCSI interface module
1241  */
1242 static void
1243 storvsc_poll(struct cam_sim *sim)
1244 {
1245         struct storvsc_softc *sc = cam_sim_softc(sim);
1246
1247         mtx_assert(&sc->hs_lock, MA_OWNED);
1248         mtx_unlock(&sc->hs_lock);
1249         hv_storvsc_on_channel_callback(sc->hs_chan);
1250         mtx_lock(&sc->hs_lock);
1251 }
1252
1253 /**
1254  * @brief StorVSC device action function
1255  *
1256  * This function is responsible for handling SCSI operations which
1257  * are passed from the CAM layer.  The requests are in the form of
1258  * CAM control blocks which indicate the action being performed.
1259  * Not all actions require converting the request to a VSCSI protocol
1260  * message - these actions can be responded to by this driver.
1261  * Requests which are destined for a backend storage device are converted
1262  * to a VSCSI protocol message and sent on the channel connection associated
1263  * with this device.
1264  *
1265  * @param sim pointer to a CAM SCSI interface module
1266  * @param ccb pointer to a CAM control block
1267  */
1268 static void
1269 storvsc_action(struct cam_sim *sim, union ccb *ccb)
1270 {
1271         struct storvsc_softc *sc = cam_sim_softc(sim);
1272         int res;
1273
1274         mtx_assert(&sc->hs_lock, MA_OWNED);
1275         switch (ccb->ccb_h.func_code) {
1276         case XPT_PATH_INQ: {
1277                 struct ccb_pathinq *cpi = &ccb->cpi;
1278
1279                 cpi->version_num = 1;
1280                 cpi->hba_inquiry = PI_TAG_ABLE|PI_SDTR_ABLE;
1281                 cpi->target_sprt = 0;
1282                 cpi->hba_misc = PIM_NOBUSRESET;
1283                 cpi->hba_eng_cnt = 0;
1284                 cpi->max_target = STORVSC_MAX_TARGETS;
1285                 cpi->max_lun = sc->hs_drv_props->drv_max_luns_per_target;
1286                 cpi->initiator_id = cpi->max_target;
1287                 cpi->bus_id = cam_sim_bus(sim);
1288                 cpi->base_transfer_speed = 300000;
1289                 cpi->transport = XPORT_SAS;
1290                 cpi->transport_version = 0;
1291                 cpi->protocol = PROTO_SCSI;
1292                 cpi->protocol_version = SCSI_REV_SPC2;
1293                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1294                 strncpy(cpi->hba_vid, sc->hs_drv_props->drv_name, HBA_IDLEN);
1295                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1296                 cpi->unit_number = cam_sim_unit(sim);
1297
1298                 ccb->ccb_h.status = CAM_REQ_CMP;
1299                 xpt_done(ccb);
1300                 return;
1301         }
1302         case XPT_GET_TRAN_SETTINGS: {
1303                 struct  ccb_trans_settings *cts = &ccb->cts;
1304
1305                 cts->transport = XPORT_SAS;
1306                 cts->transport_version = 0;
1307                 cts->protocol = PROTO_SCSI;
1308                 cts->protocol_version = SCSI_REV_SPC2;
1309
1310                 /* enable tag queuing and disconnected mode */
1311                 cts->proto_specific.valid = CTS_SCSI_VALID_TQ;
1312                 cts->proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
1313                 cts->proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
1314                 cts->xport_specific.valid = CTS_SPI_VALID_DISC;
1315                 cts->xport_specific.spi.flags = CTS_SPI_FLAGS_DISC_ENB;
1316                         
1317                 ccb->ccb_h.status = CAM_REQ_CMP;
1318                 xpt_done(ccb);
1319                 return;
1320         }
1321         case XPT_SET_TRAN_SETTINGS:     {
1322                 ccb->ccb_h.status = CAM_REQ_CMP;
1323                 xpt_done(ccb);
1324                 return;
1325         }
1326         case XPT_CALC_GEOMETRY:{
1327                 cam_calc_geometry(&ccb->ccg, 1);
1328                 xpt_done(ccb);
1329                 return;
1330         }
1331         case  XPT_RESET_BUS:
1332         case  XPT_RESET_DEV:{
1333 #if HVS_HOST_RESET
1334                 if ((res = hv_storvsc_host_reset(sc)) != 0) {
1335                         xpt_print(ccb->ccb_h.path,
1336                                 "hv_storvsc_host_reset failed with %d\n", res);
1337                         ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1338                         xpt_done(ccb);
1339                         return;
1340                 }
1341                 ccb->ccb_h.status = CAM_REQ_CMP;
1342                 xpt_done(ccb);
1343                 return;
1344 #else
1345                 xpt_print(ccb->ccb_h.path,
1346                                   "%s reset not supported.\n",
1347                                   (ccb->ccb_h.func_code == XPT_RESET_BUS)?
1348                                   "bus" : "dev");
1349                 ccb->ccb_h.status = CAM_REQ_INVALID;
1350                 xpt_done(ccb);
1351                 return;
1352 #endif  /* HVS_HOST_RESET */
1353         }
1354         case XPT_SCSI_IO:
1355         case XPT_IMMED_NOTIFY: {
1356                 struct hv_storvsc_request *reqp = NULL;
1357
1358                 if (ccb->csio.cdb_len == 0) {
1359                         panic("cdl_len is 0\n");
1360                 }
1361
1362                 if (LIST_EMPTY(&sc->hs_free_list)) {
1363                         ccb->ccb_h.status = CAM_REQUEUE_REQ;
1364                         if (sc->hs_frozen == 0) {
1365                                 sc->hs_frozen = 1;
1366                                 xpt_freeze_simq(sim, /* count*/1);
1367                         }
1368                         xpt_done(ccb);
1369                         return;
1370                 }
1371
1372                 reqp = LIST_FIRST(&sc->hs_free_list);
1373                 LIST_REMOVE(reqp, link);
1374
1375                 bzero(reqp, sizeof(struct hv_storvsc_request));
1376                 reqp->softc = sc;
1377                 
1378                 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1379                 if ((res = create_storvsc_request(ccb, reqp)) != 0) {
1380                         ccb->ccb_h.status = CAM_REQ_INVALID;
1381                         xpt_done(ccb);
1382                         return;
1383                 }
1384
1385 #ifdef notyet
1386                 if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1387                         callout_init(&reqp->callout, CALLOUT_MPSAFE);
1388                         callout_reset_sbt(&reqp->callout,
1389                             SBT_1MS * ccb->ccb_h.timeout, 0,
1390                             storvsc_timeout, reqp, 0);
1391 #if HVS_TIMEOUT_TEST
1392                         cv_init(&reqp->event.cv, "storvsc timeout cv");
1393                         mtx_init(&reqp->event.mtx, "storvsc timeout mutex",
1394                                         NULL, MTX_DEF);
1395                         switch (reqp->vstor_packet.vm_srb.cdb[0]) {
1396                                 case MODE_SELECT_10:
1397                                 case SEND_DIAGNOSTIC:
1398                                         /* To have timer send the request. */
1399                                         return;
1400                                 default:
1401                                         break;
1402                         }
1403 #endif /* HVS_TIMEOUT_TEST */
1404                 }
1405 #endif
1406
1407                 if ((res = hv_storvsc_io_request(sc, reqp)) != 0) {
1408                         xpt_print(ccb->ccb_h.path,
1409                                 "hv_storvsc_io_request failed with %d\n", res);
1410                         ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1411                         storvsc_free_request(sc, reqp);
1412                         xpt_done(ccb);
1413                         return;
1414                 }
1415                 return;
1416         }
1417
1418         default:
1419                 ccb->ccb_h.status = CAM_REQ_INVALID;
1420                 xpt_done(ccb);
1421                 return;
1422         }
1423 }
1424
1425 /**
1426  * @brief destroy bounce buffer
1427  *
1428  * This function is responsible for destroy a Scatter/Gather list
1429  * that create by storvsc_create_bounce_buffer()
1430  *
1431  * @param sgl- the Scatter/Gather need be destroy
1432  * @param sg_count- page count of the SG list.
1433  *
1434  */
1435 static void
1436 storvsc_destroy_bounce_buffer(struct sglist *sgl)
1437 {
1438         struct hv_sgl_node *sgl_node = NULL;
1439         if (LIST_EMPTY(&g_hv_sgl_page_pool.in_use_sgl_list)) {
1440                 printf("storvsc error: not enough in use sgl\n");
1441                 return;
1442         }
1443         sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.in_use_sgl_list);
1444         LIST_REMOVE(sgl_node, link);
1445         sgl_node->sgl_data = sgl;
1446         LIST_INSERT_HEAD(&g_hv_sgl_page_pool.free_sgl_list, sgl_node, link);
1447 }
1448
1449 /**
1450  * @brief create bounce buffer
1451  *
1452  * This function is responsible for create a Scatter/Gather list,
1453  * which hold several pages that can be aligned with page size.
1454  *
1455  * @param seg_count- SG-list segments count
1456  * @param write - if WRITE_TYPE, set SG list page used size to 0,
1457  * otherwise set used size to page size.
1458  *
1459  * return NULL if create failed
1460  */
1461 static struct sglist *
1462 storvsc_create_bounce_buffer(uint16_t seg_count, int write)
1463 {
1464         int i = 0;
1465         struct sglist *bounce_sgl = NULL;
1466         unsigned int buf_len = ((write == WRITE_TYPE) ? 0 : PAGE_SIZE);
1467         struct hv_sgl_node *sgl_node = NULL;    
1468
1469         /* get struct sglist from free_sgl_list */
1470         if (LIST_EMPTY(&g_hv_sgl_page_pool.free_sgl_list)) {
1471                 printf("storvsc error: not enough free sgl\n");
1472                 return NULL;
1473         }
1474         sgl_node = LIST_FIRST(&g_hv_sgl_page_pool.free_sgl_list);
1475         LIST_REMOVE(sgl_node, link);
1476         bounce_sgl = sgl_node->sgl_data;
1477         LIST_INSERT_HEAD(&g_hv_sgl_page_pool.in_use_sgl_list, sgl_node, link);
1478
1479         bounce_sgl->sg_maxseg = seg_count;
1480
1481         if (write == WRITE_TYPE)
1482                 bounce_sgl->sg_nseg = 0;
1483         else
1484                 bounce_sgl->sg_nseg = seg_count;
1485
1486         for (i = 0; i < seg_count; i++)
1487                 bounce_sgl->sg_segs[i].ss_len = buf_len;
1488
1489         return bounce_sgl;
1490 }
1491
1492 /**
1493  * @brief copy data from SG list to bounce buffer
1494  *
1495  * This function is responsible for copy data from one SG list's segments
1496  * to another SG list which used as bounce buffer.
1497  *
1498  * @param bounce_sgl - the destination SG list
1499  * @param orig_sgl - the segment of the source SG list.
1500  * @param orig_sgl_count - the count of segments.
1501  * @param orig_sgl_count - indicate which segment need bounce buffer,
1502  *  set 1 means need.
1503  *
1504  */
1505 static void
1506 storvsc_copy_sgl_to_bounce_buf(struct sglist *bounce_sgl,
1507                                bus_dma_segment_t *orig_sgl,
1508                                unsigned int orig_sgl_count,
1509                                uint64_t seg_bits)
1510 {
1511         int src_sgl_idx = 0;
1512
1513         for (src_sgl_idx = 0; src_sgl_idx < orig_sgl_count; src_sgl_idx++) {
1514                 if (seg_bits & (1 << src_sgl_idx)) {
1515                         memcpy((void*)bounce_sgl->sg_segs[src_sgl_idx].ss_paddr,
1516                             (void*)orig_sgl[src_sgl_idx].ds_addr,
1517                             orig_sgl[src_sgl_idx].ds_len);
1518
1519                         bounce_sgl->sg_segs[src_sgl_idx].ss_len =
1520                             orig_sgl[src_sgl_idx].ds_len;
1521                 }
1522         }
1523 }
1524
1525 /**
1526  * @brief copy data from SG list which used as bounce to another SG list
1527  *
1528  * This function is responsible for copy data from one SG list with bounce
1529  * buffer to another SG list's segments.
1530  *
1531  * @param dest_sgl - the destination SG list's segments
1532  * @param dest_sgl_count - the count of destination SG list's segment.
1533  * @param src_sgl - the source SG list.
1534  * @param seg_bits - indicate which segment used bounce buffer of src SG-list.
1535  *
1536  */
1537 void
1538 storvsc_copy_from_bounce_buf_to_sgl(bus_dma_segment_t *dest_sgl,
1539                                     unsigned int dest_sgl_count,
1540                                     struct sglist* src_sgl,
1541                                     uint64_t seg_bits)
1542 {
1543         int sgl_idx = 0;
1544         
1545         for (sgl_idx = 0; sgl_idx < dest_sgl_count; sgl_idx++) {
1546                 if (seg_bits & (1 << sgl_idx)) {
1547                         memcpy((void*)(dest_sgl[sgl_idx].ds_addr),
1548                             (void*)(src_sgl->sg_segs[sgl_idx].ss_paddr),
1549                             src_sgl->sg_segs[sgl_idx].ss_len);
1550                 }
1551         }
1552 }
1553
1554 /**
1555  * @brief check SG list with bounce buffer or not
1556  *
1557  * This function is responsible for check if need bounce buffer for SG list.
1558  *
1559  * @param sgl - the SG list's segments
1560  * @param sg_count - the count of SG list's segment.
1561  * @param bits - segmengs number that need bounce buffer
1562  *
1563  * return -1 if SG list needless bounce buffer
1564  */
1565 static int
1566 storvsc_check_bounce_buffer_sgl(bus_dma_segment_t *sgl,
1567                                 unsigned int sg_count,
1568                                 uint64_t *bits)
1569 {
1570         int i = 0;
1571         int offset = 0;
1572         uint64_t phys_addr = 0;
1573         uint64_t tmp_bits = 0;
1574         boolean_t found_hole = FALSE;
1575         boolean_t pre_aligned = TRUE;
1576
1577         if (sg_count < 2){
1578                 return -1;
1579         }
1580
1581         *bits = 0;
1582         
1583         phys_addr = vtophys(sgl[0].ds_addr);
1584         offset =  phys_addr - trunc_page(phys_addr);
1585
1586         if (offset != 0) {
1587                 pre_aligned = FALSE;
1588                 tmp_bits |= 1;
1589         }
1590
1591         for (i = 1; i < sg_count; i++) {
1592                 phys_addr = vtophys(sgl[i].ds_addr);
1593                 offset =  phys_addr - trunc_page(phys_addr);
1594
1595                 if (offset == 0) {
1596                         if (FALSE == pre_aligned){
1597                                 /*
1598                                  * This segment is aligned, if the previous
1599                                  * one is not aligned, find a hole
1600                                  */
1601                                 found_hole = TRUE;
1602                         }
1603                         pre_aligned = TRUE;
1604                 } else {
1605                         tmp_bits |= 1 << i;
1606                         if (!pre_aligned) {
1607                                 if (phys_addr != vtophys(sgl[i-1].ds_addr +
1608                                     sgl[i-1].ds_len)) {
1609                                         /*
1610                                          * Check whether connect to previous
1611                                          * segment,if not, find the hole
1612                                          */
1613                                         found_hole = TRUE;
1614                                 }
1615                         } else {
1616                                 found_hole = TRUE;
1617                         }
1618                         pre_aligned = FALSE;
1619                 }
1620         }
1621
1622         if (!found_hole) {
1623                 return (-1);
1624         } else {
1625                 *bits = tmp_bits;
1626                 return 0;
1627         }
1628 }
1629
1630 /**
1631  * @brief Fill in a request structure based on a CAM control block
1632  *
1633  * Fills in a request structure based on the contents of a CAM control
1634  * block.  The request structure holds the payload information for
1635  * VSCSI protocol request.
1636  *
1637  * @param ccb pointer to a CAM contorl block
1638  * @param reqp pointer to a request structure
1639  */
1640 static int
1641 create_storvsc_request(union ccb *ccb, struct hv_storvsc_request *reqp)
1642 {
1643         struct ccb_scsiio *csio = &ccb->csio;
1644         uint64_t phys_addr;
1645         uint32_t bytes_to_copy = 0;
1646         uint32_t pfn_num = 0;
1647         uint32_t pfn;
1648         uint64_t not_aligned_seg_bits = 0;
1649         struct hvs_gpa_range *prplist;
1650         
1651         /* refer to struct vmscsi_req for meanings of these two fields */
1652         reqp->vstor_packet.u.vm_srb.port =
1653                 cam_sim_unit(xpt_path_sim(ccb->ccb_h.path));
1654         reqp->vstor_packet.u.vm_srb.path_id =
1655                 cam_sim_bus(xpt_path_sim(ccb->ccb_h.path));
1656
1657         reqp->vstor_packet.u.vm_srb.target_id = ccb->ccb_h.target_id;
1658         reqp->vstor_packet.u.vm_srb.lun = ccb->ccb_h.target_lun;
1659
1660         reqp->vstor_packet.u.vm_srb.cdb_len = csio->cdb_len;
1661         if(ccb->ccb_h.flags & CAM_CDB_POINTER) {
1662                 memcpy(&reqp->vstor_packet.u.vm_srb.u.cdb, csio->cdb_io.cdb_ptr,
1663                         csio->cdb_len);
1664         } else {
1665                 memcpy(&reqp->vstor_packet.u.vm_srb.u.cdb, csio->cdb_io.cdb_bytes,
1666                         csio->cdb_len);
1667         }
1668
1669         switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
1670         case CAM_DIR_OUT:
1671                 reqp->vstor_packet.u.vm_srb.data_in = WRITE_TYPE;       
1672                 break;
1673         case CAM_DIR_IN:
1674                 reqp->vstor_packet.u.vm_srb.data_in = READ_TYPE;
1675                 break;
1676         case CAM_DIR_NONE:
1677                 reqp->vstor_packet.u.vm_srb.data_in = UNKNOWN_TYPE;
1678                 break;
1679         default:
1680                 reqp->vstor_packet.u.vm_srb.data_in = UNKNOWN_TYPE;
1681                 break;
1682         }
1683
1684         reqp->sense_data     = &csio->sense_data;
1685         reqp->sense_info_len = csio->sense_len;
1686
1687         reqp->ccb = ccb;
1688
1689         if (0 == csio->dxfer_len) {
1690                 return (0);
1691         }
1692
1693         prplist = &reqp->prp_list;
1694         prplist->gpa_range.gpa_len = csio->dxfer_len;
1695
1696         switch (ccb->ccb_h.flags & CAM_DATA_MASK) {
1697         case CAM_DATA_VADDR:
1698         {
1699                 bytes_to_copy = csio->dxfer_len;
1700                 phys_addr = vtophys(csio->data_ptr);
1701                 prplist->gpa_range.gpa_ofs = phys_addr & PAGE_MASK;
1702                 
1703                 while (bytes_to_copy != 0) {
1704                         int bytes, page_offset;
1705                         phys_addr =
1706                             vtophys(&csio->data_ptr[prplist->gpa_range.gpa_len -
1707                             bytes_to_copy]);
1708                         pfn = phys_addr >> PAGE_SHIFT;
1709                         prplist->gpa_page[pfn_num] = pfn;
1710                         page_offset = phys_addr & PAGE_MASK;
1711
1712                         bytes = min(PAGE_SIZE - page_offset, bytes_to_copy);
1713
1714                         bytes_to_copy -= bytes;
1715                         pfn_num++;
1716                 }
1717                 reqp->prp_cnt = pfn_num;
1718                 break;
1719         }
1720
1721         case CAM_DATA_SG:
1722         {
1723                 int i = 0;
1724                 int offset = 0;
1725                 int ret;
1726
1727                 bus_dma_segment_t *storvsc_sglist =
1728                     (bus_dma_segment_t *)ccb->csio.data_ptr;
1729                 u_int16_t storvsc_sg_count = ccb->csio.sglist_cnt;
1730
1731                 printf("Storvsc: get SG I/O operation, %d\n",
1732                     reqp->vstor_packet.u.vm_srb.data_in);
1733
1734                 if (storvsc_sg_count > VMBUS_CHAN_PRPLIST_MAX){
1735                         printf("Storvsc: %d segments is too much, "
1736                             "only support %d segments\n",
1737                             storvsc_sg_count, VMBUS_CHAN_PRPLIST_MAX);
1738                         return (EINVAL);
1739                 }
1740
1741                 /*
1742                  * We create our own bounce buffer function currently. Idealy
1743                  * we should use BUS_DMA(9) framework. But with current BUS_DMA
1744                  * code there is no callback API to check the page alignment of
1745                  * middle segments before busdma can decide if a bounce buffer
1746                  * is needed for particular segment. There is callback,
1747                  * "bus_dma_filter_t *filter", but the parrameters are not
1748                  * sufficient for storvsc driver.
1749                  * TODO:
1750                  *      Add page alignment check in BUS_DMA(9) callback. Once
1751                  *      this is complete, switch the following code to use
1752                  *      BUS_DMA(9) for storvsc bounce buffer support.
1753                  */
1754                 /* check if we need to create bounce buffer */
1755                 ret = storvsc_check_bounce_buffer_sgl(storvsc_sglist,
1756                     storvsc_sg_count, &not_aligned_seg_bits);
1757                 if (ret != -1) {
1758                         reqp->bounce_sgl =
1759                             storvsc_create_bounce_buffer(storvsc_sg_count,
1760                             reqp->vstor_packet.u.vm_srb.data_in);
1761                         if (NULL == reqp->bounce_sgl) {
1762                                 printf("Storvsc_error: "
1763                                     "create bounce buffer failed.\n");
1764                                 return (ENOMEM);
1765                         }
1766
1767                         reqp->bounce_sgl_count = storvsc_sg_count;
1768                         reqp->not_aligned_seg_bits = not_aligned_seg_bits;
1769
1770                         /*
1771                          * if it is write, we need copy the original data
1772                          *to bounce buffer
1773                          */
1774                         if (WRITE_TYPE == reqp->vstor_packet.u.vm_srb.data_in) {
1775                                 storvsc_copy_sgl_to_bounce_buf(
1776                                     reqp->bounce_sgl,
1777                                     storvsc_sglist,
1778                                     storvsc_sg_count,
1779                                     reqp->not_aligned_seg_bits);
1780                         }
1781
1782                         /* transfer virtual address to physical frame number */
1783                         if (reqp->not_aligned_seg_bits & 0x1){
1784                                 phys_addr =
1785                                     vtophys(reqp->bounce_sgl->sg_segs[0].ss_paddr);
1786                         }else{
1787                                 phys_addr =
1788                                         vtophys(storvsc_sglist[0].ds_addr);
1789                         }
1790                         prplist->gpa_range.gpa_ofs = phys_addr & PAGE_MASK;
1791
1792                         pfn = phys_addr >> PAGE_SHIFT;
1793                         prplist->gpa_page[0] = pfn;
1794                         
1795                         for (i = 1; i < storvsc_sg_count; i++) {
1796                                 if (reqp->not_aligned_seg_bits & (1 << i)) {
1797                                         phys_addr =
1798                                             vtophys(reqp->bounce_sgl->sg_segs[i].ss_paddr);
1799                                 } else {
1800                                         phys_addr =
1801                                             vtophys(storvsc_sglist[i].ds_addr);
1802                                 }
1803
1804                                 pfn = phys_addr >> PAGE_SHIFT;
1805                                 prplist->gpa_page[i] = pfn;
1806                         }
1807                         reqp->prp_cnt = i;
1808                 } else {
1809                         phys_addr = vtophys(storvsc_sglist[0].ds_addr);
1810
1811                         prplist->gpa_range.gpa_ofs = phys_addr & PAGE_MASK;
1812
1813                         for (i = 0; i < storvsc_sg_count; i++) {
1814                                 phys_addr = vtophys(storvsc_sglist[i].ds_addr);
1815                                 pfn = phys_addr >> PAGE_SHIFT;
1816                                 prplist->gpa_page[i] = pfn;
1817                         }
1818                         reqp->prp_cnt = i;
1819
1820                         /* check the last segment cross boundary or not */
1821                         offset = phys_addr & PAGE_MASK;
1822                         if (offset) {
1823                                 /* Add one more PRP entry */
1824                                 phys_addr =
1825                                     vtophys(storvsc_sglist[i-1].ds_addr +
1826                                     PAGE_SIZE - offset);
1827                                 pfn = phys_addr >> PAGE_SHIFT;
1828                                 prplist->gpa_page[i] = pfn;
1829                                 reqp->prp_cnt++;
1830                         }
1831                         
1832                         reqp->bounce_sgl_count = 0;
1833                 }
1834                 break;
1835         }
1836         default:
1837                 printf("Unknow flags: %d\n", ccb->ccb_h.flags);
1838                 return(EINVAL);
1839         }
1840
1841         return(0);
1842 }
1843
1844 /**
1845  * @brief completion function before returning to CAM
1846  *
1847  * I/O process has been completed and the result needs
1848  * to be passed to the CAM layer.
1849  * Free resources related to this request.
1850  *
1851  * @param reqp pointer to a request structure
1852  */
1853 static void
1854 storvsc_io_done(struct hv_storvsc_request *reqp)
1855 {
1856         union ccb *ccb = reqp->ccb;
1857         struct ccb_scsiio *csio = &ccb->csio;
1858         struct storvsc_softc *sc = reqp->softc;
1859         struct vmscsi_req *vm_srb = &reqp->vstor_packet.u.vm_srb;
1860         bus_dma_segment_t *ori_sglist = NULL;
1861         int ori_sg_count = 0;
1862         /* destroy bounce buffer if it is used */
1863         if (reqp->bounce_sgl_count) {
1864                 ori_sglist = (bus_dma_segment_t *)ccb->csio.data_ptr;
1865                 ori_sg_count = ccb->csio.sglist_cnt;
1866
1867                 /*
1868                  * If it is READ operation, we should copy back the data
1869                  * to original SG list.
1870                  */
1871                 if (READ_TYPE == reqp->vstor_packet.u.vm_srb.data_in) {
1872                         storvsc_copy_from_bounce_buf_to_sgl(ori_sglist,
1873                             ori_sg_count,
1874                             reqp->bounce_sgl,
1875                             reqp->not_aligned_seg_bits);
1876                 }
1877
1878                 storvsc_destroy_bounce_buffer(reqp->bounce_sgl);
1879                 reqp->bounce_sgl_count = 0;
1880         }
1881                 
1882         if (reqp->retries > 0) {
1883                 mtx_lock(&sc->hs_lock);
1884 #if HVS_TIMEOUT_TEST
1885                 xpt_print(ccb->ccb_h.path,
1886                         "%u: IO returned after timeout, "
1887                         "waking up timer handler if any.\n", ticks);
1888                 mtx_lock(&reqp->event.mtx);
1889                 cv_signal(&reqp->event.cv);
1890                 mtx_unlock(&reqp->event.mtx);
1891 #endif
1892                 reqp->retries = 0;
1893                 xpt_print(ccb->ccb_h.path,
1894                         "%u: IO returned after timeout, "
1895                         "stopping timer if any.\n", ticks);
1896                 mtx_unlock(&sc->hs_lock);
1897         }
1898
1899 #ifdef notyet
1900         /*
1901          * callout_drain() will wait for the timer handler to finish
1902          * if it is running. So we don't need any lock to synchronize
1903          * between this routine and the timer handler.
1904          * Note that we need to make sure reqp is not freed when timer
1905          * handler is using or will use it.
1906          */
1907         if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1908                 callout_drain(&reqp->callout);
1909         }
1910 #endif
1911
1912         ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1913         ccb->ccb_h.status &= ~CAM_STATUS_MASK;
1914         if (vm_srb->scsi_status == SCSI_STATUS_OK) {
1915                 const struct scsi_generic *cmd;
1916
1917                 if (vm_srb->srb_status != SRB_STATUS_SUCCESS) {
1918                         if (vm_srb->srb_status == SRB_STATUS_INVALID_LUN) {
1919                                 xpt_print(ccb->ccb_h.path, "invalid LUN %d\n",
1920                                     vm_srb->lun);
1921                         } else {
1922                                 xpt_print(ccb->ccb_h.path, "Unknown SRB flag: %d\n",
1923                                     vm_srb->srb_status);
1924                         }
1925                         /*
1926                          * If there are errors, for example, invalid LUN,
1927                          * host will inform VM through SRB status.
1928                          */
1929                         ccb->ccb_h.status |= CAM_SEL_TIMEOUT;
1930                 } else {
1931                         ccb->ccb_h.status |= CAM_REQ_CMP;
1932                 }
1933
1934                 cmd = (const struct scsi_generic *)
1935                     ((ccb->ccb_h.flags & CAM_CDB_POINTER) ?
1936                      csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes);
1937                 if (cmd->opcode == INQUIRY) {
1938                         struct scsi_inquiry_data *inq_data =
1939                             (struct scsi_inquiry_data *)csio->data_ptr;
1940                         uint8_t *resp_buf = (uint8_t *)csio->data_ptr;
1941                         int resp_xfer_len, resp_buf_len, data_len;
1942
1943                         /* Get the buffer length reported by host */
1944                         resp_xfer_len = vm_srb->transfer_len;
1945                         /* Get the available buffer length */
1946                         resp_buf_len = resp_xfer_len >= 5 ? resp_buf[4] + 5 : 0;
1947                         data_len = (resp_buf_len < resp_xfer_len) ?
1948                             resp_buf_len : resp_xfer_len;
1949
1950                         if (bootverbose && data_len >= 5) {
1951                                 xpt_print(ccb->ccb_h.path, "storvsc inquiry "
1952                                     "(%d) [%x %x %x %x %x ... ]\n", data_len,
1953                                     resp_buf[0], resp_buf[1], resp_buf[2],
1954                                     resp_buf[3], resp_buf[4]);
1955                         }
1956                         if (vm_srb->srb_status == SRB_STATUS_SUCCESS &&
1957                             data_len > SHORT_INQUIRY_LENGTH) {
1958                                 char vendor[16];
1959
1960                                 cam_strvis(vendor, inq_data->vendor,
1961                                     sizeof(inq_data->vendor), sizeof(vendor));
1962
1963                                 /*
1964                                  * XXX: Upgrade SPC2 to SPC3 if host is WIN8 or
1965                                  * WIN2012 R2 in order to support UNMAP feature.
1966                                  */
1967                                 if (!strncmp(vendor, "Msft", 4) &&
1968                                     SID_ANSI_REV(inq_data) == SCSI_REV_SPC2 &&
1969                                     (vmstor_proto_version ==
1970                                      VMSTOR_PROTOCOL_VERSION_WIN8_1 ||
1971                                      vmstor_proto_version ==
1972                                      VMSTOR_PROTOCOL_VERSION_WIN8)) {
1973                                         inq_data->version = SCSI_REV_SPC3;
1974                                         if (bootverbose) {
1975                                                 xpt_print(ccb->ccb_h.path,
1976                                                     "storvsc upgrades "
1977                                                     "SPC2 to SPC3\n");
1978                                         }
1979                                 }
1980                         }
1981                 }
1982         } else {
1983                 mtx_lock(&sc->hs_lock);
1984                 xpt_print(ccb->ccb_h.path,
1985                         "storvsc scsi_status = %d\n",
1986                         vm_srb->scsi_status);
1987                 mtx_unlock(&sc->hs_lock);
1988                 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1989         }
1990
1991         ccb->csio.scsi_status = (vm_srb->scsi_status & 0xFF);
1992         ccb->csio.resid = ccb->csio.dxfer_len - vm_srb->transfer_len;
1993
1994         if (reqp->sense_info_len != 0) {
1995                 csio->sense_resid = csio->sense_len - reqp->sense_info_len;
1996                 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1997         }
1998
1999         mtx_lock(&sc->hs_lock);
2000         if (reqp->softc->hs_frozen == 1) {
2001                 xpt_print(ccb->ccb_h.path,
2002                         "%u: storvsc unfreezing softc 0x%p.\n",
2003                         ticks, reqp->softc);
2004                 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2005                 reqp->softc->hs_frozen = 0;
2006         }
2007         storvsc_free_request(sc, reqp);
2008         mtx_unlock(&sc->hs_lock);
2009
2010         xpt_done_direct(ccb);
2011 }
2012
2013 /**
2014  * @brief Free a request structure
2015  *
2016  * Free a request structure by returning it to the free list
2017  *
2018  * @param sc pointer to a softc
2019  * @param reqp pointer to a request structure
2020  */     
2021 static void
2022 storvsc_free_request(struct storvsc_softc *sc, struct hv_storvsc_request *reqp)
2023 {
2024
2025         LIST_INSERT_HEAD(&sc->hs_free_list, reqp, link);
2026 }
2027
2028 /**
2029  * @brief Determine type of storage device from GUID
2030  *
2031  * Using the type GUID, determine if this is a StorVSC (paravirtual
2032  * SCSI or BlkVSC (paravirtual IDE) device.
2033  *
2034  * @param dev a device
2035  * returns an enum
2036  */
2037 static enum hv_storage_type
2038 storvsc_get_storage_type(device_t dev)
2039 {
2040         device_t parent = device_get_parent(dev);
2041
2042         if (VMBUS_PROBE_GUID(parent, dev, &gBlkVscDeviceType) == 0)
2043                 return DRIVER_BLKVSC;
2044         if (VMBUS_PROBE_GUID(parent, dev, &gStorVscDeviceType) == 0)
2045                 return DRIVER_STORVSC;
2046         return DRIVER_UNKNOWN;
2047 }