1 /**********************************************************************
2  * Author: Cavium, Inc.
3  *
4  * Contact: support@cavium.com
5  *          Please include "LiquidIO" in the subject.
6  *
7  * Copyright (c) 2003-2015 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  * This file may also be available under a different license from Cavium.
20  * Contact Cavium, Inc. for more information
21  **********************************************************************/
22 #include <linux/version.h>
23 #include <linux/types.h>
24 #include <linux/list.h>
25 #include <linux/interrupt.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/pci.h>
28 #include <linux/kthread.h>
29 #include <linux/netdevice.h>
30 #include "octeon_config.h"
31 #include "liquidio_common.h"
32 #include "octeon_droq.h"
33 #include "octeon_iq.h"
34 #include "response_manager.h"
35 #include "octeon_device.h"
36 #include "octeon_nic.h"
37 #include "octeon_main.h"
38 #include "octeon_network.h"
39 #include "cn66xx_regs.h"
40 #include "cn66xx_device.h"
41 #include "cn68xx_regs.h"
42 #include "cn68xx_device.h"
43 #include "liquidio_image.h"
44 
45 static void oct_poll_req_completion(struct work_struct *work);
46 
47 int octeon_setup_response_list(struct octeon_device *oct)
48 {
49 	int i, ret = 0;
50 	struct cavium_wq *cwq;
51 
52 	for (i = 0; i < MAX_RESPONSE_LISTS; i++) {
53 		INIT_LIST_HEAD(&oct->response_list[i].head);
54 		spin_lock_init(&oct->response_list[i].lock);
55 		atomic_set(&oct->response_list[i].pending_req_count, 0);
56 	}
57 
58 	oct->dma_comp_wq.wq = create_workqueue("dma-comp");
59 	if (!oct->dma_comp_wq.wq) {
60 		dev_err(&oct->pci_dev->dev, "failed to create wq thread\n");
61 		return -ENOMEM;
62 	}
63 
64 	cwq = &oct->dma_comp_wq;
65 	INIT_DELAYED_WORK(&cwq->wk.work, oct_poll_req_completion);
66 	cwq->wk.ctxptr = oct;
67 	queue_delayed_work(cwq->wq, &cwq->wk.work, msecs_to_jiffies(100));
68 
69 	return ret;
70 }
71 
72 void octeon_delete_response_list(struct octeon_device *oct)
73 {
74 	cancel_delayed_work_sync(&oct->dma_comp_wq.wk.work);
75 	flush_workqueue(oct->dma_comp_wq.wq);
76 	destroy_workqueue(oct->dma_comp_wq.wq);
77 }
78 
79 int lio_process_ordered_list(struct octeon_device *octeon_dev,
80 			     u32 force_quit)
81 {
82 	struct octeon_response_list *ordered_sc_list;
83 	struct octeon_soft_command *sc;
84 	int request_complete = 0;
85 	int resp_to_process = MAX_ORD_REQS_TO_PROCESS;
86 	u32 status;
87 	u64 status64;
88 	struct octeon_instr_rdp *rdp;
89 
90 	ordered_sc_list = &octeon_dev->response_list[OCTEON_ORDERED_SC_LIST];
91 
92 	do {
93 		spin_lock_bh(&ordered_sc_list->lock);
94 
95 		if (ordered_sc_list->head.next == &ordered_sc_list->head) {
96 			/* ordered_sc_list is empty; there is
97 			 * nothing to process
98 			 */
99 			spin_unlock_bh
100 			    (&ordered_sc_list->lock);
101 			return 1;
102 		}
103 
104 		sc = (struct octeon_soft_command *)ordered_sc_list->
105 		    head.next;
106 		rdp = (struct octeon_instr_rdp *)&sc->cmd.rdp;
107 
108 		status = OCTEON_REQUEST_PENDING;
109 
110 		/* check if octeon has finished DMA'ing a response
111 		 * to where rptr is pointing to
112 		 */
113 		dma_sync_single_for_cpu(&octeon_dev->pci_dev->dev,
114 					sc->cmd.rptr, rdp->rlen,
115 					DMA_FROM_DEVICE);
116 		status64 = *sc->status_word;
117 
118 		if (status64 != COMPLETION_WORD_INIT) {
119 			if ((status64 & 0xff) != 0xff) {
120 				octeon_swap_8B_data(&status64, 1);
121 				if (((status64 & 0xff) != 0xff)) {
122 					status = (u32)(status64 &
123 						       0xffffffffULL);
124 				}
125 			}
126 		} else if (force_quit || (sc->timeout &&
127 			time_after(jiffies, (unsigned long)sc->timeout))) {
128 			status = OCTEON_REQUEST_TIMEOUT;
129 		}
130 
131 		if (status != OCTEON_REQUEST_PENDING) {
132 			/* we have received a response or we have timed out */
133 			/* remove node from linked list */
134 			list_del(&sc->node);
135 			atomic_dec(&octeon_dev->response_list
136 					  [OCTEON_ORDERED_SC_LIST].
137 					  pending_req_count);
138 			spin_unlock_bh
139 			    (&ordered_sc_list->lock);
140 
141 			if (sc->callback)
142 				sc->callback(octeon_dev, status,
143 					     sc->callback_arg);
144 
145 			request_complete++;
146 
147 		} else {
148 			/* no response yet */
149 			request_complete = 0;
150 			spin_unlock_bh
151 			    (&ordered_sc_list->lock);
152 		}
153 
154 		/* If we hit the Max Ordered requests to process every loop,
155 		 * we quit
156 		 * and let this function be invoked the next time the poll
157 		 * thread runs
158 		 * to process the remaining requests. This function can take up
159 		 * the entire CPU if there is no upper limit to the requests
160 		 * processed.
161 		 */
162 		if (request_complete >= resp_to_process)
163 			break;
164 	} while (request_complete);
165 
166 	return 0;
167 }
168 
169 static void oct_poll_req_completion(struct work_struct *work)
170 {
171 	struct cavium_wk *wk = (struct cavium_wk *)work;
172 	struct octeon_device *oct = (struct octeon_device *)wk->ctxptr;
173 	struct cavium_wq *cwq = &oct->dma_comp_wq;
174 
175 	lio_process_ordered_list(oct, 0);
176 
177 	queue_delayed_work(cwq->wq, &cwq->wk.work, msecs_to_jiffies(100));
178 }
179