Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 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/kernel.h> |
| 19 | #include <linux/kthread.h> |
| 20 | #include <linux/printk.h> |
| 21 | #include <linux/pci_ids.h> |
| 22 | #include <linux/netdevice.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/sched.h> |
| 25 | #include <linux/mmc/sdio.h> |
| 26 | #include <linux/mmc/sdio_func.h> |
| 27 | #include <linux/mmc/card.h> |
| 28 | #include <linux/semaphore.h> |
| 29 | #include <linux/firmware.h> |
Stephen Rothwell | b7a57e7 | 2011-10-12 21:35:07 +0200 | [diff] [blame] | 30 | #include <linux/module.h> |
Franky Lin | 99ba15c | 2011-11-04 22:23:42 +0100 | [diff] [blame] | 31 | #include <linux/bcma/bcma.h> |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 32 | #include <asm/unaligned.h> |
| 33 | #include <defs.h> |
| 34 | #include <brcmu_wifi.h> |
| 35 | #include <brcmu_utils.h> |
| 36 | #include <brcm_hw_ids.h> |
| 37 | #include <soc.h> |
| 38 | #include "sdio_host.h" |
Franky Lin | a83369b | 2011-11-04 22:23:28 +0100 | [diff] [blame] | 39 | #include "sdio_chip.h" |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 40 | |
| 41 | #define DCMD_RESP_TIMEOUT 2000 /* In milli second */ |
| 42 | |
| 43 | #ifdef BCMDBG |
| 44 | |
| 45 | #define BRCMF_TRAP_INFO_SIZE 80 |
| 46 | |
| 47 | #define CBUF_LEN (128) |
| 48 | |
| 49 | struct rte_log_le { |
| 50 | __le32 buf; /* Can't be pointer on (64-bit) hosts */ |
| 51 | __le32 buf_size; |
| 52 | __le32 idx; |
| 53 | char *_buf_compat; /* Redundant pointer for backward compat. */ |
| 54 | }; |
| 55 | |
| 56 | struct rte_console { |
| 57 | /* Virtual UART |
| 58 | * When there is no UART (e.g. Quickturn), |
| 59 | * the host should write a complete |
| 60 | * input line directly into cbuf and then write |
| 61 | * the length into vcons_in. |
| 62 | * This may also be used when there is a real UART |
| 63 | * (at risk of conflicting with |
| 64 | * the real UART). vcons_out is currently unused. |
| 65 | */ |
| 66 | uint vcons_in; |
| 67 | uint vcons_out; |
| 68 | |
| 69 | /* Output (logging) buffer |
| 70 | * Console output is written to a ring buffer log_buf at index log_idx. |
| 71 | * The host may read the output when it sees log_idx advance. |
| 72 | * Output will be lost if the output wraps around faster than the host |
| 73 | * polls. |
| 74 | */ |
| 75 | struct rte_log_le log_le; |
| 76 | |
| 77 | /* Console input line buffer |
| 78 | * Characters are read one at a time into cbuf |
| 79 | * until <CR> is received, then |
| 80 | * the buffer is processed as a command line. |
| 81 | * Also used for virtual UART. |
| 82 | */ |
| 83 | uint cbuf_idx; |
| 84 | char cbuf[CBUF_LEN]; |
| 85 | }; |
| 86 | |
| 87 | #endif /* BCMDBG */ |
| 88 | #include <chipcommon.h> |
| 89 | |
| 90 | #include "dhd.h" |
| 91 | #include "dhd_bus.h" |
| 92 | #include "dhd_proto.h" |
| 93 | #include "dhd_dbg.h" |
| 94 | #include <bcmchip.h> |
| 95 | |
| 96 | #define TXQLEN 2048 /* bulk tx queue length */ |
| 97 | #define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */ |
| 98 | #define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */ |
| 99 | #define PRIOMASK 7 |
| 100 | |
| 101 | #define TXRETRIES 2 /* # of retries for tx frames */ |
| 102 | |
| 103 | #define BRCMF_RXBOUND 50 /* Default for max rx frames in |
| 104 | one scheduling */ |
| 105 | |
| 106 | #define BRCMF_TXBOUND 20 /* Default for max tx frames in |
| 107 | one scheduling */ |
| 108 | |
| 109 | #define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */ |
| 110 | |
| 111 | #define MEMBLOCK 2048 /* Block size used for downloading |
| 112 | of dongle image */ |
| 113 | #define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold |
| 114 | biggest possible glom */ |
| 115 | |
| 116 | #define BRCMF_FIRSTREAD (1 << 6) |
| 117 | |
| 118 | |
| 119 | /* SBSDIO_DEVICE_CTL */ |
| 120 | |
| 121 | /* 1: device will assert busy signal when receiving CMD53 */ |
| 122 | #define SBSDIO_DEVCTL_SETBUSY 0x01 |
| 123 | /* 1: assertion of sdio interrupt is synchronous to the sdio clock */ |
| 124 | #define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02 |
| 125 | /* 1: mask all interrupts to host except the chipActive (rev 8) */ |
| 126 | #define SBSDIO_DEVCTL_CA_INT_ONLY 0x04 |
| 127 | /* 1: isolate internal sdio signals, put external pads in tri-state; requires |
| 128 | * sdio bus power cycle to clear (rev 9) */ |
| 129 | #define SBSDIO_DEVCTL_PADS_ISO 0x08 |
| 130 | /* Force SD->SB reset mapping (rev 11) */ |
| 131 | #define SBSDIO_DEVCTL_SB_RST_CTL 0x30 |
| 132 | /* Determined by CoreControl bit */ |
| 133 | #define SBSDIO_DEVCTL_RST_CORECTL 0x00 |
| 134 | /* Force backplane reset */ |
| 135 | #define SBSDIO_DEVCTL_RST_BPRESET 0x10 |
| 136 | /* Force no backplane reset */ |
| 137 | #define SBSDIO_DEVCTL_RST_NOBPRESET 0x20 |
| 138 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 139 | /* direct(mapped) cis space */ |
| 140 | |
| 141 | /* MAPPED common CIS address */ |
| 142 | #define SBSDIO_CIS_BASE_COMMON 0x1000 |
| 143 | /* maximum bytes in one CIS */ |
| 144 | #define SBSDIO_CIS_SIZE_LIMIT 0x200 |
| 145 | /* cis offset addr is < 17 bits */ |
| 146 | #define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF |
| 147 | |
| 148 | /* manfid tuple length, include tuple, link bytes */ |
| 149 | #define SBSDIO_CIS_MANFID_TUPLE_LEN 6 |
| 150 | |
| 151 | /* intstatus */ |
| 152 | #define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */ |
| 153 | #define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */ |
| 154 | #define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */ |
| 155 | #define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */ |
| 156 | #define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */ |
| 157 | #define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */ |
| 158 | #define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */ |
| 159 | #define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */ |
| 160 | #define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */ |
| 161 | #define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */ |
| 162 | #define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */ |
| 163 | #define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */ |
| 164 | #define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */ |
| 165 | #define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */ |
| 166 | #define I_PC (1 << 10) /* descriptor error */ |
| 167 | #define I_PD (1 << 11) /* data error */ |
| 168 | #define I_DE (1 << 12) /* Descriptor protocol Error */ |
| 169 | #define I_RU (1 << 13) /* Receive descriptor Underflow */ |
| 170 | #define I_RO (1 << 14) /* Receive fifo Overflow */ |
| 171 | #define I_XU (1 << 15) /* Transmit fifo Underflow */ |
| 172 | #define I_RI (1 << 16) /* Receive Interrupt */ |
| 173 | #define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */ |
| 174 | #define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */ |
| 175 | #define I_XI (1 << 24) /* Transmit Interrupt */ |
| 176 | #define I_RF_TERM (1 << 25) /* Read Frame Terminate */ |
| 177 | #define I_WF_TERM (1 << 26) /* Write Frame Terminate */ |
| 178 | #define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */ |
| 179 | #define I_SBINT (1 << 28) /* sbintstatus Interrupt */ |
| 180 | #define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */ |
| 181 | #define I_SRESET (1 << 30) /* CCCR RES interrupt */ |
| 182 | #define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */ |
| 183 | #define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU) |
| 184 | #define I_DMA (I_RI | I_XI | I_ERRORS) |
| 185 | |
| 186 | /* corecontrol */ |
| 187 | #define CC_CISRDY (1 << 0) /* CIS Ready */ |
| 188 | #define CC_BPRESEN (1 << 1) /* CCCR RES signal */ |
| 189 | #define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */ |
| 190 | #define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */ |
| 191 | #define CC_XMTDATAAVAIL_MODE (1 << 4) |
| 192 | #define CC_XMTDATAAVAIL_CTRL (1 << 5) |
| 193 | |
| 194 | /* SDA_FRAMECTRL */ |
| 195 | #define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */ |
| 196 | #define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */ |
| 197 | #define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */ |
| 198 | #define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */ |
| 199 | |
| 200 | /* HW frame tag */ |
| 201 | #define SDPCM_FRAMETAG_LEN 4 /* 2 bytes len, 2 bytes check val */ |
| 202 | |
| 203 | /* Total length of frame header for dongle protocol */ |
| 204 | #define SDPCM_HDRLEN (SDPCM_FRAMETAG_LEN + SDPCM_SWHEADER_LEN) |
| 205 | #define SDPCM_RESERVE (SDPCM_HDRLEN + BRCMF_SDALIGN) |
| 206 | |
| 207 | /* |
| 208 | * Software allocation of To SB Mailbox resources |
| 209 | */ |
| 210 | |
| 211 | /* tosbmailbox bits corresponding to intstatus bits */ |
| 212 | #define SMB_NAK (1 << 0) /* Frame NAK */ |
| 213 | #define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */ |
| 214 | #define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */ |
| 215 | #define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */ |
| 216 | |
| 217 | /* tosbmailboxdata */ |
| 218 | #define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */ |
| 219 | |
| 220 | /* |
| 221 | * Software allocation of To Host Mailbox resources |
| 222 | */ |
| 223 | |
| 224 | /* intstatus bits */ |
| 225 | #define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */ |
| 226 | #define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */ |
| 227 | #define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */ |
| 228 | #define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */ |
| 229 | |
| 230 | /* tohostmailboxdata */ |
| 231 | #define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */ |
| 232 | #define HMB_DATA_DEVREADY 2 /* talk to host after enable */ |
| 233 | #define HMB_DATA_FC 4 /* per prio flowcontrol update flag */ |
| 234 | #define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */ |
| 235 | |
| 236 | #define HMB_DATA_FCDATA_MASK 0xff000000 |
| 237 | #define HMB_DATA_FCDATA_SHIFT 24 |
| 238 | |
| 239 | #define HMB_DATA_VERSION_MASK 0x00ff0000 |
| 240 | #define HMB_DATA_VERSION_SHIFT 16 |
| 241 | |
| 242 | /* |
| 243 | * Software-defined protocol header |
| 244 | */ |
| 245 | |
| 246 | /* Current protocol version */ |
| 247 | #define SDPCM_PROT_VERSION 4 |
| 248 | |
| 249 | /* SW frame header */ |
| 250 | #define SDPCM_PACKET_SEQUENCE(p) (((u8 *)p)[0] & 0xff) |
| 251 | |
| 252 | #define SDPCM_CHANNEL_MASK 0x00000f00 |
| 253 | #define SDPCM_CHANNEL_SHIFT 8 |
| 254 | #define SDPCM_PACKET_CHANNEL(p) (((u8 *)p)[1] & 0x0f) |
| 255 | |
| 256 | #define SDPCM_NEXTLEN_OFFSET 2 |
| 257 | |
| 258 | /* Data Offset from SOF (HW Tag, SW Tag, Pad) */ |
| 259 | #define SDPCM_DOFFSET_OFFSET 3 /* Data Offset */ |
| 260 | #define SDPCM_DOFFSET_VALUE(p) (((u8 *)p)[SDPCM_DOFFSET_OFFSET] & 0xff) |
| 261 | #define SDPCM_DOFFSET_MASK 0xff000000 |
| 262 | #define SDPCM_DOFFSET_SHIFT 24 |
| 263 | #define SDPCM_FCMASK_OFFSET 4 /* Flow control */ |
| 264 | #define SDPCM_FCMASK_VALUE(p) (((u8 *)p)[SDPCM_FCMASK_OFFSET] & 0xff) |
| 265 | #define SDPCM_WINDOW_OFFSET 5 /* Credit based fc */ |
| 266 | #define SDPCM_WINDOW_VALUE(p) (((u8 *)p)[SDPCM_WINDOW_OFFSET] & 0xff) |
| 267 | |
| 268 | #define SDPCM_SWHEADER_LEN 8 /* SW header is 64 bits */ |
| 269 | |
| 270 | /* logical channel numbers */ |
| 271 | #define SDPCM_CONTROL_CHANNEL 0 /* Control channel Id */ |
| 272 | #define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication Channel Id */ |
| 273 | #define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv Channel Id */ |
| 274 | #define SDPCM_GLOM_CHANNEL 3 /* For coalesced packets */ |
| 275 | #define SDPCM_TEST_CHANNEL 15 /* Reserved for test/debug packets */ |
| 276 | |
| 277 | #define SDPCM_SEQUENCE_WRAP 256 /* wrap-around val for 8bit frame seq */ |
| 278 | |
| 279 | #define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80) |
| 280 | |
| 281 | /* |
| 282 | * Shared structure between dongle and the host. |
| 283 | * The structure contains pointers to trap or assert information. |
| 284 | */ |
| 285 | #define SDPCM_SHARED_VERSION 0x0002 |
| 286 | #define SDPCM_SHARED_VERSION_MASK 0x00FF |
| 287 | #define SDPCM_SHARED_ASSERT_BUILT 0x0100 |
| 288 | #define SDPCM_SHARED_ASSERT 0x0200 |
| 289 | #define SDPCM_SHARED_TRAP 0x0400 |
| 290 | |
| 291 | /* Space for header read, limit for data packets */ |
| 292 | #define MAX_HDR_READ (1 << 6) |
| 293 | #define MAX_RX_DATASZ 2048 |
| 294 | |
| 295 | /* Maximum milliseconds to wait for F2 to come up */ |
| 296 | #define BRCMF_WAIT_F2RDY 3000 |
| 297 | |
| 298 | /* Bump up limit on waiting for HT to account for first startup; |
| 299 | * if the image is doing a CRC calculation before programming the PMU |
| 300 | * for HT availability, it could take a couple hundred ms more, so |
| 301 | * max out at a 1 second (1000000us). |
| 302 | */ |
| 303 | #undef PMU_MAX_TRANSITION_DLY |
| 304 | #define PMU_MAX_TRANSITION_DLY 1000000 |
| 305 | |
| 306 | /* Value for ChipClockCSR during initial setup */ |
| 307 | #define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \ |
| 308 | SBSDIO_ALP_AVAIL_REQ) |
| 309 | |
| 310 | /* Flags for SDH calls */ |
| 311 | #define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED) |
| 312 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 313 | /* |
| 314 | * Conversion of 802.1D priority to precedence level |
| 315 | */ |
| 316 | static uint prio2prec(u32 prio) |
| 317 | { |
| 318 | return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ? |
| 319 | (prio^2) : prio; |
| 320 | } |
| 321 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 322 | /* core registers */ |
| 323 | struct sdpcmd_regs { |
| 324 | u32 corecontrol; /* 0x00, rev8 */ |
| 325 | u32 corestatus; /* rev8 */ |
| 326 | u32 PAD[1]; |
| 327 | u32 biststatus; /* rev8 */ |
| 328 | |
| 329 | /* PCMCIA access */ |
| 330 | u16 pcmciamesportaladdr; /* 0x010, rev8 */ |
| 331 | u16 PAD[1]; |
| 332 | u16 pcmciamesportalmask; /* rev8 */ |
| 333 | u16 PAD[1]; |
| 334 | u16 pcmciawrframebc; /* rev8 */ |
| 335 | u16 PAD[1]; |
| 336 | u16 pcmciaunderflowtimer; /* rev8 */ |
| 337 | u16 PAD[1]; |
| 338 | |
| 339 | /* interrupt */ |
| 340 | u32 intstatus; /* 0x020, rev8 */ |
| 341 | u32 hostintmask; /* rev8 */ |
| 342 | u32 intmask; /* rev8 */ |
| 343 | u32 sbintstatus; /* rev8 */ |
| 344 | u32 sbintmask; /* rev8 */ |
| 345 | u32 funcintmask; /* rev4 */ |
| 346 | u32 PAD[2]; |
| 347 | u32 tosbmailbox; /* 0x040, rev8 */ |
| 348 | u32 tohostmailbox; /* rev8 */ |
| 349 | u32 tosbmailboxdata; /* rev8 */ |
| 350 | u32 tohostmailboxdata; /* rev8 */ |
| 351 | |
| 352 | /* synchronized access to registers in SDIO clock domain */ |
| 353 | u32 sdioaccess; /* 0x050, rev8 */ |
| 354 | u32 PAD[3]; |
| 355 | |
| 356 | /* PCMCIA frame control */ |
| 357 | u8 pcmciaframectrl; /* 0x060, rev8 */ |
| 358 | u8 PAD[3]; |
| 359 | u8 pcmciawatermark; /* rev8 */ |
| 360 | u8 PAD[155]; |
| 361 | |
| 362 | /* interrupt batching control */ |
| 363 | u32 intrcvlazy; /* 0x100, rev8 */ |
| 364 | u32 PAD[3]; |
| 365 | |
| 366 | /* counters */ |
| 367 | u32 cmd52rd; /* 0x110, rev8 */ |
| 368 | u32 cmd52wr; /* rev8 */ |
| 369 | u32 cmd53rd; /* rev8 */ |
| 370 | u32 cmd53wr; /* rev8 */ |
| 371 | u32 abort; /* rev8 */ |
| 372 | u32 datacrcerror; /* rev8 */ |
| 373 | u32 rdoutofsync; /* rev8 */ |
| 374 | u32 wroutofsync; /* rev8 */ |
| 375 | u32 writebusy; /* rev8 */ |
| 376 | u32 readwait; /* rev8 */ |
| 377 | u32 readterm; /* rev8 */ |
| 378 | u32 writeterm; /* rev8 */ |
| 379 | u32 PAD[40]; |
| 380 | u32 clockctlstatus; /* rev8 */ |
| 381 | u32 PAD[7]; |
| 382 | |
| 383 | u32 PAD[128]; /* DMA engines */ |
| 384 | |
| 385 | /* SDIO/PCMCIA CIS region */ |
| 386 | char cis[512]; /* 0x400-0x5ff, rev6 */ |
| 387 | |
| 388 | /* PCMCIA function control registers */ |
| 389 | char pcmciafcr[256]; /* 0x600-6ff, rev6 */ |
| 390 | u16 PAD[55]; |
| 391 | |
| 392 | /* PCMCIA backplane access */ |
| 393 | u16 backplanecsr; /* 0x76E, rev6 */ |
| 394 | u16 backplaneaddr0; /* rev6 */ |
| 395 | u16 backplaneaddr1; /* rev6 */ |
| 396 | u16 backplaneaddr2; /* rev6 */ |
| 397 | u16 backplaneaddr3; /* rev6 */ |
| 398 | u16 backplanedata0; /* rev6 */ |
| 399 | u16 backplanedata1; /* rev6 */ |
| 400 | u16 backplanedata2; /* rev6 */ |
| 401 | u16 backplanedata3; /* rev6 */ |
| 402 | u16 PAD[31]; |
| 403 | |
| 404 | /* sprom "size" & "blank" info */ |
| 405 | u16 spromstatus; /* 0x7BE, rev2 */ |
| 406 | u32 PAD[464]; |
| 407 | |
| 408 | u16 PAD[0x80]; |
| 409 | }; |
| 410 | |
| 411 | #ifdef BCMDBG |
| 412 | /* Device console log buffer state */ |
| 413 | struct brcmf_console { |
| 414 | uint count; /* Poll interval msec counter */ |
| 415 | uint log_addr; /* Log struct address (fixed) */ |
| 416 | struct rte_log_le log_le; /* Log struct (host copy) */ |
| 417 | uint bufsize; /* Size of log buffer */ |
| 418 | u8 *buf; /* Log buffer (host copy) */ |
| 419 | uint last; /* Last buffer read index */ |
| 420 | }; |
| 421 | #endif /* BCMDBG */ |
| 422 | |
| 423 | struct sdpcm_shared { |
| 424 | u32 flags; |
| 425 | u32 trap_addr; |
| 426 | u32 assert_exp_addr; |
| 427 | u32 assert_file_addr; |
| 428 | u32 assert_line; |
| 429 | u32 console_addr; /* Address of struct rte_console */ |
| 430 | u32 msgtrace_addr; |
| 431 | u8 tag[32]; |
| 432 | }; |
| 433 | |
| 434 | struct sdpcm_shared_le { |
| 435 | __le32 flags; |
| 436 | __le32 trap_addr; |
| 437 | __le32 assert_exp_addr; |
| 438 | __le32 assert_file_addr; |
| 439 | __le32 assert_line; |
| 440 | __le32 console_addr; /* Address of struct rte_console */ |
| 441 | __le32 msgtrace_addr; |
| 442 | u8 tag[32]; |
| 443 | }; |
| 444 | |
| 445 | |
| 446 | /* misc chip info needed by some of the routines */ |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 447 | /* Private data for SDIO bus interaction */ |
| 448 | struct brcmf_bus { |
| 449 | struct brcmf_pub *drvr; |
| 450 | |
| 451 | struct brcmf_sdio_dev *sdiodev; /* sdio device handler */ |
| 452 | struct chip_info *ci; /* Chip info struct */ |
| 453 | char *vars; /* Variables (from CIS and/or other) */ |
| 454 | uint varsz; /* Size of variables buffer */ |
| 455 | |
| 456 | u32 ramsize; /* Size of RAM in SOCRAM (bytes) */ |
| 457 | |
| 458 | u32 hostintmask; /* Copy of Host Interrupt Mask */ |
| 459 | u32 intstatus; /* Intstatus bits (events) pending */ |
| 460 | bool dpc_sched; /* Indicates DPC schedule (intrpt rcvd) */ |
| 461 | bool fcstate; /* State of dongle flow-control */ |
| 462 | |
| 463 | uint blocksize; /* Block size of SDIO transfers */ |
| 464 | uint roundup; /* Max roundup limit */ |
| 465 | |
| 466 | struct pktq txq; /* Queue length used for flow-control */ |
| 467 | u8 flowcontrol; /* per prio flow control bitmask */ |
| 468 | u8 tx_seq; /* Transmit sequence number (next) */ |
| 469 | u8 tx_max; /* Maximum transmit sequence allowed */ |
| 470 | |
| 471 | u8 hdrbuf[MAX_HDR_READ + BRCMF_SDALIGN]; |
| 472 | u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */ |
| 473 | u16 nextlen; /* Next Read Len from last header */ |
| 474 | u8 rx_seq; /* Receive sequence number (expected) */ |
| 475 | bool rxskip; /* Skip receive (awaiting NAK ACK) */ |
| 476 | |
| 477 | uint rxbound; /* Rx frames to read before resched */ |
| 478 | uint txbound; /* Tx frames to send before resched */ |
| 479 | uint txminmax; |
| 480 | |
| 481 | struct sk_buff *glomd; /* Packet containing glomming descriptor */ |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 482 | struct sk_buff_head glom; /* Packet list for glommed superframe */ |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 483 | uint glomerr; /* Glom packet read errors */ |
| 484 | |
| 485 | u8 *rxbuf; /* Buffer for receiving control packets */ |
| 486 | uint rxblen; /* Allocated length of rxbuf */ |
| 487 | u8 *rxctl; /* Aligned pointer into rxbuf */ |
| 488 | u8 *databuf; /* Buffer for receiving big glom packet */ |
| 489 | u8 *dataptr; /* Aligned pointer into databuf */ |
| 490 | uint rxlen; /* Length of valid data in buffer */ |
| 491 | |
| 492 | u8 sdpcm_ver; /* Bus protocol reported by dongle */ |
| 493 | |
| 494 | bool intr; /* Use interrupts */ |
| 495 | bool poll; /* Use polling */ |
| 496 | bool ipend; /* Device interrupt is pending */ |
| 497 | uint intrcount; /* Count of device interrupt callbacks */ |
| 498 | uint lastintrs; /* Count as of last watchdog timer */ |
| 499 | uint spurious; /* Count of spurious interrupts */ |
| 500 | uint pollrate; /* Ticks between device polls */ |
| 501 | uint polltick; /* Tick counter */ |
| 502 | uint pollcnt; /* Count of active polls */ |
| 503 | |
| 504 | #ifdef BCMDBG |
| 505 | uint console_interval; |
| 506 | struct brcmf_console console; /* Console output polling support */ |
| 507 | uint console_addr; /* Console address from shared struct */ |
| 508 | #endif /* BCMDBG */ |
| 509 | |
| 510 | uint regfails; /* Count of R_REG failures */ |
| 511 | |
| 512 | uint clkstate; /* State of sd and backplane clock(s) */ |
| 513 | bool activity; /* Activity flag for clock down */ |
| 514 | s32 idletime; /* Control for activity timeout */ |
| 515 | s32 idlecount; /* Activity timeout counter */ |
| 516 | s32 idleclock; /* How to set bus driver when idle */ |
| 517 | s32 sd_rxchain; |
| 518 | bool use_rxchain; /* If brcmf should use PKT chains */ |
| 519 | bool sleeping; /* Is SDIO bus sleeping? */ |
| 520 | bool rxflow_mode; /* Rx flow control mode */ |
| 521 | bool rxflow; /* Is rx flow control on */ |
| 522 | bool alp_only; /* Don't use HT clock (ALP only) */ |
| 523 | /* Field to decide if rx of control frames happen in rxbuf or lb-pool */ |
| 524 | bool usebufpool; |
| 525 | |
| 526 | /* Some additional counters */ |
| 527 | uint tx_sderrs; /* Count of tx attempts with sd errors */ |
| 528 | uint fcqueued; /* Tx packets that got queued */ |
| 529 | uint rxrtx; /* Count of rtx requests (NAK to dongle) */ |
| 530 | uint rx_toolong; /* Receive frames too long to receive */ |
| 531 | uint rxc_errors; /* SDIO errors when reading control frames */ |
| 532 | uint rx_hdrfail; /* SDIO errors on header reads */ |
| 533 | uint rx_badhdr; /* Bad received headers (roosync?) */ |
| 534 | uint rx_badseq; /* Mismatched rx sequence number */ |
| 535 | uint fc_rcvd; /* Number of flow-control events received */ |
| 536 | uint fc_xoff; /* Number which turned on flow-control */ |
| 537 | uint fc_xon; /* Number which turned off flow-control */ |
| 538 | uint rxglomfail; /* Failed deglom attempts */ |
| 539 | uint rxglomframes; /* Number of glom frames (superframes) */ |
| 540 | uint rxglompkts; /* Number of packets from glom frames */ |
| 541 | uint f2rxhdrs; /* Number of header reads */ |
| 542 | uint f2rxdata; /* Number of frame data reads */ |
| 543 | uint f2txdata; /* Number of f2 frame writes */ |
| 544 | uint f1regdata; /* Number of f1 register accesses */ |
| 545 | |
| 546 | u8 *ctrl_frame_buf; |
| 547 | u32 ctrl_frame_len; |
| 548 | bool ctrl_frame_stat; |
| 549 | |
| 550 | spinlock_t txqlock; |
| 551 | wait_queue_head_t ctrl_wait; |
| 552 | wait_queue_head_t dcmd_resp_wait; |
| 553 | |
| 554 | struct timer_list timer; |
| 555 | struct completion watchdog_wait; |
| 556 | struct task_struct *watchdog_tsk; |
| 557 | bool wd_timer_valid; |
| 558 | uint save_ms; |
| 559 | |
| 560 | struct task_struct *dpc_tsk; |
| 561 | struct completion dpc_wait; |
| 562 | |
| 563 | struct semaphore sdsem; |
| 564 | |
| 565 | const char *fw_name; |
| 566 | const struct firmware *firmware; |
| 567 | const char *nv_name; |
| 568 | u32 fw_ptr; |
| 569 | }; |
| 570 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 571 | /* clkstate */ |
| 572 | #define CLK_NONE 0 |
| 573 | #define CLK_SDONLY 1 |
| 574 | #define CLK_PENDING 2 /* Not used yet */ |
| 575 | #define CLK_AVAIL 3 |
| 576 | |
| 577 | #ifdef BCMDBG |
| 578 | static int qcount[NUMPRIO]; |
| 579 | static int tx_packets[NUMPRIO]; |
| 580 | #endif /* BCMDBG */ |
| 581 | |
| 582 | #define SDIO_DRIVE_STRENGTH 6 /* in milliamps */ |
| 583 | |
| 584 | #define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL) |
| 585 | |
| 586 | /* Retry count for register access failures */ |
| 587 | static const uint retry_limit = 2; |
| 588 | |
| 589 | /* Limit on rounding up frames */ |
| 590 | static const uint max_roundup = 512; |
| 591 | |
| 592 | #define ALIGNMENT 4 |
| 593 | |
| 594 | static void pkt_align(struct sk_buff *p, int len, int align) |
| 595 | { |
| 596 | uint datalign; |
| 597 | datalign = (unsigned long)(p->data); |
| 598 | datalign = roundup(datalign, (align)) - datalign; |
| 599 | if (datalign) |
| 600 | skb_pull(p, datalign); |
| 601 | __skb_trim(p, len); |
| 602 | } |
| 603 | |
| 604 | /* To check if there's window offered */ |
| 605 | static bool data_ok(struct brcmf_bus *bus) |
| 606 | { |
| 607 | return (u8)(bus->tx_max - bus->tx_seq) != 0 && |
| 608 | ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0; |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | * Reads a register in the SDIO hardware block. This block occupies a series of |
| 613 | * adresses on the 32 bit backplane bus. |
| 614 | */ |
| 615 | static void |
| 616 | r_sdreg32(struct brcmf_bus *bus, u32 *regvar, u32 reg_offset, u32 *retryvar) |
| 617 | { |
Franky Lin | 99ba15c | 2011-11-04 22:23:42 +0100 | [diff] [blame] | 618 | u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 619 | *retryvar = 0; |
| 620 | do { |
| 621 | *regvar = brcmf_sdcard_reg_read(bus->sdiodev, |
Franky Lin | 99ba15c | 2011-11-04 22:23:42 +0100 | [diff] [blame] | 622 | bus->ci->c_inf[idx].base + reg_offset, |
| 623 | sizeof(u32)); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 624 | } while (brcmf_sdcard_regfail(bus->sdiodev) && |
| 625 | (++(*retryvar) <= retry_limit)); |
| 626 | if (*retryvar) { |
| 627 | bus->regfails += (*retryvar-1); |
| 628 | if (*retryvar > retry_limit) { |
| 629 | brcmf_dbg(ERROR, "FAILED READ %Xh\n", reg_offset); |
| 630 | *regvar = 0; |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | static void |
| 636 | w_sdreg32(struct brcmf_bus *bus, u32 regval, u32 reg_offset, u32 *retryvar) |
| 637 | { |
Franky Lin | 99ba15c | 2011-11-04 22:23:42 +0100 | [diff] [blame] | 638 | u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 639 | *retryvar = 0; |
| 640 | do { |
| 641 | brcmf_sdcard_reg_write(bus->sdiodev, |
Franky Lin | 99ba15c | 2011-11-04 22:23:42 +0100 | [diff] [blame] | 642 | bus->ci->c_inf[idx].base + reg_offset, |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 643 | sizeof(u32), regval); |
| 644 | } while (brcmf_sdcard_regfail(bus->sdiodev) && |
| 645 | (++(*retryvar) <= retry_limit)); |
| 646 | if (*retryvar) { |
| 647 | bus->regfails += (*retryvar-1); |
| 648 | if (*retryvar > retry_limit) |
| 649 | brcmf_dbg(ERROR, "FAILED REGISTER WRITE %Xh\n", |
| 650 | reg_offset); |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | #define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND) |
| 655 | |
| 656 | #define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE) |
| 657 | |
| 658 | /* Packet free applicable unconditionally for sdio and sdspi. |
| 659 | * Conditional if bufpool was present for gspi bus. |
| 660 | */ |
| 661 | static void brcmf_sdbrcm_pktfree2(struct brcmf_bus *bus, struct sk_buff *pkt) |
| 662 | { |
| 663 | if (bus->usebufpool) |
| 664 | brcmu_pkt_buf_free_skb(pkt); |
| 665 | } |
| 666 | |
| 667 | /* Turn backplane clock on or off */ |
| 668 | static int brcmf_sdbrcm_htclk(struct brcmf_bus *bus, bool on, bool pendok) |
| 669 | { |
| 670 | int err; |
| 671 | u8 clkctl, clkreq, devctl; |
| 672 | unsigned long timeout; |
| 673 | |
| 674 | brcmf_dbg(TRACE, "Enter\n"); |
| 675 | |
| 676 | clkctl = 0; |
| 677 | |
| 678 | if (on) { |
| 679 | /* Request HT Avail */ |
| 680 | clkreq = |
| 681 | bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ; |
| 682 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 683 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 684 | SBSDIO_FUNC1_CHIPCLKCSR, clkreq, &err); |
| 685 | if (err) { |
| 686 | brcmf_dbg(ERROR, "HT Avail request error: %d\n", err); |
| 687 | return -EBADE; |
| 688 | } |
| 689 | |
Franky Lin | 99ba15c | 2011-11-04 22:23:42 +0100 | [diff] [blame] | 690 | if (pendok && ((bus->ci->c_inf[1].id == PCMCIA_CORE_ID) |
| 691 | && (bus->ci->c_inf[1].rev == 9))) { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 692 | u32 dummy, retries; |
| 693 | r_sdreg32(bus, &dummy, |
| 694 | offsetof(struct sdpcmd_regs, clockctlstatus), |
| 695 | &retries); |
| 696 | } |
| 697 | |
| 698 | /* Check current status */ |
| 699 | clkctl = brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 700 | SBSDIO_FUNC1_CHIPCLKCSR, &err); |
| 701 | if (err) { |
| 702 | brcmf_dbg(ERROR, "HT Avail read error: %d\n", err); |
| 703 | return -EBADE; |
| 704 | } |
| 705 | |
| 706 | /* Go to pending and await interrupt if appropriate */ |
| 707 | if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) { |
| 708 | /* Allow only clock-available interrupt */ |
| 709 | devctl = brcmf_sdcard_cfg_read(bus->sdiodev, |
| 710 | SDIO_FUNC_1, |
| 711 | SBSDIO_DEVICE_CTL, &err); |
| 712 | if (err) { |
| 713 | brcmf_dbg(ERROR, "Devctl error setting CA: %d\n", |
| 714 | err); |
| 715 | return -EBADE; |
| 716 | } |
| 717 | |
| 718 | devctl |= SBSDIO_DEVCTL_CA_INT_ONLY; |
| 719 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 720 | SBSDIO_DEVICE_CTL, devctl, &err); |
| 721 | brcmf_dbg(INFO, "CLKCTL: set PENDING\n"); |
| 722 | bus->clkstate = CLK_PENDING; |
| 723 | |
| 724 | return 0; |
| 725 | } else if (bus->clkstate == CLK_PENDING) { |
| 726 | /* Cancel CA-only interrupt filter */ |
| 727 | devctl = |
| 728 | brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 729 | SBSDIO_DEVICE_CTL, &err); |
| 730 | devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY; |
| 731 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 732 | SBSDIO_DEVICE_CTL, devctl, &err); |
| 733 | } |
| 734 | |
| 735 | /* Otherwise, wait here (polling) for HT Avail */ |
| 736 | timeout = jiffies + |
| 737 | msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000); |
| 738 | while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) { |
| 739 | clkctl = brcmf_sdcard_cfg_read(bus->sdiodev, |
| 740 | SDIO_FUNC_1, |
| 741 | SBSDIO_FUNC1_CHIPCLKCSR, |
| 742 | &err); |
| 743 | if (time_after(jiffies, timeout)) |
| 744 | break; |
| 745 | else |
| 746 | usleep_range(5000, 10000); |
| 747 | } |
| 748 | if (err) { |
| 749 | brcmf_dbg(ERROR, "HT Avail request error: %d\n", err); |
| 750 | return -EBADE; |
| 751 | } |
| 752 | if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) { |
| 753 | brcmf_dbg(ERROR, "HT Avail timeout (%d): clkctl 0x%02x\n", |
| 754 | PMU_MAX_TRANSITION_DLY, clkctl); |
| 755 | return -EBADE; |
| 756 | } |
| 757 | |
| 758 | /* Mark clock available */ |
| 759 | bus->clkstate = CLK_AVAIL; |
| 760 | brcmf_dbg(INFO, "CLKCTL: turned ON\n"); |
| 761 | |
| 762 | #if defined(BCMDBG) |
| 763 | if (bus->alp_only != true) { |
| 764 | if (SBSDIO_ALPONLY(clkctl)) |
| 765 | brcmf_dbg(ERROR, "HT Clock should be on\n"); |
| 766 | } |
| 767 | #endif /* defined (BCMDBG) */ |
| 768 | |
| 769 | bus->activity = true; |
| 770 | } else { |
| 771 | clkreq = 0; |
| 772 | |
| 773 | if (bus->clkstate == CLK_PENDING) { |
| 774 | /* Cancel CA-only interrupt filter */ |
| 775 | devctl = brcmf_sdcard_cfg_read(bus->sdiodev, |
| 776 | SDIO_FUNC_1, |
| 777 | SBSDIO_DEVICE_CTL, &err); |
| 778 | devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY; |
| 779 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 780 | SBSDIO_DEVICE_CTL, devctl, &err); |
| 781 | } |
| 782 | |
| 783 | bus->clkstate = CLK_SDONLY; |
| 784 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 785 | SBSDIO_FUNC1_CHIPCLKCSR, clkreq, &err); |
| 786 | brcmf_dbg(INFO, "CLKCTL: turned OFF\n"); |
| 787 | if (err) { |
| 788 | brcmf_dbg(ERROR, "Failed access turning clock off: %d\n", |
| 789 | err); |
| 790 | return -EBADE; |
| 791 | } |
| 792 | } |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | /* Change idle/active SD state */ |
| 797 | static int brcmf_sdbrcm_sdclk(struct brcmf_bus *bus, bool on) |
| 798 | { |
| 799 | brcmf_dbg(TRACE, "Enter\n"); |
| 800 | |
| 801 | if (on) |
| 802 | bus->clkstate = CLK_SDONLY; |
| 803 | else |
| 804 | bus->clkstate = CLK_NONE; |
| 805 | |
| 806 | return 0; |
| 807 | } |
| 808 | |
| 809 | /* Transition SD and backplane clock readiness */ |
| 810 | static int brcmf_sdbrcm_clkctl(struct brcmf_bus *bus, uint target, bool pendok) |
| 811 | { |
| 812 | #ifdef BCMDBG |
| 813 | uint oldstate = bus->clkstate; |
| 814 | #endif /* BCMDBG */ |
| 815 | |
| 816 | brcmf_dbg(TRACE, "Enter\n"); |
| 817 | |
| 818 | /* Early exit if we're already there */ |
| 819 | if (bus->clkstate == target) { |
| 820 | if (target == CLK_AVAIL) { |
| 821 | brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS); |
| 822 | bus->activity = true; |
| 823 | } |
| 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | switch (target) { |
| 828 | case CLK_AVAIL: |
| 829 | /* Make sure SD clock is available */ |
| 830 | if (bus->clkstate == CLK_NONE) |
| 831 | brcmf_sdbrcm_sdclk(bus, true); |
| 832 | /* Now request HT Avail on the backplane */ |
| 833 | brcmf_sdbrcm_htclk(bus, true, pendok); |
| 834 | brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS); |
| 835 | bus->activity = true; |
| 836 | break; |
| 837 | |
| 838 | case CLK_SDONLY: |
| 839 | /* Remove HT request, or bring up SD clock */ |
| 840 | if (bus->clkstate == CLK_NONE) |
| 841 | brcmf_sdbrcm_sdclk(bus, true); |
| 842 | else if (bus->clkstate == CLK_AVAIL) |
| 843 | brcmf_sdbrcm_htclk(bus, false, false); |
| 844 | else |
| 845 | brcmf_dbg(ERROR, "request for %d -> %d\n", |
| 846 | bus->clkstate, target); |
| 847 | brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS); |
| 848 | break; |
| 849 | |
| 850 | case CLK_NONE: |
| 851 | /* Make sure to remove HT request */ |
| 852 | if (bus->clkstate == CLK_AVAIL) |
| 853 | brcmf_sdbrcm_htclk(bus, false, false); |
| 854 | /* Now remove the SD clock */ |
| 855 | brcmf_sdbrcm_sdclk(bus, false); |
| 856 | brcmf_sdbrcm_wd_timer(bus, 0); |
| 857 | break; |
| 858 | } |
| 859 | #ifdef BCMDBG |
| 860 | brcmf_dbg(INFO, "%d -> %d\n", oldstate, bus->clkstate); |
| 861 | #endif /* BCMDBG */ |
| 862 | |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | static int brcmf_sdbrcm_bussleep(struct brcmf_bus *bus, bool sleep) |
| 867 | { |
| 868 | uint retries = 0; |
| 869 | |
| 870 | brcmf_dbg(INFO, "request %s (currently %s)\n", |
| 871 | sleep ? "SLEEP" : "WAKE", |
| 872 | bus->sleeping ? "SLEEP" : "WAKE"); |
| 873 | |
| 874 | /* Done if we're already in the requested state */ |
| 875 | if (sleep == bus->sleeping) |
| 876 | return 0; |
| 877 | |
| 878 | /* Going to sleep: set the alarm and turn off the lights... */ |
| 879 | if (sleep) { |
| 880 | /* Don't sleep if something is pending */ |
| 881 | if (bus->dpc_sched || bus->rxskip || pktq_len(&bus->txq)) |
| 882 | return -EBUSY; |
| 883 | |
| 884 | /* Make sure the controller has the bus up */ |
| 885 | brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false); |
| 886 | |
| 887 | /* Tell device to start using OOB wakeup */ |
| 888 | w_sdreg32(bus, SMB_USE_OOB, |
| 889 | offsetof(struct sdpcmd_regs, tosbmailbox), &retries); |
| 890 | if (retries > retry_limit) |
| 891 | brcmf_dbg(ERROR, "CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"); |
| 892 | |
| 893 | /* Turn off our contribution to the HT clock request */ |
| 894 | brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false); |
| 895 | |
| 896 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 897 | SBSDIO_FUNC1_CHIPCLKCSR, |
| 898 | SBSDIO_FORCE_HW_CLKREQ_OFF, NULL); |
| 899 | |
| 900 | /* Isolate the bus */ |
Franky Lin | 718897eb | 2011-11-04 22:23:27 +0100 | [diff] [blame] | 901 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 902 | SBSDIO_DEVICE_CTL, |
| 903 | SBSDIO_DEVCTL_PADS_ISO, NULL); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 904 | |
| 905 | /* Change state */ |
| 906 | bus->sleeping = true; |
| 907 | |
| 908 | } else { |
| 909 | /* Waking up: bus power up is ok, set local state */ |
| 910 | |
| 911 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 912 | SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL); |
| 913 | |
| 914 | /* Force pad isolation off if possible |
| 915 | (in case power never toggled) */ |
Franky Lin | 99ba15c | 2011-11-04 22:23:42 +0100 | [diff] [blame] | 916 | if ((bus->ci->c_inf[1].id == PCMCIA_CORE_ID) |
| 917 | && (bus->ci->c_inf[1].rev >= 10)) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 918 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 919 | SBSDIO_DEVICE_CTL, 0, NULL); |
| 920 | |
| 921 | /* Make sure the controller has the bus up */ |
| 922 | brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false); |
| 923 | |
| 924 | /* Send misc interrupt to indicate OOB not needed */ |
| 925 | w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, tosbmailboxdata), |
| 926 | &retries); |
| 927 | if (retries <= retry_limit) |
| 928 | w_sdreg32(bus, SMB_DEV_INT, |
| 929 | offsetof(struct sdpcmd_regs, tosbmailbox), |
| 930 | &retries); |
| 931 | |
| 932 | if (retries > retry_limit) |
| 933 | brcmf_dbg(ERROR, "CANNOT SIGNAL CHIP TO CLEAR OOB!!\n"); |
| 934 | |
| 935 | /* Make sure we have SD bus access */ |
| 936 | brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false); |
| 937 | |
| 938 | /* Change state */ |
| 939 | bus->sleeping = false; |
| 940 | } |
| 941 | |
| 942 | return 0; |
| 943 | } |
| 944 | |
| 945 | static void bus_wake(struct brcmf_bus *bus) |
| 946 | { |
| 947 | if (bus->sleeping) |
| 948 | brcmf_sdbrcm_bussleep(bus, false); |
| 949 | } |
| 950 | |
| 951 | static u32 brcmf_sdbrcm_hostmail(struct brcmf_bus *bus) |
| 952 | { |
| 953 | u32 intstatus = 0; |
| 954 | u32 hmb_data; |
| 955 | u8 fcbits; |
| 956 | uint retries = 0; |
| 957 | |
| 958 | brcmf_dbg(TRACE, "Enter\n"); |
| 959 | |
| 960 | /* Read mailbox data and ack that we did so */ |
| 961 | r_sdreg32(bus, &hmb_data, |
| 962 | offsetof(struct sdpcmd_regs, tohostmailboxdata), &retries); |
| 963 | |
| 964 | if (retries <= retry_limit) |
| 965 | w_sdreg32(bus, SMB_INT_ACK, |
| 966 | offsetof(struct sdpcmd_regs, tosbmailbox), &retries); |
| 967 | bus->f1regdata += 2; |
| 968 | |
| 969 | /* Dongle recomposed rx frames, accept them again */ |
| 970 | if (hmb_data & HMB_DATA_NAKHANDLED) { |
| 971 | brcmf_dbg(INFO, "Dongle reports NAK handled, expect rtx of %d\n", |
| 972 | bus->rx_seq); |
| 973 | if (!bus->rxskip) |
| 974 | brcmf_dbg(ERROR, "unexpected NAKHANDLED!\n"); |
| 975 | |
| 976 | bus->rxskip = false; |
| 977 | intstatus |= I_HMB_FRAME_IND; |
| 978 | } |
| 979 | |
| 980 | /* |
| 981 | * DEVREADY does not occur with gSPI. |
| 982 | */ |
| 983 | if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) { |
| 984 | bus->sdpcm_ver = |
| 985 | (hmb_data & HMB_DATA_VERSION_MASK) >> |
| 986 | HMB_DATA_VERSION_SHIFT; |
| 987 | if (bus->sdpcm_ver != SDPCM_PROT_VERSION) |
| 988 | brcmf_dbg(ERROR, "Version mismatch, dongle reports %d, " |
| 989 | "expecting %d\n", |
| 990 | bus->sdpcm_ver, SDPCM_PROT_VERSION); |
| 991 | else |
| 992 | brcmf_dbg(INFO, "Dongle ready, protocol version %d\n", |
| 993 | bus->sdpcm_ver); |
| 994 | } |
| 995 | |
| 996 | /* |
| 997 | * Flow Control has been moved into the RX headers and this out of band |
| 998 | * method isn't used any more. |
| 999 | * remaining backward compatible with older dongles. |
| 1000 | */ |
| 1001 | if (hmb_data & HMB_DATA_FC) { |
| 1002 | fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >> |
| 1003 | HMB_DATA_FCDATA_SHIFT; |
| 1004 | |
| 1005 | if (fcbits & ~bus->flowcontrol) |
| 1006 | bus->fc_xoff++; |
| 1007 | |
| 1008 | if (bus->flowcontrol & ~fcbits) |
| 1009 | bus->fc_xon++; |
| 1010 | |
| 1011 | bus->fc_rcvd++; |
| 1012 | bus->flowcontrol = fcbits; |
| 1013 | } |
| 1014 | |
| 1015 | /* Shouldn't be any others */ |
| 1016 | if (hmb_data & ~(HMB_DATA_DEVREADY | |
| 1017 | HMB_DATA_NAKHANDLED | |
| 1018 | HMB_DATA_FC | |
| 1019 | HMB_DATA_FWREADY | |
| 1020 | HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK)) |
| 1021 | brcmf_dbg(ERROR, "Unknown mailbox data content: 0x%02x\n", |
| 1022 | hmb_data); |
| 1023 | |
| 1024 | return intstatus; |
| 1025 | } |
| 1026 | |
| 1027 | static void brcmf_sdbrcm_rxfail(struct brcmf_bus *bus, bool abort, bool rtx) |
| 1028 | { |
| 1029 | uint retries = 0; |
| 1030 | u16 lastrbc; |
| 1031 | u8 hi, lo; |
| 1032 | int err; |
| 1033 | |
| 1034 | brcmf_dbg(ERROR, "%sterminate frame%s\n", |
| 1035 | abort ? "abort command, " : "", |
| 1036 | rtx ? ", send NAK" : ""); |
| 1037 | |
| 1038 | if (abort) |
| 1039 | brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2); |
| 1040 | |
| 1041 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 1042 | SBSDIO_FUNC1_FRAMECTRL, |
| 1043 | SFC_RF_TERM, &err); |
| 1044 | bus->f1regdata++; |
| 1045 | |
| 1046 | /* Wait until the packet has been flushed (device/FIFO stable) */ |
| 1047 | for (lastrbc = retries = 0xffff; retries > 0; retries--) { |
| 1048 | hi = brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 1049 | SBSDIO_FUNC1_RFRAMEBCHI, NULL); |
| 1050 | lo = brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 1051 | SBSDIO_FUNC1_RFRAMEBCLO, NULL); |
| 1052 | bus->f1regdata += 2; |
| 1053 | |
| 1054 | if ((hi == 0) && (lo == 0)) |
| 1055 | break; |
| 1056 | |
| 1057 | if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) { |
| 1058 | brcmf_dbg(ERROR, "count growing: last 0x%04x now 0x%04x\n", |
| 1059 | lastrbc, (hi << 8) + lo); |
| 1060 | } |
| 1061 | lastrbc = (hi << 8) + lo; |
| 1062 | } |
| 1063 | |
| 1064 | if (!retries) |
| 1065 | brcmf_dbg(ERROR, "count never zeroed: last 0x%04x\n", lastrbc); |
| 1066 | else |
| 1067 | brcmf_dbg(INFO, "flush took %d iterations\n", 0xffff - retries); |
| 1068 | |
| 1069 | if (rtx) { |
| 1070 | bus->rxrtx++; |
| 1071 | w_sdreg32(bus, SMB_NAK, |
| 1072 | offsetof(struct sdpcmd_regs, tosbmailbox), &retries); |
| 1073 | |
| 1074 | bus->f1regdata++; |
| 1075 | if (retries <= retry_limit) |
| 1076 | bus->rxskip = true; |
| 1077 | } |
| 1078 | |
| 1079 | /* Clear partial in any case */ |
| 1080 | bus->nextlen = 0; |
| 1081 | |
| 1082 | /* If we can't reach the device, signal failure */ |
| 1083 | if (err || brcmf_sdcard_regfail(bus->sdiodev)) |
| 1084 | bus->drvr->busstate = BRCMF_BUS_DOWN; |
| 1085 | } |
| 1086 | |
Arend van Spriel | 20e5ca1 | 2011-10-18 14:03:09 +0200 | [diff] [blame] | 1087 | /* copy a buffer into a pkt buffer chain */ |
| 1088 | static uint brcmf_sdbrcm_glom_from_buf(struct brcmf_bus *bus, uint len) |
| 1089 | { |
| 1090 | uint n, ret = 0; |
| 1091 | struct sk_buff *p; |
| 1092 | u8 *buf; |
| 1093 | |
Arend van Spriel | 20e5ca1 | 2011-10-18 14:03:09 +0200 | [diff] [blame] | 1094 | buf = bus->dataptr; |
| 1095 | |
| 1096 | /* copy the data */ |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 1097 | skb_queue_walk(&bus->glom, p) { |
Arend van Spriel | 20e5ca1 | 2011-10-18 14:03:09 +0200 | [diff] [blame] | 1098 | n = min_t(uint, p->len, len); |
| 1099 | memcpy(p->data, buf, n); |
| 1100 | buf += n; |
| 1101 | len -= n; |
| 1102 | ret += n; |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 1103 | if (!len) |
| 1104 | break; |
Arend van Spriel | 20e5ca1 | 2011-10-18 14:03:09 +0200 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | return ret; |
| 1108 | } |
| 1109 | |
Arend van Spriel | 9a95e60 | 2011-11-10 20:30:29 +0100 | [diff] [blame] | 1110 | /* return total length of buffer chain */ |
| 1111 | static uint brcmf_sdbrcm_glom_len(struct brcmf_bus *bus) |
| 1112 | { |
| 1113 | struct sk_buff *p; |
| 1114 | uint total; |
| 1115 | |
| 1116 | total = 0; |
| 1117 | skb_queue_walk(&bus->glom, p) |
| 1118 | total += p->len; |
| 1119 | return total; |
| 1120 | } |
| 1121 | |
Arend van Spriel | 046808d | 2011-11-10 20:30:31 +0100 | [diff] [blame^] | 1122 | static void brcmf_sdbrcm_free_glom(struct brcmf_bus *bus) |
| 1123 | { |
| 1124 | struct sk_buff *cur, *next; |
| 1125 | |
| 1126 | skb_queue_walk_safe(&bus->glom, cur, next) { |
| 1127 | skb_unlink(cur, &bus->glom); |
| 1128 | brcmu_pkt_buf_free_skb(cur); |
| 1129 | } |
| 1130 | } |
| 1131 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1132 | static u8 brcmf_sdbrcm_rxglom(struct brcmf_bus *bus, u8 rxseq) |
| 1133 | { |
| 1134 | u16 dlen, totlen; |
| 1135 | u8 *dptr, num = 0; |
| 1136 | |
| 1137 | u16 sublen, check; |
| 1138 | struct sk_buff *pfirst, *plast, *pnext, *save_pfirst; |
| 1139 | |
| 1140 | int errcode; |
| 1141 | u8 chan, seq, doff, sfdoff; |
| 1142 | u8 txmax; |
| 1143 | |
| 1144 | int ifidx = 0; |
| 1145 | bool usechain = bus->use_rxchain; |
| 1146 | |
| 1147 | /* If packets, issue read(s) and send up packet chain */ |
| 1148 | /* Return sequence numbers consumed? */ |
| 1149 | |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 1150 | brcmf_dbg(TRACE, "start: glomd %p glom %p\n", |
| 1151 | bus->glomd, skb_peek(&bus->glom)); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1152 | |
| 1153 | /* If there's a descriptor, generate the packet chain */ |
| 1154 | if (bus->glomd) { |
| 1155 | pfirst = plast = pnext = NULL; |
| 1156 | dlen = (u16) (bus->glomd->len); |
| 1157 | dptr = bus->glomd->data; |
| 1158 | if (!dlen || (dlen & 1)) { |
| 1159 | brcmf_dbg(ERROR, "bad glomd len(%d), ignore descriptor\n", |
| 1160 | dlen); |
| 1161 | dlen = 0; |
| 1162 | } |
| 1163 | |
| 1164 | for (totlen = num = 0; dlen; num++) { |
| 1165 | /* Get (and move past) next length */ |
| 1166 | sublen = get_unaligned_le16(dptr); |
| 1167 | dlen -= sizeof(u16); |
| 1168 | dptr += sizeof(u16); |
| 1169 | if ((sublen < SDPCM_HDRLEN) || |
| 1170 | ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) { |
| 1171 | brcmf_dbg(ERROR, "descriptor len %d bad: %d\n", |
| 1172 | num, sublen); |
| 1173 | pnext = NULL; |
| 1174 | break; |
| 1175 | } |
| 1176 | if (sublen % BRCMF_SDALIGN) { |
| 1177 | brcmf_dbg(ERROR, "sublen %d not multiple of %d\n", |
| 1178 | sublen, BRCMF_SDALIGN); |
| 1179 | usechain = false; |
| 1180 | } |
| 1181 | totlen += sublen; |
| 1182 | |
| 1183 | /* For last frame, adjust read len so total |
| 1184 | is a block multiple */ |
| 1185 | if (!dlen) { |
| 1186 | sublen += |
| 1187 | (roundup(totlen, bus->blocksize) - totlen); |
| 1188 | totlen = roundup(totlen, bus->blocksize); |
| 1189 | } |
| 1190 | |
| 1191 | /* Allocate/chain packet for next subframe */ |
| 1192 | pnext = brcmu_pkt_buf_get_skb(sublen + BRCMF_SDALIGN); |
| 1193 | if (pnext == NULL) { |
| 1194 | brcmf_dbg(ERROR, "bcm_pkt_buf_get_skb failed, num %d len %d\n", |
| 1195 | num, sublen); |
| 1196 | break; |
| 1197 | } |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 1198 | skb_queue_tail(&bus->glom, pnext); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1199 | |
| 1200 | /* Adhere to start alignment requirements */ |
| 1201 | pkt_align(pnext, sublen, BRCMF_SDALIGN); |
| 1202 | } |
| 1203 | |
| 1204 | /* If all allocations succeeded, save packet chain |
| 1205 | in bus structure */ |
| 1206 | if (pnext) { |
| 1207 | brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n", |
| 1208 | totlen, num); |
| 1209 | if (BRCMF_GLOM_ON() && bus->nextlen && |
| 1210 | totlen != bus->nextlen) { |
| 1211 | brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n", |
| 1212 | bus->nextlen, totlen, rxseq); |
| 1213 | } |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1214 | pfirst = pnext = NULL; |
| 1215 | } else { |
Arend van Spriel | 046808d | 2011-11-10 20:30:31 +0100 | [diff] [blame^] | 1216 | brcmf_sdbrcm_free_glom(bus); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1217 | num = 0; |
| 1218 | } |
| 1219 | |
| 1220 | /* Done with descriptor packet */ |
| 1221 | brcmu_pkt_buf_free_skb(bus->glomd); |
| 1222 | bus->glomd = NULL; |
| 1223 | bus->nextlen = 0; |
| 1224 | } |
| 1225 | |
| 1226 | /* Ok -- either we just generated a packet chain, |
| 1227 | or had one from before */ |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 1228 | if (!skb_queue_empty(&bus->glom)) { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1229 | if (BRCMF_GLOM_ON()) { |
| 1230 | brcmf_dbg(GLOM, "try superframe read, packet chain:\n"); |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 1231 | skb_queue_walk(&bus->glom, pnext) { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1232 | brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n", |
| 1233 | pnext, (u8 *) (pnext->data), |
| 1234 | pnext->len, pnext->len); |
| 1235 | } |
| 1236 | } |
| 1237 | |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 1238 | pfirst = skb_peek(&bus->glom); |
Arend van Spriel | 9a95e60 | 2011-11-10 20:30:29 +0100 | [diff] [blame] | 1239 | dlen = (u16) brcmf_sdbrcm_glom_len(bus); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1240 | |
| 1241 | /* Do an SDIO read for the superframe. Configurable iovar to |
| 1242 | * read directly into the chained packet, or allocate a large |
| 1243 | * packet and and copy into the chain. |
| 1244 | */ |
| 1245 | if (usechain) { |
| 1246 | errcode = brcmf_sdcard_recv_buf(bus->sdiodev, |
| 1247 | bus->sdiodev->sbwad, |
| 1248 | SDIO_FUNC_2, |
| 1249 | F2SYNC, (u8 *) pfirst->data, dlen, |
| 1250 | pfirst); |
| 1251 | } else if (bus->dataptr) { |
| 1252 | errcode = brcmf_sdcard_recv_buf(bus->sdiodev, |
| 1253 | bus->sdiodev->sbwad, |
| 1254 | SDIO_FUNC_2, |
| 1255 | F2SYNC, bus->dataptr, dlen, |
| 1256 | NULL); |
Arend van Spriel | 20e5ca1 | 2011-10-18 14:03:09 +0200 | [diff] [blame] | 1257 | sublen = (u16) brcmf_sdbrcm_glom_from_buf(bus, dlen); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1258 | if (sublen != dlen) { |
| 1259 | brcmf_dbg(ERROR, "FAILED TO COPY, dlen %d sublen %d\n", |
| 1260 | dlen, sublen); |
| 1261 | errcode = -1; |
| 1262 | } |
| 1263 | pnext = NULL; |
| 1264 | } else { |
| 1265 | brcmf_dbg(ERROR, "COULDN'T ALLOC %d-BYTE GLOM, FORCE FAILURE\n", |
| 1266 | dlen); |
| 1267 | errcode = -1; |
| 1268 | } |
| 1269 | bus->f2rxdata++; |
| 1270 | |
| 1271 | /* On failure, kill the superframe, allow a couple retries */ |
| 1272 | if (errcode < 0) { |
| 1273 | brcmf_dbg(ERROR, "glom read of %d bytes failed: %d\n", |
| 1274 | dlen, errcode); |
| 1275 | bus->drvr->rx_errors++; |
| 1276 | |
| 1277 | if (bus->glomerr++ < 3) { |
| 1278 | brcmf_sdbrcm_rxfail(bus, true, true); |
| 1279 | } else { |
| 1280 | bus->glomerr = 0; |
| 1281 | brcmf_sdbrcm_rxfail(bus, true, false); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1282 | bus->rxglomfail++; |
Arend van Spriel | 046808d | 2011-11-10 20:30:31 +0100 | [diff] [blame^] | 1283 | brcmf_sdbrcm_free_glom(bus); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1284 | } |
| 1285 | return 0; |
| 1286 | } |
| 1287 | #ifdef BCMDBG |
| 1288 | if (BRCMF_GLOM_ON()) { |
| 1289 | printk(KERN_DEBUG "SUPERFRAME:\n"); |
| 1290 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 1291 | pfirst->data, min_t(int, pfirst->len, 48)); |
| 1292 | } |
| 1293 | #endif |
| 1294 | |
| 1295 | /* Validate the superframe header */ |
| 1296 | dptr = (u8 *) (pfirst->data); |
| 1297 | sublen = get_unaligned_le16(dptr); |
| 1298 | check = get_unaligned_le16(dptr + sizeof(u16)); |
| 1299 | |
| 1300 | chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]); |
| 1301 | seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]); |
| 1302 | bus->nextlen = dptr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET]; |
| 1303 | if ((bus->nextlen << 4) > MAX_RX_DATASZ) { |
| 1304 | brcmf_dbg(INFO, "nextlen too large (%d) seq %d\n", |
| 1305 | bus->nextlen, seq); |
| 1306 | bus->nextlen = 0; |
| 1307 | } |
| 1308 | doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]); |
| 1309 | txmax = SDPCM_WINDOW_VALUE(&dptr[SDPCM_FRAMETAG_LEN]); |
| 1310 | |
| 1311 | errcode = 0; |
| 1312 | if ((u16)~(sublen ^ check)) { |
| 1313 | brcmf_dbg(ERROR, "(superframe): HW hdr error: len/check 0x%04x/0x%04x\n", |
| 1314 | sublen, check); |
| 1315 | errcode = -1; |
| 1316 | } else if (roundup(sublen, bus->blocksize) != dlen) { |
| 1317 | brcmf_dbg(ERROR, "(superframe): len 0x%04x, rounded 0x%04x, expect 0x%04x\n", |
| 1318 | sublen, roundup(sublen, bus->blocksize), |
| 1319 | dlen); |
| 1320 | errcode = -1; |
| 1321 | } else if (SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]) != |
| 1322 | SDPCM_GLOM_CHANNEL) { |
| 1323 | brcmf_dbg(ERROR, "(superframe): bad channel %d\n", |
| 1324 | SDPCM_PACKET_CHANNEL( |
| 1325 | &dptr[SDPCM_FRAMETAG_LEN])); |
| 1326 | errcode = -1; |
| 1327 | } else if (SDPCM_GLOMDESC(&dptr[SDPCM_FRAMETAG_LEN])) { |
| 1328 | brcmf_dbg(ERROR, "(superframe): got 2nd descriptor?\n"); |
| 1329 | errcode = -1; |
| 1330 | } else if ((doff < SDPCM_HDRLEN) || |
| 1331 | (doff > (pfirst->len - SDPCM_HDRLEN))) { |
| 1332 | brcmf_dbg(ERROR, "(superframe): Bad data offset %d: HW %d pkt %d min %d\n", |
| 1333 | doff, sublen, pfirst->len, SDPCM_HDRLEN); |
| 1334 | errcode = -1; |
| 1335 | } |
| 1336 | |
| 1337 | /* Check sequence number of superframe SW header */ |
| 1338 | if (rxseq != seq) { |
| 1339 | brcmf_dbg(INFO, "(superframe) rx_seq %d, expected %d\n", |
| 1340 | seq, rxseq); |
| 1341 | bus->rx_badseq++; |
| 1342 | rxseq = seq; |
| 1343 | } |
| 1344 | |
| 1345 | /* Check window for sanity */ |
| 1346 | if ((u8) (txmax - bus->tx_seq) > 0x40) { |
| 1347 | brcmf_dbg(ERROR, "unlikely tx max %d with tx_seq %d\n", |
| 1348 | txmax, bus->tx_seq); |
| 1349 | txmax = bus->tx_seq + 2; |
| 1350 | } |
| 1351 | bus->tx_max = txmax; |
| 1352 | |
| 1353 | /* Remove superframe header, remember offset */ |
| 1354 | skb_pull(pfirst, doff); |
| 1355 | sfdoff = doff; |
| 1356 | |
| 1357 | /* Validate all the subframe headers */ |
| 1358 | for (num = 0, pnext = pfirst; pnext && !errcode; |
| 1359 | num++, pnext = pnext->next) { |
| 1360 | dptr = (u8 *) (pnext->data); |
| 1361 | dlen = (u16) (pnext->len); |
| 1362 | sublen = get_unaligned_le16(dptr); |
| 1363 | check = get_unaligned_le16(dptr + sizeof(u16)); |
| 1364 | chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]); |
| 1365 | doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]); |
| 1366 | #ifdef BCMDBG |
| 1367 | if (BRCMF_GLOM_ON()) { |
| 1368 | printk(KERN_DEBUG "subframe:\n"); |
| 1369 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 1370 | dptr, 32); |
| 1371 | } |
| 1372 | #endif |
| 1373 | |
| 1374 | if ((u16)~(sublen ^ check)) { |
| 1375 | brcmf_dbg(ERROR, "(subframe %d): HW hdr error: len/check 0x%04x/0x%04x\n", |
| 1376 | num, sublen, check); |
| 1377 | errcode = -1; |
| 1378 | } else if ((sublen > dlen) || (sublen < SDPCM_HDRLEN)) { |
| 1379 | brcmf_dbg(ERROR, "(subframe %d): length mismatch: len 0x%04x, expect 0x%04x\n", |
| 1380 | num, sublen, dlen); |
| 1381 | errcode = -1; |
| 1382 | } else if ((chan != SDPCM_DATA_CHANNEL) && |
| 1383 | (chan != SDPCM_EVENT_CHANNEL)) { |
| 1384 | brcmf_dbg(ERROR, "(subframe %d): bad channel %d\n", |
| 1385 | num, chan); |
| 1386 | errcode = -1; |
| 1387 | } else if ((doff < SDPCM_HDRLEN) || (doff > sublen)) { |
| 1388 | brcmf_dbg(ERROR, "(subframe %d): Bad data offset %d: HW %d min %d\n", |
| 1389 | num, doff, sublen, SDPCM_HDRLEN); |
| 1390 | errcode = -1; |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | if (errcode) { |
| 1395 | /* Terminate frame on error, request |
| 1396 | a couple retries */ |
| 1397 | if (bus->glomerr++ < 3) { |
| 1398 | /* Restore superframe header space */ |
| 1399 | skb_push(pfirst, sfdoff); |
| 1400 | brcmf_sdbrcm_rxfail(bus, true, true); |
| 1401 | } else { |
| 1402 | bus->glomerr = 0; |
| 1403 | brcmf_sdbrcm_rxfail(bus, true, false); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1404 | bus->rxglomfail++; |
Arend van Spriel | 046808d | 2011-11-10 20:30:31 +0100 | [diff] [blame^] | 1405 | brcmf_sdbrcm_free_glom(bus); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1406 | } |
| 1407 | bus->nextlen = 0; |
| 1408 | return 0; |
| 1409 | } |
| 1410 | |
| 1411 | /* Basic SD framing looks ok - process each packet (header) */ |
| 1412 | save_pfirst = pfirst; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1413 | plast = NULL; |
| 1414 | |
| 1415 | for (num = 0; pfirst; rxseq++, pfirst = pnext) { |
| 1416 | pnext = pfirst->next; |
| 1417 | pfirst->next = NULL; |
| 1418 | |
| 1419 | dptr = (u8 *) (pfirst->data); |
| 1420 | sublen = get_unaligned_le16(dptr); |
| 1421 | chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]); |
| 1422 | seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]); |
| 1423 | doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]); |
| 1424 | |
| 1425 | brcmf_dbg(GLOM, "Get subframe %d, %p(%p/%d), sublen %d chan %d seq %d\n", |
| 1426 | num, pfirst, pfirst->data, |
| 1427 | pfirst->len, sublen, chan, seq); |
| 1428 | |
| 1429 | /* precondition: chan == SDPCM_DATA_CHANNEL || |
| 1430 | chan == SDPCM_EVENT_CHANNEL */ |
| 1431 | |
| 1432 | if (rxseq != seq) { |
| 1433 | brcmf_dbg(GLOM, "rx_seq %d, expected %d\n", |
| 1434 | seq, rxseq); |
| 1435 | bus->rx_badseq++; |
| 1436 | rxseq = seq; |
| 1437 | } |
| 1438 | #ifdef BCMDBG |
| 1439 | if (BRCMF_BYTES_ON() && BRCMF_DATA_ON()) { |
| 1440 | printk(KERN_DEBUG "Rx Subframe Data:\n"); |
| 1441 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 1442 | dptr, dlen); |
| 1443 | } |
| 1444 | #endif |
| 1445 | |
| 1446 | __skb_trim(pfirst, sublen); |
| 1447 | skb_pull(pfirst, doff); |
| 1448 | |
| 1449 | if (pfirst->len == 0) { |
| 1450 | brcmu_pkt_buf_free_skb(pfirst); |
| 1451 | if (plast) |
| 1452 | plast->next = pnext; |
| 1453 | else |
| 1454 | save_pfirst = pnext; |
| 1455 | |
| 1456 | continue; |
| 1457 | } else if (brcmf_proto_hdrpull(bus->drvr, &ifidx, |
| 1458 | pfirst) != 0) { |
| 1459 | brcmf_dbg(ERROR, "rx protocol error\n"); |
| 1460 | bus->drvr->rx_errors++; |
| 1461 | brcmu_pkt_buf_free_skb(pfirst); |
| 1462 | if (plast) |
| 1463 | plast->next = pnext; |
| 1464 | else |
| 1465 | save_pfirst = pnext; |
| 1466 | |
| 1467 | continue; |
| 1468 | } |
| 1469 | |
| 1470 | /* this packet will go up, link back into |
| 1471 | chain and count it */ |
| 1472 | pfirst->next = pnext; |
| 1473 | plast = pfirst; |
| 1474 | num++; |
| 1475 | |
| 1476 | #ifdef BCMDBG |
| 1477 | if (BRCMF_GLOM_ON()) { |
| 1478 | brcmf_dbg(GLOM, "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n", |
| 1479 | num, pfirst, pfirst->data, |
| 1480 | pfirst->len, pfirst->next, |
| 1481 | pfirst->prev); |
| 1482 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 1483 | pfirst->data, |
| 1484 | min_t(int, pfirst->len, 32)); |
| 1485 | } |
| 1486 | #endif /* BCMDBG */ |
| 1487 | } |
| 1488 | if (num) { |
| 1489 | up(&bus->sdsem); |
| 1490 | brcmf_rx_frame(bus->drvr, ifidx, save_pfirst, num); |
| 1491 | down(&bus->sdsem); |
| 1492 | } |
| 1493 | |
| 1494 | bus->rxglomframes++; |
| 1495 | bus->rxglompkts += num; |
| 1496 | } |
| 1497 | return num; |
| 1498 | } |
| 1499 | |
| 1500 | static int brcmf_sdbrcm_dcmd_resp_wait(struct brcmf_bus *bus, uint *condition, |
| 1501 | bool *pending) |
| 1502 | { |
| 1503 | DECLARE_WAITQUEUE(wait, current); |
| 1504 | int timeout = msecs_to_jiffies(DCMD_RESP_TIMEOUT); |
| 1505 | |
| 1506 | /* Wait until control frame is available */ |
| 1507 | add_wait_queue(&bus->dcmd_resp_wait, &wait); |
| 1508 | set_current_state(TASK_INTERRUPTIBLE); |
| 1509 | |
| 1510 | while (!(*condition) && (!signal_pending(current) && timeout)) |
| 1511 | timeout = schedule_timeout(timeout); |
| 1512 | |
| 1513 | if (signal_pending(current)) |
| 1514 | *pending = true; |
| 1515 | |
| 1516 | set_current_state(TASK_RUNNING); |
| 1517 | remove_wait_queue(&bus->dcmd_resp_wait, &wait); |
| 1518 | |
| 1519 | return timeout; |
| 1520 | } |
| 1521 | |
| 1522 | static int brcmf_sdbrcm_dcmd_resp_wake(struct brcmf_bus *bus) |
| 1523 | { |
| 1524 | if (waitqueue_active(&bus->dcmd_resp_wait)) |
| 1525 | wake_up_interruptible(&bus->dcmd_resp_wait); |
| 1526 | |
| 1527 | return 0; |
| 1528 | } |
| 1529 | static void |
| 1530 | brcmf_sdbrcm_read_control(struct brcmf_bus *bus, u8 *hdr, uint len, uint doff) |
| 1531 | { |
| 1532 | uint rdlen, pad; |
| 1533 | |
| 1534 | int sdret; |
| 1535 | |
| 1536 | brcmf_dbg(TRACE, "Enter\n"); |
| 1537 | |
| 1538 | /* Set rxctl for frame (w/optional alignment) */ |
| 1539 | bus->rxctl = bus->rxbuf; |
| 1540 | bus->rxctl += BRCMF_FIRSTREAD; |
| 1541 | pad = ((unsigned long)bus->rxctl % BRCMF_SDALIGN); |
| 1542 | if (pad) |
| 1543 | bus->rxctl += (BRCMF_SDALIGN - pad); |
| 1544 | bus->rxctl -= BRCMF_FIRSTREAD; |
| 1545 | |
| 1546 | /* Copy the already-read portion over */ |
| 1547 | memcpy(bus->rxctl, hdr, BRCMF_FIRSTREAD); |
| 1548 | if (len <= BRCMF_FIRSTREAD) |
| 1549 | goto gotpkt; |
| 1550 | |
| 1551 | /* Raise rdlen to next SDIO block to avoid tail command */ |
| 1552 | rdlen = len - BRCMF_FIRSTREAD; |
| 1553 | if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) { |
| 1554 | pad = bus->blocksize - (rdlen % bus->blocksize); |
| 1555 | if ((pad <= bus->roundup) && (pad < bus->blocksize) && |
| 1556 | ((len + pad) < bus->drvr->maxctl)) |
| 1557 | rdlen += pad; |
| 1558 | } else if (rdlen % BRCMF_SDALIGN) { |
| 1559 | rdlen += BRCMF_SDALIGN - (rdlen % BRCMF_SDALIGN); |
| 1560 | } |
| 1561 | |
| 1562 | /* Satisfy length-alignment requirements */ |
| 1563 | if (rdlen & (ALIGNMENT - 1)) |
| 1564 | rdlen = roundup(rdlen, ALIGNMENT); |
| 1565 | |
| 1566 | /* Drop if the read is too big or it exceeds our maximum */ |
| 1567 | if ((rdlen + BRCMF_FIRSTREAD) > bus->drvr->maxctl) { |
| 1568 | brcmf_dbg(ERROR, "%d-byte control read exceeds %d-byte buffer\n", |
| 1569 | rdlen, bus->drvr->maxctl); |
| 1570 | bus->drvr->rx_errors++; |
| 1571 | brcmf_sdbrcm_rxfail(bus, false, false); |
| 1572 | goto done; |
| 1573 | } |
| 1574 | |
| 1575 | if ((len - doff) > bus->drvr->maxctl) { |
| 1576 | brcmf_dbg(ERROR, "%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n", |
| 1577 | len, len - doff, bus->drvr->maxctl); |
| 1578 | bus->drvr->rx_errors++; |
| 1579 | bus->rx_toolong++; |
| 1580 | brcmf_sdbrcm_rxfail(bus, false, false); |
| 1581 | goto done; |
| 1582 | } |
| 1583 | |
| 1584 | /* Read remainder of frame body into the rxctl buffer */ |
| 1585 | sdret = brcmf_sdcard_recv_buf(bus->sdiodev, |
| 1586 | bus->sdiodev->sbwad, |
| 1587 | SDIO_FUNC_2, |
| 1588 | F2SYNC, (bus->rxctl + BRCMF_FIRSTREAD), rdlen, |
| 1589 | NULL); |
| 1590 | bus->f2rxdata++; |
| 1591 | |
| 1592 | /* Control frame failures need retransmission */ |
| 1593 | if (sdret < 0) { |
| 1594 | brcmf_dbg(ERROR, "read %d control bytes failed: %d\n", |
| 1595 | rdlen, sdret); |
| 1596 | bus->rxc_errors++; |
| 1597 | brcmf_sdbrcm_rxfail(bus, true, true); |
| 1598 | goto done; |
| 1599 | } |
| 1600 | |
| 1601 | gotpkt: |
| 1602 | |
| 1603 | #ifdef BCMDBG |
| 1604 | if (BRCMF_BYTES_ON() && BRCMF_CTL_ON()) { |
| 1605 | printk(KERN_DEBUG "RxCtrl:\n"); |
| 1606 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, bus->rxctl, len); |
| 1607 | } |
| 1608 | #endif |
| 1609 | |
| 1610 | /* Point to valid data and indicate its length */ |
| 1611 | bus->rxctl += doff; |
| 1612 | bus->rxlen = len - doff; |
| 1613 | |
| 1614 | done: |
| 1615 | /* Awake any waiters */ |
| 1616 | brcmf_sdbrcm_dcmd_resp_wake(bus); |
| 1617 | } |
| 1618 | |
| 1619 | /* Pad read to blocksize for efficiency */ |
| 1620 | static void brcmf_pad(struct brcmf_bus *bus, u16 *pad, u16 *rdlen) |
| 1621 | { |
| 1622 | if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) { |
| 1623 | *pad = bus->blocksize - (*rdlen % bus->blocksize); |
| 1624 | if (*pad <= bus->roundup && *pad < bus->blocksize && |
| 1625 | *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ) |
| 1626 | *rdlen += *pad; |
| 1627 | } else if (*rdlen % BRCMF_SDALIGN) { |
| 1628 | *rdlen += BRCMF_SDALIGN - (*rdlen % BRCMF_SDALIGN); |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | static void |
| 1633 | brcmf_alloc_pkt_and_read(struct brcmf_bus *bus, u16 rdlen, |
| 1634 | struct sk_buff **pkt, u8 **rxbuf) |
| 1635 | { |
| 1636 | int sdret; /* Return code from calls */ |
| 1637 | |
| 1638 | *pkt = brcmu_pkt_buf_get_skb(rdlen + BRCMF_SDALIGN); |
| 1639 | if (*pkt == NULL) |
| 1640 | return; |
| 1641 | |
| 1642 | pkt_align(*pkt, rdlen, BRCMF_SDALIGN); |
| 1643 | *rxbuf = (u8 *) ((*pkt)->data); |
| 1644 | /* Read the entire frame */ |
| 1645 | sdret = brcmf_sdcard_recv_buf(bus->sdiodev, bus->sdiodev->sbwad, |
| 1646 | SDIO_FUNC_2, F2SYNC, |
| 1647 | *rxbuf, rdlen, *pkt); |
| 1648 | bus->f2rxdata++; |
| 1649 | |
| 1650 | if (sdret < 0) { |
| 1651 | brcmf_dbg(ERROR, "(nextlen): read %d bytes failed: %d\n", |
| 1652 | rdlen, sdret); |
| 1653 | brcmu_pkt_buf_free_skb(*pkt); |
| 1654 | bus->drvr->rx_errors++; |
| 1655 | /* Force retry w/normal header read. |
| 1656 | * Don't attempt NAK for |
| 1657 | * gSPI |
| 1658 | */ |
| 1659 | brcmf_sdbrcm_rxfail(bus, true, true); |
| 1660 | *pkt = NULL; |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | /* Checks the header */ |
| 1665 | static int |
| 1666 | brcmf_check_rxbuf(struct brcmf_bus *bus, struct sk_buff *pkt, u8 *rxbuf, |
| 1667 | u8 rxseq, u16 nextlen, u16 *len) |
| 1668 | { |
| 1669 | u16 check; |
| 1670 | bool len_consistent; /* Result of comparing readahead len and |
| 1671 | len from hw-hdr */ |
| 1672 | |
| 1673 | memcpy(bus->rxhdr, rxbuf, SDPCM_HDRLEN); |
| 1674 | |
| 1675 | /* Extract hardware header fields */ |
| 1676 | *len = get_unaligned_le16(bus->rxhdr); |
| 1677 | check = get_unaligned_le16(bus->rxhdr + sizeof(u16)); |
| 1678 | |
| 1679 | /* All zeros means readahead info was bad */ |
| 1680 | if (!(*len | check)) { |
| 1681 | brcmf_dbg(INFO, "(nextlen): read zeros in HW header???\n"); |
| 1682 | goto fail; |
| 1683 | } |
| 1684 | |
| 1685 | /* Validate check bytes */ |
| 1686 | if ((u16)~(*len ^ check)) { |
| 1687 | brcmf_dbg(ERROR, "(nextlen): HW hdr error: nextlen/len/check 0x%04x/0x%04x/0x%04x\n", |
| 1688 | nextlen, *len, check); |
| 1689 | bus->rx_badhdr++; |
| 1690 | brcmf_sdbrcm_rxfail(bus, false, false); |
| 1691 | goto fail; |
| 1692 | } |
| 1693 | |
| 1694 | /* Validate frame length */ |
| 1695 | if (*len < SDPCM_HDRLEN) { |
| 1696 | brcmf_dbg(ERROR, "(nextlen): HW hdr length invalid: %d\n", |
| 1697 | *len); |
| 1698 | goto fail; |
| 1699 | } |
| 1700 | |
| 1701 | /* Check for consistency with readahead info */ |
| 1702 | len_consistent = (nextlen != (roundup(*len, 16) >> 4)); |
| 1703 | if (len_consistent) { |
| 1704 | /* Mismatch, force retry w/normal |
| 1705 | header (may be >4K) */ |
| 1706 | brcmf_dbg(ERROR, "(nextlen): mismatch, nextlen %d len %d rnd %d; expected rxseq %d\n", |
| 1707 | nextlen, *len, roundup(*len, 16), |
| 1708 | rxseq); |
| 1709 | brcmf_sdbrcm_rxfail(bus, true, true); |
| 1710 | goto fail; |
| 1711 | } |
| 1712 | |
| 1713 | return 0; |
| 1714 | |
| 1715 | fail: |
| 1716 | brcmf_sdbrcm_pktfree2(bus, pkt); |
| 1717 | return -EINVAL; |
| 1718 | } |
| 1719 | |
| 1720 | /* Return true if there may be more frames to read */ |
| 1721 | static uint |
| 1722 | brcmf_sdbrcm_readframes(struct brcmf_bus *bus, uint maxframes, bool *finished) |
| 1723 | { |
| 1724 | u16 len, check; /* Extracted hardware header fields */ |
| 1725 | u8 chan, seq, doff; /* Extracted software header fields */ |
| 1726 | u8 fcbits; /* Extracted fcbits from software header */ |
| 1727 | |
| 1728 | struct sk_buff *pkt; /* Packet for event or data frames */ |
| 1729 | u16 pad; /* Number of pad bytes to read */ |
| 1730 | u16 rdlen; /* Total number of bytes to read */ |
| 1731 | u8 rxseq; /* Next sequence number to expect */ |
| 1732 | uint rxleft = 0; /* Remaining number of frames allowed */ |
| 1733 | int sdret; /* Return code from calls */ |
| 1734 | u8 txmax; /* Maximum tx sequence offered */ |
| 1735 | u8 *rxbuf; |
| 1736 | int ifidx = 0; |
| 1737 | uint rxcount = 0; /* Total frames read */ |
| 1738 | |
| 1739 | brcmf_dbg(TRACE, "Enter\n"); |
| 1740 | |
| 1741 | /* Not finished unless we encounter no more frames indication */ |
| 1742 | *finished = false; |
| 1743 | |
| 1744 | for (rxseq = bus->rx_seq, rxleft = maxframes; |
| 1745 | !bus->rxskip && rxleft && bus->drvr->busstate != BRCMF_BUS_DOWN; |
| 1746 | rxseq++, rxleft--) { |
| 1747 | |
| 1748 | /* Handle glomming separately */ |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 1749 | if (bus->glomd || !skb_queue_empty(&bus->glom)) { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1750 | u8 cnt; |
| 1751 | brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n", |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 1752 | bus->glomd, skb_peek(&bus->glom)); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 1753 | cnt = brcmf_sdbrcm_rxglom(bus, rxseq); |
| 1754 | brcmf_dbg(GLOM, "rxglom returned %d\n", cnt); |
| 1755 | rxseq += cnt - 1; |
| 1756 | rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1; |
| 1757 | continue; |
| 1758 | } |
| 1759 | |
| 1760 | /* Try doing single read if we can */ |
| 1761 | if (bus->nextlen) { |
| 1762 | u16 nextlen = bus->nextlen; |
| 1763 | bus->nextlen = 0; |
| 1764 | |
| 1765 | rdlen = len = nextlen << 4; |
| 1766 | brcmf_pad(bus, &pad, &rdlen); |
| 1767 | |
| 1768 | /* |
| 1769 | * After the frame is received we have to |
| 1770 | * distinguish whether it is data |
| 1771 | * or non-data frame. |
| 1772 | */ |
| 1773 | brcmf_alloc_pkt_and_read(bus, rdlen, &pkt, &rxbuf); |
| 1774 | if (pkt == NULL) { |
| 1775 | /* Give up on data, request rtx of events */ |
| 1776 | brcmf_dbg(ERROR, "(nextlen): brcmf_alloc_pkt_and_read failed: len %d rdlen %d expected rxseq %d\n", |
| 1777 | len, rdlen, rxseq); |
| 1778 | continue; |
| 1779 | } |
| 1780 | |
| 1781 | if (brcmf_check_rxbuf(bus, pkt, rxbuf, rxseq, nextlen, |
| 1782 | &len) < 0) |
| 1783 | continue; |
| 1784 | |
| 1785 | /* Extract software header fields */ |
| 1786 | chan = SDPCM_PACKET_CHANNEL( |
| 1787 | &bus->rxhdr[SDPCM_FRAMETAG_LEN]); |
| 1788 | seq = SDPCM_PACKET_SEQUENCE( |
| 1789 | &bus->rxhdr[SDPCM_FRAMETAG_LEN]); |
| 1790 | doff = SDPCM_DOFFSET_VALUE( |
| 1791 | &bus->rxhdr[SDPCM_FRAMETAG_LEN]); |
| 1792 | txmax = SDPCM_WINDOW_VALUE( |
| 1793 | &bus->rxhdr[SDPCM_FRAMETAG_LEN]); |
| 1794 | |
| 1795 | bus->nextlen = |
| 1796 | bus->rxhdr[SDPCM_FRAMETAG_LEN + |
| 1797 | SDPCM_NEXTLEN_OFFSET]; |
| 1798 | if ((bus->nextlen << 4) > MAX_RX_DATASZ) { |
| 1799 | brcmf_dbg(INFO, "(nextlen): got frame w/nextlen too large (%d), seq %d\n", |
| 1800 | bus->nextlen, seq); |
| 1801 | bus->nextlen = 0; |
| 1802 | } |
| 1803 | |
| 1804 | bus->drvr->rx_readahead_cnt++; |
| 1805 | |
| 1806 | /* Handle Flow Control */ |
| 1807 | fcbits = SDPCM_FCMASK_VALUE( |
| 1808 | &bus->rxhdr[SDPCM_FRAMETAG_LEN]); |
| 1809 | |
| 1810 | if (bus->flowcontrol != fcbits) { |
| 1811 | if (~bus->flowcontrol & fcbits) |
| 1812 | bus->fc_xoff++; |
| 1813 | |
| 1814 | if (bus->flowcontrol & ~fcbits) |
| 1815 | bus->fc_xon++; |
| 1816 | |
| 1817 | bus->fc_rcvd++; |
| 1818 | bus->flowcontrol = fcbits; |
| 1819 | } |
| 1820 | |
| 1821 | /* Check and update sequence number */ |
| 1822 | if (rxseq != seq) { |
| 1823 | brcmf_dbg(INFO, "(nextlen): rx_seq %d, expected %d\n", |
| 1824 | seq, rxseq); |
| 1825 | bus->rx_badseq++; |
| 1826 | rxseq = seq; |
| 1827 | } |
| 1828 | |
| 1829 | /* Check window for sanity */ |
| 1830 | if ((u8) (txmax - bus->tx_seq) > 0x40) { |
| 1831 | brcmf_dbg(ERROR, "got unlikely tx max %d with tx_seq %d\n", |
| 1832 | txmax, bus->tx_seq); |
| 1833 | txmax = bus->tx_seq + 2; |
| 1834 | } |
| 1835 | bus->tx_max = txmax; |
| 1836 | |
| 1837 | #ifdef BCMDBG |
| 1838 | if (BRCMF_BYTES_ON() && BRCMF_DATA_ON()) { |
| 1839 | printk(KERN_DEBUG "Rx Data:\n"); |
| 1840 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 1841 | rxbuf, len); |
| 1842 | } else if (BRCMF_HDRS_ON()) { |
| 1843 | printk(KERN_DEBUG "RxHdr:\n"); |
| 1844 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 1845 | bus->rxhdr, SDPCM_HDRLEN); |
| 1846 | } |
| 1847 | #endif |
| 1848 | |
| 1849 | if (chan == SDPCM_CONTROL_CHANNEL) { |
| 1850 | brcmf_dbg(ERROR, "(nextlen): readahead on control packet %d?\n", |
| 1851 | seq); |
| 1852 | /* Force retry w/normal header read */ |
| 1853 | bus->nextlen = 0; |
| 1854 | brcmf_sdbrcm_rxfail(bus, false, true); |
| 1855 | brcmf_sdbrcm_pktfree2(bus, pkt); |
| 1856 | continue; |
| 1857 | } |
| 1858 | |
| 1859 | /* Validate data offset */ |
| 1860 | if ((doff < SDPCM_HDRLEN) || (doff > len)) { |
| 1861 | brcmf_dbg(ERROR, "(nextlen): bad data offset %d: HW len %d min %d\n", |
| 1862 | doff, len, SDPCM_HDRLEN); |
| 1863 | brcmf_sdbrcm_rxfail(bus, false, false); |
| 1864 | brcmf_sdbrcm_pktfree2(bus, pkt); |
| 1865 | continue; |
| 1866 | } |
| 1867 | |
| 1868 | /* All done with this one -- now deliver the packet */ |
| 1869 | goto deliver; |
| 1870 | } |
| 1871 | |
| 1872 | /* Read frame header (hardware and software) */ |
| 1873 | sdret = brcmf_sdcard_recv_buf(bus->sdiodev, bus->sdiodev->sbwad, |
| 1874 | SDIO_FUNC_2, F2SYNC, bus->rxhdr, |
| 1875 | BRCMF_FIRSTREAD, NULL); |
| 1876 | bus->f2rxhdrs++; |
| 1877 | |
| 1878 | if (sdret < 0) { |
| 1879 | brcmf_dbg(ERROR, "RXHEADER FAILED: %d\n", sdret); |
| 1880 | bus->rx_hdrfail++; |
| 1881 | brcmf_sdbrcm_rxfail(bus, true, true); |
| 1882 | continue; |
| 1883 | } |
| 1884 | #ifdef BCMDBG |
| 1885 | if (BRCMF_BYTES_ON() || BRCMF_HDRS_ON()) { |
| 1886 | printk(KERN_DEBUG "RxHdr:\n"); |
| 1887 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 1888 | bus->rxhdr, SDPCM_HDRLEN); |
| 1889 | } |
| 1890 | #endif |
| 1891 | |
| 1892 | /* Extract hardware header fields */ |
| 1893 | len = get_unaligned_le16(bus->rxhdr); |
| 1894 | check = get_unaligned_le16(bus->rxhdr + sizeof(u16)); |
| 1895 | |
| 1896 | /* All zeros means no more frames */ |
| 1897 | if (!(len | check)) { |
| 1898 | *finished = true; |
| 1899 | break; |
| 1900 | } |
| 1901 | |
| 1902 | /* Validate check bytes */ |
| 1903 | if ((u16) ~(len ^ check)) { |
| 1904 | brcmf_dbg(ERROR, "HW hdr err: len/check 0x%04x/0x%04x\n", |
| 1905 | len, check); |
| 1906 | bus->rx_badhdr++; |
| 1907 | brcmf_sdbrcm_rxfail(bus, false, false); |
| 1908 | continue; |
| 1909 | } |
| 1910 | |
| 1911 | /* Validate frame length */ |
| 1912 | if (len < SDPCM_HDRLEN) { |
| 1913 | brcmf_dbg(ERROR, "HW hdr length invalid: %d\n", len); |
| 1914 | continue; |
| 1915 | } |
| 1916 | |
| 1917 | /* Extract software header fields */ |
| 1918 | chan = SDPCM_PACKET_CHANNEL(&bus->rxhdr[SDPCM_FRAMETAG_LEN]); |
| 1919 | seq = SDPCM_PACKET_SEQUENCE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]); |
| 1920 | doff = SDPCM_DOFFSET_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]); |
| 1921 | txmax = SDPCM_WINDOW_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]); |
| 1922 | |
| 1923 | /* Validate data offset */ |
| 1924 | if ((doff < SDPCM_HDRLEN) || (doff > len)) { |
| 1925 | brcmf_dbg(ERROR, "Bad data offset %d: HW len %d, min %d seq %d\n", |
| 1926 | doff, len, SDPCM_HDRLEN, seq); |
| 1927 | bus->rx_badhdr++; |
| 1928 | brcmf_sdbrcm_rxfail(bus, false, false); |
| 1929 | continue; |
| 1930 | } |
| 1931 | |
| 1932 | /* Save the readahead length if there is one */ |
| 1933 | bus->nextlen = |
| 1934 | bus->rxhdr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET]; |
| 1935 | if ((bus->nextlen << 4) > MAX_RX_DATASZ) { |
| 1936 | brcmf_dbg(INFO, "(nextlen): got frame w/nextlen too large (%d), seq %d\n", |
| 1937 | bus->nextlen, seq); |
| 1938 | bus->nextlen = 0; |
| 1939 | } |
| 1940 | |
| 1941 | /* Handle Flow Control */ |
| 1942 | fcbits = SDPCM_FCMASK_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]); |
| 1943 | |
| 1944 | if (bus->flowcontrol != fcbits) { |
| 1945 | if (~bus->flowcontrol & fcbits) |
| 1946 | bus->fc_xoff++; |
| 1947 | |
| 1948 | if (bus->flowcontrol & ~fcbits) |
| 1949 | bus->fc_xon++; |
| 1950 | |
| 1951 | bus->fc_rcvd++; |
| 1952 | bus->flowcontrol = fcbits; |
| 1953 | } |
| 1954 | |
| 1955 | /* Check and update sequence number */ |
| 1956 | if (rxseq != seq) { |
| 1957 | brcmf_dbg(INFO, "rx_seq %d, expected %d\n", seq, rxseq); |
| 1958 | bus->rx_badseq++; |
| 1959 | rxseq = seq; |
| 1960 | } |
| 1961 | |
| 1962 | /* Check window for sanity */ |
| 1963 | if ((u8) (txmax - bus->tx_seq) > 0x40) { |
| 1964 | brcmf_dbg(ERROR, "unlikely tx max %d with tx_seq %d\n", |
| 1965 | txmax, bus->tx_seq); |
| 1966 | txmax = bus->tx_seq + 2; |
| 1967 | } |
| 1968 | bus->tx_max = txmax; |
| 1969 | |
| 1970 | /* Call a separate function for control frames */ |
| 1971 | if (chan == SDPCM_CONTROL_CHANNEL) { |
| 1972 | brcmf_sdbrcm_read_control(bus, bus->rxhdr, len, doff); |
| 1973 | continue; |
| 1974 | } |
| 1975 | |
| 1976 | /* precondition: chan is either SDPCM_DATA_CHANNEL, |
| 1977 | SDPCM_EVENT_CHANNEL, SDPCM_TEST_CHANNEL or |
| 1978 | SDPCM_GLOM_CHANNEL */ |
| 1979 | |
| 1980 | /* Length to read */ |
| 1981 | rdlen = (len > BRCMF_FIRSTREAD) ? (len - BRCMF_FIRSTREAD) : 0; |
| 1982 | |
| 1983 | /* May pad read to blocksize for efficiency */ |
| 1984 | if (bus->roundup && bus->blocksize && |
| 1985 | (rdlen > bus->blocksize)) { |
| 1986 | pad = bus->blocksize - (rdlen % bus->blocksize); |
| 1987 | if ((pad <= bus->roundup) && (pad < bus->blocksize) && |
| 1988 | ((rdlen + pad + BRCMF_FIRSTREAD) < MAX_RX_DATASZ)) |
| 1989 | rdlen += pad; |
| 1990 | } else if (rdlen % BRCMF_SDALIGN) { |
| 1991 | rdlen += BRCMF_SDALIGN - (rdlen % BRCMF_SDALIGN); |
| 1992 | } |
| 1993 | |
| 1994 | /* Satisfy length-alignment requirements */ |
| 1995 | if (rdlen & (ALIGNMENT - 1)) |
| 1996 | rdlen = roundup(rdlen, ALIGNMENT); |
| 1997 | |
| 1998 | if ((rdlen + BRCMF_FIRSTREAD) > MAX_RX_DATASZ) { |
| 1999 | /* Too long -- skip this frame */ |
| 2000 | brcmf_dbg(ERROR, "too long: len %d rdlen %d\n", |
| 2001 | len, rdlen); |
| 2002 | bus->drvr->rx_errors++; |
| 2003 | bus->rx_toolong++; |
| 2004 | brcmf_sdbrcm_rxfail(bus, false, false); |
| 2005 | continue; |
| 2006 | } |
| 2007 | |
| 2008 | pkt = brcmu_pkt_buf_get_skb(rdlen + |
| 2009 | BRCMF_FIRSTREAD + BRCMF_SDALIGN); |
| 2010 | if (!pkt) { |
| 2011 | /* Give up on data, request rtx of events */ |
| 2012 | brcmf_dbg(ERROR, "brcmu_pkt_buf_get_skb failed: rdlen %d chan %d\n", |
| 2013 | rdlen, chan); |
| 2014 | bus->drvr->rx_dropped++; |
| 2015 | brcmf_sdbrcm_rxfail(bus, false, RETRYCHAN(chan)); |
| 2016 | continue; |
| 2017 | } |
| 2018 | |
| 2019 | /* Leave room for what we already read, and align remainder */ |
| 2020 | skb_pull(pkt, BRCMF_FIRSTREAD); |
| 2021 | pkt_align(pkt, rdlen, BRCMF_SDALIGN); |
| 2022 | |
| 2023 | /* Read the remaining frame data */ |
| 2024 | sdret = brcmf_sdcard_recv_buf(bus->sdiodev, bus->sdiodev->sbwad, |
| 2025 | SDIO_FUNC_2, F2SYNC, ((u8 *) (pkt->data)), |
| 2026 | rdlen, pkt); |
| 2027 | bus->f2rxdata++; |
| 2028 | |
| 2029 | if (sdret < 0) { |
| 2030 | brcmf_dbg(ERROR, "read %d %s bytes failed: %d\n", rdlen, |
| 2031 | ((chan == SDPCM_EVENT_CHANNEL) ? "event" |
| 2032 | : ((chan == SDPCM_DATA_CHANNEL) ? "data" |
| 2033 | : "test")), sdret); |
| 2034 | brcmu_pkt_buf_free_skb(pkt); |
| 2035 | bus->drvr->rx_errors++; |
| 2036 | brcmf_sdbrcm_rxfail(bus, true, RETRYCHAN(chan)); |
| 2037 | continue; |
| 2038 | } |
| 2039 | |
| 2040 | /* Copy the already-read portion */ |
| 2041 | skb_push(pkt, BRCMF_FIRSTREAD); |
| 2042 | memcpy(pkt->data, bus->rxhdr, BRCMF_FIRSTREAD); |
| 2043 | |
| 2044 | #ifdef BCMDBG |
| 2045 | if (BRCMF_BYTES_ON() && BRCMF_DATA_ON()) { |
| 2046 | printk(KERN_DEBUG "Rx Data:\n"); |
| 2047 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 2048 | pkt->data, len); |
| 2049 | } |
| 2050 | #endif |
| 2051 | |
| 2052 | deliver: |
| 2053 | /* Save superframe descriptor and allocate packet frame */ |
| 2054 | if (chan == SDPCM_GLOM_CHANNEL) { |
| 2055 | if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_FRAMETAG_LEN])) { |
| 2056 | brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n", |
| 2057 | len); |
| 2058 | #ifdef BCMDBG |
| 2059 | if (BRCMF_GLOM_ON()) { |
| 2060 | printk(KERN_DEBUG "Glom Data:\n"); |
| 2061 | print_hex_dump_bytes("", |
| 2062 | DUMP_PREFIX_OFFSET, |
| 2063 | pkt->data, len); |
| 2064 | } |
| 2065 | #endif |
| 2066 | __skb_trim(pkt, len); |
| 2067 | skb_pull(pkt, SDPCM_HDRLEN); |
| 2068 | bus->glomd = pkt; |
| 2069 | } else { |
| 2070 | brcmf_dbg(ERROR, "%s: glom superframe w/o " |
| 2071 | "descriptor!\n", __func__); |
| 2072 | brcmf_sdbrcm_rxfail(bus, false, false); |
| 2073 | } |
| 2074 | continue; |
| 2075 | } |
| 2076 | |
| 2077 | /* Fill in packet len and prio, deliver upward */ |
| 2078 | __skb_trim(pkt, len); |
| 2079 | skb_pull(pkt, doff); |
| 2080 | |
| 2081 | if (pkt->len == 0) { |
| 2082 | brcmu_pkt_buf_free_skb(pkt); |
| 2083 | continue; |
| 2084 | } else if (brcmf_proto_hdrpull(bus->drvr, &ifidx, pkt) != 0) { |
| 2085 | brcmf_dbg(ERROR, "rx protocol error\n"); |
| 2086 | brcmu_pkt_buf_free_skb(pkt); |
| 2087 | bus->drvr->rx_errors++; |
| 2088 | continue; |
| 2089 | } |
| 2090 | |
| 2091 | /* Unlock during rx call */ |
| 2092 | up(&bus->sdsem); |
| 2093 | brcmf_rx_frame(bus->drvr, ifidx, pkt, 1); |
| 2094 | down(&bus->sdsem); |
| 2095 | } |
| 2096 | rxcount = maxframes - rxleft; |
| 2097 | #ifdef BCMDBG |
| 2098 | /* Message if we hit the limit */ |
| 2099 | if (!rxleft) |
| 2100 | brcmf_dbg(DATA, "hit rx limit of %d frames\n", |
| 2101 | maxframes); |
| 2102 | else |
| 2103 | #endif /* BCMDBG */ |
| 2104 | brcmf_dbg(DATA, "processed %d frames\n", rxcount); |
| 2105 | /* Back off rxseq if awaiting rtx, update rx_seq */ |
| 2106 | if (bus->rxskip) |
| 2107 | rxseq--; |
| 2108 | bus->rx_seq = rxseq; |
| 2109 | |
| 2110 | return rxcount; |
| 2111 | } |
| 2112 | |
| 2113 | static int |
| 2114 | brcmf_sdbrcm_send_buf(struct brcmf_bus *bus, u32 addr, uint fn, uint flags, |
| 2115 | u8 *buf, uint nbytes, struct sk_buff *pkt) |
| 2116 | { |
| 2117 | return brcmf_sdcard_send_buf |
| 2118 | (bus->sdiodev, addr, fn, flags, buf, nbytes, pkt); |
| 2119 | } |
| 2120 | |
| 2121 | static void |
| 2122 | brcmf_sdbrcm_wait_for_event(struct brcmf_bus *bus, bool *lockvar) |
| 2123 | { |
| 2124 | up(&bus->sdsem); |
| 2125 | wait_event_interruptible_timeout(bus->ctrl_wait, |
| 2126 | (*lockvar == false), HZ * 2); |
| 2127 | down(&bus->sdsem); |
| 2128 | return; |
| 2129 | } |
| 2130 | |
| 2131 | static void |
| 2132 | brcmf_sdbrcm_wait_event_wakeup(struct brcmf_bus *bus) |
| 2133 | { |
| 2134 | if (waitqueue_active(&bus->ctrl_wait)) |
| 2135 | wake_up_interruptible(&bus->ctrl_wait); |
| 2136 | return; |
| 2137 | } |
| 2138 | |
| 2139 | /* Writes a HW/SW header into the packet and sends it. */ |
| 2140 | /* Assumes: (a) header space already there, (b) caller holds lock */ |
| 2141 | static int brcmf_sdbrcm_txpkt(struct brcmf_bus *bus, struct sk_buff *pkt, |
| 2142 | uint chan, bool free_pkt) |
| 2143 | { |
| 2144 | int ret; |
| 2145 | u8 *frame; |
| 2146 | u16 len, pad = 0; |
| 2147 | u32 swheader; |
| 2148 | struct sk_buff *new; |
| 2149 | int i; |
| 2150 | |
| 2151 | brcmf_dbg(TRACE, "Enter\n"); |
| 2152 | |
| 2153 | frame = (u8 *) (pkt->data); |
| 2154 | |
| 2155 | /* Add alignment padding, allocate new packet if needed */ |
| 2156 | pad = ((unsigned long)frame % BRCMF_SDALIGN); |
| 2157 | if (pad) { |
| 2158 | if (skb_headroom(pkt) < pad) { |
| 2159 | brcmf_dbg(INFO, "insufficient headroom %d for %d pad\n", |
| 2160 | skb_headroom(pkt), pad); |
| 2161 | bus->drvr->tx_realloc++; |
| 2162 | new = brcmu_pkt_buf_get_skb(pkt->len + BRCMF_SDALIGN); |
| 2163 | if (!new) { |
| 2164 | brcmf_dbg(ERROR, "couldn't allocate new %d-byte packet\n", |
| 2165 | pkt->len + BRCMF_SDALIGN); |
| 2166 | ret = -ENOMEM; |
| 2167 | goto done; |
| 2168 | } |
| 2169 | |
| 2170 | pkt_align(new, pkt->len, BRCMF_SDALIGN); |
| 2171 | memcpy(new->data, pkt->data, pkt->len); |
| 2172 | if (free_pkt) |
| 2173 | brcmu_pkt_buf_free_skb(pkt); |
| 2174 | /* free the pkt if canned one is not used */ |
| 2175 | free_pkt = true; |
| 2176 | pkt = new; |
| 2177 | frame = (u8 *) (pkt->data); |
| 2178 | /* precondition: (frame % BRCMF_SDALIGN) == 0) */ |
| 2179 | pad = 0; |
| 2180 | } else { |
| 2181 | skb_push(pkt, pad); |
| 2182 | frame = (u8 *) (pkt->data); |
| 2183 | /* precondition: pad + SDPCM_HDRLEN <= pkt->len */ |
| 2184 | memset(frame, 0, pad + SDPCM_HDRLEN); |
| 2185 | } |
| 2186 | } |
| 2187 | /* precondition: pad < BRCMF_SDALIGN */ |
| 2188 | |
| 2189 | /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */ |
| 2190 | len = (u16) (pkt->len); |
| 2191 | *(__le16 *) frame = cpu_to_le16(len); |
| 2192 | *(((__le16 *) frame) + 1) = cpu_to_le16(~len); |
| 2193 | |
| 2194 | /* Software tag: channel, sequence number, data offset */ |
| 2195 | swheader = |
| 2196 | ((chan << SDPCM_CHANNEL_SHIFT) & SDPCM_CHANNEL_MASK) | bus->tx_seq | |
| 2197 | (((pad + |
| 2198 | SDPCM_HDRLEN) << SDPCM_DOFFSET_SHIFT) & SDPCM_DOFFSET_MASK); |
| 2199 | |
| 2200 | put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN); |
| 2201 | put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader)); |
| 2202 | |
| 2203 | #ifdef BCMDBG |
| 2204 | tx_packets[pkt->priority]++; |
| 2205 | if (BRCMF_BYTES_ON() && |
| 2206 | (((BRCMF_CTL_ON() && (chan == SDPCM_CONTROL_CHANNEL)) || |
| 2207 | (BRCMF_DATA_ON() && (chan != SDPCM_CONTROL_CHANNEL))))) { |
| 2208 | printk(KERN_DEBUG "Tx Frame:\n"); |
| 2209 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, frame, len); |
| 2210 | } else if (BRCMF_HDRS_ON()) { |
| 2211 | printk(KERN_DEBUG "TxHdr:\n"); |
| 2212 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 2213 | frame, min_t(u16, len, 16)); |
| 2214 | } |
| 2215 | #endif |
| 2216 | |
| 2217 | /* Raise len to next SDIO block to eliminate tail command */ |
| 2218 | if (bus->roundup && bus->blocksize && (len > bus->blocksize)) { |
| 2219 | u16 pad = bus->blocksize - (len % bus->blocksize); |
| 2220 | if ((pad <= bus->roundup) && (pad < bus->blocksize)) |
| 2221 | len += pad; |
| 2222 | } else if (len % BRCMF_SDALIGN) { |
| 2223 | len += BRCMF_SDALIGN - (len % BRCMF_SDALIGN); |
| 2224 | } |
| 2225 | |
| 2226 | /* Some controllers have trouble with odd bytes -- round to even */ |
| 2227 | if (len & (ALIGNMENT - 1)) |
| 2228 | len = roundup(len, ALIGNMENT); |
| 2229 | |
| 2230 | ret = brcmf_sdbrcm_send_buf(bus, bus->sdiodev->sbwad, |
| 2231 | SDIO_FUNC_2, F2SYNC, frame, |
| 2232 | len, pkt); |
| 2233 | bus->f2txdata++; |
| 2234 | |
| 2235 | if (ret < 0) { |
| 2236 | /* On failure, abort the command and terminate the frame */ |
| 2237 | brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n", |
| 2238 | ret); |
| 2239 | bus->tx_sderrs++; |
| 2240 | |
| 2241 | brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2); |
| 2242 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 2243 | SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM, |
| 2244 | NULL); |
| 2245 | bus->f1regdata++; |
| 2246 | |
| 2247 | for (i = 0; i < 3; i++) { |
| 2248 | u8 hi, lo; |
| 2249 | hi = brcmf_sdcard_cfg_read(bus->sdiodev, |
| 2250 | SDIO_FUNC_1, |
| 2251 | SBSDIO_FUNC1_WFRAMEBCHI, |
| 2252 | NULL); |
| 2253 | lo = brcmf_sdcard_cfg_read(bus->sdiodev, |
| 2254 | SDIO_FUNC_1, |
| 2255 | SBSDIO_FUNC1_WFRAMEBCLO, |
| 2256 | NULL); |
| 2257 | bus->f1regdata += 2; |
| 2258 | if ((hi == 0) && (lo == 0)) |
| 2259 | break; |
| 2260 | } |
| 2261 | |
| 2262 | } |
| 2263 | if (ret == 0) |
| 2264 | bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP; |
| 2265 | |
| 2266 | done: |
| 2267 | /* restore pkt buffer pointer before calling tx complete routine */ |
| 2268 | skb_pull(pkt, SDPCM_HDRLEN + pad); |
| 2269 | up(&bus->sdsem); |
| 2270 | brcmf_txcomplete(bus->drvr, pkt, ret != 0); |
| 2271 | down(&bus->sdsem); |
| 2272 | |
| 2273 | if (free_pkt) |
| 2274 | brcmu_pkt_buf_free_skb(pkt); |
| 2275 | |
| 2276 | return ret; |
| 2277 | } |
| 2278 | |
| 2279 | static uint brcmf_sdbrcm_sendfromq(struct brcmf_bus *bus, uint maxframes) |
| 2280 | { |
| 2281 | struct sk_buff *pkt; |
| 2282 | u32 intstatus = 0; |
| 2283 | uint retries = 0; |
| 2284 | int ret = 0, prec_out; |
| 2285 | uint cnt = 0; |
| 2286 | uint datalen; |
| 2287 | u8 tx_prec_map; |
| 2288 | |
| 2289 | struct brcmf_pub *drvr = bus->drvr; |
| 2290 | |
| 2291 | brcmf_dbg(TRACE, "Enter\n"); |
| 2292 | |
| 2293 | tx_prec_map = ~bus->flowcontrol; |
| 2294 | |
| 2295 | /* Send frames until the limit or some other event */ |
| 2296 | for (cnt = 0; (cnt < maxframes) && data_ok(bus); cnt++) { |
| 2297 | spin_lock_bh(&bus->txqlock); |
| 2298 | pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map, &prec_out); |
| 2299 | if (pkt == NULL) { |
| 2300 | spin_unlock_bh(&bus->txqlock); |
| 2301 | break; |
| 2302 | } |
| 2303 | spin_unlock_bh(&bus->txqlock); |
| 2304 | datalen = pkt->len - SDPCM_HDRLEN; |
| 2305 | |
| 2306 | ret = brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true); |
| 2307 | if (ret) |
| 2308 | bus->drvr->tx_errors++; |
| 2309 | else |
| 2310 | bus->drvr->dstats.tx_bytes += datalen; |
| 2311 | |
| 2312 | /* In poll mode, need to check for other events */ |
| 2313 | if (!bus->intr && cnt) { |
| 2314 | /* Check device status, signal pending interrupt */ |
| 2315 | r_sdreg32(bus, &intstatus, |
| 2316 | offsetof(struct sdpcmd_regs, intstatus), |
| 2317 | &retries); |
| 2318 | bus->f2txdata++; |
| 2319 | if (brcmf_sdcard_regfail(bus->sdiodev)) |
| 2320 | break; |
| 2321 | if (intstatus & bus->hostintmask) |
| 2322 | bus->ipend = true; |
| 2323 | } |
| 2324 | } |
| 2325 | |
| 2326 | /* Deflow-control stack if needed */ |
| 2327 | if (drvr->up && (drvr->busstate == BRCMF_BUS_DATA) && |
| 2328 | drvr->txoff && (pktq_len(&bus->txq) < TXLOW)) |
| 2329 | brcmf_txflowcontrol(drvr, 0, OFF); |
| 2330 | |
| 2331 | return cnt; |
| 2332 | } |
| 2333 | |
| 2334 | static bool brcmf_sdbrcm_dpc(struct brcmf_bus *bus) |
| 2335 | { |
| 2336 | u32 intstatus, newstatus = 0; |
| 2337 | uint retries = 0; |
| 2338 | uint rxlimit = bus->rxbound; /* Rx frames to read before resched */ |
| 2339 | uint txlimit = bus->txbound; /* Tx frames to send before resched */ |
| 2340 | uint framecnt = 0; /* Temporary counter of tx/rx frames */ |
| 2341 | bool rxdone = true; /* Flag for no more read data */ |
| 2342 | bool resched = false; /* Flag indicating resched wanted */ |
| 2343 | |
| 2344 | brcmf_dbg(TRACE, "Enter\n"); |
| 2345 | |
| 2346 | /* Start with leftover status bits */ |
| 2347 | intstatus = bus->intstatus; |
| 2348 | |
| 2349 | down(&bus->sdsem); |
| 2350 | |
| 2351 | /* If waiting for HTAVAIL, check status */ |
| 2352 | if (bus->clkstate == CLK_PENDING) { |
| 2353 | int err; |
| 2354 | u8 clkctl, devctl = 0; |
| 2355 | |
| 2356 | #ifdef BCMDBG |
| 2357 | /* Check for inconsistent device control */ |
| 2358 | devctl = brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 2359 | SBSDIO_DEVICE_CTL, &err); |
| 2360 | if (err) { |
| 2361 | brcmf_dbg(ERROR, "error reading DEVCTL: %d\n", err); |
| 2362 | bus->drvr->busstate = BRCMF_BUS_DOWN; |
| 2363 | } |
| 2364 | #endif /* BCMDBG */ |
| 2365 | |
| 2366 | /* Read CSR, if clock on switch to AVAIL, else ignore */ |
| 2367 | clkctl = brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 2368 | SBSDIO_FUNC1_CHIPCLKCSR, &err); |
| 2369 | if (err) { |
| 2370 | brcmf_dbg(ERROR, "error reading CSR: %d\n", |
| 2371 | err); |
| 2372 | bus->drvr->busstate = BRCMF_BUS_DOWN; |
| 2373 | } |
| 2374 | |
| 2375 | brcmf_dbg(INFO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n", |
| 2376 | devctl, clkctl); |
| 2377 | |
| 2378 | if (SBSDIO_HTAV(clkctl)) { |
| 2379 | devctl = brcmf_sdcard_cfg_read(bus->sdiodev, |
| 2380 | SDIO_FUNC_1, |
| 2381 | SBSDIO_DEVICE_CTL, &err); |
| 2382 | if (err) { |
| 2383 | brcmf_dbg(ERROR, "error reading DEVCTL: %d\n", |
| 2384 | err); |
| 2385 | bus->drvr->busstate = BRCMF_BUS_DOWN; |
| 2386 | } |
| 2387 | devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY; |
| 2388 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 2389 | SBSDIO_DEVICE_CTL, devctl, &err); |
| 2390 | if (err) { |
| 2391 | brcmf_dbg(ERROR, "error writing DEVCTL: %d\n", |
| 2392 | err); |
| 2393 | bus->drvr->busstate = BRCMF_BUS_DOWN; |
| 2394 | } |
| 2395 | bus->clkstate = CLK_AVAIL; |
| 2396 | } else { |
| 2397 | goto clkwait; |
| 2398 | } |
| 2399 | } |
| 2400 | |
| 2401 | bus_wake(bus); |
| 2402 | |
| 2403 | /* Make sure backplane clock is on */ |
| 2404 | brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, true); |
| 2405 | if (bus->clkstate == CLK_PENDING) |
| 2406 | goto clkwait; |
| 2407 | |
| 2408 | /* Pending interrupt indicates new device status */ |
| 2409 | if (bus->ipend) { |
| 2410 | bus->ipend = false; |
| 2411 | r_sdreg32(bus, &newstatus, |
| 2412 | offsetof(struct sdpcmd_regs, intstatus), &retries); |
| 2413 | bus->f1regdata++; |
| 2414 | if (brcmf_sdcard_regfail(bus->sdiodev)) |
| 2415 | newstatus = 0; |
| 2416 | newstatus &= bus->hostintmask; |
| 2417 | bus->fcstate = !!(newstatus & I_HMB_FC_STATE); |
| 2418 | if (newstatus) { |
| 2419 | w_sdreg32(bus, newstatus, |
| 2420 | offsetof(struct sdpcmd_regs, intstatus), |
| 2421 | &retries); |
| 2422 | bus->f1regdata++; |
| 2423 | } |
| 2424 | } |
| 2425 | |
| 2426 | /* Merge new bits with previous */ |
| 2427 | intstatus |= newstatus; |
| 2428 | bus->intstatus = 0; |
| 2429 | |
| 2430 | /* Handle flow-control change: read new state in case our ack |
| 2431 | * crossed another change interrupt. If change still set, assume |
| 2432 | * FC ON for safety, let next loop through do the debounce. |
| 2433 | */ |
| 2434 | if (intstatus & I_HMB_FC_CHANGE) { |
| 2435 | intstatus &= ~I_HMB_FC_CHANGE; |
| 2436 | w_sdreg32(bus, I_HMB_FC_CHANGE, |
| 2437 | offsetof(struct sdpcmd_regs, intstatus), &retries); |
| 2438 | |
| 2439 | r_sdreg32(bus, &newstatus, |
| 2440 | offsetof(struct sdpcmd_regs, intstatus), &retries); |
| 2441 | bus->f1regdata += 2; |
| 2442 | bus->fcstate = |
| 2443 | !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)); |
| 2444 | intstatus |= (newstatus & bus->hostintmask); |
| 2445 | } |
| 2446 | |
| 2447 | /* Handle host mailbox indication */ |
| 2448 | if (intstatus & I_HMB_HOST_INT) { |
| 2449 | intstatus &= ~I_HMB_HOST_INT; |
| 2450 | intstatus |= brcmf_sdbrcm_hostmail(bus); |
| 2451 | } |
| 2452 | |
| 2453 | /* Generally don't ask for these, can get CRC errors... */ |
| 2454 | if (intstatus & I_WR_OOSYNC) { |
| 2455 | brcmf_dbg(ERROR, "Dongle reports WR_OOSYNC\n"); |
| 2456 | intstatus &= ~I_WR_OOSYNC; |
| 2457 | } |
| 2458 | |
| 2459 | if (intstatus & I_RD_OOSYNC) { |
| 2460 | brcmf_dbg(ERROR, "Dongle reports RD_OOSYNC\n"); |
| 2461 | intstatus &= ~I_RD_OOSYNC; |
| 2462 | } |
| 2463 | |
| 2464 | if (intstatus & I_SBINT) { |
| 2465 | brcmf_dbg(ERROR, "Dongle reports SBINT\n"); |
| 2466 | intstatus &= ~I_SBINT; |
| 2467 | } |
| 2468 | |
| 2469 | /* Would be active due to wake-wlan in gSPI */ |
| 2470 | if (intstatus & I_CHIPACTIVE) { |
| 2471 | brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n"); |
| 2472 | intstatus &= ~I_CHIPACTIVE; |
| 2473 | } |
| 2474 | |
| 2475 | /* Ignore frame indications if rxskip is set */ |
| 2476 | if (bus->rxskip) |
| 2477 | intstatus &= ~I_HMB_FRAME_IND; |
| 2478 | |
| 2479 | /* On frame indication, read available frames */ |
| 2480 | if (PKT_AVAILABLE()) { |
| 2481 | framecnt = brcmf_sdbrcm_readframes(bus, rxlimit, &rxdone); |
| 2482 | if (rxdone || bus->rxskip) |
| 2483 | intstatus &= ~I_HMB_FRAME_IND; |
| 2484 | rxlimit -= min(framecnt, rxlimit); |
| 2485 | } |
| 2486 | |
| 2487 | /* Keep still-pending events for next scheduling */ |
| 2488 | bus->intstatus = intstatus; |
| 2489 | |
| 2490 | clkwait: |
| 2491 | if (data_ok(bus) && bus->ctrl_frame_stat && |
| 2492 | (bus->clkstate == CLK_AVAIL)) { |
| 2493 | int ret, i; |
| 2494 | |
| 2495 | ret = brcmf_sdbrcm_send_buf(bus, bus->sdiodev->sbwad, |
| 2496 | SDIO_FUNC_2, F2SYNC, (u8 *) bus->ctrl_frame_buf, |
| 2497 | (u32) bus->ctrl_frame_len, NULL); |
| 2498 | |
| 2499 | if (ret < 0) { |
| 2500 | /* On failure, abort the command and |
| 2501 | terminate the frame */ |
| 2502 | brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n", |
| 2503 | ret); |
| 2504 | bus->tx_sderrs++; |
| 2505 | |
| 2506 | brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2); |
| 2507 | |
| 2508 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 2509 | SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM, |
| 2510 | NULL); |
| 2511 | bus->f1regdata++; |
| 2512 | |
| 2513 | for (i = 0; i < 3; i++) { |
| 2514 | u8 hi, lo; |
| 2515 | hi = brcmf_sdcard_cfg_read(bus->sdiodev, |
| 2516 | SDIO_FUNC_1, |
| 2517 | SBSDIO_FUNC1_WFRAMEBCHI, |
| 2518 | NULL); |
| 2519 | lo = brcmf_sdcard_cfg_read(bus->sdiodev, |
| 2520 | SDIO_FUNC_1, |
| 2521 | SBSDIO_FUNC1_WFRAMEBCLO, |
| 2522 | NULL); |
| 2523 | bus->f1regdata += 2; |
| 2524 | if ((hi == 0) && (lo == 0)) |
| 2525 | break; |
| 2526 | } |
| 2527 | |
| 2528 | } |
| 2529 | if (ret == 0) |
| 2530 | bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP; |
| 2531 | |
| 2532 | brcmf_dbg(INFO, "Return_dpc value is : %d\n", ret); |
| 2533 | bus->ctrl_frame_stat = false; |
| 2534 | brcmf_sdbrcm_wait_event_wakeup(bus); |
| 2535 | } |
| 2536 | /* Send queued frames (limit 1 if rx may still be pending) */ |
| 2537 | else if ((bus->clkstate == CLK_AVAIL) && !bus->fcstate && |
| 2538 | brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit |
| 2539 | && data_ok(bus)) { |
| 2540 | framecnt = rxdone ? txlimit : min(txlimit, bus->txminmax); |
| 2541 | framecnt = brcmf_sdbrcm_sendfromq(bus, framecnt); |
| 2542 | txlimit -= framecnt; |
| 2543 | } |
| 2544 | |
| 2545 | /* Resched if events or tx frames are pending, |
| 2546 | else await next interrupt */ |
| 2547 | /* On failed register access, all bets are off: |
| 2548 | no resched or interrupts */ |
| 2549 | if ((bus->drvr->busstate == BRCMF_BUS_DOWN) || |
| 2550 | brcmf_sdcard_regfail(bus->sdiodev)) { |
| 2551 | brcmf_dbg(ERROR, "failed backplane access over SDIO, halting operation %d\n", |
| 2552 | brcmf_sdcard_regfail(bus->sdiodev)); |
| 2553 | bus->drvr->busstate = BRCMF_BUS_DOWN; |
| 2554 | bus->intstatus = 0; |
| 2555 | } else if (bus->clkstate == CLK_PENDING) { |
| 2556 | brcmf_dbg(INFO, "rescheduled due to CLK_PENDING awaiting I_CHIPACTIVE interrupt\n"); |
| 2557 | resched = true; |
| 2558 | } else if (bus->intstatus || bus->ipend || |
| 2559 | (!bus->fcstate && brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) |
| 2560 | && data_ok(bus)) || PKT_AVAILABLE()) { |
| 2561 | resched = true; |
| 2562 | } |
| 2563 | |
| 2564 | bus->dpc_sched = resched; |
| 2565 | |
| 2566 | /* If we're done for now, turn off clock request. */ |
| 2567 | if ((bus->clkstate != CLK_PENDING) |
| 2568 | && bus->idletime == BRCMF_IDLE_IMMEDIATE) { |
| 2569 | bus->activity = false; |
| 2570 | brcmf_sdbrcm_clkctl(bus, CLK_NONE, false); |
| 2571 | } |
| 2572 | |
| 2573 | up(&bus->sdsem); |
| 2574 | |
| 2575 | return resched; |
| 2576 | } |
| 2577 | |
| 2578 | static int brcmf_sdbrcm_dpc_thread(void *data) |
| 2579 | { |
| 2580 | struct brcmf_bus *bus = (struct brcmf_bus *) data; |
| 2581 | |
| 2582 | allow_signal(SIGTERM); |
| 2583 | /* Run until signal received */ |
| 2584 | while (1) { |
| 2585 | if (kthread_should_stop()) |
| 2586 | break; |
| 2587 | if (!wait_for_completion_interruptible(&bus->dpc_wait)) { |
| 2588 | /* Call bus dpc unless it indicated down |
| 2589 | (then clean stop) */ |
| 2590 | if (bus->drvr->busstate != BRCMF_BUS_DOWN) { |
| 2591 | if (brcmf_sdbrcm_dpc(bus)) |
| 2592 | complete(&bus->dpc_wait); |
| 2593 | } else { |
| 2594 | /* after stopping the bus, exit thread */ |
| 2595 | brcmf_sdbrcm_bus_stop(bus); |
| 2596 | bus->dpc_tsk = NULL; |
| 2597 | break; |
| 2598 | } |
| 2599 | } else |
| 2600 | break; |
| 2601 | } |
| 2602 | return 0; |
| 2603 | } |
| 2604 | |
| 2605 | int brcmf_sdbrcm_bus_txdata(struct brcmf_bus *bus, struct sk_buff *pkt) |
| 2606 | { |
| 2607 | int ret = -EBADE; |
| 2608 | uint datalen, prec; |
| 2609 | |
| 2610 | brcmf_dbg(TRACE, "Enter\n"); |
| 2611 | |
| 2612 | datalen = pkt->len; |
| 2613 | |
| 2614 | /* Add space for the header */ |
| 2615 | skb_push(pkt, SDPCM_HDRLEN); |
| 2616 | /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */ |
| 2617 | |
| 2618 | prec = prio2prec((pkt->priority & PRIOMASK)); |
| 2619 | |
| 2620 | /* Check for existing queue, current flow-control, |
| 2621 | pending event, or pending clock */ |
| 2622 | brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq)); |
| 2623 | bus->fcqueued++; |
| 2624 | |
| 2625 | /* Priority based enq */ |
| 2626 | spin_lock_bh(&bus->txqlock); |
| 2627 | if (brcmf_c_prec_enq(bus->drvr, &bus->txq, pkt, prec) == false) { |
| 2628 | skb_pull(pkt, SDPCM_HDRLEN); |
| 2629 | brcmf_txcomplete(bus->drvr, pkt, false); |
| 2630 | brcmu_pkt_buf_free_skb(pkt); |
| 2631 | brcmf_dbg(ERROR, "out of bus->txq !!!\n"); |
| 2632 | ret = -ENOSR; |
| 2633 | } else { |
| 2634 | ret = 0; |
| 2635 | } |
| 2636 | spin_unlock_bh(&bus->txqlock); |
| 2637 | |
| 2638 | if (pktq_len(&bus->txq) >= TXHI) |
| 2639 | brcmf_txflowcontrol(bus->drvr, 0, ON); |
| 2640 | |
| 2641 | #ifdef BCMDBG |
| 2642 | if (pktq_plen(&bus->txq, prec) > qcount[prec]) |
| 2643 | qcount[prec] = pktq_plen(&bus->txq, prec); |
| 2644 | #endif |
| 2645 | /* Schedule DPC if needed to send queued packet(s) */ |
| 2646 | if (!bus->dpc_sched) { |
| 2647 | bus->dpc_sched = true; |
| 2648 | if (bus->dpc_tsk) |
| 2649 | complete(&bus->dpc_wait); |
| 2650 | } |
| 2651 | |
| 2652 | return ret; |
| 2653 | } |
| 2654 | |
| 2655 | static int |
| 2656 | brcmf_sdbrcm_membytes(struct brcmf_bus *bus, bool write, u32 address, u8 *data, |
| 2657 | uint size) |
| 2658 | { |
| 2659 | int bcmerror = 0; |
| 2660 | u32 sdaddr; |
| 2661 | uint dsize; |
| 2662 | |
| 2663 | /* Determine initial transfer parameters */ |
| 2664 | sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK; |
| 2665 | if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK) |
| 2666 | dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr); |
| 2667 | else |
| 2668 | dsize = size; |
| 2669 | |
| 2670 | /* Set the backplane window to include the start address */ |
| 2671 | bcmerror = brcmf_sdcard_set_sbaddr_window(bus->sdiodev, address); |
| 2672 | if (bcmerror) { |
| 2673 | brcmf_dbg(ERROR, "window change failed\n"); |
| 2674 | goto xfer_done; |
| 2675 | } |
| 2676 | |
| 2677 | /* Do the transfer(s) */ |
| 2678 | while (size) { |
| 2679 | brcmf_dbg(INFO, "%s %d bytes at offset 0x%08x in window 0x%08x\n", |
| 2680 | write ? "write" : "read", dsize, |
| 2681 | sdaddr, address & SBSDIO_SBWINDOW_MASK); |
| 2682 | bcmerror = brcmf_sdcard_rwdata(bus->sdiodev, write, |
| 2683 | sdaddr, data, dsize); |
| 2684 | if (bcmerror) { |
| 2685 | brcmf_dbg(ERROR, "membytes transfer failed\n"); |
| 2686 | break; |
| 2687 | } |
| 2688 | |
| 2689 | /* Adjust for next transfer (if any) */ |
| 2690 | size -= dsize; |
| 2691 | if (size) { |
| 2692 | data += dsize; |
| 2693 | address += dsize; |
| 2694 | bcmerror = brcmf_sdcard_set_sbaddr_window(bus->sdiodev, |
| 2695 | address); |
| 2696 | if (bcmerror) { |
| 2697 | brcmf_dbg(ERROR, "window change failed\n"); |
| 2698 | break; |
| 2699 | } |
| 2700 | sdaddr = 0; |
| 2701 | dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size); |
| 2702 | } |
| 2703 | } |
| 2704 | |
| 2705 | xfer_done: |
| 2706 | /* Return the window to backplane enumeration space for core access */ |
| 2707 | if (brcmf_sdcard_set_sbaddr_window(bus->sdiodev, bus->sdiodev->sbwad)) |
| 2708 | brcmf_dbg(ERROR, "FAILED to set window back to 0x%x\n", |
| 2709 | bus->sdiodev->sbwad); |
| 2710 | |
| 2711 | return bcmerror; |
| 2712 | } |
| 2713 | |
| 2714 | #ifdef BCMDBG |
| 2715 | #define CONSOLE_LINE_MAX 192 |
| 2716 | |
| 2717 | static int brcmf_sdbrcm_readconsole(struct brcmf_bus *bus) |
| 2718 | { |
| 2719 | struct brcmf_console *c = &bus->console; |
| 2720 | u8 line[CONSOLE_LINE_MAX], ch; |
| 2721 | u32 n, idx, addr; |
| 2722 | int rv; |
| 2723 | |
| 2724 | /* Don't do anything until FWREADY updates console address */ |
| 2725 | if (bus->console_addr == 0) |
| 2726 | return 0; |
| 2727 | |
| 2728 | /* Read console log struct */ |
| 2729 | addr = bus->console_addr + offsetof(struct rte_console, log_le); |
| 2730 | rv = brcmf_sdbrcm_membytes(bus, false, addr, (u8 *)&c->log_le, |
| 2731 | sizeof(c->log_le)); |
| 2732 | if (rv < 0) |
| 2733 | return rv; |
| 2734 | |
| 2735 | /* Allocate console buffer (one time only) */ |
| 2736 | if (c->buf == NULL) { |
| 2737 | c->bufsize = le32_to_cpu(c->log_le.buf_size); |
| 2738 | c->buf = kmalloc(c->bufsize, GFP_ATOMIC); |
| 2739 | if (c->buf == NULL) |
| 2740 | return -ENOMEM; |
| 2741 | } |
| 2742 | |
| 2743 | idx = le32_to_cpu(c->log_le.idx); |
| 2744 | |
| 2745 | /* Protect against corrupt value */ |
| 2746 | if (idx > c->bufsize) |
| 2747 | return -EBADE; |
| 2748 | |
| 2749 | /* Skip reading the console buffer if the index pointer |
| 2750 | has not moved */ |
| 2751 | if (idx == c->last) |
| 2752 | return 0; |
| 2753 | |
| 2754 | /* Read the console buffer */ |
| 2755 | addr = le32_to_cpu(c->log_le.buf); |
| 2756 | rv = brcmf_sdbrcm_membytes(bus, false, addr, c->buf, c->bufsize); |
| 2757 | if (rv < 0) |
| 2758 | return rv; |
| 2759 | |
| 2760 | while (c->last != idx) { |
| 2761 | for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) { |
| 2762 | if (c->last == idx) { |
| 2763 | /* This would output a partial line. |
| 2764 | * Instead, back up |
| 2765 | * the buffer pointer and output this |
| 2766 | * line next time around. |
| 2767 | */ |
| 2768 | if (c->last >= n) |
| 2769 | c->last -= n; |
| 2770 | else |
| 2771 | c->last = c->bufsize - n; |
| 2772 | goto break2; |
| 2773 | } |
| 2774 | ch = c->buf[c->last]; |
| 2775 | c->last = (c->last + 1) % c->bufsize; |
| 2776 | if (ch == '\n') |
| 2777 | break; |
| 2778 | line[n] = ch; |
| 2779 | } |
| 2780 | |
| 2781 | if (n > 0) { |
| 2782 | if (line[n - 1] == '\r') |
| 2783 | n--; |
| 2784 | line[n] = 0; |
| 2785 | printk(KERN_DEBUG "CONSOLE: %s\n", line); |
| 2786 | } |
| 2787 | } |
| 2788 | break2: |
| 2789 | |
| 2790 | return 0; |
| 2791 | } |
| 2792 | #endif /* BCMDBG */ |
| 2793 | |
| 2794 | static int brcmf_tx_frame(struct brcmf_bus *bus, u8 *frame, u16 len) |
| 2795 | { |
| 2796 | int i; |
| 2797 | int ret; |
| 2798 | |
| 2799 | bus->ctrl_frame_stat = false; |
| 2800 | ret = brcmf_sdbrcm_send_buf(bus, bus->sdiodev->sbwad, |
| 2801 | SDIO_FUNC_2, F2SYNC, frame, len, NULL); |
| 2802 | |
| 2803 | if (ret < 0) { |
| 2804 | /* On failure, abort the command and terminate the frame */ |
| 2805 | brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n", |
| 2806 | ret); |
| 2807 | bus->tx_sderrs++; |
| 2808 | |
| 2809 | brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2); |
| 2810 | |
| 2811 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 2812 | SBSDIO_FUNC1_FRAMECTRL, |
| 2813 | SFC_WF_TERM, NULL); |
| 2814 | bus->f1regdata++; |
| 2815 | |
| 2816 | for (i = 0; i < 3; i++) { |
| 2817 | u8 hi, lo; |
| 2818 | hi = brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 2819 | SBSDIO_FUNC1_WFRAMEBCHI, |
| 2820 | NULL); |
| 2821 | lo = brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 2822 | SBSDIO_FUNC1_WFRAMEBCLO, |
| 2823 | NULL); |
| 2824 | bus->f1regdata += 2; |
| 2825 | if (hi == 0 && lo == 0) |
| 2826 | break; |
| 2827 | } |
| 2828 | return ret; |
| 2829 | } |
| 2830 | |
| 2831 | bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP; |
| 2832 | |
| 2833 | return ret; |
| 2834 | } |
| 2835 | |
| 2836 | int |
| 2837 | brcmf_sdbrcm_bus_txctl(struct brcmf_bus *bus, unsigned char *msg, uint msglen) |
| 2838 | { |
| 2839 | u8 *frame; |
| 2840 | u16 len; |
| 2841 | u32 swheader; |
| 2842 | uint retries = 0; |
| 2843 | u8 doff = 0; |
| 2844 | int ret = -1; |
| 2845 | |
| 2846 | brcmf_dbg(TRACE, "Enter\n"); |
| 2847 | |
| 2848 | /* Back the pointer to make a room for bus header */ |
| 2849 | frame = msg - SDPCM_HDRLEN; |
| 2850 | len = (msglen += SDPCM_HDRLEN); |
| 2851 | |
| 2852 | /* Add alignment padding (optional for ctl frames) */ |
| 2853 | doff = ((unsigned long)frame % BRCMF_SDALIGN); |
| 2854 | if (doff) { |
| 2855 | frame -= doff; |
| 2856 | len += doff; |
| 2857 | msglen += doff; |
| 2858 | memset(frame, 0, doff + SDPCM_HDRLEN); |
| 2859 | } |
| 2860 | /* precondition: doff < BRCMF_SDALIGN */ |
| 2861 | doff += SDPCM_HDRLEN; |
| 2862 | |
| 2863 | /* Round send length to next SDIO block */ |
| 2864 | if (bus->roundup && bus->blocksize && (len > bus->blocksize)) { |
| 2865 | u16 pad = bus->blocksize - (len % bus->blocksize); |
| 2866 | if ((pad <= bus->roundup) && (pad < bus->blocksize)) |
| 2867 | len += pad; |
| 2868 | } else if (len % BRCMF_SDALIGN) { |
| 2869 | len += BRCMF_SDALIGN - (len % BRCMF_SDALIGN); |
| 2870 | } |
| 2871 | |
| 2872 | /* Satisfy length-alignment requirements */ |
| 2873 | if (len & (ALIGNMENT - 1)) |
| 2874 | len = roundup(len, ALIGNMENT); |
| 2875 | |
| 2876 | /* precondition: IS_ALIGNED((unsigned long)frame, 2) */ |
| 2877 | |
| 2878 | /* Need to lock here to protect txseq and SDIO tx calls */ |
| 2879 | down(&bus->sdsem); |
| 2880 | |
| 2881 | bus_wake(bus); |
| 2882 | |
| 2883 | /* Make sure backplane clock is on */ |
| 2884 | brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false); |
| 2885 | |
| 2886 | /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */ |
| 2887 | *(__le16 *) frame = cpu_to_le16((u16) msglen); |
| 2888 | *(((__le16 *) frame) + 1) = cpu_to_le16(~msglen); |
| 2889 | |
| 2890 | /* Software tag: channel, sequence number, data offset */ |
| 2891 | swheader = |
| 2892 | ((SDPCM_CONTROL_CHANNEL << SDPCM_CHANNEL_SHIFT) & |
| 2893 | SDPCM_CHANNEL_MASK) |
| 2894 | | bus->tx_seq | ((doff << SDPCM_DOFFSET_SHIFT) & |
| 2895 | SDPCM_DOFFSET_MASK); |
| 2896 | put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN); |
| 2897 | put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader)); |
| 2898 | |
| 2899 | if (!data_ok(bus)) { |
| 2900 | brcmf_dbg(INFO, "No bus credit bus->tx_max %d, bus->tx_seq %d\n", |
| 2901 | bus->tx_max, bus->tx_seq); |
| 2902 | bus->ctrl_frame_stat = true; |
| 2903 | /* Send from dpc */ |
| 2904 | bus->ctrl_frame_buf = frame; |
| 2905 | bus->ctrl_frame_len = len; |
| 2906 | |
| 2907 | brcmf_sdbrcm_wait_for_event(bus, &bus->ctrl_frame_stat); |
| 2908 | |
| 2909 | if (bus->ctrl_frame_stat == false) { |
| 2910 | brcmf_dbg(INFO, "ctrl_frame_stat == false\n"); |
| 2911 | ret = 0; |
| 2912 | } else { |
| 2913 | brcmf_dbg(INFO, "ctrl_frame_stat == true\n"); |
| 2914 | ret = -1; |
| 2915 | } |
| 2916 | } |
| 2917 | |
| 2918 | if (ret == -1) { |
| 2919 | #ifdef BCMDBG |
| 2920 | if (BRCMF_BYTES_ON() && BRCMF_CTL_ON()) { |
| 2921 | printk(KERN_DEBUG "Tx Frame:\n"); |
| 2922 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 2923 | frame, len); |
| 2924 | } else if (BRCMF_HDRS_ON()) { |
| 2925 | printk(KERN_DEBUG "TxHdr:\n"); |
| 2926 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, |
| 2927 | frame, min_t(u16, len, 16)); |
| 2928 | } |
| 2929 | #endif |
| 2930 | |
| 2931 | do { |
| 2932 | ret = brcmf_tx_frame(bus, frame, len); |
| 2933 | } while (ret < 0 && retries++ < TXRETRIES); |
| 2934 | } |
| 2935 | |
| 2936 | if ((bus->idletime == BRCMF_IDLE_IMMEDIATE) && !bus->dpc_sched) { |
| 2937 | bus->activity = false; |
| 2938 | brcmf_sdbrcm_clkctl(bus, CLK_NONE, true); |
| 2939 | } |
| 2940 | |
| 2941 | up(&bus->sdsem); |
| 2942 | |
| 2943 | if (ret) |
| 2944 | bus->drvr->tx_ctlerrs++; |
| 2945 | else |
| 2946 | bus->drvr->tx_ctlpkts++; |
| 2947 | |
| 2948 | return ret ? -EIO : 0; |
| 2949 | } |
| 2950 | |
| 2951 | int |
| 2952 | brcmf_sdbrcm_bus_rxctl(struct brcmf_bus *bus, unsigned char *msg, uint msglen) |
| 2953 | { |
| 2954 | int timeleft; |
| 2955 | uint rxlen = 0; |
| 2956 | bool pending; |
| 2957 | |
| 2958 | brcmf_dbg(TRACE, "Enter\n"); |
| 2959 | |
| 2960 | /* Wait until control frame is available */ |
| 2961 | timeleft = brcmf_sdbrcm_dcmd_resp_wait(bus, &bus->rxlen, &pending); |
| 2962 | |
| 2963 | down(&bus->sdsem); |
| 2964 | rxlen = bus->rxlen; |
| 2965 | memcpy(msg, bus->rxctl, min(msglen, rxlen)); |
| 2966 | bus->rxlen = 0; |
| 2967 | up(&bus->sdsem); |
| 2968 | |
| 2969 | if (rxlen) { |
| 2970 | brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n", |
| 2971 | rxlen, msglen); |
| 2972 | } else if (timeleft == 0) { |
| 2973 | brcmf_dbg(ERROR, "resumed on timeout\n"); |
| 2974 | } else if (pending == true) { |
| 2975 | brcmf_dbg(CTL, "cancelled\n"); |
| 2976 | return -ERESTARTSYS; |
| 2977 | } else { |
| 2978 | brcmf_dbg(CTL, "resumed for unknown reason?\n"); |
| 2979 | } |
| 2980 | |
| 2981 | if (rxlen) |
| 2982 | bus->drvr->rx_ctlpkts++; |
| 2983 | else |
| 2984 | bus->drvr->rx_ctlerrs++; |
| 2985 | |
| 2986 | return rxlen ? (int)rxlen : -ETIMEDOUT; |
| 2987 | } |
| 2988 | |
| 2989 | static int brcmf_sdbrcm_downloadvars(struct brcmf_bus *bus, void *arg, int len) |
| 2990 | { |
| 2991 | int bcmerror = 0; |
| 2992 | |
| 2993 | brcmf_dbg(TRACE, "Enter\n"); |
| 2994 | |
| 2995 | /* Basic sanity checks */ |
| 2996 | if (bus->drvr->up) { |
| 2997 | bcmerror = -EISCONN; |
| 2998 | goto err; |
| 2999 | } |
| 3000 | if (!len) { |
| 3001 | bcmerror = -EOVERFLOW; |
| 3002 | goto err; |
| 3003 | } |
| 3004 | |
| 3005 | /* Free the old ones and replace with passed variables */ |
| 3006 | kfree(bus->vars); |
| 3007 | |
| 3008 | bus->vars = kmalloc(len, GFP_ATOMIC); |
| 3009 | bus->varsz = bus->vars ? len : 0; |
| 3010 | if (bus->vars == NULL) { |
| 3011 | bcmerror = -ENOMEM; |
| 3012 | goto err; |
| 3013 | } |
| 3014 | |
| 3015 | /* Copy the passed variables, which should include the |
| 3016 | terminating double-null */ |
| 3017 | memcpy(bus->vars, arg, bus->varsz); |
| 3018 | err: |
| 3019 | return bcmerror; |
| 3020 | } |
| 3021 | |
| 3022 | static int brcmf_sdbrcm_write_vars(struct brcmf_bus *bus) |
| 3023 | { |
| 3024 | int bcmerror = 0; |
| 3025 | u32 varsize; |
| 3026 | u32 varaddr; |
| 3027 | u8 *vbuffer; |
| 3028 | u32 varsizew; |
| 3029 | __le32 varsizew_le; |
| 3030 | #ifdef BCMDBG |
| 3031 | char *nvram_ularray; |
| 3032 | #endif /* BCMDBG */ |
| 3033 | |
| 3034 | /* Even if there are no vars are to be written, we still |
| 3035 | need to set the ramsize. */ |
| 3036 | varsize = bus->varsz ? roundup(bus->varsz, 4) : 0; |
| 3037 | varaddr = (bus->ramsize - 4) - varsize; |
| 3038 | |
| 3039 | if (bus->vars) { |
| 3040 | vbuffer = kzalloc(varsize, GFP_ATOMIC); |
| 3041 | if (!vbuffer) |
| 3042 | return -ENOMEM; |
| 3043 | |
| 3044 | memcpy(vbuffer, bus->vars, bus->varsz); |
| 3045 | |
| 3046 | /* Write the vars list */ |
| 3047 | bcmerror = |
| 3048 | brcmf_sdbrcm_membytes(bus, true, varaddr, vbuffer, varsize); |
| 3049 | #ifdef BCMDBG |
| 3050 | /* Verify NVRAM bytes */ |
| 3051 | brcmf_dbg(INFO, "Compare NVRAM dl & ul; varsize=%d\n", varsize); |
| 3052 | nvram_ularray = kmalloc(varsize, GFP_ATOMIC); |
| 3053 | if (!nvram_ularray) |
| 3054 | return -ENOMEM; |
| 3055 | |
| 3056 | /* Upload image to verify downloaded contents. */ |
| 3057 | memset(nvram_ularray, 0xaa, varsize); |
| 3058 | |
| 3059 | /* Read the vars list to temp buffer for comparison */ |
| 3060 | bcmerror = |
| 3061 | brcmf_sdbrcm_membytes(bus, false, varaddr, nvram_ularray, |
| 3062 | varsize); |
| 3063 | if (bcmerror) { |
| 3064 | brcmf_dbg(ERROR, "error %d on reading %d nvram bytes at 0x%08x\n", |
| 3065 | bcmerror, varsize, varaddr); |
| 3066 | } |
| 3067 | /* Compare the org NVRAM with the one read from RAM */ |
| 3068 | if (memcmp(vbuffer, nvram_ularray, varsize)) |
| 3069 | brcmf_dbg(ERROR, "Downloaded NVRAM image is corrupted\n"); |
| 3070 | else |
| 3071 | brcmf_dbg(ERROR, "Download/Upload/Compare of NVRAM ok\n"); |
| 3072 | |
| 3073 | kfree(nvram_ularray); |
| 3074 | #endif /* BCMDBG */ |
| 3075 | |
| 3076 | kfree(vbuffer); |
| 3077 | } |
| 3078 | |
| 3079 | /* adjust to the user specified RAM */ |
| 3080 | brcmf_dbg(INFO, "Physical memory size: %d\n", bus->ramsize); |
| 3081 | brcmf_dbg(INFO, "Vars are at %d, orig varsize is %d\n", |
| 3082 | varaddr, varsize); |
| 3083 | varsize = ((bus->ramsize - 4) - varaddr); |
| 3084 | |
| 3085 | /* |
| 3086 | * Determine the length token: |
| 3087 | * Varsize, converted to words, in lower 16-bits, checksum |
| 3088 | * in upper 16-bits. |
| 3089 | */ |
| 3090 | if (bcmerror) { |
| 3091 | varsizew = 0; |
| 3092 | varsizew_le = cpu_to_le32(0); |
| 3093 | } else { |
| 3094 | varsizew = varsize / 4; |
| 3095 | varsizew = (~varsizew << 16) | (varsizew & 0x0000FFFF); |
| 3096 | varsizew_le = cpu_to_le32(varsizew); |
| 3097 | } |
| 3098 | |
| 3099 | brcmf_dbg(INFO, "New varsize is %d, length token=0x%08x\n", |
| 3100 | varsize, varsizew); |
| 3101 | |
| 3102 | /* Write the length token to the last word */ |
| 3103 | bcmerror = brcmf_sdbrcm_membytes(bus, true, (bus->ramsize - 4), |
| 3104 | (u8 *)&varsizew_le, 4); |
| 3105 | |
| 3106 | return bcmerror; |
| 3107 | } |
| 3108 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3109 | static int brcmf_sdbrcm_download_state(struct brcmf_bus *bus, bool enter) |
| 3110 | { |
| 3111 | uint retries; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3112 | int bcmerror = 0; |
Franky Lin | 99ba15c | 2011-11-04 22:23:42 +0100 | [diff] [blame] | 3113 | struct chip_info *ci = bus->ci; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3114 | |
| 3115 | /* To enter download state, disable ARM and reset SOCRAM. |
| 3116 | * To exit download state, simply reset ARM (default is RAM boot). |
| 3117 | */ |
| 3118 | if (enter) { |
| 3119 | bus->alp_only = true; |
| 3120 | |
Franky Lin | 086a2e0 | 2011-11-10 20:30:23 +0100 | [diff] [blame] | 3121 | ci->coredisable(bus->sdiodev, ci, BCMA_CORE_ARM_CM3); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3122 | |
Franky Lin | d77e70f | 2011-11-10 20:30:24 +0100 | [diff] [blame] | 3123 | ci->resetcore(bus->sdiodev, ci, BCMA_CORE_INTERNAL_MEM); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3124 | |
| 3125 | /* Clear the top bit of memory */ |
| 3126 | if (bus->ramsize) { |
| 3127 | u32 zeros = 0; |
| 3128 | brcmf_sdbrcm_membytes(bus, true, bus->ramsize - 4, |
| 3129 | (u8 *)&zeros, 4); |
| 3130 | } |
| 3131 | } else { |
Franky Lin | 6ca687d | 2011-11-10 20:30:21 +0100 | [diff] [blame] | 3132 | if (!ci->iscoreup(bus->sdiodev, ci, BCMA_CORE_INTERNAL_MEM)) { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3133 | brcmf_dbg(ERROR, "SOCRAM core is down after reset?\n"); |
| 3134 | bcmerror = -EBADE; |
| 3135 | goto fail; |
| 3136 | } |
| 3137 | |
| 3138 | bcmerror = brcmf_sdbrcm_write_vars(bus); |
| 3139 | if (bcmerror) { |
| 3140 | brcmf_dbg(ERROR, "no vars written to RAM\n"); |
| 3141 | bcmerror = 0; |
| 3142 | } |
| 3143 | |
| 3144 | w_sdreg32(bus, 0xFFFFFFFF, |
| 3145 | offsetof(struct sdpcmd_regs, intstatus), &retries); |
| 3146 | |
Franky Lin | d77e70f | 2011-11-10 20:30:24 +0100 | [diff] [blame] | 3147 | ci->resetcore(bus->sdiodev, ci, BCMA_CORE_ARM_CM3); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3148 | |
| 3149 | /* Allow HT Clock now that the ARM is running. */ |
| 3150 | bus->alp_only = false; |
| 3151 | |
| 3152 | bus->drvr->busstate = BRCMF_BUS_LOAD; |
| 3153 | } |
| 3154 | fail: |
| 3155 | return bcmerror; |
| 3156 | } |
| 3157 | |
| 3158 | static int brcmf_sdbrcm_get_image(char *buf, int len, struct brcmf_bus *bus) |
| 3159 | { |
| 3160 | if (bus->firmware->size < bus->fw_ptr + len) |
| 3161 | len = bus->firmware->size - bus->fw_ptr; |
| 3162 | |
| 3163 | memcpy(buf, &bus->firmware->data[bus->fw_ptr], len); |
| 3164 | bus->fw_ptr += len; |
| 3165 | return len; |
| 3166 | } |
| 3167 | |
| 3168 | MODULE_FIRMWARE(BCM4329_FW_NAME); |
| 3169 | MODULE_FIRMWARE(BCM4329_NV_NAME); |
| 3170 | |
| 3171 | static int brcmf_sdbrcm_download_code_file(struct brcmf_bus *bus) |
| 3172 | { |
| 3173 | int offset = 0; |
| 3174 | uint len; |
| 3175 | u8 *memblock = NULL, *memptr; |
| 3176 | int ret; |
| 3177 | |
| 3178 | brcmf_dbg(INFO, "Enter\n"); |
| 3179 | |
| 3180 | bus->fw_name = BCM4329_FW_NAME; |
| 3181 | ret = request_firmware(&bus->firmware, bus->fw_name, |
| 3182 | &bus->sdiodev->func[2]->dev); |
| 3183 | if (ret) { |
| 3184 | brcmf_dbg(ERROR, "Fail to request firmware %d\n", ret); |
| 3185 | return ret; |
| 3186 | } |
| 3187 | bus->fw_ptr = 0; |
| 3188 | |
| 3189 | memptr = memblock = kmalloc(MEMBLOCK + BRCMF_SDALIGN, GFP_ATOMIC); |
| 3190 | if (memblock == NULL) { |
| 3191 | ret = -ENOMEM; |
| 3192 | goto err; |
| 3193 | } |
| 3194 | if ((u32)(unsigned long)memblock % BRCMF_SDALIGN) |
| 3195 | memptr += (BRCMF_SDALIGN - |
| 3196 | ((u32)(unsigned long)memblock % BRCMF_SDALIGN)); |
| 3197 | |
| 3198 | /* Download image */ |
| 3199 | while ((len = |
| 3200 | brcmf_sdbrcm_get_image((char *)memptr, MEMBLOCK, bus))) { |
| 3201 | ret = brcmf_sdbrcm_membytes(bus, true, offset, memptr, len); |
| 3202 | if (ret) { |
| 3203 | brcmf_dbg(ERROR, "error %d on writing %d membytes at 0x%08x\n", |
| 3204 | ret, MEMBLOCK, offset); |
| 3205 | goto err; |
| 3206 | } |
| 3207 | |
| 3208 | offset += MEMBLOCK; |
| 3209 | } |
| 3210 | |
| 3211 | err: |
| 3212 | kfree(memblock); |
| 3213 | |
| 3214 | release_firmware(bus->firmware); |
| 3215 | bus->fw_ptr = 0; |
| 3216 | |
| 3217 | return ret; |
| 3218 | } |
| 3219 | |
| 3220 | /* |
| 3221 | * ProcessVars:Takes a buffer of "<var>=<value>\n" lines read from a file |
| 3222 | * and ending in a NUL. |
| 3223 | * Removes carriage returns, empty lines, comment lines, and converts |
| 3224 | * newlines to NULs. |
| 3225 | * Shortens buffer as needed and pads with NULs. End of buffer is marked |
| 3226 | * by two NULs. |
| 3227 | */ |
| 3228 | |
| 3229 | static uint brcmf_process_nvram_vars(char *varbuf, uint len) |
| 3230 | { |
| 3231 | char *dp; |
| 3232 | bool findNewline; |
| 3233 | int column; |
| 3234 | uint buf_len, n; |
| 3235 | |
| 3236 | dp = varbuf; |
| 3237 | |
| 3238 | findNewline = false; |
| 3239 | column = 0; |
| 3240 | |
| 3241 | for (n = 0; n < len; n++) { |
| 3242 | if (varbuf[n] == 0) |
| 3243 | break; |
| 3244 | if (varbuf[n] == '\r') |
| 3245 | continue; |
| 3246 | if (findNewline && varbuf[n] != '\n') |
| 3247 | continue; |
| 3248 | findNewline = false; |
| 3249 | if (varbuf[n] == '#') { |
| 3250 | findNewline = true; |
| 3251 | continue; |
| 3252 | } |
| 3253 | if (varbuf[n] == '\n') { |
| 3254 | if (column == 0) |
| 3255 | continue; |
| 3256 | *dp++ = 0; |
| 3257 | column = 0; |
| 3258 | continue; |
| 3259 | } |
| 3260 | *dp++ = varbuf[n]; |
| 3261 | column++; |
| 3262 | } |
| 3263 | buf_len = dp - varbuf; |
| 3264 | |
| 3265 | while (dp < varbuf + n) |
| 3266 | *dp++ = 0; |
| 3267 | |
| 3268 | return buf_len; |
| 3269 | } |
| 3270 | |
| 3271 | static int brcmf_sdbrcm_download_nvram(struct brcmf_bus *bus) |
| 3272 | { |
| 3273 | uint len; |
| 3274 | char *memblock = NULL; |
| 3275 | char *bufp; |
| 3276 | int ret; |
| 3277 | |
| 3278 | bus->nv_name = BCM4329_NV_NAME; |
| 3279 | ret = request_firmware(&bus->firmware, bus->nv_name, |
| 3280 | &bus->sdiodev->func[2]->dev); |
| 3281 | if (ret) { |
| 3282 | brcmf_dbg(ERROR, "Fail to request nvram %d\n", ret); |
| 3283 | return ret; |
| 3284 | } |
| 3285 | bus->fw_ptr = 0; |
| 3286 | |
| 3287 | memblock = kmalloc(MEMBLOCK, GFP_ATOMIC); |
| 3288 | if (memblock == NULL) { |
| 3289 | ret = -ENOMEM; |
| 3290 | goto err; |
| 3291 | } |
| 3292 | |
| 3293 | len = brcmf_sdbrcm_get_image(memblock, MEMBLOCK, bus); |
| 3294 | |
| 3295 | if (len > 0 && len < MEMBLOCK) { |
| 3296 | bufp = (char *)memblock; |
| 3297 | bufp[len] = 0; |
| 3298 | len = brcmf_process_nvram_vars(bufp, len); |
| 3299 | bufp += len; |
| 3300 | *bufp++ = 0; |
| 3301 | if (len) |
| 3302 | ret = brcmf_sdbrcm_downloadvars(bus, memblock, len + 1); |
| 3303 | if (ret) |
| 3304 | brcmf_dbg(ERROR, "error downloading vars: %d\n", ret); |
| 3305 | } else { |
| 3306 | brcmf_dbg(ERROR, "error reading nvram file: %d\n", len); |
| 3307 | ret = -EIO; |
| 3308 | } |
| 3309 | |
| 3310 | err: |
| 3311 | kfree(memblock); |
| 3312 | |
| 3313 | release_firmware(bus->firmware); |
| 3314 | bus->fw_ptr = 0; |
| 3315 | |
| 3316 | return ret; |
| 3317 | } |
| 3318 | |
| 3319 | static int _brcmf_sdbrcm_download_firmware(struct brcmf_bus *bus) |
| 3320 | { |
| 3321 | int bcmerror = -1; |
| 3322 | |
| 3323 | /* Keep arm in reset */ |
| 3324 | if (brcmf_sdbrcm_download_state(bus, true)) { |
| 3325 | brcmf_dbg(ERROR, "error placing ARM core in reset\n"); |
| 3326 | goto err; |
| 3327 | } |
| 3328 | |
| 3329 | /* External image takes precedence if specified */ |
| 3330 | if (brcmf_sdbrcm_download_code_file(bus)) { |
| 3331 | brcmf_dbg(ERROR, "dongle image file download failed\n"); |
| 3332 | goto err; |
| 3333 | } |
| 3334 | |
| 3335 | /* External nvram takes precedence if specified */ |
| 3336 | if (brcmf_sdbrcm_download_nvram(bus)) |
| 3337 | brcmf_dbg(ERROR, "dongle nvram file download failed\n"); |
| 3338 | |
| 3339 | /* Take arm out of reset */ |
| 3340 | if (brcmf_sdbrcm_download_state(bus, false)) { |
| 3341 | brcmf_dbg(ERROR, "error getting out of ARM core reset\n"); |
| 3342 | goto err; |
| 3343 | } |
| 3344 | |
| 3345 | bcmerror = 0; |
| 3346 | |
| 3347 | err: |
| 3348 | return bcmerror; |
| 3349 | } |
| 3350 | |
| 3351 | static bool |
| 3352 | brcmf_sdbrcm_download_firmware(struct brcmf_bus *bus) |
| 3353 | { |
| 3354 | bool ret; |
| 3355 | |
| 3356 | /* Download the firmware */ |
| 3357 | brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false); |
| 3358 | |
| 3359 | ret = _brcmf_sdbrcm_download_firmware(bus) == 0; |
| 3360 | |
| 3361 | brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false); |
| 3362 | |
| 3363 | return ret; |
| 3364 | } |
| 3365 | |
| 3366 | void brcmf_sdbrcm_bus_stop(struct brcmf_bus *bus) |
| 3367 | { |
| 3368 | u32 local_hostintmask; |
| 3369 | u8 saveclk; |
| 3370 | uint retries; |
| 3371 | int err; |
| 3372 | |
| 3373 | brcmf_dbg(TRACE, "Enter\n"); |
| 3374 | |
| 3375 | if (bus->watchdog_tsk) { |
| 3376 | send_sig(SIGTERM, bus->watchdog_tsk, 1); |
| 3377 | kthread_stop(bus->watchdog_tsk); |
| 3378 | bus->watchdog_tsk = NULL; |
| 3379 | } |
| 3380 | |
| 3381 | if (bus->dpc_tsk && bus->dpc_tsk != current) { |
| 3382 | send_sig(SIGTERM, bus->dpc_tsk, 1); |
| 3383 | kthread_stop(bus->dpc_tsk); |
| 3384 | bus->dpc_tsk = NULL; |
| 3385 | } |
| 3386 | |
| 3387 | down(&bus->sdsem); |
| 3388 | |
| 3389 | bus_wake(bus); |
| 3390 | |
| 3391 | /* Enable clock for device interrupts */ |
| 3392 | brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false); |
| 3393 | |
| 3394 | /* Disable and clear interrupts at the chip level also */ |
| 3395 | w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask), &retries); |
| 3396 | local_hostintmask = bus->hostintmask; |
| 3397 | bus->hostintmask = 0; |
| 3398 | |
| 3399 | /* Change our idea of bus state */ |
| 3400 | bus->drvr->busstate = BRCMF_BUS_DOWN; |
| 3401 | |
| 3402 | /* Force clocks on backplane to be sure F2 interrupt propagates */ |
| 3403 | saveclk = brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 3404 | SBSDIO_FUNC1_CHIPCLKCSR, &err); |
| 3405 | if (!err) { |
| 3406 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 3407 | SBSDIO_FUNC1_CHIPCLKCSR, |
| 3408 | (saveclk | SBSDIO_FORCE_HT), &err); |
| 3409 | } |
| 3410 | if (err) |
| 3411 | brcmf_dbg(ERROR, "Failed to force clock for F2: err %d\n", err); |
| 3412 | |
| 3413 | /* Turn off the bus (F2), free any pending packets */ |
| 3414 | brcmf_dbg(INTR, "disable SDIO interrupts\n"); |
| 3415 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_0, SDIO_CCCR_IOEx, |
| 3416 | SDIO_FUNC_ENABLE_1, NULL); |
| 3417 | |
| 3418 | /* Clear any pending interrupts now that F2 is disabled */ |
| 3419 | w_sdreg32(bus, local_hostintmask, |
| 3420 | offsetof(struct sdpcmd_regs, intstatus), &retries); |
| 3421 | |
| 3422 | /* Turn off the backplane clock (only) */ |
| 3423 | brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false); |
| 3424 | |
| 3425 | /* Clear the data packet queues */ |
| 3426 | brcmu_pktq_flush(&bus->txq, true, NULL, NULL); |
| 3427 | |
| 3428 | /* Clear any held glomming stuff */ |
| 3429 | if (bus->glomd) |
| 3430 | brcmu_pkt_buf_free_skb(bus->glomd); |
Arend van Spriel | 046808d | 2011-11-10 20:30:31 +0100 | [diff] [blame^] | 3431 | brcmf_sdbrcm_free_glom(bus); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3432 | |
| 3433 | /* Clear rx control and wake any waiters */ |
| 3434 | bus->rxlen = 0; |
| 3435 | brcmf_sdbrcm_dcmd_resp_wake(bus); |
| 3436 | |
| 3437 | /* Reset some F2 state stuff */ |
| 3438 | bus->rxskip = false; |
| 3439 | bus->tx_seq = bus->rx_seq = 0; |
| 3440 | |
| 3441 | up(&bus->sdsem); |
| 3442 | } |
| 3443 | |
| 3444 | int brcmf_sdbrcm_bus_init(struct brcmf_pub *drvr) |
| 3445 | { |
| 3446 | struct brcmf_bus *bus = drvr->bus; |
| 3447 | unsigned long timeout; |
| 3448 | uint retries = 0; |
| 3449 | u8 ready, enable; |
| 3450 | int err, ret = 0; |
| 3451 | u8 saveclk; |
| 3452 | |
| 3453 | brcmf_dbg(TRACE, "Enter\n"); |
| 3454 | |
| 3455 | /* try to download image and nvram to the dongle */ |
| 3456 | if (drvr->busstate == BRCMF_BUS_DOWN) { |
| 3457 | if (!(brcmf_sdbrcm_download_firmware(bus))) |
| 3458 | return -1; |
| 3459 | } |
| 3460 | |
| 3461 | if (!bus->drvr) |
| 3462 | return 0; |
| 3463 | |
| 3464 | /* Start the watchdog timer */ |
| 3465 | bus->drvr->tickcnt = 0; |
| 3466 | brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS); |
| 3467 | |
| 3468 | down(&bus->sdsem); |
| 3469 | |
| 3470 | /* Make sure backplane clock is on, needed to generate F2 interrupt */ |
| 3471 | brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false); |
| 3472 | if (bus->clkstate != CLK_AVAIL) |
| 3473 | goto exit; |
| 3474 | |
| 3475 | /* Force clocks on backplane to be sure F2 interrupt propagates */ |
| 3476 | saveclk = |
| 3477 | brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 3478 | SBSDIO_FUNC1_CHIPCLKCSR, &err); |
| 3479 | if (!err) { |
| 3480 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 3481 | SBSDIO_FUNC1_CHIPCLKCSR, |
| 3482 | (saveclk | SBSDIO_FORCE_HT), &err); |
| 3483 | } |
| 3484 | if (err) { |
| 3485 | brcmf_dbg(ERROR, "Failed to force clock for F2: err %d\n", err); |
| 3486 | goto exit; |
| 3487 | } |
| 3488 | |
| 3489 | /* Enable function 2 (frame transfers) */ |
| 3490 | w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT, |
| 3491 | offsetof(struct sdpcmd_regs, tosbmailboxdata), &retries); |
| 3492 | enable = (SDIO_FUNC_ENABLE_1 | SDIO_FUNC_ENABLE_2); |
| 3493 | |
| 3494 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_0, SDIO_CCCR_IOEx, |
| 3495 | enable, NULL); |
| 3496 | |
| 3497 | timeout = jiffies + msecs_to_jiffies(BRCMF_WAIT_F2RDY); |
| 3498 | ready = 0; |
| 3499 | while (enable != ready) { |
| 3500 | ready = brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_0, |
| 3501 | SDIO_CCCR_IORx, NULL); |
| 3502 | if (time_after(jiffies, timeout)) |
| 3503 | break; |
| 3504 | else if (time_after(jiffies, timeout - BRCMF_WAIT_F2RDY + 50)) |
| 3505 | /* prevent busy waiting if it takes too long */ |
| 3506 | msleep_interruptible(20); |
| 3507 | } |
| 3508 | |
| 3509 | brcmf_dbg(INFO, "enable 0x%02x, ready 0x%02x\n", enable, ready); |
| 3510 | |
| 3511 | /* If F2 successfully enabled, set core and enable interrupts */ |
| 3512 | if (ready == enable) { |
| 3513 | /* Set up the interrupt mask and enable interrupts */ |
| 3514 | bus->hostintmask = HOSTINTMASK; |
| 3515 | w_sdreg32(bus, bus->hostintmask, |
| 3516 | offsetof(struct sdpcmd_regs, hostintmask), &retries); |
| 3517 | |
| 3518 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 3519 | SBSDIO_WATERMARK, 8, &err); |
| 3520 | |
| 3521 | /* Set bus state according to enable result */ |
| 3522 | drvr->busstate = BRCMF_BUS_DATA; |
| 3523 | } |
| 3524 | |
| 3525 | else { |
| 3526 | /* Disable F2 again */ |
| 3527 | enable = SDIO_FUNC_ENABLE_1; |
| 3528 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_0, |
| 3529 | SDIO_CCCR_IOEx, enable, NULL); |
| 3530 | } |
| 3531 | |
| 3532 | /* Restore previous clock setting */ |
| 3533 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 3534 | SBSDIO_FUNC1_CHIPCLKCSR, saveclk, &err); |
| 3535 | |
| 3536 | /* If we didn't come up, turn off backplane clock */ |
| 3537 | if (drvr->busstate != BRCMF_BUS_DATA) |
| 3538 | brcmf_sdbrcm_clkctl(bus, CLK_NONE, false); |
| 3539 | |
| 3540 | exit: |
| 3541 | up(&bus->sdsem); |
| 3542 | |
| 3543 | return ret; |
| 3544 | } |
| 3545 | |
| 3546 | void brcmf_sdbrcm_isr(void *arg) |
| 3547 | { |
| 3548 | struct brcmf_bus *bus = (struct brcmf_bus *) arg; |
| 3549 | |
| 3550 | brcmf_dbg(TRACE, "Enter\n"); |
| 3551 | |
| 3552 | if (!bus) { |
| 3553 | brcmf_dbg(ERROR, "bus is null pointer, exiting\n"); |
| 3554 | return; |
| 3555 | } |
| 3556 | |
| 3557 | if (bus->drvr->busstate == BRCMF_BUS_DOWN) { |
| 3558 | brcmf_dbg(ERROR, "bus is down. we have nothing to do\n"); |
| 3559 | return; |
| 3560 | } |
| 3561 | /* Count the interrupt call */ |
| 3562 | bus->intrcount++; |
| 3563 | bus->ipend = true; |
| 3564 | |
| 3565 | /* Shouldn't get this interrupt if we're sleeping? */ |
| 3566 | if (bus->sleeping) { |
| 3567 | brcmf_dbg(ERROR, "INTERRUPT WHILE SLEEPING??\n"); |
| 3568 | return; |
| 3569 | } |
| 3570 | |
| 3571 | /* Disable additional interrupts (is this needed now)? */ |
| 3572 | if (!bus->intr) |
| 3573 | brcmf_dbg(ERROR, "isr w/o interrupt configured!\n"); |
| 3574 | |
| 3575 | bus->dpc_sched = true; |
| 3576 | if (bus->dpc_tsk) |
| 3577 | complete(&bus->dpc_wait); |
| 3578 | } |
| 3579 | |
| 3580 | static bool brcmf_sdbrcm_bus_watchdog(struct brcmf_pub *drvr) |
| 3581 | { |
| 3582 | struct brcmf_bus *bus; |
| 3583 | |
| 3584 | brcmf_dbg(TIMER, "Enter\n"); |
| 3585 | |
| 3586 | bus = drvr->bus; |
| 3587 | |
| 3588 | /* Ignore the timer if simulating bus down */ |
| 3589 | if (bus->sleeping) |
| 3590 | return false; |
| 3591 | |
| 3592 | down(&bus->sdsem); |
| 3593 | |
| 3594 | /* Poll period: check device if appropriate. */ |
| 3595 | if (bus->poll && (++bus->polltick >= bus->pollrate)) { |
| 3596 | u32 intstatus = 0; |
| 3597 | |
| 3598 | /* Reset poll tick */ |
| 3599 | bus->polltick = 0; |
| 3600 | |
| 3601 | /* Check device if no interrupts */ |
| 3602 | if (!bus->intr || (bus->intrcount == bus->lastintrs)) { |
| 3603 | |
| 3604 | if (!bus->dpc_sched) { |
| 3605 | u8 devpend; |
| 3606 | devpend = brcmf_sdcard_cfg_read(bus->sdiodev, |
| 3607 | SDIO_FUNC_0, SDIO_CCCR_INTx, |
| 3608 | NULL); |
| 3609 | intstatus = |
| 3610 | devpend & (INTR_STATUS_FUNC1 | |
| 3611 | INTR_STATUS_FUNC2); |
| 3612 | } |
| 3613 | |
| 3614 | /* If there is something, make like the ISR and |
| 3615 | schedule the DPC */ |
| 3616 | if (intstatus) { |
| 3617 | bus->pollcnt++; |
| 3618 | bus->ipend = true; |
| 3619 | |
| 3620 | bus->dpc_sched = true; |
| 3621 | if (bus->dpc_tsk) |
| 3622 | complete(&bus->dpc_wait); |
| 3623 | } |
| 3624 | } |
| 3625 | |
| 3626 | /* Update interrupt tracking */ |
| 3627 | bus->lastintrs = bus->intrcount; |
| 3628 | } |
| 3629 | #ifdef BCMDBG |
| 3630 | /* Poll for console output periodically */ |
| 3631 | if (drvr->busstate == BRCMF_BUS_DATA && bus->console_interval != 0) { |
| 3632 | bus->console.count += BRCMF_WD_POLL_MS; |
| 3633 | if (bus->console.count >= bus->console_interval) { |
| 3634 | bus->console.count -= bus->console_interval; |
| 3635 | /* Make sure backplane clock is on */ |
| 3636 | brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false); |
| 3637 | if (brcmf_sdbrcm_readconsole(bus) < 0) |
| 3638 | /* stop on error */ |
| 3639 | bus->console_interval = 0; |
| 3640 | } |
| 3641 | } |
| 3642 | #endif /* BCMDBG */ |
| 3643 | |
| 3644 | /* On idle timeout clear activity flag and/or turn off clock */ |
| 3645 | if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) { |
| 3646 | if (++bus->idlecount >= bus->idletime) { |
| 3647 | bus->idlecount = 0; |
| 3648 | if (bus->activity) { |
| 3649 | bus->activity = false; |
| 3650 | brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS); |
| 3651 | } else { |
| 3652 | brcmf_sdbrcm_clkctl(bus, CLK_NONE, false); |
| 3653 | } |
| 3654 | } |
| 3655 | } |
| 3656 | |
| 3657 | up(&bus->sdsem); |
| 3658 | |
| 3659 | return bus->ipend; |
| 3660 | } |
| 3661 | |
| 3662 | static bool brcmf_sdbrcm_chipmatch(u16 chipid) |
| 3663 | { |
| 3664 | if (chipid == BCM4329_CHIP_ID) |
| 3665 | return true; |
| 3666 | return false; |
| 3667 | } |
| 3668 | |
| 3669 | static void brcmf_sdbrcm_release_malloc(struct brcmf_bus *bus) |
| 3670 | { |
| 3671 | brcmf_dbg(TRACE, "Enter\n"); |
| 3672 | |
| 3673 | kfree(bus->rxbuf); |
| 3674 | bus->rxctl = bus->rxbuf = NULL; |
| 3675 | bus->rxlen = 0; |
| 3676 | |
| 3677 | kfree(bus->databuf); |
| 3678 | bus->databuf = NULL; |
| 3679 | } |
| 3680 | |
| 3681 | static bool brcmf_sdbrcm_probe_malloc(struct brcmf_bus *bus) |
| 3682 | { |
| 3683 | brcmf_dbg(TRACE, "Enter\n"); |
| 3684 | |
| 3685 | if (bus->drvr->maxctl) { |
| 3686 | bus->rxblen = |
| 3687 | roundup((bus->drvr->maxctl + SDPCM_HDRLEN), |
| 3688 | ALIGNMENT) + BRCMF_SDALIGN; |
| 3689 | bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC); |
| 3690 | if (!(bus->rxbuf)) |
| 3691 | goto fail; |
| 3692 | } |
| 3693 | |
| 3694 | /* Allocate buffer to receive glomed packet */ |
| 3695 | bus->databuf = kmalloc(MAX_DATA_BUF, GFP_ATOMIC); |
| 3696 | if (!(bus->databuf)) { |
| 3697 | /* release rxbuf which was already located as above */ |
| 3698 | if (!bus->rxblen) |
| 3699 | kfree(bus->rxbuf); |
| 3700 | goto fail; |
| 3701 | } |
| 3702 | |
| 3703 | /* Align the buffer */ |
| 3704 | if ((unsigned long)bus->databuf % BRCMF_SDALIGN) |
| 3705 | bus->dataptr = bus->databuf + (BRCMF_SDALIGN - |
| 3706 | ((unsigned long)bus->databuf % BRCMF_SDALIGN)); |
| 3707 | else |
| 3708 | bus->dataptr = bus->databuf; |
| 3709 | |
| 3710 | return true; |
| 3711 | |
| 3712 | fail: |
| 3713 | return false; |
| 3714 | } |
| 3715 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3716 | static bool |
| 3717 | brcmf_sdbrcm_probe_attach(struct brcmf_bus *bus, u32 regsva) |
| 3718 | { |
| 3719 | u8 clkctl = 0; |
| 3720 | int err = 0; |
| 3721 | int reg_addr; |
| 3722 | u32 reg_val; |
Franky Lin | 99ba15c | 2011-11-04 22:23:42 +0100 | [diff] [blame] | 3723 | u8 idx; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3724 | |
| 3725 | bus->alp_only = true; |
| 3726 | |
| 3727 | /* Return the window to backplane enumeration space for core access */ |
| 3728 | if (brcmf_sdcard_set_sbaddr_window(bus->sdiodev, SI_ENUM_BASE)) |
| 3729 | brcmf_dbg(ERROR, "FAILED to return to SI_ENUM_BASE\n"); |
| 3730 | |
| 3731 | #ifdef BCMDBG |
| 3732 | printk(KERN_DEBUG "F1 signature read @0x18000000=0x%4x\n", |
| 3733 | brcmf_sdcard_reg_read(bus->sdiodev, SI_ENUM_BASE, 4)); |
| 3734 | |
| 3735 | #endif /* BCMDBG */ |
| 3736 | |
| 3737 | /* |
Franky Lin | a97e4fc5 | 2011-11-04 22:23:35 +0100 | [diff] [blame] | 3738 | * Force PLL off until brcmf_sdio_chip_attach() |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3739 | * programs PLL control regs |
| 3740 | */ |
| 3741 | |
| 3742 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 3743 | SBSDIO_FUNC1_CHIPCLKCSR, |
| 3744 | BRCMF_INIT_CLKCTL1, &err); |
| 3745 | if (!err) |
| 3746 | clkctl = |
| 3747 | brcmf_sdcard_cfg_read(bus->sdiodev, SDIO_FUNC_1, |
| 3748 | SBSDIO_FUNC1_CHIPCLKCSR, &err); |
| 3749 | |
| 3750 | if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) { |
| 3751 | brcmf_dbg(ERROR, "ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n", |
| 3752 | err, BRCMF_INIT_CLKCTL1, clkctl); |
| 3753 | goto fail; |
| 3754 | } |
| 3755 | |
Franky Lin | a97e4fc5 | 2011-11-04 22:23:35 +0100 | [diff] [blame] | 3756 | if (brcmf_sdio_chip_attach(bus->sdiodev, &bus->ci, regsva)) { |
| 3757 | brcmf_dbg(ERROR, "brcmf_sdio_chip_attach failed!\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3758 | goto fail; |
| 3759 | } |
| 3760 | |
| 3761 | if (!brcmf_sdbrcm_chipmatch((u16) bus->ci->chip)) { |
| 3762 | brcmf_dbg(ERROR, "unsupported chip: 0x%04x\n", bus->ci->chip); |
| 3763 | goto fail; |
| 3764 | } |
| 3765 | |
Franky Lin | e12afb6 | 2011-11-04 22:23:40 +0100 | [diff] [blame] | 3766 | brcmf_sdio_chip_drivestrengthinit(bus->sdiodev, bus->ci, |
| 3767 | SDIO_DRIVE_STRENGTH); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3768 | |
Franky Lin | 454d2a8 | 2011-11-04 22:23:37 +0100 | [diff] [blame] | 3769 | /* Get info on the SOCRAM cores... */ |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3770 | bus->ramsize = bus->ci->ramsize; |
| 3771 | if (!(bus->ramsize)) { |
| 3772 | brcmf_dbg(ERROR, "failed to find SOCRAM memory!\n"); |
| 3773 | goto fail; |
| 3774 | } |
| 3775 | |
| 3776 | /* Set core control so an SDIO reset does a backplane reset */ |
Franky Lin | 99ba15c | 2011-11-04 22:23:42 +0100 | [diff] [blame] | 3777 | idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV); |
| 3778 | reg_addr = bus->ci->c_inf[idx].base + |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3779 | offsetof(struct sdpcmd_regs, corecontrol); |
| 3780 | reg_val = brcmf_sdcard_reg_read(bus->sdiodev, reg_addr, sizeof(u32)); |
| 3781 | brcmf_sdcard_reg_write(bus->sdiodev, reg_addr, sizeof(u32), |
| 3782 | reg_val | CC_BPRESEN); |
| 3783 | |
| 3784 | brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN); |
| 3785 | |
| 3786 | /* Locate an appropriately-aligned portion of hdrbuf */ |
| 3787 | bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0], |
| 3788 | BRCMF_SDALIGN); |
| 3789 | |
| 3790 | /* Set the poll and/or interrupt flags */ |
| 3791 | bus->intr = true; |
| 3792 | bus->poll = false; |
| 3793 | if (bus->poll) |
| 3794 | bus->pollrate = 1; |
| 3795 | |
| 3796 | return true; |
| 3797 | |
| 3798 | fail: |
| 3799 | return false; |
| 3800 | } |
| 3801 | |
| 3802 | static bool brcmf_sdbrcm_probe_init(struct brcmf_bus *bus) |
| 3803 | { |
| 3804 | brcmf_dbg(TRACE, "Enter\n"); |
| 3805 | |
| 3806 | /* Disable F2 to clear any intermediate frame state on the dongle */ |
| 3807 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_0, SDIO_CCCR_IOEx, |
| 3808 | SDIO_FUNC_ENABLE_1, NULL); |
| 3809 | |
| 3810 | bus->drvr->busstate = BRCMF_BUS_DOWN; |
| 3811 | bus->sleeping = false; |
| 3812 | bus->rxflow = false; |
| 3813 | |
| 3814 | /* Done with backplane-dependent accesses, can drop clock... */ |
| 3815 | brcmf_sdcard_cfg_write(bus->sdiodev, SDIO_FUNC_1, |
| 3816 | SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL); |
| 3817 | |
| 3818 | /* ...and initialize clock/power states */ |
| 3819 | bus->clkstate = CLK_SDONLY; |
| 3820 | bus->idletime = BRCMF_IDLE_INTERVAL; |
| 3821 | bus->idleclock = BRCMF_IDLE_ACTIVE; |
| 3822 | |
| 3823 | /* Query the F2 block size, set roundup accordingly */ |
| 3824 | bus->blocksize = bus->sdiodev->func[2]->cur_blksize; |
| 3825 | bus->roundup = min(max_roundup, bus->blocksize); |
| 3826 | |
| 3827 | /* bus module does not support packet chaining */ |
| 3828 | bus->use_rxchain = false; |
| 3829 | bus->sd_rxchain = false; |
| 3830 | |
| 3831 | return true; |
| 3832 | } |
| 3833 | |
| 3834 | static int |
| 3835 | brcmf_sdbrcm_watchdog_thread(void *data) |
| 3836 | { |
| 3837 | struct brcmf_bus *bus = (struct brcmf_bus *)data; |
| 3838 | |
| 3839 | allow_signal(SIGTERM); |
| 3840 | /* Run until signal received */ |
| 3841 | while (1) { |
| 3842 | if (kthread_should_stop()) |
| 3843 | break; |
| 3844 | if (!wait_for_completion_interruptible(&bus->watchdog_wait)) { |
| 3845 | brcmf_sdbrcm_bus_watchdog(bus->drvr); |
| 3846 | /* Count the tick for reference */ |
| 3847 | bus->drvr->tickcnt++; |
| 3848 | } else |
| 3849 | break; |
| 3850 | } |
| 3851 | return 0; |
| 3852 | } |
| 3853 | |
| 3854 | static void |
| 3855 | brcmf_sdbrcm_watchdog(unsigned long data) |
| 3856 | { |
| 3857 | struct brcmf_bus *bus = (struct brcmf_bus *)data; |
| 3858 | |
| 3859 | if (bus->watchdog_tsk) { |
| 3860 | complete(&bus->watchdog_wait); |
| 3861 | /* Reschedule the watchdog */ |
| 3862 | if (bus->wd_timer_valid) |
| 3863 | mod_timer(&bus->timer, |
| 3864 | jiffies + BRCMF_WD_POLL_MS * HZ / 1000); |
| 3865 | } |
| 3866 | } |
| 3867 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3868 | static void brcmf_sdbrcm_release_dongle(struct brcmf_bus *bus) |
| 3869 | { |
| 3870 | brcmf_dbg(TRACE, "Enter\n"); |
| 3871 | |
| 3872 | if (bus->ci) { |
| 3873 | brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false); |
| 3874 | brcmf_sdbrcm_clkctl(bus, CLK_NONE, false); |
Franky Lin | a8a6c04 | 2011-11-04 22:23:39 +0100 | [diff] [blame] | 3875 | brcmf_sdio_chip_detach(&bus->ci); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3876 | if (bus->vars && bus->varsz) |
| 3877 | kfree(bus->vars); |
| 3878 | bus->vars = NULL; |
| 3879 | } |
| 3880 | |
| 3881 | brcmf_dbg(TRACE, "Disconnected\n"); |
| 3882 | } |
| 3883 | |
| 3884 | /* Detach and free everything */ |
| 3885 | static void brcmf_sdbrcm_release(struct brcmf_bus *bus) |
| 3886 | { |
| 3887 | brcmf_dbg(TRACE, "Enter\n"); |
| 3888 | |
| 3889 | if (bus) { |
| 3890 | /* De-register interrupt handler */ |
| 3891 | brcmf_sdcard_intr_dereg(bus->sdiodev); |
| 3892 | |
| 3893 | if (bus->drvr) { |
| 3894 | brcmf_detach(bus->drvr); |
| 3895 | brcmf_sdbrcm_release_dongle(bus); |
| 3896 | bus->drvr = NULL; |
| 3897 | } |
| 3898 | |
| 3899 | brcmf_sdbrcm_release_malloc(bus); |
| 3900 | |
| 3901 | kfree(bus); |
| 3902 | } |
| 3903 | |
| 3904 | brcmf_dbg(TRACE, "Disconnected\n"); |
| 3905 | } |
| 3906 | |
| 3907 | void *brcmf_sdbrcm_probe(u16 bus_no, u16 slot, u16 func, uint bustype, |
| 3908 | u32 regsva, struct brcmf_sdio_dev *sdiodev) |
| 3909 | { |
| 3910 | int ret; |
| 3911 | struct brcmf_bus *bus; |
| 3912 | |
| 3913 | /* Init global variables at run-time, not as part of the declaration. |
| 3914 | * This is required to support init/de-init of the driver. |
| 3915 | * Initialization |
| 3916 | * of globals as part of the declaration results in non-deterministic |
| 3917 | * behavior since the value of the globals may be different on the |
| 3918 | * first time that the driver is initialized vs subsequent |
| 3919 | * initializations. |
| 3920 | */ |
| 3921 | brcmf_c_init(); |
| 3922 | |
| 3923 | brcmf_dbg(TRACE, "Enter\n"); |
| 3924 | |
| 3925 | /* We make an assumption about address window mappings: |
| 3926 | * regsva == SI_ENUM_BASE*/ |
| 3927 | |
| 3928 | /* Allocate private bus interface state */ |
| 3929 | bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC); |
| 3930 | if (!bus) |
| 3931 | goto fail; |
| 3932 | |
| 3933 | bus->sdiodev = sdiodev; |
| 3934 | sdiodev->bus = bus; |
Arend van Spriel | b83db86 | 2011-10-19 12:51:09 +0200 | [diff] [blame] | 3935 | skb_queue_head_init(&bus->glom); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 3936 | bus->txbound = BRCMF_TXBOUND; |
| 3937 | bus->rxbound = BRCMF_RXBOUND; |
| 3938 | bus->txminmax = BRCMF_TXMINMAX; |
| 3939 | bus->tx_seq = SDPCM_SEQUENCE_WRAP - 1; |
| 3940 | bus->usebufpool = false; /* Use bufpool if allocated, |
| 3941 | else use locally malloced rxbuf */ |
| 3942 | |
| 3943 | /* attempt to attach to the dongle */ |
| 3944 | if (!(brcmf_sdbrcm_probe_attach(bus, regsva))) { |
| 3945 | brcmf_dbg(ERROR, "brcmf_sdbrcm_probe_attach failed\n"); |
| 3946 | goto fail; |
| 3947 | } |
| 3948 | |
| 3949 | spin_lock_init(&bus->txqlock); |
| 3950 | init_waitqueue_head(&bus->ctrl_wait); |
| 3951 | init_waitqueue_head(&bus->dcmd_resp_wait); |
| 3952 | |
| 3953 | /* Set up the watchdog timer */ |
| 3954 | init_timer(&bus->timer); |
| 3955 | bus->timer.data = (unsigned long)bus; |
| 3956 | bus->timer.function = brcmf_sdbrcm_watchdog; |
| 3957 | |
| 3958 | /* Initialize thread based operation and lock */ |
| 3959 | sema_init(&bus->sdsem, 1); |
| 3960 | |
| 3961 | /* Initialize watchdog thread */ |
| 3962 | init_completion(&bus->watchdog_wait); |
| 3963 | bus->watchdog_tsk = kthread_run(brcmf_sdbrcm_watchdog_thread, |
| 3964 | bus, "brcmf_watchdog"); |
| 3965 | if (IS_ERR(bus->watchdog_tsk)) { |
| 3966 | printk(KERN_WARNING |
| 3967 | "brcmf_watchdog thread failed to start\n"); |
| 3968 | bus->watchdog_tsk = NULL; |
| 3969 | } |
| 3970 | /* Initialize DPC thread */ |
| 3971 | init_completion(&bus->dpc_wait); |
| 3972 | bus->dpc_tsk = kthread_run(brcmf_sdbrcm_dpc_thread, |
| 3973 | bus, "brcmf_dpc"); |
| 3974 | if (IS_ERR(bus->dpc_tsk)) { |
| 3975 | printk(KERN_WARNING |
| 3976 | "brcmf_dpc thread failed to start\n"); |
| 3977 | bus->dpc_tsk = NULL; |
| 3978 | } |
| 3979 | |
| 3980 | /* Attach to the brcmf/OS/network interface */ |
| 3981 | bus->drvr = brcmf_attach(bus, SDPCM_RESERVE); |
| 3982 | if (!bus->drvr) { |
| 3983 | brcmf_dbg(ERROR, "brcmf_attach failed\n"); |
| 3984 | goto fail; |
| 3985 | } |
| 3986 | |
| 3987 | /* Allocate buffers */ |
| 3988 | if (!(brcmf_sdbrcm_probe_malloc(bus))) { |
| 3989 | brcmf_dbg(ERROR, "brcmf_sdbrcm_probe_malloc failed\n"); |
| 3990 | goto fail; |
| 3991 | } |
| 3992 | |
| 3993 | if (!(brcmf_sdbrcm_probe_init(bus))) { |
| 3994 | brcmf_dbg(ERROR, "brcmf_sdbrcm_probe_init failed\n"); |
| 3995 | goto fail; |
| 3996 | } |
| 3997 | |
| 3998 | /* Register interrupt callback, but mask it (not operational yet). */ |
| 3999 | brcmf_dbg(INTR, "disable SDIO interrupts (not interested yet)\n"); |
| 4000 | ret = brcmf_sdcard_intr_reg(bus->sdiodev); |
| 4001 | if (ret != 0) { |
| 4002 | brcmf_dbg(ERROR, "FAILED: sdcard_intr_reg returned %d\n", ret); |
| 4003 | goto fail; |
| 4004 | } |
| 4005 | brcmf_dbg(INTR, "registered SDIO interrupt function ok\n"); |
| 4006 | |
| 4007 | brcmf_dbg(INFO, "completed!!\n"); |
| 4008 | |
| 4009 | /* if firmware path present try to download and bring up bus */ |
| 4010 | ret = brcmf_bus_start(bus->drvr); |
| 4011 | if (ret != 0) { |
| 4012 | if (ret == -ENOLINK) { |
| 4013 | brcmf_dbg(ERROR, "dongle is not responding\n"); |
| 4014 | goto fail; |
| 4015 | } |
| 4016 | } |
Franky Lin | 15d45b6 | 2011-10-21 16:16:32 +0200 | [diff] [blame] | 4017 | |
| 4018 | /* add interface and open for business */ |
| 4019 | if (brcmf_add_if((struct brcmf_info *)bus->drvr, 0, "wlan%d", NULL)) { |
| 4020 | brcmf_dbg(ERROR, "Add primary net device interface failed!!\n"); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 4021 | goto fail; |
| 4022 | } |
| 4023 | |
| 4024 | return bus; |
| 4025 | |
| 4026 | fail: |
| 4027 | brcmf_sdbrcm_release(bus); |
| 4028 | return NULL; |
| 4029 | } |
| 4030 | |
| 4031 | void brcmf_sdbrcm_disconnect(void *ptr) |
| 4032 | { |
| 4033 | struct brcmf_bus *bus = (struct brcmf_bus *)ptr; |
| 4034 | |
| 4035 | brcmf_dbg(TRACE, "Enter\n"); |
| 4036 | |
| 4037 | if (bus) |
| 4038 | brcmf_sdbrcm_release(bus); |
| 4039 | |
| 4040 | brcmf_dbg(TRACE, "Disconnected\n"); |
| 4041 | } |
| 4042 | |
| 4043 | struct device *brcmf_bus_get_device(struct brcmf_bus *bus) |
| 4044 | { |
| 4045 | return &bus->sdiodev->func[2]->dev; |
| 4046 | } |
| 4047 | |
| 4048 | void |
| 4049 | brcmf_sdbrcm_wd_timer(struct brcmf_bus *bus, uint wdtick) |
| 4050 | { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 4051 | /* Totally stop the timer */ |
| 4052 | if (!wdtick && bus->wd_timer_valid == true) { |
| 4053 | del_timer_sync(&bus->timer); |
| 4054 | bus->wd_timer_valid = false; |
| 4055 | bus->save_ms = wdtick; |
| 4056 | return; |
| 4057 | } |
| 4058 | |
Franky Lin | ece960e | 2011-10-21 16:16:19 +0200 | [diff] [blame] | 4059 | /* don't start the wd until fw is loaded */ |
| 4060 | if (bus->drvr->busstate == BRCMF_BUS_DOWN) |
| 4061 | return; |
| 4062 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 4063 | if (wdtick) { |
| 4064 | if (bus->save_ms != BRCMF_WD_POLL_MS) { |
| 4065 | if (bus->wd_timer_valid == true) |
| 4066 | /* Stop timer and restart at new value */ |
| 4067 | del_timer_sync(&bus->timer); |
| 4068 | |
| 4069 | /* Create timer again when watchdog period is |
| 4070 | dynamically changed or in the first instance |
| 4071 | */ |
| 4072 | bus->timer.expires = |
| 4073 | jiffies + BRCMF_WD_POLL_MS * HZ / 1000; |
| 4074 | add_timer(&bus->timer); |
| 4075 | |
| 4076 | } else { |
| 4077 | /* Re arm the timer, at last watchdog period */ |
| 4078 | mod_timer(&bus->timer, |
| 4079 | jiffies + BRCMF_WD_POLL_MS * HZ / 1000); |
| 4080 | } |
| 4081 | |
| 4082 | bus->wd_timer_valid = true; |
| 4083 | bus->save_ms = wdtick; |
| 4084 | } |
| 4085 | } |