blob: 2c644a93c5bc71dd00882524c4ef3ad092049107 [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
Joe Perches02f77192012-01-15 00:38:44 -080017#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
Arend van Spriel5b435de2011-10-05 13:19:03 +020019#include <linux/types.h>
20#include <linux/kernel.h>
21#include <linux/kthread.h>
22#include <linux/printk.h>
23#include <linux/pci_ids.h>
24#include <linux/netdevice.h>
25#include <linux/interrupt.h>
26#include <linux/sched.h>
27#include <linux/mmc/sdio.h>
28#include <linux/mmc/sdio_func.h>
29#include <linux/mmc/card.h>
30#include <linux/semaphore.h>
31#include <linux/firmware.h>
Stephen Rothwellb7a57e72011-10-12 21:35:07 +020032#include <linux/module.h>
Franky Lin99ba15c2011-11-04 22:23:42 +010033#include <linux/bcma/bcma.h>
Arend van Spriel5b435de2011-10-05 13:19:03 +020034#include <asm/unaligned.h>
35#include <defs.h>
36#include <brcmu_wifi.h>
37#include <brcmu_utils.h>
38#include <brcm_hw_ids.h>
39#include <soc.h>
40#include "sdio_host.h"
Franky Lina83369b2011-11-04 22:23:28 +010041#include "sdio_chip.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020042
43#define DCMD_RESP_TIMEOUT 2000 /* In milli second */
44
Joe Perches8ae74652012-01-15 00:38:38 -080045#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +020046
47#define BRCMF_TRAP_INFO_SIZE 80
48
49#define CBUF_LEN (128)
50
51struct rte_log_le {
52 __le32 buf; /* Can't be pointer on (64-bit) hosts */
53 __le32 buf_size;
54 __le32 idx;
55 char *_buf_compat; /* Redundant pointer for backward compat. */
56};
57
58struct rte_console {
59 /* Virtual UART
60 * When there is no UART (e.g. Quickturn),
61 * the host should write a complete
62 * input line directly into cbuf and then write
63 * the length into vcons_in.
64 * This may also be used when there is a real UART
65 * (at risk of conflicting with
66 * the real UART). vcons_out is currently unused.
67 */
68 uint vcons_in;
69 uint vcons_out;
70
71 /* Output (logging) buffer
72 * Console output is written to a ring buffer log_buf at index log_idx.
73 * The host may read the output when it sees log_idx advance.
74 * Output will be lost if the output wraps around faster than the host
75 * polls.
76 */
77 struct rte_log_le log_le;
78
79 /* Console input line buffer
80 * Characters are read one at a time into cbuf
81 * until <CR> is received, then
82 * the buffer is processed as a command line.
83 * Also used for virtual UART.
84 */
85 uint cbuf_idx;
86 char cbuf[CBUF_LEN];
87};
88
Joe Perches8ae74652012-01-15 00:38:38 -080089#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +020090#include <chipcommon.h>
91
Arend van Spriel5b435de2011-10-05 13:19:03 +020092#include "dhd_bus.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020093#include "dhd_dbg.h"
Arend van Spriel5b435de2011-10-05 13:19:03 +020094
95#define TXQLEN 2048 /* bulk tx queue length */
96#define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
97#define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
98#define PRIOMASK 7
99
100#define TXRETRIES 2 /* # of retries for tx frames */
101
102#define BRCMF_RXBOUND 50 /* Default for max rx frames in
103 one scheduling */
104
105#define BRCMF_TXBOUND 20 /* Default for max tx frames in
106 one scheduling */
107
108#define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */
109
110#define MEMBLOCK 2048 /* Block size used for downloading
111 of dongle image */
112#define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
113 biggest possible glom */
114
115#define BRCMF_FIRSTREAD (1 << 6)
116
117
118/* SBSDIO_DEVICE_CTL */
119
120/* 1: device will assert busy signal when receiving CMD53 */
121#define SBSDIO_DEVCTL_SETBUSY 0x01
122/* 1: assertion of sdio interrupt is synchronous to the sdio clock */
123#define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02
124/* 1: mask all interrupts to host except the chipActive (rev 8) */
125#define SBSDIO_DEVCTL_CA_INT_ONLY 0x04
126/* 1: isolate internal sdio signals, put external pads in tri-state; requires
127 * sdio bus power cycle to clear (rev 9) */
128#define SBSDIO_DEVCTL_PADS_ISO 0x08
129/* Force SD->SB reset mapping (rev 11) */
130#define SBSDIO_DEVCTL_SB_RST_CTL 0x30
131/* Determined by CoreControl bit */
132#define SBSDIO_DEVCTL_RST_CORECTL 0x00
133/* Force backplane reset */
134#define SBSDIO_DEVCTL_RST_BPRESET 0x10
135/* Force no backplane reset */
136#define SBSDIO_DEVCTL_RST_NOBPRESET 0x20
137
Arend van Spriel5b435de2011-10-05 13:19:03 +0200138/* direct(mapped) cis space */
139
140/* MAPPED common CIS address */
141#define SBSDIO_CIS_BASE_COMMON 0x1000
142/* maximum bytes in one CIS */
143#define SBSDIO_CIS_SIZE_LIMIT 0x200
144/* cis offset addr is < 17 bits */
145#define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF
146
147/* manfid tuple length, include tuple, link bytes */
148#define SBSDIO_CIS_MANFID_TUPLE_LEN 6
149
150/* intstatus */
151#define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */
152#define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */
153#define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */
154#define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */
155#define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */
156#define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */
157#define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */
158#define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */
159#define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */
160#define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */
161#define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */
162#define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */
163#define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */
164#define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */
165#define I_PC (1 << 10) /* descriptor error */
166#define I_PD (1 << 11) /* data error */
167#define I_DE (1 << 12) /* Descriptor protocol Error */
168#define I_RU (1 << 13) /* Receive descriptor Underflow */
169#define I_RO (1 << 14) /* Receive fifo Overflow */
170#define I_XU (1 << 15) /* Transmit fifo Underflow */
171#define I_RI (1 << 16) /* Receive Interrupt */
172#define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */
173#define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */
174#define I_XI (1 << 24) /* Transmit Interrupt */
175#define I_RF_TERM (1 << 25) /* Read Frame Terminate */
176#define I_WF_TERM (1 << 26) /* Write Frame Terminate */
177#define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */
178#define I_SBINT (1 << 28) /* sbintstatus Interrupt */
179#define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */
180#define I_SRESET (1 << 30) /* CCCR RES interrupt */
181#define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */
182#define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
183#define I_DMA (I_RI | I_XI | I_ERRORS)
184
185/* corecontrol */
186#define CC_CISRDY (1 << 0) /* CIS Ready */
187#define CC_BPRESEN (1 << 1) /* CCCR RES signal */
188#define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */
189#define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */
190#define CC_XMTDATAAVAIL_MODE (1 << 4)
191#define CC_XMTDATAAVAIL_CTRL (1 << 5)
192
193/* SDA_FRAMECTRL */
194#define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */
195#define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */
196#define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */
197#define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */
198
199/* HW frame tag */
200#define SDPCM_FRAMETAG_LEN 4 /* 2 bytes len, 2 bytes check val */
201
202/* Total length of frame header for dongle protocol */
203#define SDPCM_HDRLEN (SDPCM_FRAMETAG_LEN + SDPCM_SWHEADER_LEN)
204#define SDPCM_RESERVE (SDPCM_HDRLEN + BRCMF_SDALIGN)
205
206/*
207 * Software allocation of To SB Mailbox resources
208 */
209
210/* tosbmailbox bits corresponding to intstatus bits */
211#define SMB_NAK (1 << 0) /* Frame NAK */
212#define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
213#define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
214#define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
215
216/* tosbmailboxdata */
217#define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
218
219/*
220 * Software allocation of To Host Mailbox resources
221 */
222
223/* intstatus bits */
224#define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
225#define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
226#define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
227#define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
228
229/* tohostmailboxdata */
230#define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
231#define HMB_DATA_DEVREADY 2 /* talk to host after enable */
232#define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
233#define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
234
235#define HMB_DATA_FCDATA_MASK 0xff000000
236#define HMB_DATA_FCDATA_SHIFT 24
237
238#define HMB_DATA_VERSION_MASK 0x00ff0000
239#define HMB_DATA_VERSION_SHIFT 16
240
241/*
242 * Software-defined protocol header
243 */
244
245/* Current protocol version */
246#define SDPCM_PROT_VERSION 4
247
248/* SW frame header */
249#define SDPCM_PACKET_SEQUENCE(p) (((u8 *)p)[0] & 0xff)
250
251#define SDPCM_CHANNEL_MASK 0x00000f00
252#define SDPCM_CHANNEL_SHIFT 8
253#define SDPCM_PACKET_CHANNEL(p) (((u8 *)p)[1] & 0x0f)
254
255#define SDPCM_NEXTLEN_OFFSET 2
256
257/* Data Offset from SOF (HW Tag, SW Tag, Pad) */
258#define SDPCM_DOFFSET_OFFSET 3 /* Data Offset */
259#define SDPCM_DOFFSET_VALUE(p) (((u8 *)p)[SDPCM_DOFFSET_OFFSET] & 0xff)
260#define SDPCM_DOFFSET_MASK 0xff000000
261#define SDPCM_DOFFSET_SHIFT 24
262#define SDPCM_FCMASK_OFFSET 4 /* Flow control */
263#define SDPCM_FCMASK_VALUE(p) (((u8 *)p)[SDPCM_FCMASK_OFFSET] & 0xff)
264#define SDPCM_WINDOW_OFFSET 5 /* Credit based fc */
265#define SDPCM_WINDOW_VALUE(p) (((u8 *)p)[SDPCM_WINDOW_OFFSET] & 0xff)
266
267#define SDPCM_SWHEADER_LEN 8 /* SW header is 64 bits */
268
269/* logical channel numbers */
270#define SDPCM_CONTROL_CHANNEL 0 /* Control channel Id */
271#define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication Channel Id */
272#define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv Channel Id */
273#define SDPCM_GLOM_CHANNEL 3 /* For coalesced packets */
274#define SDPCM_TEST_CHANNEL 15 /* Reserved for test/debug packets */
275
276#define SDPCM_SEQUENCE_WRAP 256 /* wrap-around val for 8bit frame seq */
277
278#define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
279
280/*
281 * Shared structure between dongle and the host.
282 * The structure contains pointers to trap or assert information.
283 */
284#define SDPCM_SHARED_VERSION 0x0002
285#define SDPCM_SHARED_VERSION_MASK 0x00FF
286#define SDPCM_SHARED_ASSERT_BUILT 0x0100
287#define SDPCM_SHARED_ASSERT 0x0200
288#define SDPCM_SHARED_TRAP 0x0400
289
290/* Space for header read, limit for data packets */
291#define MAX_HDR_READ (1 << 6)
292#define MAX_RX_DATASZ 2048
293
294/* Maximum milliseconds to wait for F2 to come up */
295#define BRCMF_WAIT_F2RDY 3000
296
297/* Bump up limit on waiting for HT to account for first startup;
298 * if the image is doing a CRC calculation before programming the PMU
299 * for HT availability, it could take a couple hundred ms more, so
300 * max out at a 1 second (1000000us).
301 */
302#undef PMU_MAX_TRANSITION_DLY
303#define PMU_MAX_TRANSITION_DLY 1000000
304
305/* Value for ChipClockCSR during initial setup */
306#define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
307 SBSDIO_ALP_AVAIL_REQ)
308
309/* Flags for SDH calls */
310#define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
311
Arend van Spriel52e14092012-02-09 21:09:09 +0100312#define BRCMF_SDIO_FW_NAME "brcm/brcmfmac-sdio.bin"
313#define BRCMF_SDIO_NV_NAME "brcm/brcmfmac-sdio.txt"
314MODULE_FIRMWARE(BRCMF_SDIO_FW_NAME);
315MODULE_FIRMWARE(BRCMF_SDIO_NV_NAME);
Franky Lin8dd939c2011-11-22 17:21:41 -0800316
Franky Lin382a9e02011-12-16 18:37:18 -0800317#define BRCMF_IDLE_IMMEDIATE (-1) /* Enter idle immediately */
318#define BRCMF_IDLE_ACTIVE 0 /* Do not request any SD clock change
319 * when idle
320 */
321#define BRCMF_IDLE_INTERVAL 1
322
Arend van Spriel5b435de2011-10-05 13:19:03 +0200323/*
324 * Conversion of 802.1D priority to precedence level
325 */
326static uint prio2prec(u32 prio)
327{
328 return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ?
329 (prio^2) : prio;
330}
331
Arend van Spriel5b435de2011-10-05 13:19:03 +0200332/* core registers */
333struct sdpcmd_regs {
334 u32 corecontrol; /* 0x00, rev8 */
335 u32 corestatus; /* rev8 */
336 u32 PAD[1];
337 u32 biststatus; /* rev8 */
338
339 /* PCMCIA access */
340 u16 pcmciamesportaladdr; /* 0x010, rev8 */
341 u16 PAD[1];
342 u16 pcmciamesportalmask; /* rev8 */
343 u16 PAD[1];
344 u16 pcmciawrframebc; /* rev8 */
345 u16 PAD[1];
346 u16 pcmciaunderflowtimer; /* rev8 */
347 u16 PAD[1];
348
349 /* interrupt */
350 u32 intstatus; /* 0x020, rev8 */
351 u32 hostintmask; /* rev8 */
352 u32 intmask; /* rev8 */
353 u32 sbintstatus; /* rev8 */
354 u32 sbintmask; /* rev8 */
355 u32 funcintmask; /* rev4 */
356 u32 PAD[2];
357 u32 tosbmailbox; /* 0x040, rev8 */
358 u32 tohostmailbox; /* rev8 */
359 u32 tosbmailboxdata; /* rev8 */
360 u32 tohostmailboxdata; /* rev8 */
361
362 /* synchronized access to registers in SDIO clock domain */
363 u32 sdioaccess; /* 0x050, rev8 */
364 u32 PAD[3];
365
366 /* PCMCIA frame control */
367 u8 pcmciaframectrl; /* 0x060, rev8 */
368 u8 PAD[3];
369 u8 pcmciawatermark; /* rev8 */
370 u8 PAD[155];
371
372 /* interrupt batching control */
373 u32 intrcvlazy; /* 0x100, rev8 */
374 u32 PAD[3];
375
376 /* counters */
377 u32 cmd52rd; /* 0x110, rev8 */
378 u32 cmd52wr; /* rev8 */
379 u32 cmd53rd; /* rev8 */
380 u32 cmd53wr; /* rev8 */
381 u32 abort; /* rev8 */
382 u32 datacrcerror; /* rev8 */
383 u32 rdoutofsync; /* rev8 */
384 u32 wroutofsync; /* rev8 */
385 u32 writebusy; /* rev8 */
386 u32 readwait; /* rev8 */
387 u32 readterm; /* rev8 */
388 u32 writeterm; /* rev8 */
389 u32 PAD[40];
390 u32 clockctlstatus; /* rev8 */
391 u32 PAD[7];
392
393 u32 PAD[128]; /* DMA engines */
394
395 /* SDIO/PCMCIA CIS region */
396 char cis[512]; /* 0x400-0x5ff, rev6 */
397
398 /* PCMCIA function control registers */
399 char pcmciafcr[256]; /* 0x600-6ff, rev6 */
400 u16 PAD[55];
401
402 /* PCMCIA backplane access */
403 u16 backplanecsr; /* 0x76E, rev6 */
404 u16 backplaneaddr0; /* rev6 */
405 u16 backplaneaddr1; /* rev6 */
406 u16 backplaneaddr2; /* rev6 */
407 u16 backplaneaddr3; /* rev6 */
408 u16 backplanedata0; /* rev6 */
409 u16 backplanedata1; /* rev6 */
410 u16 backplanedata2; /* rev6 */
411 u16 backplanedata3; /* rev6 */
412 u16 PAD[31];
413
414 /* sprom "size" & "blank" info */
415 u16 spromstatus; /* 0x7BE, rev2 */
416 u32 PAD[464];
417
418 u16 PAD[0x80];
419};
420
Joe Perches8ae74652012-01-15 00:38:38 -0800421#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200422/* Device console log buffer state */
423struct brcmf_console {
424 uint count; /* Poll interval msec counter */
425 uint log_addr; /* Log struct address (fixed) */
426 struct rte_log_le log_le; /* Log struct (host copy) */
427 uint bufsize; /* Size of log buffer */
428 u8 *buf; /* Log buffer (host copy) */
429 uint last; /* Last buffer read index */
430};
Joe Perches8ae74652012-01-15 00:38:38 -0800431#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200432
433struct sdpcm_shared {
434 u32 flags;
435 u32 trap_addr;
436 u32 assert_exp_addr;
437 u32 assert_file_addr;
438 u32 assert_line;
439 u32 console_addr; /* Address of struct rte_console */
440 u32 msgtrace_addr;
441 u8 tag[32];
442};
443
444struct sdpcm_shared_le {
445 __le32 flags;
446 __le32 trap_addr;
447 __le32 assert_exp_addr;
448 __le32 assert_file_addr;
449 __le32 assert_line;
450 __le32 console_addr; /* Address of struct rte_console */
451 __le32 msgtrace_addr;
452 u8 tag[32];
453};
454
455
456/* misc chip info needed by some of the routines */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200457/* Private data for SDIO bus interaction */
Franky Line92eedf2011-11-22 17:21:50 -0800458struct brcmf_sdio {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200459 struct brcmf_sdio_dev *sdiodev; /* sdio device handler */
460 struct chip_info *ci; /* Chip info struct */
461 char *vars; /* Variables (from CIS and/or other) */
462 uint varsz; /* Size of variables buffer */
463
464 u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
465
466 u32 hostintmask; /* Copy of Host Interrupt Mask */
467 u32 intstatus; /* Intstatus bits (events) pending */
468 bool dpc_sched; /* Indicates DPC schedule (intrpt rcvd) */
469 bool fcstate; /* State of dongle flow-control */
470
471 uint blocksize; /* Block size of SDIO transfers */
472 uint roundup; /* Max roundup limit */
473
474 struct pktq txq; /* Queue length used for flow-control */
475 u8 flowcontrol; /* per prio flow control bitmask */
476 u8 tx_seq; /* Transmit sequence number (next) */
477 u8 tx_max; /* Maximum transmit sequence allowed */
478
479 u8 hdrbuf[MAX_HDR_READ + BRCMF_SDALIGN];
480 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
481 u16 nextlen; /* Next Read Len from last header */
482 u8 rx_seq; /* Receive sequence number (expected) */
483 bool rxskip; /* Skip receive (awaiting NAK ACK) */
484
485 uint rxbound; /* Rx frames to read before resched */
486 uint txbound; /* Tx frames to send before resched */
487 uint txminmax;
488
489 struct sk_buff *glomd; /* Packet containing glomming descriptor */
Arend van Sprielb83db862011-10-19 12:51:09 +0200490 struct sk_buff_head glom; /* Packet list for glommed superframe */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200491 uint glomerr; /* Glom packet read errors */
492
493 u8 *rxbuf; /* Buffer for receiving control packets */
494 uint rxblen; /* Allocated length of rxbuf */
495 u8 *rxctl; /* Aligned pointer into rxbuf */
496 u8 *databuf; /* Buffer for receiving big glom packet */
497 u8 *dataptr; /* Aligned pointer into databuf */
498 uint rxlen; /* Length of valid data in buffer */
499
500 u8 sdpcm_ver; /* Bus protocol reported by dongle */
501
502 bool intr; /* Use interrupts */
503 bool poll; /* Use polling */
504 bool ipend; /* Device interrupt is pending */
505 uint intrcount; /* Count of device interrupt callbacks */
506 uint lastintrs; /* Count as of last watchdog timer */
507 uint spurious; /* Count of spurious interrupts */
508 uint pollrate; /* Ticks between device polls */
509 uint polltick; /* Tick counter */
510 uint pollcnt; /* Count of active polls */
511
Joe Perches8ae74652012-01-15 00:38:38 -0800512#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200513 uint console_interval;
514 struct brcmf_console console; /* Console output polling support */
515 uint console_addr; /* Console address from shared struct */
Joe Perches8ae74652012-01-15 00:38:38 -0800516#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200517
518 uint regfails; /* Count of R_REG failures */
519
520 uint clkstate; /* State of sd and backplane clock(s) */
521 bool activity; /* Activity flag for clock down */
522 s32 idletime; /* Control for activity timeout */
523 s32 idlecount; /* Activity timeout counter */
524 s32 idleclock; /* How to set bus driver when idle */
525 s32 sd_rxchain;
526 bool use_rxchain; /* If brcmf should use PKT chains */
527 bool sleeping; /* Is SDIO bus sleeping? */
528 bool rxflow_mode; /* Rx flow control mode */
529 bool rxflow; /* Is rx flow control on */
530 bool alp_only; /* Don't use HT clock (ALP only) */
531/* Field to decide if rx of control frames happen in rxbuf or lb-pool */
532 bool usebufpool;
533
534 /* Some additional counters */
535 uint tx_sderrs; /* Count of tx attempts with sd errors */
536 uint fcqueued; /* Tx packets that got queued */
537 uint rxrtx; /* Count of rtx requests (NAK to dongle) */
538 uint rx_toolong; /* Receive frames too long to receive */
539 uint rxc_errors; /* SDIO errors when reading control frames */
540 uint rx_hdrfail; /* SDIO errors on header reads */
541 uint rx_badhdr; /* Bad received headers (roosync?) */
542 uint rx_badseq; /* Mismatched rx sequence number */
543 uint fc_rcvd; /* Number of flow-control events received */
544 uint fc_xoff; /* Number which turned on flow-control */
545 uint fc_xon; /* Number which turned off flow-control */
546 uint rxglomfail; /* Failed deglom attempts */
547 uint rxglomframes; /* Number of glom frames (superframes) */
548 uint rxglompkts; /* Number of packets from glom frames */
549 uint f2rxhdrs; /* Number of header reads */
550 uint f2rxdata; /* Number of frame data reads */
551 uint f2txdata; /* Number of f2 frame writes */
552 uint f1regdata; /* Number of f1 register accesses */
Franky Lin28a1a3b2011-12-16 18:37:02 -0800553 uint tickcnt; /* Number of watchdog been schedule */
554 unsigned long tx_ctlerrs; /* Err of sending ctrl frames */
555 unsigned long tx_ctlpkts; /* Ctrl frames sent to dongle */
556 unsigned long rx_ctlerrs; /* Err of processing rx ctrl frames */
557 unsigned long rx_ctlpkts; /* Ctrl frames processed from dongle */
558 unsigned long rx_readahead_cnt; /* Number of packets where header
559 * read-ahead was used. */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200560
561 u8 *ctrl_frame_buf;
562 u32 ctrl_frame_len;
563 bool ctrl_frame_stat;
564
565 spinlock_t txqlock;
566 wait_queue_head_t ctrl_wait;
567 wait_queue_head_t dcmd_resp_wait;
568
569 struct timer_list timer;
570 struct completion watchdog_wait;
571 struct task_struct *watchdog_tsk;
572 bool wd_timer_valid;
573 uint save_ms;
574
575 struct task_struct *dpc_tsk;
576 struct completion dpc_wait;
Franky Linb948a852012-04-23 14:24:53 -0700577 struct list_head dpc_tsklst;
578 spinlock_t dpc_tl_lock;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200579
580 struct semaphore sdsem;
581
Arend van Spriel5b435de2011-10-05 13:19:03 +0200582 const struct firmware *firmware;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200583 u32 fw_ptr;
Franky Linc8bf3482011-12-16 18:37:08 -0800584
585 bool txoff; /* Transmit flow-controlled */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200586};
587
Arend van Spriel5b435de2011-10-05 13:19:03 +0200588/* clkstate */
589#define CLK_NONE 0
590#define CLK_SDONLY 1
591#define CLK_PENDING 2 /* Not used yet */
592#define CLK_AVAIL 3
593
Joe Perches8ae74652012-01-15 00:38:38 -0800594#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200595static int qcount[NUMPRIO];
596static int tx_packets[NUMPRIO];
Joe Perches8ae74652012-01-15 00:38:38 -0800597#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200598
599#define SDIO_DRIVE_STRENGTH 6 /* in milliamps */
600
601#define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
602
603/* Retry count for register access failures */
604static const uint retry_limit = 2;
605
606/* Limit on rounding up frames */
607static const uint max_roundup = 512;
608
609#define ALIGNMENT 4
610
611static void pkt_align(struct sk_buff *p, int len, int align)
612{
613 uint datalign;
614 datalign = (unsigned long)(p->data);
615 datalign = roundup(datalign, (align)) - datalign;
616 if (datalign)
617 skb_pull(p, datalign);
618 __skb_trim(p, len);
619}
620
621/* To check if there's window offered */
Franky Line92eedf2011-11-22 17:21:50 -0800622static bool data_ok(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200623{
624 return (u8)(bus->tx_max - bus->tx_seq) != 0 &&
625 ((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0;
626}
627
628/*
629 * Reads a register in the SDIO hardware block. This block occupies a series of
630 * adresses on the 32 bit backplane bus.
631 */
632static void
Franky Lin79ae3952012-05-04 18:27:34 -0700633r_sdreg32(struct brcmf_sdio *bus, u32 *regvar, u32 offset, u32 *retryvar)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200634{
Franky Lin99ba15c2011-11-04 22:23:42 +0100635 u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
Franky Lin79ae3952012-05-04 18:27:34 -0700636 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200637 *retryvar = 0;
638 do {
Franky Lin79ae3952012-05-04 18:27:34 -0700639 *regvar = brcmf_sdio_regrl(bus->sdiodev,
640 bus->ci->c_inf[idx].base + offset,
641 &ret);
642 } while ((ret != 0) && (++(*retryvar) <= retry_limit));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200643 if (*retryvar) {
644 bus->regfails += (*retryvar-1);
645 if (*retryvar > retry_limit) {
Franky Lin79ae3952012-05-04 18:27:34 -0700646 brcmf_dbg(ERROR, "FAILED READ %Xh\n", offset);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200647 *regvar = 0;
648 }
649 }
650}
651
652static void
Franky Line92eedf2011-11-22 17:21:50 -0800653w_sdreg32(struct brcmf_sdio *bus, u32 regval, u32 reg_offset, u32 *retryvar)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200654{
Franky Lin99ba15c2011-11-04 22:23:42 +0100655 u8 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
Franky Line13ce262012-05-04 18:27:35 -0700656 int ret;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200657 *retryvar = 0;
658 do {
Franky Line13ce262012-05-04 18:27:35 -0700659 brcmf_sdio_regwl(bus->sdiodev,
660 bus->ci->c_inf[idx].base + reg_offset,
661 regval, &ret);
662 } while ((ret != 0) && (++(*retryvar) <= retry_limit));
Arend van Spriel5b435de2011-10-05 13:19:03 +0200663 if (*retryvar) {
664 bus->regfails += (*retryvar-1);
665 if (*retryvar > retry_limit)
666 brcmf_dbg(ERROR, "FAILED REGISTER WRITE %Xh\n",
667 reg_offset);
668 }
669}
670
671#define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
672
673#define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
674
675/* Packet free applicable unconditionally for sdio and sdspi.
676 * Conditional if bufpool was present for gspi bus.
677 */
Franky Line92eedf2011-11-22 17:21:50 -0800678static void brcmf_sdbrcm_pktfree2(struct brcmf_sdio *bus, struct sk_buff *pkt)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200679{
680 if (bus->usebufpool)
681 brcmu_pkt_buf_free_skb(pkt);
682}
683
684/* Turn backplane clock on or off */
Franky Line92eedf2011-11-22 17:21:50 -0800685static int brcmf_sdbrcm_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200686{
687 int err;
688 u8 clkctl, clkreq, devctl;
689 unsigned long timeout;
690
691 brcmf_dbg(TRACE, "Enter\n");
692
693 clkctl = 0;
694
695 if (on) {
696 /* Request HT Avail */
697 clkreq =
698 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
699
Franky Lin3bba8292012-05-04 18:27:33 -0700700 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
701 clkreq, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200702 if (err) {
703 brcmf_dbg(ERROR, "HT Avail request error: %d\n", err);
704 return -EBADE;
705 }
706
Arend van Spriel5b435de2011-10-05 13:19:03 +0200707 /* Check current status */
Franky Lin45db3392012-05-04 18:27:32 -0700708 clkctl = brcmf_sdio_regrb(bus->sdiodev,
709 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200710 if (err) {
711 brcmf_dbg(ERROR, "HT Avail read error: %d\n", err);
712 return -EBADE;
713 }
714
715 /* Go to pending and await interrupt if appropriate */
716 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
717 /* Allow only clock-available interrupt */
Franky Lin45db3392012-05-04 18:27:32 -0700718 devctl = brcmf_sdio_regrb(bus->sdiodev,
719 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200720 if (err) {
721 brcmf_dbg(ERROR, "Devctl error setting CA: %d\n",
722 err);
723 return -EBADE;
724 }
725
726 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
Franky Lin3bba8292012-05-04 18:27:33 -0700727 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
728 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200729 brcmf_dbg(INFO, "CLKCTL: set PENDING\n");
730 bus->clkstate = CLK_PENDING;
731
732 return 0;
733 } else if (bus->clkstate == CLK_PENDING) {
734 /* Cancel CA-only interrupt filter */
Franky Lin45db3392012-05-04 18:27:32 -0700735 devctl = brcmf_sdio_regrb(bus->sdiodev,
Arend van Spriel5b435de2011-10-05 13:19:03 +0200736 SBSDIO_DEVICE_CTL, &err);
737 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Franky Lin3bba8292012-05-04 18:27:33 -0700738 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
739 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200740 }
741
742 /* Otherwise, wait here (polling) for HT Avail */
743 timeout = jiffies +
744 msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
745 while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
Franky Lin45db3392012-05-04 18:27:32 -0700746 clkctl = brcmf_sdio_regrb(bus->sdiodev,
747 SBSDIO_FUNC1_CHIPCLKCSR,
748 &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200749 if (time_after(jiffies, timeout))
750 break;
751 else
752 usleep_range(5000, 10000);
753 }
754 if (err) {
755 brcmf_dbg(ERROR, "HT Avail request error: %d\n", err);
756 return -EBADE;
757 }
758 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
759 brcmf_dbg(ERROR, "HT Avail timeout (%d): clkctl 0x%02x\n",
760 PMU_MAX_TRANSITION_DLY, clkctl);
761 return -EBADE;
762 }
763
764 /* Mark clock available */
765 bus->clkstate = CLK_AVAIL;
766 brcmf_dbg(INFO, "CLKCTL: turned ON\n");
767
Joe Perches8ae74652012-01-15 00:38:38 -0800768#if defined(DEBUG)
Joe Perches23677ce2012-02-09 11:17:23 +0000769 if (!bus->alp_only) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200770 if (SBSDIO_ALPONLY(clkctl))
771 brcmf_dbg(ERROR, "HT Clock should be on\n");
772 }
Joe Perches8ae74652012-01-15 00:38:38 -0800773#endif /* defined (DEBUG) */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200774
775 bus->activity = true;
776 } else {
777 clkreq = 0;
778
779 if (bus->clkstate == CLK_PENDING) {
780 /* Cancel CA-only interrupt filter */
Franky Lin45db3392012-05-04 18:27:32 -0700781 devctl = brcmf_sdio_regrb(bus->sdiodev,
782 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200783 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Franky Lin3bba8292012-05-04 18:27:33 -0700784 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
785 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200786 }
787
788 bus->clkstate = CLK_SDONLY;
Franky Lin3bba8292012-05-04 18:27:33 -0700789 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
790 clkreq, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200791 brcmf_dbg(INFO, "CLKCTL: turned OFF\n");
792 if (err) {
793 brcmf_dbg(ERROR, "Failed access turning clock off: %d\n",
794 err);
795 return -EBADE;
796 }
797 }
798 return 0;
799}
800
801/* Change idle/active SD state */
Franky Line92eedf2011-11-22 17:21:50 -0800802static int brcmf_sdbrcm_sdclk(struct brcmf_sdio *bus, bool on)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200803{
804 brcmf_dbg(TRACE, "Enter\n");
805
806 if (on)
807 bus->clkstate = CLK_SDONLY;
808 else
809 bus->clkstate = CLK_NONE;
810
811 return 0;
812}
813
814/* Transition SD and backplane clock readiness */
Franky Line92eedf2011-11-22 17:21:50 -0800815static int brcmf_sdbrcm_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200816{
Joe Perches8ae74652012-01-15 00:38:38 -0800817#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200818 uint oldstate = bus->clkstate;
Joe Perches8ae74652012-01-15 00:38:38 -0800819#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200820
821 brcmf_dbg(TRACE, "Enter\n");
822
823 /* Early exit if we're already there */
824 if (bus->clkstate == target) {
825 if (target == CLK_AVAIL) {
826 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
827 bus->activity = true;
828 }
829 return 0;
830 }
831
832 switch (target) {
833 case CLK_AVAIL:
834 /* Make sure SD clock is available */
835 if (bus->clkstate == CLK_NONE)
836 brcmf_sdbrcm_sdclk(bus, true);
837 /* Now request HT Avail on the backplane */
838 brcmf_sdbrcm_htclk(bus, true, pendok);
839 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
840 bus->activity = true;
841 break;
842
843 case CLK_SDONLY:
844 /* Remove HT request, or bring up SD clock */
845 if (bus->clkstate == CLK_NONE)
846 brcmf_sdbrcm_sdclk(bus, true);
847 else if (bus->clkstate == CLK_AVAIL)
848 brcmf_sdbrcm_htclk(bus, false, false);
849 else
850 brcmf_dbg(ERROR, "request for %d -> %d\n",
851 bus->clkstate, target);
852 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
853 break;
854
855 case CLK_NONE:
856 /* Make sure to remove HT request */
857 if (bus->clkstate == CLK_AVAIL)
858 brcmf_sdbrcm_htclk(bus, false, false);
859 /* Now remove the SD clock */
860 brcmf_sdbrcm_sdclk(bus, false);
861 brcmf_sdbrcm_wd_timer(bus, 0);
862 break;
863 }
Joe Perches8ae74652012-01-15 00:38:38 -0800864#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200865 brcmf_dbg(INFO, "%d -> %d\n", oldstate, bus->clkstate);
Joe Perches8ae74652012-01-15 00:38:38 -0800866#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200867
868 return 0;
869}
870
Franky Line92eedf2011-11-22 17:21:50 -0800871static int brcmf_sdbrcm_bussleep(struct brcmf_sdio *bus, bool sleep)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200872{
873 uint retries = 0;
874
875 brcmf_dbg(INFO, "request %s (currently %s)\n",
876 sleep ? "SLEEP" : "WAKE",
877 bus->sleeping ? "SLEEP" : "WAKE");
878
879 /* Done if we're already in the requested state */
880 if (sleep == bus->sleeping)
881 return 0;
882
883 /* Going to sleep: set the alarm and turn off the lights... */
884 if (sleep) {
885 /* Don't sleep if something is pending */
886 if (bus->dpc_sched || bus->rxskip || pktq_len(&bus->txq))
887 return -EBUSY;
888
889 /* Make sure the controller has the bus up */
890 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
891
892 /* Tell device to start using OOB wakeup */
893 w_sdreg32(bus, SMB_USE_OOB,
894 offsetof(struct sdpcmd_regs, tosbmailbox), &retries);
895 if (retries > retry_limit)
896 brcmf_dbg(ERROR, "CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n");
897
898 /* Turn off our contribution to the HT clock request */
899 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
900
Franky Lin3bba8292012-05-04 18:27:33 -0700901 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
902 SBSDIO_FORCE_HW_CLKREQ_OFF, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200903
904 /* Isolate the bus */
Franky Lin3bba8292012-05-04 18:27:33 -0700905 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
906 SBSDIO_DEVCTL_PADS_ISO, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200907
908 /* Change state */
909 bus->sleeping = true;
910
911 } else {
912 /* Waking up: bus power up is ok, set local state */
913
Franky Lin3bba8292012-05-04 18:27:33 -0700914 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
915 0, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200916
Arend van Spriel5b435de2011-10-05 13:19:03 +0200917 /* Make sure the controller has the bus up */
918 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
919
920 /* Send misc interrupt to indicate OOB not needed */
921 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, tosbmailboxdata),
922 &retries);
923 if (retries <= retry_limit)
924 w_sdreg32(bus, SMB_DEV_INT,
925 offsetof(struct sdpcmd_regs, tosbmailbox),
926 &retries);
927
928 if (retries > retry_limit)
929 brcmf_dbg(ERROR, "CANNOT SIGNAL CHIP TO CLEAR OOB!!\n");
930
931 /* Make sure we have SD bus access */
932 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
933
934 /* Change state */
935 bus->sleeping = false;
936 }
937
938 return 0;
939}
940
Franky Line92eedf2011-11-22 17:21:50 -0800941static void bus_wake(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200942{
943 if (bus->sleeping)
944 brcmf_sdbrcm_bussleep(bus, false);
945}
946
Franky Line92eedf2011-11-22 17:21:50 -0800947static u32 brcmf_sdbrcm_hostmail(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +0200948{
949 u32 intstatus = 0;
950 u32 hmb_data;
951 u8 fcbits;
952 uint retries = 0;
953
954 brcmf_dbg(TRACE, "Enter\n");
955
956 /* Read mailbox data and ack that we did so */
957 r_sdreg32(bus, &hmb_data,
958 offsetof(struct sdpcmd_regs, tohostmailboxdata), &retries);
959
960 if (retries <= retry_limit)
961 w_sdreg32(bus, SMB_INT_ACK,
962 offsetof(struct sdpcmd_regs, tosbmailbox), &retries);
963 bus->f1regdata += 2;
964
965 /* Dongle recomposed rx frames, accept them again */
966 if (hmb_data & HMB_DATA_NAKHANDLED) {
967 brcmf_dbg(INFO, "Dongle reports NAK handled, expect rtx of %d\n",
968 bus->rx_seq);
969 if (!bus->rxskip)
970 brcmf_dbg(ERROR, "unexpected NAKHANDLED!\n");
971
972 bus->rxskip = false;
973 intstatus |= I_HMB_FRAME_IND;
974 }
975
976 /*
977 * DEVREADY does not occur with gSPI.
978 */
979 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
980 bus->sdpcm_ver =
981 (hmb_data & HMB_DATA_VERSION_MASK) >>
982 HMB_DATA_VERSION_SHIFT;
983 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
984 brcmf_dbg(ERROR, "Version mismatch, dongle reports %d, "
985 "expecting %d\n",
986 bus->sdpcm_ver, SDPCM_PROT_VERSION);
987 else
988 brcmf_dbg(INFO, "Dongle ready, protocol version %d\n",
989 bus->sdpcm_ver);
990 }
991
992 /*
993 * Flow Control has been moved into the RX headers and this out of band
994 * method isn't used any more.
995 * remaining backward compatible with older dongles.
996 */
997 if (hmb_data & HMB_DATA_FC) {
998 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
999 HMB_DATA_FCDATA_SHIFT;
1000
1001 if (fcbits & ~bus->flowcontrol)
1002 bus->fc_xoff++;
1003
1004 if (bus->flowcontrol & ~fcbits)
1005 bus->fc_xon++;
1006
1007 bus->fc_rcvd++;
1008 bus->flowcontrol = fcbits;
1009 }
1010
1011 /* Shouldn't be any others */
1012 if (hmb_data & ~(HMB_DATA_DEVREADY |
1013 HMB_DATA_NAKHANDLED |
1014 HMB_DATA_FC |
1015 HMB_DATA_FWREADY |
1016 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
1017 brcmf_dbg(ERROR, "Unknown mailbox data content: 0x%02x\n",
1018 hmb_data);
1019
1020 return intstatus;
1021}
1022
Franky Line92eedf2011-11-22 17:21:50 -08001023static void brcmf_sdbrcm_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001024{
1025 uint retries = 0;
1026 u16 lastrbc;
1027 u8 hi, lo;
1028 int err;
1029
1030 brcmf_dbg(ERROR, "%sterminate frame%s\n",
1031 abort ? "abort command, " : "",
1032 rtx ? ", send NAK" : "");
1033
1034 if (abort)
1035 brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
1036
Franky Lin3bba8292012-05-04 18:27:33 -07001037 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
1038 SFC_RF_TERM, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001039 bus->f1regdata++;
1040
1041 /* Wait until the packet has been flushed (device/FIFO stable) */
1042 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
Franky Lin45db3392012-05-04 18:27:32 -07001043 hi = brcmf_sdio_regrb(bus->sdiodev,
1044 SBSDIO_FUNC1_RFRAMEBCHI, NULL);
1045 lo = brcmf_sdio_regrb(bus->sdiodev,
1046 SBSDIO_FUNC1_RFRAMEBCLO, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001047 bus->f1regdata += 2;
1048
1049 if ((hi == 0) && (lo == 0))
1050 break;
1051
1052 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
1053 brcmf_dbg(ERROR, "count growing: last 0x%04x now 0x%04x\n",
1054 lastrbc, (hi << 8) + lo);
1055 }
1056 lastrbc = (hi << 8) + lo;
1057 }
1058
1059 if (!retries)
1060 brcmf_dbg(ERROR, "count never zeroed: last 0x%04x\n", lastrbc);
1061 else
1062 brcmf_dbg(INFO, "flush took %d iterations\n", 0xffff - retries);
1063
1064 if (rtx) {
1065 bus->rxrtx++;
1066 w_sdreg32(bus, SMB_NAK,
1067 offsetof(struct sdpcmd_regs, tosbmailbox), &retries);
1068
1069 bus->f1regdata++;
1070 if (retries <= retry_limit)
1071 bus->rxskip = true;
1072 }
1073
1074 /* Clear partial in any case */
1075 bus->nextlen = 0;
1076
1077 /* If we can't reach the device, signal failure */
1078 if (err || brcmf_sdcard_regfail(bus->sdiodev))
Franky Lin712ac5b2011-12-16 18:37:09 -08001079 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001080}
1081
Arend van Spriel20e5ca12011-10-18 14:03:09 +02001082/* copy a buffer into a pkt buffer chain */
Franky Line92eedf2011-11-22 17:21:50 -08001083static uint brcmf_sdbrcm_glom_from_buf(struct brcmf_sdio *bus, uint len)
Arend van Spriel20e5ca12011-10-18 14:03:09 +02001084{
1085 uint n, ret = 0;
1086 struct sk_buff *p;
1087 u8 *buf;
1088
Arend van Spriel20e5ca12011-10-18 14:03:09 +02001089 buf = bus->dataptr;
1090
1091 /* copy the data */
Arend van Sprielb83db862011-10-19 12:51:09 +02001092 skb_queue_walk(&bus->glom, p) {
Arend van Spriel20e5ca12011-10-18 14:03:09 +02001093 n = min_t(uint, p->len, len);
1094 memcpy(p->data, buf, n);
1095 buf += n;
1096 len -= n;
1097 ret += n;
Arend van Sprielb83db862011-10-19 12:51:09 +02001098 if (!len)
1099 break;
Arend van Spriel20e5ca12011-10-18 14:03:09 +02001100 }
1101
1102 return ret;
1103}
1104
Arend van Spriel9a95e602011-11-10 20:30:29 +01001105/* return total length of buffer chain */
Franky Line92eedf2011-11-22 17:21:50 -08001106static uint brcmf_sdbrcm_glom_len(struct brcmf_sdio *bus)
Arend van Spriel9a95e602011-11-10 20:30:29 +01001107{
1108 struct sk_buff *p;
1109 uint total;
1110
1111 total = 0;
1112 skb_queue_walk(&bus->glom, p)
1113 total += p->len;
1114 return total;
1115}
1116
Franky Line92eedf2011-11-22 17:21:50 -08001117static void brcmf_sdbrcm_free_glom(struct brcmf_sdio *bus)
Arend van Spriel046808d2011-11-10 20:30:31 +01001118{
1119 struct sk_buff *cur, *next;
1120
1121 skb_queue_walk_safe(&bus->glom, cur, next) {
1122 skb_unlink(cur, &bus->glom);
1123 brcmu_pkt_buf_free_skb(cur);
1124 }
1125}
1126
Franky Line92eedf2011-11-22 17:21:50 -08001127static u8 brcmf_sdbrcm_rxglom(struct brcmf_sdio *bus, u8 rxseq)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001128{
1129 u16 dlen, totlen;
1130 u8 *dptr, num = 0;
1131
1132 u16 sublen, check;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001133 struct sk_buff *pfirst, *pnext;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001134
1135 int errcode;
1136 u8 chan, seq, doff, sfdoff;
1137 u8 txmax;
1138
1139 int ifidx = 0;
1140 bool usechain = bus->use_rxchain;
1141
1142 /* If packets, issue read(s) and send up packet chain */
1143 /* Return sequence numbers consumed? */
1144
Arend van Sprielb83db862011-10-19 12:51:09 +02001145 brcmf_dbg(TRACE, "start: glomd %p glom %p\n",
1146 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001147
1148 /* If there's a descriptor, generate the packet chain */
1149 if (bus->glomd) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001150 pfirst = pnext = NULL;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001151 dlen = (u16) (bus->glomd->len);
1152 dptr = bus->glomd->data;
1153 if (!dlen || (dlen & 1)) {
1154 brcmf_dbg(ERROR, "bad glomd len(%d), ignore descriptor\n",
1155 dlen);
1156 dlen = 0;
1157 }
1158
1159 for (totlen = num = 0; dlen; num++) {
1160 /* Get (and move past) next length */
1161 sublen = get_unaligned_le16(dptr);
1162 dlen -= sizeof(u16);
1163 dptr += sizeof(u16);
1164 if ((sublen < SDPCM_HDRLEN) ||
1165 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
1166 brcmf_dbg(ERROR, "descriptor len %d bad: %d\n",
1167 num, sublen);
1168 pnext = NULL;
1169 break;
1170 }
1171 if (sublen % BRCMF_SDALIGN) {
1172 brcmf_dbg(ERROR, "sublen %d not multiple of %d\n",
1173 sublen, BRCMF_SDALIGN);
1174 usechain = false;
1175 }
1176 totlen += sublen;
1177
1178 /* For last frame, adjust read len so total
1179 is a block multiple */
1180 if (!dlen) {
1181 sublen +=
1182 (roundup(totlen, bus->blocksize) - totlen);
1183 totlen = roundup(totlen, bus->blocksize);
1184 }
1185
1186 /* Allocate/chain packet for next subframe */
1187 pnext = brcmu_pkt_buf_get_skb(sublen + BRCMF_SDALIGN);
1188 if (pnext == NULL) {
1189 brcmf_dbg(ERROR, "bcm_pkt_buf_get_skb failed, num %d len %d\n",
1190 num, sublen);
1191 break;
1192 }
Arend van Sprielb83db862011-10-19 12:51:09 +02001193 skb_queue_tail(&bus->glom, pnext);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001194
1195 /* Adhere to start alignment requirements */
1196 pkt_align(pnext, sublen, BRCMF_SDALIGN);
1197 }
1198
1199 /* If all allocations succeeded, save packet chain
1200 in bus structure */
1201 if (pnext) {
1202 brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1203 totlen, num);
1204 if (BRCMF_GLOM_ON() && bus->nextlen &&
1205 totlen != bus->nextlen) {
1206 brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
1207 bus->nextlen, totlen, rxseq);
1208 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02001209 pfirst = pnext = NULL;
1210 } else {
Arend van Spriel046808d2011-11-10 20:30:31 +01001211 brcmf_sdbrcm_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001212 num = 0;
1213 }
1214
1215 /* Done with descriptor packet */
1216 brcmu_pkt_buf_free_skb(bus->glomd);
1217 bus->glomd = NULL;
1218 bus->nextlen = 0;
1219 }
1220
1221 /* Ok -- either we just generated a packet chain,
1222 or had one from before */
Arend van Sprielb83db862011-10-19 12:51:09 +02001223 if (!skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001224 if (BRCMF_GLOM_ON()) {
1225 brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
Arend van Sprielb83db862011-10-19 12:51:09 +02001226 skb_queue_walk(&bus->glom, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001227 brcmf_dbg(GLOM, " %p: %p len 0x%04x (%d)\n",
1228 pnext, (u8 *) (pnext->data),
1229 pnext->len, pnext->len);
1230 }
1231 }
1232
Arend van Sprielb83db862011-10-19 12:51:09 +02001233 pfirst = skb_peek(&bus->glom);
Arend van Spriel9a95e602011-11-10 20:30:29 +01001234 dlen = (u16) brcmf_sdbrcm_glom_len(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001235
1236 /* Do an SDIO read for the superframe. Configurable iovar to
1237 * read directly into the chained packet, or allocate a large
1238 * packet and and copy into the chain.
1239 */
1240 if (usechain) {
Arend van Spriel5adfeb62011-11-22 17:21:38 -08001241 errcode = brcmf_sdcard_recv_chain(bus->sdiodev,
Arend van Spriel5b435de2011-10-05 13:19:03 +02001242 bus->sdiodev->sbwad,
Arend van Spriel5adfeb62011-11-22 17:21:38 -08001243 SDIO_FUNC_2, F2SYNC, &bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001244 } else if (bus->dataptr) {
1245 errcode = brcmf_sdcard_recv_buf(bus->sdiodev,
1246 bus->sdiodev->sbwad,
Arend van Spriel5adfeb62011-11-22 17:21:38 -08001247 SDIO_FUNC_2, F2SYNC,
1248 bus->dataptr, dlen);
Arend van Spriel20e5ca12011-10-18 14:03:09 +02001249 sublen = (u16) brcmf_sdbrcm_glom_from_buf(bus, dlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001250 if (sublen != dlen) {
1251 brcmf_dbg(ERROR, "FAILED TO COPY, dlen %d sublen %d\n",
1252 dlen, sublen);
1253 errcode = -1;
1254 }
1255 pnext = NULL;
1256 } else {
1257 brcmf_dbg(ERROR, "COULDN'T ALLOC %d-BYTE GLOM, FORCE FAILURE\n",
1258 dlen);
1259 errcode = -1;
1260 }
1261 bus->f2rxdata++;
1262
1263 /* On failure, kill the superframe, allow a couple retries */
1264 if (errcode < 0) {
1265 brcmf_dbg(ERROR, "glom read of %d bytes failed: %d\n",
1266 dlen, errcode);
Franky Lin719f2732011-12-16 18:37:06 -08001267 bus->sdiodev->bus_if->dstats.rx_errors++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001268
1269 if (bus->glomerr++ < 3) {
1270 brcmf_sdbrcm_rxfail(bus, true, true);
1271 } else {
1272 bus->glomerr = 0;
1273 brcmf_sdbrcm_rxfail(bus, true, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001274 bus->rxglomfail++;
Arend van Spriel046808d2011-11-10 20:30:31 +01001275 brcmf_sdbrcm_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001276 }
1277 return 0;
1278 }
Joe Perches1e023822012-01-15 00:38:40 -08001279
1280 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1281 pfirst->data, min_t(int, pfirst->len, 48),
1282 "SUPERFRAME:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001283
1284 /* Validate the superframe header */
1285 dptr = (u8 *) (pfirst->data);
1286 sublen = get_unaligned_le16(dptr);
1287 check = get_unaligned_le16(dptr + sizeof(u16));
1288
1289 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
1290 seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
1291 bus->nextlen = dptr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
1292 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
1293 brcmf_dbg(INFO, "nextlen too large (%d) seq %d\n",
1294 bus->nextlen, seq);
1295 bus->nextlen = 0;
1296 }
1297 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
1298 txmax = SDPCM_WINDOW_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
1299
1300 errcode = 0;
1301 if ((u16)~(sublen ^ check)) {
1302 brcmf_dbg(ERROR, "(superframe): HW hdr error: len/check 0x%04x/0x%04x\n",
1303 sublen, check);
1304 errcode = -1;
1305 } else if (roundup(sublen, bus->blocksize) != dlen) {
1306 brcmf_dbg(ERROR, "(superframe): len 0x%04x, rounded 0x%04x, expect 0x%04x\n",
1307 sublen, roundup(sublen, bus->blocksize),
1308 dlen);
1309 errcode = -1;
1310 } else if (SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]) !=
1311 SDPCM_GLOM_CHANNEL) {
1312 brcmf_dbg(ERROR, "(superframe): bad channel %d\n",
1313 SDPCM_PACKET_CHANNEL(
1314 &dptr[SDPCM_FRAMETAG_LEN]));
1315 errcode = -1;
1316 } else if (SDPCM_GLOMDESC(&dptr[SDPCM_FRAMETAG_LEN])) {
1317 brcmf_dbg(ERROR, "(superframe): got 2nd descriptor?\n");
1318 errcode = -1;
1319 } else if ((doff < SDPCM_HDRLEN) ||
1320 (doff > (pfirst->len - SDPCM_HDRLEN))) {
1321 brcmf_dbg(ERROR, "(superframe): Bad data offset %d: HW %d pkt %d min %d\n",
1322 doff, sublen, pfirst->len, SDPCM_HDRLEN);
1323 errcode = -1;
1324 }
1325
1326 /* Check sequence number of superframe SW header */
1327 if (rxseq != seq) {
1328 brcmf_dbg(INFO, "(superframe) rx_seq %d, expected %d\n",
1329 seq, rxseq);
1330 bus->rx_badseq++;
1331 rxseq = seq;
1332 }
1333
1334 /* Check window for sanity */
1335 if ((u8) (txmax - bus->tx_seq) > 0x40) {
1336 brcmf_dbg(ERROR, "unlikely tx max %d with tx_seq %d\n",
1337 txmax, bus->tx_seq);
1338 txmax = bus->tx_seq + 2;
1339 }
1340 bus->tx_max = txmax;
1341
1342 /* Remove superframe header, remember offset */
1343 skb_pull(pfirst, doff);
1344 sfdoff = doff;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001345 num = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001346
1347 /* Validate all the subframe headers */
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001348 skb_queue_walk(&bus->glom, pnext) {
1349 /* leave when invalid subframe is found */
1350 if (errcode)
1351 break;
1352
Arend van Spriel5b435de2011-10-05 13:19:03 +02001353 dptr = (u8 *) (pnext->data);
1354 dlen = (u16) (pnext->len);
1355 sublen = get_unaligned_le16(dptr);
1356 check = get_unaligned_le16(dptr + sizeof(u16));
1357 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
1358 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
Joe Perches1e023822012-01-15 00:38:40 -08001359 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1360 dptr, 32, "subframe:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001361
1362 if ((u16)~(sublen ^ check)) {
1363 brcmf_dbg(ERROR, "(subframe %d): HW hdr error: len/check 0x%04x/0x%04x\n",
1364 num, sublen, check);
1365 errcode = -1;
1366 } else if ((sublen > dlen) || (sublen < SDPCM_HDRLEN)) {
1367 brcmf_dbg(ERROR, "(subframe %d): length mismatch: len 0x%04x, expect 0x%04x\n",
1368 num, sublen, dlen);
1369 errcode = -1;
1370 } else if ((chan != SDPCM_DATA_CHANNEL) &&
1371 (chan != SDPCM_EVENT_CHANNEL)) {
1372 brcmf_dbg(ERROR, "(subframe %d): bad channel %d\n",
1373 num, chan);
1374 errcode = -1;
1375 } else if ((doff < SDPCM_HDRLEN) || (doff > sublen)) {
1376 brcmf_dbg(ERROR, "(subframe %d): Bad data offset %d: HW %d min %d\n",
1377 num, doff, sublen, SDPCM_HDRLEN);
1378 errcode = -1;
1379 }
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001380 /* increase the subframe count */
1381 num++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001382 }
1383
1384 if (errcode) {
1385 /* Terminate frame on error, request
1386 a couple retries */
1387 if (bus->glomerr++ < 3) {
1388 /* Restore superframe header space */
1389 skb_push(pfirst, sfdoff);
1390 brcmf_sdbrcm_rxfail(bus, true, true);
1391 } else {
1392 bus->glomerr = 0;
1393 brcmf_sdbrcm_rxfail(bus, true, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001394 bus->rxglomfail++;
Arend van Spriel046808d2011-11-10 20:30:31 +01001395 brcmf_sdbrcm_free_glom(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001396 }
1397 bus->nextlen = 0;
1398 return 0;
1399 }
1400
1401 /* Basic SD framing looks ok - process each packet (header) */
Arend van Spriel5b435de2011-10-05 13:19:03 +02001402
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001403 skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001404 dptr = (u8 *) (pfirst->data);
1405 sublen = get_unaligned_le16(dptr);
1406 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
1407 seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
1408 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
1409
1410 brcmf_dbg(GLOM, "Get subframe %d, %p(%p/%d), sublen %d chan %d seq %d\n",
1411 num, pfirst, pfirst->data,
1412 pfirst->len, sublen, chan, seq);
1413
1414 /* precondition: chan == SDPCM_DATA_CHANNEL ||
1415 chan == SDPCM_EVENT_CHANNEL */
1416
1417 if (rxseq != seq) {
1418 brcmf_dbg(GLOM, "rx_seq %d, expected %d\n",
1419 seq, rxseq);
1420 bus->rx_badseq++;
1421 rxseq = seq;
1422 }
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001423 rxseq++;
1424
Joe Perches1e023822012-01-15 00:38:40 -08001425 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
1426 dptr, dlen, "Rx Subframe Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001427
1428 __skb_trim(pfirst, sublen);
1429 skb_pull(pfirst, doff);
1430
1431 if (pfirst->len == 0) {
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001432 skb_unlink(pfirst, &bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001433 brcmu_pkt_buf_free_skb(pfirst);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001434 continue;
Franky Lind5625ee2011-12-16 18:37:01 -08001435 } else if (brcmf_proto_hdrpull(bus->sdiodev->dev,
1436 &ifidx, pfirst) != 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001437 brcmf_dbg(ERROR, "rx protocol error\n");
Franky Lin719f2732011-12-16 18:37:06 -08001438 bus->sdiodev->bus_if->dstats.rx_errors++;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001439 skb_unlink(pfirst, &bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001440 brcmu_pkt_buf_free_skb(pfirst);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001441 continue;
1442 }
1443
Joe Perches1e023822012-01-15 00:38:40 -08001444 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1445 pfirst->data,
1446 min_t(int, pfirst->len, 32),
1447 "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1448 bus->glom.qlen, pfirst, pfirst->data,
1449 pfirst->len, pfirst->next,
1450 pfirst->prev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001451 }
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001452 /* sent any remaining packets up */
1453 if (bus->glom.qlen) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001454 up(&bus->sdsem);
Franky Lin228bb432011-12-16 18:37:00 -08001455 brcmf_rx_frame(bus->sdiodev->dev, ifidx, &bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001456 down(&bus->sdsem);
1457 }
1458
1459 bus->rxglomframes++;
Arend van Spriel0b45bf72011-11-22 17:21:36 -08001460 bus->rxglompkts += bus->glom.qlen;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001461 }
1462 return num;
1463}
1464
Franky Line92eedf2011-11-22 17:21:50 -08001465static int brcmf_sdbrcm_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
Arend van Spriel5b435de2011-10-05 13:19:03 +02001466 bool *pending)
1467{
1468 DECLARE_WAITQUEUE(wait, current);
1469 int timeout = msecs_to_jiffies(DCMD_RESP_TIMEOUT);
1470
1471 /* Wait until control frame is available */
1472 add_wait_queue(&bus->dcmd_resp_wait, &wait);
1473 set_current_state(TASK_INTERRUPTIBLE);
1474
1475 while (!(*condition) && (!signal_pending(current) && timeout))
1476 timeout = schedule_timeout(timeout);
1477
1478 if (signal_pending(current))
1479 *pending = true;
1480
1481 set_current_state(TASK_RUNNING);
1482 remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1483
1484 return timeout;
1485}
1486
Franky Line92eedf2011-11-22 17:21:50 -08001487static int brcmf_sdbrcm_dcmd_resp_wake(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001488{
1489 if (waitqueue_active(&bus->dcmd_resp_wait))
1490 wake_up_interruptible(&bus->dcmd_resp_wait);
1491
1492 return 0;
1493}
1494static void
Franky Line92eedf2011-11-22 17:21:50 -08001495brcmf_sdbrcm_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001496{
1497 uint rdlen, pad;
1498
1499 int sdret;
1500
1501 brcmf_dbg(TRACE, "Enter\n");
1502
1503 /* Set rxctl for frame (w/optional alignment) */
1504 bus->rxctl = bus->rxbuf;
1505 bus->rxctl += BRCMF_FIRSTREAD;
1506 pad = ((unsigned long)bus->rxctl % BRCMF_SDALIGN);
1507 if (pad)
1508 bus->rxctl += (BRCMF_SDALIGN - pad);
1509 bus->rxctl -= BRCMF_FIRSTREAD;
1510
1511 /* Copy the already-read portion over */
1512 memcpy(bus->rxctl, hdr, BRCMF_FIRSTREAD);
1513 if (len <= BRCMF_FIRSTREAD)
1514 goto gotpkt;
1515
1516 /* Raise rdlen to next SDIO block to avoid tail command */
1517 rdlen = len - BRCMF_FIRSTREAD;
1518 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1519 pad = bus->blocksize - (rdlen % bus->blocksize);
1520 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
Franky Linb01a6b32011-12-16 18:37:03 -08001521 ((len + pad) < bus->sdiodev->bus_if->maxctl))
Arend van Spriel5b435de2011-10-05 13:19:03 +02001522 rdlen += pad;
1523 } else if (rdlen % BRCMF_SDALIGN) {
1524 rdlen += BRCMF_SDALIGN - (rdlen % BRCMF_SDALIGN);
1525 }
1526
1527 /* Satisfy length-alignment requirements */
1528 if (rdlen & (ALIGNMENT - 1))
1529 rdlen = roundup(rdlen, ALIGNMENT);
1530
1531 /* Drop if the read is too big or it exceeds our maximum */
Franky Linb01a6b32011-12-16 18:37:03 -08001532 if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001533 brcmf_dbg(ERROR, "%d-byte control read exceeds %d-byte buffer\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001534 rdlen, bus->sdiodev->bus_if->maxctl);
Franky Lin719f2732011-12-16 18:37:06 -08001535 bus->sdiodev->bus_if->dstats.rx_errors++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001536 brcmf_sdbrcm_rxfail(bus, false, false);
1537 goto done;
1538 }
1539
Franky Linb01a6b32011-12-16 18:37:03 -08001540 if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001541 brcmf_dbg(ERROR, "%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
Franky Linb01a6b32011-12-16 18:37:03 -08001542 len, len - doff, bus->sdiodev->bus_if->maxctl);
Franky Lin719f2732011-12-16 18:37:06 -08001543 bus->sdiodev->bus_if->dstats.rx_errors++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001544 bus->rx_toolong++;
1545 brcmf_sdbrcm_rxfail(bus, false, false);
1546 goto done;
1547 }
1548
1549 /* Read remainder of frame body into the rxctl buffer */
1550 sdret = brcmf_sdcard_recv_buf(bus->sdiodev,
1551 bus->sdiodev->sbwad,
1552 SDIO_FUNC_2,
Arend van Spriel5adfeb62011-11-22 17:21:38 -08001553 F2SYNC, (bus->rxctl + BRCMF_FIRSTREAD), rdlen);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001554 bus->f2rxdata++;
1555
1556 /* Control frame failures need retransmission */
1557 if (sdret < 0) {
1558 brcmf_dbg(ERROR, "read %d control bytes failed: %d\n",
1559 rdlen, sdret);
1560 bus->rxc_errors++;
1561 brcmf_sdbrcm_rxfail(bus, true, true);
1562 goto done;
1563 }
1564
1565gotpkt:
1566
Joe Perches1e023822012-01-15 00:38:40 -08001567 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
1568 bus->rxctl, len, "RxCtrl:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001569
1570 /* Point to valid data and indicate its length */
1571 bus->rxctl += doff;
1572 bus->rxlen = len - doff;
1573
1574done:
1575 /* Awake any waiters */
1576 brcmf_sdbrcm_dcmd_resp_wake(bus);
1577}
1578
1579/* Pad read to blocksize for efficiency */
Franky Line92eedf2011-11-22 17:21:50 -08001580static void brcmf_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001581{
1582 if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1583 *pad = bus->blocksize - (*rdlen % bus->blocksize);
1584 if (*pad <= bus->roundup && *pad < bus->blocksize &&
1585 *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1586 *rdlen += *pad;
1587 } else if (*rdlen % BRCMF_SDALIGN) {
1588 *rdlen += BRCMF_SDALIGN - (*rdlen % BRCMF_SDALIGN);
1589 }
1590}
1591
1592static void
Franky Line92eedf2011-11-22 17:21:50 -08001593brcmf_alloc_pkt_and_read(struct brcmf_sdio *bus, u16 rdlen,
Arend van Spriel5b435de2011-10-05 13:19:03 +02001594 struct sk_buff **pkt, u8 **rxbuf)
1595{
1596 int sdret; /* Return code from calls */
1597
1598 *pkt = brcmu_pkt_buf_get_skb(rdlen + BRCMF_SDALIGN);
1599 if (*pkt == NULL)
1600 return;
1601
1602 pkt_align(*pkt, rdlen, BRCMF_SDALIGN);
1603 *rxbuf = (u8 *) ((*pkt)->data);
1604 /* Read the entire frame */
Arend van Spriel5adfeb62011-11-22 17:21:38 -08001605 sdret = brcmf_sdcard_recv_pkt(bus->sdiodev, bus->sdiodev->sbwad,
1606 SDIO_FUNC_2, F2SYNC, *pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001607 bus->f2rxdata++;
1608
1609 if (sdret < 0) {
1610 brcmf_dbg(ERROR, "(nextlen): read %d bytes failed: %d\n",
1611 rdlen, sdret);
1612 brcmu_pkt_buf_free_skb(*pkt);
Franky Lin719f2732011-12-16 18:37:06 -08001613 bus->sdiodev->bus_if->dstats.rx_errors++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001614 /* Force retry w/normal header read.
1615 * Don't attempt NAK for
1616 * gSPI
1617 */
1618 brcmf_sdbrcm_rxfail(bus, true, true);
1619 *pkt = NULL;
1620 }
1621}
1622
1623/* Checks the header */
1624static int
Franky Line92eedf2011-11-22 17:21:50 -08001625brcmf_check_rxbuf(struct brcmf_sdio *bus, struct sk_buff *pkt, u8 *rxbuf,
Arend van Spriel5b435de2011-10-05 13:19:03 +02001626 u8 rxseq, u16 nextlen, u16 *len)
1627{
1628 u16 check;
1629 bool len_consistent; /* Result of comparing readahead len and
1630 len from hw-hdr */
1631
1632 memcpy(bus->rxhdr, rxbuf, SDPCM_HDRLEN);
1633
1634 /* Extract hardware header fields */
1635 *len = get_unaligned_le16(bus->rxhdr);
1636 check = get_unaligned_le16(bus->rxhdr + sizeof(u16));
1637
1638 /* All zeros means readahead info was bad */
1639 if (!(*len | check)) {
1640 brcmf_dbg(INFO, "(nextlen): read zeros in HW header???\n");
1641 goto fail;
1642 }
1643
1644 /* Validate check bytes */
1645 if ((u16)~(*len ^ check)) {
1646 brcmf_dbg(ERROR, "(nextlen): HW hdr error: nextlen/len/check 0x%04x/0x%04x/0x%04x\n",
1647 nextlen, *len, check);
1648 bus->rx_badhdr++;
1649 brcmf_sdbrcm_rxfail(bus, false, false);
1650 goto fail;
1651 }
1652
1653 /* Validate frame length */
1654 if (*len < SDPCM_HDRLEN) {
1655 brcmf_dbg(ERROR, "(nextlen): HW hdr length invalid: %d\n",
1656 *len);
1657 goto fail;
1658 }
1659
1660 /* Check for consistency with readahead info */
1661 len_consistent = (nextlen != (roundup(*len, 16) >> 4));
1662 if (len_consistent) {
1663 /* Mismatch, force retry w/normal
1664 header (may be >4K) */
1665 brcmf_dbg(ERROR, "(nextlen): mismatch, nextlen %d len %d rnd %d; expected rxseq %d\n",
1666 nextlen, *len, roundup(*len, 16),
1667 rxseq);
1668 brcmf_sdbrcm_rxfail(bus, true, true);
1669 goto fail;
1670 }
1671
1672 return 0;
1673
1674fail:
1675 brcmf_sdbrcm_pktfree2(bus, pkt);
1676 return -EINVAL;
1677}
1678
1679/* Return true if there may be more frames to read */
1680static uint
Franky Line92eedf2011-11-22 17:21:50 -08001681brcmf_sdbrcm_readframes(struct brcmf_sdio *bus, uint maxframes, bool *finished)
Arend van Spriel5b435de2011-10-05 13:19:03 +02001682{
1683 u16 len, check; /* Extracted hardware header fields */
1684 u8 chan, seq, doff; /* Extracted software header fields */
1685 u8 fcbits; /* Extracted fcbits from software header */
1686
1687 struct sk_buff *pkt; /* Packet for event or data frames */
1688 u16 pad; /* Number of pad bytes to read */
1689 u16 rdlen; /* Total number of bytes to read */
1690 u8 rxseq; /* Next sequence number to expect */
1691 uint rxleft = 0; /* Remaining number of frames allowed */
1692 int sdret; /* Return code from calls */
1693 u8 txmax; /* Maximum tx sequence offered */
1694 u8 *rxbuf;
1695 int ifidx = 0;
1696 uint rxcount = 0; /* Total frames read */
1697
1698 brcmf_dbg(TRACE, "Enter\n");
1699
1700 /* Not finished unless we encounter no more frames indication */
1701 *finished = false;
1702
1703 for (rxseq = bus->rx_seq, rxleft = maxframes;
Franky Lin8d169aa2011-11-22 17:21:52 -08001704 !bus->rxskip && rxleft &&
Franky Lin712ac5b2011-12-16 18:37:09 -08001705 bus->sdiodev->bus_if->state != BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001706 rxseq++, rxleft--) {
1707
1708 /* Handle glomming separately */
Arend van Sprielb83db862011-10-19 12:51:09 +02001709 if (bus->glomd || !skb_queue_empty(&bus->glom)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02001710 u8 cnt;
1711 brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
Arend van Sprielb83db862011-10-19 12:51:09 +02001712 bus->glomd, skb_peek(&bus->glom));
Arend van Spriel5b435de2011-10-05 13:19:03 +02001713 cnt = brcmf_sdbrcm_rxglom(bus, rxseq);
1714 brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
1715 rxseq += cnt - 1;
1716 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1717 continue;
1718 }
1719
1720 /* Try doing single read if we can */
1721 if (bus->nextlen) {
1722 u16 nextlen = bus->nextlen;
1723 bus->nextlen = 0;
1724
1725 rdlen = len = nextlen << 4;
1726 brcmf_pad(bus, &pad, &rdlen);
1727
1728 /*
1729 * After the frame is received we have to
1730 * distinguish whether it is data
1731 * or non-data frame.
1732 */
1733 brcmf_alloc_pkt_and_read(bus, rdlen, &pkt, &rxbuf);
1734 if (pkt == NULL) {
1735 /* Give up on data, request rtx of events */
1736 brcmf_dbg(ERROR, "(nextlen): brcmf_alloc_pkt_and_read failed: len %d rdlen %d expected rxseq %d\n",
1737 len, rdlen, rxseq);
1738 continue;
1739 }
1740
1741 if (brcmf_check_rxbuf(bus, pkt, rxbuf, rxseq, nextlen,
1742 &len) < 0)
1743 continue;
1744
1745 /* Extract software header fields */
1746 chan = SDPCM_PACKET_CHANNEL(
1747 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
1748 seq = SDPCM_PACKET_SEQUENCE(
1749 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
1750 doff = SDPCM_DOFFSET_VALUE(
1751 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
1752 txmax = SDPCM_WINDOW_VALUE(
1753 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
1754
1755 bus->nextlen =
1756 bus->rxhdr[SDPCM_FRAMETAG_LEN +
1757 SDPCM_NEXTLEN_OFFSET];
1758 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
1759 brcmf_dbg(INFO, "(nextlen): got frame w/nextlen too large (%d), seq %d\n",
1760 bus->nextlen, seq);
1761 bus->nextlen = 0;
1762 }
1763
Franky Lin28a1a3b2011-12-16 18:37:02 -08001764 bus->rx_readahead_cnt++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001765
1766 /* Handle Flow Control */
1767 fcbits = SDPCM_FCMASK_VALUE(
1768 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
1769
1770 if (bus->flowcontrol != fcbits) {
1771 if (~bus->flowcontrol & fcbits)
1772 bus->fc_xoff++;
1773
1774 if (bus->flowcontrol & ~fcbits)
1775 bus->fc_xon++;
1776
1777 bus->fc_rcvd++;
1778 bus->flowcontrol = fcbits;
1779 }
1780
1781 /* Check and update sequence number */
1782 if (rxseq != seq) {
1783 brcmf_dbg(INFO, "(nextlen): rx_seq %d, expected %d\n",
1784 seq, rxseq);
1785 bus->rx_badseq++;
1786 rxseq = seq;
1787 }
1788
1789 /* Check window for sanity */
1790 if ((u8) (txmax - bus->tx_seq) > 0x40) {
1791 brcmf_dbg(ERROR, "got unlikely tx max %d with tx_seq %d\n",
1792 txmax, bus->tx_seq);
1793 txmax = bus->tx_seq + 2;
1794 }
1795 bus->tx_max = txmax;
1796
Joe Perches1e023822012-01-15 00:38:40 -08001797 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
1798 rxbuf, len, "Rx Data:\n");
1799 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
1800 BRCMF_DATA_ON()) &&
1801 BRCMF_HDRS_ON(),
1802 bus->rxhdr, SDPCM_HDRLEN,
1803 "RxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001804
1805 if (chan == SDPCM_CONTROL_CHANNEL) {
1806 brcmf_dbg(ERROR, "(nextlen): readahead on control packet %d?\n",
1807 seq);
1808 /* Force retry w/normal header read */
1809 bus->nextlen = 0;
1810 brcmf_sdbrcm_rxfail(bus, false, true);
1811 brcmf_sdbrcm_pktfree2(bus, pkt);
1812 continue;
1813 }
1814
1815 /* Validate data offset */
1816 if ((doff < SDPCM_HDRLEN) || (doff > len)) {
1817 brcmf_dbg(ERROR, "(nextlen): bad data offset %d: HW len %d min %d\n",
1818 doff, len, SDPCM_HDRLEN);
1819 brcmf_sdbrcm_rxfail(bus, false, false);
1820 brcmf_sdbrcm_pktfree2(bus, pkt);
1821 continue;
1822 }
1823
1824 /* All done with this one -- now deliver the packet */
1825 goto deliver;
1826 }
1827
1828 /* Read frame header (hardware and software) */
1829 sdret = brcmf_sdcard_recv_buf(bus->sdiodev, bus->sdiodev->sbwad,
1830 SDIO_FUNC_2, F2SYNC, bus->rxhdr,
Arend van Spriel5adfeb62011-11-22 17:21:38 -08001831 BRCMF_FIRSTREAD);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001832 bus->f2rxhdrs++;
1833
1834 if (sdret < 0) {
1835 brcmf_dbg(ERROR, "RXHEADER FAILED: %d\n", sdret);
1836 bus->rx_hdrfail++;
1837 brcmf_sdbrcm_rxfail(bus, true, true);
1838 continue;
1839 }
Joe Perches1e023822012-01-15 00:38:40 -08001840 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1841 bus->rxhdr, SDPCM_HDRLEN, "RxHdr:\n");
1842
Arend van Spriel5b435de2011-10-05 13:19:03 +02001843
1844 /* Extract hardware header fields */
1845 len = get_unaligned_le16(bus->rxhdr);
1846 check = get_unaligned_le16(bus->rxhdr + sizeof(u16));
1847
1848 /* All zeros means no more frames */
1849 if (!(len | check)) {
1850 *finished = true;
1851 break;
1852 }
1853
1854 /* Validate check bytes */
1855 if ((u16) ~(len ^ check)) {
1856 brcmf_dbg(ERROR, "HW hdr err: len/check 0x%04x/0x%04x\n",
1857 len, check);
1858 bus->rx_badhdr++;
1859 brcmf_sdbrcm_rxfail(bus, false, false);
1860 continue;
1861 }
1862
1863 /* Validate frame length */
1864 if (len < SDPCM_HDRLEN) {
1865 brcmf_dbg(ERROR, "HW hdr length invalid: %d\n", len);
1866 continue;
1867 }
1868
1869 /* Extract software header fields */
1870 chan = SDPCM_PACKET_CHANNEL(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
1871 seq = SDPCM_PACKET_SEQUENCE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
1872 doff = SDPCM_DOFFSET_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
1873 txmax = SDPCM_WINDOW_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
1874
1875 /* Validate data offset */
1876 if ((doff < SDPCM_HDRLEN) || (doff > len)) {
1877 brcmf_dbg(ERROR, "Bad data offset %d: HW len %d, min %d seq %d\n",
1878 doff, len, SDPCM_HDRLEN, seq);
1879 bus->rx_badhdr++;
1880 brcmf_sdbrcm_rxfail(bus, false, false);
1881 continue;
1882 }
1883
1884 /* Save the readahead length if there is one */
1885 bus->nextlen =
1886 bus->rxhdr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
1887 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
1888 brcmf_dbg(INFO, "(nextlen): got frame w/nextlen too large (%d), seq %d\n",
1889 bus->nextlen, seq);
1890 bus->nextlen = 0;
1891 }
1892
1893 /* Handle Flow Control */
1894 fcbits = SDPCM_FCMASK_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
1895
1896 if (bus->flowcontrol != fcbits) {
1897 if (~bus->flowcontrol & fcbits)
1898 bus->fc_xoff++;
1899
1900 if (bus->flowcontrol & ~fcbits)
1901 bus->fc_xon++;
1902
1903 bus->fc_rcvd++;
1904 bus->flowcontrol = fcbits;
1905 }
1906
1907 /* Check and update sequence number */
1908 if (rxseq != seq) {
1909 brcmf_dbg(INFO, "rx_seq %d, expected %d\n", seq, rxseq);
1910 bus->rx_badseq++;
1911 rxseq = seq;
1912 }
1913
1914 /* Check window for sanity */
1915 if ((u8) (txmax - bus->tx_seq) > 0x40) {
1916 brcmf_dbg(ERROR, "unlikely tx max %d with tx_seq %d\n",
1917 txmax, bus->tx_seq);
1918 txmax = bus->tx_seq + 2;
1919 }
1920 bus->tx_max = txmax;
1921
1922 /* Call a separate function for control frames */
1923 if (chan == SDPCM_CONTROL_CHANNEL) {
1924 brcmf_sdbrcm_read_control(bus, bus->rxhdr, len, doff);
1925 continue;
1926 }
1927
1928 /* precondition: chan is either SDPCM_DATA_CHANNEL,
1929 SDPCM_EVENT_CHANNEL, SDPCM_TEST_CHANNEL or
1930 SDPCM_GLOM_CHANNEL */
1931
1932 /* Length to read */
1933 rdlen = (len > BRCMF_FIRSTREAD) ? (len - BRCMF_FIRSTREAD) : 0;
1934
1935 /* May pad read to blocksize for efficiency */
1936 if (bus->roundup && bus->blocksize &&
1937 (rdlen > bus->blocksize)) {
1938 pad = bus->blocksize - (rdlen % bus->blocksize);
1939 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
1940 ((rdlen + pad + BRCMF_FIRSTREAD) < MAX_RX_DATASZ))
1941 rdlen += pad;
1942 } else if (rdlen % BRCMF_SDALIGN) {
1943 rdlen += BRCMF_SDALIGN - (rdlen % BRCMF_SDALIGN);
1944 }
1945
1946 /* Satisfy length-alignment requirements */
1947 if (rdlen & (ALIGNMENT - 1))
1948 rdlen = roundup(rdlen, ALIGNMENT);
1949
1950 if ((rdlen + BRCMF_FIRSTREAD) > MAX_RX_DATASZ) {
1951 /* Too long -- skip this frame */
1952 brcmf_dbg(ERROR, "too long: len %d rdlen %d\n",
1953 len, rdlen);
Franky Lin719f2732011-12-16 18:37:06 -08001954 bus->sdiodev->bus_if->dstats.rx_errors++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001955 bus->rx_toolong++;
1956 brcmf_sdbrcm_rxfail(bus, false, false);
1957 continue;
1958 }
1959
1960 pkt = brcmu_pkt_buf_get_skb(rdlen +
1961 BRCMF_FIRSTREAD + BRCMF_SDALIGN);
1962 if (!pkt) {
1963 /* Give up on data, request rtx of events */
1964 brcmf_dbg(ERROR, "brcmu_pkt_buf_get_skb failed: rdlen %d chan %d\n",
1965 rdlen, chan);
Franky Lin719f2732011-12-16 18:37:06 -08001966 bus->sdiodev->bus_if->dstats.rx_dropped++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001967 brcmf_sdbrcm_rxfail(bus, false, RETRYCHAN(chan));
1968 continue;
1969 }
1970
1971 /* Leave room for what we already read, and align remainder */
1972 skb_pull(pkt, BRCMF_FIRSTREAD);
1973 pkt_align(pkt, rdlen, BRCMF_SDALIGN);
1974
1975 /* Read the remaining frame data */
Arend van Spriel5adfeb62011-11-22 17:21:38 -08001976 sdret = brcmf_sdcard_recv_pkt(bus->sdiodev, bus->sdiodev->sbwad,
1977 SDIO_FUNC_2, F2SYNC, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02001978 bus->f2rxdata++;
1979
1980 if (sdret < 0) {
1981 brcmf_dbg(ERROR, "read %d %s bytes failed: %d\n", rdlen,
1982 ((chan == SDPCM_EVENT_CHANNEL) ? "event"
1983 : ((chan == SDPCM_DATA_CHANNEL) ? "data"
1984 : "test")), sdret);
1985 brcmu_pkt_buf_free_skb(pkt);
Franky Lin719f2732011-12-16 18:37:06 -08001986 bus->sdiodev->bus_if->dstats.rx_errors++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02001987 brcmf_sdbrcm_rxfail(bus, true, RETRYCHAN(chan));
1988 continue;
1989 }
1990
1991 /* Copy the already-read portion */
1992 skb_push(pkt, BRCMF_FIRSTREAD);
1993 memcpy(pkt->data, bus->rxhdr, BRCMF_FIRSTREAD);
1994
Joe Perches1e023822012-01-15 00:38:40 -08001995 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
1996 pkt->data, len, "Rx Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02001997
1998deliver:
1999 /* Save superframe descriptor and allocate packet frame */
2000 if (chan == SDPCM_GLOM_CHANNEL) {
2001 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_FRAMETAG_LEN])) {
2002 brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
2003 len);
Joe Perches1e023822012-01-15 00:38:40 -08002004 brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
2005 pkt->data, len,
2006 "Glom Data:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002007 __skb_trim(pkt, len);
2008 skb_pull(pkt, SDPCM_HDRLEN);
2009 bus->glomd = pkt;
2010 } else {
2011 brcmf_dbg(ERROR, "%s: glom superframe w/o "
2012 "descriptor!\n", __func__);
2013 brcmf_sdbrcm_rxfail(bus, false, false);
2014 }
2015 continue;
2016 }
2017
2018 /* Fill in packet len and prio, deliver upward */
2019 __skb_trim(pkt, len);
2020 skb_pull(pkt, doff);
2021
2022 if (pkt->len == 0) {
2023 brcmu_pkt_buf_free_skb(pkt);
2024 continue;
Franky Lind5625ee2011-12-16 18:37:01 -08002025 } else if (brcmf_proto_hdrpull(bus->sdiodev->dev, &ifidx,
2026 pkt) != 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002027 brcmf_dbg(ERROR, "rx protocol error\n");
2028 brcmu_pkt_buf_free_skb(pkt);
Franky Lin719f2732011-12-16 18:37:06 -08002029 bus->sdiodev->bus_if->dstats.rx_errors++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002030 continue;
2031 }
2032
2033 /* Unlock during rx call */
2034 up(&bus->sdsem);
Franky Lin228bb432011-12-16 18:37:00 -08002035 brcmf_rx_packet(bus->sdiodev->dev, ifidx, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002036 down(&bus->sdsem);
2037 }
2038 rxcount = maxframes - rxleft;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002039 /* Message if we hit the limit */
2040 if (!rxleft)
2041 brcmf_dbg(DATA, "hit rx limit of %d frames\n",
2042 maxframes);
2043 else
Arend van Spriel5b435de2011-10-05 13:19:03 +02002044 brcmf_dbg(DATA, "processed %d frames\n", rxcount);
2045 /* Back off rxseq if awaiting rtx, update rx_seq */
2046 if (bus->rxskip)
2047 rxseq--;
2048 bus->rx_seq = rxseq;
2049
2050 return rxcount;
2051}
2052
Arend van Spriel5b435de2011-10-05 13:19:03 +02002053static void
Franky Line92eedf2011-11-22 17:21:50 -08002054brcmf_sdbrcm_wait_for_event(struct brcmf_sdio *bus, bool *lockvar)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002055{
2056 up(&bus->sdsem);
Joe Perches23677ce2012-02-09 11:17:23 +00002057 wait_event_interruptible_timeout(bus->ctrl_wait, !*lockvar, HZ * 2);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002058 down(&bus->sdsem);
2059 return;
2060}
2061
2062static void
Franky Line92eedf2011-11-22 17:21:50 -08002063brcmf_sdbrcm_wait_event_wakeup(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002064{
2065 if (waitqueue_active(&bus->ctrl_wait))
2066 wake_up_interruptible(&bus->ctrl_wait);
2067 return;
2068}
2069
2070/* Writes a HW/SW header into the packet and sends it. */
2071/* Assumes: (a) header space already there, (b) caller holds lock */
Franky Line92eedf2011-11-22 17:21:50 -08002072static int brcmf_sdbrcm_txpkt(struct brcmf_sdio *bus, struct sk_buff *pkt,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002073 uint chan, bool free_pkt)
2074{
2075 int ret;
2076 u8 *frame;
2077 u16 len, pad = 0;
2078 u32 swheader;
2079 struct sk_buff *new;
2080 int i;
2081
2082 brcmf_dbg(TRACE, "Enter\n");
2083
2084 frame = (u8 *) (pkt->data);
2085
2086 /* Add alignment padding, allocate new packet if needed */
2087 pad = ((unsigned long)frame % BRCMF_SDALIGN);
2088 if (pad) {
2089 if (skb_headroom(pkt) < pad) {
2090 brcmf_dbg(INFO, "insufficient headroom %d for %d pad\n",
2091 skb_headroom(pkt), pad);
Franky Lin9c1a0432011-12-16 18:37:07 -08002092 bus->sdiodev->bus_if->tx_realloc++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002093 new = brcmu_pkt_buf_get_skb(pkt->len + BRCMF_SDALIGN);
2094 if (!new) {
2095 brcmf_dbg(ERROR, "couldn't allocate new %d-byte packet\n",
2096 pkt->len + BRCMF_SDALIGN);
2097 ret = -ENOMEM;
2098 goto done;
2099 }
2100
2101 pkt_align(new, pkt->len, BRCMF_SDALIGN);
2102 memcpy(new->data, pkt->data, pkt->len);
2103 if (free_pkt)
2104 brcmu_pkt_buf_free_skb(pkt);
2105 /* free the pkt if canned one is not used */
2106 free_pkt = true;
2107 pkt = new;
2108 frame = (u8 *) (pkt->data);
2109 /* precondition: (frame % BRCMF_SDALIGN) == 0) */
2110 pad = 0;
2111 } else {
2112 skb_push(pkt, pad);
2113 frame = (u8 *) (pkt->data);
2114 /* precondition: pad + SDPCM_HDRLEN <= pkt->len */
2115 memset(frame, 0, pad + SDPCM_HDRLEN);
2116 }
2117 }
2118 /* precondition: pad < BRCMF_SDALIGN */
2119
2120 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
2121 len = (u16) (pkt->len);
2122 *(__le16 *) frame = cpu_to_le16(len);
2123 *(((__le16 *) frame) + 1) = cpu_to_le16(~len);
2124
2125 /* Software tag: channel, sequence number, data offset */
2126 swheader =
2127 ((chan << SDPCM_CHANNEL_SHIFT) & SDPCM_CHANNEL_MASK) | bus->tx_seq |
2128 (((pad +
2129 SDPCM_HDRLEN) << SDPCM_DOFFSET_SHIFT) & SDPCM_DOFFSET_MASK);
2130
2131 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
2132 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
2133
Joe Perches8ae74652012-01-15 00:38:38 -08002134#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002135 tx_packets[pkt->priority]++;
Joe Perches18aad4f2012-01-15 00:38:42 -08002136#endif
Joe Perches1e023822012-01-15 00:38:40 -08002137
2138 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() &&
2139 ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
2140 (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)),
2141 frame, len, "Tx Frame:\n");
2142 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
2143 ((BRCMF_CTL_ON() &&
2144 chan == SDPCM_CONTROL_CHANNEL) ||
2145 (BRCMF_DATA_ON() &&
2146 chan != SDPCM_CONTROL_CHANNEL))) &&
2147 BRCMF_HDRS_ON(),
2148 frame, min_t(u16, len, 16), "TxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002149
2150 /* Raise len to next SDIO block to eliminate tail command */
2151 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
2152 u16 pad = bus->blocksize - (len % bus->blocksize);
2153 if ((pad <= bus->roundup) && (pad < bus->blocksize))
2154 len += pad;
2155 } else if (len % BRCMF_SDALIGN) {
2156 len += BRCMF_SDALIGN - (len % BRCMF_SDALIGN);
2157 }
2158
2159 /* Some controllers have trouble with odd bytes -- round to even */
2160 if (len & (ALIGNMENT - 1))
2161 len = roundup(len, ALIGNMENT);
2162
Arend van Spriel5adfeb62011-11-22 17:21:38 -08002163 ret = brcmf_sdcard_send_pkt(bus->sdiodev, bus->sdiodev->sbwad,
2164 SDIO_FUNC_2, F2SYNC, pkt);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002165 bus->f2txdata++;
2166
2167 if (ret < 0) {
2168 /* On failure, abort the command and terminate the frame */
2169 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2170 ret);
2171 bus->tx_sderrs++;
2172
2173 brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
Franky Lin3bba8292012-05-04 18:27:33 -07002174 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2175 SFC_WF_TERM, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002176 bus->f1regdata++;
2177
2178 for (i = 0; i < 3; i++) {
2179 u8 hi, lo;
Franky Lin45db3392012-05-04 18:27:32 -07002180 hi = brcmf_sdio_regrb(bus->sdiodev,
2181 SBSDIO_FUNC1_WFRAMEBCHI, NULL);
2182 lo = brcmf_sdio_regrb(bus->sdiodev,
2183 SBSDIO_FUNC1_WFRAMEBCLO, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002184 bus->f1regdata += 2;
2185 if ((hi == 0) && (lo == 0))
2186 break;
2187 }
2188
2189 }
2190 if (ret == 0)
2191 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
2192
2193done:
2194 /* restore pkt buffer pointer before calling tx complete routine */
2195 skb_pull(pkt, SDPCM_HDRLEN + pad);
2196 up(&bus->sdsem);
Franky Linc9957882011-12-16 18:36:58 -08002197 brcmf_txcomplete(bus->sdiodev->dev, pkt, ret != 0);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002198 down(&bus->sdsem);
2199
2200 if (free_pkt)
2201 brcmu_pkt_buf_free_skb(pkt);
2202
2203 return ret;
2204}
2205
Franky Line92eedf2011-11-22 17:21:50 -08002206static uint brcmf_sdbrcm_sendfromq(struct brcmf_sdio *bus, uint maxframes)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002207{
2208 struct sk_buff *pkt;
2209 u32 intstatus = 0;
2210 uint retries = 0;
2211 int ret = 0, prec_out;
2212 uint cnt = 0;
2213 uint datalen;
2214 u8 tx_prec_map;
2215
Arend van Spriel5b435de2011-10-05 13:19:03 +02002216 brcmf_dbg(TRACE, "Enter\n");
2217
2218 tx_prec_map = ~bus->flowcontrol;
2219
2220 /* Send frames until the limit or some other event */
2221 for (cnt = 0; (cnt < maxframes) && data_ok(bus); cnt++) {
2222 spin_lock_bh(&bus->txqlock);
2223 pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map, &prec_out);
2224 if (pkt == NULL) {
2225 spin_unlock_bh(&bus->txqlock);
2226 break;
2227 }
2228 spin_unlock_bh(&bus->txqlock);
2229 datalen = pkt->len - SDPCM_HDRLEN;
2230
2231 ret = brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
2232 if (ret)
Franky Lin719f2732011-12-16 18:37:06 -08002233 bus->sdiodev->bus_if->dstats.tx_errors++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002234 else
Franky Lin719f2732011-12-16 18:37:06 -08002235 bus->sdiodev->bus_if->dstats.tx_bytes += datalen;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002236
2237 /* In poll mode, need to check for other events */
2238 if (!bus->intr && cnt) {
2239 /* Check device status, signal pending interrupt */
2240 r_sdreg32(bus, &intstatus,
2241 offsetof(struct sdpcmd_regs, intstatus),
2242 &retries);
2243 bus->f2txdata++;
2244 if (brcmf_sdcard_regfail(bus->sdiodev))
2245 break;
2246 if (intstatus & bus->hostintmask)
2247 bus->ipend = true;
2248 }
2249 }
2250
2251 /* Deflow-control stack if needed */
Franky Lin712ac5b2011-12-16 18:37:09 -08002252 if (bus->sdiodev->bus_if->drvr_up &&
2253 (bus->sdiodev->bus_if->state == BRCMF_BUS_DATA) &&
Franky Linc8bf3482011-12-16 18:37:08 -08002254 bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
2255 bus->txoff = OFF;
Franky Lin2b459052011-12-16 18:36:57 -08002256 brcmf_txflowcontrol(bus->sdiodev->dev, 0, OFF);
Franky Linc8bf3482011-12-16 18:37:08 -08002257 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002258
2259 return cnt;
2260}
2261
Franky Lina9ffda82011-12-16 18:37:12 -08002262static void brcmf_sdbrcm_bus_stop(struct device *dev)
2263{
2264 u32 local_hostintmask;
2265 u8 saveclk;
2266 uint retries;
2267 int err;
2268 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002269 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lina9ffda82011-12-16 18:37:12 -08002270 struct brcmf_sdio *bus = sdiodev->bus;
2271
2272 brcmf_dbg(TRACE, "Enter\n");
2273
2274 if (bus->watchdog_tsk) {
2275 send_sig(SIGTERM, bus->watchdog_tsk, 1);
2276 kthread_stop(bus->watchdog_tsk);
2277 bus->watchdog_tsk = NULL;
2278 }
2279
2280 if (bus->dpc_tsk && bus->dpc_tsk != current) {
2281 send_sig(SIGTERM, bus->dpc_tsk, 1);
2282 kthread_stop(bus->dpc_tsk);
2283 bus->dpc_tsk = NULL;
2284 }
2285
2286 down(&bus->sdsem);
2287
2288 bus_wake(bus);
2289
2290 /* Enable clock for device interrupts */
2291 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
2292
2293 /* Disable and clear interrupts at the chip level also */
2294 w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, hostintmask), &retries);
2295 local_hostintmask = bus->hostintmask;
2296 bus->hostintmask = 0;
2297
2298 /* Change our idea of bus state */
2299 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
2300
2301 /* Force clocks on backplane to be sure F2 interrupt propagates */
Franky Lin45db3392012-05-04 18:27:32 -07002302 saveclk = brcmf_sdio_regrb(bus->sdiodev,
2303 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Franky Lina9ffda82011-12-16 18:37:12 -08002304 if (!err) {
Franky Lin3bba8292012-05-04 18:27:33 -07002305 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2306 (saveclk | SBSDIO_FORCE_HT), &err);
Franky Lina9ffda82011-12-16 18:37:12 -08002307 }
2308 if (err)
2309 brcmf_dbg(ERROR, "Failed to force clock for F2: err %d\n", err);
2310
2311 /* Turn off the bus (F2), free any pending packets */
2312 brcmf_dbg(INTR, "disable SDIO interrupts\n");
Franky Lin3bba8292012-05-04 18:27:33 -07002313 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx, SDIO_FUNC_ENABLE_1,
2314 NULL);
Franky Lina9ffda82011-12-16 18:37:12 -08002315
2316 /* Clear any pending interrupts now that F2 is disabled */
2317 w_sdreg32(bus, local_hostintmask,
2318 offsetof(struct sdpcmd_regs, intstatus), &retries);
2319
2320 /* Turn off the backplane clock (only) */
2321 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
2322
2323 /* Clear the data packet queues */
2324 brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
2325
2326 /* Clear any held glomming stuff */
2327 if (bus->glomd)
2328 brcmu_pkt_buf_free_skb(bus->glomd);
2329 brcmf_sdbrcm_free_glom(bus);
2330
2331 /* Clear rx control and wake any waiters */
2332 bus->rxlen = 0;
2333 brcmf_sdbrcm_dcmd_resp_wake(bus);
2334
2335 /* Reset some F2 state stuff */
2336 bus->rxskip = false;
2337 bus->tx_seq = bus->rx_seq = 0;
2338
2339 up(&bus->sdsem);
2340}
2341
Franky Linba89bf12012-04-27 18:56:59 -07002342#ifdef CONFIG_BRCMFMAC_SDIO_OOB
2343static inline void brcmf_sdbrcm_clrintr(struct brcmf_sdio *bus)
2344{
2345 unsigned long flags;
2346
2347 spin_lock_irqsave(&bus->sdiodev->irq_en_lock, flags);
2348 if (!bus->sdiodev->irq_en && !bus->ipend) {
2349 enable_irq(bus->sdiodev->irq);
2350 bus->sdiodev->irq_en = true;
2351 }
2352 spin_unlock_irqrestore(&bus->sdiodev->irq_en_lock, flags);
2353}
2354#else
2355static inline void brcmf_sdbrcm_clrintr(struct brcmf_sdio *bus)
2356{
2357}
2358#endif /* CONFIG_BRCMFMAC_SDIO_OOB */
2359
Franky Line92eedf2011-11-22 17:21:50 -08002360static bool brcmf_sdbrcm_dpc(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002361{
2362 u32 intstatus, newstatus = 0;
2363 uint retries = 0;
2364 uint rxlimit = bus->rxbound; /* Rx frames to read before resched */
2365 uint txlimit = bus->txbound; /* Tx frames to send before resched */
2366 uint framecnt = 0; /* Temporary counter of tx/rx frames */
2367 bool rxdone = true; /* Flag for no more read data */
2368 bool resched = false; /* Flag indicating resched wanted */
2369
2370 brcmf_dbg(TRACE, "Enter\n");
2371
2372 /* Start with leftover status bits */
2373 intstatus = bus->intstatus;
2374
2375 down(&bus->sdsem);
2376
2377 /* If waiting for HTAVAIL, check status */
2378 if (bus->clkstate == CLK_PENDING) {
2379 int err;
2380 u8 clkctl, devctl = 0;
2381
Joe Perches8ae74652012-01-15 00:38:38 -08002382#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002383 /* Check for inconsistent device control */
Franky Lin45db3392012-05-04 18:27:32 -07002384 devctl = brcmf_sdio_regrb(bus->sdiodev,
2385 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002386 if (err) {
2387 brcmf_dbg(ERROR, "error reading DEVCTL: %d\n", err);
Franky Lin712ac5b2011-12-16 18:37:09 -08002388 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002389 }
Joe Perches8ae74652012-01-15 00:38:38 -08002390#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002391
2392 /* Read CSR, if clock on switch to AVAIL, else ignore */
Franky Lin45db3392012-05-04 18:27:32 -07002393 clkctl = brcmf_sdio_regrb(bus->sdiodev,
2394 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002395 if (err) {
2396 brcmf_dbg(ERROR, "error reading CSR: %d\n",
2397 err);
Franky Lin712ac5b2011-12-16 18:37:09 -08002398 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002399 }
2400
2401 brcmf_dbg(INFO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
2402 devctl, clkctl);
2403
2404 if (SBSDIO_HTAV(clkctl)) {
Franky Lin45db3392012-05-04 18:27:32 -07002405 devctl = brcmf_sdio_regrb(bus->sdiodev,
2406 SBSDIO_DEVICE_CTL, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002407 if (err) {
2408 brcmf_dbg(ERROR, "error reading DEVCTL: %d\n",
2409 err);
Franky Lin712ac5b2011-12-16 18:37:09 -08002410 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002411 }
2412 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
Franky Lin3bba8292012-05-04 18:27:33 -07002413 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2414 devctl, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002415 if (err) {
2416 brcmf_dbg(ERROR, "error writing DEVCTL: %d\n",
2417 err);
Franky Lin712ac5b2011-12-16 18:37:09 -08002418 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002419 }
2420 bus->clkstate = CLK_AVAIL;
2421 } else {
2422 goto clkwait;
2423 }
2424 }
2425
2426 bus_wake(bus);
2427
2428 /* Make sure backplane clock is on */
2429 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, true);
2430 if (bus->clkstate == CLK_PENDING)
2431 goto clkwait;
2432
2433 /* Pending interrupt indicates new device status */
2434 if (bus->ipend) {
2435 bus->ipend = false;
2436 r_sdreg32(bus, &newstatus,
2437 offsetof(struct sdpcmd_regs, intstatus), &retries);
2438 bus->f1regdata++;
2439 if (brcmf_sdcard_regfail(bus->sdiodev))
2440 newstatus = 0;
2441 newstatus &= bus->hostintmask;
2442 bus->fcstate = !!(newstatus & I_HMB_FC_STATE);
2443 if (newstatus) {
2444 w_sdreg32(bus, newstatus,
2445 offsetof(struct sdpcmd_regs, intstatus),
2446 &retries);
2447 bus->f1regdata++;
2448 }
2449 }
2450
2451 /* Merge new bits with previous */
2452 intstatus |= newstatus;
2453 bus->intstatus = 0;
2454
2455 /* Handle flow-control change: read new state in case our ack
2456 * crossed another change interrupt. If change still set, assume
2457 * FC ON for safety, let next loop through do the debounce.
2458 */
2459 if (intstatus & I_HMB_FC_CHANGE) {
2460 intstatus &= ~I_HMB_FC_CHANGE;
2461 w_sdreg32(bus, I_HMB_FC_CHANGE,
2462 offsetof(struct sdpcmd_regs, intstatus), &retries);
2463
2464 r_sdreg32(bus, &newstatus,
2465 offsetof(struct sdpcmd_regs, intstatus), &retries);
2466 bus->f1regdata += 2;
2467 bus->fcstate =
2468 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE));
2469 intstatus |= (newstatus & bus->hostintmask);
2470 }
2471
2472 /* Handle host mailbox indication */
2473 if (intstatus & I_HMB_HOST_INT) {
2474 intstatus &= ~I_HMB_HOST_INT;
2475 intstatus |= brcmf_sdbrcm_hostmail(bus);
2476 }
2477
2478 /* Generally don't ask for these, can get CRC errors... */
2479 if (intstatus & I_WR_OOSYNC) {
2480 brcmf_dbg(ERROR, "Dongle reports WR_OOSYNC\n");
2481 intstatus &= ~I_WR_OOSYNC;
2482 }
2483
2484 if (intstatus & I_RD_OOSYNC) {
2485 brcmf_dbg(ERROR, "Dongle reports RD_OOSYNC\n");
2486 intstatus &= ~I_RD_OOSYNC;
2487 }
2488
2489 if (intstatus & I_SBINT) {
2490 brcmf_dbg(ERROR, "Dongle reports SBINT\n");
2491 intstatus &= ~I_SBINT;
2492 }
2493
2494 /* Would be active due to wake-wlan in gSPI */
2495 if (intstatus & I_CHIPACTIVE) {
2496 brcmf_dbg(INFO, "Dongle reports CHIPACTIVE\n");
2497 intstatus &= ~I_CHIPACTIVE;
2498 }
2499
2500 /* Ignore frame indications if rxskip is set */
2501 if (bus->rxskip)
2502 intstatus &= ~I_HMB_FRAME_IND;
2503
2504 /* On frame indication, read available frames */
2505 if (PKT_AVAILABLE()) {
2506 framecnt = brcmf_sdbrcm_readframes(bus, rxlimit, &rxdone);
2507 if (rxdone || bus->rxskip)
2508 intstatus &= ~I_HMB_FRAME_IND;
2509 rxlimit -= min(framecnt, rxlimit);
2510 }
2511
2512 /* Keep still-pending events for next scheduling */
2513 bus->intstatus = intstatus;
2514
2515clkwait:
Franky Linba89bf12012-04-27 18:56:59 -07002516 brcmf_sdbrcm_clrintr(bus);
2517
Arend van Spriel5b435de2011-10-05 13:19:03 +02002518 if (data_ok(bus) && bus->ctrl_frame_stat &&
2519 (bus->clkstate == CLK_AVAIL)) {
2520 int ret, i;
2521
Arend van Spriel5adfeb62011-11-22 17:21:38 -08002522 ret = brcmf_sdcard_send_buf(bus->sdiodev, bus->sdiodev->sbwad,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002523 SDIO_FUNC_2, F2SYNC, (u8 *) bus->ctrl_frame_buf,
Arend van Spriel5adfeb62011-11-22 17:21:38 -08002524 (u32) bus->ctrl_frame_len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002525
2526 if (ret < 0) {
2527 /* On failure, abort the command and
2528 terminate the frame */
2529 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2530 ret);
2531 bus->tx_sderrs++;
2532
2533 brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
2534
Franky Lin3bba8292012-05-04 18:27:33 -07002535 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2536 SFC_WF_TERM, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002537 bus->f1regdata++;
2538
2539 for (i = 0; i < 3; i++) {
2540 u8 hi, lo;
Franky Lin45db3392012-05-04 18:27:32 -07002541 hi = brcmf_sdio_regrb(bus->sdiodev,
2542 SBSDIO_FUNC1_WFRAMEBCHI,
2543 NULL);
2544 lo = brcmf_sdio_regrb(bus->sdiodev,
2545 SBSDIO_FUNC1_WFRAMEBCLO,
2546 NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002547 bus->f1regdata += 2;
2548 if ((hi == 0) && (lo == 0))
2549 break;
2550 }
2551
2552 }
2553 if (ret == 0)
2554 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
2555
2556 brcmf_dbg(INFO, "Return_dpc value is : %d\n", ret);
2557 bus->ctrl_frame_stat = false;
2558 brcmf_sdbrcm_wait_event_wakeup(bus);
2559 }
2560 /* Send queued frames (limit 1 if rx may still be pending) */
2561 else if ((bus->clkstate == CLK_AVAIL) && !bus->fcstate &&
2562 brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit
2563 && data_ok(bus)) {
2564 framecnt = rxdone ? txlimit : min(txlimit, bus->txminmax);
2565 framecnt = brcmf_sdbrcm_sendfromq(bus, framecnt);
2566 txlimit -= framecnt;
2567 }
2568
2569 /* Resched if events or tx frames are pending,
2570 else await next interrupt */
2571 /* On failed register access, all bets are off:
2572 no resched or interrupts */
Franky Lin712ac5b2011-12-16 18:37:09 -08002573 if ((bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) ||
Arend van Spriel5b435de2011-10-05 13:19:03 +02002574 brcmf_sdcard_regfail(bus->sdiodev)) {
2575 brcmf_dbg(ERROR, "failed backplane access over SDIO, halting operation %d\n",
2576 brcmf_sdcard_regfail(bus->sdiodev));
Franky Lin712ac5b2011-12-16 18:37:09 -08002577 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002578 bus->intstatus = 0;
2579 } else if (bus->clkstate == CLK_PENDING) {
2580 brcmf_dbg(INFO, "rescheduled due to CLK_PENDING awaiting I_CHIPACTIVE interrupt\n");
2581 resched = true;
2582 } else if (bus->intstatus || bus->ipend ||
2583 (!bus->fcstate && brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol)
2584 && data_ok(bus)) || PKT_AVAILABLE()) {
2585 resched = true;
2586 }
2587
2588 bus->dpc_sched = resched;
2589
2590 /* If we're done for now, turn off clock request. */
2591 if ((bus->clkstate != CLK_PENDING)
2592 && bus->idletime == BRCMF_IDLE_IMMEDIATE) {
2593 bus->activity = false;
2594 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
2595 }
2596
2597 up(&bus->sdsem);
2598
2599 return resched;
2600}
2601
Franky Linb948a852012-04-23 14:24:53 -07002602static inline void brcmf_sdbrcm_adddpctsk(struct brcmf_sdio *bus)
2603{
2604 struct list_head *new_hd;
2605 unsigned long flags;
2606
2607 if (in_interrupt())
2608 new_hd = kzalloc(sizeof(struct list_head), GFP_ATOMIC);
2609 else
2610 new_hd = kzalloc(sizeof(struct list_head), GFP_KERNEL);
2611 if (new_hd == NULL)
2612 return;
2613
2614 spin_lock_irqsave(&bus->dpc_tl_lock, flags);
2615 list_add_tail(new_hd, &bus->dpc_tsklst);
2616 spin_unlock_irqrestore(&bus->dpc_tl_lock, flags);
2617}
2618
Arend van Spriel5b435de2011-10-05 13:19:03 +02002619static int brcmf_sdbrcm_dpc_thread(void *data)
2620{
Franky Line92eedf2011-11-22 17:21:50 -08002621 struct brcmf_sdio *bus = (struct brcmf_sdio *) data;
Franky Linb948a852012-04-23 14:24:53 -07002622 struct list_head *cur_hd, *tmp_hd;
2623 unsigned long flags;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002624
2625 allow_signal(SIGTERM);
2626 /* Run until signal received */
2627 while (1) {
2628 if (kthread_should_stop())
2629 break;
Franky Linb948a852012-04-23 14:24:53 -07002630
2631 if (list_empty(&bus->dpc_tsklst))
2632 if (wait_for_completion_interruptible(&bus->dpc_wait))
2633 break;
2634
2635 spin_lock_irqsave(&bus->dpc_tl_lock, flags);
2636 list_for_each_safe(cur_hd, tmp_hd, &bus->dpc_tsklst) {
2637 spin_unlock_irqrestore(&bus->dpc_tl_lock, flags);
2638
2639 if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002640 /* after stopping the bus, exit thread */
Franky Lin94c2fb82011-11-22 17:21:59 -08002641 brcmf_sdbrcm_bus_stop(bus->sdiodev->dev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002642 bus->dpc_tsk = NULL;
Franky Lincf043172012-04-30 11:43:07 -07002643 spin_lock_irqsave(&bus->dpc_tl_lock, flags);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002644 break;
2645 }
Franky Linb948a852012-04-23 14:24:53 -07002646
2647 if (brcmf_sdbrcm_dpc(bus))
2648 brcmf_sdbrcm_adddpctsk(bus);
2649
2650 spin_lock_irqsave(&bus->dpc_tl_lock, flags);
2651 list_del(cur_hd);
2652 kfree(cur_hd);
2653 }
2654 spin_unlock_irqrestore(&bus->dpc_tl_lock, flags);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002655 }
2656 return 0;
2657}
2658
Franky Linb9692d12011-12-16 18:37:15 -08002659static int brcmf_sdbrcm_bus_txdata(struct device *dev, struct sk_buff *pkt)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002660{
2661 int ret = -EBADE;
2662 uint datalen, prec;
Franky Linbf347bb2011-11-22 17:21:56 -08002663 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002664 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linbf347bb2011-11-22 17:21:56 -08002665 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002666
2667 brcmf_dbg(TRACE, "Enter\n");
2668
2669 datalen = pkt->len;
2670
2671 /* Add space for the header */
2672 skb_push(pkt, SDPCM_HDRLEN);
2673 /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2674
2675 prec = prio2prec((pkt->priority & PRIOMASK));
2676
2677 /* Check for existing queue, current flow-control,
2678 pending event, or pending clock */
2679 brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
2680 bus->fcqueued++;
2681
2682 /* Priority based enq */
2683 spin_lock_bh(&bus->txqlock);
Joe Perches23677ce2012-02-09 11:17:23 +00002684 if (!brcmf_c_prec_enq(bus->sdiodev->dev, &bus->txq, pkt, prec)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002685 skb_pull(pkt, SDPCM_HDRLEN);
Franky Linc9957882011-12-16 18:36:58 -08002686 brcmf_txcomplete(bus->sdiodev->dev, pkt, false);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002687 brcmu_pkt_buf_free_skb(pkt);
2688 brcmf_dbg(ERROR, "out of bus->txq !!!\n");
2689 ret = -ENOSR;
2690 } else {
2691 ret = 0;
2692 }
2693 spin_unlock_bh(&bus->txqlock);
2694
Franky Linc8bf3482011-12-16 18:37:08 -08002695 if (pktq_len(&bus->txq) >= TXHI) {
2696 bus->txoff = ON;
Franky Lin2b459052011-12-16 18:36:57 -08002697 brcmf_txflowcontrol(bus->sdiodev->dev, 0, ON);
Franky Linc8bf3482011-12-16 18:37:08 -08002698 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002699
Joe Perches8ae74652012-01-15 00:38:38 -08002700#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002701 if (pktq_plen(&bus->txq, prec) > qcount[prec])
2702 qcount[prec] = pktq_plen(&bus->txq, prec);
2703#endif
2704 /* Schedule DPC if needed to send queued packet(s) */
2705 if (!bus->dpc_sched) {
2706 bus->dpc_sched = true;
Franky Linb948a852012-04-23 14:24:53 -07002707 if (bus->dpc_tsk) {
2708 brcmf_sdbrcm_adddpctsk(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002709 complete(&bus->dpc_wait);
Franky Linb948a852012-04-23 14:24:53 -07002710 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02002711 }
2712
2713 return ret;
2714}
2715
2716static int
Franky Line92eedf2011-11-22 17:21:50 -08002717brcmf_sdbrcm_membytes(struct brcmf_sdio *bus, bool write, u32 address, u8 *data,
Arend van Spriel5b435de2011-10-05 13:19:03 +02002718 uint size)
2719{
2720 int bcmerror = 0;
2721 u32 sdaddr;
2722 uint dsize;
2723
2724 /* Determine initial transfer parameters */
2725 sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK;
2726 if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK)
2727 dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr);
2728 else
2729 dsize = size;
2730
2731 /* Set the backplane window to include the start address */
2732 bcmerror = brcmf_sdcard_set_sbaddr_window(bus->sdiodev, address);
2733 if (bcmerror) {
2734 brcmf_dbg(ERROR, "window change failed\n");
2735 goto xfer_done;
2736 }
2737
2738 /* Do the transfer(s) */
2739 while (size) {
2740 brcmf_dbg(INFO, "%s %d bytes at offset 0x%08x in window 0x%08x\n",
2741 write ? "write" : "read", dsize,
2742 sdaddr, address & SBSDIO_SBWINDOW_MASK);
2743 bcmerror = brcmf_sdcard_rwdata(bus->sdiodev, write,
2744 sdaddr, data, dsize);
2745 if (bcmerror) {
2746 brcmf_dbg(ERROR, "membytes transfer failed\n");
2747 break;
2748 }
2749
2750 /* Adjust for next transfer (if any) */
2751 size -= dsize;
2752 if (size) {
2753 data += dsize;
2754 address += dsize;
2755 bcmerror = brcmf_sdcard_set_sbaddr_window(bus->sdiodev,
2756 address);
2757 if (bcmerror) {
2758 brcmf_dbg(ERROR, "window change failed\n");
2759 break;
2760 }
2761 sdaddr = 0;
2762 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
2763 }
2764 }
2765
2766xfer_done:
2767 /* Return the window to backplane enumeration space for core access */
2768 if (brcmf_sdcard_set_sbaddr_window(bus->sdiodev, bus->sdiodev->sbwad))
2769 brcmf_dbg(ERROR, "FAILED to set window back to 0x%x\n",
2770 bus->sdiodev->sbwad);
2771
2772 return bcmerror;
2773}
2774
Joe Perches8ae74652012-01-15 00:38:38 -08002775#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02002776#define CONSOLE_LINE_MAX 192
2777
Franky Line92eedf2011-11-22 17:21:50 -08002778static int brcmf_sdbrcm_readconsole(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002779{
2780 struct brcmf_console *c = &bus->console;
2781 u8 line[CONSOLE_LINE_MAX], ch;
2782 u32 n, idx, addr;
2783 int rv;
2784
2785 /* Don't do anything until FWREADY updates console address */
2786 if (bus->console_addr == 0)
2787 return 0;
2788
2789 /* Read console log struct */
2790 addr = bus->console_addr + offsetof(struct rte_console, log_le);
2791 rv = brcmf_sdbrcm_membytes(bus, false, addr, (u8 *)&c->log_le,
2792 sizeof(c->log_le));
2793 if (rv < 0)
2794 return rv;
2795
2796 /* Allocate console buffer (one time only) */
2797 if (c->buf == NULL) {
2798 c->bufsize = le32_to_cpu(c->log_le.buf_size);
2799 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2800 if (c->buf == NULL)
2801 return -ENOMEM;
2802 }
2803
2804 idx = le32_to_cpu(c->log_le.idx);
2805
2806 /* Protect against corrupt value */
2807 if (idx > c->bufsize)
2808 return -EBADE;
2809
2810 /* Skip reading the console buffer if the index pointer
2811 has not moved */
2812 if (idx == c->last)
2813 return 0;
2814
2815 /* Read the console buffer */
2816 addr = le32_to_cpu(c->log_le.buf);
2817 rv = brcmf_sdbrcm_membytes(bus, false, addr, c->buf, c->bufsize);
2818 if (rv < 0)
2819 return rv;
2820
2821 while (c->last != idx) {
2822 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2823 if (c->last == idx) {
2824 /* This would output a partial line.
2825 * Instead, back up
2826 * the buffer pointer and output this
2827 * line next time around.
2828 */
2829 if (c->last >= n)
2830 c->last -= n;
2831 else
2832 c->last = c->bufsize - n;
2833 goto break2;
2834 }
2835 ch = c->buf[c->last];
2836 c->last = (c->last + 1) % c->bufsize;
2837 if (ch == '\n')
2838 break;
2839 line[n] = ch;
2840 }
2841
2842 if (n > 0) {
2843 if (line[n - 1] == '\r')
2844 n--;
2845 line[n] = 0;
Joe Perches18aad4f2012-01-15 00:38:42 -08002846 pr_debug("CONSOLE: %s\n", line);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002847 }
2848 }
2849break2:
2850
2851 return 0;
2852}
Joe Perches8ae74652012-01-15 00:38:38 -08002853#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02002854
Franky Line92eedf2011-11-22 17:21:50 -08002855static int brcmf_tx_frame(struct brcmf_sdio *bus, u8 *frame, u16 len)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002856{
2857 int i;
2858 int ret;
2859
2860 bus->ctrl_frame_stat = false;
Arend van Spriel5adfeb62011-11-22 17:21:38 -08002861 ret = brcmf_sdcard_send_buf(bus->sdiodev, bus->sdiodev->sbwad,
2862 SDIO_FUNC_2, F2SYNC, frame, len);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002863
2864 if (ret < 0) {
2865 /* On failure, abort the command and terminate the frame */
2866 brcmf_dbg(INFO, "sdio error %d, abort command and terminate frame\n",
2867 ret);
2868 bus->tx_sderrs++;
2869
2870 brcmf_sdcard_abort(bus->sdiodev, SDIO_FUNC_2);
2871
Franky Lin3bba8292012-05-04 18:27:33 -07002872 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL,
2873 SFC_WF_TERM, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002874 bus->f1regdata++;
2875
2876 for (i = 0; i < 3; i++) {
2877 u8 hi, lo;
Franky Lin45db3392012-05-04 18:27:32 -07002878 hi = brcmf_sdio_regrb(bus->sdiodev,
2879 SBSDIO_FUNC1_WFRAMEBCHI, NULL);
2880 lo = brcmf_sdio_regrb(bus->sdiodev,
2881 SBSDIO_FUNC1_WFRAMEBCLO, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02002882 bus->f1regdata += 2;
2883 if (hi == 0 && lo == 0)
2884 break;
2885 }
2886 return ret;
2887 }
2888
2889 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
2890
2891 return ret;
2892}
2893
Franky Linfcf094f2011-12-16 18:37:16 -08002894static int
Franky Lin47a1ce72011-11-22 17:21:55 -08002895brcmf_sdbrcm_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02002896{
2897 u8 *frame;
2898 u16 len;
2899 u32 swheader;
2900 uint retries = 0;
2901 u8 doff = 0;
2902 int ret = -1;
Franky Lin47a1ce72011-11-22 17:21:55 -08002903 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01002904 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin47a1ce72011-11-22 17:21:55 -08002905 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02002906
2907 brcmf_dbg(TRACE, "Enter\n");
2908
2909 /* Back the pointer to make a room for bus header */
2910 frame = msg - SDPCM_HDRLEN;
2911 len = (msglen += SDPCM_HDRLEN);
2912
2913 /* Add alignment padding (optional for ctl frames) */
2914 doff = ((unsigned long)frame % BRCMF_SDALIGN);
2915 if (doff) {
2916 frame -= doff;
2917 len += doff;
2918 msglen += doff;
2919 memset(frame, 0, doff + SDPCM_HDRLEN);
2920 }
2921 /* precondition: doff < BRCMF_SDALIGN */
2922 doff += SDPCM_HDRLEN;
2923
2924 /* Round send length to next SDIO block */
2925 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
2926 u16 pad = bus->blocksize - (len % bus->blocksize);
2927 if ((pad <= bus->roundup) && (pad < bus->blocksize))
2928 len += pad;
2929 } else if (len % BRCMF_SDALIGN) {
2930 len += BRCMF_SDALIGN - (len % BRCMF_SDALIGN);
2931 }
2932
2933 /* Satisfy length-alignment requirements */
2934 if (len & (ALIGNMENT - 1))
2935 len = roundup(len, ALIGNMENT);
2936
2937 /* precondition: IS_ALIGNED((unsigned long)frame, 2) */
2938
2939 /* Need to lock here to protect txseq and SDIO tx calls */
2940 down(&bus->sdsem);
2941
2942 bus_wake(bus);
2943
2944 /* Make sure backplane clock is on */
2945 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
2946
2947 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
2948 *(__le16 *) frame = cpu_to_le16((u16) msglen);
2949 *(((__le16 *) frame) + 1) = cpu_to_le16(~msglen);
2950
2951 /* Software tag: channel, sequence number, data offset */
2952 swheader =
2953 ((SDPCM_CONTROL_CHANNEL << SDPCM_CHANNEL_SHIFT) &
2954 SDPCM_CHANNEL_MASK)
2955 | bus->tx_seq | ((doff << SDPCM_DOFFSET_SHIFT) &
2956 SDPCM_DOFFSET_MASK);
2957 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
2958 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
2959
2960 if (!data_ok(bus)) {
2961 brcmf_dbg(INFO, "No bus credit bus->tx_max %d, bus->tx_seq %d\n",
2962 bus->tx_max, bus->tx_seq);
2963 bus->ctrl_frame_stat = true;
2964 /* Send from dpc */
2965 bus->ctrl_frame_buf = frame;
2966 bus->ctrl_frame_len = len;
2967
2968 brcmf_sdbrcm_wait_for_event(bus, &bus->ctrl_frame_stat);
2969
Joe Perches23677ce2012-02-09 11:17:23 +00002970 if (!bus->ctrl_frame_stat) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02002971 brcmf_dbg(INFO, "ctrl_frame_stat == false\n");
2972 ret = 0;
2973 } else {
2974 brcmf_dbg(INFO, "ctrl_frame_stat == true\n");
2975 ret = -1;
2976 }
2977 }
2978
2979 if (ret == -1) {
Joe Perches1e023822012-01-15 00:38:40 -08002980 brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2981 frame, len, "Tx Frame:\n");
2982 brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2983 BRCMF_HDRS_ON(),
2984 frame, min_t(u16, len, 16), "TxHdr:\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02002985
2986 do {
2987 ret = brcmf_tx_frame(bus, frame, len);
2988 } while (ret < 0 && retries++ < TXRETRIES);
2989 }
2990
2991 if ((bus->idletime == BRCMF_IDLE_IMMEDIATE) && !bus->dpc_sched) {
2992 bus->activity = false;
2993 brcmf_sdbrcm_clkctl(bus, CLK_NONE, true);
2994 }
2995
2996 up(&bus->sdsem);
2997
2998 if (ret)
Franky Lin28a1a3b2011-12-16 18:37:02 -08002999 bus->tx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003000 else
Franky Lin28a1a3b2011-12-16 18:37:02 -08003001 bus->tx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003002
3003 return ret ? -EIO : 0;
3004}
3005
Franky Linfcf094f2011-12-16 18:37:16 -08003006static int
Franky Lin532cdd32011-11-22 17:21:54 -08003007brcmf_sdbrcm_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003008{
3009 int timeleft;
3010 uint rxlen = 0;
3011 bool pending;
Franky Lin532cdd32011-11-22 17:21:54 -08003012 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01003013 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Lin532cdd32011-11-22 17:21:54 -08003014 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003015
3016 brcmf_dbg(TRACE, "Enter\n");
3017
3018 /* Wait until control frame is available */
3019 timeleft = brcmf_sdbrcm_dcmd_resp_wait(bus, &bus->rxlen, &pending);
3020
3021 down(&bus->sdsem);
3022 rxlen = bus->rxlen;
3023 memcpy(msg, bus->rxctl, min(msglen, rxlen));
3024 bus->rxlen = 0;
3025 up(&bus->sdsem);
3026
3027 if (rxlen) {
3028 brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3029 rxlen, msglen);
3030 } else if (timeleft == 0) {
3031 brcmf_dbg(ERROR, "resumed on timeout\n");
Joe Perches23677ce2012-02-09 11:17:23 +00003032 } else if (pending) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003033 brcmf_dbg(CTL, "cancelled\n");
3034 return -ERESTARTSYS;
3035 } else {
3036 brcmf_dbg(CTL, "resumed for unknown reason?\n");
3037 }
3038
3039 if (rxlen)
Franky Lin28a1a3b2011-12-16 18:37:02 -08003040 bus->rx_ctlpkts++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003041 else
Franky Lin28a1a3b2011-12-16 18:37:02 -08003042 bus->rx_ctlerrs++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003043
3044 return rxlen ? (int)rxlen : -ETIMEDOUT;
3045}
3046
Franky Line92eedf2011-11-22 17:21:50 -08003047static int brcmf_sdbrcm_downloadvars(struct brcmf_sdio *bus, void *arg, int len)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003048{
3049 int bcmerror = 0;
3050
3051 brcmf_dbg(TRACE, "Enter\n");
3052
3053 /* Basic sanity checks */
Franky Lin3fb1d8d2011-12-16 18:37:04 -08003054 if (bus->sdiodev->bus_if->drvr_up) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003055 bcmerror = -EISCONN;
3056 goto err;
3057 }
3058 if (!len) {
3059 bcmerror = -EOVERFLOW;
3060 goto err;
3061 }
3062
3063 /* Free the old ones and replace with passed variables */
3064 kfree(bus->vars);
3065
3066 bus->vars = kmalloc(len, GFP_ATOMIC);
3067 bus->varsz = bus->vars ? len : 0;
3068 if (bus->vars == NULL) {
3069 bcmerror = -ENOMEM;
3070 goto err;
3071 }
3072
3073 /* Copy the passed variables, which should include the
3074 terminating double-null */
3075 memcpy(bus->vars, arg, bus->varsz);
3076err:
3077 return bcmerror;
3078}
3079
Franky Line92eedf2011-11-22 17:21:50 -08003080static int brcmf_sdbrcm_write_vars(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003081{
3082 int bcmerror = 0;
3083 u32 varsize;
3084 u32 varaddr;
3085 u8 *vbuffer;
3086 u32 varsizew;
3087 __le32 varsizew_le;
Joe Perches8ae74652012-01-15 00:38:38 -08003088#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02003089 char *nvram_ularray;
Joe Perches8ae74652012-01-15 00:38:38 -08003090#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003091
3092 /* Even if there are no vars are to be written, we still
3093 need to set the ramsize. */
3094 varsize = bus->varsz ? roundup(bus->varsz, 4) : 0;
3095 varaddr = (bus->ramsize - 4) - varsize;
3096
3097 if (bus->vars) {
3098 vbuffer = kzalloc(varsize, GFP_ATOMIC);
3099 if (!vbuffer)
3100 return -ENOMEM;
3101
3102 memcpy(vbuffer, bus->vars, bus->varsz);
3103
3104 /* Write the vars list */
3105 bcmerror =
3106 brcmf_sdbrcm_membytes(bus, true, varaddr, vbuffer, varsize);
Joe Perches8ae74652012-01-15 00:38:38 -08003107#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02003108 /* Verify NVRAM bytes */
3109 brcmf_dbg(INFO, "Compare NVRAM dl & ul; varsize=%d\n", varsize);
3110 nvram_ularray = kmalloc(varsize, GFP_ATOMIC);
Jesper Juhlc40701e2012-01-08 23:41:21 +01003111 if (!nvram_ularray) {
3112 kfree(vbuffer);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003113 return -ENOMEM;
Jesper Juhlc40701e2012-01-08 23:41:21 +01003114 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003115
3116 /* Upload image to verify downloaded contents. */
3117 memset(nvram_ularray, 0xaa, varsize);
3118
3119 /* Read the vars list to temp buffer for comparison */
3120 bcmerror =
3121 brcmf_sdbrcm_membytes(bus, false, varaddr, nvram_ularray,
3122 varsize);
3123 if (bcmerror) {
3124 brcmf_dbg(ERROR, "error %d on reading %d nvram bytes at 0x%08x\n",
3125 bcmerror, varsize, varaddr);
3126 }
3127 /* Compare the org NVRAM with the one read from RAM */
3128 if (memcmp(vbuffer, nvram_ularray, varsize))
3129 brcmf_dbg(ERROR, "Downloaded NVRAM image is corrupted\n");
3130 else
3131 brcmf_dbg(ERROR, "Download/Upload/Compare of NVRAM ok\n");
3132
3133 kfree(nvram_ularray);
Joe Perches8ae74652012-01-15 00:38:38 -08003134#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003135
3136 kfree(vbuffer);
3137 }
3138
3139 /* adjust to the user specified RAM */
3140 brcmf_dbg(INFO, "Physical memory size: %d\n", bus->ramsize);
3141 brcmf_dbg(INFO, "Vars are at %d, orig varsize is %d\n",
3142 varaddr, varsize);
3143 varsize = ((bus->ramsize - 4) - varaddr);
3144
3145 /*
3146 * Determine the length token:
3147 * Varsize, converted to words, in lower 16-bits, checksum
3148 * in upper 16-bits.
3149 */
3150 if (bcmerror) {
3151 varsizew = 0;
3152 varsizew_le = cpu_to_le32(0);
3153 } else {
3154 varsizew = varsize / 4;
3155 varsizew = (~varsizew << 16) | (varsizew & 0x0000FFFF);
3156 varsizew_le = cpu_to_le32(varsizew);
3157 }
3158
3159 brcmf_dbg(INFO, "New varsize is %d, length token=0x%08x\n",
3160 varsize, varsizew);
3161
3162 /* Write the length token to the last word */
3163 bcmerror = brcmf_sdbrcm_membytes(bus, true, (bus->ramsize - 4),
3164 (u8 *)&varsizew_le, 4);
3165
3166 return bcmerror;
3167}
3168
Franky Line92eedf2011-11-22 17:21:50 -08003169static int brcmf_sdbrcm_download_state(struct brcmf_sdio *bus, bool enter)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003170{
3171 uint retries;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003172 int bcmerror = 0;
Franky Lin99ba15c2011-11-04 22:23:42 +01003173 struct chip_info *ci = bus->ci;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003174
3175 /* To enter download state, disable ARM and reset SOCRAM.
3176 * To exit download state, simply reset ARM (default is RAM boot).
3177 */
3178 if (enter) {
3179 bus->alp_only = true;
3180
Franky Lin086a2e02011-11-10 20:30:23 +01003181 ci->coredisable(bus->sdiodev, ci, BCMA_CORE_ARM_CM3);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003182
Franky Lind77e70f2011-11-10 20:30:24 +01003183 ci->resetcore(bus->sdiodev, ci, BCMA_CORE_INTERNAL_MEM);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003184
3185 /* Clear the top bit of memory */
3186 if (bus->ramsize) {
3187 u32 zeros = 0;
3188 brcmf_sdbrcm_membytes(bus, true, bus->ramsize - 4,
3189 (u8 *)&zeros, 4);
3190 }
3191 } else {
Franky Lin6ca687d2011-11-10 20:30:21 +01003192 if (!ci->iscoreup(bus->sdiodev, ci, BCMA_CORE_INTERNAL_MEM)) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003193 brcmf_dbg(ERROR, "SOCRAM core is down after reset?\n");
3194 bcmerror = -EBADE;
3195 goto fail;
3196 }
3197
3198 bcmerror = brcmf_sdbrcm_write_vars(bus);
3199 if (bcmerror) {
3200 brcmf_dbg(ERROR, "no vars written to RAM\n");
3201 bcmerror = 0;
3202 }
3203
3204 w_sdreg32(bus, 0xFFFFFFFF,
3205 offsetof(struct sdpcmd_regs, intstatus), &retries);
3206
Franky Lind77e70f2011-11-10 20:30:24 +01003207 ci->resetcore(bus->sdiodev, ci, BCMA_CORE_ARM_CM3);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003208
3209 /* Allow HT Clock now that the ARM is running. */
3210 bus->alp_only = false;
3211
Franky Lin712ac5b2011-12-16 18:37:09 -08003212 bus->sdiodev->bus_if->state = BRCMF_BUS_LOAD;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003213 }
3214fail:
3215 return bcmerror;
3216}
3217
Franky Line92eedf2011-11-22 17:21:50 -08003218static int brcmf_sdbrcm_get_image(char *buf, int len, struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003219{
3220 if (bus->firmware->size < bus->fw_ptr + len)
3221 len = bus->firmware->size - bus->fw_ptr;
3222
3223 memcpy(buf, &bus->firmware->data[bus->fw_ptr], len);
3224 bus->fw_ptr += len;
3225 return len;
3226}
3227
Franky Line92eedf2011-11-22 17:21:50 -08003228static int brcmf_sdbrcm_download_code_file(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003229{
3230 int offset = 0;
3231 uint len;
3232 u8 *memblock = NULL, *memptr;
3233 int ret;
3234
3235 brcmf_dbg(INFO, "Enter\n");
3236
Arend van Spriel52e14092012-02-09 21:09:09 +01003237 ret = request_firmware(&bus->firmware, BRCMF_SDIO_FW_NAME,
Arend van Spriel5b435de2011-10-05 13:19:03 +02003238 &bus->sdiodev->func[2]->dev);
3239 if (ret) {
3240 brcmf_dbg(ERROR, "Fail to request firmware %d\n", ret);
3241 return ret;
3242 }
3243 bus->fw_ptr = 0;
3244
3245 memptr = memblock = kmalloc(MEMBLOCK + BRCMF_SDALIGN, GFP_ATOMIC);
3246 if (memblock == NULL) {
3247 ret = -ENOMEM;
3248 goto err;
3249 }
3250 if ((u32)(unsigned long)memblock % BRCMF_SDALIGN)
3251 memptr += (BRCMF_SDALIGN -
3252 ((u32)(unsigned long)memblock % BRCMF_SDALIGN));
3253
3254 /* Download image */
3255 while ((len =
3256 brcmf_sdbrcm_get_image((char *)memptr, MEMBLOCK, bus))) {
3257 ret = brcmf_sdbrcm_membytes(bus, true, offset, memptr, len);
3258 if (ret) {
3259 brcmf_dbg(ERROR, "error %d on writing %d membytes at 0x%08x\n",
3260 ret, MEMBLOCK, offset);
3261 goto err;
3262 }
3263
3264 offset += MEMBLOCK;
3265 }
3266
3267err:
3268 kfree(memblock);
3269
3270 release_firmware(bus->firmware);
3271 bus->fw_ptr = 0;
3272
3273 return ret;
3274}
3275
3276/*
3277 * ProcessVars:Takes a buffer of "<var>=<value>\n" lines read from a file
3278 * and ending in a NUL.
3279 * Removes carriage returns, empty lines, comment lines, and converts
3280 * newlines to NULs.
3281 * Shortens buffer as needed and pads with NULs. End of buffer is marked
3282 * by two NULs.
3283*/
3284
3285static uint brcmf_process_nvram_vars(char *varbuf, uint len)
3286{
3287 char *dp;
3288 bool findNewline;
3289 int column;
3290 uint buf_len, n;
3291
3292 dp = varbuf;
3293
3294 findNewline = false;
3295 column = 0;
3296
3297 for (n = 0; n < len; n++) {
3298 if (varbuf[n] == 0)
3299 break;
3300 if (varbuf[n] == '\r')
3301 continue;
3302 if (findNewline && varbuf[n] != '\n')
3303 continue;
3304 findNewline = false;
3305 if (varbuf[n] == '#') {
3306 findNewline = true;
3307 continue;
3308 }
3309 if (varbuf[n] == '\n') {
3310 if (column == 0)
3311 continue;
3312 *dp++ = 0;
3313 column = 0;
3314 continue;
3315 }
3316 *dp++ = varbuf[n];
3317 column++;
3318 }
3319 buf_len = dp - varbuf;
3320
3321 while (dp < varbuf + n)
3322 *dp++ = 0;
3323
3324 return buf_len;
3325}
3326
Franky Line92eedf2011-11-22 17:21:50 -08003327static int brcmf_sdbrcm_download_nvram(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003328{
3329 uint len;
3330 char *memblock = NULL;
3331 char *bufp;
3332 int ret;
3333
Arend van Spriel52e14092012-02-09 21:09:09 +01003334 ret = request_firmware(&bus->firmware, BRCMF_SDIO_NV_NAME,
Arend van Spriel5b435de2011-10-05 13:19:03 +02003335 &bus->sdiodev->func[2]->dev);
3336 if (ret) {
3337 brcmf_dbg(ERROR, "Fail to request nvram %d\n", ret);
3338 return ret;
3339 }
3340 bus->fw_ptr = 0;
3341
3342 memblock = kmalloc(MEMBLOCK, GFP_ATOMIC);
3343 if (memblock == NULL) {
3344 ret = -ENOMEM;
3345 goto err;
3346 }
3347
3348 len = brcmf_sdbrcm_get_image(memblock, MEMBLOCK, bus);
3349
3350 if (len > 0 && len < MEMBLOCK) {
3351 bufp = (char *)memblock;
3352 bufp[len] = 0;
3353 len = brcmf_process_nvram_vars(bufp, len);
3354 bufp += len;
3355 *bufp++ = 0;
3356 if (len)
3357 ret = brcmf_sdbrcm_downloadvars(bus, memblock, len + 1);
3358 if (ret)
3359 brcmf_dbg(ERROR, "error downloading vars: %d\n", ret);
3360 } else {
3361 brcmf_dbg(ERROR, "error reading nvram file: %d\n", len);
3362 ret = -EIO;
3363 }
3364
3365err:
3366 kfree(memblock);
3367
3368 release_firmware(bus->firmware);
3369 bus->fw_ptr = 0;
3370
3371 return ret;
3372}
3373
Franky Line92eedf2011-11-22 17:21:50 -08003374static int _brcmf_sdbrcm_download_firmware(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003375{
3376 int bcmerror = -1;
3377
3378 /* Keep arm in reset */
3379 if (brcmf_sdbrcm_download_state(bus, true)) {
3380 brcmf_dbg(ERROR, "error placing ARM core in reset\n");
3381 goto err;
3382 }
3383
3384 /* External image takes precedence if specified */
3385 if (brcmf_sdbrcm_download_code_file(bus)) {
3386 brcmf_dbg(ERROR, "dongle image file download failed\n");
3387 goto err;
3388 }
3389
3390 /* External nvram takes precedence if specified */
3391 if (brcmf_sdbrcm_download_nvram(bus))
3392 brcmf_dbg(ERROR, "dongle nvram file download failed\n");
3393
3394 /* Take arm out of reset */
3395 if (brcmf_sdbrcm_download_state(bus, false)) {
3396 brcmf_dbg(ERROR, "error getting out of ARM core reset\n");
3397 goto err;
3398 }
3399
3400 bcmerror = 0;
3401
3402err:
3403 return bcmerror;
3404}
3405
3406static bool
Franky Line92eedf2011-11-22 17:21:50 -08003407brcmf_sdbrcm_download_firmware(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003408{
3409 bool ret;
3410
3411 /* Download the firmware */
3412 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3413
3414 ret = _brcmf_sdbrcm_download_firmware(bus) == 0;
3415
3416 brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
3417
3418 return ret;
3419}
3420
Franky Lin99a0b8f2011-12-16 18:37:14 -08003421static int brcmf_sdbrcm_bus_init(struct device *dev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003422{
Franky Linfa20b912011-11-22 17:21:57 -08003423 struct brcmf_bus *bus_if = dev_get_drvdata(dev);
Arend van Spriel0a332e42012-02-09 21:09:02 +01003424 struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
Franky Linfa20b912011-11-22 17:21:57 -08003425 struct brcmf_sdio *bus = sdiodev->bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003426 unsigned long timeout;
3427 uint retries = 0;
3428 u8 ready, enable;
3429 int err, ret = 0;
3430 u8 saveclk;
3431
3432 brcmf_dbg(TRACE, "Enter\n");
3433
3434 /* try to download image and nvram to the dongle */
Franky Linfa20b912011-11-22 17:21:57 -08003435 if (bus_if->state == BRCMF_BUS_DOWN) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003436 if (!(brcmf_sdbrcm_download_firmware(bus)))
3437 return -1;
3438 }
3439
Franky Lin712ac5b2011-12-16 18:37:09 -08003440 if (!bus->sdiodev->bus_if->drvr)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003441 return 0;
3442
3443 /* Start the watchdog timer */
Franky Lin28a1a3b2011-12-16 18:37:02 -08003444 bus->tickcnt = 0;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003445 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
3446
3447 down(&bus->sdsem);
3448
3449 /* Make sure backplane clock is on, needed to generate F2 interrupt */
3450 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3451 if (bus->clkstate != CLK_AVAIL)
3452 goto exit;
3453
3454 /* Force clocks on backplane to be sure F2 interrupt propagates */
Franky Lin45db3392012-05-04 18:27:32 -07003455 saveclk = brcmf_sdio_regrb(bus->sdiodev,
3456 SBSDIO_FUNC1_CHIPCLKCSR, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003457 if (!err) {
Franky Lin3bba8292012-05-04 18:27:33 -07003458 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3459 (saveclk | SBSDIO_FORCE_HT), &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003460 }
3461 if (err) {
3462 brcmf_dbg(ERROR, "Failed to force clock for F2: err %d\n", err);
3463 goto exit;
3464 }
3465
3466 /* Enable function 2 (frame transfers) */
3467 w_sdreg32(bus, SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT,
3468 offsetof(struct sdpcmd_regs, tosbmailboxdata), &retries);
3469 enable = (SDIO_FUNC_ENABLE_1 | SDIO_FUNC_ENABLE_2);
3470
Franky Lin3bba8292012-05-04 18:27:33 -07003471 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx, enable, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003472
3473 timeout = jiffies + msecs_to_jiffies(BRCMF_WAIT_F2RDY);
3474 ready = 0;
3475 while (enable != ready) {
Franky Lin45db3392012-05-04 18:27:32 -07003476 ready = brcmf_sdio_regrb(bus->sdiodev,
3477 SDIO_CCCR_IORx, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003478 if (time_after(jiffies, timeout))
3479 break;
3480 else if (time_after(jiffies, timeout - BRCMF_WAIT_F2RDY + 50))
3481 /* prevent busy waiting if it takes too long */
3482 msleep_interruptible(20);
3483 }
3484
3485 brcmf_dbg(INFO, "enable 0x%02x, ready 0x%02x\n", enable, ready);
3486
3487 /* If F2 successfully enabled, set core and enable interrupts */
3488 if (ready == enable) {
3489 /* Set up the interrupt mask and enable interrupts */
3490 bus->hostintmask = HOSTINTMASK;
3491 w_sdreg32(bus, bus->hostintmask,
3492 offsetof(struct sdpcmd_regs, hostintmask), &retries);
3493
Franky Lin3bba8292012-05-04 18:27:33 -07003494 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_WATERMARK, 8, &err);
Arend van Sprielc0e89f02012-02-09 21:09:04 +01003495 } else {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003496 /* Disable F2 again */
3497 enable = SDIO_FUNC_ENABLE_1;
Franky Lin3bba8292012-05-04 18:27:33 -07003498 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx, enable, NULL);
Arend van Sprielc0e89f02012-02-09 21:09:04 +01003499 ret = -ENODEV;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003500 }
3501
3502 /* Restore previous clock setting */
Franky Lin3bba8292012-05-04 18:27:33 -07003503 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, saveclk, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003504
Franky Line2f93cc2012-04-27 18:56:58 -07003505 if (ret == 0) {
Franky Linba89bf12012-04-27 18:56:59 -07003506 ret = brcmf_sdio_intr_register(bus->sdiodev);
Franky Line2f93cc2012-04-27 18:56:58 -07003507 if (ret != 0)
3508 brcmf_dbg(ERROR, "intr register failed:%d\n", ret);
3509 }
3510
Arend van Spriel5b435de2011-10-05 13:19:03 +02003511 /* If we didn't come up, turn off backplane clock */
Franky Lind9126e02012-04-27 18:56:57 -07003512 if (bus_if->state != BRCMF_BUS_DATA)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003513 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{
Franky Line92eedf2011-11-22 17:21:50 -08003523 struct brcmf_sdio *bus = (struct brcmf_sdio *) arg;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003524
3525 brcmf_dbg(TRACE, "Enter\n");
3526
3527 if (!bus) {
3528 brcmf_dbg(ERROR, "bus is null pointer, exiting\n");
3529 return;
3530 }
3531
Franky Lin712ac5b2011-12-16 18:37:09 -08003532 if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003533 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;
Franky Linb948a852012-04-23 14:24:53 -07003551 if (bus->dpc_tsk) {
3552 brcmf_sdbrcm_adddpctsk(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003553 complete(&bus->dpc_wait);
Franky Linb948a852012-04-23 14:24:53 -07003554 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003555}
3556
Franky Lincad2b262011-11-22 17:21:53 -08003557static bool brcmf_sdbrcm_bus_watchdog(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003558{
Joe Perches8ae74652012-01-15 00:38:38 -08003559#ifdef DEBUG
Franky Lincad2b262011-11-22 17:21:53 -08003560 struct brcmf_bus *bus_if = dev_get_drvdata(bus->sdiodev->dev);
Joe Perches8ae74652012-01-15 00:38:38 -08003561#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003562
3563 brcmf_dbg(TIMER, "Enter\n");
3564
Arend van Spriel5b435de2011-10-05 13:19:03 +02003565 /* Ignore the timer if simulating bus down */
3566 if (bus->sleeping)
3567 return false;
3568
3569 down(&bus->sdsem);
3570
3571 /* Poll period: check device if appropriate. */
3572 if (bus->poll && (++bus->polltick >= bus->pollrate)) {
3573 u32 intstatus = 0;
3574
3575 /* Reset poll tick */
3576 bus->polltick = 0;
3577
3578 /* Check device if no interrupts */
3579 if (!bus->intr || (bus->intrcount == bus->lastintrs)) {
3580
3581 if (!bus->dpc_sched) {
3582 u8 devpend;
Franky Lin45db3392012-05-04 18:27:32 -07003583 devpend = brcmf_sdio_regrb(bus->sdiodev,
3584 SDIO_CCCR_INTx,
3585 NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003586 intstatus =
3587 devpend & (INTR_STATUS_FUNC1 |
3588 INTR_STATUS_FUNC2);
3589 }
3590
3591 /* If there is something, make like the ISR and
3592 schedule the DPC */
3593 if (intstatus) {
3594 bus->pollcnt++;
3595 bus->ipend = true;
3596
3597 bus->dpc_sched = true;
Franky Linb948a852012-04-23 14:24:53 -07003598 if (bus->dpc_tsk) {
3599 brcmf_sdbrcm_adddpctsk(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003600 complete(&bus->dpc_wait);
Franky Linb948a852012-04-23 14:24:53 -07003601 }
Arend van Spriel5b435de2011-10-05 13:19:03 +02003602 }
3603 }
3604
3605 /* Update interrupt tracking */
3606 bus->lastintrs = bus->intrcount;
3607 }
Joe Perches8ae74652012-01-15 00:38:38 -08003608#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +02003609 /* Poll for console output periodically */
Franky Lincad2b262011-11-22 17:21:53 -08003610 if (bus_if->state == BRCMF_BUS_DATA &&
Franky Lin8d169aa2011-11-22 17:21:52 -08003611 bus->console_interval != 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003612 bus->console.count += BRCMF_WD_POLL_MS;
3613 if (bus->console.count >= bus->console_interval) {
3614 bus->console.count -= bus->console_interval;
3615 /* Make sure backplane clock is on */
3616 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3617 if (brcmf_sdbrcm_readconsole(bus) < 0)
3618 /* stop on error */
3619 bus->console_interval = 0;
3620 }
3621 }
Joe Perches8ae74652012-01-15 00:38:38 -08003622#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003623
3624 /* On idle timeout clear activity flag and/or turn off clock */
3625 if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
3626 if (++bus->idlecount >= bus->idletime) {
3627 bus->idlecount = 0;
3628 if (bus->activity) {
3629 bus->activity = false;
3630 brcmf_sdbrcm_wd_timer(bus, BRCMF_WD_POLL_MS);
3631 } else {
3632 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
3633 }
3634 }
3635 }
3636
3637 up(&bus->sdsem);
3638
3639 return bus->ipend;
3640}
3641
3642static bool brcmf_sdbrcm_chipmatch(u16 chipid)
3643{
3644 if (chipid == BCM4329_CHIP_ID)
3645 return true;
Franky Lince2d7d72011-12-08 15:06:39 -08003646 if (chipid == BCM4330_CHIP_ID)
3647 return true;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003648 return false;
3649}
3650
Franky Line92eedf2011-11-22 17:21:50 -08003651static void brcmf_sdbrcm_release_malloc(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003652{
3653 brcmf_dbg(TRACE, "Enter\n");
3654
3655 kfree(bus->rxbuf);
3656 bus->rxctl = bus->rxbuf = NULL;
3657 bus->rxlen = 0;
3658
3659 kfree(bus->databuf);
3660 bus->databuf = NULL;
3661}
3662
Franky Line92eedf2011-11-22 17:21:50 -08003663static bool brcmf_sdbrcm_probe_malloc(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003664{
3665 brcmf_dbg(TRACE, "Enter\n");
3666
Franky Linb01a6b32011-12-16 18:37:03 -08003667 if (bus->sdiodev->bus_if->maxctl) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003668 bus->rxblen =
Franky Linb01a6b32011-12-16 18:37:03 -08003669 roundup((bus->sdiodev->bus_if->maxctl + SDPCM_HDRLEN),
Arend van Spriel5b435de2011-10-05 13:19:03 +02003670 ALIGNMENT) + BRCMF_SDALIGN;
3671 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
3672 if (!(bus->rxbuf))
3673 goto fail;
3674 }
3675
3676 /* Allocate buffer to receive glomed packet */
3677 bus->databuf = kmalloc(MAX_DATA_BUF, GFP_ATOMIC);
3678 if (!(bus->databuf)) {
3679 /* release rxbuf which was already located as above */
3680 if (!bus->rxblen)
3681 kfree(bus->rxbuf);
3682 goto fail;
3683 }
3684
3685 /* Align the buffer */
3686 if ((unsigned long)bus->databuf % BRCMF_SDALIGN)
3687 bus->dataptr = bus->databuf + (BRCMF_SDALIGN -
3688 ((unsigned long)bus->databuf % BRCMF_SDALIGN));
3689 else
3690 bus->dataptr = bus->databuf;
3691
3692 return true;
3693
3694fail:
3695 return false;
3696}
3697
Arend van Spriel5b435de2011-10-05 13:19:03 +02003698static bool
Franky Line92eedf2011-11-22 17:21:50 -08003699brcmf_sdbrcm_probe_attach(struct brcmf_sdio *bus, u32 regsva)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003700{
3701 u8 clkctl = 0;
3702 int err = 0;
3703 int reg_addr;
3704 u32 reg_val;
Franky Lin99ba15c2011-11-04 22:23:42 +01003705 u8 idx;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003706
3707 bus->alp_only = true;
3708
Joe Perches18aad4f2012-01-15 00:38:42 -08003709 pr_debug("F1 signature read @0x18000000=0x%4x\n",
Franky Lin79ae3952012-05-04 18:27:34 -07003710 brcmf_sdio_regrl(bus->sdiodev, SI_ENUM_BASE, NULL));
Arend van Spriel5b435de2011-10-05 13:19:03 +02003711
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
Franky Lin3bba8292012-05-04 18:27:33 -07003717 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3718 BRCMF_INIT_CLKCTL1, &err);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003719 if (!err)
Franky Lin45db3392012-05-04 18:27:32 -07003720 clkctl = brcmf_sdio_regrb(bus->sdiodev,
Arend van Spriel5b435de2011-10-05 13:19:03 +02003721 SBSDIO_FUNC1_CHIPCLKCSR, &err);
3722
3723 if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
3724 brcmf_dbg(ERROR, "ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
3725 err, BRCMF_INIT_CLKCTL1, clkctl);
3726 goto fail;
3727 }
3728
Franky Lina97e4fc2011-11-04 22:23:35 +01003729 if (brcmf_sdio_chip_attach(bus->sdiodev, &bus->ci, regsva)) {
3730 brcmf_dbg(ERROR, "brcmf_sdio_chip_attach failed!\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003731 goto fail;
3732 }
3733
3734 if (!brcmf_sdbrcm_chipmatch((u16) bus->ci->chip)) {
3735 brcmf_dbg(ERROR, "unsupported chip: 0x%04x\n", bus->ci->chip);
3736 goto fail;
3737 }
3738
Franky Line12afb62011-11-04 22:23:40 +01003739 brcmf_sdio_chip_drivestrengthinit(bus->sdiodev, bus->ci,
3740 SDIO_DRIVE_STRENGTH);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003741
Franky Lin454d2a82011-11-04 22:23:37 +01003742 /* Get info on the SOCRAM cores... */
Arend van Spriel5b435de2011-10-05 13:19:03 +02003743 bus->ramsize = bus->ci->ramsize;
3744 if (!(bus->ramsize)) {
3745 brcmf_dbg(ERROR, "failed to find SOCRAM memory!\n");
3746 goto fail;
3747 }
3748
3749 /* Set core control so an SDIO reset does a backplane reset */
Franky Lin99ba15c2011-11-04 22:23:42 +01003750 idx = brcmf_sdio_chip_getinfidx(bus->ci, BCMA_CORE_SDIO_DEV);
3751 reg_addr = bus->ci->c_inf[idx].base +
Arend van Spriel5b435de2011-10-05 13:19:03 +02003752 offsetof(struct sdpcmd_regs, corecontrol);
Franky Lin79ae3952012-05-04 18:27:34 -07003753 reg_val = brcmf_sdio_regrl(bus->sdiodev, reg_addr, NULL);
Franky Line13ce262012-05-04 18:27:35 -07003754 brcmf_sdio_regwl(bus->sdiodev, reg_addr, reg_val | CC_BPRESEN, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003755
3756 brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
3757
3758 /* Locate an appropriately-aligned portion of hdrbuf */
3759 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
3760 BRCMF_SDALIGN);
3761
3762 /* Set the poll and/or interrupt flags */
3763 bus->intr = true;
3764 bus->poll = false;
3765 if (bus->poll)
3766 bus->pollrate = 1;
3767
3768 return true;
3769
3770fail:
3771 return false;
3772}
3773
Franky Line92eedf2011-11-22 17:21:50 -08003774static bool brcmf_sdbrcm_probe_init(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003775{
3776 brcmf_dbg(TRACE, "Enter\n");
3777
3778 /* Disable F2 to clear any intermediate frame state on the dongle */
Franky Lin3bba8292012-05-04 18:27:33 -07003779 brcmf_sdio_regwb(bus->sdiodev, SDIO_CCCR_IOEx,
3780 SDIO_FUNC_ENABLE_1, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003781
Franky Lin712ac5b2011-12-16 18:37:09 -08003782 bus->sdiodev->bus_if->state = BRCMF_BUS_DOWN;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003783 bus->sleeping = false;
3784 bus->rxflow = false;
3785
3786 /* Done with backplane-dependent accesses, can drop clock... */
Franky Lin3bba8292012-05-04 18:27:33 -07003787 brcmf_sdio_regwb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003788
3789 /* ...and initialize clock/power states */
3790 bus->clkstate = CLK_SDONLY;
3791 bus->idletime = BRCMF_IDLE_INTERVAL;
3792 bus->idleclock = BRCMF_IDLE_ACTIVE;
3793
3794 /* Query the F2 block size, set roundup accordingly */
3795 bus->blocksize = bus->sdiodev->func[2]->cur_blksize;
3796 bus->roundup = min(max_roundup, bus->blocksize);
3797
3798 /* bus module does not support packet chaining */
3799 bus->use_rxchain = false;
3800 bus->sd_rxchain = false;
3801
3802 return true;
3803}
3804
3805static int
3806brcmf_sdbrcm_watchdog_thread(void *data)
3807{
Franky Line92eedf2011-11-22 17:21:50 -08003808 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003809
3810 allow_signal(SIGTERM);
3811 /* Run until signal received */
3812 while (1) {
3813 if (kthread_should_stop())
3814 break;
3815 if (!wait_for_completion_interruptible(&bus->watchdog_wait)) {
Franky Lincad2b262011-11-22 17:21:53 -08003816 brcmf_sdbrcm_bus_watchdog(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003817 /* Count the tick for reference */
Franky Lin28a1a3b2011-12-16 18:37:02 -08003818 bus->tickcnt++;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003819 } else
3820 break;
3821 }
3822 return 0;
3823}
3824
3825static void
3826brcmf_sdbrcm_watchdog(unsigned long data)
3827{
Franky Line92eedf2011-11-22 17:21:50 -08003828 struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003829
3830 if (bus->watchdog_tsk) {
3831 complete(&bus->watchdog_wait);
3832 /* Reschedule the watchdog */
3833 if (bus->wd_timer_valid)
3834 mod_timer(&bus->timer,
3835 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
3836 }
3837}
3838
Franky Line92eedf2011-11-22 17:21:50 -08003839static void brcmf_sdbrcm_release_dongle(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003840{
3841 brcmf_dbg(TRACE, "Enter\n");
3842
3843 if (bus->ci) {
3844 brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
3845 brcmf_sdbrcm_clkctl(bus, CLK_NONE, false);
Franky Lina8a6c042011-11-04 22:23:39 +01003846 brcmf_sdio_chip_detach(&bus->ci);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003847 if (bus->vars && bus->varsz)
3848 kfree(bus->vars);
3849 bus->vars = NULL;
3850 }
3851
3852 brcmf_dbg(TRACE, "Disconnected\n");
3853}
3854
3855/* Detach and free everything */
Franky Line92eedf2011-11-22 17:21:50 -08003856static void brcmf_sdbrcm_release(struct brcmf_sdio *bus)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003857{
3858 brcmf_dbg(TRACE, "Enter\n");
3859
3860 if (bus) {
3861 /* De-register interrupt handler */
Franky Linba89bf12012-04-27 18:56:59 -07003862 brcmf_sdio_intr_unregister(bus->sdiodev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003863
Franky Lin5f947ad2011-12-16 18:36:56 -08003864 if (bus->sdiodev->bus_if->drvr) {
3865 brcmf_detach(bus->sdiodev->dev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003866 brcmf_sdbrcm_release_dongle(bus);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003867 }
3868
3869 brcmf_sdbrcm_release_malloc(bus);
3870
3871 kfree(bus);
3872 }
3873
3874 brcmf_dbg(TRACE, "Disconnected\n");
3875}
3876
Franky Lin4175b882011-11-22 17:21:49 -08003877void *brcmf_sdbrcm_probe(u32 regsva, struct brcmf_sdio_dev *sdiodev)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003878{
3879 int ret;
Franky Line92eedf2011-11-22 17:21:50 -08003880 struct brcmf_sdio *bus;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003881
Arend van Spriel5b435de2011-10-05 13:19:03 +02003882 brcmf_dbg(TRACE, "Enter\n");
3883
3884 /* We make an assumption about address window mappings:
3885 * regsva == SI_ENUM_BASE*/
3886
3887 /* Allocate private bus interface state */
Franky Line92eedf2011-11-22 17:21:50 -08003888 bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003889 if (!bus)
3890 goto fail;
3891
3892 bus->sdiodev = sdiodev;
3893 sdiodev->bus = bus;
Arend van Sprielb83db862011-10-19 12:51:09 +02003894 skb_queue_head_init(&bus->glom);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003895 bus->txbound = BRCMF_TXBOUND;
3896 bus->rxbound = BRCMF_RXBOUND;
3897 bus->txminmax = BRCMF_TXMINMAX;
3898 bus->tx_seq = SDPCM_SEQUENCE_WRAP - 1;
3899 bus->usebufpool = false; /* Use bufpool if allocated,
3900 else use locally malloced rxbuf */
3901
3902 /* attempt to attach to the dongle */
3903 if (!(brcmf_sdbrcm_probe_attach(bus, regsva))) {
3904 brcmf_dbg(ERROR, "brcmf_sdbrcm_probe_attach failed\n");
3905 goto fail;
3906 }
3907
3908 spin_lock_init(&bus->txqlock);
3909 init_waitqueue_head(&bus->ctrl_wait);
3910 init_waitqueue_head(&bus->dcmd_resp_wait);
3911
3912 /* Set up the watchdog timer */
3913 init_timer(&bus->timer);
3914 bus->timer.data = (unsigned long)bus;
3915 bus->timer.function = brcmf_sdbrcm_watchdog;
3916
3917 /* Initialize thread based operation and lock */
3918 sema_init(&bus->sdsem, 1);
3919
3920 /* Initialize watchdog thread */
3921 init_completion(&bus->watchdog_wait);
3922 bus->watchdog_tsk = kthread_run(brcmf_sdbrcm_watchdog_thread,
3923 bus, "brcmf_watchdog");
3924 if (IS_ERR(bus->watchdog_tsk)) {
Joe Perches02f77192012-01-15 00:38:44 -08003925 pr_warn("brcmf_watchdog thread failed to start\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003926 bus->watchdog_tsk = NULL;
3927 }
3928 /* Initialize DPC thread */
3929 init_completion(&bus->dpc_wait);
Franky Linb948a852012-04-23 14:24:53 -07003930 INIT_LIST_HEAD(&bus->dpc_tsklst);
3931 spin_lock_init(&bus->dpc_tl_lock);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003932 bus->dpc_tsk = kthread_run(brcmf_sdbrcm_dpc_thread,
3933 bus, "brcmf_dpc");
3934 if (IS_ERR(bus->dpc_tsk)) {
Joe Perches02f77192012-01-15 00:38:44 -08003935 pr_warn("brcmf_dpc thread failed to start\n");
Arend van Spriel5b435de2011-10-05 13:19:03 +02003936 bus->dpc_tsk = NULL;
3937 }
3938
Franky Lina9ffda82011-12-16 18:37:12 -08003939 /* Assign bus interface call back */
3940 bus->sdiodev->bus_if->brcmf_bus_stop = brcmf_sdbrcm_bus_stop;
Franky Lin99a0b8f2011-12-16 18:37:14 -08003941 bus->sdiodev->bus_if->brcmf_bus_init = brcmf_sdbrcm_bus_init;
Franky Linb9692d12011-12-16 18:37:15 -08003942 bus->sdiodev->bus_if->brcmf_bus_txdata = brcmf_sdbrcm_bus_txdata;
Franky Linfcf094f2011-12-16 18:37:16 -08003943 bus->sdiodev->bus_if->brcmf_bus_txctl = brcmf_sdbrcm_bus_txctl;
3944 bus->sdiodev->bus_if->brcmf_bus_rxctl = brcmf_sdbrcm_bus_rxctl;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003945 /* Attach to the brcmf/OS/network interface */
Franky Lin2447ffb2011-12-16 18:37:10 -08003946 ret = brcmf_attach(SDPCM_RESERVE, bus->sdiodev->dev);
Franky Lin712ac5b2011-12-16 18:37:09 -08003947 if (ret != 0) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003948 brcmf_dbg(ERROR, "brcmf_attach failed\n");
3949 goto fail;
3950 }
3951
3952 /* Allocate buffers */
3953 if (!(brcmf_sdbrcm_probe_malloc(bus))) {
3954 brcmf_dbg(ERROR, "brcmf_sdbrcm_probe_malloc failed\n");
3955 goto fail;
3956 }
3957
3958 if (!(brcmf_sdbrcm_probe_init(bus))) {
3959 brcmf_dbg(ERROR, "brcmf_sdbrcm_probe_init failed\n");
3960 goto fail;
3961 }
3962
Arend van Spriel5b435de2011-10-05 13:19:03 +02003963 brcmf_dbg(INFO, "completed!!\n");
3964
3965 /* if firmware path present try to download and bring up bus */
Franky Lined683c92011-12-16 18:36:55 -08003966 ret = brcmf_bus_start(bus->sdiodev->dev);
Arend van Spriel5b435de2011-10-05 13:19:03 +02003967 if (ret != 0) {
3968 if (ret == -ENOLINK) {
3969 brcmf_dbg(ERROR, "dongle is not responding\n");
3970 goto fail;
3971 }
3972 }
Franky Lin15d45b62011-10-21 16:16:32 +02003973
Arend van Spriel5b435de2011-10-05 13:19:03 +02003974 return bus;
3975
3976fail:
3977 brcmf_sdbrcm_release(bus);
3978 return NULL;
3979}
3980
3981void brcmf_sdbrcm_disconnect(void *ptr)
3982{
Franky Line92eedf2011-11-22 17:21:50 -08003983 struct brcmf_sdio *bus = (struct brcmf_sdio *)ptr;
Arend van Spriel5b435de2011-10-05 13:19:03 +02003984
3985 brcmf_dbg(TRACE, "Enter\n");
3986
3987 if (bus)
3988 brcmf_sdbrcm_release(bus);
3989
3990 brcmf_dbg(TRACE, "Disconnected\n");
3991}
3992
Arend van Spriel5b435de2011-10-05 13:19:03 +02003993void
Franky Line92eedf2011-11-22 17:21:50 -08003994brcmf_sdbrcm_wd_timer(struct brcmf_sdio *bus, uint wdtick)
Arend van Spriel5b435de2011-10-05 13:19:03 +02003995{
Arend van Spriel5b435de2011-10-05 13:19:03 +02003996 /* Totally stop the timer */
Joe Perches23677ce2012-02-09 11:17:23 +00003997 if (!wdtick && bus->wd_timer_valid) {
Arend van Spriel5b435de2011-10-05 13:19:03 +02003998 del_timer_sync(&bus->timer);
3999 bus->wd_timer_valid = false;
4000 bus->save_ms = wdtick;
4001 return;
4002 }
4003
Franky Linece960e2011-10-21 16:16:19 +02004004 /* don't start the wd until fw is loaded */
Franky Lin712ac5b2011-12-16 18:37:09 -08004005 if (bus->sdiodev->bus_if->state == BRCMF_BUS_DOWN)
Franky Linece960e2011-10-21 16:16:19 +02004006 return;
4007
Arend van Spriel5b435de2011-10-05 13:19:03 +02004008 if (wdtick) {
4009 if (bus->save_ms != BRCMF_WD_POLL_MS) {
Joe Perches23677ce2012-02-09 11:17:23 +00004010 if (bus->wd_timer_valid)
Arend van Spriel5b435de2011-10-05 13:19:03 +02004011 /* Stop timer and restart at new value */
4012 del_timer_sync(&bus->timer);
4013
4014 /* Create timer again when watchdog period is
4015 dynamically changed or in the first instance
4016 */
4017 bus->timer.expires =
4018 jiffies + BRCMF_WD_POLL_MS * HZ / 1000;
4019 add_timer(&bus->timer);
4020
4021 } else {
4022 /* Re arm the timer, at last watchdog period */
4023 mod_timer(&bus->timer,
4024 jiffies + BRCMF_WD_POLL_MS * HZ / 1000);
4025 }
4026
4027 bus->wd_timer_valid = true;
4028 bus->save_ms = wdtick;
4029 }
4030}