]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/brcm80211/brcmfmac/dhd_sdio.c
Staging: brcm80211: remove uintptr typedef usage
[mv-sheeva.git] / drivers / staging / brcm80211 / brcmfmac / dhd_sdio.c
1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <typedefs.h>
18 #include <osl.h>
19 #include <bcmsdh.h>
20
21 #ifdef BCMEMBEDIMAGE
22 #include BCMEMBEDIMAGE
23 #endif                          /* BCMEMBEDIMAGE */
24
25 #include <bcmdefs.h>
26 #include <bcmutils.h>
27 #include <bcmendian.h>
28 #include <bcmdevs.h>
29
30 #include <siutils.h>
31 #include <hndpmu.h>
32 #include <hndsoc.h>
33 #ifdef DHD_DEBUG
34 #include <hndrte_armtrap.h>
35 #include <hndrte_cons.h>
36 #endif                          /* DHD_DEBUG */
37 #include <sbchipc.h>
38 #include <sbhnddma.h>
39
40 #include <sdio.h>
41 #include <sbsdio.h>
42 #include <sbsdpcmdev.h>
43 #include <bcmsdpcm.h>
44
45 #include <proto/ethernet.h>
46 #include <proto/802.1d.h>
47 #include <proto/802.11.h>
48
49 #include <dngl_stats.h>
50 #include <dhd.h>
51 #include <dhd_bus.h>
52 #include <dhd_proto.h>
53 #include <dhd_dbg.h>
54 #include <dhdioctl.h>
55 #include <sdiovar.h>
56 #include <siutils_priv.h>
57
58 #ifndef DHDSDIO_MEM_DUMP_FNAME
59 #define DHDSDIO_MEM_DUMP_FNAME         "mem_dump"
60 #endif
61
62 #define QLEN            256     /* bulk rx and tx queue lengths */
63 #define FCHI            (QLEN - 10)
64 #define FCLOW           (FCHI / 2)
65 #define PRIOMASK        7
66
67 #define TXRETRIES       2       /* # of retries for tx frames */
68
69 #if defined(CONFIG_MACH_SANDGATE2G)
70 #define DHD_RXBOUND     250     /* Default for max rx frames in
71                                  one scheduling */
72 #else
73 #define DHD_RXBOUND     50      /* Default for max rx frames in
74                                  one scheduling */
75 #endif                          /* defined(CONFIG_MACH_SANDGATE2G) */
76
77 #define DHD_TXBOUND     20      /* Default for max tx frames in
78                                  one scheduling */
79
80 #define DHD_TXMINMAX    1       /* Max tx frames if rx still pending */
81
82 #define MEMBLOCK        2048    /* Block size used for downloading
83                                  of dongle image */
84 #define MAX_DATA_BUF    (32 * 1024)     /* Must be large enough to hold
85                                  biggest possible glom */
86
87 /* Packet alignment for most efficient SDIO (can change based on platform) */
88 #ifndef DHD_SDALIGN
89 #define DHD_SDALIGN     32
90 #endif
91 #if !ISPOWEROF2(DHD_SDALIGN)
92 #error DHD_SDALIGN is not a power of 2!
93 #endif
94
95 #ifndef DHD_FIRSTREAD
96 #define DHD_FIRSTREAD   32
97 #endif
98 #if !ISPOWEROF2(DHD_FIRSTREAD)
99 #error DHD_FIRSTREAD is not a power of 2!
100 #endif
101
102 /* Total length of frame header for dongle protocol */
103 #define SDPCM_HDRLEN    (SDPCM_FRAMETAG_LEN + SDPCM_SWHEADER_LEN)
104 #ifdef SDTEST
105 #define SDPCM_RESERVE   (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + DHD_SDALIGN)
106 #else
107 #define SDPCM_RESERVE   (SDPCM_HDRLEN + DHD_SDALIGN)
108 #endif
109
110 /* Space for header read, limit for data packets */
111 #ifndef MAX_HDR_READ
112 #define MAX_HDR_READ    32
113 #endif
114 #if !ISPOWEROF2(MAX_HDR_READ)
115 #error MAX_HDR_READ is not a power of 2!
116 #endif
117
118 #define MAX_RX_DATASZ   2048
119
120 /* Maximum milliseconds to wait for F2 to come up */
121 #define DHD_WAIT_F2RDY  3000
122
123 /* Bump up limit on waiting for HT to account for first startup;
124  * if the image is doing a CRC calculation before programming the PMU
125  * for HT availability, it could take a couple hundred ms more, so
126  * max out at a 1 second (1000000us).
127  */
128 #if (PMU_MAX_TRANSITION_DLY <= 1000000)
129 #undef PMU_MAX_TRANSITION_DLY
130 #define PMU_MAX_TRANSITION_DLY 1000000
131 #endif
132
133 /* Value for ChipClockCSR during initial setup */
134 #define DHD_INIT_CLKCTL1        (SBSDIO_FORCE_HW_CLKREQ_OFF |   \
135                                         SBSDIO_ALP_AVAIL_REQ)
136 #define DHD_INIT_CLKCTL2        (SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP)
137
138 /* Flags for SDH calls */
139 #define F2SYNC  (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
140
141 /* Packet free applicable unconditionally for sdio and sdspi.  Conditional if
142  * bufpool was present for gspi bus.
143  */
144 #define PKTFREE2()              if ((bus->bus != SPI_BUS) || bus->usebufpool) \
145                                         PKTFREE(bus->dhd->osh, pkt, false);
146 DHD_SPINWAIT_SLEEP_INIT(sdioh_spinwait_sleep);
147 extern 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 */
152 typedef 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 */
157         u8 *buf;                /* Log buffer (host copy) */
158         uint last;              /* Last buffer read index */
159 } dhd_console_t;
160 #endif                          /* DHD_DEBUG */
161
162 /* Private data for SDIO bus interaction */
163 typedef struct dhd_bus {
164         dhd_pub_t *dhd;
165
166         bcmsdh_info_t *sdh;     /* Handle for BCMSDH calls */
167         si_t *sih;              /* Handle for SI calls */
168         char *vars;             /* Variables (from CIS and/or other) */
169         uint varsz;             /* Size of variables buffer */
170         u32 sbaddr;             /* Current SB window pointer (-1, invalid) */
171
172         sdpcmd_regs_t *regs;    /* Registers for SDIO core */
173         uint sdpcmrev;          /* SDIO core revision */
174         uint armrev;            /* CPU core revision */
175         uint ramrev;            /* SOCRAM core revision */
176         u32 ramsize;            /* Size of RAM in SOCRAM (bytes) */
177         u32 orig_ramsize;       /* Size of RAM in SOCRAM (bytes) */
178
179         u32 bus;                /* gSPI or SDIO bus */
180         u32 hostintmask;        /* Copy of Host Interrupt Mask */
181         u32 intstatus;  /* Intstatus bits (events) pending */
182         bool dpc_sched;         /* Indicates DPC schedule (intrpt rcvd) */
183         bool fcstate;           /* State of dongle flow-control */
184
185         u16 cl_devid;   /* cached devid for dhdsdio_probe_attach() */
186         char *fw_path;          /* module_param: path to firmware image */
187         char *nv_path;          /* module_param: path to nvram vars file */
188         const char *nvram_params;       /* user specified nvram params. */
189
190         uint blocksize;         /* Block size of SDIO transfers */
191         uint roundup;           /* Max roundup limit */
192
193         struct pktq txq;        /* Queue length used for flow-control */
194         u8 flowcontrol; /* per prio flow control bitmask */
195         u8 tx_seq;              /* Transmit sequence number (next) */
196         u8 tx_max;              /* Maximum transmit sequence allowed */
197
198         u8 hdrbuf[MAX_HDR_READ + DHD_SDALIGN];
199         u8 *rxhdr;              /* Header of current rx frame (in hdrbuf) */
200         u16 nextlen;            /* Next Read Len from last header */
201         u8 rx_seq;              /* Receive sequence number (expected) */
202         bool rxskip;            /* Skip receive (awaiting NAK ACK) */
203
204         void *glomd;            /* Packet containing glomming descriptor */
205         void *glom;             /* Packet chain for glommed superframe */
206         uint glomerr;           /* Glom packet read errors */
207
208         u8 *rxbuf;              /* Buffer for receiving control packets */
209         uint rxblen;            /* Allocated length of rxbuf */
210         u8 *rxctl;              /* Aligned pointer into rxbuf */
211         u8 *databuf;            /* Buffer for receiving big glom packet */
212         u8 *dataptr;            /* Aligned pointer into databuf */
213         uint rxlen;             /* Length of valid data in buffer */
214
215         u8 sdpcm_ver;   /* Bus protocol reported by dongle */
216
217         bool intr;              /* Use interrupts */
218         bool poll;              /* Use polling */
219         bool ipend;             /* Device interrupt is pending */
220         bool intdis;            /* Interrupts disabled by isr */
221         uint intrcount;         /* Count of device interrupt callbacks */
222         uint lastintrs;         /* Count as of last watchdog timer */
223         uint spurious;          /* Count of spurious interrupts */
224         uint pollrate;          /* Ticks between device polls */
225         uint polltick;          /* Tick counter */
226         uint pollcnt;           /* Count of active polls */
227
228 #ifdef DHD_DEBUG
229         dhd_console_t console;  /* Console output polling support */
230         uint console_addr;      /* Console address from shared struct */
231 #endif                          /* DHD_DEBUG */
232
233         uint regfails;          /* Count of R_REG/W_REG failures */
234
235         uint clkstate;          /* State of sd and backplane clock(s) */
236         bool activity;          /* Activity flag for clock down */
237         s32 idletime;           /* Control for activity timeout */
238         s32 idlecount;  /* Activity timeout counter */
239         s32 idleclock;  /* How to set bus driver when idle */
240         s32 sd_divisor; /* Speed control to bus driver */
241         s32 sd_mode;            /* Mode control to bus driver */
242         s32 sd_rxchain; /* If bcmsdh api accepts PKT chains */
243         bool use_rxchain;       /* If dhd should use PKT chains */
244         bool sleeping;          /* Is SDIO bus sleeping? */
245         bool rxflow_mode;       /* Rx flow control mode */
246         bool rxflow;            /* Is rx flow control on */
247         uint prev_rxlim_hit;    /* Is prev rx limit exceeded
248                                          (per dpc schedule) */
249         bool alp_only;          /* Don't use HT clock (ALP only) */
250 /* Field to decide if rx of control frames happen in rxbuf or lb-pool */
251         bool usebufpool;
252
253 #ifdef SDTEST
254         /* external loopback */
255         bool ext_loop;
256         u8 loopid;
257
258         /* pktgen configuration */
259         uint pktgen_freq;       /* Ticks between bursts */
260         uint pktgen_count;      /* Packets to send each burst */
261         uint pktgen_print;      /* Bursts between count displays */
262         uint pktgen_total;      /* Stop after this many */
263         uint pktgen_minlen;     /* Minimum packet data len */
264         uint pktgen_maxlen;     /* Maximum packet data len */
265         uint pktgen_mode;       /* Configured mode: tx, rx, or echo */
266         uint pktgen_stop;       /* Number of tx failures causing stop */
267
268         /* active pktgen fields */
269         uint pktgen_tick;       /* Tick counter for bursts */
270         uint pktgen_ptick;      /* Burst counter for printing */
271         uint pktgen_sent;       /* Number of test packets generated */
272         uint pktgen_rcvd;       /* Number of test packets received */
273         uint pktgen_fail;       /* Number of failed send attempts */
274         u16 pktgen_len; /* Length of next packet to send */
275 #endif                          /* SDTEST */
276
277         /* Some additional counters */
278         uint tx_sderrs;         /* Count of tx attempts with sd errors */
279         uint fcqueued;          /* Tx packets that got queued */
280         uint rxrtx;             /* Count of rtx requests (NAK to dongle) */
281         uint rx_toolong;        /* Receive frames too long to receive */
282         uint rxc_errors;        /* SDIO errors when reading control frames */
283         uint rx_hdrfail;        /* SDIO errors on header reads */
284         uint rx_badhdr;         /* Bad received headers (roosync?) */
285         uint rx_badseq;         /* Mismatched rx sequence number */
286         uint fc_rcvd;           /* Number of flow-control events received */
287         uint fc_xoff;           /* Number which turned on flow-control */
288         uint fc_xon;            /* Number which turned off flow-control */
289         uint rxglomfail;        /* Failed deglom attempts */
290         uint rxglomframes;      /* Number of glom frames (superframes) */
291         uint rxglompkts;        /* Number of packets from glom frames */
292         uint f2rxhdrs;          /* Number of header reads */
293         uint f2rxdata;          /* Number of frame data reads */
294         uint f2txdata;          /* Number of f2 frame writes */
295         uint f1regdata;         /* Number of f1 register accesses */
296
297         u8 *ctrl_frame_buf;
298         u32 ctrl_frame_len;
299         bool ctrl_frame_stat;
300 } dhd_bus_t;
301
302 /* clkstate */
303 #define CLK_NONE        0
304 #define CLK_SDONLY      1
305 #define CLK_PENDING     2       /* Not used yet */
306 #define CLK_AVAIL       3
307
308 #define DHD_NOPMU(dhd)  (false)
309
310 #ifdef DHD_DEBUG
311 static int qcount[NUMPRIO];
312 static int tx_packets[NUMPRIO];
313 #endif                          /* DHD_DEBUG */
314
315 /* Deferred transmit */
316 const uint dhd_deferred_tx = 1;
317
318 extern uint dhd_watchdog_ms;
319 extern void dhd_os_wd_timer(void *bus, uint wdtick);
320
321 /* Tx/Rx bounds */
322 uint dhd_txbound;
323 uint dhd_rxbound;
324 uint dhd_txminmax;
325
326 /* override the RAM size if possible */
327 #define DONGLE_MIN_MEMSIZE (128 * 1024)
328 int dhd_dongle_memsize;
329
330 static bool dhd_doflow;
331 static bool dhd_alignctl;
332
333 static bool sd1idle;
334
335 static bool retrydata;
336 #define RETRYCHAN(chan) (((chan) == SDPCM_EVENT_CHANNEL) || retrydata)
337
338 static const uint watermark = 8;
339 static const uint firstread = DHD_FIRSTREAD;
340
341 #define HDATLEN (firstread - (SDPCM_HDRLEN))
342
343 /* Retry count for register access failures */
344 static const uint retry_limit = 2;
345
346 /* Force even SD lengths (some host controllers mess up on odd bytes) */
347 static bool forcealign;
348
349 #define ALIGNMENT  4
350
351 #if defined(OOB_INTR_ONLY) && defined(HW_OOB)
352 extern void bcmsdh_enable_hw_oob_intr(void *sdh, bool enable);
353 #endif
354
355 #if defined(OOB_INTR_ONLY) && defined(SDIO_ISR_THREAD)
356 #error OOB_INTR_ONLY is NOT working with SDIO_ISR_THREAD
357 #endif  /* defined(OOB_INTR_ONLY) && defined(SDIO_ISR_THREAD) */
358 #define PKTALIGN(osh, p, len, align)                                    \
359         do {                                                            \
360                 uint datalign;                                          \
361                 datalign = (unsigned long)PKTDATA((p));                 \
362                 datalign = roundup(datalign, (align)) - datalign;       \
363                 ASSERT(datalign < (align));                             \
364                 ASSERT(PKTLEN((p)) >= ((len) + datalign));      \
365                 if (datalign)                                           \
366                         PKTPULL((p), datalign);                 \
367                 PKTSETLEN((p), (len));                          \
368         } while (0)
369
370 /* Limit on rounding up frames */
371 static const uint max_roundup = 512;
372
373 /* Try doing readahead */
374 static bool dhd_readahead;
375
376 /* To check if there's window offered */
377 #define DATAOK(bus) \
378         (((u8)(bus->tx_max - bus->tx_seq) != 0) && \
379         (((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0))
380
381 /* Macros to get register read/write status */
382 /* NOTE: these assume a local dhdsdio_bus_t *bus! */
383 #define R_SDREG(regvar, regaddr, retryvar) \
384 do { \
385         retryvar = 0; \
386         do { \
387                 regvar = R_REG(bus->dhd->osh, regaddr); \
388         } while (bcmsdh_regfail(bus->sdh) && (++retryvar <= retry_limit)); \
389         if (retryvar) { \
390                 bus->regfails += (retryvar-1); \
391                 if (retryvar > retry_limit) { \
392                         DHD_ERROR(("%s: FAILED" #regvar "READ, LINE %d\n", \
393                         __func__, __LINE__)); \
394                         regvar = 0; \
395                 } \
396         } \
397 } while (0)
398
399 #define W_SDREG(regval, regaddr, retryvar) \
400 do { \
401         retryvar = 0; \
402         do { \
403                 W_REG(bus->dhd->osh, regaddr, regval); \
404         } while (bcmsdh_regfail(bus->sdh) && (++retryvar <= retry_limit)); \
405         if (retryvar) { \
406                 bus->regfails += (retryvar-1); \
407                 if (retryvar > retry_limit) \
408                         DHD_ERROR(("%s: FAILED REGISTER WRITE, LINE %d\n", \
409                         __func__, __LINE__)); \
410         } \
411 } while (0)
412
413 #define DHD_BUS                 SDIO_BUS
414
415 #define PKT_AVAILABLE()         (intstatus & I_HMB_FRAME_IND)
416
417 #define HOSTINTMASK             (I_HMB_SW_MASK | I_CHIPACTIVE)
418
419 #define GSPI_PR55150_BAILOUT
420
421 #ifdef SDTEST
422 static void dhdsdio_testrcv(dhd_bus_t *bus, void *pkt, uint seq);
423 static void dhdsdio_sdtest_set(dhd_bus_t *bus, bool start);
424 #endif
425
426 #ifdef DHD_DEBUG
427 static int dhdsdio_checkdied(dhd_bus_t *bus, u8 *data, uint size);
428 static int dhdsdio_mem_dump(dhd_bus_t *bus);
429 #endif                          /* DHD_DEBUG  */
430 static int dhdsdio_download_state(dhd_bus_t *bus, bool enter);
431
432 static void dhdsdio_release(dhd_bus_t *bus, osl_t *osh);
433 static void dhdsdio_release_malloc(dhd_bus_t *bus, osl_t *osh);
434 static void dhdsdio_disconnect(void *ptr);
435 static bool dhdsdio_chipmatch(u16 chipid);
436 static bool dhdsdio_probe_attach(dhd_bus_t *bus, osl_t *osh, void *sdh,
437                                  void *regsva, u16 devid);
438 static bool dhdsdio_probe_malloc(dhd_bus_t *bus, osl_t *osh, void *sdh);
439 static bool dhdsdio_probe_init(dhd_bus_t *bus, osl_t *osh, void *sdh);
440 static void dhdsdio_release_dongle(dhd_bus_t *bus, osl_t * osh);
441
442 static uint process_nvram_vars(char *varbuf, uint len);
443
444 static void dhd_dongle_setmemsize(struct dhd_bus *bus, int mem_size);
445 static int dhd_bcmsdh_recv_buf(dhd_bus_t *bus, u32 addr, uint fn,
446                                uint flags, u8 *buf, uint nbytes, void *pkt,
447                                bcmsdh_cmplt_fn_t complete, void *handle);
448 static int dhd_bcmsdh_send_buf(dhd_bus_t *bus, u32 addr, uint fn,
449                                uint flags, u8 *buf, uint nbytes, void *pkt,
450                                bcmsdh_cmplt_fn_t complete, void *handle);
451
452 static bool dhdsdio_download_firmware(struct dhd_bus *bus, osl_t *osh,
453                                       void *sdh);
454 static int _dhdsdio_download_firmware(struct dhd_bus *bus);
455
456 static int dhdsdio_download_code_file(struct dhd_bus *bus, char *image_path);
457 static int dhdsdio_download_nvram(struct dhd_bus *bus);
458 #ifdef BCMEMBEDIMAGE
459 static int dhdsdio_download_code_array(struct dhd_bus *bus);
460 #endif
461
462 static void dhd_dongle_setmemsize(struct dhd_bus *bus, int mem_size)
463 {
464         s32 min_size = DONGLE_MIN_MEMSIZE;
465         /* Restrict the memsize to user specified limit */
466         DHD_ERROR(("user: Restrict the dongle ram size to %d, min %d\n",
467                 dhd_dongle_memsize, min_size));
468         if ((dhd_dongle_memsize > min_size) &&
469             (dhd_dongle_memsize < (s32) bus->orig_ramsize))
470                 bus->ramsize = dhd_dongle_memsize;
471 }
472
473 static int dhdsdio_set_siaddr_window(dhd_bus_t *bus, u32 address)
474 {
475         int err = 0;
476         bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW,
477                          (address >> 8) & SBSDIO_SBADDRLOW_MASK, &err);
478         if (!err)
479                 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRMID,
480                                  (address >> 16) & SBSDIO_SBADDRMID_MASK, &err);
481         if (!err)
482                 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRHIGH,
483                                  (address >> 24) & SBSDIO_SBADDRHIGH_MASK,
484                                  &err);
485         return err;
486 }
487
488 /* Turn backplane clock on or off */
489 static int dhdsdio_htclk(dhd_bus_t *bus, bool on, bool pendok)
490 {
491         int err;
492         u8 clkctl, clkreq, devctl;
493         bcmsdh_info_t *sdh;
494
495         DHD_TRACE(("%s: Enter\n", __func__));
496
497 #if defined(OOB_INTR_ONLY)
498         pendok = false;
499 #endif
500         clkctl = 0;
501         sdh = bus->sdh;
502
503         if (on) {
504                 /* Request HT Avail */
505                 clkreq =
506                     bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
507
508                 if ((bus->sih->chip == BCM4329_CHIP_ID)
509                     && (bus->sih->chiprev == 0))
510                         clkreq |= SBSDIO_FORCE_ALP;
511
512                 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
513                                  clkreq, &err);
514                 if (err) {
515                         DHD_ERROR(("%s: HT Avail request error: %d\n",
516                                    __func__, err));
517                         return BCME_ERROR;
518                 }
519
520                 if (pendok && ((bus->sih->buscoretype == PCMCIA_CORE_ID)
521                                && (bus->sih->buscorerev == 9))) {
522                         u32 dummy, retries;
523                         R_SDREG(dummy, &bus->regs->clockctlstatus, retries);
524                 }
525
526                 /* Check current status */
527                 clkctl =
528                     bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
529                                     &err);
530                 if (err) {
531                         DHD_ERROR(("%s: HT Avail read error: %d\n",
532                                    __func__, err));
533                         return BCME_ERROR;
534                 }
535
536                 /* Go to pending and await interrupt if appropriate */
537                 if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
538                         /* Allow only clock-available interrupt */
539                         devctl =
540                             bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
541                                             &err);
542                         if (err) {
543                                 DHD_ERROR(("%s: Devctl error setting CA: %d\n",
544                                         __func__, err));
545                                 return BCME_ERROR;
546                         }
547
548                         devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
549                         bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
550                                          devctl, &err);
551                         DHD_INFO(("CLKCTL: set PENDING\n"));
552                         bus->clkstate = CLK_PENDING;
553
554                         return BCME_OK;
555                 } else if (bus->clkstate == CLK_PENDING) {
556                         /* Cancel CA-only interrupt filter */
557                         devctl =
558                             bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
559                                             &err);
560                         devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
561                         bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
562                                          devctl, &err);
563                 }
564
565                 /* Otherwise, wait here (polling) for HT Avail */
566                 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
567                         SPINWAIT_SLEEP(sdioh_spinwait_sleep,
568                                        ((clkctl =
569                                          bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
570                                                  SBSDIO_FUNC1_CHIPCLKCSR,
571                                                          &err)),
572                                         !SBSDIO_CLKAV(clkctl, bus->alp_only)),
573                                        PMU_MAX_TRANSITION_DLY);
574                 }
575                 if (err) {
576                         DHD_ERROR(("%s: HT Avail request error: %d\n",
577                                    __func__, err));
578                         return BCME_ERROR;
579                 }
580                 if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
581                         DHD_ERROR(("%s: HT Avail timeout (%d): clkctl 0x%02x\n",
582                                    __func__, PMU_MAX_TRANSITION_DLY, clkctl));
583                         return BCME_ERROR;
584                 }
585
586                 /* Mark clock available */
587                 bus->clkstate = CLK_AVAIL;
588                 DHD_INFO(("CLKCTL: turned ON\n"));
589
590 #if defined(DHD_DEBUG)
591                 if (bus->alp_only == true) {
592 #if !defined(BCMLXSDMMC)
593                         if (!SBSDIO_ALPONLY(clkctl)) {
594                                 DHD_ERROR(("%s: HT Clock, when ALP Only\n",
595                                            __func__));
596                         }
597 #endif                          /* !defined(BCMLXSDMMC) */
598                 } else {
599                         if (SBSDIO_ALPONLY(clkctl)) {
600                                 DHD_ERROR(("%s: HT Clock should be on.\n",
601                                            __func__));
602                         }
603                 }
604 #endif                          /* defined (DHD_DEBUG) */
605
606                 bus->activity = true;
607         } else {
608                 clkreq = 0;
609
610                 if (bus->clkstate == CLK_PENDING) {
611                         /* Cancel CA-only interrupt filter */
612                         devctl =
613                             bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
614                                             &err);
615                         devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
616                         bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
617                                          devctl, &err);
618                 }
619
620                 bus->clkstate = CLK_SDONLY;
621                 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
622                                  clkreq, &err);
623                 DHD_INFO(("CLKCTL: turned OFF\n"));
624                 if (err) {
625                         DHD_ERROR(("%s: Failed access turning clock off: %d\n",
626                                    __func__, err));
627                         return BCME_ERROR;
628                 }
629         }
630         return BCME_OK;
631 }
632
633 /* Change idle/active SD state */
634 static int dhdsdio_sdclk(dhd_bus_t *bus, bool on)
635 {
636         int err;
637         s32 iovalue;
638
639         DHD_TRACE(("%s: Enter\n", __func__));
640
641         if (on) {
642                 if (bus->idleclock == DHD_IDLE_STOP) {
643                         /* Turn on clock and restore mode */
644                         iovalue = 1;
645                         err = bcmsdh_iovar_op(bus->sdh, "sd_clock", NULL, 0,
646                                               &iovalue, sizeof(iovalue), true);
647                         if (err) {
648                                 DHD_ERROR(("%s: error enabling sd_clock: %d\n",
649                                            __func__, err));
650                                 return BCME_ERROR;
651                         }
652
653                         iovalue = bus->sd_mode;
654                         err = bcmsdh_iovar_op(bus->sdh, "sd_mode", NULL, 0,
655                                               &iovalue, sizeof(iovalue), true);
656                         if (err) {
657                                 DHD_ERROR(("%s: error changing sd_mode: %d\n",
658                                            __func__, err));
659                                 return BCME_ERROR;
660                         }
661                 } else if (bus->idleclock != DHD_IDLE_ACTIVE) {
662                         /* Restore clock speed */
663                         iovalue = bus->sd_divisor;
664                         err = bcmsdh_iovar_op(bus->sdh, "sd_divisor", NULL, 0,
665                                               &iovalue, sizeof(iovalue), true);
666                         if (err) {
667                                 DHD_ERROR(("%s: error restoring sd_divisor: %d\n",
668                                         __func__, err));
669                                 return BCME_ERROR;
670                         }
671                 }
672                 bus->clkstate = CLK_SDONLY;
673         } else {
674                 /* Stop or slow the SD clock itself */
675                 if ((bus->sd_divisor == -1) || (bus->sd_mode == -1)) {
676                         DHD_TRACE(("%s: can't idle clock, divisor %d mode %d\n",
677                                    __func__, bus->sd_divisor, bus->sd_mode));
678                         return BCME_ERROR;
679                 }
680                 if (bus->idleclock == DHD_IDLE_STOP) {
681                         if (sd1idle) {
682                                 /* Change to SD1 mode and turn off clock */
683                                 iovalue = 1;
684                                 err =
685                                     bcmsdh_iovar_op(bus->sdh, "sd_mode", NULL,
686                                                     0, &iovalue,
687                                                     sizeof(iovalue), true);
688                                 if (err) {
689                                         DHD_ERROR(("%s: error changing sd_clock: %d\n",
690                                                 __func__, err));
691                                         return BCME_ERROR;
692                                 }
693                         }
694
695                         iovalue = 0;
696                         err = bcmsdh_iovar_op(bus->sdh, "sd_clock", NULL, 0,
697                                               &iovalue, sizeof(iovalue), true);
698                         if (err) {
699                                 DHD_ERROR(("%s: error disabling sd_clock: %d\n",
700                                            __func__, err));
701                                 return BCME_ERROR;
702                         }
703                 } else if (bus->idleclock != DHD_IDLE_ACTIVE) {
704                         /* Set divisor to idle value */
705                         iovalue = bus->idleclock;
706                         err = bcmsdh_iovar_op(bus->sdh, "sd_divisor", NULL, 0,
707                                               &iovalue, sizeof(iovalue), true);
708                         if (err) {
709                                 DHD_ERROR(("%s: error changing sd_divisor: %d\n",
710                                         __func__, err));
711                                 return BCME_ERROR;
712                         }
713                 }
714                 bus->clkstate = CLK_NONE;
715         }
716
717         return BCME_OK;
718 }
719
720 /* Transition SD and backplane clock readiness */
721 static int dhdsdio_clkctl(dhd_bus_t *bus, uint target, bool pendok)
722 {
723 #ifdef DHD_DEBUG
724         uint oldstate = bus->clkstate;
725 #endif                          /* DHD_DEBUG */
726
727         DHD_TRACE(("%s: Enter\n", __func__));
728
729         /* Early exit if we're already there */
730         if (bus->clkstate == target) {
731                 if (target == CLK_AVAIL) {
732                         dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
733                         bus->activity = true;
734                 }
735                 return BCME_OK;
736         }
737
738         switch (target) {
739         case CLK_AVAIL:
740                 /* Make sure SD clock is available */
741                 if (bus->clkstate == CLK_NONE)
742                         dhdsdio_sdclk(bus, true);
743                 /* Now request HT Avail on the backplane */
744                 dhdsdio_htclk(bus, true, pendok);
745                 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
746                 bus->activity = true;
747                 break;
748
749         case CLK_SDONLY:
750                 /* Remove HT request, or bring up SD clock */
751                 if (bus->clkstate == CLK_NONE)
752                         dhdsdio_sdclk(bus, true);
753                 else if (bus->clkstate == CLK_AVAIL)
754                         dhdsdio_htclk(bus, false, false);
755                 else
756                         DHD_ERROR(("dhdsdio_clkctl: request for %d -> %d\n",
757                                    bus->clkstate, target));
758                 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
759                 break;
760
761         case CLK_NONE:
762                 /* Make sure to remove HT request */
763                 if (bus->clkstate == CLK_AVAIL)
764                         dhdsdio_htclk(bus, false, false);
765                 /* Now remove the SD clock */
766                 dhdsdio_sdclk(bus, false);
767                 dhd_os_wd_timer(bus->dhd, 0);
768                 break;
769         }
770 #ifdef DHD_DEBUG
771         DHD_INFO(("dhdsdio_clkctl: %d -> %d\n", oldstate, bus->clkstate));
772 #endif                          /* DHD_DEBUG */
773
774         return BCME_OK;
775 }
776
777 int dhdsdio_bussleep(dhd_bus_t *bus, bool sleep)
778 {
779         bcmsdh_info_t *sdh = bus->sdh;
780         sdpcmd_regs_t *regs = bus->regs;
781         uint retries = 0;
782
783         DHD_INFO(("dhdsdio_bussleep: request %s (currently %s)\n",
784                   (sleep ? "SLEEP" : "WAKE"),
785                   (bus->sleeping ? "SLEEP" : "WAKE")));
786
787         /* Done if we're already in the requested state */
788         if (sleep == bus->sleeping)
789                 return BCME_OK;
790
791         /* Going to sleep: set the alarm and turn off the lights... */
792         if (sleep) {
793                 /* Don't sleep if something is pending */
794                 if (bus->dpc_sched || bus->rxskip || pktq_len(&bus->txq))
795                         return BCME_BUSY;
796
797                 /* Disable SDIO interrupts (no longer interested) */
798                 bcmsdh_intr_disable(bus->sdh);
799
800                 /* Make sure the controller has the bus up */
801                 dhdsdio_clkctl(bus, CLK_AVAIL, false);
802
803                 /* Tell device to start using OOB wakeup */
804                 W_SDREG(SMB_USE_OOB, &regs->tosbmailbox, retries);
805                 if (retries > retry_limit)
806                         DHD_ERROR(("CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"));
807
808                 /* Turn off our contribution to the HT clock request */
809                 dhdsdio_clkctl(bus, CLK_SDONLY, false);
810
811                 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
812                                  SBSDIO_FORCE_HW_CLKREQ_OFF, NULL);
813
814                 /* Isolate the bus */
815                 if (bus->sih->chip != BCM4329_CHIP_ID
816                     && bus->sih->chip != BCM4319_CHIP_ID) {
817                         bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
818                                          SBSDIO_DEVCTL_PADS_ISO, NULL);
819                 }
820
821                 /* Change state */
822                 bus->sleeping = true;
823
824         } else {
825                 /* Waking up: bus power up is ok, set local state */
826
827                 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
828                                  0, NULL);
829
830                 /* Force pad isolation off if possible
831                          (in case power never toggled) */
832                 if ((bus->sih->buscoretype == PCMCIA_CORE_ID)
833                     && (bus->sih->buscorerev >= 10))
834                         bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, 0,
835                                          NULL);
836
837                 /* Make sure the controller has the bus up */
838                 dhdsdio_clkctl(bus, CLK_AVAIL, false);
839
840                 /* Send misc interrupt to indicate OOB not needed */
841                 W_SDREG(0, &regs->tosbmailboxdata, retries);
842                 if (retries <= retry_limit)
843                         W_SDREG(SMB_DEV_INT, &regs->tosbmailbox, retries);
844
845                 if (retries > retry_limit)
846                         DHD_ERROR(("CANNOT SIGNAL CHIP TO CLEAR OOB!!\n"));
847
848                 /* Make sure we have SD bus access */
849                 dhdsdio_clkctl(bus, CLK_SDONLY, false);
850
851                 /* Change state */
852                 bus->sleeping = false;
853
854                 /* Enable interrupts again */
855                 if (bus->intr && (bus->dhd->busstate == DHD_BUS_DATA)) {
856                         bus->intdis = false;
857                         bcmsdh_intr_enable(bus->sdh);
858                 }
859         }
860
861         return BCME_OK;
862 }
863
864 #if defined(OOB_INTR_ONLY)
865 void dhd_enable_oob_intr(struct dhd_bus *bus, bool enable)
866 {
867 #if defined(HW_OOB)
868         bcmsdh_enable_hw_oob_intr(bus->sdh, enable);
869 #else
870         sdpcmd_regs_t *regs = bus->regs;
871         uint retries = 0;
872
873         dhdsdio_clkctl(bus, CLK_AVAIL, false);
874         if (enable == true) {
875
876                 /* Tell device to start using OOB wakeup */
877                 W_SDREG(SMB_USE_OOB, &regs->tosbmailbox, retries);
878                 if (retries > retry_limit)
879                         DHD_ERROR(("CANNOT SIGNAL CHIP, WILL NOT WAKE UP!!\n"));
880
881         } else {
882                 /* Send misc interrupt to indicate OOB not needed */
883                 W_SDREG(0, &regs->tosbmailboxdata, retries);
884                 if (retries <= retry_limit)
885                         W_SDREG(SMB_DEV_INT, &regs->tosbmailbox, retries);
886         }
887
888         /* Turn off our contribution to the HT clock request */
889         dhdsdio_clkctl(bus, CLK_SDONLY, false);
890 #endif                          /* !defined(HW_OOB) */
891 }
892 #endif                          /* defined(OOB_INTR_ONLY) */
893
894 #define BUS_WAKE(bus) \
895         do { \
896                 if ((bus)->sleeping) \
897                         dhdsdio_bussleep((bus), false); \
898         } while (0);
899
900 /* Writes a HW/SW header into the packet and sends it. */
901 /* Assumes: (a) header space already there, (b) caller holds lock */
902 static int dhdsdio_txpkt(dhd_bus_t *bus, void *pkt, uint chan, bool free_pkt)
903 {
904         int ret;
905         osl_t *osh;
906         u8 *frame;
907         u16 len, pad = 0;
908         u32 swheader;
909         uint retries = 0;
910         bcmsdh_info_t *sdh;
911         void *new;
912         int i;
913
914         DHD_TRACE(("%s: Enter\n", __func__));
915
916         sdh = bus->sdh;
917         osh = bus->dhd->osh;
918
919         if (bus->dhd->dongle_reset) {
920                 ret = BCME_NOTREADY;
921                 goto done;
922         }
923
924         frame = (u8 *) PKTDATA(pkt);
925
926         /* Add alignment padding, allocate new packet if needed */
927         pad = ((unsigned long)frame % DHD_SDALIGN);
928         if (pad) {
929                 if (PKTHEADROOM(pkt) < pad) {
930                         DHD_INFO(("%s: insufficient headroom %d for %d pad\n",
931                                   __func__, (int)PKTHEADROOM(pkt), pad));
932                         bus->dhd->tx_realloc++;
933                         new = PKTGET(osh, (PKTLEN(pkt) + DHD_SDALIGN), true);
934                         if (!new) {
935                                 DHD_ERROR(("%s: couldn't allocate new %d-byte "
936                                         "packet\n",
937                                         __func__, PKTLEN(pkt) + DHD_SDALIGN));
938                                 ret = BCME_NOMEM;
939                                 goto done;
940                         }
941
942                         PKTALIGN(osh, new, PKTLEN(pkt), DHD_SDALIGN);
943                         bcopy(PKTDATA(pkt), PKTDATA(new), PKTLEN(pkt));
944                         if (free_pkt)
945                                 PKTFREE(osh, pkt, true);
946                         /* free the pkt if canned one is not used */
947                         free_pkt = true;
948                         pkt = new;
949                         frame = (u8 *) PKTDATA(pkt);
950                         ASSERT(((unsigned long)frame % DHD_SDALIGN) == 0);
951                         pad = 0;
952                 } else {
953                         PKTPUSH(pkt, pad);
954                         frame = (u8 *) PKTDATA(pkt);
955
956                         ASSERT((pad + SDPCM_HDRLEN) <= (int)PKTLEN(pkt));
957                         bzero(frame, pad + SDPCM_HDRLEN);
958                 }
959         }
960         ASSERT(pad < DHD_SDALIGN);
961
962         /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
963         len = (u16) PKTLEN(pkt);
964         *(u16 *) frame = htol16(len);
965         *(((u16 *) frame) + 1) = htol16(~len);
966
967         /* Software tag: channel, sequence number, data offset */
968         swheader =
969             ((chan << SDPCM_CHANNEL_SHIFT) & SDPCM_CHANNEL_MASK) | bus->tx_seq |
970             (((pad +
971                SDPCM_HDRLEN) << SDPCM_DOFFSET_SHIFT) & SDPCM_DOFFSET_MASK);
972         htol32_ua_store(swheader, frame + SDPCM_FRAMETAG_LEN);
973         htol32_ua_store(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
974
975 #ifdef DHD_DEBUG
976         tx_packets[PKTPRIO(pkt)]++;
977         if (DHD_BYTES_ON() &&
978             (((DHD_CTL_ON() && (chan == SDPCM_CONTROL_CHANNEL)) ||
979               (DHD_DATA_ON() && (chan != SDPCM_CONTROL_CHANNEL))))) {
980                 prhex("Tx Frame", frame, len);
981         } else if (DHD_HDRS_ON()) {
982                 prhex("TxHdr", frame, min_t(u16, len, 16));
983         }
984 #endif
985
986         /* Raise len to next SDIO block to eliminate tail command */
987         if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
988                 u16 pad = bus->blocksize - (len % bus->blocksize);
989                 if ((pad <= bus->roundup) && (pad < bus->blocksize))
990 #ifdef NOTUSED
991                         if (pad <= PKTTAILROOM(pkt))
992 #endif                          /* NOTUSED */
993                                 len += pad;
994         } else if (len % DHD_SDALIGN) {
995                 len += DHD_SDALIGN - (len % DHD_SDALIGN);
996         }
997
998         /* Some controllers have trouble with odd bytes -- round to even */
999         if (forcealign && (len & (ALIGNMENT - 1))) {
1000 #ifdef NOTUSED
1001                 if (PKTTAILROOM(pkt))
1002 #endif
1003                         len = roundup(len, ALIGNMENT);
1004 #ifdef NOTUSED
1005                 else
1006                         DHD_ERROR(("%s: sending unrounded %d-byte packet\n",
1007                                    __func__, len));
1008 #endif
1009         }
1010
1011         do {
1012                 ret =
1013                     dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
1014                                         F2SYNC, frame, len, pkt, NULL, NULL);
1015                 bus->f2txdata++;
1016                 ASSERT(ret != BCME_PENDING);
1017
1018                 if (ret < 0) {
1019                         /* On failure, abort the command
1020                          and terminate the frame */
1021                         DHD_INFO(("%s: sdio error %d, abort command and "
1022                                 "terminate frame.\n", __func__, ret));
1023                         bus->tx_sderrs++;
1024
1025                         bcmsdh_abort(sdh, SDIO_FUNC_2);
1026                         bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
1027                                          SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
1028                                          NULL);
1029                         bus->f1regdata++;
1030
1031                         for (i = 0; i < 3; i++) {
1032                                 u8 hi, lo;
1033                                 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1034                                                      SBSDIO_FUNC1_WFRAMEBCHI,
1035                                                      NULL);
1036                                 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1037                                                      SBSDIO_FUNC1_WFRAMEBCLO,
1038                                                      NULL);
1039                                 bus->f1regdata += 2;
1040                                 if ((hi == 0) && (lo == 0))
1041                                         break;
1042                         }
1043
1044                 }
1045                 if (ret == 0)
1046                         bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1047
1048         } while ((ret < 0) && retrydata && retries++ < TXRETRIES);
1049
1050 done:
1051         /* restore pkt buffer pointer before calling tx complete routine */
1052         PKTPULL(pkt, SDPCM_HDRLEN + pad);
1053         dhd_os_sdunlock(bus->dhd);
1054         dhd_txcomplete(bus->dhd, pkt, ret != 0);
1055         dhd_os_sdlock(bus->dhd);
1056
1057         if (free_pkt)
1058                 PKTFREE(osh, pkt, true);
1059
1060         return ret;
1061 }
1062
1063 int dhd_bus_txdata(struct dhd_bus *bus, void *pkt)
1064 {
1065         int ret = BCME_ERROR;
1066         osl_t *osh;
1067         uint datalen, prec;
1068
1069         DHD_TRACE(("%s: Enter\n", __func__));
1070
1071         osh = bus->dhd->osh;
1072         datalen = PKTLEN(pkt);
1073
1074 #ifdef SDTEST
1075         /* Push the test header if doing loopback */
1076         if (bus->ext_loop) {
1077                 u8 *data;
1078                 PKTPUSH(pkt, SDPCM_TEST_HDRLEN);
1079                 data = PKTDATA(pkt);
1080                 *data++ = SDPCM_TEST_ECHOREQ;
1081                 *data++ = (u8) bus->loopid++;
1082                 *data++ = (datalen >> 0);
1083                 *data++ = (datalen >> 8);
1084                 datalen += SDPCM_TEST_HDRLEN;
1085         }
1086 #endif                          /* SDTEST */
1087
1088         /* Add space for the header */
1089         PKTPUSH(pkt, SDPCM_HDRLEN);
1090         ASSERT(IS_ALIGNED((unsigned long)PKTDATA(pkt), 2));
1091
1092         prec = PRIO2PREC((PKTPRIO(pkt) & PRIOMASK));
1093
1094         /* Check for existing queue, current flow-control,
1095                          pending event, or pending clock */
1096         if (dhd_deferred_tx || bus->fcstate || pktq_len(&bus->txq)
1097             || bus->dpc_sched || (!DATAOK(bus))
1098             || (bus->flowcontrol & NBITVAL(prec))
1099             || (bus->clkstate != CLK_AVAIL)) {
1100                 DHD_TRACE(("%s: deferring pktq len %d\n", __func__,
1101                            pktq_len(&bus->txq)));
1102                 bus->fcqueued++;
1103
1104                 /* Priority based enq */
1105                 dhd_os_sdlock_txq(bus->dhd);
1106                 if (dhd_prec_enq(bus->dhd, &bus->txq, pkt, prec) == false) {
1107                         PKTPULL(pkt, SDPCM_HDRLEN);
1108                         dhd_txcomplete(bus->dhd, pkt, false);
1109                         PKTFREE(osh, pkt, true);
1110                         DHD_ERROR(("%s: out of bus->txq !!!\n", __func__));
1111                         ret = BCME_NORESOURCE;
1112                 } else {
1113                         ret = BCME_OK;
1114                 }
1115                 dhd_os_sdunlock_txq(bus->dhd);
1116
1117                 if ((pktq_len(&bus->txq) >= FCHI) && dhd_doflow)
1118                         dhd_txflowcontrol(bus->dhd, 0, ON);
1119
1120 #ifdef DHD_DEBUG
1121                 if (pktq_plen(&bus->txq, prec) > qcount[prec])
1122                         qcount[prec] = pktq_plen(&bus->txq, prec);
1123 #endif
1124                 /* Schedule DPC if needed to send queued packet(s) */
1125                 if (dhd_deferred_tx && !bus->dpc_sched) {
1126                         bus->dpc_sched = true;
1127                         dhd_sched_dpc(bus->dhd);
1128                 }
1129         } else {
1130                 /* Lock: we're about to use shared data/code (and SDIO) */
1131                 dhd_os_sdlock(bus->dhd);
1132
1133                 /* Otherwise, send it now */
1134                 BUS_WAKE(bus);
1135                 /* Make sure back plane ht clk is on, no pending allowed */
1136                 dhdsdio_clkctl(bus, CLK_AVAIL, true);
1137
1138 #ifndef SDTEST
1139                 DHD_TRACE(("%s: calling txpkt\n", __func__));
1140                 ret = dhdsdio_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
1141 #else
1142                 ret = dhdsdio_txpkt(bus, pkt,
1143                                     (bus->ext_loop ? SDPCM_TEST_CHANNEL :
1144                                      SDPCM_DATA_CHANNEL), true);
1145 #endif
1146                 if (ret)
1147                         bus->dhd->tx_errors++;
1148                 else
1149                         bus->dhd->dstats.tx_bytes += datalen;
1150
1151                 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
1152                         bus->activity = false;
1153                         dhdsdio_clkctl(bus, CLK_NONE, true);
1154                 }
1155
1156                 dhd_os_sdunlock(bus->dhd);
1157         }
1158
1159         return ret;
1160 }
1161
1162 static uint dhdsdio_sendfromq(dhd_bus_t *bus, uint maxframes)
1163 {
1164         void *pkt;
1165         u32 intstatus = 0;
1166         uint retries = 0;
1167         int ret = 0, prec_out;
1168         uint cnt = 0;
1169         uint datalen;
1170         u8 tx_prec_map;
1171
1172         dhd_pub_t *dhd = bus->dhd;
1173         sdpcmd_regs_t *regs = bus->regs;
1174
1175         DHD_TRACE(("%s: Enter\n", __func__));
1176
1177         tx_prec_map = ~bus->flowcontrol;
1178
1179         /* Send frames until the limit or some other event */
1180         for (cnt = 0; (cnt < maxframes) && DATAOK(bus); cnt++) {
1181                 dhd_os_sdlock_txq(bus->dhd);
1182                 pkt = pktq_mdeq(&bus->txq, tx_prec_map, &prec_out);
1183                 if (pkt == NULL) {
1184                         dhd_os_sdunlock_txq(bus->dhd);
1185                         break;
1186                 }
1187                 dhd_os_sdunlock_txq(bus->dhd);
1188                 datalen = PKTLEN(pkt) - SDPCM_HDRLEN;
1189
1190 #ifndef SDTEST
1191                 ret = dhdsdio_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
1192 #else
1193                 ret = dhdsdio_txpkt(bus, pkt,
1194                                     (bus->ext_loop ? SDPCM_TEST_CHANNEL :
1195                                      SDPCM_DATA_CHANNEL), true);
1196 #endif
1197                 if (ret)
1198                         bus->dhd->tx_errors++;
1199                 else
1200                         bus->dhd->dstats.tx_bytes += datalen;
1201
1202                 /* In poll mode, need to check for other events */
1203                 if (!bus->intr && cnt) {
1204                         /* Check device status, signal pending interrupt */
1205                         R_SDREG(intstatus, &regs->intstatus, retries);
1206                         bus->f2txdata++;
1207                         if (bcmsdh_regfail(bus->sdh))
1208                                 break;
1209                         if (intstatus & bus->hostintmask)
1210                                 bus->ipend = true;
1211                 }
1212         }
1213
1214         /* Deflow-control stack if needed */
1215         if (dhd_doflow && dhd->up && (dhd->busstate == DHD_BUS_DATA) &&
1216             dhd->txoff && (pktq_len(&bus->txq) < FCLOW))
1217                 dhd_txflowcontrol(dhd, 0, OFF);
1218
1219         return cnt;
1220 }
1221
1222 int dhd_bus_txctl(struct dhd_bus *bus, unsigned char *msg, uint msglen)
1223 {
1224         u8 *frame;
1225         u16 len;
1226         u32 swheader;
1227         uint retries = 0;
1228         bcmsdh_info_t *sdh = bus->sdh;
1229         u8 doff = 0;
1230         int ret = -1;
1231         int i;
1232
1233         DHD_TRACE(("%s: Enter\n", __func__));
1234
1235         if (bus->dhd->dongle_reset)
1236                 return -EIO;
1237
1238         /* Back the pointer to make a room for bus header */
1239         frame = msg - SDPCM_HDRLEN;
1240         len = (msglen += SDPCM_HDRLEN);
1241
1242         /* Add alignment padding (optional for ctl frames) */
1243         if (dhd_alignctl) {
1244                 doff = ((unsigned long)frame % DHD_SDALIGN);
1245                 if (doff) {
1246                         frame -= doff;
1247                         len += doff;
1248                         msglen += doff;
1249                         bzero(frame, doff + SDPCM_HDRLEN);
1250                 }
1251                 ASSERT(doff < DHD_SDALIGN);
1252         }
1253         doff += SDPCM_HDRLEN;
1254
1255         /* Round send length to next SDIO block */
1256         if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
1257                 u16 pad = bus->blocksize - (len % bus->blocksize);
1258                 if ((pad <= bus->roundup) && (pad < bus->blocksize))
1259                         len += pad;
1260         } else if (len % DHD_SDALIGN) {
1261                 len += DHD_SDALIGN - (len % DHD_SDALIGN);
1262         }
1263
1264         /* Satisfy length-alignment requirements */
1265         if (forcealign && (len & (ALIGNMENT - 1)))
1266                 len = roundup(len, ALIGNMENT);
1267
1268         ASSERT(IS_ALIGNED((unsigned long)frame, 2));
1269
1270         /* Need to lock here to protect txseq and SDIO tx calls */
1271         dhd_os_sdlock(bus->dhd);
1272
1273         BUS_WAKE(bus);
1274
1275         /* Make sure backplane clock is on */
1276         dhdsdio_clkctl(bus, CLK_AVAIL, false);
1277
1278         /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
1279         *(u16 *) frame = htol16((u16) msglen);
1280         *(((u16 *) frame) + 1) = htol16(~msglen);
1281
1282         /* Software tag: channel, sequence number, data offset */
1283         swheader =
1284             ((SDPCM_CONTROL_CHANNEL << SDPCM_CHANNEL_SHIFT) &
1285              SDPCM_CHANNEL_MASK)
1286             | bus->tx_seq | ((doff << SDPCM_DOFFSET_SHIFT) &
1287                              SDPCM_DOFFSET_MASK);
1288         htol32_ua_store(swheader, frame + SDPCM_FRAMETAG_LEN);
1289         htol32_ua_store(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
1290
1291         if (!DATAOK(bus)) {
1292                 DHD_INFO(("%s: No bus credit bus->tx_max %d, bus->tx_seq %d\n",
1293                           __func__, bus->tx_max, bus->tx_seq));
1294                 bus->ctrl_frame_stat = true;
1295                 /* Send from dpc */
1296                 bus->ctrl_frame_buf = frame;
1297                 bus->ctrl_frame_len = len;
1298
1299                 dhd_wait_for_event(bus->dhd, &bus->ctrl_frame_stat);
1300
1301                 if (bus->ctrl_frame_stat == false) {
1302                         DHD_INFO(("%s: ctrl_frame_stat == false\n", __func__));
1303                         ret = 0;
1304                 } else {
1305                         DHD_INFO(("%s: ctrl_frame_stat == true\n", __func__));
1306                         ret = -1;
1307                 }
1308         }
1309
1310         if (ret == -1) {
1311 #ifdef DHD_DEBUG
1312                 if (DHD_BYTES_ON() && DHD_CTL_ON())
1313                         prhex("Tx Frame", frame, len);
1314                 else if (DHD_HDRS_ON())
1315                         prhex("TxHdr", frame, min_t(u16, len, 16));
1316 #endif
1317
1318                 do {
1319                         bus->ctrl_frame_stat = false;
1320                         ret =
1321                             dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh),
1322                                                 SDIO_FUNC_2, F2SYNC, frame, len,
1323                                                 NULL, NULL, NULL);
1324
1325                         ASSERT(ret != BCME_PENDING);
1326
1327                         if (ret < 0) {
1328                                 /* On failure, abort the command and
1329                                  terminate the frame */
1330                                 DHD_INFO(("%s: sdio error %d, abort command and terminate frame.\n",
1331                                         __func__, ret));
1332                                 bus->tx_sderrs++;
1333
1334                                 bcmsdh_abort(sdh, SDIO_FUNC_2);
1335
1336                                 bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
1337                                                  SBSDIO_FUNC1_FRAMECTRL,
1338                                                  SFC_WF_TERM, NULL);
1339                                 bus->f1regdata++;
1340
1341                                 for (i = 0; i < 3; i++) {
1342                                         u8 hi, lo;
1343                                         hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1344                                              SBSDIO_FUNC1_WFRAMEBCHI,
1345                                              NULL);
1346                                         lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
1347                                              SBSDIO_FUNC1_WFRAMEBCLO,
1348                                                              NULL);
1349                                         bus->f1regdata += 2;
1350                                         if ((hi == 0) && (lo == 0))
1351                                                 break;
1352                                 }
1353
1354                         }
1355                         if (ret == 0) {
1356                                 bus->tx_seq =
1357                                     (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
1358                         }
1359                 } while ((ret < 0) && retries++ < TXRETRIES);
1360         }
1361
1362         if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
1363                 bus->activity = false;
1364                 dhdsdio_clkctl(bus, CLK_NONE, true);
1365         }
1366
1367         dhd_os_sdunlock(bus->dhd);
1368
1369         if (ret)
1370                 bus->dhd->tx_ctlerrs++;
1371         else
1372                 bus->dhd->tx_ctlpkts++;
1373
1374         return ret ? -EIO : 0;
1375 }
1376
1377 int dhd_bus_rxctl(struct dhd_bus *bus, unsigned char *msg, uint msglen)
1378 {
1379         int timeleft;
1380         uint rxlen = 0;
1381         bool pending;
1382
1383         DHD_TRACE(("%s: Enter\n", __func__));
1384
1385         if (bus->dhd->dongle_reset)
1386                 return -EIO;
1387
1388         /* Wait until control frame is available */
1389         timeleft = dhd_os_ioctl_resp_wait(bus->dhd, &bus->rxlen, &pending);
1390
1391         dhd_os_sdlock(bus->dhd);
1392         rxlen = bus->rxlen;
1393         bcopy(bus->rxctl, msg, min(msglen, rxlen));
1394         bus->rxlen = 0;
1395         dhd_os_sdunlock(bus->dhd);
1396
1397         if (rxlen) {
1398                 DHD_CTL(("%s: resumed on rxctl frame, got %d expected %d\n",
1399                          __func__, rxlen, msglen));
1400         } else if (timeleft == 0) {
1401                 DHD_ERROR(("%s: resumed on timeout\n", __func__));
1402 #ifdef DHD_DEBUG
1403                 dhd_os_sdlock(bus->dhd);
1404                 dhdsdio_checkdied(bus, NULL, 0);
1405                 dhd_os_sdunlock(bus->dhd);
1406 #endif                          /* DHD_DEBUG */
1407         } else if (pending == true) {
1408                 DHD_CTL(("%s: cancelled\n", __func__));
1409                 return -ERESTARTSYS;
1410         } else {
1411                 DHD_CTL(("%s: resumed for unknown reason?\n", __func__));
1412 #ifdef DHD_DEBUG
1413                 dhd_os_sdlock(bus->dhd);
1414                 dhdsdio_checkdied(bus, NULL, 0);
1415                 dhd_os_sdunlock(bus->dhd);
1416 #endif                          /* DHD_DEBUG */
1417         }
1418
1419         if (rxlen)
1420                 bus->dhd->rx_ctlpkts++;
1421         else
1422                 bus->dhd->rx_ctlerrs++;
1423
1424         return rxlen ? (int)rxlen : -ETIMEDOUT;
1425 }
1426
1427 /* IOVar table */
1428 enum {
1429         IOV_INTR = 1,
1430         IOV_POLLRATE,
1431         IOV_SDREG,
1432         IOV_SBREG,
1433         IOV_SDCIS,
1434         IOV_MEMBYTES,
1435         IOV_MEMSIZE,
1436 #ifdef DHD_DEBUG
1437         IOV_CHECKDIED,
1438 #endif
1439         IOV_DOWNLOAD,
1440         IOV_FORCEEVEN,
1441         IOV_SDIOD_DRIVE,
1442         IOV_READAHEAD,
1443         IOV_SDRXCHAIN,
1444         IOV_ALIGNCTL,
1445         IOV_SDALIGN,
1446         IOV_DEVRESET,
1447         IOV_CPU,
1448 #ifdef SDTEST
1449         IOV_PKTGEN,
1450         IOV_EXTLOOP,
1451 #endif                          /* SDTEST */
1452         IOV_SPROM,
1453         IOV_TXBOUND,
1454         IOV_RXBOUND,
1455         IOV_TXMINMAX,
1456         IOV_IDLETIME,
1457         IOV_IDLECLOCK,
1458         IOV_SD1IDLE,
1459         IOV_SLEEP,
1460         IOV_VARS
1461 };
1462
1463 const bcm_iovar_t dhdsdio_iovars[] = {
1464         {"intr", IOV_INTR, 0, IOVT_BOOL, 0},
1465         {"sleep", IOV_SLEEP, 0, IOVT_BOOL, 0},
1466         {"pollrate", IOV_POLLRATE, 0, IOVT_UINT32, 0},
1467         {"idletime", IOV_IDLETIME, 0, IOVT_INT32, 0},
1468         {"idleclock", IOV_IDLECLOCK, 0, IOVT_INT32, 0},
1469         {"sd1idle", IOV_SD1IDLE, 0, IOVT_BOOL, 0},
1470         {"membytes", IOV_MEMBYTES, 0, IOVT_BUFFER, 2 * sizeof(int)},
1471         {"memsize", IOV_MEMSIZE, 0, IOVT_UINT32, 0},
1472         {"download", IOV_DOWNLOAD, 0, IOVT_BOOL, 0},
1473         {"vars", IOV_VARS, 0, IOVT_BUFFER, 0},
1474         {"sdiod_drive", IOV_SDIOD_DRIVE, 0, IOVT_UINT32, 0},
1475         {"readahead", IOV_READAHEAD, 0, IOVT_BOOL, 0},
1476         {"sdrxchain", IOV_SDRXCHAIN, 0, IOVT_BOOL, 0},
1477         {"alignctl", IOV_ALIGNCTL, 0, IOVT_BOOL, 0},
1478         {"sdalign", IOV_SDALIGN, 0, IOVT_BOOL, 0},
1479         {"devreset", IOV_DEVRESET, 0, IOVT_BOOL, 0},
1480 #ifdef DHD_DEBUG
1481         {"sdreg", IOV_SDREG, 0, IOVT_BUFFER, sizeof(sdreg_t)}
1482         ,
1483         {"sbreg", IOV_SBREG, 0, IOVT_BUFFER, sizeof(sdreg_t)}
1484         ,
1485         {"sd_cis", IOV_SDCIS, 0, IOVT_BUFFER, DHD_IOCTL_MAXLEN}
1486         ,
1487         {"forcealign", IOV_FORCEEVEN, 0, IOVT_BOOL, 0}
1488         ,
1489         {"txbound", IOV_TXBOUND, 0, IOVT_UINT32, 0}
1490         ,
1491         {"rxbound", IOV_RXBOUND, 0, IOVT_UINT32, 0}
1492         ,
1493         {"txminmax", IOV_TXMINMAX, 0, IOVT_UINT32, 0}
1494         ,
1495         {"cpu", IOV_CPU, 0, IOVT_BOOL, 0}
1496         ,
1497 #ifdef DHD_DEBUG
1498         {"checkdied", IOV_CHECKDIED, 0, IOVT_BUFFER, 0}
1499         ,
1500 #endif                          /* DHD_DEBUG  */
1501 #endif                          /* DHD_DEBUG */
1502 #ifdef SDTEST
1503         {"extloop", IOV_EXTLOOP, 0, IOVT_BOOL, 0}
1504         ,
1505         {"pktgen", IOV_PKTGEN, 0, IOVT_BUFFER, sizeof(dhd_pktgen_t)}
1506         ,
1507 #endif                          /* SDTEST */
1508
1509         {NULL, 0, 0, 0, 0}
1510 };
1511
1512 static void
1513 dhd_dump_pct(struct bcmstrbuf *strbuf, char *desc, uint num, uint div)
1514 {
1515         uint q1, q2;
1516
1517         if (!div) {
1518                 bcm_bprintf(strbuf, "%s N/A", desc);
1519         } else {
1520                 q1 = num / div;
1521                 q2 = (100 * (num - (q1 * div))) / div;
1522                 bcm_bprintf(strbuf, "%s %d.%02d", desc, q1, q2);
1523         }
1524 }
1525
1526 void dhd_bus_dump(dhd_pub_t *dhdp, struct bcmstrbuf *strbuf)
1527 {
1528         dhd_bus_t *bus = dhdp->bus;
1529
1530         bcm_bprintf(strbuf, "Bus SDIO structure:\n");
1531         bcm_bprintf(strbuf,
1532                     "hostintmask 0x%08x intstatus 0x%08x sdpcm_ver %d\n",
1533                     bus->hostintmask, bus->intstatus, bus->sdpcm_ver);
1534         bcm_bprintf(strbuf,
1535                     "fcstate %d qlen %d tx_seq %d, max %d, rxskip %d rxlen %d rx_seq %d\n",
1536                     bus->fcstate, pktq_len(&bus->txq), bus->tx_seq, bus->tx_max,
1537                     bus->rxskip, bus->rxlen, bus->rx_seq);
1538         bcm_bprintf(strbuf, "intr %d intrcount %d lastintrs %d spurious %d\n",
1539                     bus->intr, bus->intrcount, bus->lastintrs, bus->spurious);
1540         bcm_bprintf(strbuf, "pollrate %d pollcnt %d regfails %d\n",
1541                     bus->pollrate, bus->pollcnt, bus->regfails);
1542
1543         bcm_bprintf(strbuf, "\nAdditional counters:\n");
1544         bcm_bprintf(strbuf,
1545                     "tx_sderrs %d fcqueued %d rxrtx %d rx_toolong %d rxc_errors %d\n",
1546                     bus->tx_sderrs, bus->fcqueued, bus->rxrtx, bus->rx_toolong,
1547                     bus->rxc_errors);
1548         bcm_bprintf(strbuf, "rx_hdrfail %d badhdr %d badseq %d\n",
1549                     bus->rx_hdrfail, bus->rx_badhdr, bus->rx_badseq);
1550         bcm_bprintf(strbuf, "fc_rcvd %d, fc_xoff %d, fc_xon %d\n", bus->fc_rcvd,
1551                     bus->fc_xoff, bus->fc_xon);
1552         bcm_bprintf(strbuf, "rxglomfail %d, rxglomframes %d, rxglompkts %d\n",
1553                     bus->rxglomfail, bus->rxglomframes, bus->rxglompkts);
1554         bcm_bprintf(strbuf, "f2rx (hdrs/data) %d (%d/%d), f2tx %d f1regs %d\n",
1555                     (bus->f2rxhdrs + bus->f2rxdata), bus->f2rxhdrs,
1556                     bus->f2rxdata, bus->f2txdata, bus->f1regdata);
1557         {
1558                 dhd_dump_pct(strbuf, "\nRx: pkts/f2rd", bus->dhd->rx_packets,
1559                              (bus->f2rxhdrs + bus->f2rxdata));
1560                 dhd_dump_pct(strbuf, ", pkts/f1sd", bus->dhd->rx_packets,
1561                              bus->f1regdata);
1562                 dhd_dump_pct(strbuf, ", pkts/sd", bus->dhd->rx_packets,
1563                              (bus->f2rxhdrs + bus->f2rxdata + bus->f1regdata));
1564                 dhd_dump_pct(strbuf, ", pkts/int", bus->dhd->rx_packets,
1565                              bus->intrcount);
1566                 bcm_bprintf(strbuf, "\n");
1567
1568                 dhd_dump_pct(strbuf, "Rx: glom pct", (100 * bus->rxglompkts),
1569                              bus->dhd->rx_packets);
1570                 dhd_dump_pct(strbuf, ", pkts/glom", bus->rxglompkts,
1571                              bus->rxglomframes);
1572                 bcm_bprintf(strbuf, "\n");
1573
1574                 dhd_dump_pct(strbuf, "Tx: pkts/f2wr", bus->dhd->tx_packets,
1575                              bus->f2txdata);
1576                 dhd_dump_pct(strbuf, ", pkts/f1sd", bus->dhd->tx_packets,
1577                              bus->f1regdata);
1578                 dhd_dump_pct(strbuf, ", pkts/sd", bus->dhd->tx_packets,
1579                              (bus->f2txdata + bus->f1regdata));
1580                 dhd_dump_pct(strbuf, ", pkts/int", bus->dhd->tx_packets,
1581                              bus->intrcount);
1582                 bcm_bprintf(strbuf, "\n");
1583
1584                 dhd_dump_pct(strbuf, "Total: pkts/f2rw",
1585                              (bus->dhd->tx_packets + bus->dhd->rx_packets),
1586                              (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata));
1587                 dhd_dump_pct(strbuf, ", pkts/f1sd",
1588                              (bus->dhd->tx_packets + bus->dhd->rx_packets),
1589                              bus->f1regdata);
1590                 dhd_dump_pct(strbuf, ", pkts/sd",
1591                              (bus->dhd->tx_packets + bus->dhd->rx_packets),
1592                              (bus->f2txdata + bus->f2rxhdrs + bus->f2rxdata +
1593                               bus->f1regdata));
1594                 dhd_dump_pct(strbuf, ", pkts/int",
1595                              (bus->dhd->tx_packets + bus->dhd->rx_packets),
1596                              bus->intrcount);
1597                 bcm_bprintf(strbuf, "\n\n");
1598         }
1599
1600 #ifdef SDTEST
1601         if (bus->pktgen_count) {
1602                 bcm_bprintf(strbuf, "pktgen config and count:\n");
1603                 bcm_bprintf(strbuf,
1604                             "freq %d count %d print %d total %d min %d len %d\n",
1605                             bus->pktgen_freq, bus->pktgen_count,
1606                             bus->pktgen_print, bus->pktgen_total,
1607                             bus->pktgen_minlen, bus->pktgen_maxlen);
1608                 bcm_bprintf(strbuf, "send attempts %d rcvd %d fail %d\n",
1609                             bus->pktgen_sent, bus->pktgen_rcvd,
1610                             bus->pktgen_fail);
1611         }
1612 #endif                          /* SDTEST */
1613 #ifdef DHD_DEBUG
1614         bcm_bprintf(strbuf, "dpc_sched %d host interrupt%spending\n",
1615                     bus->dpc_sched,
1616                     (bcmsdh_intr_pending(bus->sdh) ? " " : " not "));
1617         bcm_bprintf(strbuf, "blocksize %d roundup %d\n", bus->blocksize,
1618                     bus->roundup);
1619 #endif                          /* DHD_DEBUG */
1620         bcm_bprintf(strbuf,
1621                     "clkstate %d activity %d idletime %d idlecount %d sleeping %d\n",
1622                     bus->clkstate, bus->activity, bus->idletime, bus->idlecount,
1623                     bus->sleeping);
1624 }
1625
1626 void dhd_bus_clearcounts(dhd_pub_t *dhdp)
1627 {
1628         dhd_bus_t *bus = (dhd_bus_t *) dhdp->bus;
1629
1630         bus->intrcount = bus->lastintrs = bus->spurious = bus->regfails = 0;
1631         bus->rxrtx = bus->rx_toolong = bus->rxc_errors = 0;
1632         bus->rx_hdrfail = bus->rx_badhdr = bus->rx_badseq = 0;
1633         bus->tx_sderrs = bus->fc_rcvd = bus->fc_xoff = bus->fc_xon = 0;
1634         bus->rxglomfail = bus->rxglomframes = bus->rxglompkts = 0;
1635         bus->f2rxhdrs = bus->f2rxdata = bus->f2txdata = bus->f1regdata = 0;
1636 }
1637
1638 #ifdef SDTEST
1639 static int dhdsdio_pktgen_get(dhd_bus_t *bus, u8 *arg)
1640 {
1641         dhd_pktgen_t pktgen;
1642
1643         pktgen.version = DHD_PKTGEN_VERSION;
1644         pktgen.freq = bus->pktgen_freq;
1645         pktgen.count = bus->pktgen_count;
1646         pktgen.print = bus->pktgen_print;
1647         pktgen.total = bus->pktgen_total;
1648         pktgen.minlen = bus->pktgen_minlen;
1649         pktgen.maxlen = bus->pktgen_maxlen;
1650         pktgen.numsent = bus->pktgen_sent;
1651         pktgen.numrcvd = bus->pktgen_rcvd;
1652         pktgen.numfail = bus->pktgen_fail;
1653         pktgen.mode = bus->pktgen_mode;
1654         pktgen.stop = bus->pktgen_stop;
1655
1656         bcopy(&pktgen, arg, sizeof(pktgen));
1657
1658         return 0;
1659 }
1660
1661 static int dhdsdio_pktgen_set(dhd_bus_t *bus, u8 *arg)
1662 {
1663         dhd_pktgen_t pktgen;
1664         uint oldcnt, oldmode;
1665
1666         bcopy(arg, &pktgen, sizeof(pktgen));
1667         if (pktgen.version != DHD_PKTGEN_VERSION)
1668                 return BCME_BADARG;
1669
1670         oldcnt = bus->pktgen_count;
1671         oldmode = bus->pktgen_mode;
1672
1673         bus->pktgen_freq = pktgen.freq;
1674         bus->pktgen_count = pktgen.count;
1675         bus->pktgen_print = pktgen.print;
1676         bus->pktgen_total = pktgen.total;
1677         bus->pktgen_minlen = pktgen.minlen;
1678         bus->pktgen_maxlen = pktgen.maxlen;
1679         bus->pktgen_mode = pktgen.mode;
1680         bus->pktgen_stop = pktgen.stop;
1681
1682         bus->pktgen_tick = bus->pktgen_ptick = 0;
1683         bus->pktgen_len = max(bus->pktgen_len, bus->pktgen_minlen);
1684         bus->pktgen_len = min(bus->pktgen_len, bus->pktgen_maxlen);
1685
1686         /* Clear counts for a new pktgen (mode change, or was stopped) */
1687         if (bus->pktgen_count && (!oldcnt || oldmode != bus->pktgen_mode))
1688                 bus->pktgen_sent = bus->pktgen_rcvd = bus->pktgen_fail = 0;
1689
1690         return 0;
1691 }
1692 #endif                          /* SDTEST */
1693
1694 static int
1695 dhdsdio_membytes(dhd_bus_t *bus, bool write, u32 address, u8 *data,
1696                  uint size)
1697 {
1698         int bcmerror = 0;
1699         u32 sdaddr;
1700         uint dsize;
1701
1702         /* Determine initial transfer parameters */
1703         sdaddr = address & SBSDIO_SB_OFT_ADDR_MASK;
1704         if ((sdaddr + size) & SBSDIO_SBWINDOW_MASK)
1705                 dsize = (SBSDIO_SB_OFT_ADDR_LIMIT - sdaddr);
1706         else
1707                 dsize = size;
1708
1709         /* Set the backplane window to include the start address */
1710         bcmerror = dhdsdio_set_siaddr_window(bus, address);
1711         if (bcmerror) {
1712                 DHD_ERROR(("%s: window change failed\n", __func__));
1713                 goto xfer_done;
1714         }
1715
1716         /* Do the transfer(s) */
1717         while (size) {
1718                 DHD_INFO(("%s: %s %d bytes at offset 0x%08x in window 0x%08x\n",
1719                           __func__, (write ? "write" : "read"), dsize,
1720                           sdaddr, (address & SBSDIO_SBWINDOW_MASK)));
1721                 bcmerror =
1722                      bcmsdh_rwdata(bus->sdh, write, sdaddr, data, dsize);
1723                 if (bcmerror) {
1724                         DHD_ERROR(("%s: membytes transfer failed\n", __func__));
1725                         break;
1726                 }
1727
1728                 /* Adjust for next transfer (if any) */
1729                 size -= dsize;
1730                 if (size) {
1731                         data += dsize;
1732                         address += dsize;
1733                         bcmerror = dhdsdio_set_siaddr_window(bus, address);
1734                         if (bcmerror) {
1735                                 DHD_ERROR(("%s: window change failed\n",
1736                                            __func__));
1737                                 break;
1738                         }
1739                         sdaddr = 0;
1740                         dsize = min_t(uint, SBSDIO_SB_OFT_ADDR_LIMIT, size);
1741                 }
1742         }
1743
1744 xfer_done:
1745         /* Return the window to backplane enumeration space for core access */
1746         if (dhdsdio_set_siaddr_window(bus, bcmsdh_cur_sbwad(bus->sdh))) {
1747                 DHD_ERROR(("%s: FAILED to set window back to 0x%x\n",
1748                            __func__, bcmsdh_cur_sbwad(bus->sdh)));
1749         }
1750
1751         return bcmerror;
1752 }
1753
1754 #ifdef DHD_DEBUG
1755 static int dhdsdio_readshared(dhd_bus_t *bus, sdpcm_shared_t *sh)
1756 {
1757         u32 addr;
1758         int rv;
1759
1760         /* Read last word in memory to determine address of
1761                          sdpcm_shared structure */
1762         rv = dhdsdio_membytes(bus, false, bus->ramsize - 4, (u8 *)&addr, 4);
1763         if (rv < 0)
1764                 return rv;
1765
1766         addr = ltoh32(addr);
1767
1768         DHD_INFO(("sdpcm_shared address 0x%08X\n", addr));
1769
1770         /*
1771          * Check if addr is valid.
1772          * NVRAM length at the end of memory should have been overwritten.
1773          */
1774         if (addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff)) {
1775                 DHD_ERROR(("%s: address (0x%08x) of sdpcm_shared invalid\n",
1776                            __func__, addr));
1777                 return BCME_ERROR;
1778         }
1779
1780         /* Read hndrte_shared structure */
1781         rv = dhdsdio_membytes(bus, false, addr, (u8 *) sh,
1782                               sizeof(sdpcm_shared_t));
1783         if (rv < 0)
1784                 return rv;
1785
1786         /* Endianness */
1787         sh->flags = ltoh32(sh->flags);
1788         sh->trap_addr = ltoh32(sh->trap_addr);
1789         sh->assert_exp_addr = ltoh32(sh->assert_exp_addr);
1790         sh->assert_file_addr = ltoh32(sh->assert_file_addr);
1791         sh->assert_line = ltoh32(sh->assert_line);
1792         sh->console_addr = ltoh32(sh->console_addr);
1793         sh->msgtrace_addr = ltoh32(sh->msgtrace_addr);
1794
1795         if ((sh->flags & SDPCM_SHARED_VERSION_MASK) != SDPCM_SHARED_VERSION) {
1796                 DHD_ERROR(("%s: sdpcm_shared version %d in dhd "
1797                            "is different than sdpcm_shared version %d in dongle\n",
1798                            __func__, SDPCM_SHARED_VERSION,
1799                            sh->flags & SDPCM_SHARED_VERSION_MASK));
1800                 return BCME_ERROR;
1801         }
1802
1803         return BCME_OK;
1804 }
1805
1806 static int dhdsdio_checkdied(dhd_bus_t *bus, u8 *data, uint size)
1807 {
1808         int bcmerror = 0;
1809         uint msize = 512;
1810         char *mbuffer = NULL;
1811         uint maxstrlen = 256;
1812         char *str = NULL;
1813         trap_t tr;
1814         sdpcm_shared_t sdpcm_shared;
1815         struct bcmstrbuf strbuf;
1816
1817         DHD_TRACE(("%s: Enter\n", __func__));
1818
1819         if (data == NULL) {
1820                 /*
1821                  * Called after a rx ctrl timeout. "data" is NULL.
1822                  * allocate memory to trace the trap or assert.
1823                  */
1824                 size = msize;
1825                 mbuffer = data = kmalloc(msize, GFP_ATOMIC);
1826                 if (mbuffer == NULL) {
1827                         DHD_ERROR(("%s: kmalloc(%d) failed\n", __func__,
1828                                    msize));
1829                         bcmerror = BCME_NOMEM;
1830                         goto done;
1831                 }
1832         }
1833
1834         str = kmalloc(maxstrlen, GFP_ATOMIC);
1835         if (str == NULL) {
1836                 DHD_ERROR(("%s: kmalloc(%d) failed\n", __func__, maxstrlen));
1837                 bcmerror = BCME_NOMEM;
1838                 goto done;
1839         }
1840
1841         bcmerror = dhdsdio_readshared(bus, &sdpcm_shared);
1842         if (bcmerror < 0)
1843                 goto done;
1844
1845         bcm_binit(&strbuf, data, size);
1846
1847         bcm_bprintf(&strbuf,
1848                     "msgtrace address : 0x%08X\nconsole address  : 0x%08X\n",
1849                     sdpcm_shared.msgtrace_addr, sdpcm_shared.console_addr);
1850
1851         if ((sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
1852                 /* NOTE: Misspelled assert is intentional - DO NOT FIX.
1853                  * (Avoids conflict with real asserts for programmatic
1854                  * parsing of output.)
1855                  */
1856                 bcm_bprintf(&strbuf, "Assrt not built in dongle\n");
1857         }
1858
1859         if ((sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP)) ==
1860             0) {
1861                 /* NOTE: Misspelled assert is intentional - DO NOT FIX.
1862                  * (Avoids conflict with real asserts for programmatic
1863                  * parsing of output.)
1864                  */
1865                 bcm_bprintf(&strbuf, "No trap%s in dongle",
1866                             (sdpcm_shared.flags & SDPCM_SHARED_ASSERT_BUILT)
1867                             ? "/assrt" : "");
1868         } else {
1869                 if (sdpcm_shared.flags & SDPCM_SHARED_ASSERT) {
1870                         /* Download assert */
1871                         bcm_bprintf(&strbuf, "Dongle assert");
1872                         if (sdpcm_shared.assert_exp_addr != 0) {
1873                                 str[0] = '\0';
1874                                 bcmerror = dhdsdio_membytes(bus, false,
1875                                                 sdpcm_shared.assert_exp_addr,
1876                                                 (u8 *) str, maxstrlen);
1877                                 if (bcmerror < 0)
1878                                         goto done;
1879
1880                                 str[maxstrlen - 1] = '\0';
1881                                 bcm_bprintf(&strbuf, " expr \"%s\"", str);
1882                         }
1883
1884                         if (sdpcm_shared.assert_file_addr != 0) {
1885                                 str[0] = '\0';
1886                                 bcmerror = dhdsdio_membytes(bus, false,
1887                                                 sdpcm_shared.assert_file_addr,
1888                                                 (u8 *) str, maxstrlen);
1889                                 if (bcmerror < 0)
1890                                         goto done;
1891
1892                                 str[maxstrlen - 1] = '\0';
1893                                 bcm_bprintf(&strbuf, " file \"%s\"", str);
1894                         }
1895
1896                         bcm_bprintf(&strbuf, " line %d ",
1897                                     sdpcm_shared.assert_line);
1898                 }
1899
1900                 if (sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
1901                         bcmerror = dhdsdio_membytes(bus, false,
1902                                         sdpcm_shared.trap_addr, (u8 *)&tr,
1903                                         sizeof(trap_t));
1904                         if (bcmerror < 0)
1905                                 goto done;
1906
1907                         bcm_bprintf(&strbuf,
1908                                     "Dongle trap type 0x%x @ epc 0x%x, cpsr 0x%x, spsr 0x%x, sp 0x%x,"
1909                                     "lp 0x%x, rpc 0x%x Trap offset 0x%x, "
1910                                     "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",
1911                                     tr.type, tr.epc, tr.cpsr, tr.spsr, tr.r13,
1912                                     tr.r14, tr.pc, sdpcm_shared.trap_addr,
1913                                     tr.r0, tr.r1, tr.r2, tr.r3, tr.r4, tr.r5,
1914                                     tr.r6, tr.r7);
1915                 }
1916         }
1917
1918         if (sdpcm_shared.flags & (SDPCM_SHARED_ASSERT | SDPCM_SHARED_TRAP))
1919                 DHD_ERROR(("%s: %s\n", __func__, strbuf.origbuf));
1920
1921 #ifdef DHD_DEBUG
1922         if (sdpcm_shared.flags & SDPCM_SHARED_TRAP) {
1923                 /* Mem dump to a file on device */
1924                 dhdsdio_mem_dump(bus);
1925         }
1926 #endif                          /* DHD_DEBUG */
1927
1928 done:
1929         if (mbuffer)
1930                 kfree(mbuffer);
1931         if (str)
1932                 kfree(str);
1933
1934         return bcmerror;
1935 }
1936
1937 static int dhdsdio_mem_dump(dhd_bus_t *bus)
1938 {
1939         int ret = 0;
1940         int size;               /* Full mem size */
1941         int start = 0;          /* Start address */
1942         int read_size = 0;      /* Read size of each iteration */
1943         u8 *buf = NULL, *databuf = NULL;
1944
1945         /* Get full mem size */
1946         size = bus->ramsize;
1947         buf = kmalloc(size, GFP_ATOMIC);
1948         if (!buf) {
1949                 printf("%s: Out of memory (%d bytes)\n", __func__, size);
1950                 return -1;
1951         }
1952
1953         /* Read mem content */
1954         printf("Dump dongle memory");
1955         databuf = buf;
1956         while (size) {
1957                 read_size = min(MEMBLOCK, size);
1958                 ret = dhdsdio_membytes(bus, false, start, databuf, read_size);
1959                 if (ret) {
1960                         printf("%s: Error membytes %d\n", __func__, ret);
1961                         if (buf)
1962                                 kfree(buf);
1963                         return -1;
1964                 }
1965                 printf(".");
1966
1967                 /* Decrement size and increment start address */
1968                 size -= read_size;
1969                 start += read_size;
1970                 databuf += read_size;
1971         }
1972         printf("Done\n");
1973
1974         /* free buf before return !!! */
1975         if (write_to_file(bus->dhd, buf, bus->ramsize)) {
1976                 printf("%s: Error writing to files\n", __func__);
1977                 return -1;
1978         }
1979
1980         /* buf free handled in write_to_file, not here */
1981         return 0;
1982 }
1983
1984 #define CONSOLE_LINE_MAX        192
1985
1986 static int dhdsdio_readconsole(dhd_bus_t *bus)
1987 {
1988         dhd_console_t *c = &bus->console;
1989         u8 line[CONSOLE_LINE_MAX], ch;
1990         u32 n, idx, addr;
1991         int rv;
1992
1993         /* Don't do anything until FWREADY updates console address */
1994         if (bus->console_addr == 0)
1995                 return 0;
1996
1997         /* Read console log struct */
1998         addr = bus->console_addr + offsetof(hndrte_cons_t, log);
1999         rv = dhdsdio_membytes(bus, false, addr, (u8 *)&c->log,
2000                                 sizeof(c->log));
2001         if (rv < 0)
2002                 return rv;
2003
2004         /* Allocate console buffer (one time only) */
2005         if (c->buf == NULL) {
2006                 c->bufsize = ltoh32(c->log.buf_size);
2007                 c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2008                 if (c->buf == NULL)
2009                         return BCME_NOMEM;
2010         }
2011
2012         idx = ltoh32(c->log.idx);
2013
2014         /* Protect against corrupt value */
2015         if (idx > c->bufsize)
2016                 return BCME_ERROR;
2017
2018         /* Skip reading the console buffer if the index pointer
2019          has not moved */
2020         if (idx == c->last)
2021                 return BCME_OK;
2022
2023         /* Read the console buffer */
2024         addr = ltoh32(c->log.buf);
2025         rv = dhdsdio_membytes(bus, false, addr, c->buf, c->bufsize);
2026         if (rv < 0)
2027                 return rv;
2028
2029         while (c->last != idx) {
2030                 for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2031                         if (c->last == idx) {
2032                                 /* This would output a partial line.
2033                                  * Instead, back up
2034                                  * the buffer pointer and output this
2035                                  * line next time around.
2036                                  */
2037                                 if (c->last >= n)
2038                                         c->last -= n;
2039                                 else
2040                                         c->last = c->bufsize - n;
2041                                 goto break2;
2042                         }
2043                         ch = c->buf[c->last];
2044                         c->last = (c->last + 1) % c->bufsize;
2045                         if (ch == '\n')
2046                                 break;
2047                         line[n] = ch;
2048                 }
2049
2050                 if (n > 0) {
2051                         if (line[n - 1] == '\r')
2052                                 n--;
2053                         line[n] = 0;
2054                         printf("CONSOLE: %s\n", line);
2055                 }
2056         }
2057 break2:
2058
2059         return BCME_OK;
2060 }
2061 #endif                          /* DHD_DEBUG */
2062
2063 int dhdsdio_downloadvars(dhd_bus_t *bus, void *arg, int len)
2064 {
2065         int bcmerror = BCME_OK;
2066
2067         DHD_TRACE(("%s: Enter\n", __func__));
2068
2069         /* Basic sanity checks */
2070         if (bus->dhd->up) {
2071                 bcmerror = BCME_NOTDOWN;
2072                 goto err;
2073         }
2074         if (!len) {
2075                 bcmerror = BCME_BUFTOOSHORT;
2076                 goto err;
2077         }
2078
2079         /* Free the old ones and replace with passed variables */
2080         if (bus->vars)
2081                 kfree(bus->vars);
2082
2083         bus->vars = kmalloc(len, GFP_ATOMIC);
2084         bus->varsz = bus->vars ? len : 0;
2085         if (bus->vars == NULL) {
2086                 bcmerror = BCME_NOMEM;
2087                 goto err;
2088         }
2089
2090         /* Copy the passed variables, which should include the
2091                  terminating double-null */
2092         bcopy(arg, bus->vars, bus->varsz);
2093 err:
2094         return bcmerror;
2095 }
2096
2097 static int
2098 dhdsdio_doiovar(dhd_bus_t *bus, const bcm_iovar_t *vi, u32 actionid,
2099                 const char *name, void *params, int plen, void *arg, int len,
2100                 int val_size)
2101 {
2102         int bcmerror = 0;
2103         s32 int_val = 0;
2104         bool bool_val = 0;
2105
2106         DHD_TRACE(("%s: Enter, action %d name %s params %p plen %d arg %p "
2107                 "len %d val_size %d\n",
2108                 __func__, actionid, name, params, plen, arg, len, val_size));
2109
2110         bcmerror = bcm_iovar_lencheck(vi, arg, len, IOV_ISSET(actionid));
2111         if (bcmerror != 0)
2112                 goto exit;
2113
2114         if (plen >= (int)sizeof(int_val))
2115                 bcopy(params, &int_val, sizeof(int_val));
2116
2117         bool_val = (int_val != 0) ? true : false;
2118
2119         /* Some ioctls use the bus */
2120         dhd_os_sdlock(bus->dhd);
2121
2122         /* Check if dongle is in reset. If so, only allow DEVRESET iovars */
2123         if (bus->dhd->dongle_reset && !(actionid == IOV_SVAL(IOV_DEVRESET) ||
2124                                         actionid == IOV_GVAL(IOV_DEVRESET))) {
2125                 bcmerror = BCME_NOTREADY;
2126                 goto exit;
2127         }
2128
2129         /* Handle sleep stuff before any clock mucking */
2130         if (vi->varid == IOV_SLEEP) {
2131                 if (IOV_ISSET(actionid)) {
2132                         bcmerror = dhdsdio_bussleep(bus, bool_val);
2133                 } else {
2134                         int_val = (s32) bus->sleeping;
2135                         bcopy(&int_val, arg, val_size);
2136                 }
2137                 goto exit;
2138         }
2139
2140         /* Request clock to allow SDIO accesses */
2141         if (!bus->dhd->dongle_reset) {
2142                 BUS_WAKE(bus);
2143                 dhdsdio_clkctl(bus, CLK_AVAIL, false);
2144         }
2145
2146         switch (actionid) {
2147         case IOV_GVAL(IOV_INTR):
2148                 int_val = (s32) bus->intr;
2149                 bcopy(&int_val, arg, val_size);
2150                 break;
2151
2152         case IOV_SVAL(IOV_INTR):
2153                 bus->intr = bool_val;
2154                 bus->intdis = false;
2155                 if (bus->dhd->up) {
2156                         if (bus->intr) {
2157                                 DHD_INTR(("%s: enable SDIO device interrupts\n",
2158                                           __func__));
2159                                 bcmsdh_intr_enable(bus->sdh);
2160                         } else {
2161                                 DHD_INTR(("%s: disable SDIO interrupts\n",
2162                                           __func__));
2163                                 bcmsdh_intr_disable(bus->sdh);
2164                         }
2165                 }
2166                 break;
2167
2168         case IOV_GVAL(IOV_POLLRATE):
2169                 int_val = (s32) bus->pollrate;
2170                 bcopy(&int_val, arg, val_size);
2171                 break;
2172
2173         case IOV_SVAL(IOV_POLLRATE):
2174                 bus->pollrate = (uint) int_val;
2175                 bus->poll = (bus->pollrate != 0);
2176                 break;
2177
2178         case IOV_GVAL(IOV_IDLETIME):
2179                 int_val = bus->idletime;
2180                 bcopy(&int_val, arg, val_size);
2181                 break;
2182
2183         case IOV_SVAL(IOV_IDLETIME):
2184                 if ((int_val < 0) && (int_val != DHD_IDLE_IMMEDIATE))
2185                         bcmerror = BCME_BADARG;
2186                 else
2187                         bus->idletime = int_val;
2188                 break;
2189
2190         case IOV_GVAL(IOV_IDLECLOCK):
2191                 int_val = (s32) bus->idleclock;
2192                 bcopy(&int_val, arg, val_size);
2193                 break;
2194
2195         case IOV_SVAL(IOV_IDLECLOCK):
2196                 bus->idleclock = int_val;
2197                 break;
2198
2199         case IOV_GVAL(IOV_SD1IDLE):
2200                 int_val = (s32) sd1idle;
2201                 bcopy(&int_val, arg, val_size);
2202                 break;
2203
2204         case IOV_SVAL(IOV_SD1IDLE):
2205                 sd1idle = bool_val;
2206                 break;
2207
2208         case IOV_SVAL(IOV_MEMBYTES):
2209         case IOV_GVAL(IOV_MEMBYTES):
2210                 {
2211                         u32 address;
2212                         uint size, dsize;
2213                         u8 *data;
2214
2215                         bool set = (actionid == IOV_SVAL(IOV_MEMBYTES));
2216
2217                         ASSERT(plen >= 2 * sizeof(int));
2218
2219                         address = (u32) int_val;
2220                         bcopy((char *)params + sizeof(int_val), &int_val,
2221                               sizeof(int_val));
2222                         size = (uint) int_val;
2223
2224                         /* Do some validation */
2225                         dsize = set ? plen - (2 * sizeof(int)) : len;
2226                         if (dsize < size) {
2227                                 DHD_ERROR(("%s: error on %s membytes, addr "
2228                                 "0x%08x size %d dsize %d\n",
2229                                 __func__, (set ? "set" : "get"),
2230                                 address, size, dsize));
2231                                 bcmerror = BCME_BADARG;
2232                                 break;
2233                         }
2234
2235                         DHD_INFO(("%s: Request to %s %d bytes at address "
2236                         "0x%08x\n",
2237                         __func__, (set ? "write" : "read"), size, address));
2238
2239                         /* If we know about SOCRAM, check for a fit */
2240                         if ((bus->orig_ramsize) &&
2241                             ((address > bus->orig_ramsize)
2242                              || (address + size > bus->orig_ramsize))) {
2243                                 DHD_ERROR(("%s: ramsize 0x%08x doesn't have %d "
2244                                 "bytes at 0x%08x\n",
2245                                 __func__, bus->orig_ramsize, size, address));
2246                                 bcmerror = BCME_BADARG;
2247                                 break;
2248                         }
2249
2250                         /* Generate the actual data pointer */
2251                         data =
2252                             set ? (u8 *) params +
2253                             2 * sizeof(int) : (u8 *) arg;
2254
2255                         /* Call to do the transfer */
2256                         bcmerror =
2257                             dhdsdio_membytes(bus, set, address, data, size);
2258
2259                         break;
2260                 }
2261
2262         case IOV_GVAL(IOV_MEMSIZE):
2263                 int_val = (s32) bus->ramsize;
2264                 bcopy(&int_val, arg, val_size);
2265                 break;
2266
2267         case IOV_GVAL(IOV_SDIOD_DRIVE):
2268                 int_val = (s32) dhd_sdiod_drive_strength;
2269                 bcopy(&int_val, arg, val_size);
2270                 break;
2271
2272         case IOV_SVAL(IOV_SDIOD_DRIVE):
2273                 dhd_sdiod_drive_strength = int_val;
2274                 si_sdiod_drive_strength_init(bus->sih, bus->dhd->osh,
2275                                              dhd_sdiod_drive_strength);
2276                 break;
2277
2278         case IOV_SVAL(IOV_DOWNLOAD):
2279                 bcmerror = dhdsdio_download_state(bus, bool_val);
2280                 break;
2281
2282         case IOV_SVAL(IOV_VARS):
2283                 bcmerror = dhdsdio_downloadvars(bus, arg, len);
2284                 break;
2285
2286         case IOV_GVAL(IOV_READAHEAD):
2287                 int_val = (s32) dhd_readahead;
2288                 bcopy(&int_val, arg, val_size);
2289                 break;
2290
2291         case IOV_SVAL(IOV_READAHEAD):
2292                 if (bool_val && !dhd_readahead)
2293                         bus->nextlen = 0;
2294                 dhd_readahead = bool_val;
2295                 break;
2296
2297         case IOV_GVAL(IOV_SDRXCHAIN):
2298                 int_val = (s32) bus->use_rxchain;
2299                 bcopy(&int_val, arg, val_size);
2300                 break;
2301
2302         case IOV_SVAL(IOV_SDRXCHAIN):
2303                 if (bool_val && !bus->sd_rxchain)
2304                         bcmerror = BCME_UNSUPPORTED;
2305                 else
2306                         bus->use_rxchain = bool_val;
2307                 break;
2308         case IOV_GVAL(IOV_ALIGNCTL):
2309                 int_val = (s32) dhd_alignctl;
2310                 bcopy(&int_val, arg, val_size);
2311                 break;
2312
2313         case IOV_SVAL(IOV_ALIGNCTL):
2314                 dhd_alignctl = bool_val;
2315                 break;
2316
2317         case IOV_GVAL(IOV_SDALIGN):
2318                 int_val = DHD_SDALIGN;
2319                 bcopy(&int_val, arg, val_size);
2320                 break;
2321
2322 #ifdef DHD_DEBUG
2323         case IOV_GVAL(IOV_VARS):
2324                 if (bus->varsz < (uint) len)
2325                         bcopy(bus->vars, arg, bus->varsz);
2326                 else
2327                         bcmerror = BCME_BUFTOOSHORT;
2328                 break;
2329 #endif                          /* DHD_DEBUG */
2330
2331 #ifdef DHD_DEBUG
2332         case IOV_GVAL(IOV_SDREG):
2333                 {
2334                         sdreg_t *sd_ptr;
2335                         u32 addr, size;
2336
2337                         sd_ptr = (sdreg_t *) params;
2338
2339                         addr = (unsigned long)bus->regs + sd_ptr->offset;
2340                         size = sd_ptr->func;
2341                         int_val = (s32) bcmsdh_reg_read(bus->sdh, addr, size);
2342                         if (bcmsdh_regfail(bus->sdh))
2343                                 bcmerror = BCME_SDIO_ERROR;
2344                         bcopy(&int_val, arg, sizeof(s32));
2345                         break;
2346                 }
2347
2348         case IOV_SVAL(IOV_SDREG):
2349                 {
2350                         sdreg_t *sd_ptr;
2351                         u32 addr, size;
2352
2353                         sd_ptr = (sdreg_t *) params;
2354
2355                         addr = (unsigned long)bus->regs + sd_ptr->offset;
2356                         size = sd_ptr->func;
2357                         bcmsdh_reg_write(bus->sdh, addr, size, sd_ptr->value);
2358                         if (bcmsdh_regfail(bus->sdh))
2359                                 bcmerror = BCME_SDIO_ERROR;
2360                         break;
2361                 }
2362
2363                 /* Same as above, but offset is not backplane
2364                  (not SDIO core) */
2365         case IOV_GVAL(IOV_SBREG):
2366                 {
2367                         sdreg_t sdreg;
2368                         u32 addr, size;
2369
2370                         bcopy(params, &sdreg, sizeof(sdreg));
2371
2372                         addr = SI_ENUM_BASE + sdreg.offset;
2373                         size = sdreg.func;
2374                         int_val = (s32) bcmsdh_reg_read(bus->sdh, addr, size);
2375                         if (bcmsdh_regfail(bus->sdh))
2376                                 bcmerror = BCME_SDIO_ERROR;
2377                         bcopy(&int_val, arg, sizeof(s32));
2378                         break;
2379                 }
2380
2381         case IOV_SVAL(IOV_SBREG):
2382                 {
2383                         sdreg_t sdreg;
2384                         u32 addr, size;
2385
2386                         bcopy(params, &sdreg, sizeof(sdreg));
2387
2388                         addr = SI_ENUM_BASE + sdreg.offset;
2389                         size = sdreg.func;
2390                         bcmsdh_reg_write(bus->sdh, addr, size, sdreg.value);
2391                         if (bcmsdh_regfail(bus->sdh))
2392                                 bcmerror = BCME_SDIO_ERROR;
2393                         break;
2394                 }
2395
2396         case IOV_GVAL(IOV_SDCIS):
2397                 {
2398                         *(char *)arg = 0;
2399
2400                         strcat(arg, "\nFunc 0\n");
2401                         bcmsdh_cis_read(bus->sdh, 0x10,
2402                                         (u8 *) arg + strlen(arg),
2403                                         SBSDIO_CIS_SIZE_LIMIT);
2404                         strcat(arg, "\nFunc 1\n");
2405                         bcmsdh_cis_read(bus->sdh, 0x11,
2406                                         (u8 *) arg + strlen(arg),
2407                                         SBSDIO_CIS_SIZE_LIMIT);
2408                         strcat(arg, "\nFunc 2\n");
2409                         bcmsdh_cis_read(bus->sdh, 0x12,
2410                                         (u8 *) arg + strlen(arg),
2411                                         SBSDIO_CIS_SIZE_LIMIT);
2412                         break;
2413                 }
2414
2415         case IOV_GVAL(IOV_FORCEEVEN):
2416                 int_val = (s32) forcealign;
2417                 bcopy(&int_val, arg, val_size);
2418                 break;
2419
2420         case IOV_SVAL(IOV_FORCEEVEN):
2421                 forcealign = bool_val;
2422                 break;
2423
2424         case IOV_GVAL(IOV_TXBOUND):
2425                 int_val = (s32) dhd_txbound;
2426                 bcopy(&int_val, arg, val_size);
2427                 break;
2428
2429         case IOV_SVAL(IOV_TXBOUND):
2430                 dhd_txbound = (uint) int_val;
2431                 break;
2432
2433         case IOV_GVAL(IOV_RXBOUND):
2434                 int_val = (s32) dhd_rxbound;
2435                 bcopy(&int_val, arg, val_size);
2436                 break;
2437
2438         case IOV_SVAL(IOV_RXBOUND):
2439                 dhd_rxbound = (uint) int_val;
2440                 break;
2441
2442         case IOV_GVAL(IOV_TXMINMAX):
2443                 int_val = (s32) dhd_txminmax;
2444                 bcopy(&int_val, arg, val_size);
2445                 break;
2446
2447         case IOV_SVAL(IOV_TXMINMAX):
2448                 dhd_txminmax = (uint) int_val;
2449                 break;
2450 #endif                          /* DHD_DEBUG */
2451
2452 #ifdef SDTEST
2453         case IOV_GVAL(IOV_EXTLOOP):
2454                 int_val = (s32) bus->ext_loop;
2455                 bcopy(&int_val, arg, val_size);
2456                 break;
2457
2458         case IOV_SVAL(IOV_EXTLOOP):
2459                 bus->ext_loop = bool_val;
2460                 break;
2461
2462         case IOV_GVAL(IOV_PKTGEN):
2463                 bcmerror = dhdsdio_pktgen_get(bus, arg);
2464                 break;
2465
2466         case IOV_SVAL(IOV_PKTGEN):
2467                 bcmerror = dhdsdio_pktgen_set(bus, arg);
2468                 break;
2469 #endif                          /* SDTEST */
2470
2471         case IOV_SVAL(IOV_DEVRESET):
2472                 DHD_TRACE(("%s: Called set IOV_DEVRESET=%d dongle_reset=%d "
2473                         "busstate=%d\n",
2474                         __func__, bool_val, bus->dhd->dongle_reset,
2475                         bus->dhd->busstate));
2476
2477                 ASSERT(bus->dhd->osh);
2478                 /* ASSERT(bus->cl_devid); */
2479
2480                 dhd_bus_devreset(bus->dhd, (u8) bool_val);
2481
2482                 break;
2483
2484         case IOV_GVAL(IOV_DEVRESET):
2485                 DHD_TRACE(("%s: Called get IOV_DEVRESET\n", __func__));
2486
2487                 /* Get its status */
2488                 int_val = (bool) bus->dhd->dongle_reset;
2489                 bcopy(&int_val, arg, val_size);
2490
2491                 break;
2492
2493         default:
2494                 bcmerror = BCME_UNSUPPORTED;
2495                 break;
2496         }
2497
2498 exit:
2499         if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
2500                 bus->activity = false;
2501                 dhdsdio_clkctl(bus, CLK_NONE, true);
2502         }
2503
2504         dhd_os_sdunlock(bus->dhd);
2505
2506         if (actionid == IOV_SVAL(IOV_DEVRESET) && bool_val == false)
2507                 dhd_preinit_ioctls((dhd_pub_t *) bus->dhd);
2508
2509         return bcmerror;
2510 }
2511
2512 static int dhdsdio_write_vars(dhd_bus_t *bus)
2513 {
2514         int bcmerror = 0;
2515         u32 varsize;
2516         u32 varaddr;
2517         u8 *vbuffer;
2518         u32 varsizew;
2519 #ifdef DHD_DEBUG
2520         char *nvram_ularray;
2521 #endif                          /* DHD_DEBUG */
2522
2523         /* Even if there are no vars are to be written, we still
2524                  need to set the ramsize. */
2525         varsize = bus->varsz ? roundup(bus->varsz, 4) : 0;
2526         varaddr = (bus->ramsize - 4) - varsize;
2527
2528         if (bus->vars) {
2529                 vbuffer = kmalloc(varsize, GFP_ATOMIC);
2530                 if (!vbuffer)
2531                         return BCME_NOMEM;
2532
2533                 bzero(vbuffer, varsize);
2534                 bcopy(bus->vars, vbuffer, bus->varsz);
2535
2536                 /* Write the vars list */
2537                 bcmerror =
2538                     dhdsdio_membytes(bus, true, varaddr, vbuffer, varsize);
2539 #ifdef DHD_DEBUG
2540                 /* Verify NVRAM bytes */
2541                 DHD_INFO(("Compare NVRAM dl & ul; varsize=%d\n", varsize));
2542                 nvram_ularray = kmalloc(varsize, GFP_ATOMIC);
2543                 if (!nvram_ularray)
2544                         return BCME_NOMEM;
2545
2546                 /* Upload image to verify downloaded contents. */
2547                 memset(nvram_ularray, 0xaa, varsize);
2548
2549                 /* Read the vars list to temp buffer for comparison */
2550                 bcmerror =
2551                     dhdsdio_membytes(bus, false, varaddr, nvram_ularray,
2552                                      varsize);
2553                 if (bcmerror) {
2554                         DHD_ERROR(("%s: error %d on reading %d nvram bytes at "
2555                         "0x%08x\n", __func__, bcmerror, varsize, varaddr));
2556                 }
2557                 /* Compare the org NVRAM with the one read from RAM */
2558                 if (memcmp(vbuffer, nvram_ularray, varsize)) {
2559                         DHD_ERROR(("%s: Downloaded NVRAM image is corrupted.\n",
2560                                    __func__));
2561                 } else
2562                         DHD_ERROR(("%s: Download/Upload/Compare of NVRAM ok.\n",
2563                                 __func__));
2564
2565                 kfree(nvram_ularray);
2566 #endif                          /* DHD_DEBUG */
2567
2568                 kfree(vbuffer);
2569         }
2570
2571         /* adjust to the user specified RAM */
2572         DHD_INFO(("Physical memory size: %d, usable memory size: %d\n",
2573                   bus->orig_ramsize, bus->ramsize));
2574         DHD_INFO(("Vars are at %d, orig varsize is %d\n", varaddr, varsize));
2575         varsize = ((bus->orig_ramsize - 4) - varaddr);
2576
2577         /*
2578          * Determine the length token:
2579          * Varsize, converted to words, in lower 16-bits, checksum
2580          * in upper 16-bits.
2581          */
2582         if (bcmerror) {
2583                 varsizew = 0;
2584         } else {
2585                 varsizew = varsize / 4;
2586                 varsizew = (~varsizew << 16) | (varsizew & 0x0000FFFF);
2587                 varsizew = htol32(varsizew);
2588         }
2589
2590         DHD_INFO(("New varsize is %d, length token=0x%08x\n", varsize,
2591                   varsizew));
2592
2593         /* Write the length token to the last word */
2594         bcmerror = dhdsdio_membytes(bus, true, (bus->orig_ramsize - 4),
2595                                     (u8 *)&varsizew, 4);
2596
2597         return bcmerror;
2598 }
2599
2600 static int dhdsdio_download_state(dhd_bus_t *bus, bool enter)
2601 {
2602         uint retries;
2603         int bcmerror = 0;
2604
2605         /* To enter download state, disable ARM and reset SOCRAM.
2606          * To exit download state, simply reset ARM (default is RAM boot).
2607          */
2608         if (enter) {
2609
2610                 bus->alp_only = true;
2611
2612                 if (!(si_setcore(bus->sih, ARM7S_CORE_ID, 0)) &&
2613                     !(si_setcore(bus->sih, ARMCM3_CORE_ID, 0))) {
2614                         DHD_ERROR(("%s: Failed to find ARM core!\n", __func__));
2615                         bcmerror = BCME_ERROR;
2616                         goto fail;
2617                 }
2618
2619                 si_core_disable(bus->sih, 0);
2620                 if (bcmsdh_regfail(bus->sdh)) {
2621                         bcmerror = BCME_SDIO_ERROR;
2622                         goto fail;
2623                 }
2624
2625                 if (!(si_setcore(bus->sih, SOCRAM_CORE_ID, 0))) {
2626                         DHD_ERROR(("%s: Failed to find SOCRAM core!\n",
2627                                    __func__));
2628                         bcmerror = BCME_ERROR;
2629                         goto fail;
2630                 }
2631
2632                 si_core_reset(bus->sih, 0, 0);
2633                 if (bcmsdh_regfail(bus->sdh)) {
2634                         DHD_ERROR(("%s: Failure trying reset SOCRAM core?\n",
2635                                    __func__));
2636                         bcmerror = BCME_SDIO_ERROR;
2637                         goto fail;
2638                 }
2639
2640                 /* Clear the top bit of memory */
2641                 if (bus->ramsize) {
2642                         u32 zeros = 0;
2643                         dhdsdio_membytes(bus, true, bus->ramsize - 4,
2644                                          (u8 *)&zeros, 4);
2645                 }
2646         } else {
2647                 if (!(si_setcore(bus->sih, SOCRAM_CORE_ID, 0))) {
2648                         DHD_ERROR(("%s: Failed to find SOCRAM core!\n",
2649                                    __func__));
2650                         bcmerror = BCME_ERROR;
2651                         goto fail;
2652                 }
2653
2654                 if (!si_iscoreup(bus->sih)) {
2655                         DHD_ERROR(("%s: SOCRAM core is down after reset?\n",
2656                                    __func__));
2657                         bcmerror = BCME_ERROR;
2658                         goto fail;
2659                 }
2660
2661                 bcmerror = dhdsdio_write_vars(bus);
2662                 if (bcmerror) {
2663                         DHD_ERROR(("%s: no vars written to RAM\n", __func__));
2664                         bcmerror = 0;
2665                 }
2666
2667                 if (!si_setcore(bus->sih, PCMCIA_CORE_ID, 0) &&
2668                     !si_setcore(bus->sih, SDIOD_CORE_ID, 0)) {
2669                         DHD_ERROR(("%s: Can't change back to SDIO core?\n",
2670                                    __func__));
2671                         bcmerror = BCME_ERROR;
2672                         goto fail;
2673                 }
2674                 W_SDREG(0xFFFFFFFF, &bus->regs->intstatus, retries);
2675
2676                 if (!(si_setcore(bus->sih, ARM7S_CORE_ID, 0)) &&
2677                     !(si_setcore(bus->sih, ARMCM3_CORE_ID, 0))) {
2678                         DHD_ERROR(("%s: Failed to find ARM core!\n", __func__));
2679                         bcmerror = BCME_ERROR;
2680                         goto fail;
2681                 }
2682
2683                 si_core_reset(bus->sih, 0, 0);
2684                 if (bcmsdh_regfail(bus->sdh)) {
2685                         DHD_ERROR(("%s: Failure trying to reset ARM core?\n",
2686                                    __func__));
2687                         bcmerror = BCME_SDIO_ERROR;
2688                         goto fail;
2689                 }
2690
2691                 /* Allow HT Clock now that the ARM is running. */
2692                 bus->alp_only = false;
2693
2694                 bus->dhd->busstate = DHD_BUS_LOAD;
2695         }
2696
2697 fail:
2698         /* Always return to SDIOD core */
2699         if (!si_setcore(bus->sih, PCMCIA_CORE_ID, 0))
2700                 si_setcore(bus->sih, SDIOD_CORE_ID, 0);
2701
2702         return bcmerror;
2703 }
2704
2705 int
2706 dhd_bus_iovar_op(dhd_pub_t *dhdp, const char *name,
2707                  void *params, int plen, void *arg, int len, bool set)
2708 {
2709         dhd_bus_t *bus = dhdp->bus;
2710         const bcm_iovar_t *vi = NULL;
2711         int bcmerror = 0;
2712         int val_size;
2713         u32 actionid;
2714
2715         DHD_TRACE(("%s: Enter\n", __func__));
2716
2717         ASSERT(name);
2718         ASSERT(len >= 0);
2719
2720         /* Get MUST have return space */
2721         ASSERT(set || (arg && len));
2722
2723         /* Set does NOT take qualifiers */
2724         ASSERT(!set || (!params && !plen));
2725
2726         /* Look up var locally; if not found pass to host driver */
2727         vi = bcm_iovar_lookup(dhdsdio_iovars, name);
2728         if (vi == NULL) {
2729                 dhd_os_sdlock(bus->dhd);
2730
2731                 BUS_WAKE(bus);
2732
2733                 /* Turn on clock in case SD command needs backplane */
2734                 dhdsdio_clkctl(bus, CLK_AVAIL, false);
2735
2736                 bcmerror =
2737                     bcmsdh_iovar_op(bus->sdh, name, params, plen, arg, len,
2738                                     set);
2739
2740                 /* Check for bus configuration changes of interest */
2741
2742                 /* If it was divisor change, read the new one */
2743                 if (set && strcmp(name, "sd_divisor") == 0) {
2744                         if (bcmsdh_iovar_op(bus->sdh, "sd_divisor", NULL, 0,
2745                                             &bus->sd_divisor, sizeof(s32),
2746                                             false) != BCME_OK) {
2747                                 bus->sd_divisor = -1;
2748                                 DHD_ERROR(("%s: fail on %s get\n", __func__,
2749                                            name));
2750                         } else {
2751                                 DHD_INFO(("%s: noted %s update, value now %d\n",
2752                                           __func__, name, bus->sd_divisor));
2753                         }
2754                 }
2755                 /* If it was a mode change, read the new one */
2756                 if (set && strcmp(name, "sd_mode") == 0) {
2757                         if (bcmsdh_iovar_op(bus->sdh, "sd_mode", NULL, 0,
2758                                             &bus->sd_mode, sizeof(s32),
2759                                             false) != BCME_OK) {
2760                                 bus->sd_mode = -1;
2761                                 DHD_ERROR(("%s: fail on %s get\n", __func__,
2762                                            name));
2763                         } else {
2764                                 DHD_INFO(("%s: noted %s update, value now %d\n",
2765                                           __func__, name, bus->sd_mode));
2766                         }
2767                 }
2768                 /* Similar check for blocksize change */
2769                 if (set && strcmp(name, "sd_blocksize") == 0) {
2770                         s32 fnum = 2;
2771                         if (bcmsdh_iovar_op
2772                             (bus->sdh, "sd_blocksize", &fnum, sizeof(s32),
2773                              &bus->blocksize, sizeof(s32),
2774                              false) != BCME_OK) {
2775                                 bus->blocksize = 0;
2776                                 DHD_ERROR(("%s: fail on %s get\n", __func__,
2777                                            "sd_blocksize"));
2778                         } else {
2779                                 DHD_INFO(("%s: noted %s update, value now %d\n",
2780                                           __func__, "sd_blocksize",
2781                                           bus->blocksize));
2782                         }
2783                 }
2784                 bus->roundup = min(max_roundup, bus->blocksize);
2785
2786                 if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
2787                         bus->activity = false;
2788                         dhdsdio_clkctl(bus, CLK_NONE, true);
2789                 }
2790
2791                 dhd_os_sdunlock(bus->dhd);
2792                 goto exit;
2793         }
2794
2795         DHD_CTL(("%s: %s %s, len %d plen %d\n", __func__,
2796                  name, (set ? "set" : "get"), len, plen));
2797
2798         /* set up 'params' pointer in case this is a set command so that
2799          * the convenience int and bool code can be common to set and get
2800          */
2801         if (params == NULL) {
2802                 params = arg;
2803                 plen = len;
2804         }
2805
2806         if (vi->type == IOVT_VOID)
2807                 val_size = 0;
2808         else if (vi->type == IOVT_BUFFER)
2809                 val_size = len;
2810         else
2811                 /* all other types are integer sized */
2812                 val_size = sizeof(int);
2813
2814         actionid = set ? IOV_SVAL(vi->varid) : IOV_GVAL(vi->varid);
2815         bcmerror =
2816             dhdsdio_doiovar(bus, vi, actionid, name, params, plen, arg, len,
2817                             val_size);
2818
2819 exit:
2820         return bcmerror;
2821 }
2822
2823 void dhd_bus_stop(struct dhd_bus *bus, bool enforce_mutex)
2824 {
2825         osl_t *osh = bus->dhd->osh;
2826         u32 local_hostintmask;
2827         u8 saveclk;
2828         uint retries;
2829         int err;
2830
2831         DHD_TRACE(("%s: Enter\n", __func__));
2832
2833         if (enforce_mutex)
2834                 dhd_os_sdlock(bus->dhd);
2835
2836         BUS_WAKE(bus);
2837
2838         /* Enable clock for device interrupts */
2839         dhdsdio_clkctl(bus, CLK_AVAIL, false);
2840
2841         /* Disable and clear interrupts at the chip level also */
2842         W_SDREG(0, &bus->regs->hostintmask, retries);
2843         local_hostintmask = bus->hostintmask;
2844         bus->hostintmask = 0;
2845
2846         /* Change our idea of bus state */
2847         bus->dhd->busstate = DHD_BUS_DOWN;
2848
2849         /* Force clocks on backplane to be sure F2 interrupt propagates */
2850         saveclk =
2851             bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2852                             &err);
2853         if (!err) {
2854                 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2855                                  (saveclk | SBSDIO_FORCE_HT), &err);
2856         }
2857         if (err) {
2858                 DHD_ERROR(("%s: Failed to force clock for F2: err %d\n",
2859                            __func__, err));
2860         }
2861
2862         /* Turn off the bus (F2), free any pending packets */
2863         DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
2864         bcmsdh_intr_disable(bus->sdh);
2865         bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN,
2866                          SDIO_FUNC_ENABLE_1, NULL);
2867
2868         /* Clear any pending interrupts now that F2 is disabled */
2869         W_SDREG(local_hostintmask, &bus->regs->intstatus, retries);
2870
2871         /* Turn off the backplane clock (only) */
2872         dhdsdio_clkctl(bus, CLK_SDONLY, false);
2873
2874         /* Clear the data packet queues */
2875         pktq_flush(osh, &bus->txq, true);
2876
2877         /* Clear any held glomming stuff */
2878         if (bus->glomd)
2879                 PKTFREE(osh, bus->glomd, false);
2880
2881         if (bus->glom)
2882                 PKTFREE(osh, bus->glom, false);
2883
2884         bus->glom = bus->glomd = NULL;
2885
2886         /* Clear rx control and wake any waiters */
2887         bus->rxlen = 0;
2888         dhd_os_ioctl_resp_wake(bus->dhd);
2889
2890         /* Reset some F2 state stuff */
2891         bus->rxskip = false;
2892         bus->tx_seq = bus->rx_seq = 0;
2893
2894         if (enforce_mutex)
2895                 dhd_os_sdunlock(bus->dhd);
2896 }
2897
2898 int dhd_bus_init(dhd_pub_t *dhdp, bool enforce_mutex)
2899 {
2900         dhd_bus_t *bus = dhdp->bus;
2901         dhd_timeout_t tmo;
2902         uint retries = 0;
2903         u8 ready, enable;
2904         int err, ret = 0;
2905         u8 saveclk;
2906
2907         DHD_TRACE(("%s: Enter\n", __func__));
2908
2909         ASSERT(bus->dhd);
2910         if (!bus->dhd)
2911                 return 0;
2912
2913         if (enforce_mutex)
2914                 dhd_os_sdlock(bus->dhd);
2915
2916         /* Make sure backplane clock is on, needed to generate F2 interrupt */
2917         dhdsdio_clkctl(bus, CLK_AVAIL, false);
2918         if (bus->clkstate != CLK_AVAIL)
2919                 goto exit;
2920
2921         /* Force clocks on backplane to be sure F2 interrupt propagates */
2922         saveclk =
2923             bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2924                             &err);
2925         if (!err) {
2926                 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2927                                  (saveclk | SBSDIO_FORCE_HT), &err);
2928         }
2929         if (err) {
2930                 DHD_ERROR(("%s: Failed to force clock for F2: err %d\n",
2931                            __func__, err));
2932                 goto exit;
2933         }
2934
2935         /* Enable function 2 (frame transfers) */
2936         W_SDREG((SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT),
2937                 &bus->regs->tosbmailboxdata, retries);
2938         enable = (SDIO_FUNC_ENABLE_1 | SDIO_FUNC_ENABLE_2);
2939
2940         bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, enable, NULL);
2941
2942         /* Give the dongle some time to do its thing and set IOR2 */
2943         dhd_timeout_start(&tmo, DHD_WAIT_F2RDY * 1000);
2944
2945         ready = 0;
2946         while (ready != enable && !dhd_timeout_expired(&tmo))
2947                 ready =
2948                     bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IORDY,
2949                                     NULL);
2950
2951         DHD_INFO(("%s: enable 0x%02x, ready 0x%02x (waited %uus)\n",
2952                   __func__, enable, ready, tmo.elapsed));
2953
2954         /* If F2 successfully enabled, set core and enable interrupts */
2955         if (ready == enable) {
2956                 /* Make sure we're talking to the core. */
2957                 bus->regs = si_setcore(bus->sih, PCMCIA_CORE_ID, 0);
2958                 if (!(bus->regs))
2959                         bus->regs = si_setcore(bus->sih, SDIOD_CORE_ID, 0);
2960
2961                 /* Set up the interrupt mask and enable interrupts */
2962                 bus->hostintmask = HOSTINTMASK;
2963                 W_SDREG(bus->hostintmask, &bus->regs->hostintmask, retries);
2964
2965                 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_WATERMARK,
2966                                  (u8) watermark, &err);
2967
2968                 /* Set bus state according to enable result */
2969                 dhdp->busstate = DHD_BUS_DATA;
2970
2971                 /* bcmsdh_intr_unmask(bus->sdh); */
2972
2973                 bus->intdis = false;
2974                 if (bus->intr) {
2975                         DHD_INTR(("%s: enable SDIO device interrupts\n",
2976                                   __func__));
2977                         bcmsdh_intr_enable(bus->sdh);
2978                 } else {
2979                         DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
2980                         bcmsdh_intr_disable(bus->sdh);
2981                 }
2982
2983         }
2984
2985         else {
2986                 /* Disable F2 again */
2987                 enable = SDIO_FUNC_ENABLE_1;
2988                 bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, enable,
2989                                  NULL);
2990         }
2991
2992         /* Restore previous clock setting */
2993         bcmsdh_cfg_write(bus->sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
2994                          saveclk, &err);
2995
2996         /* If we didn't come up, turn off backplane clock */
2997         if (dhdp->busstate != DHD_BUS_DATA)
2998                 dhdsdio_clkctl(bus, CLK_NONE, false);
2999
3000 exit:
3001         if (enforce_mutex)
3002                 dhd_os_sdunlock(bus->dhd);
3003
3004         return ret;
3005 }
3006
3007 static void dhdsdio_rxfail(dhd_bus_t *bus, bool abort, bool rtx)
3008 {
3009         bcmsdh_info_t *sdh = bus->sdh;
3010         sdpcmd_regs_t *regs = bus->regs;
3011         uint retries = 0;
3012         u16 lastrbc;
3013         u8 hi, lo;
3014         int err;
3015
3016         DHD_ERROR(("%s: %sterminate frame%s\n", __func__,
3017                    (abort ? "abort command, " : ""),
3018                    (rtx ? ", send NAK" : "")));
3019
3020         if (abort)
3021                 bcmsdh_abort(sdh, SDIO_FUNC_2);
3022
3023         bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_FRAMECTRL, SFC_RF_TERM,
3024                          &err);
3025         bus->f1regdata++;
3026
3027         /* Wait until the packet has been flushed (device/FIFO stable) */
3028         for (lastrbc = retries = 0xffff; retries > 0; retries--) {
3029                 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_RFRAMEBCHI,
3030                                      NULL);
3031                 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_RFRAMEBCLO,
3032                                      NULL);
3033                 bus->f1regdata += 2;
3034
3035                 if ((hi == 0) && (lo == 0))
3036                         break;
3037
3038                 if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
3039                         DHD_ERROR(("%s: count growing: last 0x%04x now "
3040                                 "0x%04x\n",
3041                                 __func__, lastrbc, ((hi << 8) + lo)));
3042                 }
3043                 lastrbc = (hi << 8) + lo;
3044         }
3045
3046         if (!retries) {
3047                 DHD_ERROR(("%s: count never zeroed: last 0x%04x\n",
3048                            __func__, lastrbc));
3049         } else {
3050                 DHD_INFO(("%s: flush took %d iterations\n", __func__,
3051                           (0xffff - retries)));
3052         }
3053
3054         if (rtx) {
3055                 bus->rxrtx++;
3056                 W_SDREG(SMB_NAK, &regs->tosbmailbox, retries);
3057                 bus->f1regdata++;
3058                 if (retries <= retry_limit)
3059                         bus->rxskip = true;
3060         }
3061
3062         /* Clear partial in any case */
3063         bus->nextlen = 0;
3064
3065         /* If we can't reach the device, signal failure */
3066         if (err || bcmsdh_regfail(sdh))
3067                 bus->dhd->busstate = DHD_BUS_DOWN;
3068 }
3069
3070 static void
3071 dhdsdio_read_control(dhd_bus_t *bus, u8 *hdr, uint len, uint doff)
3072 {
3073         bcmsdh_info_t *sdh = bus->sdh;
3074         uint rdlen, pad;
3075
3076         int sdret;
3077
3078         DHD_TRACE(("%s: Enter\n", __func__));
3079
3080         /* Control data already received in aligned rxctl */
3081         if ((bus->bus == SPI_BUS) && (!bus->usebufpool))
3082                 goto gotpkt;
3083
3084         ASSERT(bus->rxbuf);
3085         /* Set rxctl for frame (w/optional alignment) */
3086         bus->rxctl = bus->rxbuf;
3087         if (dhd_alignctl) {
3088                 bus->rxctl += firstread;
3089                 pad = ((unsigned long)bus->rxctl % DHD_SDALIGN);
3090                 if (pad)
3091                         bus->rxctl += (DHD_SDALIGN - pad);
3092                 bus->rxctl -= firstread;
3093         }
3094         ASSERT(bus->rxctl >= bus->rxbuf);
3095
3096         /* Copy the already-read portion over */
3097         bcopy(hdr, bus->rxctl, firstread);
3098         if (len <= firstread)
3099                 goto gotpkt;
3100
3101         /* Copy the full data pkt in gSPI case and process ioctl. */
3102         if (bus->bus == SPI_BUS) {
3103                 bcopy(hdr, bus->rxctl, len);
3104                 goto gotpkt;
3105         }
3106
3107         /* Raise rdlen to next SDIO block to avoid tail command */
3108         rdlen = len - firstread;
3109         if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
3110                 pad = bus->blocksize - (rdlen % bus->blocksize);
3111                 if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
3112                     ((len + pad) < bus->dhd->maxctl))
3113                         rdlen += pad;
3114         } else if (rdlen % DHD_SDALIGN) {
3115                 rdlen += DHD_SDALIGN - (rdlen % DHD_SDALIGN);
3116         }
3117
3118         /* Satisfy length-alignment requirements */
3119         if (forcealign && (rdlen & (ALIGNMENT - 1)))
3120                 rdlen = roundup(rdlen, ALIGNMENT);
3121
3122         /* Drop if the read is too big or it exceeds our maximum */
3123         if ((rdlen + firstread) > bus->dhd->maxctl) {
3124                 DHD_ERROR(("%s: %d-byte control read exceeds %d-byte buffer\n",
3125                            __func__, rdlen, bus->dhd->maxctl));
3126                 bus->dhd->rx_errors++;
3127                 dhdsdio_rxfail(bus, false, false);
3128                 goto done;
3129         }
3130
3131         if ((len - doff) > bus->dhd->maxctl) {
3132                 DHD_ERROR(("%s: %d-byte ctl frame (%d-byte ctl data) exceeds "
3133                         "%d-byte limit\n",
3134                         __func__, len, (len - doff), bus->dhd->maxctl));
3135                 bus->dhd->rx_errors++;
3136                 bus->rx_toolong++;
3137                 dhdsdio_rxfail(bus, false, false);
3138                 goto done;
3139         }
3140
3141         /* Read remainder of frame body into the rxctl buffer */
3142         sdret =
3143             dhd_bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2, F2SYNC,
3144                                 (bus->rxctl + firstread), rdlen, NULL, NULL,
3145                                 NULL);
3146         bus->f2rxdata++;
3147         ASSERT(sdret != BCME_PENDING);
3148
3149         /* Control frame failures need retransmission */
3150         if (sdret < 0) {
3151                 DHD_ERROR(("%s: read %d control bytes failed: %d\n",
3152                            __func__, rdlen, sdret));
3153                 bus->rxc_errors++;      /* dhd.rx_ctlerrs is higher level */
3154                 dhdsdio_rxfail(bus, true, true);
3155                 goto done;
3156         }
3157
3158 gotpkt:
3159
3160 #ifdef DHD_DEBUG
3161         if (DHD_BYTES_ON() && DHD_CTL_ON())
3162                 prhex("RxCtrl", bus->rxctl, len);
3163 #endif
3164
3165         /* Point to valid data and indicate its length */
3166         bus->rxctl += doff;
3167         bus->rxlen = len - doff;
3168
3169 done:
3170         /* Awake any waiters */
3171         dhd_os_ioctl_resp_wake(bus->dhd);
3172 }
3173
3174 static u8 dhdsdio_rxglom(dhd_bus_t *bus, u8 rxseq)
3175 {
3176         u16 dlen, totlen;
3177         u8 *dptr, num = 0;
3178
3179         u16 sublen, check;
3180         void *pfirst, *plast, *pnext, *save_pfirst;
3181         osl_t *osh = bus->dhd->osh;
3182
3183         int errcode;
3184         u8 chan, seq, doff, sfdoff;
3185         u8 txmax;
3186
3187         int ifidx = 0;
3188         bool usechain = bus->use_rxchain;
3189
3190         /* If packets, issue read(s) and send up packet chain */
3191         /* Return sequence numbers consumed? */
3192
3193         DHD_TRACE(("dhdsdio_rxglom: start: glomd %p glom %p\n", bus->glomd,
3194                    bus->glom));
3195
3196         /* If there's a descriptor, generate the packet chain */
3197         if (bus->glomd) {
3198                 dhd_os_sdlock_rxq(bus->dhd);
3199
3200                 pfirst = plast = pnext = NULL;
3201                 dlen = (u16) PKTLEN(bus->glomd);
3202                 dptr = PKTDATA(bus->glomd);
3203                 if (!dlen || (dlen & 1)) {
3204                         DHD_ERROR(("%s: bad glomd len(%d), ignore descriptor\n",
3205                         __func__, dlen));
3206                         dlen = 0;
3207                 }
3208
3209                 for (totlen = num = 0; dlen; num++) {
3210                         /* Get (and move past) next length */
3211                         sublen = ltoh16_ua(dptr);
3212                         dlen -= sizeof(u16);
3213                         dptr += sizeof(u16);
3214                         if ((sublen < SDPCM_HDRLEN) ||
3215                             ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
3216                                 DHD_ERROR(("%s: descriptor len %d bad: %d\n",
3217                                            __func__, num, sublen));
3218                                 pnext = NULL;
3219                                 break;
3220                         }
3221                         if (sublen % DHD_SDALIGN) {
3222                                 DHD_ERROR(("%s: sublen %d not multiple of %d\n",
3223                                 __func__, sublen, DHD_SDALIGN));
3224                                 usechain = false;
3225                         }
3226                         totlen += sublen;
3227
3228                         /* For last frame, adjust read len so total
3229                                  is a block multiple */
3230                         if (!dlen) {
3231                                 sublen +=
3232                                     (roundup(totlen, bus->blocksize) - totlen);
3233                                 totlen = roundup(totlen, bus->blocksize);
3234                         }
3235
3236                         /* Allocate/chain packet for next subframe */
3237                         pnext = PKTGET(osh, sublen + DHD_SDALIGN, false);
3238                         if (pnext == NULL) {
3239                                 DHD_ERROR(("%s: PKTGET failed, num %d len %d\n",
3240                                            __func__, num, sublen));
3241                                 break;
3242                         }
3243                         ASSERT(!PKTLINK(pnext));
3244                         if (!pfirst) {
3245                                 ASSERT(!plast);
3246                                 pfirst = plast = pnext;
3247                         } else {
3248                                 ASSERT(plast);
3249                                 PKTSETNEXT(plast, pnext);
3250                                 plast = pnext;
3251                         }
3252
3253                         /* Adhere to start alignment requirements */
3254                         PKTALIGN(osh, pnext, sublen, DHD_SDALIGN);
3255                 }
3256
3257                 /* If all allocations succeeded, save packet chain
3258                          in bus structure */
3259                 if (pnext) {
3260                         DHD_GLOM(("%s: allocated %d-byte packet chain for %d "
3261                                 "subframes\n", __func__, totlen, num));
3262                         if (DHD_GLOM_ON() && bus->nextlen) {
3263                                 if (totlen != bus->nextlen) {
3264                                         DHD_GLOM(("%s: glomdesc mismatch: nextlen %d glomdesc %d " "rxseq %d\n",
3265                                                 __func__, bus->nextlen,
3266                                                 totlen, rxseq));
3267                                 }
3268                         }
3269                         bus->glom = pfirst;
3270                         pfirst = pnext = NULL;
3271                 } else {
3272                         if (pfirst)
3273                                 PKTFREE(osh, pfirst, false);
3274                         bus->glom = NULL;
3275                         num = 0;
3276                 }
3277
3278                 /* Done with descriptor packet */
3279                 PKTFREE(osh, bus->glomd, false);
3280                 bus->glomd = NULL;
3281                 bus->nextlen = 0;
3282
3283                 dhd_os_sdunlock_rxq(bus->dhd);
3284         }
3285
3286         /* Ok -- either we just generated a packet chain,
3287                  or had one from before */
3288         if (bus->glom) {
3289                 if (DHD_GLOM_ON()) {
3290                         DHD_GLOM(("%s: try superframe read, packet chain:\n",
3291                                 __func__));
3292                         for (pnext = bus->glom; pnext; pnext = PKTNEXT(pnext)) {
3293                                 DHD_GLOM(("    %p: %p len 0x%04x (%d)\n",
3294                                           pnext, (u8 *) PKTDATA(pnext),
3295                                           PKTLEN(pnext), PKTLEN(pnext)));
3296                         }
3297                 }
3298
3299                 pfirst = bus->glom;
3300                 dlen = (u16) pkttotlen(osh, pfirst);
3301
3302                 /* Do an SDIO read for the superframe.  Configurable iovar to
3303                  * read directly into the chained packet, or allocate a large
3304                  * packet and and copy into the chain.
3305                  */
3306                 if (usechain) {
3307                         errcode = dhd_bcmsdh_recv_buf(bus,
3308                                                       bcmsdh_cur_sbwad
3309                                                       (bus->sdh), SDIO_FUNC_2,
3310                                                       F2SYNC,
3311                                                       (u8 *) PKTDATA(pfirst),
3312                                                       dlen, pfirst, NULL, NULL);
3313                 } else if (bus->dataptr) {
3314                         errcode = dhd_bcmsdh_recv_buf(bus,
3315                                                       bcmsdh_cur_sbwad
3316                                                       (bus->sdh), SDIO_FUNC_2,
3317                                                       F2SYNC, bus->dataptr,
3318                                                       dlen, NULL, NULL, NULL);
3319                         sublen =
3320                             (u16) pktfrombuf(osh, pfirst, 0, dlen,
3321                                                 bus->dataptr);
3322                         if (sublen != dlen) {
3323                                 DHD_ERROR(("%s: FAILED TO COPY, dlen %d sublen %d\n",
3324                                         __func__, dlen, sublen));
3325                                 errcode = -1;
3326                         }
3327                         pnext = NULL;
3328                 } else {
3329                         DHD_ERROR(("COULDN'T ALLOC %d-BYTE GLOM, FORCE FAILURE\n",
3330                                 dlen));
3331                         errcode = -1;
3332                 }
3333                 bus->f2rxdata++;
3334                 ASSERT(errcode != BCME_PENDING);
3335
3336                 /* On failure, kill the superframe, allow a couple retries */
3337                 if (errcode < 0) {
3338                         DHD_ERROR(("%s: glom read of %d bytes failed: %d\n",
3339                                    __func__, dlen, errcode));
3340                         bus->dhd->rx_errors++;
3341
3342                         if (bus->glomerr++ < 3) {
3343                                 dhdsdio_rxfail(bus, true, true);
3344                         } else {
3345                                 bus->glomerr = 0;
3346                                 dhdsdio_rxfail(bus, true, false);
3347                                 dhd_os_sdlock_rxq(bus->dhd);
3348                                 PKTFREE(osh, bus->glom, false);
3349                                 dhd_os_sdunlock_rxq(bus->dhd);
3350                                 bus->rxglomfail++;
3351                                 bus->glom = NULL;
3352                         }
3353                         return 0;
3354                 }
3355 #ifdef DHD_DEBUG
3356                 if (DHD_GLOM_ON()) {
3357                         prhex("SUPERFRAME", PKTDATA(pfirst),
3358                               min_t(int, PKTLEN(pfirst), 48));
3359                 }
3360 #endif
3361
3362                 /* Validate the superframe header */
3363                 dptr = (u8 *) PKTDATA(pfirst);
3364                 sublen = ltoh16_ua(dptr);
3365                 check = ltoh16_ua(dptr + sizeof(u16));
3366
3367                 chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3368                 seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
3369                 bus->nextlen = dptr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
3370                 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
3371                         DHD_INFO(("%s: nextlen too large (%d) seq %d\n",
3372                                 __func__, bus->nextlen, seq));
3373                         bus->nextlen = 0;
3374                 }
3375                 doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3376                 txmax = SDPCM_WINDOW_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3377
3378                 errcode = 0;
3379                 if ((u16)~(sublen ^ check)) {
3380                         DHD_ERROR(("%s (superframe): HW hdr error: len/check "
3381                                 "0x%04x/0x%04x\n", __func__, sublen, check));
3382                         errcode = -1;
3383                 } else if (roundup(sublen, bus->blocksize) != dlen) {
3384                         DHD_ERROR(("%s (superframe): len 0x%04x, rounded "
3385                                 "0x%04x, expect 0x%04x\n",
3386                                 __func__, sublen,
3387                                 roundup(sublen, bus->blocksize), dlen));
3388                         errcode = -1;
3389                 } else if (SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]) !=
3390                            SDPCM_GLOM_CHANNEL) {
3391                         DHD_ERROR(("%s (superframe): bad channel %d\n",
3392                                    __func__,
3393                                    SDPCM_PACKET_CHANNEL(&dptr
3394                                                         [SDPCM_FRAMETAG_LEN])));
3395                         errcode = -1;
3396                 } else if (SDPCM_GLOMDESC(&dptr[SDPCM_FRAMETAG_LEN])) {
3397                         DHD_ERROR(("%s (superframe): got second descriptor?\n",
3398                                    __func__));
3399                         errcode = -1;
3400                 } else if ((doff < SDPCM_HDRLEN) ||
3401                            (doff > (PKTLEN(pfirst) - SDPCM_HDRLEN))) {
3402                         DHD_ERROR(("%s (superframe): Bad data offset %d: HW %d "
3403                                 "pkt %d min %d\n",
3404                                 __func__, doff, sublen,
3405                                 PKTLEN(pfirst), SDPCM_HDRLEN));
3406                         errcode = -1;
3407                 }
3408
3409                 /* Check sequence number of superframe SW header */
3410                 if (rxseq != seq) {
3411                         DHD_INFO(("%s: (superframe) rx_seq %d, expected %d\n",
3412                                   __func__, seq, rxseq));
3413                         bus->rx_badseq++;
3414                         rxseq = seq;
3415                 }
3416
3417                 /* Check window for sanity */
3418                 if ((u8) (txmax - bus->tx_seq) > 0x40) {
3419                         DHD_ERROR(("%s: unlikely tx max %d with tx_seq %d\n",
3420                                 __func__, txmax, bus->tx_seq));
3421                         txmax = bus->tx_seq + 2;
3422                 }
3423                 bus->tx_max = txmax;
3424
3425                 /* Remove superframe header, remember offset */
3426                 PKTPULL(pfirst, doff);
3427                 sfdoff = doff;
3428
3429                 /* Validate all the subframe headers */
3430                 for (num = 0, pnext = pfirst; pnext && !errcode;
3431                      num++, pnext = PKTNEXT(pnext)) {
3432                         dptr = (u8 *) PKTDATA(pnext);
3433                         dlen = (u16) PKTLEN(pnext);
3434                         sublen = ltoh16_ua(dptr);
3435                         check = ltoh16_ua(dptr + sizeof(u16));
3436                         chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3437                         doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3438 #ifdef DHD_DEBUG
3439                         if (DHD_GLOM_ON())
3440                                 prhex("subframe", dptr, 32);
3441 #endif
3442
3443                         if ((u16)~(sublen ^ check)) {
3444                                 DHD_ERROR(("%s (subframe %d): HW hdr error: "
3445                                            "len/check 0x%04x/0x%04x\n",
3446                                            __func__, num, sublen, check));
3447                                 errcode = -1;
3448                         } else if ((sublen > dlen) || (sublen < SDPCM_HDRLEN)) {
3449                                 DHD_ERROR(("%s (subframe %d): length mismatch: "
3450                                            "len 0x%04x, expect 0x%04x\n",
3451                                            __func__, num, sublen, dlen));
3452                                 errcode = -1;
3453                         } else if ((chan != SDPCM_DATA_CHANNEL) &&
3454                                    (chan != SDPCM_EVENT_CHANNEL)) {
3455                                 DHD_ERROR(("%s (subframe %d): bad channel %d\n",
3456                                            __func__, num, chan));
3457                                 errcode = -1;
3458                         } else if ((doff < SDPCM_HDRLEN) || (doff > sublen)) {
3459                                 DHD_ERROR(("%s (subframe %d): Bad data offset %d: HW %d min %d\n",
3460                                         __func__, num, doff, sublen,
3461                                         SDPCM_HDRLEN));
3462                                 errcode = -1;
3463                         }
3464                 }
3465
3466                 if (errcode) {
3467                         /* Terminate frame on error, request
3468                                  a couple retries */
3469                         if (bus->glomerr++ < 3) {
3470                                 /* Restore superframe header space */
3471                                 PKTPUSH(pfirst, sfdoff);
3472                                 dhdsdio_rxfail(bus, true, true);
3473                         } else {
3474                                 bus->glomerr = 0;
3475                                 dhdsdio_rxfail(bus, true, false);
3476                                 dhd_os_sdlock_rxq(bus->dhd);
3477                                 PKTFREE(osh, bus->glom, false);
3478                                 dhd_os_sdunlock_rxq(bus->dhd);
3479                                 bus->rxglomfail++;
3480                                 bus->glom = NULL;
3481                         }
3482                         bus->nextlen = 0;
3483                         return 0;
3484                 }
3485
3486                 /* Basic SD framing looks ok - process each packet (header) */
3487                 save_pfirst = pfirst;
3488                 bus->glom = NULL;
3489                 plast = NULL;
3490
3491                 dhd_os_sdlock_rxq(bus->dhd);
3492                 for (num = 0; pfirst; rxseq++, pfirst = pnext) {
3493                         pnext = PKTNEXT(pfirst);
3494                         PKTSETNEXT(pfirst, NULL);
3495
3496                         dptr = (u8 *) PKTDATA(pfirst);
3497                         sublen = ltoh16_ua(dptr);
3498                         chan = SDPCM_PACKET_CHANNEL(&dptr[SDPCM_FRAMETAG_LEN]);
3499                         seq = SDPCM_PACKET_SEQUENCE(&dptr[SDPCM_FRAMETAG_LEN]);
3500                         doff = SDPCM_DOFFSET_VALUE(&dptr[SDPCM_FRAMETAG_LEN]);
3501
3502                         DHD_GLOM(("%s: Get subframe %d, %p(%p/%d), sublen %d "
3503                                 "chan %d seq %d\n",
3504                                 __func__, num, pfirst, PKTDATA(pfirst),
3505                                 PKTLEN(pfirst), sublen, chan, seq));
3506
3507                         ASSERT((chan == SDPCM_DATA_CHANNEL)
3508                                || (chan == SDPCM_EVENT_CHANNEL));
3509
3510                         if (rxseq != seq) {
3511                                 DHD_GLOM(("%s: rx_seq %d, expected %d\n",
3512                                           __func__, seq, rxseq));
3513                                 bus->rx_badseq++;
3514                                 rxseq = seq;
3515                         }
3516 #ifdef DHD_DEBUG
3517                         if (DHD_BYTES_ON() && DHD_DATA_ON())
3518                                 prhex("Rx Subframe Data", dptr, dlen);
3519 #endif
3520
3521                         PKTSETLEN(pfirst, sublen);
3522                         PKTPULL(pfirst, doff);
3523
3524                         if (PKTLEN(pfirst) == 0) {
3525                                 PKTFREE(bus->dhd->osh, pfirst, false);
3526                                 if (plast) {
3527                                         PKTSETNEXT(plast, pnext);
3528                                 } else {
3529                                         ASSERT(save_pfirst == pfirst);
3530                                         save_pfirst = pnext;
3531                                 }
3532                                 continue;
3533                         } else if (dhd_prot_hdrpull(bus->dhd, &ifidx, pfirst) !=
3534                                    0) {
3535                                 DHD_ERROR(("%s: rx protocol error\n",
3536                                            __func__));
3537                                 bus->dhd->rx_errors++;
3538                                 PKTFREE(osh, pfirst, false);
3539                                 if (plast) {
3540                                         PKTSETNEXT(plast, pnext);
3541                                 } else {
3542                                         ASSERT(save_pfirst == pfirst);
3543                                         save_pfirst = pnext;
3544                                 }
3545                                 continue;
3546                         }
3547
3548                         /* this packet will go up, link back into
3549                                  chain and count it */
3550                         PKTSETNEXT(pfirst, pnext);
3551                         plast = pfirst;
3552                         num++;
3553
3554 #ifdef DHD_DEBUG
3555                         if (DHD_GLOM_ON()) {
3556                                 DHD_GLOM(("%s subframe %d to stack, %p(%p/%d) "
3557                                 "nxt/lnk %p/%p\n",
3558                                 __func__, num, pfirst, PKTDATA(pfirst),
3559                                 PKTLEN(pfirst), PKTNEXT(pfirst),
3560                                 PKTLINK(pfirst)));
3561                                 prhex("", (u8 *) PKTDATA(pfirst),
3562                                       min_t(int, PKTLEN(pfirst), 32));
3563                         }
3564 #endif                          /* DHD_DEBUG */
3565                 }
3566                 dhd_os_sdunlock_rxq(bus->dhd);
3567                 if (num) {
3568                         dhd_os_sdunlock(bus->dhd);
3569                         dhd_rx_frame(bus->dhd, ifidx, save_pfirst, num);
3570                         dhd_os_sdlock(bus->dhd);
3571                 }
3572
3573                 bus->rxglomframes++;
3574                 bus->rxglompkts += num;
3575         }
3576         return num;
3577 }
3578
3579 /* Return true if there may be more frames to read */
3580 static uint dhdsdio_readframes(dhd_bus_t *bus, uint maxframes, bool *finished)
3581 {
3582         osl_t *osh = bus->dhd->osh;
3583         bcmsdh_info_t *sdh = bus->sdh;
3584
3585         u16 len, check; /* Extracted hardware header fields */
3586         u8 chan, seq, doff;     /* Extracted software header fields */
3587         u8 fcbits;              /* Extracted fcbits from software header */
3588         u8 delta;
3589
3590         void *pkt;              /* Packet for event or data frames */
3591         u16 pad;                /* Number of pad bytes to read */
3592         u16 rdlen;              /* Total number of bytes to read */
3593         u8 rxseq;               /* Next sequence number to expect */
3594         uint rxleft = 0;        /* Remaining number of frames allowed */
3595         int sdret;              /* Return code from bcmsdh calls */
3596         u8 txmax;               /* Maximum tx sequence offered */
3597         bool len_consistent;    /* Result of comparing readahead len and
3598                                          len from hw-hdr */
3599         u8 *rxbuf;
3600         int ifidx = 0;
3601         uint rxcount = 0;       /* Total frames read */
3602
3603 #if defined(DHD_DEBUG) || defined(SDTEST)
3604         bool sdtest = false;    /* To limit message spew from test mode */
3605 #endif
3606
3607         DHD_TRACE(("%s: Enter\n", __func__));
3608
3609         ASSERT(maxframes);
3610
3611 #ifdef SDTEST
3612         /* Allow pktgen to override maxframes */
3613         if (bus->pktgen_count && (bus->pktgen_mode == DHD_PKTGEN_RECV)) {
3614                 maxframes = bus->pktgen_count;
3615                 sdtest = true;
3616         }
3617 #endif
3618
3619         /* Not finished unless we encounter no more frames indication */
3620         *finished = false;
3621
3622         for (rxseq = bus->rx_seq, rxleft = maxframes;
3623              !bus->rxskip && rxleft && bus->dhd->busstate != DHD_BUS_DOWN;
3624              rxseq++, rxleft--) {
3625
3626                 /* Handle glomming separately */
3627                 if (bus->glom || bus->glomd) {
3628                         u8 cnt;
3629                         DHD_GLOM(("%s: calling rxglom: glomd %p, glom %p\n",
3630                                   __func__, bus->glomd, bus->glom));
3631                         cnt = dhdsdio_rxglom(bus, rxseq);
3632                         DHD_GLOM(("%s: rxglom returned %d\n", __func__, cnt));
3633                         rxseq += cnt - 1;
3634                         rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
3635                         continue;
3636                 }
3637
3638                 /* Try doing single read if we can */
3639                 if (dhd_readahead && bus->nextlen) {
3640                         u16 nextlen = bus->nextlen;
3641                         bus->nextlen = 0;
3642
3643                         if (bus->bus == SPI_BUS) {
3644                                 rdlen = len = nextlen;
3645                         } else {
3646                                 rdlen = len = nextlen << 4;
3647
3648                                 /* Pad read to blocksize for efficiency */
3649                                 if (bus->roundup && bus->blocksize
3650                                     && (rdlen > bus->blocksize)) {
3651                                         pad =
3652                                             bus->blocksize -
3653                                             (rdlen % bus->blocksize);
3654                                         if ((pad <= bus->roundup)
3655                                             && (pad < bus->blocksize)
3656                                             && ((rdlen + pad + firstread) <
3657                                                 MAX_RX_DATASZ))
3658                                                 rdlen += pad;
3659                                 } else if (rdlen % DHD_SDALIGN) {
3660                                         rdlen +=
3661                                             DHD_SDALIGN - (rdlen % DHD_SDALIGN);
3662                                 }
3663                         }
3664
3665                         /* We use bus->rxctl buffer in WinXP for initial
3666                          * control pkt receives.
3667                          * Later we use buffer-poll for data as well
3668                          * as control packets.
3669                          * This is required becuase dhd receives full
3670                          * frame in gSPI unlike SDIO.
3671                          * After the frame is received we have to
3672                          * distinguish whether it is data
3673                          * or non-data frame.
3674                          */
3675                         /* Allocate a packet buffer */
3676                         dhd_os_sdlock_rxq(bus->dhd);
3677                         pkt = PKTGET(osh, rdlen + DHD_SDALIGN, false);
3678                         if (!pkt) {
3679                                 if (bus->bus == SPI_BUS) {
3680                                         bus->usebufpool = false;
3681                                         bus->rxctl = bus->rxbuf;
3682                                         if (dhd_alignctl) {
3683                                                 bus->rxctl += firstread;
3684                                                 pad = ((unsigned long)bus->rxctl %
3685                                                       DHD_SDALIGN);
3686                                                 if (pad)
3687                                                         bus->rxctl +=
3688                                                             (DHD_SDALIGN - pad);
3689                                                 bus->rxctl -= firstread;
3690                                         }
3691                                         ASSERT(bus->rxctl >= bus->rxbuf);
3692                                         rxbuf = bus->rxctl;
3693                                         /* Read the entire frame */
3694                                         sdret = dhd_bcmsdh_recv_buf(bus,
3695                                                     bcmsdh_cur_sbwad
3696                                                     (sdh),
3697                                                     SDIO_FUNC_2,
3698                                                     F2SYNC,
3699                                                     rxbuf,
3700                                                     rdlen, NULL,
3701                                                     NULL, NULL);
3702                                         bus->f2rxdata++;
3703                                         ASSERT(sdret != BCME_PENDING);
3704
3705                                         /* Control frame failures need
3706                                          retransmission */
3707                                         if (sdret < 0) {
3708                                                 DHD_ERROR(("%s: read %d control bytes failed: %d\n",
3709                                                         __func__,
3710                                                         rdlen, sdret));
3711                                                 /* dhd.rx_ctlerrs is higher */
3712                                                 bus->rxc_errors++;
3713                                                 dhd_os_sdunlock_rxq(bus->dhd);
3714                                                 dhdsdio_rxfail(bus, true,
3715                                                        (bus->bus ==
3716                                                         SPI_BUS) ? false
3717                                                        : true);
3718                                                 continue;
3719                                         }
3720                                 } else {
3721                                         /* Give up on data,
3722                                         request rtx of events */
3723                                         DHD_ERROR(("%s (nextlen): PKTGET failed: len %d rdlen %d " "expected rxseq %d\n",
3724                                                 __func__, len, rdlen, rxseq));
3725                                         /* Just go try again w/normal
3726                                         header read */
3727                                         dhd_os_sdunlock_rxq(bus->dhd);
3728                                         continue;
3729                                 }
3730                         } else {
3731                                 if (bus->bus == SPI_BUS)
3732                                         bus->usebufpool = true;
3733
3734                                 ASSERT(!PKTLINK(pkt));
3735                                 PKTALIGN(osh, pkt, rdlen, DHD_SDALIGN);
3736                                 rxbuf = (u8 *) PKTDATA(pkt);
3737                                 /* Read the entire frame */
3738                                 sdret =
3739                                     dhd_bcmsdh_recv_buf(bus,
3740                                                 bcmsdh_cur_sbwad(sdh),
3741                                                 SDIO_FUNC_2, F2SYNC,
3742                                                 rxbuf, rdlen, pkt, NULL,
3743                                                 NULL);
3744                                 bus->f2rxdata++;
3745                                 ASSERT(sdret != BCME_PENDING);
3746
3747                                 if (sdret < 0) {
3748                                         DHD_ERROR(("%s (nextlen): read %d bytes failed: %d\n",
3749                                                 __func__, rdlen, sdret));
3750                                         PKTFREE(bus->dhd->osh, pkt, false);
3751                                         bus->dhd->rx_errors++;
3752                                         dhd_os_sdunlock_rxq(bus->dhd);
3753                                         /* Force retry w/normal header read.
3754                                          * Don't attemp NAK for
3755                                          * gSPI
3756                                          */
3757                                         dhdsdio_rxfail(bus, true,
3758                                                        (bus->bus ==
3759                                                         SPI_BUS) ? false :
3760                                                        true);
3761                                         continue;
3762                                 }
3763                         }
3764                         dhd_os_sdunlock_rxq(bus->dhd);
3765
3766                         /* Now check the header */
3767                         bcopy(rxbuf, bus->rxhdr, SDPCM_HDRLEN);
3768
3769                         /* Extract hardware header fields */
3770                         len = ltoh16_ua(bus->rxhdr);
3771                         check = ltoh16_ua(bus->rxhdr + sizeof(u16));
3772
3773                         /* All zeros means readahead info was bad */
3774                         if (!(len | check)) {
3775                                 DHD_INFO(("%s (nextlen): read zeros in HW "
3776                                         "header???\n", __func__));
3777                                 dhd_os_sdlock_rxq(bus->dhd);
3778                                 PKTFREE2();
3779                                 dhd_os_sdunlock_rxq(bus->dhd);
3780                                 GSPI_PR55150_BAILOUT;
3781                                 continue;
3782                         }
3783
3784                         /* Validate check bytes */
3785                         if ((u16)~(len ^ check)) {
3786                                 DHD_ERROR(("%s (nextlen): HW hdr error: nextlen/len/check" " 0x%04x/0x%04x/0x%04x\n",
3787                                         __func__, nextlen, len, check));
3788                                 dhd_os_sdlock_rxq(bus->dhd);
3789                                 PKTFREE2();
3790                                 dhd_os_sdunlock_rxq(bus->dhd);
3791                                 bus->rx_badhdr++;
3792                                 dhdsdio_rxfail(bus, false, false);
3793                                 GSPI_PR55150_BAILOUT;
3794                                 continue;
3795                         }
3796
3797                         /* Validate frame length */
3798                         if (len < SDPCM_HDRLEN) {
3799                                 DHD_ERROR(("%s (nextlen): HW hdr length "
3800                                         "invalid: %d\n", __func__, len));
3801                                 dhd_os_sdlock_rxq(bus->dhd);
3802                                 PKTFREE2();
3803                                 dhd_os_sdunlock_rxq(bus->dhd);
3804                                 GSPI_PR55150_BAILOUT;
3805                                 continue;
3806                         }
3807
3808                         /* Check for consistency withreadahead info */
3809                         len_consistent = (nextlen != (roundup(len, 16) >> 4));
3810                         if (len_consistent) {
3811                                 /* Mismatch, force retry w/normal
3812                                         header (may be >4K) */
3813                                 DHD_ERROR(("%s (nextlen): mismatch, nextlen %d len %d rnd %d; " "expected rxseq %d\n",
3814                                         __func__, nextlen,
3815                                         len, roundup(len, 16), rxseq));
3816                                 dhd_os_sdlock_rxq(bus->dhd);
3817                                 PKTFREE2();
3818                                 dhd_os_sdunlock_rxq(bus->dhd);
3819                                 dhdsdio_rxfail(bus, true,
3820                                                (bus->bus ==
3821                                                 SPI_BUS) ? false : true);
3822                                 GSPI_PR55150_BAILOUT;
3823                                 continue;
3824                         }
3825
3826                         /* Extract software header fields */
3827                         chan =
3828                             SDPCM_PACKET_CHANNEL(&bus->rxhdr
3829                                                  [SDPCM_FRAMETAG_LEN]);
3830                         seq =
3831                             SDPCM_PACKET_SEQUENCE(&bus->rxhdr
3832                                                   [SDPCM_FRAMETAG_LEN]);
3833                         doff =
3834                             SDPCM_DOFFSET_VALUE(&bus->rxhdr
3835                                                 [SDPCM_FRAMETAG_LEN]);
3836                         txmax =
3837                             SDPCM_WINDOW_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3838
3839                         bus->nextlen =
3840                             bus->rxhdr[SDPCM_FRAMETAG_LEN +
3841                                        SDPCM_NEXTLEN_OFFSET];
3842                         if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
3843                                 DHD_INFO(("%s (nextlen): got frame w/nextlen too large" " (%d), seq %d\n",
3844                                         __func__, bus->nextlen, seq));
3845                                 bus->nextlen = 0;
3846                         }
3847
3848                         bus->dhd->rx_readahead_cnt++;
3849                         /* Handle Flow Control */
3850                         fcbits =
3851                             SDPCM_FCMASK_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3852
3853                         delta = 0;
3854                         if (~bus->flowcontrol & fcbits) {
3855                                 bus->fc_xoff++;
3856                                 delta = 1;
3857                         }
3858                         if (bus->flowcontrol & ~fcbits) {
3859                                 bus->fc_xon++;
3860                                 delta = 1;
3861                         }
3862
3863                         if (delta) {
3864                                 bus->fc_rcvd++;
3865                                 bus->flowcontrol = fcbits;
3866                         }
3867
3868                         /* Check and update sequence number */
3869                         if (rxseq != seq) {
3870                                 DHD_INFO(("%s (nextlen): rx_seq %d, expected "
3871                                         "%d\n", __func__, seq, rxseq));
3872                                 bus->rx_badseq++;
3873                                 rxseq = seq;
3874                         }
3875
3876                         /* Check window for sanity */
3877                         if ((u8) (txmax - bus->tx_seq) > 0x40) {
3878                                 DHD_ERROR(("%s: got unlikely tx max %d with "
3879                                         "tx_seq %d\n",
3880                                         __func__, txmax, bus->tx_seq));
3881                                 txmax = bus->tx_seq + 2;
3882                         }
3883                         bus->tx_max = txmax;
3884
3885 #ifdef DHD_DEBUG
3886                         if (DHD_BYTES_ON() && DHD_DATA_ON())
3887                                 prhex("Rx Data", rxbuf, len);
3888                         else if (DHD_HDRS_ON())
3889                                 prhex("RxHdr", bus->rxhdr, SDPCM_HDRLEN);
3890 #endif
3891
3892                         if (chan == SDPCM_CONTROL_CHANNEL) {
3893                                 if (bus->bus == SPI_BUS) {
3894                                         dhdsdio_read_control(bus, rxbuf, len,
3895                                                              doff);
3896                                         if (bus->usebufpool) {
3897                                                 dhd_os_sdlock_rxq(bus->dhd);
3898                                                 PKTFREE(bus->dhd->osh, pkt,
3899                                                         false);
3900                                                 dhd_os_sdunlock_rxq(bus->dhd);
3901                                         }
3902                                         continue;
3903                                 } else {
3904                                         DHD_ERROR(("%s (nextlen): readahead on control" " packet %d?\n",
3905                                                 __func__, seq));
3906                                         /* Force retry w/normal header read */
3907                                         bus->nextlen = 0;
3908                                         dhdsdio_rxfail(bus, false, true);
3909                                         dhd_os_sdlock_rxq(bus->dhd);
3910                                         PKTFREE2();
3911                                         dhd_os_sdunlock_rxq(bus->dhd);
3912                                         continue;
3913                                 }
3914                         }
3915
3916                         if ((bus->bus == SPI_BUS) && !bus->usebufpool) {
3917                                 DHD_ERROR(("Received %d bytes on %d channel. Running out of " "rx pktbuf's or not yet malloced.\n",
3918                                         len, chan));
3919                                 continue;
3920                         }
3921
3922                         /* Validate data offset */
3923                         if ((doff < SDPCM_HDRLEN) || (doff > len)) {
3924                                 DHD_ERROR(("%s (nextlen): bad data offset %d: HW len %d min %d\n",
3925                                         __func__, doff, len, SDPCM_HDRLEN));
3926                                 dhd_os_sdlock_rxq(bus->dhd);
3927                                 PKTFREE2();
3928                                 dhd_os_sdunlock_rxq(bus->dhd);
3929                                 ASSERT(0);
3930                                 dhdsdio_rxfail(bus, false, false);
3931                                 continue;
3932                         }
3933
3934                         /* All done with this one -- now deliver the packet */
3935                         goto deliver;
3936                 }
3937                 /* gSPI frames should not be handled in fractions */
3938                 if (bus->bus == SPI_BUS)
3939                         break;
3940
3941                 /* Read frame header (hardware and software) */
3942                 sdret =
3943                     dhd_bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
3944                                         F2SYNC, bus->rxhdr, firstread, NULL,
3945                                         NULL, NULL);
3946                 bus->f2rxhdrs++;
3947                 ASSERT(sdret != BCME_PENDING);
3948
3949                 if (sdret < 0) {
3950                         DHD_ERROR(("%s: RXHEADER FAILED: %d\n", __func__,
3951                                    sdret));
3952                         bus->rx_hdrfail++;
3953                         dhdsdio_rxfail(bus, true, true);
3954                         continue;
3955                 }
3956 #ifdef DHD_DEBUG
3957                 if (DHD_BYTES_ON() || DHD_HDRS_ON())
3958                         prhex("RxHdr", bus->rxhdr, SDPCM_HDRLEN);
3959 #endif
3960
3961                 /* Extract hardware header fields */
3962                 len = ltoh16_ua(bus->rxhdr);
3963                 check = ltoh16_ua(bus->rxhdr + sizeof(u16));
3964
3965                 /* All zeros means no more frames */
3966                 if (!(len | check)) {
3967                         *finished = true;
3968                         break;
3969                 }
3970
3971                 /* Validate check bytes */
3972                 if ((u16) ~(len ^ check)) {
3973                         DHD_ERROR(("%s: HW hdr err: len/check 0x%04x/0x%04x\n",
3974                                 __func__, len, check));
3975                         bus->rx_badhdr++;
3976                         dhdsdio_rxfail(bus, false, false);
3977                         continue;
3978                 }
3979
3980                 /* Validate frame length */
3981                 if (len < SDPCM_HDRLEN) {
3982                         DHD_ERROR(("%s: HW hdr length invalid: %d\n",
3983                                    __func__, len));
3984                         continue;
3985                 }
3986
3987                 /* Extract software header fields */
3988                 chan = SDPCM_PACKET_CHANNEL(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3989                 seq = SDPCM_PACKET_SEQUENCE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3990                 doff = SDPCM_DOFFSET_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3991                 txmax = SDPCM_WINDOW_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
3992
3993                 /* Validate data offset */
3994                 if ((doff < SDPCM_HDRLEN) || (doff > len)) {
3995                         DHD_ERROR(("%s: Bad data offset %d: HW len %d, min %d "
3996                                 "seq %d\n",
3997                                 __func__, doff, len, SDPCM_HDRLEN, seq));
3998                         bus->rx_badhdr++;
3999                         ASSERT(0);
4000                         dhdsdio_rxfail(bus, false, false);
4001                         continue;
4002                 }
4003
4004                 /* Save the readahead length if there is one */
4005                 bus->nextlen =
4006                     bus->rxhdr[SDPCM_FRAMETAG_LEN + SDPCM_NEXTLEN_OFFSET];
4007                 if ((bus->nextlen << 4) > MAX_RX_DATASZ) {
4008                         DHD_INFO(("%s (nextlen): got frame w/nextlen too large "
4009                                 "(%d), seq %d\n",
4010                                 __func__, bus->nextlen, seq));
4011                         bus->nextlen = 0;
4012                 }
4013
4014                 /* Handle Flow Control */
4015                 fcbits = SDPCM_FCMASK_VALUE(&bus->rxhdr[SDPCM_FRAMETAG_LEN]);
4016
4017                 delta = 0;
4018                 if (~bus->flowcontrol & fcbits) {
4019                         bus->fc_xoff++;
4020                         delta = 1;
4021                 }
4022                 if (bus->flowcontrol & ~fcbits) {
4023                         bus->fc_xon++;
4024                         delta = 1;
4025                 }
4026
4027                 if (delta) {
4028                         bus->fc_rcvd++;
4029                         bus->flowcontrol = fcbits;
4030                 }
4031
4032                 /* Check and update sequence number */
4033                 if (rxseq != seq) {
4034                         DHD_INFO(("%s: rx_seq %d, expected %d\n", __func__,
4035                                   seq, rxseq));
4036                         bus->rx_badseq++;
4037                         rxseq = seq;
4038                 }
4039
4040                 /* Check window for sanity */
4041                 if ((u8) (txmax - bus->tx_seq) > 0x40) {
4042                         DHD_ERROR(("%s: unlikely tx max %d with tx_seq %d\n",
4043                                 __func__, txmax, bus->tx_seq));
4044                         txmax = bus->tx_seq + 2;
4045                 }
4046                 bus->tx_max = txmax;
4047
4048                 /* Call a separate function for control frames */
4049                 if (chan == SDPCM_CONTROL_CHANNEL) {
4050                         dhdsdio_read_control(bus, bus->rxhdr, len, doff);
4051                         continue;
4052                 }
4053
4054                 ASSERT((chan == SDPCM_DATA_CHANNEL)
4055                        || (chan == SDPCM_EVENT_CHANNEL)
4056                        || (chan == SDPCM_TEST_CHANNEL)
4057                        || (chan == SDPCM_GLOM_CHANNEL));
4058
4059                 /* Length to read */
4060                 rdlen = (len > firstread) ? (len - firstread) : 0;
4061
4062                 /* May pad read to blocksize for efficiency */
4063                 if (bus->roundup && bus->blocksize &&
4064                         (rdlen > bus->blocksize)) {
4065                         pad = bus->blocksize - (rdlen % bus->blocksize);
4066                         if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
4067                             ((rdlen + pad + firstread) < MAX_RX_DATASZ))
4068                                 rdlen += pad;
4069                 } else if (rdlen % DHD_SDALIGN) {
4070                         rdlen += DHD_SDALIGN - (rdlen % DHD_SDALIGN);
4071                 }
4072
4073                 /* Satisfy length-alignment requirements */
4074                 if (forcealign && (rdlen & (ALIGNMENT - 1)))
4075                         rdlen = roundup(rdlen, ALIGNMENT);
4076
4077                 if ((rdlen + firstread) > MAX_RX_DATASZ) {
4078                         /* Too long -- skip this frame */
4079                         DHD_ERROR(("%s: too long: len %d rdlen %d\n",
4080                                    __func__, len, rdlen));
4081                         bus->dhd->rx_errors++;
4082                         bus->rx_toolong++;
4083                         dhdsdio_rxfail(bus, false, false);
4084                         continue;
4085                 }
4086
4087                 dhd_os_sdlock_rxq(bus->dhd);
4088                 pkt = PKTGET(osh, (rdlen + firstread + DHD_SDALIGN), false);
4089                 if (!pkt) {
4090                         /* Give up on data, request rtx of events */
4091                         DHD_ERROR(("%s: PKTGET failed: rdlen %d chan %d\n",
4092                                    __func__, rdlen, chan));
4093                         bus->dhd->rx_dropped++;
4094                         dhd_os_sdunlock_rxq(bus->dhd);
4095                         dhdsdio_rxfail(bus, false, RETRYCHAN(chan));
4096                         continue;
4097                 }
4098                 dhd_os_sdunlock_rxq(bus->dhd);
4099
4100                 ASSERT(!PKTLINK(pkt));
4101
4102                 /* Leave room for what we already read, and align remainder */
4103                 ASSERT(firstread < (PKTLEN(pkt)));
4104                 PKTPULL(pkt, firstread);
4105                 PKTALIGN(osh, pkt, rdlen, DHD_SDALIGN);
4106
4107                 /* Read the remaining frame data */
4108                 sdret =
4109                     dhd_bcmsdh_recv_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
4110                                         F2SYNC, ((u8 *) PKTDATA(pkt)), rdlen,
4111                                         pkt, NULL, NULL);
4112                 bus->f2rxdata++;
4113                 ASSERT(sdret != BCME_PENDING);
4114
4115                 if (sdret < 0) {
4116                         DHD_ERROR(("%s: read %d %s bytes failed: %d\n",
4117                                    __func__, rdlen,
4118                                    ((chan ==
4119                                      SDPCM_EVENT_CHANNEL) ? "event" : ((chan ==
4120                                         SDPCM_DATA_CHANNEL)
4121                                        ? "data" : "test")),
4122                                    sdret));
4123                         dhd_os_sdlock_rxq(bus->dhd);
4124                         PKTFREE(bus->dhd->osh, pkt, false);
4125                         dhd_os_sdunlock_rxq(bus->dhd);
4126                         bus->dhd->rx_errors++;
4127                         dhdsdio_rxfail(bus, true, RETRYCHAN(chan));
4128                         continue;
4129                 }
4130
4131                 /* Copy the already-read portion */
4132                 PKTPUSH(pkt, firstread);
4133                 bcopy(bus->rxhdr, PKTDATA(pkt), firstread);
4134
4135 #ifdef DHD_DEBUG
4136                 if (DHD_BYTES_ON() && DHD_DATA_ON())
4137                         prhex("Rx Data", PKTDATA(pkt), len);
4138 #endif
4139
4140 deliver:
4141                 /* Save superframe descriptor and allocate packet frame */
4142                 if (chan == SDPCM_GLOM_CHANNEL) {
4143                         if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_FRAMETAG_LEN])) {
4144                                 DHD_GLOM(("%s: glom descriptor, %d bytes:\n",
4145                                         __func__, len));
4146 #ifdef DHD_DEBUG
4147                                 if (DHD_GLOM_ON()) {
4148                                         prhex("Glom Data", PKTDATA(pkt), len);
4149                                 }
4150 #endif
4151                                 PKTSETLEN(pkt, len);
4152                                 ASSERT(doff == SDPCM_HDRLEN);
4153                                 PKTPULL(pkt, SDPCM_HDRLEN);
4154                                 bus->glomd = pkt;
4155                         } else {
4156                                 DHD_ERROR(("%s: glom superframe w/o "
4157                                         "descriptor!\n", __func__));
4158                                 dhdsdio_rxfail(bus, false, false);
4159                         }
4160                         continue;
4161                 }
4162
4163                 /* Fill in packet len and prio, deliver upward */
4164                 PKTSETLEN(pkt, len);
4165                 PKTPULL(pkt, doff);
4166
4167 #ifdef SDTEST
4168                 /* Test channel packets are processed separately */
4169                 if (chan == SDPCM_TEST_CHANNEL) {
4170                         dhdsdio_testrcv(bus, pkt, seq);
4171                         continue;
4172                 }
4173 #endif                          /* SDTEST */
4174
4175                 if (PKTLEN(pkt) == 0) {
4176                         dhd_os_sdlock_rxq(bus->dhd);
4177                         PKTFREE(bus->dhd->osh, pkt, false);
4178                         dhd_os_sdunlock_rxq(bus->dhd);
4179                         continue;
4180                 } else if (dhd_prot_hdrpull(bus->dhd, &ifidx, pkt) != 0) {
4181                         DHD_ERROR(("%s: rx protocol error\n", __func__));
4182                         dhd_os_sdlock_rxq(bus->dhd);
4183                         PKTFREE(bus->dhd->osh, pkt, false);
4184                         dhd_os_sdunlock_rxq(bus->dhd);
4185                         bus->dhd->rx_errors++;
4186                         continue;
4187                 }
4188
4189                 /* Unlock during rx call */
4190                 dhd_os_sdunlock(bus->dhd);
4191                 dhd_rx_frame(bus->dhd, ifidx, pkt, 1);
4192                 dhd_os_sdlock(bus->dhd);
4193         }
4194         rxcount = maxframes - rxleft;
4195 #ifdef DHD_DEBUG
4196         /* Message if we hit the limit */
4197         if (!rxleft && !sdtest)
4198                 DHD_DATA(("%s: hit rx limit of %d frames\n", __func__,
4199                           maxframes));
4200         else
4201 #endif                          /* DHD_DEBUG */
4202                 DHD_DATA(("%s: processed %d frames\n", __func__, rxcount));
4203         /* Back off rxseq if awaiting rtx, update rx_seq */
4204         if (bus->rxskip)
4205                 rxseq--;
4206         bus->rx_seq = rxseq;
4207
4208         return rxcount;
4209 }
4210
4211 static u32 dhdsdio_hostmail(dhd_bus_t *bus)
4212 {
4213         sdpcmd_regs_t *regs = bus->regs;
4214         u32 intstatus = 0;
4215         u32 hmb_data;
4216         u8 fcbits;
4217         uint retries = 0;
4218
4219         DHD_TRACE(("%s: Enter\n", __func__));
4220
4221         /* Read mailbox data and ack that we did so */
4222         R_SDREG(hmb_data, &regs->tohostmailboxdata, retries);
4223         if (retries <= retry_limit)
4224                 W_SDREG(SMB_INT_ACK, &regs->tosbmailbox, retries);
4225         bus->f1regdata += 2;
4226
4227         /* Dongle recomposed rx frames, accept them again */
4228         if (hmb_data & HMB_DATA_NAKHANDLED) {
4229                 DHD_INFO(("Dongle reports NAK handled, expect rtx of %d\n",
4230                           bus->rx_seq));
4231                 if (!bus->rxskip)
4232                         DHD_ERROR(("%s: unexpected NAKHANDLED!\n", __func__));
4233
4234                 bus->rxskip = false;
4235                 intstatus |= I_HMB_FRAME_IND;
4236         }
4237
4238         /*
4239          * DEVREADY does not occur with gSPI.
4240          */
4241         if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
4242                 bus->sdpcm_ver =
4243                     (hmb_data & HMB_DATA_VERSION_MASK) >>
4244                     HMB_DATA_VERSION_SHIFT;
4245                 if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
4246                         DHD_ERROR(("Version mismatch, dongle reports %d, "
4247                                 "expecting %d\n",
4248                                 bus->sdpcm_ver, SDPCM_PROT_VERSION));
4249                 else
4250                         DHD_INFO(("Dongle ready, protocol version %d\n",
4251                                   bus->sdpcm_ver));
4252         }
4253
4254         /*
4255          * Flow Control has been moved into the RX headers and this out of band
4256          * method isn't used any more.  Leae this here for possibly
4257          * remaining backward
4258          * compatible with older dongles
4259          */
4260         if (hmb_data & HMB_DATA_FC) {
4261                 fcbits =
4262                     (hmb_data & HMB_DATA_FCDATA_MASK) >> HMB_DATA_FCDATA_SHIFT;
4263
4264                 if (fcbits & ~bus->flowcontrol)
4265                         bus->fc_xoff++;
4266                 if (bus->flowcontrol & ~fcbits)
4267                         bus->fc_xon++;
4268
4269                 bus->fc_rcvd++;
4270                 bus->flowcontrol = fcbits;
4271         }
4272
4273         /* Shouldn't be any others */
4274         if (hmb_data & ~(HMB_DATA_DEVREADY |
4275                          HMB_DATA_NAKHANDLED |
4276                          HMB_DATA_FC |
4277                          HMB_DATA_FWREADY |
4278                          HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK)) {
4279                 DHD_ERROR(("Unknown mailbox data content: 0x%02x\n", hmb_data));
4280         }
4281
4282         return intstatus;
4283 }
4284
4285 bool dhdsdio_dpc(dhd_bus_t *bus)
4286 {
4287         bcmsdh_info_t *sdh = bus->sdh;
4288         sdpcmd_regs_t *regs = bus->regs;
4289         u32 intstatus, newstatus = 0;
4290         uint retries = 0;
4291         uint rxlimit = dhd_rxbound;     /* Rx frames to read before resched */
4292         uint txlimit = dhd_txbound;     /* Tx frames to send before resched */
4293         uint framecnt = 0;      /* Temporary counter of tx/rx frames */
4294         bool rxdone = true;     /* Flag for no more read data */
4295         bool resched = false;   /* Flag indicating resched wanted */
4296
4297         DHD_TRACE(("%s: Enter\n", __func__));
4298
4299         /* Start with leftover status bits */
4300         intstatus = bus->intstatus;
4301
4302         dhd_os_sdlock(bus->dhd);
4303
4304         /* If waiting for HTAVAIL, check status */
4305         if (bus->clkstate == CLK_PENDING) {
4306                 int err;
4307                 u8 clkctl, devctl = 0;
4308
4309 #ifdef DHD_DEBUG
4310                 /* Check for inconsistent device control */
4311                 devctl =
4312                     bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL, &err);
4313                 if (err) {
4314                         DHD_ERROR(("%s: error reading DEVCTL: %d\n",
4315                                    __func__, err));
4316                         bus->dhd->busstate = DHD_BUS_DOWN;
4317                 } else {
4318                         ASSERT(devctl & SBSDIO_DEVCTL_CA_INT_ONLY);
4319                 }
4320 #endif                          /* DHD_DEBUG */
4321
4322                 /* Read CSR, if clock on switch to AVAIL, else ignore */
4323                 clkctl =
4324                     bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
4325                                     &err);
4326                 if (err) {
4327                         DHD_ERROR(("%s: error reading CSR: %d\n", __func__,
4328                                    err));
4329                         bus->dhd->busstate = DHD_BUS_DOWN;
4330                 }
4331
4332                 DHD_INFO(("DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n", devctl,
4333                           clkctl));
4334
4335                 if (SBSDIO_HTAV(clkctl)) {
4336                         devctl =
4337                             bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
4338                                             &err);
4339                         if (err) {
4340                                 DHD_ERROR(("%s: error reading DEVCTL: %d\n",
4341                                            __func__, err));
4342                                 bus->dhd->busstate = DHD_BUS_DOWN;
4343                         }
4344                         devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
4345                         bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_DEVICE_CTL,
4346                                          devctl, &err);
4347                         if (err) {
4348                                 DHD_ERROR(("%s: error writing DEVCTL: %d\n",
4349                                            __func__, err));
4350                                 bus->dhd->busstate = DHD_BUS_DOWN;
4351                         }
4352                         bus->clkstate = CLK_AVAIL;
4353                 } else {
4354                         goto clkwait;
4355                 }
4356         }
4357
4358         BUS_WAKE(bus);
4359
4360         /* Make sure backplane clock is on */
4361         dhdsdio_clkctl(bus, CLK_AVAIL, true);
4362         if (bus->clkstate == CLK_PENDING)
4363                 goto clkwait;
4364
4365         /* Pending interrupt indicates new device status */
4366         if (bus->ipend) {
4367                 bus->ipend = false;
4368                 R_SDREG(newstatus, &regs->intstatus, retries);
4369                 bus->f1regdata++;
4370                 if (bcmsdh_regfail(bus->sdh))
4371                         newstatus = 0;
4372                 newstatus &= bus->hostintmask;
4373                 bus->fcstate = !!(newstatus & I_HMB_FC_STATE);
4374                 if (newstatus) {
4375                         W_SDREG(newstatus, &regs->intstatus, retries);
4376                         bus->f1regdata++;
4377                 }
4378         }
4379
4380         /* Merge new bits with previous */
4381         intstatus |= newstatus;
4382         bus->intstatus = 0;
4383
4384         /* Handle flow-control change: read new state in case our ack
4385          * crossed another change interrupt.  If change still set, assume
4386          * FC ON for safety, let next loop through do the debounce.
4387          */
4388         if (intstatus & I_HMB_FC_CHANGE) {
4389                 intstatus &= ~I_HMB_FC_CHANGE;
4390                 W_SDREG(I_HMB_FC_CHANGE, &regs->intstatus, retries);
4391                 R_SDREG(newstatus, &regs->intstatus, retries);
4392                 bus->f1regdata += 2;
4393                 bus->fcstate =
4394                     !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE));
4395                 intstatus |= (newstatus & bus->hostintmask);
4396         }
4397
4398         /* Handle host mailbox indication */
4399         if (intstatus & I_HMB_HOST_INT) {
4400                 intstatus &= ~I_HMB_HOST_INT;
4401                 intstatus |= dhdsdio_hostmail(bus);
4402         }
4403
4404         /* Generally don't ask for these, can get CRC errors... */
4405         if (intstatus & I_WR_OOSYNC) {
4406                 DHD_ERROR(("Dongle reports WR_OOSYNC\n"));
4407                 intstatus &= ~I_WR_OOSYNC;
4408         }
4409
4410         if (intstatus & I_RD_OOSYNC) {
4411                 DHD_ERROR(("Dongle reports RD_OOSYNC\n"));
4412                 intstatus &= ~I_RD_OOSYNC;
4413         }
4414
4415         if (intstatus & I_SBINT) {
4416                 DHD_ERROR(("Dongle reports SBINT\n"));
4417                 intstatus &= ~I_SBINT;
4418         }
4419
4420         /* Would be active due to wake-wlan in gSPI */
4421         if (intstatus & I_CHIPACTIVE) {
4422                 DHD_INFO(("Dongle reports CHIPACTIVE\n"));
4423                 intstatus &= ~I_CHIPACTIVE;
4424         }
4425
4426         /* Ignore frame indications if rxskip is set */
4427         if (bus->rxskip)
4428                 intstatus &= ~I_HMB_FRAME_IND;
4429
4430         /* On frame indication, read available frames */
4431         if (PKT_AVAILABLE()) {
4432                 framecnt = dhdsdio_readframes(bus, rxlimit, &rxdone);
4433                 if (rxdone || bus->rxskip)
4434                         intstatus &= ~I_HMB_FRAME_IND;
4435                 rxlimit -= min(framecnt, rxlimit);
4436         }
4437
4438         /* Keep still-pending events for next scheduling */
4439         bus->intstatus = intstatus;
4440
4441 clkwait:
4442 #if defined(OOB_INTR_ONLY)
4443         bcmsdh_oob_intr_set(1);
4444 #endif                          /* (OOB_INTR_ONLY) */
4445         /* Re-enable interrupts to detect new device events (mailbox, rx frame)
4446          * or clock availability.  (Allows tx loop to check ipend if desired.)
4447          * (Unless register access seems hosed, as we may not be able to ACK...)
4448          */
4449         if (bus->intr && bus->intdis && !bcmsdh_regfail(sdh)) {
4450                 DHD_INTR(("%s: enable SDIO interrupts, rxdone %d framecnt %d\n",
4451                           __func__, rxdone, framecnt));
4452                 bus->intdis = false;
4453                 bcmsdh_intr_enable(sdh);
4454         }
4455
4456         if (DATAOK(bus) && bus->ctrl_frame_stat &&
4457                 (bus->clkstate == CLK_AVAIL)) {
4458                 int ret, i;
4459
4460                 ret =
4461                     dhd_bcmsdh_send_buf(bus, bcmsdh_cur_sbwad(sdh), SDIO_FUNC_2,
4462                                         F2SYNC, (u8 *) bus->ctrl_frame_buf,
4463                                         (u32) bus->ctrl_frame_len, NULL,
4464                                         NULL, NULL);
4465                 ASSERT(ret != BCME_PENDING);
4466
4467                 if (ret < 0) {
4468                         /* On failure, abort the command and
4469                                 terminate the frame */
4470                         DHD_INFO(("%s: sdio error %d, abort command and "
4471                                 "terminate frame.\n", __func__, ret));
4472                         bus->tx_sderrs++;
4473
4474                         bcmsdh_abort(sdh, SDIO_FUNC_2);
4475
4476                         bcmsdh_cfg_write(sdh, SDIO_FUNC_1,
4477                                          SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
4478                                          NULL);
4479                         bus->f1regdata++;
4480
4481                         for (i = 0; i < 3; i++) {
4482                                 u8 hi, lo;
4483                                 hi = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
4484                                                      SBSDIO_FUNC1_WFRAMEBCHI,
4485                                                      NULL);
4486                                 lo = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
4487                                                      SBSDIO_FUNC1_WFRAMEBCLO,
4488                                                      NULL);
4489                                 bus->f1regdata += 2;
4490                                 if ((hi == 0) && (lo == 0))
4491                                         break;
4492                         }
4493
4494                 }
4495                 if (ret == 0)
4496                         bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
4497
4498                 printf("Return_dpc value is : %d\n", ret);
4499                 bus->ctrl_frame_stat = false;
4500                 dhd_wait_event_wakeup(bus->dhd);
4501         }
4502         /* Send queued frames (limit 1 if rx may still be pending) */
4503         else if ((bus->clkstate == CLK_AVAIL) && !bus->fcstate &&
4504                  pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit
4505                  && DATAOK(bus)) {
4506                 framecnt = rxdone ? txlimit : min(txlimit, dhd_txminmax);
4507                 framecnt = dhdsdio_sendfromq(bus, framecnt);
4508                 txlimit -= framecnt;
4509         }
4510
4511         /* Resched if events or tx frames are pending,
4512                  else await next interrupt */
4513         /* On failed register access, all bets are off:
4514                  no resched or interrupts */
4515         if ((bus->dhd->busstate == DHD_BUS_DOWN) || bcmsdh_regfail(sdh)) {
4516                 DHD_ERROR(("%s: failed backplane access over SDIO, halting "
4517                         "operation %d\n", __func__, bcmsdh_regfail(sdh)));
4518                 bus->dhd->busstate = DHD_BUS_DOWN;
4519                 bus->intstatus = 0;
4520         } else if (bus->clkstate == CLK_PENDING) {
4521                 DHD_INFO(("%s: rescheduled due to CLK_PENDING awaiting "
4522                         "I_CHIPACTIVE interrupt\n", __func__));
4523                 resched = true;
4524         } else if (bus->intstatus || bus->ipend ||
4525                 (!bus->fcstate && pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
4526                         DATAOK(bus)) || PKT_AVAILABLE()) {
4527                 resched = true;
4528         }
4529
4530         bus->dpc_sched = resched;
4531
4532         /* If we're done for now, turn off clock request. */
4533         if ((bus->clkstate != CLK_PENDING)
4534             && bus->idletime == DHD_IDLE_IMMEDIATE) {
4535                 bus->activity = false;
4536                 dhdsdio_clkctl(bus, CLK_NONE, false);
4537         }
4538
4539         dhd_os_sdunlock(bus->dhd);
4540
4541         return resched;
4542 }
4543
4544 bool dhd_bus_dpc(struct dhd_bus *bus)
4545 {
4546         bool resched;
4547
4548         /* Call the DPC directly. */
4549         DHD_TRACE(("Calling dhdsdio_dpc() from %s\n", __func__));
4550         resched = dhdsdio_dpc(bus);
4551
4552         return resched;
4553 }
4554
4555 void dhdsdio_isr(void *arg)
4556 {
4557         dhd_bus_t *bus = (dhd_bus_t *) arg;
4558         bcmsdh_info_t *sdh;
4559
4560         DHD_TRACE(("%s: Enter\n", __func__));
4561
4562         if (!bus) {
4563                 DHD_ERROR(("%s : bus is null pointer , exit\n", __func__));
4564                 return;
4565         }
4566         sdh = bus->sdh;
4567
4568         if (bus->dhd->busstate == DHD_BUS_DOWN) {
4569                 DHD_ERROR(("%s : bus is down. we have nothing to do\n",
4570                            __func__));
4571                 return;
4572         }
4573         /* Count the interrupt call */
4574         bus->intrcount++;
4575         bus->ipend = true;
4576
4577         /* Shouldn't get this interrupt if we're sleeping? */
4578         if (bus->sleeping) {
4579                 DHD_ERROR(("INTERRUPT WHILE SLEEPING??\n"));
4580                 return;
4581         }
4582
4583         /* Disable additional interrupts (is this needed now)? */
4584         if (bus->intr)
4585                 DHD_INTR(("%s: disable SDIO interrupts\n", __func__));
4586         else
4587                 DHD_ERROR(("dhdsdio_isr() w/o interrupt configured!\n"));
4588
4589         bcmsdh_intr_disable(sdh);
4590         bus->intdis = true;
4591
4592 #if defined(SDIO_ISR_THREAD)
4593         DHD_TRACE(("Calling dhdsdio_dpc() from %s\n", __func__));
4594         while (dhdsdio_dpc(bus))
4595                 ;
4596 #else
4597         bus->dpc_sched = true;
4598         dhd_sched_dpc(bus->dhd);
4599 #endif
4600
4601 }
4602
4603 #ifdef SDTEST
4604 static void dhdsdio_pktgen_init(dhd_bus_t *bus)
4605 {
4606         /* Default to specified length, or full range */
4607         if (dhd_pktgen_len) {
4608                 bus->pktgen_maxlen = min(dhd_pktgen_len, MAX_PKTGEN_LEN);
4609                 bus->pktgen_minlen = bus->pktgen_maxlen;
4610         } else {
4611                 bus->pktgen_maxlen = MAX_PKTGEN_LEN;
4612                 bus->pktgen_minlen = 0;
4613         }
4614         bus->pktgen_len = (u16) bus->pktgen_minlen;
4615
4616         /* Default to per-watchdog burst with 10s print time */
4617         bus->pktgen_freq = 1;
4618         bus->pktgen_print = 10000 / dhd_watchdog_ms;
4619         bus->pktgen_count = (dhd_pktgen * dhd_watchdog_ms + 999) / 1000;
4620
4621         /* Default to echo mode */
4622         bus->pktgen_mode = DHD_PKTGEN_ECHO;
4623         bus->pktgen_stop = 1;
4624 }
4625
4626 static void dhdsdio_pktgen(dhd_bus_t *bus)
4627 {
4628         void *pkt;
4629         u8 *data;
4630         uint pktcount;
4631         uint fillbyte;
4632         osl_t *osh = bus->dhd->osh;
4633         u16 len;
4634
4635         /* Display current count if appropriate */
4636         if (bus->pktgen_print && (++bus->pktgen_ptick >= bus->pktgen_print)) {
4637                 bus->pktgen_ptick = 0;
4638                 printf("%s: send attempts %d rcvd %d\n",
4639                        __func__, bus->pktgen_sent, bus->pktgen_rcvd);
4640         }
4641
4642         /* For recv mode, just make sure dongle has started sending */
4643         if (bus->pktgen_mode == DHD_PKTGEN_RECV) {
4644                 if (!bus->pktgen_rcvd)
4645                         dhdsdio_sdtest_set(bus, true);
4646                 return;
4647         }
4648
4649         /* Otherwise, generate or request the specified number of packets */
4650         for (pktcount = 0; pktcount < bus->pktgen_count; pktcount++) {
4651                 /* Stop if total has been reached */
4652                 if (bus->pktgen_total
4653                     && (bus->pktgen_sent >= bus->pktgen_total)) {
4654                         bus->pktgen_count = 0;
4655                         break;
4656                 }
4657
4658                 /* Allocate an appropriate-sized packet */
4659                 len = bus->pktgen_len;
4660                 pkt = PKTGET(osh,
4661                         (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + DHD_SDALIGN),
4662                         true);
4663                 if (!pkt) {
4664                         DHD_ERROR(("%s: PKTGET failed!\n", __func__));
4665                         break;
4666                 }
4667                 PKTALIGN(osh, pkt, (len + SDPCM_HDRLEN + SDPCM_TEST_HDRLEN),
4668                          DHD_SDALIGN);
4669                 data = (u8 *) PKTDATA(pkt) + SDPCM_HDRLEN;
4670
4671                 /* Write test header cmd and extra based on mode */
4672                 switch (bus->pktgen_mode) {
4673                 case DHD_PKTGEN_ECHO:
4674                         *data++ = SDPCM_TEST_ECHOREQ;
4675                         *data++ = (u8) bus->pktgen_sent;
4676                         break;
4677
4678                 case DHD_PKTGEN_SEND:
4679                         *data++ = SDPCM_TEST_DISCARD;
4680                         *data++ = (u8) bus->pktgen_sent;
4681                         break;
4682
4683                 case DHD_PKTGEN_RXBURST:
4684                         *data++ = SDPCM_TEST_BURST;
4685                         *data++ = (u8) bus->pktgen_count;
4686                         break;
4687
4688                 default:
4689                         DHD_ERROR(("Unrecognized pktgen mode %d\n",
4690                                    bus->pktgen_mode));
4691                         PKTFREE(osh, pkt, true);
4692                         bus->pktgen_count = 0;
4693                         return;
4694                 }
4695
4696                 /* Write test header length field */
4697                 *data++ = (len >> 0);
4698                 *data++ = (len >> 8);
4699
4700                 /* Then fill in the remainder -- N/A for burst,
4701                          but who cares... */
4702                 for (fillbyte = 0; fillbyte < len; fillbyte++)
4703                         *data++ =
4704                             SDPCM_TEST_FILL(fillbyte, (u8) bus->pktgen_sent);
4705
4706 #ifdef DHD_DEBUG
4707                 if (DHD_BYTES_ON() && DHD_DATA_ON()) {
4708                         data = (u8 *) PKTDATA(pkt) + SDPCM_HDRLEN;
4709                         prhex("dhdsdio_pktgen: Tx Data", data,
4710                               PKTLEN(pkt) - SDPCM_HDRLEN);
4711                 }
4712 #endif
4713
4714                 /* Send it */
4715                 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true)) {
4716                         bus->pktgen_fail++;
4717                         if (bus->pktgen_stop
4718                             && bus->pktgen_stop == bus->pktgen_fail)
4719                                 bus->pktgen_count = 0;
4720                 }
4721                 bus->pktgen_sent++;
4722
4723                 /* Bump length if not fixed, wrap at max */
4724                 if (++bus->pktgen_len > bus->pktgen_maxlen)
4725                         bus->pktgen_len = (u16) bus->pktgen_minlen;
4726
4727                 /* Special case for burst mode: just send one request! */
4728                 if (bus->pktgen_mode == DHD_PKTGEN_RXBURST)
4729                         break;
4730         }
4731 }
4732
4733 static void dhdsdio_sdtest_set(dhd_bus_t *bus, bool start)
4734 {
4735         void *pkt;
4736         u8 *data;
4737         osl_t *osh = bus->dhd->osh;
4738
4739         /* Allocate the packet */
4740         pkt = PKTGET(osh, SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + DHD_SDALIGN,
4741                         true);
4742         if (!pkt) {
4743                 DHD_ERROR(("%s: PKTGET failed!\n", __func__));
4744                 return;
4745         }
4746         PKTALIGN(osh, pkt, (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN), DHD_SDALIGN);
4747         data = (u8 *) PKTDATA(pkt) + SDPCM_HDRLEN;
4748
4749         /* Fill in the test header */
4750         *data++ = SDPCM_TEST_SEND;
4751         *data++ = start;
4752         *data++ = (bus->pktgen_maxlen >> 0);
4753         *data++ = (bus->pktgen_maxlen >> 8);
4754
4755         /* Send it */
4756         if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true))
4757                 bus->pktgen_fail++;
4758 }
4759
4760 static void dhdsdio_testrcv(dhd_bus_t *bus, void *pkt, uint seq)
4761 {
4762         osl_t *osh = bus->dhd->osh;
4763         u8 *data;
4764         uint pktlen;
4765
4766         u8 cmd;
4767         u8 extra;
4768         u16 len;
4769         u16 offset;
4770
4771         /* Check for min length */
4772         pktlen = PKTLEN(pkt);
4773         if (pktlen < SDPCM_TEST_HDRLEN) {
4774                 DHD_ERROR(("dhdsdio_restrcv: toss runt frame, pktlen %d\n",
4775                            pktlen));
4776                 PKTFREE(osh, pkt, false);
4777                 return;
4778         }
4779
4780         /* Extract header fields */
4781         data = PKTDATA(pkt);
4782         cmd = *data++;
4783         extra = *data++;
4784         len = *data++;
4785         len += *data++ << 8;
4786
4787         /* Check length for relevant commands */
4788         if (cmd == SDPCM_TEST_DISCARD || cmd == SDPCM_TEST_ECHOREQ
4789             || cmd == SDPCM_TEST_ECHORSP) {
4790                 if (pktlen != len + SDPCM_TEST_HDRLEN) {
4791                         DHD_ERROR(("dhdsdio_testrcv: frame length mismatch, "
4792                                 "pktlen %d seq %d" " cmd %d extra %d len %d\n",
4793                                 pktlen, seq, cmd, extra, len));
4794                         PKTFREE(osh, pkt, false);
4795                         return;
4796                 }
4797         }
4798
4799         /* Process as per command */
4800         switch (cmd) {
4801         case SDPCM_TEST_ECHOREQ:
4802                 /* Rx->Tx turnaround ok (even on NDIS w/current
4803                          implementation) */
4804                 *(u8 *) (PKTDATA(pkt)) = SDPCM_TEST_ECHORSP;
4805                 if (dhdsdio_txpkt(bus, pkt, SDPCM_TEST_CHANNEL, true) == 0) {
4806                         bus->pktgen_sent++;
4807                 } else {
4808                         bus->pktgen_fail++;
4809                         PKTFREE(osh, pkt, false);
4810                 }
4811                 bus->pktgen_rcvd++;
4812                 break;
4813
4814         case SDPCM_TEST_ECHORSP:
4815                 if (bus->ext_loop) {
4816                         PKTFREE(osh, pkt, false);
4817                         bus->pktgen_rcvd++;
4818                         break;
4819                 }
4820
4821                 for (offset = 0; offset < len; offset++, data++) {
4822                         if (*data != SDPCM_TEST_FILL(offset, extra)) {
4823                                 DHD_ERROR(("dhdsdio_testrcv: echo data mismatch: " "offset %d (len %d) expect 0x%02x rcvd 0x%02x\n",
4824                                         offset, len,
4825                                         SDPCM_TEST_FILL(offset, extra), *data));
4826                                 break;
4827                         }
4828                 }
4829                 PKTFREE(osh, pkt, false);
4830                 bus->pktgen_rcvd++;
4831                 break;
4832
4833         case SDPCM_TEST_DISCARD:
4834                 PKTFREE(osh, pkt, false);
4835                 bus->pktgen_rcvd++;
4836                 break;
4837
4838         case SDPCM_TEST_BURST:
4839         case SDPCM_TEST_SEND:
4840         default:
4841                 DHD_INFO(("dhdsdio_testrcv: unsupported or unknown command, "
4842                         "pktlen %d seq %d" " cmd %d extra %d len %d\n",
4843                         pktlen, seq, cmd, extra, len));
4844                 PKTFREE(osh, pkt, false);
4845                 break;
4846         }
4847
4848         /* For recv mode, stop at limie (and tell dongle to stop sending) */
4849         if (bus->pktgen_mode == DHD_PKTGEN_RECV) {
4850                 if (bus->pktgen_total
4851                     && (bus->pktgen_rcvd >= bus->pktgen_total)) {
4852                         bus->pktgen_count = 0;
4853                         dhdsdio_sdtest_set(bus, false);
4854                 }
4855         }
4856 }
4857 #endif                          /* SDTEST */
4858
4859 extern bool dhd_bus_watchdog(dhd_pub_t *dhdp)
4860 {
4861         dhd_bus_t *bus;
4862
4863         DHD_TIMER(("%s: Enter\n", __func__));
4864
4865         bus = dhdp->bus;
4866
4867         if (bus->dhd->dongle_reset)
4868                 return false;
4869
4870         /* Ignore the timer if simulating bus down */
4871         if (bus->sleeping)
4872                 return false;
4873
4874         dhd_os_sdlock(bus->dhd);
4875
4876         /* Poll period: check device if appropriate. */
4877         if (bus->poll && (++bus->polltick >= bus->pollrate)) {
4878                 u32 intstatus = 0;
4879
4880                 /* Reset poll tick */
4881                 bus->polltick = 0;
4882
4883                 /* Check device if no interrupts */
4884                 if (!bus->intr || (bus->intrcount == bus->lastintrs)) {
4885
4886                         if (!bus->dpc_sched) {
4887                                 u8 devpend;
4888                                 devpend = bcmsdh_cfg_read(bus->sdh, SDIO_FUNC_0,
4889                                                           SDIOD_CCCR_INTPEND,
4890                                                           NULL);
4891                                 intstatus =
4892                                     devpend & (INTR_STATUS_FUNC1 |
4893                                                INTR_STATUS_FUNC2);
4894                         }
4895
4896                         /* If there is something, make like the ISR and
4897                                  schedule the DPC */
4898                         if (intstatus) {
4899                                 bus->pollcnt++;
4900                                 bus->ipend = true;
4901                                 if (bus->intr)
4902                                         bcmsdh_intr_disable(bus->sdh);
4903
4904                                 bus->dpc_sched = true;
4905                                 dhd_sched_dpc(bus->dhd);
4906
4907                         }
4908                 }
4909
4910                 /* Update interrupt tracking */
4911                 bus->lastintrs = bus->intrcount;
4912         }
4913 #ifdef DHD_DEBUG
4914         /* Poll for console output periodically */
4915         if (dhdp->busstate == DHD_BUS_DATA && dhd_console_ms != 0) {
4916                 bus->console.count += dhd_watchdog_ms;
4917                 if (bus->console.count >= dhd_console_ms) {
4918                         bus->console.count -= dhd_console_ms;
4919                         /* Make sure backplane clock is on */
4920                         dhdsdio_clkctl(bus, CLK_AVAIL, false);
4921                         if (dhdsdio_readconsole(bus) < 0)
4922                                 dhd_console_ms = 0;     /* On error,
4923                                                          stop trying */
4924                 }
4925         }
4926 #endif                          /* DHD_DEBUG */
4927
4928 #ifdef SDTEST
4929         /* Generate packets if configured */
4930         if (bus->pktgen_count && (++bus->pktgen_tick >= bus->pktgen_freq)) {
4931                 /* Make sure backplane clock is on */
4932                 dhdsdio_clkctl(bus, CLK_AVAIL, false);
4933                 bus->pktgen_tick = 0;
4934                 dhdsdio_pktgen(bus);
4935         }
4936 #endif
4937
4938         /* On idle timeout clear activity flag and/or turn off clock */
4939         if ((bus->idletime > 0) && (bus->clkstate == CLK_AVAIL)) {
4940                 if (++bus->idlecount >= bus->idletime) {
4941                         bus->idlecount = 0;
4942                         if (bus->activity) {
4943                                 bus->activity = false;
4944                                 dhd_os_wd_timer(bus->dhd, dhd_watchdog_ms);
4945                         } else {
4946                                 dhdsdio_clkctl(bus, CLK_NONE, false);
4947                         }
4948                 }
4949         }
4950
4951         dhd_os_sdunlock(bus->dhd);
4952
4953         return bus->ipend;
4954 }
4955
4956 #ifdef DHD_DEBUG
4957 extern int dhd_bus_console_in(dhd_pub_t *dhdp, unsigned char *msg, uint msglen)
4958 {
4959         dhd_bus_t *bus = dhdp->bus;
4960         u32 addr, val;
4961         int rv;
4962         void *pkt;
4963
4964         /* Address could be zero if CONSOLE := 0 in dongle Makefile */
4965         if (bus->console_addr == 0)
4966                 return BCME_UNSUPPORTED;
4967
4968         /* Exclusive bus access */
4969         dhd_os_sdlock(bus->dhd);
4970
4971         /* Don't allow input if dongle is in reset */
4972         if (bus->dhd->dongle_reset) {
4973                 dhd_os_sdunlock(bus->dhd);
4974                 return BCME_NOTREADY;
4975         }
4976
4977         /* Request clock to allow SDIO accesses */
4978         BUS_WAKE(bus);
4979         /* No pend allowed since txpkt is called later, ht clk has to be on */
4980         dhdsdio_clkctl(bus, CLK_AVAIL, false);
4981
4982         /* Zero cbuf_index */
4983         addr = bus->console_addr + offsetof(hndrte_cons_t, cbuf_idx);
4984         val = htol32(0);
4985         rv = dhdsdio_membytes(bus, true, addr, (u8 *)&val, sizeof(val));
4986         if (rv < 0)
4987                 goto done;
4988
4989         /* Write message into cbuf */
4990         addr = bus->console_addr + offsetof(hndrte_cons_t, cbuf);
4991         rv = dhdsdio_membytes(bus, true, addr, (u8 *)msg, msglen);
4992         if (rv < 0)
4993                 goto done;
4994
4995         /* Write length into vcons_in */
4996         addr = bus->console_addr + offsetof(hndrte_cons_t, vcons_in);
4997         val = htol32(msglen);
4998         rv = dhdsdio_membytes(bus, true, addr, (u8 *)&val, sizeof(val));
4999         if (rv < 0)
5000                 goto done;
5001
5002         /* Bump dongle by sending an empty event pkt.
5003          * sdpcm_sendup (RX) checks for virtual console input.
5004          */
5005         pkt = PKTGET(bus->dhd->osh, 4 + SDPCM_RESERVE, true);
5006         if ((pkt != NULL) && bus->clkstate == CLK_AVAIL)
5007                 dhdsdio_txpkt(bus, pkt, SDPCM_EVENT_CHANNEL, true);
5008
5009 done:
5010         if ((bus->idletime == DHD_IDLE_IMMEDIATE) && !bus->dpc_sched) {
5011                 bus->activity = false;
5012                 dhdsdio_clkctl(bus, CLK_NONE, true);
5013         }
5014
5015         dhd_os_sdunlock(bus->dhd);
5016
5017         return rv;
5018 }
5019 #endif                          /* DHD_DEBUG */
5020
5021 #ifdef DHD_DEBUG
5022 static void dhd_dump_cis(uint fn, u8 *cis)
5023 {
5024         uint byte, tag, tdata;
5025         DHD_INFO(("Function %d CIS:\n", fn));
5026
5027         for (tdata = byte = 0; byte < SBSDIO_CIS_SIZE_LIMIT; byte++) {
5028                 if ((byte % 16) == 0)
5029                         DHD_INFO(("    "));
5030                 DHD_INFO(("%02x ", cis[byte]));
5031                 if ((byte % 16) == 15)
5032                         DHD_INFO(("\n"));
5033                 if (!tdata--) {
5034                         tag = cis[byte];
5035                         if (tag == 0xff)
5036                                 break;
5037                         else if (!tag)
5038                                 tdata = 0;
5039                         else if ((byte + 1) < SBSDIO_CIS_SIZE_LIMIT)
5040                                 tdata = cis[byte + 1] + 1;
5041                         else
5042                                 DHD_INFO(("]"));
5043                 }
5044         }
5045         if ((byte % 16) != 15)
5046                 DHD_INFO(("\n"));
5047 }
5048 #endif                          /* DHD_DEBUG */
5049
5050 static bool dhdsdio_chipmatch(u16 chipid)
5051 {
5052         if (chipid == BCM4325_CHIP_ID)
5053                 return true;
5054         if (chipid == BCM4329_CHIP_ID)
5055                 return true;
5056         if (chipid == BCM4319_CHIP_ID)
5057                 return true;
5058         return false;
5059 }
5060
5061 static void *dhdsdio_probe(u16 venid, u16 devid, u16 bus_no,
5062                            u16 slot, u16 func, uint bustype, void *regsva,
5063                            osl_t *osh, void *sdh)
5064 {
5065         int ret;
5066         dhd_bus_t *bus;
5067
5068         /* Init global variables at run-time, not as part of the declaration.
5069          * This is required to support init/de-init of the driver.
5070          * Initialization
5071          * of globals as part of the declaration results in non-deterministic
5072          * behavior since the value of the globals may be different on the
5073          * first time that the driver is initialized vs subsequent
5074          * initializations.
5075          */
5076         dhd_txbound = DHD_TXBOUND;
5077         dhd_rxbound = DHD_RXBOUND;
5078         dhd_alignctl = true;
5079         sd1idle = true;
5080         dhd_readahead = true;
5081         retrydata = false;
5082         dhd_doflow = false;
5083         dhd_dongle_memsize = 0;
5084         dhd_txminmax = DHD_TXMINMAX;
5085
5086         forcealign = true;
5087
5088         dhd_common_init();
5089
5090         DHD_TRACE(("%s: Enter\n", __func__));
5091         DHD_INFO(("%s: venid 0x%04x devid 0x%04x\n", __func__, venid, devid));
5092
5093         /* We make assumptions about address window mappings */
5094         ASSERT((unsigned long)regsva == SI_ENUM_BASE);
5095
5096         /* BCMSDH passes venid and devid based on CIS parsing -- but
5097          * low-power start
5098          * means early parse could fail, so here we should get either an ID
5099          * we recognize OR (-1) indicating we must request power first.
5100          */
5101         /* Check the Vendor ID */
5102         switch (venid) {
5103         case 0x0000:
5104         case VENDOR_BROADCOM:
5105                 break;
5106         default:
5107                 DHD_ERROR(("%s: unknown vendor: 0x%04x\n", __func__, venid));
5108                 return NULL;
5109         }
5110
5111         /* Check the Device ID and make sure it's one that we support */
5112         switch (devid) {
5113         case BCM4325_D11DUAL_ID:        /* 4325 802.11a/g id */
5114         case BCM4325_D11G_ID:   /* 4325 802.11g 2.4Ghz band id */
5115         case BCM4325_D11A_ID:   /* 4325 802.11a 5Ghz band id */
5116                 DHD_INFO(("%s: found 4325 Dongle\n", __func__));
5117                 break;
5118         case BCM4329_D11NDUAL_ID:       /* 4329 802.11n dualband device */
5119         case BCM4329_D11N2G_ID: /* 4329 802.11n 2.4G device */
5120         case BCM4329_D11N5G_ID: /* 4329 802.11n 5G device */
5121         case 0x4329:
5122                 DHD_INFO(("%s: found 4329 Dongle\n", __func__));
5123                 break;
5124         case BCM4319_D11N_ID:   /* 4319 802.11n id */
5125         case BCM4319_D11N2G_ID: /* 4319 802.11n2g id */
5126         case BCM4319_D11N5G_ID: /* 4319 802.11n5g id */
5127                 DHD_INFO(("%s: found 4319 Dongle\n", __func__));
5128                 break;
5129         case 0:
5130                 DHD_INFO(("%s: allow device id 0, will check chip internals\n",
5131                           __func__));
5132                 break;
5133
5134         default:
5135                 DHD_ERROR(("%s: skipping 0x%04x/0x%04x, not a dongle\n",
5136                            __func__, venid, devid));
5137                 return NULL;
5138         }
5139
5140         if (osh == NULL) {
5141                 /* Ask the OS interface part for an OSL handle */
5142                 osh = dhd_osl_attach(sdh, DHD_BUS);
5143                 if (!osh) {
5144                         DHD_ERROR(("%s: osl_attach failed!\n", __func__));
5145                         return NULL;
5146                 }
5147         }
5148
5149         /* Allocate private bus interface state */
5150         bus = kzalloc(sizeof(dhd_bus_t), GFP_ATOMIC);
5151         if (!bus) {
5152                 DHD_ERROR(("%s: kmalloc of dhd_bus_t failed\n", __func__));
5153                 goto fail;
5154         }
5155         bus->sdh = sdh;
5156         bus->cl_devid = (u16) devid;
5157         bus->bus = DHD_BUS;
5158         bus->tx_seq = SDPCM_SEQUENCE_WRAP - 1;
5159         bus->usebufpool = false;        /* Use bufpool if allocated,
5160                                          else use locally malloced rxbuf */
5161
5162         /* attempt to attach to the dongle */
5163         if (!(dhdsdio_probe_attach(bus, osh, sdh, regsva, devid))) {
5164                 DHD_ERROR(("%s: dhdsdio_probe_attach failed\n", __func__));
5165                 goto fail;
5166         }
5167
5168         /* Attach to the dhd/OS/network interface */
5169         bus->dhd = dhd_attach(osh, bus, SDPCM_RESERVE);
5170         if (!bus->dhd) {
5171                 DHD_ERROR(("%s: dhd_attach failed\n", __func__));
5172                 goto fail;
5173         }
5174
5175         /* Allocate buffers */
5176         if (!(dhdsdio_probe_malloc(bus, osh, sdh))) {
5177                 DHD_ERROR(("%s: dhdsdio_probe_malloc failed\n", __func__));
5178                 goto fail;
5179         }
5180
5181         if (!(dhdsdio_probe_init(bus, osh, sdh))) {
5182                 DHD_ERROR(("%s: dhdsdio_probe_init failed\n", __func__));
5183                 goto fail;
5184         }
5185
5186         /* Register interrupt callback, but mask it (not operational yet). */
5187         DHD_INTR(("%s: disable SDIO interrupts (not interested yet)\n",
5188                   __func__));
5189         bcmsdh_intr_disable(sdh);
5190         ret = bcmsdh_intr_reg(sdh, dhdsdio_isr, bus);
5191         if (ret != 0) {
5192                 DHD_ERROR(("%s: FAILED: bcmsdh_intr_reg returned %d\n",
5193                            __func__, ret));
5194                 goto fail;
5195         }
5196         DHD_INTR(("%s: registered SDIO interrupt function ok\n", __func__));
5197
5198         DHD_INFO(("%s: completed!!\n", __func__));
5199
5200         /* if firmware path present try to download and bring up bus */
5201         ret = dhd_bus_start(bus->dhd);
5202         if (ret != 0) {
5203                 if (ret == BCME_NOTUP) {
5204                         DHD_ERROR(("%s: dongle is not responding\n", __func__));
5205                         goto fail;
5206                 }
5207         }
5208         /* Ok, have the per-port tell the stack we're open for business */
5209         if (dhd_net_attach(bus->dhd, 0) != 0) {
5210                 DHD_ERROR(("%s: Net attach failed!!\n", __func__));
5211                 goto fail;
5212         }
5213
5214         return bus;
5215
5216 fail:
5217         dhdsdio_release(bus, osh);
5218         return NULL;
5219 }
5220
5221 static bool
5222 dhdsdio_probe_attach(struct dhd_bus *bus, osl_t *osh, void *sdh, void *regsva,
5223                      u16 devid)
5224 {
5225         u8 clkctl = 0;
5226         int err = 0;
5227
5228         bus->alp_only = true;
5229
5230         /* Return the window to backplane enumeration space for core access */
5231         if (dhdsdio_set_siaddr_window(bus, SI_ENUM_BASE))
5232                 DHD_ERROR(("%s: FAILED to return to SI_ENUM_BASE\n", __func__));
5233
5234 #ifdef DHD_DEBUG
5235         printf("F1 signature read @0x18000000=0x%4x\n",
5236                bcmsdh_reg_read(bus->sdh, SI_ENUM_BASE, 4));
5237
5238 #endif                          /* DHD_DEBUG */
5239
5240         /* Force PLL off until si_attach() programs PLL control regs */
5241
5242         bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5243                          DHD_INIT_CLKCTL1, &err);
5244         if (!err)
5245                 clkctl =
5246                     bcmsdh_cfg_read(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5247                                     &err);
5248
5249         if (err || ((clkctl & ~SBSDIO_AVBITS) != DHD_INIT_CLKCTL1)) {
5250                 DHD_ERROR(("dhdsdio_probe: ChipClkCSR access: err %d wrote "
5251                         "0x%02x read 0x%02x\n",
5252                         err, DHD_INIT_CLKCTL1, clkctl));
5253                 goto fail;
5254         }
5255 #ifdef DHD_DEBUG
5256         if (DHD_INFO_ON()) {
5257                 uint fn, numfn;
5258                 u8 *cis[SDIOD_MAX_IOFUNCS];
5259                 int err = 0;
5260
5261                 numfn = bcmsdh_query_iofnum(sdh);
5262                 ASSERT(numfn <= SDIOD_MAX_IOFUNCS);
5263
5264                 /* Make sure ALP is available before trying to read CIS */
5265                 SPINWAIT(((clkctl = bcmsdh_cfg_read(sdh, SDIO_FUNC_1,
5266                                                     SBSDIO_FUNC1_CHIPCLKCSR,
5267                                                     NULL)),
5268                           !SBSDIO_ALPAV(clkctl)), PMU_MAX_TRANSITION_DLY);
5269
5270                 /* Now request ALP be put on the bus */
5271                 bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR,
5272                                  DHD_INIT_CLKCTL2, &err);
5273                 udelay(65);
5274
5275                 for (fn = 0; fn <= numfn; fn++) {
5276                         cis[fn] = kmalloc(SBSDIO_CIS_SIZE_LIMIT, GFP_ATOMIC);
5277                         if (!cis[fn]) {
5278                                 DHD_INFO(("dhdsdio_probe: fn %d cis malloc "
5279                                         "failed\n", fn));
5280                                 break;
5281                         }
5282                         bzero(cis[fn], SBSDIO_CIS_SIZE_LIMIT);
5283
5284                         err = bcmsdh_cis_read(sdh, fn, cis[fn],
5285                                                 SBSDIO_CIS_SIZE_LIMIT);
5286                         if (err) {
5287                                 DHD_INFO(("dhdsdio_probe: fn %d cis read "
5288                                         "err %d\n", fn, err));
5289                                 kfree(cis[fn]);
5290                                 break;
5291                         }
5292                         dhd_dump_cis(fn, cis[fn]);
5293                 }
5294
5295                 while (fn-- > 0) {
5296                         ASSERT(cis[fn]);
5297                         kfree(cis[fn]);
5298                 }
5299
5300                 if (err) {
5301                         DHD_ERROR(("dhdsdio_probe: error read/parsing CIS\n"));
5302                         goto fail;
5303                 }
5304         }
5305 #endif                          /* DHD_DEBUG */
5306
5307         /* si_attach() will provide an SI handle and scan the backplane */
5308         bus->sih = si_attach((uint) devid, osh, regsva, DHD_BUS, sdh,
5309                                    &bus->vars, &bus->varsz);
5310         if (!(bus->sih)) {
5311                 DHD_ERROR(("%s: si_attach failed!\n", __func__));
5312                 goto fail;
5313         }
5314
5315         bcmsdh_chipinfo(sdh, bus->sih->chip, bus->sih->chiprev);
5316
5317         if (!dhdsdio_chipmatch((u16) bus->sih->chip)) {
5318                 DHD_ERROR(("%s: unsupported chip: 0x%04x\n",
5319                            __func__, bus->sih->chip));
5320                 goto fail;
5321         }
5322
5323         si_sdiod_drive_strength_init(bus->sih, osh, dhd_sdiod_drive_strength);
5324
5325         /* Get info on the ARM and SOCRAM cores... */
5326         if (!DHD_NOPMU(bus)) {
5327                 if ((si_setcore(bus->sih, ARM7S_CORE_ID, 0)) ||
5328                     (si_setcore(bus->sih, ARMCM3_CORE_ID, 0))) {
5329                         bus->armrev = si_corerev(bus->sih);
5330                 } else {
5331                         DHD_ERROR(("%s: failed to find ARM core!\n", __func__));
5332                         goto fail;
5333                 }
5334                 bus->orig_ramsize = si_socram_size(bus->sih);
5335                 if (!(bus->orig_ramsize)) {
5336                         DHD_ERROR(("%s: failed to find SOCRAM memory!\n",
5337                                    __func__));
5338                         goto fail;
5339                 }
5340                 bus->ramsize = bus->orig_ramsize;
5341                 if (dhd_dongle_memsize)
5342                         dhd_dongle_setmemsize(bus, dhd_dongle_memsize);
5343
5344                 DHD_ERROR(("DHD: dongle ram size is set to %d(orig %d)\n",
5345                            bus->ramsize, bus->orig_ramsize));
5346         }
5347
5348         /* ...but normally deal with the SDPCMDEV core */
5349         bus->regs = si_setcore(bus->sih, PCMCIA_CORE_ID, 0);
5350         if (!bus->regs) {
5351                 bus->regs = si_setcore(bus->sih, SDIOD_CORE_ID, 0);
5352                 if (!bus->regs) {
5353                         DHD_ERROR(("%s: failed to find SDIODEV core!\n",
5354                                         __func__));
5355                         goto fail;
5356                 }
5357         }
5358         bus->sdpcmrev = si_corerev(bus->sih);
5359
5360         /* Set core control so an SDIO reset does a backplane reset */
5361         OR_REG(osh, &bus->regs->corecontrol, CC_BPRESEN);
5362
5363         pktq_init(&bus->txq, (PRIOMASK + 1), QLEN);
5364
5365         /* Locate an appropriately-aligned portion of hdrbuf */
5366         bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0], DHD_SDALIGN);
5367
5368         /* Set the poll and/or interrupt flags */
5369         bus->intr = (bool) dhd_intr;
5370         bus->poll = (bool) dhd_poll;
5371         if (bus->poll)
5372                 bus->pollrate = 1;
5373
5374         return true;
5375
5376 fail:
5377         return false;
5378 }
5379
5380 static bool dhdsdio_probe_malloc(dhd_bus_t *bus, osl_t *osh, void *sdh)
5381 {
5382         DHD_TRACE(("%s: Enter\n", __func__));
5383
5384         if (bus->dhd->maxctl) {
5385                 bus->rxblen =
5386                     roundup((bus->dhd->maxctl + SDPCM_HDRLEN),
5387                             ALIGNMENT) + DHD_SDALIGN;
5388                 bus->rxbuf = kmalloc(bus->rxblen, GFP_ATOMIC);
5389                 if (!(bus->rxbuf)) {
5390                         DHD_ERROR(("%s: kmalloc of %d-byte rxbuf failed\n",
5391                                    __func__, bus->rxblen));
5392                         goto fail;
5393                 }
5394         }
5395
5396         /* Allocate buffer to receive glomed packet */
5397         bus->databuf = kmalloc(MAX_DATA_BUF, GFP_ATOMIC);
5398         if (!(bus->databuf)) {
5399                 DHD_ERROR(("%s: kmalloc of %d-byte databuf failed\n",
5400                            __func__, MAX_DATA_BUF));
5401                 /* release rxbuf which was already located as above */
5402                 if (!bus->rxblen)
5403                         kfree(bus->rxbuf);
5404                 goto fail;
5405         }
5406
5407         /* Align the buffer */
5408         if ((unsigned long)bus->databuf % DHD_SDALIGN)
5409                 bus->dataptr =
5410                     bus->databuf + (DHD_SDALIGN -
5411                                     ((unsigned long)bus->databuf % DHD_SDALIGN));
5412         else
5413                 bus->dataptr = bus->databuf;
5414
5415         return true;
5416
5417 fail:
5418         return false;
5419 }
5420
5421 static bool dhdsdio_probe_init(dhd_bus_t *bus, osl_t *osh, void *sdh)
5422 {
5423         s32 fnum;
5424
5425         DHD_TRACE(("%s: Enter\n", __func__));
5426
5427 #ifdef SDTEST
5428         dhdsdio_pktgen_init(bus);
5429 #endif                          /* SDTEST */
5430
5431         /* Disable F2 to clear any intermediate frame state on the dongle */
5432         bcmsdh_cfg_write(sdh, SDIO_FUNC_0, SDIOD_CCCR_IOEN, SDIO_FUNC_ENABLE_1,
5433                          NULL);
5434
5435         bus->dhd->busstate = DHD_BUS_DOWN;
5436         bus->sleeping = false;
5437         bus->rxflow = false;
5438         bus->prev_rxlim_hit = 0;
5439
5440         /* Done with backplane-dependent accesses, can drop clock... */
5441         bcmsdh_cfg_write(sdh, SDIO_FUNC_1, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
5442
5443         /* ...and initialize clock/power states */
5444         bus->clkstate = CLK_SDONLY;
5445         bus->idletime = (s32) dhd_idletime;
5446         bus->idleclock = DHD_IDLE_ACTIVE;
5447
5448         /* Query the SD clock speed */
5449         if (bcmsdh_iovar_op(sdh, "sd_divisor", NULL, 0,
5450                             &bus->sd_divisor, sizeof(s32),
5451                             false) != BCME_OK) {
5452                 DHD_ERROR(("%s: fail on %s get\n", __func__, "sd_divisor"));
5453                 bus->sd_divisor = -1;
5454         } else {
5455                 DHD_INFO(("%s: Initial value for %s is %d\n",
5456                           __func__, "sd_divisor", bus->sd_divisor));
5457         }
5458
5459         /* Query the SD bus mode */
5460         if (bcmsdh_iovar_op(sdh, "sd_mode", NULL, 0,
5461                             &bus->sd_mode, sizeof(s32), false) != BCME_OK) {
5462                 DHD_ERROR(("%s: fail on %s get\n", __func__, "sd_mode"));
5463                 bus->sd_mode = -1;
5464         } else {
5465                 DHD_INFO(("%s: Initial value for %s is %d\n",
5466                           __func__, "sd_mode", bus->sd_mode));
5467         }
5468
5469         /* Query the F2 block size, set roundup accordingly */
5470         fnum = 2;
5471         if (bcmsdh_iovar_op(sdh, "sd_blocksize", &fnum, sizeof(s32),
5472                             &bus->blocksize, sizeof(s32), false) != BCME_OK) {
5473                 bus->blocksize = 0;
5474                 DHD_ERROR(("%s: fail on %s get\n", __func__, "sd_blocksize"));
5475         } else {
5476                 DHD_INFO(("%s: Initial value for %s is %d\n",
5477                           __func__, "sd_blocksize", bus->blocksize));
5478         }
5479         bus->roundup = min(max_roundup, bus->blocksize);
5480
5481         /* Query if bus module supports packet chaining,
5482                  default to use if supported */
5483         if (bcmsdh_iovar_op(sdh, "sd_rxchain", NULL, 0,
5484                             &bus->sd_rxchain, sizeof(s32),
5485                             false) != BCME_OK) {
5486                 bus->sd_rxchain = false;
5487         } else {
5488                 DHD_INFO(("%s: bus module (through bcmsdh API) %s chaining\n",
5489                           __func__,
5490                           (bus->sd_rxchain ? "supports" : "does not support")));
5491         }
5492         bus->use_rxchain = (bool) bus->sd_rxchain;
5493
5494         return true;
5495 }
5496
5497 bool
5498 dhd_bus_download_firmware(struct dhd_bus *bus, osl_t *osh,
5499                           char *fw_path, char *nv_path)
5500 {
5501         bool ret;
5502         bus->fw_path = fw_path;
5503         bus->nv_path = nv_path;
5504
5505         ret = dhdsdio_download_firmware(bus, osh, bus->sdh);
5506
5507         return ret;
5508 }
5509
5510 static bool
5511 dhdsdio_download_firmware(struct dhd_bus *bus, osl_t *osh, void *sdh)
5512 {
5513         bool ret;
5514
5515         /* Download the firmware */
5516         dhdsdio_clkctl(bus, CLK_AVAIL, false);
5517
5518         ret = _dhdsdio_download_firmware(bus) == 0;
5519
5520         dhdsdio_clkctl(bus, CLK_SDONLY, false);
5521
5522         return ret;
5523 }
5524
5525 /* Detach and free everything */
5526 static void dhdsdio_release(dhd_bus_t *bus, osl_t *osh)
5527 {
5528         DHD_TRACE(("%s: Enter\n", __func__));
5529
5530         if (bus) {
5531                 ASSERT(osh);
5532
5533                 /* De-register interrupt handler */
5534                 bcmsdh_intr_disable(bus->sdh);
5535                 bcmsdh_intr_dereg(bus->sdh);
5536
5537                 if (bus->dhd) {
5538
5539                         dhdsdio_release_dongle(bus, osh);
5540
5541                         dhd_detach(bus->dhd);
5542                         bus->dhd = NULL;
5543                 }
5544
5545                 dhdsdio_release_malloc(bus, osh);
5546
5547                 kfree(bus);
5548         }
5549
5550         if (osh)
5551                 dhd_osl_detach(osh);
5552
5553         DHD_TRACE(("%s: Disconnected\n", __func__));
5554 }
5555
5556 static void dhdsdio_release_malloc(dhd_bus_t *bus, osl_t *osh)
5557 {
5558         DHD_TRACE(("%s: Enter\n", __func__));
5559
5560         if (bus->dhd && bus->dhd->dongle_reset)
5561                 return;
5562
5563         if (bus->rxbuf) {
5564                 kfree(bus->rxbuf);
5565                 bus->rxctl = bus->rxbuf = NULL;
5566                 bus->rxlen = 0;
5567         }
5568
5569         if (bus->databuf) {
5570                 kfree(bus->databuf);
5571                 bus->databuf = NULL;
5572         }
5573 }
5574
5575 static void dhdsdio_release_dongle(dhd_bus_t *bus, osl_t *osh)
5576 {
5577         DHD_TRACE(("%s: Enter\n", __func__));
5578
5579         if (bus->dhd && bus->dhd->dongle_reset)
5580                 return;
5581
5582         if (bus->sih) {
5583                 dhdsdio_clkctl(bus, CLK_AVAIL, false);
5584 #if !defined(BCMLXSDMMC)
5585                 si_watchdog(bus->sih, 4);
5586 #endif                          /* !defined(BCMLXSDMMC) */
5587                 dhdsdio_clkctl(bus, CLK_NONE, false);
5588                 si_detach(bus->sih);
5589                 if (bus->vars && bus->varsz)
5590                         kfree(bus->vars);
5591                 bus->vars = NULL;
5592         }
5593
5594         DHD_TRACE(("%s: Disconnected\n", __func__));
5595 }
5596
5597 static void dhdsdio_disconnect(void *ptr)
5598 {
5599         dhd_bus_t *bus = (dhd_bus_t *)ptr;
5600
5601         DHD_TRACE(("%s: Enter\n", __func__));
5602
5603         if (bus) {
5604                 ASSERT(bus->dhd);
5605                 dhdsdio_release(bus, bus->dhd->osh);
5606         }
5607
5608         DHD_TRACE(("%s: Disconnected\n", __func__));
5609 }
5610
5611 /* Register/Unregister functions are called by the main DHD entry
5612  * point (e.g. module insertion) to link with the bus driver, in
5613  * order to look for or await the device.
5614  */
5615
5616 static bcmsdh_driver_t dhd_sdio = {
5617         dhdsdio_probe,
5618         dhdsdio_disconnect
5619 };
5620
5621 int dhd_bus_register(void)
5622 {
5623         DHD_TRACE(("%s: Enter\n", __func__));
5624
5625         return bcmsdh_register(&dhd_sdio);
5626 }
5627
5628 void dhd_bus_unregister(void)
5629 {
5630         DHD_TRACE(("%s: Enter\n", __func__));
5631
5632         bcmsdh_unregister();
5633 }
5634
5635 #ifdef BCMEMBEDIMAGE
5636 static int dhdsdio_download_code_array(struct dhd_bus *bus)
5637 {
5638         int bcmerror = -1;
5639         int offset = 0;
5640
5641         DHD_INFO(("%s: download embedded firmware...\n", __func__));
5642
5643         /* Download image */
5644         while ((offset + MEMBLOCK) < sizeof(dlarray)) {
5645                 bcmerror =
5646                     dhdsdio_membytes(bus, true, offset, dlarray + offset,
5647                                      MEMBLOCK);
5648                 if (bcmerror) {
5649                         DHD_ERROR(("%s: error %d on writing %d membytes at "
5650                                 "0x%08x\n",
5651                                 __func__, bcmerror, MEMBLOCK, offset));
5652                         goto err;
5653                 }
5654
5655                 offset += MEMBLOCK;
5656         }
5657
5658         if (offset < sizeof(dlarray)) {
5659                 bcmerror = dhdsdio_membytes(bus, true, offset,
5660                                             dlarray + offset,
5661                                             sizeof(dlarray) - offset);
5662                 if (bcmerror) {
5663                         DHD_ERROR(("%s: error %d on writing %d membytes at "
5664                                 "0x%08x\n", __func__, bcmerror,
5665                                 sizeof(dlarray) - offset, offset));
5666                         goto err;
5667                 }
5668         }
5669 #ifdef DHD_DEBUG
5670         /* Upload and compare the downloaded code */
5671         {
5672                 unsigned char *ularray;
5673
5674                 ularray = kmalloc(bus->ramsize, GFP_ATOMIC);
5675                 /* Upload image to verify downloaded contents. */
5676                 offset = 0;
5677                 memset(ularray, 0xaa, bus->ramsize);
5678                 while ((offset + MEMBLOCK) < sizeof(dlarray)) {
5679                         bcmerror =
5680                             dhdsdio_membytes(bus, false, offset,
5681                                              ularray + offset, MEMBLOCK);
5682                         if (bcmerror) {
5683                                 DHD_ERROR(("%s: error %d on reading %d membytes"
5684                                         " at 0x%08x\n",
5685                                         __func__, bcmerror, MEMBLOCK, offset));
5686                                 goto err;
5687                         }
5688
5689                         offset += MEMBLOCK;
5690                 }
5691
5692                 if (offset < sizeof(dlarray)) {
5693                         bcmerror = dhdsdio_membytes(bus, false, offset,
5694                                                     ularray + offset,
5695                                                     sizeof(dlarray) - offset);
5696                         if (bcmerror) {
5697                                 DHD_ERROR(("%s: error %d on reading %d membytes at 0x%08x\n",
5698                                 __func__, bcmerror,
5699                                 sizeof(dlarray) - offset, offset));
5700                                 goto err;
5701                         }
5702                 }
5703
5704                 if (memcmp(dlarray, ularray, sizeof(dlarray))) {
5705                         DHD_ERROR(("%s: Downloaded image is corrupted.\n",
5706                                    __func__));
5707                         ASSERT(0);
5708                         goto err;
5709                 } else
5710                         DHD_ERROR(("%s: Download/Upload/Compare succeeded.\n",
5711                                 __func__));
5712
5713                 kfree(ularray);
5714         }
5715 #endif                          /* DHD_DEBUG */
5716
5717 err:
5718         return bcmerror;
5719 }
5720 #endif                          /* BCMEMBEDIMAGE */
5721
5722 static int dhdsdio_download_code_file(struct dhd_bus *bus, char *fw_path)
5723 {
5724         int bcmerror = -1;
5725         int offset = 0;
5726         uint len;
5727         void *image = NULL;
5728         u8 *memblock = NULL, *memptr;
5729
5730         DHD_INFO(("%s: download firmware %s\n", __func__, fw_path));
5731
5732         image = dhd_os_open_image(fw_path);
5733         if (image == NULL)
5734                 goto err;
5735
5736         memptr = memblock = kmalloc(MEMBLOCK + DHD_SDALIGN, GFP_ATOMIC);
5737         if (memblock == NULL) {
5738                 DHD_ERROR(("%s: Failed to allocate memory %d bytes\n",
5739                            __func__, MEMBLOCK));
5740                 goto err;
5741         }
5742         if ((u32)(unsigned long)memblock % DHD_SDALIGN)
5743                 memptr +=
5744                     (DHD_SDALIGN - ((u32)(unsigned long)memblock % DHD_SDALIGN));
5745
5746         /* Download image */
5747         while ((len =
5748                 dhd_os_get_image_block((char *)memptr, MEMBLOCK, image))) {
5749                 bcmerror = dhdsdio_membytes(bus, true, offset, memptr, len);
5750                 if (bcmerror) {
5751                         DHD_ERROR(("%s: error %d on writing %d membytes at "
5752                         "0x%08x\n", __func__, bcmerror, MEMBLOCK, offset));
5753                         goto err;
5754                 }
5755
5756                 offset += MEMBLOCK;
5757         }
5758
5759 err:
5760         if (memblock)
5761                 kfree(memblock);
5762
5763         if (image)
5764                 dhd_os_close_image(image);
5765
5766         return bcmerror;
5767 }
5768
5769 /*
5770  * ProcessVars:Takes a buffer of "<var>=<value>\n" lines read from a file
5771  * and ending in a NUL.
5772  * Removes carriage returns, empty lines, comment lines, and converts
5773  * newlines to NULs.
5774  * Shortens buffer as needed and pads with NULs.  End of buffer is marked
5775  * by two NULs.
5776 */
5777
5778 static uint process_nvram_vars(char *varbuf, uint len)
5779 {
5780         char *dp;
5781         bool findNewline;
5782         int column;
5783         uint buf_len, n;
5784
5785         dp = varbuf;
5786
5787         findNewline = false;
5788         column = 0;
5789
5790         for (n = 0; n < len; n++) {
5791                 if (varbuf[n] == 0)
5792                         break;
5793                 if (varbuf[n] == '\r')
5794                         continue;
5795                 if (findNewline && varbuf[n] != '\n')
5796                         continue;
5797                 findNewline = false;
5798                 if (varbuf[n] == '#') {
5799                         findNewline = true;
5800                         continue;
5801                 }
5802                 if (varbuf[n] == '\n') {
5803                         if (column == 0)
5804                                 continue;
5805                         *dp++ = 0;
5806                         column = 0;
5807                         continue;
5808                 }
5809                 *dp++ = varbuf[n];
5810                 column++;
5811         }
5812         buf_len = dp - varbuf;
5813
5814         while (dp < varbuf + n)
5815                 *dp++ = 0;
5816
5817         return buf_len;
5818 }
5819
5820 /*
5821         EXAMPLE: nvram_array
5822         nvram_arry format:
5823         name=value
5824         Use carriage return at the end of each assignment,
5825          and an empty string with
5826         carriage return at the end of array.
5827
5828         For example:
5829         unsigned char  nvram_array[] = {"name1=value1\n",
5830         "name2=value2\n", "\n"};
5831         Hex values start with 0x, and mac addr format: xx:xx:xx:xx:xx:xx.
5832
5833         Search "EXAMPLE: nvram_array" to see how the array is activated.
5834 */
5835
5836 void dhd_bus_set_nvram_params(struct dhd_bus *bus, const char *nvram_params)
5837 {
5838         bus->nvram_params = nvram_params;
5839 }
5840
5841 static int dhdsdio_download_nvram(struct dhd_bus *bus)
5842 {
5843         int bcmerror = -1;
5844         uint len;
5845         void *image = NULL;
5846         char *memblock = NULL;
5847         char *bufp;
5848         char *nv_path;
5849         bool nvram_file_exists;
5850
5851         nv_path = bus->nv_path;
5852
5853         nvram_file_exists = ((nv_path != NULL) && (nv_path[0] != '\0'));
5854         if (!nvram_file_exists && (bus->nvram_params == NULL))
5855                 return 0;
5856
5857         if (nvram_file_exists) {
5858                 image = dhd_os_open_image(nv_path);
5859                 if (image == NULL)
5860                         goto err;
5861         }
5862
5863         memblock = kmalloc(MEMBLOCK, GFP_ATOMIC);
5864         if (memblock == NULL) {
5865                 DHD_ERROR(("%s: Failed to allocate memory %d bytes\n",
5866                            __func__, MEMBLOCK));
5867                 goto err;
5868         }
5869
5870         /* Download variables */
5871         if (nvram_file_exists) {
5872                 len = dhd_os_get_image_block(memblock, MEMBLOCK, image);
5873         } else {
5874                 len = strlen(bus->nvram_params);
5875                 ASSERT(len <= MEMBLOCK);
5876                 if (len > MEMBLOCK)
5877                         len = MEMBLOCK;
5878                 memcpy(memblock, bus->nvram_params, len);
5879         }
5880
5881         if (len > 0 && len < MEMBLOCK) {
5882                 bufp = (char *)memblock;
5883                 bufp[len] = 0;
5884                 len = process_nvram_vars(bufp, len);
5885                 bufp += len;
5886                 *bufp++ = 0;
5887                 if (len)
5888                         bcmerror = dhdsdio_downloadvars(bus, memblock, len + 1);
5889                 if (bcmerror) {
5890                         DHD_ERROR(("%s: error downloading vars: %d\n",
5891                                    __func__, bcmerror));
5892                 }
5893         } else {
5894                 DHD_ERROR(("%s: error reading nvram file: %d\n",
5895                            __func__, len));
5896                 bcmerror = BCME_SDIO_ERROR;
5897         }
5898
5899 err:
5900         if (memblock)
5901                 kfree(memblock);
5902
5903         if (image)
5904                 dhd_os_close_image(image);
5905
5906         return bcmerror;
5907 }
5908
5909 static int _dhdsdio_download_firmware(struct dhd_bus *bus)
5910 {
5911         int bcmerror = -1;
5912
5913         bool embed = false;     /* download embedded firmware */
5914         bool dlok = false;      /* download firmware succeeded */
5915
5916         /* Out immediately if no image to download */
5917         if ((bus->fw_path == NULL) || (bus->fw_path[0] == '\0')) {
5918 #ifdef BCMEMBEDIMAGE
5919                 embed = true;
5920 #else
5921                 return bcmerror;
5922 #endif
5923         }
5924
5925         /* Keep arm in reset */
5926         if (dhdsdio_download_state(bus, true)) {
5927                 DHD_ERROR(("%s: error placing ARM core in reset\n", __func__));
5928                 goto err;
5929         }
5930
5931         /* External image takes precedence if specified */
5932         if ((bus->fw_path != NULL) && (bus->fw_path[0] != '\0')) {
5933                 if (dhdsdio_download_code_file(bus, bus->fw_path)) {
5934                         DHD_ERROR(("%s: dongle image file download failed\n",
5935                                    __func__));
5936 #ifdef BCMEMBEDIMAGE
5937                         embed = true;
5938 #else
5939                         goto err;
5940 #endif
5941                 } else {
5942                         embed = false;
5943                         dlok = true;
5944                 }
5945         }
5946 #ifdef BCMEMBEDIMAGE
5947         if (embed) {
5948                 if (dhdsdio_download_code_array(bus)) {
5949                         DHD_ERROR(("%s: dongle image array download failed\n",
5950                                    __func__));
5951                         goto err;
5952                 } else {
5953                         dlok = true;
5954                 }
5955         }
5956 #endif
5957         if (!dlok) {
5958                 DHD_ERROR(("%s: dongle image download failed\n", __func__));
5959                 goto err;
5960         }
5961
5962         /* EXAMPLE: nvram_array */
5963         /* If a valid nvram_arry is specified as above, it can be passed
5964                  down to dongle */
5965         /* dhd_bus_set_nvram_params(bus, (char *)&nvram_array); */
5966
5967         /* External nvram takes precedence if specified */
5968         if (dhdsdio_download_nvram(bus)) {
5969                 DHD_ERROR(("%s: dongle nvram file download failed\n",
5970                            __func__));
5971         }
5972
5973         /* Take arm out of reset */
5974         if (dhdsdio_download_state(bus, false)) {
5975                 DHD_ERROR(("%s: error getting out of ARM core reset\n",
5976                            __func__));
5977                 goto err;
5978         }
5979
5980         bcmerror = 0;
5981
5982 err:
5983         return bcmerror;
5984 }
5985
5986 static int
5987 dhd_bcmsdh_recv_buf(dhd_bus_t *bus, u32 addr, uint fn, uint flags,
5988                     u8 *buf, uint nbytes, void *pkt,
5989                     bcmsdh_cmplt_fn_t complete, void *handle)
5990 {
5991         int status;
5992
5993         /* 4329: GSPI check */
5994         status =
5995             bcmsdh_recv_buf(bus->sdh, addr, fn, flags, buf, nbytes, pkt,
5996                             complete, handle);
5997         return status;
5998 }
5999
6000 static int
6001 dhd_bcmsdh_send_buf(dhd_bus_t *bus, u32 addr, uint fn, uint flags,
6002                     u8 *buf, uint nbytes, void *pkt,
6003                     bcmsdh_cmplt_fn_t complete, void *handle)
6004 {
6005         return bcmsdh_send_buf
6006                 (bus->sdh, addr, fn, flags, buf, nbytes, pkt, complete,
6007                  handle);
6008 }
6009
6010 uint dhd_bus_chip(struct dhd_bus *bus)
6011 {
6012         ASSERT(bus->sih != NULL);
6013         return bus->sih->chip;
6014 }
6015
6016 void *dhd_bus_pub(struct dhd_bus *bus)
6017 {
6018         return bus->dhd;
6019 }
6020
6021 void *dhd_bus_txq(struct dhd_bus *bus)
6022 {
6023         return &bus->txq;
6024 }
6025
6026 uint dhd_bus_hdrlen(struct dhd_bus *bus)
6027 {
6028         return SDPCM_HDRLEN;
6029 }
6030
6031 int dhd_bus_devreset(dhd_pub_t *dhdp, u8 flag)
6032 {
6033         int bcmerror = 0;
6034         dhd_bus_t *bus;
6035
6036         bus = dhdp->bus;
6037
6038         if (flag == true) {
6039                 if (!bus->dhd->dongle_reset) {
6040                         /* Expect app to have torn down any
6041                          connection before calling */
6042                         /* Stop the bus, disable F2 */
6043                         dhd_bus_stop(bus, false);
6044
6045                         /* Clean tx/rx buffer pointers,
6046                          detach from the dongle */
6047                         dhdsdio_release_dongle(bus, bus->dhd->osh);
6048
6049                         bus->dhd->dongle_reset = true;
6050                         bus->dhd->up = false;
6051
6052                         DHD_TRACE(("%s:  WLAN OFF DONE\n", __func__));
6053                         /* App can now remove power from device */
6054                 } else
6055                         bcmerror = BCME_SDIO_ERROR;
6056         } else {
6057                 /* App must have restored power to device before calling */
6058
6059                 DHD_TRACE(("\n\n%s: == WLAN ON ==\n", __func__));
6060
6061                 if (bus->dhd->dongle_reset) {
6062                         /* Turn on WLAN */
6063                         /* Reset SD client */
6064                         bcmsdh_reset(bus->sdh);
6065
6066                         /* Attempt to re-attach & download */
6067                         if (dhdsdio_probe_attach(bus, bus->dhd->osh, bus->sdh,
6068                                                  (u32 *) SI_ENUM_BASE,
6069                                                  bus->cl_devid)) {
6070                                 /* Attempt to download binary to the dongle */
6071                                 if (dhdsdio_probe_init
6072                                     (bus, bus->dhd->osh, bus->sdh)
6073                                     && dhdsdio_download_firmware(bus,
6074                                                                  bus->dhd->osh,
6075                                                                  bus->sdh)) {
6076
6077                                         /* Re-init bus, enable F2 transfer */
6078                                         dhd_bus_init((dhd_pub_t *) bus->dhd,
6079                                                      false);
6080
6081 #if defined(OOB_INTR_ONLY)
6082                                         dhd_enable_oob_intr(bus, true);
6083 #endif                          /* defined(OOB_INTR_ONLY) */
6084
6085                                         bus->dhd->dongle_reset = false;
6086                                         bus->dhd->up = true;
6087
6088                                         DHD_TRACE(("%s: WLAN ON DONE\n",
6089                                                    __func__));
6090                                 } else
6091                                         bcmerror = BCME_SDIO_ERROR;
6092                         } else
6093                                 bcmerror = BCME_SDIO_ERROR;
6094                 } else {
6095                         bcmerror = BCME_NOTDOWN;
6096                         DHD_ERROR(("%s: Set DEVRESET=false invoked when device "
6097                                 "is on\n", __func__));
6098                         bcmerror = BCME_SDIO_ERROR;
6099                 }
6100         }
6101         return bcmerror;
6102 }