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