1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * PS3 gelic network driver. 4 * 5 * Copyright (C) 2007 Sony Computer Entertainment Inc. 6 * Copyright 2006, 2007 Sony Corporation 7 * 8 * This file is based on: spider_net.c 9 * 10 * (C) Copyright IBM Corp. 2005 11 * 12 * Authors : Utz Bacher <utz.bacher@de.ibm.com> 13 * Jens Osterkamp <Jens.Osterkamp@de.ibm.com> 14 */ 15 16 #undef DEBUG 17 18 #include <linux/interrupt.h> 19 #include <linux/kernel.h> 20 #include <linux/module.h> 21 #include <linux/slab.h> 22 23 #include <linux/etherdevice.h> 24 #include <linux/ethtool.h> 25 #include <linux/if_vlan.h> 26 27 #include <linux/in.h> 28 #include <linux/ip.h> 29 #include <linux/tcp.h> 30 31 #include <linux/dma-mapping.h> 32 #include <net/checksum.h> 33 #include <asm/firmware.h> 34 #include <asm/ps3.h> 35 #include <asm/lv1call.h> 36 37 #include "ps3_gelic_net.h" 38 #include "ps3_gelic_wireless.h" 39 40 #define DRV_NAME "Gelic Network Driver" 41 #define DRV_VERSION "2.0" 42 43 MODULE_AUTHOR("SCE Inc."); 44 MODULE_DESCRIPTION("Gelic Network driver"); 45 MODULE_LICENSE("GPL"); 46 47 48 /* set irq_mask */ 49 int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask) 50 { 51 int status; 52 53 status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card), 54 mask, 0); 55 if (status) 56 dev_info(ctodev(card), 57 "%s failed %d\n", __func__, status); 58 return status; 59 } 60 61 static void gelic_card_rx_irq_on(struct gelic_card *card) 62 { 63 card->irq_mask |= GELIC_CARD_RXINT; 64 gelic_card_set_irq_mask(card, card->irq_mask); 65 } 66 static void gelic_card_rx_irq_off(struct gelic_card *card) 67 { 68 card->irq_mask &= ~GELIC_CARD_RXINT; 69 gelic_card_set_irq_mask(card, card->irq_mask); 70 } 71 72 static void gelic_card_get_ether_port_status(struct gelic_card *card, 73 int inform) 74 { 75 u64 v2; 76 struct net_device *ether_netdev; 77 78 lv1_net_control(bus_id(card), dev_id(card), 79 GELIC_LV1_GET_ETH_PORT_STATUS, 80 GELIC_LV1_VLAN_TX_ETHERNET_0, 0, 0, 81 &card->ether_port_status, &v2); 82 83 if (inform) { 84 ether_netdev = card->netdev[GELIC_PORT_ETHERNET_0]; 85 if (card->ether_port_status & GELIC_LV1_ETHER_LINK_UP) 86 netif_carrier_on(ether_netdev); 87 else 88 netif_carrier_off(ether_netdev); 89 } 90 } 91 92 /** 93 * gelic_descr_get_status -- returns the status of a descriptor 94 * @descr: descriptor to look at 95 * 96 * returns the status as in the hw_regs.dmac_cmd_status field of the descriptor 97 */ 98 static enum gelic_descr_dma_status 99 gelic_descr_get_status(struct gelic_descr *descr) 100 { 101 return be32_to_cpu(descr->hw_regs.dmac_cmd_status) & 102 GELIC_DESCR_DMA_STAT_MASK; 103 } 104 105 static int gelic_card_set_link_mode(struct gelic_card *card, int mode) 106 { 107 int status; 108 u64 v1, v2; 109 110 status = lv1_net_control(bus_id(card), dev_id(card), 111 GELIC_LV1_SET_NEGOTIATION_MODE, 112 GELIC_LV1_PHY_ETHERNET_0, mode, 0, &v1, &v2); 113 if (status) { 114 pr_info("%s: failed setting negotiation mode %d\n", __func__, 115 status); 116 return -EBUSY; 117 } 118 119 card->link_mode = mode; 120 return 0; 121 } 122 123 /** 124 * gelic_card_disable_txdmac - disables the transmit DMA controller 125 * @card: card structure 126 * 127 * gelic_card_disable_txdmac terminates processing on the DMA controller by 128 * turing off DMA and issuing a force end 129 */ 130 static void gelic_card_disable_txdmac(struct gelic_card *card) 131 { 132 int status; 133 134 /* this hvc blocks until the DMA in progress really stopped */ 135 status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card)); 136 if (status) 137 dev_err(ctodev(card), 138 "lv1_net_stop_tx_dma failed, status=%d\n", status); 139 } 140 141 /** 142 * gelic_card_enable_rxdmac - enables the receive DMA controller 143 * @card: card structure 144 * 145 * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN 146 * in the GDADMACCNTR register 147 */ 148 static void gelic_card_enable_rxdmac(struct gelic_card *card) 149 { 150 int status; 151 152 #ifdef DEBUG 153 if (gelic_descr_get_status(card->rx_chain.head) != 154 GELIC_DESCR_DMA_CARDOWNED) { 155 printk(KERN_ERR "%s: status=%x\n", __func__, 156 be32_to_cpu(card->rx_chain.head->hw_regs.dmac_cmd_status)); 157 printk(KERN_ERR "%s: nextphy=%x\n", __func__, 158 be32_to_cpu(card->rx_chain.head->hw_regs.next_descr_addr)); 159 printk(KERN_ERR "%s: head=%p\n", __func__, 160 card->rx_chain.head); 161 } 162 #endif 163 status = lv1_net_start_rx_dma(bus_id(card), dev_id(card), 164 card->rx_chain.head->link.cpu_addr, 0); 165 if (status) 166 dev_info(ctodev(card), 167 "lv1_net_start_rx_dma failed, status=%d\n", status); 168 } 169 170 /** 171 * gelic_card_disable_rxdmac - disables the receive DMA controller 172 * @card: card structure 173 * 174 * gelic_card_disable_rxdmac terminates processing on the DMA controller by 175 * turing off DMA and issuing a force end 176 */ 177 static void gelic_card_disable_rxdmac(struct gelic_card *card) 178 { 179 int status; 180 181 /* this hvc blocks until the DMA in progress really stopped */ 182 status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card)); 183 if (status) 184 dev_err(ctodev(card), 185 "lv1_net_stop_rx_dma failed, %d\n", status); 186 } 187 188 /** 189 * gelic_descr_set_status -- sets the status of a descriptor 190 * @descr: descriptor to change 191 * @status: status to set in the descriptor 192 * 193 * changes the status to the specified value. Doesn't change other bits 194 * in the status 195 */ 196 static void gelic_descr_set_status(struct gelic_descr *descr, 197 enum gelic_descr_dma_status status) 198 { 199 descr->hw_regs.dmac_cmd_status = cpu_to_be32(status | 200 (be32_to_cpu(descr->hw_regs.dmac_cmd_status) & 201 ~GELIC_DESCR_DMA_STAT_MASK)); 202 /* 203 * dma_cmd_status field is used to indicate whether the descriptor 204 * is valid or not. 205 * Usually caller of this function wants to inform that to the 206 * hardware, so we assure here the hardware sees the change. 207 */ 208 wmb(); 209 } 210 211 /** 212 * gelic_card_reset_chain - reset status of a descriptor chain 213 * @card: card structure 214 * @chain: address of chain 215 * @start_descr: address of descriptor array 216 * 217 * Reset the status of dma descriptors to ready state 218 * and re-initialize the hardware chain for later use 219 */ 220 static void gelic_card_reset_chain(struct gelic_card *card, 221 struct gelic_descr_chain *chain, 222 struct gelic_descr *start_descr) 223 { 224 struct gelic_descr *descr; 225 226 for (descr = start_descr; start_descr != descr->next; descr++) { 227 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED); 228 descr->hw_regs.next_descr_addr 229 = cpu_to_be32(descr->next->link.cpu_addr); 230 } 231 232 chain->head = start_descr; 233 chain->tail = (descr - 1); 234 235 (descr - 1)->hw_regs.next_descr_addr = 0; 236 } 237 238 void gelic_card_up(struct gelic_card *card) 239 { 240 pr_debug("%s: called\n", __func__); 241 mutex_lock(&card->updown_lock); 242 if (atomic_inc_return(&card->users) == 1) { 243 pr_debug("%s: real do\n", __func__); 244 /* enable irq */ 245 gelic_card_set_irq_mask(card, card->irq_mask); 246 /* start rx */ 247 gelic_card_enable_rxdmac(card); 248 249 napi_enable(&card->napi); 250 } 251 mutex_unlock(&card->updown_lock); 252 pr_debug("%s: done\n", __func__); 253 } 254 255 void gelic_card_down(struct gelic_card *card) 256 { 257 u64 mask; 258 pr_debug("%s: called\n", __func__); 259 mutex_lock(&card->updown_lock); 260 if (atomic_dec_if_positive(&card->users) == 0) { 261 pr_debug("%s: real do\n", __func__); 262 napi_disable(&card->napi); 263 timer_delete_sync(&card->rx_oom_timer); 264 /* 265 * Disable irq. Wireless interrupts will 266 * be disabled later if any 267 */ 268 mask = card->irq_mask & (GELIC_CARD_WLAN_EVENT_RECEIVED | 269 GELIC_CARD_WLAN_COMMAND_COMPLETED); 270 gelic_card_set_irq_mask(card, mask); 271 /* stop rx */ 272 gelic_card_disable_rxdmac(card); 273 gelic_card_reset_chain(card, &card->rx_chain, 274 card->descr + GELIC_NET_TX_DESCRIPTORS); 275 /* stop tx */ 276 gelic_card_disable_txdmac(card); 277 } 278 mutex_unlock(&card->updown_lock); 279 pr_debug("%s: done\n", __func__); 280 } 281 282 /** 283 * gelic_card_free_chain - free descriptor chain 284 * @card: card structure 285 * @descr_in: address of desc 286 */ 287 static void gelic_card_free_chain(struct gelic_card *card, 288 struct gelic_descr *descr_in) 289 { 290 struct gelic_descr *descr; 291 292 for (descr = descr_in; descr && descr->link.cpu_addr; 293 descr = descr->next) { 294 dma_unmap_single(ctodev(card), descr->link.cpu_addr, 295 descr->link.size, DMA_BIDIRECTIONAL); 296 descr->link.cpu_addr = 0; 297 descr->link.size = 0; 298 } 299 } 300 301 /** 302 * gelic_card_init_chain - links descriptor chain 303 * @card: card structure 304 * @chain: address of chain 305 * @start_descr: address of descriptor array 306 * @no: number of descriptors 307 * 308 * we manage a circular list that mirrors the hardware structure, 309 * except that the hardware uses bus addresses. 310 * 311 * returns 0 on success, <0 on failure 312 */ 313 static int gelic_card_init_chain(struct gelic_card *card, 314 struct gelic_descr_chain *chain, 315 struct gelic_descr *start_descr, int no) 316 { 317 int i; 318 struct gelic_descr *descr; 319 320 descr = start_descr; 321 memset(descr, 0, sizeof(*descr) * no); 322 323 /* set up the hardware pointers in each descriptor */ 324 for (i = 0; i < no; i++, descr++) { 325 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE); 326 327 descr->link.size = sizeof(struct gelic_hw_regs); 328 descr->link.cpu_addr = dma_map_single(ctodev(card), descr, 329 descr->link.size, DMA_BIDIRECTIONAL); 330 331 if (dma_mapping_error(ctodev(card), descr->link.cpu_addr)) { 332 for (i--, descr--; 0 <= i; i--, descr--) { 333 dma_unmap_single(ctodev(card), 334 descr->link.cpu_addr, descr->link.size, 335 DMA_BIDIRECTIONAL); 336 } 337 return -ENOMEM; 338 } 339 340 descr->next = descr + 1; 341 descr->prev = descr - 1; 342 } 343 /* make them as ring */ 344 (descr - 1)->next = start_descr; 345 start_descr->prev = (descr - 1); 346 347 /* chain bus addr of hw descriptor */ 348 descr = start_descr; 349 for (i = 0; i < no; i++, descr++) { 350 descr->hw_regs.next_descr_addr = 351 cpu_to_be32(descr->next->link.cpu_addr); 352 } 353 354 chain->head = start_descr; 355 chain->tail = start_descr; 356 357 /* do not chain last hw descriptor */ 358 (descr - 1)->hw_regs.next_descr_addr = 0; 359 360 return 0; 361 } 362 363 /** 364 * gelic_descr_prepare_rx - reinitializes a rx descriptor 365 * @card: card structure 366 * @descr: descriptor to re-init 367 * @napi_mode: is it running in napi poll 368 * 369 * return 0 on success, <0 on failure 370 * 371 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor. 372 * Activate the descriptor state-wise 373 * 374 * Gelic RX sk_buffs must be aligned to GELIC_NET_RXBUF_ALIGN and the length 375 * must be a multiple of GELIC_NET_RXBUF_ALIGN. 376 */ 377 static int gelic_descr_prepare_rx(struct gelic_card *card, 378 struct gelic_descr *descr, 379 bool napi_mode) 380 { 381 static const unsigned int rx_skb_size = 382 ALIGN(GELIC_NET_MAX_FRAME, GELIC_NET_RXBUF_ALIGN) + 383 GELIC_NET_RXBUF_ALIGN - 1; 384 dma_addr_t cpu_addr; 385 int offset; 386 387 if (gelic_descr_get_status(descr) != GELIC_DESCR_DMA_NOT_IN_USE) 388 dev_info(ctodev(card), "%s: ERROR status\n", __func__); 389 390 descr->hw_regs.dmac_cmd_status = 0; 391 descr->hw_regs.result_size = 0; 392 descr->hw_regs.valid_size = 0; 393 descr->hw_regs.data_error = 0; 394 descr->hw_regs.payload.dev_addr = 0; 395 descr->hw_regs.payload.size = 0; 396 397 if (napi_mode) 398 descr->skb = napi_alloc_skb(&card->napi, rx_skb_size); 399 else 400 descr->skb = netdev_alloc_skb(*card->netdev, rx_skb_size); 401 if (!descr->skb) { 402 descr->hw_regs.payload.dev_addr = 0; /* tell DMAC don't touch memory */ 403 return -ENOMEM; 404 } 405 406 offset = ((unsigned long)descr->skb->data) & 407 (GELIC_NET_RXBUF_ALIGN - 1); 408 if (offset) 409 skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset); 410 /* io-mmu-map the skb */ 411 cpu_addr = dma_map_single(ctodev(card), descr->skb->data, 412 GELIC_NET_MAX_FRAME, DMA_FROM_DEVICE); 413 descr->hw_regs.payload.dev_addr = cpu_to_be32(cpu_addr); 414 if (dma_mapping_error(ctodev(card), cpu_addr)) { 415 dev_kfree_skb_any(descr->skb); 416 descr->skb = NULL; 417 dev_info(ctodev(card), 418 "%s:Could not iommu-map rx buffer\n", __func__); 419 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE); 420 return -ENOMEM; 421 } 422 423 descr->hw_regs.payload.size = cpu_to_be32(GELIC_NET_MAX_FRAME); 424 descr->hw_regs.payload.dev_addr = cpu_to_be32(cpu_addr); 425 426 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED); 427 428 return 0; 429 } 430 431 /** 432 * gelic_card_release_rx_chain - free all skb of rx descr 433 * @card: card structure 434 * 435 */ 436 static void gelic_card_release_rx_chain(struct gelic_card *card) 437 { 438 struct gelic_descr *descr = card->rx_chain.head; 439 440 do { 441 if (descr->skb) { 442 dma_unmap_single(ctodev(card), 443 be32_to_cpu(descr->hw_regs.payload.dev_addr), 444 descr->skb->len, 445 DMA_FROM_DEVICE); 446 descr->hw_regs.payload.dev_addr = 0; 447 descr->hw_regs.payload.size = 0; 448 dev_kfree_skb_any(descr->skb); 449 descr->skb = NULL; 450 gelic_descr_set_status(descr, 451 GELIC_DESCR_DMA_NOT_IN_USE); 452 } 453 descr = descr->next; 454 } while (descr != card->rx_chain.head); 455 } 456 457 /** 458 * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains 459 * @card: card structure 460 * 461 * fills all descriptors in the rx chain: allocates skbs 462 * and iommu-maps them. 463 * returns 0 on success, < 0 on failure 464 */ 465 static int gelic_card_fill_rx_chain(struct gelic_card *card) 466 { 467 struct gelic_descr *descr = card->rx_chain.head; 468 int ret; 469 470 do { 471 if (!descr->skb) { 472 ret = gelic_descr_prepare_rx(card, descr, false); 473 if (ret) 474 goto rewind; 475 } 476 descr = descr->next; 477 } while (descr != card->rx_chain.head); 478 479 return 0; 480 rewind: 481 gelic_card_release_rx_chain(card); 482 return ret; 483 } 484 485 /** 486 * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains 487 * @card: card structure 488 * 489 * returns 0 on success, < 0 on failure 490 */ 491 static int gelic_card_alloc_rx_skbs(struct gelic_card *card) 492 { 493 struct gelic_descr_chain *chain; 494 int ret; 495 chain = &card->rx_chain; 496 ret = gelic_card_fill_rx_chain(card); 497 chain->tail = card->rx_top->prev; /* point to the last */ 498 return ret; 499 } 500 501 /** 502 * gelic_descr_release_tx - processes a used tx descriptor 503 * @card: card structure 504 * @descr: descriptor to release 505 * 506 * releases a used tx descriptor (unmapping, freeing of skb) 507 */ 508 static void gelic_descr_release_tx(struct gelic_card *card, 509 struct gelic_descr *descr) 510 { 511 struct sk_buff *skb = descr->skb; 512 513 BUG_ON(!(be32_to_cpu(descr->hw_regs.data_status) & GELIC_DESCR_TX_TAIL)); 514 515 dma_unmap_single(ctodev(card), 516 be32_to_cpu(descr->hw_regs.payload.dev_addr), skb->len, 517 DMA_TO_DEVICE); 518 dev_kfree_skb_any(skb); 519 520 descr->hw_regs.payload.dev_addr = 0; 521 descr->hw_regs.payload.size = 0; 522 descr->hw_regs.next_descr_addr = 0; 523 descr->hw_regs.result_size = 0; 524 descr->hw_regs.valid_size = 0; 525 descr->hw_regs.data_status = 0; 526 descr->hw_regs.data_error = 0; 527 descr->skb = NULL; 528 529 /* set descr status */ 530 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE); 531 } 532 533 static void gelic_card_stop_queues(struct gelic_card *card) 534 { 535 netif_stop_queue(card->netdev[GELIC_PORT_ETHERNET_0]); 536 537 if (card->netdev[GELIC_PORT_WIRELESS]) 538 netif_stop_queue(card->netdev[GELIC_PORT_WIRELESS]); 539 } 540 static void gelic_card_wake_queues(struct gelic_card *card) 541 { 542 netif_wake_queue(card->netdev[GELIC_PORT_ETHERNET_0]); 543 544 if (card->netdev[GELIC_PORT_WIRELESS]) 545 netif_wake_queue(card->netdev[GELIC_PORT_WIRELESS]); 546 } 547 /** 548 * gelic_card_release_tx_chain - processes sent tx descriptors 549 * @card: adapter structure 550 * @stop: net_stop sequence 551 * 552 * releases the tx descriptors that gelic has finished with 553 */ 554 static void gelic_card_release_tx_chain(struct gelic_card *card, int stop) 555 { 556 struct gelic_descr_chain *tx_chain; 557 enum gelic_descr_dma_status status; 558 struct net_device *netdev; 559 int release = 0; 560 561 for (tx_chain = &card->tx_chain; 562 tx_chain->head != tx_chain->tail && tx_chain->tail; 563 tx_chain->tail = tx_chain->tail->next) { 564 status = gelic_descr_get_status(tx_chain->tail); 565 netdev = tx_chain->tail->skb->dev; 566 switch (status) { 567 case GELIC_DESCR_DMA_RESPONSE_ERROR: 568 case GELIC_DESCR_DMA_PROTECTION_ERROR: 569 case GELIC_DESCR_DMA_FORCE_END: 570 if (printk_ratelimit()) 571 dev_info(ctodev(card), 572 "%s: forcing end of tx descriptor " \ 573 "with status %x\n", 574 __func__, status); 575 netdev->stats.tx_dropped++; 576 break; 577 578 case GELIC_DESCR_DMA_COMPLETE: 579 if (tx_chain->tail->skb) { 580 netdev->stats.tx_packets++; 581 netdev->stats.tx_bytes += 582 tx_chain->tail->skb->len; 583 } 584 break; 585 586 case GELIC_DESCR_DMA_CARDOWNED: 587 /* pending tx request */ 588 default: 589 /* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */ 590 if (!stop) 591 goto out; 592 } 593 gelic_descr_release_tx(card, tx_chain->tail); 594 release ++; 595 } 596 out: 597 if (!stop && release) 598 gelic_card_wake_queues(card); 599 } 600 601 /** 602 * gelic_net_set_multi - sets multicast addresses and promisc flags 603 * @netdev: interface device structure 604 * 605 * gelic_net_set_multi configures multicast addresses as needed for the 606 * netdev interface. It also sets up multicast, allmulti and promisc 607 * flags appropriately 608 */ 609 void gelic_net_set_multi(struct net_device *netdev) 610 { 611 struct gelic_card *card = netdev_card(netdev); 612 struct netdev_hw_addr *ha; 613 unsigned int i; 614 uint8_t *p; 615 u64 addr; 616 int status; 617 618 /* clear all multicast address */ 619 status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card), 620 0, 1); 621 if (status) 622 dev_err(ctodev(card), 623 "lv1_net_remove_multicast_address failed %d\n", 624 status); 625 /* set broadcast address */ 626 status = lv1_net_add_multicast_address(bus_id(card), dev_id(card), 627 GELIC_NET_BROADCAST_ADDR, 0); 628 if (status) 629 dev_err(ctodev(card), 630 "lv1_net_add_multicast_address failed, %d\n", 631 status); 632 633 if ((netdev->flags & IFF_ALLMULTI) || 634 (netdev_mc_count(netdev) > GELIC_NET_MC_COUNT_MAX)) { 635 status = lv1_net_add_multicast_address(bus_id(card), 636 dev_id(card), 637 0, 1); 638 if (status) 639 dev_err(ctodev(card), 640 "lv1_net_add_multicast_address failed, %d\n", 641 status); 642 return; 643 } 644 645 /* set multicast addresses */ 646 netdev_for_each_mc_addr(ha, netdev) { 647 addr = 0; 648 p = ha->addr; 649 for (i = 0; i < ETH_ALEN; i++) { 650 addr <<= 8; 651 addr |= *p++; 652 } 653 status = lv1_net_add_multicast_address(bus_id(card), 654 dev_id(card), 655 addr, 0); 656 if (status) 657 dev_err(ctodev(card), 658 "lv1_net_add_multicast_address failed, %d\n", 659 status); 660 } 661 } 662 663 /** 664 * gelic_net_stop - called upon ifconfig down 665 * @netdev: interface device structure 666 * 667 * always returns 0 668 */ 669 int gelic_net_stop(struct net_device *netdev) 670 { 671 struct gelic_card *card; 672 673 pr_debug("%s: start\n", __func__); 674 675 netif_stop_queue(netdev); 676 netif_carrier_off(netdev); 677 678 card = netdev_card(netdev); 679 gelic_card_down(card); 680 681 pr_debug("%s: done\n", __func__); 682 return 0; 683 } 684 685 /** 686 * gelic_card_get_next_tx_descr - returns the next available tx descriptor 687 * @card: device structure to get descriptor from 688 * 689 * returns the address of the next descriptor, or NULL if not available. 690 */ 691 static struct gelic_descr * 692 gelic_card_get_next_tx_descr(struct gelic_card *card) 693 { 694 if (!card->tx_chain.head) 695 return NULL; 696 /* see if the next descriptor is free */ 697 if (card->tx_chain.tail != card->tx_chain.head->next && 698 gelic_descr_get_status(card->tx_chain.head) == 699 GELIC_DESCR_DMA_NOT_IN_USE) 700 return card->tx_chain.head; 701 else 702 return NULL; 703 704 } 705 706 /** 707 * gelic_descr_set_tx_cmdstat - sets the tx descriptor command field 708 * @descr: descriptor structure to fill out 709 * @skb: packet to consider 710 * 711 * fills out the command and status field of the descriptor structure, 712 * depending on hardware checksum settings. This function assumes a wmb() 713 * has executed before. 714 */ 715 static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr, 716 struct sk_buff *skb) 717 { 718 if (skb->ip_summed != CHECKSUM_PARTIAL) 719 descr->hw_regs.dmac_cmd_status = 720 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM | 721 GELIC_DESCR_TX_DMA_FRAME_TAIL); 722 else { 723 /* is packet ip? 724 * if yes: tcp? udp? */ 725 if (skb->protocol == htons(ETH_P_IP)) { 726 if (ip_hdr(skb)->protocol == IPPROTO_TCP) 727 descr->hw_regs.dmac_cmd_status = 728 cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM | 729 GELIC_DESCR_TX_DMA_FRAME_TAIL); 730 731 else if (ip_hdr(skb)->protocol == IPPROTO_UDP) 732 descr->hw_regs.dmac_cmd_status = 733 cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM | 734 GELIC_DESCR_TX_DMA_FRAME_TAIL); 735 else /* 736 * the stack should checksum non-tcp and non-udp 737 * packets on his own: NETIF_F_IP_CSUM 738 */ 739 descr->hw_regs.dmac_cmd_status = 740 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM | 741 GELIC_DESCR_TX_DMA_FRAME_TAIL); 742 } 743 } 744 } 745 746 static struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb, 747 unsigned short tag) 748 { 749 struct vlan_ethhdr *veth; 750 static unsigned int c; 751 752 if (skb_headroom(skb) < VLAN_HLEN) { 753 struct sk_buff *sk_tmp = skb; 754 pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c); 755 skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN); 756 if (!skb) 757 return NULL; 758 dev_kfree_skb_any(sk_tmp); 759 } 760 veth = skb_push(skb, VLAN_HLEN); 761 762 /* Move the mac addresses to the top of buffer */ 763 memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN); 764 765 veth->h_vlan_proto = cpu_to_be16(ETH_P_8021Q); 766 veth->h_vlan_TCI = htons(tag); 767 768 return skb; 769 } 770 771 /** 772 * gelic_descr_prepare_tx - setup a descriptor for sending packets 773 * @card: card structure 774 * @descr: descriptor structure 775 * @skb: packet to use 776 * 777 * returns 0 on success, <0 on failure. 778 * 779 */ 780 static int gelic_descr_prepare_tx(struct gelic_card *card, 781 struct gelic_descr *descr, 782 struct sk_buff *skb) 783 { 784 dma_addr_t buf; 785 786 if (card->vlan_required) { 787 struct sk_buff *skb_tmp; 788 enum gelic_port_type type; 789 790 type = netdev_port(skb->dev)->type; 791 skb_tmp = gelic_put_vlan_tag(skb, 792 card->vlan[type].tx); 793 if (!skb_tmp) 794 return -ENOMEM; 795 skb = skb_tmp; 796 } 797 798 buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE); 799 800 if (dma_mapping_error(ctodev(card), buf)) { 801 dev_err(ctodev(card), 802 "dma map 2 failed (%p, %i). Dropping packet\n", 803 skb->data, skb->len); 804 return -ENOMEM; 805 } 806 807 descr->hw_regs.payload.dev_addr = cpu_to_be32(buf); 808 descr->hw_regs.payload.size = cpu_to_be32(skb->len); 809 descr->skb = skb; 810 descr->hw_regs.data_status = 0; 811 descr->hw_regs.next_descr_addr = 0; /* terminate hw descr */ 812 gelic_descr_set_tx_cmdstat(descr, skb); 813 814 /* bump free descriptor pointer */ 815 card->tx_chain.head = descr->next; 816 return 0; 817 } 818 819 /** 820 * gelic_card_kick_txdma - enables TX DMA processing 821 * @card: card structure 822 * @descr: descriptor address to enable TX processing at 823 * 824 */ 825 static int gelic_card_kick_txdma(struct gelic_card *card, 826 struct gelic_descr *descr) 827 { 828 int status = 0; 829 830 if (card->tx_dma_progress) 831 return 0; 832 833 if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) { 834 card->tx_dma_progress = 1; 835 status = lv1_net_start_tx_dma(bus_id(card), dev_id(card), 836 descr->link.cpu_addr, 0); 837 if (status) { 838 card->tx_dma_progress = 0; 839 dev_info(ctodev(card), "lv1_net_start_txdma failed," \ 840 "status=%d\n", status); 841 } 842 } 843 return status; 844 } 845 846 /** 847 * gelic_net_xmit - transmits a frame over the device 848 * @skb: packet to send out 849 * @netdev: interface device structure 850 * 851 * returns NETDEV_TX_OK on success, NETDEV_TX_BUSY on failure 852 */ 853 netdev_tx_t gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev) 854 { 855 struct gelic_card *card = netdev_card(netdev); 856 struct gelic_descr *descr; 857 int result; 858 unsigned long flags; 859 860 spin_lock_irqsave(&card->tx_lock, flags); 861 862 gelic_card_release_tx_chain(card, 0); 863 864 descr = gelic_card_get_next_tx_descr(card); 865 if (!descr) { 866 /* 867 * no more descriptors free 868 */ 869 gelic_card_stop_queues(card); 870 spin_unlock_irqrestore(&card->tx_lock, flags); 871 return NETDEV_TX_BUSY; 872 } 873 874 result = gelic_descr_prepare_tx(card, descr, skb); 875 if (result) { 876 /* 877 * DMA map failed. As chances are that failure 878 * would continue, just release skb and return 879 */ 880 netdev->stats.tx_dropped++; 881 dev_kfree_skb_any(skb); 882 spin_unlock_irqrestore(&card->tx_lock, flags); 883 return NETDEV_TX_OK; 884 } 885 /* 886 * link this prepared descriptor to previous one 887 * to achieve high performance 888 */ 889 descr->prev->hw_regs.next_descr_addr = 890 cpu_to_be32(descr->link.cpu_addr); 891 /* 892 * as hardware descriptor is modified in the above lines, 893 * ensure that the hardware sees it 894 */ 895 wmb(); 896 if (gelic_card_kick_txdma(card, descr)) { 897 /* 898 * kick failed. 899 * release descriptor which was just prepared 900 */ 901 netdev->stats.tx_dropped++; 902 /* don't trigger BUG_ON() in gelic_descr_release_tx */ 903 descr->hw_regs.data_status = cpu_to_be32(GELIC_DESCR_TX_TAIL); 904 gelic_descr_release_tx(card, descr); 905 /* reset head */ 906 card->tx_chain.head = descr; 907 /* reset hw termination */ 908 descr->prev->hw_regs.next_descr_addr = 0; 909 dev_info(ctodev(card), "%s: kick failure\n", __func__); 910 } 911 912 spin_unlock_irqrestore(&card->tx_lock, flags); 913 return NETDEV_TX_OK; 914 } 915 916 /** 917 * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on 918 * @descr: descriptor to process 919 * @card: card structure 920 * @netdev: net_device structure to be passed packet 921 * 922 * iommu-unmaps the skb, fills out skb structure and passes the data to the 923 * stack. The descriptor state is not changed. 924 */ 925 static void gelic_net_pass_skb_up(struct gelic_descr *descr, 926 struct gelic_card *card, 927 struct net_device *netdev) 928 929 { 930 struct sk_buff *skb = descr->skb; 931 u32 data_status, data_error; 932 933 data_status = be32_to_cpu(descr->hw_regs.data_status); 934 data_error = be32_to_cpu(descr->hw_regs.data_error); 935 /* unmap skb buffer */ 936 dma_unmap_single(ctodev(card), 937 be32_to_cpu(descr->hw_regs.payload.dev_addr), 938 be32_to_cpu(descr->hw_regs.payload.size), DMA_FROM_DEVICE); 939 940 skb_put(skb, be32_to_cpu(descr->hw_regs.valid_size)? 941 be32_to_cpu(descr->hw_regs.valid_size) : 942 be32_to_cpu(descr->hw_regs.result_size)); 943 if (!descr->hw_regs.valid_size) 944 dev_info(ctodev(card), "buffer full %x %x %x\n", 945 be32_to_cpu(descr->hw_regs.result_size), 946 be32_to_cpu(descr->hw_regs.payload.size), 947 be32_to_cpu(descr->hw_regs.dmac_cmd_status)); 948 949 descr->skb = NULL; 950 /* 951 * the card put 2 bytes vlan tag in front 952 * of the ethernet frame 953 */ 954 skb_pull(skb, 2); 955 skb->protocol = eth_type_trans(skb, netdev); 956 957 /* checksum offload */ 958 if (netdev->features & NETIF_F_RXCSUM) { 959 if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) && 960 (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK))) 961 skb->ip_summed = CHECKSUM_UNNECESSARY; 962 else 963 skb_checksum_none_assert(skb); 964 } else 965 skb_checksum_none_assert(skb); 966 967 /* update netdevice statistics */ 968 netdev->stats.rx_packets++; 969 netdev->stats.rx_bytes += skb->len; 970 971 /* pass skb up to stack */ 972 napi_gro_receive(&card->napi, skb); 973 } 974 975 /** 976 * gelic_card_decode_one_descr - processes an rx descriptor 977 * @card: card structure 978 * 979 * returns 1 if a packet has been sent to the stack, -ENOMEM on skb alloc 980 * failure, otherwise 0 981 * 982 * processes an rx descriptor by iommu-unmapping the data buffer and passing 983 * the packet up to the stack 984 */ 985 static int gelic_card_decode_one_descr(struct gelic_card *card) 986 { 987 enum gelic_descr_dma_status status; 988 struct gelic_descr_chain *chain = &card->rx_chain; 989 struct gelic_descr *descr = chain->head; 990 struct net_device *netdev = NULL; 991 int dmac_chain_ended = 0; 992 int prepare_rx_ret; 993 994 status = gelic_descr_get_status(descr); 995 996 if (status == GELIC_DESCR_DMA_CARDOWNED) 997 return 0; 998 999 if (status == GELIC_DESCR_DMA_NOT_IN_USE || !descr->skb) { 1000 dev_dbg(ctodev(card), "dormant descr? %p\n", descr); 1001 dmac_chain_ended = 1; 1002 goto refill; 1003 } 1004 1005 /* netdevice select */ 1006 if (card->vlan_required) { 1007 unsigned int i; 1008 u16 vid; 1009 vid = *(u16 *)(descr->skb->data) & VLAN_VID_MASK; 1010 for (i = 0; i < GELIC_PORT_MAX; i++) { 1011 if (card->vlan[i].rx == vid) { 1012 netdev = card->netdev[i]; 1013 break; 1014 } 1015 } 1016 if (GELIC_PORT_MAX <= i) { 1017 pr_info("%s: unknown packet vid=%x\n", __func__, vid); 1018 goto refill; 1019 } 1020 } else 1021 netdev = card->netdev[GELIC_PORT_ETHERNET_0]; 1022 1023 if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) || 1024 (status == GELIC_DESCR_DMA_PROTECTION_ERROR) || 1025 (status == GELIC_DESCR_DMA_FORCE_END)) { 1026 dev_info(ctodev(card), "dropping RX descriptor with state %x\n", 1027 status); 1028 netdev->stats.rx_dropped++; 1029 goto refill; 1030 } 1031 1032 if (status == GELIC_DESCR_DMA_BUFFER_FULL) { 1033 /* 1034 * Buffer full would occur if and only if 1035 * the frame length was longer than the size of this 1036 * descriptor's buffer. If the frame length was equal 1037 * to or shorter than buffer'size, FRAME_END condition 1038 * would occur. 1039 * Anyway this frame was longer than the MTU, 1040 * just drop it. 1041 */ 1042 dev_info(ctodev(card), "overlength frame\n"); 1043 goto refill; 1044 } 1045 /* 1046 * descriptors any other than FRAME_END here should 1047 * be treated as error. 1048 */ 1049 if (status != GELIC_DESCR_DMA_FRAME_END) { 1050 dev_dbg(ctodev(card), "RX descriptor with state %x\n", 1051 status); 1052 goto refill; 1053 } 1054 1055 /* ok, we've got a packet in descr */ 1056 gelic_net_pass_skb_up(descr, card, netdev); 1057 refill: 1058 1059 /* is the current descriptor terminated with next_descr == NULL? */ 1060 if (!dmac_chain_ended) 1061 dmac_chain_ended = 1062 be32_to_cpu(descr->hw_regs.dmac_cmd_status) & 1063 GELIC_DESCR_RX_DMA_CHAIN_END; 1064 /* 1065 * So that always DMAC can see the end 1066 * of the descriptor chain to avoid 1067 * from unwanted DMAC overrun. 1068 */ 1069 descr->hw_regs.next_descr_addr = 0; 1070 1071 /* change the descriptor state: */ 1072 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE); 1073 1074 /* 1075 * this call can fail, propagate the error 1076 */ 1077 prepare_rx_ret = gelic_descr_prepare_rx(card, descr, true); 1078 if (prepare_rx_ret) 1079 return prepare_rx_ret; 1080 1081 chain->tail = descr; 1082 chain->head = descr->next; 1083 1084 /* 1085 * Set this descriptor the end of the chain. 1086 */ 1087 descr->prev->hw_regs.next_descr_addr = 1088 cpu_to_be32(descr->link.cpu_addr); 1089 1090 /* 1091 * If dmac chain was met, DMAC stopped. 1092 * thus re-enable it 1093 */ 1094 1095 if (dmac_chain_ended) 1096 gelic_card_enable_rxdmac(card); 1097 1098 return 1; 1099 } 1100 1101 static void gelic_rx_oom_timer(struct timer_list *t) 1102 { 1103 struct gelic_card *card = timer_container_of(card, t, rx_oom_timer); 1104 1105 napi_schedule(&card->napi); 1106 } 1107 1108 /** 1109 * gelic_net_poll - NAPI poll function called by the stack to return packets 1110 * @napi: napi structure 1111 * @budget: number of packets we can pass to the stack at most 1112 * 1113 * returns the number of the processed packets 1114 * 1115 */ 1116 static int gelic_net_poll(struct napi_struct *napi, int budget) 1117 { 1118 struct gelic_card *card = container_of(napi, struct gelic_card, napi); 1119 int packets_done = 0; 1120 int work_result = 0; 1121 1122 while (packets_done < budget) { 1123 work_result = gelic_card_decode_one_descr(card); 1124 if (work_result != 1) 1125 break; 1126 1127 packets_done++; 1128 } 1129 1130 if (work_result == -ENOMEM) { 1131 napi_complete_done(napi, packets_done); 1132 mod_timer(&card->rx_oom_timer, jiffies + 1); 1133 return packets_done; 1134 } 1135 1136 if (packets_done < budget) { 1137 napi_complete_done(napi, packets_done); 1138 gelic_card_rx_irq_on(card); 1139 } 1140 return packets_done; 1141 } 1142 1143 /* 1144 * gelic_card_interrupt - event handler for gelic_net 1145 */ 1146 static irqreturn_t gelic_card_interrupt(int irq, void *ptr) 1147 { 1148 unsigned long flags; 1149 struct gelic_card *card = ptr; 1150 u64 status; 1151 1152 status = card->irq_status; 1153 1154 if (!status) 1155 return IRQ_NONE; 1156 1157 status &= card->irq_mask; 1158 1159 if (status & GELIC_CARD_RXINT) { 1160 gelic_card_rx_irq_off(card); 1161 napi_schedule(&card->napi); 1162 } 1163 1164 if (status & GELIC_CARD_TXINT) { 1165 spin_lock_irqsave(&card->tx_lock, flags); 1166 card->tx_dma_progress = 0; 1167 gelic_card_release_tx_chain(card, 0); 1168 /* kick outstanding tx descriptor if any */ 1169 gelic_card_kick_txdma(card, card->tx_chain.tail); 1170 spin_unlock_irqrestore(&card->tx_lock, flags); 1171 } 1172 1173 /* ether port status changed */ 1174 if (status & GELIC_CARD_PORT_STATUS_CHANGED) 1175 gelic_card_get_ether_port_status(card, 1); 1176 1177 #ifdef CONFIG_GELIC_WIRELESS 1178 if (status & (GELIC_CARD_WLAN_EVENT_RECEIVED | 1179 GELIC_CARD_WLAN_COMMAND_COMPLETED)) 1180 gelic_wl_interrupt(card->netdev[GELIC_PORT_WIRELESS], status); 1181 #endif 1182 1183 return IRQ_HANDLED; 1184 } 1185 1186 #ifdef CONFIG_NET_POLL_CONTROLLER 1187 /** 1188 * gelic_net_poll_controller - artificial interrupt for netconsole etc. 1189 * @netdev: interface device structure 1190 * 1191 * see Documentation/networking/netconsole.rst 1192 */ 1193 void gelic_net_poll_controller(struct net_device *netdev) 1194 { 1195 struct gelic_card *card = netdev_card(netdev); 1196 1197 gelic_card_set_irq_mask(card, 0); 1198 gelic_card_interrupt(netdev->irq, netdev); 1199 gelic_card_set_irq_mask(card, card->irq_mask); 1200 } 1201 #endif /* CONFIG_NET_POLL_CONTROLLER */ 1202 1203 /** 1204 * gelic_net_open - called upon ifconfig up 1205 * @netdev: interface device structure 1206 * 1207 * returns 0 on success, <0 on failure 1208 * 1209 * gelic_net_open allocates all the descriptors and memory needed for 1210 * operation, sets up multicast list and enables interrupts 1211 */ 1212 int gelic_net_open(struct net_device *netdev) 1213 { 1214 struct gelic_card *card = netdev_card(netdev); 1215 1216 dev_dbg(ctodev(card), " -> %s %p\n", __func__, netdev); 1217 1218 gelic_card_up(card); 1219 1220 netif_start_queue(netdev); 1221 gelic_card_get_ether_port_status(card, 1); 1222 1223 dev_dbg(ctodev(card), " <- %s\n", __func__); 1224 return 0; 1225 } 1226 1227 void gelic_net_get_drvinfo(struct net_device *netdev, 1228 struct ethtool_drvinfo *info) 1229 { 1230 strscpy(info->driver, DRV_NAME, sizeof(info->driver)); 1231 strscpy(info->version, DRV_VERSION, sizeof(info->version)); 1232 } 1233 1234 static int gelic_ether_get_link_ksettings(struct net_device *netdev, 1235 struct ethtool_link_ksettings *cmd) 1236 { 1237 struct gelic_card *card = netdev_card(netdev); 1238 u32 supported, advertising; 1239 1240 gelic_card_get_ether_port_status(card, 0); 1241 1242 if (card->ether_port_status & GELIC_LV1_ETHER_FULL_DUPLEX) 1243 cmd->base.duplex = DUPLEX_FULL; 1244 else 1245 cmd->base.duplex = DUPLEX_HALF; 1246 1247 switch (card->ether_port_status & GELIC_LV1_ETHER_SPEED_MASK) { 1248 case GELIC_LV1_ETHER_SPEED_10: 1249 cmd->base.speed = SPEED_10; 1250 break; 1251 case GELIC_LV1_ETHER_SPEED_100: 1252 cmd->base.speed = SPEED_100; 1253 break; 1254 case GELIC_LV1_ETHER_SPEED_1000: 1255 cmd->base.speed = SPEED_1000; 1256 break; 1257 default: 1258 pr_info("%s: speed unknown\n", __func__); 1259 cmd->base.speed = SPEED_10; 1260 break; 1261 } 1262 1263 supported = SUPPORTED_TP | SUPPORTED_Autoneg | 1264 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full | 1265 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full | 1266 SUPPORTED_1000baseT_Full; 1267 advertising = supported; 1268 if (card->link_mode & GELIC_LV1_ETHER_AUTO_NEG) { 1269 cmd->base.autoneg = AUTONEG_ENABLE; 1270 } else { 1271 cmd->base.autoneg = AUTONEG_DISABLE; 1272 advertising &= ~ADVERTISED_Autoneg; 1273 } 1274 cmd->base.port = PORT_TP; 1275 1276 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported, 1277 supported); 1278 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising, 1279 advertising); 1280 1281 return 0; 1282 } 1283 1284 static int 1285 gelic_ether_set_link_ksettings(struct net_device *netdev, 1286 const struct ethtool_link_ksettings *cmd) 1287 { 1288 struct gelic_card *card = netdev_card(netdev); 1289 u64 mode; 1290 int ret; 1291 1292 if (cmd->base.autoneg == AUTONEG_ENABLE) { 1293 mode = GELIC_LV1_ETHER_AUTO_NEG; 1294 } else { 1295 switch (cmd->base.speed) { 1296 case SPEED_10: 1297 mode = GELIC_LV1_ETHER_SPEED_10; 1298 break; 1299 case SPEED_100: 1300 mode = GELIC_LV1_ETHER_SPEED_100; 1301 break; 1302 case SPEED_1000: 1303 mode = GELIC_LV1_ETHER_SPEED_1000; 1304 break; 1305 default: 1306 return -EINVAL; 1307 } 1308 if (cmd->base.duplex == DUPLEX_FULL) { 1309 mode |= GELIC_LV1_ETHER_FULL_DUPLEX; 1310 } else if (cmd->base.speed == SPEED_1000) { 1311 pr_info("1000 half duplex is not supported.\n"); 1312 return -EINVAL; 1313 } 1314 } 1315 1316 ret = gelic_card_set_link_mode(card, mode); 1317 1318 if (ret) 1319 return ret; 1320 1321 return 0; 1322 } 1323 1324 static void gelic_net_get_wol(struct net_device *netdev, 1325 struct ethtool_wolinfo *wol) 1326 { 1327 if (0 <= ps3_compare_firmware_version(2, 2, 0)) 1328 wol->supported = WAKE_MAGIC; 1329 else 1330 wol->supported = 0; 1331 1332 wol->wolopts = ps3_sys_manager_get_wol() ? wol->supported : 0; 1333 memset(&wol->sopass, 0, sizeof(wol->sopass)); 1334 } 1335 static int gelic_net_set_wol(struct net_device *netdev, 1336 struct ethtool_wolinfo *wol) 1337 { 1338 int status; 1339 struct gelic_card *card; 1340 u64 v1, v2; 1341 1342 if (ps3_compare_firmware_version(2, 2, 0) < 0 || 1343 !capable(CAP_NET_ADMIN)) 1344 return -EPERM; 1345 1346 if (wol->wolopts & ~WAKE_MAGIC) 1347 return -EINVAL; 1348 1349 card = netdev_card(netdev); 1350 if (wol->wolopts & WAKE_MAGIC) { 1351 status = lv1_net_control(bus_id(card), dev_id(card), 1352 GELIC_LV1_SET_WOL, 1353 GELIC_LV1_WOL_MAGIC_PACKET, 1354 0, GELIC_LV1_WOL_MP_ENABLE, 1355 &v1, &v2); 1356 if (status) { 1357 pr_info("%s: enabling WOL failed %d\n", __func__, 1358 status); 1359 status = -EIO; 1360 goto done; 1361 } 1362 status = lv1_net_control(bus_id(card), dev_id(card), 1363 GELIC_LV1_SET_WOL, 1364 GELIC_LV1_WOL_ADD_MATCH_ADDR, 1365 0, GELIC_LV1_WOL_MATCH_ALL, 1366 &v1, &v2); 1367 if (!status) 1368 ps3_sys_manager_set_wol(1); 1369 else { 1370 pr_info("%s: enabling WOL filter failed %d\n", 1371 __func__, status); 1372 status = -EIO; 1373 } 1374 } else { 1375 status = lv1_net_control(bus_id(card), dev_id(card), 1376 GELIC_LV1_SET_WOL, 1377 GELIC_LV1_WOL_MAGIC_PACKET, 1378 0, GELIC_LV1_WOL_MP_DISABLE, 1379 &v1, &v2); 1380 if (status) { 1381 pr_info("%s: disabling WOL failed %d\n", __func__, 1382 status); 1383 status = -EIO; 1384 goto done; 1385 } 1386 status = lv1_net_control(bus_id(card), dev_id(card), 1387 GELIC_LV1_SET_WOL, 1388 GELIC_LV1_WOL_DELETE_MATCH_ADDR, 1389 0, GELIC_LV1_WOL_MATCH_ALL, 1390 &v1, &v2); 1391 if (!status) 1392 ps3_sys_manager_set_wol(0); 1393 else { 1394 pr_info("%s: removing WOL filter failed %d\n", 1395 __func__, status); 1396 status = -EIO; 1397 } 1398 } 1399 done: 1400 return status; 1401 } 1402 1403 static const struct ethtool_ops gelic_ether_ethtool_ops = { 1404 .get_drvinfo = gelic_net_get_drvinfo, 1405 .get_link = ethtool_op_get_link, 1406 .get_wol = gelic_net_get_wol, 1407 .set_wol = gelic_net_set_wol, 1408 .get_link_ksettings = gelic_ether_get_link_ksettings, 1409 .set_link_ksettings = gelic_ether_set_link_ksettings, 1410 }; 1411 1412 /** 1413 * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout 1414 * function (to be called not under interrupt status) 1415 * @work: work is context of tx timout task 1416 * 1417 * called as task when tx hangs, resets interface (if interface is up) 1418 */ 1419 static void gelic_net_tx_timeout_task(struct work_struct *work) 1420 { 1421 struct gelic_card *card = 1422 container_of(work, struct gelic_card, tx_timeout_task); 1423 struct net_device *netdev = card->netdev[GELIC_PORT_ETHERNET_0]; 1424 1425 dev_info(ctodev(card), "%s:Timed out. Restarting...\n", __func__); 1426 1427 if (!(netdev->flags & IFF_UP)) 1428 goto out; 1429 1430 netif_device_detach(netdev); 1431 gelic_net_stop(netdev); 1432 1433 gelic_net_open(netdev); 1434 netif_device_attach(netdev); 1435 1436 out: 1437 atomic_dec(&card->tx_timeout_task_counter); 1438 } 1439 1440 /** 1441 * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in. 1442 * @netdev: interface device structure 1443 * @txqueue: unused 1444 * 1445 * called, if tx hangs. Schedules a task that resets the interface 1446 */ 1447 void gelic_net_tx_timeout(struct net_device *netdev, unsigned int txqueue) 1448 { 1449 struct gelic_card *card; 1450 1451 card = netdev_card(netdev); 1452 atomic_inc(&card->tx_timeout_task_counter); 1453 if (netdev->flags & IFF_UP) 1454 schedule_work(&card->tx_timeout_task); 1455 else 1456 atomic_dec(&card->tx_timeout_task_counter); 1457 } 1458 1459 static const struct net_device_ops gelic_netdevice_ops = { 1460 .ndo_open = gelic_net_open, 1461 .ndo_stop = gelic_net_stop, 1462 .ndo_start_xmit = gelic_net_xmit, 1463 .ndo_set_rx_mode = gelic_net_set_multi, 1464 .ndo_tx_timeout = gelic_net_tx_timeout, 1465 .ndo_set_mac_address = eth_mac_addr, 1466 .ndo_validate_addr = eth_validate_addr, 1467 #ifdef CONFIG_NET_POLL_CONTROLLER 1468 .ndo_poll_controller = gelic_net_poll_controller, 1469 #endif 1470 }; 1471 1472 /** 1473 * gelic_ether_setup_netdev_ops - initialization of net_device operations 1474 * @netdev: net_device structure 1475 * @napi: napi structure 1476 * 1477 * fills out function pointers in the net_device structure 1478 */ 1479 static void gelic_ether_setup_netdev_ops(struct net_device *netdev, 1480 struct napi_struct *napi) 1481 { 1482 netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT; 1483 /* NAPI */ 1484 netif_napi_add(netdev, napi, gelic_net_poll); 1485 netdev->ethtool_ops = &gelic_ether_ethtool_ops; 1486 netdev->netdev_ops = &gelic_netdevice_ops; 1487 } 1488 1489 /** 1490 * gelic_net_setup_netdev - initialization of net_device 1491 * @netdev: net_device structure 1492 * @card: card structure 1493 * 1494 * Returns 0 on success or <0 on failure 1495 * 1496 * gelic_ether_setup_netdev initializes the net_device structure 1497 * and register it. 1498 **/ 1499 int gelic_net_setup_netdev(struct net_device *netdev, struct gelic_card *card) 1500 { 1501 int status; 1502 u64 v1, v2; 1503 1504 netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM; 1505 1506 netdev->features = NETIF_F_IP_CSUM; 1507 if (GELIC_CARD_RX_CSUM_DEFAULT) 1508 netdev->features |= NETIF_F_RXCSUM; 1509 1510 status = lv1_net_control(bus_id(card), dev_id(card), 1511 GELIC_LV1_GET_MAC_ADDRESS, 1512 0, 0, 0, &v1, &v2); 1513 v1 <<= 16; 1514 if (status || !is_valid_ether_addr((u8 *)&v1)) { 1515 dev_info(ctodev(card), 1516 "%s:lv1_net_control GET_MAC_ADDR failed %d\n", 1517 __func__, status); 1518 return -EINVAL; 1519 } 1520 eth_hw_addr_set(netdev, (u8 *)&v1); 1521 1522 if (card->vlan_required) { 1523 netdev->hard_header_len += VLAN_HLEN; 1524 /* 1525 * As vlan is internally used, 1526 * we can not receive vlan packets 1527 */ 1528 netdev->features |= NETIF_F_VLAN_CHALLENGED; 1529 } 1530 1531 /* MTU range: 64 - 1518 */ 1532 netdev->min_mtu = GELIC_NET_MIN_MTU; 1533 netdev->max_mtu = GELIC_NET_MAX_MTU; 1534 1535 status = register_netdev(netdev); 1536 if (status) { 1537 dev_err(ctodev(card), "%s:Couldn't register %s %d\n", 1538 __func__, netdev->name, status); 1539 return status; 1540 } 1541 dev_info(ctodev(card), "%s: MAC addr %pM\n", 1542 netdev->name, netdev->dev_addr); 1543 1544 return 0; 1545 } 1546 1547 #define GELIC_ALIGN (32) 1548 1549 /** 1550 * gelic_alloc_card_net - allocates net_device and card structure 1551 * @netdev: interface device structure 1552 * 1553 * returns the card structure or NULL in case of errors 1554 * 1555 * the card and net_device structures are linked to each other 1556 */ 1557 static struct gelic_card *gelic_alloc_card_net(struct net_device **netdev) 1558 { 1559 struct gelic_card *card; 1560 struct gelic_port *port; 1561 void *p; 1562 size_t alloc_size; 1563 /* 1564 * gelic requires dma descriptor is 32 bytes aligned and 1565 * the hypervisor requires irq_status is 8 bytes aligned. 1566 */ 1567 BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8); 1568 BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32); 1569 alloc_size = 1570 sizeof(struct gelic_card) + 1571 sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS + 1572 sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS + 1573 GELIC_ALIGN - 1; 1574 1575 p = kzalloc(alloc_size, GFP_KERNEL); 1576 if (!p) 1577 return NULL; 1578 card = PTR_ALIGN(p, GELIC_ALIGN); 1579 card->unalign = p; 1580 1581 /* 1582 * alloc netdev 1583 */ 1584 *netdev = alloc_etherdev(sizeof(struct gelic_port)); 1585 if (!*netdev) { 1586 kfree(card->unalign); 1587 return NULL; 1588 } 1589 port = netdev_priv(*netdev); 1590 1591 /* gelic_port */ 1592 port->netdev = *netdev; 1593 port->card = card; 1594 port->type = GELIC_PORT_ETHERNET_0; 1595 1596 /* gelic_card */ 1597 card->netdev[GELIC_PORT_ETHERNET_0] = *netdev; 1598 1599 INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task); 1600 init_waitqueue_head(&card->waitq); 1601 atomic_set(&card->tx_timeout_task_counter, 0); 1602 mutex_init(&card->updown_lock); 1603 atomic_set(&card->users, 0); 1604 1605 timer_setup(&card->rx_oom_timer, gelic_rx_oom_timer, 0); 1606 1607 return card; 1608 } 1609 1610 static void gelic_card_get_vlan_info(struct gelic_card *card) 1611 { 1612 u64 v1, v2; 1613 int status; 1614 unsigned int i; 1615 struct { 1616 int tx; 1617 int rx; 1618 } vlan_id_ix[2] = { 1619 [GELIC_PORT_ETHERNET_0] = { 1620 .tx = GELIC_LV1_VLAN_TX_ETHERNET_0, 1621 .rx = GELIC_LV1_VLAN_RX_ETHERNET_0 1622 }, 1623 [GELIC_PORT_WIRELESS] = { 1624 .tx = GELIC_LV1_VLAN_TX_WIRELESS, 1625 .rx = GELIC_LV1_VLAN_RX_WIRELESS 1626 } 1627 }; 1628 1629 for (i = 0; i < ARRAY_SIZE(vlan_id_ix); i++) { 1630 /* tx tag */ 1631 status = lv1_net_control(bus_id(card), dev_id(card), 1632 GELIC_LV1_GET_VLAN_ID, 1633 vlan_id_ix[i].tx, 1634 0, 0, &v1, &v2); 1635 if (status || !v1) { 1636 if (status != LV1_NO_ENTRY) 1637 dev_dbg(ctodev(card), 1638 "get vlan id for tx(%d) failed(%d)\n", 1639 vlan_id_ix[i].tx, status); 1640 card->vlan[i].tx = 0; 1641 card->vlan[i].rx = 0; 1642 continue; 1643 } 1644 card->vlan[i].tx = (u16)v1; 1645 1646 /* rx tag */ 1647 status = lv1_net_control(bus_id(card), dev_id(card), 1648 GELIC_LV1_GET_VLAN_ID, 1649 vlan_id_ix[i].rx, 1650 0, 0, &v1, &v2); 1651 if (status || !v1) { 1652 if (status != LV1_NO_ENTRY) 1653 dev_info(ctodev(card), 1654 "get vlan id for rx(%d) failed(%d)\n", 1655 vlan_id_ix[i].rx, status); 1656 card->vlan[i].tx = 0; 1657 card->vlan[i].rx = 0; 1658 continue; 1659 } 1660 card->vlan[i].rx = (u16)v1; 1661 1662 dev_dbg(ctodev(card), "vlan_id[%d] tx=%02x rx=%02x\n", 1663 i, card->vlan[i].tx, card->vlan[i].rx); 1664 } 1665 1666 if (card->vlan[GELIC_PORT_ETHERNET_0].tx) { 1667 BUG_ON(!card->vlan[GELIC_PORT_WIRELESS].tx); 1668 card->vlan_required = 1; 1669 } else 1670 card->vlan_required = 0; 1671 1672 /* check wirelss capable firmware */ 1673 if (ps3_compare_firmware_version(1, 6, 0) < 0) { 1674 card->vlan[GELIC_PORT_WIRELESS].tx = 0; 1675 card->vlan[GELIC_PORT_WIRELESS].rx = 0; 1676 } 1677 1678 dev_info(ctodev(card), "internal vlan %s\n", 1679 card->vlan_required? "enabled" : "disabled"); 1680 } 1681 /* 1682 * ps3_gelic_driver_probe - add a device to the control of this driver 1683 */ 1684 static int ps3_gelic_driver_probe(struct ps3_system_bus_device *dev) 1685 { 1686 struct gelic_card *card; 1687 struct net_device *netdev; 1688 int result; 1689 1690 pr_debug("%s: called\n", __func__); 1691 1692 udbg_shutdown_ps3gelic(); 1693 1694 result = ps3_open_hv_device(dev); 1695 1696 if (result) { 1697 dev_dbg(&dev->core, "%s:ps3_open_hv_device failed\n", 1698 __func__); 1699 goto fail_open; 1700 } 1701 1702 result = ps3_dma_region_create(dev->d_region); 1703 1704 if (result) { 1705 dev_dbg(&dev->core, "%s:ps3_dma_region_create failed(%d)\n", 1706 __func__, result); 1707 BUG_ON("check region type"); 1708 goto fail_dma_region; 1709 } 1710 1711 /* alloc card/netdevice */ 1712 card = gelic_alloc_card_net(&netdev); 1713 if (!card) { 1714 dev_info(&dev->core, "%s:gelic_net_alloc_card failed\n", 1715 __func__); 1716 result = -ENOMEM; 1717 goto fail_alloc_card; 1718 } 1719 ps3_system_bus_set_drvdata(dev, card); 1720 card->dev = dev; 1721 1722 /* get internal vlan info */ 1723 gelic_card_get_vlan_info(card); 1724 1725 card->link_mode = GELIC_LV1_ETHER_AUTO_NEG; 1726 1727 /* setup interrupt */ 1728 result = lv1_net_set_interrupt_status_indicator(bus_id(card), 1729 dev_id(card), 1730 ps3_mm_phys_to_lpar(__pa(&card->irq_status)), 1731 0); 1732 1733 if (result) { 1734 dev_dbg(&dev->core, 1735 "%s:set_interrupt_status_indicator failed: %s\n", 1736 __func__, ps3_result(result)); 1737 result = -EIO; 1738 goto fail_status_indicator; 1739 } 1740 1741 result = ps3_sb_event_receive_port_setup(dev, PS3_BINDING_CPU_ANY, 1742 &card->irq); 1743 1744 if (result) { 1745 dev_info(ctodev(card), 1746 "%s:gelic_net_open_device failed (%d)\n", 1747 __func__, result); 1748 result = -EPERM; 1749 goto fail_alloc_irq; 1750 } 1751 result = request_irq(card->irq, gelic_card_interrupt, 1752 0, netdev->name, card); 1753 1754 if (result) { 1755 dev_info(ctodev(card), "%s:request_irq failed (%d)\n", 1756 __func__, result); 1757 goto fail_request_irq; 1758 } 1759 1760 /* setup card structure */ 1761 card->irq_mask = GELIC_CARD_RXINT | GELIC_CARD_TXINT | 1762 GELIC_CARD_PORT_STATUS_CHANGED; 1763 1764 1765 result = gelic_card_init_chain(card, &card->tx_chain, 1766 card->descr, GELIC_NET_TX_DESCRIPTORS); 1767 if (result) 1768 goto fail_alloc_tx; 1769 result = gelic_card_init_chain(card, &card->rx_chain, 1770 card->descr + GELIC_NET_TX_DESCRIPTORS, 1771 GELIC_NET_RX_DESCRIPTORS); 1772 if (result) 1773 goto fail_alloc_rx; 1774 1775 /* head of chain */ 1776 card->tx_top = card->tx_chain.head; 1777 card->rx_top = card->rx_chain.head; 1778 dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n", 1779 card->rx_top, card->tx_top, sizeof(struct gelic_descr), 1780 GELIC_NET_RX_DESCRIPTORS); 1781 /* allocate rx skbs */ 1782 result = gelic_card_alloc_rx_skbs(card); 1783 if (result) 1784 goto fail_alloc_skbs; 1785 1786 spin_lock_init(&card->tx_lock); 1787 card->tx_dma_progress = 0; 1788 1789 /* setup net_device structure */ 1790 netdev->irq = card->irq; 1791 SET_NETDEV_DEV(netdev, &card->dev->core); 1792 gelic_ether_setup_netdev_ops(netdev, &card->napi); 1793 result = gelic_net_setup_netdev(netdev, card); 1794 if (result) { 1795 dev_dbg(&dev->core, "%s: setup_netdev failed %d\n", 1796 __func__, result); 1797 goto fail_setup_netdev; 1798 } 1799 1800 #ifdef CONFIG_GELIC_WIRELESS 1801 result = gelic_wl_driver_probe(card); 1802 if (result) { 1803 dev_dbg(&dev->core, "%s: WL init failed\n", __func__); 1804 goto fail_setup_netdev; 1805 } 1806 #endif 1807 pr_debug("%s: done\n", __func__); 1808 return 0; 1809 1810 fail_setup_netdev: 1811 fail_alloc_skbs: 1812 gelic_card_free_chain(card, card->rx_chain.head); 1813 fail_alloc_rx: 1814 gelic_card_free_chain(card, card->tx_chain.head); 1815 fail_alloc_tx: 1816 free_irq(card->irq, card); 1817 netdev->irq = 0; 1818 fail_request_irq: 1819 ps3_sb_event_receive_port_destroy(dev, card->irq); 1820 fail_alloc_irq: 1821 lv1_net_set_interrupt_status_indicator(bus_id(card), 1822 bus_id(card), 1823 0, 0); 1824 fail_status_indicator: 1825 ps3_system_bus_set_drvdata(dev, NULL); 1826 kfree(netdev_card(netdev)->unalign); 1827 free_netdev(netdev); 1828 fail_alloc_card: 1829 ps3_dma_region_free(dev->d_region); 1830 fail_dma_region: 1831 ps3_close_hv_device(dev); 1832 fail_open: 1833 return result; 1834 } 1835 1836 /* 1837 * ps3_gelic_driver_remove - remove a device from the control of this driver 1838 */ 1839 1840 static void ps3_gelic_driver_remove(struct ps3_system_bus_device *dev) 1841 { 1842 struct gelic_card *card = ps3_system_bus_get_drvdata(dev); 1843 struct net_device *netdev0; 1844 pr_debug("%s: called\n", __func__); 1845 1846 /* set auto-negotiation */ 1847 gelic_card_set_link_mode(card, GELIC_LV1_ETHER_AUTO_NEG); 1848 1849 #ifdef CONFIG_GELIC_WIRELESS 1850 gelic_wl_driver_remove(card); 1851 #endif 1852 /* stop interrupt */ 1853 gelic_card_set_irq_mask(card, 0); 1854 1855 /* turn off DMA, force end */ 1856 gelic_card_disable_rxdmac(card); 1857 gelic_card_disable_txdmac(card); 1858 1859 /* release chains */ 1860 gelic_card_release_tx_chain(card, 1); 1861 gelic_card_release_rx_chain(card); 1862 1863 gelic_card_free_chain(card, card->tx_top); 1864 gelic_card_free_chain(card, card->rx_top); 1865 1866 netdev0 = card->netdev[GELIC_PORT_ETHERNET_0]; 1867 /* disconnect event port */ 1868 free_irq(card->irq, card); 1869 netdev0->irq = 0; 1870 ps3_sb_event_receive_port_destroy(card->dev, card->irq); 1871 1872 wait_event(card->waitq, 1873 atomic_read(&card->tx_timeout_task_counter) == 0); 1874 1875 lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card), 1876 0 , 0); 1877 1878 unregister_netdev(netdev0); 1879 kfree(netdev_card(netdev0)->unalign); 1880 free_netdev(netdev0); 1881 1882 ps3_system_bus_set_drvdata(dev, NULL); 1883 1884 ps3_dma_region_free(dev->d_region); 1885 1886 ps3_close_hv_device(dev); 1887 1888 pr_debug("%s: done\n", __func__); 1889 } 1890 1891 static struct ps3_system_bus_driver ps3_gelic_driver = { 1892 .match_id = PS3_MATCH_ID_GELIC, 1893 .probe = ps3_gelic_driver_probe, 1894 .remove = ps3_gelic_driver_remove, 1895 .shutdown = ps3_gelic_driver_remove, 1896 .core.name = "ps3_gelic_driver", 1897 .core.owner = THIS_MODULE, 1898 }; 1899 1900 static int __init ps3_gelic_driver_init (void) 1901 { 1902 return firmware_has_feature(FW_FEATURE_PS3_LV1) 1903 ? ps3_system_bus_driver_register(&ps3_gelic_driver) 1904 : -ENODEV; 1905 } 1906 1907 static void __exit ps3_gelic_driver_exit (void) 1908 { 1909 ps3_system_bus_driver_unregister(&ps3_gelic_driver); 1910 } 1911 1912 module_init(ps3_gelic_driver_init); 1913 module_exit(ps3_gelic_driver_exit); 1914 1915 MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC); 1916 1917