fadecaa11bd211384a8cda557a4bd0ed3a36cc2e
[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         /* RAR, Multicast, VLAN */
31         mac->set_rar = txgbe_set_rar_vf;
32         mac->set_uc_addr = txgbevf_set_uc_addr_vf;
33
34         mac->max_tx_queues = 1;
35         mac->max_rx_queues = 1;
36
37         mbx->init_params = txgbe_init_mbx_params_vf;
38         mbx->read = txgbe_read_mbx_vf;
39         mbx->write = txgbe_write_mbx_vf;
40         mbx->read_posted = txgbe_read_posted_mbx;
41         mbx->write_posted = txgbe_write_posted_mbx;
42         mbx->check_for_msg = txgbe_check_for_msg_vf;
43         mbx->check_for_ack = txgbe_check_for_ack_vf;
44         mbx->check_for_rst = txgbe_check_for_rst_vf;
45
46         return 0;
47 }
48
49 /* txgbe_virt_clr_reg - Set register to default (power on) state.
50  * @hw: pointer to hardware structure
51  */
52 static void txgbe_virt_clr_reg(struct txgbe_hw *hw)
53 {
54         int i;
55         u32 vfsrrctl;
56
57         /* default values (BUF_SIZE = 2048, HDR_SIZE = 256) */
58         vfsrrctl = TXGBE_RXCFG_HDRLEN(TXGBE_RX_HDR_SIZE);
59         vfsrrctl |= TXGBE_RXCFG_PKTLEN(TXGBE_RX_BUF_SIZE);
60
61         for (i = 0; i < 8; i++) {
62                 wr32m(hw, TXGBE_RXCFG(i),
63                         (TXGBE_RXCFG_HDRLEN_MASK | TXGBE_RXCFG_PKTLEN_MASK),
64                         vfsrrctl);
65         }
66
67         txgbe_flush(hw);
68 }
69
70 /**
71  *  txgbe_start_hw_vf - Prepare hardware for Tx/Rx
72  *  @hw: pointer to hardware structure
73  *
74  *  Starts the hardware by filling the bus info structure and media type, clears
75  *  all on chip counters, initializes receive address registers, multicast
76  *  table, VLAN filter table, calls routine to set up link and flow control
77  *  settings, and leaves transmit and receive units disabled and uninitialized
78  **/
79 s32 txgbe_start_hw_vf(struct txgbe_hw *hw)
80 {
81         /* Clear adapter stopped flag */
82         hw->adapter_stopped = false;
83
84         return 0;
85 }
86
87 /**
88  *  txgbe_reset_hw_vf - Performs hardware reset
89  *  @hw: pointer to hardware structure
90  *
91  *  Resets the hardware by resetting the transmit and receive units, masks and
92  *  clears all interrupts.
93  **/
94 s32 txgbe_reset_hw_vf(struct txgbe_hw *hw)
95 {
96         struct txgbe_mbx_info *mbx = &hw->mbx;
97         u32 timeout = TXGBE_VF_INIT_TIMEOUT;
98         s32 ret_val = TXGBE_ERR_INVALID_MAC_ADDR;
99         u32 msgbuf[TXGBE_VF_PERMADDR_MSG_LEN];
100         u8 *addr = (u8 *)(&msgbuf[1]);
101
102         DEBUGFUNC("txgbevf_reset_hw_vf");
103
104         /* Call adapter stop to disable tx/rx and clear interrupts */
105         hw->mac.stop_hw(hw);
106
107         /* reset the api version */
108         hw->api_version = txgbe_mbox_api_10;
109
110         /* backup msix vectors */
111         mbx->timeout = TXGBE_VF_MBX_INIT_TIMEOUT;
112         msgbuf[0] = TXGBE_VF_BACKUP;
113         mbx->write_posted(hw, msgbuf, 1, 0);
114         msec_delay(10);
115
116         DEBUGOUT("Issuing a function level reset to MAC\n");
117         wr32(hw, TXGBE_VFRST, TXGBE_VFRST_SET);
118         txgbe_flush(hw);
119         msec_delay(50);
120
121         hw->offset_loaded = 1;
122
123         /* we cannot reset while the RSTI / RSTD bits are asserted */
124         while (!mbx->check_for_rst(hw, 0) && timeout) {
125                 timeout--;
126                 /* if it doesn't work, try in 1 ms */
127                 usec_delay(5);
128         }
129
130         if (!timeout)
131                 return TXGBE_ERR_RESET_FAILED;
132
133         /* Reset VF registers to initial values */
134         txgbe_virt_clr_reg(hw);
135
136         /* mailbox timeout can now become active */
137         mbx->timeout = TXGBE_VF_MBX_INIT_TIMEOUT;
138
139         msgbuf[0] = TXGBE_VF_RESET;
140         mbx->write_posted(hw, msgbuf, 1, 0);
141
142         msec_delay(10);
143
144         /*
145          * set our "perm_addr" based on info provided by PF
146          * also set up the mc_filter_type which is piggy backed
147          * on the mac address in word 3
148          */
149         ret_val = mbx->read_posted(hw, msgbuf,
150                         TXGBE_VF_PERMADDR_MSG_LEN, 0);
151         if (ret_val)
152                 return ret_val;
153
154         if (msgbuf[0] != (TXGBE_VF_RESET | TXGBE_VT_MSGTYPE_ACK) &&
155             msgbuf[0] != (TXGBE_VF_RESET | TXGBE_VT_MSGTYPE_NACK))
156                 return TXGBE_ERR_INVALID_MAC_ADDR;
157
158         if (msgbuf[0] == (TXGBE_VF_RESET | TXGBE_VT_MSGTYPE_ACK))
159                 memcpy(hw->mac.perm_addr, addr, ETH_ADDR_LEN);
160
161         hw->mac.mc_filter_type = msgbuf[TXGBE_VF_MC_TYPE_WORD];
162
163         return ret_val;
164 }
165
166 /**
167  *  txgbe_stop_hw_vf - Generic stop Tx/Rx units
168  *  @hw: pointer to hardware structure
169  *
170  *  Sets the adapter_stopped flag within txgbe_hw struct. Clears interrupts,
171  *  disables transmit and receive units. The adapter_stopped flag is used by
172  *  the shared code and drivers to determine if the adapter is in a stopped
173  *  state and should not touch the hardware.
174  **/
175 s32 txgbe_stop_hw_vf(struct txgbe_hw *hw)
176 {
177         u16 i;
178
179         /*
180          * Set the adapter_stopped flag so other driver functions stop touching
181          * the hardware
182          */
183         hw->adapter_stopped = true;
184
185         /* Clear interrupt mask to stop from interrupts being generated */
186         wr32(hw, TXGBE_VFIMC, TXGBE_VFIMC_MASK);
187
188         /* Clear any pending interrupts, flush previous writes */
189         wr32(hw, TXGBE_VFICR, TXGBE_VFICR_MASK);
190
191         /* Disable the transmit unit.  Each queue must be disabled. */
192         for (i = 0; i < hw->mac.max_tx_queues; i++)
193                 wr32(hw, TXGBE_TXCFG(i), TXGBE_TXCFG_FLUSH);
194
195         /* Disable the receive unit by stopping each queue */
196         for (i = 0; i < hw->mac.max_rx_queues; i++)
197                 wr32m(hw, TXGBE_RXCFG(i), TXGBE_RXCFG_ENA, 0);
198
199         /* Clear packet split and pool config */
200         wr32(hw, TXGBE_VFPLCFG, 0);
201         hw->rx_loaded = 1;
202
203         /* flush all queues disables */
204         txgbe_flush(hw);
205         msec_delay(2);
206
207         return 0;
208 }
209
210 STATIC s32 txgbevf_write_msg_read_ack(struct txgbe_hw *hw, u32 *msg,
211                                       u32 *retmsg, u16 size)
212 {
213         struct txgbe_mbx_info *mbx = &hw->mbx;
214         s32 retval = mbx->write_posted(hw, msg, size, 0);
215
216         if (retval)
217                 return retval;
218
219         return mbx->read_posted(hw, retmsg, size, 0);
220 }
221
222 /**
223  *  txgbe_set_rar_vf - set device MAC address
224  *  @hw: pointer to hardware structure
225  *  @index: Receive address register to write
226  *  @addr: Address to put into receive address register
227  *  @vmdq: VMDq "set" or "pool" index
228  *  @enable_addr: set flag that address is active
229  **/
230 s32 txgbe_set_rar_vf(struct txgbe_hw *hw, u32 index, u8 *addr, u32 vmdq,
231                      u32 enable_addr)
232 {
233         u32 msgbuf[3];
234         u8 *msg_addr = (u8 *)(&msgbuf[1]);
235         s32 ret_val;
236         UNREFERENCED_PARAMETER(vmdq, enable_addr, index);
237
238         memset(msgbuf, 0, 12);
239         msgbuf[0] = TXGBE_VF_SET_MAC_ADDR;
240         memcpy(msg_addr, addr, 6);
241         ret_val = txgbevf_write_msg_read_ack(hw, msgbuf, msgbuf, 3);
242
243         msgbuf[0] &= ~TXGBE_VT_MSGTYPE_CTS;
244
245         /* if nacked the address was rejected, use "perm_addr" */
246         if (!ret_val &&
247             (msgbuf[0] == (TXGBE_VF_SET_MAC_ADDR | TXGBE_VT_MSGTYPE_NACK))) {
248                 txgbe_get_mac_addr_vf(hw, hw->mac.addr);
249                 return TXGBE_ERR_MBX;
250         }
251
252         return ret_val;
253 }
254
255 /**
256  * txgbe_get_mac_addr_vf - Read device MAC address
257  * @hw: pointer to the HW structure
258  * @mac_addr: the MAC address
259  **/
260 s32 txgbe_get_mac_addr_vf(struct txgbe_hw *hw, u8 *mac_addr)
261 {
262         int i;
263
264         for (i = 0; i < ETH_ADDR_LEN; i++)
265                 mac_addr[i] = hw->mac.perm_addr[i];
266
267         return 0;
268 }
269
270 s32 txgbevf_set_uc_addr_vf(struct txgbe_hw *hw, u32 index, u8 *addr)
271 {
272         u32 msgbuf[3], msgbuf_chk;
273         u8 *msg_addr = (u8 *)(&msgbuf[1]);
274         s32 ret_val;
275
276         memset(msgbuf, 0, sizeof(msgbuf));
277         /*
278          * If index is one then this is the start of a new list and needs
279          * indication to the PF so it can do it's own list management.
280          * If it is zero then that tells the PF to just clear all of
281          * this VF's macvlans and there is no new list.
282          */
283         msgbuf[0] |= index << TXGBE_VT_MSGINFO_SHIFT;
284         msgbuf[0] |= TXGBE_VF_SET_MACVLAN;
285         msgbuf_chk = msgbuf[0];
286         if (addr)
287                 memcpy(msg_addr, addr, 6);
288
289         ret_val = txgbevf_write_msg_read_ack(hw, msgbuf, msgbuf, 3);
290         if (!ret_val) {
291                 msgbuf[0] &= ~TXGBE_VT_MSGTYPE_CTS;
292
293                 if (msgbuf[0] == (msgbuf_chk | TXGBE_VT_MSGTYPE_NACK))
294                         return TXGBE_ERR_OUT_OF_MEM;
295         }
296
297         return ret_val;
298 }
299
300 /**
301  *  txgbevf_negotiate_api_version - Negotiate supported API version
302  *  @hw: pointer to the HW structure
303  *  @api: integer containing requested API version
304  **/
305 int txgbevf_negotiate_api_version(struct txgbe_hw *hw, int api)
306 {
307         int err;
308         u32 msg[3];
309
310         /* Negotiate the mailbox API version */
311         msg[0] = TXGBE_VF_API_NEGOTIATE;
312         msg[1] = api;
313         msg[2] = 0;
314
315         err = txgbevf_write_msg_read_ack(hw, msg, msg, 3);
316         if (!err) {
317                 msg[0] &= ~TXGBE_VT_MSGTYPE_CTS;
318
319                 /* Store value and return 0 on success */
320                 if (msg[0] == (TXGBE_VF_API_NEGOTIATE | TXGBE_VT_MSGTYPE_ACK)) {
321                         hw->api_version = api;
322                         return 0;
323                 }
324
325                 err = TXGBE_ERR_INVALID_ARGUMENT;
326         }
327
328         return err;
329 }
330
331 int txgbevf_get_queues(struct txgbe_hw *hw, unsigned int *num_tcs,
332                        unsigned int *default_tc)
333 {
334         int err, i;
335         u32 msg[5];
336
337         /* do nothing if API doesn't support txgbevf_get_queues */
338         switch (hw->api_version) {
339         case txgbe_mbox_api_11:
340         case txgbe_mbox_api_12:
341         case txgbe_mbox_api_13:
342                 break;
343         default:
344                 return 0;
345         }
346
347         /* Fetch queue configuration from the PF */
348         msg[0] = TXGBE_VF_GET_QUEUES;
349         for (i = 1; i < 5; i++)
350                 msg[i] = 0;
351
352         err = txgbevf_write_msg_read_ack(hw, msg, msg, 5);
353         if (!err) {
354                 msg[0] &= ~TXGBE_VT_MSGTYPE_CTS;
355
356                 /*
357                  * if we didn't get an ACK there must have been
358                  * some sort of mailbox error so we should treat it
359                  * as such
360                  */
361                 if (msg[0] != (TXGBE_VF_GET_QUEUES | TXGBE_VT_MSGTYPE_ACK))
362                         return TXGBE_ERR_MBX;
363
364                 /* record and validate values from message */
365                 hw->mac.max_tx_queues = msg[TXGBE_VF_TX_QUEUES];
366                 if (hw->mac.max_tx_queues == 0 ||
367                     hw->mac.max_tx_queues > TXGBE_VF_MAX_TX_QUEUES)
368                         hw->mac.max_tx_queues = TXGBE_VF_MAX_TX_QUEUES;
369
370                 hw->mac.max_rx_queues = msg[TXGBE_VF_RX_QUEUES];
371                 if (hw->mac.max_rx_queues == 0 ||
372                     hw->mac.max_rx_queues > TXGBE_VF_MAX_RX_QUEUES)
373                         hw->mac.max_rx_queues = TXGBE_VF_MAX_RX_QUEUES;
374
375                 *num_tcs = msg[TXGBE_VF_TRANS_VLAN];
376                 /* in case of unknown state assume we cannot tag frames */
377                 if (*num_tcs > hw->mac.max_rx_queues)
378                         *num_tcs = 1;
379
380                 *default_tc = msg[TXGBE_VF_DEF_QUEUE];
381                 /* default to queue 0 on out-of-bounds queue number */
382                 if (*default_tc >= hw->mac.max_tx_queues)
383                         *default_tc = 0;
384         }
385
386         return err;
387 }