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