net/txgbe: support VF promiscuous and allmulticast
[dpdk.git] / drivers / net / txgbe / base / txgbe_vf.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2015-2020
3  */
4
5 #include "txgbe_mbx.h"
6 #include "txgbe_vf.h"
7
8 /**
9  *  txgbe_init_ops_vf - Initialize the pointers for vf
10  *  @hw: pointer to hardware structure
11  *
12  *  This will assign function pointers, adapter-specific functions can
13  *  override the assignment of generic function pointers by assigning
14  *  their own adapter-specific function pointers.
15  *  Does not touch the hardware.
16  **/
17 s32 txgbe_init_ops_vf(struct txgbe_hw *hw)
18 {
19         struct txgbe_mac_info *mac = &hw->mac;
20         struct txgbe_mbx_info *mbx = &hw->mbx;
21
22         /* MAC */
23         mac->reset_hw = txgbe_reset_hw_vf;
24         mac->start_hw = txgbe_start_hw_vf;
25         /* Cannot clear stats on VF */
26         mac->get_mac_addr = txgbe_get_mac_addr_vf;
27         mac->stop_hw = txgbe_stop_hw_vf;
28         mac->negotiate_api_version = txgbevf_negotiate_api_version;
29
30         /* Link */
31         mac->check_link = txgbe_check_mac_link_vf;
32
33         /* RAR, Multicast, VLAN */
34         mac->set_rar = txgbe_set_rar_vf;
35         mac->set_uc_addr = txgbevf_set_uc_addr_vf;
36         mac->update_xcast_mode = txgbevf_update_xcast_mode;
37         mac->set_vfta = txgbe_set_vfta_vf;
38         mac->set_rlpml = txgbevf_rlpml_set_vf;
39
40         mac->max_tx_queues = 1;
41         mac->max_rx_queues = 1;
42
43         mbx->init_params = txgbe_init_mbx_params_vf;
44         mbx->read = txgbe_read_mbx_vf;
45         mbx->write = txgbe_write_mbx_vf;
46         mbx->read_posted = txgbe_read_posted_mbx;
47         mbx->write_posted = txgbe_write_posted_mbx;
48         mbx->check_for_msg = txgbe_check_for_msg_vf;
49         mbx->check_for_ack = txgbe_check_for_ack_vf;
50         mbx->check_for_rst = txgbe_check_for_rst_vf;
51
52         return 0;
53 }
54
55 /* txgbe_virt_clr_reg - Set register to default (power on) state.
56  * @hw: pointer to hardware structure
57  */
58 static void txgbe_virt_clr_reg(struct txgbe_hw *hw)
59 {
60         int i;
61         u32 vfsrrctl;
62
63         /* default values (BUF_SIZE = 2048, HDR_SIZE = 256) */
64         vfsrrctl = TXGBE_RXCFG_HDRLEN(TXGBE_RX_HDR_SIZE);
65         vfsrrctl |= TXGBE_RXCFG_PKTLEN(TXGBE_RX_BUF_SIZE);
66
67         for (i = 0; i < 8; i++) {
68                 wr32m(hw, TXGBE_RXCFG(i),
69                         (TXGBE_RXCFG_HDRLEN_MASK | TXGBE_RXCFG_PKTLEN_MASK),
70                         vfsrrctl);
71         }
72
73         txgbe_flush(hw);
74 }
75
76 /**
77  *  txgbe_start_hw_vf - Prepare hardware for Tx/Rx
78  *  @hw: pointer to hardware structure
79  *
80  *  Starts the hardware by filling the bus info structure and media type, clears
81  *  all on chip counters, initializes receive address registers, multicast
82  *  table, VLAN filter table, calls routine to set up link and flow control
83  *  settings, and leaves transmit and receive units disabled and uninitialized
84  **/
85 s32 txgbe_start_hw_vf(struct txgbe_hw *hw)
86 {
87         /* Clear adapter stopped flag */
88         hw->adapter_stopped = false;
89
90         return 0;
91 }
92
93 /**
94  *  txgbe_reset_hw_vf - Performs hardware reset
95  *  @hw: pointer to hardware structure
96  *
97  *  Resets the hardware by resetting the transmit and receive units, masks and
98  *  clears all interrupts.
99  **/
100 s32 txgbe_reset_hw_vf(struct txgbe_hw *hw)
101 {
102         struct txgbe_mbx_info *mbx = &hw->mbx;
103         u32 timeout = TXGBE_VF_INIT_TIMEOUT;
104         s32 ret_val = TXGBE_ERR_INVALID_MAC_ADDR;
105         u32 msgbuf[TXGBE_VF_PERMADDR_MSG_LEN];
106         u8 *addr = (u8 *)(&msgbuf[1]);
107
108         DEBUGFUNC("txgbevf_reset_hw_vf");
109
110         /* Call adapter stop to disable tx/rx and clear interrupts */
111         hw->mac.stop_hw(hw);
112
113         /* reset the api version */
114         hw->api_version = txgbe_mbox_api_10;
115
116         /* backup msix vectors */
117         mbx->timeout = TXGBE_VF_MBX_INIT_TIMEOUT;
118         msgbuf[0] = TXGBE_VF_BACKUP;
119         mbx->write_posted(hw, msgbuf, 1, 0);
120         msec_delay(10);
121
122         DEBUGOUT("Issuing a function level reset to MAC\n");
123         wr32(hw, TXGBE_VFRST, TXGBE_VFRST_SET);
124         txgbe_flush(hw);
125         msec_delay(50);
126
127         hw->offset_loaded = 1;
128
129         /* we cannot reset while the RSTI / RSTD bits are asserted */
130         while (!mbx->check_for_rst(hw, 0) && timeout) {
131                 timeout--;
132                 /* if it doesn't work, try in 1 ms */
133                 usec_delay(5);
134         }
135
136         if (!timeout)
137                 return TXGBE_ERR_RESET_FAILED;
138
139         /* Reset VF registers to initial values */
140         txgbe_virt_clr_reg(hw);
141
142         /* mailbox timeout can now become active */
143         mbx->timeout = TXGBE_VF_MBX_INIT_TIMEOUT;
144
145         msgbuf[0] = TXGBE_VF_RESET;
146         mbx->write_posted(hw, msgbuf, 1, 0);
147
148         msec_delay(10);
149
150         /*
151          * set our "perm_addr" based on info provided by PF
152          * also set up the mc_filter_type which is piggy backed
153          * on the mac address in word 3
154          */
155         ret_val = mbx->read_posted(hw, msgbuf,
156                         TXGBE_VF_PERMADDR_MSG_LEN, 0);
157         if (ret_val)
158                 return ret_val;
159
160         if (msgbuf[0] != (TXGBE_VF_RESET | TXGBE_VT_MSGTYPE_ACK) &&
161             msgbuf[0] != (TXGBE_VF_RESET | TXGBE_VT_MSGTYPE_NACK))
162                 return TXGBE_ERR_INVALID_MAC_ADDR;
163
164         if (msgbuf[0] == (TXGBE_VF_RESET | TXGBE_VT_MSGTYPE_ACK))
165                 memcpy(hw->mac.perm_addr, addr, ETH_ADDR_LEN);
166
167         hw->mac.mc_filter_type = msgbuf[TXGBE_VF_MC_TYPE_WORD];
168
169         return ret_val;
170 }
171
172 /**
173  *  txgbe_stop_hw_vf - Generic stop Tx/Rx units
174  *  @hw: pointer to hardware structure
175  *
176  *  Sets the adapter_stopped flag within txgbe_hw struct. Clears interrupts,
177  *  disables transmit and receive units. The adapter_stopped flag is used by
178  *  the shared code and drivers to determine if the adapter is in a stopped
179  *  state and should not touch the hardware.
180  **/
181 s32 txgbe_stop_hw_vf(struct txgbe_hw *hw)
182 {
183         u16 i;
184
185         /*
186          * Set the adapter_stopped flag so other driver functions stop touching
187          * the hardware
188          */
189         hw->adapter_stopped = true;
190
191         /* Clear interrupt mask to stop from interrupts being generated */
192         wr32(hw, TXGBE_VFIMC, TXGBE_VFIMC_MASK);
193
194         /* Clear any pending interrupts, flush previous writes */
195         wr32(hw, TXGBE_VFICR, TXGBE_VFICR_MASK);
196
197         /* Disable the transmit unit.  Each queue must be disabled. */
198         for (i = 0; i < hw->mac.max_tx_queues; i++)
199                 wr32(hw, TXGBE_TXCFG(i), TXGBE_TXCFG_FLUSH);
200
201         /* Disable the receive unit by stopping each queue */
202         for (i = 0; i < hw->mac.max_rx_queues; i++)
203                 wr32m(hw, TXGBE_RXCFG(i), TXGBE_RXCFG_ENA, 0);
204
205         /* Clear packet split and pool config */
206         wr32(hw, TXGBE_VFPLCFG, 0);
207         hw->rx_loaded = 1;
208
209         /* flush all queues disables */
210         txgbe_flush(hw);
211         msec_delay(2);
212
213         return 0;
214 }
215
216 STATIC s32 txgbevf_write_msg_read_ack(struct txgbe_hw *hw, u32 *msg,
217                                       u32 *retmsg, u16 size)
218 {
219         struct txgbe_mbx_info *mbx = &hw->mbx;
220         s32 retval = mbx->write_posted(hw, msg, size, 0);
221
222         if (retval)
223                 return retval;
224
225         return mbx->read_posted(hw, retmsg, size, 0);
226 }
227
228 /**
229  *  txgbe_set_rar_vf - set device MAC address
230  *  @hw: pointer to hardware structure
231  *  @index: Receive address register to write
232  *  @addr: Address to put into receive address register
233  *  @vmdq: VMDq "set" or "pool" index
234  *  @enable_addr: set flag that address is active
235  **/
236 s32 txgbe_set_rar_vf(struct txgbe_hw *hw, u32 index, u8 *addr, u32 vmdq,
237                      u32 enable_addr)
238 {
239         u32 msgbuf[3];
240         u8 *msg_addr = (u8 *)(&msgbuf[1]);
241         s32 ret_val;
242         UNREFERENCED_PARAMETER(vmdq, enable_addr, index);
243
244         memset(msgbuf, 0, 12);
245         msgbuf[0] = TXGBE_VF_SET_MAC_ADDR;
246         memcpy(msg_addr, addr, 6);
247         ret_val = txgbevf_write_msg_read_ack(hw, msgbuf, msgbuf, 3);
248
249         msgbuf[0] &= ~TXGBE_VT_MSGTYPE_CTS;
250
251         /* if nacked the address was rejected, use "perm_addr" */
252         if (!ret_val &&
253             (msgbuf[0] == (TXGBE_VF_SET_MAC_ADDR | TXGBE_VT_MSGTYPE_NACK))) {
254                 txgbe_get_mac_addr_vf(hw, hw->mac.addr);
255                 return TXGBE_ERR_MBX;
256         }
257
258         return ret_val;
259 }
260
261 /**
262  *  txgbevf_update_xcast_mode - Update Multicast mode
263  *  @hw: pointer to the HW structure
264  *  @xcast_mode: new multicast mode
265  *
266  *  Updates the Multicast Mode of VF.
267  **/
268 s32 txgbevf_update_xcast_mode(struct txgbe_hw *hw, int xcast_mode)
269 {
270         u32 msgbuf[2];
271         s32 err;
272
273         switch (hw->api_version) {
274         case txgbe_mbox_api_12:
275                 /* New modes were introduced in 1.3 version */
276                 if (xcast_mode > TXGBEVF_XCAST_MODE_ALLMULTI)
277                         return TXGBE_ERR_FEATURE_NOT_SUPPORTED;
278                 /* Fall through */
279         case txgbe_mbox_api_13:
280                 break;
281         default:
282                 return TXGBE_ERR_FEATURE_NOT_SUPPORTED;
283         }
284
285         msgbuf[0] = TXGBE_VF_UPDATE_XCAST_MODE;
286         msgbuf[1] = xcast_mode;
287
288         err = txgbevf_write_msg_read_ack(hw, msgbuf, msgbuf, 2);
289         if (err)
290                 return err;
291
292         msgbuf[0] &= ~TXGBE_VT_MSGTYPE_CTS;
293         if (msgbuf[0] == (TXGBE_VF_UPDATE_XCAST_MODE | TXGBE_VT_MSGTYPE_NACK))
294                 return TXGBE_ERR_FEATURE_NOT_SUPPORTED;
295         return 0;
296 }
297
298 /**
299  *  txgbe_set_vfta_vf - Set/Unset vlan filter table address
300  *  @hw: pointer to the HW structure
301  *  @vlan: 12 bit VLAN ID
302  *  @vind: unused by VF drivers
303  *  @vlan_on: if true then set bit, else clear bit
304  *  @vlvf_bypass: boolean flag indicating updating default pool is okay
305  *
306  *  Turn on/off specified VLAN in the VLAN filter table.
307  **/
308 s32 txgbe_set_vfta_vf(struct txgbe_hw *hw, u32 vlan, u32 vind,
309                       bool vlan_on, bool vlvf_bypass)
310 {
311         u32 msgbuf[2];
312         s32 ret_val;
313         UNREFERENCED_PARAMETER(vind, vlvf_bypass);
314
315         msgbuf[0] = TXGBE_VF_SET_VLAN;
316         msgbuf[1] = vlan;
317         /* Setting the 8 bit field MSG INFO to TRUE indicates "add" */
318         msgbuf[0] |= vlan_on << TXGBE_VT_MSGINFO_SHIFT;
319
320         ret_val = txgbevf_write_msg_read_ack(hw, msgbuf, msgbuf, 2);
321         if (!ret_val && (msgbuf[0] & TXGBE_VT_MSGTYPE_ACK))
322                 return 0;
323
324         return ret_val | (msgbuf[0] & TXGBE_VT_MSGTYPE_NACK);
325 }
326
327 /**
328  * txgbe_get_mac_addr_vf - Read device MAC address
329  * @hw: pointer to the HW structure
330  * @mac_addr: the MAC address
331  **/
332 s32 txgbe_get_mac_addr_vf(struct txgbe_hw *hw, u8 *mac_addr)
333 {
334         int i;
335
336         for (i = 0; i < ETH_ADDR_LEN; i++)
337                 mac_addr[i] = hw->mac.perm_addr[i];
338
339         return 0;
340 }
341
342 s32 txgbevf_set_uc_addr_vf(struct txgbe_hw *hw, u32 index, u8 *addr)
343 {
344         u32 msgbuf[3], msgbuf_chk;
345         u8 *msg_addr = (u8 *)(&msgbuf[1]);
346         s32 ret_val;
347
348         memset(msgbuf, 0, sizeof(msgbuf));
349         /*
350          * If index is one then this is the start of a new list and needs
351          * indication to the PF so it can do it's own list management.
352          * If it is zero then that tells the PF to just clear all of
353          * this VF's macvlans and there is no new list.
354          */
355         msgbuf[0] |= index << TXGBE_VT_MSGINFO_SHIFT;
356         msgbuf[0] |= TXGBE_VF_SET_MACVLAN;
357         msgbuf_chk = msgbuf[0];
358         if (addr)
359                 memcpy(msg_addr, addr, 6);
360
361         ret_val = txgbevf_write_msg_read_ack(hw, msgbuf, msgbuf, 3);
362         if (!ret_val) {
363                 msgbuf[0] &= ~TXGBE_VT_MSGTYPE_CTS;
364
365                 if (msgbuf[0] == (msgbuf_chk | TXGBE_VT_MSGTYPE_NACK))
366                         return TXGBE_ERR_OUT_OF_MEM;
367         }
368
369         return ret_val;
370 }
371
372 /**
373  *  txgbe_check_mac_link_vf - Get link/speed status
374  *  @hw: pointer to hardware structure
375  *  @speed: pointer to link speed
376  *  @link_up: true is link is up, false otherwise
377  *  @autoneg_wait_to_complete: true when waiting for completion is needed
378  *
379  *  Reads the links register to determine if link is up and the current speed
380  **/
381 s32 txgbe_check_mac_link_vf(struct txgbe_hw *hw, u32 *speed,
382                             bool *link_up, bool wait_to_complete)
383 {
384         /**
385          * for a quick link status checking, wait_to_compelet == 0,
386          * skip PF link status checking
387          */
388         bool no_pflink_check = wait_to_complete == 0;
389         struct txgbe_mbx_info *mbx = &hw->mbx;
390         struct txgbe_mac_info *mac = &hw->mac;
391         s32 ret_val = 0;
392         u32 links_reg;
393         u32 in_msg = 0;
394
395         /* If we were hit with a reset drop the link */
396         if (!mbx->check_for_rst(hw, 0) || !mbx->timeout)
397                 mac->get_link_status = true;
398
399         if (!mac->get_link_status)
400                 goto out;
401
402         /* if link status is down no point in checking to see if pf is up */
403         links_reg = rd32(hw, TXGBE_VFSTATUS);
404         if (!(links_reg & TXGBE_VFSTATUS_UP))
405                 goto out;
406
407         /* for SFP+ modules and DA cables it can take up to 500usecs
408          * before the link status is correct
409          */
410         if (mac->type == txgbe_mac_raptor_vf && wait_to_complete) {
411                 if (po32m(hw, TXGBE_VFSTATUS, TXGBE_VFSTATUS_UP,
412                         0, NULL, 5, 100))
413                         goto out;
414         }
415
416         switch (links_reg & TXGBE_VFSTATUS_BW_MASK) {
417         case TXGBE_VFSTATUS_BW_10G:
418                 *speed = TXGBE_LINK_SPEED_10GB_FULL;
419                 break;
420         case TXGBE_VFSTATUS_BW_1G:
421                 *speed = TXGBE_LINK_SPEED_1GB_FULL;
422                 break;
423         case TXGBE_VFSTATUS_BW_100M:
424                 *speed = TXGBE_LINK_SPEED_100M_FULL;
425                 break;
426         default:
427                 *speed = TXGBE_LINK_SPEED_UNKNOWN;
428         }
429
430         if (no_pflink_check) {
431                 if (*speed == TXGBE_LINK_SPEED_UNKNOWN)
432                         mac->get_link_status = true;
433                 else
434                         mac->get_link_status = false;
435
436                 goto out;
437         }
438
439         /* if the read failed it could just be a mailbox collision, best wait
440          * until we are called again and don't report an error
441          */
442         if (mbx->read(hw, &in_msg, 1, 0))
443                 goto out;
444
445         if (!(in_msg & TXGBE_VT_MSGTYPE_CTS)) {
446                 /* msg is not CTS and is NACK we must have lost CTS status */
447                 if (in_msg & TXGBE_VT_MSGTYPE_NACK)
448                         ret_val = -1;
449                 goto out;
450         }
451
452         /* the pf is talking, if we timed out in the past we reinit */
453         if (!mbx->timeout) {
454                 ret_val = -1;
455                 goto out;
456         }
457
458         /* if we passed all the tests above then the link is up and we no
459          * longer need to check for link
460          */
461         mac->get_link_status = false;
462
463 out:
464         *link_up = !mac->get_link_status;
465         return ret_val;
466 }
467
468 /**
469  *  txgbevf_rlpml_set_vf - Set the maximum receive packet length
470  *  @hw: pointer to the HW structure
471  *  @max_size: value to assign to max frame size
472  **/
473 s32 txgbevf_rlpml_set_vf(struct txgbe_hw *hw, u16 max_size)
474 {
475         u32 msgbuf[2];
476         s32 retval;
477
478         msgbuf[0] = TXGBE_VF_SET_LPE;
479         msgbuf[1] = max_size;
480
481         retval = txgbevf_write_msg_read_ack(hw, msgbuf, msgbuf, 2);
482         if (retval)
483                 return retval;
484         if ((msgbuf[0] & TXGBE_VF_SET_LPE) &&
485             (msgbuf[0] & TXGBE_VT_MSGTYPE_NACK))
486                 return TXGBE_ERR_MBX;
487
488         return 0;
489 }
490
491 /**
492  *  txgbevf_negotiate_api_version - Negotiate supported API version
493  *  @hw: pointer to the HW structure
494  *  @api: integer containing requested API version
495  **/
496 int txgbevf_negotiate_api_version(struct txgbe_hw *hw, int api)
497 {
498         int err;
499         u32 msg[3];
500
501         /* Negotiate the mailbox API version */
502         msg[0] = TXGBE_VF_API_NEGOTIATE;
503         msg[1] = api;
504         msg[2] = 0;
505
506         err = txgbevf_write_msg_read_ack(hw, msg, msg, 3);
507         if (!err) {
508                 msg[0] &= ~TXGBE_VT_MSGTYPE_CTS;
509
510                 /* Store value and return 0 on success */
511                 if (msg[0] == (TXGBE_VF_API_NEGOTIATE | TXGBE_VT_MSGTYPE_ACK)) {
512                         hw->api_version = api;
513                         return 0;
514                 }
515
516                 err = TXGBE_ERR_INVALID_ARGUMENT;
517         }
518
519         return err;
520 }
521
522 int txgbevf_get_queues(struct txgbe_hw *hw, unsigned int *num_tcs,
523                        unsigned int *default_tc)
524 {
525         int err, i;
526         u32 msg[5];
527
528         /* do nothing if API doesn't support txgbevf_get_queues */
529         switch (hw->api_version) {
530         case txgbe_mbox_api_11:
531         case txgbe_mbox_api_12:
532         case txgbe_mbox_api_13:
533                 break;
534         default:
535                 return 0;
536         }
537
538         /* Fetch queue configuration from the PF */
539         msg[0] = TXGBE_VF_GET_QUEUES;
540         for (i = 1; i < 5; i++)
541                 msg[i] = 0;
542
543         err = txgbevf_write_msg_read_ack(hw, msg, msg, 5);
544         if (!err) {
545                 msg[0] &= ~TXGBE_VT_MSGTYPE_CTS;
546
547                 /*
548                  * if we didn't get an ACK there must have been
549                  * some sort of mailbox error so we should treat it
550                  * as such
551                  */
552                 if (msg[0] != (TXGBE_VF_GET_QUEUES | TXGBE_VT_MSGTYPE_ACK))
553                         return TXGBE_ERR_MBX;
554
555                 /* record and validate values from message */
556                 hw->mac.max_tx_queues = msg[TXGBE_VF_TX_QUEUES];
557                 if (hw->mac.max_tx_queues == 0 ||
558                     hw->mac.max_tx_queues > TXGBE_VF_MAX_TX_QUEUES)
559                         hw->mac.max_tx_queues = TXGBE_VF_MAX_TX_QUEUES;
560
561                 hw->mac.max_rx_queues = msg[TXGBE_VF_RX_QUEUES];
562                 if (hw->mac.max_rx_queues == 0 ||
563                     hw->mac.max_rx_queues > TXGBE_VF_MAX_RX_QUEUES)
564                         hw->mac.max_rx_queues = TXGBE_VF_MAX_RX_QUEUES;
565
566                 *num_tcs = msg[TXGBE_VF_TRANS_VLAN];
567                 /* in case of unknown state assume we cannot tag frames */
568                 if (*num_tcs > hw->mac.max_rx_queues)
569                         *num_tcs = 1;
570
571                 *default_tc = msg[TXGBE_VF_DEF_QUEUE];
572                 /* default to queue 0 on out-of-bounds queue number */
573                 if (*default_tc >= hw->mac.max_tx_queues)
574                         *default_tc = 0;
575         }
576
577         return err;
578 }