blob: 17490ec024281b00094f2b739b4ea0aead9e56b4 [file] [log] [blame]
Sunil Goutham4863dea2015-05-26 19:20:15 -07001/*
2 * Copyright (C) 2015 Cavium, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License
6 * as published by the Free Software Foundation.
7 */
8
9#include <linux/module.h>
10#include <linux/interrupt.h>
11#include <linux/pci.h>
12#include <linux/etherdevice.h>
13#include <linux/of.h>
Sunil Goutham712c3182016-11-15 17:37:36 +053014#include <linux/if_vlan.h>
Sunil Goutham4863dea2015-05-26 19:20:15 -070015
16#include "nic_reg.h"
17#include "nic.h"
18#include "q_struct.h"
19#include "thunder_bgx.h"
20
21#define DRV_NAME "thunder-nic"
22#define DRV_VERSION "1.0"
23
Sunil Gouthama5c3d492016-08-12 16:51:24 +053024struct hw_info {
25 u8 bgx_cnt;
26 u8 chans_per_lmac;
27 u8 chans_per_bgx; /* Rx/Tx chans */
Sunil Goutham0025d93e2016-08-12 16:51:26 +053028 u8 chans_per_rgx;
29 u8 chans_per_lbk;
Sunil Gouthama5c3d492016-08-12 16:51:24 +053030 u16 cpi_cnt;
31 u16 rssi_cnt;
32 u16 rss_ind_tbl_size;
33 u16 tl4_cnt;
34 u16 tl3_cnt;
35 u8 tl2_cnt;
36 u8 tl1_cnt;
37 bool tl1_per_bgx; /* TL1 per BGX or per LMAC */
38};
39
Sunil Goutham4863dea2015-05-26 19:20:15 -070040struct nicpf {
41 struct pci_dev *pdev;
Sunil Gouthama5c3d492016-08-12 16:51:24 +053042 struct hw_info *hw;
Sunil Goutham4863dea2015-05-26 19:20:15 -070043 u8 node;
44 unsigned int flags;
45 u8 num_vf_en; /* No of VF enabled */
46 bool vf_enabled[MAX_NUM_VFS_SUPPORTED];
47 void __iomem *reg_base; /* Register start address */
Sunil Goutham92dc8762015-08-30 12:29:15 +030048 u8 num_sqs_en; /* Secondary qsets enabled */
49 u64 nicvf[MAX_NUM_VFS_SUPPORTED];
50 u8 vf_sqs[MAX_NUM_VFS_SUPPORTED][MAX_SQS_PER_VF];
51 u8 pqs_vf[MAX_NUM_VFS_SUPPORTED];
52 bool sqs_used[MAX_NUM_VFS_SUPPORTED];
Sunil Goutham4863dea2015-05-26 19:20:15 -070053 struct pkind_cfg pkind;
54#define NIC_SET_VF_LMAC_MAP(bgx, lmac) (((bgx & 0xF) << 4) | (lmac & 0xF))
55#define NIC_GET_BGX_FROM_VF_LMAC_MAP(map) ((map >> 4) & 0xF)
56#define NIC_GET_LMAC_FROM_VF_LMAC_MAP(map) (map & 0xF)
Sunil Goutham949b5332016-08-12 16:51:34 +053057 u8 *vf_lmac_map;
Sunil Goutham4863dea2015-05-26 19:20:15 -070058 struct delayed_work dwork;
59 struct workqueue_struct *check_link;
Sunil Goutham949b5332016-08-12 16:51:34 +053060 u8 *link;
61 u8 *duplex;
62 u32 *speed;
Sunil Goutham4863dea2015-05-26 19:20:15 -070063 u16 cpi_base[MAX_NUM_VFS_SUPPORTED];
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -070064 u16 rssi_base[MAX_NUM_VFS_SUPPORTED];
Sunil Goutham4863dea2015-05-26 19:20:15 -070065 bool mbx_lock[MAX_NUM_VFS_SUPPORTED];
66
67 /* MSI-X */
68 bool msix_enabled;
69 u8 num_vec;
Sunil Goutham52358aa2016-08-12 16:51:29 +053070 struct msix_entry *msix_entries;
Sunil Goutham4863dea2015-05-26 19:20:15 -070071 bool irq_allocated[NIC_PF_MSIX_VECTORS];
Sunil Goutham52358aa2016-08-12 16:51:29 +053072 char irq_name[NIC_PF_MSIX_VECTORS][20];
Sunil Goutham4863dea2015-05-26 19:20:15 -070073};
74
75/* Supported devices */
76static const struct pci_device_id nic_id_table[] = {
77 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_NIC_PF) },
78 { 0, } /* end of table */
79};
80
81MODULE_AUTHOR("Sunil Goutham");
82MODULE_DESCRIPTION("Cavium Thunder NIC Physical Function Driver");
83MODULE_LICENSE("GPL v2");
84MODULE_VERSION(DRV_VERSION);
85MODULE_DEVICE_TABLE(pci, nic_id_table);
86
87/* The Cavium ThunderX network controller can *only* be found in SoCs
88 * containing the ThunderX ARM64 CPU implementation. All accesses to the device
89 * registers on this platform are implicitly strongly ordered with respect
90 * to memory accesses. So writeq_relaxed() and readq_relaxed() are safe to use
91 * with no memory barriers in this driver. The readq()/writeq() functions add
92 * explicit ordering operation which in this case are redundant, and only
93 * add overhead.
94 */
95
96/* Register read/write APIs */
97static void nic_reg_write(struct nicpf *nic, u64 offset, u64 val)
98{
99 writeq_relaxed(val, nic->reg_base + offset);
100}
101
102static u64 nic_reg_read(struct nicpf *nic, u64 offset)
103{
104 return readq_relaxed(nic->reg_base + offset);
105}
106
107/* PF -> VF mailbox communication APIs */
108static void nic_enable_mbx_intr(struct nicpf *nic)
109{
Sunil Goutham52358aa2016-08-12 16:51:29 +0530110 int vf_cnt = pci_sriov_get_totalvfs(nic->pdev);
111
112#define INTR_MASK(vfs) ((vfs < 64) ? (BIT_ULL(vfs) - 1) : (~0ull))
113
114 /* Clear it, to avoid spurious interrupts (if any) */
115 nic_reg_write(nic, NIC_PF_MAILBOX_INT, INTR_MASK(vf_cnt));
116
117 /* Enable mailbox interrupt for all VFs */
118 nic_reg_write(nic, NIC_PF_MAILBOX_ENA_W1S, INTR_MASK(vf_cnt));
119 /* One mailbox intr enable reg per 64 VFs */
120 if (vf_cnt > 64) {
121 nic_reg_write(nic, NIC_PF_MAILBOX_INT + sizeof(u64),
122 INTR_MASK(vf_cnt - 64));
123 nic_reg_write(nic, NIC_PF_MAILBOX_ENA_W1S + sizeof(u64),
124 INTR_MASK(vf_cnt - 64));
125 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700126}
127
128static void nic_clear_mbx_intr(struct nicpf *nic, int vf, int mbx_reg)
129{
130 nic_reg_write(nic, NIC_PF_MAILBOX_INT + (mbx_reg << 3), BIT_ULL(vf));
131}
132
133static u64 nic_get_mbx_addr(int vf)
134{
135 return NIC_PF_VF_0_127_MAILBOX_0_1 + (vf << NIC_VF_NUM_SHIFT);
136}
137
138/* Send a mailbox message to VF
139 * @vf: vf to which this message to be sent
140 * @mbx: Message to be sent
141 */
142static void nic_send_msg_to_vf(struct nicpf *nic, int vf, union nic_mbx *mbx)
143{
144 void __iomem *mbx_addr = nic->reg_base + nic_get_mbx_addr(vf);
145 u64 *msg = (u64 *)mbx;
146
147 /* In first revision HW, mbox interrupt is triggerred
148 * when PF writes to MBOX(1), in next revisions when
149 * PF writes to MBOX(0)
150 */
Sunil Goutham40fb5f82015-12-10 13:25:19 +0530151 if (pass1_silicon(nic->pdev)) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700152 /* see the comment for nic_reg_write()/nic_reg_read()
153 * functions above
154 */
155 writeq_relaxed(msg[0], mbx_addr);
156 writeq_relaxed(msg[1], mbx_addr + 8);
157 } else {
158 writeq_relaxed(msg[1], mbx_addr + 8);
159 writeq_relaxed(msg[0], mbx_addr);
160 }
161}
162
163/* Responds to VF's READY message with VF's
164 * ID, node, MAC address e.t.c
165 * @vf: VF which sent READY message
166 */
167static void nic_mbx_send_ready(struct nicpf *nic, int vf)
168{
169 union nic_mbx mbx = {};
170 int bgx_idx, lmac;
171 const char *mac;
172
173 mbx.nic_cfg.msg = NIC_MBOX_MSG_READY;
174 mbx.nic_cfg.vf_id = vf;
175
176 mbx.nic_cfg.tns_mode = NIC_TNS_BYPASS_MODE;
177
Sunil Goutham949b5332016-08-12 16:51:34 +0530178 if (vf < nic->num_vf_en) {
Sunil Goutham92dc8762015-08-30 12:29:15 +0300179 bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
180 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700181
Sunil Goutham92dc8762015-08-30 12:29:15 +0300182 mac = bgx_get_lmac_mac(nic->node, bgx_idx, lmac);
183 if (mac)
184 ether_addr_copy((u8 *)&mbx.nic_cfg.mac_addr, mac);
185 }
186 mbx.nic_cfg.sqs_mode = (vf >= nic->num_vf_en) ? true : false;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700187 mbx.nic_cfg.node_id = nic->node;
Sunil Gouthamd77a2382015-08-30 12:29:16 +0300188
Sunil Goutham949b5332016-08-12 16:51:34 +0530189 mbx.nic_cfg.loopback_supported = vf < nic->num_vf_en;
Sunil Gouthamd77a2382015-08-30 12:29:16 +0300190
Sunil Goutham4863dea2015-05-26 19:20:15 -0700191 nic_send_msg_to_vf(nic, vf, &mbx);
192}
193
194/* ACKs VF's mailbox message
195 * @vf: VF to which ACK to be sent
196 */
197static void nic_mbx_send_ack(struct nicpf *nic, int vf)
198{
199 union nic_mbx mbx = {};
200
201 mbx.msg.msg = NIC_MBOX_MSG_ACK;
202 nic_send_msg_to_vf(nic, vf, &mbx);
203}
204
205/* NACKs VF's mailbox message that PF is not able to
206 * complete the action
207 * @vf: VF to which ACK to be sent
208 */
209static void nic_mbx_send_nack(struct nicpf *nic, int vf)
210{
211 union nic_mbx mbx = {};
212
213 mbx.msg.msg = NIC_MBOX_MSG_NACK;
214 nic_send_msg_to_vf(nic, vf, &mbx);
215}
216
217/* Flush all in flight receive packets to memory and
218 * bring down an active RQ
219 */
220static int nic_rcv_queue_sw_sync(struct nicpf *nic)
221{
222 u16 timeout = ~0x00;
223
224 nic_reg_write(nic, NIC_PF_SW_SYNC_RX, 0x01);
225 /* Wait till sync cycle is finished */
226 while (timeout) {
227 if (nic_reg_read(nic, NIC_PF_SW_SYNC_RX_DONE) & 0x1)
228 break;
229 timeout--;
230 }
231 nic_reg_write(nic, NIC_PF_SW_SYNC_RX, 0x00);
232 if (!timeout) {
233 dev_err(&nic->pdev->dev, "Receive queue software sync failed");
234 return 1;
235 }
236 return 0;
237}
238
239/* Get BGX Rx/Tx stats and respond to VF's request */
240static void nic_get_bgx_stats(struct nicpf *nic, struct bgx_stats_msg *bgx)
241{
242 int bgx_idx, lmac;
243 union nic_mbx mbx = {};
244
245 bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[bgx->vf_id]);
246 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[bgx->vf_id]);
247
248 mbx.bgx_stats.msg = NIC_MBOX_MSG_BGX_STATS;
249 mbx.bgx_stats.vf_id = bgx->vf_id;
250 mbx.bgx_stats.rx = bgx->rx;
251 mbx.bgx_stats.idx = bgx->idx;
252 if (bgx->rx)
253 mbx.bgx_stats.stats = bgx_get_rx_stats(nic->node, bgx_idx,
254 lmac, bgx->idx);
255 else
256 mbx.bgx_stats.stats = bgx_get_tx_stats(nic->node, bgx_idx,
257 lmac, bgx->idx);
258 nic_send_msg_to_vf(nic, bgx->vf_id, &mbx);
259}
260
261/* Update hardware min/max frame size */
262static int nic_update_hw_frs(struct nicpf *nic, int new_frs, int vf)
263{
Sunil Goutham712c3182016-11-15 17:37:36 +0530264 int bgx, lmac, lmac_cnt;
265 u64 lmac_credits;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700266
Sunil Goutham712c3182016-11-15 17:37:36 +0530267 if ((new_frs > NIC_HW_MAX_FRS) || (new_frs < NIC_HW_MIN_FRS))
268 return 1;
269
270 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
271 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
272 lmac += bgx * MAX_LMAC_PER_BGX;
273
274 new_frs += VLAN_ETH_HLEN + ETH_FCS_LEN + 4;
275
276 /* Update corresponding LMAC credits */
277 lmac_cnt = bgx_get_lmac_count(nic->node, bgx);
278 lmac_credits = nic_reg_read(nic, NIC_PF_LMAC_0_7_CREDIT + (lmac * 8));
279 lmac_credits &= ~(0xFFFFFULL << 12);
280 lmac_credits |= (((((48 * 1024) / lmac_cnt) - new_frs) / 16) << 12);
281 nic_reg_write(nic, NIC_PF_LMAC_0_7_CREDIT + (lmac * 8), lmac_credits);
282
283 /* Enforce MTU in HW
284 * This config is supported only from 88xx pass 2.0 onwards.
285 */
286 if (!pass1_silicon(nic->pdev))
287 nic_reg_write(nic,
288 NIC_PF_LMAC_0_7_CFG2 + (lmac * 8), new_frs);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700289 return 0;
290}
291
292/* Set minimum transmit packet size */
293static void nic_set_tx_pkt_pad(struct nicpf *nic, int size)
294{
Sunil Goutham949b5332016-08-12 16:51:34 +0530295 int lmac, max_lmac;
296 u16 sdevid;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700297 u64 lmac_cfg;
298
Sunil Goutham57e81d42016-08-30 11:36:26 +0530299 /* There is a issue in HW where-in while sending GSO sized
300 * pkts as part of TSO, if pkt len falls below this size
301 * NIC will zero PAD packet and also updates IP total length.
302 * Hence set this value to lessthan min pkt size of MAC+IP+TCP
303 * headers, BGX will do the padding to transmit 64 byte pkt.
304 */
305 if (size > 52)
306 size = 52;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700307
Sunil Goutham949b5332016-08-12 16:51:34 +0530308 pci_read_config_word(nic->pdev, PCI_SUBSYSTEM_ID, &sdevid);
309 /* 81xx's RGX has only one LMAC */
310 if (sdevid == PCI_SUBSYS_DEVID_81XX_NIC_PF)
311 max_lmac = ((nic->hw->bgx_cnt - 1) * MAX_LMAC_PER_BGX) + 1;
312 else
313 max_lmac = nic->hw->bgx_cnt * MAX_LMAC_PER_BGX;
314
315 for (lmac = 0; lmac < max_lmac; lmac++) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700316 lmac_cfg = nic_reg_read(nic, NIC_PF_LMAC_0_7_CFG | (lmac << 3));
317 lmac_cfg &= ~(0xF << 2);
318 lmac_cfg |= ((size / 4) << 2);
319 nic_reg_write(nic, NIC_PF_LMAC_0_7_CFG | (lmac << 3), lmac_cfg);
320 }
321}
322
323/* Function to check number of LMACs present and set VF::LMAC mapping.
324 * Mapping will be used while initializing channels.
325 */
326static void nic_set_lmac_vf_mapping(struct nicpf *nic)
327{
328 unsigned bgx_map = bgx_get_map(nic->node);
329 int bgx, next_bgx_lmac = 0;
330 int lmac, lmac_cnt = 0;
331 u64 lmac_credit;
332
333 nic->num_vf_en = 0;
334
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530335 for (bgx = 0; bgx < nic->hw->bgx_cnt; bgx++) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700336 if (!(bgx_map & (1 << bgx)))
337 continue;
338 lmac_cnt = bgx_get_lmac_count(nic->node, bgx);
339 for (lmac = 0; lmac < lmac_cnt; lmac++)
340 nic->vf_lmac_map[next_bgx_lmac++] =
341 NIC_SET_VF_LMAC_MAP(bgx, lmac);
342 nic->num_vf_en += lmac_cnt;
343
344 /* Program LMAC credits */
345 lmac_credit = (1ull << 1); /* channel credit enable */
346 lmac_credit |= (0x1ff << 2); /* Max outstanding pkt count */
347 /* 48KB BGX Tx buffer size, each unit is of size 16bytes */
348 lmac_credit |= (((((48 * 1024) / lmac_cnt) -
349 NIC_HW_MAX_FRS) / 16) << 12);
350 lmac = bgx * MAX_LMAC_PER_BGX;
351 for (; lmac < lmac_cnt + (bgx * MAX_LMAC_PER_BGX); lmac++)
352 nic_reg_write(nic,
353 NIC_PF_LMAC_0_7_CREDIT + (lmac * 8),
354 lmac_credit);
Sunil Goutham64658592016-08-12 16:51:33 +0530355
356 /* On CN81XX there are only 8 VFs but max possible no of
357 * interfaces are 9.
358 */
359 if (nic->num_vf_en >= pci_sriov_get_totalvfs(nic->pdev)) {
360 nic->num_vf_en = pci_sriov_get_totalvfs(nic->pdev);
361 break;
362 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700363 }
364}
365
Sunil Goutham949b5332016-08-12 16:51:34 +0530366static void nic_free_lmacmem(struct nicpf *nic)
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530367{
Sunil Goutham949b5332016-08-12 16:51:34 +0530368 kfree(nic->vf_lmac_map);
369 kfree(nic->link);
370 kfree(nic->duplex);
371 kfree(nic->speed);
372}
373
374static int nic_get_hw_info(struct nicpf *nic)
375{
376 u8 max_lmac;
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530377 u16 sdevid;
378 struct hw_info *hw = nic->hw;
379
380 pci_read_config_word(nic->pdev, PCI_SUBSYSTEM_ID, &sdevid);
381
382 switch (sdevid) {
383 case PCI_SUBSYS_DEVID_88XX_NIC_PF:
384 hw->bgx_cnt = MAX_BGX_PER_CN88XX;
385 hw->chans_per_lmac = 16;
386 hw->chans_per_bgx = 128;
387 hw->cpi_cnt = 2048;
388 hw->rssi_cnt = 4096;
389 hw->rss_ind_tbl_size = NIC_MAX_RSS_IDR_TBL_SIZE;
390 hw->tl3_cnt = 256;
391 hw->tl2_cnt = 64;
392 hw->tl1_cnt = 2;
393 hw->tl1_per_bgx = true;
394 break;
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530395 case PCI_SUBSYS_DEVID_81XX_NIC_PF:
396 hw->bgx_cnt = MAX_BGX_PER_CN81XX;
397 hw->chans_per_lmac = 8;
398 hw->chans_per_bgx = 32;
399 hw->chans_per_rgx = 8;
400 hw->chans_per_lbk = 24;
401 hw->cpi_cnt = 512;
402 hw->rssi_cnt = 256;
403 hw->rss_ind_tbl_size = 32; /* Max RSSI / Max interfaces */
404 hw->tl3_cnt = 64;
405 hw->tl2_cnt = 16;
406 hw->tl1_cnt = 10;
407 hw->tl1_per_bgx = false;
408 break;
409 case PCI_SUBSYS_DEVID_83XX_NIC_PF:
410 hw->bgx_cnt = MAX_BGX_PER_CN83XX;
411 hw->chans_per_lmac = 8;
412 hw->chans_per_bgx = 32;
413 hw->chans_per_lbk = 64;
414 hw->cpi_cnt = 2048;
415 hw->rssi_cnt = 1024;
416 hw->rss_ind_tbl_size = 64; /* Max RSSI / Max interfaces */
417 hw->tl3_cnt = 256;
418 hw->tl2_cnt = 64;
419 hw->tl1_cnt = 18;
420 hw->tl1_per_bgx = false;
421 break;
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530422 }
423 hw->tl4_cnt = MAX_QUEUES_PER_QSET * pci_sriov_get_totalvfs(nic->pdev);
Sunil Goutham949b5332016-08-12 16:51:34 +0530424
425 /* Allocate memory for LMAC tracking elements */
426 max_lmac = hw->bgx_cnt * MAX_LMAC_PER_BGX;
427 nic->vf_lmac_map = kmalloc_array(max_lmac, sizeof(u8), GFP_KERNEL);
428 if (!nic->vf_lmac_map)
429 goto error;
430 nic->link = kmalloc_array(max_lmac, sizeof(u8), GFP_KERNEL);
431 if (!nic->link)
432 goto error;
433 nic->duplex = kmalloc_array(max_lmac, sizeof(u8), GFP_KERNEL);
434 if (!nic->duplex)
435 goto error;
436 nic->speed = kmalloc_array(max_lmac, sizeof(u32), GFP_KERNEL);
437 if (!nic->speed)
438 goto error;
439 return 0;
440
441error:
442 nic_free_lmacmem(nic);
443 return -ENOMEM;
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530444}
445
Sunil Goutham4863dea2015-05-26 19:20:15 -0700446#define BGX0_BLOCK 8
447#define BGX1_BLOCK 9
448
Sunil Goutham949b5332016-08-12 16:51:34 +0530449static int nic_init_hw(struct nicpf *nic)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700450{
Sunil Goutham949b5332016-08-12 16:51:34 +0530451 int i, err;
Sunil Goutham4c0b6eaf2016-02-24 16:40:50 +0530452 u64 cqm_cfg;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700453
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530454 /* Get HW capability info */
Sunil Goutham949b5332016-08-12 16:51:34 +0530455 err = nic_get_hw_info(nic);
456 if (err)
457 return err;
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530458
Sunil Goutham4863dea2015-05-26 19:20:15 -0700459 /* Enable NIC HW block */
460 nic_reg_write(nic, NIC_PF_CFG, 0x3);
461
462 /* Enable backpressure */
463 nic_reg_write(nic, NIC_PF_BP_CFG, (1ULL << 6) | 0x03);
464
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530465 /* TNS and TNS bypass modes are present only on 88xx */
466 if (nic->pdev->subsystem_device == PCI_SUBSYS_DEVID_88XX_NIC_PF) {
467 /* Disable TNS mode on both interfaces */
468 nic_reg_write(nic, NIC_PF_INTF_0_1_SEND_CFG,
469 (NIC_TNS_BYPASS_MODE << 7) | BGX0_BLOCK);
470 nic_reg_write(nic, NIC_PF_INTF_0_1_SEND_CFG | (1 << 8),
471 (NIC_TNS_BYPASS_MODE << 7) | BGX1_BLOCK);
472 }
473
Sunil Goutham4863dea2015-05-26 19:20:15 -0700474 nic_reg_write(nic, NIC_PF_INTF_0_1_BP_CFG,
475 (1ULL << 63) | BGX0_BLOCK);
476 nic_reg_write(nic, NIC_PF_INTF_0_1_BP_CFG + (1 << 8),
477 (1ULL << 63) | BGX1_BLOCK);
478
479 /* PKIND configuration */
480 nic->pkind.minlen = 0;
Sunil Goutham712c3182016-11-15 17:37:36 +0530481 nic->pkind.maxlen = NIC_HW_MAX_FRS + VLAN_ETH_HLEN + ETH_FCS_LEN + 4;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700482 nic->pkind.lenerr_en = 1;
483 nic->pkind.rx_hdr = 0;
484 nic->pkind.hdr_sl = 0;
485
486 for (i = 0; i < NIC_MAX_PKIND; i++)
487 nic_reg_write(nic, NIC_PF_PKIND_0_15_CFG | (i << 3),
488 *(u64 *)&nic->pkind);
489
490 nic_set_tx_pkt_pad(nic, NIC_HW_MIN_FRS);
491
492 /* Timer config */
493 nic_reg_write(nic, NIC_PF_INTR_TIMER_CFG, NICPF_CLK_PER_INT_TICK);
Sunil Gouthamaa2e2592015-08-30 12:29:13 +0300494
495 /* Enable VLAN ethertype matching and stripping */
496 nic_reg_write(nic, NIC_PF_RX_ETYPE_0_7,
497 (2 << 19) | (ETYPE_ALG_VLAN_STRIP << 16) | ETH_P_8021Q);
Sunil Goutham4c0b6eaf2016-02-24 16:40:50 +0530498
499 /* Check if HW expected value is higher (could be in future chips) */
500 cqm_cfg = nic_reg_read(nic, NIC_PF_CQM_CFG);
501 if (cqm_cfg < NICPF_CQM_MIN_DROP_LEVEL)
502 nic_reg_write(nic, NIC_PF_CQM_CFG, NICPF_CQM_MIN_DROP_LEVEL);
Sunil Goutham949b5332016-08-12 16:51:34 +0530503
504 return 0;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700505}
506
507/* Channel parse index configuration */
508static void nic_config_cpi(struct nicpf *nic, struct cpi_cfg_msg *cfg)
509{
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530510 struct hw_info *hw = nic->hw;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700511 u32 vnic, bgx, lmac, chan;
512 u32 padd, cpi_count = 0;
513 u64 cpi_base, cpi, rssi_base, rssi;
514 u8 qset, rq_idx = 0;
515
516 vnic = cfg->vf_id;
517 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vnic]);
518 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vnic]);
519
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530520 chan = (lmac * hw->chans_per_lmac) + (bgx * hw->chans_per_bgx);
Sunil Goutham64658592016-08-12 16:51:33 +0530521 cpi_base = vnic * NIC_MAX_CPI_PER_LMAC;
522 rssi_base = vnic * hw->rss_ind_tbl_size;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700523
524 /* Rx channel configuration */
525 nic_reg_write(nic, NIC_PF_CHAN_0_255_RX_BP_CFG | (chan << 3),
526 (1ull << 63) | (vnic << 0));
527 nic_reg_write(nic, NIC_PF_CHAN_0_255_RX_CFG | (chan << 3),
528 ((u64)cfg->cpi_alg << 62) | (cpi_base << 48));
529
530 if (cfg->cpi_alg == CPI_ALG_NONE)
531 cpi_count = 1;
532 else if (cfg->cpi_alg == CPI_ALG_VLAN) /* 3 bits of PCP */
533 cpi_count = 8;
534 else if (cfg->cpi_alg == CPI_ALG_VLAN16) /* 3 bits PCP + DEI */
535 cpi_count = 16;
536 else if (cfg->cpi_alg == CPI_ALG_DIFF) /* 6bits DSCP */
537 cpi_count = NIC_MAX_CPI_PER_LMAC;
538
539 /* RSS Qset, Qidx mapping */
540 qset = cfg->vf_id;
541 rssi = rssi_base;
542 for (; rssi < (rssi_base + cfg->rq_cnt); rssi++) {
543 nic_reg_write(nic, NIC_PF_RSSI_0_4097_RQ | (rssi << 3),
544 (qset << 3) | rq_idx);
545 rq_idx++;
546 }
547
548 rssi = 0;
549 cpi = cpi_base;
550 for (; cpi < (cpi_base + cpi_count); cpi++) {
551 /* Determine port to channel adder */
552 if (cfg->cpi_alg != CPI_ALG_DIFF)
553 padd = cpi % cpi_count;
554 else
555 padd = cpi % 8; /* 3 bits CS out of 6bits DSCP */
556
557 /* Leave RSS_SIZE as '0' to disable RSS */
Sunil Goutham40fb5f82015-12-10 13:25:19 +0530558 if (pass1_silicon(nic->pdev)) {
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700559 nic_reg_write(nic, NIC_PF_CPI_0_2047_CFG | (cpi << 3),
560 (vnic << 24) | (padd << 16) |
561 (rssi_base + rssi));
562 } else {
563 /* Set MPI_ALG to '0' to disable MCAM parsing */
564 nic_reg_write(nic, NIC_PF_CPI_0_2047_CFG | (cpi << 3),
565 (padd << 16));
566 /* MPI index is same as CPI if MPI_ALG is not enabled */
567 nic_reg_write(nic, NIC_PF_MPI_0_2047_CFG | (cpi << 3),
568 (vnic << 24) | (rssi_base + rssi));
569 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700570
571 if ((rssi + 1) >= cfg->rq_cnt)
572 continue;
573
574 if (cfg->cpi_alg == CPI_ALG_VLAN)
575 rssi++;
576 else if (cfg->cpi_alg == CPI_ALG_VLAN16)
577 rssi = ((cpi - cpi_base) & 0xe) >> 1;
578 else if (cfg->cpi_alg == CPI_ALG_DIFF)
579 rssi = ((cpi - cpi_base) & 0x38) >> 3;
580 }
581 nic->cpi_base[cfg->vf_id] = cpi_base;
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700582 nic->rssi_base[cfg->vf_id] = rssi_base;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700583}
584
585/* Responsds to VF with its RSS indirection table size */
586static void nic_send_rss_size(struct nicpf *nic, int vf)
587{
588 union nic_mbx mbx = {};
589 u64 *msg;
590
591 msg = (u64 *)&mbx;
592
593 mbx.rss_size.msg = NIC_MBOX_MSG_RSS_SIZE;
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530594 mbx.rss_size.ind_tbl_size = nic->hw->rss_ind_tbl_size;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700595 nic_send_msg_to_vf(nic, vf, &mbx);
596}
597
598/* Receive side scaling configuration
599 * configure:
600 * - RSS index
601 * - indir table i.e hash::RQ mapping
602 * - no of hash bits to consider
603 */
604static void nic_config_rss(struct nicpf *nic, struct rss_cfg_msg *cfg)
605{
606 u8 qset, idx = 0;
607 u64 cpi_cfg, cpi_base, rssi_base, rssi;
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700608 u64 idx_addr;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700609
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700610 rssi_base = nic->rssi_base[cfg->vf_id] + cfg->tbl_offset;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700611
612 rssi = rssi_base;
613 qset = cfg->vf_id;
614
615 for (; rssi < (rssi_base + cfg->tbl_len); rssi++) {
Sunil Goutham92dc8762015-08-30 12:29:15 +0300616 u8 svf = cfg->ind_tbl[idx] >> 3;
617
618 if (svf)
619 qset = nic->vf_sqs[cfg->vf_id][svf - 1];
620 else
621 qset = cfg->vf_id;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700622 nic_reg_write(nic, NIC_PF_RSSI_0_4097_RQ | (rssi << 3),
623 (qset << 3) | (cfg->ind_tbl[idx] & 0x7));
624 idx++;
625 }
626
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700627 cpi_base = nic->cpi_base[cfg->vf_id];
Sunil Goutham40fb5f82015-12-10 13:25:19 +0530628 if (pass1_silicon(nic->pdev))
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700629 idx_addr = NIC_PF_CPI_0_2047_CFG;
630 else
631 idx_addr = NIC_PF_MPI_0_2047_CFG;
632 cpi_cfg = nic_reg_read(nic, idx_addr | (cpi_base << 3));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700633 cpi_cfg &= ~(0xFULL << 20);
634 cpi_cfg |= (cfg->hash_bits << 20);
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700635 nic_reg_write(nic, idx_addr | (cpi_base << 3), cpi_cfg);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700636}
637
638/* 4 level transmit side scheduler configutation
639 * for TNS bypass mode
640 *
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530641 * Sample configuration for SQ0 on 88xx
Sunil Goutham4863dea2015-05-26 19:20:15 -0700642 * VNIC0-SQ0 -> TL4(0) -> TL3[0] -> TL2[0] -> TL1[0] -> BGX0
643 * VNIC1-SQ0 -> TL4(8) -> TL3[2] -> TL2[0] -> TL1[0] -> BGX0
644 * VNIC2-SQ0 -> TL4(16) -> TL3[4] -> TL2[1] -> TL1[0] -> BGX0
645 * VNIC3-SQ0 -> TL4(24) -> TL3[6] -> TL2[1] -> TL1[0] -> BGX0
646 * VNIC4-SQ0 -> TL4(512) -> TL3[128] -> TL2[32] -> TL1[1] -> BGX1
647 * VNIC5-SQ0 -> TL4(520) -> TL3[130] -> TL2[32] -> TL1[1] -> BGX1
648 * VNIC6-SQ0 -> TL4(528) -> TL3[132] -> TL2[33] -> TL1[1] -> BGX1
649 * VNIC7-SQ0 -> TL4(536) -> TL3[134] -> TL2[33] -> TL1[1] -> BGX1
650 */
Sunil Goutham92dc8762015-08-30 12:29:15 +0300651static void nic_tx_channel_cfg(struct nicpf *nic, u8 vnic,
652 struct sq_cfg_msg *sq)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700653{
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530654 struct hw_info *hw = nic->hw;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700655 u32 bgx, lmac, chan;
656 u32 tl2, tl3, tl4;
657 u32 rr_quantum;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300658 u8 sq_idx = sq->sq_num;
659 u8 pqs_vnic;
Sunil Goutham3e29adb2016-06-27 15:30:03 +0530660 int svf;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700661
Sunil Goutham92dc8762015-08-30 12:29:15 +0300662 if (sq->sqs_mode)
663 pqs_vnic = nic->pqs_vf[vnic];
664 else
665 pqs_vnic = vnic;
666
667 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[pqs_vnic]);
668 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[pqs_vnic]);
669
Sunil Goutham4863dea2015-05-26 19:20:15 -0700670 /* 24 bytes for FCS, IPG and preamble */
671 rr_quantum = ((NIC_HW_MAX_FRS + 24) / 4);
672
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530673 /* For 88xx 0-511 TL4 transmits via BGX0 and
674 * 512-1023 TL4s transmit via BGX1.
675 */
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530676 if (hw->tl1_per_bgx) {
677 tl4 = bgx * (hw->tl4_cnt / hw->bgx_cnt);
678 if (!sq->sqs_mode) {
679 tl4 += (lmac * MAX_QUEUES_PER_QSET);
680 } else {
681 for (svf = 0; svf < MAX_SQS_PER_VF; svf++) {
682 if (nic->vf_sqs[pqs_vnic][svf] == vnic)
683 break;
684 }
685 tl4 += (MAX_LMAC_PER_BGX * MAX_QUEUES_PER_QSET);
686 tl4 += (lmac * MAX_QUEUES_PER_QSET * MAX_SQS_PER_VF);
687 tl4 += (svf * MAX_QUEUES_PER_QSET);
Sunil Goutham3e29adb2016-06-27 15:30:03 +0530688 }
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530689 } else {
690 tl4 = (vnic * MAX_QUEUES_PER_QSET);
Sunil Goutham3e29adb2016-06-27 15:30:03 +0530691 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700692 tl4 += sq_idx;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300693
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530694 tl3 = tl4 / (hw->tl4_cnt / hw->tl3_cnt);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700695 nic_reg_write(nic, NIC_PF_QSET_0_127_SQ_0_7_CFG2 |
696 ((u64)vnic << NIC_QS_ID_SHIFT) |
697 ((u32)sq_idx << NIC_Q_NUM_SHIFT), tl4);
698 nic_reg_write(nic, NIC_PF_TL4_0_1023_CFG | (tl4 << 3),
699 ((u64)vnic << 27) | ((u32)sq_idx << 24) | rr_quantum);
700
701 nic_reg_write(nic, NIC_PF_TL3_0_255_CFG | (tl3 << 3), rr_quantum);
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530702
703 /* On 88xx 0-127 channels are for BGX0 and
704 * 127-255 channels for BGX1.
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530705 *
706 * On 81xx/83xx TL3_CHAN reg should be configured with channel
707 * within LMAC i.e 0-7 and not the actual channel number like on 88xx
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530708 */
709 chan = (lmac * hw->chans_per_lmac) + (bgx * hw->chans_per_bgx);
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530710 if (hw->tl1_per_bgx)
711 nic_reg_write(nic, NIC_PF_TL3_0_255_CHAN | (tl3 << 3), chan);
712 else
713 nic_reg_write(nic, NIC_PF_TL3_0_255_CHAN | (tl3 << 3), 0);
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530714
Sunil Goutham4863dea2015-05-26 19:20:15 -0700715 /* Enable backpressure on the channel */
716 nic_reg_write(nic, NIC_PF_CHAN_0_255_TX_CFG | (chan << 3), 1);
717
718 tl2 = tl3 >> 2;
719 nic_reg_write(nic, NIC_PF_TL3A_0_63_CFG | (tl2 << 3), tl2);
720 nic_reg_write(nic, NIC_PF_TL2_0_63_CFG | (tl2 << 3), rr_quantum);
721 /* No priorities as of now */
722 nic_reg_write(nic, NIC_PF_TL2_0_63_PRI | (tl2 << 3), 0x00);
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530723
724 /* Unlike 88xx where TL2s 0-31 transmits to TL1 '0' and rest to TL1 '1'
725 * on 81xx/83xx TL2 needs to be configured to transmit to one of the
726 * possible LMACs.
727 *
728 * This register doesn't exist on 88xx.
729 */
730 if (!hw->tl1_per_bgx)
731 nic_reg_write(nic, NIC_PF_TL2_LMAC | (tl2 << 3),
732 lmac + (bgx * MAX_LMAC_PER_BGX));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700733}
734
Sunil Goutham92dc8762015-08-30 12:29:15 +0300735/* Send primary nicvf pointer to secondary QS's VF */
736static void nic_send_pnicvf(struct nicpf *nic, int sqs)
737{
738 union nic_mbx mbx = {};
739
740 mbx.nicvf.msg = NIC_MBOX_MSG_PNICVF_PTR;
741 mbx.nicvf.nicvf = nic->nicvf[nic->pqs_vf[sqs]];
742 nic_send_msg_to_vf(nic, sqs, &mbx);
743}
744
745/* Send SQS's nicvf pointer to primary QS's VF */
746static void nic_send_snicvf(struct nicpf *nic, struct nicvf_ptr *nicvf)
747{
748 union nic_mbx mbx = {};
749 int sqs_id = nic->vf_sqs[nicvf->vf_id][nicvf->sqs_id];
750
751 mbx.nicvf.msg = NIC_MBOX_MSG_SNICVF_PTR;
752 mbx.nicvf.sqs_id = nicvf->sqs_id;
753 mbx.nicvf.nicvf = nic->nicvf[sqs_id];
754 nic_send_msg_to_vf(nic, nicvf->vf_id, &mbx);
755}
756
757/* Find next available Qset that can be assigned as a
758 * secondary Qset to a VF.
759 */
760static int nic_nxt_avail_sqs(struct nicpf *nic)
761{
762 int sqs;
763
764 for (sqs = 0; sqs < nic->num_sqs_en; sqs++) {
765 if (!nic->sqs_used[sqs])
766 nic->sqs_used[sqs] = true;
767 else
768 continue;
769 return sqs + nic->num_vf_en;
770 }
771 return -1;
772}
773
774/* Allocate additional Qsets for requested VF */
775static void nic_alloc_sqs(struct nicpf *nic, struct sqs_alloc *sqs)
776{
777 union nic_mbx mbx = {};
778 int idx, alloc_qs = 0;
779 int sqs_id;
780
781 if (!nic->num_sqs_en)
782 goto send_mbox;
783
784 for (idx = 0; idx < sqs->qs_count; idx++) {
785 sqs_id = nic_nxt_avail_sqs(nic);
786 if (sqs_id < 0)
787 break;
788 nic->vf_sqs[sqs->vf_id][idx] = sqs_id;
789 nic->pqs_vf[sqs_id] = sqs->vf_id;
790 alloc_qs++;
791 }
792
793send_mbox:
794 mbx.sqs_alloc.msg = NIC_MBOX_MSG_ALLOC_SQS;
795 mbx.sqs_alloc.vf_id = sqs->vf_id;
796 mbx.sqs_alloc.qs_count = alloc_qs;
797 nic_send_msg_to_vf(nic, sqs->vf_id, &mbx);
798}
799
Sunil Gouthamd77a2382015-08-30 12:29:16 +0300800static int nic_config_loopback(struct nicpf *nic, struct set_loopback *lbk)
801{
802 int bgx_idx, lmac_idx;
803
Sunil Goutham949b5332016-08-12 16:51:34 +0530804 if (lbk->vf_id >= nic->num_vf_en)
Sunil Gouthamd77a2382015-08-30 12:29:16 +0300805 return -1;
806
807 bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lbk->vf_id]);
808 lmac_idx = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lbk->vf_id]);
809
810 bgx_lmac_internal_loopback(nic->node, bgx_idx, lmac_idx, lbk->enable);
811
Sunil Gouthamd5b2d7a2016-11-24 14:48:02 +0530812 /* Enable moving average calculation.
813 * Keep the LVL/AVG delay to HW enforced minimum so that, not too many
814 * packets sneek in between average calculations.
815 */
816 nic_reg_write(nic, NIC_PF_CQ_AVG_CFG,
817 (BIT_ULL(20) | 0x2ull << 14 | 0x1));
818 nic_reg_write(nic, NIC_PF_RRM_AVG_CFG,
819 (BIT_ULL(20) | 0x3ull << 14 | 0x1));
820
Sunil Gouthamd77a2382015-08-30 12:29:16 +0300821 return 0;
822}
823
Jerin Jacob3458c402016-08-12 16:51:39 +0530824/* Reset statistics counters */
825static int nic_reset_stat_counters(struct nicpf *nic,
826 int vf, struct reset_stat_cfg *cfg)
827{
828 int i, stat, qnum;
829 u64 reg_addr;
830
831 for (i = 0; i < RX_STATS_ENUM_LAST; i++) {
832 if (cfg->rx_stat_mask & BIT(i)) {
833 reg_addr = NIC_PF_VNIC_0_127_RX_STAT_0_13 |
834 (vf << NIC_QS_ID_SHIFT) |
835 (i << 3);
836 nic_reg_write(nic, reg_addr, 0);
837 }
838 }
839
840 for (i = 0; i < TX_STATS_ENUM_LAST; i++) {
841 if (cfg->tx_stat_mask & BIT(i)) {
842 reg_addr = NIC_PF_VNIC_0_127_TX_STAT_0_4 |
843 (vf << NIC_QS_ID_SHIFT) |
844 (i << 3);
845 nic_reg_write(nic, reg_addr, 0);
846 }
847 }
848
849 for (i = 0; i <= 15; i++) {
850 qnum = i >> 1;
851 stat = i & 1 ? 1 : 0;
852 reg_addr = (vf << NIC_QS_ID_SHIFT) |
853 (qnum << NIC_Q_NUM_SHIFT) | (stat << 3);
854 if (cfg->rq_stat_mask & BIT(i)) {
855 reg_addr |= NIC_PF_QSET_0_127_RQ_0_7_STAT_0_1;
856 nic_reg_write(nic, reg_addr, 0);
857 }
858 if (cfg->sq_stat_mask & BIT(i)) {
859 reg_addr |= NIC_PF_QSET_0_127_SQ_0_7_STAT_0_1;
860 nic_reg_write(nic, reg_addr, 0);
861 }
862 }
Sunil Goutham964cb692016-11-15 17:38:16 +0530863
Jerin Jacob3458c402016-08-12 16:51:39 +0530864 return 0;
865}
866
Zyta Szpake22e86e2016-08-12 16:51:42 +0530867static void nic_enable_tunnel_parsing(struct nicpf *nic, int vf)
868{
869 u64 prot_def = (IPV6_PROT << 32) | (IPV4_PROT << 16) | ET_PROT;
870 u64 vxlan_prot_def = (IPV6_PROT_DEF << 32) |
871 (IPV4_PROT_DEF) << 16 | ET_PROT_DEF;
872
873 /* Configure tunnel parsing parameters */
874 nic_reg_write(nic, NIC_PF_RX_GENEVE_DEF,
875 (1ULL << 63 | UDP_GENEVE_PORT_NUM));
876 nic_reg_write(nic, NIC_PF_RX_GENEVE_PROT_DEF,
877 ((7ULL << 61) | prot_def));
878 nic_reg_write(nic, NIC_PF_RX_NVGRE_PROT_DEF,
879 ((7ULL << 61) | prot_def));
880 nic_reg_write(nic, NIC_PF_RX_VXLAN_DEF_0_1,
881 ((1ULL << 63) | UDP_VXLAN_PORT_NUM));
882 nic_reg_write(nic, NIC_PF_RX_VXLAN_PROT_DEF,
883 ((0xfULL << 60) | vxlan_prot_def));
884}
885
Pavel Fedinf406ce42015-12-08 10:37:44 +0300886static void nic_enable_vf(struct nicpf *nic, int vf, bool enable)
887{
888 int bgx, lmac;
889
890 nic->vf_enabled[vf] = enable;
891
892 if (vf >= nic->num_vf_en)
893 return;
894
895 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
896 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
897
898 bgx_lmac_rx_tx_enable(nic->node, bgx, lmac, enable);
899}
900
Sunil Goutham4863dea2015-05-26 19:20:15 -0700901/* Interrupt handler to handle mailbox messages from VFs */
902static void nic_handle_mbx_intr(struct nicpf *nic, int vf)
903{
904 union nic_mbx mbx = {};
905 u64 *mbx_data;
906 u64 mbx_addr;
907 u64 reg_addr;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300908 u64 cfg;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700909 int bgx, lmac;
910 int i;
911 int ret = 0;
912
913 nic->mbx_lock[vf] = true;
914
915 mbx_addr = nic_get_mbx_addr(vf);
916 mbx_data = (u64 *)&mbx;
917
918 for (i = 0; i < NIC_PF_VF_MAILBOX_SIZE; i++) {
919 *mbx_data = nic_reg_read(nic, mbx_addr);
920 mbx_data++;
921 mbx_addr += sizeof(u64);
922 }
923
Radoslaw Biernackiecae29c2016-08-12 16:51:38 +0530924 dev_dbg(&nic->pdev->dev, "%s: Mailbox msg 0x%02x from VF%d\n",
Sunil Goutham4863dea2015-05-26 19:20:15 -0700925 __func__, mbx.msg.msg, vf);
926 switch (mbx.msg.msg) {
927 case NIC_MBOX_MSG_READY:
928 nic_mbx_send_ready(nic, vf);
Sunil Goutham949b5332016-08-12 16:51:34 +0530929 if (vf < nic->num_vf_en) {
Sunil Goutham92dc8762015-08-30 12:29:15 +0300930 nic->link[vf] = 0;
931 nic->duplex[vf] = 0;
932 nic->speed[vf] = 0;
933 }
Radoslaw Biernackiecae29c2016-08-12 16:51:38 +0530934 goto unlock;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700935 case NIC_MBOX_MSG_QS_CFG:
936 reg_addr = NIC_PF_QSET_0_127_CFG |
937 (mbx.qs.num << NIC_QS_ID_SHIFT);
Sunil Goutham92dc8762015-08-30 12:29:15 +0300938 cfg = mbx.qs.cfg;
939 /* Check if its a secondary Qset */
940 if (vf >= nic->num_vf_en) {
941 cfg = cfg & (~0x7FULL);
942 /* Assign this Qset to primary Qset's VF */
943 cfg |= nic->pqs_vf[vf];
944 }
945 nic_reg_write(nic, reg_addr, cfg);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700946 break;
947 case NIC_MBOX_MSG_RQ_CFG:
948 reg_addr = NIC_PF_QSET_0_127_RQ_0_7_CFG |
949 (mbx.rq.qs_num << NIC_QS_ID_SHIFT) |
950 (mbx.rq.rq_num << NIC_Q_NUM_SHIFT);
951 nic_reg_write(nic, reg_addr, mbx.rq.cfg);
Sunil Goutham02a72bd2016-08-12 16:51:28 +0530952 /* Enable CQE_RX2_S extension in CQE_RX descriptor.
953 * This gets appended by default on 81xx/83xx chips,
954 * for consistency enabling the same on 88xx pass2
955 * where this is introduced.
956 */
957 if (pass2_silicon(nic->pdev))
958 nic_reg_write(nic, NIC_PF_RX_CFG, 0x01);
Zyta Szpake22e86e2016-08-12 16:51:42 +0530959 if (!pass1_silicon(nic->pdev))
960 nic_enable_tunnel_parsing(nic, vf);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700961 break;
962 case NIC_MBOX_MSG_RQ_BP_CFG:
963 reg_addr = NIC_PF_QSET_0_127_RQ_0_7_BP_CFG |
964 (mbx.rq.qs_num << NIC_QS_ID_SHIFT) |
965 (mbx.rq.rq_num << NIC_Q_NUM_SHIFT);
966 nic_reg_write(nic, reg_addr, mbx.rq.cfg);
967 break;
968 case NIC_MBOX_MSG_RQ_SW_SYNC:
969 ret = nic_rcv_queue_sw_sync(nic);
970 break;
971 case NIC_MBOX_MSG_RQ_DROP_CFG:
972 reg_addr = NIC_PF_QSET_0_127_RQ_0_7_DROP_CFG |
973 (mbx.rq.qs_num << NIC_QS_ID_SHIFT) |
974 (mbx.rq.rq_num << NIC_Q_NUM_SHIFT);
975 nic_reg_write(nic, reg_addr, mbx.rq.cfg);
976 break;
977 case NIC_MBOX_MSG_SQ_CFG:
978 reg_addr = NIC_PF_QSET_0_127_SQ_0_7_CFG |
979 (mbx.sq.qs_num << NIC_QS_ID_SHIFT) |
980 (mbx.sq.sq_num << NIC_Q_NUM_SHIFT);
981 nic_reg_write(nic, reg_addr, mbx.sq.cfg);
Sunil Goutham92dc8762015-08-30 12:29:15 +0300982 nic_tx_channel_cfg(nic, mbx.qs.num, &mbx.sq);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700983 break;
984 case NIC_MBOX_MSG_SET_MAC:
Radoslaw Biernackiecae29c2016-08-12 16:51:38 +0530985 if (vf >= nic->num_vf_en) {
986 ret = -1; /* NACK */
Sunil Goutham92dc8762015-08-30 12:29:15 +0300987 break;
Radoslaw Biernackiecae29c2016-08-12 16:51:38 +0530988 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700989 lmac = mbx.mac.vf_id;
990 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lmac]);
991 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lmac]);
Aleksey Makarove610cb32015-06-02 11:00:21 -0700992 bgx_set_lmac_mac(nic->node, bgx, lmac, mbx.mac.mac_addr);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700993 break;
994 case NIC_MBOX_MSG_SET_MAX_FRS:
995 ret = nic_update_hw_frs(nic, mbx.frs.max_frs,
996 mbx.frs.vf_id);
997 break;
998 case NIC_MBOX_MSG_CPI_CFG:
999 nic_config_cpi(nic, &mbx.cpi_cfg);
1000 break;
1001 case NIC_MBOX_MSG_RSS_SIZE:
1002 nic_send_rss_size(nic, vf);
1003 goto unlock;
1004 case NIC_MBOX_MSG_RSS_CFG:
1005 case NIC_MBOX_MSG_RSS_CFG_CONT:
1006 nic_config_rss(nic, &mbx.rss_cfg);
1007 break;
1008 case NIC_MBOX_MSG_CFG_DONE:
1009 /* Last message of VF config msg sequence */
Pavel Fedinf406ce42015-12-08 10:37:44 +03001010 nic_enable_vf(nic, vf, true);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001011 goto unlock;
1012 case NIC_MBOX_MSG_SHUTDOWN:
1013 /* First msg in VF teardown sequence */
Sunil Goutham92dc8762015-08-30 12:29:15 +03001014 if (vf >= nic->num_vf_en)
1015 nic->sqs_used[vf - nic->num_vf_en] = false;
1016 nic->pqs_vf[vf] = 0;
Pavel Fedinf406ce42015-12-08 10:37:44 +03001017 nic_enable_vf(nic, vf, false);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001018 break;
Sunil Goutham92dc8762015-08-30 12:29:15 +03001019 case NIC_MBOX_MSG_ALLOC_SQS:
1020 nic_alloc_sqs(nic, &mbx.sqs_alloc);
1021 goto unlock;
1022 case NIC_MBOX_MSG_NICVF_PTR:
1023 nic->nicvf[vf] = mbx.nicvf.nicvf;
1024 break;
1025 case NIC_MBOX_MSG_PNICVF_PTR:
1026 nic_send_pnicvf(nic, vf);
1027 goto unlock;
1028 case NIC_MBOX_MSG_SNICVF_PTR:
1029 nic_send_snicvf(nic, &mbx.nicvf);
1030 goto unlock;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001031 case NIC_MBOX_MSG_BGX_STATS:
1032 nic_get_bgx_stats(nic, &mbx.bgx_stats);
1033 goto unlock;
Sunil Gouthamd77a2382015-08-30 12:29:16 +03001034 case NIC_MBOX_MSG_LOOPBACK:
1035 ret = nic_config_loopback(nic, &mbx.lbk);
1036 break;
Jerin Jacob3458c402016-08-12 16:51:39 +05301037 case NIC_MBOX_MSG_RESET_STAT_COUNTER:
1038 ret = nic_reset_stat_counters(nic, vf, &mbx.reset_stat);
1039 break;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001040 default:
1041 dev_err(&nic->pdev->dev,
1042 "Invalid msg from VF%d, msg 0x%x\n", vf, mbx.msg.msg);
1043 break;
1044 }
1045
Radoslaw Biernackiecae29c2016-08-12 16:51:38 +05301046 if (!ret) {
Sunil Goutham4863dea2015-05-26 19:20:15 -07001047 nic_mbx_send_ack(nic, vf);
Radoslaw Biernackiecae29c2016-08-12 16:51:38 +05301048 } else if (mbx.msg.msg != NIC_MBOX_MSG_READY) {
1049 dev_err(&nic->pdev->dev, "NACK for MBOX 0x%02x from VF %d\n",
1050 mbx.msg.msg, vf);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001051 nic_mbx_send_nack(nic, vf);
Radoslaw Biernackiecae29c2016-08-12 16:51:38 +05301052 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001053unlock:
1054 nic->mbx_lock[vf] = false;
1055}
1056
Sunil Goutham52358aa2016-08-12 16:51:29 +05301057static irqreturn_t nic_mbx_intr_handler(int irq, void *nic_irq)
Sunil Goutham4863dea2015-05-26 19:20:15 -07001058{
Sunil Goutham52358aa2016-08-12 16:51:29 +05301059 struct nicpf *nic = (struct nicpf *)nic_irq;
1060 int mbx;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001061 u64 intr;
1062 u8 vf, vf_per_mbx_reg = 64;
1063
Sunil Goutham52358aa2016-08-12 16:51:29 +05301064 if (irq == nic->msix_entries[NIC_PF_INTR_ID_MBOX0].vector)
1065 mbx = 0;
1066 else
1067 mbx = 1;
1068
Sunil Goutham4863dea2015-05-26 19:20:15 -07001069 intr = nic_reg_read(nic, NIC_PF_MAILBOX_INT + (mbx << 3));
1070 dev_dbg(&nic->pdev->dev, "PF interrupt Mbox%d 0x%llx\n", mbx, intr);
1071 for (vf = 0; vf < vf_per_mbx_reg; vf++) {
1072 if (intr & (1ULL << vf)) {
1073 dev_dbg(&nic->pdev->dev, "Intr from VF %d\n",
1074 vf + (mbx * vf_per_mbx_reg));
Sunil Goutham92dc8762015-08-30 12:29:15 +03001075
Sunil Goutham4863dea2015-05-26 19:20:15 -07001076 nic_handle_mbx_intr(nic, vf + (mbx * vf_per_mbx_reg));
1077 nic_clear_mbx_intr(nic, vf, mbx);
1078 }
1079 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001080 return IRQ_HANDLED;
1081}
1082
1083static int nic_enable_msix(struct nicpf *nic)
1084{
1085 int i, ret;
1086
Sunil Goutham52358aa2016-08-12 16:51:29 +05301087 nic->num_vec = pci_msix_vec_count(nic->pdev);
1088
1089 nic->msix_entries = kmalloc_array(nic->num_vec,
1090 sizeof(struct msix_entry),
1091 GFP_KERNEL);
1092 if (!nic->msix_entries)
1093 return -ENOMEM;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001094
1095 for (i = 0; i < nic->num_vec; i++)
1096 nic->msix_entries[i].entry = i;
1097
1098 ret = pci_enable_msix(nic->pdev, nic->msix_entries, nic->num_vec);
1099 if (ret) {
1100 dev_err(&nic->pdev->dev,
Sunil Goutham52358aa2016-08-12 16:51:29 +05301101 "Request for #%d msix vectors failed, returned %d\n",
1102 nic->num_vec, ret);
1103 kfree(nic->msix_entries);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001104 return ret;
1105 }
1106
1107 nic->msix_enabled = 1;
1108 return 0;
1109}
1110
1111static void nic_disable_msix(struct nicpf *nic)
1112{
1113 if (nic->msix_enabled) {
1114 pci_disable_msix(nic->pdev);
Sunil Goutham52358aa2016-08-12 16:51:29 +05301115 kfree(nic->msix_entries);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001116 nic->msix_enabled = 0;
1117 nic->num_vec = 0;
1118 }
1119}
1120
1121static void nic_free_all_interrupts(struct nicpf *nic)
1122{
1123 int irq;
1124
1125 for (irq = 0; irq < nic->num_vec; irq++) {
1126 if (nic->irq_allocated[irq])
1127 free_irq(nic->msix_entries[irq].vector, nic);
1128 nic->irq_allocated[irq] = false;
1129 }
1130}
1131
1132static int nic_register_interrupts(struct nicpf *nic)
1133{
Sunil Goutham52358aa2016-08-12 16:51:29 +05301134 int i, ret;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001135
1136 /* Enable MSI-X */
1137 ret = nic_enable_msix(nic);
1138 if (ret)
1139 return ret;
1140
Sunil Goutham52358aa2016-08-12 16:51:29 +05301141 /* Register mailbox interrupt handler */
1142 for (i = NIC_PF_INTR_ID_MBOX0; i < nic->num_vec; i++) {
1143 sprintf(nic->irq_name[i],
1144 "NICPF Mbox%d", (i - NIC_PF_INTR_ID_MBOX0));
Sunil Goutham4863dea2015-05-26 19:20:15 -07001145
Sunil Goutham52358aa2016-08-12 16:51:29 +05301146 ret = request_irq(nic->msix_entries[i].vector,
1147 nic_mbx_intr_handler, 0,
1148 nic->irq_name[i], nic);
1149 if (ret)
1150 goto fail;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001151
Sunil Goutham52358aa2016-08-12 16:51:29 +05301152 nic->irq_allocated[i] = true;
1153 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001154
1155 /* Enable mailbox interrupt */
1156 nic_enable_mbx_intr(nic);
1157 return 0;
1158
1159fail:
1160 dev_err(&nic->pdev->dev, "Request irq failed\n");
1161 nic_free_all_interrupts(nic);
Sunil Goutham52358aa2016-08-12 16:51:29 +05301162 nic_disable_msix(nic);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001163 return ret;
1164}
1165
1166static void nic_unregister_interrupts(struct nicpf *nic)
1167{
1168 nic_free_all_interrupts(nic);
1169 nic_disable_msix(nic);
1170}
1171
Sunil Goutham92dc8762015-08-30 12:29:15 +03001172static int nic_num_sqs_en(struct nicpf *nic, int vf_en)
1173{
1174 int pos, sqs_per_vf = MAX_SQS_PER_VF_SINGLE_NODE;
1175 u16 total_vf;
1176
Sunil Goutham3a397eb2016-08-12 16:51:27 +05301177 /* Secondary Qsets are needed only if CPU count is
1178 * morethan MAX_QUEUES_PER_QSET.
1179 */
1180 if (num_online_cpus() <= MAX_QUEUES_PER_QSET)
1181 return 0;
1182
Sunil Goutham92dc8762015-08-30 12:29:15 +03001183 /* Check if its a multi-node environment */
1184 if (nr_node_ids > 1)
1185 sqs_per_vf = MAX_SQS_PER_VF;
1186
1187 pos = pci_find_ext_capability(nic->pdev, PCI_EXT_CAP_ID_SRIOV);
1188 pci_read_config_word(nic->pdev, (pos + PCI_SRIOV_TOTAL_VF), &total_vf);
1189 return min(total_vf - vf_en, vf_en * sqs_per_vf);
1190}
1191
Sunil Goutham4863dea2015-05-26 19:20:15 -07001192static int nic_sriov_init(struct pci_dev *pdev, struct nicpf *nic)
1193{
1194 int pos = 0;
Sunil Goutham92dc8762015-08-30 12:29:15 +03001195 int vf_en;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001196 int err;
1197 u16 total_vf_cnt;
1198
1199 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1200 if (!pos) {
1201 dev_err(&pdev->dev, "SRIOV capability is not found in PCIe config space\n");
1202 return -ENODEV;
1203 }
1204
1205 pci_read_config_word(pdev, (pos + PCI_SRIOV_TOTAL_VF), &total_vf_cnt);
1206 if (total_vf_cnt < nic->num_vf_en)
1207 nic->num_vf_en = total_vf_cnt;
1208
1209 if (!total_vf_cnt)
1210 return 0;
1211
Sunil Goutham92dc8762015-08-30 12:29:15 +03001212 vf_en = nic->num_vf_en;
1213 nic->num_sqs_en = nic_num_sqs_en(nic, nic->num_vf_en);
1214 vf_en += nic->num_sqs_en;
1215
1216 err = pci_enable_sriov(pdev, vf_en);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001217 if (err) {
1218 dev_err(&pdev->dev, "SRIOV enable failed, num VF is %d\n",
Sunil Goutham92dc8762015-08-30 12:29:15 +03001219 vf_en);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001220 nic->num_vf_en = 0;
1221 return err;
1222 }
1223
1224 dev_info(&pdev->dev, "SRIOV enabled, number of VF available %d\n",
Sunil Goutham92dc8762015-08-30 12:29:15 +03001225 vf_en);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001226
1227 nic->flags |= NIC_SRIOV_ENABLED;
1228 return 0;
1229}
1230
1231/* Poll for BGX LMAC link status and update corresponding VF
1232 * if there is a change, valid only if internal L2 switch
1233 * is not present otherwise VF link is always treated as up
1234 */
1235static void nic_poll_for_link(struct work_struct *work)
1236{
1237 union nic_mbx mbx = {};
1238 struct nicpf *nic;
1239 struct bgx_link_status link;
1240 u8 vf, bgx, lmac;
1241
1242 nic = container_of(work, struct nicpf, dwork.work);
1243
1244 mbx.link_status.msg = NIC_MBOX_MSG_BGX_LINK_CHANGE;
1245
Pavel Fedinf406ce42015-12-08 10:37:44 +03001246 for (vf = 0; vf < nic->num_vf_en; vf++) {
Sunil Goutham4863dea2015-05-26 19:20:15 -07001247 /* Poll only if VF is UP */
1248 if (!nic->vf_enabled[vf])
1249 continue;
1250
1251 /* Get BGX, LMAC indices for the VF */
1252 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
1253 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
1254 /* Get interface link status */
1255 bgx_get_lmac_link_state(nic->node, bgx, lmac, &link);
1256
1257 /* Inform VF only if link status changed */
1258 if (nic->link[vf] == link.link_up)
1259 continue;
1260
1261 if (!nic->mbx_lock[vf]) {
1262 nic->link[vf] = link.link_up;
1263 nic->duplex[vf] = link.duplex;
1264 nic->speed[vf] = link.speed;
1265
1266 /* Send a mbox message to VF with current link status */
1267 mbx.link_status.link_up = link.link_up;
1268 mbx.link_status.duplex = link.duplex;
1269 mbx.link_status.speed = link.speed;
Thanneeru Srinivasulu1cc70252016-11-24 14:48:01 +05301270 mbx.link_status.mac_type = link.mac_type;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001271 nic_send_msg_to_vf(nic, vf, &mbx);
1272 }
1273 }
1274 queue_delayed_work(nic->check_link, &nic->dwork, HZ * 2);
1275}
1276
1277static int nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1278{
1279 struct device *dev = &pdev->dev;
1280 struct nicpf *nic;
1281 int err;
1282
1283 BUILD_BUG_ON(sizeof(union nic_mbx) > 16);
1284
1285 nic = devm_kzalloc(dev, sizeof(*nic), GFP_KERNEL);
1286 if (!nic)
1287 return -ENOMEM;
1288
Sunil Gouthama5c3d492016-08-12 16:51:24 +05301289 nic->hw = devm_kzalloc(dev, sizeof(struct hw_info), GFP_KERNEL);
1290 if (!nic->hw) {
1291 devm_kfree(dev, nic);
1292 return -ENOMEM;
1293 }
1294
Sunil Goutham4863dea2015-05-26 19:20:15 -07001295 pci_set_drvdata(pdev, nic);
1296
1297 nic->pdev = pdev;
1298
1299 err = pci_enable_device(pdev);
1300 if (err) {
1301 dev_err(dev, "Failed to enable PCI device\n");
1302 pci_set_drvdata(pdev, NULL);
1303 return err;
1304 }
1305
1306 err = pci_request_regions(pdev, DRV_NAME);
1307 if (err) {
1308 dev_err(dev, "PCI request regions failed 0x%x\n", err);
1309 goto err_disable_device;
1310 }
1311
1312 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(48));
1313 if (err) {
1314 dev_err(dev, "Unable to get usable DMA configuration\n");
1315 goto err_release_regions;
1316 }
1317
1318 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(48));
1319 if (err) {
1320 dev_err(dev, "Unable to get 48-bit DMA for consistent allocations\n");
1321 goto err_release_regions;
1322 }
1323
1324 /* MAP PF's configuration registers */
1325 nic->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
1326 if (!nic->reg_base) {
1327 dev_err(dev, "Cannot map config register space, aborting\n");
1328 err = -ENOMEM;
1329 goto err_release_regions;
1330 }
1331
Robert Richterd768b672015-06-02 11:00:18 -07001332 nic->node = nic_get_node_id(pdev);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001333
Sunil Goutham4863dea2015-05-26 19:20:15 -07001334 /* Initialize hardware */
Sunil Goutham949b5332016-08-12 16:51:34 +05301335 err = nic_init_hw(nic);
1336 if (err)
1337 goto err_release_regions;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001338
Sunil Gouthama5c3d492016-08-12 16:51:24 +05301339 nic_set_lmac_vf_mapping(nic);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001340
1341 /* Register interrupts */
1342 err = nic_register_interrupts(nic);
1343 if (err)
1344 goto err_release_regions;
1345
1346 /* Configure SRIOV */
1347 err = nic_sriov_init(pdev, nic);
1348 if (err)
1349 goto err_unregister_interrupts;
1350
1351 /* Register a physical link status poll fn() */
1352 nic->check_link = alloc_workqueue("check_link_status",
1353 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1354 if (!nic->check_link) {
1355 err = -ENOMEM;
1356 goto err_disable_sriov;
1357 }
1358
1359 INIT_DELAYED_WORK(&nic->dwork, nic_poll_for_link);
1360 queue_delayed_work(nic->check_link, &nic->dwork, 0);
1361
1362 return 0;
1363
1364err_disable_sriov:
1365 if (nic->flags & NIC_SRIOV_ENABLED)
1366 pci_disable_sriov(pdev);
1367err_unregister_interrupts:
1368 nic_unregister_interrupts(nic);
1369err_release_regions:
1370 pci_release_regions(pdev);
1371err_disable_device:
Sunil Goutham949b5332016-08-12 16:51:34 +05301372 nic_free_lmacmem(nic);
Sunil Gouthama5c3d492016-08-12 16:51:24 +05301373 devm_kfree(dev, nic->hw);
1374 devm_kfree(dev, nic);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001375 pci_disable_device(pdev);
1376 pci_set_drvdata(pdev, NULL);
1377 return err;
1378}
1379
1380static void nic_remove(struct pci_dev *pdev)
1381{
1382 struct nicpf *nic = pci_get_drvdata(pdev);
1383
1384 if (nic->flags & NIC_SRIOV_ENABLED)
1385 pci_disable_sriov(pdev);
1386
1387 if (nic->check_link) {
1388 /* Destroy work Queue */
Thanneeru Srinivasulua7b1f532015-12-02 15:36:14 +05301389 cancel_delayed_work_sync(&nic->dwork);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001390 destroy_workqueue(nic->check_link);
1391 }
1392
1393 nic_unregister_interrupts(nic);
1394 pci_release_regions(pdev);
Sunil Gouthama5c3d492016-08-12 16:51:24 +05301395
Sunil Goutham949b5332016-08-12 16:51:34 +05301396 nic_free_lmacmem(nic);
Sunil Gouthama5c3d492016-08-12 16:51:24 +05301397 devm_kfree(&pdev->dev, nic->hw);
1398 devm_kfree(&pdev->dev, nic);
1399
Sunil Goutham4863dea2015-05-26 19:20:15 -07001400 pci_disable_device(pdev);
1401 pci_set_drvdata(pdev, NULL);
1402}
1403
1404static struct pci_driver nic_driver = {
1405 .name = DRV_NAME,
1406 .id_table = nic_id_table,
1407 .probe = nic_probe,
1408 .remove = nic_remove,
1409};
1410
1411static int __init nic_init_module(void)
1412{
1413 pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION);
1414
1415 return pci_register_driver(&nic_driver);
1416}
1417
1418static void __exit nic_cleanup_module(void)
1419{
1420 pci_unregister_driver(&nic_driver);
1421}
1422
1423module_init(nic_init_module);
1424module_exit(nic_cleanup_module);