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