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