blob: 91c575d418d0fb5f2b040c3821e16b6dd8f6e339 [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);
Sunil Goutham64658592016-08-12 16:51:33 +0530328
329 /* On CN81XX there are only 8 VFs but max possible no of
330 * interfaces are 9.
331 */
332 if (nic->num_vf_en >= pci_sriov_get_totalvfs(nic->pdev)) {
333 nic->num_vf_en = pci_sriov_get_totalvfs(nic->pdev);
334 break;
335 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700336 }
337}
338
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530339static void nic_get_hw_info(struct nicpf *nic)
340{
341 u16 sdevid;
342 struct hw_info *hw = nic->hw;
343
344 pci_read_config_word(nic->pdev, PCI_SUBSYSTEM_ID, &sdevid);
345
346 switch (sdevid) {
347 case PCI_SUBSYS_DEVID_88XX_NIC_PF:
348 hw->bgx_cnt = MAX_BGX_PER_CN88XX;
349 hw->chans_per_lmac = 16;
350 hw->chans_per_bgx = 128;
351 hw->cpi_cnt = 2048;
352 hw->rssi_cnt = 4096;
353 hw->rss_ind_tbl_size = NIC_MAX_RSS_IDR_TBL_SIZE;
354 hw->tl3_cnt = 256;
355 hw->tl2_cnt = 64;
356 hw->tl1_cnt = 2;
357 hw->tl1_per_bgx = true;
358 break;
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530359 case PCI_SUBSYS_DEVID_81XX_NIC_PF:
360 hw->bgx_cnt = MAX_BGX_PER_CN81XX;
361 hw->chans_per_lmac = 8;
362 hw->chans_per_bgx = 32;
363 hw->chans_per_rgx = 8;
364 hw->chans_per_lbk = 24;
365 hw->cpi_cnt = 512;
366 hw->rssi_cnt = 256;
367 hw->rss_ind_tbl_size = 32; /* Max RSSI / Max interfaces */
368 hw->tl3_cnt = 64;
369 hw->tl2_cnt = 16;
370 hw->tl1_cnt = 10;
371 hw->tl1_per_bgx = false;
372 break;
373 case PCI_SUBSYS_DEVID_83XX_NIC_PF:
374 hw->bgx_cnt = MAX_BGX_PER_CN83XX;
375 hw->chans_per_lmac = 8;
376 hw->chans_per_bgx = 32;
377 hw->chans_per_lbk = 64;
378 hw->cpi_cnt = 2048;
379 hw->rssi_cnt = 1024;
380 hw->rss_ind_tbl_size = 64; /* Max RSSI / Max interfaces */
381 hw->tl3_cnt = 256;
382 hw->tl2_cnt = 64;
383 hw->tl1_cnt = 18;
384 hw->tl1_per_bgx = false;
385 break;
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530386 }
387 hw->tl4_cnt = MAX_QUEUES_PER_QSET * pci_sriov_get_totalvfs(nic->pdev);
388}
389
Sunil Goutham4863dea2015-05-26 19:20:15 -0700390#define BGX0_BLOCK 8
391#define BGX1_BLOCK 9
392
393static void nic_init_hw(struct nicpf *nic)
394{
395 int i;
Sunil Goutham4c0b6eaf2016-02-24 16:40:50 +0530396 u64 cqm_cfg;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700397
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530398 /* Get HW capability info */
399 nic_get_hw_info(nic);
400
Sunil Goutham4863dea2015-05-26 19:20:15 -0700401 /* Enable NIC HW block */
402 nic_reg_write(nic, NIC_PF_CFG, 0x3);
403
404 /* Enable backpressure */
405 nic_reg_write(nic, NIC_PF_BP_CFG, (1ULL << 6) | 0x03);
406
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530407 /* TNS and TNS bypass modes are present only on 88xx */
408 if (nic->pdev->subsystem_device == PCI_SUBSYS_DEVID_88XX_NIC_PF) {
409 /* Disable TNS mode on both interfaces */
410 nic_reg_write(nic, NIC_PF_INTF_0_1_SEND_CFG,
411 (NIC_TNS_BYPASS_MODE << 7) | BGX0_BLOCK);
412 nic_reg_write(nic, NIC_PF_INTF_0_1_SEND_CFG | (1 << 8),
413 (NIC_TNS_BYPASS_MODE << 7) | BGX1_BLOCK);
414 }
415
Sunil Goutham4863dea2015-05-26 19:20:15 -0700416 nic_reg_write(nic, NIC_PF_INTF_0_1_BP_CFG,
417 (1ULL << 63) | BGX0_BLOCK);
418 nic_reg_write(nic, NIC_PF_INTF_0_1_BP_CFG + (1 << 8),
419 (1ULL << 63) | BGX1_BLOCK);
420
421 /* PKIND configuration */
422 nic->pkind.minlen = 0;
423 nic->pkind.maxlen = NIC_HW_MAX_FRS + ETH_HLEN;
424 nic->pkind.lenerr_en = 1;
425 nic->pkind.rx_hdr = 0;
426 nic->pkind.hdr_sl = 0;
427
428 for (i = 0; i < NIC_MAX_PKIND; i++)
429 nic_reg_write(nic, NIC_PF_PKIND_0_15_CFG | (i << 3),
430 *(u64 *)&nic->pkind);
431
432 nic_set_tx_pkt_pad(nic, NIC_HW_MIN_FRS);
433
434 /* Timer config */
435 nic_reg_write(nic, NIC_PF_INTR_TIMER_CFG, NICPF_CLK_PER_INT_TICK);
Sunil Gouthamaa2e2592015-08-30 12:29:13 +0300436
437 /* Enable VLAN ethertype matching and stripping */
438 nic_reg_write(nic, NIC_PF_RX_ETYPE_0_7,
439 (2 << 19) | (ETYPE_ALG_VLAN_STRIP << 16) | ETH_P_8021Q);
Sunil Goutham4c0b6eaf2016-02-24 16:40:50 +0530440
441 /* Check if HW expected value is higher (could be in future chips) */
442 cqm_cfg = nic_reg_read(nic, NIC_PF_CQM_CFG);
443 if (cqm_cfg < NICPF_CQM_MIN_DROP_LEVEL)
444 nic_reg_write(nic, NIC_PF_CQM_CFG, NICPF_CQM_MIN_DROP_LEVEL);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700445}
446
447/* Channel parse index configuration */
448static void nic_config_cpi(struct nicpf *nic, struct cpi_cfg_msg *cfg)
449{
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530450 struct hw_info *hw = nic->hw;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700451 u32 vnic, bgx, lmac, chan;
452 u32 padd, cpi_count = 0;
453 u64 cpi_base, cpi, rssi_base, rssi;
454 u8 qset, rq_idx = 0;
455
456 vnic = cfg->vf_id;
457 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vnic]);
458 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vnic]);
459
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530460 chan = (lmac * hw->chans_per_lmac) + (bgx * hw->chans_per_bgx);
Sunil Goutham64658592016-08-12 16:51:33 +0530461 cpi_base = vnic * NIC_MAX_CPI_PER_LMAC;
462 rssi_base = vnic * hw->rss_ind_tbl_size;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700463
464 /* Rx channel configuration */
465 nic_reg_write(nic, NIC_PF_CHAN_0_255_RX_BP_CFG | (chan << 3),
466 (1ull << 63) | (vnic << 0));
467 nic_reg_write(nic, NIC_PF_CHAN_0_255_RX_CFG | (chan << 3),
468 ((u64)cfg->cpi_alg << 62) | (cpi_base << 48));
469
470 if (cfg->cpi_alg == CPI_ALG_NONE)
471 cpi_count = 1;
472 else if (cfg->cpi_alg == CPI_ALG_VLAN) /* 3 bits of PCP */
473 cpi_count = 8;
474 else if (cfg->cpi_alg == CPI_ALG_VLAN16) /* 3 bits PCP + DEI */
475 cpi_count = 16;
476 else if (cfg->cpi_alg == CPI_ALG_DIFF) /* 6bits DSCP */
477 cpi_count = NIC_MAX_CPI_PER_LMAC;
478
479 /* RSS Qset, Qidx mapping */
480 qset = cfg->vf_id;
481 rssi = rssi_base;
482 for (; rssi < (rssi_base + cfg->rq_cnt); rssi++) {
483 nic_reg_write(nic, NIC_PF_RSSI_0_4097_RQ | (rssi << 3),
484 (qset << 3) | rq_idx);
485 rq_idx++;
486 }
487
488 rssi = 0;
489 cpi = cpi_base;
490 for (; cpi < (cpi_base + cpi_count); cpi++) {
491 /* Determine port to channel adder */
492 if (cfg->cpi_alg != CPI_ALG_DIFF)
493 padd = cpi % cpi_count;
494 else
495 padd = cpi % 8; /* 3 bits CS out of 6bits DSCP */
496
497 /* Leave RSS_SIZE as '0' to disable RSS */
Sunil Goutham40fb5f82015-12-10 13:25:19 +0530498 if (pass1_silicon(nic->pdev)) {
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700499 nic_reg_write(nic, NIC_PF_CPI_0_2047_CFG | (cpi << 3),
500 (vnic << 24) | (padd << 16) |
501 (rssi_base + rssi));
502 } else {
503 /* Set MPI_ALG to '0' to disable MCAM parsing */
504 nic_reg_write(nic, NIC_PF_CPI_0_2047_CFG | (cpi << 3),
505 (padd << 16));
506 /* MPI index is same as CPI if MPI_ALG is not enabled */
507 nic_reg_write(nic, NIC_PF_MPI_0_2047_CFG | (cpi << 3),
508 (vnic << 24) | (rssi_base + rssi));
509 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700510
511 if ((rssi + 1) >= cfg->rq_cnt)
512 continue;
513
514 if (cfg->cpi_alg == CPI_ALG_VLAN)
515 rssi++;
516 else if (cfg->cpi_alg == CPI_ALG_VLAN16)
517 rssi = ((cpi - cpi_base) & 0xe) >> 1;
518 else if (cfg->cpi_alg == CPI_ALG_DIFF)
519 rssi = ((cpi - cpi_base) & 0x38) >> 3;
520 }
521 nic->cpi_base[cfg->vf_id] = cpi_base;
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700522 nic->rssi_base[cfg->vf_id] = rssi_base;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700523}
524
525/* Responsds to VF with its RSS indirection table size */
526static void nic_send_rss_size(struct nicpf *nic, int vf)
527{
528 union nic_mbx mbx = {};
529 u64 *msg;
530
531 msg = (u64 *)&mbx;
532
533 mbx.rss_size.msg = NIC_MBOX_MSG_RSS_SIZE;
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530534 mbx.rss_size.ind_tbl_size = nic->hw->rss_ind_tbl_size;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700535 nic_send_msg_to_vf(nic, vf, &mbx);
536}
537
538/* Receive side scaling configuration
539 * configure:
540 * - RSS index
541 * - indir table i.e hash::RQ mapping
542 * - no of hash bits to consider
543 */
544static void nic_config_rss(struct nicpf *nic, struct rss_cfg_msg *cfg)
545{
546 u8 qset, idx = 0;
547 u64 cpi_cfg, cpi_base, rssi_base, rssi;
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700548 u64 idx_addr;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700549
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700550 rssi_base = nic->rssi_base[cfg->vf_id] + cfg->tbl_offset;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700551
552 rssi = rssi_base;
553 qset = cfg->vf_id;
554
555 for (; rssi < (rssi_base + cfg->tbl_len); rssi++) {
Sunil Goutham92dc8762015-08-30 12:29:15 +0300556 u8 svf = cfg->ind_tbl[idx] >> 3;
557
558 if (svf)
559 qset = nic->vf_sqs[cfg->vf_id][svf - 1];
560 else
561 qset = cfg->vf_id;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700562 nic_reg_write(nic, NIC_PF_RSSI_0_4097_RQ | (rssi << 3),
563 (qset << 3) | (cfg->ind_tbl[idx] & 0x7));
564 idx++;
565 }
566
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700567 cpi_base = nic->cpi_base[cfg->vf_id];
Sunil Goutham40fb5f82015-12-10 13:25:19 +0530568 if (pass1_silicon(nic->pdev))
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700569 idx_addr = NIC_PF_CPI_0_2047_CFG;
570 else
571 idx_addr = NIC_PF_MPI_0_2047_CFG;
572 cpi_cfg = nic_reg_read(nic, idx_addr | (cpi_base << 3));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700573 cpi_cfg &= ~(0xFULL << 20);
574 cpi_cfg |= (cfg->hash_bits << 20);
Thanneeru Srinivasulu34411b62015-10-23 17:14:10 -0700575 nic_reg_write(nic, idx_addr | (cpi_base << 3), cpi_cfg);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700576}
577
578/* 4 level transmit side scheduler configutation
579 * for TNS bypass mode
580 *
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530581 * Sample configuration for SQ0 on 88xx
Sunil Goutham4863dea2015-05-26 19:20:15 -0700582 * VNIC0-SQ0 -> TL4(0) -> TL3[0] -> TL2[0] -> TL1[0] -> BGX0
583 * VNIC1-SQ0 -> TL4(8) -> TL3[2] -> TL2[0] -> TL1[0] -> BGX0
584 * VNIC2-SQ0 -> TL4(16) -> TL3[4] -> TL2[1] -> TL1[0] -> BGX0
585 * VNIC3-SQ0 -> TL4(24) -> TL3[6] -> TL2[1] -> TL1[0] -> BGX0
586 * VNIC4-SQ0 -> TL4(512) -> TL3[128] -> TL2[32] -> TL1[1] -> BGX1
587 * VNIC5-SQ0 -> TL4(520) -> TL3[130] -> TL2[32] -> TL1[1] -> BGX1
588 * VNIC6-SQ0 -> TL4(528) -> TL3[132] -> TL2[33] -> TL1[1] -> BGX1
589 * VNIC7-SQ0 -> TL4(536) -> TL3[134] -> TL2[33] -> TL1[1] -> BGX1
590 */
Sunil Goutham92dc8762015-08-30 12:29:15 +0300591static void nic_tx_channel_cfg(struct nicpf *nic, u8 vnic,
592 struct sq_cfg_msg *sq)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700593{
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530594 struct hw_info *hw = nic->hw;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700595 u32 bgx, lmac, chan;
596 u32 tl2, tl3, tl4;
597 u32 rr_quantum;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300598 u8 sq_idx = sq->sq_num;
599 u8 pqs_vnic;
Sunil Goutham3e29adb2016-06-27 15:30:03 +0530600 int svf;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700601
Sunil Goutham92dc8762015-08-30 12:29:15 +0300602 if (sq->sqs_mode)
603 pqs_vnic = nic->pqs_vf[vnic];
604 else
605 pqs_vnic = vnic;
606
607 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[pqs_vnic]);
608 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[pqs_vnic]);
609
Sunil Goutham4863dea2015-05-26 19:20:15 -0700610 /* 24 bytes for FCS, IPG and preamble */
611 rr_quantum = ((NIC_HW_MAX_FRS + 24) / 4);
612
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530613 /* For 88xx 0-511 TL4 transmits via BGX0 and
614 * 512-1023 TL4s transmit via BGX1.
615 */
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530616 if (hw->tl1_per_bgx) {
617 tl4 = bgx * (hw->tl4_cnt / hw->bgx_cnt);
618 if (!sq->sqs_mode) {
619 tl4 += (lmac * MAX_QUEUES_PER_QSET);
620 } else {
621 for (svf = 0; svf < MAX_SQS_PER_VF; svf++) {
622 if (nic->vf_sqs[pqs_vnic][svf] == vnic)
623 break;
624 }
625 tl4 += (MAX_LMAC_PER_BGX * MAX_QUEUES_PER_QSET);
626 tl4 += (lmac * MAX_QUEUES_PER_QSET * MAX_SQS_PER_VF);
627 tl4 += (svf * MAX_QUEUES_PER_QSET);
Sunil Goutham3e29adb2016-06-27 15:30:03 +0530628 }
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530629 } else {
630 tl4 = (vnic * MAX_QUEUES_PER_QSET);
Sunil Goutham3e29adb2016-06-27 15:30:03 +0530631 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700632 tl4 += sq_idx;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300633
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530634 tl3 = tl4 / (hw->tl4_cnt / hw->tl3_cnt);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700635 nic_reg_write(nic, NIC_PF_QSET_0_127_SQ_0_7_CFG2 |
636 ((u64)vnic << NIC_QS_ID_SHIFT) |
637 ((u32)sq_idx << NIC_Q_NUM_SHIFT), tl4);
638 nic_reg_write(nic, NIC_PF_TL4_0_1023_CFG | (tl4 << 3),
639 ((u64)vnic << 27) | ((u32)sq_idx << 24) | rr_quantum);
640
641 nic_reg_write(nic, NIC_PF_TL3_0_255_CFG | (tl3 << 3), rr_quantum);
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530642
643 /* On 88xx 0-127 channels are for BGX0 and
644 * 127-255 channels for BGX1.
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530645 *
646 * On 81xx/83xx TL3_CHAN reg should be configured with channel
647 * within LMAC i.e 0-7 and not the actual channel number like on 88xx
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530648 */
649 chan = (lmac * hw->chans_per_lmac) + (bgx * hw->chans_per_bgx);
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530650 if (hw->tl1_per_bgx)
651 nic_reg_write(nic, NIC_PF_TL3_0_255_CHAN | (tl3 << 3), chan);
652 else
653 nic_reg_write(nic, NIC_PF_TL3_0_255_CHAN | (tl3 << 3), 0);
Sunil Gouthama5c3d492016-08-12 16:51:24 +0530654
Sunil Goutham4863dea2015-05-26 19:20:15 -0700655 /* Enable backpressure on the channel */
656 nic_reg_write(nic, NIC_PF_CHAN_0_255_TX_CFG | (chan << 3), 1);
657
658 tl2 = tl3 >> 2;
659 nic_reg_write(nic, NIC_PF_TL3A_0_63_CFG | (tl2 << 3), tl2);
660 nic_reg_write(nic, NIC_PF_TL2_0_63_CFG | (tl2 << 3), rr_quantum);
661 /* No priorities as of now */
662 nic_reg_write(nic, NIC_PF_TL2_0_63_PRI | (tl2 << 3), 0x00);
Sunil Goutham0025d93e2016-08-12 16:51:26 +0530663
664 /* Unlike 88xx where TL2s 0-31 transmits to TL1 '0' and rest to TL1 '1'
665 * on 81xx/83xx TL2 needs to be configured to transmit to one of the
666 * possible LMACs.
667 *
668 * This register doesn't exist on 88xx.
669 */
670 if (!hw->tl1_per_bgx)
671 nic_reg_write(nic, NIC_PF_TL2_LMAC | (tl2 << 3),
672 lmac + (bgx * MAX_LMAC_PER_BGX));
Sunil Goutham4863dea2015-05-26 19:20:15 -0700673}
674
Sunil Goutham92dc8762015-08-30 12:29:15 +0300675/* Send primary nicvf pointer to secondary QS's VF */
676static void nic_send_pnicvf(struct nicpf *nic, int sqs)
677{
678 union nic_mbx mbx = {};
679
680 mbx.nicvf.msg = NIC_MBOX_MSG_PNICVF_PTR;
681 mbx.nicvf.nicvf = nic->nicvf[nic->pqs_vf[sqs]];
682 nic_send_msg_to_vf(nic, sqs, &mbx);
683}
684
685/* Send SQS's nicvf pointer to primary QS's VF */
686static void nic_send_snicvf(struct nicpf *nic, struct nicvf_ptr *nicvf)
687{
688 union nic_mbx mbx = {};
689 int sqs_id = nic->vf_sqs[nicvf->vf_id][nicvf->sqs_id];
690
691 mbx.nicvf.msg = NIC_MBOX_MSG_SNICVF_PTR;
692 mbx.nicvf.sqs_id = nicvf->sqs_id;
693 mbx.nicvf.nicvf = nic->nicvf[sqs_id];
694 nic_send_msg_to_vf(nic, nicvf->vf_id, &mbx);
695}
696
697/* Find next available Qset that can be assigned as a
698 * secondary Qset to a VF.
699 */
700static int nic_nxt_avail_sqs(struct nicpf *nic)
701{
702 int sqs;
703
704 for (sqs = 0; sqs < nic->num_sqs_en; sqs++) {
705 if (!nic->sqs_used[sqs])
706 nic->sqs_used[sqs] = true;
707 else
708 continue;
709 return sqs + nic->num_vf_en;
710 }
711 return -1;
712}
713
714/* Allocate additional Qsets for requested VF */
715static void nic_alloc_sqs(struct nicpf *nic, struct sqs_alloc *sqs)
716{
717 union nic_mbx mbx = {};
718 int idx, alloc_qs = 0;
719 int sqs_id;
720
721 if (!nic->num_sqs_en)
722 goto send_mbox;
723
724 for (idx = 0; idx < sqs->qs_count; idx++) {
725 sqs_id = nic_nxt_avail_sqs(nic);
726 if (sqs_id < 0)
727 break;
728 nic->vf_sqs[sqs->vf_id][idx] = sqs_id;
729 nic->pqs_vf[sqs_id] = sqs->vf_id;
730 alloc_qs++;
731 }
732
733send_mbox:
734 mbx.sqs_alloc.msg = NIC_MBOX_MSG_ALLOC_SQS;
735 mbx.sqs_alloc.vf_id = sqs->vf_id;
736 mbx.sqs_alloc.qs_count = alloc_qs;
737 nic_send_msg_to_vf(nic, sqs->vf_id, &mbx);
738}
739
Sunil Gouthamd77a2382015-08-30 12:29:16 +0300740static int nic_config_loopback(struct nicpf *nic, struct set_loopback *lbk)
741{
742 int bgx_idx, lmac_idx;
743
744 if (lbk->vf_id > MAX_LMAC)
745 return -1;
746
747 bgx_idx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lbk->vf_id]);
748 lmac_idx = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lbk->vf_id]);
749
750 bgx_lmac_internal_loopback(nic->node, bgx_idx, lmac_idx, lbk->enable);
751
752 return 0;
753}
754
Pavel Fedinf406ce42015-12-08 10:37:44 +0300755static void nic_enable_vf(struct nicpf *nic, int vf, bool enable)
756{
757 int bgx, lmac;
758
759 nic->vf_enabled[vf] = enable;
760
761 if (vf >= nic->num_vf_en)
762 return;
763
764 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
765 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
766
767 bgx_lmac_rx_tx_enable(nic->node, bgx, lmac, enable);
768}
769
Sunil Goutham4863dea2015-05-26 19:20:15 -0700770/* Interrupt handler to handle mailbox messages from VFs */
771static void nic_handle_mbx_intr(struct nicpf *nic, int vf)
772{
773 union nic_mbx mbx = {};
774 u64 *mbx_data;
775 u64 mbx_addr;
776 u64 reg_addr;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300777 u64 cfg;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700778 int bgx, lmac;
779 int i;
780 int ret = 0;
781
782 nic->mbx_lock[vf] = true;
783
784 mbx_addr = nic_get_mbx_addr(vf);
785 mbx_data = (u64 *)&mbx;
786
787 for (i = 0; i < NIC_PF_VF_MAILBOX_SIZE; i++) {
788 *mbx_data = nic_reg_read(nic, mbx_addr);
789 mbx_data++;
790 mbx_addr += sizeof(u64);
791 }
792
793 dev_dbg(&nic->pdev->dev, "%s: Mailbox msg %d from VF%d\n",
794 __func__, mbx.msg.msg, vf);
795 switch (mbx.msg.msg) {
796 case NIC_MBOX_MSG_READY:
797 nic_mbx_send_ready(nic, vf);
Sunil Goutham92dc8762015-08-30 12:29:15 +0300798 if (vf < MAX_LMAC) {
799 nic->link[vf] = 0;
800 nic->duplex[vf] = 0;
801 nic->speed[vf] = 0;
802 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700803 ret = 1;
804 break;
805 case NIC_MBOX_MSG_QS_CFG:
806 reg_addr = NIC_PF_QSET_0_127_CFG |
807 (mbx.qs.num << NIC_QS_ID_SHIFT);
Sunil Goutham92dc8762015-08-30 12:29:15 +0300808 cfg = mbx.qs.cfg;
809 /* Check if its a secondary Qset */
810 if (vf >= nic->num_vf_en) {
811 cfg = cfg & (~0x7FULL);
812 /* Assign this Qset to primary Qset's VF */
813 cfg |= nic->pqs_vf[vf];
814 }
815 nic_reg_write(nic, reg_addr, cfg);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700816 break;
817 case NIC_MBOX_MSG_RQ_CFG:
818 reg_addr = NIC_PF_QSET_0_127_RQ_0_7_CFG |
819 (mbx.rq.qs_num << NIC_QS_ID_SHIFT) |
820 (mbx.rq.rq_num << NIC_Q_NUM_SHIFT);
821 nic_reg_write(nic, reg_addr, mbx.rq.cfg);
Sunil Goutham02a72bd2016-08-12 16:51:28 +0530822 /* Enable CQE_RX2_S extension in CQE_RX descriptor.
823 * This gets appended by default on 81xx/83xx chips,
824 * for consistency enabling the same on 88xx pass2
825 * where this is introduced.
826 */
827 if (pass2_silicon(nic->pdev))
828 nic_reg_write(nic, NIC_PF_RX_CFG, 0x01);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700829 break;
830 case NIC_MBOX_MSG_RQ_BP_CFG:
831 reg_addr = NIC_PF_QSET_0_127_RQ_0_7_BP_CFG |
832 (mbx.rq.qs_num << NIC_QS_ID_SHIFT) |
833 (mbx.rq.rq_num << NIC_Q_NUM_SHIFT);
834 nic_reg_write(nic, reg_addr, mbx.rq.cfg);
835 break;
836 case NIC_MBOX_MSG_RQ_SW_SYNC:
837 ret = nic_rcv_queue_sw_sync(nic);
838 break;
839 case NIC_MBOX_MSG_RQ_DROP_CFG:
840 reg_addr = NIC_PF_QSET_0_127_RQ_0_7_DROP_CFG |
841 (mbx.rq.qs_num << NIC_QS_ID_SHIFT) |
842 (mbx.rq.rq_num << NIC_Q_NUM_SHIFT);
843 nic_reg_write(nic, reg_addr, mbx.rq.cfg);
844 break;
845 case NIC_MBOX_MSG_SQ_CFG:
846 reg_addr = NIC_PF_QSET_0_127_SQ_0_7_CFG |
847 (mbx.sq.qs_num << NIC_QS_ID_SHIFT) |
848 (mbx.sq.sq_num << NIC_Q_NUM_SHIFT);
849 nic_reg_write(nic, reg_addr, mbx.sq.cfg);
Sunil Goutham92dc8762015-08-30 12:29:15 +0300850 nic_tx_channel_cfg(nic, mbx.qs.num, &mbx.sq);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700851 break;
852 case NIC_MBOX_MSG_SET_MAC:
Sunil Goutham92dc8762015-08-30 12:29:15 +0300853 if (vf >= nic->num_vf_en)
854 break;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700855 lmac = mbx.mac.vf_id;
856 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lmac]);
857 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[lmac]);
Aleksey Makarove610cb32015-06-02 11:00:21 -0700858 bgx_set_lmac_mac(nic->node, bgx, lmac, mbx.mac.mac_addr);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700859 break;
860 case NIC_MBOX_MSG_SET_MAX_FRS:
861 ret = nic_update_hw_frs(nic, mbx.frs.max_frs,
862 mbx.frs.vf_id);
863 break;
864 case NIC_MBOX_MSG_CPI_CFG:
865 nic_config_cpi(nic, &mbx.cpi_cfg);
866 break;
867 case NIC_MBOX_MSG_RSS_SIZE:
868 nic_send_rss_size(nic, vf);
869 goto unlock;
870 case NIC_MBOX_MSG_RSS_CFG:
871 case NIC_MBOX_MSG_RSS_CFG_CONT:
872 nic_config_rss(nic, &mbx.rss_cfg);
873 break;
874 case NIC_MBOX_MSG_CFG_DONE:
875 /* Last message of VF config msg sequence */
Pavel Fedinf406ce42015-12-08 10:37:44 +0300876 nic_enable_vf(nic, vf, true);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700877 goto unlock;
878 case NIC_MBOX_MSG_SHUTDOWN:
879 /* First msg in VF teardown sequence */
Sunil Goutham92dc8762015-08-30 12:29:15 +0300880 if (vf >= nic->num_vf_en)
881 nic->sqs_used[vf - nic->num_vf_en] = false;
882 nic->pqs_vf[vf] = 0;
Pavel Fedinf406ce42015-12-08 10:37:44 +0300883 nic_enable_vf(nic, vf, false);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700884 break;
Sunil Goutham92dc8762015-08-30 12:29:15 +0300885 case NIC_MBOX_MSG_ALLOC_SQS:
886 nic_alloc_sqs(nic, &mbx.sqs_alloc);
887 goto unlock;
888 case NIC_MBOX_MSG_NICVF_PTR:
889 nic->nicvf[vf] = mbx.nicvf.nicvf;
890 break;
891 case NIC_MBOX_MSG_PNICVF_PTR:
892 nic_send_pnicvf(nic, vf);
893 goto unlock;
894 case NIC_MBOX_MSG_SNICVF_PTR:
895 nic_send_snicvf(nic, &mbx.nicvf);
896 goto unlock;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700897 case NIC_MBOX_MSG_BGX_STATS:
898 nic_get_bgx_stats(nic, &mbx.bgx_stats);
899 goto unlock;
Sunil Gouthamd77a2382015-08-30 12:29:16 +0300900 case NIC_MBOX_MSG_LOOPBACK:
901 ret = nic_config_loopback(nic, &mbx.lbk);
902 break;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700903 default:
904 dev_err(&nic->pdev->dev,
905 "Invalid msg from VF%d, msg 0x%x\n", vf, mbx.msg.msg);
906 break;
907 }
908
909 if (!ret)
910 nic_mbx_send_ack(nic, vf);
911 else if (mbx.msg.msg != NIC_MBOX_MSG_READY)
912 nic_mbx_send_nack(nic, vf);
913unlock:
914 nic->mbx_lock[vf] = false;
915}
916
Sunil Goutham52358aa2016-08-12 16:51:29 +0530917static irqreturn_t nic_mbx_intr_handler(int irq, void *nic_irq)
Sunil Goutham4863dea2015-05-26 19:20:15 -0700918{
Sunil Goutham52358aa2016-08-12 16:51:29 +0530919 struct nicpf *nic = (struct nicpf *)nic_irq;
920 int mbx;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700921 u64 intr;
922 u8 vf, vf_per_mbx_reg = 64;
923
Sunil Goutham52358aa2016-08-12 16:51:29 +0530924 if (irq == nic->msix_entries[NIC_PF_INTR_ID_MBOX0].vector)
925 mbx = 0;
926 else
927 mbx = 1;
928
Sunil Goutham4863dea2015-05-26 19:20:15 -0700929 intr = nic_reg_read(nic, NIC_PF_MAILBOX_INT + (mbx << 3));
930 dev_dbg(&nic->pdev->dev, "PF interrupt Mbox%d 0x%llx\n", mbx, intr);
931 for (vf = 0; vf < vf_per_mbx_reg; vf++) {
932 if (intr & (1ULL << vf)) {
933 dev_dbg(&nic->pdev->dev, "Intr from VF %d\n",
934 vf + (mbx * vf_per_mbx_reg));
Sunil Goutham92dc8762015-08-30 12:29:15 +0300935
Sunil Goutham4863dea2015-05-26 19:20:15 -0700936 nic_handle_mbx_intr(nic, vf + (mbx * vf_per_mbx_reg));
937 nic_clear_mbx_intr(nic, vf, mbx);
938 }
939 }
Sunil Goutham4863dea2015-05-26 19:20:15 -0700940 return IRQ_HANDLED;
941}
942
943static int nic_enable_msix(struct nicpf *nic)
944{
945 int i, ret;
946
Sunil Goutham52358aa2016-08-12 16:51:29 +0530947 nic->num_vec = pci_msix_vec_count(nic->pdev);
948
949 nic->msix_entries = kmalloc_array(nic->num_vec,
950 sizeof(struct msix_entry),
951 GFP_KERNEL);
952 if (!nic->msix_entries)
953 return -ENOMEM;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700954
955 for (i = 0; i < nic->num_vec; i++)
956 nic->msix_entries[i].entry = i;
957
958 ret = pci_enable_msix(nic->pdev, nic->msix_entries, nic->num_vec);
959 if (ret) {
960 dev_err(&nic->pdev->dev,
Sunil Goutham52358aa2016-08-12 16:51:29 +0530961 "Request for #%d msix vectors failed, returned %d\n",
962 nic->num_vec, ret);
963 kfree(nic->msix_entries);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700964 return ret;
965 }
966
967 nic->msix_enabled = 1;
968 return 0;
969}
970
971static void nic_disable_msix(struct nicpf *nic)
972{
973 if (nic->msix_enabled) {
974 pci_disable_msix(nic->pdev);
Sunil Goutham52358aa2016-08-12 16:51:29 +0530975 kfree(nic->msix_entries);
Sunil Goutham4863dea2015-05-26 19:20:15 -0700976 nic->msix_enabled = 0;
977 nic->num_vec = 0;
978 }
979}
980
981static void nic_free_all_interrupts(struct nicpf *nic)
982{
983 int irq;
984
985 for (irq = 0; irq < nic->num_vec; irq++) {
986 if (nic->irq_allocated[irq])
987 free_irq(nic->msix_entries[irq].vector, nic);
988 nic->irq_allocated[irq] = false;
989 }
990}
991
992static int nic_register_interrupts(struct nicpf *nic)
993{
Sunil Goutham52358aa2016-08-12 16:51:29 +0530994 int i, ret;
Sunil Goutham4863dea2015-05-26 19:20:15 -0700995
996 /* Enable MSI-X */
997 ret = nic_enable_msix(nic);
998 if (ret)
999 return ret;
1000
Sunil Goutham52358aa2016-08-12 16:51:29 +05301001 /* Register mailbox interrupt handler */
1002 for (i = NIC_PF_INTR_ID_MBOX0; i < nic->num_vec; i++) {
1003 sprintf(nic->irq_name[i],
1004 "NICPF Mbox%d", (i - NIC_PF_INTR_ID_MBOX0));
Sunil Goutham4863dea2015-05-26 19:20:15 -07001005
Sunil Goutham52358aa2016-08-12 16:51:29 +05301006 ret = request_irq(nic->msix_entries[i].vector,
1007 nic_mbx_intr_handler, 0,
1008 nic->irq_name[i], nic);
1009 if (ret)
1010 goto fail;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001011
Sunil Goutham52358aa2016-08-12 16:51:29 +05301012 nic->irq_allocated[i] = true;
1013 }
Sunil Goutham4863dea2015-05-26 19:20:15 -07001014
1015 /* Enable mailbox interrupt */
1016 nic_enable_mbx_intr(nic);
1017 return 0;
1018
1019fail:
1020 dev_err(&nic->pdev->dev, "Request irq failed\n");
1021 nic_free_all_interrupts(nic);
Sunil Goutham52358aa2016-08-12 16:51:29 +05301022 nic_disable_msix(nic);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001023 return ret;
1024}
1025
1026static void nic_unregister_interrupts(struct nicpf *nic)
1027{
1028 nic_free_all_interrupts(nic);
1029 nic_disable_msix(nic);
1030}
1031
Sunil Goutham92dc8762015-08-30 12:29:15 +03001032static int nic_num_sqs_en(struct nicpf *nic, int vf_en)
1033{
1034 int pos, sqs_per_vf = MAX_SQS_PER_VF_SINGLE_NODE;
1035 u16 total_vf;
1036
Sunil Goutham3a397eb2016-08-12 16:51:27 +05301037 /* Secondary Qsets are needed only if CPU count is
1038 * morethan MAX_QUEUES_PER_QSET.
1039 */
1040 if (num_online_cpus() <= MAX_QUEUES_PER_QSET)
1041 return 0;
1042
Sunil Goutham92dc8762015-08-30 12:29:15 +03001043 /* Check if its a multi-node environment */
1044 if (nr_node_ids > 1)
1045 sqs_per_vf = MAX_SQS_PER_VF;
1046
1047 pos = pci_find_ext_capability(nic->pdev, PCI_EXT_CAP_ID_SRIOV);
1048 pci_read_config_word(nic->pdev, (pos + PCI_SRIOV_TOTAL_VF), &total_vf);
1049 return min(total_vf - vf_en, vf_en * sqs_per_vf);
1050}
1051
Sunil Goutham4863dea2015-05-26 19:20:15 -07001052static int nic_sriov_init(struct pci_dev *pdev, struct nicpf *nic)
1053{
1054 int pos = 0;
Sunil Goutham92dc8762015-08-30 12:29:15 +03001055 int vf_en;
Sunil Goutham4863dea2015-05-26 19:20:15 -07001056 int err;
1057 u16 total_vf_cnt;
1058
1059 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
1060 if (!pos) {
1061 dev_err(&pdev->dev, "SRIOV capability is not found in PCIe config space\n");
1062 return -ENODEV;
1063 }
1064
1065 pci_read_config_word(pdev, (pos + PCI_SRIOV_TOTAL_VF), &total_vf_cnt);
1066 if (total_vf_cnt < nic->num_vf_en)
1067 nic->num_vf_en = total_vf_cnt;
1068
1069 if (!total_vf_cnt)
1070 return 0;
1071
Sunil Goutham92dc8762015-08-30 12:29:15 +03001072 vf_en = nic->num_vf_en;
1073 nic->num_sqs_en = nic_num_sqs_en(nic, nic->num_vf_en);
1074 vf_en += nic->num_sqs_en;
1075
1076 err = pci_enable_sriov(pdev, vf_en);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001077 if (err) {
1078 dev_err(&pdev->dev, "SRIOV enable failed, num VF is %d\n",
Sunil Goutham92dc8762015-08-30 12:29:15 +03001079 vf_en);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001080 nic->num_vf_en = 0;
1081 return err;
1082 }
1083
1084 dev_info(&pdev->dev, "SRIOV enabled, number of VF available %d\n",
Sunil Goutham92dc8762015-08-30 12:29:15 +03001085 vf_en);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001086
1087 nic->flags |= NIC_SRIOV_ENABLED;
1088 return 0;
1089}
1090
1091/* Poll for BGX LMAC link status and update corresponding VF
1092 * if there is a change, valid only if internal L2 switch
1093 * is not present otherwise VF link is always treated as up
1094 */
1095static void nic_poll_for_link(struct work_struct *work)
1096{
1097 union nic_mbx mbx = {};
1098 struct nicpf *nic;
1099 struct bgx_link_status link;
1100 u8 vf, bgx, lmac;
1101
1102 nic = container_of(work, struct nicpf, dwork.work);
1103
1104 mbx.link_status.msg = NIC_MBOX_MSG_BGX_LINK_CHANGE;
1105
Pavel Fedinf406ce42015-12-08 10:37:44 +03001106 for (vf = 0; vf < nic->num_vf_en; vf++) {
Sunil Goutham4863dea2015-05-26 19:20:15 -07001107 /* Poll only if VF is UP */
1108 if (!nic->vf_enabled[vf])
1109 continue;
1110
1111 /* Get BGX, LMAC indices for the VF */
1112 bgx = NIC_GET_BGX_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
1113 lmac = NIC_GET_LMAC_FROM_VF_LMAC_MAP(nic->vf_lmac_map[vf]);
1114 /* Get interface link status */
1115 bgx_get_lmac_link_state(nic->node, bgx, lmac, &link);
1116
1117 /* Inform VF only if link status changed */
1118 if (nic->link[vf] == link.link_up)
1119 continue;
1120
1121 if (!nic->mbx_lock[vf]) {
1122 nic->link[vf] = link.link_up;
1123 nic->duplex[vf] = link.duplex;
1124 nic->speed[vf] = link.speed;
1125
1126 /* Send a mbox message to VF with current link status */
1127 mbx.link_status.link_up = link.link_up;
1128 mbx.link_status.duplex = link.duplex;
1129 mbx.link_status.speed = link.speed;
1130 nic_send_msg_to_vf(nic, vf, &mbx);
1131 }
1132 }
1133 queue_delayed_work(nic->check_link, &nic->dwork, HZ * 2);
1134}
1135
1136static int nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1137{
1138 struct device *dev = &pdev->dev;
1139 struct nicpf *nic;
1140 int err;
1141
1142 BUILD_BUG_ON(sizeof(union nic_mbx) > 16);
1143
1144 nic = devm_kzalloc(dev, sizeof(*nic), GFP_KERNEL);
1145 if (!nic)
1146 return -ENOMEM;
1147
Sunil Gouthama5c3d492016-08-12 16:51:24 +05301148 nic->hw = devm_kzalloc(dev, sizeof(struct hw_info), GFP_KERNEL);
1149 if (!nic->hw) {
1150 devm_kfree(dev, nic);
1151 return -ENOMEM;
1152 }
1153
Sunil Goutham4863dea2015-05-26 19:20:15 -07001154 pci_set_drvdata(pdev, nic);
1155
1156 nic->pdev = pdev;
1157
1158 err = pci_enable_device(pdev);
1159 if (err) {
1160 dev_err(dev, "Failed to enable PCI device\n");
1161 pci_set_drvdata(pdev, NULL);
1162 return err;
1163 }
1164
1165 err = pci_request_regions(pdev, DRV_NAME);
1166 if (err) {
1167 dev_err(dev, "PCI request regions failed 0x%x\n", err);
1168 goto err_disable_device;
1169 }
1170
1171 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(48));
1172 if (err) {
1173 dev_err(dev, "Unable to get usable DMA configuration\n");
1174 goto err_release_regions;
1175 }
1176
1177 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(48));
1178 if (err) {
1179 dev_err(dev, "Unable to get 48-bit DMA for consistent allocations\n");
1180 goto err_release_regions;
1181 }
1182
1183 /* MAP PF's configuration registers */
1184 nic->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
1185 if (!nic->reg_base) {
1186 dev_err(dev, "Cannot map config register space, aborting\n");
1187 err = -ENOMEM;
1188 goto err_release_regions;
1189 }
1190
Robert Richterd768b672015-06-02 11:00:18 -07001191 nic->node = nic_get_node_id(pdev);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001192
Sunil Goutham4863dea2015-05-26 19:20:15 -07001193 /* Initialize hardware */
1194 nic_init_hw(nic);
1195
Sunil Gouthama5c3d492016-08-12 16:51:24 +05301196 nic_set_lmac_vf_mapping(nic);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001197
1198 /* Register interrupts */
1199 err = nic_register_interrupts(nic);
1200 if (err)
1201 goto err_release_regions;
1202
1203 /* Configure SRIOV */
1204 err = nic_sriov_init(pdev, nic);
1205 if (err)
1206 goto err_unregister_interrupts;
1207
1208 /* Register a physical link status poll fn() */
1209 nic->check_link = alloc_workqueue("check_link_status",
1210 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
1211 if (!nic->check_link) {
1212 err = -ENOMEM;
1213 goto err_disable_sriov;
1214 }
1215
1216 INIT_DELAYED_WORK(&nic->dwork, nic_poll_for_link);
1217 queue_delayed_work(nic->check_link, &nic->dwork, 0);
1218
1219 return 0;
1220
1221err_disable_sriov:
1222 if (nic->flags & NIC_SRIOV_ENABLED)
1223 pci_disable_sriov(pdev);
1224err_unregister_interrupts:
1225 nic_unregister_interrupts(nic);
1226err_release_regions:
1227 pci_release_regions(pdev);
1228err_disable_device:
Sunil Gouthama5c3d492016-08-12 16:51:24 +05301229 devm_kfree(dev, nic->hw);
1230 devm_kfree(dev, nic);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001231 pci_disable_device(pdev);
1232 pci_set_drvdata(pdev, NULL);
1233 return err;
1234}
1235
1236static void nic_remove(struct pci_dev *pdev)
1237{
1238 struct nicpf *nic = pci_get_drvdata(pdev);
1239
1240 if (nic->flags & NIC_SRIOV_ENABLED)
1241 pci_disable_sriov(pdev);
1242
1243 if (nic->check_link) {
1244 /* Destroy work Queue */
Thanneeru Srinivasulua7b1f532015-12-02 15:36:14 +05301245 cancel_delayed_work_sync(&nic->dwork);
Sunil Goutham4863dea2015-05-26 19:20:15 -07001246 destroy_workqueue(nic->check_link);
1247 }
1248
1249 nic_unregister_interrupts(nic);
1250 pci_release_regions(pdev);
Sunil Gouthama5c3d492016-08-12 16:51:24 +05301251
1252 devm_kfree(&pdev->dev, nic->hw);
1253 devm_kfree(&pdev->dev, nic);
1254
Sunil Goutham4863dea2015-05-26 19:20:15 -07001255 pci_disable_device(pdev);
1256 pci_set_drvdata(pdev, NULL);
1257}
1258
1259static struct pci_driver nic_driver = {
1260 .name = DRV_NAME,
1261 .id_table = nic_id_table,
1262 .probe = nic_probe,
1263 .remove = nic_remove,
1264};
1265
1266static int __init nic_init_module(void)
1267{
1268 pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION);
1269
1270 return pci_register_driver(&nic_driver);
1271}
1272
1273static void __exit nic_cleanup_module(void)
1274{
1275 pci_unregister_driver(&nic_driver);
1276}
1277
1278module_init(nic_init_module);
1279module_exit(nic_cleanup_module);