blob: 6b0dd87ca0a4dd6f86d677b8de8bf0124336a499 [file] [log] [blame]
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001/*
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
Greg Kroah-Hartmana1c16ed2010-10-21 11:17:44 -070017#include <linux/types.h>
Arend van Spriel34227312011-05-10 22:25:32 +020018#include <linux/kernel.h>
19#include <linux/printk.h>
Stanislav Fomichevbe1c09f2011-03-28 01:31:36 +040020#include <linux/pci_ids.h>
Brett Rudleyc6ac24e2010-10-26 11:55:23 -070021#include <linux/netdevice.h>
Arend van Spriel34227312011-05-10 22:25:32 +020022#include <bcmdefs.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -070023#include <bcmsdh.h>
24
25#ifdef BCMEMBEDIMAGE
26#include BCMEMBEDIMAGE
27#endif /* BCMEMBEDIMAGE */
28
29#include <bcmdefs.h>
30#include <bcmutils.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -070031#include <bcmdevs.h>
32
Roland Vossen189aed02011-06-01 13:45:07 +020033#include <bcmsoc.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -070034#ifdef DHD_DEBUG
Roland Vossenf1c7a082011-06-01 13:45:09 +020035
36/* ARM trap handling */
37
38/* Trap types defined by ARM (see arminc.h) */
39
40/* Trap locations in lo memory */
41#define TRAP_STRIDE 4
42#define FIRST_TRAP TR_RST
43#define LAST_TRAP (TR_FIQ * TRAP_STRIDE)
44
45#if defined(__ARM_ARCH_4T__)
46#define MAX_TRAP_TYPE (TR_FIQ + 1)
47#elif defined(__ARM_ARCH_7M__)
48#define MAX_TRAP_TYPE (TR_ISR + ARMCM3_NUMINTS)
49#endif /* __ARM_ARCH_7M__ */
50
51/* The trap structure is defined here as offsets for assembly */
52#define TR_TYPE 0x00
53#define TR_EPC 0x04
54#define TR_CPSR 0x08
55#define TR_SPSR 0x0c
56#define TR_REGS 0x10
57#define TR_REG(n) (TR_REGS + (n) * 4)
58#define TR_SP TR_REG(13)
59#define TR_LR TR_REG(14)
60#define TR_PC TR_REG(15)
61
62#define TRAP_T_SIZE 80
63
64#ifndef _LANGUAGE_ASSEMBLY
65
66typedef struct _trap_struct {
67 u32 type;
68 u32 epc;
69 u32 cpsr;
70 u32 spsr;
71 u32 r0;
72 u32 r1;
73 u32 r2;
74 u32 r3;
75 u32 r4;
76 u32 r5;
77 u32 r6;
78 u32 r7;
79 u32 r8;
80 u32 r9;
81 u32 r10;
82 u32 r11;
83 u32 r12;
84 u32 r13;
85 u32 r14;
86 u32 pc;
87} trap_t;
88
89#endif /* !_LANGUAGE_ASSEMBLY */
90
91#define CBUF_LEN (128)
92
93#define LOG_BUF_LEN 1024
94
95typedef struct {
96 u32 buf; /* Can't be pointer on (64-bit) hosts */
97 uint buf_size;
98 uint idx;
99 char *_buf_compat; /* Redundant pointer for backward compat. */
100} rte_log_t;
101
102typedef struct {
103 /* Virtual UART
104 * When there is no UART (e.g. Quickturn),
105 * the host should write a complete
106 * input line directly into cbuf and then write
107 * the length into vcons_in.
108 * This may also be used when there is a real UART
109 * (at risk of conflicting with
110 * the real UART). vcons_out is currently unused.
111 */
112 volatile uint vcons_in;
113 volatile uint vcons_out;
114
115 /* Output (logging) buffer
116 * Console output is written to a ring buffer log_buf at index log_idx.
117 * The host may read the output when it sees log_idx advance.
118 * Output will be lost if the output wraps around faster than the host
119 * polls.
120 */
121 rte_log_t log;
122
123 /* Console input line buffer
124 * Characters are read one at a time into cbuf
125 * until <CR> is received, then
126 * the buffer is processed as a command line.
127 * Also used for virtual UART.
128 */
129 uint cbuf_idx;
130 char cbuf[CBUF_LEN];
131} rte_cons_t;
132
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700133#endif /* DHD_DEBUG */
134#include <sbchipc.h>
Roland Vossen189aed02011-06-01 13:45:07 +0200135#include <sbdma.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700136
137#include <sdio.h>
138#include <sbsdio.h>
139#include <sbsdpcmdev.h>
140#include <bcmsdpcm.h>
141
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700142#include <proto/802.11.h>
143
144#include <dngl_stats.h>
145#include <dhd.h>
146#include <dhd_bus.h>
147#include <dhd_proto.h>
148#include <dhd_dbg.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700149#include <sdiovar.h>
Franky Lincb63e4c2011-04-25 15:45:08 -0700150#include <bcmchip.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700151
152#ifndef DHDSDIO_MEM_DUMP_FNAME
153#define DHDSDIO_MEM_DUMP_FNAME "mem_dump"
154#endif
155
Grant Grundler26a71a42011-03-09 10:41:25 -0800156#define TXQLEN 2048 /* bulk tx queue length */
157#define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
158#define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700159#define PRIOMASK 7
160
161#define TXRETRIES 2 /* # of retries for tx frames */
162
163#if defined(CONFIG_MACH_SANDGATE2G)
164#define DHD_RXBOUND 250 /* Default for max rx frames in
165 one scheduling */
166#else
167#define DHD_RXBOUND 50 /* Default for max rx frames in
168 one scheduling */
169#endif /* defined(CONFIG_MACH_SANDGATE2G) */
170
171#define DHD_TXBOUND 20 /* Default for max tx frames in
172 one scheduling */
173
174#define DHD_TXMINMAX 1 /* Max tx frames if rx still pending */
175
176#define MEMBLOCK 2048 /* Block size used for downloading
177 of dongle image */
178#define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
179 biggest possible glom */
180
181/* Packet alignment for most efficient SDIO (can change based on platform) */
182#ifndef DHD_SDALIGN
183#define DHD_SDALIGN 32
184#endif
185#if !ISPOWEROF2(DHD_SDALIGN)
186#error DHD_SDALIGN is not a power of 2!
187#endif
188
189#ifndef DHD_FIRSTREAD
190#define DHD_FIRSTREAD 32
191#endif
192#if !ISPOWEROF2(DHD_FIRSTREAD)
193#error DHD_FIRSTREAD is not a power of 2!
194#endif
195
196/* Total length of frame header for dongle protocol */
197#define SDPCM_HDRLEN (SDPCM_FRAMETAG_LEN + SDPCM_SWHEADER_LEN)
198#ifdef SDTEST
199#define SDPCM_RESERVE (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + DHD_SDALIGN)
200#else
201#define SDPCM_RESERVE (SDPCM_HDRLEN + DHD_SDALIGN)
202#endif
203
204/* Space for header read, limit for data packets */
205#ifndef MAX_HDR_READ
206#define MAX_HDR_READ 32
207#endif
208#if !ISPOWEROF2(MAX_HDR_READ)
209#error MAX_HDR_READ is not a power of 2!
210#endif
211
212#define MAX_RX_DATASZ 2048
213
214/* Maximum milliseconds to wait for F2 to come up */
215#define DHD_WAIT_F2RDY 3000
216
217/* Bump up limit on waiting for HT to account for first startup;
218 * if the image is doing a CRC calculation before programming the PMU
219 * for HT availability, it could take a couple hundred ms more, so
220 * max out at a 1 second (1000000us).
221 */
222#if (PMU_MAX_TRANSITION_DLY <= 1000000)
223#undef PMU_MAX_TRANSITION_DLY
224#define PMU_MAX_TRANSITION_DLY 1000000
225#endif
226
227/* Value for ChipClockCSR during initial setup */
228#define DHD_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
229 SBSDIO_ALP_AVAIL_REQ)
230#define DHD_INIT_CLKCTL2 (SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP)
231
232/* Flags for SDH calls */
233#define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
234
Arend van Spriel70dfb582011-02-25 16:39:17 +0100235/*
236 * Conversion of 802.1D priority to precedence level
237 */
238#define PRIO2PREC(prio) \
239 (((prio) == PRIO_8021D_NONE || (prio) == PRIO_8021D_BE) ? \
240 ((prio^2)) : (prio))
241
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700242DHD_SPINWAIT_SLEEP_INIT(sdioh_spinwait_sleep);
243extern int dhdcdc_set_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf,
244 uint len);
245
246#ifdef DHD_DEBUG
247/* Device console log buffer state */
248typedef struct dhd_console {
249 uint count; /* Poll interval msec counter */
250 uint log_addr; /* Log struct address (fixed) */
Roland Vossen70963f92011-06-01 13:45:08 +0200251 rte_log_t log; /* Log struct (host copy) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700252 uint bufsize; /* Size of log buffer */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700253 u8 *buf; /* Log buffer (host copy) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700254 uint last; /* Last buffer read index */
255} dhd_console_t;
256#endif /* DHD_DEBUG */
257
Franky Lincb63e4c2011-04-25 15:45:08 -0700258/* misc chip info needed by some of the routines */
259struct chip_info {
260 u32 chip;
261 u32 chiprev;
262 u32 cccorebase;
263 u32 ccrev;
264 u32 cccaps;
265 u32 buscorebase;
266 u32 buscorerev;
267 u32 buscoretype;
268 u32 ramcorebase;
269 u32 armcorebase;
270 u32 pmurev;
Franky Linc05df632011-04-25 19:34:07 -0700271 u32 ramsize;
Franky Lincb63e4c2011-04-25 15:45:08 -0700272};
273
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700274/* Private data for SDIO bus interaction */
275typedef struct dhd_bus {
276 dhd_pub_t *dhd;
277
278 bcmsdh_info_t *sdh; /* Handle for BCMSDH calls */
Franky Lincb63e4c2011-04-25 15:45:08 -0700279 struct chip_info *ci; /* Chip info struct */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700280 char *vars; /* Variables (from CIS and/or other) */
281 uint varsz; /* Size of variables buffer */
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700282 u32 sbaddr; /* Current SB window pointer (-1, invalid) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700283
284 sdpcmd_regs_t *regs; /* Registers for SDIO core */
285 uint sdpcmrev; /* SDIO core revision */
286 uint armrev; /* CPU core revision */
287 uint ramrev; /* SOCRAM core revision */
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700288 u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
289 u32 orig_ramsize; /* Size of RAM in SOCRAM (bytes) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700290
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700291 u32 bus; /* gSPI or SDIO bus */
292 u32 hostintmask; /* Copy of Host Interrupt Mask */
293 u32 intstatus; /* Intstatus bits (events) pending */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700294 bool dpc_sched; /* Indicates DPC schedule (intrpt rcvd) */
295 bool fcstate; /* State of dongle flow-control */
296
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700297 u16 cl_devid; /* cached devid for dhdsdio_probe_attach() */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700298 char *fw_path; /* module_param: path to firmware image */
299 char *nv_path; /* module_param: path to nvram vars file */
300 const char *nvram_params; /* user specified nvram params. */
301
302 uint blocksize; /* Block size of SDIO transfers */
303 uint roundup; /* Max roundup limit */
304
305 struct pktq txq; /* Queue length used for flow-control */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700306 u8 flowcontrol; /* per prio flow control bitmask */
307 u8 tx_seq; /* Transmit sequence number (next) */
308 u8 tx_max; /* Maximum transmit sequence allowed */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700309
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700310 u8 hdrbuf[MAX_HDR_READ + DHD_SDALIGN];
311 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700312 u16 nextlen; /* Next Read Len from last header */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700313 u8 rx_seq; /* Receive sequence number (expected) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700314 bool rxskip; /* Skip receive (awaiting NAK ACK) */
315
Arend van Sprielc26b1372010-11-23 14:06:23 +0100316 struct sk_buff *glomd; /* Packet containing glomming descriptor */
317 struct sk_buff *glom; /* Packet chain for glommed superframe */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700318 uint glomerr; /* Glom packet read errors */
319
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700320 u8 *rxbuf; /* Buffer for receiving control packets */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700321 uint rxblen; /* Allocated length of rxbuf */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700322 u8 *rxctl; /* Aligned pointer into rxbuf */
323 u8 *databuf; /* Buffer for receiving big glom packet */
324 u8 *dataptr; /* Aligned pointer into databuf */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700325 uint rxlen; /* Length of valid data in buffer */
326
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700327 u8 sdpcm_ver; /* Bus protocol reported by dongle */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700328
329 bool intr; /* Use interrupts */
330 bool poll; /* Use polling */
331 bool ipend; /* Device interrupt is pending */
332 bool intdis; /* Interrupts disabled by isr */
333 uint intrcount; /* Count of device interrupt callbacks */
334 uint lastintrs; /* Count as of last watchdog timer */
335 uint spurious; /* Count of spurious interrupts */
336 uint pollrate; /* Ticks between device polls */
337 uint polltick; /* Tick counter */
338 uint pollcnt; /* Count of active polls */
339
340#ifdef DHD_DEBUG
341 dhd_console_t console; /* Console output polling support */
342 uint console_addr; /* Console address from shared struct */
343#endif /* DHD_DEBUG */
344
345 uint regfails; /* Count of R_REG/W_REG failures */
346
347 uint clkstate; /* State of sd and backplane clock(s) */
348 bool activity; /* Activity flag for clock down */
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -0700349 s32 idletime; /* Control for activity timeout */
350 s32 idlecount; /* Activity timeout counter */
351 s32 idleclock; /* How to set bus driver when idle */
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -0700352 s32 sd_rxchain; /* If bcmsdh api accepts PKT chains */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700353 bool use_rxchain; /* If dhd should use PKT chains */
354 bool sleeping; /* Is SDIO bus sleeping? */
355 bool rxflow_mode; /* Rx flow control mode */
356 bool rxflow; /* Is rx flow control on */
357 uint prev_rxlim_hit; /* Is prev rx limit exceeded
358 (per dpc schedule) */
359 bool alp_only; /* Don't use HT clock (ALP only) */
360/* Field to decide if rx of control frames happen in rxbuf or lb-pool */
361 bool usebufpool;
362
363#ifdef SDTEST
364 /* external loopback */
365 bool ext_loop;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700366 u8 loopid;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700367
368 /* pktgen configuration */
369 uint pktgen_freq; /* Ticks between bursts */
370 uint pktgen_count; /* Packets to send each burst */
371 uint pktgen_print; /* Bursts between count displays */
372 uint pktgen_total; /* Stop after this many */
373 uint pktgen_minlen; /* Minimum packet data len */
374 uint pktgen_maxlen; /* Maximum packet data len */
375 uint pktgen_mode; /* Configured mode: tx, rx, or echo */
376 uint pktgen_stop; /* Number of tx failures causing stop */
377
378 /* active pktgen fields */
379 uint pktgen_tick; /* Tick counter for bursts */
380 uint pktgen_ptick; /* Burst counter for printing */
381 uint pktgen_sent; /* Number of test packets generated */
382 uint pktgen_rcvd; /* Number of test packets received */
383 uint pktgen_fail; /* Number of failed send attempts */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700384 u16 pktgen_len; /* Length of next packet to send */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700385#endif /* SDTEST */
386
387 /* Some additional counters */
388 uint tx_sderrs; /* Count of tx attempts with sd errors */
389 uint fcqueued; /* Tx packets that got queued */
390 uint rxrtx; /* Count of rtx requests (NAK to dongle) */
391 uint rx_toolong; /* Receive frames too long to receive */
392 uint rxc_errors; /* SDIO errors when reading control frames */
393 uint rx_hdrfail; /* SDIO errors on header reads */
394 uint rx_badhdr; /* Bad received headers (roosync?) */
395 uint rx_badseq; /* Mismatched rx sequence number */
396 uint fc_rcvd; /* Number of flow-control events received */
397 uint fc_xoff; /* Number which turned on flow-control */
398 uint fc_xon; /* Number which turned off flow-control */
399 uint rxglomfail; /* Failed deglom attempts */
400 uint rxglomframes; /* Number of glom frames (superframes) */
401 uint rxglompkts; /* Number of packets from glom frames */
402 uint f2rxhdrs; /* Number of header reads */
403 uint f2rxdata; /* Number of frame data reads */
404 uint f2txdata; /* Number of f2 frame writes */
405 uint f1regdata; /* Number of f1 register accesses */
406
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700407 u8 *ctrl_frame_buf;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700408 u32 ctrl_frame_len;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700409 bool ctrl_frame_stat;
410} dhd_bus_t;
411
412/* clkstate */
413#define CLK_NONE 0
414#define CLK_SDONLY 1
415#define CLK_PENDING 2 /* Not used yet */
416#define CLK_AVAIL 3
417
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700418#define DHD_NOPMU(dhd) (false)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700419
420#ifdef DHD_DEBUG
421static int qcount[NUMPRIO];
422static int tx_packets[NUMPRIO];
423#endif /* DHD_DEBUG */
424
425/* Deferred transmit */
426const uint dhd_deferred_tx = 1;
427
428extern uint dhd_watchdog_ms;
429extern void dhd_os_wd_timer(void *bus, uint wdtick);
430
431/* Tx/Rx bounds */
432uint dhd_txbound;
433uint dhd_rxbound;
434uint dhd_txminmax;
435
436/* override the RAM size if possible */
437#define DONGLE_MIN_MEMSIZE (128 * 1024)
438int dhd_dongle_memsize;
439
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700440static bool dhd_alignctl;
441
442static bool sd1idle;
443
444static bool retrydata;
445#define RETRYCHAN(chan) (((chan) == SDPCM_EVENT_CHANNEL) || retrydata)
446
447static const uint watermark = 8;
448static const uint firstread = DHD_FIRSTREAD;
449
450#define HDATLEN (firstread - (SDPCM_HDRLEN))
451
452/* Retry count for register access failures */
453static const uint retry_limit = 2;
454
455/* Force even SD lengths (some host controllers mess up on odd bytes) */
456static bool forcealign;
457
458#define ALIGNMENT 4
459
460#if defined(OOB_INTR_ONLY) && defined(HW_OOB)
461extern void bcmsdh_enable_hw_oob_intr(void *sdh, bool enable);
462#endif
463
464#if defined(OOB_INTR_ONLY) && defined(SDIO_ISR_THREAD)
465#error OOB_INTR_ONLY is NOT working with SDIO_ISR_THREAD
466#endif /* defined(OOB_INTR_ONLY) && defined(SDIO_ISR_THREAD) */
Arend van Spriel3c9d4c32011-03-02 21:18:48 +0100467#define PKTALIGN(_p, _len, _align) \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700468 do { \
469 uint datalign; \
Arend van Spriel54991ad2010-11-23 14:06:24 +0100470 datalign = (unsigned long)((_p)->data); \
471 datalign = roundup(datalign, (_align)) - datalign; \
472 ASSERT(datalign < (_align)); \
473 ASSERT((_p)->len >= ((_len) + datalign)); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700474 if (datalign) \
Arend van Spriel54991ad2010-11-23 14:06:24 +0100475 skb_pull((_p), datalign); \
476 __skb_trim((_p), (_len)); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700477 } while (0)
478
479/* Limit on rounding up frames */
480static const uint max_roundup = 512;
481
482/* Try doing readahead */
483static bool dhd_readahead;
484
485/* To check if there's window offered */
486#define DATAOK(bus) \
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700487 (((u8)(bus->tx_max - bus->tx_seq) != 0) && \
488 (((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700489
490/* Macros to get register read/write status */
491/* NOTE: these assume a local dhdsdio_bus_t *bus! */
492#define R_SDREG(regvar, regaddr, retryvar) \
493do { \
494 retryvar = 0; \
495 do { \
Arend van Sprielff31c542011-03-01 10:56:54 +0100496 regvar = R_REG(regaddr); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700497 } while (bcmsdh_regfail(bus->sdh) && (++retryvar <= retry_limit)); \
498 if (retryvar) { \
499 bus->regfails += (retryvar-1); \
500 if (retryvar > retry_limit) { \
501 DHD_ERROR(("%s: FAILED" #regvar "READ, LINE %d\n", \
502 __func__, __LINE__)); \
503 regvar = 0; \
504 } \
505 } \
506} while (0)
507
508#define W_SDREG(regval, regaddr, retryvar) \
509do { \
510 retryvar = 0; \
511 do { \
Arend van Sprielff31c542011-03-01 10:56:54 +0100512 W_REG(regaddr, regval); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700513 } while (bcmsdh_regfail(bus->sdh) && (++retryvar <= retry_limit)); \
514 if (retryvar) { \
515 bus->regfails += (retryvar-1); \
516 if (retryvar > retry_limit) \
517 DHD_ERROR(("%s: FAILED REGISTER WRITE, LINE %d\n", \
518 __func__, __LINE__)); \
519 } \
520} while (0)
521
522#define DHD_BUS SDIO_BUS
523
524#define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
525
526#define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
527
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700528#ifdef SDTEST
529static void dhdsdio_testrcv(dhd_bus_t *bus, void *pkt, uint seq);
530static void dhdsdio_sdtest_set(dhd_bus_t *bus, bool start);
531#endif
532
533#ifdef DHD_DEBUG
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700534static int dhdsdio_checkdied(dhd_bus_t *bus, u8 *data, uint size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700535static int dhdsdio_mem_dump(dhd_bus_t *bus);
536#endif /* DHD_DEBUG */
537static int dhdsdio_download_state(dhd_bus_t *bus, bool enter);
538
Arend van Spriel3c9d4c32011-03-02 21:18:48 +0100539static void dhdsdio_release(dhd_bus_t *bus);
Arend van Spriel8da4a3a2011-03-02 21:18:42 +0100540static void dhdsdio_release_malloc(dhd_bus_t *bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700541static void dhdsdio_disconnect(void *ptr);
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700542static bool dhdsdio_chipmatch(u16 chipid);
Arend van Spriel8da4a3a2011-03-02 21:18:42 +0100543static bool dhdsdio_probe_attach(dhd_bus_t *bus, void *sdh,
544 void *regsva, u16 devid);
545static bool dhdsdio_probe_malloc(dhd_bus_t *bus, void *sdh);
546static bool dhdsdio_probe_init(dhd_bus_t *bus, void *sdh);
547static void dhdsdio_release_dongle(dhd_bus_t *bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700548
549static uint process_nvram_vars(char *varbuf, uint len);
550
551static void dhd_dongle_setmemsize(struct dhd_bus *bus, int mem_size);
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700552static int dhd_bcmsdh_send_buf(dhd_bus_t *bus, u32 addr, uint fn,
Arend van Sprielc26b1372010-11-23 14:06:23 +0100553 uint flags, u8 *buf, uint nbytes,
554 struct sk_buff *pkt, bcmsdh_cmplt_fn_t complete,
555 void *handle);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700556
Arend van Spriel8da4a3a2011-03-02 21:18:42 +0100557static bool dhdsdio_download_firmware(struct dhd_bus *bus, void *sdh);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700558static int _dhdsdio_download_firmware(struct dhd_bus *bus);
559
560static int dhdsdio_download_code_file(struct dhd_bus *bus, char *image_path);
561static int dhdsdio_download_nvram(struct dhd_bus *bus);
562#ifdef BCMEMBEDIMAGE
563static int dhdsdio_download_code_array(struct dhd_bus *bus);
564#endif
Franky Lineb5dc512011-04-25 19:34:04 -0700565static void dhdsdio_chip_disablecore(bcmsdh_info_t *sdh, u32 corebase);
Franky Lincb63e4c2011-04-25 15:45:08 -0700566static int dhdsdio_chip_attach(struct dhd_bus *bus, void *regs);
Franky Lineb5dc512011-04-25 19:34:04 -0700567static void dhdsdio_chip_resetcore(bcmsdh_info_t *sdh, u32 corebase);
Franky Lin5d0d7a92011-04-25 19:34:05 -0700568static void dhdsdio_sdiod_drive_strength_init(struct dhd_bus *bus,
569 u32 drivestrength);
Franky Lincee3cf42011-04-25 19:34:06 -0700570static void dhdsdio_chip_detach(struct dhd_bus *bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700571
Grant Grundler4b455e02011-05-04 09:59:47 -0700572/* Packet free applicable unconditionally for sdio and sdspi.
573 * Conditional if bufpool was present for gspi bus.
574 */
575static void dhdsdio_pktfree2(dhd_bus_t *bus, struct sk_buff *pkt)
576{
577 dhd_os_sdlock_rxq(bus->dhd);
578 if ((bus->bus != SPI_BUS) || bus->usebufpool)
Henry Ptasinskib33f0e22011-05-10 22:25:29 +0200579 bcm_pkt_buf_free_skb(pkt);
Grant Grundler4b455e02011-05-04 09:59:47 -0700580 dhd_os_sdunlock_rxq(bus->dhd);
581}
582
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700583static void dhd_dongle_setmemsize(struct dhd_bus *bus, int mem_size)
584{
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -0700585 s32 min_size = DONGLE_MIN_MEMSIZE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700586 /* Restrict the memsize to user specified limit */
587 DHD_ERROR(("user: Restrict the dongle ram size to %d, min %d\n",
588 dhd_dongle_memsize, min_size));
589 if ((dhd_dongle_memsize > min_size) &&
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -0700590 (dhd_dongle_memsize < (s32) bus->orig_ramsize))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700591 bus->ramsize = dhd_dongle_memsize;
592}
593
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700594static int dhdsdio_set_siaddr_window(dhd_bus_t *bus, u32 address)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700595{
596 int err = 0;
597 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW,
598 (address >> 8) & SBSDIO_SBADDRLOW_MASK, &err);
599 if (!err)
600 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRMID,
601 (address >> 16) & SBSDIO_SBADDRMID_MASK, &err);
602 if (!err)
603 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRHIGH,
604 (address >> 24) & SBSDIO_SBADDRHIGH_MASK,
605 &err);
606 return err;
607}
608
609/* Turn backplane clock on or off */
610static int dhdsdio_htclk(dhd_bus_t *bus, bool on, bool pendok)
611{
612 int err;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700613 u8 clkctl, clkreq, devctl;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700614 bcmsdh_info_t *sdh;
615
616 DHD_TRACE(("%s: Enter\n", __func__));
617
618#if defined(OOB_INTR_ONLY)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700619 pendok = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700620#endif
621 clkctl = 0;
622 sdh = bus->sdh;
623
624 if (on) {
625 /* Request HT Avail */
626 clkreq =
627 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
628
Franky Linc05df632011-04-25 19:34:07 -0700629 if ((bus->ci->chip == BCM4329_CHIP_ID)
630 && (bus->ci->chiprev == 0))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700631 clkreq |= SBSDIO_FORCE_ALP;
632
633 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
634 clkreq, &err);
635 if (err) {
636 DHD_ERROR(("%s: HT Avail request error: %d\n",
637 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200638 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700639 }
640
Franky Linc05df632011-04-25 19:34:07 -0700641 if (pendok && ((bus->ci->buscoretype == PCMCIA_CORE_ID)
642 && (bus->ci->buscorerev == 9))) {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700643 u32 dummy, retries;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700644 R_SDREG(dummy, &bus->regs->clockctlstatus, retries);
645 }
646
647 /* Check current status */
648 clkctl =
649 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
650 &err);
651 if (err) {
652 DHD_ERROR(("%s: HT Avail read error: %d\n",
653 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200654 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700655 }
656
657 /* Go to pending and await interrupt if appropriate */
658 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
659 /* Allow only clock-available interrupt */
660 devctl =
661 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
662 &err);
663 if (err) {
664 DHD_ERROR(("%s: Devctl error setting CA: %d\n",
665 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200666 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700667 }
668
669 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
670 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
671 devctl, &err);
672 DHD_INFO(("CLKCTL: set PENDING\n"));
673 bus->clkstate = CLK_PENDING;
674
Roland Vossena1c5ad82011-04-11 15:16:24 +0200675 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700676 } else if (bus->clkstate == CLK_PENDING) {
677 /* Cancel CA-only interrupt filter */
678 devctl =
679 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
680 &err);
681 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
682 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
683 devctl, &err);
684 }
685
686 /* Otherwise, wait here (polling) for HT Avail */
687 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
688 SPINWAIT_SLEEP(sdioh_spinwait_sleep,
689 ((clkctl =
690 bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
691 SBSDIO_FUNC1_CHIPCLKCSR,
692 &err)),
693 !SBSDIO_CLKAV(clkctl, bus->alp_only)),
694 PMU_MAX_TRANSITION_DLY);
695 }
696 if (err) {
697 DHD_ERROR(("%s: HT Avail request error: %d\n",
698 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200699 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700700 }
701 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
702 DHD_ERROR(("%s: HT Avail timeout (%d): clkctl 0x%02x\n",
703 __func__, PMU_MAX_TRANSITION_DLY, clkctl));
Roland Vossenb74ac122011-05-03 11:35:20 +0200704 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700705 }
706
707 /* Mark clock available */
708 bus->clkstate = CLK_AVAIL;
709 DHD_INFO(("CLKCTL: turned ON\n"));
710
711#if defined(DHD_DEBUG)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700712 if (bus->alp_only == true) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700713#if !defined(BCMLXSDMMC)
714 if (!SBSDIO_ALPONLY(clkctl)) {
715 DHD_ERROR(("%s: HT Clock, when ALP Only\n",
716 __func__));
717 }
718#endif /* !defined(BCMLXSDMMC) */
719 } else {
720 if (SBSDIO_ALPONLY(clkctl)) {
721 DHD_ERROR(("%s: HT Clock should be on.\n",
722 __func__));
723 }
724 }
725#endif /* defined (DHD_DEBUG) */
726
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700727 bus->activity = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700728 } else {
729 clkreq = 0;
730
731 if (bus->clkstate == CLK_PENDING) {
732 /* Cancel CA-only interrupt filter */
733 devctl =
734 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
735 &err);
736 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
737 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
738 devctl, &err);
739 }
740
741 bus->clkstate = CLK_SDONLY;
742 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
743 clkreq, &err);
744 DHD_INFO(("CLKCTL: turned OFF\n"));
745 if (err) {
746 DHD_ERROR(("%s: Failed access turning clock off: %d\n",
747 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200748 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700749 }
750 }
Roland Vossena1c5ad82011-04-11 15:16:24 +0200751 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700752}
753
754/* Change idle/active SD state */
755static int dhdsdio_sdclk(dhd_bus_t *bus, bool on)
756{
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700757 DHD_TRACE(("%s: Enter\n", __func__));
758
Franky Lin602a8ab2011-06-01 13:45:04 +0200759 if (on)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700760 bus->clkstate = CLK_SDONLY;
Franky Lin602a8ab2011-06-01 13:45:04 +0200761 else
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700762 bus->clkstate = CLK_NONE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700763
Roland Vossena1c5ad82011-04-11 15:16:24 +0200764 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700765}
766
767/* Transition SD and backplane clock readiness */
768static int dhdsdio_clkctl(dhd_bus_t *bus, uint target, bool pendok)
769{
770#ifdef DHD_DEBUG
771 uint oldstate = bus->clkstate;
772#endif /* DHD_DEBUG */
773
774 DHD_TRACE(("%s: Enter\n", __func__));
775
776 /* Early exit if we're already there */
777 if (bus->clkstate == target) {
778 if (target == CLK_AVAIL) {
779 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700780 bus->activity = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700781 }
Roland Vossena1c5ad82011-04-11 15:16:24 +0200782 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700783 }
784
785 switch (target) {
786 case CLK_AVAIL:
787 /* Make sure SD clock is available */
788 if (bus->clkstate == CLK_NONE)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700789 dhdsdio_sdclk(bus, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700790 /* Now request HT Avail on the backplane */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700791 dhdsdio_htclk(bus, true, pendok);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700792 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700793 bus->activity = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700794 break;
795
796 case CLK_SDONLY:
797 /* Remove HT request, or bring up SD clock */
798 if (bus->clkstate == CLK_NONE)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700799 dhdsdio_sdclk(bus, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700800 else if (bus->clkstate == CLK_AVAIL)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700801 dhdsdio_htclk(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700802 else
803 DHD_ERROR(("dhdsdio_clkctl: request for %d -> %d\n",
804 bus->clkstate, target));
805 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
806 break;
807
808 case CLK_NONE:
809 /* Make sure to remove HT request */
810 if (bus->clkstate == CLK_AVAIL)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700811 dhdsdio_htclk(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700812 /* Now remove the SD clock */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700813 dhdsdio_sdclk(bus, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700814 dhd_os_wd_timer(bus->dhd, 0);
815 break;
816 }
817#ifdef DHD_DEBUG
818 DHD_INFO(("dhdsdio_clkctl: %d -> %d\n", oldstate, bus->clkstate));
819#endif /* DHD_DEBUG */
820
Roland Vossena1c5ad82011-04-11 15:16:24 +0200821 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700822}
823
824int dhdsdio_bussleep(dhd_bus_t *bus, bool sleep)
825{
826 bcmsdh_info_t *sdh = bus->sdh;
827 sdpcmd_regs_t *regs = bus->regs;
828 uint retries = 0;
829
830 DHD_INFO(("dhdsdio_bussleep: request %s (currently %s)\n",
831 (sleep ? "SLEEP" : "WAKE"),
832 (bus->sleeping ? "SLEEP" : "WAKE")));
833
834 /* Done if we're already in the requested state */
835 if (sleep == bus->sleeping)
Roland Vossena1c5ad82011-04-11 15:16:24 +0200836 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700837
838 /* Going to sleep: set the alarm and turn off the lights... */
839 if (sleep) {
840 /* Don't sleep if something is pending */
841 if (bus->dpc_sched || bus->rxskip || pktq_len(&bus->txq))
Roland Vossene10d82d2011-05-03 11:35:19 +0200842 return -EBUSY;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700843
844 /* Disable SDIO interrupts (no longer interested) */
845 bcmsdh_intr_disable(bus->sdh);
846
847 /* Make sure the controller has the bus up */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700848 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700849
850 /* Tell device to start using OOB wakeup */
851 W_SDREG(SMB_USE_OOB, &regs->tosbmailbox, retries);
852 if (retries > retry_limit)
853 DHD_ERROR(("CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"));
854
855 /* Turn off our contribution to the HT clock request */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700856 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700857
858 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
859 SBSDIO_FORCE_HW_CLKREQ_OFF, NULL);
860
861 /* Isolate the bus */
Franky Linc05df632011-04-25 19:34:07 -0700862 if (bus->ci->chip != BCM4329_CHIP_ID
863 && bus->ci->chip != BCM4319_CHIP_ID) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700864 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
865 SBSDIO_DEVCTL_PADS_ISO, NULL);
866 }
867
868 /* Change state */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700869 bus->sleeping = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700870
871 } else {
872 /* Waking up: bus power up is ok, set local state */
873
874 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
875 0, NULL);
876
877 /* Force pad isolation off if possible
878 (in case power never toggled) */
Franky Linc05df632011-04-25 19:34:07 -0700879 if ((bus->ci->buscoretype == PCMCIA_CORE_ID)
880 && (bus->ci->buscorerev >= 10))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700881 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, 0,
882 NULL);
883
884 /* Make sure the controller has the bus up */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700885 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700886
887 /* Send misc interrupt to indicate OOB not needed */
888 W_SDREG(0, &regs->tosbmailboxdata, retries);
889 if (retries <= retry_limit)
890 W_SDREG(SMB_DEV_INT, &regs->tosbmailbox, retries);
891
892 if (retries > retry_limit)
893 DHD_ERROR(("CANNOT SIGNAL CHIP TO CLEAR OOB!!\n"));
894
895 /* Make sure we have SD bus access */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700896 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700897
898 /* Change state */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700899 bus->sleeping = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700900
901 /* Enable interrupts again */
902 if (bus->intr && (bus->dhd->busstate == DHD_BUS_DATA)) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700903 bus->intdis = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700904 bcmsdh_intr_enable(bus->sdh);
905 }
906 }
907
Roland Vossena1c5ad82011-04-11 15:16:24 +0200908 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700909}
910
911#if defined(OOB_INTR_ONLY)
912void dhd_enable_oob_intr(struct dhd_bus *bus, bool enable)
913{
914#if defined(HW_OOB)
915 bcmsdh_enable_hw_oob_intr(bus->sdh, enable);
916#else
917 sdpcmd_regs_t *regs = bus->regs;
918 uint retries = 0;
919
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700920 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700921 if (enable == true) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700922
923 /* Tell device to start using OOB wakeup */
924 W_SDREG(SMB_USE_OOB, &regs->tosbmailbox, retries);
925 if (retries > retry_limit)
926 DHD_ERROR(("CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"));
927
928 } else {
929 /* Send misc interrupt to indicate OOB not needed */
930 W_SDREG(0, &regs->tosbmailboxdata, retries);
931 if (retries <= retry_limit)
932 W_SDREG(SMB_DEV_INT, &regs->tosbmailbox, retries);
933 }
934
935 /* Turn off our contribution to the HT clock request */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700936 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700937#endif /* !defined(HW_OOB) */
938}
939#endif /* defined(OOB_INTR_ONLY) */
940
941#define BUS_WAKE(bus) \
942 do { \
943 if ((bus)->sleeping) \
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700944 dhdsdio_bussleep((bus), false); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700945 } while (0);
946
947/* Writes a HW/SW header into the packet and sends it. */
948/* Assumes: (a) header space already there, (b) caller holds lock */
Arend van Sprielc26b1372010-11-23 14:06:23 +0100949static int dhdsdio_txpkt(dhd_bus_t *bus, struct sk_buff *pkt, uint chan,
950 bool free_pkt)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700951{
952 int ret;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700953 u8 *frame;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700954 u16 len, pad = 0;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700955 u32 swheader;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700956 uint retries = 0;
957 bcmsdh_info_t *sdh;
Arend van Sprielc26b1372010-11-23 14:06:23 +0100958 struct sk_buff *new;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700959 int i;
960
961 DHD_TRACE(("%s: Enter\n", __func__));
962
963 sdh = bus->sdh;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700964
965 if (bus->dhd->dongle_reset) {
Roland Vossenb74ac122011-05-03 11:35:20 +0200966 ret = -EPERM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700967 goto done;
968 }
969
Arend van Spriel54991ad2010-11-23 14:06:24 +0100970 frame = (u8 *) (pkt->data);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700971
972 /* Add alignment padding, allocate new packet if needed */
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -0700973 pad = ((unsigned long)frame % DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700974 if (pad) {
Arend van Spriel3be727c2010-11-23 22:20:30 +0100975 if (skb_headroom(pkt) < pad) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700976 DHD_INFO(("%s: insufficient headroom %d for %d pad\n",
Arend van Spriel3be727c2010-11-23 22:20:30 +0100977 __func__, skb_headroom(pkt), pad));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700978 bus->dhd->tx_realloc++;
Henry Ptasinskib33f0e22011-05-10 22:25:29 +0200979 new = bcm_pkt_buf_get_skb(pkt->len + DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700980 if (!new) {
981 DHD_ERROR(("%s: couldn't allocate new %d-byte "
982 "packet\n",
Arend van Spriel54991ad2010-11-23 14:06:24 +0100983 __func__, pkt->len + DHD_SDALIGN));
Roland Vossene10d82d2011-05-03 11:35:19 +0200984 ret = -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700985 goto done;
986 }
987
Arend van Spriel3c9d4c32011-03-02 21:18:48 +0100988 PKTALIGN(new, pkt->len, DHD_SDALIGN);
Stanislav Fomichev02160692011-02-15 01:05:10 +0300989 memcpy(new->data, pkt->data, pkt->len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700990 if (free_pkt)
Henry Ptasinskib33f0e22011-05-10 22:25:29 +0200991 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700992 /* free the pkt if canned one is not used */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700993 free_pkt = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700994 pkt = new;
Arend van Spriel54991ad2010-11-23 14:06:24 +0100995 frame = (u8 *) (pkt->data);
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -0700996 ASSERT(((unsigned long)frame % DHD_SDALIGN) == 0);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700997 pad = 0;
998 } else {
Arend van Sprielc303ecb2010-11-18 20:46:43 +0100999 skb_push(pkt, pad);
Arend van Spriel54991ad2010-11-23 14:06:24 +01001000 frame = (u8 *) (pkt->data);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001001
Arend van Spriel54991ad2010-11-23 14:06:24 +01001002 ASSERT((pad + SDPCM_HDRLEN) <= (int)(pkt->len));
Brett Rudley9249ede2010-11-30 20:09:49 -08001003 memset(frame, 0, pad + SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001004 }
1005 }
1006 ASSERT(pad < DHD_SDALIGN);
1007
1008 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
Arend van Spriel54991ad2010-11-23 14:06:24 +01001009 len = (u16) (pkt->len);
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03001010 *(u16 *) frame = cpu_to_le16(len);
1011 *(((u16 *) frame) + 1) = cpu_to_le16(~len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001012
1013 /* Software tag: channel, sequence number, data offset */
1014 swheader =
1015 ((chan << SDPCM_CHANNEL_SHIFT) & SDPCM_CHANNEL_MASK) | bus->tx_seq |
1016 (((pad +
1017 SDPCM_HDRLEN) << SDPCM_DOFFSET_SHIFT) & SDPCM_DOFFSET_MASK);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03001018
1019 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
1020 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001021
1022#ifdef DHD_DEBUG
Arend van Spriel54991ad2010-11-23 14:06:24 +01001023 tx_packets[pkt->priority]++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001024 if (DHD_BYTES_ON() &&
1025 (((DHD_CTL_ON() && (chan == SDPCM_CONTROL_CHANNEL)) ||
1026 (DHD_DATA_ON() && (chan != SDPCM_CONTROL_CHANNEL))))) {
Arend van Spriel34227312011-05-10 22:25:32 +02001027 printk(KERN_DEBUG "Tx Frame:\n");
1028 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, frame, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001029 } else if (DHD_HDRS_ON()) {
Arend van Spriel34227312011-05-10 22:25:32 +02001030 printk(KERN_DEBUG "TxHdr:\n");
1031 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1032 frame, min_t(u16, len, 16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001033 }
1034#endif
1035
1036 /* Raise len to next SDIO block to eliminate tail command */
1037 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07001038 u16 pad = bus->blocksize - (len % bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001039 if ((pad <= bus->roundup) && (pad < bus->blocksize))
1040#ifdef NOTUSED
Arend van Spriel3be727c2010-11-23 22:20:30 +01001041 if (pad <= skb_tailroom(pkt))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001042#endif /* NOTUSED */
1043 len += pad;
1044 } else if (len % DHD_SDALIGN) {
1045 len += DHD_SDALIGN - (len % DHD_SDALIGN);
1046 }
1047
1048 /* Some controllers have trouble with odd bytes -- round to even */
1049 if (forcealign && (len & (ALIGNMENT - 1))) {
1050#ifdef NOTUSED
Arend van Spriel3be727c2010-11-23 22:20:30 +01001051 if (skb_tailroom(pkt))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001052#endif
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07001053 len = roundup(len, ALIGNMENT);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001054#ifdef NOTUSED
1055 else
1056 DHD_ERROR(("%s: sending unrounded %d-byte packet\n",
1057 __func__, len));
1058#endif
1059 }
1060
1061 do {
1062 ret =
1063 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
1064 F2SYNC, frame, len, pkt, NULL, NULL);
1065 bus->f2txdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02001066 ASSERT(ret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001067
1068 if (ret < 0) {
1069 /* On failure, abort the command
1070 and terminate the frame */
1071 DHD_INFO(("%s: sdio error %d, abort command and "
1072 "terminate frame.\n", __func__, ret));
1073 bus->tx_sderrs++;
1074
1075 bcmsdh_abort(sdh, SDIO_FUNC_2);
1076 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
1077 SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
1078 NULL);
1079 bus->f1regdata++;
1080
1081 for (i = 0; i < 3; i++) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001082 u8 hi, lo;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001083 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1084 SBSDIO_FUNC1_WFRAMEBCHI,
1085 NULL);
1086 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1087 SBSDIO_FUNC1_WFRAMEBCLO,
1088 NULL);
1089 bus->f1regdata += 2;
1090 if ((hi == 0) && (lo == 0))
1091 break;
1092 }
1093
1094 }
1095 if (ret == 0)
1096 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1097
1098 } while ((ret < 0) && retrydata && retries++ < TXRETRIES);
1099
1100done:
1101 /* restore pkt buffer pointer before calling tx complete routine */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001102 skb_pull(pkt, SDPCM_HDRLEN + pad);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001103 dhd_os_sdunlock(bus->dhd);
1104 dhd_txcomplete(bus->dhd, pkt, ret != 0);
1105 dhd_os_sdlock(bus->dhd);
1106
1107 if (free_pkt)
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02001108 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001109
1110 return ret;
1111}
1112
Arend van Sprielc26b1372010-11-23 14:06:23 +01001113int dhd_bus_txdata(struct dhd_bus *bus, struct sk_buff *pkt)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001114{
Roland Vossenb74ac122011-05-03 11:35:20 +02001115 int ret = -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001116 uint datalen, prec;
1117
1118 DHD_TRACE(("%s: Enter\n", __func__));
1119
Arend van Spriel54991ad2010-11-23 14:06:24 +01001120 datalen = pkt->len;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001121
1122#ifdef SDTEST
1123 /* Push the test header if doing loopback */
1124 if (bus->ext_loop) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001125 u8 *data;
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001126 skb_push(pkt, SDPCM_TEST_HDRLEN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01001127 data = pkt->data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001128 *data++ = SDPCM_TEST_ECHOREQ;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001129 *data++ = (u8) bus->loopid++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001130 *data++ = (datalen >> 0);
1131 *data++ = (datalen >> 8);
1132 datalen += SDPCM_TEST_HDRLEN;
1133 }
1134#endif /* SDTEST */
1135
1136 /* Add space for the header */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001137 skb_push(pkt, SDPCM_HDRLEN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01001138 ASSERT(IS_ALIGNED((unsigned long)(pkt->data), 2));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001139
Arend van Spriel54991ad2010-11-23 14:06:24 +01001140 prec = PRIO2PREC((pkt->priority & PRIOMASK));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001141
1142 /* Check for existing queue, current flow-control,
1143 pending event, or pending clock */
1144 if (dhd_deferred_tx || bus->fcstate || pktq_len(&bus->txq)
1145 || bus->dpc_sched || (!DATAOK(bus))
1146 || (bus->flowcontrol & NBITVAL(prec))
1147 || (bus->clkstate != CLK_AVAIL)) {
1148 DHD_TRACE(("%s: deferring pktq len %d\n", __func__,
1149 pktq_len(&bus->txq)));
1150 bus->fcqueued++;
1151
1152 /* Priority based enq */
1153 dhd_os_sdlock_txq(bus->dhd);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001154 if (dhd_prec_enq(bus->dhd, &bus->txq, pkt, prec) == false) {
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001155 skb_pull(pkt, SDPCM_HDRLEN);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001156 dhd_txcomplete(bus->dhd, pkt, false);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02001157 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001158 DHD_ERROR(("%s: out of bus->txq !!!\n", __func__));
Roland Vossene10d82d2011-05-03 11:35:19 +02001159 ret = -ENOSR;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001160 } else {
Roland Vossena1c5ad82011-04-11 15:16:24 +02001161 ret = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001162 }
1163 dhd_os_sdunlock_txq(bus->dhd);
1164
Grant Grundler7c316072011-03-09 15:04:15 -08001165 if (pktq_len(&bus->txq) >= TXHI)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001166 dhd_txflowcontrol(bus->dhd, 0, ON);
1167
1168#ifdef DHD_DEBUG
1169 if (pktq_plen(&bus->txq, prec) > qcount[prec])
1170 qcount[prec] = pktq_plen(&bus->txq, prec);
1171#endif
1172 /* Schedule DPC if needed to send queued packet(s) */
1173 if (dhd_deferred_tx && !bus->dpc_sched) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001174 bus->dpc_sched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001175 dhd_sched_dpc(bus->dhd);
1176 }
1177 } else {
1178 /* Lock: we're about to use shared data/code (and SDIO) */
1179 dhd_os_sdlock(bus->dhd);
1180
1181 /* Otherwise, send it now */
1182 BUS_WAKE(bus);
1183 /* Make sure back plane ht clk is on, no pending allowed */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001184 dhdsdio_clkctl(bus, CLK_AVAIL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001185
1186#ifndef SDTEST
1187 DHD_TRACE(("%s: calling txpkt\n", __func__));
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001188 ret = dhdsdio_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001189#else
1190 ret = dhdsdio_txpkt(bus, pkt,
1191 (bus->ext_loop ? SDPCM_TEST_CHANNEL :
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001192 SDPCM_DATA_CHANNEL), true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001193#endif
1194 if (ret)
1195 bus->dhd->tx_errors++;
1196 else
1197 bus->dhd->dstats.tx_bytes += datalen;
1198
1199 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001200 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001201 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001202 }
1203
1204 dhd_os_sdunlock(bus->dhd);
1205 }
1206
1207 return ret;
1208}
1209
1210static uint dhdsdio_sendfromq(dhd_bus_t *bus, uint maxframes)
1211{
Arend van Sprielc26b1372010-11-23 14:06:23 +01001212 struct sk_buff *pkt;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001213 u32 intstatus = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001214 uint retries = 0;
1215 int ret = 0, prec_out;
1216 uint cnt = 0;
1217 uint datalen;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001218 u8 tx_prec_map;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001219
1220 dhd_pub_t *dhd = bus->dhd;
1221 sdpcmd_regs_t *regs = bus->regs;
1222
1223 DHD_TRACE(("%s: Enter\n", __func__));
1224
1225 tx_prec_map = ~bus->flowcontrol;
1226
1227 /* Send frames until the limit or some other event */
1228 for (cnt = 0; (cnt < maxframes) && DATAOK(bus); cnt++) {
1229 dhd_os_sdlock_txq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02001230 pkt = bcm_pktq_mdeq(&bus->txq, tx_prec_map, &prec_out);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001231 if (pkt == NULL) {
1232 dhd_os_sdunlock_txq(bus->dhd);
1233 break;
1234 }
1235 dhd_os_sdunlock_txq(bus->dhd);
Arend van Spriel54991ad2010-11-23 14:06:24 +01001236 datalen = pkt->len - SDPCM_HDRLEN;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001237
1238#ifndef SDTEST
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001239 ret = dhdsdio_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001240#else
1241 ret = dhdsdio_txpkt(bus, pkt,
1242 (bus->ext_loop ? SDPCM_TEST_CHANNEL :
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001243 SDPCM_DATA_CHANNEL), true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001244#endif
1245 if (ret)
1246 bus->dhd->tx_errors++;
1247 else
1248 bus->dhd->dstats.tx_bytes += datalen;
1249
1250 /* In poll mode, need to check for other events */
1251 if (!bus->intr && cnt) {
1252 /* Check device status, signal pending interrupt */
1253 R_SDREG(intstatus, &regs->intstatus, retries);
1254 bus->f2txdata++;
1255 if (bcmsdh_regfail(bus->sdh))
1256 break;
1257 if (intstatus & bus->hostintmask)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001258 bus->ipend = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001259 }
1260 }
1261
1262 /* Deflow-control stack if needed */
Grant Grundler7c316072011-03-09 15:04:15 -08001263 if (dhd->up && (dhd->busstate == DHD_BUS_DATA) &&
Grant Grundler26a71a42011-03-09 10:41:25 -08001264 dhd->txoff && (pktq_len(&bus->txq) < TXLOW))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001265 dhd_txflowcontrol(dhd, 0, OFF);
1266
1267 return cnt;
1268}
1269
Greg Kroah-Hartman580a0bd2010-10-05 11:09:48 -07001270int dhd_bus_txctl(struct dhd_bus *bus, unsigned char *msg, uint msglen)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001271{
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001272 u8 *frame;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07001273 u16 len;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001274 u32 swheader;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001275 uint retries = 0;
1276 bcmsdh_info_t *sdh = bus->sdh;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001277 u8 doff = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001278 int ret = -1;
1279 int i;
1280
1281 DHD_TRACE(("%s: Enter\n", __func__));
1282
1283 if (bus->dhd->dongle_reset)
1284 return -EIO;
1285
1286 /* Back the pointer to make a room for bus header */
1287 frame = msg - SDPCM_HDRLEN;
1288 len = (msglen += SDPCM_HDRLEN);
1289
1290 /* Add alignment padding (optional for ctl frames) */
1291 if (dhd_alignctl) {
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07001292 doff = ((unsigned long)frame % DHD_SDALIGN);
Jason Cooper9b890322010-09-30 15:15:39 -04001293 if (doff) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001294 frame -= doff;
1295 len += doff;
1296 msglen += doff;
Brett Rudley9249ede2010-11-30 20:09:49 -08001297 memset(frame, 0, doff + SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001298 }
1299 ASSERT(doff < DHD_SDALIGN);
1300 }
1301 doff += SDPCM_HDRLEN;
1302
1303 /* Round send length to next SDIO block */
1304 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07001305 u16 pad = bus->blocksize - (len % bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001306 if ((pad <= bus->roundup) && (pad < bus->blocksize))
1307 len += pad;
1308 } else if (len % DHD_SDALIGN) {
1309 len += DHD_SDALIGN - (len % DHD_SDALIGN);
1310 }
1311
1312 /* Satisfy length-alignment requirements */
1313 if (forcealign && (len & (ALIGNMENT - 1)))
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07001314 len = roundup(len, ALIGNMENT);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001315
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07001316 ASSERT(IS_ALIGNED((unsigned long)frame, 2));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001317
1318 /* Need to lock here to protect txseq and SDIO tx calls */
1319 dhd_os_sdlock(bus->dhd);
1320
1321 BUS_WAKE(bus);
1322
1323 /* Make sure backplane clock is on */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001324 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001325
1326 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03001327 *(u16 *) frame = cpu_to_le16((u16) msglen);
1328 *(((u16 *) frame) + 1) = cpu_to_le16(~msglen);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001329
1330 /* Software tag: channel, sequence number, data offset */
1331 swheader =
1332 ((SDPCM_CONTROL_CHANNEL << SDPCM_CHANNEL_SHIFT) &
1333 SDPCM_CHANNEL_MASK)
1334 | bus->tx_seq | ((doff << SDPCM_DOFFSET_SHIFT) &
1335 SDPCM_DOFFSET_MASK);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03001336 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
1337 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001338
1339 if (!DATAOK(bus)) {
1340 DHD_INFO(("%s: No bus credit bus->tx_max %d, bus->tx_seq %d\n",
1341 __func__, bus->tx_max, bus->tx_seq));
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001342 bus->ctrl_frame_stat = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001343 /* Send from dpc */
1344 bus->ctrl_frame_buf = frame;
1345 bus->ctrl_frame_len = len;
1346
1347 dhd_wait_for_event(bus->dhd, &bus->ctrl_frame_stat);
1348
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001349 if (bus->ctrl_frame_stat == false) {
1350 DHD_INFO(("%s: ctrl_frame_stat == false\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001351 ret = 0;
1352 } else {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001353 DHD_INFO(("%s: ctrl_frame_stat == true\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001354 ret = -1;
1355 }
1356 }
1357
1358 if (ret == -1) {
1359#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02001360 if (DHD_BYTES_ON() && DHD_CTL_ON()) {
1361 printk(KERN_DEBUG "Tx Frame:\n");
1362 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1363 frame, len);
1364 } else if (DHD_HDRS_ON()) {
1365 printk(KERN_DEBUG "TxHdr:\n");
1366 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1367 frame, min_t(u16, len, 16));
1368 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001369#endif
1370
1371 do {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001372 bus->ctrl_frame_stat = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001373 ret =
1374 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh),
1375 SDIO_FUNC_2, F2SYNC, frame, len,
1376 NULL, NULL, NULL);
1377
Roland Vossenb7ef2a92011-05-03 11:35:02 +02001378 ASSERT(ret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001379
1380 if (ret < 0) {
1381 /* On failure, abort the command and
1382 terminate the frame */
1383 DHD_INFO(("%s: sdio error %d, abort command and terminate frame.\n",
1384 __func__, ret));
1385 bus->tx_sderrs++;
1386
1387 bcmsdh_abort(sdh, SDIO_FUNC_2);
1388
1389 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
1390 SBSDIO_FUNC1_FRAMECTRL,
1391 SFC_WF_TERM, NULL);
1392 bus->f1regdata++;
1393
1394 for (i = 0; i < 3; i++) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001395 u8 hi, lo;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001396 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1397 SBSDIO_FUNC1_WFRAMEBCHI,
1398 NULL);
1399 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1400 SBSDIO_FUNC1_WFRAMEBCLO,
1401 NULL);
1402 bus->f1regdata += 2;
1403 if ((hi == 0) && (lo == 0))
1404 break;
1405 }
1406
1407 }
1408 if (ret == 0) {
1409 bus->tx_seq =
1410 (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1411 }
1412 } while ((ret < 0) && retries++ < TXRETRIES);
1413 }
1414
1415 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001416 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001417 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001418 }
1419
1420 dhd_os_sdunlock(bus->dhd);
1421
1422 if (ret)
1423 bus->dhd->tx_ctlerrs++;
1424 else
1425 bus->dhd->tx_ctlpkts++;
1426
1427 return ret ? -EIO : 0;
1428}
1429
Greg Kroah-Hartman580a0bd2010-10-05 11:09:48 -07001430int dhd_bus_rxctl(struct dhd_bus *bus, unsigned char *msg, uint msglen)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001431{
1432 int timeleft;
1433 uint rxlen = 0;
1434 bool pending;
1435
1436 DHD_TRACE(("%s: Enter\n", __func__));
1437
1438 if (bus->dhd->dongle_reset)
1439 return -EIO;
1440
1441 /* Wait until control frame is available */
1442 timeleft = dhd_os_ioctl_resp_wait(bus->dhd, &bus->rxlen, &pending);
1443
1444 dhd_os_sdlock(bus->dhd);
1445 rxlen = bus->rxlen;
Stanislav Fomichev02160692011-02-15 01:05:10 +03001446 memcpy(msg, bus->rxctl, min(msglen, rxlen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001447 bus->rxlen = 0;
1448 dhd_os_sdunlock(bus->dhd);
1449
1450 if (rxlen) {
1451 DHD_CTL(("%s: resumed on rxctl frame, got %d expected %d\n",
1452 __func__, rxlen, msglen));
1453 } else if (timeleft == 0) {
1454 DHD_ERROR(("%s: resumed on timeout\n", __func__));
1455#ifdef DHD_DEBUG
1456 dhd_os_sdlock(bus->dhd);
1457 dhdsdio_checkdied(bus, NULL, 0);
1458 dhd_os_sdunlock(bus->dhd);
1459#endif /* DHD_DEBUG */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001460 } else if (pending == true) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001461 DHD_CTL(("%s: cancelled\n", __func__));
1462 return -ERESTARTSYS;
1463 } else {
1464 DHD_CTL(("%s: resumed for unknown reason?\n", __func__));
1465#ifdef DHD_DEBUG
1466 dhd_os_sdlock(bus->dhd);
1467 dhdsdio_checkdied(bus, NULL, 0);
1468 dhd_os_sdunlock(bus->dhd);
1469#endif /* DHD_DEBUG */
1470 }
1471
1472 if (rxlen)
1473 bus->dhd->rx_ctlpkts++;
1474 else
1475 bus->dhd->rx_ctlerrs++;
1476
Jason Coopere9887c92010-10-06 10:08:02 -04001477 return rxlen ? (int)rxlen : -ETIMEDOUT;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001478}
1479
1480/* IOVar table */
1481enum {
1482 IOV_INTR = 1,
1483 IOV_POLLRATE,
1484 IOV_SDREG,
1485 IOV_SBREG,
1486 IOV_SDCIS,
1487 IOV_MEMBYTES,
1488 IOV_MEMSIZE,
1489#ifdef DHD_DEBUG
1490 IOV_CHECKDIED,
1491#endif
1492 IOV_DOWNLOAD,
1493 IOV_FORCEEVEN,
1494 IOV_SDIOD_DRIVE,
1495 IOV_READAHEAD,
1496 IOV_SDRXCHAIN,
1497 IOV_ALIGNCTL,
1498 IOV_SDALIGN,
1499 IOV_DEVRESET,
1500 IOV_CPU,
1501#ifdef SDTEST
1502 IOV_PKTGEN,
1503 IOV_EXTLOOP,
1504#endif /* SDTEST */
1505 IOV_SPROM,
1506 IOV_TXBOUND,
1507 IOV_RXBOUND,
1508 IOV_TXMINMAX,
1509 IOV_IDLETIME,
1510 IOV_IDLECLOCK,
1511 IOV_SD1IDLE,
1512 IOV_SLEEP,
1513 IOV_VARS
1514};
1515
1516const bcm_iovar_t dhdsdio_iovars[] = {
1517 {"intr", IOV_INTR, 0, IOVT_BOOL, 0},
1518 {"sleep", IOV_SLEEP, 0, IOVT_BOOL, 0},
1519 {"pollrate", IOV_POLLRATE, 0, IOVT_UINT32, 0},
1520 {"idletime", IOV_IDLETIME, 0, IOVT_INT32, 0},
1521 {"idleclock", IOV_IDLECLOCK, 0, IOVT_INT32, 0},
1522 {"sd1idle", IOV_SD1IDLE, 0, IOVT_BOOL, 0},
1523 {"membytes", IOV_MEMBYTES, 0, IOVT_BUFFER, 2 * sizeof(int)},
1524 {"memsize", IOV_MEMSIZE, 0, IOVT_UINT32, 0},
1525 {"download", IOV_DOWNLOAD, 0, IOVT_BOOL, 0},
1526 {"vars", IOV_VARS, 0, IOVT_BUFFER, 0},
1527 {"sdiod_drive", IOV_SDIOD_DRIVE, 0, IOVT_UINT32, 0},
1528 {"readahead", IOV_READAHEAD, 0, IOVT_BOOL, 0},
1529 {"sdrxchain", IOV_SDRXCHAIN, 0, IOVT_BOOL, 0},
1530 {"alignctl", IOV_ALIGNCTL, 0, IOVT_BOOL, 0},
1531 {"sdalign", IOV_SDALIGN, 0, IOVT_BOOL, 0},
1532 {"devreset", IOV_DEVRESET, 0, IOVT_BOOL, 0},
1533#ifdef DHD_DEBUG
1534 {"sdreg", IOV_SDREG, 0, IOVT_BUFFER, sizeof(sdreg_t)}
1535 ,
1536 {"sbreg", IOV_SBREG, 0, IOVT_BUFFER, sizeof(sdreg_t)}
1537 ,
1538 {"sd_cis", IOV_SDCIS, 0, IOVT_BUFFER, DHD_IOCTL_MAXLEN}
1539 ,
1540 {"forcealign", IOV_FORCEEVEN, 0, IOVT_BOOL, 0}
1541 ,
1542 {"txbound", IOV_TXBOUND, 0, IOVT_UINT32, 0}
1543 ,
1544 {"rxbound", IOV_RXBOUND, 0, IOVT_UINT32, 0}
1545 ,
1546 {"txminmax", IOV_TXMINMAX, 0, IOVT_UINT32, 0}
1547 ,
1548 {"cpu", IOV_CPU, 0, IOVT_BOOL, 0}
1549 ,
1550#ifdef DHD_DEBUG
1551 {"checkdied", IOV_CHECKDIED, 0, IOVT_BUFFER, 0}
1552 ,
1553#endif /* DHD_DEBUG */
1554#endif /* DHD_DEBUG */
1555#ifdef SDTEST
1556 {"extloop", IOV_EXTLOOP, 0, IOVT_BOOL, 0}
1557 ,
1558 {"pktgen", IOV_PKTGEN, 0, IOVT_BUFFER, sizeof(dhd_pktgen_t)}
1559 ,
1560#endif /* SDTEST */
1561
1562 {NULL, 0, 0, 0, 0}
1563};
1564
1565static void
1566dhd_dump_pct(struct bcmstrbuf *strbuf, char *desc, uint num, uint div)
1567{
1568 uint q1, q2;
1569
1570 if (!div) {
1571 bcm_bprintf(strbuf, "%s N/A", desc);
1572 } else {
1573 q1 = num / div;
1574 q2 = (100 * (num - (q1 * div))) / div;
1575 bcm_bprintf(strbuf, "%s %d.%02d", desc, q1, q2);
1576 }
1577}
1578
1579void dhd_bus_dump(dhd_pub_t *dhdp, struct bcmstrbuf *strbuf)
1580{
1581 dhd_bus_t *bus = dhdp->bus;
1582
1583 bcm_bprintf(strbuf, "Bus SDIO structure:\n");
1584 bcm_bprintf(strbuf,
1585 "hostintmask 0x%08x intstatus 0x%08x sdpcm_ver %d\n",
1586 bus->hostintmask, bus->intstatus, bus->sdpcm_ver);
1587 bcm_bprintf(strbuf,
1588 "fcstate %d qlen %d tx_seq %d, max %d, rxskip %d rxlen %d rx_seq %d\n",
1589 bus->fcstate, pktq_len(&bus->txq), bus->tx_seq, bus->tx_max,
1590 bus->rxskip, bus->rxlen, bus->rx_seq);
1591 bcm_bprintf(strbuf, "intr %d intrcount %d lastintrs %d spurious %d\n",
1592 bus->intr, bus->intrcount, bus->lastintrs, bus->spurious);
1593 bcm_bprintf(strbuf, "pollrate %d pollcnt %d regfails %d\n",
1594 bus->pollrate, bus->pollcnt, bus->regfails);
1595
1596 bcm_bprintf(strbuf, "\nAdditional counters:\n");
1597 bcm_bprintf(strbuf,
1598 "tx_sderrs %d fcqueued %d rxrtx %d rx_toolong %d rxc_errors %d\n",
1599 bus->tx_sderrs, bus->fcqueued, bus->rxrtx, bus->rx_toolong,
1600 bus->rxc_errors);
1601 bcm_bprintf(strbuf, "rx_hdrfail %d badhdr %d badseq %d\n",
1602 bus->rx_hdrfail, bus->rx_badhdr, bus->rx_badseq);
1603 bcm_bprintf(strbuf, "fc_rcvd %d, fc_xoff %d, fc_xon %d\n", bus->fc_rcvd,
1604 bus->fc_xoff, bus->fc_xon);
1605 bcm_bprintf(strbuf, "rxglomfail %d, rxglomframes %d, rxglompkts %d\n",
1606 bus->rxglomfail, bus->rxglomframes, bus->rxglompkts);
1607 bcm_bprintf(strbuf, "f2rx (hdrs/data) %d (%d/%d), f2tx %d f1regs %d\n",
1608 (bus->f2rxhdrs + bus->f2rxdata), bus->f2rxhdrs,
1609 bus->f2rxdata, bus->f2txdata, bus->f1regdata);
1610 {
1611 dhd_dump_pct(strbuf, "\nRx: pkts/f2rd", bus->dhd->rx_packets,
1612 (bus->f2rxhdrs + bus->f2rxdata));
1613 dhd_dump_pct(strbuf, ", pkts/f1sd", bus->dhd->rx_packets,
1614 bus->f1regdata);
1615 dhd_dump_pct(strbuf, ", pkts/sd", bus->dhd->rx_packets,
1616 (bus->f2rxhdrs + bus->f2rxdata + bus->f1regdata));
1617 dhd_dump_pct(strbuf, ", pkts/int", bus->dhd->rx_packets,
1618 bus->intrcount);
1619 bcm_bprintf(strbuf, "\n");
1620
1621 dhd_dump_pct(strbuf, "Rx: glom pct", (100 * bus->rxglompkts),
1622 bus->dhd->rx_packets);
1623 dhd_dump_pct(strbuf, ", pkts/glom", bus->rxglompkts,
1624 bus->rxglomframes);
1625 bcm_bprintf(strbuf, "\n");
1626
1627 dhd_dump_pct(strbuf, "Tx: pkts/f2wr", bus->dhd->tx_packets,
1628 bus->f2txdata);
1629 dhd_dump_pct(strbuf, ", pkts/f1sd", bus->dhd->tx_packets,
1630 bus->f1regdata);
1631 dhd_dump_pct(strbuf, ", pkts/sd", bus->dhd->tx_packets,
1632 (bus->f2txdata + bus->f1regdata));
1633 dhd_dump_pct(strbuf, ", pkts/int", bus->dhd->tx_packets,
1634 bus->intrcount);
1635 bcm_bprintf(strbuf, "\n");
1636
1637 dhd_dump_pct(strbuf, "Total: pkts/f2rw",
1638 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1639 (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata));
1640 dhd_dump_pct(strbuf, ", pkts/f1sd",
1641 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1642 bus->f1regdata);
1643 dhd_dump_pct(strbuf, ", pkts/sd",
1644 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1645 (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata +
1646 bus->f1regdata));
1647 dhd_dump_pct(strbuf, ", pkts/int",
1648 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1649 bus->intrcount);
1650 bcm_bprintf(strbuf, "\n\n");
1651 }
1652
1653#ifdef SDTEST
1654 if (bus->pktgen_count) {
1655 bcm_bprintf(strbuf, "pktgen config and count:\n");
1656 bcm_bprintf(strbuf,
1657 "freq %d count %d print %d total %d min %d len %d\n",
1658 bus->pktgen_freq, bus->pktgen_count,
1659 bus->pktgen_print, bus->pktgen_total,
1660 bus->pktgen_minlen, bus->pktgen_maxlen);
1661 bcm_bprintf(strbuf, "send attempts %d rcvd %d fail %d\n",
1662 bus->pktgen_sent, bus->pktgen_rcvd,
1663 bus->pktgen_fail);
1664 }
1665#endif /* SDTEST */
1666#ifdef DHD_DEBUG
1667 bcm_bprintf(strbuf, "dpc_sched %d host interrupt%spending\n",
1668 bus->dpc_sched,
1669 (bcmsdh_intr_pending(bus->sdh) ? " " : " not "));
1670 bcm_bprintf(strbuf, "blocksize %d roundup %d\n", bus->blocksize,
1671 bus->roundup);
1672#endif /* DHD_DEBUG */
1673 bcm_bprintf(strbuf,
1674 "clkstate %d activity %d idletime %d idlecount %d sleeping %d\n",
1675 bus->clkstate, bus->activity, bus->idletime, bus->idlecount,
1676 bus->sleeping);
1677}
1678
1679void dhd_bus_clearcounts(dhd_pub_t *dhdp)
1680{
1681 dhd_bus_t *bus = (dhd_bus_t *) dhdp->bus;
1682
1683 bus->intrcount = bus->lastintrs = bus->spurious = bus->regfails = 0;
1684 bus->rxrtx = bus->rx_toolong = bus->rxc_errors = 0;
1685 bus->rx_hdrfail = bus->rx_badhdr = bus->rx_badseq = 0;
1686 bus->tx_sderrs = bus->fc_rcvd = bus->fc_xoff = bus->fc_xon = 0;
1687 bus->rxglomfail = bus->rxglomframes = bus->rxglompkts = 0;
1688 bus->f2rxhdrs = bus->f2rxdata = bus->f2txdata = bus->f1regdata = 0;
1689}
1690
1691#ifdef SDTEST
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001692static int dhdsdio_pktgen_get(dhd_bus_t *bus, u8 *arg)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001693{
1694 dhd_pktgen_t pktgen;
1695
1696 pktgen.version = DHD_PKTGEN_VERSION;
1697 pktgen.freq = bus->pktgen_freq;
1698 pktgen.count = bus->pktgen_count;
1699 pktgen.print = bus->pktgen_print;
1700 pktgen.total = bus->pktgen_total;
1701 pktgen.minlen = bus->pktgen_minlen;
1702 pktgen.maxlen = bus->pktgen_maxlen;
1703 pktgen.numsent = bus->pktgen_sent;
1704 pktgen.numrcvd = bus->pktgen_rcvd;
1705 pktgen.numfail = bus->pktgen_fail;
1706 pktgen.mode = bus->pktgen_mode;
1707 pktgen.stop = bus->pktgen_stop;
1708
Stanislav Fomichev02160692011-02-15 01:05:10 +03001709 memcpy(arg, &pktgen, sizeof(pktgen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001710
1711 return 0;
1712}
1713
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001714static int dhdsdio_pktgen_set(dhd_bus_t *bus, u8 *arg)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001715{
1716 dhd_pktgen_t pktgen;
1717 uint oldcnt, oldmode;
1718
Stanislav Fomichev02160692011-02-15 01:05:10 +03001719 memcpy(&pktgen, arg, sizeof(pktgen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001720 if (pktgen.version != DHD_PKTGEN_VERSION)
Roland Vossene10d82d2011-05-03 11:35:19 +02001721 return -EINVAL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001722
1723 oldcnt = bus->pktgen_count;
1724 oldmode = bus->pktgen_mode;
1725
1726 bus->pktgen_freq = pktgen.freq;
1727 bus->pktgen_count = pktgen.count;
1728 bus->pktgen_print = pktgen.print;
1729 bus->pktgen_total = pktgen.total;
1730 bus->pktgen_minlen = pktgen.minlen;
1731 bus->pktgen_maxlen = pktgen.maxlen;
1732 bus->pktgen_mode = pktgen.mode;
1733 bus->pktgen_stop = pktgen.stop;
1734
1735 bus->pktgen_tick = bus->pktgen_ptick = 0;
Greg Kroah-Hartman3ea2f4d2010-10-08 11:39:43 -07001736 bus->pktgen_len = max(bus->pktgen_len, bus->pktgen_minlen);
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07001737 bus->pktgen_len = min(bus->pktgen_len, bus->pktgen_maxlen);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001738
1739 /* Clear counts for a new pktgen (mode change, or was stopped) */
1740 if (bus->pktgen_count && (!oldcnt || oldmode != bus->pktgen_mode))
1741 bus->pktgen_sent = bus->pktgen_rcvd = bus->pktgen_fail = 0;
1742
1743 return 0;
1744}
1745#endif /* SDTEST */
1746
1747static int
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001748dhdsdio_membytes(dhd_bus_t *bus, bool write, u32 address, u8 *data,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001749 uint size)
1750{
1751 int bcmerror = 0;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001752 u32 sdaddr;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001753 uint dsize;
1754
1755 /* Determine initial transfer parameters */
1756 sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK;
1757 if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK)
1758 dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr);
1759 else
1760 dsize = size;
1761
1762 /* Set the backplane window to include the start address */
1763 bcmerror = dhdsdio_set_siaddr_window(bus, address);
1764 if (bcmerror) {
1765 DHD_ERROR(("%s: window change failed\n", __func__));
1766 goto xfer_done;
1767 }
1768
1769 /* Do the transfer(s) */
1770 while (size) {
1771 DHD_INFO(("%s: %s %d bytes at offset 0x%08x in window 0x%08x\n",
1772 __func__, (write ? "write" : "read"), dsize,
1773 sdaddr, (address & SBSDIO_SBWINDOW_MASK)));
1774 bcmerror =
1775 bcmsdh_rwdata(bus->sdh, write, sdaddr, data, dsize);
1776 if (bcmerror) {
1777 DHD_ERROR(("%s: membytes transfer failed\n", __func__));
1778 break;
1779 }
1780
1781 /* Adjust for next transfer (if any) */
1782 size -= dsize;
1783 if (size) {
1784 data += dsize;
1785 address += dsize;
1786 bcmerror = dhdsdio_set_siaddr_window(bus, address);
1787 if (bcmerror) {
1788 DHD_ERROR(("%s: window change failed\n",
1789 __func__));
1790 break;
1791 }
1792 sdaddr = 0;
Greg Kroah-Hartmanb61640d2010-10-08 12:37:39 -07001793 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001794 }
1795 }
1796
1797xfer_done:
1798 /* Return the window to backplane enumeration space for core access */
1799 if (dhdsdio_set_siaddr_window(bus, bcmsdh_cur_sbwad(bus->sdh))) {
1800 DHD_ERROR(("%s: FAILED to set window back to 0x%x\n",
1801 __func__, bcmsdh_cur_sbwad(bus->sdh)));
1802 }
1803
1804 return bcmerror;
1805}
1806
1807#ifdef DHD_DEBUG
1808static int dhdsdio_readshared(dhd_bus_t *bus, sdpcm_shared_t *sh)
1809{
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001810 u32 addr;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001811 int rv;
1812
1813 /* Read last word in memory to determine address of
1814 sdpcm_shared structure */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001815 rv = dhdsdio_membytes(bus, false, bus->ramsize - 4, (u8 *)&addr, 4);
Jason Cooper9b890322010-09-30 15:15:39 -04001816 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001817 return rv;
1818
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03001819 addr = le32_to_cpu(addr);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001820
1821 DHD_INFO(("sdpcm_shared address 0x%08X\n", addr));
1822
1823 /*
1824 * Check if addr is valid.
1825 * NVRAM length at the end of memory should have been overwritten.
1826 */
1827 if (addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff)) {
1828 DHD_ERROR(("%s: address (0x%08x) of sdpcm_shared invalid\n",
1829 __func__, addr));
Roland Vossenb74ac122011-05-03 11:35:20 +02001830 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001831 }
1832
Roland Vossen70963f92011-06-01 13:45:08 +02001833 /* Read rte_shared structure */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001834 rv = dhdsdio_membytes(bus, false, addr, (u8 *) sh,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001835 sizeof(sdpcm_shared_t));
1836 if (rv < 0)
1837 return rv;
1838
1839 /* Endianness */
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03001840 sh->flags = le32_to_cpu(sh->flags);
1841 sh->trap_addr = le32_to_cpu(sh->trap_addr);
1842 sh->assert_exp_addr = le32_to_cpu(sh->assert_exp_addr);
1843 sh->assert_file_addr = le32_to_cpu(sh->assert_file_addr);
1844 sh->assert_line = le32_to_cpu(sh->assert_line);
1845 sh->console_addr = le32_to_cpu(sh->console_addr);
1846 sh->msgtrace_addr = le32_to_cpu(sh->msgtrace_addr);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001847
1848 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) != SDPCM_SHARED_VERSION) {
1849 DHD_ERROR(("%s: sdpcm_shared version %d in dhd "
1850 "is different than sdpcm_shared version %d in dongle\n",
1851 __func__, SDPCM_SHARED_VERSION,
1852 sh->flags & SDPCM_SHARED_VERSION_MASK));
Roland Vossenb74ac122011-05-03 11:35:20 +02001853 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001854 }
1855
Roland Vossena1c5ad82011-04-11 15:16:24 +02001856 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001857}
1858
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001859static int dhdsdio_checkdied(dhd_bus_t *bus, u8 *data, uint size)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001860{
1861 int bcmerror = 0;
1862 uint msize = 512;
1863 char *mbuffer = NULL;
1864 uint maxstrlen = 256;
1865 char *str = NULL;
1866 trap_t tr;
1867 sdpcm_shared_t sdpcm_shared;
1868 struct bcmstrbuf strbuf;
1869
1870 DHD_TRACE(("%s: Enter\n", __func__));
1871
1872 if (data == NULL) {
1873 /*
1874 * Called after a rx ctrl timeout. "data" is NULL.
1875 * allocate memory to trace the trap or assert.
1876 */
1877 size = msize;
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02001878 mbuffer = data = kmalloc(msize, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001879 if (mbuffer == NULL) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02001880 DHD_ERROR(("%s: kmalloc(%d) failed\n", __func__,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001881 msize));
Roland Vossene10d82d2011-05-03 11:35:19 +02001882 bcmerror = -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001883 goto done;
1884 }
1885 }
1886
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02001887 str = kmalloc(maxstrlen, GFP_ATOMIC);
Jason Cooper9b890322010-09-30 15:15:39 -04001888 if (str == NULL) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02001889 DHD_ERROR(("%s: kmalloc(%d) failed\n", __func__, maxstrlen));
Roland Vossene10d82d2011-05-03 11:35:19 +02001890 bcmerror = -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001891 goto done;
1892 }
1893
Jason Cooper9b890322010-09-30 15:15:39 -04001894 bcmerror = dhdsdio_readshared(bus, &sdpcm_shared);
1895 if (bcmerror < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001896 goto done;
1897
1898 bcm_binit(&strbuf, data, size);
1899
1900 bcm_bprintf(&strbuf,
1901 "msgtrace address : 0x%08X\nconsole address : 0x%08X\n",
1902 sdpcm_shared.msgtrace_addr, sdpcm_shared.console_addr);
1903
1904 if ((sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
1905 /* NOTE: Misspelled assert is intentional - DO NOT FIX.
1906 * (Avoids conflict with real asserts for programmatic
1907 * parsing of output.)
1908 */
1909 bcm_bprintf(&strbuf, "Assrt not built in dongle\n");
1910 }
1911
1912 if ((sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP)) ==
1913 0) {
1914 /* NOTE: Misspelled assert is intentional - DO NOT FIX.
1915 * (Avoids conflict with real asserts for programmatic
1916 * parsing of output.)
1917 */
1918 bcm_bprintf(&strbuf, "No trap%s in dongle",
1919 (sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT)
1920 ? "/assrt" : "");
1921 } else {
1922 if (sdpcm_shared.flags & SDPCM_SHARED_ASSERT) {
1923 /* Download assert */
1924 bcm_bprintf(&strbuf, "Dongle assert");
1925 if (sdpcm_shared.assert_exp_addr != 0) {
1926 str[0] = '\0';
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001927 bcmerror = dhdsdio_membytes(bus, false,
Jason Cooper9b890322010-09-30 15:15:39 -04001928 sdpcm_shared.assert_exp_addr,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001929 (u8 *) str, maxstrlen);
Jason Cooper9b890322010-09-30 15:15:39 -04001930 if (bcmerror < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001931 goto done;
1932
1933 str[maxstrlen - 1] = '\0';
1934 bcm_bprintf(&strbuf, " expr \"%s\"", str);
1935 }
1936
1937 if (sdpcm_shared.assert_file_addr != 0) {
1938 str[0] = '\0';
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001939 bcmerror = dhdsdio_membytes(bus, false,
Jason Cooper9b890322010-09-30 15:15:39 -04001940 sdpcm_shared.assert_file_addr,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001941 (u8 *) str, maxstrlen);
Jason Cooper9b890322010-09-30 15:15:39 -04001942 if (bcmerror < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001943 goto done;
1944
1945 str[maxstrlen - 1] = '\0';
1946 bcm_bprintf(&strbuf, " file \"%s\"", str);
1947 }
1948
1949 bcm_bprintf(&strbuf, " line %d ",
1950 sdpcm_shared.assert_line);
1951 }
1952
1953 if (sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001954 bcmerror = dhdsdio_membytes(bus, false,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001955 sdpcm_shared.trap_addr, (u8 *)&tr,
Jason Cooper9b890322010-09-30 15:15:39 -04001956 sizeof(trap_t));
1957 if (bcmerror < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001958 goto done;
1959
1960 bcm_bprintf(&strbuf,
1961 "Dongle trap type 0x%x @ epc 0x%x, cpsr 0x%x, spsr 0x%x, sp 0x%x,"
1962 "lp 0x%x, rpc 0x%x Trap offset 0x%x, "
1963 "r0 0x%x, r1 0x%x, r2 0x%x, r3 0x%x, r4 0x%x, r5 0x%x, r6 0x%x, r7 0x%x\n",
1964 tr.type, tr.epc, tr.cpsr, tr.spsr, tr.r13,
1965 tr.r14, tr.pc, sdpcm_shared.trap_addr,
1966 tr.r0, tr.r1, tr.r2, tr.r3, tr.r4, tr.r5,
1967 tr.r6, tr.r7);
1968 }
1969 }
1970
1971 if (sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP))
1972 DHD_ERROR(("%s: %s\n", __func__, strbuf.origbuf));
1973
1974#ifdef DHD_DEBUG
1975 if (sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
1976 /* Mem dump to a file on device */
1977 dhdsdio_mem_dump(bus);
1978 }
1979#endif /* DHD_DEBUG */
1980
1981done:
Ilia Mirkin46d994b2011-03-13 00:28:56 -05001982 kfree(mbuffer);
1983 kfree(str);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001984
1985 return bcmerror;
1986}
1987
1988static int dhdsdio_mem_dump(dhd_bus_t *bus)
1989{
1990 int ret = 0;
1991 int size; /* Full mem size */
1992 int start = 0; /* Start address */
1993 int read_size = 0; /* Read size of each iteration */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001994 u8 *buf = NULL, *databuf = NULL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001995
1996 /* Get full mem size */
1997 size = bus->ramsize;
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02001998 buf = kmalloc(size, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001999 if (!buf) {
Arend van Spriel0bef7742011-02-10 12:03:44 +01002000 DHD_ERROR(("%s: Out of memory (%d bytes)\n", __func__, size));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002001 return -1;
2002 }
2003
2004 /* Read mem content */
Arend van Spriel0bef7742011-02-10 12:03:44 +01002005 printk(KERN_DEBUG "Dump dongle memory");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002006 databuf = buf;
2007 while (size) {
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07002008 read_size = min(MEMBLOCK, size);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002009 ret = dhdsdio_membytes(bus, false, start, databuf, read_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002010 if (ret) {
Arend van Spriel0bef7742011-02-10 12:03:44 +01002011 DHD_ERROR(("%s: Error membytes %d\n", __func__, ret));
Ilia Mirkin46d994b2011-03-13 00:28:56 -05002012 kfree(buf);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002013 return -1;
2014 }
Arend van Spriel0bef7742011-02-10 12:03:44 +01002015 printk(".");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002016
2017 /* Decrement size and increment start address */
2018 size -= read_size;
2019 start += read_size;
2020 databuf += read_size;
2021 }
Arend van Spriel0bef7742011-02-10 12:03:44 +01002022 printk(KERN_DEBUG "Done\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002023
2024 /* free buf before return !!! */
2025 if (write_to_file(bus->dhd, buf, bus->ramsize)) {
Arend van Spriel0bef7742011-02-10 12:03:44 +01002026 DHD_ERROR(("%s: Error writing to files\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002027 return -1;
2028 }
2029
2030 /* buf free handled in write_to_file, not here */
2031 return 0;
2032}
2033
2034#define CONSOLE_LINE_MAX 192
2035
2036static int dhdsdio_readconsole(dhd_bus_t *bus)
2037{
2038 dhd_console_t *c = &bus->console;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002039 u8 line[CONSOLE_LINE_MAX], ch;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002040 u32 n, idx, addr;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002041 int rv;
2042
2043 /* Don't do anything until FWREADY updates console address */
2044 if (bus->console_addr == 0)
2045 return 0;
2046
2047 /* Read console log struct */
Roland Vossen70963f92011-06-01 13:45:08 +02002048 addr = bus->console_addr + offsetof(rte_cons_t, log);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002049 rv = dhdsdio_membytes(bus, false, addr, (u8 *)&c->log,
Jason Cooper9b890322010-09-30 15:15:39 -04002050 sizeof(c->log));
2051 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002052 return rv;
2053
2054 /* Allocate console buffer (one time only) */
2055 if (c->buf == NULL) {
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002056 c->bufsize = le32_to_cpu(c->log.buf_size);
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002057 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
Jason Cooper9b890322010-09-30 15:15:39 -04002058 if (c->buf == NULL)
Roland Vossene10d82d2011-05-03 11:35:19 +02002059 return -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002060 }
2061
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002062 idx = le32_to_cpu(c->log.idx);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002063
2064 /* Protect against corrupt value */
2065 if (idx > c->bufsize)
Roland Vossenb74ac122011-05-03 11:35:20 +02002066 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002067
2068 /* Skip reading the console buffer if the index pointer
2069 has not moved */
2070 if (idx == c->last)
Roland Vossena1c5ad82011-04-11 15:16:24 +02002071 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002072
2073 /* Read the console buffer */
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002074 addr = le32_to_cpu(c->log.buf);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002075 rv = dhdsdio_membytes(bus, false, addr, c->buf, c->bufsize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002076 if (rv < 0)
2077 return rv;
2078
2079 while (c->last != idx) {
2080 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2081 if (c->last == idx) {
2082 /* This would output a partial line.
2083 * Instead, back up
2084 * the buffer pointer and output this
2085 * line next time around.
2086 */
2087 if (c->last >= n)
2088 c->last -= n;
2089 else
2090 c->last = c->bufsize - n;
2091 goto break2;
2092 }
2093 ch = c->buf[c->last];
2094 c->last = (c->last + 1) % c->bufsize;
2095 if (ch == '\n')
2096 break;
2097 line[n] = ch;
2098 }
2099
2100 if (n > 0) {
2101 if (line[n - 1] == '\r')
2102 n--;
2103 line[n] = 0;
Arend van Spriel0bef7742011-02-10 12:03:44 +01002104 printk(KERN_DEBUG "CONSOLE: %s\n", line);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002105 }
2106 }
2107break2:
2108
Roland Vossena1c5ad82011-04-11 15:16:24 +02002109 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002110}
2111#endif /* DHD_DEBUG */
2112
2113int dhdsdio_downloadvars(dhd_bus_t *bus, void *arg, int len)
2114{
Roland Vossena1c5ad82011-04-11 15:16:24 +02002115 int bcmerror = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002116
2117 DHD_TRACE(("%s: Enter\n", __func__));
2118
2119 /* Basic sanity checks */
2120 if (bus->dhd->up) {
Roland Vossenb74ac122011-05-03 11:35:20 +02002121 bcmerror = -EISCONN;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002122 goto err;
2123 }
2124 if (!len) {
Roland Vossene10d82d2011-05-03 11:35:19 +02002125 bcmerror = -EOVERFLOW;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002126 goto err;
2127 }
2128
2129 /* Free the old ones and replace with passed variables */
Ilia Mirkin46d994b2011-03-13 00:28:56 -05002130 kfree(bus->vars);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002131
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002132 bus->vars = kmalloc(len, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002133 bus->varsz = bus->vars ? len : 0;
2134 if (bus->vars == NULL) {
Roland Vossene10d82d2011-05-03 11:35:19 +02002135 bcmerror = -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002136 goto err;
2137 }
2138
2139 /* Copy the passed variables, which should include the
2140 terminating double-null */
Stanislav Fomichev02160692011-02-15 01:05:10 +03002141 memcpy(bus->vars, arg, bus->varsz);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002142err:
2143 return bcmerror;
2144}
2145
2146static int
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002147dhdsdio_doiovar(dhd_bus_t *bus, const bcm_iovar_t *vi, u32 actionid,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002148 const char *name, void *params, int plen, void *arg, int len,
2149 int val_size)
2150{
2151 int bcmerror = 0;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002152 s32 int_val = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002153 bool bool_val = 0;
2154
2155 DHD_TRACE(("%s: Enter, action %d name %s params %p plen %d arg %p "
2156 "len %d val_size %d\n",
2157 __func__, actionid, name, params, plen, arg, len, val_size));
2158
2159 bcmerror = bcm_iovar_lencheck(vi, arg, len, IOV_ISSET(actionid));
2160 if (bcmerror != 0)
2161 goto exit;
2162
2163 if (plen >= (int)sizeof(int_val))
Stanislav Fomichev02160692011-02-15 01:05:10 +03002164 memcpy(&int_val, params, sizeof(int_val));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002165
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002166 bool_val = (int_val != 0) ? true : false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002167
2168 /* Some ioctls use the bus */
2169 dhd_os_sdlock(bus->dhd);
2170
2171 /* Check if dongle is in reset. If so, only allow DEVRESET iovars */
2172 if (bus->dhd->dongle_reset && !(actionid == IOV_SVAL(IOV_DEVRESET) ||
2173 actionid == IOV_GVAL(IOV_DEVRESET))) {
Roland Vossenb74ac122011-05-03 11:35:20 +02002174 bcmerror = -EPERM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002175 goto exit;
2176 }
2177
2178 /* Handle sleep stuff before any clock mucking */
2179 if (vi->varid == IOV_SLEEP) {
2180 if (IOV_ISSET(actionid)) {
2181 bcmerror = dhdsdio_bussleep(bus, bool_val);
2182 } else {
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002183 int_val = (s32) bus->sleeping;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002184 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002185 }
2186 goto exit;
2187 }
2188
2189 /* Request clock to allow SDIO accesses */
2190 if (!bus->dhd->dongle_reset) {
2191 BUS_WAKE(bus);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002192 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002193 }
2194
2195 switch (actionid) {
2196 case IOV_GVAL(IOV_INTR):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002197 int_val = (s32) bus->intr;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002198 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002199 break;
2200
2201 case IOV_SVAL(IOV_INTR):
2202 bus->intr = bool_val;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002203 bus->intdis = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002204 if (bus->dhd->up) {
2205 if (bus->intr) {
2206 DHD_INTR(("%s: enable SDIO device interrupts\n",
2207 __func__));
2208 bcmsdh_intr_enable(bus->sdh);
2209 } else {
2210 DHD_INTR(("%s: disable SDIO interrupts\n",
2211 __func__));
2212 bcmsdh_intr_disable(bus->sdh);
2213 }
2214 }
2215 break;
2216
2217 case IOV_GVAL(IOV_POLLRATE):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002218 int_val = (s32) bus->pollrate;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002219 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002220 break;
2221
2222 case IOV_SVAL(IOV_POLLRATE):
2223 bus->pollrate = (uint) int_val;
2224 bus->poll = (bus->pollrate != 0);
2225 break;
2226
2227 case IOV_GVAL(IOV_IDLETIME):
2228 int_val = bus->idletime;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002229 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002230 break;
2231
2232 case IOV_SVAL(IOV_IDLETIME):
2233 if ((int_val < 0) && (int_val != DHD_IDLE_IMMEDIATE))
Roland Vossene10d82d2011-05-03 11:35:19 +02002234 bcmerror = -EINVAL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002235 else
2236 bus->idletime = int_val;
2237 break;
2238
2239 case IOV_GVAL(IOV_IDLECLOCK):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002240 int_val = (s32) bus->idleclock;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002241 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002242 break;
2243
2244 case IOV_SVAL(IOV_IDLECLOCK):
2245 bus->idleclock = int_val;
2246 break;
2247
2248 case IOV_GVAL(IOV_SD1IDLE):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002249 int_val = (s32) sd1idle;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002250 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002251 break;
2252
2253 case IOV_SVAL(IOV_SD1IDLE):
2254 sd1idle = bool_val;
2255 break;
2256
2257 case IOV_SVAL(IOV_MEMBYTES):
2258 case IOV_GVAL(IOV_MEMBYTES):
2259 {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002260 u32 address;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002261 uint size, dsize;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002262 u8 *data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002263
2264 bool set = (actionid == IOV_SVAL(IOV_MEMBYTES));
2265
2266 ASSERT(plen >= 2 * sizeof(int));
2267
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002268 address = (u32) int_val;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002269 memcpy(&int_val, (char *)params + sizeof(int_val),
2270 sizeof(int_val));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002271 size = (uint) int_val;
2272
2273 /* Do some validation */
2274 dsize = set ? plen - (2 * sizeof(int)) : len;
2275 if (dsize < size) {
2276 DHD_ERROR(("%s: error on %s membytes, addr "
2277 "0x%08x size %d dsize %d\n",
2278 __func__, (set ? "set" : "get"),
2279 address, size, dsize));
Roland Vossene10d82d2011-05-03 11:35:19 +02002280 bcmerror = -EINVAL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002281 break;
2282 }
2283
2284 DHD_INFO(("%s: Request to %s %d bytes at address "
2285 "0x%08x\n",
2286 __func__, (set ? "write" : "read"), size, address));
2287
2288 /* If we know about SOCRAM, check for a fit */
2289 if ((bus->orig_ramsize) &&
2290 ((address > bus->orig_ramsize)
2291 || (address + size > bus->orig_ramsize))) {
2292 DHD_ERROR(("%s: ramsize 0x%08x doesn't have %d "
2293 "bytes at 0x%08x\n",
2294 __func__, bus->orig_ramsize, size, address));
Roland Vossene10d82d2011-05-03 11:35:19 +02002295 bcmerror = -EINVAL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002296 break;
2297 }
2298
2299 /* Generate the actual data pointer */
2300 data =
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002301 set ? (u8 *) params +
2302 2 * sizeof(int) : (u8 *) arg;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002303
2304 /* Call to do the transfer */
2305 bcmerror =
2306 dhdsdio_membytes(bus, set, address, data, size);
2307
2308 break;
2309 }
2310
2311 case IOV_GVAL(IOV_MEMSIZE):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002312 int_val = (s32) bus->ramsize;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002313 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002314 break;
2315
2316 case IOV_GVAL(IOV_SDIOD_DRIVE):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002317 int_val = (s32) dhd_sdiod_drive_strength;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002318 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002319 break;
2320
2321 case IOV_SVAL(IOV_SDIOD_DRIVE):
2322 dhd_sdiod_drive_strength = int_val;
Franky Lin5d0d7a92011-04-25 19:34:05 -07002323 dhdsdio_sdiod_drive_strength_init(bus,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002324 dhd_sdiod_drive_strength);
2325 break;
2326
2327 case IOV_SVAL(IOV_DOWNLOAD):
2328 bcmerror = dhdsdio_download_state(bus, bool_val);
2329 break;
2330
2331 case IOV_SVAL(IOV_VARS):
2332 bcmerror = dhdsdio_downloadvars(bus, arg, len);
2333 break;
2334
2335 case IOV_GVAL(IOV_READAHEAD):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002336 int_val = (s32) dhd_readahead;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002337 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002338 break;
2339
2340 case IOV_SVAL(IOV_READAHEAD):
2341 if (bool_val && !dhd_readahead)
2342 bus->nextlen = 0;
2343 dhd_readahead = bool_val;
2344 break;
2345
2346 case IOV_GVAL(IOV_SDRXCHAIN):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002347 int_val = (s32) bus->use_rxchain;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002348 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002349 break;
2350
2351 case IOV_SVAL(IOV_SDRXCHAIN):
2352 if (bool_val && !bus->sd_rxchain)
Roland Vossene10d82d2011-05-03 11:35:19 +02002353 bcmerror = -ENOTSUPP;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002354 else
2355 bus->use_rxchain = bool_val;
2356 break;
2357 case IOV_GVAL(IOV_ALIGNCTL):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002358 int_val = (s32) dhd_alignctl;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002359 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002360 break;
2361
2362 case IOV_SVAL(IOV_ALIGNCTL):
2363 dhd_alignctl = bool_val;
2364 break;
2365
2366 case IOV_GVAL(IOV_SDALIGN):
2367 int_val = DHD_SDALIGN;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002368 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002369 break;
2370
2371#ifdef DHD_DEBUG
2372 case IOV_GVAL(IOV_VARS):
2373 if (bus->varsz < (uint) len)
Stanislav Fomichev02160692011-02-15 01:05:10 +03002374 memcpy(arg, bus->vars, bus->varsz);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002375 else
Roland Vossene10d82d2011-05-03 11:35:19 +02002376 bcmerror = -EOVERFLOW;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002377 break;
2378#endif /* DHD_DEBUG */
2379
2380#ifdef DHD_DEBUG
2381 case IOV_GVAL(IOV_SDREG):
2382 {
2383 sdreg_t *sd_ptr;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002384 u32 addr, size;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002385
2386 sd_ptr = (sdreg_t *) params;
2387
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07002388 addr = (unsigned long)bus->regs + sd_ptr->offset;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002389 size = sd_ptr->func;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002390 int_val = (s32) bcmsdh_reg_read(bus->sdh, addr, size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002391 if (bcmsdh_regfail(bus->sdh))
Roland Vossenb74ac122011-05-03 11:35:20 +02002392 bcmerror = -EIO;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002393 memcpy(arg, &int_val, sizeof(s32));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002394 break;
2395 }
2396
2397 case IOV_SVAL(IOV_SDREG):
2398 {
2399 sdreg_t *sd_ptr;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002400 u32 addr, size;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002401
2402 sd_ptr = (sdreg_t *) params;
2403
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07002404 addr = (unsigned long)bus->regs + sd_ptr->offset;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002405 size = sd_ptr->func;
2406 bcmsdh_reg_write(bus->sdh, addr, size, sd_ptr->value);
2407 if (bcmsdh_regfail(bus->sdh))
Roland Vossenb74ac122011-05-03 11:35:20 +02002408 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002409 break;
2410 }
2411
2412 /* Same as above, but offset is not backplane
2413 (not SDIO core) */
2414 case IOV_GVAL(IOV_SBREG):
2415 {
2416 sdreg_t sdreg;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002417 u32 addr, size;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002418
Stanislav Fomichev02160692011-02-15 01:05:10 +03002419 memcpy(&sdreg, params, sizeof(sdreg));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002420
2421 addr = SI_ENUM_BASE + sdreg.offset;
2422 size = sdreg.func;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002423 int_val = (s32) bcmsdh_reg_read(bus->sdh, addr, size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002424 if (bcmsdh_regfail(bus->sdh))
Roland Vossenb74ac122011-05-03 11:35:20 +02002425 bcmerror = -EIO;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002426 memcpy(arg, &int_val, sizeof(s32));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002427 break;
2428 }
2429
2430 case IOV_SVAL(IOV_SBREG):
2431 {
2432 sdreg_t sdreg;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002433 u32 addr, size;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002434
Stanislav Fomichev02160692011-02-15 01:05:10 +03002435 memcpy(&sdreg, params, sizeof(sdreg));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002436
2437 addr = SI_ENUM_BASE + sdreg.offset;
2438 size = sdreg.func;
2439 bcmsdh_reg_write(bus->sdh, addr, size, sdreg.value);
2440 if (bcmsdh_regfail(bus->sdh))
Roland Vossenb74ac122011-05-03 11:35:20 +02002441 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002442 break;
2443 }
2444
2445 case IOV_GVAL(IOV_SDCIS):
2446 {
2447 *(char *)arg = 0;
2448
nohee koea3b8a22010-10-09 10:34:38 -07002449 strcat(arg, "\nFunc 0\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002450 bcmsdh_cis_read(bus->sdh, 0x10,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002451 (u8 *) arg + strlen(arg),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002452 SBSDIO_CIS_SIZE_LIMIT);
nohee koea3b8a22010-10-09 10:34:38 -07002453 strcat(arg, "\nFunc 1\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002454 bcmsdh_cis_read(bus->sdh, 0x11,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002455 (u8 *) arg + strlen(arg),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002456 SBSDIO_CIS_SIZE_LIMIT);
nohee koea3b8a22010-10-09 10:34:38 -07002457 strcat(arg, "\nFunc 2\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002458 bcmsdh_cis_read(bus->sdh, 0x12,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002459 (u8 *) arg + strlen(arg),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002460 SBSDIO_CIS_SIZE_LIMIT);
2461 break;
2462 }
2463
2464 case IOV_GVAL(IOV_FORCEEVEN):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002465 int_val = (s32) forcealign;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002466 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002467 break;
2468
2469 case IOV_SVAL(IOV_FORCEEVEN):
2470 forcealign = bool_val;
2471 break;
2472
2473 case IOV_GVAL(IOV_TXBOUND):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002474 int_val = (s32) dhd_txbound;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002475 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002476 break;
2477
2478 case IOV_SVAL(IOV_TXBOUND):
2479 dhd_txbound = (uint) int_val;
2480 break;
2481
2482 case IOV_GVAL(IOV_RXBOUND):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002483 int_val = (s32) dhd_rxbound;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002484 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002485 break;
2486
2487 case IOV_SVAL(IOV_RXBOUND):
2488 dhd_rxbound = (uint) int_val;
2489 break;
2490
2491 case IOV_GVAL(IOV_TXMINMAX):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002492 int_val = (s32) dhd_txminmax;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002493 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002494 break;
2495
2496 case IOV_SVAL(IOV_TXMINMAX):
2497 dhd_txminmax = (uint) int_val;
2498 break;
2499#endif /* DHD_DEBUG */
2500
2501#ifdef SDTEST
2502 case IOV_GVAL(IOV_EXTLOOP):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002503 int_val = (s32) bus->ext_loop;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002504 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002505 break;
2506
2507 case IOV_SVAL(IOV_EXTLOOP):
2508 bus->ext_loop = bool_val;
2509 break;
2510
2511 case IOV_GVAL(IOV_PKTGEN):
2512 bcmerror = dhdsdio_pktgen_get(bus, arg);
2513 break;
2514
2515 case IOV_SVAL(IOV_PKTGEN):
2516 bcmerror = dhdsdio_pktgen_set(bus, arg);
2517 break;
2518#endif /* SDTEST */
2519
2520 case IOV_SVAL(IOV_DEVRESET):
2521 DHD_TRACE(("%s: Called set IOV_DEVRESET=%d dongle_reset=%d "
2522 "busstate=%d\n",
2523 __func__, bool_val, bus->dhd->dongle_reset,
2524 bus->dhd->busstate));
2525
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002526 dhd_bus_devreset(bus->dhd, (u8) bool_val);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002527
2528 break;
2529
2530 case IOV_GVAL(IOV_DEVRESET):
2531 DHD_TRACE(("%s: Called get IOV_DEVRESET\n", __func__));
2532
2533 /* Get its status */
2534 int_val = (bool) bus->dhd->dongle_reset;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002535 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002536
2537 break;
2538
2539 default:
Roland Vossene10d82d2011-05-03 11:35:19 +02002540 bcmerror = -ENOTSUPP;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002541 break;
2542 }
2543
2544exit:
2545 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002546 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002547 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002548 }
2549
2550 dhd_os_sdunlock(bus->dhd);
2551
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002552 if (actionid == IOV_SVAL(IOV_DEVRESET) && bool_val == false)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002553 dhd_preinit_ioctls((dhd_pub_t *) bus->dhd);
2554
2555 return bcmerror;
2556}
2557
2558static int dhdsdio_write_vars(dhd_bus_t *bus)
2559{
2560 int bcmerror = 0;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002561 u32 varsize;
2562 u32 varaddr;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002563 u8 *vbuffer;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002564 u32 varsizew;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002565#ifdef DHD_DEBUG
2566 char *nvram_ularray;
2567#endif /* DHD_DEBUG */
2568
2569 /* Even if there are no vars are to be written, we still
2570 need to set the ramsize. */
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07002571 varsize = bus->varsz ? roundup(bus->varsz, 4) : 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002572 varaddr = (bus->ramsize - 4) - varsize;
2573
2574 if (bus->vars) {
Alexander Beregalov12d0eb42011-03-09 03:53:35 +03002575 vbuffer = kzalloc(varsize, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002576 if (!vbuffer)
Roland Vossene10d82d2011-05-03 11:35:19 +02002577 return -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002578
Stanislav Fomichev02160692011-02-15 01:05:10 +03002579 memcpy(vbuffer, bus->vars, bus->varsz);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002580
2581 /* Write the vars list */
2582 bcmerror =
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002583 dhdsdio_membytes(bus, true, varaddr, vbuffer, varsize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002584#ifdef DHD_DEBUG
2585 /* Verify NVRAM bytes */
2586 DHD_INFO(("Compare NVRAM dl & ul; varsize=%d\n", varsize));
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002587 nvram_ularray = kmalloc(varsize, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002588 if (!nvram_ularray)
Roland Vossene10d82d2011-05-03 11:35:19 +02002589 return -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002590
2591 /* Upload image to verify downloaded contents. */
2592 memset(nvram_ularray, 0xaa, varsize);
2593
2594 /* Read the vars list to temp buffer for comparison */
2595 bcmerror =
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002596 dhdsdio_membytes(bus, false, varaddr, nvram_ularray,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002597 varsize);
2598 if (bcmerror) {
2599 DHD_ERROR(("%s: error %d on reading %d nvram bytes at "
2600 "0x%08x\n", __func__, bcmerror, varsize, varaddr));
2601 }
2602 /* Compare the org NVRAM with the one read from RAM */
2603 if (memcmp(vbuffer, nvram_ularray, varsize)) {
2604 DHD_ERROR(("%s: Downloaded NVRAM image is corrupted.\n",
2605 __func__));
2606 } else
2607 DHD_ERROR(("%s: Download/Upload/Compare of NVRAM ok.\n",
2608 __func__));
2609
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02002610 kfree(nvram_ularray);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002611#endif /* DHD_DEBUG */
2612
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02002613 kfree(vbuffer);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002614 }
2615
2616 /* adjust to the user specified RAM */
2617 DHD_INFO(("Physical memory size: %d, usable memory size: %d\n",
2618 bus->orig_ramsize, bus->ramsize));
2619 DHD_INFO(("Vars are at %d, orig varsize is %d\n", varaddr, varsize));
2620 varsize = ((bus->orig_ramsize - 4) - varaddr);
2621
2622 /*
2623 * Determine the length token:
2624 * Varsize, converted to words, in lower 16-bits, checksum
2625 * in upper 16-bits.
2626 */
2627 if (bcmerror) {
2628 varsizew = 0;
2629 } else {
2630 varsizew = varsize / 4;
2631 varsizew = (~varsizew << 16) | (varsizew & 0x0000FFFF);
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002632 varsizew = cpu_to_le32(varsizew);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002633 }
2634
2635 DHD_INFO(("New varsize is %d, length token=0x%08x\n", varsize,
2636 varsizew));
2637
2638 /* Write the length token to the last word */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002639 bcmerror = dhdsdio_membytes(bus, true, (bus->orig_ramsize - 4),
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002640 (u8 *)&varsizew, 4);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002641
2642 return bcmerror;
2643}
2644
2645static int dhdsdio_download_state(dhd_bus_t *bus, bool enter)
2646{
2647 uint retries;
Franky Lineb5dc512011-04-25 19:34:04 -07002648 u32 regdata;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002649 int bcmerror = 0;
2650
2651 /* To enter download state, disable ARM and reset SOCRAM.
2652 * To exit download state, simply reset ARM (default is RAM boot).
2653 */
2654 if (enter) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002655 bus->alp_only = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002656
Franky Lineb5dc512011-04-25 19:34:04 -07002657 dhdsdio_chip_disablecore(bus->sdh, bus->ci->armcorebase);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002658
Franky Lineb5dc512011-04-25 19:34:04 -07002659 dhdsdio_chip_resetcore(bus->sdh, bus->ci->ramcorebase);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002660
2661 /* Clear the top bit of memory */
2662 if (bus->ramsize) {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002663 u32 zeros = 0;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002664 dhdsdio_membytes(bus, true, bus->ramsize - 4,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002665 (u8 *)&zeros, 4);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002666 }
2667 } else {
Franky Lineb5dc512011-04-25 19:34:04 -07002668 regdata = bcmsdh_reg_read(bus->sdh,
2669 CORE_SB(bus->ci->ramcorebase, sbtmstatelow), 4);
2670 regdata &= (SBTML_RESET | SBTML_REJ_MASK |
2671 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
2672 if ((SICF_CLOCK_EN << SBTML_SICF_SHIFT) != regdata) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002673 DHD_ERROR(("%s: SOCRAM core is down after reset?\n",
2674 __func__));
Roland Vossenb74ac122011-05-03 11:35:20 +02002675 bcmerror = -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002676 goto fail;
2677 }
2678
2679 bcmerror = dhdsdio_write_vars(bus);
2680 if (bcmerror) {
2681 DHD_ERROR(("%s: no vars written to RAM\n", __func__));
2682 bcmerror = 0;
2683 }
2684
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002685 W_SDREG(0xFFFFFFFF, &bus->regs->intstatus, retries);
2686
Franky Lineb5dc512011-04-25 19:34:04 -07002687 dhdsdio_chip_resetcore(bus->sdh, bus->ci->armcorebase);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002688
2689 /* Allow HT Clock now that the ARM is running. */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002690 bus->alp_only = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002691
2692 bus->dhd->busstate = DHD_BUS_LOAD;
2693 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002694fail:
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002695 return bcmerror;
2696}
2697
2698int
2699dhd_bus_iovar_op(dhd_pub_t *dhdp, const char *name,
2700 void *params, int plen, void *arg, int len, bool set)
2701{
2702 dhd_bus_t *bus = dhdp->bus;
2703 const bcm_iovar_t *vi = NULL;
2704 int bcmerror = 0;
2705 int val_size;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002706 u32 actionid;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002707
2708 DHD_TRACE(("%s: Enter\n", __func__));
2709
2710 ASSERT(name);
2711 ASSERT(len >= 0);
2712
2713 /* Get MUST have return space */
2714 ASSERT(set || (arg && len));
2715
2716 /* Set does NOT take qualifiers */
2717 ASSERT(!set || (!params && !plen));
2718
2719 /* Look up var locally; if not found pass to host driver */
2720 vi = bcm_iovar_lookup(dhdsdio_iovars, name);
2721 if (vi == NULL) {
2722 dhd_os_sdlock(bus->dhd);
2723
2724 BUS_WAKE(bus);
2725
2726 /* Turn on clock in case SD command needs backplane */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002727 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002728
2729 bcmerror =
2730 bcmsdh_iovar_op(bus->sdh, name, params, plen, arg, len,
2731 set);
2732
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002733 /* Similar check for blocksize change */
2734 if (set && strcmp(name, "sd_blocksize") == 0) {
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002735 s32 fnum = 2;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002736 if (bcmsdh_iovar_op
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002737 (bus->sdh, "sd_blocksize", &fnum, sizeof(s32),
2738 &bus->blocksize, sizeof(s32),
Roland Vossena1c5ad82011-04-11 15:16:24 +02002739 false) != 0) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002740 bus->blocksize = 0;
2741 DHD_ERROR(("%s: fail on %s get\n", __func__,
2742 "sd_blocksize"));
2743 } else {
2744 DHD_INFO(("%s: noted %s update, value now %d\n",
2745 __func__, "sd_blocksize",
2746 bus->blocksize));
2747 }
2748 }
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07002749 bus->roundup = min(max_roundup, bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002750
2751 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002752 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002753 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002754 }
2755
2756 dhd_os_sdunlock(bus->dhd);
2757 goto exit;
2758 }
2759
2760 DHD_CTL(("%s: %s %s, len %d plen %d\n", __func__,
2761 name, (set ? "set" : "get"), len, plen));
2762
2763 /* set up 'params' pointer in case this is a set command so that
2764 * the convenience int and bool code can be common to set and get
2765 */
2766 if (params == NULL) {
2767 params = arg;
2768 plen = len;
2769 }
2770
2771 if (vi->type == IOVT_VOID)
2772 val_size = 0;
2773 else if (vi->type == IOVT_BUFFER)
2774 val_size = len;
2775 else
2776 /* all other types are integer sized */
2777 val_size = sizeof(int);
2778
2779 actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
2780 bcmerror =
2781 dhdsdio_doiovar(bus, vi, actionid, name, params, plen, arg, len,
2782 val_size);
2783
2784exit:
2785 return bcmerror;
2786}
2787
2788void dhd_bus_stop(struct dhd_bus *bus, bool enforce_mutex)
2789{
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002790 u32 local_hostintmask;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002791 u8 saveclk;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002792 uint retries;
2793 int err;
2794
2795 DHD_TRACE(("%s: Enter\n", __func__));
2796
2797 if (enforce_mutex)
2798 dhd_os_sdlock(bus->dhd);
2799
2800 BUS_WAKE(bus);
2801
2802 /* Enable clock for device interrupts */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002803 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002804
2805 /* Disable and clear interrupts at the chip level also */
2806 W_SDREG(0, &bus->regs->hostintmask, retries);
2807 local_hostintmask = bus->hostintmask;
2808 bus->hostintmask = 0;
2809
2810 /* Change our idea of bus state */
2811 bus->dhd->busstate = DHD_BUS_DOWN;
2812
2813 /* Force clocks on backplane to be sure F2 interrupt propagates */
2814 saveclk =
2815 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2816 &err);
2817 if (!err) {
2818 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2819 (saveclk | SBSDIO_FORCE_HT), &err);
2820 }
2821 if (err) {
2822 DHD_ERROR(("%s: Failed to force clock for F2: err %d\n",
2823 __func__, err));
2824 }
2825
2826 /* Turn off the bus (F2), free any pending packets */
2827 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
2828 bcmsdh_intr_disable(bus->sdh);
2829 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN,
2830 SDIO_FUNC_ENABLE_1, NULL);
2831
2832 /* Clear any pending interrupts now that F2 is disabled */
2833 W_SDREG(local_hostintmask, &bus->regs->intstatus, retries);
2834
2835 /* Turn off the backplane clock (only) */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002836 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002837
2838 /* Clear the data packet queues */
Clemens Noss84067de2011-05-15 22:45:30 +02002839 bcm_pktq_flush(&bus->txq, true, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002840
2841 /* Clear any held glomming stuff */
2842 if (bus->glomd)
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02002843 bcm_pkt_buf_free_skb(bus->glomd);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002844
2845 if (bus->glom)
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02002846 bcm_pkt_buf_free_skb(bus->glom);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002847
2848 bus->glom = bus->glomd = NULL;
2849
2850 /* Clear rx control and wake any waiters */
2851 bus->rxlen = 0;
2852 dhd_os_ioctl_resp_wake(bus->dhd);
2853
2854 /* Reset some F2 state stuff */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002855 bus->rxskip = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002856 bus->tx_seq = bus->rx_seq = 0;
2857
2858 if (enforce_mutex)
2859 dhd_os_sdunlock(bus->dhd);
2860}
2861
2862int dhd_bus_init(dhd_pub_t *dhdp, bool enforce_mutex)
2863{
2864 dhd_bus_t *bus = dhdp->bus;
2865 dhd_timeout_t tmo;
2866 uint retries = 0;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002867 u8 ready, enable;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002868 int err, ret = 0;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002869 u8 saveclk;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002870
2871 DHD_TRACE(("%s: Enter\n", __func__));
2872
2873 ASSERT(bus->dhd);
2874 if (!bus->dhd)
2875 return 0;
2876
2877 if (enforce_mutex)
2878 dhd_os_sdlock(bus->dhd);
2879
2880 /* Make sure backplane clock is on, needed to generate F2 interrupt */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002881 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002882 if (bus->clkstate != CLK_AVAIL)
2883 goto exit;
2884
2885 /* Force clocks on backplane to be sure F2 interrupt propagates */
2886 saveclk =
2887 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2888 &err);
2889 if (!err) {
2890 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2891 (saveclk | SBSDIO_FORCE_HT), &err);
2892 }
2893 if (err) {
2894 DHD_ERROR(("%s: Failed to force clock for F2: err %d\n",
2895 __func__, err));
2896 goto exit;
2897 }
2898
2899 /* Enable function 2 (frame transfers) */
2900 W_SDREG((SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT),
2901 &bus->regs->tosbmailboxdata, retries);
2902 enable = (SDIO_FUNC_ENABLE_1 | SDIO_FUNC_ENABLE_2);
2903
2904 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, enable, NULL);
2905
2906 /* Give the dongle some time to do its thing and set IOR2 */
2907 dhd_timeout_start(&tmo, DHD_WAIT_F2RDY * 1000);
2908
2909 ready = 0;
2910 while (ready != enable && !dhd_timeout_expired(&tmo))
2911 ready =
2912 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IORDY,
2913 NULL);
2914
2915 DHD_INFO(("%s: enable 0x%02x, ready 0x%02x (waited %uus)\n",
2916 __func__, enable, ready, tmo.elapsed));
2917
2918 /* If F2 successfully enabled, set core and enable interrupts */
2919 if (ready == enable) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002920 /* Set up the interrupt mask and enable interrupts */
2921 bus->hostintmask = HOSTINTMASK;
Franky Linc05df632011-04-25 19:34:07 -07002922 W_SDREG(bus->hostintmask,
2923 (unsigned int *)CORE_BUS_REG(bus->ci->buscorebase,
2924 hostintmask), retries);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002925
2926 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_WATERMARK,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002927 (u8) watermark, &err);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002928
2929 /* Set bus state according to enable result */
2930 dhdp->busstate = DHD_BUS_DATA;
2931
2932 /* bcmsdh_intr_unmask(bus->sdh); */
2933
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002934 bus->intdis = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002935 if (bus->intr) {
2936 DHD_INTR(("%s: enable SDIO device interrupts\n",
2937 __func__));
2938 bcmsdh_intr_enable(bus->sdh);
2939 } else {
2940 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
2941 bcmsdh_intr_disable(bus->sdh);
2942 }
2943
2944 }
2945
2946 else {
2947 /* Disable F2 again */
2948 enable = SDIO_FUNC_ENABLE_1;
2949 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, enable,
2950 NULL);
2951 }
2952
2953 /* Restore previous clock setting */
2954 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2955 saveclk, &err);
2956
2957 /* If we didn't come up, turn off backplane clock */
2958 if (dhdp->busstate != DHD_BUS_DATA)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002959 dhdsdio_clkctl(bus, CLK_NONE, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002960
2961exit:
2962 if (enforce_mutex)
2963 dhd_os_sdunlock(bus->dhd);
2964
2965 return ret;
2966}
2967
2968static void dhdsdio_rxfail(dhd_bus_t *bus, bool abort, bool rtx)
2969{
2970 bcmsdh_info_t *sdh = bus->sdh;
2971 sdpcmd_regs_t *regs = bus->regs;
2972 uint retries = 0;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07002973 u16 lastrbc;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002974 u8 hi, lo;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002975 int err;
2976
2977 DHD_ERROR(("%s: %sterminate frame%s\n", __func__,
2978 (abort ? "abort command, " : ""),
2979 (rtx ? ", send NAK" : "")));
2980
2981 if (abort)
2982 bcmsdh_abort(sdh, SDIO_FUNC_2);
2983
2984 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_FRAMECTRL, SFC_RF_TERM,
2985 &err);
2986 bus->f1regdata++;
2987
2988 /* Wait until the packet has been flushed (device/FIFO stable) */
2989 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
2990 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_RFRAMEBCHI,
2991 NULL);
2992 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_RFRAMEBCLO,
2993 NULL);
2994 bus->f1regdata += 2;
2995
2996 if ((hi == 0) && (lo == 0))
2997 break;
2998
2999 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
3000 DHD_ERROR(("%s: count growing: last 0x%04x now "
3001 "0x%04x\n",
3002 __func__, lastrbc, ((hi << 8) + lo)));
3003 }
3004 lastrbc = (hi << 8) + lo;
3005 }
3006
3007 if (!retries) {
3008 DHD_ERROR(("%s: count never zeroed: last 0x%04x\n",
3009 __func__, lastrbc));
3010 } else {
3011 DHD_INFO(("%s: flush took %d iterations\n", __func__,
3012 (0xffff - retries)));
3013 }
3014
3015 if (rtx) {
3016 bus->rxrtx++;
3017 W_SDREG(SMB_NAK, &regs->tosbmailbox, retries);
3018 bus->f1regdata++;
3019 if (retries <= retry_limit)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003020 bus->rxskip = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003021 }
3022
3023 /* Clear partial in any case */
3024 bus->nextlen = 0;
3025
3026 /* If we can't reach the device, signal failure */
3027 if (err || bcmsdh_regfail(sdh))
3028 bus->dhd->busstate = DHD_BUS_DOWN;
3029}
3030
3031static void
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003032dhdsdio_read_control(dhd_bus_t *bus, u8 *hdr, uint len, uint doff)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003033{
3034 bcmsdh_info_t *sdh = bus->sdh;
3035 uint rdlen, pad;
3036
3037 int sdret;
3038
3039 DHD_TRACE(("%s: Enter\n", __func__));
3040
3041 /* Control data already received in aligned rxctl */
3042 if ((bus->bus == SPI_BUS) && (!bus->usebufpool))
3043 goto gotpkt;
3044
3045 ASSERT(bus->rxbuf);
3046 /* Set rxctl for frame (w/optional alignment) */
3047 bus->rxctl = bus->rxbuf;
3048 if (dhd_alignctl) {
3049 bus->rxctl += firstread;
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07003050 pad = ((unsigned long)bus->rxctl % DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003051 if (pad)
3052 bus->rxctl += (DHD_SDALIGN - pad);
3053 bus->rxctl -= firstread;
3054 }
3055 ASSERT(bus->rxctl >= bus->rxbuf);
3056
3057 /* Copy the already-read portion over */
Stanislav Fomichev02160692011-02-15 01:05:10 +03003058 memcpy(bus->rxctl, hdr, firstread);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003059 if (len <= firstread)
3060 goto gotpkt;
3061
3062 /* Copy the full data pkt in gSPI case and process ioctl. */
3063 if (bus->bus == SPI_BUS) {
Stanislav Fomichev02160692011-02-15 01:05:10 +03003064 memcpy(bus->rxctl, hdr, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003065 goto gotpkt;
3066 }
3067
3068 /* Raise rdlen to next SDIO block to avoid tail command */
3069 rdlen = len - firstread;
3070 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
3071 pad = bus->blocksize - (rdlen % bus->blocksize);
3072 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
3073 ((len + pad) < bus->dhd->maxctl))
3074 rdlen += pad;
3075 } else if (rdlen % DHD_SDALIGN) {
3076 rdlen += DHD_SDALIGN - (rdlen % DHD_SDALIGN);
3077 }
3078
3079 /* Satisfy length-alignment requirements */
3080 if (forcealign && (rdlen & (ALIGNMENT - 1)))
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003081 rdlen = roundup(rdlen, ALIGNMENT);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003082
3083 /* Drop if the read is too big or it exceeds our maximum */
3084 if ((rdlen + firstread) > bus->dhd->maxctl) {
3085 DHD_ERROR(("%s: %d-byte control read exceeds %d-byte buffer\n",
3086 __func__, rdlen, bus->dhd->maxctl));
3087 bus->dhd->rx_errors++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003088 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003089 goto done;
3090 }
3091
3092 if ((len - doff) > bus->dhd->maxctl) {
3093 DHD_ERROR(("%s: %d-byte ctl frame (%d-byte ctl data) exceeds "
3094 "%d-byte limit\n",
3095 __func__, len, (len - doff), bus->dhd->maxctl));
3096 bus->dhd->rx_errors++;
3097 bus->rx_toolong++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003098 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003099 goto done;
3100 }
3101
3102 /* Read remainder of frame body into the rxctl buffer */
Grant Grundler4b455e02011-05-04 09:59:47 -07003103 sdret = bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
3104 F2SYNC, (bus->rxctl + firstread), rdlen,
3105 NULL, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003106 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003107 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003108
3109 /* Control frame failures need retransmission */
3110 if (sdret < 0) {
3111 DHD_ERROR(("%s: read %d control bytes failed: %d\n",
3112 __func__, rdlen, sdret));
3113 bus->rxc_errors++; /* dhd.rx_ctlerrs is higher level */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003114 dhdsdio_rxfail(bus, true, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003115 goto done;
3116 }
3117
3118gotpkt:
3119
3120#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003121 if (DHD_BYTES_ON() && DHD_CTL_ON()) {
3122 printk(KERN_DEBUG "RxCtrl:\n");
3123 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, bus->rxctl, len);
3124 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003125#endif
3126
3127 /* Point to valid data and indicate its length */
3128 bus->rxctl += doff;
3129 bus->rxlen = len - doff;
3130
3131done:
3132 /* Awake any waiters */
3133 dhd_os_ioctl_resp_wake(bus->dhd);
3134}
3135
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003136static u8 dhdsdio_rxglom(dhd_bus_t *bus, u8 rxseq)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003137{
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003138 u16 dlen, totlen;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003139 u8 *dptr, num = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003140
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003141 u16 sublen, check;
Arend van Sprielc26b1372010-11-23 14:06:23 +01003142 struct sk_buff *pfirst, *plast, *pnext, *save_pfirst;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003143
3144 int errcode;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003145 u8 chan, seq, doff, sfdoff;
3146 u8 txmax;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003147
3148 int ifidx = 0;
3149 bool usechain = bus->use_rxchain;
3150
3151 /* If packets, issue read(s) and send up packet chain */
3152 /* Return sequence numbers consumed? */
3153
3154 DHD_TRACE(("dhdsdio_rxglom: start: glomd %p glom %p\n", bus->glomd,
3155 bus->glom));
3156
3157 /* If there's a descriptor, generate the packet chain */
3158 if (bus->glomd) {
3159 dhd_os_sdlock_rxq(bus->dhd);
3160
3161 pfirst = plast = pnext = NULL;
Arend van Spriel54991ad2010-11-23 14:06:24 +01003162 dlen = (u16) (bus->glomd->len);
3163 dptr = bus->glomd->data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003164 if (!dlen || (dlen & 1)) {
3165 DHD_ERROR(("%s: bad glomd len(%d), ignore descriptor\n",
3166 __func__, dlen));
3167 dlen = 0;
3168 }
3169
3170 for (totlen = num = 0; dlen; num++) {
3171 /* Get (and move past) next length */
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003172 sublen = get_unaligned_le16(dptr);
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003173 dlen -= sizeof(u16);
3174 dptr += sizeof(u16);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003175 if ((sublen < SDPCM_HDRLEN) ||
3176 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
3177 DHD_ERROR(("%s: descriptor len %d bad: %d\n",
3178 __func__, num, sublen));
3179 pnext = NULL;
3180 break;
3181 }
3182 if (sublen % DHD_SDALIGN) {
3183 DHD_ERROR(("%s: sublen %d not multiple of %d\n",
3184 __func__, sublen, DHD_SDALIGN));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003185 usechain = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003186 }
3187 totlen += sublen;
3188
3189 /* For last frame, adjust read len so total
3190 is a block multiple */
3191 if (!dlen) {
3192 sublen +=
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003193 (roundup(totlen, bus->blocksize) - totlen);
3194 totlen = roundup(totlen, bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003195 }
3196
3197 /* Allocate/chain packet for next subframe */
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003198 pnext = bcm_pkt_buf_get_skb(sublen + DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003199 if (pnext == NULL) {
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003200 DHD_ERROR(("%s: bcm_pkt_buf_get_skb failed, "
3201 "num %d len %d\n", __func__,
3202 num, sublen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003203 break;
3204 }
Arend van Spriel54991ad2010-11-23 14:06:24 +01003205 ASSERT(!(pnext->prev));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003206 if (!pfirst) {
3207 ASSERT(!plast);
3208 pfirst = plast = pnext;
3209 } else {
3210 ASSERT(plast);
Arend van Spriel54991ad2010-11-23 14:06:24 +01003211 plast->next = pnext;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003212 plast = pnext;
3213 }
3214
3215 /* Adhere to start alignment requirements */
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01003216 PKTALIGN(pnext, sublen, DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003217 }
3218
3219 /* If all allocations succeeded, save packet chain
3220 in bus structure */
3221 if (pnext) {
3222 DHD_GLOM(("%s: allocated %d-byte packet chain for %d "
3223 "subframes\n", __func__, totlen, num));
3224 if (DHD_GLOM_ON() && bus->nextlen) {
3225 if (totlen != bus->nextlen) {
3226 DHD_GLOM(("%s: glomdesc mismatch: nextlen %d glomdesc %d " "rxseq %d\n",
3227 __func__, bus->nextlen,
3228 totlen, rxseq));
3229 }
3230 }
3231 bus->glom = pfirst;
3232 pfirst = pnext = NULL;
3233 } else {
3234 if (pfirst)
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003235 bcm_pkt_buf_free_skb(pfirst);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003236 bus->glom = NULL;
3237 num = 0;
3238 }
3239
3240 /* Done with descriptor packet */
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003241 bcm_pkt_buf_free_skb(bus->glomd);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003242 bus->glomd = NULL;
3243 bus->nextlen = 0;
3244
3245 dhd_os_sdunlock_rxq(bus->dhd);
3246 }
3247
3248 /* Ok -- either we just generated a packet chain,
3249 or had one from before */
3250 if (bus->glom) {
3251 if (DHD_GLOM_ON()) {
3252 DHD_GLOM(("%s: try superframe read, packet chain:\n",
3253 __func__));
Arend van Spriel54991ad2010-11-23 14:06:24 +01003254 for (pnext = bus->glom; pnext; pnext = pnext->next) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003255 DHD_GLOM((" %p: %p len 0x%04x (%d)\n",
Arend van Spriel54991ad2010-11-23 14:06:24 +01003256 pnext, (u8 *) (pnext->data),
3257 pnext->len, pnext->len));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003258 }
3259 }
3260
3261 pfirst = bus->glom;
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003262 dlen = (u16) bcm_pkttotlen(pfirst);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003263
3264 /* Do an SDIO read for the superframe. Configurable iovar to
3265 * read directly into the chained packet, or allocate a large
3266 * packet and and copy into the chain.
3267 */
3268 if (usechain) {
Grant Grundler4b455e02011-05-04 09:59:47 -07003269 errcode = bcmsdh_recv_buf(bus,
3270 bcmsdh_cur_sbwad(bus->sdh), SDIO_FUNC_2,
3271 F2SYNC, (u8 *) pfirst->data, dlen,
3272 pfirst, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003273 } else if (bus->dataptr) {
Grant Grundler4b455e02011-05-04 09:59:47 -07003274 errcode = bcmsdh_recv_buf(bus,
3275 bcmsdh_cur_sbwad(bus->sdh), SDIO_FUNC_2,
3276 F2SYNC, bus->dataptr, dlen,
3277 NULL, NULL, NULL);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003278 sublen = (u16) bcm_pktfrombuf(pfirst, 0, dlen,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003279 bus->dataptr);
3280 if (sublen != dlen) {
3281 DHD_ERROR(("%s: FAILED TO COPY, dlen %d sublen %d\n",
3282 __func__, dlen, sublen));
3283 errcode = -1;
3284 }
3285 pnext = NULL;
3286 } else {
3287 DHD_ERROR(("COULDN'T ALLOC %d-BYTE GLOM, FORCE FAILURE\n",
3288 dlen));
3289 errcode = -1;
3290 }
3291 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003292 ASSERT(errcode != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003293
3294 /* On failure, kill the superframe, allow a couple retries */
3295 if (errcode < 0) {
3296 DHD_ERROR(("%s: glom read of %d bytes failed: %d\n",
3297 __func__, dlen, errcode));
3298 bus->dhd->rx_errors++;
3299
3300 if (bus->glomerr++ < 3) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003301 dhdsdio_rxfail(bus, true, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003302 } else {
3303 bus->glomerr = 0;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003304 dhdsdio_rxfail(bus, true, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003305 dhd_os_sdlock_rxq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003306 bcm_pkt_buf_free_skb(bus->glom);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003307 dhd_os_sdunlock_rxq(bus->dhd);
3308 bus->rxglomfail++;
3309 bus->glom = NULL;
3310 }
3311 return 0;
3312 }
3313#ifdef DHD_DEBUG
3314 if (DHD_GLOM_ON()) {
Arend van Spriel34227312011-05-10 22:25:32 +02003315 printk(KERN_DEBUG "SUPERFRAME:\n");
3316 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3317 pfirst->data, min_t(int, pfirst->len, 48));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003318 }
3319#endif
3320
3321 /* Validate the superframe header */
Arend van Spriel54991ad2010-11-23 14:06:24 +01003322 dptr = (u8 *) (pfirst->data);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003323 sublen = get_unaligned_le16(dptr);
3324 check = get_unaligned_le16(dptr + sizeof(u16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003325
3326 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3327 seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
3328 bus->nextlen = dptr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
3329 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
3330 DHD_INFO(("%s: nextlen too large (%d) seq %d\n",
3331 __func__, bus->nextlen, seq));
3332 bus->nextlen = 0;
3333 }
3334 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3335 txmax = SDPCM_WINDOW_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3336
3337 errcode = 0;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003338 if ((u16)~(sublen ^ check)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003339 DHD_ERROR(("%s (superframe): HW hdr error: len/check "
3340 "0x%04x/0x%04x\n", __func__, sublen, check));
3341 errcode = -1;
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003342 } else if (roundup(sublen, bus->blocksize) != dlen) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003343 DHD_ERROR(("%s (superframe): len 0x%04x, rounded "
3344 "0x%04x, expect 0x%04x\n",
3345 __func__, sublen,
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003346 roundup(sublen, bus->blocksize), dlen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003347 errcode = -1;
3348 } else if (SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]) !=
3349 SDPCM_GLOM_CHANNEL) {
3350 DHD_ERROR(("%s (superframe): bad channel %d\n",
3351 __func__,
3352 SDPCM_PACKET_CHANNEL(&dptr
3353 [SDPCM_FRAMETAG_LEN])));
3354 errcode = -1;
3355 } else if (SDPCM_GLOMDESC(&dptr[SDPCM_FRAMETAG_LEN])) {
3356 DHD_ERROR(("%s (superframe): got second descriptor?\n",
3357 __func__));
3358 errcode = -1;
3359 } else if ((doff < SDPCM_HDRLEN) ||
Arend van Spriel54991ad2010-11-23 14:06:24 +01003360 (doff > (pfirst->len - SDPCM_HDRLEN))) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003361 DHD_ERROR(("%s (superframe): Bad data offset %d: HW %d "
3362 "pkt %d min %d\n",
3363 __func__, doff, sublen,
Arend van Spriel54991ad2010-11-23 14:06:24 +01003364 pfirst->len, SDPCM_HDRLEN));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003365 errcode = -1;
3366 }
3367
3368 /* Check sequence number of superframe SW header */
3369 if (rxseq != seq) {
3370 DHD_INFO(("%s: (superframe) rx_seq %d, expected %d\n",
3371 __func__, seq, rxseq));
3372 bus->rx_badseq++;
3373 rxseq = seq;
3374 }
3375
3376 /* Check window for sanity */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003377 if ((u8) (txmax - bus->tx_seq) > 0x40) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003378 DHD_ERROR(("%s: unlikely tx max %d with tx_seq %d\n",
3379 __func__, txmax, bus->tx_seq));
3380 txmax = bus->tx_seq + 2;
3381 }
3382 bus->tx_max = txmax;
3383
3384 /* Remove superframe header, remember offset */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01003385 skb_pull(pfirst, doff);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003386 sfdoff = doff;
3387
3388 /* Validate all the subframe headers */
3389 for (num = 0, pnext = pfirst; pnext && !errcode;
Arend van Spriel54991ad2010-11-23 14:06:24 +01003390 num++, pnext = pnext->next) {
3391 dptr = (u8 *) (pnext->data);
3392 dlen = (u16) (pnext->len);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003393 sublen = get_unaligned_le16(dptr);
3394 check = get_unaligned_le16(dptr + sizeof(u16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003395 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3396 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3397#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003398 if (DHD_GLOM_ON()) {
3399 printk(KERN_DEBUG "subframe:\n");
3400 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3401 dptr, 32);
3402 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003403#endif
3404
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003405 if ((u16)~(sublen ^ check)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003406 DHD_ERROR(("%s (subframe %d): HW hdr error: "
3407 "len/check 0x%04x/0x%04x\n",
3408 __func__, num, sublen, check));
3409 errcode = -1;
3410 } else if ((sublen > dlen) || (sublen < SDPCM_HDRLEN)) {
3411 DHD_ERROR(("%s (subframe %d): length mismatch: "
3412 "len 0x%04x, expect 0x%04x\n",
3413 __func__, num, sublen, dlen));
3414 errcode = -1;
3415 } else if ((chan != SDPCM_DATA_CHANNEL) &&
3416 (chan != SDPCM_EVENT_CHANNEL)) {
3417 DHD_ERROR(("%s (subframe %d): bad channel %d\n",
3418 __func__, num, chan));
3419 errcode = -1;
3420 } else if ((doff < SDPCM_HDRLEN) || (doff > sublen)) {
3421 DHD_ERROR(("%s (subframe %d): Bad data offset %d: HW %d min %d\n",
3422 __func__, num, doff, sublen,
3423 SDPCM_HDRLEN));
3424 errcode = -1;
3425 }
3426 }
3427
3428 if (errcode) {
3429 /* Terminate frame on error, request
3430 a couple retries */
3431 if (bus->glomerr++ < 3) {
3432 /* Restore superframe header space */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01003433 skb_push(pfirst, sfdoff);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003434 dhdsdio_rxfail(bus, true, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003435 } else {
3436 bus->glomerr = 0;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003437 dhdsdio_rxfail(bus, true, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003438 dhd_os_sdlock_rxq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003439 bcm_pkt_buf_free_skb(bus->glom);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003440 dhd_os_sdunlock_rxq(bus->dhd);
3441 bus->rxglomfail++;
3442 bus->glom = NULL;
3443 }
3444 bus->nextlen = 0;
3445 return 0;
3446 }
3447
3448 /* Basic SD framing looks ok - process each packet (header) */
3449 save_pfirst = pfirst;
3450 bus->glom = NULL;
3451 plast = NULL;
3452
3453 dhd_os_sdlock_rxq(bus->dhd);
3454 for (num = 0; pfirst; rxseq++, pfirst = pnext) {
Arend van Spriel54991ad2010-11-23 14:06:24 +01003455 pnext = pfirst->next;
3456 pfirst->next = NULL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003457
Arend van Spriel54991ad2010-11-23 14:06:24 +01003458 dptr = (u8 *) (pfirst->data);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003459 sublen = get_unaligned_le16(dptr);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003460 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3461 seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
3462 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3463
3464 DHD_GLOM(("%s: Get subframe %d, %p(%p/%d), sublen %d "
3465 "chan %d seq %d\n",
Arend van Spriel54991ad2010-11-23 14:06:24 +01003466 __func__, num, pfirst, pfirst->data,
3467 pfirst->len, sublen, chan, seq));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003468
3469 ASSERT((chan == SDPCM_DATA_CHANNEL)
3470 || (chan == SDPCM_EVENT_CHANNEL));
3471
3472 if (rxseq != seq) {
3473 DHD_GLOM(("%s: rx_seq %d, expected %d\n",
3474 __func__, seq, rxseq));
3475 bus->rx_badseq++;
3476 rxseq = seq;
3477 }
3478#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003479 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
3480 printk(KERN_DEBUG "Rx Subframe Data:\n");
3481 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3482 dptr, dlen);
3483 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003484#endif
3485
Arend van Spriel2cb8ada2010-11-18 20:46:44 +01003486 __skb_trim(pfirst, sublen);
Arend van Sprielc303ecb2010-11-18 20:46:43 +01003487 skb_pull(pfirst, doff);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003488
Arend van Spriel54991ad2010-11-23 14:06:24 +01003489 if (pfirst->len == 0) {
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003490 bcm_pkt_buf_free_skb(pfirst);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003491 if (plast) {
Arend van Spriel54991ad2010-11-23 14:06:24 +01003492 plast->next = pnext;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003493 } else {
3494 ASSERT(save_pfirst == pfirst);
3495 save_pfirst = pnext;
3496 }
3497 continue;
3498 } else if (dhd_prot_hdrpull(bus->dhd, &ifidx, pfirst) !=
3499 0) {
3500 DHD_ERROR(("%s: rx protocol error\n",
3501 __func__));
3502 bus->dhd->rx_errors++;
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003503 bcm_pkt_buf_free_skb(pfirst);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003504 if (plast) {
Arend van Spriel54991ad2010-11-23 14:06:24 +01003505 plast->next = pnext;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003506 } else {
3507 ASSERT(save_pfirst == pfirst);
3508 save_pfirst = pnext;
3509 }
3510 continue;
3511 }
3512
3513 /* this packet will go up, link back into
3514 chain and count it */
Arend van Spriel54991ad2010-11-23 14:06:24 +01003515 pfirst->next = pnext;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003516 plast = pfirst;
3517 num++;
3518
3519#ifdef DHD_DEBUG
3520 if (DHD_GLOM_ON()) {
3521 DHD_GLOM(("%s subframe %d to stack, %p(%p/%d) "
3522 "nxt/lnk %p/%p\n",
Arend van Spriel54991ad2010-11-23 14:06:24 +01003523 __func__, num, pfirst, pfirst->data,
3524 pfirst->len, pfirst->next,
3525 pfirst->prev));
Arend van Spriel34227312011-05-10 22:25:32 +02003526 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3527 pfirst->data,
3528 min_t(int, pfirst->len, 32));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003529 }
3530#endif /* DHD_DEBUG */
3531 }
3532 dhd_os_sdunlock_rxq(bus->dhd);
3533 if (num) {
3534 dhd_os_sdunlock(bus->dhd);
3535 dhd_rx_frame(bus->dhd, ifidx, save_pfirst, num);
3536 dhd_os_sdlock(bus->dhd);
3537 }
3538
3539 bus->rxglomframes++;
3540 bus->rxglompkts += num;
3541 }
3542 return num;
3543}
3544
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003545/* Return true if there may be more frames to read */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003546static uint dhdsdio_readframes(dhd_bus_t *bus, uint maxframes, bool *finished)
3547{
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003548 bcmsdh_info_t *sdh = bus->sdh;
3549
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003550 u16 len, check; /* Extracted hardware header fields */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003551 u8 chan, seq, doff; /* Extracted software header fields */
3552 u8 fcbits; /* Extracted fcbits from software header */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003553
Arend van Sprielc26b1372010-11-23 14:06:23 +01003554 struct sk_buff *pkt; /* Packet for event or data frames */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003555 u16 pad; /* Number of pad bytes to read */
3556 u16 rdlen; /* Total number of bytes to read */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003557 u8 rxseq; /* Next sequence number to expect */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003558 uint rxleft = 0; /* Remaining number of frames allowed */
3559 int sdret; /* Return code from bcmsdh calls */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003560 u8 txmax; /* Maximum tx sequence offered */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003561 bool len_consistent; /* Result of comparing readahead len and
3562 len from hw-hdr */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003563 u8 *rxbuf;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003564 int ifidx = 0;
3565 uint rxcount = 0; /* Total frames read */
3566
3567#if defined(DHD_DEBUG) || defined(SDTEST)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003568 bool sdtest = false; /* To limit message spew from test mode */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003569#endif
3570
3571 DHD_TRACE(("%s: Enter\n", __func__));
3572
3573 ASSERT(maxframes);
3574
3575#ifdef SDTEST
3576 /* Allow pktgen to override maxframes */
3577 if (bus->pktgen_count && (bus->pktgen_mode == DHD_PKTGEN_RECV)) {
3578 maxframes = bus->pktgen_count;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003579 sdtest = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003580 }
3581#endif
3582
3583 /* Not finished unless we encounter no more frames indication */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003584 *finished = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003585
3586 for (rxseq = bus->rx_seq, rxleft = maxframes;
3587 !bus->rxskip && rxleft && bus->dhd->busstate != DHD_BUS_DOWN;
3588 rxseq++, rxleft--) {
3589
3590 /* Handle glomming separately */
3591 if (bus->glom || bus->glomd) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003592 u8 cnt;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003593 DHD_GLOM(("%s: calling rxglom: glomd %p, glom %p\n",
3594 __func__, bus->glomd, bus->glom));
3595 cnt = dhdsdio_rxglom(bus, rxseq);
3596 DHD_GLOM(("%s: rxglom returned %d\n", __func__, cnt));
3597 rxseq += cnt - 1;
3598 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
3599 continue;
3600 }
3601
3602 /* Try doing single read if we can */
3603 if (dhd_readahead && bus->nextlen) {
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003604 u16 nextlen = bus->nextlen;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003605 bus->nextlen = 0;
3606
3607 if (bus->bus == SPI_BUS) {
3608 rdlen = len = nextlen;
3609 } else {
3610 rdlen = len = nextlen << 4;
3611
3612 /* Pad read to blocksize for efficiency */
3613 if (bus->roundup && bus->blocksize
3614 && (rdlen > bus->blocksize)) {
3615 pad =
3616 bus->blocksize -
3617 (rdlen % bus->blocksize);
3618 if ((pad <= bus->roundup)
3619 && (pad < bus->blocksize)
3620 && ((rdlen + pad + firstread) <
3621 MAX_RX_DATASZ))
3622 rdlen += pad;
3623 } else if (rdlen % DHD_SDALIGN) {
3624 rdlen +=
3625 DHD_SDALIGN - (rdlen % DHD_SDALIGN);
3626 }
3627 }
3628
3629 /* We use bus->rxctl buffer in WinXP for initial
3630 * control pkt receives.
3631 * Later we use buffer-poll for data as well
3632 * as control packets.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003633 * This is required because dhd receives full
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003634 * frame in gSPI unlike SDIO.
3635 * After the frame is received we have to
3636 * distinguish whether it is data
3637 * or non-data frame.
3638 */
3639 /* Allocate a packet buffer */
3640 dhd_os_sdlock_rxq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003641 pkt = bcm_pkt_buf_get_skb(rdlen + DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003642 if (!pkt) {
3643 if (bus->bus == SPI_BUS) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003644 bus->usebufpool = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003645 bus->rxctl = bus->rxbuf;
3646 if (dhd_alignctl) {
3647 bus->rxctl += firstread;
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07003648 pad = ((unsigned long)bus->rxctl %
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003649 DHD_SDALIGN);
3650 if (pad)
3651 bus->rxctl +=
3652 (DHD_SDALIGN - pad);
3653 bus->rxctl -= firstread;
3654 }
3655 ASSERT(bus->rxctl >= bus->rxbuf);
3656 rxbuf = bus->rxctl;
3657 /* Read the entire frame */
Grant Grundler4b455e02011-05-04 09:59:47 -07003658 sdret = bcmsdh_recv_buf(bus,
3659 bcmsdh_cur_sbwad(sdh),
3660 SDIO_FUNC_2, F2SYNC,
3661 rxbuf, rdlen,
3662 NULL, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003663 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003664 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003665
3666 /* Control frame failures need
3667 retransmission */
3668 if (sdret < 0) {
3669 DHD_ERROR(("%s: read %d control bytes failed: %d\n",
3670 __func__,
3671 rdlen, sdret));
3672 /* dhd.rx_ctlerrs is higher */
3673 bus->rxc_errors++;
3674 dhd_os_sdunlock_rxq(bus->dhd);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003675 dhdsdio_rxfail(bus, true,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003676 (bus->bus ==
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003677 SPI_BUS) ? false
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003678 : true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003679 continue;
3680 }
3681 } else {
3682 /* Give up on data,
3683 request rtx of events */
Arend van Sprielcda64a52011-05-10 22:25:33 +02003684 DHD_ERROR(("%s (nextlen): "
3685 "bcm_pkt_buf_get_skb failed:"
3686 " len %d rdlen %d expected"
3687 " rxseq %d\n", __func__,
3688 len, rdlen, rxseq));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003689 /* Just go try again w/normal
3690 header read */
3691 dhd_os_sdunlock_rxq(bus->dhd);
3692 continue;
3693 }
3694 } else {
3695 if (bus->bus == SPI_BUS)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003696 bus->usebufpool = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003697
Arend van Spriel54991ad2010-11-23 14:06:24 +01003698 ASSERT(!(pkt->prev));
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01003699 PKTALIGN(pkt, rdlen, DHD_SDALIGN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01003700 rxbuf = (u8 *) (pkt->data);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003701 /* Read the entire frame */
Grant Grundler4b455e02011-05-04 09:59:47 -07003702 sdret = bcmsdh_recv_buf(bus,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003703 bcmsdh_cur_sbwad(sdh),
3704 SDIO_FUNC_2, F2SYNC,
Grant Grundler4b455e02011-05-04 09:59:47 -07003705 rxbuf, rdlen,
3706 pkt, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003707 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003708 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003709
3710 if (sdret < 0) {
3711 DHD_ERROR(("%s (nextlen): read %d bytes failed: %d\n",
3712 __func__, rdlen, sdret));
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003713 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003714 bus->dhd->rx_errors++;
3715 dhd_os_sdunlock_rxq(bus->dhd);
3716 /* Force retry w/normal header read.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003717 * Don't attempt NAK for
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003718 * gSPI
3719 */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003720 dhdsdio_rxfail(bus, true,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003721 (bus->bus ==
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003722 SPI_BUS) ? false :
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003723 true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003724 continue;
3725 }
3726 }
3727 dhd_os_sdunlock_rxq(bus->dhd);
3728
3729 /* Now check the header */
Stanislav Fomichev02160692011-02-15 01:05:10 +03003730 memcpy(bus->rxhdr, rxbuf, SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003731
3732 /* Extract hardware header fields */
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003733 len = get_unaligned_le16(bus->rxhdr);
3734 check = get_unaligned_le16(bus->rxhdr + sizeof(u16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003735
3736 /* All zeros means readahead info was bad */
3737 if (!(len | check)) {
3738 DHD_INFO(("%s (nextlen): read zeros in HW "
3739 "header???\n", __func__));
Grant Grundler4b455e02011-05-04 09:59:47 -07003740 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003741 continue;
3742 }
3743
3744 /* Validate check bytes */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003745 if ((u16)~(len ^ check)) {
Grant Grundler4b455e02011-05-04 09:59:47 -07003746 DHD_ERROR(("%s (nextlen): HW hdr error:"
3747 " nextlen/len/check"
3748 " 0x%04x/0x%04x/0x%04x\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003749 __func__, nextlen, len, check));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003750 bus->rx_badhdr++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003751 dhdsdio_rxfail(bus, false, false);
Grant Grundler4b455e02011-05-04 09:59:47 -07003752 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003753 continue;
3754 }
3755
3756 /* Validate frame length */
3757 if (len < SDPCM_HDRLEN) {
3758 DHD_ERROR(("%s (nextlen): HW hdr length "
3759 "invalid: %d\n", __func__, len));
Grant Grundler4b455e02011-05-04 09:59:47 -07003760 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003761 continue;
3762 }
3763
3764 /* Check for consistency withreadahead info */
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003765 len_consistent = (nextlen != (roundup(len, 16) >> 4));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003766 if (len_consistent) {
3767 /* Mismatch, force retry w/normal
3768 header (may be >4K) */
Grant Grundler4b455e02011-05-04 09:59:47 -07003769 DHD_ERROR(("%s (nextlen): mismatch, "
3770 "nextlen %d len %d rnd %d; "
3771 "expected rxseq %d\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003772 __func__, nextlen,
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003773 len, roundup(len, 16), rxseq));
Grant Grundler4b455e02011-05-04 09:59:47 -07003774 dhdsdio_rxfail(bus, true, (bus->bus != SPI_BUS));
3775 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003776 continue;
3777 }
3778
3779 /* Extract software header fields */
Grant Grundler4b455e02011-05-04 09:59:47 -07003780 chan = SDPCM_PACKET_CHANNEL(
3781 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3782 seq = SDPCM_PACKET_SEQUENCE(
3783 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3784 doff = SDPCM_DOFFSET_VALUE(
3785 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3786 txmax = SDPCM_WINDOW_VALUE(
3787 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003788
3789 bus->nextlen =
3790 bus->rxhdr[SDPCM_FRAMETAG_LEN +
3791 SDPCM_NEXTLEN_OFFSET];
3792 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
3793 DHD_INFO(("%s (nextlen): got frame w/nextlen too large" " (%d), seq %d\n",
3794 __func__, bus->nextlen, seq));
3795 bus->nextlen = 0;
3796 }
3797
3798 bus->dhd->rx_readahead_cnt++;
Grant Grundler4b455e02011-05-04 09:59:47 -07003799
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003800 /* Handle Flow Control */
Grant Grundler4b455e02011-05-04 09:59:47 -07003801 fcbits = SDPCM_FCMASK_VALUE(
3802 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003803
Grant Grundler4b455e02011-05-04 09:59:47 -07003804 if (bus->flowcontrol != fcbits) {
3805 if (~bus->flowcontrol & fcbits)
3806 bus->fc_xoff++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003807
Grant Grundler4b455e02011-05-04 09:59:47 -07003808 if (bus->flowcontrol & ~fcbits)
3809 bus->fc_xon++;
3810
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003811 bus->fc_rcvd++;
3812 bus->flowcontrol = fcbits;
3813 }
3814
3815 /* Check and update sequence number */
3816 if (rxseq != seq) {
3817 DHD_INFO(("%s (nextlen): rx_seq %d, expected "
3818 "%d\n", __func__, seq, rxseq));
3819 bus->rx_badseq++;
3820 rxseq = seq;
3821 }
3822
3823 /* Check window for sanity */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003824 if ((u8) (txmax - bus->tx_seq) > 0x40) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003825 DHD_ERROR(("%s: got unlikely tx max %d with "
3826 "tx_seq %d\n",
3827 __func__, txmax, bus->tx_seq));
3828 txmax = bus->tx_seq + 2;
3829 }
3830 bus->tx_max = txmax;
3831
3832#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003833 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
3834 printk(KERN_DEBUG "Rx Data:\n");
3835 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3836 rxbuf, len);
3837 } else if (DHD_HDRS_ON()) {
3838 printk(KERN_DEBUG "RxHdr:\n");
3839 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3840 bus->rxhdr, SDPCM_HDRLEN);
3841 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003842#endif
3843
3844 if (chan == SDPCM_CONTROL_CHANNEL) {
3845 if (bus->bus == SPI_BUS) {
3846 dhdsdio_read_control(bus, rxbuf, len,
3847 doff);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003848 } else {
3849 DHD_ERROR(("%s (nextlen): readahead on control" " packet %d?\n",
3850 __func__, seq));
3851 /* Force retry w/normal header read */
3852 bus->nextlen = 0;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003853 dhdsdio_rxfail(bus, false, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003854 }
Grant Grundler4b455e02011-05-04 09:59:47 -07003855 dhdsdio_pktfree2(bus, pkt);
3856 continue;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003857 }
3858
3859 if ((bus->bus == SPI_BUS) && !bus->usebufpool) {
3860 DHD_ERROR(("Received %d bytes on %d channel. Running out of " "rx pktbuf's or not yet malloced.\n",
3861 len, chan));
3862 continue;
3863 }
3864
3865 /* Validate data offset */
3866 if ((doff < SDPCM_HDRLEN) || (doff > len)) {
3867 DHD_ERROR(("%s (nextlen): bad data offset %d: HW len %d min %d\n",
3868 __func__, doff, len, SDPCM_HDRLEN));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003869 dhdsdio_rxfail(bus, false, false);
Grant Grundler4b455e02011-05-04 09:59:47 -07003870 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003871 continue;
3872 }
3873
3874 /* All done with this one -- now deliver the packet */
3875 goto deliver;
3876 }
3877 /* gSPI frames should not be handled in fractions */
3878 if (bus->bus == SPI_BUS)
3879 break;
3880
3881 /* Read frame header (hardware and software) */
Grant Grundler4b455e02011-05-04 09:59:47 -07003882 sdret = bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh),
3883 SDIO_FUNC_2, F2SYNC, bus->rxhdr, firstread,
3884 NULL, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003885 bus->f2rxhdrs++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003886 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003887
3888 if (sdret < 0) {
3889 DHD_ERROR(("%s: RXHEADER FAILED: %d\n", __func__,
3890 sdret));
3891 bus->rx_hdrfail++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003892 dhdsdio_rxfail(bus, true, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003893 continue;
3894 }
3895#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003896 if (DHD_BYTES_ON() || DHD_HDRS_ON()) {
3897 printk(KERN_DEBUG "RxHdr:\n");
3898 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3899 bus->rxhdr, SDPCM_HDRLEN);
3900 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003901#endif
3902
3903 /* Extract hardware header fields */
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003904 len = get_unaligned_le16(bus->rxhdr);
3905 check = get_unaligned_le16(bus->rxhdr + sizeof(u16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003906
3907 /* All zeros means no more frames */
3908 if (!(len | check)) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003909 *finished = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003910 break;
3911 }
3912
3913 /* Validate check bytes */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003914 if ((u16) ~(len ^ check)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003915 DHD_ERROR(("%s: HW hdr err: len/check 0x%04x/0x%04x\n",
3916 __func__, len, check));
3917 bus->rx_badhdr++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003918 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003919 continue;
3920 }
3921
3922 /* Validate frame length */
3923 if (len < SDPCM_HDRLEN) {
3924 DHD_ERROR(("%s: HW hdr length invalid: %d\n",
3925 __func__, len));
3926 continue;
3927 }
3928
3929 /* Extract software header fields */
3930 chan = SDPCM_PACKET_CHANNEL(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3931 seq = SDPCM_PACKET_SEQUENCE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3932 doff = SDPCM_DOFFSET_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3933 txmax = SDPCM_WINDOW_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3934
3935 /* Validate data offset */
3936 if ((doff < SDPCM_HDRLEN) || (doff > len)) {
3937 DHD_ERROR(("%s: Bad data offset %d: HW len %d, min %d "
3938 "seq %d\n",
3939 __func__, doff, len, SDPCM_HDRLEN, seq));
3940 bus->rx_badhdr++;
3941 ASSERT(0);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003942 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003943 continue;
3944 }
3945
3946 /* Save the readahead length if there is one */
3947 bus->nextlen =
3948 bus->rxhdr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
3949 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
3950 DHD_INFO(("%s (nextlen): got frame w/nextlen too large "
3951 "(%d), seq %d\n",
3952 __func__, bus->nextlen, seq));
3953 bus->nextlen = 0;
3954 }
3955
3956 /* Handle Flow Control */
3957 fcbits = SDPCM_FCMASK_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3958
Grant Grundler4b455e02011-05-04 09:59:47 -07003959 if (bus->flowcontrol != fcbits) {
3960 if (~bus->flowcontrol & fcbits)
3961 bus->fc_xoff++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003962
Grant Grundler4b455e02011-05-04 09:59:47 -07003963 if (bus->flowcontrol & ~fcbits)
3964 bus->fc_xon++;
3965
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003966 bus->fc_rcvd++;
3967 bus->flowcontrol = fcbits;
3968 }
3969
3970 /* Check and update sequence number */
3971 if (rxseq != seq) {
3972 DHD_INFO(("%s: rx_seq %d, expected %d\n", __func__,
3973 seq, rxseq));
3974 bus->rx_badseq++;
3975 rxseq = seq;
3976 }
3977
3978 /* Check window for sanity */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003979 if ((u8) (txmax - bus->tx_seq) > 0x40) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003980 DHD_ERROR(("%s: unlikely tx max %d with tx_seq %d\n",
3981 __func__, txmax, bus->tx_seq));
3982 txmax = bus->tx_seq + 2;
3983 }
3984 bus->tx_max = txmax;
3985
3986 /* Call a separate function for control frames */
3987 if (chan == SDPCM_CONTROL_CHANNEL) {
3988 dhdsdio_read_control(bus, bus->rxhdr, len, doff);
3989 continue;
3990 }
3991
3992 ASSERT((chan == SDPCM_DATA_CHANNEL)
3993 || (chan == SDPCM_EVENT_CHANNEL)
3994 || (chan == SDPCM_TEST_CHANNEL)
3995 || (chan == SDPCM_GLOM_CHANNEL));
3996
3997 /* Length to read */
3998 rdlen = (len > firstread) ? (len - firstread) : 0;
3999
4000 /* May pad read to blocksize for efficiency */
4001 if (bus->roundup && bus->blocksize &&
4002 (rdlen > bus->blocksize)) {
4003 pad = bus->blocksize - (rdlen % bus->blocksize);
4004 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
4005 ((rdlen + pad + firstread) < MAX_RX_DATASZ))
4006 rdlen += pad;
4007 } else if (rdlen % DHD_SDALIGN) {
4008 rdlen += DHD_SDALIGN - (rdlen % DHD_SDALIGN);
4009 }
4010
4011 /* Satisfy length-alignment requirements */
4012 if (forcealign && (rdlen & (ALIGNMENT - 1)))
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07004013 rdlen = roundup(rdlen, ALIGNMENT);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004014
4015 if ((rdlen + firstread) > MAX_RX_DATASZ) {
4016 /* Too long -- skip this frame */
4017 DHD_ERROR(("%s: too long: len %d rdlen %d\n",
4018 __func__, len, rdlen));
4019 bus->dhd->rx_errors++;
4020 bus->rx_toolong++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004021 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004022 continue;
4023 }
4024
4025 dhd_os_sdlock_rxq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004026 pkt = bcm_pkt_buf_get_skb(rdlen + firstread + DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004027 if (!pkt) {
4028 /* Give up on data, request rtx of events */
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004029 DHD_ERROR(("%s: bcm_pkt_buf_get_skb failed: rdlen %d "
4030 "chan %d\n", __func__, rdlen, chan));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004031 bus->dhd->rx_dropped++;
4032 dhd_os_sdunlock_rxq(bus->dhd);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004033 dhdsdio_rxfail(bus, false, RETRYCHAN(chan));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004034 continue;
4035 }
4036 dhd_os_sdunlock_rxq(bus->dhd);
4037
Arend van Spriel54991ad2010-11-23 14:06:24 +01004038 ASSERT(!(pkt->prev));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004039
4040 /* Leave room for what we already read, and align remainder */
Arend van Spriel54991ad2010-11-23 14:06:24 +01004041 ASSERT(firstread < pkt->len);
Arend van Sprielc303ecb2010-11-18 20:46:43 +01004042 skb_pull(pkt, firstread);
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01004043 PKTALIGN(pkt, rdlen, DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004044
4045 /* Read the remaining frame data */
Grant Grundler4b455e02011-05-04 09:59:47 -07004046 sdret = bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
Arend van Spriel54991ad2010-11-23 14:06:24 +01004047 F2SYNC, ((u8 *) (pkt->data)), rdlen,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004048 pkt, NULL, NULL);
4049 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02004050 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004051
4052 if (sdret < 0) {
4053 DHD_ERROR(("%s: read %d %s bytes failed: %d\n",
4054 __func__, rdlen,
4055 ((chan ==
4056 SDPCM_EVENT_CHANNEL) ? "event" : ((chan ==
4057 SDPCM_DATA_CHANNEL)
4058 ? "data" : "test")),
4059 sdret));
4060 dhd_os_sdlock_rxq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004061 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004062 dhd_os_sdunlock_rxq(bus->dhd);
4063 bus->dhd->rx_errors++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004064 dhdsdio_rxfail(bus, true, RETRYCHAN(chan));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004065 continue;
4066 }
4067
4068 /* Copy the already-read portion */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01004069 skb_push(pkt, firstread);
Stanislav Fomichev02160692011-02-15 01:05:10 +03004070 memcpy(pkt->data, bus->rxhdr, firstread);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004071
4072#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02004073 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
4074 printk(KERN_DEBUG "Rx Data:\n");
4075 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
4076 pkt->data, len);
4077 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004078#endif
4079
4080deliver:
4081 /* Save superframe descriptor and allocate packet frame */
4082 if (chan == SDPCM_GLOM_CHANNEL) {
4083 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_FRAMETAG_LEN])) {
4084 DHD_GLOM(("%s: glom descriptor, %d bytes:\n",
4085 __func__, len));
4086#ifdef DHD_DEBUG
4087 if (DHD_GLOM_ON()) {
Arend van Spriel34227312011-05-10 22:25:32 +02004088 printk(KERN_DEBUG "Glom Data:\n");
4089 print_hex_dump_bytes("",
4090 DUMP_PREFIX_OFFSET,
4091 pkt->data, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004092 }
4093#endif
Arend van Spriel2cb8ada2010-11-18 20:46:44 +01004094 __skb_trim(pkt, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004095 ASSERT(doff == SDPCM_HDRLEN);
Arend van Sprielc303ecb2010-11-18 20:46:43 +01004096 skb_pull(pkt, SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004097 bus->glomd = pkt;
4098 } else {
4099 DHD_ERROR(("%s: glom superframe w/o "
4100 "descriptor!\n", __func__));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004101 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004102 }
4103 continue;
4104 }
4105
4106 /* Fill in packet len and prio, deliver upward */
Arend van Spriel2cb8ada2010-11-18 20:46:44 +01004107 __skb_trim(pkt, len);
Arend van Sprielc303ecb2010-11-18 20:46:43 +01004108 skb_pull(pkt, doff);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004109
4110#ifdef SDTEST
4111 /* Test channel packets are processed separately */
4112 if (chan == SDPCM_TEST_CHANNEL) {
4113 dhdsdio_testrcv(bus, pkt, seq);
4114 continue;
4115 }
4116#endif /* SDTEST */
4117
Arend van Spriel54991ad2010-11-23 14:06:24 +01004118 if (pkt->len == 0) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004119 dhd_os_sdlock_rxq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004120 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004121 dhd_os_sdunlock_rxq(bus->dhd);
4122 continue;
4123 } else if (dhd_prot_hdrpull(bus->dhd, &ifidx, pkt) != 0) {
4124 DHD_ERROR(("%s: rx protocol error\n", __func__));
4125 dhd_os_sdlock_rxq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004126 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004127 dhd_os_sdunlock_rxq(bus->dhd);
4128 bus->dhd->rx_errors++;
4129 continue;
4130 }
4131
4132 /* Unlock during rx call */
4133 dhd_os_sdunlock(bus->dhd);
4134 dhd_rx_frame(bus->dhd, ifidx, pkt, 1);
4135 dhd_os_sdlock(bus->dhd);
4136 }
4137 rxcount = maxframes - rxleft;
4138#ifdef DHD_DEBUG
4139 /* Message if we hit the limit */
4140 if (!rxleft && !sdtest)
4141 DHD_DATA(("%s: hit rx limit of %d frames\n", __func__,
4142 maxframes));
4143 else
4144#endif /* DHD_DEBUG */
4145 DHD_DATA(("%s: processed %d frames\n", __func__, rxcount));
4146 /* Back off rxseq if awaiting rtx, update rx_seq */
4147 if (bus->rxskip)
4148 rxseq--;
4149 bus->rx_seq = rxseq;
4150
4151 return rxcount;
4152}
4153
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004154static u32 dhdsdio_hostmail(dhd_bus_t *bus)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004155{
4156 sdpcmd_regs_t *regs = bus->regs;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004157 u32 intstatus = 0;
4158 u32 hmb_data;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004159 u8 fcbits;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004160 uint retries = 0;
4161
4162 DHD_TRACE(("%s: Enter\n", __func__));
4163
4164 /* Read mailbox data and ack that we did so */
4165 R_SDREG(hmb_data, &regs->tohostmailboxdata, retries);
4166 if (retries <= retry_limit)
4167 W_SDREG(SMB_INT_ACK, &regs->tosbmailbox, retries);
4168 bus->f1regdata += 2;
4169
4170 /* Dongle recomposed rx frames, accept them again */
4171 if (hmb_data & HMB_DATA_NAKHANDLED) {
4172 DHD_INFO(("Dongle reports NAK handled, expect rtx of %d\n",
4173 bus->rx_seq));
4174 if (!bus->rxskip)
4175 DHD_ERROR(("%s: unexpected NAKHANDLED!\n", __func__));
4176
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004177 bus->rxskip = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004178 intstatus |= I_HMB_FRAME_IND;
4179 }
4180
4181 /*
4182 * DEVREADY does not occur with gSPI.
4183 */
4184 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
4185 bus->sdpcm_ver =
4186 (hmb_data & HMB_DATA_VERSION_MASK) >>
4187 HMB_DATA_VERSION_SHIFT;
4188 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
4189 DHD_ERROR(("Version mismatch, dongle reports %d, "
4190 "expecting %d\n",
4191 bus->sdpcm_ver, SDPCM_PROT_VERSION));
4192 else
4193 DHD_INFO(("Dongle ready, protocol version %d\n",
4194 bus->sdpcm_ver));
4195 }
4196
4197 /*
4198 * Flow Control has been moved into the RX headers and this out of band
Grant Grundler4b455e02011-05-04 09:59:47 -07004199 * method isn't used any more.
4200 * remaining backward compatible with older dongles.
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004201 */
4202 if (hmb_data & HMB_DATA_FC) {
Grant Grundler4b455e02011-05-04 09:59:47 -07004203 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
4204 HMB_DATA_FCDATA_SHIFT;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004205
4206 if (fcbits & ~bus->flowcontrol)
4207 bus->fc_xoff++;
Grant Grundler4b455e02011-05-04 09:59:47 -07004208
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004209 if (bus->flowcontrol & ~fcbits)
4210 bus->fc_xon++;
4211
4212 bus->fc_rcvd++;
4213 bus->flowcontrol = fcbits;
4214 }
4215
4216 /* Shouldn't be any others */
4217 if (hmb_data & ~(HMB_DATA_DEVREADY |
4218 HMB_DATA_NAKHANDLED |
4219 HMB_DATA_FC |
4220 HMB_DATA_FWREADY |
4221 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK)) {
4222 DHD_ERROR(("Unknown mailbox data content: 0x%02x\n", hmb_data));
4223 }
4224
4225 return intstatus;
4226}
4227
4228bool dhdsdio_dpc(dhd_bus_t *bus)
4229{
4230 bcmsdh_info_t *sdh = bus->sdh;
4231 sdpcmd_regs_t *regs = bus->regs;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004232 u32 intstatus, newstatus = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004233 uint retries = 0;
4234 uint rxlimit = dhd_rxbound; /* Rx frames to read before resched */
4235 uint txlimit = dhd_txbound; /* Tx frames to send before resched */
4236 uint framecnt = 0; /* Temporary counter of tx/rx frames */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004237 bool rxdone = true; /* Flag for no more read data */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004238 bool resched = false; /* Flag indicating resched wanted */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004239
4240 DHD_TRACE(("%s: Enter\n", __func__));
4241
4242 /* Start with leftover status bits */
4243 intstatus = bus->intstatus;
4244
4245 dhd_os_sdlock(bus->dhd);
4246
4247 /* If waiting for HTAVAIL, check status */
4248 if (bus->clkstate == CLK_PENDING) {
4249 int err;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004250 u8 clkctl, devctl = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004251
4252#ifdef DHD_DEBUG
4253 /* Check for inconsistent device control */
4254 devctl =
4255 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, &err);
4256 if (err) {
4257 DHD_ERROR(("%s: error reading DEVCTL: %d\n",
4258 __func__, err));
4259 bus->dhd->busstate = DHD_BUS_DOWN;
4260 } else {
4261 ASSERT(devctl & SBSDIO_DEVCTL_CA_INT_ONLY);
4262 }
4263#endif /* DHD_DEBUG */
4264
4265 /* Read CSR, if clock on switch to AVAIL, else ignore */
4266 clkctl =
4267 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
4268 &err);
4269 if (err) {
4270 DHD_ERROR(("%s: error reading CSR: %d\n", __func__,
4271 err));
4272 bus->dhd->busstate = DHD_BUS_DOWN;
4273 }
4274
4275 DHD_INFO(("DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n", devctl,
4276 clkctl));
4277
4278 if (SBSDIO_HTAV(clkctl)) {
4279 devctl =
4280 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
4281 &err);
4282 if (err) {
4283 DHD_ERROR(("%s: error reading DEVCTL: %d\n",
4284 __func__, err));
4285 bus->dhd->busstate = DHD_BUS_DOWN;
4286 }
4287 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
4288 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
4289 devctl, &err);
4290 if (err) {
4291 DHD_ERROR(("%s: error writing DEVCTL: %d\n",
4292 __func__, err));
4293 bus->dhd->busstate = DHD_BUS_DOWN;
4294 }
4295 bus->clkstate = CLK_AVAIL;
4296 } else {
4297 goto clkwait;
4298 }
4299 }
4300
4301 BUS_WAKE(bus);
4302
4303 /* Make sure backplane clock is on */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004304 dhdsdio_clkctl(bus, CLK_AVAIL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004305 if (bus->clkstate == CLK_PENDING)
4306 goto clkwait;
4307
4308 /* Pending interrupt indicates new device status */
4309 if (bus->ipend) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004310 bus->ipend = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004311 R_SDREG(newstatus, &regs->intstatus, retries);
4312 bus->f1regdata++;
4313 if (bcmsdh_regfail(bus->sdh))
4314 newstatus = 0;
4315 newstatus &= bus->hostintmask;
4316 bus->fcstate = !!(newstatus & I_HMB_FC_STATE);
4317 if (newstatus) {
4318 W_SDREG(newstatus, &regs->intstatus, retries);
4319 bus->f1regdata++;
4320 }
4321 }
4322
4323 /* Merge new bits with previous */
4324 intstatus |= newstatus;
4325 bus->intstatus = 0;
4326
4327 /* Handle flow-control change: read new state in case our ack
4328 * crossed another change interrupt. If change still set, assume
4329 * FC ON for safety, let next loop through do the debounce.
4330 */
4331 if (intstatus & I_HMB_FC_CHANGE) {
4332 intstatus &= ~I_HMB_FC_CHANGE;
4333 W_SDREG(I_HMB_FC_CHANGE, &regs->intstatus, retries);
4334 R_SDREG(newstatus, &regs->intstatus, retries);
4335 bus->f1regdata += 2;
4336 bus->fcstate =
4337 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE));
4338 intstatus |= (newstatus & bus->hostintmask);
4339 }
4340
4341 /* Handle host mailbox indication */
4342 if (intstatus & I_HMB_HOST_INT) {
4343 intstatus &= ~I_HMB_HOST_INT;
4344 intstatus |= dhdsdio_hostmail(bus);
4345 }
4346
4347 /* Generally don't ask for these, can get CRC errors... */
4348 if (intstatus & I_WR_OOSYNC) {
4349 DHD_ERROR(("Dongle reports WR_OOSYNC\n"));
4350 intstatus &= ~I_WR_OOSYNC;
4351 }
4352
4353 if (intstatus & I_RD_OOSYNC) {
4354 DHD_ERROR(("Dongle reports RD_OOSYNC\n"));
4355 intstatus &= ~I_RD_OOSYNC;
4356 }
4357
4358 if (intstatus & I_SBINT) {
4359 DHD_ERROR(("Dongle reports SBINT\n"));
4360 intstatus &= ~I_SBINT;
4361 }
4362
4363 /* Would be active due to wake-wlan in gSPI */
4364 if (intstatus & I_CHIPACTIVE) {
4365 DHD_INFO(("Dongle reports CHIPACTIVE\n"));
4366 intstatus &= ~I_CHIPACTIVE;
4367 }
4368
4369 /* Ignore frame indications if rxskip is set */
4370 if (bus->rxskip)
4371 intstatus &= ~I_HMB_FRAME_IND;
4372
4373 /* On frame indication, read available frames */
4374 if (PKT_AVAILABLE()) {
4375 framecnt = dhdsdio_readframes(bus, rxlimit, &rxdone);
4376 if (rxdone || bus->rxskip)
4377 intstatus &= ~I_HMB_FRAME_IND;
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07004378 rxlimit -= min(framecnt, rxlimit);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004379 }
4380
4381 /* Keep still-pending events for next scheduling */
4382 bus->intstatus = intstatus;
4383
4384clkwait:
4385#if defined(OOB_INTR_ONLY)
4386 bcmsdh_oob_intr_set(1);
4387#endif /* (OOB_INTR_ONLY) */
4388 /* Re-enable interrupts to detect new device events (mailbox, rx frame)
4389 * or clock availability. (Allows tx loop to check ipend if desired.)
4390 * (Unless register access seems hosed, as we may not be able to ACK...)
4391 */
4392 if (bus->intr && bus->intdis && !bcmsdh_regfail(sdh)) {
4393 DHD_INTR(("%s: enable SDIO interrupts, rxdone %d framecnt %d\n",
4394 __func__, rxdone, framecnt));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004395 bus->intdis = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004396 bcmsdh_intr_enable(sdh);
4397 }
4398
4399 if (DATAOK(bus) && bus->ctrl_frame_stat &&
4400 (bus->clkstate == CLK_AVAIL)) {
4401 int ret, i;
4402
4403 ret =
4404 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004405 F2SYNC, (u8 *) bus->ctrl_frame_buf,
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004406 (u32) bus->ctrl_frame_len, NULL,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004407 NULL, NULL);
Roland Vossenb7ef2a92011-05-03 11:35:02 +02004408 ASSERT(ret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004409
4410 if (ret < 0) {
4411 /* On failure, abort the command and
4412 terminate the frame */
4413 DHD_INFO(("%s: sdio error %d, abort command and "
4414 "terminate frame.\n", __func__, ret));
4415 bus->tx_sderrs++;
4416
4417 bcmsdh_abort(sdh, SDIO_FUNC_2);
4418
4419 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
4420 SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
4421 NULL);
4422 bus->f1regdata++;
4423
4424 for (i = 0; i < 3; i++) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004425 u8 hi, lo;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004426 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
4427 SBSDIO_FUNC1_WFRAMEBCHI,
4428 NULL);
4429 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
4430 SBSDIO_FUNC1_WFRAMEBCLO,
4431 NULL);
4432 bus->f1regdata += 2;
4433 if ((hi == 0) && (lo == 0))
4434 break;
4435 }
4436
4437 }
4438 if (ret == 0)
4439 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
4440
Arend van Spriel0bef7742011-02-10 12:03:44 +01004441 DHD_INFO(("Return_dpc value is : %d\n", ret));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004442 bus->ctrl_frame_stat = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004443 dhd_wait_event_wakeup(bus->dhd);
4444 }
4445 /* Send queued frames (limit 1 if rx may still be pending) */
4446 else if ((bus->clkstate == CLK_AVAIL) && !bus->fcstate &&
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004447 bcm_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004448 && DATAOK(bus)) {
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07004449 framecnt = rxdone ? txlimit : min(txlimit, dhd_txminmax);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004450 framecnt = dhdsdio_sendfromq(bus, framecnt);
4451 txlimit -= framecnt;
4452 }
4453
4454 /* Resched if events or tx frames are pending,
4455 else await next interrupt */
4456 /* On failed register access, all bets are off:
4457 no resched or interrupts */
4458 if ((bus->dhd->busstate == DHD_BUS_DOWN) || bcmsdh_regfail(sdh)) {
4459 DHD_ERROR(("%s: failed backplane access over SDIO, halting "
4460 "operation %d\n", __func__, bcmsdh_regfail(sdh)));
4461 bus->dhd->busstate = DHD_BUS_DOWN;
4462 bus->intstatus = 0;
4463 } else if (bus->clkstate == CLK_PENDING) {
4464 DHD_INFO(("%s: rescheduled due to CLK_PENDING awaiting "
4465 "I_CHIPACTIVE interrupt\n", __func__));
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004466 resched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004467 } else if (bus->intstatus || bus->ipend ||
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004468 (!bus->fcstate && bcm_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004469 DATAOK(bus)) || PKT_AVAILABLE()) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004470 resched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004471 }
4472
4473 bus->dpc_sched = resched;
4474
4475 /* If we're done for now, turn off clock request. */
4476 if ((bus->clkstate != CLK_PENDING)
4477 && bus->idletime == DHD_IDLE_IMMEDIATE) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004478 bus->activity = false;
4479 dhdsdio_clkctl(bus, CLK_NONE, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004480 }
4481
4482 dhd_os_sdunlock(bus->dhd);
4483
4484 return resched;
4485}
4486
4487bool dhd_bus_dpc(struct dhd_bus *bus)
4488{
4489 bool resched;
4490
4491 /* Call the DPC directly. */
4492 DHD_TRACE(("Calling dhdsdio_dpc() from %s\n", __func__));
4493 resched = dhdsdio_dpc(bus);
4494
4495 return resched;
4496}
4497
4498void dhdsdio_isr(void *arg)
4499{
4500 dhd_bus_t *bus = (dhd_bus_t *) arg;
4501 bcmsdh_info_t *sdh;
4502
4503 DHD_TRACE(("%s: Enter\n", __func__));
4504
4505 if (!bus) {
4506 DHD_ERROR(("%s : bus is null pointer , exit\n", __func__));
4507 return;
4508 }
4509 sdh = bus->sdh;
4510
4511 if (bus->dhd->busstate == DHD_BUS_DOWN) {
4512 DHD_ERROR(("%s : bus is down. we have nothing to do\n",
4513 __func__));
4514 return;
4515 }
4516 /* Count the interrupt call */
4517 bus->intrcount++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004518 bus->ipend = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004519
4520 /* Shouldn't get this interrupt if we're sleeping? */
4521 if (bus->sleeping) {
4522 DHD_ERROR(("INTERRUPT WHILE SLEEPING??\n"));
4523 return;
4524 }
4525
4526 /* Disable additional interrupts (is this needed now)? */
4527 if (bus->intr)
4528 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
4529 else
4530 DHD_ERROR(("dhdsdio_isr() w/o interrupt configured!\n"));
4531
4532 bcmsdh_intr_disable(sdh);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004533 bus->intdis = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004534
4535#if defined(SDIO_ISR_THREAD)
4536 DHD_TRACE(("Calling dhdsdio_dpc() from %s\n", __func__));
4537 while (dhdsdio_dpc(bus))
4538 ;
4539#else
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004540 bus->dpc_sched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004541 dhd_sched_dpc(bus->dhd);
4542#endif
4543
4544}
4545
4546#ifdef SDTEST
4547static void dhdsdio_pktgen_init(dhd_bus_t *bus)
4548{
4549 /* Default to specified length, or full range */
4550 if (dhd_pktgen_len) {
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07004551 bus->pktgen_maxlen = min(dhd_pktgen_len, MAX_PKTGEN_LEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004552 bus->pktgen_minlen = bus->pktgen_maxlen;
4553 } else {
4554 bus->pktgen_maxlen = MAX_PKTGEN_LEN;
4555 bus->pktgen_minlen = 0;
4556 }
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004557 bus->pktgen_len = (u16) bus->pktgen_minlen;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004558
4559 /* Default to per-watchdog burst with 10s print time */
4560 bus->pktgen_freq = 1;
4561 bus->pktgen_print = 10000 / dhd_watchdog_ms;
4562 bus->pktgen_count = (dhd_pktgen * dhd_watchdog_ms + 999) / 1000;
4563
4564 /* Default to echo mode */
4565 bus->pktgen_mode = DHD_PKTGEN_ECHO;
4566 bus->pktgen_stop = 1;
4567}
4568
4569static void dhdsdio_pktgen(dhd_bus_t *bus)
4570{
Arend van Sprielc26b1372010-11-23 14:06:23 +01004571 struct sk_buff *pkt;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004572 u8 *data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004573 uint pktcount;
4574 uint fillbyte;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004575 u16 len;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004576
4577 /* Display current count if appropriate */
4578 if (bus->pktgen_print && (++bus->pktgen_ptick >= bus->pktgen_print)) {
4579 bus->pktgen_ptick = 0;
Arend van Spriel0bef7742011-02-10 12:03:44 +01004580 printk(KERN_DEBUG "%s: send attempts %d rcvd %d\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004581 __func__, bus->pktgen_sent, bus->pktgen_rcvd);
4582 }
4583
4584 /* For recv mode, just make sure dongle has started sending */
4585 if (bus->pktgen_mode == DHD_PKTGEN_RECV) {
4586 if (!bus->pktgen_rcvd)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004587 dhdsdio_sdtest_set(bus, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004588 return;
4589 }
4590
4591 /* Otherwise, generate or request the specified number of packets */
4592 for (pktcount = 0; pktcount < bus->pktgen_count; pktcount++) {
4593 /* Stop if total has been reached */
4594 if (bus->pktgen_total
4595 && (bus->pktgen_sent >= bus->pktgen_total)) {
4596 bus->pktgen_count = 0;
4597 break;
4598 }
4599
4600 /* Allocate an appropriate-sized packet */
4601 len = bus->pktgen_len;
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004602 pkt = bcm_pkt_buf_get_skb(
Jason Cooper9b890322010-09-30 15:15:39 -04004603 (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + DHD_SDALIGN),
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004604 true);
Jason Cooper9b890322010-09-30 15:15:39 -04004605 if (!pkt) {
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004606 DHD_ERROR(("%s: bcm_pkt_buf_get_skb failed!\n",
4607 __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004608 break;
4609 }
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01004610 PKTALIGN(pkt, (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004611 DHD_SDALIGN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01004612 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004613
4614 /* Write test header cmd and extra based on mode */
4615 switch (bus->pktgen_mode) {
4616 case DHD_PKTGEN_ECHO:
4617 *data++ = SDPCM_TEST_ECHOREQ;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004618 *data++ = (u8) bus->pktgen_sent;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004619 break;
4620
4621 case DHD_PKTGEN_SEND:
4622 *data++ = SDPCM_TEST_DISCARD;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004623 *data++ = (u8) bus->pktgen_sent;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004624 break;
4625
4626 case DHD_PKTGEN_RXBURST:
4627 *data++ = SDPCM_TEST_BURST;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004628 *data++ = (u8) bus->pktgen_count;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004629 break;
4630
4631 default:
4632 DHD_ERROR(("Unrecognized pktgen mode %d\n",
4633 bus->pktgen_mode));
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004634 bcm_pkt_buf_free_skb(pkt, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004635 bus->pktgen_count = 0;
4636 return;
4637 }
4638
4639 /* Write test header length field */
4640 *data++ = (len >> 0);
4641 *data++ = (len >> 8);
4642
4643 /* Then fill in the remainder -- N/A for burst,
4644 but who cares... */
4645 for (fillbyte = 0; fillbyte < len; fillbyte++)
4646 *data++ =
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004647 SDPCM_TEST_FILL(fillbyte, (u8) bus->pktgen_sent);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004648
4649#ifdef DHD_DEBUG
4650 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
Arend van Spriel54991ad2010-11-23 14:06:24 +01004651 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
Arend van Spriel34227312011-05-10 22:25:32 +02004652 printk(KERN_DEBUG "dhdsdio_pktgen: Tx Data:\n");
4653 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, data,
4654 pkt->len - SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004655 }
4656#endif
4657
4658 /* Send it */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004659 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004660 bus->pktgen_fail++;
4661 if (bus->pktgen_stop
4662 && bus->pktgen_stop == bus->pktgen_fail)
4663 bus->pktgen_count = 0;
4664 }
4665 bus->pktgen_sent++;
4666
4667 /* Bump length if not fixed, wrap at max */
4668 if (++bus->pktgen_len > bus->pktgen_maxlen)
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004669 bus->pktgen_len = (u16) bus->pktgen_minlen;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004670
4671 /* Special case for burst mode: just send one request! */
4672 if (bus->pktgen_mode == DHD_PKTGEN_RXBURST)
4673 break;
4674 }
4675}
4676
4677static void dhdsdio_sdtest_set(dhd_bus_t *bus, bool start)
4678{
Arend van Sprielc26b1372010-11-23 14:06:23 +01004679 struct sk_buff *pkt;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004680 u8 *data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004681
4682 /* Allocate the packet */
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004683 pkt = bcm_pkt_buf_get_skb(SDPCM_HDRLEN + SDPCM_TEST_HDRLEN +
4684 DHD_SDALIGN, true);
Jason Cooper9b890322010-09-30 15:15:39 -04004685 if (!pkt) {
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004686 DHD_ERROR(("%s: bcm_pkt_buf_get_skb failed!\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004687 return;
4688 }
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01004689 PKTALIGN(pkt, (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN), DHD_SDALIGN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01004690 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004691
4692 /* Fill in the test header */
4693 *data++ = SDPCM_TEST_SEND;
4694 *data++ = start;
4695 *data++ = (bus->pktgen_maxlen >> 0);
4696 *data++ = (bus->pktgen_maxlen >> 8);
4697
4698 /* Send it */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004699 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004700 bus->pktgen_fail++;
4701}
4702
Arend van Sprielc26b1372010-11-23 14:06:23 +01004703static void dhdsdio_testrcv(dhd_bus_t *bus, struct sk_buff *pkt, uint seq)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004704{
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004705 u8 *data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004706 uint pktlen;
4707
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004708 u8 cmd;
4709 u8 extra;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004710 u16 len;
4711 u16 offset;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004712
4713 /* Check for min length */
Arend van Spriel54991ad2010-11-23 14:06:24 +01004714 pktlen = pkt->len;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004715 if (pktlen < SDPCM_TEST_HDRLEN) {
4716 DHD_ERROR(("dhdsdio_restrcv: toss runt frame, pktlen %d\n",
4717 pktlen));
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004718 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004719 return;
4720 }
4721
4722 /* Extract header fields */
Arend van Spriel54991ad2010-11-23 14:06:24 +01004723 data = pkt->data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004724 cmd = *data++;
4725 extra = *data++;
4726 len = *data++;
4727 len += *data++ << 8;
4728
4729 /* Check length for relevant commands */
4730 if (cmd == SDPCM_TEST_DISCARD || cmd == SDPCM_TEST_ECHOREQ
4731 || cmd == SDPCM_TEST_ECHORSP) {
4732 if (pktlen != len + SDPCM_TEST_HDRLEN) {
4733 DHD_ERROR(("dhdsdio_testrcv: frame length mismatch, "
4734 "pktlen %d seq %d" " cmd %d extra %d len %d\n",
4735 pktlen, seq, cmd, extra, len));
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004736 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004737 return;
4738 }
4739 }
4740
4741 /* Process as per command */
4742 switch (cmd) {
4743 case SDPCM_TEST_ECHOREQ:
4744 /* Rx->Tx turnaround ok (even on NDIS w/current
4745 implementation) */
Arend van Spriel54991ad2010-11-23 14:06:24 +01004746 *(u8 *) (pkt->data) = SDPCM_TEST_ECHORSP;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004747 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true) == 0) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004748 bus->pktgen_sent++;
4749 } else {
4750 bus->pktgen_fail++;
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004751 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004752 }
4753 bus->pktgen_rcvd++;
4754 break;
4755
4756 case SDPCM_TEST_ECHORSP:
4757 if (bus->ext_loop) {
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004758 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004759 bus->pktgen_rcvd++;
4760 break;
4761 }
4762
4763 for (offset = 0; offset < len; offset++, data++) {
4764 if (*data != SDPCM_TEST_FILL(offset, extra)) {
4765 DHD_ERROR(("dhdsdio_testrcv: echo data mismatch: " "offset %d (len %d) expect 0x%02x rcvd 0x%02x\n",
4766 offset, len,
4767 SDPCM_TEST_FILL(offset, extra), *data));
4768 break;
4769 }
4770 }
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004771 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004772 bus->pktgen_rcvd++;
4773 break;
4774
4775 case SDPCM_TEST_DISCARD:
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004776 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004777 bus->pktgen_rcvd++;
4778 break;
4779
4780 case SDPCM_TEST_BURST:
4781 case SDPCM_TEST_SEND:
4782 default:
4783 DHD_INFO(("dhdsdio_testrcv: unsupported or unknown command, "
4784 "pktlen %d seq %d" " cmd %d extra %d len %d\n",
4785 pktlen, seq, cmd, extra, len));
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004786 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004787 break;
4788 }
4789
4790 /* For recv mode, stop at limie (and tell dongle to stop sending) */
4791 if (bus->pktgen_mode == DHD_PKTGEN_RECV) {
4792 if (bus->pktgen_total
4793 && (bus->pktgen_rcvd >= bus->pktgen_total)) {
4794 bus->pktgen_count = 0;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004795 dhdsdio_sdtest_set(bus, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004796 }
4797 }
4798}
4799#endif /* SDTEST */
4800
4801extern bool dhd_bus_watchdog(dhd_pub_t *dhdp)
4802{
4803 dhd_bus_t *bus;
4804
4805 DHD_TIMER(("%s: Enter\n", __func__));
4806
4807 bus = dhdp->bus;
4808
4809 if (bus->dhd->dongle_reset)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004810 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004811
4812 /* Ignore the timer if simulating bus down */
4813 if (bus->sleeping)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004814 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004815
4816 dhd_os_sdlock(bus->dhd);
4817
4818 /* Poll period: check device if appropriate. */
4819 if (bus->poll && (++bus->polltick >= bus->pollrate)) {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004820 u32 intstatus = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004821
4822 /* Reset poll tick */
4823 bus->polltick = 0;
4824
4825 /* Check device if no interrupts */
4826 if (!bus->intr || (bus->intrcount == bus->lastintrs)) {
4827
4828 if (!bus->dpc_sched) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004829 u8 devpend;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004830 devpend = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_0,
4831 SDIOD_CCCR_INTPEND,
4832 NULL);
4833 intstatus =
4834 devpend & (INTR_STATUS_FUNC1 |
4835 INTR_STATUS_FUNC2);
4836 }
4837
4838 /* If there is something, make like the ISR and
4839 schedule the DPC */
4840 if (intstatus) {
4841 bus->pollcnt++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004842 bus->ipend = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004843 if (bus->intr)
4844 bcmsdh_intr_disable(bus->sdh);
4845
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004846 bus->dpc_sched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004847 dhd_sched_dpc(bus->dhd);
4848
4849 }
4850 }
4851
4852 /* Update interrupt tracking */
4853 bus->lastintrs = bus->intrcount;
4854 }
4855#ifdef DHD_DEBUG
4856 /* Poll for console output periodically */
4857 if (dhdp->busstate == DHD_BUS_DATA && dhd_console_ms != 0) {
4858 bus->console.count += dhd_watchdog_ms;
4859 if (bus->console.count >= dhd_console_ms) {
4860 bus->console.count -= dhd_console_ms;
4861 /* Make sure backplane clock is on */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004862 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004863 if (dhdsdio_readconsole(bus) < 0)
4864 dhd_console_ms = 0; /* On error,
4865 stop trying */
4866 }
4867 }
4868#endif /* DHD_DEBUG */
4869
4870#ifdef SDTEST
4871 /* Generate packets if configured */
4872 if (bus->pktgen_count && (++bus->pktgen_tick >= bus->pktgen_freq)) {
4873 /* Make sure backplane clock is on */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004874 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004875 bus->pktgen_tick = 0;
4876 dhdsdio_pktgen(bus);
4877 }
4878#endif
4879
4880 /* On idle timeout clear activity flag and/or turn off clock */
4881 if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
4882 if (++bus->idlecount >= bus->idletime) {
4883 bus->idlecount = 0;
4884 if (bus->activity) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004885 bus->activity = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004886 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
4887 } else {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004888 dhdsdio_clkctl(bus, CLK_NONE, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004889 }
4890 }
4891 }
4892
4893 dhd_os_sdunlock(bus->dhd);
4894
4895 return bus->ipend;
4896}
4897
4898#ifdef DHD_DEBUG
Greg Kroah-Hartman580a0bd2010-10-05 11:09:48 -07004899extern int dhd_bus_console_in(dhd_pub_t *dhdp, unsigned char *msg, uint msglen)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004900{
4901 dhd_bus_t *bus = dhdp->bus;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004902 u32 addr, val;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004903 int rv;
Arend van Sprielc26b1372010-11-23 14:06:23 +01004904 struct sk_buff *pkt;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004905
4906 /* Address could be zero if CONSOLE := 0 in dongle Makefile */
4907 if (bus->console_addr == 0)
Roland Vossene10d82d2011-05-03 11:35:19 +02004908 return -ENOTSUPP;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004909
4910 /* Exclusive bus access */
4911 dhd_os_sdlock(bus->dhd);
4912
4913 /* Don't allow input if dongle is in reset */
4914 if (bus->dhd->dongle_reset) {
4915 dhd_os_sdunlock(bus->dhd);
Roland Vossenb74ac122011-05-03 11:35:20 +02004916 return -EPERM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004917 }
4918
4919 /* Request clock to allow SDIO accesses */
4920 BUS_WAKE(bus);
4921 /* No pend allowed since txpkt is called later, ht clk has to be on */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004922 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004923
4924 /* Zero cbuf_index */
Roland Vossen70963f92011-06-01 13:45:08 +02004925 addr = bus->console_addr + offsetof(rte_cons_t, cbuf_idx);
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03004926 val = cpu_to_le32(0);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004927 rv = dhdsdio_membytes(bus, true, addr, (u8 *)&val, sizeof(val));
Jason Cooper9b890322010-09-30 15:15:39 -04004928 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004929 goto done;
4930
4931 /* Write message into cbuf */
Roland Vossen70963f92011-06-01 13:45:08 +02004932 addr = bus->console_addr + offsetof(rte_cons_t, cbuf);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004933 rv = dhdsdio_membytes(bus, true, addr, (u8 *)msg, msglen);
Jason Cooper9b890322010-09-30 15:15:39 -04004934 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004935 goto done;
4936
4937 /* Write length into vcons_in */
Roland Vossen70963f92011-06-01 13:45:08 +02004938 addr = bus->console_addr + offsetof(rte_cons_t, vcons_in);
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03004939 val = cpu_to_le32(msglen);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004940 rv = dhdsdio_membytes(bus, true, addr, (u8 *)&val, sizeof(val));
Jason Cooper9b890322010-09-30 15:15:39 -04004941 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004942 goto done;
4943
4944 /* Bump dongle by sending an empty event pkt.
4945 * sdpcm_sendup (RX) checks for virtual console input.
4946 */
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004947 pkt = bcm_pkt_buf_get_skb(4 + SDPCM_RESERVE);
Jason Cooper9b890322010-09-30 15:15:39 -04004948 if ((pkt != NULL) && bus->clkstate == CLK_AVAIL)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004949 dhdsdio_txpkt(bus, pkt, SDPCM_EVENT_CHANNEL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004950
4951done:
4952 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004953 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004954 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004955 }
4956
4957 dhd_os_sdunlock(bus->dhd);
4958
4959 return rv;
4960}
4961#endif /* DHD_DEBUG */
4962
4963#ifdef DHD_DEBUG
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004964static void dhd_dump_cis(uint fn, u8 *cis)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004965{
4966 uint byte, tag, tdata;
4967 DHD_INFO(("Function %d CIS:\n", fn));
4968
4969 for (tdata = byte = 0; byte < SBSDIO_CIS_SIZE_LIMIT; byte++) {
4970 if ((byte % 16) == 0)
4971 DHD_INFO((" "));
4972 DHD_INFO(("%02x ", cis[byte]));
4973 if ((byte % 16) == 15)
4974 DHD_INFO(("\n"));
4975 if (!tdata--) {
4976 tag = cis[byte];
4977 if (tag == 0xff)
4978 break;
4979 else if (!tag)
4980 tdata = 0;
4981 else if ((byte + 1) < SBSDIO_CIS_SIZE_LIMIT)
4982 tdata = cis[byte + 1] + 1;
4983 else
4984 DHD_INFO(("]"));
4985 }
4986 }
4987 if ((byte % 16) != 15)
4988 DHD_INFO(("\n"));
4989}
4990#endif /* DHD_DEBUG */
4991
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004992static bool dhdsdio_chipmatch(u16 chipid)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004993{
4994 if (chipid == BCM4325_CHIP_ID)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004995 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004996 if (chipid == BCM4329_CHIP_ID)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004997 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004998 if (chipid == BCM4319_CHIP_ID)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004999 return true;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005000 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005001}
5002
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07005003static void *dhdsdio_probe(u16 venid, u16 devid, u16 bus_no,
5004 u16 slot, u16 func, uint bustype, void *regsva,
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005005 void *sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005006{
5007 int ret;
5008 dhd_bus_t *bus;
5009
5010 /* Init global variables at run-time, not as part of the declaration.
5011 * This is required to support init/de-init of the driver.
5012 * Initialization
5013 * of globals as part of the declaration results in non-deterministic
5014 * behavior since the value of the globals may be different on the
5015 * first time that the driver is initialized vs subsequent
5016 * initializations.
5017 */
5018 dhd_txbound = DHD_TXBOUND;
5019 dhd_rxbound = DHD_RXBOUND;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005020 dhd_alignctl = true;
5021 sd1idle = true;
5022 dhd_readahead = true;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005023 retrydata = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005024 dhd_dongle_memsize = 0;
5025 dhd_txminmax = DHD_TXMINMAX;
5026
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005027 forcealign = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005028
5029 dhd_common_init();
5030
5031 DHD_TRACE(("%s: Enter\n", __func__));
5032 DHD_INFO(("%s: venid 0x%04x devid 0x%04x\n", __func__, venid, devid));
5033
5034 /* We make assumptions about address window mappings */
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005035 ASSERT((unsigned long)regsva == SI_ENUM_BASE);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005036
5037 /* BCMSDH passes venid and devid based on CIS parsing -- but
5038 * low-power start
5039 * means early parse could fail, so here we should get either an ID
5040 * we recognize OR (-1) indicating we must request power first.
5041 */
5042 /* Check the Vendor ID */
5043 switch (venid) {
5044 case 0x0000:
Stanislav Fomichevbe1c09f2011-03-28 01:31:36 +04005045 case PCI_VENDOR_ID_BROADCOM:
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005046 break;
5047 default:
5048 DHD_ERROR(("%s: unknown vendor: 0x%04x\n", __func__, venid));
5049 return NULL;
5050 }
5051
5052 /* Check the Device ID and make sure it's one that we support */
5053 switch (devid) {
5054 case BCM4325_D11DUAL_ID: /* 4325 802.11a/g id */
5055 case BCM4325_D11G_ID: /* 4325 802.11g 2.4Ghz band id */
5056 case BCM4325_D11A_ID: /* 4325 802.11a 5Ghz band id */
5057 DHD_INFO(("%s: found 4325 Dongle\n", __func__));
5058 break;
5059 case BCM4329_D11NDUAL_ID: /* 4329 802.11n dualband device */
5060 case BCM4329_D11N2G_ID: /* 4329 802.11n 2.4G device */
5061 case BCM4329_D11N5G_ID: /* 4329 802.11n 5G device */
5062 case 0x4329:
5063 DHD_INFO(("%s: found 4329 Dongle\n", __func__));
5064 break;
5065 case BCM4319_D11N_ID: /* 4319 802.11n id */
5066 case BCM4319_D11N2G_ID: /* 4319 802.11n2g id */
5067 case BCM4319_D11N5G_ID: /* 4319 802.11n5g id */
5068 DHD_INFO(("%s: found 4319 Dongle\n", __func__));
5069 break;
5070 case 0:
5071 DHD_INFO(("%s: allow device id 0, will check chip internals\n",
5072 __func__));
5073 break;
5074
5075 default:
5076 DHD_ERROR(("%s: skipping 0x%04x/0x%04x, not a dongle\n",
5077 __func__, venid, devid));
5078 return NULL;
5079 }
5080
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005081 /* Allocate private bus interface state */
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005082 bus = kzalloc(sizeof(dhd_bus_t), GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005083 if (!bus) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005084 DHD_ERROR(("%s: kmalloc of dhd_bus_t failed\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005085 goto fail;
5086 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005087 bus->sdh = sdh;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07005088 bus->cl_devid = (u16) devid;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005089 bus->bus = DHD_BUS;
5090 bus->tx_seq = SDPCM_SEQUENCE_WRAP - 1;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005091 bus->usebufpool = false; /* Use bufpool if allocated,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005092 else use locally malloced rxbuf */
5093
5094 /* attempt to attach to the dongle */
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005095 if (!(dhdsdio_probe_attach(bus, sdh, regsva, devid))) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005096 DHD_ERROR(("%s: dhdsdio_probe_attach failed\n", __func__));
5097 goto fail;
5098 }
5099
5100 /* Attach to the dhd/OS/network interface */
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005101 bus->dhd = dhd_attach(bus, SDPCM_RESERVE);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005102 if (!bus->dhd) {
5103 DHD_ERROR(("%s: dhd_attach failed\n", __func__));
5104 goto fail;
5105 }
5106
5107 /* Allocate buffers */
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005108 if (!(dhdsdio_probe_malloc(bus, sdh))) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005109 DHD_ERROR(("%s: dhdsdio_probe_malloc failed\n", __func__));
5110 goto fail;
5111 }
5112
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005113 if (!(dhdsdio_probe_init(bus, sdh))) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005114 DHD_ERROR(("%s: dhdsdio_probe_init failed\n", __func__));
5115 goto fail;
5116 }
5117
5118 /* Register interrupt callback, but mask it (not operational yet). */
5119 DHD_INTR(("%s: disable SDIO interrupts (not interested yet)\n",
5120 __func__));
5121 bcmsdh_intr_disable(sdh);
5122 ret = bcmsdh_intr_reg(sdh, dhdsdio_isr, bus);
5123 if (ret != 0) {
5124 DHD_ERROR(("%s: FAILED: bcmsdh_intr_reg returned %d\n",
5125 __func__, ret));
5126 goto fail;
5127 }
5128 DHD_INTR(("%s: registered SDIO interrupt function ok\n", __func__));
5129
5130 DHD_INFO(("%s: completed!!\n", __func__));
5131
5132 /* if firmware path present try to download and bring up bus */
Jason Cooper9b890322010-09-30 15:15:39 -04005133 ret = dhd_bus_start(bus->dhd);
5134 if (ret != 0) {
Roland Vossene10d82d2011-05-03 11:35:19 +02005135 if (ret == -ENOLINK) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005136 DHD_ERROR(("%s: dongle is not responding\n", __func__));
5137 goto fail;
5138 }
5139 }
5140 /* Ok, have the per-port tell the stack we're open for business */
5141 if (dhd_net_attach(bus->dhd, 0) != 0) {
5142 DHD_ERROR(("%s: Net attach failed!!\n", __func__));
5143 goto fail;
5144 }
5145
5146 return bus;
5147
5148fail:
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005149 dhdsdio_release(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005150 return NULL;
5151}
5152
5153static bool
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005154dhdsdio_probe_attach(struct dhd_bus *bus, void *sdh, void *regsva, u16 devid)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005155{
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005156 u8 clkctl = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005157 int err = 0;
5158
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005159 bus->alp_only = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005160
5161 /* Return the window to backplane enumeration space for core access */
5162 if (dhdsdio_set_siaddr_window(bus, SI_ENUM_BASE))
5163 DHD_ERROR(("%s: FAILED to return to SI_ENUM_BASE\n", __func__));
5164
5165#ifdef DHD_DEBUG
Arend van Spriel0bef7742011-02-10 12:03:44 +01005166 printk(KERN_DEBUG "F1 signature read @0x18000000=0x%4x\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005167 bcmsdh_reg_read(bus->sdh, SI_ENUM_BASE, 4));
5168
5169#endif /* DHD_DEBUG */
5170
Franky Linc05df632011-04-25 19:34:07 -07005171 /*
5172 * Force PLL off until dhdsdio_chip_attach()
5173 * programs PLL control regs
5174 */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005175
5176 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5177 DHD_INIT_CLKCTL1, &err);
5178 if (!err)
5179 clkctl =
5180 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5181 &err);
5182
5183 if (err || ((clkctl & ~SBSDIO_AVBITS) != DHD_INIT_CLKCTL1)) {
5184 DHD_ERROR(("dhdsdio_probe: ChipClkCSR access: err %d wrote "
5185 "0x%02x read 0x%02x\n",
5186 err, DHD_INIT_CLKCTL1, clkctl));
5187 goto fail;
5188 }
5189#ifdef DHD_DEBUG
5190 if (DHD_INFO_ON()) {
5191 uint fn, numfn;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005192 u8 *cis[SDIOD_MAX_IOFUNCS];
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005193 int err = 0;
5194
5195 numfn = bcmsdh_query_iofnum(sdh);
5196 ASSERT(numfn <= SDIOD_MAX_IOFUNCS);
5197
5198 /* Make sure ALP is available before trying to read CIS */
5199 SPINWAIT(((clkctl = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
5200 SBSDIO_FUNC1_CHIPCLKCSR,
5201 NULL)),
5202 !SBSDIO_ALPAV(clkctl)), PMU_MAX_TRANSITION_DLY);
5203
5204 /* Now request ALP be put on the bus */
5205 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5206 DHD_INIT_CLKCTL2, &err);
mike.rapoport@gmail.com73831412010-10-13 00:09:07 +02005207 udelay(65);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005208
5209 for (fn = 0; fn <= numfn; fn++) {
Alexander Beregalov12d0eb42011-03-09 03:53:35 +03005210 cis[fn] = kzalloc(SBSDIO_CIS_SIZE_LIMIT, GFP_ATOMIC);
Jason Cooper9b890322010-09-30 15:15:39 -04005211 if (!cis[fn]) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005212 DHD_INFO(("dhdsdio_probe: fn %d cis malloc "
5213 "failed\n", fn));
5214 break;
5215 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005216
Jason Cooper9b890322010-09-30 15:15:39 -04005217 err = bcmsdh_cis_read(sdh, fn, cis[fn],
5218 SBSDIO_CIS_SIZE_LIMIT);
5219 if (err) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005220 DHD_INFO(("dhdsdio_probe: fn %d cis read "
5221 "err %d\n", fn, err));
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005222 kfree(cis[fn]);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005223 break;
5224 }
5225 dhd_dump_cis(fn, cis[fn]);
5226 }
5227
5228 while (fn-- > 0) {
5229 ASSERT(cis[fn]);
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005230 kfree(cis[fn]);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005231 }
5232
5233 if (err) {
5234 DHD_ERROR(("dhdsdio_probe: error read/parsing CIS\n"));
5235 goto fail;
5236 }
5237 }
5238#endif /* DHD_DEBUG */
5239
Franky Lincb63e4c2011-04-25 15:45:08 -07005240 if (dhdsdio_chip_attach(bus, regsva)) {
5241 DHD_ERROR(("%s: dhdsdio_chip_attach failed!\n", __func__));
5242 goto fail;
5243 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005244
Franky Linc05df632011-04-25 19:34:07 -07005245 bcmsdh_chipinfo(sdh, bus->ci->chip, bus->ci->chiprev);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005246
Franky Linc05df632011-04-25 19:34:07 -07005247 if (!dhdsdio_chipmatch((u16) bus->ci->chip)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005248 DHD_ERROR(("%s: unsupported chip: 0x%04x\n",
Franky Linc05df632011-04-25 19:34:07 -07005249 __func__, bus->ci->chip));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005250 goto fail;
5251 }
5252
Franky Lin5d0d7a92011-04-25 19:34:05 -07005253 dhdsdio_sdiod_drive_strength_init(bus, dhd_sdiod_drive_strength);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005254
5255 /* Get info on the ARM and SOCRAM cores... */
5256 if (!DHD_NOPMU(bus)) {
Franky Linc05df632011-04-25 19:34:07 -07005257 bus->armrev = SBCOREREV(bcmsdh_reg_read(bus->sdh,
5258 CORE_SB(bus->ci->armcorebase, sbidhigh), 4));
5259 bus->orig_ramsize = bus->ci->ramsize;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005260 if (!(bus->orig_ramsize)) {
5261 DHD_ERROR(("%s: failed to find SOCRAM memory!\n",
5262 __func__));
5263 goto fail;
5264 }
5265 bus->ramsize = bus->orig_ramsize;
5266 if (dhd_dongle_memsize)
5267 dhd_dongle_setmemsize(bus, dhd_dongle_memsize);
5268
5269 DHD_ERROR(("DHD: dongle ram size is set to %d(orig %d)\n",
5270 bus->ramsize, bus->orig_ramsize));
5271 }
5272
Franky Linc05df632011-04-25 19:34:07 -07005273 bus->regs = (void *)bus->ci->buscorebase;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005274
5275 /* Set core control so an SDIO reset does a backplane reset */
Arend van Sprielff31c542011-03-01 10:56:54 +01005276 OR_REG(&bus->regs->corecontrol, CC_BPRESEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005277
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02005278 bcm_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005279
5280 /* Locate an appropriately-aligned portion of hdrbuf */
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005281 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0], DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005282
5283 /* Set the poll and/or interrupt flags */
5284 bus->intr = (bool) dhd_intr;
Jason Cooper9b890322010-09-30 15:15:39 -04005285 bus->poll = (bool) dhd_poll;
5286 if (bus->poll)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005287 bus->pollrate = 1;
5288
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005289 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005290
5291fail:
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005292 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005293}
5294
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005295static bool dhdsdio_probe_malloc(dhd_bus_t *bus, void *sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005296{
5297 DHD_TRACE(("%s: Enter\n", __func__));
5298
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005299 if (bus->dhd->maxctl) {
5300 bus->rxblen =
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07005301 roundup((bus->dhd->maxctl + SDPCM_HDRLEN),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005302 ALIGNMENT) + DHD_SDALIGN;
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005303 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005304 if (!(bus->rxbuf)) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005305 DHD_ERROR(("%s: kmalloc of %d-byte rxbuf failed\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005306 __func__, bus->rxblen));
5307 goto fail;
5308 }
5309 }
5310
5311 /* Allocate buffer to receive glomed packet */
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005312 bus->databuf = kmalloc(MAX_DATA_BUF, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005313 if (!(bus->databuf)) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005314 DHD_ERROR(("%s: kmalloc of %d-byte databuf failed\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005315 __func__, MAX_DATA_BUF));
5316 /* release rxbuf which was already located as above */
5317 if (!bus->rxblen)
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005318 kfree(bus->rxbuf);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005319 goto fail;
5320 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005321
5322 /* Align the buffer */
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005323 if ((unsigned long)bus->databuf % DHD_SDALIGN)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005324 bus->dataptr =
5325 bus->databuf + (DHD_SDALIGN -
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005326 ((unsigned long)bus->databuf % DHD_SDALIGN));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005327 else
5328 bus->dataptr = bus->databuf;
5329
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005330 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005331
5332fail:
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005333 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005334}
5335
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005336static bool dhdsdio_probe_init(dhd_bus_t *bus, void *sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005337{
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07005338 s32 fnum;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005339
5340 DHD_TRACE(("%s: Enter\n", __func__));
5341
5342#ifdef SDTEST
5343 dhdsdio_pktgen_init(bus);
5344#endif /* SDTEST */
5345
5346 /* Disable F2 to clear any intermediate frame state on the dongle */
5347 bcmsdh_cfg_write(sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, SDIO_FUNC_ENABLE_1,
5348 NULL);
5349
5350 bus->dhd->busstate = DHD_BUS_DOWN;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005351 bus->sleeping = false;
5352 bus->rxflow = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005353 bus->prev_rxlim_hit = 0;
5354
5355 /* Done with backplane-dependent accesses, can drop clock... */
5356 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
5357
5358 /* ...and initialize clock/power states */
5359 bus->clkstate = CLK_SDONLY;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07005360 bus->idletime = (s32) dhd_idletime;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005361 bus->idleclock = DHD_IDLE_ACTIVE;
5362
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005363 /* Query the F2 block size, set roundup accordingly */
5364 fnum = 2;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07005365 if (bcmsdh_iovar_op(sdh, "sd_blocksize", &fnum, sizeof(s32),
Roland Vossena1c5ad82011-04-11 15:16:24 +02005366 &bus->blocksize, sizeof(s32), false) != 0) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005367 bus->blocksize = 0;
5368 DHD_ERROR(("%s: fail on %s get\n", __func__, "sd_blocksize"));
5369 } else {
5370 DHD_INFO(("%s: Initial value for %s is %d\n",
5371 __func__, "sd_blocksize", bus->blocksize));
5372 }
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07005373 bus->roundup = min(max_roundup, bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005374
5375 /* Query if bus module supports packet chaining,
5376 default to use if supported */
5377 if (bcmsdh_iovar_op(sdh, "sd_rxchain", NULL, 0,
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07005378 &bus->sd_rxchain, sizeof(s32),
Roland Vossena1c5ad82011-04-11 15:16:24 +02005379 false) != 0) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005380 bus->sd_rxchain = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005381 } else {
5382 DHD_INFO(("%s: bus module (through bcmsdh API) %s chaining\n",
5383 __func__,
5384 (bus->sd_rxchain ? "supports" : "does not support")));
5385 }
5386 bus->use_rxchain = (bool) bus->sd_rxchain;
5387
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005388 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005389}
5390
5391bool
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005392dhd_bus_download_firmware(struct dhd_bus *bus, char *fw_path, char *nv_path)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005393{
5394 bool ret;
5395 bus->fw_path = fw_path;
5396 bus->nv_path = nv_path;
5397
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005398 ret = dhdsdio_download_firmware(bus, bus->sdh);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005399
5400 return ret;
5401}
5402
5403static bool
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005404dhdsdio_download_firmware(struct dhd_bus *bus, void *sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005405{
5406 bool ret;
5407
5408 /* Download the firmware */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005409 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005410
5411 ret = _dhdsdio_download_firmware(bus) == 0;
5412
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005413 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005414
5415 return ret;
5416}
5417
5418/* Detach and free everything */
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005419static void dhdsdio_release(dhd_bus_t *bus)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005420{
5421 DHD_TRACE(("%s: Enter\n", __func__));
5422
5423 if (bus) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005424 /* De-register interrupt handler */
5425 bcmsdh_intr_disable(bus->sdh);
5426 bcmsdh_intr_dereg(bus->sdh);
5427
5428 if (bus->dhd) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005429 dhd_detach(bus->dhd);
Franky Lincee3cf42011-04-25 19:34:06 -07005430 dhdsdio_release_dongle(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005431 bus->dhd = NULL;
5432 }
5433
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005434 dhdsdio_release_malloc(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005435
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005436 kfree(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005437 }
5438
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005439 DHD_TRACE(("%s: Disconnected\n", __func__));
5440}
5441
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005442static void dhdsdio_release_malloc(dhd_bus_t *bus)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005443{
5444 DHD_TRACE(("%s: Enter\n", __func__));
5445
5446 if (bus->dhd && bus->dhd->dongle_reset)
5447 return;
5448
5449 if (bus->rxbuf) {
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005450 kfree(bus->rxbuf);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005451 bus->rxctl = bus->rxbuf = NULL;
5452 bus->rxlen = 0;
5453 }
5454
Ilia Mirkin46d994b2011-03-13 00:28:56 -05005455 kfree(bus->databuf);
5456 bus->databuf = NULL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005457}
5458
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005459static void dhdsdio_release_dongle(dhd_bus_t *bus)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005460{
5461 DHD_TRACE(("%s: Enter\n", __func__));
5462
5463 if (bus->dhd && bus->dhd->dongle_reset)
5464 return;
5465
Franky Linc05df632011-04-25 19:34:07 -07005466 if (bus->ci) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005467 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005468 dhdsdio_clkctl(bus, CLK_NONE, false);
Franky Lincee3cf42011-04-25 19:34:06 -07005469 dhdsdio_chip_detach(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005470 if (bus->vars && bus->varsz)
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005471 kfree(bus->vars);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005472 bus->vars = NULL;
5473 }
5474
5475 DHD_TRACE(("%s: Disconnected\n", __func__));
5476}
5477
5478static void dhdsdio_disconnect(void *ptr)
5479{
5480 dhd_bus_t *bus = (dhd_bus_t *)ptr;
5481
5482 DHD_TRACE(("%s: Enter\n", __func__));
5483
5484 if (bus) {
5485 ASSERT(bus->dhd);
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005486 dhdsdio_release(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005487 }
5488
5489 DHD_TRACE(("%s: Disconnected\n", __func__));
5490}
5491
5492/* Register/Unregister functions are called by the main DHD entry
5493 * point (e.g. module insertion) to link with the bus driver, in
5494 * order to look for or await the device.
5495 */
5496
5497static bcmsdh_driver_t dhd_sdio = {
5498 dhdsdio_probe,
5499 dhdsdio_disconnect
5500};
5501
5502int dhd_bus_register(void)
5503{
5504 DHD_TRACE(("%s: Enter\n", __func__));
5505
5506 return bcmsdh_register(&dhd_sdio);
5507}
5508
5509void dhd_bus_unregister(void)
5510{
5511 DHD_TRACE(("%s: Enter\n", __func__));
5512
5513 bcmsdh_unregister();
5514}
5515
5516#ifdef BCMEMBEDIMAGE
5517static int dhdsdio_download_code_array(struct dhd_bus *bus)
5518{
5519 int bcmerror = -1;
5520 int offset = 0;
5521
5522 DHD_INFO(("%s: download embedded firmware...\n", __func__));
5523
5524 /* Download image */
5525 while ((offset + MEMBLOCK) < sizeof(dlarray)) {
5526 bcmerror =
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005527 dhdsdio_membytes(bus, true, offset, dlarray + offset,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005528 MEMBLOCK);
5529 if (bcmerror) {
5530 DHD_ERROR(("%s: error %d on writing %d membytes at "
5531 "0x%08x\n",
5532 __func__, bcmerror, MEMBLOCK, offset));
5533 goto err;
5534 }
5535
5536 offset += MEMBLOCK;
5537 }
5538
5539 if (offset < sizeof(dlarray)) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005540 bcmerror = dhdsdio_membytes(bus, true, offset,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005541 dlarray + offset,
5542 sizeof(dlarray) - offset);
5543 if (bcmerror) {
5544 DHD_ERROR(("%s: error %d on writing %d membytes at "
5545 "0x%08x\n", __func__, bcmerror,
5546 sizeof(dlarray) - offset, offset));
5547 goto err;
5548 }
5549 }
5550#ifdef DHD_DEBUG
5551 /* Upload and compare the downloaded code */
5552 {
5553 unsigned char *ularray;
5554
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005555 ularray = kmalloc(bus->ramsize, GFP_ATOMIC);
Alexander Beregalov570edd32011-03-13 21:58:47 +03005556 if (!ularray) {
Roland Vossene10d82d2011-05-03 11:35:19 +02005557 bcmerror = -ENOMEM;
Alexander Beregalov570edd32011-03-13 21:58:47 +03005558 goto err;
5559 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005560 /* Upload image to verify downloaded contents. */
5561 offset = 0;
5562 memset(ularray, 0xaa, bus->ramsize);
5563 while ((offset + MEMBLOCK) < sizeof(dlarray)) {
5564 bcmerror =
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005565 dhdsdio_membytes(bus, false, offset,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005566 ularray + offset, MEMBLOCK);
5567 if (bcmerror) {
5568 DHD_ERROR(("%s: error %d on reading %d membytes"
5569 " at 0x%08x\n",
5570 __func__, bcmerror, MEMBLOCK, offset));
Alexander Beregalov570edd32011-03-13 21:58:47 +03005571 goto free;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005572 }
5573
5574 offset += MEMBLOCK;
5575 }
5576
5577 if (offset < sizeof(dlarray)) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005578 bcmerror = dhdsdio_membytes(bus, false, offset,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005579 ularray + offset,
5580 sizeof(dlarray) - offset);
5581 if (bcmerror) {
5582 DHD_ERROR(("%s: error %d on reading %d membytes at 0x%08x\n",
5583 __func__, bcmerror,
5584 sizeof(dlarray) - offset, offset));
Alexander Beregalov570edd32011-03-13 21:58:47 +03005585 goto free;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005586 }
5587 }
5588
5589 if (memcmp(dlarray, ularray, sizeof(dlarray))) {
5590 DHD_ERROR(("%s: Downloaded image is corrupted.\n",
5591 __func__));
5592 ASSERT(0);
Alexander Beregalov570edd32011-03-13 21:58:47 +03005593 goto free;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005594 } else
5595 DHD_ERROR(("%s: Download/Upload/Compare succeeded.\n",
5596 __func__));
Alexander Beregalov570edd32011-03-13 21:58:47 +03005597free:
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005598 kfree(ularray);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005599 }
5600#endif /* DHD_DEBUG */
5601
5602err:
5603 return bcmerror;
5604}
5605#endif /* BCMEMBEDIMAGE */
5606
5607static int dhdsdio_download_code_file(struct dhd_bus *bus, char *fw_path)
5608{
5609 int bcmerror = -1;
5610 int offset = 0;
5611 uint len;
5612 void *image = NULL;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005613 u8 *memblock = NULL, *memptr;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005614
5615 DHD_INFO(("%s: download firmware %s\n", __func__, fw_path));
5616
5617 image = dhd_os_open_image(fw_path);
5618 if (image == NULL)
5619 goto err;
5620
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005621 memptr = memblock = kmalloc(MEMBLOCK + DHD_SDALIGN, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005622 if (memblock == NULL) {
5623 DHD_ERROR(("%s: Failed to allocate memory %d bytes\n",
5624 __func__, MEMBLOCK));
5625 goto err;
5626 }
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005627 if ((u32)(unsigned long)memblock % DHD_SDALIGN)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005628 memptr +=
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005629 (DHD_SDALIGN - ((u32)(unsigned long)memblock % DHD_SDALIGN));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005630
5631 /* Download image */
5632 while ((len =
5633 dhd_os_get_image_block((char *)memptr, MEMBLOCK, image))) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005634 bcmerror = dhdsdio_membytes(bus, true, offset, memptr, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005635 if (bcmerror) {
5636 DHD_ERROR(("%s: error %d on writing %d membytes at "
5637 "0x%08x\n", __func__, bcmerror, MEMBLOCK, offset));
5638 goto err;
5639 }
5640
5641 offset += MEMBLOCK;
5642 }
5643
5644err:
Ilia Mirkin46d994b2011-03-13 00:28:56 -05005645 kfree(memblock);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005646
5647 if (image)
5648 dhd_os_close_image(image);
5649
5650 return bcmerror;
5651}
5652
5653/*
5654 * ProcessVars:Takes a buffer of "<var>=<value>\n" lines read from a file
5655 * and ending in a NUL.
5656 * Removes carriage returns, empty lines, comment lines, and converts
5657 * newlines to NULs.
5658 * Shortens buffer as needed and pads with NULs. End of buffer is marked
5659 * by two NULs.
5660*/
5661
5662static uint process_nvram_vars(char *varbuf, uint len)
5663{
5664 char *dp;
5665 bool findNewline;
5666 int column;
5667 uint buf_len, n;
5668
5669 dp = varbuf;
5670
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005671 findNewline = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005672 column = 0;
5673
5674 for (n = 0; n < len; n++) {
5675 if (varbuf[n] == 0)
5676 break;
5677 if (varbuf[n] == '\r')
5678 continue;
5679 if (findNewline && varbuf[n] != '\n')
5680 continue;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005681 findNewline = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005682 if (varbuf[n] == '#') {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005683 findNewline = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005684 continue;
5685 }
5686 if (varbuf[n] == '\n') {
5687 if (column == 0)
5688 continue;
5689 *dp++ = 0;
5690 column = 0;
5691 continue;
5692 }
5693 *dp++ = varbuf[n];
5694 column++;
5695 }
5696 buf_len = dp - varbuf;
5697
5698 while (dp < varbuf + n)
5699 *dp++ = 0;
5700
5701 return buf_len;
5702}
5703
5704/*
5705 EXAMPLE: nvram_array
5706 nvram_arry format:
5707 name=value
5708 Use carriage return at the end of each assignment,
5709 and an empty string with
5710 carriage return at the end of array.
5711
5712 For example:
5713 unsigned char nvram_array[] = {"name1=value1\n",
5714 "name2=value2\n", "\n"};
5715 Hex values start with 0x, and mac addr format: xx:xx:xx:xx:xx:xx.
5716
5717 Search "EXAMPLE: nvram_array" to see how the array is activated.
5718*/
5719
5720void dhd_bus_set_nvram_params(struct dhd_bus *bus, const char *nvram_params)
5721{
5722 bus->nvram_params = nvram_params;
5723}
5724
5725static int dhdsdio_download_nvram(struct dhd_bus *bus)
5726{
5727 int bcmerror = -1;
5728 uint len;
5729 void *image = NULL;
5730 char *memblock = NULL;
5731 char *bufp;
5732 char *nv_path;
5733 bool nvram_file_exists;
5734
5735 nv_path = bus->nv_path;
5736
5737 nvram_file_exists = ((nv_path != NULL) && (nv_path[0] != '\0'));
5738 if (!nvram_file_exists && (bus->nvram_params == NULL))
5739 return 0;
5740
5741 if (nvram_file_exists) {
5742 image = dhd_os_open_image(nv_path);
5743 if (image == NULL)
5744 goto err;
5745 }
5746
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005747 memblock = kmalloc(MEMBLOCK, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005748 if (memblock == NULL) {
5749 DHD_ERROR(("%s: Failed to allocate memory %d bytes\n",
5750 __func__, MEMBLOCK));
5751 goto err;
5752 }
5753
5754 /* Download variables */
5755 if (nvram_file_exists) {
5756 len = dhd_os_get_image_block(memblock, MEMBLOCK, image);
5757 } else {
5758 len = strlen(bus->nvram_params);
5759 ASSERT(len <= MEMBLOCK);
5760 if (len > MEMBLOCK)
5761 len = MEMBLOCK;
5762 memcpy(memblock, bus->nvram_params, len);
5763 }
5764
5765 if (len > 0 && len < MEMBLOCK) {
5766 bufp = (char *)memblock;
5767 bufp[len] = 0;
5768 len = process_nvram_vars(bufp, len);
5769 bufp += len;
5770 *bufp++ = 0;
5771 if (len)
5772 bcmerror = dhdsdio_downloadvars(bus, memblock, len + 1);
5773 if (bcmerror) {
5774 DHD_ERROR(("%s: error downloading vars: %d\n",
5775 __func__, bcmerror));
5776 }
5777 } else {
5778 DHD_ERROR(("%s: error reading nvram file: %d\n",
5779 __func__, len));
Roland Vossenb74ac122011-05-03 11:35:20 +02005780 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005781 }
5782
5783err:
Ilia Mirkin46d994b2011-03-13 00:28:56 -05005784 kfree(memblock);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005785
5786 if (image)
5787 dhd_os_close_image(image);
5788
5789 return bcmerror;
5790}
5791
5792static int _dhdsdio_download_firmware(struct dhd_bus *bus)
5793{
5794 int bcmerror = -1;
5795
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005796 bool embed = false; /* download embedded firmware */
5797 bool dlok = false; /* download firmware succeeded */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005798
5799 /* Out immediately if no image to download */
5800 if ((bus->fw_path == NULL) || (bus->fw_path[0] == '\0')) {
5801#ifdef BCMEMBEDIMAGE
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005802 embed = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005803#else
5804 return bcmerror;
5805#endif
5806 }
5807
5808 /* Keep arm in reset */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005809 if (dhdsdio_download_state(bus, true)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005810 DHD_ERROR(("%s: error placing ARM core in reset\n", __func__));
5811 goto err;
5812 }
5813
5814 /* External image takes precedence if specified */
5815 if ((bus->fw_path != NULL) && (bus->fw_path[0] != '\0')) {
5816 if (dhdsdio_download_code_file(bus, bus->fw_path)) {
5817 DHD_ERROR(("%s: dongle image file download failed\n",
5818 __func__));
5819#ifdef BCMEMBEDIMAGE
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005820 embed = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005821#else
5822 goto err;
5823#endif
5824 } else {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005825 embed = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005826 dlok = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005827 }
5828 }
5829#ifdef BCMEMBEDIMAGE
5830 if (embed) {
5831 if (dhdsdio_download_code_array(bus)) {
5832 DHD_ERROR(("%s: dongle image array download failed\n",
5833 __func__));
5834 goto err;
5835 } else {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005836 dlok = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005837 }
5838 }
5839#endif
5840 if (!dlok) {
5841 DHD_ERROR(("%s: dongle image download failed\n", __func__));
5842 goto err;
5843 }
5844
5845 /* EXAMPLE: nvram_array */
5846 /* If a valid nvram_arry is specified as above, it can be passed
5847 down to dongle */
5848 /* dhd_bus_set_nvram_params(bus, (char *)&nvram_array); */
5849
5850 /* External nvram takes precedence if specified */
5851 if (dhdsdio_download_nvram(bus)) {
5852 DHD_ERROR(("%s: dongle nvram file download failed\n",
5853 __func__));
5854 }
5855
5856 /* Take arm out of reset */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005857 if (dhdsdio_download_state(bus, false)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005858 DHD_ERROR(("%s: error getting out of ARM core reset\n",
5859 __func__));
5860 goto err;
5861 }
5862
5863 bcmerror = 0;
5864
5865err:
5866 return bcmerror;
5867}
5868
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005869
5870static int
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07005871dhd_bcmsdh_send_buf(dhd_bus_t *bus, u32 addr, uint fn, uint flags,
Arend van Sprielc26b1372010-11-23 14:06:23 +01005872 u8 *buf, uint nbytes, struct sk_buff *pkt,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005873 bcmsdh_cmplt_fn_t complete, void *handle)
5874{
5875 return bcmsdh_send_buf
5876 (bus->sdh, addr, fn, flags, buf, nbytes, pkt, complete,
5877 handle);
5878}
5879
5880uint dhd_bus_chip(struct dhd_bus *bus)
5881{
Franky Linc05df632011-04-25 19:34:07 -07005882 ASSERT(bus->ci != NULL);
5883 return bus->ci->chip;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005884}
5885
5886void *dhd_bus_pub(struct dhd_bus *bus)
5887{
5888 return bus->dhd;
5889}
5890
5891void *dhd_bus_txq(struct dhd_bus *bus)
5892{
5893 return &bus->txq;
5894}
5895
5896uint dhd_bus_hdrlen(struct dhd_bus *bus)
5897{
5898 return SDPCM_HDRLEN;
5899}
5900
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005901int dhd_bus_devreset(dhd_pub_t *dhdp, u8 flag)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005902{
5903 int bcmerror = 0;
5904 dhd_bus_t *bus;
5905
5906 bus = dhdp->bus;
5907
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005908 if (flag == true) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005909 if (!bus->dhd->dongle_reset) {
5910 /* Expect app to have torn down any
5911 connection before calling */
5912 /* Stop the bus, disable F2 */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005913 dhd_bus_stop(bus, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005914
5915 /* Clean tx/rx buffer pointers,
5916 detach from the dongle */
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005917 dhdsdio_release_dongle(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005918
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005919 bus->dhd->dongle_reset = true;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005920 bus->dhd->up = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005921
5922 DHD_TRACE(("%s: WLAN OFF DONE\n", __func__));
5923 /* App can now remove power from device */
5924 } else
Roland Vossenb74ac122011-05-03 11:35:20 +02005925 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005926 } else {
5927 /* App must have restored power to device before calling */
5928
5929 DHD_TRACE(("\n\n%s: == WLAN ON ==\n", __func__));
5930
5931 if (bus->dhd->dongle_reset) {
5932 /* Turn on WLAN */
5933 /* Reset SD client */
5934 bcmsdh_reset(bus->sdh);
5935
5936 /* Attempt to re-attach & download */
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005937 if (dhdsdio_probe_attach(bus, bus->sdh,
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07005938 (u32 *) SI_ENUM_BASE,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005939 bus->cl_devid)) {
5940 /* Attempt to download binary to the dongle */
5941 if (dhdsdio_probe_init
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005942 (bus, bus->sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005943 && dhdsdio_download_firmware(bus,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005944 bus->sdh)) {
5945
5946 /* Re-init bus, enable F2 transfer */
5947 dhd_bus_init((dhd_pub_t *) bus->dhd,
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005948 false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005949
5950#if defined(OOB_INTR_ONLY)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005951 dhd_enable_oob_intr(bus, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005952#endif /* defined(OOB_INTR_ONLY) */
5953
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005954 bus->dhd->dongle_reset = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005955 bus->dhd->up = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005956
5957 DHD_TRACE(("%s: WLAN ON DONE\n",
5958 __func__));
5959 } else
Roland Vossenb74ac122011-05-03 11:35:20 +02005960 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005961 } else
Roland Vossenb74ac122011-05-03 11:35:20 +02005962 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005963 } else {
Roland Vossenb74ac122011-05-03 11:35:20 +02005964 bcmerror = -EISCONN;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005965 DHD_ERROR(("%s: Set DEVRESET=false invoked when device "
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005966 "is on\n", __func__));
Roland Vossenb74ac122011-05-03 11:35:20 +02005967 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005968 }
5969 }
5970 return bcmerror;
5971}
Franky Lincb63e4c2011-04-25 15:45:08 -07005972
5973static int
5974dhdsdio_chip_recognition(bcmsdh_info_t *sdh, struct chip_info *ci, void *regs)
5975{
5976 u32 regdata;
5977
5978 /*
5979 * Get CC core rev
5980 * Chipid is assume to be at offset 0 from regs arg
5981 * For different chiptypes or old sdio hosts w/o chipcommon,
5982 * other ways of recognition should be added here.
5983 */
5984 ci->cccorebase = (u32)regs;
5985 regdata = bcmsdh_reg_read(sdh, CORE_CC_REG(ci->cccorebase, chipid), 4);
5986 ci->chip = regdata & CID_ID_MASK;
5987 ci->chiprev = (regdata & CID_REV_MASK) >> CID_REV_SHIFT;
5988
5989 DHD_INFO(("%s: chipid=0x%x chiprev=%d\n",
5990 __func__, ci->chip, ci->chiprev));
5991
5992 /* Address of cores for new chips should be added here */
5993 switch (ci->chip) {
5994 case BCM4329_CHIP_ID:
5995 ci->buscorebase = BCM4329_CORE_BUS_BASE;
5996 ci->ramcorebase = BCM4329_CORE_SOCRAM_BASE;
5997 ci->armcorebase = BCM4329_CORE_ARM_BASE;
Franky Linc05df632011-04-25 19:34:07 -07005998 ci->ramsize = BCM4329_RAMSIZE;
Franky Lincb63e4c2011-04-25 15:45:08 -07005999 break;
6000 default:
6001 DHD_ERROR(("%s: chipid 0x%x is not supported\n",
6002 __func__, ci->chip));
6003 return -ENODEV;
6004 }
6005
6006 regdata = bcmsdh_reg_read(sdh,
6007 CORE_SB(ci->cccorebase, sbidhigh), 4);
6008 ci->ccrev = SBCOREREV(regdata);
6009
6010 regdata = bcmsdh_reg_read(sdh,
6011 CORE_CC_REG(ci->cccorebase, pmucapabilities), 4);
6012 ci->pmurev = regdata & PCAP_REV_MASK;
6013
6014 regdata = bcmsdh_reg_read(sdh, CORE_SB(ci->buscorebase, sbidhigh), 4);
6015 ci->buscorerev = SBCOREREV(regdata);
6016 ci->buscoretype = (regdata & SBIDH_CC_MASK) >> SBIDH_CC_SHIFT;
6017
6018 DHD_INFO(("%s: ccrev=%d, pmurev=%d, buscore rev/type=%d/0x%x\n",
6019 __func__, ci->ccrev, ci->pmurev,
6020 ci->buscorerev, ci->buscoretype));
6021
6022 /* get chipcommon capabilites */
6023 ci->cccaps = bcmsdh_reg_read(sdh,
6024 CORE_CC_REG(ci->cccorebase, capabilities), 4);
6025
6026 return 0;
6027}
6028
6029static void
6030dhdsdio_chip_disablecore(bcmsdh_info_t *sdh, u32 corebase)
6031{
6032 u32 regdata;
6033
6034 regdata = bcmsdh_reg_read(sdh,
6035 CORE_SB(corebase, sbtmstatelow), 4);
6036 if (regdata & SBTML_RESET)
6037 return;
6038
6039 regdata = bcmsdh_reg_read(sdh,
6040 CORE_SB(corebase, sbtmstatelow), 4);
6041 if ((regdata & (SICF_CLOCK_EN << SBTML_SICF_SHIFT)) != 0) {
6042 /*
6043 * set target reject and spin until busy is clear
6044 * (preserve core-specific bits)
6045 */
6046 regdata = bcmsdh_reg_read(sdh,
6047 CORE_SB(corebase, sbtmstatelow), 4);
6048 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6049 regdata | SBTML_REJ);
6050
6051 regdata = bcmsdh_reg_read(sdh,
6052 CORE_SB(corebase, sbtmstatelow), 4);
6053 udelay(1);
6054 SPINWAIT((bcmsdh_reg_read(sdh,
6055 CORE_SB(corebase, sbtmstatehigh), 4) &
6056 SBTMH_BUSY), 100000);
6057
6058 regdata = bcmsdh_reg_read(sdh,
6059 CORE_SB(corebase, sbtmstatehigh), 4);
6060 if (regdata & SBTMH_BUSY)
6061 DHD_ERROR(("%s: ARM core still busy\n", __func__));
6062
6063 regdata = bcmsdh_reg_read(sdh,
6064 CORE_SB(corebase, sbidlow), 4);
6065 if (regdata & SBIDL_INIT) {
6066 regdata = bcmsdh_reg_read(sdh,
6067 CORE_SB(corebase, sbimstate), 4) |
6068 SBIM_RJ;
6069 bcmsdh_reg_write(sdh,
6070 CORE_SB(corebase, sbimstate), 4,
6071 regdata);
6072 regdata = bcmsdh_reg_read(sdh,
6073 CORE_SB(corebase, sbimstate), 4);
6074 udelay(1);
6075 SPINWAIT((bcmsdh_reg_read(sdh,
6076 CORE_SB(corebase, sbimstate), 4) &
6077 SBIM_BY), 100000);
6078 }
6079
6080 /* set reset and reject while enabling the clocks */
6081 bcmsdh_reg_write(sdh,
6082 CORE_SB(corebase, sbtmstatelow), 4,
6083 (((SICF_FGC | SICF_CLOCK_EN) << SBTML_SICF_SHIFT) |
6084 SBTML_REJ | SBTML_RESET));
6085 regdata = bcmsdh_reg_read(sdh,
6086 CORE_SB(corebase, sbtmstatelow), 4);
6087 udelay(10);
6088
6089 /* clear the initiator reject bit */
6090 regdata = bcmsdh_reg_read(sdh,
6091 CORE_SB(corebase, sbidlow), 4);
6092 if (regdata & SBIDL_INIT) {
6093 regdata = bcmsdh_reg_read(sdh,
6094 CORE_SB(corebase, sbimstate), 4) &
6095 ~SBIM_RJ;
6096 bcmsdh_reg_write(sdh,
6097 CORE_SB(corebase, sbimstate), 4,
6098 regdata);
6099 }
6100 }
6101
6102 /* leave reset and reject asserted */
6103 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6104 (SBTML_REJ | SBTML_RESET));
6105 udelay(1);
6106}
6107
6108static int
6109dhdsdio_chip_attach(struct dhd_bus *bus, void *regs)
6110{
6111 struct chip_info *ci;
6112 int err;
6113 u8 clkval, clkset;
6114
6115 DHD_TRACE(("%s: Enter\n", __func__));
6116
6117 /* alloc chip_info_t */
6118 ci = kmalloc(sizeof(struct chip_info), GFP_ATOMIC);
6119 if (NULL == ci) {
6120 DHD_ERROR(("%s: malloc failed!\n", __func__));
6121 return -ENOMEM;
6122 }
6123
6124 memset((unsigned char *)ci, 0, sizeof(struct chip_info));
6125
6126 /* bus/core/clk setup for register access */
6127 /* Try forcing SDIO core to do ALPAvail request only */
6128 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
6129 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
6130 clkset, &err);
6131 if (err) {
6132 DHD_ERROR(("%s: error writing for HT off\n", __func__));
6133 goto fail;
6134 }
6135
6136 /* If register supported, wait for ALPAvail and then force ALP */
6137 /* This may take up to 15 milliseconds */
6138 clkval = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
6139 SBSDIO_FUNC1_CHIPCLKCSR, NULL);
6140 if ((clkval & ~SBSDIO_AVBITS) == clkset) {
6141 SPINWAIT(((clkval =
6142 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
6143 SBSDIO_FUNC1_CHIPCLKCSR,
6144 NULL)),
6145 !SBSDIO_ALPAV(clkval)),
6146 PMU_MAX_TRANSITION_DLY);
6147 if (!SBSDIO_ALPAV(clkval)) {
6148 DHD_ERROR(("%s: timeout on ALPAV wait, clkval 0x%02x\n",
6149 __func__, clkval));
6150 err = -EBUSY;
6151 goto fail;
6152 }
6153 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF |
6154 SBSDIO_FORCE_ALP;
6155 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1,
6156 SBSDIO_FUNC1_CHIPCLKCSR,
6157 clkset, &err);
6158 udelay(65);
6159 } else {
6160 DHD_ERROR(("%s: ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
6161 __func__, clkset, clkval));
6162 err = -EACCES;
6163 goto fail;
6164 }
6165
6166 /* Also, disable the extra SDIO pull-ups */
6167 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SDIOPULLUP, 0,
6168 NULL);
6169
6170 err = dhdsdio_chip_recognition(bus->sdh, ci, regs);
6171 if (err)
6172 goto fail;
6173
6174 /*
6175 * Make sure any on-chip ARM is off (in case strapping is wrong),
6176 * or downloaded code was already running.
6177 */
6178 dhdsdio_chip_disablecore(bus->sdh, ci->armcorebase);
6179
6180 bcmsdh_reg_write(bus->sdh,
6181 CORE_CC_REG(ci->cccorebase, gpiopullup), 4, 0);
6182 bcmsdh_reg_write(bus->sdh,
6183 CORE_CC_REG(ci->cccorebase, gpiopulldown), 4, 0);
6184
6185 /* Disable F2 to clear any intermediate frame state on the dongle */
6186 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN,
6187 SDIO_FUNC_ENABLE_1, NULL);
6188
6189 /* WAR: cmd52 backplane read so core HW will drop ALPReq */
6190 clkval = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
6191 0, NULL);
6192
6193 /* Done with backplane-dependent accesses, can drop clock... */
6194 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, 0,
6195 NULL);
6196
6197 bus->ci = ci;
6198 return 0;
6199fail:
6200 bus->ci = NULL;
6201 kfree(ci);
6202 return err;
6203}
Franky Lineb5dc512011-04-25 19:34:04 -07006204
6205static void
6206dhdsdio_chip_resetcore(bcmsdh_info_t *sdh, u32 corebase)
6207{
6208 u32 regdata;
6209
6210 /*
6211 * Must do the disable sequence first to work for
6212 * arbitrary current core state.
6213 */
6214 dhdsdio_chip_disablecore(sdh, corebase);
6215
6216 /*
6217 * Now do the initialization sequence.
6218 * set reset while enabling the clock and
6219 * forcing them on throughout the core
6220 */
6221 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6222 ((SICF_FGC | SICF_CLOCK_EN) << SBTML_SICF_SHIFT) |
6223 SBTML_RESET);
6224 udelay(1);
6225
6226 regdata = bcmsdh_reg_read(sdh, CORE_SB(corebase, sbtmstatehigh), 4);
6227 if (regdata & SBTMH_SERR)
6228 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatehigh), 4, 0);
6229
6230 regdata = bcmsdh_reg_read(sdh, CORE_SB(corebase, sbimstate), 4);
6231 if (regdata & (SBIM_IBE | SBIM_TO))
6232 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbimstate), 4,
6233 regdata & ~(SBIM_IBE | SBIM_TO));
6234
6235 /* clear reset and allow it to propagate throughout the core */
6236 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6237 (SICF_FGC << SBTML_SICF_SHIFT) |
6238 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
6239 udelay(1);
6240
6241 /* leave clock enabled */
6242 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6243 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
6244 udelay(1);
6245}
Franky Lin5d0d7a92011-04-25 19:34:05 -07006246
6247/* SDIO Pad drive strength to select value mappings */
6248struct sdiod_drive_str {
6249 u8 strength; /* Pad Drive Strength in mA */
6250 u8 sel; /* Chip-specific select value */
6251};
6252
6253/* SDIO Drive Strength to sel value table for PMU Rev 1 */
6254static const struct sdiod_drive_str sdiod_drive_strength_tab1[] = {
6255 {
6256 4, 0x2}, {
6257 2, 0x3}, {
6258 1, 0x0}, {
6259 0, 0x0}
6260 };
6261
6262/* SDIO Drive Strength to sel value table for PMU Rev 2, 3 */
6263static const struct sdiod_drive_str sdiod_drive_strength_tab2[] = {
6264 {
6265 12, 0x7}, {
6266 10, 0x6}, {
6267 8, 0x5}, {
6268 6, 0x4}, {
6269 4, 0x2}, {
6270 2, 0x1}, {
6271 0, 0x0}
6272 };
6273
6274/* SDIO Drive Strength to sel value table for PMU Rev 8 (1.8V) */
6275static const struct sdiod_drive_str sdiod_drive_strength_tab3[] = {
6276 {
6277 32, 0x7}, {
6278 26, 0x6}, {
6279 22, 0x5}, {
6280 16, 0x4}, {
6281 12, 0x3}, {
6282 8, 0x2}, {
6283 4, 0x1}, {
6284 0, 0x0}
6285 };
6286
6287#define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
6288
6289static void
6290dhdsdio_sdiod_drive_strength_init(struct dhd_bus *bus, u32 drivestrength) {
6291 struct sdiod_drive_str *str_tab = NULL;
6292 u32 str_mask = 0;
6293 u32 str_shift = 0;
Franky Lin5d0d7a92011-04-25 19:34:05 -07006294 char chn[8];
Franky Lin5d0d7a92011-04-25 19:34:05 -07006295
6296 if (!(bus->ci->cccaps & CC_CAP_PMU))
6297 return;
6298
6299 switch (SDIOD_DRVSTR_KEY(bus->ci->chip, bus->ci->pmurev)) {
6300 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 1):
6301 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab1;
6302 str_mask = 0x30000000;
6303 str_shift = 28;
6304 break;
6305 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 2):
6306 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 3):
6307 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab2;
6308 str_mask = 0x00003800;
6309 str_shift = 11;
6310 break;
6311 case SDIOD_DRVSTR_KEY(BCM4336_CHIP_ID, 8):
6312 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab3;
6313 str_mask = 0x00003800;
6314 str_shift = 11;
6315 break;
6316 default:
6317 DHD_ERROR(("No SDIO Drive strength init"
6318 "done for chip %s rev %d pmurev %d\n",
6319 bcm_chipname(bus->ci->chip, chn, 8),
6320 bus->ci->chiprev, bus->ci->pmurev));
6321 break;
6322 }
6323
6324 if (str_tab != NULL) {
6325 u32 drivestrength_sel = 0;
6326 u32 cc_data_temp;
6327 int i;
6328
6329 for (i = 0; str_tab[i].strength != 0; i++) {
6330 if (drivestrength >= str_tab[i].strength) {
6331 drivestrength_sel = str_tab[i].sel;
6332 break;
6333 }
6334 }
6335
6336 bcmsdh_reg_write(bus->sdh,
6337 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr),
6338 4, 1);
6339 cc_data_temp = bcmsdh_reg_read(bus->sdh,
6340 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr), 4);
6341 cc_data_temp &= ~str_mask;
6342 drivestrength_sel <<= str_shift;
6343 cc_data_temp |= drivestrength_sel;
6344 bcmsdh_reg_write(bus->sdh,
6345 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr),
6346 4, cc_data_temp);
6347
6348 DHD_INFO(("SDIO: %dmA drive strength selected, set to 0x%08x\n",
6349 drivestrength, cc_data_temp));
6350 }
6351}
Franky Lincee3cf42011-04-25 19:34:06 -07006352
6353static void
6354dhdsdio_chip_detach(struct dhd_bus *bus)
6355{
6356 DHD_TRACE(("%s: Enter\n", __func__));
6357
6358 kfree(bus->ci);
6359 bus->ci = NULL;
6360}