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