ig: Factor out functions for flow control
[dragonfly.git] / sys / dev / netif / ig_hal / e1000_osdep.c
1 /******************************************************************************
2
3   Copyright (c) 2001-2014, Intel Corporation 
4   All rights reserved.
5   
6   Redistribution and use in source and binary forms, with or without 
7   modification, 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   
20   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   POSSIBILITY OF SUCH DAMAGE.
31
32 ******************************************************************************/
33
34 #include <sys/param.h>
35 #include <sys/sysctl.h>
36 #include <net/if_var.h>
37
38 #include "e1000_api.h"
39 #include "e1000_dragonfly.h"
40
41 /*
42  * NOTE: the following routines using the e1000 
43  *      naming style are provided to the shared
44  *      code but are OS specific
45  */
46
47 void
48 e1000_write_pci_cfg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
49 {
50         pci_write_config(((struct e1000_osdep *)hw->back)->dev, reg, *value, 2);
51 }
52
53 void
54 e1000_read_pci_cfg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
55 {
56         *value = pci_read_config(((struct e1000_osdep *)hw->back)->dev, reg, 2);
57 }
58
59 void
60 e1000_pci_set_mwi(struct e1000_hw *hw)
61 {
62         pci_write_config(((struct e1000_osdep *)hw->back)->dev, PCIR_COMMAND,
63             (hw->bus.pci_cmd_word | CMD_MEM_WRT_INVALIDATE), 2);
64 }
65
66 void
67 e1000_pci_clear_mwi(struct e1000_hw *hw)
68 {
69         pci_write_config(((struct e1000_osdep *)hw->back)->dev, PCIR_COMMAND,
70             (hw->bus.pci_cmd_word & ~CMD_MEM_WRT_INVALIDATE), 2);
71 }
72
73 /*
74  * Read the PCI Express capabilities
75  */
76 int32_t
77 e1000_read_pcie_cap_reg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
78 {
79         device_t dev = ((struct e1000_osdep *)hw->back)->dev;
80         uint8_t pcie_ptr;
81
82         pcie_ptr = pci_get_pciecap_ptr(dev);
83         if (pcie_ptr == 0)
84                 return E1000_NOT_IMPLEMENTED;
85
86         *value = pci_read_config(dev, pcie_ptr + reg, 2);
87         return E1000_SUCCESS;
88 }
89
90 int32_t
91 e1000_write_pcie_cap_reg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
92 {
93         device_t dev = ((struct e1000_osdep *)hw->back)->dev;
94         uint8_t pcie_ptr;
95
96         pcie_ptr = pci_get_pciecap_ptr(dev);
97         if (pcie_ptr == 0)
98                 return E1000_NOT_IMPLEMENTED;
99
100         pci_write_config(dev, pcie_ptr + reg, *value, 2);
101         return E1000_SUCCESS;
102 }
103
104 enum e1000_fc_mode
105 e1000_str2fc(const char *str)
106 {
107         if (strcmp(str, E1000_FC_STR_NONE) == 0)
108                 return e1000_fc_none;
109         else if (strcmp(str, E1000_FC_STR_RX_PAUSE) == 0)
110                 return e1000_fc_rx_pause;
111         else if (strcmp(str, E1000_FC_STR_TX_PAUSE) == 0)
112                 return e1000_fc_tx_pause;
113         else
114                 return e1000_fc_full;
115 }
116
117 void
118 e1000_fc2str(enum e1000_fc_mode fc, char *str, int len)
119 {
120         const char *fc_str = E1000_FC_STR_FULL;
121
122         switch (fc) {
123         case e1000_fc_none:
124                 fc_str = E1000_FC_STR_NONE;
125                 break;
126
127         case e1000_fc_rx_pause:
128                 fc_str = E1000_FC_STR_RX_PAUSE;
129                 break;
130
131         case e1000_fc_tx_pause:
132                 fc_str = E1000_FC_STR_TX_PAUSE;
133                 break;
134
135         default:
136                 break;
137         }
138         strlcpy(str, fc_str, len);
139 }
140
141 int
142 e1000_sysctl_flowctrl(struct ifnet *ifp, enum e1000_fc_mode *fc0,
143     struct e1000_hw *hw, struct sysctl_oid *oidp, struct sysctl_req *req)
144 {
145         char flowctrl[E1000_FC_STRLEN];
146         enum e1000_fc_mode fc;
147         int error;
148
149         e1000_fc2str(*fc0, flowctrl, sizeof(flowctrl));
150         error = sysctl_handle_string(oidp, flowctrl, sizeof(flowctrl), req);
151         if (error != 0 || req->newptr == NULL)
152                 return error;
153
154         fc = e1000_str2fc(flowctrl);
155
156         ifnet_serialize_all(ifp);
157         if (fc == *fc0)
158                 goto done;
159
160         *fc0 = fc;
161         hw->fc.requested_mode = fc;
162         hw->fc.current_mode = fc;
163         e1000_force_mac_fc(hw);
164 done:
165         ifnet_deserialize_all(ifp);
166
167         return 0;
168 }
169
170 /* Module glue */
171 static moduledata_t ig_hal_mod = { "ig_hal" };
172 DECLARE_MODULE(ig_hal, ig_hal_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
173 MODULE_VERSION(ig_hal, 1);