blob: 955c52261c59f83df43ffc11ccb2630bac323aab [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>
14
15#include "nic_reg.h"
16#include "nic.h"
17#include "q_struct.h"
18#include "thunder_bgx.h"
19
20#define DRV_NAME "thunder-nic"
21#define DRV_VERSION "1.0"
22
Sunil Gouthama5c3d492016-08-12 16:51:24 +053023struct hw_info {
24 u8 bgx_cnt;
25 u8 chans_per_lmac;
26 u8 chans_per_bgx; /* Rx/Tx chans */
Sunil Goutham0025d93e2016-08-12 16:51:26 +053027 u8 chans_per_rgx;
28 u8 chans_per_lbk;
Sunil Gouthama5c3d492016-08-12 16:51:24 +053029 u16 cpi_cnt;
30 u16 rssi_cnt;
31 u16 rss_ind_tbl_size;
32 u16 tl4_cnt;
33 u16 tl3_cnt;
34 u8 tl2_cnt;
35 u8 tl1_cnt;
36 bool tl1_per_bgx; /* TL1 per BGX or per LMAC */
37};
38
Sunil Goutham4863dea2015-05-26 19:20:15 -070039struct nicpf {
40 struct pci_dev *pdev;
Sunil Gouthama5c3d492016-08-12 16:51:24 +053041 struct hw_info *hw;
Sunil Goutham4863dea2015-05-26 19:20:15 -070042 u8 node;
43 unsigned int flags;
44 u8 num_vf_en; /* No of VF enabled */
45 bool vf_enabled[MAX_NUM_VFS_SUPPORTED];
46 void __iomem *reg_base; /* Register start address */
Sunil Goutham92dc8762015-08-30 12:29:15 +030047 u8 num_sqs_en; /* Secondary qsets enabled */
48 u64 nicvf[MAX_NUM_VFS_SUPPORTED];
49 u8 vf_sqs[MAX_NUM_VFS_SUPPORTED][MAX_SQS_PER_VF];
50 u8 pqs_vf[MAX_NUM_VFS_SUPPORTED];
51 bool sqs_used[MAX_NUM_VFS_SUPPORTED];
Sunil Goutham4863dea2015-05-26 19:20:15 -070052 struct pkind_cfg pkind;
53#define NIC_SET_VF_LMAC_MAP(bgx, lmac) (((bgx & 0xF) << 4) | (lmac & 0xF))
54#define NIC_GET_BGX_FROM_VF_LMAC_MAP(map) ((map >> 4) & 0xF)
55#define NIC_GET_LMAC_FROM_VF_LMAC_MAP(map) (map & 0xF)
56 u8 vf_lmac_map[MAX_LMAC];
57 struct delayed_work dwork;
58 struct workqueue_struct *check_link;
59 u8 link[MAX_LMAC];
60 u8 duplex[MAX_LMAC];
61 u32 speed[MAX_LMAC];
62 u16 cpi_base[MAX_NUM_VFS_SUPPORTED];
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -070063 u16 rssi_base[MAX_NUM_VFS_SUPPORTED];
Sunil Goutham4863dea2015-05-26 19:20:15 -070064 bool mbx_lock[MAX_NUM_VFS_SUPPORTED];
65
66 /* MSI-X */
67 bool msix_enabled;
68 u8 num_vec;
Sunil Goutham52358aa2016-08-12 16:51:29 +053069 struct msix_entry *msix_entries;
Sunil Goutham4863dea2015-05-26 19:20:15 -070070 bool irq_allocated[NIC_PF_MSIX_VECTORS];
Sunil Goutham52358aa2016-08-12 16:51:29 +053071 char irq_name[NIC_PF_MSIX_VECTORS][20];
Sunil Goutham4863dea2015-05-26 19:20:15 -070072};
73
74/* Supported devices */
75static const struct pci_device_id nic_id_table[] = {
76 { PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVICE_ID_THUNDER_NIC_PF) },
77 { 0, } /* end of table */
78};
79
80MODULE_AUTHOR("Sunil Goutham");
81MODULE_DESCRIPTION("Cavium Thunder NIC Physical Function Driver");
82MODULE_LICENSE("GPL v2");
83MODULE_VERSION(DRV_VERSION);
84MODULE_DEVICE_TABLE(pci, nic_id_table);
85
86/* The Cavium ThunderX network controller can *only* be found in SoCs
87 * containing the ThunderX ARM64 CPU implementation. All accesses to the device
88 * registers on this platform are implicitly strongly ordered with respect
89 * to memory accesses. So writeq_relaxed() and readq_relaxed() are safe to use
90 * with no memory barriers in this driver. The readq()/writeq() functions add
91 * explicit ordering operation which in this case are redundant, and only
92 * add overhead.
93 */
94
95/* Register read/write APIs */
96static void nic_reg_write(struct nicpf *nic, u64 offset, u64 val)
97{
98 writeq_relaxed(val, nic->reg_base + offset);
99}
100
101static u64 nic_reg_read(struct nicpf *nic, u64 offset)
102{
103 return readq_relaxed(nic->reg_base + offset);
104}
105
106/* PF -> VF mailbox communication APIs */
107static void nic_enable_mbx_intr(struct nicpf *nic)
108{
Sunil Goutham52358aa2016-08-12 16:51:29 +0530109 int vf_cnt = pci_sriov_get_totalvfs(nic->pdev);
110
111#define INTR_MASK(vfs) ((vfs < 64) ? (BIT_ULL(vfs) - 1) : (~0ull))
112
113 /* Clear it, to avoid spurious interrupts (if any) */
114 nic_reg_write(nic, NIC_PF_MAILBOX_INT, INTR_MASK(vf_cnt));
115
116 /* Enable mailbox interrupt for all VFs */
117 nic_reg_write(nic, NIC_PF_MAILBOX_ENA_W1S, INTR_MASK(vf_cnt));
118 /* One mailbox intr enable reg per 64 VFs */
119 if (vf_cnt > 64) {
120 nic_reg_write(nic, NIC_PF_MAILBOX_INT + sizeof(u64),
121 INTR_MASK(vf_cnt - 64));
122 nic_reg_write(nic, NIC_PF_MAILBOX_ENA_W1S + sizeof(u64),
123 INTR_MASK(vf_cnt - 64));
124 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700125}
126
127static void nic_clear_mbx_intr(struct nicpf *nic, int vf, int mbx_reg)
128{
129 nic_reg_write(nic, NIC_PF_MAILBOX_INT + (mbx_reg << 3), BIT_ULL(vf));
130}
131
132static u64 nic_get_mbx_addr(int vf)
133{
134 return NIC_PF_VF_0_127_MAILBOX_0_1 + (vf << NIC_VF_NUM_SHIFT);
135}
136
137/* Send a mailbox message to VF
138 * @vf: vf to which this message to be sent
139 * @mbx: Message to be sent
140 */
141static void nic_send_msg_to_vf(struct nicpf *nic, int vf, union nic_mbx *mbx)
142{
143 void __iomem *mbx_addr = nic->reg_base + nic_get_mbx_addr(vf);
144 u64 *msg = (u64 *)mbx;
145
146 /* In first revision HW, mbox interrupt is triggerred
147 * when PF writes to MBOX(1), in next revisions when
148 * PF writes to MBOX(0)
149 */
Sunil Goutham40fb5f82015-12-10 13:25:19 +0530150 if (pass1_silicon(nic->pdev)) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700151 /* see the comment for nic_reg_write()/nic_reg_read()
152 * functions above
153 */
154 writeq_relaxed(msg[0], mbx_addr);
155 writeq_relaxed(msg[1], mbx_addr + 8);
156 } else {
157 writeq_relaxed(msg[1], mbx_addr + 8);
158 writeq_relaxed(msg[0], mbx_addr);
159 }
160}
161
162/* Responds to VF's READY message with VF's
163 * ID, node, MAC address e.t.c
164 * @vf: VF which sent READY message
165 */
166static void nic_mbx_send_ready(struct nicpf *nic, int vf)
167{
168 union nic_mbx mbx = {};
169 int bgx_idx, lmac;
170 const char *mac;
171
172 mbx.nic_cfg.msg = NIC_MBOX_MSG_READY;
173 mbx.nic_cfg.vf_id = vf;
174
175 mbx.nic_cfg.tns_mode = NIC_TNS_BYPASS_MODE;
176
Sunil Goutham92dc8762015-08-30 12:29:15 +0300177 if (vf < MAX_LMAC) {
178 bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
179 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700180
Sunil Goutham92dc8762015-08-30 12:29:15 +0300181 mac = bgx_get_lmac_mac(nic->node, bgx_idx, lmac);
182 if (mac)
183 ether_addr_copy((u8 *)&mbx.nic_cfg.mac_addr, mac);
184 }
185 mbx.nic_cfg.sqs_mode = (vf >= nic->num_vf_en) ? true : false;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700186 mbx.nic_cfg.node_id = nic->node;
Sunil Gouthamd77a2382015-08-30 12:29:16 +0300187
188 mbx.nic_cfg.loopback_supported = vf < MAX_LMAC;
189
Sunil Goutham4863dea2015-05-26 19:20:15 -0700190 nic_send_msg_to_vf(nic, vf, &mbx);
191}
192
193/* ACKs VF's mailbox message
194 * @vf: VF to which ACK to be sent
195 */
196static void nic_mbx_send_ack(struct nicpf *nic, int vf)
197{
198 union nic_mbx mbx = {};
199
200 mbx.msg.msg = NIC_MBOX_MSG_ACK;
201 nic_send_msg_to_vf(nic, vf, &mbx);
202}
203
204/* NACKs VF's mailbox message that PF is not able to
205 * complete the action
206 * @vf: VF to which ACK to be sent
207 */
208static void nic_mbx_send_nack(struct nicpf *nic, int vf)
209{
210 union nic_mbx mbx = {};
211
212 mbx.msg.msg = NIC_MBOX_MSG_NACK;
213 nic_send_msg_to_vf(nic, vf, &mbx);
214}
215
216/* Flush all in flight receive packets to memory and
217 * bring down an active RQ
218 */
219static int nic_rcv_queue_sw_sync(struct nicpf *nic)
220{
221 u16 timeout = ~0x00;
222
223 nic_reg_write(nic, NIC_PF_SW_SYNC_RX, 0x01);
224 /* Wait till sync cycle is finished */
225 while (timeout) {
226 if (nic_reg_read(nic, NIC_PF_SW_SYNC_RX_DONE) & 0x1)
227 break;
228 timeout--;
229 }
230 nic_reg_write(nic, NIC_PF_SW_SYNC_RX, 0x00);
231 if (!timeout) {
232 dev_err(&nic->pdev->dev, "Receive queue software sync failed");
233 return 1;
234 }
235 return 0;
236}
237
238/* Get BGX Rx/Tx stats and respond to VF's request */
239static void nic_get_bgx_stats(struct nicpf *nic, struct bgx_stats_msg *bgx)
240{
241 int bgx_idx, lmac;
242 union nic_mbx mbx = {};
243
244 bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[bgx->vf_id]);
245 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[bgx->vf_id]);
246
247 mbx.bgx_stats.msg = NIC_MBOX_MSG_BGX_STATS;
248 mbx.bgx_stats.vf_id = bgx->vf_id;
249 mbx.bgx_stats.rx = bgx->rx;
250 mbx.bgx_stats.idx = bgx->idx;
251 if (bgx->rx)
252 mbx.bgx_stats.stats = bgx_get_rx_stats(nic->node, bgx_idx,
253 lmac, bgx->idx);
254 else
255 mbx.bgx_stats.stats = bgx_get_tx_stats(nic->node, bgx_idx,
256 lmac, bgx->idx);
257 nic_send_msg_to_vf(nic, bgx->vf_id, &mbx);
258}
259
260/* Update hardware min/max frame size */
261static int nic_update_hw_frs(struct nicpf *nic, int new_frs, int vf)
262{
263 if ((new_frs > NIC_HW_MAX_FRS) || (new_frs < NIC_HW_MIN_FRS)) {
264 dev_err(&nic->pdev->dev,
265 "Invalid MTU setting from VF%d rejected, should be between %d and %d\n",
266 vf, NIC_HW_MIN_FRS, NIC_HW_MAX_FRS);
267 return 1;
268 }
269 new_frs += ETH_HLEN;
270 if (new_frs <= nic->pkind.maxlen)
271 return 0;
272
273 nic->pkind.maxlen = new_frs;
274 nic_reg_write(nic, NIC_PF_PKIND_0_15_CFG, *(u64 *)&nic->pkind);
275 return 0;
276}
277
278/* Set minimum transmit packet size */
279static void nic_set_tx_pkt_pad(struct nicpf *nic, int size)
280{
281 int lmac;
282 u64 lmac_cfg;
283
284 /* Max value that can be set is 60 */
285 if (size > 60)
286 size = 60;
287
288 for (lmac = 0; lmac < (MAX_BGX_PER_CN88XX * MAX_LMAC_PER_BGX); lmac++) {
289 lmac_cfg = nic_reg_read(nic, NIC_PF_LMAC_0_7_CFG | (lmac << 3));
290 lmac_cfg &= ~(0xF << 2);
291 lmac_cfg |= ((size / 4) << 2);
292 nic_reg_write(nic, NIC_PF_LMAC_0_7_CFG | (lmac << 3), lmac_cfg);
293 }
294}
295
296/* Function to check number of LMACs present and set VF::LMAC mapping.
297 * Mapping will be used while initializing channels.
298 */
299static void nic_set_lmac_vf_mapping(struct nicpf *nic)
300{
301 unsigned bgx_map = bgx_get_map(nic->node);
302 int bgx, next_bgx_lmac = 0;
303 int lmac, lmac_cnt = 0;
304 u64 lmac_credit;
305
306 nic->num_vf_en = 0;
307
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530308 for (bgx = 0; bgx < nic->hw->bgx_cnt; bgx++) {
Sunil Goutham4863dea2015-05-26 19:20:15 -0700309 if (!(bgx_map & (1 << bgx)))
310 continue;
311 lmac_cnt = bgx_get_lmac_count(nic->node, bgx);
312 for (lmac = 0; lmac < lmac_cnt; lmac++)
313 nic->vf_lmac_map[next_bgx_lmac++] =
314 NIC_SET_VF_LMAC_MAP(bgx, lmac);
315 nic->num_vf_en += lmac_cnt;
316
317 /* Program LMAC credits */
318 lmac_credit = (1ull << 1); /* channel credit enable */
319 lmac_credit |= (0x1ff << 2); /* Max outstanding pkt count */
320 /* 48KB BGX Tx buffer size, each unit is of size 16bytes */
321 lmac_credit |= (((((48 * 1024) / lmac_cnt) -
322 NIC_HW_MAX_FRS) / 16) << 12);
323 lmac = bgx * MAX_LMAC_PER_BGX;
324 for (; lmac < lmac_cnt + (bgx * MAX_LMAC_PER_BGX); lmac++)
325 nic_reg_write(nic,
326 NIC_PF_LMAC_0_7_CREDIT + (lmac * 8),
327 lmac_credit);
328 }
329}
330
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530331static void nic_get_hw_info(struct nicpf *nic)
332{
333 u16 sdevid;
334 struct hw_info *hw = nic->hw;
335
336 pci_read_config_word(nic->pdev, PCI_SUBSYSTEM_ID, &sdevid);
337
338 switch (sdevid) {
339 case PCI_SUBSYS_DEVID_88XX_NIC_PF:
340 hw->bgx_cnt = MAX_BGX_PER_CN88XX;
341 hw->chans_per_lmac = 16;
342 hw->chans_per_bgx = 128;
343 hw->cpi_cnt = 2048;
344 hw->rssi_cnt = 4096;
345 hw->rss_ind_tbl_size = NIC_MAX_RSS_IDR_TBL_SIZE;
346 hw->tl3_cnt = 256;
347 hw->tl2_cnt = 64;
348 hw->tl1_cnt = 2;
349 hw->tl1_per_bgx = true;
350 break;
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530351 case PCI_SUBSYS_DEVID_81XX_NIC_PF:
352 hw->bgx_cnt = MAX_BGX_PER_CN81XX;
353 hw->chans_per_lmac = 8;
354 hw->chans_per_bgx = 32;
355 hw->chans_per_rgx = 8;
356 hw->chans_per_lbk = 24;
357 hw->cpi_cnt = 512;
358 hw->rssi_cnt = 256;
359 hw->rss_ind_tbl_size = 32; /* Max RSSI / Max interfaces */
360 hw->tl3_cnt = 64;
361 hw->tl2_cnt = 16;
362 hw->tl1_cnt = 10;
363 hw->tl1_per_bgx = false;
364 break;
365 case PCI_SUBSYS_DEVID_83XX_NIC_PF:
366 hw->bgx_cnt = MAX_BGX_PER_CN83XX;
367 hw->chans_per_lmac = 8;
368 hw->chans_per_bgx = 32;
369 hw->chans_per_lbk = 64;
370 hw->cpi_cnt = 2048;
371 hw->rssi_cnt = 1024;
372 hw->rss_ind_tbl_size = 64; /* Max RSSI / Max interfaces */
373 hw->tl3_cnt = 256;
374 hw->tl2_cnt = 64;
375 hw->tl1_cnt = 18;
376 hw->tl1_per_bgx = false;
377 break;
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530378 }
379 hw->tl4_cnt = MAX_QUEUES_PER_QSET * pci_sriov_get_totalvfs(nic->pdev);
380}
381
Sunil Goutham4863dea2015-05-26 19:20:15 -0700382#define BGX0_BLOCK 8
383#define BGX1_BLOCK 9
384
385static void nic_init_hw(struct nicpf *nic)
386{
387 int i;
Sunil Goutham4c0b6eaf2016-02-24 16:40:50 +0530388 u64 cqm_cfg;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700389
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530390 /* Get HW capability info */
391 nic_get_hw_info(nic);
392
Sunil Goutham4863dea2015-05-26 19:20:15 -0700393 /* Enable NIC HW block */
394 nic_reg_write(nic, NIC_PF_CFG, 0x3);
395
396 /* Enable backpressure */
397 nic_reg_write(nic, NIC_PF_BP_CFG, (1ULL << 6) | 0x03);
398
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530399 /* TNS and TNS bypass modes are present only on 88xx */
400 if (nic->pdev->subsystem_device == PCI_SUBSYS_DEVID_88XX_NIC_PF) {
401 /* Disable TNS mode on both interfaces */
402 nic_reg_write(nic, NIC_PF_INTF_0_1_SEND_CFG,
403 (NIC_TNS_BYPASS_MODE << 7) | BGX0_BLOCK);
404 nic_reg_write(nic, NIC_PF_INTF_0_1_SEND_CFG | (1 << 8),
405 (NIC_TNS_BYPASS_MODE << 7) | BGX1_BLOCK);
406 }
407
Sunil Goutham4863dea2015-05-26 19:20:15 -0700408 nic_reg_write(nic, NIC_PF_INTF_0_1_BP_CFG,
409 (1ULL << 63) | BGX0_BLOCK);
410 nic_reg_write(nic, NIC_PF_INTF_0_1_BP_CFG + (1 << 8),
411 (1ULL << 63) | BGX1_BLOCK);
412
413 /* PKIND configuration */
414 nic->pkind.minlen = 0;
415 nic->pkind.maxlen = NIC_HW_MAX_FRS + ETH_HLEN;
416 nic->pkind.lenerr_en = 1;
417 nic->pkind.rx_hdr = 0;
418 nic->pkind.hdr_sl = 0;
419
420 for (i = 0; i < NIC_MAX_PKIND; i++)
421 nic_reg_write(nic, NIC_PF_PKIND_0_15_CFG | (i << 3),
422 *(u64 *)&nic->pkind);
423
424 nic_set_tx_pkt_pad(nic, NIC_HW_MIN_FRS);
425
426 /* Timer config */
427 nic_reg_write(nic, NIC_PF_INTR_TIMER_CFG, NICPF_CLK_PER_INT_TICK);
Sunil Gouthamaa2e2592015-08-30 12:29:13 +0300428
429 /* Enable VLAN ethertype matching and stripping */
430 nic_reg_write(nic, NIC_PF_RX_ETYPE_0_7,
431 (2 << 19) | (ETYPE_ALG_VLAN_STRIP << 16) | ETH_P_8021Q);
Sunil Goutham4c0b6eaf2016-02-24 16:40:50 +0530432
433 /* Check if HW expected value is higher (could be in future chips) */
434 cqm_cfg = nic_reg_read(nic, NIC_PF_CQM_CFG);
435 if (cqm_cfg < NICPF_CQM_MIN_DROP_LEVEL)
436 nic_reg_write(nic, NIC_PF_CQM_CFG, NICPF_CQM_MIN_DROP_LEVEL);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700437}
438
439/* Channel parse index configuration */
440static void nic_config_cpi(struct nicpf *nic, struct cpi_cfg_msg *cfg)
441{
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530442 struct hw_info *hw = nic->hw;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700443 u32 vnic, bgx, lmac, chan;
444 u32 padd, cpi_count = 0;
445 u64 cpi_base, cpi, rssi_base, rssi;
446 u8 qset, rq_idx = 0;
447
448 vnic = cfg->vf_id;
449 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vnic]);
450 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vnic]);
451
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530452 chan = (lmac * hw->chans_per_lmac) + (bgx * hw->chans_per_bgx);
453 cpi_base = (lmac * NIC_MAX_CPI_PER_LMAC) +
454 (bgx * (hw->cpi_cnt / hw->bgx_cnt));
455 rssi_base = (lmac * hw->rss_ind_tbl_size) +
456 (bgx * (hw->rssi_cnt / hw->bgx_cnt));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700457
458 /* Rx channel configuration */
459 nic_reg_write(nic, NIC_PF_CHAN_0_255_RX_BP_CFG | (chan << 3),
460 (1ull << 63) | (vnic << 0));
461 nic_reg_write(nic, NIC_PF_CHAN_0_255_RX_CFG | (chan << 3),
462 ((u64)cfg->cpi_alg << 62) | (cpi_base << 48));
463
464 if (cfg->cpi_alg == CPI_ALG_NONE)
465 cpi_count = 1;
466 else if (cfg->cpi_alg == CPI_ALG_VLAN) /* 3 bits of PCP */
467 cpi_count = 8;
468 else if (cfg->cpi_alg == CPI_ALG_VLAN16) /* 3 bits PCP + DEI */
469 cpi_count = 16;
470 else if (cfg->cpi_alg == CPI_ALG_DIFF) /* 6bits DSCP */
471 cpi_count = NIC_MAX_CPI_PER_LMAC;
472
473 /* RSS Qset, Qidx mapping */
474 qset = cfg->vf_id;
475 rssi = rssi_base;
476 for (; rssi < (rssi_base + cfg->rq_cnt); rssi++) {
477 nic_reg_write(nic, NIC_PF_RSSI_0_4097_RQ | (rssi << 3),
478 (qset << 3) | rq_idx);
479 rq_idx++;
480 }
481
482 rssi = 0;
483 cpi = cpi_base;
484 for (; cpi < (cpi_base + cpi_count); cpi++) {
485 /* Determine port to channel adder */
486 if (cfg->cpi_alg != CPI_ALG_DIFF)
487 padd = cpi % cpi_count;
488 else
489 padd = cpi % 8; /* 3 bits CS out of 6bits DSCP */
490
491 /* Leave RSS_SIZE as '0' to disable RSS */
Sunil Goutham40fb5f82015-12-10 13:25:19 +0530492 if (pass1_silicon(nic->pdev)) {
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700493 nic_reg_write(nic, NIC_PF_CPI_0_2047_CFG | (cpi << 3),
494 (vnic << 24) | (padd << 16) |
495 (rssi_base + rssi));
496 } else {
497 /* Set MPI_ALG to '0' to disable MCAM parsing */
498 nic_reg_write(nic, NIC_PF_CPI_0_2047_CFG | (cpi << 3),
499 (padd << 16));
500 /* MPI index is same as CPI if MPI_ALG is not enabled */
501 nic_reg_write(nic, NIC_PF_MPI_0_2047_CFG | (cpi << 3),
502 (vnic << 24) | (rssi_base + rssi));
503 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700504
505 if ((rssi + 1) >= cfg->rq_cnt)
506 continue;
507
508 if (cfg->cpi_alg == CPI_ALG_VLAN)
509 rssi++;
510 else if (cfg->cpi_alg == CPI_ALG_VLAN16)
511 rssi = ((cpi - cpi_base) & 0xe) >> 1;
512 else if (cfg->cpi_alg == CPI_ALG_DIFF)
513 rssi = ((cpi - cpi_base) & 0x38) >> 3;
514 }
515 nic->cpi_base[cfg->vf_id] = cpi_base;
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700516 nic->rssi_base[cfg->vf_id] = rssi_base;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700517}
518
519/* Responsds to VF with its RSS indirection table size */
520static void nic_send_rss_size(struct nicpf *nic, int vf)
521{
522 union nic_mbx mbx = {};
523 u64 *msg;
524
525 msg = (u64 *)&mbx;
526
527 mbx.rss_size.msg = NIC_MBOX_MSG_RSS_SIZE;
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530528 mbx.rss_size.ind_tbl_size = nic->hw->rss_ind_tbl_size;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700529 nic_send_msg_to_vf(nic, vf, &mbx);
530}
531
532/* Receive side scaling configuration
533 * configure:
534 * - RSS index
535 * - indir table i.e hash::RQ mapping
536 * - no of hash bits to consider
537 */
538static void nic_config_rss(struct nicpf *nic, struct rss_cfg_msg *cfg)
539{
540 u8 qset, idx = 0;
541 u64 cpi_cfg, cpi_base, rssi_base, rssi;
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700542 u64 idx_addr;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700543
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700544 rssi_base = nic->rssi_base[cfg->vf_id] + cfg->tbl_offset;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700545
546 rssi = rssi_base;
547 qset = cfg->vf_id;
548
549 for (; rssi < (rssi_base + cfg->tbl_len); rssi++) {
Sunil Goutham92dc8762015-08-30 12:29:15 +0300550 u8 svf = cfg->ind_tbl[idx] >> 3;
551
552 if (svf)
553 qset = nic->vf_sqs[cfg->vf_id][svf - 1];
554 else
555 qset = cfg->vf_id;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700556 nic_reg_write(nic, NIC_PF_RSSI_0_4097_RQ | (rssi << 3),
557 (qset << 3) | (cfg->ind_tbl[idx] & 0x7));
558 idx++;
559 }
560
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700561 cpi_base = nic->cpi_base[cfg->vf_id];
Sunil Goutham40fb5f82015-12-10 13:25:19 +0530562 if (pass1_silicon(nic->pdev))
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700563 idx_addr = NIC_PF_CPI_0_2047_CFG;
564 else
565 idx_addr = NIC_PF_MPI_0_2047_CFG;
566 cpi_cfg = nic_reg_read(nic, idx_addr | (cpi_base << 3));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700567 cpi_cfg &= ~(0xFULL << 20);
568 cpi_cfg |= (cfg->hash_bits << 20);
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700569 nic_reg_write(nic, idx_addr | (cpi_base << 3), cpi_cfg);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700570}
571
572/* 4 level transmit side scheduler configutation
573 * for TNS bypass mode
574 *
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530575 * Sample configuration for SQ0 on 88xx
Sunil Goutham4863dea2015-05-26 19:20:15 -0700576 * VNIC0-SQ0 -> TL4(0) -> TL3[0] -> TL2[0] -> TL1[0] -> BGX0
577 * VNIC1-SQ0 -> TL4(8) -> TL3[2] -> TL2[0] -> TL1[0] -> BGX0
578 * VNIC2-SQ0 -> TL4(16) -> TL3[4] -> TL2[1] -> TL1[0] -> BGX0
579 * VNIC3-SQ0 -> TL4(24) -> TL3[6] -> TL2[1] -> TL1[0] -> BGX0
580 * VNIC4-SQ0 -> TL4(512) -> TL3[128] -> TL2[32] -> TL1[1] -> BGX1
581 * VNIC5-SQ0 -> TL4(520) -> TL3[130] -> TL2[32] -> TL1[1] -> BGX1
582 * VNIC6-SQ0 -> TL4(528) -> TL3[132] -> TL2[33] -> TL1[1] -> BGX1
583 * VNIC7-SQ0 -> TL4(536) -> TL3[134] -> TL2[33] -> TL1[1] -> BGX1
584 */
Sunil Goutham92dc8762015-08-30 12:29:15 +0300585static void nic_tx_channel_cfg(struct nicpf *nic, u8 vnic,
586 struct sq_cfg_msg *sq)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700587{
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530588 struct hw_info *hw = nic->hw;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700589 u32 bgx, lmac, chan;
590 u32 tl2, tl3, tl4;
591 u32 rr_quantum;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300592 u8 sq_idx = sq->sq_num;
593 u8 pqs_vnic;
Sunil Goutham3e29adb2016-06-27 15:30:03 +0530594 int svf;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700595
Sunil Goutham92dc8762015-08-30 12:29:15 +0300596 if (sq->sqs_mode)
597 pqs_vnic = nic->pqs_vf[vnic];
598 else
599 pqs_vnic = vnic;
600
601 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[pqs_vnic]);
602 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[pqs_vnic]);
603
Sunil Goutham4863dea2015-05-26 19:20:15 -0700604 /* 24 bytes for FCS, IPG and preamble */
605 rr_quantum = ((NIC_HW_MAX_FRS + 24) / 4);
606
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530607 /* For 88xx 0-511 TL4 transmits via BGX0 and
608 * 512-1023 TL4s transmit via BGX1.
609 */
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530610 if (hw->tl1_per_bgx) {
611 tl4 = bgx * (hw->tl4_cnt / hw->bgx_cnt);
612 if (!sq->sqs_mode) {
613 tl4 += (lmac * MAX_QUEUES_PER_QSET);
614 } else {
615 for (svf = 0; svf < MAX_SQS_PER_VF; svf++) {
616 if (nic->vf_sqs[pqs_vnic][svf] == vnic)
617 break;
618 }
619 tl4 += (MAX_LMAC_PER_BGX * MAX_QUEUES_PER_QSET);
620 tl4 += (lmac * MAX_QUEUES_PER_QSET * MAX_SQS_PER_VF);
621 tl4 += (svf * MAX_QUEUES_PER_QSET);
Sunil Goutham3e29adb2016-06-27 15:30:03 +0530622 }
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530623 } else {
624 tl4 = (vnic * MAX_QUEUES_PER_QSET);
Sunil Goutham3e29adb2016-06-27 15:30:03 +0530625 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700626 tl4 += sq_idx;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300627
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530628 tl3 = tl4 / (hw->tl4_cnt / hw->tl3_cnt);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700629 nic_reg_write(nic, NIC_PF_QSET_0_127_SQ_0_7_CFG2 |
630 ((u64)vnic << NIC_QS_ID_SHIFT) |
631 ((u32)sq_idx << NIC_Q_NUM_SHIFT), tl4);
632 nic_reg_write(nic, NIC_PF_TL4_0_1023_CFG | (tl4 << 3),
633 ((u64)vnic << 27) | ((u32)sq_idx << 24) | rr_quantum);
634
635 nic_reg_write(nic, NIC_PF_TL3_0_255_CFG | (tl3 << 3), rr_quantum);
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530636
637 /* On 88xx 0-127 channels are for BGX0 and
638 * 127-255 channels for BGX1.
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530639 *
640 * On 81xx/83xx TL3_CHAN reg should be configured with channel
641 * within LMAC i.e 0-7 and not the actual channel number like on 88xx
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530642 */
643 chan = (lmac * hw->chans_per_lmac) + (bgx * hw->chans_per_bgx);
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530644 if (hw->tl1_per_bgx)
645 nic_reg_write(nic, NIC_PF_TL3_0_255_CHAN | (tl3 << 3), chan);
646 else
647 nic_reg_write(nic, NIC_PF_TL3_0_255_CHAN | (tl3 << 3), 0);
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530648
Sunil Goutham4863dea2015-05-26 19:20:15 -0700649 /* Enable backpressure on the channel */
650 nic_reg_write(nic, NIC_PF_CHAN_0_255_TX_CFG | (chan << 3), 1);
651
652 tl2 = tl3 >> 2;
653 nic_reg_write(nic, NIC_PF_TL3A_0_63_CFG | (tl2 << 3), tl2);
654 nic_reg_write(nic, NIC_PF_TL2_0_63_CFG | (tl2 << 3), rr_quantum);
655 /* No priorities as of now */
656 nic_reg_write(nic, NIC_PF_TL2_0_63_PRI | (tl2 << 3), 0x00);
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530657
658 /* Unlike 88xx where TL2s 0-31 transmits to TL1 '0' and rest to TL1 '1'
659 * on 81xx/83xx TL2 needs to be configured to transmit to one of the
660 * possible LMACs.
661 *
662 * This register doesn't exist on 88xx.
663 */
664 if (!hw->tl1_per_bgx)
665 nic_reg_write(nic, NIC_PF_TL2_LMAC | (tl2 << 3),
666 lmac + (bgx * MAX_LMAC_PER_BGX));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700667}
668
Sunil Goutham92dc8762015-08-30 12:29:15 +0300669/* Send primary nicvf pointer to secondary QS's VF */
670static void nic_send_pnicvf(struct nicpf *nic, int sqs)
671{
672 union nic_mbx mbx = {};
673
674 mbx.nicvf.msg = NIC_MBOX_MSG_PNICVF_PTR;
675 mbx.nicvf.nicvf = nic->nicvf[nic->pqs_vf[sqs]];
676 nic_send_msg_to_vf(nic, sqs, &mbx);
677}
678
679/* Send SQS's nicvf pointer to primary QS's VF */
680static void nic_send_snicvf(struct nicpf *nic, struct nicvf_ptr *nicvf)
681{
682 union nic_mbx mbx = {};
683 int sqs_id = nic->vf_sqs[nicvf->vf_id][nicvf->sqs_id];
684
685 mbx.nicvf.msg = NIC_MBOX_MSG_SNICVF_PTR;
686 mbx.nicvf.sqs_id = nicvf->sqs_id;
687 mbx.nicvf.nicvf = nic->nicvf[sqs_id];
688 nic_send_msg_to_vf(nic, nicvf->vf_id, &mbx);
689}
690
691/* Find next available Qset that can be assigned as a
692 * secondary Qset to a VF.
693 */
694static int nic_nxt_avail_sqs(struct nicpf *nic)
695{
696 int sqs;
697
698 for (sqs = 0; sqs < nic->num_sqs_en; sqs++) {
699 if (!nic->sqs_used[sqs])
700 nic->sqs_used[sqs] = true;
701 else
702 continue;
703 return sqs + nic->num_vf_en;
704 }
705 return -1;
706}
707
708/* Allocate additional Qsets for requested VF */
709static void nic_alloc_sqs(struct nicpf *nic, struct sqs_alloc *sqs)
710{
711 union nic_mbx mbx = {};
712 int idx, alloc_qs = 0;
713 int sqs_id;
714
715 if (!nic->num_sqs_en)
716 goto send_mbox;
717
718 for (idx = 0; idx < sqs->qs_count; idx++) {
719 sqs_id = nic_nxt_avail_sqs(nic);
720 if (sqs_id < 0)
721 break;
722 nic->vf_sqs[sqs->vf_id][idx] = sqs_id;
723 nic->pqs_vf[sqs_id] = sqs->vf_id;
724 alloc_qs++;
725 }
726
727send_mbox:
728 mbx.sqs_alloc.msg = NIC_MBOX_MSG_ALLOC_SQS;
729 mbx.sqs_alloc.vf_id = sqs->vf_id;
730 mbx.sqs_alloc.qs_count = alloc_qs;
731 nic_send_msg_to_vf(nic, sqs->vf_id, &mbx);
732}
733
Sunil Gouthamd77a2382015-08-30 12:29:16 +0300734static int nic_config_loopback(struct nicpf *nic, struct set_loopback *lbk)
735{
736 int bgx_idx, lmac_idx;
737
738 if (lbk->vf_id > MAX_LMAC)
739 return -1;
740
741 bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lbk->vf_id]);
742 lmac_idx = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lbk->vf_id]);
743
744 bgx_lmac_internal_loopback(nic->node, bgx_idx, lmac_idx, lbk->enable);
745
746 return 0;
747}
748
Pavel Fedinf406ce42015-12-08 10:37:44 +0300749static void nic_enable_vf(struct nicpf *nic, int vf, bool enable)
750{
751 int bgx, lmac;
752
753 nic->vf_enabled[vf] = enable;
754
755 if (vf >= nic->num_vf_en)
756 return;
757
758 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
759 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
760
761 bgx_lmac_rx_tx_enable(nic->node, bgx, lmac, enable);
762}
763
Sunil Goutham4863dea2015-05-26 19:20:15 -0700764/* Interrupt handler to handle mailbox messages from VFs */
765static void nic_handle_mbx_intr(struct nicpf *nic, int vf)
766{
767 union nic_mbx mbx = {};
768 u64 *mbx_data;
769 u64 mbx_addr;
770 u64 reg_addr;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300771 u64 cfg;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700772 int bgx, lmac;
773 int i;
774 int ret = 0;
775
776 nic->mbx_lock[vf] = true;
777
778 mbx_addr = nic_get_mbx_addr(vf);
779 mbx_data = (u64 *)&mbx;
780
781 for (i = 0; i < NIC_PF_VF_MAILBOX_SIZE; i++) {
782 *mbx_data = nic_reg_read(nic, mbx_addr);
783 mbx_data++;
784 mbx_addr += sizeof(u64);
785 }
786
787 dev_dbg(&nic->pdev->dev, "%s: Mailbox msg %d from VF%d\n",
788 __func__, mbx.msg.msg, vf);
789 switch (mbx.msg.msg) {
790 case NIC_MBOX_MSG_READY:
791 nic_mbx_send_ready(nic, vf);
Sunil Goutham92dc8762015-08-30 12:29:15 +0300792 if (vf < MAX_LMAC) {
793 nic->link[vf] = 0;
794 nic->duplex[vf] = 0;
795 nic->speed[vf] = 0;
796 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700797 ret = 1;
798 break;
799 case NIC_MBOX_MSG_QS_CFG:
800 reg_addr = NIC_PF_QSET_0_127_CFG |
801 (mbx.qs.num << NIC_QS_ID_SHIFT);
Sunil Goutham92dc8762015-08-30 12:29:15 +0300802 cfg = mbx.qs.cfg;
803 /* Check if its a secondary Qset */
804 if (vf >= nic->num_vf_en) {
805 cfg = cfg & (~0x7FULL);
806 /* Assign this Qset to primary Qset's VF */
807 cfg |= nic->pqs_vf[vf];
808 }
809 nic_reg_write(nic, reg_addr, cfg);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700810 break;
811 case NIC_MBOX_MSG_RQ_CFG:
812 reg_addr = NIC_PF_QSET_0_127_RQ_0_7_CFG |
813 (mbx.rq.qs_num << NIC_QS_ID_SHIFT) |
814 (mbx.rq.rq_num << NIC_Q_NUM_SHIFT);
815 nic_reg_write(nic, reg_addr, mbx.rq.cfg);
Sunil Goutham02a72bd2016-08-12 16:51:28 +0530816 /* Enable CQE_RX2_S extension in CQE_RX descriptor.
817 * This gets appended by default on 81xx/83xx chips,
818 * for consistency enabling the same on 88xx pass2
819 * where this is introduced.
820 */
821 if (pass2_silicon(nic->pdev))
822 nic_reg_write(nic, NIC_PF_RX_CFG, 0x01);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700823 break;
824 case NIC_MBOX_MSG_RQ_BP_CFG:
825 reg_addr = NIC_PF_QSET_0_127_RQ_0_7_BP_CFG |
826 (mbx.rq.qs_num << NIC_QS_ID_SHIFT) |
827 (mbx.rq.rq_num << NIC_Q_NUM_SHIFT);
828 nic_reg_write(nic, reg_addr, mbx.rq.cfg);
829 break;
830 case NIC_MBOX_MSG_RQ_SW_SYNC:
831 ret = nic_rcv_queue_sw_sync(nic);
832 break;
833 case NIC_MBOX_MSG_RQ_DROP_CFG:
834 reg_addr = NIC_PF_QSET_0_127_RQ_0_7_DROP_CFG |
835 (mbx.rq.qs_num << NIC_QS_ID_SHIFT) |
836 (mbx.rq.rq_num << NIC_Q_NUM_SHIFT);
837 nic_reg_write(nic, reg_addr, mbx.rq.cfg);
838 break;
839 case NIC_MBOX_MSG_SQ_CFG:
840 reg_addr = NIC_PF_QSET_0_127_SQ_0_7_CFG |
841 (mbx.sq.qs_num << NIC_QS_ID_SHIFT) |
842 (mbx.sq.sq_num << NIC_Q_NUM_SHIFT);
843 nic_reg_write(nic, reg_addr, mbx.sq.cfg);
Sunil Goutham92dc8762015-08-30 12:29:15 +0300844 nic_tx_channel_cfg(nic, mbx.qs.num, &mbx.sq);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700845 break;
846 case NIC_MBOX_MSG_SET_MAC:
Sunil Goutham92dc8762015-08-30 12:29:15 +0300847 if (vf >= nic->num_vf_en)
848 break;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700849 lmac = mbx.mac.vf_id;
850 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lmac]);
851 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lmac]);
Aleksey Makarove610cb32015-06-02 11:00:21 -0700852 bgx_set_lmac_mac(nic->node, bgx, lmac, mbx.mac.mac_addr);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700853 break;
854 case NIC_MBOX_MSG_SET_MAX_FRS:
855 ret = nic_update_hw_frs(nic, mbx.frs.max_frs,
856 mbx.frs.vf_id);
857 break;
858 case NIC_MBOX_MSG_CPI_CFG:
859 nic_config_cpi(nic, &mbx.cpi_cfg);
860 break;
861 case NIC_MBOX_MSG_RSS_SIZE:
862 nic_send_rss_size(nic, vf);
863 goto unlock;
864 case NIC_MBOX_MSG_RSS_CFG:
865 case NIC_MBOX_MSG_RSS_CFG_CONT:
866 nic_config_rss(nic, &mbx.rss_cfg);
867 break;
868 case NIC_MBOX_MSG_CFG_DONE:
869 /* Last message of VF config msg sequence */
Pavel Fedinf406ce42015-12-08 10:37:44 +0300870 nic_enable_vf(nic, vf, true);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700871 goto unlock;
872 case NIC_MBOX_MSG_SHUTDOWN:
873 /* First msg in VF teardown sequence */
Sunil Goutham92dc8762015-08-30 12:29:15 +0300874 if (vf >= nic->num_vf_en)
875 nic->sqs_used[vf - nic->num_vf_en] = false;
876 nic->pqs_vf[vf] = 0;
Pavel Fedinf406ce42015-12-08 10:37:44 +0300877 nic_enable_vf(nic, vf, false);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700878 break;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300879 case NIC_MBOX_MSG_ALLOC_SQS:
880 nic_alloc_sqs(nic, &mbx.sqs_alloc);
881 goto unlock;
882 case NIC_MBOX_MSG_NICVF_PTR:
883 nic->nicvf[vf] = mbx.nicvf.nicvf;
884 break;
885 case NIC_MBOX_MSG_PNICVF_PTR:
886 nic_send_pnicvf(nic, vf);
887 goto unlock;
888 case NIC_MBOX_MSG_SNICVF_PTR:
889 nic_send_snicvf(nic, &mbx.nicvf);
890 goto unlock;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700891 case NIC_MBOX_MSG_BGX_STATS:
892 nic_get_bgx_stats(nic, &mbx.bgx_stats);
893 goto unlock;
Sunil Gouthamd77a2382015-08-30 12:29:16 +0300894 case NIC_MBOX_MSG_LOOPBACK:
895 ret = nic_config_loopback(nic, &mbx.lbk);
896 break;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700897 default:
898 dev_err(&nic->pdev->dev,
899 "Invalid msg from VF%d, msg 0x%x\n", vf, mbx.msg.msg);
900 break;
901 }
902
903 if (!ret)
904 nic_mbx_send_ack(nic, vf);
905 else if (mbx.msg.msg != NIC_MBOX_MSG_READY)
906 nic_mbx_send_nack(nic, vf);
907unlock:
908 nic->mbx_lock[vf] = false;
909}
910
Sunil Goutham52358aa2016-08-12 16:51:29 +0530911static irqreturn_t nic_mbx_intr_handler(int irq, void *nic_irq)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700912{
Sunil Goutham52358aa2016-08-12 16:51:29 +0530913 struct nicpf *nic = (struct nicpf *)nic_irq;
914 int mbx;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700915 u64 intr;
916 u8 vf, vf_per_mbx_reg = 64;
917
Sunil Goutham52358aa2016-08-12 16:51:29 +0530918 if (irq == nic->msix_entries[NIC_PF_INTR_ID_MBOX0].vector)
919 mbx = 0;
920 else
921 mbx = 1;
922
Sunil Goutham4863dea2015-05-26 19:20:15 -0700923 intr = nic_reg_read(nic, NIC_PF_MAILBOX_INT + (mbx << 3));
924 dev_dbg(&nic->pdev->dev, "PF interrupt Mbox%d 0x%llx\n", mbx, intr);
925 for (vf = 0; vf < vf_per_mbx_reg; vf++) {
926 if (intr & (1ULL << vf)) {
927 dev_dbg(&nic->pdev->dev, "Intr from VF %d\n",
928 vf + (mbx * vf_per_mbx_reg));
Sunil Goutham92dc8762015-08-30 12:29:15 +0300929
Sunil Goutham4863dea2015-05-26 19:20:15 -0700930 nic_handle_mbx_intr(nic, vf + (mbx * vf_per_mbx_reg));
931 nic_clear_mbx_intr(nic, vf, mbx);
932 }
933 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700934 return IRQ_HANDLED;
935}
936
937static int nic_enable_msix(struct nicpf *nic)
938{
939 int i, ret;
940
Sunil Goutham52358aa2016-08-12 16:51:29 +0530941 nic->num_vec = pci_msix_vec_count(nic->pdev);
942
943 nic->msix_entries = kmalloc_array(nic->num_vec,
944 sizeof(struct msix_entry),
945 GFP_KERNEL);
946 if (!nic->msix_entries)
947 return -ENOMEM;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700948
949 for (i = 0; i < nic->num_vec; i++)
950 nic->msix_entries[i].entry = i;
951
952 ret = pci_enable_msix(nic->pdev, nic->msix_entries, nic->num_vec);
953 if (ret) {
954 dev_err(&nic->pdev->dev,
Sunil Goutham52358aa2016-08-12 16:51:29 +0530955 "Request for #%d msix vectors failed, returned %d\n",
956 nic->num_vec, ret);
957 kfree(nic->msix_entries);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700958 return ret;
959 }
960
961 nic->msix_enabled = 1;
962 return 0;
963}
964
965static void nic_disable_msix(struct nicpf *nic)
966{
967 if (nic->msix_enabled) {
968 pci_disable_msix(nic->pdev);
Sunil Goutham52358aa2016-08-12 16:51:29 +0530969 kfree(nic->msix_entries);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700970 nic->msix_enabled = 0;
971 nic->num_vec = 0;
972 }
973}
974
975static void nic_free_all_interrupts(struct nicpf *nic)
976{
977 int irq;
978
979 for (irq = 0; irq < nic->num_vec; irq++) {
980 if (nic->irq_allocated[irq])
981 free_irq(nic->msix_entries[irq].vector, nic);
982 nic->irq_allocated[irq] = false;
983 }
984}
985
986static int nic_register_interrupts(struct nicpf *nic)
987{
Sunil Goutham52358aa2016-08-12 16:51:29 +0530988 int i, ret;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700989
990 /* Enable MSI-X */
991 ret = nic_enable_msix(nic);
992 if (ret)
993 return ret;
994
Sunil Goutham52358aa2016-08-12 16:51:29 +0530995 /* Register mailbox interrupt handler */
996 for (i = NIC_PF_INTR_ID_MBOX0; i < nic->num_vec; i++) {
997 sprintf(nic->irq_name[i],
998 "NICPF Mbox%d", (i - NIC_PF_INTR_ID_MBOX0));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700999
Sunil Goutham52358aa2016-08-12 16:51:29 +05301000 ret = request_irq(nic->msix_entries[i].vector,
1001 nic_mbx_intr_handler, 0,
1002 nic->irq_name[i], nic);
1003 if (ret)
1004 goto fail;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001005
Sunil Goutham52358aa2016-08-12 16:51:29 +05301006 nic->irq_allocated[i] = true;
1007 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001008
1009 /* Enable mailbox interrupt */
1010 nic_enable_mbx_intr(nic);
1011 return 0;
1012
1013fail:
1014 dev_err(&nic->pdev->dev, "Request irq failed\n");
1015 nic_free_all_interrupts(nic);
Sunil Goutham52358aa2016-08-12 16:51:29 +05301016 nic_disable_msix(nic);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001017 return ret;
1018}
1019
1020static void nic_unregister_interrupts(struct nicpf *nic)
1021{
1022 nic_free_all_interrupts(nic);
1023 nic_disable_msix(nic);
1024}
1025
Sunil Goutham92dc8762015-08-30 12:29:15 +03001026static int nic_num_sqs_en(struct nicpf *nic, int vf_en)
1027{
1028 int pos, sqs_per_vf = MAX_SQS_PER_VF_SINGLE_NODE;
1029 u16 total_vf;
1030
Sunil Goutham3a397eb2016-08-12 16:51:27 +05301031 /* Secondary Qsets are needed only if CPU count is
1032 * morethan MAX_QUEUES_PER_QSET.
1033 */
1034 if (num_online_cpus() <= MAX_QUEUES_PER_QSET)
1035 return 0;
1036
Sunil Goutham92dc8762015-08-30 12:29:15 +03001037 /* Check if its a multi-node environment */
1038 if (nr_node_ids > 1)
1039 sqs_per_vf = MAX_SQS_PER_VF;
1040
1041 pos = pci_find_ext_capability(nic->pdev, PCI_EXT_CAP_ID_SRIOV);
1042 pci_read_config_word(nic->pdev, (pos + PCI_SRIOV_TOTAL_VF), &total_vf);
1043 return min(total_vf - vf_en, vf_en * sqs_per_vf);
1044}
1045
Sunil Goutham4863dea2015-05-26 19:20:15 -07001046static int nic_sriov_init(struct pci_dev *pdev, struct nicpf *nic)
1047{
1048 int pos = 0;
Sunil Goutham92dc8762015-08-30 12:29:15 +03001049 int vf_en;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001050 int err;
1051 u16 total_vf_cnt;
1052
1053 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1054 if (!pos) {
1055 dev_err(&pdev->dev, "SRIOV capability is not found in PCIe config space\n");
1056 return -ENODEV;
1057 }
1058
1059 pci_read_config_word(pdev, (pos + PCI_SRIOV_TOTAL_VF), &total_vf_cnt);
1060 if (total_vf_cnt < nic->num_vf_en)
1061 nic->num_vf_en = total_vf_cnt;
1062
1063 if (!total_vf_cnt)
1064 return 0;
1065
Sunil Goutham92dc8762015-08-30 12:29:15 +03001066 vf_en = nic->num_vf_en;
1067 nic->num_sqs_en = nic_num_sqs_en(nic, nic->num_vf_en);
1068 vf_en += nic->num_sqs_en;
1069
1070 err = pci_enable_sriov(pdev, vf_en);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001071 if (err) {
1072 dev_err(&pdev->dev, "SRIOV enable failed, num VF is %d\n",
Sunil Goutham92dc8762015-08-30 12:29:15 +03001073 vf_en);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001074 nic->num_vf_en = 0;
1075 return err;
1076 }
1077
1078 dev_info(&pdev->dev, "SRIOV enabled, number of VF available %d\n",
Sunil Goutham92dc8762015-08-30 12:29:15 +03001079 vf_en);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001080
1081 nic->flags |= NIC_SRIOV_ENABLED;
1082 return 0;
1083}
1084
1085/* Poll for BGX LMAC link status and update corresponding VF
1086 * if there is a change, valid only if internal L2 switch
1087 * is not present otherwise VF link is always treated as up
1088 */
1089static void nic_poll_for_link(struct work_struct *work)
1090{
1091 union nic_mbx mbx = {};
1092 struct nicpf *nic;
1093 struct bgx_link_status link;
1094 u8 vf, bgx, lmac;
1095
1096 nic = container_of(work, struct nicpf, dwork.work);
1097
1098 mbx.link_status.msg = NIC_MBOX_MSG_BGX_LINK_CHANGE;
1099
Pavel Fedinf406ce42015-12-08 10:37:44 +03001100 for (vf = 0; vf < nic->num_vf_en; vf++) {
Sunil Goutham4863dea2015-05-26 19:20:15 -07001101 /* Poll only if VF is UP */
1102 if (!nic->vf_enabled[vf])
1103 continue;
1104
1105 /* Get BGX, LMAC indices for the VF */
1106 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
1107 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
1108 /* Get interface link status */
1109 bgx_get_lmac_link_state(nic->node, bgx, lmac, &link);
1110
1111 /* Inform VF only if link status changed */
1112 if (nic->link[vf] == link.link_up)
1113 continue;
1114
1115 if (!nic->mbx_lock[vf]) {
1116 nic->link[vf] = link.link_up;
1117 nic->duplex[vf] = link.duplex;
1118 nic->speed[vf] = link.speed;
1119
1120 /* Send a mbox message to VF with current link status */
1121 mbx.link_status.link_up = link.link_up;
1122 mbx.link_status.duplex = link.duplex;
1123 mbx.link_status.speed = link.speed;
1124 nic_send_msg_to_vf(nic, vf, &mbx);
1125 }
1126 }
1127 queue_delayed_work(nic->check_link, &nic->dwork, HZ * 2);
1128}
1129
1130static int nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1131{
1132 struct device *dev = &pdev->dev;
1133 struct nicpf *nic;
1134 int err;
1135
1136 BUILD_BUG_ON(sizeof(union nic_mbx) > 16);
1137
1138 nic = devm_kzalloc(dev, sizeof(*nic), GFP_KERNEL);
1139 if (!nic)
1140 return -ENOMEM;
1141
Sunil Gouthama5c3d492016-08-12 16:51:24 +05301142 nic->hw = devm_kzalloc(dev, sizeof(struct hw_info), GFP_KERNEL);
1143 if (!nic->hw) {
1144 devm_kfree(dev, nic);
1145 return -ENOMEM;
1146 }
1147
Sunil Goutham4863dea2015-05-26 19:20:15 -07001148 pci_set_drvdata(pdev, nic);
1149
1150 nic->pdev = pdev;
1151
1152 err = pci_enable_device(pdev);
1153 if (err) {
1154 dev_err(dev, "Failed to enable PCI device\n");
1155 pci_set_drvdata(pdev, NULL);
1156 return err;
1157 }
1158
1159 err = pci_request_regions(pdev, DRV_NAME);
1160 if (err) {
1161 dev_err(dev, "PCI request regions failed 0x%x\n", err);
1162 goto err_disable_device;
1163 }
1164
1165 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(48));
1166 if (err) {
1167 dev_err(dev, "Unable to get usable DMA configuration\n");
1168 goto err_release_regions;
1169 }
1170
1171 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(48));
1172 if (err) {
1173 dev_err(dev, "Unable to get 48-bit DMA for consistent allocations\n");
1174 goto err_release_regions;
1175 }
1176
1177 /* MAP PF's configuration registers */
1178 nic->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
1179 if (!nic->reg_base) {
1180 dev_err(dev, "Cannot map config register space, aborting\n");
1181 err = -ENOMEM;
1182 goto err_release_regions;
1183 }
1184
Robert Richterd768b672015-06-02 11:00:18 -07001185 nic->node = nic_get_node_id(pdev);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001186
Sunil Goutham4863dea2015-05-26 19:20:15 -07001187 /* Initialize hardware */
1188 nic_init_hw(nic);
1189
Sunil Gouthama5c3d492016-08-12 16:51:24 +05301190 nic_set_lmac_vf_mapping(nic);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001191
1192 /* Register interrupts */
1193 err = nic_register_interrupts(nic);
1194 if (err)
1195 goto err_release_regions;
1196
1197 /* Configure SRIOV */
1198 err = nic_sriov_init(pdev, nic);
1199 if (err)
1200 goto err_unregister_interrupts;
1201
1202 /* Register a physical link status poll fn() */
1203 nic->check_link = alloc_workqueue("check_link_status",
1204 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1205 if (!nic->check_link) {
1206 err = -ENOMEM;
1207 goto err_disable_sriov;
1208 }
1209
1210 INIT_DELAYED_WORK(&nic->dwork, nic_poll_for_link);
1211 queue_delayed_work(nic->check_link, &nic->dwork, 0);
1212
1213 return 0;
1214
1215err_disable_sriov:
1216 if (nic->flags & NIC_SRIOV_ENABLED)
1217 pci_disable_sriov(pdev);
1218err_unregister_interrupts:
1219 nic_unregister_interrupts(nic);
1220err_release_regions:
1221 pci_release_regions(pdev);
1222err_disable_device:
Sunil Gouthama5c3d492016-08-12 16:51:24 +05301223 devm_kfree(dev, nic->hw);
1224 devm_kfree(dev, nic);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001225 pci_disable_device(pdev);
1226 pci_set_drvdata(pdev, NULL);
1227 return err;
1228}
1229
1230static void nic_remove(struct pci_dev *pdev)
1231{
1232 struct nicpf *nic = pci_get_drvdata(pdev);
1233
1234 if (nic->flags & NIC_SRIOV_ENABLED)
1235 pci_disable_sriov(pdev);
1236
1237 if (nic->check_link) {
1238 /* Destroy work Queue */
Thanneeru Srinivasulua7b1f532015-12-02 15:36:14 +05301239 cancel_delayed_work_sync(&nic->dwork);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001240 destroy_workqueue(nic->check_link);
1241 }
1242
1243 nic_unregister_interrupts(nic);
1244 pci_release_regions(pdev);
Sunil Gouthama5c3d492016-08-12 16:51:24 +05301245
1246 devm_kfree(&pdev->dev, nic->hw);
1247 devm_kfree(&pdev->dev, nic);
1248
Sunil Goutham4863dea2015-05-26 19:20:15 -07001249 pci_disable_device(pdev);
1250 pci_set_drvdata(pdev, NULL);
1251}
1252
1253static struct pci_driver nic_driver = {
1254 .name = DRV_NAME,
1255 .id_table = nic_id_table,
1256 .probe = nic_probe,
1257 .remove = nic_remove,
1258};
1259
1260static int __init nic_init_module(void)
1261{
1262 pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION);
1263
1264 return pci_register_driver(&nic_driver);
1265}
1266
1267static void __exit nic_cleanup_module(void)
1268{
1269 pci_unregister_driver(&nic_driver);
1270}
1271
1272module_init(nic_init_module);
1273module_exit(nic_cleanup_module);