1 /**************************************************************************
2
3 Copyright (c) 2007-2008, 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: releng/7.4/sys/dev/cxgb/cxgb_osdep.h 202745 2010-01-21 12:18:29Z np $
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 typedef struct port_info pinfo_t;
57 struct sge_rspq;
58
59 enum {
60 TP_TMR_RES = 200, /* TP timer resolution in usec */
61 MAX_NPORTS = 4, /* max # of ports */
62 TP_SRAM_OFFSET = 4096, /* TP SRAM content offset in eeprom */
63 TP_SRAM_LEN = 2112, /* TP SRAM content offset in eeprom */
64 };
65
66 struct t3_mbuf_hdr {
67 struct mbuf *mh_head;
68 struct mbuf *mh_tail;
69 };
70
71 #ifndef PANIC_IF
72 #define PANIC_IF(exp) do { \
73 if (exp) \
74 panic("BUG: %s", #exp); \
75 } while (0)
76 #endif
77
78 #define m_get_priority(m) ((uintptr_t)(m)->m_pkthdr.rcvif)
79 #define m_set_priority(m, pri) ((m)->m_pkthdr.rcvif = (struct ifnet *)((uintptr_t)pri))
80 #define m_set_sgl(m, sgl) ((m)->m_pkthdr.header = (sgl))
81 #define m_get_sgl(m) ((bus_dma_segment_t *)(m)->m_pkthdr.header)
82 #define m_set_sgllen(m, len) ((m)->m_pkthdr.ether_vtag = len)
83 #define m_get_sgllen(m) ((m)->m_pkthdr.ether_vtag)
84
85 /*
86 * XXX FIXME
87 */
88 #define m_set_toep(m, a) ((m)->m_pkthdr.header = (a))
89 #define m_get_toep(m) ((m)->m_pkthdr.header)
90 #define m_set_handler(m, handler) ((m)->m_pkthdr.header = (handler))
91
92 #define m_set_socket(m, a) ((m)->m_pkthdr.header = (a))
93 #define m_get_socket(m) ((m)->m_pkthdr.header)
94
95 #define KTR_CXGB KTR_SPARE2
96 void cxgb_log_tcb(struct adapter *sc, unsigned int tid);
97
98 #define MT_DONTFREE 128
99
100 #if __FreeBSD_version > 700030
101 #define INTR_FILTERS
102 #define FIRMWARE_LATEST
103 #endif
104
105 #if ((__FreeBSD_version > 602103) && (__FreeBSD_version < 700000))
106 #define FIRMWARE_LATEST
107 #endif
108
109 #if __FreeBSD_version > 700000
110 #define MSI_SUPPORTED
111 #define TSO_SUPPORTED
112 #define VLAN_SUPPORTED
113 #define TASKQUEUE_CURRENT
114 #else
115 #define if_name(ifp) (ifp)->if_xname
116 #define M_SANITY(m, n)
117 #endif
118
119 #define __read_mostly __attribute__((__section__(".data.read_mostly")))
120
121 /*
122 * Workaround for weird Chelsio issue
123 */
124 #if __FreeBSD_version > 700029
125 #define PRIV_SUPPORTED
126 #endif
127
128 #define CXGB_TX_CLEANUP_THRESHOLD 32
129
130
131 #ifdef DEBUG_PRINT
132 #define DPRINTF printf
133 #else
134 #define DPRINTF(...)
135 #endif
136
137 #define TX_MAX_SIZE (1 << 16) /* 64KB */
138 #define TX_MAX_SEGS 36 /* maximum supported by card */
139
140 #define TX_MAX_DESC 4 /* max descriptors per packet */
141
142
143 #define TX_START_MIN_DESC (TX_MAX_DESC << 2)
144 #define TX_START_MAX_DESC (TX_MAX_DESC << 3) /* maximum number of descriptors
145 * call to start used per */
146
147 #define TX_CLEAN_MAX_DESC (TX_MAX_DESC << 4) /* maximum tx descriptors
148 * to clean per iteration */
149 #define TX_WR_SIZE_MAX 11*1024 /* the maximum total size of packets aggregated into a single
150 * TX WR
151 */
152 #define TX_WR_COUNT_MAX 7 /* the maximum total number of packets that can be
153 * aggregated into a single TX WR
154 */
155
156
157 #if defined(__i386__) || defined(__amd64__)
158 #define mb() __asm volatile("mfence":::"memory")
159 #define rmb() __asm volatile("lfence":::"memory")
160 #define wmb() __asm volatile("sfence" ::: "memory")
161 #define smp_mb() mb()
162
163 #define L1_CACHE_BYTES 128
164 static __inline
165 void prefetch(void *x)
166 {
167 __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
168 }
169
170 extern void kdb_backtrace(void);
171
172 #define WARN_ON(condition) do { \
173 if (__predict_false((condition)!=0)) { \
174 log(LOG_WARNING, "BUG: warning at %s:%d/%s()\n", __FILE__, __LINE__, __FUNCTION__); \
175 kdb_backtrace(); \
176 } \
177 } while (0)
178
179
180 #else /* !i386 && !amd64 */
181 #define mb()
182 #define rmb()
183 #define wmb()
184 #define smp_mb()
185 #define prefetch(x)
186 #define L1_CACHE_BYTES 32
187 #endif
188
189 struct buf_ring {
190 caddr_t *br_ring;
191 volatile uint32_t br_cons;
192 volatile uint32_t br_prod;
193 int br_size;
194 struct mtx br_lock;
195 };
196
197 struct buf_ring *buf_ring_alloc(int count, int flags);
198 void buf_ring_free(struct buf_ring *);
199
200 static __inline int
201 buf_ring_count(struct buf_ring *mr)
202 {
203 int size = mr->br_size;
204 uint32_t mask = size - 1;
205
206 return ((size + mr->br_prod - mr->br_cons) & mask);
207 }
208
209 static __inline int
210 buf_ring_empty(struct buf_ring *mr)
211 {
212 return (mr->br_cons == mr->br_prod);
213 }
214
215 static __inline int
216 buf_ring_full(struct buf_ring *mr)
217 {
218 uint32_t mask;
219
220 mask = mr->br_size - 1;
221 return (mr->br_cons == ((mr->br_prod + 1) & mask));
222 }
223
224 /*
225 * The producer and consumer are independently locked
226 * this relies on the consumer providing his own serialization
227 *
228 */
229 static __inline void *
230 buf_ring_dequeue(struct buf_ring *mr)
231 {
232 uint32_t prod, cons, mask;
233 caddr_t *ring, m;
234
235 ring = (caddr_t *)mr->br_ring;
236 mask = mr->br_size - 1;
237 cons = mr->br_cons;
238 mb();
239 prod = mr->br_prod;
240 m = NULL;
241 if (cons != prod) {
242 m = ring[cons];
243 ring[cons] = NULL;
244 mr->br_cons = (cons + 1) & mask;
245 mb();
246 }
247 return (m);
248 }
249
250 #ifdef DEBUG_BUFRING
251 static __inline void
252 __buf_ring_scan(struct buf_ring *mr, void *m, char *file, int line)
253 {
254 int i;
255
256 for (i = 0; i < mr->br_size; i++)
257 if (m == mr->br_ring[i])
258 panic("%s:%d m=%p present prod=%d cons=%d idx=%d", file,
259 line, m, mr->br_prod, mr->br_cons, i);
260 }
261
262 static __inline void
263 buf_ring_scan(struct buf_ring *mr, void *m, char *file, int line)
264 {
265 mtx_lock(&mr->br_lock);
266 __buf_ring_scan(mr, m, file, line);
267 mtx_unlock(&mr->br_lock);
268 }
269
270 #else
271 static __inline void
272 __buf_ring_scan(struct buf_ring *mr, void *m, char *file, int line)
273 {
274 }
275
276 static __inline void
277 buf_ring_scan(struct buf_ring *mr, void *m, char *file, int line)
278 {
279 }
280 #endif
281
282 static __inline int
283 __buf_ring_enqueue(struct buf_ring *mr, void *m, char *file, int line)
284 {
285
286 uint32_t prod, cons, mask;
287 int err;
288
289 mask = mr->br_size - 1;
290 prod = mr->br_prod;
291 mb();
292 cons = mr->br_cons;
293 __buf_ring_scan(mr, m, file, line);
294 if (((prod + 1) & mask) != cons) {
295 KASSERT(mr->br_ring[prod] == NULL, ("overwriting entry"));
296 mr->br_ring[prod] = m;
297 mb();
298 mr->br_prod = (prod + 1) & mask;
299 err = 0;
300 } else
301 err = ENOBUFS;
302
303 return (err);
304 }
305
306 static __inline int
307 buf_ring_enqueue_(struct buf_ring *mr, void *m, char *file, int line)
308 {
309 int err;
310
311 mtx_lock(&mr->br_lock);
312 err = __buf_ring_enqueue(mr, m, file, line);
313 mtx_unlock(&mr->br_lock);
314
315 return (err);
316 }
317
318 #define buf_ring_enqueue(mr, m) buf_ring_enqueue_((mr), (m), __FILE__, __LINE__)
319
320
321 static __inline void *
322 buf_ring_peek(struct buf_ring *mr)
323 {
324 int prod, cons, mask;
325 caddr_t *ring, m;
326
327 ring = (caddr_t *)mr->br_ring;
328 mask = mr->br_size - 1;
329 cons = mr->br_cons;
330 prod = mr->br_prod;
331 m = NULL;
332 if (cons != prod)
333 m = ring[cons];
334
335 return (m);
336 }
337
338 #define DBG_RX (1 << 0)
339 static const int debug_flags = DBG_RX;
340
341 #ifdef DEBUG_PRINT
342 #define DBG(flag, msg) do { \
343 if ((flag & debug_flags)) \
344 printf msg; \
345 } while (0)
346 #else
347 #define DBG(...)
348 #endif
349
350 #include <sys/syslog.h>
351
352 #define promisc_rx_mode(rm) ((rm)->port->ifp->if_flags & IFF_PROMISC)
353 #define allmulti_rx_mode(rm) ((rm)->port->ifp->if_flags & IFF_ALLMULTI)
354
355 #define CH_ERR(adap, fmt, ...) log(LOG_ERR, fmt, ##__VA_ARGS__)
356 #define CH_WARN(adap, fmt, ...) log(LOG_WARNING, fmt, ##__VA_ARGS__)
357 #define CH_ALERT(adap, fmt, ...) log(LOG_ALERT, fmt, ##__VA_ARGS__)
358
359 #define t3_os_sleep(x) DELAY((x) * 1000)
360
361 #define test_and_clear_bit(bit, p) atomic_cmpset_int((p), ((*(p)) | (1<<bit)), ((*(p)) & ~(1<<bit)))
362
363 #define max_t(type, a, b) (type)max((a), (b))
364 #define net_device ifnet
365 #define cpu_to_be32 htobe32
366
367 /* Standard PHY definitions */
368 #define BMCR_LOOPBACK BMCR_LOOP
369 #define BMCR_ISOLATE BMCR_ISO
370 #define BMCR_ANENABLE BMCR_AUTOEN
371 #define BMCR_SPEED1000 BMCR_SPEED1
372 #define BMCR_SPEED100 BMCR_SPEED0
373 #define BMCR_ANRESTART BMCR_STARTNEG
374 #define BMCR_FULLDPLX BMCR_FDX
375 #define BMSR_LSTATUS BMSR_LINK
376 #define BMSR_ANEGCOMPLETE BMSR_ACOMP
377
378 #define MII_LPA MII_ANLPAR
379 #define MII_ADVERTISE MII_ANAR
380 #define MII_CTRL1000 MII_100T2CR
381
382 #define ADVERTISE_PAUSE_CAP ANAR_FC
383 #define ADVERTISE_PAUSE_ASYM 0x800
384 #define ADVERTISE_PAUSE ANAR_FC
385 #define ADVERTISE_1000HALF 0x100
386 #define ADVERTISE_1000FULL 0x200
387 #define ADVERTISE_10FULL ANAR_10_FD
388 #define ADVERTISE_10HALF ANAR_10
389 #define ADVERTISE_100FULL ANAR_TX_FD
390 #define ADVERTISE_100HALF ANAR_TX
391
392
393 #define ADVERTISE_1000XHALF ANAR_X_HD
394 #define ADVERTISE_1000XFULL ANAR_X_FD
395 #define ADVERTISE_1000XPSE_ASYM ANAR_X_PAUSE_ASYM
396 #define ADVERTISE_1000XPAUSE ANAR_X_PAUSE_SYM
397
398 #define ADVERTISE_CSMA ANAR_CSMA
399 #define ADVERTISE_NPAGE ANAR_NP
400
401
402 /* Standard PCI Extended Capabilities definitions */
403 #define PCI_CAP_ID_VPD PCIY_VPD
404 #define PCI_VPD_ADDR PCIR_VPD_ADDR
405 #define PCI_VPD_ADDR_F 0x8000
406 #define PCI_VPD_DATA PCIR_VPD_DATA
407
408 #define PCI_CAP_ID_EXP PCIY_EXPRESS
409 #define PCI_EXP_DEVCTL PCIR_EXPRESS_DEVICE_CTL
410 #define PCI_EXP_DEVCTL_PAYLOAD PCIM_EXP_CTL_MAX_PAYLOAD
411 #define PCI_EXP_DEVCTL_READRQ PCIM_EXP_CTL_MAX_READ_REQUEST
412 #define PCI_EXP_LNKCTL PCIR_EXPRESS_LINK_CTL
413 #define PCI_EXP_LNKSTA PCIR_EXPRESS_LINK_STA
414
415 /*
416 * Linux compatibility macros
417 */
418
419 /* Some simple translations */
420 #define __devinit
421 #define udelay(x) DELAY(x)
422 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
423 #define le32_to_cpu(x) le32toh(x)
424 #define le16_to_cpu(x) le16toh(x)
425 #define cpu_to_le32(x) htole32(x)
426 #define swab32(x) bswap32(x)
427 #define simple_strtoul strtoul
428
429
430 #ifndef LINUX_TYPES_DEFINED
431 typedef uint8_t u8;
432 typedef uint16_t u16;
433 typedef uint32_t u32;
434 typedef uint64_t u64;
435
436 typedef uint8_t __u8;
437 typedef uint16_t __u16;
438 typedef uint32_t __u32;
439 typedef uint8_t __be8;
440 typedef uint16_t __be16;
441 typedef uint32_t __be32;
442 typedef uint64_t __be64;
443 #endif
444
445
446 #if BYTE_ORDER == BIG_ENDIAN
447 #define __BIG_ENDIAN_BITFIELD
448 #elif BYTE_ORDER == LITTLE_ENDIAN
449 #define __LITTLE_ENDIAN_BITFIELD
450 #else
451 #error "Must set BYTE_ORDER"
452 #endif
453
454 /* Indicates what features are supported by the interface. */
455 #define SUPPORTED_10baseT_Half (1 << 0)
456 #define SUPPORTED_10baseT_Full (1 << 1)
457 #define SUPPORTED_100baseT_Half (1 << 2)
458 #define SUPPORTED_100baseT_Full (1 << 3)
459 #define SUPPORTED_1000baseT_Half (1 << 4)
460 #define SUPPORTED_1000baseT_Full (1 << 5)
461 #define SUPPORTED_Autoneg (1 << 6)
462 #define SUPPORTED_TP (1 << 7)
463 #define SUPPORTED_AUI (1 << 8)
464 #define SUPPORTED_MII (1 << 9)
465 #define SUPPORTED_FIBRE (1 << 10)
466 #define SUPPORTED_BNC (1 << 11)
467 #define SUPPORTED_10000baseT_Full (1 << 12)
468 #define SUPPORTED_Pause (1 << 13)
469 #define SUPPORTED_Asym_Pause (1 << 14)
470
471 /* Indicates what features are advertised by the interface. */
472 #define ADVERTISED_10baseT_Half (1 << 0)
473 #define ADVERTISED_10baseT_Full (1 << 1)
474 #define ADVERTISED_100baseT_Half (1 << 2)
475 #define ADVERTISED_100baseT_Full (1 << 3)
476 #define ADVERTISED_1000baseT_Half (1 << 4)
477 #define ADVERTISED_1000baseT_Full (1 << 5)
478 #define ADVERTISED_Autoneg (1 << 6)
479 #define ADVERTISED_TP (1 << 7)
480 #define ADVERTISED_AUI (1 << 8)
481 #define ADVERTISED_MII (1 << 9)
482 #define ADVERTISED_FIBRE (1 << 10)
483 #define ADVERTISED_BNC (1 << 11)
484 #define ADVERTISED_10000baseT_Full (1 << 12)
485 #define ADVERTISED_Pause (1 << 13)
486 #define ADVERTISED_Asym_Pause (1 << 14)
487
488 /* Enable or disable autonegotiation. If this is set to enable,
489 * the forced link modes above are completely ignored.
490 */
491 #define AUTONEG_DISABLE 0x00
492 #define AUTONEG_ENABLE 0x01
493
494 #define SPEED_10 10
495 #define SPEED_100 100
496 #define SPEED_1000 1000
497 #define SPEED_10000 10000
498 #define DUPLEX_HALF 0
499 #define DUPLEX_FULL 1
500
501 #endif
Cache object: debb1b8f8bcf954c3ce13c844bffa222
|