1 /* 2 * Copyright (c) 2017-2018 Cavium, Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 * 27 * $FreeBSD$ 28 */ 29 30 #ifndef __BCM_OSAL_ECORE_PACKAGE 31 #define __BCM_OSAL_ECORE_PACKAGE 32 33 #include "qlnx_os.h" 34 #include "ecore_status.h" 35 #include <sys/bitstring.h> 36 37 #if __FreeBSD_version >= 1100090 38 #include <linux/bitmap.h> 39 #else 40 #include <ofed/include/linux/bitops.h> 41 #endif 42 43 /* 44 * prototypes of freebsd specific functions required by ecore 45 */ 46 extern uint32_t qlnx_pci_bus_get_bar_size(void *ecore_dev, uint8_t bar_id); 47 extern uint32_t qlnx_pci_read_config_byte(void *ecore_dev, uint32_t pci_reg, 48 uint8_t *reg_value); 49 extern uint32_t qlnx_pci_read_config_word(void *ecore_dev, uint32_t pci_reg, 50 uint16_t *reg_value); 51 extern uint32_t qlnx_pci_read_config_dword(void *ecore_dev, uint32_t pci_reg, 52 uint32_t *reg_value); 53 extern void qlnx_pci_write_config_byte(void *ecore_dev, uint32_t pci_reg, 54 uint8_t reg_value); 55 extern void qlnx_pci_write_config_word(void *ecore_dev, uint32_t pci_reg, 56 uint16_t reg_value); 57 extern void qlnx_pci_write_config_dword(void *ecore_dev, uint32_t pci_reg, 58 uint32_t reg_value); 59 extern int qlnx_pci_find_capability(void *ecore_dev, int cap); 60 61 extern uint32_t qlnx_direct_reg_rd32(void *p_hwfn, uint32_t *reg_addr); 62 extern void qlnx_direct_reg_wr32(void *p_hwfn, void *reg_addr, uint32_t value); 63 64 extern uint32_t qlnx_reg_rd32(void *p_hwfn, uint32_t reg_addr); 65 extern void qlnx_reg_wr32(void *p_hwfn, uint32_t reg_addr, uint32_t value); 66 extern void qlnx_reg_wr16(void *p_hwfn, uint32_t reg_addr, uint16_t value); 67 68 extern void qlnx_dbell_wr32(void *p_hwfn, uint32_t reg_addr, uint32_t value); 69 70 extern void *qlnx_dma_alloc_coherent(void *ecore_dev, bus_addr_t *phys, 71 uint32_t size); 72 extern void qlnx_dma_free_coherent(void *ecore_dev, void *v_addr, 73 bus_addr_t phys, uint32_t size); 74 75 extern void qlnx_link_update(void *p_hwfn); 76 extern void qlnx_barrier(void *p_hwfn); 77 78 extern void *qlnx_zalloc(uint32_t size); 79 80 extern void qlnx_get_protocol_stats(void *cdev, int proto_type, 81 void *proto_stats); 82 83 extern void qlnx_sp_isr(void *arg); 84 85 86 #ifdef ECORE_PACKAGE 87 88 /* Memory Types */ 89 #define u8 uint8_t 90 #define u16 uint16_t 91 #define u32 uint32_t 92 #define u64 uint64_t 93 #define s16 uint16_t 94 #define s32 uint32_t 95 96 #ifndef QLNX_RDMA 97 98 typedef uint16_t __le16; 99 typedef uint32_t __le32; 100 typedef uint16_t __be16; 101 typedef uint32_t __be32; 102 103 static __inline unsigned long 104 roundup_pow_of_two(unsigned long x) 105 { 106 return (1UL << flsl(x - 1)); 107 } 108 109 static __inline int 110 is_power_of_2(unsigned long n) 111 { 112 return (n == roundup_pow_of_two(n)); 113 } 114 115 static __inline unsigned long 116 rounddown_pow_of_two(unsigned long x) 117 { 118 return (1UL << (flsl(x) - 1)); 119 } 120 121 #define max_t(type, val1, val2) \ 122 ((type)(val1) > (type)(val2) ? (type)(val1) : (val2)) 123 #define min_t(type, val1, val2) \ 124 ((type)(val1) < (type)(val2) ? (type)(val1) : (val2)) 125 126 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) 127 #define nothing do {} while(0) 128 #define BUILD_BUG_ON(cond) nothing 129 130 #endif /* #ifndef QLNX_RDMA */ 131 132 #define OSAL_CPU_TO_BE64(val) htobe64(val) 133 #define OSAL_BE64_TO_CPU(val) be64toh(val) 134 135 #define OSAL_CPU_TO_BE32(val) htobe32(val) 136 #define OSAL_BE32_TO_CPU(val) be32toh(val) 137 138 #define OSAL_CPU_TO_LE32(val) htole32(val) 139 #define OSAL_LE32_TO_CPU(val) le32toh(val) 140 141 #define OSAL_CPU_TO_BE16(val) htobe16(val) 142 #define OSAL_BE16_TO_CPU(val) be16toh(val) 143 144 #define OSAL_CPU_TO_LE16(val) htole16(val) 145 #define OSAL_LE16_TO_CPU(val) le16toh(val) 146 147 static __inline uint32_t 148 qlnx_get_cache_line_size(void) 149 { 150 return (CACHE_LINE_SIZE); 151 } 152 153 #define OSAL_CACHE_LINE_SIZE qlnx_get_cache_line_size() 154 155 #define OSAL_BE32 uint32_t 156 #define dma_addr_t bus_addr_t 157 #define osal_size_t size_t 158 159 typedef struct mtx osal_spinlock_t; 160 typedef struct mtx osal_mutex_t; 161 162 typedef void * osal_dpc_t; 163 164 typedef struct _osal_list_entry_t 165 { 166 struct _osal_list_entry_t *next, *prev; 167 } osal_list_entry_t; 168 169 typedef struct osal_list_t 170 { 171 osal_list_entry_t *head, *tail; 172 unsigned long cnt; 173 } osal_list_t; 174 175 /* OSAL functions */ 176 177 #define OSAL_UDELAY(time) DELAY(time) 178 #define OSAL_MSLEEP(time) qlnx_mdelay(__func__, time) 179 180 #define OSAL_ALLOC(dev, GFP, size) qlnx_zalloc(size) 181 #define OSAL_ZALLOC(dev, GFP, size) qlnx_zalloc(size) 182 #define OSAL_VALLOC(dev, size) qlnx_zalloc(size) 183 #define OSAL_VZALLOC(dev, size) qlnx_zalloc(size) 184 185 #define OSAL_FREE(dev, memory) free(memory, M_QLNXBUF) 186 #define OSAL_VFREE(dev, memory) free(memory, M_QLNXBUF) 187 188 #define OSAL_MEM_ZERO(mem, size) bzero(mem, size) 189 190 #define OSAL_MEMCPY(dst, src, size) memcpy(dst, src, size) 191 192 #define OSAL_DMA_ALLOC_COHERENT(dev, phys, size) \ 193 qlnx_dma_alloc_coherent(dev, phys, size) 194 195 #define OSAL_DMA_FREE_COHERENT(dev, virt, phys, size) \ 196 qlnx_dma_free_coherent(dev, virt, phys, size) 197 #define OSAL_VF_CQE_COMPLETION(_dev_p, _cqe, _protocol) (0) 198 199 #define REG_WR(hwfn, addr, val) qlnx_reg_wr32(hwfn, addr, val) 200 #define REG_WR16(hwfn, addr, val) qlnx_reg_wr16(hwfn, addr, val) 201 #define DIRECT_REG_WR(p_hwfn, addr, value) qlnx_direct_reg_wr32(p_hwfn, addr, value) 202 #define DIRECT_REG_RD(p_hwfn, addr) qlnx_direct_reg_rd32(p_hwfn, addr) 203 #define REG_RD(hwfn, addr) qlnx_reg_rd32(hwfn, addr) 204 #define DOORBELL(hwfn, addr, value) \ 205 qlnx_dbell_wr32(hwfn, addr, value) 206 207 #define OSAL_SPIN_LOCK_ALLOC(p_hwfn, mutex) 208 #define OSAL_SPIN_LOCK_DEALLOC(mutex) mtx_destroy(mutex) 209 #define OSAL_SPIN_LOCK_INIT(lock) {\ 210 mtx_init(lock, __func__, MTX_NETWORK_LOCK, MTX_SPIN); \ 211 } 212 213 #define OSAL_SPIN_UNLOCK(lock) {\ 214 mtx_unlock(lock); \ 215 } 216 #define OSAL_SPIN_LOCK(lock) {\ 217 mtx_lock(lock); \ 218 } 219 220 #define OSAL_MUTEX_ALLOC(p_hwfn, mutex) 221 #define OSAL_MUTEX_DEALLOC(mutex) mtx_destroy(mutex) 222 #define OSAL_MUTEX_INIT(lock) {\ 223 mtx_init(lock, __func__, MTX_NETWORK_LOCK, MTX_DEF);\ 224 } 225 226 #define OSAL_MUTEX_ACQUIRE(lock) mtx_lock(lock) 227 #define OSAL_MUTEX_RELEASE(lock) mtx_unlock(lock) 228 229 #define OSAL_DPC_ALLOC(hwfn) malloc(PAGE_SIZE, M_QLNXBUF, M_NOWAIT) 230 #define OSAL_DPC_INIT(dpc, hwfn) nothing 231 #define OSAL_SCHEDULE_RECOVERY_HANDLER(x) nothing 232 #define OSAL_HW_ERROR_OCCURRED(hwfn, err_type) nothing 233 #define OSAL_DPC_SYNC(hwfn) nothing 234 235 static inline void OSAL_DCBX_AEN(void *p_hwfn, u32 mib_type) 236 { 237 return; 238 } 239 240 static inline bool OSAL_NVM_IS_ACCESS_ENABLED(void *p_hwfn) 241 { 242 return 1; 243 } 244 245 #define OSAL_LIST_INIT(list) \ 246 do { \ 247 (list)->head = NULL; \ 248 (list)->tail = NULL; \ 249 (list)->cnt = 0; \ 250 } while (0) 251 252 #define OSAL_LIST_INSERT_ENTRY_AFTER(entry, entry_prev, list) \ 253 do { \ 254 (entry)->prev = (entry_prev); \ 255 (entry)->next = (entry_prev)->next; \ 256 (entry)->next->prev = (entry); \ 257 (entry_prev)->next = (entry); \ 258 (list)->cnt++; \ 259 } while (0); 260 261 #define OSAL_LIST_SPLICE_TAIL_INIT(new_list, list) \ 262 do { \ 263 ((new_list)->tail)->next = ((list)->head); \ 264 ((list)->head)->prev = ((new_list)->tail); \ 265 (list)->head = (new_list)->head; \ 266 (list)->cnt = (list)->cnt + (new_list)->cnt; \ 267 OSAL_LIST_INIT(new_list); \ 268 } while (0); 269 270 #define OSAL_LIST_PUSH_HEAD(entry, list) \ 271 do { \ 272 (entry)->prev = (osal_list_entry_t *)0; \ 273 (entry)->next = (list)->head; \ 274 if ((list)->tail == (osal_list_entry_t *)0) { \ 275 (list)->tail = (entry); \ 276 } else { \ 277 (list)->head->prev = (entry); \ 278 } \ 279 (list)->head = (entry); \ 280 (list)->cnt++; \ 281 } while (0) 282 283 #define OSAL_LIST_PUSH_TAIL(entry, list) \ 284 do { \ 285 (entry)->next = (osal_list_entry_t *)0; \ 286 (entry)->prev = (list)->tail; \ 287 if ((list)->tail) { \ 288 (list)->tail->next = (entry); \ 289 } else { \ 290 (list)->head = (entry); \ 291 } \ 292 (list)->tail = (entry); \ 293 (list)->cnt++; \ 294 } while (0) 295 296 #define OSAL_LIST_FIRST_ENTRY(list, type, field) \ 297 (type *)((list)->head) 298 299 #define OSAL_LIST_REMOVE_ENTRY(entry, list) \ 300 do { \ 301 if ((list)->head == (entry)) { \ 302 if ((list)->head) { \ 303 (list)->head = (list)->head->next; \ 304 if ((list)->head) { \ 305 (list)->head->prev = (osal_list_entry_t *)0; \ 306 } else { \ 307 (list)->tail = (osal_list_entry_t *)0; \ 308 } \ 309 (list)->cnt--; \ 310 } \ 311 } else if ((list)->tail == (entry)) { \ 312 if ((list)->tail) { \ 313 (list)->tail = (list)->tail->prev; \ 314 if ((list)->tail) { \ 315 (list)->tail->next = (osal_list_entry_t *)0; \ 316 } else { \ 317 (list)->head = (osal_list_entry_t *)0; \ 318 } \ 319 (list)->cnt--; \ 320 } \ 321 } else { \ 322 (entry)->prev->next = (entry)->next; \ 323 (entry)->next->prev = (entry)->prev; \ 324 (list)->cnt--; \ 325 } \ 326 } while (0) 327 328 329 #define OSAL_LIST_IS_EMPTY(list) \ 330 ((list)->cnt == 0) 331 332 #define OSAL_LIST_NEXT(entry, field, type) \ 333 (type *)((&((entry)->field))->next) 334 335 #define OSAL_LIST_FOR_EACH_ENTRY(entry, list, field, type) \ 336 for (entry = OSAL_LIST_FIRST_ENTRY(list, type, field); \ 337 entry; \ 338 entry = OSAL_LIST_NEXT(entry, field, type)) 339 340 #define OSAL_LIST_FOR_EACH_ENTRY_SAFE(entry, tmp_entry, list, field, type) \ 341 for (entry = OSAL_LIST_FIRST_ENTRY(list, type, field), \ 342 tmp_entry = (entry) ? OSAL_LIST_NEXT(entry, field, type) : NULL; \ 343 entry != NULL; \ 344 entry = (type *)tmp_entry, \ 345 tmp_entry = (entry) ? OSAL_LIST_NEXT(entry, field, type) : NULL) 346 347 348 #define OSAL_BAR_SIZE(dev, bar_id) qlnx_pci_bus_get_bar_size(dev, bar_id) 349 350 #define OSAL_PCI_READ_CONFIG_BYTE(dev, reg, value) \ 351 qlnx_pci_read_config_byte(dev, reg, value); 352 #define OSAL_PCI_READ_CONFIG_WORD(dev, reg, value) \ 353 qlnx_pci_read_config_word(dev, reg, value); 354 #define OSAL_PCI_READ_CONFIG_DWORD(dev, reg, value) \ 355 qlnx_pci_read_config_dword(dev, reg, value); 356 357 #define OSAL_PCI_WRITE_CONFIG_BYTE(dev, reg, value) \ 358 qlnx_pci_write_config_byte(dev, reg, value); 359 #define OSAL_PCI_WRITE_CONFIG_WORD(dev, reg, value) \ 360 qlnx_pci_write_config_word(dev, reg, value); 361 #define OSAL_PCI_WRITE_CONFIG_DWORD(dev, reg, value) \ 362 qlnx_pci_write_config_dword(dev, reg, value); 363 364 #define OSAL_PCI_FIND_CAPABILITY(dev, cap) qlnx_pci_find_capability(dev, cap); 365 366 #define OSAL_MMIOWB(dev) qlnx_barrier(dev) 367 #define OSAL_BARRIER(dev) qlnx_barrier(dev) 368 369 #define OSAL_SMP_MB(dev) mb() 370 #define OSAL_SMP_RMB(dev) rmb() 371 #define OSAL_SMP_WMB(dev) wmb() 372 #define OSAL_RMB(dev) rmb() 373 #define OSAL_WMB(dev) wmb() 374 #define OSAL_DMA_SYNC(dev, addr, length, is_post) 375 376 #define OSAL_FIND_FIRST_BIT find_first_bit 377 #define OSAL_SET_BIT(bit, bitmap) bit_set((bitstr_t *)bitmap, bit) 378 #define OSAL_CLEAR_BIT(bit, bitmap) bit_clear((bitstr_t *)bitmap, bit) 379 #define OSAL_TEST_BIT(bit, bitmap) bit_test((bitstr_t *)bitmap, bit) 380 #define OSAL_FIND_FIRST_ZERO_BIT(bitmap, length) \ 381 find_first_zero_bit(bitmap, length) 382 383 #define OSAL_LINK_UPDATE(hwfn) qlnx_link_update(hwfn) 384 #define OSAL_VF_FLR_UPDATE(hwfn) 385 386 #define QLNX_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) 387 #define QLNX_ROUNDUP(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) 388 389 #define OSAL_NUM_ACTIVE_CPU() mp_ncpus 390 391 #ifndef DIV_ROUND_UP 392 #define DIV_ROUND_UP(size, to_what) QLNX_DIV_ROUND_UP((size), (to_what)) 393 #endif 394 395 #define ROUNDUP(value, to_what) QLNX_ROUNDUP((value), (to_what)) 396 397 #define OSAL_ROUNDUP_POW_OF_TWO(val) roundup_pow_of_two((val)) 398 399 static __inline uint32_t 400 qlnx_log2(uint32_t x) 401 { 402 uint32_t log = 0; 403 404 while (x >>= 1) log++; 405 406 return (log); 407 } 408 409 #define OSAL_LOG2(val) qlnx_log2(val) 410 #define OFFSETOF(str, field) offsetof(str, field) 411 #define PRINT device_printf 412 #define PRINT_ERR device_printf 413 #define OSAL_ASSERT(is_assert) nothing 414 #define OSAL_BEFORE_PF_START(cdev, my_id) {}; 415 #define OSAL_AFTER_PF_STOP(cdev, my_id) {}; 416 417 #define INLINE __inline 418 #define OSAL_INLINE __inline 419 #define OSAL_UNLIKELY 420 #define OSAL_NULL NULL 421 422 423 #define OSAL_MAX_T(type, __max1, __max2) max_t(type, __max1, __max2) 424 #define OSAL_MIN_T(type, __max1, __max2) min_t(type, __max1, __max2) 425 426 #define __iomem 427 #define OSAL_IOMEM 428 429 #define int_ptr_t void * 430 #define osal_int_ptr_t void * 431 #define OSAL_BUILD_BUG_ON(cond) nothing 432 #define REG_ADDR(hwfn, offset) (void *)((u8 *)(hwfn->regview) + (offset)) 433 #define OSAL_REG_ADDR(hwfn, offset) (void *)((u8 *)(hwfn->regview) + (offset)) 434 435 #define OSAL_PAGE_SIZE PAGE_SIZE 436 437 #define OSAL_STRCPY(dst, src) strcpy(dst, src) 438 #define OSAL_STRNCPY(dst, src, bytes) strncpy(dst, src, bytes) 439 #define OSAL_STRLEN(src) strlen(src) 440 #define OSAL_SPRINTF sprintf 441 #define OSAL_SNPRINTF snprintf 442 #define OSAL_MEMSET memset 443 #define OSAL_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) 444 #define osal_uintptr_t u64 445 446 #define OSAL_SLOWPATH_IRQ_REQ(p_hwfn) (0) 447 #define OSAL_GET_PROTOCOL_STATS(p_hwfn, type, stats) \ 448 qlnx_get_protocol_stats(p_hwfn, type, stats); 449 #define OSAL_POLL_MODE_DPC(hwfn) {if (cold) qlnx_sp_isr(hwfn);} 450 #define OSAL_WARN(cond, fmt, args...) \ 451 if (cond) printf("%s: WARNING: " fmt, __func__, ## args); 452 453 #define OSAL_BITMAP_WEIGHT(bitmap, nbits) bitmap_weight(bitmap, nbits) 454 #define OSAL_GET_RDMA_SB_ID(p_hwfn, cnq_id) ecore_rdma_get_sb_id(p_hwfn, cnq_id) 455 456 static inline int 457 qlnx_test_and_change_bit(long bit, volatile unsigned long *var) 458 { 459 long val; 460 461 var += BIT_WORD(bit); 462 bit %= BITS_PER_LONG; 463 bit = (1UL << bit); 464 465 val = *var; 466 467 #if __FreeBSD_version >= 1100000 468 if (val & bit) 469 return (test_and_clear_bit(bit, var)); 470 471 return (test_and_set_bit(bit, var)); 472 #else 473 if (val & bit) 474 return (test_and_clear_bit(bit, (long *)var)); 475 476 return (test_and_set_bit(bit, (long *)var)); 477 478 #endif 479 } 480 481 #if __FreeBSD_version < 1100000 482 static inline unsigned 483 bitmap_weight(unsigned long *bitmap, unsigned nbits) 484 { 485 unsigned bit; 486 unsigned retval = 0; 487 488 for_each_set_bit(bit, bitmap, nbits) 489 retval++; 490 return (retval); 491 } 492 493 #endif 494 495 496 #define OSAL_TEST_AND_FLIP_BIT qlnx_test_and_change_bit 497 #define OSAL_TEST_AND_CLEAR_BIT test_and_clear_bit 498 #define OSAL_MEMCMP memcmp 499 #define OSAL_SPIN_LOCK_IRQSAVE(x,y) {y=0; mtx_lock(x);} 500 #define OSAL_SPIN_UNLOCK_IRQSAVE(x,y) {y= 0; mtx_unlock(x);} 501 502 static inline u32 503 OSAL_CRC32(u32 crc, u8 *ptr, u32 length) 504 { 505 int i; 506 507 while (length--) { 508 crc ^= *ptr++; 509 for (i = 0; i < 8; i++) 510 crc = (crc >> 1) ^ ((crc & 1) ? 0xedb88320 : 0); 511 } 512 return crc; 513 } 514 515 static inline void 516 OSAL_CRC8_POPULATE(u8 * cdu_crc8_table, u8 polynomial) 517 { 518 return; 519 } 520 521 static inline u8 522 OSAL_CRC8(u8 * cdu_crc8_table, u8 * data_to_crc, int data_to_crc_len, u8 init_value) 523 { 524 return ECORE_NOTIMPL; 525 } 526 527 #define OSAL_HW_INFO_CHANGE(p_hwfn, offset) 528 #define OSAL_MFW_TLV_REQ(p_hwfn) 529 #define OSAL_VF_FILL_ACQUIRE_RESC_REQ(p_hwfn, req, vf_sw_info) {}; 530 #define OSAL_VF_UPDATE_ACQUIRE_RESC_RESP(p_hwfn, res) (0) 531 532 #endif /* #ifdef ECORE_PACKAGE */ 533 534 #endif /* #ifdef __BCM_OSAL_ECORE_PACKAGE */ 535