xref: /illumos-gate/usr/src/uts/common/io/chxge/glue.c (revision 19397407)
1d39a76e7Sxw /*
2d39a76e7Sxw  * CDDL HEADER START
3d39a76e7Sxw  *
4d39a76e7Sxw  * The contents of this file are subject to the terms of the
5d39a76e7Sxw  * Common Development and Distribution License (the "License").
6d39a76e7Sxw  * You may not use this file except in compliance with the License.
7d39a76e7Sxw  *
8d39a76e7Sxw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9d39a76e7Sxw  * or http://www.opensolaris.org/os/licensing.
10d39a76e7Sxw  * See the License for the specific language governing permissions
11d39a76e7Sxw  * and limitations under the License.
12d39a76e7Sxw  *
13d39a76e7Sxw  * When distributing Covered Code, include this CDDL HEADER in each
14d39a76e7Sxw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15d39a76e7Sxw  * If applicable, add the following below this CDDL HEADER, with the
16d39a76e7Sxw  * fields enclosed by brackets "[]" replaced with your own identifying
17d39a76e7Sxw  * information: Portions Copyright [yyyy] [name of copyright owner]
18d39a76e7Sxw  *
19d39a76e7Sxw  * CDDL HEADER END
20d39a76e7Sxw  */
21d39a76e7Sxw 
22d39a76e7Sxw /*
23*19397407SSherry Moore  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24d39a76e7Sxw  * Use is subject to license terms.
25d39a76e7Sxw  */
26d39a76e7Sxw 
27d39a76e7Sxw /*
28d39a76e7Sxw  * This file is part of the Chelsio T1 Ethernet driver.
29d39a76e7Sxw  *
30d39a76e7Sxw  * Copyright (C) 2003-2005 Chelsio Communications.  All rights reserved.
31d39a76e7Sxw  */
32d39a76e7Sxw 
33d39a76e7Sxw /*
34d39a76e7Sxw  * Solaris support routines for common code part of
35d39a76e7Sxw  * Chelsio PCI Ethernet Driver.
36d39a76e7Sxw  */
37d39a76e7Sxw 
38d39a76e7Sxw #include <sys/types.h>
39d39a76e7Sxw #include <sys/conf.h>
40d39a76e7Sxw #include <sys/stropts.h>
41d39a76e7Sxw #include <sys/stream.h>
42d39a76e7Sxw #include <sys/strlog.h>
43d39a76e7Sxw #include <sys/kmem.h>
44d39a76e7Sxw #include <sys/stat.h>
45d39a76e7Sxw #include <sys/kstat.h>
46d39a76e7Sxw #include <sys/modctl.h>
47d39a76e7Sxw #include <sys/errno.h>
48d39a76e7Sxw #include <sys/varargs.h>
49d39a76e7Sxw #include <sys/ddi.h>
50d39a76e7Sxw #include <sys/sunddi.h>
51d39a76e7Sxw #include <sys/dlpi.h>
52d39a76e7Sxw #include <sys/ethernet.h>
53d39a76e7Sxw #include <sys/strsun.h>
54d39a76e7Sxw #include "ostypes.h"
55d39a76e7Sxw #undef OFFSET
56d39a76e7Sxw #include "common.h"
57d39a76e7Sxw #include <sys/gld.h>
58d39a76e7Sxw #include "oschtoe.h"
59d39a76e7Sxw #include "ch.h"			/* Chelsio Driver specific parameters */
60d39a76e7Sxw #include "sge.h"
61d39a76e7Sxw #include "regs.h"
62d39a76e7Sxw 
63d39a76e7Sxw /*
64d39a76e7Sxw  * Device specific.
65d39a76e7Sxw  */
66d39a76e7Sxw struct pe_reg {
67d39a76e7Sxw 	uint32_t cmd;
68d39a76e7Sxw 	uint32_t addr;
69d39a76e7Sxw 	union {
70d39a76e7Sxw 		uint32_t v32;
71d39a76e7Sxw 		uint64_t v64;
72d39a76e7Sxw 	}vv;
73d39a76e7Sxw 	union {
74d39a76e7Sxw 		uint32_t m32;
75d39a76e7Sxw 		uint64_t m64;
76d39a76e7Sxw 	}mm;
77d39a76e7Sxw };
78d39a76e7Sxw #define	pe_reg_val vv.v32
79d39a76e7Sxw #define	pe_opt_val vv.v64
80d39a76e7Sxw #define	pe_mask32  mm.m32
81d39a76e7Sxw #define	pe_mask64  mm.m64
82d39a76e7Sxw 
83d39a76e7Sxw struct toetool_reg {
84d39a76e7Sxw 	uint32_t cmd;
85d39a76e7Sxw 	uint32_t addr;
86d39a76e7Sxw 	uint32_t val;
87d39a76e7Sxw };
88d39a76e7Sxw 
89d39a76e7Sxw uint32_t
t1_read_reg_4(ch_t * obj,uint32_t reg_val)90d39a76e7Sxw t1_read_reg_4(ch_t *obj, uint32_t reg_val)
91d39a76e7Sxw {
92d39a76e7Sxw 	return (ddi_get32(obj->ch_hbar0, (uint32_t *)(obj->ch_bar0 + reg_val)));
93d39a76e7Sxw }
94d39a76e7Sxw 
95d39a76e7Sxw void
t1_write_reg_4(ch_t * obj,uint32_t reg_val,uint32_t write_val)96d39a76e7Sxw t1_write_reg_4(ch_t *obj, uint32_t reg_val, uint32_t write_val)
97d39a76e7Sxw {
98d39a76e7Sxw 	ddi_put32(obj->ch_hbar0, (uint32_t *)(obj->ch_bar0+reg_val), write_val);
99d39a76e7Sxw }
100d39a76e7Sxw 
101d39a76e7Sxw uint32_t
t1_os_pci_read_config_2(ch_t * obj,uint32_t reg,uint16_t * val)102d39a76e7Sxw t1_os_pci_read_config_2(ch_t *obj, uint32_t reg, uint16_t *val)
103d39a76e7Sxw {
104d39a76e7Sxw 	*val = pci_config_get16(obj->ch_hpci, reg);
105d39a76e7Sxw 	return (0);
106d39a76e7Sxw }
107d39a76e7Sxw 
108d39a76e7Sxw int
t1_os_pci_write_config_2(ch_t * obj,uint32_t reg,uint16_t val)109d39a76e7Sxw t1_os_pci_write_config_2(ch_t *obj, uint32_t reg, uint16_t val)
110d39a76e7Sxw {
111d39a76e7Sxw 	pci_config_put16(obj->ch_hpci, reg, val);
112d39a76e7Sxw 	return (0);
113d39a76e7Sxw }
114d39a76e7Sxw 
115d39a76e7Sxw uint32_t
t1_os_pci_read_config_4(ch_t * obj,uint32_t reg,uint32_t * val)116d39a76e7Sxw t1_os_pci_read_config_4(ch_t *obj, uint32_t reg, uint32_t *val)
117d39a76e7Sxw {
118d39a76e7Sxw 	*val = pci_config_get32(obj->ch_hpci, reg);
119d39a76e7Sxw 	return (0);
120d39a76e7Sxw }
121d39a76e7Sxw 
122d39a76e7Sxw int
t1_os_pci_write_config_4(ch_t * obj,uint32_t reg,uint32_t val)123d39a76e7Sxw t1_os_pci_write_config_4(ch_t *obj, uint32_t reg, uint32_t val)
124d39a76e7Sxw {
125d39a76e7Sxw 	pci_config_put32(obj->ch_hpci, reg, val);
126d39a76e7Sxw 	return (0);
127d39a76e7Sxw }
128d39a76e7Sxw 
129d39a76e7Sxw void *
t1_os_malloc_wait_zero(size_t len)130d39a76e7Sxw t1_os_malloc_wait_zero(size_t len)
131d39a76e7Sxw {
132d39a76e7Sxw 	return (kmem_zalloc(len, KM_SLEEP));
133d39a76e7Sxw }
134d39a76e7Sxw 
135d39a76e7Sxw void
t1_os_free(void * adr,size_t len)136d39a76e7Sxw t1_os_free(void *adr, size_t len)
137d39a76e7Sxw {
138d39a76e7Sxw 	kmem_free(adr, len);
139d39a76e7Sxw }
140d39a76e7Sxw 
141d39a76e7Sxw int
t1_num_of_ports(ch_t * obj)142d39a76e7Sxw t1_num_of_ports(ch_t *obj)
143d39a76e7Sxw {
144d39a76e7Sxw 	return (obj->config_data.num_of_ports);
145d39a76e7Sxw }
146d39a76e7Sxw 
147d39a76e7Sxw /* ARGSUSED */
148d39a76e7Sxw int
pe_os_mem_copy(ch_t * obj,void * dst,void * src,size_t len)149d39a76e7Sxw pe_os_mem_copy(ch_t *obj, void *dst, void *src, size_t len)
150d39a76e7Sxw {
151d39a76e7Sxw 	bcopy(src, dst, len);
152d39a76e7Sxw 	return (0);
153d39a76e7Sxw }
154d39a76e7Sxw 
155d39a76e7Sxw int
pe_is_ring_buffer_enabled(ch_t * obj)156d39a76e7Sxw pe_is_ring_buffer_enabled(ch_t *obj)
157d39a76e7Sxw {
158d39a76e7Sxw 	return (obj->config & CFGMD_RINGB);
159d39a76e7Sxw }
160d39a76e7Sxw 
161d39a76e7Sxw #define	PE_READ_REG  _IOR('i', 0xAB, 0x18)
162d39a76e7Sxw #define	PE_WRITE_REG _IOW('i', 0xAB, 0x18)
163d39a76e7Sxw #define	PE_READ_PCI  _IOR('i', 0xAC, 0x18)
164d39a76e7Sxw #define	PE_WRITE_PCI _IOW('i', 0xAC, 0x18)
165d39a76e7Sxw #define	PE_READ_INTR _IOR('i', 0xAD, 0x20)
166d39a76e7Sxw #define	TOETOOL_GETTPI _IOR('i', 0xAE, 0xc)
167d39a76e7Sxw #define	TOETOOL_SETTPI _IOW('i', 0xAE, 0xc)
168d39a76e7Sxw 
169d39a76e7Sxw void
pe_ioctl(ch_t * chp,queue_t * q,mblk_t * mp)170d39a76e7Sxw pe_ioctl(ch_t *chp, queue_t *q, mblk_t *mp)
171d39a76e7Sxw {
172d39a76e7Sxw 	struct iocblk *iocp;
173d39a76e7Sxw 	mblk_t *dmp;
174d39a76e7Sxw 	struct pe_reg *pe;
175d39a76e7Sxw 	struct toetool_reg *te;
176d39a76e7Sxw 	uint32_t reg;
177d39a76e7Sxw 	struct sge_intr_counts *se, *sep;
178d39a76e7Sxw 
179d39a76e7Sxw 	iocp = (struct iocblk *)mp->b_rptr;
180d39a76e7Sxw 
181d39a76e7Sxw 	/* don't support TRASPARENT ioctls */
182d39a76e7Sxw 	if (iocp->ioc_count == TRANSPARENT) {
183d39a76e7Sxw 		iocp->ioc_error = ENOTTY;
184d39a76e7Sxw 		goto bad;
185d39a76e7Sxw 	}
186d39a76e7Sxw 
187d39a76e7Sxw 	/*
188d39a76e7Sxw 	 * sanity checks. There should be a M_DATA mblk following
189d39a76e7Sxw 	 * the initial M_IOCTL mblk
190d39a76e7Sxw 	 */
191d39a76e7Sxw 	if ((dmp = mp->b_cont) == NULL) {
192d39a76e7Sxw 		iocp->ioc_error = ENOTTY;
193d39a76e7Sxw 		goto bad;
194d39a76e7Sxw 	}
195d39a76e7Sxw 
196d39a76e7Sxw 	if (dmp->b_datap->db_type != M_DATA) {
197d39a76e7Sxw 		iocp->ioc_error = ENOTTY;
198d39a76e7Sxw 		goto bad;
199d39a76e7Sxw 	}
200d39a76e7Sxw 
201d39a76e7Sxw 	pe = (struct pe_reg *)dmp->b_rptr;
202d39a76e7Sxw 	se = (struct sge_intr_counts *)dmp->b_rptr;
203d39a76e7Sxw 	te = (struct toetool_reg *)dmp->b_rptr;
204d39a76e7Sxw 
205d39a76e7Sxw 	/* now process the ioctl */
206d39a76e7Sxw 	switch (iocp->ioc_cmd) {
207d39a76e7Sxw 	case PE_READ_REG:
208d39a76e7Sxw 
209d39a76e7Sxw 		if ((dmp->b_wptr - dmp->b_rptr) != sizeof (*pe)) {
210d39a76e7Sxw 			iocp->ioc_error = ENOTTY;
211d39a76e7Sxw 			goto bad;
212d39a76e7Sxw 		}
213d39a76e7Sxw 
214d39a76e7Sxw 		/* protect against bad addr values */
215d39a76e7Sxw 		pe->addr &= (uint32_t)~3;
216d39a76e7Sxw 
217d39a76e7Sxw 		pe->pe_mask32 = 0xFFFFFFFF;
218d39a76e7Sxw 
219d39a76e7Sxw 		if (pe->addr == 0x950)
220d39a76e7Sxw 			pe->pe_reg_val = reg = t1_sge_get_ptimeout(chp);
221d39a76e7Sxw 		else
222d39a76e7Sxw 			pe->pe_reg_val = reg = t1_read_reg_4(chp, pe->addr);
223d39a76e7Sxw 
224d39a76e7Sxw 		mp->b_datap->db_type = M_IOCACK;
225d39a76e7Sxw 		iocp->ioc_count = sizeof (*pe);
226d39a76e7Sxw 
227d39a76e7Sxw 		break;
228d39a76e7Sxw 
229d39a76e7Sxw 	case PE_WRITE_REG:
230d39a76e7Sxw 
231d39a76e7Sxw 		if ((dmp->b_wptr - dmp->b_rptr) != sizeof (*pe)) {
232d39a76e7Sxw 			iocp->ioc_error = ENOTTY;
233d39a76e7Sxw 			goto bad;
234d39a76e7Sxw 		}
235d39a76e7Sxw 
236d39a76e7Sxw 		if (pe->addr == 0x950)
237d39a76e7Sxw 			t1_sge_set_ptimeout(chp, pe->pe_reg_val);
238d39a76e7Sxw 		else {
239d39a76e7Sxw 			if (pe->pe_mask32 != 0xffffffff) {
240d39a76e7Sxw 				reg = t1_read_reg_4(chp, pe->addr);
241d39a76e7Sxw 				pe->pe_reg_val |= (reg & ~pe->pe_mask32);
242d39a76e7Sxw 			}
243d39a76e7Sxw 
244d39a76e7Sxw 			t1_write_reg_4(chp, pe->addr,  pe->pe_reg_val);
245d39a76e7Sxw 		}
246d39a76e7Sxw 
247d39a76e7Sxw 		if (mp->b_cont)
248d39a76e7Sxw 			freemsg(mp->b_cont);
249d39a76e7Sxw 		mp->b_cont = NULL;
250d39a76e7Sxw 		mp->b_datap->db_type = M_IOCACK;
251d39a76e7Sxw 		break;
252d39a76e7Sxw 
253d39a76e7Sxw 	case PE_READ_PCI:
254d39a76e7Sxw 
255d39a76e7Sxw 		if ((dmp->b_wptr - dmp->b_rptr) != sizeof (*pe)) {
256d39a76e7Sxw 			iocp->ioc_error = ENOTTY;
257d39a76e7Sxw 			goto bad;
258d39a76e7Sxw 		}
259d39a76e7Sxw 
260d39a76e7Sxw 		/* protect against bad addr values */
261d39a76e7Sxw 		pe->addr &= (uint32_t)~3;
262d39a76e7Sxw 
263d39a76e7Sxw 		pe->pe_mask32 = 0xFFFFFFFF;
264d39a76e7Sxw 		pe->pe_reg_val = reg = pci_config_get32(chp->ch_hpci, pe->addr);
265d39a76e7Sxw 		mp->b_datap->db_type = M_IOCACK;
266d39a76e7Sxw 		iocp->ioc_count = sizeof (*pe);
267d39a76e7Sxw 
268d39a76e7Sxw 		break;
269d39a76e7Sxw 
270d39a76e7Sxw 	case PE_WRITE_PCI:
271d39a76e7Sxw 
272d39a76e7Sxw 		if ((dmp->b_wptr - dmp->b_rptr) != sizeof (*pe)) {
273d39a76e7Sxw 			iocp->ioc_error = ENOTTY;
274d39a76e7Sxw 			goto bad;
275d39a76e7Sxw 		}
276d39a76e7Sxw 
277d39a76e7Sxw 		if (pe->pe_mask32 != 0xffffffff) {
278d39a76e7Sxw 			reg = pci_config_get32(chp->ch_hpci, pe->addr);
279d39a76e7Sxw 			pe->pe_reg_val |= (reg & ~pe->pe_mask32);
280d39a76e7Sxw 		}
281d39a76e7Sxw 
282d39a76e7Sxw 		pci_config_put32(chp->ch_hpci, pe->addr,  pe->pe_reg_val);
283d39a76e7Sxw 
284d39a76e7Sxw 		if (mp->b_cont)
285d39a76e7Sxw 			freemsg(mp->b_cont);
286d39a76e7Sxw 		mp->b_cont = NULL;
287d39a76e7Sxw 		mp->b_datap->db_type = M_IOCACK;
288d39a76e7Sxw 		break;
289d39a76e7Sxw 
290d39a76e7Sxw 	case PE_READ_INTR:
291d39a76e7Sxw 
292d39a76e7Sxw 		if ((dmp->b_wptr - dmp->b_rptr) != sizeof (*se)) {
293d39a76e7Sxw 			iocp->ioc_error = ENOTTY;
294d39a76e7Sxw 			goto bad;
295d39a76e7Sxw 		}
296d39a76e7Sxw 
297d39a76e7Sxw 		sep = sge_get_stat(chp->sge);
298d39a76e7Sxw 		bcopy(sep, se, sizeof (*se));
299d39a76e7Sxw 		mp->b_datap->db_type = M_IOCACK;
300d39a76e7Sxw 		iocp->ioc_count = sizeof (*se);
301d39a76e7Sxw 		break;
302d39a76e7Sxw 
303d39a76e7Sxw 	case TOETOOL_GETTPI:
304d39a76e7Sxw 
305d39a76e7Sxw 		if ((dmp->b_wptr - dmp->b_rptr) != sizeof (*te)) {
306d39a76e7Sxw 			iocp->ioc_error = ENOTTY;
307d39a76e7Sxw 			goto bad;
308d39a76e7Sxw 		}
309d39a76e7Sxw 
310d39a76e7Sxw 		/* protect against bad addr values */
311d39a76e7Sxw 		if ((te->addr & 3) != 0) {
312d39a76e7Sxw 			iocp->ioc_error = ENOTTY;
313d39a76e7Sxw 			goto bad;
314d39a76e7Sxw 		}
315d39a76e7Sxw 
316d39a76e7Sxw 		(void) t1_tpi_read(chp, te->addr, &te->val);
317d39a76e7Sxw 		mp->b_datap->db_type = M_IOCACK;
318d39a76e7Sxw 		iocp->ioc_count = sizeof (*te);
319d39a76e7Sxw 
320d39a76e7Sxw 		break;
321d39a76e7Sxw 
322d39a76e7Sxw 	case TOETOOL_SETTPI:
323d39a76e7Sxw 
324d39a76e7Sxw 		if ((dmp->b_wptr - dmp->b_rptr) != sizeof (*te)) {
325d39a76e7Sxw 			iocp->ioc_error = ENOTTY;
326d39a76e7Sxw 			goto bad;
327d39a76e7Sxw 		}
328d39a76e7Sxw 
329d39a76e7Sxw 		/* protect against bad addr values */
330d39a76e7Sxw 		if ((te->addr & 3) != 0) {
331d39a76e7Sxw 			iocp->ioc_error = ENOTTY;
332d39a76e7Sxw 			goto bad;
333d39a76e7Sxw 		}
334d39a76e7Sxw 
335d39a76e7Sxw 		(void) t1_tpi_write(chp, te->addr, te->val);
336d39a76e7Sxw 
337d39a76e7Sxw 		mp->b_datap->db_type = M_IOCACK;
338d39a76e7Sxw 		iocp->ioc_count = sizeof (*te);
339d39a76e7Sxw 
340d39a76e7Sxw 		break;
341d39a76e7Sxw 
342d39a76e7Sxw 	default:
343d39a76e7Sxw 		iocp->ioc_error = ENOTTY;
344d39a76e7Sxw 		goto bad;
345d39a76e7Sxw 	}
346d39a76e7Sxw 
347d39a76e7Sxw 	qreply(q, mp);
348d39a76e7Sxw 
349d39a76e7Sxw 	return;
350d39a76e7Sxw 
351d39a76e7Sxw bad:
352d39a76e7Sxw 	if (mp->b_cont)
353d39a76e7Sxw 		freemsg(mp->b_cont);
354d39a76e7Sxw 	mp->b_cont = NULL;
355d39a76e7Sxw 	mp->b_datap->db_type = M_IOCNAK;
356d39a76e7Sxw 
357d39a76e7Sxw 	qreply(q, mp);
358d39a76e7Sxw }
359d39a76e7Sxw 
360d39a76e7Sxw /*
361d39a76e7Sxw  * Can't wait for memory here, since we have to use the Solaris dma
362d39a76e7Sxw  * mechanisms to determine the physical address.
363d39a76e7Sxw  * flg is either 0 (read) or DMA_OUT (write).
364d39a76e7Sxw  */
365d39a76e7Sxw void *
pe_os_malloc_contig_wait_zero(ch_t * chp,size_t len,uint64_t * dma_addr,ulong_t * dh,ulong_t * ah,uint32_t flg)366d39a76e7Sxw pe_os_malloc_contig_wait_zero(ch_t *chp, size_t len, uint64_t *dma_addr,
367d39a76e7Sxw 	ulong_t *dh, ulong_t *ah, uint32_t flg)
368d39a76e7Sxw {
369d39a76e7Sxw 	void *mem = NULL;
370d39a76e7Sxw 	uint64_t pa;
371d39a76e7Sxw 
372d39a76e7Sxw 	/*
373d39a76e7Sxw 	 * byte swap, consistant mapping & 4k aligned
374d39a76e7Sxw 	 */
375d39a76e7Sxw 	mem = ch_alloc_dma_mem(chp, 1, DMA_4KALN|flg, len, &pa, dh, ah);
376d39a76e7Sxw 	if (mem == NULL) {
377d39a76e7Sxw 		return (0);
378d39a76e7Sxw 	}
379d39a76e7Sxw 
380d39a76e7Sxw 	if (dma_addr)
381d39a76e7Sxw 		*dma_addr = pa;
382d39a76e7Sxw 
383d39a76e7Sxw 	bzero(mem, len);
384d39a76e7Sxw 
385d39a76e7Sxw 	return ((void *)mem);
386d39a76e7Sxw }
387d39a76e7Sxw 
388d39a76e7Sxw /* ARGSUSED */
389d39a76e7Sxw void
pe_os_free_contig(ch_t * obj,size_t len,void * addr,uint64_t dma_addr,ulong_t dh,ulong_t ah)390d39a76e7Sxw pe_os_free_contig(ch_t *obj, size_t len, void *addr, uint64_t dma_addr,
391d39a76e7Sxw 			ulong_t dh, ulong_t ah)
392d39a76e7Sxw {
393d39a76e7Sxw 	ch_free_dma_mem(dh, ah);
394d39a76e7Sxw }
395d39a76e7Sxw 
396d39a76e7Sxw void
t1_fatal_err(ch_t * adapter)397d39a76e7Sxw t1_fatal_err(ch_t *adapter)
398d39a76e7Sxw {
399d39a76e7Sxw 	if (adapter->ch_flags & PEINITDONE) {
400d39a76e7Sxw 		(void) sge_stop(adapter->sge);
401d39a76e7Sxw 		t1_interrupts_disable(adapter);
402d39a76e7Sxw 	}
403d39a76e7Sxw 	CH_ALERT("%s: encountered fatal error, operation suspended\n",
404*19397407SSherry Moore 	    adapter_name(adapter));
405d39a76e7Sxw }
406d39a76e7Sxw 
407d39a76e7Sxw void
CH_ALERT(const char * fmt,...)408d39a76e7Sxw CH_ALERT(const char *fmt, ...)
409d39a76e7Sxw {
410d39a76e7Sxw 	va_list	ap;
411d39a76e7Sxw 	char	buf[128];
412d39a76e7Sxw 
413d39a76e7Sxw 	/* format buf using fmt and arguments contained in ap */
414d39a76e7Sxw 
415d39a76e7Sxw 	va_start(ap, fmt);
416d39a76e7Sxw 	(void) vsprintf(buf, fmt, ap);
417d39a76e7Sxw 	va_end(ap);
418d39a76e7Sxw 
419d39a76e7Sxw 	/* pass formatted string to cmn_err(9F) */
420d39a76e7Sxw 	cmn_err(CE_WARN, "%s", buf);
421d39a76e7Sxw }
422d39a76e7Sxw 
423d39a76e7Sxw void
CH_WARN(const char * fmt,...)424d39a76e7Sxw CH_WARN(const char *fmt, ...)
425d39a76e7Sxw {
426d39a76e7Sxw 	va_list	ap;
427d39a76e7Sxw 	char	buf[128];
428d39a76e7Sxw 
429d39a76e7Sxw 	/* format buf using fmt and arguments contained in ap */
430d39a76e7Sxw 
431d39a76e7Sxw 	va_start(ap, fmt);
432d39a76e7Sxw 	(void) vsprintf(buf, fmt, ap);
433d39a76e7Sxw 	va_end(ap);
434d39a76e7Sxw 
435d39a76e7Sxw 	/* pass formatted string to cmn_err(9F) */
436d39a76e7Sxw 	cmn_err(CE_WARN, "%s", buf);
437d39a76e7Sxw }
438d39a76e7Sxw 
439d39a76e7Sxw void
CH_ERR(const char * fmt,...)440d39a76e7Sxw CH_ERR(const char *fmt, ...)
441d39a76e7Sxw {
442d39a76e7Sxw 	va_list	ap;
443d39a76e7Sxw 	char	buf[128];
444d39a76e7Sxw 
445d39a76e7Sxw 	/* format buf using fmt and arguments contained in ap */
446d39a76e7Sxw 
447d39a76e7Sxw 	va_start(ap, fmt);
448d39a76e7Sxw 	(void) vsprintf(buf, fmt, ap);
449d39a76e7Sxw 	va_end(ap);
450d39a76e7Sxw 
451d39a76e7Sxw 	/* pass formatted string to cmn_err(9F) */
452d39a76e7Sxw 	cmn_err(CE_WARN, "%s", buf);
453d39a76e7Sxw }
454d39a76e7Sxw 
455d39a76e7Sxw u32
le32_to_cpu(u32 data)456d39a76e7Sxw le32_to_cpu(u32 data)
457d39a76e7Sxw {
458d39a76e7Sxw #if BYTE_ORDER == BIG_ENDIAN
459d39a76e7Sxw 	uint8_t *in, t;
460d39a76e7Sxw 	in = (uint8_t *)&data;
461d39a76e7Sxw 	t = in[0];
462d39a76e7Sxw 	in[0] = in[3];
463d39a76e7Sxw 	in[3] = t;
464d39a76e7Sxw 	t = in[1];
465d39a76e7Sxw 	in[1] = in[2];
466d39a76e7Sxw 	in[2] = t;
467d39a76e7Sxw #endif
468d39a76e7Sxw 	return (data);
469d39a76e7Sxw }
470d39a76e7Sxw 
471d39a76e7Sxw /*
472d39a76e7Sxw  * This function initializes a polling routine, Poll_func
473d39a76e7Sxw  * which will be polled ever N Microsecond, where N is
474d39a76e7Sxw  * provided in the cyclic start routine.
475d39a76e7Sxw  */
476d39a76e7Sxw /* ARGSUSED */
477d39a76e7Sxw void
ch_init_cyclic(void * adapter,p_ch_cyclic_t cyclic,void (* poll_func)(void *),void * arg)478d39a76e7Sxw ch_init_cyclic(void *adapter, p_ch_cyclic_t cyclic,
479d39a76e7Sxw 		void (*poll_func)(void *), void *arg)
480d39a76e7Sxw {
481d39a76e7Sxw 	cyclic->func = poll_func;
482d39a76e7Sxw 	cyclic->arg = arg;
483d39a76e7Sxw 	cyclic->timer = 0;
484d39a76e7Sxw }
485d39a76e7Sxw 
486d39a76e7Sxw /*
487d39a76e7Sxw  * Cyclic function which provides a periodic polling
488d39a76e7Sxw  * capability to Solaris. The poll function provided by
489d39a76e7Sxw  * the 'ch_init_cyclic' function is called from this
490d39a76e7Sxw  * here, and this routine launches a new one-shot
491d39a76e7Sxw  * timer to bring it back in some period later.
492d39a76e7Sxw  */
493d39a76e7Sxw void
ch_cyclic(p_ch_cyclic_t cyclic)494d39a76e7Sxw ch_cyclic(p_ch_cyclic_t cyclic)
495d39a76e7Sxw {
496d39a76e7Sxw 	if (cyclic->timer != 0) {
497d39a76e7Sxw 		cyclic->func(cyclic->arg);
498d39a76e7Sxw 		cyclic->timer = timeout((void(*)(void  *))ch_cyclic,
499*19397407SSherry Moore 		    (void *)cyclic, cyclic->period);
500d39a76e7Sxw 	}
501d39a76e7Sxw }
502d39a76e7Sxw 
503d39a76e7Sxw /*
504d39a76e7Sxw  * The 'ch_start_cyclic' starts the polling.
505d39a76e7Sxw  */
506d39a76e7Sxw void
ch_start_cyclic(p_ch_cyclic_t cyclic,unsigned long period)507d39a76e7Sxw ch_start_cyclic(p_ch_cyclic_t cyclic, unsigned long period)
508d39a76e7Sxw {
509d39a76e7Sxw 	cyclic->period = drv_usectohz(period * 1000);
510d39a76e7Sxw 	if (cyclic->timer == 0) {
511d39a76e7Sxw 		cyclic->timer = timeout((void(*)(void  *))ch_cyclic,
512*19397407SSherry Moore 		    (void *)cyclic, cyclic->period);
513d39a76e7Sxw 	}
514d39a76e7Sxw }
515d39a76e7Sxw 
516d39a76e7Sxw /*
517d39a76e7Sxw  * The 'ch_stop_cyclic' stops the polling.
518d39a76e7Sxw  */
519d39a76e7Sxw void
ch_stop_cyclic(p_ch_cyclic_t cyclic)520d39a76e7Sxw ch_stop_cyclic(p_ch_cyclic_t cyclic)
521d39a76e7Sxw {
522d39a76e7Sxw 	timeout_id_t timer;
523d39a76e7Sxw 	clock_t value;
524d39a76e7Sxw 
525d39a76e7Sxw 	do {
526d39a76e7Sxw 		timer = cyclic->timer;
527d39a76e7Sxw 		cyclic->timer = 0;
528d39a76e7Sxw 		value = untimeout(timer);
529d39a76e7Sxw 		if (value == 0)
530*19397407SSherry Moore 			drv_usecwait(drv_hztousec(2 * cyclic->period));
531d39a76e7Sxw 	} while ((timer != 0) && (value == 0));
532d39a76e7Sxw }
533