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