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