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