1 /* 2 * Copyright (c) 2010 Broadcom Corporation 3 * 4 * Permission to use, copy, modify, and/or distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #include <linux/types.h> 18 #include <linux/atomic.h> 19 #include <linux/kernel.h> 20 #include <linux/kthread.h> 21 #include <linux/printk.h> 22 #include <linux/pci_ids.h> 23 #include <linux/netdevice.h> 24 #include <linux/interrupt.h> 25 #include <linux/sched/signal.h> 26 #include <linux/mmc/sdio.h> 27 #include <linux/mmc/sdio_ids.h> 28 #include <linux/mmc/sdio_func.h> 29 #include <linux/mmc/card.h> 30 #include <linux/semaphore.h> 31 #include <linux/firmware.h> 32 #include <linux/module.h> 33 #include <linux/bcma/bcma.h> 34 #include <linux/debugfs.h> 35 #include <linux/vmalloc.h> 36 #include <asm/unaligned.h> 37 #include <defs.h> 38 #include <brcmu_wifi.h> 39 #include <brcmu_utils.h> 40 #include <brcm_hw_ids.h> 41 #include <soc.h> 42 #include "sdio.h" 43 #include "chip.h" 44 #include "firmware.h" 45 #include "core.h" 46 #include "common.h" 47 #include "bcdc.h" 48 49 #define DCMD_RESP_TIMEOUT msecs_to_jiffies(2500) 50 #define CTL_DONE_TIMEOUT msecs_to_jiffies(2500) 51 52 /* watermark expressed in number of words */ 53 #define DEFAULT_F2_WATERMARK 0x8 54 #define CY_4373_F2_WATERMARK 0x40 55 #define CY_43012_F2_WATERMARK 0x60 56 57 #ifdef DEBUG 58 59 #define BRCMF_TRAP_INFO_SIZE 80 60 61 #define CBUF_LEN (128) 62 63 /* Device console log buffer state */ 64 #define CONSOLE_BUFFER_MAX 2024 65 66 struct rte_log_le { 67 __le32 buf; /* Can't be pointer on (64-bit) hosts */ 68 __le32 buf_size; 69 __le32 idx; 70 char *_buf_compat; /* Redundant pointer for backward compat. */ 71 }; 72 73 struct rte_console { 74 /* Virtual UART 75 * When there is no UART (e.g. Quickturn), 76 * the host should write a complete 77 * input line directly into cbuf and then write 78 * the length into vcons_in. 79 * This may also be used when there is a real UART 80 * (at risk of conflicting with 81 * the real UART). vcons_out is currently unused. 82 */ 83 uint vcons_in; 84 uint vcons_out; 85 86 /* Output (logging) buffer 87 * Console output is written to a ring buffer log_buf at index log_idx. 88 * The host may read the output when it sees log_idx advance. 89 * Output will be lost if the output wraps around faster than the host 90 * polls. 91 */ 92 struct rte_log_le log_le; 93 94 /* Console input line buffer 95 * Characters are read one at a time into cbuf 96 * until <CR> is received, then 97 * the buffer is processed as a command line. 98 * Also used for virtual UART. 99 */ 100 uint cbuf_idx; 101 char cbuf[CBUF_LEN]; 102 }; 103 104 #endif /* DEBUG */ 105 #include <chipcommon.h> 106 107 #include "bus.h" 108 #include "debug.h" 109 #include "tracepoint.h" 110 111 #define TXQLEN 2048 /* bulk tx queue length */ 112 #define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */ 113 #define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */ 114 #define PRIOMASK 7 115 116 #define TXRETRIES 2 /* # of retries for tx frames */ 117 118 #define BRCMF_RXBOUND 50 /* Default for max rx frames in 119 one scheduling */ 120 121 #define BRCMF_TXBOUND 20 /* Default for max tx frames in 122 one scheduling */ 123 124 #define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */ 125 126 #define MEMBLOCK 2048 /* Block size used for downloading 127 of dongle image */ 128 #define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold 129 biggest possible glom */ 130 131 #define BRCMF_FIRSTREAD (1 << 6) 132 133 #define BRCMF_CONSOLE 10 /* watchdog interval to poll console */ 134 135 /* SBSDIO_DEVICE_CTL */ 136 137 /* 1: device will assert busy signal when receiving CMD53 */ 138 #define SBSDIO_DEVCTL_SETBUSY 0x01 139 /* 1: assertion of sdio interrupt is synchronous to the sdio clock */ 140 #define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02 141 /* 1: mask all interrupts to host except the chipActive (rev 8) */ 142 #define SBSDIO_DEVCTL_CA_INT_ONLY 0x04 143 /* 1: isolate internal sdio signals, put external pads in tri-state; requires 144 * sdio bus power cycle to clear (rev 9) */ 145 #define SBSDIO_DEVCTL_PADS_ISO 0x08 146 /* 1: enable F2 Watermark */ 147 #define SBSDIO_DEVCTL_F2WM_ENAB 0x10 148 /* Force SD->SB reset mapping (rev 11) */ 149 #define SBSDIO_DEVCTL_SB_RST_CTL 0x30 150 /* Determined by CoreControl bit */ 151 #define SBSDIO_DEVCTL_RST_CORECTL 0x00 152 /* Force backplane reset */ 153 #define SBSDIO_DEVCTL_RST_BPRESET 0x10 154 /* Force no backplane reset */ 155 #define SBSDIO_DEVCTL_RST_NOBPRESET 0x20 156 157 /* direct(mapped) cis space */ 158 159 /* MAPPED common CIS address */ 160 #define SBSDIO_CIS_BASE_COMMON 0x1000 161 /* maximum bytes in one CIS */ 162 #define SBSDIO_CIS_SIZE_LIMIT 0x200 163 /* cis offset addr is < 17 bits */ 164 #define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF 165 166 /* manfid tuple length, include tuple, link bytes */ 167 #define SBSDIO_CIS_MANFID_TUPLE_LEN 6 168 169 #define SD_REG(field) \ 170 (offsetof(struct sdpcmd_regs, field)) 171 172 /* SDIO function 1 register CHIPCLKCSR */ 173 /* Force ALP request to backplane */ 174 #define SBSDIO_FORCE_ALP 0x01 175 /* Force HT request to backplane */ 176 #define SBSDIO_FORCE_HT 0x02 177 /* Force ILP request to backplane */ 178 #define SBSDIO_FORCE_ILP 0x04 179 /* Make ALP ready (power up xtal) */ 180 #define SBSDIO_ALP_AVAIL_REQ 0x08 181 /* Make HT ready (power up PLL) */ 182 #define SBSDIO_HT_AVAIL_REQ 0x10 183 /* Squelch clock requests from HW */ 184 #define SBSDIO_FORCE_HW_CLKREQ_OFF 0x20 185 /* Status: ALP is ready */ 186 #define SBSDIO_ALP_AVAIL 0x40 187 /* Status: HT is ready */ 188 #define SBSDIO_HT_AVAIL 0x80 189 #define SBSDIO_CSR_MASK 0x1F 190 #define SBSDIO_AVBITS (SBSDIO_HT_AVAIL | SBSDIO_ALP_AVAIL) 191 #define SBSDIO_ALPAV(regval) ((regval) & SBSDIO_AVBITS) 192 #define SBSDIO_HTAV(regval) (((regval) & SBSDIO_AVBITS) == SBSDIO_AVBITS) 193 #define SBSDIO_ALPONLY(regval) (SBSDIO_ALPAV(regval) && !SBSDIO_HTAV(regval)) 194 #define SBSDIO_CLKAV(regval, alponly) \ 195 (SBSDIO_ALPAV(regval) && (alponly ? 1 : SBSDIO_HTAV(regval))) 196 197 /* intstatus */ 198 #define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */ 199 #define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */ 200 #define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */ 201 #define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */ 202 #define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */ 203 #define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */ 204 #define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */ 205 #define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */ 206 #define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */ 207 #define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */ 208 #define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */ 209 #define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */ 210 #define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */ 211 #define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */ 212 #define I_PC (1 << 10) /* descriptor error */ 213 #define I_PD (1 << 11) /* data error */ 214 #define I_DE (1 << 12) /* Descriptor protocol Error */ 215 #define I_RU (1 << 13) /* Receive descriptor Underflow */ 216 #define I_RO (1 << 14) /* Receive fifo Overflow */ 217 #define I_XU (1 << 15) /* Transmit fifo Underflow */ 218 #define I_RI (1 << 16) /* Receive Interrupt */ 219 #define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */ 220 #define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */ 221 #define I_XI (1 << 24) /* Transmit Interrupt */ 222 #define I_RF_TERM (1 << 25) /* Read Frame Terminate */ 223 #define I_WF_TERM (1 << 26) /* Write Frame Terminate */ 224 #define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */ 225 #define I_SBINT (1 << 28) /* sbintstatus Interrupt */ 226 #define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */ 227 #define I_SRESET (1 << 30) /* CCCR RES interrupt */ 228 #define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */ 229 #define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU) 230 #define I_DMA (I_RI | I_XI | I_ERRORS) 231 232 /* corecontrol */ 233 #define CC_CISRDY (1 << 0) /* CIS Ready */ 234 #define CC_BPRESEN (1 << 1) /* CCCR RES signal */ 235 #define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */ 236 #define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */ 237 #define CC_XMTDATAAVAIL_MODE (1 << 4) 238 #define CC_XMTDATAAVAIL_CTRL (1 << 5) 239 240 /* SDA_FRAMECTRL */ 241 #define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */ 242 #define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */ 243 #define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */ 244 #define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */ 245 246 /* 247 * Software allocation of To SB Mailbox resources 248 */ 249 250 /* tosbmailbox bits corresponding to intstatus bits */ 251 #define SMB_NAK (1 << 0) /* Frame NAK */ 252 #define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */ 253 #define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */ 254 #define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */ 255 256 /* tosbmailboxdata */ 257 #define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */ 258 259 /* 260 * Software allocation of To Host Mailbox resources 261 */ 262 263 /* intstatus bits */ 264 #define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */ 265 #define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */ 266 #define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */ 267 #define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */ 268 269 /* tohostmailboxdata */ 270 #define HMB_DATA_NAKHANDLED 0x0001 /* retransmit NAK'd frame */ 271 #define HMB_DATA_DEVREADY 0x0002 /* talk to host after enable */ 272 #define HMB_DATA_FC 0x0004 /* per prio flowcontrol update flag */ 273 #define HMB_DATA_FWREADY 0x0008 /* fw ready for protocol activity */ 274 #define HMB_DATA_FWHALT 0x0010 /* firmware halted */ 275 276 #define HMB_DATA_FCDATA_MASK 0xff000000 277 #define HMB_DATA_FCDATA_SHIFT 24 278 279 #define HMB_DATA_VERSION_MASK 0x00ff0000 280 #define HMB_DATA_VERSION_SHIFT 16 281 282 /* 283 * Software-defined protocol header 284 */ 285 286 /* Current protocol version */ 287 #define SDPCM_PROT_VERSION 4 288 289 /* 290 * Shared structure between dongle and the host. 291 * The structure contains pointers to trap or assert information. 292 */ 293 #define SDPCM_SHARED_VERSION 0x0003 294 #define SDPCM_SHARED_VERSION_MASK 0x00FF 295 #define SDPCM_SHARED_ASSERT_BUILT 0x0100 296 #define SDPCM_SHARED_ASSERT 0x0200 297 #define SDPCM_SHARED_TRAP 0x0400 298 299 /* Space for header read, limit for data packets */ 300 #define MAX_HDR_READ (1 << 6) 301 #define MAX_RX_DATASZ 2048 302 303 /* Bump up limit on waiting for HT to account for first startup; 304 * if the image is doing a CRC calculation before programming the PMU 305 * for HT availability, it could take a couple hundred ms more, so 306 * max out at a 1 second (1000000us). 307 */ 308 #undef PMU_MAX_TRANSITION_DLY 309 #define PMU_MAX_TRANSITION_DLY 1000000 310 311 /* Value for ChipClockCSR during initial setup */ 312 #define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \ 313 SBSDIO_ALP_AVAIL_REQ) 314 315 /* Flags for SDH calls */ 316 #define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED) 317 318 #define BRCMF_IDLE_ACTIVE 0 /* Do not request any SD clock change 319 * when idle 320 */ 321 #define BRCMF_IDLE_INTERVAL 1 322 323 #define KSO_WAIT_US 50 324 #define MAX_KSO_ATTEMPTS (PMU_MAX_TRANSITION_DLY/KSO_WAIT_US) 325 #define BRCMF_SDIO_MAX_ACCESS_ERRORS 5 326 327 /* 328 * Conversion of 802.1D priority to precedence level 329 */ 330 static uint prio2prec(u32 prio) 331 { 332 return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ? 333 (prio^2) : prio; 334 } 335 336 #ifdef DEBUG 337 /* Device console log buffer state */ 338 struct brcmf_console { 339 uint count; /* Poll interval msec counter */ 340 uint log_addr; /* Log struct address (fixed) */ 341 struct rte_log_le log_le; /* Log struct (host copy) */ 342 uint bufsize; /* Size of log buffer */ 343 u8 *buf; /* Log buffer (host copy) */ 344 uint last; /* Last buffer read index */ 345 }; 346 347 struct brcmf_trap_info { 348 __le32 type; 349 __le32 epc; 350 __le32 cpsr; 351 __le32 spsr; 352 __le32 r0; /* a1 */ 353 __le32 r1; /* a2 */ 354 __le32 r2; /* a3 */ 355 __le32 r3; /* a4 */ 356 __le32 r4; /* v1 */ 357 __le32 r5; /* v2 */ 358 __le32 r6; /* v3 */ 359 __le32 r7; /* v4 */ 360 __le32 r8; /* v5 */ 361 __le32 r9; /* sb/v6 */ 362 __le32 r10; /* sl/v7 */ 363 __le32 r11; /* fp/v8 */ 364 __le32 r12; /* ip */ 365 __le32 r13; /* sp */ 366 __le32 r14; /* lr */ 367 __le32 pc; /* r15 */ 368 }; 369 #endif /* DEBUG */ 370 371 struct sdpcm_shared { 372 u32 flags; 373 u32 trap_addr; 374 u32 assert_exp_addr; 375 u32 assert_file_addr; 376 u32 assert_line; 377 u32 console_addr; /* Address of struct rte_console */ 378 u32 msgtrace_addr; 379 u8 tag[32]; 380 u32 brpt_addr; 381 }; 382 383 struct sdpcm_shared_le { 384 __le32 flags; 385 __le32 trap_addr; 386 __le32 assert_exp_addr; 387 __le32 assert_file_addr; 388 __le32 assert_line; 389 __le32 console_addr; /* Address of struct rte_console */ 390 __le32 msgtrace_addr; 391 u8 tag[32]; 392 __le32 brpt_addr; 393 }; 394 395 /* dongle SDIO bus specific header info */ 396 struct brcmf_sdio_hdrinfo { 397 u8 seq_num; 398 u8 channel; 399 u16 len; 400 u16 len_left; 401 u16 len_nxtfrm; 402 u8 dat_offset; 403 bool lastfrm; 404 u16 tail_pad; 405 }; 406 407 /* 408 * hold counter variables 409 */ 410 struct brcmf_sdio_count { 411 uint intrcount; /* Count of device interrupt callbacks */ 412 uint lastintrs; /* Count as of last watchdog timer */ 413 uint pollcnt; /* Count of active polls */ 414 uint regfails; /* Count of R_REG failures */ 415 uint tx_sderrs; /* Count of tx attempts with sd errors */ 416 uint fcqueued; /* Tx packets that got queued */ 417 uint rxrtx; /* Count of rtx requests (NAK to dongle) */ 418 uint rx_toolong; /* Receive frames too long to receive */ 419 uint rxc_errors; /* SDIO errors when reading control frames */ 420 uint rx_hdrfail; /* SDIO errors on header reads */ 421 uint rx_badhdr; /* Bad received headers (roosync?) */ 422 uint rx_badseq; /* Mismatched rx sequence number */ 423 uint fc_rcvd; /* Number of flow-control events received */ 424 uint fc_xoff; /* Number which turned on flow-control */ 425 uint fc_xon; /* Number which turned off flow-control */ 426 uint rxglomfail; /* Failed deglom attempts */ 427 uint rxglomframes; /* Number of glom frames (superframes) */ 428 uint rxglompkts; /* Number of packets from glom frames */ 429 uint f2rxhdrs; /* Number of header reads */ 430 uint f2rxdata; /* Number of frame data reads */ 431 uint f2txdata; /* Number of f2 frame writes */ 432 uint f1regdata; /* Number of f1 register accesses */ 433 uint tickcnt; /* Number of watchdog been schedule */ 434 ulong tx_ctlerrs; /* Err of sending ctrl frames */ 435 ulong tx_ctlpkts; /* Ctrl frames sent to dongle */ 436 ulong rx_ctlerrs; /* Err of processing rx ctrl frames */ 437 ulong rx_ctlpkts; /* Ctrl frames processed from dongle */ 438 ulong rx_readahead_cnt; /* packets where header read-ahead was used */ 439 }; 440 441 /* misc chip info needed by some of the routines */ 442 /* Private data for SDIO bus interaction */ 443 struct brcmf_sdio { 444 struct brcmf_sdio_dev *sdiodev; /* sdio device handler */ 445 struct brcmf_chip *ci; /* Chip info struct */ 446 struct brcmf_core *sdio_core; /* sdio core info struct */ 447 448 u32 hostintmask; /* Copy of Host Interrupt Mask */ 449 atomic_t intstatus; /* Intstatus bits (events) pending */ 450 atomic_t fcstate; /* State of dongle flow-control */ 451 452 uint blocksize; /* Block size of SDIO transfers */ 453 uint roundup; /* Max roundup limit */ 454 455 struct pktq txq; /* Queue length used for flow-control */ 456 u8 flowcontrol; /* per prio flow control bitmask */ 457 u8 tx_seq; /* Transmit sequence number (next) */ 458 u8 tx_max; /* Maximum transmit sequence allowed */ 459 460 u8 *hdrbuf; /* buffer for handling rx frame */ 461 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */ 462 u8 rx_seq; /* Receive sequence number (expected) */ 463 struct brcmf_sdio_hdrinfo cur_read; 464 /* info of current read frame */ 465 bool rxskip; /* Skip receive (awaiting NAK ACK) */ 466 bool rxpending; /* Data frame pending in dongle */ 467 468 uint rxbound; /* Rx frames to read before resched */ 469 uint txbound; /* Tx frames to send before resched */ 470 uint txminmax; 471 472 struct sk_buff *glomd; /* Packet containing glomming descriptor */ 473 struct sk_buff_head glom; /* Packet list for glommed superframe */ 474 475 u8 *rxbuf; /* Buffer for receiving control packets */ 476 uint rxblen; /* Allocated length of rxbuf */ 477 u8 *rxctl; /* Aligned pointer into rxbuf */ 478 u8 *rxctl_orig; /* pointer for freeing rxctl */ 479 uint rxlen; /* Length of valid data in buffer */ 480 spinlock_t rxctl_lock; /* protection lock for ctrl frame resources */ 481 482 u8 sdpcm_ver; /* Bus protocol reported by dongle */ 483 484 bool intr; /* Use interrupts */ 485 bool poll; /* Use polling */ 486 atomic_t ipend; /* Device interrupt is pending */ 487 uint spurious; /* Count of spurious interrupts */ 488 uint pollrate; /* Ticks between device polls */ 489 uint polltick; /* Tick counter */ 490 491 #ifdef DEBUG 492 uint console_interval; 493 struct brcmf_console console; /* Console output polling support */ 494 uint console_addr; /* Console address from shared struct */ 495 #endif /* DEBUG */ 496 497 uint clkstate; /* State of sd and backplane clock(s) */ 498 s32 idletime; /* Control for activity timeout */ 499 s32 idlecount; /* Activity timeout counter */ 500 s32 idleclock; /* How to set bus driver when idle */ 501 bool rxflow_mode; /* Rx flow control mode */ 502 bool rxflow; /* Is rx flow control on */ 503 bool alp_only; /* Don't use HT clock (ALP only) */ 504 505 u8 *ctrl_frame_buf; 506 u16 ctrl_frame_len; 507 bool ctrl_frame_stat; 508 int ctrl_frame_err; 509 510 spinlock_t txq_lock; /* protect bus->txq */ 511 wait_queue_head_t ctrl_wait; 512 wait_queue_head_t dcmd_resp_wait; 513 514 struct timer_list timer; 515 struct completion watchdog_wait; 516 struct task_struct *watchdog_tsk; 517 bool wd_active; 518 519 struct workqueue_struct *brcmf_wq; 520 struct work_struct datawork; 521 bool dpc_triggered; 522 bool dpc_running; 523 524 bool txoff; /* Transmit flow-controlled */ 525 struct brcmf_sdio_count sdcnt; 526 bool sr_enabled; /* SaveRestore enabled */ 527 bool sleeping; 528 529 u8 tx_hdrlen; /* sdio bus header length for tx packet */ 530 bool txglom; /* host tx glomming enable flag */ 531 u16 head_align; /* buffer pointer alignment */ 532 u16 sgentry_align; /* scatter-gather buffer alignment */ 533 }; 534 535 /* clkstate */ 536 #define CLK_NONE 0 537 #define CLK_SDONLY 1 538 #define CLK_PENDING 2 539 #define CLK_AVAIL 3 540 541 #ifdef DEBUG 542 static int qcount[NUMPRIO]; 543 #endif /* DEBUG */ 544 545 #define DEFAULT_SDIO_DRIVE_STRENGTH 6 /* in milliamps */ 546 547 #define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL) 548 549 /* Limit on rounding up frames */ 550 static const uint max_roundup = 512; 551 552 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT 553 #define ALIGNMENT 8 554 #else 555 #define ALIGNMENT 4 556 #endif 557 558 enum brcmf_sdio_frmtype { 559 BRCMF_SDIO_FT_NORMAL, 560 BRCMF_SDIO_FT_SUPER, 561 BRCMF_SDIO_FT_SUB, 562 }; 563 564 #define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu)) 565 566 /* SDIO Pad drive strength to select value mappings */ 567 struct sdiod_drive_str { 568 u8 strength; /* Pad Drive Strength in mA */ 569 u8 sel; /* Chip-specific select value */ 570 }; 571 572 /* SDIO Drive Strength to sel value table for PMU Rev 11 (1.8V) */ 573 static const struct sdiod_drive_str sdiod_drvstr_tab1_1v8[] = { 574 {32, 0x6}, 575 {26, 0x7}, 576 {22, 0x4}, 577 {16, 0x5}, 578 {12, 0x2}, 579 {8, 0x3}, 580 {4, 0x0}, 581 {0, 0x1} 582 }; 583 584 /* SDIO Drive Strength to sel value table for PMU Rev 13 (1.8v) */ 585 static const struct sdiod_drive_str sdiod_drive_strength_tab5_1v8[] = { 586 {6, 0x7}, 587 {5, 0x6}, 588 {4, 0x5}, 589 {3, 0x4}, 590 {2, 0x2}, 591 {1, 0x1}, 592 {0, 0x0} 593 }; 594 595 /* SDIO Drive Strength to sel value table for PMU Rev 17 (1.8v) */ 596 static const struct sdiod_drive_str sdiod_drvstr_tab6_1v8[] = { 597 {3, 0x3}, 598 {2, 0x2}, 599 {1, 0x1}, 600 {0, 0x0} }; 601 602 /* SDIO Drive Strength to sel value table for 43143 PMU Rev 17 (3.3V) */ 603 static const struct sdiod_drive_str sdiod_drvstr_tab2_3v3[] = { 604 {16, 0x7}, 605 {12, 0x5}, 606 {8, 0x3}, 607 {4, 0x1} 608 }; 609 610 BRCMF_FW_DEF(43143, "brcmfmac43143-sdio"); 611 BRCMF_FW_DEF(43241B0, "brcmfmac43241b0-sdio"); 612 BRCMF_FW_DEF(43241B4, "brcmfmac43241b4-sdio"); 613 BRCMF_FW_DEF(43241B5, "brcmfmac43241b5-sdio"); 614 BRCMF_FW_DEF(4329, "brcmfmac4329-sdio"); 615 BRCMF_FW_DEF(4330, "brcmfmac4330-sdio"); 616 BRCMF_FW_DEF(4334, "brcmfmac4334-sdio"); 617 BRCMF_FW_DEF(43340, "brcmfmac43340-sdio"); 618 BRCMF_FW_DEF(4335, "brcmfmac4335-sdio"); 619 BRCMF_FW_DEF(43362, "brcmfmac43362-sdio"); 620 BRCMF_FW_DEF(4339, "brcmfmac4339-sdio"); 621 BRCMF_FW_DEF(43430A0, "brcmfmac43430a0-sdio"); 622 /* Note the names are not postfixed with a1 for backward compatibility */ 623 BRCMF_FW_DEF(43430A1, "brcmfmac43430-sdio"); 624 BRCMF_FW_DEF(43455, "brcmfmac43455-sdio"); 625 BRCMF_FW_DEF(43456, "brcmfmac43456-sdio"); 626 BRCMF_FW_DEF(4354, "brcmfmac4354-sdio"); 627 BRCMF_FW_DEF(4356, "brcmfmac4356-sdio"); 628 BRCMF_FW_DEF(4373, "brcmfmac4373-sdio"); 629 BRCMF_FW_DEF(43012, "brcmfmac43012-sdio"); 630 631 static const struct brcmf_firmware_mapping brcmf_sdio_fwnames[] = { 632 BRCMF_FW_ENTRY(BRCM_CC_43143_CHIP_ID, 0xFFFFFFFF, 43143), 633 BRCMF_FW_ENTRY(BRCM_CC_43241_CHIP_ID, 0x0000001F, 43241B0), 634 BRCMF_FW_ENTRY(BRCM_CC_43241_CHIP_ID, 0x00000020, 43241B4), 635 BRCMF_FW_ENTRY(BRCM_CC_43241_CHIP_ID, 0xFFFFFFC0, 43241B5), 636 BRCMF_FW_ENTRY(BRCM_CC_4329_CHIP_ID, 0xFFFFFFFF, 4329), 637 BRCMF_FW_ENTRY(BRCM_CC_4330_CHIP_ID, 0xFFFFFFFF, 4330), 638 BRCMF_FW_ENTRY(BRCM_CC_4334_CHIP_ID, 0xFFFFFFFF, 4334), 639 BRCMF_FW_ENTRY(BRCM_CC_43340_CHIP_ID, 0xFFFFFFFF, 43340), 640 BRCMF_FW_ENTRY(BRCM_CC_43341_CHIP_ID, 0xFFFFFFFF, 43340), 641 BRCMF_FW_ENTRY(BRCM_CC_4335_CHIP_ID, 0xFFFFFFFF, 4335), 642 BRCMF_FW_ENTRY(BRCM_CC_43362_CHIP_ID, 0xFFFFFFFE, 43362), 643 BRCMF_FW_ENTRY(BRCM_CC_4339_CHIP_ID, 0xFFFFFFFF, 4339), 644 BRCMF_FW_ENTRY(BRCM_CC_43430_CHIP_ID, 0x00000001, 43430A0), 645 BRCMF_FW_ENTRY(BRCM_CC_43430_CHIP_ID, 0xFFFFFFFE, 43430A1), 646 BRCMF_FW_ENTRY(BRCM_CC_4345_CHIP_ID, 0x00000200, 43456), 647 BRCMF_FW_ENTRY(BRCM_CC_4345_CHIP_ID, 0xFFFFFDC0, 43455), 648 BRCMF_FW_ENTRY(BRCM_CC_4354_CHIP_ID, 0xFFFFFFFF, 4354), 649 BRCMF_FW_ENTRY(BRCM_CC_4356_CHIP_ID, 0xFFFFFFFF, 4356), 650 BRCMF_FW_ENTRY(CY_CC_4373_CHIP_ID, 0xFFFFFFFF, 4373), 651 BRCMF_FW_ENTRY(CY_CC_43012_CHIP_ID, 0xFFFFFFFF, 43012) 652 }; 653 654 static void pkt_align(struct sk_buff *p, int len, int align) 655 { 656 uint datalign; 657 datalign = (unsigned long)(p->data); 658 datalign = roundup(datalign, (align)) - datalign; 659 if (datalign) 660 skb_pull(p, datalign); 661 __skb_trim(p, len); 662 } 663 664 /* To check if there's window offered */ 665 static bool data_ok(struct brcmf_sdio *bus) 666 { 667 return (u8)(bus->tx_max - bus->tx_seq) != 0 && 668 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0; 669 } 670 671 static int 672 brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on) 673 { 674 u8 wr_val = 0, rd_val, cmp_val, bmask; 675 int err = 0; 676 int err_cnt = 0; 677 int try_cnt = 0; 678 679 brcmf_dbg(TRACE, "Enter: on=%d\n", on); 680 681 wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT); 682 /* 1st KSO write goes to AOS wake up core if device is asleep */ 683 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, wr_val, &err); 684 685 /* In case of 43012 chip, the chip could go down immediately after 686 * KSO bit is cleared. So the further reads of KSO register could 687 * fail. Thereby just bailing out immediately after clearing KSO 688 * bit, to avoid polling of KSO bit. 689 */ 690 if (!on && bus->ci->chip == CY_CC_43012_CHIP_ID) 691 return err; 692 693 if (on) { 694 /* device WAKEUP through KSO: 695 * write bit 0 & read back until 696 * both bits 0 (kso bit) & 1 (dev on status) are set 697 */ 698 cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK | 699 SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK; 700 bmask = cmp_val; 701 usleep_range(2000, 3000); 702 } else { 703 /* Put device to sleep, turn off KSO */ 704 cmp_val = 0; 705 /* only check for bit0, bit1(dev on status) may not 706 * get cleared right away 707 */ 708 bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK; 709 } 710 711 do { 712 /* reliable KSO bit set/clr: 713 * the sdiod sleep write access is synced to PMU 32khz clk 714 * just one write attempt may fail, 715 * read it back until it matches written value 716 */ 717 rd_val = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, 718 &err); 719 if (!err) { 720 if ((rd_val & bmask) == cmp_val) 721 break; 722 err_cnt = 0; 723 } 724 /* bail out upon subsequent access errors */ 725 if (err && (err_cnt++ > BRCMF_SDIO_MAX_ACCESS_ERRORS)) 726 break; 727 728 udelay(KSO_WAIT_US); 729 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, wr_val, 730 &err); 731 732 } while (try_cnt++ < MAX_KSO_ATTEMPTS); 733 734 if (try_cnt > 2) 735 brcmf_dbg(SDIO, "try_cnt=%d rd_val=0x%x err=%d\n", try_cnt, 736 rd_val, err); 737 738 if (try_cnt > MAX_KSO_ATTEMPTS) 739 brcmf_err("max tries: rd_val=0x%x err=%d\n", rd_val, err); 740 741 return err; 742 } 743 744 #define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE) 745 746 /* Turn backplane clock on or off */ 747 static int brcmf_sdio_htclk(struct brcmf_sdio *bus, bool on, bool pendok) 748 { 749 int err; 750 u8 clkctl, clkreq, devctl; 751 unsigned long timeout; 752 753 brcmf_dbg(SDIO, "Enter\n"); 754 755 clkctl = 0; 756 757 if (bus->sr_enabled) { 758 bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY); 759 return 0; 760 } 761 762 if (on) { 763 /* Request HT Avail */ 764 clkreq = 765 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ; 766 767 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 768 clkreq, &err); 769 if (err) { 770 brcmf_err("HT Avail request error: %d\n", err); 771 return -EBADE; 772 } 773 774 /* Check current status */ 775 clkctl = brcmf_sdiod_readb(bus->sdiodev, 776 SBSDIO_FUNC1_CHIPCLKCSR, &err); 777 if (err) { 778 brcmf_err("HT Avail read error: %d\n", err); 779 return -EBADE; 780 } 781 782 /* Go to pending and await interrupt if appropriate */ 783 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) { 784 /* Allow only clock-available interrupt */ 785 devctl = brcmf_sdiod_readb(bus->sdiodev, 786 SBSDIO_DEVICE_CTL, &err); 787 if (err) { 788 brcmf_err("Devctl error setting CA: %d\n", err); 789 return -EBADE; 790 } 791 792 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY; 793 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_DEVICE_CTL, 794 devctl, &err); 795 brcmf_dbg(SDIO, "CLKCTL: set PENDING\n"); 796 bus->clkstate = CLK_PENDING; 797 798 return 0; 799 } else if (bus->clkstate == CLK_PENDING) { 800 /* Cancel CA-only interrupt filter */ 801 devctl = brcmf_sdiod_readb(bus->sdiodev, 802 SBSDIO_DEVICE_CTL, &err); 803 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY; 804 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_DEVICE_CTL, 805 devctl, &err); 806 } 807 808 /* Otherwise, wait here (polling) for HT Avail */ 809 timeout = jiffies + 810 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000); 811 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) { 812 clkctl = brcmf_sdiod_readb(bus->sdiodev, 813 SBSDIO_FUNC1_CHIPCLKCSR, 814 &err); 815 if (time_after(jiffies, timeout)) 816 break; 817 else 818 usleep_range(5000, 10000); 819 } 820 if (err) { 821 brcmf_err("HT Avail request error: %d\n", err); 822 return -EBADE; 823 } 824 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) { 825 brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n", 826 PMU_MAX_TRANSITION_DLY, clkctl); 827 return -EBADE; 828 } 829 830 /* Mark clock available */ 831 bus->clkstate = CLK_AVAIL; 832 brcmf_dbg(SDIO, "CLKCTL: turned ON\n"); 833 834 #if defined(DEBUG) 835 if (!bus->alp_only) { 836 if (SBSDIO_ALPONLY(clkctl)) 837 brcmf_err("HT Clock should be on\n"); 838 } 839 #endif /* defined (DEBUG) */ 840 841 } else { 842 clkreq = 0; 843 844 if (bus->clkstate == CLK_PENDING) { 845 /* Cancel CA-only interrupt filter */ 846 devctl = brcmf_sdiod_readb(bus->sdiodev, 847 SBSDIO_DEVICE_CTL, &err); 848 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY; 849 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_DEVICE_CTL, 850 devctl, &err); 851 } 852 853 bus->clkstate = CLK_SDONLY; 854 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 855 clkreq, &err); 856 brcmf_dbg(SDIO, "CLKCTL: turned OFF\n"); 857 if (err) { 858 brcmf_err("Failed access turning clock off: %d\n", 859 err); 860 return -EBADE; 861 } 862 } 863 return 0; 864 } 865 866 /* Change idle/active SD state */ 867 static int brcmf_sdio_sdclk(struct brcmf_sdio *bus, bool on) 868 { 869 brcmf_dbg(SDIO, "Enter\n"); 870 871 if (on) 872 bus->clkstate = CLK_SDONLY; 873 else 874 bus->clkstate = CLK_NONE; 875 876 return 0; 877 } 878 879 /* Transition SD and backplane clock readiness */ 880 static int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok) 881 { 882 #ifdef DEBUG 883 uint oldstate = bus->clkstate; 884 #endif /* DEBUG */ 885 886 brcmf_dbg(SDIO, "Enter\n"); 887 888 /* Early exit if we're already there */ 889 if (bus->clkstate == target) 890 return 0; 891 892 switch (target) { 893 case CLK_AVAIL: 894 /* Make sure SD clock is available */ 895 if (bus->clkstate == CLK_NONE) 896 brcmf_sdio_sdclk(bus, true); 897 /* Now request HT Avail on the backplane */ 898 brcmf_sdio_htclk(bus, true, pendok); 899 break; 900 901 case CLK_SDONLY: 902 /* Remove HT request, or bring up SD clock */ 903 if (bus->clkstate == CLK_NONE) 904 brcmf_sdio_sdclk(bus, true); 905 else if (bus->clkstate == CLK_AVAIL) 906 brcmf_sdio_htclk(bus, false, false); 907 else 908 brcmf_err("request for %d -> %d\n", 909 bus->clkstate, target); 910 break; 911 912 case CLK_NONE: 913 /* Make sure to remove HT request */ 914 if (bus->clkstate == CLK_AVAIL) 915 brcmf_sdio_htclk(bus, false, false); 916 /* Now remove the SD clock */ 917 brcmf_sdio_sdclk(bus, false); 918 break; 919 } 920 #ifdef DEBUG 921 brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate); 922 #endif /* DEBUG */ 923 924 return 0; 925 } 926 927 static int 928 brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok) 929 { 930 int err = 0; 931 u8 clkcsr; 932 933 brcmf_dbg(SDIO, "Enter: request %s currently %s\n", 934 (sleep ? "SLEEP" : "WAKE"), 935 (bus->sleeping ? "SLEEP" : "WAKE")); 936 937 /* If SR is enabled control bus state with KSO */ 938 if (bus->sr_enabled) { 939 /* Done if we're already in the requested state */ 940 if (sleep == bus->sleeping) 941 goto end; 942 943 /* Going to sleep */ 944 if (sleep) { 945 clkcsr = brcmf_sdiod_readb(bus->sdiodev, 946 SBSDIO_FUNC1_CHIPCLKCSR, 947 &err); 948 if ((clkcsr & SBSDIO_CSR_MASK) == 0) { 949 brcmf_dbg(SDIO, "no clock, set ALP\n"); 950 brcmf_sdiod_writeb(bus->sdiodev, 951 SBSDIO_FUNC1_CHIPCLKCSR, 952 SBSDIO_ALP_AVAIL_REQ, &err); 953 } 954 err = brcmf_sdio_kso_control(bus, false); 955 } else { 956 err = brcmf_sdio_kso_control(bus, true); 957 } 958 if (err) { 959 brcmf_err("error while changing bus sleep state %d\n", 960 err); 961 goto done; 962 } 963 } 964 965 end: 966 /* control clocks */ 967 if (sleep) { 968 if (!bus->sr_enabled) 969 brcmf_sdio_clkctl(bus, CLK_NONE, pendok); 970 } else { 971 brcmf_sdio_clkctl(bus, CLK_AVAIL, pendok); 972 brcmf_sdio_wd_timer(bus, true); 973 } 974 bus->sleeping = sleep; 975 brcmf_dbg(SDIO, "new state %s\n", 976 (sleep ? "SLEEP" : "WAKE")); 977 done: 978 brcmf_dbg(SDIO, "Exit: err=%d\n", err); 979 return err; 980 981 } 982 983 #ifdef DEBUG 984 static inline bool brcmf_sdio_valid_shared_address(u32 addr) 985 { 986 return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff)); 987 } 988 989 static int brcmf_sdio_readshared(struct brcmf_sdio *bus, 990 struct sdpcm_shared *sh) 991 { 992 u32 addr = 0; 993 int rv; 994 u32 shaddr = 0; 995 struct sdpcm_shared_le sh_le; 996 __le32 addr_le; 997 998 sdio_claim_host(bus->sdiodev->func1); 999 brcmf_sdio_bus_sleep(bus, false, false); 1000 1001 /* 1002 * Read last word in socram to determine 1003 * address of sdpcm_shared structure 1004 */ 1005 shaddr = bus->ci->rambase + bus->ci->ramsize - 4; 1006 if (!bus->ci->rambase && brcmf_chip_sr_capable(bus->ci)) 1007 shaddr -= bus->ci->srsize; 1008 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, shaddr, 1009 (u8 *)&addr_le, 4); 1010 if (rv < 0) 1011 goto fail; 1012 1013 /* 1014 * Check if addr is valid. 1015 * NVRAM length at the end of memory should have been overwritten. 1016 */ 1017 addr = le32_to_cpu(addr_le); 1018 if (!brcmf_sdio_valid_shared_address(addr)) { 1019 brcmf_err("invalid sdpcm_shared address 0x%08X\n", addr); 1020 rv = -EINVAL; 1021 goto fail; 1022 } 1023 1024 brcmf_dbg(INFO, "sdpcm_shared address 0x%08X\n", addr); 1025 1026 /* Read hndrte_shared structure */ 1027 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le, 1028 sizeof(struct sdpcm_shared_le)); 1029 if (rv < 0) 1030 goto fail; 1031 1032 sdio_release_host(bus->sdiodev->func1); 1033 1034 /* Endianness */ 1035 sh->flags = le32_to_cpu(sh_le.flags); 1036 sh->trap_addr = le32_to_cpu(sh_le.trap_addr); 1037 sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr); 1038 sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr); 1039 sh->assert_line = le32_to_cpu(sh_le.assert_line); 1040 sh->console_addr = le32_to_cpu(sh_le.console_addr); 1041 sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr); 1042 1043 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) { 1044 brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n", 1045 SDPCM_SHARED_VERSION, 1046 sh->flags & SDPCM_SHARED_VERSION_MASK); 1047 return -EPROTO; 1048 } 1049 return 0; 1050 1051 fail: 1052 brcmf_err("unable to obtain sdpcm_shared info: rv=%d (addr=0x%x)\n", 1053 rv, addr); 1054 sdio_release_host(bus->sdiodev->func1); 1055 return rv; 1056 } 1057 1058 static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus) 1059 { 1060 struct sdpcm_shared sh; 1061 1062 if (brcmf_sdio_readshared(bus, &sh) == 0) 1063 bus->console_addr = sh.console_addr; 1064 } 1065 #else 1066 static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus) 1067 { 1068 } 1069 #endif /* DEBUG */ 1070 1071 static u32 brcmf_sdio_hostmail(struct brcmf_sdio *bus) 1072 { 1073 struct brcmf_sdio_dev *sdiod = bus->sdiodev; 1074 struct brcmf_core *core = bus->sdio_core; 1075 u32 intstatus = 0; 1076 u32 hmb_data; 1077 u8 fcbits; 1078 int ret; 1079 1080 brcmf_dbg(SDIO, "Enter\n"); 1081 1082 /* Read mailbox data and ack that we did so */ 1083 hmb_data = brcmf_sdiod_readl(sdiod, 1084 core->base + SD_REG(tohostmailboxdata), 1085 &ret); 1086 1087 if (!ret) 1088 brcmf_sdiod_writel(sdiod, core->base + SD_REG(tosbmailbox), 1089 SMB_INT_ACK, &ret); 1090 1091 bus->sdcnt.f1regdata += 2; 1092 1093 /* dongle indicates the firmware has halted/crashed */ 1094 if (hmb_data & HMB_DATA_FWHALT) { 1095 brcmf_dbg(SDIO, "mailbox indicates firmware halted\n"); 1096 brcmf_fw_crashed(&sdiod->func1->dev); 1097 } 1098 1099 /* Dongle recomposed rx frames, accept them again */ 1100 if (hmb_data & HMB_DATA_NAKHANDLED) { 1101 brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n", 1102 bus->rx_seq); 1103 if (!bus->rxskip) 1104 brcmf_err("unexpected NAKHANDLED!\n"); 1105 1106 bus->rxskip = false; 1107 intstatus |= I_HMB_FRAME_IND; 1108 } 1109 1110 /* 1111 * DEVREADY does not occur with gSPI. 1112 */ 1113 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) { 1114 bus->sdpcm_ver = 1115 (hmb_data & HMB_DATA_VERSION_MASK) >> 1116 HMB_DATA_VERSION_SHIFT; 1117 if (bus->sdpcm_ver != SDPCM_PROT_VERSION) 1118 brcmf_err("Version mismatch, dongle reports %d, " 1119 "expecting %d\n", 1120 bus->sdpcm_ver, SDPCM_PROT_VERSION); 1121 else 1122 brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n", 1123 bus->sdpcm_ver); 1124 1125 /* 1126 * Retrieve console state address now that firmware should have 1127 * updated it. 1128 */ 1129 brcmf_sdio_get_console_addr(bus); 1130 } 1131 1132 /* 1133 * Flow Control has been moved into the RX headers and this out of band 1134 * method isn't used any more. 1135 * remaining backward compatible with older dongles. 1136 */ 1137 if (hmb_data & HMB_DATA_FC) { 1138 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >> 1139 HMB_DATA_FCDATA_SHIFT; 1140 1141 if (fcbits & ~bus->flowcontrol) 1142 bus->sdcnt.fc_xoff++; 1143 1144 if (bus->flowcontrol & ~fcbits) 1145 bus->sdcnt.fc_xon++; 1146 1147 bus->sdcnt.fc_rcvd++; 1148 bus->flowcontrol = fcbits; 1149 } 1150 1151 /* Shouldn't be any others */ 1152 if (hmb_data & ~(HMB_DATA_DEVREADY | 1153 HMB_DATA_NAKHANDLED | 1154 HMB_DATA_FC | 1155 HMB_DATA_FWREADY | 1156 HMB_DATA_FWHALT | 1157 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK)) 1158 brcmf_err("Unknown mailbox data content: 0x%02x\n", 1159 hmb_data); 1160 1161 return intstatus; 1162 } 1163 1164 static void brcmf_sdio_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx) 1165 { 1166 struct brcmf_sdio_dev *sdiod = bus->sdiodev; 1167 struct brcmf_core *core = bus->sdio_core; 1168 uint retries = 0; 1169 u16 lastrbc; 1170 u8 hi, lo; 1171 int err; 1172 1173 brcmf_err("%sterminate frame%s\n", 1174 abort ? "abort command, " : "", 1175 rtx ? ", send NAK" : ""); 1176 1177 if (abort) 1178 brcmf_sdiod_abort(bus->sdiodev, bus->sdiodev->func2); 1179 1180 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL, SFC_RF_TERM, 1181 &err); 1182 bus->sdcnt.f1regdata++; 1183 1184 /* Wait until the packet has been flushed (device/FIFO stable) */ 1185 for (lastrbc = retries = 0xffff; retries > 0; retries--) { 1186 hi = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_FUNC1_RFRAMEBCHI, 1187 &err); 1188 lo = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_FUNC1_RFRAMEBCLO, 1189 &err); 1190 bus->sdcnt.f1regdata += 2; 1191 1192 if ((hi == 0) && (lo == 0)) 1193 break; 1194 1195 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) { 1196 brcmf_err("count growing: last 0x%04x now 0x%04x\n", 1197 lastrbc, (hi << 8) + lo); 1198 } 1199 lastrbc = (hi << 8) + lo; 1200 } 1201 1202 if (!retries) 1203 brcmf_err("count never zeroed: last 0x%04x\n", lastrbc); 1204 else 1205 brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries); 1206 1207 if (rtx) { 1208 bus->sdcnt.rxrtx++; 1209 brcmf_sdiod_writel(sdiod, core->base + SD_REG(tosbmailbox), 1210 SMB_NAK, &err); 1211 1212 bus->sdcnt.f1regdata++; 1213 if (err == 0) 1214 bus->rxskip = true; 1215 } 1216 1217 /* Clear partial in any case */ 1218 bus->cur_read.len = 0; 1219 } 1220 1221 static void brcmf_sdio_txfail(struct brcmf_sdio *bus) 1222 { 1223 struct brcmf_sdio_dev *sdiodev = bus->sdiodev; 1224 u8 i, hi, lo; 1225 1226 /* On failure, abort the command and terminate the frame */ 1227 brcmf_err("sdio error, abort command and terminate frame\n"); 1228 bus->sdcnt.tx_sderrs++; 1229 1230 brcmf_sdiod_abort(sdiodev, sdiodev->func2); 1231 brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM, NULL); 1232 bus->sdcnt.f1regdata++; 1233 1234 for (i = 0; i < 3; i++) { 1235 hi = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_WFRAMEBCHI, NULL); 1236 lo = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_WFRAMEBCLO, NULL); 1237 bus->sdcnt.f1regdata += 2; 1238 if ((hi == 0) && (lo == 0)) 1239 break; 1240 } 1241 } 1242 1243 /* return total length of buffer chain */ 1244 static uint brcmf_sdio_glom_len(struct brcmf_sdio *bus) 1245 { 1246 struct sk_buff *p; 1247 uint total; 1248 1249 total = 0; 1250 skb_queue_walk(&bus->glom, p) 1251 total += p->len; 1252 return total; 1253 } 1254 1255 static void brcmf_sdio_free_glom(struct brcmf_sdio *bus) 1256 { 1257 struct sk_buff *cur, *next; 1258 1259 skb_queue_walk_safe(&bus->glom, cur, next) { 1260 skb_unlink(cur, &bus->glom); 1261 brcmu_pkt_buf_free_skb(cur); 1262 } 1263 } 1264 1265 /** 1266 * brcmfmac sdio bus specific header 1267 * This is the lowest layer header wrapped on the packets transmitted between 1268 * host and WiFi dongle which contains information needed for SDIO core and 1269 * firmware 1270 * 1271 * It consists of 3 parts: hardware header, hardware extension header and 1272 * software header 1273 * hardware header (frame tag) - 4 bytes 1274 * Byte 0~1: Frame length 1275 * Byte 2~3: Checksum, bit-wise inverse of frame length 1276 * hardware extension header - 8 bytes 1277 * Tx glom mode only, N/A for Rx or normal Tx 1278 * Byte 0~1: Packet length excluding hw frame tag 1279 * Byte 2: Reserved 1280 * Byte 3: Frame flags, bit 0: last frame indication 1281 * Byte 4~5: Reserved 1282 * Byte 6~7: Tail padding length 1283 * software header - 8 bytes 1284 * Byte 0: Rx/Tx sequence number 1285 * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag 1286 * Byte 2: Length of next data frame, reserved for Tx 1287 * Byte 3: Data offset 1288 * Byte 4: Flow control bits, reserved for Tx 1289 * Byte 5: Maximum Sequence number allowed by firmware for Tx, N/A for Tx packet 1290 * Byte 6~7: Reserved 1291 */ 1292 #define SDPCM_HWHDR_LEN 4 1293 #define SDPCM_HWEXT_LEN 8 1294 #define SDPCM_SWHDR_LEN 8 1295 #define SDPCM_HDRLEN (SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN) 1296 /* software header */ 1297 #define SDPCM_SEQ_MASK 0x000000ff 1298 #define SDPCM_SEQ_WRAP 256 1299 #define SDPCM_CHANNEL_MASK 0x00000f00 1300 #define SDPCM_CHANNEL_SHIFT 8 1301 #define SDPCM_CONTROL_CHANNEL 0 /* Control */ 1302 #define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication */ 1303 #define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv */ 1304 #define SDPCM_GLOM_CHANNEL 3 /* Coalesced packets */ 1305 #define SDPCM_TEST_CHANNEL 15 /* Test/debug packets */ 1306 #define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80) 1307 #define SDPCM_NEXTLEN_MASK 0x00ff0000 1308 #define SDPCM_NEXTLEN_SHIFT 16 1309 #define SDPCM_DOFFSET_MASK 0xff000000 1310 #define SDPCM_DOFFSET_SHIFT 24 1311 #define SDPCM_FCMASK_MASK 0x000000ff 1312 #define SDPCM_WINDOW_MASK 0x0000ff00 1313 #define SDPCM_WINDOW_SHIFT 8 1314 1315 static inline u8 brcmf_sdio_getdatoffset(u8 *swheader) 1316 { 1317 u32 hdrvalue; 1318 hdrvalue = *(u32 *)swheader; 1319 return (u8)((hdrvalue & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT); 1320 } 1321 1322 static inline bool brcmf_sdio_fromevntchan(u8 *swheader) 1323 { 1324 u32 hdrvalue; 1325 u8 ret; 1326 1327 hdrvalue = *(u32 *)swheader; 1328 ret = (u8)((hdrvalue & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT); 1329 1330 return (ret == SDPCM_EVENT_CHANNEL); 1331 } 1332 1333 static int brcmf_sdio_hdparse(struct brcmf_sdio *bus, u8 *header, 1334 struct brcmf_sdio_hdrinfo *rd, 1335 enum brcmf_sdio_frmtype type) 1336 { 1337 u16 len, checksum; 1338 u8 rx_seq, fc, tx_seq_max; 1339 u32 swheader; 1340 1341 trace_brcmf_sdpcm_hdr(SDPCM_RX, header); 1342 1343 /* hw header */ 1344 len = get_unaligned_le16(header); 1345 checksum = get_unaligned_le16(header + sizeof(u16)); 1346 /* All zero means no more to read */ 1347 if (!(len | checksum)) { 1348 bus->rxpending = false; 1349 return -ENODATA; 1350 } 1351 if ((u16)(~(len ^ checksum))) { 1352 brcmf_err("HW header checksum error\n"); 1353 bus->sdcnt.rx_badhdr++; 1354 brcmf_sdio_rxfail(bus, false, false); 1355 return -EIO; 1356 } 1357 if (len < SDPCM_HDRLEN) { 1358 brcmf_err("HW header length error\n"); 1359 return -EPROTO; 1360 } 1361 if (type == BRCMF_SDIO_FT_SUPER && 1362 (roundup(len, bus->blocksize) != rd->len)) { 1363 brcmf_err("HW superframe header length error\n"); 1364 return -EPROTO; 1365 } 1366 if (type == BRCMF_SDIO_FT_SUB && len > rd->len) { 1367 brcmf_err("HW subframe header length error\n"); 1368 return -EPROTO; 1369 } 1370 rd->len = len; 1371 1372 /* software header */ 1373 header += SDPCM_HWHDR_LEN; 1374 swheader = le32_to_cpu(*(__le32 *)header); 1375 if (type == BRCMF_SDIO_FT_SUPER && SDPCM_GLOMDESC(header)) { 1376 brcmf_err("Glom descriptor found in superframe head\n"); 1377 rd->len = 0; 1378 return -EINVAL; 1379 } 1380 rx_seq = (u8)(swheader & SDPCM_SEQ_MASK); 1381 rd->channel = (swheader & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT; 1382 if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL && 1383 type != BRCMF_SDIO_FT_SUPER) { 1384 brcmf_err("HW header length too long\n"); 1385 bus->sdcnt.rx_toolong++; 1386 brcmf_sdio_rxfail(bus, false, false); 1387 rd->len = 0; 1388 return -EPROTO; 1389 } 1390 if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) { 1391 brcmf_err("Wrong channel for superframe\n"); 1392 rd->len = 0; 1393 return -EINVAL; 1394 } 1395 if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL && 1396 rd->channel != SDPCM_EVENT_CHANNEL) { 1397 brcmf_err("Wrong channel for subframe\n"); 1398 rd->len = 0; 1399 return -EINVAL; 1400 } 1401 rd->dat_offset = brcmf_sdio_getdatoffset(header); 1402 if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) { 1403 brcmf_err("seq %d: bad data offset\n", rx_seq); 1404 bus->sdcnt.rx_badhdr++; 1405 brcmf_sdio_rxfail(bus, false, false); 1406 rd->len = 0; 1407 return -ENXIO; 1408 } 1409 if (rd->seq_num != rx_seq) { 1410 brcmf_dbg(SDIO, "seq %d, expected %d\n", rx_seq, rd->seq_num); 1411 bus->sdcnt.rx_badseq++; 1412 rd->seq_num = rx_seq; 1413 } 1414 /* no need to check the reset for subframe */ 1415 if (type == BRCMF_SDIO_FT_SUB) 1416 return 0; 1417 rd->len_nxtfrm = (swheader & SDPCM_NEXTLEN_MASK) >> SDPCM_NEXTLEN_SHIFT; 1418 if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) { 1419 /* only warm for NON glom packet */ 1420 if (rd->channel != SDPCM_GLOM_CHANNEL) 1421 brcmf_err("seq %d: next length error\n", rx_seq); 1422 rd->len_nxtfrm = 0; 1423 } 1424 swheader = le32_to_cpu(*(__le32 *)(header + 4)); 1425 fc = swheader & SDPCM_FCMASK_MASK; 1426 if (bus->flowcontrol != fc) { 1427 if (~bus->flowcontrol & fc) 1428 bus->sdcnt.fc_xoff++; 1429 if (bus->flowcontrol & ~fc) 1430 bus->sdcnt.fc_xon++; 1431 bus->sdcnt.fc_rcvd++; 1432 bus->flowcontrol = fc; 1433 } 1434 tx_seq_max = (swheader & SDPCM_WINDOW_MASK) >> SDPCM_WINDOW_SHIFT; 1435 if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) { 1436 brcmf_err("seq %d: max tx seq number error\n", rx_seq); 1437 tx_seq_max = bus->tx_seq + 2; 1438 } 1439 bus->tx_max = tx_seq_max; 1440 1441 return 0; 1442 } 1443 1444 static inline void brcmf_sdio_update_hwhdr(u8 *header, u16 frm_length) 1445 { 1446 *(__le16 *)header = cpu_to_le16(frm_length); 1447 *(((__le16 *)header) + 1) = cpu_to_le16(~frm_length); 1448 } 1449 1450 static void brcmf_sdio_hdpack(struct brcmf_sdio *bus, u8 *header, 1451 struct brcmf_sdio_hdrinfo *hd_info) 1452 { 1453 u32 hdrval; 1454 u8 hdr_offset; 1455 1456 brcmf_sdio_update_hwhdr(header, hd_info->len); 1457 hdr_offset = SDPCM_HWHDR_LEN; 1458 1459 if (bus->txglom) { 1460 hdrval = (hd_info->len - hdr_offset) | (hd_info->lastfrm << 24); 1461 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval); 1462 hdrval = (u16)hd_info->tail_pad << 16; 1463 *(((__le32 *)(header + hdr_offset)) + 1) = cpu_to_le32(hdrval); 1464 hdr_offset += SDPCM_HWEXT_LEN; 1465 } 1466 1467 hdrval = hd_info->seq_num; 1468 hdrval |= (hd_info->channel << SDPCM_CHANNEL_SHIFT) & 1469 SDPCM_CHANNEL_MASK; 1470 hdrval |= (hd_info->dat_offset << SDPCM_DOFFSET_SHIFT) & 1471 SDPCM_DOFFSET_MASK; 1472 *((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval); 1473 *(((__le32 *)(header + hdr_offset)) + 1) = 0; 1474 trace_brcmf_sdpcm_hdr(SDPCM_TX + !!(bus->txglom), header); 1475 } 1476 1477 static u8 brcmf_sdio_rxglom(struct brcmf_sdio *bus, u8 rxseq) 1478 { 1479 u16 dlen, totlen; 1480 u8 *dptr, num = 0; 1481 u16 sublen; 1482 struct sk_buff *pfirst, *pnext; 1483 1484 int errcode; 1485 u8 doff; 1486 1487 struct brcmf_sdio_hdrinfo rd_new; 1488 1489 /* If packets, issue read(s) and send up packet chain */ 1490 /* Return sequence numbers consumed? */ 1491 1492 brcmf_dbg(SDIO, "start: glomd %p glom %p\n", 1493 bus->glomd, skb_peek(&bus->glom)); 1494 1495 /* If there's a descriptor, generate the packet chain */ 1496 if (bus->glomd) { 1497 pfirst = pnext = NULL; 1498 dlen = (u16) (bus->glomd->len); 1499 dptr = bus->glomd->data; 1500 if (!dlen || (dlen & 1)) { 1501 brcmf_err("bad glomd len(%d), ignore descriptor\n", 1502 dlen); 1503 dlen = 0; 1504 } 1505 1506 for (totlen = num = 0; dlen; num++) { 1507 /* Get (and move past) next length */ 1508 sublen = get_unaligned_le16(dptr); 1509 dlen -= sizeof(u16); 1510 dptr += sizeof(u16); 1511 if ((sublen < SDPCM_HDRLEN) || 1512 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) { 1513 brcmf_err("descriptor len %d bad: %d\n", 1514 num, sublen); 1515 pnext = NULL; 1516 break; 1517 } 1518 if (sublen % bus->sgentry_align) { 1519 brcmf_err("sublen %d not multiple of %d\n", 1520 sublen, bus->sgentry_align); 1521 } 1522 totlen += sublen; 1523 1524 /* For last frame, adjust read len so total 1525 is a block multiple */ 1526 if (!dlen) { 1527 sublen += 1528 (roundup(totlen, bus->blocksize) - totlen); 1529 totlen = roundup(totlen, bus->blocksize); 1530 } 1531 1532 /* Allocate/chain packet for next subframe */ 1533 pnext = brcmu_pkt_buf_get_skb(sublen + bus->sgentry_align); 1534 if (pnext == NULL) { 1535 brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n", 1536 num, sublen); 1537 break; 1538 } 1539 skb_queue_tail(&bus->glom, pnext); 1540 1541 /* Adhere to start alignment requirements */ 1542 pkt_align(pnext, sublen, bus->sgentry_align); 1543 } 1544 1545 /* If all allocations succeeded, save packet chain 1546 in bus structure */ 1547 if (pnext) { 1548 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n", 1549 totlen, num); 1550 if (BRCMF_GLOM_ON() && bus->cur_read.len && 1551 totlen != bus->cur_read.len) { 1552 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n", 1553 bus->cur_read.len, totlen, rxseq); 1554 } 1555 pfirst = pnext = NULL; 1556 } else { 1557 brcmf_sdio_free_glom(bus); 1558 num = 0; 1559 } 1560 1561 /* Done with descriptor packet */ 1562 brcmu_pkt_buf_free_skb(bus->glomd); 1563 bus->glomd = NULL; 1564 bus->cur_read.len = 0; 1565 } 1566 1567 /* Ok -- either we just generated a packet chain, 1568 or had one from before */ 1569 if (!skb_queue_empty(&bus->glom)) { 1570 if (BRCMF_GLOM_ON()) { 1571 brcmf_dbg(GLOM, "try superframe read, packet chain:\n"); 1572 skb_queue_walk(&bus->glom, pnext) { 1573 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n", 1574 pnext, (u8 *) (pnext->data), 1575 pnext->len, pnext->len); 1576 } 1577 } 1578 1579 pfirst = skb_peek(&bus->glom); 1580 dlen = (u16) brcmf_sdio_glom_len(bus); 1581 1582 /* Do an SDIO read for the superframe. Configurable iovar to 1583 * read directly into the chained packet, or allocate a large 1584 * packet and and copy into the chain. 1585 */ 1586 sdio_claim_host(bus->sdiodev->func1); 1587 errcode = brcmf_sdiod_recv_chain(bus->sdiodev, 1588 &bus->glom, dlen); 1589 sdio_release_host(bus->sdiodev->func1); 1590 bus->sdcnt.f2rxdata++; 1591 1592 /* On failure, kill the superframe */ 1593 if (errcode < 0) { 1594 brcmf_err("glom read of %d bytes failed: %d\n", 1595 dlen, errcode); 1596 1597 sdio_claim_host(bus->sdiodev->func1); 1598 brcmf_sdio_rxfail(bus, true, false); 1599 bus->sdcnt.rxglomfail++; 1600 brcmf_sdio_free_glom(bus); 1601 sdio_release_host(bus->sdiodev->func1); 1602 return 0; 1603 } 1604 1605 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(), 1606 pfirst->data, min_t(int, pfirst->len, 48), 1607 "SUPERFRAME:\n"); 1608 1609 rd_new.seq_num = rxseq; 1610 rd_new.len = dlen; 1611 sdio_claim_host(bus->sdiodev->func1); 1612 errcode = brcmf_sdio_hdparse(bus, pfirst->data, &rd_new, 1613 BRCMF_SDIO_FT_SUPER); 1614 sdio_release_host(bus->sdiodev->func1); 1615 bus->cur_read.len = rd_new.len_nxtfrm << 4; 1616 1617 /* Remove superframe header, remember offset */ 1618 skb_pull(pfirst, rd_new.dat_offset); 1619 num = 0; 1620 1621 /* Validate all the subframe headers */ 1622 skb_queue_walk(&bus->glom, pnext) { 1623 /* leave when invalid subframe is found */ 1624 if (errcode) 1625 break; 1626 1627 rd_new.len = pnext->len; 1628 rd_new.seq_num = rxseq++; 1629 sdio_claim_host(bus->sdiodev->func1); 1630 errcode = brcmf_sdio_hdparse(bus, pnext->data, &rd_new, 1631 BRCMF_SDIO_FT_SUB); 1632 sdio_release_host(bus->sdiodev->func1); 1633 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(), 1634 pnext->data, 32, "subframe:\n"); 1635 1636 num++; 1637 } 1638 1639 if (errcode) { 1640 /* Terminate frame on error */ 1641 sdio_claim_host(bus->sdiodev->func1); 1642 brcmf_sdio_rxfail(bus, true, false); 1643 bus->sdcnt.rxglomfail++; 1644 brcmf_sdio_free_glom(bus); 1645 sdio_release_host(bus->sdiodev->func1); 1646 bus->cur_read.len = 0; 1647 return 0; 1648 } 1649 1650 /* Basic SD framing looks ok - process each packet (header) */ 1651 1652 skb_queue_walk_safe(&bus->glom, pfirst, pnext) { 1653 dptr = (u8 *) (pfirst->data); 1654 sublen = get_unaligned_le16(dptr); 1655 doff = brcmf_sdio_getdatoffset(&dptr[SDPCM_HWHDR_LEN]); 1656 1657 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(), 1658 dptr, pfirst->len, 1659 "Rx Subframe Data:\n"); 1660 1661 __skb_trim(pfirst, sublen); 1662 skb_pull(pfirst, doff); 1663 1664 if (pfirst->len == 0) { 1665 skb_unlink(pfirst, &bus->glom); 1666 brcmu_pkt_buf_free_skb(pfirst); 1667 continue; 1668 } 1669 1670 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(), 1671 pfirst->data, 1672 min_t(int, pfirst->len, 32), 1673 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n", 1674 bus->glom.qlen, pfirst, pfirst->data, 1675 pfirst->len, pfirst->next, 1676 pfirst->prev); 1677 skb_unlink(pfirst, &bus->glom); 1678 if (brcmf_sdio_fromevntchan(&dptr[SDPCM_HWHDR_LEN])) 1679 brcmf_rx_event(bus->sdiodev->dev, pfirst); 1680 else 1681 brcmf_rx_frame(bus->sdiodev->dev, pfirst, 1682 false); 1683 bus->sdcnt.rxglompkts++; 1684 } 1685 1686 bus->sdcnt.rxglomframes++; 1687 } 1688 return num; 1689 } 1690 1691 static int brcmf_sdio_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition, 1692 bool *pending) 1693 { 1694 DECLARE_WAITQUEUE(wait, current); 1695 int timeout = DCMD_RESP_TIMEOUT; 1696 1697 /* Wait until control frame is available */ 1698 add_wait_queue(&bus->dcmd_resp_wait, &wait); 1699 set_current_state(TASK_INTERRUPTIBLE); 1700 1701 while (!(*condition) && (!signal_pending(current) && timeout)) 1702 timeout = schedule_timeout(timeout); 1703 1704 if (signal_pending(current)) 1705 *pending = true; 1706 1707 set_current_state(TASK_RUNNING); 1708 remove_wait_queue(&bus->dcmd_resp_wait, &wait); 1709 1710 return timeout; 1711 } 1712 1713 static int brcmf_sdio_dcmd_resp_wake(struct brcmf_sdio *bus) 1714 { 1715 wake_up_interruptible(&bus->dcmd_resp_wait); 1716 1717 return 0; 1718 } 1719 static void 1720 brcmf_sdio_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff) 1721 { 1722 uint rdlen, pad; 1723 u8 *buf = NULL, *rbuf; 1724 int sdret; 1725 1726 brcmf_dbg(SDIO, "Enter\n"); 1727 if (bus->rxblen) 1728 buf = vzalloc(bus->rxblen); 1729 if (!buf) 1730 goto done; 1731 1732 rbuf = bus->rxbuf; 1733 pad = ((unsigned long)rbuf % bus->head_align); 1734 if (pad) 1735 rbuf += (bus->head_align - pad); 1736 1737 /* Copy the already-read portion over */ 1738 memcpy(buf, hdr, BRCMF_FIRSTREAD); 1739 if (len <= BRCMF_FIRSTREAD) 1740 goto gotpkt; 1741 1742 /* Raise rdlen to next SDIO block to avoid tail command */ 1743 rdlen = len - BRCMF_FIRSTREAD; 1744 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) { 1745 pad = bus->blocksize - (rdlen % bus->blocksize); 1746 if ((pad <= bus->roundup) && (pad < bus->blocksize) && 1747 ((len + pad) < bus->sdiodev->bus_if->maxctl)) 1748 rdlen += pad; 1749 } else if (rdlen % bus->head_align) { 1750 rdlen += bus->head_align - (rdlen % bus->head_align); 1751 } 1752 1753 /* Drop if the read is too big or it exceeds our maximum */ 1754 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) { 1755 brcmf_err("%d-byte control read exceeds %d-byte buffer\n", 1756 rdlen, bus->sdiodev->bus_if->maxctl); 1757 brcmf_sdio_rxfail(bus, false, false); 1758 goto done; 1759 } 1760 1761 if ((len - doff) > bus->sdiodev->bus_if->maxctl) { 1762 brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n", 1763 len, len - doff, bus->sdiodev->bus_if->maxctl); 1764 bus->sdcnt.rx_toolong++; 1765 brcmf_sdio_rxfail(bus, false, false); 1766 goto done; 1767 } 1768 1769 /* Read remain of frame body */ 1770 sdret = brcmf_sdiod_recv_buf(bus->sdiodev, rbuf, rdlen); 1771 bus->sdcnt.f2rxdata++; 1772 1773 /* Control frame failures need retransmission */ 1774 if (sdret < 0) { 1775 brcmf_err("read %d control bytes failed: %d\n", 1776 rdlen, sdret); 1777 bus->sdcnt.rxc_errors++; 1778 brcmf_sdio_rxfail(bus, true, true); 1779 goto done; 1780 } else 1781 memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen); 1782 1783 gotpkt: 1784 1785 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(), 1786 buf, len, "RxCtrl:\n"); 1787 1788 /* Point to valid data and indicate its length */ 1789 spin_lock_bh(&bus->rxctl_lock); 1790 if (bus->rxctl) { 1791 brcmf_err("last control frame is being processed.\n"); 1792 spin_unlock_bh(&bus->rxctl_lock); 1793 vfree(buf); 1794 goto done; 1795 } 1796 bus->rxctl = buf + doff; 1797 bus->rxctl_orig = buf; 1798 bus->rxlen = len - doff; 1799 spin_unlock_bh(&bus->rxctl_lock); 1800 1801 done: 1802 /* Awake any waiters */ 1803 brcmf_sdio_dcmd_resp_wake(bus); 1804 } 1805 1806 /* Pad read to blocksize for efficiency */ 1807 static void brcmf_sdio_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen) 1808 { 1809 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) { 1810 *pad = bus->blocksize - (*rdlen % bus->blocksize); 1811 if (*pad <= bus->roundup && *pad < bus->blocksize && 1812 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ) 1813 *rdlen += *pad; 1814 } else if (*rdlen % bus->head_align) { 1815 *rdlen += bus->head_align - (*rdlen % bus->head_align); 1816 } 1817 } 1818 1819 static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes) 1820 { 1821 struct sk_buff *pkt; /* Packet for event or data frames */ 1822 u16 pad; /* Number of pad bytes to read */ 1823 uint rxleft = 0; /* Remaining number of frames allowed */ 1824 int ret; /* Return code from calls */ 1825 uint rxcount = 0; /* Total frames read */ 1826 struct brcmf_sdio_hdrinfo *rd = &bus->cur_read, rd_new; 1827 u8 head_read = 0; 1828 1829 brcmf_dbg(SDIO, "Enter\n"); 1830 1831 /* Not finished unless we encounter no more frames indication */ 1832 bus->rxpending = true; 1833 1834 for (rd->seq_num = bus->rx_seq, rxleft = maxframes; 1835 !bus->rxskip && rxleft && bus->sdiodev->state == BRCMF_SDIOD_DATA; 1836 rd->seq_num++, rxleft--) { 1837 1838 /* Handle glomming separately */ 1839 if (bus->glomd || !skb_queue_empty(&bus->glom)) { 1840 u8 cnt; 1841 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n", 1842 bus->glomd, skb_peek(&bus->glom)); 1843 cnt = brcmf_sdio_rxglom(bus, rd->seq_num); 1844 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt); 1845 rd->seq_num += cnt - 1; 1846 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1; 1847 continue; 1848 } 1849 1850 rd->len_left = rd->len; 1851 /* read header first for unknow frame length */ 1852 sdio_claim_host(bus->sdiodev->func1); 1853 if (!rd->len) { 1854 ret = brcmf_sdiod_recv_buf(bus->sdiodev, 1855 bus->rxhdr, BRCMF_FIRSTREAD); 1856 bus->sdcnt.f2rxhdrs++; 1857 if (ret < 0) { 1858 brcmf_err("RXHEADER FAILED: %d\n", 1859 ret); 1860 bus->sdcnt.rx_hdrfail++; 1861 brcmf_sdio_rxfail(bus, true, true); 1862 sdio_release_host(bus->sdiodev->func1); 1863 continue; 1864 } 1865 1866 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(), 1867 bus->rxhdr, SDPCM_HDRLEN, 1868 "RxHdr:\n"); 1869 1870 if (brcmf_sdio_hdparse(bus, bus->rxhdr, rd, 1871 BRCMF_SDIO_FT_NORMAL)) { 1872 sdio_release_host(bus->sdiodev->func1); 1873 if (!bus->rxpending) 1874 break; 1875 else 1876 continue; 1877 } 1878 1879 if (rd->channel == SDPCM_CONTROL_CHANNEL) { 1880 brcmf_sdio_read_control(bus, bus->rxhdr, 1881 rd->len, 1882 rd->dat_offset); 1883 /* prepare the descriptor for the next read */ 1884 rd->len = rd->len_nxtfrm << 4; 1885 rd->len_nxtfrm = 0; 1886 /* treat all packet as event if we don't know */ 1887 rd->channel = SDPCM_EVENT_CHANNEL; 1888 sdio_release_host(bus->sdiodev->func1); 1889 continue; 1890 } 1891 rd->len_left = rd->len > BRCMF_FIRSTREAD ? 1892 rd->len - BRCMF_FIRSTREAD : 0; 1893 head_read = BRCMF_FIRSTREAD; 1894 } 1895 1896 brcmf_sdio_pad(bus, &pad, &rd->len_left); 1897 1898 pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read + 1899 bus->head_align); 1900 if (!pkt) { 1901 /* Give up on data, request rtx of events */ 1902 brcmf_err("brcmu_pkt_buf_get_skb failed\n"); 1903 brcmf_sdio_rxfail(bus, false, 1904 RETRYCHAN(rd->channel)); 1905 sdio_release_host(bus->sdiodev->func1); 1906 continue; 1907 } 1908 skb_pull(pkt, head_read); 1909 pkt_align(pkt, rd->len_left, bus->head_align); 1910 1911 ret = brcmf_sdiod_recv_pkt(bus->sdiodev, pkt); 1912 bus->sdcnt.f2rxdata++; 1913 sdio_release_host(bus->sdiodev->func1); 1914 1915 if (ret < 0) { 1916 brcmf_err("read %d bytes from channel %d failed: %d\n", 1917 rd->len, rd->channel, ret); 1918 brcmu_pkt_buf_free_skb(pkt); 1919 sdio_claim_host(bus->sdiodev->func1); 1920 brcmf_sdio_rxfail(bus, true, 1921 RETRYCHAN(rd->channel)); 1922 sdio_release_host(bus->sdiodev->func1); 1923 continue; 1924 } 1925 1926 if (head_read) { 1927 skb_push(pkt, head_read); 1928 memcpy(pkt->data, bus->rxhdr, head_read); 1929 head_read = 0; 1930 } else { 1931 memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN); 1932 rd_new.seq_num = rd->seq_num; 1933 sdio_claim_host(bus->sdiodev->func1); 1934 if (brcmf_sdio_hdparse(bus, bus->rxhdr, &rd_new, 1935 BRCMF_SDIO_FT_NORMAL)) { 1936 rd->len = 0; 1937 brcmu_pkt_buf_free_skb(pkt); 1938 } 1939 bus->sdcnt.rx_readahead_cnt++; 1940 if (rd->len != roundup(rd_new.len, 16)) { 1941 brcmf_err("frame length mismatch:read %d, should be %d\n", 1942 rd->len, 1943 roundup(rd_new.len, 16) >> 4); 1944 rd->len = 0; 1945 brcmf_sdio_rxfail(bus, true, true); 1946 sdio_release_host(bus->sdiodev->func1); 1947 brcmu_pkt_buf_free_skb(pkt); 1948 continue; 1949 } 1950 sdio_release_host(bus->sdiodev->func1); 1951 rd->len_nxtfrm = rd_new.len_nxtfrm; 1952 rd->channel = rd_new.channel; 1953 rd->dat_offset = rd_new.dat_offset; 1954 1955 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && 1956 BRCMF_DATA_ON()) && 1957 BRCMF_HDRS_ON(), 1958 bus->rxhdr, SDPCM_HDRLEN, 1959 "RxHdr:\n"); 1960 1961 if (rd_new.channel == SDPCM_CONTROL_CHANNEL) { 1962 brcmf_err("readahead on control packet %d?\n", 1963 rd_new.seq_num); 1964 /* Force retry w/normal header read */ 1965 rd->len = 0; 1966 sdio_claim_host(bus->sdiodev->func1); 1967 brcmf_sdio_rxfail(bus, false, true); 1968 sdio_release_host(bus->sdiodev->func1); 1969 brcmu_pkt_buf_free_skb(pkt); 1970 continue; 1971 } 1972 } 1973 1974 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(), 1975 pkt->data, rd->len, "Rx Data:\n"); 1976 1977 /* Save superframe descriptor and allocate packet frame */ 1978 if (rd->channel == SDPCM_GLOM_CHANNEL) { 1979 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_HWHDR_LEN])) { 1980 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n", 1981 rd->len); 1982 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(), 1983 pkt->data, rd->len, 1984 "Glom Data:\n"); 1985 __skb_trim(pkt, rd->len); 1986 skb_pull(pkt, SDPCM_HDRLEN); 1987 bus->glomd = pkt; 1988 } else { 1989 brcmf_err("%s: glom superframe w/o " 1990 "descriptor!\n", __func__); 1991 sdio_claim_host(bus->sdiodev->func1); 1992 brcmf_sdio_rxfail(bus, false, false); 1993 sdio_release_host(bus->sdiodev->func1); 1994 } 1995 /* prepare the descriptor for the next read */ 1996 rd->len = rd->len_nxtfrm << 4; 1997 rd->len_nxtfrm = 0; 1998 /* treat all packet as event if we don't know */ 1999 rd->channel = SDPCM_EVENT_CHANNEL; 2000 continue; 2001 } 2002 2003 /* Fill in packet len and prio, deliver upward */ 2004 __skb_trim(pkt, rd->len); 2005 skb_pull(pkt, rd->dat_offset); 2006 2007 if (pkt->len == 0) 2008 brcmu_pkt_buf_free_skb(pkt); 2009 else if (rd->channel == SDPCM_EVENT_CHANNEL) 2010 brcmf_rx_event(bus->sdiodev->dev, pkt); 2011 else 2012 brcmf_rx_frame(bus->sdiodev->dev, pkt, 2013 false); 2014 2015 /* prepare the descriptor for the next read */ 2016 rd->len = rd->len_nxtfrm << 4; 2017 rd->len_nxtfrm = 0; 2018 /* treat all packet as event if we don't know */ 2019 rd->channel = SDPCM_EVENT_CHANNEL; 2020 } 2021 2022 rxcount = maxframes - rxleft; 2023 /* Message if we hit the limit */ 2024 if (!rxleft) 2025 brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes); 2026 else 2027 brcmf_dbg(DATA, "processed %d frames\n", rxcount); 2028 /* Back off rxseq if awaiting rtx, update rx_seq */ 2029 if (bus->rxskip) 2030 rd->seq_num--; 2031 bus->rx_seq = rd->seq_num; 2032 2033 return rxcount; 2034 } 2035 2036 static void 2037 brcmf_sdio_wait_event_wakeup(struct brcmf_sdio *bus) 2038 { 2039 wake_up_interruptible(&bus->ctrl_wait); 2040 return; 2041 } 2042 2043 static int brcmf_sdio_txpkt_hdalign(struct brcmf_sdio *bus, struct sk_buff *pkt) 2044 { 2045 struct brcmf_bus_stats *stats; 2046 u16 head_pad; 2047 u8 *dat_buf; 2048 2049 dat_buf = (u8 *)(pkt->data); 2050 2051 /* Check head padding */ 2052 head_pad = ((unsigned long)dat_buf % bus->head_align); 2053 if (head_pad) { 2054 if (skb_headroom(pkt) < head_pad) { 2055 stats = &bus->sdiodev->bus_if->stats; 2056 atomic_inc(&stats->pktcowed); 2057 if (skb_cow_head(pkt, head_pad)) { 2058 atomic_inc(&stats->pktcow_failed); 2059 return -ENOMEM; 2060 } 2061 head_pad = 0; 2062 } 2063 skb_push(pkt, head_pad); 2064 dat_buf = (u8 *)(pkt->data); 2065 } 2066 memset(dat_buf, 0, head_pad + bus->tx_hdrlen); 2067 return head_pad; 2068 } 2069 2070 /* 2071 * struct brcmf_skbuff_cb reserves first two bytes in sk_buff::cb for 2072 * bus layer usage. 2073 */ 2074 /* flag marking a dummy skb added for DMA alignment requirement */ 2075 #define ALIGN_SKB_FLAG 0x8000 2076 /* bit mask of data length chopped from the previous packet */ 2077 #define ALIGN_SKB_CHOP_LEN_MASK 0x7fff 2078 2079 static int brcmf_sdio_txpkt_prep_sg(struct brcmf_sdio *bus, 2080 struct sk_buff_head *pktq, 2081 struct sk_buff *pkt, u16 total_len) 2082 { 2083 struct brcmf_sdio_dev *sdiodev; 2084 struct sk_buff *pkt_pad; 2085 u16 tail_pad, tail_chop, chain_pad; 2086 unsigned int blksize; 2087 bool lastfrm; 2088 int ntail, ret; 2089 2090 sdiodev = bus->sdiodev; 2091 blksize = sdiodev->func2->cur_blksize; 2092 /* sg entry alignment should be a divisor of block size */ 2093 WARN_ON(blksize % bus->sgentry_align); 2094 2095 /* Check tail padding */ 2096 lastfrm = skb_queue_is_last(pktq, pkt); 2097 tail_pad = 0; 2098 tail_chop = pkt->len % bus->sgentry_align; 2099 if (tail_chop) 2100 tail_pad = bus->sgentry_align - tail_chop; 2101 chain_pad = (total_len + tail_pad) % blksize; 2102 if (lastfrm && chain_pad) 2103 tail_pad += blksize - chain_pad; 2104 if (skb_tailroom(pkt) < tail_pad && pkt->len > blksize) { 2105 pkt_pad = brcmu_pkt_buf_get_skb(tail_pad + tail_chop + 2106 bus->head_align); 2107 if (pkt_pad == NULL) 2108 return -ENOMEM; 2109 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_pad); 2110 if (unlikely(ret < 0)) { 2111 kfree_skb(pkt_pad); 2112 return ret; 2113 } 2114 memcpy(pkt_pad->data, 2115 pkt->data + pkt->len - tail_chop, 2116 tail_chop); 2117 *(u16 *)(pkt_pad->cb) = ALIGN_SKB_FLAG + tail_chop; 2118 skb_trim(pkt, pkt->len - tail_chop); 2119 skb_trim(pkt_pad, tail_pad + tail_chop); 2120 __skb_queue_after(pktq, pkt, pkt_pad); 2121 } else { 2122 ntail = pkt->data_len + tail_pad - 2123 (pkt->end - pkt->tail); 2124 if (skb_cloned(pkt) || ntail > 0) 2125 if (pskb_expand_head(pkt, 0, ntail, GFP_ATOMIC)) 2126 return -ENOMEM; 2127 if (skb_linearize(pkt)) 2128 return -ENOMEM; 2129 __skb_put(pkt, tail_pad); 2130 } 2131 2132 return tail_pad; 2133 } 2134 2135 /** 2136 * brcmf_sdio_txpkt_prep - packet preparation for transmit 2137 * @bus: brcmf_sdio structure pointer 2138 * @pktq: packet list pointer 2139 * @chan: virtual channel to transmit the packet 2140 * 2141 * Processes to be applied to the packet 2142 * - Align data buffer pointer 2143 * - Align data buffer length 2144 * - Prepare header 2145 * Return: negative value if there is error 2146 */ 2147 static int 2148 brcmf_sdio_txpkt_prep(struct brcmf_sdio *bus, struct sk_buff_head *pktq, 2149 uint chan) 2150 { 2151 u16 head_pad, total_len; 2152 struct sk_buff *pkt_next; 2153 u8 txseq; 2154 int ret; 2155 struct brcmf_sdio_hdrinfo hd_info = {0}; 2156 2157 txseq = bus->tx_seq; 2158 total_len = 0; 2159 skb_queue_walk(pktq, pkt_next) { 2160 /* alignment packet inserted in previous 2161 * loop cycle can be skipped as it is 2162 * already properly aligned and does not 2163 * need an sdpcm header. 2164 */ 2165 if (*(u16 *)(pkt_next->cb) & ALIGN_SKB_FLAG) 2166 continue; 2167 2168 /* align packet data pointer */ 2169 ret = brcmf_sdio_txpkt_hdalign(bus, pkt_next); 2170 if (ret < 0) 2171 return ret; 2172 head_pad = (u16)ret; 2173 if (head_pad) 2174 memset(pkt_next->data + bus->tx_hdrlen, 0, head_pad); 2175 2176 total_len += pkt_next->len; 2177 2178 hd_info.len = pkt_next->len; 2179 hd_info.lastfrm = skb_queue_is_last(pktq, pkt_next); 2180 if (bus->txglom && pktq->qlen > 1) { 2181 ret = brcmf_sdio_txpkt_prep_sg(bus, pktq, 2182 pkt_next, total_len); 2183 if (ret < 0) 2184 return ret; 2185 hd_info.tail_pad = (u16)ret; 2186 total_len += (u16)ret; 2187 } 2188 2189 hd_info.channel = chan; 2190 hd_info.dat_offset = head_pad + bus->tx_hdrlen; 2191 hd_info.seq_num = txseq++; 2192 2193 /* Now fill the header */ 2194 brcmf_sdio_hdpack(bus, pkt_next->data, &hd_info); 2195 2196 if (BRCMF_BYTES_ON() && 2197 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) || 2198 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL))) 2199 brcmf_dbg_hex_dump(true, pkt_next->data, hd_info.len, 2200 "Tx Frame:\n"); 2201 else if (BRCMF_HDRS_ON()) 2202 brcmf_dbg_hex_dump(true, pkt_next->data, 2203 head_pad + bus->tx_hdrlen, 2204 "Tx Header:\n"); 2205 } 2206 /* Hardware length tag of the first packet should be total 2207 * length of the chain (including padding) 2208 */ 2209 if (bus->txglom) 2210 brcmf_sdio_update_hwhdr(__skb_peek(pktq)->data, total_len); 2211 return 0; 2212 } 2213 2214 /** 2215 * brcmf_sdio_txpkt_postp - packet post processing for transmit 2216 * @bus: brcmf_sdio structure pointer 2217 * @pktq: packet list pointer 2218 * 2219 * Processes to be applied to the packet 2220 * - Remove head padding 2221 * - Remove tail padding 2222 */ 2223 static void 2224 brcmf_sdio_txpkt_postp(struct brcmf_sdio *bus, struct sk_buff_head *pktq) 2225 { 2226 u8 *hdr; 2227 u32 dat_offset; 2228 u16 tail_pad; 2229 u16 dummy_flags, chop_len; 2230 struct sk_buff *pkt_next, *tmp, *pkt_prev; 2231 2232 skb_queue_walk_safe(pktq, pkt_next, tmp) { 2233 dummy_flags = *(u16 *)(pkt_next->cb); 2234 if (dummy_flags & ALIGN_SKB_FLAG) { 2235 chop_len = dummy_flags & ALIGN_SKB_CHOP_LEN_MASK; 2236 if (chop_len) { 2237 pkt_prev = pkt_next->prev; 2238 skb_put(pkt_prev, chop_len); 2239 } 2240 __skb_unlink(pkt_next, pktq); 2241 brcmu_pkt_buf_free_skb(pkt_next); 2242 } else { 2243 hdr = pkt_next->data + bus->tx_hdrlen - SDPCM_SWHDR_LEN; 2244 dat_offset = le32_to_cpu(*(__le32 *)hdr); 2245 dat_offset = (dat_offset & SDPCM_DOFFSET_MASK) >> 2246 SDPCM_DOFFSET_SHIFT; 2247 skb_pull(pkt_next, dat_offset); 2248 if (bus->txglom) { 2249 tail_pad = le16_to_cpu(*(__le16 *)(hdr - 2)); 2250 skb_trim(pkt_next, pkt_next->len - tail_pad); 2251 } 2252 } 2253 } 2254 } 2255 2256 /* Writes a HW/SW header into the packet and sends it. */ 2257 /* Assumes: (a) header space already there, (b) caller holds lock */ 2258 static int brcmf_sdio_txpkt(struct brcmf_sdio *bus, struct sk_buff_head *pktq, 2259 uint chan) 2260 { 2261 int ret; 2262 struct sk_buff *pkt_next, *tmp; 2263 2264 brcmf_dbg(TRACE, "Enter\n"); 2265 2266 ret = brcmf_sdio_txpkt_prep(bus, pktq, chan); 2267 if (ret) 2268 goto done; 2269 2270 sdio_claim_host(bus->sdiodev->func1); 2271 ret = brcmf_sdiod_send_pkt(bus->sdiodev, pktq); 2272 bus->sdcnt.f2txdata++; 2273 2274 if (ret < 0) 2275 brcmf_sdio_txfail(bus); 2276 2277 sdio_release_host(bus->sdiodev->func1); 2278 2279 done: 2280 brcmf_sdio_txpkt_postp(bus, pktq); 2281 if (ret == 0) 2282 bus->tx_seq = (bus->tx_seq + pktq->qlen) % SDPCM_SEQ_WRAP; 2283 skb_queue_walk_safe(pktq, pkt_next, tmp) { 2284 __skb_unlink(pkt_next, pktq); 2285 brcmf_proto_bcdc_txcomplete(bus->sdiodev->dev, pkt_next, 2286 ret == 0); 2287 } 2288 return ret; 2289 } 2290 2291 static uint brcmf_sdio_sendfromq(struct brcmf_sdio *bus, uint maxframes) 2292 { 2293 struct sk_buff *pkt; 2294 struct sk_buff_head pktq; 2295 u32 intstat_addr = bus->sdio_core->base + SD_REG(intstatus); 2296 u32 intstatus = 0; 2297 int ret = 0, prec_out, i; 2298 uint cnt = 0; 2299 u8 tx_prec_map, pkt_num; 2300 2301 brcmf_dbg(TRACE, "Enter\n"); 2302 2303 tx_prec_map = ~bus->flowcontrol; 2304 2305 /* Send frames until the limit or some other event */ 2306 for (cnt = 0; (cnt < maxframes) && data_ok(bus);) { 2307 pkt_num = 1; 2308 if (bus->txglom) 2309 pkt_num = min_t(u8, bus->tx_max - bus->tx_seq, 2310 bus->sdiodev->txglomsz); 2311 pkt_num = min_t(u32, pkt_num, 2312 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol)); 2313 __skb_queue_head_init(&pktq); 2314 spin_lock_bh(&bus->txq_lock); 2315 for (i = 0; i < pkt_num; i++) { 2316 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map, 2317 &prec_out); 2318 if (pkt == NULL) 2319 break; 2320 __skb_queue_tail(&pktq, pkt); 2321 } 2322 spin_unlock_bh(&bus->txq_lock); 2323 if (i == 0) 2324 break; 2325 2326 ret = brcmf_sdio_txpkt(bus, &pktq, SDPCM_DATA_CHANNEL); 2327 2328 cnt += i; 2329 2330 /* In poll mode, need to check for other events */ 2331 if (!bus->intr) { 2332 /* Check device status, signal pending interrupt */ 2333 sdio_claim_host(bus->sdiodev->func1); 2334 intstatus = brcmf_sdiod_readl(bus->sdiodev, 2335 intstat_addr, &ret); 2336 sdio_release_host(bus->sdiodev->func1); 2337 2338 bus->sdcnt.f2txdata++; 2339 if (ret != 0) 2340 break; 2341 if (intstatus & bus->hostintmask) 2342 atomic_set(&bus->ipend, 1); 2343 } 2344 } 2345 2346 /* Deflow-control stack if needed */ 2347 if ((bus->sdiodev->state == BRCMF_SDIOD_DATA) && 2348 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) { 2349 bus->txoff = false; 2350 brcmf_proto_bcdc_txflowblock(bus->sdiodev->dev, false); 2351 } 2352 2353 return cnt; 2354 } 2355 2356 static int brcmf_sdio_tx_ctrlframe(struct brcmf_sdio *bus, u8 *frame, u16 len) 2357 { 2358 u8 doff; 2359 u16 pad; 2360 uint retries = 0; 2361 struct brcmf_sdio_hdrinfo hd_info = {0}; 2362 int ret; 2363 2364 brcmf_dbg(SDIO, "Enter\n"); 2365 2366 /* Back the pointer to make room for bus header */ 2367 frame -= bus->tx_hdrlen; 2368 len += bus->tx_hdrlen; 2369 2370 /* Add alignment padding (optional for ctl frames) */ 2371 doff = ((unsigned long)frame % bus->head_align); 2372 if (doff) { 2373 frame -= doff; 2374 len += doff; 2375 memset(frame + bus->tx_hdrlen, 0, doff); 2376 } 2377 2378 /* Round send length to next SDIO block */ 2379 pad = 0; 2380 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) { 2381 pad = bus->blocksize - (len % bus->blocksize); 2382 if ((pad > bus->roundup) || (pad >= bus->blocksize)) 2383 pad = 0; 2384 } else if (len % bus->head_align) { 2385 pad = bus->head_align - (len % bus->head_align); 2386 } 2387 len += pad; 2388 2389 hd_info.len = len - pad; 2390 hd_info.channel = SDPCM_CONTROL_CHANNEL; 2391 hd_info.dat_offset = doff + bus->tx_hdrlen; 2392 hd_info.seq_num = bus->tx_seq; 2393 hd_info.lastfrm = true; 2394 hd_info.tail_pad = pad; 2395 brcmf_sdio_hdpack(bus, frame, &hd_info); 2396 2397 if (bus->txglom) 2398 brcmf_sdio_update_hwhdr(frame, len); 2399 2400 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(), 2401 frame, len, "Tx Frame:\n"); 2402 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) && 2403 BRCMF_HDRS_ON(), 2404 frame, min_t(u16, len, 16), "TxHdr:\n"); 2405 2406 do { 2407 ret = brcmf_sdiod_send_buf(bus->sdiodev, frame, len); 2408 2409 if (ret < 0) 2410 brcmf_sdio_txfail(bus); 2411 else 2412 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP; 2413 } while (ret < 0 && retries++ < TXRETRIES); 2414 2415 return ret; 2416 } 2417 2418 static bool brcmf_chip_is_ulp(struct brcmf_chip *ci) 2419 { 2420 if (ci->chip == CY_CC_43012_CHIP_ID) 2421 return true; 2422 else 2423 return false; 2424 } 2425 2426 static void brcmf_sdio_bus_stop(struct device *dev) 2427 { 2428 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 2429 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; 2430 struct brcmf_sdio *bus = sdiodev->bus; 2431 struct brcmf_core *core = bus->sdio_core; 2432 u32 local_hostintmask; 2433 u8 saveclk, bpreq; 2434 int err; 2435 2436 brcmf_dbg(TRACE, "Enter\n"); 2437 2438 if (bus->watchdog_tsk) { 2439 send_sig(SIGTERM, bus->watchdog_tsk, 1); 2440 kthread_stop(bus->watchdog_tsk); 2441 bus->watchdog_tsk = NULL; 2442 } 2443 2444 if (sdiodev->state != BRCMF_SDIOD_NOMEDIUM) { 2445 sdio_claim_host(sdiodev->func1); 2446 2447 /* Enable clock for device interrupts */ 2448 brcmf_sdio_bus_sleep(bus, false, false); 2449 2450 /* Disable and clear interrupts at the chip level also */ 2451 brcmf_sdiod_writel(sdiodev, core->base + SD_REG(hostintmask), 2452 0, NULL); 2453 2454 local_hostintmask = bus->hostintmask; 2455 bus->hostintmask = 0; 2456 2457 /* Force backplane clocks to assure F2 interrupt propagates */ 2458 saveclk = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 2459 &err); 2460 if (!err) { 2461 bpreq = saveclk; 2462 bpreq |= brcmf_chip_is_ulp(bus->ci) ? 2463 SBSDIO_HT_AVAIL_REQ : SBSDIO_FORCE_HT; 2464 brcmf_sdiod_writeb(sdiodev, 2465 SBSDIO_FUNC1_CHIPCLKCSR, 2466 bpreq, &err); 2467 } 2468 if (err) 2469 brcmf_err("Failed to force clock for F2: err %d\n", 2470 err); 2471 2472 /* Turn off the bus (F2), free any pending packets */ 2473 brcmf_dbg(INTR, "disable SDIO interrupts\n"); 2474 sdio_disable_func(sdiodev->func2); 2475 2476 /* Clear any pending interrupts now that F2 is disabled */ 2477 brcmf_sdiod_writel(sdiodev, core->base + SD_REG(intstatus), 2478 local_hostintmask, NULL); 2479 2480 sdio_release_host(sdiodev->func1); 2481 } 2482 /* Clear the data packet queues */ 2483 brcmu_pktq_flush(&bus->txq, true, NULL, NULL); 2484 2485 /* Clear any held glomming stuff */ 2486 brcmu_pkt_buf_free_skb(bus->glomd); 2487 brcmf_sdio_free_glom(bus); 2488 2489 /* Clear rx control and wake any waiters */ 2490 spin_lock_bh(&bus->rxctl_lock); 2491 bus->rxlen = 0; 2492 spin_unlock_bh(&bus->rxctl_lock); 2493 brcmf_sdio_dcmd_resp_wake(bus); 2494 2495 /* Reset some F2 state stuff */ 2496 bus->rxskip = false; 2497 bus->tx_seq = bus->rx_seq = 0; 2498 } 2499 2500 static inline void brcmf_sdio_clrintr(struct brcmf_sdio *bus) 2501 { 2502 struct brcmf_sdio_dev *sdiodev; 2503 unsigned long flags; 2504 2505 sdiodev = bus->sdiodev; 2506 if (sdiodev->oob_irq_requested) { 2507 spin_lock_irqsave(&sdiodev->irq_en_lock, flags); 2508 if (!sdiodev->irq_en && !atomic_read(&bus->ipend)) { 2509 enable_irq(sdiodev->settings->bus.sdio.oob_irq_nr); 2510 sdiodev->irq_en = true; 2511 } 2512 spin_unlock_irqrestore(&sdiodev->irq_en_lock, flags); 2513 } 2514 } 2515 2516 static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus) 2517 { 2518 struct brcmf_core *core = bus->sdio_core; 2519 u32 addr; 2520 unsigned long val; 2521 int ret; 2522 2523 addr = core->base + SD_REG(intstatus); 2524 2525 val = brcmf_sdiod_readl(bus->sdiodev, addr, &ret); 2526 bus->sdcnt.f1regdata++; 2527 if (ret != 0) 2528 return ret; 2529 2530 val &= bus->hostintmask; 2531 atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE)); 2532 2533 /* Clear interrupts */ 2534 if (val) { 2535 brcmf_sdiod_writel(bus->sdiodev, addr, val, &ret); 2536 bus->sdcnt.f1regdata++; 2537 atomic_or(val, &bus->intstatus); 2538 } 2539 2540 return ret; 2541 } 2542 2543 static void brcmf_sdio_dpc(struct brcmf_sdio *bus) 2544 { 2545 struct brcmf_sdio_dev *sdiod = bus->sdiodev; 2546 u32 newstatus = 0; 2547 u32 intstat_addr = bus->sdio_core->base + SD_REG(intstatus); 2548 unsigned long intstatus; 2549 uint txlimit = bus->txbound; /* Tx frames to send before resched */ 2550 uint framecnt; /* Temporary counter of tx/rx frames */ 2551 int err = 0; 2552 2553 brcmf_dbg(SDIO, "Enter\n"); 2554 2555 sdio_claim_host(bus->sdiodev->func1); 2556 2557 /* If waiting for HTAVAIL, check status */ 2558 if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) { 2559 u8 clkctl, devctl = 0; 2560 2561 #ifdef DEBUG 2562 /* Check for inconsistent device control */ 2563 devctl = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_DEVICE_CTL, 2564 &err); 2565 #endif /* DEBUG */ 2566 2567 /* Read CSR, if clock on switch to AVAIL, else ignore */ 2568 clkctl = brcmf_sdiod_readb(bus->sdiodev, 2569 SBSDIO_FUNC1_CHIPCLKCSR, &err); 2570 2571 brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n", 2572 devctl, clkctl); 2573 2574 if (SBSDIO_HTAV(clkctl)) { 2575 devctl = brcmf_sdiod_readb(bus->sdiodev, 2576 SBSDIO_DEVICE_CTL, &err); 2577 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY; 2578 brcmf_sdiod_writeb(bus->sdiodev, 2579 SBSDIO_DEVICE_CTL, devctl, &err); 2580 bus->clkstate = CLK_AVAIL; 2581 } 2582 } 2583 2584 /* Make sure backplane clock is on */ 2585 brcmf_sdio_bus_sleep(bus, false, true); 2586 2587 /* Pending interrupt indicates new device status */ 2588 if (atomic_read(&bus->ipend) > 0) { 2589 atomic_set(&bus->ipend, 0); 2590 err = brcmf_sdio_intr_rstatus(bus); 2591 } 2592 2593 /* Start with leftover status bits */ 2594 intstatus = atomic_xchg(&bus->intstatus, 0); 2595 2596 /* Handle flow-control change: read new state in case our ack 2597 * crossed another change interrupt. If change still set, assume 2598 * FC ON for safety, let next loop through do the debounce. 2599 */ 2600 if (intstatus & I_HMB_FC_CHANGE) { 2601 intstatus &= ~I_HMB_FC_CHANGE; 2602 brcmf_sdiod_writel(sdiod, intstat_addr, I_HMB_FC_CHANGE, &err); 2603 2604 newstatus = brcmf_sdiod_readl(sdiod, intstat_addr, &err); 2605 2606 bus->sdcnt.f1regdata += 2; 2607 atomic_set(&bus->fcstate, 2608 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE))); 2609 intstatus |= (newstatus & bus->hostintmask); 2610 } 2611 2612 /* Handle host mailbox indication */ 2613 if (intstatus & I_HMB_HOST_INT) { 2614 intstatus &= ~I_HMB_HOST_INT; 2615 intstatus |= brcmf_sdio_hostmail(bus); 2616 } 2617 2618 sdio_release_host(bus->sdiodev->func1); 2619 2620 /* Generally don't ask for these, can get CRC errors... */ 2621 if (intstatus & I_WR_OOSYNC) { 2622 brcmf_err("Dongle reports WR_OOSYNC\n"); 2623 intstatus &= ~I_WR_OOSYNC; 2624 } 2625 2626 if (intstatus & I_RD_OOSYNC) { 2627 brcmf_err("Dongle reports RD_OOSYNC\n"); 2628 intstatus &= ~I_RD_OOSYNC; 2629 } 2630 2631 if (intstatus & I_SBINT) { 2632 brcmf_err("Dongle reports SBINT\n"); 2633 intstatus &= ~I_SBINT; 2634 } 2635 2636 /* Would be active due to wake-wlan in gSPI */ 2637 if (intstatus & I_CHIPACTIVE) { 2638 brcmf_dbg(SDIO, "Dongle reports CHIPACTIVE\n"); 2639 intstatus &= ~I_CHIPACTIVE; 2640 } 2641 2642 /* Ignore frame indications if rxskip is set */ 2643 if (bus->rxskip) 2644 intstatus &= ~I_HMB_FRAME_IND; 2645 2646 /* On frame indication, read available frames */ 2647 if ((intstatus & I_HMB_FRAME_IND) && (bus->clkstate == CLK_AVAIL)) { 2648 brcmf_sdio_readframes(bus, bus->rxbound); 2649 if (!bus->rxpending) 2650 intstatus &= ~I_HMB_FRAME_IND; 2651 } 2652 2653 /* Keep still-pending events for next scheduling */ 2654 if (intstatus) 2655 atomic_or(intstatus, &bus->intstatus); 2656 2657 brcmf_sdio_clrintr(bus); 2658 2659 if (bus->ctrl_frame_stat && (bus->clkstate == CLK_AVAIL) && 2660 data_ok(bus)) { 2661 sdio_claim_host(bus->sdiodev->func1); 2662 if (bus->ctrl_frame_stat) { 2663 err = brcmf_sdio_tx_ctrlframe(bus, bus->ctrl_frame_buf, 2664 bus->ctrl_frame_len); 2665 bus->ctrl_frame_err = err; 2666 wmb(); 2667 bus->ctrl_frame_stat = false; 2668 } 2669 sdio_release_host(bus->sdiodev->func1); 2670 brcmf_sdio_wait_event_wakeup(bus); 2671 } 2672 /* Send queued frames (limit 1 if rx may still be pending) */ 2673 if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) && 2674 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit && 2675 data_ok(bus)) { 2676 framecnt = bus->rxpending ? min(txlimit, bus->txminmax) : 2677 txlimit; 2678 brcmf_sdio_sendfromq(bus, framecnt); 2679 } 2680 2681 if ((bus->sdiodev->state != BRCMF_SDIOD_DATA) || (err != 0)) { 2682 brcmf_err("failed backplane access over SDIO, halting operation\n"); 2683 atomic_set(&bus->intstatus, 0); 2684 if (bus->ctrl_frame_stat) { 2685 sdio_claim_host(bus->sdiodev->func1); 2686 if (bus->ctrl_frame_stat) { 2687 bus->ctrl_frame_err = -ENODEV; 2688 wmb(); 2689 bus->ctrl_frame_stat = false; 2690 brcmf_sdio_wait_event_wakeup(bus); 2691 } 2692 sdio_release_host(bus->sdiodev->func1); 2693 } 2694 } else if (atomic_read(&bus->intstatus) || 2695 atomic_read(&bus->ipend) > 0 || 2696 (!atomic_read(&bus->fcstate) && 2697 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && 2698 data_ok(bus))) { 2699 bus->dpc_triggered = true; 2700 } 2701 } 2702 2703 static struct pktq *brcmf_sdio_bus_gettxq(struct device *dev) 2704 { 2705 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 2706 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; 2707 struct brcmf_sdio *bus = sdiodev->bus; 2708 2709 return &bus->txq; 2710 } 2711 2712 static bool brcmf_sdio_prec_enq(struct pktq *q, struct sk_buff *pkt, int prec) 2713 { 2714 struct sk_buff *p; 2715 int eprec = -1; /* precedence to evict from */ 2716 2717 /* Fast case, precedence queue is not full and we are also not 2718 * exceeding total queue length 2719 */ 2720 if (!pktq_pfull(q, prec) && !pktq_full(q)) { 2721 brcmu_pktq_penq(q, prec, pkt); 2722 return true; 2723 } 2724 2725 /* Determine precedence from which to evict packet, if any */ 2726 if (pktq_pfull(q, prec)) { 2727 eprec = prec; 2728 } else if (pktq_full(q)) { 2729 p = brcmu_pktq_peek_tail(q, &eprec); 2730 if (eprec > prec) 2731 return false; 2732 } 2733 2734 /* Evict if needed */ 2735 if (eprec >= 0) { 2736 /* Detect queueing to unconfigured precedence */ 2737 if (eprec == prec) 2738 return false; /* refuse newer (incoming) packet */ 2739 /* Evict packet according to discard policy */ 2740 p = brcmu_pktq_pdeq_tail(q, eprec); 2741 if (p == NULL) 2742 brcmf_err("brcmu_pktq_pdeq_tail() failed\n"); 2743 brcmu_pkt_buf_free_skb(p); 2744 } 2745 2746 /* Enqueue */ 2747 p = brcmu_pktq_penq(q, prec, pkt); 2748 if (p == NULL) 2749 brcmf_err("brcmu_pktq_penq() failed\n"); 2750 2751 return p != NULL; 2752 } 2753 2754 static int brcmf_sdio_bus_txdata(struct device *dev, struct sk_buff *pkt) 2755 { 2756 int ret = -EBADE; 2757 uint prec; 2758 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 2759 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; 2760 struct brcmf_sdio *bus = sdiodev->bus; 2761 2762 brcmf_dbg(TRACE, "Enter: pkt: data %p len %d\n", pkt->data, pkt->len); 2763 if (sdiodev->state != BRCMF_SDIOD_DATA) 2764 return -EIO; 2765 2766 /* Add space for the header */ 2767 skb_push(pkt, bus->tx_hdrlen); 2768 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */ 2769 2770 prec = prio2prec((pkt->priority & PRIOMASK)); 2771 2772 /* Check for existing queue, current flow-control, 2773 pending event, or pending clock */ 2774 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq)); 2775 bus->sdcnt.fcqueued++; 2776 2777 /* Priority based enq */ 2778 spin_lock_bh(&bus->txq_lock); 2779 /* reset bus_flags in packet cb */ 2780 *(u16 *)(pkt->cb) = 0; 2781 if (!brcmf_sdio_prec_enq(&bus->txq, pkt, prec)) { 2782 skb_pull(pkt, bus->tx_hdrlen); 2783 brcmf_err("out of bus->txq !!!\n"); 2784 ret = -ENOSR; 2785 } else { 2786 ret = 0; 2787 } 2788 2789 if (pktq_len(&bus->txq) >= TXHI) { 2790 bus->txoff = true; 2791 brcmf_proto_bcdc_txflowblock(dev, true); 2792 } 2793 spin_unlock_bh(&bus->txq_lock); 2794 2795 #ifdef DEBUG 2796 if (pktq_plen(&bus->txq, prec) > qcount[prec]) 2797 qcount[prec] = pktq_plen(&bus->txq, prec); 2798 #endif 2799 2800 brcmf_sdio_trigger_dpc(bus); 2801 return ret; 2802 } 2803 2804 #ifdef DEBUG 2805 #define CONSOLE_LINE_MAX 192 2806 2807 static int brcmf_sdio_readconsole(struct brcmf_sdio *bus) 2808 { 2809 struct brcmf_console *c = &bus->console; 2810 u8 line[CONSOLE_LINE_MAX], ch; 2811 u32 n, idx, addr; 2812 int rv; 2813 2814 /* Don't do anything until FWREADY updates console address */ 2815 if (bus->console_addr == 0) 2816 return 0; 2817 2818 /* Read console log struct */ 2819 addr = bus->console_addr + offsetof(struct rte_console, log_le); 2820 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le, 2821 sizeof(c->log_le)); 2822 if (rv < 0) 2823 return rv; 2824 2825 /* Allocate console buffer (one time only) */ 2826 if (c->buf == NULL) { 2827 c->bufsize = le32_to_cpu(c->log_le.buf_size); 2828 c->buf = kmalloc(c->bufsize, GFP_ATOMIC); 2829 if (c->buf == NULL) 2830 return -ENOMEM; 2831 } 2832 2833 idx = le32_to_cpu(c->log_le.idx); 2834 2835 /* Protect against corrupt value */ 2836 if (idx > c->bufsize) 2837 return -EBADE; 2838 2839 /* Skip reading the console buffer if the index pointer 2840 has not moved */ 2841 if (idx == c->last) 2842 return 0; 2843 2844 /* Read the console buffer */ 2845 addr = le32_to_cpu(c->log_le.buf); 2846 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize); 2847 if (rv < 0) 2848 return rv; 2849 2850 while (c->last != idx) { 2851 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) { 2852 if (c->last == idx) { 2853 /* This would output a partial line. 2854 * Instead, back up 2855 * the buffer pointer and output this 2856 * line next time around. 2857 */ 2858 if (c->last >= n) 2859 c->last -= n; 2860 else 2861 c->last = c->bufsize - n; 2862 goto break2; 2863 } 2864 ch = c->buf[c->last]; 2865 c->last = (c->last + 1) % c->bufsize; 2866 if (ch == '\n') 2867 break; 2868 line[n] = ch; 2869 } 2870 2871 if (n > 0) { 2872 if (line[n - 1] == '\r') 2873 n--; 2874 line[n] = 0; 2875 pr_debug("CONSOLE: %s\n", line); 2876 } 2877 } 2878 break2: 2879 2880 return 0; 2881 } 2882 #endif /* DEBUG */ 2883 2884 static int 2885 brcmf_sdio_bus_txctl(struct device *dev, unsigned char *msg, uint msglen) 2886 { 2887 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 2888 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; 2889 struct brcmf_sdio *bus = sdiodev->bus; 2890 int ret; 2891 2892 brcmf_dbg(TRACE, "Enter\n"); 2893 if (sdiodev->state != BRCMF_SDIOD_DATA) 2894 return -EIO; 2895 2896 /* Send from dpc */ 2897 bus->ctrl_frame_buf = msg; 2898 bus->ctrl_frame_len = msglen; 2899 wmb(); 2900 bus->ctrl_frame_stat = true; 2901 2902 brcmf_sdio_trigger_dpc(bus); 2903 wait_event_interruptible_timeout(bus->ctrl_wait, !bus->ctrl_frame_stat, 2904 CTL_DONE_TIMEOUT); 2905 ret = 0; 2906 if (bus->ctrl_frame_stat) { 2907 sdio_claim_host(bus->sdiodev->func1); 2908 if (bus->ctrl_frame_stat) { 2909 brcmf_dbg(SDIO, "ctrl_frame timeout\n"); 2910 bus->ctrl_frame_stat = false; 2911 ret = -ETIMEDOUT; 2912 } 2913 sdio_release_host(bus->sdiodev->func1); 2914 } 2915 if (!ret) { 2916 brcmf_dbg(SDIO, "ctrl_frame complete, err=%d\n", 2917 bus->ctrl_frame_err); 2918 rmb(); 2919 ret = bus->ctrl_frame_err; 2920 } 2921 2922 if (ret) 2923 bus->sdcnt.tx_ctlerrs++; 2924 else 2925 bus->sdcnt.tx_ctlpkts++; 2926 2927 return ret; 2928 } 2929 2930 #ifdef DEBUG 2931 static int brcmf_sdio_dump_console(struct seq_file *seq, struct brcmf_sdio *bus, 2932 struct sdpcm_shared *sh) 2933 { 2934 u32 addr, console_ptr, console_size, console_index; 2935 char *conbuf = NULL; 2936 __le32 sh_val; 2937 int rv; 2938 2939 /* obtain console information from device memory */ 2940 addr = sh->console_addr + offsetof(struct rte_console, log_le); 2941 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, 2942 (u8 *)&sh_val, sizeof(u32)); 2943 if (rv < 0) 2944 return rv; 2945 console_ptr = le32_to_cpu(sh_val); 2946 2947 addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size); 2948 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, 2949 (u8 *)&sh_val, sizeof(u32)); 2950 if (rv < 0) 2951 return rv; 2952 console_size = le32_to_cpu(sh_val); 2953 2954 addr = sh->console_addr + offsetof(struct rte_console, log_le.idx); 2955 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, 2956 (u8 *)&sh_val, sizeof(u32)); 2957 if (rv < 0) 2958 return rv; 2959 console_index = le32_to_cpu(sh_val); 2960 2961 /* allocate buffer for console data */ 2962 if (console_size <= CONSOLE_BUFFER_MAX) 2963 conbuf = vzalloc(console_size+1); 2964 2965 if (!conbuf) 2966 return -ENOMEM; 2967 2968 /* obtain the console data from device */ 2969 conbuf[console_size] = '\0'; 2970 rv = brcmf_sdiod_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf, 2971 console_size); 2972 if (rv < 0) 2973 goto done; 2974 2975 rv = seq_write(seq, conbuf + console_index, 2976 console_size - console_index); 2977 if (rv < 0) 2978 goto done; 2979 2980 if (console_index > 0) 2981 rv = seq_write(seq, conbuf, console_index - 1); 2982 2983 done: 2984 vfree(conbuf); 2985 return rv; 2986 } 2987 2988 static int brcmf_sdio_trap_info(struct seq_file *seq, struct brcmf_sdio *bus, 2989 struct sdpcm_shared *sh) 2990 { 2991 int error; 2992 struct brcmf_trap_info tr; 2993 2994 if ((sh->flags & SDPCM_SHARED_TRAP) == 0) { 2995 brcmf_dbg(INFO, "no trap in firmware\n"); 2996 return 0; 2997 } 2998 2999 error = brcmf_sdiod_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr, 3000 sizeof(struct brcmf_trap_info)); 3001 if (error < 0) 3002 return error; 3003 3004 if (seq) 3005 seq_printf(seq, 3006 "dongle trap info: type 0x%x @ epc 0x%08x\n" 3007 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n" 3008 " lr 0x%08x pc 0x%08x offset 0x%x\n" 3009 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n" 3010 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n", 3011 le32_to_cpu(tr.type), le32_to_cpu(tr.epc), 3012 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr), 3013 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14), 3014 le32_to_cpu(tr.pc), sh->trap_addr, 3015 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1), 3016 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3), 3017 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5), 3018 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7)); 3019 else 3020 pr_debug("dongle trap info: type 0x%x @ epc 0x%08x\n" 3021 " cpsr 0x%08x spsr 0x%08x sp 0x%08x\n" 3022 " lr 0x%08x pc 0x%08x offset 0x%x\n" 3023 " r0 0x%08x r1 0x%08x r2 0x%08x r3 0x%08x\n" 3024 " r4 0x%08x r5 0x%08x r6 0x%08x r7 0x%08x\n", 3025 le32_to_cpu(tr.type), le32_to_cpu(tr.epc), 3026 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr), 3027 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14), 3028 le32_to_cpu(tr.pc), sh->trap_addr, 3029 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1), 3030 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3), 3031 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5), 3032 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7)); 3033 return 0; 3034 } 3035 3036 static int brcmf_sdio_assert_info(struct seq_file *seq, struct brcmf_sdio *bus, 3037 struct sdpcm_shared *sh) 3038 { 3039 int error = 0; 3040 char file[80] = "?"; 3041 char expr[80] = "<???>"; 3042 3043 if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) { 3044 brcmf_dbg(INFO, "firmware not built with -assert\n"); 3045 return 0; 3046 } else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) { 3047 brcmf_dbg(INFO, "no assert in dongle\n"); 3048 return 0; 3049 } 3050 3051 sdio_claim_host(bus->sdiodev->func1); 3052 if (sh->assert_file_addr != 0) { 3053 error = brcmf_sdiod_ramrw(bus->sdiodev, false, 3054 sh->assert_file_addr, (u8 *)file, 80); 3055 if (error < 0) 3056 return error; 3057 } 3058 if (sh->assert_exp_addr != 0) { 3059 error = brcmf_sdiod_ramrw(bus->sdiodev, false, 3060 sh->assert_exp_addr, (u8 *)expr, 80); 3061 if (error < 0) 3062 return error; 3063 } 3064 sdio_release_host(bus->sdiodev->func1); 3065 3066 seq_printf(seq, "dongle assert: %s:%d: assert(%s)\n", 3067 file, sh->assert_line, expr); 3068 return 0; 3069 } 3070 3071 static int brcmf_sdio_checkdied(struct brcmf_sdio *bus) 3072 { 3073 int error; 3074 struct sdpcm_shared sh; 3075 3076 error = brcmf_sdio_readshared(bus, &sh); 3077 3078 if (error < 0) 3079 return error; 3080 3081 if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0) 3082 brcmf_dbg(INFO, "firmware not built with -assert\n"); 3083 else if (sh.flags & SDPCM_SHARED_ASSERT) 3084 brcmf_err("assertion in dongle\n"); 3085 3086 if (sh.flags & SDPCM_SHARED_TRAP) { 3087 brcmf_err("firmware trap in dongle\n"); 3088 brcmf_sdio_trap_info(NULL, bus, &sh); 3089 } 3090 3091 return 0; 3092 } 3093 3094 static int brcmf_sdio_died_dump(struct seq_file *seq, struct brcmf_sdio *bus) 3095 { 3096 int error = 0; 3097 struct sdpcm_shared sh; 3098 3099 error = brcmf_sdio_readshared(bus, &sh); 3100 if (error < 0) 3101 goto done; 3102 3103 error = brcmf_sdio_assert_info(seq, bus, &sh); 3104 if (error < 0) 3105 goto done; 3106 3107 error = brcmf_sdio_trap_info(seq, bus, &sh); 3108 if (error < 0) 3109 goto done; 3110 3111 error = brcmf_sdio_dump_console(seq, bus, &sh); 3112 3113 done: 3114 return error; 3115 } 3116 3117 static int brcmf_sdio_forensic_read(struct seq_file *seq, void *data) 3118 { 3119 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private); 3120 struct brcmf_sdio *bus = bus_if->bus_priv.sdio->bus; 3121 3122 return brcmf_sdio_died_dump(seq, bus); 3123 } 3124 3125 static int brcmf_debugfs_sdio_count_read(struct seq_file *seq, void *data) 3126 { 3127 struct brcmf_bus *bus_if = dev_get_drvdata(seq->private); 3128 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; 3129 struct brcmf_sdio_count *sdcnt = &sdiodev->bus->sdcnt; 3130 3131 seq_printf(seq, 3132 "intrcount: %u\nlastintrs: %u\n" 3133 "pollcnt: %u\nregfails: %u\n" 3134 "tx_sderrs: %u\nfcqueued: %u\n" 3135 "rxrtx: %u\nrx_toolong: %u\n" 3136 "rxc_errors: %u\nrx_hdrfail: %u\n" 3137 "rx_badhdr: %u\nrx_badseq: %u\n" 3138 "fc_rcvd: %u\nfc_xoff: %u\n" 3139 "fc_xon: %u\nrxglomfail: %u\n" 3140 "rxglomframes: %u\nrxglompkts: %u\n" 3141 "f2rxhdrs: %u\nf2rxdata: %u\n" 3142 "f2txdata: %u\nf1regdata: %u\n" 3143 "tickcnt: %u\ntx_ctlerrs: %lu\n" 3144 "tx_ctlpkts: %lu\nrx_ctlerrs: %lu\n" 3145 "rx_ctlpkts: %lu\nrx_readahead: %lu\n", 3146 sdcnt->intrcount, sdcnt->lastintrs, 3147 sdcnt->pollcnt, sdcnt->regfails, 3148 sdcnt->tx_sderrs, sdcnt->fcqueued, 3149 sdcnt->rxrtx, sdcnt->rx_toolong, 3150 sdcnt->rxc_errors, sdcnt->rx_hdrfail, 3151 sdcnt->rx_badhdr, sdcnt->rx_badseq, 3152 sdcnt->fc_rcvd, sdcnt->fc_xoff, 3153 sdcnt->fc_xon, sdcnt->rxglomfail, 3154 sdcnt->rxglomframes, sdcnt->rxglompkts, 3155 sdcnt->f2rxhdrs, sdcnt->f2rxdata, 3156 sdcnt->f2txdata, sdcnt->f1regdata, 3157 sdcnt->tickcnt, sdcnt->tx_ctlerrs, 3158 sdcnt->tx_ctlpkts, sdcnt->rx_ctlerrs, 3159 sdcnt->rx_ctlpkts, sdcnt->rx_readahead_cnt); 3160 3161 return 0; 3162 } 3163 3164 static void brcmf_sdio_debugfs_create(struct device *dev) 3165 { 3166 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 3167 struct brcmf_pub *drvr = bus_if->drvr; 3168 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; 3169 struct brcmf_sdio *bus = sdiodev->bus; 3170 struct dentry *dentry = brcmf_debugfs_get_devdir(drvr); 3171 3172 if (IS_ERR_OR_NULL(dentry)) 3173 return; 3174 3175 bus->console_interval = BRCMF_CONSOLE; 3176 3177 brcmf_debugfs_add_entry(drvr, "forensics", brcmf_sdio_forensic_read); 3178 brcmf_debugfs_add_entry(drvr, "counters", 3179 brcmf_debugfs_sdio_count_read); 3180 debugfs_create_u32("console_interval", 0644, dentry, 3181 &bus->console_interval); 3182 } 3183 #else 3184 static int brcmf_sdio_checkdied(struct brcmf_sdio *bus) 3185 { 3186 return 0; 3187 } 3188 3189 static void brcmf_sdio_debugfs_create(struct device *dev) 3190 { 3191 } 3192 #endif /* DEBUG */ 3193 3194 static int 3195 brcmf_sdio_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen) 3196 { 3197 int timeleft; 3198 uint rxlen = 0; 3199 bool pending; 3200 u8 *buf; 3201 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 3202 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; 3203 struct brcmf_sdio *bus = sdiodev->bus; 3204 3205 brcmf_dbg(TRACE, "Enter\n"); 3206 if (sdiodev->state != BRCMF_SDIOD_DATA) 3207 return -EIO; 3208 3209 /* Wait until control frame is available */ 3210 timeleft = brcmf_sdio_dcmd_resp_wait(bus, &bus->rxlen, &pending); 3211 3212 spin_lock_bh(&bus->rxctl_lock); 3213 rxlen = bus->rxlen; 3214 memcpy(msg, bus->rxctl, min(msglen, rxlen)); 3215 bus->rxctl = NULL; 3216 buf = bus->rxctl_orig; 3217 bus->rxctl_orig = NULL; 3218 bus->rxlen = 0; 3219 spin_unlock_bh(&bus->rxctl_lock); 3220 vfree(buf); 3221 3222 if (rxlen) { 3223 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n", 3224 rxlen, msglen); 3225 } else if (timeleft == 0) { 3226 brcmf_err("resumed on timeout\n"); 3227 brcmf_sdio_checkdied(bus); 3228 } else if (pending) { 3229 brcmf_dbg(CTL, "cancelled\n"); 3230 return -ERESTARTSYS; 3231 } else { 3232 brcmf_dbg(CTL, "resumed for unknown reason?\n"); 3233 brcmf_sdio_checkdied(bus); 3234 } 3235 3236 if (rxlen) 3237 bus->sdcnt.rx_ctlpkts++; 3238 else 3239 bus->sdcnt.rx_ctlerrs++; 3240 3241 return rxlen ? (int)rxlen : -ETIMEDOUT; 3242 } 3243 3244 #ifdef DEBUG 3245 static bool 3246 brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr, 3247 u8 *ram_data, uint ram_sz) 3248 { 3249 char *ram_cmp; 3250 int err; 3251 bool ret = true; 3252 int address; 3253 int offset; 3254 int len; 3255 3256 /* read back and verify */ 3257 brcmf_dbg(INFO, "Compare RAM dl & ul at 0x%08x; size=%d\n", ram_addr, 3258 ram_sz); 3259 ram_cmp = kmalloc(MEMBLOCK, GFP_KERNEL); 3260 /* do not proceed while no memory but */ 3261 if (!ram_cmp) 3262 return true; 3263 3264 address = ram_addr; 3265 offset = 0; 3266 while (offset < ram_sz) { 3267 len = ((offset + MEMBLOCK) < ram_sz) ? MEMBLOCK : 3268 ram_sz - offset; 3269 err = brcmf_sdiod_ramrw(sdiodev, false, address, ram_cmp, len); 3270 if (err) { 3271 brcmf_err("error %d on reading %d membytes at 0x%08x\n", 3272 err, len, address); 3273 ret = false; 3274 break; 3275 } else if (memcmp(ram_cmp, &ram_data[offset], len)) { 3276 brcmf_err("Downloaded RAM image is corrupted, block offset is %d, len is %d\n", 3277 offset, len); 3278 ret = false; 3279 break; 3280 } 3281 offset += len; 3282 address += len; 3283 } 3284 3285 kfree(ram_cmp); 3286 3287 return ret; 3288 } 3289 #else /* DEBUG */ 3290 static bool 3291 brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr, 3292 u8 *ram_data, uint ram_sz) 3293 { 3294 return true; 3295 } 3296 #endif /* DEBUG */ 3297 3298 static int brcmf_sdio_download_code_file(struct brcmf_sdio *bus, 3299 const struct firmware *fw) 3300 { 3301 int err; 3302 3303 brcmf_dbg(TRACE, "Enter\n"); 3304 3305 err = brcmf_sdiod_ramrw(bus->sdiodev, true, bus->ci->rambase, 3306 (u8 *)fw->data, fw->size); 3307 if (err) 3308 brcmf_err("error %d on writing %d membytes at 0x%08x\n", 3309 err, (int)fw->size, bus->ci->rambase); 3310 else if (!brcmf_sdio_verifymemory(bus->sdiodev, bus->ci->rambase, 3311 (u8 *)fw->data, fw->size)) 3312 err = -EIO; 3313 3314 return err; 3315 } 3316 3317 static int brcmf_sdio_download_nvram(struct brcmf_sdio *bus, 3318 void *vars, u32 varsz) 3319 { 3320 int address; 3321 int err; 3322 3323 brcmf_dbg(TRACE, "Enter\n"); 3324 3325 address = bus->ci->ramsize - varsz + bus->ci->rambase; 3326 err = brcmf_sdiod_ramrw(bus->sdiodev, true, address, vars, varsz); 3327 if (err) 3328 brcmf_err("error %d on writing %d nvram bytes at 0x%08x\n", 3329 err, varsz, address); 3330 else if (!brcmf_sdio_verifymemory(bus->sdiodev, address, vars, varsz)) 3331 err = -EIO; 3332 3333 return err; 3334 } 3335 3336 static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus, 3337 const struct firmware *fw, 3338 void *nvram, u32 nvlen) 3339 { 3340 int bcmerror; 3341 u32 rstvec; 3342 3343 sdio_claim_host(bus->sdiodev->func1); 3344 brcmf_sdio_clkctl(bus, CLK_AVAIL, false); 3345 3346 rstvec = get_unaligned_le32(fw->data); 3347 brcmf_dbg(SDIO, "firmware rstvec: %x\n", rstvec); 3348 3349 bcmerror = brcmf_sdio_download_code_file(bus, fw); 3350 release_firmware(fw); 3351 if (bcmerror) { 3352 brcmf_err("dongle image file download failed\n"); 3353 brcmf_fw_nvram_free(nvram); 3354 goto err; 3355 } 3356 3357 bcmerror = brcmf_sdio_download_nvram(bus, nvram, nvlen); 3358 brcmf_fw_nvram_free(nvram); 3359 if (bcmerror) { 3360 brcmf_err("dongle nvram file download failed\n"); 3361 goto err; 3362 } 3363 3364 /* Take arm out of reset */ 3365 if (!brcmf_chip_set_active(bus->ci, rstvec)) { 3366 brcmf_err("error getting out of ARM core reset\n"); 3367 goto err; 3368 } 3369 3370 err: 3371 brcmf_sdio_clkctl(bus, CLK_SDONLY, false); 3372 sdio_release_host(bus->sdiodev->func1); 3373 return bcmerror; 3374 } 3375 3376 static bool brcmf_sdio_aos_no_decode(struct brcmf_sdio *bus) 3377 { 3378 if (bus->ci->chip == CY_CC_43012_CHIP_ID || 3379 bus->ci->chip == CY_CC_4373_CHIP_ID || 3380 bus->ci->chip == BRCM_CC_4339_CHIP_ID || 3381 bus->ci->chip == BRCM_CC_4345_CHIP_ID || 3382 bus->ci->chip == BRCM_CC_4354_CHIP_ID) 3383 return true; 3384 else 3385 return false; 3386 } 3387 3388 static void brcmf_sdio_sr_init(struct brcmf_sdio *bus) 3389 { 3390 int err = 0; 3391 u8 val; 3392 u8 wakeupctrl; 3393 u8 cardcap; 3394 u8 chipclkcsr; 3395 3396 brcmf_dbg(TRACE, "Enter\n"); 3397 3398 if (brcmf_chip_is_ulp(bus->ci)) { 3399 wakeupctrl = SBSDIO_FUNC1_WCTRL_ALPWAIT_SHIFT; 3400 chipclkcsr = SBSDIO_HT_AVAIL_REQ; 3401 } else { 3402 wakeupctrl = SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT; 3403 chipclkcsr = SBSDIO_FORCE_HT; 3404 } 3405 3406 if (brcmf_sdio_aos_no_decode(bus)) { 3407 cardcap = SDIO_CCCR_BRCM_CARDCAP_CMD_NODEC; 3408 } else { 3409 cardcap = (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT | 3410 SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT); 3411 } 3412 3413 val = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, &err); 3414 if (err) { 3415 brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n"); 3416 return; 3417 } 3418 val |= 1 << wakeupctrl; 3419 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, val, &err); 3420 if (err) { 3421 brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n"); 3422 return; 3423 } 3424 3425 /* Add CMD14 Support */ 3426 brcmf_sdiod_func0_wb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP, 3427 cardcap, 3428 &err); 3429 if (err) { 3430 brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n"); 3431 return; 3432 } 3433 3434 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 3435 chipclkcsr, &err); 3436 if (err) { 3437 brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n"); 3438 return; 3439 } 3440 3441 /* set flag */ 3442 bus->sr_enabled = true; 3443 brcmf_dbg(INFO, "SR enabled\n"); 3444 } 3445 3446 /* enable KSO bit */ 3447 static int brcmf_sdio_kso_init(struct brcmf_sdio *bus) 3448 { 3449 struct brcmf_core *core = bus->sdio_core; 3450 u8 val; 3451 int err = 0; 3452 3453 brcmf_dbg(TRACE, "Enter\n"); 3454 3455 /* KSO bit added in SDIO core rev 12 */ 3456 if (core->rev < 12) 3457 return 0; 3458 3459 val = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, &err); 3460 if (err) { 3461 brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n"); 3462 return err; 3463 } 3464 3465 if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) { 3466 val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN << 3467 SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT); 3468 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, 3469 val, &err); 3470 if (err) { 3471 brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n"); 3472 return err; 3473 } 3474 } 3475 3476 return 0; 3477 } 3478 3479 3480 static int brcmf_sdio_bus_preinit(struct device *dev) 3481 { 3482 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 3483 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; 3484 struct brcmf_sdio *bus = sdiodev->bus; 3485 struct brcmf_core *core = bus->sdio_core; 3486 u32 value; 3487 int err; 3488 3489 /* maxctl provided by common layer */ 3490 if (WARN_ON(!bus_if->maxctl)) 3491 return -EINVAL; 3492 3493 /* Allocate control receive buffer */ 3494 bus_if->maxctl += bus->roundup; 3495 value = roundup((bus_if->maxctl + SDPCM_HDRLEN), ALIGNMENT); 3496 value += bus->head_align; 3497 bus->rxbuf = kmalloc(value, GFP_ATOMIC); 3498 if (bus->rxbuf) 3499 bus->rxblen = value; 3500 3501 /* the commands below use the terms tx and rx from 3502 * a device perspective, ie. bus:txglom affects the 3503 * bus transfers from device to host. 3504 */ 3505 if (core->rev < 12) { 3506 /* for sdio core rev < 12, disable txgloming */ 3507 value = 0; 3508 err = brcmf_iovar_data_set(dev, "bus:txglom", &value, 3509 sizeof(u32)); 3510 } else { 3511 /* otherwise, set txglomalign */ 3512 value = sdiodev->settings->bus.sdio.sd_sgentry_align; 3513 /* SDIO ADMA requires at least 32 bit alignment */ 3514 value = max_t(u32, value, ALIGNMENT); 3515 err = brcmf_iovar_data_set(dev, "bus:txglomalign", &value, 3516 sizeof(u32)); 3517 } 3518 3519 if (err < 0) 3520 goto done; 3521 3522 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN; 3523 if (sdiodev->sg_support) { 3524 bus->txglom = false; 3525 value = 1; 3526 err = brcmf_iovar_data_set(bus->sdiodev->dev, "bus:rxglom", 3527 &value, sizeof(u32)); 3528 if (err < 0) { 3529 /* bus:rxglom is allowed to fail */ 3530 err = 0; 3531 } else { 3532 bus->txglom = true; 3533 bus->tx_hdrlen += SDPCM_HWEXT_LEN; 3534 } 3535 } 3536 brcmf_bus_add_txhdrlen(bus->sdiodev->dev, bus->tx_hdrlen); 3537 3538 done: 3539 return err; 3540 } 3541 3542 static size_t brcmf_sdio_bus_get_ramsize(struct device *dev) 3543 { 3544 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 3545 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; 3546 struct brcmf_sdio *bus = sdiodev->bus; 3547 3548 return bus->ci->ramsize - bus->ci->srsize; 3549 } 3550 3551 static int brcmf_sdio_bus_get_memdump(struct device *dev, void *data, 3552 size_t mem_size) 3553 { 3554 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 3555 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio; 3556 struct brcmf_sdio *bus = sdiodev->bus; 3557 int err; 3558 int address; 3559 int offset; 3560 int len; 3561 3562 brcmf_dbg(INFO, "dump at 0x%08x: size=%zu\n", bus->ci->rambase, 3563 mem_size); 3564 3565 address = bus->ci->rambase; 3566 offset = err = 0; 3567 sdio_claim_host(sdiodev->func1); 3568 while (offset < mem_size) { 3569 len = ((offset + MEMBLOCK) < mem_size) ? MEMBLOCK : 3570 mem_size - offset; 3571 err = brcmf_sdiod_ramrw(sdiodev, false, address, data, len); 3572 if (err) { 3573 brcmf_err("error %d on reading %d membytes at 0x%08x\n", 3574 err, len, address); 3575 goto done; 3576 } 3577 data += len; 3578 offset += len; 3579 address += len; 3580 } 3581 3582 done: 3583 sdio_release_host(sdiodev->func1); 3584 return err; 3585 } 3586 3587 void brcmf_sdio_trigger_dpc(struct brcmf_sdio *bus) 3588 { 3589 if (!bus->dpc_triggered) { 3590 bus->dpc_triggered = true; 3591 queue_work(bus->brcmf_wq, &bus->datawork); 3592 } 3593 } 3594 3595 void brcmf_sdio_isr(struct brcmf_sdio *bus) 3596 { 3597 brcmf_dbg(TRACE, "Enter\n"); 3598 3599 if (!bus) { 3600 brcmf_err("bus is null pointer, exiting\n"); 3601 return; 3602 } 3603 3604 /* Count the interrupt call */ 3605 bus->sdcnt.intrcount++; 3606 if (in_interrupt()) 3607 atomic_set(&bus->ipend, 1); 3608 else 3609 if (brcmf_sdio_intr_rstatus(bus)) { 3610 brcmf_err("failed backplane access\n"); 3611 } 3612 3613 /* Disable additional interrupts (is this needed now)? */ 3614 if (!bus->intr) 3615 brcmf_err("isr w/o interrupt configured!\n"); 3616 3617 bus->dpc_triggered = true; 3618 queue_work(bus->brcmf_wq, &bus->datawork); 3619 } 3620 3621 static void brcmf_sdio_bus_watchdog(struct brcmf_sdio *bus) 3622 { 3623 brcmf_dbg(TIMER, "Enter\n"); 3624 3625 /* Poll period: check device if appropriate. */ 3626 if (!bus->sr_enabled && 3627 bus->poll && (++bus->polltick >= bus->pollrate)) { 3628 u32 intstatus = 0; 3629 3630 /* Reset poll tick */ 3631 bus->polltick = 0; 3632 3633 /* Check device if no interrupts */ 3634 if (!bus->intr || 3635 (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) { 3636 3637 if (!bus->dpc_triggered) { 3638 u8 devpend; 3639 3640 sdio_claim_host(bus->sdiodev->func1); 3641 devpend = brcmf_sdiod_func0_rb(bus->sdiodev, 3642 SDIO_CCCR_INTx, NULL); 3643 sdio_release_host(bus->sdiodev->func1); 3644 intstatus = devpend & (INTR_STATUS_FUNC1 | 3645 INTR_STATUS_FUNC2); 3646 } 3647 3648 /* If there is something, make like the ISR and 3649 schedule the DPC */ 3650 if (intstatus) { 3651 bus->sdcnt.pollcnt++; 3652 atomic_set(&bus->ipend, 1); 3653 3654 bus->dpc_triggered = true; 3655 queue_work(bus->brcmf_wq, &bus->datawork); 3656 } 3657 } 3658 3659 /* Update interrupt tracking */ 3660 bus->sdcnt.lastintrs = bus->sdcnt.intrcount; 3661 } 3662 #ifdef DEBUG 3663 /* Poll for console output periodically */ 3664 if (bus->sdiodev->state == BRCMF_SDIOD_DATA && BRCMF_FWCON_ON() && 3665 bus->console_interval != 0) { 3666 bus->console.count += jiffies_to_msecs(BRCMF_WD_POLL); 3667 if (bus->console.count >= bus->console_interval) { 3668 bus->console.count -= bus->console_interval; 3669 sdio_claim_host(bus->sdiodev->func1); 3670 /* Make sure backplane clock is on */ 3671 brcmf_sdio_bus_sleep(bus, false, false); 3672 if (brcmf_sdio_readconsole(bus) < 0) 3673 /* stop on error */ 3674 bus->console_interval = 0; 3675 sdio_release_host(bus->sdiodev->func1); 3676 } 3677 } 3678 #endif /* DEBUG */ 3679 3680 /* On idle timeout clear activity flag and/or turn off clock */ 3681 if (!bus->dpc_triggered) { 3682 rmb(); 3683 if ((!bus->dpc_running) && (bus->idletime > 0) && 3684 (bus->clkstate == CLK_AVAIL)) { 3685 bus->idlecount++; 3686 if (bus->idlecount > bus->idletime) { 3687 brcmf_dbg(SDIO, "idle\n"); 3688 sdio_claim_host(bus->sdiodev->func1); 3689 brcmf_sdio_wd_timer(bus, false); 3690 bus->idlecount = 0; 3691 brcmf_sdio_bus_sleep(bus, true, false); 3692 sdio_release_host(bus->sdiodev->func1); 3693 } 3694 } else { 3695 bus->idlecount = 0; 3696 } 3697 } else { 3698 bus->idlecount = 0; 3699 } 3700 } 3701 3702 static void brcmf_sdio_dataworker(struct work_struct *work) 3703 { 3704 struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio, 3705 datawork); 3706 3707 bus->dpc_running = true; 3708 wmb(); 3709 while (READ_ONCE(bus->dpc_triggered)) { 3710 bus->dpc_triggered = false; 3711 brcmf_sdio_dpc(bus); 3712 bus->idlecount = 0; 3713 } 3714 bus->dpc_running = false; 3715 if (brcmf_sdiod_freezing(bus->sdiodev)) { 3716 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DOWN); 3717 brcmf_sdiod_try_freeze(bus->sdiodev); 3718 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA); 3719 } 3720 } 3721 3722 static void 3723 brcmf_sdio_drivestrengthinit(struct brcmf_sdio_dev *sdiodev, 3724 struct brcmf_chip *ci, u32 drivestrength) 3725 { 3726 const struct sdiod_drive_str *str_tab = NULL; 3727 u32 str_mask; 3728 u32 str_shift; 3729 u32 i; 3730 u32 drivestrength_sel = 0; 3731 u32 cc_data_temp; 3732 u32 addr; 3733 3734 if (!(ci->cc_caps & CC_CAP_PMU)) 3735 return; 3736 3737 switch (SDIOD_DRVSTR_KEY(ci->chip, ci->pmurev)) { 3738 case SDIOD_DRVSTR_KEY(BRCM_CC_4330_CHIP_ID, 12): 3739 str_tab = sdiod_drvstr_tab1_1v8; 3740 str_mask = 0x00003800; 3741 str_shift = 11; 3742 break; 3743 case SDIOD_DRVSTR_KEY(BRCM_CC_4334_CHIP_ID, 17): 3744 str_tab = sdiod_drvstr_tab6_1v8; 3745 str_mask = 0x00001800; 3746 str_shift = 11; 3747 break; 3748 case SDIOD_DRVSTR_KEY(BRCM_CC_43143_CHIP_ID, 17): 3749 /* note: 43143 does not support tristate */ 3750 i = ARRAY_SIZE(sdiod_drvstr_tab2_3v3) - 1; 3751 if (drivestrength >= sdiod_drvstr_tab2_3v3[i].strength) { 3752 str_tab = sdiod_drvstr_tab2_3v3; 3753 str_mask = 0x00000007; 3754 str_shift = 0; 3755 } else 3756 brcmf_err("Invalid SDIO Drive strength for chip %s, strength=%d\n", 3757 ci->name, drivestrength); 3758 break; 3759 case SDIOD_DRVSTR_KEY(BRCM_CC_43362_CHIP_ID, 13): 3760 str_tab = sdiod_drive_strength_tab5_1v8; 3761 str_mask = 0x00003800; 3762 str_shift = 11; 3763 break; 3764 default: 3765 brcmf_dbg(INFO, "No SDIO driver strength init needed for chip %s rev %d pmurev %d\n", 3766 ci->name, ci->chiprev, ci->pmurev); 3767 break; 3768 } 3769 3770 if (str_tab != NULL) { 3771 struct brcmf_core *pmu = brcmf_chip_get_pmu(ci); 3772 3773 for (i = 0; str_tab[i].strength != 0; i++) { 3774 if (drivestrength >= str_tab[i].strength) { 3775 drivestrength_sel = str_tab[i].sel; 3776 break; 3777 } 3778 } 3779 addr = CORE_CC_REG(pmu->base, chipcontrol_addr); 3780 brcmf_sdiod_writel(sdiodev, addr, 1, NULL); 3781 cc_data_temp = brcmf_sdiod_readl(sdiodev, addr, NULL); 3782 cc_data_temp &= ~str_mask; 3783 drivestrength_sel <<= str_shift; 3784 cc_data_temp |= drivestrength_sel; 3785 brcmf_sdiod_writel(sdiodev, addr, cc_data_temp, NULL); 3786 3787 brcmf_dbg(INFO, "SDIO: %d mA (req=%d mA) drive strength selected, set to 0x%08x\n", 3788 str_tab[i].strength, drivestrength, cc_data_temp); 3789 } 3790 } 3791 3792 static int brcmf_sdio_buscoreprep(void *ctx) 3793 { 3794 struct brcmf_sdio_dev *sdiodev = ctx; 3795 int err = 0; 3796 u8 clkval, clkset; 3797 3798 /* Try forcing SDIO core to do ALPAvail request only */ 3799 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ; 3800 brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err); 3801 if (err) { 3802 brcmf_err("error writing for HT off\n"); 3803 return err; 3804 } 3805 3806 /* If register supported, wait for ALPAvail and then force ALP */ 3807 /* This may take up to 15 milliseconds */ 3808 clkval = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, NULL); 3809 3810 if ((clkval & ~SBSDIO_AVBITS) != clkset) { 3811 brcmf_err("ChipClkCSR access: wrote 0x%02x read 0x%02x\n", 3812 clkset, clkval); 3813 return -EACCES; 3814 } 3815 3816 SPINWAIT(((clkval = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 3817 NULL)), 3818 !SBSDIO_ALPAV(clkval)), 3819 PMU_MAX_TRANSITION_DLY); 3820 3821 if (!SBSDIO_ALPAV(clkval)) { 3822 brcmf_err("timeout on ALPAV wait, clkval 0x%02x\n", 3823 clkval); 3824 return -EBUSY; 3825 } 3826 3827 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP; 3828 brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err); 3829 udelay(65); 3830 3831 /* Also, disable the extra SDIO pull-ups */ 3832 brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_SDIOPULLUP, 0, NULL); 3833 3834 return 0; 3835 } 3836 3837 static void brcmf_sdio_buscore_activate(void *ctx, struct brcmf_chip *chip, 3838 u32 rstvec) 3839 { 3840 struct brcmf_sdio_dev *sdiodev = ctx; 3841 struct brcmf_core *core = sdiodev->bus->sdio_core; 3842 u32 reg_addr; 3843 3844 /* clear all interrupts */ 3845 reg_addr = core->base + SD_REG(intstatus); 3846 brcmf_sdiod_writel(sdiodev, reg_addr, 0xFFFFFFFF, NULL); 3847 3848 if (rstvec) 3849 /* Write reset vector to address 0 */ 3850 brcmf_sdiod_ramrw(sdiodev, true, 0, (void *)&rstvec, 3851 sizeof(rstvec)); 3852 } 3853 3854 static u32 brcmf_sdio_buscore_read32(void *ctx, u32 addr) 3855 { 3856 struct brcmf_sdio_dev *sdiodev = ctx; 3857 u32 val, rev; 3858 3859 val = brcmf_sdiod_readl(sdiodev, addr, NULL); 3860 3861 /* 3862 * this is a bit of special handling if reading the chipcommon chipid 3863 * register. The 4339 is a next-gen of the 4335. It uses the same 3864 * SDIO device id as 4335 and the chipid register returns 4335 as well. 3865 * It can be identified as 4339 by looking at the chip revision. It 3866 * is corrected here so the chip.c module has the right info. 3867 */ 3868 if (addr == CORE_CC_REG(SI_ENUM_BASE, chipid) && 3869 (sdiodev->func1->device == SDIO_DEVICE_ID_BROADCOM_4339 || 3870 sdiodev->func1->device == SDIO_DEVICE_ID_BROADCOM_4335_4339)) { 3871 rev = (val & CID_REV_MASK) >> CID_REV_SHIFT; 3872 if (rev >= 2) { 3873 val &= ~CID_ID_MASK; 3874 val |= BRCM_CC_4339_CHIP_ID; 3875 } 3876 } 3877 3878 return val; 3879 } 3880 3881 static void brcmf_sdio_buscore_write32(void *ctx, u32 addr, u32 val) 3882 { 3883 struct brcmf_sdio_dev *sdiodev = ctx; 3884 3885 brcmf_sdiod_writel(sdiodev, addr, val, NULL); 3886 } 3887 3888 static const struct brcmf_buscore_ops brcmf_sdio_buscore_ops = { 3889 .prepare = brcmf_sdio_buscoreprep, 3890 .activate = brcmf_sdio_buscore_activate, 3891 .read32 = brcmf_sdio_buscore_read32, 3892 .write32 = brcmf_sdio_buscore_write32, 3893 }; 3894 3895 static bool 3896 brcmf_sdio_probe_attach(struct brcmf_sdio *bus) 3897 { 3898 struct brcmf_sdio_dev *sdiodev; 3899 u8 clkctl = 0; 3900 int err = 0; 3901 int reg_addr; 3902 u32 reg_val; 3903 u32 drivestrength; 3904 3905 sdiodev = bus->sdiodev; 3906 sdio_claim_host(sdiodev->func1); 3907 3908 pr_debug("F1 signature read @0x18000000=0x%4x\n", 3909 brcmf_sdiod_readl(sdiodev, SI_ENUM_BASE, NULL)); 3910 3911 /* 3912 * Force PLL off until brcmf_chip_attach() 3913 * programs PLL control regs 3914 */ 3915 3916 brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, BRCMF_INIT_CLKCTL1, 3917 &err); 3918 if (!err) 3919 clkctl = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 3920 &err); 3921 3922 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) { 3923 brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n", 3924 err, BRCMF_INIT_CLKCTL1, clkctl); 3925 goto fail; 3926 } 3927 3928 bus->ci = brcmf_chip_attach(sdiodev, &brcmf_sdio_buscore_ops); 3929 if (IS_ERR(bus->ci)) { 3930 brcmf_err("brcmf_chip_attach failed!\n"); 3931 bus->ci = NULL; 3932 goto fail; 3933 } 3934 3935 /* Pick up the SDIO core info struct from chip.c */ 3936 bus->sdio_core = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV); 3937 if (!bus->sdio_core) 3938 goto fail; 3939 3940 /* Pick up the CHIPCOMMON core info struct, for bulk IO in bcmsdh.c */ 3941 sdiodev->cc_core = brcmf_chip_get_core(bus->ci, BCMA_CORE_CHIPCOMMON); 3942 if (!sdiodev->cc_core) 3943 goto fail; 3944 3945 sdiodev->settings = brcmf_get_module_param(sdiodev->dev, 3946 BRCMF_BUSTYPE_SDIO, 3947 bus->ci->chip, 3948 bus->ci->chiprev); 3949 if (!sdiodev->settings) { 3950 brcmf_err("Failed to get device parameters\n"); 3951 goto fail; 3952 } 3953 /* platform specific configuration: 3954 * alignments must be at least 4 bytes for ADMA 3955 */ 3956 bus->head_align = ALIGNMENT; 3957 bus->sgentry_align = ALIGNMENT; 3958 if (sdiodev->settings->bus.sdio.sd_head_align > ALIGNMENT) 3959 bus->head_align = sdiodev->settings->bus.sdio.sd_head_align; 3960 if (sdiodev->settings->bus.sdio.sd_sgentry_align > ALIGNMENT) 3961 bus->sgentry_align = 3962 sdiodev->settings->bus.sdio.sd_sgentry_align; 3963 3964 /* allocate scatter-gather table. sg support 3965 * will be disabled upon allocation failure. 3966 */ 3967 brcmf_sdiod_sgtable_alloc(sdiodev); 3968 3969 #ifdef CONFIG_PM_SLEEP 3970 /* wowl can be supported when KEEP_POWER is true and (WAKE_SDIO_IRQ 3971 * is true or when platform data OOB irq is true). 3972 */ 3973 if ((sdio_get_host_pm_caps(sdiodev->func1) & MMC_PM_KEEP_POWER) && 3974 ((sdio_get_host_pm_caps(sdiodev->func1) & MMC_PM_WAKE_SDIO_IRQ) || 3975 (sdiodev->settings->bus.sdio.oob_irq_supported))) 3976 sdiodev->bus_if->wowl_supported = true; 3977 #endif 3978 3979 if (brcmf_sdio_kso_init(bus)) { 3980 brcmf_err("error enabling KSO\n"); 3981 goto fail; 3982 } 3983 3984 if (sdiodev->settings->bus.sdio.drive_strength) 3985 drivestrength = sdiodev->settings->bus.sdio.drive_strength; 3986 else 3987 drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH; 3988 brcmf_sdio_drivestrengthinit(sdiodev, bus->ci, drivestrength); 3989 3990 /* Set card control so an SDIO card reset does a WLAN backplane reset */ 3991 reg_val = brcmf_sdiod_func0_rb(sdiodev, SDIO_CCCR_BRCM_CARDCTRL, &err); 3992 if (err) 3993 goto fail; 3994 3995 reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET; 3996 3997 brcmf_sdiod_func0_wb(sdiodev, SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err); 3998 if (err) 3999 goto fail; 4000 4001 /* set PMUControl so a backplane reset does PMU state reload */ 4002 reg_addr = CORE_CC_REG(brcmf_chip_get_pmu(bus->ci)->base, pmucontrol); 4003 reg_val = brcmf_sdiod_readl(sdiodev, reg_addr, &err); 4004 if (err) 4005 goto fail; 4006 4007 reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT); 4008 4009 brcmf_sdiod_writel(sdiodev, reg_addr, reg_val, &err); 4010 if (err) 4011 goto fail; 4012 4013 sdio_release_host(sdiodev->func1); 4014 4015 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN); 4016 4017 /* allocate header buffer */ 4018 bus->hdrbuf = kzalloc(MAX_HDR_READ + bus->head_align, GFP_KERNEL); 4019 if (!bus->hdrbuf) 4020 return false; 4021 /* Locate an appropriately-aligned portion of hdrbuf */ 4022 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0], 4023 bus->head_align); 4024 4025 /* Set the poll and/or interrupt flags */ 4026 bus->intr = true; 4027 bus->poll = false; 4028 if (bus->poll) 4029 bus->pollrate = 1; 4030 4031 return true; 4032 4033 fail: 4034 sdio_release_host(sdiodev->func1); 4035 return false; 4036 } 4037 4038 static int 4039 brcmf_sdio_watchdog_thread(void *data) 4040 { 4041 struct brcmf_sdio *bus = (struct brcmf_sdio *)data; 4042 int wait; 4043 4044 allow_signal(SIGTERM); 4045 /* Run until signal received */ 4046 brcmf_sdiod_freezer_count(bus->sdiodev); 4047 while (1) { 4048 if (kthread_should_stop()) 4049 break; 4050 brcmf_sdiod_freezer_uncount(bus->sdiodev); 4051 wait = wait_for_completion_interruptible(&bus->watchdog_wait); 4052 brcmf_sdiod_freezer_count(bus->sdiodev); 4053 brcmf_sdiod_try_freeze(bus->sdiodev); 4054 if (!wait) { 4055 brcmf_sdio_bus_watchdog(bus); 4056 /* Count the tick for reference */ 4057 bus->sdcnt.tickcnt++; 4058 reinit_completion(&bus->watchdog_wait); 4059 } else 4060 break; 4061 } 4062 return 0; 4063 } 4064 4065 static void 4066 brcmf_sdio_watchdog(struct timer_list *t) 4067 { 4068 struct brcmf_sdio *bus = from_timer(bus, t, timer); 4069 4070 if (bus->watchdog_tsk) { 4071 complete(&bus->watchdog_wait); 4072 /* Reschedule the watchdog */ 4073 if (bus->wd_active) 4074 mod_timer(&bus->timer, 4075 jiffies + BRCMF_WD_POLL); 4076 } 4077 } 4078 4079 static 4080 int brcmf_sdio_get_fwname(struct device *dev, const char *ext, u8 *fw_name) 4081 { 4082 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 4083 struct brcmf_fw_request *fwreq; 4084 struct brcmf_fw_name fwnames[] = { 4085 { ext, fw_name }, 4086 }; 4087 4088 fwreq = brcmf_fw_alloc_request(bus_if->chip, bus_if->chiprev, 4089 brcmf_sdio_fwnames, 4090 ARRAY_SIZE(brcmf_sdio_fwnames), 4091 fwnames, ARRAY_SIZE(fwnames)); 4092 if (!fwreq) 4093 return -ENOMEM; 4094 4095 kfree(fwreq); 4096 return 0; 4097 } 4098 4099 static const struct brcmf_bus_ops brcmf_sdio_bus_ops = { 4100 .stop = brcmf_sdio_bus_stop, 4101 .preinit = brcmf_sdio_bus_preinit, 4102 .txdata = brcmf_sdio_bus_txdata, 4103 .txctl = brcmf_sdio_bus_txctl, 4104 .rxctl = brcmf_sdio_bus_rxctl, 4105 .gettxq = brcmf_sdio_bus_gettxq, 4106 .wowl_config = brcmf_sdio_wowl_config, 4107 .get_ramsize = brcmf_sdio_bus_get_ramsize, 4108 .get_memdump = brcmf_sdio_bus_get_memdump, 4109 .get_fwname = brcmf_sdio_get_fwname, 4110 .debugfs_create = brcmf_sdio_debugfs_create 4111 }; 4112 4113 #define BRCMF_SDIO_FW_CODE 0 4114 #define BRCMF_SDIO_FW_NVRAM 1 4115 4116 static void brcmf_sdio_firmware_callback(struct device *dev, int err, 4117 struct brcmf_fw_request *fwreq) 4118 { 4119 struct brcmf_bus *bus_if = dev_get_drvdata(dev); 4120 struct brcmf_sdio_dev *sdiod = bus_if->bus_priv.sdio; 4121 struct brcmf_sdio *bus = sdiod->bus; 4122 struct brcmf_core *core = bus->sdio_core; 4123 const struct firmware *code; 4124 void *nvram; 4125 u32 nvram_len; 4126 u8 saveclk, bpreq; 4127 u8 devctl; 4128 4129 brcmf_dbg(TRACE, "Enter: dev=%s, err=%d\n", dev_name(dev), err); 4130 4131 if (err) 4132 goto fail; 4133 4134 code = fwreq->items[BRCMF_SDIO_FW_CODE].binary; 4135 nvram = fwreq->items[BRCMF_SDIO_FW_NVRAM].nv_data.data; 4136 nvram_len = fwreq->items[BRCMF_SDIO_FW_NVRAM].nv_data.len; 4137 kfree(fwreq); 4138 4139 /* try to download image and nvram to the dongle */ 4140 bus->alp_only = true; 4141 err = brcmf_sdio_download_firmware(bus, code, nvram, nvram_len); 4142 if (err) 4143 goto fail; 4144 bus->alp_only = false; 4145 4146 /* Start the watchdog timer */ 4147 bus->sdcnt.tickcnt = 0; 4148 brcmf_sdio_wd_timer(bus, true); 4149 4150 sdio_claim_host(sdiod->func1); 4151 4152 /* Make sure backplane clock is on, needed to generate F2 interrupt */ 4153 brcmf_sdio_clkctl(bus, CLK_AVAIL, false); 4154 if (bus->clkstate != CLK_AVAIL) 4155 goto release; 4156 4157 /* Force clocks on backplane to be sure F2 interrupt propagates */ 4158 saveclk = brcmf_sdiod_readb(sdiod, SBSDIO_FUNC1_CHIPCLKCSR, &err); 4159 if (!err) { 4160 bpreq = saveclk; 4161 bpreq |= brcmf_chip_is_ulp(bus->ci) ? 4162 SBSDIO_HT_AVAIL_REQ : SBSDIO_FORCE_HT; 4163 brcmf_sdiod_writeb(sdiod, SBSDIO_FUNC1_CHIPCLKCSR, 4164 bpreq, &err); 4165 } 4166 if (err) { 4167 brcmf_err("Failed to force clock for F2: err %d\n", err); 4168 goto release; 4169 } 4170 4171 /* Enable function 2 (frame transfers) */ 4172 brcmf_sdiod_writel(sdiod, core->base + SD_REG(tosbmailboxdata), 4173 SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT, NULL); 4174 4175 err = sdio_enable_func(sdiod->func2); 4176 4177 brcmf_dbg(INFO, "enable F2: err=%d\n", err); 4178 4179 /* If F2 successfully enabled, set core and enable interrupts */ 4180 if (!err) { 4181 /* Set up the interrupt mask and enable interrupts */ 4182 bus->hostintmask = HOSTINTMASK; 4183 brcmf_sdiod_writel(sdiod, core->base + SD_REG(hostintmask), 4184 bus->hostintmask, NULL); 4185 4186 switch (sdiod->func1->device) { 4187 case SDIO_DEVICE_ID_CYPRESS_4373: 4188 brcmf_dbg(INFO, "set F2 watermark to 0x%x*4 bytes\n", 4189 CY_4373_F2_WATERMARK); 4190 brcmf_sdiod_writeb(sdiod, SBSDIO_WATERMARK, 4191 CY_4373_F2_WATERMARK, &err); 4192 devctl = brcmf_sdiod_readb(sdiod, SBSDIO_DEVICE_CTL, 4193 &err); 4194 devctl |= SBSDIO_DEVCTL_F2WM_ENAB; 4195 brcmf_sdiod_writeb(sdiod, SBSDIO_DEVICE_CTL, devctl, 4196 &err); 4197 brcmf_sdiod_writeb(sdiod, SBSDIO_FUNC1_MESBUSYCTRL, 4198 CY_4373_F2_WATERMARK | 4199 SBSDIO_MESBUSYCTRL_ENAB, &err); 4200 break; 4201 case SDIO_DEVICE_ID_CYPRESS_43012: 4202 brcmf_dbg(INFO, "set F2 watermark to 0x%x*4 bytes\n", 4203 CY_43012_F2_WATERMARK); 4204 brcmf_sdiod_writeb(sdiod, SBSDIO_WATERMARK, 4205 CY_43012_F2_WATERMARK, &err); 4206 devctl = brcmf_sdiod_readb(sdiod, SBSDIO_DEVICE_CTL, 4207 &err); 4208 devctl |= SBSDIO_DEVCTL_F2WM_ENAB; 4209 brcmf_sdiod_writeb(sdiod, SBSDIO_DEVICE_CTL, devctl, 4210 &err); 4211 break; 4212 default: 4213 brcmf_sdiod_writeb(sdiod, SBSDIO_WATERMARK, 4214 DEFAULT_F2_WATERMARK, &err); 4215 break; 4216 } 4217 } else { 4218 /* Disable F2 again */ 4219 sdio_disable_func(sdiod->func2); 4220 goto checkdied; 4221 } 4222 4223 if (brcmf_chip_sr_capable(bus->ci)) { 4224 brcmf_sdio_sr_init(bus); 4225 } else { 4226 /* Restore previous clock setting */ 4227 brcmf_sdiod_writeb(sdiod, SBSDIO_FUNC1_CHIPCLKCSR, 4228 saveclk, &err); 4229 } 4230 4231 if (err == 0) { 4232 /* Allow full data communication using DPC from now on. */ 4233 brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA); 4234 4235 err = brcmf_sdiod_intr_register(sdiod); 4236 if (err != 0) 4237 brcmf_err("intr register failed:%d\n", err); 4238 } 4239 4240 /* If we didn't come up, turn off backplane clock */ 4241 if (err != 0) { 4242 brcmf_sdio_clkctl(bus, CLK_NONE, false); 4243 goto checkdied; 4244 } 4245 4246 sdio_release_host(sdiod->func1); 4247 4248 /* Assign bus interface call back */ 4249 sdiod->bus_if->dev = sdiod->dev; 4250 sdiod->bus_if->ops = &brcmf_sdio_bus_ops; 4251 sdiod->bus_if->chip = bus->ci->chip; 4252 sdiod->bus_if->chiprev = bus->ci->chiprev; 4253 4254 /* Attach to the common layer, reserve hdr space */ 4255 err = brcmf_attach(sdiod->dev, sdiod->settings); 4256 if (err != 0) { 4257 brcmf_err("brcmf_attach failed\n"); 4258 sdio_claim_host(sdiod->func1); 4259 goto checkdied; 4260 } 4261 4262 /* ready */ 4263 return; 4264 4265 checkdied: 4266 brcmf_sdio_checkdied(bus); 4267 release: 4268 sdio_release_host(sdiod->func1); 4269 fail: 4270 brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), err); 4271 device_release_driver(&sdiod->func2->dev); 4272 device_release_driver(dev); 4273 } 4274 4275 static struct brcmf_fw_request * 4276 brcmf_sdio_prepare_fw_request(struct brcmf_sdio *bus) 4277 { 4278 struct brcmf_fw_request *fwreq; 4279 struct brcmf_fw_name fwnames[] = { 4280 { ".bin", bus->sdiodev->fw_name }, 4281 { ".txt", bus->sdiodev->nvram_name }, 4282 }; 4283 4284 fwreq = brcmf_fw_alloc_request(bus->ci->chip, bus->ci->chiprev, 4285 brcmf_sdio_fwnames, 4286 ARRAY_SIZE(brcmf_sdio_fwnames), 4287 fwnames, ARRAY_SIZE(fwnames)); 4288 if (!fwreq) 4289 return NULL; 4290 4291 fwreq->items[BRCMF_SDIO_FW_CODE].type = BRCMF_FW_TYPE_BINARY; 4292 fwreq->items[BRCMF_SDIO_FW_NVRAM].type = BRCMF_FW_TYPE_NVRAM; 4293 fwreq->board_type = bus->sdiodev->settings->board_type; 4294 4295 return fwreq; 4296 } 4297 4298 struct brcmf_sdio *brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev) 4299 { 4300 int ret; 4301 struct brcmf_sdio *bus; 4302 struct workqueue_struct *wq; 4303 struct brcmf_fw_request *fwreq; 4304 4305 brcmf_dbg(TRACE, "Enter\n"); 4306 4307 /* Allocate private bus interface state */ 4308 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC); 4309 if (!bus) 4310 goto fail; 4311 4312 bus->sdiodev = sdiodev; 4313 sdiodev->bus = bus; 4314 skb_queue_head_init(&bus->glom); 4315 bus->txbound = BRCMF_TXBOUND; 4316 bus->rxbound = BRCMF_RXBOUND; 4317 bus->txminmax = BRCMF_TXMINMAX; 4318 bus->tx_seq = SDPCM_SEQ_WRAP - 1; 4319 4320 /* single-threaded workqueue */ 4321 wq = alloc_ordered_workqueue("brcmf_wq/%s", WQ_MEM_RECLAIM, 4322 dev_name(&sdiodev->func1->dev)); 4323 if (!wq) { 4324 brcmf_err("insufficient memory to create txworkqueue\n"); 4325 goto fail; 4326 } 4327 brcmf_sdiod_freezer_count(sdiodev); 4328 INIT_WORK(&bus->datawork, brcmf_sdio_dataworker); 4329 bus->brcmf_wq = wq; 4330 4331 /* attempt to attach to the dongle */ 4332 if (!(brcmf_sdio_probe_attach(bus))) { 4333 brcmf_err("brcmf_sdio_probe_attach failed\n"); 4334 goto fail; 4335 } 4336 4337 spin_lock_init(&bus->rxctl_lock); 4338 spin_lock_init(&bus->txq_lock); 4339 init_waitqueue_head(&bus->ctrl_wait); 4340 init_waitqueue_head(&bus->dcmd_resp_wait); 4341 4342 /* Set up the watchdog timer */ 4343 timer_setup(&bus->timer, brcmf_sdio_watchdog, 0); 4344 /* Initialize watchdog thread */ 4345 init_completion(&bus->watchdog_wait); 4346 bus->watchdog_tsk = kthread_run(brcmf_sdio_watchdog_thread, 4347 bus, "brcmf_wdog/%s", 4348 dev_name(&sdiodev->func1->dev)); 4349 if (IS_ERR(bus->watchdog_tsk)) { 4350 pr_warn("brcmf_watchdog thread failed to start\n"); 4351 bus->watchdog_tsk = NULL; 4352 } 4353 /* Initialize DPC thread */ 4354 bus->dpc_triggered = false; 4355 bus->dpc_running = false; 4356 4357 /* default sdio bus header length for tx packet */ 4358 bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN; 4359 4360 /* Query the F2 block size, set roundup accordingly */ 4361 bus->blocksize = bus->sdiodev->func2->cur_blksize; 4362 bus->roundup = min(max_roundup, bus->blocksize); 4363 4364 sdio_claim_host(bus->sdiodev->func1); 4365 4366 /* Disable F2 to clear any intermediate frame state on the dongle */ 4367 sdio_disable_func(bus->sdiodev->func2); 4368 4369 bus->rxflow = false; 4370 4371 /* Done with backplane-dependent accesses, can drop clock... */ 4372 brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL); 4373 4374 sdio_release_host(bus->sdiodev->func1); 4375 4376 /* ...and initialize clock/power states */ 4377 bus->clkstate = CLK_SDONLY; 4378 bus->idletime = BRCMF_IDLE_INTERVAL; 4379 bus->idleclock = BRCMF_IDLE_ACTIVE; 4380 4381 /* SR state */ 4382 bus->sr_enabled = false; 4383 4384 brcmf_dbg(INFO, "completed!!\n"); 4385 4386 fwreq = brcmf_sdio_prepare_fw_request(bus); 4387 if (!fwreq) { 4388 ret = -ENOMEM; 4389 goto fail; 4390 } 4391 4392 ret = brcmf_fw_get_firmwares(sdiodev->dev, fwreq, 4393 brcmf_sdio_firmware_callback); 4394 if (ret != 0) { 4395 brcmf_err("async firmware request failed: %d\n", ret); 4396 kfree(fwreq); 4397 goto fail; 4398 } 4399 4400 return bus; 4401 4402 fail: 4403 brcmf_sdio_remove(bus); 4404 return NULL; 4405 } 4406 4407 /* Detach and free everything */ 4408 void brcmf_sdio_remove(struct brcmf_sdio *bus) 4409 { 4410 brcmf_dbg(TRACE, "Enter\n"); 4411 4412 if (bus) { 4413 /* Stop watchdog task */ 4414 if (bus->watchdog_tsk) { 4415 send_sig(SIGTERM, bus->watchdog_tsk, 1); 4416 kthread_stop(bus->watchdog_tsk); 4417 bus->watchdog_tsk = NULL; 4418 } 4419 4420 /* De-register interrupt handler */ 4421 brcmf_sdiod_intr_unregister(bus->sdiodev); 4422 4423 brcmf_detach(bus->sdiodev->dev); 4424 4425 cancel_work_sync(&bus->datawork); 4426 if (bus->brcmf_wq) 4427 destroy_workqueue(bus->brcmf_wq); 4428 4429 if (bus->ci) { 4430 if (bus->sdiodev->state != BRCMF_SDIOD_NOMEDIUM) { 4431 sdio_claim_host(bus->sdiodev->func1); 4432 brcmf_sdio_wd_timer(bus, false); 4433 brcmf_sdio_clkctl(bus, CLK_AVAIL, false); 4434 /* Leave the device in state where it is 4435 * 'passive'. This is done by resetting all 4436 * necessary cores. 4437 */ 4438 msleep(20); 4439 brcmf_chip_set_passive(bus->ci); 4440 brcmf_sdio_clkctl(bus, CLK_NONE, false); 4441 sdio_release_host(bus->sdiodev->func1); 4442 } 4443 brcmf_chip_detach(bus->ci); 4444 } 4445 if (bus->sdiodev->settings) 4446 brcmf_release_module_param(bus->sdiodev->settings); 4447 4448 kfree(bus->rxbuf); 4449 kfree(bus->hdrbuf); 4450 kfree(bus); 4451 } 4452 4453 brcmf_dbg(TRACE, "Disconnected\n"); 4454 } 4455 4456 void brcmf_sdio_wd_timer(struct brcmf_sdio *bus, bool active) 4457 { 4458 /* Totally stop the timer */ 4459 if (!active && bus->wd_active) { 4460 del_timer_sync(&bus->timer); 4461 bus->wd_active = false; 4462 return; 4463 } 4464 4465 /* don't start the wd until fw is loaded */ 4466 if (bus->sdiodev->state != BRCMF_SDIOD_DATA) 4467 return; 4468 4469 if (active) { 4470 if (!bus->wd_active) { 4471 /* Create timer again when watchdog period is 4472 dynamically changed or in the first instance 4473 */ 4474 bus->timer.expires = jiffies + BRCMF_WD_POLL; 4475 add_timer(&bus->timer); 4476 bus->wd_active = true; 4477 } else { 4478 /* Re arm the timer, at last watchdog period */ 4479 mod_timer(&bus->timer, jiffies + BRCMF_WD_POLL); 4480 } 4481 } 4482 } 4483 4484 int brcmf_sdio_sleep(struct brcmf_sdio *bus, bool sleep) 4485 { 4486 int ret; 4487 4488 sdio_claim_host(bus->sdiodev->func1); 4489 ret = brcmf_sdio_bus_sleep(bus, sleep, false); 4490 sdio_release_host(bus->sdiodev->func1); 4491 4492 return ret; 4493 } 4494 4495