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 details.
17  ***********************************************************************/
18 #include <linux/pci.h>
19 #include <linux/netdevice.h>
20 #include "liquidio_common.h"
21 #include "octeon_droq.h"
22 #include "octeon_iq.h"
23 #include "response_manager.h"
24 #include "octeon_device.h"
25 #include "octeon_main.h"
26 #include "octeon_mailbox.h"
27 
28 /**
29  * octeon_mbox_read:
30  * @oct: Pointer mailbox
31  *
32  * Reads the 8-bytes of data from the mbox register
33  * Writes back the acknowldgement inidcating completion of read
34  */
35 int octeon_mbox_read(struct octeon_mbox *mbox)
36 {
37 	union octeon_mbox_message msg;
38 	int ret = 0;
39 
40 	spin_lock(&mbox->lock);
41 
42 	msg.u64 = readq(mbox->mbox_read_reg);
43 
44 	if ((msg.u64 == OCTEON_PFVFACK) || (msg.u64 == OCTEON_PFVFSIG)) {
45 		spin_unlock(&mbox->lock);
46 		return 0;
47 	}
48 
49 	if (mbox->state & OCTEON_MBOX_STATE_REQUEST_RECEIVING) {
50 		mbox->mbox_req.data[mbox->mbox_req.recv_len - 1] = msg.u64;
51 		mbox->mbox_req.recv_len++;
52 	} else {
53 		if (mbox->state & OCTEON_MBOX_STATE_RESPONSE_RECEIVING) {
54 			mbox->mbox_resp.data[mbox->mbox_resp.recv_len - 1] =
55 				msg.u64;
56 			mbox->mbox_resp.recv_len++;
57 		} else {
58 			if ((mbox->state & OCTEON_MBOX_STATE_IDLE) &&
59 			    (msg.s.type == OCTEON_MBOX_REQUEST)) {
60 				mbox->state &= ~OCTEON_MBOX_STATE_IDLE;
61 				mbox->state |=
62 				    OCTEON_MBOX_STATE_REQUEST_RECEIVING;
63 				mbox->mbox_req.msg.u64 = msg.u64;
64 				mbox->mbox_req.q_no = mbox->q_no;
65 				mbox->mbox_req.recv_len = 1;
66 			} else {
67 				if ((mbox->state &
68 				     OCTEON_MBOX_STATE_RESPONSE_PENDING) &&
69 				    (msg.s.type == OCTEON_MBOX_RESPONSE)) {
70 					mbox->state &=
71 					    ~OCTEON_MBOX_STATE_RESPONSE_PENDING;
72 					mbox->state |=
73 					    OCTEON_MBOX_STATE_RESPONSE_RECEIVING
74 					    ;
75 					mbox->mbox_resp.msg.u64 = msg.u64;
76 					mbox->mbox_resp.q_no = mbox->q_no;
77 					mbox->mbox_resp.recv_len = 1;
78 				} else {
79 					writeq(OCTEON_PFVFERR,
80 					       mbox->mbox_read_reg);
81 					mbox->state |= OCTEON_MBOX_STATE_ERROR;
82 					spin_unlock(&mbox->lock);
83 					return 1;
84 				}
85 			}
86 		}
87 	}
88 
89 	if (mbox->state & OCTEON_MBOX_STATE_REQUEST_RECEIVING) {
90 		if (mbox->mbox_req.recv_len < msg.s.len) {
91 			ret = 0;
92 		} else {
93 			mbox->state &= ~OCTEON_MBOX_STATE_REQUEST_RECEIVING;
94 			mbox->state |= OCTEON_MBOX_STATE_REQUEST_RECEIVED;
95 			ret = 1;
96 		}
97 	} else {
98 		if (mbox->state & OCTEON_MBOX_STATE_RESPONSE_RECEIVING) {
99 			if (mbox->mbox_resp.recv_len < msg.s.len) {
100 				ret = 0;
101 			} else {
102 				mbox->state &=
103 				    ~OCTEON_MBOX_STATE_RESPONSE_RECEIVING;
104 				mbox->state |=
105 				    OCTEON_MBOX_STATE_RESPONSE_RECEIVED;
106 				ret = 1;
107 			}
108 		} else {
109 			WARN_ON(1);
110 		}
111 	}
112 
113 	writeq(OCTEON_PFVFACK, mbox->mbox_read_reg);
114 
115 	spin_unlock(&mbox->lock);
116 
117 	return ret;
118 }
119 
120 /**
121  * octeon_mbox_write:
122  * @oct: Pointer Octeon Device
123  * @mbox_cmd: Cmd to send to mailbox.
124  *
125  * Populates the queue specific mbox structure
126  * with cmd information.
127  * Write the cmd to mbox register
128  */
129 int octeon_mbox_write(struct octeon_device *oct,
130 		      struct octeon_mbox_cmd *mbox_cmd)
131 {
132 	struct octeon_mbox *mbox = oct->mbox[mbox_cmd->q_no];
133 	u32 count, i, ret = OCTEON_MBOX_STATUS_SUCCESS;
134 	long timeout = LIO_MBOX_WRITE_WAIT_TIME;
135 	unsigned long flags;
136 
137 	spin_lock_irqsave(&mbox->lock, flags);
138 
139 	if ((mbox_cmd->msg.s.type == OCTEON_MBOX_RESPONSE) &&
140 	    !(mbox->state & OCTEON_MBOX_STATE_REQUEST_RECEIVED)) {
141 		spin_unlock_irqrestore(&mbox->lock, flags);
142 		return OCTEON_MBOX_STATUS_FAILED;
143 	}
144 
145 	if ((mbox_cmd->msg.s.type == OCTEON_MBOX_REQUEST) &&
146 	    !(mbox->state & OCTEON_MBOX_STATE_IDLE)) {
147 		spin_unlock_irqrestore(&mbox->lock, flags);
148 		return OCTEON_MBOX_STATUS_BUSY;
149 	}
150 
151 	if (mbox_cmd->msg.s.type == OCTEON_MBOX_REQUEST) {
152 		memcpy(&mbox->mbox_resp, mbox_cmd,
153 		       sizeof(struct octeon_mbox_cmd));
154 		mbox->state = OCTEON_MBOX_STATE_RESPONSE_PENDING;
155 	}
156 
157 	spin_unlock_irqrestore(&mbox->lock, flags);
158 
159 	count = 0;
160 
161 	while (readq(mbox->mbox_write_reg) != OCTEON_PFVFSIG) {
162 		schedule_timeout_uninterruptible(timeout);
163 		if (count++ == LIO_MBOX_WRITE_WAIT_CNT) {
164 			ret = OCTEON_MBOX_STATUS_FAILED;
165 			break;
166 		}
167 	}
168 
169 	if (ret == OCTEON_MBOX_STATUS_SUCCESS) {
170 		writeq(mbox_cmd->msg.u64, mbox->mbox_write_reg);
171 		for (i = 0; i < (u32)(mbox_cmd->msg.s.len - 1); i++) {
172 			count = 0;
173 			while (readq(mbox->mbox_write_reg) !=
174 			       OCTEON_PFVFACK) {
175 				schedule_timeout_uninterruptible(timeout);
176 				if (count++ == LIO_MBOX_WRITE_WAIT_CNT) {
177 					ret = OCTEON_MBOX_STATUS_FAILED;
178 					break;
179 				}
180 			}
181 			writeq(mbox_cmd->data[i], mbox->mbox_write_reg);
182 		}
183 	}
184 
185 	spin_lock_irqsave(&mbox->lock, flags);
186 	if (mbox_cmd->msg.s.type == OCTEON_MBOX_RESPONSE) {
187 		mbox->state = OCTEON_MBOX_STATE_IDLE;
188 		writeq(OCTEON_PFVFSIG, mbox->mbox_read_reg);
189 	} else {
190 		if ((!mbox_cmd->msg.s.resp_needed) ||
191 		    (ret == OCTEON_MBOX_STATUS_FAILED)) {
192 			mbox->state &= ~OCTEON_MBOX_STATE_RESPONSE_PENDING;
193 			if (!(mbox->state &
194 			      (OCTEON_MBOX_STATE_REQUEST_RECEIVING |
195 			       OCTEON_MBOX_STATE_REQUEST_RECEIVED)))
196 				mbox->state = OCTEON_MBOX_STATE_IDLE;
197 		}
198 	}
199 	spin_unlock_irqrestore(&mbox->lock, flags);
200 
201 	return ret;
202 }
203 
204 /**
205  * octeon_mbox_process_cmd:
206  * @mbox: Pointer mailbox
207  * @mbox_cmd: Pointer to command received
208  *
209  * Process the cmd received in mbox
210  */
211 static int octeon_mbox_process_cmd(struct octeon_mbox *mbox,
212 				   struct octeon_mbox_cmd *mbox_cmd)
213 {
214 	struct octeon_device *oct = mbox->oct_dev;
215 
216 	switch (mbox_cmd->msg.s.cmd) {
217 	case OCTEON_VF_ACTIVE:
218 		dev_dbg(&oct->pci_dev->dev, "got vfactive sending data back\n");
219 		mbox_cmd->msg.s.type = OCTEON_MBOX_RESPONSE;
220 		mbox_cmd->msg.s.resp_needed = 1;
221 		mbox_cmd->msg.s.len = 2;
222 		mbox_cmd->data[0] = 0; /* VF version is in mbox_cmd->data[0] */
223 		((struct lio_version *)&mbox_cmd->data[0])->major =
224 			LIQUIDIO_BASE_MAJOR_VERSION;
225 		((struct lio_version *)&mbox_cmd->data[0])->minor =
226 			LIQUIDIO_BASE_MINOR_VERSION;
227 		((struct lio_version *)&mbox_cmd->data[0])->micro =
228 			LIQUIDIO_BASE_MICRO_VERSION;
229 		memcpy(mbox_cmd->msg.s.params, (uint8_t *)&oct->pfvf_hsword, 6);
230 		/* Sending core cofig info to the corresponding active VF.*/
231 		octeon_mbox_write(oct, mbox_cmd);
232 		break;
233 
234 	case OCTEON_VF_FLR_REQUEST:
235 		dev_info(&oct->pci_dev->dev,
236 			 "got a request for FLR from VF that owns DPI ring %u\n",
237 			 mbox->q_no);
238 		pcie_capability_set_word(
239 			oct->sriov_info.dpiring_to_vfpcidev_lut[mbox->q_no],
240 			PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_BCR_FLR);
241 		break;
242 
243 	case OCTEON_PF_CHANGED_VF_MACADDR:
244 		if (OCTEON_CN23XX_VF(oct))
245 			octeon_pf_changed_vf_macaddr(oct,
246 						     mbox_cmd->msg.s.params);
247 		break;
248 
249 	default:
250 		break;
251 	}
252 	return 0;
253 }
254 
255 /**
256  *octeon_mbox_process_message:
257  *
258  * Process the received mbox message.
259  */
260 int octeon_mbox_process_message(struct octeon_mbox *mbox)
261 {
262 	struct octeon_mbox_cmd mbox_cmd;
263 	unsigned long flags;
264 
265 	spin_lock_irqsave(&mbox->lock, flags);
266 
267 	if (mbox->state & OCTEON_MBOX_STATE_ERROR) {
268 		if (mbox->state & (OCTEON_MBOX_STATE_RESPONSE_PENDING |
269 				   OCTEON_MBOX_STATE_RESPONSE_RECEIVING)) {
270 			memcpy(&mbox_cmd, &mbox->mbox_resp,
271 			       sizeof(struct octeon_mbox_cmd));
272 			mbox->state = OCTEON_MBOX_STATE_IDLE;
273 			writeq(OCTEON_PFVFSIG, mbox->mbox_read_reg);
274 			spin_unlock_irqrestore(&mbox->lock, flags);
275 			mbox_cmd.recv_status = 1;
276 			if (mbox_cmd.fn)
277 				mbox_cmd.fn(mbox->oct_dev, &mbox_cmd,
278 					    mbox_cmd.fn_arg);
279 			return 0;
280 		}
281 
282 		mbox->state = OCTEON_MBOX_STATE_IDLE;
283 		writeq(OCTEON_PFVFSIG, mbox->mbox_read_reg);
284 		spin_unlock_irqrestore(&mbox->lock, flags);
285 		return 0;
286 	}
287 
288 	if (mbox->state & OCTEON_MBOX_STATE_RESPONSE_RECEIVED) {
289 		memcpy(&mbox_cmd, &mbox->mbox_resp,
290 		       sizeof(struct octeon_mbox_cmd));
291 		mbox->state = OCTEON_MBOX_STATE_IDLE;
292 		writeq(OCTEON_PFVFSIG, mbox->mbox_read_reg);
293 		spin_unlock_irqrestore(&mbox->lock, flags);
294 		mbox_cmd.recv_status = 0;
295 		if (mbox_cmd.fn)
296 			mbox_cmd.fn(mbox->oct_dev, &mbox_cmd, mbox_cmd.fn_arg);
297 		return 0;
298 	}
299 
300 	if (mbox->state & OCTEON_MBOX_STATE_REQUEST_RECEIVED) {
301 		memcpy(&mbox_cmd, &mbox->mbox_req,
302 		       sizeof(struct octeon_mbox_cmd));
303 		if (!mbox_cmd.msg.s.resp_needed) {
304 			mbox->state &= ~OCTEON_MBOX_STATE_REQUEST_RECEIVED;
305 			if (!(mbox->state &
306 			      OCTEON_MBOX_STATE_RESPONSE_PENDING))
307 				mbox->state = OCTEON_MBOX_STATE_IDLE;
308 			writeq(OCTEON_PFVFSIG, mbox->mbox_read_reg);
309 		}
310 
311 		spin_unlock_irqrestore(&mbox->lock, flags);
312 		octeon_mbox_process_cmd(mbox, &mbox_cmd);
313 		return 0;
314 	}
315 
316 	spin_unlock_irqrestore(&mbox->lock, flags);
317 	WARN_ON(1);
318 
319 	return 0;
320 }
321