1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell OcteonTx2 RVU Physcial Function ethernet driver
3  *
4  * Copyright (C) 2020 Marvell International Ltd.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/pci.h>
14 #include <linux/etherdevice.h>
15 #include <linux/of.h>
16 #include <linux/if_vlan.h>
17 #include <linux/iommu.h>
18 #include <net/ip.h>
19 
20 #include "otx2_reg.h"
21 #include "otx2_common.h"
22 #include "otx2_txrx.h"
23 #include "otx2_struct.h"
24 #include "otx2_ptp.h"
25 #include <rvu_trace.h>
26 
27 #define DRV_NAME	"octeontx2-nicpf"
28 #define DRV_STRING	"Marvell OcteonTX2 NIC Physical Function Driver"
29 
30 /* Supported devices */
31 static const struct pci_device_id otx2_pf_id_table[] = {
32 	{ PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_RVU_PF) },
33 	{ 0, }  /* end of table */
34 };
35 
36 MODULE_AUTHOR("Sunil Goutham <sgoutham@marvell.com>");
37 MODULE_DESCRIPTION(DRV_STRING);
38 MODULE_LICENSE("GPL v2");
39 MODULE_DEVICE_TABLE(pci, otx2_pf_id_table);
40 
41 enum {
42 	TYPE_PFAF,
43 	TYPE_PFVF,
44 };
45 
46 static int otx2_config_hw_tx_tstamp(struct otx2_nic *pfvf, bool enable);
47 static int otx2_config_hw_rx_tstamp(struct otx2_nic *pfvf, bool enable);
48 
otx2_change_mtu(struct net_device *netdev, int new_mtu)49 static int otx2_change_mtu(struct net_device *netdev, int new_mtu)
50 {
51 	bool if_up = netif_running(netdev);
52 	int err = 0;
53 
54 	if (if_up)
55 		otx2_stop(netdev);
56 
57 	netdev_info(netdev, "Changing MTU from %d to %d\n",
58 		    netdev->mtu, new_mtu);
59 	netdev->mtu = new_mtu;
60 
61 	if (if_up)
62 		err = otx2_open(netdev);
63 
64 	return err;
65 }
66 
otx2_disable_flr_me_intr(struct otx2_nic *pf)67 static void otx2_disable_flr_me_intr(struct otx2_nic *pf)
68 {
69 	int irq, vfs = pf->total_vfs;
70 
71 	/* Disable VFs ME interrupts */
72 	otx2_write64(pf, RVU_PF_VFME_INT_ENA_W1CX(0), INTR_MASK(vfs));
73 	irq = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFME0);
74 	free_irq(irq, pf);
75 
76 	/* Disable VFs FLR interrupts */
77 	otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1CX(0), INTR_MASK(vfs));
78 	irq = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFFLR0);
79 	free_irq(irq, pf);
80 
81 	if (vfs <= 64)
82 		return;
83 
84 	otx2_write64(pf, RVU_PF_VFME_INT_ENA_W1CX(1), INTR_MASK(vfs - 64));
85 	irq = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFME1);
86 	free_irq(irq, pf);
87 
88 	otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1CX(1), INTR_MASK(vfs - 64));
89 	irq = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFFLR1);
90 	free_irq(irq, pf);
91 }
92 
otx2_flr_wq_destroy(struct otx2_nic *pf)93 static void otx2_flr_wq_destroy(struct otx2_nic *pf)
94 {
95 	if (!pf->flr_wq)
96 		return;
97 	destroy_workqueue(pf->flr_wq);
98 	pf->flr_wq = NULL;
99 	devm_kfree(pf->dev, pf->flr_wrk);
100 }
101 
otx2_flr_handler(struct work_struct *work)102 static void otx2_flr_handler(struct work_struct *work)
103 {
104 	struct flr_work *flrwork = container_of(work, struct flr_work, work);
105 	struct otx2_nic *pf = flrwork->pf;
106 	struct mbox *mbox = &pf->mbox;
107 	struct msg_req *req;
108 	int vf, reg = 0;
109 
110 	vf = flrwork - pf->flr_wrk;
111 
112 	mutex_lock(&mbox->lock);
113 	req = otx2_mbox_alloc_msg_vf_flr(mbox);
114 	if (!req) {
115 		mutex_unlock(&mbox->lock);
116 		return;
117 	}
118 	req->hdr.pcifunc &= RVU_PFVF_FUNC_MASK;
119 	req->hdr.pcifunc |= (vf + 1) & RVU_PFVF_FUNC_MASK;
120 
121 	if (!otx2_sync_mbox_msg(&pf->mbox)) {
122 		if (vf >= 64) {
123 			reg = 1;
124 			vf = vf - 64;
125 		}
126 		/* clear transcation pending bit */
127 		otx2_write64(pf, RVU_PF_VFTRPENDX(reg), BIT_ULL(vf));
128 		otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1SX(reg), BIT_ULL(vf));
129 	}
130 
131 	mutex_unlock(&mbox->lock);
132 }
133 
otx2_pf_flr_intr_handler(int irq, void *pf_irq)134 static irqreturn_t otx2_pf_flr_intr_handler(int irq, void *pf_irq)
135 {
136 	struct otx2_nic *pf = (struct otx2_nic *)pf_irq;
137 	int reg, dev, vf, start_vf, num_reg = 1;
138 	u64 intr;
139 
140 	if (pf->total_vfs > 64)
141 		num_reg = 2;
142 
143 	for (reg = 0; reg < num_reg; reg++) {
144 		intr = otx2_read64(pf, RVU_PF_VFFLR_INTX(reg));
145 		if (!intr)
146 			continue;
147 		start_vf = 64 * reg;
148 		for (vf = 0; vf < 64; vf++) {
149 			if (!(intr & BIT_ULL(vf)))
150 				continue;
151 			dev = vf + start_vf;
152 			queue_work(pf->flr_wq, &pf->flr_wrk[dev].work);
153 			/* Clear interrupt */
154 			otx2_write64(pf, RVU_PF_VFFLR_INTX(reg), BIT_ULL(vf));
155 			/* Disable the interrupt */
156 			otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1CX(reg),
157 				     BIT_ULL(vf));
158 		}
159 	}
160 	return IRQ_HANDLED;
161 }
162 
otx2_pf_me_intr_handler(int irq, void *pf_irq)163 static irqreturn_t otx2_pf_me_intr_handler(int irq, void *pf_irq)
164 {
165 	struct otx2_nic *pf = (struct otx2_nic *)pf_irq;
166 	int vf, reg, num_reg = 1;
167 	u64 intr;
168 
169 	if (pf->total_vfs > 64)
170 		num_reg = 2;
171 
172 	for (reg = 0; reg < num_reg; reg++) {
173 		intr = otx2_read64(pf, RVU_PF_VFME_INTX(reg));
174 		if (!intr)
175 			continue;
176 		for (vf = 0; vf < 64; vf++) {
177 			if (!(intr & BIT_ULL(vf)))
178 				continue;
179 			/* clear trpend bit */
180 			otx2_write64(pf, RVU_PF_VFTRPENDX(reg), BIT_ULL(vf));
181 			/* clear interrupt */
182 			otx2_write64(pf, RVU_PF_VFME_INTX(reg), BIT_ULL(vf));
183 		}
184 	}
185 	return IRQ_HANDLED;
186 }
187 
otx2_register_flr_me_intr(struct otx2_nic *pf, int numvfs)188 static int otx2_register_flr_me_intr(struct otx2_nic *pf, int numvfs)
189 {
190 	struct otx2_hw *hw = &pf->hw;
191 	char *irq_name;
192 	int ret;
193 
194 	/* Register ME interrupt handler*/
195 	irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFME0 * NAME_SIZE];
196 	snprintf(irq_name, NAME_SIZE, "RVUPF%d_ME0", rvu_get_pf(pf->pcifunc));
197 	ret = request_irq(pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFME0),
198 			  otx2_pf_me_intr_handler, 0, irq_name, pf);
199 	if (ret) {
200 		dev_err(pf->dev,
201 			"RVUPF: IRQ registration failed for ME0\n");
202 	}
203 
204 	/* Register FLR interrupt handler */
205 	irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFFLR0 * NAME_SIZE];
206 	snprintf(irq_name, NAME_SIZE, "RVUPF%d_FLR0", rvu_get_pf(pf->pcifunc));
207 	ret = request_irq(pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFFLR0),
208 			  otx2_pf_flr_intr_handler, 0, irq_name, pf);
209 	if (ret) {
210 		dev_err(pf->dev,
211 			"RVUPF: IRQ registration failed for FLR0\n");
212 		return ret;
213 	}
214 
215 	if (numvfs > 64) {
216 		irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFME1 * NAME_SIZE];
217 		snprintf(irq_name, NAME_SIZE, "RVUPF%d_ME1",
218 			 rvu_get_pf(pf->pcifunc));
219 		ret = request_irq(pci_irq_vector
220 				  (pf->pdev, RVU_PF_INT_VEC_VFME1),
221 				  otx2_pf_me_intr_handler, 0, irq_name, pf);
222 		if (ret) {
223 			dev_err(pf->dev,
224 				"RVUPF: IRQ registration failed for ME1\n");
225 		}
226 		irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFFLR1 * NAME_SIZE];
227 		snprintf(irq_name, NAME_SIZE, "RVUPF%d_FLR1",
228 			 rvu_get_pf(pf->pcifunc));
229 		ret = request_irq(pci_irq_vector
230 				  (pf->pdev, RVU_PF_INT_VEC_VFFLR1),
231 				  otx2_pf_flr_intr_handler, 0, irq_name, pf);
232 		if (ret) {
233 			dev_err(pf->dev,
234 				"RVUPF: IRQ registration failed for FLR1\n");
235 			return ret;
236 		}
237 	}
238 
239 	/* Enable ME interrupt for all VFs*/
240 	otx2_write64(pf, RVU_PF_VFME_INTX(0), INTR_MASK(numvfs));
241 	otx2_write64(pf, RVU_PF_VFME_INT_ENA_W1SX(0), INTR_MASK(numvfs));
242 
243 	/* Enable FLR interrupt for all VFs*/
244 	otx2_write64(pf, RVU_PF_VFFLR_INTX(0), INTR_MASK(numvfs));
245 	otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1SX(0), INTR_MASK(numvfs));
246 
247 	if (numvfs > 64) {
248 		numvfs -= 64;
249 
250 		otx2_write64(pf, RVU_PF_VFME_INTX(1), INTR_MASK(numvfs));
251 		otx2_write64(pf, RVU_PF_VFME_INT_ENA_W1SX(1),
252 			     INTR_MASK(numvfs));
253 
254 		otx2_write64(pf, RVU_PF_VFFLR_INTX(1), INTR_MASK(numvfs));
255 		otx2_write64(pf, RVU_PF_VFFLR_INT_ENA_W1SX(1),
256 			     INTR_MASK(numvfs));
257 	}
258 	return 0;
259 }
260 
otx2_pf_flr_init(struct otx2_nic *pf, int num_vfs)261 static int otx2_pf_flr_init(struct otx2_nic *pf, int num_vfs)
262 {
263 	int vf;
264 
265 	pf->flr_wq = alloc_workqueue("otx2_pf_flr_wq",
266 				     WQ_UNBOUND | WQ_HIGHPRI, 1);
267 	if (!pf->flr_wq)
268 		return -ENOMEM;
269 
270 	pf->flr_wrk = devm_kcalloc(pf->dev, num_vfs,
271 				   sizeof(struct flr_work), GFP_KERNEL);
272 	if (!pf->flr_wrk) {
273 		destroy_workqueue(pf->flr_wq);
274 		return -ENOMEM;
275 	}
276 
277 	for (vf = 0; vf < num_vfs; vf++) {
278 		pf->flr_wrk[vf].pf = pf;
279 		INIT_WORK(&pf->flr_wrk[vf].work, otx2_flr_handler);
280 	}
281 
282 	return 0;
283 }
284 
otx2_queue_work(struct mbox *mw, struct workqueue_struct *mbox_wq, int first, int mdevs, u64 intr, int type)285 static void otx2_queue_work(struct mbox *mw, struct workqueue_struct *mbox_wq,
286 			    int first, int mdevs, u64 intr, int type)
287 {
288 	struct otx2_mbox_dev *mdev;
289 	struct otx2_mbox *mbox;
290 	struct mbox_hdr *hdr;
291 	int i;
292 
293 	for (i = first; i < mdevs; i++) {
294 		/* start from 0 */
295 		if (!(intr & BIT_ULL(i - first)))
296 			continue;
297 
298 		mbox = &mw->mbox;
299 		mdev = &mbox->dev[i];
300 		if (type == TYPE_PFAF)
301 			otx2_sync_mbox_bbuf(mbox, i);
302 		hdr = mdev->mbase + mbox->rx_start;
303 		/* The hdr->num_msgs is set to zero immediately in the interrupt
304 		 * handler to  ensure that it holds a correct value next time
305 		 * when the interrupt handler is called.
306 		 * pf->mbox.num_msgs holds the data for use in pfaf_mbox_handler
307 		 * pf>mbox.up_num_msgs holds the data for use in
308 		 * pfaf_mbox_up_handler.
309 		 */
310 		if (hdr->num_msgs) {
311 			mw[i].num_msgs = hdr->num_msgs;
312 			hdr->num_msgs = 0;
313 			if (type == TYPE_PFAF)
314 				memset(mbox->hwbase + mbox->rx_start, 0,
315 				       ALIGN(sizeof(struct mbox_hdr),
316 					     sizeof(u64)));
317 
318 			queue_work(mbox_wq, &mw[i].mbox_wrk);
319 		}
320 
321 		mbox = &mw->mbox_up;
322 		mdev = &mbox->dev[i];
323 		if (type == TYPE_PFAF)
324 			otx2_sync_mbox_bbuf(mbox, i);
325 		hdr = mdev->mbase + mbox->rx_start;
326 		if (hdr->num_msgs) {
327 			mw[i].up_num_msgs = hdr->num_msgs;
328 			hdr->num_msgs = 0;
329 			if (type == TYPE_PFAF)
330 				memset(mbox->hwbase + mbox->rx_start, 0,
331 				       ALIGN(sizeof(struct mbox_hdr),
332 					     sizeof(u64)));
333 
334 			queue_work(mbox_wq, &mw[i].mbox_up_wrk);
335 		}
336 	}
337 }
338 
otx2_forward_msg_pfvf(struct otx2_mbox_dev *mdev, struct otx2_mbox *pfvf_mbox, void *bbuf_base, int devid)339 static void otx2_forward_msg_pfvf(struct otx2_mbox_dev *mdev,
340 				  struct otx2_mbox *pfvf_mbox, void *bbuf_base,
341 				  int devid)
342 {
343 	struct otx2_mbox_dev *src_mdev = mdev;
344 	int offset;
345 
346 	/* Msgs are already copied, trigger VF's mbox irq */
347 	smp_wmb();
348 
349 	offset = pfvf_mbox->trigger | (devid << pfvf_mbox->tr_shift);
350 	writeq(1, (void __iomem *)pfvf_mbox->reg_base + offset);
351 
352 	/* Restore VF's mbox bounce buffer region address */
353 	src_mdev->mbase = bbuf_base;
354 }
355 
otx2_forward_vf_mbox_msgs(struct otx2_nic *pf, struct otx2_mbox *src_mbox, int dir, int vf, int num_msgs)356 static int otx2_forward_vf_mbox_msgs(struct otx2_nic *pf,
357 				     struct otx2_mbox *src_mbox,
358 				     int dir, int vf, int num_msgs)
359 {
360 	struct otx2_mbox_dev *src_mdev, *dst_mdev;
361 	struct mbox_hdr *mbox_hdr;
362 	struct mbox_hdr *req_hdr;
363 	struct mbox *dst_mbox;
364 	int dst_size, err;
365 
366 	if (dir == MBOX_DIR_PFAF) {
367 		/* Set VF's mailbox memory as PF's bounce buffer memory, so
368 		 * that explicit copying of VF's msgs to PF=>AF mbox region
369 		 * and AF=>PF responses to VF's mbox region can be avoided.
370 		 */
371 		src_mdev = &src_mbox->dev[vf];
372 		mbox_hdr = src_mbox->hwbase +
373 				src_mbox->rx_start + (vf * MBOX_SIZE);
374 
375 		dst_mbox = &pf->mbox;
376 		dst_size = dst_mbox->mbox.tx_size -
377 				ALIGN(sizeof(*mbox_hdr), MBOX_MSG_ALIGN);
378 		/* Check if msgs fit into destination area and has valid size */
379 		if (mbox_hdr->msg_size > dst_size || !mbox_hdr->msg_size)
380 			return -EINVAL;
381 
382 		dst_mdev = &dst_mbox->mbox.dev[0];
383 
384 		mutex_lock(&pf->mbox.lock);
385 		dst_mdev->mbase = src_mdev->mbase;
386 		dst_mdev->msg_size = mbox_hdr->msg_size;
387 		dst_mdev->num_msgs = num_msgs;
388 		err = otx2_sync_mbox_msg(dst_mbox);
389 		/* Error code -EIO indicate there is a communication failure
390 		 * to the AF. Rest of the error codes indicate that AF processed
391 		 * VF messages and set the error codes in response messages
392 		 * (if any) so simply forward responses to VF.
393 		 */
394 		if (err == -EIO) {
395 			dev_warn(pf->dev,
396 				 "AF not responding to VF%d messages\n", vf);
397 			/* restore PF mbase and exit */
398 			dst_mdev->mbase = pf->mbox.bbuf_base;
399 			mutex_unlock(&pf->mbox.lock);
400 			return err;
401 		}
402 		/* At this point, all the VF messages sent to AF are acked
403 		 * with proper responses and responses are copied to VF
404 		 * mailbox hence raise interrupt to VF.
405 		 */
406 		req_hdr = (struct mbox_hdr *)(dst_mdev->mbase +
407 					      dst_mbox->mbox.rx_start);
408 		req_hdr->num_msgs = num_msgs;
409 
410 		otx2_forward_msg_pfvf(dst_mdev, &pf->mbox_pfvf[0].mbox,
411 				      pf->mbox.bbuf_base, vf);
412 		mutex_unlock(&pf->mbox.lock);
413 	} else if (dir == MBOX_DIR_PFVF_UP) {
414 		src_mdev = &src_mbox->dev[0];
415 		mbox_hdr = src_mbox->hwbase + src_mbox->rx_start;
416 		req_hdr = (struct mbox_hdr *)(src_mdev->mbase +
417 					      src_mbox->rx_start);
418 		req_hdr->num_msgs = num_msgs;
419 
420 		dst_mbox = &pf->mbox_pfvf[0];
421 		dst_size = dst_mbox->mbox_up.tx_size -
422 				ALIGN(sizeof(*mbox_hdr), MBOX_MSG_ALIGN);
423 		/* Check if msgs fit into destination area */
424 		if (mbox_hdr->msg_size > dst_size)
425 			return -EINVAL;
426 
427 		dst_mdev = &dst_mbox->mbox_up.dev[vf];
428 		dst_mdev->mbase = src_mdev->mbase;
429 		dst_mdev->msg_size = mbox_hdr->msg_size;
430 		dst_mdev->num_msgs = mbox_hdr->num_msgs;
431 		err = otx2_sync_mbox_up_msg(dst_mbox, vf);
432 		if (err) {
433 			dev_warn(pf->dev,
434 				 "VF%d is not responding to mailbox\n", vf);
435 			return err;
436 		}
437 	} else if (dir == MBOX_DIR_VFPF_UP) {
438 		req_hdr = (struct mbox_hdr *)(src_mbox->dev[0].mbase +
439 					      src_mbox->rx_start);
440 		req_hdr->num_msgs = num_msgs;
441 		otx2_forward_msg_pfvf(&pf->mbox_pfvf->mbox_up.dev[vf],
442 				      &pf->mbox.mbox_up,
443 				      pf->mbox_pfvf[vf].bbuf_base,
444 				      0);
445 	}
446 
447 	return 0;
448 }
449 
otx2_pfvf_mbox_handler(struct work_struct *work)450 static void otx2_pfvf_mbox_handler(struct work_struct *work)
451 {
452 	struct mbox_msghdr *msg = NULL;
453 	int offset, vf_idx, id, err;
454 	struct otx2_mbox_dev *mdev;
455 	struct mbox_hdr *req_hdr;
456 	struct otx2_mbox *mbox;
457 	struct mbox *vf_mbox;
458 	struct otx2_nic *pf;
459 
460 	vf_mbox = container_of(work, struct mbox, mbox_wrk);
461 	pf = vf_mbox->pfvf;
462 	vf_idx = vf_mbox - pf->mbox_pfvf;
463 
464 	mbox = &pf->mbox_pfvf[0].mbox;
465 	mdev = &mbox->dev[vf_idx];
466 	req_hdr = (struct mbox_hdr *)(mdev->mbase + mbox->rx_start);
467 
468 	offset = ALIGN(sizeof(*req_hdr), MBOX_MSG_ALIGN);
469 
470 	for (id = 0; id < vf_mbox->num_msgs; id++) {
471 		msg = (struct mbox_msghdr *)(mdev->mbase + mbox->rx_start +
472 					     offset);
473 
474 		if (msg->sig != OTX2_MBOX_REQ_SIG)
475 			goto inval_msg;
476 
477 		/* Set VF's number in each of the msg */
478 		msg->pcifunc &= RVU_PFVF_FUNC_MASK;
479 		msg->pcifunc |= (vf_idx + 1) & RVU_PFVF_FUNC_MASK;
480 		offset = msg->next_msgoff;
481 	}
482 	err = otx2_forward_vf_mbox_msgs(pf, mbox, MBOX_DIR_PFAF, vf_idx,
483 					vf_mbox->num_msgs);
484 	if (err)
485 		goto inval_msg;
486 	return;
487 
488 inval_msg:
489 	otx2_reply_invalid_msg(mbox, vf_idx, 0, msg->id);
490 	otx2_mbox_msg_send(mbox, vf_idx);
491 }
492 
otx2_pfvf_mbox_up_handler(struct work_struct *work)493 static void otx2_pfvf_mbox_up_handler(struct work_struct *work)
494 {
495 	struct mbox *vf_mbox = container_of(work, struct mbox, mbox_up_wrk);
496 	struct otx2_nic *pf = vf_mbox->pfvf;
497 	struct otx2_mbox_dev *mdev;
498 	int offset, id, vf_idx = 0;
499 	struct mbox_hdr *rsp_hdr;
500 	struct mbox_msghdr *msg;
501 	struct otx2_mbox *mbox;
502 
503 	vf_idx = vf_mbox - pf->mbox_pfvf;
504 	mbox = &pf->mbox_pfvf[0].mbox_up;
505 	mdev = &mbox->dev[vf_idx];
506 
507 	rsp_hdr = (struct mbox_hdr *)(mdev->mbase + mbox->rx_start);
508 	offset = mbox->rx_start + ALIGN(sizeof(*rsp_hdr), MBOX_MSG_ALIGN);
509 
510 	for (id = 0; id < vf_mbox->up_num_msgs; id++) {
511 		msg = mdev->mbase + offset;
512 
513 		if (msg->id >= MBOX_MSG_MAX) {
514 			dev_err(pf->dev,
515 				"Mbox msg with unknown ID 0x%x\n", msg->id);
516 			goto end;
517 		}
518 
519 		if (msg->sig != OTX2_MBOX_RSP_SIG) {
520 			dev_err(pf->dev,
521 				"Mbox msg with wrong signature %x, ID 0x%x\n",
522 				msg->sig, msg->id);
523 			goto end;
524 		}
525 
526 		switch (msg->id) {
527 		case MBOX_MSG_CGX_LINK_EVENT:
528 			break;
529 		default:
530 			if (msg->rc)
531 				dev_err(pf->dev,
532 					"Mbox msg response has err %d, ID 0x%x\n",
533 					msg->rc, msg->id);
534 			break;
535 		}
536 
537 end:
538 		offset = mbox->rx_start + msg->next_msgoff;
539 		if (mdev->msgs_acked == (vf_mbox->up_num_msgs - 1))
540 			__otx2_mbox_reset(mbox, 0);
541 		mdev->msgs_acked++;
542 	}
543 }
544 
otx2_pfvf_mbox_intr_handler(int irq, void *pf_irq)545 static irqreturn_t otx2_pfvf_mbox_intr_handler(int irq, void *pf_irq)
546 {
547 	struct otx2_nic *pf = (struct otx2_nic *)(pf_irq);
548 	int vfs = pf->total_vfs;
549 	struct mbox *mbox;
550 	u64 intr;
551 
552 	mbox = pf->mbox_pfvf;
553 	/* Handle VF interrupts */
554 	if (vfs > 64) {
555 		intr = otx2_read64(pf, RVU_PF_VFPF_MBOX_INTX(1));
556 		otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(1), intr);
557 		otx2_queue_work(mbox, pf->mbox_pfvf_wq, 64, vfs, intr,
558 				TYPE_PFVF);
559 		if (intr)
560 			trace_otx2_msg_interrupt(mbox->mbox.pdev, "VF(s) to PF", intr);
561 		vfs = 64;
562 	}
563 
564 	intr = otx2_read64(pf, RVU_PF_VFPF_MBOX_INTX(0));
565 	otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(0), intr);
566 
567 	otx2_queue_work(mbox, pf->mbox_pfvf_wq, 0, vfs, intr, TYPE_PFVF);
568 
569 	if (intr)
570 		trace_otx2_msg_interrupt(mbox->mbox.pdev, "VF(s) to PF", intr);
571 
572 	return IRQ_HANDLED;
573 }
574 
otx2_pfvf_mbox_init(struct otx2_nic *pf, int numvfs)575 static int otx2_pfvf_mbox_init(struct otx2_nic *pf, int numvfs)
576 {
577 	void __iomem *hwbase;
578 	struct mbox *mbox;
579 	int err, vf;
580 	u64 base;
581 
582 	if (!numvfs)
583 		return -EINVAL;
584 
585 	pf->mbox_pfvf = devm_kcalloc(&pf->pdev->dev, numvfs,
586 				     sizeof(struct mbox), GFP_KERNEL);
587 	if (!pf->mbox_pfvf)
588 		return -ENOMEM;
589 
590 	pf->mbox_pfvf_wq = alloc_workqueue("otx2_pfvf_mailbox",
591 					   WQ_UNBOUND | WQ_HIGHPRI |
592 					   WQ_MEM_RECLAIM, 1);
593 	if (!pf->mbox_pfvf_wq)
594 		return -ENOMEM;
595 
596 	base = readq((void __iomem *)((u64)pf->reg_base + RVU_PF_VF_BAR4_ADDR));
597 	hwbase = ioremap_wc(base, MBOX_SIZE * pf->total_vfs);
598 
599 	if (!hwbase) {
600 		err = -ENOMEM;
601 		goto free_wq;
602 	}
603 
604 	mbox = &pf->mbox_pfvf[0];
605 	err = otx2_mbox_init(&mbox->mbox, hwbase, pf->pdev, pf->reg_base,
606 			     MBOX_DIR_PFVF, numvfs);
607 	if (err)
608 		goto free_iomem;
609 
610 	err = otx2_mbox_init(&mbox->mbox_up, hwbase, pf->pdev, pf->reg_base,
611 			     MBOX_DIR_PFVF_UP, numvfs);
612 	if (err)
613 		goto free_iomem;
614 
615 	for (vf = 0; vf < numvfs; vf++) {
616 		mbox->pfvf = pf;
617 		INIT_WORK(&mbox->mbox_wrk, otx2_pfvf_mbox_handler);
618 		INIT_WORK(&mbox->mbox_up_wrk, otx2_pfvf_mbox_up_handler);
619 		mbox++;
620 	}
621 
622 	return 0;
623 
624 free_iomem:
625 	if (hwbase)
626 		iounmap(hwbase);
627 free_wq:
628 	destroy_workqueue(pf->mbox_pfvf_wq);
629 	return err;
630 }
631 
otx2_pfvf_mbox_destroy(struct otx2_nic *pf)632 static void otx2_pfvf_mbox_destroy(struct otx2_nic *pf)
633 {
634 	struct mbox *mbox = &pf->mbox_pfvf[0];
635 
636 	if (!mbox)
637 		return;
638 
639 	if (pf->mbox_pfvf_wq) {
640 		destroy_workqueue(pf->mbox_pfvf_wq);
641 		pf->mbox_pfvf_wq = NULL;
642 	}
643 
644 	if (mbox->mbox.hwbase)
645 		iounmap(mbox->mbox.hwbase);
646 
647 	otx2_mbox_destroy(&mbox->mbox);
648 }
649 
otx2_enable_pfvf_mbox_intr(struct otx2_nic *pf, int numvfs)650 static void otx2_enable_pfvf_mbox_intr(struct otx2_nic *pf, int numvfs)
651 {
652 	/* Clear PF <=> VF mailbox IRQ */
653 	otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(0), ~0ull);
654 	otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(1), ~0ull);
655 
656 	/* Enable PF <=> VF mailbox IRQ */
657 	otx2_write64(pf, RVU_PF_VFPF_MBOX_INT_ENA_W1SX(0), INTR_MASK(numvfs));
658 	if (numvfs > 64) {
659 		numvfs -= 64;
660 		otx2_write64(pf, RVU_PF_VFPF_MBOX_INT_ENA_W1SX(1),
661 			     INTR_MASK(numvfs));
662 	}
663 }
664 
otx2_disable_pfvf_mbox_intr(struct otx2_nic *pf, int numvfs)665 static void otx2_disable_pfvf_mbox_intr(struct otx2_nic *pf, int numvfs)
666 {
667 	int vector;
668 
669 	/* Disable PF <=> VF mailbox IRQ */
670 	otx2_write64(pf, RVU_PF_VFPF_MBOX_INT_ENA_W1CX(0), ~0ull);
671 	otx2_write64(pf, RVU_PF_VFPF_MBOX_INT_ENA_W1CX(1), ~0ull);
672 
673 	otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(0), ~0ull);
674 	vector = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFPF_MBOX0);
675 	free_irq(vector, pf);
676 
677 	if (numvfs > 64) {
678 		otx2_write64(pf, RVU_PF_VFPF_MBOX_INTX(1), ~0ull);
679 		vector = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFPF_MBOX1);
680 		free_irq(vector, pf);
681 	}
682 }
683 
otx2_register_pfvf_mbox_intr(struct otx2_nic *pf, int numvfs)684 static int otx2_register_pfvf_mbox_intr(struct otx2_nic *pf, int numvfs)
685 {
686 	struct otx2_hw *hw = &pf->hw;
687 	char *irq_name;
688 	int err;
689 
690 	/* Register MBOX0 interrupt handler */
691 	irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFPF_MBOX0 * NAME_SIZE];
692 	if (pf->pcifunc)
693 		snprintf(irq_name, NAME_SIZE,
694 			 "RVUPF%d_VF Mbox0", rvu_get_pf(pf->pcifunc));
695 	else
696 		snprintf(irq_name, NAME_SIZE, "RVUPF_VF Mbox0");
697 	err = request_irq(pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_VFPF_MBOX0),
698 			  otx2_pfvf_mbox_intr_handler, 0, irq_name, pf);
699 	if (err) {
700 		dev_err(pf->dev,
701 			"RVUPF: IRQ registration failed for PFVF mbox0 irq\n");
702 		return err;
703 	}
704 
705 	if (numvfs > 64) {
706 		/* Register MBOX1 interrupt handler */
707 		irq_name = &hw->irq_name[RVU_PF_INT_VEC_VFPF_MBOX1 * NAME_SIZE];
708 		if (pf->pcifunc)
709 			snprintf(irq_name, NAME_SIZE,
710 				 "RVUPF%d_VF Mbox1", rvu_get_pf(pf->pcifunc));
711 		else
712 			snprintf(irq_name, NAME_SIZE, "RVUPF_VF Mbox1");
713 		err = request_irq(pci_irq_vector(pf->pdev,
714 						 RVU_PF_INT_VEC_VFPF_MBOX1),
715 						 otx2_pfvf_mbox_intr_handler,
716 						 0, irq_name, pf);
717 		if (err) {
718 			dev_err(pf->dev,
719 				"RVUPF: IRQ registration failed for PFVF mbox1 irq\n");
720 			return err;
721 		}
722 	}
723 
724 	otx2_enable_pfvf_mbox_intr(pf, numvfs);
725 
726 	return 0;
727 }
728 
otx2_process_pfaf_mbox_msg(struct otx2_nic *pf, struct mbox_msghdr *msg)729 static void otx2_process_pfaf_mbox_msg(struct otx2_nic *pf,
730 				       struct mbox_msghdr *msg)
731 {
732 	int devid;
733 
734 	if (msg->id >= MBOX_MSG_MAX) {
735 		dev_err(pf->dev,
736 			"Mbox msg with unknown ID 0x%x\n", msg->id);
737 		return;
738 	}
739 
740 	if (msg->sig != OTX2_MBOX_RSP_SIG) {
741 		dev_err(pf->dev,
742 			"Mbox msg with wrong signature %x, ID 0x%x\n",
743 			 msg->sig, msg->id);
744 		return;
745 	}
746 
747 	/* message response heading VF */
748 	devid = msg->pcifunc & RVU_PFVF_FUNC_MASK;
749 	if (devid) {
750 		struct otx2_vf_config *config = &pf->vf_configs[devid - 1];
751 		struct delayed_work *dwork;
752 
753 		switch (msg->id) {
754 		case MBOX_MSG_NIX_LF_START_RX:
755 			config->intf_down = false;
756 			dwork = &config->link_event_work;
757 			schedule_delayed_work(dwork, msecs_to_jiffies(100));
758 			break;
759 		case MBOX_MSG_NIX_LF_STOP_RX:
760 			config->intf_down = true;
761 			break;
762 		}
763 
764 		return;
765 	}
766 
767 	switch (msg->id) {
768 	case MBOX_MSG_READY:
769 		pf->pcifunc = msg->pcifunc;
770 		break;
771 	case MBOX_MSG_MSIX_OFFSET:
772 		mbox_handler_msix_offset(pf, (struct msix_offset_rsp *)msg);
773 		break;
774 	case MBOX_MSG_NPA_LF_ALLOC:
775 		mbox_handler_npa_lf_alloc(pf, (struct npa_lf_alloc_rsp *)msg);
776 		break;
777 	case MBOX_MSG_NIX_LF_ALLOC:
778 		mbox_handler_nix_lf_alloc(pf, (struct nix_lf_alloc_rsp *)msg);
779 		break;
780 	case MBOX_MSG_NIX_TXSCH_ALLOC:
781 		mbox_handler_nix_txsch_alloc(pf,
782 					     (struct nix_txsch_alloc_rsp *)msg);
783 		break;
784 	case MBOX_MSG_NIX_BP_ENABLE:
785 		mbox_handler_nix_bp_enable(pf, (struct nix_bp_cfg_rsp *)msg);
786 		break;
787 	case MBOX_MSG_CGX_STATS:
788 		mbox_handler_cgx_stats(pf, (struct cgx_stats_rsp *)msg);
789 		break;
790 	default:
791 		if (msg->rc)
792 			dev_err(pf->dev,
793 				"Mbox msg response has err %d, ID 0x%x\n",
794 				msg->rc, msg->id);
795 		break;
796 	}
797 }
798 
otx2_pfaf_mbox_handler(struct work_struct *work)799 static void otx2_pfaf_mbox_handler(struct work_struct *work)
800 {
801 	struct otx2_mbox_dev *mdev;
802 	struct mbox_hdr *rsp_hdr;
803 	struct mbox_msghdr *msg;
804 	struct otx2_mbox *mbox;
805 	struct mbox *af_mbox;
806 	struct otx2_nic *pf;
807 	int offset, id;
808 
809 	af_mbox = container_of(work, struct mbox, mbox_wrk);
810 	mbox = &af_mbox->mbox;
811 	mdev = &mbox->dev[0];
812 	rsp_hdr = (struct mbox_hdr *)(mdev->mbase + mbox->rx_start);
813 
814 	offset = mbox->rx_start + ALIGN(sizeof(*rsp_hdr), MBOX_MSG_ALIGN);
815 	pf = af_mbox->pfvf;
816 
817 	for (id = 0; id < af_mbox->num_msgs; id++) {
818 		msg = (struct mbox_msghdr *)(mdev->mbase + offset);
819 		otx2_process_pfaf_mbox_msg(pf, msg);
820 		offset = mbox->rx_start + msg->next_msgoff;
821 		if (mdev->msgs_acked == (af_mbox->num_msgs - 1))
822 			__otx2_mbox_reset(mbox, 0);
823 		mdev->msgs_acked++;
824 	}
825 
826 }
827 
otx2_handle_link_event(struct otx2_nic *pf)828 static void otx2_handle_link_event(struct otx2_nic *pf)
829 {
830 	struct cgx_link_user_info *linfo = &pf->linfo;
831 	struct net_device *netdev = pf->netdev;
832 
833 	pr_info("%s NIC Link is %s %d Mbps %s duplex\n", netdev->name,
834 		linfo->link_up ? "UP" : "DOWN", linfo->speed,
835 		linfo->full_duplex ? "Full" : "Half");
836 	if (linfo->link_up) {
837 		netif_carrier_on(netdev);
838 		netif_tx_start_all_queues(netdev);
839 	} else {
840 		netif_tx_stop_all_queues(netdev);
841 		netif_carrier_off(netdev);
842 	}
843 }
844 
otx2_mbox_up_handler_cgx_link_event(struct otx2_nic *pf, struct cgx_link_info_msg *msg, struct msg_rsp *rsp)845 int otx2_mbox_up_handler_cgx_link_event(struct otx2_nic *pf,
846 					struct cgx_link_info_msg *msg,
847 					struct msg_rsp *rsp)
848 {
849 	int i;
850 
851 	/* Copy the link info sent by AF */
852 	pf->linfo = msg->link_info;
853 
854 	/* notify VFs about link event */
855 	for (i = 0; i < pci_num_vf(pf->pdev); i++) {
856 		struct otx2_vf_config *config = &pf->vf_configs[i];
857 		struct delayed_work *dwork = &config->link_event_work;
858 
859 		if (config->intf_down)
860 			continue;
861 
862 		schedule_delayed_work(dwork, msecs_to_jiffies(100));
863 	}
864 
865 	/* interface has not been fully configured yet */
866 	if (pf->flags & OTX2_FLAG_INTF_DOWN)
867 		return 0;
868 
869 	otx2_handle_link_event(pf);
870 	return 0;
871 }
872 
otx2_process_mbox_msg_up(struct otx2_nic *pf, struct mbox_msghdr *req)873 static int otx2_process_mbox_msg_up(struct otx2_nic *pf,
874 				    struct mbox_msghdr *req)
875 {
876 	/* Check if valid, if not reply with a invalid msg */
877 	if (req->sig != OTX2_MBOX_REQ_SIG) {
878 		otx2_reply_invalid_msg(&pf->mbox.mbox_up, 0, 0, req->id);
879 		return -ENODEV;
880 	}
881 
882 	switch (req->id) {
883 #define M(_name, _id, _fn_name, _req_type, _rsp_type)			\
884 	case _id: {							\
885 		struct _rsp_type *rsp;					\
886 		int err;						\
887 									\
888 		rsp = (struct _rsp_type *)otx2_mbox_alloc_msg(		\
889 			&pf->mbox.mbox_up, 0,				\
890 			sizeof(struct _rsp_type));			\
891 		if (!rsp)						\
892 			return -ENOMEM;					\
893 									\
894 		rsp->hdr.id = _id;					\
895 		rsp->hdr.sig = OTX2_MBOX_RSP_SIG;			\
896 		rsp->hdr.pcifunc = 0;					\
897 		rsp->hdr.rc = 0;					\
898 									\
899 		err = otx2_mbox_up_handler_ ## _fn_name(		\
900 			pf, (struct _req_type *)req, rsp);		\
901 		return err;						\
902 	}
903 MBOX_UP_CGX_MESSAGES
904 #undef M
905 		break;
906 	default:
907 		otx2_reply_invalid_msg(&pf->mbox.mbox_up, 0, 0, req->id);
908 		return -ENODEV;
909 	}
910 	return 0;
911 }
912 
otx2_pfaf_mbox_up_handler(struct work_struct *work)913 static void otx2_pfaf_mbox_up_handler(struct work_struct *work)
914 {
915 	struct mbox *af_mbox = container_of(work, struct mbox, mbox_up_wrk);
916 	struct otx2_mbox *mbox = &af_mbox->mbox_up;
917 	struct otx2_mbox_dev *mdev = &mbox->dev[0];
918 	struct otx2_nic *pf = af_mbox->pfvf;
919 	int offset, id, devid = 0;
920 	struct mbox_hdr *rsp_hdr;
921 	struct mbox_msghdr *msg;
922 
923 	rsp_hdr = (struct mbox_hdr *)(mdev->mbase + mbox->rx_start);
924 
925 	offset = mbox->rx_start + ALIGN(sizeof(*rsp_hdr), MBOX_MSG_ALIGN);
926 
927 	for (id = 0; id < af_mbox->up_num_msgs; id++) {
928 		msg = (struct mbox_msghdr *)(mdev->mbase + offset);
929 
930 		devid = msg->pcifunc & RVU_PFVF_FUNC_MASK;
931 		/* Skip processing VF's messages */
932 		if (!devid)
933 			otx2_process_mbox_msg_up(pf, msg);
934 		offset = mbox->rx_start + msg->next_msgoff;
935 	}
936 	if (devid) {
937 		otx2_forward_vf_mbox_msgs(pf, &pf->mbox.mbox_up,
938 					  MBOX_DIR_PFVF_UP, devid - 1,
939 					  af_mbox->up_num_msgs);
940 		return;
941 	}
942 
943 	otx2_mbox_msg_send(mbox, 0);
944 }
945 
otx2_pfaf_mbox_intr_handler(int irq, void *pf_irq)946 static irqreturn_t otx2_pfaf_mbox_intr_handler(int irq, void *pf_irq)
947 {
948 	struct otx2_nic *pf = (struct otx2_nic *)pf_irq;
949 	struct mbox *mbox;
950 
951 	/* Clear the IRQ */
952 	otx2_write64(pf, RVU_PF_INT, BIT_ULL(0));
953 
954 	mbox = &pf->mbox;
955 
956 	trace_otx2_msg_interrupt(mbox->mbox.pdev, "AF to PF", BIT_ULL(0));
957 
958 	otx2_queue_work(mbox, pf->mbox_wq, 0, 1, 1, TYPE_PFAF);
959 
960 	return IRQ_HANDLED;
961 }
962 
otx2_disable_mbox_intr(struct otx2_nic *pf)963 static void otx2_disable_mbox_intr(struct otx2_nic *pf)
964 {
965 	int vector = pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_AFPF_MBOX);
966 
967 	/* Disable AF => PF mailbox IRQ */
968 	otx2_write64(pf, RVU_PF_INT_ENA_W1C, BIT_ULL(0));
969 	free_irq(vector, pf);
970 }
971 
otx2_register_mbox_intr(struct otx2_nic *pf, bool probe_af)972 static int otx2_register_mbox_intr(struct otx2_nic *pf, bool probe_af)
973 {
974 	struct otx2_hw *hw = &pf->hw;
975 	struct msg_req *req;
976 	char *irq_name;
977 	int err;
978 
979 	/* Register mailbox interrupt handler */
980 	irq_name = &hw->irq_name[RVU_PF_INT_VEC_AFPF_MBOX * NAME_SIZE];
981 	snprintf(irq_name, NAME_SIZE, "RVUPFAF Mbox");
982 	err = request_irq(pci_irq_vector(pf->pdev, RVU_PF_INT_VEC_AFPF_MBOX),
983 			  otx2_pfaf_mbox_intr_handler, 0, irq_name, pf);
984 	if (err) {
985 		dev_err(pf->dev,
986 			"RVUPF: IRQ registration failed for PFAF mbox irq\n");
987 		return err;
988 	}
989 
990 	/* Enable mailbox interrupt for msgs coming from AF.
991 	 * First clear to avoid spurious interrupts, if any.
992 	 */
993 	otx2_write64(pf, RVU_PF_INT, BIT_ULL(0));
994 	otx2_write64(pf, RVU_PF_INT_ENA_W1S, BIT_ULL(0));
995 
996 	if (!probe_af)
997 		return 0;
998 
999 	/* Check mailbox communication with AF */
1000 	req = otx2_mbox_alloc_msg_ready(&pf->mbox);
1001 	if (!req) {
1002 		otx2_disable_mbox_intr(pf);
1003 		return -ENOMEM;
1004 	}
1005 	err = otx2_sync_mbox_msg(&pf->mbox);
1006 	if (err) {
1007 		dev_warn(pf->dev,
1008 			 "AF not responding to mailbox, deferring probe\n");
1009 		otx2_disable_mbox_intr(pf);
1010 		return -EPROBE_DEFER;
1011 	}
1012 
1013 	return 0;
1014 }
1015 
otx2_pfaf_mbox_destroy(struct otx2_nic *pf)1016 static void otx2_pfaf_mbox_destroy(struct otx2_nic *pf)
1017 {
1018 	struct mbox *mbox = &pf->mbox;
1019 
1020 	if (pf->mbox_wq) {
1021 		destroy_workqueue(pf->mbox_wq);
1022 		pf->mbox_wq = NULL;
1023 	}
1024 
1025 	if (mbox->mbox.hwbase)
1026 		iounmap((void __iomem *)mbox->mbox.hwbase);
1027 
1028 	otx2_mbox_destroy(&mbox->mbox);
1029 	otx2_mbox_destroy(&mbox->mbox_up);
1030 }
1031 
otx2_pfaf_mbox_init(struct otx2_nic *pf)1032 static int otx2_pfaf_mbox_init(struct otx2_nic *pf)
1033 {
1034 	struct mbox *mbox = &pf->mbox;
1035 	void __iomem *hwbase;
1036 	int err;
1037 
1038 	mbox->pfvf = pf;
1039 	pf->mbox_wq = alloc_workqueue("otx2_pfaf_mailbox",
1040 				      WQ_UNBOUND | WQ_HIGHPRI |
1041 				      WQ_MEM_RECLAIM, 1);
1042 	if (!pf->mbox_wq)
1043 		return -ENOMEM;
1044 
1045 	/* Mailbox is a reserved memory (in RAM) region shared between
1046 	 * admin function (i.e AF) and this PF, shouldn't be mapped as
1047 	 * device memory to allow unaligned accesses.
1048 	 */
1049 	hwbase = ioremap_wc(pci_resource_start(pf->pdev, PCI_MBOX_BAR_NUM),
1050 			    pci_resource_len(pf->pdev, PCI_MBOX_BAR_NUM));
1051 	if (!hwbase) {
1052 		dev_err(pf->dev, "Unable to map PFAF mailbox region\n");
1053 		err = -ENOMEM;
1054 		goto exit;
1055 	}
1056 
1057 	err = otx2_mbox_init(&mbox->mbox, hwbase, pf->pdev, pf->reg_base,
1058 			     MBOX_DIR_PFAF, 1);
1059 	if (err)
1060 		goto exit;
1061 
1062 	err = otx2_mbox_init(&mbox->mbox_up, hwbase, pf->pdev, pf->reg_base,
1063 			     MBOX_DIR_PFAF_UP, 1);
1064 	if (err)
1065 		goto exit;
1066 
1067 	err = otx2_mbox_bbuf_init(mbox, pf->pdev);
1068 	if (err)
1069 		goto exit;
1070 
1071 	INIT_WORK(&mbox->mbox_wrk, otx2_pfaf_mbox_handler);
1072 	INIT_WORK(&mbox->mbox_up_wrk, otx2_pfaf_mbox_up_handler);
1073 	mutex_init(&mbox->lock);
1074 
1075 	return 0;
1076 exit:
1077 	otx2_pfaf_mbox_destroy(pf);
1078 	return err;
1079 }
1080 
otx2_cgx_config_linkevents(struct otx2_nic *pf, bool enable)1081 static int otx2_cgx_config_linkevents(struct otx2_nic *pf, bool enable)
1082 {
1083 	struct msg_req *msg;
1084 	int err;
1085 
1086 	mutex_lock(&pf->mbox.lock);
1087 	if (enable)
1088 		msg = otx2_mbox_alloc_msg_cgx_start_linkevents(&pf->mbox);
1089 	else
1090 		msg = otx2_mbox_alloc_msg_cgx_stop_linkevents(&pf->mbox);
1091 
1092 	if (!msg) {
1093 		mutex_unlock(&pf->mbox.lock);
1094 		return -ENOMEM;
1095 	}
1096 
1097 	err = otx2_sync_mbox_msg(&pf->mbox);
1098 	mutex_unlock(&pf->mbox.lock);
1099 	return err;
1100 }
1101 
otx2_cgx_config_loopback(struct otx2_nic *pf, bool enable)1102 static int otx2_cgx_config_loopback(struct otx2_nic *pf, bool enable)
1103 {
1104 	struct msg_req *msg;
1105 	int err;
1106 
1107 	mutex_lock(&pf->mbox.lock);
1108 	if (enable)
1109 		msg = otx2_mbox_alloc_msg_cgx_intlbk_enable(&pf->mbox);
1110 	else
1111 		msg = otx2_mbox_alloc_msg_cgx_intlbk_disable(&pf->mbox);
1112 
1113 	if (!msg) {
1114 		mutex_unlock(&pf->mbox.lock);
1115 		return -ENOMEM;
1116 	}
1117 
1118 	err = otx2_sync_mbox_msg(&pf->mbox);
1119 	mutex_unlock(&pf->mbox.lock);
1120 	return err;
1121 }
1122 
otx2_set_real_num_queues(struct net_device *netdev, int tx_queues, int rx_queues)1123 int otx2_set_real_num_queues(struct net_device *netdev,
1124 			     int tx_queues, int rx_queues)
1125 {
1126 	int err;
1127 
1128 	err = netif_set_real_num_tx_queues(netdev, tx_queues);
1129 	if (err) {
1130 		netdev_err(netdev,
1131 			   "Failed to set no of Tx queues: %d\n", tx_queues);
1132 		return err;
1133 	}
1134 
1135 	err = netif_set_real_num_rx_queues(netdev, rx_queues);
1136 	if (err)
1137 		netdev_err(netdev,
1138 			   "Failed to set no of Rx queues: %d\n", rx_queues);
1139 	return err;
1140 }
1141 EXPORT_SYMBOL(otx2_set_real_num_queues);
1142 
otx2_q_intr_handler(int irq, void *data)1143 static irqreturn_t otx2_q_intr_handler(int irq, void *data)
1144 {
1145 	struct otx2_nic *pf = data;
1146 	u64 val, *ptr;
1147 	u64 qidx = 0;
1148 
1149 	/* CQ */
1150 	for (qidx = 0; qidx < pf->qset.cq_cnt; qidx++) {
1151 		ptr = otx2_get_regaddr(pf, NIX_LF_CQ_OP_INT);
1152 		val = otx2_atomic64_add((qidx << 44), ptr);
1153 
1154 		otx2_write64(pf, NIX_LF_CQ_OP_INT, (qidx << 44) |
1155 			     (val & NIX_CQERRINT_BITS));
1156 		if (!(val & (NIX_CQERRINT_BITS | BIT_ULL(42))))
1157 			continue;
1158 
1159 		if (val & BIT_ULL(42)) {
1160 			netdev_err(pf->netdev, "CQ%lld: error reading NIX_LF_CQ_OP_INT, NIX_LF_ERR_INT 0x%llx\n",
1161 				   qidx, otx2_read64(pf, NIX_LF_ERR_INT));
1162 		} else {
1163 			if (val & BIT_ULL(NIX_CQERRINT_DOOR_ERR))
1164 				netdev_err(pf->netdev, "CQ%lld: Doorbell error",
1165 					   qidx);
1166 			if (val & BIT_ULL(NIX_CQERRINT_CQE_FAULT))
1167 				netdev_err(pf->netdev, "CQ%lld: Memory fault on CQE write to LLC/DRAM",
1168 					   qidx);
1169 		}
1170 
1171 		schedule_work(&pf->reset_task);
1172 	}
1173 
1174 	/* SQ */
1175 	for (qidx = 0; qidx < pf->hw.tx_queues; qidx++) {
1176 		ptr = otx2_get_regaddr(pf, NIX_LF_SQ_OP_INT);
1177 		val = otx2_atomic64_add((qidx << 44), ptr);
1178 		otx2_write64(pf, NIX_LF_SQ_OP_INT, (qidx << 44) |
1179 			     (val & NIX_SQINT_BITS));
1180 
1181 		if (!(val & (NIX_SQINT_BITS | BIT_ULL(42))))
1182 			continue;
1183 
1184 		if (val & BIT_ULL(42)) {
1185 			netdev_err(pf->netdev, "SQ%lld: error reading NIX_LF_SQ_OP_INT, NIX_LF_ERR_INT 0x%llx\n",
1186 				   qidx, otx2_read64(pf, NIX_LF_ERR_INT));
1187 		} else {
1188 			if (val & BIT_ULL(NIX_SQINT_LMT_ERR)) {
1189 				netdev_err(pf->netdev, "SQ%lld: LMT store error NIX_LF_SQ_OP_ERR_DBG:0x%llx",
1190 					   qidx,
1191 					   otx2_read64(pf,
1192 						       NIX_LF_SQ_OP_ERR_DBG));
1193 				otx2_write64(pf, NIX_LF_SQ_OP_ERR_DBG,
1194 					     BIT_ULL(44));
1195 			}
1196 			if (val & BIT_ULL(NIX_SQINT_MNQ_ERR)) {
1197 				netdev_err(pf->netdev, "SQ%lld: Meta-descriptor enqueue error NIX_LF_MNQ_ERR_DGB:0x%llx\n",
1198 					   qidx,
1199 					   otx2_read64(pf, NIX_LF_MNQ_ERR_DBG));
1200 				otx2_write64(pf, NIX_LF_MNQ_ERR_DBG,
1201 					     BIT_ULL(44));
1202 			}
1203 			if (val & BIT_ULL(NIX_SQINT_SEND_ERR)) {
1204 				netdev_err(pf->netdev, "SQ%lld: Send error, NIX_LF_SEND_ERR_DBG 0x%llx",
1205 					   qidx,
1206 					   otx2_read64(pf,
1207 						       NIX_LF_SEND_ERR_DBG));
1208 				otx2_write64(pf, NIX_LF_SEND_ERR_DBG,
1209 					     BIT_ULL(44));
1210 			}
1211 			if (val & BIT_ULL(NIX_SQINT_SQB_ALLOC_FAIL))
1212 				netdev_err(pf->netdev, "SQ%lld: SQB allocation failed",
1213 					   qidx);
1214 		}
1215 
1216 		schedule_work(&pf->reset_task);
1217 	}
1218 
1219 	return IRQ_HANDLED;
1220 }
1221 
otx2_cq_intr_handler(int irq, void *cq_irq)1222 static irqreturn_t otx2_cq_intr_handler(int irq, void *cq_irq)
1223 {
1224 	struct otx2_cq_poll *cq_poll = (struct otx2_cq_poll *)cq_irq;
1225 	struct otx2_nic *pf = (struct otx2_nic *)cq_poll->dev;
1226 	int qidx = cq_poll->cint_idx;
1227 
1228 	/* Disable interrupts.
1229 	 *
1230 	 * Completion interrupts behave in a level-triggered interrupt
1231 	 * fashion, and hence have to be cleared only after it is serviced.
1232 	 */
1233 	otx2_write64(pf, NIX_LF_CINTX_ENA_W1C(qidx), BIT_ULL(0));
1234 
1235 	/* Schedule NAPI */
1236 	napi_schedule_irqoff(&cq_poll->napi);
1237 
1238 	return IRQ_HANDLED;
1239 }
1240 
otx2_disable_napi(struct otx2_nic *pf)1241 static void otx2_disable_napi(struct otx2_nic *pf)
1242 {
1243 	struct otx2_qset *qset = &pf->qset;
1244 	struct otx2_cq_poll *cq_poll;
1245 	int qidx;
1246 
1247 	for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
1248 		cq_poll = &qset->napi[qidx];
1249 		napi_disable(&cq_poll->napi);
1250 		netif_napi_del(&cq_poll->napi);
1251 	}
1252 }
1253 
otx2_free_cq_res(struct otx2_nic *pf)1254 static void otx2_free_cq_res(struct otx2_nic *pf)
1255 {
1256 	struct otx2_qset *qset = &pf->qset;
1257 	struct otx2_cq_queue *cq;
1258 	int qidx;
1259 
1260 	/* Disable CQs */
1261 	otx2_ctx_disable(&pf->mbox, NIX_AQ_CTYPE_CQ, false);
1262 	for (qidx = 0; qidx < qset->cq_cnt; qidx++) {
1263 		cq = &qset->cq[qidx];
1264 		qmem_free(pf->dev, cq->cqe);
1265 	}
1266 }
1267 
otx2_free_sq_res(struct otx2_nic *pf)1268 static void otx2_free_sq_res(struct otx2_nic *pf)
1269 {
1270 	struct otx2_qset *qset = &pf->qset;
1271 	struct otx2_snd_queue *sq;
1272 	int qidx;
1273 
1274 	/* Disable SQs */
1275 	otx2_ctx_disable(&pf->mbox, NIX_AQ_CTYPE_SQ, false);
1276 	/* Free SQB pointers */
1277 	otx2_sq_free_sqbs(pf);
1278 	for (qidx = 0; qidx < pf->hw.tx_queues; qidx++) {
1279 		sq = &qset->sq[qidx];
1280 		qmem_free(pf->dev, sq->sqe);
1281 		qmem_free(pf->dev, sq->tso_hdrs);
1282 		kfree(sq->sg);
1283 		kfree(sq->sqb_ptrs);
1284 	}
1285 }
1286 
otx2_init_hw_resources(struct otx2_nic *pf)1287 static int otx2_init_hw_resources(struct otx2_nic *pf)
1288 {
1289 	struct mbox *mbox = &pf->mbox;
1290 	struct otx2_hw *hw = &pf->hw;
1291 	struct msg_req *req;
1292 	int err = 0, lvl;
1293 
1294 	/* Set required NPA LF's pool counts
1295 	 * Auras and Pools are used in a 1:1 mapping,
1296 	 * so, aura count = pool count.
1297 	 */
1298 	hw->rqpool_cnt = hw->rx_queues;
1299 	hw->sqpool_cnt = hw->tx_queues;
1300 	hw->pool_cnt = hw->rqpool_cnt + hw->sqpool_cnt;
1301 
1302 	/* Get the size of receive buffers to allocate */
1303 	pf->rbsize = RCV_FRAG_LEN(OTX2_HW_TIMESTAMP_LEN + pf->netdev->mtu +
1304 				  OTX2_ETH_HLEN);
1305 
1306 	mutex_lock(&mbox->lock);
1307 	/* NPA init */
1308 	err = otx2_config_npa(pf);
1309 	if (err)
1310 		goto exit;
1311 
1312 	/* NIX init */
1313 	err = otx2_config_nix(pf);
1314 	if (err)
1315 		goto err_free_npa_lf;
1316 
1317 	/* Enable backpressure for CGX mapped PF/VFs */
1318 	if (!is_otx2_lbkvf(pf->pdev))
1319 		otx2_nix_config_bp(pf, true);
1320 
1321 	/* Init Auras and pools used by NIX RQ, for free buffer ptrs */
1322 	err = otx2_rq_aura_pool_init(pf);
1323 	if (err) {
1324 		mutex_unlock(&mbox->lock);
1325 		goto err_free_nix_lf;
1326 	}
1327 	/* Init Auras and pools used by NIX SQ, for queueing SQEs */
1328 	err = otx2_sq_aura_pool_init(pf);
1329 	if (err) {
1330 		mutex_unlock(&mbox->lock);
1331 		goto err_free_rq_ptrs;
1332 	}
1333 
1334 	err = otx2_txsch_alloc(pf);
1335 	if (err) {
1336 		mutex_unlock(&mbox->lock);
1337 		goto err_free_sq_ptrs;
1338 	}
1339 
1340 	err = otx2_config_nix_queues(pf);
1341 	if (err) {
1342 		mutex_unlock(&mbox->lock);
1343 		goto err_free_txsch;
1344 	}
1345 	for (lvl = 0; lvl < NIX_TXSCH_LVL_CNT; lvl++) {
1346 		err = otx2_txschq_config(pf, lvl);
1347 		if (err) {
1348 			mutex_unlock(&mbox->lock);
1349 			goto err_free_nix_queues;
1350 		}
1351 	}
1352 	mutex_unlock(&mbox->lock);
1353 	return err;
1354 
1355 err_free_nix_queues:
1356 	otx2_free_sq_res(pf);
1357 	otx2_free_cq_res(pf);
1358 	otx2_ctx_disable(mbox, NIX_AQ_CTYPE_RQ, false);
1359 err_free_txsch:
1360 	if (otx2_txschq_stop(pf))
1361 		dev_err(pf->dev, "%s failed to stop TX schedulers\n", __func__);
1362 err_free_sq_ptrs:
1363 	otx2_sq_free_sqbs(pf);
1364 err_free_rq_ptrs:
1365 	otx2_free_aura_ptr(pf, AURA_NIX_RQ);
1366 	otx2_ctx_disable(mbox, NPA_AQ_CTYPE_POOL, true);
1367 	otx2_ctx_disable(mbox, NPA_AQ_CTYPE_AURA, true);
1368 	otx2_aura_pool_free(pf);
1369 err_free_nix_lf:
1370 	mutex_lock(&mbox->lock);
1371 	req = otx2_mbox_alloc_msg_nix_lf_free(mbox);
1372 	if (req) {
1373 		if (otx2_sync_mbox_msg(mbox))
1374 			dev_err(pf->dev, "%s failed to free nixlf\n", __func__);
1375 	}
1376 err_free_npa_lf:
1377 	/* Reset NPA LF */
1378 	req = otx2_mbox_alloc_msg_npa_lf_free(mbox);
1379 	if (req) {
1380 		if (otx2_sync_mbox_msg(mbox))
1381 			dev_err(pf->dev, "%s failed to free npalf\n", __func__);
1382 	}
1383 exit:
1384 	mutex_unlock(&mbox->lock);
1385 	return err;
1386 }
1387 
otx2_free_hw_resources(struct otx2_nic *pf)1388 static void otx2_free_hw_resources(struct otx2_nic *pf)
1389 {
1390 	struct otx2_qset *qset = &pf->qset;
1391 	struct mbox *mbox = &pf->mbox;
1392 	struct otx2_cq_queue *cq;
1393 	struct msg_req *req;
1394 	int qidx, err;
1395 
1396 	/* Ensure all SQE are processed */
1397 	otx2_sqb_flush(pf);
1398 
1399 	/* Stop transmission */
1400 	err = otx2_txschq_stop(pf);
1401 	if (err)
1402 		dev_err(pf->dev, "RVUPF: Failed to stop/free TX schedulers\n");
1403 
1404 	mutex_lock(&mbox->lock);
1405 	/* Disable backpressure */
1406 	if (!(pf->pcifunc & RVU_PFVF_FUNC_MASK))
1407 		otx2_nix_config_bp(pf, false);
1408 	mutex_unlock(&mbox->lock);
1409 
1410 	/* Disable RQs */
1411 	otx2_ctx_disable(mbox, NIX_AQ_CTYPE_RQ, false);
1412 
1413 	/*Dequeue all CQEs */
1414 	for (qidx = 0; qidx < qset->cq_cnt; qidx++) {
1415 		cq = &qset->cq[qidx];
1416 		if (cq->cq_type == CQ_RX)
1417 			otx2_cleanup_rx_cqes(pf, cq);
1418 		else
1419 			otx2_cleanup_tx_cqes(pf, cq);
1420 	}
1421 
1422 	otx2_free_sq_res(pf);
1423 
1424 	/* Free RQ buffer pointers*/
1425 	otx2_free_aura_ptr(pf, AURA_NIX_RQ);
1426 
1427 	otx2_free_cq_res(pf);
1428 
1429 	mutex_lock(&mbox->lock);
1430 	/* Reset NIX LF */
1431 	req = otx2_mbox_alloc_msg_nix_lf_free(mbox);
1432 	if (req) {
1433 		if (otx2_sync_mbox_msg(mbox))
1434 			dev_err(pf->dev, "%s failed to free nixlf\n", __func__);
1435 	}
1436 	mutex_unlock(&mbox->lock);
1437 
1438 	/* Disable NPA Pool and Aura hw context */
1439 	otx2_ctx_disable(mbox, NPA_AQ_CTYPE_POOL, true);
1440 	otx2_ctx_disable(mbox, NPA_AQ_CTYPE_AURA, true);
1441 	otx2_aura_pool_free(pf);
1442 
1443 	mutex_lock(&mbox->lock);
1444 	/* Reset NPA LF */
1445 	req = otx2_mbox_alloc_msg_npa_lf_free(mbox);
1446 	if (req) {
1447 		if (otx2_sync_mbox_msg(mbox))
1448 			dev_err(pf->dev, "%s failed to free npalf\n", __func__);
1449 	}
1450 	mutex_unlock(&mbox->lock);
1451 }
1452 
otx2_open(struct net_device *netdev)1453 int otx2_open(struct net_device *netdev)
1454 {
1455 	struct otx2_nic *pf = netdev_priv(netdev);
1456 	struct otx2_cq_poll *cq_poll = NULL;
1457 	struct otx2_qset *qset = &pf->qset;
1458 	int err = 0, qidx, vec;
1459 	char *irq_name;
1460 
1461 	netif_carrier_off(netdev);
1462 
1463 	pf->qset.cq_cnt = pf->hw.rx_queues + pf->hw.tx_queues;
1464 	/* RQ and SQs are mapped to different CQs,
1465 	 * so find out max CQ IRQs (i.e CINTs) needed.
1466 	 */
1467 	pf->hw.cint_cnt = max(pf->hw.rx_queues, pf->hw.tx_queues);
1468 	qset->napi = kcalloc(pf->hw.cint_cnt, sizeof(*cq_poll), GFP_KERNEL);
1469 	if (!qset->napi)
1470 		return -ENOMEM;
1471 
1472 	/* CQ size of RQ */
1473 	qset->rqe_cnt = qset->rqe_cnt ? qset->rqe_cnt : Q_COUNT(Q_SIZE_256);
1474 	/* CQ size of SQ */
1475 	qset->sqe_cnt = qset->sqe_cnt ? qset->sqe_cnt : Q_COUNT(Q_SIZE_4K);
1476 
1477 	err = -ENOMEM;
1478 	qset->cq = kcalloc(pf->qset.cq_cnt,
1479 			   sizeof(struct otx2_cq_queue), GFP_KERNEL);
1480 	if (!qset->cq)
1481 		goto err_free_mem;
1482 
1483 	qset->sq = kcalloc(pf->hw.tx_queues,
1484 			   sizeof(struct otx2_snd_queue), GFP_KERNEL);
1485 	if (!qset->sq)
1486 		goto err_free_mem;
1487 
1488 	qset->rq = kcalloc(pf->hw.rx_queues,
1489 			   sizeof(struct otx2_rcv_queue), GFP_KERNEL);
1490 	if (!qset->rq)
1491 		goto err_free_mem;
1492 
1493 	err = otx2_init_hw_resources(pf);
1494 	if (err)
1495 		goto err_free_mem;
1496 
1497 	/* Register NAPI handler */
1498 	for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
1499 		cq_poll = &qset->napi[qidx];
1500 		cq_poll->cint_idx = qidx;
1501 		/* RQ0 & SQ0 are mapped to CINT0 and so on..
1502 		 * 'cq_ids[0]' points to RQ's CQ and
1503 		 * 'cq_ids[1]' points to SQ's CQ and
1504 		 */
1505 		cq_poll->cq_ids[CQ_RX] =
1506 			(qidx <  pf->hw.rx_queues) ? qidx : CINT_INVALID_CQ;
1507 		cq_poll->cq_ids[CQ_TX] = (qidx < pf->hw.tx_queues) ?
1508 				      qidx + pf->hw.rx_queues : CINT_INVALID_CQ;
1509 		cq_poll->dev = (void *)pf;
1510 		netif_napi_add(netdev, &cq_poll->napi,
1511 			       otx2_napi_handler, NAPI_POLL_WEIGHT);
1512 		napi_enable(&cq_poll->napi);
1513 	}
1514 
1515 	/* Set maximum frame size allowed in HW */
1516 	err = otx2_hw_set_mtu(pf, netdev->mtu);
1517 	if (err)
1518 		goto err_disable_napi;
1519 
1520 	/* Setup segmentation algorithms, if failed, clear offload capability */
1521 	otx2_setup_segmentation(pf);
1522 
1523 	/* Initialize RSS */
1524 	err = otx2_rss_init(pf);
1525 	if (err)
1526 		goto err_disable_napi;
1527 
1528 	/* Register Queue IRQ handlers */
1529 	vec = pf->hw.nix_msixoff + NIX_LF_QINT_VEC_START;
1530 	irq_name = &pf->hw.irq_name[vec * NAME_SIZE];
1531 
1532 	snprintf(irq_name, NAME_SIZE, "%s-qerr", pf->netdev->name);
1533 
1534 	err = request_irq(pci_irq_vector(pf->pdev, vec),
1535 			  otx2_q_intr_handler, 0, irq_name, pf);
1536 	if (err) {
1537 		dev_err(pf->dev,
1538 			"RVUPF%d: IRQ registration failed for QERR\n",
1539 			rvu_get_pf(pf->pcifunc));
1540 		goto err_disable_napi;
1541 	}
1542 
1543 	/* Enable QINT IRQ */
1544 	otx2_write64(pf, NIX_LF_QINTX_ENA_W1S(0), BIT_ULL(0));
1545 
1546 	/* Register CQ IRQ handlers */
1547 	vec = pf->hw.nix_msixoff + NIX_LF_CINT_VEC_START;
1548 	for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
1549 		irq_name = &pf->hw.irq_name[vec * NAME_SIZE];
1550 
1551 		snprintf(irq_name, NAME_SIZE, "%s-rxtx-%d", pf->netdev->name,
1552 			 qidx);
1553 
1554 		err = request_irq(pci_irq_vector(pf->pdev, vec),
1555 				  otx2_cq_intr_handler, 0, irq_name,
1556 				  &qset->napi[qidx]);
1557 		if (err) {
1558 			dev_err(pf->dev,
1559 				"RVUPF%d: IRQ registration failed for CQ%d\n",
1560 				rvu_get_pf(pf->pcifunc), qidx);
1561 			goto err_free_cints;
1562 		}
1563 		vec++;
1564 
1565 		otx2_config_irq_coalescing(pf, qidx);
1566 
1567 		/* Enable CQ IRQ */
1568 		otx2_write64(pf, NIX_LF_CINTX_INT(qidx), BIT_ULL(0));
1569 		otx2_write64(pf, NIX_LF_CINTX_ENA_W1S(qidx), BIT_ULL(0));
1570 	}
1571 
1572 	otx2_set_cints_affinity(pf);
1573 
1574 	/* When reinitializing enable time stamping if it is enabled before */
1575 	if (pf->flags & OTX2_FLAG_TX_TSTAMP_ENABLED) {
1576 		pf->flags &= ~OTX2_FLAG_TX_TSTAMP_ENABLED;
1577 		otx2_config_hw_tx_tstamp(pf, true);
1578 	}
1579 	if (pf->flags & OTX2_FLAG_RX_TSTAMP_ENABLED) {
1580 		pf->flags &= ~OTX2_FLAG_RX_TSTAMP_ENABLED;
1581 		otx2_config_hw_rx_tstamp(pf, true);
1582 	}
1583 
1584 	pf->flags &= ~OTX2_FLAG_INTF_DOWN;
1585 	/* 'intf_down' may be checked on any cpu */
1586 	smp_wmb();
1587 
1588 	/* we have already received link status notification */
1589 	if (pf->linfo.link_up && !(pf->pcifunc & RVU_PFVF_FUNC_MASK))
1590 		otx2_handle_link_event(pf);
1591 
1592 	/* Restore pause frame settings */
1593 	otx2_config_pause_frm(pf);
1594 
1595 	err = otx2_rxtx_enable(pf, true);
1596 	/* If a mbox communication error happens at this point then interface
1597 	 * will end up in a state such that it is in down state but hardware
1598 	 * mcam entries are enabled to receive the packets. Hence disable the
1599 	 * packet I/O.
1600 	 */
1601 	if (err == EIO)
1602 		goto err_disable_rxtx;
1603 	else if (err)
1604 		goto err_tx_stop_queues;
1605 
1606 	return 0;
1607 
1608 err_disable_rxtx:
1609 	otx2_rxtx_enable(pf, false);
1610 err_tx_stop_queues:
1611 	netif_tx_stop_all_queues(netdev);
1612 	netif_carrier_off(netdev);
1613 	pf->flags |= OTX2_FLAG_INTF_DOWN;
1614 err_free_cints:
1615 	otx2_free_cints(pf, qidx);
1616 	vec = pci_irq_vector(pf->pdev,
1617 			     pf->hw.nix_msixoff + NIX_LF_QINT_VEC_START);
1618 	otx2_write64(pf, NIX_LF_QINTX_ENA_W1C(0), BIT_ULL(0));
1619 	synchronize_irq(vec);
1620 	free_irq(vec, pf);
1621 err_disable_napi:
1622 	otx2_disable_napi(pf);
1623 	otx2_free_hw_resources(pf);
1624 err_free_mem:
1625 	kfree(qset->sq);
1626 	kfree(qset->cq);
1627 	kfree(qset->rq);
1628 	kfree(qset->napi);
1629 	return err;
1630 }
1631 EXPORT_SYMBOL(otx2_open);
1632 
otx2_stop(struct net_device *netdev)1633 int otx2_stop(struct net_device *netdev)
1634 {
1635 	struct otx2_nic *pf = netdev_priv(netdev);
1636 	struct otx2_cq_poll *cq_poll = NULL;
1637 	struct otx2_qset *qset = &pf->qset;
1638 	struct otx2_rss_info *rss;
1639 	int qidx, vec, wrk;
1640 
1641 	/* If the DOWN flag is set resources are already freed */
1642 	if (pf->flags & OTX2_FLAG_INTF_DOWN)
1643 		return 0;
1644 
1645 	netif_carrier_off(netdev);
1646 	netif_tx_stop_all_queues(netdev);
1647 
1648 	pf->flags |= OTX2_FLAG_INTF_DOWN;
1649 	/* 'intf_down' may be checked on any cpu */
1650 	smp_wmb();
1651 
1652 	/* First stop packet Rx/Tx */
1653 	otx2_rxtx_enable(pf, false);
1654 
1655 	/* Clear RSS enable flag */
1656 	rss = &pf->hw.rss_info;
1657 	rss->enable = false;
1658 
1659 	/* Cleanup Queue IRQ */
1660 	vec = pci_irq_vector(pf->pdev,
1661 			     pf->hw.nix_msixoff + NIX_LF_QINT_VEC_START);
1662 	otx2_write64(pf, NIX_LF_QINTX_ENA_W1C(0), BIT_ULL(0));
1663 	synchronize_irq(vec);
1664 	free_irq(vec, pf);
1665 
1666 	/* Cleanup CQ NAPI and IRQ */
1667 	vec = pf->hw.nix_msixoff + NIX_LF_CINT_VEC_START;
1668 	for (qidx = 0; qidx < pf->hw.cint_cnt; qidx++) {
1669 		/* Disable interrupt */
1670 		otx2_write64(pf, NIX_LF_CINTX_ENA_W1C(qidx), BIT_ULL(0));
1671 
1672 		synchronize_irq(pci_irq_vector(pf->pdev, vec));
1673 
1674 		cq_poll = &qset->napi[qidx];
1675 		napi_synchronize(&cq_poll->napi);
1676 		vec++;
1677 	}
1678 
1679 	netif_tx_disable(netdev);
1680 
1681 	otx2_free_hw_resources(pf);
1682 	otx2_free_cints(pf, pf->hw.cint_cnt);
1683 	otx2_disable_napi(pf);
1684 
1685 	for (qidx = 0; qidx < netdev->num_tx_queues; qidx++)
1686 		netdev_tx_reset_queue(netdev_get_tx_queue(netdev, qidx));
1687 
1688 	for (wrk = 0; wrk < pf->qset.cq_cnt; wrk++)
1689 		cancel_delayed_work_sync(&pf->refill_wrk[wrk].pool_refill_work);
1690 	devm_kfree(pf->dev, pf->refill_wrk);
1691 
1692 	kfree(qset->sq);
1693 	kfree(qset->cq);
1694 	kfree(qset->rq);
1695 	kfree(qset->napi);
1696 	/* Do not clear RQ/SQ ringsize settings */
1697 	memset((void *)qset + offsetof(struct otx2_qset, sqe_cnt), 0,
1698 	       sizeof(*qset) - offsetof(struct otx2_qset, sqe_cnt));
1699 	return 0;
1700 }
1701 EXPORT_SYMBOL(otx2_stop);
1702 
otx2_xmit(struct sk_buff *skb, struct net_device *netdev)1703 static netdev_tx_t otx2_xmit(struct sk_buff *skb, struct net_device *netdev)
1704 {
1705 	struct otx2_nic *pf = netdev_priv(netdev);
1706 	int qidx = skb_get_queue_mapping(skb);
1707 	struct otx2_snd_queue *sq;
1708 	struct netdev_queue *txq;
1709 
1710 	/* Check for minimum and maximum packet length */
1711 	if (skb->len <= ETH_HLEN ||
1712 	    (!skb_shinfo(skb)->gso_size && skb->len > pf->max_frs)) {
1713 		dev_kfree_skb(skb);
1714 		return NETDEV_TX_OK;
1715 	}
1716 
1717 	sq = &pf->qset.sq[qidx];
1718 	txq = netdev_get_tx_queue(netdev, qidx);
1719 
1720 	if (!otx2_sq_append_skb(netdev, sq, skb, qidx)) {
1721 		netif_tx_stop_queue(txq);
1722 
1723 		/* Check again, incase SQBs got freed up */
1724 		smp_mb();
1725 		if (((sq->num_sqbs - *sq->aura_fc_addr) * sq->sqe_per_sqb)
1726 							> sq->sqe_thresh)
1727 			netif_tx_wake_queue(txq);
1728 
1729 		return NETDEV_TX_BUSY;
1730 	}
1731 
1732 	return NETDEV_TX_OK;
1733 }
1734 
otx2_set_rx_mode(struct net_device *netdev)1735 static void otx2_set_rx_mode(struct net_device *netdev)
1736 {
1737 	struct otx2_nic *pf = netdev_priv(netdev);
1738 
1739 	queue_work(pf->otx2_wq, &pf->rx_mode_work);
1740 }
1741 
otx2_do_set_rx_mode(struct work_struct *work)1742 static void otx2_do_set_rx_mode(struct work_struct *work)
1743 {
1744 	struct otx2_nic *pf = container_of(work, struct otx2_nic, rx_mode_work);
1745 	struct net_device *netdev = pf->netdev;
1746 	struct nix_rx_mode *req;
1747 
1748 	if (!(netdev->flags & IFF_UP))
1749 		return;
1750 
1751 	mutex_lock(&pf->mbox.lock);
1752 	req = otx2_mbox_alloc_msg_nix_set_rx_mode(&pf->mbox);
1753 	if (!req) {
1754 		mutex_unlock(&pf->mbox.lock);
1755 		return;
1756 	}
1757 
1758 	req->mode = NIX_RX_MODE_UCAST;
1759 
1760 	/* We don't support MAC address filtering yet */
1761 	if (netdev->flags & IFF_PROMISC)
1762 		req->mode |= NIX_RX_MODE_PROMISC;
1763 	else if (netdev->flags & (IFF_ALLMULTI | IFF_MULTICAST))
1764 		req->mode |= NIX_RX_MODE_ALLMULTI;
1765 
1766 	otx2_sync_mbox_msg(&pf->mbox);
1767 	mutex_unlock(&pf->mbox.lock);
1768 }
1769 
otx2_set_features(struct net_device *netdev, netdev_features_t features)1770 static int otx2_set_features(struct net_device *netdev,
1771 			     netdev_features_t features)
1772 {
1773 	netdev_features_t changed = features ^ netdev->features;
1774 	struct otx2_nic *pf = netdev_priv(netdev);
1775 
1776 	if ((changed & NETIF_F_LOOPBACK) && netif_running(netdev))
1777 		return otx2_cgx_config_loopback(pf,
1778 						features & NETIF_F_LOOPBACK);
1779 	return 0;
1780 }
1781 
otx2_reset_task(struct work_struct *work)1782 static void otx2_reset_task(struct work_struct *work)
1783 {
1784 	struct otx2_nic *pf = container_of(work, struct otx2_nic, reset_task);
1785 
1786 	if (!netif_running(pf->netdev))
1787 		return;
1788 
1789 	rtnl_lock();
1790 	otx2_stop(pf->netdev);
1791 	pf->reset_count++;
1792 	otx2_open(pf->netdev);
1793 	netif_trans_update(pf->netdev);
1794 	rtnl_unlock();
1795 }
1796 
otx2_config_hw_rx_tstamp(struct otx2_nic *pfvf, bool enable)1797 static int otx2_config_hw_rx_tstamp(struct otx2_nic *pfvf, bool enable)
1798 {
1799 	struct msg_req *req;
1800 	int err;
1801 
1802 	if (pfvf->flags & OTX2_FLAG_RX_TSTAMP_ENABLED && enable)
1803 		return 0;
1804 
1805 	mutex_lock(&pfvf->mbox.lock);
1806 	if (enable)
1807 		req = otx2_mbox_alloc_msg_cgx_ptp_rx_enable(&pfvf->mbox);
1808 	else
1809 		req = otx2_mbox_alloc_msg_cgx_ptp_rx_disable(&pfvf->mbox);
1810 	if (!req) {
1811 		mutex_unlock(&pfvf->mbox.lock);
1812 		return -ENOMEM;
1813 	}
1814 
1815 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1816 	if (err) {
1817 		mutex_unlock(&pfvf->mbox.lock);
1818 		return err;
1819 	}
1820 
1821 	mutex_unlock(&pfvf->mbox.lock);
1822 	if (enable)
1823 		pfvf->flags |= OTX2_FLAG_RX_TSTAMP_ENABLED;
1824 	else
1825 		pfvf->flags &= ~OTX2_FLAG_RX_TSTAMP_ENABLED;
1826 	return 0;
1827 }
1828 
otx2_config_hw_tx_tstamp(struct otx2_nic *pfvf, bool enable)1829 static int otx2_config_hw_tx_tstamp(struct otx2_nic *pfvf, bool enable)
1830 {
1831 	struct msg_req *req;
1832 	int err;
1833 
1834 	if (pfvf->flags & OTX2_FLAG_TX_TSTAMP_ENABLED && enable)
1835 		return 0;
1836 
1837 	mutex_lock(&pfvf->mbox.lock);
1838 	if (enable)
1839 		req = otx2_mbox_alloc_msg_nix_lf_ptp_tx_enable(&pfvf->mbox);
1840 	else
1841 		req = otx2_mbox_alloc_msg_nix_lf_ptp_tx_disable(&pfvf->mbox);
1842 	if (!req) {
1843 		mutex_unlock(&pfvf->mbox.lock);
1844 		return -ENOMEM;
1845 	}
1846 
1847 	err = otx2_sync_mbox_msg(&pfvf->mbox);
1848 	if (err) {
1849 		mutex_unlock(&pfvf->mbox.lock);
1850 		return err;
1851 	}
1852 
1853 	mutex_unlock(&pfvf->mbox.lock);
1854 	if (enable)
1855 		pfvf->flags |= OTX2_FLAG_TX_TSTAMP_ENABLED;
1856 	else
1857 		pfvf->flags &= ~OTX2_FLAG_TX_TSTAMP_ENABLED;
1858 	return 0;
1859 }
1860 
otx2_config_hwtstamp(struct net_device *netdev, struct ifreq *ifr)1861 static int otx2_config_hwtstamp(struct net_device *netdev, struct ifreq *ifr)
1862 {
1863 	struct otx2_nic *pfvf = netdev_priv(netdev);
1864 	struct hwtstamp_config config;
1865 
1866 	if (!pfvf->ptp)
1867 		return -ENODEV;
1868 
1869 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1870 		return -EFAULT;
1871 
1872 	/* reserved for future extensions */
1873 	if (config.flags)
1874 		return -EINVAL;
1875 
1876 	switch (config.tx_type) {
1877 	case HWTSTAMP_TX_OFF:
1878 		otx2_config_hw_tx_tstamp(pfvf, false);
1879 		break;
1880 	case HWTSTAMP_TX_ON:
1881 		otx2_config_hw_tx_tstamp(pfvf, true);
1882 		break;
1883 	default:
1884 		return -ERANGE;
1885 	}
1886 
1887 	switch (config.rx_filter) {
1888 	case HWTSTAMP_FILTER_NONE:
1889 		otx2_config_hw_rx_tstamp(pfvf, false);
1890 		break;
1891 	case HWTSTAMP_FILTER_ALL:
1892 	case HWTSTAMP_FILTER_SOME:
1893 	case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
1894 	case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
1895 	case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
1896 	case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
1897 	case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
1898 	case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
1899 	case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1900 	case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
1901 	case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
1902 	case HWTSTAMP_FILTER_PTP_V2_EVENT:
1903 	case HWTSTAMP_FILTER_PTP_V2_SYNC:
1904 	case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
1905 		otx2_config_hw_rx_tstamp(pfvf, true);
1906 		config.rx_filter = HWTSTAMP_FILTER_ALL;
1907 		break;
1908 	default:
1909 		return -ERANGE;
1910 	}
1911 
1912 	memcpy(&pfvf->tstamp, &config, sizeof(config));
1913 
1914 	return copy_to_user(ifr->ifr_data, &config,
1915 			    sizeof(config)) ? -EFAULT : 0;
1916 }
1917 
otx2_ioctl(struct net_device *netdev, struct ifreq *req, int cmd)1918 static int otx2_ioctl(struct net_device *netdev, struct ifreq *req, int cmd)
1919 {
1920 	struct otx2_nic *pfvf = netdev_priv(netdev);
1921 	struct hwtstamp_config *cfg = &pfvf->tstamp;
1922 
1923 	switch (cmd) {
1924 	case SIOCSHWTSTAMP:
1925 		return otx2_config_hwtstamp(netdev, req);
1926 	case SIOCGHWTSTAMP:
1927 		return copy_to_user(req->ifr_data, cfg,
1928 				    sizeof(*cfg)) ? -EFAULT : 0;
1929 	default:
1930 		return -EOPNOTSUPP;
1931 	}
1932 }
1933 
1934 static const struct net_device_ops otx2_netdev_ops = {
1935 	.ndo_open		= otx2_open,
1936 	.ndo_stop		= otx2_stop,
1937 	.ndo_start_xmit		= otx2_xmit,
1938 	.ndo_set_mac_address    = otx2_set_mac_address,
1939 	.ndo_change_mtu		= otx2_change_mtu,
1940 	.ndo_set_rx_mode	= otx2_set_rx_mode,
1941 	.ndo_set_features	= otx2_set_features,
1942 	.ndo_tx_timeout		= otx2_tx_timeout,
1943 	.ndo_get_stats64	= otx2_get_stats64,
1944 	.ndo_do_ioctl		= otx2_ioctl,
1945 };
1946 
otx2_wq_init(struct otx2_nic *pf)1947 static int otx2_wq_init(struct otx2_nic *pf)
1948 {
1949 	pf->otx2_wq = create_singlethread_workqueue("otx2_wq");
1950 	if (!pf->otx2_wq)
1951 		return -ENOMEM;
1952 
1953 	INIT_WORK(&pf->rx_mode_work, otx2_do_set_rx_mode);
1954 	INIT_WORK(&pf->reset_task, otx2_reset_task);
1955 	return 0;
1956 }
1957 
otx2_check_pf_usable(struct otx2_nic *nic)1958 static int otx2_check_pf_usable(struct otx2_nic *nic)
1959 {
1960 	u64 rev;
1961 
1962 	rev = otx2_read64(nic, RVU_PF_BLOCK_ADDRX_DISC(BLKADDR_RVUM));
1963 	rev = (rev >> 12) & 0xFF;
1964 	/* Check if AF has setup revision for RVUM block,
1965 	 * otherwise this driver probe should be deferred
1966 	 * until AF driver comes up.
1967 	 */
1968 	if (!rev) {
1969 		dev_warn(nic->dev,
1970 			 "AF is not initialized, deferring probe\n");
1971 		return -EPROBE_DEFER;
1972 	}
1973 	return 0;
1974 }
1975 
otx2_realloc_msix_vectors(struct otx2_nic *pf)1976 static int otx2_realloc_msix_vectors(struct otx2_nic *pf)
1977 {
1978 	struct otx2_hw *hw = &pf->hw;
1979 	int num_vec, err;
1980 
1981 	/* NPA interrupts are inot registered, so alloc only
1982 	 * upto NIX vector offset.
1983 	 */
1984 	num_vec = hw->nix_msixoff;
1985 	num_vec += NIX_LF_CINT_VEC_START + hw->max_queues;
1986 
1987 	otx2_disable_mbox_intr(pf);
1988 	pci_free_irq_vectors(hw->pdev);
1989 	err = pci_alloc_irq_vectors(hw->pdev, num_vec, num_vec, PCI_IRQ_MSIX);
1990 	if (err < 0) {
1991 		dev_err(pf->dev, "%s: Failed to realloc %d IRQ vectors\n",
1992 			__func__, num_vec);
1993 		return err;
1994 	}
1995 
1996 	return otx2_register_mbox_intr(pf, false);
1997 }
1998 
otx2_probe(struct pci_dev *pdev, const struct pci_device_id *id)1999 static int otx2_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2000 {
2001 	struct device *dev = &pdev->dev;
2002 	struct net_device *netdev;
2003 	struct otx2_nic *pf;
2004 	struct otx2_hw *hw;
2005 	int err, qcount;
2006 	int num_vec;
2007 
2008 	err = pcim_enable_device(pdev);
2009 	if (err) {
2010 		dev_err(dev, "Failed to enable PCI device\n");
2011 		return err;
2012 	}
2013 
2014 	err = pci_request_regions(pdev, DRV_NAME);
2015 	if (err) {
2016 		dev_err(dev, "PCI request regions failed 0x%x\n", err);
2017 		return err;
2018 	}
2019 
2020 	err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48));
2021 	if (err) {
2022 		dev_err(dev, "DMA mask config failed, abort\n");
2023 		goto err_release_regions;
2024 	}
2025 
2026 	pci_set_master(pdev);
2027 
2028 	/* Set number of queues */
2029 	qcount = min_t(int, num_online_cpus(), OTX2_MAX_CQ_CNT);
2030 
2031 	netdev = alloc_etherdev_mqs(sizeof(*pf), qcount, qcount);
2032 	if (!netdev) {
2033 		err = -ENOMEM;
2034 		goto err_release_regions;
2035 	}
2036 
2037 	pci_set_drvdata(pdev, netdev);
2038 	SET_NETDEV_DEV(netdev, &pdev->dev);
2039 	pf = netdev_priv(netdev);
2040 	pf->netdev = netdev;
2041 	pf->pdev = pdev;
2042 	pf->dev = dev;
2043 	pf->total_vfs = pci_sriov_get_totalvfs(pdev);
2044 	pf->flags |= OTX2_FLAG_INTF_DOWN;
2045 
2046 	hw = &pf->hw;
2047 	hw->pdev = pdev;
2048 	hw->rx_queues = qcount;
2049 	hw->tx_queues = qcount;
2050 	hw->max_queues = qcount;
2051 
2052 	num_vec = pci_msix_vec_count(pdev);
2053 	hw->irq_name = devm_kmalloc_array(&hw->pdev->dev, num_vec, NAME_SIZE,
2054 					  GFP_KERNEL);
2055 	if (!hw->irq_name) {
2056 		err = -ENOMEM;
2057 		goto err_free_netdev;
2058 	}
2059 
2060 	hw->affinity_mask = devm_kcalloc(&hw->pdev->dev, num_vec,
2061 					 sizeof(cpumask_var_t), GFP_KERNEL);
2062 	if (!hw->affinity_mask) {
2063 		err = -ENOMEM;
2064 		goto err_free_netdev;
2065 	}
2066 
2067 	/* Map CSRs */
2068 	pf->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
2069 	if (!pf->reg_base) {
2070 		dev_err(dev, "Unable to map physical function CSRs, aborting\n");
2071 		err = -ENOMEM;
2072 		goto err_free_netdev;
2073 	}
2074 
2075 	err = otx2_check_pf_usable(pf);
2076 	if (err)
2077 		goto err_free_netdev;
2078 
2079 	err = pci_alloc_irq_vectors(hw->pdev, RVU_PF_INT_VEC_CNT,
2080 				    RVU_PF_INT_VEC_CNT, PCI_IRQ_MSIX);
2081 	if (err < 0) {
2082 		dev_err(dev, "%s: Failed to alloc %d IRQ vectors\n",
2083 			__func__, num_vec);
2084 		goto err_free_netdev;
2085 	}
2086 
2087 	/* Init PF <=> AF mailbox stuff */
2088 	err = otx2_pfaf_mbox_init(pf);
2089 	if (err)
2090 		goto err_free_irq_vectors;
2091 
2092 	/* Register mailbox interrupt */
2093 	err = otx2_register_mbox_intr(pf, true);
2094 	if (err)
2095 		goto err_mbox_destroy;
2096 
2097 	/* Request AF to attach NPA and NIX LFs to this PF.
2098 	 * NIX and NPA LFs are needed for this PF to function as a NIC.
2099 	 */
2100 	err = otx2_attach_npa_nix(pf);
2101 	if (err)
2102 		goto err_disable_mbox_intr;
2103 
2104 	err = otx2_realloc_msix_vectors(pf);
2105 	if (err)
2106 		goto err_detach_rsrc;
2107 
2108 	err = otx2_set_real_num_queues(netdev, hw->tx_queues, hw->rx_queues);
2109 	if (err)
2110 		goto err_detach_rsrc;
2111 
2112 	otx2_setup_dev_hw_settings(pf);
2113 
2114 	/* Assign default mac address */
2115 	otx2_get_mac_from_af(netdev);
2116 
2117 	/* Don't check for error.  Proceed without ptp */
2118 	otx2_ptp_init(pf);
2119 
2120 	/* NPA's pool is a stack to which SW frees buffer pointers via Aura.
2121 	 * HW allocates buffer pointer from stack and uses it for DMA'ing
2122 	 * ingress packet. In some scenarios HW can free back allocated buffer
2123 	 * pointers to pool. This makes it impossible for SW to maintain a
2124 	 * parallel list where physical addresses of buffer pointers (IOVAs)
2125 	 * given to HW can be saved for later reference.
2126 	 *
2127 	 * So the only way to convert Rx packet's buffer address is to use
2128 	 * IOMMU's iova_to_phys() handler which translates the address by
2129 	 * walking through the translation tables.
2130 	 */
2131 	pf->iommu_domain = iommu_get_domain_for_dev(dev);
2132 
2133 	netdev->hw_features = (NETIF_F_RXCSUM | NETIF_F_IP_CSUM |
2134 			       NETIF_F_IPV6_CSUM | NETIF_F_RXHASH |
2135 			       NETIF_F_SG | NETIF_F_TSO | NETIF_F_TSO6 |
2136 			       NETIF_F_GSO_UDP_L4);
2137 	netdev->features |= netdev->hw_features;
2138 
2139 	netdev->hw_features |= NETIF_F_LOOPBACK | NETIF_F_RXALL;
2140 
2141 	netdev->gso_max_segs = OTX2_MAX_GSO_SEGS;
2142 	netdev->watchdog_timeo = OTX2_TX_TIMEOUT;
2143 
2144 	netdev->netdev_ops = &otx2_netdev_ops;
2145 
2146 	/* MTU range: 64 - 9190 */
2147 	netdev->min_mtu = OTX2_MIN_MTU;
2148 	netdev->max_mtu = OTX2_MAX_MTU;
2149 
2150 	err = register_netdev(netdev);
2151 	if (err) {
2152 		dev_err(dev, "Failed to register netdevice\n");
2153 		goto err_ptp_destroy;
2154 	}
2155 
2156 	err = otx2_wq_init(pf);
2157 	if (err)
2158 		goto err_unreg_netdev;
2159 
2160 	otx2_set_ethtool_ops(netdev);
2161 
2162 	/* Enable link notifications */
2163 	otx2_cgx_config_linkevents(pf, true);
2164 
2165 	/* Enable pause frames by default */
2166 	pf->flags |= OTX2_FLAG_RX_PAUSE_ENABLED;
2167 	pf->flags |= OTX2_FLAG_TX_PAUSE_ENABLED;
2168 
2169 	return 0;
2170 
2171 err_unreg_netdev:
2172 	unregister_netdev(netdev);
2173 err_ptp_destroy:
2174 	otx2_ptp_destroy(pf);
2175 err_detach_rsrc:
2176 	otx2_detach_resources(&pf->mbox);
2177 err_disable_mbox_intr:
2178 	otx2_disable_mbox_intr(pf);
2179 err_mbox_destroy:
2180 	otx2_pfaf_mbox_destroy(pf);
2181 err_free_irq_vectors:
2182 	pci_free_irq_vectors(hw->pdev);
2183 err_free_netdev:
2184 	pci_set_drvdata(pdev, NULL);
2185 	free_netdev(netdev);
2186 err_release_regions:
2187 	pci_release_regions(pdev);
2188 	return err;
2189 }
2190 
otx2_vf_link_event_task(struct work_struct *work)2191 static void otx2_vf_link_event_task(struct work_struct *work)
2192 {
2193 	struct otx2_vf_config *config;
2194 	struct cgx_link_info_msg *req;
2195 	struct mbox_msghdr *msghdr;
2196 	struct otx2_nic *pf;
2197 	int vf_idx;
2198 
2199 	config = container_of(work, struct otx2_vf_config,
2200 			      link_event_work.work);
2201 	vf_idx = config - config->pf->vf_configs;
2202 	pf = config->pf;
2203 
2204 	msghdr = otx2_mbox_alloc_msg_rsp(&pf->mbox_pfvf[0].mbox_up, vf_idx,
2205 					 sizeof(*req), sizeof(struct msg_rsp));
2206 	if (!msghdr) {
2207 		dev_err(pf->dev, "Failed to create VF%d link event\n", vf_idx);
2208 		return;
2209 	}
2210 
2211 	req = (struct cgx_link_info_msg *)msghdr;
2212 	req->hdr.id = MBOX_MSG_CGX_LINK_EVENT;
2213 	req->hdr.sig = OTX2_MBOX_REQ_SIG;
2214 	memcpy(&req->link_info, &pf->linfo, sizeof(req->link_info));
2215 
2216 	otx2_sync_mbox_up_msg(&pf->mbox_pfvf[0], vf_idx);
2217 }
2218 
otx2_sriov_enable(struct pci_dev *pdev, int numvfs)2219 static int otx2_sriov_enable(struct pci_dev *pdev, int numvfs)
2220 {
2221 	struct net_device *netdev = pci_get_drvdata(pdev);
2222 	struct otx2_nic *pf = netdev_priv(netdev);
2223 	int ret, i;
2224 
2225 	/* Init PF <=> VF mailbox stuff */
2226 	ret = otx2_pfvf_mbox_init(pf, numvfs);
2227 	if (ret)
2228 		return ret;
2229 
2230 	ret = otx2_register_pfvf_mbox_intr(pf, numvfs);
2231 	if (ret)
2232 		goto free_mbox;
2233 
2234 	pf->vf_configs = kcalloc(numvfs, sizeof(struct otx2_vf_config),
2235 				 GFP_KERNEL);
2236 	if (!pf->vf_configs) {
2237 		ret = -ENOMEM;
2238 		goto free_intr;
2239 	}
2240 
2241 	for (i = 0; i < numvfs; i++) {
2242 		pf->vf_configs[i].pf = pf;
2243 		pf->vf_configs[i].intf_down = true;
2244 		INIT_DELAYED_WORK(&pf->vf_configs[i].link_event_work,
2245 				  otx2_vf_link_event_task);
2246 	}
2247 
2248 	ret = otx2_pf_flr_init(pf, numvfs);
2249 	if (ret)
2250 		goto free_configs;
2251 
2252 	ret = otx2_register_flr_me_intr(pf, numvfs);
2253 	if (ret)
2254 		goto free_flr;
2255 
2256 	ret = pci_enable_sriov(pdev, numvfs);
2257 	if (ret)
2258 		goto free_flr_intr;
2259 
2260 	return numvfs;
2261 free_flr_intr:
2262 	otx2_disable_flr_me_intr(pf);
2263 free_flr:
2264 	otx2_flr_wq_destroy(pf);
2265 free_configs:
2266 	kfree(pf->vf_configs);
2267 free_intr:
2268 	otx2_disable_pfvf_mbox_intr(pf, numvfs);
2269 free_mbox:
2270 	otx2_pfvf_mbox_destroy(pf);
2271 	return ret;
2272 }
2273 
otx2_sriov_disable(struct pci_dev *pdev)2274 static int otx2_sriov_disable(struct pci_dev *pdev)
2275 {
2276 	struct net_device *netdev = pci_get_drvdata(pdev);
2277 	struct otx2_nic *pf = netdev_priv(netdev);
2278 	int numvfs = pci_num_vf(pdev);
2279 	int i;
2280 
2281 	if (!numvfs)
2282 		return 0;
2283 
2284 	pci_disable_sriov(pdev);
2285 
2286 	for (i = 0; i < pci_num_vf(pdev); i++)
2287 		cancel_delayed_work_sync(&pf->vf_configs[i].link_event_work);
2288 	kfree(pf->vf_configs);
2289 
2290 	otx2_disable_flr_me_intr(pf);
2291 	otx2_flr_wq_destroy(pf);
2292 	otx2_disable_pfvf_mbox_intr(pf, numvfs);
2293 	otx2_pfvf_mbox_destroy(pf);
2294 
2295 	return 0;
2296 }
2297 
otx2_sriov_configure(struct pci_dev *pdev, int numvfs)2298 static int otx2_sriov_configure(struct pci_dev *pdev, int numvfs)
2299 {
2300 	if (numvfs == 0)
2301 		return otx2_sriov_disable(pdev);
2302 	else
2303 		return otx2_sriov_enable(pdev, numvfs);
2304 }
2305 
otx2_remove(struct pci_dev *pdev)2306 static void otx2_remove(struct pci_dev *pdev)
2307 {
2308 	struct net_device *netdev = pci_get_drvdata(pdev);
2309 	struct otx2_nic *pf;
2310 
2311 	if (!netdev)
2312 		return;
2313 
2314 	pf = netdev_priv(netdev);
2315 
2316 	if (pf->flags & OTX2_FLAG_TX_TSTAMP_ENABLED)
2317 		otx2_config_hw_tx_tstamp(pf, false);
2318 	if (pf->flags & OTX2_FLAG_RX_TSTAMP_ENABLED)
2319 		otx2_config_hw_rx_tstamp(pf, false);
2320 
2321 	cancel_work_sync(&pf->reset_task);
2322 	/* Disable link notifications */
2323 	otx2_cgx_config_linkevents(pf, false);
2324 
2325 	unregister_netdev(netdev);
2326 	otx2_sriov_disable(pf->pdev);
2327 	if (pf->otx2_wq)
2328 		destroy_workqueue(pf->otx2_wq);
2329 
2330 	otx2_ptp_destroy(pf);
2331 	otx2_detach_resources(&pf->mbox);
2332 	otx2_disable_mbox_intr(pf);
2333 	otx2_pfaf_mbox_destroy(pf);
2334 	pci_free_irq_vectors(pf->pdev);
2335 	pci_set_drvdata(pdev, NULL);
2336 	free_netdev(netdev);
2337 
2338 	pci_release_regions(pdev);
2339 }
2340 
2341 static struct pci_driver otx2_pf_driver = {
2342 	.name = DRV_NAME,
2343 	.id_table = otx2_pf_id_table,
2344 	.probe = otx2_probe,
2345 	.shutdown = otx2_remove,
2346 	.remove = otx2_remove,
2347 	.sriov_configure = otx2_sriov_configure
2348 };
2349 
otx2_rvupf_init_module(void)2350 static int __init otx2_rvupf_init_module(void)
2351 {
2352 	pr_info("%s: %s\n", DRV_NAME, DRV_STRING);
2353 
2354 	return pci_register_driver(&otx2_pf_driver);
2355 }
2356 
otx2_rvupf_cleanup_module(void)2357 static void __exit otx2_rvupf_cleanup_module(void)
2358 {
2359 	pci_unregister_driver(&otx2_pf_driver);
2360 }
2361 
2362 module_init(otx2_rvupf_init_module);
2363 module_exit(otx2_rvupf_cleanup_module);
2364