]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/hpt27xx/hpt27xx_osm_bsd.c
Update to mandoc cvs version as of 20141201
[FreeBSD/FreeBSD.git] / sys / dev / hpt27xx / hpt27xx_osm_bsd.c
1 /*-
2  * Copyright (c) 2011 HighPoint Technologies, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <dev/hpt27xx/hpt27xx_config.h>
30
31 #include <dev/hpt27xx/os_bsd.h>
32 #include <dev/hpt27xx/hptintf.h>
33
34 static HIM *hpt_match(device_t dev)
35 {
36         PCI_ID pci_id;
37         HIM *him;
38         int i;
39
40         for (him = him_list; him; him = him->next) {
41                 for (i=0; him->get_supported_device_id(i, &pci_id); i++) {
42                         if (him->get_controller_count)
43                                 him->get_controller_count(&pci_id,0,0);
44                         if ((pci_get_vendor(dev) == pci_id.vid) &&
45                                 (pci_get_device(dev) == pci_id.did)){
46                                 return (him);
47                         }
48                 }
49         }
50         return (NULL);
51 }
52
53 static int hpt_probe(device_t dev)
54 {
55         HIM *him;
56
57         him = hpt_match(dev);
58         if (him != NULL) {
59                 KdPrint(("hpt_probe: adapter at PCI %d:%d:%d, IRQ %d",
60                         pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev), pci_get_irq(dev)
61                         ));
62                 device_set_desc(dev, him->name);
63                 return (BUS_PROBE_DEFAULT);
64         }
65
66         return (ENXIO);
67 }
68
69 static int hpt_attach(device_t dev)
70 {
71         PHBA hba = (PHBA)device_get_softc(dev);
72         HIM *him;
73         PCI_ID pci_id;
74         HPT_UINT size;
75         PVBUS vbus;
76         PVBUS_EXT vbus_ext;
77         
78         KdPrint(("hpt_attach(%d/%d/%d)", pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev)));
79
80         him = hpt_match(dev);
81         hba->ext_type = EXT_TYPE_HBA;
82         hba->ldm_adapter.him = him;
83         pci_enable_busmaster(dev);
84
85         pci_id.vid = pci_get_vendor(dev);
86         pci_id.did = pci_get_device(dev);
87         pci_id.rev = pci_get_revid(dev);
88         pci_id.subsys = (HPT_U32)(pci_get_subdevice(dev)) << 16 | pci_get_subvendor(dev);
89
90         size = him->get_adapter_size(&pci_id);
91         hba->ldm_adapter.him_handle = malloc(size, M_DEVBUF, M_WAITOK);
92         if (!hba->ldm_adapter.him_handle)
93                 return ENXIO;
94
95         hba->pcidev = dev;
96         hba->pciaddr.tree = 0;
97         hba->pciaddr.bus = pci_get_bus(dev);
98         hba->pciaddr.device = pci_get_slot(dev);
99         hba->pciaddr.function = pci_get_function(dev);
100
101         if (!him->create_adapter(&pci_id, hba->pciaddr, hba->ldm_adapter.him_handle, hba)) {
102                 free(hba->ldm_adapter.him_handle, M_DEVBUF);
103                 return ENXIO;
104         }
105
106         os_printk("adapter at PCI %d:%d:%d, IRQ %d",
107                 hba->pciaddr.bus, hba->pciaddr.device, hba->pciaddr.function, pci_get_irq(dev));
108
109         if (!ldm_register_adapter(&hba->ldm_adapter)) {
110                 size = ldm_get_vbus_size();
111                 vbus_ext = malloc(sizeof(VBUS_EXT) + size, M_DEVBUF, M_WAITOK);
112                 if (!vbus_ext) {
113                         free(hba->ldm_adapter.him_handle, M_DEVBUF);
114                         return ENXIO;
115                 }
116                 memset(vbus_ext, 0, sizeof(VBUS_EXT));
117                 vbus_ext->ext_type = EXT_TYPE_VBUS;
118                 ldm_create_vbus((PVBUS)vbus_ext->vbus, vbus_ext);
119                 ldm_register_adapter(&hba->ldm_adapter);
120         }
121
122         ldm_for_each_vbus(vbus, vbus_ext) {
123                 if (hba->ldm_adapter.vbus==vbus) {
124                         hba->vbus_ext = vbus_ext;
125                         hba->next = vbus_ext->hba_list;
126                         vbus_ext->hba_list = hba;
127                         break;
128                 }
129         }
130         return 0;
131 }
132
133 /*
134  * Maybe we'd better to use the bus_dmamem_alloc to alloc DMA memory,
135  * but there are some problems currently (alignment, etc).
136  */
137 static __inline void *__get_free_pages(int order)
138 {
139         /* don't use low memory - other devices may get starved */
140         return contigmalloc(PAGE_SIZE<<order, 
141                         M_DEVBUF, M_WAITOK, BUS_SPACE_MAXADDR_24BIT, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
142 }
143
144 static __inline void free_pages(void *p, int order)
145 {
146         contigfree(p, PAGE_SIZE<<order, M_DEVBUF);
147 }
148
149 static int hpt_alloc_mem(PVBUS_EXT vbus_ext)
150 {
151         PHBA hba;
152         struct freelist *f;
153         HPT_UINT i;
154         void **p;
155
156         for (hba = vbus_ext->hba_list; hba; hba = hba->next)
157                 hba->ldm_adapter.him->get_meminfo(hba->ldm_adapter.him_handle);
158
159         ldm_get_mem_info((PVBUS)vbus_ext->vbus, 0);
160
161         for (f=vbus_ext->freelist_head; f; f=f->next) {
162                 KdPrint(("%s: %d*%d=%d bytes",
163                         f->tag, f->count, f->size, f->count*f->size));
164                 for (i=0; i<f->count; i++) {
165                         p = (void **)malloc(f->size, M_DEVBUF, M_WAITOK);
166                         if (!p) return (ENXIO);
167                         *p = f->head;
168                         f->head = p;
169                 }
170         }
171
172         for (f=vbus_ext->freelist_dma_head; f; f=f->next) {
173                 int order, size, j;
174
175                 HPT_ASSERT((f->size & (f->alignment-1))==0);
176
177                 for (order=0, size=PAGE_SIZE; size<f->size; order++, size<<=1)
178                         ;
179
180                 KdPrint(("%s: %d*%d=%d bytes, order %d",
181                         f->tag, f->count, f->size, f->count*f->size, order));
182                 HPT_ASSERT(f->alignment<=PAGE_SIZE);
183
184                 for (i=0; i<f->count;) {
185                         p = (void **)__get_free_pages(order);
186                         if (!p) return -1;
187                         for (j = size/f->size; j && i<f->count; i++,j--) {
188                                 *p = f->head;
189                                 *(BUS_ADDRESS *)(p+1) = (BUS_ADDRESS)vtophys(p);
190                                 f->head = p;
191                                 p = (void **)((unsigned long)p + f->size);
192                         }
193                 }
194         }
195         
196         HPT_ASSERT(PAGE_SIZE==DMAPOOL_PAGE_SIZE);
197
198         for (i=0; i<os_max_cache_pages; i++) {
199                 p = (void **)__get_free_pages(0);
200                 if (!p) return -1;
201                 HPT_ASSERT(((HPT_UPTR)p & (DMAPOOL_PAGE_SIZE-1))==0);
202                 dmapool_put_page((PVBUS)vbus_ext->vbus, p, (BUS_ADDRESS)vtophys(p));
203         }
204
205         return 0;
206 }
207
208 static void hpt_free_mem(PVBUS_EXT vbus_ext)
209 {
210         struct freelist *f;
211         void *p;
212         int i;
213         BUS_ADDRESS bus;
214
215         for (f=vbus_ext->freelist_head; f; f=f->next) {
216 #if DBG
217                 if (f->count!=f->reserved_count) {
218                         KdPrint(("memory leak for freelist %s (%d/%d)", f->tag, f->count, f->reserved_count));
219                 }
220 #endif
221                 while ((p=freelist_get(f)))
222                         free(p, M_DEVBUF);
223         }
224
225         for (i=0; i<os_max_cache_pages; i++) {
226                 p = dmapool_get_page((PVBUS)vbus_ext->vbus, &bus);
227                 HPT_ASSERT(p);
228                 free_pages(p, 0);
229         }
230
231         for (f=vbus_ext->freelist_dma_head; f; f=f->next) {
232                 int order, size;
233 #if DBG
234                 if (f->count!=f->reserved_count) {
235                         KdPrint(("memory leak for dma freelist %s (%d/%d)", f->tag, f->count, f->reserved_count));
236                 }
237 #endif
238                 for (order=0, size=PAGE_SIZE; size<f->size; order++, size<<=1) ;
239
240                 while ((p=freelist_get_dma(f, &bus))) {
241                         if (order)
242                                 free_pages(p, order);
243                         else {
244                         /* can't free immediately since other blocks in this page may still be in the list */
245                                 if (((HPT_UPTR)p & (PAGE_SIZE-1))==0)
246                                         dmapool_put_page((PVBUS)vbus_ext->vbus, p, bus);
247                         }
248                 }
249         }
250         
251         while ((p = dmapool_get_page((PVBUS)vbus_ext->vbus, &bus)))
252                 free_pages(p, 0);
253 }
254
255 static int hpt_init_vbus(PVBUS_EXT vbus_ext)
256 {
257         PHBA hba;
258
259         for (hba = vbus_ext->hba_list; hba; hba = hba->next)
260                 if (!hba->ldm_adapter.him->initialize(hba->ldm_adapter.him_handle)) {
261                         KdPrint(("fail to initialize %p", hba));
262                         return -1;
263                 }
264
265         ldm_initialize_vbus((PVBUS)vbus_ext->vbus, &vbus_ext->hba_list->ldm_adapter);
266         return 0;
267 }
268
269 static void hpt_flush_done(PCOMMAND pCmd)
270 {
271         PVDEV vd = pCmd->target;
272
273         if (mIsArray(vd->type) && vd->u.array.transform && vd!=vd->u.array.transform->target) {
274                 vd = vd->u.array.transform->target;
275                 HPT_ASSERT(vd);
276                 pCmd->target = vd;
277                 pCmd->Result = RETURN_PENDING;
278                 vdev_queue_cmd(pCmd);
279                 return;
280         }
281
282         *(int *)pCmd->priv = 1;
283         wakeup(pCmd);
284 }
285
286 /*
287  * flush a vdev (without retry).
288  */
289 static int hpt_flush_vdev(PVBUS_EXT vbus_ext, PVDEV vd)
290 {
291         PCOMMAND pCmd;
292         int result = 0, done;
293         HPT_UINT count;
294
295         KdPrint(("flusing dev %p", vd));
296
297         hpt_lock_vbus(vbus_ext);
298
299         if (mIsArray(vd->type) && vd->u.array.transform)
300                 count = MAX(vd->u.array.transform->source->cmds_per_request,
301                                         vd->u.array.transform->target->cmds_per_request);
302         else
303                 count = vd->cmds_per_request;
304
305         pCmd = ldm_alloc_cmds(vd->vbus, count);
306
307         if (!pCmd) {
308                 hpt_unlock_vbus(vbus_ext);
309                 return -1;
310         }
311
312         pCmd->type = CMD_TYPE_FLUSH;
313         pCmd->flags.hard_flush = 1;
314         pCmd->target = vd;
315         pCmd->done = hpt_flush_done;
316         done = 0;
317         pCmd->priv = &done;
318
319         ldm_queue_cmd(pCmd);
320         
321         if (!done) {
322                 while (hpt_sleep(vbus_ext, pCmd, PPAUSE, "hptfls", HPT_OSM_TIMEOUT)) {
323                         ldm_reset_vbus(vd->vbus);
324                 }
325         }
326
327         KdPrint(("flush result %d", pCmd->Result));
328
329         if (pCmd->Result!=RETURN_SUCCESS)
330                 result = -1;
331
332         ldm_free_cmds(pCmd);
333
334         hpt_unlock_vbus(vbus_ext);
335
336         return result;
337 }
338
339 static void hpt_stop_tasks(PVBUS_EXT vbus_ext);
340 static void hpt_shutdown_vbus(PVBUS_EXT vbus_ext, int howto)
341 {
342         PVBUS     vbus = (PVBUS)vbus_ext->vbus;
343         PHBA hba;
344         int i;
345         
346         KdPrint(("hpt_shutdown_vbus"));
347
348         /* stop all ctl tasks and disable the worker taskqueue */
349         hpt_stop_tasks(vbus_ext);
350         vbus_ext->worker.ta_context = 0;
351
352         /* flush devices */
353         for (i=0; i<osm_max_targets; i++) {
354                 PVDEV vd = ldm_find_target(vbus, i);
355                 if (vd) {
356                         /* retry once */
357                         if (hpt_flush_vdev(vbus_ext, vd))
358                                 hpt_flush_vdev(vbus_ext, vd);
359                 }
360         }
361
362         hpt_lock_vbus(vbus_ext);
363         ldm_shutdown(vbus);
364         hpt_unlock_vbus(vbus_ext);
365
366         ldm_release_vbus(vbus);
367
368         for (hba=vbus_ext->hba_list; hba; hba=hba->next)
369                 bus_teardown_intr(hba->pcidev, hba->irq_res, hba->irq_handle);
370
371         hpt_free_mem(vbus_ext);
372
373         while ((hba=vbus_ext->hba_list)) {
374                 vbus_ext->hba_list = hba->next;
375                 free(hba->ldm_adapter.him_handle, M_DEVBUF);
376         }
377
378         free(vbus_ext, M_DEVBUF);
379         KdPrint(("hpt_shutdown_vbus done"));
380 }
381
382 static void __hpt_do_tasks(PVBUS_EXT vbus_ext)
383 {
384         OSM_TASK *tasks;
385
386         tasks = vbus_ext->tasks;
387         vbus_ext->tasks = 0;
388
389         while (tasks) {
390                 OSM_TASK *t = tasks;
391                 tasks = t->next;
392                 t->next = 0;
393                 t->func(vbus_ext->vbus, t->data);
394         }
395 }
396
397 static void hpt_do_tasks(PVBUS_EXT vbus_ext, int pending)
398 {
399         if(vbus_ext){
400                 hpt_lock_vbus(vbus_ext);
401                 __hpt_do_tasks(vbus_ext);
402                 hpt_unlock_vbus(vbus_ext);
403         }
404 }
405
406 static void hpt_action(struct cam_sim *sim, union ccb *ccb);
407 static void hpt_poll(struct cam_sim *sim);
408 static void hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, void * arg);
409 static void hpt_pci_intr(void *arg);
410
411 static __inline POS_CMDEXT cmdext_get(PVBUS_EXT vbus_ext)
412 {
413         POS_CMDEXT p = vbus_ext->cmdext_list;
414         if (p)
415                 vbus_ext->cmdext_list = p->next;
416         return p;
417 }
418
419 static __inline void cmdext_put(POS_CMDEXT p)
420 {
421         p->next = p->vbus_ext->cmdext_list;
422         p->vbus_ext->cmdext_list = p;
423 }
424
425 static void hpt_timeout(void *arg)
426 {
427         PCOMMAND pCmd = (PCOMMAND)arg;
428         POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
429         
430         KdPrint(("pCmd %p timeout", pCmd));
431         
432         ldm_reset_vbus((PVBUS)ext->vbus_ext->vbus);
433 }
434
435 static void os_cmddone(PCOMMAND pCmd)
436 {
437         POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
438         union ccb *ccb = ext->ccb;
439
440         KdPrint(("os_cmddone(%p, %d)", pCmd, pCmd->Result));
441
442         callout_stop(&ext->timeout);
443
444         switch(pCmd->Result) {
445         case RETURN_SUCCESS:
446                 ccb->ccb_h.status = CAM_REQ_CMP;
447                 break;
448         case RETURN_BAD_DEVICE:
449                 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
450                 break;
451         case RETURN_DEVICE_BUSY:
452                 ccb->ccb_h.status = CAM_BUSY;
453                 break;
454         case RETURN_INVALID_REQUEST:
455                 ccb->ccb_h.status = CAM_REQ_INVALID;
456                 break;
457         case RETURN_SELECTION_TIMEOUT:
458                 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
459                 break;
460         case RETURN_RETRY:
461                 ccb->ccb_h.status = CAM_BUSY;
462                 break;
463         default:
464                 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
465                 break;
466         }
467
468         if (pCmd->flags.data_in) {
469                 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_POSTREAD);
470         }
471         else if (pCmd->flags.data_out) {
472                 bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_POSTWRITE);
473         }
474         
475         bus_dmamap_unload(ext->vbus_ext->io_dmat, ext->dma_map);
476
477         cmdext_put(ext);
478         ldm_free_cmds(pCmd);
479         xpt_done(ccb);
480 }
481
482 static int os_buildsgl(PCOMMAND pCmd, PSG pSg, int logical)
483 {
484         POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
485         union ccb *ccb = ext->ccb;
486
487         if (logical) {
488                 os_set_sgptr(pSg, (HPT_U8 *)ccb->csio.data_ptr);
489                 pSg->size = ccb->csio.dxfer_len;
490                 pSg->eot = 1;
491                 return TRUE;
492         }
493
494         /* since we have provided physical sg, nobody will ask us to build physical sg */
495         HPT_ASSERT(0);
496         return FALSE;
497 }
498
499 static void hpt_io_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
500 {
501         PCOMMAND pCmd = (PCOMMAND)arg;
502         POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
503         PSG psg = pCmd->psg;
504         int idx;
505         
506         HPT_ASSERT(pCmd->flags.physical_sg);
507         
508         if (error)
509                 panic("busdma error");
510                 
511         HPT_ASSERT(nsegs<=os_max_sg_descriptors);
512
513         if (nsegs != 0) {
514                 for (idx = 0; idx < nsegs; idx++, psg++) {
515                         psg->addr.bus = segs[idx].ds_addr;
516                         psg->size = segs[idx].ds_len;
517                         psg->eot = 0;
518                 }
519                 psg[-1].eot = 1;
520         
521                 if (pCmd->flags.data_in) {
522                         bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map,
523                             BUS_DMASYNC_PREREAD);
524                 }
525                 else if (pCmd->flags.data_out) {
526                         bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map,
527                             BUS_DMASYNC_PREWRITE);
528                 }
529         }
530         callout_reset(&ext->timeout, HPT_OSM_TIMEOUT, hpt_timeout, pCmd);
531         ldm_queue_cmd(pCmd);
532 }
533
534 static void hpt_scsi_io(PVBUS_EXT vbus_ext, union ccb *ccb)
535 {
536         PVBUS vbus = (PVBUS)vbus_ext->vbus;
537         PVDEV vd;
538         PCOMMAND pCmd;
539         POS_CMDEXT ext;
540         HPT_U8 *cdb;
541
542         if (ccb->ccb_h.flags & CAM_CDB_POINTER)
543                 cdb = ccb->csio.cdb_io.cdb_ptr;
544         else
545                 cdb = ccb->csio.cdb_io.cdb_bytes;
546         
547         KdPrint(("hpt_scsi_io: ccb %x id %d lun %d cdb %x-%x-%x",
548                 ccb,
549                 ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
550                 *(HPT_U32 *)&cdb[0], *(HPT_U32 *)&cdb[4], *(HPT_U32 *)&cdb[8]
551         ));
552
553         /* ccb->ccb_h.path_id is not our bus id - don't check it */
554         if (ccb->ccb_h.target_lun != 0 ||
555                 ccb->ccb_h.target_id >= osm_max_targets ||
556                 (ccb->ccb_h.flags & CAM_CDB_PHYS))
557         {
558                 ccb->ccb_h.status = CAM_TID_INVALID;
559                 xpt_done(ccb);
560                 return;
561         }
562
563         vd = ldm_find_target(vbus, ccb->ccb_h.target_id);
564
565         if (!vd) {
566                 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
567                 xpt_done(ccb);
568                 return;
569         }
570    
571         switch (cdb[0]) {
572         case TEST_UNIT_READY:
573         case START_STOP_UNIT:
574         case SYNCHRONIZE_CACHE:
575                 ccb->ccb_h.status = CAM_REQ_CMP;
576                 break;
577
578         case INQUIRY:
579                 {
580                         PINQUIRYDATA inquiryData;
581                         memset(ccb->csio.data_ptr, 0, ccb->csio.dxfer_len);
582                         inquiryData = (PINQUIRYDATA)ccb->csio.data_ptr;
583                 
584                         inquiryData->AdditionalLength = 31;
585                         inquiryData->CommandQueue = 1;
586                         memcpy(&inquiryData->VendorId, "HPT     ", 8);
587                         memcpy(&inquiryData->ProductId, "DISK 0_0        ", 16);
588         
589                         if (vd->target_id / 10) {
590                                 inquiryData->ProductId[7] = (vd->target_id % 100) / 10 + '0';
591                                 inquiryData->ProductId[8] = (vd->target_id % 100) % 10 + '0';
592                         }
593                         else
594                                 inquiryData->ProductId[7] = (vd->target_id % 100) % 10 + '0';
595         
596                         memcpy(&inquiryData->ProductRevisionLevel, "4.00", 4);
597         
598                         ccb->ccb_h.status = CAM_REQ_CMP;
599                 }
600                 break;
601
602         case READ_CAPACITY:
603         {
604                 HPT_U8 *rbuf = ccb->csio.data_ptr;
605                 HPT_U32 cap;
606                 
607                 if (vd->capacity>0xfffffffful)
608                         cap = 0xfffffffful;
609                 else
610                         cap = vd->capacity - 1;
611         
612                 rbuf[0] = (HPT_U8)(cap>>24);
613                 rbuf[1] = (HPT_U8)(cap>>16);
614                 rbuf[2] = (HPT_U8)(cap>>8);
615                 rbuf[3] = (HPT_U8)cap;
616                 rbuf[4] = 0;
617                 rbuf[5] = 0;
618                 rbuf[6] = 2;
619                 rbuf[7] = 0;
620
621                 ccb->ccb_h.status = CAM_REQ_CMP;
622                 break;
623         }
624         
625         case SERVICE_ACTION_IN: 
626         {
627                 HPT_U8 *rbuf = ccb->csio.data_ptr;
628                 HPT_U64 cap = vd->capacity - 1;
629                 
630                 rbuf[0] = (HPT_U8)(cap>>56);
631                 rbuf[1] = (HPT_U8)(cap>>48);
632                 rbuf[2] = (HPT_U8)(cap>>40);
633                 rbuf[3] = (HPT_U8)(cap>>32);
634                 rbuf[4] = (HPT_U8)(cap>>24);
635                 rbuf[5] = (HPT_U8)(cap>>16);
636                 rbuf[6] = (HPT_U8)(cap>>8);
637                 rbuf[7] = (HPT_U8)cap;
638                 rbuf[8] = 0;
639                 rbuf[9] = 0;
640                 rbuf[10] = 2;
641                 rbuf[11] = 0;
642                 
643                 ccb->ccb_h.status = CAM_REQ_CMP;
644                 break;  
645         }
646         
647         case READ_6:
648         case READ_10:
649         case READ_16:
650         case WRITE_6:
651         case WRITE_10:
652         case WRITE_16:
653         case 0x13:
654         case 0x2f:
655         case 0x8f: /* VERIFY_16 */
656         {
657                 int error;
658                 pCmd = ldm_alloc_cmds(vbus, vd->cmds_per_request);
659                 if(!pCmd){
660                         KdPrint(("Failed to allocate command!"));
661                         ccb->ccb_h.status = CAM_BUSY;
662                         break;
663                 }
664
665                 switch (cdb[0]) {
666                 case READ_6:
667                 case WRITE_6:
668                 case 0x13:
669                         pCmd->uCmd.Ide.Lba =  ((HPT_U32)cdb[1] << 16) | ((HPT_U32)cdb[2] << 8) | (HPT_U32)cdb[3];
670                         pCmd->uCmd.Ide.nSectors = (HPT_U16) cdb[4];
671                         break;
672                 case READ_16:
673                 case WRITE_16:
674                 case 0x8f: /* VERIFY_16 */
675                 {
676                         HPT_U64 block =
677                                 ((HPT_U64)cdb[2]<<56) |
678                                 ((HPT_U64)cdb[3]<<48) |
679                                 ((HPT_U64)cdb[4]<<40) |
680                                 ((HPT_U64)cdb[5]<<32) |
681                                 ((HPT_U64)cdb[6]<<24) |
682                                 ((HPT_U64)cdb[7]<<16) |
683                                 ((HPT_U64)cdb[8]<<8) |
684                                 ((HPT_U64)cdb[9]);
685                         pCmd->uCmd.Ide.Lba = block;
686                         pCmd->uCmd.Ide.nSectors = (HPT_U16)cdb[13] | ((HPT_U16)cdb[12]<<8);
687                         break;
688                 }
689                 
690                 default:
691                         pCmd->uCmd.Ide.Lba = (HPT_U32)cdb[5] | ((HPT_U32)cdb[4] << 8) | ((HPT_U32)cdb[3] << 16) | ((HPT_U32)cdb[2] << 24);
692                         pCmd->uCmd.Ide.nSectors = (HPT_U16) cdb[8] | ((HPT_U16)cdb[7]<<8);
693                         break;
694                 }
695                 
696                 switch (cdb[0]) {
697                 case READ_6:
698                 case READ_10:
699                 case READ_16:
700                         pCmd->flags.data_in = 1;
701                         break;
702                 case WRITE_6:
703                 case WRITE_10:
704                 case WRITE_16:
705                         pCmd->flags.data_out = 1;
706                         break;
707                 }
708                 pCmd->priv = ext = cmdext_get(vbus_ext);
709                 HPT_ASSERT(ext);
710                 ext->ccb = ccb;
711                 pCmd->target = vd;
712                 pCmd->done = os_cmddone;
713                 pCmd->buildsgl = os_buildsgl;
714                 pCmd->psg = ext->psg;
715                 pCmd->flags.physical_sg = 1;
716                 error = bus_dmamap_load_ccb(vbus_ext->io_dmat, 
717                                         ext->dma_map, ccb, 
718                                         hpt_io_dmamap_callback, pCmd,
719                                         BUS_DMA_WAITOK
720                                         );
721                 KdPrint(("bus_dmamap_load return %d", error));
722                 if (error && error!=EINPROGRESS) {
723                         os_printk("bus_dmamap_load error %d", error);
724                         cmdext_put(ext);
725                         ldm_free_cmds(pCmd);
726                         ccb->ccb_h.status = CAM_REQ_CMP_ERR;
727                         xpt_done(ccb);
728                 }
729                 return;
730         }
731
732         default:
733                 ccb->ccb_h.status = CAM_REQ_INVALID;
734                 break;
735         }
736
737         xpt_done(ccb);
738         return;
739 }
740
741 static void hpt_action(struct cam_sim *sim, union ccb *ccb)
742 {
743         PVBUS_EXT vbus_ext = (PVBUS_EXT)cam_sim_softc(sim);
744
745         KdPrint(("hpt_action(fn=%d, id=%d)", ccb->ccb_h.func_code, ccb->ccb_h.target_id));
746
747         hpt_assert_vbus_locked(vbus_ext);
748         switch (ccb->ccb_h.func_code) {
749         
750         case XPT_SCSI_IO:
751                 hpt_scsi_io(vbus_ext, ccb);
752                 return;
753
754         case XPT_RESET_BUS:
755                 ldm_reset_vbus((PVBUS)vbus_ext->vbus);
756                 break;
757
758         case XPT_GET_TRAN_SETTINGS:
759         case XPT_SET_TRAN_SETTINGS:
760                 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
761                 break;
762
763         case XPT_CALC_GEOMETRY:
764                 ccb->ccg.heads = 255;
765                 ccb->ccg.secs_per_track = 63;
766                 ccb->ccg.cylinders = ccb->ccg.volume_size / (ccb->ccg.heads * ccb->ccg.secs_per_track);
767                 ccb->ccb_h.status = CAM_REQ_CMP;
768                 break;
769
770         case XPT_PATH_INQ:
771         {
772                 struct ccb_pathinq *cpi = &ccb->cpi;
773
774                 cpi->version_num = 1;
775                 cpi->hba_inquiry = PI_SDTR_ABLE;
776                 cpi->target_sprt = 0;
777                 cpi->hba_misc = PIM_NOBUSRESET;
778                 cpi->hba_eng_cnt = 0;
779                 cpi->max_target = osm_max_targets;
780                 cpi->max_lun = 0;
781                 cpi->unit_number = cam_sim_unit(sim);
782                 cpi->bus_id = cam_sim_bus(sim);
783                 cpi->initiator_id = osm_max_targets;
784                 cpi->base_transfer_speed = 3300;
785
786                 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
787                 strncpy(cpi->hba_vid, "HPT   ", HBA_IDLEN);
788                 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
789                 cpi->transport = XPORT_SPI;
790                 cpi->transport_version = 2;
791                 cpi->protocol = PROTO_SCSI;
792                 cpi->protocol_version = SCSI_REV_2;
793                 cpi->ccb_h.status = CAM_REQ_CMP;
794                 break;
795         }
796
797         default:
798                 ccb->ccb_h.status = CAM_REQ_INVALID;
799                 break;
800         }
801
802         xpt_done(ccb);
803         return;
804 }
805
806 static void hpt_pci_intr(void *arg)
807 {       
808         PVBUS_EXT vbus_ext = (PVBUS_EXT)arg;
809         hpt_lock_vbus(vbus_ext);
810         ldm_intr((PVBUS)vbus_ext->vbus);
811         hpt_unlock_vbus(vbus_ext);
812 }
813
814 static void hpt_poll(struct cam_sim *sim)
815 {
816         PVBUS_EXT vbus_ext = (PVBUS_EXT)cam_sim_softc(sim);
817
818         hpt_assert_vbus_locked(vbus_ext);
819         ldm_intr((PVBUS)vbus_ext->vbus);
820 }
821
822 static void hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, void * arg)
823 {
824         KdPrint(("hpt_async"));
825 }
826
827 static int hpt_shutdown(device_t dev)
828 {
829         KdPrint(("hpt_shutdown(dev=%p)", dev));
830         return 0;
831 }
832
833 static int hpt_detach(device_t dev)
834 {
835         /* we don't allow the driver to be unloaded. */
836         return EBUSY;
837 }
838
839 static void hpt_ioctl_done(struct _IOCTL_ARG *arg)
840 {
841         arg->ioctl_cmnd = 0;
842         wakeup(arg);
843 }
844
845 static void __hpt_do_ioctl(PVBUS_EXT vbus_ext, IOCTL_ARG *ioctl_args)
846 {
847         ioctl_args->result = -1;
848         ioctl_args->done = hpt_ioctl_done;
849         ioctl_args->ioctl_cmnd = (void *)1;
850
851         hpt_lock_vbus(vbus_ext);
852         ldm_ioctl((PVBUS)vbus_ext->vbus, ioctl_args);
853
854         while (ioctl_args->ioctl_cmnd) {
855                 if (hpt_sleep(vbus_ext, ioctl_args, PPAUSE, "hptctl", HPT_OSM_TIMEOUT)==0)
856                         break;
857                 ldm_reset_vbus((PVBUS)vbus_ext->vbus);
858                 __hpt_do_tasks(vbus_ext);
859         }
860
861         /* KdPrint(("ioctl %x result %d", ioctl_args->dwIoControlCode, ioctl_args->result)); */
862
863         hpt_unlock_vbus(vbus_ext);
864 }
865
866 static void hpt_do_ioctl(IOCTL_ARG *ioctl_args)
867 {
868         PVBUS vbus;
869         PVBUS_EXT vbus_ext;
870
871         ldm_for_each_vbus(vbus, vbus_ext) {
872                 __hpt_do_ioctl(vbus_ext, ioctl_args);
873                 if (ioctl_args->result!=HPT_IOCTL_RESULT_WRONG_VBUS)
874                         return;
875         }
876 }
877
878 #define HPT_DO_IOCTL(code, inbuf, insize, outbuf, outsize) ({\
879         IOCTL_ARG arg;\
880         arg.dwIoControlCode = code;\
881         arg.lpInBuffer = inbuf;\
882         arg.lpOutBuffer = outbuf;\
883         arg.nInBufferSize = insize;\
884         arg.nOutBufferSize = outsize;\
885         arg.lpBytesReturned = 0;\
886         hpt_do_ioctl(&arg);\
887         arg.result;\
888 })
889
890 #define DEVICEID_VALID(id) ((id) && ((HPT_U32)(id)!=0xffffffff))
891
892 static int hpt_get_logical_devices(DEVICEID * pIds, int nMaxCount)
893 {
894         int i;
895         HPT_U32 count = nMaxCount-1;
896         
897         if (HPT_DO_IOCTL(HPT_IOCTL_GET_LOGICAL_DEVICES,
898                         &count, sizeof(HPT_U32), pIds, sizeof(DEVICEID)*nMaxCount))
899                 return -1;
900
901         nMaxCount = (int)pIds[0];
902         for (i=0; i<nMaxCount; i++) pIds[i] = pIds[i+1];
903         return nMaxCount;
904 }
905
906 static int hpt_get_device_info_v3(DEVICEID id, PLOGICAL_DEVICE_INFO_V3 pInfo)
907 {
908         return HPT_DO_IOCTL(HPT_IOCTL_GET_DEVICE_INFO_V3,
909                                 &id, sizeof(DEVICEID), pInfo, sizeof(LOGICAL_DEVICE_INFO_V3));
910 }
911
912 /* not belong to this file logically, but we want to use ioctl interface */
913 static int __hpt_stop_tasks(PVBUS_EXT vbus_ext, DEVICEID id)
914 {
915         LOGICAL_DEVICE_INFO_V3 devinfo;
916         int i, result;
917         DEVICEID param[2] = { id, 0 };
918         
919         if (hpt_get_device_info_v3(id, &devinfo))
920                 return -1;
921                 
922         if (devinfo.Type!=LDT_ARRAY)
923                 return -1;
924                 
925         if (devinfo.u.array.Flags & ARRAY_FLAG_REBUILDING)
926                 param[1] = AS_REBUILD_ABORT;
927         else if (devinfo.u.array.Flags & ARRAY_FLAG_VERIFYING)
928                 param[1] = AS_VERIFY_ABORT;
929         else if (devinfo.u.array.Flags & ARRAY_FLAG_INITIALIZING)
930                 param[1] = AS_INITIALIZE_ABORT;
931         else if (devinfo.u.array.Flags & ARRAY_FLAG_TRANSFORMING)
932                 param[1] = AS_TRANSFORM_ABORT;
933         else
934                 return -1;
935
936         KdPrint(("SET_ARRAY_STATE(%x, %d)", param[0], param[1]));
937         result = HPT_DO_IOCTL(HPT_IOCTL_SET_ARRAY_STATE,
938                                 param, sizeof(param), 0, 0);
939                                 
940         for (i=0; i<devinfo.u.array.nDisk; i++)
941                 if (DEVICEID_VALID(devinfo.u.array.Members[i]))
942                         __hpt_stop_tasks(vbus_ext, devinfo.u.array.Members[i]);
943                         
944         return result;
945 }
946
947 static void hpt_stop_tasks(PVBUS_EXT vbus_ext)
948 {
949         DEVICEID ids[32];
950         int i, count;
951
952         count = hpt_get_logical_devices((DEVICEID *)&ids, sizeof(ids)/sizeof(ids[0]));
953         
954         for (i=0; i<count; i++)
955                 __hpt_stop_tasks(vbus_ext, ids[i]);
956 }
957
958 static  d_open_t        hpt_open;
959 static  d_close_t       hpt_close;
960 static  d_ioctl_t       hpt_ioctl;
961 static  int             hpt_rescan_bus(void);
962
963 static struct cdevsw hpt_cdevsw = {
964         .d_open =       hpt_open,
965         .d_close =      hpt_close,
966         .d_ioctl =      hpt_ioctl,
967         .d_name =       driver_name,
968         .d_version =    D_VERSION,
969 };
970
971 static struct intr_config_hook hpt_ich;
972
973 /*
974  * hpt_final_init will be called after all hpt_attach.
975  */
976 static void hpt_final_init(void *dummy)
977 {
978         int       i,unit_number=0;
979         PVBUS_EXT vbus_ext;
980         PVBUS vbus;
981         PHBA hba;
982
983         /* Clear the config hook */
984         config_intrhook_disestablish(&hpt_ich);
985
986         /* allocate memory */
987         i = 0;
988         ldm_for_each_vbus(vbus, vbus_ext) {
989                 if (hpt_alloc_mem(vbus_ext)) {
990                         os_printk("out of memory");
991                         return;
992                 }
993                 i++;
994         }
995
996         if (!i) {
997                 if (bootverbose)
998                         os_printk("no controller detected.");
999                 return;
1000         }
1001
1002         /* initializing hardware */
1003         ldm_for_each_vbus(vbus, vbus_ext) {
1004                 /* make timer available here */
1005                 mtx_init(&vbus_ext->lock, "hptsleeplock", NULL, MTX_DEF);
1006                 callout_init_mtx(&vbus_ext->timer, &vbus_ext->lock, 0);
1007                 if (hpt_init_vbus(vbus_ext)) {
1008                         os_printk("fail to initialize hardware");
1009                         break; /* FIXME */
1010                 }
1011         }
1012
1013         /* register CAM interface */
1014         ldm_for_each_vbus(vbus, vbus_ext) {
1015                 struct cam_devq *devq;
1016                 struct ccb_setasync     ccb;
1017                 
1018                 if (bus_dma_tag_create(NULL,/* parent */
1019                                 4,      /* alignment */
1020                                 BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
1021                                 BUS_SPACE_MAXADDR,      /* lowaddr */
1022                                 BUS_SPACE_MAXADDR,      /* highaddr */
1023                                 NULL, NULL,             /* filter, filterarg */
1024                                 PAGE_SIZE * (os_max_sg_descriptors-1),  /* maxsize */
1025                                 os_max_sg_descriptors,  /* nsegments */
1026                                 0x10000,        /* maxsegsize */
1027                                 BUS_DMA_WAITOK,         /* flags */
1028                                 busdma_lock_mutex,      /* lockfunc */
1029                                 &vbus_ext->lock,                /* lockfuncarg */
1030                                 &vbus_ext->io_dmat      /* tag */))
1031                 {
1032                         return ;
1033                 }
1034
1035                 for (i=0; i<os_max_queue_comm; i++) {
1036                         POS_CMDEXT ext = (POS_CMDEXT)malloc(sizeof(OS_CMDEXT), M_DEVBUF, M_WAITOK);
1037                         if (!ext) {
1038                                 os_printk("Can't alloc cmdext(%d)", i);
1039                                 return ;
1040                         }
1041                         ext->vbus_ext = vbus_ext;
1042                         ext->next = vbus_ext->cmdext_list;
1043                         vbus_ext->cmdext_list = ext;
1044         
1045                         if (bus_dmamap_create(vbus_ext->io_dmat, 0, &ext->dma_map)) {
1046                                 os_printk("Can't create dma map(%d)", i);
1047                                 return ;
1048                         }
1049                         callout_init_mtx(&ext->timeout, &vbus_ext->lock, 0);
1050                 }
1051
1052                 if ((devq = cam_simq_alloc(os_max_queue_comm)) == NULL) {
1053                         os_printk("cam_simq_alloc failed");
1054                         return ;
1055                 }
1056
1057                 vbus_ext->sim = cam_sim_alloc(hpt_action, hpt_poll, driver_name,
1058                                 vbus_ext, unit_number, &vbus_ext->lock, os_max_queue_comm, /*tagged*/8,  devq);
1059                 unit_number++;
1060                 if (!vbus_ext->sim) {
1061                         os_printk("cam_sim_alloc failed");
1062                         cam_simq_free(devq);
1063                         return ;
1064                 }
1065
1066                 hpt_lock_vbus(vbus_ext);
1067                 if (xpt_bus_register(vbus_ext->sim, NULL, 0) != CAM_SUCCESS) {
1068                         hpt_unlock_vbus(vbus_ext);
1069                         os_printk("xpt_bus_register failed");
1070                         cam_sim_free(vbus_ext->sim, /*free devq*/ TRUE);
1071                         vbus_ext->sim = NULL;
1072                         return ;
1073                 }
1074         
1075                 if (xpt_create_path(&vbus_ext->path, /*periph */ NULL,
1076                                 cam_sim_path(vbus_ext->sim), CAM_TARGET_WILDCARD,
1077                                 CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1078                 {
1079                         hpt_unlock_vbus(vbus_ext);
1080                         os_printk("xpt_create_path failed");
1081                         xpt_bus_deregister(cam_sim_path(vbus_ext->sim));
1082                         cam_sim_free(vbus_ext->sim, /*free_devq*/TRUE);
1083                         vbus_ext->sim = NULL;
1084                         return ;
1085                 }
1086
1087                 xpt_setup_ccb(&ccb.ccb_h, vbus_ext->path, /*priority*/5);
1088                 ccb.ccb_h.func_code = XPT_SASYNC_CB;
1089                 ccb.event_enable = AC_LOST_DEVICE;
1090                 ccb.callback = hpt_async;
1091                 ccb.callback_arg = vbus_ext;
1092                 xpt_action((union ccb *)&ccb);
1093                 hpt_unlock_vbus(vbus_ext);
1094
1095                 for (hba = vbus_ext->hba_list; hba; hba = hba->next) {
1096                         int rid = 0;
1097                         if ((hba->irq_res = bus_alloc_resource(hba->pcidev,
1098                                 SYS_RES_IRQ, &rid, 0, ~0ul, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL)
1099                         {
1100                                 os_printk("can't allocate interrupt");
1101                                 return ;
1102                         }
1103                         
1104                         if (bus_setup_intr(hba->pcidev, hba->irq_res, INTR_TYPE_CAM | INTR_MPSAFE,
1105                                 NULL, hpt_pci_intr, vbus_ext, &hba->irq_handle)) 
1106                         {
1107                                 os_printk("can't set up interrupt");
1108                                 return ;
1109                         }
1110                         hba->ldm_adapter.him->intr_control(hba->ldm_adapter.him_handle, HPT_TRUE);
1111
1112                 }
1113
1114                 vbus_ext->shutdown_eh = EVENTHANDLER_REGISTER(shutdown_final, 
1115                                                                         hpt_shutdown_vbus, vbus_ext, SHUTDOWN_PRI_DEFAULT);
1116                 if (!vbus_ext->shutdown_eh)
1117                         os_printk("Shutdown event registration failed");
1118         }
1119         
1120         ldm_for_each_vbus(vbus, vbus_ext) {
1121                 TASK_INIT(&vbus_ext->worker, 0, (task_fn_t *)hpt_do_tasks, vbus_ext);
1122                 if (vbus_ext->tasks)
1123                         TASK_ENQUEUE(&vbus_ext->worker);
1124         }       
1125
1126         make_dev(&hpt_cdevsw, DRIVER_MINOR, UID_ROOT, GID_OPERATOR,
1127             S_IRUSR | S_IWUSR, "%s", driver_name);
1128 }
1129
1130 #if defined(KLD_MODULE)
1131
1132 typedef struct driverlink *driverlink_t;
1133 struct driverlink {
1134         kobj_class_t    driver;
1135         TAILQ_ENTRY(driverlink) link;   /* list of drivers in devclass */
1136 };
1137
1138 typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
1139
1140 struct devclass {
1141         TAILQ_ENTRY(devclass) link;
1142         devclass_t      parent;         /* parent in devclass hierarchy */
1143         driver_list_t   drivers;     /* bus devclasses store drivers for bus */
1144         char            *name;
1145         device_t        *devices;       /* array of devices indexed by unit */
1146         int             maxunit;        /* size of devices array */
1147 };
1148
1149 static void override_kernel_driver(void)
1150 {
1151         driverlink_t dl, dlfirst;
1152         driver_t *tmpdriver;
1153         devclass_t dc = devclass_find("pci");
1154         
1155         if (dc){
1156                 dlfirst = TAILQ_FIRST(&dc->drivers);
1157                 for (dl = dlfirst; dl; dl = TAILQ_NEXT(dl, link)) {
1158                         if(strcmp(dl->driver->name, driver_name) == 0) {
1159                                 tmpdriver=dl->driver;
1160                                 dl->driver=dlfirst->driver;
1161                                 dlfirst->driver=tmpdriver;
1162                                 break;
1163                         }
1164                 }
1165         }
1166 }
1167
1168 #else 
1169 #define override_kernel_driver()
1170 #endif
1171
1172 static void hpt_init(void *dummy)
1173 {
1174         if (bootverbose)
1175                 os_printk("%s %s", driver_name_long, driver_ver);
1176
1177         override_kernel_driver();
1178         init_config();
1179
1180         hpt_ich.ich_func = hpt_final_init;
1181         hpt_ich.ich_arg = NULL;
1182         if (config_intrhook_establish(&hpt_ich) != 0) {
1183                 printf("%s: cannot establish configuration hook\n",
1184                     driver_name_long);
1185         }
1186
1187 }
1188 SYSINIT(hptinit, SI_SUB_CONFIGURE, SI_ORDER_FIRST, hpt_init, NULL);
1189
1190 /*
1191  * CAM driver interface
1192  */
1193 static device_method_t driver_methods[] = {
1194         /* Device interface */
1195         DEVMETHOD(device_probe,         hpt_probe),
1196         DEVMETHOD(device_attach,        hpt_attach),
1197         DEVMETHOD(device_detach,        hpt_detach),
1198         DEVMETHOD(device_shutdown,      hpt_shutdown),
1199         { 0, 0 }
1200 };
1201
1202 static driver_t hpt_pci_driver = {
1203         driver_name,
1204         driver_methods,
1205         sizeof(HBA)
1206 };
1207
1208 static devclass_t       hpt_devclass;
1209
1210 #ifndef TARGETNAME
1211 #error "no TARGETNAME found"
1212 #endif
1213
1214 /* use this to make TARGETNAME be expanded */
1215 #define __DRIVER_MODULE(p1, p2, p3, p4, p5, p6) DRIVER_MODULE(p1, p2, p3, p4, p5, p6)
1216 #define __MODULE_VERSION(p1, p2) MODULE_VERSION(p1, p2)
1217 #define __MODULE_DEPEND(p1, p2, p3, p4, p5) MODULE_DEPEND(p1, p2, p3, p4, p5)
1218 __DRIVER_MODULE(TARGETNAME, pci, hpt_pci_driver, hpt_devclass, 0, 0);
1219 __MODULE_VERSION(TARGETNAME, 1);
1220 __MODULE_DEPEND(TARGETNAME, cam, 1, 1, 1);
1221
1222 static int hpt_open(struct cdev *dev, int flags, int devtype, struct thread *td)
1223 {
1224         return 0;
1225 }
1226
1227 static int hpt_close(struct cdev *dev, int flags, int devtype, struct thread *td)
1228 {
1229         return 0;
1230 }
1231
1232 static int hpt_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
1233 {
1234         PHPT_IOCTL_PARAM piop=(PHPT_IOCTL_PARAM)data;
1235         IOCTL_ARG ioctl_args;
1236         HPT_U32 bytesReturned;
1237
1238         switch (cmd){
1239         case HPT_DO_IOCONTROL:
1240         {       
1241                 if (piop->Magic == HPT_IOCTL_MAGIC || piop->Magic == HPT_IOCTL_MAGIC32) {
1242                         KdPrint(("ioctl=%x in=%p len=%d out=%p len=%d\n",
1243                                 piop->dwIoControlCode,
1244                                 piop->lpInBuffer,
1245                                 piop->nInBufferSize,
1246                                 piop->lpOutBuffer,
1247                                 piop->nOutBufferSize));
1248                         
1249                 memset(&ioctl_args, 0, sizeof(ioctl_args));
1250                 
1251                 ioctl_args.dwIoControlCode = piop->dwIoControlCode;
1252                 ioctl_args.nInBufferSize = piop->nInBufferSize;
1253                 ioctl_args.nOutBufferSize = piop->nOutBufferSize;
1254                 ioctl_args.lpBytesReturned = &bytesReturned;
1255
1256                 if (ioctl_args.nInBufferSize) {
1257                         ioctl_args.lpInBuffer = malloc(ioctl_args.nInBufferSize, M_DEVBUF, M_WAITOK);
1258                         if (!ioctl_args.lpInBuffer)
1259                                 goto invalid;
1260                         if (copyin((void*)piop->lpInBuffer,
1261                                         ioctl_args.lpInBuffer, piop->nInBufferSize))
1262                                 goto invalid;
1263                 }
1264         
1265                 if (ioctl_args.nOutBufferSize) {
1266                         ioctl_args.lpOutBuffer = malloc(ioctl_args.nOutBufferSize, M_DEVBUF, M_WAITOK);
1267                         if (!ioctl_args.lpOutBuffer)
1268                                 goto invalid;
1269                 }
1270                 
1271                 hpt_do_ioctl(&ioctl_args);
1272         
1273                 if (ioctl_args.result==HPT_IOCTL_RESULT_OK) {
1274                         if (piop->nOutBufferSize) {
1275                                 if (copyout(ioctl_args.lpOutBuffer,
1276                                         (void*)piop->lpOutBuffer, piop->nOutBufferSize))
1277                                         goto invalid;
1278                         }
1279                         if (piop->lpBytesReturned) {
1280                                 if (copyout(&bytesReturned,
1281                                         (void*)piop->lpBytesReturned, sizeof(HPT_U32)))
1282                                         goto invalid;
1283                         }
1284                         if (ioctl_args.lpInBuffer) free(ioctl_args.lpInBuffer, M_DEVBUF);
1285                         if (ioctl_args.lpOutBuffer) free(ioctl_args.lpOutBuffer, M_DEVBUF);
1286                         return 0;
1287                 }
1288 invalid:
1289                 if (ioctl_args.lpInBuffer) free(ioctl_args.lpInBuffer, M_DEVBUF);
1290                 if (ioctl_args.lpOutBuffer) free(ioctl_args.lpOutBuffer, M_DEVBUF);
1291                 return EFAULT;
1292         }
1293         return EFAULT;
1294         }
1295
1296         case HPT_SCAN_BUS:
1297         {
1298                 return hpt_rescan_bus();
1299         }
1300         default:
1301                 KdPrint(("invalid command!"));
1302                 return EFAULT;
1303         }       
1304
1305 }
1306
1307 static int      hpt_rescan_bus(void)
1308 {
1309         union ccb                       *ccb;
1310         PVBUS                           vbus;
1311         PVBUS_EXT                       vbus_ext;       
1312                 
1313         ldm_for_each_vbus(vbus, vbus_ext) {
1314                 if ((ccb = xpt_alloc_ccb()) == NULL)
1315                 {
1316                         return(ENOMEM);
1317                 }
1318                 if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(vbus_ext->sim),
1319                         CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP)  
1320                 {
1321                         xpt_free_ccb(ccb);
1322                         return(EIO);
1323                 }
1324                 xpt_rescan(ccb);
1325         }
1326         return(0);
1327 }
1328