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