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