1/*******************************************************************************
2
3Copyright (c) 2001-2015, Intel Corporation
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
8
9 1. Redistributions of source code must retain the above copyright notice,
10    this list of conditions and the following disclaimer.
11
12 2. Redistributions in binary form must reproduce the above copyright
13    notice, this list of conditions and the following disclaimer in the
14    documentation and/or other materials provided with the distribution.
15
16 3. Neither the name of the Intel Corporation nor the names of its
17    contributors may be used to endorse or promote products derived from
18    this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30POSSIBILITY OF SUCH DAMAGE.
31
32***************************************************************************/
33
34
35#include "e1000_api.h"
36
37
38STATIC s32 e1000_init_phy_params_vf(struct e1000_hw *hw);
39STATIC s32 e1000_init_nvm_params_vf(struct e1000_hw *hw);
40STATIC void e1000_release_vf(struct e1000_hw *hw);
41STATIC s32 e1000_acquire_vf(struct e1000_hw *hw);
42STATIC s32 e1000_setup_link_vf(struct e1000_hw *hw);
43STATIC s32 e1000_get_bus_info_pcie_vf(struct e1000_hw *hw);
44STATIC s32 e1000_init_mac_params_vf(struct e1000_hw *hw);
45STATIC s32 e1000_check_for_link_vf(struct e1000_hw *hw);
46STATIC s32 e1000_get_link_up_info_vf(struct e1000_hw *hw, u16 *speed,
47				     u16 *duplex);
48STATIC s32 e1000_init_hw_vf(struct e1000_hw *hw);
49STATIC s32 e1000_reset_hw_vf(struct e1000_hw *hw);
50STATIC void e1000_update_mc_addr_list_vf(struct e1000_hw *hw, u8 *, u32);
51STATIC int  e1000_rar_set_vf(struct e1000_hw *, u8 *, u32);
52STATIC s32 e1000_read_mac_addr_vf(struct e1000_hw *);
53
54/**
55 *  e1000_init_phy_params_vf - Inits PHY params
56 *  @hw: pointer to the HW structure
57 *
58 *  Doesn't do much - there's no PHY available to the VF.
59 **/
60STATIC s32 e1000_init_phy_params_vf(struct e1000_hw *hw)
61{
62	DEBUGFUNC("e1000_init_phy_params_vf");
63	hw->phy.type = e1000_phy_vf;
64	hw->phy.ops.acquire = e1000_acquire_vf;
65	hw->phy.ops.release = e1000_release_vf;
66
67	return E1000_SUCCESS;
68}
69
70/**
71 *  e1000_init_nvm_params_vf - Inits NVM params
72 *  @hw: pointer to the HW structure
73 *
74 *  Doesn't do much - there's no NVM available to the VF.
75 **/
76STATIC s32 e1000_init_nvm_params_vf(struct e1000_hw *hw)
77{
78	DEBUGFUNC("e1000_init_nvm_params_vf");
79	hw->nvm.type = e1000_nvm_none;
80	hw->nvm.ops.acquire = e1000_acquire_vf;
81	hw->nvm.ops.release = e1000_release_vf;
82
83	return E1000_SUCCESS;
84}
85
86/**
87 *  e1000_init_mac_params_vf - Inits MAC params
88 *  @hw: pointer to the HW structure
89 **/
90STATIC s32 e1000_init_mac_params_vf(struct e1000_hw *hw)
91{
92	struct e1000_mac_info *mac = &hw->mac;
93
94	DEBUGFUNC("e1000_init_mac_params_vf");
95
96	/* Set media type */
97	/*
98	 * Virtual functions don't care what they're media type is as they
99	 * have no direct access to the PHY, or the media.  That is handled
100	 * by the physical function driver.
101	 */
102	hw->phy.media_type = e1000_media_type_unknown;
103
104	/* No ASF features for the VF driver */
105	mac->asf_firmware_present = false;
106	/* ARC subsystem not supported */
107	mac->arc_subsystem_valid = false;
108	/* Disable adaptive IFS mode so the generic funcs don't do anything */
109	mac->adaptive_ifs = false;
110	/* VF's have no MTA Registers - PF feature only */
111	mac->mta_reg_count = 128;
112	/* VF's have no access to RAR entries  */
113	mac->rar_entry_count = 1;
114
115	/* Function pointers */
116	/* link setup */
117	mac->ops.setup_link = e1000_setup_link_vf;
118	/* bus type/speed/width */
119	mac->ops.get_bus_info = e1000_get_bus_info_pcie_vf;
120	/* reset */
121	mac->ops.reset_hw = e1000_reset_hw_vf;
122	/* hw initialization */
123	mac->ops.init_hw = e1000_init_hw_vf;
124	/* check for link */
125	mac->ops.check_for_link = e1000_check_for_link_vf;
126	/* link info */
127	mac->ops.get_link_up_info = e1000_get_link_up_info_vf;
128	/* multicast address update */
129	mac->ops.update_mc_addr_list = e1000_update_mc_addr_list_vf;
130	/* set mac address */
131	mac->ops.rar_set = e1000_rar_set_vf;
132	/* read mac address */
133	mac->ops.read_mac_addr = e1000_read_mac_addr_vf;
134
135
136	return E1000_SUCCESS;
137}
138
139/**
140 *  e1000_init_function_pointers_vf - Inits function pointers
141 *  @hw: pointer to the HW structure
142 **/
143void e1000_init_function_pointers_vf(struct e1000_hw *hw)
144{
145	DEBUGFUNC("e1000_init_function_pointers_vf");
146
147	hw->mac.ops.init_params = e1000_init_mac_params_vf;
148	hw->nvm.ops.init_params = e1000_init_nvm_params_vf;
149	hw->phy.ops.init_params = e1000_init_phy_params_vf;
150	hw->mbx.ops.init_params = e1000_init_mbx_params_vf;
151}
152
153/**
154 *  e1000_acquire_vf - Acquire rights to access PHY or NVM.
155 *  @hw: pointer to the HW structure
156 *
157 *  There is no PHY or NVM so we want all attempts to acquire these to fail.
158 *  In addition, the MAC registers to access PHY/NVM don't exist so we don't
159 *  even want any SW to attempt to use them.
160 **/
161STATIC s32 e1000_acquire_vf(struct e1000_hw E1000_UNUSEDARG *hw)
162{
163	UNREFERENCED_1PARAMETER(hw);
164	return -E1000_ERR_PHY;
165}
166
167/**
168 *  e1000_release_vf - Release PHY or NVM
169 *  @hw: pointer to the HW structure
170 *
171 *  There is no PHY or NVM so we want all attempts to acquire these to fail.
172 *  In addition, the MAC registers to access PHY/NVM don't exist so we don't
173 *  even want any SW to attempt to use them.
174 **/
175STATIC void e1000_release_vf(struct e1000_hw E1000_UNUSEDARG *hw)
176{
177	UNREFERENCED_1PARAMETER(hw);
178	return;
179}
180
181/**
182 *  e1000_setup_link_vf - Sets up link.
183 *  @hw: pointer to the HW structure
184 *
185 *  Virtual functions cannot change link.
186 **/
187STATIC s32 e1000_setup_link_vf(struct e1000_hw E1000_UNUSEDARG *hw)
188{
189	DEBUGFUNC("e1000_setup_link_vf");
190	UNREFERENCED_1PARAMETER(hw);
191
192	return E1000_SUCCESS;
193}
194
195/**
196 *  e1000_get_bus_info_pcie_vf - Gets the bus info.
197 *  @hw: pointer to the HW structure
198 *
199 *  Virtual functions are not really on their own bus.
200 **/
201STATIC s32 e1000_get_bus_info_pcie_vf(struct e1000_hw *hw)
202{
203	struct e1000_bus_info *bus = &hw->bus;
204
205	DEBUGFUNC("e1000_get_bus_info_pcie_vf");
206
207	/* Do not set type PCI-E because we don't want disable master to run */
208	bus->type = e1000_bus_type_reserved;
209	bus->speed = e1000_bus_speed_2500;
210
211	return 0;
212}
213
214/**
215 *  e1000_get_link_up_info_vf - Gets link info.
216 *  @hw: pointer to the HW structure
217 *  @speed: pointer to 16 bit value to store link speed.
218 *  @duplex: pointer to 16 bit value to store duplex.
219 *
220 *  Since we cannot read the PHY and get accurate link info, we must rely upon
221 *  the status register's data which is often stale and inaccurate.
222 **/
223STATIC s32 e1000_get_link_up_info_vf(struct e1000_hw *hw, u16 *speed,
224				     u16 *duplex)
225{
226	s32 status;
227
228	DEBUGFUNC("e1000_get_link_up_info_vf");
229
230	status = E1000_READ_REG(hw, E1000_STATUS);
231	if (status & E1000_STATUS_SPEED_1000) {
232		*speed = SPEED_1000;
233		DEBUGOUT("1000 Mbs, ");
234	} else if (status & E1000_STATUS_SPEED_100) {
235		*speed = SPEED_100;
236		DEBUGOUT("100 Mbs, ");
237	} else {
238		*speed = SPEED_10;
239		DEBUGOUT("10 Mbs, ");
240	}
241
242	if (status & E1000_STATUS_FD) {
243		*duplex = FULL_DUPLEX;
244		DEBUGOUT("Full Duplex\n");
245	} else {
246		*duplex = HALF_DUPLEX;
247		DEBUGOUT("Half Duplex\n");
248	}
249
250	return E1000_SUCCESS;
251}
252
253/**
254 *  e1000_reset_hw_vf - Resets the HW
255 *  @hw: pointer to the HW structure
256 *
257 *  VF's provide a function level reset. This is done using bit 26 of ctrl_reg.
258 *  This is all the reset we can perform on a VF.
259 **/
260STATIC s32 e1000_reset_hw_vf(struct e1000_hw *hw)
261{
262	struct e1000_mbx_info *mbx = &hw->mbx;
263	u32 timeout = E1000_VF_INIT_TIMEOUT;
264	s32 ret_val = -E1000_ERR_MAC_INIT;
265	u32 ctrl, msgbuf[3];
266	u8 *addr = (u8 *)(&msgbuf[1]);
267
268	DEBUGFUNC("e1000_reset_hw_vf");
269
270	DEBUGOUT("Issuing a function level reset to MAC\n");
271	ctrl = E1000_READ_REG(hw, E1000_CTRL);
272	E1000_WRITE_REG(hw, E1000_CTRL, ctrl | E1000_CTRL_RST);
273
274	/* we cannot reset while the RSTI / RSTD bits are asserted */
275	while (!mbx->ops.check_for_rst(hw, 0) && timeout) {
276		timeout--;
277		usec_delay(5);
278	}
279
280	if (timeout) {
281		/* mailbox timeout can now become active */
282		mbx->timeout = E1000_VF_MBX_INIT_TIMEOUT;
283
284		msgbuf[0] = E1000_VF_RESET;
285		mbx->ops.write_posted(hw, msgbuf, 1, 0);
286
287		msec_delay(10);
288
289		/* set our "perm_addr" based on info provided by PF */
290		ret_val = mbx->ops.read_posted(hw, msgbuf, 3, 0);
291		if (!ret_val) {
292			if (msgbuf[0] == (E1000_VF_RESET |
293			    E1000_VT_MSGTYPE_ACK))
294				memcpy(hw->mac.perm_addr, addr, 6);
295			else
296				ret_val = -E1000_ERR_MAC_INIT;
297		}
298	}
299
300	return ret_val;
301}
302
303/**
304 *  e1000_init_hw_vf - Inits the HW
305 *  @hw: pointer to the HW structure
306 *
307 *  Not much to do here except clear the PF Reset indication if there is one.
308 **/
309STATIC s32 e1000_init_hw_vf(struct e1000_hw *hw)
310{
311	DEBUGFUNC("e1000_init_hw_vf");
312
313	/* attempt to set and restore our mac address */
314	e1000_rar_set_vf(hw, hw->mac.addr, 0);
315
316	return E1000_SUCCESS;
317}
318
319/**
320 *  e1000_rar_set_vf - set device MAC address
321 *  @hw: pointer to the HW structure
322 *  @addr: pointer to the receive address
323 *  @index receive address array register
324 **/
325STATIC int e1000_rar_set_vf(struct e1000_hw *hw, u8 *addr,
326			     u32 E1000_UNUSEDARG index)
327{
328	struct e1000_mbx_info *mbx = &hw->mbx;
329	u32 msgbuf[3];
330	u8 *msg_addr = (u8 *)(&msgbuf[1]);
331	s32 ret_val;
332
333	UNREFERENCED_1PARAMETER(index);
334	memset(msgbuf, 0, 12);
335	msgbuf[0] = E1000_VF_SET_MAC_ADDR;
336	memcpy(msg_addr, addr, 6);
337	ret_val = mbx->ops.write_posted(hw, msgbuf, 3, 0);
338
339	if (!ret_val)
340		ret_val = mbx->ops.read_posted(hw, msgbuf, 3, 0);
341
342	msgbuf[0] &= ~E1000_VT_MSGTYPE_CTS;
343
344	/* if nacked the address was rejected, use "perm_addr" */
345	if (!ret_val &&
346	    (msgbuf[0] == (E1000_VF_SET_MAC_ADDR | E1000_VT_MSGTYPE_NACK)))
347		e1000_read_mac_addr_vf(hw);
348
349	return E1000_SUCCESS;
350}
351
352/**
353 *  e1000_hash_mc_addr_vf - Generate a multicast hash value
354 *  @hw: pointer to the HW structure
355 *  @mc_addr: pointer to a multicast address
356 *
357 *  Generates a multicast address hash value which is used to determine
358 *  the multicast filter table array address and new table value.
359 **/
360STATIC u32 e1000_hash_mc_addr_vf(struct e1000_hw *hw, u8 *mc_addr)
361{
362	u32 hash_value, hash_mask;
363	u8 bit_shift = 0;
364
365	DEBUGFUNC("e1000_hash_mc_addr_generic");
366
367	/* Register count multiplied by bits per register */
368	hash_mask = (hw->mac.mta_reg_count * 32) - 1;
369
370	/*
371	 * The bit_shift is the number of left-shifts
372	 * where 0xFF would still fall within the hash mask.
373	 */
374	while (hash_mask >> bit_shift != 0xFF)
375		bit_shift++;
376
377	hash_value = hash_mask & (((mc_addr[4] >> (8 - bit_shift)) |
378				  (((u16) mc_addr[5]) << bit_shift)));
379
380	return hash_value;
381}
382
383STATIC void e1000_write_msg_read_ack(struct e1000_hw *hw,
384				     u32 *msg, u16 size)
385{
386	struct e1000_mbx_info *mbx = &hw->mbx;
387	u32 retmsg[E1000_VFMAILBOX_SIZE];
388	s32 retval = mbx->ops.write_posted(hw, msg, size, 0);
389
390	if (!retval)
391		mbx->ops.read_posted(hw, retmsg, E1000_VFMAILBOX_SIZE, 0);
392}
393
394/**
395 *  e1000_update_mc_addr_list_vf - Update Multicast addresses
396 *  @hw: pointer to the HW structure
397 *  @mc_addr_list: array of multicast addresses to program
398 *  @mc_addr_count: number of multicast addresses to program
399 *
400 *  Updates the Multicast Table Array.
401 *  The caller must have a packed mc_addr_list of multicast addresses.
402 **/
403void e1000_update_mc_addr_list_vf(struct e1000_hw *hw,
404				  u8 *mc_addr_list, u32 mc_addr_count)
405{
406	u32 msgbuf[E1000_VFMAILBOX_SIZE];
407	u16 *hash_list = (u16 *)&msgbuf[1];
408	u32 hash_value;
409	u32 i;
410
411	DEBUGFUNC("e1000_update_mc_addr_list_vf");
412
413	/* Each entry in the list uses 1 16 bit word.  We have 30
414	 * 16 bit words available in our HW msg buffer (minus 1 for the
415	 * msg type).  That's 30 hash values if we pack 'em right.  If
416	 * there are more than 30 MC addresses to add then punt the
417	 * extras for now and then add code to handle more than 30 later.
418	 * It would be unusual for a server to request that many multi-cast
419	 * addresses except for in large enterprise network environments.
420	 */
421
422	DEBUGOUT1("MC Addr Count = %d\n", mc_addr_count);
423
424	if (mc_addr_count > 30) {
425		msgbuf[0] |= E1000_VF_SET_MULTICAST_OVERFLOW;
426		mc_addr_count = 30;
427	}
428
429	msgbuf[0] = E1000_VF_SET_MULTICAST;
430	msgbuf[0] |= mc_addr_count << E1000_VT_MSGINFO_SHIFT;
431
432	for (i = 0; i < mc_addr_count; i++) {
433		hash_value = e1000_hash_mc_addr_vf(hw, mc_addr_list);
434		DEBUGOUT1("Hash value = 0x%03X\n", hash_value);
435		hash_list[i] = hash_value & 0x0FFF;
436		mc_addr_list += ETH_ADDR_LEN;
437	}
438
439	e1000_write_msg_read_ack(hw, msgbuf, E1000_VFMAILBOX_SIZE);
440}
441
442/**
443 *  e1000_vfta_set_vf - Set/Unset vlan filter table address
444 *  @hw: pointer to the HW structure
445 *  @vid: determines the vfta register and bit to set/unset
446 *  @set: if true then set bit, else clear bit
447 **/
448void e1000_vfta_set_vf(struct e1000_hw *hw, u16 vid, bool set)
449{
450	u32 msgbuf[2];
451
452	msgbuf[0] = E1000_VF_SET_VLAN;
453	msgbuf[1] = vid;
454	/* Setting the 8 bit field MSG INFO to TRUE indicates "add" */
455	if (set)
456		msgbuf[0] |= E1000_VF_SET_VLAN_ADD;
457
458	e1000_write_msg_read_ack(hw, msgbuf, 2);
459}
460
461/** e1000_rlpml_set_vf - Set the maximum receive packet length
462 *  @hw: pointer to the HW structure
463 *  @max_size: value to assign to max frame size
464 **/
465void e1000_rlpml_set_vf(struct e1000_hw *hw, u16 max_size)
466{
467	u32 msgbuf[2];
468
469	msgbuf[0] = E1000_VF_SET_LPE;
470	msgbuf[1] = max_size;
471
472	e1000_write_msg_read_ack(hw, msgbuf, 2);
473}
474
475/**
476 *  e1000_promisc_set_vf - Set flags for Unicast or Multicast promisc
477 *  @hw: pointer to the HW structure
478 *  @uni: boolean indicating unicast promisc status
479 *  @multi: boolean indicating multicast promisc status
480 **/
481s32 e1000_promisc_set_vf(struct e1000_hw *hw, enum e1000_promisc_type type)
482{
483	struct e1000_mbx_info *mbx = &hw->mbx;
484	u32 msgbuf = E1000_VF_SET_PROMISC;
485	s32 ret_val;
486
487	switch (type) {
488	case e1000_promisc_multicast:
489		msgbuf |= E1000_VF_SET_PROMISC_MULTICAST;
490		break;
491	case e1000_promisc_enabled:
492		msgbuf |= E1000_VF_SET_PROMISC_MULTICAST;
493	case e1000_promisc_unicast:
494		msgbuf |= E1000_VF_SET_PROMISC_UNICAST;
495	case e1000_promisc_disabled:
496		break;
497	default:
498		return -E1000_ERR_MAC_INIT;
499	}
500
501	 ret_val = mbx->ops.write_posted(hw, &msgbuf, 1, 0);
502
503	if (!ret_val)
504		ret_val = mbx->ops.read_posted(hw, &msgbuf, 1, 0);
505
506	if (!ret_val && !(msgbuf & E1000_VT_MSGTYPE_ACK))
507		ret_val = -E1000_ERR_MAC_INIT;
508
509	return ret_val;
510}
511
512/**
513 *  e1000_read_mac_addr_vf - Read device MAC address
514 *  @hw: pointer to the HW structure
515 **/
516STATIC s32 e1000_read_mac_addr_vf(struct e1000_hw *hw)
517{
518	int i;
519
520	for (i = 0; i < ETH_ADDR_LEN; i++)
521		hw->mac.addr[i] = hw->mac.perm_addr[i];
522
523	return E1000_SUCCESS;
524}
525
526/**
527 *  e1000_check_for_link_vf - Check for link for a virtual interface
528 *  @hw: pointer to the HW structure
529 *
530 *  Checks to see if the underlying PF is still talking to the VF and
531 *  if it is then it reports the link state to the hardware, otherwise
532 *  it reports link down and returns an error.
533 **/
534STATIC s32 e1000_check_for_link_vf(struct e1000_hw *hw)
535{
536	struct e1000_mbx_info *mbx = &hw->mbx;
537	struct e1000_mac_info *mac = &hw->mac;
538	s32 ret_val = E1000_SUCCESS;
539	u32 in_msg = 0;
540
541	DEBUGFUNC("e1000_check_for_link_vf");
542
543	/*
544	 * We only want to run this if there has been a rst asserted.
545	 * in this case that could mean a link change, device reset,
546	 * or a virtual function reset
547	 */
548
549	/* If we were hit with a reset or timeout drop the link */
550	if (!mbx->ops.check_for_rst(hw, 0) || !mbx->timeout)
551		mac->get_link_status = true;
552
553	if (!mac->get_link_status)
554		goto out;
555
556	/* if link status is down no point in checking to see if pf is up */
557	if (!(E1000_READ_REG(hw, E1000_STATUS) & E1000_STATUS_LU))
558		goto out;
559
560	/* if the read failed it could just be a mailbox collision, best wait
561	 * until we are called again and don't report an error */
562	if (mbx->ops.read(hw, &in_msg, 1, 0))
563		goto out;
564
565	/* if incoming message isn't clear to send we are waiting on response */
566	if (!(in_msg & E1000_VT_MSGTYPE_CTS)) {
567		/* message is not CTS and is NACK we have lost CTS status */
568		if (in_msg & E1000_VT_MSGTYPE_NACK)
569			ret_val = -E1000_ERR_MAC_INIT;
570		goto out;
571	}
572
573	/* at this point we know the PF is talking to us, check and see if
574	 * we are still accepting timeout or if we had a timeout failure.
575	 * if we failed then we will need to reinit */
576	if (!mbx->timeout) {
577		ret_val = -E1000_ERR_MAC_INIT;
578		goto out;
579	}
580
581	/* if we passed all the tests above then the link is up and we no
582	 * longer need to check for link */
583	mac->get_link_status = false;
584
585out:
586	return ret_val;
587}
588
589