ath10k: add boot debug messages to pci.c and ce.c
[cascardo/linux.git] / drivers / net / wireless / ath / ath10k / ce.c
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 #include "hif.h"
19 #include "pci.h"
20 #include "ce.h"
21 #include "debug.h"
22
23 /*
24  * Support for Copy Engine hardware, which is mainly used for
25  * communication between Host and Target over a PCIe interconnect.
26  */
27
28 /*
29  * A single CopyEngine (CE) comprises two "rings":
30  *   a source ring
31  *   a destination ring
32  *
33  * Each ring consists of a number of descriptors which specify
34  * an address, length, and meta-data.
35  *
36  * Typically, one side of the PCIe interconnect (Host or Target)
37  * controls one ring and the other side controls the other ring.
38  * The source side chooses when to initiate a transfer and it
39  * chooses what to send (buffer address, length). The destination
40  * side keeps a supply of "anonymous receive buffers" available and
41  * it handles incoming data as it arrives (when the destination
42  * recieves an interrupt).
43  *
44  * The sender may send a simple buffer (address/length) or it may
45  * send a small list of buffers.  When a small list is sent, hardware
46  * "gathers" these and they end up in a single destination buffer
47  * with a single interrupt.
48  *
49  * There are several "contexts" managed by this layer -- more, it
50  * may seem -- than should be needed. These are provided mainly for
51  * maximum flexibility and especially to facilitate a simpler HIF
52  * implementation. There are per-CopyEngine recv, send, and watermark
53  * contexts. These are supplied by the caller when a recv, send,
54  * or watermark handler is established and they are echoed back to
55  * the caller when the respective callbacks are invoked. There is
56  * also a per-transfer context supplied by the caller when a buffer
57  * (or sendlist) is sent and when a buffer is enqueued for recv.
58  * These per-transfer contexts are echoed back to the caller when
59  * the buffer is sent/received.
60  */
61
62 static inline void ath10k_ce_dest_ring_write_index_set(struct ath10k *ar,
63                                                        u32 ce_ctrl_addr,
64                                                        unsigned int n)
65 {
66         ath10k_pci_write32(ar, ce_ctrl_addr + DST_WR_INDEX_ADDRESS, n);
67 }
68
69 static inline u32 ath10k_ce_dest_ring_write_index_get(struct ath10k *ar,
70                                                       u32 ce_ctrl_addr)
71 {
72         return ath10k_pci_read32(ar, ce_ctrl_addr + DST_WR_INDEX_ADDRESS);
73 }
74
75 static inline void ath10k_ce_src_ring_write_index_set(struct ath10k *ar,
76                                                       u32 ce_ctrl_addr,
77                                                       unsigned int n)
78 {
79         ath10k_pci_write32(ar, ce_ctrl_addr + SR_WR_INDEX_ADDRESS, n);
80 }
81
82 static inline u32 ath10k_ce_src_ring_write_index_get(struct ath10k *ar,
83                                                      u32 ce_ctrl_addr)
84 {
85         return ath10k_pci_read32(ar, ce_ctrl_addr + SR_WR_INDEX_ADDRESS);
86 }
87
88 static inline u32 ath10k_ce_src_ring_read_index_get(struct ath10k *ar,
89                                                     u32 ce_ctrl_addr)
90 {
91         return ath10k_pci_read32(ar, ce_ctrl_addr + CURRENT_SRRI_ADDRESS);
92 }
93
94 static inline void ath10k_ce_src_ring_base_addr_set(struct ath10k *ar,
95                                                     u32 ce_ctrl_addr,
96                                                     unsigned int addr)
97 {
98         ath10k_pci_write32(ar, ce_ctrl_addr + SR_BA_ADDRESS, addr);
99 }
100
101 static inline void ath10k_ce_src_ring_size_set(struct ath10k *ar,
102                                                u32 ce_ctrl_addr,
103                                                unsigned int n)
104 {
105         ath10k_pci_write32(ar, ce_ctrl_addr + SR_SIZE_ADDRESS, n);
106 }
107
108 static inline void ath10k_ce_src_ring_dmax_set(struct ath10k *ar,
109                                                u32 ce_ctrl_addr,
110                                                unsigned int n)
111 {
112         u32 ctrl1_addr = ath10k_pci_read32((ar),
113                                            (ce_ctrl_addr) + CE_CTRL1_ADDRESS);
114
115         ath10k_pci_write32(ar, ce_ctrl_addr + CE_CTRL1_ADDRESS,
116                            (ctrl1_addr &  ~CE_CTRL1_DMAX_LENGTH_MASK) |
117                            CE_CTRL1_DMAX_LENGTH_SET(n));
118 }
119
120 static inline void ath10k_ce_src_ring_byte_swap_set(struct ath10k *ar,
121                                                     u32 ce_ctrl_addr,
122                                                     unsigned int n)
123 {
124         u32 ctrl1_addr = ath10k_pci_read32(ar, ce_ctrl_addr + CE_CTRL1_ADDRESS);
125
126         ath10k_pci_write32(ar, ce_ctrl_addr + CE_CTRL1_ADDRESS,
127                            (ctrl1_addr & ~CE_CTRL1_SRC_RING_BYTE_SWAP_EN_MASK) |
128                            CE_CTRL1_SRC_RING_BYTE_SWAP_EN_SET(n));
129 }
130
131 static inline void ath10k_ce_dest_ring_byte_swap_set(struct ath10k *ar,
132                                                      u32 ce_ctrl_addr,
133                                                      unsigned int n)
134 {
135         u32 ctrl1_addr = ath10k_pci_read32(ar, ce_ctrl_addr + CE_CTRL1_ADDRESS);
136
137         ath10k_pci_write32(ar, ce_ctrl_addr + CE_CTRL1_ADDRESS,
138                            (ctrl1_addr & ~CE_CTRL1_DST_RING_BYTE_SWAP_EN_MASK) |
139                            CE_CTRL1_DST_RING_BYTE_SWAP_EN_SET(n));
140 }
141
142 static inline u32 ath10k_ce_dest_ring_read_index_get(struct ath10k *ar,
143                                                      u32 ce_ctrl_addr)
144 {
145         return ath10k_pci_read32(ar, ce_ctrl_addr + CURRENT_DRRI_ADDRESS);
146 }
147
148 static inline void ath10k_ce_dest_ring_base_addr_set(struct ath10k *ar,
149                                                      u32 ce_ctrl_addr,
150                                                      u32 addr)
151 {
152         ath10k_pci_write32(ar, ce_ctrl_addr + DR_BA_ADDRESS, addr);
153 }
154
155 static inline void ath10k_ce_dest_ring_size_set(struct ath10k *ar,
156                                                 u32 ce_ctrl_addr,
157                                                 unsigned int n)
158 {
159         ath10k_pci_write32(ar, ce_ctrl_addr + DR_SIZE_ADDRESS, n);
160 }
161
162 static inline void ath10k_ce_src_ring_highmark_set(struct ath10k *ar,
163                                                    u32 ce_ctrl_addr,
164                                                    unsigned int n)
165 {
166         u32 addr = ath10k_pci_read32(ar, ce_ctrl_addr + SRC_WATERMARK_ADDRESS);
167
168         ath10k_pci_write32(ar, ce_ctrl_addr + SRC_WATERMARK_ADDRESS,
169                            (addr & ~SRC_WATERMARK_HIGH_MASK) |
170                            SRC_WATERMARK_HIGH_SET(n));
171 }
172
173 static inline void ath10k_ce_src_ring_lowmark_set(struct ath10k *ar,
174                                                   u32 ce_ctrl_addr,
175                                                   unsigned int n)
176 {
177         u32 addr = ath10k_pci_read32(ar, ce_ctrl_addr + SRC_WATERMARK_ADDRESS);
178
179         ath10k_pci_write32(ar, ce_ctrl_addr + SRC_WATERMARK_ADDRESS,
180                            (addr & ~SRC_WATERMARK_LOW_MASK) |
181                            SRC_WATERMARK_LOW_SET(n));
182 }
183
184 static inline void ath10k_ce_dest_ring_highmark_set(struct ath10k *ar,
185                                                     u32 ce_ctrl_addr,
186                                                     unsigned int n)
187 {
188         u32 addr = ath10k_pci_read32(ar, ce_ctrl_addr + DST_WATERMARK_ADDRESS);
189
190         ath10k_pci_write32(ar, ce_ctrl_addr + DST_WATERMARK_ADDRESS,
191                            (addr & ~DST_WATERMARK_HIGH_MASK) |
192                            DST_WATERMARK_HIGH_SET(n));
193 }
194
195 static inline void ath10k_ce_dest_ring_lowmark_set(struct ath10k *ar,
196                                                    u32 ce_ctrl_addr,
197                                                    unsigned int n)
198 {
199         u32 addr = ath10k_pci_read32(ar, ce_ctrl_addr + DST_WATERMARK_ADDRESS);
200
201         ath10k_pci_write32(ar, ce_ctrl_addr + DST_WATERMARK_ADDRESS,
202                            (addr & ~DST_WATERMARK_LOW_MASK) |
203                            DST_WATERMARK_LOW_SET(n));
204 }
205
206 static inline void ath10k_ce_copy_complete_inter_enable(struct ath10k *ar,
207                                                         u32 ce_ctrl_addr)
208 {
209         u32 host_ie_addr = ath10k_pci_read32(ar,
210                                              ce_ctrl_addr + HOST_IE_ADDRESS);
211
212         ath10k_pci_write32(ar, ce_ctrl_addr + HOST_IE_ADDRESS,
213                            host_ie_addr | HOST_IE_COPY_COMPLETE_MASK);
214 }
215
216 static inline void ath10k_ce_copy_complete_intr_disable(struct ath10k *ar,
217                                                         u32 ce_ctrl_addr)
218 {
219         u32 host_ie_addr = ath10k_pci_read32(ar,
220                                              ce_ctrl_addr + HOST_IE_ADDRESS);
221
222         ath10k_pci_write32(ar, ce_ctrl_addr + HOST_IE_ADDRESS,
223                            host_ie_addr & ~HOST_IE_COPY_COMPLETE_MASK);
224 }
225
226 static inline void ath10k_ce_watermark_intr_disable(struct ath10k *ar,
227                                                     u32 ce_ctrl_addr)
228 {
229         u32 host_ie_addr = ath10k_pci_read32(ar,
230                                              ce_ctrl_addr + HOST_IE_ADDRESS);
231
232         ath10k_pci_write32(ar, ce_ctrl_addr + HOST_IE_ADDRESS,
233                            host_ie_addr & ~CE_WATERMARK_MASK);
234 }
235
236 static inline void ath10k_ce_error_intr_enable(struct ath10k *ar,
237                                                u32 ce_ctrl_addr)
238 {
239         u32 misc_ie_addr = ath10k_pci_read32(ar,
240                                              ce_ctrl_addr + MISC_IE_ADDRESS);
241
242         ath10k_pci_write32(ar, ce_ctrl_addr + MISC_IE_ADDRESS,
243                            misc_ie_addr | CE_ERROR_MASK);
244 }
245
246 static inline void ath10k_ce_engine_int_status_clear(struct ath10k *ar,
247                                                      u32 ce_ctrl_addr,
248                                                      unsigned int mask)
249 {
250         ath10k_pci_write32(ar, ce_ctrl_addr + HOST_IS_ADDRESS, mask);
251 }
252
253
254 /*
255  * Guts of ath10k_ce_send, used by both ath10k_ce_send and
256  * ath10k_ce_sendlist_send.
257  * The caller takes responsibility for any needed locking.
258  */
259 static int ath10k_ce_send_nolock(struct ath10k_ce_pipe *ce_state,
260                                  void *per_transfer_context,
261                                  u32 buffer,
262                                  unsigned int nbytes,
263                                  unsigned int transfer_id,
264                                  unsigned int flags)
265 {
266         struct ath10k *ar = ce_state->ar;
267         struct ath10k_ce_ring *src_ring = ce_state->src_ring;
268         struct ce_desc *desc, *sdesc;
269         unsigned int nentries_mask = src_ring->nentries_mask;
270         unsigned int sw_index = src_ring->sw_index;
271         unsigned int write_index = src_ring->write_index;
272         u32 ctrl_addr = ce_state->ctrl_addr;
273         u32 desc_flags = 0;
274         int ret = 0;
275
276         if (nbytes > ce_state->src_sz_max)
277                 ath10k_warn("%s: send more we can (nbytes: %d, max: %d)\n",
278                             __func__, nbytes, ce_state->src_sz_max);
279
280         ret = ath10k_pci_wake(ar);
281         if (ret)
282                 return ret;
283
284         if (unlikely(CE_RING_DELTA(nentries_mask,
285                                    write_index, sw_index - 1) <= 0)) {
286                 ret = -EIO;
287                 goto exit;
288         }
289
290         desc = CE_SRC_RING_TO_DESC(src_ring->base_addr_owner_space,
291                                    write_index);
292         sdesc = CE_SRC_RING_TO_DESC(src_ring->shadow_base, write_index);
293
294         desc_flags |= SM(transfer_id, CE_DESC_FLAGS_META_DATA);
295
296         if (flags & CE_SEND_FLAG_GATHER)
297                 desc_flags |= CE_DESC_FLAGS_GATHER;
298         if (flags & CE_SEND_FLAG_BYTE_SWAP)
299                 desc_flags |= CE_DESC_FLAGS_BYTE_SWAP;
300
301         sdesc->addr   = __cpu_to_le32(buffer);
302         sdesc->nbytes = __cpu_to_le16(nbytes);
303         sdesc->flags  = __cpu_to_le16(desc_flags);
304
305         *desc = *sdesc;
306
307         src_ring->per_transfer_context[write_index] = per_transfer_context;
308
309         /* Update Source Ring Write Index */
310         write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
311
312         /* WORKAROUND */
313         if (!(flags & CE_SEND_FLAG_GATHER))
314                 ath10k_ce_src_ring_write_index_set(ar, ctrl_addr, write_index);
315
316         src_ring->write_index = write_index;
317 exit:
318         ath10k_pci_sleep(ar);
319         return ret;
320 }
321
322 int ath10k_ce_send(struct ath10k_ce_pipe *ce_state,
323                    void *per_transfer_context,
324                    u32 buffer,
325                    unsigned int nbytes,
326                    unsigned int transfer_id,
327                    unsigned int flags)
328 {
329         struct ath10k *ar = ce_state->ar;
330         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
331         int ret;
332
333         spin_lock_bh(&ar_pci->ce_lock);
334         ret = ath10k_ce_send_nolock(ce_state, per_transfer_context,
335                                     buffer, nbytes, transfer_id, flags);
336         spin_unlock_bh(&ar_pci->ce_lock);
337
338         return ret;
339 }
340
341 void ath10k_ce_sendlist_buf_add(struct ce_sendlist *sendlist, u32 buffer,
342                                 unsigned int nbytes, u32 flags)
343 {
344         unsigned int num_items = sendlist->num_items;
345         struct ce_sendlist_item *item;
346
347         item = &sendlist->item[num_items];
348         item->data = buffer;
349         item->u.nbytes = nbytes;
350         item->flags = flags;
351         sendlist->num_items++;
352 }
353
354 int ath10k_ce_sendlist_send(struct ath10k_ce_pipe *ce_state,
355                             void *per_transfer_context,
356                             struct ce_sendlist *sendlist,
357                             unsigned int transfer_id)
358 {
359         struct ath10k_ce_ring *src_ring = ce_state->src_ring;
360         struct ce_sendlist_item *item;
361         struct ath10k *ar = ce_state->ar;
362         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
363         unsigned int nentries_mask = src_ring->nentries_mask;
364         unsigned int num_items = sendlist->num_items;
365         unsigned int sw_index;
366         unsigned int write_index;
367         int i, delta, ret = -ENOMEM;
368
369         spin_lock_bh(&ar_pci->ce_lock);
370
371         sw_index = src_ring->sw_index;
372         write_index = src_ring->write_index;
373
374         delta = CE_RING_DELTA(nentries_mask, write_index, sw_index - 1);
375
376         if (delta >= num_items) {
377                 /*
378                  * Handle all but the last item uniformly.
379                  */
380                 for (i = 0; i < num_items - 1; i++) {
381                         item = &sendlist->item[i];
382                         ret = ath10k_ce_send_nolock(ce_state,
383                                                     CE_SENDLIST_ITEM_CTXT,
384                                                     (u32) item->data,
385                                                     item->u.nbytes, transfer_id,
386                                                     item->flags |
387                                                     CE_SEND_FLAG_GATHER);
388                         if (ret)
389                                 ath10k_warn("CE send failed for item: %d\n", i);
390                 }
391                 /*
392                  * Provide valid context pointer for final item.
393                  */
394                 item = &sendlist->item[i];
395                 ret = ath10k_ce_send_nolock(ce_state, per_transfer_context,
396                                             (u32) item->data, item->u.nbytes,
397                                             transfer_id, item->flags);
398                 if (ret)
399                         ath10k_warn("CE send failed for last item: %d\n", i);
400         }
401
402         spin_unlock_bh(&ar_pci->ce_lock);
403
404         return ret;
405 }
406
407 int ath10k_ce_recv_buf_enqueue(struct ath10k_ce_pipe *ce_state,
408                                void *per_recv_context,
409                                u32 buffer)
410 {
411         struct ath10k_ce_ring *dest_ring = ce_state->dest_ring;
412         u32 ctrl_addr = ce_state->ctrl_addr;
413         struct ath10k *ar = ce_state->ar;
414         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
415         unsigned int nentries_mask = dest_ring->nentries_mask;
416         unsigned int write_index;
417         unsigned int sw_index;
418         int ret;
419
420         spin_lock_bh(&ar_pci->ce_lock);
421         write_index = dest_ring->write_index;
422         sw_index = dest_ring->sw_index;
423
424         ret = ath10k_pci_wake(ar);
425         if (ret)
426                 goto out;
427
428         if (CE_RING_DELTA(nentries_mask, write_index, sw_index - 1) > 0) {
429                 struct ce_desc *base = dest_ring->base_addr_owner_space;
430                 struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, write_index);
431
432                 /* Update destination descriptor */
433                 desc->addr    = __cpu_to_le32(buffer);
434                 desc->nbytes = 0;
435
436                 dest_ring->per_transfer_context[write_index] =
437                                                         per_recv_context;
438
439                 /* Update Destination Ring Write Index */
440                 write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
441                 ath10k_ce_dest_ring_write_index_set(ar, ctrl_addr, write_index);
442                 dest_ring->write_index = write_index;
443                 ret = 0;
444         } else {
445                 ret = -EIO;
446         }
447         ath10k_pci_sleep(ar);
448
449 out:
450         spin_unlock_bh(&ar_pci->ce_lock);
451
452         return ret;
453 }
454
455 /*
456  * Guts of ath10k_ce_completed_recv_next.
457  * The caller takes responsibility for any necessary locking.
458  */
459 static int ath10k_ce_completed_recv_next_nolock(struct ath10k_ce_pipe *ce_state,
460                                                 void **per_transfer_contextp,
461                                                 u32 *bufferp,
462                                                 unsigned int *nbytesp,
463                                                 unsigned int *transfer_idp,
464                                                 unsigned int *flagsp)
465 {
466         struct ath10k_ce_ring *dest_ring = ce_state->dest_ring;
467         unsigned int nentries_mask = dest_ring->nentries_mask;
468         unsigned int sw_index = dest_ring->sw_index;
469
470         struct ce_desc *base = dest_ring->base_addr_owner_space;
471         struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, sw_index);
472         struct ce_desc sdesc;
473         u16 nbytes;
474
475         /* Copy in one go for performance reasons */
476         sdesc = *desc;
477
478         nbytes = __le16_to_cpu(sdesc.nbytes);
479         if (nbytes == 0) {
480                 /*
481                  * This closes a relatively unusual race where the Host
482                  * sees the updated DRRI before the update to the
483                  * corresponding descriptor has completed. We treat this
484                  * as a descriptor that is not yet done.
485                  */
486                 return -EIO;
487         }
488
489         desc->nbytes = 0;
490
491         /* Return data from completed destination descriptor */
492         *bufferp = __le32_to_cpu(sdesc.addr);
493         *nbytesp = nbytes;
494         *transfer_idp = MS(__le16_to_cpu(sdesc.flags), CE_DESC_FLAGS_META_DATA);
495
496         if (__le16_to_cpu(sdesc.flags) & CE_DESC_FLAGS_BYTE_SWAP)
497                 *flagsp = CE_RECV_FLAG_SWAPPED;
498         else
499                 *flagsp = 0;
500
501         if (per_transfer_contextp)
502                 *per_transfer_contextp =
503                         dest_ring->per_transfer_context[sw_index];
504
505         /* sanity */
506         dest_ring->per_transfer_context[sw_index] = NULL;
507
508         /* Update sw_index */
509         sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
510         dest_ring->sw_index = sw_index;
511
512         return 0;
513 }
514
515 int ath10k_ce_completed_recv_next(struct ath10k_ce_pipe *ce_state,
516                                   void **per_transfer_contextp,
517                                   u32 *bufferp,
518                                   unsigned int *nbytesp,
519                                   unsigned int *transfer_idp,
520                                   unsigned int *flagsp)
521 {
522         struct ath10k *ar = ce_state->ar;
523         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
524         int ret;
525
526         spin_lock_bh(&ar_pci->ce_lock);
527         ret = ath10k_ce_completed_recv_next_nolock(ce_state,
528                                                    per_transfer_contextp,
529                                                    bufferp, nbytesp,
530                                                    transfer_idp, flagsp);
531         spin_unlock_bh(&ar_pci->ce_lock);
532
533         return ret;
534 }
535
536 int ath10k_ce_revoke_recv_next(struct ath10k_ce_pipe *ce_state,
537                                void **per_transfer_contextp,
538                                u32 *bufferp)
539 {
540         struct ath10k_ce_ring *dest_ring;
541         unsigned int nentries_mask;
542         unsigned int sw_index;
543         unsigned int write_index;
544         int ret;
545         struct ath10k *ar;
546         struct ath10k_pci *ar_pci;
547
548         dest_ring = ce_state->dest_ring;
549
550         if (!dest_ring)
551                 return -EIO;
552
553         ar = ce_state->ar;
554         ar_pci = ath10k_pci_priv(ar);
555
556         spin_lock_bh(&ar_pci->ce_lock);
557
558         nentries_mask = dest_ring->nentries_mask;
559         sw_index = dest_ring->sw_index;
560         write_index = dest_ring->write_index;
561         if (write_index != sw_index) {
562                 struct ce_desc *base = dest_ring->base_addr_owner_space;
563                 struct ce_desc *desc = CE_DEST_RING_TO_DESC(base, sw_index);
564
565                 /* Return data from completed destination descriptor */
566                 *bufferp = __le32_to_cpu(desc->addr);
567
568                 if (per_transfer_contextp)
569                         *per_transfer_contextp =
570                                 dest_ring->per_transfer_context[sw_index];
571
572                 /* sanity */
573                 dest_ring->per_transfer_context[sw_index] = NULL;
574
575                 /* Update sw_index */
576                 sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
577                 dest_ring->sw_index = sw_index;
578                 ret = 0;
579         } else {
580                 ret = -EIO;
581         }
582
583         spin_unlock_bh(&ar_pci->ce_lock);
584
585         return ret;
586 }
587
588 /*
589  * Guts of ath10k_ce_completed_send_next.
590  * The caller takes responsibility for any necessary locking.
591  */
592 static int ath10k_ce_completed_send_next_nolock(struct ath10k_ce_pipe *ce_state,
593                                                 void **per_transfer_contextp,
594                                                 u32 *bufferp,
595                                                 unsigned int *nbytesp,
596                                                 unsigned int *transfer_idp)
597 {
598         struct ath10k_ce_ring *src_ring = ce_state->src_ring;
599         u32 ctrl_addr = ce_state->ctrl_addr;
600         struct ath10k *ar = ce_state->ar;
601         unsigned int nentries_mask = src_ring->nentries_mask;
602         unsigned int sw_index = src_ring->sw_index;
603         struct ce_desc *sdesc, *sbase;
604         unsigned int read_index;
605         int ret;
606
607         if (src_ring->hw_index == sw_index) {
608                 /*
609                  * The SW completion index has caught up with the cached
610                  * version of the HW completion index.
611                  * Update the cached HW completion index to see whether
612                  * the SW has really caught up to the HW, or if the cached
613                  * value of the HW index has become stale.
614                  */
615
616                 ret = ath10k_pci_wake(ar);
617                 if (ret)
618                         return ret;
619
620                 src_ring->hw_index =
621                         ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
622                 src_ring->hw_index &= nentries_mask;
623
624                 ath10k_pci_sleep(ar);
625         }
626
627         read_index = src_ring->hw_index;
628
629         if ((read_index == sw_index) || (read_index == 0xffffffff))
630                 return -EIO;
631
632         sbase = src_ring->shadow_base;
633         sdesc = CE_SRC_RING_TO_DESC(sbase, sw_index);
634
635         /* Return data from completed source descriptor */
636         *bufferp = __le32_to_cpu(sdesc->addr);
637         *nbytesp = __le16_to_cpu(sdesc->nbytes);
638         *transfer_idp = MS(__le16_to_cpu(sdesc->flags),
639                            CE_DESC_FLAGS_META_DATA);
640
641         if (per_transfer_contextp)
642                 *per_transfer_contextp =
643                         src_ring->per_transfer_context[sw_index];
644
645         /* sanity */
646         src_ring->per_transfer_context[sw_index] = NULL;
647
648         /* Update sw_index */
649         sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
650         src_ring->sw_index = sw_index;
651
652         return 0;
653 }
654
655 /* NB: Modeled after ath10k_ce_completed_send_next */
656 int ath10k_ce_cancel_send_next(struct ath10k_ce_pipe *ce_state,
657                                void **per_transfer_contextp,
658                                u32 *bufferp,
659                                unsigned int *nbytesp,
660                                unsigned int *transfer_idp)
661 {
662         struct ath10k_ce_ring *src_ring;
663         unsigned int nentries_mask;
664         unsigned int sw_index;
665         unsigned int write_index;
666         int ret;
667         struct ath10k *ar;
668         struct ath10k_pci *ar_pci;
669
670         src_ring = ce_state->src_ring;
671
672         if (!src_ring)
673                 return -EIO;
674
675         ar = ce_state->ar;
676         ar_pci = ath10k_pci_priv(ar);
677
678         spin_lock_bh(&ar_pci->ce_lock);
679
680         nentries_mask = src_ring->nentries_mask;
681         sw_index = src_ring->sw_index;
682         write_index = src_ring->write_index;
683
684         if (write_index != sw_index) {
685                 struct ce_desc *base = src_ring->base_addr_owner_space;
686                 struct ce_desc *desc = CE_SRC_RING_TO_DESC(base, sw_index);
687
688                 /* Return data from completed source descriptor */
689                 *bufferp = __le32_to_cpu(desc->addr);
690                 *nbytesp = __le16_to_cpu(desc->nbytes);
691                 *transfer_idp = MS(__le16_to_cpu(desc->flags),
692                                                 CE_DESC_FLAGS_META_DATA);
693
694                 if (per_transfer_contextp)
695                         *per_transfer_contextp =
696                                 src_ring->per_transfer_context[sw_index];
697
698                 /* sanity */
699                 src_ring->per_transfer_context[sw_index] = NULL;
700
701                 /* Update sw_index */
702                 sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
703                 src_ring->sw_index = sw_index;
704                 ret = 0;
705         } else {
706                 ret = -EIO;
707         }
708
709         spin_unlock_bh(&ar_pci->ce_lock);
710
711         return ret;
712 }
713
714 int ath10k_ce_completed_send_next(struct ath10k_ce_pipe *ce_state,
715                                   void **per_transfer_contextp,
716                                   u32 *bufferp,
717                                   unsigned int *nbytesp,
718                                   unsigned int *transfer_idp)
719 {
720         struct ath10k *ar = ce_state->ar;
721         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
722         int ret;
723
724         spin_lock_bh(&ar_pci->ce_lock);
725         ret = ath10k_ce_completed_send_next_nolock(ce_state,
726                                                    per_transfer_contextp,
727                                                    bufferp, nbytesp,
728                                                    transfer_idp);
729         spin_unlock_bh(&ar_pci->ce_lock);
730
731         return ret;
732 }
733
734 /*
735  * Guts of interrupt handler for per-engine interrupts on a particular CE.
736  *
737  * Invokes registered callbacks for recv_complete,
738  * send_complete, and watermarks.
739  */
740 void ath10k_ce_per_engine_service(struct ath10k *ar, unsigned int ce_id)
741 {
742         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
743         struct ath10k_ce_pipe *ce_state = &ar_pci->ce_states[ce_id];
744         u32 ctrl_addr = ce_state->ctrl_addr;
745         int ret;
746
747         ret = ath10k_pci_wake(ar);
748         if (ret)
749                 return;
750
751         spin_lock_bh(&ar_pci->ce_lock);
752
753         /* Clear the copy-complete interrupts that will be handled here. */
754         ath10k_ce_engine_int_status_clear(ar, ctrl_addr,
755                                           HOST_IS_COPY_COMPLETE_MASK);
756
757         spin_unlock_bh(&ar_pci->ce_lock);
758
759         if (ce_state->recv_cb)
760                 ce_state->recv_cb(ce_state);
761
762         if (ce_state->send_cb)
763                 ce_state->send_cb(ce_state);
764
765         spin_lock_bh(&ar_pci->ce_lock);
766
767         /*
768          * Misc CE interrupts are not being handled, but still need
769          * to be cleared.
770          */
771         ath10k_ce_engine_int_status_clear(ar, ctrl_addr, CE_WATERMARK_MASK);
772
773         spin_unlock_bh(&ar_pci->ce_lock);
774         ath10k_pci_sleep(ar);
775 }
776
777 /*
778  * Handler for per-engine interrupts on ALL active CEs.
779  * This is used in cases where the system is sharing a
780  * single interrput for all CEs
781  */
782
783 void ath10k_ce_per_engine_service_any(struct ath10k *ar)
784 {
785         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
786         int ce_id, ret;
787         u32 intr_summary;
788
789         ret = ath10k_pci_wake(ar);
790         if (ret)
791                 return;
792
793         intr_summary = CE_INTERRUPT_SUMMARY(ar);
794
795         for (ce_id = 0; intr_summary && (ce_id < ar_pci->ce_count); ce_id++) {
796                 if (intr_summary & (1 << ce_id))
797                         intr_summary &= ~(1 << ce_id);
798                 else
799                         /* no intr pending on this CE */
800                         continue;
801
802                 ath10k_ce_per_engine_service(ar, ce_id);
803         }
804
805         ath10k_pci_sleep(ar);
806 }
807
808 /*
809  * Adjust interrupts for the copy complete handler.
810  * If it's needed for either send or recv, then unmask
811  * this interrupt; otherwise, mask it.
812  *
813  * Called with ce_lock held.
814  */
815 static void ath10k_ce_per_engine_handler_adjust(struct ath10k_ce_pipe *ce_state,
816                                                 int disable_copy_compl_intr)
817 {
818         u32 ctrl_addr = ce_state->ctrl_addr;
819         struct ath10k *ar = ce_state->ar;
820         int ret;
821
822         ret = ath10k_pci_wake(ar);
823         if (ret)
824                 return;
825
826         if ((!disable_copy_compl_intr) &&
827             (ce_state->send_cb || ce_state->recv_cb))
828                 ath10k_ce_copy_complete_inter_enable(ar, ctrl_addr);
829         else
830                 ath10k_ce_copy_complete_intr_disable(ar, ctrl_addr);
831
832         ath10k_ce_watermark_intr_disable(ar, ctrl_addr);
833
834         ath10k_pci_sleep(ar);
835 }
836
837 void ath10k_ce_disable_interrupts(struct ath10k *ar)
838 {
839         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
840         int ce_id, ret;
841
842         ret = ath10k_pci_wake(ar);
843         if (ret)
844                 return;
845
846         for (ce_id = 0; ce_id < ar_pci->ce_count; ce_id++) {
847                 struct ath10k_ce_pipe *ce_state = &ar_pci->ce_states[ce_id];
848                 u32 ctrl_addr = ce_state->ctrl_addr;
849
850                 ath10k_ce_copy_complete_intr_disable(ar, ctrl_addr);
851         }
852         ath10k_pci_sleep(ar);
853 }
854
855 void ath10k_ce_send_cb_register(struct ath10k_ce_pipe *ce_state,
856                                 void (*send_cb)(struct ath10k_ce_pipe *),
857                                 int disable_interrupts)
858 {
859         struct ath10k *ar = ce_state->ar;
860         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
861
862         spin_lock_bh(&ar_pci->ce_lock);
863         ce_state->send_cb = send_cb;
864         ath10k_ce_per_engine_handler_adjust(ce_state, disable_interrupts);
865         spin_unlock_bh(&ar_pci->ce_lock);
866 }
867
868 void ath10k_ce_recv_cb_register(struct ath10k_ce_pipe *ce_state,
869                                 void (*recv_cb)(struct ath10k_ce_pipe *))
870 {
871         struct ath10k *ar = ce_state->ar;
872         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
873
874         spin_lock_bh(&ar_pci->ce_lock);
875         ce_state->recv_cb = recv_cb;
876         ath10k_ce_per_engine_handler_adjust(ce_state, 0);
877         spin_unlock_bh(&ar_pci->ce_lock);
878 }
879
880 static int ath10k_ce_init_src_ring(struct ath10k *ar,
881                                    unsigned int ce_id,
882                                    struct ath10k_ce_pipe *ce_state,
883                                    const struct ce_attr *attr)
884 {
885         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
886         struct ath10k_ce_ring *src_ring;
887         unsigned int nentries = attr->src_nentries;
888         unsigned int ce_nbytes;
889         u32 ctrl_addr = ath10k_ce_base_address(ce_id);
890         dma_addr_t base_addr;
891         char *ptr;
892
893         nentries = roundup_pow_of_two(nentries);
894
895         if (ce_state->src_ring) {
896                 WARN_ON(ce_state->src_ring->nentries != nentries);
897                 return 0;
898         }
899
900         ce_nbytes = sizeof(struct ath10k_ce_ring) + (nentries * sizeof(void *));
901         ptr = kzalloc(ce_nbytes, GFP_KERNEL);
902         if (ptr == NULL)
903                 return -ENOMEM;
904
905         ce_state->src_ring = (struct ath10k_ce_ring *)ptr;
906         src_ring = ce_state->src_ring;
907
908         ptr += sizeof(struct ath10k_ce_ring);
909         src_ring->nentries = nentries;
910         src_ring->nentries_mask = nentries - 1;
911
912         src_ring->sw_index = ath10k_ce_src_ring_read_index_get(ar, ctrl_addr);
913         src_ring->sw_index &= src_ring->nentries_mask;
914         src_ring->hw_index = src_ring->sw_index;
915
916         src_ring->write_index =
917                 ath10k_ce_src_ring_write_index_get(ar, ctrl_addr);
918         src_ring->write_index &= src_ring->nentries_mask;
919
920         src_ring->per_transfer_context = (void **)ptr;
921
922         /*
923          * Legacy platforms that do not support cache
924          * coherent DMA are unsupported
925          */
926         src_ring->base_addr_owner_space_unaligned =
927                 pci_alloc_consistent(ar_pci->pdev,
928                                      (nentries * sizeof(struct ce_desc) +
929                                       CE_DESC_RING_ALIGN),
930                                      &base_addr);
931         if (!src_ring->base_addr_owner_space_unaligned) {
932                 kfree(ce_state->src_ring);
933                 ce_state->src_ring = NULL;
934                 return -ENOMEM;
935         }
936
937         src_ring->base_addr_ce_space_unaligned = base_addr;
938
939         src_ring->base_addr_owner_space = PTR_ALIGN(
940                         src_ring->base_addr_owner_space_unaligned,
941                         CE_DESC_RING_ALIGN);
942         src_ring->base_addr_ce_space = ALIGN(
943                         src_ring->base_addr_ce_space_unaligned,
944                         CE_DESC_RING_ALIGN);
945
946         /*
947          * Also allocate a shadow src ring in regular
948          * mem to use for faster access.
949          */
950         src_ring->shadow_base_unaligned =
951                 kmalloc((nentries * sizeof(struct ce_desc) +
952                          CE_DESC_RING_ALIGN), GFP_KERNEL);
953         if (!src_ring->shadow_base_unaligned) {
954                 pci_free_consistent(ar_pci->pdev,
955                                     (nentries * sizeof(struct ce_desc) +
956                                      CE_DESC_RING_ALIGN),
957                                     src_ring->base_addr_owner_space,
958                                     src_ring->base_addr_ce_space);
959                 kfree(ce_state->src_ring);
960                 ce_state->src_ring = NULL;
961                 return -ENOMEM;
962         }
963
964         src_ring->shadow_base = PTR_ALIGN(
965                         src_ring->shadow_base_unaligned,
966                         CE_DESC_RING_ALIGN);
967
968         ath10k_ce_src_ring_base_addr_set(ar, ctrl_addr,
969                                          src_ring->base_addr_ce_space);
970         ath10k_ce_src_ring_size_set(ar, ctrl_addr, nentries);
971         ath10k_ce_src_ring_dmax_set(ar, ctrl_addr, attr->src_sz_max);
972         ath10k_ce_src_ring_byte_swap_set(ar, ctrl_addr, 0);
973         ath10k_ce_src_ring_lowmark_set(ar, ctrl_addr, 0);
974         ath10k_ce_src_ring_highmark_set(ar, ctrl_addr, nentries);
975
976         ath10k_dbg(ATH10K_DBG_BOOT,
977                    "boot ce src ring id %d entries %d base_addr %p\n",
978                    ce_id, nentries, src_ring->base_addr_owner_space);
979
980         return 0;
981 }
982
983 static int ath10k_ce_init_dest_ring(struct ath10k *ar,
984                                     unsigned int ce_id,
985                                     struct ath10k_ce_pipe *ce_state,
986                                     const struct ce_attr *attr)
987 {
988         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
989         struct ath10k_ce_ring *dest_ring;
990         unsigned int nentries = attr->dest_nentries;
991         unsigned int ce_nbytes;
992         u32 ctrl_addr = ath10k_ce_base_address(ce_id);
993         dma_addr_t base_addr;
994         char *ptr;
995
996         nentries = roundup_pow_of_two(nentries);
997
998         if (ce_state->dest_ring) {
999                 WARN_ON(ce_state->dest_ring->nentries != nentries);
1000                 return 0;
1001         }
1002
1003         ce_nbytes = sizeof(struct ath10k_ce_ring) + (nentries * sizeof(void *));
1004         ptr = kzalloc(ce_nbytes, GFP_KERNEL);
1005         if (ptr == NULL)
1006                 return -ENOMEM;
1007
1008         ce_state->dest_ring = (struct ath10k_ce_ring *)ptr;
1009         dest_ring = ce_state->dest_ring;
1010
1011         ptr += sizeof(struct ath10k_ce_ring);
1012         dest_ring->nentries = nentries;
1013         dest_ring->nentries_mask = nentries - 1;
1014
1015         dest_ring->sw_index = ath10k_ce_dest_ring_read_index_get(ar, ctrl_addr);
1016         dest_ring->sw_index &= dest_ring->nentries_mask;
1017         dest_ring->write_index =
1018                 ath10k_ce_dest_ring_write_index_get(ar, ctrl_addr);
1019         dest_ring->write_index &= dest_ring->nentries_mask;
1020
1021         dest_ring->per_transfer_context = (void **)ptr;
1022
1023         /*
1024          * Legacy platforms that do not support cache
1025          * coherent DMA are unsupported
1026          */
1027         dest_ring->base_addr_owner_space_unaligned =
1028                 pci_alloc_consistent(ar_pci->pdev,
1029                                      (nentries * sizeof(struct ce_desc) +
1030                                       CE_DESC_RING_ALIGN),
1031                                      &base_addr);
1032         if (!dest_ring->base_addr_owner_space_unaligned) {
1033                 kfree(ce_state->dest_ring);
1034                 ce_state->dest_ring = NULL;
1035                 return -ENOMEM;
1036         }
1037
1038         dest_ring->base_addr_ce_space_unaligned = base_addr;
1039
1040         /*
1041          * Correctly initialize memory to 0 to prevent garbage
1042          * data crashing system when download firmware
1043          */
1044         memset(dest_ring->base_addr_owner_space_unaligned, 0,
1045                nentries * sizeof(struct ce_desc) + CE_DESC_RING_ALIGN);
1046
1047         dest_ring->base_addr_owner_space = PTR_ALIGN(
1048                         dest_ring->base_addr_owner_space_unaligned,
1049                         CE_DESC_RING_ALIGN);
1050         dest_ring->base_addr_ce_space = ALIGN(
1051                         dest_ring->base_addr_ce_space_unaligned,
1052                         CE_DESC_RING_ALIGN);
1053
1054         ath10k_ce_dest_ring_base_addr_set(ar, ctrl_addr,
1055                                           dest_ring->base_addr_ce_space);
1056         ath10k_ce_dest_ring_size_set(ar, ctrl_addr, nentries);
1057         ath10k_ce_dest_ring_byte_swap_set(ar, ctrl_addr, 0);
1058         ath10k_ce_dest_ring_lowmark_set(ar, ctrl_addr, 0);
1059         ath10k_ce_dest_ring_highmark_set(ar, ctrl_addr, nentries);
1060
1061         ath10k_dbg(ATH10K_DBG_BOOT,
1062                    "boot ce dest ring id %d entries %d base_addr %p\n",
1063                    ce_id, nentries, dest_ring->base_addr_owner_space);
1064
1065         return 0;
1066 }
1067
1068 static struct ath10k_ce_pipe *ath10k_ce_init_state(struct ath10k *ar,
1069                                              unsigned int ce_id,
1070                                              const struct ce_attr *attr)
1071 {
1072         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1073         struct ath10k_ce_pipe *ce_state = &ar_pci->ce_states[ce_id];
1074         u32 ctrl_addr = ath10k_ce_base_address(ce_id);
1075
1076         spin_lock_bh(&ar_pci->ce_lock);
1077
1078         ce_state->ar = ar;
1079         ce_state->id = ce_id;
1080         ce_state->ctrl_addr = ctrl_addr;
1081         ce_state->attr_flags = attr->flags;
1082         ce_state->src_sz_max = attr->src_sz_max;
1083
1084         spin_unlock_bh(&ar_pci->ce_lock);
1085
1086         return ce_state;
1087 }
1088
1089 /*
1090  * Initialize a Copy Engine based on caller-supplied attributes.
1091  * This may be called once to initialize both source and destination
1092  * rings or it may be called twice for separate source and destination
1093  * initialization. It may be that only one side or the other is
1094  * initialized by software/firmware.
1095  */
1096 struct ath10k_ce_pipe *ath10k_ce_init(struct ath10k *ar,
1097                                 unsigned int ce_id,
1098                                 const struct ce_attr *attr)
1099 {
1100         struct ath10k_ce_pipe *ce_state;
1101         u32 ctrl_addr = ath10k_ce_base_address(ce_id);
1102         int ret;
1103
1104         ret = ath10k_pci_wake(ar);
1105         if (ret)
1106                 return NULL;
1107
1108         ce_state = ath10k_ce_init_state(ar, ce_id, attr);
1109         if (!ce_state) {
1110                 ath10k_err("Failed to initialize CE state for ID: %d\n", ce_id);
1111                 return NULL;
1112         }
1113
1114         if (attr->src_nentries) {
1115                 ret = ath10k_ce_init_src_ring(ar, ce_id, ce_state, attr);
1116                 if (ret) {
1117                         ath10k_err("Failed to initialize CE src ring for ID: %d (%d)\n",
1118                                    ce_id, ret);
1119                         ath10k_ce_deinit(ce_state);
1120                         return NULL;
1121                 }
1122         }
1123
1124         if (attr->dest_nentries) {
1125                 ret = ath10k_ce_init_dest_ring(ar, ce_id, ce_state, attr);
1126                 if (ret) {
1127                         ath10k_err("Failed to initialize CE dest ring for ID: %d (%d)\n",
1128                                    ce_id, ret);
1129                         ath10k_ce_deinit(ce_state);
1130                         return NULL;
1131                 }
1132         }
1133
1134         /* Enable CE error interrupts */
1135         ath10k_ce_error_intr_enable(ar, ctrl_addr);
1136
1137         ath10k_pci_sleep(ar);
1138
1139         return ce_state;
1140 }
1141
1142 void ath10k_ce_deinit(struct ath10k_ce_pipe *ce_state)
1143 {
1144         struct ath10k *ar = ce_state->ar;
1145         struct ath10k_pci *ar_pci = ath10k_pci_priv(ar);
1146
1147         if (ce_state->src_ring) {
1148                 kfree(ce_state->src_ring->shadow_base_unaligned);
1149                 pci_free_consistent(ar_pci->pdev,
1150                                     (ce_state->src_ring->nentries *
1151                                      sizeof(struct ce_desc) +
1152                                      CE_DESC_RING_ALIGN),
1153                                     ce_state->src_ring->base_addr_owner_space,
1154                                     ce_state->src_ring->base_addr_ce_space);
1155                 kfree(ce_state->src_ring);
1156         }
1157
1158         if (ce_state->dest_ring) {
1159                 pci_free_consistent(ar_pci->pdev,
1160                                     (ce_state->dest_ring->nentries *
1161                                      sizeof(struct ce_desc) +
1162                                      CE_DESC_RING_ALIGN),
1163                                     ce_state->dest_ring->base_addr_owner_space,
1164                                     ce_state->dest_ring->base_addr_ce_space);
1165                 kfree(ce_state->dest_ring);
1166         }
1167
1168         ce_state->src_ring = NULL;
1169         ce_state->dest_ring = NULL;
1170 }