1 /*
2  * QLogic qlcnic NIC Driver
3  * Copyright (c) 2009-2013 QLogic Corporation
4  *
5  * See LICENSE.qlcnic for copyright and licensing details.
6  */
7 
8 #include "qlcnic.h"
9 #include "qlcnic_hw.h"
10 
11 static int qlcnic_83xx_enable_vnic_mode(struct qlcnic_adapter *adapter, int lock)
12 {
13 	if (lock) {
14 		if (qlcnic_83xx_lock_driver(adapter))
15 			return -EBUSY;
16 	}
17 	QLCWRX(adapter->ahw, QLC_83XX_VNIC_STATE, QLCNIC_DEV_NPAR_OPER);
18 	if (lock)
19 		qlcnic_83xx_unlock_driver(adapter);
20 
21 	return 0;
22 }
23 
24 int qlcnic_83xx_disable_vnic_mode(struct qlcnic_adapter *adapter, int lock)
25 {
26 	struct qlcnic_hardware_context *ahw = adapter->ahw;
27 
28 	if (lock) {
29 		if (qlcnic_83xx_lock_driver(adapter))
30 			return -EBUSY;
31 	}
32 
33 	QLCWRX(adapter->ahw, QLC_83XX_VNIC_STATE, QLCNIC_DEV_NPAR_NON_OPER);
34 	ahw->idc.vnic_state = QLCNIC_DEV_NPAR_NON_OPER;
35 
36 	if (lock)
37 		qlcnic_83xx_unlock_driver(adapter);
38 
39 	return 0;
40 }
41 
42 int qlcnic_83xx_set_vnic_opmode(struct qlcnic_adapter *adapter)
43 {
44 	u8 id;
45 	int ret = -EBUSY;
46 	u32 data = QLCNIC_MGMT_FUNC;
47 	struct qlcnic_hardware_context *ahw = adapter->ahw;
48 
49 	if (qlcnic_83xx_lock_driver(adapter))
50 		return ret;
51 
52 	id = ahw->pci_func;
53 	data = QLCRDX(adapter->ahw, QLC_83XX_DRV_OP_MODE);
54 	data = (data & ~QLC_83XX_SET_FUNC_OPMODE(0x3, id)) |
55 	       QLC_83XX_SET_FUNC_OPMODE(QLCNIC_MGMT_FUNC, id);
56 
57 	QLCWRX(adapter->ahw, QLC_83XX_DRV_OP_MODE, data);
58 
59 	qlcnic_83xx_unlock_driver(adapter);
60 
61 	return 0;
62 }
63 
64 static void
65 qlcnic_83xx_config_vnic_buff_descriptors(struct qlcnic_adapter *adapter)
66 {
67 	struct qlcnic_hardware_context *ahw = adapter->ahw;
68 
69 	if (ahw->port_type == QLCNIC_XGBE) {
70 		adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_VF;
71 		adapter->max_rxd = MAX_RCV_DESCRIPTORS_VF;
72 		adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
73 		adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
74 
75 	} else if (ahw->port_type == QLCNIC_GBE) {
76 		adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
77 		adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
78 		adapter->max_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
79 		adapter->max_rxd = MAX_RCV_DESCRIPTORS_1G;
80 	}
81 	adapter->num_txd = MAX_CMD_DESCRIPTORS;
82 	adapter->max_rds_rings = MAX_RDS_RINGS;
83 }
84 
85 
86 /**
87  * qlcnic_83xx_init_mgmt_vnic
88  *
89  * @adapter: adapter structure
90  * Management virtual NIC sets the operational mode of other vNIC's and
91  * configures embedded switch (ESWITCH).
92  * Returns: Success(0) or error code.
93  *
94  **/
95 static int qlcnic_83xx_init_mgmt_vnic(struct qlcnic_adapter *adapter)
96 {
97 	struct qlcnic_hardware_context *ahw = adapter->ahw;
98 	struct device *dev = &adapter->pdev->dev;
99 	struct qlcnic_npar_info *npar;
100 	int i, err = -EIO;
101 
102 	qlcnic_83xx_get_minidump_template(adapter);
103 
104 	if (!(adapter->flags & QLCNIC_ADAPTER_INITIALIZED)) {
105 		if (qlcnic_init_pci_info(adapter))
106 			return err;
107 
108 		npar = adapter->npars;
109 
110 		for (i = 0; i < ahw->total_nic_func; i++, npar++) {
111 			dev_info(dev, "id:%d active:%d type:%d port:%d min_bw:%d max_bw:%d mac_addr:%pM\n",
112 				 npar->pci_func, npar->active, npar->type,
113 				 npar->phy_port, npar->min_bw, npar->max_bw,
114 				 npar->mac);
115 		}
116 
117 		dev_info(dev, "Max functions = %d, active functions = %d\n",
118 			 ahw->max_pci_func, ahw->total_nic_func);
119 
120 		if (qlcnic_83xx_set_vnic_opmode(adapter))
121 			return err;
122 
123 		if (qlcnic_set_default_offload_settings(adapter))
124 			return err;
125 	} else {
126 		if (qlcnic_reset_npar_config(adapter))
127 			return err;
128 	}
129 
130 	if (qlcnic_83xx_get_port_info(adapter))
131 		return err;
132 
133 	qlcnic_83xx_config_vnic_buff_descriptors(adapter);
134 	ahw->msix_supported = qlcnic_use_msi_x ? 1 : 0;
135 	adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
136 	qlcnic_83xx_enable_vnic_mode(adapter, 1);
137 
138 	dev_info(dev, "HAL Version: %d, Management function\n",
139 		 ahw->fw_hal_version);
140 
141 	return 0;
142 }
143 
144 static int qlcnic_83xx_init_privileged_vnic(struct qlcnic_adapter *adapter)
145 {
146 	int err = -EIO;
147 
148 	qlcnic_83xx_get_minidump_template(adapter);
149 	if (qlcnic_83xx_get_port_info(adapter))
150 		return err;
151 
152 	qlcnic_83xx_config_vnic_buff_descriptors(adapter);
153 	adapter->ahw->msix_supported = !!qlcnic_use_msi_x;
154 	adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
155 
156 	dev_info(&adapter->pdev->dev,
157 		 "HAL Version: %d, Privileged function\n",
158 		 adapter->ahw->fw_hal_version);
159 	return 0;
160 }
161 
162 static int qlcnic_83xx_init_non_privileged_vnic(struct qlcnic_adapter *adapter)
163 {
164 	int err = -EIO;
165 
166 	qlcnic_83xx_get_fw_version(adapter);
167 	if (qlcnic_set_eswitch_port_config(adapter))
168 		return err;
169 
170 	if (qlcnic_83xx_get_port_info(adapter))
171 		return err;
172 
173 	qlcnic_83xx_config_vnic_buff_descriptors(adapter);
174 	adapter->ahw->msix_supported = !!qlcnic_use_msi_x;
175 	adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
176 
177 	dev_info(&adapter->pdev->dev, "HAL Version: %d, Virtual function\n",
178 		 adapter->ahw->fw_hal_version);
179 
180 	return 0;
181 }
182 
183 /**
184  * qlcnic_83xx_vnic_opmode
185  *
186  * @adapter: adapter structure
187  * Identify virtual NIC operational modes.
188  *
189  * Returns: Success(0) or error code.
190  *
191  **/
192 int qlcnic_83xx_config_vnic_opmode(struct qlcnic_adapter *adapter)
193 {
194 	u32 op_mode, priv_level;
195 	struct qlcnic_hardware_context *ahw = adapter->ahw;
196 	struct qlcnic_nic_template *nic_ops = adapter->nic_ops;
197 
198 	qlcnic_get_func_no(adapter);
199 	op_mode = QLCRDX(adapter->ahw, QLC_83XX_DRV_OP_MODE);
200 
201 	if (op_mode == QLC_83XX_DEFAULT_OPMODE)
202 		priv_level = QLCNIC_MGMT_FUNC;
203 	else
204 		priv_level = QLC_83XX_GET_FUNC_PRIVILEGE(op_mode,
205 							 ahw->pci_func);
206 	switch (priv_level) {
207 	case QLCNIC_NON_PRIV_FUNC:
208 		ahw->op_mode = QLCNIC_NON_PRIV_FUNC;
209 		ahw->idc.state_entry = qlcnic_83xx_idc_ready_state_entry;
210 		nic_ops->init_driver = qlcnic_83xx_init_non_privileged_vnic;
211 		break;
212 	case QLCNIC_PRIV_FUNC:
213 		ahw->op_mode = QLCNIC_PRIV_FUNC;
214 		ahw->idc.state_entry = qlcnic_83xx_idc_vnic_pf_entry;
215 		nic_ops->init_driver = qlcnic_83xx_init_privileged_vnic;
216 		break;
217 	case QLCNIC_MGMT_FUNC:
218 		ahw->op_mode = QLCNIC_MGMT_FUNC;
219 		ahw->idc.state_entry = qlcnic_83xx_idc_ready_state_entry;
220 		nic_ops->init_driver = qlcnic_83xx_init_mgmt_vnic;
221 		break;
222 	default:
223 		dev_err(&adapter->pdev->dev, "Invalid Virtual NIC opmode\n");
224 		return -EIO;
225 	}
226 
227 	if (ahw->capabilities & QLC_83XX_ESWITCH_CAPABILITY) {
228 		adapter->flags |= QLCNIC_ESWITCH_ENABLED;
229 		if (adapter->drv_mac_learn)
230 			adapter->rx_mac_learn = true;
231 	} else {
232 		adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
233 		adapter->rx_mac_learn = false;
234 	}
235 
236 	ahw->idc.vnic_state = QLCNIC_DEV_NPAR_NON_OPER;
237 	ahw->idc.vnic_wait_limit = QLCNIC_DEV_NPAR_OPER_TIMEO;
238 
239 	return 0;
240 }
241 
242 int qlcnic_83xx_check_vnic_state(struct qlcnic_adapter *adapter)
243 {
244 	struct qlcnic_hardware_context *ahw = adapter->ahw;
245 	struct qlc_83xx_idc *idc = &ahw->idc;
246 	u32 state;
247 
248 	state = QLCRDX(ahw, QLC_83XX_VNIC_STATE);
249 	while (state != QLCNIC_DEV_NPAR_OPER && idc->vnic_wait_limit) {
250 		idc->vnic_wait_limit--;
251 		msleep(1000);
252 		state = QLCRDX(ahw, QLC_83XX_VNIC_STATE);
253 	}
254 
255 	if (state != QLCNIC_DEV_NPAR_OPER) {
256 		dev_err(&adapter->pdev->dev,
257 			"vNIC mode not operational, state check timed out.\n");
258 		return -EIO;
259 	}
260 
261 	return 0;
262 }
263 
264 int qlcnic_83xx_set_port_eswitch_status(struct qlcnic_adapter *adapter,
265 					int func, int *port_id)
266 {
267 	struct qlcnic_info nic_info;
268 	int err = 0;
269 
270 	memset(&nic_info, 0, sizeof(struct qlcnic_info));
271 
272 	err = qlcnic_get_nic_info(adapter, &nic_info, func);
273 	if (err)
274 		return err;
275 
276 	if (nic_info.capabilities & QLC_83XX_ESWITCH_CAPABILITY)
277 		*port_id = nic_info.phys_port;
278 	else
279 		err = -EIO;
280 
281 	if (!err)
282 		adapter->eswitch[*port_id].flags |= QLCNIC_SWITCH_ENABLE;
283 
284 	return err;
285 }
286