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$
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;
55255320Sdelphij				return (BUS_PROBE_DEFAULT);
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
435255871Sscottl	untimeout(hpt_timeout, pCmd, ext->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	}
513255871Sscottl	ext->timeout_ch = timeout(hpt_timeout, pCmd, HPT_OSM_TIMEOUT);
514228940Sdelphij	ldm_queue_cmd(pCmd);
515228940Sdelphij}
516228940Sdelphij
517228940Sdelphijstatic void hpt_scsi_io(PVBUS_EXT vbus_ext, union ccb *ccb)
518228940Sdelphij{
519228940Sdelphij	PVBUS vbus = (PVBUS)vbus_ext->vbus;
520228940Sdelphij	PVDEV vd;
521228940Sdelphij	PCOMMAND pCmd;
522228940Sdelphij	POS_CMDEXT ext;
523228940Sdelphij	HPT_U8 *cdb;
524228940Sdelphij
525228940Sdelphij	if (ccb->ccb_h.flags & CAM_CDB_POINTER)
526228940Sdelphij		cdb = ccb->csio.cdb_io.cdb_ptr;
527228940Sdelphij	else
528228940Sdelphij		cdb = ccb->csio.cdb_io.cdb_bytes;
529228940Sdelphij
530228940Sdelphij	KdPrint(("hpt_scsi_io: ccb %x id %d lun %d cdb %x-%x-%x",
531228940Sdelphij		ccb,
532228940Sdelphij		ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
533228940Sdelphij		*(HPT_U32 *)&cdb[0], *(HPT_U32 *)&cdb[4], *(HPT_U32 *)&cdb[8]
534228940Sdelphij	));
535228940Sdelphij
536228940Sdelphij	/* ccb->ccb_h.path_id is not our bus id - don't check it */
537228940Sdelphij	if (ccb->ccb_h.target_lun != 0 ||
538228940Sdelphij		ccb->ccb_h.target_id >= osm_max_targets ||
539228940Sdelphij		(ccb->ccb_h.flags & CAM_CDB_PHYS))
540228940Sdelphij	{
541228940Sdelphij		ccb->ccb_h.status = CAM_TID_INVALID;
542228940Sdelphij		xpt_done(ccb);
543228940Sdelphij		return;
544228940Sdelphij	}
545228940Sdelphij
546228940Sdelphij	vd = ldm_find_target(vbus, ccb->ccb_h.target_id);
547228940Sdelphij
548228940Sdelphij	if (!vd) {
549228940Sdelphij		ccb->ccb_h.status = CAM_TID_INVALID;
550228940Sdelphij		xpt_done(ccb);
551228940Sdelphij		return;
552228940Sdelphij	}
553228940Sdelphij
554228940Sdelphij	switch (cdb[0]) {
555228940Sdelphij	case TEST_UNIT_READY:
556228940Sdelphij	case START_STOP_UNIT:
557228940Sdelphij	case SYNCHRONIZE_CACHE:
558228940Sdelphij		ccb->ccb_h.status = CAM_REQ_CMP;
559228940Sdelphij		break;
560228940Sdelphij
561228940Sdelphij	case INQUIRY:
562228940Sdelphij		{
563228940Sdelphij			PINQUIRYDATA inquiryData;
564228940Sdelphij			memset(ccb->csio.data_ptr, 0, ccb->csio.dxfer_len);
565228940Sdelphij			inquiryData = (PINQUIRYDATA)ccb->csio.data_ptr;
566228940Sdelphij
567228940Sdelphij			inquiryData->AdditionalLength = 31;
568228940Sdelphij			inquiryData->CommandQueue = 1;
569228940Sdelphij			memcpy(&inquiryData->VendorId, "HPT     ", 8);
570228940Sdelphij			memcpy(&inquiryData->ProductId, "DISK 0_0        ", 16);
571228940Sdelphij
572228940Sdelphij			if (vd->target_id / 10) {
573228940Sdelphij				inquiryData->ProductId[7] = (vd->target_id % 100) / 10 + '0';
574228940Sdelphij				inquiryData->ProductId[8] = (vd->target_id % 100) % 10 + '0';
575228940Sdelphij			}
576228940Sdelphij			else
577228940Sdelphij				inquiryData->ProductId[7] = (vd->target_id % 100) % 10 + '0';
578228940Sdelphij
579228940Sdelphij			memcpy(&inquiryData->ProductRevisionLevel, "4.00", 4);
580228940Sdelphij
581228940Sdelphij			ccb->ccb_h.status = CAM_REQ_CMP;
582228940Sdelphij		}
583228940Sdelphij		break;
584228940Sdelphij
585228940Sdelphij	case READ_CAPACITY:
586228940Sdelphij	{
587228940Sdelphij		HPT_U8 *rbuf = ccb->csio.data_ptr;
588228940Sdelphij		HPT_U32 cap;
589228940Sdelphij
590228940Sdelphij		if (vd->capacity>0xfffffffful)
591228940Sdelphij			cap = 0xfffffffful;
592228940Sdelphij		else
593228940Sdelphij			cap = vd->capacity - 1;
594228940Sdelphij
595228940Sdelphij		rbuf[0] = (HPT_U8)(cap>>24);
596228940Sdelphij		rbuf[1] = (HPT_U8)(cap>>16);
597228940Sdelphij		rbuf[2] = (HPT_U8)(cap>>8);
598228940Sdelphij		rbuf[3] = (HPT_U8)cap;
599228940Sdelphij		rbuf[4] = 0;
600228940Sdelphij		rbuf[5] = 0;
601228940Sdelphij		rbuf[6] = 2;
602228940Sdelphij		rbuf[7] = 0;
603228940Sdelphij
604228940Sdelphij		ccb->ccb_h.status = CAM_REQ_CMP;
605228940Sdelphij		break;
606228940Sdelphij	}
607228940Sdelphij
608228940Sdelphij	case SERVICE_ACTION_IN:
609228940Sdelphij	{
610228940Sdelphij		HPT_U8 *rbuf = ccb->csio.data_ptr;
611228940Sdelphij		HPT_U64	cap = vd->capacity - 1;
612228940Sdelphij
613228940Sdelphij		rbuf[0] = (HPT_U8)(cap>>56);
614228940Sdelphij		rbuf[1] = (HPT_U8)(cap>>48);
615228940Sdelphij		rbuf[2] = (HPT_U8)(cap>>40);
616228940Sdelphij		rbuf[3] = (HPT_U8)(cap>>32);
617228940Sdelphij		rbuf[4] = (HPT_U8)(cap>>24);
618228940Sdelphij		rbuf[5] = (HPT_U8)(cap>>16);
619228940Sdelphij		rbuf[6] = (HPT_U8)(cap>>8);
620228940Sdelphij		rbuf[7] = (HPT_U8)cap;
621228940Sdelphij		rbuf[8] = 0;
622228940Sdelphij		rbuf[9] = 0;
623228940Sdelphij		rbuf[10] = 2;
624228940Sdelphij		rbuf[11] = 0;
625228940Sdelphij
626228940Sdelphij		ccb->ccb_h.status = CAM_REQ_CMP;
627228940Sdelphij		break;
628228940Sdelphij	}
629228940Sdelphij
630228940Sdelphij	case READ_6:
631228940Sdelphij	case READ_10:
632228940Sdelphij	case READ_16:
633228940Sdelphij	case WRITE_6:
634228940Sdelphij	case WRITE_10:
635228940Sdelphij	case WRITE_16:
636228940Sdelphij	case 0x13:
637228940Sdelphij	case 0x2f:
638228940Sdelphij	case 0x8f: /* VERIFY_16 */
639228940Sdelphij	{
640246713Skib		int error;
641228940Sdelphij		pCmd = ldm_alloc_cmds(vbus, vd->cmds_per_request);
642228940Sdelphij		if(!pCmd){
643228940Sdelphij			KdPrint(("Failed to allocate command!"));
644228940Sdelphij			ccb->ccb_h.status = CAM_BUSY;
645228940Sdelphij			break;
646228940Sdelphij		}
647228940Sdelphij
648228940Sdelphij		switch (cdb[0])	{
649228940Sdelphij		case READ_6:
650228940Sdelphij		case WRITE_6:
651228940Sdelphij		case 0x13:
652228940Sdelphij			pCmd->uCmd.Ide.Lba =  ((HPT_U32)cdb[1] << 16) | ((HPT_U32)cdb[2] << 8) | (HPT_U32)cdb[3];
653228940Sdelphij			pCmd->uCmd.Ide.nSectors = (HPT_U16) cdb[4];
654228940Sdelphij			break;
655228940Sdelphij		case READ_16:
656228940Sdelphij		case WRITE_16:
657228940Sdelphij		case 0x8f: /* VERIFY_16 */
658228940Sdelphij		{
659228940Sdelphij			HPT_U64 block =
660228940Sdelphij				((HPT_U64)cdb[2]<<56) |
661228940Sdelphij				((HPT_U64)cdb[3]<<48) |
662228940Sdelphij				((HPT_U64)cdb[4]<<40) |
663228940Sdelphij				((HPT_U64)cdb[5]<<32) |
664228940Sdelphij				((HPT_U64)cdb[6]<<24) |
665228940Sdelphij				((HPT_U64)cdb[7]<<16) |
666228940Sdelphij				((HPT_U64)cdb[8]<<8) |
667228940Sdelphij				((HPT_U64)cdb[9]);
668228940Sdelphij			pCmd->uCmd.Ide.Lba = block;
669228940Sdelphij			pCmd->uCmd.Ide.nSectors = (HPT_U16)cdb[13] | ((HPT_U16)cdb[12]<<8);
670228940Sdelphij			break;
671228940Sdelphij		}
672228940Sdelphij
673228940Sdelphij		default:
674228940Sdelphij			pCmd->uCmd.Ide.Lba = (HPT_U32)cdb[5] | ((HPT_U32)cdb[4] << 8) | ((HPT_U32)cdb[3] << 16) | ((HPT_U32)cdb[2] << 24);
675228940Sdelphij			pCmd->uCmd.Ide.nSectors = (HPT_U16) cdb[8] | ((HPT_U16)cdb[7]<<8);
676228940Sdelphij			break;
677228940Sdelphij		}
678228940Sdelphij
679228940Sdelphij		switch (cdb[0]) {
680228940Sdelphij		case READ_6:
681228940Sdelphij		case READ_10:
682228940Sdelphij		case READ_16:
683228940Sdelphij			pCmd->flags.data_in = 1;
684228940Sdelphij			break;
685228940Sdelphij		case WRITE_6:
686228940Sdelphij		case WRITE_10:
687228940Sdelphij		case WRITE_16:
688228940Sdelphij			pCmd->flags.data_out = 1;
689228940Sdelphij			break;
690228940Sdelphij		}
691228940Sdelphij		pCmd->priv = ext = cmdext_get(vbus_ext);
692228940Sdelphij		HPT_ASSERT(ext);
693228940Sdelphij		ext->ccb = ccb;
694228940Sdelphij		pCmd->target = vd;
695228940Sdelphij		pCmd->done = os_cmddone;
696228940Sdelphij		pCmd->buildsgl = os_buildsgl;
697228940Sdelphij		pCmd->psg = ext->psg;
698246713Skib		pCmd->flags.physical_sg = 1;
699246713Skib		error = bus_dmamap_load_ccb(vbus_ext->io_dmat,
700246713Skib					ext->dma_map, ccb,
701246713Skib					hpt_io_dmamap_callback, pCmd,
702228940Sdelphij				    	BUS_DMA_WAITOK
703228940Sdelphij					);
704246713Skib		KdPrint(("bus_dmamap_load return %d", error));
705246713Skib		if (error && error!=EINPROGRESS) {
706246713Skib			os_printk("bus_dmamap_load error %d", error);
707246713Skib			cmdext_put(ext);
708246713Skib			ldm_free_cmds(pCmd);
709246713Skib			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
710246713Skib			xpt_done(ccb);
711228940Sdelphij		}
712228940Sdelphij		return;
713228940Sdelphij	}
714228940Sdelphij
715228940Sdelphij	default:
716228940Sdelphij		ccb->ccb_h.status = CAM_REQ_INVALID;
717228940Sdelphij		break;
718228940Sdelphij	}
719228940Sdelphij
720228940Sdelphij	xpt_done(ccb);
721228940Sdelphij	return;
722228940Sdelphij}
723228940Sdelphij
724228940Sdelphijstatic void hpt_action(struct cam_sim *sim, union ccb *ccb)
725228940Sdelphij{
726228940Sdelphij	PVBUS_EXT vbus_ext = (PVBUS_EXT)cam_sim_softc(sim);
727228940Sdelphij
728228940Sdelphij	KdPrint(("hpt_action(fn=%d, id=%d)", ccb->ccb_h.func_code, ccb->ccb_h.target_id));
729228940Sdelphij
730228940Sdelphij	switch (ccb->ccb_h.func_code) {
731228940Sdelphij
732228940Sdelphij	case XPT_SCSI_IO:
733228940Sdelphij		hpt_lock_vbus(vbus_ext);
734228940Sdelphij		hpt_scsi_io(vbus_ext, ccb);
735228940Sdelphij		hpt_unlock_vbus(vbus_ext);
736228940Sdelphij		return;
737228940Sdelphij
738228940Sdelphij	case XPT_RESET_BUS:
739228940Sdelphij		hpt_lock_vbus(vbus_ext);
740228940Sdelphij		ldm_reset_vbus((PVBUS)vbus_ext->vbus);
741228940Sdelphij		hpt_unlock_vbus(vbus_ext);
742228940Sdelphij		break;
743228940Sdelphij
744228940Sdelphij	case XPT_GET_TRAN_SETTINGS:
745228940Sdelphij	case XPT_SET_TRAN_SETTINGS:
746228940Sdelphij		ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
747228940Sdelphij		break;
748228940Sdelphij
749228940Sdelphij	case XPT_CALC_GEOMETRY:
750228940Sdelphij		ccb->ccg.heads = 255;
751228940Sdelphij		ccb->ccg.secs_per_track = 63;
752228940Sdelphij		ccb->ccg.cylinders = ccb->ccg.volume_size / (ccb->ccg.heads * ccb->ccg.secs_per_track);
753228940Sdelphij		ccb->ccb_h.status = CAM_REQ_CMP;
754228940Sdelphij		break;
755228940Sdelphij
756228940Sdelphij	case XPT_PATH_INQ:
757228940Sdelphij	{
758228940Sdelphij		struct ccb_pathinq *cpi = &ccb->cpi;
759228940Sdelphij
760228940Sdelphij		cpi->version_num = 1;
761228940Sdelphij		cpi->hba_inquiry = PI_SDTR_ABLE;
762228940Sdelphij		cpi->target_sprt = 0;
763228940Sdelphij		cpi->hba_misc = PIM_NOBUSRESET;
764228940Sdelphij		cpi->hba_eng_cnt = 0;
765228940Sdelphij		cpi->max_target = osm_max_targets;
766228940Sdelphij		cpi->max_lun = 0;
767228940Sdelphij		cpi->unit_number = cam_sim_unit(sim);
768228940Sdelphij		cpi->bus_id = cam_sim_bus(sim);
769228940Sdelphij		cpi->initiator_id = osm_max_targets;
770228940Sdelphij		cpi->base_transfer_speed = 3300;
771228940Sdelphij
772228940Sdelphij		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
773228940Sdelphij		strncpy(cpi->hba_vid, "HPT   ", HBA_IDLEN);
774228940Sdelphij		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
775228940Sdelphij#if (__FreeBSD_version >= 800000)
776228940Sdelphij		cpi->transport = XPORT_SPI;
777228940Sdelphij		cpi->transport_version = 2;
778228940Sdelphij		cpi->protocol = PROTO_SCSI;
779228940Sdelphij		cpi->protocol_version = SCSI_REV_2;
780228940Sdelphij#endif
781228940Sdelphij		cpi->ccb_h.status = CAM_REQ_CMP;
782228940Sdelphij		break;
783228940Sdelphij	}
784228940Sdelphij
785228940Sdelphij	default:
786228940Sdelphij		ccb->ccb_h.status = CAM_REQ_INVALID;
787228940Sdelphij		break;
788228940Sdelphij	}
789228940Sdelphij
790228940Sdelphij	xpt_done(ccb);
791228940Sdelphij	return;
792228940Sdelphij}
793228940Sdelphij
794228940Sdelphijstatic void hpt_pci_intr(void *arg)
795228940Sdelphij{
796228940Sdelphij	PVBUS_EXT vbus_ext = (PVBUS_EXT)arg;
797228940Sdelphij	hpt_lock_vbus(vbus_ext);
798228940Sdelphij	ldm_intr((PVBUS)vbus_ext->vbus);
799228940Sdelphij	hpt_unlock_vbus(vbus_ext);
800228940Sdelphij}
801228940Sdelphij
802228940Sdelphijstatic void hpt_poll(struct cam_sim *sim)
803228940Sdelphij{
804228940Sdelphij	hpt_pci_intr(cam_sim_softc(sim));
805228940Sdelphij}
806228940Sdelphij
807228940Sdelphijstatic void hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, void * arg)
808228940Sdelphij{
809228940Sdelphij	KdPrint(("hpt_async"));
810228940Sdelphij}
811228940Sdelphij
812228940Sdelphijstatic int hpt_shutdown(device_t dev)
813228940Sdelphij{
814228940Sdelphij	KdPrint(("hpt_shutdown(dev=%p)", dev));
815228940Sdelphij	return 0;
816228940Sdelphij}
817228940Sdelphij
818228940Sdelphijstatic int hpt_detach(device_t dev)
819228940Sdelphij{
820228940Sdelphij	/* we don't allow the driver to be unloaded. */
821228940Sdelphij	return EBUSY;
822228940Sdelphij}
823228940Sdelphij
824228940Sdelphijstatic void hpt_ioctl_done(struct _IOCTL_ARG *arg)
825228940Sdelphij{
826228940Sdelphij	arg->ioctl_cmnd = 0;
827228940Sdelphij	wakeup(arg);
828228940Sdelphij}
829228940Sdelphij
830228940Sdelphijstatic void __hpt_do_ioctl(PVBUS_EXT vbus_ext, IOCTL_ARG *ioctl_args)
831228940Sdelphij{
832228940Sdelphij	ioctl_args->result = -1;
833228940Sdelphij	ioctl_args->done = hpt_ioctl_done;
834228940Sdelphij	ioctl_args->ioctl_cmnd = (void *)1;
835228940Sdelphij
836228940Sdelphij	hpt_lock_vbus(vbus_ext);
837228940Sdelphij	ldm_ioctl((PVBUS)vbus_ext->vbus, ioctl_args);
838228940Sdelphij
839228940Sdelphij	while (ioctl_args->ioctl_cmnd) {
840228940Sdelphij		if (hpt_sleep(vbus_ext, ioctl_args, PPAUSE, "hptctl", HPT_OSM_TIMEOUT)==0)
841228940Sdelphij			break;
842228940Sdelphij		ldm_reset_vbus((PVBUS)vbus_ext->vbus);
843228940Sdelphij		__hpt_do_tasks(vbus_ext);
844228940Sdelphij	}
845228940Sdelphij
846228940Sdelphij	/* KdPrint(("ioctl %x result %d", ioctl_args->dwIoControlCode, ioctl_args->result)); */
847228940Sdelphij
848228940Sdelphij	hpt_unlock_vbus(vbus_ext);
849228940Sdelphij}
850228940Sdelphij
851228940Sdelphijstatic void hpt_do_ioctl(IOCTL_ARG *ioctl_args)
852228940Sdelphij{
853228940Sdelphij	PVBUS vbus;
854228940Sdelphij	PVBUS_EXT vbus_ext;
855228940Sdelphij
856228940Sdelphij	ldm_for_each_vbus(vbus, vbus_ext) {
857228940Sdelphij		__hpt_do_ioctl(vbus_ext, ioctl_args);
858228940Sdelphij		if (ioctl_args->result!=HPT_IOCTL_RESULT_WRONG_VBUS)
859228940Sdelphij			return;
860228940Sdelphij	}
861228940Sdelphij}
862228940Sdelphij
863228940Sdelphij#define HPT_DO_IOCTL(code, inbuf, insize, outbuf, outsize) ({\
864228940Sdelphij	IOCTL_ARG arg;\
865228940Sdelphij	arg.dwIoControlCode = code;\
866228940Sdelphij	arg.lpInBuffer = inbuf;\
867228940Sdelphij	arg.lpOutBuffer = outbuf;\
868228940Sdelphij	arg.nInBufferSize = insize;\
869228940Sdelphij	arg.nOutBufferSize = outsize;\
870228940Sdelphij	arg.lpBytesReturned = 0;\
871228940Sdelphij	hpt_do_ioctl(&arg);\
872228940Sdelphij	arg.result;\
873228940Sdelphij})
874228940Sdelphij
875228940Sdelphij#define DEVICEID_VALID(id) ((id) && ((HPT_U32)(id)!=0xffffffff))
876228940Sdelphij
877228940Sdelphijstatic int hpt_get_logical_devices(DEVICEID * pIds, int nMaxCount)
878228940Sdelphij{
879228940Sdelphij	int i;
880228940Sdelphij	HPT_U32 count = nMaxCount-1;
881228940Sdelphij
882228940Sdelphij	if (HPT_DO_IOCTL(HPT_IOCTL_GET_LOGICAL_DEVICES,
883228940Sdelphij			&count, sizeof(HPT_U32), pIds, sizeof(DEVICEID)*nMaxCount))
884228940Sdelphij		return -1;
885228940Sdelphij
886228940Sdelphij	nMaxCount = (int)pIds[0];
887228940Sdelphij	for (i=0; i<nMaxCount; i++) pIds[i] = pIds[i+1];
888228940Sdelphij	return nMaxCount;
889228940Sdelphij}
890228940Sdelphij
891228940Sdelphijstatic int hpt_get_device_info_v3(DEVICEID id, PLOGICAL_DEVICE_INFO_V3 pInfo)
892228940Sdelphij{
893228940Sdelphij	return HPT_DO_IOCTL(HPT_IOCTL_GET_DEVICE_INFO_V3,
894228940Sdelphij				&id, sizeof(DEVICEID), pInfo, sizeof(LOGICAL_DEVICE_INFO_V3));
895228940Sdelphij}
896228940Sdelphij
897228940Sdelphij/* not belong to this file logically, but we want to use ioctl interface */
898228940Sdelphijstatic int __hpt_stop_tasks(PVBUS_EXT vbus_ext, DEVICEID id)
899228940Sdelphij{
900228940Sdelphij	LOGICAL_DEVICE_INFO_V3 devinfo;
901228940Sdelphij	int i, result;
902228940Sdelphij	DEVICEID param[2] = { id, 0 };
903228940Sdelphij
904228940Sdelphij	if (hpt_get_device_info_v3(id, &devinfo))
905228940Sdelphij		return -1;
906228940Sdelphij
907228940Sdelphij	if (devinfo.Type!=LDT_ARRAY)
908228940Sdelphij		return -1;
909228940Sdelphij
910228940Sdelphij	if (devinfo.u.array.Flags & ARRAY_FLAG_REBUILDING)
911228940Sdelphij		param[1] = AS_REBUILD_ABORT;
912228940Sdelphij	else if (devinfo.u.array.Flags & ARRAY_FLAG_VERIFYING)
913228940Sdelphij		param[1] = AS_VERIFY_ABORT;
914228940Sdelphij	else if (devinfo.u.array.Flags & ARRAY_FLAG_INITIALIZING)
915228940Sdelphij		param[1] = AS_INITIALIZE_ABORT;
916228940Sdelphij	else if (devinfo.u.array.Flags & ARRAY_FLAG_TRANSFORMING)
917228940Sdelphij		param[1] = AS_TRANSFORM_ABORT;
918228940Sdelphij	else
919228940Sdelphij		return -1;
920228940Sdelphij
921228940Sdelphij	KdPrint(("SET_ARRAY_STATE(%x, %d)", param[0], param[1]));
922228940Sdelphij	result = HPT_DO_IOCTL(HPT_IOCTL_SET_ARRAY_STATE,
923228940Sdelphij				param, sizeof(param), 0, 0);
924228940Sdelphij
925228940Sdelphij	for (i=0; i<devinfo.u.array.nDisk; i++)
926228940Sdelphij		if (DEVICEID_VALID(devinfo.u.array.Members[i]))
927228940Sdelphij			__hpt_stop_tasks(vbus_ext, devinfo.u.array.Members[i]);
928228940Sdelphij
929228940Sdelphij	return result;
930228940Sdelphij}
931228940Sdelphij
932228940Sdelphijstatic void hpt_stop_tasks(PVBUS_EXT vbus_ext)
933228940Sdelphij{
934228940Sdelphij	DEVICEID ids[32];
935228940Sdelphij	int i, count;
936228940Sdelphij
937228940Sdelphij	count = hpt_get_logical_devices((DEVICEID *)&ids, sizeof(ids)/sizeof(ids[0]));
938228940Sdelphij
939228940Sdelphij	for (i=0; i<count; i++)
940228940Sdelphij		__hpt_stop_tasks(vbus_ext, ids[i]);
941228940Sdelphij}
942228940Sdelphij
943228940Sdelphijstatic	d_open_t	hpt_open;
944228940Sdelphijstatic	d_close_t	hpt_close;
945228940Sdelphijstatic	d_ioctl_t	hpt_ioctl;
946228940Sdelphijstatic  int 		hpt_rescan_bus(void);
947228940Sdelphij
948228940Sdelphijstatic struct cdevsw hpt_cdevsw = {
949228940Sdelphij	.d_open =	hpt_open,
950228940Sdelphij	.d_close =	hpt_close,
951228940Sdelphij	.d_ioctl =	hpt_ioctl,
952228940Sdelphij	.d_name =	driver_name,
953228940Sdelphij#if __FreeBSD_version>=503000
954228940Sdelphij	.d_version =	D_VERSION,
955228940Sdelphij#endif
956228940Sdelphij#if (__FreeBSD_version>=503000 && __FreeBSD_version<600034)
957228940Sdelphij	.d_flags =	D_NEEDGIANT,
958228940Sdelphij#endif
959228940Sdelphij#if __FreeBSD_version<600034
960228940Sdelphij#if __FreeBSD_version>501000
961228940Sdelphij	.d_maj = 	MAJOR_AUTO,
962228940Sdelphij#else
963228940Sdelphij	.d_maj = HPT_DEV_MAJOR,
964228940Sdelphij#endif
965228940Sdelphij#endif
966228940Sdelphij};
967228940Sdelphij
968228940Sdelphijstatic struct intr_config_hook hpt_ich;
969228940Sdelphij
970228940Sdelphij/*
971228940Sdelphij * hpt_final_init will be called after all hpt_attach.
972228940Sdelphij */
973228940Sdelphijstatic void hpt_final_init(void *dummy)
974228940Sdelphij{
975252852Sdelphij	int       i,unit_number=0;
976228940Sdelphij	PVBUS_EXT vbus_ext;
977228940Sdelphij	PVBUS vbus;
978228940Sdelphij	PHBA hba;
979228940Sdelphij
980228940Sdelphij	/* Clear the config hook */
981228940Sdelphij	config_intrhook_disestablish(&hpt_ich);
982228940Sdelphij
983228940Sdelphij	/* allocate memory */
984228940Sdelphij	i = 0;
985228940Sdelphij	ldm_for_each_vbus(vbus, vbus_ext) {
986228940Sdelphij		if (hpt_alloc_mem(vbus_ext)) {
987228940Sdelphij			os_printk("out of memory");
988228940Sdelphij			return;
989228940Sdelphij		}
990228940Sdelphij		i++;
991228940Sdelphij	}
992228940Sdelphij
993228940Sdelphij	if (!i) {
994245768Sdelphij		if (bootverbose)
995228940Sdelphij			os_printk("no controller detected.");
996228940Sdelphij		return;
997228940Sdelphij	}
998228940Sdelphij
999228940Sdelphij	/* initializing hardware */
1000228940Sdelphij	ldm_for_each_vbus(vbus, vbus_ext) {
1001228940Sdelphij		/* make timer available here */
1002228940Sdelphij		callout_handle_init(&vbus_ext->timer);
1003228940Sdelphij		if (hpt_init_vbus(vbus_ext)) {
1004228940Sdelphij			os_printk("fail to initialize hardware");
1005228940Sdelphij			break; /* FIXME */
1006228940Sdelphij		}
1007228940Sdelphij	}
1008228940Sdelphij
1009228940Sdelphij	/* register CAM interface */
1010228940Sdelphij	ldm_for_each_vbus(vbus, vbus_ext) {
1011228940Sdelphij		struct cam_devq *devq;
1012228940Sdelphij		struct ccb_setasync	ccb;
1013228940Sdelphij
1014228940Sdelphij#if (__FreeBSD_version >= 500000)
1015228940Sdelphij		mtx_init(&vbus_ext->lock, "hptsleeplock", NULL, MTX_DEF);
1016228940Sdelphij#endif
1017228940Sdelphij		if (bus_dma_tag_create(NULL,/* parent */
1018228940Sdelphij				4,	/* alignment */
1019228940Sdelphij				BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
1020228940Sdelphij				BUS_SPACE_MAXADDR,	/* lowaddr */
1021228940Sdelphij				BUS_SPACE_MAXADDR, 	/* highaddr */
1022228940Sdelphij				NULL, NULL, 		/* filter, filterarg */
1023228940Sdelphij				PAGE_SIZE * (os_max_sg_descriptors-1),	/* maxsize */
1024228940Sdelphij				os_max_sg_descriptors,	/* nsegments */
1025228940Sdelphij				0x10000,	/* maxsegsize */
1026228940Sdelphij				BUS_DMA_WAITOK,		/* flags */
1027228940Sdelphij#if __FreeBSD_version>502000
1028228940Sdelphij				busdma_lock_mutex,	/* lockfunc */
1029228940Sdelphij				&vbus_ext->lock,		/* lockfuncarg */
1030228940Sdelphij#endif
1031228940Sdelphij				&vbus_ext->io_dmat	/* tag */))
1032228940Sdelphij		{
1033228940Sdelphij			return ;
1034228940Sdelphij		}
1035228940Sdelphij
1036228940Sdelphij		for (i=0; i<os_max_queue_comm; i++) {
1037228940Sdelphij			POS_CMDEXT ext = (POS_CMDEXT)malloc(sizeof(OS_CMDEXT), M_DEVBUF, M_WAITOK);
1038228940Sdelphij			if (!ext) {
1039228940Sdelphij				os_printk("Can't alloc cmdext(%d)", i);
1040228940Sdelphij				return ;
1041228940Sdelphij			}
1042228940Sdelphij			ext->vbus_ext = vbus_ext;
1043228940Sdelphij			ext->next = vbus_ext->cmdext_list;
1044228940Sdelphij			vbus_ext->cmdext_list = ext;
1045228940Sdelphij
1046228940Sdelphij			if (bus_dmamap_create(vbus_ext->io_dmat, 0, &ext->dma_map)) {
1047228940Sdelphij				os_printk("Can't create dma map(%d)", i);
1048228940Sdelphij				return ;
1049228940Sdelphij			}
1050255871Sscottl			callout_handle_init(&ext->timeout_ch);
1051228940Sdelphij		}
1052228940Sdelphij
1053228940Sdelphij		if ((devq = cam_simq_alloc(os_max_queue_comm)) == NULL) {
1054228940Sdelphij			os_printk("cam_simq_alloc failed");
1055228940Sdelphij			return ;
1056228940Sdelphij		}
1057228940Sdelphij
1058228940Sdelphij#if __FreeBSD_version > 700025
1059228940Sdelphij		vbus_ext->sim = cam_sim_alloc(hpt_action, hpt_poll, driver_name,
1060252852Sdelphij				vbus_ext, unit_number, &Giant, os_max_queue_comm, /*tagged*/8,  devq);
1061228940Sdelphij#else
1062228940Sdelphij		vbus_ext->sim = cam_sim_alloc(hpt_action, hpt_poll, driver_name,
1063252852Sdelphij				vbus_ext, unit_number, os_max_queue_comm, /*tagged*/8,  devq);
1064228940Sdelphij#endif
1065252852Sdelphij		unit_number++;
1066228940Sdelphij		if (!vbus_ext->sim) {
1067228940Sdelphij			os_printk("cam_sim_alloc failed");
1068228940Sdelphij			cam_simq_free(devq);
1069228940Sdelphij			return ;
1070228940Sdelphij		}
1071228940Sdelphij
1072228940Sdelphij#if __FreeBSD_version > 700044
1073228940Sdelphij		if (xpt_bus_register(vbus_ext->sim, NULL, 0) != CAM_SUCCESS) {
1074228940Sdelphij#else
1075228940Sdelphij		if (xpt_bus_register(vbus_ext->sim, 0) != CAM_SUCCESS) {
1076228940Sdelphij#endif
1077228940Sdelphij			os_printk("xpt_bus_register failed");
1078228940Sdelphij			cam_sim_free(vbus_ext->sim, /*free devq*/ TRUE);
1079228940Sdelphij			vbus_ext->sim = NULL;
1080228940Sdelphij			return ;
1081228940Sdelphij		}
1082228940Sdelphij
1083228940Sdelphij		if (xpt_create_path(&vbus_ext->path, /*periph */ NULL,
1084228940Sdelphij				cam_sim_path(vbus_ext->sim), CAM_TARGET_WILDCARD,
1085228940Sdelphij				CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1086228940Sdelphij		{
1087228940Sdelphij			os_printk("xpt_create_path failed");
1088228940Sdelphij			xpt_bus_deregister(cam_sim_path(vbus_ext->sim));
1089228940Sdelphij			cam_sim_free(vbus_ext->sim, /*free_devq*/TRUE);
1090228940Sdelphij			vbus_ext->sim = NULL;
1091228940Sdelphij			return ;
1092228940Sdelphij		}
1093228940Sdelphij
1094228940Sdelphij		xpt_setup_ccb(&ccb.ccb_h, vbus_ext->path, /*priority*/5);
1095228940Sdelphij		ccb.ccb_h.func_code = XPT_SASYNC_CB;
1096228940Sdelphij		ccb.event_enable = AC_LOST_DEVICE;
1097228940Sdelphij		ccb.callback = hpt_async;
1098228940Sdelphij		ccb.callback_arg = vbus_ext;
1099228940Sdelphij		xpt_action((union ccb *)&ccb);
1100228940Sdelphij
1101228940Sdelphij		for (hba = vbus_ext->hba_list; hba; hba = hba->next) {
1102228940Sdelphij			int rid = 0;
1103228940Sdelphij			if ((hba->irq_res = bus_alloc_resource(hba->pcidev,
1104228940Sdelphij				SYS_RES_IRQ, &rid, 0, ~0ul, 1, RF_SHAREABLE | RF_ACTIVE)) == NULL)
1105228940Sdelphij			{
1106228940Sdelphij				os_printk("can't allocate interrupt");
1107228940Sdelphij				return ;
1108228940Sdelphij			}
1109228940Sdelphij
1110228940Sdelphij			if (bus_setup_intr(hba->pcidev, hba->irq_res, INTR_TYPE_CAM,
1111228940Sdelphij#if __FreeBSD_version > 700025
1112228940Sdelphij				NULL, hpt_pci_intr, vbus_ext, &hba->irq_handle))
1113228940Sdelphij#else
1114228940Sdelphij				hpt_pci_intr, vbus_ext, &hba->irq_handle))
1115228940Sdelphij#endif
1116228940Sdelphij			{
1117228940Sdelphij				os_printk("can't set up interrupt");
1118228940Sdelphij				return ;
1119228940Sdelphij			}
1120228940Sdelphij			hba->ldm_adapter.him->intr_control(hba->ldm_adapter.him_handle, HPT_TRUE);
1121228940Sdelphij
1122228940Sdelphij		}
1123228940Sdelphij
1124228940Sdelphij		vbus_ext->shutdown_eh = EVENTHANDLER_REGISTER(shutdown_final,
1125228940Sdelphij									hpt_shutdown_vbus, vbus_ext, SHUTDOWN_PRI_DEFAULT);
1126228940Sdelphij		if (!vbus_ext->shutdown_eh)
1127228940Sdelphij			os_printk("Shutdown event registration failed");
1128228940Sdelphij	}
1129228940Sdelphij
1130228940Sdelphij	ldm_for_each_vbus(vbus, vbus_ext) {
1131228940Sdelphij		TASK_INIT(&vbus_ext->worker, 0, (task_fn_t *)hpt_do_tasks, vbus_ext);
1132228940Sdelphij		if (vbus_ext->tasks)
1133228940Sdelphij			TASK_ENQUEUE(&vbus_ext->worker);
1134228940Sdelphij	}
1135228940Sdelphij
1136228940Sdelphij	make_dev(&hpt_cdevsw, DRIVER_MINOR, UID_ROOT, GID_OPERATOR,
1137245768Sdelphij	    S_IRUSR | S_IWUSR, "%s", driver_name);
1138228940Sdelphij}
1139228940Sdelphij
1140228940Sdelphij#if defined(KLD_MODULE) && (__FreeBSD_version >= 503000)
1141228940Sdelphij
1142228940Sdelphijtypedef struct driverlink *driverlink_t;
1143228940Sdelphijstruct driverlink {
1144228940Sdelphij	kobj_class_t	driver;
1145228940Sdelphij	TAILQ_ENTRY(driverlink) link;	/* list of drivers in devclass */
1146228940Sdelphij};
1147228940Sdelphij
1148228940Sdelphijtypedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
1149228940Sdelphij
1150228940Sdelphijstruct devclass {
1151228940Sdelphij	TAILQ_ENTRY(devclass) link;
1152228940Sdelphij	devclass_t	parent;		/* parent in devclass hierarchy */
1153228940Sdelphij	driver_list_t	drivers;     /* bus devclasses store drivers for bus */
1154228940Sdelphij	char		*name;
1155228940Sdelphij	device_t	*devices;	/* array of devices indexed by unit */
1156228940Sdelphij	int		maxunit;	/* size of devices array */
1157228940Sdelphij};
1158228940Sdelphij
1159228940Sdelphijstatic void override_kernel_driver(void)
1160228940Sdelphij{
1161228940Sdelphij	driverlink_t dl, dlfirst;
1162228940Sdelphij	driver_t *tmpdriver;
1163228940Sdelphij	devclass_t dc = devclass_find("pci");
1164228940Sdelphij
1165228940Sdelphij	if (dc){
1166228940Sdelphij		dlfirst = TAILQ_FIRST(&dc->drivers);
1167228940Sdelphij		for (dl = dlfirst; dl; dl = TAILQ_NEXT(dl, link)) {
1168228940Sdelphij			if(strcmp(dl->driver->name, driver_name) == 0) {
1169228940Sdelphij				tmpdriver=dl->driver;
1170228940Sdelphij				dl->driver=dlfirst->driver;
1171228940Sdelphij				dlfirst->driver=tmpdriver;
1172228940Sdelphij				break;
1173228940Sdelphij			}
1174228940Sdelphij		}
1175228940Sdelphij	}
1176228940Sdelphij}
1177228940Sdelphij
1178228940Sdelphij#else
1179228940Sdelphij#define override_kernel_driver()
1180228940Sdelphij#endif
1181228940Sdelphij
1182228940Sdelphijstatic void hpt_init(void *dummy)
1183228940Sdelphij{
1184245768Sdelphij	if (bootverbose)
1185228940Sdelphij		os_printk("%s %s", driver_name_long, driver_ver);
1186228940Sdelphij
1187228940Sdelphij	override_kernel_driver();
1188228940Sdelphij	init_config();
1189228940Sdelphij
1190228940Sdelphij	hpt_ich.ich_func = hpt_final_init;
1191228940Sdelphij	hpt_ich.ich_arg = NULL;
1192228940Sdelphij	if (config_intrhook_establish(&hpt_ich) != 0) {
1193228940Sdelphij		printf("%s: cannot establish configuration hook\n",
1194228940Sdelphij		    driver_name_long);
1195228940Sdelphij	}
1196228940Sdelphij
1197228940Sdelphij}
1198228940SdelphijSYSINIT(hptinit, SI_SUB_CONFIGURE, SI_ORDER_FIRST, hpt_init, NULL);
1199228940Sdelphij
1200228940Sdelphij/*
1201228940Sdelphij * CAM driver interface
1202228940Sdelphij */
1203228940Sdelphijstatic device_method_t driver_methods[] = {
1204228940Sdelphij	/* Device interface */
1205228940Sdelphij	DEVMETHOD(device_probe,		hpt_probe),
1206228940Sdelphij	DEVMETHOD(device_attach,	hpt_attach),
1207228940Sdelphij	DEVMETHOD(device_detach,	hpt_detach),
1208228940Sdelphij	DEVMETHOD(device_shutdown,	hpt_shutdown),
1209228940Sdelphij	{ 0, 0 }
1210228940Sdelphij};
1211228940Sdelphij
1212228940Sdelphijstatic driver_t hpt_pci_driver = {
1213228940Sdelphij	driver_name,
1214228940Sdelphij	driver_methods,
1215228940Sdelphij	sizeof(HBA)
1216228940Sdelphij};
1217228940Sdelphij
1218228940Sdelphijstatic devclass_t	hpt_devclass;
1219228940Sdelphij
1220228940Sdelphij#ifndef TARGETNAME
1221228940Sdelphij#error "no TARGETNAME found"
1222228940Sdelphij#endif
1223228940Sdelphij
1224228940Sdelphij/* use this to make TARGETNAME be expanded */
1225228940Sdelphij#define __DRIVER_MODULE(p1, p2, p3, p4, p5, p6) DRIVER_MODULE(p1, p2, p3, p4, p5, p6)
1226228940Sdelphij#define __MODULE_VERSION(p1, p2) MODULE_VERSION(p1, p2)
1227228940Sdelphij#define __MODULE_DEPEND(p1, p2, p3, p4, p5) MODULE_DEPEND(p1, p2, p3, p4, p5)
1228228940Sdelphij__DRIVER_MODULE(TARGETNAME, pci, hpt_pci_driver, hpt_devclass, 0, 0);
1229228940Sdelphij__MODULE_VERSION(TARGETNAME, 1);
1230228940Sdelphij__MODULE_DEPEND(TARGETNAME, cam, 1, 1, 1);
1231228940Sdelphij
1232228940Sdelphij#if __FreeBSD_version>503000
1233228940Sdelphijtypedef struct cdev * ioctl_dev_t;
1234228940Sdelphij#else
1235228940Sdelphijtypedef dev_t ioctl_dev_t;
1236228940Sdelphij#endif
1237228940Sdelphij
1238228940Sdelphij#if __FreeBSD_version >= 500000
1239228940Sdelphijtypedef	struct thread *	ioctl_thread_t;
1240228940Sdelphij#else
1241228940Sdelphijtypedef struct proc *	ioctl_thread_t;
1242228940Sdelphij#endif
1243228940Sdelphij
1244228940Sdelphijstatic int hpt_open(ioctl_dev_t dev, int flags, int devtype, ioctl_thread_t td)
1245228940Sdelphij{
1246228940Sdelphij	return 0;
1247228940Sdelphij}
1248228940Sdelphij
1249228940Sdelphijstatic int hpt_close(ioctl_dev_t dev, int flags, int devtype, ioctl_thread_t td)
1250228940Sdelphij{
1251228940Sdelphij	return 0;
1252228940Sdelphij}
1253228940Sdelphij
1254228940Sdelphijstatic int hpt_ioctl(ioctl_dev_t dev, u_long cmd, caddr_t data, int fflag, ioctl_thread_t td)
1255228940Sdelphij{
1256228940Sdelphij	PHPT_IOCTL_PARAM piop=(PHPT_IOCTL_PARAM)data;
1257228940Sdelphij	IOCTL_ARG ioctl_args;
1258228940Sdelphij	HPT_U32 bytesReturned;
1259228940Sdelphij
1260228940Sdelphij	switch (cmd){
1261228940Sdelphij	case HPT_DO_IOCONTROL:
1262228940Sdelphij	{
1263228940Sdelphij		if (piop->Magic == HPT_IOCTL_MAGIC || piop->Magic == HPT_IOCTL_MAGIC32) {
1264228940Sdelphij			KdPrint(("ioctl=%x in=%p len=%d out=%p len=%d\n",
1265228940Sdelphij				piop->dwIoControlCode,
1266228940Sdelphij				piop->lpInBuffer,
1267228940Sdelphij				piop->nInBufferSize,
1268228940Sdelphij				piop->lpOutBuffer,
1269228940Sdelphij				piop->nOutBufferSize));
1270228940Sdelphij
1271228940Sdelphij		memset(&ioctl_args, 0, sizeof(ioctl_args));
1272228940Sdelphij
1273228940Sdelphij		ioctl_args.dwIoControlCode = piop->dwIoControlCode;
1274228940Sdelphij		ioctl_args.nInBufferSize = piop->nInBufferSize;
1275228940Sdelphij		ioctl_args.nOutBufferSize = piop->nOutBufferSize;
1276228940Sdelphij		ioctl_args.lpBytesReturned = &bytesReturned;
1277228940Sdelphij
1278228940Sdelphij		if (ioctl_args.nInBufferSize) {
1279228940Sdelphij			ioctl_args.lpInBuffer = malloc(ioctl_args.nInBufferSize, M_DEVBUF, M_WAITOK);
1280228940Sdelphij			if (!ioctl_args.lpInBuffer)
1281228940Sdelphij				goto invalid;
1282228940Sdelphij			if (copyin((void*)piop->lpInBuffer,
1283228940Sdelphij					ioctl_args.lpInBuffer, piop->nInBufferSize))
1284228940Sdelphij				goto invalid;
1285228940Sdelphij		}
1286228940Sdelphij
1287228940Sdelphij		if (ioctl_args.nOutBufferSize) {
1288228940Sdelphij			ioctl_args.lpOutBuffer = malloc(ioctl_args.nOutBufferSize, M_DEVBUF, M_WAITOK);
1289228940Sdelphij			if (!ioctl_args.lpOutBuffer)
1290228940Sdelphij				goto invalid;
1291228940Sdelphij		}
1292228940Sdelphij
1293228940Sdelphij#if (__FreeBSD_version >= 500000)
1294228940Sdelphij		mtx_lock(&Giant);
1295228940Sdelphij#endif
1296228940Sdelphij
1297228940Sdelphij		hpt_do_ioctl(&ioctl_args);
1298228940Sdelphij
1299228940Sdelphij#if (__FreeBSD_version >= 500000)
1300228940Sdelphij		mtx_unlock(&Giant);
1301228940Sdelphij#endif
1302228940Sdelphij
1303228940Sdelphij		if (ioctl_args.result==HPT_IOCTL_RESULT_OK) {
1304228940Sdelphij			if (piop->nOutBufferSize) {
1305228940Sdelphij				if (copyout(ioctl_args.lpOutBuffer,
1306228940Sdelphij					(void*)piop->lpOutBuffer, piop->nOutBufferSize))
1307228940Sdelphij					goto invalid;
1308228940Sdelphij			}
1309228940Sdelphij			if (piop->lpBytesReturned) {
1310228940Sdelphij				if (copyout(&bytesReturned,
1311228940Sdelphij					(void*)piop->lpBytesReturned, sizeof(HPT_U32)))
1312228940Sdelphij					goto invalid;
1313228940Sdelphij			}
1314228940Sdelphij			if (ioctl_args.lpInBuffer) free(ioctl_args.lpInBuffer, M_DEVBUF);
1315228940Sdelphij			if (ioctl_args.lpOutBuffer) free(ioctl_args.lpOutBuffer, M_DEVBUF);
1316228940Sdelphij			return 0;
1317228940Sdelphij		}
1318228940Sdelphijinvalid:
1319228940Sdelphij		if (ioctl_args.lpInBuffer) free(ioctl_args.lpInBuffer, M_DEVBUF);
1320228940Sdelphij		if (ioctl_args.lpOutBuffer) free(ioctl_args.lpOutBuffer, M_DEVBUF);
1321228940Sdelphij		return EFAULT;
1322228940Sdelphij	}
1323228940Sdelphij	return EFAULT;
1324228940Sdelphij	}
1325228940Sdelphij
1326228940Sdelphij	case HPT_SCAN_BUS:
1327228940Sdelphij	{
1328228940Sdelphij		return hpt_rescan_bus();
1329228940Sdelphij	}
1330228940Sdelphij	default:
1331228940Sdelphij		KdPrint(("invalid command!"));
1332228940Sdelphij		return EFAULT;
1333228940Sdelphij	}
1334228940Sdelphij
1335228940Sdelphij}
1336228940Sdelphij
1337228940Sdelphijstatic int	hpt_rescan_bus(void)
1338228940Sdelphij{
1339228940Sdelphij	union ccb			*ccb;
1340228940Sdelphij	PVBUS 				vbus;
1341228940Sdelphij	PVBUS_EXT			vbus_ext;
1342228940Sdelphij
1343228940Sdelphij	mtx_lock(&Giant);
1344228940Sdelphij
1345228940Sdelphij	ldm_for_each_vbus(vbus, vbus_ext) {
1346252852Sdelphij		if ((ccb = xpt_alloc_ccb()) == NULL)
1347252852Sdelphij		{
1348252852Sdelphij			return(ENOMEM);
1349252852Sdelphij		}
1350252852Sdelphij		if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(vbus_ext->sim),
1351228940Sdelphij			CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1352252852Sdelphij		{
1353252852Sdelphij			xpt_free_ccb(ccb);
1354228940Sdelphij			return(EIO);
1355252852Sdelphij		}
1356252852Sdelphij		xpt_rescan(ccb);
1357228940Sdelphij	}
1358228940Sdelphij	mtx_unlock(&Giant);
1359228940Sdelphij	return(0);
1360228940Sdelphij}
1361228940Sdelphij
1362