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