blob: 5d747b851ed7cde8fd0bf9d93b82e5ddaa5c4bc3 [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
Henry Ptasinskicf2b4482010-09-20 22:33:12 -070033#include <hndsoc.h>
34#ifdef DHD_DEBUG
35#include <hndrte_armtrap.h>
36#include <hndrte_cons.h>
37#endif /* DHD_DEBUG */
38#include <sbchipc.h>
39#include <sbhnddma.h>
40
41#include <sdio.h>
42#include <sbsdio.h>
43#include <sbsdpcmdev.h>
44#include <bcmsdpcm.h>
45
Henry Ptasinskicf2b4482010-09-20 22:33:12 -070046#include <proto/802.11.h>
47
48#include <dngl_stats.h>
49#include <dhd.h>
50#include <dhd_bus.h>
51#include <dhd_proto.h>
52#include <dhd_dbg.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -070053#include <sdiovar.h>
Franky Lincb63e4c2011-04-25 15:45:08 -070054#include <bcmchip.h>
Henry Ptasinskicf2b4482010-09-20 22:33:12 -070055
56#ifndef DHDSDIO_MEM_DUMP_FNAME
57#define DHDSDIO_MEM_DUMP_FNAME "mem_dump"
58#endif
59
Grant Grundler26a71a42011-03-09 10:41:25 -080060#define TXQLEN 2048 /* bulk tx queue length */
61#define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
62#define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -070063#define PRIOMASK 7
64
65#define TXRETRIES 2 /* # of retries for tx frames */
66
67#if defined(CONFIG_MACH_SANDGATE2G)
68#define DHD_RXBOUND 250 /* Default for max rx frames in
69 one scheduling */
70#else
71#define DHD_RXBOUND 50 /* Default for max rx frames in
72 one scheduling */
73#endif /* defined(CONFIG_MACH_SANDGATE2G) */
74
75#define DHD_TXBOUND 20 /* Default for max tx frames in
76 one scheduling */
77
78#define DHD_TXMINMAX 1 /* Max tx frames if rx still pending */
79
80#define MEMBLOCK 2048 /* Block size used for downloading
81 of dongle image */
82#define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
83 biggest possible glom */
84
85/* Packet alignment for most efficient SDIO (can change based on platform) */
86#ifndef DHD_SDALIGN
87#define DHD_SDALIGN 32
88#endif
89#if !ISPOWEROF2(DHD_SDALIGN)
90#error DHD_SDALIGN is not a power of 2!
91#endif
92
93#ifndef DHD_FIRSTREAD
94#define DHD_FIRSTREAD 32
95#endif
96#if !ISPOWEROF2(DHD_FIRSTREAD)
97#error DHD_FIRSTREAD is not a power of 2!
98#endif
99
100/* Total length of frame header for dongle protocol */
101#define SDPCM_HDRLEN (SDPCM_FRAMETAG_LEN + SDPCM_SWHEADER_LEN)
102#ifdef SDTEST
103#define SDPCM_RESERVE (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + DHD_SDALIGN)
104#else
105#define SDPCM_RESERVE (SDPCM_HDRLEN + DHD_SDALIGN)
106#endif
107
108/* Space for header read, limit for data packets */
109#ifndef MAX_HDR_READ
110#define MAX_HDR_READ 32
111#endif
112#if !ISPOWEROF2(MAX_HDR_READ)
113#error MAX_HDR_READ is not a power of 2!
114#endif
115
116#define MAX_RX_DATASZ 2048
117
118/* Maximum milliseconds to wait for F2 to come up */
119#define DHD_WAIT_F2RDY 3000
120
121/* Bump up limit on waiting for HT to account for first startup;
122 * if the image is doing a CRC calculation before programming the PMU
123 * for HT availability, it could take a couple hundred ms more, so
124 * max out at a 1 second (1000000us).
125 */
126#if (PMU_MAX_TRANSITION_DLY <= 1000000)
127#undef PMU_MAX_TRANSITION_DLY
128#define PMU_MAX_TRANSITION_DLY 1000000
129#endif
130
131/* Value for ChipClockCSR during initial setup */
132#define DHD_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
133 SBSDIO_ALP_AVAIL_REQ)
134#define DHD_INIT_CLKCTL2 (SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP)
135
136/* Flags for SDH calls */
137#define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
138
Arend van Spriel70dfb582011-02-25 16:39:17 +0100139/*
140 * Conversion of 802.1D priority to precedence level
141 */
142#define PRIO2PREC(prio) \
143 (((prio) == PRIO_8021D_NONE || (prio) == PRIO_8021D_BE) ? \
144 ((prio^2)) : (prio))
145
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700146DHD_SPINWAIT_SLEEP_INIT(sdioh_spinwait_sleep);
147extern int dhdcdc_set_ioctl(dhd_pub_t *dhd, int ifidx, uint cmd, void *buf,
148 uint len);
149
150#ifdef DHD_DEBUG
151/* Device console log buffer state */
152typedef struct dhd_console {
153 uint count; /* Poll interval msec counter */
154 uint log_addr; /* Log struct address (fixed) */
155 hndrte_log_t log; /* Log struct (host copy) */
156 uint bufsize; /* Size of log buffer */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700157 u8 *buf; /* Log buffer (host copy) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700158 uint last; /* Last buffer read index */
159} dhd_console_t;
160#endif /* DHD_DEBUG */
161
Franky Lincb63e4c2011-04-25 15:45:08 -0700162/* misc chip info needed by some of the routines */
163struct chip_info {
164 u32 chip;
165 u32 chiprev;
166 u32 cccorebase;
167 u32 ccrev;
168 u32 cccaps;
169 u32 buscorebase;
170 u32 buscorerev;
171 u32 buscoretype;
172 u32 ramcorebase;
173 u32 armcorebase;
174 u32 pmurev;
Franky Linc05df632011-04-25 19:34:07 -0700175 u32 ramsize;
Franky Lincb63e4c2011-04-25 15:45:08 -0700176};
177
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700178/* Private data for SDIO bus interaction */
179typedef struct dhd_bus {
180 dhd_pub_t *dhd;
181
182 bcmsdh_info_t *sdh; /* Handle for BCMSDH calls */
Franky Lincb63e4c2011-04-25 15:45:08 -0700183 struct chip_info *ci; /* Chip info struct */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700184 char *vars; /* Variables (from CIS and/or other) */
185 uint varsz; /* Size of variables buffer */
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700186 u32 sbaddr; /* Current SB window pointer (-1, invalid) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700187
188 sdpcmd_regs_t *regs; /* Registers for SDIO core */
189 uint sdpcmrev; /* SDIO core revision */
190 uint armrev; /* CPU core revision */
191 uint ramrev; /* SOCRAM core revision */
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700192 u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
193 u32 orig_ramsize; /* Size of RAM in SOCRAM (bytes) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700194
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700195 u32 bus; /* gSPI or SDIO bus */
196 u32 hostintmask; /* Copy of Host Interrupt Mask */
197 u32 intstatus; /* Intstatus bits (events) pending */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700198 bool dpc_sched; /* Indicates DPC schedule (intrpt rcvd) */
199 bool fcstate; /* State of dongle flow-control */
200
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700201 u16 cl_devid; /* cached devid for dhdsdio_probe_attach() */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700202 char *fw_path; /* module_param: path to firmware image */
203 char *nv_path; /* module_param: path to nvram vars file */
204 const char *nvram_params; /* user specified nvram params. */
205
206 uint blocksize; /* Block size of SDIO transfers */
207 uint roundup; /* Max roundup limit */
208
209 struct pktq txq; /* Queue length used for flow-control */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700210 u8 flowcontrol; /* per prio flow control bitmask */
211 u8 tx_seq; /* Transmit sequence number (next) */
212 u8 tx_max; /* Maximum transmit sequence allowed */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700213
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700214 u8 hdrbuf[MAX_HDR_READ + DHD_SDALIGN];
215 u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700216 u16 nextlen; /* Next Read Len from last header */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700217 u8 rx_seq; /* Receive sequence number (expected) */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700218 bool rxskip; /* Skip receive (awaiting NAK ACK) */
219
Arend van Sprielc26b1372010-11-23 14:06:23 +0100220 struct sk_buff *glomd; /* Packet containing glomming descriptor */
221 struct sk_buff *glom; /* Packet chain for glommed superframe */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700222 uint glomerr; /* Glom packet read errors */
223
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700224 u8 *rxbuf; /* Buffer for receiving control packets */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700225 uint rxblen; /* Allocated length of rxbuf */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700226 u8 *rxctl; /* Aligned pointer into rxbuf */
227 u8 *databuf; /* Buffer for receiving big glom packet */
228 u8 *dataptr; /* Aligned pointer into databuf */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700229 uint rxlen; /* Length of valid data in buffer */
230
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700231 u8 sdpcm_ver; /* Bus protocol reported by dongle */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700232
233 bool intr; /* Use interrupts */
234 bool poll; /* Use polling */
235 bool ipend; /* Device interrupt is pending */
236 bool intdis; /* Interrupts disabled by isr */
237 uint intrcount; /* Count of device interrupt callbacks */
238 uint lastintrs; /* Count as of last watchdog timer */
239 uint spurious; /* Count of spurious interrupts */
240 uint pollrate; /* Ticks between device polls */
241 uint polltick; /* Tick counter */
242 uint pollcnt; /* Count of active polls */
243
244#ifdef DHD_DEBUG
245 dhd_console_t console; /* Console output polling support */
246 uint console_addr; /* Console address from shared struct */
247#endif /* DHD_DEBUG */
248
249 uint regfails; /* Count of R_REG/W_REG failures */
250
251 uint clkstate; /* State of sd and backplane clock(s) */
252 bool activity; /* Activity flag for clock down */
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -0700253 s32 idletime; /* Control for activity timeout */
254 s32 idlecount; /* Activity timeout counter */
255 s32 idleclock; /* How to set bus driver when idle */
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -0700256 s32 sd_rxchain; /* If bcmsdh api accepts PKT chains */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700257 bool use_rxchain; /* If dhd should use PKT chains */
258 bool sleeping; /* Is SDIO bus sleeping? */
259 bool rxflow_mode; /* Rx flow control mode */
260 bool rxflow; /* Is rx flow control on */
261 uint prev_rxlim_hit; /* Is prev rx limit exceeded
262 (per dpc schedule) */
263 bool alp_only; /* Don't use HT clock (ALP only) */
264/* Field to decide if rx of control frames happen in rxbuf or lb-pool */
265 bool usebufpool;
266
267#ifdef SDTEST
268 /* external loopback */
269 bool ext_loop;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700270 u8 loopid;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700271
272 /* pktgen configuration */
273 uint pktgen_freq; /* Ticks between bursts */
274 uint pktgen_count; /* Packets to send each burst */
275 uint pktgen_print; /* Bursts between count displays */
276 uint pktgen_total; /* Stop after this many */
277 uint pktgen_minlen; /* Minimum packet data len */
278 uint pktgen_maxlen; /* Maximum packet data len */
279 uint pktgen_mode; /* Configured mode: tx, rx, or echo */
280 uint pktgen_stop; /* Number of tx failures causing stop */
281
282 /* active pktgen fields */
283 uint pktgen_tick; /* Tick counter for bursts */
284 uint pktgen_ptick; /* Burst counter for printing */
285 uint pktgen_sent; /* Number of test packets generated */
286 uint pktgen_rcvd; /* Number of test packets received */
287 uint pktgen_fail; /* Number of failed send attempts */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700288 u16 pktgen_len; /* Length of next packet to send */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700289#endif /* SDTEST */
290
291 /* Some additional counters */
292 uint tx_sderrs; /* Count of tx attempts with sd errors */
293 uint fcqueued; /* Tx packets that got queued */
294 uint rxrtx; /* Count of rtx requests (NAK to dongle) */
295 uint rx_toolong; /* Receive frames too long to receive */
296 uint rxc_errors; /* SDIO errors when reading control frames */
297 uint rx_hdrfail; /* SDIO errors on header reads */
298 uint rx_badhdr; /* Bad received headers (roosync?) */
299 uint rx_badseq; /* Mismatched rx sequence number */
300 uint fc_rcvd; /* Number of flow-control events received */
301 uint fc_xoff; /* Number which turned on flow-control */
302 uint fc_xon; /* Number which turned off flow-control */
303 uint rxglomfail; /* Failed deglom attempts */
304 uint rxglomframes; /* Number of glom frames (superframes) */
305 uint rxglompkts; /* Number of packets from glom frames */
306 uint f2rxhdrs; /* Number of header reads */
307 uint f2rxdata; /* Number of frame data reads */
308 uint f2txdata; /* Number of f2 frame writes */
309 uint f1regdata; /* Number of f1 register accesses */
310
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700311 u8 *ctrl_frame_buf;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700312 u32 ctrl_frame_len;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700313 bool ctrl_frame_stat;
314} dhd_bus_t;
315
316/* clkstate */
317#define CLK_NONE 0
318#define CLK_SDONLY 1
319#define CLK_PENDING 2 /* Not used yet */
320#define CLK_AVAIL 3
321
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700322#define DHD_NOPMU(dhd) (false)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700323
324#ifdef DHD_DEBUG
325static int qcount[NUMPRIO];
326static int tx_packets[NUMPRIO];
327#endif /* DHD_DEBUG */
328
329/* Deferred transmit */
330const uint dhd_deferred_tx = 1;
331
332extern uint dhd_watchdog_ms;
333extern void dhd_os_wd_timer(void *bus, uint wdtick);
334
335/* Tx/Rx bounds */
336uint dhd_txbound;
337uint dhd_rxbound;
338uint dhd_txminmax;
339
340/* override the RAM size if possible */
341#define DONGLE_MIN_MEMSIZE (128 * 1024)
342int dhd_dongle_memsize;
343
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700344static bool dhd_alignctl;
345
346static bool sd1idle;
347
348static bool retrydata;
349#define RETRYCHAN(chan) (((chan) == SDPCM_EVENT_CHANNEL) || retrydata)
350
351static const uint watermark = 8;
352static const uint firstread = DHD_FIRSTREAD;
353
354#define HDATLEN (firstread - (SDPCM_HDRLEN))
355
356/* Retry count for register access failures */
357static const uint retry_limit = 2;
358
359/* Force even SD lengths (some host controllers mess up on odd bytes) */
360static bool forcealign;
361
362#define ALIGNMENT 4
363
364#if defined(OOB_INTR_ONLY) && defined(HW_OOB)
365extern void bcmsdh_enable_hw_oob_intr(void *sdh, bool enable);
366#endif
367
368#if defined(OOB_INTR_ONLY) && defined(SDIO_ISR_THREAD)
369#error OOB_INTR_ONLY is NOT working with SDIO_ISR_THREAD
370#endif /* defined(OOB_INTR_ONLY) && defined(SDIO_ISR_THREAD) */
Arend van Spriel3c9d4c32011-03-02 21:18:48 +0100371#define PKTALIGN(_p, _len, _align) \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700372 do { \
373 uint datalign; \
Arend van Spriel54991ad2010-11-23 14:06:24 +0100374 datalign = (unsigned long)((_p)->data); \
375 datalign = roundup(datalign, (_align)) - datalign; \
376 ASSERT(datalign < (_align)); \
377 ASSERT((_p)->len >= ((_len) + datalign)); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700378 if (datalign) \
Arend van Spriel54991ad2010-11-23 14:06:24 +0100379 skb_pull((_p), datalign); \
380 __skb_trim((_p), (_len)); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700381 } while (0)
382
383/* Limit on rounding up frames */
384static const uint max_roundup = 512;
385
386/* Try doing readahead */
387static bool dhd_readahead;
388
389/* To check if there's window offered */
390#define DATAOK(bus) \
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700391 (((u8)(bus->tx_max - bus->tx_seq) != 0) && \
392 (((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700393
394/* Macros to get register read/write status */
395/* NOTE: these assume a local dhdsdio_bus_t *bus! */
396#define R_SDREG(regvar, regaddr, retryvar) \
397do { \
398 retryvar = 0; \
399 do { \
Arend van Sprielff31c542011-03-01 10:56:54 +0100400 regvar = R_REG(regaddr); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700401 } while (bcmsdh_regfail(bus->sdh) && (++retryvar <= retry_limit)); \
402 if (retryvar) { \
403 bus->regfails += (retryvar-1); \
404 if (retryvar > retry_limit) { \
405 DHD_ERROR(("%s: FAILED" #regvar "READ, LINE %d\n", \
406 __func__, __LINE__)); \
407 regvar = 0; \
408 } \
409 } \
410} while (0)
411
412#define W_SDREG(regval, regaddr, retryvar) \
413do { \
414 retryvar = 0; \
415 do { \
Arend van Sprielff31c542011-03-01 10:56:54 +0100416 W_REG(regaddr, regval); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700417 } while (bcmsdh_regfail(bus->sdh) && (++retryvar <= retry_limit)); \
418 if (retryvar) { \
419 bus->regfails += (retryvar-1); \
420 if (retryvar > retry_limit) \
421 DHD_ERROR(("%s: FAILED REGISTER WRITE, LINE %d\n", \
422 __func__, __LINE__)); \
423 } \
424} while (0)
425
426#define DHD_BUS SDIO_BUS
427
428#define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
429
430#define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
431
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700432#ifdef SDTEST
433static void dhdsdio_testrcv(dhd_bus_t *bus, void *pkt, uint seq);
434static void dhdsdio_sdtest_set(dhd_bus_t *bus, bool start);
435#endif
436
437#ifdef DHD_DEBUG
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700438static int dhdsdio_checkdied(dhd_bus_t *bus, u8 *data, uint size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700439static int dhdsdio_mem_dump(dhd_bus_t *bus);
440#endif /* DHD_DEBUG */
441static int dhdsdio_download_state(dhd_bus_t *bus, bool enter);
442
Arend van Spriel3c9d4c32011-03-02 21:18:48 +0100443static void dhdsdio_release(dhd_bus_t *bus);
Arend van Spriel8da4a3a2011-03-02 21:18:42 +0100444static void dhdsdio_release_malloc(dhd_bus_t *bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700445static void dhdsdio_disconnect(void *ptr);
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700446static bool dhdsdio_chipmatch(u16 chipid);
Arend van Spriel8da4a3a2011-03-02 21:18:42 +0100447static bool dhdsdio_probe_attach(dhd_bus_t *bus, void *sdh,
448 void *regsva, u16 devid);
449static bool dhdsdio_probe_malloc(dhd_bus_t *bus, void *sdh);
450static bool dhdsdio_probe_init(dhd_bus_t *bus, void *sdh);
451static void dhdsdio_release_dongle(dhd_bus_t *bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700452
453static uint process_nvram_vars(char *varbuf, uint len);
454
455static void dhd_dongle_setmemsize(struct dhd_bus *bus, int mem_size);
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700456static int dhd_bcmsdh_send_buf(dhd_bus_t *bus, u32 addr, uint fn,
Arend van Sprielc26b1372010-11-23 14:06:23 +0100457 uint flags, u8 *buf, uint nbytes,
458 struct sk_buff *pkt, bcmsdh_cmplt_fn_t complete,
459 void *handle);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700460
Arend van Spriel8da4a3a2011-03-02 21:18:42 +0100461static bool dhdsdio_download_firmware(struct dhd_bus *bus, void *sdh);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700462static int _dhdsdio_download_firmware(struct dhd_bus *bus);
463
464static int dhdsdio_download_code_file(struct dhd_bus *bus, char *image_path);
465static int dhdsdio_download_nvram(struct dhd_bus *bus);
466#ifdef BCMEMBEDIMAGE
467static int dhdsdio_download_code_array(struct dhd_bus *bus);
468#endif
Franky Lineb5dc512011-04-25 19:34:04 -0700469static void dhdsdio_chip_disablecore(bcmsdh_info_t *sdh, u32 corebase);
Franky Lincb63e4c2011-04-25 15:45:08 -0700470static int dhdsdio_chip_attach(struct dhd_bus *bus, void *regs);
Franky Lineb5dc512011-04-25 19:34:04 -0700471static void dhdsdio_chip_resetcore(bcmsdh_info_t *sdh, u32 corebase);
Franky Lin5d0d7a92011-04-25 19:34:05 -0700472static void dhdsdio_sdiod_drive_strength_init(struct dhd_bus *bus,
473 u32 drivestrength);
Franky Lincee3cf42011-04-25 19:34:06 -0700474static void dhdsdio_chip_detach(struct dhd_bus *bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700475
Grant Grundler4b455e02011-05-04 09:59:47 -0700476/* Packet free applicable unconditionally for sdio and sdspi.
477 * Conditional if bufpool was present for gspi bus.
478 */
479static void dhdsdio_pktfree2(dhd_bus_t *bus, struct sk_buff *pkt)
480{
481 dhd_os_sdlock_rxq(bus->dhd);
482 if ((bus->bus != SPI_BUS) || bus->usebufpool)
Henry Ptasinskib33f0e22011-05-10 22:25:29 +0200483 bcm_pkt_buf_free_skb(pkt);
Grant Grundler4b455e02011-05-04 09:59:47 -0700484 dhd_os_sdunlock_rxq(bus->dhd);
485}
486
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700487static void dhd_dongle_setmemsize(struct dhd_bus *bus, int mem_size)
488{
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -0700489 s32 min_size = DONGLE_MIN_MEMSIZE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700490 /* Restrict the memsize to user specified limit */
491 DHD_ERROR(("user: Restrict the dongle ram size to %d, min %d\n",
492 dhd_dongle_memsize, min_size));
493 if ((dhd_dongle_memsize > min_size) &&
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -0700494 (dhd_dongle_memsize < (s32) bus->orig_ramsize))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700495 bus->ramsize = dhd_dongle_memsize;
496}
497
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700498static int dhdsdio_set_siaddr_window(dhd_bus_t *bus, u32 address)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700499{
500 int err = 0;
501 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW,
502 (address >> 8) & SBSDIO_SBADDRLOW_MASK, &err);
503 if (!err)
504 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRMID,
505 (address >> 16) & SBSDIO_SBADDRMID_MASK, &err);
506 if (!err)
507 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRHIGH,
508 (address >> 24) & SBSDIO_SBADDRHIGH_MASK,
509 &err);
510 return err;
511}
512
513/* Turn backplane clock on or off */
514static int dhdsdio_htclk(dhd_bus_t *bus, bool on, bool pendok)
515{
516 int err;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700517 u8 clkctl, clkreq, devctl;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700518 bcmsdh_info_t *sdh;
519
520 DHD_TRACE(("%s: Enter\n", __func__));
521
522#if defined(OOB_INTR_ONLY)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700523 pendok = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700524#endif
525 clkctl = 0;
526 sdh = bus->sdh;
527
528 if (on) {
529 /* Request HT Avail */
530 clkreq =
531 bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
532
Franky Linc05df632011-04-25 19:34:07 -0700533 if ((bus->ci->chip == BCM4329_CHIP_ID)
534 && (bus->ci->chiprev == 0))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700535 clkreq |= SBSDIO_FORCE_ALP;
536
537 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
538 clkreq, &err);
539 if (err) {
540 DHD_ERROR(("%s: HT Avail request error: %d\n",
541 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200542 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700543 }
544
Franky Linc05df632011-04-25 19:34:07 -0700545 if (pendok && ((bus->ci->buscoretype == PCMCIA_CORE_ID)
546 && (bus->ci->buscorerev == 9))) {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700547 u32 dummy, retries;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700548 R_SDREG(dummy, &bus->regs->clockctlstatus, retries);
549 }
550
551 /* Check current status */
552 clkctl =
553 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
554 &err);
555 if (err) {
556 DHD_ERROR(("%s: HT Avail read error: %d\n",
557 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200558 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700559 }
560
561 /* Go to pending and await interrupt if appropriate */
562 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
563 /* Allow only clock-available interrupt */
564 devctl =
565 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
566 &err);
567 if (err) {
568 DHD_ERROR(("%s: Devctl error setting CA: %d\n",
569 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200570 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700571 }
572
573 devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
574 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
575 devctl, &err);
576 DHD_INFO(("CLKCTL: set PENDING\n"));
577 bus->clkstate = CLK_PENDING;
578
Roland Vossena1c5ad82011-04-11 15:16:24 +0200579 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700580 } else if (bus->clkstate == CLK_PENDING) {
581 /* Cancel CA-only interrupt filter */
582 devctl =
583 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
584 &err);
585 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
586 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
587 devctl, &err);
588 }
589
590 /* Otherwise, wait here (polling) for HT Avail */
591 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
592 SPINWAIT_SLEEP(sdioh_spinwait_sleep,
593 ((clkctl =
594 bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
595 SBSDIO_FUNC1_CHIPCLKCSR,
596 &err)),
597 !SBSDIO_CLKAV(clkctl, bus->alp_only)),
598 PMU_MAX_TRANSITION_DLY);
599 }
600 if (err) {
601 DHD_ERROR(("%s: HT Avail request error: %d\n",
602 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200603 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700604 }
605 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
606 DHD_ERROR(("%s: HT Avail timeout (%d): clkctl 0x%02x\n",
607 __func__, PMU_MAX_TRANSITION_DLY, clkctl));
Roland Vossenb74ac122011-05-03 11:35:20 +0200608 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700609 }
610
611 /* Mark clock available */
612 bus->clkstate = CLK_AVAIL;
613 DHD_INFO(("CLKCTL: turned ON\n"));
614
615#if defined(DHD_DEBUG)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700616 if (bus->alp_only == true) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700617#if !defined(BCMLXSDMMC)
618 if (!SBSDIO_ALPONLY(clkctl)) {
619 DHD_ERROR(("%s: HT Clock, when ALP Only\n",
620 __func__));
621 }
622#endif /* !defined(BCMLXSDMMC) */
623 } else {
624 if (SBSDIO_ALPONLY(clkctl)) {
625 DHD_ERROR(("%s: HT Clock should be on.\n",
626 __func__));
627 }
628 }
629#endif /* defined (DHD_DEBUG) */
630
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700631 bus->activity = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700632 } else {
633 clkreq = 0;
634
635 if (bus->clkstate == CLK_PENDING) {
636 /* Cancel CA-only interrupt filter */
637 devctl =
638 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
639 &err);
640 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
641 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
642 devctl, &err);
643 }
644
645 bus->clkstate = CLK_SDONLY;
646 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
647 clkreq, &err);
648 DHD_INFO(("CLKCTL: turned OFF\n"));
649 if (err) {
650 DHD_ERROR(("%s: Failed access turning clock off: %d\n",
651 __func__, err));
Roland Vossenb74ac122011-05-03 11:35:20 +0200652 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700653 }
654 }
Roland Vossena1c5ad82011-04-11 15:16:24 +0200655 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700656}
657
658/* Change idle/active SD state */
659static int dhdsdio_sdclk(dhd_bus_t *bus, bool on)
660{
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700661 DHD_TRACE(("%s: Enter\n", __func__));
662
Franky Lin602a8ab2011-06-01 13:45:04 +0200663 if (on)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700664 bus->clkstate = CLK_SDONLY;
Franky Lin602a8ab2011-06-01 13:45:04 +0200665 else
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700666 bus->clkstate = CLK_NONE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700667
Roland Vossena1c5ad82011-04-11 15:16:24 +0200668 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700669}
670
671/* Transition SD and backplane clock readiness */
672static int dhdsdio_clkctl(dhd_bus_t *bus, uint target, bool pendok)
673{
674#ifdef DHD_DEBUG
675 uint oldstate = bus->clkstate;
676#endif /* DHD_DEBUG */
677
678 DHD_TRACE(("%s: Enter\n", __func__));
679
680 /* Early exit if we're already there */
681 if (bus->clkstate == target) {
682 if (target == CLK_AVAIL) {
683 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700684 bus->activity = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700685 }
Roland Vossena1c5ad82011-04-11 15:16:24 +0200686 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700687 }
688
689 switch (target) {
690 case CLK_AVAIL:
691 /* Make sure SD clock is available */
692 if (bus->clkstate == CLK_NONE)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700693 dhdsdio_sdclk(bus, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700694 /* Now request HT Avail on the backplane */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700695 dhdsdio_htclk(bus, true, pendok);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700696 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700697 bus->activity = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700698 break;
699
700 case CLK_SDONLY:
701 /* Remove HT request, or bring up SD clock */
702 if (bus->clkstate == CLK_NONE)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700703 dhdsdio_sdclk(bus, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700704 else if (bus->clkstate == CLK_AVAIL)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700705 dhdsdio_htclk(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700706 else
707 DHD_ERROR(("dhdsdio_clkctl: request for %d -> %d\n",
708 bus->clkstate, target));
709 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
710 break;
711
712 case CLK_NONE:
713 /* Make sure to remove HT request */
714 if (bus->clkstate == CLK_AVAIL)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700715 dhdsdio_htclk(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700716 /* Now remove the SD clock */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700717 dhdsdio_sdclk(bus, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700718 dhd_os_wd_timer(bus->dhd, 0);
719 break;
720 }
721#ifdef DHD_DEBUG
722 DHD_INFO(("dhdsdio_clkctl: %d -> %d\n", oldstate, bus->clkstate));
723#endif /* DHD_DEBUG */
724
Roland Vossena1c5ad82011-04-11 15:16:24 +0200725 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700726}
727
728int dhdsdio_bussleep(dhd_bus_t *bus, bool sleep)
729{
730 bcmsdh_info_t *sdh = bus->sdh;
731 sdpcmd_regs_t *regs = bus->regs;
732 uint retries = 0;
733
734 DHD_INFO(("dhdsdio_bussleep: request %s (currently %s)\n",
735 (sleep ? "SLEEP" : "WAKE"),
736 (bus->sleeping ? "SLEEP" : "WAKE")));
737
738 /* Done if we're already in the requested state */
739 if (sleep == bus->sleeping)
Roland Vossena1c5ad82011-04-11 15:16:24 +0200740 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700741
742 /* Going to sleep: set the alarm and turn off the lights... */
743 if (sleep) {
744 /* Don't sleep if something is pending */
745 if (bus->dpc_sched || bus->rxskip || pktq_len(&bus->txq))
Roland Vossene10d82d2011-05-03 11:35:19 +0200746 return -EBUSY;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700747
748 /* Disable SDIO interrupts (no longer interested) */
749 bcmsdh_intr_disable(bus->sdh);
750
751 /* Make sure the controller has the bus up */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700752 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700753
754 /* Tell device to start using OOB wakeup */
755 W_SDREG(SMB_USE_OOB, &regs->tosbmailbox, retries);
756 if (retries > retry_limit)
757 DHD_ERROR(("CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"));
758
759 /* Turn off our contribution to the HT clock request */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700760 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700761
762 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
763 SBSDIO_FORCE_HW_CLKREQ_OFF, NULL);
764
765 /* Isolate the bus */
Franky Linc05df632011-04-25 19:34:07 -0700766 if (bus->ci->chip != BCM4329_CHIP_ID
767 && bus->ci->chip != BCM4319_CHIP_ID) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700768 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
769 SBSDIO_DEVCTL_PADS_ISO, NULL);
770 }
771
772 /* Change state */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700773 bus->sleeping = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700774
775 } else {
776 /* Waking up: bus power up is ok, set local state */
777
778 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
779 0, NULL);
780
781 /* Force pad isolation off if possible
782 (in case power never toggled) */
Franky Linc05df632011-04-25 19:34:07 -0700783 if ((bus->ci->buscoretype == PCMCIA_CORE_ID)
784 && (bus->ci->buscorerev >= 10))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700785 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, 0,
786 NULL);
787
788 /* Make sure the controller has the bus up */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700789 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700790
791 /* Send misc interrupt to indicate OOB not needed */
792 W_SDREG(0, &regs->tosbmailboxdata, retries);
793 if (retries <= retry_limit)
794 W_SDREG(SMB_DEV_INT, &regs->tosbmailbox, retries);
795
796 if (retries > retry_limit)
797 DHD_ERROR(("CANNOT SIGNAL CHIP TO CLEAR OOB!!\n"));
798
799 /* Make sure we have SD bus access */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700800 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700801
802 /* Change state */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700803 bus->sleeping = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700804
805 /* Enable interrupts again */
806 if (bus->intr && (bus->dhd->busstate == DHD_BUS_DATA)) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700807 bus->intdis = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700808 bcmsdh_intr_enable(bus->sdh);
809 }
810 }
811
Roland Vossena1c5ad82011-04-11 15:16:24 +0200812 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700813}
814
815#if defined(OOB_INTR_ONLY)
816void dhd_enable_oob_intr(struct dhd_bus *bus, bool enable)
817{
818#if defined(HW_OOB)
819 bcmsdh_enable_hw_oob_intr(bus->sdh, enable);
820#else
821 sdpcmd_regs_t *regs = bus->regs;
822 uint retries = 0;
823
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700824 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700825 if (enable == true) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700826
827 /* Tell device to start using OOB wakeup */
828 W_SDREG(SMB_USE_OOB, &regs->tosbmailbox, retries);
829 if (retries > retry_limit)
830 DHD_ERROR(("CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"));
831
832 } else {
833 /* Send misc interrupt to indicate OOB not needed */
834 W_SDREG(0, &regs->tosbmailboxdata, retries);
835 if (retries <= retry_limit)
836 W_SDREG(SMB_DEV_INT, &regs->tosbmailbox, retries);
837 }
838
839 /* Turn off our contribution to the HT clock request */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700840 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700841#endif /* !defined(HW_OOB) */
842}
843#endif /* defined(OOB_INTR_ONLY) */
844
845#define BUS_WAKE(bus) \
846 do { \
847 if ((bus)->sleeping) \
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -0700848 dhdsdio_bussleep((bus), false); \
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700849 } while (0);
850
851/* Writes a HW/SW header into the packet and sends it. */
852/* Assumes: (a) header space already there, (b) caller holds lock */
Arend van Sprielc26b1372010-11-23 14:06:23 +0100853static int dhdsdio_txpkt(dhd_bus_t *bus, struct sk_buff *pkt, uint chan,
854 bool free_pkt)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700855{
856 int ret;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700857 u8 *frame;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700858 u16 len, pad = 0;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -0700859 u32 swheader;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700860 uint retries = 0;
861 bcmsdh_info_t *sdh;
Arend van Sprielc26b1372010-11-23 14:06:23 +0100862 struct sk_buff *new;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700863 int i;
864
865 DHD_TRACE(("%s: Enter\n", __func__));
866
867 sdh = bus->sdh;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700868
869 if (bus->dhd->dongle_reset) {
Roland Vossenb74ac122011-05-03 11:35:20 +0200870 ret = -EPERM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700871 goto done;
872 }
873
Arend van Spriel54991ad2010-11-23 14:06:24 +0100874 frame = (u8 *) (pkt->data);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700875
876 /* Add alignment padding, allocate new packet if needed */
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -0700877 pad = ((unsigned long)frame % DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700878 if (pad) {
Arend van Spriel3be727c2010-11-23 22:20:30 +0100879 if (skb_headroom(pkt) < pad) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700880 DHD_INFO(("%s: insufficient headroom %d for %d pad\n",
Arend van Spriel3be727c2010-11-23 22:20:30 +0100881 __func__, skb_headroom(pkt), pad));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700882 bus->dhd->tx_realloc++;
Henry Ptasinskib33f0e22011-05-10 22:25:29 +0200883 new = bcm_pkt_buf_get_skb(pkt->len + DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700884 if (!new) {
885 DHD_ERROR(("%s: couldn't allocate new %d-byte "
886 "packet\n",
Arend van Spriel54991ad2010-11-23 14:06:24 +0100887 __func__, pkt->len + DHD_SDALIGN));
Roland Vossene10d82d2011-05-03 11:35:19 +0200888 ret = -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700889 goto done;
890 }
891
Arend van Spriel3c9d4c32011-03-02 21:18:48 +0100892 PKTALIGN(new, pkt->len, DHD_SDALIGN);
Stanislav Fomichev02160692011-02-15 01:05:10 +0300893 memcpy(new->data, pkt->data, pkt->len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700894 if (free_pkt)
Henry Ptasinskib33f0e22011-05-10 22:25:29 +0200895 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700896 /* free the pkt if canned one is not used */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -0700897 free_pkt = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700898 pkt = new;
Arend van Spriel54991ad2010-11-23 14:06:24 +0100899 frame = (u8 *) (pkt->data);
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -0700900 ASSERT(((unsigned long)frame % DHD_SDALIGN) == 0);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700901 pad = 0;
902 } else {
Arend van Sprielc303ecb2010-11-18 20:46:43 +0100903 skb_push(pkt, pad);
Arend van Spriel54991ad2010-11-23 14:06:24 +0100904 frame = (u8 *) (pkt->data);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700905
Arend van Spriel54991ad2010-11-23 14:06:24 +0100906 ASSERT((pad + SDPCM_HDRLEN) <= (int)(pkt->len));
Brett Rudley9249ede2010-11-30 20:09:49 -0800907 memset(frame, 0, pad + SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700908 }
909 }
910 ASSERT(pad < DHD_SDALIGN);
911
912 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
Arend van Spriel54991ad2010-11-23 14:06:24 +0100913 len = (u16) (pkt->len);
Stanislav Fomichev628f10b2011-02-20 21:43:32 +0300914 *(u16 *) frame = cpu_to_le16(len);
915 *(((u16 *) frame) + 1) = cpu_to_le16(~len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700916
917 /* Software tag: channel, sequence number, data offset */
918 swheader =
919 ((chan << SDPCM_CHANNEL_SHIFT) & SDPCM_CHANNEL_MASK) | bus->tx_seq |
920 (((pad +
921 SDPCM_HDRLEN) << SDPCM_DOFFSET_SHIFT) & SDPCM_DOFFSET_MASK);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +0300922
923 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
924 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700925
926#ifdef DHD_DEBUG
Arend van Spriel54991ad2010-11-23 14:06:24 +0100927 tx_packets[pkt->priority]++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700928 if (DHD_BYTES_ON() &&
929 (((DHD_CTL_ON() && (chan == SDPCM_CONTROL_CHANNEL)) ||
930 (DHD_DATA_ON() && (chan != SDPCM_CONTROL_CHANNEL))))) {
Arend van Spriel34227312011-05-10 22:25:32 +0200931 printk(KERN_DEBUG "Tx Frame:\n");
932 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, frame, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700933 } else if (DHD_HDRS_ON()) {
Arend van Spriel34227312011-05-10 22:25:32 +0200934 printk(KERN_DEBUG "TxHdr:\n");
935 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
936 frame, min_t(u16, len, 16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700937 }
938#endif
939
940 /* Raise len to next SDIO block to eliminate tail command */
941 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -0700942 u16 pad = bus->blocksize - (len % bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700943 if ((pad <= bus->roundup) && (pad < bus->blocksize))
944#ifdef NOTUSED
Arend van Spriel3be727c2010-11-23 22:20:30 +0100945 if (pad <= skb_tailroom(pkt))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700946#endif /* NOTUSED */
947 len += pad;
948 } else if (len % DHD_SDALIGN) {
949 len += DHD_SDALIGN - (len % DHD_SDALIGN);
950 }
951
952 /* Some controllers have trouble with odd bytes -- round to even */
953 if (forcealign && (len & (ALIGNMENT - 1))) {
954#ifdef NOTUSED
Arend van Spriel3be727c2010-11-23 22:20:30 +0100955 if (skb_tailroom(pkt))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700956#endif
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -0700957 len = roundup(len, ALIGNMENT);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700958#ifdef NOTUSED
959 else
960 DHD_ERROR(("%s: sending unrounded %d-byte packet\n",
961 __func__, len));
962#endif
963 }
964
965 do {
966 ret =
967 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
968 F2SYNC, frame, len, pkt, NULL, NULL);
969 bus->f2txdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +0200970 ASSERT(ret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700971
972 if (ret < 0) {
973 /* On failure, abort the command
974 and terminate the frame */
975 DHD_INFO(("%s: sdio error %d, abort command and "
976 "terminate frame.\n", __func__, ret));
977 bus->tx_sderrs++;
978
979 bcmsdh_abort(sdh, SDIO_FUNC_2);
980 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
981 SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
982 NULL);
983 bus->f1regdata++;
984
985 for (i = 0; i < 3; i++) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -0700986 u8 hi, lo;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -0700987 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
988 SBSDIO_FUNC1_WFRAMEBCHI,
989 NULL);
990 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
991 SBSDIO_FUNC1_WFRAMEBCLO,
992 NULL);
993 bus->f1regdata += 2;
994 if ((hi == 0) && (lo == 0))
995 break;
996 }
997
998 }
999 if (ret == 0)
1000 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1001
1002 } while ((ret < 0) && retrydata && retries++ < TXRETRIES);
1003
1004done:
1005 /* restore pkt buffer pointer before calling tx complete routine */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001006 skb_pull(pkt, SDPCM_HDRLEN + pad);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001007 dhd_os_sdunlock(bus->dhd);
1008 dhd_txcomplete(bus->dhd, pkt, ret != 0);
1009 dhd_os_sdlock(bus->dhd);
1010
1011 if (free_pkt)
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02001012 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001013
1014 return ret;
1015}
1016
Arend van Sprielc26b1372010-11-23 14:06:23 +01001017int dhd_bus_txdata(struct dhd_bus *bus, struct sk_buff *pkt)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001018{
Roland Vossenb74ac122011-05-03 11:35:20 +02001019 int ret = -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001020 uint datalen, prec;
1021
1022 DHD_TRACE(("%s: Enter\n", __func__));
1023
Arend van Spriel54991ad2010-11-23 14:06:24 +01001024 datalen = pkt->len;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001025
1026#ifdef SDTEST
1027 /* Push the test header if doing loopback */
1028 if (bus->ext_loop) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001029 u8 *data;
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001030 skb_push(pkt, SDPCM_TEST_HDRLEN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01001031 data = pkt->data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001032 *data++ = SDPCM_TEST_ECHOREQ;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001033 *data++ = (u8) bus->loopid++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001034 *data++ = (datalen >> 0);
1035 *data++ = (datalen >> 8);
1036 datalen += SDPCM_TEST_HDRLEN;
1037 }
1038#endif /* SDTEST */
1039
1040 /* Add space for the header */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001041 skb_push(pkt, SDPCM_HDRLEN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01001042 ASSERT(IS_ALIGNED((unsigned long)(pkt->data), 2));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001043
Arend van Spriel54991ad2010-11-23 14:06:24 +01001044 prec = PRIO2PREC((pkt->priority & PRIOMASK));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001045
1046 /* Check for existing queue, current flow-control,
1047 pending event, or pending clock */
1048 if (dhd_deferred_tx || bus->fcstate || pktq_len(&bus->txq)
1049 || bus->dpc_sched || (!DATAOK(bus))
1050 || (bus->flowcontrol & NBITVAL(prec))
1051 || (bus->clkstate != CLK_AVAIL)) {
1052 DHD_TRACE(("%s: deferring pktq len %d\n", __func__,
1053 pktq_len(&bus->txq)));
1054 bus->fcqueued++;
1055
1056 /* Priority based enq */
1057 dhd_os_sdlock_txq(bus->dhd);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001058 if (dhd_prec_enq(bus->dhd, &bus->txq, pkt, prec) == false) {
Arend van Sprielc303ecb2010-11-18 20:46:43 +01001059 skb_pull(pkt, SDPCM_HDRLEN);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001060 dhd_txcomplete(bus->dhd, pkt, false);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02001061 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001062 DHD_ERROR(("%s: out of bus->txq !!!\n", __func__));
Roland Vossene10d82d2011-05-03 11:35:19 +02001063 ret = -ENOSR;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001064 } else {
Roland Vossena1c5ad82011-04-11 15:16:24 +02001065 ret = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001066 }
1067 dhd_os_sdunlock_txq(bus->dhd);
1068
Grant Grundler7c316072011-03-09 15:04:15 -08001069 if (pktq_len(&bus->txq) >= TXHI)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001070 dhd_txflowcontrol(bus->dhd, 0, ON);
1071
1072#ifdef DHD_DEBUG
1073 if (pktq_plen(&bus->txq, prec) > qcount[prec])
1074 qcount[prec] = pktq_plen(&bus->txq, prec);
1075#endif
1076 /* Schedule DPC if needed to send queued packet(s) */
1077 if (dhd_deferred_tx && !bus->dpc_sched) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001078 bus->dpc_sched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001079 dhd_sched_dpc(bus->dhd);
1080 }
1081 } else {
1082 /* Lock: we're about to use shared data/code (and SDIO) */
1083 dhd_os_sdlock(bus->dhd);
1084
1085 /* Otherwise, send it now */
1086 BUS_WAKE(bus);
1087 /* Make sure back plane ht clk is on, no pending allowed */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001088 dhdsdio_clkctl(bus, CLK_AVAIL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001089
1090#ifndef SDTEST
1091 DHD_TRACE(("%s: calling txpkt\n", __func__));
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001092 ret = dhdsdio_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001093#else
1094 ret = dhdsdio_txpkt(bus, pkt,
1095 (bus->ext_loop ? SDPCM_TEST_CHANNEL :
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001096 SDPCM_DATA_CHANNEL), true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001097#endif
1098 if (ret)
1099 bus->dhd->tx_errors++;
1100 else
1101 bus->dhd->dstats.tx_bytes += datalen;
1102
1103 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001104 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001105 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001106 }
1107
1108 dhd_os_sdunlock(bus->dhd);
1109 }
1110
1111 return ret;
1112}
1113
1114static uint dhdsdio_sendfromq(dhd_bus_t *bus, uint maxframes)
1115{
Arend van Sprielc26b1372010-11-23 14:06:23 +01001116 struct sk_buff *pkt;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001117 u32 intstatus = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001118 uint retries = 0;
1119 int ret = 0, prec_out;
1120 uint cnt = 0;
1121 uint datalen;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001122 u8 tx_prec_map;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001123
1124 dhd_pub_t *dhd = bus->dhd;
1125 sdpcmd_regs_t *regs = bus->regs;
1126
1127 DHD_TRACE(("%s: Enter\n", __func__));
1128
1129 tx_prec_map = ~bus->flowcontrol;
1130
1131 /* Send frames until the limit or some other event */
1132 for (cnt = 0; (cnt < maxframes) && DATAOK(bus); cnt++) {
1133 dhd_os_sdlock_txq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02001134 pkt = bcm_pktq_mdeq(&bus->txq, tx_prec_map, &prec_out);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001135 if (pkt == NULL) {
1136 dhd_os_sdunlock_txq(bus->dhd);
1137 break;
1138 }
1139 dhd_os_sdunlock_txq(bus->dhd);
Arend van Spriel54991ad2010-11-23 14:06:24 +01001140 datalen = pkt->len - SDPCM_HDRLEN;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001141
1142#ifndef SDTEST
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001143 ret = dhdsdio_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001144#else
1145 ret = dhdsdio_txpkt(bus, pkt,
1146 (bus->ext_loop ? SDPCM_TEST_CHANNEL :
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001147 SDPCM_DATA_CHANNEL), true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001148#endif
1149 if (ret)
1150 bus->dhd->tx_errors++;
1151 else
1152 bus->dhd->dstats.tx_bytes += datalen;
1153
1154 /* In poll mode, need to check for other events */
1155 if (!bus->intr && cnt) {
1156 /* Check device status, signal pending interrupt */
1157 R_SDREG(intstatus, &regs->intstatus, retries);
1158 bus->f2txdata++;
1159 if (bcmsdh_regfail(bus->sdh))
1160 break;
1161 if (intstatus & bus->hostintmask)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001162 bus->ipend = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001163 }
1164 }
1165
1166 /* Deflow-control stack if needed */
Grant Grundler7c316072011-03-09 15:04:15 -08001167 if (dhd->up && (dhd->busstate == DHD_BUS_DATA) &&
Grant Grundler26a71a42011-03-09 10:41:25 -08001168 dhd->txoff && (pktq_len(&bus->txq) < TXLOW))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001169 dhd_txflowcontrol(dhd, 0, OFF);
1170
1171 return cnt;
1172}
1173
Greg Kroah-Hartman580a0bd2010-10-05 11:09:48 -07001174int dhd_bus_txctl(struct dhd_bus *bus, unsigned char *msg, uint msglen)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001175{
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001176 u8 *frame;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07001177 u16 len;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001178 u32 swheader;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001179 uint retries = 0;
1180 bcmsdh_info_t *sdh = bus->sdh;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001181 u8 doff = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001182 int ret = -1;
1183 int i;
1184
1185 DHD_TRACE(("%s: Enter\n", __func__));
1186
1187 if (bus->dhd->dongle_reset)
1188 return -EIO;
1189
1190 /* Back the pointer to make a room for bus header */
1191 frame = msg - SDPCM_HDRLEN;
1192 len = (msglen += SDPCM_HDRLEN);
1193
1194 /* Add alignment padding (optional for ctl frames) */
1195 if (dhd_alignctl) {
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07001196 doff = ((unsigned long)frame % DHD_SDALIGN);
Jason Cooper9b890322010-09-30 15:15:39 -04001197 if (doff) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001198 frame -= doff;
1199 len += doff;
1200 msglen += doff;
Brett Rudley9249ede2010-11-30 20:09:49 -08001201 memset(frame, 0, doff + SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001202 }
1203 ASSERT(doff < DHD_SDALIGN);
1204 }
1205 doff += SDPCM_HDRLEN;
1206
1207 /* Round send length to next SDIO block */
1208 if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07001209 u16 pad = bus->blocksize - (len % bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001210 if ((pad <= bus->roundup) && (pad < bus->blocksize))
1211 len += pad;
1212 } else if (len % DHD_SDALIGN) {
1213 len += DHD_SDALIGN - (len % DHD_SDALIGN);
1214 }
1215
1216 /* Satisfy length-alignment requirements */
1217 if (forcealign && (len & (ALIGNMENT - 1)))
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07001218 len = roundup(len, ALIGNMENT);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001219
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07001220 ASSERT(IS_ALIGNED((unsigned long)frame, 2));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001221
1222 /* Need to lock here to protect txseq and SDIO tx calls */
1223 dhd_os_sdlock(bus->dhd);
1224
1225 BUS_WAKE(bus);
1226
1227 /* Make sure backplane clock is on */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001228 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001229
1230 /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03001231 *(u16 *) frame = cpu_to_le16((u16) msglen);
1232 *(((u16 *) frame) + 1) = cpu_to_le16(~msglen);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001233
1234 /* Software tag: channel, sequence number, data offset */
1235 swheader =
1236 ((SDPCM_CONTROL_CHANNEL << SDPCM_CHANNEL_SHIFT) &
1237 SDPCM_CHANNEL_MASK)
1238 | bus->tx_seq | ((doff << SDPCM_DOFFSET_SHIFT) &
1239 SDPCM_DOFFSET_MASK);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03001240 put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
1241 put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001242
1243 if (!DATAOK(bus)) {
1244 DHD_INFO(("%s: No bus credit bus->tx_max %d, bus->tx_seq %d\n",
1245 __func__, bus->tx_max, bus->tx_seq));
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001246 bus->ctrl_frame_stat = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001247 /* Send from dpc */
1248 bus->ctrl_frame_buf = frame;
1249 bus->ctrl_frame_len = len;
1250
1251 dhd_wait_for_event(bus->dhd, &bus->ctrl_frame_stat);
1252
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001253 if (bus->ctrl_frame_stat == false) {
1254 DHD_INFO(("%s: ctrl_frame_stat == false\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001255 ret = 0;
1256 } else {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001257 DHD_INFO(("%s: ctrl_frame_stat == true\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001258 ret = -1;
1259 }
1260 }
1261
1262 if (ret == -1) {
1263#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02001264 if (DHD_BYTES_ON() && DHD_CTL_ON()) {
1265 printk(KERN_DEBUG "Tx Frame:\n");
1266 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1267 frame, len);
1268 } else if (DHD_HDRS_ON()) {
1269 printk(KERN_DEBUG "TxHdr:\n");
1270 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
1271 frame, min_t(u16, len, 16));
1272 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001273#endif
1274
1275 do {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001276 bus->ctrl_frame_stat = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001277 ret =
1278 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh),
1279 SDIO_FUNC_2, F2SYNC, frame, len,
1280 NULL, NULL, NULL);
1281
Roland Vossenb7ef2a92011-05-03 11:35:02 +02001282 ASSERT(ret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001283
1284 if (ret < 0) {
1285 /* On failure, abort the command and
1286 terminate the frame */
1287 DHD_INFO(("%s: sdio error %d, abort command and terminate frame.\n",
1288 __func__, ret));
1289 bus->tx_sderrs++;
1290
1291 bcmsdh_abort(sdh, SDIO_FUNC_2);
1292
1293 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
1294 SBSDIO_FUNC1_FRAMECTRL,
1295 SFC_WF_TERM, NULL);
1296 bus->f1regdata++;
1297
1298 for (i = 0; i < 3; i++) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001299 u8 hi, lo;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001300 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1301 SBSDIO_FUNC1_WFRAMEBCHI,
1302 NULL);
1303 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1304 SBSDIO_FUNC1_WFRAMEBCLO,
1305 NULL);
1306 bus->f1regdata += 2;
1307 if ((hi == 0) && (lo == 0))
1308 break;
1309 }
1310
1311 }
1312 if (ret == 0) {
1313 bus->tx_seq =
1314 (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1315 }
1316 } while ((ret < 0) && retries++ < TXRETRIES);
1317 }
1318
1319 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001320 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001321 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001322 }
1323
1324 dhd_os_sdunlock(bus->dhd);
1325
1326 if (ret)
1327 bus->dhd->tx_ctlerrs++;
1328 else
1329 bus->dhd->tx_ctlpkts++;
1330
1331 return ret ? -EIO : 0;
1332}
1333
Greg Kroah-Hartman580a0bd2010-10-05 11:09:48 -07001334int dhd_bus_rxctl(struct dhd_bus *bus, unsigned char *msg, uint msglen)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001335{
1336 int timeleft;
1337 uint rxlen = 0;
1338 bool pending;
1339
1340 DHD_TRACE(("%s: Enter\n", __func__));
1341
1342 if (bus->dhd->dongle_reset)
1343 return -EIO;
1344
1345 /* Wait until control frame is available */
1346 timeleft = dhd_os_ioctl_resp_wait(bus->dhd, &bus->rxlen, &pending);
1347
1348 dhd_os_sdlock(bus->dhd);
1349 rxlen = bus->rxlen;
Stanislav Fomichev02160692011-02-15 01:05:10 +03001350 memcpy(msg, bus->rxctl, min(msglen, rxlen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001351 bus->rxlen = 0;
1352 dhd_os_sdunlock(bus->dhd);
1353
1354 if (rxlen) {
1355 DHD_CTL(("%s: resumed on rxctl frame, got %d expected %d\n",
1356 __func__, rxlen, msglen));
1357 } else if (timeleft == 0) {
1358 DHD_ERROR(("%s: resumed on timeout\n", __func__));
1359#ifdef DHD_DEBUG
1360 dhd_os_sdlock(bus->dhd);
1361 dhdsdio_checkdied(bus, NULL, 0);
1362 dhd_os_sdunlock(bus->dhd);
1363#endif /* DHD_DEBUG */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07001364 } else if (pending == true) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001365 DHD_CTL(("%s: cancelled\n", __func__));
1366 return -ERESTARTSYS;
1367 } else {
1368 DHD_CTL(("%s: resumed for unknown reason?\n", __func__));
1369#ifdef DHD_DEBUG
1370 dhd_os_sdlock(bus->dhd);
1371 dhdsdio_checkdied(bus, NULL, 0);
1372 dhd_os_sdunlock(bus->dhd);
1373#endif /* DHD_DEBUG */
1374 }
1375
1376 if (rxlen)
1377 bus->dhd->rx_ctlpkts++;
1378 else
1379 bus->dhd->rx_ctlerrs++;
1380
Jason Coopere9887c92010-10-06 10:08:02 -04001381 return rxlen ? (int)rxlen : -ETIMEDOUT;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001382}
1383
1384/* IOVar table */
1385enum {
1386 IOV_INTR = 1,
1387 IOV_POLLRATE,
1388 IOV_SDREG,
1389 IOV_SBREG,
1390 IOV_SDCIS,
1391 IOV_MEMBYTES,
1392 IOV_MEMSIZE,
1393#ifdef DHD_DEBUG
1394 IOV_CHECKDIED,
1395#endif
1396 IOV_DOWNLOAD,
1397 IOV_FORCEEVEN,
1398 IOV_SDIOD_DRIVE,
1399 IOV_READAHEAD,
1400 IOV_SDRXCHAIN,
1401 IOV_ALIGNCTL,
1402 IOV_SDALIGN,
1403 IOV_DEVRESET,
1404 IOV_CPU,
1405#ifdef SDTEST
1406 IOV_PKTGEN,
1407 IOV_EXTLOOP,
1408#endif /* SDTEST */
1409 IOV_SPROM,
1410 IOV_TXBOUND,
1411 IOV_RXBOUND,
1412 IOV_TXMINMAX,
1413 IOV_IDLETIME,
1414 IOV_IDLECLOCK,
1415 IOV_SD1IDLE,
1416 IOV_SLEEP,
1417 IOV_VARS
1418};
1419
1420const bcm_iovar_t dhdsdio_iovars[] = {
1421 {"intr", IOV_INTR, 0, IOVT_BOOL, 0},
1422 {"sleep", IOV_SLEEP, 0, IOVT_BOOL, 0},
1423 {"pollrate", IOV_POLLRATE, 0, IOVT_UINT32, 0},
1424 {"idletime", IOV_IDLETIME, 0, IOVT_INT32, 0},
1425 {"idleclock", IOV_IDLECLOCK, 0, IOVT_INT32, 0},
1426 {"sd1idle", IOV_SD1IDLE, 0, IOVT_BOOL, 0},
1427 {"membytes", IOV_MEMBYTES, 0, IOVT_BUFFER, 2 * sizeof(int)},
1428 {"memsize", IOV_MEMSIZE, 0, IOVT_UINT32, 0},
1429 {"download", IOV_DOWNLOAD, 0, IOVT_BOOL, 0},
1430 {"vars", IOV_VARS, 0, IOVT_BUFFER, 0},
1431 {"sdiod_drive", IOV_SDIOD_DRIVE, 0, IOVT_UINT32, 0},
1432 {"readahead", IOV_READAHEAD, 0, IOVT_BOOL, 0},
1433 {"sdrxchain", IOV_SDRXCHAIN, 0, IOVT_BOOL, 0},
1434 {"alignctl", IOV_ALIGNCTL, 0, IOVT_BOOL, 0},
1435 {"sdalign", IOV_SDALIGN, 0, IOVT_BOOL, 0},
1436 {"devreset", IOV_DEVRESET, 0, IOVT_BOOL, 0},
1437#ifdef DHD_DEBUG
1438 {"sdreg", IOV_SDREG, 0, IOVT_BUFFER, sizeof(sdreg_t)}
1439 ,
1440 {"sbreg", IOV_SBREG, 0, IOVT_BUFFER, sizeof(sdreg_t)}
1441 ,
1442 {"sd_cis", IOV_SDCIS, 0, IOVT_BUFFER, DHD_IOCTL_MAXLEN}
1443 ,
1444 {"forcealign", IOV_FORCEEVEN, 0, IOVT_BOOL, 0}
1445 ,
1446 {"txbound", IOV_TXBOUND, 0, IOVT_UINT32, 0}
1447 ,
1448 {"rxbound", IOV_RXBOUND, 0, IOVT_UINT32, 0}
1449 ,
1450 {"txminmax", IOV_TXMINMAX, 0, IOVT_UINT32, 0}
1451 ,
1452 {"cpu", IOV_CPU, 0, IOVT_BOOL, 0}
1453 ,
1454#ifdef DHD_DEBUG
1455 {"checkdied", IOV_CHECKDIED, 0, IOVT_BUFFER, 0}
1456 ,
1457#endif /* DHD_DEBUG */
1458#endif /* DHD_DEBUG */
1459#ifdef SDTEST
1460 {"extloop", IOV_EXTLOOP, 0, IOVT_BOOL, 0}
1461 ,
1462 {"pktgen", IOV_PKTGEN, 0, IOVT_BUFFER, sizeof(dhd_pktgen_t)}
1463 ,
1464#endif /* SDTEST */
1465
1466 {NULL, 0, 0, 0, 0}
1467};
1468
1469static void
1470dhd_dump_pct(struct bcmstrbuf *strbuf, char *desc, uint num, uint div)
1471{
1472 uint q1, q2;
1473
1474 if (!div) {
1475 bcm_bprintf(strbuf, "%s N/A", desc);
1476 } else {
1477 q1 = num / div;
1478 q2 = (100 * (num - (q1 * div))) / div;
1479 bcm_bprintf(strbuf, "%s %d.%02d", desc, q1, q2);
1480 }
1481}
1482
1483void dhd_bus_dump(dhd_pub_t *dhdp, struct bcmstrbuf *strbuf)
1484{
1485 dhd_bus_t *bus = dhdp->bus;
1486
1487 bcm_bprintf(strbuf, "Bus SDIO structure:\n");
1488 bcm_bprintf(strbuf,
1489 "hostintmask 0x%08x intstatus 0x%08x sdpcm_ver %d\n",
1490 bus->hostintmask, bus->intstatus, bus->sdpcm_ver);
1491 bcm_bprintf(strbuf,
1492 "fcstate %d qlen %d tx_seq %d, max %d, rxskip %d rxlen %d rx_seq %d\n",
1493 bus->fcstate, pktq_len(&bus->txq), bus->tx_seq, bus->tx_max,
1494 bus->rxskip, bus->rxlen, bus->rx_seq);
1495 bcm_bprintf(strbuf, "intr %d intrcount %d lastintrs %d spurious %d\n",
1496 bus->intr, bus->intrcount, bus->lastintrs, bus->spurious);
1497 bcm_bprintf(strbuf, "pollrate %d pollcnt %d regfails %d\n",
1498 bus->pollrate, bus->pollcnt, bus->regfails);
1499
1500 bcm_bprintf(strbuf, "\nAdditional counters:\n");
1501 bcm_bprintf(strbuf,
1502 "tx_sderrs %d fcqueued %d rxrtx %d rx_toolong %d rxc_errors %d\n",
1503 bus->tx_sderrs, bus->fcqueued, bus->rxrtx, bus->rx_toolong,
1504 bus->rxc_errors);
1505 bcm_bprintf(strbuf, "rx_hdrfail %d badhdr %d badseq %d\n",
1506 bus->rx_hdrfail, bus->rx_badhdr, bus->rx_badseq);
1507 bcm_bprintf(strbuf, "fc_rcvd %d, fc_xoff %d, fc_xon %d\n", bus->fc_rcvd,
1508 bus->fc_xoff, bus->fc_xon);
1509 bcm_bprintf(strbuf, "rxglomfail %d, rxglomframes %d, rxglompkts %d\n",
1510 bus->rxglomfail, bus->rxglomframes, bus->rxglompkts);
1511 bcm_bprintf(strbuf, "f2rx (hdrs/data) %d (%d/%d), f2tx %d f1regs %d\n",
1512 (bus->f2rxhdrs + bus->f2rxdata), bus->f2rxhdrs,
1513 bus->f2rxdata, bus->f2txdata, bus->f1regdata);
1514 {
1515 dhd_dump_pct(strbuf, "\nRx: pkts/f2rd", bus->dhd->rx_packets,
1516 (bus->f2rxhdrs + bus->f2rxdata));
1517 dhd_dump_pct(strbuf, ", pkts/f1sd", bus->dhd->rx_packets,
1518 bus->f1regdata);
1519 dhd_dump_pct(strbuf, ", pkts/sd", bus->dhd->rx_packets,
1520 (bus->f2rxhdrs + bus->f2rxdata + bus->f1regdata));
1521 dhd_dump_pct(strbuf, ", pkts/int", bus->dhd->rx_packets,
1522 bus->intrcount);
1523 bcm_bprintf(strbuf, "\n");
1524
1525 dhd_dump_pct(strbuf, "Rx: glom pct", (100 * bus->rxglompkts),
1526 bus->dhd->rx_packets);
1527 dhd_dump_pct(strbuf, ", pkts/glom", bus->rxglompkts,
1528 bus->rxglomframes);
1529 bcm_bprintf(strbuf, "\n");
1530
1531 dhd_dump_pct(strbuf, "Tx: pkts/f2wr", bus->dhd->tx_packets,
1532 bus->f2txdata);
1533 dhd_dump_pct(strbuf, ", pkts/f1sd", bus->dhd->tx_packets,
1534 bus->f1regdata);
1535 dhd_dump_pct(strbuf, ", pkts/sd", bus->dhd->tx_packets,
1536 (bus->f2txdata + bus->f1regdata));
1537 dhd_dump_pct(strbuf, ", pkts/int", bus->dhd->tx_packets,
1538 bus->intrcount);
1539 bcm_bprintf(strbuf, "\n");
1540
1541 dhd_dump_pct(strbuf, "Total: pkts/f2rw",
1542 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1543 (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata));
1544 dhd_dump_pct(strbuf, ", pkts/f1sd",
1545 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1546 bus->f1regdata);
1547 dhd_dump_pct(strbuf, ", pkts/sd",
1548 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1549 (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata +
1550 bus->f1regdata));
1551 dhd_dump_pct(strbuf, ", pkts/int",
1552 (bus->dhd->tx_packets + bus->dhd->rx_packets),
1553 bus->intrcount);
1554 bcm_bprintf(strbuf, "\n\n");
1555 }
1556
1557#ifdef SDTEST
1558 if (bus->pktgen_count) {
1559 bcm_bprintf(strbuf, "pktgen config and count:\n");
1560 bcm_bprintf(strbuf,
1561 "freq %d count %d print %d total %d min %d len %d\n",
1562 bus->pktgen_freq, bus->pktgen_count,
1563 bus->pktgen_print, bus->pktgen_total,
1564 bus->pktgen_minlen, bus->pktgen_maxlen);
1565 bcm_bprintf(strbuf, "send attempts %d rcvd %d fail %d\n",
1566 bus->pktgen_sent, bus->pktgen_rcvd,
1567 bus->pktgen_fail);
1568 }
1569#endif /* SDTEST */
1570#ifdef DHD_DEBUG
1571 bcm_bprintf(strbuf, "dpc_sched %d host interrupt%spending\n",
1572 bus->dpc_sched,
1573 (bcmsdh_intr_pending(bus->sdh) ? " " : " not "));
1574 bcm_bprintf(strbuf, "blocksize %d roundup %d\n", bus->blocksize,
1575 bus->roundup);
1576#endif /* DHD_DEBUG */
1577 bcm_bprintf(strbuf,
1578 "clkstate %d activity %d idletime %d idlecount %d sleeping %d\n",
1579 bus->clkstate, bus->activity, bus->idletime, bus->idlecount,
1580 bus->sleeping);
1581}
1582
1583void dhd_bus_clearcounts(dhd_pub_t *dhdp)
1584{
1585 dhd_bus_t *bus = (dhd_bus_t *) dhdp->bus;
1586
1587 bus->intrcount = bus->lastintrs = bus->spurious = bus->regfails = 0;
1588 bus->rxrtx = bus->rx_toolong = bus->rxc_errors = 0;
1589 bus->rx_hdrfail = bus->rx_badhdr = bus->rx_badseq = 0;
1590 bus->tx_sderrs = bus->fc_rcvd = bus->fc_xoff = bus->fc_xon = 0;
1591 bus->rxglomfail = bus->rxglomframes = bus->rxglompkts = 0;
1592 bus->f2rxhdrs = bus->f2rxdata = bus->f2txdata = bus->f1regdata = 0;
1593}
1594
1595#ifdef SDTEST
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001596static int dhdsdio_pktgen_get(dhd_bus_t *bus, u8 *arg)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001597{
1598 dhd_pktgen_t pktgen;
1599
1600 pktgen.version = DHD_PKTGEN_VERSION;
1601 pktgen.freq = bus->pktgen_freq;
1602 pktgen.count = bus->pktgen_count;
1603 pktgen.print = bus->pktgen_print;
1604 pktgen.total = bus->pktgen_total;
1605 pktgen.minlen = bus->pktgen_minlen;
1606 pktgen.maxlen = bus->pktgen_maxlen;
1607 pktgen.numsent = bus->pktgen_sent;
1608 pktgen.numrcvd = bus->pktgen_rcvd;
1609 pktgen.numfail = bus->pktgen_fail;
1610 pktgen.mode = bus->pktgen_mode;
1611 pktgen.stop = bus->pktgen_stop;
1612
Stanislav Fomichev02160692011-02-15 01:05:10 +03001613 memcpy(arg, &pktgen, sizeof(pktgen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001614
1615 return 0;
1616}
1617
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001618static int dhdsdio_pktgen_set(dhd_bus_t *bus, u8 *arg)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001619{
1620 dhd_pktgen_t pktgen;
1621 uint oldcnt, oldmode;
1622
Stanislav Fomichev02160692011-02-15 01:05:10 +03001623 memcpy(&pktgen, arg, sizeof(pktgen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001624 if (pktgen.version != DHD_PKTGEN_VERSION)
Roland Vossene10d82d2011-05-03 11:35:19 +02001625 return -EINVAL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001626
1627 oldcnt = bus->pktgen_count;
1628 oldmode = bus->pktgen_mode;
1629
1630 bus->pktgen_freq = pktgen.freq;
1631 bus->pktgen_count = pktgen.count;
1632 bus->pktgen_print = pktgen.print;
1633 bus->pktgen_total = pktgen.total;
1634 bus->pktgen_minlen = pktgen.minlen;
1635 bus->pktgen_maxlen = pktgen.maxlen;
1636 bus->pktgen_mode = pktgen.mode;
1637 bus->pktgen_stop = pktgen.stop;
1638
1639 bus->pktgen_tick = bus->pktgen_ptick = 0;
Greg Kroah-Hartman3ea2f4d2010-10-08 11:39:43 -07001640 bus->pktgen_len = max(bus->pktgen_len, bus->pktgen_minlen);
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07001641 bus->pktgen_len = min(bus->pktgen_len, bus->pktgen_maxlen);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001642
1643 /* Clear counts for a new pktgen (mode change, or was stopped) */
1644 if (bus->pktgen_count && (!oldcnt || oldmode != bus->pktgen_mode))
1645 bus->pktgen_sent = bus->pktgen_rcvd = bus->pktgen_fail = 0;
1646
1647 return 0;
1648}
1649#endif /* SDTEST */
1650
1651static int
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001652dhdsdio_membytes(dhd_bus_t *bus, bool write, u32 address, u8 *data,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001653 uint size)
1654{
1655 int bcmerror = 0;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001656 u32 sdaddr;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001657 uint dsize;
1658
1659 /* Determine initial transfer parameters */
1660 sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK;
1661 if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK)
1662 dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr);
1663 else
1664 dsize = size;
1665
1666 /* Set the backplane window to include the start address */
1667 bcmerror = dhdsdio_set_siaddr_window(bus, address);
1668 if (bcmerror) {
1669 DHD_ERROR(("%s: window change failed\n", __func__));
1670 goto xfer_done;
1671 }
1672
1673 /* Do the transfer(s) */
1674 while (size) {
1675 DHD_INFO(("%s: %s %d bytes at offset 0x%08x in window 0x%08x\n",
1676 __func__, (write ? "write" : "read"), dsize,
1677 sdaddr, (address & SBSDIO_SBWINDOW_MASK)));
1678 bcmerror =
1679 bcmsdh_rwdata(bus->sdh, write, sdaddr, data, dsize);
1680 if (bcmerror) {
1681 DHD_ERROR(("%s: membytes transfer failed\n", __func__));
1682 break;
1683 }
1684
1685 /* Adjust for next transfer (if any) */
1686 size -= dsize;
1687 if (size) {
1688 data += dsize;
1689 address += dsize;
1690 bcmerror = dhdsdio_set_siaddr_window(bus, address);
1691 if (bcmerror) {
1692 DHD_ERROR(("%s: window change failed\n",
1693 __func__));
1694 break;
1695 }
1696 sdaddr = 0;
Greg Kroah-Hartmanb61640d2010-10-08 12:37:39 -07001697 dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001698 }
1699 }
1700
1701xfer_done:
1702 /* Return the window to backplane enumeration space for core access */
1703 if (dhdsdio_set_siaddr_window(bus, bcmsdh_cur_sbwad(bus->sdh))) {
1704 DHD_ERROR(("%s: FAILED to set window back to 0x%x\n",
1705 __func__, bcmsdh_cur_sbwad(bus->sdh)));
1706 }
1707
1708 return bcmerror;
1709}
1710
1711#ifdef DHD_DEBUG
1712static int dhdsdio_readshared(dhd_bus_t *bus, sdpcm_shared_t *sh)
1713{
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001714 u32 addr;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001715 int rv;
1716
1717 /* Read last word in memory to determine address of
1718 sdpcm_shared structure */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001719 rv = dhdsdio_membytes(bus, false, bus->ramsize - 4, (u8 *)&addr, 4);
Jason Cooper9b890322010-09-30 15:15:39 -04001720 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001721 return rv;
1722
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03001723 addr = le32_to_cpu(addr);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001724
1725 DHD_INFO(("sdpcm_shared address 0x%08X\n", addr));
1726
1727 /*
1728 * Check if addr is valid.
1729 * NVRAM length at the end of memory should have been overwritten.
1730 */
1731 if (addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff)) {
1732 DHD_ERROR(("%s: address (0x%08x) of sdpcm_shared invalid\n",
1733 __func__, addr));
Roland Vossenb74ac122011-05-03 11:35:20 +02001734 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001735 }
1736
1737 /* Read hndrte_shared structure */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001738 rv = dhdsdio_membytes(bus, false, addr, (u8 *) sh,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001739 sizeof(sdpcm_shared_t));
1740 if (rv < 0)
1741 return rv;
1742
1743 /* Endianness */
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03001744 sh->flags = le32_to_cpu(sh->flags);
1745 sh->trap_addr = le32_to_cpu(sh->trap_addr);
1746 sh->assert_exp_addr = le32_to_cpu(sh->assert_exp_addr);
1747 sh->assert_file_addr = le32_to_cpu(sh->assert_file_addr);
1748 sh->assert_line = le32_to_cpu(sh->assert_line);
1749 sh->console_addr = le32_to_cpu(sh->console_addr);
1750 sh->msgtrace_addr = le32_to_cpu(sh->msgtrace_addr);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001751
1752 if ((sh->flags & SDPCM_SHARED_VERSION_MASK) != SDPCM_SHARED_VERSION) {
1753 DHD_ERROR(("%s: sdpcm_shared version %d in dhd "
1754 "is different than sdpcm_shared version %d in dongle\n",
1755 __func__, SDPCM_SHARED_VERSION,
1756 sh->flags & SDPCM_SHARED_VERSION_MASK));
Roland Vossenb74ac122011-05-03 11:35:20 +02001757 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001758 }
1759
Roland Vossena1c5ad82011-04-11 15:16:24 +02001760 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001761}
1762
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001763static int dhdsdio_checkdied(dhd_bus_t *bus, u8 *data, uint size)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001764{
1765 int bcmerror = 0;
1766 uint msize = 512;
1767 char *mbuffer = NULL;
1768 uint maxstrlen = 256;
1769 char *str = NULL;
1770 trap_t tr;
1771 sdpcm_shared_t sdpcm_shared;
1772 struct bcmstrbuf strbuf;
1773
1774 DHD_TRACE(("%s: Enter\n", __func__));
1775
1776 if (data == NULL) {
1777 /*
1778 * Called after a rx ctrl timeout. "data" is NULL.
1779 * allocate memory to trace the trap or assert.
1780 */
1781 size = msize;
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02001782 mbuffer = data = kmalloc(msize, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001783 if (mbuffer == NULL) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02001784 DHD_ERROR(("%s: kmalloc(%d) failed\n", __func__,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001785 msize));
Roland Vossene10d82d2011-05-03 11:35:19 +02001786 bcmerror = -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001787 goto done;
1788 }
1789 }
1790
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02001791 str = kmalloc(maxstrlen, GFP_ATOMIC);
Jason Cooper9b890322010-09-30 15:15:39 -04001792 if (str == NULL) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02001793 DHD_ERROR(("%s: kmalloc(%d) failed\n", __func__, maxstrlen));
Roland Vossene10d82d2011-05-03 11:35:19 +02001794 bcmerror = -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001795 goto done;
1796 }
1797
Jason Cooper9b890322010-09-30 15:15:39 -04001798 bcmerror = dhdsdio_readshared(bus, &sdpcm_shared);
1799 if (bcmerror < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001800 goto done;
1801
1802 bcm_binit(&strbuf, data, size);
1803
1804 bcm_bprintf(&strbuf,
1805 "msgtrace address : 0x%08X\nconsole address : 0x%08X\n",
1806 sdpcm_shared.msgtrace_addr, sdpcm_shared.console_addr);
1807
1808 if ((sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
1809 /* NOTE: Misspelled assert is intentional - DO NOT FIX.
1810 * (Avoids conflict with real asserts for programmatic
1811 * parsing of output.)
1812 */
1813 bcm_bprintf(&strbuf, "Assrt not built in dongle\n");
1814 }
1815
1816 if ((sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP)) ==
1817 0) {
1818 /* NOTE: Misspelled assert is intentional - DO NOT FIX.
1819 * (Avoids conflict with real asserts for programmatic
1820 * parsing of output.)
1821 */
1822 bcm_bprintf(&strbuf, "No trap%s in dongle",
1823 (sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT)
1824 ? "/assrt" : "");
1825 } else {
1826 if (sdpcm_shared.flags & SDPCM_SHARED_ASSERT) {
1827 /* Download assert */
1828 bcm_bprintf(&strbuf, "Dongle assert");
1829 if (sdpcm_shared.assert_exp_addr != 0) {
1830 str[0] = '\0';
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001831 bcmerror = dhdsdio_membytes(bus, false,
Jason Cooper9b890322010-09-30 15:15:39 -04001832 sdpcm_shared.assert_exp_addr,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001833 (u8 *) str, maxstrlen);
Jason Cooper9b890322010-09-30 15:15:39 -04001834 if (bcmerror < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001835 goto done;
1836
1837 str[maxstrlen - 1] = '\0';
1838 bcm_bprintf(&strbuf, " expr \"%s\"", str);
1839 }
1840
1841 if (sdpcm_shared.assert_file_addr != 0) {
1842 str[0] = '\0';
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001843 bcmerror = dhdsdio_membytes(bus, false,
Jason Cooper9b890322010-09-30 15:15:39 -04001844 sdpcm_shared.assert_file_addr,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001845 (u8 *) str, maxstrlen);
Jason Cooper9b890322010-09-30 15:15:39 -04001846 if (bcmerror < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001847 goto done;
1848
1849 str[maxstrlen - 1] = '\0';
1850 bcm_bprintf(&strbuf, " file \"%s\"", str);
1851 }
1852
1853 bcm_bprintf(&strbuf, " line %d ",
1854 sdpcm_shared.assert_line);
1855 }
1856
1857 if (sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001858 bcmerror = dhdsdio_membytes(bus, false,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001859 sdpcm_shared.trap_addr, (u8 *)&tr,
Jason Cooper9b890322010-09-30 15:15:39 -04001860 sizeof(trap_t));
1861 if (bcmerror < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001862 goto done;
1863
1864 bcm_bprintf(&strbuf,
1865 "Dongle trap type 0x%x @ epc 0x%x, cpsr 0x%x, spsr 0x%x, sp 0x%x,"
1866 "lp 0x%x, rpc 0x%x Trap offset 0x%x, "
1867 "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",
1868 tr.type, tr.epc, tr.cpsr, tr.spsr, tr.r13,
1869 tr.r14, tr.pc, sdpcm_shared.trap_addr,
1870 tr.r0, tr.r1, tr.r2, tr.r3, tr.r4, tr.r5,
1871 tr.r6, tr.r7);
1872 }
1873 }
1874
1875 if (sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP))
1876 DHD_ERROR(("%s: %s\n", __func__, strbuf.origbuf));
1877
1878#ifdef DHD_DEBUG
1879 if (sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
1880 /* Mem dump to a file on device */
1881 dhdsdio_mem_dump(bus);
1882 }
1883#endif /* DHD_DEBUG */
1884
1885done:
Ilia Mirkin46d994b2011-03-13 00:28:56 -05001886 kfree(mbuffer);
1887 kfree(str);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001888
1889 return bcmerror;
1890}
1891
1892static int dhdsdio_mem_dump(dhd_bus_t *bus)
1893{
1894 int ret = 0;
1895 int size; /* Full mem size */
1896 int start = 0; /* Start address */
1897 int read_size = 0; /* Read size of each iteration */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001898 u8 *buf = NULL, *databuf = NULL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001899
1900 /* Get full mem size */
1901 size = bus->ramsize;
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02001902 buf = kmalloc(size, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001903 if (!buf) {
Arend van Spriel0bef7742011-02-10 12:03:44 +01001904 DHD_ERROR(("%s: Out of memory (%d bytes)\n", __func__, size));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001905 return -1;
1906 }
1907
1908 /* Read mem content */
Arend van Spriel0bef7742011-02-10 12:03:44 +01001909 printk(KERN_DEBUG "Dump dongle memory");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001910 databuf = buf;
1911 while (size) {
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07001912 read_size = min(MEMBLOCK, size);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001913 ret = dhdsdio_membytes(bus, false, start, databuf, read_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001914 if (ret) {
Arend van Spriel0bef7742011-02-10 12:03:44 +01001915 DHD_ERROR(("%s: Error membytes %d\n", __func__, ret));
Ilia Mirkin46d994b2011-03-13 00:28:56 -05001916 kfree(buf);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001917 return -1;
1918 }
Arend van Spriel0bef7742011-02-10 12:03:44 +01001919 printk(".");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001920
1921 /* Decrement size and increment start address */
1922 size -= read_size;
1923 start += read_size;
1924 databuf += read_size;
1925 }
Arend van Spriel0bef7742011-02-10 12:03:44 +01001926 printk(KERN_DEBUG "Done\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001927
1928 /* free buf before return !!! */
1929 if (write_to_file(bus->dhd, buf, bus->ramsize)) {
Arend van Spriel0bef7742011-02-10 12:03:44 +01001930 DHD_ERROR(("%s: Error writing to files\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001931 return -1;
1932 }
1933
1934 /* buf free handled in write_to_file, not here */
1935 return 0;
1936}
1937
1938#define CONSOLE_LINE_MAX 192
1939
1940static int dhdsdio_readconsole(dhd_bus_t *bus)
1941{
1942 dhd_console_t *c = &bus->console;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07001943 u8 line[CONSOLE_LINE_MAX], ch;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07001944 u32 n, idx, addr;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001945 int rv;
1946
1947 /* Don't do anything until FWREADY updates console address */
1948 if (bus->console_addr == 0)
1949 return 0;
1950
1951 /* Read console log struct */
Greg Kroah-Hartmance0f1b82010-10-08 11:44:45 -07001952 addr = bus->console_addr + offsetof(hndrte_cons_t, log);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001953 rv = dhdsdio_membytes(bus, false, addr, (u8 *)&c->log,
Jason Cooper9b890322010-09-30 15:15:39 -04001954 sizeof(c->log));
1955 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001956 return rv;
1957
1958 /* Allocate console buffer (one time only) */
1959 if (c->buf == NULL) {
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03001960 c->bufsize = le32_to_cpu(c->log.buf_size);
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02001961 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
Jason Cooper9b890322010-09-30 15:15:39 -04001962 if (c->buf == NULL)
Roland Vossene10d82d2011-05-03 11:35:19 +02001963 return -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001964 }
1965
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03001966 idx = le32_to_cpu(c->log.idx);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001967
1968 /* Protect against corrupt value */
1969 if (idx > c->bufsize)
Roland Vossenb74ac122011-05-03 11:35:20 +02001970 return -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001971
1972 /* Skip reading the console buffer if the index pointer
1973 has not moved */
1974 if (idx == c->last)
Roland Vossena1c5ad82011-04-11 15:16:24 +02001975 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001976
1977 /* Read the console buffer */
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03001978 addr = le32_to_cpu(c->log.buf);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07001979 rv = dhdsdio_membytes(bus, false, addr, c->buf, c->bufsize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07001980 if (rv < 0)
1981 return rv;
1982
1983 while (c->last != idx) {
1984 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
1985 if (c->last == idx) {
1986 /* This would output a partial line.
1987 * Instead, back up
1988 * the buffer pointer and output this
1989 * line next time around.
1990 */
1991 if (c->last >= n)
1992 c->last -= n;
1993 else
1994 c->last = c->bufsize - n;
1995 goto break2;
1996 }
1997 ch = c->buf[c->last];
1998 c->last = (c->last + 1) % c->bufsize;
1999 if (ch == '\n')
2000 break;
2001 line[n] = ch;
2002 }
2003
2004 if (n > 0) {
2005 if (line[n - 1] == '\r')
2006 n--;
2007 line[n] = 0;
Arend van Spriel0bef7742011-02-10 12:03:44 +01002008 printk(KERN_DEBUG "CONSOLE: %s\n", line);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002009 }
2010 }
2011break2:
2012
Roland Vossena1c5ad82011-04-11 15:16:24 +02002013 return 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002014}
2015#endif /* DHD_DEBUG */
2016
2017int dhdsdio_downloadvars(dhd_bus_t *bus, void *arg, int len)
2018{
Roland Vossena1c5ad82011-04-11 15:16:24 +02002019 int bcmerror = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002020
2021 DHD_TRACE(("%s: Enter\n", __func__));
2022
2023 /* Basic sanity checks */
2024 if (bus->dhd->up) {
Roland Vossenb74ac122011-05-03 11:35:20 +02002025 bcmerror = -EISCONN;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002026 goto err;
2027 }
2028 if (!len) {
Roland Vossene10d82d2011-05-03 11:35:19 +02002029 bcmerror = -EOVERFLOW;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002030 goto err;
2031 }
2032
2033 /* Free the old ones and replace with passed variables */
Ilia Mirkin46d994b2011-03-13 00:28:56 -05002034 kfree(bus->vars);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002035
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002036 bus->vars = kmalloc(len, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002037 bus->varsz = bus->vars ? len : 0;
2038 if (bus->vars == NULL) {
Roland Vossene10d82d2011-05-03 11:35:19 +02002039 bcmerror = -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002040 goto err;
2041 }
2042
2043 /* Copy the passed variables, which should include the
2044 terminating double-null */
Stanislav Fomichev02160692011-02-15 01:05:10 +03002045 memcpy(bus->vars, arg, bus->varsz);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002046err:
2047 return bcmerror;
2048}
2049
2050static int
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002051dhdsdio_doiovar(dhd_bus_t *bus, const bcm_iovar_t *vi, u32 actionid,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002052 const char *name, void *params, int plen, void *arg, int len,
2053 int val_size)
2054{
2055 int bcmerror = 0;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002056 s32 int_val = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002057 bool bool_val = 0;
2058
2059 DHD_TRACE(("%s: Enter, action %d name %s params %p plen %d arg %p "
2060 "len %d val_size %d\n",
2061 __func__, actionid, name, params, plen, arg, len, val_size));
2062
2063 bcmerror = bcm_iovar_lencheck(vi, arg, len, IOV_ISSET(actionid));
2064 if (bcmerror != 0)
2065 goto exit;
2066
2067 if (plen >= (int)sizeof(int_val))
Stanislav Fomichev02160692011-02-15 01:05:10 +03002068 memcpy(&int_val, params, sizeof(int_val));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002069
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002070 bool_val = (int_val != 0) ? true : false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002071
2072 /* Some ioctls use the bus */
2073 dhd_os_sdlock(bus->dhd);
2074
2075 /* Check if dongle is in reset. If so, only allow DEVRESET iovars */
2076 if (bus->dhd->dongle_reset && !(actionid == IOV_SVAL(IOV_DEVRESET) ||
2077 actionid == IOV_GVAL(IOV_DEVRESET))) {
Roland Vossenb74ac122011-05-03 11:35:20 +02002078 bcmerror = -EPERM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002079 goto exit;
2080 }
2081
2082 /* Handle sleep stuff before any clock mucking */
2083 if (vi->varid == IOV_SLEEP) {
2084 if (IOV_ISSET(actionid)) {
2085 bcmerror = dhdsdio_bussleep(bus, bool_val);
2086 } else {
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002087 int_val = (s32) bus->sleeping;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002088 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002089 }
2090 goto exit;
2091 }
2092
2093 /* Request clock to allow SDIO accesses */
2094 if (!bus->dhd->dongle_reset) {
2095 BUS_WAKE(bus);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002096 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002097 }
2098
2099 switch (actionid) {
2100 case IOV_GVAL(IOV_INTR):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002101 int_val = (s32) bus->intr;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002102 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002103 break;
2104
2105 case IOV_SVAL(IOV_INTR):
2106 bus->intr = bool_val;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002107 bus->intdis = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002108 if (bus->dhd->up) {
2109 if (bus->intr) {
2110 DHD_INTR(("%s: enable SDIO device interrupts\n",
2111 __func__));
2112 bcmsdh_intr_enable(bus->sdh);
2113 } else {
2114 DHD_INTR(("%s: disable SDIO interrupts\n",
2115 __func__));
2116 bcmsdh_intr_disable(bus->sdh);
2117 }
2118 }
2119 break;
2120
2121 case IOV_GVAL(IOV_POLLRATE):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002122 int_val = (s32) bus->pollrate;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002123 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002124 break;
2125
2126 case IOV_SVAL(IOV_POLLRATE):
2127 bus->pollrate = (uint) int_val;
2128 bus->poll = (bus->pollrate != 0);
2129 break;
2130
2131 case IOV_GVAL(IOV_IDLETIME):
2132 int_val = bus->idletime;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002133 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002134 break;
2135
2136 case IOV_SVAL(IOV_IDLETIME):
2137 if ((int_val < 0) && (int_val != DHD_IDLE_IMMEDIATE))
Roland Vossene10d82d2011-05-03 11:35:19 +02002138 bcmerror = -EINVAL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002139 else
2140 bus->idletime = int_val;
2141 break;
2142
2143 case IOV_GVAL(IOV_IDLECLOCK):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002144 int_val = (s32) bus->idleclock;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002145 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002146 break;
2147
2148 case IOV_SVAL(IOV_IDLECLOCK):
2149 bus->idleclock = int_val;
2150 break;
2151
2152 case IOV_GVAL(IOV_SD1IDLE):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002153 int_val = (s32) sd1idle;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002154 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002155 break;
2156
2157 case IOV_SVAL(IOV_SD1IDLE):
2158 sd1idle = bool_val;
2159 break;
2160
2161 case IOV_SVAL(IOV_MEMBYTES):
2162 case IOV_GVAL(IOV_MEMBYTES):
2163 {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002164 u32 address;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002165 uint size, dsize;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002166 u8 *data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002167
2168 bool set = (actionid == IOV_SVAL(IOV_MEMBYTES));
2169
2170 ASSERT(plen >= 2 * sizeof(int));
2171
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002172 address = (u32) int_val;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002173 memcpy(&int_val, (char *)params + sizeof(int_val),
2174 sizeof(int_val));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002175 size = (uint) int_val;
2176
2177 /* Do some validation */
2178 dsize = set ? plen - (2 * sizeof(int)) : len;
2179 if (dsize < size) {
2180 DHD_ERROR(("%s: error on %s membytes, addr "
2181 "0x%08x size %d dsize %d\n",
2182 __func__, (set ? "set" : "get"),
2183 address, size, dsize));
Roland Vossene10d82d2011-05-03 11:35:19 +02002184 bcmerror = -EINVAL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002185 break;
2186 }
2187
2188 DHD_INFO(("%s: Request to %s %d bytes at address "
2189 "0x%08x\n",
2190 __func__, (set ? "write" : "read"), size, address));
2191
2192 /* If we know about SOCRAM, check for a fit */
2193 if ((bus->orig_ramsize) &&
2194 ((address > bus->orig_ramsize)
2195 || (address + size > bus->orig_ramsize))) {
2196 DHD_ERROR(("%s: ramsize 0x%08x doesn't have %d "
2197 "bytes at 0x%08x\n",
2198 __func__, bus->orig_ramsize, size, address));
Roland Vossene10d82d2011-05-03 11:35:19 +02002199 bcmerror = -EINVAL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002200 break;
2201 }
2202
2203 /* Generate the actual data pointer */
2204 data =
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002205 set ? (u8 *) params +
2206 2 * sizeof(int) : (u8 *) arg;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002207
2208 /* Call to do the transfer */
2209 bcmerror =
2210 dhdsdio_membytes(bus, set, address, data, size);
2211
2212 break;
2213 }
2214
2215 case IOV_GVAL(IOV_MEMSIZE):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002216 int_val = (s32) bus->ramsize;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002217 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002218 break;
2219
2220 case IOV_GVAL(IOV_SDIOD_DRIVE):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002221 int_val = (s32) dhd_sdiod_drive_strength;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002222 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002223 break;
2224
2225 case IOV_SVAL(IOV_SDIOD_DRIVE):
2226 dhd_sdiod_drive_strength = int_val;
Franky Lin5d0d7a92011-04-25 19:34:05 -07002227 dhdsdio_sdiod_drive_strength_init(bus,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002228 dhd_sdiod_drive_strength);
2229 break;
2230
2231 case IOV_SVAL(IOV_DOWNLOAD):
2232 bcmerror = dhdsdio_download_state(bus, bool_val);
2233 break;
2234
2235 case IOV_SVAL(IOV_VARS):
2236 bcmerror = dhdsdio_downloadvars(bus, arg, len);
2237 break;
2238
2239 case IOV_GVAL(IOV_READAHEAD):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002240 int_val = (s32) dhd_readahead;
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_READAHEAD):
2245 if (bool_val && !dhd_readahead)
2246 bus->nextlen = 0;
2247 dhd_readahead = bool_val;
2248 break;
2249
2250 case IOV_GVAL(IOV_SDRXCHAIN):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002251 int_val = (s32) bus->use_rxchain;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002252 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002253 break;
2254
2255 case IOV_SVAL(IOV_SDRXCHAIN):
2256 if (bool_val && !bus->sd_rxchain)
Roland Vossene10d82d2011-05-03 11:35:19 +02002257 bcmerror = -ENOTSUPP;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002258 else
2259 bus->use_rxchain = bool_val;
2260 break;
2261 case IOV_GVAL(IOV_ALIGNCTL):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002262 int_val = (s32) dhd_alignctl;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002263 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002264 break;
2265
2266 case IOV_SVAL(IOV_ALIGNCTL):
2267 dhd_alignctl = bool_val;
2268 break;
2269
2270 case IOV_GVAL(IOV_SDALIGN):
2271 int_val = DHD_SDALIGN;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002272 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002273 break;
2274
2275#ifdef DHD_DEBUG
2276 case IOV_GVAL(IOV_VARS):
2277 if (bus->varsz < (uint) len)
Stanislav Fomichev02160692011-02-15 01:05:10 +03002278 memcpy(arg, bus->vars, bus->varsz);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002279 else
Roland Vossene10d82d2011-05-03 11:35:19 +02002280 bcmerror = -EOVERFLOW;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002281 break;
2282#endif /* DHD_DEBUG */
2283
2284#ifdef DHD_DEBUG
2285 case IOV_GVAL(IOV_SDREG):
2286 {
2287 sdreg_t *sd_ptr;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002288 u32 addr, size;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002289
2290 sd_ptr = (sdreg_t *) params;
2291
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07002292 addr = (unsigned long)bus->regs + sd_ptr->offset;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002293 size = sd_ptr->func;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002294 int_val = (s32) bcmsdh_reg_read(bus->sdh, addr, size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002295 if (bcmsdh_regfail(bus->sdh))
Roland Vossenb74ac122011-05-03 11:35:20 +02002296 bcmerror = -EIO;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002297 memcpy(arg, &int_val, sizeof(s32));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002298 break;
2299 }
2300
2301 case IOV_SVAL(IOV_SDREG):
2302 {
2303 sdreg_t *sd_ptr;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002304 u32 addr, size;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002305
2306 sd_ptr = (sdreg_t *) params;
2307
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07002308 addr = (unsigned long)bus->regs + sd_ptr->offset;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002309 size = sd_ptr->func;
2310 bcmsdh_reg_write(bus->sdh, addr, size, sd_ptr->value);
2311 if (bcmsdh_regfail(bus->sdh))
Roland Vossenb74ac122011-05-03 11:35:20 +02002312 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002313 break;
2314 }
2315
2316 /* Same as above, but offset is not backplane
2317 (not SDIO core) */
2318 case IOV_GVAL(IOV_SBREG):
2319 {
2320 sdreg_t sdreg;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002321 u32 addr, size;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002322
Stanislav Fomichev02160692011-02-15 01:05:10 +03002323 memcpy(&sdreg, params, sizeof(sdreg));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002324
2325 addr = SI_ENUM_BASE + sdreg.offset;
2326 size = sdreg.func;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002327 int_val = (s32) bcmsdh_reg_read(bus->sdh, addr, size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002328 if (bcmsdh_regfail(bus->sdh))
Roland Vossenb74ac122011-05-03 11:35:20 +02002329 bcmerror = -EIO;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002330 memcpy(arg, &int_val, sizeof(s32));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002331 break;
2332 }
2333
2334 case IOV_SVAL(IOV_SBREG):
2335 {
2336 sdreg_t sdreg;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002337 u32 addr, size;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002338
Stanislav Fomichev02160692011-02-15 01:05:10 +03002339 memcpy(&sdreg, params, sizeof(sdreg));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002340
2341 addr = SI_ENUM_BASE + sdreg.offset;
2342 size = sdreg.func;
2343 bcmsdh_reg_write(bus->sdh, addr, size, sdreg.value);
2344 if (bcmsdh_regfail(bus->sdh))
Roland Vossenb74ac122011-05-03 11:35:20 +02002345 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002346 break;
2347 }
2348
2349 case IOV_GVAL(IOV_SDCIS):
2350 {
2351 *(char *)arg = 0;
2352
nohee koea3b8a22010-10-09 10:34:38 -07002353 strcat(arg, "\nFunc 0\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002354 bcmsdh_cis_read(bus->sdh, 0x10,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002355 (u8 *) arg + strlen(arg),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002356 SBSDIO_CIS_SIZE_LIMIT);
nohee koea3b8a22010-10-09 10:34:38 -07002357 strcat(arg, "\nFunc 1\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002358 bcmsdh_cis_read(bus->sdh, 0x11,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002359 (u8 *) arg + strlen(arg),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002360 SBSDIO_CIS_SIZE_LIMIT);
nohee koea3b8a22010-10-09 10:34:38 -07002361 strcat(arg, "\nFunc 2\n");
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002362 bcmsdh_cis_read(bus->sdh, 0x12,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002363 (u8 *) arg + strlen(arg),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002364 SBSDIO_CIS_SIZE_LIMIT);
2365 break;
2366 }
2367
2368 case IOV_GVAL(IOV_FORCEEVEN):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002369 int_val = (s32) forcealign;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002370 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002371 break;
2372
2373 case IOV_SVAL(IOV_FORCEEVEN):
2374 forcealign = bool_val;
2375 break;
2376
2377 case IOV_GVAL(IOV_TXBOUND):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002378 int_val = (s32) dhd_txbound;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002379 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002380 break;
2381
2382 case IOV_SVAL(IOV_TXBOUND):
2383 dhd_txbound = (uint) int_val;
2384 break;
2385
2386 case IOV_GVAL(IOV_RXBOUND):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002387 int_val = (s32) dhd_rxbound;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002388 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002389 break;
2390
2391 case IOV_SVAL(IOV_RXBOUND):
2392 dhd_rxbound = (uint) int_val;
2393 break;
2394
2395 case IOV_GVAL(IOV_TXMINMAX):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002396 int_val = (s32) dhd_txminmax;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002397 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002398 break;
2399
2400 case IOV_SVAL(IOV_TXMINMAX):
2401 dhd_txminmax = (uint) int_val;
2402 break;
2403#endif /* DHD_DEBUG */
2404
2405#ifdef SDTEST
2406 case IOV_GVAL(IOV_EXTLOOP):
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002407 int_val = (s32) bus->ext_loop;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002408 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002409 break;
2410
2411 case IOV_SVAL(IOV_EXTLOOP):
2412 bus->ext_loop = bool_val;
2413 break;
2414
2415 case IOV_GVAL(IOV_PKTGEN):
2416 bcmerror = dhdsdio_pktgen_get(bus, arg);
2417 break;
2418
2419 case IOV_SVAL(IOV_PKTGEN):
2420 bcmerror = dhdsdio_pktgen_set(bus, arg);
2421 break;
2422#endif /* SDTEST */
2423
2424 case IOV_SVAL(IOV_DEVRESET):
2425 DHD_TRACE(("%s: Called set IOV_DEVRESET=%d dongle_reset=%d "
2426 "busstate=%d\n",
2427 __func__, bool_val, bus->dhd->dongle_reset,
2428 bus->dhd->busstate));
2429
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002430 dhd_bus_devreset(bus->dhd, (u8) bool_val);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002431
2432 break;
2433
2434 case IOV_GVAL(IOV_DEVRESET):
2435 DHD_TRACE(("%s: Called get IOV_DEVRESET\n", __func__));
2436
2437 /* Get its status */
2438 int_val = (bool) bus->dhd->dongle_reset;
Stanislav Fomichev02160692011-02-15 01:05:10 +03002439 memcpy(arg, &int_val, val_size);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002440
2441 break;
2442
2443 default:
Roland Vossene10d82d2011-05-03 11:35:19 +02002444 bcmerror = -ENOTSUPP;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002445 break;
2446 }
2447
2448exit:
2449 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002450 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002451 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002452 }
2453
2454 dhd_os_sdunlock(bus->dhd);
2455
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002456 if (actionid == IOV_SVAL(IOV_DEVRESET) && bool_val == false)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002457 dhd_preinit_ioctls((dhd_pub_t *) bus->dhd);
2458
2459 return bcmerror;
2460}
2461
2462static int dhdsdio_write_vars(dhd_bus_t *bus)
2463{
2464 int bcmerror = 0;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002465 u32 varsize;
2466 u32 varaddr;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002467 u8 *vbuffer;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002468 u32 varsizew;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002469#ifdef DHD_DEBUG
2470 char *nvram_ularray;
2471#endif /* DHD_DEBUG */
2472
2473 /* Even if there are no vars are to be written, we still
2474 need to set the ramsize. */
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07002475 varsize = bus->varsz ? roundup(bus->varsz, 4) : 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002476 varaddr = (bus->ramsize - 4) - varsize;
2477
2478 if (bus->vars) {
Alexander Beregalov12d0eb42011-03-09 03:53:35 +03002479 vbuffer = kzalloc(varsize, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002480 if (!vbuffer)
Roland Vossene10d82d2011-05-03 11:35:19 +02002481 return -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002482
Stanislav Fomichev02160692011-02-15 01:05:10 +03002483 memcpy(vbuffer, bus->vars, bus->varsz);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002484
2485 /* Write the vars list */
2486 bcmerror =
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002487 dhdsdio_membytes(bus, true, varaddr, vbuffer, varsize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002488#ifdef DHD_DEBUG
2489 /* Verify NVRAM bytes */
2490 DHD_INFO(("Compare NVRAM dl & ul; varsize=%d\n", varsize));
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02002491 nvram_ularray = kmalloc(varsize, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002492 if (!nvram_ularray)
Roland Vossene10d82d2011-05-03 11:35:19 +02002493 return -ENOMEM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002494
2495 /* Upload image to verify downloaded contents. */
2496 memset(nvram_ularray, 0xaa, varsize);
2497
2498 /* Read the vars list to temp buffer for comparison */
2499 bcmerror =
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002500 dhdsdio_membytes(bus, false, varaddr, nvram_ularray,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002501 varsize);
2502 if (bcmerror) {
2503 DHD_ERROR(("%s: error %d on reading %d nvram bytes at "
2504 "0x%08x\n", __func__, bcmerror, varsize, varaddr));
2505 }
2506 /* Compare the org NVRAM with the one read from RAM */
2507 if (memcmp(vbuffer, nvram_ularray, varsize)) {
2508 DHD_ERROR(("%s: Downloaded NVRAM image is corrupted.\n",
2509 __func__));
2510 } else
2511 DHD_ERROR(("%s: Download/Upload/Compare of NVRAM ok.\n",
2512 __func__));
2513
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02002514 kfree(nvram_ularray);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002515#endif /* DHD_DEBUG */
2516
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02002517 kfree(vbuffer);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002518 }
2519
2520 /* adjust to the user specified RAM */
2521 DHD_INFO(("Physical memory size: %d, usable memory size: %d\n",
2522 bus->orig_ramsize, bus->ramsize));
2523 DHD_INFO(("Vars are at %d, orig varsize is %d\n", varaddr, varsize));
2524 varsize = ((bus->orig_ramsize - 4) - varaddr);
2525
2526 /*
2527 * Determine the length token:
2528 * Varsize, converted to words, in lower 16-bits, checksum
2529 * in upper 16-bits.
2530 */
2531 if (bcmerror) {
2532 varsizew = 0;
2533 } else {
2534 varsizew = varsize / 4;
2535 varsizew = (~varsizew << 16) | (varsizew & 0x0000FFFF);
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03002536 varsizew = cpu_to_le32(varsizew);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002537 }
2538
2539 DHD_INFO(("New varsize is %d, length token=0x%08x\n", varsize,
2540 varsizew));
2541
2542 /* Write the length token to the last word */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002543 bcmerror = dhdsdio_membytes(bus, true, (bus->orig_ramsize - 4),
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002544 (u8 *)&varsizew, 4);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002545
2546 return bcmerror;
2547}
2548
2549static int dhdsdio_download_state(dhd_bus_t *bus, bool enter)
2550{
2551 uint retries;
Franky Lineb5dc512011-04-25 19:34:04 -07002552 u32 regdata;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002553 int bcmerror = 0;
2554
2555 /* To enter download state, disable ARM and reset SOCRAM.
2556 * To exit download state, simply reset ARM (default is RAM boot).
2557 */
2558 if (enter) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002559 bus->alp_only = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002560
Franky Lineb5dc512011-04-25 19:34:04 -07002561 dhdsdio_chip_disablecore(bus->sdh, bus->ci->armcorebase);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002562
Franky Lineb5dc512011-04-25 19:34:04 -07002563 dhdsdio_chip_resetcore(bus->sdh, bus->ci->ramcorebase);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002564
2565 /* Clear the top bit of memory */
2566 if (bus->ramsize) {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002567 u32 zeros = 0;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002568 dhdsdio_membytes(bus, true, bus->ramsize - 4,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002569 (u8 *)&zeros, 4);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002570 }
2571 } else {
Franky Lineb5dc512011-04-25 19:34:04 -07002572 regdata = bcmsdh_reg_read(bus->sdh,
2573 CORE_SB(bus->ci->ramcorebase, sbtmstatelow), 4);
2574 regdata &= (SBTML_RESET | SBTML_REJ_MASK |
2575 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
2576 if ((SICF_CLOCK_EN << SBTML_SICF_SHIFT) != regdata) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002577 DHD_ERROR(("%s: SOCRAM core is down after reset?\n",
2578 __func__));
Roland Vossenb74ac122011-05-03 11:35:20 +02002579 bcmerror = -EBADE;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002580 goto fail;
2581 }
2582
2583 bcmerror = dhdsdio_write_vars(bus);
2584 if (bcmerror) {
2585 DHD_ERROR(("%s: no vars written to RAM\n", __func__));
2586 bcmerror = 0;
2587 }
2588
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002589 W_SDREG(0xFFFFFFFF, &bus->regs->intstatus, retries);
2590
Franky Lineb5dc512011-04-25 19:34:04 -07002591 dhdsdio_chip_resetcore(bus->sdh, bus->ci->armcorebase);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002592
2593 /* Allow HT Clock now that the ARM is running. */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002594 bus->alp_only = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002595
2596 bus->dhd->busstate = DHD_BUS_LOAD;
2597 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002598fail:
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002599 return bcmerror;
2600}
2601
2602int
2603dhd_bus_iovar_op(dhd_pub_t *dhdp, const char *name,
2604 void *params, int plen, void *arg, int len, bool set)
2605{
2606 dhd_bus_t *bus = dhdp->bus;
2607 const bcm_iovar_t *vi = NULL;
2608 int bcmerror = 0;
2609 int val_size;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002610 u32 actionid;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002611
2612 DHD_TRACE(("%s: Enter\n", __func__));
2613
2614 ASSERT(name);
2615 ASSERT(len >= 0);
2616
2617 /* Get MUST have return space */
2618 ASSERT(set || (arg && len));
2619
2620 /* Set does NOT take qualifiers */
2621 ASSERT(!set || (!params && !plen));
2622
2623 /* Look up var locally; if not found pass to host driver */
2624 vi = bcm_iovar_lookup(dhdsdio_iovars, name);
2625 if (vi == NULL) {
2626 dhd_os_sdlock(bus->dhd);
2627
2628 BUS_WAKE(bus);
2629
2630 /* Turn on clock in case SD command needs backplane */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002631 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002632
2633 bcmerror =
2634 bcmsdh_iovar_op(bus->sdh, name, params, plen, arg, len,
2635 set);
2636
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002637 /* Similar check for blocksize change */
2638 if (set && strcmp(name, "sd_blocksize") == 0) {
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002639 s32 fnum = 2;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002640 if (bcmsdh_iovar_op
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07002641 (bus->sdh, "sd_blocksize", &fnum, sizeof(s32),
2642 &bus->blocksize, sizeof(s32),
Roland Vossena1c5ad82011-04-11 15:16:24 +02002643 false) != 0) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002644 bus->blocksize = 0;
2645 DHD_ERROR(("%s: fail on %s get\n", __func__,
2646 "sd_blocksize"));
2647 } else {
2648 DHD_INFO(("%s: noted %s update, value now %d\n",
2649 __func__, "sd_blocksize",
2650 bus->blocksize));
2651 }
2652 }
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07002653 bus->roundup = min(max_roundup, bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002654
2655 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002656 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002657 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002658 }
2659
2660 dhd_os_sdunlock(bus->dhd);
2661 goto exit;
2662 }
2663
2664 DHD_CTL(("%s: %s %s, len %d plen %d\n", __func__,
2665 name, (set ? "set" : "get"), len, plen));
2666
2667 /* set up 'params' pointer in case this is a set command so that
2668 * the convenience int and bool code can be common to set and get
2669 */
2670 if (params == NULL) {
2671 params = arg;
2672 plen = len;
2673 }
2674
2675 if (vi->type == IOVT_VOID)
2676 val_size = 0;
2677 else if (vi->type == IOVT_BUFFER)
2678 val_size = len;
2679 else
2680 /* all other types are integer sized */
2681 val_size = sizeof(int);
2682
2683 actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
2684 bcmerror =
2685 dhdsdio_doiovar(bus, vi, actionid, name, params, plen, arg, len,
2686 val_size);
2687
2688exit:
2689 return bcmerror;
2690}
2691
2692void dhd_bus_stop(struct dhd_bus *bus, bool enforce_mutex)
2693{
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07002694 u32 local_hostintmask;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002695 u8 saveclk;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002696 uint retries;
2697 int err;
2698
2699 DHD_TRACE(("%s: Enter\n", __func__));
2700
2701 if (enforce_mutex)
2702 dhd_os_sdlock(bus->dhd);
2703
2704 BUS_WAKE(bus);
2705
2706 /* Enable clock for device interrupts */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002707 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002708
2709 /* Disable and clear interrupts at the chip level also */
2710 W_SDREG(0, &bus->regs->hostintmask, retries);
2711 local_hostintmask = bus->hostintmask;
2712 bus->hostintmask = 0;
2713
2714 /* Change our idea of bus state */
2715 bus->dhd->busstate = DHD_BUS_DOWN;
2716
2717 /* Force clocks on backplane to be sure F2 interrupt propagates */
2718 saveclk =
2719 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2720 &err);
2721 if (!err) {
2722 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2723 (saveclk | SBSDIO_FORCE_HT), &err);
2724 }
2725 if (err) {
2726 DHD_ERROR(("%s: Failed to force clock for F2: err %d\n",
2727 __func__, err));
2728 }
2729
2730 /* Turn off the bus (F2), free any pending packets */
2731 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
2732 bcmsdh_intr_disable(bus->sdh);
2733 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN,
2734 SDIO_FUNC_ENABLE_1, NULL);
2735
2736 /* Clear any pending interrupts now that F2 is disabled */
2737 W_SDREG(local_hostintmask, &bus->regs->intstatus, retries);
2738
2739 /* Turn off the backplane clock (only) */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002740 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002741
2742 /* Clear the data packet queues */
Clemens Noss84067de2011-05-15 22:45:30 +02002743 bcm_pktq_flush(&bus->txq, true, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002744
2745 /* Clear any held glomming stuff */
2746 if (bus->glomd)
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02002747 bcm_pkt_buf_free_skb(bus->glomd);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002748
2749 if (bus->glom)
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02002750 bcm_pkt_buf_free_skb(bus->glom);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002751
2752 bus->glom = bus->glomd = NULL;
2753
2754 /* Clear rx control and wake any waiters */
2755 bus->rxlen = 0;
2756 dhd_os_ioctl_resp_wake(bus->dhd);
2757
2758 /* Reset some F2 state stuff */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002759 bus->rxskip = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002760 bus->tx_seq = bus->rx_seq = 0;
2761
2762 if (enforce_mutex)
2763 dhd_os_sdunlock(bus->dhd);
2764}
2765
2766int dhd_bus_init(dhd_pub_t *dhdp, bool enforce_mutex)
2767{
2768 dhd_bus_t *bus = dhdp->bus;
2769 dhd_timeout_t tmo;
2770 uint retries = 0;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002771 u8 ready, enable;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002772 int err, ret = 0;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002773 u8 saveclk;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002774
2775 DHD_TRACE(("%s: Enter\n", __func__));
2776
2777 ASSERT(bus->dhd);
2778 if (!bus->dhd)
2779 return 0;
2780
2781 if (enforce_mutex)
2782 dhd_os_sdlock(bus->dhd);
2783
2784 /* Make sure backplane clock is on, needed to generate F2 interrupt */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002785 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002786 if (bus->clkstate != CLK_AVAIL)
2787 goto exit;
2788
2789 /* Force clocks on backplane to be sure F2 interrupt propagates */
2790 saveclk =
2791 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2792 &err);
2793 if (!err) {
2794 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2795 (saveclk | SBSDIO_FORCE_HT), &err);
2796 }
2797 if (err) {
2798 DHD_ERROR(("%s: Failed to force clock for F2: err %d\n",
2799 __func__, err));
2800 goto exit;
2801 }
2802
2803 /* Enable function 2 (frame transfers) */
2804 W_SDREG((SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT),
2805 &bus->regs->tosbmailboxdata, retries);
2806 enable = (SDIO_FUNC_ENABLE_1 | SDIO_FUNC_ENABLE_2);
2807
2808 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, enable, NULL);
2809
2810 /* Give the dongle some time to do its thing and set IOR2 */
2811 dhd_timeout_start(&tmo, DHD_WAIT_F2RDY * 1000);
2812
2813 ready = 0;
2814 while (ready != enable && !dhd_timeout_expired(&tmo))
2815 ready =
2816 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IORDY,
2817 NULL);
2818
2819 DHD_INFO(("%s: enable 0x%02x, ready 0x%02x (waited %uus)\n",
2820 __func__, enable, ready, tmo.elapsed));
2821
2822 /* If F2 successfully enabled, set core and enable interrupts */
2823 if (ready == enable) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002824 /* Set up the interrupt mask and enable interrupts */
2825 bus->hostintmask = HOSTINTMASK;
Franky Linc05df632011-04-25 19:34:07 -07002826 W_SDREG(bus->hostintmask,
2827 (unsigned int *)CORE_BUS_REG(bus->ci->buscorebase,
2828 hostintmask), retries);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002829
2830 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_WATERMARK,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002831 (u8) watermark, &err);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002832
2833 /* Set bus state according to enable result */
2834 dhdp->busstate = DHD_BUS_DATA;
2835
2836 /* bcmsdh_intr_unmask(bus->sdh); */
2837
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002838 bus->intdis = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002839 if (bus->intr) {
2840 DHD_INTR(("%s: enable SDIO device interrupts\n",
2841 __func__));
2842 bcmsdh_intr_enable(bus->sdh);
2843 } else {
2844 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
2845 bcmsdh_intr_disable(bus->sdh);
2846 }
2847
2848 }
2849
2850 else {
2851 /* Disable F2 again */
2852 enable = SDIO_FUNC_ENABLE_1;
2853 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, enable,
2854 NULL);
2855 }
2856
2857 /* Restore previous clock setting */
2858 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2859 saveclk, &err);
2860
2861 /* If we didn't come up, turn off backplane clock */
2862 if (dhdp->busstate != DHD_BUS_DATA)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002863 dhdsdio_clkctl(bus, CLK_NONE, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002864
2865exit:
2866 if (enforce_mutex)
2867 dhd_os_sdunlock(bus->dhd);
2868
2869 return ret;
2870}
2871
2872static void dhdsdio_rxfail(dhd_bus_t *bus, bool abort, bool rtx)
2873{
2874 bcmsdh_info_t *sdh = bus->sdh;
2875 sdpcmd_regs_t *regs = bus->regs;
2876 uint retries = 0;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07002877 u16 lastrbc;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002878 u8 hi, lo;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002879 int err;
2880
2881 DHD_ERROR(("%s: %sterminate frame%s\n", __func__,
2882 (abort ? "abort command, " : ""),
2883 (rtx ? ", send NAK" : "")));
2884
2885 if (abort)
2886 bcmsdh_abort(sdh, SDIO_FUNC_2);
2887
2888 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_FRAMECTRL, SFC_RF_TERM,
2889 &err);
2890 bus->f1regdata++;
2891
2892 /* Wait until the packet has been flushed (device/FIFO stable) */
2893 for (lastrbc = retries = 0xffff; retries > 0; retries--) {
2894 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_RFRAMEBCHI,
2895 NULL);
2896 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_RFRAMEBCLO,
2897 NULL);
2898 bus->f1regdata += 2;
2899
2900 if ((hi == 0) && (lo == 0))
2901 break;
2902
2903 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
2904 DHD_ERROR(("%s: count growing: last 0x%04x now "
2905 "0x%04x\n",
2906 __func__, lastrbc, ((hi << 8) + lo)));
2907 }
2908 lastrbc = (hi << 8) + lo;
2909 }
2910
2911 if (!retries) {
2912 DHD_ERROR(("%s: count never zeroed: last 0x%04x\n",
2913 __func__, lastrbc));
2914 } else {
2915 DHD_INFO(("%s: flush took %d iterations\n", __func__,
2916 (0xffff - retries)));
2917 }
2918
2919 if (rtx) {
2920 bus->rxrtx++;
2921 W_SDREG(SMB_NAK, &regs->tosbmailbox, retries);
2922 bus->f1regdata++;
2923 if (retries <= retry_limit)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07002924 bus->rxskip = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002925 }
2926
2927 /* Clear partial in any case */
2928 bus->nextlen = 0;
2929
2930 /* If we can't reach the device, signal failure */
2931 if (err || bcmsdh_regfail(sdh))
2932 bus->dhd->busstate = DHD_BUS_DOWN;
2933}
2934
2935static void
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07002936dhdsdio_read_control(dhd_bus_t *bus, u8 *hdr, uint len, uint doff)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002937{
2938 bcmsdh_info_t *sdh = bus->sdh;
2939 uint rdlen, pad;
2940
2941 int sdret;
2942
2943 DHD_TRACE(("%s: Enter\n", __func__));
2944
2945 /* Control data already received in aligned rxctl */
2946 if ((bus->bus == SPI_BUS) && (!bus->usebufpool))
2947 goto gotpkt;
2948
2949 ASSERT(bus->rxbuf);
2950 /* Set rxctl for frame (w/optional alignment) */
2951 bus->rxctl = bus->rxbuf;
2952 if (dhd_alignctl) {
2953 bus->rxctl += firstread;
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07002954 pad = ((unsigned long)bus->rxctl % DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002955 if (pad)
2956 bus->rxctl += (DHD_SDALIGN - pad);
2957 bus->rxctl -= firstread;
2958 }
2959 ASSERT(bus->rxctl >= bus->rxbuf);
2960
2961 /* Copy the already-read portion over */
Stanislav Fomichev02160692011-02-15 01:05:10 +03002962 memcpy(bus->rxctl, hdr, firstread);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002963 if (len <= firstread)
2964 goto gotpkt;
2965
2966 /* Copy the full data pkt in gSPI case and process ioctl. */
2967 if (bus->bus == SPI_BUS) {
Stanislav Fomichev02160692011-02-15 01:05:10 +03002968 memcpy(bus->rxctl, hdr, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002969 goto gotpkt;
2970 }
2971
2972 /* Raise rdlen to next SDIO block to avoid tail command */
2973 rdlen = len - firstread;
2974 if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
2975 pad = bus->blocksize - (rdlen % bus->blocksize);
2976 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
2977 ((len + pad) < bus->dhd->maxctl))
2978 rdlen += pad;
2979 } else if (rdlen % DHD_SDALIGN) {
2980 rdlen += DHD_SDALIGN - (rdlen % DHD_SDALIGN);
2981 }
2982
2983 /* Satisfy length-alignment requirements */
2984 if (forcealign && (rdlen & (ALIGNMENT - 1)))
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07002985 rdlen = roundup(rdlen, ALIGNMENT);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002986
2987 /* Drop if the read is too big or it exceeds our maximum */
2988 if ((rdlen + firstread) > bus->dhd->maxctl) {
2989 DHD_ERROR(("%s: %d-byte control read exceeds %d-byte buffer\n",
2990 __func__, rdlen, bus->dhd->maxctl));
2991 bus->dhd->rx_errors++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07002992 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07002993 goto done;
2994 }
2995
2996 if ((len - doff) > bus->dhd->maxctl) {
2997 DHD_ERROR(("%s: %d-byte ctl frame (%d-byte ctl data) exceeds "
2998 "%d-byte limit\n",
2999 __func__, len, (len - doff), bus->dhd->maxctl));
3000 bus->dhd->rx_errors++;
3001 bus->rx_toolong++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003002 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003003 goto done;
3004 }
3005
3006 /* Read remainder of frame body into the rxctl buffer */
Grant Grundler4b455e02011-05-04 09:59:47 -07003007 sdret = bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
3008 F2SYNC, (bus->rxctl + firstread), rdlen,
3009 NULL, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003010 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003011 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003012
3013 /* Control frame failures need retransmission */
3014 if (sdret < 0) {
3015 DHD_ERROR(("%s: read %d control bytes failed: %d\n",
3016 __func__, rdlen, sdret));
3017 bus->rxc_errors++; /* dhd.rx_ctlerrs is higher level */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003018 dhdsdio_rxfail(bus, true, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003019 goto done;
3020 }
3021
3022gotpkt:
3023
3024#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003025 if (DHD_BYTES_ON() && DHD_CTL_ON()) {
3026 printk(KERN_DEBUG "RxCtrl:\n");
3027 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, bus->rxctl, len);
3028 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003029#endif
3030
3031 /* Point to valid data and indicate its length */
3032 bus->rxctl += doff;
3033 bus->rxlen = len - doff;
3034
3035done:
3036 /* Awake any waiters */
3037 dhd_os_ioctl_resp_wake(bus->dhd);
3038}
3039
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003040static u8 dhdsdio_rxglom(dhd_bus_t *bus, u8 rxseq)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003041{
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003042 u16 dlen, totlen;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003043 u8 *dptr, num = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003044
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003045 u16 sublen, check;
Arend van Sprielc26b1372010-11-23 14:06:23 +01003046 struct sk_buff *pfirst, *plast, *pnext, *save_pfirst;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003047
3048 int errcode;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003049 u8 chan, seq, doff, sfdoff;
3050 u8 txmax;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003051
3052 int ifidx = 0;
3053 bool usechain = bus->use_rxchain;
3054
3055 /* If packets, issue read(s) and send up packet chain */
3056 /* Return sequence numbers consumed? */
3057
3058 DHD_TRACE(("dhdsdio_rxglom: start: glomd %p glom %p\n", bus->glomd,
3059 bus->glom));
3060
3061 /* If there's a descriptor, generate the packet chain */
3062 if (bus->glomd) {
3063 dhd_os_sdlock_rxq(bus->dhd);
3064
3065 pfirst = plast = pnext = NULL;
Arend van Spriel54991ad2010-11-23 14:06:24 +01003066 dlen = (u16) (bus->glomd->len);
3067 dptr = bus->glomd->data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003068 if (!dlen || (dlen & 1)) {
3069 DHD_ERROR(("%s: bad glomd len(%d), ignore descriptor\n",
3070 __func__, dlen));
3071 dlen = 0;
3072 }
3073
3074 for (totlen = num = 0; dlen; num++) {
3075 /* Get (and move past) next length */
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003076 sublen = get_unaligned_le16(dptr);
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003077 dlen -= sizeof(u16);
3078 dptr += sizeof(u16);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003079 if ((sublen < SDPCM_HDRLEN) ||
3080 ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
3081 DHD_ERROR(("%s: descriptor len %d bad: %d\n",
3082 __func__, num, sublen));
3083 pnext = NULL;
3084 break;
3085 }
3086 if (sublen % DHD_SDALIGN) {
3087 DHD_ERROR(("%s: sublen %d not multiple of %d\n",
3088 __func__, sublen, DHD_SDALIGN));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003089 usechain = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003090 }
3091 totlen += sublen;
3092
3093 /* For last frame, adjust read len so total
3094 is a block multiple */
3095 if (!dlen) {
3096 sublen +=
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003097 (roundup(totlen, bus->blocksize) - totlen);
3098 totlen = roundup(totlen, bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003099 }
3100
3101 /* Allocate/chain packet for next subframe */
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003102 pnext = bcm_pkt_buf_get_skb(sublen + DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003103 if (pnext == NULL) {
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003104 DHD_ERROR(("%s: bcm_pkt_buf_get_skb failed, "
3105 "num %d len %d\n", __func__,
3106 num, sublen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003107 break;
3108 }
Arend van Spriel54991ad2010-11-23 14:06:24 +01003109 ASSERT(!(pnext->prev));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003110 if (!pfirst) {
3111 ASSERT(!plast);
3112 pfirst = plast = pnext;
3113 } else {
3114 ASSERT(plast);
Arend van Spriel54991ad2010-11-23 14:06:24 +01003115 plast->next = pnext;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003116 plast = pnext;
3117 }
3118
3119 /* Adhere to start alignment requirements */
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01003120 PKTALIGN(pnext, sublen, DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003121 }
3122
3123 /* If all allocations succeeded, save packet chain
3124 in bus structure */
3125 if (pnext) {
3126 DHD_GLOM(("%s: allocated %d-byte packet chain for %d "
3127 "subframes\n", __func__, totlen, num));
3128 if (DHD_GLOM_ON() && bus->nextlen) {
3129 if (totlen != bus->nextlen) {
3130 DHD_GLOM(("%s: glomdesc mismatch: nextlen %d glomdesc %d " "rxseq %d\n",
3131 __func__, bus->nextlen,
3132 totlen, rxseq));
3133 }
3134 }
3135 bus->glom = pfirst;
3136 pfirst = pnext = NULL;
3137 } else {
3138 if (pfirst)
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003139 bcm_pkt_buf_free_skb(pfirst);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003140 bus->glom = NULL;
3141 num = 0;
3142 }
3143
3144 /* Done with descriptor packet */
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003145 bcm_pkt_buf_free_skb(bus->glomd);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003146 bus->glomd = NULL;
3147 bus->nextlen = 0;
3148
3149 dhd_os_sdunlock_rxq(bus->dhd);
3150 }
3151
3152 /* Ok -- either we just generated a packet chain,
3153 or had one from before */
3154 if (bus->glom) {
3155 if (DHD_GLOM_ON()) {
3156 DHD_GLOM(("%s: try superframe read, packet chain:\n",
3157 __func__));
Arend van Spriel54991ad2010-11-23 14:06:24 +01003158 for (pnext = bus->glom; pnext; pnext = pnext->next) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003159 DHD_GLOM((" %p: %p len 0x%04x (%d)\n",
Arend van Spriel54991ad2010-11-23 14:06:24 +01003160 pnext, (u8 *) (pnext->data),
3161 pnext->len, pnext->len));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003162 }
3163 }
3164
3165 pfirst = bus->glom;
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003166 dlen = (u16) bcm_pkttotlen(pfirst);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003167
3168 /* Do an SDIO read for the superframe. Configurable iovar to
3169 * read directly into the chained packet, or allocate a large
3170 * packet and and copy into the chain.
3171 */
3172 if (usechain) {
Grant Grundler4b455e02011-05-04 09:59:47 -07003173 errcode = bcmsdh_recv_buf(bus,
3174 bcmsdh_cur_sbwad(bus->sdh), SDIO_FUNC_2,
3175 F2SYNC, (u8 *) pfirst->data, dlen,
3176 pfirst, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003177 } else if (bus->dataptr) {
Grant Grundler4b455e02011-05-04 09:59:47 -07003178 errcode = bcmsdh_recv_buf(bus,
3179 bcmsdh_cur_sbwad(bus->sdh), SDIO_FUNC_2,
3180 F2SYNC, bus->dataptr, dlen,
3181 NULL, NULL, NULL);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003182 sublen = (u16) bcm_pktfrombuf(pfirst, 0, dlen,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003183 bus->dataptr);
3184 if (sublen != dlen) {
3185 DHD_ERROR(("%s: FAILED TO COPY, dlen %d sublen %d\n",
3186 __func__, dlen, sublen));
3187 errcode = -1;
3188 }
3189 pnext = NULL;
3190 } else {
3191 DHD_ERROR(("COULDN'T ALLOC %d-BYTE GLOM, FORCE FAILURE\n",
3192 dlen));
3193 errcode = -1;
3194 }
3195 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003196 ASSERT(errcode != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003197
3198 /* On failure, kill the superframe, allow a couple retries */
3199 if (errcode < 0) {
3200 DHD_ERROR(("%s: glom read of %d bytes failed: %d\n",
3201 __func__, dlen, errcode));
3202 bus->dhd->rx_errors++;
3203
3204 if (bus->glomerr++ < 3) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003205 dhdsdio_rxfail(bus, true, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003206 } else {
3207 bus->glomerr = 0;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003208 dhdsdio_rxfail(bus, true, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003209 dhd_os_sdlock_rxq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003210 bcm_pkt_buf_free_skb(bus->glom);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003211 dhd_os_sdunlock_rxq(bus->dhd);
3212 bus->rxglomfail++;
3213 bus->glom = NULL;
3214 }
3215 return 0;
3216 }
3217#ifdef DHD_DEBUG
3218 if (DHD_GLOM_ON()) {
Arend van Spriel34227312011-05-10 22:25:32 +02003219 printk(KERN_DEBUG "SUPERFRAME:\n");
3220 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3221 pfirst->data, min_t(int, pfirst->len, 48));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003222 }
3223#endif
3224
3225 /* Validate the superframe header */
Arend van Spriel54991ad2010-11-23 14:06:24 +01003226 dptr = (u8 *) (pfirst->data);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003227 sublen = get_unaligned_le16(dptr);
3228 check = get_unaligned_le16(dptr + sizeof(u16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003229
3230 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3231 seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
3232 bus->nextlen = dptr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
3233 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
3234 DHD_INFO(("%s: nextlen too large (%d) seq %d\n",
3235 __func__, bus->nextlen, seq));
3236 bus->nextlen = 0;
3237 }
3238 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3239 txmax = SDPCM_WINDOW_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3240
3241 errcode = 0;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003242 if ((u16)~(sublen ^ check)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003243 DHD_ERROR(("%s (superframe): HW hdr error: len/check "
3244 "0x%04x/0x%04x\n", __func__, sublen, check));
3245 errcode = -1;
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003246 } else if (roundup(sublen, bus->blocksize) != dlen) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003247 DHD_ERROR(("%s (superframe): len 0x%04x, rounded "
3248 "0x%04x, expect 0x%04x\n",
3249 __func__, sublen,
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003250 roundup(sublen, bus->blocksize), dlen));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003251 errcode = -1;
3252 } else if (SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]) !=
3253 SDPCM_GLOM_CHANNEL) {
3254 DHD_ERROR(("%s (superframe): bad channel %d\n",
3255 __func__,
3256 SDPCM_PACKET_CHANNEL(&dptr
3257 [SDPCM_FRAMETAG_LEN])));
3258 errcode = -1;
3259 } else if (SDPCM_GLOMDESC(&dptr[SDPCM_FRAMETAG_LEN])) {
3260 DHD_ERROR(("%s (superframe): got second descriptor?\n",
3261 __func__));
3262 errcode = -1;
3263 } else if ((doff < SDPCM_HDRLEN) ||
Arend van Spriel54991ad2010-11-23 14:06:24 +01003264 (doff > (pfirst->len - SDPCM_HDRLEN))) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003265 DHD_ERROR(("%s (superframe): Bad data offset %d: HW %d "
3266 "pkt %d min %d\n",
3267 __func__, doff, sublen,
Arend van Spriel54991ad2010-11-23 14:06:24 +01003268 pfirst->len, SDPCM_HDRLEN));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003269 errcode = -1;
3270 }
3271
3272 /* Check sequence number of superframe SW header */
3273 if (rxseq != seq) {
3274 DHD_INFO(("%s: (superframe) rx_seq %d, expected %d\n",
3275 __func__, seq, rxseq));
3276 bus->rx_badseq++;
3277 rxseq = seq;
3278 }
3279
3280 /* Check window for sanity */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003281 if ((u8) (txmax - bus->tx_seq) > 0x40) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003282 DHD_ERROR(("%s: unlikely tx max %d with tx_seq %d\n",
3283 __func__, txmax, bus->tx_seq));
3284 txmax = bus->tx_seq + 2;
3285 }
3286 bus->tx_max = txmax;
3287
3288 /* Remove superframe header, remember offset */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01003289 skb_pull(pfirst, doff);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003290 sfdoff = doff;
3291
3292 /* Validate all the subframe headers */
3293 for (num = 0, pnext = pfirst; pnext && !errcode;
Arend van Spriel54991ad2010-11-23 14:06:24 +01003294 num++, pnext = pnext->next) {
3295 dptr = (u8 *) (pnext->data);
3296 dlen = (u16) (pnext->len);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003297 sublen = get_unaligned_le16(dptr);
3298 check = get_unaligned_le16(dptr + sizeof(u16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003299 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3300 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3301#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003302 if (DHD_GLOM_ON()) {
3303 printk(KERN_DEBUG "subframe:\n");
3304 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3305 dptr, 32);
3306 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003307#endif
3308
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003309 if ((u16)~(sublen ^ check)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003310 DHD_ERROR(("%s (subframe %d): HW hdr error: "
3311 "len/check 0x%04x/0x%04x\n",
3312 __func__, num, sublen, check));
3313 errcode = -1;
3314 } else if ((sublen > dlen) || (sublen < SDPCM_HDRLEN)) {
3315 DHD_ERROR(("%s (subframe %d): length mismatch: "
3316 "len 0x%04x, expect 0x%04x\n",
3317 __func__, num, sublen, dlen));
3318 errcode = -1;
3319 } else if ((chan != SDPCM_DATA_CHANNEL) &&
3320 (chan != SDPCM_EVENT_CHANNEL)) {
3321 DHD_ERROR(("%s (subframe %d): bad channel %d\n",
3322 __func__, num, chan));
3323 errcode = -1;
3324 } else if ((doff < SDPCM_HDRLEN) || (doff > sublen)) {
3325 DHD_ERROR(("%s (subframe %d): Bad data offset %d: HW %d min %d\n",
3326 __func__, num, doff, sublen,
3327 SDPCM_HDRLEN));
3328 errcode = -1;
3329 }
3330 }
3331
3332 if (errcode) {
3333 /* Terminate frame on error, request
3334 a couple retries */
3335 if (bus->glomerr++ < 3) {
3336 /* Restore superframe header space */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01003337 skb_push(pfirst, sfdoff);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003338 dhdsdio_rxfail(bus, true, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003339 } else {
3340 bus->glomerr = 0;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003341 dhdsdio_rxfail(bus, true, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003342 dhd_os_sdlock_rxq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003343 bcm_pkt_buf_free_skb(bus->glom);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003344 dhd_os_sdunlock_rxq(bus->dhd);
3345 bus->rxglomfail++;
3346 bus->glom = NULL;
3347 }
3348 bus->nextlen = 0;
3349 return 0;
3350 }
3351
3352 /* Basic SD framing looks ok - process each packet (header) */
3353 save_pfirst = pfirst;
3354 bus->glom = NULL;
3355 plast = NULL;
3356
3357 dhd_os_sdlock_rxq(bus->dhd);
3358 for (num = 0; pfirst; rxseq++, pfirst = pnext) {
Arend van Spriel54991ad2010-11-23 14:06:24 +01003359 pnext = pfirst->next;
3360 pfirst->next = NULL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003361
Arend van Spriel54991ad2010-11-23 14:06:24 +01003362 dptr = (u8 *) (pfirst->data);
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003363 sublen = get_unaligned_le16(dptr);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003364 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3365 seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
3366 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3367
3368 DHD_GLOM(("%s: Get subframe %d, %p(%p/%d), sublen %d "
3369 "chan %d seq %d\n",
Arend van Spriel54991ad2010-11-23 14:06:24 +01003370 __func__, num, pfirst, pfirst->data,
3371 pfirst->len, sublen, chan, seq));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003372
3373 ASSERT((chan == SDPCM_DATA_CHANNEL)
3374 || (chan == SDPCM_EVENT_CHANNEL));
3375
3376 if (rxseq != seq) {
3377 DHD_GLOM(("%s: rx_seq %d, expected %d\n",
3378 __func__, seq, rxseq));
3379 bus->rx_badseq++;
3380 rxseq = seq;
3381 }
3382#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003383 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
3384 printk(KERN_DEBUG "Rx Subframe Data:\n");
3385 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3386 dptr, dlen);
3387 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003388#endif
3389
Arend van Spriel2cb8ada2010-11-18 20:46:44 +01003390 __skb_trim(pfirst, sublen);
Arend van Sprielc303ecb2010-11-18 20:46:43 +01003391 skb_pull(pfirst, doff);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003392
Arend van Spriel54991ad2010-11-23 14:06:24 +01003393 if (pfirst->len == 0) {
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003394 bcm_pkt_buf_free_skb(pfirst);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003395 if (plast) {
Arend van Spriel54991ad2010-11-23 14:06:24 +01003396 plast->next = pnext;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003397 } else {
3398 ASSERT(save_pfirst == pfirst);
3399 save_pfirst = pnext;
3400 }
3401 continue;
3402 } else if (dhd_prot_hdrpull(bus->dhd, &ifidx, pfirst) !=
3403 0) {
3404 DHD_ERROR(("%s: rx protocol error\n",
3405 __func__));
3406 bus->dhd->rx_errors++;
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003407 bcm_pkt_buf_free_skb(pfirst);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003408 if (plast) {
Arend van Spriel54991ad2010-11-23 14:06:24 +01003409 plast->next = pnext;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003410 } else {
3411 ASSERT(save_pfirst == pfirst);
3412 save_pfirst = pnext;
3413 }
3414 continue;
3415 }
3416
3417 /* this packet will go up, link back into
3418 chain and count it */
Arend van Spriel54991ad2010-11-23 14:06:24 +01003419 pfirst->next = pnext;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003420 plast = pfirst;
3421 num++;
3422
3423#ifdef DHD_DEBUG
3424 if (DHD_GLOM_ON()) {
3425 DHD_GLOM(("%s subframe %d to stack, %p(%p/%d) "
3426 "nxt/lnk %p/%p\n",
Arend van Spriel54991ad2010-11-23 14:06:24 +01003427 __func__, num, pfirst, pfirst->data,
3428 pfirst->len, pfirst->next,
3429 pfirst->prev));
Arend van Spriel34227312011-05-10 22:25:32 +02003430 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3431 pfirst->data,
3432 min_t(int, pfirst->len, 32));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003433 }
3434#endif /* DHD_DEBUG */
3435 }
3436 dhd_os_sdunlock_rxq(bus->dhd);
3437 if (num) {
3438 dhd_os_sdunlock(bus->dhd);
3439 dhd_rx_frame(bus->dhd, ifidx, save_pfirst, num);
3440 dhd_os_sdlock(bus->dhd);
3441 }
3442
3443 bus->rxglomframes++;
3444 bus->rxglompkts += num;
3445 }
3446 return num;
3447}
3448
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003449/* Return true if there may be more frames to read */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003450static uint dhdsdio_readframes(dhd_bus_t *bus, uint maxframes, bool *finished)
3451{
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003452 bcmsdh_info_t *sdh = bus->sdh;
3453
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003454 u16 len, check; /* Extracted hardware header fields */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003455 u8 chan, seq, doff; /* Extracted software header fields */
3456 u8 fcbits; /* Extracted fcbits from software header */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003457
Arend van Sprielc26b1372010-11-23 14:06:23 +01003458 struct sk_buff *pkt; /* Packet for event or data frames */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003459 u16 pad; /* Number of pad bytes to read */
3460 u16 rdlen; /* Total number of bytes to read */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003461 u8 rxseq; /* Next sequence number to expect */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003462 uint rxleft = 0; /* Remaining number of frames allowed */
3463 int sdret; /* Return code from bcmsdh calls */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003464 u8 txmax; /* Maximum tx sequence offered */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003465 bool len_consistent; /* Result of comparing readahead len and
3466 len from hw-hdr */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003467 u8 *rxbuf;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003468 int ifidx = 0;
3469 uint rxcount = 0; /* Total frames read */
3470
3471#if defined(DHD_DEBUG) || defined(SDTEST)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003472 bool sdtest = false; /* To limit message spew from test mode */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003473#endif
3474
3475 DHD_TRACE(("%s: Enter\n", __func__));
3476
3477 ASSERT(maxframes);
3478
3479#ifdef SDTEST
3480 /* Allow pktgen to override maxframes */
3481 if (bus->pktgen_count && (bus->pktgen_mode == DHD_PKTGEN_RECV)) {
3482 maxframes = bus->pktgen_count;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003483 sdtest = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003484 }
3485#endif
3486
3487 /* Not finished unless we encounter no more frames indication */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003488 *finished = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003489
3490 for (rxseq = bus->rx_seq, rxleft = maxframes;
3491 !bus->rxskip && rxleft && bus->dhd->busstate != DHD_BUS_DOWN;
3492 rxseq++, rxleft--) {
3493
3494 /* Handle glomming separately */
3495 if (bus->glom || bus->glomd) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003496 u8 cnt;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003497 DHD_GLOM(("%s: calling rxglom: glomd %p, glom %p\n",
3498 __func__, bus->glomd, bus->glom));
3499 cnt = dhdsdio_rxglom(bus, rxseq);
3500 DHD_GLOM(("%s: rxglom returned %d\n", __func__, cnt));
3501 rxseq += cnt - 1;
3502 rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
3503 continue;
3504 }
3505
3506 /* Try doing single read if we can */
3507 if (dhd_readahead && bus->nextlen) {
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003508 u16 nextlen = bus->nextlen;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003509 bus->nextlen = 0;
3510
3511 if (bus->bus == SPI_BUS) {
3512 rdlen = len = nextlen;
3513 } else {
3514 rdlen = len = nextlen << 4;
3515
3516 /* Pad read to blocksize for efficiency */
3517 if (bus->roundup && bus->blocksize
3518 && (rdlen > bus->blocksize)) {
3519 pad =
3520 bus->blocksize -
3521 (rdlen % bus->blocksize);
3522 if ((pad <= bus->roundup)
3523 && (pad < bus->blocksize)
3524 && ((rdlen + pad + firstread) <
3525 MAX_RX_DATASZ))
3526 rdlen += pad;
3527 } else if (rdlen % DHD_SDALIGN) {
3528 rdlen +=
3529 DHD_SDALIGN - (rdlen % DHD_SDALIGN);
3530 }
3531 }
3532
3533 /* We use bus->rxctl buffer in WinXP for initial
3534 * control pkt receives.
3535 * Later we use buffer-poll for data as well
3536 * as control packets.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003537 * This is required because dhd receives full
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003538 * frame in gSPI unlike SDIO.
3539 * After the frame is received we have to
3540 * distinguish whether it is data
3541 * or non-data frame.
3542 */
3543 /* Allocate a packet buffer */
3544 dhd_os_sdlock_rxq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003545 pkt = bcm_pkt_buf_get_skb(rdlen + DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003546 if (!pkt) {
3547 if (bus->bus == SPI_BUS) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003548 bus->usebufpool = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003549 bus->rxctl = bus->rxbuf;
3550 if (dhd_alignctl) {
3551 bus->rxctl += firstread;
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07003552 pad = ((unsigned long)bus->rxctl %
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003553 DHD_SDALIGN);
3554 if (pad)
3555 bus->rxctl +=
3556 (DHD_SDALIGN - pad);
3557 bus->rxctl -= firstread;
3558 }
3559 ASSERT(bus->rxctl >= bus->rxbuf);
3560 rxbuf = bus->rxctl;
3561 /* Read the entire frame */
Grant Grundler4b455e02011-05-04 09:59:47 -07003562 sdret = bcmsdh_recv_buf(bus,
3563 bcmsdh_cur_sbwad(sdh),
3564 SDIO_FUNC_2, F2SYNC,
3565 rxbuf, rdlen,
3566 NULL, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003567 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003568 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003569
3570 /* Control frame failures need
3571 retransmission */
3572 if (sdret < 0) {
3573 DHD_ERROR(("%s: read %d control bytes failed: %d\n",
3574 __func__,
3575 rdlen, sdret));
3576 /* dhd.rx_ctlerrs is higher */
3577 bus->rxc_errors++;
3578 dhd_os_sdunlock_rxq(bus->dhd);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003579 dhdsdio_rxfail(bus, true,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003580 (bus->bus ==
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003581 SPI_BUS) ? false
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003582 : true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003583 continue;
3584 }
3585 } else {
3586 /* Give up on data,
3587 request rtx of events */
Arend van Sprielcda64a52011-05-10 22:25:33 +02003588 DHD_ERROR(("%s (nextlen): "
3589 "bcm_pkt_buf_get_skb failed:"
3590 " len %d rdlen %d expected"
3591 " rxseq %d\n", __func__,
3592 len, rdlen, rxseq));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003593 /* Just go try again w/normal
3594 header read */
3595 dhd_os_sdunlock_rxq(bus->dhd);
3596 continue;
3597 }
3598 } else {
3599 if (bus->bus == SPI_BUS)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003600 bus->usebufpool = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003601
Arend van Spriel54991ad2010-11-23 14:06:24 +01003602 ASSERT(!(pkt->prev));
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01003603 PKTALIGN(pkt, rdlen, DHD_SDALIGN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01003604 rxbuf = (u8 *) (pkt->data);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003605 /* Read the entire frame */
Grant Grundler4b455e02011-05-04 09:59:47 -07003606 sdret = bcmsdh_recv_buf(bus,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003607 bcmsdh_cur_sbwad(sdh),
3608 SDIO_FUNC_2, F2SYNC,
Grant Grundler4b455e02011-05-04 09:59:47 -07003609 rxbuf, rdlen,
3610 pkt, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003611 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003612 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003613
3614 if (sdret < 0) {
3615 DHD_ERROR(("%s (nextlen): read %d bytes failed: %d\n",
3616 __func__, rdlen, sdret));
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003617 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003618 bus->dhd->rx_errors++;
3619 dhd_os_sdunlock_rxq(bus->dhd);
3620 /* Force retry w/normal header read.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003621 * Don't attempt NAK for
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003622 * gSPI
3623 */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003624 dhdsdio_rxfail(bus, true,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003625 (bus->bus ==
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003626 SPI_BUS) ? false :
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003627 true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003628 continue;
3629 }
3630 }
3631 dhd_os_sdunlock_rxq(bus->dhd);
3632
3633 /* Now check the header */
Stanislav Fomichev02160692011-02-15 01:05:10 +03003634 memcpy(bus->rxhdr, rxbuf, SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003635
3636 /* Extract hardware header fields */
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003637 len = get_unaligned_le16(bus->rxhdr);
3638 check = get_unaligned_le16(bus->rxhdr + sizeof(u16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003639
3640 /* All zeros means readahead info was bad */
3641 if (!(len | check)) {
3642 DHD_INFO(("%s (nextlen): read zeros in HW "
3643 "header???\n", __func__));
Grant Grundler4b455e02011-05-04 09:59:47 -07003644 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003645 continue;
3646 }
3647
3648 /* Validate check bytes */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003649 if ((u16)~(len ^ check)) {
Grant Grundler4b455e02011-05-04 09:59:47 -07003650 DHD_ERROR(("%s (nextlen): HW hdr error:"
3651 " nextlen/len/check"
3652 " 0x%04x/0x%04x/0x%04x\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003653 __func__, nextlen, len, check));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003654 bus->rx_badhdr++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003655 dhdsdio_rxfail(bus, false, false);
Grant Grundler4b455e02011-05-04 09:59:47 -07003656 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003657 continue;
3658 }
3659
3660 /* Validate frame length */
3661 if (len < SDPCM_HDRLEN) {
3662 DHD_ERROR(("%s (nextlen): HW hdr length "
3663 "invalid: %d\n", __func__, len));
Grant Grundler4b455e02011-05-04 09:59:47 -07003664 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003665 continue;
3666 }
3667
3668 /* Check for consistency withreadahead info */
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003669 len_consistent = (nextlen != (roundup(len, 16) >> 4));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003670 if (len_consistent) {
3671 /* Mismatch, force retry w/normal
3672 header (may be >4K) */
Grant Grundler4b455e02011-05-04 09:59:47 -07003673 DHD_ERROR(("%s (nextlen): mismatch, "
3674 "nextlen %d len %d rnd %d; "
3675 "expected rxseq %d\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003676 __func__, nextlen,
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003677 len, roundup(len, 16), rxseq));
Grant Grundler4b455e02011-05-04 09:59:47 -07003678 dhdsdio_rxfail(bus, true, (bus->bus != SPI_BUS));
3679 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003680 continue;
3681 }
3682
3683 /* Extract software header fields */
Grant Grundler4b455e02011-05-04 09:59:47 -07003684 chan = SDPCM_PACKET_CHANNEL(
3685 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3686 seq = SDPCM_PACKET_SEQUENCE(
3687 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3688 doff = SDPCM_DOFFSET_VALUE(
3689 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3690 txmax = SDPCM_WINDOW_VALUE(
3691 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003692
3693 bus->nextlen =
3694 bus->rxhdr[SDPCM_FRAMETAG_LEN +
3695 SDPCM_NEXTLEN_OFFSET];
3696 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
3697 DHD_INFO(("%s (nextlen): got frame w/nextlen too large" " (%d), seq %d\n",
3698 __func__, bus->nextlen, seq));
3699 bus->nextlen = 0;
3700 }
3701
3702 bus->dhd->rx_readahead_cnt++;
Grant Grundler4b455e02011-05-04 09:59:47 -07003703
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003704 /* Handle Flow Control */
Grant Grundler4b455e02011-05-04 09:59:47 -07003705 fcbits = SDPCM_FCMASK_VALUE(
3706 &bus->rxhdr[SDPCM_FRAMETAG_LEN]);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003707
Grant Grundler4b455e02011-05-04 09:59:47 -07003708 if (bus->flowcontrol != fcbits) {
3709 if (~bus->flowcontrol & fcbits)
3710 bus->fc_xoff++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003711
Grant Grundler4b455e02011-05-04 09:59:47 -07003712 if (bus->flowcontrol & ~fcbits)
3713 bus->fc_xon++;
3714
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003715 bus->fc_rcvd++;
3716 bus->flowcontrol = fcbits;
3717 }
3718
3719 /* Check and update sequence number */
3720 if (rxseq != seq) {
3721 DHD_INFO(("%s (nextlen): rx_seq %d, expected "
3722 "%d\n", __func__, seq, rxseq));
3723 bus->rx_badseq++;
3724 rxseq = seq;
3725 }
3726
3727 /* Check window for sanity */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003728 if ((u8) (txmax - bus->tx_seq) > 0x40) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003729 DHD_ERROR(("%s: got unlikely tx max %d with "
3730 "tx_seq %d\n",
3731 __func__, txmax, bus->tx_seq));
3732 txmax = bus->tx_seq + 2;
3733 }
3734 bus->tx_max = txmax;
3735
3736#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003737 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
3738 printk(KERN_DEBUG "Rx Data:\n");
3739 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3740 rxbuf, len);
3741 } else if (DHD_HDRS_ON()) {
3742 printk(KERN_DEBUG "RxHdr:\n");
3743 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3744 bus->rxhdr, SDPCM_HDRLEN);
3745 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003746#endif
3747
3748 if (chan == SDPCM_CONTROL_CHANNEL) {
3749 if (bus->bus == SPI_BUS) {
3750 dhdsdio_read_control(bus, rxbuf, len,
3751 doff);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003752 } else {
3753 DHD_ERROR(("%s (nextlen): readahead on control" " packet %d?\n",
3754 __func__, seq));
3755 /* Force retry w/normal header read */
3756 bus->nextlen = 0;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003757 dhdsdio_rxfail(bus, false, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003758 }
Grant Grundler4b455e02011-05-04 09:59:47 -07003759 dhdsdio_pktfree2(bus, pkt);
3760 continue;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003761 }
3762
3763 if ((bus->bus == SPI_BUS) && !bus->usebufpool) {
3764 DHD_ERROR(("Received %d bytes on %d channel. Running out of " "rx pktbuf's or not yet malloced.\n",
3765 len, chan));
3766 continue;
3767 }
3768
3769 /* Validate data offset */
3770 if ((doff < SDPCM_HDRLEN) || (doff > len)) {
3771 DHD_ERROR(("%s (nextlen): bad data offset %d: HW len %d min %d\n",
3772 __func__, doff, len, SDPCM_HDRLEN));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003773 dhdsdio_rxfail(bus, false, false);
Grant Grundler4b455e02011-05-04 09:59:47 -07003774 dhdsdio_pktfree2(bus, pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003775 continue;
3776 }
3777
3778 /* All done with this one -- now deliver the packet */
3779 goto deliver;
3780 }
3781 /* gSPI frames should not be handled in fractions */
3782 if (bus->bus == SPI_BUS)
3783 break;
3784
3785 /* Read frame header (hardware and software) */
Grant Grundler4b455e02011-05-04 09:59:47 -07003786 sdret = bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh),
3787 SDIO_FUNC_2, F2SYNC, bus->rxhdr, firstread,
3788 NULL, NULL, NULL);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003789 bus->f2rxhdrs++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003790 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003791
3792 if (sdret < 0) {
3793 DHD_ERROR(("%s: RXHEADER FAILED: %d\n", __func__,
3794 sdret));
3795 bus->rx_hdrfail++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003796 dhdsdio_rxfail(bus, true, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003797 continue;
3798 }
3799#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003800 if (DHD_BYTES_ON() || DHD_HDRS_ON()) {
3801 printk(KERN_DEBUG "RxHdr:\n");
3802 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3803 bus->rxhdr, SDPCM_HDRLEN);
3804 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003805#endif
3806
3807 /* Extract hardware header fields */
Stanislav Fomichev56dfe3c2011-02-20 21:43:33 +03003808 len = get_unaligned_le16(bus->rxhdr);
3809 check = get_unaligned_le16(bus->rxhdr + sizeof(u16));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003810
3811 /* All zeros means no more frames */
3812 if (!(len | check)) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003813 *finished = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003814 break;
3815 }
3816
3817 /* Validate check bytes */
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07003818 if ((u16) ~(len ^ check)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003819 DHD_ERROR(("%s: HW hdr err: len/check 0x%04x/0x%04x\n",
3820 __func__, len, check));
3821 bus->rx_badhdr++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003822 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003823 continue;
3824 }
3825
3826 /* Validate frame length */
3827 if (len < SDPCM_HDRLEN) {
3828 DHD_ERROR(("%s: HW hdr length invalid: %d\n",
3829 __func__, len));
3830 continue;
3831 }
3832
3833 /* Extract software header fields */
3834 chan = SDPCM_PACKET_CHANNEL(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3835 seq = SDPCM_PACKET_SEQUENCE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3836 doff = SDPCM_DOFFSET_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3837 txmax = SDPCM_WINDOW_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3838
3839 /* Validate data offset */
3840 if ((doff < SDPCM_HDRLEN) || (doff > len)) {
3841 DHD_ERROR(("%s: Bad data offset %d: HW len %d, min %d "
3842 "seq %d\n",
3843 __func__, doff, len, SDPCM_HDRLEN, seq));
3844 bus->rx_badhdr++;
3845 ASSERT(0);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003846 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003847 continue;
3848 }
3849
3850 /* Save the readahead length if there is one */
3851 bus->nextlen =
3852 bus->rxhdr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
3853 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
3854 DHD_INFO(("%s (nextlen): got frame w/nextlen too large "
3855 "(%d), seq %d\n",
3856 __func__, bus->nextlen, seq));
3857 bus->nextlen = 0;
3858 }
3859
3860 /* Handle Flow Control */
3861 fcbits = SDPCM_FCMASK_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3862
Grant Grundler4b455e02011-05-04 09:59:47 -07003863 if (bus->flowcontrol != fcbits) {
3864 if (~bus->flowcontrol & fcbits)
3865 bus->fc_xoff++;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003866
Grant Grundler4b455e02011-05-04 09:59:47 -07003867 if (bus->flowcontrol & ~fcbits)
3868 bus->fc_xon++;
3869
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003870 bus->fc_rcvd++;
3871 bus->flowcontrol = fcbits;
3872 }
3873
3874 /* Check and update sequence number */
3875 if (rxseq != seq) {
3876 DHD_INFO(("%s: rx_seq %d, expected %d\n", __func__,
3877 seq, rxseq));
3878 bus->rx_badseq++;
3879 rxseq = seq;
3880 }
3881
3882 /* Check window for sanity */
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07003883 if ((u8) (txmax - bus->tx_seq) > 0x40) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003884 DHD_ERROR(("%s: unlikely tx max %d with tx_seq %d\n",
3885 __func__, txmax, bus->tx_seq));
3886 txmax = bus->tx_seq + 2;
3887 }
3888 bus->tx_max = txmax;
3889
3890 /* Call a separate function for control frames */
3891 if (chan == SDPCM_CONTROL_CHANNEL) {
3892 dhdsdio_read_control(bus, bus->rxhdr, len, doff);
3893 continue;
3894 }
3895
3896 ASSERT((chan == SDPCM_DATA_CHANNEL)
3897 || (chan == SDPCM_EVENT_CHANNEL)
3898 || (chan == SDPCM_TEST_CHANNEL)
3899 || (chan == SDPCM_GLOM_CHANNEL));
3900
3901 /* Length to read */
3902 rdlen = (len > firstread) ? (len - firstread) : 0;
3903
3904 /* May pad read to blocksize for efficiency */
3905 if (bus->roundup && bus->blocksize &&
3906 (rdlen > bus->blocksize)) {
3907 pad = bus->blocksize - (rdlen % bus->blocksize);
3908 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
3909 ((rdlen + pad + firstread) < MAX_RX_DATASZ))
3910 rdlen += pad;
3911 } else if (rdlen % DHD_SDALIGN) {
3912 rdlen += DHD_SDALIGN - (rdlen % DHD_SDALIGN);
3913 }
3914
3915 /* Satisfy length-alignment requirements */
3916 if (forcealign && (rdlen & (ALIGNMENT - 1)))
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07003917 rdlen = roundup(rdlen, ALIGNMENT);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003918
3919 if ((rdlen + firstread) > MAX_RX_DATASZ) {
3920 /* Too long -- skip this frame */
3921 DHD_ERROR(("%s: too long: len %d rdlen %d\n",
3922 __func__, len, rdlen));
3923 bus->dhd->rx_errors++;
3924 bus->rx_toolong++;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003925 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003926 continue;
3927 }
3928
3929 dhd_os_sdlock_rxq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003930 pkt = bcm_pkt_buf_get_skb(rdlen + firstread + DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003931 if (!pkt) {
3932 /* Give up on data, request rtx of events */
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003933 DHD_ERROR(("%s: bcm_pkt_buf_get_skb failed: rdlen %d "
3934 "chan %d\n", __func__, rdlen, chan));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003935 bus->dhd->rx_dropped++;
3936 dhd_os_sdunlock_rxq(bus->dhd);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07003937 dhdsdio_rxfail(bus, false, RETRYCHAN(chan));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003938 continue;
3939 }
3940 dhd_os_sdunlock_rxq(bus->dhd);
3941
Arend van Spriel54991ad2010-11-23 14:06:24 +01003942 ASSERT(!(pkt->prev));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003943
3944 /* Leave room for what we already read, and align remainder */
Arend van Spriel54991ad2010-11-23 14:06:24 +01003945 ASSERT(firstread < pkt->len);
Arend van Sprielc303ecb2010-11-18 20:46:43 +01003946 skb_pull(pkt, firstread);
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01003947 PKTALIGN(pkt, rdlen, DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003948
3949 /* Read the remaining frame data */
Grant Grundler4b455e02011-05-04 09:59:47 -07003950 sdret = bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
Arend van Spriel54991ad2010-11-23 14:06:24 +01003951 F2SYNC, ((u8 *) (pkt->data)), rdlen,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003952 pkt, NULL, NULL);
3953 bus->f2rxdata++;
Roland Vossenb7ef2a92011-05-03 11:35:02 +02003954 ASSERT(sdret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003955
3956 if (sdret < 0) {
3957 DHD_ERROR(("%s: read %d %s bytes failed: %d\n",
3958 __func__, rdlen,
3959 ((chan ==
3960 SDPCM_EVENT_CHANNEL) ? "event" : ((chan ==
3961 SDPCM_DATA_CHANNEL)
3962 ? "data" : "test")),
3963 sdret));
3964 dhd_os_sdlock_rxq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02003965 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003966 dhd_os_sdunlock_rxq(bus->dhd);
3967 bus->dhd->rx_errors++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07003968 dhdsdio_rxfail(bus, true, RETRYCHAN(chan));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003969 continue;
3970 }
3971
3972 /* Copy the already-read portion */
Arend van Sprielc303ecb2010-11-18 20:46:43 +01003973 skb_push(pkt, firstread);
Stanislav Fomichev02160692011-02-15 01:05:10 +03003974 memcpy(pkt->data, bus->rxhdr, firstread);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003975
3976#ifdef DHD_DEBUG
Arend van Spriel34227312011-05-10 22:25:32 +02003977 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
3978 printk(KERN_DEBUG "Rx Data:\n");
3979 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
3980 pkt->data, len);
3981 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003982#endif
3983
3984deliver:
3985 /* Save superframe descriptor and allocate packet frame */
3986 if (chan == SDPCM_GLOM_CHANNEL) {
3987 if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_FRAMETAG_LEN])) {
3988 DHD_GLOM(("%s: glom descriptor, %d bytes:\n",
3989 __func__, len));
3990#ifdef DHD_DEBUG
3991 if (DHD_GLOM_ON()) {
Arend van Spriel34227312011-05-10 22:25:32 +02003992 printk(KERN_DEBUG "Glom Data:\n");
3993 print_hex_dump_bytes("",
3994 DUMP_PREFIX_OFFSET,
3995 pkt->data, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003996 }
3997#endif
Arend van Spriel2cb8ada2010-11-18 20:46:44 +01003998 __skb_trim(pkt, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07003999 ASSERT(doff == SDPCM_HDRLEN);
Arend van Sprielc303ecb2010-11-18 20:46:43 +01004000 skb_pull(pkt, SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004001 bus->glomd = pkt;
4002 } else {
4003 DHD_ERROR(("%s: glom superframe w/o "
4004 "descriptor!\n", __func__));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004005 dhdsdio_rxfail(bus, false, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004006 }
4007 continue;
4008 }
4009
4010 /* Fill in packet len and prio, deliver upward */
Arend van Spriel2cb8ada2010-11-18 20:46:44 +01004011 __skb_trim(pkt, len);
Arend van Sprielc303ecb2010-11-18 20:46:43 +01004012 skb_pull(pkt, doff);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004013
4014#ifdef SDTEST
4015 /* Test channel packets are processed separately */
4016 if (chan == SDPCM_TEST_CHANNEL) {
4017 dhdsdio_testrcv(bus, pkt, seq);
4018 continue;
4019 }
4020#endif /* SDTEST */
4021
Arend van Spriel54991ad2010-11-23 14:06:24 +01004022 if (pkt->len == 0) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004023 dhd_os_sdlock_rxq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004024 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004025 dhd_os_sdunlock_rxq(bus->dhd);
4026 continue;
4027 } else if (dhd_prot_hdrpull(bus->dhd, &ifidx, pkt) != 0) {
4028 DHD_ERROR(("%s: rx protocol error\n", __func__));
4029 dhd_os_sdlock_rxq(bus->dhd);
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004030 bcm_pkt_buf_free_skb(pkt);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004031 dhd_os_sdunlock_rxq(bus->dhd);
4032 bus->dhd->rx_errors++;
4033 continue;
4034 }
4035
4036 /* Unlock during rx call */
4037 dhd_os_sdunlock(bus->dhd);
4038 dhd_rx_frame(bus->dhd, ifidx, pkt, 1);
4039 dhd_os_sdlock(bus->dhd);
4040 }
4041 rxcount = maxframes - rxleft;
4042#ifdef DHD_DEBUG
4043 /* Message if we hit the limit */
4044 if (!rxleft && !sdtest)
4045 DHD_DATA(("%s: hit rx limit of %d frames\n", __func__,
4046 maxframes));
4047 else
4048#endif /* DHD_DEBUG */
4049 DHD_DATA(("%s: processed %d frames\n", __func__, rxcount));
4050 /* Back off rxseq if awaiting rtx, update rx_seq */
4051 if (bus->rxskip)
4052 rxseq--;
4053 bus->rx_seq = rxseq;
4054
4055 return rxcount;
4056}
4057
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004058static u32 dhdsdio_hostmail(dhd_bus_t *bus)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004059{
4060 sdpcmd_regs_t *regs = bus->regs;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004061 u32 intstatus = 0;
4062 u32 hmb_data;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004063 u8 fcbits;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004064 uint retries = 0;
4065
4066 DHD_TRACE(("%s: Enter\n", __func__));
4067
4068 /* Read mailbox data and ack that we did so */
4069 R_SDREG(hmb_data, &regs->tohostmailboxdata, retries);
4070 if (retries <= retry_limit)
4071 W_SDREG(SMB_INT_ACK, &regs->tosbmailbox, retries);
4072 bus->f1regdata += 2;
4073
4074 /* Dongle recomposed rx frames, accept them again */
4075 if (hmb_data & HMB_DATA_NAKHANDLED) {
4076 DHD_INFO(("Dongle reports NAK handled, expect rtx of %d\n",
4077 bus->rx_seq));
4078 if (!bus->rxskip)
4079 DHD_ERROR(("%s: unexpected NAKHANDLED!\n", __func__));
4080
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004081 bus->rxskip = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004082 intstatus |= I_HMB_FRAME_IND;
4083 }
4084
4085 /*
4086 * DEVREADY does not occur with gSPI.
4087 */
4088 if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
4089 bus->sdpcm_ver =
4090 (hmb_data & HMB_DATA_VERSION_MASK) >>
4091 HMB_DATA_VERSION_SHIFT;
4092 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
4093 DHD_ERROR(("Version mismatch, dongle reports %d, "
4094 "expecting %d\n",
4095 bus->sdpcm_ver, SDPCM_PROT_VERSION));
4096 else
4097 DHD_INFO(("Dongle ready, protocol version %d\n",
4098 bus->sdpcm_ver));
4099 }
4100
4101 /*
4102 * Flow Control has been moved into the RX headers and this out of band
Grant Grundler4b455e02011-05-04 09:59:47 -07004103 * method isn't used any more.
4104 * remaining backward compatible with older dongles.
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004105 */
4106 if (hmb_data & HMB_DATA_FC) {
Grant Grundler4b455e02011-05-04 09:59:47 -07004107 fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
4108 HMB_DATA_FCDATA_SHIFT;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004109
4110 if (fcbits & ~bus->flowcontrol)
4111 bus->fc_xoff++;
Grant Grundler4b455e02011-05-04 09:59:47 -07004112
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004113 if (bus->flowcontrol & ~fcbits)
4114 bus->fc_xon++;
4115
4116 bus->fc_rcvd++;
4117 bus->flowcontrol = fcbits;
4118 }
4119
4120 /* Shouldn't be any others */
4121 if (hmb_data & ~(HMB_DATA_DEVREADY |
4122 HMB_DATA_NAKHANDLED |
4123 HMB_DATA_FC |
4124 HMB_DATA_FWREADY |
4125 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK)) {
4126 DHD_ERROR(("Unknown mailbox data content: 0x%02x\n", hmb_data));
4127 }
4128
4129 return intstatus;
4130}
4131
4132bool dhdsdio_dpc(dhd_bus_t *bus)
4133{
4134 bcmsdh_info_t *sdh = bus->sdh;
4135 sdpcmd_regs_t *regs = bus->regs;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004136 u32 intstatus, newstatus = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004137 uint retries = 0;
4138 uint rxlimit = dhd_rxbound; /* Rx frames to read before resched */
4139 uint txlimit = dhd_txbound; /* Tx frames to send before resched */
4140 uint framecnt = 0; /* Temporary counter of tx/rx frames */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004141 bool rxdone = true; /* Flag for no more read data */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004142 bool resched = false; /* Flag indicating resched wanted */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004143
4144 DHD_TRACE(("%s: Enter\n", __func__));
4145
4146 /* Start with leftover status bits */
4147 intstatus = bus->intstatus;
4148
4149 dhd_os_sdlock(bus->dhd);
4150
4151 /* If waiting for HTAVAIL, check status */
4152 if (bus->clkstate == CLK_PENDING) {
4153 int err;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004154 u8 clkctl, devctl = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004155
4156#ifdef DHD_DEBUG
4157 /* Check for inconsistent device control */
4158 devctl =
4159 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, &err);
4160 if (err) {
4161 DHD_ERROR(("%s: error reading DEVCTL: %d\n",
4162 __func__, err));
4163 bus->dhd->busstate = DHD_BUS_DOWN;
4164 } else {
4165 ASSERT(devctl & SBSDIO_DEVCTL_CA_INT_ONLY);
4166 }
4167#endif /* DHD_DEBUG */
4168
4169 /* Read CSR, if clock on switch to AVAIL, else ignore */
4170 clkctl =
4171 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
4172 &err);
4173 if (err) {
4174 DHD_ERROR(("%s: error reading CSR: %d\n", __func__,
4175 err));
4176 bus->dhd->busstate = DHD_BUS_DOWN;
4177 }
4178
4179 DHD_INFO(("DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n", devctl,
4180 clkctl));
4181
4182 if (SBSDIO_HTAV(clkctl)) {
4183 devctl =
4184 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
4185 &err);
4186 if (err) {
4187 DHD_ERROR(("%s: error reading DEVCTL: %d\n",
4188 __func__, err));
4189 bus->dhd->busstate = DHD_BUS_DOWN;
4190 }
4191 devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
4192 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
4193 devctl, &err);
4194 if (err) {
4195 DHD_ERROR(("%s: error writing DEVCTL: %d\n",
4196 __func__, err));
4197 bus->dhd->busstate = DHD_BUS_DOWN;
4198 }
4199 bus->clkstate = CLK_AVAIL;
4200 } else {
4201 goto clkwait;
4202 }
4203 }
4204
4205 BUS_WAKE(bus);
4206
4207 /* Make sure backplane clock is on */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004208 dhdsdio_clkctl(bus, CLK_AVAIL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004209 if (bus->clkstate == CLK_PENDING)
4210 goto clkwait;
4211
4212 /* Pending interrupt indicates new device status */
4213 if (bus->ipend) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004214 bus->ipend = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004215 R_SDREG(newstatus, &regs->intstatus, retries);
4216 bus->f1regdata++;
4217 if (bcmsdh_regfail(bus->sdh))
4218 newstatus = 0;
4219 newstatus &= bus->hostintmask;
4220 bus->fcstate = !!(newstatus & I_HMB_FC_STATE);
4221 if (newstatus) {
4222 W_SDREG(newstatus, &regs->intstatus, retries);
4223 bus->f1regdata++;
4224 }
4225 }
4226
4227 /* Merge new bits with previous */
4228 intstatus |= newstatus;
4229 bus->intstatus = 0;
4230
4231 /* Handle flow-control change: read new state in case our ack
4232 * crossed another change interrupt. If change still set, assume
4233 * FC ON for safety, let next loop through do the debounce.
4234 */
4235 if (intstatus & I_HMB_FC_CHANGE) {
4236 intstatus &= ~I_HMB_FC_CHANGE;
4237 W_SDREG(I_HMB_FC_CHANGE, &regs->intstatus, retries);
4238 R_SDREG(newstatus, &regs->intstatus, retries);
4239 bus->f1regdata += 2;
4240 bus->fcstate =
4241 !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE));
4242 intstatus |= (newstatus & bus->hostintmask);
4243 }
4244
4245 /* Handle host mailbox indication */
4246 if (intstatus & I_HMB_HOST_INT) {
4247 intstatus &= ~I_HMB_HOST_INT;
4248 intstatus |= dhdsdio_hostmail(bus);
4249 }
4250
4251 /* Generally don't ask for these, can get CRC errors... */
4252 if (intstatus & I_WR_OOSYNC) {
4253 DHD_ERROR(("Dongle reports WR_OOSYNC\n"));
4254 intstatus &= ~I_WR_OOSYNC;
4255 }
4256
4257 if (intstatus & I_RD_OOSYNC) {
4258 DHD_ERROR(("Dongle reports RD_OOSYNC\n"));
4259 intstatus &= ~I_RD_OOSYNC;
4260 }
4261
4262 if (intstatus & I_SBINT) {
4263 DHD_ERROR(("Dongle reports SBINT\n"));
4264 intstatus &= ~I_SBINT;
4265 }
4266
4267 /* Would be active due to wake-wlan in gSPI */
4268 if (intstatus & I_CHIPACTIVE) {
4269 DHD_INFO(("Dongle reports CHIPACTIVE\n"));
4270 intstatus &= ~I_CHIPACTIVE;
4271 }
4272
4273 /* Ignore frame indications if rxskip is set */
4274 if (bus->rxskip)
4275 intstatus &= ~I_HMB_FRAME_IND;
4276
4277 /* On frame indication, read available frames */
4278 if (PKT_AVAILABLE()) {
4279 framecnt = dhdsdio_readframes(bus, rxlimit, &rxdone);
4280 if (rxdone || bus->rxskip)
4281 intstatus &= ~I_HMB_FRAME_IND;
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07004282 rxlimit -= min(framecnt, rxlimit);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004283 }
4284
4285 /* Keep still-pending events for next scheduling */
4286 bus->intstatus = intstatus;
4287
4288clkwait:
4289#if defined(OOB_INTR_ONLY)
4290 bcmsdh_oob_intr_set(1);
4291#endif /* (OOB_INTR_ONLY) */
4292 /* Re-enable interrupts to detect new device events (mailbox, rx frame)
4293 * or clock availability. (Allows tx loop to check ipend if desired.)
4294 * (Unless register access seems hosed, as we may not be able to ACK...)
4295 */
4296 if (bus->intr && bus->intdis && !bcmsdh_regfail(sdh)) {
4297 DHD_INTR(("%s: enable SDIO interrupts, rxdone %d framecnt %d\n",
4298 __func__, rxdone, framecnt));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004299 bus->intdis = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004300 bcmsdh_intr_enable(sdh);
4301 }
4302
4303 if (DATAOK(bus) && bus->ctrl_frame_stat &&
4304 (bus->clkstate == CLK_AVAIL)) {
4305 int ret, i;
4306
4307 ret =
4308 dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004309 F2SYNC, (u8 *) bus->ctrl_frame_buf,
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004310 (u32) bus->ctrl_frame_len, NULL,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004311 NULL, NULL);
Roland Vossenb7ef2a92011-05-03 11:35:02 +02004312 ASSERT(ret != -BCME_PENDING);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004313
4314 if (ret < 0) {
4315 /* On failure, abort the command and
4316 terminate the frame */
4317 DHD_INFO(("%s: sdio error %d, abort command and "
4318 "terminate frame.\n", __func__, ret));
4319 bus->tx_sderrs++;
4320
4321 bcmsdh_abort(sdh, SDIO_FUNC_2);
4322
4323 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
4324 SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
4325 NULL);
4326 bus->f1regdata++;
4327
4328 for (i = 0; i < 3; i++) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004329 u8 hi, lo;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004330 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
4331 SBSDIO_FUNC1_WFRAMEBCHI,
4332 NULL);
4333 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
4334 SBSDIO_FUNC1_WFRAMEBCLO,
4335 NULL);
4336 bus->f1regdata += 2;
4337 if ((hi == 0) && (lo == 0))
4338 break;
4339 }
4340
4341 }
4342 if (ret == 0)
4343 bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
4344
Arend van Spriel0bef7742011-02-10 12:03:44 +01004345 DHD_INFO(("Return_dpc value is : %d\n", ret));
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004346 bus->ctrl_frame_stat = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004347 dhd_wait_event_wakeup(bus->dhd);
4348 }
4349 /* Send queued frames (limit 1 if rx may still be pending) */
4350 else if ((bus->clkstate == CLK_AVAIL) && !bus->fcstate &&
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004351 bcm_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004352 && DATAOK(bus)) {
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07004353 framecnt = rxdone ? txlimit : min(txlimit, dhd_txminmax);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004354 framecnt = dhdsdio_sendfromq(bus, framecnt);
4355 txlimit -= framecnt;
4356 }
4357
4358 /* Resched if events or tx frames are pending,
4359 else await next interrupt */
4360 /* On failed register access, all bets are off:
4361 no resched or interrupts */
4362 if ((bus->dhd->busstate == DHD_BUS_DOWN) || bcmsdh_regfail(sdh)) {
4363 DHD_ERROR(("%s: failed backplane access over SDIO, halting "
4364 "operation %d\n", __func__, bcmsdh_regfail(sdh)));
4365 bus->dhd->busstate = DHD_BUS_DOWN;
4366 bus->intstatus = 0;
4367 } else if (bus->clkstate == CLK_PENDING) {
4368 DHD_INFO(("%s: rescheduled due to CLK_PENDING awaiting "
4369 "I_CHIPACTIVE interrupt\n", __func__));
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004370 resched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004371 } else if (bus->intstatus || bus->ipend ||
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004372 (!bus->fcstate && bcm_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004373 DATAOK(bus)) || PKT_AVAILABLE()) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004374 resched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004375 }
4376
4377 bus->dpc_sched = resched;
4378
4379 /* If we're done for now, turn off clock request. */
4380 if ((bus->clkstate != CLK_PENDING)
4381 && bus->idletime == DHD_IDLE_IMMEDIATE) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004382 bus->activity = false;
4383 dhdsdio_clkctl(bus, CLK_NONE, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004384 }
4385
4386 dhd_os_sdunlock(bus->dhd);
4387
4388 return resched;
4389}
4390
4391bool dhd_bus_dpc(struct dhd_bus *bus)
4392{
4393 bool resched;
4394
4395 /* Call the DPC directly. */
4396 DHD_TRACE(("Calling dhdsdio_dpc() from %s\n", __func__));
4397 resched = dhdsdio_dpc(bus);
4398
4399 return resched;
4400}
4401
4402void dhdsdio_isr(void *arg)
4403{
4404 dhd_bus_t *bus = (dhd_bus_t *) arg;
4405 bcmsdh_info_t *sdh;
4406
4407 DHD_TRACE(("%s: Enter\n", __func__));
4408
4409 if (!bus) {
4410 DHD_ERROR(("%s : bus is null pointer , exit\n", __func__));
4411 return;
4412 }
4413 sdh = bus->sdh;
4414
4415 if (bus->dhd->busstate == DHD_BUS_DOWN) {
4416 DHD_ERROR(("%s : bus is down. we have nothing to do\n",
4417 __func__));
4418 return;
4419 }
4420 /* Count the interrupt call */
4421 bus->intrcount++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004422 bus->ipend = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004423
4424 /* Shouldn't get this interrupt if we're sleeping? */
4425 if (bus->sleeping) {
4426 DHD_ERROR(("INTERRUPT WHILE SLEEPING??\n"));
4427 return;
4428 }
4429
4430 /* Disable additional interrupts (is this needed now)? */
4431 if (bus->intr)
4432 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
4433 else
4434 DHD_ERROR(("dhdsdio_isr() w/o interrupt configured!\n"));
4435
4436 bcmsdh_intr_disable(sdh);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004437 bus->intdis = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004438
4439#if defined(SDIO_ISR_THREAD)
4440 DHD_TRACE(("Calling dhdsdio_dpc() from %s\n", __func__));
4441 while (dhdsdio_dpc(bus))
4442 ;
4443#else
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004444 bus->dpc_sched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004445 dhd_sched_dpc(bus->dhd);
4446#endif
4447
4448}
4449
4450#ifdef SDTEST
4451static void dhdsdio_pktgen_init(dhd_bus_t *bus)
4452{
4453 /* Default to specified length, or full range */
4454 if (dhd_pktgen_len) {
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07004455 bus->pktgen_maxlen = min(dhd_pktgen_len, MAX_PKTGEN_LEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004456 bus->pktgen_minlen = bus->pktgen_maxlen;
4457 } else {
4458 bus->pktgen_maxlen = MAX_PKTGEN_LEN;
4459 bus->pktgen_minlen = 0;
4460 }
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004461 bus->pktgen_len = (u16) bus->pktgen_minlen;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004462
4463 /* Default to per-watchdog burst with 10s print time */
4464 bus->pktgen_freq = 1;
4465 bus->pktgen_print = 10000 / dhd_watchdog_ms;
4466 bus->pktgen_count = (dhd_pktgen * dhd_watchdog_ms + 999) / 1000;
4467
4468 /* Default to echo mode */
4469 bus->pktgen_mode = DHD_PKTGEN_ECHO;
4470 bus->pktgen_stop = 1;
4471}
4472
4473static void dhdsdio_pktgen(dhd_bus_t *bus)
4474{
Arend van Sprielc26b1372010-11-23 14:06:23 +01004475 struct sk_buff *pkt;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004476 u8 *data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004477 uint pktcount;
4478 uint fillbyte;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004479 u16 len;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004480
4481 /* Display current count if appropriate */
4482 if (bus->pktgen_print && (++bus->pktgen_ptick >= bus->pktgen_print)) {
4483 bus->pktgen_ptick = 0;
Arend van Spriel0bef7742011-02-10 12:03:44 +01004484 printk(KERN_DEBUG "%s: send attempts %d rcvd %d\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004485 __func__, bus->pktgen_sent, bus->pktgen_rcvd);
4486 }
4487
4488 /* For recv mode, just make sure dongle has started sending */
4489 if (bus->pktgen_mode == DHD_PKTGEN_RECV) {
4490 if (!bus->pktgen_rcvd)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004491 dhdsdio_sdtest_set(bus, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004492 return;
4493 }
4494
4495 /* Otherwise, generate or request the specified number of packets */
4496 for (pktcount = 0; pktcount < bus->pktgen_count; pktcount++) {
4497 /* Stop if total has been reached */
4498 if (bus->pktgen_total
4499 && (bus->pktgen_sent >= bus->pktgen_total)) {
4500 bus->pktgen_count = 0;
4501 break;
4502 }
4503
4504 /* Allocate an appropriate-sized packet */
4505 len = bus->pktgen_len;
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004506 pkt = bcm_pkt_buf_get_skb(
Jason Cooper9b890322010-09-30 15:15:39 -04004507 (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + DHD_SDALIGN),
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004508 true);
Jason Cooper9b890322010-09-30 15:15:39 -04004509 if (!pkt) {
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004510 DHD_ERROR(("%s: bcm_pkt_buf_get_skb failed!\n",
4511 __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004512 break;
4513 }
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01004514 PKTALIGN(pkt, (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004515 DHD_SDALIGN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01004516 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004517
4518 /* Write test header cmd and extra based on mode */
4519 switch (bus->pktgen_mode) {
4520 case DHD_PKTGEN_ECHO:
4521 *data++ = SDPCM_TEST_ECHOREQ;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004522 *data++ = (u8) bus->pktgen_sent;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004523 break;
4524
4525 case DHD_PKTGEN_SEND:
4526 *data++ = SDPCM_TEST_DISCARD;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004527 *data++ = (u8) bus->pktgen_sent;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004528 break;
4529
4530 case DHD_PKTGEN_RXBURST:
4531 *data++ = SDPCM_TEST_BURST;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004532 *data++ = (u8) bus->pktgen_count;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004533 break;
4534
4535 default:
4536 DHD_ERROR(("Unrecognized pktgen mode %d\n",
4537 bus->pktgen_mode));
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004538 bcm_pkt_buf_free_skb(pkt, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004539 bus->pktgen_count = 0;
4540 return;
4541 }
4542
4543 /* Write test header length field */
4544 *data++ = (len >> 0);
4545 *data++ = (len >> 8);
4546
4547 /* Then fill in the remainder -- N/A for burst,
4548 but who cares... */
4549 for (fillbyte = 0; fillbyte < len; fillbyte++)
4550 *data++ =
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004551 SDPCM_TEST_FILL(fillbyte, (u8) bus->pktgen_sent);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004552
4553#ifdef DHD_DEBUG
4554 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
Arend van Spriel54991ad2010-11-23 14:06:24 +01004555 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
Arend van Spriel34227312011-05-10 22:25:32 +02004556 printk(KERN_DEBUG "dhdsdio_pktgen: Tx Data:\n");
4557 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, data,
4558 pkt->len - SDPCM_HDRLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004559 }
4560#endif
4561
4562 /* Send it */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004563 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004564 bus->pktgen_fail++;
4565 if (bus->pktgen_stop
4566 && bus->pktgen_stop == bus->pktgen_fail)
4567 bus->pktgen_count = 0;
4568 }
4569 bus->pktgen_sent++;
4570
4571 /* Bump length if not fixed, wrap at max */
4572 if (++bus->pktgen_len > bus->pktgen_maxlen)
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004573 bus->pktgen_len = (u16) bus->pktgen_minlen;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004574
4575 /* Special case for burst mode: just send one request! */
4576 if (bus->pktgen_mode == DHD_PKTGEN_RXBURST)
4577 break;
4578 }
4579}
4580
4581static void dhdsdio_sdtest_set(dhd_bus_t *bus, bool start)
4582{
Arend van Sprielc26b1372010-11-23 14:06:23 +01004583 struct sk_buff *pkt;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004584 u8 *data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004585
4586 /* Allocate the packet */
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004587 pkt = bcm_pkt_buf_get_skb(SDPCM_HDRLEN + SDPCM_TEST_HDRLEN +
4588 DHD_SDALIGN, true);
Jason Cooper9b890322010-09-30 15:15:39 -04004589 if (!pkt) {
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004590 DHD_ERROR(("%s: bcm_pkt_buf_get_skb failed!\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004591 return;
4592 }
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01004593 PKTALIGN(pkt, (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN), DHD_SDALIGN);
Arend van Spriel54991ad2010-11-23 14:06:24 +01004594 data = (u8 *) (pkt->data) + SDPCM_HDRLEN;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004595
4596 /* Fill in the test header */
4597 *data++ = SDPCM_TEST_SEND;
4598 *data++ = start;
4599 *data++ = (bus->pktgen_maxlen >> 0);
4600 *data++ = (bus->pktgen_maxlen >> 8);
4601
4602 /* Send it */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004603 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true))
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004604 bus->pktgen_fail++;
4605}
4606
Arend van Sprielc26b1372010-11-23 14:06:23 +01004607static void dhdsdio_testrcv(dhd_bus_t *bus, struct sk_buff *pkt, uint seq)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004608{
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004609 u8 *data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004610 uint pktlen;
4611
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004612 u8 cmd;
4613 u8 extra;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004614 u16 len;
4615 u16 offset;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004616
4617 /* Check for min length */
Arend van Spriel54991ad2010-11-23 14:06:24 +01004618 pktlen = pkt->len;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004619 if (pktlen < SDPCM_TEST_HDRLEN) {
4620 DHD_ERROR(("dhdsdio_restrcv: toss runt frame, pktlen %d\n",
4621 pktlen));
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004622 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004623 return;
4624 }
4625
4626 /* Extract header fields */
Arend van Spriel54991ad2010-11-23 14:06:24 +01004627 data = pkt->data;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004628 cmd = *data++;
4629 extra = *data++;
4630 len = *data++;
4631 len += *data++ << 8;
4632
4633 /* Check length for relevant commands */
4634 if (cmd == SDPCM_TEST_DISCARD || cmd == SDPCM_TEST_ECHOREQ
4635 || cmd == SDPCM_TEST_ECHORSP) {
4636 if (pktlen != len + SDPCM_TEST_HDRLEN) {
4637 DHD_ERROR(("dhdsdio_testrcv: frame length mismatch, "
4638 "pktlen %d seq %d" " cmd %d extra %d len %d\n",
4639 pktlen, seq, cmd, extra, len));
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004640 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004641 return;
4642 }
4643 }
4644
4645 /* Process as per command */
4646 switch (cmd) {
4647 case SDPCM_TEST_ECHOREQ:
4648 /* Rx->Tx turnaround ok (even on NDIS w/current
4649 implementation) */
Arend van Spriel54991ad2010-11-23 14:06:24 +01004650 *(u8 *) (pkt->data) = SDPCM_TEST_ECHORSP;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004651 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true) == 0) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004652 bus->pktgen_sent++;
4653 } else {
4654 bus->pktgen_fail++;
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004655 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004656 }
4657 bus->pktgen_rcvd++;
4658 break;
4659
4660 case SDPCM_TEST_ECHORSP:
4661 if (bus->ext_loop) {
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004662 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004663 bus->pktgen_rcvd++;
4664 break;
4665 }
4666
4667 for (offset = 0; offset < len; offset++, data++) {
4668 if (*data != SDPCM_TEST_FILL(offset, extra)) {
4669 DHD_ERROR(("dhdsdio_testrcv: echo data mismatch: " "offset %d (len %d) expect 0x%02x rcvd 0x%02x\n",
4670 offset, len,
4671 SDPCM_TEST_FILL(offset, extra), *data));
4672 break;
4673 }
4674 }
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004675 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004676 bus->pktgen_rcvd++;
4677 break;
4678
4679 case SDPCM_TEST_DISCARD:
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004680 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004681 bus->pktgen_rcvd++;
4682 break;
4683
4684 case SDPCM_TEST_BURST:
4685 case SDPCM_TEST_SEND:
4686 default:
4687 DHD_INFO(("dhdsdio_testrcv: unsupported or unknown command, "
4688 "pktlen %d seq %d" " cmd %d extra %d len %d\n",
4689 pktlen, seq, cmd, extra, len));
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004690 bcm_pkt_buf_free_skb(pkt, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004691 break;
4692 }
4693
4694 /* For recv mode, stop at limie (and tell dongle to stop sending) */
4695 if (bus->pktgen_mode == DHD_PKTGEN_RECV) {
4696 if (bus->pktgen_total
4697 && (bus->pktgen_rcvd >= bus->pktgen_total)) {
4698 bus->pktgen_count = 0;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004699 dhdsdio_sdtest_set(bus, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004700 }
4701 }
4702}
4703#endif /* SDTEST */
4704
4705extern bool dhd_bus_watchdog(dhd_pub_t *dhdp)
4706{
4707 dhd_bus_t *bus;
4708
4709 DHD_TIMER(("%s: Enter\n", __func__));
4710
4711 bus = dhdp->bus;
4712
4713 if (bus->dhd->dongle_reset)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004714 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004715
4716 /* Ignore the timer if simulating bus down */
4717 if (bus->sleeping)
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004718 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004719
4720 dhd_os_sdlock(bus->dhd);
4721
4722 /* Poll period: check device if appropriate. */
4723 if (bus->poll && (++bus->polltick >= bus->pollrate)) {
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004724 u32 intstatus = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004725
4726 /* Reset poll tick */
4727 bus->polltick = 0;
4728
4729 /* Check device if no interrupts */
4730 if (!bus->intr || (bus->intrcount == bus->lastintrs)) {
4731
4732 if (!bus->dpc_sched) {
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004733 u8 devpend;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004734 devpend = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_0,
4735 SDIOD_CCCR_INTPEND,
4736 NULL);
4737 intstatus =
4738 devpend & (INTR_STATUS_FUNC1 |
4739 INTR_STATUS_FUNC2);
4740 }
4741
4742 /* If there is something, make like the ISR and
4743 schedule the DPC */
4744 if (intstatus) {
4745 bus->pollcnt++;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004746 bus->ipend = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004747 if (bus->intr)
4748 bcmsdh_intr_disable(bus->sdh);
4749
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004750 bus->dpc_sched = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004751 dhd_sched_dpc(bus->dhd);
4752
4753 }
4754 }
4755
4756 /* Update interrupt tracking */
4757 bus->lastintrs = bus->intrcount;
4758 }
4759#ifdef DHD_DEBUG
4760 /* Poll for console output periodically */
4761 if (dhdp->busstate == DHD_BUS_DATA && dhd_console_ms != 0) {
4762 bus->console.count += dhd_watchdog_ms;
4763 if (bus->console.count >= dhd_console_ms) {
4764 bus->console.count -= dhd_console_ms;
4765 /* Make sure backplane clock is on */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004766 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004767 if (dhdsdio_readconsole(bus) < 0)
4768 dhd_console_ms = 0; /* On error,
4769 stop trying */
4770 }
4771 }
4772#endif /* DHD_DEBUG */
4773
4774#ifdef SDTEST
4775 /* Generate packets if configured */
4776 if (bus->pktgen_count && (++bus->pktgen_tick >= bus->pktgen_freq)) {
4777 /* Make sure backplane clock is on */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004778 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004779 bus->pktgen_tick = 0;
4780 dhdsdio_pktgen(bus);
4781 }
4782#endif
4783
4784 /* On idle timeout clear activity flag and/or turn off clock */
4785 if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
4786 if (++bus->idlecount >= bus->idletime) {
4787 bus->idlecount = 0;
4788 if (bus->activity) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004789 bus->activity = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004790 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
4791 } else {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004792 dhdsdio_clkctl(bus, CLK_NONE, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004793 }
4794 }
4795 }
4796
4797 dhd_os_sdunlock(bus->dhd);
4798
4799 return bus->ipend;
4800}
4801
4802#ifdef DHD_DEBUG
Greg Kroah-Hartman580a0bd2010-10-05 11:09:48 -07004803extern int dhd_bus_console_in(dhd_pub_t *dhdp, unsigned char *msg, uint msglen)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004804{
4805 dhd_bus_t *bus = dhdp->bus;
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07004806 u32 addr, val;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004807 int rv;
Arend van Sprielc26b1372010-11-23 14:06:23 +01004808 struct sk_buff *pkt;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004809
4810 /* Address could be zero if CONSOLE := 0 in dongle Makefile */
4811 if (bus->console_addr == 0)
Roland Vossene10d82d2011-05-03 11:35:19 +02004812 return -ENOTSUPP;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004813
4814 /* Exclusive bus access */
4815 dhd_os_sdlock(bus->dhd);
4816
4817 /* Don't allow input if dongle is in reset */
4818 if (bus->dhd->dongle_reset) {
4819 dhd_os_sdunlock(bus->dhd);
Roland Vossenb74ac122011-05-03 11:35:20 +02004820 return -EPERM;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004821 }
4822
4823 /* Request clock to allow SDIO accesses */
4824 BUS_WAKE(bus);
4825 /* No pend allowed since txpkt is called later, ht clk has to be on */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004826 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004827
4828 /* Zero cbuf_index */
Greg Kroah-Hartmance0f1b82010-10-08 11:44:45 -07004829 addr = bus->console_addr + offsetof(hndrte_cons_t, cbuf_idx);
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03004830 val = cpu_to_le32(0);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004831 rv = dhdsdio_membytes(bus, true, addr, (u8 *)&val, sizeof(val));
Jason Cooper9b890322010-09-30 15:15:39 -04004832 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004833 goto done;
4834
4835 /* Write message into cbuf */
Greg Kroah-Hartmance0f1b82010-10-08 11:44:45 -07004836 addr = bus->console_addr + offsetof(hndrte_cons_t, cbuf);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004837 rv = dhdsdio_membytes(bus, true, addr, (u8 *)msg, msglen);
Jason Cooper9b890322010-09-30 15:15:39 -04004838 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004839 goto done;
4840
4841 /* Write length into vcons_in */
Greg Kroah-Hartmance0f1b82010-10-08 11:44:45 -07004842 addr = bus->console_addr + offsetof(hndrte_cons_t, vcons_in);
Stanislav Fomichev628f10b2011-02-20 21:43:32 +03004843 val = cpu_to_le32(msglen);
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004844 rv = dhdsdio_membytes(bus, true, addr, (u8 *)&val, sizeof(val));
Jason Cooper9b890322010-09-30 15:15:39 -04004845 if (rv < 0)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004846 goto done;
4847
4848 /* Bump dongle by sending an empty event pkt.
4849 * sdpcm_sendup (RX) checks for virtual console input.
4850 */
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02004851 pkt = bcm_pkt_buf_get_skb(4 + SDPCM_RESERVE);
Jason Cooper9b890322010-09-30 15:15:39 -04004852 if ((pkt != NULL) && bus->clkstate == CLK_AVAIL)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004853 dhdsdio_txpkt(bus, pkt, SDPCM_EVENT_CHANNEL, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004854
4855done:
4856 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004857 bus->activity = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004858 dhdsdio_clkctl(bus, CLK_NONE, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004859 }
4860
4861 dhd_os_sdunlock(bus->dhd);
4862
4863 return rv;
4864}
4865#endif /* DHD_DEBUG */
4866
4867#ifdef DHD_DEBUG
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07004868static void dhd_dump_cis(uint fn, u8 *cis)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004869{
4870 uint byte, tag, tdata;
4871 DHD_INFO(("Function %d CIS:\n", fn));
4872
4873 for (tdata = byte = 0; byte < SBSDIO_CIS_SIZE_LIMIT; byte++) {
4874 if ((byte % 16) == 0)
4875 DHD_INFO((" "));
4876 DHD_INFO(("%02x ", cis[byte]));
4877 if ((byte % 16) == 15)
4878 DHD_INFO(("\n"));
4879 if (!tdata--) {
4880 tag = cis[byte];
4881 if (tag == 0xff)
4882 break;
4883 else if (!tag)
4884 tdata = 0;
4885 else if ((byte + 1) < SBSDIO_CIS_SIZE_LIMIT)
4886 tdata = cis[byte + 1] + 1;
4887 else
4888 DHD_INFO(("]"));
4889 }
4890 }
4891 if ((byte % 16) != 15)
4892 DHD_INFO(("\n"));
4893}
4894#endif /* DHD_DEBUG */
4895
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004896static bool dhdsdio_chipmatch(u16 chipid)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004897{
4898 if (chipid == BCM4325_CHIP_ID)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004899 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004900 if (chipid == BCM4329_CHIP_ID)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004901 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004902 if (chipid == BCM4319_CHIP_ID)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004903 return true;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004904 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004905}
4906
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004907static void *dhdsdio_probe(u16 venid, u16 devid, u16 bus_no,
4908 u16 slot, u16 func, uint bustype, void *regsva,
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01004909 void *sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004910{
4911 int ret;
4912 dhd_bus_t *bus;
4913
4914 /* Init global variables at run-time, not as part of the declaration.
4915 * This is required to support init/de-init of the driver.
4916 * Initialization
4917 * of globals as part of the declaration results in non-deterministic
4918 * behavior since the value of the globals may be different on the
4919 * first time that the driver is initialized vs subsequent
4920 * initializations.
4921 */
4922 dhd_txbound = DHD_TXBOUND;
4923 dhd_rxbound = DHD_RXBOUND;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004924 dhd_alignctl = true;
4925 sd1idle = true;
4926 dhd_readahead = true;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004927 retrydata = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004928 dhd_dongle_memsize = 0;
4929 dhd_txminmax = DHD_TXMINMAX;
4930
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07004931 forcealign = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004932
4933 dhd_common_init();
4934
4935 DHD_TRACE(("%s: Enter\n", __func__));
4936 DHD_INFO(("%s: venid 0x%04x devid 0x%04x\n", __func__, venid, devid));
4937
4938 /* We make assumptions about address window mappings */
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07004939 ASSERT((unsigned long)regsva == SI_ENUM_BASE);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004940
4941 /* BCMSDH passes venid and devid based on CIS parsing -- but
4942 * low-power start
4943 * means early parse could fail, so here we should get either an ID
4944 * we recognize OR (-1) indicating we must request power first.
4945 */
4946 /* Check the Vendor ID */
4947 switch (venid) {
4948 case 0x0000:
Stanislav Fomichevbe1c09f2011-03-28 01:31:36 +04004949 case PCI_VENDOR_ID_BROADCOM:
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004950 break;
4951 default:
4952 DHD_ERROR(("%s: unknown vendor: 0x%04x\n", __func__, venid));
4953 return NULL;
4954 }
4955
4956 /* Check the Device ID and make sure it's one that we support */
4957 switch (devid) {
4958 case BCM4325_D11DUAL_ID: /* 4325 802.11a/g id */
4959 case BCM4325_D11G_ID: /* 4325 802.11g 2.4Ghz band id */
4960 case BCM4325_D11A_ID: /* 4325 802.11a 5Ghz band id */
4961 DHD_INFO(("%s: found 4325 Dongle\n", __func__));
4962 break;
4963 case BCM4329_D11NDUAL_ID: /* 4329 802.11n dualband device */
4964 case BCM4329_D11N2G_ID: /* 4329 802.11n 2.4G device */
4965 case BCM4329_D11N5G_ID: /* 4329 802.11n 5G device */
4966 case 0x4329:
4967 DHD_INFO(("%s: found 4329 Dongle\n", __func__));
4968 break;
4969 case BCM4319_D11N_ID: /* 4319 802.11n id */
4970 case BCM4319_D11N2G_ID: /* 4319 802.11n2g id */
4971 case BCM4319_D11N5G_ID: /* 4319 802.11n5g id */
4972 DHD_INFO(("%s: found 4319 Dongle\n", __func__));
4973 break;
4974 case 0:
4975 DHD_INFO(("%s: allow device id 0, will check chip internals\n",
4976 __func__));
4977 break;
4978
4979 default:
4980 DHD_ERROR(("%s: skipping 0x%04x/0x%04x, not a dongle\n",
4981 __func__, venid, devid));
4982 return NULL;
4983 }
4984
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004985 /* Allocate private bus interface state */
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02004986 bus = kzalloc(sizeof(dhd_bus_t), GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004987 if (!bus) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02004988 DHD_ERROR(("%s: kmalloc of dhd_bus_t failed\n", __func__));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004989 goto fail;
4990 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004991 bus->sdh = sdh;
Greg Kroah-Hartman7d4df482010-10-07 17:04:47 -07004992 bus->cl_devid = (u16) devid;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004993 bus->bus = DHD_BUS;
4994 bus->tx_seq = SDPCM_SEQUENCE_WRAP - 1;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07004995 bus->usebufpool = false; /* Use bufpool if allocated,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07004996 else use locally malloced rxbuf */
4997
4998 /* attempt to attach to the dongle */
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01004999 if (!(dhdsdio_probe_attach(bus, sdh, regsva, devid))) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005000 DHD_ERROR(("%s: dhdsdio_probe_attach failed\n", __func__));
5001 goto fail;
5002 }
5003
5004 /* Attach to the dhd/OS/network interface */
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005005 bus->dhd = dhd_attach(bus, SDPCM_RESERVE);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005006 if (!bus->dhd) {
5007 DHD_ERROR(("%s: dhd_attach failed\n", __func__));
5008 goto fail;
5009 }
5010
5011 /* Allocate buffers */
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005012 if (!(dhdsdio_probe_malloc(bus, sdh))) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005013 DHD_ERROR(("%s: dhdsdio_probe_malloc failed\n", __func__));
5014 goto fail;
5015 }
5016
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005017 if (!(dhdsdio_probe_init(bus, sdh))) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005018 DHD_ERROR(("%s: dhdsdio_probe_init failed\n", __func__));
5019 goto fail;
5020 }
5021
5022 /* Register interrupt callback, but mask it (not operational yet). */
5023 DHD_INTR(("%s: disable SDIO interrupts (not interested yet)\n",
5024 __func__));
5025 bcmsdh_intr_disable(sdh);
5026 ret = bcmsdh_intr_reg(sdh, dhdsdio_isr, bus);
5027 if (ret != 0) {
5028 DHD_ERROR(("%s: FAILED: bcmsdh_intr_reg returned %d\n",
5029 __func__, ret));
5030 goto fail;
5031 }
5032 DHD_INTR(("%s: registered SDIO interrupt function ok\n", __func__));
5033
5034 DHD_INFO(("%s: completed!!\n", __func__));
5035
5036 /* if firmware path present try to download and bring up bus */
Jason Cooper9b890322010-09-30 15:15:39 -04005037 ret = dhd_bus_start(bus->dhd);
5038 if (ret != 0) {
Roland Vossene10d82d2011-05-03 11:35:19 +02005039 if (ret == -ENOLINK) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005040 DHD_ERROR(("%s: dongle is not responding\n", __func__));
5041 goto fail;
5042 }
5043 }
5044 /* Ok, have the per-port tell the stack we're open for business */
5045 if (dhd_net_attach(bus->dhd, 0) != 0) {
5046 DHD_ERROR(("%s: Net attach failed!!\n", __func__));
5047 goto fail;
5048 }
5049
5050 return bus;
5051
5052fail:
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005053 dhdsdio_release(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005054 return NULL;
5055}
5056
5057static bool
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005058dhdsdio_probe_attach(struct dhd_bus *bus, void *sdh, void *regsva, u16 devid)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005059{
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005060 u8 clkctl = 0;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005061 int err = 0;
5062
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005063 bus->alp_only = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005064
5065 /* Return the window to backplane enumeration space for core access */
5066 if (dhdsdio_set_siaddr_window(bus, SI_ENUM_BASE))
5067 DHD_ERROR(("%s: FAILED to return to SI_ENUM_BASE\n", __func__));
5068
5069#ifdef DHD_DEBUG
Arend van Spriel0bef7742011-02-10 12:03:44 +01005070 printk(KERN_DEBUG "F1 signature read @0x18000000=0x%4x\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005071 bcmsdh_reg_read(bus->sdh, SI_ENUM_BASE, 4));
5072
5073#endif /* DHD_DEBUG */
5074
Franky Linc05df632011-04-25 19:34:07 -07005075 /*
5076 * Force PLL off until dhdsdio_chip_attach()
5077 * programs PLL control regs
5078 */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005079
5080 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5081 DHD_INIT_CLKCTL1, &err);
5082 if (!err)
5083 clkctl =
5084 bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5085 &err);
5086
5087 if (err || ((clkctl & ~SBSDIO_AVBITS) != DHD_INIT_CLKCTL1)) {
5088 DHD_ERROR(("dhdsdio_probe: ChipClkCSR access: err %d wrote "
5089 "0x%02x read 0x%02x\n",
5090 err, DHD_INIT_CLKCTL1, clkctl));
5091 goto fail;
5092 }
5093#ifdef DHD_DEBUG
5094 if (DHD_INFO_ON()) {
5095 uint fn, numfn;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005096 u8 *cis[SDIOD_MAX_IOFUNCS];
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005097 int err = 0;
5098
5099 numfn = bcmsdh_query_iofnum(sdh);
5100 ASSERT(numfn <= SDIOD_MAX_IOFUNCS);
5101
5102 /* Make sure ALP is available before trying to read CIS */
5103 SPINWAIT(((clkctl = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
5104 SBSDIO_FUNC1_CHIPCLKCSR,
5105 NULL)),
5106 !SBSDIO_ALPAV(clkctl)), PMU_MAX_TRANSITION_DLY);
5107
5108 /* Now request ALP be put on the bus */
5109 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5110 DHD_INIT_CLKCTL2, &err);
mike.rapoport@gmail.com73831412010-10-13 00:09:07 +02005111 udelay(65);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005112
5113 for (fn = 0; fn <= numfn; fn++) {
Alexander Beregalov12d0eb42011-03-09 03:53:35 +03005114 cis[fn] = kzalloc(SBSDIO_CIS_SIZE_LIMIT, GFP_ATOMIC);
Jason Cooper9b890322010-09-30 15:15:39 -04005115 if (!cis[fn]) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005116 DHD_INFO(("dhdsdio_probe: fn %d cis malloc "
5117 "failed\n", fn));
5118 break;
5119 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005120
Jason Cooper9b890322010-09-30 15:15:39 -04005121 err = bcmsdh_cis_read(sdh, fn, cis[fn],
5122 SBSDIO_CIS_SIZE_LIMIT);
5123 if (err) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005124 DHD_INFO(("dhdsdio_probe: fn %d cis read "
5125 "err %d\n", fn, err));
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005126 kfree(cis[fn]);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005127 break;
5128 }
5129 dhd_dump_cis(fn, cis[fn]);
5130 }
5131
5132 while (fn-- > 0) {
5133 ASSERT(cis[fn]);
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005134 kfree(cis[fn]);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005135 }
5136
5137 if (err) {
5138 DHD_ERROR(("dhdsdio_probe: error read/parsing CIS\n"));
5139 goto fail;
5140 }
5141 }
5142#endif /* DHD_DEBUG */
5143
Franky Lincb63e4c2011-04-25 15:45:08 -07005144 if (dhdsdio_chip_attach(bus, regsva)) {
5145 DHD_ERROR(("%s: dhdsdio_chip_attach failed!\n", __func__));
5146 goto fail;
5147 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005148
Franky Linc05df632011-04-25 19:34:07 -07005149 bcmsdh_chipinfo(sdh, bus->ci->chip, bus->ci->chiprev);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005150
Franky Linc05df632011-04-25 19:34:07 -07005151 if (!dhdsdio_chipmatch((u16) bus->ci->chip)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005152 DHD_ERROR(("%s: unsupported chip: 0x%04x\n",
Franky Linc05df632011-04-25 19:34:07 -07005153 __func__, bus->ci->chip));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005154 goto fail;
5155 }
5156
Franky Lin5d0d7a92011-04-25 19:34:05 -07005157 dhdsdio_sdiod_drive_strength_init(bus, dhd_sdiod_drive_strength);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005158
5159 /* Get info on the ARM and SOCRAM cores... */
5160 if (!DHD_NOPMU(bus)) {
Franky Linc05df632011-04-25 19:34:07 -07005161 bus->armrev = SBCOREREV(bcmsdh_reg_read(bus->sdh,
5162 CORE_SB(bus->ci->armcorebase, sbidhigh), 4));
5163 bus->orig_ramsize = bus->ci->ramsize;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005164 if (!(bus->orig_ramsize)) {
5165 DHD_ERROR(("%s: failed to find SOCRAM memory!\n",
5166 __func__));
5167 goto fail;
5168 }
5169 bus->ramsize = bus->orig_ramsize;
5170 if (dhd_dongle_memsize)
5171 dhd_dongle_setmemsize(bus, dhd_dongle_memsize);
5172
5173 DHD_ERROR(("DHD: dongle ram size is set to %d(orig %d)\n",
5174 bus->ramsize, bus->orig_ramsize));
5175 }
5176
Franky Linc05df632011-04-25 19:34:07 -07005177 bus->regs = (void *)bus->ci->buscorebase;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005178
5179 /* Set core control so an SDIO reset does a backplane reset */
Arend van Sprielff31c542011-03-01 10:56:54 +01005180 OR_REG(&bus->regs->corecontrol, CC_BPRESEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005181
Henry Ptasinskib33f0e22011-05-10 22:25:29 +02005182 bcm_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005183
5184 /* Locate an appropriately-aligned portion of hdrbuf */
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005185 bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0], DHD_SDALIGN);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005186
5187 /* Set the poll and/or interrupt flags */
5188 bus->intr = (bool) dhd_intr;
Jason Cooper9b890322010-09-30 15:15:39 -04005189 bus->poll = (bool) dhd_poll;
5190 if (bus->poll)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005191 bus->pollrate = 1;
5192
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005193 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005194
5195fail:
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005196 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005197}
5198
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005199static bool dhdsdio_probe_malloc(dhd_bus_t *bus, void *sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005200{
5201 DHD_TRACE(("%s: Enter\n", __func__));
5202
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005203 if (bus->dhd->maxctl) {
5204 bus->rxblen =
Greg Kroah-Hartmane18d5312010-10-08 11:59:06 -07005205 roundup((bus->dhd->maxctl + SDPCM_HDRLEN),
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005206 ALIGNMENT) + DHD_SDALIGN;
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005207 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005208 if (!(bus->rxbuf)) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005209 DHD_ERROR(("%s: kmalloc of %d-byte rxbuf failed\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005210 __func__, bus->rxblen));
5211 goto fail;
5212 }
5213 }
5214
5215 /* Allocate buffer to receive glomed packet */
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005216 bus->databuf = kmalloc(MAX_DATA_BUF, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005217 if (!(bus->databuf)) {
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005218 DHD_ERROR(("%s: kmalloc of %d-byte databuf failed\n",
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005219 __func__, MAX_DATA_BUF));
5220 /* release rxbuf which was already located as above */
5221 if (!bus->rxblen)
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005222 kfree(bus->rxbuf);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005223 goto fail;
5224 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005225
5226 /* Align the buffer */
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005227 if ((unsigned long)bus->databuf % DHD_SDALIGN)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005228 bus->dataptr =
5229 bus->databuf + (DHD_SDALIGN -
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005230 ((unsigned long)bus->databuf % DHD_SDALIGN));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005231 else
5232 bus->dataptr = bus->databuf;
5233
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005234 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005235
5236fail:
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005237 return false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005238}
5239
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005240static bool dhdsdio_probe_init(dhd_bus_t *bus, void *sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005241{
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07005242 s32 fnum;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005243
5244 DHD_TRACE(("%s: Enter\n", __func__));
5245
5246#ifdef SDTEST
5247 dhdsdio_pktgen_init(bus);
5248#endif /* SDTEST */
5249
5250 /* Disable F2 to clear any intermediate frame state on the dongle */
5251 bcmsdh_cfg_write(sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, SDIO_FUNC_ENABLE_1,
5252 NULL);
5253
5254 bus->dhd->busstate = DHD_BUS_DOWN;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005255 bus->sleeping = false;
5256 bus->rxflow = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005257 bus->prev_rxlim_hit = 0;
5258
5259 /* Done with backplane-dependent accesses, can drop clock... */
5260 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
5261
5262 /* ...and initialize clock/power states */
5263 bus->clkstate = CLK_SDONLY;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07005264 bus->idletime = (s32) dhd_idletime;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005265 bus->idleclock = DHD_IDLE_ACTIVE;
5266
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005267 /* Query the F2 block size, set roundup accordingly */
5268 fnum = 2;
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07005269 if (bcmsdh_iovar_op(sdh, "sd_blocksize", &fnum, sizeof(s32),
Roland Vossena1c5ad82011-04-11 15:16:24 +02005270 &bus->blocksize, sizeof(s32), false) != 0) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005271 bus->blocksize = 0;
5272 DHD_ERROR(("%s: fail on %s get\n", __func__, "sd_blocksize"));
5273 } else {
5274 DHD_INFO(("%s: Initial value for %s is %d\n",
5275 __func__, "sd_blocksize", bus->blocksize));
5276 }
Greg Kroah-Hartman7068c2f2010-10-08 11:34:59 -07005277 bus->roundup = min(max_roundup, bus->blocksize);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005278
5279 /* Query if bus module supports packet chaining,
5280 default to use if supported */
5281 if (bcmsdh_iovar_op(sdh, "sd_rxchain", NULL, 0,
Greg Kroah-Hartman3e264162010-10-08 11:11:13 -07005282 &bus->sd_rxchain, sizeof(s32),
Roland Vossena1c5ad82011-04-11 15:16:24 +02005283 false) != 0) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005284 bus->sd_rxchain = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005285 } else {
5286 DHD_INFO(("%s: bus module (through bcmsdh API) %s chaining\n",
5287 __func__,
5288 (bus->sd_rxchain ? "supports" : "does not support")));
5289 }
5290 bus->use_rxchain = (bool) bus->sd_rxchain;
5291
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005292 return true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005293}
5294
5295bool
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005296dhd_bus_download_firmware(struct dhd_bus *bus, char *fw_path, char *nv_path)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005297{
5298 bool ret;
5299 bus->fw_path = fw_path;
5300 bus->nv_path = nv_path;
5301
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005302 ret = dhdsdio_download_firmware(bus, bus->sdh);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005303
5304 return ret;
5305}
5306
5307static bool
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005308dhdsdio_download_firmware(struct dhd_bus *bus, void *sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005309{
5310 bool ret;
5311
5312 /* Download the firmware */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005313 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005314
5315 ret = _dhdsdio_download_firmware(bus) == 0;
5316
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005317 dhdsdio_clkctl(bus, CLK_SDONLY, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005318
5319 return ret;
5320}
5321
5322/* Detach and free everything */
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005323static void dhdsdio_release(dhd_bus_t *bus)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005324{
5325 DHD_TRACE(("%s: Enter\n", __func__));
5326
5327 if (bus) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005328 /* De-register interrupt handler */
5329 bcmsdh_intr_disable(bus->sdh);
5330 bcmsdh_intr_dereg(bus->sdh);
5331
5332 if (bus->dhd) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005333 dhd_detach(bus->dhd);
Franky Lincee3cf42011-04-25 19:34:06 -07005334 dhdsdio_release_dongle(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005335 bus->dhd = NULL;
5336 }
5337
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005338 dhdsdio_release_malloc(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005339
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005340 kfree(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005341 }
5342
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005343 DHD_TRACE(("%s: Disconnected\n", __func__));
5344}
5345
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005346static void dhdsdio_release_malloc(dhd_bus_t *bus)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005347{
5348 DHD_TRACE(("%s: Enter\n", __func__));
5349
5350 if (bus->dhd && bus->dhd->dongle_reset)
5351 return;
5352
5353 if (bus->rxbuf) {
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005354 kfree(bus->rxbuf);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005355 bus->rxctl = bus->rxbuf = NULL;
5356 bus->rxlen = 0;
5357 }
5358
Ilia Mirkin46d994b2011-03-13 00:28:56 -05005359 kfree(bus->databuf);
5360 bus->databuf = NULL;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005361}
5362
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005363static void dhdsdio_release_dongle(dhd_bus_t *bus)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005364{
5365 DHD_TRACE(("%s: Enter\n", __func__));
5366
5367 if (bus->dhd && bus->dhd->dongle_reset)
5368 return;
5369
Franky Linc05df632011-04-25 19:34:07 -07005370 if (bus->ci) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005371 dhdsdio_clkctl(bus, CLK_AVAIL, false);
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005372 dhdsdio_clkctl(bus, CLK_NONE, false);
Franky Lincee3cf42011-04-25 19:34:06 -07005373 dhdsdio_chip_detach(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005374 if (bus->vars && bus->varsz)
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005375 kfree(bus->vars);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005376 bus->vars = NULL;
5377 }
5378
5379 DHD_TRACE(("%s: Disconnected\n", __func__));
5380}
5381
5382static void dhdsdio_disconnect(void *ptr)
5383{
5384 dhd_bus_t *bus = (dhd_bus_t *)ptr;
5385
5386 DHD_TRACE(("%s: Enter\n", __func__));
5387
5388 if (bus) {
5389 ASSERT(bus->dhd);
Arend van Spriel3c9d4c32011-03-02 21:18:48 +01005390 dhdsdio_release(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005391 }
5392
5393 DHD_TRACE(("%s: Disconnected\n", __func__));
5394}
5395
5396/* Register/Unregister functions are called by the main DHD entry
5397 * point (e.g. module insertion) to link with the bus driver, in
5398 * order to look for or await the device.
5399 */
5400
5401static bcmsdh_driver_t dhd_sdio = {
5402 dhdsdio_probe,
5403 dhdsdio_disconnect
5404};
5405
5406int dhd_bus_register(void)
5407{
5408 DHD_TRACE(("%s: Enter\n", __func__));
5409
5410 return bcmsdh_register(&dhd_sdio);
5411}
5412
5413void dhd_bus_unregister(void)
5414{
5415 DHD_TRACE(("%s: Enter\n", __func__));
5416
5417 bcmsdh_unregister();
5418}
5419
5420#ifdef BCMEMBEDIMAGE
5421static int dhdsdio_download_code_array(struct dhd_bus *bus)
5422{
5423 int bcmerror = -1;
5424 int offset = 0;
5425
5426 DHD_INFO(("%s: download embedded firmware...\n", __func__));
5427
5428 /* Download image */
5429 while ((offset + MEMBLOCK) < sizeof(dlarray)) {
5430 bcmerror =
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005431 dhdsdio_membytes(bus, true, offset, dlarray + offset,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005432 MEMBLOCK);
5433 if (bcmerror) {
5434 DHD_ERROR(("%s: error %d on writing %d membytes at "
5435 "0x%08x\n",
5436 __func__, bcmerror, MEMBLOCK, offset));
5437 goto err;
5438 }
5439
5440 offset += MEMBLOCK;
5441 }
5442
5443 if (offset < sizeof(dlarray)) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005444 bcmerror = dhdsdio_membytes(bus, true, offset,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005445 dlarray + offset,
5446 sizeof(dlarray) - offset);
5447 if (bcmerror) {
5448 DHD_ERROR(("%s: error %d on writing %d membytes at "
5449 "0x%08x\n", __func__, bcmerror,
5450 sizeof(dlarray) - offset, offset));
5451 goto err;
5452 }
5453 }
5454#ifdef DHD_DEBUG
5455 /* Upload and compare the downloaded code */
5456 {
5457 unsigned char *ularray;
5458
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005459 ularray = kmalloc(bus->ramsize, GFP_ATOMIC);
Alexander Beregalov570edd32011-03-13 21:58:47 +03005460 if (!ularray) {
Roland Vossene10d82d2011-05-03 11:35:19 +02005461 bcmerror = -ENOMEM;
Alexander Beregalov570edd32011-03-13 21:58:47 +03005462 goto err;
5463 }
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005464 /* Upload image to verify downloaded contents. */
5465 offset = 0;
5466 memset(ularray, 0xaa, bus->ramsize);
5467 while ((offset + MEMBLOCK) < sizeof(dlarray)) {
5468 bcmerror =
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005469 dhdsdio_membytes(bus, false, offset,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005470 ularray + offset, MEMBLOCK);
5471 if (bcmerror) {
5472 DHD_ERROR(("%s: error %d on reading %d membytes"
5473 " at 0x%08x\n",
5474 __func__, bcmerror, MEMBLOCK, offset));
Alexander Beregalov570edd32011-03-13 21:58:47 +03005475 goto free;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005476 }
5477
5478 offset += MEMBLOCK;
5479 }
5480
5481 if (offset < sizeof(dlarray)) {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005482 bcmerror = dhdsdio_membytes(bus, false, offset,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005483 ularray + offset,
5484 sizeof(dlarray) - offset);
5485 if (bcmerror) {
5486 DHD_ERROR(("%s: error %d on reading %d membytes at 0x%08x\n",
5487 __func__, bcmerror,
5488 sizeof(dlarray) - offset, offset));
Alexander Beregalov570edd32011-03-13 21:58:47 +03005489 goto free;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005490 }
5491 }
5492
5493 if (memcmp(dlarray, ularray, sizeof(dlarray))) {
5494 DHD_ERROR(("%s: Downloaded image is corrupted.\n",
5495 __func__));
5496 ASSERT(0);
Alexander Beregalov570edd32011-03-13 21:58:47 +03005497 goto free;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005498 } else
5499 DHD_ERROR(("%s: Download/Upload/Compare succeeded.\n",
5500 __func__));
Alexander Beregalov570edd32011-03-13 21:58:47 +03005501free:
mike.rapoport@gmail.com182acb32010-10-13 00:09:12 +02005502 kfree(ularray);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005503 }
5504#endif /* DHD_DEBUG */
5505
5506err:
5507 return bcmerror;
5508}
5509#endif /* BCMEMBEDIMAGE */
5510
5511static int dhdsdio_download_code_file(struct dhd_bus *bus, char *fw_path)
5512{
5513 int bcmerror = -1;
5514 int offset = 0;
5515 uint len;
5516 void *image = NULL;
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005517 u8 *memblock = NULL, *memptr;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005518
5519 DHD_INFO(("%s: download firmware %s\n", __func__, fw_path));
5520
5521 image = dhd_os_open_image(fw_path);
5522 if (image == NULL)
5523 goto err;
5524
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005525 memptr = memblock = kmalloc(MEMBLOCK + DHD_SDALIGN, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005526 if (memblock == NULL) {
5527 DHD_ERROR(("%s: Failed to allocate memory %d bytes\n",
5528 __func__, MEMBLOCK));
5529 goto err;
5530 }
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005531 if ((u32)(unsigned long)memblock % DHD_SDALIGN)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005532 memptr +=
Greg Kroah-Hartmanf024c482010-10-21 10:50:21 -07005533 (DHD_SDALIGN - ((u32)(unsigned long)memblock % DHD_SDALIGN));
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005534
5535 /* Download image */
5536 while ((len =
5537 dhd_os_get_image_block((char *)memptr, MEMBLOCK, image))) {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005538 bcmerror = dhdsdio_membytes(bus, true, offset, memptr, len);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005539 if (bcmerror) {
5540 DHD_ERROR(("%s: error %d on writing %d membytes at "
5541 "0x%08x\n", __func__, bcmerror, MEMBLOCK, offset));
5542 goto err;
5543 }
5544
5545 offset += MEMBLOCK;
5546 }
5547
5548err:
Ilia Mirkin46d994b2011-03-13 00:28:56 -05005549 kfree(memblock);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005550
5551 if (image)
5552 dhd_os_close_image(image);
5553
5554 return bcmerror;
5555}
5556
5557/*
5558 * ProcessVars:Takes a buffer of "<var>=<value>\n" lines read from a file
5559 * and ending in a NUL.
5560 * Removes carriage returns, empty lines, comment lines, and converts
5561 * newlines to NULs.
5562 * Shortens buffer as needed and pads with NULs. End of buffer is marked
5563 * by two NULs.
5564*/
5565
5566static uint process_nvram_vars(char *varbuf, uint len)
5567{
5568 char *dp;
5569 bool findNewline;
5570 int column;
5571 uint buf_len, n;
5572
5573 dp = varbuf;
5574
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005575 findNewline = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005576 column = 0;
5577
5578 for (n = 0; n < len; n++) {
5579 if (varbuf[n] == 0)
5580 break;
5581 if (varbuf[n] == '\r')
5582 continue;
5583 if (findNewline && varbuf[n] != '\n')
5584 continue;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005585 findNewline = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005586 if (varbuf[n] == '#') {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005587 findNewline = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005588 continue;
5589 }
5590 if (varbuf[n] == '\n') {
5591 if (column == 0)
5592 continue;
5593 *dp++ = 0;
5594 column = 0;
5595 continue;
5596 }
5597 *dp++ = varbuf[n];
5598 column++;
5599 }
5600 buf_len = dp - varbuf;
5601
5602 while (dp < varbuf + n)
5603 *dp++ = 0;
5604
5605 return buf_len;
5606}
5607
5608/*
5609 EXAMPLE: nvram_array
5610 nvram_arry format:
5611 name=value
5612 Use carriage return at the end of each assignment,
5613 and an empty string with
5614 carriage return at the end of array.
5615
5616 For example:
5617 unsigned char nvram_array[] = {"name1=value1\n",
5618 "name2=value2\n", "\n"};
5619 Hex values start with 0x, and mac addr format: xx:xx:xx:xx:xx:xx.
5620
5621 Search "EXAMPLE: nvram_array" to see how the array is activated.
5622*/
5623
5624void dhd_bus_set_nvram_params(struct dhd_bus *bus, const char *nvram_params)
5625{
5626 bus->nvram_params = nvram_params;
5627}
5628
5629static int dhdsdio_download_nvram(struct dhd_bus *bus)
5630{
5631 int bcmerror = -1;
5632 uint len;
5633 void *image = NULL;
5634 char *memblock = NULL;
5635 char *bufp;
5636 char *nv_path;
5637 bool nvram_file_exists;
5638
5639 nv_path = bus->nv_path;
5640
5641 nvram_file_exists = ((nv_path != NULL) && (nv_path[0] != '\0'));
5642 if (!nvram_file_exists && (bus->nvram_params == NULL))
5643 return 0;
5644
5645 if (nvram_file_exists) {
5646 image = dhd_os_open_image(nv_path);
5647 if (image == NULL)
5648 goto err;
5649 }
5650
mike.rapoport@gmail.com5fcc1fc2010-10-13 00:09:10 +02005651 memblock = kmalloc(MEMBLOCK, GFP_ATOMIC);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005652 if (memblock == NULL) {
5653 DHD_ERROR(("%s: Failed to allocate memory %d bytes\n",
5654 __func__, MEMBLOCK));
5655 goto err;
5656 }
5657
5658 /* Download variables */
5659 if (nvram_file_exists) {
5660 len = dhd_os_get_image_block(memblock, MEMBLOCK, image);
5661 } else {
5662 len = strlen(bus->nvram_params);
5663 ASSERT(len <= MEMBLOCK);
5664 if (len > MEMBLOCK)
5665 len = MEMBLOCK;
5666 memcpy(memblock, bus->nvram_params, len);
5667 }
5668
5669 if (len > 0 && len < MEMBLOCK) {
5670 bufp = (char *)memblock;
5671 bufp[len] = 0;
5672 len = process_nvram_vars(bufp, len);
5673 bufp += len;
5674 *bufp++ = 0;
5675 if (len)
5676 bcmerror = dhdsdio_downloadvars(bus, memblock, len + 1);
5677 if (bcmerror) {
5678 DHD_ERROR(("%s: error downloading vars: %d\n",
5679 __func__, bcmerror));
5680 }
5681 } else {
5682 DHD_ERROR(("%s: error reading nvram file: %d\n",
5683 __func__, len));
Roland Vossenb74ac122011-05-03 11:35:20 +02005684 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005685 }
5686
5687err:
Ilia Mirkin46d994b2011-03-13 00:28:56 -05005688 kfree(memblock);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005689
5690 if (image)
5691 dhd_os_close_image(image);
5692
5693 return bcmerror;
5694}
5695
5696static int _dhdsdio_download_firmware(struct dhd_bus *bus)
5697{
5698 int bcmerror = -1;
5699
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005700 bool embed = false; /* download embedded firmware */
5701 bool dlok = false; /* download firmware succeeded */
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005702
5703 /* Out immediately if no image to download */
5704 if ((bus->fw_path == NULL) || (bus->fw_path[0] == '\0')) {
5705#ifdef BCMEMBEDIMAGE
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005706 embed = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005707#else
5708 return bcmerror;
5709#endif
5710 }
5711
5712 /* Keep arm in reset */
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005713 if (dhdsdio_download_state(bus, true)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005714 DHD_ERROR(("%s: error placing ARM core in reset\n", __func__));
5715 goto err;
5716 }
5717
5718 /* External image takes precedence if specified */
5719 if ((bus->fw_path != NULL) && (bus->fw_path[0] != '\0')) {
5720 if (dhdsdio_download_code_file(bus, bus->fw_path)) {
5721 DHD_ERROR(("%s: dongle image file download failed\n",
5722 __func__));
5723#ifdef BCMEMBEDIMAGE
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005724 embed = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005725#else
5726 goto err;
5727#endif
5728 } else {
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005729 embed = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005730 dlok = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005731 }
5732 }
5733#ifdef BCMEMBEDIMAGE
5734 if (embed) {
5735 if (dhdsdio_download_code_array(bus)) {
5736 DHD_ERROR(("%s: dongle image array download failed\n",
5737 __func__));
5738 goto err;
5739 } else {
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005740 dlok = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005741 }
5742 }
5743#endif
5744 if (!dlok) {
5745 DHD_ERROR(("%s: dongle image download failed\n", __func__));
5746 goto err;
5747 }
5748
5749 /* EXAMPLE: nvram_array */
5750 /* If a valid nvram_arry is specified as above, it can be passed
5751 down to dongle */
5752 /* dhd_bus_set_nvram_params(bus, (char *)&nvram_array); */
5753
5754 /* External nvram takes precedence if specified */
5755 if (dhdsdio_download_nvram(bus)) {
5756 DHD_ERROR(("%s: dongle nvram file download failed\n",
5757 __func__));
5758 }
5759
5760 /* Take arm out of reset */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005761 if (dhdsdio_download_state(bus, false)) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005762 DHD_ERROR(("%s: error getting out of ARM core reset\n",
5763 __func__));
5764 goto err;
5765 }
5766
5767 bcmerror = 0;
5768
5769err:
5770 return bcmerror;
5771}
5772
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005773
5774static int
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07005775dhd_bcmsdh_send_buf(dhd_bus_t *bus, u32 addr, uint fn, uint flags,
Arend van Sprielc26b1372010-11-23 14:06:23 +01005776 u8 *buf, uint nbytes, struct sk_buff *pkt,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005777 bcmsdh_cmplt_fn_t complete, void *handle)
5778{
5779 return bcmsdh_send_buf
5780 (bus->sdh, addr, fn, flags, buf, nbytes, pkt, complete,
5781 handle);
5782}
5783
5784uint dhd_bus_chip(struct dhd_bus *bus)
5785{
Franky Linc05df632011-04-25 19:34:07 -07005786 ASSERT(bus->ci != NULL);
5787 return bus->ci->chip;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005788}
5789
5790void *dhd_bus_pub(struct dhd_bus *bus)
5791{
5792 return bus->dhd;
5793}
5794
5795void *dhd_bus_txq(struct dhd_bus *bus)
5796{
5797 return &bus->txq;
5798}
5799
5800uint dhd_bus_hdrlen(struct dhd_bus *bus)
5801{
5802 return SDPCM_HDRLEN;
5803}
5804
Greg Kroah-Hartman3fd79f72010-10-05 10:11:12 -07005805int dhd_bus_devreset(dhd_pub_t *dhdp, u8 flag)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005806{
5807 int bcmerror = 0;
5808 dhd_bus_t *bus;
5809
5810 bus = dhdp->bus;
5811
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005812 if (flag == true) {
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005813 if (!bus->dhd->dongle_reset) {
5814 /* Expect app to have torn down any
5815 connection before calling */
5816 /* Stop the bus, disable F2 */
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005817 dhd_bus_stop(bus, false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005818
5819 /* Clean tx/rx buffer pointers,
5820 detach from the dongle */
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005821 dhdsdio_release_dongle(bus);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005822
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005823 bus->dhd->dongle_reset = true;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005824 bus->dhd->up = false;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005825
5826 DHD_TRACE(("%s: WLAN OFF DONE\n", __func__));
5827 /* App can now remove power from device */
5828 } else
Roland Vossenb74ac122011-05-03 11:35:20 +02005829 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005830 } else {
5831 /* App must have restored power to device before calling */
5832
5833 DHD_TRACE(("\n\n%s: == WLAN ON ==\n", __func__));
5834
5835 if (bus->dhd->dongle_reset) {
5836 /* Turn on WLAN */
5837 /* Reset SD client */
5838 bcmsdh_reset(bus->sdh);
5839
5840 /* Attempt to re-attach & download */
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005841 if (dhdsdio_probe_attach(bus, bus->sdh,
Greg Kroah-Hartman66cbd3a2010-10-08 11:05:47 -07005842 (u32 *) SI_ENUM_BASE,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005843 bus->cl_devid)) {
5844 /* Attempt to download binary to the dongle */
5845 if (dhdsdio_probe_init
Arend van Spriel8da4a3a2011-03-02 21:18:42 +01005846 (bus, bus->sdh)
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005847 && dhdsdio_download_firmware(bus,
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005848 bus->sdh)) {
5849
5850 /* Re-init bus, enable F2 transfer */
5851 dhd_bus_init((dhd_pub_t *) bus->dhd,
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005852 false);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005853
5854#if defined(OOB_INTR_ONLY)
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005855 dhd_enable_oob_intr(bus, true);
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005856#endif /* defined(OOB_INTR_ONLY) */
5857
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005858 bus->dhd->dongle_reset = false;
Greg Kroah-Hartman0f0881b2010-10-12 12:15:18 -07005859 bus->dhd->up = true;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005860
5861 DHD_TRACE(("%s: WLAN ON DONE\n",
5862 __func__));
5863 } else
Roland Vossenb74ac122011-05-03 11:35:20 +02005864 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005865 } else
Roland Vossenb74ac122011-05-03 11:35:20 +02005866 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005867 } else {
Roland Vossenb74ac122011-05-03 11:35:20 +02005868 bcmerror = -EISCONN;
Greg Kroah-Hartman0965ae82010-10-12 12:50:15 -07005869 DHD_ERROR(("%s: Set DEVRESET=false invoked when device "
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005870 "is on\n", __func__));
Roland Vossenb74ac122011-05-03 11:35:20 +02005871 bcmerror = -EIO;
Henry Ptasinskicf2b4482010-09-20 22:33:12 -07005872 }
5873 }
5874 return bcmerror;
5875}
Franky Lincb63e4c2011-04-25 15:45:08 -07005876
5877static int
5878dhdsdio_chip_recognition(bcmsdh_info_t *sdh, struct chip_info *ci, void *regs)
5879{
5880 u32 regdata;
5881
5882 /*
5883 * Get CC core rev
5884 * Chipid is assume to be at offset 0 from regs arg
5885 * For different chiptypes or old sdio hosts w/o chipcommon,
5886 * other ways of recognition should be added here.
5887 */
5888 ci->cccorebase = (u32)regs;
5889 regdata = bcmsdh_reg_read(sdh, CORE_CC_REG(ci->cccorebase, chipid), 4);
5890 ci->chip = regdata & CID_ID_MASK;
5891 ci->chiprev = (regdata & CID_REV_MASK) >> CID_REV_SHIFT;
5892
5893 DHD_INFO(("%s: chipid=0x%x chiprev=%d\n",
5894 __func__, ci->chip, ci->chiprev));
5895
5896 /* Address of cores for new chips should be added here */
5897 switch (ci->chip) {
5898 case BCM4329_CHIP_ID:
5899 ci->buscorebase = BCM4329_CORE_BUS_BASE;
5900 ci->ramcorebase = BCM4329_CORE_SOCRAM_BASE;
5901 ci->armcorebase = BCM4329_CORE_ARM_BASE;
Franky Linc05df632011-04-25 19:34:07 -07005902 ci->ramsize = BCM4329_RAMSIZE;
Franky Lincb63e4c2011-04-25 15:45:08 -07005903 break;
5904 default:
5905 DHD_ERROR(("%s: chipid 0x%x is not supported\n",
5906 __func__, ci->chip));
5907 return -ENODEV;
5908 }
5909
5910 regdata = bcmsdh_reg_read(sdh,
5911 CORE_SB(ci->cccorebase, sbidhigh), 4);
5912 ci->ccrev = SBCOREREV(regdata);
5913
5914 regdata = bcmsdh_reg_read(sdh,
5915 CORE_CC_REG(ci->cccorebase, pmucapabilities), 4);
5916 ci->pmurev = regdata & PCAP_REV_MASK;
5917
5918 regdata = bcmsdh_reg_read(sdh, CORE_SB(ci->buscorebase, sbidhigh), 4);
5919 ci->buscorerev = SBCOREREV(regdata);
5920 ci->buscoretype = (regdata & SBIDH_CC_MASK) >> SBIDH_CC_SHIFT;
5921
5922 DHD_INFO(("%s: ccrev=%d, pmurev=%d, buscore rev/type=%d/0x%x\n",
5923 __func__, ci->ccrev, ci->pmurev,
5924 ci->buscorerev, ci->buscoretype));
5925
5926 /* get chipcommon capabilites */
5927 ci->cccaps = bcmsdh_reg_read(sdh,
5928 CORE_CC_REG(ci->cccorebase, capabilities), 4);
5929
5930 return 0;
5931}
5932
5933static void
5934dhdsdio_chip_disablecore(bcmsdh_info_t *sdh, u32 corebase)
5935{
5936 u32 regdata;
5937
5938 regdata = bcmsdh_reg_read(sdh,
5939 CORE_SB(corebase, sbtmstatelow), 4);
5940 if (regdata & SBTML_RESET)
5941 return;
5942
5943 regdata = bcmsdh_reg_read(sdh,
5944 CORE_SB(corebase, sbtmstatelow), 4);
5945 if ((regdata & (SICF_CLOCK_EN << SBTML_SICF_SHIFT)) != 0) {
5946 /*
5947 * set target reject and spin until busy is clear
5948 * (preserve core-specific bits)
5949 */
5950 regdata = bcmsdh_reg_read(sdh,
5951 CORE_SB(corebase, sbtmstatelow), 4);
5952 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
5953 regdata | SBTML_REJ);
5954
5955 regdata = bcmsdh_reg_read(sdh,
5956 CORE_SB(corebase, sbtmstatelow), 4);
5957 udelay(1);
5958 SPINWAIT((bcmsdh_reg_read(sdh,
5959 CORE_SB(corebase, sbtmstatehigh), 4) &
5960 SBTMH_BUSY), 100000);
5961
5962 regdata = bcmsdh_reg_read(sdh,
5963 CORE_SB(corebase, sbtmstatehigh), 4);
5964 if (regdata & SBTMH_BUSY)
5965 DHD_ERROR(("%s: ARM core still busy\n", __func__));
5966
5967 regdata = bcmsdh_reg_read(sdh,
5968 CORE_SB(corebase, sbidlow), 4);
5969 if (regdata & SBIDL_INIT) {
5970 regdata = bcmsdh_reg_read(sdh,
5971 CORE_SB(corebase, sbimstate), 4) |
5972 SBIM_RJ;
5973 bcmsdh_reg_write(sdh,
5974 CORE_SB(corebase, sbimstate), 4,
5975 regdata);
5976 regdata = bcmsdh_reg_read(sdh,
5977 CORE_SB(corebase, sbimstate), 4);
5978 udelay(1);
5979 SPINWAIT((bcmsdh_reg_read(sdh,
5980 CORE_SB(corebase, sbimstate), 4) &
5981 SBIM_BY), 100000);
5982 }
5983
5984 /* set reset and reject while enabling the clocks */
5985 bcmsdh_reg_write(sdh,
5986 CORE_SB(corebase, sbtmstatelow), 4,
5987 (((SICF_FGC | SICF_CLOCK_EN) << SBTML_SICF_SHIFT) |
5988 SBTML_REJ | SBTML_RESET));
5989 regdata = bcmsdh_reg_read(sdh,
5990 CORE_SB(corebase, sbtmstatelow), 4);
5991 udelay(10);
5992
5993 /* clear the initiator reject bit */
5994 regdata = bcmsdh_reg_read(sdh,
5995 CORE_SB(corebase, sbidlow), 4);
5996 if (regdata & SBIDL_INIT) {
5997 regdata = bcmsdh_reg_read(sdh,
5998 CORE_SB(corebase, sbimstate), 4) &
5999 ~SBIM_RJ;
6000 bcmsdh_reg_write(sdh,
6001 CORE_SB(corebase, sbimstate), 4,
6002 regdata);
6003 }
6004 }
6005
6006 /* leave reset and reject asserted */
6007 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6008 (SBTML_REJ | SBTML_RESET));
6009 udelay(1);
6010}
6011
6012static int
6013dhdsdio_chip_attach(struct dhd_bus *bus, void *regs)
6014{
6015 struct chip_info *ci;
6016 int err;
6017 u8 clkval, clkset;
6018
6019 DHD_TRACE(("%s: Enter\n", __func__));
6020
6021 /* alloc chip_info_t */
6022 ci = kmalloc(sizeof(struct chip_info), GFP_ATOMIC);
6023 if (NULL == ci) {
6024 DHD_ERROR(("%s: malloc failed!\n", __func__));
6025 return -ENOMEM;
6026 }
6027
6028 memset((unsigned char *)ci, 0, sizeof(struct chip_info));
6029
6030 /* bus/core/clk setup for register access */
6031 /* Try forcing SDIO core to do ALPAvail request only */
6032 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
6033 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
6034 clkset, &err);
6035 if (err) {
6036 DHD_ERROR(("%s: error writing for HT off\n", __func__));
6037 goto fail;
6038 }
6039
6040 /* If register supported, wait for ALPAvail and then force ALP */
6041 /* This may take up to 15 milliseconds */
6042 clkval = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
6043 SBSDIO_FUNC1_CHIPCLKCSR, NULL);
6044 if ((clkval & ~SBSDIO_AVBITS) == clkset) {
6045 SPINWAIT(((clkval =
6046 bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
6047 SBSDIO_FUNC1_CHIPCLKCSR,
6048 NULL)),
6049 !SBSDIO_ALPAV(clkval)),
6050 PMU_MAX_TRANSITION_DLY);
6051 if (!SBSDIO_ALPAV(clkval)) {
6052 DHD_ERROR(("%s: timeout on ALPAV wait, clkval 0x%02x\n",
6053 __func__, clkval));
6054 err = -EBUSY;
6055 goto fail;
6056 }
6057 clkset = SBSDIO_FORCE_HW_CLKREQ_OFF |
6058 SBSDIO_FORCE_ALP;
6059 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1,
6060 SBSDIO_FUNC1_CHIPCLKCSR,
6061 clkset, &err);
6062 udelay(65);
6063 } else {
6064 DHD_ERROR(("%s: ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
6065 __func__, clkset, clkval));
6066 err = -EACCES;
6067 goto fail;
6068 }
6069
6070 /* Also, disable the extra SDIO pull-ups */
6071 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SDIOPULLUP, 0,
6072 NULL);
6073
6074 err = dhdsdio_chip_recognition(bus->sdh, ci, regs);
6075 if (err)
6076 goto fail;
6077
6078 /*
6079 * Make sure any on-chip ARM is off (in case strapping is wrong),
6080 * or downloaded code was already running.
6081 */
6082 dhdsdio_chip_disablecore(bus->sdh, ci->armcorebase);
6083
6084 bcmsdh_reg_write(bus->sdh,
6085 CORE_CC_REG(ci->cccorebase, gpiopullup), 4, 0);
6086 bcmsdh_reg_write(bus->sdh,
6087 CORE_CC_REG(ci->cccorebase, gpiopulldown), 4, 0);
6088
6089 /* Disable F2 to clear any intermediate frame state on the dongle */
6090 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN,
6091 SDIO_FUNC_ENABLE_1, NULL);
6092
6093 /* WAR: cmd52 backplane read so core HW will drop ALPReq */
6094 clkval = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1,
6095 0, NULL);
6096
6097 /* Done with backplane-dependent accesses, can drop clock... */
6098 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, 0,
6099 NULL);
6100
6101 bus->ci = ci;
6102 return 0;
6103fail:
6104 bus->ci = NULL;
6105 kfree(ci);
6106 return err;
6107}
Franky Lineb5dc512011-04-25 19:34:04 -07006108
6109static void
6110dhdsdio_chip_resetcore(bcmsdh_info_t *sdh, u32 corebase)
6111{
6112 u32 regdata;
6113
6114 /*
6115 * Must do the disable sequence first to work for
6116 * arbitrary current core state.
6117 */
6118 dhdsdio_chip_disablecore(sdh, corebase);
6119
6120 /*
6121 * Now do the initialization sequence.
6122 * set reset while enabling the clock and
6123 * forcing them on throughout the core
6124 */
6125 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6126 ((SICF_FGC | SICF_CLOCK_EN) << SBTML_SICF_SHIFT) |
6127 SBTML_RESET);
6128 udelay(1);
6129
6130 regdata = bcmsdh_reg_read(sdh, CORE_SB(corebase, sbtmstatehigh), 4);
6131 if (regdata & SBTMH_SERR)
6132 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatehigh), 4, 0);
6133
6134 regdata = bcmsdh_reg_read(sdh, CORE_SB(corebase, sbimstate), 4);
6135 if (regdata & (SBIM_IBE | SBIM_TO))
6136 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbimstate), 4,
6137 regdata & ~(SBIM_IBE | SBIM_TO));
6138
6139 /* clear reset and allow it to propagate throughout the core */
6140 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6141 (SICF_FGC << SBTML_SICF_SHIFT) |
6142 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
6143 udelay(1);
6144
6145 /* leave clock enabled */
6146 bcmsdh_reg_write(sdh, CORE_SB(corebase, sbtmstatelow), 4,
6147 (SICF_CLOCK_EN << SBTML_SICF_SHIFT));
6148 udelay(1);
6149}
Franky Lin5d0d7a92011-04-25 19:34:05 -07006150
6151/* SDIO Pad drive strength to select value mappings */
6152struct sdiod_drive_str {
6153 u8 strength; /* Pad Drive Strength in mA */
6154 u8 sel; /* Chip-specific select value */
6155};
6156
6157/* SDIO Drive Strength to sel value table for PMU Rev 1 */
6158static const struct sdiod_drive_str sdiod_drive_strength_tab1[] = {
6159 {
6160 4, 0x2}, {
6161 2, 0x3}, {
6162 1, 0x0}, {
6163 0, 0x0}
6164 };
6165
6166/* SDIO Drive Strength to sel value table for PMU Rev 2, 3 */
6167static const struct sdiod_drive_str sdiod_drive_strength_tab2[] = {
6168 {
6169 12, 0x7}, {
6170 10, 0x6}, {
6171 8, 0x5}, {
6172 6, 0x4}, {
6173 4, 0x2}, {
6174 2, 0x1}, {
6175 0, 0x0}
6176 };
6177
6178/* SDIO Drive Strength to sel value table for PMU Rev 8 (1.8V) */
6179static const struct sdiod_drive_str sdiod_drive_strength_tab3[] = {
6180 {
6181 32, 0x7}, {
6182 26, 0x6}, {
6183 22, 0x5}, {
6184 16, 0x4}, {
6185 12, 0x3}, {
6186 8, 0x2}, {
6187 4, 0x1}, {
6188 0, 0x0}
6189 };
6190
6191#define SDIOD_DRVSTR_KEY(chip, pmu) (((chip) << 16) | (pmu))
6192
6193static void
6194dhdsdio_sdiod_drive_strength_init(struct dhd_bus *bus, u32 drivestrength) {
6195 struct sdiod_drive_str *str_tab = NULL;
6196 u32 str_mask = 0;
6197 u32 str_shift = 0;
Franky Lin5d0d7a92011-04-25 19:34:05 -07006198 char chn[8];
Franky Lin5d0d7a92011-04-25 19:34:05 -07006199
6200 if (!(bus->ci->cccaps & CC_CAP_PMU))
6201 return;
6202
6203 switch (SDIOD_DRVSTR_KEY(bus->ci->chip, bus->ci->pmurev)) {
6204 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 1):
6205 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab1;
6206 str_mask = 0x30000000;
6207 str_shift = 28;
6208 break;
6209 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 2):
6210 case SDIOD_DRVSTR_KEY(BCM4325_CHIP_ID, 3):
6211 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab2;
6212 str_mask = 0x00003800;
6213 str_shift = 11;
6214 break;
6215 case SDIOD_DRVSTR_KEY(BCM4336_CHIP_ID, 8):
6216 str_tab = (struct sdiod_drive_str *)&sdiod_drive_strength_tab3;
6217 str_mask = 0x00003800;
6218 str_shift = 11;
6219 break;
6220 default:
6221 DHD_ERROR(("No SDIO Drive strength init"
6222 "done for chip %s rev %d pmurev %d\n",
6223 bcm_chipname(bus->ci->chip, chn, 8),
6224 bus->ci->chiprev, bus->ci->pmurev));
6225 break;
6226 }
6227
6228 if (str_tab != NULL) {
6229 u32 drivestrength_sel = 0;
6230 u32 cc_data_temp;
6231 int i;
6232
6233 for (i = 0; str_tab[i].strength != 0; i++) {
6234 if (drivestrength >= str_tab[i].strength) {
6235 drivestrength_sel = str_tab[i].sel;
6236 break;
6237 }
6238 }
6239
6240 bcmsdh_reg_write(bus->sdh,
6241 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr),
6242 4, 1);
6243 cc_data_temp = bcmsdh_reg_read(bus->sdh,
6244 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr), 4);
6245 cc_data_temp &= ~str_mask;
6246 drivestrength_sel <<= str_shift;
6247 cc_data_temp |= drivestrength_sel;
6248 bcmsdh_reg_write(bus->sdh,
6249 CORE_CC_REG(bus->ci->cccorebase, chipcontrol_addr),
6250 4, cc_data_temp);
6251
6252 DHD_INFO(("SDIO: %dmA drive strength selected, set to 0x%08x\n",
6253 drivestrength, cc_data_temp));
6254 }
6255}
Franky Lincee3cf42011-04-25 19:34:06 -07006256
6257static void
6258dhdsdio_chip_detach(struct dhd_bus *bus)
6259{
6260 DHD_TRACE(("%s: Enter\n", __func__));
6261
6262 kfree(bus->ci);
6263 bus->ci = NULL;
6264}