liquidio CN23XX: copyrights changes and alignment
[linux.git] / drivers / net / ethernet / cavium / liquidio / response_manager.c
1 /**********************************************************************
2  * Author: Cavium, Inc.
3  *
4  * Contact: support@cavium.com
5  *          Please include "LiquidIO" in the subject.
6  *
7  * Copyright (c) 2003-2016 Cavium, Inc.
8  *
9  * This file is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License, Version 2, as
11  * published by the Free Software Foundation.
12  *
13  * This file is distributed in the hope that it will be useful, but
14  * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16  * NONINFRINGEMENT.  See the GNU General Public License for more
17  * details.
18  **********************************************************************/
19 #include <linux/pci.h>
20 #include <linux/netdevice.h>
21 #include "liquidio_common.h"
22 #include "octeon_droq.h"
23 #include "octeon_iq.h"
24 #include "response_manager.h"
25 #include "octeon_device.h"
26 #include "octeon_main.h"
27
28 static void oct_poll_req_completion(struct work_struct *work);
29
30 int octeon_setup_response_list(struct octeon_device *oct)
31 {
32         int i, ret = 0;
33         struct cavium_wq *cwq;
34
35         for (i = 0; i < MAX_RESPONSE_LISTS; i++) {
36                 INIT_LIST_HEAD(&oct->response_list[i].head);
37                 spin_lock_init(&oct->response_list[i].lock);
38                 atomic_set(&oct->response_list[i].pending_req_count, 0);
39         }
40         spin_lock_init(&oct->cmd_resp_wqlock);
41
42         oct->dma_comp_wq.wq = alloc_workqueue("dma-comp", WQ_MEM_RECLAIM, 0);
43         if (!oct->dma_comp_wq.wq) {
44                 dev_err(&oct->pci_dev->dev, "failed to create wq thread\n");
45                 return -ENOMEM;
46         }
47
48         cwq = &oct->dma_comp_wq;
49         INIT_DELAYED_WORK(&cwq->wk.work, oct_poll_req_completion);
50         cwq->wk.ctxptr = oct;
51         oct->cmd_resp_state = OCT_DRV_ONLINE;
52         queue_delayed_work(cwq->wq, &cwq->wk.work, msecs_to_jiffies(50));
53
54         return ret;
55 }
56
57 void octeon_delete_response_list(struct octeon_device *oct)
58 {
59         cancel_delayed_work_sync(&oct->dma_comp_wq.wk.work);
60         destroy_workqueue(oct->dma_comp_wq.wq);
61 }
62
63 int lio_process_ordered_list(struct octeon_device *octeon_dev,
64                              u32 force_quit)
65 {
66         struct octeon_response_list *ordered_sc_list;
67         struct octeon_soft_command *sc;
68         int request_complete = 0;
69         int resp_to_process = MAX_ORD_REQS_TO_PROCESS;
70         u32 status;
71         u64 status64;
72         struct octeon_instr_rdp *rdp;
73         u64 rptr;
74
75         ordered_sc_list = &octeon_dev->response_list[OCTEON_ORDERED_SC_LIST];
76
77         do {
78                 spin_lock_bh(&ordered_sc_list->lock);
79
80                 if (ordered_sc_list->head.next == &ordered_sc_list->head) {
81                         spin_unlock_bh(&ordered_sc_list->lock);
82                         return 1;
83                 }
84
85                 sc = (struct octeon_soft_command *)ordered_sc_list->
86                     head.next;
87                 if (OCTEON_CN23XX_PF(octeon_dev)) {
88                         rdp = (struct octeon_instr_rdp *)&sc->cmd.cmd3.rdp;
89                         rptr = sc->cmd.cmd3.rptr;
90                 } else {
91                         rdp = (struct octeon_instr_rdp *)&sc->cmd.cmd2.rdp;
92                         rptr = sc->cmd.cmd2.rptr;
93                 }
94
95                 status = OCTEON_REQUEST_PENDING;
96
97                 /* check if octeon has finished DMA'ing a response
98                  * to where rptr is pointing to
99                  */
100                 dma_sync_single_for_cpu(&octeon_dev->pci_dev->dev,
101                                         rptr, rdp->rlen,
102                                         DMA_FROM_DEVICE);
103                 status64 = *sc->status_word;
104
105                 if (status64 != COMPLETION_WORD_INIT) {
106                         if ((status64 & 0xff) != 0xff) {
107                                 octeon_swap_8B_data(&status64, 1);
108                                 if (((status64 & 0xff) != 0xff)) {
109                                         status = (u32)(status64 &
110                                                        0xffffffffULL);
111                                 }
112                         }
113                 } else if (force_quit || (sc->timeout &&
114                         time_after(jiffies, (unsigned long)sc->timeout))) {
115                         status = OCTEON_REQUEST_TIMEOUT;
116                 }
117
118                 if (status != OCTEON_REQUEST_PENDING) {
119                         /* we have received a response or we have timed out */
120                         /* remove node from linked list */
121                         list_del(&sc->node);
122                         atomic_dec(&octeon_dev->response_list
123                                           [OCTEON_ORDERED_SC_LIST].
124                                           pending_req_count);
125                         spin_unlock_bh
126                             (&ordered_sc_list->lock);
127
128                         if (sc->callback)
129                                 sc->callback(octeon_dev, status,
130                                              sc->callback_arg);
131
132                         request_complete++;
133
134                 } else {
135                         /* no response yet */
136                         request_complete = 0;
137                         spin_unlock_bh
138                             (&ordered_sc_list->lock);
139                 }
140
141                 /* If we hit the Max Ordered requests to process every loop,
142                  * we quit
143                  * and let this function be invoked the next time the poll
144                  * thread runs
145                  * to process the remaining requests. This function can take up
146                  * the entire CPU if there is no upper limit to the requests
147                  * processed.
148                  */
149                 if (request_complete >= resp_to_process)
150                         break;
151         } while (request_complete);
152
153         return 0;
154 }
155
156 static void oct_poll_req_completion(struct work_struct *work)
157 {
158         struct cavium_wk *wk = (struct cavium_wk *)work;
159         struct octeon_device *oct = (struct octeon_device *)wk->ctxptr;
160         struct cavium_wq *cwq = &oct->dma_comp_wq;
161
162         lio_process_ordered_list(oct, 0);
163         queue_delayed_work(cwq->wq, &cwq->wk.work, msecs_to_jiffies(50));
164 }