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