[b7a6d23a] | 1 | /************************************************************************** |
---|
| 2 | |
---|
| 3 | Copyright (c) 2001-2005, 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 | /*$FreeBSD: /repoman/r/ncvs/src/sys/dev/em/if_em.c,v 1.67 2005/08/03 00:18:29 rwatson Exp $*/ |
---|
| 35 | #ifndef __rtems__ |
---|
| 36 | #include <dev/em/if_em.h> |
---|
| 37 | #else |
---|
| 38 | #include <rtems.h> |
---|
| 39 | #include <rtemscompat.h> |
---|
| 40 | #include <if_em.h> |
---|
| 41 | #include <rtemscompat1.h> |
---|
| 42 | #endif |
---|
| 43 | |
---|
| 44 | /********************************************************************* |
---|
| 45 | * Set this to one to display debug statistics |
---|
| 46 | *********************************************************************/ |
---|
| 47 | int em_display_debug_stats = 0; |
---|
| 48 | |
---|
| 49 | /********************************************************************* |
---|
| 50 | * Linked list of board private structures for all NICs found |
---|
| 51 | *********************************************************************/ |
---|
| 52 | |
---|
| 53 | struct adapter *em_adapter_list = NULL; |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | /********************************************************************* |
---|
| 57 | * Driver version |
---|
| 58 | *********************************************************************/ |
---|
| 59 | |
---|
| 60 | char em_driver_version[] = "2.1.7"; |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | /********************************************************************* |
---|
| 64 | * PCI Device ID Table |
---|
| 65 | * |
---|
| 66 | * Used by probe to select devices to load on |
---|
| 67 | * Last field stores an index into em_strings |
---|
| 68 | * Last entry must be all 0s |
---|
| 69 | * |
---|
| 70 | * { Vendor ID, Device ID, SubVendor ID, SubDevice ID, String Index } |
---|
| 71 | *********************************************************************/ |
---|
| 72 | |
---|
| 73 | static em_vendor_info_t em_vendor_info_array[] = |
---|
| 74 | { |
---|
| 75 | /* Intel(R) PRO/1000 Network Connection */ |
---|
| 76 | { 0x8086, E1000_DEV_ID_82540EM, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 77 | { 0x8086, E1000_DEV_ID_82540EM_LOM, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 78 | { 0x8086, E1000_DEV_ID_82540EP, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 79 | { 0x8086, E1000_DEV_ID_82540EP_LOM, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 80 | { 0x8086, E1000_DEV_ID_82540EP_LP, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 81 | |
---|
| 82 | { 0x8086, E1000_DEV_ID_82541EI, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 83 | { 0x8086, E1000_DEV_ID_82541ER, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 84 | { 0x8086, E1000_DEV_ID_82541ER_LOM, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 85 | { 0x8086, E1000_DEV_ID_82541EI_MOBILE, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 86 | { 0x8086, E1000_DEV_ID_82541GI, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 87 | { 0x8086, E1000_DEV_ID_82541GI_LF, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 88 | { 0x8086, E1000_DEV_ID_82541GI_MOBILE, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 89 | |
---|
| 90 | { 0x8086, E1000_DEV_ID_82542, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 91 | |
---|
| 92 | { 0x8086, E1000_DEV_ID_82543GC_FIBER, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 93 | { 0x8086, E1000_DEV_ID_82543GC_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 94 | |
---|
| 95 | { 0x8086, E1000_DEV_ID_82544EI_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 96 | { 0x8086, E1000_DEV_ID_82544EI_FIBER, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 97 | { 0x8086, E1000_DEV_ID_82544GC_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 98 | { 0x8086, E1000_DEV_ID_82544GC_LOM, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 99 | |
---|
| 100 | { 0x8086, E1000_DEV_ID_82545EM_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 101 | { 0x8086, E1000_DEV_ID_82545EM_FIBER, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 102 | { 0x8086, E1000_DEV_ID_82545GM_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 103 | { 0x8086, E1000_DEV_ID_82545GM_FIBER, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 104 | { 0x8086, E1000_DEV_ID_82545GM_SERDES, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 105 | |
---|
| 106 | { 0x8086, E1000_DEV_ID_82546EB_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 107 | { 0x8086, E1000_DEV_ID_82546EB_FIBER, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 108 | { 0x8086, E1000_DEV_ID_82546EB_QUAD_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 109 | { 0x8086, E1000_DEV_ID_82546GB_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 110 | { 0x8086, E1000_DEV_ID_82546GB_FIBER, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 111 | { 0x8086, E1000_DEV_ID_82546GB_SERDES, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 112 | { 0x8086, E1000_DEV_ID_82546GB_PCIE, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 113 | { 0x8086, E1000_DEV_ID_82546GB_QUAD_COPPER, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 114 | |
---|
| 115 | { 0x8086, E1000_DEV_ID_82547EI, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 116 | { 0x8086, E1000_DEV_ID_82547EI_MOBILE, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 117 | { 0x8086, E1000_DEV_ID_82547GI, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 118 | |
---|
| 119 | { 0x8086, E1000_DEV_ID_82573E, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 120 | { 0x8086, E1000_DEV_ID_82573E_IAMT, PCI_ANY_ID, PCI_ANY_ID, 0}, |
---|
| 121 | |
---|
| 122 | /* required last entry */ |
---|
| 123 | { 0, 0, 0, 0, 0} |
---|
| 124 | }; |
---|
| 125 | |
---|
| 126 | /********************************************************************* |
---|
| 127 | * Table of branding strings for all supported NICs. |
---|
| 128 | *********************************************************************/ |
---|
| 129 | |
---|
| 130 | static char *em_strings[] = { |
---|
| 131 | "Intel(R) PRO/1000 Network Connection" |
---|
| 132 | }; |
---|
| 133 | |
---|
| 134 | /********************************************************************* |
---|
| 135 | * Function prototypes |
---|
| 136 | *********************************************************************/ |
---|
| 137 | static int em_probe(device_t); |
---|
| 138 | static int em_attach(device_t); |
---|
| 139 | #if !defined(__rtems__) || defined(DEBUG_MODULAR) |
---|
| 140 | static int em_detach(device_t); |
---|
| 141 | #endif |
---|
| 142 | #ifndef __rtems__ |
---|
| 143 | static int em_shutdown(device_t); |
---|
| 144 | #endif |
---|
| 145 | static void em_intr(void *); |
---|
| 146 | static void em_start(struct ifnet *); |
---|
| 147 | #ifndef __rtems__ |
---|
| 148 | static int em_ioctl(struct ifnet *, u_long, caddr_t); |
---|
| 149 | #else |
---|
| 150 | static int em_ioctl(struct ifnet *, ioctl_command_t, caddr_t); |
---|
| 151 | #endif |
---|
| 152 | static void em_watchdog(struct ifnet *); |
---|
| 153 | static void em_init(void *); |
---|
| 154 | static void em_init_locked(struct adapter *); |
---|
| 155 | static void em_stop(void *); |
---|
| 156 | static void em_media_status(struct ifnet *, struct ifmediareq *); |
---|
| 157 | #ifndef __rtems__ |
---|
| 158 | static int em_media_change(struct ifnet *); |
---|
| 159 | #else |
---|
| 160 | static int em_media_change(struct ifnet *ifp, struct rtems_ifmedia *ifm); |
---|
| 161 | #endif |
---|
| 162 | static void em_identify_hardware(struct adapter *); |
---|
| 163 | static int em_allocate_pci_resources(struct adapter *); |
---|
| 164 | #ifndef __rtems__ |
---|
| 165 | static void em_free_pci_resources(struct adapter *); |
---|
| 166 | static void em_local_timer(void *); |
---|
| 167 | #endif |
---|
| 168 | static int em_hardware_init(struct adapter *); |
---|
| 169 | static void em_setup_interface(device_t, struct adapter *); |
---|
| 170 | static int em_setup_transmit_structures(struct adapter *); |
---|
| 171 | static void em_initialize_transmit_unit(struct adapter *); |
---|
| 172 | static int em_setup_receive_structures(struct adapter *); |
---|
| 173 | static void em_initialize_receive_unit(struct adapter *); |
---|
| 174 | static void em_enable_intr(struct adapter *); |
---|
| 175 | static void em_disable_intr(struct adapter *); |
---|
| 176 | static void em_free_transmit_structures(struct adapter *); |
---|
| 177 | static void em_free_receive_structures(struct adapter *); |
---|
| 178 | static void em_update_stats_counters(struct adapter *); |
---|
| 179 | static void em_clean_transmit_interrupts(struct adapter *); |
---|
| 180 | static int em_allocate_receive_structures(struct adapter *); |
---|
| 181 | static int em_allocate_transmit_structures(struct adapter *); |
---|
| 182 | static void em_process_receive_interrupts(struct adapter *, int); |
---|
| 183 | #ifndef __rtems__ |
---|
| 184 | static void em_receive_checksum(struct adapter *, |
---|
| 185 | struct em_rx_desc *, |
---|
| 186 | struct mbuf *); |
---|
| 187 | static void em_transmit_checksum_setup(struct adapter *, |
---|
| 188 | struct mbuf *, |
---|
| 189 | u_int32_t *, |
---|
| 190 | u_int32_t *); |
---|
| 191 | #endif |
---|
| 192 | static void em_set_promisc(struct adapter *); |
---|
| 193 | static void em_disable_promisc(struct adapter *); |
---|
| 194 | static void em_set_multi(struct adapter *); |
---|
| 195 | static void em_print_hw_stats(struct adapter *); |
---|
| 196 | static void em_print_link_status(struct adapter *); |
---|
| 197 | static int em_get_buf(int i, struct adapter *, |
---|
| 198 | struct mbuf *); |
---|
| 199 | #ifndef __rtems__ |
---|
| 200 | static void em_enable_vlans(struct adapter *); |
---|
| 201 | static void em_disable_vlans(struct adapter *); |
---|
| 202 | #endif |
---|
| 203 | static int em_encap(struct adapter *, struct mbuf **); |
---|
| 204 | #ifndef __rtems__ |
---|
| 205 | static void em_smartspeed(struct adapter *); |
---|
| 206 | #endif |
---|
| 207 | static int em_82547_fifo_workaround(struct adapter *, int); |
---|
| 208 | static void em_82547_update_fifo_head(struct adapter *, int); |
---|
| 209 | static int em_82547_tx_fifo_reset(struct adapter *); |
---|
| 210 | #ifndef __rtems__ |
---|
| 211 | static void em_82547_move_tail(void *arg); |
---|
| 212 | #endif |
---|
| 213 | static void em_82547_move_tail_locked(struct adapter *); |
---|
| 214 | static int em_dma_malloc(struct adapter *, bus_size_t, |
---|
| 215 | struct em_dma_alloc *, int); |
---|
| 216 | static void em_dma_free(struct adapter *, struct em_dma_alloc *); |
---|
| 217 | #ifndef __rtems__ |
---|
| 218 | static void em_print_debug_info(struct adapter *); |
---|
| 219 | #endif |
---|
| 220 | static int em_is_valid_ether_addr(u_int8_t *); |
---|
| 221 | #ifndef __rtems__ |
---|
| 222 | static int em_sysctl_stats(SYSCTL_HANDLER_ARGS); |
---|
| 223 | static int em_sysctl_debug_info(SYSCTL_HANDLER_ARGS); |
---|
| 224 | #endif |
---|
| 225 | static u_int32_t em_fill_descriptors (u_int64_t address, |
---|
| 226 | u_int32_t length, |
---|
| 227 | PDESC_ARRAY desc_array); |
---|
| 228 | #ifndef __rtems__ |
---|
| 229 | static int em_sysctl_int_delay(SYSCTL_HANDLER_ARGS); |
---|
| 230 | static void em_add_int_delay_sysctl(struct adapter *, const char *, |
---|
| 231 | const char *, struct em_int_delay_info *, |
---|
| 232 | int, int); |
---|
| 233 | #endif |
---|
| 234 | |
---|
| 235 | /********************************************************************* |
---|
| 236 | * FreeBSD Device Interface Entry Points |
---|
| 237 | *********************************************************************/ |
---|
| 238 | |
---|
| 239 | #ifndef __rtems__ |
---|
| 240 | static device_method_t em_methods[] = { |
---|
| 241 | /* Device interface */ |
---|
| 242 | DEVMETHOD(device_probe, em_probe), |
---|
| 243 | DEVMETHOD(device_attach, em_attach), |
---|
| 244 | DEVMETHOD(device_detach, em_detach), |
---|
| 245 | DEVMETHOD(device_shutdown, em_shutdown), |
---|
| 246 | {0, 0} |
---|
| 247 | }; |
---|
| 248 | |
---|
| 249 | static driver_t em_driver = { |
---|
| 250 | "em", em_methods, sizeof(struct adapter ), |
---|
| 251 | }; |
---|
| 252 | |
---|
| 253 | static devclass_t em_devclass; |
---|
| 254 | DRIVER_MODULE(em, pci, em_driver, em_devclass, 0, 0); |
---|
| 255 | MODULE_DEPEND(em, pci, 1, 1, 1); |
---|
| 256 | MODULE_DEPEND(em, ether, 1, 1, 1); |
---|
| 257 | #else |
---|
| 258 | net_drv_tbl_t METHODS = { |
---|
| 259 | n_probe : em_probe, |
---|
| 260 | n_attach : em_attach, |
---|
| 261 | #ifdef DEBUG_MODULAR |
---|
| 262 | n_detach : em_detach, |
---|
| 263 | #else |
---|
| 264 | n_detach: 0, |
---|
| 265 | #endif |
---|
| 266 | n_intr : em_intr, |
---|
| 267 | }; |
---|
| 268 | #endif |
---|
| 269 | |
---|
| 270 | /********************************************************************* |
---|
| 271 | * Tunable default values. |
---|
| 272 | *********************************************************************/ |
---|
| 273 | |
---|
| 274 | #define E1000_TICKS_TO_USECS(ticks) ((1024 * (ticks) + 500) / 1000) |
---|
| 275 | #define E1000_USECS_TO_TICKS(usecs) ((1000 * (usecs) + 512) / 1024) |
---|
| 276 | |
---|
| 277 | #ifndef __rtems__ |
---|
| 278 | static int em_tx_int_delay_dflt = E1000_TICKS_TO_USECS(EM_TIDV); |
---|
| 279 | static int em_rx_int_delay_dflt = E1000_TICKS_TO_USECS(EM_RDTR); |
---|
| 280 | static int em_tx_abs_int_delay_dflt = E1000_TICKS_TO_USECS(EM_TADV); |
---|
| 281 | static int em_rx_abs_int_delay_dflt = E1000_TICKS_TO_USECS(EM_RADV); |
---|
| 282 | |
---|
| 283 | TUNABLE_INT("hw.em.tx_int_delay", &em_tx_int_delay_dflt); |
---|
| 284 | TUNABLE_INT("hw.em.rx_int_delay", &em_rx_int_delay_dflt); |
---|
| 285 | TUNABLE_INT("hw.em.tx_abs_int_delay", &em_tx_abs_int_delay_dflt); |
---|
| 286 | TUNABLE_INT("hw.em.rx_abs_int_delay", &em_rx_abs_int_delay_dflt); |
---|
| 287 | #endif |
---|
| 288 | |
---|
| 289 | /********************************************************************* |
---|
| 290 | * Device identification routine |
---|
| 291 | * |
---|
| 292 | * em_probe determines if the driver should be loaded on |
---|
| 293 | * adapter based on PCI vendor/device id of the adapter. |
---|
| 294 | * |
---|
| 295 | * return BUS_PROBE_DEFAULT on success, positive on failure |
---|
| 296 | *********************************************************************/ |
---|
| 297 | |
---|
| 298 | static int |
---|
| 299 | em_probe(device_t dev) |
---|
| 300 | { |
---|
| 301 | em_vendor_info_t *ent; |
---|
| 302 | |
---|
| 303 | u_int16_t pci_vendor_id = 0; |
---|
| 304 | u_int16_t pci_device_id = 0; |
---|
| 305 | u_int16_t pci_subvendor_id = 0; |
---|
| 306 | u_int16_t pci_subdevice_id = 0; |
---|
| 307 | char adapter_name[60]; |
---|
| 308 | |
---|
| 309 | INIT_DEBUGOUT("em_probe: begin"); |
---|
| 310 | |
---|
| 311 | pci_vendor_id = pci_get_vendor(dev); |
---|
| 312 | if (pci_vendor_id != EM_VENDOR_ID) |
---|
| 313 | return(ENXIO); |
---|
| 314 | |
---|
| 315 | pci_device_id = pci_get_device(dev); |
---|
| 316 | pci_subvendor_id = pci_get_subvendor(dev); |
---|
| 317 | pci_subdevice_id = pci_get_subdevice(dev); |
---|
| 318 | |
---|
| 319 | ent = em_vendor_info_array; |
---|
| 320 | while (ent->vendor_id != 0) { |
---|
| 321 | if ((pci_vendor_id == ent->vendor_id) && |
---|
| 322 | (pci_device_id == ent->device_id) && |
---|
| 323 | |
---|
| 324 | ((pci_subvendor_id == ent->subvendor_id) || |
---|
| 325 | (ent->subvendor_id == PCI_ANY_ID)) && |
---|
| 326 | |
---|
| 327 | ((pci_subdevice_id == ent->subdevice_id) || |
---|
| 328 | (ent->subdevice_id == PCI_ANY_ID))) { |
---|
| 329 | sprintf(adapter_name, "%s, Version - %s", |
---|
| 330 | em_strings[ent->index], |
---|
| 331 | em_driver_version); |
---|
| 332 | device_set_desc_copy(dev, adapter_name); |
---|
| 333 | return(BUS_PROBE_DEFAULT); |
---|
| 334 | } |
---|
| 335 | ent++; |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | return(ENXIO); |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | /********************************************************************* |
---|
| 342 | * Device initialization routine |
---|
| 343 | * |
---|
| 344 | * The attach entry point is called when the driver is being loaded. |
---|
| 345 | * This routine identifies the type of hardware, allocates all resources |
---|
| 346 | * and initializes the hardware. |
---|
| 347 | * |
---|
| 348 | * return 0 on success, positive on failure |
---|
| 349 | *********************************************************************/ |
---|
| 350 | |
---|
| 351 | static int |
---|
| 352 | em_attach(device_t dev) |
---|
| 353 | { |
---|
| 354 | struct adapter * adapter; |
---|
| 355 | int tsize, rsize; |
---|
| 356 | int error = 0; |
---|
| 357 | |
---|
| 358 | INIT_DEBUGOUT("em_attach: begin"); |
---|
| 359 | |
---|
| 360 | /* Allocate, clear, and link in our adapter structure */ |
---|
| 361 | if (!(adapter = device_get_softc(dev))) { |
---|
| 362 | printf("em: adapter structure allocation failed\n"); |
---|
| 363 | return(ENOMEM); |
---|
| 364 | } |
---|
| 365 | #ifndef __rtems__ |
---|
| 366 | bzero(adapter, sizeof(struct adapter )); |
---|
| 367 | #else |
---|
| 368 | /* softc structure is maintained outside of this |
---|
| 369 | * and the osdep already contains vital fields (memory address) |
---|
| 370 | */ |
---|
| 371 | #endif |
---|
| 372 | adapter->dev = dev; |
---|
| 373 | adapter->osdep.dev = dev; |
---|
| 374 | adapter->unit = device_get_unit(dev); |
---|
| 375 | EM_LOCK_INIT(adapter, device_get_nameunit(dev)); |
---|
| 376 | |
---|
| 377 | if (em_adapter_list != NULL) |
---|
| 378 | em_adapter_list->prev = adapter; |
---|
| 379 | adapter->next = em_adapter_list; |
---|
| 380 | em_adapter_list = adapter; |
---|
| 381 | |
---|
| 382 | #ifndef __rtems__ |
---|
| 383 | /* SYSCTL stuff */ |
---|
| 384 | SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), |
---|
| 385 | SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), |
---|
| 386 | OID_AUTO, "debug_info", CTLTYPE_INT|CTLFLAG_RW, |
---|
| 387 | (void *)adapter, 0, |
---|
| 388 | em_sysctl_debug_info, "I", "Debug Information"); |
---|
| 389 | |
---|
| 390 | SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), |
---|
| 391 | SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), |
---|
| 392 | OID_AUTO, "stats", CTLTYPE_INT|CTLFLAG_RW, |
---|
| 393 | (void *)adapter, 0, |
---|
| 394 | em_sysctl_stats, "I", "Statistics"); |
---|
| 395 | #endif |
---|
| 396 | |
---|
| 397 | callout_init(&adapter->timer, CALLOUT_MPSAFE); |
---|
| 398 | callout_init(&adapter->tx_fifo_timer, CALLOUT_MPSAFE); |
---|
| 399 | |
---|
| 400 | /* Determine hardware revision */ |
---|
| 401 | em_identify_hardware(adapter); |
---|
| 402 | |
---|
| 403 | #ifndef __rtems__ |
---|
| 404 | /* Set up some sysctls for the tunable interrupt delays */ |
---|
| 405 | em_add_int_delay_sysctl(adapter, "rx_int_delay", |
---|
| 406 | "receive interrupt delay in usecs", &adapter->rx_int_delay, |
---|
| 407 | E1000_REG_OFFSET(&adapter->hw, RDTR), em_rx_int_delay_dflt); |
---|
| 408 | em_add_int_delay_sysctl(adapter, "tx_int_delay", |
---|
| 409 | "transmit interrupt delay in usecs", &adapter->tx_int_delay, |
---|
| 410 | E1000_REG_OFFSET(&adapter->hw, TIDV), em_tx_int_delay_dflt); |
---|
| 411 | if (adapter->hw.mac_type >= em_82540) { |
---|
| 412 | em_add_int_delay_sysctl(adapter, "rx_abs_int_delay", |
---|
| 413 | "receive interrupt delay limit in usecs", |
---|
| 414 | &adapter->rx_abs_int_delay, |
---|
| 415 | E1000_REG_OFFSET(&adapter->hw, RADV), |
---|
| 416 | em_rx_abs_int_delay_dflt); |
---|
| 417 | em_add_int_delay_sysctl(adapter, "tx_abs_int_delay", |
---|
| 418 | "transmit interrupt delay limit in usecs", |
---|
| 419 | &adapter->tx_abs_int_delay, |
---|
| 420 | E1000_REG_OFFSET(&adapter->hw, TADV), |
---|
| 421 | em_tx_abs_int_delay_dflt); |
---|
| 422 | } |
---|
| 423 | #endif |
---|
| 424 | |
---|
| 425 | /* Parameters (to be read from user) */ |
---|
| 426 | adapter->num_tx_desc = EM_MAX_TXD; |
---|
| 427 | adapter->num_rx_desc = EM_MAX_RXD; |
---|
| 428 | #ifdef __rtems__ |
---|
| 429 | if ( dev->d_ifconfig->rbuf_count > 0 ) { |
---|
| 430 | adapter->num_rx_desc = dev->d_ifconfig->rbuf_count; |
---|
| 431 | } |
---|
| 432 | if ( adapter->num_rx_desc < 80 ) |
---|
| 433 | adapter->num_rx_desc = 80; |
---|
| 434 | if ( adapter->num_rx_desc > 256 ) |
---|
| 435 | adapter->num_rx_desc = 256; |
---|
| 436 | if ( dev->d_ifconfig->xbuf_count > 0 ) { |
---|
| 437 | adapter->num_tx_desc = dev->d_ifconfig->xbuf_count; |
---|
| 438 | } |
---|
| 439 | if ( adapter->num_tx_desc < 80 ) |
---|
| 440 | adapter->num_tx_desc = 80; |
---|
| 441 | if ( adapter->num_tx_desc > 256 ) |
---|
| 442 | adapter->num_tx_desc = 256; |
---|
| 443 | adapter->tx_cleanup_threshold = adapter->num_tx_desc/8; |
---|
| 444 | #endif |
---|
| 445 | adapter->hw.autoneg = DO_AUTO_NEG; |
---|
| 446 | adapter->hw.wait_autoneg_complete = WAIT_FOR_AUTO_NEG_DEFAULT; |
---|
| 447 | adapter->hw.autoneg_advertised = AUTONEG_ADV_DEFAULT; |
---|
| 448 | adapter->hw.tbi_compatibility_en = TRUE; |
---|
| 449 | adapter->rx_buffer_len = EM_RXBUFFER_2048; |
---|
| 450 | |
---|
| 451 | /* |
---|
| 452 | * These parameters control the automatic generation(Tx) and |
---|
| 453 | * response(Rx) to Ethernet PAUSE frames. |
---|
| 454 | */ |
---|
| 455 | adapter->hw.fc_high_water = FC_DEFAULT_HI_THRESH; |
---|
| 456 | adapter->hw.fc_low_water = FC_DEFAULT_LO_THRESH; |
---|
| 457 | adapter->hw.fc_pause_time = FC_DEFAULT_TX_TIMER; |
---|
| 458 | adapter->hw.fc_send_xon = TRUE; |
---|
| 459 | adapter->hw.fc = em_fc_full; |
---|
| 460 | |
---|
| 461 | adapter->hw.phy_init_script = 1; |
---|
| 462 | adapter->hw.phy_reset_disable = FALSE; |
---|
| 463 | |
---|
| 464 | #ifndef EM_MASTER_SLAVE |
---|
| 465 | adapter->hw.master_slave = em_ms_hw_default; |
---|
| 466 | #else |
---|
| 467 | adapter->hw.master_slave = EM_MASTER_SLAVE; |
---|
| 468 | #endif |
---|
| 469 | /* |
---|
| 470 | * Set the max frame size assuming standard ethernet |
---|
| 471 | * sized frames |
---|
| 472 | */ |
---|
| 473 | adapter->hw.max_frame_size = |
---|
| 474 | ETHERMTU + ETHER_HDR_LEN + ETHER_CRC_LEN; |
---|
| 475 | |
---|
| 476 | adapter->hw.min_frame_size = |
---|
| 477 | MINIMUM_ETHERNET_PACKET_SIZE + ETHER_CRC_LEN; |
---|
| 478 | |
---|
| 479 | /* |
---|
| 480 | * This controls when hardware reports transmit completion |
---|
| 481 | * status. |
---|
| 482 | */ |
---|
| 483 | adapter->hw.report_tx_early = 1; |
---|
| 484 | |
---|
| 485 | |
---|
| 486 | if (em_allocate_pci_resources(adapter)) { |
---|
| 487 | printf("em%d: Allocation of PCI resources failed\n", |
---|
| 488 | adapter->unit); |
---|
| 489 | error = ENXIO; |
---|
| 490 | goto err_pci; |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | |
---|
| 494 | /* Initialize eeprom parameters */ |
---|
| 495 | em_init_eeprom_params(&adapter->hw); |
---|
| 496 | |
---|
| 497 | tsize = EM_ROUNDUP(adapter->num_tx_desc * |
---|
| 498 | sizeof(struct em_tx_desc), 4096); |
---|
| 499 | |
---|
| 500 | /* Allocate Transmit Descriptor ring */ |
---|
| 501 | if (em_dma_malloc(adapter, tsize, &adapter->txdma, BUS_DMA_NOWAIT)) { |
---|
| 502 | printf("em%d: Unable to allocate tx_desc memory\n", |
---|
| 503 | adapter->unit); |
---|
| 504 | error = ENOMEM; |
---|
| 505 | goto err_tx_desc; |
---|
| 506 | } |
---|
| 507 | adapter->tx_desc_base = (struct em_tx_desc *) adapter->txdma.dma_vaddr; |
---|
| 508 | |
---|
| 509 | rsize = EM_ROUNDUP(adapter->num_rx_desc * |
---|
| 510 | sizeof(struct em_rx_desc), 4096); |
---|
| 511 | |
---|
| 512 | /* Allocate Receive Descriptor ring */ |
---|
| 513 | if (em_dma_malloc(adapter, rsize, &adapter->rxdma, BUS_DMA_NOWAIT)) { |
---|
| 514 | printf("em%d: Unable to allocate rx_desc memory\n", |
---|
| 515 | adapter->unit); |
---|
| 516 | error = ENOMEM; |
---|
| 517 | goto err_rx_desc; |
---|
| 518 | } |
---|
| 519 | adapter->rx_desc_base = (struct em_rx_desc *) adapter->rxdma.dma_vaddr; |
---|
| 520 | |
---|
| 521 | /* Initialize the hardware */ |
---|
| 522 | if (em_hardware_init(adapter)) { |
---|
| 523 | printf("em%d: Unable to initialize the hardware\n", |
---|
| 524 | adapter->unit); |
---|
| 525 | error = EIO; |
---|
| 526 | goto err_hw_init; |
---|
| 527 | } |
---|
| 528 | |
---|
| 529 | /* Copy the permanent MAC address out of the EEPROM */ |
---|
| 530 | if (em_read_mac_addr(&adapter->hw) < 0) { |
---|
| 531 | printf("em%d: EEPROM read error while reading mac address\n", |
---|
| 532 | adapter->unit); |
---|
| 533 | error = EIO; |
---|
| 534 | goto err_mac_addr; |
---|
| 535 | } |
---|
| 536 | |
---|
| 537 | #ifdef __rtems__ |
---|
| 538 | /* if the configuration has not set a mac address, copy the permanent |
---|
| 539 | * address from the device to the arpcom struct. |
---|
| 540 | */ |
---|
| 541 | { |
---|
| 542 | int i; |
---|
| 543 | for ( i=0; i<ETHER_ADDR_LEN; i++ ) { |
---|
| 544 | if ( adapter->arpcom.ac_enaddr[i] ) |
---|
| 545 | break; |
---|
| 546 | } |
---|
| 547 | if ( i >= ETHER_ADDR_LEN ) { |
---|
| 548 | /* all nulls */ |
---|
| 549 | bcopy(adapter->hw.mac_addr, adapter->arpcom.ac_enaddr, |
---|
| 550 | ETHER_ADDR_LEN); |
---|
| 551 | } |
---|
| 552 | } |
---|
| 553 | #endif |
---|
| 554 | |
---|
| 555 | if (!em_is_valid_ether_addr(adapter->hw.mac_addr)) { |
---|
| 556 | printf("em%d: Invalid mac address\n", adapter->unit); |
---|
| 557 | error = EIO; |
---|
| 558 | goto err_mac_addr; |
---|
| 559 | } |
---|
| 560 | |
---|
| 561 | /* Setup OS specific network interface */ |
---|
| 562 | em_setup_interface(dev, adapter); |
---|
| 563 | |
---|
| 564 | /* Initialize statistics */ |
---|
| 565 | em_clear_hw_cntrs(&adapter->hw); |
---|
| 566 | em_update_stats_counters(adapter); |
---|
| 567 | adapter->hw.get_link_status = 1; |
---|
| 568 | #ifndef __rtems__ |
---|
| 569 | em_check_for_link(&adapter->hw); |
---|
| 570 | #else |
---|
| 571 | /* first check during hw init usually fails - probably we need to wait longer; |
---|
| 572 | * could take a while till the link is up, depends on the partner? |
---|
| 573 | * in any case, rather than waiting here we just proceed... |
---|
| 574 | */ |
---|
| 575 | em_check_for_link(&adapter->hw); |
---|
| 576 | /* em_check_for_link doesn't update 'link_active' |
---|
| 577 | * -- they usually call em_print_link_status() right |
---|
| 578 | * after check_for_link, so let's repeat this |
---|
| 579 | * algorithm here. |
---|
| 580 | */ |
---|
| 581 | em_print_link_status(adapter); |
---|
| 582 | #endif |
---|
| 583 | |
---|
| 584 | /* Print the link status */ |
---|
| 585 | if (adapter->link_active == 1) { |
---|
| 586 | em_get_speed_and_duplex(&adapter->hw, &adapter->link_speed, |
---|
| 587 | &adapter->link_duplex); |
---|
| 588 | printf("em%d: Speed:%d Mbps Duplex:%s\n", |
---|
| 589 | adapter->unit, |
---|
| 590 | adapter->link_speed, |
---|
| 591 | adapter->link_duplex == FULL_DUPLEX ? "Full" : "Half"); |
---|
| 592 | } else |
---|
| 593 | printf("em%d: Speed:N/A Duplex:N/A\n", adapter->unit); |
---|
| 594 | |
---|
| 595 | /* Identify 82544 on PCIX */ |
---|
| 596 | em_get_bus_info(&adapter->hw); |
---|
| 597 | if(adapter->hw.bus_type == em_bus_type_pcix && |
---|
| 598 | adapter->hw.mac_type == em_82544) { |
---|
| 599 | adapter->pcix_82544 = TRUE; |
---|
| 600 | } |
---|
| 601 | else { |
---|
| 602 | adapter->pcix_82544 = FALSE; |
---|
| 603 | } |
---|
| 604 | INIT_DEBUGOUT("em_attach: end"); |
---|
| 605 | return(0); |
---|
| 606 | |
---|
| 607 | err_mac_addr: |
---|
| 608 | err_hw_init: |
---|
| 609 | em_dma_free(adapter, &adapter->rxdma); |
---|
| 610 | err_rx_desc: |
---|
| 611 | em_dma_free(adapter, &adapter->txdma); |
---|
| 612 | err_tx_desc: |
---|
| 613 | err_pci: |
---|
| 614 | #ifndef __rtems__ |
---|
| 615 | em_free_pci_resources(adapter); |
---|
| 616 | #endif |
---|
| 617 | return(error); |
---|
| 618 | |
---|
| 619 | } |
---|
| 620 | |
---|
| 621 | /********************************************************************* |
---|
| 622 | * Device removal routine |
---|
| 623 | * |
---|
| 624 | * The detach entry point is called when the driver is being removed. |
---|
| 625 | * This routine stops the adapter and deallocates all the resources |
---|
| 626 | * that were allocated for driver operation. |
---|
| 627 | * |
---|
| 628 | * return 0 on success, positive on failure |
---|
| 629 | *********************************************************************/ |
---|
| 630 | |
---|
| 631 | #if !defined(__rtems__) || defined(DEBUG_MODULAR) |
---|
| 632 | |
---|
| 633 | static int |
---|
| 634 | em_detach(device_t dev) |
---|
| 635 | { |
---|
| 636 | struct adapter * adapter = device_get_softc(dev); |
---|
| 637 | struct ifnet *ifp = &adapter->arpcom.ac_if; |
---|
| 638 | |
---|
| 639 | INIT_DEBUGOUT("em_detach: begin"); |
---|
| 640 | |
---|
| 641 | EM_LOCK(adapter); |
---|
| 642 | adapter->in_detach = 1; |
---|
| 643 | em_stop(adapter); |
---|
| 644 | em_phy_hw_reset(&adapter->hw); |
---|
| 645 | EM_UNLOCK(adapter); |
---|
| 646 | #ifndef __rtems__ |
---|
| 647 | #if __FreeBSD_version < 500000 |
---|
| 648 | ether_ifdetach(adapter->ifp, ETHER_BPF_SUPPORTED); |
---|
| 649 | #else |
---|
| 650 | ether_ifdetach(adapter->ifp); |
---|
| 651 | if_free(ifp); |
---|
| 652 | #endif |
---|
| 653 | em_free_pci_resources(adapter); |
---|
| 654 | bus_generic_detach(dev); |
---|
| 655 | #else |
---|
| 656 | ether_ifdetach(ifp); |
---|
| 657 | #endif |
---|
| 658 | |
---|
| 659 | /* Free Transmit Descriptor ring */ |
---|
| 660 | if (adapter->tx_desc_base) { |
---|
| 661 | em_dma_free(adapter, &adapter->txdma); |
---|
| 662 | adapter->tx_desc_base = NULL; |
---|
| 663 | } |
---|
| 664 | |
---|
| 665 | /* Free Receive Descriptor ring */ |
---|
| 666 | if (adapter->rx_desc_base) { |
---|
| 667 | em_dma_free(adapter, &adapter->rxdma); |
---|
| 668 | adapter->rx_desc_base = NULL; |
---|
| 669 | } |
---|
| 670 | |
---|
| 671 | /* Remove from the adapter list */ |
---|
| 672 | if (em_adapter_list == adapter) |
---|
| 673 | em_adapter_list = adapter->next; |
---|
| 674 | if (adapter->next != NULL) |
---|
| 675 | adapter->next->prev = adapter->prev; |
---|
| 676 | if (adapter->prev != NULL) |
---|
| 677 | adapter->prev->next = adapter->next; |
---|
| 678 | |
---|
| 679 | EM_LOCK_DESTROY(adapter); |
---|
| 680 | |
---|
| 681 | ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); |
---|
| 682 | ifp->if_timer = 0; |
---|
| 683 | |
---|
| 684 | return(0); |
---|
| 685 | } |
---|
| 686 | |
---|
| 687 | #endif |
---|
| 688 | |
---|
| 689 | #ifndef __rtems__ |
---|
| 690 | /********************************************************************* |
---|
| 691 | * |
---|
| 692 | * Shutdown entry point |
---|
| 693 | * |
---|
| 694 | **********************************************************************/ |
---|
| 695 | |
---|
| 696 | static int |
---|
| 697 | em_shutdown(device_t dev) |
---|
| 698 | { |
---|
| 699 | struct adapter *adapter = device_get_softc(dev); |
---|
| 700 | EM_LOCK(adapter); |
---|
| 701 | em_stop(adapter); |
---|
| 702 | EM_UNLOCK(adapter); |
---|
| 703 | return(0); |
---|
| 704 | } |
---|
| 705 | #endif |
---|
| 706 | |
---|
| 707 | /********************************************************************* |
---|
| 708 | * Transmit entry point |
---|
| 709 | * |
---|
| 710 | * em_start is called by the stack to initiate a transmit. |
---|
| 711 | * The driver will remain in this routine as long as there are |
---|
| 712 | * packets to transmit and transmit resources are available. |
---|
| 713 | * In case resources are not available stack is notified and |
---|
| 714 | * the packet is requeued. |
---|
| 715 | **********************************************************************/ |
---|
| 716 | |
---|
| 717 | static void |
---|
| 718 | em_start_locked(struct ifnet *ifp) |
---|
| 719 | { |
---|
| 720 | struct mbuf *m_head; |
---|
| 721 | struct adapter *adapter = ifp->if_softc; |
---|
| 722 | |
---|
| 723 | mtx_assert(&adapter->mtx, MA_OWNED); |
---|
| 724 | |
---|
| 725 | if (!adapter->link_active) |
---|
| 726 | return; |
---|
| 727 | |
---|
| 728 | while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) { |
---|
| 729 | |
---|
| 730 | IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head); |
---|
| 731 | |
---|
| 732 | if (m_head == NULL) break; |
---|
| 733 | |
---|
| 734 | /* |
---|
| 735 | * em_encap() can modify our pointer, and or make it NULL on |
---|
| 736 | * failure. In that event, we can't requeue. |
---|
| 737 | */ |
---|
| 738 | if (em_encap(adapter, &m_head)) { |
---|
| 739 | if (m_head == NULL) |
---|
| 740 | break; |
---|
| 741 | ifp->if_flags |= IFF_OACTIVE; |
---|
| 742 | IFQ_DRV_PREPEND(&ifp->if_snd, m_head); |
---|
| 743 | break; |
---|
| 744 | } |
---|
| 745 | |
---|
| 746 | /* Send a copy of the frame to the BPF listener */ |
---|
| 747 | #if __FreeBSD_version < 500000 && !defined(__rtems__) |
---|
| 748 | if (ifp->if_bpf) |
---|
| 749 | bpf_mtap(ifp, m_head); |
---|
| 750 | #else |
---|
| 751 | BPF_MTAP(ifp, m_head); |
---|
| 752 | #endif |
---|
| 753 | |
---|
| 754 | /* Set timeout in case hardware has problems transmitting */ |
---|
| 755 | ifp->if_timer = EM_TX_TIMEOUT; |
---|
| 756 | |
---|
| 757 | } |
---|
| 758 | return; |
---|
| 759 | } |
---|
| 760 | |
---|
| 761 | static void |
---|
| 762 | em_start(struct ifnet *ifp) |
---|
| 763 | { |
---|
| 764 | struct adapter *adapter __attribute__((unused)) = ifp->if_softc; |
---|
| 765 | |
---|
| 766 | EM_LOCK(adapter); |
---|
| 767 | em_start_locked(ifp); |
---|
| 768 | EM_UNLOCK(adapter); |
---|
| 769 | return; |
---|
| 770 | } |
---|
| 771 | |
---|
| 772 | /********************************************************************* |
---|
| 773 | * Ioctl entry point |
---|
| 774 | * |
---|
| 775 | * em_ioctl is called when the user wants to configure the |
---|
| 776 | * interface. |
---|
| 777 | * |
---|
| 778 | * return 0 on success, positive on failure |
---|
| 779 | **********************************************************************/ |
---|
| 780 | |
---|
| 781 | #ifndef __rtems__ |
---|
| 782 | static int |
---|
| 783 | em_ioctl(struct ifnet *ifp, u_long command, caddr_t data) |
---|
| 784 | #else |
---|
| 785 | static int |
---|
| 786 | em_ioctl(struct ifnet *ifp, ioctl_command_t command, caddr_t data) |
---|
| 787 | #endif |
---|
| 788 | { |
---|
| 789 | #ifndef __rtems__ |
---|
| 790 | int mask, reinit, error = 0; |
---|
| 791 | #else |
---|
| 792 | int error = 0; |
---|
| 793 | #endif |
---|
| 794 | struct ifreq *ifr = (struct ifreq *) data; |
---|
| 795 | struct adapter * adapter = ifp->if_softc; |
---|
| 796 | |
---|
| 797 | if (adapter->in_detach) return(error); |
---|
| 798 | |
---|
| 799 | switch (command) { |
---|
| 800 | case SIOCSIFADDR: |
---|
| 801 | case SIOCGIFADDR: |
---|
| 802 | IOCTL_DEBUGOUT("ioctl rcv'd: SIOCxIFADDR (Get/Set Interface Addr)"); |
---|
| 803 | ether_ioctl(ifp, command, data); |
---|
| 804 | break; |
---|
| 805 | case SIOCSIFMTU: |
---|
| 806 | IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFMTU (Set Interface MTU)"); |
---|
| 807 | if (ifr->ifr_mtu > MAX_JUMBO_FRAME_SIZE - ETHER_HDR_LEN || \ |
---|
| 808 | /* 82573 does not support jumbo frames */ |
---|
| 809 | (adapter->hw.mac_type == em_82573 && ifr->ifr_mtu > ETHERMTU) ) { |
---|
| 810 | error = EINVAL; |
---|
| 811 | } else { |
---|
| 812 | EM_LOCK(adapter); |
---|
| 813 | ifp->if_mtu = ifr->ifr_mtu; |
---|
| 814 | adapter->hw.max_frame_size = |
---|
| 815 | ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN; |
---|
| 816 | em_init_locked(adapter); |
---|
| 817 | EM_UNLOCK(adapter); |
---|
| 818 | } |
---|
| 819 | break; |
---|
| 820 | case SIOCSIFFLAGS: |
---|
| 821 | IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFFLAGS (Set Interface Flags)"); |
---|
| 822 | EM_LOCK(adapter); |
---|
| 823 | if (ifp->if_flags & IFF_UP) { |
---|
| 824 | if (!(ifp->if_flags & IFF_RUNNING)) { |
---|
| 825 | em_init_locked(adapter); |
---|
| 826 | } |
---|
| 827 | |
---|
| 828 | em_disable_promisc(adapter); |
---|
| 829 | em_set_promisc(adapter); |
---|
| 830 | } else { |
---|
| 831 | if (ifp->if_flags & IFF_RUNNING) { |
---|
| 832 | em_stop(adapter); |
---|
| 833 | } |
---|
| 834 | } |
---|
| 835 | EM_UNLOCK(adapter); |
---|
| 836 | break; |
---|
| 837 | case SIOCADDMULTI: |
---|
| 838 | case SIOCDELMULTI: |
---|
| 839 | #ifdef __rtems__ |
---|
| 840 | if ( (error = ( SIOCADDMULTI == command ? |
---|
| 841 | ether_addmulti( ifr, (struct arpcom*)ifp ) : |
---|
| 842 | ether_delmulti( ifr, (struct arpcom*)ifp ) ) ) ) { |
---|
| 843 | if ( ENETRESET == error ) |
---|
| 844 | error = 0; |
---|
| 845 | else |
---|
| 846 | break; |
---|
| 847 | } |
---|
| 848 | #endif |
---|
| 849 | IOCTL_DEBUGOUT("ioctl rcv'd: SIOC(ADD|DEL)MULTI"); |
---|
| 850 | if (ifp->if_flags & IFF_RUNNING) { |
---|
| 851 | EM_LOCK(adapter); |
---|
| 852 | em_disable_intr(adapter); |
---|
| 853 | em_set_multi(adapter); |
---|
| 854 | if (adapter->hw.mac_type == em_82542_rev2_0) { |
---|
| 855 | em_initialize_receive_unit(adapter); |
---|
| 856 | } |
---|
| 857 | #ifdef DEVICE_POLLING |
---|
| 858 | if (!(ifp->if_flags & IFF_POLLING)) |
---|
| 859 | #endif |
---|
| 860 | em_enable_intr(adapter); |
---|
| 861 | EM_UNLOCK(adapter); |
---|
| 862 | } |
---|
| 863 | break; |
---|
| 864 | #ifndef __rtems__ |
---|
| 865 | case SIOCSIFMEDIA: |
---|
| 866 | case SIOCGIFMEDIA: |
---|
| 867 | IOCTL_DEBUGOUT("ioctl rcv'd: SIOCxIFMEDIA (Get/Set Interface Media)"); |
---|
| 868 | error = ifmedia_ioctl(ifp, ifr, &adapter->media, command); |
---|
| 869 | break; |
---|
| 870 | #else |
---|
| 871 | case SIOCSIFMEDIA: |
---|
| 872 | { |
---|
| 873 | struct rtems_ifmedia mhack; |
---|
| 874 | mhack.ifm_media = ifr->ifr_media; |
---|
| 875 | error = em_media_change(ifp, &mhack); |
---|
| 876 | } |
---|
| 877 | break; |
---|
| 878 | case SIOCGIFMEDIA: |
---|
| 879 | { |
---|
| 880 | struct ifmediareq ifmr; |
---|
| 881 | em_media_status(ifp, &ifmr); |
---|
| 882 | ifr->ifr_media = ifmr.ifm_active; |
---|
| 883 | /* add-in rtems flags */ |
---|
| 884 | if ( adapter->link_active ) |
---|
| 885 | ifr->ifr_media |= IFM_LINK_OK; |
---|
| 886 | if ( !adapter->hw.autoneg ) |
---|
| 887 | ifr->ifr_media |= IFM_ANEG_DIS; |
---|
| 888 | error = 0; |
---|
| 889 | } |
---|
| 890 | break; |
---|
| 891 | #endif |
---|
| 892 | #ifndef __rtems__ |
---|
| 893 | case SIOCSIFCAP: |
---|
| 894 | IOCTL_DEBUGOUT("ioctl rcv'd: SIOCSIFCAP (Set Capabilities)"); |
---|
| 895 | reinit = 0; |
---|
| 896 | mask = ifr->ifr_reqcap ^ ifp->if_capenable; |
---|
| 897 | if (mask & IFCAP_POLLING) |
---|
| 898 | ifp->if_capenable ^= IFCAP_POLLING; |
---|
| 899 | if (mask & IFCAP_HWCSUM) { |
---|
| 900 | ifp->if_capenable ^= IFCAP_HWCSUM; |
---|
| 901 | reinit = 1; |
---|
| 902 | } |
---|
| 903 | if (mask & IFCAP_VLAN_HWTAGGING) { |
---|
| 904 | ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING; |
---|
| 905 | reinit = 1; |
---|
| 906 | } |
---|
| 907 | if (reinit && (ifp->if_flags & IFF_RUNNING)) |
---|
| 908 | em_init(adapter); |
---|
| 909 | break; |
---|
| 910 | #endif |
---|
| 911 | |
---|
| 912 | #ifdef __rtems__ |
---|
| 913 | case SIO_RTEMS_SHOW_STATS: |
---|
| 914 | em_print_hw_stats(adapter); |
---|
| 915 | error = 0; |
---|
| 916 | break; |
---|
| 917 | #endif |
---|
| 918 | |
---|
| 919 | default: |
---|
| 920 | IOCTL_DEBUGOUT1("ioctl received: UNKNOWN (0x%x)", (int)command); |
---|
| 921 | error = EINVAL; |
---|
| 922 | } |
---|
| 923 | |
---|
| 924 | return(error); |
---|
| 925 | } |
---|
| 926 | |
---|
| 927 | /********************************************************************* |
---|
| 928 | * Watchdog entry point |
---|
| 929 | * |
---|
| 930 | * This routine is called whenever hardware quits transmitting. |
---|
| 931 | * |
---|
| 932 | **********************************************************************/ |
---|
| 933 | |
---|
| 934 | static void |
---|
| 935 | em_watchdog(struct ifnet *ifp) |
---|
| 936 | { |
---|
| 937 | struct adapter * adapter; |
---|
| 938 | adapter = ifp->if_softc; |
---|
| 939 | |
---|
| 940 | /* If we are in this routine because of pause frames, then |
---|
| 941 | * don't reset the hardware. |
---|
| 942 | */ |
---|
| 943 | if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_TXOFF) { |
---|
| 944 | ifp->if_timer = EM_TX_TIMEOUT; |
---|
| 945 | return; |
---|
| 946 | } |
---|
| 947 | |
---|
| 948 | if (em_check_for_link(&adapter->hw)) |
---|
| 949 | printf("em%d: watchdog timeout -- resetting\n", adapter->unit); |
---|
| 950 | |
---|
| 951 | ifp->if_flags &= ~IFF_RUNNING; |
---|
| 952 | |
---|
| 953 | em_init(adapter); |
---|
| 954 | |
---|
| 955 | ifp->if_oerrors++; |
---|
| 956 | return; |
---|
| 957 | } |
---|
| 958 | |
---|
| 959 | /********************************************************************* |
---|
| 960 | * Init entry point |
---|
| 961 | * |
---|
| 962 | * This routine is used in two ways. It is used by the stack as |
---|
| 963 | * init entry point in network interface structure. It is also used |
---|
| 964 | * by the driver as a hw/sw initialization routine to get to a |
---|
| 965 | * consistent state. |
---|
| 966 | * |
---|
| 967 | * return 0 on success, positive on failure |
---|
| 968 | **********************************************************************/ |
---|
| 969 | |
---|
| 970 | static void |
---|
| 971 | em_init_locked(struct adapter * adapter) |
---|
| 972 | { |
---|
| 973 | struct ifnet *ifp; |
---|
| 974 | |
---|
| 975 | uint32_t pba; |
---|
| 976 | ifp = &adapter->arpcom.ac_if; |
---|
| 977 | |
---|
| 978 | INIT_DEBUGOUT("em_init: begin"); |
---|
| 979 | |
---|
| 980 | mtx_assert(&adapter->mtx, MA_OWNED); |
---|
| 981 | |
---|
| 982 | em_stop(adapter); |
---|
| 983 | |
---|
| 984 | /* Packet Buffer Allocation (PBA) |
---|
| 985 | * Writing PBA sets the receive portion of the buffer |
---|
| 986 | * the remainder is used for the transmit buffer. |
---|
| 987 | * |
---|
| 988 | * Devices before the 82547 had a Packet Buffer of 64K. |
---|
| 989 | * Default allocation: PBA=48K for Rx, leaving 16K for Tx. |
---|
| 990 | * After the 82547 the buffer was reduced to 40K. |
---|
| 991 | * Default allocation: PBA=30K for Rx, leaving 10K for Tx. |
---|
| 992 | * Note: default does not leave enough room for Jumbo Frame >10k. |
---|
| 993 | */ |
---|
| 994 | if(adapter->hw.mac_type < em_82547) { |
---|
| 995 | /* Total FIFO is 64K */ |
---|
| 996 | if(adapter->rx_buffer_len > EM_RXBUFFER_8192) |
---|
| 997 | pba = E1000_PBA_40K; /* 40K for Rx, 24K for Tx */ |
---|
| 998 | else |
---|
| 999 | pba = E1000_PBA_48K; /* 48K for Rx, 16K for Tx */ |
---|
| 1000 | } else { |
---|
| 1001 | /* Total FIFO is 40K */ |
---|
| 1002 | if(adapter->hw.max_frame_size > EM_RXBUFFER_8192) { |
---|
| 1003 | pba = E1000_PBA_22K; /* 22K for Rx, 18K for Tx */ |
---|
| 1004 | } else { |
---|
| 1005 | pba = E1000_PBA_30K; /* 30K for Rx, 10K for Tx */ |
---|
| 1006 | } |
---|
| 1007 | adapter->tx_fifo_head = 0; |
---|
| 1008 | adapter->tx_head_addr = pba << EM_TX_HEAD_ADDR_SHIFT; |
---|
| 1009 | adapter->tx_fifo_size = (E1000_PBA_40K - pba) << EM_PBA_BYTES_SHIFT; |
---|
| 1010 | } |
---|
| 1011 | INIT_DEBUGOUT1("em_init: pba=%" PRId32 "K",pba); |
---|
| 1012 | E1000_WRITE_REG(&adapter->hw, PBA, pba); |
---|
| 1013 | |
---|
| 1014 | /* Get the latest mac address, User can use a LAA */ |
---|
| 1015 | bcopy(adapter->arpcom.ac_enaddr, adapter->hw.mac_addr, |
---|
| 1016 | ETHER_ADDR_LEN); |
---|
| 1017 | |
---|
| 1018 | /* Initialize the hardware */ |
---|
| 1019 | if (em_hardware_init(adapter)) { |
---|
| 1020 | printf("em%d: Unable to initialize the hardware\n", |
---|
| 1021 | adapter->unit); |
---|
| 1022 | return; |
---|
| 1023 | } |
---|
| 1024 | |
---|
| 1025 | #ifndef __rtems__ |
---|
| 1026 | if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) |
---|
| 1027 | em_enable_vlans(adapter); |
---|
| 1028 | #endif |
---|
| 1029 | |
---|
| 1030 | /* Prepare transmit descriptors and buffers */ |
---|
| 1031 | if (em_setup_transmit_structures(adapter)) { |
---|
| 1032 | printf("em%d: Could not setup transmit structures\n", |
---|
| 1033 | adapter->unit); |
---|
| 1034 | em_stop(adapter); |
---|
| 1035 | return; |
---|
| 1036 | } |
---|
| 1037 | em_initialize_transmit_unit(adapter); |
---|
| 1038 | |
---|
| 1039 | /* Setup Multicast table */ |
---|
| 1040 | em_set_multi(adapter); |
---|
| 1041 | |
---|
| 1042 | /* Prepare receive descriptors and buffers */ |
---|
| 1043 | if (em_setup_receive_structures(adapter)) { |
---|
| 1044 | printf("em%d: Could not setup receive structures\n", |
---|
| 1045 | adapter->unit); |
---|
| 1046 | em_stop(adapter); |
---|
| 1047 | return; |
---|
| 1048 | } |
---|
| 1049 | em_initialize_receive_unit(adapter); |
---|
| 1050 | |
---|
| 1051 | /* Don't loose promiscuous settings */ |
---|
| 1052 | em_set_promisc(adapter); |
---|
| 1053 | |
---|
| 1054 | ifp->if_flags |= IFF_RUNNING; |
---|
| 1055 | ifp->if_flags &= ~IFF_OACTIVE; |
---|
| 1056 | |
---|
| 1057 | #ifndef __rtems__ |
---|
| 1058 | if (adapter->hw.mac_type >= em_82543) { |
---|
| 1059 | if (ifp->if_capenable & IFCAP_TXCSUM) |
---|
| 1060 | ifp->if_hwassist = EM_CHECKSUM_FEATURES; |
---|
| 1061 | else |
---|
| 1062 | ifp->if_hwassist = 0; |
---|
| 1063 | } |
---|
| 1064 | #endif |
---|
| 1065 | |
---|
| 1066 | callout_reset(&adapter->timer, hz, em_local_timer, adapter); |
---|
| 1067 | em_clear_hw_cntrs(&adapter->hw); |
---|
| 1068 | #ifdef DEVICE_POLLING |
---|
| 1069 | /* |
---|
| 1070 | * Only enable interrupts if we are not polling, make sure |
---|
| 1071 | * they are off otherwise. |
---|
| 1072 | */ |
---|
| 1073 | if (ifp->if_flags & IFF_POLLING) |
---|
| 1074 | em_disable_intr(adapter); |
---|
| 1075 | else |
---|
| 1076 | #endif /* DEVICE_POLLING */ |
---|
| 1077 | em_enable_intr(adapter); |
---|
| 1078 | |
---|
| 1079 | /* Don't reset the phy next time init gets called */ |
---|
| 1080 | adapter->hw.phy_reset_disable = TRUE; |
---|
| 1081 | |
---|
| 1082 | return; |
---|
| 1083 | } |
---|
| 1084 | |
---|
| 1085 | static void |
---|
| 1086 | em_init(void *arg) |
---|
| 1087 | { |
---|
| 1088 | struct adapter * adapter = arg; |
---|
| 1089 | |
---|
| 1090 | EM_LOCK(adapter); |
---|
| 1091 | em_init_locked(adapter); |
---|
| 1092 | EM_UNLOCK(adapter); |
---|
| 1093 | return; |
---|
| 1094 | } |
---|
| 1095 | |
---|
| 1096 | |
---|
| 1097 | #ifdef DEVICE_POLLING |
---|
| 1098 | static poll_handler_t em_poll; |
---|
| 1099 | |
---|
| 1100 | static void |
---|
| 1101 | em_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count) |
---|
| 1102 | { |
---|
| 1103 | struct adapter *adapter = ifp->if_softc; |
---|
| 1104 | u_int32_t reg_icr; |
---|
| 1105 | |
---|
| 1106 | mtx_assert(&adapter->mtx, MA_OWNED); |
---|
| 1107 | |
---|
| 1108 | if (!(ifp->if_capenable & IFCAP_POLLING)) { |
---|
| 1109 | ether_poll_deregister(ifp); |
---|
| 1110 | cmd = POLL_DEREGISTER; |
---|
| 1111 | } |
---|
| 1112 | if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */ |
---|
| 1113 | em_enable_intr(adapter); |
---|
| 1114 | return; |
---|
| 1115 | } |
---|
| 1116 | if (cmd == POLL_AND_CHECK_STATUS) { |
---|
| 1117 | reg_icr = E1000_READ_REG(&adapter->hw, ICR); |
---|
| 1118 | if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) { |
---|
| 1119 | callout_stop(&adapter->timer); |
---|
| 1120 | adapter->hw.get_link_status = 1; |
---|
| 1121 | em_check_for_link(&adapter->hw); |
---|
| 1122 | em_print_link_status(adapter); |
---|
| 1123 | callout_reset(&adapter->timer, hz, em_local_timer, adapter); |
---|
| 1124 | } |
---|
| 1125 | } |
---|
| 1126 | if (ifp->if_flags & IFF_RUNNING) { |
---|
| 1127 | em_process_receive_interrupts(adapter, count); |
---|
| 1128 | em_clean_transmit_interrupts(adapter); |
---|
| 1129 | } |
---|
| 1130 | |
---|
| 1131 | if (ifp->if_flags & IFF_RUNNING && !IFQ_DRV_IS_EMPTY(&ifp->if_snd)) |
---|
| 1132 | em_start_locked(ifp); |
---|
| 1133 | } |
---|
| 1134 | |
---|
| 1135 | static void |
---|
| 1136 | em_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) |
---|
| 1137 | { |
---|
| 1138 | struct adapter *adapter = ifp->if_softc; |
---|
| 1139 | |
---|
| 1140 | EM_LOCK(adapter); |
---|
| 1141 | em_poll_locked(ifp, cmd, count); |
---|
| 1142 | EM_UNLOCK(adapter); |
---|
| 1143 | } |
---|
| 1144 | #endif /* DEVICE_POLLING */ |
---|
| 1145 | |
---|
| 1146 | /********************************************************************* |
---|
| 1147 | * |
---|
| 1148 | * Interrupt Service routine |
---|
| 1149 | * |
---|
| 1150 | **********************************************************************/ |
---|
| 1151 | static void |
---|
| 1152 | em_intr(void *arg) |
---|
| 1153 | { |
---|
| 1154 | u_int32_t loop_cnt = EM_MAX_INTR; |
---|
| 1155 | u_int32_t reg_icr; |
---|
| 1156 | struct ifnet *ifp; |
---|
| 1157 | struct adapter *adapter = arg; |
---|
| 1158 | |
---|
| 1159 | EM_LOCK(adapter); |
---|
| 1160 | |
---|
| 1161 | ifp = &adapter->arpcom.ac_if; |
---|
| 1162 | |
---|
| 1163 | #ifdef DEVICE_POLLING |
---|
| 1164 | if (ifp->if_flags & IFF_POLLING) { |
---|
| 1165 | EM_UNLOCK(adapter); |
---|
| 1166 | return; |
---|
| 1167 | } |
---|
| 1168 | |
---|
| 1169 | if ((ifp->if_capenable & IFCAP_POLLING) && |
---|
| 1170 | ether_poll_register(em_poll, ifp)) { |
---|
| 1171 | em_disable_intr(adapter); |
---|
| 1172 | em_poll_locked(ifp, 0, 1); |
---|
| 1173 | EM_UNLOCK(adapter); |
---|
| 1174 | return; |
---|
| 1175 | } |
---|
| 1176 | #endif /* DEVICE_POLLING */ |
---|
| 1177 | |
---|
| 1178 | reg_icr = E1000_READ_REG(&adapter->hw, ICR); |
---|
| 1179 | if (!reg_icr) { |
---|
| 1180 | EM_UNLOCK(adapter); |
---|
| 1181 | return; |
---|
| 1182 | } |
---|
| 1183 | |
---|
| 1184 | /* Link status change */ |
---|
| 1185 | if (reg_icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) { |
---|
| 1186 | callout_stop(&adapter->timer); |
---|
| 1187 | adapter->hw.get_link_status = 1; |
---|
| 1188 | em_check_for_link(&adapter->hw); |
---|
| 1189 | em_print_link_status(adapter); |
---|
| 1190 | callout_reset(&adapter->timer, hz, em_local_timer, adapter); |
---|
| 1191 | } |
---|
| 1192 | |
---|
| 1193 | while (loop_cnt > 0) { |
---|
| 1194 | if (ifp->if_flags & IFF_RUNNING) { |
---|
| 1195 | em_process_receive_interrupts(adapter, -1); |
---|
| 1196 | em_clean_transmit_interrupts(adapter); |
---|
| 1197 | } |
---|
| 1198 | loop_cnt--; |
---|
| 1199 | } |
---|
| 1200 | |
---|
| 1201 | if (ifp->if_flags & IFF_RUNNING && !IFQ_DRV_IS_EMPTY(&ifp->if_snd)) |
---|
| 1202 | em_start_locked(ifp); |
---|
| 1203 | |
---|
| 1204 | EM_UNLOCK(adapter); |
---|
| 1205 | return; |
---|
| 1206 | } |
---|
| 1207 | |
---|
| 1208 | |
---|
| 1209 | |
---|
| 1210 | /********************************************************************* |
---|
| 1211 | * |
---|
| 1212 | * Media Ioctl callback |
---|
| 1213 | * |
---|
| 1214 | * This routine is called whenever the user queries the status of |
---|
| 1215 | * the interface using ifconfig. |
---|
| 1216 | * |
---|
| 1217 | **********************************************************************/ |
---|
| 1218 | static void |
---|
| 1219 | em_media_status(struct ifnet *ifp, struct ifmediareq *ifmr) |
---|
| 1220 | { |
---|
| 1221 | struct adapter * adapter = ifp->if_softc; |
---|
| 1222 | |
---|
| 1223 | INIT_DEBUGOUT("em_media_status: begin"); |
---|
| 1224 | |
---|
| 1225 | em_check_for_link(&adapter->hw); |
---|
| 1226 | if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU) { |
---|
| 1227 | if (adapter->link_active == 0) { |
---|
| 1228 | em_get_speed_and_duplex(&adapter->hw, |
---|
| 1229 | &adapter->link_speed, |
---|
| 1230 | &adapter->link_duplex); |
---|
| 1231 | adapter->link_active = 1; |
---|
| 1232 | } |
---|
| 1233 | } else { |
---|
| 1234 | if (adapter->link_active == 1) { |
---|
| 1235 | adapter->link_speed = 0; |
---|
| 1236 | adapter->link_duplex = 0; |
---|
| 1237 | adapter->link_active = 0; |
---|
| 1238 | } |
---|
| 1239 | } |
---|
| 1240 | |
---|
| 1241 | ifmr->ifm_status = IFM_AVALID; |
---|
| 1242 | ifmr->ifm_active = IFM_ETHER; |
---|
| 1243 | |
---|
| 1244 | if (!adapter->link_active) |
---|
| 1245 | return; |
---|
| 1246 | |
---|
| 1247 | ifmr->ifm_status |= IFM_ACTIVE; |
---|
| 1248 | |
---|
| 1249 | if (adapter->hw.media_type == em_media_type_fiber) { |
---|
| 1250 | ifmr->ifm_active |= IFM_1000_SX | IFM_FDX; |
---|
| 1251 | } else { |
---|
| 1252 | switch (adapter->link_speed) { |
---|
| 1253 | case 10: |
---|
| 1254 | ifmr->ifm_active |= IFM_10_T; |
---|
| 1255 | break; |
---|
| 1256 | case 100: |
---|
| 1257 | ifmr->ifm_active |= IFM_100_TX; |
---|
| 1258 | break; |
---|
| 1259 | case 1000: |
---|
| 1260 | #if __FreeBSD_version < 500000 && !defined(__rtems__) |
---|
| 1261 | ifmr->ifm_active |= IFM_1000_TX; |
---|
| 1262 | #else |
---|
| 1263 | ifmr->ifm_active |= IFM_1000_T; |
---|
| 1264 | #endif |
---|
| 1265 | break; |
---|
| 1266 | } |
---|
| 1267 | if (adapter->link_duplex == FULL_DUPLEX) |
---|
| 1268 | ifmr->ifm_active |= IFM_FDX; |
---|
| 1269 | else |
---|
| 1270 | ifmr->ifm_active |= IFM_HDX; |
---|
| 1271 | } |
---|
| 1272 | return; |
---|
| 1273 | } |
---|
| 1274 | |
---|
| 1275 | /********************************************************************* |
---|
| 1276 | * |
---|
| 1277 | * Media Ioctl callback |
---|
| 1278 | * |
---|
| 1279 | * This routine is called when the user changes speed/duplex using |
---|
| 1280 | * media/mediopt option with ifconfig. |
---|
| 1281 | * |
---|
| 1282 | **********************************************************************/ |
---|
| 1283 | static int |
---|
| 1284 | #ifndef __rtems__ |
---|
| 1285 | em_media_change(struct ifnet *ifp) |
---|
| 1286 | #else |
---|
| 1287 | em_media_change(struct ifnet *ifp, struct rtems_ifmedia *ifm) |
---|
| 1288 | #endif |
---|
| 1289 | { |
---|
| 1290 | struct adapter * adapter = ifp->if_softc; |
---|
| 1291 | #ifndef __rtems__ |
---|
| 1292 | struct ifmedia *ifm = &adapter->media; |
---|
| 1293 | #endif |
---|
| 1294 | |
---|
| 1295 | INIT_DEBUGOUT("em_media_change: begin"); |
---|
| 1296 | |
---|
| 1297 | if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) |
---|
| 1298 | return(EINVAL); |
---|
| 1299 | |
---|
| 1300 | switch (IFM_SUBTYPE(ifm->ifm_media)) { |
---|
| 1301 | case IFM_AUTO: |
---|
| 1302 | adapter->hw.autoneg = DO_AUTO_NEG; |
---|
| 1303 | adapter->hw.autoneg_advertised = AUTONEG_ADV_DEFAULT; |
---|
| 1304 | break; |
---|
| 1305 | case IFM_1000_SX: |
---|
| 1306 | #if __FreeBSD_version < 500000 && !defined(__rtems__) |
---|
| 1307 | case IFM_1000_TX: |
---|
| 1308 | #else |
---|
| 1309 | case IFM_1000_T: |
---|
| 1310 | #endif |
---|
| 1311 | adapter->hw.autoneg = DO_AUTO_NEG; |
---|
| 1312 | adapter->hw.autoneg_advertised = ADVERTISE_1000_FULL; |
---|
| 1313 | break; |
---|
| 1314 | case IFM_100_TX: |
---|
| 1315 | adapter->hw.autoneg = FALSE; |
---|
| 1316 | adapter->hw.autoneg_advertised = 0; |
---|
| 1317 | if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) |
---|
| 1318 | adapter->hw.forced_speed_duplex = em_100_full; |
---|
| 1319 | else |
---|
| 1320 | adapter->hw.forced_speed_duplex = em_100_half; |
---|
| 1321 | break; |
---|
| 1322 | case IFM_10_T: |
---|
| 1323 | adapter->hw.autoneg = FALSE; |
---|
| 1324 | adapter->hw.autoneg_advertised = 0; |
---|
| 1325 | if ((ifm->ifm_media & IFM_GMASK) == IFM_FDX) |
---|
| 1326 | adapter->hw.forced_speed_duplex = em_10_full; |
---|
| 1327 | else |
---|
| 1328 | adapter->hw.forced_speed_duplex = em_10_half; |
---|
| 1329 | break; |
---|
| 1330 | default: |
---|
| 1331 | printf("em%d: Unsupported media type\n", adapter->unit); |
---|
| 1332 | } |
---|
| 1333 | |
---|
| 1334 | /* As the speed/duplex settings my have changed we need to |
---|
| 1335 | * reset the PHY. |
---|
| 1336 | */ |
---|
| 1337 | adapter->hw.phy_reset_disable = FALSE; |
---|
| 1338 | |
---|
| 1339 | em_init(adapter); |
---|
| 1340 | |
---|
| 1341 | return(0); |
---|
| 1342 | } |
---|
| 1343 | |
---|
| 1344 | /********************************************************************* |
---|
| 1345 | * |
---|
| 1346 | * This routine maps the mbufs to tx descriptors. |
---|
| 1347 | * |
---|
| 1348 | * return 0 on success, positive on failure |
---|
| 1349 | **********************************************************************/ |
---|
| 1350 | static int |
---|
| 1351 | em_encap(struct adapter *adapter, struct mbuf **m_headp) |
---|
| 1352 | { |
---|
| 1353 | u_int32_t txd_upper; |
---|
| 1354 | u_int32_t txd_lower, txd_used = 0, txd_saved = 0; |
---|
| 1355 | int i, j, error; |
---|
| 1356 | u_int64_t address; |
---|
| 1357 | |
---|
| 1358 | struct mbuf *m_head; |
---|
| 1359 | |
---|
| 1360 | /* For 82544 Workaround */ |
---|
| 1361 | DESC_ARRAY desc_array; |
---|
| 1362 | u_int32_t array_elements; |
---|
| 1363 | u_int32_t counter; |
---|
| 1364 | |
---|
| 1365 | #ifndef __rtems__ |
---|
| 1366 | #if __FreeBSD_version < 500000 |
---|
| 1367 | struct ifvlan *ifv = NULL; |
---|
| 1368 | #else |
---|
| 1369 | struct m_tag *mtag; |
---|
| 1370 | #endif |
---|
| 1371 | #endif |
---|
| 1372 | bus_dma_segment_t segs[EM_MAX_SCATTER]; |
---|
| 1373 | #ifndef __rtems__ |
---|
| 1374 | bus_dmamap_t map; |
---|
| 1375 | #endif |
---|
| 1376 | int nsegs; |
---|
| 1377 | struct em_buffer *tx_buffer = NULL; |
---|
| 1378 | struct em_tx_desc *current_tx_desc = NULL; |
---|
| 1379 | #ifndef __rtems__ |
---|
| 1380 | struct ifnet *ifp = &adapter->arpcom.ac_if; |
---|
| 1381 | #endif |
---|
| 1382 | |
---|
| 1383 | m_head = *m_headp; |
---|
| 1384 | |
---|
| 1385 | /* |
---|
| 1386 | * Force a cleanup if number of TX descriptors |
---|
| 1387 | * available hits the threshold |
---|
| 1388 | */ |
---|
| 1389 | if (adapter->num_tx_desc_avail <= EM_TX_CLEANUP_THRESHOLD) { |
---|
| 1390 | em_clean_transmit_interrupts(adapter); |
---|
| 1391 | if (adapter->num_tx_desc_avail <= EM_TX_CLEANUP_THRESHOLD) { |
---|
| 1392 | adapter->no_tx_desc_avail1++; |
---|
| 1393 | return(ENOBUFS); |
---|
| 1394 | } |
---|
| 1395 | } |
---|
| 1396 | |
---|
| 1397 | #ifndef __rtems__ |
---|
| 1398 | /* |
---|
| 1399 | * Map the packet for DMA. |
---|
| 1400 | */ |
---|
| 1401 | if (bus_dmamap_create(adapter->txtag, BUS_DMA_NOWAIT, &map)) { |
---|
| 1402 | adapter->no_tx_map_avail++; |
---|
| 1403 | return (ENOMEM); |
---|
| 1404 | } |
---|
| 1405 | error = bus_dmamap_load_mbuf_sg(adapter->txtag, map, m_head, segs, |
---|
| 1406 | &nsegs, BUS_DMA_NOWAIT); |
---|
| 1407 | if (error != 0) { |
---|
| 1408 | adapter->no_tx_dma_setup++; |
---|
| 1409 | bus_dmamap_destroy(adapter->txtag, map); |
---|
| 1410 | return (error); |
---|
| 1411 | } |
---|
| 1412 | #else |
---|
| 1413 | error = 0; |
---|
| 1414 | { |
---|
| 1415 | struct mbuf *m; |
---|
| 1416 | for ( m=m_head, nsegs=0; m; m=m->m_next, nsegs++ ) { |
---|
| 1417 | if ( nsegs >= sizeof(segs)/sizeof(segs[0]) ) { |
---|
| 1418 | error = -1; |
---|
| 1419 | break; |
---|
| 1420 | } |
---|
| 1421 | segs[nsegs].ds_addr = mtod(m, unsigned); |
---|
| 1422 | segs[nsegs].ds_len = m->m_len; |
---|
| 1423 | } |
---|
| 1424 | } |
---|
| 1425 | #endif |
---|
| 1426 | KASSERT(nsegs != 0, ("em_encap: empty packet")); |
---|
| 1427 | |
---|
| 1428 | if (nsegs > adapter->num_tx_desc_avail) { |
---|
| 1429 | adapter->no_tx_desc_avail2++; |
---|
| 1430 | bus_dmamap_destroy(adapter->txtag, map); |
---|
| 1431 | return (ENOBUFS); |
---|
| 1432 | } |
---|
| 1433 | |
---|
| 1434 | |
---|
| 1435 | #ifndef __rtems__ |
---|
| 1436 | if (ifp->if_hwassist > 0) { |
---|
| 1437 | em_transmit_checksum_setup(adapter, m_head, |
---|
| 1438 | &txd_upper, &txd_lower); |
---|
| 1439 | } else |
---|
| 1440 | #endif |
---|
| 1441 | txd_upper = txd_lower = 0; |
---|
| 1442 | |
---|
| 1443 | |
---|
| 1444 | #ifndef __rtems__ |
---|
| 1445 | /* Find out if we are in vlan mode */ |
---|
| 1446 | #if __FreeBSD_version < 500000 |
---|
| 1447 | if ((m_head->m_flags & (M_PROTO1|M_PKTHDR)) == (M_PROTO1|M_PKTHDR) && |
---|
| 1448 | m_head->m_pkthdr.rcvif != NULL && |
---|
| 1449 | m_head->m_pkthdr.rcvif->if_type == IFT_L2VLAN) |
---|
| 1450 | ifv = m_head->m_pkthdr.rcvif->if_softc; |
---|
| 1451 | #else |
---|
| 1452 | mtag = VLAN_OUTPUT_TAG(ifp, m_head); |
---|
| 1453 | #endif |
---|
| 1454 | |
---|
| 1455 | /* |
---|
| 1456 | * When operating in promiscuous mode, hardware encapsulation for |
---|
| 1457 | * packets is disabled. This means we have to add the vlan |
---|
| 1458 | * encapsulation in the driver, since it will have come down from the |
---|
| 1459 | * VLAN layer with a tag instead of a VLAN header. |
---|
| 1460 | */ |
---|
| 1461 | if (mtag != NULL && adapter->em_insert_vlan_header) { |
---|
| 1462 | struct ether_vlan_header *evl; |
---|
| 1463 | struct ether_header eh; |
---|
| 1464 | |
---|
| 1465 | m_head = m_pullup(m_head, sizeof(eh)); |
---|
| 1466 | if (m_head == NULL) { |
---|
| 1467 | *m_headp = NULL; |
---|
| 1468 | bus_dmamap_destroy(adapter->txtag, map); |
---|
| 1469 | return (ENOBUFS); |
---|
| 1470 | } |
---|
| 1471 | eh = *mtod(m_head, struct ether_header *); |
---|
| 1472 | M_PREPEND(m_head, sizeof(*evl), M_DONTWAIT); |
---|
| 1473 | if (m_head == NULL) { |
---|
| 1474 | *m_headp = NULL; |
---|
| 1475 | bus_dmamap_destroy(adapter->txtag, map); |
---|
| 1476 | return (ENOBUFS); |
---|
| 1477 | } |
---|
| 1478 | m_head = m_pullup(m_head, sizeof(*evl)); |
---|
| 1479 | if (m_head == NULL) { |
---|
| 1480 | *m_headp = NULL; |
---|
| 1481 | bus_dmamap_destroy(adapter->txtag, map); |
---|
| 1482 | return (ENOBUFS); |
---|
| 1483 | } |
---|
| 1484 | evl = mtod(m_head, struct ether_vlan_header *); |
---|
| 1485 | bcopy(&eh, evl, sizeof(*evl)); |
---|
| 1486 | evl->evl_proto = evl->evl_encap_proto; |
---|
| 1487 | evl->evl_encap_proto = htons(ETHERTYPE_VLAN); |
---|
| 1488 | evl->evl_tag = htons(VLAN_TAG_VALUE(mtag)); |
---|
| 1489 | m_tag_delete(m_head, mtag); |
---|
| 1490 | mtag = NULL; |
---|
| 1491 | *m_headp = m_head; |
---|
| 1492 | } |
---|
| 1493 | #endif |
---|
| 1494 | |
---|
| 1495 | i = adapter->next_avail_tx_desc; |
---|
| 1496 | if (adapter->pcix_82544) { |
---|
| 1497 | txd_saved = i; |
---|
| 1498 | txd_used = 0; |
---|
| 1499 | } |
---|
| 1500 | for (j = 0; j < nsegs; j++) { |
---|
| 1501 | /* If adapter is 82544 and on PCIX bus */ |
---|
| 1502 | if(adapter->pcix_82544) { |
---|
| 1503 | array_elements = 0; |
---|
| 1504 | address = htole64(segs[j].ds_addr); |
---|
| 1505 | /* |
---|
| 1506 | * Check the Address and Length combination and |
---|
| 1507 | * split the data accordingly |
---|
| 1508 | */ |
---|
| 1509 | array_elements = em_fill_descriptors(address, |
---|
| 1510 | htole32(segs[j].ds_len), |
---|
| 1511 | &desc_array); |
---|
| 1512 | for (counter = 0; counter < array_elements; counter++) { |
---|
| 1513 | if (txd_used == adapter->num_tx_desc_avail) { |
---|
| 1514 | adapter->next_avail_tx_desc = txd_saved; |
---|
| 1515 | adapter->no_tx_desc_avail2++; |
---|
| 1516 | bus_dmamap_destroy(adapter->txtag, map); |
---|
| 1517 | return (ENOBUFS); |
---|
| 1518 | } |
---|
| 1519 | tx_buffer = &adapter->tx_buffer_area[i]; |
---|
| 1520 | current_tx_desc = &adapter->tx_desc_base[i]; |
---|
| 1521 | current_tx_desc->buffer_addr = htole64( |
---|
| 1522 | desc_array.descriptor[counter].address); |
---|
| 1523 | current_tx_desc->lower.data = htole32( |
---|
| 1524 | (adapter->txd_cmd | txd_lower | |
---|
| 1525 | (u_int16_t)desc_array.descriptor[counter].length)); |
---|
| 1526 | current_tx_desc->upper.data = htole32((txd_upper)); |
---|
| 1527 | if (++i == adapter->num_tx_desc) |
---|
| 1528 | i = 0; |
---|
| 1529 | |
---|
| 1530 | tx_buffer->m_head = NULL; |
---|
| 1531 | txd_used++; |
---|
| 1532 | } |
---|
| 1533 | } else { |
---|
| 1534 | tx_buffer = &adapter->tx_buffer_area[i]; |
---|
| 1535 | current_tx_desc = &adapter->tx_desc_base[i]; |
---|
| 1536 | |
---|
| 1537 | current_tx_desc->buffer_addr = htole64(segs[j].ds_addr); |
---|
| 1538 | current_tx_desc->lower.data = htole32( |
---|
| 1539 | adapter->txd_cmd | txd_lower | segs[j].ds_len); |
---|
| 1540 | current_tx_desc->upper.data = htole32(txd_upper); |
---|
| 1541 | |
---|
| 1542 | if (++i == adapter->num_tx_desc) |
---|
| 1543 | i = 0; |
---|
| 1544 | |
---|
| 1545 | tx_buffer->m_head = NULL; |
---|
| 1546 | } |
---|
| 1547 | } |
---|
| 1548 | |
---|
| 1549 | adapter->next_avail_tx_desc = i; |
---|
| 1550 | if (adapter->pcix_82544) { |
---|
| 1551 | adapter->num_tx_desc_avail -= txd_used; |
---|
| 1552 | } |
---|
| 1553 | else { |
---|
| 1554 | adapter->num_tx_desc_avail -= nsegs; |
---|
| 1555 | } |
---|
| 1556 | |
---|
| 1557 | #ifndef __rtems__ |
---|
| 1558 | #if __FreeBSD_version < 500000 |
---|
| 1559 | if (ifv != NULL) { |
---|
| 1560 | /* Set the vlan id */ |
---|
| 1561 | current_tx_desc->upper.fields.special = htole16(ifv->ifv_tag); |
---|
| 1562 | #else |
---|
| 1563 | if (mtag != NULL) { |
---|
| 1564 | /* Set the vlan id */ |
---|
| 1565 | current_tx_desc->upper.fields.special = htole16(VLAN_TAG_VALUE(mtag)); |
---|
| 1566 | #endif |
---|
| 1567 | |
---|
| 1568 | /* Tell hardware to add tag */ |
---|
| 1569 | current_tx_desc->lower.data |= htole32(E1000_TXD_CMD_VLE); |
---|
| 1570 | } |
---|
| 1571 | #endif |
---|
| 1572 | |
---|
| 1573 | tx_buffer->m_head = m_head; |
---|
| 1574 | #ifndef __rtems__ |
---|
| 1575 | tx_buffer->map = map; |
---|
| 1576 | #endif |
---|
| 1577 | bus_dmamap_sync(adapter->txtag, map, BUS_DMASYNC_PREWRITE); |
---|
| 1578 | |
---|
| 1579 | /* |
---|
| 1580 | * Last Descriptor of Packet needs End Of Packet (EOP) |
---|
| 1581 | */ |
---|
| 1582 | current_tx_desc->lower.data |= htole32(E1000_TXD_CMD_EOP); |
---|
| 1583 | |
---|
| 1584 | /* |
---|
| 1585 | * Advance the Transmit Descriptor Tail (Tdt), this tells the E1000 |
---|
| 1586 | * that this frame is available to transmit. |
---|
| 1587 | */ |
---|
| 1588 | bus_dmamap_sync(adapter->txdma.dma_tag, adapter->txdma.dma_map, |
---|
| 1589 | BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); |
---|
| 1590 | if (adapter->hw.mac_type == em_82547 && |
---|
| 1591 | adapter->link_duplex == HALF_DUPLEX) { |
---|
| 1592 | em_82547_move_tail_locked(adapter); |
---|
| 1593 | } else { |
---|
| 1594 | E1000_WRITE_REG(&adapter->hw, TDT, i); |
---|
| 1595 | if (adapter->hw.mac_type == em_82547) { |
---|
| 1596 | em_82547_update_fifo_head(adapter, m_head->m_pkthdr.len); |
---|
| 1597 | } |
---|
| 1598 | } |
---|
| 1599 | |
---|
| 1600 | return(0); |
---|
| 1601 | } |
---|
| 1602 | |
---|
| 1603 | /********************************************************************* |
---|
| 1604 | * |
---|
| 1605 | * 82547 workaround to avoid controller hang in half-duplex environment. |
---|
| 1606 | * The workaround is to avoid queuing a large packet that would span |
---|
| 1607 | * the internal Tx FIFO ring boundary. We need to reset the FIFO pointers |
---|
| 1608 | * in this case. We do that only when FIFO is quiescent. |
---|
| 1609 | * |
---|
| 1610 | **********************************************************************/ |
---|
| 1611 | static void |
---|
| 1612 | em_82547_move_tail_locked(struct adapter *adapter) |
---|
| 1613 | { |
---|
| 1614 | uint16_t hw_tdt; |
---|
| 1615 | uint16_t sw_tdt; |
---|
| 1616 | struct em_tx_desc *tx_desc; |
---|
| 1617 | uint16_t length = 0; |
---|
| 1618 | boolean_t eop = 0; |
---|
| 1619 | |
---|
| 1620 | EM_LOCK_ASSERT(adapter); |
---|
| 1621 | |
---|
| 1622 | hw_tdt = E1000_READ_REG(&adapter->hw, TDT); |
---|
| 1623 | sw_tdt = adapter->next_avail_tx_desc; |
---|
| 1624 | |
---|
| 1625 | while (hw_tdt != sw_tdt) { |
---|
| 1626 | tx_desc = &adapter->tx_desc_base[hw_tdt]; |
---|
| 1627 | length += tx_desc->lower.flags.length; |
---|
| 1628 | eop = tx_desc->lower.data & E1000_TXD_CMD_EOP; |
---|
| 1629 | if(++hw_tdt == adapter->num_tx_desc) |
---|
| 1630 | hw_tdt = 0; |
---|
| 1631 | |
---|
| 1632 | if(eop) { |
---|
| 1633 | if (em_82547_fifo_workaround(adapter, length)) { |
---|
| 1634 | adapter->tx_fifo_wrk_cnt++; |
---|
| 1635 | callout_reset(&adapter->tx_fifo_timer, 1, |
---|
| 1636 | em_82547_move_tail, adapter); |
---|
| 1637 | break; |
---|
| 1638 | } |
---|
| 1639 | E1000_WRITE_REG(&adapter->hw, TDT, hw_tdt); |
---|
| 1640 | em_82547_update_fifo_head(adapter, length); |
---|
| 1641 | length = 0; |
---|
| 1642 | } |
---|
| 1643 | } |
---|
| 1644 | return; |
---|
| 1645 | } |
---|
| 1646 | |
---|
| 1647 | #ifndef __rtems__ |
---|
| 1648 | static void |
---|
| 1649 | em_82547_move_tail(void *arg) |
---|
| 1650 | { |
---|
| 1651 | struct adapter *adapter = arg; |
---|
| 1652 | |
---|
| 1653 | EM_LOCK(adapter); |
---|
| 1654 | em_82547_move_tail_locked(adapter); |
---|
| 1655 | EM_UNLOCK(adapter); |
---|
| 1656 | } |
---|
| 1657 | #endif |
---|
| 1658 | |
---|
| 1659 | static int |
---|
| 1660 | em_82547_fifo_workaround(struct adapter *adapter, int len) |
---|
| 1661 | { |
---|
| 1662 | int fifo_space, fifo_pkt_len; |
---|
| 1663 | |
---|
| 1664 | fifo_pkt_len = EM_ROUNDUP(len + EM_FIFO_HDR, EM_FIFO_HDR); |
---|
| 1665 | |
---|
| 1666 | if (adapter->link_duplex == HALF_DUPLEX) { |
---|
| 1667 | fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head; |
---|
| 1668 | |
---|
| 1669 | if (fifo_pkt_len >= (EM_82547_PKT_THRESH + fifo_space)) { |
---|
| 1670 | if (em_82547_tx_fifo_reset(adapter)) { |
---|
| 1671 | return(0); |
---|
| 1672 | } |
---|
| 1673 | else { |
---|
| 1674 | return(1); |
---|
| 1675 | } |
---|
| 1676 | } |
---|
| 1677 | } |
---|
| 1678 | |
---|
| 1679 | return(0); |
---|
| 1680 | } |
---|
| 1681 | |
---|
| 1682 | static void |
---|
| 1683 | em_82547_update_fifo_head(struct adapter *adapter, int len) |
---|
| 1684 | { |
---|
| 1685 | int fifo_pkt_len = EM_ROUNDUP(len + EM_FIFO_HDR, EM_FIFO_HDR); |
---|
| 1686 | |
---|
| 1687 | /* tx_fifo_head is always 16 byte aligned */ |
---|
| 1688 | adapter->tx_fifo_head += fifo_pkt_len; |
---|
| 1689 | if (adapter->tx_fifo_head >= adapter->tx_fifo_size) { |
---|
| 1690 | adapter->tx_fifo_head -= adapter->tx_fifo_size; |
---|
| 1691 | } |
---|
| 1692 | |
---|
| 1693 | return; |
---|
| 1694 | } |
---|
| 1695 | |
---|
| 1696 | |
---|
| 1697 | static int |
---|
| 1698 | em_82547_tx_fifo_reset(struct adapter *adapter) |
---|
| 1699 | { |
---|
| 1700 | uint32_t tctl; |
---|
| 1701 | |
---|
| 1702 | if ( (E1000_READ_REG(&adapter->hw, TDT) == |
---|
| 1703 | E1000_READ_REG(&adapter->hw, TDH)) && |
---|
| 1704 | (E1000_READ_REG(&adapter->hw, TDFT) == |
---|
| 1705 | E1000_READ_REG(&adapter->hw, TDFH)) && |
---|
| 1706 | (E1000_READ_REG(&adapter->hw, TDFTS) == |
---|
| 1707 | E1000_READ_REG(&adapter->hw, TDFHS)) && |
---|
| 1708 | (E1000_READ_REG(&adapter->hw, TDFPC) == 0)) { |
---|
| 1709 | |
---|
| 1710 | /* Disable TX unit */ |
---|
| 1711 | tctl = E1000_READ_REG(&adapter->hw, TCTL); |
---|
| 1712 | E1000_WRITE_REG(&adapter->hw, TCTL, tctl & ~E1000_TCTL_EN); |
---|
| 1713 | |
---|
| 1714 | /* Reset FIFO pointers */ |
---|
| 1715 | E1000_WRITE_REG(&adapter->hw, TDFT, adapter->tx_head_addr); |
---|
| 1716 | E1000_WRITE_REG(&adapter->hw, TDFH, adapter->tx_head_addr); |
---|
| 1717 | E1000_WRITE_REG(&adapter->hw, TDFTS, adapter->tx_head_addr); |
---|
| 1718 | E1000_WRITE_REG(&adapter->hw, TDFHS, adapter->tx_head_addr); |
---|
| 1719 | |
---|
| 1720 | /* Re-enable TX unit */ |
---|
| 1721 | E1000_WRITE_REG(&adapter->hw, TCTL, tctl); |
---|
| 1722 | E1000_WRITE_FLUSH(&adapter->hw); |
---|
| 1723 | |
---|
| 1724 | adapter->tx_fifo_head = 0; |
---|
| 1725 | adapter->tx_fifo_reset_cnt++; |
---|
| 1726 | |
---|
| 1727 | return(TRUE); |
---|
| 1728 | } |
---|
| 1729 | else { |
---|
| 1730 | return(FALSE); |
---|
| 1731 | } |
---|
| 1732 | } |
---|
| 1733 | |
---|
| 1734 | static void |
---|
| 1735 | em_set_promisc(struct adapter * adapter) |
---|
| 1736 | { |
---|
| 1737 | |
---|
| 1738 | u_int32_t reg_rctl; |
---|
| 1739 | struct ifnet *ifp = &adapter->arpcom.ac_if; |
---|
| 1740 | |
---|
| 1741 | reg_rctl = E1000_READ_REG(&adapter->hw, RCTL); |
---|
| 1742 | |
---|
| 1743 | if (ifp->if_flags & IFF_PROMISC) { |
---|
| 1744 | reg_rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE); |
---|
| 1745 | E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl); |
---|
| 1746 | #ifndef __rtems__ |
---|
| 1747 | /* Disable VLAN stripping in promiscous mode |
---|
| 1748 | * This enables bridging of vlan tagged frames to occur |
---|
| 1749 | * and also allows vlan tags to be seen in tcpdump |
---|
| 1750 | */ |
---|
| 1751 | if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) |
---|
| 1752 | em_disable_vlans(adapter); |
---|
| 1753 | adapter->em_insert_vlan_header = 1; |
---|
| 1754 | #endif |
---|
| 1755 | } else if (ifp->if_flags & IFF_ALLMULTI) { |
---|
| 1756 | reg_rctl |= E1000_RCTL_MPE; |
---|
| 1757 | reg_rctl &= ~E1000_RCTL_UPE; |
---|
| 1758 | E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl); |
---|
| 1759 | #ifndef __rtems__ |
---|
| 1760 | adapter->em_insert_vlan_header = 0; |
---|
| 1761 | } else |
---|
| 1762 | adapter->em_insert_vlan_header = 0; |
---|
| 1763 | #else |
---|
| 1764 | } |
---|
| 1765 | #endif |
---|
| 1766 | |
---|
| 1767 | return; |
---|
| 1768 | } |
---|
| 1769 | |
---|
| 1770 | static void |
---|
| 1771 | em_disable_promisc(struct adapter * adapter) |
---|
| 1772 | { |
---|
| 1773 | u_int32_t reg_rctl; |
---|
| 1774 | #ifndef __rtems__ |
---|
| 1775 | struct ifnet *ifp = adapter->ifp; |
---|
| 1776 | #endif |
---|
| 1777 | |
---|
| 1778 | reg_rctl = E1000_READ_REG(&adapter->hw, RCTL); |
---|
| 1779 | |
---|
| 1780 | reg_rctl &= (~E1000_RCTL_UPE); |
---|
| 1781 | reg_rctl &= (~E1000_RCTL_MPE); |
---|
| 1782 | E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl); |
---|
| 1783 | |
---|
| 1784 | #ifndef __rtems__ |
---|
| 1785 | if (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) |
---|
| 1786 | em_enable_vlans(adapter); |
---|
| 1787 | adapter->em_insert_vlan_header = 0; |
---|
| 1788 | #endif |
---|
| 1789 | |
---|
| 1790 | return; |
---|
| 1791 | } |
---|
| 1792 | |
---|
| 1793 | |
---|
| 1794 | /********************************************************************* |
---|
| 1795 | * Multicast Update |
---|
| 1796 | * |
---|
| 1797 | * This routine is called whenever multicast address list is updated. |
---|
| 1798 | * |
---|
| 1799 | **********************************************************************/ |
---|
| 1800 | |
---|
| 1801 | static void |
---|
| 1802 | em_set_multi(struct adapter * adapter) |
---|
| 1803 | { |
---|
| 1804 | u_int32_t reg_rctl = 0; |
---|
| 1805 | u_int8_t mta[MAX_NUM_MULTICAST_ADDRESSES * ETH_LENGTH_OF_ADDRESS]; |
---|
| 1806 | #ifndef __rtems__ |
---|
| 1807 | struct ifmultiaddr *ifma; |
---|
| 1808 | #endif |
---|
| 1809 | int mcnt = 0; |
---|
| 1810 | struct ifnet *ifp = &adapter->arpcom.ac_if; |
---|
| 1811 | |
---|
| 1812 | IOCTL_DEBUGOUT("em_set_multi: begin"); |
---|
| 1813 | |
---|
| 1814 | if (adapter->hw.mac_type == em_82542_rev2_0) { |
---|
| 1815 | reg_rctl = E1000_READ_REG(&adapter->hw, RCTL); |
---|
| 1816 | if (adapter->hw.pci_cmd_word & CMD_MEM_WRT_INVALIDATE) { |
---|
| 1817 | em_pci_clear_mwi(&adapter->hw); |
---|
| 1818 | } |
---|
| 1819 | reg_rctl |= E1000_RCTL_RST; |
---|
| 1820 | E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl); |
---|
| 1821 | msec_delay(5); |
---|
| 1822 | } |
---|
| 1823 | |
---|
| 1824 | #ifndef __rtems__ |
---|
| 1825 | IF_ADDR_LOCK(ifp); |
---|
| 1826 | #if __FreeBSD_version < 500000 |
---|
| 1827 | LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { |
---|
| 1828 | #else |
---|
| 1829 | TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { |
---|
| 1830 | #endif |
---|
| 1831 | if (ifma->ifma_addr->sa_family != AF_LINK) |
---|
| 1832 | continue; |
---|
| 1833 | |
---|
| 1834 | if (mcnt == MAX_NUM_MULTICAST_ADDRESSES) break; |
---|
| 1835 | |
---|
| 1836 | bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr), |
---|
| 1837 | &mta[mcnt*ETH_LENGTH_OF_ADDRESS], ETH_LENGTH_OF_ADDRESS); |
---|
| 1838 | mcnt++; |
---|
| 1839 | } |
---|
| 1840 | IF_ADDR_UNLOCK(ifp); |
---|
| 1841 | #else |
---|
| 1842 | { |
---|
| 1843 | /* Don't know how to handle address ranges - we warn and ignore |
---|
| 1844 | * for now... |
---|
| 1845 | */ |
---|
| 1846 | struct ether_multi *enm; |
---|
| 1847 | struct ether_multistep step; |
---|
| 1848 | |
---|
| 1849 | ETHER_FIRST_MULTI(step, (struct arpcom*)ifp, enm); |
---|
| 1850 | while ( enm != NULL ) { |
---|
| 1851 | if ( mcnt == MAX_NUM_MULTICAST_ADDRESSES ) |
---|
| 1852 | break; |
---|
| 1853 | if ( memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN) ) { |
---|
| 1854 | printk("if_em: Unable to handle multicast wildcard (not ported yet); skipping/ignoring\n"); |
---|
| 1855 | goto skiptonext; |
---|
| 1856 | } else { |
---|
| 1857 | bcopy(enm->enm_addrlo, &mta[mcnt * ETHER_ADDR_LEN], ETHER_ADDR_LEN); |
---|
| 1858 | } |
---|
| 1859 | mcnt++; |
---|
| 1860 | skiptonext: |
---|
| 1861 | ETHER_NEXT_MULTI( step, enm ); |
---|
| 1862 | } |
---|
| 1863 | } |
---|
| 1864 | #endif |
---|
| 1865 | |
---|
| 1866 | if (mcnt >= MAX_NUM_MULTICAST_ADDRESSES) { |
---|
| 1867 | reg_rctl = E1000_READ_REG(&adapter->hw, RCTL); |
---|
| 1868 | reg_rctl |= E1000_RCTL_MPE; |
---|
| 1869 | E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl); |
---|
| 1870 | } else |
---|
| 1871 | em_mc_addr_list_update(&adapter->hw, mta, mcnt, 0, 1); |
---|
| 1872 | |
---|
| 1873 | if (adapter->hw.mac_type == em_82542_rev2_0) { |
---|
| 1874 | reg_rctl = E1000_READ_REG(&adapter->hw, RCTL); |
---|
| 1875 | reg_rctl &= ~E1000_RCTL_RST; |
---|
| 1876 | E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl); |
---|
| 1877 | msec_delay(5); |
---|
| 1878 | if (adapter->hw.pci_cmd_word & CMD_MEM_WRT_INVALIDATE) { |
---|
| 1879 | em_pci_set_mwi(&adapter->hw); |
---|
| 1880 | } |
---|
| 1881 | } |
---|
| 1882 | |
---|
| 1883 | return; |
---|
| 1884 | } |
---|
| 1885 | |
---|
| 1886 | #ifndef __rtems__ |
---|
| 1887 | /********************************************************************* |
---|
| 1888 | * Timer routine |
---|
| 1889 | * |
---|
| 1890 | * This routine checks for link status and updates statistics. |
---|
| 1891 | * |
---|
| 1892 | **********************************************************************/ |
---|
| 1893 | |
---|
| 1894 | static void |
---|
| 1895 | em_local_timer(void *arg) |
---|
| 1896 | { |
---|
| 1897 | struct ifnet *ifp; |
---|
| 1898 | struct adapter * adapter = arg; |
---|
| 1899 | ifp = &adapter->arpcom.ac_if; |
---|
| 1900 | |
---|
| 1901 | EM_LOCK(adapter); |
---|
| 1902 | |
---|
| 1903 | em_check_for_link(&adapter->hw); |
---|
| 1904 | em_print_link_status(adapter); |
---|
| 1905 | em_update_stats_counters(adapter); |
---|
| 1906 | if (em_display_debug_stats && ifp->if_flags & IFF_RUNNING) { |
---|
| 1907 | em_print_hw_stats(adapter); |
---|
| 1908 | } |
---|
| 1909 | em_smartspeed(adapter); |
---|
| 1910 | |
---|
| 1911 | callout_reset(&adapter->timer, hz, em_local_timer, adapter); |
---|
| 1912 | |
---|
| 1913 | EM_UNLOCK(adapter); |
---|
| 1914 | return; |
---|
| 1915 | } |
---|
| 1916 | #endif |
---|
| 1917 | |
---|
| 1918 | static void |
---|
| 1919 | em_print_link_status(struct adapter * adapter) |
---|
| 1920 | { |
---|
| 1921 | #ifndef __rtems__ |
---|
| 1922 | struct ifnet *ifp = &adapter->arpcom.ac_if; |
---|
| 1923 | #endif |
---|
| 1924 | |
---|
| 1925 | if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU) { |
---|
| 1926 | if (adapter->link_active == 0) { |
---|
| 1927 | em_get_speed_and_duplex(&adapter->hw, |
---|
| 1928 | &adapter->link_speed, |
---|
| 1929 | &adapter->link_duplex); |
---|
| 1930 | if (bootverbose) |
---|
| 1931 | printf("em%d: Link is up %d Mbps %s\n", |
---|
| 1932 | adapter->unit, |
---|
| 1933 | adapter->link_speed, |
---|
| 1934 | ((adapter->link_duplex == FULL_DUPLEX) ? |
---|
| 1935 | "Full Duplex" : "Half Duplex")); |
---|
| 1936 | adapter->link_active = 1; |
---|
| 1937 | adapter->smartspeed = 0; |
---|
| 1938 | #ifndef __rtems__ |
---|
| 1939 | if_link_state_change(ifp, LINK_STATE_UP); |
---|
| 1940 | #endif |
---|
| 1941 | } |
---|
| 1942 | } else { |
---|
| 1943 | if (adapter->link_active == 1) { |
---|
| 1944 | adapter->link_speed = 0; |
---|
| 1945 | adapter->link_duplex = 0; |
---|
| 1946 | if (bootverbose) |
---|
| 1947 | printf("em%d: Link is Down\n", adapter->unit); |
---|
| 1948 | adapter->link_active = 0; |
---|
| 1949 | #ifndef __rtems__ |
---|
| 1950 | if_link_state_change(ifp, LINK_STATE_UP); |
---|
| 1951 | if_link_state_change(ifp, LINK_STATE_DOWN); |
---|
| 1952 | #endif |
---|
| 1953 | } |
---|
| 1954 | } |
---|
| 1955 | |
---|
| 1956 | return; |
---|
| 1957 | } |
---|
| 1958 | |
---|
| 1959 | /********************************************************************* |
---|
| 1960 | * |
---|
| 1961 | * This routine disables all traffic on the adapter by issuing a |
---|
| 1962 | * global reset on the MAC and deallocates TX/RX buffers. |
---|
| 1963 | * |
---|
| 1964 | **********************************************************************/ |
---|
| 1965 | |
---|
| 1966 | static void |
---|
| 1967 | em_stop(void *arg) |
---|
| 1968 | { |
---|
| 1969 | struct ifnet *ifp; |
---|
| 1970 | struct adapter * adapter = arg; |
---|
| 1971 | ifp = &adapter->arpcom.ac_if; |
---|
| 1972 | |
---|
| 1973 | mtx_assert(&adapter->mtx, MA_OWNED); |
---|
| 1974 | |
---|
| 1975 | INIT_DEBUGOUT("em_stop: begin"); |
---|
| 1976 | #ifdef DEVICE_POLLING |
---|
| 1977 | ether_poll_deregister(ifp); |
---|
| 1978 | #endif |
---|
| 1979 | em_disable_intr(adapter); |
---|
| 1980 | em_reset_hw(&adapter->hw); |
---|
| 1981 | callout_stop(&adapter->timer); |
---|
| 1982 | callout_stop(&adapter->tx_fifo_timer); |
---|
| 1983 | em_free_transmit_structures(adapter); |
---|
| 1984 | em_free_receive_structures(adapter); |
---|
| 1985 | |
---|
| 1986 | |
---|
| 1987 | /* Tell the stack that the interface is no longer active */ |
---|
| 1988 | ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); |
---|
| 1989 | |
---|
| 1990 | return; |
---|
| 1991 | } |
---|
| 1992 | |
---|
| 1993 | |
---|
| 1994 | /********************************************************************* |
---|
| 1995 | * |
---|
| 1996 | * Determine hardware revision. |
---|
| 1997 | * |
---|
| 1998 | **********************************************************************/ |
---|
| 1999 | static void |
---|
| 2000 | em_identify_hardware(struct adapter * adapter) |
---|
| 2001 | { |
---|
| 2002 | device_t dev = adapter->dev; |
---|
| 2003 | |
---|
| 2004 | /* Make sure our PCI config space has the necessary stuff set */ |
---|
| 2005 | adapter->hw.pci_cmd_word = pci_read_config(dev, PCIR_COMMAND, 2); |
---|
| 2006 | if (!((adapter->hw.pci_cmd_word & PCIM_CMD_BUSMASTEREN) && |
---|
| 2007 | (adapter->hw.pci_cmd_word & PCIM_CMD_MEMEN))) { |
---|
| 2008 | printf("em%d: Memory Access and/or Bus Master bits were not set!\n", |
---|
| 2009 | adapter->unit); |
---|
| 2010 | adapter->hw.pci_cmd_word |= |
---|
| 2011 | (PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN); |
---|
| 2012 | pci_write_config(dev, PCIR_COMMAND, adapter->hw.pci_cmd_word, 2); |
---|
| 2013 | } |
---|
| 2014 | |
---|
| 2015 | /* Save off the information about this board */ |
---|
| 2016 | adapter->hw.vendor_id = pci_get_vendor(dev); |
---|
| 2017 | adapter->hw.device_id = pci_get_device(dev); |
---|
| 2018 | adapter->hw.revision_id = pci_read_config(dev, PCIR_REVID, 1); |
---|
| 2019 | adapter->hw.subsystem_vendor_id = pci_read_config(dev, PCIR_SUBVEND_0, 2); |
---|
| 2020 | adapter->hw.subsystem_id = pci_read_config(dev, PCIR_SUBDEV_0, 2); |
---|
| 2021 | |
---|
| 2022 | /* Identify the MAC */ |
---|
| 2023 | if (em_set_mac_type(&adapter->hw)) |
---|
| 2024 | printf("em%d: Unknown MAC Type\n", adapter->unit); |
---|
| 2025 | |
---|
| 2026 | if(adapter->hw.mac_type == em_82541 || |
---|
| 2027 | adapter->hw.mac_type == em_82541_rev_2 || |
---|
| 2028 | adapter->hw.mac_type == em_82547 || |
---|
| 2029 | adapter->hw.mac_type == em_82547_rev_2) |
---|
| 2030 | adapter->hw.phy_init_script = TRUE; |
---|
| 2031 | |
---|
| 2032 | return; |
---|
| 2033 | } |
---|
| 2034 | |
---|
| 2035 | static int |
---|
| 2036 | em_allocate_pci_resources(struct adapter * adapter) |
---|
| 2037 | { |
---|
| 2038 | int i, val, rid; |
---|
| 2039 | device_t dev = adapter->dev; |
---|
| 2040 | |
---|
| 2041 | rid = EM_MMBA; |
---|
| 2042 | |
---|
| 2043 | #ifndef __rtems__ |
---|
| 2044 | adapter->res_memory = bus_alloc_resource_any(dev, SYS_RES_MEMORY, |
---|
| 2045 | &rid, RF_ACTIVE); |
---|
| 2046 | if (!(adapter->res_memory)) { |
---|
| 2047 | printf("em%d: Unable to allocate bus resource: memory\n", |
---|
| 2048 | adapter->unit); |
---|
| 2049 | return(ENXIO); |
---|
| 2050 | } |
---|
| 2051 | adapter->osdep.mem_bus_space_tag = |
---|
| 2052 | rman_get_bustag(adapter->res_memory); |
---|
| 2053 | adapter->osdep.mem_bus_space_handle = |
---|
| 2054 | rman_get_bushandle(adapter->res_memory); |
---|
| 2055 | #endif |
---|
| 2056 | |
---|
| 2057 | adapter->hw.hw_addr = (uint8_t *)&adapter->osdep.mem_bus_space_handle; |
---|
| 2058 | |
---|
| 2059 | |
---|
| 2060 | if (adapter->hw.mac_type > em_82543) { |
---|
| 2061 | /* Figure our where our IO BAR is ? */ |
---|
| 2062 | rid = EM_MMBA; |
---|
| 2063 | for (i = 0; i < 5; i++) { |
---|
| 2064 | val = pci_read_config(dev, rid, 4); |
---|
| 2065 | if (val & 0x00000001) { |
---|
| 2066 | #ifndef __rtems__ |
---|
| 2067 | adapter->io_rid = rid; |
---|
| 2068 | #endif |
---|
| 2069 | break; |
---|
| 2070 | } |
---|
| 2071 | rid += 4; |
---|
| 2072 | } |
---|
| 2073 | |
---|
| 2074 | #ifndef __rtems__ |
---|
| 2075 | adapter->res_ioport = bus_alloc_resource_any(dev, |
---|
| 2076 | SYS_RES_IOPORT, |
---|
| 2077 | &adapter->io_rid, |
---|
| 2078 | RF_ACTIVE); |
---|
| 2079 | if (!(adapter->res_ioport)) { |
---|
| 2080 | printf("em%d: Unable to allocate bus resource: ioport\n", |
---|
| 2081 | adapter->unit); |
---|
| 2082 | return(ENXIO); |
---|
| 2083 | } |
---|
| 2084 | |
---|
| 2085 | adapter->hw.io_base = |
---|
| 2086 | rman_get_start(adapter->res_ioport); |
---|
| 2087 | #else |
---|
| 2088 | adapter->hw.io_base = val & PCI_BASE_ADDRESS_IO_MASK; |
---|
| 2089 | #endif |
---|
| 2090 | } |
---|
| 2091 | |
---|
| 2092 | #ifndef __rtems__ |
---|
| 2093 | rid = 0x0; |
---|
| 2094 | adapter->res_interrupt = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, |
---|
| 2095 | RF_SHAREABLE | |
---|
| 2096 | RF_ACTIVE); |
---|
| 2097 | if (!(adapter->res_interrupt)) { |
---|
| 2098 | printf("em%d: Unable to allocate bus resource: interrupt\n", |
---|
| 2099 | adapter->unit); |
---|
| 2100 | return(ENXIO); |
---|
| 2101 | } |
---|
| 2102 | if (bus_setup_intr(dev, adapter->res_interrupt, |
---|
| 2103 | INTR_TYPE_NET | INTR_MPSAFE, |
---|
| 2104 | (void (*)(void *)) em_intr, adapter, |
---|
| 2105 | &adapter->int_handler_tag)) { |
---|
| 2106 | printf("em%d: Error registering interrupt handler!\n", |
---|
| 2107 | adapter->unit); |
---|
| 2108 | return(ENXIO); |
---|
| 2109 | } |
---|
| 2110 | #endif |
---|
| 2111 | |
---|
| 2112 | adapter->hw.back = &adapter->osdep; |
---|
| 2113 | |
---|
| 2114 | return(0); |
---|
| 2115 | } |
---|
| 2116 | |
---|
| 2117 | #ifndef __rtems__ |
---|
| 2118 | static void |
---|
| 2119 | em_free_pci_resources(struct adapter * adapter) |
---|
| 2120 | { |
---|
| 2121 | device_t dev = adapter->dev; |
---|
| 2122 | |
---|
| 2123 | if (adapter->res_interrupt != NULL) { |
---|
| 2124 | bus_teardown_intr(dev, adapter->res_interrupt, |
---|
| 2125 | adapter->int_handler_tag); |
---|
| 2126 | bus_release_resource(dev, SYS_RES_IRQ, 0, |
---|
| 2127 | adapter->res_interrupt); |
---|
| 2128 | } |
---|
| 2129 | if (adapter->res_memory != NULL) { |
---|
| 2130 | bus_release_resource(dev, SYS_RES_MEMORY, EM_MMBA, |
---|
| 2131 | adapter->res_memory); |
---|
| 2132 | } |
---|
| 2133 | |
---|
| 2134 | if (adapter->res_ioport != NULL) { |
---|
| 2135 | bus_release_resource(dev, SYS_RES_IOPORT, adapter->io_rid, |
---|
| 2136 | adapter->res_ioport); |
---|
| 2137 | } |
---|
| 2138 | return; |
---|
| 2139 | } |
---|
| 2140 | #endif |
---|
| 2141 | |
---|
| 2142 | /********************************************************************* |
---|
| 2143 | * |
---|
| 2144 | * Initialize the hardware to a configuration as specified by the |
---|
| 2145 | * adapter structure. The controller is reset, the EEPROM is |
---|
| 2146 | * verified, the MAC address is set, then the shared initialization |
---|
| 2147 | * routines are called. |
---|
| 2148 | * |
---|
| 2149 | **********************************************************************/ |
---|
| 2150 | static int |
---|
| 2151 | em_hardware_init(struct adapter * adapter) |
---|
| 2152 | { |
---|
| 2153 | INIT_DEBUGOUT("em_hardware_init: begin"); |
---|
| 2154 | /* Issue a global reset */ |
---|
| 2155 | em_reset_hw(&adapter->hw); |
---|
| 2156 | |
---|
| 2157 | /* When hardware is reset, fifo_head is also reset */ |
---|
| 2158 | adapter->tx_fifo_head = 0; |
---|
| 2159 | |
---|
| 2160 | /* Make sure we have a good EEPROM before we read from it */ |
---|
| 2161 | if (em_validate_eeprom_checksum(&adapter->hw) < 0) { |
---|
| 2162 | printf("em%d: The EEPROM Checksum Is Not Valid\n", |
---|
| 2163 | adapter->unit); |
---|
| 2164 | return(EIO); |
---|
| 2165 | } |
---|
| 2166 | |
---|
| 2167 | if (em_read_part_num(&adapter->hw, &(adapter->part_num)) < 0) { |
---|
| 2168 | printf("em%d: EEPROM read error while reading part number\n", |
---|
| 2169 | adapter->unit); |
---|
| 2170 | return(EIO); |
---|
| 2171 | } |
---|
| 2172 | |
---|
| 2173 | if (em_init_hw(&adapter->hw) < 0) { |
---|
| 2174 | printf("em%d: Hardware Initialization Failed", |
---|
| 2175 | adapter->unit); |
---|
| 2176 | return(EIO); |
---|
| 2177 | } |
---|
| 2178 | |
---|
| 2179 | em_check_for_link(&adapter->hw); |
---|
| 2180 | if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU) |
---|
| 2181 | adapter->link_active = 1; |
---|
| 2182 | else |
---|
| 2183 | adapter->link_active = 0; |
---|
| 2184 | |
---|
| 2185 | if (adapter->link_active) { |
---|
| 2186 | em_get_speed_and_duplex(&adapter->hw, |
---|
| 2187 | &adapter->link_speed, |
---|
| 2188 | &adapter->link_duplex); |
---|
| 2189 | } else { |
---|
| 2190 | adapter->link_speed = 0; |
---|
| 2191 | adapter->link_duplex = 0; |
---|
| 2192 | } |
---|
| 2193 | |
---|
| 2194 | return(0); |
---|
| 2195 | } |
---|
| 2196 | |
---|
| 2197 | /********************************************************************* |
---|
| 2198 | * |
---|
| 2199 | * Setup networking device structure and register an interface. |
---|
| 2200 | * |
---|
| 2201 | **********************************************************************/ |
---|
| 2202 | static void |
---|
| 2203 | em_setup_interface(device_t dev, struct adapter * adapter) |
---|
| 2204 | { |
---|
| 2205 | struct ifnet *ifp = &device_get_softc(dev)->arpcom.ac_if; |
---|
| 2206 | INIT_DEBUGOUT("em_setup_interface: begin"); |
---|
| 2207 | |
---|
| 2208 | if_initname(ifp, device_get_name(dev), device_get_unit(dev)); |
---|
| 2209 | ifp->if_mtu = ETHERMTU; |
---|
| 2210 | ifp->if_baudrate = 1000000000; |
---|
| 2211 | ifp->if_init = em_init; |
---|
| 2212 | ifp->if_softc = adapter; |
---|
| 2213 | ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; |
---|
| 2214 | #ifdef __rtems__ |
---|
| 2215 | ifp->if_output = ether_output; |
---|
| 2216 | #endif |
---|
| 2217 | ifp->if_ioctl = em_ioctl; |
---|
| 2218 | ifp->if_start = em_start; |
---|
| 2219 | ifp->if_watchdog = em_watchdog; |
---|
| 2220 | #ifndef __rtems__ |
---|
| 2221 | IFQ_SET_MAXLEN(&ifp->if_snd, adapter->num_tx_desc - 1); |
---|
| 2222 | ifp->if_snd.ifq_drv_maxlen = adapter->num_tx_desc - 1; |
---|
| 2223 | IFQ_SET_READY(&ifp->if_snd); |
---|
| 2224 | #else |
---|
| 2225 | ifp->if_snd.ifq_maxlen = adapter->num_tx_desc - 1; |
---|
| 2226 | #endif |
---|
| 2227 | |
---|
| 2228 | #ifndef __rtems__ |
---|
| 2229 | #if __FreeBSD_version < 500000 |
---|
| 2230 | ether_ifattach(ifp, ETHER_BPF_SUPPORTED); |
---|
| 2231 | #else |
---|
| 2232 | ether_ifattach(ifp, adapter->hw.mac_addr); |
---|
| 2233 | #endif |
---|
| 2234 | #else |
---|
| 2235 | if ( !ifp->if_addrlist ) /* reattach hack */ |
---|
| 2236 | { |
---|
| 2237 | if_attach(ifp); |
---|
| 2238 | ether_ifattach(ifp); |
---|
| 2239 | } |
---|
| 2240 | #endif |
---|
| 2241 | |
---|
| 2242 | #ifndef __rtems__ |
---|
| 2243 | ifp->if_capabilities = ifp->if_capenable = 0; |
---|
| 2244 | |
---|
| 2245 | if (adapter->hw.mac_type >= em_82543) { |
---|
| 2246 | ifp->if_capabilities |= IFCAP_HWCSUM; |
---|
| 2247 | ifp->if_capenable |= IFCAP_HWCSUM; |
---|
| 2248 | } |
---|
| 2249 | |
---|
| 2250 | /* |
---|
| 2251 | * Tell the upper layer(s) we support long frames. |
---|
| 2252 | */ |
---|
| 2253 | ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header); |
---|
| 2254 | #if __FreeBSD_version >= 500000 |
---|
| 2255 | ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU; |
---|
| 2256 | ifp->if_capenable |= IFCAP_VLAN_MTU; |
---|
| 2257 | #endif |
---|
| 2258 | |
---|
| 2259 | #ifdef DEVICE_POLLING |
---|
| 2260 | ifp->if_capabilities |= IFCAP_POLLING; |
---|
| 2261 | ifp->if_capenable |= IFCAP_POLLING; |
---|
| 2262 | #endif |
---|
| 2263 | |
---|
| 2264 | /* |
---|
| 2265 | * Specify the media types supported by this adapter and register |
---|
| 2266 | * callbacks to update media and link information |
---|
| 2267 | */ |
---|
| 2268 | ifmedia_init(&adapter->media, IFM_IMASK, em_media_change, |
---|
| 2269 | em_media_status); |
---|
| 2270 | if (adapter->hw.media_type == em_media_type_fiber) { |
---|
| 2271 | ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_SX | IFM_FDX, |
---|
| 2272 | 0, NULL); |
---|
| 2273 | ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_SX, |
---|
| 2274 | 0, NULL); |
---|
| 2275 | } else { |
---|
| 2276 | ifmedia_add(&adapter->media, IFM_ETHER | IFM_10_T, 0, NULL); |
---|
| 2277 | ifmedia_add(&adapter->media, IFM_ETHER | IFM_10_T | IFM_FDX, |
---|
| 2278 | 0, NULL); |
---|
| 2279 | ifmedia_add(&adapter->media, IFM_ETHER | IFM_100_TX, |
---|
| 2280 | 0, NULL); |
---|
| 2281 | ifmedia_add(&adapter->media, IFM_ETHER | IFM_100_TX | IFM_FDX, |
---|
| 2282 | 0, NULL); |
---|
| 2283 | #if __FreeBSD_version < 500000 |
---|
| 2284 | ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_TX | IFM_FDX, |
---|
| 2285 | 0, NULL); |
---|
| 2286 | ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_TX, 0, NULL); |
---|
| 2287 | #else |
---|
| 2288 | ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_T | IFM_FDX, |
---|
| 2289 | 0, NULL); |
---|
| 2290 | ifmedia_add(&adapter->media, IFM_ETHER | IFM_1000_T, 0, NULL); |
---|
| 2291 | #endif |
---|
| 2292 | } |
---|
| 2293 | ifmedia_add(&adapter->media, IFM_ETHER | IFM_AUTO, 0, NULL); |
---|
| 2294 | ifmedia_set(&adapter->media, IFM_ETHER | IFM_AUTO); |
---|
| 2295 | #endif |
---|
| 2296 | |
---|
| 2297 | return; |
---|
| 2298 | } |
---|
| 2299 | |
---|
| 2300 | #ifndef __rtems__ |
---|
| 2301 | /********************************************************************* |
---|
| 2302 | * |
---|
| 2303 | * Workaround for SmartSpeed on 82541 and 82547 controllers |
---|
| 2304 | * |
---|
| 2305 | **********************************************************************/ |
---|
| 2306 | static void |
---|
| 2307 | em_smartspeed(struct adapter *adapter) |
---|
| 2308 | { |
---|
| 2309 | uint16_t phy_tmp; |
---|
| 2310 | |
---|
| 2311 | if(adapter->link_active || (adapter->hw.phy_type != em_phy_igp) || |
---|
| 2312 | !adapter->hw.autoneg || !(adapter->hw.autoneg_advertised & ADVERTISE_1000_FULL)) |
---|
| 2313 | return; |
---|
| 2314 | |
---|
| 2315 | if(adapter->smartspeed == 0) { |
---|
| 2316 | /* If Master/Slave config fault is asserted twice, |
---|
| 2317 | * we assume back-to-back */ |
---|
| 2318 | em_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_tmp); |
---|
| 2319 | if(!(phy_tmp & SR_1000T_MS_CONFIG_FAULT)) return; |
---|
| 2320 | em_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_tmp); |
---|
| 2321 | if(phy_tmp & SR_1000T_MS_CONFIG_FAULT) { |
---|
| 2322 | em_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, |
---|
| 2323 | &phy_tmp); |
---|
| 2324 | if(phy_tmp & CR_1000T_MS_ENABLE) { |
---|
| 2325 | phy_tmp &= ~CR_1000T_MS_ENABLE; |
---|
| 2326 | em_write_phy_reg(&adapter->hw, |
---|
| 2327 | PHY_1000T_CTRL, phy_tmp); |
---|
| 2328 | adapter->smartspeed++; |
---|
| 2329 | if(adapter->hw.autoneg && |
---|
| 2330 | !em_phy_setup_autoneg(&adapter->hw) && |
---|
| 2331 | !em_read_phy_reg(&adapter->hw, PHY_CTRL, |
---|
| 2332 | &phy_tmp)) { |
---|
| 2333 | phy_tmp |= (MII_CR_AUTO_NEG_EN | |
---|
| 2334 | MII_CR_RESTART_AUTO_NEG); |
---|
| 2335 | em_write_phy_reg(&adapter->hw, |
---|
| 2336 | PHY_CTRL, phy_tmp); |
---|
| 2337 | } |
---|
| 2338 | } |
---|
| 2339 | } |
---|
| 2340 | return; |
---|
| 2341 | } else if(adapter->smartspeed == EM_SMARTSPEED_DOWNSHIFT) { |
---|
| 2342 | /* If still no link, perhaps using 2/3 pair cable */ |
---|
| 2343 | em_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_tmp); |
---|
| 2344 | phy_tmp |= CR_1000T_MS_ENABLE; |
---|
| 2345 | em_write_phy_reg(&adapter->hw, PHY_1000T_CTRL, phy_tmp); |
---|
| 2346 | if(adapter->hw.autoneg && |
---|
| 2347 | !em_phy_setup_autoneg(&adapter->hw) && |
---|
| 2348 | !em_read_phy_reg(&adapter->hw, PHY_CTRL, &phy_tmp)) { |
---|
| 2349 | phy_tmp |= (MII_CR_AUTO_NEG_EN | |
---|
| 2350 | MII_CR_RESTART_AUTO_NEG); |
---|
| 2351 | em_write_phy_reg(&adapter->hw, PHY_CTRL, phy_tmp); |
---|
| 2352 | } |
---|
| 2353 | } |
---|
| 2354 | /* Restart process after EM_SMARTSPEED_MAX iterations */ |
---|
| 2355 | if(adapter->smartspeed++ == EM_SMARTSPEED_MAX) |
---|
| 2356 | adapter->smartspeed = 0; |
---|
| 2357 | |
---|
| 2358 | return; |
---|
| 2359 | } |
---|
| 2360 | |
---|
| 2361 | |
---|
| 2362 | /* |
---|
| 2363 | * Manage DMA'able memory. |
---|
| 2364 | */ |
---|
| 2365 | static void |
---|
| 2366 | em_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) |
---|
| 2367 | { |
---|
| 2368 | if (error) |
---|
| 2369 | return; |
---|
| 2370 | *(bus_addr_t*) arg = segs->ds_addr; |
---|
| 2371 | return; |
---|
| 2372 | } |
---|
| 2373 | #endif |
---|
| 2374 | |
---|
| 2375 | static int |
---|
| 2376 | em_dma_malloc(struct adapter *adapter, bus_size_t size, |
---|
| 2377 | struct em_dma_alloc *dma, int mapflags) |
---|
| 2378 | { |
---|
| 2379 | int r; |
---|
| 2380 | |
---|
| 2381 | #ifndef __rtems__ |
---|
| 2382 | r = bus_dma_tag_create(NULL, /* parent */ |
---|
| 2383 | PAGE_SIZE, 0, /* alignment, bounds */ |
---|
| 2384 | BUS_SPACE_MAXADDR, /* lowaddr */ |
---|
| 2385 | BUS_SPACE_MAXADDR, /* highaddr */ |
---|
| 2386 | NULL, NULL, /* filter, filterarg */ |
---|
| 2387 | size, /* maxsize */ |
---|
| 2388 | 1, /* nsegments */ |
---|
| 2389 | size, /* maxsegsize */ |
---|
| 2390 | BUS_DMA_ALLOCNOW, /* flags */ |
---|
| 2391 | NULL, /* lockfunc */ |
---|
| 2392 | NULL, /* lockarg */ |
---|
| 2393 | &dma->dma_tag); |
---|
| 2394 | if (r != 0) { |
---|
| 2395 | printf("em%d: em_dma_malloc: bus_dma_tag_create failed; " |
---|
| 2396 | "error %u\n", adapter->unit, r); |
---|
| 2397 | goto fail_0; |
---|
| 2398 | } |
---|
| 2399 | |
---|
| 2400 | r = bus_dmamem_alloc(dma->dma_tag, (void**) &dma->dma_vaddr, |
---|
| 2401 | BUS_DMA_NOWAIT, &dma->dma_map); |
---|
| 2402 | #else |
---|
| 2403 | if ( (dma->malloc_base = malloc( size + PAGE_SIZE, M_DEVBUF, M_NOWAIT )) ) { |
---|
| 2404 | r = 0; |
---|
| 2405 | dma->dma_vaddr = (caddr_t)_DO_ALIGN(dma->malloc_base, PAGE_SIZE); |
---|
| 2406 | } else { |
---|
| 2407 | r = -1; |
---|
| 2408 | } |
---|
| 2409 | #endif |
---|
| 2410 | if (r != 0) { |
---|
| 2411 | #ifndef __rtems__ |
---|
| 2412 | printf("em%d: em_dma_malloc: bus_dmammem_alloc failed; " |
---|
| 2413 | "size %ju, error %d\n", adapter->unit, |
---|
| 2414 | (uintmax_t)size, r); |
---|
| 2415 | #else |
---|
| 2416 | printf("em%d: em_dma_malloc: bus_dmammem_alloc failed; " |
---|
| 2417 | "size %u, error %d\n", adapter->unit, |
---|
| 2418 | size, r); |
---|
| 2419 | #endif |
---|
| 2420 | goto fail_2; |
---|
| 2421 | } |
---|
| 2422 | |
---|
| 2423 | #ifndef __rtems__ |
---|
| 2424 | r = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr, |
---|
| 2425 | size, |
---|
| 2426 | em_dmamap_cb, |
---|
| 2427 | &dma->dma_paddr, |
---|
| 2428 | mapflags | BUS_DMA_NOWAIT); |
---|
| 2429 | #else |
---|
| 2430 | dma->dma_paddr = kvtop(dma->dma_vaddr); |
---|
| 2431 | #endif |
---|
| 2432 | if (r != 0) { |
---|
| 2433 | printf("em%d: em_dma_malloc: bus_dmamap_load failed; " |
---|
| 2434 | "error %u\n", adapter->unit, r); |
---|
| 2435 | goto fail_3; |
---|
| 2436 | } |
---|
| 2437 | |
---|
| 2438 | #ifndef __rtems__ |
---|
| 2439 | dma->dma_size = size; |
---|
| 2440 | #endif |
---|
| 2441 | return (0); |
---|
| 2442 | |
---|
| 2443 | fail_3: |
---|
| 2444 | bus_dmamap_unload(dma->dma_tag, dma->dma_map); |
---|
| 2445 | fail_2: |
---|
| 2446 | #ifndef __rtems__ |
---|
| 2447 | bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map); |
---|
| 2448 | #else |
---|
| 2449 | free(dma->malloc_base, M_DEVBUF); |
---|
| 2450 | dma->dma_vaddr = dma->malloc_base = 0; |
---|
| 2451 | dma->dma_paddr = 0; |
---|
| 2452 | #endif |
---|
| 2453 | bus_dma_tag_destroy(dma->dma_tag); |
---|
| 2454 | #ifndef __rtems__ |
---|
| 2455 | fail_0: |
---|
| 2456 | dma->dma_map = NULL; |
---|
| 2457 | dma->dma_tag = NULL; |
---|
| 2458 | #endif |
---|
| 2459 | return (r); |
---|
| 2460 | } |
---|
| 2461 | |
---|
| 2462 | static void |
---|
| 2463 | em_dma_free(struct adapter *adapter, struct em_dma_alloc *dma) |
---|
| 2464 | { |
---|
| 2465 | bus_dmamap_unload(dma->dma_tag, dma->dma_map); |
---|
| 2466 | #ifndef __rtems__ |
---|
| 2467 | bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map); |
---|
| 2468 | #else |
---|
| 2469 | free(dma->malloc_base, M_DEVBUF); |
---|
| 2470 | dma->dma_vaddr = dma->malloc_base = 0; |
---|
| 2471 | dma->dma_paddr = 0; |
---|
| 2472 | #endif |
---|
| 2473 | bus_dma_tag_destroy(dma->dma_tag); |
---|
| 2474 | } |
---|
| 2475 | |
---|
| 2476 | |
---|
| 2477 | /********************************************************************* |
---|
| 2478 | * |
---|
| 2479 | * Allocate memory for tx_buffer structures. The tx_buffer stores all |
---|
| 2480 | * the information needed to transmit a packet on the wire. |
---|
| 2481 | * |
---|
| 2482 | **********************************************************************/ |
---|
| 2483 | static int |
---|
| 2484 | em_allocate_transmit_structures(struct adapter * adapter) |
---|
| 2485 | { |
---|
| 2486 | if (!(adapter->tx_buffer_area = |
---|
| 2487 | (struct em_buffer *) malloc(sizeof(struct em_buffer) * |
---|
| 2488 | adapter->num_tx_desc, M_DEVBUF, |
---|
| 2489 | M_NOWAIT))) { |
---|
| 2490 | printf("em%d: Unable to allocate tx_buffer memory\n", |
---|
| 2491 | adapter->unit); |
---|
| 2492 | return ENOMEM; |
---|
| 2493 | } |
---|
| 2494 | |
---|
| 2495 | bzero(adapter->tx_buffer_area, |
---|
| 2496 | sizeof(struct em_buffer) * adapter->num_tx_desc); |
---|
| 2497 | |
---|
| 2498 | return 0; |
---|
| 2499 | } |
---|
| 2500 | |
---|
| 2501 | /********************************************************************* |
---|
| 2502 | * |
---|
| 2503 | * Allocate and initialize transmit structures. |
---|
| 2504 | * |
---|
| 2505 | **********************************************************************/ |
---|
| 2506 | static int |
---|
| 2507 | em_setup_transmit_structures(struct adapter * adapter) |
---|
| 2508 | { |
---|
| 2509 | #ifndef __rtems__ |
---|
| 2510 | /* |
---|
| 2511 | * Setup DMA descriptor areas. |
---|
| 2512 | */ |
---|
| 2513 | if (bus_dma_tag_create(NULL, /* parent */ |
---|
| 2514 | 1, 0, /* alignment, bounds */ |
---|
| 2515 | BUS_SPACE_MAXADDR, /* lowaddr */ |
---|
| 2516 | BUS_SPACE_MAXADDR, /* highaddr */ |
---|
| 2517 | NULL, NULL, /* filter, filterarg */ |
---|
| 2518 | MCLBYTES * 8, /* maxsize */ |
---|
| 2519 | EM_MAX_SCATTER, /* nsegments */ |
---|
| 2520 | MCLBYTES * 8, /* maxsegsize */ |
---|
| 2521 | BUS_DMA_ALLOCNOW, /* flags */ |
---|
| 2522 | NULL, /* lockfunc */ |
---|
| 2523 | NULL, /* lockarg */ |
---|
| 2524 | &adapter->txtag)) { |
---|
| 2525 | printf("em%d: Unable to allocate TX DMA tag\n", adapter->unit); |
---|
| 2526 | return (ENOMEM); |
---|
| 2527 | } |
---|
| 2528 | #endif |
---|
| 2529 | |
---|
| 2530 | if (em_allocate_transmit_structures(adapter)) |
---|
| 2531 | return (ENOMEM); |
---|
| 2532 | |
---|
| 2533 | bzero((void *) adapter->tx_desc_base, |
---|
| 2534 | (sizeof(struct em_tx_desc)) * adapter->num_tx_desc); |
---|
| 2535 | |
---|
| 2536 | adapter->next_avail_tx_desc = 0; |
---|
| 2537 | adapter->oldest_used_tx_desc = 0; |
---|
| 2538 | |
---|
| 2539 | /* Set number of descriptors available */ |
---|
| 2540 | adapter->num_tx_desc_avail = adapter->num_tx_desc; |
---|
| 2541 | |
---|
| 2542 | /* Set checksum context */ |
---|
| 2543 | adapter->active_checksum_context = OFFLOAD_NONE; |
---|
| 2544 | |
---|
| 2545 | return (0); |
---|
| 2546 | } |
---|
| 2547 | |
---|
| 2548 | /********************************************************************* |
---|
| 2549 | * |
---|
| 2550 | * Enable transmit unit. |
---|
| 2551 | * |
---|
| 2552 | **********************************************************************/ |
---|
| 2553 | static void |
---|
| 2554 | em_initialize_transmit_unit(struct adapter * adapter) |
---|
| 2555 | { |
---|
| 2556 | u_int32_t reg_tctl; |
---|
| 2557 | u_int32_t reg_tipg = 0; |
---|
| 2558 | u_int64_t bus_addr; |
---|
| 2559 | |
---|
| 2560 | INIT_DEBUGOUT("em_initialize_transmit_unit: begin"); |
---|
| 2561 | /* Setup the Base and Length of the Tx Descriptor Ring */ |
---|
| 2562 | bus_addr = adapter->txdma.dma_paddr; |
---|
| 2563 | E1000_WRITE_REG(&adapter->hw, TDBAL, (u_int32_t)bus_addr); |
---|
| 2564 | E1000_WRITE_REG(&adapter->hw, TDBAH, (u_int32_t)(bus_addr >> 32)); |
---|
| 2565 | E1000_WRITE_REG(&adapter->hw, TDLEN, |
---|
| 2566 | adapter->num_tx_desc * |
---|
| 2567 | sizeof(struct em_tx_desc)); |
---|
| 2568 | |
---|
| 2569 | /* Setup the HW Tx Head and Tail descriptor pointers */ |
---|
| 2570 | E1000_WRITE_REG(&adapter->hw, TDH, 0); |
---|
| 2571 | E1000_WRITE_REG(&adapter->hw, TDT, 0); |
---|
| 2572 | |
---|
| 2573 | |
---|
| 2574 | HW_DEBUGOUT2("Base = %x, Length = %x\n", |
---|
| 2575 | E1000_READ_REG(&adapter->hw, TDBAL), |
---|
| 2576 | E1000_READ_REG(&adapter->hw, TDLEN)); |
---|
| 2577 | |
---|
| 2578 | /* Set the default values for the Tx Inter Packet Gap timer */ |
---|
| 2579 | switch (adapter->hw.mac_type) { |
---|
| 2580 | case em_82542_rev2_0: |
---|
| 2581 | case em_82542_rev2_1: |
---|
| 2582 | reg_tipg = DEFAULT_82542_TIPG_IPGT; |
---|
| 2583 | reg_tipg |= DEFAULT_82542_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT; |
---|
| 2584 | reg_tipg |= DEFAULT_82542_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT; |
---|
| 2585 | break; |
---|
| 2586 | default: |
---|
| 2587 | if (adapter->hw.media_type == em_media_type_fiber) |
---|
| 2588 | reg_tipg = DEFAULT_82543_TIPG_IPGT_FIBER; |
---|
| 2589 | else |
---|
| 2590 | reg_tipg = DEFAULT_82543_TIPG_IPGT_COPPER; |
---|
| 2591 | reg_tipg |= DEFAULT_82543_TIPG_IPGR1 << E1000_TIPG_IPGR1_SHIFT; |
---|
| 2592 | reg_tipg |= DEFAULT_82543_TIPG_IPGR2 << E1000_TIPG_IPGR2_SHIFT; |
---|
| 2593 | } |
---|
| 2594 | |
---|
| 2595 | E1000_WRITE_REG(&adapter->hw, TIPG, reg_tipg); |
---|
| 2596 | E1000_WRITE_REG(&adapter->hw, TIDV, adapter->tx_int_delay.value); |
---|
| 2597 | if(adapter->hw.mac_type >= em_82540) |
---|
| 2598 | E1000_WRITE_REG(&adapter->hw, TADV, |
---|
| 2599 | adapter->tx_abs_int_delay.value); |
---|
| 2600 | |
---|
| 2601 | /* Program the Transmit Control Register */ |
---|
| 2602 | reg_tctl = E1000_TCTL_PSP | E1000_TCTL_EN | |
---|
| 2603 | (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT); |
---|
| 2604 | if (adapter->hw.mac_type >= em_82573) |
---|
| 2605 | reg_tctl |= E1000_TCTL_MULR; |
---|
| 2606 | if (adapter->link_duplex == 1) { |
---|
| 2607 | reg_tctl |= E1000_FDX_COLLISION_DISTANCE << E1000_COLD_SHIFT; |
---|
| 2608 | } else { |
---|
| 2609 | reg_tctl |= E1000_HDX_COLLISION_DISTANCE << E1000_COLD_SHIFT; |
---|
| 2610 | } |
---|
| 2611 | E1000_WRITE_REG(&adapter->hw, TCTL, reg_tctl); |
---|
| 2612 | |
---|
| 2613 | /* Setup Transmit Descriptor Settings for this adapter */ |
---|
| 2614 | adapter->txd_cmd = E1000_TXD_CMD_IFCS | E1000_TXD_CMD_RS; |
---|
| 2615 | |
---|
| 2616 | if (adapter->tx_int_delay.value > 0) |
---|
| 2617 | adapter->txd_cmd |= E1000_TXD_CMD_IDE; |
---|
| 2618 | |
---|
| 2619 | return; |
---|
| 2620 | } |
---|
| 2621 | |
---|
| 2622 | /********************************************************************* |
---|
| 2623 | * |
---|
| 2624 | * Free all transmit related data structures. |
---|
| 2625 | * |
---|
| 2626 | **********************************************************************/ |
---|
| 2627 | static void |
---|
| 2628 | em_free_transmit_structures(struct adapter * adapter) |
---|
| 2629 | { |
---|
| 2630 | struct em_buffer *tx_buffer; |
---|
| 2631 | int i; |
---|
| 2632 | |
---|
| 2633 | INIT_DEBUGOUT("free_transmit_structures: begin"); |
---|
| 2634 | |
---|
| 2635 | if (adapter->tx_buffer_area != NULL) { |
---|
| 2636 | tx_buffer = adapter->tx_buffer_area; |
---|
| 2637 | for (i = 0; i < adapter->num_tx_desc; i++, tx_buffer++) { |
---|
| 2638 | if (tx_buffer->m_head != NULL) { |
---|
| 2639 | bus_dmamap_unload(adapter->txtag, tx_buffer->map); |
---|
| 2640 | bus_dmamap_destroy(adapter->txtag, tx_buffer->map); |
---|
| 2641 | m_freem(tx_buffer->m_head); |
---|
| 2642 | } |
---|
| 2643 | tx_buffer->m_head = NULL; |
---|
| 2644 | } |
---|
| 2645 | } |
---|
| 2646 | if (adapter->tx_buffer_area != NULL) { |
---|
| 2647 | free(adapter->tx_buffer_area, M_DEVBUF); |
---|
| 2648 | adapter->tx_buffer_area = NULL; |
---|
| 2649 | } |
---|
| 2650 | #ifndef __rtems__ |
---|
| 2651 | if (adapter->txtag != NULL) { |
---|
| 2652 | bus_dma_tag_destroy(adapter->txtag); |
---|
| 2653 | adapter->txtag = NULL; |
---|
| 2654 | } |
---|
| 2655 | #endif |
---|
| 2656 | return; |
---|
| 2657 | } |
---|
| 2658 | |
---|
| 2659 | #ifndef __rtems__ |
---|
| 2660 | /********************************************************************* |
---|
| 2661 | * |
---|
| 2662 | * The offload context needs to be set when we transfer the first |
---|
| 2663 | * packet of a particular protocol (TCP/UDP). We change the |
---|
| 2664 | * context only if the protocol type changes. |
---|
| 2665 | * |
---|
| 2666 | **********************************************************************/ |
---|
| 2667 | static void |
---|
| 2668 | em_transmit_checksum_setup(struct adapter * adapter, |
---|
| 2669 | struct mbuf *mp, |
---|
| 2670 | u_int32_t *txd_upper, |
---|
| 2671 | u_int32_t *txd_lower) |
---|
| 2672 | { |
---|
| 2673 | struct em_context_desc *TXD; |
---|
| 2674 | struct em_buffer *tx_buffer; |
---|
| 2675 | int curr_txd; |
---|
| 2676 | |
---|
| 2677 | if (mp->m_pkthdr.csum_flags) { |
---|
| 2678 | |
---|
| 2679 | if (mp->m_pkthdr.csum_flags & CSUM_TCP) { |
---|
| 2680 | *txd_upper = E1000_TXD_POPTS_TXSM << 8; |
---|
| 2681 | *txd_lower = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D; |
---|
| 2682 | if (adapter->active_checksum_context == OFFLOAD_TCP_IP) |
---|
| 2683 | return; |
---|
| 2684 | else |
---|
| 2685 | adapter->active_checksum_context = OFFLOAD_TCP_IP; |
---|
| 2686 | |
---|
| 2687 | } else if (mp->m_pkthdr.csum_flags & CSUM_UDP) { |
---|
| 2688 | *txd_upper = E1000_TXD_POPTS_TXSM << 8; |
---|
| 2689 | *txd_lower = E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D; |
---|
| 2690 | if (adapter->active_checksum_context == OFFLOAD_UDP_IP) |
---|
| 2691 | return; |
---|
| 2692 | else |
---|
| 2693 | adapter->active_checksum_context = OFFLOAD_UDP_IP; |
---|
| 2694 | } else { |
---|
| 2695 | *txd_upper = 0; |
---|
| 2696 | *txd_lower = 0; |
---|
| 2697 | return; |
---|
| 2698 | } |
---|
| 2699 | } else { |
---|
| 2700 | *txd_upper = 0; |
---|
| 2701 | *txd_lower = 0; |
---|
| 2702 | return; |
---|
| 2703 | } |
---|
| 2704 | |
---|
| 2705 | /* If we reach this point, the checksum offload context |
---|
| 2706 | * needs to be reset. |
---|
| 2707 | */ |
---|
| 2708 | curr_txd = adapter->next_avail_tx_desc; |
---|
| 2709 | tx_buffer = &adapter->tx_buffer_area[curr_txd]; |
---|
| 2710 | TXD = (struct em_context_desc *) &adapter->tx_desc_base[curr_txd]; |
---|
| 2711 | |
---|
| 2712 | TXD->lower_setup.ip_fields.ipcss = ETHER_HDR_LEN; |
---|
| 2713 | TXD->lower_setup.ip_fields.ipcso = |
---|
| 2714 | ETHER_HDR_LEN + offsetof(struct ip, ip_sum); |
---|
| 2715 | TXD->lower_setup.ip_fields.ipcse = |
---|
| 2716 | htole16(ETHER_HDR_LEN + sizeof(struct ip) - 1); |
---|
| 2717 | |
---|
| 2718 | TXD->upper_setup.tcp_fields.tucss = |
---|
| 2719 | ETHER_HDR_LEN + sizeof(struct ip); |
---|
| 2720 | TXD->upper_setup.tcp_fields.tucse = htole16(0); |
---|
| 2721 | |
---|
| 2722 | if (adapter->active_checksum_context == OFFLOAD_TCP_IP) { |
---|
| 2723 | TXD->upper_setup.tcp_fields.tucso = |
---|
| 2724 | ETHER_HDR_LEN + sizeof(struct ip) + |
---|
| 2725 | offsetof(struct tcphdr, th_sum); |
---|
| 2726 | } else if (adapter->active_checksum_context == OFFLOAD_UDP_IP) { |
---|
| 2727 | TXD->upper_setup.tcp_fields.tucso = |
---|
| 2728 | ETHER_HDR_LEN + sizeof(struct ip) + |
---|
| 2729 | offsetof(struct udphdr, uh_sum); |
---|
| 2730 | } |
---|
| 2731 | |
---|
| 2732 | TXD->tcp_seg_setup.data = htole32(0); |
---|
| 2733 | TXD->cmd_and_length = htole32(adapter->txd_cmd | E1000_TXD_CMD_DEXT); |
---|
| 2734 | |
---|
| 2735 | tx_buffer->m_head = NULL; |
---|
| 2736 | |
---|
| 2737 | if (++curr_txd == adapter->num_tx_desc) |
---|
| 2738 | curr_txd = 0; |
---|
| 2739 | |
---|
| 2740 | adapter->num_tx_desc_avail--; |
---|
| 2741 | adapter->next_avail_tx_desc = curr_txd; |
---|
| 2742 | |
---|
| 2743 | return; |
---|
| 2744 | } |
---|
| 2745 | #endif |
---|
| 2746 | |
---|
| 2747 | /********************************************************************** |
---|
| 2748 | * |
---|
| 2749 | * Examine each tx_buffer in the used queue. If the hardware is done |
---|
| 2750 | * processing the packet then free associated resources. The |
---|
| 2751 | * tx_buffer is put back on the free queue. |
---|
| 2752 | * |
---|
| 2753 | **********************************************************************/ |
---|
| 2754 | static void |
---|
| 2755 | em_clean_transmit_interrupts(struct adapter * adapter) |
---|
| 2756 | { |
---|
| 2757 | int i, num_avail; |
---|
| 2758 | struct em_buffer *tx_buffer; |
---|
| 2759 | struct em_tx_desc *tx_desc; |
---|
| 2760 | struct ifnet *ifp = &adapter->arpcom.ac_if; |
---|
| 2761 | |
---|
| 2762 | mtx_assert(&adapter->mtx, MA_OWNED); |
---|
| 2763 | |
---|
| 2764 | if (adapter->num_tx_desc_avail == adapter->num_tx_desc) |
---|
| 2765 | return; |
---|
| 2766 | |
---|
| 2767 | num_avail = adapter->num_tx_desc_avail; |
---|
| 2768 | i = adapter->oldest_used_tx_desc; |
---|
| 2769 | |
---|
| 2770 | tx_buffer = &adapter->tx_buffer_area[i]; |
---|
| 2771 | tx_desc = &adapter->tx_desc_base[i]; |
---|
| 2772 | |
---|
| 2773 | bus_dmamap_sync(adapter->txdma.dma_tag, adapter->txdma.dma_map, |
---|
| 2774 | BUS_DMASYNC_POSTREAD); |
---|
| 2775 | while (tx_desc->upper.fields.status & E1000_TXD_STAT_DD) { |
---|
| 2776 | |
---|
| 2777 | tx_desc->upper.data = 0; |
---|
| 2778 | num_avail++; |
---|
| 2779 | |
---|
| 2780 | if (tx_buffer->m_head) { |
---|
| 2781 | ifp->if_opackets++; |
---|
| 2782 | bus_dmamap_unload(adapter->txtag, tx_buffer->map); |
---|
| 2783 | bus_dmamap_destroy(adapter->txtag, tx_buffer->map); |
---|
| 2784 | |
---|
| 2785 | m_freem(tx_buffer->m_head); |
---|
| 2786 | tx_buffer->m_head = NULL; |
---|
| 2787 | } |
---|
| 2788 | |
---|
| 2789 | if (++i == adapter->num_tx_desc) |
---|
| 2790 | i = 0; |
---|
| 2791 | |
---|
| 2792 | tx_buffer = &adapter->tx_buffer_area[i]; |
---|
| 2793 | tx_desc = &adapter->tx_desc_base[i]; |
---|
| 2794 | } |
---|
| 2795 | bus_dmamap_sync(adapter->txdma.dma_tag, adapter->txdma.dma_map, |
---|
| 2796 | BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); |
---|
| 2797 | |
---|
| 2798 | adapter->oldest_used_tx_desc = i; |
---|
| 2799 | |
---|
| 2800 | /* |
---|
| 2801 | * If we have enough room, clear IFF_OACTIVE to tell the stack |
---|
| 2802 | * that it is OK to send packets. |
---|
| 2803 | * If there are no pending descriptors, clear the timeout. Otherwise, |
---|
| 2804 | * if some descriptors have been freed, restart the timeout. |
---|
| 2805 | */ |
---|
| 2806 | if (num_avail > EM_TX_CLEANUP_THRESHOLD) { |
---|
| 2807 | ifp->if_flags &= ~IFF_OACTIVE; |
---|
| 2808 | if (num_avail == adapter->num_tx_desc) |
---|
| 2809 | ifp->if_timer = 0; |
---|
| 2810 | else if (num_avail == adapter->num_tx_desc_avail) |
---|
| 2811 | ifp->if_timer = EM_TX_TIMEOUT; |
---|
| 2812 | } |
---|
| 2813 | adapter->num_tx_desc_avail = num_avail; |
---|
| 2814 | return; |
---|
| 2815 | } |
---|
| 2816 | |
---|
| 2817 | /********************************************************************* |
---|
| 2818 | * |
---|
| 2819 | * Get a buffer from system mbuf buffer pool. |
---|
| 2820 | * |
---|
| 2821 | **********************************************************************/ |
---|
| 2822 | static int |
---|
| 2823 | em_get_buf(int i, struct adapter *adapter, |
---|
| 2824 | struct mbuf *nmp) |
---|
| 2825 | { |
---|
| 2826 | register struct mbuf *mp = nmp; |
---|
| 2827 | struct em_buffer *rx_buffer; |
---|
| 2828 | struct ifnet *ifp; |
---|
| 2829 | bus_addr_t paddr; |
---|
| 2830 | #ifndef __rtems__ |
---|
| 2831 | int error; |
---|
| 2832 | #endif |
---|
| 2833 | |
---|
| 2834 | ifp = &adapter->arpcom.ac_if; |
---|
| 2835 | |
---|
| 2836 | if (mp == NULL) { |
---|
| 2837 | #ifndef __rtems__ |
---|
| 2838 | mp = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); |
---|
| 2839 | #else |
---|
| 2840 | MGETHDR(mp, M_DONTWAIT, MT_DATA); |
---|
| 2841 | if ( mp ) { |
---|
| 2842 | MCLGET( mp, M_DONTWAIT ); |
---|
| 2843 | if ( !(mp->m_flags & M_EXT) ) { |
---|
| 2844 | m_freem(mp); |
---|
| 2845 | mp = 0; |
---|
| 2846 | } |
---|
| 2847 | } |
---|
| 2848 | #endif |
---|
| 2849 | if (mp == NULL) { |
---|
| 2850 | adapter->mbuf_cluster_failed++; |
---|
| 2851 | return(ENOBUFS); |
---|
| 2852 | } |
---|
| 2853 | mp->m_len = mp->m_pkthdr.len = MCLBYTES; |
---|
| 2854 | } else { |
---|
| 2855 | mp->m_len = mp->m_pkthdr.len = MCLBYTES; |
---|
| 2856 | mp->m_data = mp->m_ext.ext_buf; |
---|
| 2857 | mp->m_next = NULL; |
---|
| 2858 | } |
---|
| 2859 | |
---|
| 2860 | if (ifp->if_mtu <= ETHERMTU) { |
---|
| 2861 | m_adj(mp, ETHER_ALIGN); |
---|
| 2862 | } |
---|
| 2863 | |
---|
| 2864 | rx_buffer = &adapter->rx_buffer_area[i]; |
---|
| 2865 | |
---|
| 2866 | #ifndef __rtems__ |
---|
| 2867 | /* |
---|
| 2868 | * Using memory from the mbuf cluster pool, invoke the |
---|
| 2869 | * bus_dma machinery to arrange the memory mapping. |
---|
| 2870 | */ |
---|
| 2871 | error = bus_dmamap_load(adapter->rxtag, rx_buffer->map, |
---|
| 2872 | mtod(mp, void *), mp->m_len, |
---|
| 2873 | em_dmamap_cb, &paddr, 0); |
---|
| 2874 | if (error) { |
---|
| 2875 | m_free(mp); |
---|
| 2876 | return(error); |
---|
| 2877 | } |
---|
| 2878 | #else |
---|
| 2879 | paddr = kvtop(mtod(mp, void*)); |
---|
| 2880 | #endif |
---|
| 2881 | |
---|
| 2882 | rx_buffer->m_head = mp; |
---|
| 2883 | adapter->rx_desc_base[i].buffer_addr = htole64(paddr); |
---|
| 2884 | bus_dmamap_sync(adapter->rxtag, rx_buffer->map, BUS_DMASYNC_PREREAD); |
---|
| 2885 | |
---|
| 2886 | return(0); |
---|
| 2887 | } |
---|
| 2888 | |
---|
| 2889 | /********************************************************************* |
---|
| 2890 | * |
---|
| 2891 | * Allocate memory for rx_buffer structures. Since we use one |
---|
| 2892 | * rx_buffer per received packet, the maximum number of rx_buffer's |
---|
| 2893 | * that we'll need is equal to the number of receive descriptors |
---|
| 2894 | * that we've allocated. |
---|
| 2895 | * |
---|
| 2896 | **********************************************************************/ |
---|
| 2897 | static int |
---|
| 2898 | em_allocate_receive_structures(struct adapter * adapter) |
---|
| 2899 | { |
---|
| 2900 | int i, error; |
---|
| 2901 | #ifndef __rtems__ |
---|
| 2902 | struct em_buffer *rx_buffer; |
---|
| 2903 | #endif |
---|
| 2904 | |
---|
| 2905 | if (!(adapter->rx_buffer_area = |
---|
| 2906 | (struct em_buffer *) malloc(sizeof(struct em_buffer) * |
---|
| 2907 | adapter->num_rx_desc, M_DEVBUF, |
---|
| 2908 | M_NOWAIT))) { |
---|
| 2909 | printf("em%d: Unable to allocate rx_buffer memory\n", |
---|
| 2910 | adapter->unit); |
---|
| 2911 | return(ENOMEM); |
---|
| 2912 | } |
---|
| 2913 | |
---|
| 2914 | bzero(adapter->rx_buffer_area, |
---|
| 2915 | sizeof(struct em_buffer) * adapter->num_rx_desc); |
---|
| 2916 | |
---|
| 2917 | #ifndef __rtems__ |
---|
| 2918 | error = bus_dma_tag_create(NULL, /* parent */ |
---|
| 2919 | 1, 0, /* alignment, bounds */ |
---|
| 2920 | BUS_SPACE_MAXADDR, /* lowaddr */ |
---|
| 2921 | BUS_SPACE_MAXADDR, /* highaddr */ |
---|
| 2922 | NULL, NULL, /* filter, filterarg */ |
---|
| 2923 | MCLBYTES, /* maxsize */ |
---|
| 2924 | 1, /* nsegments */ |
---|
| 2925 | MCLBYTES, /* maxsegsize */ |
---|
| 2926 | BUS_DMA_ALLOCNOW, /* flags */ |
---|
| 2927 | NULL, /* lockfunc */ |
---|
| 2928 | NULL, /* lockarg */ |
---|
| 2929 | &adapter->rxtag); |
---|
| 2930 | if (error != 0) { |
---|
| 2931 | printf("em%d: em_allocate_receive_structures: " |
---|
| 2932 | "bus_dma_tag_create failed; error %u\n", |
---|
| 2933 | adapter->unit, error); |
---|
| 2934 | goto fail_0; |
---|
| 2935 | } |
---|
| 2936 | rx_buffer = adapter->rx_buffer_area; |
---|
| 2937 | for (i = 0; i < adapter->num_rx_desc; i++, rx_buffer++) { |
---|
| 2938 | error = bus_dmamap_create(adapter->rxtag, BUS_DMA_NOWAIT, |
---|
| 2939 | &rx_buffer->map); |
---|
| 2940 | if (error != 0) { |
---|
| 2941 | printf("em%d: em_allocate_receive_structures: " |
---|
| 2942 | "bus_dmamap_create failed; error %u\n", |
---|
| 2943 | adapter->unit, error); |
---|
| 2944 | goto fail_1; |
---|
| 2945 | } |
---|
| 2946 | } |
---|
| 2947 | |
---|
| 2948 | #else |
---|
| 2949 | error = 0; |
---|
| 2950 | #endif |
---|
| 2951 | |
---|
| 2952 | for (i = 0; i < adapter->num_rx_desc; i++) { |
---|
| 2953 | error = em_get_buf(i, adapter, NULL); |
---|
| 2954 | if (error != 0) { |
---|
| 2955 | adapter->rx_buffer_area[i].m_head = NULL; |
---|
| 2956 | adapter->rx_desc_base[i].buffer_addr = 0; |
---|
| 2957 | return(error); |
---|
| 2958 | } |
---|
| 2959 | } |
---|
| 2960 | bus_dmamap_sync(adapter->rxdma.dma_tag, adapter->rxdma.dma_map, |
---|
| 2961 | BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); |
---|
| 2962 | |
---|
| 2963 | return(0); |
---|
| 2964 | |
---|
| 2965 | #ifndef __rtems__ |
---|
| 2966 | fail_1: |
---|
| 2967 | bus_dma_tag_destroy(adapter->rxtag); |
---|
| 2968 | fail_0: |
---|
| 2969 | adapter->rxtag = NULL; |
---|
| 2970 | #endif |
---|
| 2971 | free(adapter->rx_buffer_area, M_DEVBUF); |
---|
| 2972 | adapter->rx_buffer_area = NULL; |
---|
| 2973 | return (error); |
---|
| 2974 | } |
---|
| 2975 | |
---|
| 2976 | /********************************************************************* |
---|
| 2977 | * |
---|
| 2978 | * Allocate and initialize receive structures. |
---|
| 2979 | * |
---|
| 2980 | **********************************************************************/ |
---|
| 2981 | static int |
---|
| 2982 | em_setup_receive_structures(struct adapter * adapter) |
---|
| 2983 | { |
---|
| 2984 | bzero((void *) adapter->rx_desc_base, |
---|
| 2985 | (sizeof(struct em_rx_desc)) * adapter->num_rx_desc); |
---|
| 2986 | |
---|
| 2987 | if (em_allocate_receive_structures(adapter)) |
---|
| 2988 | return ENOMEM; |
---|
| 2989 | |
---|
| 2990 | /* Setup our descriptor pointers */ |
---|
| 2991 | adapter->next_rx_desc_to_check = 0; |
---|
| 2992 | return(0); |
---|
| 2993 | } |
---|
| 2994 | |
---|
| 2995 | /********************************************************************* |
---|
| 2996 | * |
---|
| 2997 | * Enable receive unit. |
---|
| 2998 | * |
---|
| 2999 | **********************************************************************/ |
---|
| 3000 | static void |
---|
| 3001 | em_initialize_receive_unit(struct adapter * adapter) |
---|
| 3002 | { |
---|
| 3003 | u_int32_t reg_rctl; |
---|
| 3004 | #ifndef __rtems__ |
---|
| 3005 | u_int32_t reg_rxcsum; |
---|
| 3006 | #endif |
---|
| 3007 | struct ifnet *ifp; |
---|
| 3008 | u_int64_t bus_addr; |
---|
| 3009 | |
---|
| 3010 | INIT_DEBUGOUT("em_initialize_receive_unit: begin"); |
---|
| 3011 | ifp = &adapter->arpcom.ac_if; |
---|
| 3012 | |
---|
| 3013 | /* Make sure receives are disabled while setting up the descriptor ring */ |
---|
| 3014 | E1000_WRITE_REG(&adapter->hw, RCTL, 0); |
---|
| 3015 | |
---|
| 3016 | /* Set the Receive Delay Timer Register */ |
---|
| 3017 | E1000_WRITE_REG(&adapter->hw, RDTR, |
---|
| 3018 | adapter->rx_int_delay.value | E1000_RDT_FPDB); |
---|
| 3019 | |
---|
| 3020 | if(adapter->hw.mac_type >= em_82540) { |
---|
| 3021 | E1000_WRITE_REG(&adapter->hw, RADV, |
---|
| 3022 | adapter->rx_abs_int_delay.value); |
---|
| 3023 | |
---|
| 3024 | /* Set the interrupt throttling rate. Value is calculated |
---|
| 3025 | * as DEFAULT_ITR = 1/(MAX_INTS_PER_SEC * 256ns) */ |
---|
| 3026 | #define MAX_INTS_PER_SEC 8000 |
---|
| 3027 | #define DEFAULT_ITR 1000000000/(MAX_INTS_PER_SEC * 256) |
---|
| 3028 | E1000_WRITE_REG(&adapter->hw, ITR, DEFAULT_ITR); |
---|
| 3029 | } |
---|
| 3030 | |
---|
| 3031 | /* Setup the Base and Length of the Rx Descriptor Ring */ |
---|
| 3032 | bus_addr = adapter->rxdma.dma_paddr; |
---|
| 3033 | E1000_WRITE_REG(&adapter->hw, RDBAL, (u_int32_t)bus_addr); |
---|
| 3034 | E1000_WRITE_REG(&adapter->hw, RDBAH, (u_int32_t)(bus_addr >> 32)); |
---|
| 3035 | E1000_WRITE_REG(&adapter->hw, RDLEN, adapter->num_rx_desc * |
---|
| 3036 | sizeof(struct em_rx_desc)); |
---|
| 3037 | |
---|
| 3038 | /* Setup the HW Rx Head and Tail Descriptor Pointers */ |
---|
| 3039 | E1000_WRITE_REG(&adapter->hw, RDH, 0); |
---|
| 3040 | E1000_WRITE_REG(&adapter->hw, RDT, adapter->num_rx_desc - 1); |
---|
| 3041 | |
---|
| 3042 | /* Setup the Receive Control Register */ |
---|
| 3043 | reg_rctl = E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_LBM_NO | |
---|
| 3044 | E1000_RCTL_RDMTS_HALF | |
---|
| 3045 | (adapter->hw.mc_filter_type << E1000_RCTL_MO_SHIFT); |
---|
| 3046 | |
---|
| 3047 | if (adapter->hw.tbi_compatibility_on == TRUE) |
---|
| 3048 | reg_rctl |= E1000_RCTL_SBP; |
---|
| 3049 | |
---|
| 3050 | |
---|
| 3051 | switch (adapter->rx_buffer_len) { |
---|
| 3052 | default: |
---|
| 3053 | case EM_RXBUFFER_2048: |
---|
| 3054 | reg_rctl |= E1000_RCTL_SZ_2048; |
---|
| 3055 | break; |
---|
| 3056 | case EM_RXBUFFER_4096: |
---|
| 3057 | reg_rctl |= E1000_RCTL_SZ_4096 | E1000_RCTL_BSEX | E1000_RCTL_LPE; |
---|
| 3058 | break; |
---|
| 3059 | case EM_RXBUFFER_8192: |
---|
| 3060 | reg_rctl |= E1000_RCTL_SZ_8192 | E1000_RCTL_BSEX | E1000_RCTL_LPE; |
---|
| 3061 | break; |
---|
| 3062 | case EM_RXBUFFER_16384: |
---|
| 3063 | reg_rctl |= E1000_RCTL_SZ_16384 | E1000_RCTL_BSEX | E1000_RCTL_LPE; |
---|
| 3064 | break; |
---|
| 3065 | } |
---|
| 3066 | |
---|
| 3067 | if (ifp->if_mtu > ETHERMTU) |
---|
| 3068 | reg_rctl |= E1000_RCTL_LPE; |
---|
| 3069 | |
---|
| 3070 | #ifndef __rtems__ |
---|
| 3071 | /* Enable 82543 Receive Checksum Offload for TCP and UDP */ |
---|
| 3072 | if ((adapter->hw.mac_type >= em_82543) && |
---|
| 3073 | (ifp->if_capenable & IFCAP_RXCSUM)) { |
---|
| 3074 | reg_rxcsum = E1000_READ_REG(&adapter->hw, RXCSUM); |
---|
| 3075 | reg_rxcsum |= (E1000_RXCSUM_IPOFL | E1000_RXCSUM_TUOFL); |
---|
| 3076 | E1000_WRITE_REG(&adapter->hw, RXCSUM, reg_rxcsum); |
---|
| 3077 | } |
---|
| 3078 | #endif |
---|
| 3079 | |
---|
| 3080 | /* Enable Receives */ |
---|
| 3081 | E1000_WRITE_REG(&adapter->hw, RCTL, reg_rctl); |
---|
| 3082 | |
---|
| 3083 | return; |
---|
| 3084 | } |
---|
| 3085 | |
---|
| 3086 | /********************************************************************* |
---|
| 3087 | * |
---|
| 3088 | * Free receive related data structures. |
---|
| 3089 | * |
---|
| 3090 | **********************************************************************/ |
---|
| 3091 | static void |
---|
| 3092 | em_free_receive_structures(struct adapter *adapter) |
---|
| 3093 | { |
---|
| 3094 | struct em_buffer *rx_buffer; |
---|
| 3095 | int i; |
---|
| 3096 | |
---|
| 3097 | INIT_DEBUGOUT("free_receive_structures: begin"); |
---|
| 3098 | |
---|
| 3099 | if (adapter->rx_buffer_area != NULL) { |
---|
| 3100 | rx_buffer = adapter->rx_buffer_area; |
---|
| 3101 | for (i = 0; i < adapter->num_rx_desc; i++, rx_buffer++) { |
---|
| 3102 | #ifndef __rtems__ |
---|
| 3103 | if (rx_buffer->map != NULL) { |
---|
| 3104 | bus_dmamap_unload(adapter->rxtag, rx_buffer->map); |
---|
| 3105 | bus_dmamap_destroy(adapter->rxtag, rx_buffer->map); |
---|
| 3106 | } |
---|
| 3107 | #endif |
---|
| 3108 | if (rx_buffer->m_head != NULL) |
---|
| 3109 | m_freem(rx_buffer->m_head); |
---|
| 3110 | rx_buffer->m_head = NULL; |
---|
| 3111 | } |
---|
| 3112 | } |
---|
| 3113 | if (adapter->rx_buffer_area != NULL) { |
---|
| 3114 | free(adapter->rx_buffer_area, M_DEVBUF); |
---|
| 3115 | adapter->rx_buffer_area = NULL; |
---|
| 3116 | } |
---|
| 3117 | #ifndef __rtems__ |
---|
| 3118 | if (adapter->rxtag != NULL) { |
---|
| 3119 | bus_dma_tag_destroy(adapter->rxtag); |
---|
| 3120 | adapter->rxtag = NULL; |
---|
| 3121 | } |
---|
| 3122 | #endif |
---|
| 3123 | return; |
---|
| 3124 | } |
---|
| 3125 | |
---|
| 3126 | /********************************************************************* |
---|
| 3127 | * |
---|
| 3128 | * This routine executes in interrupt context. It replenishes |
---|
| 3129 | * the mbufs in the descriptor and sends data which has been |
---|
| 3130 | * dma'ed into host memory to upper layer. |
---|
| 3131 | * |
---|
| 3132 | * We loop at most count times if count is > 0, or until done if |
---|
| 3133 | * count < 0. |
---|
| 3134 | * |
---|
| 3135 | *********************************************************************/ |
---|
| 3136 | static void |
---|
| 3137 | em_process_receive_interrupts(struct adapter * adapter, int count) |
---|
| 3138 | { |
---|
| 3139 | struct ifnet *ifp; |
---|
| 3140 | struct mbuf *mp; |
---|
| 3141 | #if __FreeBSD_version < 500000 |
---|
| 3142 | struct ether_header *eh; |
---|
| 3143 | #endif |
---|
| 3144 | u_int8_t accept_frame = 0; |
---|
| 3145 | u_int8_t eop = 0; |
---|
| 3146 | u_int16_t len, desc_len, prev_len_adj; |
---|
| 3147 | int i; |
---|
| 3148 | |
---|
| 3149 | /* Pointer to the receive descriptor being examined. */ |
---|
| 3150 | struct em_rx_desc *current_desc; |
---|
| 3151 | |
---|
| 3152 | mtx_assert(&adapter->mtx, MA_OWNED); |
---|
| 3153 | |
---|
| 3154 | ifp = &adapter->arpcom.ac_if; |
---|
| 3155 | i = adapter->next_rx_desc_to_check; |
---|
| 3156 | current_desc = &adapter->rx_desc_base[i]; |
---|
| 3157 | bus_dmamap_sync(adapter->rxdma.dma_tag, adapter->rxdma.dma_map, |
---|
| 3158 | BUS_DMASYNC_POSTREAD); |
---|
| 3159 | |
---|
| 3160 | if (!((current_desc->status) & E1000_RXD_STAT_DD)) { |
---|
| 3161 | return; |
---|
| 3162 | } |
---|
| 3163 | |
---|
| 3164 | while ((current_desc->status & E1000_RXD_STAT_DD) && (count != 0)) { |
---|
| 3165 | |
---|
| 3166 | mp = adapter->rx_buffer_area[i].m_head; |
---|
| 3167 | bus_dmamap_sync(adapter->rxtag, adapter->rx_buffer_area[i].map, |
---|
| 3168 | BUS_DMASYNC_POSTREAD); |
---|
| 3169 | |
---|
| 3170 | accept_frame = 1; |
---|
| 3171 | prev_len_adj = 0; |
---|
| 3172 | desc_len = le16toh(current_desc->length); |
---|
| 3173 | if (current_desc->status & E1000_RXD_STAT_EOP) { |
---|
| 3174 | count--; |
---|
| 3175 | eop = 1; |
---|
| 3176 | if (desc_len < ETHER_CRC_LEN) { |
---|
| 3177 | len = 0; |
---|
| 3178 | prev_len_adj = ETHER_CRC_LEN - desc_len; |
---|
| 3179 | } |
---|
| 3180 | else { |
---|
| 3181 | len = desc_len - ETHER_CRC_LEN; |
---|
| 3182 | } |
---|
| 3183 | } else { |
---|
| 3184 | eop = 0; |
---|
| 3185 | len = desc_len; |
---|
| 3186 | } |
---|
| 3187 | |
---|
| 3188 | if (current_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK) { |
---|
| 3189 | u_int8_t last_byte; |
---|
| 3190 | u_int32_t pkt_len = desc_len; |
---|
| 3191 | |
---|
| 3192 | if (adapter->fmp != NULL) |
---|
| 3193 | pkt_len += adapter->fmp->m_pkthdr.len; |
---|
| 3194 | |
---|
| 3195 | last_byte = *(mtod(mp, caddr_t) + desc_len - 1); |
---|
| 3196 | |
---|
| 3197 | if (TBI_ACCEPT(&adapter->hw, current_desc->status, |
---|
| 3198 | current_desc->errors, |
---|
| 3199 | pkt_len, last_byte)) { |
---|
| 3200 | em_tbi_adjust_stats(&adapter->hw, |
---|
| 3201 | &adapter->stats, |
---|
| 3202 | pkt_len, |
---|
| 3203 | adapter->hw.mac_addr); |
---|
| 3204 | if (len > 0) len--; |
---|
| 3205 | } |
---|
| 3206 | else { |
---|
| 3207 | accept_frame = 0; |
---|
| 3208 | } |
---|
| 3209 | } |
---|
| 3210 | |
---|
| 3211 | if (accept_frame) { |
---|
| 3212 | |
---|
| 3213 | if (em_get_buf(i, adapter, NULL) == ENOBUFS) { |
---|
| 3214 | adapter->dropped_pkts++; |
---|
| 3215 | em_get_buf(i, adapter, mp); |
---|
| 3216 | if (adapter->fmp != NULL) |
---|
| 3217 | m_freem(adapter->fmp); |
---|
| 3218 | adapter->fmp = NULL; |
---|
| 3219 | adapter->lmp = NULL; |
---|
| 3220 | break; |
---|
| 3221 | } |
---|
| 3222 | |
---|
| 3223 | /* Assign correct length to the current fragment */ |
---|
| 3224 | mp->m_len = len; |
---|
| 3225 | |
---|
| 3226 | if (adapter->fmp == NULL) { |
---|
| 3227 | mp->m_pkthdr.len = len; |
---|
| 3228 | adapter->fmp = mp; /* Store the first mbuf */ |
---|
| 3229 | adapter->lmp = mp; |
---|
| 3230 | } else { |
---|
| 3231 | /* Chain mbuf's together */ |
---|
| 3232 | mp->m_flags &= ~M_PKTHDR; |
---|
| 3233 | /* |
---|
| 3234 | * Adjust length of previous mbuf in chain if we |
---|
| 3235 | * received less than 4 bytes in the last descriptor. |
---|
| 3236 | */ |
---|
| 3237 | if (prev_len_adj > 0) { |
---|
| 3238 | adapter->lmp->m_len -= prev_len_adj; |
---|
| 3239 | adapter->fmp->m_pkthdr.len -= prev_len_adj; |
---|
| 3240 | } |
---|
| 3241 | adapter->lmp->m_next = mp; |
---|
| 3242 | adapter->lmp = adapter->lmp->m_next; |
---|
| 3243 | adapter->fmp->m_pkthdr.len += len; |
---|
| 3244 | } |
---|
| 3245 | |
---|
| 3246 | if (eop) { |
---|
| 3247 | adapter->fmp->m_pkthdr.rcvif = ifp; |
---|
| 3248 | ifp->if_ipackets++; |
---|
| 3249 | |
---|
| 3250 | #if __FreeBSD_version < 500000 |
---|
| 3251 | eh = mtod(adapter->fmp, struct ether_header *); |
---|
| 3252 | /* Remove ethernet header from mbuf */ |
---|
| 3253 | m_adj(adapter->fmp, sizeof(struct ether_header)); |
---|
| 3254 | #ifndef __rtems__ |
---|
| 3255 | em_receive_checksum(adapter, current_desc, |
---|
| 3256 | adapter->fmp); |
---|
| 3257 | if (current_desc->status & E1000_RXD_STAT_VP) |
---|
| 3258 | VLAN_INPUT_TAG(eh, adapter->fmp, |
---|
| 3259 | (current_desc->special & |
---|
| 3260 | E1000_RXD_SPC_VLAN_MASK)); |
---|
| 3261 | else |
---|
| 3262 | #endif |
---|
| 3263 | ether_input(ifp, eh, adapter->fmp); |
---|
| 3264 | #else |
---|
| 3265 | |
---|
| 3266 | em_receive_checksum(adapter, current_desc, |
---|
| 3267 | adapter->fmp); |
---|
| 3268 | if (current_desc->status & E1000_RXD_STAT_VP) |
---|
| 3269 | VLAN_INPUT_TAG(ifp, adapter->fmp, |
---|
| 3270 | (current_desc->special & |
---|
| 3271 | E1000_RXD_SPC_VLAN_MASK), |
---|
| 3272 | adapter->fmp = NULL); |
---|
| 3273 | |
---|
| 3274 | if (adapter->fmp != NULL) { |
---|
| 3275 | EM_UNLOCK(adapter); |
---|
| 3276 | (*ifp->if_input)(ifp, adapter->fmp); |
---|
| 3277 | EM_LOCK(adapter); |
---|
| 3278 | } |
---|
| 3279 | #endif |
---|
| 3280 | adapter->fmp = NULL; |
---|
| 3281 | adapter->lmp = NULL; |
---|
| 3282 | } |
---|
| 3283 | } else { |
---|
| 3284 | adapter->dropped_pkts++; |
---|
| 3285 | em_get_buf(i, adapter, mp); |
---|
| 3286 | if (adapter->fmp != NULL) |
---|
| 3287 | m_freem(adapter->fmp); |
---|
| 3288 | adapter->fmp = NULL; |
---|
| 3289 | adapter->lmp = NULL; |
---|
| 3290 | } |
---|
| 3291 | |
---|
| 3292 | /* Zero out the receive descriptors status */ |
---|
| 3293 | current_desc->status = 0; |
---|
| 3294 | |
---|
| 3295 | /* Advance the E1000's Receive Queue #0 "Tail Pointer". */ |
---|
| 3296 | E1000_WRITE_REG(&adapter->hw, RDT, i); |
---|
| 3297 | |
---|
| 3298 | /* Advance our pointers to the next descriptor */ |
---|
| 3299 | if (++i == adapter->num_rx_desc) { |
---|
| 3300 | i = 0; |
---|
| 3301 | current_desc = adapter->rx_desc_base; |
---|
| 3302 | } else |
---|
| 3303 | current_desc++; |
---|
| 3304 | } |
---|
| 3305 | bus_dmamap_sync(adapter->rxdma.dma_tag, adapter->rxdma.dma_map, |
---|
| 3306 | BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); |
---|
| 3307 | adapter->next_rx_desc_to_check = i; |
---|
| 3308 | return; |
---|
| 3309 | } |
---|
| 3310 | |
---|
| 3311 | #ifndef __rtems__ |
---|
| 3312 | /********************************************************************* |
---|
| 3313 | * |
---|
| 3314 | * Verify that the hardware indicated that the checksum is valid. |
---|
| 3315 | * Inform the stack about the status of checksum so that stack |
---|
| 3316 | * doesn't spend time verifying the checksum. |
---|
| 3317 | * |
---|
| 3318 | *********************************************************************/ |
---|
| 3319 | static void |
---|
| 3320 | em_receive_checksum(struct adapter *adapter, |
---|
| 3321 | struct em_rx_desc *rx_desc, |
---|
| 3322 | struct mbuf *mp) |
---|
| 3323 | { |
---|
| 3324 | /* 82543 or newer only */ |
---|
| 3325 | if ((adapter->hw.mac_type < em_82543) || |
---|
| 3326 | /* Ignore Checksum bit is set */ |
---|
| 3327 | (rx_desc->status & E1000_RXD_STAT_IXSM)) { |
---|
| 3328 | mp->m_pkthdr.csum_flags = 0; |
---|
| 3329 | return; |
---|
| 3330 | } |
---|
| 3331 | |
---|
| 3332 | if (rx_desc->status & E1000_RXD_STAT_IPCS) { |
---|
| 3333 | /* Did it pass? */ |
---|
| 3334 | if (!(rx_desc->errors & E1000_RXD_ERR_IPE)) { |
---|
| 3335 | /* IP Checksum Good */ |
---|
| 3336 | mp->m_pkthdr.csum_flags = CSUM_IP_CHECKED; |
---|
| 3337 | mp->m_pkthdr.csum_flags |= CSUM_IP_VALID; |
---|
| 3338 | |
---|
| 3339 | } else { |
---|
| 3340 | mp->m_pkthdr.csum_flags = 0; |
---|
| 3341 | } |
---|
| 3342 | } |
---|
| 3343 | |
---|
| 3344 | if (rx_desc->status & E1000_RXD_STAT_TCPCS) { |
---|
| 3345 | /* Did it pass? */ |
---|
| 3346 | if (!(rx_desc->errors & E1000_RXD_ERR_TCPE)) { |
---|
| 3347 | mp->m_pkthdr.csum_flags |= |
---|
| 3348 | (CSUM_DATA_VALID | CSUM_PSEUDO_HDR); |
---|
| 3349 | mp->m_pkthdr.csum_data = htons(0xffff); |
---|
| 3350 | } |
---|
| 3351 | } |
---|
| 3352 | |
---|
| 3353 | return; |
---|
| 3354 | } |
---|
| 3355 | |
---|
| 3356 | |
---|
| 3357 | static void |
---|
| 3358 | em_enable_vlans(struct adapter *adapter) |
---|
| 3359 | { |
---|
| 3360 | uint32_t ctrl; |
---|
| 3361 | |
---|
| 3362 | E1000_WRITE_REG(&adapter->hw, VET, ETHERTYPE_VLAN); |
---|
| 3363 | |
---|
| 3364 | ctrl = E1000_READ_REG(&adapter->hw, CTRL); |
---|
| 3365 | ctrl |= E1000_CTRL_VME; |
---|
| 3366 | E1000_WRITE_REG(&adapter->hw, CTRL, ctrl); |
---|
| 3367 | |
---|
| 3368 | return; |
---|
| 3369 | } |
---|
| 3370 | |
---|
| 3371 | static void |
---|
| 3372 | em_disable_vlans(struct adapter *adapter) |
---|
| 3373 | { |
---|
| 3374 | uint32_t ctrl; |
---|
| 3375 | |
---|
| 3376 | ctrl = E1000_READ_REG(&adapter->hw, CTRL); |
---|
| 3377 | ctrl &= ~E1000_CTRL_VME; |
---|
| 3378 | E1000_WRITE_REG(&adapter->hw, CTRL, ctrl); |
---|
| 3379 | |
---|
| 3380 | return; |
---|
| 3381 | } |
---|
| 3382 | #endif |
---|
| 3383 | |
---|
| 3384 | static void |
---|
| 3385 | em_enable_intr(struct adapter * adapter) |
---|
| 3386 | { |
---|
| 3387 | E1000_WRITE_REG(&adapter->hw, IMS, (IMS_ENABLE_MASK)); |
---|
| 3388 | return; |
---|
| 3389 | } |
---|
| 3390 | |
---|
| 3391 | static void |
---|
| 3392 | em_disable_intr(struct adapter *adapter) |
---|
| 3393 | { |
---|
| 3394 | /* |
---|
| 3395 | * The first version of 82542 had an errata where when link was forced it |
---|
| 3396 | * would stay up even up even if the cable was disconnected. Sequence errors |
---|
| 3397 | * were used to detect the disconnect and then the driver would unforce the link. |
---|
| 3398 | * This code in the in the ISR. For this to work correctly the Sequence error |
---|
| 3399 | * interrupt had to be enabled all the time. |
---|
| 3400 | */ |
---|
| 3401 | |
---|
| 3402 | if (adapter->hw.mac_type == em_82542_rev2_0) |
---|
| 3403 | E1000_WRITE_REG(&adapter->hw, IMC, |
---|
| 3404 | (0xffffffff & ~E1000_IMC_RXSEQ)); |
---|
| 3405 | else |
---|
| 3406 | E1000_WRITE_REG(&adapter->hw, IMC, |
---|
| 3407 | 0xffffffff); |
---|
| 3408 | return; |
---|
| 3409 | } |
---|
| 3410 | |
---|
| 3411 | static int |
---|
| 3412 | em_is_valid_ether_addr(u_int8_t *addr) |
---|
| 3413 | { |
---|
| 3414 | char zero_addr[6] = { 0, 0, 0, 0, 0, 0 }; |
---|
| 3415 | |
---|
| 3416 | if ((addr[0] & 1) || (!bcmp(addr, zero_addr, ETHER_ADDR_LEN))) { |
---|
| 3417 | return (FALSE); |
---|
| 3418 | } |
---|
| 3419 | |
---|
| 3420 | return(TRUE); |
---|
| 3421 | } |
---|
| 3422 | |
---|
| 3423 | void |
---|
| 3424 | em_write_pci_cfg(struct em_hw *hw, |
---|
| 3425 | uint32_t reg, |
---|
| 3426 | uint16_t *value) |
---|
| 3427 | { |
---|
| 3428 | pci_write_config(((struct em_osdep *)hw->back)->dev, reg, |
---|
| 3429 | *value, 2); |
---|
| 3430 | } |
---|
| 3431 | |
---|
| 3432 | void |
---|
| 3433 | em_read_pci_cfg(struct em_hw *hw, uint32_t reg, |
---|
| 3434 | uint16_t *value) |
---|
| 3435 | { |
---|
| 3436 | *value = pci_read_config(((struct em_osdep *)hw->back)->dev, |
---|
| 3437 | reg, 2); |
---|
| 3438 | return; |
---|
| 3439 | } |
---|
| 3440 | |
---|
| 3441 | void |
---|
| 3442 | em_pci_set_mwi(struct em_hw *hw) |
---|
| 3443 | { |
---|
| 3444 | pci_write_config(((struct em_osdep *)hw->back)->dev, |
---|
| 3445 | PCIR_COMMAND, |
---|
| 3446 | (hw->pci_cmd_word | CMD_MEM_WRT_INVALIDATE), 2); |
---|
| 3447 | return; |
---|
| 3448 | } |
---|
| 3449 | |
---|
| 3450 | void |
---|
| 3451 | em_pci_clear_mwi(struct em_hw *hw) |
---|
| 3452 | { |
---|
| 3453 | pci_write_config(((struct em_osdep *)hw->back)->dev, |
---|
| 3454 | PCIR_COMMAND, |
---|
| 3455 | (hw->pci_cmd_word & ~CMD_MEM_WRT_INVALIDATE), 2); |
---|
| 3456 | return; |
---|
| 3457 | } |
---|
| 3458 | |
---|
| 3459 | uint32_t |
---|
| 3460 | em_io_read(struct em_hw *hw, unsigned long port) |
---|
| 3461 | { |
---|
| 3462 | return(inl(port)); |
---|
| 3463 | } |
---|
| 3464 | |
---|
| 3465 | void |
---|
| 3466 | em_io_write(struct em_hw *hw, unsigned long port, uint32_t value) |
---|
| 3467 | { |
---|
| 3468 | #ifndef __rtems__ |
---|
| 3469 | outl(port, value); |
---|
| 3470 | #else |
---|
| 3471 | /* everybody else has this the other way round! */ |
---|
| 3472 | outl(value, port); |
---|
| 3473 | #endif |
---|
| 3474 | return; |
---|
| 3475 | } |
---|
| 3476 | |
---|
| 3477 | /********************************************************************* |
---|
| 3478 | * 82544 Coexistence issue workaround. |
---|
| 3479 | * There are 2 issues. |
---|
| 3480 | * 1. Transmit Hang issue. |
---|
| 3481 | * To detect this issue, following equation can be used... |
---|
| 3482 | * SIZE[3:0] + ADDR[2:0] = SUM[3:0]. |
---|
| 3483 | * If SUM[3:0] is in between 1 to 4, we will have this issue. |
---|
| 3484 | * |
---|
| 3485 | * 2. DAC issue. |
---|
| 3486 | * To detect this issue, following equation can be used... |
---|
| 3487 | * SIZE[3:0] + ADDR[2:0] = SUM[3:0]. |
---|
| 3488 | * If SUM[3:0] is in between 9 to c, we will have this issue. |
---|
| 3489 | * |
---|
| 3490 | * |
---|
| 3491 | * WORKAROUND: |
---|
| 3492 | * Make sure we do not have ending address as 1,2,3,4(Hang) or 9,a,b,c (DAC) |
---|
| 3493 | * |
---|
| 3494 | *** *********************************************************************/ |
---|
| 3495 | static u_int32_t |
---|
| 3496 | em_fill_descriptors (u_int64_t address, |
---|
| 3497 | u_int32_t length, |
---|
| 3498 | PDESC_ARRAY desc_array) |
---|
| 3499 | { |
---|
| 3500 | /* Since issue is sensitive to length and address.*/ |
---|
| 3501 | /* Let us first check the address...*/ |
---|
| 3502 | u_int32_t safe_terminator; |
---|
| 3503 | if (length <= 4) { |
---|
| 3504 | desc_array->descriptor[0].address = address; |
---|
| 3505 | desc_array->descriptor[0].length = length; |
---|
| 3506 | desc_array->elements = 1; |
---|
| 3507 | return desc_array->elements; |
---|
| 3508 | } |
---|
| 3509 | safe_terminator = (u_int32_t)((((u_int32_t)address & 0x7) + (length & 0xF)) & 0xF); |
---|
| 3510 | /* if it does not fall between 0x1 to 0x4 and 0x9 to 0xC then return */ |
---|
| 3511 | if (safe_terminator == 0 || |
---|
| 3512 | (safe_terminator > 4 && |
---|
| 3513 | safe_terminator < 9) || |
---|
| 3514 | (safe_terminator > 0xC && |
---|
| 3515 | safe_terminator <= 0xF)) { |
---|
| 3516 | desc_array->descriptor[0].address = address; |
---|
| 3517 | desc_array->descriptor[0].length = length; |
---|
| 3518 | desc_array->elements = 1; |
---|
| 3519 | return desc_array->elements; |
---|
| 3520 | } |
---|
| 3521 | |
---|
| 3522 | desc_array->descriptor[0].address = address; |
---|
| 3523 | desc_array->descriptor[0].length = length - 4; |
---|
| 3524 | desc_array->descriptor[1].address = address + (length - 4); |
---|
| 3525 | desc_array->descriptor[1].length = 4; |
---|
| 3526 | desc_array->elements = 2; |
---|
| 3527 | return desc_array->elements; |
---|
| 3528 | } |
---|
| 3529 | |
---|
| 3530 | /********************************************************************** |
---|
| 3531 | * |
---|
| 3532 | * Update the board statistics counters. |
---|
| 3533 | * |
---|
| 3534 | **********************************************************************/ |
---|
| 3535 | static void |
---|
| 3536 | em_update_stats_counters(struct adapter *adapter) |
---|
| 3537 | { |
---|
| 3538 | struct ifnet *ifp; |
---|
| 3539 | |
---|
| 3540 | if(adapter->hw.media_type == em_media_type_copper || |
---|
| 3541 | (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU)) { |
---|
| 3542 | adapter->stats.symerrs += E1000_READ_REG(&adapter->hw, SYMERRS); |
---|
| 3543 | adapter->stats.sec += E1000_READ_REG(&adapter->hw, SEC); |
---|
| 3544 | } |
---|
| 3545 | adapter->stats.crcerrs += E1000_READ_REG(&adapter->hw, CRCERRS); |
---|
| 3546 | adapter->stats.mpc += E1000_READ_REG(&adapter->hw, MPC); |
---|
| 3547 | adapter->stats.scc += E1000_READ_REG(&adapter->hw, SCC); |
---|
| 3548 | adapter->stats.ecol += E1000_READ_REG(&adapter->hw, ECOL); |
---|
| 3549 | |
---|
| 3550 | adapter->stats.mcc += E1000_READ_REG(&adapter->hw, MCC); |
---|
| 3551 | adapter->stats.latecol += E1000_READ_REG(&adapter->hw, LATECOL); |
---|
| 3552 | adapter->stats.colc += E1000_READ_REG(&adapter->hw, COLC); |
---|
| 3553 | adapter->stats.dc += E1000_READ_REG(&adapter->hw, DC); |
---|
| 3554 | adapter->stats.rlec += E1000_READ_REG(&adapter->hw, RLEC); |
---|
| 3555 | adapter->stats.xonrxc += E1000_READ_REG(&adapter->hw, XONRXC); |
---|
| 3556 | adapter->stats.xontxc += E1000_READ_REG(&adapter->hw, XONTXC); |
---|
| 3557 | adapter->stats.xoffrxc += E1000_READ_REG(&adapter->hw, XOFFRXC); |
---|
| 3558 | adapter->stats.xofftxc += E1000_READ_REG(&adapter->hw, XOFFTXC); |
---|
| 3559 | adapter->stats.fcruc += E1000_READ_REG(&adapter->hw, FCRUC); |
---|
| 3560 | adapter->stats.prc64 += E1000_READ_REG(&adapter->hw, PRC64); |
---|
| 3561 | adapter->stats.prc127 += E1000_READ_REG(&adapter->hw, PRC127); |
---|
| 3562 | adapter->stats.prc255 += E1000_READ_REG(&adapter->hw, PRC255); |
---|
| 3563 | adapter->stats.prc511 += E1000_READ_REG(&adapter->hw, PRC511); |
---|
| 3564 | adapter->stats.prc1023 += E1000_READ_REG(&adapter->hw, PRC1023); |
---|
| 3565 | adapter->stats.prc1522 += E1000_READ_REG(&adapter->hw, PRC1522); |
---|
| 3566 | adapter->stats.gprc += E1000_READ_REG(&adapter->hw, GPRC); |
---|
| 3567 | adapter->stats.bprc += E1000_READ_REG(&adapter->hw, BPRC); |
---|
| 3568 | adapter->stats.mprc += E1000_READ_REG(&adapter->hw, MPRC); |
---|
| 3569 | adapter->stats.gptc += E1000_READ_REG(&adapter->hw, GPTC); |
---|
| 3570 | |
---|
| 3571 | /* For the 64-bit byte counters the low dword must be read first. */ |
---|
| 3572 | /* Both registers clear on the read of the high dword */ |
---|
| 3573 | |
---|
| 3574 | adapter->stats.gorcl += E1000_READ_REG(&adapter->hw, GORCL); |
---|
| 3575 | adapter->stats.gorch += E1000_READ_REG(&adapter->hw, GORCH); |
---|
| 3576 | adapter->stats.gotcl += E1000_READ_REG(&adapter->hw, GOTCL); |
---|
| 3577 | adapter->stats.gotch += E1000_READ_REG(&adapter->hw, GOTCH); |
---|
| 3578 | |
---|
| 3579 | adapter->stats.rnbc += E1000_READ_REG(&adapter->hw, RNBC); |
---|
| 3580 | adapter->stats.ruc += E1000_READ_REG(&adapter->hw, RUC); |
---|
| 3581 | adapter->stats.rfc += E1000_READ_REG(&adapter->hw, RFC); |
---|
| 3582 | adapter->stats.roc += E1000_READ_REG(&adapter->hw, ROC); |
---|
| 3583 | adapter->stats.rjc += E1000_READ_REG(&adapter->hw, RJC); |
---|
| 3584 | |
---|
| 3585 | adapter->stats.torl += E1000_READ_REG(&adapter->hw, TORL); |
---|
| 3586 | adapter->stats.torh += E1000_READ_REG(&adapter->hw, TORH); |
---|
| 3587 | adapter->stats.totl += E1000_READ_REG(&adapter->hw, TOTL); |
---|
| 3588 | adapter->stats.toth += E1000_READ_REG(&adapter->hw, TOTH); |
---|
| 3589 | |
---|
| 3590 | adapter->stats.tpr += E1000_READ_REG(&adapter->hw, TPR); |
---|
| 3591 | adapter->stats.tpt += E1000_READ_REG(&adapter->hw, TPT); |
---|
| 3592 | adapter->stats.ptc64 += E1000_READ_REG(&adapter->hw, PTC64); |
---|
| 3593 | adapter->stats.ptc127 += E1000_READ_REG(&adapter->hw, PTC127); |
---|
| 3594 | adapter->stats.ptc255 += E1000_READ_REG(&adapter->hw, PTC255); |
---|
| 3595 | adapter->stats.ptc511 += E1000_READ_REG(&adapter->hw, PTC511); |
---|
| 3596 | adapter->stats.ptc1023 += E1000_READ_REG(&adapter->hw, PTC1023); |
---|
| 3597 | adapter->stats.ptc1522 += E1000_READ_REG(&adapter->hw, PTC1522); |
---|
| 3598 | adapter->stats.mptc += E1000_READ_REG(&adapter->hw, MPTC); |
---|
| 3599 | adapter->stats.bptc += E1000_READ_REG(&adapter->hw, BPTC); |
---|
| 3600 | |
---|
| 3601 | if (adapter->hw.mac_type >= em_82543) { |
---|
| 3602 | adapter->stats.algnerrc += |
---|
| 3603 | E1000_READ_REG(&adapter->hw, ALGNERRC); |
---|
| 3604 | adapter->stats.rxerrc += |
---|
| 3605 | E1000_READ_REG(&adapter->hw, RXERRC); |
---|
| 3606 | adapter->stats.tncrs += |
---|
| 3607 | E1000_READ_REG(&adapter->hw, TNCRS); |
---|
| 3608 | adapter->stats.cexterr += |
---|
| 3609 | E1000_READ_REG(&adapter->hw, CEXTERR); |
---|
| 3610 | adapter->stats.tsctc += |
---|
| 3611 | E1000_READ_REG(&adapter->hw, TSCTC); |
---|
| 3612 | adapter->stats.tsctfc += |
---|
| 3613 | E1000_READ_REG(&adapter->hw, TSCTFC); |
---|
| 3614 | } |
---|
| 3615 | ifp = &adapter->arpcom.ac_if; |
---|
| 3616 | |
---|
| 3617 | /* Fill out the OS statistics structure */ |
---|
| 3618 | ifp->if_ibytes = adapter->stats.gorcl; |
---|
| 3619 | ifp->if_obytes = adapter->stats.gotcl; |
---|
| 3620 | ifp->if_imcasts = adapter->stats.mprc; |
---|
| 3621 | ifp->if_collisions = adapter->stats.colc; |
---|
| 3622 | |
---|
| 3623 | /* Rx Errors */ |
---|
| 3624 | ifp->if_ierrors = |
---|
| 3625 | adapter->dropped_pkts + |
---|
| 3626 | adapter->stats.rxerrc + |
---|
| 3627 | adapter->stats.crcerrs + |
---|
| 3628 | adapter->stats.algnerrc + |
---|
| 3629 | adapter->stats.rlec + |
---|
| 3630 | adapter->stats.mpc + adapter->stats.cexterr; |
---|
| 3631 | |
---|
| 3632 | /* Tx Errors */ |
---|
| 3633 | ifp->if_oerrors = adapter->stats.ecol + adapter->stats.latecol; |
---|
| 3634 | |
---|
| 3635 | } |
---|
| 3636 | |
---|
| 3637 | #ifndef __rtems__ |
---|
| 3638 | /********************************************************************** |
---|
| 3639 | * |
---|
| 3640 | * This routine is called only when em_display_debug_stats is enabled. |
---|
| 3641 | * This routine provides a way to take a look at important statistics |
---|
| 3642 | * maintained by the driver and hardware. |
---|
| 3643 | * |
---|
| 3644 | **********************************************************************/ |
---|
| 3645 | static void |
---|
| 3646 | em_print_debug_info(struct adapter *adapter) |
---|
| 3647 | { |
---|
| 3648 | int unit = adapter->unit; |
---|
| 3649 | uint8_t *hw_addr = adapter->hw.hw_addr; |
---|
| 3650 | |
---|
| 3651 | printf("em%d: Adapter hardware address = %p \n", unit, hw_addr); |
---|
| 3652 | printf("em%d:CTRL = 0x%x\n", unit, |
---|
| 3653 | E1000_READ_REG(&adapter->hw, CTRL)); |
---|
| 3654 | printf("em%d:RCTL = 0x%x PS=(0x8402)\n", unit, |
---|
| 3655 | E1000_READ_REG(&adapter->hw, RCTL)); |
---|
| 3656 | printf("em%d:tx_int_delay = %d, tx_abs_int_delay = %d\n", unit, |
---|
| 3657 | E1000_READ_REG(&adapter->hw, TIDV), |
---|
| 3658 | E1000_READ_REG(&adapter->hw, TADV)); |
---|
| 3659 | printf("em%d:rx_int_delay = %d, rx_abs_int_delay = %d\n", unit, |
---|
| 3660 | E1000_READ_REG(&adapter->hw, RDTR), |
---|
| 3661 | E1000_READ_REG(&adapter->hw, RADV)); |
---|
| 3662 | printf("em%d: fifo workaround = %lld, fifo_reset = %lld\n", unit, |
---|
| 3663 | (long long)adapter->tx_fifo_wrk_cnt, |
---|
| 3664 | (long long)adapter->tx_fifo_reset_cnt); |
---|
| 3665 | printf("em%d: hw tdh = %d, hw tdt = %d\n", unit, |
---|
| 3666 | E1000_READ_REG(&adapter->hw, TDH), |
---|
| 3667 | E1000_READ_REG(&adapter->hw, TDT)); |
---|
| 3668 | printf("em%d: Num Tx descriptors avail = %d\n", unit, |
---|
| 3669 | adapter->num_tx_desc_avail); |
---|
| 3670 | printf("em%d: Tx Descriptors not avail1 = %ld\n", unit, |
---|
| 3671 | adapter->no_tx_desc_avail1); |
---|
| 3672 | printf("em%d: Tx Descriptors not avail2 = %ld\n", unit, |
---|
| 3673 | adapter->no_tx_desc_avail2); |
---|
| 3674 | printf("em%d: Std mbuf failed = %ld\n", unit, |
---|
| 3675 | adapter->mbuf_alloc_failed); |
---|
| 3676 | printf("em%d: Std mbuf cluster failed = %ld\n", unit, |
---|
| 3677 | adapter->mbuf_cluster_failed); |
---|
| 3678 | printf("em%d: Driver dropped packets = %ld\n", unit, |
---|
| 3679 | adapter->dropped_pkts); |
---|
| 3680 | |
---|
| 3681 | return; |
---|
| 3682 | } |
---|
| 3683 | #endif |
---|
| 3684 | |
---|
| 3685 | static void |
---|
| 3686 | em_print_hw_stats(struct adapter *adapter) |
---|
| 3687 | { |
---|
| 3688 | int unit = adapter->unit; |
---|
| 3689 | |
---|
| 3690 | printf("em%d: Excessive collisions = %lld\n", unit, |
---|
| 3691 | (long long)adapter->stats.ecol); |
---|
| 3692 | printf("em%d: Symbol errors = %lld\n", unit, |
---|
| 3693 | (long long)adapter->stats.symerrs); |
---|
| 3694 | printf("em%d: Sequence errors = %lld\n", unit, |
---|
| 3695 | (long long)adapter->stats.sec); |
---|
| 3696 | printf("em%d: Defer count = %lld\n", unit, |
---|
| 3697 | (long long)adapter->stats.dc); |
---|
| 3698 | |
---|
| 3699 | printf("em%d: Missed Packets = %lld\n", unit, |
---|
| 3700 | (long long)adapter->stats.mpc); |
---|
| 3701 | printf("em%d: Receive No Buffers = %lld\n", unit, |
---|
| 3702 | (long long)adapter->stats.rnbc); |
---|
| 3703 | printf("em%d: Receive length errors = %lld\n", unit, |
---|
| 3704 | (long long)adapter->stats.rlec); |
---|
| 3705 | printf("em%d: Receive errors = %lld\n", unit, |
---|
| 3706 | (long long)adapter->stats.rxerrc); |
---|
| 3707 | printf("em%d: Crc errors = %lld\n", unit, |
---|
| 3708 | (long long)adapter->stats.crcerrs); |
---|
| 3709 | printf("em%d: Alignment errors = %lld\n", unit, |
---|
| 3710 | (long long)adapter->stats.algnerrc); |
---|
| 3711 | printf("em%d: Carrier extension errors = %lld\n", unit, |
---|
| 3712 | (long long)adapter->stats.cexterr); |
---|
| 3713 | |
---|
| 3714 | printf("em%d: XON Rcvd = %lld\n", unit, |
---|
| 3715 | (long long)adapter->stats.xonrxc); |
---|
| 3716 | printf("em%d: XON Xmtd = %lld\n", unit, |
---|
| 3717 | (long long)adapter->stats.xontxc); |
---|
| 3718 | printf("em%d: XOFF Rcvd = %lld\n", unit, |
---|
| 3719 | (long long)adapter->stats.xoffrxc); |
---|
| 3720 | printf("em%d: XOFF Xmtd = %lld\n", unit, |
---|
| 3721 | (long long)adapter->stats.xofftxc); |
---|
| 3722 | |
---|
| 3723 | printf("em%d: Good Packets Rcvd = %lld\n", unit, |
---|
| 3724 | (long long)adapter->stats.gprc); |
---|
| 3725 | printf("em%d: Good Packets Xmtd = %lld\n", unit, |
---|
| 3726 | (long long)adapter->stats.gptc); |
---|
| 3727 | |
---|
| 3728 | return; |
---|
| 3729 | } |
---|
| 3730 | |
---|
| 3731 | #ifndef __rtems__ |
---|
| 3732 | static int |
---|
| 3733 | em_sysctl_debug_info(SYSCTL_HANDLER_ARGS) |
---|
| 3734 | { |
---|
| 3735 | int error; |
---|
| 3736 | int result; |
---|
| 3737 | struct adapter *adapter; |
---|
| 3738 | |
---|
| 3739 | result = -1; |
---|
| 3740 | error = sysctl_handle_int(oidp, &result, 0, req); |
---|
| 3741 | |
---|
| 3742 | if (error || !req->newptr) |
---|
| 3743 | return (error); |
---|
| 3744 | |
---|
| 3745 | if (result == 1) { |
---|
| 3746 | adapter = (struct adapter *)arg1; |
---|
| 3747 | em_print_debug_info(adapter); |
---|
| 3748 | } |
---|
| 3749 | |
---|
| 3750 | return error; |
---|
| 3751 | } |
---|
| 3752 | |
---|
| 3753 | static int |
---|
| 3754 | em_sysctl_stats(SYSCTL_HANDLER_ARGS) |
---|
| 3755 | { |
---|
| 3756 | int error; |
---|
| 3757 | int result; |
---|
| 3758 | struct adapter *adapter; |
---|
| 3759 | |
---|
| 3760 | result = -1; |
---|
| 3761 | error = sysctl_handle_int(oidp, &result, 0, req); |
---|
| 3762 | |
---|
| 3763 | if (error || !req->newptr) |
---|
| 3764 | return (error); |
---|
| 3765 | |
---|
| 3766 | if (result == 1) { |
---|
| 3767 | adapter = (struct adapter *)arg1; |
---|
| 3768 | em_print_hw_stats(adapter); |
---|
| 3769 | } |
---|
| 3770 | |
---|
| 3771 | return error; |
---|
| 3772 | } |
---|
| 3773 | |
---|
| 3774 | static int |
---|
| 3775 | em_sysctl_int_delay(SYSCTL_HANDLER_ARGS) |
---|
| 3776 | { |
---|
| 3777 | struct em_int_delay_info *info; |
---|
| 3778 | struct adapter *adapter; |
---|
| 3779 | u_int32_t regval; |
---|
| 3780 | int error; |
---|
| 3781 | int usecs; |
---|
| 3782 | int ticks; |
---|
| 3783 | int s; |
---|
| 3784 | |
---|
| 3785 | info = (struct em_int_delay_info *)arg1; |
---|
| 3786 | adapter = info->adapter; |
---|
| 3787 | usecs = info->value; |
---|
| 3788 | error = sysctl_handle_int(oidp, &usecs, 0, req); |
---|
| 3789 | if (error != 0 || req->newptr == NULL) |
---|
| 3790 | return error; |
---|
| 3791 | if (usecs < 0 || usecs > E1000_TICKS_TO_USECS(65535)) |
---|
| 3792 | return EINVAL; |
---|
| 3793 | info->value = usecs; |
---|
| 3794 | ticks = E1000_USECS_TO_TICKS(usecs); |
---|
| 3795 | |
---|
| 3796 | s = splimp(); |
---|
| 3797 | regval = E1000_READ_OFFSET(&adapter->hw, info->offset); |
---|
| 3798 | regval = (regval & ~0xffff) | (ticks & 0xffff); |
---|
| 3799 | /* Handle a few special cases. */ |
---|
| 3800 | switch (info->offset) { |
---|
| 3801 | case E1000_RDTR: |
---|
| 3802 | case E1000_82542_RDTR: |
---|
| 3803 | regval |= E1000_RDT_FPDB; |
---|
| 3804 | break; |
---|
| 3805 | case E1000_TIDV: |
---|
| 3806 | case E1000_82542_TIDV: |
---|
| 3807 | if (ticks == 0) { |
---|
| 3808 | adapter->txd_cmd &= ~E1000_TXD_CMD_IDE; |
---|
| 3809 | /* Don't write 0 into the TIDV register. */ |
---|
| 3810 | regval++; |
---|
| 3811 | } else |
---|
| 3812 | adapter->txd_cmd |= E1000_TXD_CMD_IDE; |
---|
| 3813 | break; |
---|
| 3814 | } |
---|
| 3815 | E1000_WRITE_OFFSET(&adapter->hw, info->offset, regval); |
---|
| 3816 | splx(s); |
---|
| 3817 | return 0; |
---|
| 3818 | } |
---|
| 3819 | |
---|
| 3820 | static void |
---|
| 3821 | em_add_int_delay_sysctl(struct adapter *adapter, const char *name, |
---|
| 3822 | const char *description, struct em_int_delay_info *info, |
---|
| 3823 | int offset, int value) |
---|
| 3824 | { |
---|
| 3825 | info->adapter = adapter; |
---|
| 3826 | info->offset = offset; |
---|
| 3827 | info->value = value; |
---|
| 3828 | SYSCTL_ADD_PROC(device_get_sysctl_ctx(adapter->dev), |
---|
| 3829 | SYSCTL_CHILDREN(device_get_sysctl_tree(adapter->dev)), |
---|
| 3830 | OID_AUTO, name, CTLTYPE_INT|CTLFLAG_RW, |
---|
| 3831 | info, 0, em_sysctl_int_delay, "I", description); |
---|
| 3832 | } |
---|
| 3833 | #endif |
---|
| 3834 | |
---|
| 3835 | #ifdef __rtems__ |
---|
| 3836 | /* Initialize bare minimals so we can check the phy link status */ |
---|
| 3837 | int |
---|
| 3838 | em_hw_early_init(device_t dev) |
---|
| 3839 | { |
---|
| 3840 | struct adapter *adapter = device_get_softc(dev); |
---|
| 3841 | adapter->dev = dev; |
---|
| 3842 | adapter->osdep.dev = dev; |
---|
| 3843 | em_identify_hardware(adapter); |
---|
| 3844 | return em_allocate_pci_resources(adapter); |
---|
| 3845 | } |
---|
| 3846 | #endif |
---|