1/* ----------------------------------------------------------------------- *
2 *
3 *   Copyright 2008 H. Peter Anvin - All Rights Reserved
4 *   Copyright 2009 Intel Corporation; author: H. Peter Anvin
5 *
6 *   This program is free software; you can redistribute it and/or modify
7 *   it under the terms of the GNU General Public License as published by
8 *   the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
9 *   Boston MA 02110-1301, USA; either version 2 of the License, or
10 *   (at your option) any later version; incorporated herein by reference.
11 *
12 * ----------------------------------------------------------------------- */
13
14#include <stdio.h>
15#include <string.h>
16#include <core.h>
17#include <fs.h>
18#include <minmax.h>
19#include <sys/cpu.h>
20#include "pxe.h"
21
22static int pxe_idle_poll(void)
23{
24    static __lowmem char junk_pkt[PKTBUF_SIZE];
25    static __lowmem t_PXENV_UDP_READ read_buf;
26
27    memset(&read_buf, 0, sizeof read_buf);
28
29    read_buf.src_ip  = 0;	 /* Any destination */
30    read_buf.dest_ip = IPInfo.myip;
31    read_buf.s_port  = 0;	 /* Any source port */
32    read_buf.d_port  = htons(9); /* Discard port (not used...) */
33    read_buf.buffer_size = sizeof junk_pkt;
34    read_buf.buffer  = FAR_PTR(junk_pkt);
35
36    pxe_call(PXENV_UDP_READ, &read_buf);
37
38    return 0;
39}
40
41static uint32_t pxe_detect_nic_type(void)
42{
43    static __lowmem t_PXENV_UNDI_GET_NIC_TYPE nic_type;
44
45    if (pxe_call(PXENV_UNDI_GET_NIC_TYPE, &nic_type))
46	return -1;		/* Unknown NIC */
47
48    if (nic_type.NicType != PCI_NIC && nic_type.NicType != CardBus_NIC)
49	return -1;		/* Not a PCI NIC */
50
51    /*
52     * Return VID:DID as a single number, with the VID in the high word
53     * -- this is opposite from the usual order, but it makes it easier to
54     * enforce that the table is sorted.
55     */
56    return (nic_type.info.pci.Vendor_ID << 16) + nic_type.info.pci.Dev_ID;
57}
58
59#define PCI_DEV(vid, did)	(((vid) << 16) + (did))
60
61/* This array should be sorted!! */
62static const uint32_t pxe_need_idle_drain[] =
63{
64    /*
65     * Older Broadcom NICs: they need receive calls on idle to avoid
66     * FIFO stalls.
67     */
68    PCI_DEV(0x14e4, 0x1659),	/* BCM5721 */
69    PCI_DEV(0x14e4, 0x165a),	/* BCM5722 */
70    PCI_DEV(0x14e4, 0x165b),	/* BCM5723 */
71    PCI_DEV(0x14e4, 0x1668),	/* BCM5714 */
72    PCI_DEV(0x14e4, 0x1669),	/* BCM5714S */
73    PCI_DEV(0x14e4, 0x166a),	/* BCM5780 */
74    PCI_DEV(0x14e4, 0x1673),	/* BCM5755M */
75    PCI_DEV(0x14e4, 0x1674),	/* BCM5756ME */
76    PCI_DEV(0x14e4, 0x1678),	/* BCM5715 */
77    PCI_DEV(0x14e4, 0x1679),	/* BCM5715S */
78    PCI_DEV(0x14e4, 0x167b),	/* BCM5755 */
79};
80
81void pxe_idle_init(void)
82{
83    uint32_t dev_id = pxe_detect_nic_type();
84    int l, h;
85    bool found;
86
87    l = 0;
88    h = sizeof pxe_need_idle_drain / sizeof pxe_need_idle_drain[0] - 1;
89
90    found = false;
91    while (h >= l) {
92	int x = (l+h) >> 1;
93	uint32_t id = pxe_need_idle_drain[x];
94
95	if (id == dev_id) {
96	    found = true;
97	    break;
98	} else if (id < dev_id) {
99	    l = x+1;
100	} else {
101	    h = x-1;
102	}
103    }
104
105    if (found)
106	idle_hook_func = pxe_idle_poll;
107}
108
109void pxe_idle_cleanup(void)
110{
111    idle_hook_func = NULL;
112}
113