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