xref: /openbmc/linux/drivers/net/ethernet/marvell/octeon_ep/octep_main.c (revision e1f4ecab19338ec7079830e8700e4869f991fd45)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Marvell Octeon EP (EndPoint) Ethernet Driver
3  *
4  * Copyright (C) 2020 Marvell.
5  *
6  */
7 
8 #include <linux/types.h>
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/aer.h>
12 #include <linux/netdevice.h>
13 #include <linux/etherdevice.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/vmalloc.h>
16 
17 #include "octep_config.h"
18 #include "octep_main.h"
19 #include "octep_ctrl_net.h"
20 
21 struct workqueue_struct *octep_wq;
22 
23 /* Supported Devices */
24 static const struct pci_device_id octep_pci_id_tbl[] = {
25 	{PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, OCTEP_PCI_DEVICE_ID_CN93_PF)},
26 	{PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, OCTEP_PCI_DEVICE_ID_CNF95N_PF)},
27 	{0, },
28 };
29 MODULE_DEVICE_TABLE(pci, octep_pci_id_tbl);
30 
31 MODULE_AUTHOR("Veerasenareddy Burru <vburru@marvell.com>");
32 MODULE_DESCRIPTION(OCTEP_DRV_STRING);
33 MODULE_LICENSE("GPL");
34 
35 /**
36  * octep_alloc_ioq_vectors() - Allocate Tx/Rx Queue interrupt info.
37  *
38  * @oct: Octeon device private data structure.
39  *
40  * Allocate resources to hold per Tx/Rx queue interrupt info.
41  * This is the information passed to interrupt handler, from which napi poll
42  * is scheduled and includes quick access to private data of Tx/Rx queue
43  * corresponding to the interrupt being handled.
44  *
45  * Return: 0, on successful allocation of resources for all queue interrupts.
46  *         -1, if failed to allocate any resource.
47  */
48 static int octep_alloc_ioq_vectors(struct octep_device *oct)
49 {
50 	int i;
51 	struct octep_ioq_vector *ioq_vector;
52 
53 	for (i = 0; i < oct->num_oqs; i++) {
54 		oct->ioq_vector[i] = vzalloc(sizeof(*oct->ioq_vector[i]));
55 		if (!oct->ioq_vector[i])
56 			goto free_ioq_vector;
57 
58 		ioq_vector = oct->ioq_vector[i];
59 		ioq_vector->iq = oct->iq[i];
60 		ioq_vector->oq = oct->oq[i];
61 		ioq_vector->octep_dev = oct;
62 	}
63 
64 	dev_info(&oct->pdev->dev, "Allocated %d IOQ vectors\n", oct->num_oqs);
65 	return 0;
66 
67 free_ioq_vector:
68 	while (i) {
69 		i--;
70 		vfree(oct->ioq_vector[i]);
71 		oct->ioq_vector[i] = NULL;
72 	}
73 	return -1;
74 }
75 
76 /**
77  * octep_free_ioq_vectors() - Free Tx/Rx Queue interrupt vector info.
78  *
79  * @oct: Octeon device private data structure.
80  */
81 static void octep_free_ioq_vectors(struct octep_device *oct)
82 {
83 	int i;
84 
85 	for (i = 0; i < oct->num_oqs; i++) {
86 		if (oct->ioq_vector[i]) {
87 			vfree(oct->ioq_vector[i]);
88 			oct->ioq_vector[i] = NULL;
89 		}
90 	}
91 	netdev_info(oct->netdev, "Freed IOQ Vectors\n");
92 }
93 
94 /**
95  * octep_enable_msix_range() - enable MSI-x interrupts.
96  *
97  * @oct: Octeon device private data structure.
98  *
99  * Allocate and enable all MSI-x interrupts (queue and non-queue interrupts)
100  * for the Octeon device.
101  *
102  * Return: 0, on successfully enabling all MSI-x interrupts.
103  *         -1, if failed to enable any MSI-x interrupt.
104  */
105 static int octep_enable_msix_range(struct octep_device *oct)
106 {
107 	int num_msix, msix_allocated;
108 	int i;
109 
110 	/* Generic interrupts apart from input/output queues */
111 	num_msix = oct->num_oqs + CFG_GET_NON_IOQ_MSIX(oct->conf);
112 	oct->msix_entries = kcalloc(num_msix,
113 				    sizeof(struct msix_entry), GFP_KERNEL);
114 	if (!oct->msix_entries)
115 		goto msix_alloc_err;
116 
117 	for (i = 0; i < num_msix; i++)
118 		oct->msix_entries[i].entry = i;
119 
120 	msix_allocated = pci_enable_msix_range(oct->pdev, oct->msix_entries,
121 					       num_msix, num_msix);
122 	if (msix_allocated != num_msix) {
123 		dev_err(&oct->pdev->dev,
124 			"Failed to enable %d msix irqs; got only %d\n",
125 			num_msix, msix_allocated);
126 		goto enable_msix_err;
127 	}
128 	oct->num_irqs = msix_allocated;
129 	dev_info(&oct->pdev->dev, "MSI-X enabled successfully\n");
130 
131 	return 0;
132 
133 enable_msix_err:
134 	if (msix_allocated > 0)
135 		pci_disable_msix(oct->pdev);
136 	kfree(oct->msix_entries);
137 	oct->msix_entries = NULL;
138 msix_alloc_err:
139 	return -1;
140 }
141 
142 /**
143  * octep_disable_msix() - disable MSI-x interrupts.
144  *
145  * @oct: Octeon device private data structure.
146  *
147  * Disable MSI-x on the Octeon device.
148  */
149 static void octep_disable_msix(struct octep_device *oct)
150 {
151 	pci_disable_msix(oct->pdev);
152 	kfree(oct->msix_entries);
153 	oct->msix_entries = NULL;
154 	dev_info(&oct->pdev->dev, "Disabled MSI-X\n");
155 }
156 
157 /**
158  * octep_non_ioq_intr_handler() - common handler for all generic interrupts.
159  *
160  * @irq: Interrupt number.
161  * @data: interrupt data.
162  *
163  * this is common handler for all non-queue (generic) interrupts.
164  */
165 static irqreturn_t octep_non_ioq_intr_handler(int irq, void *data)
166 {
167 	struct octep_device *oct = data;
168 
169 	return oct->hw_ops.non_ioq_intr_handler(oct);
170 }
171 
172 /**
173  * octep_ioq_intr_handler() - handler for all Tx/Rx queue interrupts.
174  *
175  * @irq: Interrupt number.
176  * @data: interrupt data contains pointers to Tx/Rx queue private data
177  *         and correspong NAPI context.
178  *
179  * this is common handler for all non-queue (generic) interrupts.
180  */
181 static irqreturn_t octep_ioq_intr_handler(int irq, void *data)
182 {
183 	struct octep_ioq_vector *ioq_vector = data;
184 	struct octep_device *oct = ioq_vector->octep_dev;
185 
186 	return oct->hw_ops.ioq_intr_handler(ioq_vector);
187 }
188 
189 /**
190  * octep_request_irqs() - Register interrupt handlers.
191  *
192  * @oct: Octeon device private data structure.
193  *
194  * Register handlers for all queue and non-queue interrupts.
195  *
196  * Return: 0, on successful registration of all interrupt handlers.
197  *         -1, on any error.
198  */
199 static int octep_request_irqs(struct octep_device *oct)
200 {
201 	struct net_device *netdev = oct->netdev;
202 	struct octep_ioq_vector *ioq_vector;
203 	struct msix_entry *msix_entry;
204 	char **non_ioq_msix_names;
205 	int num_non_ioq_msix;
206 	int ret, i, j;
207 
208 	num_non_ioq_msix = CFG_GET_NON_IOQ_MSIX(oct->conf);
209 	non_ioq_msix_names = CFG_GET_NON_IOQ_MSIX_NAMES(oct->conf);
210 
211 	oct->non_ioq_irq_names = kcalloc(num_non_ioq_msix,
212 					 OCTEP_MSIX_NAME_SIZE, GFP_KERNEL);
213 	if (!oct->non_ioq_irq_names)
214 		goto alloc_err;
215 
216 	/* First few MSI-X interrupts are non-queue interrupts */
217 	for (i = 0; i < num_non_ioq_msix; i++) {
218 		char *irq_name;
219 
220 		irq_name = &oct->non_ioq_irq_names[i * OCTEP_MSIX_NAME_SIZE];
221 		msix_entry = &oct->msix_entries[i];
222 
223 		snprintf(irq_name, OCTEP_MSIX_NAME_SIZE,
224 			 "%s-%s", netdev->name, non_ioq_msix_names[i]);
225 		ret = request_irq(msix_entry->vector,
226 				  octep_non_ioq_intr_handler, 0,
227 				  irq_name, oct);
228 		if (ret) {
229 			netdev_err(netdev,
230 				   "request_irq failed for %s; err=%d",
231 				   irq_name, ret);
232 			goto non_ioq_irq_err;
233 		}
234 	}
235 
236 	/* Request IRQs for Tx/Rx queues */
237 	for (j = 0; j < oct->num_oqs; j++) {
238 		ioq_vector = oct->ioq_vector[j];
239 		msix_entry = &oct->msix_entries[j + num_non_ioq_msix];
240 
241 		snprintf(ioq_vector->name, sizeof(ioq_vector->name),
242 			 "%s-q%d", netdev->name, j);
243 		ret = request_irq(msix_entry->vector,
244 				  octep_ioq_intr_handler, 0,
245 				  ioq_vector->name, ioq_vector);
246 		if (ret) {
247 			netdev_err(netdev,
248 				   "request_irq failed for Q-%d; err=%d",
249 				   j, ret);
250 			goto ioq_irq_err;
251 		}
252 
253 		cpumask_set_cpu(j % num_online_cpus(),
254 				&ioq_vector->affinity_mask);
255 		irq_set_affinity_hint(msix_entry->vector,
256 				      &ioq_vector->affinity_mask);
257 	}
258 
259 	return 0;
260 ioq_irq_err:
261 	while (j) {
262 		--j;
263 		ioq_vector = oct->ioq_vector[j];
264 		msix_entry = &oct->msix_entries[j + num_non_ioq_msix];
265 
266 		irq_set_affinity_hint(msix_entry->vector, NULL);
267 		free_irq(msix_entry->vector, ioq_vector);
268 	}
269 non_ioq_irq_err:
270 	while (i) {
271 		--i;
272 		free_irq(oct->msix_entries[i].vector, oct);
273 	}
274 	kfree(oct->non_ioq_irq_names);
275 	oct->non_ioq_irq_names = NULL;
276 alloc_err:
277 	return -1;
278 }
279 
280 /**
281  * octep_free_irqs() - free all registered interrupts.
282  *
283  * @oct: Octeon device private data structure.
284  *
285  * Free all queue and non-queue interrupts of the Octeon device.
286  */
287 static void octep_free_irqs(struct octep_device *oct)
288 {
289 	int i;
290 
291 	/* First few MSI-X interrupts are non queue interrupts; free them */
292 	for (i = 0; i < CFG_GET_NON_IOQ_MSIX(oct->conf); i++)
293 		free_irq(oct->msix_entries[i].vector, oct);
294 	kfree(oct->non_ioq_irq_names);
295 
296 	/* Free IRQs for Input/Output (Tx/Rx) queues */
297 	for (i = CFG_GET_NON_IOQ_MSIX(oct->conf); i < oct->num_irqs; i++) {
298 		irq_set_affinity_hint(oct->msix_entries[i].vector, NULL);
299 		free_irq(oct->msix_entries[i].vector,
300 			 oct->ioq_vector[i - CFG_GET_NON_IOQ_MSIX(oct->conf)]);
301 	}
302 	netdev_info(oct->netdev, "IRQs freed\n");
303 }
304 
305 /**
306  * octep_setup_irqs() - setup interrupts for the Octeon device.
307  *
308  * @oct: Octeon device private data structure.
309  *
310  * Allocate data structures to hold per interrupt information, allocate/enable
311  * MSI-x interrupt and register interrupt handlers.
312  *
313  * Return: 0, on successful allocation and registration of all interrupts.
314  *         -1, on any error.
315  */
316 static int octep_setup_irqs(struct octep_device *oct)
317 {
318 	if (octep_alloc_ioq_vectors(oct))
319 		goto ioq_vector_err;
320 
321 	if (octep_enable_msix_range(oct))
322 		goto enable_msix_err;
323 
324 	if (octep_request_irqs(oct))
325 		goto request_irq_err;
326 
327 	return 0;
328 
329 request_irq_err:
330 	octep_disable_msix(oct);
331 enable_msix_err:
332 	octep_free_ioq_vectors(oct);
333 ioq_vector_err:
334 	return -1;
335 }
336 
337 /**
338  * octep_clean_irqs() - free all interrupts and its resources.
339  *
340  * @oct: Octeon device private data structure.
341  */
342 static void octep_clean_irqs(struct octep_device *oct)
343 {
344 	octep_free_irqs(oct);
345 	octep_disable_msix(oct);
346 	octep_free_ioq_vectors(oct);
347 }
348 
349 /**
350  * octep_enable_ioq_irq() - Enable MSI-x interrupt of a Tx/Rx queue.
351  *
352  * @iq: Octeon Tx queue data structure.
353  * @oq: Octeon Rx queue data structure.
354  */
355 static void octep_enable_ioq_irq(struct octep_iq *iq, struct octep_oq *oq)
356 {
357 	u32 pkts_pend = oq->pkts_pending;
358 
359 	netdev_dbg(iq->netdev, "enabling intr for Q-%u\n", iq->q_no);
360 	if (iq->pkts_processed) {
361 		writel(iq->pkts_processed, iq->inst_cnt_reg);
362 		iq->pkt_in_done -= iq->pkts_processed;
363 		iq->pkts_processed = 0;
364 	}
365 	if (oq->last_pkt_count - pkts_pend) {
366 		writel(oq->last_pkt_count - pkts_pend, oq->pkts_sent_reg);
367 		oq->last_pkt_count = pkts_pend;
368 	}
369 
370 	/* Flush the previous wrties before writing to RESEND bit */
371 	wmb();
372 	writeq(1UL << OCTEP_OQ_INTR_RESEND_BIT, oq->pkts_sent_reg);
373 	writeq(1UL << OCTEP_IQ_INTR_RESEND_BIT, iq->inst_cnt_reg);
374 }
375 
376 /**
377  * octep_napi_poll() - NAPI poll function for Tx/Rx.
378  *
379  * @napi: pointer to napi context.
380  * @budget: max number of packets to be processed in single invocation.
381  */
382 static int octep_napi_poll(struct napi_struct *napi, int budget)
383 {
384 	struct octep_ioq_vector *ioq_vector =
385 		container_of(napi, struct octep_ioq_vector, napi);
386 	u32 tx_pending, rx_done;
387 
388 	tx_pending = octep_iq_process_completions(ioq_vector->iq, budget);
389 	rx_done = octep_oq_process_rx(ioq_vector->oq, budget);
390 
391 	/* need more polling if tx completion processing is still pending or
392 	 * processed at least 'budget' number of rx packets.
393 	 */
394 	if (tx_pending || rx_done >= budget)
395 		return budget;
396 
397 	napi_complete(napi);
398 	octep_enable_ioq_irq(ioq_vector->iq, ioq_vector->oq);
399 	return rx_done;
400 }
401 
402 /**
403  * octep_napi_add() - Add NAPI poll for all Tx/Rx queues.
404  *
405  * @oct: Octeon device private data structure.
406  */
407 static void octep_napi_add(struct octep_device *oct)
408 {
409 	int i;
410 
411 	for (i = 0; i < oct->num_oqs; i++) {
412 		netdev_dbg(oct->netdev, "Adding NAPI on Q-%d\n", i);
413 		netif_napi_add(oct->netdev, &oct->ioq_vector[i]->napi,
414 			       octep_napi_poll);
415 		oct->oq[i]->napi = &oct->ioq_vector[i]->napi;
416 	}
417 }
418 
419 /**
420  * octep_napi_delete() - delete NAPI poll callback for all Tx/Rx queues.
421  *
422  * @oct: Octeon device private data structure.
423  */
424 static void octep_napi_delete(struct octep_device *oct)
425 {
426 	int i;
427 
428 	for (i = 0; i < oct->num_oqs; i++) {
429 		netdev_dbg(oct->netdev, "Deleting NAPI on Q-%d\n", i);
430 		netif_napi_del(&oct->ioq_vector[i]->napi);
431 		oct->oq[i]->napi = NULL;
432 	}
433 }
434 
435 /**
436  * octep_napi_enable() - enable NAPI for all Tx/Rx queues.
437  *
438  * @oct: Octeon device private data structure.
439  */
440 static void octep_napi_enable(struct octep_device *oct)
441 {
442 	int i;
443 
444 	for (i = 0; i < oct->num_oqs; i++) {
445 		netdev_dbg(oct->netdev, "Enabling NAPI on Q-%d\n", i);
446 		napi_enable(&oct->ioq_vector[i]->napi);
447 	}
448 }
449 
450 /**
451  * octep_napi_disable() - disable NAPI for all Tx/Rx queues.
452  *
453  * @oct: Octeon device private data structure.
454  */
455 static void octep_napi_disable(struct octep_device *oct)
456 {
457 	int i;
458 
459 	for (i = 0; i < oct->num_oqs; i++) {
460 		netdev_dbg(oct->netdev, "Disabling NAPI on Q-%d\n", i);
461 		napi_disable(&oct->ioq_vector[i]->napi);
462 	}
463 }
464 
465 static void octep_link_up(struct net_device *netdev)
466 {
467 	netif_carrier_on(netdev);
468 	netif_tx_start_all_queues(netdev);
469 }
470 
471 /**
472  * octep_open() - start the octeon network device.
473  *
474  * @netdev: pointer to kernel network device.
475  *
476  * setup Tx/Rx queues, interrupts and enable hardware operation of Tx/Rx queues
477  * and interrupts..
478  *
479  * Return: 0, on successfully setting up device and bring it up.
480  *         -1, on any error.
481  */
482 static int octep_open(struct net_device *netdev)
483 {
484 	struct octep_device *oct = netdev_priv(netdev);
485 	int err, ret;
486 
487 	netdev_info(netdev, "Starting netdev ...\n");
488 	netif_carrier_off(netdev);
489 
490 	oct->hw_ops.reset_io_queues(oct);
491 
492 	if (octep_setup_iqs(oct))
493 		goto setup_iq_err;
494 	if (octep_setup_oqs(oct))
495 		goto setup_oq_err;
496 	if (octep_setup_irqs(oct))
497 		goto setup_irq_err;
498 
499 	err = netif_set_real_num_tx_queues(netdev, oct->num_oqs);
500 	if (err)
501 		goto set_queues_err;
502 	err = netif_set_real_num_rx_queues(netdev, oct->num_iqs);
503 	if (err)
504 		goto set_queues_err;
505 
506 	octep_napi_add(oct);
507 	octep_napi_enable(oct);
508 
509 	oct->link_info.admin_up = 1;
510 	octep_set_rx_state(oct, true);
511 
512 	ret = octep_get_link_status(oct);
513 	if (!ret)
514 		octep_set_link_status(oct, true);
515 
516 	/* Enable the input and output queues for this Octeon device */
517 	oct->hw_ops.enable_io_queues(oct);
518 
519 	/* Enable Octeon device interrupts */
520 	oct->hw_ops.enable_interrupts(oct);
521 
522 	octep_oq_dbell_init(oct);
523 
524 	ret = octep_get_link_status(oct);
525 	if (ret)
526 		octep_link_up(netdev);
527 
528 	return 0;
529 
530 set_queues_err:
531 	octep_napi_disable(oct);
532 	octep_napi_delete(oct);
533 	octep_clean_irqs(oct);
534 setup_irq_err:
535 	octep_free_oqs(oct);
536 setup_oq_err:
537 	octep_free_iqs(oct);
538 setup_iq_err:
539 	return -1;
540 }
541 
542 /**
543  * octep_stop() - stop the octeon network device.
544  *
545  * @netdev: pointer to kernel network device.
546  *
547  * stop the device Tx/Rx operations, bring down the link and
548  * free up all resources allocated for Tx/Rx queues and interrupts.
549  */
550 static int octep_stop(struct net_device *netdev)
551 {
552 	struct octep_device *oct = netdev_priv(netdev);
553 
554 	netdev_info(netdev, "Stopping the device ...\n");
555 
556 	/* Stop Tx from stack */
557 	netif_tx_stop_all_queues(netdev);
558 	netif_carrier_off(netdev);
559 	netif_tx_disable(netdev);
560 
561 	octep_set_link_status(oct, false);
562 	octep_set_rx_state(oct, false);
563 
564 	oct->link_info.admin_up = 0;
565 	oct->link_info.oper_up = 0;
566 
567 	oct->hw_ops.disable_interrupts(oct);
568 	octep_napi_disable(oct);
569 	octep_napi_delete(oct);
570 
571 	octep_clean_irqs(oct);
572 	octep_clean_iqs(oct);
573 
574 	oct->hw_ops.disable_io_queues(oct);
575 	oct->hw_ops.reset_io_queues(oct);
576 	octep_free_oqs(oct);
577 	octep_free_iqs(oct);
578 	netdev_info(netdev, "Device stopped !!\n");
579 	return 0;
580 }
581 
582 /**
583  * octep_iq_full_check() - check if a Tx queue is full.
584  *
585  * @iq: Octeon Tx queue data structure.
586  *
587  * Return: 0, if the Tx queue is not full.
588  *         1, if the Tx queue is full.
589  */
590 static inline int octep_iq_full_check(struct octep_iq *iq)
591 {
592 	if (likely((iq->max_count - atomic_read(&iq->instr_pending)) >=
593 		   OCTEP_WAKE_QUEUE_THRESHOLD))
594 		return 0;
595 
596 	/* Stop the queue if unable to send */
597 	netif_stop_subqueue(iq->netdev, iq->q_no);
598 
599 	/* check again and restart the queue, in case NAPI has just freed
600 	 * enough Tx ring entries.
601 	 */
602 	if (unlikely((iq->max_count - atomic_read(&iq->instr_pending)) >=
603 		     OCTEP_WAKE_QUEUE_THRESHOLD)) {
604 		netif_start_subqueue(iq->netdev, iq->q_no);
605 		iq->stats.restart_cnt++;
606 		return 0;
607 	}
608 
609 	return 1;
610 }
611 
612 /**
613  * octep_start_xmit() - Enqueue packet to Octoen hardware Tx Queue.
614  *
615  * @skb: packet skbuff pointer.
616  * @netdev: kernel network device.
617  *
618  * Return: NETDEV_TX_BUSY, if Tx Queue is full.
619  *         NETDEV_TX_OK, if successfully enqueued to hardware Tx queue.
620  */
621 static netdev_tx_t octep_start_xmit(struct sk_buff *skb,
622 				    struct net_device *netdev)
623 {
624 	struct octep_device *oct = netdev_priv(netdev);
625 	struct octep_tx_sglist_desc *sglist;
626 	struct octep_tx_buffer *tx_buffer;
627 	struct octep_tx_desc_hw *hw_desc;
628 	struct skb_shared_info *shinfo;
629 	struct octep_instr_hdr *ih;
630 	struct octep_iq *iq;
631 	skb_frag_t *frag;
632 	u16 nr_frags, si;
633 	u16 q_no, wi;
634 
635 	q_no = skb_get_queue_mapping(skb);
636 	if (q_no >= oct->num_iqs) {
637 		netdev_err(netdev, "Invalid Tx skb->queue_mapping=%d\n", q_no);
638 		q_no = q_no % oct->num_iqs;
639 	}
640 
641 	iq = oct->iq[q_no];
642 	if (octep_iq_full_check(iq)) {
643 		iq->stats.tx_busy++;
644 		return NETDEV_TX_BUSY;
645 	}
646 
647 	shinfo = skb_shinfo(skb);
648 	nr_frags = shinfo->nr_frags;
649 
650 	wi = iq->host_write_index;
651 	hw_desc = &iq->desc_ring[wi];
652 	hw_desc->ih64 = 0;
653 
654 	tx_buffer = iq->buff_info + wi;
655 	tx_buffer->skb = skb;
656 
657 	ih = &hw_desc->ih;
658 	ih->tlen = skb->len;
659 	ih->pkind = oct->pkind;
660 
661 	if (!nr_frags) {
662 		tx_buffer->gather = 0;
663 		tx_buffer->dma = dma_map_single(iq->dev, skb->data,
664 						skb->len, DMA_TO_DEVICE);
665 		if (dma_mapping_error(iq->dev, tx_buffer->dma))
666 			goto dma_map_err;
667 		hw_desc->dptr = tx_buffer->dma;
668 	} else {
669 		/* Scatter/Gather */
670 		dma_addr_t dma;
671 		u16 len;
672 
673 		sglist = tx_buffer->sglist;
674 
675 		ih->gsz = nr_frags + 1;
676 		ih->gather = 1;
677 		tx_buffer->gather = 1;
678 
679 		len = skb_headlen(skb);
680 		dma = dma_map_single(iq->dev, skb->data, len, DMA_TO_DEVICE);
681 		if (dma_mapping_error(iq->dev, dma))
682 			goto dma_map_err;
683 
684 		dma_sync_single_for_cpu(iq->dev, tx_buffer->sglist_dma,
685 					OCTEP_SGLIST_SIZE_PER_PKT,
686 					DMA_TO_DEVICE);
687 		memset(sglist, 0, OCTEP_SGLIST_SIZE_PER_PKT);
688 		sglist[0].len[3] = len;
689 		sglist[0].dma_ptr[0] = dma;
690 
691 		si = 1; /* entry 0 is main skb, mapped above */
692 		frag = &shinfo->frags[0];
693 		while (nr_frags--) {
694 			len = skb_frag_size(frag);
695 			dma = skb_frag_dma_map(iq->dev, frag, 0,
696 					       len, DMA_TO_DEVICE);
697 			if (dma_mapping_error(iq->dev, dma))
698 				goto dma_map_sg_err;
699 
700 			sglist[si >> 2].len[3 - (si & 3)] = len;
701 			sglist[si >> 2].dma_ptr[si & 3] = dma;
702 
703 			frag++;
704 			si++;
705 		}
706 		dma_sync_single_for_device(iq->dev, tx_buffer->sglist_dma,
707 					   OCTEP_SGLIST_SIZE_PER_PKT,
708 					   DMA_TO_DEVICE);
709 
710 		hw_desc->dptr = tx_buffer->sglist_dma;
711 	}
712 
713 	/* Flush the hw descriptor before writing to doorbell */
714 	wmb();
715 
716 	/* Ring Doorbell to notify the NIC there is a new packet */
717 	writel(1, iq->doorbell_reg);
718 	atomic_inc(&iq->instr_pending);
719 	wi++;
720 	if (wi == iq->max_count)
721 		wi = 0;
722 	iq->host_write_index = wi;
723 
724 	netdev_tx_sent_queue(iq->netdev_q, skb->len);
725 	iq->stats.instr_posted++;
726 	skb_tx_timestamp(skb);
727 	return NETDEV_TX_OK;
728 
729 dma_map_sg_err:
730 	if (si > 0) {
731 		dma_unmap_single(iq->dev, sglist[0].dma_ptr[0],
732 				 sglist[0].len[0], DMA_TO_DEVICE);
733 		sglist[0].len[0] = 0;
734 	}
735 	while (si > 1) {
736 		dma_unmap_page(iq->dev, sglist[si >> 2].dma_ptr[si & 3],
737 			       sglist[si >> 2].len[si & 3], DMA_TO_DEVICE);
738 		sglist[si >> 2].len[si & 3] = 0;
739 		si--;
740 	}
741 	tx_buffer->gather = 0;
742 dma_map_err:
743 	dev_kfree_skb_any(skb);
744 	return NETDEV_TX_OK;
745 }
746 
747 /**
748  * octep_get_stats64() - Get Octeon network device statistics.
749  *
750  * @netdev: kernel network device.
751  * @stats: pointer to stats structure to be filled in.
752  */
753 static void octep_get_stats64(struct net_device *netdev,
754 			      struct rtnl_link_stats64 *stats)
755 {
756 	u64 tx_packets, tx_bytes, rx_packets, rx_bytes;
757 	struct octep_device *oct = netdev_priv(netdev);
758 	int q;
759 
760 	octep_get_if_stats(oct);
761 	tx_packets = 0;
762 	tx_bytes = 0;
763 	rx_packets = 0;
764 	rx_bytes = 0;
765 	for (q = 0; q < oct->num_oqs; q++) {
766 		struct octep_iq *iq = oct->iq[q];
767 		struct octep_oq *oq = oct->oq[q];
768 
769 		tx_packets += iq->stats.instr_completed;
770 		tx_bytes += iq->stats.bytes_sent;
771 		rx_packets += oq->stats.packets;
772 		rx_bytes += oq->stats.bytes;
773 	}
774 	stats->tx_packets = tx_packets;
775 	stats->tx_bytes = tx_bytes;
776 	stats->rx_packets = rx_packets;
777 	stats->rx_bytes = rx_bytes;
778 	stats->multicast = oct->iface_rx_stats.mcast_pkts;
779 	stats->rx_errors = oct->iface_rx_stats.err_pkts;
780 	stats->collisions = oct->iface_tx_stats.xscol;
781 	stats->tx_fifo_errors = oct->iface_tx_stats.undflw;
782 }
783 
784 /**
785  * octep_tx_timeout_task - work queue task to Handle Tx queue timeout.
786  *
787  * @work: pointer to Tx queue timeout work_struct
788  *
789  * Stop and start the device so that it frees up all queue resources
790  * and restarts the queues, that potentially clears a Tx queue timeout
791  * condition.
792  **/
793 static void octep_tx_timeout_task(struct work_struct *work)
794 {
795 	struct octep_device *oct = container_of(work, struct octep_device,
796 						tx_timeout_task);
797 	struct net_device *netdev = oct->netdev;
798 
799 	rtnl_lock();
800 	if (netif_running(netdev)) {
801 		octep_stop(netdev);
802 		octep_open(netdev);
803 	}
804 	rtnl_unlock();
805 }
806 
807 /**
808  * octep_tx_timeout() - Handle Tx Queue timeout.
809  *
810  * @netdev: pointer to kernel network device.
811  * @txqueue: Timed out Tx queue number.
812  *
813  * Schedule a work to handle Tx queue timeout.
814  */
815 static void octep_tx_timeout(struct net_device *netdev, unsigned int txqueue)
816 {
817 	struct octep_device *oct = netdev_priv(netdev);
818 
819 	queue_work(octep_wq, &oct->tx_timeout_task);
820 }
821 
822 static int octep_set_mac(struct net_device *netdev, void *p)
823 {
824 	struct octep_device *oct = netdev_priv(netdev);
825 	struct sockaddr *addr = (struct sockaddr *)p;
826 	int err;
827 
828 	if (!is_valid_ether_addr(addr->sa_data))
829 		return -EADDRNOTAVAIL;
830 
831 	err = octep_set_mac_addr(oct, addr->sa_data);
832 	if (err)
833 		return err;
834 
835 	memcpy(oct->mac_addr, addr->sa_data, ETH_ALEN);
836 	eth_hw_addr_set(netdev, addr->sa_data);
837 
838 	return 0;
839 }
840 
841 static int octep_change_mtu(struct net_device *netdev, int new_mtu)
842 {
843 	struct octep_device *oct = netdev_priv(netdev);
844 	struct octep_iface_link_info *link_info;
845 	int err = 0;
846 
847 	link_info = &oct->link_info;
848 	if (link_info->mtu == new_mtu)
849 		return 0;
850 
851 	err = octep_set_mtu(oct, new_mtu);
852 	if (!err) {
853 		oct->link_info.mtu = new_mtu;
854 		netdev->mtu = new_mtu;
855 	}
856 
857 	return err;
858 }
859 
860 static const struct net_device_ops octep_netdev_ops = {
861 	.ndo_open                = octep_open,
862 	.ndo_stop                = octep_stop,
863 	.ndo_start_xmit          = octep_start_xmit,
864 	.ndo_get_stats64         = octep_get_stats64,
865 	.ndo_tx_timeout          = octep_tx_timeout,
866 	.ndo_set_mac_address     = octep_set_mac,
867 	.ndo_change_mtu          = octep_change_mtu,
868 };
869 
870 /**
871  * octep_ctrl_mbox_task - work queue task to handle ctrl mbox messages.
872  *
873  * @work: pointer to ctrl mbox work_struct
874  *
875  * Poll ctrl mbox message queue and handle control messages from firmware.
876  **/
877 static void octep_ctrl_mbox_task(struct work_struct *work)
878 {
879 	struct octep_device *oct = container_of(work, struct octep_device,
880 						ctrl_mbox_task);
881 	struct net_device *netdev = oct->netdev;
882 	struct octep_ctrl_net_f2h_req req = {};
883 	struct octep_ctrl_mbox_msg msg;
884 	int ret = 0;
885 
886 	msg.msg = &req;
887 	while (true) {
888 		ret = octep_ctrl_mbox_recv(&oct->ctrl_mbox, &msg);
889 		if (ret)
890 			break;
891 
892 		switch (req.hdr.cmd) {
893 		case OCTEP_CTRL_NET_F2H_CMD_LINK_STATUS:
894 			if (netif_running(netdev)) {
895 				if (req.link.state) {
896 					dev_info(&oct->pdev->dev, "netif_carrier_on\n");
897 					netif_carrier_on(netdev);
898 				} else {
899 					dev_info(&oct->pdev->dev, "netif_carrier_off\n");
900 					netif_carrier_off(netdev);
901 				}
902 			}
903 			break;
904 		default:
905 			pr_info("Unknown mbox req : %u\n", req.hdr.cmd);
906 			break;
907 		}
908 	}
909 }
910 
911 static const char *octep_devid_to_str(struct octep_device *oct)
912 {
913 	switch (oct->chip_id) {
914 	case OCTEP_PCI_DEVICE_ID_CN93_PF:
915 		return "CN93XX";
916 	case OCTEP_PCI_DEVICE_ID_CNF95N_PF:
917 		return "CNF95N";
918 	default:
919 		return "Unsupported";
920 	}
921 }
922 
923 /**
924  * octep_device_setup() - Setup Octeon Device.
925  *
926  * @oct: Octeon device private data structure.
927  *
928  * Setup Octeon device hardware operations, configuration, etc ...
929  */
930 int octep_device_setup(struct octep_device *oct)
931 {
932 	struct octep_ctrl_mbox *ctrl_mbox;
933 	struct pci_dev *pdev = oct->pdev;
934 	int i, ret;
935 
936 	/* allocate memory for oct->conf */
937 	oct->conf = kzalloc(sizeof(*oct->conf), GFP_KERNEL);
938 	if (!oct->conf)
939 		return -ENOMEM;
940 
941 	/* Map BAR regions */
942 	for (i = 0; i < OCTEP_MMIO_REGIONS; i++) {
943 		oct->mmio[i].hw_addr =
944 			ioremap(pci_resource_start(oct->pdev, i * 2),
945 				pci_resource_len(oct->pdev, i * 2));
946 		oct->mmio[i].mapped = 1;
947 	}
948 
949 	oct->chip_id = pdev->device;
950 	oct->rev_id = pdev->revision;
951 	dev_info(&pdev->dev, "chip_id = 0x%x\n", pdev->device);
952 
953 	switch (oct->chip_id) {
954 	case OCTEP_PCI_DEVICE_ID_CN93_PF:
955 	case OCTEP_PCI_DEVICE_ID_CNF95N_PF:
956 		dev_info(&pdev->dev, "Setting up OCTEON %s PF PASS%d.%d\n",
957 			 octep_devid_to_str(oct), OCTEP_MAJOR_REV(oct),
958 			 OCTEP_MINOR_REV(oct));
959 		octep_device_setup_cn93_pf(oct);
960 		break;
961 	default:
962 		dev_err(&pdev->dev,
963 			"%s: unsupported device\n", __func__);
964 		goto unsupported_dev;
965 	}
966 
967 	oct->pkind = CFG_GET_IQ_PKIND(oct->conf);
968 
969 	/* Initialize control mbox */
970 	ctrl_mbox = &oct->ctrl_mbox;
971 	ctrl_mbox->barmem = CFG_GET_CTRL_MBOX_MEM_ADDR(oct->conf);
972 	ret = octep_ctrl_mbox_init(ctrl_mbox);
973 	if (ret) {
974 		dev_err(&pdev->dev, "Failed to initialize control mbox\n");
975 		return -1;
976 	}
977 	oct->ctrl_mbox_ifstats_offset = OCTEP_CTRL_MBOX_SZ(ctrl_mbox->h2fq.elem_sz,
978 							   ctrl_mbox->h2fq.elem_cnt,
979 							   ctrl_mbox->f2hq.elem_sz,
980 							   ctrl_mbox->f2hq.elem_cnt);
981 
982 	return 0;
983 
984 unsupported_dev:
985 	return -1;
986 }
987 
988 /**
989  * octep_device_cleanup() - Cleanup Octeon Device.
990  *
991  * @oct: Octeon device private data structure.
992  *
993  * Cleanup Octeon device allocated resources.
994  */
995 static void octep_device_cleanup(struct octep_device *oct)
996 {
997 	int i;
998 
999 	dev_info(&oct->pdev->dev, "Cleaning up Octeon Device ...\n");
1000 
1001 	for (i = 0; i < OCTEP_MAX_VF; i++) {
1002 		vfree(oct->mbox[i]);
1003 		oct->mbox[i] = NULL;
1004 	}
1005 
1006 	octep_ctrl_mbox_uninit(&oct->ctrl_mbox);
1007 
1008 	oct->hw_ops.soft_reset(oct);
1009 	for (i = 0; i < OCTEP_MMIO_REGIONS; i++) {
1010 		if (oct->mmio[i].mapped)
1011 			iounmap(oct->mmio[i].hw_addr);
1012 	}
1013 
1014 	kfree(oct->conf);
1015 	oct->conf = NULL;
1016 }
1017 
1018 /**
1019  * octep_probe() - Octeon PCI device probe handler.
1020  *
1021  * @pdev: PCI device structure.
1022  * @ent: entry in Octeon PCI device ID table.
1023  *
1024  * Initializes and enables the Octeon PCI device for network operations.
1025  * Initializes Octeon private data structure and registers a network device.
1026  */
1027 static int octep_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1028 {
1029 	struct octep_device *octep_dev = NULL;
1030 	struct net_device *netdev;
1031 	int err;
1032 
1033 	err = pci_enable_device(pdev);
1034 	if (err) {
1035 		dev_err(&pdev->dev, "Failed to enable PCI device\n");
1036 		return  err;
1037 	}
1038 
1039 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1040 	if (err) {
1041 		dev_err(&pdev->dev, "Failed to set DMA mask !!\n");
1042 		goto err_dma_mask;
1043 	}
1044 
1045 	err = pci_request_mem_regions(pdev, OCTEP_DRV_NAME);
1046 	if (err) {
1047 		dev_err(&pdev->dev, "Failed to map PCI memory regions\n");
1048 		goto err_pci_regions;
1049 	}
1050 
1051 	pci_enable_pcie_error_reporting(pdev);
1052 	pci_set_master(pdev);
1053 
1054 	netdev = alloc_etherdev_mq(sizeof(struct octep_device),
1055 				   OCTEP_MAX_QUEUES);
1056 	if (!netdev) {
1057 		dev_err(&pdev->dev, "Failed to allocate netdev\n");
1058 		err = -ENOMEM;
1059 		goto err_alloc_netdev;
1060 	}
1061 	SET_NETDEV_DEV(netdev, &pdev->dev);
1062 
1063 	octep_dev = netdev_priv(netdev);
1064 	octep_dev->netdev = netdev;
1065 	octep_dev->pdev = pdev;
1066 	octep_dev->dev = &pdev->dev;
1067 	pci_set_drvdata(pdev, octep_dev);
1068 
1069 	err = octep_device_setup(octep_dev);
1070 	if (err) {
1071 		dev_err(&pdev->dev, "Device setup failed\n");
1072 		goto err_octep_config;
1073 	}
1074 	INIT_WORK(&octep_dev->tx_timeout_task, octep_tx_timeout_task);
1075 	INIT_WORK(&octep_dev->ctrl_mbox_task, octep_ctrl_mbox_task);
1076 
1077 	netdev->netdev_ops = &octep_netdev_ops;
1078 	octep_set_ethtool_ops(netdev);
1079 	netif_carrier_off(netdev);
1080 
1081 	netdev->hw_features = NETIF_F_SG;
1082 	netdev->features |= netdev->hw_features;
1083 	netdev->min_mtu = OCTEP_MIN_MTU;
1084 	netdev->max_mtu = OCTEP_MAX_MTU;
1085 	netdev->mtu = OCTEP_DEFAULT_MTU;
1086 
1087 	octep_get_mac_addr(octep_dev, octep_dev->mac_addr);
1088 	eth_hw_addr_set(netdev, octep_dev->mac_addr);
1089 
1090 	err = register_netdev(netdev);
1091 	if (err) {
1092 		dev_err(&pdev->dev, "Failed to register netdev\n");
1093 		goto register_dev_err;
1094 	}
1095 	dev_info(&pdev->dev, "Device probe successful\n");
1096 	return 0;
1097 
1098 register_dev_err:
1099 	octep_device_cleanup(octep_dev);
1100 err_octep_config:
1101 	free_netdev(netdev);
1102 err_alloc_netdev:
1103 	pci_disable_pcie_error_reporting(pdev);
1104 	pci_release_mem_regions(pdev);
1105 err_pci_regions:
1106 err_dma_mask:
1107 	pci_disable_device(pdev);
1108 	return err;
1109 }
1110 
1111 /**
1112  * octep_remove() - Remove Octeon PCI device from driver control.
1113  *
1114  * @pdev: PCI device structure of the Octeon device.
1115  *
1116  * Cleanup all resources allocated for the Octeon device.
1117  * Unregister from network device and disable the PCI device.
1118  */
1119 static void octep_remove(struct pci_dev *pdev)
1120 {
1121 	struct octep_device *oct = pci_get_drvdata(pdev);
1122 	struct net_device *netdev;
1123 
1124 	if (!oct)
1125 		return;
1126 
1127 	cancel_work_sync(&oct->tx_timeout_task);
1128 	cancel_work_sync(&oct->ctrl_mbox_task);
1129 	netdev = oct->netdev;
1130 	if (netdev->reg_state == NETREG_REGISTERED)
1131 		unregister_netdev(netdev);
1132 
1133 	octep_device_cleanup(oct);
1134 	pci_release_mem_regions(pdev);
1135 	free_netdev(netdev);
1136 	pci_disable_pcie_error_reporting(pdev);
1137 	pci_disable_device(pdev);
1138 }
1139 
1140 static struct pci_driver octep_driver = {
1141 	.name = OCTEP_DRV_NAME,
1142 	.id_table = octep_pci_id_tbl,
1143 	.probe = octep_probe,
1144 	.remove = octep_remove,
1145 };
1146 
1147 /**
1148  * octep_init_module() - Module initialiation.
1149  *
1150  * create common resource for the driver and register PCI driver.
1151  */
1152 static int __init octep_init_module(void)
1153 {
1154 	int ret;
1155 
1156 	pr_info("%s: Loading %s ...\n", OCTEP_DRV_NAME, OCTEP_DRV_STRING);
1157 
1158 	/* work queue for all deferred tasks */
1159 	octep_wq = create_singlethread_workqueue(OCTEP_DRV_NAME);
1160 	if (!octep_wq) {
1161 		pr_err("%s: Failed to create common workqueue\n",
1162 		       OCTEP_DRV_NAME);
1163 		return -ENOMEM;
1164 	}
1165 
1166 	ret = pci_register_driver(&octep_driver);
1167 	if (ret < 0) {
1168 		pr_err("%s: Failed to register PCI driver; err=%d\n",
1169 		       OCTEP_DRV_NAME, ret);
1170 		destroy_workqueue(octep_wq);
1171 		return ret;
1172 	}
1173 
1174 	pr_info("%s: Loaded successfully !\n", OCTEP_DRV_NAME);
1175 
1176 	return ret;
1177 }
1178 
1179 /**
1180  * octep_exit_module() - Module exit routine.
1181  *
1182  * unregister the driver with PCI subsystem and cleanup common resources.
1183  */
1184 static void __exit octep_exit_module(void)
1185 {
1186 	pr_info("%s: Unloading ...\n", OCTEP_DRV_NAME);
1187 
1188 	pci_unregister_driver(&octep_driver);
1189 	destroy_workqueue(octep_wq);
1190 
1191 	pr_info("%s: Unloading complete\n", OCTEP_DRV_NAME);
1192 }
1193 
1194 module_init(octep_init_module);
1195 module_exit(octep_exit_module);
1196