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