cfef7d0fc5c4f67533bc1a0a2c4f8c126871b58c
[cascardo/linux.git] / drivers / net / wireless / ath / ath10k / ce.h
1 /*
2  * Copyright (c) 2005-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #ifndef _CE_H_
19 #define _CE_H_
20
21 #include "hif.h"
22
23
24 /* Maximum number of Copy Engine's supported */
25 #define CE_COUNT_MAX 8
26 #define CE_HTT_H2T_MSG_SRC_NENTRIES 2048
27
28 /* Descriptor rings must be aligned to this boundary */
29 #define CE_DESC_RING_ALIGN      8
30 #define CE_SENDLIST_ITEMS_MAX   12
31 #define CE_SEND_FLAG_GATHER     0x00010000
32
33 /*
34  * Copy Engine support: low-level Target-side Copy Engine API.
35  * This is a hardware access layer used by code that understands
36  * how to use copy engines.
37  */
38
39 struct ath10k_ce_pipe;
40
41
42 #define CE_DESC_FLAGS_GATHER         (1 << 0)
43 #define CE_DESC_FLAGS_BYTE_SWAP      (1 << 1)
44 #define CE_DESC_FLAGS_META_DATA_MASK 0xFFFC
45 #define CE_DESC_FLAGS_META_DATA_LSB  3
46
47 struct ce_desc {
48         __le32 addr;
49         __le16 nbytes;
50         __le16 flags; /* %CE_DESC_FLAGS_ */
51 };
52
53 struct ath10k_ce_ring {
54         /* Number of entries in this ring; must be power of 2 */
55         unsigned int nentries;
56         unsigned int nentries_mask;
57
58         /*
59          * For dest ring, this is the next index to be processed
60          * by software after it was/is received into.
61          *
62          * For src ring, this is the last descriptor that was sent
63          * and completion processed by software.
64          *
65          * Regardless of src or dest ring, this is an invariant
66          * (modulo ring size):
67          *     write index >= read index >= sw_index
68          */
69         unsigned int sw_index;
70         /* cached copy */
71         unsigned int write_index;
72         /*
73          * For src ring, this is the next index not yet processed by HW.
74          * This is a cached copy of the real HW index (read index), used
75          * for avoiding reading the HW index register more often than
76          * necessary.
77          * This extends the invariant:
78          *     write index >= read index >= hw_index >= sw_index
79          *
80          * For dest ring, this is currently unused.
81          */
82         /* cached copy */
83         unsigned int hw_index;
84
85         /* Start of DMA-coherent area reserved for descriptors */
86         /* Host address space */
87         void *base_addr_owner_space_unaligned;
88         /* CE address space */
89         u32 base_addr_ce_space_unaligned;
90
91         /*
92          * Actual start of descriptors.
93          * Aligned to descriptor-size boundary.
94          * Points into reserved DMA-coherent area, above.
95          */
96         /* Host address space */
97         void *base_addr_owner_space;
98
99         /* CE address space */
100         u32 base_addr_ce_space;
101         /*
102          * Start of shadow copy of descriptors, within regular memory.
103          * Aligned to descriptor-size boundary.
104          */
105         void *shadow_base_unaligned;
106         struct ce_desc *shadow_base;
107
108         void **per_transfer_context;
109 };
110
111 struct ath10k_ce_pipe {
112         struct ath10k *ar;
113         unsigned int id;
114
115         unsigned int attr_flags;
116
117         u32 ctrl_addr;
118
119         void (*send_cb) (struct ath10k_ce_pipe *ce_state,
120                          void *per_transfer_send_context,
121                          u32 buffer,
122                          unsigned int nbytes,
123                          unsigned int transfer_id);
124         void (*recv_cb) (struct ath10k_ce_pipe *ce_state,
125                          void *per_transfer_recv_context,
126                          u32 buffer,
127                          unsigned int nbytes,
128                          unsigned int transfer_id,
129                          unsigned int flags);
130
131         unsigned int src_sz_max;
132         struct ath10k_ce_ring *src_ring;
133         struct ath10k_ce_ring *dest_ring;
134 };
135
136 struct ce_sendlist_item {
137         /* e.g. buffer or desc list */
138         dma_addr_t data;
139         union {
140                 /* simple buffer */
141                 unsigned int nbytes;
142                 /* Rx descriptor list */
143                 unsigned int ndesc;
144         } u;
145         /* externally-specified flags; OR-ed with internal flags */
146         u32 flags;
147 };
148
149 struct ce_sendlist {
150         unsigned int num_items;
151         struct ce_sendlist_item item[CE_SENDLIST_ITEMS_MAX];
152 };
153
154 /* Copy Engine settable attributes */
155 struct ce_attr;
156
157 /*==================Send====================*/
158
159 /* ath10k_ce_send flags */
160 #define CE_SEND_FLAG_BYTE_SWAP 1
161
162 /*
163  * Queue a source buffer to be sent to an anonymous destination buffer.
164  *   ce         - which copy engine to use
165  *   buffer          - address of buffer
166  *   nbytes          - number of bytes to send
167  *   transfer_id     - arbitrary ID; reflected to destination
168  *   flags           - CE_SEND_FLAG_* values
169  * Returns 0 on success; otherwise an error status.
170  *
171  * Note: If no flags are specified, use CE's default data swap mode.
172  *
173  * Implementation note: pushes 1 buffer to Source ring
174  */
175 int ath10k_ce_send(struct ath10k_ce_pipe *ce_state,
176                    void *per_transfer_send_context,
177                    u32 buffer,
178                    unsigned int nbytes,
179                    /* 14 bits */
180                    unsigned int transfer_id,
181                    unsigned int flags);
182
183 void ath10k_ce_send_cb_register(struct ath10k_ce_pipe *ce_state,
184                                 void (*send_cb)(struct ath10k_ce_pipe *ce_state,
185                                                 void *transfer_context,
186                                                 u32 buffer,
187                                                 unsigned int nbytes,
188                                                 unsigned int transfer_id),
189                                 int disable_interrupts);
190
191 /* Append a simple buffer (address/length) to a sendlist. */
192 void ath10k_ce_sendlist_buf_add(struct ce_sendlist *sendlist,
193                                 u32 buffer,
194                                 unsigned int nbytes,
195                                 /* OR-ed with internal flags */
196                                 u32 flags);
197
198 /*
199  * Queue a "sendlist" of buffers to be sent using gather to a single
200  * anonymous destination buffer
201  *   ce         - which copy engine to use
202  *   sendlist        - list of simple buffers to send using gather
203  *   transfer_id     - arbitrary ID; reflected to destination
204  * Returns 0 on success; otherwise an error status.
205  *
206  * Implemenation note: Pushes multiple buffers with Gather to Source ring.
207  */
208 int ath10k_ce_sendlist_send(struct ath10k_ce_pipe *ce_state,
209                             void *per_transfer_send_context,
210                             struct ce_sendlist *sendlist,
211                             /* 14 bits */
212                             unsigned int transfer_id);
213
214 /*==================Recv=======================*/
215
216 /*
217  * Make a buffer available to receive. The buffer must be at least of a
218  * minimal size appropriate for this copy engine (src_sz_max attribute).
219  *   ce                    - which copy engine to use
220  *   per_transfer_recv_context  - context passed back to caller's recv_cb
221  *   buffer                     - address of buffer in CE space
222  * Returns 0 on success; otherwise an error status.
223  *
224  * Implemenation note: Pushes a buffer to Dest ring.
225  */
226 int ath10k_ce_recv_buf_enqueue(struct ath10k_ce_pipe *ce_state,
227                                void *per_transfer_recv_context,
228                                u32 buffer);
229
230 void ath10k_ce_recv_cb_register(struct ath10k_ce_pipe *ce_state,
231                                 void (*recv_cb)(struct ath10k_ce_pipe *ce_state,
232                                                 void *transfer_context,
233                                                 u32 buffer,
234                                                 unsigned int nbytes,
235                                                 unsigned int transfer_id,
236                                                 unsigned int flags));
237
238 /* recv flags */
239 /* Data is byte-swapped */
240 #define CE_RECV_FLAG_SWAPPED    1
241
242 /*
243  * Supply data for the next completed unprocessed receive descriptor.
244  * Pops buffer from Dest ring.
245  */
246 int ath10k_ce_completed_recv_next(struct ath10k_ce_pipe *ce_state,
247                                   void **per_transfer_contextp,
248                                   u32 *bufferp,
249                                   unsigned int *nbytesp,
250                                   unsigned int *transfer_idp,
251                                   unsigned int *flagsp);
252 /*
253  * Supply data for the next completed unprocessed send descriptor.
254  * Pops 1 completed send buffer from Source ring.
255  */
256 int ath10k_ce_completed_send_next(struct ath10k_ce_pipe *ce_state,
257                            void **per_transfer_contextp,
258                            u32 *bufferp,
259                            unsigned int *nbytesp,
260                            unsigned int *transfer_idp);
261
262 /*==================CE Engine Initialization=======================*/
263
264 /* Initialize an instance of a CE */
265 struct ath10k_ce_pipe *ath10k_ce_init(struct ath10k *ar,
266                                 unsigned int ce_id,
267                                 const struct ce_attr *attr);
268
269 /*==================CE Engine Shutdown=======================*/
270 /*
271  * Support clean shutdown by allowing the caller to revoke
272  * receive buffers.  Target DMA must be stopped before using
273  * this API.
274  */
275 int ath10k_ce_revoke_recv_next(struct ath10k_ce_pipe *ce_state,
276                                void **per_transfer_contextp,
277                                u32 *bufferp);
278
279 /*
280  * Support clean shutdown by allowing the caller to cancel
281  * pending sends.  Target DMA must be stopped before using
282  * this API.
283  */
284 int ath10k_ce_cancel_send_next(struct ath10k_ce_pipe *ce_state,
285                                void **per_transfer_contextp,
286                                u32 *bufferp,
287                                unsigned int *nbytesp,
288                                unsigned int *transfer_idp);
289
290 void ath10k_ce_deinit(struct ath10k_ce_pipe *ce_state);
291
292 /*==================CE Interrupt Handlers====================*/
293 void ath10k_ce_per_engine_service_any(struct ath10k *ar);
294 void ath10k_ce_per_engine_service(struct ath10k *ar, unsigned int ce_id);
295 void ath10k_ce_disable_interrupts(struct ath10k *ar);
296
297 /* ce_attr.flags values */
298 /* Use NonSnooping PCIe accesses? */
299 #define CE_ATTR_NO_SNOOP                1
300
301 /* Byte swap data words */
302 #define CE_ATTR_BYTE_SWAP_DATA          2
303
304 /* Swizzle descriptors? */
305 #define CE_ATTR_SWIZZLE_DESCRIPTORS     4
306
307 /* no interrupt on copy completion */
308 #define CE_ATTR_DIS_INTR                8
309
310 /* Attributes of an instance of a Copy Engine */
311 struct ce_attr {
312         /* CE_ATTR_* values */
313         unsigned int flags;
314
315         /* #entries in source ring - Must be a power of 2 */
316         unsigned int src_nentries;
317
318         /*
319          * Max source send size for this CE.
320          * This is also the minimum size of a destination buffer.
321          */
322         unsigned int src_sz_max;
323
324         /* #entries in destination ring - Must be a power of 2 */
325         unsigned int dest_nentries;
326 };
327
328 /*
329  * When using sendlist_send to transfer multiple buffer fragments, the
330  * transfer context of each fragment, except last one, will be filled
331  * with CE_SENDLIST_ITEM_CTXT. ce_completed_send will return success for
332  * each fragment done with send and the transfer context would be
333  * CE_SENDLIST_ITEM_CTXT. Upper layer could use this to identify the
334  * status of a send completion.
335  */
336 #define CE_SENDLIST_ITEM_CTXT   ((void *)0xcecebeef)
337
338 #define SR_BA_ADDRESS           0x0000
339 #define SR_SIZE_ADDRESS         0x0004
340 #define DR_BA_ADDRESS           0x0008
341 #define DR_SIZE_ADDRESS         0x000c
342 #define CE_CMD_ADDRESS          0x0018
343
344 #define CE_CTRL1_DST_RING_BYTE_SWAP_EN_MSB      17
345 #define CE_CTRL1_DST_RING_BYTE_SWAP_EN_LSB      17
346 #define CE_CTRL1_DST_RING_BYTE_SWAP_EN_MASK     0x00020000
347 #define CE_CTRL1_DST_RING_BYTE_SWAP_EN_SET(x) \
348         (((0 | (x)) << CE_CTRL1_DST_RING_BYTE_SWAP_EN_LSB) & \
349         CE_CTRL1_DST_RING_BYTE_SWAP_EN_MASK)
350
351 #define CE_CTRL1_SRC_RING_BYTE_SWAP_EN_MSB      16
352 #define CE_CTRL1_SRC_RING_BYTE_SWAP_EN_LSB      16
353 #define CE_CTRL1_SRC_RING_BYTE_SWAP_EN_MASK     0x00010000
354 #define CE_CTRL1_SRC_RING_BYTE_SWAP_EN_GET(x) \
355         (((x) & CE_CTRL1_SRC_RING_BYTE_SWAP_EN_MASK) >> \
356          CE_CTRL1_SRC_RING_BYTE_SWAP_EN_LSB)
357 #define CE_CTRL1_SRC_RING_BYTE_SWAP_EN_SET(x) \
358         (((0 | (x)) << CE_CTRL1_SRC_RING_BYTE_SWAP_EN_LSB) & \
359          CE_CTRL1_SRC_RING_BYTE_SWAP_EN_MASK)
360
361 #define CE_CTRL1_DMAX_LENGTH_MSB                15
362 #define CE_CTRL1_DMAX_LENGTH_LSB                0
363 #define CE_CTRL1_DMAX_LENGTH_MASK               0x0000ffff
364 #define CE_CTRL1_DMAX_LENGTH_GET(x) \
365         (((x) & CE_CTRL1_DMAX_LENGTH_MASK) >> CE_CTRL1_DMAX_LENGTH_LSB)
366 #define CE_CTRL1_DMAX_LENGTH_SET(x) \
367         (((0 | (x)) << CE_CTRL1_DMAX_LENGTH_LSB) & CE_CTRL1_DMAX_LENGTH_MASK)
368
369 #define CE_CTRL1_ADDRESS                        0x0010
370 #define CE_CTRL1_HW_MASK                        0x0007ffff
371 #define CE_CTRL1_SW_MASK                        0x0007ffff
372 #define CE_CTRL1_HW_WRITE_MASK                  0x00000000
373 #define CE_CTRL1_SW_WRITE_MASK                  0x0007ffff
374 #define CE_CTRL1_RSTMASK                        0xffffffff
375 #define CE_CTRL1_RESET                          0x00000080
376
377 #define CE_CMD_HALT_STATUS_MSB                  3
378 #define CE_CMD_HALT_STATUS_LSB                  3
379 #define CE_CMD_HALT_STATUS_MASK                 0x00000008
380 #define CE_CMD_HALT_STATUS_GET(x) \
381         (((x) & CE_CMD_HALT_STATUS_MASK) >> CE_CMD_HALT_STATUS_LSB)
382 #define CE_CMD_HALT_STATUS_SET(x) \
383         (((0 | (x)) << CE_CMD_HALT_STATUS_LSB) & CE_CMD_HALT_STATUS_MASK)
384 #define CE_CMD_HALT_STATUS_RESET                0
385 #define CE_CMD_HALT_MSB                         0
386 #define CE_CMD_HALT_MASK                        0x00000001
387
388 #define HOST_IE_COPY_COMPLETE_MSB               0
389 #define HOST_IE_COPY_COMPLETE_LSB               0
390 #define HOST_IE_COPY_COMPLETE_MASK              0x00000001
391 #define HOST_IE_COPY_COMPLETE_GET(x) \
392         (((x) & HOST_IE_COPY_COMPLETE_MASK) >> HOST_IE_COPY_COMPLETE_LSB)
393 #define HOST_IE_COPY_COMPLETE_SET(x) \
394         (((0 | (x)) << HOST_IE_COPY_COMPLETE_LSB) & HOST_IE_COPY_COMPLETE_MASK)
395 #define HOST_IE_COPY_COMPLETE_RESET             0
396 #define HOST_IE_ADDRESS                         0x002c
397
398 #define HOST_IS_DST_RING_LOW_WATERMARK_MASK     0x00000010
399 #define HOST_IS_DST_RING_HIGH_WATERMARK_MASK    0x00000008
400 #define HOST_IS_SRC_RING_LOW_WATERMARK_MASK     0x00000004
401 #define HOST_IS_SRC_RING_HIGH_WATERMARK_MASK    0x00000002
402 #define HOST_IS_COPY_COMPLETE_MASK              0x00000001
403 #define HOST_IS_ADDRESS                         0x0030
404
405 #define MISC_IE_ADDRESS                         0x0034
406
407 #define MISC_IS_AXI_ERR_MASK                    0x00000400
408
409 #define MISC_IS_DST_ADDR_ERR_MASK               0x00000200
410 #define MISC_IS_SRC_LEN_ERR_MASK                0x00000100
411 #define MISC_IS_DST_MAX_LEN_VIO_MASK            0x00000080
412 #define MISC_IS_DST_RING_OVERFLOW_MASK          0x00000040
413 #define MISC_IS_SRC_RING_OVERFLOW_MASK          0x00000020
414
415 #define MISC_IS_ADDRESS                         0x0038
416
417 #define SR_WR_INDEX_ADDRESS                     0x003c
418
419 #define DST_WR_INDEX_ADDRESS                    0x0040
420
421 #define CURRENT_SRRI_ADDRESS                    0x0044
422
423 #define CURRENT_DRRI_ADDRESS                    0x0048
424
425 #define SRC_WATERMARK_LOW_MSB                   31
426 #define SRC_WATERMARK_LOW_LSB                   16
427 #define SRC_WATERMARK_LOW_MASK                  0xffff0000
428 #define SRC_WATERMARK_LOW_GET(x) \
429         (((x) & SRC_WATERMARK_LOW_MASK) >> SRC_WATERMARK_LOW_LSB)
430 #define SRC_WATERMARK_LOW_SET(x) \
431         (((0 | (x)) << SRC_WATERMARK_LOW_LSB) & SRC_WATERMARK_LOW_MASK)
432 #define SRC_WATERMARK_LOW_RESET                 0
433 #define SRC_WATERMARK_HIGH_MSB                  15
434 #define SRC_WATERMARK_HIGH_LSB                  0
435 #define SRC_WATERMARK_HIGH_MASK                 0x0000ffff
436 #define SRC_WATERMARK_HIGH_GET(x) \
437         (((x) & SRC_WATERMARK_HIGH_MASK) >> SRC_WATERMARK_HIGH_LSB)
438 #define SRC_WATERMARK_HIGH_SET(x) \
439         (((0 | (x)) << SRC_WATERMARK_HIGH_LSB) & SRC_WATERMARK_HIGH_MASK)
440 #define SRC_WATERMARK_HIGH_RESET                0
441 #define SRC_WATERMARK_ADDRESS                   0x004c
442
443 #define DST_WATERMARK_LOW_LSB                   16
444 #define DST_WATERMARK_LOW_MASK                  0xffff0000
445 #define DST_WATERMARK_LOW_SET(x) \
446         (((0 | (x)) << DST_WATERMARK_LOW_LSB) & DST_WATERMARK_LOW_MASK)
447 #define DST_WATERMARK_LOW_RESET                 0
448 #define DST_WATERMARK_HIGH_MSB                  15
449 #define DST_WATERMARK_HIGH_LSB                  0
450 #define DST_WATERMARK_HIGH_MASK                 0x0000ffff
451 #define DST_WATERMARK_HIGH_GET(x) \
452         (((x) & DST_WATERMARK_HIGH_MASK) >> DST_WATERMARK_HIGH_LSB)
453 #define DST_WATERMARK_HIGH_SET(x) \
454         (((0 | (x)) << DST_WATERMARK_HIGH_LSB) & DST_WATERMARK_HIGH_MASK)
455 #define DST_WATERMARK_HIGH_RESET                0
456 #define DST_WATERMARK_ADDRESS                   0x0050
457
458
459 static inline u32 ath10k_ce_base_address(unsigned int ce_id)
460 {
461         return CE0_BASE_ADDRESS + (CE1_BASE_ADDRESS - CE0_BASE_ADDRESS) * ce_id;
462 }
463
464 #define CE_WATERMARK_MASK (HOST_IS_SRC_RING_LOW_WATERMARK_MASK  | \
465                            HOST_IS_SRC_RING_HIGH_WATERMARK_MASK | \
466                            HOST_IS_DST_RING_LOW_WATERMARK_MASK  | \
467                            HOST_IS_DST_RING_HIGH_WATERMARK_MASK)
468
469 #define CE_ERROR_MASK   (MISC_IS_AXI_ERR_MASK           | \
470                          MISC_IS_DST_ADDR_ERR_MASK      | \
471                          MISC_IS_SRC_LEN_ERR_MASK       | \
472                          MISC_IS_DST_MAX_LEN_VIO_MASK   | \
473                          MISC_IS_DST_RING_OVERFLOW_MASK | \
474                          MISC_IS_SRC_RING_OVERFLOW_MASK)
475
476 #define CE_SRC_RING_TO_DESC(baddr, idx) \
477         (&(((struct ce_desc *)baddr)[idx]))
478
479 #define CE_DEST_RING_TO_DESC(baddr, idx) \
480         (&(((struct ce_desc *)baddr)[idx]))
481
482 /* Ring arithmetic (modulus number of entries in ring, which is a pwr of 2). */
483 #define CE_RING_DELTA(nentries_mask, fromidx, toidx) \
484         (((int)(toidx)-(int)(fromidx)) & (nentries_mask))
485
486 #define CE_RING_IDX_INCR(nentries_mask, idx) (((idx) + 1) & (nentries_mask))
487
488 #define CE_WRAPPER_INTERRUPT_SUMMARY_HOST_MSI_LSB               8
489 #define CE_WRAPPER_INTERRUPT_SUMMARY_HOST_MSI_MASK              0x0000ff00
490 #define CE_WRAPPER_INTERRUPT_SUMMARY_HOST_MSI_GET(x) \
491         (((x) & CE_WRAPPER_INTERRUPT_SUMMARY_HOST_MSI_MASK) >> \
492                 CE_WRAPPER_INTERRUPT_SUMMARY_HOST_MSI_LSB)
493 #define CE_WRAPPER_INTERRUPT_SUMMARY_ADDRESS                    0x0000
494
495 #define CE_INTERRUPT_SUMMARY(ar) \
496         CE_WRAPPER_INTERRUPT_SUMMARY_HOST_MSI_GET( \
497                 ath10k_pci_read32((ar), CE_WRAPPER_BASE_ADDRESS + \
498                 CE_WRAPPER_INTERRUPT_SUMMARY_ADDRESS))
499
500 #endif /* _CE_H_ */