1718cf2ccSPedro F. Giffuni /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
4b063a422SScott Long * Copyright (c) HighPoint Technologies, Inc.
5b063a422SScott Long * All rights reserved.
6b063a422SScott Long *
7b063a422SScott Long * Redistribution and use in source and binary forms, with or without
8b063a422SScott Long * modification, are permitted provided that the following conditions
9b063a422SScott Long * are met:
10b063a422SScott Long * 1. Redistributions of source code must retain the above copyright
11b063a422SScott Long * notice, this list of conditions and the following disclaimer.
12b063a422SScott Long * 2. Redistributions in binary form must reproduce the above copyright
13b063a422SScott Long * notice, this list of conditions and the following disclaimer in the
14b063a422SScott Long * documentation and/or other materials provided with the distribution.
15b063a422SScott Long *
16b063a422SScott Long * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17b063a422SScott Long * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18b063a422SScott Long * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19b063a422SScott Long * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20b063a422SScott Long * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21b063a422SScott Long * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22b063a422SScott Long * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23b063a422SScott Long * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24b063a422SScott Long * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25b063a422SScott Long * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26b063a422SScott Long * SUCH DAMAGE.
27b063a422SScott Long */
28f3b080e6SMarius Strobl
29f3b080e6SMarius Strobl #include <sys/cdefs.h>
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
hpt_match(device_t dev)4204584935SJohn Baldwin static HIM *hpt_match(device_t dev)
43b063a422SScott Long {
44b063a422SScott Long PCI_ID pci_id;
45b063a422SScott Long int i;
4604584935SJohn Baldwin HIM *him;
47b063a422SScott Long
48c43ee887SAlexander Motin /* Some of supported chips are used not only by HPT. */
49c43ee887SAlexander Motin if (pci_get_vendor(dev) != 0x1103 && !attach_generic)
5004584935SJohn Baldwin return (NULL);
51b063a422SScott Long for (him = him_list; him; him = him->next) {
52b063a422SScott Long for (i=0; him->get_supported_device_id(i, &pci_id); i++) {
53b063a422SScott Long if ((pci_get_vendor(dev) == pci_id.vid) &&
54b063a422SScott Long (pci_get_device(dev) == pci_id.did)){
5504584935SJohn Baldwin return (him);
5604584935SJohn Baldwin }
5704584935SJohn Baldwin }
5804584935SJohn Baldwin }
5904584935SJohn Baldwin return (NULL);
6004584935SJohn Baldwin }
6104584935SJohn Baldwin
hpt_probe(device_t dev)6204584935SJohn Baldwin static int hpt_probe(device_t dev)
6304584935SJohn Baldwin {
6404584935SJohn Baldwin HIM *him;
6504584935SJohn Baldwin
6604584935SJohn Baldwin him = hpt_match(dev);
6704584935SJohn Baldwin if (him != NULL) {
68b063a422SScott Long KdPrint(("hpt_probe: adapter at PCI %d:%d:%d, IRQ %d",
69b063a422SScott Long pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev), pci_get_irq(dev)
70b063a422SScott Long ));
71b063a422SScott Long device_set_desc(dev, him->name);
727634a04bSXin LI return (BUS_PROBE_DEFAULT);
73b063a422SScott Long }
74b063a422SScott Long
75b063a422SScott Long return (ENXIO);
76b063a422SScott Long }
77b063a422SScott Long
hpt_attach(device_t dev)78b063a422SScott Long static int hpt_attach(device_t dev)
79b063a422SScott Long {
80b063a422SScott Long PHBA hba = (PHBA)device_get_softc(dev);
8104584935SJohn Baldwin HIM *him;
82b063a422SScott Long PCI_ID pci_id;
83b063a422SScott Long HPT_UINT size;
84b063a422SScott Long PVBUS vbus;
85b063a422SScott Long PVBUS_EXT vbus_ext;
86b063a422SScott Long
87b063a422SScott Long KdPrint(("hpt_attach(%d/%d/%d)", pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev)));
88b063a422SScott Long
8904584935SJohn Baldwin him = hpt_match(dev);
9004584935SJohn Baldwin hba->ext_type = EXT_TYPE_HBA;
9104584935SJohn Baldwin hba->ldm_adapter.him = him;
9204584935SJohn Baldwin
93b063a422SScott Long pci_enable_busmaster(dev);
94b063a422SScott Long
95b063a422SScott Long pci_id.vid = pci_get_vendor(dev);
96b063a422SScott Long pci_id.did = pci_get_device(dev);
97b063a422SScott Long pci_id.rev = pci_get_revid(dev);
98b063a422SScott Long
99b063a422SScott Long size = him->get_adapter_size(&pci_id);
100b063a422SScott Long hba->ldm_adapter.him_handle = malloc(size, M_DEVBUF, M_WAITOK);
101b063a422SScott Long
102b063a422SScott Long hba->pcidev = dev;
103b063a422SScott Long hba->pciaddr.tree = 0;
104b063a422SScott Long hba->pciaddr.bus = pci_get_bus(dev);
105b063a422SScott Long hba->pciaddr.device = pci_get_slot(dev);
106b063a422SScott Long hba->pciaddr.function = pci_get_function(dev);
107b063a422SScott Long
108b063a422SScott Long if (!him->create_adapter(&pci_id, hba->pciaddr, hba->ldm_adapter.him_handle, hba)) {
109b063a422SScott Long free(hba->ldm_adapter.him_handle, M_DEVBUF);
11004584935SJohn Baldwin return ENXIO;
111b063a422SScott Long }
112b063a422SScott Long
113b063a422SScott Long os_printk("adapter at PCI %d:%d:%d, IRQ %d",
114b063a422SScott Long hba->pciaddr.bus, hba->pciaddr.device, hba->pciaddr.function, pci_get_irq(dev));
115b063a422SScott Long
116b063a422SScott Long if (!ldm_register_adapter(&hba->ldm_adapter)) {
117b063a422SScott Long size = ldm_get_vbus_size();
11804584935SJohn Baldwin vbus_ext = malloc(sizeof(VBUS_EXT) + size, M_DEVBUF, M_WAITOK |
11904584935SJohn Baldwin M_ZERO);
120b063a422SScott Long vbus_ext->ext_type = EXT_TYPE_VBUS;
121b063a422SScott Long ldm_create_vbus((PVBUS)vbus_ext->vbus, vbus_ext);
122b063a422SScott Long ldm_register_adapter(&hba->ldm_adapter);
123b063a422SScott Long }
124b063a422SScott Long
125b063a422SScott Long ldm_for_each_vbus(vbus, vbus_ext) {
126b063a422SScott Long if (hba->ldm_adapter.vbus==vbus) {
127b063a422SScott Long hba->vbus_ext = vbus_ext;
128b063a422SScott Long hba->next = vbus_ext->hba_list;
129b063a422SScott Long vbus_ext->hba_list = hba;
130b063a422SScott Long break;
131b063a422SScott Long }
132b063a422SScott Long }
133b063a422SScott Long return 0;
134b063a422SScott Long }
135b063a422SScott Long
136b063a422SScott Long /*
137b063a422SScott Long * Maybe we'd better to use the bus_dmamem_alloc to alloc DMA memory,
138b063a422SScott Long * but there are some problems currently (alignment, etc).
139b063a422SScott Long */
__get_free_pages(int order)140b063a422SScott Long static __inline void *__get_free_pages(int order)
141b063a422SScott Long {
142b063a422SScott Long /* don't use low memory - other devices may get starved */
143b063a422SScott Long return contigmalloc(PAGE_SIZE<<order,
144b063a422SScott Long M_DEVBUF, M_WAITOK, BUS_SPACE_MAXADDR_24BIT, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
145b063a422SScott Long }
146b063a422SScott Long
free_pages(void * p)147d1bdc282SBjoern A. Zeeb static __inline void free_pages(void *p)
148b063a422SScott Long {
149d1bdc282SBjoern A. Zeeb free(p, M_DEVBUF);
150b063a422SScott Long }
151b063a422SScott Long
hpt_alloc_mem(PVBUS_EXT vbus_ext)152b063a422SScott Long static int hpt_alloc_mem(PVBUS_EXT vbus_ext)
153b063a422SScott Long {
154b063a422SScott Long PHBA hba;
155b063a422SScott Long struct freelist *f;
156b063a422SScott Long HPT_UINT i;
157b063a422SScott Long void **p;
158b063a422SScott Long
159b063a422SScott Long for (hba = vbus_ext->hba_list; hba; hba = hba->next)
160b063a422SScott Long hba->ldm_adapter.him->get_meminfo(hba->ldm_adapter.him_handle);
161b063a422SScott Long
162b063a422SScott Long ldm_get_mem_info((PVBUS)vbus_ext->vbus, 0);
163b063a422SScott Long
164b063a422SScott Long for (f=vbus_ext->freelist_head; f; f=f->next) {
165b063a422SScott Long KdPrint(("%s: %d*%d=%d bytes",
166b063a422SScott Long f->tag, f->count, f->size, f->count*f->size));
167b063a422SScott Long for (i=0; i<f->count; i++) {
168b063a422SScott Long p = (void **)malloc(f->size, M_DEVBUF, M_WAITOK);
169b063a422SScott Long if (!p) return (ENXIO);
170b063a422SScott Long *p = f->head;
171b063a422SScott Long f->head = p;
172b063a422SScott Long }
173b063a422SScott Long }
174b063a422SScott Long
175b063a422SScott Long for (f=vbus_ext->freelist_dma_head; f; f=f->next) {
176b063a422SScott Long int order, size, j;
177b063a422SScott Long
178b063a422SScott Long HPT_ASSERT((f->size & (f->alignment-1))==0);
179b063a422SScott Long
180afbd1372SSean Bruno for (order=0, size=PAGE_SIZE; size<f->size; order++, size<<=1)
181afbd1372SSean Bruno ;
182b063a422SScott Long
183b063a422SScott Long KdPrint(("%s: %d*%d=%d bytes, order %d",
184b063a422SScott Long f->tag, f->count, f->size, f->count*f->size, order));
185b063a422SScott Long HPT_ASSERT(f->alignment<=PAGE_SIZE);
186b063a422SScott Long
187b063a422SScott Long for (i=0; i<f->count;) {
188b063a422SScott Long p = (void **)__get_free_pages(order);
189b063a422SScott Long if (!p) return -1;
190b063a422SScott Long for (j = size/f->size; j && i<f->count; i++,j--) {
191b063a422SScott Long *p = f->head;
192b063a422SScott Long *(BUS_ADDRESS *)(p+1) = (BUS_ADDRESS)vtophys(p);
193b063a422SScott Long f->head = p;
194b063a422SScott Long p = (void **)((unsigned long)p + f->size);
195b063a422SScott Long }
196b063a422SScott Long }
197b063a422SScott Long }
198b063a422SScott Long
199b063a422SScott Long HPT_ASSERT(PAGE_SIZE==DMAPOOL_PAGE_SIZE);
200b063a422SScott Long
201b063a422SScott Long for (i=0; i<os_max_cache_pages; i++) {
202b063a422SScott Long p = (void **)__get_free_pages(0);
203b063a422SScott Long if (!p) return -1;
204b063a422SScott Long HPT_ASSERT(((HPT_UPTR)p & (DMAPOOL_PAGE_SIZE-1))==0);
205b063a422SScott Long dmapool_put_page((PVBUS)vbus_ext->vbus, p, (BUS_ADDRESS)vtophys(p));
206b063a422SScott Long }
207b063a422SScott Long
208b063a422SScott Long return 0;
209b063a422SScott Long }
210b063a422SScott Long
hpt_free_mem(PVBUS_EXT vbus_ext)211b063a422SScott Long static void hpt_free_mem(PVBUS_EXT vbus_ext)
212b063a422SScott Long {
213b063a422SScott Long struct freelist *f;
214b063a422SScott Long void *p;
215b063a422SScott Long int i;
216b063a422SScott Long BUS_ADDRESS bus;
217b063a422SScott Long
218b063a422SScott Long for (f=vbus_ext->freelist_head; f; f=f->next) {
2199d6a74ebSScott Long #if DBG
220b063a422SScott Long if (f->count!=f->reserved_count) {
221b063a422SScott Long KdPrint(("memory leak for freelist %s (%d/%d)", f->tag, f->count, f->reserved_count));
222b063a422SScott Long }
223b063a422SScott Long #endif
224b063a422SScott Long while ((p=freelist_get(f)))
225b063a422SScott Long free(p, M_DEVBUF);
226b063a422SScott Long }
227b063a422SScott Long
228b063a422SScott Long for (i=0; i<os_max_cache_pages; i++) {
229b063a422SScott Long p = dmapool_get_page((PVBUS)vbus_ext->vbus, &bus);
230b063a422SScott Long HPT_ASSERT(p);
231d1bdc282SBjoern A. Zeeb free_pages(p);
232b063a422SScott Long }
233b063a422SScott Long
234b063a422SScott Long for (f=vbus_ext->freelist_dma_head; f; f=f->next) {
235b063a422SScott Long int order, size;
2369d6a74ebSScott Long #if DBG
237b063a422SScott Long if (f->count!=f->reserved_count) {
238b063a422SScott Long KdPrint(("memory leak for dma freelist %s (%d/%d)", f->tag, f->count, f->reserved_count));
239b063a422SScott Long }
240b063a422SScott Long #endif
241b063a422SScott Long for (order=0, size=PAGE_SIZE; size<f->size; order++, size<<=1) ;
242b063a422SScott Long
243b063a422SScott Long while ((p=freelist_get_dma(f, &bus))) {
244b063a422SScott Long if (order)
245d1bdc282SBjoern A. Zeeb free_pages(p);
246b063a422SScott Long else {
247b063a422SScott Long /* can't free immediately since other blocks in this page may still be in the list */
248b063a422SScott Long if (((HPT_UPTR)p & (PAGE_SIZE-1))==0)
249b063a422SScott Long dmapool_put_page((PVBUS)vbus_ext->vbus, p, bus);
250b063a422SScott Long }
251b063a422SScott Long }
252b063a422SScott Long }
253b063a422SScott Long
254b063a422SScott Long while ((p = dmapool_get_page((PVBUS)vbus_ext->vbus, &bus)))
255d1bdc282SBjoern A. Zeeb free_pages(p);
256b063a422SScott Long }
257b063a422SScott Long
hpt_init_vbus(PVBUS_EXT vbus_ext)258b063a422SScott Long static int hpt_init_vbus(PVBUS_EXT vbus_ext)
259b063a422SScott Long {
260b063a422SScott Long PHBA hba;
261b063a422SScott Long
262b063a422SScott Long for (hba = vbus_ext->hba_list; hba; hba = hba->next)
263b063a422SScott Long if (!hba->ldm_adapter.him->initialize(hba->ldm_adapter.him_handle)) {
264b063a422SScott Long KdPrint(("fail to initialize %p", hba));
265b063a422SScott Long return -1;
266b063a422SScott Long }
267b063a422SScott Long
268b063a422SScott Long ldm_initialize_vbus((PVBUS)vbus_ext->vbus, &vbus_ext->hba_list->ldm_adapter);
269b063a422SScott Long return 0;
270b063a422SScott Long }
271b063a422SScott Long
hpt_flush_done(PCOMMAND pCmd)272b063a422SScott Long static void hpt_flush_done(PCOMMAND pCmd)
273b063a422SScott Long {
274b063a422SScott Long PVDEV vd = pCmd->target;
275b063a422SScott Long
2764fdb276aSScott Long if (mIsArray(vd->type) && vd->u.array.transform && vd!=vd->u.array.transform->target) {
277b063a422SScott Long vd = vd->u.array.transform->target;
278b063a422SScott Long HPT_ASSERT(vd);
279b063a422SScott Long pCmd->target = vd;
280b063a422SScott Long pCmd->Result = RETURN_PENDING;
281b063a422SScott Long vdev_queue_cmd(pCmd);
282b063a422SScott Long return;
283b063a422SScott Long }
284b063a422SScott Long
285b063a422SScott Long *(int *)pCmd->priv = 1;
286b063a422SScott Long wakeup(pCmd);
287b063a422SScott Long }
288b063a422SScott Long
289b063a422SScott Long /*
290b063a422SScott Long * flush a vdev (without retry).
291b063a422SScott Long */
hpt_flush_vdev(PVBUS_EXT vbus_ext,PVDEV vd)292b063a422SScott Long static int hpt_flush_vdev(PVBUS_EXT vbus_ext, PVDEV vd)
293b063a422SScott Long {
294b063a422SScott Long PCOMMAND pCmd;
295b063a422SScott Long int result = 0, done;
296b063a422SScott Long HPT_UINT count;
297b063a422SScott Long
298b063a422SScott Long KdPrint(("flusing dev %p", vd));
299b063a422SScott Long
30004584935SJohn Baldwin hpt_assert_vbus_locked(vbus_ext);
301b063a422SScott Long
3024fdb276aSScott Long if (mIsArray(vd->type) && vd->u.array.transform)
303a0dedc30SPedro F. Giffuni count = max(vd->u.array.transform->source->cmds_per_request,
304b063a422SScott Long vd->u.array.transform->target->cmds_per_request);
305b063a422SScott Long else
306b063a422SScott Long count = vd->cmds_per_request;
307b063a422SScott Long
308b063a422SScott Long pCmd = ldm_alloc_cmds(vd->vbus, count);
309b063a422SScott Long
310b063a422SScott Long if (!pCmd) {
311b063a422SScott Long return -1;
312b063a422SScott Long }
313b063a422SScott Long
314b063a422SScott Long pCmd->type = CMD_TYPE_FLUSH;
315b063a422SScott Long pCmd->flags.hard_flush = 1;
316b063a422SScott Long pCmd->target = vd;
317b063a422SScott Long pCmd->done = hpt_flush_done;
318b063a422SScott Long done = 0;
319b063a422SScott Long pCmd->priv = &done;
320b063a422SScott Long
321b063a422SScott Long ldm_queue_cmd(pCmd);
322b063a422SScott Long
323b063a422SScott Long if (!done) {
324b063a422SScott Long while (hpt_sleep(vbus_ext, pCmd, PPAUSE, "hptfls", HPT_OSM_TIMEOUT)) {
325b063a422SScott Long ldm_reset_vbus(vd->vbus);
326b063a422SScott Long }
327b063a422SScott Long }
328b063a422SScott Long
329b063a422SScott Long KdPrint(("flush result %d", pCmd->Result));
330b063a422SScott Long
331b063a422SScott Long if (pCmd->Result!=RETURN_SUCCESS)
332b063a422SScott Long result = -1;
333b063a422SScott Long
334b063a422SScott Long ldm_free_cmds(pCmd);
335b063a422SScott Long
336b063a422SScott Long return result;
337b063a422SScott Long }
338b063a422SScott Long
339b063a422SScott Long static void hpt_stop_tasks(PVBUS_EXT vbus_ext);
hpt_shutdown_vbus(PVBUS_EXT vbus_ext,int howto)340b063a422SScott Long static void hpt_shutdown_vbus(PVBUS_EXT vbus_ext, int howto)
341b063a422SScott Long {
342b063a422SScott Long PVBUS vbus = (PVBUS)vbus_ext->vbus;
343b063a422SScott Long PHBA hba;
344b063a422SScott Long int i;
345b063a422SScott Long
346b063a422SScott Long KdPrint(("hpt_shutdown_vbus"));
347b063a422SScott Long
348b063a422SScott Long /* stop all ctl tasks and disable the worker taskqueue */
349b063a422SScott Long hpt_stop_tasks(vbus_ext);
35004584935SJohn Baldwin hpt_lock_vbus(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 ldm_shutdown(vbus);
364b063a422SScott Long hpt_unlock_vbus(vbus_ext);
365b063a422SScott Long
366b063a422SScott Long ldm_release_vbus(vbus);
367b063a422SScott Long
368b063a422SScott Long for (hba=vbus_ext->hba_list; hba; hba=hba->next)
369b063a422SScott Long bus_teardown_intr(hba->pcidev, hba->irq_res, hba->irq_handle);
370b063a422SScott Long
371b063a422SScott Long hpt_free_mem(vbus_ext);
372b063a422SScott Long
373b063a422SScott Long while ((hba=vbus_ext->hba_list)) {
374b063a422SScott Long vbus_ext->hba_list = hba->next;
375b063a422SScott Long free(hba->ldm_adapter.him_handle, M_DEVBUF);
376b063a422SScott Long }
377b063a422SScott Long
37804584935SJohn Baldwin callout_drain(&vbus_ext->timer);
37904584935SJohn Baldwin mtx_destroy(&vbus_ext->lock);
380b063a422SScott Long free(vbus_ext, M_DEVBUF);
381b063a422SScott Long KdPrint(("hpt_shutdown_vbus done"));
382b063a422SScott Long }
383b063a422SScott Long
__hpt_do_tasks(PVBUS_EXT vbus_ext)384b063a422SScott Long static void __hpt_do_tasks(PVBUS_EXT vbus_ext)
385b063a422SScott Long {
386b063a422SScott Long OSM_TASK *tasks;
387b063a422SScott Long
388b063a422SScott Long tasks = vbus_ext->tasks;
389b063a422SScott Long vbus_ext->tasks = 0;
390b063a422SScott Long
391b063a422SScott Long while (tasks) {
392b063a422SScott Long OSM_TASK *t = tasks;
393b063a422SScott Long tasks = t->next;
394b063a422SScott Long t->next = 0;
395b063a422SScott Long t->func(vbus_ext->vbus, t->data);
396b063a422SScott Long }
397b063a422SScott Long }
398b063a422SScott Long
hpt_do_tasks(PVBUS_EXT vbus_ext,int pending)399b063a422SScott Long static void hpt_do_tasks(PVBUS_EXT vbus_ext, int pending)
400b063a422SScott Long {
401b063a422SScott Long if(vbus_ext){
402b063a422SScott Long hpt_lock_vbus(vbus_ext);
403b063a422SScott Long __hpt_do_tasks(vbus_ext);
404b063a422SScott Long hpt_unlock_vbus(vbus_ext);
405b063a422SScott Long }
406b063a422SScott Long }
407b063a422SScott Long
408b063a422SScott Long static void hpt_action(struct cam_sim *sim, union ccb *ccb);
409b063a422SScott Long static void hpt_poll(struct cam_sim *sim);
410b063a422SScott Long static void hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, void * arg);
411b063a422SScott Long static void hpt_pci_intr(void *arg);
412b063a422SScott Long
cmdext_get(PVBUS_EXT vbus_ext)413b063a422SScott Long static __inline POS_CMDEXT cmdext_get(PVBUS_EXT vbus_ext)
414b063a422SScott Long {
415b063a422SScott Long POS_CMDEXT p = vbus_ext->cmdext_list;
416b063a422SScott Long if (p)
417b063a422SScott Long vbus_ext->cmdext_list = p->next;
418b063a422SScott Long return p;
419b063a422SScott Long }
420b063a422SScott Long
cmdext_put(POS_CMDEXT p)421b063a422SScott Long static __inline void cmdext_put(POS_CMDEXT p)
422b063a422SScott Long {
423b063a422SScott Long p->next = p->vbus_ext->cmdext_list;
424b063a422SScott Long p->vbus_ext->cmdext_list = p;
425b063a422SScott Long }
426b063a422SScott Long
hpt_timeout(void * arg)427b063a422SScott Long static void hpt_timeout(void *arg)
428b063a422SScott Long {
429b063a422SScott Long PCOMMAND pCmd = (PCOMMAND)arg;
430b063a422SScott Long POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
431b063a422SScott Long
432b063a422SScott Long KdPrint(("pCmd %p timeout", pCmd));
433b063a422SScott Long
434b063a422SScott Long ldm_reset_vbus((PVBUS)ext->vbus_ext->vbus);
435b063a422SScott Long }
436b063a422SScott Long
os_cmddone(PCOMMAND pCmd)437b063a422SScott Long static void os_cmddone(PCOMMAND pCmd)
438b063a422SScott Long {
439b063a422SScott Long POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
440b063a422SScott Long union ccb *ccb = ext->ccb;
441b063a422SScott Long
442b063a422SScott Long KdPrint(("os_cmddone(%p, %d)", pCmd, pCmd->Result));
443b063a422SScott Long
44404584935SJohn Baldwin callout_stop(&ext->timeout);
445b063a422SScott Long
446b063a422SScott Long switch(pCmd->Result) {
447b063a422SScott Long case RETURN_SUCCESS:
448b063a422SScott Long ccb->ccb_h.status = CAM_REQ_CMP;
449b063a422SScott Long break;
450b063a422SScott Long case RETURN_BAD_DEVICE:
451b063a422SScott Long ccb->ccb_h.status = CAM_DEV_NOT_THERE;
452b063a422SScott Long break;
453b063a422SScott Long case RETURN_DEVICE_BUSY:
454b063a422SScott Long ccb->ccb_h.status = CAM_BUSY;
455b063a422SScott Long break;
456b063a422SScott Long case RETURN_INVALID_REQUEST:
457b063a422SScott Long ccb->ccb_h.status = CAM_REQ_INVALID;
458b063a422SScott Long break;
459b063a422SScott Long case RETURN_SELECTION_TIMEOUT:
460b063a422SScott Long ccb->ccb_h.status = CAM_SEL_TIMEOUT;
461b063a422SScott Long break;
462b063a422SScott Long case RETURN_RETRY:
463b063a422SScott Long ccb->ccb_h.status = CAM_BUSY;
464b063a422SScott Long break;
465b063a422SScott Long default:
466*f52154a2SWarner Losh ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
467b063a422SScott Long break;
468b063a422SScott Long }
469b063a422SScott Long
470b063a422SScott Long if (pCmd->flags.data_in) {
471b063a422SScott Long bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_POSTREAD);
472b063a422SScott Long }
473b063a422SScott Long else if (pCmd->flags.data_out) {
474b063a422SScott Long bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map, BUS_DMASYNC_POSTWRITE);
475b063a422SScott Long }
476b063a422SScott Long
477b063a422SScott Long bus_dmamap_unload(ext->vbus_ext->io_dmat, ext->dma_map);
478b063a422SScott Long
479b063a422SScott Long cmdext_put(ext);
480b063a422SScott Long ldm_free_cmds(pCmd);
481b063a422SScott Long xpt_done(ccb);
482b063a422SScott Long }
483b063a422SScott Long
os_buildsgl(PCOMMAND pCmd,PSG pSg,int logical)484b063a422SScott Long static int os_buildsgl(PCOMMAND pCmd, PSG pSg, int logical)
485b063a422SScott Long {
4867634a04bSXin LI POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
4877634a04bSXin LI union ccb *ccb = ext->ccb;
4887634a04bSXin LI
4897634a04bSXin LI if (logical) {
4907634a04bSXin LI os_set_sgptr(pSg, (HPT_U8 *)ccb->csio.data_ptr);
4917634a04bSXin LI pSg->size = ccb->csio.dxfer_len;
4927634a04bSXin LI pSg->eot = 1;
4937634a04bSXin LI return TRUE;
4947634a04bSXin LI }
495b063a422SScott Long
496b063a422SScott Long /* since we have provided physical sg, nobody will ask us to build physical sg */
497b063a422SScott Long HPT_ASSERT(0);
498b063a422SScott Long return FALSE;
499b063a422SScott Long }
500b063a422SScott Long
hpt_io_dmamap_callback(void * arg,bus_dma_segment_t * segs,int nsegs,int error)501b063a422SScott Long static void hpt_io_dmamap_callback(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
502b063a422SScott Long {
503b063a422SScott Long PCOMMAND pCmd = (PCOMMAND)arg;
504b063a422SScott Long POS_CMDEXT ext = (POS_CMDEXT)pCmd->priv;
505b063a422SScott Long PSG psg = pCmd->psg;
506b063a422SScott Long int idx;
507b063a422SScott Long
508b063a422SScott Long HPT_ASSERT(pCmd->flags.physical_sg);
509b063a422SScott Long
510dd0b4fb6SKonstantin Belousov if (error)
511b063a422SScott Long panic("busdma error");
512b063a422SScott Long
513b063a422SScott Long HPT_ASSERT(nsegs<=os_max_sg_descriptors);
514b063a422SScott Long
515dd0b4fb6SKonstantin Belousov if (nsegs != 0) {
516b063a422SScott Long for (idx = 0; idx < nsegs; idx++, psg++) {
517b063a422SScott Long psg->addr.bus = segs[idx].ds_addr;
518b063a422SScott Long psg->size = segs[idx].ds_len;
519b063a422SScott Long psg->eot = 0;
520b063a422SScott Long }
521b063a422SScott Long psg[-1].eot = 1;
522b063a422SScott Long
523b063a422SScott Long if (pCmd->flags.data_in) {
524dd0b4fb6SKonstantin Belousov bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map,
525dd0b4fb6SKonstantin Belousov BUS_DMASYNC_PREREAD);
526b063a422SScott Long }
527b063a422SScott Long else if (pCmd->flags.data_out) {
528dd0b4fb6SKonstantin Belousov bus_dmamap_sync(ext->vbus_ext->io_dmat, ext->dma_map,
529dd0b4fb6SKonstantin Belousov BUS_DMASYNC_PREWRITE);
530b063a422SScott Long }
531dd0b4fb6SKonstantin Belousov }
53204584935SJohn Baldwin callout_reset(&ext->timeout, HPT_OSM_TIMEOUT, hpt_timeout, pCmd);
533b063a422SScott Long ldm_queue_cmd(pCmd);
534b063a422SScott Long }
535b063a422SScott Long
hpt_scsi_io(PVBUS_EXT vbus_ext,union ccb * ccb)536b063a422SScott Long static void hpt_scsi_io(PVBUS_EXT vbus_ext, union ccb *ccb)
537b063a422SScott Long {
538b063a422SScott Long PVBUS vbus = (PVBUS)vbus_ext->vbus;
539b063a422SScott Long PVDEV vd;
540b063a422SScott Long PCOMMAND pCmd;
541b063a422SScott Long POS_CMDEXT ext;
542b063a422SScott Long HPT_U8 *cdb;
543b063a422SScott Long
544b063a422SScott Long if (ccb->ccb_h.flags & CAM_CDB_POINTER)
545b063a422SScott Long cdb = ccb->csio.cdb_io.cdb_ptr;
546b063a422SScott Long else
547b063a422SScott Long cdb = ccb->csio.cdb_io.cdb_bytes;
548b063a422SScott Long
549b063a422SScott Long KdPrint(("hpt_scsi_io: ccb %x id %d lun %d cdb %x-%x-%x",
550b063a422SScott Long ccb,
551b063a422SScott Long ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
552b063a422SScott Long *(HPT_U32 *)&cdb[0], *(HPT_U32 *)&cdb[4], *(HPT_U32 *)&cdb[8]
553b063a422SScott Long ));
554b063a422SScott Long
555b063a422SScott Long /* ccb->ccb_h.path_id is not our bus id - don't check it */
556b063a422SScott Long if (ccb->ccb_h.target_lun != 0 ||
557b063a422SScott Long ccb->ccb_h.target_id >= osm_max_targets ||
558b063a422SScott Long (ccb->ccb_h.flags & CAM_CDB_PHYS))
559b063a422SScott Long {
560*f52154a2SWarner Losh ccb->ccb_h.status = CAM_SEL_TIMEOUT;
561b063a422SScott Long xpt_done(ccb);
562b063a422SScott Long return;
563b063a422SScott Long }
564b063a422SScott Long
565b063a422SScott Long vd = ldm_find_target(vbus, ccb->ccb_h.target_id);
566b063a422SScott Long
567b063a422SScott Long if (!vd) {
5687634a04bSXin LI ccb->ccb_h.status = CAM_SEL_TIMEOUT;
569b063a422SScott Long xpt_done(ccb);
570b063a422SScott Long return;
571b063a422SScott Long }
572b063a422SScott Long
573b063a422SScott Long switch (cdb[0]) {
574b063a422SScott Long case TEST_UNIT_READY:
575b063a422SScott Long case START_STOP_UNIT:
576b063a422SScott Long case SYNCHRONIZE_CACHE:
577b063a422SScott Long ccb->ccb_h.status = CAM_REQ_CMP;
578b063a422SScott Long break;
579b063a422SScott Long
580b063a422SScott Long case INQUIRY:
581b063a422SScott Long {
582b063a422SScott Long PINQUIRYDATA inquiryData;
583b063a422SScott Long memset(ccb->csio.data_ptr, 0, ccb->csio.dxfer_len);
584b063a422SScott Long inquiryData = (PINQUIRYDATA)ccb->csio.data_ptr;
585b063a422SScott Long
586b063a422SScott Long inquiryData->AdditionalLength = 31;
587b063a422SScott Long inquiryData->CommandQueue = 1;
588b063a422SScott Long memcpy(&inquiryData->VendorId, "HPT ", 8);
589b063a422SScott Long memcpy(&inquiryData->ProductId, "DISK 0_0 ", 16);
590b063a422SScott Long
591b063a422SScott Long if (vd->target_id / 10) {
592b063a422SScott Long inquiryData->ProductId[7] = (vd->target_id % 100) / 10 + '0';
593b063a422SScott Long inquiryData->ProductId[8] = (vd->target_id % 100) % 10 + '0';
594b063a422SScott Long }
595b063a422SScott Long else
596b063a422SScott Long inquiryData->ProductId[7] = (vd->target_id % 100) % 10 + '0';
597b063a422SScott Long
598b063a422SScott Long memcpy(&inquiryData->ProductRevisionLevel, "4.00", 4);
599b063a422SScott Long
600b063a422SScott Long ccb->ccb_h.status = CAM_REQ_CMP;
601b063a422SScott Long }
602b063a422SScott Long break;
603b063a422SScott Long
604b063a422SScott Long case READ_CAPACITY:
605b063a422SScott Long {
606b063a422SScott Long HPT_U8 *rbuf = ccb->csio.data_ptr;
607b063a422SScott Long HPT_U32 cap;
608b063a422SScott Long
609b063a422SScott Long if (vd->capacity>0xfffffffful)
610b063a422SScott Long cap = 0xfffffffful;
611b063a422SScott Long else
612b063a422SScott Long cap = vd->capacity - 1;
613b063a422SScott Long
614b063a422SScott Long rbuf[0] = (HPT_U8)(cap>>24);
615b063a422SScott Long rbuf[1] = (HPT_U8)(cap>>16);
616b063a422SScott Long rbuf[2] = (HPT_U8)(cap>>8);
617b063a422SScott Long rbuf[3] = (HPT_U8)cap;
618b063a422SScott Long rbuf[4] = 0;
619b063a422SScott Long rbuf[5] = 0;
620b063a422SScott Long rbuf[6] = 2;
621b063a422SScott Long rbuf[7] = 0;
622b063a422SScott Long
623b063a422SScott Long ccb->ccb_h.status = CAM_REQ_CMP;
624b063a422SScott Long break;
625b063a422SScott Long }
626b063a422SScott Long
627b063a422SScott Long case SERVICE_ACTION_IN:
628b063a422SScott Long {
629b063a422SScott Long HPT_U8 *rbuf = ccb->csio.data_ptr;
630b063a422SScott Long HPT_U64 cap = vd->capacity - 1;
631b063a422SScott Long
632b063a422SScott Long rbuf[0] = (HPT_U8)(cap>>56);
633b063a422SScott Long rbuf[1] = (HPT_U8)(cap>>48);
634b063a422SScott Long rbuf[2] = (HPT_U8)(cap>>40);
635b063a422SScott Long rbuf[3] = (HPT_U8)(cap>>32);
636b063a422SScott Long rbuf[4] = (HPT_U8)(cap>>24);
637b063a422SScott Long rbuf[5] = (HPT_U8)(cap>>16);
638b063a422SScott Long rbuf[6] = (HPT_U8)(cap>>8);
639b063a422SScott Long rbuf[7] = (HPT_U8)cap;
640b063a422SScott Long rbuf[8] = 0;
641b063a422SScott Long rbuf[9] = 0;
642b063a422SScott Long rbuf[10] = 2;
643b063a422SScott Long rbuf[11] = 0;
644b063a422SScott Long
645b063a422SScott Long ccb->ccb_h.status = CAM_REQ_CMP;
646b063a422SScott Long break;
647b063a422SScott Long }
648b063a422SScott Long
649b063a422SScott Long case READ_6:
650b063a422SScott Long case READ_10:
651b063a422SScott Long case READ_16:
652b063a422SScott Long case WRITE_6:
653b063a422SScott Long case WRITE_10:
654b063a422SScott Long case WRITE_16:
655b063a422SScott Long case 0x13:
656b063a422SScott Long case 0x2f:
657b063a422SScott Long {
658dd0b4fb6SKonstantin Belousov int error;
659dd0b4fb6SKonstantin Belousov
660b063a422SScott Long pCmd = ldm_alloc_cmds(vbus, vd->cmds_per_request);
661b063a422SScott Long if(!pCmd){
662b063a422SScott Long KdPrint(("Failed to allocate command!"));
663b063a422SScott Long ccb->ccb_h.status = CAM_BUSY;
664b063a422SScott Long break;
665b063a422SScott Long }
666b063a422SScott Long
667b063a422SScott Long switch (cdb[0]) {
668b063a422SScott Long case READ_6:
669b063a422SScott Long case WRITE_6:
670b063a422SScott Long case 0x13:
671b063a422SScott Long pCmd->uCmd.Ide.Lba = ((HPT_U32)cdb[1] << 16) | ((HPT_U32)cdb[2] << 8) | (HPT_U32)cdb[3];
672b063a422SScott Long pCmd->uCmd.Ide.nSectors = (HPT_U16) cdb[4];
673b063a422SScott Long break;
674b063a422SScott Long case READ_16:
675b063a422SScott Long case WRITE_16:
676b063a422SScott Long {
677b063a422SScott Long HPT_U64 block =
678b063a422SScott Long ((HPT_U64)cdb[2]<<56) |
679b063a422SScott Long ((HPT_U64)cdb[3]<<48) |
680b063a422SScott Long ((HPT_U64)cdb[4]<<40) |
681b063a422SScott Long ((HPT_U64)cdb[5]<<32) |
682b063a422SScott Long ((HPT_U64)cdb[6]<<24) |
683b063a422SScott Long ((HPT_U64)cdb[7]<<16) |
684b063a422SScott Long ((HPT_U64)cdb[8]<<8) |
685b063a422SScott Long ((HPT_U64)cdb[9]);
686b063a422SScott Long pCmd->uCmd.Ide.Lba = block;
687b063a422SScott Long pCmd->uCmd.Ide.nSectors = (HPT_U16)cdb[13] | ((HPT_U16)cdb[12]<<8);
688b063a422SScott Long break;
689b063a422SScott Long }
690b063a422SScott Long
691b063a422SScott Long default:
692b063a422SScott Long pCmd->uCmd.Ide.Lba = (HPT_U32)cdb[5] | ((HPT_U32)cdb[4] << 8) | ((HPT_U32)cdb[3] << 16) | ((HPT_U32)cdb[2] << 24);
693b063a422SScott Long pCmd->uCmd.Ide.nSectors = (HPT_U16) cdb[8] | ((HPT_U16)cdb[7]<<8);
694b063a422SScott Long break;
695b063a422SScott Long }
696b063a422SScott Long
697b063a422SScott Long switch (cdb[0]) {
698b063a422SScott Long case READ_6:
699b063a422SScott Long case READ_10:
700b063a422SScott Long case READ_16:
701b063a422SScott Long pCmd->flags.data_in = 1;
702b063a422SScott Long break;
703b063a422SScott Long case WRITE_6:
704b063a422SScott Long case WRITE_10:
705b063a422SScott Long case WRITE_16:
706b063a422SScott Long pCmd->flags.data_out = 1;
707b063a422SScott Long break;
708b063a422SScott Long }
709b063a422SScott Long pCmd->priv = ext = cmdext_get(vbus_ext);
710b063a422SScott Long HPT_ASSERT(ext);
711b063a422SScott Long ext->ccb = ccb;
712b063a422SScott Long pCmd->target = vd;
713b063a422SScott Long pCmd->done = os_cmddone;
714b063a422SScott Long pCmd->buildsgl = os_buildsgl;
715b063a422SScott Long pCmd->psg = ext->psg;
716b063a422SScott Long pCmd->flags.physical_sg = 1;
717dd0b4fb6SKonstantin Belousov error = bus_dmamap_load_ccb(vbus_ext->io_dmat,
718b063a422SScott Long ext->dma_map,
719dd0b4fb6SKonstantin Belousov ccb,
720b063a422SScott Long hpt_io_dmamap_callback, pCmd,
721b063a422SScott Long BUS_DMA_WAITOK
722b063a422SScott Long );
723b063a422SScott Long KdPrint(("bus_dmamap_load return %d", error));
724b063a422SScott Long if (error && error!=EINPROGRESS) {
725b063a422SScott Long os_printk("bus_dmamap_load error %d", error);
726b063a422SScott Long cmdext_put(ext);
727b063a422SScott Long ldm_free_cmds(pCmd);
728b063a422SScott Long ccb->ccb_h.status = CAM_REQ_CMP_ERR;
729b063a422SScott Long xpt_done(ccb);
730b063a422SScott Long }
731b063a422SScott Long return;
732b063a422SScott Long }
733b063a422SScott Long
734b063a422SScott Long default:
735b063a422SScott Long ccb->ccb_h.status = CAM_REQ_INVALID;
736b063a422SScott Long break;
737b063a422SScott Long }
738b063a422SScott Long
739b063a422SScott Long xpt_done(ccb);
740b063a422SScott Long return;
741b063a422SScott Long }
742b063a422SScott Long
hpt_action(struct cam_sim * sim,union ccb * ccb)743b063a422SScott Long static void hpt_action(struct cam_sim *sim, union ccb *ccb)
744b063a422SScott Long {
745b063a422SScott Long PVBUS_EXT vbus_ext = (PVBUS_EXT)cam_sim_softc(sim);
746b063a422SScott Long
747b063a422SScott Long KdPrint(("hpt_action(fn=%d, id=%d)", ccb->ccb_h.func_code, ccb->ccb_h.target_id));
748b063a422SScott Long
74904584935SJohn Baldwin hpt_assert_vbus_locked(vbus_ext);
750b063a422SScott Long switch (ccb->ccb_h.func_code) {
751b063a422SScott Long
752b063a422SScott Long case XPT_SCSI_IO:
753b063a422SScott Long hpt_scsi_io(vbus_ext, ccb);
754b063a422SScott Long return;
755b063a422SScott Long
756b063a422SScott Long case XPT_RESET_BUS:
757b063a422SScott Long ldm_reset_vbus((PVBUS)vbus_ext->vbus);
758b063a422SScott Long break;
759b063a422SScott Long
760b063a422SScott Long case XPT_GET_TRAN_SETTINGS:
761b063a422SScott Long case XPT_SET_TRAN_SETTINGS:
762b063a422SScott Long ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
763b063a422SScott Long break;
764b063a422SScott Long
765b063a422SScott Long case XPT_CALC_GEOMETRY:
766f3b080e6SMarius Strobl cam_calc_geometry(&ccb->ccg, 1);
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
7854195c7deSAlan Somers strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
7864195c7deSAlan Somers strlcpy(cpi->hba_vid, "HPT ", HBA_IDLEN);
7874195c7deSAlan Somers strlcpy(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
hpt_pci_intr(void * arg)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
hpt_poll(struct cam_sim * sim)813b063a422SScott Long static void hpt_poll(struct cam_sim *sim)
814b063a422SScott Long {
81504584935SJohn Baldwin PVBUS_EXT vbus_ext = cam_sim_softc(sim);
81604584935SJohn Baldwin hpt_assert_vbus_locked(vbus_ext);
81704584935SJohn Baldwin ldm_intr((PVBUS)vbus_ext->vbus);
818b063a422SScott Long }
819b063a422SScott Long
hpt_async(void * callback_arg,u_int32_t code,struct cam_path * path,void * arg)820b063a422SScott Long static void hpt_async(void * callback_arg, u_int32_t code, struct cam_path * path, void * arg)
821b063a422SScott Long {
822b063a422SScott Long KdPrint(("hpt_async"));
823b063a422SScott Long }
824b063a422SScott Long
hpt_shutdown(device_t dev)825b063a422SScott Long static int hpt_shutdown(device_t dev)
826b063a422SScott Long {
827b063a422SScott Long KdPrint(("hpt_shutdown(dev=%p)", dev));
828b063a422SScott Long return 0;
829b063a422SScott Long }
830b063a422SScott Long
hpt_detach(device_t dev)831b063a422SScott Long static int hpt_detach(device_t dev)
832b063a422SScott Long {
833b063a422SScott Long /* we don't allow the driver to be unloaded. */
834b063a422SScott Long return EBUSY;
835b063a422SScott Long }
836b063a422SScott Long
hpt_ioctl_done(struct _IOCTL_ARG * arg)837b063a422SScott Long static void hpt_ioctl_done(struct _IOCTL_ARG *arg)
838b063a422SScott Long {
839b063a422SScott Long arg->ioctl_cmnd = 0;
840b063a422SScott Long wakeup(arg);
841b063a422SScott Long }
842b063a422SScott Long
__hpt_do_ioctl(PVBUS_EXT vbus_ext,IOCTL_ARG * ioctl_args)843b063a422SScott Long static void __hpt_do_ioctl(PVBUS_EXT vbus_ext, IOCTL_ARG *ioctl_args)
844b063a422SScott Long {
845b063a422SScott Long ioctl_args->result = -1;
846b063a422SScott Long ioctl_args->done = hpt_ioctl_done;
847b063a422SScott Long ioctl_args->ioctl_cmnd = (void *)1;
848b063a422SScott Long
849b063a422SScott Long hpt_lock_vbus(vbus_ext);
850b063a422SScott Long ldm_ioctl((PVBUS)vbus_ext->vbus, ioctl_args);
851b063a422SScott Long
852b063a422SScott Long while (ioctl_args->ioctl_cmnd) {
853b063a422SScott Long if (hpt_sleep(vbus_ext, ioctl_args, PPAUSE, "hptctl", HPT_OSM_TIMEOUT)==0)
854b063a422SScott Long break;
855b063a422SScott Long ldm_reset_vbus((PVBUS)vbus_ext->vbus);
856b063a422SScott Long __hpt_do_tasks(vbus_ext);
857b063a422SScott Long }
858b063a422SScott Long
859b063a422SScott Long /* KdPrint(("ioctl %x result %d", ioctl_args->dwIoControlCode, ioctl_args->result)); */
860b063a422SScott Long
861b063a422SScott Long hpt_unlock_vbus(vbus_ext);
862b063a422SScott Long }
863b063a422SScott Long
hpt_do_ioctl(IOCTL_ARG * ioctl_args)864b063a422SScott Long static void hpt_do_ioctl(IOCTL_ARG *ioctl_args)
865b063a422SScott Long {
866b063a422SScott Long PVBUS vbus;
867b063a422SScott Long PVBUS_EXT vbus_ext;
868b063a422SScott Long
869b063a422SScott Long ldm_for_each_vbus(vbus, vbus_ext) {
870b063a422SScott Long __hpt_do_ioctl(vbus_ext, ioctl_args);
871b063a422SScott Long if (ioctl_args->result!=HPT_IOCTL_RESULT_WRONG_VBUS)
872b063a422SScott Long return;
873b063a422SScott Long }
874b063a422SScott Long }
875b063a422SScott Long
876b063a422SScott Long #define HPT_DO_IOCTL(code, inbuf, insize, outbuf, outsize) ({\
877b063a422SScott Long IOCTL_ARG arg;\
878b063a422SScott Long arg.dwIoControlCode = code;\
879b063a422SScott Long arg.lpInBuffer = inbuf;\
880b063a422SScott Long arg.lpOutBuffer = outbuf;\
881b063a422SScott Long arg.nInBufferSize = insize;\
882b063a422SScott Long arg.nOutBufferSize = outsize;\
883b063a422SScott Long arg.lpBytesReturned = 0;\
884b063a422SScott Long hpt_do_ioctl(&arg);\
885b063a422SScott Long arg.result;\
886b063a422SScott Long })
887b063a422SScott Long
888b063a422SScott Long #define DEVICEID_VALID(id) ((id) && ((HPT_U32)(id)!=0xffffffff))
889b063a422SScott Long
hpt_get_logical_devices(DEVICEID * pIds,int nMaxCount)890b063a422SScott Long static int hpt_get_logical_devices(DEVICEID * pIds, int nMaxCount)
891b063a422SScott Long {
892b063a422SScott Long int i;
893b063a422SScott Long HPT_U32 count = nMaxCount-1;
894b063a422SScott Long
895b063a422SScott Long if (HPT_DO_IOCTL(HPT_IOCTL_GET_LOGICAL_DEVICES,
896b063a422SScott Long &count, sizeof(HPT_U32), pIds, sizeof(DEVICEID)*nMaxCount))
897b063a422SScott Long return -1;
898b063a422SScott Long
899b063a422SScott Long nMaxCount = (int)pIds[0];
900b063a422SScott Long for (i=0; i<nMaxCount; i++) pIds[i] = pIds[i+1];
901b063a422SScott Long return nMaxCount;
902b063a422SScott Long }
903b063a422SScott Long
hpt_get_device_info_v3(DEVICEID id,PLOGICAL_DEVICE_INFO_V3 pInfo)904b063a422SScott Long static int hpt_get_device_info_v3(DEVICEID id, PLOGICAL_DEVICE_INFO_V3 pInfo)
905b063a422SScott Long {
906b063a422SScott Long return HPT_DO_IOCTL(HPT_IOCTL_GET_DEVICE_INFO_V3,
907b063a422SScott Long &id, sizeof(DEVICEID), pInfo, sizeof(LOGICAL_DEVICE_INFO_V3));
908b063a422SScott Long }
909b063a422SScott Long
910b063a422SScott Long /* not belong to this file logically, but we want to use ioctl interface */
__hpt_stop_tasks(PVBUS_EXT vbus_ext,DEVICEID id)911b063a422SScott Long static int __hpt_stop_tasks(PVBUS_EXT vbus_ext, DEVICEID id)
912b063a422SScott Long {
913b063a422SScott Long LOGICAL_DEVICE_INFO_V3 devinfo;
914b063a422SScott Long int i, result;
915b063a422SScott Long DEVICEID param[2] = { id, 0 };
916b063a422SScott Long
917b063a422SScott Long if (hpt_get_device_info_v3(id, &devinfo))
918b063a422SScott Long return -1;
919b063a422SScott Long
920b063a422SScott Long if (devinfo.Type!=LDT_ARRAY)
921b063a422SScott Long return -1;
922b063a422SScott Long
923b063a422SScott Long if (devinfo.u.array.Flags & ARRAY_FLAG_REBUILDING)
924b063a422SScott Long param[1] = AS_REBUILD_ABORT;
925b063a422SScott Long else if (devinfo.u.array.Flags & ARRAY_FLAG_VERIFYING)
926b063a422SScott Long param[1] = AS_VERIFY_ABORT;
927b063a422SScott Long else if (devinfo.u.array.Flags & ARRAY_FLAG_INITIALIZING)
928b063a422SScott Long param[1] = AS_INITIALIZE_ABORT;
929b063a422SScott Long else if (devinfo.u.array.Flags & ARRAY_FLAG_TRANSFORMING)
930b063a422SScott Long param[1] = AS_TRANSFORM_ABORT;
931b063a422SScott Long else
932b063a422SScott Long return -1;
933b063a422SScott Long
934b063a422SScott Long KdPrint(("SET_ARRAY_STATE(%x, %d)", param[0], param[1]));
935b063a422SScott Long result = HPT_DO_IOCTL(HPT_IOCTL_SET_ARRAY_STATE,
936b063a422SScott Long param, sizeof(param), 0, 0);
937b063a422SScott Long
938b063a422SScott Long for (i=0; i<devinfo.u.array.nDisk; i++)
939b063a422SScott Long if (DEVICEID_VALID(devinfo.u.array.Members[i]))
940b063a422SScott Long __hpt_stop_tasks(vbus_ext, devinfo.u.array.Members[i]);
941b063a422SScott Long
942b063a422SScott Long return result;
943b063a422SScott Long }
944b063a422SScott Long
hpt_stop_tasks(PVBUS_EXT vbus_ext)945b063a422SScott Long static void hpt_stop_tasks(PVBUS_EXT vbus_ext)
946b063a422SScott Long {
947b063a422SScott Long DEVICEID ids[32];
948b063a422SScott Long int i, count;
949b063a422SScott Long
950b063a422SScott Long count = hpt_get_logical_devices((DEVICEID *)&ids, sizeof(ids)/sizeof(ids[0]));
951b063a422SScott Long
952b063a422SScott Long for (i=0; i<count; i++)
953b063a422SScott Long __hpt_stop_tasks(vbus_ext, ids[i]);
954b063a422SScott Long }
955b063a422SScott Long
956b063a422SScott Long static d_open_t hpt_open;
957b063a422SScott Long static d_close_t hpt_close;
958b063a422SScott Long static d_ioctl_t hpt_ioctl;
959b063a422SScott Long static int hpt_rescan_bus(void);
960b063a422SScott Long
961b063a422SScott Long static struct cdevsw hpt_cdevsw = {
962b063a422SScott Long .d_open = hpt_open,
963b063a422SScott Long .d_close = hpt_close,
964b063a422SScott Long .d_ioctl = hpt_ioctl,
965b063a422SScott Long .d_name = driver_name,
966b063a422SScott Long .d_version = D_VERSION,
967b063a422SScott Long };
968b063a422SScott Long
969b063a422SScott Long static struct intr_config_hook hpt_ich;
970b063a422SScott Long
971b063a422SScott Long /*
972b063a422SScott Long * hpt_final_init will be called after all hpt_attach.
973b063a422SScott Long */
hpt_final_init(void * dummy)974b063a422SScott Long static void hpt_final_init(void *dummy)
975b063a422SScott Long {
976b063a422SScott Long int i;
977b063a422SScott Long PVBUS_EXT vbus_ext;
978b063a422SScott Long PVBUS vbus;
979b063a422SScott Long PHBA hba;
980b063a422SScott Long
981b063a422SScott Long /* Clear the config hook */
982b063a422SScott Long config_intrhook_disestablish(&hpt_ich);
983b063a422SScott Long
984b063a422SScott Long /* allocate memory */
985b063a422SScott Long i = 0;
986b063a422SScott Long ldm_for_each_vbus(vbus, vbus_ext) {
987b063a422SScott Long if (hpt_alloc_mem(vbus_ext)) {
988b063a422SScott Long os_printk("out of memory");
989b063a422SScott Long return;
990b063a422SScott Long }
991b063a422SScott Long i++;
992b063a422SScott Long }
993b063a422SScott Long
994b063a422SScott Long if (!i) {
9959e2211fdSMaxim Sobolev if (bootverbose)
996b063a422SScott Long os_printk("no controller detected.");
997b063a422SScott Long return;
998b063a422SScott Long }
999b063a422SScott Long
1000b063a422SScott Long /* initializing hardware */
1001b063a422SScott Long ldm_for_each_vbus(vbus, vbus_ext) {
1002b063a422SScott Long /* make timer available here */
100304584935SJohn Baldwin mtx_init(&vbus_ext->lock, "hptsleeplock", NULL, MTX_DEF);
100404584935SJohn Baldwin callout_init_mtx(&vbus_ext->timer, &vbus_ext->lock, 0);
1005b063a422SScott Long if (hpt_init_vbus(vbus_ext)) {
1006b063a422SScott Long os_printk("fail to initialize hardware");
1007b063a422SScott Long break; /* FIXME */
1008b063a422SScott Long }
1009b063a422SScott Long }
1010b063a422SScott Long
1011b063a422SScott Long /* register CAM interface */
1012b063a422SScott Long ldm_for_each_vbus(vbus, vbus_ext) {
1013b063a422SScott Long struct cam_devq *devq;
1014b063a422SScott Long struct ccb_setasync ccb;
1015b063a422SScott Long
1016b063a422SScott Long if (bus_dma_tag_create(NULL,/* parent */
1017b063a422SScott Long 4, /* alignment */
1018b063a422SScott Long BUS_SPACE_MAXADDR_32BIT+1, /* boundary */
1019b063a422SScott Long BUS_SPACE_MAXADDR, /* lowaddr */
1020b063a422SScott Long BUS_SPACE_MAXADDR, /* highaddr */
1021b063a422SScott Long NULL, NULL, /* filter, filterarg */
1022b063a422SScott Long PAGE_SIZE * (os_max_sg_descriptors-1), /* maxsize */
1023b063a422SScott Long os_max_sg_descriptors, /* nsegments */
1024b063a422SScott Long 0x10000, /* maxsegsize */
1025b063a422SScott Long BUS_DMA_WAITOK, /* flags */
1026b063a422SScott Long busdma_lock_mutex, /* lockfunc */
1027b063a422SScott Long &vbus_ext->lock, /* lockfuncarg */
1028b063a422SScott Long &vbus_ext->io_dmat /* tag */))
1029b063a422SScott Long {
1030b063a422SScott Long return ;
1031b063a422SScott Long }
1032b063a422SScott Long
1033b063a422SScott Long for (i=0; i<os_max_queue_comm; i++) {
1034b063a422SScott Long POS_CMDEXT ext = (POS_CMDEXT)malloc(sizeof(OS_CMDEXT), M_DEVBUF, M_WAITOK);
1035b063a422SScott Long ext->vbus_ext = vbus_ext;
1036b063a422SScott Long ext->next = vbus_ext->cmdext_list;
1037b063a422SScott Long vbus_ext->cmdext_list = ext;
1038b063a422SScott Long
1039b063a422SScott Long if (bus_dmamap_create(vbus_ext->io_dmat, 0, &ext->dma_map)) {
1040b063a422SScott Long os_printk("Can't create dma map(%d)", i);
1041b063a422SScott Long return ;
1042b063a422SScott Long }
104304584935SJohn Baldwin callout_init_mtx(&ext->timeout, &vbus_ext->lock, 0);
1044b063a422SScott Long }
1045b063a422SScott Long
1046b063a422SScott Long if ((devq = cam_simq_alloc(os_max_queue_comm)) == NULL) {
1047b063a422SScott Long os_printk("cam_simq_alloc failed");
1048b063a422SScott Long return ;
1049b063a422SScott Long }
1050b063a422SScott Long
1051b063a422SScott Long vbus_ext->sim = cam_sim_alloc(hpt_action, hpt_poll, driver_name,
105204584935SJohn Baldwin vbus_ext, 0, &vbus_ext->lock, os_max_queue_comm,
105304584935SJohn Baldwin /*tagged*/8, devq);
1054b063a422SScott Long
1055b063a422SScott Long if (!vbus_ext->sim) {
1056b063a422SScott Long os_printk("cam_sim_alloc failed");
1057b063a422SScott Long cam_simq_free(devq);
1058b063a422SScott Long return ;
1059b063a422SScott Long }
1060b063a422SScott Long
106104584935SJohn Baldwin hpt_lock_vbus(vbus_ext);
1062b063a422SScott Long if (xpt_bus_register(vbus_ext->sim, NULL, 0) != CAM_SUCCESS) {
1063b063a422SScott Long os_printk("xpt_bus_register failed");
1064b063a422SScott Long cam_sim_free(vbus_ext->sim, /*free devq*/ TRUE);
106504584935SJohn Baldwin hpt_unlock_vbus(vbus_ext);
1066b063a422SScott Long vbus_ext->sim = NULL;
1067b063a422SScott Long return ;
1068b063a422SScott Long }
1069b063a422SScott Long
1070b063a422SScott Long if (xpt_create_path(&vbus_ext->path, /*periph */ NULL,
1071b063a422SScott Long cam_sim_path(vbus_ext->sim), CAM_TARGET_WILDCARD,
1072b063a422SScott Long CAM_LUN_WILDCARD) != CAM_REQ_CMP)
1073b063a422SScott Long {
1074b063a422SScott Long os_printk("xpt_create_path failed");
1075b063a422SScott Long xpt_bus_deregister(cam_sim_path(vbus_ext->sim));
1076b063a422SScott Long cam_sim_free(vbus_ext->sim, /*free_devq*/TRUE);
107704584935SJohn Baldwin hpt_unlock_vbus(vbus_ext);
1078b063a422SScott Long vbus_ext->sim = NULL;
1079b063a422SScott Long return ;
1080b063a422SScott Long }
108104584935SJohn Baldwin hpt_unlock_vbus(vbus_ext);
1082b063a422SScott Long
10838dc96b74SEdward Tomasz Napierala memset(&ccb, 0, sizeof(ccb));
1084b063a422SScott Long xpt_setup_ccb(&ccb.ccb_h, vbus_ext->path, /*priority*/5);
1085b063a422SScott Long ccb.ccb_h.func_code = XPT_SASYNC_CB;
1086b063a422SScott Long ccb.event_enable = AC_LOST_DEVICE;
1087b063a422SScott Long ccb.callback = hpt_async;
1088b063a422SScott Long ccb.callback_arg = vbus_ext;
1089b063a422SScott Long xpt_action((union ccb *)&ccb);
1090b063a422SScott Long
1091b063a422SScott Long for (hba = vbus_ext->hba_list; hba; hba = hba->next) {
1092b063a422SScott Long int rid = 0;
109343cd6160SJustin Hibbits if ((hba->irq_res = bus_alloc_resource_any(hba->pcidev,
109443cd6160SJustin Hibbits SYS_RES_IRQ, &rid, RF_SHAREABLE | RF_ACTIVE)) == NULL)
1095b063a422SScott Long {
1096b063a422SScott Long os_printk("can't allocate interrupt");
1097b063a422SScott Long return ;
1098b063a422SScott Long }
1099b063a422SScott Long
110004584935SJohn Baldwin if (bus_setup_intr(hba->pcidev, hba->irq_res, INTR_TYPE_CAM | INTR_MPSAFE,
1101b063a422SScott Long NULL, hpt_pci_intr, vbus_ext, &hba->irq_handle))
1102b063a422SScott Long {
1103b063a422SScott Long os_printk("can't set up interrupt");
1104b063a422SScott Long return ;
1105b063a422SScott Long }
1106b063a422SScott Long hba->ldm_adapter.him->intr_control(hba->ldm_adapter.him_handle, HPT_TRUE);
1107b063a422SScott Long }
1108b063a422SScott Long
1109b063a422SScott Long vbus_ext->shutdown_eh = EVENTHANDLER_REGISTER(shutdown_final,
1110b063a422SScott Long hpt_shutdown_vbus, vbus_ext, SHUTDOWN_PRI_DEFAULT);
1111b063a422SScott Long if (!vbus_ext->shutdown_eh)
1112b063a422SScott Long os_printk("Shutdown event registration failed");
1113b063a422SScott Long }
1114b063a422SScott Long
1115b063a422SScott Long ldm_for_each_vbus(vbus, vbus_ext) {
1116b063a422SScott Long TASK_INIT(&vbus_ext->worker, 0, (task_fn_t *)hpt_do_tasks, vbus_ext);
1117b063a422SScott Long if (vbus_ext->tasks)
1118b063a422SScott Long TASK_ENQUEUE(&vbus_ext->worker);
1119b063a422SScott Long }
1120b063a422SScott Long
1121b063a422SScott Long make_dev(&hpt_cdevsw, DRIVER_MINOR, UID_ROOT, GID_OPERATOR,
112230aa84b7SRui Paulo S_IRUSR | S_IWUSR, "%s", driver_name);
1123b063a422SScott Long }
1124b063a422SScott Long
112504584935SJohn Baldwin #if defined(KLD_MODULE)
1126b063a422SScott Long
1127b063a422SScott Long typedef struct driverlink *driverlink_t;
1128b063a422SScott Long struct driverlink {
1129b063a422SScott Long kobj_class_t driver;
1130b063a422SScott Long TAILQ_ENTRY(driverlink) link; /* list of drivers in devclass */
1131b063a422SScott Long };
1132b063a422SScott Long
1133b063a422SScott Long typedef TAILQ_HEAD(driver_list, driverlink) driver_list_t;
1134b063a422SScott Long
1135b063a422SScott Long struct devclass {
1136b063a422SScott Long TAILQ_ENTRY(devclass) link;
1137b063a422SScott Long devclass_t parent; /* parent in devclass hierarchy */
1138b063a422SScott Long driver_list_t drivers; /* bus devclasses store drivers for bus */
1139b063a422SScott Long char *name;
1140b063a422SScott Long device_t *devices; /* array of devices indexed by unit */
1141b063a422SScott Long int maxunit; /* size of devices array */
1142b063a422SScott Long };
1143b063a422SScott Long
override_kernel_driver(void)1144b063a422SScott Long static void override_kernel_driver(void)
1145b063a422SScott Long {
1146b063a422SScott Long driverlink_t dl, dlfirst;
1147b063a422SScott Long driver_t *tmpdriver;
1148b063a422SScott Long devclass_t dc = devclass_find("pci");
1149b063a422SScott Long
1150b063a422SScott Long if (dc){
1151b063a422SScott Long dlfirst = TAILQ_FIRST(&dc->drivers);
1152b063a422SScott Long for (dl = dlfirst; dl; dl = TAILQ_NEXT(dl, link)) {
1153b063a422SScott Long if(strcmp(dl->driver->name, driver_name) == 0) {
1154b063a422SScott Long tmpdriver=dl->driver;
1155b063a422SScott Long dl->driver=dlfirst->driver;
1156b063a422SScott Long dlfirst->driver=tmpdriver;
1157b063a422SScott Long break;
1158b063a422SScott Long }
1159b063a422SScott Long }
1160b063a422SScott Long }
1161b063a422SScott Long }
1162b063a422SScott Long
1163b063a422SScott Long #else
1164b063a422SScott Long #define override_kernel_driver()
1165b063a422SScott Long #endif
1166b063a422SScott Long
hpt_init(void * dummy)1167b063a422SScott Long static void hpt_init(void *dummy)
1168b063a422SScott Long {
11699e2211fdSMaxim Sobolev if (bootverbose)
1170b063a422SScott Long os_printk("%s %s", driver_name_long, driver_ver);
1171b063a422SScott Long
1172b063a422SScott Long override_kernel_driver();
1173b063a422SScott Long init_config();
1174b063a422SScott Long
1175b063a422SScott Long hpt_ich.ich_func = hpt_final_init;
1176b063a422SScott Long hpt_ich.ich_arg = NULL;
1177b063a422SScott Long if (config_intrhook_establish(&hpt_ich) != 0) {
1178b063a422SScott Long printf("%s: cannot establish configuration hook\n",
1179b063a422SScott Long driver_name_long);
1180b063a422SScott Long }
1181b063a422SScott Long
1182b063a422SScott Long }
1183b063a422SScott Long SYSINIT(hptinit, SI_SUB_CONFIGURE, SI_ORDER_FIRST, hpt_init, NULL);
1184b063a422SScott Long
1185b063a422SScott Long /*
1186b063a422SScott Long * CAM driver interface
1187b063a422SScott Long */
1188b063a422SScott Long static device_method_t driver_methods[] = {
1189b063a422SScott Long /* Device interface */
1190b063a422SScott Long DEVMETHOD(device_probe, hpt_probe),
1191b063a422SScott Long DEVMETHOD(device_attach, hpt_attach),
1192b063a422SScott Long DEVMETHOD(device_detach, hpt_detach),
1193b063a422SScott Long DEVMETHOD(device_shutdown, hpt_shutdown),
1194f3b080e6SMarius Strobl DEVMETHOD_END
1195b063a422SScott Long };
1196b063a422SScott Long
1197b063a422SScott Long static driver_t hpt_pci_driver = {
1198b063a422SScott Long driver_name,
1199b063a422SScott Long driver_methods,
1200b063a422SScott Long sizeof(HBA)
1201b063a422SScott Long };
1202b063a422SScott Long
1203b063a422SScott Long #ifndef TARGETNAME
1204b063a422SScott Long #error "no TARGETNAME found"
1205b063a422SScott Long #endif
1206b063a422SScott Long
1207b063a422SScott Long /* use this to make TARGETNAME be expanded */
12088272dd4bSJohn Baldwin #define __DRIVER_MODULE(p1, p2, p3, p4, p5) DRIVER_MODULE(p1, p2, p3, p4, p5)
1209b063a422SScott Long #define __MODULE_VERSION(p1, p2) MODULE_VERSION(p1, p2)
1210b063a422SScott Long #define __MODULE_DEPEND(p1, p2, p3, p4, p5) MODULE_DEPEND(p1, p2, p3, p4, p5)
12118272dd4bSJohn Baldwin __DRIVER_MODULE(TARGETNAME, pci, hpt_pci_driver, 0, 0);
1212b063a422SScott Long __MODULE_VERSION(TARGETNAME, 1);
1213b063a422SScott Long __MODULE_DEPEND(TARGETNAME, cam, 1, 1, 1);
1214b063a422SScott Long
hpt_open(struct cdev * dev,int flags,int devtype,struct thread * td)121504584935SJohn Baldwin static int hpt_open(struct cdev *dev, int flags, int devtype, struct thread *td)
1216b063a422SScott Long {
1217b063a422SScott Long return 0;
1218b063a422SScott Long }
1219b063a422SScott Long
hpt_close(struct cdev * dev,int flags,int devtype,struct thread * td)122004584935SJohn Baldwin static int hpt_close(struct cdev *dev, int flags, int devtype, struct thread *td)
1221b063a422SScott Long {
1222b063a422SScott Long return 0;
1223b063a422SScott Long }
1224b063a422SScott Long
hpt_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)122504584935SJohn Baldwin static int hpt_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
1226b063a422SScott Long {
1227b063a422SScott Long PHPT_IOCTL_PARAM piop=(PHPT_IOCTL_PARAM)data;
1228b063a422SScott Long IOCTL_ARG ioctl_args;
12295d8501f4SEd Maste HPT_U32 bytesReturned = 0;
1230b063a422SScott Long
1231b063a422SScott Long switch (cmd){
1232b063a422SScott Long case HPT_DO_IOCONTROL:
1233b063a422SScott Long {
1234b063a422SScott Long if (piop->Magic == HPT_IOCTL_MAGIC || piop->Magic == HPT_IOCTL_MAGIC32) {
1235b063a422SScott Long KdPrint(("ioctl=%x in=%p len=%d out=%p len=%d\n",
1236b063a422SScott Long piop->dwIoControlCode,
1237b063a422SScott Long piop->lpInBuffer,
1238b063a422SScott Long piop->nInBufferSize,
1239b063a422SScott Long piop->lpOutBuffer,
1240b063a422SScott Long piop->nOutBufferSize));
1241b063a422SScott Long
1242b063a422SScott Long memset(&ioctl_args, 0, sizeof(ioctl_args));
1243b063a422SScott Long
1244b063a422SScott Long ioctl_args.dwIoControlCode = piop->dwIoControlCode;
1245b063a422SScott Long ioctl_args.nInBufferSize = piop->nInBufferSize;
1246b063a422SScott Long ioctl_args.nOutBufferSize = piop->nOutBufferSize;
1247b063a422SScott Long ioctl_args.lpBytesReturned = &bytesReturned;
1248b063a422SScott Long
1249b063a422SScott Long if (ioctl_args.nInBufferSize) {
1250b063a422SScott Long ioctl_args.lpInBuffer = malloc(ioctl_args.nInBufferSize, M_DEVBUF, M_WAITOK);
1251b063a422SScott Long if (copyin((void*)piop->lpInBuffer,
1252b063a422SScott Long ioctl_args.lpInBuffer, piop->nInBufferSize))
1253b063a422SScott Long goto invalid;
1254b063a422SScott Long }
1255b063a422SScott Long
125692b0370eSZhenlei Huang if (ioctl_args.nOutBufferSize)
12575d8501f4SEd Maste ioctl_args.lpOutBuffer = malloc(ioctl_args.nOutBufferSize, M_DEVBUF, M_WAITOK | M_ZERO);
1258b063a422SScott Long
1259b063a422SScott Long hpt_do_ioctl(&ioctl_args);
1260b063a422SScott Long
1261b063a422SScott Long if (ioctl_args.result==HPT_IOCTL_RESULT_OK) {
1262b063a422SScott Long if (piop->nOutBufferSize) {
1263b063a422SScott Long if (copyout(ioctl_args.lpOutBuffer,
1264b063a422SScott Long (void*)piop->lpOutBuffer, piop->nOutBufferSize))
1265b063a422SScott Long goto invalid;
1266b063a422SScott Long }
1267b063a422SScott Long if (piop->lpBytesReturned) {
1268b063a422SScott Long if (copyout(&bytesReturned,
1269b063a422SScott Long (void*)piop->lpBytesReturned, sizeof(HPT_U32)))
1270b063a422SScott Long goto invalid;
1271b063a422SScott Long }
1272b063a422SScott Long if (ioctl_args.lpInBuffer) free(ioctl_args.lpInBuffer, M_DEVBUF);
1273b063a422SScott Long if (ioctl_args.lpOutBuffer) free(ioctl_args.lpOutBuffer, M_DEVBUF);
1274b063a422SScott Long return 0;
1275b063a422SScott Long }
1276b063a422SScott Long invalid:
1277b063a422SScott Long if (ioctl_args.lpInBuffer) free(ioctl_args.lpInBuffer, M_DEVBUF);
1278b063a422SScott Long if (ioctl_args.lpOutBuffer) free(ioctl_args.lpOutBuffer, M_DEVBUF);
1279b063a422SScott Long return EFAULT;
1280b063a422SScott Long }
1281b063a422SScott Long return EFAULT;
1282b063a422SScott Long }
1283b063a422SScott Long
1284b063a422SScott Long case HPT_SCAN_BUS:
1285b063a422SScott Long {
1286b063a422SScott Long return hpt_rescan_bus();
1287b063a422SScott Long }
1288b063a422SScott Long default:
1289b063a422SScott Long KdPrint(("invalid command!"));
1290b063a422SScott Long return EFAULT;
1291b063a422SScott Long }
1292b063a422SScott Long
1293b063a422SScott Long }
1294b063a422SScott Long
hpt_rescan_bus(void)1295b063a422SScott Long static int hpt_rescan_bus(void)
1296b063a422SScott Long {
1297b063a422SScott Long union ccb *ccb;
1298b063a422SScott Long PVBUS vbus;
1299b063a422SScott Long PVBUS_EXT vbus_ext;
1300b063a422SScott Long
1301b063a422SScott Long ldm_for_each_vbus(vbus, vbus_ext) {
130283c5d981SAlexander Motin if ((ccb = xpt_alloc_ccb()) == NULL)
1303b063a422SScott Long return(ENOMEM);
1304e5dfa058SAlexander Motin if (xpt_create_path(&ccb->ccb_h.path, NULL,
130583c5d981SAlexander Motin cam_sim_path(vbus_ext->sim),
130683c5d981SAlexander Motin CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
130783c5d981SAlexander Motin xpt_free_ccb(ccb);
130883c5d981SAlexander Motin return(EIO);
130983c5d981SAlexander Motin }
131083c5d981SAlexander Motin xpt_rescan(ccb);
1311b063a422SScott Long }
1312b063a422SScott Long
1313b063a422SScott Long return(0);
1314b063a422SScott Long }
1315