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