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