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