hpt27xx_osm_bsd.c revision 246713
1228940Sdelphij/*-
2228940Sdelphij * Copyright (c) 2011 HighPoint Technologies, Inc.
3228940Sdelphij * All rights reserved.
4228940Sdelphij *
5228940Sdelphij * Redistribution and use in source and binary forms, with or without
6228940Sdelphij * modification, are permitted provided that the following conditions
7228940Sdelphij * are met:
8228940Sdelphij * 1. Redistributions of source code must retain the above copyright
9228940Sdelphij *    notice, this list of conditions and the following disclaimer.
10228940Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
11228940Sdelphij *    notice, this list of conditions and the following disclaimer in the
12228940Sdelphij *    documentation and/or other materials provided with the distribution.
13228940Sdelphij *
14228940Sdelphij * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15228940Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16228940Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17228940Sdelphij * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18228940Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19228940Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20228940Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21228940Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22228940Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23228940Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24228940Sdelphij * SUCH DAMAGE.
25228940Sdelphij *
26228940Sdelphij * $FreeBSD: head/sys/dev/hpt27xx/osm_bsd.c 246713 2013-02-12 16:57:20Z kib $
27228940Sdelphij */
28228940Sdelphij
29228940Sdelphij#include <dev/hpt27xx/hpt27xx_config.h>
30228940Sdelphij
31228940Sdelphij#include <dev/hpt27xx/os_bsd.h>
32228940Sdelphij#include <dev/hpt27xx/hptintf.h>
33228940Sdelphij
34228940Sdelphijstatic int hpt_probe(device_t dev)
35228940Sdelphij{
36228940Sdelphij	PCI_ID pci_id;
37228940Sdelphij	HIM *him;
38228940Sdelphij	int i;
39228940Sdelphij	PHBA hba;
40228940Sdelphij
41228940Sdelphij	for (him = him_list; him; him = him->next) {
42228940Sdelphij		for (i=0; him->get_supported_device_id(i, &pci_id); i++) {
43228940Sdelphij			if (him->get_controller_count)
44228940Sdelphij				him->get_controller_count(&pci_id,0,0);
45228940Sdelphij			if ((pci_get_vendor(dev) == pci_id.vid) &&
46228940Sdelphij				(pci_get_device(dev) == pci_id.did)){
47228940Sdelphij				KdPrint(("hpt_probe: adapter at PCI %d:%d:%d, IRQ %d",
48228940Sdelphij					pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev), pci_get_irq(dev)
49228940Sdelphij				));
50228940Sdelphij				device_set_desc(dev, him->name);
51228940Sdelphij				hba = (PHBA)device_get_softc(dev);
52228940Sdelphij				memset(hba, 0, sizeof(HBA));
53228940Sdelphij				hba->ext_type = EXT_TYPE_HBA;
54228940Sdelphij				hba->ldm_adapter.him = him;
55228940Sdelphij				return 0;
56228940Sdelphij			}
57228940Sdelphij		}
58228940Sdelphij	}
59228940Sdelphij
60228940Sdelphij	return (ENXIO);
61228940Sdelphij}
62228940Sdelphij
63228940Sdelphijstatic int hpt_attach(device_t dev)
64228940Sdelphij{
65228940Sdelphij	PHBA hba = (PHBA)device_get_softc(dev);
66228940Sdelphij	HIM *him = hba->ldm_adapter.him;
67228940Sdelphij	PCI_ID pci_id;
68228940Sdelphij	HPT_UINT size;
69228940Sdelphij	PVBUS vbus;
70228940Sdelphij	PVBUS_EXT vbus_ext;
71228940Sdelphij
72228940Sdelphij	KdPrint(("hpt_attach(%d/%d/%d)", pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev)));
73228940Sdelphij
74228940Sdelphij#if __FreeBSD_version >=440000
75228940Sdelphij	pci_enable_busmaster(dev);
76228940Sdelphij#endif
77228940Sdelphij
78228940Sdelphij	pci_id.vid = pci_get_vendor(dev);
79228940Sdelphij	pci_id.did = pci_get_device(dev);
80228940Sdelphij	pci_id.rev = pci_get_revid(dev);
81228940Sdelphij	pci_id.subsys = (HPT_U32)(pci_get_subdevice(dev)) << 16 | pci_get_subvendor(dev);
82228940Sdelphij
83228940Sdelphij	size = him->get_adapter_size(&pci_id);
84228940Sdelphij	hba->ldm_adapter.him_handle = malloc(size, M_DEVBUF, M_WAITOK);
85228940Sdelphij	if (!hba->ldm_adapter.him_handle)
86228940Sdelphij		return ENXIO;
87228940Sdelphij
88228940Sdelphij	hba->pcidev = dev;
89228940Sdelphij	hba->pciaddr.tree = 0;
90228940Sdelphij	hba->pciaddr.bus = pci_get_bus(dev);
91228940Sdelphij	hba->pciaddr.device = pci_get_slot(dev);
92228940Sdelphij	hba->pciaddr.function = pci_get_function(dev);
93228940Sdelphij
94228940Sdelphij	if (!him->create_adapter(&pci_id, hba->pciaddr, hba->ldm_adapter.him_handle, hba)) {
95228940Sdelphij		free(hba->ldm_adapter.him_handle, M_DEVBUF);
96228940Sdelphij		return -1;
97228940Sdelphij	}
98228940Sdelphij
99228940Sdelphij	os_printk("adapter at PCI %d:%d:%d, IRQ %d",
100228940Sdelphij		hba->pciaddr.bus, hba->pciaddr.device, hba->pciaddr.function, pci_get_irq(dev));
101228940Sdelphij
102228940Sdelphij	if (!ldm_register_adapter(&hba->ldm_adapter)) {
103228940Sdelphij		size = ldm_get_vbus_size();
104228940Sdelphij		vbus_ext = malloc(sizeof(VBUS_EXT) + size, M_DEVBUF, M_WAITOK);
105228940Sdelphij		if (!vbus_ext) {
106228940Sdelphij			free(hba->ldm_adapter.him_handle, M_DEVBUF);
107228940Sdelphij			return -1;
108228940Sdelphij		}
109228940Sdelphij		memset(vbus_ext, 0, sizeof(VBUS_EXT));
110228940Sdelphij		vbus_ext->ext_type = EXT_TYPE_VBUS;
111228940Sdelphij		ldm_create_vbus((PVBUS)vbus_ext->vbus, vbus_ext);
112228940Sdelphij		ldm_register_adapter(&hba->ldm_adapter);
113228940Sdelphij	}
114228940Sdelphij
115228940Sdelphij	ldm_for_each_vbus(vbus, vbus_ext) {
116228940Sdelphij		if (hba->ldm_adapter.vbus==vbus) {
117228940Sdelphij			hba->vbus_ext = vbus_ext;
118228940Sdelphij			hba->next = vbus_ext->hba_list;
119228940Sdelphij			vbus_ext->hba_list = hba;
120228940Sdelphij			break;
121228940Sdelphij		}
122228940Sdelphij	}
123228940Sdelphij	return 0;
124228940Sdelphij}
125228940Sdelphij
126228940Sdelphij/*
127228940Sdelphij * Maybe we'd better to use the bus_dmamem_alloc to alloc DMA memory,
128228940Sdelphij * but there are some problems currently (alignment, etc).
129228940Sdelphij */
130228940Sdelphijstatic __inline void *__get_free_pages(int order)
131228940Sdelphij{
132228940Sdelphij	/* don't use low memory - other devices may get starved */
133228940Sdelphij	return contigmalloc(PAGE_SIZE<<order,
134228940Sdelphij			M_DEVBUF, M_WAITOK, BUS_SPACE_MAXADDR_24BIT, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
135228940Sdelphij}
136228940Sdelphij
137228940Sdelphijstatic __inline void free_pages(void *p, int order)
138228940Sdelphij{
139228940Sdelphij	contigfree(p, PAGE_SIZE<<order, M_DEVBUF);
140228940Sdelphij}
141228940Sdelphij
142228940Sdelphijstatic int hpt_alloc_mem(PVBUS_EXT vbus_ext)
143228940Sdelphij{
144228940Sdelphij	PHBA hba;
145228940Sdelphij	struct freelist *f;
146228940Sdelphij	HPT_UINT i;
147228940Sdelphij	void **p;
148228940Sdelphij
149228940Sdelphij	for (hba = vbus_ext->hba_list; hba; hba = hba->next)
150228940Sdelphij		hba->ldm_adapter.him->get_meminfo(hba->ldm_adapter.him_handle);
151228940Sdelphij
152228940Sdelphij	ldm_get_mem_info((PVBUS)vbus_ext->vbus, 0);
153228940Sdelphij
154228940Sdelphij	for (f=vbus_ext->freelist_head; f; f=f->next) {
155228940Sdelphij		KdPrint(("%s: %d*%d=%d bytes",
156228940Sdelphij			f->tag, f->count, f->size, f->count*f->size));
157228940Sdelphij		for (i=0; i<f->count; i++) {
158228940Sdelphij			p = (void **)malloc(f->size, M_DEVBUF, M_WAITOK);
159228940Sdelphij			if (!p)	return (ENXIO);
160228940Sdelphij			*p = f->head;
161228940Sdelphij			f->head = p;
162228940Sdelphij		}
163228940Sdelphij	}
164228940Sdelphij
165228940Sdelphij	for (f=vbus_ext->freelist_dma_head; f; f=f->next) {
166228940Sdelphij		int order, size, j;
167228940Sdelphij
168228940Sdelphij		HPT_ASSERT((f->size & (f->alignment-1))==0);
169228940Sdelphij
170245768Sdelphij		for (order=0, size=PAGE_SIZE; size<f->size; order++, size<<=1)
171245768Sdelphij			;
172228940Sdelphij
173228940Sdelphij		KdPrint(("%s: %d*%d=%d bytes, order %d",
174228940Sdelphij			f->tag, f->count, f->size, f->count*f->size, order));
175228940Sdelphij		HPT_ASSERT(f->alignment<=PAGE_SIZE);
176228940Sdelphij
177228940Sdelphij		for (i=0; i<f->count;) {
178228940Sdelphij			p = (void **)__get_free_pages(order);
179228940Sdelphij			if (!p) return -1;
180228940Sdelphij			for (j = size/f->size; j && i<f->count; i++,j--) {
181228940Sdelphij				*p = f->head;
182228940Sdelphij				*(BUS_ADDRESS *)(p+1) = (BUS_ADDRESS)vtophys(p);
183228940Sdelphij				f->head = p;
184228940Sdelphij				p = (void **)((unsigned long)p + f->size);
185228940Sdelphij			}
186228940Sdelphij		}
187228940Sdelphij	}
188228940Sdelphij
189228940Sdelphij	HPT_ASSERT(PAGE_SIZE==DMAPOOL_PAGE_SIZE);
190228940Sdelphij
191228940Sdelphij	for (i=0; i<os_max_cache_pages; i++) {
192228940Sdelphij		p = (void **)__get_free_pages(0);
193228940Sdelphij		if (!p) return -1;
194228940Sdelphij		HPT_ASSERT(((HPT_UPTR)p & (DMAPOOL_PAGE_SIZE-1))==0);
195228940Sdelphij		dmapool_put_page((PVBUS)vbus_ext->vbus, p, (BUS_ADDRESS)vtophys(p));
196228940Sdelphij	}
197228940Sdelphij
198228940Sdelphij	return 0;
199228940Sdelphij}
200228940Sdelphij
201228940Sdelphijstatic void hpt_free_mem(PVBUS_EXT vbus_ext)
202228940Sdelphij{
203228940Sdelphij	struct freelist *f;
204228940Sdelphij	void *p;
205228940Sdelphij	int i;
206228940Sdelphij	BUS_ADDRESS bus;
207228940Sdelphij
208228940Sdelphij	for (f=vbus_ext->freelist_head; f; f=f->next) {
209228940Sdelphij#if DBG
210228940Sdelphij		if (f->count!=f->reserved_count) {
211228940Sdelphij			KdPrint(("memory leak for freelist %s (%d/%d)", f->tag, f->count, f->reserved_count));
212228940Sdelphij		}
213228940Sdelphij#endif
214228940Sdelphij		while ((p=freelist_get(f)))
215228940Sdelphij			free(p, M_DEVBUF);
216228940Sdelphij	}
217228940Sdelphij
218228940Sdelphij	for (i=0; i<os_max_cache_pages; i++) {
219228940Sdelphij		p = dmapool_get_page((PVBUS)vbus_ext->vbus, &bus);
220228940Sdelphij		HPT_ASSERT(p);
221228940Sdelphij		free_pages(p, 0);
222228940Sdelphij	}
223228940Sdelphij
224228940Sdelphij	for (f=vbus_ext->freelist_dma_head; f; f=f->next) {
225228940Sdelphij		int order, size;
226228940Sdelphij#if DBG
227228940Sdelphij		if (f->count!=f->reserved_count) {
228228940Sdelphij			KdPrint(("memory leak for dma freelist %s (%d/%d)", f->tag, f->count, f->reserved_count));
229228940Sdelphij		}
230228940Sdelphij#endif
231228940Sdelphij		for (order=0, size=PAGE_SIZE; size<f->size; order++, size<<=1) ;
232228940Sdelphij
233228940Sdelphij		while ((p=freelist_get_dma(f, &bus))) {
234228940Sdelphij			if (order)
235228940Sdelphij				free_pages(p, order);
236228940Sdelphij			else {
237228940Sdelphij			/* can't free immediately since other blocks in this page may still be in the list */
238228940Sdelphij				if (((HPT_UPTR)p & (PAGE_SIZE-1))==0)
239228940Sdelphij					dmapool_put_page((PVBUS)vbus_ext->vbus, p, bus);
240228940Sdelphij			}
241228940Sdelphij		}
242228940Sdelphij	}
243228940Sdelphij
244228940Sdelphij	while ((p = dmapool_get_page((PVBUS)vbus_ext->vbus, &bus)))
245228940Sdelphij		free_pages(p, 0);
246228940Sdelphij}
247228940Sdelphij
248228940Sdelphijstatic int hpt_init_vbus(PVBUS_EXT vbus_ext)
249228940Sdelphij{
250228940Sdelphij	PHBA hba;
251228940Sdelphij
252228940Sdelphij	for (hba = vbus_ext->hba_list; hba; hba = hba->next)
253228940Sdelphij		if (!hba->ldm_adapter.him->initialize(hba->ldm_adapter.him_handle)) {
254228940Sdelphij			KdPrint(("fail to initialize %p", hba));
255228940Sdelphij			return -1;
256228940Sdelphij		}
257228940Sdelphij
258228940Sdelphij	ldm_initialize_vbus((PVBUS)vbus_ext->vbus, &vbus_ext->hba_list->ldm_adapter);
259228940Sdelphij	return 0;
260228940Sdelphij}
261228940Sdelphij
262228940Sdelphijstatic void hpt_flush_done(PCOMMAND pCmd)
263228940Sdelphij{
264228940Sdelphij	PVDEV vd = pCmd->target;
265228940Sdelphij
266228940Sdelphij	if (mIsArray(vd->type) && vd->u.array.transform && vd!=vd->u.array.transform->target) {
267228940Sdelphij		vd = vd->u.array.transform->target;
268228940Sdelphij		HPT_ASSERT(vd);
269228940Sdelphij		pCmd->target = vd;
270228940Sdelphij		pCmd->Result = RETURN_PENDING;
271228940Sdelphij		vdev_queue_cmd(pCmd);
272228940Sdelphij		return;
273228940Sdelphij	}
274228940Sdelphij
275228940Sdelphij	*(int *)pCmd->priv = 1;
276228940Sdelphij	wakeup(pCmd);
277228940Sdelphij}
278228940Sdelphij
279228940Sdelphij/*
280228940Sdelphij * flush a vdev (without retry).
281228940Sdelphij */
282228940Sdelphijstatic int hpt_flush_vdev(PVBUS_EXT vbus_ext, PVDEV vd)
283228940Sdelphij{
284228940Sdelphij	PCOMMAND pCmd;
285228940Sdelphij	int result = 0, done;
286228940Sdelphij	HPT_UINT count;
287228940Sdelphij
288228940Sdelphij	KdPrint(("flusing dev %p", vd));
289228940Sdelphij
290228940Sdelphij	hpt_lock_vbus(vbus_ext);
291228940Sdelphij
292228940Sdelphij	if (mIsArray(vd->type) && vd->u.array.transform)
293228940Sdelphij		count = MAX(vd->u.array.transform->source->cmds_per_request,
294228940Sdelphij					vd->u.array.transform->target->cmds_per_request);
295228940Sdelphij	else
296228940Sdelphij		count = vd->cmds_per_request;
297228940Sdelphij
298228940Sdelphij	pCmd = ldm_alloc_cmds(vd->vbus, count);
299228940Sdelphij
300228940Sdelphij	if (!pCmd) {
301228940Sdelphij		hpt_unlock_vbus(vbus_ext);
302228940Sdelphij		return -1;
303228940Sdelphij	}
304228940Sdelphij
305228940Sdelphij	pCmd->type = CMD_TYPE_FLUSH;
306228940Sdelphij	pCmd->flags.hard_flush = 1;
307228940Sdelphij	pCmd->target = vd;
308228940Sdelphij	pCmd->done = hpt_flush_done;
309228940Sdelphij	done = 0;
310228940Sdelphij	pCmd->priv = &done;
311228940Sdelphij
312228940Sdelphij	ldm_queue_cmd(pCmd);
313228940Sdelphij
314228940Sdelphij	if (!done) {
315228940Sdelphij		while (hpt_sleep(vbus_ext, pCmd, PPAUSE, "hptfls", HPT_OSM_TIMEOUT)) {
316228940Sdelphij			ldm_reset_vbus(vd->vbus);
317228940Sdelphij		}
318228940Sdelphij	}
319228940Sdelphij
320228940Sdelphij	KdPrint(("flush result %d", pCmd->Result));
321228940Sdelphij
322228940Sdelphij	if (pCmd->Result!=RETURN_SUCCESS)
323228940Sdelphij		result = -1;
324228940Sdelphij
325228940Sdelphij	ldm_free_cmds(pCmd);
326228940Sdelphij
327228940Sdelphij	hpt_unlock_vbus(vbus_ext);
328228940Sdelphij
329228940Sdelphij	return result;
330228940Sdelphij}
331228940Sdelphij
332228940Sdelphijstatic void hpt_stop_tasks(PVBUS_EXT vbus_ext);
333228940Sdelphijstatic void hpt_shutdown_vbus(PVBUS_EXT vbus_ext, int howto)
334228940Sdelphij{
335228940Sdelphij	PVBUS     vbus = (PVBUS)vbus_ext->vbus;
336228940Sdelphij	PHBA hba;
337228940Sdelphij	int i;
338228940Sdelphij
339228940Sdelphij	KdPrint(("hpt_shutdown_vbus"));
340228940Sdelphij
341228940Sdelphij	/* stop all ctl tasks and disable the worker taskqueue */
342228940Sdelphij	hpt_stop_tasks(vbus_ext);
343228940Sdelphij	vbus_ext->worker.ta_context = 0;
344228940Sdelphij
345228940Sdelphij	/* flush devices */
346228940Sdelphij	for (i=0; i<osm_max_targets; i++) {
347228940Sdelphij		PVDEV vd = ldm_find_target(vbus, i);
348228940Sdelphij		if (vd) {
349228940Sdelphij			/* retry once */
350228940Sdelphij			if (hpt_flush_vdev(vbus_ext, vd))
351228940Sdelphij				hpt_flush_vdev(vbus_ext, vd);
352228940Sdelphij		}
353228940Sdelphij	}
354228940Sdelphij
355228940Sdelphij	hpt_lock_vbus(vbus_ext);
356228940Sdelphij	ldm_shutdown(vbus);
357228940Sdelphij	hpt_unlock_vbus(vbus_ext);
358228940Sdelphij
359228940Sdelphij	ldm_release_vbus(vbus);
360228940Sdelphij
361228940Sdelphij	for (hba=vbus_ext->hba_list; hba; hba=hba->next)
362228940Sdelphij		bus_teardown_intr(hba->pcidev, hba->irq_res, hba->irq_handle);
363228940Sdelphij
364228940Sdelphij	hpt_free_mem(vbus_ext);
365228940Sdelphij
366228940Sdelphij	while ((hba=vbus_ext->hba_list)) {
367228940Sdelphij		vbus_ext->hba_list = hba->next;
368228940Sdelphij		free(hba->ldm_adapter.him_handle, M_DEVBUF);
369228940Sdelphij	}
370228940Sdelphij
371228940Sdelphij	free(vbus_ext, M_DEVBUF);
372228940Sdelphij	KdPrint(("hpt_shutdown_vbus done"));
373228940Sdelphij}
374228940Sdelphij
375228940Sdelphijstatic void __hpt_do_tasks(PVBUS_EXT vbus_ext)
376228940Sdelphij{
377228940Sdelphij	OSM_TASK *tasks;
378228940Sdelphij
379228940Sdelphij	tasks = vbus_ext->tasks;
380228940Sdelphij	vbus_ext->tasks = 0;
381228940Sdelphij
382228940Sdelphij	while (tasks) {
383228940Sdelphij		OSM_TASK *t = tasks;
384228940Sdelphij		tasks = t->next;
385228940Sdelphij		t->next = 0;
386228940Sdelphij		t->func(vbus_ext->vbus, t->data);
387228940Sdelphij	}
388228940Sdelphij}
389228940Sdelphij
390228940Sdelphijstatic void hpt_do_tasks(PVBUS_EXT vbus_ext, int pending)
391228940Sdelphij{
392228940Sdelphij	if(vbus_ext){
393228940Sdelphij		hpt_lock_vbus(vbus_ext);
394228940Sdelphij		__hpt_do_tasks(vbus_ext);
395228940Sdelphij		hpt_unlock_vbus(vbus_ext);
396228940Sdelphij	}
397228940Sdelphij}
398228940Sdelphij
399228940Sdelphijstatic void hpt_action(struct cam_sim *sim, union ccb *ccb);
400228940Sdelphijstatic void hpt_poll(struct cam_sim *sim);
401228940Sdelphijstatic void hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, void * arg);
402228940Sdelphijstatic void hpt_pci_intr(void *arg);
403228940Sdelphij
404228940Sdelphijstatic __inline POS_CMDEXT cmdext_get(PVBUS_EXT vbus_ext)
405228940Sdelphij{
406228940Sdelphij	POS_CMDEXT p = vbus_ext->cmdext_list;
407228940Sdelphij	if (p)
408228940Sdelphij		vbus_ext->cmdext_list = p->next;
409228940Sdelphij	return p;
410228940Sdelphij}
411228940Sdelphij
412228940Sdelphijstatic __inline void cmdext_put(POS_CMDEXT p)
413228940Sdelphij{
414228940Sdelphij	p->next = p->vbus_ext->cmdext_list;
415228940Sdelphij	p->vbus_ext->cmdext_list = p;
416228940Sdelphij}
417228940Sdelphij
418228940Sdelphijstatic void hpt_timeout(void *arg)
419228940Sdelphij{
420228940Sdelphij	PCOMMAND pCmd = (PCOMMAND)arg;
421228940Sdelphij	POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
422228940Sdelphij
423228940Sdelphij	KdPrint(("pCmd %p timeout", pCmd));
424228940Sdelphij
425228940Sdelphij	ldm_reset_vbus((PVBUS)ext->vbus_ext->vbus);
426228940Sdelphij}
427228940Sdelphij
428228940Sdelphijstatic void os_cmddone(PCOMMAND pCmd)
429228940Sdelphij{
430228940Sdelphij	POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
431228940Sdelphij	union ccb *ccb = ext->ccb;
432228940Sdelphij
433228940Sdelphij	KdPrint(("os_cmddone(%p, %d)", pCmd, pCmd->Result));
434228940Sdelphij
435228940Sdelphij	untimeout(hpt_timeout, pCmd, ccb->ccb_h.timeout_ch);
436228940Sdelphij
437228940Sdelphij	switch(pCmd->Result) {
438228940Sdelphij	case RETURN_SUCCESS:
439228940Sdelphij		ccb->ccb_h.status = CAM_REQ_CMP;
440228940Sdelphij		break;
441228940Sdelphij	case RETURN_BAD_DEVICE:
442228940Sdelphij		ccb->ccb_h.status = CAM_DEV_NOT_THERE;
443228940Sdelphij		break;
444228940Sdelphij	case RETURN_DEVICE_BUSY:
445228940Sdelphij		ccb->ccb_h.status = CAM_BUSY;
446228940Sdelphij		break;
447228940Sdelphij	case RETURN_INVALID_REQUEST:
448228940Sdelphij		ccb->ccb_h.status = CAM_REQ_INVALID;
449228940Sdelphij		break;
450228940Sdelphij	case RETURN_SELECTION_TIMEOUT:
451228940Sdelphij		ccb->ccb_h.status = CAM_SEL_TIMEOUT;
452228940Sdelphij		break;
453228940Sdelphij	case RETURN_RETRY:
454228940Sdelphij		ccb->ccb_h.status = CAM_BUSY;
455228940Sdelphij		break;
456228940Sdelphij	default:
457228940Sdelphij		ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
458228940Sdelphij		break;
459228940Sdelphij	}
460228940Sdelphij
461228940Sdelphij	if (pCmd->flags.data_in) {
462228940Sdelphij		bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_POSTREAD);
463228940Sdelphij	}
464228940Sdelphij	else if (pCmd->flags.data_out) {
465228940Sdelphij		bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_POSTWRITE);
466228940Sdelphij	}
467228940Sdelphij
468228940Sdelphij	bus_dmamap_unload(ext->vbus_ext->io_dmat, ext->dma_map);
469228940Sdelphij
470228940Sdelphij	cmdext_put(ext);
471228940Sdelphij	ldm_free_cmds(pCmd);
472228940Sdelphij	xpt_done(ccb);
473228940Sdelphij}
474228940Sdelphij
475228940Sdelphijstatic int os_buildsgl(PCOMMAND pCmd, PSG pSg, int logical)
476228940Sdelphij{
477228940Sdelphij	/* since we have provided physical sg, nobody will ask us to build physical sg */
478228940Sdelphij	HPT_ASSERT(0);
479228940Sdelphij	return FALSE;
480228940Sdelphij}
481228940Sdelphij
482228940Sdelphijstatic void hpt_io_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
483228940Sdelphij{
484228940Sdelphij	PCOMMAND pCmd = (PCOMMAND)arg;
485228940Sdelphij	POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
486228940Sdelphij	PSG psg = pCmd->psg;
487228940Sdelphij	int idx;
488228940Sdelphij
489228940Sdelphij	HPT_ASSERT(pCmd->flags.physical_sg);
490228940Sdelphij
491246713Skib	if (error)
492228940Sdelphij		panic("busdma error");
493228940Sdelphij
494228940Sdelphij	HPT_ASSERT(nsegs<=os_max_sg_descriptors);
495228940Sdelphij
496246713Skib	if (nsegs != 0) {
497246713Skib		for (idx = 0; idx < nsegs; idx++, psg++) {
498246713Skib			psg->addr.bus = segs[idx].ds_addr;
499246713Skib			psg->size = segs[idx].ds_len;
500246713Skib			psg->eot = 0;
501246713Skib		}
502246713Skib		psg[-1].eot = 1;
503228940Sdelphij
504246713Skib		if (pCmd->flags.data_in) {
505246713Skib			bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map,
506246713Skib			    BUS_DMASYNC_PREREAD);
507246713Skib		}
508246713Skib		else if (pCmd->flags.data_out) {
509246713Skib			bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map,
510246713Skib			    BUS_DMASYNC_PREWRITE);
511246713Skib		}
512228940Sdelphij	}
513228940Sdelphij
514228940Sdelphij	ext->ccb->ccb_h.timeout_ch = timeout(hpt_timeout, pCmd, HPT_OSM_TIMEOUT);
515228940Sdelphij	ldm_queue_cmd(pCmd);
516228940Sdelphij}
517228940Sdelphij
518228940Sdelphijstatic void hpt_scsi_io(PVBUS_EXT vbus_ext, union ccb *ccb)
519228940Sdelphij{
520228940Sdelphij	PVBUS vbus = (PVBUS)vbus_ext->vbus;
521228940Sdelphij	PVDEV vd;
522228940Sdelphij	PCOMMAND pCmd;
523228940Sdelphij	POS_CMDEXT ext;
524228940Sdelphij	HPT_U8 *cdb;
525228940Sdelphij
526228940Sdelphij	if (ccb->ccb_h.flags & CAM_CDB_POINTER)
527228940Sdelphij		cdb = ccb->csio.cdb_io.cdb_ptr;
528228940Sdelphij	else
529228940Sdelphij		cdb = ccb->csio.cdb_io.cdb_bytes;
530228940Sdelphij
531228940Sdelphij	KdPrint(("hpt_scsi_io: ccb %x id %d lun %d cdb %x-%x-%x",
532228940Sdelphij		ccb,
533228940Sdelphij		ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
534228940Sdelphij		*(HPT_U32 *)&cdb[0], *(HPT_U32 *)&cdb[4], *(HPT_U32 *)&cdb[8]
535228940Sdelphij	));
536228940Sdelphij
537228940Sdelphij	/* ccb->ccb_h.path_id is not our bus id - don't check it */
538228940Sdelphij	if (ccb->ccb_h.target_lun != 0 ||
539228940Sdelphij		ccb->ccb_h.target_id >= osm_max_targets ||
540228940Sdelphij		(ccb->ccb_h.flags & CAM_CDB_PHYS))
541228940Sdelphij	{
542228940Sdelphij		ccb->ccb_h.status = CAM_TID_INVALID;
543228940Sdelphij		xpt_done(ccb);
544228940Sdelphij		return;
545228940Sdelphij	}
546228940Sdelphij
547228940Sdelphij	vd = ldm_find_target(vbus, ccb->ccb_h.target_id);
548228940Sdelphij
549228940Sdelphij	if (!vd) {
550228940Sdelphij		ccb->ccb_h.status = CAM_TID_INVALID;
551228940Sdelphij		xpt_done(ccb);
552228940Sdelphij		return;
553228940Sdelphij	}
554228940Sdelphij
555228940Sdelphij	switch (cdb[0]) {
556228940Sdelphij	case TEST_UNIT_READY:
557228940Sdelphij	case START_STOP_UNIT:
558228940Sdelphij	case SYNCHRONIZE_CACHE:
559228940Sdelphij		ccb->ccb_h.status = CAM_REQ_CMP;
560228940Sdelphij		break;
561228940Sdelphij
562228940Sdelphij	case INQUIRY:
563228940Sdelphij		{
564228940Sdelphij			PINQUIRYDATA inquiryData;
565228940Sdelphij			memset(ccb->csio.data_ptr, 0, ccb->csio.dxfer_len);
566228940Sdelphij			inquiryData = (PINQUIRYDATA)ccb->csio.data_ptr;
567228940Sdelphij
568228940Sdelphij			inquiryData->AdditionalLength = 31;
569228940Sdelphij			inquiryData->CommandQueue = 1;
570228940Sdelphij			memcpy(&inquiryData->VendorId, "HPT     ", 8);
571228940Sdelphij			memcpy(&inquiryData->ProductId, "DISK 0_0        ", 16);
572228940Sdelphij
573228940Sdelphij			if (vd->target_id / 10) {
574228940Sdelphij				inquiryData->ProductId[7] = (vd->target_id % 100) / 10 + '0';
575228940Sdelphij				inquiryData->ProductId[8] = (vd->target_id % 100) % 10 + '0';
576228940Sdelphij			}
577228940Sdelphij			else
578228940Sdelphij				inquiryData->ProductId[7] = (vd->target_id % 100) % 10 + '0';
579228940Sdelphij
580228940Sdelphij			memcpy(&inquiryData->ProductRevisionLevel, "4.00", 4);
581228940Sdelphij
582228940Sdelphij			ccb->ccb_h.status = CAM_REQ_CMP;
583228940Sdelphij		}
584228940Sdelphij		break;
585228940Sdelphij
586228940Sdelphij	case READ_CAPACITY:
587228940Sdelphij	{
588228940Sdelphij		HPT_U8 *rbuf = ccb->csio.data_ptr;
589228940Sdelphij		HPT_U32 cap;
590228940Sdelphij
591228940Sdelphij		if (vd->capacity>0xfffffffful)
592228940Sdelphij			cap = 0xfffffffful;
593228940Sdelphij		else
594228940Sdelphij			cap = vd->capacity - 1;
595228940Sdelphij
596228940Sdelphij		rbuf[0] = (HPT_U8)(cap>>24);
597228940Sdelphij		rbuf[1] = (HPT_U8)(cap>>16);
598228940Sdelphij		rbuf[2] = (HPT_U8)(cap>>8);
599228940Sdelphij		rbuf[3] = (HPT_U8)cap;
600228940Sdelphij		rbuf[4] = 0;
601228940Sdelphij		rbuf[5] = 0;
602228940Sdelphij		rbuf[6] = 2;
603228940Sdelphij		rbuf[7] = 0;
604228940Sdelphij
605228940Sdelphij		ccb->ccb_h.status = CAM_REQ_CMP;
606228940Sdelphij		break;
607228940Sdelphij	}
608228940Sdelphij
609228940Sdelphij	case SERVICE_ACTION_IN:
610228940Sdelphij	{
611228940Sdelphij		HPT_U8 *rbuf = ccb->csio.data_ptr;
612228940Sdelphij		HPT_U64	cap = vd->capacity - 1;
613228940Sdelphij
614228940Sdelphij		rbuf[0] = (HPT_U8)(cap>>56);
615228940Sdelphij		rbuf[1] = (HPT_U8)(cap>>48);
616228940Sdelphij		rbuf[2] = (HPT_U8)(cap>>40);
617228940Sdelphij		rbuf[3] = (HPT_U8)(cap>>32);
618228940Sdelphij		rbuf[4] = (HPT_U8)(cap>>24);
619228940Sdelphij		rbuf[5] = (HPT_U8)(cap>>16);
620228940Sdelphij		rbuf[6] = (HPT_U8)(cap>>8);
621228940Sdelphij		rbuf[7] = (HPT_U8)cap;
622228940Sdelphij		rbuf[8] = 0;
623228940Sdelphij		rbuf[9] = 0;
624228940Sdelphij		rbuf[10] = 2;
625228940Sdelphij		rbuf[11] = 0;
626228940Sdelphij
627228940Sdelphij		ccb->ccb_h.status = CAM_REQ_CMP;
628228940Sdelphij		break;
629228940Sdelphij	}
630228940Sdelphij
631228940Sdelphij	case READ_6:
632228940Sdelphij	case READ_10:
633228940Sdelphij	case READ_16:
634228940Sdelphij	case WRITE_6:
635228940Sdelphij	case WRITE_10:
636228940Sdelphij	case WRITE_16:
637228940Sdelphij	case 0x13:
638228940Sdelphij	case 0x2f:
639228940Sdelphij	case 0x8f: /* VERIFY_16 */
640228940Sdelphij	{
641246713Skib		int error;
642228940Sdelphij		pCmd = ldm_alloc_cmds(vbus, vd->cmds_per_request);
643228940Sdelphij		if(!pCmd){
644228940Sdelphij			KdPrint(("Failed to allocate command!"));
645228940Sdelphij			ccb->ccb_h.status = CAM_BUSY;
646228940Sdelphij			break;
647228940Sdelphij		}
648228940Sdelphij
649228940Sdelphij		switch (cdb[0])	{
650228940Sdelphij		case READ_6:
651228940Sdelphij		case WRITE_6:
652228940Sdelphij		case 0x13:
653228940Sdelphij			pCmd->uCmd.Ide.Lba =  ((HPT_U32)cdb[1] << 16) | ((HPT_U32)cdb[2] << 8) | (HPT_U32)cdb[3];
654228940Sdelphij			pCmd->uCmd.Ide.nSectors = (HPT_U16) cdb[4];
655228940Sdelphij			break;
656228940Sdelphij		case READ_16:
657228940Sdelphij		case WRITE_16:
658228940Sdelphij		case 0x8f: /* VERIFY_16 */
659228940Sdelphij		{
660228940Sdelphij			HPT_U64 block =
661228940Sdelphij				((HPT_U64)cdb[2]<<56) |
662228940Sdelphij				((HPT_U64)cdb[3]<<48) |
663228940Sdelphij				((HPT_U64)cdb[4]<<40) |
664228940Sdelphij				((HPT_U64)cdb[5]<<32) |
665228940Sdelphij				((HPT_U64)cdb[6]<<24) |
666228940Sdelphij				((HPT_U64)cdb[7]<<16) |
667228940Sdelphij				((HPT_U64)cdb[8]<<8) |
668228940Sdelphij				((HPT_U64)cdb[9]);
669228940Sdelphij			pCmd->uCmd.Ide.Lba = block;
670228940Sdelphij			pCmd->uCmd.Ide.nSectors = (HPT_U16)cdb[13] | ((HPT_U16)cdb[12]<<8);
671228940Sdelphij			break;
672228940Sdelphij		}
673228940Sdelphij
674228940Sdelphij		default:
675228940Sdelphij			pCmd->uCmd.Ide.Lba = (HPT_U32)cdb[5] | ((HPT_U32)cdb[4] << 8) | ((HPT_U32)cdb[3] << 16) | ((HPT_U32)cdb[2] << 24);
676228940Sdelphij			pCmd->uCmd.Ide.nSectors = (HPT_U16) cdb[8] | ((HPT_U16)cdb[7]<<8);
677228940Sdelphij			break;
678228940Sdelphij		}
679228940Sdelphij
680228940Sdelphij		switch (cdb[0]) {
681228940Sdelphij		case READ_6:
682228940Sdelphij		case READ_10:
683228940Sdelphij		case READ_16:
684228940Sdelphij			pCmd->flags.data_in = 1;
685228940Sdelphij			break;
686228940Sdelphij		case WRITE_6:
687228940Sdelphij		case WRITE_10:
688228940Sdelphij		case WRITE_16:
689228940Sdelphij			pCmd->flags.data_out = 1;
690228940Sdelphij			break;
691228940Sdelphij		}
692228940Sdelphij		pCmd->priv = ext = cmdext_get(vbus_ext);
693228940Sdelphij		HPT_ASSERT(ext);
694228940Sdelphij		ext->ccb = ccb;
695228940Sdelphij		pCmd->target = vd;
696228940Sdelphij		pCmd->done = os_cmddone;
697228940Sdelphij		pCmd->buildsgl = os_buildsgl;
698228940Sdelphij		pCmd->psg = ext->psg;
699246713Skib		pCmd->flags.physical_sg = 1;
700246713Skib		error = bus_dmamap_load_ccb(vbus_ext->io_dmat,
701246713Skib					ext->dma_map, ccb,
702246713Skib					hpt_io_dmamap_callback, pCmd,
703228940Sdelphij				    	BUS_DMA_WAITOK
704228940Sdelphij					);
705246713Skib		KdPrint(("bus_dmamap_load return %d", error));
706246713Skib		if (error && error!=EINPROGRESS) {
707246713Skib			os_printk("bus_dmamap_load error %d", error);
708246713Skib			cmdext_put(ext);
709246713Skib			ldm_free_cmds(pCmd);
710246713Skib			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
711246713Skib			xpt_done(ccb);
712228940Sdelphij		}
713228940Sdelphij		return;
714228940Sdelphij	}
715228940Sdelphij
716228940Sdelphij	default:
717228940Sdelphij		ccb->ccb_h.status = CAM_REQ_INVALID;
718228940Sdelphij		break;
719228940Sdelphij	}
720228940Sdelphij
721228940Sdelphij	xpt_done(ccb);
722228940Sdelphij	return;
723228940Sdelphij}
724228940Sdelphij
725228940Sdelphijstatic void hpt_action(struct cam_sim *sim, union ccb *ccb)
726228940Sdelphij{
727228940Sdelphij	PVBUS_EXT vbus_ext = (PVBUS_EXT)cam_sim_softc(sim);
728228940Sdelphij
729228940Sdelphij	KdPrint(("hpt_action(fn=%d, id=%d)", ccb->ccb_h.func_code, ccb->ccb_h.target_id));
730228940Sdelphij
731228940Sdelphij	switch (ccb->ccb_h.func_code) {
732228940Sdelphij
733228940Sdelphij	case XPT_SCSI_IO:
734228940Sdelphij		hpt_lock_vbus(vbus_ext);
735228940Sdelphij		hpt_scsi_io(vbus_ext, ccb);
736228940Sdelphij		hpt_unlock_vbus(vbus_ext);
737228940Sdelphij		return;
738228940Sdelphij
739228940Sdelphij	case XPT_RESET_BUS:
740228940Sdelphij		hpt_lock_vbus(vbus_ext);
741228940Sdelphij		ldm_reset_vbus((PVBUS)vbus_ext->vbus);
742228940Sdelphij		hpt_unlock_vbus(vbus_ext);
743228940Sdelphij		break;
744228940Sdelphij
745228940Sdelphij	case XPT_GET_TRAN_SETTINGS:
746228940Sdelphij	case XPT_SET_TRAN_SETTINGS:
747228940Sdelphij		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
748228940Sdelphij		break;
749228940Sdelphij
750228940Sdelphij	case XPT_CALC_GEOMETRY:
751228940Sdelphij		ccb->ccg.heads = 255;
752228940Sdelphij		ccb->ccg.secs_per_track = 63;
753228940Sdelphij		ccb->ccg.cylinders = ccb->ccg.volume_size / (ccb->ccg.heads * ccb->ccg.secs_per_track);
754228940Sdelphij		ccb->ccb_h.status = CAM_REQ_CMP;
755228940Sdelphij		break;
756228940Sdelphij
757228940Sdelphij	case XPT_PATH_INQ:
758228940Sdelphij	{
759228940Sdelphij		struct ccb_pathinq *cpi = &ccb->cpi;
760228940Sdelphij
761228940Sdelphij		cpi->version_num = 1;
762228940Sdelphij		cpi->hba_inquiry = PI_SDTR_ABLE;
763228940Sdelphij		cpi->target_sprt = 0;
764228940Sdelphij		cpi->hba_misc = PIM_NOBUSRESET;
765228940Sdelphij		cpi->hba_eng_cnt = 0;
766228940Sdelphij		cpi->max_target = osm_max_targets;
767228940Sdelphij		cpi->max_lun = 0;
768228940Sdelphij		cpi->unit_number = cam_sim_unit(sim);
769228940Sdelphij		cpi->bus_id = cam_sim_bus(sim);
770228940Sdelphij		cpi->initiator_id = osm_max_targets;
771228940Sdelphij		cpi->base_transfer_speed = 3300;
772228940Sdelphij
773228940Sdelphij		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
774228940Sdelphij		strncpy(cpi->hba_vid, "HPT   ", HBA_IDLEN);
775228940Sdelphij		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
776228940Sdelphij#if (__FreeBSD_version >= 800000)
777228940Sdelphij		cpi->transport = XPORT_SPI;
778228940Sdelphij		cpi->transport_version = 2;
779228940Sdelphij		cpi->protocol = PROTO_SCSI;
780228940Sdelphij		cpi->protocol_version = SCSI_REV_2;
781228940Sdelphij#endif
782228940Sdelphij		cpi->ccb_h.status = CAM_REQ_CMP;
783228940Sdelphij		break;
784228940Sdelphij	}
785228940Sdelphij
786228940Sdelphij	default:
787228940Sdelphij		ccb->ccb_h.status = CAM_REQ_INVALID;
788228940Sdelphij		break;
789228940Sdelphij	}
790228940Sdelphij
791228940Sdelphij	xpt_done(ccb);
792228940Sdelphij	return;
793228940Sdelphij}
794228940Sdelphij
795228940Sdelphijstatic void hpt_pci_intr(void *arg)
796228940Sdelphij{
797228940Sdelphij	PVBUS_EXT vbus_ext = (PVBUS_EXT)arg;
798228940Sdelphij	hpt_lock_vbus(vbus_ext);
799228940Sdelphij	ldm_intr((PVBUS)vbus_ext->vbus);
800228940Sdelphij	hpt_unlock_vbus(vbus_ext);
801228940Sdelphij}
802228940Sdelphij
803228940Sdelphijstatic void hpt_poll(struct cam_sim *sim)
804228940Sdelphij{
805228940Sdelphij	hpt_pci_intr(cam_sim_softc(sim));
806228940Sdelphij}
807228940Sdelphij
808228940Sdelphijstatic void hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, void * arg)
809228940Sdelphij{
810228940Sdelphij	KdPrint(("hpt_async"));
811228940Sdelphij}
812228940Sdelphij
813228940Sdelphijstatic int hpt_shutdown(device_t dev)
814228940Sdelphij{
815228940Sdelphij	KdPrint(("hpt_shutdown(dev=%p)", dev));
816228940Sdelphij	return 0;
817228940Sdelphij}
818228940Sdelphij
819228940Sdelphijstatic int hpt_detach(device_t dev)
820228940Sdelphij{
821228940Sdelphij	/* we don't allow the driver to be unloaded. */
822228940Sdelphij	return EBUSY;
823228940Sdelphij}
824228940Sdelphij
825228940Sdelphijstatic void hpt_ioctl_done(struct _IOCTL_ARG *arg)
826228940Sdelphij{
827228940Sdelphij	arg->ioctl_cmnd = 0;
828228940Sdelphij	wakeup(arg);
829228940Sdelphij}
830228940Sdelphij
831228940Sdelphijstatic void __hpt_do_ioctl(PVBUS_EXT vbus_ext, IOCTL_ARG *ioctl_args)
832228940Sdelphij{
833228940Sdelphij	ioctl_args->result = -1;
834228940Sdelphij	ioctl_args->done = hpt_ioctl_done;
835228940Sdelphij	ioctl_args->ioctl_cmnd = (void *)1;
836228940Sdelphij
837228940Sdelphij	hpt_lock_vbus(vbus_ext);
838228940Sdelphij	ldm_ioctl((PVBUS)vbus_ext->vbus, ioctl_args);
839228940Sdelphij
840228940Sdelphij	while (ioctl_args->ioctl_cmnd) {
841228940Sdelphij		if (hpt_sleep(vbus_ext, ioctl_args, PPAUSE, "hptctl", HPT_OSM_TIMEOUT)==0)
842228940Sdelphij			break;
843228940Sdelphij		ldm_reset_vbus((PVBUS)vbus_ext->vbus);
844228940Sdelphij		__hpt_do_tasks(vbus_ext);
845228940Sdelphij	}
846228940Sdelphij
847228940Sdelphij	/* KdPrint(("ioctl %x result %d", ioctl_args->dwIoControlCode, ioctl_args->result)); */
848228940Sdelphij
849228940Sdelphij	hpt_unlock_vbus(vbus_ext);
850228940Sdelphij}
851228940Sdelphij
852228940Sdelphijstatic void hpt_do_ioctl(IOCTL_ARG *ioctl_args)
853228940Sdelphij{
854228940Sdelphij	PVBUS vbus;
855228940Sdelphij	PVBUS_EXT vbus_ext;
856228940Sdelphij
857228940Sdelphij	ldm_for_each_vbus(vbus, vbus_ext) {
858228940Sdelphij		__hpt_do_ioctl(vbus_ext, ioctl_args);
859228940Sdelphij		if (ioctl_args->result!=HPT_IOCTL_RESULT_WRONG_VBUS)
860228940Sdelphij			return;
861228940Sdelphij	}
862228940Sdelphij}
863228940Sdelphij
864228940Sdelphij#define HPT_DO_IOCTL(code, inbuf, insize, outbuf, outsize) ({\
865228940Sdelphij	IOCTL_ARG arg;\
866228940Sdelphij	arg.dwIoControlCode = code;\
867228940Sdelphij	arg.lpInBuffer = inbuf;\
868228940Sdelphij	arg.lpOutBuffer = outbuf;\
869228940Sdelphij	arg.nInBufferSize = insize;\
870228940Sdelphij	arg.nOutBufferSize = outsize;\
871228940Sdelphij	arg.lpBytesReturned = 0;\
872228940Sdelphij	hpt_do_ioctl(&arg);\
873228940Sdelphij	arg.result;\
874228940Sdelphij})
875228940Sdelphij
876228940Sdelphij#define DEVICEID_VALID(id) ((id) && ((HPT_U32)(id)!=0xffffffff))
877228940Sdelphij
878228940Sdelphijstatic int hpt_get_logical_devices(DEVICEID * pIds, int nMaxCount)
879228940Sdelphij{
880228940Sdelphij	int i;
881228940Sdelphij	HPT_U32 count = nMaxCount-1;
882228940Sdelphij
883228940Sdelphij	if (HPT_DO_IOCTL(HPT_IOCTL_GET_LOGICAL_DEVICES,
884228940Sdelphij			&count, sizeof(HPT_U32), pIds, sizeof(DEVICEID)*nMaxCount))
885228940Sdelphij		return -1;
886228940Sdelphij
887228940Sdelphij	nMaxCount = (int)pIds[0];
888228940Sdelphij	for (i=0; i<nMaxCount; i++) pIds[i] = pIds[i+1];
889228940Sdelphij	return nMaxCount;
890228940Sdelphij}
891228940Sdelphij
892228940Sdelphijstatic int hpt_get_device_info_v3(DEVICEID id, PLOGICAL_DEVICE_INFO_V3 pInfo)
893228940Sdelphij{
894228940Sdelphij	return HPT_DO_IOCTL(HPT_IOCTL_GET_DEVICE_INFO_V3,
895228940Sdelphij				&id, sizeof(DEVICEID), pInfo, sizeof(LOGICAL_DEVICE_INFO_V3));
896228940Sdelphij}
897228940Sdelphij
898228940Sdelphij/* not belong to this file logically, but we want to use ioctl interface */
899228940Sdelphijstatic int __hpt_stop_tasks(PVBUS_EXT vbus_ext, DEVICEID id)
900228940Sdelphij{
901228940Sdelphij	LOGICAL_DEVICE_INFO_V3 devinfo;
902228940Sdelphij	int i, result;
903228940Sdelphij	DEVICEID param[2] = { id, 0 };
904228940Sdelphij
905228940Sdelphij	if (hpt_get_device_info_v3(id, &devinfo))
906228940Sdelphij		return -1;
907228940Sdelphij
908228940Sdelphij	if (devinfo.Type!=LDT_ARRAY)
909228940Sdelphij		return -1;
910228940Sdelphij
911228940Sdelphij	if (devinfo.u.array.Flags & ARRAY_FLAG_REBUILDING)
912228940Sdelphij		param[1] = AS_REBUILD_ABORT;
913228940Sdelphij	else if (devinfo.u.array.Flags & ARRAY_FLAG_VERIFYING)
914228940Sdelphij		param[1] = AS_VERIFY_ABORT;
915228940Sdelphij	else if (devinfo.u.array.Flags & ARRAY_FLAG_INITIALIZING)
916228940Sdelphij		param[1] = AS_INITIALIZE_ABORT;
917228940Sdelphij	else if (devinfo.u.array.Flags & ARRAY_FLAG_TRANSFORMING)
918228940Sdelphij		param[1] = AS_TRANSFORM_ABORT;
919228940Sdelphij	else
920228940Sdelphij		return -1;
921228940Sdelphij
922228940Sdelphij	KdPrint(("SET_ARRAY_STATE(%x, %d)", param[0], param[1]));
923228940Sdelphij	result = HPT_DO_IOCTL(HPT_IOCTL_SET_ARRAY_STATE,
924228940Sdelphij				param, sizeof(param), 0, 0);
925228940Sdelphij
926228940Sdelphij	for (i=0; i<devinfo.u.array.nDisk; i++)
927228940Sdelphij		if (DEVICEID_VALID(devinfo.u.array.Members[i]))
928228940Sdelphij			__hpt_stop_tasks(vbus_ext, devinfo.u.array.Members[i]);
929228940Sdelphij
930228940Sdelphij	return result;
931228940Sdelphij}
932228940Sdelphij
933228940Sdelphijstatic void hpt_stop_tasks(PVBUS_EXT vbus_ext)
934228940Sdelphij{
935228940Sdelphij	DEVICEID ids[32];
936228940Sdelphij	int i, count;
937228940Sdelphij
938228940Sdelphij	count = hpt_get_logical_devices((DEVICEID *)&ids, sizeof(ids)/sizeof(ids[0]));
939228940Sdelphij
940228940Sdelphij	for (i=0; i<count; i++)
941228940Sdelphij		__hpt_stop_tasks(vbus_ext, ids[i]);
942228940Sdelphij}
943228940Sdelphij
944228940Sdelphijstatic	d_open_t	hpt_open;
945228940Sdelphijstatic	d_close_t	hpt_close;
946228940Sdelphijstatic	d_ioctl_t	hpt_ioctl;
947228940Sdelphijstatic	void		hpt_bus_scan_cb(struct cam_periph *periph, union ccb *ccb);
948228940Sdelphijstatic  int 		hpt_rescan_bus(void);
949228940Sdelphij
950228940Sdelphijstatic struct cdevsw hpt_cdevsw = {
951228940Sdelphij	.d_open =	hpt_open,
952228940Sdelphij	.d_close =	hpt_close,
953228940Sdelphij	.d_ioctl =	hpt_ioctl,
954228940Sdelphij	.d_name =	driver_name,
955228940Sdelphij#if __FreeBSD_version>=503000
956228940Sdelphij	.d_version =	D_VERSION,
957228940Sdelphij#endif
958228940Sdelphij#if (__FreeBSD_version>=503000 && __FreeBSD_version<600034)
959228940Sdelphij	.d_flags =	D_NEEDGIANT,
960228940Sdelphij#endif
961228940Sdelphij#if __FreeBSD_version<600034
962228940Sdelphij#if __FreeBSD_version>501000
963228940Sdelphij	.d_maj = 	MAJOR_AUTO,
964228940Sdelphij#else
965228940Sdelphij	.d_maj = HPT_DEV_MAJOR,
966228940Sdelphij#endif
967228940Sdelphij#endif
968228940Sdelphij};
969228940Sdelphij
970228940Sdelphijstatic struct intr_config_hook hpt_ich;
971228940Sdelphij
972228940Sdelphij/*
973228940Sdelphij * hpt_final_init will be called after all hpt_attach.
974228940Sdelphij */
975228940Sdelphijstatic void hpt_final_init(void *dummy)
976228940Sdelphij{
977228940Sdelphij	int       i;
978228940Sdelphij	PVBUS_EXT vbus_ext;
979228940Sdelphij	PVBUS vbus;
980228940Sdelphij	PHBA hba;
981228940Sdelphij
982228940Sdelphij	/* Clear the config hook */
983228940Sdelphij	config_intrhook_disestablish(&hpt_ich);
984228940Sdelphij
985228940Sdelphij	/* allocate memory */
986228940Sdelphij	i = 0;
987228940Sdelphij	ldm_for_each_vbus(vbus, vbus_ext) {
988228940Sdelphij		if (hpt_alloc_mem(vbus_ext)) {
989228940Sdelphij			os_printk("out of memory");
990228940Sdelphij			return;
991228940Sdelphij		}
992228940Sdelphij		i++;
993228940Sdelphij	}
994228940Sdelphij
995228940Sdelphij	if (!i) {
996245768Sdelphij		if (bootverbose)
997228940Sdelphij			os_printk("no controller detected.");
998228940Sdelphij		return;
999228940Sdelphij	}
1000228940Sdelphij
1001228940Sdelphij	/* initializing hardware */
1002228940Sdelphij	ldm_for_each_vbus(vbus, vbus_ext) {
1003228940Sdelphij		/* make timer available here */
1004228940Sdelphij		callout_handle_init(&vbus_ext->timer);
1005228940Sdelphij		if (hpt_init_vbus(vbus_ext)) {
1006228940Sdelphij			os_printk("fail to initialize hardware");
1007228940Sdelphij			break; /* FIXME */
1008228940Sdelphij		}
1009228940Sdelphij	}
1010228940Sdelphij
1011228940Sdelphij	/* register CAM interface */
1012228940Sdelphij	ldm_for_each_vbus(vbus, vbus_ext) {
1013228940Sdelphij		struct cam_devq *devq;
1014228940Sdelphij		struct ccb_setasync	ccb;
1015228940Sdelphij
1016228940Sdelphij#if (__FreeBSD_version >= 500000)
1017228940Sdelphij		mtx_init(&vbus_ext->lock, "hptsleeplock", NULL, MTX_DEF);
1018228940Sdelphij#endif
1019228940Sdelphij		if (bus_dma_tag_create(NULL,/* parent */
1020228940Sdelphij				4,	/* alignment */
1021228940Sdelphij				BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
1022228940Sdelphij				BUS_SPACE_MAXADDR,	/* lowaddr */
1023228940Sdelphij				BUS_SPACE_MAXADDR, 	/* highaddr */
1024228940Sdelphij				NULL, NULL, 		/* filter, filterarg */
1025228940Sdelphij				PAGE_SIZE * (os_max_sg_descriptors-1),	/* maxsize */
1026228940Sdelphij				os_max_sg_descriptors,	/* nsegments */
1027228940Sdelphij				0x10000,	/* maxsegsize */
1028228940Sdelphij				BUS_DMA_WAITOK,		/* flags */
1029228940Sdelphij#if __FreeBSD_version>502000
1030228940Sdelphij				busdma_lock_mutex,	/* lockfunc */
1031228940Sdelphij				&vbus_ext->lock,		/* lockfuncarg */
1032228940Sdelphij#endif
1033228940Sdelphij				&vbus_ext->io_dmat	/* tag */))
1034228940Sdelphij		{
1035228940Sdelphij			return ;
1036228940Sdelphij		}
1037228940Sdelphij
1038228940Sdelphij		for (i=0; i<os_max_queue_comm; i++) {
1039228940Sdelphij			POS_CMDEXT ext = (POS_CMDEXT)malloc(sizeof(OS_CMDEXT), M_DEVBUF, M_WAITOK);
1040228940Sdelphij			if (!ext) {
1041228940Sdelphij				os_printk("Can't alloc cmdext(%d)", i);
1042228940Sdelphij				return ;
1043228940Sdelphij			}
1044228940Sdelphij			ext->vbus_ext = vbus_ext;
1045228940Sdelphij			ext->next = vbus_ext->cmdext_list;
1046228940Sdelphij			vbus_ext->cmdext_list = ext;
1047228940Sdelphij
1048228940Sdelphij			if (bus_dmamap_create(vbus_ext->io_dmat, 0, &ext->dma_map)) {
1049228940Sdelphij				os_printk("Can't create dma map(%d)", i);
1050228940Sdelphij				return ;
1051228940Sdelphij			}
1052228940Sdelphij		}
1053228940Sdelphij
1054228940Sdelphij		if ((devq = cam_simq_alloc(os_max_queue_comm)) == NULL) {
1055228940Sdelphij			os_printk("cam_simq_alloc failed");
1056228940Sdelphij			return ;
1057228940Sdelphij		}
1058228940Sdelphij
1059228940Sdelphij#if __FreeBSD_version > 700025
1060228940Sdelphij		vbus_ext->sim = cam_sim_alloc(hpt_action, hpt_poll, driver_name,
1061228940Sdelphij				vbus_ext, 0, &Giant, os_max_queue_comm, /*tagged*/8,  devq);
1062228940Sdelphij#else
1063228940Sdelphij		vbus_ext->sim = cam_sim_alloc(hpt_action, hpt_poll, driver_name,
1064228940Sdelphij				vbus_ext, 0, os_max_queue_comm, /*tagged*/8,  devq);
1065228940Sdelphij#endif
1066228940Sdelphij
1067228940Sdelphij		if (!vbus_ext->sim) {
1068228940Sdelphij			os_printk("cam_sim_alloc failed");
1069228940Sdelphij			cam_simq_free(devq);
1070228940Sdelphij			return ;
1071228940Sdelphij		}
1072228940Sdelphij
1073228940Sdelphij#if __FreeBSD_version > 700044
1074228940Sdelphij		if (xpt_bus_register(vbus_ext->sim, NULL, 0) != CAM_SUCCESS) {
1075228940Sdelphij#else
1076228940Sdelphij		if (xpt_bus_register(vbus_ext->sim, 0) != CAM_SUCCESS) {
1077228940Sdelphij#endif
1078228940Sdelphij			os_printk("xpt_bus_register failed");
1079228940Sdelphij			cam_sim_free(vbus_ext->sim, /*free devq*/ TRUE);
1080228940Sdelphij			vbus_ext->sim = NULL;
1081228940Sdelphij			return ;
1082228940Sdelphij		}
1083228940Sdelphij
1084228940Sdelphij		if (xpt_create_path(&vbus_ext->path, /*periph */ NULL,
1085228940Sdelphij				cam_sim_path(vbus_ext->sim), CAM_TARGET_WILDCARD,
1086228940Sdelphij				CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1087228940Sdelphij		{
1088228940Sdelphij			os_printk("xpt_create_path failed");
1089228940Sdelphij			xpt_bus_deregister(cam_sim_path(vbus_ext->sim));
1090228940Sdelphij			cam_sim_free(vbus_ext->sim, /*free_devq*/TRUE);
1091228940Sdelphij			vbus_ext->sim = NULL;
1092228940Sdelphij			return ;
1093228940Sdelphij		}
1094228940Sdelphij
1095228940Sdelphij		xpt_setup_ccb(&ccb.ccb_h, vbus_ext->path, /*priority*/5);
1096228940Sdelphij		ccb.ccb_h.func_code = XPT_SASYNC_CB;
1097228940Sdelphij		ccb.event_enable = AC_LOST_DEVICE;
1098228940Sdelphij		ccb.callback = hpt_async;
1099228940Sdelphij		ccb.callback_arg = vbus_ext;
1100228940Sdelphij		xpt_action((union ccb *)&ccb);
1101228940Sdelphij
1102228940Sdelphij		for (hba = vbus_ext->hba_list; hba; hba = hba->next) {
1103228940Sdelphij			int rid = 0;
1104228940Sdelphij			if ((hba->irq_res = bus_alloc_resource(hba->pcidev,
1105228940Sdelphij				SYS_RES_IRQ, &rid, 0, ~0ul, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL)
1106228940Sdelphij			{
1107228940Sdelphij				os_printk("can't allocate interrupt");
1108228940Sdelphij				return ;
1109228940Sdelphij			}
1110228940Sdelphij
1111228940Sdelphij			if (bus_setup_intr(hba->pcidev, hba->irq_res, INTR_TYPE_CAM,
1112228940Sdelphij#if __FreeBSD_version > 700025
1113228940Sdelphij				NULL, hpt_pci_intr, vbus_ext, &hba->irq_handle))
1114228940Sdelphij#else
1115228940Sdelphij				hpt_pci_intr, vbus_ext, &hba->irq_handle))
1116228940Sdelphij#endif
1117228940Sdelphij			{
1118228940Sdelphij				os_printk("can't set up interrupt");
1119228940Sdelphij				return ;
1120228940Sdelphij			}
1121228940Sdelphij			hba->ldm_adapter.him->intr_control(hba->ldm_adapter.him_handle, HPT_TRUE);
1122228940Sdelphij
1123228940Sdelphij		}
1124228940Sdelphij
1125228940Sdelphij		vbus_ext->shutdown_eh = EVENTHANDLER_REGISTER(shutdown_final,
1126228940Sdelphij									hpt_shutdown_vbus, vbus_ext, SHUTDOWN_PRI_DEFAULT);
1127228940Sdelphij		if (!vbus_ext->shutdown_eh)
1128228940Sdelphij			os_printk("Shutdown event registration failed");
1129228940Sdelphij	}
1130228940Sdelphij
1131228940Sdelphij	ldm_for_each_vbus(vbus, vbus_ext) {
1132228940Sdelphij		TASK_INIT(&vbus_ext->worker, 0, (task_fn_t *)hpt_do_tasks, vbus_ext);
1133228940Sdelphij		if (vbus_ext->tasks)
1134228940Sdelphij			TASK_ENQUEUE(&vbus_ext->worker);
1135228940Sdelphij	}
1136228940Sdelphij
1137228940Sdelphij	make_dev(&hpt_cdevsw, DRIVER_MINOR, UID_ROOT, GID_OPERATOR,
1138245768Sdelphij	    S_IRUSR | S_IWUSR, "%s", driver_name);
1139228940Sdelphij}
1140228940Sdelphij
1141228940Sdelphij#if defined(KLD_MODULE) && (__FreeBSD_version >= 503000)
1142228940Sdelphij
1143228940Sdelphijtypedef struct driverlink *driverlink_t;
1144228940Sdelphijstruct driverlink {
1145228940Sdelphij	kobj_class_t	driver;
1146228940Sdelphij	TAILQ_ENTRY(driverlink) link;	/* list of drivers in devclass */
1147228940Sdelphij};
1148228940Sdelphij
1149228940Sdelphijtypedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
1150228940Sdelphij
1151228940Sdelphijstruct devclass {
1152228940Sdelphij	TAILQ_ENTRY(devclass) link;
1153228940Sdelphij	devclass_t	parent;		/* parent in devclass hierarchy */
1154228940Sdelphij	driver_list_t	drivers;     /* bus devclasses store drivers for bus */
1155228940Sdelphij	char		*name;
1156228940Sdelphij	device_t	*devices;	/* array of devices indexed by unit */
1157228940Sdelphij	int		maxunit;	/* size of devices array */
1158228940Sdelphij};
1159228940Sdelphij
1160228940Sdelphijstatic void override_kernel_driver(void)
1161228940Sdelphij{
1162228940Sdelphij	driverlink_t dl, dlfirst;
1163228940Sdelphij	driver_t *tmpdriver;
1164228940Sdelphij	devclass_t dc = devclass_find("pci");
1165228940Sdelphij
1166228940Sdelphij	if (dc){
1167228940Sdelphij		dlfirst = TAILQ_FIRST(&dc->drivers);
1168228940Sdelphij		for (dl = dlfirst; dl; dl = TAILQ_NEXT(dl, link)) {
1169228940Sdelphij			if(strcmp(dl->driver->name, driver_name) == 0) {
1170228940Sdelphij				tmpdriver=dl->driver;
1171228940Sdelphij				dl->driver=dlfirst->driver;
1172228940Sdelphij				dlfirst->driver=tmpdriver;
1173228940Sdelphij				break;
1174228940Sdelphij			}
1175228940Sdelphij		}
1176228940Sdelphij	}
1177228940Sdelphij}
1178228940Sdelphij
1179228940Sdelphij#else
1180228940Sdelphij#define override_kernel_driver()
1181228940Sdelphij#endif
1182228940Sdelphij
1183228940Sdelphijstatic void hpt_init(void *dummy)
1184228940Sdelphij{
1185245768Sdelphij	if (bootverbose)
1186228940Sdelphij		os_printk("%s %s", driver_name_long, driver_ver);
1187228940Sdelphij
1188228940Sdelphij	override_kernel_driver();
1189228940Sdelphij	init_config();
1190228940Sdelphij
1191228940Sdelphij	hpt_ich.ich_func = hpt_final_init;
1192228940Sdelphij	hpt_ich.ich_arg = NULL;
1193228940Sdelphij	if (config_intrhook_establish(&hpt_ich) != 0) {
1194228940Sdelphij		printf("%s: cannot establish configuration hook\n",
1195228940Sdelphij		    driver_name_long);
1196228940Sdelphij	}
1197228940Sdelphij
1198228940Sdelphij}
1199228940SdelphijSYSINIT(hptinit, SI_SUB_CONFIGURE, SI_ORDER_FIRST, hpt_init, NULL);
1200228940Sdelphij
1201228940Sdelphij/*
1202228940Sdelphij * CAM driver interface
1203228940Sdelphij */
1204228940Sdelphijstatic device_method_t driver_methods[] = {
1205228940Sdelphij	/* Device interface */
1206228940Sdelphij	DEVMETHOD(device_probe,		hpt_probe),
1207228940Sdelphij	DEVMETHOD(device_attach,	hpt_attach),
1208228940Sdelphij	DEVMETHOD(device_detach,	hpt_detach),
1209228940Sdelphij	DEVMETHOD(device_shutdown,	hpt_shutdown),
1210228940Sdelphij	{ 0, 0 }
1211228940Sdelphij};
1212228940Sdelphij
1213228940Sdelphijstatic driver_t hpt_pci_driver = {
1214228940Sdelphij	driver_name,
1215228940Sdelphij	driver_methods,
1216228940Sdelphij	sizeof(HBA)
1217228940Sdelphij};
1218228940Sdelphij
1219228940Sdelphijstatic devclass_t	hpt_devclass;
1220228940Sdelphij
1221228940Sdelphij#ifndef TARGETNAME
1222228940Sdelphij#error "no TARGETNAME found"
1223228940Sdelphij#endif
1224228940Sdelphij
1225228940Sdelphij/* use this to make TARGETNAME be expanded */
1226228940Sdelphij#define __DRIVER_MODULE(p1, p2, p3, p4, p5, p6) DRIVER_MODULE(p1, p2, p3, p4, p5, p6)
1227228940Sdelphij#define __MODULE_VERSION(p1, p2) MODULE_VERSION(p1, p2)
1228228940Sdelphij#define __MODULE_DEPEND(p1, p2, p3, p4, p5) MODULE_DEPEND(p1, p2, p3, p4, p5)
1229228940Sdelphij__DRIVER_MODULE(TARGETNAME, pci, hpt_pci_driver, hpt_devclass, 0, 0);
1230228940Sdelphij__MODULE_VERSION(TARGETNAME, 1);
1231228940Sdelphij__MODULE_DEPEND(TARGETNAME, cam, 1, 1, 1);
1232228940Sdelphij
1233228940Sdelphij#if __FreeBSD_version>503000
1234228940Sdelphijtypedef struct cdev * ioctl_dev_t;
1235228940Sdelphij#else
1236228940Sdelphijtypedef dev_t ioctl_dev_t;
1237228940Sdelphij#endif
1238228940Sdelphij
1239228940Sdelphij#if __FreeBSD_version >= 500000
1240228940Sdelphijtypedef	struct thread *	ioctl_thread_t;
1241228940Sdelphij#else
1242228940Sdelphijtypedef struct proc *	ioctl_thread_t;
1243228940Sdelphij#endif
1244228940Sdelphij
1245228940Sdelphijstatic int hpt_open(ioctl_dev_t dev, int flags, int devtype, ioctl_thread_t td)
1246228940Sdelphij{
1247228940Sdelphij	return 0;
1248228940Sdelphij}
1249228940Sdelphij
1250228940Sdelphijstatic int hpt_close(ioctl_dev_t dev, int flags, int devtype, ioctl_thread_t td)
1251228940Sdelphij{
1252228940Sdelphij	return 0;
1253228940Sdelphij}
1254228940Sdelphij
1255228940Sdelphijstatic int hpt_ioctl(ioctl_dev_t dev, u_long cmd, caddr_t data, int fflag, ioctl_thread_t td)
1256228940Sdelphij{
1257228940Sdelphij	PHPT_IOCTL_PARAM piop=(PHPT_IOCTL_PARAM)data;
1258228940Sdelphij	IOCTL_ARG ioctl_args;
1259228940Sdelphij	HPT_U32 bytesReturned;
1260228940Sdelphij
1261228940Sdelphij	switch (cmd){
1262228940Sdelphij	case HPT_DO_IOCONTROL:
1263228940Sdelphij	{
1264228940Sdelphij		if (piop->Magic == HPT_IOCTL_MAGIC || piop->Magic == HPT_IOCTL_MAGIC32) {
1265228940Sdelphij			KdPrint(("ioctl=%x in=%p len=%d out=%p len=%d\n",
1266228940Sdelphij				piop->dwIoControlCode,
1267228940Sdelphij				piop->lpInBuffer,
1268228940Sdelphij				piop->nInBufferSize,
1269228940Sdelphij				piop->lpOutBuffer,
1270228940Sdelphij				piop->nOutBufferSize));
1271228940Sdelphij
1272228940Sdelphij		memset(&ioctl_args, 0, sizeof(ioctl_args));
1273228940Sdelphij
1274228940Sdelphij		ioctl_args.dwIoControlCode = piop->dwIoControlCode;
1275228940Sdelphij		ioctl_args.nInBufferSize = piop->nInBufferSize;
1276228940Sdelphij		ioctl_args.nOutBufferSize = piop->nOutBufferSize;
1277228940Sdelphij		ioctl_args.lpBytesReturned = &bytesReturned;
1278228940Sdelphij
1279228940Sdelphij		if (ioctl_args.nInBufferSize) {
1280228940Sdelphij			ioctl_args.lpInBuffer = malloc(ioctl_args.nInBufferSize, M_DEVBUF, M_WAITOK);
1281228940Sdelphij			if (!ioctl_args.lpInBuffer)
1282228940Sdelphij				goto invalid;
1283228940Sdelphij			if (copyin((void*)piop->lpInBuffer,
1284228940Sdelphij					ioctl_args.lpInBuffer, piop->nInBufferSize))
1285228940Sdelphij				goto invalid;
1286228940Sdelphij		}
1287228940Sdelphij
1288228940Sdelphij		if (ioctl_args.nOutBufferSize) {
1289228940Sdelphij			ioctl_args.lpOutBuffer = malloc(ioctl_args.nOutBufferSize, M_DEVBUF, M_WAITOK);
1290228940Sdelphij			if (!ioctl_args.lpOutBuffer)
1291228940Sdelphij				goto invalid;
1292228940Sdelphij		}
1293228940Sdelphij
1294228940Sdelphij#if (__FreeBSD_version >= 500000)
1295228940Sdelphij		mtx_lock(&Giant);
1296228940Sdelphij#endif
1297228940Sdelphij
1298228940Sdelphij		hpt_do_ioctl(&ioctl_args);
1299228940Sdelphij
1300228940Sdelphij#if (__FreeBSD_version >= 500000)
1301228940Sdelphij		mtx_unlock(&Giant);
1302228940Sdelphij#endif
1303228940Sdelphij
1304228940Sdelphij		if (ioctl_args.result==HPT_IOCTL_RESULT_OK) {
1305228940Sdelphij			if (piop->nOutBufferSize) {
1306228940Sdelphij				if (copyout(ioctl_args.lpOutBuffer,
1307228940Sdelphij					(void*)piop->lpOutBuffer, piop->nOutBufferSize))
1308228940Sdelphij					goto invalid;
1309228940Sdelphij			}
1310228940Sdelphij			if (piop->lpBytesReturned) {
1311228940Sdelphij				if (copyout(&bytesReturned,
1312228940Sdelphij					(void*)piop->lpBytesReturned, sizeof(HPT_U32)))
1313228940Sdelphij					goto invalid;
1314228940Sdelphij			}
1315228940Sdelphij			if (ioctl_args.lpInBuffer) free(ioctl_args.lpInBuffer, M_DEVBUF);
1316228940Sdelphij			if (ioctl_args.lpOutBuffer) free(ioctl_args.lpOutBuffer, M_DEVBUF);
1317228940Sdelphij			return 0;
1318228940Sdelphij		}
1319228940Sdelphijinvalid:
1320228940Sdelphij		if (ioctl_args.lpInBuffer) free(ioctl_args.lpInBuffer, M_DEVBUF);
1321228940Sdelphij		if (ioctl_args.lpOutBuffer) free(ioctl_args.lpOutBuffer, M_DEVBUF);
1322228940Sdelphij		return EFAULT;
1323228940Sdelphij	}
1324228940Sdelphij	return EFAULT;
1325228940Sdelphij	}
1326228940Sdelphij
1327228940Sdelphij	case HPT_SCAN_BUS:
1328228940Sdelphij	{
1329228940Sdelphij		return hpt_rescan_bus();
1330228940Sdelphij	}
1331228940Sdelphij	default:
1332228940Sdelphij		KdPrint(("invalid command!"));
1333228940Sdelphij		return EFAULT;
1334228940Sdelphij	}
1335228940Sdelphij
1336228940Sdelphij}
1337228940Sdelphij
1338228940Sdelphijstatic int	hpt_rescan_bus(void)
1339228940Sdelphij{
1340228940Sdelphij	struct cam_path		*path;
1341228940Sdelphij	union ccb			*ccb;
1342228940Sdelphij	PVBUS 				vbus;
1343228940Sdelphij	PVBUS_EXT			vbus_ext;
1344228940Sdelphij
1345228940Sdelphij#if (__FreeBSD_version >= 500000)
1346228940Sdelphij	mtx_lock(&Giant);
1347228940Sdelphij#endif
1348228940Sdelphij
1349228940Sdelphij	ldm_for_each_vbus(vbus, vbus_ext) {
1350228940Sdelphij		if (xpt_create_path(&path, xpt_periph, cam_sim_path(vbus_ext->sim),
1351228940Sdelphij			CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1352228940Sdelphij			return(EIO);
1353228940Sdelphij		if ((ccb = malloc(sizeof(union ccb), M_TEMP, M_WAITOK)) == NULL)
1354228940Sdelphij			return(ENOMEM);
1355228940Sdelphij		bzero(ccb, sizeof(union ccb));
1356228940Sdelphij		xpt_setup_ccb(&ccb->ccb_h, path, 5);
1357228940Sdelphij		ccb->ccb_h.func_code = XPT_SCAN_BUS;
1358228940Sdelphij		ccb->ccb_h.cbfcnp = hpt_bus_scan_cb;
1359228940Sdelphij		ccb->crcn.flags = CAM_FLAG_NONE;
1360228940Sdelphij		xpt_action(ccb);
1361228940Sdelphij	}
1362228940Sdelphij
1363228940Sdelphij#if (__FreeBSD_version >= 500000)
1364228940Sdelphij	mtx_unlock(&Giant);
1365228940Sdelphij#endif
1366228940Sdelphij
1367228940Sdelphij	return(0);
1368228940Sdelphij}
1369228940Sdelphij
1370228940Sdelphijstatic	void	hpt_bus_scan_cb(struct cam_periph *periph, union ccb *ccb)
1371228940Sdelphij{
1372228940Sdelphij	if (ccb->ccb_h.status != CAM_REQ_CMP)
1373228940Sdelphij		KdPrint(("cam_scan_callback: failure status = %x",ccb->ccb_h.status));
1374228940Sdelphij	else
1375228940Sdelphij		KdPrint(("Scan bus successfully!"));
1376228940Sdelphij
1377228940Sdelphij	xpt_free_path(ccb->ccb_h.path);
1378228940Sdelphij	free(ccb, M_TEMP);
1379228940Sdelphij	return;
1380228940Sdelphij}
1381