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