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