• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (c) 2010 Broadcom Corporation
4  */
5 
6 #include <linux/types.h>
7 #include <linux/atomic.h>
8 #include <linux/kernel.h>
9 #include <linux/kthread.h>
10 #include <linux/printk.h>
11 #include <linux/pci_ids.h>
12 #include <linux/netdevice.h>
13 #include <linux/interrupt.h>
14 #include <linux/sched/signal.h>
15 #include <linux/mmc/sdio.h>
16 #include <linux/mmc/sdio_ids.h>
17 #include <linux/mmc/sdio_func.h>
18 #include <linux/mmc/card.h>
19 #include <linux/mmc/core.h>
20 #include <linux/semaphore.h>
21 #include <linux/firmware.h>
22 #include <linux/module.h>
23 #include <linux/bcma/bcma.h>
24 #include <linux/debugfs.h>
25 #include <linux/vmalloc.h>
26 #include <asm/unaligned.h>
27 #include <defs.h>
28 #include <brcmu_wifi.h>
29 #include <brcmu_utils.h>
30 #include <brcm_hw_ids.h>
31 #include <soc.h>
32 #include "sdio.h"
33 #include "chip.h"
34 #include "firmware.h"
35 #include "core.h"
36 #include "common.h"
37 #include "bcdc.h"
38 
39 #define DCMD_RESP_TIMEOUT	msecs_to_jiffies(2500)
40 #define CTL_DONE_TIMEOUT	msecs_to_jiffies(2500)
41 
42 /* watermark expressed in number of words */
43 #define DEFAULT_F2_WATERMARK    0x8
44 #define CY_4373_F2_WATERMARK    0x40
45 #define CY_4373_F1_MESBUSYCTRL  (CY_4373_F2_WATERMARK | SBSDIO_MESBUSYCTRL_ENAB)
46 #define CY_43012_F2_WATERMARK    0x60
47 #define CY_43012_MES_WATERMARK  0x50
48 #define CY_43012_MESBUSYCTRL    (CY_43012_MES_WATERMARK | \
49 				 SBSDIO_MESBUSYCTRL_ENAB)
50 #define CY_4339_F2_WATERMARK    48
51 #define CY_4339_MES_WATERMARK	80
52 #define CY_4339_MESBUSYCTRL	(CY_4339_MES_WATERMARK | \
53 				 SBSDIO_MESBUSYCTRL_ENAB)
54 #define CY_43455_F2_WATERMARK	0x60
55 #define CY_43455_MES_WATERMARK	0x50
56 #define CY_43455_MESBUSYCTRL	(CY_43455_MES_WATERMARK | \
57 				 SBSDIO_MESBUSYCTRL_ENAB)
58 #define CY_435X_F2_WATERMARK	0x40
59 #define CY_435X_F1_MESBUSYCTRL	(CY_435X_F2_WATERMARK | \
60 				 SBSDIO_MESBUSYCTRL_ENAB)
61 
62 #ifdef DEBUG
63 
64 #define BRCMF_TRAP_INFO_SIZE	80
65 
66 #define CBUF_LEN	(128)
67 
68 /* Device console log buffer state */
69 #define CONSOLE_BUFFER_MAX	2024
70 
71 struct rte_log_le {
72 	__le32 buf;		/* Can't be pointer on (64-bit) hosts */
73 	__le32 buf_size;
74 	__le32 idx;
75 	char *_buf_compat;	/* Redundant pointer for backward compat. */
76 };
77 
78 struct rte_console {
79 	/* Virtual UART
80 	 * When there is no UART (e.g. Quickturn),
81 	 * the host should write a complete
82 	 * input line directly into cbuf and then write
83 	 * the length into vcons_in.
84 	 * This may also be used when there is a real UART
85 	 * (at risk of conflicting with
86 	 * the real UART).  vcons_out is currently unused.
87 	 */
88 	uint vcons_in;
89 	uint vcons_out;
90 
91 	/* Output (logging) buffer
92 	 * Console output is written to a ring buffer log_buf at index log_idx.
93 	 * The host may read the output when it sees log_idx advance.
94 	 * Output will be lost if the output wraps around faster than the host
95 	 * polls.
96 	 */
97 	struct rte_log_le log_le;
98 
99 	/* Console input line buffer
100 	 * Characters are read one at a time into cbuf
101 	 * until <CR> is received, then
102 	 * the buffer is processed as a command line.
103 	 * Also used for virtual UART.
104 	 */
105 	uint cbuf_idx;
106 	char cbuf[CBUF_LEN];
107 };
108 
109 #endif				/* DEBUG */
110 #include <chipcommon.h>
111 
112 #include "bus.h"
113 #include "debug.h"
114 #include "tracepoint.h"
115 
116 #define TXQLEN		2048	/* bulk tx queue length */
117 #define TXHI		(TXQLEN - 256)	/* turn on flow control above TXHI */
118 #define TXLOW		(TXHI - 256)	/* turn off flow control below TXLOW */
119 #define PRIOMASK	7
120 
121 #define TXRETRIES	2	/* # of retries for tx frames */
122 
123 #define BRCMF_RXBOUND	50	/* Default for max rx frames in
124 				 one scheduling */
125 
126 #define BRCMF_TXBOUND	20	/* Default for max tx frames in
127 				 one scheduling */
128 
129 #define BRCMF_TXMINMAX	1	/* Max tx frames if rx still pending */
130 
131 #define MEMBLOCK	2048	/* Block size used for downloading
132 				 of dongle image */
133 #define MAX_DATA_BUF	(32 * 1024)	/* Must be large enough to hold
134 				 biggest possible glom */
135 
136 #define BRCMF_FIRSTREAD	(1 << 6)
137 
138 #define BRCMF_CONSOLE	10	/* watchdog interval to poll console */
139 
140 /* SBSDIO_DEVICE_CTL */
141 
142 /* 1: device will assert busy signal when receiving CMD53 */
143 #define SBSDIO_DEVCTL_SETBUSY		0x01
144 /* 1: assertion of sdio interrupt is synchronous to the sdio clock */
145 #define SBSDIO_DEVCTL_SPI_INTR_SYNC	0x02
146 /* 1: mask all interrupts to host except the chipActive (rev 8) */
147 #define SBSDIO_DEVCTL_CA_INT_ONLY	0x04
148 /* 1: isolate internal sdio signals, put external pads in tri-state; requires
149  * sdio bus power cycle to clear (rev 9) */
150 #define SBSDIO_DEVCTL_PADS_ISO		0x08
151 /* 1: enable F2 Watermark */
152 #define SBSDIO_DEVCTL_F2WM_ENAB		0x10
153 /* Force SD->SB reset mapping (rev 11) */
154 #define SBSDIO_DEVCTL_SB_RST_CTL	0x30
155 /*   Determined by CoreControl bit */
156 #define SBSDIO_DEVCTL_RST_CORECTL	0x00
157 /*   Force backplane reset */
158 #define SBSDIO_DEVCTL_RST_BPRESET	0x10
159 /*   Force no backplane reset */
160 #define SBSDIO_DEVCTL_RST_NOBPRESET	0x20
161 
162 /* direct(mapped) cis space */
163 
164 /* MAPPED common CIS address */
165 #define SBSDIO_CIS_BASE_COMMON		0x1000
166 /* maximum bytes in one CIS */
167 #define SBSDIO_CIS_SIZE_LIMIT		0x200
168 /* cis offset addr is < 17 bits */
169 #define SBSDIO_CIS_OFT_ADDR_MASK	0x1FFFF
170 
171 /* manfid tuple length, include tuple, link bytes */
172 #define SBSDIO_CIS_MANFID_TUPLE_LEN	6
173 
174 #define SD_REG(field) \
175 		(offsetof(struct sdpcmd_regs, field))
176 
177 /* SDIO function 1 register CHIPCLKCSR */
178 /* Force ALP request to backplane */
179 #define SBSDIO_FORCE_ALP		0x01
180 /* Force HT request to backplane */
181 #define SBSDIO_FORCE_HT			0x02
182 /* Force ILP request to backplane */
183 #define SBSDIO_FORCE_ILP		0x04
184 /* Make ALP ready (power up xtal) */
185 #define SBSDIO_ALP_AVAIL_REQ		0x08
186 /* Make HT ready (power up PLL) */
187 #define SBSDIO_HT_AVAIL_REQ		0x10
188 /* Squelch clock requests from HW */
189 #define SBSDIO_FORCE_HW_CLKREQ_OFF	0x20
190 /* Status: ALP is ready */
191 #define SBSDIO_ALP_AVAIL		0x40
192 /* Status: HT is ready */
193 #define SBSDIO_HT_AVAIL			0x80
194 #define SBSDIO_CSR_MASK			0x1F
195 #define SBSDIO_AVBITS		(SBSDIO_HT_AVAIL | SBSDIO_ALP_AVAIL)
196 #define SBSDIO_ALPAV(regval)	((regval) & SBSDIO_AVBITS)
197 #define SBSDIO_HTAV(regval)	(((regval) & SBSDIO_AVBITS) == SBSDIO_AVBITS)
198 #define SBSDIO_ALPONLY(regval)	(SBSDIO_ALPAV(regval) && !SBSDIO_HTAV(regval))
199 #define SBSDIO_CLKAV(regval, alponly) \
200 	(SBSDIO_ALPAV(regval) && (alponly ? 1 : SBSDIO_HTAV(regval)))
201 
202 /* intstatus */
203 #define I_SMB_SW0	(1 << 0)	/* To SB Mail S/W interrupt 0 */
204 #define I_SMB_SW1	(1 << 1)	/* To SB Mail S/W interrupt 1 */
205 #define I_SMB_SW2	(1 << 2)	/* To SB Mail S/W interrupt 2 */
206 #define I_SMB_SW3	(1 << 3)	/* To SB Mail S/W interrupt 3 */
207 #define I_SMB_SW_MASK	0x0000000f	/* To SB Mail S/W interrupts mask */
208 #define I_SMB_SW_SHIFT	0	/* To SB Mail S/W interrupts shift */
209 #define I_HMB_SW0	(1 << 4)	/* To Host Mail S/W interrupt 0 */
210 #define I_HMB_SW1	(1 << 5)	/* To Host Mail S/W interrupt 1 */
211 #define I_HMB_SW2	(1 << 6)	/* To Host Mail S/W interrupt 2 */
212 #define I_HMB_SW3	(1 << 7)	/* To Host Mail S/W interrupt 3 */
213 #define I_HMB_SW_MASK	0x000000f0	/* To Host Mail S/W interrupts mask */
214 #define I_HMB_SW_SHIFT	4	/* To Host Mail S/W interrupts shift */
215 #define I_WR_OOSYNC	(1 << 8)	/* Write Frame Out Of Sync */
216 #define I_RD_OOSYNC	(1 << 9)	/* Read Frame Out Of Sync */
217 #define	I_PC		(1 << 10)	/* descriptor error */
218 #define	I_PD		(1 << 11)	/* data error */
219 #define	I_DE		(1 << 12)	/* Descriptor protocol Error */
220 #define	I_RU		(1 << 13)	/* Receive descriptor Underflow */
221 #define	I_RO		(1 << 14)	/* Receive fifo Overflow */
222 #define	I_XU		(1 << 15)	/* Transmit fifo Underflow */
223 #define	I_RI		(1 << 16)	/* Receive Interrupt */
224 #define I_BUSPWR	(1 << 17)	/* SDIO Bus Power Change (rev 9) */
225 #define I_XMTDATA_AVAIL (1 << 23)	/* bits in fifo */
226 #define	I_XI		(1 << 24)	/* Transmit Interrupt */
227 #define I_RF_TERM	(1 << 25)	/* Read Frame Terminate */
228 #define I_WF_TERM	(1 << 26)	/* Write Frame Terminate */
229 #define I_PCMCIA_XU	(1 << 27)	/* PCMCIA Transmit FIFO Underflow */
230 #define I_SBINT		(1 << 28)	/* sbintstatus Interrupt */
231 #define I_CHIPACTIVE	(1 << 29)	/* chip from doze to active state */
232 #define I_SRESET	(1 << 30)	/* CCCR RES interrupt */
233 #define I_IOE2		(1U << 31)	/* CCCR IOE2 Bit Changed */
234 #define	I_ERRORS	(I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
235 #define I_DMA		(I_RI | I_XI | I_ERRORS)
236 
237 /* corecontrol */
238 #define CC_CISRDY		(1 << 0)	/* CIS Ready */
239 #define CC_BPRESEN		(1 << 1)	/* CCCR RES signal */
240 #define CC_F2RDY		(1 << 2)	/* set CCCR IOR2 bit */
241 #define CC_CLRPADSISO		(1 << 3)	/* clear SDIO pads isolation */
242 #define CC_XMTDATAAVAIL_MODE	(1 << 4)
243 #define CC_XMTDATAAVAIL_CTRL	(1 << 5)
244 
245 /* SDA_FRAMECTRL */
246 #define SFC_RF_TERM	(1 << 0)	/* Read Frame Terminate */
247 #define SFC_WF_TERM	(1 << 1)	/* Write Frame Terminate */
248 #define SFC_CRC4WOOS	(1 << 2)	/* CRC error for write out of sync */
249 #define SFC_ABORTALL	(1 << 3)	/* Abort all in-progress frames */
250 
251 /*
252  * Software allocation of To SB Mailbox resources
253  */
254 
255 /* tosbmailbox bits corresponding to intstatus bits */
256 #define SMB_NAK		(1 << 0)	/* Frame NAK */
257 #define SMB_INT_ACK	(1 << 1)	/* Host Interrupt ACK */
258 #define SMB_USE_OOB	(1 << 2)	/* Use OOB Wakeup */
259 #define SMB_DEV_INT	(1 << 3)	/* Miscellaneous Interrupt */
260 
261 /* tosbmailboxdata */
262 #define SMB_DATA_VERSION_SHIFT	16	/* host protocol version */
263 
264 /*
265  * Software allocation of To Host Mailbox resources
266  */
267 
268 /* intstatus bits */
269 #define I_HMB_FC_STATE	I_HMB_SW0	/* Flow Control State */
270 #define I_HMB_FC_CHANGE	I_HMB_SW1	/* Flow Control State Changed */
271 #define I_HMB_FRAME_IND	I_HMB_SW2	/* Frame Indication */
272 #define I_HMB_HOST_INT	I_HMB_SW3	/* Miscellaneous Interrupt */
273 
274 /* tohostmailboxdata */
275 #define HMB_DATA_NAKHANDLED	0x0001	/* retransmit NAK'd frame */
276 #define HMB_DATA_DEVREADY	0x0002	/* talk to host after enable */
277 #define HMB_DATA_FC		0x0004	/* per prio flowcontrol update flag */
278 #define HMB_DATA_FWREADY	0x0008	/* fw ready for protocol activity */
279 #define HMB_DATA_FWHALT		0x0010	/* firmware halted */
280 
281 #define HMB_DATA_FCDATA_MASK	0xff000000
282 #define HMB_DATA_FCDATA_SHIFT	24
283 
284 #define HMB_DATA_VERSION_MASK	0x00ff0000
285 #define HMB_DATA_VERSION_SHIFT	16
286 
287 /*
288  * Software-defined protocol header
289  */
290 
291 /* Current protocol version */
292 #define SDPCM_PROT_VERSION	4
293 
294 /*
295  * Shared structure between dongle and the host.
296  * The structure contains pointers to trap or assert information.
297  */
298 #define SDPCM_SHARED_VERSION       0x0003
299 #define SDPCM_SHARED_VERSION_MASK  0x00FF
300 #define SDPCM_SHARED_ASSERT_BUILT  0x0100
301 #define SDPCM_SHARED_ASSERT        0x0200
302 #define SDPCM_SHARED_TRAP          0x0400
303 
304 /* Space for header read, limit for data packets */
305 #define MAX_HDR_READ	(1 << 6)
306 #define MAX_RX_DATASZ	2048
307 
308 /* Bump up limit on waiting for HT to account for first startup;
309  * if the image is doing a CRC calculation before programming the PMU
310  * for HT availability, it could take a couple hundred ms more, so
311  * max out at a 1 second (1000000us).
312  */
313 #undef PMU_MAX_TRANSITION_DLY
314 #define PMU_MAX_TRANSITION_DLY 1000000
315 
316 /* Value for ChipClockCSR during initial setup */
317 #define BRCMF_INIT_CLKCTL1	(SBSDIO_FORCE_HW_CLKREQ_OFF |	\
318 					SBSDIO_ALP_AVAIL_REQ)
319 
320 /* Flags for SDH calls */
321 #define F2SYNC	(SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
322 
323 #define BRCMF_IDLE_ACTIVE	0	/* Do not request any SD clock change
324 					 * when idle
325 					 */
326 #define BRCMF_IDLE_INTERVAL	1
327 
328 #define KSO_WAIT_US 50
329 #define MAX_KSO_ATTEMPTS (PMU_MAX_TRANSITION_DLY/KSO_WAIT_US)
330 #define BRCMF_SDIO_MAX_ACCESS_ERRORS	5
331 
332 #ifdef DEBUG
333 /* Device console log buffer state */
334 struct brcmf_console {
335 	uint count;		/* Poll interval msec counter */
336 	uint log_addr;		/* Log struct address (fixed) */
337 	struct rte_log_le log_le;	/* Log struct (host copy) */
338 	uint bufsize;		/* Size of log buffer */
339 	u8 *buf;		/* Log buffer (host copy) */
340 	uint last;		/* Last buffer read index */
341 };
342 
343 struct brcmf_trap_info {
344 	__le32		type;
345 	__le32		epc;
346 	__le32		cpsr;
347 	__le32		spsr;
348 	__le32		r0;	/* a1 */
349 	__le32		r1;	/* a2 */
350 	__le32		r2;	/* a3 */
351 	__le32		r3;	/* a4 */
352 	__le32		r4;	/* v1 */
353 	__le32		r5;	/* v2 */
354 	__le32		r6;	/* v3 */
355 	__le32		r7;	/* v4 */
356 	__le32		r8;	/* v5 */
357 	__le32		r9;	/* sb/v6 */
358 	__le32		r10;	/* sl/v7 */
359 	__le32		r11;	/* fp/v8 */
360 	__le32		r12;	/* ip */
361 	__le32		r13;	/* sp */
362 	__le32		r14;	/* lr */
363 	__le32		pc;	/* r15 */
364 };
365 #endif				/* DEBUG */
366 
367 struct sdpcm_shared {
368 	u32 flags;
369 	u32 trap_addr;
370 	u32 assert_exp_addr;
371 	u32 assert_file_addr;
372 	u32 assert_line;
373 	u32 console_addr;	/* Address of struct rte_console */
374 	u32 msgtrace_addr;
375 	u8 tag[32];
376 	u32 brpt_addr;
377 };
378 
379 struct sdpcm_shared_le {
380 	__le32 flags;
381 	__le32 trap_addr;
382 	__le32 assert_exp_addr;
383 	__le32 assert_file_addr;
384 	__le32 assert_line;
385 	__le32 console_addr;	/* Address of struct rte_console */
386 	__le32 msgtrace_addr;
387 	u8 tag[32];
388 	__le32 brpt_addr;
389 };
390 
391 /* dongle SDIO bus specific header info */
392 struct brcmf_sdio_hdrinfo {
393 	u8 seq_num;
394 	u8 channel;
395 	u16 len;
396 	u16 len_left;
397 	u16 len_nxtfrm;
398 	u8 dat_offset;
399 	bool lastfrm;
400 	u16 tail_pad;
401 };
402 
403 /*
404  * hold counter variables
405  */
406 struct brcmf_sdio_count {
407 	uint intrcount;		/* Count of device interrupt callbacks */
408 	uint lastintrs;		/* Count as of last watchdog timer */
409 	uint pollcnt;		/* Count of active polls */
410 	uint regfails;		/* Count of R_REG failures */
411 	uint tx_sderrs;		/* Count of tx attempts with sd errors */
412 	uint fcqueued;		/* Tx packets that got queued */
413 	uint rxrtx;		/* Count of rtx requests (NAK to dongle) */
414 	uint rx_toolong;	/* Receive frames too long to receive */
415 	uint rxc_errors;	/* SDIO errors when reading control frames */
416 	uint rx_hdrfail;	/* SDIO errors on header reads */
417 	uint rx_badhdr;		/* Bad received headers (roosync?) */
418 	uint rx_badseq;		/* Mismatched rx sequence number */
419 	uint fc_rcvd;		/* Number of flow-control events received */
420 	uint fc_xoff;		/* Number which turned on flow-control */
421 	uint fc_xon;		/* Number which turned off flow-control */
422 	uint rxglomfail;	/* Failed deglom attempts */
423 	uint rxglomframes;	/* Number of glom frames (superframes) */
424 	uint rxglompkts;	/* Number of packets from glom frames */
425 	uint f2rxhdrs;		/* Number of header reads */
426 	uint f2rxdata;		/* Number of frame data reads */
427 	uint f2txdata;		/* Number of f2 frame writes */
428 	uint f1regdata;		/* Number of f1 register accesses */
429 	uint tickcnt;		/* Number of watchdog been schedule */
430 	ulong tx_ctlerrs;	/* Err of sending ctrl frames */
431 	ulong tx_ctlpkts;	/* Ctrl frames sent to dongle */
432 	ulong rx_ctlerrs;	/* Err of processing rx ctrl frames */
433 	ulong rx_ctlpkts;	/* Ctrl frames processed from dongle */
434 	ulong rx_readahead_cnt;	/* packets where header read-ahead was used */
435 };
436 
437 /* misc chip info needed by some of the routines */
438 /* Private data for SDIO bus interaction */
439 struct brcmf_sdio {
440 	struct brcmf_sdio_dev *sdiodev;	/* sdio device handler */
441 	struct brcmf_chip *ci;	/* Chip info struct */
442 	struct brcmf_core *sdio_core; /* sdio core info struct */
443 
444 	u32 hostintmask;	/* Copy of Host Interrupt Mask */
445 	atomic_t intstatus;	/* Intstatus bits (events) pending */
446 	atomic_t fcstate;	/* State of dongle flow-control */
447 
448 	uint blocksize;		/* Block size of SDIO transfers */
449 	uint roundup;		/* Max roundup limit */
450 
451 	struct pktq txq;	/* Queue length used for flow-control */
452 	u8 flowcontrol;	/* per prio flow control bitmask */
453 	u8 tx_seq;		/* Transmit sequence number (next) */
454 	u8 tx_max;		/* Maximum transmit sequence allowed */
455 
456 	u8 *hdrbuf;		/* buffer for handling rx frame */
457 	u8 *rxhdr;		/* Header of current rx frame (in hdrbuf) */
458 	u8 rx_seq;		/* Receive sequence number (expected) */
459 	struct brcmf_sdio_hdrinfo cur_read;
460 				/* info of current read frame */
461 	bool rxskip;		/* Skip receive (awaiting NAK ACK) */
462 	bool rxpending;		/* Data frame pending in dongle */
463 
464 	uint rxbound;		/* Rx frames to read before resched */
465 	uint txbound;		/* Tx frames to send before resched */
466 	uint txminmax;
467 
468 	struct sk_buff *glomd;	/* Packet containing glomming descriptor */
469 	struct sk_buff_head glom; /* Packet list for glommed superframe */
470 
471 	u8 *rxbuf;		/* Buffer for receiving control packets */
472 	uint rxblen;		/* Allocated length of rxbuf */
473 	u8 *rxctl;		/* Aligned pointer into rxbuf */
474 	u8 *rxctl_orig;		/* pointer for freeing rxctl */
475 	uint rxlen;		/* Length of valid data in buffer */
476 	spinlock_t rxctl_lock;	/* protection lock for ctrl frame resources */
477 
478 	u8 sdpcm_ver;	/* Bus protocol reported by dongle */
479 
480 	bool intr;		/* Use interrupts */
481 	bool poll;		/* Use polling */
482 	atomic_t ipend;		/* Device interrupt is pending */
483 	uint spurious;		/* Count of spurious interrupts */
484 	uint pollrate;		/* Ticks between device polls */
485 	uint polltick;		/* Tick counter */
486 
487 #ifdef DEBUG
488 	uint console_interval;
489 	struct brcmf_console console;	/* Console output polling support */
490 	uint console_addr;	/* Console address from shared struct */
491 #endif				/* DEBUG */
492 
493 	uint clkstate;		/* State of sd and backplane clock(s) */
494 	s32 idletime;		/* Control for activity timeout */
495 	s32 idlecount;		/* Activity timeout counter */
496 	s32 idleclock;		/* How to set bus driver when idle */
497 	bool rxflow_mode;	/* Rx flow control mode */
498 	bool rxflow;		/* Is rx flow control on */
499 	bool alp_only;		/* Don't use HT clock (ALP only) */
500 
501 	u8 *ctrl_frame_buf;
502 	u16 ctrl_frame_len;
503 	bool ctrl_frame_stat;
504 	int ctrl_frame_err;
505 
506 	spinlock_t txq_lock;		/* protect bus->txq */
507 	wait_queue_head_t ctrl_wait;
508 	wait_queue_head_t dcmd_resp_wait;
509 
510 	struct timer_list timer;
511 	struct completion watchdog_wait;
512 	struct task_struct *watchdog_tsk;
513 	bool wd_active;
514 
515 	struct workqueue_struct *brcmf_wq;
516 	struct work_struct datawork;
517 	bool dpc_triggered;
518 	bool dpc_running;
519 
520 	bool txoff;		/* Transmit flow-controlled */
521 	struct brcmf_sdio_count sdcnt;
522 	bool sr_enabled; /* SaveRestore enabled */
523 	bool sleeping;
524 
525 	u8 tx_hdrlen;		/* sdio bus header length for tx packet */
526 	bool txglom;		/* host tx glomming enable flag */
527 	u16 head_align;		/* buffer pointer alignment */
528 	u16 sgentry_align;	/* scatter-gather buffer alignment */
529 };
530 
531 /* clkstate */
532 #define CLK_NONE	0
533 #define CLK_SDONLY	1
534 #define CLK_PENDING	2
535 #define CLK_AVAIL	3
536 
537 #ifdef DEBUG
538 static int qcount[NUMPRIO];
539 #endif				/* DEBUG */
540 
541 #define DEFAULT_SDIO_DRIVE_STRENGTH	6	/* in milliamps */
542 
543 #define RETRYCHAN(chan) ((chan) == SDPCM_EVENT_CHANNEL)
544 
545 /* Limit on rounding up frames */
546 static const uint max_roundup = 512;
547 
548 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
549 #define ALIGNMENT  8
550 #else
551 #define ALIGNMENT  4
552 #endif
553 
554 enum brcmf_sdio_frmtype {
555 	BRCMF_SDIO_FT_NORMAL,
556 	BRCMF_SDIO_FT_SUPER,
557 	BRCMF_SDIO_FT_SUB,
558 };
559 
560 #define SDIOD_DRVSTR_KEY(chip, pmu)     (((unsigned int)(chip) << 16) | (pmu))
561 
562 /* SDIO Pad drive strength to select value mappings */
563 struct sdiod_drive_str {
564 	u8 strength;	/* Pad Drive Strength in mA */
565 	u8 sel;		/* Chip-specific select value */
566 };
567 
568 /* SDIO Drive Strength to sel value table for PMU Rev 11 (1.8V) */
569 static const struct sdiod_drive_str sdiod_drvstr_tab1_1v8[] = {
570 	{32, 0x6},
571 	{26, 0x7},
572 	{22, 0x4},
573 	{16, 0x5},
574 	{12, 0x2},
575 	{8, 0x3},
576 	{4, 0x0},
577 	{0, 0x1}
578 };
579 
580 /* SDIO Drive Strength to sel value table for PMU Rev 13 (1.8v) */
581 static const struct sdiod_drive_str sdiod_drive_strength_tab5_1v8[] = {
582 	{6, 0x7},
583 	{5, 0x6},
584 	{4, 0x5},
585 	{3, 0x4},
586 	{2, 0x2},
587 	{1, 0x1},
588 	{0, 0x0}
589 };
590 
591 /* SDIO Drive Strength to sel value table for PMU Rev 17 (1.8v) */
592 static const struct sdiod_drive_str sdiod_drvstr_tab6_1v8[] = {
593 	{3, 0x3},
594 	{2, 0x2},
595 	{1, 0x1},
596 	{0, 0x0} };
597 
598 /* SDIO Drive Strength to sel value table for 43143 PMU Rev 17 (3.3V) */
599 static const struct sdiod_drive_str sdiod_drvstr_tab2_3v3[] = {
600 	{16, 0x7},
601 	{12, 0x5},
602 	{8,  0x3},
603 	{4,  0x1}
604 };
605 
606 BRCMF_FW_DEF(43143, "brcmfmac43143-sdio");
607 BRCMF_FW_DEF(43241B0, "brcmfmac43241b0-sdio");
608 BRCMF_FW_DEF(43241B4, "brcmfmac43241b4-sdio");
609 BRCMF_FW_DEF(43241B5, "brcmfmac43241b5-sdio");
610 BRCMF_FW_DEF(4329, "brcmfmac4329-sdio");
611 BRCMF_FW_DEF(4330, "brcmfmac4330-sdio");
612 BRCMF_FW_DEF(4334, "brcmfmac4334-sdio");
613 BRCMF_FW_DEF(43340, "brcmfmac43340-sdio");
614 BRCMF_FW_DEF(4335, "brcmfmac4335-sdio");
615 BRCMF_FW_DEF(43362, "brcmfmac43362-sdio");
616 BRCMF_FW_DEF(4339, "brcmfmac4339-sdio");
617 BRCMF_FW_DEF(43430A0, "brcmfmac43430a0-sdio");
618 /* Note the names are not postfixed with a1 for backward compatibility */
619 BRCMF_FW_CLM_DEF(43430A1, "brcmfmac43430-sdio");
620 BRCMF_FW_DEF(43430B0, "brcmfmac43430b0-sdio");
621 BRCMF_FW_CLM_DEF(43439, "brcmfmac43439-sdio");
622 BRCMF_FW_CLM_DEF(43455, "brcmfmac43455-sdio");
623 BRCMF_FW_DEF(43456, "brcmfmac43456-sdio");
624 BRCMF_FW_CLM_DEF(4354, "brcmfmac4354-sdio");
625 BRCMF_FW_CLM_DEF(4356, "brcmfmac4356-sdio");
626 BRCMF_FW_DEF(4359, "brcmfmac4359-sdio");
627 BRCMF_FW_CLM_DEF(4373, "brcmfmac4373-sdio");
628 BRCMF_FW_CLM_DEF(43012, "brcmfmac43012-sdio");
629 BRCMF_FW_CLM_DEF(43752, "brcmfmac43752-sdio");
630 
631 /* firmware config files */
632 MODULE_FIRMWARE(BRCMF_FW_DEFAULT_PATH "brcmfmac*-sdio.*.txt");
633 
634 /* per-board firmware binaries */
635 MODULE_FIRMWARE(BRCMF_FW_DEFAULT_PATH "brcmfmac*-sdio.*.bin");
636 
637 static const struct brcmf_firmware_mapping brcmf_sdio_fwnames[] = {
638 	BRCMF_FW_ENTRY(BRCM_CC_43143_CHIP_ID, 0xFFFFFFFF, 43143),
639 	BRCMF_FW_ENTRY(BRCM_CC_43241_CHIP_ID, 0x0000001F, 43241B0),
640 	BRCMF_FW_ENTRY(BRCM_CC_43241_CHIP_ID, 0x00000020, 43241B4),
641 	BRCMF_FW_ENTRY(BRCM_CC_43241_CHIP_ID, 0xFFFFFFC0, 43241B5),
642 	BRCMF_FW_ENTRY(BRCM_CC_4329_CHIP_ID, 0xFFFFFFFF, 4329),
643 	BRCMF_FW_ENTRY(BRCM_CC_4330_CHIP_ID, 0xFFFFFFFF, 4330),
644 	BRCMF_FW_ENTRY(BRCM_CC_4334_CHIP_ID, 0xFFFFFFFF, 4334),
645 	BRCMF_FW_ENTRY(BRCM_CC_43340_CHIP_ID, 0xFFFFFFFF, 43340),
646 	BRCMF_FW_ENTRY(BRCM_CC_43341_CHIP_ID, 0xFFFFFFFF, 43340),
647 	BRCMF_FW_ENTRY(BRCM_CC_4335_CHIP_ID, 0xFFFFFFFF, 4335),
648 	BRCMF_FW_ENTRY(BRCM_CC_43362_CHIP_ID, 0xFFFFFFFE, 43362),
649 	BRCMF_FW_ENTRY(BRCM_CC_4339_CHIP_ID, 0xFFFFFFFF, 4339),
650 	BRCMF_FW_ENTRY(BRCM_CC_43430_CHIP_ID, 0x00000001, 43430A0),
651 	BRCMF_FW_ENTRY(BRCM_CC_43430_CHIP_ID, 0x00000002, 43430A1),
652 	BRCMF_FW_ENTRY(BRCM_CC_43430_CHIP_ID, 0xFFFFFFFC, 43430B0),
653 	BRCMF_FW_ENTRY(BRCM_CC_4345_CHIP_ID, 0x00000200, 43456),
654 	BRCMF_FW_ENTRY(BRCM_CC_4345_CHIP_ID, 0xFFFFFDC0, 43455),
655 	BRCMF_FW_ENTRY(BRCM_CC_43454_CHIP_ID, 0x00000040, 43455),
656 	BRCMF_FW_ENTRY(BRCM_CC_4354_CHIP_ID, 0xFFFFFFFF, 4354),
657 	BRCMF_FW_ENTRY(BRCM_CC_4356_CHIP_ID, 0xFFFFFFFF, 4356),
658 	BRCMF_FW_ENTRY(BRCM_CC_4359_CHIP_ID, 0xFFFFFFFF, 4359),
659 	BRCMF_FW_ENTRY(CY_CC_4373_CHIP_ID, 0xFFFFFFFF, 4373),
660 	BRCMF_FW_ENTRY(CY_CC_43012_CHIP_ID, 0xFFFFFFFF, 43012),
661 	BRCMF_FW_ENTRY(CY_CC_43439_CHIP_ID, 0xFFFFFFFF, 43439),
662 	BRCMF_FW_ENTRY(CY_CC_43752_CHIP_ID, 0xFFFFFFFF, 43752)
663 };
664 
665 #define TXCTL_CREDITS	2
666 
pkt_align(struct sk_buff * p,int len,int align)667 static void pkt_align(struct sk_buff *p, int len, int align)
668 {
669 	uint datalign;
670 	datalign = (unsigned long)(p->data);
671 	datalign = roundup(datalign, (align)) - datalign;
672 	if (datalign)
673 		skb_pull(p, datalign);
674 	__skb_trim(p, len);
675 }
676 
677 /* To check if there's window offered */
data_ok(struct brcmf_sdio * bus)678 static bool data_ok(struct brcmf_sdio *bus)
679 {
680 	u8 tx_rsv = 0;
681 
682 	/* Reserve TXCTL_CREDITS credits for txctl when it is ready to send */
683 	if (bus->ctrl_frame_stat)
684 		tx_rsv = TXCTL_CREDITS;
685 
686 	return (bus->tx_max - bus->tx_seq - tx_rsv) != 0 &&
687 	       ((bus->tx_max - bus->tx_seq - tx_rsv) & 0x80) == 0;
688 
689 }
690 
691 /* To check if there's window offered */
txctl_ok(struct brcmf_sdio * bus)692 static bool txctl_ok(struct brcmf_sdio *bus)
693 {
694 	return (bus->tx_max - bus->tx_seq) != 0 &&
695 	       ((bus->tx_max - bus->tx_seq) & 0x80) == 0;
696 }
697 
698 static int
brcmf_sdio_kso_control(struct brcmf_sdio * bus,bool on)699 brcmf_sdio_kso_control(struct brcmf_sdio *bus, bool on)
700 {
701 	u8 wr_val = 0, rd_val, cmp_val, bmask;
702 	int err = 0;
703 	int err_cnt = 0;
704 	int try_cnt = 0;
705 
706 	brcmf_dbg(TRACE, "Enter: on=%d\n", on);
707 
708 	sdio_retune_crc_disable(bus->sdiodev->func1);
709 
710 	/* Cannot re-tune if device is asleep; defer till we're awake */
711 	if (on)
712 		sdio_retune_hold_now(bus->sdiodev->func1);
713 
714 	wr_val = (on << SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
715 	/* 1st KSO write goes to AOS wake up core if device is asleep  */
716 	brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, wr_val, &err);
717 
718 	/* In case of 43012 chip, the chip could go down immediately after
719 	 * KSO bit is cleared. So the further reads of KSO register could
720 	 * fail. Thereby just bailing out immediately after clearing KSO
721 	 * bit, to avoid polling of KSO bit.
722 	 */
723 	if (!on && bus->ci->chip == CY_CC_43012_CHIP_ID)
724 		return err;
725 
726 	if (on) {
727 		/* device WAKEUP through KSO:
728 		 * write bit 0 & read back until
729 		 * both bits 0 (kso bit) & 1 (dev on status) are set
730 		 */
731 		cmp_val = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK |
732 			  SBSDIO_FUNC1_SLEEPCSR_DEVON_MASK;
733 		bmask = cmp_val;
734 		usleep_range(2000, 3000);
735 	} else {
736 		/* Put device to sleep, turn off KSO */
737 		cmp_val = 0;
738 		/* only check for bit0, bit1(dev on status) may not
739 		 * get cleared right away
740 		 */
741 		bmask = SBSDIO_FUNC1_SLEEPCSR_KSO_MASK;
742 	}
743 
744 	do {
745 		/* reliable KSO bit set/clr:
746 		 * the sdiod sleep write access is synced to PMU 32khz clk
747 		 * just one write attempt may fail,
748 		 * read it back until it matches written value
749 		 */
750 		rd_val = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
751 					   &err);
752 		if (!err) {
753 			if ((rd_val & bmask) == cmp_val)
754 				break;
755 			err_cnt = 0;
756 		}
757 		/* bail out upon subsequent access errors */
758 		if (err && (err_cnt++ > BRCMF_SDIO_MAX_ACCESS_ERRORS))
759 			break;
760 
761 		udelay(KSO_WAIT_US);
762 		brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, wr_val,
763 				   &err);
764 
765 	} while (try_cnt++ < MAX_KSO_ATTEMPTS);
766 
767 	if (try_cnt > 2)
768 		brcmf_dbg(SDIO, "try_cnt=%d rd_val=0x%x err=%d\n", try_cnt,
769 			  rd_val, err);
770 
771 	if (try_cnt > MAX_KSO_ATTEMPTS)
772 		brcmf_err("max tries: rd_val=0x%x err=%d\n", rd_val, err);
773 
774 	if (on)
775 		sdio_retune_release(bus->sdiodev->func1);
776 
777 	sdio_retune_crc_enable(bus->sdiodev->func1);
778 
779 	return err;
780 }
781 
782 #define HOSTINTMASK		(I_HMB_SW_MASK | I_CHIPACTIVE)
783 
784 /* Turn backplane clock on or off */
brcmf_sdio_htclk(struct brcmf_sdio * bus,bool on,bool pendok)785 static int brcmf_sdio_htclk(struct brcmf_sdio *bus, bool on, bool pendok)
786 {
787 	int err;
788 	u8 clkctl, clkreq, devctl;
789 	unsigned long timeout;
790 
791 	brcmf_dbg(SDIO, "Enter\n");
792 
793 	clkctl = 0;
794 
795 	if (bus->sr_enabled) {
796 		bus->clkstate = (on ? CLK_AVAIL : CLK_SDONLY);
797 		return 0;
798 	}
799 
800 	if (on) {
801 		/* Request HT Avail */
802 		clkreq =
803 		    bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
804 
805 		brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
806 				   clkreq, &err);
807 		if (err) {
808 			brcmf_err("HT Avail request error: %d\n", err);
809 			return -EBADE;
810 		}
811 
812 		/* Check current status */
813 		clkctl = brcmf_sdiod_readb(bus->sdiodev,
814 					   SBSDIO_FUNC1_CHIPCLKCSR, &err);
815 		if (err) {
816 			brcmf_err("HT Avail read error: %d\n", err);
817 			return -EBADE;
818 		}
819 
820 		/* Go to pending and await interrupt if appropriate */
821 		if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
822 			/* Allow only clock-available interrupt */
823 			devctl = brcmf_sdiod_readb(bus->sdiodev,
824 						   SBSDIO_DEVICE_CTL, &err);
825 			if (err) {
826 				brcmf_err("Devctl error setting CA: %d\n", err);
827 				return -EBADE;
828 			}
829 
830 			devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
831 			brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_DEVICE_CTL,
832 					   devctl, &err);
833 			brcmf_dbg(SDIO, "CLKCTL: set PENDING\n");
834 			bus->clkstate = CLK_PENDING;
835 
836 			return 0;
837 		} else if (bus->clkstate == CLK_PENDING) {
838 			/* Cancel CA-only interrupt filter */
839 			devctl = brcmf_sdiod_readb(bus->sdiodev,
840 						   SBSDIO_DEVICE_CTL, &err);
841 			devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
842 			brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_DEVICE_CTL,
843 					   devctl, &err);
844 		}
845 
846 		/* Otherwise, wait here (polling) for HT Avail */
847 		timeout = jiffies +
848 			  msecs_to_jiffies(PMU_MAX_TRANSITION_DLY/1000);
849 		while (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
850 			clkctl = brcmf_sdiod_readb(bus->sdiodev,
851 						   SBSDIO_FUNC1_CHIPCLKCSR,
852 						   &err);
853 			if (time_after(jiffies, timeout))
854 				break;
855 			else
856 				usleep_range(5000, 10000);
857 		}
858 		if (err) {
859 			brcmf_err("HT Avail request error: %d\n", err);
860 			return -EBADE;
861 		}
862 		if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
863 			brcmf_err("HT Avail timeout (%d): clkctl 0x%02x\n",
864 				  PMU_MAX_TRANSITION_DLY, clkctl);
865 			return -EBADE;
866 		}
867 
868 		/* Mark clock available */
869 		bus->clkstate = CLK_AVAIL;
870 		brcmf_dbg(SDIO, "CLKCTL: turned ON\n");
871 
872 #if defined(DEBUG)
873 		if (!bus->alp_only) {
874 			if (SBSDIO_ALPONLY(clkctl))
875 				brcmf_err("HT Clock should be on\n");
876 		}
877 #endif				/* defined (DEBUG) */
878 
879 	} else {
880 		clkreq = 0;
881 
882 		if (bus->clkstate == CLK_PENDING) {
883 			/* Cancel CA-only interrupt filter */
884 			devctl = brcmf_sdiod_readb(bus->sdiodev,
885 						   SBSDIO_DEVICE_CTL, &err);
886 			devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
887 			brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_DEVICE_CTL,
888 					   devctl, &err);
889 		}
890 
891 		bus->clkstate = CLK_SDONLY;
892 		brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
893 				   clkreq, &err);
894 		brcmf_dbg(SDIO, "CLKCTL: turned OFF\n");
895 		if (err) {
896 			brcmf_err("Failed access turning clock off: %d\n",
897 				  err);
898 			return -EBADE;
899 		}
900 	}
901 	return 0;
902 }
903 
904 /* Change idle/active SD state */
brcmf_sdio_sdclk(struct brcmf_sdio * bus,bool on)905 static int brcmf_sdio_sdclk(struct brcmf_sdio *bus, bool on)
906 {
907 	brcmf_dbg(SDIO, "Enter\n");
908 
909 	if (on)
910 		bus->clkstate = CLK_SDONLY;
911 	else
912 		bus->clkstate = CLK_NONE;
913 
914 	return 0;
915 }
916 
917 /* Transition SD and backplane clock readiness */
brcmf_sdio_clkctl(struct brcmf_sdio * bus,uint target,bool pendok)918 static int brcmf_sdio_clkctl(struct brcmf_sdio *bus, uint target, bool pendok)
919 {
920 #ifdef DEBUG
921 	uint oldstate = bus->clkstate;
922 #endif				/* DEBUG */
923 
924 	brcmf_dbg(SDIO, "Enter\n");
925 
926 	/* Early exit if we're already there */
927 	if (bus->clkstate == target)
928 		return 0;
929 
930 	switch (target) {
931 	case CLK_AVAIL:
932 		/* Make sure SD clock is available */
933 		if (bus->clkstate == CLK_NONE)
934 			brcmf_sdio_sdclk(bus, true);
935 		/* Now request HT Avail on the backplane */
936 		brcmf_sdio_htclk(bus, true, pendok);
937 		break;
938 
939 	case CLK_SDONLY:
940 		/* Remove HT request, or bring up SD clock */
941 		if (bus->clkstate == CLK_NONE)
942 			brcmf_sdio_sdclk(bus, true);
943 		else if (bus->clkstate == CLK_AVAIL)
944 			brcmf_sdio_htclk(bus, false, false);
945 		else
946 			brcmf_err("request for %d -> %d\n",
947 				  bus->clkstate, target);
948 		break;
949 
950 	case CLK_NONE:
951 		/* Make sure to remove HT request */
952 		if (bus->clkstate == CLK_AVAIL)
953 			brcmf_sdio_htclk(bus, false, false);
954 		/* Now remove the SD clock */
955 		brcmf_sdio_sdclk(bus, false);
956 		break;
957 	}
958 #ifdef DEBUG
959 	brcmf_dbg(SDIO, "%d -> %d\n", oldstate, bus->clkstate);
960 #endif				/* DEBUG */
961 
962 	return 0;
963 }
964 
965 static int
brcmf_sdio_bus_sleep(struct brcmf_sdio * bus,bool sleep,bool pendok)966 brcmf_sdio_bus_sleep(struct brcmf_sdio *bus, bool sleep, bool pendok)
967 {
968 	int err = 0;
969 	u8 clkcsr;
970 
971 	brcmf_dbg(SDIO, "Enter: request %s currently %s\n",
972 		  (sleep ? "SLEEP" : "WAKE"),
973 		  (bus->sleeping ? "SLEEP" : "WAKE"));
974 
975 	/* If SR is enabled control bus state with KSO */
976 	if (bus->sr_enabled) {
977 		/* Done if we're already in the requested state */
978 		if (sleep == bus->sleeping)
979 			goto end;
980 
981 		/* Going to sleep */
982 		if (sleep) {
983 			clkcsr = brcmf_sdiod_readb(bus->sdiodev,
984 						   SBSDIO_FUNC1_CHIPCLKCSR,
985 						   &err);
986 			if ((clkcsr & SBSDIO_CSR_MASK) == 0) {
987 				brcmf_dbg(SDIO, "no clock, set ALP\n");
988 				brcmf_sdiod_writeb(bus->sdiodev,
989 						   SBSDIO_FUNC1_CHIPCLKCSR,
990 						   SBSDIO_ALP_AVAIL_REQ, &err);
991 			}
992 			err = brcmf_sdio_kso_control(bus, false);
993 		} else {
994 			err = brcmf_sdio_kso_control(bus, true);
995 		}
996 		if (err) {
997 			brcmf_err("error while changing bus sleep state %d\n",
998 				  err);
999 			goto done;
1000 		}
1001 	}
1002 
1003 end:
1004 	/* control clocks */
1005 	if (sleep) {
1006 		if (!bus->sr_enabled)
1007 			brcmf_sdio_clkctl(bus, CLK_NONE, pendok);
1008 	} else {
1009 		brcmf_sdio_clkctl(bus, CLK_AVAIL, pendok);
1010 		brcmf_sdio_wd_timer(bus, true);
1011 	}
1012 	bus->sleeping = sleep;
1013 	brcmf_dbg(SDIO, "new state %s\n",
1014 		  (sleep ? "SLEEP" : "WAKE"));
1015 done:
1016 	brcmf_dbg(SDIO, "Exit: err=%d\n", err);
1017 	return err;
1018 
1019 }
1020 
1021 #ifdef DEBUG
brcmf_sdio_valid_shared_address(u32 addr)1022 static inline bool brcmf_sdio_valid_shared_address(u32 addr)
1023 {
1024 	return !(addr == 0 || ((~addr >> 16) & 0xffff) == (addr & 0xffff));
1025 }
1026 
brcmf_sdio_readshared(struct brcmf_sdio * bus,struct sdpcm_shared * sh)1027 static int brcmf_sdio_readshared(struct brcmf_sdio *bus,
1028 				 struct sdpcm_shared *sh)
1029 {
1030 	u32 addr = 0;
1031 	int rv;
1032 	u32 shaddr = 0;
1033 	struct sdpcm_shared_le sh_le;
1034 	__le32 addr_le;
1035 
1036 	sdio_claim_host(bus->sdiodev->func1);
1037 	brcmf_sdio_bus_sleep(bus, false, false);
1038 
1039 	/*
1040 	 * Read last word in socram to determine
1041 	 * address of sdpcm_shared structure
1042 	 */
1043 	shaddr = bus->ci->rambase + bus->ci->ramsize - 4;
1044 	if (!bus->ci->rambase && brcmf_chip_sr_capable(bus->ci))
1045 		shaddr -= bus->ci->srsize;
1046 	rv = brcmf_sdiod_ramrw(bus->sdiodev, false, shaddr,
1047 			       (u8 *)&addr_le, 4);
1048 	if (rv < 0)
1049 		goto fail;
1050 
1051 	/*
1052 	 * Check if addr is valid.
1053 	 * NVRAM length at the end of memory should have been overwritten.
1054 	 */
1055 	addr = le32_to_cpu(addr_le);
1056 	if (!brcmf_sdio_valid_shared_address(addr)) {
1057 		brcmf_err("invalid sdpcm_shared address 0x%08X\n", addr);
1058 		rv = -EINVAL;
1059 		goto fail;
1060 	}
1061 
1062 	brcmf_dbg(INFO, "sdpcm_shared address 0x%08X\n", addr);
1063 
1064 	/* Read hndrte_shared structure */
1065 	rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&sh_le,
1066 			       sizeof(struct sdpcm_shared_le));
1067 	if (rv < 0)
1068 		goto fail;
1069 
1070 	sdio_release_host(bus->sdiodev->func1);
1071 
1072 	/* Endianness */
1073 	sh->flags = le32_to_cpu(sh_le.flags);
1074 	sh->trap_addr = le32_to_cpu(sh_le.trap_addr);
1075 	sh->assert_exp_addr = le32_to_cpu(sh_le.assert_exp_addr);
1076 	sh->assert_file_addr = le32_to_cpu(sh_le.assert_file_addr);
1077 	sh->assert_line = le32_to_cpu(sh_le.assert_line);
1078 	sh->console_addr = le32_to_cpu(sh_le.console_addr);
1079 	sh->msgtrace_addr = le32_to_cpu(sh_le.msgtrace_addr);
1080 
1081 	if ((sh->flags & SDPCM_SHARED_VERSION_MASK) > SDPCM_SHARED_VERSION) {
1082 		brcmf_err("sdpcm shared version unsupported: dhd %d dongle %d\n",
1083 			  SDPCM_SHARED_VERSION,
1084 			  sh->flags & SDPCM_SHARED_VERSION_MASK);
1085 		return -EPROTO;
1086 	}
1087 	return 0;
1088 
1089 fail:
1090 	brcmf_err("unable to obtain sdpcm_shared info: rv=%d (addr=0x%x)\n",
1091 		  rv, addr);
1092 	sdio_release_host(bus->sdiodev->func1);
1093 	return rv;
1094 }
1095 
brcmf_sdio_get_console_addr(struct brcmf_sdio * bus)1096 static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1097 {
1098 	struct sdpcm_shared sh;
1099 
1100 	if (brcmf_sdio_readshared(bus, &sh) == 0)
1101 		bus->console_addr = sh.console_addr;
1102 }
1103 #else
brcmf_sdio_get_console_addr(struct brcmf_sdio * bus)1104 static void brcmf_sdio_get_console_addr(struct brcmf_sdio *bus)
1105 {
1106 }
1107 #endif /* DEBUG */
1108 
brcmf_sdio_hostmail(struct brcmf_sdio * bus)1109 static u32 brcmf_sdio_hostmail(struct brcmf_sdio *bus)
1110 {
1111 	struct brcmf_sdio_dev *sdiod = bus->sdiodev;
1112 	struct brcmf_core *core = bus->sdio_core;
1113 	u32 intstatus = 0;
1114 	u32 hmb_data;
1115 	u8 fcbits;
1116 	int ret;
1117 
1118 	brcmf_dbg(SDIO, "Enter\n");
1119 
1120 	/* Read mailbox data and ack that we did so */
1121 	hmb_data = brcmf_sdiod_readl(sdiod,
1122 				     core->base + SD_REG(tohostmailboxdata),
1123 				     &ret);
1124 
1125 	if (!ret)
1126 		brcmf_sdiod_writel(sdiod, core->base + SD_REG(tosbmailbox),
1127 				   SMB_INT_ACK, &ret);
1128 
1129 	bus->sdcnt.f1regdata += 2;
1130 
1131 	/* dongle indicates the firmware has halted/crashed */
1132 	if (hmb_data & HMB_DATA_FWHALT) {
1133 		brcmf_dbg(SDIO, "mailbox indicates firmware halted\n");
1134 		brcmf_fw_crashed(&sdiod->func1->dev);
1135 	}
1136 
1137 	/* Dongle recomposed rx frames, accept them again */
1138 	if (hmb_data & HMB_DATA_NAKHANDLED) {
1139 		brcmf_dbg(SDIO, "Dongle reports NAK handled, expect rtx of %d\n",
1140 			  bus->rx_seq);
1141 		if (!bus->rxskip)
1142 			brcmf_err("unexpected NAKHANDLED!\n");
1143 
1144 		bus->rxskip = false;
1145 		intstatus |= I_HMB_FRAME_IND;
1146 	}
1147 
1148 	/*
1149 	 * DEVREADY does not occur with gSPI.
1150 	 */
1151 	if (hmb_data & (HMB_DATA_DEVREADY | HMB_DATA_FWREADY)) {
1152 		bus->sdpcm_ver =
1153 		    (hmb_data & HMB_DATA_VERSION_MASK) >>
1154 		    HMB_DATA_VERSION_SHIFT;
1155 		if (bus->sdpcm_ver != SDPCM_PROT_VERSION)
1156 			brcmf_err("Version mismatch, dongle reports %d, "
1157 				  "expecting %d\n",
1158 				  bus->sdpcm_ver, SDPCM_PROT_VERSION);
1159 		else
1160 			brcmf_dbg(SDIO, "Dongle ready, protocol version %d\n",
1161 				  bus->sdpcm_ver);
1162 
1163 		/*
1164 		 * Retrieve console state address now that firmware should have
1165 		 * updated it.
1166 		 */
1167 		brcmf_sdio_get_console_addr(bus);
1168 	}
1169 
1170 	/*
1171 	 * Flow Control has been moved into the RX headers and this out of band
1172 	 * method isn't used any more.
1173 	 * remaining backward compatible with older dongles.
1174 	 */
1175 	if (hmb_data & HMB_DATA_FC) {
1176 		fcbits = (hmb_data & HMB_DATA_FCDATA_MASK) >>
1177 							HMB_DATA_FCDATA_SHIFT;
1178 
1179 		if (fcbits & ~bus->flowcontrol)
1180 			bus->sdcnt.fc_xoff++;
1181 
1182 		if (bus->flowcontrol & ~fcbits)
1183 			bus->sdcnt.fc_xon++;
1184 
1185 		bus->sdcnt.fc_rcvd++;
1186 		bus->flowcontrol = fcbits;
1187 	}
1188 
1189 	/* Shouldn't be any others */
1190 	if (hmb_data & ~(HMB_DATA_DEVREADY |
1191 			 HMB_DATA_NAKHANDLED |
1192 			 HMB_DATA_FC |
1193 			 HMB_DATA_FWREADY |
1194 			 HMB_DATA_FWHALT |
1195 			 HMB_DATA_FCDATA_MASK | HMB_DATA_VERSION_MASK))
1196 		brcmf_err("Unknown mailbox data content: 0x%02x\n",
1197 			  hmb_data);
1198 
1199 	return intstatus;
1200 }
1201 
brcmf_sdio_rxfail(struct brcmf_sdio * bus,bool abort,bool rtx)1202 static void brcmf_sdio_rxfail(struct brcmf_sdio *bus, bool abort, bool rtx)
1203 {
1204 	struct brcmf_sdio_dev *sdiod = bus->sdiodev;
1205 	struct brcmf_core *core = bus->sdio_core;
1206 	uint retries = 0;
1207 	u16 lastrbc;
1208 	u8 hi, lo;
1209 	int err;
1210 
1211 	brcmf_err("%sterminate frame%s\n",
1212 		  abort ? "abort command, " : "",
1213 		  rtx ? ", send NAK" : "");
1214 
1215 	if (abort)
1216 		brcmf_sdiod_abort(bus->sdiodev, bus->sdiodev->func2);
1217 
1218 	brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_FRAMECTRL, SFC_RF_TERM,
1219 			   &err);
1220 	bus->sdcnt.f1regdata++;
1221 
1222 	/* Wait until the packet has been flushed (device/FIFO stable) */
1223 	for (lastrbc = retries = 0xffff; retries > 0; retries--) {
1224 		hi = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_FUNC1_RFRAMEBCHI,
1225 				       &err);
1226 		lo = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_FUNC1_RFRAMEBCLO,
1227 				       &err);
1228 		bus->sdcnt.f1regdata += 2;
1229 
1230 		if ((hi == 0) && (lo == 0))
1231 			break;
1232 
1233 		if ((hi > (lastrbc >> 8)) && (lo > (lastrbc & 0x00ff))) {
1234 			brcmf_err("count growing: last 0x%04x now 0x%04x\n",
1235 				  lastrbc, (hi << 8) + lo);
1236 		}
1237 		lastrbc = (hi << 8) + lo;
1238 	}
1239 
1240 	if (!retries)
1241 		brcmf_err("count never zeroed: last 0x%04x\n", lastrbc);
1242 	else
1243 		brcmf_dbg(SDIO, "flush took %d iterations\n", 0xffff - retries);
1244 
1245 	if (rtx) {
1246 		bus->sdcnt.rxrtx++;
1247 		brcmf_sdiod_writel(sdiod, core->base + SD_REG(tosbmailbox),
1248 				   SMB_NAK, &err);
1249 
1250 		bus->sdcnt.f1regdata++;
1251 		if (err == 0)
1252 			bus->rxskip = true;
1253 	}
1254 
1255 	/* Clear partial in any case */
1256 	bus->cur_read.len = 0;
1257 }
1258 
brcmf_sdio_txfail(struct brcmf_sdio * bus)1259 static void brcmf_sdio_txfail(struct brcmf_sdio *bus)
1260 {
1261 	struct brcmf_sdio_dev *sdiodev = bus->sdiodev;
1262 	u8 i, hi, lo;
1263 
1264 	/* On failure, abort the command and terminate the frame */
1265 	brcmf_err("sdio error, abort command and terminate frame\n");
1266 	bus->sdcnt.tx_sderrs++;
1267 
1268 	brcmf_sdiod_abort(sdiodev, sdiodev->func2);
1269 	brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM, NULL);
1270 	bus->sdcnt.f1regdata++;
1271 
1272 	for (i = 0; i < 3; i++) {
1273 		hi = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_WFRAMEBCHI, NULL);
1274 		lo = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_WFRAMEBCLO, NULL);
1275 		bus->sdcnt.f1regdata += 2;
1276 		if ((hi == 0) && (lo == 0))
1277 			break;
1278 	}
1279 }
1280 
1281 /* return total length of buffer chain */
brcmf_sdio_glom_len(struct brcmf_sdio * bus)1282 static uint brcmf_sdio_glom_len(struct brcmf_sdio *bus)
1283 {
1284 	struct sk_buff *p;
1285 	uint total;
1286 
1287 	total = 0;
1288 	skb_queue_walk(&bus->glom, p)
1289 		total += p->len;
1290 	return total;
1291 }
1292 
brcmf_sdio_free_glom(struct brcmf_sdio * bus)1293 static void brcmf_sdio_free_glom(struct brcmf_sdio *bus)
1294 {
1295 	struct sk_buff *cur, *next;
1296 
1297 	skb_queue_walk_safe(&bus->glom, cur, next) {
1298 		skb_unlink(cur, &bus->glom);
1299 		brcmu_pkt_buf_free_skb(cur);
1300 	}
1301 }
1302 
1303 /*
1304  * brcmfmac sdio bus specific header
1305  * This is the lowest layer header wrapped on the packets transmitted between
1306  * host and WiFi dongle which contains information needed for SDIO core and
1307  * firmware
1308  *
1309  * It consists of 3 parts: hardware header, hardware extension header and
1310  * software header
1311  * hardware header (frame tag) - 4 bytes
1312  * Byte 0~1: Frame length
1313  * Byte 2~3: Checksum, bit-wise inverse of frame length
1314  * hardware extension header - 8 bytes
1315  * Tx glom mode only, N/A for Rx or normal Tx
1316  * Byte 0~1: Packet length excluding hw frame tag
1317  * Byte 2: Reserved
1318  * Byte 3: Frame flags, bit 0: last frame indication
1319  * Byte 4~5: Reserved
1320  * Byte 6~7: Tail padding length
1321  * software header - 8 bytes
1322  * Byte 0: Rx/Tx sequence number
1323  * Byte 1: 4 MSB Channel number, 4 LSB arbitrary flag
1324  * Byte 2: Length of next data frame, reserved for Tx
1325  * Byte 3: Data offset
1326  * Byte 4: Flow control bits, reserved for Tx
1327  * Byte 5: Maximum Sequence number allowed by firmware for Tx, N/A for Tx packet
1328  * Byte 6~7: Reserved
1329  */
1330 #define SDPCM_HWHDR_LEN			4
1331 #define SDPCM_HWEXT_LEN			8
1332 #define SDPCM_SWHDR_LEN			8
1333 #define SDPCM_HDRLEN			(SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN)
1334 /* software header */
1335 #define SDPCM_SEQ_MASK			0x000000ff
1336 #define SDPCM_SEQ_WRAP			256
1337 #define SDPCM_CHANNEL_MASK		0x00000f00
1338 #define SDPCM_CHANNEL_SHIFT		8
1339 #define SDPCM_CONTROL_CHANNEL		0	/* Control */
1340 #define SDPCM_EVENT_CHANNEL		1	/* Asyc Event Indication */
1341 #define SDPCM_DATA_CHANNEL		2	/* Data Xmit/Recv */
1342 #define SDPCM_GLOM_CHANNEL		3	/* Coalesced packets */
1343 #define SDPCM_TEST_CHANNEL		15	/* Test/debug packets */
1344 #define SDPCM_GLOMDESC(p)		(((u8 *)p)[1] & 0x80)
1345 #define SDPCM_NEXTLEN_MASK		0x00ff0000
1346 #define SDPCM_NEXTLEN_SHIFT		16
1347 #define SDPCM_DOFFSET_MASK		0xff000000
1348 #define SDPCM_DOFFSET_SHIFT		24
1349 #define SDPCM_FCMASK_MASK		0x000000ff
1350 #define SDPCM_WINDOW_MASK		0x0000ff00
1351 #define SDPCM_WINDOW_SHIFT		8
1352 
brcmf_sdio_getdatoffset(u8 * swheader)1353 static inline u8 brcmf_sdio_getdatoffset(u8 *swheader)
1354 {
1355 	u32 hdrvalue;
1356 	hdrvalue = le32_to_cpu(*(__le32 *)swheader);
1357 	return (u8)((hdrvalue & SDPCM_DOFFSET_MASK) >> SDPCM_DOFFSET_SHIFT);
1358 }
1359 
brcmf_sdio_fromevntchan(u8 * swheader)1360 static inline bool brcmf_sdio_fromevntchan(u8 *swheader)
1361 {
1362 	u32 hdrvalue;
1363 	u8 ret;
1364 
1365 	hdrvalue = le32_to_cpu(*(__le32 *)swheader);
1366 	ret = (u8)((hdrvalue & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT);
1367 
1368 	return (ret == SDPCM_EVENT_CHANNEL);
1369 }
1370 
brcmf_sdio_hdparse(struct brcmf_sdio * bus,u8 * header,struct brcmf_sdio_hdrinfo * rd,enum brcmf_sdio_frmtype type)1371 static int brcmf_sdio_hdparse(struct brcmf_sdio *bus, u8 *header,
1372 			      struct brcmf_sdio_hdrinfo *rd,
1373 			      enum brcmf_sdio_frmtype type)
1374 {
1375 	u16 len, checksum;
1376 	u8 rx_seq, fc, tx_seq_max;
1377 	u32 swheader;
1378 
1379 	trace_brcmf_sdpcm_hdr(SDPCM_RX, header);
1380 
1381 	/* hw header */
1382 	len = get_unaligned_le16(header);
1383 	checksum = get_unaligned_le16(header + sizeof(u16));
1384 	/* All zero means no more to read */
1385 	if (!(len | checksum)) {
1386 		bus->rxpending = false;
1387 		return -ENODATA;
1388 	}
1389 	if ((u16)(~(len ^ checksum))) {
1390 		brcmf_err("HW header checksum error\n");
1391 		bus->sdcnt.rx_badhdr++;
1392 		brcmf_sdio_rxfail(bus, false, false);
1393 		return -EIO;
1394 	}
1395 	if (len < SDPCM_HDRLEN) {
1396 		brcmf_err("HW header length error\n");
1397 		return -EPROTO;
1398 	}
1399 	if (type == BRCMF_SDIO_FT_SUPER &&
1400 	    (roundup(len, bus->blocksize) != rd->len)) {
1401 		brcmf_err("HW superframe header length error\n");
1402 		return -EPROTO;
1403 	}
1404 	if (type == BRCMF_SDIO_FT_SUB && len > rd->len) {
1405 		brcmf_err("HW subframe header length error\n");
1406 		return -EPROTO;
1407 	}
1408 	rd->len = len;
1409 
1410 	/* software header */
1411 	header += SDPCM_HWHDR_LEN;
1412 	swheader = le32_to_cpu(*(__le32 *)header);
1413 	if (type == BRCMF_SDIO_FT_SUPER && SDPCM_GLOMDESC(header)) {
1414 		brcmf_err("Glom descriptor found in superframe head\n");
1415 		rd->len = 0;
1416 		return -EINVAL;
1417 	}
1418 	rx_seq = (u8)(swheader & SDPCM_SEQ_MASK);
1419 	rd->channel = (swheader & SDPCM_CHANNEL_MASK) >> SDPCM_CHANNEL_SHIFT;
1420 	if (len > MAX_RX_DATASZ && rd->channel != SDPCM_CONTROL_CHANNEL &&
1421 	    type != BRCMF_SDIO_FT_SUPER) {
1422 		brcmf_err("HW header length too long\n");
1423 		bus->sdcnt.rx_toolong++;
1424 		brcmf_sdio_rxfail(bus, false, false);
1425 		rd->len = 0;
1426 		return -EPROTO;
1427 	}
1428 	if (type == BRCMF_SDIO_FT_SUPER && rd->channel != SDPCM_GLOM_CHANNEL) {
1429 		brcmf_err("Wrong channel for superframe\n");
1430 		rd->len = 0;
1431 		return -EINVAL;
1432 	}
1433 	if (type == BRCMF_SDIO_FT_SUB && rd->channel != SDPCM_DATA_CHANNEL &&
1434 	    rd->channel != SDPCM_EVENT_CHANNEL) {
1435 		brcmf_err("Wrong channel for subframe\n");
1436 		rd->len = 0;
1437 		return -EINVAL;
1438 	}
1439 	rd->dat_offset = brcmf_sdio_getdatoffset(header);
1440 	if (rd->dat_offset < SDPCM_HDRLEN || rd->dat_offset > rd->len) {
1441 		brcmf_err("seq %d: bad data offset\n", rx_seq);
1442 		bus->sdcnt.rx_badhdr++;
1443 		brcmf_sdio_rxfail(bus, false, false);
1444 		rd->len = 0;
1445 		return -ENXIO;
1446 	}
1447 	if (rd->seq_num != rx_seq) {
1448 		brcmf_dbg(SDIO, "seq %d, expected %d\n", rx_seq, rd->seq_num);
1449 		bus->sdcnt.rx_badseq++;
1450 		rd->seq_num = rx_seq;
1451 	}
1452 	/* no need to check the reset for subframe */
1453 	if (type == BRCMF_SDIO_FT_SUB)
1454 		return 0;
1455 	rd->len_nxtfrm = (swheader & SDPCM_NEXTLEN_MASK) >> SDPCM_NEXTLEN_SHIFT;
1456 	if (rd->len_nxtfrm << 4 > MAX_RX_DATASZ) {
1457 		/* only warm for NON glom packet */
1458 		if (rd->channel != SDPCM_GLOM_CHANNEL)
1459 			brcmf_err("seq %d: next length error\n", rx_seq);
1460 		rd->len_nxtfrm = 0;
1461 	}
1462 	swheader = le32_to_cpu(*(__le32 *)(header + 4));
1463 	fc = swheader & SDPCM_FCMASK_MASK;
1464 	if (bus->flowcontrol != fc) {
1465 		if (~bus->flowcontrol & fc)
1466 			bus->sdcnt.fc_xoff++;
1467 		if (bus->flowcontrol & ~fc)
1468 			bus->sdcnt.fc_xon++;
1469 		bus->sdcnt.fc_rcvd++;
1470 		bus->flowcontrol = fc;
1471 	}
1472 	tx_seq_max = (swheader & SDPCM_WINDOW_MASK) >> SDPCM_WINDOW_SHIFT;
1473 	if ((u8)(tx_seq_max - bus->tx_seq) > 0x40) {
1474 		brcmf_err("seq %d: max tx seq number error\n", rx_seq);
1475 		tx_seq_max = bus->tx_seq + 2;
1476 	}
1477 	bus->tx_max = tx_seq_max;
1478 
1479 	return 0;
1480 }
1481 
brcmf_sdio_update_hwhdr(u8 * header,u16 frm_length)1482 static inline void brcmf_sdio_update_hwhdr(u8 *header, u16 frm_length)
1483 {
1484 	*(__le16 *)header = cpu_to_le16(frm_length);
1485 	*(((__le16 *)header) + 1) = cpu_to_le16(~frm_length);
1486 }
1487 
brcmf_sdio_hdpack(struct brcmf_sdio * bus,u8 * header,struct brcmf_sdio_hdrinfo * hd_info)1488 static void brcmf_sdio_hdpack(struct brcmf_sdio *bus, u8 *header,
1489 			      struct brcmf_sdio_hdrinfo *hd_info)
1490 {
1491 	u32 hdrval;
1492 	u8 hdr_offset;
1493 
1494 	brcmf_sdio_update_hwhdr(header, hd_info->len);
1495 	hdr_offset = SDPCM_HWHDR_LEN;
1496 
1497 	if (bus->txglom) {
1498 		hdrval = (hd_info->len - hdr_offset) | (hd_info->lastfrm << 24);
1499 		*((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1500 		hdrval = (u16)hd_info->tail_pad << 16;
1501 		*(((__le32 *)(header + hdr_offset)) + 1) = cpu_to_le32(hdrval);
1502 		hdr_offset += SDPCM_HWEXT_LEN;
1503 	}
1504 
1505 	hdrval = hd_info->seq_num;
1506 	hdrval |= (hd_info->channel << SDPCM_CHANNEL_SHIFT) &
1507 		  SDPCM_CHANNEL_MASK;
1508 	hdrval |= (hd_info->dat_offset << SDPCM_DOFFSET_SHIFT) &
1509 		  SDPCM_DOFFSET_MASK;
1510 	*((__le32 *)(header + hdr_offset)) = cpu_to_le32(hdrval);
1511 	*(((__le32 *)(header + hdr_offset)) + 1) = 0;
1512 	trace_brcmf_sdpcm_hdr(SDPCM_TX + !!(bus->txglom), header);
1513 }
1514 
brcmf_sdio_rxglom(struct brcmf_sdio * bus,u8 rxseq)1515 static u8 brcmf_sdio_rxglom(struct brcmf_sdio *bus, u8 rxseq)
1516 {
1517 	u16 dlen, totlen;
1518 	u8 *dptr, num = 0;
1519 	u16 sublen;
1520 	struct sk_buff *pfirst, *pnext;
1521 
1522 	int errcode;
1523 	u8 doff;
1524 
1525 	struct brcmf_sdio_hdrinfo rd_new;
1526 
1527 	/* If packets, issue read(s) and send up packet chain */
1528 	/* Return sequence numbers consumed? */
1529 
1530 	brcmf_dbg(SDIO, "start: glomd %p glom %p\n",
1531 		  bus->glomd, skb_peek(&bus->glom));
1532 
1533 	/* If there's a descriptor, generate the packet chain */
1534 	if (bus->glomd) {
1535 		pfirst = pnext = NULL;
1536 		dlen = (u16) (bus->glomd->len);
1537 		dptr = bus->glomd->data;
1538 		if (!dlen || (dlen & 1)) {
1539 			brcmf_err("bad glomd len(%d), ignore descriptor\n",
1540 				  dlen);
1541 			dlen = 0;
1542 		}
1543 
1544 		for (totlen = num = 0; dlen; num++) {
1545 			/* Get (and move past) next length */
1546 			sublen = get_unaligned_le16(dptr);
1547 			dlen -= sizeof(u16);
1548 			dptr += sizeof(u16);
1549 			if ((sublen < SDPCM_HDRLEN) ||
1550 			    ((num == 0) && (sublen < (2 * SDPCM_HDRLEN)))) {
1551 				brcmf_err("descriptor len %d bad: %d\n",
1552 					  num, sublen);
1553 				pnext = NULL;
1554 				break;
1555 			}
1556 			if (sublen % bus->sgentry_align) {
1557 				brcmf_err("sublen %d not multiple of %d\n",
1558 					  sublen, bus->sgentry_align);
1559 			}
1560 			totlen += sublen;
1561 
1562 			/* For last frame, adjust read len so total
1563 				 is a block multiple */
1564 			if (!dlen) {
1565 				sublen +=
1566 				    (roundup(totlen, bus->blocksize) - totlen);
1567 				totlen = roundup(totlen, bus->blocksize);
1568 			}
1569 
1570 			/* Allocate/chain packet for next subframe */
1571 			pnext = brcmu_pkt_buf_get_skb(sublen + bus->sgentry_align);
1572 			if (pnext == NULL) {
1573 				brcmf_err("bcm_pkt_buf_get_skb failed, num %d len %d\n",
1574 					  num, sublen);
1575 				break;
1576 			}
1577 			skb_queue_tail(&bus->glom, pnext);
1578 
1579 			/* Adhere to start alignment requirements */
1580 			pkt_align(pnext, sublen, bus->sgentry_align);
1581 		}
1582 
1583 		/* If all allocations succeeded, save packet chain
1584 			 in bus structure */
1585 		if (pnext) {
1586 			brcmf_dbg(GLOM, "allocated %d-byte packet chain for %d subframes\n",
1587 				  totlen, num);
1588 			if (BRCMF_GLOM_ON() && bus->cur_read.len &&
1589 			    totlen != bus->cur_read.len) {
1590 				brcmf_dbg(GLOM, "glomdesc mismatch: nextlen %d glomdesc %d rxseq %d\n",
1591 					  bus->cur_read.len, totlen, rxseq);
1592 			}
1593 			pfirst = pnext = NULL;
1594 		} else {
1595 			brcmf_sdio_free_glom(bus);
1596 			num = 0;
1597 		}
1598 
1599 		/* Done with descriptor packet */
1600 		brcmu_pkt_buf_free_skb(bus->glomd);
1601 		bus->glomd = NULL;
1602 		bus->cur_read.len = 0;
1603 	}
1604 
1605 	/* Ok -- either we just generated a packet chain,
1606 		 or had one from before */
1607 	if (!skb_queue_empty(&bus->glom)) {
1608 		if (BRCMF_GLOM_ON()) {
1609 			brcmf_dbg(GLOM, "try superframe read, packet chain:\n");
1610 			skb_queue_walk(&bus->glom, pnext) {
1611 				brcmf_dbg(GLOM, "    %p: %p len 0x%04x (%d)\n",
1612 					  pnext, (u8 *) (pnext->data),
1613 					  pnext->len, pnext->len);
1614 			}
1615 		}
1616 
1617 		pfirst = skb_peek(&bus->glom);
1618 		dlen = (u16) brcmf_sdio_glom_len(bus);
1619 
1620 		/* Do an SDIO read for the superframe.  Configurable iovar to
1621 		 * read directly into the chained packet, or allocate a large
1622 		 * packet and copy into the chain.
1623 		 */
1624 		sdio_claim_host(bus->sdiodev->func1);
1625 		errcode = brcmf_sdiod_recv_chain(bus->sdiodev,
1626 						 &bus->glom, dlen);
1627 		sdio_release_host(bus->sdiodev->func1);
1628 		bus->sdcnt.f2rxdata++;
1629 
1630 		/* On failure, kill the superframe */
1631 		if (errcode < 0) {
1632 			brcmf_err("glom read of %d bytes failed: %d\n",
1633 				  dlen, errcode);
1634 
1635 			sdio_claim_host(bus->sdiodev->func1);
1636 			brcmf_sdio_rxfail(bus, true, false);
1637 			bus->sdcnt.rxglomfail++;
1638 			brcmf_sdio_free_glom(bus);
1639 			sdio_release_host(bus->sdiodev->func1);
1640 			return 0;
1641 		}
1642 
1643 		brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1644 				   pfirst->data, min_t(int, pfirst->len, 48),
1645 				   "SUPERFRAME:\n");
1646 
1647 		rd_new.seq_num = rxseq;
1648 		rd_new.len = dlen;
1649 		sdio_claim_host(bus->sdiodev->func1);
1650 		errcode = brcmf_sdio_hdparse(bus, pfirst->data, &rd_new,
1651 					     BRCMF_SDIO_FT_SUPER);
1652 		sdio_release_host(bus->sdiodev->func1);
1653 		bus->cur_read.len = rd_new.len_nxtfrm << 4;
1654 
1655 		/* Remove superframe header, remember offset */
1656 		skb_pull(pfirst, rd_new.dat_offset);
1657 		num = 0;
1658 
1659 		/* Validate all the subframe headers */
1660 		skb_queue_walk(&bus->glom, pnext) {
1661 			/* leave when invalid subframe is found */
1662 			if (errcode)
1663 				break;
1664 
1665 			rd_new.len = pnext->len;
1666 			rd_new.seq_num = rxseq++;
1667 			sdio_claim_host(bus->sdiodev->func1);
1668 			errcode = brcmf_sdio_hdparse(bus, pnext->data, &rd_new,
1669 						     BRCMF_SDIO_FT_SUB);
1670 			sdio_release_host(bus->sdiodev->func1);
1671 			brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1672 					   pnext->data, 32, "subframe:\n");
1673 
1674 			num++;
1675 		}
1676 
1677 		if (errcode) {
1678 			/* Terminate frame on error */
1679 			sdio_claim_host(bus->sdiodev->func1);
1680 			brcmf_sdio_rxfail(bus, true, false);
1681 			bus->sdcnt.rxglomfail++;
1682 			brcmf_sdio_free_glom(bus);
1683 			sdio_release_host(bus->sdiodev->func1);
1684 			bus->cur_read.len = 0;
1685 			return 0;
1686 		}
1687 
1688 		/* Basic SD framing looks ok - process each packet (header) */
1689 
1690 		skb_queue_walk_safe(&bus->glom, pfirst, pnext) {
1691 			dptr = (u8 *) (pfirst->data);
1692 			sublen = get_unaligned_le16(dptr);
1693 			doff = brcmf_sdio_getdatoffset(&dptr[SDPCM_HWHDR_LEN]);
1694 
1695 			brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
1696 					   dptr, pfirst->len,
1697 					   "Rx Subframe Data:\n");
1698 
1699 			__skb_trim(pfirst, sublen);
1700 			skb_pull(pfirst, doff);
1701 
1702 			if (pfirst->len == 0) {
1703 				skb_unlink(pfirst, &bus->glom);
1704 				brcmu_pkt_buf_free_skb(pfirst);
1705 				continue;
1706 			}
1707 
1708 			brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
1709 					   pfirst->data,
1710 					   min_t(int, pfirst->len, 32),
1711 					   "subframe %d to stack, %p (%p/%d) nxt/lnk %p/%p\n",
1712 					   bus->glom.qlen, pfirst, pfirst->data,
1713 					   pfirst->len, pfirst->next,
1714 					   pfirst->prev);
1715 			skb_unlink(pfirst, &bus->glom);
1716 			if (brcmf_sdio_fromevntchan(&dptr[SDPCM_HWHDR_LEN]))
1717 				brcmf_rx_event(bus->sdiodev->dev, pfirst);
1718 			else
1719 				brcmf_rx_frame(bus->sdiodev->dev, pfirst,
1720 					       false, false);
1721 			bus->sdcnt.rxglompkts++;
1722 		}
1723 
1724 		bus->sdcnt.rxglomframes++;
1725 	}
1726 	return num;
1727 }
1728 
brcmf_sdio_dcmd_resp_wait(struct brcmf_sdio * bus,uint * condition,bool * pending)1729 static int brcmf_sdio_dcmd_resp_wait(struct brcmf_sdio *bus, uint *condition,
1730 				     bool *pending)
1731 {
1732 	DECLARE_WAITQUEUE(wait, current);
1733 	int timeout = DCMD_RESP_TIMEOUT;
1734 
1735 	/* Wait until control frame is available */
1736 	add_wait_queue(&bus->dcmd_resp_wait, &wait);
1737 	set_current_state(TASK_INTERRUPTIBLE);
1738 
1739 	while (!(*condition) && (!signal_pending(current) && timeout))
1740 		timeout = schedule_timeout(timeout);
1741 
1742 	if (signal_pending(current))
1743 		*pending = true;
1744 
1745 	set_current_state(TASK_RUNNING);
1746 	remove_wait_queue(&bus->dcmd_resp_wait, &wait);
1747 
1748 	return timeout;
1749 }
1750 
brcmf_sdio_dcmd_resp_wake(struct brcmf_sdio * bus)1751 static int brcmf_sdio_dcmd_resp_wake(struct brcmf_sdio *bus)
1752 {
1753 	wake_up_interruptible(&bus->dcmd_resp_wait);
1754 
1755 	return 0;
1756 }
1757 static void
brcmf_sdio_read_control(struct brcmf_sdio * bus,u8 * hdr,uint len,uint doff)1758 brcmf_sdio_read_control(struct brcmf_sdio *bus, u8 *hdr, uint len, uint doff)
1759 {
1760 	uint rdlen, pad;
1761 	u8 *buf = NULL, *rbuf;
1762 	int sdret;
1763 
1764 	brcmf_dbg(SDIO, "Enter\n");
1765 	if (bus->rxblen)
1766 		buf = vzalloc(bus->rxblen);
1767 	if (!buf)
1768 		goto done;
1769 
1770 	rbuf = bus->rxbuf;
1771 	pad = ((unsigned long)rbuf % bus->head_align);
1772 	if (pad)
1773 		rbuf += (bus->head_align - pad);
1774 
1775 	/* Copy the already-read portion over */
1776 	memcpy(buf, hdr, BRCMF_FIRSTREAD);
1777 	if (len <= BRCMF_FIRSTREAD)
1778 		goto gotpkt;
1779 
1780 	/* Raise rdlen to next SDIO block to avoid tail command */
1781 	rdlen = len - BRCMF_FIRSTREAD;
1782 	if (bus->roundup && bus->blocksize && (rdlen > bus->blocksize)) {
1783 		pad = bus->blocksize - (rdlen % bus->blocksize);
1784 		if ((pad <= bus->roundup) && (pad < bus->blocksize) &&
1785 		    ((len + pad) < bus->sdiodev->bus_if->maxctl))
1786 			rdlen += pad;
1787 	} else if (rdlen % bus->head_align) {
1788 		rdlen += bus->head_align - (rdlen % bus->head_align);
1789 	}
1790 
1791 	/* Drop if the read is too big or it exceeds our maximum */
1792 	if ((rdlen + BRCMF_FIRSTREAD) > bus->sdiodev->bus_if->maxctl) {
1793 		brcmf_err("%d-byte control read exceeds %d-byte buffer\n",
1794 			  rdlen, bus->sdiodev->bus_if->maxctl);
1795 		brcmf_sdio_rxfail(bus, false, false);
1796 		goto done;
1797 	}
1798 
1799 	if ((len - doff) > bus->sdiodev->bus_if->maxctl) {
1800 		brcmf_err("%d-byte ctl frame (%d-byte ctl data) exceeds %d-byte limit\n",
1801 			  len, len - doff, bus->sdiodev->bus_if->maxctl);
1802 		bus->sdcnt.rx_toolong++;
1803 		brcmf_sdio_rxfail(bus, false, false);
1804 		goto done;
1805 	}
1806 
1807 	/* Read remain of frame body */
1808 	sdret = brcmf_sdiod_recv_buf(bus->sdiodev, rbuf, rdlen);
1809 	bus->sdcnt.f2rxdata++;
1810 
1811 	/* Control frame failures need retransmission */
1812 	if (sdret < 0) {
1813 		brcmf_err("read %d control bytes failed: %d\n",
1814 			  rdlen, sdret);
1815 		bus->sdcnt.rxc_errors++;
1816 		brcmf_sdio_rxfail(bus, true, true);
1817 		goto done;
1818 	} else
1819 		memcpy(buf + BRCMF_FIRSTREAD, rbuf, rdlen);
1820 
1821 gotpkt:
1822 
1823 	brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
1824 			   buf, len, "RxCtrl:\n");
1825 
1826 	/* Point to valid data and indicate its length */
1827 	spin_lock_bh(&bus->rxctl_lock);
1828 	if (bus->rxctl) {
1829 		brcmf_err("last control frame is being processed.\n");
1830 		spin_unlock_bh(&bus->rxctl_lock);
1831 		vfree(buf);
1832 		goto done;
1833 	}
1834 	bus->rxctl = buf + doff;
1835 	bus->rxctl_orig = buf;
1836 	bus->rxlen = len - doff;
1837 	spin_unlock_bh(&bus->rxctl_lock);
1838 
1839 done:
1840 	/* Awake any waiters */
1841 	brcmf_sdio_dcmd_resp_wake(bus);
1842 }
1843 
1844 /* Pad read to blocksize for efficiency */
brcmf_sdio_pad(struct brcmf_sdio * bus,u16 * pad,u16 * rdlen)1845 static void brcmf_sdio_pad(struct brcmf_sdio *bus, u16 *pad, u16 *rdlen)
1846 {
1847 	if (bus->roundup && bus->blocksize && *rdlen > bus->blocksize) {
1848 		*pad = bus->blocksize - (*rdlen % bus->blocksize);
1849 		if (*pad <= bus->roundup && *pad < bus->blocksize &&
1850 		    *rdlen + *pad + BRCMF_FIRSTREAD < MAX_RX_DATASZ)
1851 			*rdlen += *pad;
1852 	} else if (*rdlen % bus->head_align) {
1853 		*rdlen += bus->head_align - (*rdlen % bus->head_align);
1854 	}
1855 }
1856 
brcmf_sdio_readframes(struct brcmf_sdio * bus,uint maxframes)1857 static uint brcmf_sdio_readframes(struct brcmf_sdio *bus, uint maxframes)
1858 {
1859 	struct sk_buff *pkt;		/* Packet for event or data frames */
1860 	u16 pad;		/* Number of pad bytes to read */
1861 	uint rxleft = 0;	/* Remaining number of frames allowed */
1862 	int ret;		/* Return code from calls */
1863 	uint rxcount = 0;	/* Total frames read */
1864 	struct brcmf_sdio_hdrinfo *rd = &bus->cur_read, rd_new;
1865 	u8 head_read = 0;
1866 
1867 	brcmf_dbg(SDIO, "Enter\n");
1868 
1869 	/* Not finished unless we encounter no more frames indication */
1870 	bus->rxpending = true;
1871 
1872 	for (rd->seq_num = bus->rx_seq, rxleft = maxframes;
1873 	     !bus->rxskip && rxleft && bus->sdiodev->state == BRCMF_SDIOD_DATA;
1874 	     rd->seq_num++, rxleft--) {
1875 
1876 		/* Handle glomming separately */
1877 		if (bus->glomd || !skb_queue_empty(&bus->glom)) {
1878 			u8 cnt;
1879 			brcmf_dbg(GLOM, "calling rxglom: glomd %p, glom %p\n",
1880 				  bus->glomd, skb_peek(&bus->glom));
1881 			cnt = brcmf_sdio_rxglom(bus, rd->seq_num);
1882 			brcmf_dbg(GLOM, "rxglom returned %d\n", cnt);
1883 			rd->seq_num += cnt - 1;
1884 			rxleft = (rxleft > cnt) ? (rxleft - cnt) : 1;
1885 			continue;
1886 		}
1887 
1888 		rd->len_left = rd->len;
1889 		/* read header first for unknow frame length */
1890 		sdio_claim_host(bus->sdiodev->func1);
1891 		if (!rd->len) {
1892 			ret = brcmf_sdiod_recv_buf(bus->sdiodev,
1893 						   bus->rxhdr, BRCMF_FIRSTREAD);
1894 			bus->sdcnt.f2rxhdrs++;
1895 			if (ret < 0) {
1896 				brcmf_err("RXHEADER FAILED: %d\n",
1897 					  ret);
1898 				bus->sdcnt.rx_hdrfail++;
1899 				brcmf_sdio_rxfail(bus, true, true);
1900 				sdio_release_host(bus->sdiodev->func1);
1901 				continue;
1902 			}
1903 
1904 			brcmf_dbg_hex_dump(BRCMF_BYTES_ON() || BRCMF_HDRS_ON(),
1905 					   bus->rxhdr, SDPCM_HDRLEN,
1906 					   "RxHdr:\n");
1907 
1908 			if (brcmf_sdio_hdparse(bus, bus->rxhdr, rd,
1909 					       BRCMF_SDIO_FT_NORMAL)) {
1910 				sdio_release_host(bus->sdiodev->func1);
1911 				if (!bus->rxpending)
1912 					break;
1913 				else
1914 					continue;
1915 			}
1916 
1917 			if (rd->channel == SDPCM_CONTROL_CHANNEL) {
1918 				brcmf_sdio_read_control(bus, bus->rxhdr,
1919 							rd->len,
1920 							rd->dat_offset);
1921 				/* prepare the descriptor for the next read */
1922 				rd->len = rd->len_nxtfrm << 4;
1923 				rd->len_nxtfrm = 0;
1924 				/* treat all packet as event if we don't know */
1925 				rd->channel = SDPCM_EVENT_CHANNEL;
1926 				sdio_release_host(bus->sdiodev->func1);
1927 				continue;
1928 			}
1929 			rd->len_left = rd->len > BRCMF_FIRSTREAD ?
1930 				       rd->len - BRCMF_FIRSTREAD : 0;
1931 			head_read = BRCMF_FIRSTREAD;
1932 		}
1933 
1934 		brcmf_sdio_pad(bus, &pad, &rd->len_left);
1935 
1936 		pkt = brcmu_pkt_buf_get_skb(rd->len_left + head_read +
1937 					    bus->head_align);
1938 		if (!pkt) {
1939 			/* Give up on data, request rtx of events */
1940 			brcmf_err("brcmu_pkt_buf_get_skb failed\n");
1941 			brcmf_sdio_rxfail(bus, false,
1942 					    RETRYCHAN(rd->channel));
1943 			sdio_release_host(bus->sdiodev->func1);
1944 			continue;
1945 		}
1946 		skb_pull(pkt, head_read);
1947 		pkt_align(pkt, rd->len_left, bus->head_align);
1948 
1949 		ret = brcmf_sdiod_recv_pkt(bus->sdiodev, pkt);
1950 		bus->sdcnt.f2rxdata++;
1951 		sdio_release_host(bus->sdiodev->func1);
1952 
1953 		if (ret < 0) {
1954 			brcmf_err("read %d bytes from channel %d failed: %d\n",
1955 				  rd->len, rd->channel, ret);
1956 			brcmu_pkt_buf_free_skb(pkt);
1957 			sdio_claim_host(bus->sdiodev->func1);
1958 			brcmf_sdio_rxfail(bus, true,
1959 					    RETRYCHAN(rd->channel));
1960 			sdio_release_host(bus->sdiodev->func1);
1961 			continue;
1962 		}
1963 
1964 		if (head_read) {
1965 			skb_push(pkt, head_read);
1966 			memcpy(pkt->data, bus->rxhdr, head_read);
1967 			head_read = 0;
1968 		} else {
1969 			memcpy(bus->rxhdr, pkt->data, SDPCM_HDRLEN);
1970 			rd_new.seq_num = rd->seq_num;
1971 			sdio_claim_host(bus->sdiodev->func1);
1972 			if (brcmf_sdio_hdparse(bus, bus->rxhdr, &rd_new,
1973 					       BRCMF_SDIO_FT_NORMAL)) {
1974 				rd->len = 0;
1975 				brcmf_sdio_rxfail(bus, true, true);
1976 				sdio_release_host(bus->sdiodev->func1);
1977 				brcmu_pkt_buf_free_skb(pkt);
1978 				continue;
1979 			}
1980 			bus->sdcnt.rx_readahead_cnt++;
1981 			if (rd->len != roundup(rd_new.len, 16)) {
1982 				brcmf_err("frame length mismatch:read %d, should be %d\n",
1983 					  rd->len,
1984 					  roundup(rd_new.len, 16) >> 4);
1985 				rd->len = 0;
1986 				brcmf_sdio_rxfail(bus, true, true);
1987 				sdio_release_host(bus->sdiodev->func1);
1988 				brcmu_pkt_buf_free_skb(pkt);
1989 				continue;
1990 			}
1991 			sdio_release_host(bus->sdiodev->func1);
1992 			rd->len_nxtfrm = rd_new.len_nxtfrm;
1993 			rd->channel = rd_new.channel;
1994 			rd->dat_offset = rd_new.dat_offset;
1995 
1996 			brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() &&
1997 					     BRCMF_DATA_ON()) &&
1998 					   BRCMF_HDRS_ON(),
1999 					   bus->rxhdr, SDPCM_HDRLEN,
2000 					   "RxHdr:\n");
2001 
2002 			if (rd_new.channel == SDPCM_CONTROL_CHANNEL) {
2003 				brcmf_err("readahead on control packet %d?\n",
2004 					  rd_new.seq_num);
2005 				/* Force retry w/normal header read */
2006 				rd->len = 0;
2007 				sdio_claim_host(bus->sdiodev->func1);
2008 				brcmf_sdio_rxfail(bus, false, true);
2009 				sdio_release_host(bus->sdiodev->func1);
2010 				brcmu_pkt_buf_free_skb(pkt);
2011 				continue;
2012 			}
2013 		}
2014 
2015 		brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_DATA_ON(),
2016 				   pkt->data, rd->len, "Rx Data:\n");
2017 
2018 		/* Save superframe descriptor and allocate packet frame */
2019 		if (rd->channel == SDPCM_GLOM_CHANNEL) {
2020 			if (SDPCM_GLOMDESC(&bus->rxhdr[SDPCM_HWHDR_LEN])) {
2021 				brcmf_dbg(GLOM, "glom descriptor, %d bytes:\n",
2022 					  rd->len);
2023 				brcmf_dbg_hex_dump(BRCMF_GLOM_ON(),
2024 						   pkt->data, rd->len,
2025 						   "Glom Data:\n");
2026 				__skb_trim(pkt, rd->len);
2027 				skb_pull(pkt, SDPCM_HDRLEN);
2028 				bus->glomd = pkt;
2029 			} else {
2030 				brcmf_err("%s: glom superframe w/o "
2031 					  "descriptor!\n", __func__);
2032 				sdio_claim_host(bus->sdiodev->func1);
2033 				brcmf_sdio_rxfail(bus, false, false);
2034 				sdio_release_host(bus->sdiodev->func1);
2035 			}
2036 			/* prepare the descriptor for the next read */
2037 			rd->len = rd->len_nxtfrm << 4;
2038 			rd->len_nxtfrm = 0;
2039 			/* treat all packet as event if we don't know */
2040 			rd->channel = SDPCM_EVENT_CHANNEL;
2041 			continue;
2042 		}
2043 
2044 		/* Fill in packet len and prio, deliver upward */
2045 		__skb_trim(pkt, rd->len);
2046 		skb_pull(pkt, rd->dat_offset);
2047 
2048 		if (pkt->len == 0)
2049 			brcmu_pkt_buf_free_skb(pkt);
2050 		else if (rd->channel == SDPCM_EVENT_CHANNEL)
2051 			brcmf_rx_event(bus->sdiodev->dev, pkt);
2052 		else
2053 			brcmf_rx_frame(bus->sdiodev->dev, pkt,
2054 				       false, false);
2055 
2056 		/* prepare the descriptor for the next read */
2057 		rd->len = rd->len_nxtfrm << 4;
2058 		rd->len_nxtfrm = 0;
2059 		/* treat all packet as event if we don't know */
2060 		rd->channel = SDPCM_EVENT_CHANNEL;
2061 	}
2062 
2063 	rxcount = maxframes - rxleft;
2064 	/* Message if we hit the limit */
2065 	if (!rxleft)
2066 		brcmf_dbg(DATA, "hit rx limit of %d frames\n", maxframes);
2067 	else
2068 		brcmf_dbg(DATA, "processed %d frames\n", rxcount);
2069 	/* Back off rxseq if awaiting rtx, update rx_seq */
2070 	if (bus->rxskip)
2071 		rd->seq_num--;
2072 	bus->rx_seq = rd->seq_num;
2073 
2074 	return rxcount;
2075 }
2076 
2077 static void
brcmf_sdio_wait_event_wakeup(struct brcmf_sdio * bus)2078 brcmf_sdio_wait_event_wakeup(struct brcmf_sdio *bus)
2079 {
2080 	wake_up_interruptible(&bus->ctrl_wait);
2081 	return;
2082 }
2083 
brcmf_sdio_txpkt_hdalign(struct brcmf_sdio * bus,struct sk_buff * pkt)2084 static int brcmf_sdio_txpkt_hdalign(struct brcmf_sdio *bus, struct sk_buff *pkt)
2085 {
2086 	struct brcmf_bus_stats *stats;
2087 	u16 head_pad;
2088 	u8 *dat_buf;
2089 
2090 	dat_buf = (u8 *)(pkt->data);
2091 
2092 	/* Check head padding */
2093 	head_pad = ((unsigned long)dat_buf % bus->head_align);
2094 	if (head_pad) {
2095 		if (skb_headroom(pkt) < head_pad) {
2096 			stats = &bus->sdiodev->bus_if->stats;
2097 			atomic_inc(&stats->pktcowed);
2098 			if (skb_cow_head(pkt, head_pad)) {
2099 				atomic_inc(&stats->pktcow_failed);
2100 				return -ENOMEM;
2101 			}
2102 			head_pad = 0;
2103 		}
2104 		skb_push(pkt, head_pad);
2105 		dat_buf = (u8 *)(pkt->data);
2106 	}
2107 	memset(dat_buf, 0, head_pad + bus->tx_hdrlen);
2108 	return head_pad;
2109 }
2110 
2111 /*
2112  * struct brcmf_skbuff_cb reserves first two bytes in sk_buff::cb for
2113  * bus layer usage.
2114  */
2115 /* flag marking a dummy skb added for DMA alignment requirement */
2116 #define ALIGN_SKB_FLAG		0x8000
2117 /* bit mask of data length chopped from the previous packet */
2118 #define ALIGN_SKB_CHOP_LEN_MASK	0x7fff
2119 
brcmf_sdio_txpkt_prep_sg(struct brcmf_sdio * bus,struct sk_buff_head * pktq,struct sk_buff * pkt,u16 total_len)2120 static int brcmf_sdio_txpkt_prep_sg(struct brcmf_sdio *bus,
2121 				    struct sk_buff_head *pktq,
2122 				    struct sk_buff *pkt, u16 total_len)
2123 {
2124 	struct brcmf_sdio_dev *sdiodev;
2125 	struct sk_buff *pkt_pad;
2126 	u16 tail_pad, tail_chop, chain_pad;
2127 	unsigned int blksize;
2128 	bool lastfrm;
2129 	int ntail, ret;
2130 
2131 	sdiodev = bus->sdiodev;
2132 	blksize = sdiodev->func2->cur_blksize;
2133 	/* sg entry alignment should be a divisor of block size */
2134 	WARN_ON(blksize % bus->sgentry_align);
2135 
2136 	/* Check tail padding */
2137 	lastfrm = skb_queue_is_last(pktq, pkt);
2138 	tail_pad = 0;
2139 	tail_chop = pkt->len % bus->sgentry_align;
2140 	if (tail_chop)
2141 		tail_pad = bus->sgentry_align - tail_chop;
2142 	chain_pad = (total_len + tail_pad) % blksize;
2143 	if (lastfrm && chain_pad)
2144 		tail_pad += blksize - chain_pad;
2145 	if (skb_tailroom(pkt) < tail_pad && pkt->len > blksize) {
2146 		pkt_pad = brcmu_pkt_buf_get_skb(tail_pad + tail_chop +
2147 						bus->head_align);
2148 		if (pkt_pad == NULL)
2149 			return -ENOMEM;
2150 		ret = brcmf_sdio_txpkt_hdalign(bus, pkt_pad);
2151 		if (unlikely(ret < 0)) {
2152 			kfree_skb(pkt_pad);
2153 			return ret;
2154 		}
2155 		memcpy(pkt_pad->data,
2156 		       pkt->data + pkt->len - tail_chop,
2157 		       tail_chop);
2158 		*(u16 *)(pkt_pad->cb) = ALIGN_SKB_FLAG + tail_chop;
2159 		skb_trim(pkt, pkt->len - tail_chop);
2160 		skb_trim(pkt_pad, tail_pad + tail_chop);
2161 		__skb_queue_after(pktq, pkt, pkt_pad);
2162 	} else {
2163 		ntail = pkt->data_len + tail_pad -
2164 			(pkt->end - pkt->tail);
2165 		if (skb_cloned(pkt) || ntail > 0)
2166 			if (pskb_expand_head(pkt, 0, ntail, GFP_ATOMIC))
2167 				return -ENOMEM;
2168 		if (skb_linearize(pkt))
2169 			return -ENOMEM;
2170 		__skb_put(pkt, tail_pad);
2171 	}
2172 
2173 	return tail_pad;
2174 }
2175 
2176 /**
2177  * brcmf_sdio_txpkt_prep - packet preparation for transmit
2178  * @bus: brcmf_sdio structure pointer
2179  * @pktq: packet list pointer
2180  * @chan: virtual channel to transmit the packet
2181  *
2182  * Processes to be applied to the packet
2183  *	- Align data buffer pointer
2184  *	- Align data buffer length
2185  *	- Prepare header
2186  * Return: negative value if there is error
2187  */
2188 static int
brcmf_sdio_txpkt_prep(struct brcmf_sdio * bus,struct sk_buff_head * pktq,uint chan)2189 brcmf_sdio_txpkt_prep(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2190 		      uint chan)
2191 {
2192 	u16 head_pad, total_len;
2193 	struct sk_buff *pkt_next;
2194 	u8 txseq;
2195 	int ret;
2196 	struct brcmf_sdio_hdrinfo hd_info = {0};
2197 
2198 	txseq = bus->tx_seq;
2199 	total_len = 0;
2200 	skb_queue_walk(pktq, pkt_next) {
2201 		/* alignment packet inserted in previous
2202 		 * loop cycle can be skipped as it is
2203 		 * already properly aligned and does not
2204 		 * need an sdpcm header.
2205 		 */
2206 		if (*(u16 *)(pkt_next->cb) & ALIGN_SKB_FLAG)
2207 			continue;
2208 
2209 		/* align packet data pointer */
2210 		ret = brcmf_sdio_txpkt_hdalign(bus, pkt_next);
2211 		if (ret < 0)
2212 			return ret;
2213 		head_pad = (u16)ret;
2214 		if (head_pad)
2215 			memset(pkt_next->data + bus->tx_hdrlen, 0, head_pad);
2216 
2217 		total_len += pkt_next->len;
2218 
2219 		hd_info.len = pkt_next->len;
2220 		hd_info.lastfrm = skb_queue_is_last(pktq, pkt_next);
2221 		if (bus->txglom && pktq->qlen > 1) {
2222 			ret = brcmf_sdio_txpkt_prep_sg(bus, pktq,
2223 						       pkt_next, total_len);
2224 			if (ret < 0)
2225 				return ret;
2226 			hd_info.tail_pad = (u16)ret;
2227 			total_len += (u16)ret;
2228 		}
2229 
2230 		hd_info.channel = chan;
2231 		hd_info.dat_offset = head_pad + bus->tx_hdrlen;
2232 		hd_info.seq_num = txseq++;
2233 
2234 		/* Now fill the header */
2235 		brcmf_sdio_hdpack(bus, pkt_next->data, &hd_info);
2236 
2237 		if (BRCMF_BYTES_ON() &&
2238 		    ((BRCMF_CTL_ON() && chan == SDPCM_CONTROL_CHANNEL) ||
2239 		     (BRCMF_DATA_ON() && chan != SDPCM_CONTROL_CHANNEL)))
2240 			brcmf_dbg_hex_dump(true, pkt_next->data, hd_info.len,
2241 					   "Tx Frame:\n");
2242 		else if (BRCMF_HDRS_ON())
2243 			brcmf_dbg_hex_dump(true, pkt_next->data,
2244 					   head_pad + bus->tx_hdrlen,
2245 					   "Tx Header:\n");
2246 	}
2247 	/* Hardware length tag of the first packet should be total
2248 	 * length of the chain (including padding)
2249 	 */
2250 	if (bus->txglom)
2251 		brcmf_sdio_update_hwhdr(__skb_peek(pktq)->data, total_len);
2252 	return 0;
2253 }
2254 
2255 /**
2256  * brcmf_sdio_txpkt_postp - packet post processing for transmit
2257  * @bus: brcmf_sdio structure pointer
2258  * @pktq: packet list pointer
2259  *
2260  * Processes to be applied to the packet
2261  *	- Remove head padding
2262  *	- Remove tail padding
2263  */
2264 static void
brcmf_sdio_txpkt_postp(struct brcmf_sdio * bus,struct sk_buff_head * pktq)2265 brcmf_sdio_txpkt_postp(struct brcmf_sdio *bus, struct sk_buff_head *pktq)
2266 {
2267 	u8 *hdr;
2268 	u32 dat_offset;
2269 	u16 tail_pad;
2270 	u16 dummy_flags, chop_len;
2271 	struct sk_buff *pkt_next, *tmp, *pkt_prev;
2272 
2273 	skb_queue_walk_safe(pktq, pkt_next, tmp) {
2274 		dummy_flags = *(u16 *)(pkt_next->cb);
2275 		if (dummy_flags & ALIGN_SKB_FLAG) {
2276 			chop_len = dummy_flags & ALIGN_SKB_CHOP_LEN_MASK;
2277 			if (chop_len) {
2278 				pkt_prev = pkt_next->prev;
2279 				skb_put(pkt_prev, chop_len);
2280 			}
2281 			__skb_unlink(pkt_next, pktq);
2282 			brcmu_pkt_buf_free_skb(pkt_next);
2283 		} else {
2284 			hdr = pkt_next->data + bus->tx_hdrlen - SDPCM_SWHDR_LEN;
2285 			dat_offset = le32_to_cpu(*(__le32 *)hdr);
2286 			dat_offset = (dat_offset & SDPCM_DOFFSET_MASK) >>
2287 				     SDPCM_DOFFSET_SHIFT;
2288 			skb_pull(pkt_next, dat_offset);
2289 			if (bus->txglom) {
2290 				tail_pad = le16_to_cpu(*(__le16 *)(hdr - 2));
2291 				skb_trim(pkt_next, pkt_next->len - tail_pad);
2292 			}
2293 		}
2294 	}
2295 }
2296 
2297 /* Writes a HW/SW header into the packet and sends it. */
2298 /* Assumes: (a) header space already there, (b) caller holds lock */
brcmf_sdio_txpkt(struct brcmf_sdio * bus,struct sk_buff_head * pktq,uint chan)2299 static int brcmf_sdio_txpkt(struct brcmf_sdio *bus, struct sk_buff_head *pktq,
2300 			    uint chan)
2301 {
2302 	int ret;
2303 	struct sk_buff *pkt_next, *tmp;
2304 
2305 	brcmf_dbg(TRACE, "Enter\n");
2306 
2307 	ret = brcmf_sdio_txpkt_prep(bus, pktq, chan);
2308 	if (ret)
2309 		goto done;
2310 
2311 	sdio_claim_host(bus->sdiodev->func1);
2312 	ret = brcmf_sdiod_send_pkt(bus->sdiodev, pktq);
2313 	bus->sdcnt.f2txdata++;
2314 
2315 	if (ret < 0)
2316 		brcmf_sdio_txfail(bus);
2317 
2318 	sdio_release_host(bus->sdiodev->func1);
2319 
2320 done:
2321 	brcmf_sdio_txpkt_postp(bus, pktq);
2322 	if (ret == 0)
2323 		bus->tx_seq = (bus->tx_seq + pktq->qlen) % SDPCM_SEQ_WRAP;
2324 	skb_queue_walk_safe(pktq, pkt_next, tmp) {
2325 		__skb_unlink(pkt_next, pktq);
2326 		brcmf_proto_bcdc_txcomplete(bus->sdiodev->dev, pkt_next,
2327 					    ret == 0);
2328 	}
2329 	return ret;
2330 }
2331 
brcmf_sdio_sendfromq(struct brcmf_sdio * bus,uint maxframes)2332 static uint brcmf_sdio_sendfromq(struct brcmf_sdio *bus, uint maxframes)
2333 {
2334 	struct sk_buff *pkt;
2335 	struct sk_buff_head pktq;
2336 	u32 intstat_addr = bus->sdio_core->base + SD_REG(intstatus);
2337 	u32 intstatus = 0;
2338 	int ret = 0, prec_out, i;
2339 	uint cnt = 0;
2340 	u8 tx_prec_map, pkt_num;
2341 
2342 	brcmf_dbg(TRACE, "Enter\n");
2343 
2344 	tx_prec_map = ~bus->flowcontrol;
2345 
2346 	/* Send frames until the limit or some other event */
2347 	for (cnt = 0; (cnt < maxframes) && data_ok(bus);) {
2348 		pkt_num = 1;
2349 		if (bus->txglom)
2350 			pkt_num = min_t(u8, bus->tx_max - bus->tx_seq,
2351 					bus->sdiodev->txglomsz);
2352 		pkt_num = min_t(u32, pkt_num,
2353 				brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol));
2354 		__skb_queue_head_init(&pktq);
2355 		spin_lock_bh(&bus->txq_lock);
2356 		for (i = 0; i < pkt_num; i++) {
2357 			pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map,
2358 					      &prec_out);
2359 			if (pkt == NULL)
2360 				break;
2361 			__skb_queue_tail(&pktq, pkt);
2362 		}
2363 		spin_unlock_bh(&bus->txq_lock);
2364 		if (i == 0)
2365 			break;
2366 
2367 		ret = brcmf_sdio_txpkt(bus, &pktq, SDPCM_DATA_CHANNEL);
2368 
2369 		cnt += i;
2370 
2371 		/* In poll mode, need to check for other events */
2372 		if (!bus->intr) {
2373 			/* Check device status, signal pending interrupt */
2374 			sdio_claim_host(bus->sdiodev->func1);
2375 			intstatus = brcmf_sdiod_readl(bus->sdiodev,
2376 						      intstat_addr, &ret);
2377 			sdio_release_host(bus->sdiodev->func1);
2378 
2379 			bus->sdcnt.f2txdata++;
2380 			if (ret != 0)
2381 				break;
2382 			if (intstatus & bus->hostintmask)
2383 				atomic_set(&bus->ipend, 1);
2384 		}
2385 	}
2386 
2387 	/* Deflow-control stack if needed */
2388 	if ((bus->sdiodev->state == BRCMF_SDIOD_DATA) &&
2389 	    bus->txoff && (pktq_len(&bus->txq) < TXLOW)) {
2390 		bus->txoff = false;
2391 		brcmf_proto_bcdc_txflowblock(bus->sdiodev->dev, false);
2392 	}
2393 
2394 	return cnt;
2395 }
2396 
brcmf_sdio_tx_ctrlframe(struct brcmf_sdio * bus,u8 * frame,u16 len)2397 static int brcmf_sdio_tx_ctrlframe(struct brcmf_sdio *bus, u8 *frame, u16 len)
2398 {
2399 	u8 doff;
2400 	u16 pad;
2401 	uint retries = 0;
2402 	struct brcmf_sdio_hdrinfo hd_info = {0};
2403 	int ret;
2404 
2405 	brcmf_dbg(SDIO, "Enter\n");
2406 
2407 	/* Back the pointer to make room for bus header */
2408 	frame -= bus->tx_hdrlen;
2409 	len += bus->tx_hdrlen;
2410 
2411 	/* Add alignment padding (optional for ctl frames) */
2412 	doff = ((unsigned long)frame % bus->head_align);
2413 	if (doff) {
2414 		frame -= doff;
2415 		len += doff;
2416 		memset(frame + bus->tx_hdrlen, 0, doff);
2417 	}
2418 
2419 	/* Round send length to next SDIO block */
2420 	pad = 0;
2421 	if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
2422 		pad = bus->blocksize - (len % bus->blocksize);
2423 		if ((pad > bus->roundup) || (pad >= bus->blocksize))
2424 			pad = 0;
2425 	} else if (len % bus->head_align) {
2426 		pad = bus->head_align - (len % bus->head_align);
2427 	}
2428 	len += pad;
2429 
2430 	hd_info.len = len - pad;
2431 	hd_info.channel = SDPCM_CONTROL_CHANNEL;
2432 	hd_info.dat_offset = doff + bus->tx_hdrlen;
2433 	hd_info.seq_num = bus->tx_seq;
2434 	hd_info.lastfrm = true;
2435 	hd_info.tail_pad = pad;
2436 	brcmf_sdio_hdpack(bus, frame, &hd_info);
2437 
2438 	if (bus->txglom)
2439 		brcmf_sdio_update_hwhdr(frame, len);
2440 
2441 	brcmf_dbg_hex_dump(BRCMF_BYTES_ON() && BRCMF_CTL_ON(),
2442 			   frame, len, "Tx Frame:\n");
2443 	brcmf_dbg_hex_dump(!(BRCMF_BYTES_ON() && BRCMF_CTL_ON()) &&
2444 			   BRCMF_HDRS_ON(),
2445 			   frame, min_t(u16, len, 16), "TxHdr:\n");
2446 
2447 	do {
2448 		ret = brcmf_sdiod_send_buf(bus->sdiodev, frame, len);
2449 
2450 		if (ret < 0)
2451 			brcmf_sdio_txfail(bus);
2452 		else
2453 			bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQ_WRAP;
2454 	} while (ret < 0 && retries++ < TXRETRIES);
2455 
2456 	return ret;
2457 }
2458 
brcmf_chip_is_ulp(struct brcmf_chip * ci)2459 static bool brcmf_chip_is_ulp(struct brcmf_chip *ci)
2460 {
2461 	if (ci->chip == CY_CC_43012_CHIP_ID)
2462 		return true;
2463 	else
2464 		return false;
2465 }
2466 
brcmf_sdio_bus_stop(struct device * dev)2467 static void brcmf_sdio_bus_stop(struct device *dev)
2468 {
2469 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2470 	struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2471 	struct brcmf_sdio *bus = sdiodev->bus;
2472 	struct brcmf_core *core = bus->sdio_core;
2473 	u32 local_hostintmask;
2474 	u8 saveclk, bpreq;
2475 	int err;
2476 
2477 	brcmf_dbg(TRACE, "Enter\n");
2478 
2479 	if (bus->watchdog_tsk) {
2480 		send_sig(SIGTERM, bus->watchdog_tsk, 1);
2481 		kthread_stop(bus->watchdog_tsk);
2482 		bus->watchdog_tsk = NULL;
2483 	}
2484 
2485 	if (sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
2486 		sdio_claim_host(sdiodev->func1);
2487 
2488 		/* Enable clock for device interrupts */
2489 		brcmf_sdio_bus_sleep(bus, false, false);
2490 
2491 		/* Disable and clear interrupts at the chip level also */
2492 		brcmf_sdiod_writel(sdiodev, core->base + SD_REG(hostintmask),
2493 				   0, NULL);
2494 
2495 		local_hostintmask = bus->hostintmask;
2496 		bus->hostintmask = 0;
2497 
2498 		/* Force backplane clocks to assure F2 interrupt propagates */
2499 		saveclk = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
2500 					    &err);
2501 		if (!err) {
2502 			bpreq = saveclk;
2503 			bpreq |= brcmf_chip_is_ulp(bus->ci) ?
2504 				SBSDIO_HT_AVAIL_REQ : SBSDIO_FORCE_HT;
2505 			brcmf_sdiod_writeb(sdiodev,
2506 					   SBSDIO_FUNC1_CHIPCLKCSR,
2507 					   bpreq, &err);
2508 		}
2509 		if (err)
2510 			brcmf_err("Failed to force clock for F2: err %d\n",
2511 				  err);
2512 
2513 		/* Turn off the bus (F2), free any pending packets */
2514 		brcmf_dbg(INTR, "disable SDIO interrupts\n");
2515 		sdio_disable_func(sdiodev->func2);
2516 
2517 		/* Clear any pending interrupts now that F2 is disabled */
2518 		brcmf_sdiod_writel(sdiodev, core->base + SD_REG(intstatus),
2519 				   local_hostintmask, NULL);
2520 
2521 		sdio_release_host(sdiodev->func1);
2522 	}
2523 	/* Clear the data packet queues */
2524 	brcmu_pktq_flush(&bus->txq, true, NULL, NULL);
2525 
2526 	/* Clear any held glomming stuff */
2527 	brcmu_pkt_buf_free_skb(bus->glomd);
2528 	brcmf_sdio_free_glom(bus);
2529 
2530 	/* Clear rx control and wake any waiters */
2531 	spin_lock_bh(&bus->rxctl_lock);
2532 	bus->rxlen = 0;
2533 	spin_unlock_bh(&bus->rxctl_lock);
2534 	brcmf_sdio_dcmd_resp_wake(bus);
2535 
2536 	/* Reset some F2 state stuff */
2537 	bus->rxskip = false;
2538 	bus->tx_seq = bus->rx_seq = 0;
2539 }
2540 
brcmf_sdio_clrintr(struct brcmf_sdio * bus)2541 static inline void brcmf_sdio_clrintr(struct brcmf_sdio *bus)
2542 {
2543 	struct brcmf_sdio_dev *sdiodev;
2544 	unsigned long flags;
2545 
2546 	sdiodev = bus->sdiodev;
2547 	if (sdiodev->oob_irq_requested) {
2548 		spin_lock_irqsave(&sdiodev->irq_en_lock, flags);
2549 		if (!sdiodev->irq_en && !atomic_read(&bus->ipend)) {
2550 			enable_irq(sdiodev->settings->bus.sdio.oob_irq_nr);
2551 			sdiodev->irq_en = true;
2552 		}
2553 		spin_unlock_irqrestore(&sdiodev->irq_en_lock, flags);
2554 	}
2555 }
2556 
brcmf_sdio_intr_rstatus(struct brcmf_sdio * bus)2557 static int brcmf_sdio_intr_rstatus(struct brcmf_sdio *bus)
2558 {
2559 	struct brcmf_core *core = bus->sdio_core;
2560 	u32 addr;
2561 	unsigned long val;
2562 	int ret;
2563 
2564 	addr = core->base + SD_REG(intstatus);
2565 
2566 	val = brcmf_sdiod_readl(bus->sdiodev, addr, &ret);
2567 	bus->sdcnt.f1regdata++;
2568 	if (ret != 0)
2569 		return ret;
2570 
2571 	val &= bus->hostintmask;
2572 	atomic_set(&bus->fcstate, !!(val & I_HMB_FC_STATE));
2573 
2574 	/* Clear interrupts */
2575 	if (val) {
2576 		brcmf_sdiod_writel(bus->sdiodev, addr, val, &ret);
2577 		bus->sdcnt.f1regdata++;
2578 		atomic_or(val, &bus->intstatus);
2579 	}
2580 
2581 	return ret;
2582 }
2583 
brcmf_sdio_dpc(struct brcmf_sdio * bus)2584 static void brcmf_sdio_dpc(struct brcmf_sdio *bus)
2585 {
2586 	struct brcmf_sdio_dev *sdiod = bus->sdiodev;
2587 	u32 newstatus = 0;
2588 	u32 intstat_addr = bus->sdio_core->base + SD_REG(intstatus);
2589 	unsigned long intstatus;
2590 	uint txlimit = bus->txbound;	/* Tx frames to send before resched */
2591 	uint framecnt;			/* Temporary counter of tx/rx frames */
2592 	int err = 0;
2593 
2594 	brcmf_dbg(SDIO, "Enter\n");
2595 
2596 	sdio_claim_host(bus->sdiodev->func1);
2597 
2598 	/* If waiting for HTAVAIL, check status */
2599 	if (!bus->sr_enabled && bus->clkstate == CLK_PENDING) {
2600 		u8 clkctl, devctl = 0;
2601 
2602 #ifdef DEBUG
2603 		/* Check for inconsistent device control */
2604 		devctl = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_DEVICE_CTL,
2605 					   &err);
2606 #endif				/* DEBUG */
2607 
2608 		/* Read CSR, if clock on switch to AVAIL, else ignore */
2609 		clkctl = brcmf_sdiod_readb(bus->sdiodev,
2610 					   SBSDIO_FUNC1_CHIPCLKCSR, &err);
2611 
2612 		brcmf_dbg(SDIO, "DPC: PENDING, devctl 0x%02x clkctl 0x%02x\n",
2613 			  devctl, clkctl);
2614 
2615 		if (SBSDIO_HTAV(clkctl)) {
2616 			devctl = brcmf_sdiod_readb(bus->sdiodev,
2617 						   SBSDIO_DEVICE_CTL, &err);
2618 			devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
2619 			brcmf_sdiod_writeb(bus->sdiodev,
2620 					   SBSDIO_DEVICE_CTL, devctl, &err);
2621 			bus->clkstate = CLK_AVAIL;
2622 		}
2623 	}
2624 
2625 	/* Make sure backplane clock is on */
2626 	brcmf_sdio_bus_sleep(bus, false, true);
2627 
2628 	/* Pending interrupt indicates new device status */
2629 	if (atomic_read(&bus->ipend) > 0) {
2630 		atomic_set(&bus->ipend, 0);
2631 		err = brcmf_sdio_intr_rstatus(bus);
2632 	}
2633 
2634 	/* Start with leftover status bits */
2635 	intstatus = atomic_xchg(&bus->intstatus, 0);
2636 
2637 	/* Handle flow-control change: read new state in case our ack
2638 	 * crossed another change interrupt.  If change still set, assume
2639 	 * FC ON for safety, let next loop through do the debounce.
2640 	 */
2641 	if (intstatus & I_HMB_FC_CHANGE) {
2642 		intstatus &= ~I_HMB_FC_CHANGE;
2643 		brcmf_sdiod_writel(sdiod, intstat_addr, I_HMB_FC_CHANGE, &err);
2644 
2645 		newstatus = brcmf_sdiod_readl(sdiod, intstat_addr, &err);
2646 
2647 		bus->sdcnt.f1regdata += 2;
2648 		atomic_set(&bus->fcstate,
2649 			   !!(newstatus & (I_HMB_FC_STATE | I_HMB_FC_CHANGE)));
2650 		intstatus |= (newstatus & bus->hostintmask);
2651 	}
2652 
2653 	/* Handle host mailbox indication */
2654 	if (intstatus & I_HMB_HOST_INT) {
2655 		intstatus &= ~I_HMB_HOST_INT;
2656 		intstatus |= brcmf_sdio_hostmail(bus);
2657 	}
2658 
2659 	sdio_release_host(bus->sdiodev->func1);
2660 
2661 	/* Generally don't ask for these, can get CRC errors... */
2662 	if (intstatus & I_WR_OOSYNC) {
2663 		brcmf_err("Dongle reports WR_OOSYNC\n");
2664 		intstatus &= ~I_WR_OOSYNC;
2665 	}
2666 
2667 	if (intstatus & I_RD_OOSYNC) {
2668 		brcmf_err("Dongle reports RD_OOSYNC\n");
2669 		intstatus &= ~I_RD_OOSYNC;
2670 	}
2671 
2672 	if (intstatus & I_SBINT) {
2673 		brcmf_err("Dongle reports SBINT\n");
2674 		intstatus &= ~I_SBINT;
2675 	}
2676 
2677 	/* Would be active due to wake-wlan in gSPI */
2678 	if (intstatus & I_CHIPACTIVE) {
2679 		brcmf_dbg(SDIO, "Dongle reports CHIPACTIVE\n");
2680 		intstatus &= ~I_CHIPACTIVE;
2681 	}
2682 
2683 	/* Ignore frame indications if rxskip is set */
2684 	if (bus->rxskip)
2685 		intstatus &= ~I_HMB_FRAME_IND;
2686 
2687 	/* On frame indication, read available frames */
2688 	if ((intstatus & I_HMB_FRAME_IND) && (bus->clkstate == CLK_AVAIL)) {
2689 		brcmf_sdio_readframes(bus, bus->rxbound);
2690 		if (!bus->rxpending)
2691 			intstatus &= ~I_HMB_FRAME_IND;
2692 	}
2693 
2694 	/* Keep still-pending events for next scheduling */
2695 	if (intstatus)
2696 		atomic_or(intstatus, &bus->intstatus);
2697 
2698 	brcmf_sdio_clrintr(bus);
2699 
2700 	if (bus->ctrl_frame_stat && (bus->clkstate == CLK_AVAIL) &&
2701 	    txctl_ok(bus)) {
2702 		sdio_claim_host(bus->sdiodev->func1);
2703 		if (bus->ctrl_frame_stat) {
2704 			err = brcmf_sdio_tx_ctrlframe(bus,  bus->ctrl_frame_buf,
2705 						      bus->ctrl_frame_len);
2706 			bus->ctrl_frame_err = err;
2707 			wmb();
2708 			bus->ctrl_frame_stat = false;
2709 			if (err)
2710 				brcmf_err("sdio ctrlframe tx failed err=%d\n",
2711 					  err);
2712 		}
2713 		sdio_release_host(bus->sdiodev->func1);
2714 		brcmf_sdio_wait_event_wakeup(bus);
2715 	}
2716 	/* Send queued frames (limit 1 if rx may still be pending) */
2717 	if ((bus->clkstate == CLK_AVAIL) && !atomic_read(&bus->fcstate) &&
2718 	    brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) && txlimit &&
2719 	    data_ok(bus)) {
2720 		framecnt = bus->rxpending ? min(txlimit, bus->txminmax) :
2721 					    txlimit;
2722 		brcmf_sdio_sendfromq(bus, framecnt);
2723 	}
2724 
2725 	if ((bus->sdiodev->state != BRCMF_SDIOD_DATA) || (err != 0)) {
2726 		brcmf_err("failed backplane access over SDIO, halting operation\n");
2727 		atomic_set(&bus->intstatus, 0);
2728 		if (bus->ctrl_frame_stat) {
2729 			sdio_claim_host(bus->sdiodev->func1);
2730 			if (bus->ctrl_frame_stat) {
2731 				bus->ctrl_frame_err = -ENODEV;
2732 				wmb();
2733 				bus->ctrl_frame_stat = false;
2734 				brcmf_sdio_wait_event_wakeup(bus);
2735 			}
2736 			sdio_release_host(bus->sdiodev->func1);
2737 		}
2738 	} else if (atomic_read(&bus->intstatus) ||
2739 		   atomic_read(&bus->ipend) > 0 ||
2740 		   (!atomic_read(&bus->fcstate) &&
2741 		    brcmu_pktq_mlen(&bus->txq, ~bus->flowcontrol) &&
2742 		    data_ok(bus))) {
2743 		bus->dpc_triggered = true;
2744 	}
2745 }
2746 
brcmf_sdio_bus_gettxq(struct device * dev)2747 static struct pktq *brcmf_sdio_bus_gettxq(struct device *dev)
2748 {
2749 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2750 	struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2751 	struct brcmf_sdio *bus = sdiodev->bus;
2752 
2753 	return &bus->txq;
2754 }
2755 
brcmf_sdio_prec_enq(struct pktq * q,struct sk_buff * pkt,int prec)2756 static bool brcmf_sdio_prec_enq(struct pktq *q, struct sk_buff *pkt, int prec)
2757 {
2758 	struct sk_buff *p;
2759 	int eprec = -1;		/* precedence to evict from */
2760 
2761 	/* Fast case, precedence queue is not full and we are also not
2762 	 * exceeding total queue length
2763 	 */
2764 	if (!pktq_pfull(q, prec) && !pktq_full(q)) {
2765 		brcmu_pktq_penq(q, prec, pkt);
2766 		return true;
2767 	}
2768 
2769 	/* Determine precedence from which to evict packet, if any */
2770 	if (pktq_pfull(q, prec)) {
2771 		eprec = prec;
2772 	} else if (pktq_full(q)) {
2773 		p = brcmu_pktq_peek_tail(q, &eprec);
2774 		if (eprec > prec)
2775 			return false;
2776 	}
2777 
2778 	/* Evict if needed */
2779 	if (eprec >= 0) {
2780 		/* Detect queueing to unconfigured precedence */
2781 		if (eprec == prec)
2782 			return false;	/* refuse newer (incoming) packet */
2783 		/* Evict packet according to discard policy */
2784 		p = brcmu_pktq_pdeq_tail(q, eprec);
2785 		if (p == NULL)
2786 			brcmf_err("brcmu_pktq_pdeq_tail() failed\n");
2787 		brcmu_pkt_buf_free_skb(p);
2788 	}
2789 
2790 	/* Enqueue */
2791 	p = brcmu_pktq_penq(q, prec, pkt);
2792 	if (p == NULL)
2793 		brcmf_err("brcmu_pktq_penq() failed\n");
2794 
2795 	return p != NULL;
2796 }
2797 
brcmf_sdio_bus_txdata(struct device * dev,struct sk_buff * pkt)2798 static int brcmf_sdio_bus_txdata(struct device *dev, struct sk_buff *pkt)
2799 {
2800 	int ret = -EBADE;
2801 	uint prec;
2802 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2803 	struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2804 	struct brcmf_sdio *bus = sdiodev->bus;
2805 
2806 	brcmf_dbg(TRACE, "Enter: pkt: data %p len %d\n", pkt->data, pkt->len);
2807 	if (sdiodev->state != BRCMF_SDIOD_DATA)
2808 		return -EIO;
2809 
2810 	/* Add space for the header */
2811 	skb_push(pkt, bus->tx_hdrlen);
2812 	/* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
2813 
2814 	/* In WLAN, priority is always set by the AP using WMM parameters
2815 	 * and this need not always follow the standard 802.1d priority.
2816 	 * Based on AP WMM config, map from 802.1d priority to corresponding
2817 	 * precedence level.
2818 	 */
2819 	prec = brcmf_map_prio_to_prec(bus_if->drvr->config,
2820 				      (pkt->priority & PRIOMASK));
2821 
2822 	/* Check for existing queue, current flow-control,
2823 			 pending event, or pending clock */
2824 	brcmf_dbg(TRACE, "deferring pktq len %d\n", pktq_len(&bus->txq));
2825 	bus->sdcnt.fcqueued++;
2826 
2827 	/* Priority based enq */
2828 	spin_lock_bh(&bus->txq_lock);
2829 	/* reset bus_flags in packet cb */
2830 	*(u16 *)(pkt->cb) = 0;
2831 	if (!brcmf_sdio_prec_enq(&bus->txq, pkt, prec)) {
2832 		skb_pull(pkt, bus->tx_hdrlen);
2833 		brcmf_err("out of bus->txq !!!\n");
2834 		ret = -ENOSR;
2835 	} else {
2836 		ret = 0;
2837 	}
2838 
2839 	if (pktq_len(&bus->txq) >= TXHI) {
2840 		bus->txoff = true;
2841 		brcmf_proto_bcdc_txflowblock(dev, true);
2842 	}
2843 	spin_unlock_bh(&bus->txq_lock);
2844 
2845 #ifdef DEBUG
2846 	if (pktq_plen(&bus->txq, prec) > qcount[prec])
2847 		qcount[prec] = pktq_plen(&bus->txq, prec);
2848 #endif
2849 
2850 	brcmf_sdio_trigger_dpc(bus);
2851 	return ret;
2852 }
2853 
2854 #ifdef DEBUG
2855 #define CONSOLE_LINE_MAX	192
2856 
brcmf_sdio_readconsole(struct brcmf_sdio * bus)2857 static int brcmf_sdio_readconsole(struct brcmf_sdio *bus)
2858 {
2859 	struct brcmf_console *c = &bus->console;
2860 	u8 line[CONSOLE_LINE_MAX], ch;
2861 	u32 n, idx, addr;
2862 	int rv;
2863 
2864 	/* Don't do anything until FWREADY updates console address */
2865 	if (bus->console_addr == 0)
2866 		return 0;
2867 
2868 	/* Read console log struct */
2869 	addr = bus->console_addr + offsetof(struct rte_console, log_le);
2870 	rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, (u8 *)&c->log_le,
2871 			       sizeof(c->log_le));
2872 	if (rv < 0)
2873 		return rv;
2874 
2875 	/* Allocate console buffer (one time only) */
2876 	if (c->buf == NULL) {
2877 		c->bufsize = le32_to_cpu(c->log_le.buf_size);
2878 		c->buf = kmalloc(c->bufsize, GFP_ATOMIC);
2879 		if (c->buf == NULL)
2880 			return -ENOMEM;
2881 	}
2882 
2883 	idx = le32_to_cpu(c->log_le.idx);
2884 
2885 	/* Protect against corrupt value */
2886 	if (idx > c->bufsize)
2887 		return -EBADE;
2888 
2889 	/* Skip reading the console buffer if the index pointer
2890 	 has not moved */
2891 	if (idx == c->last)
2892 		return 0;
2893 
2894 	/* Read the console buffer */
2895 	addr = le32_to_cpu(c->log_le.buf);
2896 	rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr, c->buf, c->bufsize);
2897 	if (rv < 0)
2898 		return rv;
2899 
2900 	while (c->last != idx) {
2901 		for (n = 0; n < CONSOLE_LINE_MAX - 2; n++) {
2902 			if (c->last == idx) {
2903 				/* This would output a partial line.
2904 				 * Instead, back up
2905 				 * the buffer pointer and output this
2906 				 * line next time around.
2907 				 */
2908 				if (c->last >= n)
2909 					c->last -= n;
2910 				else
2911 					c->last = c->bufsize - n;
2912 				goto break2;
2913 			}
2914 			ch = c->buf[c->last];
2915 			c->last = (c->last + 1) % c->bufsize;
2916 			if (ch == '\n')
2917 				break;
2918 			line[n] = ch;
2919 		}
2920 
2921 		if (n > 0) {
2922 			if (line[n - 1] == '\r')
2923 				n--;
2924 			line[n] = 0;
2925 			pr_debug("CONSOLE: %s\n", line);
2926 		}
2927 	}
2928 break2:
2929 
2930 	return 0;
2931 }
2932 #endif				/* DEBUG */
2933 
2934 static int
brcmf_sdio_bus_txctl(struct device * dev,unsigned char * msg,uint msglen)2935 brcmf_sdio_bus_txctl(struct device *dev, unsigned char *msg, uint msglen)
2936 {
2937 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
2938 	struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
2939 	struct brcmf_sdio *bus = sdiodev->bus;
2940 	int ret;
2941 
2942 	brcmf_dbg(TRACE, "Enter\n");
2943 	if (sdiodev->state != BRCMF_SDIOD_DATA)
2944 		return -EIO;
2945 
2946 	/* Send from dpc */
2947 	bus->ctrl_frame_buf = msg;
2948 	bus->ctrl_frame_len = msglen;
2949 	wmb();
2950 	bus->ctrl_frame_stat = true;
2951 
2952 	brcmf_sdio_trigger_dpc(bus);
2953 	wait_event_interruptible_timeout(bus->ctrl_wait, !bus->ctrl_frame_stat,
2954 					 CTL_DONE_TIMEOUT);
2955 	ret = 0;
2956 	if (bus->ctrl_frame_stat) {
2957 		sdio_claim_host(bus->sdiodev->func1);
2958 		if (bus->ctrl_frame_stat) {
2959 			brcmf_dbg(SDIO, "ctrl_frame timeout\n");
2960 			bus->ctrl_frame_stat = false;
2961 			ret = -ETIMEDOUT;
2962 		}
2963 		sdio_release_host(bus->sdiodev->func1);
2964 	}
2965 	if (!ret) {
2966 		brcmf_dbg(SDIO, "ctrl_frame complete, err=%d\n",
2967 			  bus->ctrl_frame_err);
2968 		rmb();
2969 		ret = bus->ctrl_frame_err;
2970 	}
2971 
2972 	if (ret)
2973 		bus->sdcnt.tx_ctlerrs++;
2974 	else
2975 		bus->sdcnt.tx_ctlpkts++;
2976 
2977 	return ret;
2978 }
2979 
2980 #ifdef DEBUG
brcmf_sdio_dump_console(struct seq_file * seq,struct brcmf_sdio * bus,struct sdpcm_shared * sh)2981 static int brcmf_sdio_dump_console(struct seq_file *seq, struct brcmf_sdio *bus,
2982 				   struct sdpcm_shared *sh)
2983 {
2984 	u32 addr, console_ptr, console_size, console_index;
2985 	char *conbuf = NULL;
2986 	__le32 sh_val;
2987 	int rv;
2988 
2989 	/* obtain console information from device memory */
2990 	addr = sh->console_addr + offsetof(struct rte_console, log_le);
2991 	rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2992 			       (u8 *)&sh_val, sizeof(u32));
2993 	if (rv < 0)
2994 		return rv;
2995 	console_ptr = le32_to_cpu(sh_val);
2996 
2997 	addr = sh->console_addr + offsetof(struct rte_console, log_le.buf_size);
2998 	rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
2999 			       (u8 *)&sh_val, sizeof(u32));
3000 	if (rv < 0)
3001 		return rv;
3002 	console_size = le32_to_cpu(sh_val);
3003 
3004 	addr = sh->console_addr + offsetof(struct rte_console, log_le.idx);
3005 	rv = brcmf_sdiod_ramrw(bus->sdiodev, false, addr,
3006 			       (u8 *)&sh_val, sizeof(u32));
3007 	if (rv < 0)
3008 		return rv;
3009 	console_index = le32_to_cpu(sh_val);
3010 
3011 	/* allocate buffer for console data */
3012 	if (console_size <= CONSOLE_BUFFER_MAX)
3013 		conbuf = vzalloc(console_size+1);
3014 
3015 	if (!conbuf)
3016 		return -ENOMEM;
3017 
3018 	/* obtain the console data from device */
3019 	conbuf[console_size] = '\0';
3020 	rv = brcmf_sdiod_ramrw(bus->sdiodev, false, console_ptr, (u8 *)conbuf,
3021 			       console_size);
3022 	if (rv < 0)
3023 		goto done;
3024 
3025 	rv = seq_write(seq, conbuf + console_index,
3026 		       console_size - console_index);
3027 	if (rv < 0)
3028 		goto done;
3029 
3030 	if (console_index > 0)
3031 		rv = seq_write(seq, conbuf, console_index - 1);
3032 
3033 done:
3034 	vfree(conbuf);
3035 	return rv;
3036 }
3037 
brcmf_sdio_trap_info(struct seq_file * seq,struct brcmf_sdio * bus,struct sdpcm_shared * sh)3038 static int brcmf_sdio_trap_info(struct seq_file *seq, struct brcmf_sdio *bus,
3039 				struct sdpcm_shared *sh)
3040 {
3041 	int error;
3042 	struct brcmf_trap_info tr;
3043 
3044 	if ((sh->flags & SDPCM_SHARED_TRAP) == 0) {
3045 		brcmf_dbg(INFO, "no trap in firmware\n");
3046 		return 0;
3047 	}
3048 
3049 	error = brcmf_sdiod_ramrw(bus->sdiodev, false, sh->trap_addr, (u8 *)&tr,
3050 				  sizeof(struct brcmf_trap_info));
3051 	if (error < 0)
3052 		return error;
3053 
3054 	if (seq)
3055 		seq_printf(seq,
3056 			   "dongle trap info: type 0x%x @ epc 0x%08x\n"
3057 			   "  cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
3058 			   "  lr   0x%08x pc   0x%08x offset 0x%x\n"
3059 			   "  r0   0x%08x r1   0x%08x r2 0x%08x r3 0x%08x\n"
3060 			   "  r4   0x%08x r5   0x%08x r6 0x%08x r7 0x%08x\n",
3061 			   le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
3062 			   le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
3063 			   le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
3064 			   le32_to_cpu(tr.pc), sh->trap_addr,
3065 			   le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
3066 			   le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
3067 			   le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
3068 			   le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
3069 	else
3070 		pr_debug("dongle trap info: type 0x%x @ epc 0x%08x\n"
3071 			 "  cpsr 0x%08x spsr 0x%08x sp 0x%08x\n"
3072 			 "  lr   0x%08x pc   0x%08x offset 0x%x\n"
3073 			 "  r0   0x%08x r1   0x%08x r2 0x%08x r3 0x%08x\n"
3074 			 "  r4   0x%08x r5   0x%08x r6 0x%08x r7 0x%08x\n",
3075 			 le32_to_cpu(tr.type), le32_to_cpu(tr.epc),
3076 			 le32_to_cpu(tr.cpsr), le32_to_cpu(tr.spsr),
3077 			 le32_to_cpu(tr.r13), le32_to_cpu(tr.r14),
3078 			 le32_to_cpu(tr.pc), sh->trap_addr,
3079 			 le32_to_cpu(tr.r0), le32_to_cpu(tr.r1),
3080 			 le32_to_cpu(tr.r2), le32_to_cpu(tr.r3),
3081 			 le32_to_cpu(tr.r4), le32_to_cpu(tr.r5),
3082 			 le32_to_cpu(tr.r6), le32_to_cpu(tr.r7));
3083 	return 0;
3084 }
3085 
brcmf_sdio_assert_info(struct seq_file * seq,struct brcmf_sdio * bus,struct sdpcm_shared * sh)3086 static int brcmf_sdio_assert_info(struct seq_file *seq, struct brcmf_sdio *bus,
3087 				  struct sdpcm_shared *sh)
3088 {
3089 	int error = 0;
3090 	char file[80] = "?";
3091 	char expr[80] = "<???>";
3092 
3093 	if ((sh->flags & SDPCM_SHARED_ASSERT_BUILT) == 0) {
3094 		brcmf_dbg(INFO, "firmware not built with -assert\n");
3095 		return 0;
3096 	} else if ((sh->flags & SDPCM_SHARED_ASSERT) == 0) {
3097 		brcmf_dbg(INFO, "no assert in dongle\n");
3098 		return 0;
3099 	}
3100 
3101 	sdio_claim_host(bus->sdiodev->func1);
3102 	if (sh->assert_file_addr != 0) {
3103 		error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3104 					  sh->assert_file_addr, (u8 *)file, 80);
3105 		if (error < 0)
3106 			return error;
3107 	}
3108 	if (sh->assert_exp_addr != 0) {
3109 		error = brcmf_sdiod_ramrw(bus->sdiodev, false,
3110 					  sh->assert_exp_addr, (u8 *)expr, 80);
3111 		if (error < 0)
3112 			return error;
3113 	}
3114 	sdio_release_host(bus->sdiodev->func1);
3115 
3116 	seq_printf(seq, "dongle assert: %s:%d: assert(%s)\n",
3117 		   file, sh->assert_line, expr);
3118 	return 0;
3119 }
3120 
brcmf_sdio_checkdied(struct brcmf_sdio * bus)3121 static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
3122 {
3123 	int error;
3124 	struct sdpcm_shared sh;
3125 
3126 	error = brcmf_sdio_readshared(bus, &sh);
3127 
3128 	if (error < 0)
3129 		return error;
3130 
3131 	if ((sh.flags & SDPCM_SHARED_ASSERT_BUILT) == 0)
3132 		brcmf_dbg(INFO, "firmware not built with -assert\n");
3133 	else if (sh.flags & SDPCM_SHARED_ASSERT)
3134 		brcmf_err("assertion in dongle\n");
3135 
3136 	if (sh.flags & SDPCM_SHARED_TRAP) {
3137 		brcmf_err("firmware trap in dongle\n");
3138 		brcmf_sdio_trap_info(NULL, bus, &sh);
3139 	}
3140 
3141 	return 0;
3142 }
3143 
brcmf_sdio_died_dump(struct seq_file * seq,struct brcmf_sdio * bus)3144 static int brcmf_sdio_died_dump(struct seq_file *seq, struct brcmf_sdio *bus)
3145 {
3146 	int error = 0;
3147 	struct sdpcm_shared sh;
3148 
3149 	error = brcmf_sdio_readshared(bus, &sh);
3150 	if (error < 0)
3151 		goto done;
3152 
3153 	error = brcmf_sdio_assert_info(seq, bus, &sh);
3154 	if (error < 0)
3155 		goto done;
3156 
3157 	error = brcmf_sdio_trap_info(seq, bus, &sh);
3158 	if (error < 0)
3159 		goto done;
3160 
3161 	error = brcmf_sdio_dump_console(seq, bus, &sh);
3162 
3163 done:
3164 	return error;
3165 }
3166 
brcmf_sdio_forensic_read(struct seq_file * seq,void * data)3167 static int brcmf_sdio_forensic_read(struct seq_file *seq, void *data)
3168 {
3169 	struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3170 	struct brcmf_sdio *bus = bus_if->bus_priv.sdio->bus;
3171 
3172 	return brcmf_sdio_died_dump(seq, bus);
3173 }
3174 
brcmf_debugfs_sdio_count_read(struct seq_file * seq,void * data)3175 static int brcmf_debugfs_sdio_count_read(struct seq_file *seq, void *data)
3176 {
3177 	struct brcmf_bus *bus_if = dev_get_drvdata(seq->private);
3178 	struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3179 	struct brcmf_sdio_count *sdcnt = &sdiodev->bus->sdcnt;
3180 
3181 	seq_printf(seq,
3182 		   "intrcount:    %u\nlastintrs:    %u\n"
3183 		   "pollcnt:      %u\nregfails:     %u\n"
3184 		   "tx_sderrs:    %u\nfcqueued:     %u\n"
3185 		   "rxrtx:        %u\nrx_toolong:   %u\n"
3186 		   "rxc_errors:   %u\nrx_hdrfail:   %u\n"
3187 		   "rx_badhdr:    %u\nrx_badseq:    %u\n"
3188 		   "fc_rcvd:      %u\nfc_xoff:      %u\n"
3189 		   "fc_xon:       %u\nrxglomfail:   %u\n"
3190 		   "rxglomframes: %u\nrxglompkts:   %u\n"
3191 		   "f2rxhdrs:     %u\nf2rxdata:     %u\n"
3192 		   "f2txdata:     %u\nf1regdata:    %u\n"
3193 		   "tickcnt:      %u\ntx_ctlerrs:   %lu\n"
3194 		   "tx_ctlpkts:   %lu\nrx_ctlerrs:   %lu\n"
3195 		   "rx_ctlpkts:   %lu\nrx_readahead: %lu\n",
3196 		   sdcnt->intrcount, sdcnt->lastintrs,
3197 		   sdcnt->pollcnt, sdcnt->regfails,
3198 		   sdcnt->tx_sderrs, sdcnt->fcqueued,
3199 		   sdcnt->rxrtx, sdcnt->rx_toolong,
3200 		   sdcnt->rxc_errors, sdcnt->rx_hdrfail,
3201 		   sdcnt->rx_badhdr, sdcnt->rx_badseq,
3202 		   sdcnt->fc_rcvd, sdcnt->fc_xoff,
3203 		   sdcnt->fc_xon, sdcnt->rxglomfail,
3204 		   sdcnt->rxglomframes, sdcnt->rxglompkts,
3205 		   sdcnt->f2rxhdrs, sdcnt->f2rxdata,
3206 		   sdcnt->f2txdata, sdcnt->f1regdata,
3207 		   sdcnt->tickcnt, sdcnt->tx_ctlerrs,
3208 		   sdcnt->tx_ctlpkts, sdcnt->rx_ctlerrs,
3209 		   sdcnt->rx_ctlpkts, sdcnt->rx_readahead_cnt);
3210 
3211 	return 0;
3212 }
3213 
brcmf_sdio_debugfs_create(struct device * dev)3214 static void brcmf_sdio_debugfs_create(struct device *dev)
3215 {
3216 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3217 	struct brcmf_pub *drvr = bus_if->drvr;
3218 	struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3219 	struct brcmf_sdio *bus = sdiodev->bus;
3220 	struct dentry *dentry = brcmf_debugfs_get_devdir(drvr);
3221 
3222 	if (IS_ERR_OR_NULL(dentry))
3223 		return;
3224 
3225 	bus->console_interval = BRCMF_CONSOLE;
3226 
3227 	brcmf_debugfs_add_entry(drvr, "forensics", brcmf_sdio_forensic_read);
3228 	brcmf_debugfs_add_entry(drvr, "counters",
3229 				brcmf_debugfs_sdio_count_read);
3230 	debugfs_create_u32("console_interval", 0644, dentry,
3231 			   &bus->console_interval);
3232 }
3233 #else
brcmf_sdio_checkdied(struct brcmf_sdio * bus)3234 static int brcmf_sdio_checkdied(struct brcmf_sdio *bus)
3235 {
3236 	return 0;
3237 }
3238 
brcmf_sdio_debugfs_create(struct device * dev)3239 static void brcmf_sdio_debugfs_create(struct device *dev)
3240 {
3241 }
3242 #endif /* DEBUG */
3243 
3244 static int
brcmf_sdio_bus_rxctl(struct device * dev,unsigned char * msg,uint msglen)3245 brcmf_sdio_bus_rxctl(struct device *dev, unsigned char *msg, uint msglen)
3246 {
3247 	int timeleft;
3248 	uint rxlen = 0;
3249 	bool pending;
3250 	u8 *buf;
3251 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3252 	struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3253 	struct brcmf_sdio *bus = sdiodev->bus;
3254 
3255 	brcmf_dbg(TRACE, "Enter\n");
3256 	if (sdiodev->state != BRCMF_SDIOD_DATA)
3257 		return -EIO;
3258 
3259 	/* Wait until control frame is available */
3260 	timeleft = brcmf_sdio_dcmd_resp_wait(bus, &bus->rxlen, &pending);
3261 
3262 	spin_lock_bh(&bus->rxctl_lock);
3263 	rxlen = bus->rxlen;
3264 	memcpy(msg, bus->rxctl, min(msglen, rxlen));
3265 	bus->rxctl = NULL;
3266 	buf = bus->rxctl_orig;
3267 	bus->rxctl_orig = NULL;
3268 	bus->rxlen = 0;
3269 	spin_unlock_bh(&bus->rxctl_lock);
3270 	vfree(buf);
3271 
3272 	if (rxlen) {
3273 		brcmf_dbg(CTL, "resumed on rxctl frame, got %d expected %d\n",
3274 			  rxlen, msglen);
3275 	} else if (timeleft == 0) {
3276 		brcmf_err("resumed on timeout\n");
3277 		brcmf_sdio_checkdied(bus);
3278 	} else if (pending) {
3279 		brcmf_dbg(CTL, "cancelled\n");
3280 		return -ERESTARTSYS;
3281 	} else {
3282 		brcmf_dbg(CTL, "resumed for unknown reason?\n");
3283 		brcmf_sdio_checkdied(bus);
3284 	}
3285 
3286 	if (rxlen)
3287 		bus->sdcnt.rx_ctlpkts++;
3288 	else
3289 		bus->sdcnt.rx_ctlerrs++;
3290 
3291 	return rxlen ? (int)rxlen : -ETIMEDOUT;
3292 }
3293 
3294 #ifdef DEBUG
3295 static bool
brcmf_sdio_verifymemory(struct brcmf_sdio_dev * sdiodev,u32 ram_addr,u8 * ram_data,uint ram_sz)3296 brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3297 			u8 *ram_data, uint ram_sz)
3298 {
3299 	char *ram_cmp;
3300 	int err;
3301 	bool ret = true;
3302 	int address;
3303 	int offset;
3304 	int len;
3305 
3306 	/* read back and verify */
3307 	brcmf_dbg(INFO, "Compare RAM dl & ul at 0x%08x; size=%d\n", ram_addr,
3308 		  ram_sz);
3309 	ram_cmp = kmalloc(MEMBLOCK, GFP_KERNEL);
3310 	/* do not proceed while no memory but  */
3311 	if (!ram_cmp)
3312 		return true;
3313 
3314 	address = ram_addr;
3315 	offset = 0;
3316 	while (offset < ram_sz) {
3317 		len = ((offset + MEMBLOCK) < ram_sz) ? MEMBLOCK :
3318 		      ram_sz - offset;
3319 		err = brcmf_sdiod_ramrw(sdiodev, false, address, ram_cmp, len);
3320 		if (err) {
3321 			brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3322 				  err, len, address);
3323 			ret = false;
3324 			break;
3325 		} else if (memcmp(ram_cmp, &ram_data[offset], len)) {
3326 			brcmf_err("Downloaded RAM image is corrupted, block offset is %d, len is %d\n",
3327 				  offset, len);
3328 			ret = false;
3329 			break;
3330 		}
3331 		offset += len;
3332 		address += len;
3333 	}
3334 
3335 	kfree(ram_cmp);
3336 
3337 	return ret;
3338 }
3339 #else	/* DEBUG */
3340 static bool
brcmf_sdio_verifymemory(struct brcmf_sdio_dev * sdiodev,u32 ram_addr,u8 * ram_data,uint ram_sz)3341 brcmf_sdio_verifymemory(struct brcmf_sdio_dev *sdiodev, u32 ram_addr,
3342 			u8 *ram_data, uint ram_sz)
3343 {
3344 	return true;
3345 }
3346 #endif	/* DEBUG */
3347 
brcmf_sdio_download_code_file(struct brcmf_sdio * bus,const struct firmware * fw)3348 static int brcmf_sdio_download_code_file(struct brcmf_sdio *bus,
3349 					 const struct firmware *fw)
3350 {
3351 	int err;
3352 
3353 	brcmf_dbg(TRACE, "Enter\n");
3354 
3355 	err = brcmf_sdiod_ramrw(bus->sdiodev, true, bus->ci->rambase,
3356 				(u8 *)fw->data, fw->size);
3357 	if (err)
3358 		brcmf_err("error %d on writing %d membytes at 0x%08x\n",
3359 			  err, (int)fw->size, bus->ci->rambase);
3360 	else if (!brcmf_sdio_verifymemory(bus->sdiodev, bus->ci->rambase,
3361 					  (u8 *)fw->data, fw->size))
3362 		err = -EIO;
3363 
3364 	return err;
3365 }
3366 
brcmf_sdio_download_nvram(struct brcmf_sdio * bus,void * vars,u32 varsz)3367 static int brcmf_sdio_download_nvram(struct brcmf_sdio *bus,
3368 				     void *vars, u32 varsz)
3369 {
3370 	int address;
3371 	int err;
3372 
3373 	brcmf_dbg(TRACE, "Enter\n");
3374 
3375 	address = bus->ci->ramsize - varsz + bus->ci->rambase;
3376 	err = brcmf_sdiod_ramrw(bus->sdiodev, true, address, vars, varsz);
3377 	if (err)
3378 		brcmf_err("error %d on writing %d nvram bytes at 0x%08x\n",
3379 			  err, varsz, address);
3380 	else if (!brcmf_sdio_verifymemory(bus->sdiodev, address, vars, varsz))
3381 		err = -EIO;
3382 
3383 	return err;
3384 }
3385 
brcmf_sdio_download_firmware(struct brcmf_sdio * bus,const struct firmware * fw,void * nvram,u32 nvlen)3386 static int brcmf_sdio_download_firmware(struct brcmf_sdio *bus,
3387 					const struct firmware *fw,
3388 					void *nvram, u32 nvlen)
3389 {
3390 	int bcmerror;
3391 	u32 rstvec;
3392 
3393 	sdio_claim_host(bus->sdiodev->func1);
3394 	brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
3395 
3396 	rstvec = get_unaligned_le32(fw->data);
3397 	brcmf_dbg(SDIO, "firmware rstvec: %x\n", rstvec);
3398 
3399 	bcmerror = brcmf_sdio_download_code_file(bus, fw);
3400 	release_firmware(fw);
3401 	if (bcmerror) {
3402 		brcmf_err("dongle image file download failed\n");
3403 		brcmf_fw_nvram_free(nvram);
3404 		goto err;
3405 	}
3406 
3407 	bcmerror = brcmf_sdio_download_nvram(bus, nvram, nvlen);
3408 	brcmf_fw_nvram_free(nvram);
3409 	if (bcmerror) {
3410 		brcmf_err("dongle nvram file download failed\n");
3411 		goto err;
3412 	}
3413 
3414 	/* Take arm out of reset */
3415 	if (!brcmf_chip_set_active(bus->ci, rstvec)) {
3416 		brcmf_err("error getting out of ARM core reset\n");
3417 		bcmerror = -EIO;
3418 		goto err;
3419 	}
3420 
3421 err:
3422 	brcmf_sdio_clkctl(bus, CLK_SDONLY, false);
3423 	sdio_release_host(bus->sdiodev->func1);
3424 	return bcmerror;
3425 }
3426 
brcmf_sdio_aos_no_decode(struct brcmf_sdio * bus)3427 static bool brcmf_sdio_aos_no_decode(struct brcmf_sdio *bus)
3428 {
3429 	if (bus->ci->chip == CY_CC_43012_CHIP_ID ||
3430 	    bus->ci->chip == CY_CC_43752_CHIP_ID)
3431 		return true;
3432 	else
3433 		return false;
3434 }
3435 
brcmf_sdio_sr_init(struct brcmf_sdio * bus)3436 static void brcmf_sdio_sr_init(struct brcmf_sdio *bus)
3437 {
3438 	int err = 0;
3439 	u8 val;
3440 	u8 wakeupctrl;
3441 	u8 cardcap;
3442 	u8 chipclkcsr;
3443 
3444 	brcmf_dbg(TRACE, "Enter\n");
3445 
3446 	if (brcmf_chip_is_ulp(bus->ci)) {
3447 		wakeupctrl = SBSDIO_FUNC1_WCTRL_ALPWAIT_SHIFT;
3448 		chipclkcsr = SBSDIO_HT_AVAIL_REQ;
3449 	} else {
3450 		wakeupctrl = SBSDIO_FUNC1_WCTRL_HTWAIT_SHIFT;
3451 		chipclkcsr = SBSDIO_FORCE_HT;
3452 	}
3453 
3454 	if (brcmf_sdio_aos_no_decode(bus)) {
3455 		cardcap = SDIO_CCCR_BRCM_CARDCAP_CMD_NODEC;
3456 	} else {
3457 		cardcap = (SDIO_CCCR_BRCM_CARDCAP_CMD14_SUPPORT |
3458 			   SDIO_CCCR_BRCM_CARDCAP_CMD14_EXT);
3459 	}
3460 
3461 	val = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, &err);
3462 	if (err) {
3463 		brcmf_err("error reading SBSDIO_FUNC1_WAKEUPCTRL\n");
3464 		return;
3465 	}
3466 	val |= 1 << wakeupctrl;
3467 	brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_WAKEUPCTRL, val, &err);
3468 	if (err) {
3469 		brcmf_err("error writing SBSDIO_FUNC1_WAKEUPCTRL\n");
3470 		return;
3471 	}
3472 
3473 	/* Add CMD14 Support */
3474 	brcmf_sdiod_func0_wb(bus->sdiodev, SDIO_CCCR_BRCM_CARDCAP,
3475 			     cardcap,
3476 			     &err);
3477 	if (err) {
3478 		brcmf_err("error writing SDIO_CCCR_BRCM_CARDCAP\n");
3479 		return;
3480 	}
3481 
3482 	brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3483 			   chipclkcsr, &err);
3484 	if (err) {
3485 		brcmf_err("error writing SBSDIO_FUNC1_CHIPCLKCSR\n");
3486 		return;
3487 	}
3488 
3489 	/* set flag */
3490 	bus->sr_enabled = true;
3491 	brcmf_dbg(INFO, "SR enabled\n");
3492 }
3493 
3494 /* enable KSO bit */
brcmf_sdio_kso_init(struct brcmf_sdio * bus)3495 static int brcmf_sdio_kso_init(struct brcmf_sdio *bus)
3496 {
3497 	struct brcmf_core *core = bus->sdio_core;
3498 	u8 val;
3499 	int err = 0;
3500 
3501 	brcmf_dbg(TRACE, "Enter\n");
3502 
3503 	/* KSO bit added in SDIO core rev 12 */
3504 	if (core->rev < 12)
3505 		return 0;
3506 
3507 	val = brcmf_sdiod_readb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR, &err);
3508 	if (err) {
3509 		brcmf_err("error reading SBSDIO_FUNC1_SLEEPCSR\n");
3510 		return err;
3511 	}
3512 
3513 	if (!(val & SBSDIO_FUNC1_SLEEPCSR_KSO_MASK)) {
3514 		val |= (SBSDIO_FUNC1_SLEEPCSR_KSO_EN <<
3515 			SBSDIO_FUNC1_SLEEPCSR_KSO_SHIFT);
3516 		brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_SLEEPCSR,
3517 				   val, &err);
3518 		if (err) {
3519 			brcmf_err("error writing SBSDIO_FUNC1_SLEEPCSR\n");
3520 			return err;
3521 		}
3522 	}
3523 
3524 	return 0;
3525 }
3526 
3527 
brcmf_sdio_bus_preinit(struct device * dev)3528 static int brcmf_sdio_bus_preinit(struct device *dev)
3529 {
3530 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3531 	struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3532 	struct brcmf_sdio *bus = sdiodev->bus;
3533 	struct brcmf_core *core = bus->sdio_core;
3534 	u32 value;
3535 	__le32 iovar;
3536 	int err;
3537 
3538 	/* maxctl provided by common layer */
3539 	if (WARN_ON(!bus_if->maxctl))
3540 		return -EINVAL;
3541 
3542 	/* Allocate control receive buffer */
3543 	bus_if->maxctl += bus->roundup;
3544 	value = roundup((bus_if->maxctl + SDPCM_HDRLEN), ALIGNMENT);
3545 	value += bus->head_align;
3546 	bus->rxbuf = kmalloc(value, GFP_ATOMIC);
3547 	if (bus->rxbuf)
3548 		bus->rxblen = value;
3549 
3550 	/* the commands below use the terms tx and rx from
3551 	 * a device perspective, ie. bus:txglom affects the
3552 	 * bus transfers from device to host.
3553 	 */
3554 	if (core->rev < 12) {
3555 		/* for sdio core rev < 12, disable txgloming */
3556 		iovar = 0;
3557 		err = brcmf_iovar_data_set(dev, "bus:txglom", &iovar,
3558 					   sizeof(iovar));
3559 	} else {
3560 		/* otherwise, set txglomalign */
3561 		value = sdiodev->settings->bus.sdio.sd_sgentry_align;
3562 		/* SDIO ADMA requires at least 32 bit alignment */
3563 		iovar = cpu_to_le32(max_t(u32, value, ALIGNMENT));
3564 		err = brcmf_iovar_data_set(dev, "bus:txglomalign", &iovar,
3565 					   sizeof(iovar));
3566 	}
3567 
3568 	if (err < 0)
3569 		goto done;
3570 
3571 	bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
3572 	if (sdiodev->sg_support) {
3573 		bus->txglom = false;
3574 		iovar = cpu_to_le32(1);
3575 		err = brcmf_iovar_data_set(bus->sdiodev->dev, "bus:rxglom",
3576 					   &iovar, sizeof(iovar));
3577 		if (err < 0) {
3578 			/* bus:rxglom is allowed to fail */
3579 			err = 0;
3580 		} else {
3581 			bus->txglom = true;
3582 			bus->tx_hdrlen += SDPCM_HWEXT_LEN;
3583 		}
3584 	}
3585 	brcmf_bus_add_txhdrlen(bus->sdiodev->dev, bus->tx_hdrlen);
3586 
3587 done:
3588 	return err;
3589 }
3590 
brcmf_sdio_bus_get_ramsize(struct device * dev)3591 static size_t brcmf_sdio_bus_get_ramsize(struct device *dev)
3592 {
3593 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3594 	struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3595 	struct brcmf_sdio *bus = sdiodev->bus;
3596 
3597 	return bus->ci->ramsize - bus->ci->srsize;
3598 }
3599 
brcmf_sdio_bus_get_memdump(struct device * dev,void * data,size_t mem_size)3600 static int brcmf_sdio_bus_get_memdump(struct device *dev, void *data,
3601 				      size_t mem_size)
3602 {
3603 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
3604 	struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
3605 	struct brcmf_sdio *bus = sdiodev->bus;
3606 	int err;
3607 	int address;
3608 	int offset;
3609 	int len;
3610 
3611 	brcmf_dbg(INFO, "dump at 0x%08x: size=%zu\n", bus->ci->rambase,
3612 		  mem_size);
3613 
3614 	address = bus->ci->rambase;
3615 	offset = err = 0;
3616 	sdio_claim_host(sdiodev->func1);
3617 	while (offset < mem_size) {
3618 		len = ((offset + MEMBLOCK) < mem_size) ? MEMBLOCK :
3619 		      mem_size - offset;
3620 		err = brcmf_sdiod_ramrw(sdiodev, false, address, data, len);
3621 		if (err) {
3622 			brcmf_err("error %d on reading %d membytes at 0x%08x\n",
3623 				  err, len, address);
3624 			goto done;
3625 		}
3626 		data += len;
3627 		offset += len;
3628 		address += len;
3629 	}
3630 
3631 done:
3632 	sdio_release_host(sdiodev->func1);
3633 	return err;
3634 }
3635 
brcmf_sdio_trigger_dpc(struct brcmf_sdio * bus)3636 void brcmf_sdio_trigger_dpc(struct brcmf_sdio *bus)
3637 {
3638 	if (!bus->dpc_triggered) {
3639 		bus->dpc_triggered = true;
3640 		queue_work(bus->brcmf_wq, &bus->datawork);
3641 	}
3642 }
3643 
brcmf_sdio_isr(struct brcmf_sdio * bus,bool in_isr)3644 void brcmf_sdio_isr(struct brcmf_sdio *bus, bool in_isr)
3645 {
3646 	brcmf_dbg(TRACE, "Enter\n");
3647 
3648 	if (!bus) {
3649 		brcmf_err("bus is null pointer, exiting\n");
3650 		return;
3651 	}
3652 
3653 	/* Count the interrupt call */
3654 	bus->sdcnt.intrcount++;
3655 	if (in_isr)
3656 		atomic_set(&bus->ipend, 1);
3657 	else
3658 		if (brcmf_sdio_intr_rstatus(bus)) {
3659 			brcmf_err("failed backplane access\n");
3660 		}
3661 
3662 	/* Disable additional interrupts (is this needed now)? */
3663 	if (!bus->intr)
3664 		brcmf_err("isr w/o interrupt configured!\n");
3665 
3666 	bus->dpc_triggered = true;
3667 	queue_work(bus->brcmf_wq, &bus->datawork);
3668 }
3669 
brcmf_sdio_bus_watchdog(struct brcmf_sdio * bus)3670 static void brcmf_sdio_bus_watchdog(struct brcmf_sdio *bus)
3671 {
3672 	brcmf_dbg(TIMER, "Enter\n");
3673 
3674 	/* Poll period: check device if appropriate. */
3675 	if (!bus->sr_enabled &&
3676 	    bus->poll && (++bus->polltick >= bus->pollrate)) {
3677 		u32 intstatus = 0;
3678 
3679 		/* Reset poll tick */
3680 		bus->polltick = 0;
3681 
3682 		/* Check device if no interrupts */
3683 		if (!bus->intr ||
3684 		    (bus->sdcnt.intrcount == bus->sdcnt.lastintrs)) {
3685 
3686 			if (!bus->dpc_triggered) {
3687 				u8 devpend;
3688 
3689 				sdio_claim_host(bus->sdiodev->func1);
3690 				devpend = brcmf_sdiod_func0_rb(bus->sdiodev,
3691 						  SDIO_CCCR_INTx, NULL);
3692 				sdio_release_host(bus->sdiodev->func1);
3693 				intstatus = devpend & (INTR_STATUS_FUNC1 |
3694 						       INTR_STATUS_FUNC2);
3695 			}
3696 
3697 			/* If there is something, make like the ISR and
3698 				 schedule the DPC */
3699 			if (intstatus) {
3700 				bus->sdcnt.pollcnt++;
3701 				atomic_set(&bus->ipend, 1);
3702 
3703 				bus->dpc_triggered = true;
3704 				queue_work(bus->brcmf_wq, &bus->datawork);
3705 			}
3706 		}
3707 
3708 		/* Update interrupt tracking */
3709 		bus->sdcnt.lastintrs = bus->sdcnt.intrcount;
3710 	}
3711 #ifdef DEBUG
3712 	/* Poll for console output periodically */
3713 	if (bus->sdiodev->state == BRCMF_SDIOD_DATA && BRCMF_FWCON_ON() &&
3714 	    bus->console_interval != 0) {
3715 		bus->console.count += jiffies_to_msecs(BRCMF_WD_POLL);
3716 		if (bus->console.count >= bus->console_interval) {
3717 			bus->console.count -= bus->console_interval;
3718 			sdio_claim_host(bus->sdiodev->func1);
3719 			/* Make sure backplane clock is on */
3720 			brcmf_sdio_bus_sleep(bus, false, false);
3721 			if (brcmf_sdio_readconsole(bus) < 0)
3722 				/* stop on error */
3723 				bus->console_interval = 0;
3724 			sdio_release_host(bus->sdiodev->func1);
3725 		}
3726 	}
3727 #endif				/* DEBUG */
3728 
3729 	/* On idle timeout clear activity flag and/or turn off clock */
3730 	if (!bus->dpc_triggered) {
3731 		rmb();
3732 		if ((!bus->dpc_running) && (bus->idletime > 0) &&
3733 		    (bus->clkstate == CLK_AVAIL)) {
3734 			bus->idlecount++;
3735 			if (bus->idlecount > bus->idletime) {
3736 				brcmf_dbg(SDIO, "idle\n");
3737 				sdio_claim_host(bus->sdiodev->func1);
3738 #ifdef DEBUG
3739 				if (!BRCMF_FWCON_ON() ||
3740 				    bus->console_interval == 0)
3741 #endif
3742 					brcmf_sdio_wd_timer(bus, false);
3743 				bus->idlecount = 0;
3744 				brcmf_sdio_bus_sleep(bus, true, false);
3745 				sdio_release_host(bus->sdiodev->func1);
3746 			}
3747 		} else {
3748 			bus->idlecount = 0;
3749 		}
3750 	} else {
3751 		bus->idlecount = 0;
3752 	}
3753 }
3754 
brcmf_sdio_dataworker(struct work_struct * work)3755 static void brcmf_sdio_dataworker(struct work_struct *work)
3756 {
3757 	struct brcmf_sdio *bus = container_of(work, struct brcmf_sdio,
3758 					      datawork);
3759 
3760 	bus->dpc_running = true;
3761 	wmb();
3762 	while (READ_ONCE(bus->dpc_triggered)) {
3763 		bus->dpc_triggered = false;
3764 		brcmf_sdio_dpc(bus);
3765 		bus->idlecount = 0;
3766 	}
3767 	bus->dpc_running = false;
3768 	if (brcmf_sdiod_freezing(bus->sdiodev)) {
3769 		brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DOWN);
3770 		brcmf_sdiod_try_freeze(bus->sdiodev);
3771 		brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
3772 	}
3773 }
3774 
3775 static void
brcmf_sdio_drivestrengthinit(struct brcmf_sdio_dev * sdiodev,struct brcmf_chip * ci,u32 drivestrength)3776 brcmf_sdio_drivestrengthinit(struct brcmf_sdio_dev *sdiodev,
3777 			     struct brcmf_chip *ci, u32 drivestrength)
3778 {
3779 	const struct sdiod_drive_str *str_tab = NULL;
3780 	u32 str_mask;
3781 	u32 str_shift;
3782 	u32 i;
3783 	u32 drivestrength_sel = 0;
3784 	u32 cc_data_temp;
3785 	u32 addr;
3786 
3787 	if (!(ci->cc_caps & CC_CAP_PMU))
3788 		return;
3789 
3790 	switch (SDIOD_DRVSTR_KEY(ci->chip, ci->pmurev)) {
3791 	case SDIOD_DRVSTR_KEY(BRCM_CC_4330_CHIP_ID, 12):
3792 		str_tab = sdiod_drvstr_tab1_1v8;
3793 		str_mask = 0x00003800;
3794 		str_shift = 11;
3795 		break;
3796 	case SDIOD_DRVSTR_KEY(BRCM_CC_4334_CHIP_ID, 17):
3797 		str_tab = sdiod_drvstr_tab6_1v8;
3798 		str_mask = 0x00001800;
3799 		str_shift = 11;
3800 		break;
3801 	case SDIOD_DRVSTR_KEY(BRCM_CC_43143_CHIP_ID, 17):
3802 		/* note: 43143 does not support tristate */
3803 		i = ARRAY_SIZE(sdiod_drvstr_tab2_3v3) - 1;
3804 		if (drivestrength >= sdiod_drvstr_tab2_3v3[i].strength) {
3805 			str_tab = sdiod_drvstr_tab2_3v3;
3806 			str_mask = 0x00000007;
3807 			str_shift = 0;
3808 		} else
3809 			brcmf_err("Invalid SDIO Drive strength for chip %s, strength=%d\n",
3810 				  ci->name, drivestrength);
3811 		break;
3812 	case SDIOD_DRVSTR_KEY(BRCM_CC_43362_CHIP_ID, 13):
3813 		str_tab = sdiod_drive_strength_tab5_1v8;
3814 		str_mask = 0x00003800;
3815 		str_shift = 11;
3816 		break;
3817 	default:
3818 		brcmf_dbg(INFO, "No SDIO driver strength init needed for chip %s rev %d pmurev %d\n",
3819 			  ci->name, ci->chiprev, ci->pmurev);
3820 		break;
3821 	}
3822 
3823 	if (str_tab != NULL) {
3824 		struct brcmf_core *pmu = brcmf_chip_get_pmu(ci);
3825 
3826 		for (i = 0; str_tab[i].strength != 0; i++) {
3827 			if (drivestrength >= str_tab[i].strength) {
3828 				drivestrength_sel = str_tab[i].sel;
3829 				break;
3830 			}
3831 		}
3832 		addr = CORE_CC_REG(pmu->base, chipcontrol_addr);
3833 		brcmf_sdiod_writel(sdiodev, addr, 1, NULL);
3834 		cc_data_temp = brcmf_sdiod_readl(sdiodev, addr, NULL);
3835 		cc_data_temp &= ~str_mask;
3836 		drivestrength_sel <<= str_shift;
3837 		cc_data_temp |= drivestrength_sel;
3838 		brcmf_sdiod_writel(sdiodev, addr, cc_data_temp, NULL);
3839 
3840 		brcmf_dbg(INFO, "SDIO: %d mA (req=%d mA) drive strength selected, set to 0x%08x\n",
3841 			  str_tab[i].strength, drivestrength, cc_data_temp);
3842 	}
3843 }
3844 
brcmf_sdio_buscoreprep(void * ctx)3845 static int brcmf_sdio_buscoreprep(void *ctx)
3846 {
3847 	struct brcmf_sdio_dev *sdiodev = ctx;
3848 	int err = 0;
3849 	u8 clkval, clkset;
3850 
3851 	/* Try forcing SDIO core to do ALPAvail request only */
3852 	clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_ALP_AVAIL_REQ;
3853 	brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3854 	if (err) {
3855 		brcmf_err("error writing for HT off\n");
3856 		return err;
3857 	}
3858 
3859 	/* If register supported, wait for ALPAvail and then force ALP */
3860 	/* This may take up to 15 milliseconds */
3861 	clkval = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, NULL);
3862 
3863 	if ((clkval & ~SBSDIO_AVBITS) != clkset) {
3864 		brcmf_err("ChipClkCSR access: wrote 0x%02x read 0x%02x\n",
3865 			  clkset, clkval);
3866 		return -EACCES;
3867 	}
3868 
3869 	SPINWAIT(((clkval = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3870 					      NULL)),
3871 		 !SBSDIO_ALPAV(clkval)),
3872 		 PMU_MAX_TRANSITION_DLY);
3873 
3874 	if (!SBSDIO_ALPAV(clkval)) {
3875 		brcmf_err("timeout on ALPAV wait, clkval 0x%02x\n",
3876 			  clkval);
3877 		return -EBUSY;
3878 	}
3879 
3880 	clkset = SBSDIO_FORCE_HW_CLKREQ_OFF | SBSDIO_FORCE_ALP;
3881 	brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, clkset, &err);
3882 	udelay(65);
3883 
3884 	/* Also, disable the extra SDIO pull-ups */
3885 	brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_SDIOPULLUP, 0, NULL);
3886 
3887 	return 0;
3888 }
3889 
brcmf_sdio_buscore_activate(void * ctx,struct brcmf_chip * chip,u32 rstvec)3890 static void brcmf_sdio_buscore_activate(void *ctx, struct brcmf_chip *chip,
3891 					u32 rstvec)
3892 {
3893 	struct brcmf_sdio_dev *sdiodev = ctx;
3894 	struct brcmf_core *core = sdiodev->bus->sdio_core;
3895 	u32 reg_addr;
3896 
3897 	/* clear all interrupts */
3898 	reg_addr = core->base + SD_REG(intstatus);
3899 	brcmf_sdiod_writel(sdiodev, reg_addr, 0xFFFFFFFF, NULL);
3900 
3901 	if (rstvec)
3902 		/* Write reset vector to address 0 */
3903 		brcmf_sdiod_ramrw(sdiodev, true, 0, (void *)&rstvec,
3904 				  sizeof(rstvec));
3905 }
3906 
brcmf_sdio_buscore_read32(void * ctx,u32 addr)3907 static u32 brcmf_sdio_buscore_read32(void *ctx, u32 addr)
3908 {
3909 	struct brcmf_sdio_dev *sdiodev = ctx;
3910 	u32 val, rev;
3911 
3912 	val = brcmf_sdiod_readl(sdiodev, addr, NULL);
3913 
3914 	/*
3915 	 * this is a bit of special handling if reading the chipcommon chipid
3916 	 * register. The 4339 is a next-gen of the 4335. It uses the same
3917 	 * SDIO device id as 4335 and the chipid register returns 4335 as well.
3918 	 * It can be identified as 4339 by looking at the chip revision. It
3919 	 * is corrected here so the chip.c module has the right info.
3920 	 */
3921 	if (addr == CORE_CC_REG(SI_ENUM_BASE_DEFAULT, chipid) &&
3922 	    (sdiodev->func1->device == SDIO_DEVICE_ID_BROADCOM_4339 ||
3923 	     sdiodev->func1->device == SDIO_DEVICE_ID_BROADCOM_4335_4339)) {
3924 		rev = (val & CID_REV_MASK) >> CID_REV_SHIFT;
3925 		if (rev >= 2) {
3926 			val &= ~CID_ID_MASK;
3927 			val |= BRCM_CC_4339_CHIP_ID;
3928 		}
3929 	}
3930 
3931 	return val;
3932 }
3933 
brcmf_sdio_buscore_write32(void * ctx,u32 addr,u32 val)3934 static void brcmf_sdio_buscore_write32(void *ctx, u32 addr, u32 val)
3935 {
3936 	struct brcmf_sdio_dev *sdiodev = ctx;
3937 
3938 	brcmf_sdiod_writel(sdiodev, addr, val, NULL);
3939 }
3940 
3941 static const struct brcmf_buscore_ops brcmf_sdio_buscore_ops = {
3942 	.prepare = brcmf_sdio_buscoreprep,
3943 	.activate = brcmf_sdio_buscore_activate,
3944 	.read32 = brcmf_sdio_buscore_read32,
3945 	.write32 = brcmf_sdio_buscore_write32,
3946 };
3947 
3948 static bool
brcmf_sdio_probe_attach(struct brcmf_sdio * bus)3949 brcmf_sdio_probe_attach(struct brcmf_sdio *bus)
3950 {
3951 	struct brcmf_sdio_dev *sdiodev;
3952 	u8 clkctl = 0;
3953 	int err = 0;
3954 	int reg_addr;
3955 	u32 reg_val;
3956 	u32 drivestrength;
3957 	u32 enum_base;
3958 
3959 	sdiodev = bus->sdiodev;
3960 	sdio_claim_host(sdiodev->func1);
3961 
3962 	enum_base = brcmf_chip_enum_base(sdiodev->func1->device);
3963 
3964 	pr_debug("F1 signature read @0x%08x=0x%4x\n", enum_base,
3965 		 brcmf_sdiod_readl(sdiodev, enum_base, NULL));
3966 
3967 	/*
3968 	 * Force PLL off until brcmf_chip_attach()
3969 	 * programs PLL control regs
3970 	 */
3971 
3972 	brcmf_sdiod_writeb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, BRCMF_INIT_CLKCTL1,
3973 			   &err);
3974 	if (!err)
3975 		clkctl = brcmf_sdiod_readb(sdiodev, SBSDIO_FUNC1_CHIPCLKCSR,
3976 					   &err);
3977 
3978 	if (err || ((clkctl & ~SBSDIO_AVBITS) != BRCMF_INIT_CLKCTL1)) {
3979 		brcmf_err("ChipClkCSR access: err %d wrote 0x%02x read 0x%02x\n",
3980 			  err, BRCMF_INIT_CLKCTL1, clkctl);
3981 		goto fail;
3982 	}
3983 
3984 	bus->ci = brcmf_chip_attach(sdiodev, sdiodev->func1->device,
3985 				    &brcmf_sdio_buscore_ops);
3986 	if (IS_ERR(bus->ci)) {
3987 		brcmf_err("brcmf_chip_attach failed!\n");
3988 		bus->ci = NULL;
3989 		goto fail;
3990 	}
3991 
3992 	/* Pick up the SDIO core info struct from chip.c */
3993 	bus->sdio_core   = brcmf_chip_get_core(bus->ci, BCMA_CORE_SDIO_DEV);
3994 	if (!bus->sdio_core)
3995 		goto fail;
3996 
3997 	/* Pick up the CHIPCOMMON core info struct, for bulk IO in bcmsdh.c */
3998 	sdiodev->cc_core = brcmf_chip_get_core(bus->ci, BCMA_CORE_CHIPCOMMON);
3999 	if (!sdiodev->cc_core)
4000 		goto fail;
4001 
4002 	sdiodev->settings = brcmf_get_module_param(sdiodev->dev,
4003 						   BRCMF_BUSTYPE_SDIO,
4004 						   bus->ci->chip,
4005 						   bus->ci->chiprev);
4006 	if (!sdiodev->settings) {
4007 		brcmf_err("Failed to get device parameters\n");
4008 		goto fail;
4009 	}
4010 	/* platform specific configuration:
4011 	 *   alignments must be at least 4 bytes for ADMA
4012 	 */
4013 	bus->head_align = ALIGNMENT;
4014 	bus->sgentry_align = ALIGNMENT;
4015 	if (sdiodev->settings->bus.sdio.sd_head_align > ALIGNMENT)
4016 		bus->head_align = sdiodev->settings->bus.sdio.sd_head_align;
4017 	if (sdiodev->settings->bus.sdio.sd_sgentry_align > ALIGNMENT)
4018 		bus->sgentry_align =
4019 				sdiodev->settings->bus.sdio.sd_sgentry_align;
4020 
4021 	/* allocate scatter-gather table. sg support
4022 	 * will be disabled upon allocation failure.
4023 	 */
4024 	brcmf_sdiod_sgtable_alloc(sdiodev);
4025 
4026 	/* wowl can be supported when KEEP_POWER is true and (WAKE_SDIO_IRQ
4027 	 * is true or when platform data OOB irq is true).
4028 	 */
4029 	if (IS_ENABLED(CONFIG_PM_SLEEP) &&
4030 	    (sdio_get_host_pm_caps(sdiodev->func1) & MMC_PM_KEEP_POWER) &&
4031 	    ((sdio_get_host_pm_caps(sdiodev->func1) & MMC_PM_WAKE_SDIO_IRQ) ||
4032 	     (sdiodev->settings->bus.sdio.oob_irq_supported)))
4033 		sdiodev->bus_if->wowl_supported = true;
4034 
4035 	if (brcmf_sdio_kso_init(bus)) {
4036 		brcmf_err("error enabling KSO\n");
4037 		goto fail;
4038 	}
4039 
4040 	if (sdiodev->settings->bus.sdio.drive_strength)
4041 		drivestrength = sdiodev->settings->bus.sdio.drive_strength;
4042 	else
4043 		drivestrength = DEFAULT_SDIO_DRIVE_STRENGTH;
4044 	brcmf_sdio_drivestrengthinit(sdiodev, bus->ci, drivestrength);
4045 
4046 	/* Set card control so an SDIO card reset does a WLAN backplane reset */
4047 	reg_val = brcmf_sdiod_func0_rb(sdiodev, SDIO_CCCR_BRCM_CARDCTRL, &err);
4048 	if (err)
4049 		goto fail;
4050 
4051 	reg_val |= SDIO_CCCR_BRCM_CARDCTRL_WLANRESET;
4052 
4053 	brcmf_sdiod_func0_wb(sdiodev, SDIO_CCCR_BRCM_CARDCTRL, reg_val, &err);
4054 	if (err)
4055 		goto fail;
4056 
4057 	/* set PMUControl so a backplane reset does PMU state reload */
4058 	reg_addr = CORE_CC_REG(brcmf_chip_get_pmu(bus->ci)->base, pmucontrol);
4059 	reg_val = brcmf_sdiod_readl(sdiodev, reg_addr, &err);
4060 	if (err)
4061 		goto fail;
4062 
4063 	reg_val |= (BCMA_CC_PMU_CTL_RES_RELOAD << BCMA_CC_PMU_CTL_RES_SHIFT);
4064 
4065 	brcmf_sdiod_writel(sdiodev, reg_addr, reg_val, &err);
4066 	if (err)
4067 		goto fail;
4068 
4069 	sdio_release_host(sdiodev->func1);
4070 
4071 	brcmu_pktq_init(&bus->txq, (PRIOMASK + 1), TXQLEN);
4072 
4073 	/* allocate header buffer */
4074 	bus->hdrbuf = kzalloc(MAX_HDR_READ + bus->head_align, GFP_KERNEL);
4075 	if (!bus->hdrbuf)
4076 		return false;
4077 	/* Locate an appropriately-aligned portion of hdrbuf */
4078 	bus->rxhdr = (u8 *) roundup((unsigned long)&bus->hdrbuf[0],
4079 				    bus->head_align);
4080 
4081 	/* Set the poll and/or interrupt flags */
4082 	bus->intr = true;
4083 	bus->poll = false;
4084 	if (bus->poll)
4085 		bus->pollrate = 1;
4086 
4087 	return true;
4088 
4089 fail:
4090 	sdio_release_host(sdiodev->func1);
4091 	return false;
4092 }
4093 
4094 static int
brcmf_sdio_watchdog_thread(void * data)4095 brcmf_sdio_watchdog_thread(void *data)
4096 {
4097 	struct brcmf_sdio *bus = (struct brcmf_sdio *)data;
4098 	int wait;
4099 
4100 	allow_signal(SIGTERM);
4101 	/* Run until signal received */
4102 	brcmf_sdiod_freezer_count(bus->sdiodev);
4103 	while (1) {
4104 		if (kthread_should_stop())
4105 			break;
4106 		brcmf_sdiod_freezer_uncount(bus->sdiodev);
4107 		wait = wait_for_completion_interruptible(&bus->watchdog_wait);
4108 		brcmf_sdiod_freezer_count(bus->sdiodev);
4109 		brcmf_sdiod_try_freeze(bus->sdiodev);
4110 		if (!wait) {
4111 			brcmf_sdio_bus_watchdog(bus);
4112 			/* Count the tick for reference */
4113 			bus->sdcnt.tickcnt++;
4114 			reinit_completion(&bus->watchdog_wait);
4115 		} else
4116 			break;
4117 	}
4118 	return 0;
4119 }
4120 
4121 static void
brcmf_sdio_watchdog(struct timer_list * t)4122 brcmf_sdio_watchdog(struct timer_list *t)
4123 {
4124 	struct brcmf_sdio *bus = from_timer(bus, t, timer);
4125 
4126 	if (bus->watchdog_tsk) {
4127 		complete(&bus->watchdog_wait);
4128 		/* Reschedule the watchdog */
4129 		if (bus->wd_active)
4130 			mod_timer(&bus->timer,
4131 				  jiffies + BRCMF_WD_POLL);
4132 	}
4133 }
4134 
brcmf_sdio_get_blob(struct device * dev,const struct firmware ** fw,enum brcmf_blob_type type)4135 static int brcmf_sdio_get_blob(struct device *dev, const struct firmware **fw,
4136 			       enum brcmf_blob_type type)
4137 {
4138 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
4139 	struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
4140 
4141 	switch (type) {
4142 	case BRCMF_BLOB_CLM:
4143 		*fw = sdiodev->clm_fw;
4144 		sdiodev->clm_fw = NULL;
4145 		break;
4146 	default:
4147 		return -ENOENT;
4148 	}
4149 
4150 	if (!*fw)
4151 		return -ENOENT;
4152 
4153 	return 0;
4154 }
4155 
brcmf_sdio_bus_reset(struct device * dev)4156 static int brcmf_sdio_bus_reset(struct device *dev)
4157 {
4158 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
4159 	struct brcmf_sdio_dev *sdiodev = bus_if->bus_priv.sdio;
4160 
4161 	brcmf_dbg(SDIO, "Enter\n");
4162 
4163 	/* start by unregistering irqs */
4164 	brcmf_sdiod_intr_unregister(sdiodev);
4165 
4166 	brcmf_sdiod_remove(sdiodev);
4167 
4168 	/* reset the adapter */
4169 	sdio_claim_host(sdiodev->func1);
4170 	mmc_hw_reset(sdiodev->func1->card);
4171 	sdio_release_host(sdiodev->func1);
4172 
4173 	brcmf_bus_change_state(sdiodev->bus_if, BRCMF_BUS_DOWN);
4174 	return 0;
4175 }
4176 
4177 static const struct brcmf_bus_ops brcmf_sdio_bus_ops = {
4178 	.stop = brcmf_sdio_bus_stop,
4179 	.preinit = brcmf_sdio_bus_preinit,
4180 	.txdata = brcmf_sdio_bus_txdata,
4181 	.txctl = brcmf_sdio_bus_txctl,
4182 	.rxctl = brcmf_sdio_bus_rxctl,
4183 	.gettxq = brcmf_sdio_bus_gettxq,
4184 	.wowl_config = brcmf_sdio_wowl_config,
4185 	.get_ramsize = brcmf_sdio_bus_get_ramsize,
4186 	.get_memdump = brcmf_sdio_bus_get_memdump,
4187 	.get_blob = brcmf_sdio_get_blob,
4188 	.debugfs_create = brcmf_sdio_debugfs_create,
4189 	.reset = brcmf_sdio_bus_reset
4190 };
4191 
4192 #define BRCMF_SDIO_FW_CODE	0
4193 #define BRCMF_SDIO_FW_NVRAM	1
4194 #define BRCMF_SDIO_FW_CLM	2
4195 
brcmf_sdio_firmware_callback(struct device * dev,int err,struct brcmf_fw_request * fwreq)4196 static void brcmf_sdio_firmware_callback(struct device *dev, int err,
4197 					 struct brcmf_fw_request *fwreq)
4198 {
4199 	struct brcmf_bus *bus_if = dev_get_drvdata(dev);
4200 	struct brcmf_sdio_dev *sdiod = bus_if->bus_priv.sdio;
4201 	struct brcmf_sdio *bus = sdiod->bus;
4202 	struct brcmf_core *core = bus->sdio_core;
4203 	const struct firmware *code;
4204 	void *nvram;
4205 	u32 nvram_len;
4206 	u8 saveclk, bpreq;
4207 	u8 devctl;
4208 
4209 	brcmf_dbg(TRACE, "Enter: dev=%s, err=%d\n", dev_name(dev), err);
4210 
4211 	if (err)
4212 		goto fail;
4213 
4214 	code = fwreq->items[BRCMF_SDIO_FW_CODE].binary;
4215 	nvram = fwreq->items[BRCMF_SDIO_FW_NVRAM].nv_data.data;
4216 	nvram_len = fwreq->items[BRCMF_SDIO_FW_NVRAM].nv_data.len;
4217 	sdiod->clm_fw = fwreq->items[BRCMF_SDIO_FW_CLM].binary;
4218 	kfree(fwreq);
4219 
4220 	/* try to download image and nvram to the dongle */
4221 	bus->alp_only = true;
4222 	err = brcmf_sdio_download_firmware(bus, code, nvram, nvram_len);
4223 	if (err)
4224 		goto fail;
4225 	bus->alp_only = false;
4226 
4227 	/* Start the watchdog timer */
4228 	bus->sdcnt.tickcnt = 0;
4229 	brcmf_sdio_wd_timer(bus, true);
4230 
4231 	sdio_claim_host(sdiod->func1);
4232 
4233 	/* Make sure backplane clock is on, needed to generate F2 interrupt */
4234 	brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4235 	if (bus->clkstate != CLK_AVAIL)
4236 		goto release;
4237 
4238 	/* Force clocks on backplane to be sure F2 interrupt propagates */
4239 	saveclk = brcmf_sdiod_readb(sdiod, SBSDIO_FUNC1_CHIPCLKCSR, &err);
4240 	if (!err) {
4241 		bpreq = saveclk;
4242 		bpreq |= brcmf_chip_is_ulp(bus->ci) ?
4243 			SBSDIO_HT_AVAIL_REQ : SBSDIO_FORCE_HT;
4244 		brcmf_sdiod_writeb(sdiod, SBSDIO_FUNC1_CHIPCLKCSR,
4245 				   bpreq, &err);
4246 	}
4247 	if (err) {
4248 		brcmf_err("Failed to force clock for F2: err %d\n", err);
4249 		goto release;
4250 	}
4251 
4252 	/* Enable function 2 (frame transfers) */
4253 	brcmf_sdiod_writel(sdiod, core->base + SD_REG(tosbmailboxdata),
4254 			   SDPCM_PROT_VERSION << SMB_DATA_VERSION_SHIFT, NULL);
4255 
4256 	err = sdio_enable_func(sdiod->func2);
4257 
4258 	brcmf_dbg(INFO, "enable F2: err=%d\n", err);
4259 
4260 	/* If F2 successfully enabled, set core and enable interrupts */
4261 	if (!err) {
4262 		/* Set up the interrupt mask and enable interrupts */
4263 		bus->hostintmask = HOSTINTMASK;
4264 		brcmf_sdiod_writel(sdiod, core->base + SD_REG(hostintmask),
4265 				   bus->hostintmask, NULL);
4266 
4267 		switch (sdiod->func1->device) {
4268 		case SDIO_DEVICE_ID_BROADCOM_CYPRESS_4373:
4269 		case SDIO_DEVICE_ID_BROADCOM_CYPRESS_43752:
4270 			brcmf_dbg(INFO, "set F2 watermark to 0x%x*4 bytes\n",
4271 				  CY_4373_F2_WATERMARK);
4272 			brcmf_sdiod_writeb(sdiod, SBSDIO_WATERMARK,
4273 					   CY_4373_F2_WATERMARK, &err);
4274 			devctl = brcmf_sdiod_readb(sdiod, SBSDIO_DEVICE_CTL,
4275 						   &err);
4276 			devctl |= SBSDIO_DEVCTL_F2WM_ENAB;
4277 			brcmf_sdiod_writeb(sdiod, SBSDIO_DEVICE_CTL, devctl,
4278 					   &err);
4279 			brcmf_sdiod_writeb(sdiod, SBSDIO_FUNC1_MESBUSYCTRL,
4280 					   CY_4373_F1_MESBUSYCTRL, &err);
4281 			break;
4282 		case SDIO_DEVICE_ID_BROADCOM_CYPRESS_43012:
4283 			brcmf_dbg(INFO, "set F2 watermark to 0x%x*4 bytes\n",
4284 				  CY_43012_F2_WATERMARK);
4285 			brcmf_sdiod_writeb(sdiod, SBSDIO_WATERMARK,
4286 					   CY_43012_F2_WATERMARK, &err);
4287 			devctl = brcmf_sdiod_readb(sdiod, SBSDIO_DEVICE_CTL,
4288 						   &err);
4289 			devctl |= SBSDIO_DEVCTL_F2WM_ENAB;
4290 			brcmf_sdiod_writeb(sdiod, SBSDIO_DEVICE_CTL, devctl,
4291 					   &err);
4292 			brcmf_sdiod_writeb(sdiod, SBSDIO_FUNC1_MESBUSYCTRL,
4293 					   CY_43012_MESBUSYCTRL, &err);
4294 			break;
4295 		case SDIO_DEVICE_ID_BROADCOM_4329:
4296 		case SDIO_DEVICE_ID_BROADCOM_4339:
4297 			brcmf_dbg(INFO, "set F2 watermark to 0x%x*4 bytes\n",
4298 				  CY_4339_F2_WATERMARK);
4299 			brcmf_sdiod_writeb(sdiod, SBSDIO_WATERMARK,
4300 					   CY_4339_F2_WATERMARK, &err);
4301 			devctl = brcmf_sdiod_readb(sdiod, SBSDIO_DEVICE_CTL,
4302 						   &err);
4303 			devctl |= SBSDIO_DEVCTL_F2WM_ENAB;
4304 			brcmf_sdiod_writeb(sdiod, SBSDIO_DEVICE_CTL, devctl,
4305 					   &err);
4306 			brcmf_sdiod_writeb(sdiod, SBSDIO_FUNC1_MESBUSYCTRL,
4307 					   CY_4339_MESBUSYCTRL, &err);
4308 			break;
4309 		case SDIO_DEVICE_ID_BROADCOM_43455:
4310 			brcmf_dbg(INFO, "set F2 watermark to 0x%x*4 bytes\n",
4311 				  CY_43455_F2_WATERMARK);
4312 			brcmf_sdiod_writeb(sdiod, SBSDIO_WATERMARK,
4313 					   CY_43455_F2_WATERMARK, &err);
4314 			devctl = brcmf_sdiod_readb(sdiod, SBSDIO_DEVICE_CTL,
4315 						   &err);
4316 			devctl |= SBSDIO_DEVCTL_F2WM_ENAB;
4317 			brcmf_sdiod_writeb(sdiod, SBSDIO_DEVICE_CTL, devctl,
4318 					   &err);
4319 			brcmf_sdiod_writeb(sdiod, SBSDIO_FUNC1_MESBUSYCTRL,
4320 					   CY_43455_MESBUSYCTRL, &err);
4321 			break;
4322 		case SDIO_DEVICE_ID_BROADCOM_4359:
4323 		case SDIO_DEVICE_ID_BROADCOM_4354:
4324 		case SDIO_DEVICE_ID_BROADCOM_4356:
4325 			brcmf_dbg(INFO, "set F2 watermark to 0x%x*4 bytes\n",
4326 				  CY_435X_F2_WATERMARK);
4327 			brcmf_sdiod_writeb(sdiod, SBSDIO_WATERMARK,
4328 					   CY_435X_F2_WATERMARK, &err);
4329 			devctl = brcmf_sdiod_readb(sdiod, SBSDIO_DEVICE_CTL,
4330 						   &err);
4331 			devctl |= SBSDIO_DEVCTL_F2WM_ENAB;
4332 			brcmf_sdiod_writeb(sdiod, SBSDIO_DEVICE_CTL, devctl,
4333 					   &err);
4334 			brcmf_sdiod_writeb(sdiod, SBSDIO_FUNC1_MESBUSYCTRL,
4335 					   CY_435X_F1_MESBUSYCTRL, &err);
4336 			break;
4337 		default:
4338 			brcmf_sdiod_writeb(sdiod, SBSDIO_WATERMARK,
4339 					   DEFAULT_F2_WATERMARK, &err);
4340 			break;
4341 		}
4342 	} else {
4343 		/* Disable F2 again */
4344 		sdio_disable_func(sdiod->func2);
4345 		goto checkdied;
4346 	}
4347 
4348 	if (brcmf_chip_sr_capable(bus->ci)) {
4349 		brcmf_sdio_sr_init(bus);
4350 	} else {
4351 		/* Restore previous clock setting */
4352 		brcmf_sdiod_writeb(sdiod, SBSDIO_FUNC1_CHIPCLKCSR,
4353 				   saveclk, &err);
4354 	}
4355 
4356 	if (err == 0) {
4357 		/* Assign bus interface call back */
4358 		sdiod->bus_if->dev = sdiod->dev;
4359 		sdiod->bus_if->ops = &brcmf_sdio_bus_ops;
4360 		sdiod->bus_if->chip = bus->ci->chip;
4361 		sdiod->bus_if->chiprev = bus->ci->chiprev;
4362 
4363 		/* Allow full data communication using DPC from now on. */
4364 		brcmf_sdiod_change_state(bus->sdiodev, BRCMF_SDIOD_DATA);
4365 
4366 		err = brcmf_sdiod_intr_register(sdiod);
4367 		if (err != 0)
4368 			brcmf_err("intr register failed:%d\n", err);
4369 	}
4370 
4371 	/* If we didn't come up, turn off backplane clock */
4372 	if (err != 0) {
4373 		brcmf_sdio_clkctl(bus, CLK_NONE, false);
4374 		goto checkdied;
4375 	}
4376 
4377 	sdio_release_host(sdiod->func1);
4378 
4379 	err = brcmf_alloc(sdiod->dev, sdiod->settings);
4380 	if (err) {
4381 		brcmf_err("brcmf_alloc failed\n");
4382 		goto claim;
4383 	}
4384 
4385 	/* Attach to the common layer, reserve hdr space */
4386 	err = brcmf_attach(sdiod->dev);
4387 	if (err != 0) {
4388 		brcmf_err("brcmf_attach failed\n");
4389 		goto free;
4390 	}
4391 
4392 	/* ready */
4393 	return;
4394 
4395 free:
4396 	brcmf_free(sdiod->dev);
4397 claim:
4398 	sdio_claim_host(sdiod->func1);
4399 checkdied:
4400 	brcmf_sdio_checkdied(bus);
4401 release:
4402 	sdio_release_host(sdiod->func1);
4403 fail:
4404 	brcmf_dbg(TRACE, "failed: dev=%s, err=%d\n", dev_name(dev), err);
4405 	device_release_driver(&sdiod->func2->dev);
4406 	device_release_driver(dev);
4407 }
4408 
4409 static struct brcmf_fw_request *
brcmf_sdio_prepare_fw_request(struct brcmf_sdio * bus)4410 brcmf_sdio_prepare_fw_request(struct brcmf_sdio *bus)
4411 {
4412 	struct brcmf_fw_request *fwreq;
4413 	struct brcmf_fw_name fwnames[] = {
4414 		{ ".bin", bus->sdiodev->fw_name },
4415 		{ ".txt", bus->sdiodev->nvram_name },
4416 		{ ".clm_blob", bus->sdiodev->clm_name },
4417 	};
4418 
4419 	fwreq = brcmf_fw_alloc_request(bus->ci->chip, bus->ci->chiprev,
4420 				       brcmf_sdio_fwnames,
4421 				       ARRAY_SIZE(brcmf_sdio_fwnames),
4422 				       fwnames, ARRAY_SIZE(fwnames));
4423 	if (!fwreq)
4424 		return NULL;
4425 
4426 	fwreq->items[BRCMF_SDIO_FW_CODE].type = BRCMF_FW_TYPE_BINARY;
4427 	fwreq->items[BRCMF_SDIO_FW_NVRAM].type = BRCMF_FW_TYPE_NVRAM;
4428 	fwreq->items[BRCMF_SDIO_FW_CLM].type = BRCMF_FW_TYPE_BINARY;
4429 	fwreq->items[BRCMF_SDIO_FW_CLM].flags = BRCMF_FW_REQF_OPTIONAL;
4430 	fwreq->board_types[0] = bus->sdiodev->settings->board_type;
4431 
4432 	return fwreq;
4433 }
4434 
brcmf_sdio_probe(struct brcmf_sdio_dev * sdiodev)4435 struct brcmf_sdio *brcmf_sdio_probe(struct brcmf_sdio_dev *sdiodev)
4436 {
4437 	int ret;
4438 	struct brcmf_sdio *bus;
4439 	struct workqueue_struct *wq;
4440 	struct brcmf_fw_request *fwreq;
4441 
4442 	brcmf_dbg(TRACE, "Enter\n");
4443 
4444 	/* Allocate private bus interface state */
4445 	bus = kzalloc(sizeof(struct brcmf_sdio), GFP_ATOMIC);
4446 	if (!bus)
4447 		goto fail;
4448 
4449 	bus->sdiodev = sdiodev;
4450 	sdiodev->bus = bus;
4451 	skb_queue_head_init(&bus->glom);
4452 	bus->txbound = BRCMF_TXBOUND;
4453 	bus->rxbound = BRCMF_RXBOUND;
4454 	bus->txminmax = BRCMF_TXMINMAX;
4455 	bus->tx_seq = SDPCM_SEQ_WRAP - 1;
4456 
4457 	/* single-threaded workqueue */
4458 	wq = alloc_ordered_workqueue("brcmf_wq/%s", WQ_MEM_RECLAIM | WQ_HIGHPRI,
4459 				     dev_name(&sdiodev->func1->dev));
4460 	if (!wq) {
4461 		brcmf_err("insufficient memory to create txworkqueue\n");
4462 		goto fail;
4463 	}
4464 	brcmf_sdiod_freezer_count(sdiodev);
4465 	INIT_WORK(&bus->datawork, brcmf_sdio_dataworker);
4466 	bus->brcmf_wq = wq;
4467 
4468 	/* attempt to attach to the dongle */
4469 	if (!(brcmf_sdio_probe_attach(bus))) {
4470 		brcmf_err("brcmf_sdio_probe_attach failed\n");
4471 		goto fail;
4472 	}
4473 
4474 	spin_lock_init(&bus->rxctl_lock);
4475 	spin_lock_init(&bus->txq_lock);
4476 	init_waitqueue_head(&bus->ctrl_wait);
4477 	init_waitqueue_head(&bus->dcmd_resp_wait);
4478 
4479 	/* Set up the watchdog timer */
4480 	timer_setup(&bus->timer, brcmf_sdio_watchdog, 0);
4481 	/* Initialize watchdog thread */
4482 	init_completion(&bus->watchdog_wait);
4483 	bus->watchdog_tsk = kthread_run(brcmf_sdio_watchdog_thread,
4484 					bus, "brcmf_wdog/%s",
4485 					dev_name(&sdiodev->func1->dev));
4486 	if (IS_ERR(bus->watchdog_tsk)) {
4487 		pr_warn("brcmf_watchdog thread failed to start\n");
4488 		bus->watchdog_tsk = NULL;
4489 	}
4490 	/* Initialize DPC thread */
4491 	bus->dpc_triggered = false;
4492 	bus->dpc_running = false;
4493 
4494 	/* default sdio bus header length for tx packet */
4495 	bus->tx_hdrlen = SDPCM_HWHDR_LEN + SDPCM_SWHDR_LEN;
4496 
4497 	/* Query the F2 block size, set roundup accordingly */
4498 	bus->blocksize = bus->sdiodev->func2->cur_blksize;
4499 	bus->roundup = min(max_roundup, bus->blocksize);
4500 
4501 	sdio_claim_host(bus->sdiodev->func1);
4502 
4503 	/* Disable F2 to clear any intermediate frame state on the dongle */
4504 	sdio_disable_func(bus->sdiodev->func2);
4505 
4506 	bus->rxflow = false;
4507 
4508 	/* Done with backplane-dependent accesses, can drop clock... */
4509 	brcmf_sdiod_writeb(bus->sdiodev, SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
4510 
4511 	sdio_release_host(bus->sdiodev->func1);
4512 
4513 	/* ...and initialize clock/power states */
4514 	bus->clkstate = CLK_SDONLY;
4515 	bus->idletime = BRCMF_IDLE_INTERVAL;
4516 	bus->idleclock = BRCMF_IDLE_ACTIVE;
4517 
4518 	/* SR state */
4519 	bus->sr_enabled = false;
4520 
4521 	brcmf_dbg(INFO, "completed!!\n");
4522 
4523 	fwreq = brcmf_sdio_prepare_fw_request(bus);
4524 	if (!fwreq) {
4525 		ret = -ENOMEM;
4526 		goto fail;
4527 	}
4528 
4529 	ret = brcmf_fw_get_firmwares(sdiodev->dev, fwreq,
4530 				     brcmf_sdio_firmware_callback);
4531 	if (ret != 0) {
4532 		brcmf_err("async firmware request failed: %d\n", ret);
4533 		kfree(fwreq);
4534 		goto fail;
4535 	}
4536 
4537 	return bus;
4538 
4539 fail:
4540 	brcmf_sdio_remove(bus);
4541 	return NULL;
4542 }
4543 
4544 /* Detach and free everything */
brcmf_sdio_remove(struct brcmf_sdio * bus)4545 void brcmf_sdio_remove(struct brcmf_sdio *bus)
4546 {
4547 	brcmf_dbg(TRACE, "Enter\n");
4548 
4549 	if (bus) {
4550 		/* Stop watchdog task */
4551 		if (bus->watchdog_tsk) {
4552 			send_sig(SIGTERM, bus->watchdog_tsk, 1);
4553 			kthread_stop(bus->watchdog_tsk);
4554 			bus->watchdog_tsk = NULL;
4555 		}
4556 
4557 		/* De-register interrupt handler */
4558 		brcmf_sdiod_intr_unregister(bus->sdiodev);
4559 
4560 		brcmf_detach(bus->sdiodev->dev);
4561 		brcmf_free(bus->sdiodev->dev);
4562 
4563 		cancel_work_sync(&bus->datawork);
4564 		if (bus->brcmf_wq)
4565 			destroy_workqueue(bus->brcmf_wq);
4566 
4567 		if (bus->ci) {
4568 			if (bus->sdiodev->state != BRCMF_SDIOD_NOMEDIUM) {
4569 				sdio_claim_host(bus->sdiodev->func1);
4570 				brcmf_sdio_wd_timer(bus, false);
4571 				brcmf_sdio_clkctl(bus, CLK_AVAIL, false);
4572 				/* Leave the device in state where it is
4573 				 * 'passive'. This is done by resetting all
4574 				 * necessary cores.
4575 				 */
4576 				msleep(20);
4577 				brcmf_chip_set_passive(bus->ci);
4578 				brcmf_sdio_clkctl(bus, CLK_NONE, false);
4579 				sdio_release_host(bus->sdiodev->func1);
4580 			}
4581 			brcmf_chip_detach(bus->ci);
4582 		}
4583 		if (bus->sdiodev->settings)
4584 			brcmf_release_module_param(bus->sdiodev->settings);
4585 
4586 		release_firmware(bus->sdiodev->clm_fw);
4587 		bus->sdiodev->clm_fw = NULL;
4588 		kfree(bus->rxbuf);
4589 		kfree(bus->hdrbuf);
4590 		kfree(bus);
4591 	}
4592 
4593 	brcmf_dbg(TRACE, "Disconnected\n");
4594 }
4595 
brcmf_sdio_wd_timer(struct brcmf_sdio * bus,bool active)4596 void brcmf_sdio_wd_timer(struct brcmf_sdio *bus, bool active)
4597 {
4598 	/* Totally stop the timer */
4599 	if (!active && bus->wd_active) {
4600 		del_timer_sync(&bus->timer);
4601 		bus->wd_active = false;
4602 		return;
4603 	}
4604 
4605 	/* don't start the wd until fw is loaded */
4606 	if (bus->sdiodev->state != BRCMF_SDIOD_DATA)
4607 		return;
4608 
4609 	if (active) {
4610 		if (!bus->wd_active) {
4611 			/* Create timer again when watchdog period is
4612 			   dynamically changed or in the first instance
4613 			 */
4614 			bus->timer.expires = jiffies + BRCMF_WD_POLL;
4615 			add_timer(&bus->timer);
4616 			bus->wd_active = true;
4617 		} else {
4618 			/* Re arm the timer, at last watchdog period */
4619 			mod_timer(&bus->timer, jiffies + BRCMF_WD_POLL);
4620 		}
4621 	}
4622 }
4623 
brcmf_sdio_sleep(struct brcmf_sdio * bus,bool sleep)4624 int brcmf_sdio_sleep(struct brcmf_sdio *bus, bool sleep)
4625 {
4626 	int ret;
4627 
4628 	sdio_claim_host(bus->sdiodev->func1);
4629 	ret = brcmf_sdio_bus_sleep(bus, sleep, false);
4630 	sdio_release_host(bus->sdiodev->func1);
4631 
4632 	return ret;
4633 }
4634