xref: /freebsd/sys/dev/cxgb/cxgb_osdep.h (revision db612abe8df3355d1eb23bb3b50fdd97bc21e979)
1 /**************************************************************************
2 
3 Copyright (c) 2007, Chelsio Inc.
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9  1. Redistributions of source code must retain the above copyright notice,
10     this list of conditions and the following disclaimer.
11 
12  2. Neither the name of the Chelsio Corporation nor the names of its
13     contributors may be used to endorse or promote products derived from
14     this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 POSSIBILITY OF SUCH DAMAGE.
27 
28 
29 $FreeBSD$
30 
31 ***************************************************************************/
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/ctype.h>
36 #include <sys/endian.h>
37 #include <sys/bus.h>
38 
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 
42 #include <dev/mii/mii.h>
43 
44 #ifdef CONFIG_DEFINED
45 #include <common/cxgb_version.h>
46 #include <cxgb_config.h>
47 #else
48 #include <dev/cxgb/common/cxgb_version.h>
49 #include <dev/cxgb/cxgb_config.h>
50 #endif
51 
52 #ifndef _CXGB_OSDEP_H_
53 #define _CXGB_OSDEP_H_
54 
55 typedef struct adapter adapter_t;
56 struct sge_rspq;
57 
58 
59 struct t3_mbuf_hdr {
60 	struct mbuf *mh_head;
61 	struct mbuf *mh_tail;
62 };
63 
64 #define PANIC_IF(exp) do {                  \
65 	if (exp)                            \
66 		panic("BUG: %s", #exp);      \
67 } while (0)
68 
69 #define m_get_priority(m) ((uintptr_t)(m)->m_pkthdr.rcvif)
70 #define m_set_priority(m, pri) ((m)->m_pkthdr.rcvif = (struct ifnet *)((uintptr_t)pri))
71 #define m_set_sgl(m, sgl) ((m)->m_pkthdr.header = (sgl))
72 #define m_get_sgl(m) ((bus_dma_segment_t *)(m)->m_pkthdr.header)
73 #define m_set_sgllen(m, len) ((m)->m_pkthdr.ether_vtag = len)
74 #define m_get_sgllen(m) ((m)->m_pkthdr.ether_vtag)
75 
76 /*
77  * XXX FIXME
78  */
79 #define m_set_toep(m, a) ((m)->m_pkthdr.header = (a))
80 #define m_get_toep(m) ((m)->m_pkthdr.header)
81 #define m_set_handler(m, handler) ((m)->m_pkthdr.header = (handler))
82 
83 #define m_set_socket(m, a) ((m)->m_pkthdr.header = (a))
84 #define m_get_socket(m) ((m)->m_pkthdr.header)
85 
86 #define	KTR_CXGB	KTR_SPARE2
87 void cxgb_log_tcb(struct adapter *sc, unsigned int tid);
88 
89 #define MT_DONTFREE  128
90 
91 #if __FreeBSD_version > 700030
92 #define INTR_FILTERS
93 #define FIRMWARE_LATEST
94 #endif
95 
96 #if ((__FreeBSD_version > 602103) && (__FreeBSD_version < 700000))
97 #define FIRMWARE_LATEST
98 #endif
99 
100 #if __FreeBSD_version > 700000
101 #define MSI_SUPPORTED
102 #define TSO_SUPPORTED
103 #define VLAN_SUPPORTED
104 #define TASKQUEUE_CURRENT
105 #else
106 #define if_name(ifp) (ifp)->if_xname
107 #define M_SANITY(m, n)
108 #endif
109 
110 #define __read_mostly __attribute__((__section__(".data.read_mostly")))
111 
112 /*
113  * Workaround for weird Chelsio issue
114  */
115 #if __FreeBSD_version > 700029
116 #define PRIV_SUPPORTED
117 #endif
118 
119 #define CXGB_TX_CLEANUP_THRESHOLD        32
120 
121 
122 #ifdef DEBUG_PRINT
123 #define DPRINTF printf
124 #else
125 #define DPRINTF(...)
126 #endif
127 
128 #define TX_MAX_SIZE                (1 << 16)    /* 64KB                          */
129 #define TX_MAX_SEGS                      36     /* maximum supported by card     */
130 
131 #define TX_MAX_DESC                       4     /* max descriptors per packet    */
132 
133 
134 #define TX_START_MIN_DESC  (TX_MAX_DESC << 2)
135 
136 
137 
138 #define TX_START_MAX_DESC (TX_MAX_DESC << 3)    /* maximum number of descriptors
139 						 * call to start used per 	 */
140 
141 #define TX_CLEAN_MAX_DESC (TX_MAX_DESC << 4)    /* maximum tx descriptors
142 						 * to clean per iteration        */
143 #define TX_WR_SIZE_MAX    11*1024              /* the maximum total size of packets aggregated into a single
144 						* TX WR
145 						*/
146 #define TX_WR_COUNT_MAX         7              /* the maximum total number of packets that can be
147 						* aggregated into a single TX WR
148 						*/
149 
150 
151 #if defined(__i386__) || defined(__amd64__)
152 #define mb()    __asm volatile("mfence":::"memory")
153 #define rmb()   __asm volatile("lfence":::"memory")
154 #define wmb()   __asm volatile("sfence" ::: "memory")
155 #define smp_mb() mb()
156 
157 #define L1_CACHE_BYTES 128
158 static __inline
159 void prefetch(void *x)
160 {
161         __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
162 }
163 
164 extern void kdb_backtrace(void);
165 
166 #define WARN_ON(condition) do { \
167         if (unlikely((condition)!=0)) { \
168                 log(LOG_WARNING, "BUG: warning at %s:%d/%s()\n", __FILE__, __LINE__, __FUNCTION__); \
169                 kdb_backtrace(); \
170         } \
171 } while (0)
172 
173 
174 #else /* !i386 && !amd64 */
175 #define mb()
176 #define rmb()
177 #define wmb()
178 #define smp_mb()
179 #define prefetch(x)
180 #define L1_CACHE_BYTES 32
181 #endif
182 
183 struct buf_ring {
184 	caddr_t          *br_ring;
185 	volatile uint32_t br_cons;
186 	volatile uint32_t br_prod;
187 	int               br_size;
188 	struct mtx        br_lock;
189 };
190 
191 struct buf_ring *buf_ring_alloc(int count, int flags);
192 void buf_ring_free(struct buf_ring *);
193 
194 static __inline int
195 buf_ring_count(struct buf_ring *mr)
196 {
197 	int size = mr->br_size;
198 	uint32_t mask = size - 1;
199 
200 	return ((size + mr->br_prod - mr->br_cons) & mask);
201 }
202 
203 static __inline int
204 buf_ring_empty(struct buf_ring *mr)
205 {
206 	return (mr->br_cons == mr->br_prod);
207 }
208 
209 static __inline int
210 buf_ring_full(struct buf_ring *mr)
211 {
212 	uint32_t mask;
213 
214 	mask = mr->br_size - 1;
215 	return (mr->br_cons == ((mr->br_prod + 1) & mask));
216 }
217 
218 /*
219  * The producer and consumer are independently locked
220  * this relies on the consumer providing his own serialization
221  *
222  */
223 static __inline void *
224 buf_ring_dequeue(struct buf_ring *mr)
225 {
226 	uint32_t prod, cons, mask;
227 	caddr_t *ring, m;
228 
229 	ring = (caddr_t *)mr->br_ring;
230 	mask = mr->br_size - 1;
231 	cons = mr->br_cons;
232 	mb();
233 	prod = mr->br_prod;
234 	m = NULL;
235 	if (cons != prod) {
236 		m = ring[cons];
237 		ring[cons] = NULL;
238 		mr->br_cons = (cons + 1) & mask;
239 		mb();
240 	}
241 	return (m);
242 }
243 
244 #ifdef DEBUG_BUFRING
245 static __inline void
246 __buf_ring_scan(struct buf_ring *mr, void *m, char *file, int line)
247 {
248 	int i;
249 
250 	for (i = 0; i < mr->br_size; i++)
251 		if (m == mr->br_ring[i])
252 			panic("%s:%d m=%p present prod=%d cons=%d idx=%d", file,
253 			    line, m, mr->br_prod, mr->br_cons, i);
254 }
255 
256 static __inline void
257 buf_ring_scan(struct buf_ring *mr, void *m, char *file, int line)
258 {
259 	mtx_lock(&mr->br_lock);
260 	__buf_ring_scan(mr, m, file, line);
261 	mtx_unlock(&mr->br_lock);
262 }
263 
264 #else
265 static __inline void
266 __buf_ring_scan(struct buf_ring *mr, void *m, char *file, int line)
267 {
268 }
269 
270 static __inline void
271 buf_ring_scan(struct buf_ring *mr, void *m, char *file, int line)
272 {
273 }
274 #endif
275 
276 static __inline int
277 __buf_ring_enqueue(struct buf_ring *mr, void *m, char *file, int line)
278 {
279 
280 	uint32_t prod, cons, mask;
281 	int err;
282 
283 	mask = mr->br_size - 1;
284 	prod = mr->br_prod;
285 	mb();
286 	cons = mr->br_cons;
287 	__buf_ring_scan(mr, m, file, line);
288 	if (((prod + 1) & mask) != cons) {
289 		KASSERT(mr->br_ring[prod] == NULL, ("overwriting entry"));
290 		mr->br_ring[prod] = m;
291 		mb();
292 		mr->br_prod = (prod + 1) & mask;
293 		err = 0;
294 	} else
295 		err = ENOBUFS;
296 
297 	return (err);
298 }
299 
300 static __inline int
301 buf_ring_enqueue_(struct buf_ring *mr, void *m, char *file, int line)
302 {
303 	int err;
304 
305 	mtx_lock(&mr->br_lock);
306 	err = __buf_ring_enqueue(mr, m, file, line);
307 	mtx_unlock(&mr->br_lock);
308 
309 	return (err);
310 }
311 
312 #define buf_ring_enqueue(mr, m) buf_ring_enqueue_((mr), (m), __FILE__, __LINE__)
313 
314 
315 static __inline void *
316 buf_ring_peek(struct buf_ring *mr)
317 {
318 	int prod, cons, mask;
319 	caddr_t *ring, m;
320 
321 	ring = (caddr_t *)mr->br_ring;
322 	mask = mr->br_size - 1;
323 	cons = mr->br_cons;
324 	prod = mr->br_prod;
325 	m = NULL;
326 	if (cons != prod)
327 		m = ring[cons];
328 
329 	return (m);
330 }
331 
332 #define DBG_RX          (1 << 0)
333 static const int debug_flags = DBG_RX;
334 
335 #ifdef DEBUG_PRINT
336 #define DBG(flag, msg) do {	\
337 	if ((flag & debug_flags))	\
338 		printf msg; \
339 } while (0)
340 #else
341 #define DBG(...)
342 #endif
343 
344 #include <sys/syslog.h>
345 
346 #define promisc_rx_mode(rm)  ((rm)->port->ifp->if_flags & IFF_PROMISC)
347 #define allmulti_rx_mode(rm) ((rm)->port->ifp->if_flags & IFF_ALLMULTI)
348 
349 #define CH_ERR(adap, fmt, ...) log(LOG_ERR, fmt, ##__VA_ARGS__)
350 #define CH_WARN(adap, fmt, ...)	log(LOG_WARNING, fmt, ##__VA_ARGS__)
351 #define CH_ALERT(adap, fmt, ...) log(LOG_ALERT, fmt, ##__VA_ARGS__)
352 
353 #define t3_os_sleep(x) DELAY((x) * 1000)
354 
355 #define test_and_clear_bit(bit, p) atomic_cmpset_int((p), ((*(p)) | (1<<bit)), ((*(p)) & ~(1<<bit)))
356 
357 #define max_t(type, a, b) (type)max((a), (b))
358 #define net_device ifnet
359 #define cpu_to_be32            htobe32
360 
361 /* Standard PHY definitions */
362 #define BMCR_LOOPBACK		BMCR_LOOP
363 #define BMCR_ISOLATE		BMCR_ISO
364 #define BMCR_ANENABLE		BMCR_AUTOEN
365 #define BMCR_SPEED1000		BMCR_SPEED1
366 #define BMCR_SPEED100		BMCR_SPEED0
367 #define BMCR_ANRESTART		BMCR_STARTNEG
368 #define BMCR_FULLDPLX		BMCR_FDX
369 #define BMSR_LSTATUS		BMSR_LINK
370 #define BMSR_ANEGCOMPLETE	BMSR_ACOMP
371 
372 #define MII_LPA			MII_ANLPAR
373 #define MII_ADVERTISE		MII_ANAR
374 #define MII_CTRL1000		MII_100T2CR
375 
376 #define ADVERTISE_PAUSE_CAP	ANAR_FC
377 #define ADVERTISE_PAUSE_ASYM	ANAR_X_PAUSE_ASYM
378 #define ADVERTISE_PAUSE		ANAR_X_PAUSE_SYM
379 #define ADVERTISE_1000HALF	ANAR_X_HD
380 #define ADVERTISE_1000FULL	ANAR_X_FD
381 #define ADVERTISE_10FULL	ANAR_10_FD
382 #define ADVERTISE_10HALF	ANAR_10
383 #define ADVERTISE_100FULL	ANAR_TX_FD
384 #define ADVERTISE_100HALF	ANAR_TX
385 
386 
387 #define ADVERTISE_1000XHALF	ANAR_X_HD
388 #define ADVERTISE_1000XFULL	ANAR_X_FD
389 #define ADVERTISE_1000XPSE_ASYM	ANAR_X_PAUSE_ASYM
390 #define ADVERTISE_1000XPAUSE	ANAR_X_PAUSE_SYM
391 
392 
393 /* Standard PCI Extended Capaibilities definitions */
394 #define PCI_CAP_ID_VPD	0x03
395 #define PCI_VPD_ADDR	2
396 #define PCI_VPD_ADDR_F	0x8000
397 #define PCI_VPD_DATA	4
398 
399 #define PCI_CAP_ID_EXP	0x10
400 #define PCI_EXP_DEVCTL	8
401 #define PCI_EXP_DEVCTL_PAYLOAD 0x00e0
402 #define PCI_EXP_LNKCTL	16
403 #define PCI_EXP_LNKSTA	18
404 
405 /*
406  * Linux compatibility macros
407  */
408 
409 /* Some simple translations */
410 #define __devinit
411 #define udelay(x) DELAY(x)
412 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
413 #define le32_to_cpu(x) le32toh(x)
414 #define le16_to_cpu(x) le16toh(x)
415 #define cpu_to_le32(x) htole32(x)
416 #define swab32(x) bswap32(x)
417 #define simple_strtoul strtoul
418 
419 
420 #ifndef LINUX_TYPES_DEFINED
421 typedef uint8_t 	u8;
422 typedef uint16_t 	u16;
423 typedef uint32_t 	u32;
424 typedef uint64_t 	u64;
425 
426 typedef uint8_t		__u8;
427 typedef uint16_t	__u16;
428 typedef uint32_t	__u32;
429 typedef uint8_t		__be8;
430 typedef uint16_t	__be16;
431 typedef uint32_t	__be32;
432 typedef uint64_t	__be64;
433 #endif
434 
435 
436 #if BYTE_ORDER == BIG_ENDIAN
437 #define __BIG_ENDIAN_BITFIELD
438 #elif BYTE_ORDER == LITTLE_ENDIAN
439 #define __LITTLE_ENDIAN_BITFIELD
440 #else
441 #error "Must set BYTE_ORDER"
442 #endif
443 
444 /* Indicates what features are supported by the interface. */
445 #define SUPPORTED_10baseT_Half          (1 << 0)
446 #define SUPPORTED_10baseT_Full          (1 << 1)
447 #define SUPPORTED_100baseT_Half         (1 << 2)
448 #define SUPPORTED_100baseT_Full         (1 << 3)
449 #define SUPPORTED_1000baseT_Half        (1 << 4)
450 #define SUPPORTED_1000baseT_Full        (1 << 5)
451 #define SUPPORTED_Autoneg               (1 << 6)
452 #define SUPPORTED_TP                    (1 << 7)
453 #define SUPPORTED_AUI                   (1 << 8)
454 #define SUPPORTED_MII                   (1 << 9)
455 #define SUPPORTED_FIBRE                 (1 << 10)
456 #define SUPPORTED_BNC                   (1 << 11)
457 #define SUPPORTED_10000baseT_Full       (1 << 12)
458 #define SUPPORTED_Pause                 (1 << 13)
459 #define SUPPORTED_Asym_Pause            (1 << 14)
460 
461 /* Indicates what features are advertised by the interface. */
462 #define ADVERTISED_10baseT_Half         (1 << 0)
463 #define ADVERTISED_10baseT_Full         (1 << 1)
464 #define ADVERTISED_100baseT_Half        (1 << 2)
465 #define ADVERTISED_100baseT_Full        (1 << 3)
466 #define ADVERTISED_1000baseT_Half       (1 << 4)
467 #define ADVERTISED_1000baseT_Full       (1 << 5)
468 #define ADVERTISED_Autoneg              (1 << 6)
469 #define ADVERTISED_TP                   (1 << 7)
470 #define ADVERTISED_AUI                  (1 << 8)
471 #define ADVERTISED_MII                  (1 << 9)
472 #define ADVERTISED_FIBRE                (1 << 10)
473 #define ADVERTISED_BNC                  (1 << 11)
474 #define ADVERTISED_10000baseT_Full      (1 << 12)
475 #define ADVERTISED_Pause                (1 << 13)
476 #define ADVERTISED_Asym_Pause           (1 << 14)
477 
478 /* Enable or disable autonegotiation.  If this is set to enable,
479  * the forced link modes above are completely ignored.
480  */
481 #define AUTONEG_DISABLE         0x00
482 #define AUTONEG_ENABLE          0x01
483 
484 #define SPEED_10		10
485 #define SPEED_100		100
486 #define SPEED_1000		1000
487 #define SPEED_10000		10000
488 #define DUPLEX_HALF		0
489 #define DUPLEX_FULL		1
490 
491 #endif
492