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