Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Network device driver for Cell Processor-Based Blade |
| 3 | * |
| 4 | * (C) Copyright IBM Corp. 2005 |
| 5 | * |
| 6 | * Authors : Utz Bacher <utz.bacher@de.ibm.com> |
| 7 | * Jens Osterkamp <Jens.Osterkamp@de.ibm.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2, or (at your option) |
| 12 | * any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | */ |
| 23 | |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 24 | #include <linux/compiler.h> |
| 25 | #include <linux/crc32.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/etherdevice.h> |
| 28 | #include <linux/ethtool.h> |
| 29 | #include <linux/firmware.h> |
| 30 | #include <linux/if_vlan.h> |
Arnd Bergmann | 7c5c220 | 2006-01-12 17:16:45 -0500 | [diff] [blame] | 31 | #include <linux/in.h> |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 32 | #include <linux/init.h> |
| 33 | #include <linux/ioport.h> |
| 34 | #include <linux/ip.h> |
| 35 | #include <linux/kernel.h> |
| 36 | #include <linux/mii.h> |
| 37 | #include <linux/module.h> |
| 38 | #include <linux/netdevice.h> |
| 39 | #include <linux/device.h> |
| 40 | #include <linux/pci.h> |
| 41 | #include <linux/skbuff.h> |
| 42 | #include <linux/slab.h> |
| 43 | #include <linux/tcp.h> |
| 44 | #include <linux/types.h> |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 45 | #include <linux/vmalloc.h> |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 46 | #include <linux/wait.h> |
| 47 | #include <linux/workqueue.h> |
| 48 | #include <asm/bitops.h> |
| 49 | #include <asm/pci-bridge.h> |
| 50 | #include <net/checksum.h> |
| 51 | |
| 52 | #include "spider_net.h" |
| 53 | |
| 54 | MODULE_AUTHOR("Utz Bacher <utz.bacher@de.ibm.com> and Jens Osterkamp " \ |
| 55 | "<Jens.Osterkamp@de.ibm.com>"); |
| 56 | MODULE_DESCRIPTION("Spider Southbridge Gigabit Ethernet driver"); |
| 57 | MODULE_LICENSE("GPL"); |
Linas Vepstas | 90f1084 | 2006-10-10 15:56:04 -0500 | [diff] [blame] | 58 | MODULE_VERSION(VERSION); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 59 | |
| 60 | static int rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_DEFAULT; |
| 61 | static int tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_DEFAULT; |
| 62 | |
Linas Vepstas | e2874f2 | 2006-10-10 15:59:02 -0500 | [diff] [blame] | 63 | module_param(rx_descriptors, int, 0444); |
| 64 | module_param(tx_descriptors, int, 0444); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 65 | |
| 66 | MODULE_PARM_DESC(rx_descriptors, "number of descriptors used " \ |
| 67 | "in rx chains"); |
| 68 | MODULE_PARM_DESC(tx_descriptors, "number of descriptors used " \ |
| 69 | "in tx chain"); |
| 70 | |
| 71 | char spider_net_driver_name[] = "spidernet"; |
| 72 | |
| 73 | static struct pci_device_id spider_net_pci_tbl[] = { |
| 74 | { PCI_VENDOR_ID_TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_SPIDER_NET, |
| 75 | PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL }, |
| 76 | { 0, } |
| 77 | }; |
| 78 | |
| 79 | MODULE_DEVICE_TABLE(pci, spider_net_pci_tbl); |
| 80 | |
| 81 | /** |
| 82 | * spider_net_read_reg - reads an SMMIO register of a card |
| 83 | * @card: device structure |
| 84 | * @reg: register to read from |
| 85 | * |
| 86 | * returns the content of the specified SMMIO register. |
| 87 | */ |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 88 | static inline u32 |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 89 | spider_net_read_reg(struct spider_net_card *card, u32 reg) |
| 90 | { |
Benjamin Herrenschmidt | 3bc0f40 | 2006-11-11 17:25:13 +1100 | [diff] [blame] | 91 | /* We use the powerpc specific variants instead of readl_be() because |
| 92 | * we know spidernet is not a real PCI device and we can thus avoid the |
| 93 | * performance hit caused by the PCI workarounds. |
| 94 | */ |
| 95 | return in_be32(card->regs + reg); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * spider_net_write_reg - writes to an SMMIO register of a card |
| 100 | * @card: device structure |
| 101 | * @reg: register to write to |
| 102 | * @value: value to write into the specified SMMIO register |
| 103 | */ |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 104 | static inline void |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 105 | spider_net_write_reg(struct spider_net_card *card, u32 reg, u32 value) |
| 106 | { |
Benjamin Herrenschmidt | 3bc0f40 | 2006-11-11 17:25:13 +1100 | [diff] [blame] | 107 | /* We use the powerpc specific variants instead of writel_be() because |
| 108 | * we know spidernet is not a real PCI device and we can thus avoid the |
| 109 | * performance hit caused by the PCI workarounds. |
| 110 | */ |
| 111 | out_be32(card->regs + reg, value); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 114 | /** spider_net_write_phy - write to phy register |
| 115 | * @netdev: adapter to be written to |
| 116 | * @mii_id: id of MII |
| 117 | * @reg: PHY register |
| 118 | * @val: value to be written to phy register |
| 119 | * |
| 120 | * spider_net_write_phy_register writes to an arbitrary PHY |
| 121 | * register via the spider GPCWOPCMD register. We assume the queue does |
| 122 | * not run full (not more than 15 commands outstanding). |
| 123 | **/ |
| 124 | static void |
| 125 | spider_net_write_phy(struct net_device *netdev, int mii_id, |
| 126 | int reg, int val) |
| 127 | { |
| 128 | struct spider_net_card *card = netdev_priv(netdev); |
| 129 | u32 writevalue; |
| 130 | |
| 131 | writevalue = ((u32)mii_id << 21) | |
| 132 | ((u32)reg << 16) | ((u32)val); |
| 133 | |
| 134 | spider_net_write_reg(card, SPIDER_NET_GPCWOPCMD, writevalue); |
| 135 | } |
| 136 | |
| 137 | /** spider_net_read_phy - read from phy register |
| 138 | * @netdev: network device to be read from |
| 139 | * @mii_id: id of MII |
| 140 | * @reg: PHY register |
| 141 | * |
| 142 | * Returns value read from PHY register |
| 143 | * |
| 144 | * spider_net_write_phy reads from an arbitrary PHY |
| 145 | * register via the spider GPCROPCMD register |
| 146 | **/ |
| 147 | static int |
| 148 | spider_net_read_phy(struct net_device *netdev, int mii_id, int reg) |
| 149 | { |
| 150 | struct spider_net_card *card = netdev_priv(netdev); |
| 151 | u32 readvalue; |
| 152 | |
| 153 | readvalue = ((u32)mii_id << 21) | ((u32)reg << 16); |
| 154 | spider_net_write_reg(card, SPIDER_NET_GPCROPCMD, readvalue); |
| 155 | |
| 156 | /* we don't use semaphores to wait for an SPIDER_NET_GPROPCMPINT |
| 157 | * interrupt, as we poll for the completion of the read operation |
| 158 | * in spider_net_read_phy. Should take about 50 us */ |
| 159 | do { |
| 160 | readvalue = spider_net_read_reg(card, SPIDER_NET_GPCROPCMD); |
| 161 | } while (readvalue & SPIDER_NET_GPREXEC); |
| 162 | |
| 163 | readvalue &= SPIDER_NET_GPRDAT_MASK; |
| 164 | |
| 165 | return readvalue; |
| 166 | } |
| 167 | |
| 168 | /** |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 169 | * spider_net_rx_irq_off - switch off rx irq on this spider card |
| 170 | * @card: device structure |
| 171 | * |
| 172 | * switches off rx irq by masking them out in the GHIINTnMSK register |
| 173 | */ |
| 174 | static void |
| 175 | spider_net_rx_irq_off(struct spider_net_card *card) |
| 176 | { |
| 177 | u32 regvalue; |
| 178 | |
| 179 | regvalue = SPIDER_NET_INT0_MASK_VALUE & (~SPIDER_NET_RXINT); |
| 180 | spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue); |
| 181 | } |
| 182 | |
| 183 | /** |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 184 | * spider_net_rx_irq_on - switch on rx irq on this spider card |
| 185 | * @card: device structure |
| 186 | * |
| 187 | * switches on rx irq by enabling them in the GHIINTnMSK register |
| 188 | */ |
| 189 | static void |
| 190 | spider_net_rx_irq_on(struct spider_net_card *card) |
| 191 | { |
| 192 | u32 regvalue; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 193 | |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 194 | regvalue = SPIDER_NET_INT0_MASK_VALUE | SPIDER_NET_RXINT; |
| 195 | spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, regvalue); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /** |
| 199 | * spider_net_set_promisc - sets the unicast address or the promiscuous mode |
| 200 | * @card: card structure |
| 201 | * |
| 202 | * spider_net_set_promisc sets the unicast destination address filter and |
| 203 | * thus either allows for non-promisc mode or promisc mode |
| 204 | */ |
| 205 | static void |
| 206 | spider_net_set_promisc(struct spider_net_card *card) |
| 207 | { |
| 208 | u32 macu, macl; |
| 209 | struct net_device *netdev = card->netdev; |
| 210 | |
| 211 | if (netdev->flags & IFF_PROMISC) { |
| 212 | /* clear destination entry 0 */ |
| 213 | spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, 0); |
| 214 | spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, 0); |
| 215 | spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, |
| 216 | SPIDER_NET_PROMISC_VALUE); |
| 217 | } else { |
| 218 | macu = netdev->dev_addr[0]; |
| 219 | macu <<= 8; |
| 220 | macu |= netdev->dev_addr[1]; |
| 221 | memcpy(&macl, &netdev->dev_addr[2], sizeof(macl)); |
| 222 | |
| 223 | macu |= SPIDER_NET_UA_DESCR_VALUE; |
| 224 | spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR, macu); |
| 225 | spider_net_write_reg(card, SPIDER_NET_GMRUAFILnR + 0x04, macl); |
| 226 | spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, |
| 227 | SPIDER_NET_NONPROMISC_VALUE); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * spider_net_get_mac_address - read mac address from spider card |
| 233 | * @card: device structure |
| 234 | * |
| 235 | * reads MAC address from GMACUNIMACU and GMACUNIMACL registers |
| 236 | */ |
| 237 | static int |
| 238 | spider_net_get_mac_address(struct net_device *netdev) |
| 239 | { |
| 240 | struct spider_net_card *card = netdev_priv(netdev); |
| 241 | u32 macl, macu; |
| 242 | |
| 243 | macl = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACL); |
| 244 | macu = spider_net_read_reg(card, SPIDER_NET_GMACUNIMACU); |
| 245 | |
| 246 | netdev->dev_addr[0] = (macu >> 24) & 0xff; |
| 247 | netdev->dev_addr[1] = (macu >> 16) & 0xff; |
| 248 | netdev->dev_addr[2] = (macu >> 8) & 0xff; |
| 249 | netdev->dev_addr[3] = macu & 0xff; |
| 250 | netdev->dev_addr[4] = (macl >> 8) & 0xff; |
| 251 | netdev->dev_addr[5] = macl & 0xff; |
| 252 | |
| 253 | if (!is_valid_ether_addr(&netdev->dev_addr[0])) |
| 254 | return -EINVAL; |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * spider_net_get_descr_status -- returns the status of a descriptor |
| 261 | * @descr: descriptor to look at |
| 262 | * |
| 263 | * returns the status as in the dmac_cmd_status field of the descriptor |
| 264 | */ |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 265 | static inline int |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 266 | spider_net_get_descr_status(struct spider_net_descr *descr) |
| 267 | { |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 268 | return descr->dmac_cmd_status & SPIDER_NET_DESCR_IND_PROC_MASK; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** |
| 272 | * spider_net_free_chain - free descriptor chain |
| 273 | * @card: card structure |
| 274 | * @chain: address of chain |
| 275 | * |
| 276 | */ |
| 277 | static void |
| 278 | spider_net_free_chain(struct spider_net_card *card, |
| 279 | struct spider_net_descr_chain *chain) |
| 280 | { |
| 281 | struct spider_net_descr *descr; |
| 282 | |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 283 | descr = chain->ring; |
| 284 | do { |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 285 | descr->bus_addr = 0; |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 286 | descr->next_descr_addr = 0; |
| 287 | descr = descr->next; |
| 288 | } while (descr != chain->ring); |
| 289 | |
| 290 | dma_free_coherent(&card->pdev->dev, chain->num_desc, |
| 291 | chain->ring, chain->dma_addr); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /** |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 295 | * spider_net_init_chain - alloc and link descriptor chain |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 296 | * @card: card structure |
| 297 | * @chain: address of chain |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 298 | * |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 299 | * We manage a circular list that mirrors the hardware structure, |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 300 | * except that the hardware uses bus addresses. |
| 301 | * |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 302 | * Returns 0 on success, <0 on failure |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 303 | */ |
| 304 | static int |
| 305 | spider_net_init_chain(struct spider_net_card *card, |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 306 | struct spider_net_descr_chain *chain) |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 307 | { |
| 308 | int i; |
| 309 | struct spider_net_descr *descr; |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 310 | dma_addr_t buf; |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 311 | size_t alloc_size; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 312 | |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 313 | alloc_size = chain->num_desc * sizeof (struct spider_net_descr); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 314 | |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 315 | chain->ring = dma_alloc_coherent(&card->pdev->dev, alloc_size, |
| 316 | &chain->dma_addr, GFP_KERNEL); |
| 317 | |
| 318 | if (!chain->ring) |
| 319 | return -ENOMEM; |
| 320 | |
| 321 | descr = chain->ring; |
| 322 | memset(descr, 0, alloc_size); |
| 323 | |
| 324 | /* Set up the hardware pointers in each descriptor */ |
| 325 | buf = chain->dma_addr; |
| 326 | for (i=0; i < chain->num_desc; i++, descr++) { |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 327 | descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 328 | |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 329 | descr->bus_addr = buf; |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 330 | descr->next_descr_addr = 0; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 331 | descr->next = descr + 1; |
| 332 | descr->prev = descr - 1; |
| 333 | |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 334 | buf += sizeof(struct spider_net_descr); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 335 | } |
| 336 | /* do actual circular list */ |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 337 | (descr-1)->next = chain->ring; |
| 338 | chain->ring->prev = descr-1; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 339 | |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 340 | spin_lock_init(&chain->lock); |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 341 | chain->head = chain->ring; |
| 342 | chain->tail = chain->ring; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 343 | return 0; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /** |
| 347 | * spider_net_free_rx_chain_contents - frees descr contents in rx chain |
| 348 | * @card: card structure |
| 349 | * |
| 350 | * returns 0 on success, <0 on failure |
| 351 | */ |
| 352 | static void |
| 353 | spider_net_free_rx_chain_contents(struct spider_net_card *card) |
| 354 | { |
| 355 | struct spider_net_descr *descr; |
| 356 | |
| 357 | descr = card->rx_chain.head; |
Linas Vepstas | 6475191 | 2006-10-10 16:22:29 -0500 | [diff] [blame] | 358 | do { |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 359 | if (descr->skb) { |
| 360 | dev_kfree_skb(descr->skb); |
| 361 | pci_unmap_single(card->pdev, descr->buf_addr, |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 362 | SPIDER_NET_MAX_FRAME, |
Linas Vepstas | 348bc2a | 2006-10-10 16:21:10 -0500 | [diff] [blame] | 363 | PCI_DMA_BIDIRECTIONAL); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 364 | } |
| 365 | descr = descr->next; |
Linas Vepstas | 6475191 | 2006-10-10 16:22:29 -0500 | [diff] [blame] | 366 | } while (descr != card->rx_chain.head); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /** |
Linas Vepstas | a4182c5 | 2006-12-13 15:19:54 -0600 | [diff] [blame] | 370 | * spider_net_prepare_rx_descr - Reinitialize RX descriptor |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 371 | * @card: card structure |
| 372 | * @descr: descriptor to re-init |
| 373 | * |
Linas Vepstas | a4182c5 | 2006-12-13 15:19:54 -0600 | [diff] [blame] | 374 | * Return 0 on succes, <0 on failure. |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 375 | * |
Linas Vepstas | a4182c5 | 2006-12-13 15:19:54 -0600 | [diff] [blame] | 376 | * Allocates a new rx skb, iommu-maps it and attaches it to the |
| 377 | * descriptor. Mark the descriptor as activated, ready-to-use. |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 378 | */ |
| 379 | static int |
| 380 | spider_net_prepare_rx_descr(struct spider_net_card *card, |
| 381 | struct spider_net_descr *descr) |
| 382 | { |
Arnd Bergmann | 8e0a613 | 2006-01-12 17:16:43 -0500 | [diff] [blame] | 383 | dma_addr_t buf; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 384 | int offset; |
| 385 | int bufsize; |
| 386 | |
| 387 | /* we need to round up the buffer size to a multiple of 128 */ |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 388 | bufsize = (SPIDER_NET_MAX_FRAME + SPIDER_NET_RXBUF_ALIGN - 1) & |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 389 | (~(SPIDER_NET_RXBUF_ALIGN - 1)); |
| 390 | |
| 391 | /* and we need to have it 128 byte aligned, therefore we allocate a |
| 392 | * bit more */ |
| 393 | /* allocate an skb */ |
| 394 | descr->skb = dev_alloc_skb(bufsize + SPIDER_NET_RXBUF_ALIGN - 1); |
| 395 | if (!descr->skb) { |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 396 | if (netif_msg_rx_err(card) && net_ratelimit()) |
| 397 | pr_err("Not enough memory to allocate rx buffer\n"); |
Jim Lewis | 9b6b0b8 | 2006-09-22 17:22:53 -0700 | [diff] [blame] | 398 | card->spider_stats.alloc_rx_skb_error++; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 399 | return -ENOMEM; |
| 400 | } |
| 401 | descr->buf_size = bufsize; |
| 402 | descr->result_size = 0; |
| 403 | descr->valid_size = 0; |
| 404 | descr->data_status = 0; |
| 405 | descr->data_error = 0; |
| 406 | |
| 407 | offset = ((unsigned long)descr->skb->data) & |
| 408 | (SPIDER_NET_RXBUF_ALIGN - 1); |
| 409 | if (offset) |
| 410 | skb_reserve(descr->skb, SPIDER_NET_RXBUF_ALIGN - offset); |
Linas Vepstas | a4182c5 | 2006-12-13 15:19:54 -0600 | [diff] [blame] | 411 | /* iommu-map the skb */ |
Arnd Bergmann | 8e0a613 | 2006-01-12 17:16:43 -0500 | [diff] [blame] | 412 | buf = pci_map_single(card->pdev, descr->skb->data, |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 413 | SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE); |
Arnd Bergmann | 8e0a613 | 2006-01-12 17:16:43 -0500 | [diff] [blame] | 414 | descr->buf_addr = buf; |
Olof Johansson | d4b0a4c | 2006-09-27 14:02:26 -0500 | [diff] [blame] | 415 | if (pci_dma_mapping_error(buf)) { |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 416 | dev_kfree_skb_any(descr->skb); |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 417 | if (netif_msg_rx_err(card) && net_ratelimit()) |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 418 | pr_err("Could not iommu-map rx buffer\n"); |
Jim Lewis | 9b6b0b8 | 2006-09-22 17:22:53 -0700 | [diff] [blame] | 419 | card->spider_stats.rx_iommu_map_error++; |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 420 | descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 421 | } else { |
Linas Vepstas | d9a9720 | 2007-02-05 16:29:43 -0800 | [diff] [blame^] | 422 | descr->next_descr_addr = 0; |
Linas Vepstas | 90476a2 | 2006-12-13 15:22:04 -0600 | [diff] [blame] | 423 | wmb(); |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 424 | descr->dmac_cmd_status = SPIDER_NET_DESCR_CARDOWNED | |
| 425 | SPIDER_NET_DMAC_NOINTR_COMPLETE; |
Linas Vepstas | d9a9720 | 2007-02-05 16:29:43 -0800 | [diff] [blame^] | 426 | |
| 427 | wmb(); |
| 428 | descr->prev->next_descr_addr = descr->bus_addr; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Linas Vepstas | a4182c5 | 2006-12-13 15:19:54 -0600 | [diff] [blame] | 431 | return 0; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /** |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 435 | * spider_net_enable_rxchtails - sets RX dmac chain tail addresses |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 436 | * @card: card structure |
| 437 | * |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 438 | * spider_net_enable_rxchtails sets the RX DMAC chain tail adresses in the |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 439 | * chip by writing to the appropriate register. DMA is enabled in |
| 440 | * spider_net_enable_rxdmac. |
| 441 | */ |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 442 | static inline void |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 443 | spider_net_enable_rxchtails(struct spider_net_card *card) |
| 444 | { |
| 445 | /* assume chain is aligned correctly */ |
| 446 | spider_net_write_reg(card, SPIDER_NET_GDADCHA , |
| 447 | card->rx_chain.tail->bus_addr); |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * spider_net_enable_rxdmac - enables a receive DMA controller |
| 452 | * @card: card structure |
| 453 | * |
| 454 | * spider_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN |
| 455 | * in the GDADMACCNTR register |
| 456 | */ |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 457 | static inline void |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 458 | spider_net_enable_rxdmac(struct spider_net_card *card) |
| 459 | { |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 460 | wmb(); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 461 | spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR, |
| 462 | SPIDER_NET_DMA_RX_VALUE); |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * spider_net_refill_rx_chain - refills descriptors/skbs in the rx chains |
| 467 | * @card: card structure |
| 468 | * |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 469 | * refills descriptors in the rx chain: allocates skbs and iommu-maps them. |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 470 | */ |
| 471 | static void |
| 472 | spider_net_refill_rx_chain(struct spider_net_card *card) |
| 473 | { |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 474 | struct spider_net_descr_chain *chain = &card->rx_chain; |
| 475 | unsigned long flags; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 476 | |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 477 | /* one context doing the refill (and a second context seeing that |
| 478 | * and omitting it) is ok. If called by NAPI, we'll be called again |
| 479 | * as spider_net_decode_one_descr is called several times. If some |
| 480 | * interrupt calls us, the NAPI is about to clean up anyway. */ |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 481 | if (!spin_trylock_irqsave(&chain->lock, flags)) |
| 482 | return; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 483 | |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 484 | while (spider_net_get_descr_status(chain->head) == |
| 485 | SPIDER_NET_DESCR_NOT_IN_USE) { |
| 486 | if (spider_net_prepare_rx_descr(card, chain->head)) |
| 487 | break; |
| 488 | chain->head = chain->head->next; |
| 489 | } |
| 490 | |
| 491 | spin_unlock_irqrestore(&chain->lock, flags); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | /** |
Linas Vepstas | 2c307db | 2006-12-13 15:20:59 -0600 | [diff] [blame] | 495 | * spider_net_alloc_rx_skbs - Allocates rx skbs in rx descriptor chains |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 496 | * @card: card structure |
| 497 | * |
Linas Vepstas | 2c307db | 2006-12-13 15:20:59 -0600 | [diff] [blame] | 498 | * Returns 0 on success, <0 on failure. |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 499 | */ |
| 500 | static int |
| 501 | spider_net_alloc_rx_skbs(struct spider_net_card *card) |
| 502 | { |
| 503 | int result; |
| 504 | struct spider_net_descr_chain *chain; |
| 505 | |
| 506 | result = -ENOMEM; |
| 507 | |
| 508 | chain = &card->rx_chain; |
Linas Vepstas | 2c307db | 2006-12-13 15:20:59 -0600 | [diff] [blame] | 509 | /* Put at least one buffer into the chain. if this fails, |
| 510 | * we've got a problem. If not, spider_net_refill_rx_chain |
| 511 | * will do the rest at the end of this function. */ |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 512 | if (spider_net_prepare_rx_descr(card, chain->head)) |
| 513 | goto error; |
| 514 | else |
| 515 | chain->head = chain->head->next; |
| 516 | |
Linas Vepstas | 2c307db | 2006-12-13 15:20:59 -0600 | [diff] [blame] | 517 | /* This will allocate the rest of the rx buffers; |
| 518 | * if not, it's business as usual later on. */ |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 519 | spider_net_refill_rx_chain(card); |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 520 | spider_net_enable_rxdmac(card); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 521 | return 0; |
| 522 | |
| 523 | error: |
| 524 | spider_net_free_rx_chain_contents(card); |
| 525 | return result; |
| 526 | } |
| 527 | |
| 528 | /** |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 529 | * spider_net_get_multicast_hash - generates hash for multicast filter table |
| 530 | * @addr: multicast address |
| 531 | * |
| 532 | * returns the hash value. |
| 533 | * |
| 534 | * spider_net_get_multicast_hash calculates a hash value for a given multicast |
| 535 | * address, that is used to set the multicast filter tables |
| 536 | */ |
| 537 | static u8 |
| 538 | spider_net_get_multicast_hash(struct net_device *netdev, __u8 *addr) |
| 539 | { |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 540 | u32 crc; |
| 541 | u8 hash; |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 542 | char addr_for_crc[ETH_ALEN] = { 0, }; |
| 543 | int i, bit; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 544 | |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 545 | for (i = 0; i < ETH_ALEN * 8; i++) { |
| 546 | bit = (addr[i / 8] >> (i % 8)) & 1; |
| 547 | addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8)); |
| 548 | } |
| 549 | |
| 550 | crc = crc32_be(~0, addr_for_crc, netdev->addr_len); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 551 | |
| 552 | hash = (crc >> 27); |
| 553 | hash <<= 3; |
| 554 | hash |= crc & 7; |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 555 | hash &= 0xff; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 556 | |
| 557 | return hash; |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * spider_net_set_multi - sets multicast addresses and promisc flags |
| 562 | * @netdev: interface device structure |
| 563 | * |
| 564 | * spider_net_set_multi configures multicast addresses as needed for the |
| 565 | * netdev interface. It also sets up multicast, allmulti and promisc |
| 566 | * flags appropriately |
| 567 | */ |
| 568 | static void |
| 569 | spider_net_set_multi(struct net_device *netdev) |
| 570 | { |
| 571 | struct dev_mc_list *mc; |
| 572 | u8 hash; |
| 573 | int i; |
| 574 | u32 reg; |
| 575 | struct spider_net_card *card = netdev_priv(netdev); |
| 576 | unsigned long bitmask[SPIDER_NET_MULTICAST_HASHES / BITS_PER_LONG] = |
| 577 | {0, }; |
| 578 | |
| 579 | spider_net_set_promisc(card); |
| 580 | |
| 581 | if (netdev->flags & IFF_ALLMULTI) { |
| 582 | for (i = 0; i < SPIDER_NET_MULTICAST_HASHES; i++) { |
| 583 | set_bit(i, bitmask); |
| 584 | } |
| 585 | goto write_hash; |
| 586 | } |
| 587 | |
| 588 | /* well, we know, what the broadcast hash value is: it's xfd |
| 589 | hash = spider_net_get_multicast_hash(netdev, netdev->broadcast); */ |
| 590 | set_bit(0xfd, bitmask); |
| 591 | |
| 592 | for (mc = netdev->mc_list; mc; mc = mc->next) { |
| 593 | hash = spider_net_get_multicast_hash(netdev, mc->dmi_addr); |
| 594 | set_bit(hash, bitmask); |
| 595 | } |
| 596 | |
| 597 | write_hash: |
| 598 | for (i = 0; i < SPIDER_NET_MULTICAST_HASHES / 4; i++) { |
| 599 | reg = 0; |
| 600 | if (test_bit(i * 4, bitmask)) |
| 601 | reg += 0x08; |
| 602 | reg <<= 8; |
| 603 | if (test_bit(i * 4 + 1, bitmask)) |
| 604 | reg += 0x08; |
| 605 | reg <<= 8; |
| 606 | if (test_bit(i * 4 + 2, bitmask)) |
| 607 | reg += 0x08; |
| 608 | reg <<= 8; |
| 609 | if (test_bit(i * 4 + 3, bitmask)) |
| 610 | reg += 0x08; |
| 611 | |
| 612 | spider_net_write_reg(card, SPIDER_NET_GMRMHFILnR + i * 4, reg); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | /** |
| 617 | * spider_net_disable_rxdmac - disables the receive DMA controller |
| 618 | * @card: card structure |
| 619 | * |
| 620 | * spider_net_disable_rxdmac terminates processing on the DMA controller by |
| 621 | * turing off DMA and issueing a force end |
| 622 | */ |
| 623 | static void |
| 624 | spider_net_disable_rxdmac(struct spider_net_card *card) |
| 625 | { |
| 626 | spider_net_write_reg(card, SPIDER_NET_GDADMACCNTR, |
| 627 | SPIDER_NET_DMA_RX_FEND_VALUE); |
| 628 | } |
| 629 | |
| 630 | /** |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 631 | * spider_net_prepare_tx_descr - fill tx descriptor with skb data |
| 632 | * @card: card structure |
| 633 | * @descr: descriptor structure to fill out |
| 634 | * @skb: packet to use |
| 635 | * |
| 636 | * returns 0 on success, <0 on failure. |
| 637 | * |
| 638 | * fills out the descriptor structure with skb data and len. Copies data, |
| 639 | * if needed (32bit DMA!) |
| 640 | */ |
| 641 | static int |
| 642 | spider_net_prepare_tx_descr(struct spider_net_card *card, |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 643 | struct sk_buff *skb) |
| 644 | { |
Linas Vepstas | 9cc7bf7 | 2006-10-10 16:14:29 -0500 | [diff] [blame] | 645 | struct spider_net_descr *descr; |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 646 | dma_addr_t buf; |
Linas Vepstas | 9cc7bf7 | 2006-10-10 16:14:29 -0500 | [diff] [blame] | 647 | unsigned long flags; |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 648 | |
James K Lewis | 9c434f5 | 2006-11-17 14:39:36 -0800 | [diff] [blame] | 649 | buf = pci_map_single(card->pdev, skb->data, skb->len, PCI_DMA_TODEVICE); |
Olof Johansson | d4b0a4c | 2006-09-27 14:02:26 -0500 | [diff] [blame] | 650 | if (pci_dma_mapping_error(buf)) { |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 651 | if (netif_msg_tx_err(card) && net_ratelimit()) |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 652 | pr_err("could not iommu-map packet (%p, %i). " |
James K Lewis | 9c434f5 | 2006-11-17 14:39:36 -0800 | [diff] [blame] | 653 | "Dropping packet\n", skb->data, skb->len); |
Jim Lewis | 9b6b0b8 | 2006-09-22 17:22:53 -0700 | [diff] [blame] | 654 | card->spider_stats.tx_iommu_map_error++; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 655 | return -ENOMEM; |
| 656 | } |
| 657 | |
Linas Vepstas | 9cc7bf7 | 2006-10-10 16:14:29 -0500 | [diff] [blame] | 658 | spin_lock_irqsave(&card->tx_chain.lock, flags); |
| 659 | descr = card->tx_chain.head; |
| 660 | card->tx_chain.head = descr->next; |
| 661 | |
Arnd Bergmann | 8e0a613 | 2006-01-12 17:16:43 -0500 | [diff] [blame] | 662 | descr->buf_addr = buf; |
James K Lewis | 9c434f5 | 2006-11-17 14:39:36 -0800 | [diff] [blame] | 663 | descr->buf_size = skb->len; |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 664 | descr->next_descr_addr = 0; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 665 | descr->skb = skb; |
| 666 | descr->data_status = 0; |
| 667 | |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 668 | descr->dmac_cmd_status = |
| 669 | SPIDER_NET_DESCR_CARDOWNED | SPIDER_NET_DMAC_NOCS; |
Linas Vepstas | 9cc7bf7 | 2006-10-10 16:14:29 -0500 | [diff] [blame] | 670 | spin_unlock_irqrestore(&card->tx_chain.lock, flags); |
| 671 | |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 672 | if (skb->protocol == htons(ETH_P_IP)) |
| 673 | switch (skb->nh.iph->protocol) { |
| 674 | case IPPROTO_TCP: |
| 675 | descr->dmac_cmd_status |= SPIDER_NET_DMAC_TCP; |
| 676 | break; |
| 677 | case IPPROTO_UDP: |
| 678 | descr->dmac_cmd_status |= SPIDER_NET_DMAC_UDP; |
| 679 | break; |
| 680 | } |
| 681 | |
Linas Vepstas | 204e5fa | 2006-10-10 16:11:33 -0500 | [diff] [blame] | 682 | /* Chain the bus address, so that the DMA engine finds this descr. */ |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 683 | descr->prev->next_descr_addr = descr->bus_addr; |
| 684 | |
Linas Vepstas | 917a5b8 | 2006-10-10 16:01:51 -0500 | [diff] [blame] | 685 | card->netdev->trans_start = jiffies; /* set netdev watchdog timer */ |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 686 | return 0; |
| 687 | } |
| 688 | |
Linas Vepstas | a664ccf | 2006-10-10 16:18:18 -0500 | [diff] [blame] | 689 | static int |
Linas Vepstas | 204e5fa | 2006-10-10 16:11:33 -0500 | [diff] [blame] | 690 | spider_net_set_low_watermark(struct spider_net_card *card) |
| 691 | { |
Linas Vepstas | 9cc7bf7 | 2006-10-10 16:14:29 -0500 | [diff] [blame] | 692 | unsigned long flags; |
Linas Vepstas | 204e5fa | 2006-10-10 16:11:33 -0500 | [diff] [blame] | 693 | int status; |
| 694 | int cnt=0; |
| 695 | int i; |
| 696 | struct spider_net_descr *descr = card->tx_chain.tail; |
| 697 | |
Linas Vepstas | 9cc7bf7 | 2006-10-10 16:14:29 -0500 | [diff] [blame] | 698 | /* Measure the length of the queue. Measurement does not |
| 699 | * need to be precise -- does not need a lock. */ |
Linas Vepstas | 204e5fa | 2006-10-10 16:11:33 -0500 | [diff] [blame] | 700 | while (descr != card->tx_chain.head) { |
| 701 | status = descr->dmac_cmd_status & SPIDER_NET_DESCR_NOT_IN_USE; |
| 702 | if (status == SPIDER_NET_DESCR_NOT_IN_USE) |
| 703 | break; |
| 704 | descr = descr->next; |
| 705 | cnt++; |
| 706 | } |
| 707 | |
| 708 | /* If TX queue is short, don't even bother with interrupts */ |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 709 | if (cnt < card->tx_chain.num_desc/4) |
Linas Vepstas | a664ccf | 2006-10-10 16:18:18 -0500 | [diff] [blame] | 710 | return cnt; |
Linas Vepstas | 204e5fa | 2006-10-10 16:11:33 -0500 | [diff] [blame] | 711 | |
| 712 | /* Set low-watermark 3/4th's of the way into the queue. */ |
| 713 | descr = card->tx_chain.tail; |
| 714 | cnt = (cnt*3)/4; |
| 715 | for (i=0;i<cnt; i++) |
| 716 | descr = descr->next; |
| 717 | |
| 718 | /* Set the new watermark, clear the old watermark */ |
Linas Vepstas | 9cc7bf7 | 2006-10-10 16:14:29 -0500 | [diff] [blame] | 719 | spin_lock_irqsave(&card->tx_chain.lock, flags); |
Linas Vepstas | 204e5fa | 2006-10-10 16:11:33 -0500 | [diff] [blame] | 720 | descr->dmac_cmd_status |= SPIDER_NET_DESCR_TXDESFLG; |
| 721 | if (card->low_watermark && card->low_watermark != descr) |
| 722 | card->low_watermark->dmac_cmd_status = |
| 723 | card->low_watermark->dmac_cmd_status & ~SPIDER_NET_DESCR_TXDESFLG; |
| 724 | card->low_watermark = descr; |
Linas Vepstas | 9cc7bf7 | 2006-10-10 16:14:29 -0500 | [diff] [blame] | 725 | spin_unlock_irqrestore(&card->tx_chain.lock, flags); |
Linas Vepstas | a664ccf | 2006-10-10 16:18:18 -0500 | [diff] [blame] | 726 | return cnt; |
Linas Vepstas | 204e5fa | 2006-10-10 16:11:33 -0500 | [diff] [blame] | 727 | } |
| 728 | |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 729 | /** |
| 730 | * spider_net_release_tx_chain - processes sent tx descriptors |
| 731 | * @card: adapter structure |
| 732 | * @brutal: if set, don't care about whether descriptor seems to be in use |
| 733 | * |
| 734 | * returns 0 if the tx ring is empty, otherwise 1. |
| 735 | * |
| 736 | * spider_net_release_tx_chain releases the tx descriptors that spider has |
| 737 | * finished with (if non-brutal) or simply release tx descriptors (if brutal). |
| 738 | * If some other context is calling this function, we return 1 so that we're |
| 739 | * scheduled again (if we were scheduled) and will not loose initiative. |
| 740 | */ |
| 741 | static int |
| 742 | spider_net_release_tx_chain(struct spider_net_card *card, int brutal) |
| 743 | { |
| 744 | struct spider_net_descr_chain *chain = &card->tx_chain; |
Linas Vepstas | 9cc7bf7 | 2006-10-10 16:14:29 -0500 | [diff] [blame] | 745 | struct spider_net_descr *descr; |
| 746 | struct sk_buff *skb; |
| 747 | u32 buf_addr; |
| 748 | unsigned long flags; |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 749 | int status; |
| 750 | |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 751 | while (chain->tail != chain->head) { |
Linas Vepstas | 9cc7bf7 | 2006-10-10 16:14:29 -0500 | [diff] [blame] | 752 | spin_lock_irqsave(&chain->lock, flags); |
| 753 | descr = chain->tail; |
| 754 | |
| 755 | status = spider_net_get_descr_status(descr); |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 756 | switch (status) { |
| 757 | case SPIDER_NET_DESCR_COMPLETE: |
| 758 | card->netdev_stats.tx_packets++; |
Linas Vepstas | 9cc7bf7 | 2006-10-10 16:14:29 -0500 | [diff] [blame] | 759 | card->netdev_stats.tx_bytes += descr->skb->len; |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 760 | break; |
| 761 | |
| 762 | case SPIDER_NET_DESCR_CARDOWNED: |
Linas Vepstas | 9cc7bf7 | 2006-10-10 16:14:29 -0500 | [diff] [blame] | 763 | if (!brutal) { |
| 764 | spin_unlock_irqrestore(&chain->lock, flags); |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 765 | return 1; |
Linas Vepstas | 9cc7bf7 | 2006-10-10 16:14:29 -0500 | [diff] [blame] | 766 | } |
| 767 | |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 768 | /* fallthrough, if we release the descriptors |
| 769 | * brutally (then we don't care about |
| 770 | * SPIDER_NET_DESCR_CARDOWNED) */ |
| 771 | |
| 772 | case SPIDER_NET_DESCR_RESPONSE_ERROR: |
| 773 | case SPIDER_NET_DESCR_PROTECTION_ERROR: |
| 774 | case SPIDER_NET_DESCR_FORCE_END: |
| 775 | if (netif_msg_tx_err(card)) |
| 776 | pr_err("%s: forcing end of tx descriptor " |
| 777 | "with status x%02x\n", |
| 778 | card->netdev->name, status); |
| 779 | card->netdev_stats.tx_errors++; |
| 780 | break; |
| 781 | |
| 782 | default: |
| 783 | card->netdev_stats.tx_dropped++; |
Linas Vepstas | 9cc7bf7 | 2006-10-10 16:14:29 -0500 | [diff] [blame] | 784 | if (!brutal) { |
| 785 | spin_unlock_irqrestore(&chain->lock, flags); |
Linas Vepstas | c3fee4c | 2006-10-10 16:00:04 -0500 | [diff] [blame] | 786 | return 1; |
Linas Vepstas | 9cc7bf7 | 2006-10-10 16:14:29 -0500 | [diff] [blame] | 787 | } |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 788 | } |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 789 | |
Linas Vepstas | 9cc7bf7 | 2006-10-10 16:14:29 -0500 | [diff] [blame] | 790 | chain->tail = descr->next; |
| 791 | descr->dmac_cmd_status |= SPIDER_NET_DESCR_NOT_IN_USE; |
| 792 | skb = descr->skb; |
| 793 | buf_addr = descr->buf_addr; |
| 794 | spin_unlock_irqrestore(&chain->lock, flags); |
| 795 | |
| 796 | /* unmap the skb */ |
| 797 | if (skb) { |
James K Lewis | 9c434f5 | 2006-11-17 14:39:36 -0800 | [diff] [blame] | 798 | pci_unmap_single(card->pdev, buf_addr, skb->len, |
| 799 | PCI_DMA_TODEVICE); |
Linas Vepstas | 9cc7bf7 | 2006-10-10 16:14:29 -0500 | [diff] [blame] | 800 | dev_kfree_skb(skb); |
| 801 | } |
| 802 | } |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 803 | return 0; |
| 804 | } |
| 805 | |
| 806 | /** |
| 807 | * spider_net_kick_tx_dma - enables TX DMA processing |
| 808 | * @card: card structure |
| 809 | * @descr: descriptor address to enable TX processing at |
| 810 | * |
Linas Vepstas | a664ccf | 2006-10-10 16:18:18 -0500 | [diff] [blame] | 811 | * This routine will start the transmit DMA running if |
| 812 | * it is not already running. This routine ned only be |
| 813 | * called when queueing a new packet to an empty tx queue. |
| 814 | * Writes the current tx chain head as start address |
| 815 | * of the tx descriptor chain and enables the transmission |
| 816 | * DMA engine. |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 817 | */ |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 818 | static inline void |
| 819 | spider_net_kick_tx_dma(struct spider_net_card *card) |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 820 | { |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 821 | struct spider_net_descr *descr; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 822 | |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 823 | if (spider_net_read_reg(card, SPIDER_NET_GDTDMACCNTR) & |
| 824 | SPIDER_NET_TX_DMA_EN) |
| 825 | goto out; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 826 | |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 827 | descr = card->tx_chain.tail; |
| 828 | for (;;) { |
| 829 | if (spider_net_get_descr_status(descr) == |
| 830 | SPIDER_NET_DESCR_CARDOWNED) { |
| 831 | spider_net_write_reg(card, SPIDER_NET_GDTDCHA, |
| 832 | descr->bus_addr); |
| 833 | spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR, |
| 834 | SPIDER_NET_DMA_TX_VALUE); |
| 835 | break; |
| 836 | } |
| 837 | if (descr == card->tx_chain.head) |
| 838 | break; |
| 839 | descr = descr->next; |
| 840 | } |
| 841 | |
| 842 | out: |
| 843 | mod_timer(&card->tx_timer, jiffies + SPIDER_NET_TX_TIMER); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | /** |
| 847 | * spider_net_xmit - transmits a frame over the device |
| 848 | * @skb: packet to send out |
| 849 | * @netdev: interface device structure |
| 850 | * |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 851 | * returns 0 on success, !0 on failure |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 852 | */ |
| 853 | static int |
| 854 | spider_net_xmit(struct sk_buff *skb, struct net_device *netdev) |
| 855 | { |
Linas Vepstas | a664ccf | 2006-10-10 16:18:18 -0500 | [diff] [blame] | 856 | int cnt; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 857 | struct spider_net_card *card = netdev_priv(netdev); |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 858 | struct spider_net_descr_chain *chain = &card->tx_chain; |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 859 | |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 860 | spider_net_release_tx_chain(card, 0); |
| 861 | |
Linas Vepstas | 313ef4b | 2006-10-10 16:04:00 -0500 | [diff] [blame] | 862 | if ((chain->head->next == chain->tail->prev) || |
Linas Vepstas | 313ef4b | 2006-10-10 16:04:00 -0500 | [diff] [blame] | 863 | (spider_net_prepare_tx_descr(card, skb) != 0)) { |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 864 | |
Jim Lewis | 9b6b0b8 | 2006-09-22 17:22:53 -0700 | [diff] [blame] | 865 | card->netdev_stats.tx_dropped++; |
Linas Vepstas | 313ef4b | 2006-10-10 16:04:00 -0500 | [diff] [blame] | 866 | netif_stop_queue(netdev); |
| 867 | return NETDEV_TX_BUSY; |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 868 | } |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 869 | |
Linas Vepstas | a664ccf | 2006-10-10 16:18:18 -0500 | [diff] [blame] | 870 | cnt = spider_net_set_low_watermark(card); |
| 871 | if (cnt < 5) |
| 872 | spider_net_kick_tx_dma(card); |
Linas Vepstas | 313ef4b | 2006-10-10 16:04:00 -0500 | [diff] [blame] | 873 | return NETDEV_TX_OK; |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | /** |
| 877 | * spider_net_cleanup_tx_ring - cleans up the TX ring |
| 878 | * @card: card structure |
| 879 | * |
Linas Vepstas | 68a8c60 | 2006-10-10 16:13:05 -0500 | [diff] [blame] | 880 | * spider_net_cleanup_tx_ring is called by either the tx_timer |
| 881 | * or from the NAPI polling routine. |
| 882 | * This routine releases resources associted with transmitted |
| 883 | * packets, including updating the queue tail pointer. |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 884 | */ |
| 885 | static void |
| 886 | spider_net_cleanup_tx_ring(struct spider_net_card *card) |
| 887 | { |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 888 | if ((spider_net_release_tx_chain(card, 0) != 0) && |
Linas Vepstas | 313ef4b | 2006-10-10 16:04:00 -0500 | [diff] [blame] | 889 | (card->netdev->flags & IFF_UP)) { |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 890 | spider_net_kick_tx_dma(card); |
Linas Vepstas | 313ef4b | 2006-10-10 16:04:00 -0500 | [diff] [blame] | 891 | netif_wake_queue(card->netdev); |
| 892 | } |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | /** |
| 896 | * spider_net_do_ioctl - called for device ioctls |
| 897 | * @netdev: interface device structure |
| 898 | * @ifr: request parameter structure for ioctl |
| 899 | * @cmd: command code for ioctl |
| 900 | * |
| 901 | * returns 0 on success, <0 on failure. Currently, we have no special ioctls. |
| 902 | * -EOPNOTSUPP is returned, if an unknown ioctl was requested |
| 903 | */ |
| 904 | static int |
| 905 | spider_net_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd) |
| 906 | { |
| 907 | switch (cmd) { |
| 908 | default: |
| 909 | return -EOPNOTSUPP; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | /** |
| 914 | * spider_net_pass_skb_up - takes an skb from a descriptor and passes it on |
| 915 | * @descr: descriptor to process |
| 916 | * @card: card structure |
| 917 | * |
Linas Vepstas | 05b346b | 2006-12-13 15:15:15 -0600 | [diff] [blame] | 918 | * Fills out skb structure and passes the data to the stack. |
| 919 | * The descriptor state is not changed. |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 920 | */ |
Linas Vepstas | 7f7223b | 2006-12-13 15:17:39 -0600 | [diff] [blame] | 921 | static void |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 922 | spider_net_pass_skb_up(struct spider_net_descr *descr, |
Linas Vepstas | 1cd173f | 2006-12-13 15:12:26 -0600 | [diff] [blame] | 923 | struct spider_net_card *card) |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 924 | { |
| 925 | struct sk_buff *skb; |
| 926 | struct net_device *netdev; |
| 927 | u32 data_status, data_error; |
| 928 | |
| 929 | data_status = descr->data_status; |
| 930 | data_error = descr->data_error; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 931 | netdev = card->netdev; |
| 932 | |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 933 | skb = descr->skb; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 934 | skb->dev = netdev; |
| 935 | skb_put(skb, descr->valid_size); |
| 936 | |
| 937 | /* the card seems to add 2 bytes of junk in front |
| 938 | * of the ethernet frame */ |
| 939 | #define SPIDER_MISALIGN 2 |
| 940 | skb_pull(skb, SPIDER_MISALIGN); |
| 941 | skb->protocol = eth_type_trans(skb, netdev); |
| 942 | |
| 943 | /* checksum offload */ |
| 944 | if (card->options.rx_csum) { |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 945 | if ( ( (data_status & SPIDER_NET_DATA_STATUS_CKSUM_MASK) == |
| 946 | SPIDER_NET_DATA_STATUS_CKSUM_MASK) && |
| 947 | !(data_error & SPIDER_NET_DATA_ERR_CKSUM_MASK)) |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 948 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 949 | else |
| 950 | skb->ip_summed = CHECKSUM_NONE; |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 951 | } else |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 952 | skb->ip_summed = CHECKSUM_NONE; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 953 | |
| 954 | if (data_status & SPIDER_NET_VLAN_PACKET) { |
| 955 | /* further enhancements: HW-accel VLAN |
| 956 | * vlan_hwaccel_receive_skb |
| 957 | */ |
| 958 | } |
| 959 | |
| 960 | /* pass skb up to stack */ |
Linas Vepstas | 1cd173f | 2006-12-13 15:12:26 -0600 | [diff] [blame] | 961 | netif_receive_skb(skb); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 962 | |
| 963 | /* update netdevice statistics */ |
| 964 | card->netdev_stats.rx_packets++; |
| 965 | card->netdev_stats.rx_bytes += skb->len; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 966 | } |
| 967 | |
Linas Vepstas | 6d24998 | 2006-12-13 15:23:56 -0600 | [diff] [blame] | 968 | #ifdef DEBUG |
| 969 | static void show_rx_chain(struct spider_net_card *card) |
| 970 | { |
| 971 | struct spider_net_descr_chain *chain = &card->rx_chain; |
| 972 | struct spider_net_descr *start= chain->tail; |
| 973 | struct spider_net_descr *descr= start; |
| 974 | int status; |
| 975 | |
| 976 | int cnt = 0; |
| 977 | int cstat = spider_net_get_descr_status(descr); |
| 978 | printk(KERN_INFO "RX chain tail at descr=%ld\n", |
| 979 | (start - card->descr) - card->tx_chain.num_desc); |
| 980 | status = cstat; |
| 981 | do |
| 982 | { |
| 983 | status = spider_net_get_descr_status(descr); |
| 984 | if (cstat != status) { |
| 985 | printk(KERN_INFO "Have %d descrs with stat=x%08x\n", cnt, cstat); |
| 986 | cstat = status; |
| 987 | cnt = 0; |
| 988 | } |
| 989 | cnt ++; |
| 990 | descr = descr->next; |
| 991 | } while (descr != start); |
| 992 | printk(KERN_INFO "Last %d descrs with stat=x%08x\n", cnt, cstat); |
| 993 | } |
| 994 | #endif |
| 995 | |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 996 | /** |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 997 | * spider_net_decode_one_descr - processes an rx descriptor |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 998 | * @card: card structure |
| 999 | * |
Linas Vepstas | 1cd173f | 2006-12-13 15:12:26 -0600 | [diff] [blame] | 1000 | * Returns 1 if a packet has been sent to the stack, otherwise 0 |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1001 | * |
Linas Vepstas | 1cd173f | 2006-12-13 15:12:26 -0600 | [diff] [blame] | 1002 | * Processes an rx descriptor by iommu-unmapping the data buffer and passing |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1003 | * the packet up to the stack. This function is called in softirq |
| 1004 | * context, e.g. either bottom half from interrupt or NAPI polling context |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1005 | */ |
| 1006 | static int |
Linas Vepstas | 1cd173f | 2006-12-13 15:12:26 -0600 | [diff] [blame] | 1007 | spider_net_decode_one_descr(struct spider_net_card *card) |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1008 | { |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 1009 | struct spider_net_descr_chain *chain = &card->rx_chain; |
| 1010 | struct spider_net_descr *descr = chain->tail; |
| 1011 | int status; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1012 | |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1013 | status = spider_net_get_descr_status(descr); |
| 1014 | |
Linas Vepstas | 80dab7c | 2006-12-13 15:18:52 -0600 | [diff] [blame] | 1015 | /* Nothing in the descriptor, or ring must be empty */ |
| 1016 | if ((status == SPIDER_NET_DESCR_CARDOWNED) || |
| 1017 | (status == SPIDER_NET_DESCR_NOT_IN_USE)) |
Linas Vepstas | 1cd173f | 2006-12-13 15:12:26 -0600 | [diff] [blame] | 1018 | return 0; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1019 | |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1020 | /* descriptor definitively used -- move on tail */ |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1021 | chain->tail = descr->next; |
| 1022 | |
Linas Vepstas | 05b346b | 2006-12-13 15:15:15 -0600 | [diff] [blame] | 1023 | /* unmap descriptor */ |
| 1024 | pci_unmap_single(card->pdev, descr->buf_addr, |
| 1025 | SPIDER_NET_MAX_FRAME, PCI_DMA_FROMDEVICE); |
| 1026 | |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1027 | if ( (status == SPIDER_NET_DESCR_RESPONSE_ERROR) || |
| 1028 | (status == SPIDER_NET_DESCR_PROTECTION_ERROR) || |
| 1029 | (status == SPIDER_NET_DESCR_FORCE_END) ) { |
| 1030 | if (netif_msg_rx_err(card)) |
| 1031 | pr_err("%s: dropping RX descriptor with state %d\n", |
| 1032 | card->netdev->name, status); |
| 1033 | card->netdev_stats.rx_dropped++; |
Linas Vepstas | 7f7223b | 2006-12-13 15:17:39 -0600 | [diff] [blame] | 1034 | goto bad_desc; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | if ( (status != SPIDER_NET_DESCR_COMPLETE) && |
| 1038 | (status != SPIDER_NET_DESCR_FRAME_END) ) { |
Linas Vepstas | 5a02887 | 2006-12-13 15:08:25 -0600 | [diff] [blame] | 1039 | if (netif_msg_rx_err(card)) |
Linas Vepstas | 05b346b | 2006-12-13 15:15:15 -0600 | [diff] [blame] | 1040 | pr_err("%s: RX descriptor with unkown state %d\n", |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1041 | card->netdev->name, status); |
Linas Vepstas | 5a02887 | 2006-12-13 15:08:25 -0600 | [diff] [blame] | 1042 | card->spider_stats.rx_desc_unk_state++; |
Linas Vepstas | 7f7223b | 2006-12-13 15:17:39 -0600 | [diff] [blame] | 1043 | goto bad_desc; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1044 | } |
| 1045 | |
Linas Vepstas | 366684b | 2006-12-13 15:16:18 -0600 | [diff] [blame] | 1046 | /* The cases we'll throw away the packet immediately */ |
| 1047 | if (descr->data_error & SPIDER_NET_DESTROY_RX_FLAGS) { |
| 1048 | if (netif_msg_rx_err(card)) |
| 1049 | pr_err("%s: error in received descriptor found, " |
| 1050 | "data_status=x%08x, data_error=x%08x\n", |
| 1051 | card->netdev->name, |
| 1052 | descr->data_status, descr->data_error); |
Linas Vepstas | 7f7223b | 2006-12-13 15:17:39 -0600 | [diff] [blame] | 1053 | goto bad_desc; |
Linas Vepstas | 366684b | 2006-12-13 15:16:18 -0600 | [diff] [blame] | 1054 | } |
| 1055 | |
Linas Vepstas | 6d24998 | 2006-12-13 15:23:56 -0600 | [diff] [blame] | 1056 | if (descr->dmac_cmd_status & 0xfefe) { |
| 1057 | pr_err("%s: bad status, cmd_status=x%08x\n", |
| 1058 | card->netdev->name, |
| 1059 | descr->dmac_cmd_status); |
| 1060 | pr_err("buf_addr=x%08x\n", descr->buf_addr); |
| 1061 | pr_err("buf_size=x%08x\n", descr->buf_size); |
| 1062 | pr_err("next_descr_addr=x%08x\n", descr->next_descr_addr); |
| 1063 | pr_err("result_size=x%08x\n", descr->result_size); |
| 1064 | pr_err("valid_size=x%08x\n", descr->valid_size); |
| 1065 | pr_err("data_status=x%08x\n", descr->data_status); |
| 1066 | pr_err("data_error=x%08x\n", descr->data_error); |
| 1067 | pr_err("bus_addr=x%08x\n", descr->bus_addr); |
| 1068 | pr_err("which=%ld\n", descr - card->rx_chain.ring); |
| 1069 | |
| 1070 | card->spider_stats.rx_desc_error++; |
| 1071 | goto bad_desc; |
| 1072 | } |
| 1073 | |
Linas Vepstas | 7f7223b | 2006-12-13 15:17:39 -0600 | [diff] [blame] | 1074 | /* Ok, we've got a packet in descr */ |
| 1075 | spider_net_pass_skb_up(descr, card); |
Linas Vepstas | 1cd173f | 2006-12-13 15:12:26 -0600 | [diff] [blame] | 1076 | descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE; |
Linas Vepstas | 7f7223b | 2006-12-13 15:17:39 -0600 | [diff] [blame] | 1077 | return 1; |
| 1078 | |
| 1079 | bad_desc: |
| 1080 | dev_kfree_skb_irq(descr->skb); |
| 1081 | descr->dmac_cmd_status = SPIDER_NET_DESCR_NOT_IN_USE; |
| 1082 | return 0; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | /** |
| 1086 | * spider_net_poll - NAPI poll function called by the stack to return packets |
| 1087 | * @netdev: interface device structure |
| 1088 | * @budget: number of packets we can pass to the stack at most |
| 1089 | * |
| 1090 | * returns 0 if no more packets available to the driver/stack. Returns 1, |
| 1091 | * if the quota is exceeded, but the driver has still packets. |
| 1092 | * |
| 1093 | * spider_net_poll returns all packets from the rx descriptors to the stack |
| 1094 | * (using netif_receive_skb). If all/enough packets are up, the driver |
| 1095 | * reenables interrupts and returns 0. If not, 1 is returned. |
| 1096 | */ |
| 1097 | static int |
| 1098 | spider_net_poll(struct net_device *netdev, int *budget) |
| 1099 | { |
| 1100 | struct spider_net_card *card = netdev_priv(netdev); |
| 1101 | int packets_to_do, packets_done = 0; |
| 1102 | int no_more_packets = 0; |
| 1103 | |
Linas Vepstas | 68a8c60 | 2006-10-10 16:13:05 -0500 | [diff] [blame] | 1104 | spider_net_cleanup_tx_ring(card); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1105 | packets_to_do = min(*budget, netdev->quota); |
| 1106 | |
| 1107 | while (packets_to_do) { |
Linas Vepstas | 1cd173f | 2006-12-13 15:12:26 -0600 | [diff] [blame] | 1108 | if (spider_net_decode_one_descr(card)) { |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1109 | packets_done++; |
| 1110 | packets_to_do--; |
| 1111 | } else { |
| 1112 | /* no more packets for the stack */ |
| 1113 | no_more_packets = 1; |
| 1114 | break; |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | netdev->quota -= packets_done; |
| 1119 | *budget -= packets_done; |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1120 | spider_net_refill_rx_chain(card); |
Linas Vepstas | 80dab7c | 2006-12-13 15:18:52 -0600 | [diff] [blame] | 1121 | spider_net_enable_rxdmac(card); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1122 | |
| 1123 | /* if all packets are in the stack, enable interrupts and return 0 */ |
| 1124 | /* if not, return 1 */ |
| 1125 | if (no_more_packets) { |
| 1126 | netif_rx_complete(netdev); |
| 1127 | spider_net_rx_irq_on(card); |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
| 1131 | return 1; |
| 1132 | } |
| 1133 | |
| 1134 | /** |
| 1135 | * spider_net_vlan_rx_reg - initializes VLAN structures in the driver and card |
| 1136 | * @netdev: interface device structure |
| 1137 | * @grp: vlan_group structure that is registered (NULL on destroying interface) |
| 1138 | */ |
| 1139 | static void |
| 1140 | spider_net_vlan_rx_reg(struct net_device *netdev, struct vlan_group *grp) |
| 1141 | { |
| 1142 | /* further enhancement... yet to do */ |
| 1143 | return; |
| 1144 | } |
| 1145 | |
| 1146 | /** |
| 1147 | * spider_net_vlan_rx_add - adds VLAN id to the card filter |
| 1148 | * @netdev: interface device structure |
| 1149 | * @vid: VLAN id to add |
| 1150 | */ |
| 1151 | static void |
| 1152 | spider_net_vlan_rx_add(struct net_device *netdev, uint16_t vid) |
| 1153 | { |
| 1154 | /* further enhancement... yet to do */ |
| 1155 | /* add vid to card's VLAN filter table */ |
| 1156 | return; |
| 1157 | } |
| 1158 | |
| 1159 | /** |
| 1160 | * spider_net_vlan_rx_kill - removes VLAN id to the card filter |
| 1161 | * @netdev: interface device structure |
| 1162 | * @vid: VLAN id to remove |
| 1163 | */ |
| 1164 | static void |
| 1165 | spider_net_vlan_rx_kill(struct net_device *netdev, uint16_t vid) |
| 1166 | { |
| 1167 | /* further enhancement... yet to do */ |
| 1168 | /* remove vid from card's VLAN filter table */ |
| 1169 | } |
| 1170 | |
| 1171 | /** |
| 1172 | * spider_net_get_stats - get interface statistics |
| 1173 | * @netdev: interface device structure |
| 1174 | * |
| 1175 | * returns the interface statistics residing in the spider_net_card struct |
| 1176 | */ |
| 1177 | static struct net_device_stats * |
| 1178 | spider_net_get_stats(struct net_device *netdev) |
| 1179 | { |
| 1180 | struct spider_net_card *card = netdev_priv(netdev); |
| 1181 | struct net_device_stats *stats = &card->netdev_stats; |
| 1182 | return stats; |
| 1183 | } |
| 1184 | |
| 1185 | /** |
| 1186 | * spider_net_change_mtu - changes the MTU of an interface |
| 1187 | * @netdev: interface device structure |
| 1188 | * @new_mtu: new MTU value |
| 1189 | * |
| 1190 | * returns 0 on success, <0 on failure |
| 1191 | */ |
| 1192 | static int |
| 1193 | spider_net_change_mtu(struct net_device *netdev, int new_mtu) |
| 1194 | { |
| 1195 | /* no need to re-alloc skbs or so -- the max mtu is about 2.3k |
| 1196 | * and mtu is outbound only anyway */ |
| 1197 | if ( (new_mtu < SPIDER_NET_MIN_MTU ) || |
| 1198 | (new_mtu > SPIDER_NET_MAX_MTU) ) |
| 1199 | return -EINVAL; |
| 1200 | netdev->mtu = new_mtu; |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
| 1204 | /** |
| 1205 | * spider_net_set_mac - sets the MAC of an interface |
| 1206 | * @netdev: interface device structure |
| 1207 | * @ptr: pointer to new MAC address |
| 1208 | * |
| 1209 | * Returns 0 on success, <0 on failure. Currently, we don't support this |
| 1210 | * and will always return EOPNOTSUPP. |
| 1211 | */ |
| 1212 | static int |
| 1213 | spider_net_set_mac(struct net_device *netdev, void *p) |
| 1214 | { |
| 1215 | struct spider_net_card *card = netdev_priv(netdev); |
Jens Osterkamp | 054034d | 2005-09-06 19:30:55 -0700 | [diff] [blame] | 1216 | u32 macl, macu, regvalue; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1217 | struct sockaddr *addr = p; |
| 1218 | |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1219 | if (!is_valid_ether_addr(addr->sa_data)) |
| 1220 | return -EADDRNOTAVAIL; |
| 1221 | |
Jens Osterkamp | 054034d | 2005-09-06 19:30:55 -0700 | [diff] [blame] | 1222 | /* switch off GMACTPE and GMACRPE */ |
| 1223 | regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD); |
| 1224 | regvalue &= ~((1 << 5) | (1 << 6)); |
| 1225 | spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue); |
| 1226 | |
| 1227 | /* write mac */ |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1228 | macu = (addr->sa_data[0]<<24) + (addr->sa_data[1]<<16) + |
| 1229 | (addr->sa_data[2]<<8) + (addr->sa_data[3]); |
| 1230 | macl = (addr->sa_data[4]<<8) + (addr->sa_data[5]); |
| 1231 | spider_net_write_reg(card, SPIDER_NET_GMACUNIMACU, macu); |
| 1232 | spider_net_write_reg(card, SPIDER_NET_GMACUNIMACL, macl); |
| 1233 | |
Jens Osterkamp | 054034d | 2005-09-06 19:30:55 -0700 | [diff] [blame] | 1234 | /* switch GMACTPE and GMACRPE back on */ |
| 1235 | regvalue = spider_net_read_reg(card, SPIDER_NET_GMACOPEMD); |
| 1236 | regvalue |= ((1 << 5) | (1 << 6)); |
| 1237 | spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, regvalue); |
| 1238 | |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1239 | spider_net_set_promisc(card); |
| 1240 | |
| 1241 | /* look up, whether we have been successful */ |
| 1242 | if (spider_net_get_mac_address(netdev)) |
| 1243 | return -EADDRNOTAVAIL; |
| 1244 | if (memcmp(netdev->dev_addr,addr->sa_data,netdev->addr_len)) |
| 1245 | return -EADDRNOTAVAIL; |
| 1246 | |
| 1247 | return 0; |
| 1248 | } |
| 1249 | |
| 1250 | /** |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1251 | * spider_net_handle_error_irq - handles errors raised by an interrupt |
| 1252 | * @card: card structure |
| 1253 | * @status_reg: interrupt status register 0 (GHIINT0STS) |
| 1254 | * |
| 1255 | * spider_net_handle_error_irq treats or ignores all error conditions |
| 1256 | * found when an interrupt is presented |
| 1257 | */ |
| 1258 | static void |
| 1259 | spider_net_handle_error_irq(struct spider_net_card *card, u32 status_reg) |
| 1260 | { |
| 1261 | u32 error_reg1, error_reg2; |
| 1262 | u32 i; |
| 1263 | int show_error = 1; |
| 1264 | |
| 1265 | error_reg1 = spider_net_read_reg(card, SPIDER_NET_GHIINT1STS); |
| 1266 | error_reg2 = spider_net_read_reg(card, SPIDER_NET_GHIINT2STS); |
| 1267 | |
| 1268 | /* check GHIINT0STS ************************************/ |
| 1269 | if (status_reg) |
| 1270 | for (i = 0; i < 32; i++) |
| 1271 | if (status_reg & (1<<i)) |
| 1272 | switch (i) |
| 1273 | { |
| 1274 | /* let error_reg1 and error_reg2 evaluation decide, what to do |
| 1275 | case SPIDER_NET_PHYINT: |
| 1276 | case SPIDER_NET_GMAC2INT: |
| 1277 | case SPIDER_NET_GMAC1INT: |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1278 | case SPIDER_NET_GFIFOINT: |
| 1279 | case SPIDER_NET_DMACINT: |
| 1280 | case SPIDER_NET_GSYSINT: |
| 1281 | break; */ |
| 1282 | |
Linas Vepstas | 98b9040 | 2006-10-10 16:08:42 -0500 | [diff] [blame] | 1283 | case SPIDER_NET_GIPSINT: |
| 1284 | show_error = 0; |
| 1285 | break; |
| 1286 | |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1287 | case SPIDER_NET_GPWOPCMPINT: |
| 1288 | /* PHY write operation completed */ |
| 1289 | show_error = 0; |
| 1290 | break; |
| 1291 | case SPIDER_NET_GPROPCMPINT: |
| 1292 | /* PHY read operation completed */ |
| 1293 | /* we don't use semaphores, as we poll for the completion |
| 1294 | * of the read operation in spider_net_read_phy. Should take |
| 1295 | * about 50 us */ |
| 1296 | show_error = 0; |
| 1297 | break; |
| 1298 | case SPIDER_NET_GPWFFINT: |
| 1299 | /* PHY command queue full */ |
| 1300 | if (netif_msg_intr(card)) |
| 1301 | pr_err("PHY write queue full\n"); |
| 1302 | show_error = 0; |
| 1303 | break; |
| 1304 | |
| 1305 | /* case SPIDER_NET_GRMDADRINT: not used. print a message */ |
| 1306 | /* case SPIDER_NET_GRMARPINT: not used. print a message */ |
| 1307 | /* case SPIDER_NET_GRMMPINT: not used. print a message */ |
| 1308 | |
| 1309 | case SPIDER_NET_GDTDEN0INT: |
| 1310 | /* someone has set TX_DMA_EN to 0 */ |
| 1311 | show_error = 0; |
| 1312 | break; |
| 1313 | |
| 1314 | case SPIDER_NET_GDDDEN0INT: /* fallthrough */ |
| 1315 | case SPIDER_NET_GDCDEN0INT: /* fallthrough */ |
| 1316 | case SPIDER_NET_GDBDEN0INT: /* fallthrough */ |
| 1317 | case SPIDER_NET_GDADEN0INT: |
| 1318 | /* someone has set RX_DMA_EN to 0 */ |
| 1319 | show_error = 0; |
| 1320 | break; |
| 1321 | |
| 1322 | /* RX interrupts */ |
| 1323 | case SPIDER_NET_GDDFDCINT: |
| 1324 | case SPIDER_NET_GDCFDCINT: |
| 1325 | case SPIDER_NET_GDBFDCINT: |
| 1326 | case SPIDER_NET_GDAFDCINT: |
| 1327 | /* case SPIDER_NET_GDNMINT: not used. print a message */ |
| 1328 | /* case SPIDER_NET_GCNMINT: not used. print a message */ |
| 1329 | /* case SPIDER_NET_GBNMINT: not used. print a message */ |
| 1330 | /* case SPIDER_NET_GANMINT: not used. print a message */ |
| 1331 | /* case SPIDER_NET_GRFNMINT: not used. print a message */ |
| 1332 | show_error = 0; |
| 1333 | break; |
| 1334 | |
| 1335 | /* TX interrupts */ |
| 1336 | case SPIDER_NET_GDTFDCINT: |
| 1337 | show_error = 0; |
| 1338 | break; |
| 1339 | case SPIDER_NET_GTTEDINT: |
| 1340 | show_error = 0; |
| 1341 | break; |
| 1342 | case SPIDER_NET_GDTDCEINT: |
| 1343 | /* chain end. If a descriptor should be sent, kick off |
| 1344 | * tx dma |
Linas Vepstas | 98b9040 | 2006-10-10 16:08:42 -0500 | [diff] [blame] | 1345 | if (card->tx_chain.tail != card->tx_chain.head) |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1346 | spider_net_kick_tx_dma(card); |
Linas Vepstas | 98b9040 | 2006-10-10 16:08:42 -0500 | [diff] [blame] | 1347 | */ |
| 1348 | show_error = 0; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1349 | break; |
| 1350 | |
| 1351 | /* case SPIDER_NET_G1TMCNTINT: not used. print a message */ |
| 1352 | /* case SPIDER_NET_GFREECNTINT: not used. print a message */ |
| 1353 | } |
| 1354 | |
| 1355 | /* check GHIINT1STS ************************************/ |
| 1356 | if (error_reg1) |
| 1357 | for (i = 0; i < 32; i++) |
| 1358 | if (error_reg1 & (1<<i)) |
| 1359 | switch (i) |
| 1360 | { |
| 1361 | case SPIDER_NET_GTMFLLINT: |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1362 | if (netif_msg_intr(card) && net_ratelimit()) |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1363 | pr_err("Spider TX RAM full\n"); |
| 1364 | show_error = 0; |
| 1365 | break; |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1366 | case SPIDER_NET_GRFDFLLINT: /* fallthrough */ |
| 1367 | case SPIDER_NET_GRFCFLLINT: /* fallthrough */ |
| 1368 | case SPIDER_NET_GRFBFLLINT: /* fallthrough */ |
| 1369 | case SPIDER_NET_GRFAFLLINT: /* fallthrough */ |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1370 | case SPIDER_NET_GRMFLLINT: |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1371 | if (netif_msg_intr(card) && net_ratelimit()) |
Linas Vepstas | 5a02887 | 2006-12-13 15:08:25 -0600 | [diff] [blame] | 1372 | pr_err("Spider RX RAM full, incoming packets " |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1373 | "might be discarded!\n"); |
| 1374 | spider_net_rx_irq_off(card); |
Linas Vepstas | 7585617 | 2006-12-13 15:10:06 -0600 | [diff] [blame] | 1375 | netif_rx_schedule(card->netdev); |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1376 | show_error = 0; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1377 | break; |
| 1378 | |
| 1379 | /* case SPIDER_NET_GTMSHTINT: problem, print a message */ |
| 1380 | case SPIDER_NET_GDTINVDINT: |
| 1381 | /* allrighty. tx from previous descr ok */ |
| 1382 | show_error = 0; |
| 1383 | break; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1384 | |
| 1385 | /* chain end */ |
| 1386 | case SPIDER_NET_GDDDCEINT: /* fallthrough */ |
| 1387 | case SPIDER_NET_GDCDCEINT: /* fallthrough */ |
| 1388 | case SPIDER_NET_GDBDCEINT: /* fallthrough */ |
| 1389 | case SPIDER_NET_GDADCEINT: |
Linas Vepstas | 5a02887 | 2006-12-13 15:08:25 -0600 | [diff] [blame] | 1390 | if (netif_msg_intr(card) && net_ratelimit()) |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1391 | pr_err("got descriptor chain end interrupt, " |
| 1392 | "restarting DMAC %c.\n", |
Linas Vepstas | 37aad75 | 2006-10-10 16:06:53 -0500 | [diff] [blame] | 1393 | 'D'-(i-SPIDER_NET_GDDDCEINT)/3); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1394 | spider_net_refill_rx_chain(card); |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1395 | spider_net_enable_rxdmac(card); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1396 | show_error = 0; |
| 1397 | break; |
| 1398 | |
| 1399 | /* invalid descriptor */ |
| 1400 | case SPIDER_NET_GDDINVDINT: /* fallthrough */ |
| 1401 | case SPIDER_NET_GDCINVDINT: /* fallthrough */ |
| 1402 | case SPIDER_NET_GDBINVDINT: /* fallthrough */ |
| 1403 | case SPIDER_NET_GDAINVDINT: |
| 1404 | /* could happen when rx chain is full */ |
| 1405 | spider_net_refill_rx_chain(card); |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1406 | spider_net_enable_rxdmac(card); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1407 | show_error = 0; |
| 1408 | break; |
| 1409 | |
| 1410 | /* case SPIDER_NET_GDTRSERINT: problem, print a message */ |
| 1411 | /* case SPIDER_NET_GDDRSERINT: problem, print a message */ |
| 1412 | /* case SPIDER_NET_GDCRSERINT: problem, print a message */ |
| 1413 | /* case SPIDER_NET_GDBRSERINT: problem, print a message */ |
| 1414 | /* case SPIDER_NET_GDARSERINT: problem, print a message */ |
| 1415 | /* case SPIDER_NET_GDSERINT: problem, print a message */ |
| 1416 | /* case SPIDER_NET_GDTPTERINT: problem, print a message */ |
| 1417 | /* case SPIDER_NET_GDDPTERINT: problem, print a message */ |
| 1418 | /* case SPIDER_NET_GDCPTERINT: problem, print a message */ |
| 1419 | /* case SPIDER_NET_GDBPTERINT: problem, print a message */ |
| 1420 | /* case SPIDER_NET_GDAPTERINT: problem, print a message */ |
| 1421 | default: |
| 1422 | show_error = 1; |
| 1423 | break; |
| 1424 | } |
| 1425 | |
| 1426 | /* check GHIINT2STS ************************************/ |
| 1427 | if (error_reg2) |
| 1428 | for (i = 0; i < 32; i++) |
| 1429 | if (error_reg2 & (1<<i)) |
| 1430 | switch (i) |
| 1431 | { |
| 1432 | /* there is nothing we can (want to) do at this time. Log a |
| 1433 | * message, we can switch on and off the specific values later on |
| 1434 | case SPIDER_NET_GPROPERINT: |
| 1435 | case SPIDER_NET_GMCTCRSNGINT: |
| 1436 | case SPIDER_NET_GMCTLCOLINT: |
| 1437 | case SPIDER_NET_GMCTTMOTINT: |
| 1438 | case SPIDER_NET_GMCRCAERINT: |
| 1439 | case SPIDER_NET_GMCRCALERINT: |
| 1440 | case SPIDER_NET_GMCRALNERINT: |
| 1441 | case SPIDER_NET_GMCROVRINT: |
| 1442 | case SPIDER_NET_GMCRRNTINT: |
| 1443 | case SPIDER_NET_GMCRRXERINT: |
| 1444 | case SPIDER_NET_GTITCSERINT: |
| 1445 | case SPIDER_NET_GTIFMTERINT: |
| 1446 | case SPIDER_NET_GTIPKTRVKINT: |
| 1447 | case SPIDER_NET_GTISPINGINT: |
| 1448 | case SPIDER_NET_GTISADNGINT: |
| 1449 | case SPIDER_NET_GTISPDNGINT: |
| 1450 | case SPIDER_NET_GRIFMTERINT: |
| 1451 | case SPIDER_NET_GRIPKTRVKINT: |
| 1452 | case SPIDER_NET_GRISPINGINT: |
| 1453 | case SPIDER_NET_GRISADNGINT: |
| 1454 | case SPIDER_NET_GRISPDNGINT: |
| 1455 | break; |
| 1456 | */ |
| 1457 | default: |
| 1458 | break; |
| 1459 | } |
| 1460 | |
Linas Vepstas | 5a02887 | 2006-12-13 15:08:25 -0600 | [diff] [blame] | 1461 | if ((show_error) && (netif_msg_intr(card)) && net_ratelimit()) |
Linas Vepstas | 98b9040 | 2006-10-10 16:08:42 -0500 | [diff] [blame] | 1462 | pr_err("Got error interrupt on %s, GHIINT0STS = 0x%08x, " |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1463 | "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n", |
Linas Vepstas | 98b9040 | 2006-10-10 16:08:42 -0500 | [diff] [blame] | 1464 | card->netdev->name, |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1465 | status_reg, error_reg1, error_reg2); |
| 1466 | |
| 1467 | /* clear interrupt sources */ |
| 1468 | spider_net_write_reg(card, SPIDER_NET_GHIINT1STS, error_reg1); |
| 1469 | spider_net_write_reg(card, SPIDER_NET_GHIINT2STS, error_reg2); |
| 1470 | } |
| 1471 | |
| 1472 | /** |
| 1473 | * spider_net_interrupt - interrupt handler for spider_net |
| 1474 | * @irq: interupt number |
| 1475 | * @ptr: pointer to net_device |
| 1476 | * @regs: PU registers |
| 1477 | * |
| 1478 | * returns IRQ_HANDLED, if interrupt was for driver, or IRQ_NONE, if no |
| 1479 | * interrupt found raised by card. |
| 1480 | * |
| 1481 | * This is the interrupt handler, that turns off |
| 1482 | * interrupts for this device and makes the stack poll the driver |
| 1483 | */ |
| 1484 | static irqreturn_t |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1485 | spider_net_interrupt(int irq, void *ptr) |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1486 | { |
| 1487 | struct net_device *netdev = ptr; |
| 1488 | struct spider_net_card *card = netdev_priv(netdev); |
| 1489 | u32 status_reg; |
| 1490 | |
| 1491 | status_reg = spider_net_read_reg(card, SPIDER_NET_GHIINT0STS); |
| 1492 | |
| 1493 | if (!status_reg) |
| 1494 | return IRQ_NONE; |
| 1495 | |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1496 | if (status_reg & SPIDER_NET_RXINT ) { |
| 1497 | spider_net_rx_irq_off(card); |
| 1498 | netif_rx_schedule(netdev); |
| 1499 | } |
Linas Vepstas | 68a8c60 | 2006-10-10 16:13:05 -0500 | [diff] [blame] | 1500 | if (status_reg & SPIDER_NET_TXINT) |
| 1501 | netif_rx_schedule(netdev); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1502 | |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1503 | if (status_reg & SPIDER_NET_ERRINT ) |
| 1504 | spider_net_handle_error_irq(card, status_reg); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1505 | |
| 1506 | /* clear interrupt sources */ |
| 1507 | spider_net_write_reg(card, SPIDER_NET_GHIINT0STS, status_reg); |
| 1508 | |
| 1509 | return IRQ_HANDLED; |
| 1510 | } |
| 1511 | |
| 1512 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1513 | /** |
| 1514 | * spider_net_poll_controller - artificial interrupt for netconsole etc. |
| 1515 | * @netdev: interface device structure |
| 1516 | * |
| 1517 | * see Documentation/networking/netconsole.txt |
| 1518 | */ |
| 1519 | static void |
| 1520 | spider_net_poll_controller(struct net_device *netdev) |
| 1521 | { |
| 1522 | disable_irq(netdev->irq); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1523 | spider_net_interrupt(netdev->irq, netdev); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1524 | enable_irq(netdev->irq); |
| 1525 | } |
| 1526 | #endif /* CONFIG_NET_POLL_CONTROLLER */ |
| 1527 | |
| 1528 | /** |
| 1529 | * spider_net_init_card - initializes the card |
| 1530 | * @card: card structure |
| 1531 | * |
| 1532 | * spider_net_init_card initializes the card so that other registers can |
| 1533 | * be used |
| 1534 | */ |
| 1535 | static void |
| 1536 | spider_net_init_card(struct spider_net_card *card) |
| 1537 | { |
| 1538 | spider_net_write_reg(card, SPIDER_NET_CKRCTRL, |
| 1539 | SPIDER_NET_CKRCTRL_STOP_VALUE); |
| 1540 | |
| 1541 | spider_net_write_reg(card, SPIDER_NET_CKRCTRL, |
| 1542 | SPIDER_NET_CKRCTRL_RUN_VALUE); |
| 1543 | } |
| 1544 | |
| 1545 | /** |
| 1546 | * spider_net_enable_card - enables the card by setting all kinds of regs |
| 1547 | * @card: card structure |
| 1548 | * |
| 1549 | * spider_net_enable_card sets a lot of SMMIO registers to enable the device |
| 1550 | */ |
| 1551 | static void |
| 1552 | spider_net_enable_card(struct spider_net_card *card) |
| 1553 | { |
| 1554 | int i; |
| 1555 | /* the following array consists of (register),(value) pairs |
| 1556 | * that are set in this function. A register of 0 ends the list */ |
| 1557 | u32 regs[][2] = { |
| 1558 | { SPIDER_NET_GRESUMINTNUM, 0 }, |
| 1559 | { SPIDER_NET_GREINTNUM, 0 }, |
| 1560 | |
| 1561 | /* set interrupt frame number registers */ |
| 1562 | /* clear the single DMA engine registers first */ |
| 1563 | { SPIDER_NET_GFAFRMNUM, SPIDER_NET_GFXFRAMES_VALUE }, |
| 1564 | { SPIDER_NET_GFBFRMNUM, SPIDER_NET_GFXFRAMES_VALUE }, |
| 1565 | { SPIDER_NET_GFCFRMNUM, SPIDER_NET_GFXFRAMES_VALUE }, |
| 1566 | { SPIDER_NET_GFDFRMNUM, SPIDER_NET_GFXFRAMES_VALUE }, |
| 1567 | /* then set, what we really need */ |
| 1568 | { SPIDER_NET_GFFRMNUM, SPIDER_NET_FRAMENUM_VALUE }, |
| 1569 | |
| 1570 | /* timer counter registers and stuff */ |
| 1571 | { SPIDER_NET_GFREECNNUM, 0 }, |
| 1572 | { SPIDER_NET_GONETIMENUM, 0 }, |
| 1573 | { SPIDER_NET_GTOUTFRMNUM, 0 }, |
| 1574 | |
| 1575 | /* RX mode setting */ |
| 1576 | { SPIDER_NET_GRXMDSET, SPIDER_NET_RXMODE_VALUE }, |
| 1577 | /* TX mode setting */ |
| 1578 | { SPIDER_NET_GTXMDSET, SPIDER_NET_TXMODE_VALUE }, |
| 1579 | /* IPSEC mode setting */ |
| 1580 | { SPIDER_NET_GIPSECINIT, SPIDER_NET_IPSECINIT_VALUE }, |
| 1581 | |
| 1582 | { SPIDER_NET_GFTRESTRT, SPIDER_NET_RESTART_VALUE }, |
| 1583 | |
| 1584 | { SPIDER_NET_GMRWOLCTRL, 0 }, |
Jens Osterkamp | b636d17 | 2006-05-04 05:59:56 -0400 | [diff] [blame] | 1585 | { SPIDER_NET_GTESTMD, 0x10000000 }, |
| 1586 | { SPIDER_NET_GTTQMSK, 0x00400040 }, |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1587 | |
| 1588 | { SPIDER_NET_GMACINTEN, 0 }, |
| 1589 | |
| 1590 | /* flow control stuff */ |
| 1591 | { SPIDER_NET_GMACAPAUSE, SPIDER_NET_MACAPAUSE_VALUE }, |
| 1592 | { SPIDER_NET_GMACTXPAUSE, SPIDER_NET_TXPAUSE_VALUE }, |
| 1593 | |
| 1594 | { SPIDER_NET_GMACBSTLMT, SPIDER_NET_BURSTLMT_VALUE }, |
| 1595 | { 0, 0} |
| 1596 | }; |
| 1597 | |
| 1598 | i = 0; |
| 1599 | while (regs[i][0]) { |
| 1600 | spider_net_write_reg(card, regs[i][0], regs[i][1]); |
| 1601 | i++; |
| 1602 | } |
| 1603 | |
| 1604 | /* clear unicast filter table entries 1 to 14 */ |
| 1605 | for (i = 1; i <= 14; i++) { |
| 1606 | spider_net_write_reg(card, |
| 1607 | SPIDER_NET_GMRUAFILnR + i * 8, |
| 1608 | 0x00080000); |
| 1609 | spider_net_write_reg(card, |
| 1610 | SPIDER_NET_GMRUAFILnR + i * 8 + 4, |
| 1611 | 0x00000000); |
| 1612 | } |
| 1613 | |
| 1614 | spider_net_write_reg(card, SPIDER_NET_GMRUA0FIL15R, 0x08080000); |
| 1615 | |
| 1616 | spider_net_write_reg(card, SPIDER_NET_ECMODE, SPIDER_NET_ECMODE_VALUE); |
| 1617 | |
| 1618 | /* set chain tail adress for RX chains and |
| 1619 | * enable DMA */ |
| 1620 | spider_net_enable_rxchtails(card); |
| 1621 | spider_net_enable_rxdmac(card); |
| 1622 | |
| 1623 | spider_net_write_reg(card, SPIDER_NET_GRXDMAEN, SPIDER_NET_WOL_VALUE); |
| 1624 | |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1625 | spider_net_write_reg(card, SPIDER_NET_GMACLENLMT, |
| 1626 | SPIDER_NET_LENLMT_VALUE); |
| 1627 | spider_net_write_reg(card, SPIDER_NET_GMACMODE, |
| 1628 | SPIDER_NET_MACMODE_VALUE); |
| 1629 | spider_net_write_reg(card, SPIDER_NET_GMACOPEMD, |
| 1630 | SPIDER_NET_OPMODE_VALUE); |
| 1631 | |
| 1632 | /* set interrupt mask registers */ |
| 1633 | spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, |
| 1634 | SPIDER_NET_INT0_MASK_VALUE); |
| 1635 | spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, |
| 1636 | SPIDER_NET_INT1_MASK_VALUE); |
| 1637 | spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, |
| 1638 | SPIDER_NET_INT2_MASK_VALUE); |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 1639 | |
| 1640 | spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR, |
Linas Vepstas | 7bd54c8 | 2006-11-17 14:39:40 -0800 | [diff] [blame] | 1641 | SPIDER_NET_GDTBSTA); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | /** |
| 1645 | * spider_net_open - called upon ifonfig up |
| 1646 | * @netdev: interface device structure |
| 1647 | * |
| 1648 | * returns 0 on success, <0 on failure |
| 1649 | * |
| 1650 | * spider_net_open allocates all the descriptors and memory needed for |
| 1651 | * operation, sets up multicast list and enables interrupts |
| 1652 | */ |
| 1653 | int |
| 1654 | spider_net_open(struct net_device *netdev) |
| 1655 | { |
| 1656 | struct spider_net_card *card = netdev_priv(netdev); |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 1657 | int result; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1658 | |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 1659 | result = spider_net_init_chain(card, &card->tx_chain); |
| 1660 | if (result) |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1661 | goto alloc_tx_failed; |
Linas Vepstas | 204e5fa | 2006-10-10 16:11:33 -0500 | [diff] [blame] | 1662 | card->low_watermark = NULL; |
| 1663 | |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 1664 | result = spider_net_init_chain(card, &card->rx_chain); |
| 1665 | if (result) |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1666 | goto alloc_rx_failed; |
| 1667 | |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 1668 | /* Allocate rx skbs */ |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1669 | if (spider_net_alloc_rx_skbs(card)) |
| 1670 | goto alloc_skbs_failed; |
| 1671 | |
| 1672 | spider_net_set_multi(netdev); |
| 1673 | |
| 1674 | /* further enhancement: setup hw vlan, if needed */ |
| 1675 | |
| 1676 | result = -EBUSY; |
| 1677 | if (request_irq(netdev->irq, spider_net_interrupt, |
Thomas Gleixner | 1fb9df5 | 2006-07-01 19:29:39 -0700 | [diff] [blame] | 1678 | IRQF_SHARED, netdev->name, netdev)) |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1679 | goto register_int_failed; |
| 1680 | |
| 1681 | spider_net_enable_card(card); |
| 1682 | |
Jens Osterkamp | 543cec5 | 2005-09-06 19:30:54 -0700 | [diff] [blame] | 1683 | netif_start_queue(netdev); |
| 1684 | netif_carrier_on(netdev); |
| 1685 | netif_poll_enable(netdev); |
| 1686 | |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1687 | return 0; |
| 1688 | |
| 1689 | register_int_failed: |
| 1690 | spider_net_free_rx_chain_contents(card); |
| 1691 | alloc_skbs_failed: |
| 1692 | spider_net_free_chain(card, &card->rx_chain); |
| 1693 | alloc_rx_failed: |
| 1694 | spider_net_free_chain(card, &card->tx_chain); |
| 1695 | alloc_tx_failed: |
| 1696 | return result; |
| 1697 | } |
| 1698 | |
| 1699 | /** |
| 1700 | * spider_net_setup_phy - setup PHY |
| 1701 | * @card: card structure |
| 1702 | * |
| 1703 | * returns 0 on success, <0 on failure |
| 1704 | * |
| 1705 | * spider_net_setup_phy is used as part of spider_net_probe. Sets |
| 1706 | * the PHY to 1000 Mbps |
| 1707 | **/ |
| 1708 | static int |
| 1709 | spider_net_setup_phy(struct spider_net_card *card) |
| 1710 | { |
| 1711 | struct mii_phy *phy = &card->phy; |
| 1712 | |
| 1713 | spider_net_write_reg(card, SPIDER_NET_GDTDMASEL, |
| 1714 | SPIDER_NET_DMASEL_VALUE); |
| 1715 | spider_net_write_reg(card, SPIDER_NET_GPCCTRL, |
| 1716 | SPIDER_NET_PHY_CTRL_VALUE); |
| 1717 | phy->mii_id = 1; |
| 1718 | phy->dev = card->netdev; |
| 1719 | phy->mdio_read = spider_net_read_phy; |
| 1720 | phy->mdio_write = spider_net_write_phy; |
| 1721 | |
| 1722 | mii_phy_probe(phy, phy->mii_id); |
| 1723 | |
| 1724 | if (phy->def->ops->setup_forced) |
| 1725 | phy->def->ops->setup_forced(phy, SPEED_1000, DUPLEX_FULL); |
| 1726 | |
Jens Osterkamp | 8ec9345 | 2006-05-04 05:59:41 -0400 | [diff] [blame] | 1727 | phy->def->ops->enable_fiber(phy); |
Jens Osterkamp | 53abbf7 | 2005-09-16 08:55:33 +0200 | [diff] [blame] | 1728 | |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1729 | phy->def->ops->read_link(phy); |
| 1730 | pr_info("Found %s with %i Mbps, %s-duplex.\n", phy->def->name, |
| 1731 | phy->speed, phy->duplex==1 ? "Full" : "Half"); |
| 1732 | |
| 1733 | return 0; |
| 1734 | } |
| 1735 | |
| 1736 | /** |
| 1737 | * spider_net_download_firmware - loads firmware into the adapter |
| 1738 | * @card: card structure |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1739 | * @firmware_ptr: pointer to firmware data |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1740 | * |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1741 | * spider_net_download_firmware loads the firmware data into the |
| 1742 | * adapter. It assumes the length etc. to be allright. |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1743 | */ |
Arnd Bergmann | 0d3ea16 | 2006-01-12 17:16:41 -0500 | [diff] [blame] | 1744 | static int |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1745 | spider_net_download_firmware(struct spider_net_card *card, |
Jeremy Kerr | 1a2509c | 2006-07-12 15:41:03 +1000 | [diff] [blame] | 1746 | const void *firmware_ptr) |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1747 | { |
| 1748 | int sequencer, i; |
Jeremy Kerr | 1a2509c | 2006-07-12 15:41:03 +1000 | [diff] [blame] | 1749 | const u32 *fw_ptr = firmware_ptr; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1750 | |
| 1751 | /* stop sequencers */ |
| 1752 | spider_net_write_reg(card, SPIDER_NET_GSINIT, |
| 1753 | SPIDER_NET_STOP_SEQ_VALUE); |
| 1754 | |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1755 | for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS; |
| 1756 | sequencer++) { |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1757 | spider_net_write_reg(card, |
| 1758 | SPIDER_NET_GSnPRGADR + sequencer * 8, 0); |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1759 | for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) { |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1760 | spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT + |
| 1761 | sequencer * 8, *fw_ptr); |
| 1762 | fw_ptr++; |
| 1763 | } |
| 1764 | } |
| 1765 | |
Arnd Bergmann | 0d3ea16 | 2006-01-12 17:16:41 -0500 | [diff] [blame] | 1766 | if (spider_net_read_reg(card, SPIDER_NET_GSINIT)) |
| 1767 | return -EIO; |
| 1768 | |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1769 | spider_net_write_reg(card, SPIDER_NET_GSINIT, |
| 1770 | SPIDER_NET_RUN_SEQ_VALUE); |
Arnd Bergmann | 0d3ea16 | 2006-01-12 17:16:41 -0500 | [diff] [blame] | 1771 | |
| 1772 | return 0; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | /** |
| 1776 | * spider_net_init_firmware - reads in firmware parts |
| 1777 | * @card: card structure |
| 1778 | * |
| 1779 | * Returns 0 on success, <0 on failure |
| 1780 | * |
| 1781 | * spider_net_init_firmware opens the sequencer firmware and does some basic |
| 1782 | * checks. This function opens and releases the firmware structure. A call |
| 1783 | * to download the firmware is performed before the release. |
| 1784 | * |
| 1785 | * Firmware format |
| 1786 | * =============== |
| 1787 | * spider_fw.bin is expected to be a file containing 6*1024*4 bytes, 4k being |
| 1788 | * the program for each sequencer. Use the command |
| 1789 | * tail -q -n +2 Seq_code1_0x088.txt Seq_code2_0x090.txt \ |
| 1790 | * Seq_code3_0x098.txt Seq_code4_0x0A0.txt Seq_code5_0x0A8.txt \ |
| 1791 | * Seq_code6_0x0B0.txt | xxd -r -p -c4 > spider_fw.bin |
| 1792 | * |
| 1793 | * to generate spider_fw.bin, if you have sequencer programs with something |
| 1794 | * like the following contents for each sequencer: |
| 1795 | * <ONE LINE COMMENT> |
| 1796 | * <FIRST 4-BYTES-WORD FOR SEQUENCER> |
| 1797 | * <SECOND 4-BYTES-WORD FOR SEQUENCER> |
| 1798 | * ... |
| 1799 | * <1024th 4-BYTES-WORD FOR SEQUENCER> |
| 1800 | */ |
| 1801 | static int |
| 1802 | spider_net_init_firmware(struct spider_net_card *card) |
| 1803 | { |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1804 | struct firmware *firmware = NULL; |
Arnd Bergmann | 030d675 | 2006-01-12 17:16:42 -0500 | [diff] [blame] | 1805 | struct device_node *dn; |
Jeremy Kerr | 1a2509c | 2006-07-12 15:41:03 +1000 | [diff] [blame] | 1806 | const u8 *fw_prop = NULL; |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1807 | int err = -ENOENT; |
| 1808 | int fw_size; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1809 | |
Arnd Bergmann | 030d675 | 2006-01-12 17:16:42 -0500 | [diff] [blame] | 1810 | if (request_firmware((const struct firmware **)&firmware, |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1811 | SPIDER_NET_FIRMWARE_NAME, &card->pdev->dev) == 0) { |
| 1812 | if ( (firmware->size != SPIDER_NET_FIRMWARE_LEN) && |
| 1813 | netif_msg_probe(card) ) { |
| 1814 | pr_err("Incorrect size of spidernet firmware in " \ |
| 1815 | "filesystem. Looking in host firmware...\n"); |
| 1816 | goto try_host_fw; |
| 1817 | } |
| 1818 | err = spider_net_download_firmware(card, firmware->data); |
Arnd Bergmann | 030d675 | 2006-01-12 17:16:42 -0500 | [diff] [blame] | 1819 | |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1820 | release_firmware(firmware); |
| 1821 | if (err) |
| 1822 | goto try_host_fw; |
Arnd Bergmann | 030d675 | 2006-01-12 17:16:42 -0500 | [diff] [blame] | 1823 | |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1824 | goto done; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1825 | } |
| 1826 | |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1827 | try_host_fw: |
| 1828 | dn = pci_device_to_OF_node(card->pdev); |
| 1829 | if (!dn) |
| 1830 | goto out_err; |
| 1831 | |
Jeremy Kerr | 1a2509c | 2006-07-12 15:41:03 +1000 | [diff] [blame] | 1832 | fw_prop = get_property(dn, "firmware", &fw_size); |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1833 | if (!fw_prop) |
| 1834 | goto out_err; |
| 1835 | |
| 1836 | if ( (fw_size != SPIDER_NET_FIRMWARE_LEN) && |
| 1837 | netif_msg_probe(card) ) { |
| 1838 | pr_err("Incorrect size of spidernet firmware in " \ |
| 1839 | "host firmware\n"); |
| 1840 | goto done; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1841 | } |
| 1842 | |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1843 | err = spider_net_download_firmware(card, fw_prop); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1844 | |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1845 | done: |
| 1846 | return err; |
| 1847 | out_err: |
| 1848 | if (netif_msg_probe(card)) |
| 1849 | pr_err("Couldn't find spidernet firmware in filesystem " \ |
| 1850 | "or host firmware\n"); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1851 | return err; |
| 1852 | } |
| 1853 | |
| 1854 | /** |
| 1855 | * spider_net_workaround_rxramfull - work around firmware bug |
| 1856 | * @card: card structure |
| 1857 | * |
| 1858 | * no return value |
| 1859 | **/ |
| 1860 | static void |
| 1861 | spider_net_workaround_rxramfull(struct spider_net_card *card) |
| 1862 | { |
| 1863 | int i, sequencer = 0; |
| 1864 | |
| 1865 | /* cancel reset */ |
| 1866 | spider_net_write_reg(card, SPIDER_NET_CKRCTRL, |
| 1867 | SPIDER_NET_CKRCTRL_RUN_VALUE); |
| 1868 | |
| 1869 | /* empty sequencer data */ |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1870 | for (sequencer = 0; sequencer < SPIDER_NET_FIRMWARE_SEQS; |
| 1871 | sequencer++) { |
Jens Osterkamp | ee962a5 | 2006-07-13 11:54:23 +0200 | [diff] [blame] | 1872 | spider_net_write_reg(card, SPIDER_NET_GSnPRGADR + |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1873 | sequencer * 8, 0x0); |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 1874 | for (i = 0; i < SPIDER_NET_FIRMWARE_SEQWORDS; i++) { |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1875 | spider_net_write_reg(card, SPIDER_NET_GSnPRGDAT + |
| 1876 | sequencer * 8, 0x0); |
| 1877 | } |
| 1878 | } |
| 1879 | |
| 1880 | /* set sequencer operation */ |
| 1881 | spider_net_write_reg(card, SPIDER_NET_GSINIT, 0x000000fe); |
| 1882 | |
| 1883 | /* reset */ |
| 1884 | spider_net_write_reg(card, SPIDER_NET_CKRCTRL, |
| 1885 | SPIDER_NET_CKRCTRL_STOP_VALUE); |
| 1886 | } |
| 1887 | |
| 1888 | /** |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 1889 | * spider_net_stop - called upon ifconfig down |
| 1890 | * @netdev: interface device structure |
| 1891 | * |
| 1892 | * always returns 0 |
| 1893 | */ |
| 1894 | int |
| 1895 | spider_net_stop(struct net_device *netdev) |
| 1896 | { |
| 1897 | struct spider_net_card *card = netdev_priv(netdev); |
| 1898 | |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 1899 | netif_poll_disable(netdev); |
| 1900 | netif_carrier_off(netdev); |
| 1901 | netif_stop_queue(netdev); |
| 1902 | del_timer_sync(&card->tx_timer); |
| 1903 | |
| 1904 | /* disable/mask all interrupts */ |
| 1905 | spider_net_write_reg(card, SPIDER_NET_GHIINT0MSK, 0); |
| 1906 | spider_net_write_reg(card, SPIDER_NET_GHIINT1MSK, 0); |
| 1907 | spider_net_write_reg(card, SPIDER_NET_GHIINT2MSK, 0); |
| 1908 | |
| 1909 | /* free_irq(netdev->irq, netdev);*/ |
| 1910 | free_irq(to_pci_dev(netdev->class_dev.dev)->irq, netdev); |
| 1911 | |
| 1912 | spider_net_write_reg(card, SPIDER_NET_GDTDMACCNTR, |
| 1913 | SPIDER_NET_DMA_TX_FEND_VALUE); |
| 1914 | |
| 1915 | /* turn off DMA, force end */ |
| 1916 | spider_net_disable_rxdmac(card); |
| 1917 | |
| 1918 | /* release chains */ |
Linas Vepstas | 9cc7bf7 | 2006-10-10 16:14:29 -0500 | [diff] [blame] | 1919 | spider_net_release_tx_chain(card, 1); |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 1920 | spider_net_free_rx_chain_contents(card); |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 1921 | |
Jens Osterkamp | a55eb05 | 2007-02-01 12:07:47 +0100 | [diff] [blame] | 1922 | spider_net_free_rx_chain_contents(card); |
| 1923 | |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 1924 | spider_net_free_chain(card, &card->tx_chain); |
| 1925 | spider_net_free_chain(card, &card->rx_chain); |
| 1926 | |
| 1927 | return 0; |
| 1928 | } |
| 1929 | |
| 1930 | /** |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1931 | * spider_net_tx_timeout_task - task scheduled by the watchdog timeout |
| 1932 | * function (to be called not under interrupt status) |
| 1933 | * @data: data, is interface device structure |
| 1934 | * |
| 1935 | * called as task when tx hangs, resets interface (if interface is up) |
| 1936 | */ |
| 1937 | static void |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1938 | spider_net_tx_timeout_task(struct work_struct *work) |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1939 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1940 | struct spider_net_card *card = |
| 1941 | container_of(work, struct spider_net_card, tx_timeout_task); |
| 1942 | struct net_device *netdev = card->netdev; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1943 | |
| 1944 | if (!(netdev->flags & IFF_UP)) |
| 1945 | goto out; |
| 1946 | |
| 1947 | netif_device_detach(netdev); |
| 1948 | spider_net_stop(netdev); |
| 1949 | |
| 1950 | spider_net_workaround_rxramfull(card); |
| 1951 | spider_net_init_card(card); |
| 1952 | |
| 1953 | if (spider_net_setup_phy(card)) |
| 1954 | goto out; |
| 1955 | if (spider_net_init_firmware(card)) |
| 1956 | goto out; |
| 1957 | |
| 1958 | spider_net_open(netdev); |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 1959 | spider_net_kick_tx_dma(card); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1960 | netif_device_attach(netdev); |
| 1961 | |
| 1962 | out: |
| 1963 | atomic_dec(&card->tx_timeout_task_counter); |
| 1964 | } |
| 1965 | |
| 1966 | /** |
| 1967 | * spider_net_tx_timeout - called when the tx timeout watchdog kicks in. |
| 1968 | * @netdev: interface device structure |
| 1969 | * |
| 1970 | * called, if tx hangs. Schedules a task that resets the interface |
| 1971 | */ |
| 1972 | static void |
| 1973 | spider_net_tx_timeout(struct net_device *netdev) |
| 1974 | { |
| 1975 | struct spider_net_card *card; |
| 1976 | |
| 1977 | card = netdev_priv(netdev); |
| 1978 | atomic_inc(&card->tx_timeout_task_counter); |
| 1979 | if (netdev->flags & IFF_UP) |
| 1980 | schedule_work(&card->tx_timeout_task); |
| 1981 | else |
| 1982 | atomic_dec(&card->tx_timeout_task_counter); |
Jim Lewis | 9b6b0b8 | 2006-09-22 17:22:53 -0700 | [diff] [blame] | 1983 | card->spider_stats.tx_timeouts++; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | /** |
| 1987 | * spider_net_setup_netdev_ops - initialization of net_device operations |
| 1988 | * @netdev: net_device structure |
| 1989 | * |
| 1990 | * fills out function pointers in the net_device structure |
| 1991 | */ |
| 1992 | static void |
| 1993 | spider_net_setup_netdev_ops(struct net_device *netdev) |
| 1994 | { |
| 1995 | netdev->open = &spider_net_open; |
| 1996 | netdev->stop = &spider_net_stop; |
| 1997 | netdev->hard_start_xmit = &spider_net_xmit; |
| 1998 | netdev->get_stats = &spider_net_get_stats; |
| 1999 | netdev->set_multicast_list = &spider_net_set_multi; |
| 2000 | netdev->set_mac_address = &spider_net_set_mac; |
| 2001 | netdev->change_mtu = &spider_net_change_mtu; |
| 2002 | netdev->do_ioctl = &spider_net_do_ioctl; |
| 2003 | /* tx watchdog */ |
| 2004 | netdev->tx_timeout = &spider_net_tx_timeout; |
| 2005 | netdev->watchdog_timeo = SPIDER_NET_WATCHDOG_TIMEOUT; |
| 2006 | /* NAPI */ |
| 2007 | netdev->poll = &spider_net_poll; |
| 2008 | netdev->weight = SPIDER_NET_NAPI_WEIGHT; |
| 2009 | /* HW VLAN */ |
| 2010 | netdev->vlan_rx_register = &spider_net_vlan_rx_reg; |
| 2011 | netdev->vlan_rx_add_vid = &spider_net_vlan_rx_add; |
| 2012 | netdev->vlan_rx_kill_vid = &spider_net_vlan_rx_kill; |
| 2013 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 2014 | /* poll controller */ |
| 2015 | netdev->poll_controller = &spider_net_poll_controller; |
| 2016 | #endif /* CONFIG_NET_POLL_CONTROLLER */ |
| 2017 | /* ethtool ops */ |
| 2018 | netdev->ethtool_ops = &spider_net_ethtool_ops; |
| 2019 | } |
| 2020 | |
| 2021 | /** |
| 2022 | * spider_net_setup_netdev - initialization of net_device |
| 2023 | * @card: card structure |
| 2024 | * |
| 2025 | * Returns 0 on success or <0 on failure |
| 2026 | * |
| 2027 | * spider_net_setup_netdev initializes the net_device structure |
| 2028 | **/ |
| 2029 | static int |
| 2030 | spider_net_setup_netdev(struct spider_net_card *card) |
| 2031 | { |
| 2032 | int result; |
| 2033 | struct net_device *netdev = card->netdev; |
| 2034 | struct device_node *dn; |
| 2035 | struct sockaddr addr; |
Jeremy Kerr | 1a2509c | 2006-07-12 15:41:03 +1000 | [diff] [blame] | 2036 | const u8 *mac; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 2037 | |
| 2038 | SET_MODULE_OWNER(netdev); |
| 2039 | SET_NETDEV_DEV(netdev, &card->pdev->dev); |
| 2040 | |
| 2041 | pci_set_drvdata(card->pdev, netdev); |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 2042 | |
Arnd Bergmann | 11f1a52 | 2006-01-12 17:16:44 -0500 | [diff] [blame] | 2043 | init_timer(&card->tx_timer); |
| 2044 | card->tx_timer.function = |
| 2045 | (void (*)(unsigned long)) spider_net_cleanup_tx_ring; |
| 2046 | card->tx_timer.data = (unsigned long) card; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 2047 | netdev->irq = card->pdev->irq; |
| 2048 | |
| 2049 | card->options.rx_csum = SPIDER_NET_RX_CSUM_DEFAULT; |
| 2050 | |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 2051 | card->tx_chain.num_desc = tx_descriptors; |
| 2052 | card->rx_chain.num_desc = rx_descriptors; |
Jim Lewis | b68a60e | 2006-08-14 23:00:23 -0700 | [diff] [blame] | 2053 | |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 2054 | spider_net_setup_netdev_ops(netdev); |
| 2055 | |
Jens Osterkamp | bdd0150 | 2006-07-13 11:54:08 +0200 | [diff] [blame] | 2056 | netdev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 2057 | /* some time: NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX | |
| 2058 | * NETIF_F_HW_VLAN_FILTER */ |
| 2059 | |
| 2060 | netdev->irq = card->pdev->irq; |
| 2061 | |
| 2062 | dn = pci_device_to_OF_node(card->pdev); |
Jens Osterkamp | 543cec5 | 2005-09-06 19:30:54 -0700 | [diff] [blame] | 2063 | if (!dn) |
| 2064 | return -EIO; |
| 2065 | |
Jeremy Kerr | 1a2509c | 2006-07-12 15:41:03 +1000 | [diff] [blame] | 2066 | mac = get_property(dn, "local-mac-address", NULL); |
Jens Osterkamp | 543cec5 | 2005-09-06 19:30:54 -0700 | [diff] [blame] | 2067 | if (!mac) |
| 2068 | return -EIO; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 2069 | memcpy(addr.sa_data, mac, ETH_ALEN); |
| 2070 | |
| 2071 | result = spider_net_set_mac(netdev, &addr); |
| 2072 | if ((result) && (netif_msg_probe(card))) |
| 2073 | pr_err("Failed to set MAC address: %i\n", result); |
| 2074 | |
| 2075 | result = register_netdev(netdev); |
| 2076 | if (result) { |
| 2077 | if (netif_msg_probe(card)) |
| 2078 | pr_err("Couldn't register net_device: %i\n", |
| 2079 | result); |
| 2080 | return result; |
| 2081 | } |
| 2082 | |
| 2083 | if (netif_msg_probe(card)) |
| 2084 | pr_info("Initialized device %s.\n", netdev->name); |
| 2085 | |
| 2086 | return 0; |
| 2087 | } |
| 2088 | |
| 2089 | /** |
| 2090 | * spider_net_alloc_card - allocates net_device and card structure |
| 2091 | * |
| 2092 | * returns the card structure or NULL in case of errors |
| 2093 | * |
| 2094 | * the card and net_device structures are linked to each other |
| 2095 | */ |
| 2096 | static struct spider_net_card * |
| 2097 | spider_net_alloc_card(void) |
| 2098 | { |
| 2099 | struct net_device *netdev; |
| 2100 | struct spider_net_card *card; |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 2101 | |
Linas Vepstas | d4ed8f8 | 2006-12-13 15:06:59 -0600 | [diff] [blame] | 2102 | netdev = alloc_etherdev(sizeof(struct spider_net_card)); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 2103 | if (!netdev) |
| 2104 | return NULL; |
| 2105 | |
| 2106 | card = netdev_priv(netdev); |
| 2107 | card->netdev = netdev; |
| 2108 | card->msg_enable = SPIDER_NET_DEFAULT_MSG; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 2109 | INIT_WORK(&card->tx_timeout_task, spider_net_tx_timeout_task); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 2110 | init_waitqueue_head(&card->waitq); |
| 2111 | atomic_set(&card->tx_timeout_task_counter, 0); |
| 2112 | |
| 2113 | return card; |
| 2114 | } |
| 2115 | |
| 2116 | /** |
| 2117 | * spider_net_undo_pci_setup - releases PCI ressources |
| 2118 | * @card: card structure |
| 2119 | * |
| 2120 | * spider_net_undo_pci_setup releases the mapped regions |
| 2121 | */ |
| 2122 | static void |
| 2123 | spider_net_undo_pci_setup(struct spider_net_card *card) |
| 2124 | { |
| 2125 | iounmap(card->regs); |
| 2126 | pci_release_regions(card->pdev); |
| 2127 | } |
| 2128 | |
| 2129 | /** |
| 2130 | * spider_net_setup_pci_dev - sets up the device in terms of PCI operations |
| 2131 | * @card: card structure |
| 2132 | * @pdev: PCI device |
| 2133 | * |
| 2134 | * Returns the card structure or NULL if any errors occur |
| 2135 | * |
| 2136 | * spider_net_setup_pci_dev initializes pdev and together with the |
| 2137 | * functions called in spider_net_open configures the device so that |
| 2138 | * data can be transferred over it |
| 2139 | * The net_device structure is attached to the card structure, if the |
| 2140 | * function returns without error. |
| 2141 | **/ |
| 2142 | static struct spider_net_card * |
| 2143 | spider_net_setup_pci_dev(struct pci_dev *pdev) |
| 2144 | { |
| 2145 | struct spider_net_card *card; |
| 2146 | unsigned long mmio_start, mmio_len; |
| 2147 | |
| 2148 | if (pci_enable_device(pdev)) { |
| 2149 | pr_err("Couldn't enable PCI device\n"); |
| 2150 | return NULL; |
| 2151 | } |
| 2152 | |
| 2153 | if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) { |
| 2154 | pr_err("Couldn't find proper PCI device base address.\n"); |
| 2155 | goto out_disable_dev; |
| 2156 | } |
| 2157 | |
| 2158 | if (pci_request_regions(pdev, spider_net_driver_name)) { |
| 2159 | pr_err("Couldn't obtain PCI resources, aborting.\n"); |
| 2160 | goto out_disable_dev; |
| 2161 | } |
| 2162 | |
| 2163 | pci_set_master(pdev); |
| 2164 | |
| 2165 | card = spider_net_alloc_card(); |
| 2166 | if (!card) { |
| 2167 | pr_err("Couldn't allocate net_device structure, " |
| 2168 | "aborting.\n"); |
| 2169 | goto out_release_regions; |
| 2170 | } |
| 2171 | card->pdev = pdev; |
| 2172 | |
| 2173 | /* fetch base address and length of first resource */ |
| 2174 | mmio_start = pci_resource_start(pdev, 0); |
| 2175 | mmio_len = pci_resource_len(pdev, 0); |
| 2176 | |
| 2177 | card->netdev->mem_start = mmio_start; |
| 2178 | card->netdev->mem_end = mmio_start + mmio_len; |
| 2179 | card->regs = ioremap(mmio_start, mmio_len); |
| 2180 | |
| 2181 | if (!card->regs) { |
| 2182 | pr_err("Couldn't obtain PCI resources, aborting.\n"); |
| 2183 | goto out_release_regions; |
| 2184 | } |
| 2185 | |
| 2186 | return card; |
| 2187 | |
| 2188 | out_release_regions: |
| 2189 | pci_release_regions(pdev); |
| 2190 | out_disable_dev: |
| 2191 | pci_disable_device(pdev); |
| 2192 | pci_set_drvdata(pdev, NULL); |
| 2193 | return NULL; |
| 2194 | } |
| 2195 | |
| 2196 | /** |
| 2197 | * spider_net_probe - initialization of a device |
| 2198 | * @pdev: PCI device |
| 2199 | * @ent: entry in the device id list |
| 2200 | * |
| 2201 | * Returns 0 on success, <0 on failure |
| 2202 | * |
| 2203 | * spider_net_probe initializes pdev and registers a net_device |
| 2204 | * structure for it. After that, the device can be ifconfig'ed up |
| 2205 | **/ |
| 2206 | static int __devinit |
| 2207 | spider_net_probe(struct pci_dev *pdev, const struct pci_device_id *ent) |
| 2208 | { |
| 2209 | int err = -EIO; |
| 2210 | struct spider_net_card *card; |
| 2211 | |
| 2212 | card = spider_net_setup_pci_dev(pdev); |
| 2213 | if (!card) |
| 2214 | goto out; |
| 2215 | |
| 2216 | spider_net_workaround_rxramfull(card); |
| 2217 | spider_net_init_card(card); |
| 2218 | |
| 2219 | err = spider_net_setup_phy(card); |
| 2220 | if (err) |
| 2221 | goto out_undo_pci; |
| 2222 | |
| 2223 | err = spider_net_init_firmware(card); |
| 2224 | if (err) |
| 2225 | goto out_undo_pci; |
| 2226 | |
| 2227 | err = spider_net_setup_netdev(card); |
| 2228 | if (err) |
| 2229 | goto out_undo_pci; |
| 2230 | |
| 2231 | return 0; |
| 2232 | |
| 2233 | out_undo_pci: |
| 2234 | spider_net_undo_pci_setup(card); |
| 2235 | free_netdev(card->netdev); |
| 2236 | out: |
| 2237 | return err; |
| 2238 | } |
| 2239 | |
| 2240 | /** |
| 2241 | * spider_net_remove - removal of a device |
| 2242 | * @pdev: PCI device |
| 2243 | * |
| 2244 | * Returns 0 on success, <0 on failure |
| 2245 | * |
| 2246 | * spider_net_remove is called to remove the device and unregisters the |
| 2247 | * net_device |
| 2248 | **/ |
| 2249 | static void __devexit |
| 2250 | spider_net_remove(struct pci_dev *pdev) |
| 2251 | { |
| 2252 | struct net_device *netdev; |
| 2253 | struct spider_net_card *card; |
| 2254 | |
| 2255 | netdev = pci_get_drvdata(pdev); |
| 2256 | card = netdev_priv(netdev); |
| 2257 | |
| 2258 | wait_event(card->waitq, |
| 2259 | atomic_read(&card->tx_timeout_task_counter) == 0); |
| 2260 | |
| 2261 | unregister_netdev(netdev); |
Jens Osterkamp | 543cec5 | 2005-09-06 19:30:54 -0700 | [diff] [blame] | 2262 | |
| 2263 | /* switch off card */ |
| 2264 | spider_net_write_reg(card, SPIDER_NET_CKRCTRL, |
| 2265 | SPIDER_NET_CKRCTRL_STOP_VALUE); |
| 2266 | spider_net_write_reg(card, SPIDER_NET_CKRCTRL, |
| 2267 | SPIDER_NET_CKRCTRL_RUN_VALUE); |
| 2268 | |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 2269 | spider_net_undo_pci_setup(card); |
| 2270 | free_netdev(netdev); |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 2271 | } |
| 2272 | |
| 2273 | static struct pci_driver spider_net_driver = { |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 2274 | .name = spider_net_driver_name, |
| 2275 | .id_table = spider_net_pci_tbl, |
| 2276 | .probe = spider_net_probe, |
| 2277 | .remove = __devexit_p(spider_net_remove) |
| 2278 | }; |
| 2279 | |
| 2280 | /** |
| 2281 | * spider_net_init - init function when the driver is loaded |
| 2282 | * |
| 2283 | * spider_net_init registers the device driver |
| 2284 | */ |
| 2285 | static int __init spider_net_init(void) |
| 2286 | { |
Linas Vepstas | 90f1084 | 2006-10-10 15:56:04 -0500 | [diff] [blame] | 2287 | printk(KERN_INFO "Spidernet version %s.\n", VERSION); |
| 2288 | |
Jens Osterkamp | aaec0fa | 2005-09-05 15:19:29 -0700 | [diff] [blame] | 2289 | if (rx_descriptors < SPIDER_NET_RX_DESCRIPTORS_MIN) { |
| 2290 | rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MIN; |
| 2291 | pr_info("adjusting rx descriptors to %i.\n", rx_descriptors); |
| 2292 | } |
| 2293 | if (rx_descriptors > SPIDER_NET_RX_DESCRIPTORS_MAX) { |
| 2294 | rx_descriptors = SPIDER_NET_RX_DESCRIPTORS_MAX; |
| 2295 | pr_info("adjusting rx descriptors to %i.\n", rx_descriptors); |
| 2296 | } |
| 2297 | if (tx_descriptors < SPIDER_NET_TX_DESCRIPTORS_MIN) { |
| 2298 | tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MIN; |
| 2299 | pr_info("adjusting tx descriptors to %i.\n", tx_descriptors); |
| 2300 | } |
| 2301 | if (tx_descriptors > SPIDER_NET_TX_DESCRIPTORS_MAX) { |
| 2302 | tx_descriptors = SPIDER_NET_TX_DESCRIPTORS_MAX; |
| 2303 | pr_info("adjusting tx descriptors to %i.\n", tx_descriptors); |
| 2304 | } |
| 2305 | |
| 2306 | return pci_register_driver(&spider_net_driver); |
| 2307 | } |
| 2308 | |
| 2309 | /** |
| 2310 | * spider_net_cleanup - exit function when driver is unloaded |
| 2311 | * |
| 2312 | * spider_net_cleanup unregisters the device driver |
| 2313 | */ |
| 2314 | static void __exit spider_net_cleanup(void) |
| 2315 | { |
| 2316 | pci_unregister_driver(&spider_net_driver); |
| 2317 | } |
| 2318 | |
| 2319 | module_init(spider_net_init); |
| 2320 | module_exit(spider_net_cleanup); |