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 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 	int err = -EIO;
98 
99 	qlcnic_83xx_get_minidump_template(adapter);
100 	if (!(adapter->flags & QLCNIC_ADAPTER_INITIALIZED)) {
101 		if (qlcnic_init_pci_info(adapter))
102 			return err;
103 
104 		if (qlcnic_83xx_set_vnic_opmode(adapter))
105 			return err;
106 
107 		if (qlcnic_set_default_offload_settings(adapter))
108 			return err;
109 	} else {
110 		if (qlcnic_reset_npar_config(adapter))
111 			return err;
112 	}
113 
114 	if (qlcnic_83xx_get_port_info(adapter))
115 		return err;
116 
117 	qlcnic_83xx_config_vnic_buff_descriptors(adapter);
118 	adapter->ahw->msix_supported = !!qlcnic_use_msi_x;
119 	adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
120 	qlcnic_83xx_enable_vnic_mode(adapter, 1);
121 
122 	dev_info(&adapter->pdev->dev, "HAL Version: %d, Management function\n",
123 		 adapter->ahw->fw_hal_version);
124 
125 	return 0;
126 }
127 
128 static int qlcnic_83xx_init_privileged_vnic(struct qlcnic_adapter *adapter)
129 {
130 	int err = -EIO;
131 
132 	qlcnic_83xx_get_minidump_template(adapter);
133 	if (qlcnic_83xx_get_port_info(adapter))
134 		return err;
135 
136 	qlcnic_83xx_config_vnic_buff_descriptors(adapter);
137 	adapter->ahw->msix_supported = !!qlcnic_use_msi_x;
138 	adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
139 
140 	dev_info(&adapter->pdev->dev,
141 		 "HAL Version: %d, Privileged function\n",
142 		 adapter->ahw->fw_hal_version);
143 	return 0;
144 }
145 
146 static int qlcnic_83xx_init_non_privileged_vnic(struct qlcnic_adapter *adapter)
147 {
148 	int err = -EIO;
149 
150 	qlcnic_83xx_get_fw_version(adapter);
151 	if (qlcnic_set_eswitch_port_config(adapter))
152 		return err;
153 
154 	if (qlcnic_83xx_get_port_info(adapter))
155 		return err;
156 
157 	qlcnic_83xx_config_vnic_buff_descriptors(adapter);
158 	adapter->ahw->msix_supported = !!qlcnic_use_msi_x;
159 	adapter->flags |= QLCNIC_ADAPTER_INITIALIZED;
160 
161 	dev_info(&adapter->pdev->dev, "HAL Version: %d, Virtual function\n",
162 		 adapter->ahw->fw_hal_version);
163 
164 	return 0;
165 }
166 
167 /**
168  * qlcnic_83xx_vnic_opmode
169  *
170  * @adapter: adapter structure
171  * Identify virtual NIC operational modes.
172  *
173  * Returns: Success(0) or error code.
174  *
175  **/
176 int qlcnic_83xx_config_vnic_opmode(struct qlcnic_adapter *adapter)
177 {
178 	u32 op_mode, priv_level;
179 	struct qlcnic_hardware_context *ahw = adapter->ahw;
180 	struct qlcnic_nic_template *nic_ops = adapter->nic_ops;
181 
182 	qlcnic_get_func_no(adapter);
183 	op_mode = QLCRDX(adapter->ahw, QLC_83XX_DRV_OP_MODE);
184 
185 	if (op_mode == QLC_83XX_DEFAULT_OPMODE)
186 		priv_level = QLCNIC_MGMT_FUNC;
187 	else
188 		priv_level = QLC_83XX_GET_FUNC_PRIVILEGE(op_mode,
189 							 ahw->pci_func);
190 	switch (priv_level) {
191 	case QLCNIC_NON_PRIV_FUNC:
192 		ahw->op_mode = QLCNIC_NON_PRIV_FUNC;
193 		ahw->idc.state_entry = qlcnic_83xx_idc_ready_state_entry;
194 		nic_ops->init_driver = qlcnic_83xx_init_non_privileged_vnic;
195 		break;
196 	case QLCNIC_PRIV_FUNC:
197 		ahw->op_mode = QLCNIC_PRIV_FUNC;
198 		ahw->idc.state_entry = qlcnic_83xx_idc_vnic_pf_entry;
199 		nic_ops->init_driver = qlcnic_83xx_init_privileged_vnic;
200 		break;
201 	case QLCNIC_MGMT_FUNC:
202 		ahw->op_mode = QLCNIC_MGMT_FUNC;
203 		ahw->idc.state_entry = qlcnic_83xx_idc_ready_state_entry;
204 		nic_ops->init_driver = qlcnic_83xx_init_mgmt_vnic;
205 		break;
206 	default:
207 		dev_err(&adapter->pdev->dev, "Invalid Virtual NIC opmode\n");
208 		return -EIO;
209 	}
210 
211 	if (ahw->capabilities & QLC_83XX_ESWITCH_CAPABILITY)
212 		adapter->flags |= QLCNIC_ESWITCH_ENABLED;
213 	else
214 		adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
215 
216 	ahw->idc.vnic_state = QLCNIC_DEV_NPAR_NON_OPER;
217 	ahw->idc.vnic_wait_limit = QLCNIC_DEV_NPAR_OPER_TIMEO;
218 
219 	return 0;
220 }
221 
222 int qlcnic_83xx_check_vnic_state(struct qlcnic_adapter *adapter)
223 {
224 	struct qlcnic_hardware_context *ahw = adapter->ahw;
225 	struct qlc_83xx_idc *idc = &ahw->idc;
226 	u32 state;
227 
228 	state = QLCRDX(ahw, QLC_83XX_VNIC_STATE);
229 	while (state != QLCNIC_DEV_NPAR_OPER && idc->vnic_wait_limit--) {
230 		msleep(1000);
231 		state = QLCRDX(ahw, QLC_83XX_VNIC_STATE);
232 	}
233 
234 	if (!idc->vnic_wait_limit) {
235 		dev_err(&adapter->pdev->dev,
236 			"vNIC mode not operational, state check timed out.\n");
237 		return -EIO;
238 	}
239 
240 	return 0;
241 }
242 
243 static int qlcnic_83xx_get_eswitch_port_info(struct qlcnic_adapter *adapter,
244 					     int func, int *port_id)
245 {
246 	struct qlcnic_info nic_info;
247 	int err = 0;
248 
249 	memset(&nic_info, 0, sizeof(struct qlcnic_info));
250 
251 	err = qlcnic_get_nic_info(adapter, &nic_info, func);
252 	if (err)
253 		return err;
254 
255 	if (nic_info.capabilities & QLC_83XX_ESWITCH_CAPABILITY)
256 		*port_id = nic_info.phys_port;
257 	else
258 		err = -EIO;
259 
260 	return err;
261 }
262 
263 int qlcnic_83xx_enable_port_eswitch(struct qlcnic_adapter *adapter, int func)
264 {
265 	int id, err = 0;
266 
267 	err = qlcnic_83xx_get_eswitch_port_info(adapter, func, &id);
268 	if (err)
269 		return err;
270 
271 	if (!(adapter->eswitch[id].flags & QLCNIC_SWITCH_ENABLE)) {
272 		if (!qlcnic_enable_eswitch(adapter, id, 1))
273 			adapter->eswitch[id].flags |= QLCNIC_SWITCH_ENABLE;
274 		else
275 			err = -EIO;
276 	}
277 
278 	return err;
279 }
280