5d4e1015860a85452a8871622f679e88ccb7a746
[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->stop_hw = txgbe_stop_hw_vf;
25         mac->negotiate_api_version = txgbevf_negotiate_api_version;
26
27         mac->max_tx_queues = 1;
28         mac->max_rx_queues = 1;
29
30         mbx->init_params = txgbe_init_mbx_params_vf;
31         mbx->read = txgbe_read_mbx_vf;
32         mbx->write = txgbe_write_mbx_vf;
33         mbx->read_posted = txgbe_read_posted_mbx;
34         mbx->write_posted = txgbe_write_posted_mbx;
35         mbx->check_for_msg = txgbe_check_for_msg_vf;
36         mbx->check_for_ack = txgbe_check_for_ack_vf;
37         mbx->check_for_rst = txgbe_check_for_rst_vf;
38
39         return 0;
40 }
41
42 /* txgbe_virt_clr_reg - Set register to default (power on) state.
43  * @hw: pointer to hardware structure
44  */
45 static void txgbe_virt_clr_reg(struct txgbe_hw *hw)
46 {
47         int i;
48         u32 vfsrrctl;
49
50         /* default values (BUF_SIZE = 2048, HDR_SIZE = 256) */
51         vfsrrctl = TXGBE_RXCFG_HDRLEN(TXGBE_RX_HDR_SIZE);
52         vfsrrctl |= TXGBE_RXCFG_PKTLEN(TXGBE_RX_BUF_SIZE);
53
54         for (i = 0; i < 8; i++) {
55                 wr32m(hw, TXGBE_RXCFG(i),
56                         (TXGBE_RXCFG_HDRLEN_MASK | TXGBE_RXCFG_PKTLEN_MASK),
57                         vfsrrctl);
58         }
59
60         txgbe_flush(hw);
61 }
62
63 /**
64  *  txgbe_reset_hw_vf - Performs hardware reset
65  *  @hw: pointer to hardware structure
66  *
67  *  Resets the hardware by resetting the transmit and receive units, masks and
68  *  clears all interrupts.
69  **/
70 s32 txgbe_reset_hw_vf(struct txgbe_hw *hw)
71 {
72         struct txgbe_mbx_info *mbx = &hw->mbx;
73         u32 timeout = TXGBE_VF_INIT_TIMEOUT;
74         s32 ret_val = TXGBE_ERR_INVALID_MAC_ADDR;
75         u32 msgbuf[TXGBE_VF_PERMADDR_MSG_LEN];
76         u8 *addr = (u8 *)(&msgbuf[1]);
77
78         DEBUGFUNC("txgbevf_reset_hw_vf");
79
80         /* Call adapter stop to disable tx/rx and clear interrupts */
81         hw->mac.stop_hw(hw);
82
83         /* reset the api version */
84         hw->api_version = txgbe_mbox_api_10;
85
86         /* backup msix vectors */
87         mbx->timeout = TXGBE_VF_MBX_INIT_TIMEOUT;
88         msgbuf[0] = TXGBE_VF_BACKUP;
89         mbx->write_posted(hw, msgbuf, 1, 0);
90         msec_delay(10);
91
92         DEBUGOUT("Issuing a function level reset to MAC\n");
93         wr32(hw, TXGBE_VFRST, TXGBE_VFRST_SET);
94         txgbe_flush(hw);
95         msec_delay(50);
96
97         hw->offset_loaded = 1;
98
99         /* we cannot reset while the RSTI / RSTD bits are asserted */
100         while (!mbx->check_for_rst(hw, 0) && timeout) {
101                 timeout--;
102                 /* if it doesn't work, try in 1 ms */
103                 usec_delay(5);
104         }
105
106         if (!timeout)
107                 return TXGBE_ERR_RESET_FAILED;
108
109         /* Reset VF registers to initial values */
110         txgbe_virt_clr_reg(hw);
111
112         /* mailbox timeout can now become active */
113         mbx->timeout = TXGBE_VF_MBX_INIT_TIMEOUT;
114
115         msgbuf[0] = TXGBE_VF_RESET;
116         mbx->write_posted(hw, msgbuf, 1, 0);
117
118         msec_delay(10);
119
120         /*
121          * set our "perm_addr" based on info provided by PF
122          * also set up the mc_filter_type which is piggy backed
123          * on the mac address in word 3
124          */
125         ret_val = mbx->read_posted(hw, msgbuf,
126                         TXGBE_VF_PERMADDR_MSG_LEN, 0);
127         if (ret_val)
128                 return ret_val;
129
130         if (msgbuf[0] != (TXGBE_VF_RESET | TXGBE_VT_MSGTYPE_ACK) &&
131             msgbuf[0] != (TXGBE_VF_RESET | TXGBE_VT_MSGTYPE_NACK))
132                 return TXGBE_ERR_INVALID_MAC_ADDR;
133
134         if (msgbuf[0] == (TXGBE_VF_RESET | TXGBE_VT_MSGTYPE_ACK))
135                 memcpy(hw->mac.perm_addr, addr, ETH_ADDR_LEN);
136
137         hw->mac.mc_filter_type = msgbuf[TXGBE_VF_MC_TYPE_WORD];
138
139         return ret_val;
140 }
141
142 /**
143  *  txgbe_stop_hw_vf - Generic stop Tx/Rx units
144  *  @hw: pointer to hardware structure
145  *
146  *  Sets the adapter_stopped flag within txgbe_hw struct. Clears interrupts,
147  *  disables transmit and receive units. The adapter_stopped flag is used by
148  *  the shared code and drivers to determine if the adapter is in a stopped
149  *  state and should not touch the hardware.
150  **/
151 s32 txgbe_stop_hw_vf(struct txgbe_hw *hw)
152 {
153         u16 i;
154
155         /*
156          * Set the adapter_stopped flag so other driver functions stop touching
157          * the hardware
158          */
159         hw->adapter_stopped = true;
160
161         /* Clear interrupt mask to stop from interrupts being generated */
162         wr32(hw, TXGBE_VFIMC, TXGBE_VFIMC_MASK);
163
164         /* Clear any pending interrupts, flush previous writes */
165         wr32(hw, TXGBE_VFICR, TXGBE_VFICR_MASK);
166
167         /* Disable the transmit unit.  Each queue must be disabled. */
168         for (i = 0; i < hw->mac.max_tx_queues; i++)
169                 wr32(hw, TXGBE_TXCFG(i), TXGBE_TXCFG_FLUSH);
170
171         /* Disable the receive unit by stopping each queue */
172         for (i = 0; i < hw->mac.max_rx_queues; i++)
173                 wr32m(hw, TXGBE_RXCFG(i), TXGBE_RXCFG_ENA, 0);
174
175         /* Clear packet split and pool config */
176         wr32(hw, TXGBE_VFPLCFG, 0);
177         hw->rx_loaded = 1;
178
179         /* flush all queues disables */
180         txgbe_flush(hw);
181         msec_delay(2);
182
183         return 0;
184 }
185
186 STATIC s32 txgbevf_write_msg_read_ack(struct txgbe_hw *hw, u32 *msg,
187                                       u32 *retmsg, u16 size)
188 {
189         struct txgbe_mbx_info *mbx = &hw->mbx;
190         s32 retval = mbx->write_posted(hw, msg, size, 0);
191
192         if (retval)
193                 return retval;
194
195         return mbx->read_posted(hw, retmsg, size, 0);
196 }
197
198 /**
199  *  txgbevf_negotiate_api_version - Negotiate supported API version
200  *  @hw: pointer to the HW structure
201  *  @api: integer containing requested API version
202  **/
203 int txgbevf_negotiate_api_version(struct txgbe_hw *hw, int api)
204 {
205         int err;
206         u32 msg[3];
207
208         /* Negotiate the mailbox API version */
209         msg[0] = TXGBE_VF_API_NEGOTIATE;
210         msg[1] = api;
211         msg[2] = 0;
212
213         err = txgbevf_write_msg_read_ack(hw, msg, msg, 3);
214         if (!err) {
215                 msg[0] &= ~TXGBE_VT_MSGTYPE_CTS;
216
217                 /* Store value and return 0 on success */
218                 if (msg[0] == (TXGBE_VF_API_NEGOTIATE | TXGBE_VT_MSGTYPE_ACK)) {
219                         hw->api_version = api;
220                         return 0;
221                 }
222
223                 err = TXGBE_ERR_INVALID_ARGUMENT;
224         }
225
226         return err;
227 }
228
229 int txgbevf_get_queues(struct txgbe_hw *hw, unsigned int *num_tcs,
230                        unsigned int *default_tc)
231 {
232         int err, i;
233         u32 msg[5];
234
235         /* do nothing if API doesn't support txgbevf_get_queues */
236         switch (hw->api_version) {
237         case txgbe_mbox_api_11:
238         case txgbe_mbox_api_12:
239         case txgbe_mbox_api_13:
240                 break;
241         default:
242                 return 0;
243         }
244
245         /* Fetch queue configuration from the PF */
246         msg[0] = TXGBE_VF_GET_QUEUES;
247         for (i = 1; i < 5; i++)
248                 msg[i] = 0;
249
250         err = txgbevf_write_msg_read_ack(hw, msg, msg, 5);
251         if (!err) {
252                 msg[0] &= ~TXGBE_VT_MSGTYPE_CTS;
253
254                 /*
255                  * if we didn't get an ACK there must have been
256                  * some sort of mailbox error so we should treat it
257                  * as such
258                  */
259                 if (msg[0] != (TXGBE_VF_GET_QUEUES | TXGBE_VT_MSGTYPE_ACK))
260                         return TXGBE_ERR_MBX;
261
262                 /* record and validate values from message */
263                 hw->mac.max_tx_queues = msg[TXGBE_VF_TX_QUEUES];
264                 if (hw->mac.max_tx_queues == 0 ||
265                     hw->mac.max_tx_queues > TXGBE_VF_MAX_TX_QUEUES)
266                         hw->mac.max_tx_queues = TXGBE_VF_MAX_TX_QUEUES;
267
268                 hw->mac.max_rx_queues = msg[TXGBE_VF_RX_QUEUES];
269                 if (hw->mac.max_rx_queues == 0 ||
270                     hw->mac.max_rx_queues > TXGBE_VF_MAX_RX_QUEUES)
271                         hw->mac.max_rx_queues = TXGBE_VF_MAX_RX_QUEUES;
272
273                 *num_tcs = msg[TXGBE_VF_TRANS_VLAN];
274                 /* in case of unknown state assume we cannot tag frames */
275                 if (*num_tcs > hw->mac.max_rx_queues)
276                         *num_tcs = 1;
277
278                 *default_tc = msg[TXGBE_VF_DEF_QUEUE];
279                 /* default to queue 0 on out-of-bounds queue number */
280                 if (*default_tc >= hw->mac.max_tx_queues)
281                         *default_tc = 0;
282         }
283
284         return err;
285 }