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