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