| 1 | /* |
| 2 | * PS3 gelic network driver. |
| 3 | * |
| 4 | * Copyright (C) 2007 Sony Computer Entertainment Inc. |
| 5 | * Copyright 2006, 2007 Sony Corporation |
| 6 | * |
| 7 | * This file is based on: spider_net.c |
| 8 | * |
| 9 | * (C) Copyright IBM Corp. 2005 |
| 10 | * |
| 11 | * Authors : Utz Bacher <utz.bacher@de.ibm.com> |
| 12 | * Jens Osterkamp <Jens.Osterkamp@de.ibm.com> |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2, or (at your option) |
| 17 | * any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program; if not, write to the Free Software |
| 26 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 27 | */ |
| 28 | |
| 29 | #undef DEBUG |
| 30 | |
| 31 | #include <linux/kernel.h> |
| 32 | #include <linux/module.h> |
| 33 | |
| 34 | #include <linux/etherdevice.h> |
| 35 | #include <linux/ethtool.h> |
| 36 | #include <linux/if_vlan.h> |
| 37 | |
| 38 | #include <linux/in.h> |
| 39 | #include <linux/ip.h> |
| 40 | #include <linux/tcp.h> |
| 41 | |
| 42 | #include <linux/dma-mapping.h> |
| 43 | #include <net/checksum.h> |
| 44 | #include <asm/firmware.h> |
| 45 | #include <asm/ps3.h> |
| 46 | #include <asm/lv1call.h> |
| 47 | |
| 48 | #include "ps3_gelic_net.h" |
| 49 | |
| 50 | #define DRV_NAME "Gelic Network Driver" |
| 51 | #define DRV_VERSION "1.0" |
| 52 | |
| 53 | MODULE_AUTHOR("SCE Inc."); |
| 54 | MODULE_DESCRIPTION("Gelic Network driver"); |
| 55 | MODULE_LICENSE("GPL"); |
| 56 | |
| 57 | static inline struct device *ctodev(struct gelic_card *card) |
| 58 | { |
| 59 | return &card->dev->core; |
| 60 | } |
| 61 | static inline u64 bus_id(struct gelic_card *card) |
| 62 | { |
| 63 | return card->dev->bus_id; |
| 64 | } |
| 65 | static inline u64 dev_id(struct gelic_card *card) |
| 66 | { |
| 67 | return card->dev->dev_id; |
| 68 | } |
| 69 | |
| 70 | /* set irq_mask */ |
| 71 | static int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask) |
| 72 | { |
| 73 | int status; |
| 74 | |
| 75 | status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card), |
| 76 | mask, 0); |
| 77 | if (status) |
| 78 | dev_info(ctodev(card), |
| 79 | "lv1_net_set_interrupt_mask failed %d\n", status); |
| 80 | return status; |
| 81 | } |
| 82 | static inline void gelic_card_rx_irq_on(struct gelic_card *card) |
| 83 | { |
| 84 | gelic_card_set_irq_mask(card, card->ghiintmask | GELIC_CARD_RXINT); |
| 85 | } |
| 86 | static inline void gelic_card_rx_irq_off(struct gelic_card *card) |
| 87 | { |
| 88 | gelic_card_set_irq_mask(card, card->ghiintmask & ~GELIC_CARD_RXINT); |
| 89 | } |
| 90 | |
| 91 | static void |
| 92 | gelic_card_get_ether_port_status(struct gelic_card *card, int inform) |
| 93 | { |
| 94 | u64 v2; |
| 95 | struct net_device *ether_netdev; |
| 96 | |
| 97 | lv1_net_control(bus_id(card), dev_id(card), |
| 98 | GELIC_LV1_GET_ETH_PORT_STATUS, |
| 99 | GELIC_LV1_VLAN_TX_ETHERNET, 0, 0, |
| 100 | &card->ether_port_status, &v2); |
| 101 | |
| 102 | if (inform) { |
| 103 | ether_netdev = card->netdev; |
| 104 | if (card->ether_port_status & GELIC_LV1_ETHER_LINK_UP) |
| 105 | netif_carrier_on(ether_netdev); |
| 106 | else |
| 107 | netif_carrier_off(ether_netdev); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * gelic_descr_get_status -- returns the status of a descriptor |
| 114 | * @descr: descriptor to look at |
| 115 | * |
| 116 | * returns the status as in the dmac_cmd_status field of the descriptor |
| 117 | */ |
| 118 | static enum gelic_descr_dma_status |
| 119 | gelic_descr_get_status(struct gelic_descr *descr) |
| 120 | { |
| 121 | return be32_to_cpu(descr->dmac_cmd_status) & GELIC_DESCR_DMA_STAT_MASK; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * gelic_descr_set_status -- sets the status of a descriptor |
| 126 | * @descr: descriptor to change |
| 127 | * @status: status to set in the descriptor |
| 128 | * |
| 129 | * changes the status to the specified value. Doesn't change other bits |
| 130 | * in the status |
| 131 | */ |
| 132 | static void gelic_descr_set_status(struct gelic_descr *descr, |
| 133 | enum gelic_descr_dma_status status) |
| 134 | { |
| 135 | descr->dmac_cmd_status = cpu_to_be32(status | |
| 136 | (be32_to_cpu(descr->dmac_cmd_status) & |
| 137 | ~GELIC_DESCR_DMA_STAT_MASK)); |
| 138 | /* |
| 139 | * dma_cmd_status field is used to indicate whether the descriptor |
| 140 | * is valid or not. |
| 141 | * Usually caller of this function wants to inform that to the |
| 142 | * hardware, so we assure here the hardware sees the change. |
| 143 | */ |
| 144 | wmb(); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * gelic_card_free_chain - free descriptor chain |
| 149 | * @card: card structure |
| 150 | * @descr_in: address of desc |
| 151 | */ |
| 152 | static void gelic_card_free_chain(struct gelic_card *card, |
| 153 | struct gelic_descr *descr_in) |
| 154 | { |
| 155 | struct gelic_descr *descr; |
| 156 | |
| 157 | for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) { |
| 158 | dma_unmap_single(ctodev(card), descr->bus_addr, |
| 159 | GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL); |
| 160 | descr->bus_addr = 0; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * gelic_card_init_chain - links descriptor chain |
| 166 | * @card: card structure |
| 167 | * @chain: address of chain |
| 168 | * @start_descr: address of descriptor array |
| 169 | * @no: number of descriptors |
| 170 | * |
| 171 | * we manage a circular list that mirrors the hardware structure, |
| 172 | * except that the hardware uses bus addresses. |
| 173 | * |
| 174 | * returns 0 on success, <0 on failure |
| 175 | */ |
| 176 | static int gelic_card_init_chain(struct gelic_card *card, |
| 177 | struct gelic_descr_chain *chain, |
| 178 | struct gelic_descr *start_descr, int no) |
| 179 | { |
| 180 | int i; |
| 181 | struct gelic_descr *descr; |
| 182 | |
| 183 | descr = start_descr; |
| 184 | memset(descr, 0, sizeof(*descr) * no); |
| 185 | |
| 186 | /* set up the hardware pointers in each descriptor */ |
| 187 | for (i = 0; i < no; i++, descr++) { |
| 188 | gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE); |
| 189 | descr->bus_addr = |
| 190 | dma_map_single(ctodev(card), descr, |
| 191 | GELIC_DESCR_SIZE, |
| 192 | DMA_BIDIRECTIONAL); |
| 193 | |
| 194 | if (!descr->bus_addr) |
| 195 | goto iommu_error; |
| 196 | |
| 197 | descr->next = descr + 1; |
| 198 | descr->prev = descr - 1; |
| 199 | } |
| 200 | /* make them as ring */ |
| 201 | (descr - 1)->next = start_descr; |
| 202 | start_descr->prev = (descr - 1); |
| 203 | |
| 204 | /* chain bus addr of hw descriptor */ |
| 205 | descr = start_descr; |
| 206 | for (i = 0; i < no; i++, descr++) { |
| 207 | descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr); |
| 208 | } |
| 209 | |
| 210 | chain->head = start_descr; |
| 211 | chain->tail = start_descr; |
| 212 | |
| 213 | /* do not chain last hw descriptor */ |
| 214 | (descr - 1)->next_descr_addr = 0; |
| 215 | |
| 216 | return 0; |
| 217 | |
| 218 | iommu_error: |
| 219 | for (i--, descr--; 0 <= i; i--, descr--) |
| 220 | if (descr->bus_addr) |
| 221 | dma_unmap_single(ctodev(card), descr->bus_addr, |
| 222 | GELIC_DESCR_SIZE, |
| 223 | DMA_BIDIRECTIONAL); |
| 224 | return -ENOMEM; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * gelic_descr_prepare_rx - reinitializes a rx descriptor |
| 229 | * @card: card structure |
| 230 | * @descr: descriptor to re-init |
| 231 | * |
| 232 | * return 0 on succes, <0 on failure |
| 233 | * |
| 234 | * allocates a new rx skb, iommu-maps it and attaches it to the descriptor. |
| 235 | * Activate the descriptor state-wise |
| 236 | */ |
| 237 | static int gelic_descr_prepare_rx(struct gelic_card *card, |
| 238 | struct gelic_descr *descr) |
| 239 | { |
| 240 | int offset; |
| 241 | unsigned int bufsize; |
| 242 | |
| 243 | if (gelic_descr_get_status(descr) != GELIC_DESCR_DMA_NOT_IN_USE) |
| 244 | dev_info(ctodev(card), "%s: ERROR status \n", __func__); |
| 245 | |
| 246 | /* we need to round up the buffer size to a multiple of 128 */ |
| 247 | bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN); |
| 248 | |
| 249 | /* and we need to have it 128 byte aligned, therefore we allocate a |
| 250 | * bit more */ |
| 251 | descr->skb = netdev_alloc_skb(card->netdev, |
| 252 | bufsize + GELIC_NET_RXBUF_ALIGN - 1); |
| 253 | if (!descr->skb) { |
| 254 | descr->buf_addr = 0; /* tell DMAC don't touch memory */ |
| 255 | dev_info(ctodev(card), |
| 256 | "%s:allocate skb failed !!\n", __func__); |
| 257 | return -ENOMEM; |
| 258 | } |
| 259 | descr->buf_size = cpu_to_be32(bufsize); |
| 260 | descr->dmac_cmd_status = 0; |
| 261 | descr->result_size = 0; |
| 262 | descr->valid_size = 0; |
| 263 | descr->data_error = 0; |
| 264 | |
| 265 | offset = ((unsigned long)descr->skb->data) & |
| 266 | (GELIC_NET_RXBUF_ALIGN - 1); |
| 267 | if (offset) |
| 268 | skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset); |
| 269 | /* io-mmu-map the skb */ |
| 270 | descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card), |
| 271 | descr->skb->data, |
| 272 | GELIC_NET_MAX_MTU, |
| 273 | DMA_FROM_DEVICE)); |
| 274 | if (!descr->buf_addr) { |
| 275 | dev_kfree_skb_any(descr->skb); |
| 276 | descr->skb = NULL; |
| 277 | dev_info(ctodev(card), |
| 278 | "%s:Could not iommu-map rx buffer\n", __func__); |
| 279 | gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE); |
| 280 | return -ENOMEM; |
| 281 | } else { |
| 282 | gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED); |
| 283 | return 0; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * gelic_card_release_rx_chain - free all skb of rx descr |
| 289 | * @card: card structure |
| 290 | * |
| 291 | */ |
| 292 | static void gelic_card_release_rx_chain(struct gelic_card *card) |
| 293 | { |
| 294 | struct gelic_descr *descr = card->rx_chain.head; |
| 295 | |
| 296 | do { |
| 297 | if (descr->skb) { |
| 298 | dma_unmap_single(ctodev(card), |
| 299 | be32_to_cpu(descr->buf_addr), |
| 300 | descr->skb->len, |
| 301 | DMA_FROM_DEVICE); |
| 302 | descr->buf_addr = 0; |
| 303 | dev_kfree_skb_any(descr->skb); |
| 304 | descr->skb = NULL; |
| 305 | gelic_descr_set_status(descr, |
| 306 | GELIC_DESCR_DMA_NOT_IN_USE); |
| 307 | } |
| 308 | descr = descr->next; |
| 309 | } while (descr != card->rx_chain.head); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains |
| 314 | * @card: card structure |
| 315 | * |
| 316 | * fills all descriptors in the rx chain: allocates skbs |
| 317 | * and iommu-maps them. |
| 318 | * returns 0 on success, < 0 on failure |
| 319 | */ |
| 320 | static int gelic_card_fill_rx_chain(struct gelic_card *card) |
| 321 | { |
| 322 | struct gelic_descr *descr = card->rx_chain.head; |
| 323 | int ret; |
| 324 | |
| 325 | do { |
| 326 | if (!descr->skb) { |
| 327 | ret = gelic_descr_prepare_rx(card, descr); |
| 328 | if (ret) |
| 329 | goto rewind; |
| 330 | } |
| 331 | descr = descr->next; |
| 332 | } while (descr != card->rx_chain.head); |
| 333 | |
| 334 | return 0; |
| 335 | rewind: |
| 336 | gelic_card_release_rx_chain(card); |
| 337 | return ret; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains |
| 342 | * @card: card structure |
| 343 | * |
| 344 | * returns 0 on success, < 0 on failure |
| 345 | */ |
| 346 | static int gelic_card_alloc_rx_skbs(struct gelic_card *card) |
| 347 | { |
| 348 | struct gelic_descr_chain *chain; |
| 349 | int ret; |
| 350 | chain = &card->rx_chain; |
| 351 | ret = gelic_card_fill_rx_chain(card); |
| 352 | chain->head = card->rx_top->prev; /* point to the last */ |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * gelic_descr_release_tx - processes a used tx descriptor |
| 358 | * @card: card structure |
| 359 | * @descr: descriptor to release |
| 360 | * |
| 361 | * releases a used tx descriptor (unmapping, freeing of skb) |
| 362 | */ |
| 363 | static void gelic_descr_release_tx(struct gelic_card *card, |
| 364 | struct gelic_descr *descr) |
| 365 | { |
| 366 | struct sk_buff *skb = descr->skb; |
| 367 | |
| 368 | #ifdef DEBUG |
| 369 | BUG_ON(!(be32_to_cpu(descr->data_status) & |
| 370 | (1 << GELIC_DESCR_TX_DMA_FRAME_TAIL))); |
| 371 | #endif |
| 372 | dma_unmap_single(ctodev(card), |
| 373 | be32_to_cpu(descr->buf_addr), skb->len, DMA_TO_DEVICE); |
| 374 | dev_kfree_skb_any(skb); |
| 375 | |
| 376 | descr->buf_addr = 0; |
| 377 | descr->buf_size = 0; |
| 378 | descr->next_descr_addr = 0; |
| 379 | descr->result_size = 0; |
| 380 | descr->valid_size = 0; |
| 381 | descr->data_status = 0; |
| 382 | descr->data_error = 0; |
| 383 | descr->skb = NULL; |
| 384 | |
| 385 | /* set descr status */ |
| 386 | gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE); |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * gelic_card_release_tx_chain - processes sent tx descriptors |
| 391 | * @card: adapter structure |
| 392 | * @stop: net_stop sequence |
| 393 | * |
| 394 | * releases the tx descriptors that gelic has finished with |
| 395 | */ |
| 396 | static void gelic_card_release_tx_chain(struct gelic_card *card, int stop) |
| 397 | { |
| 398 | struct gelic_descr_chain *tx_chain; |
| 399 | enum gelic_descr_dma_status status; |
| 400 | int release = 0; |
| 401 | |
| 402 | for (tx_chain = &card->tx_chain; |
| 403 | tx_chain->head != tx_chain->tail && tx_chain->tail; |
| 404 | tx_chain->tail = tx_chain->tail->next) { |
| 405 | status = gelic_descr_get_status(tx_chain->tail); |
| 406 | switch (status) { |
| 407 | case GELIC_DESCR_DMA_RESPONSE_ERROR: |
| 408 | case GELIC_DESCR_DMA_PROTECTION_ERROR: |
| 409 | case GELIC_DESCR_DMA_FORCE_END: |
| 410 | if (printk_ratelimit()) |
| 411 | dev_info(ctodev(card), |
| 412 | "%s: forcing end of tx descriptor " \ |
| 413 | "with status %x\n", |
| 414 | __func__, status); |
| 415 | card->netdev->stats.tx_dropped++; |
| 416 | break; |
| 417 | |
| 418 | case GELIC_DESCR_DMA_COMPLETE: |
| 419 | if (tx_chain->tail->skb) { |
| 420 | card->netdev->stats.tx_packets++; |
| 421 | card->netdev->stats.tx_bytes += |
| 422 | tx_chain->tail->skb->len; |
| 423 | } |
| 424 | break; |
| 425 | |
| 426 | case GELIC_DESCR_DMA_CARDOWNED: |
| 427 | /* pending tx request */ |
| 428 | default: |
| 429 | /* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */ |
| 430 | if (!stop) |
| 431 | goto out; |
| 432 | } |
| 433 | gelic_descr_release_tx(card, tx_chain->tail); |
| 434 | release ++; |
| 435 | } |
| 436 | out: |
| 437 | if (!stop && release) |
| 438 | netif_wake_queue(card->netdev); |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * gelic_net_set_multi - sets multicast addresses and promisc flags |
| 443 | * @netdev: interface device structure |
| 444 | * |
| 445 | * gelic_net_set_multi configures multicast addresses as needed for the |
| 446 | * netdev interface. It also sets up multicast, allmulti and promisc |
| 447 | * flags appropriately |
| 448 | */ |
| 449 | static void gelic_net_set_multi(struct net_device *netdev) |
| 450 | { |
| 451 | struct gelic_card *card = netdev_priv(netdev); |
| 452 | struct dev_mc_list *mc; |
| 453 | unsigned int i; |
| 454 | uint8_t *p; |
| 455 | u64 addr; |
| 456 | int status; |
| 457 | |
| 458 | /* clear all multicast address */ |
| 459 | status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card), |
| 460 | 0, 1); |
| 461 | if (status) |
| 462 | dev_err(ctodev(card), |
| 463 | "lv1_net_remove_multicast_address failed %d\n", |
| 464 | status); |
| 465 | /* set broadcast address */ |
| 466 | status = lv1_net_add_multicast_address(bus_id(card), dev_id(card), |
| 467 | GELIC_NET_BROADCAST_ADDR, 0); |
| 468 | if (status) |
| 469 | dev_err(ctodev(card), |
| 470 | "lv1_net_add_multicast_address failed, %d\n", |
| 471 | status); |
| 472 | |
| 473 | if (netdev->flags & IFF_ALLMULTI |
| 474 | || netdev->mc_count > GELIC_NET_MC_COUNT_MAX) { /* list max */ |
| 475 | status = lv1_net_add_multicast_address(bus_id(card), |
| 476 | dev_id(card), |
| 477 | 0, 1); |
| 478 | if (status) |
| 479 | dev_err(ctodev(card), |
| 480 | "lv1_net_add_multicast_address failed, %d\n", |
| 481 | status); |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | /* set multicast address */ |
| 486 | for (mc = netdev->mc_list; mc; mc = mc->next) { |
| 487 | addr = 0; |
| 488 | p = mc->dmi_addr; |
| 489 | for (i = 0; i < ETH_ALEN; i++) { |
| 490 | addr <<= 8; |
| 491 | addr |= *p++; |
| 492 | } |
| 493 | status = lv1_net_add_multicast_address(bus_id(card), |
| 494 | dev_id(card), |
| 495 | addr, 0); |
| 496 | if (status) |
| 497 | dev_err(ctodev(card), |
| 498 | "lv1_net_add_multicast_address failed, %d\n", |
| 499 | status); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * gelic_card_enable_rxdmac - enables the receive DMA controller |
| 505 | * @card: card structure |
| 506 | * |
| 507 | * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN |
| 508 | * in the GDADMACCNTR register |
| 509 | */ |
| 510 | static inline void gelic_card_enable_rxdmac(struct gelic_card *card) |
| 511 | { |
| 512 | int status; |
| 513 | |
| 514 | status = lv1_net_start_rx_dma(bus_id(card), dev_id(card), |
| 515 | card->rx_chain.tail->bus_addr, 0); |
| 516 | if (status) |
| 517 | dev_info(ctodev(card), |
| 518 | "lv1_net_start_rx_dma failed, status=%d\n", status); |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * gelic_card_disable_rxdmac - disables the receive DMA controller |
| 523 | * @card: card structure |
| 524 | * |
| 525 | * gelic_card_disable_rxdmac terminates processing on the DMA controller by |
| 526 | * turing off DMA and issueing a force end |
| 527 | */ |
| 528 | static inline void gelic_card_disable_rxdmac(struct gelic_card *card) |
| 529 | { |
| 530 | int status; |
| 531 | |
| 532 | /* this hvc blocks until the DMA in progress really stopped */ |
| 533 | status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card), 0); |
| 534 | if (status) |
| 535 | dev_err(ctodev(card), |
| 536 | "lv1_net_stop_rx_dma faild, %d\n", status); |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * gelic_card_disable_txdmac - disables the transmit DMA controller |
| 541 | * @card: card structure |
| 542 | * |
| 543 | * gelic_card_disable_txdmac terminates processing on the DMA controller by |
| 544 | * turing off DMA and issueing a force end |
| 545 | */ |
| 546 | static inline void gelic_card_disable_txdmac(struct gelic_card *card) |
| 547 | { |
| 548 | int status; |
| 549 | |
| 550 | /* this hvc blocks until the DMA in progress really stopped */ |
| 551 | status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card), 0); |
| 552 | if (status) |
| 553 | dev_err(ctodev(card), |
| 554 | "lv1_net_stop_tx_dma faild, status=%d\n", status); |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * gelic_net_stop - called upon ifconfig down |
| 559 | * @netdev: interface device structure |
| 560 | * |
| 561 | * always returns 0 |
| 562 | */ |
| 563 | static int gelic_net_stop(struct net_device *netdev) |
| 564 | { |
| 565 | struct gelic_card *card = netdev_priv(netdev); |
| 566 | |
| 567 | napi_disable(&card->napi); |
| 568 | netif_stop_queue(netdev); |
| 569 | |
| 570 | /* turn off DMA, force end */ |
| 571 | gelic_card_disable_rxdmac(card); |
| 572 | gelic_card_disable_txdmac(card); |
| 573 | |
| 574 | gelic_card_set_irq_mask(card, 0); |
| 575 | |
| 576 | /* disconnect event port */ |
| 577 | free_irq(card->netdev->irq, card->netdev); |
| 578 | ps3_sb_event_receive_port_destroy(card->dev, card->netdev->irq); |
| 579 | card->netdev->irq = NO_IRQ; |
| 580 | |
| 581 | netif_carrier_off(netdev); |
| 582 | |
| 583 | /* release chains */ |
| 584 | gelic_card_release_tx_chain(card, 1); |
| 585 | gelic_card_release_rx_chain(card); |
| 586 | |
| 587 | gelic_card_free_chain(card, card->tx_top); |
| 588 | gelic_card_free_chain(card, card->rx_top); |
| 589 | |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * gelic_card_get_next_tx_descr - returns the next available tx descriptor |
| 595 | * @card: device structure to get descriptor from |
| 596 | * |
| 597 | * returns the address of the next descriptor, or NULL if not available. |
| 598 | */ |
| 599 | static struct gelic_descr * |
| 600 | gelic_card_get_next_tx_descr(struct gelic_card *card) |
| 601 | { |
| 602 | if (!card->tx_chain.head) |
| 603 | return NULL; |
| 604 | /* see if the next descriptor is free */ |
| 605 | if (card->tx_chain.tail != card->tx_chain.head->next && |
| 606 | gelic_descr_get_status(card->tx_chain.head) == |
| 607 | GELIC_DESCR_DMA_NOT_IN_USE) |
| 608 | return card->tx_chain.head; |
| 609 | else |
| 610 | return NULL; |
| 611 | |
| 612 | } |
| 613 | |
| 614 | /** |
| 615 | * gelic_descr_set_tx_cmdstat - sets the tx descriptor command field |
| 616 | * @descr: descriptor structure to fill out |
| 617 | * @skb: packet to consider |
| 618 | * |
| 619 | * fills out the command and status field of the descriptor structure, |
| 620 | * depending on hardware checksum settings. This function assumes a wmb() |
| 621 | * has executed before. |
| 622 | */ |
| 623 | static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr, |
| 624 | struct sk_buff *skb) |
| 625 | { |
| 626 | if (skb->ip_summed != CHECKSUM_PARTIAL) |
| 627 | descr->dmac_cmd_status = |
| 628 | cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM | |
| 629 | GELIC_DESCR_TX_DMA_FRAME_TAIL); |
| 630 | else { |
| 631 | /* is packet ip? |
| 632 | * if yes: tcp? udp? */ |
| 633 | if (skb->protocol == htons(ETH_P_IP)) { |
| 634 | if (ip_hdr(skb)->protocol == IPPROTO_TCP) |
| 635 | descr->dmac_cmd_status = |
| 636 | cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM | |
| 637 | GELIC_DESCR_TX_DMA_FRAME_TAIL); |
| 638 | |
| 639 | else if (ip_hdr(skb)->protocol == IPPROTO_UDP) |
| 640 | descr->dmac_cmd_status = |
| 641 | cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM | |
| 642 | GELIC_DESCR_TX_DMA_FRAME_TAIL); |
| 643 | else /* |
| 644 | * the stack should checksum non-tcp and non-udp |
| 645 | * packets on his own: NETIF_F_IP_CSUM |
| 646 | */ |
| 647 | descr->dmac_cmd_status = |
| 648 | cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM | |
| 649 | GELIC_DESCR_TX_DMA_FRAME_TAIL); |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | static inline struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb, |
| 655 | unsigned short tag) |
| 656 | { |
| 657 | struct vlan_ethhdr *veth; |
| 658 | static unsigned int c; |
| 659 | |
| 660 | if (skb_headroom(skb) < VLAN_HLEN) { |
| 661 | struct sk_buff *sk_tmp = skb; |
| 662 | pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c); |
| 663 | skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN); |
| 664 | if (!skb) |
| 665 | return NULL; |
| 666 | dev_kfree_skb_any(sk_tmp); |
| 667 | } |
| 668 | veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN); |
| 669 | |
| 670 | /* Move the mac addresses to the top of buffer */ |
| 671 | memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN); |
| 672 | |
| 673 | veth->h_vlan_proto = __constant_htons(ETH_P_8021Q); |
| 674 | veth->h_vlan_TCI = htons(tag); |
| 675 | |
| 676 | return skb; |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * gelic_descr_prepare_tx - get dma address of skb_data |
| 681 | * @card: card structure |
| 682 | * @descr: descriptor structure |
| 683 | * @skb: packet to use |
| 684 | * |
| 685 | * returns 0 on success, <0 on failure. |
| 686 | * |
| 687 | */ |
| 688 | static int gelic_descr_prepare_tx(struct gelic_card *card, |
| 689 | struct gelic_descr *descr, |
| 690 | struct sk_buff *skb) |
| 691 | { |
| 692 | dma_addr_t buf; |
| 693 | |
| 694 | if (card->vlan_index != -1) { |
| 695 | struct sk_buff *skb_tmp; |
| 696 | skb_tmp = gelic_put_vlan_tag(skb, |
| 697 | card->vlan_id[card->vlan_index]); |
| 698 | if (!skb_tmp) |
| 699 | return -ENOMEM; |
| 700 | skb = skb_tmp; |
| 701 | } |
| 702 | |
| 703 | buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE); |
| 704 | |
| 705 | if (!buf) { |
| 706 | dev_err(ctodev(card), |
| 707 | "dma map 2 failed (%p, %i). Dropping packet\n", |
| 708 | skb->data, skb->len); |
| 709 | return -ENOMEM; |
| 710 | } |
| 711 | |
| 712 | descr->buf_addr = cpu_to_be32(buf); |
| 713 | descr->buf_size = cpu_to_be32(skb->len); |
| 714 | descr->skb = skb; |
| 715 | descr->data_status = 0; |
| 716 | descr->next_descr_addr = 0; /* terminate hw descr */ |
| 717 | gelic_descr_set_tx_cmdstat(descr, skb); |
| 718 | |
| 719 | /* bump free descriptor pointer */ |
| 720 | card->tx_chain.head = descr->next; |
| 721 | return 0; |
| 722 | } |
| 723 | |
| 724 | /** |
| 725 | * gelic_card_kick_txdma - enables TX DMA processing |
| 726 | * @card: card structure |
| 727 | * @descr: descriptor address to enable TX processing at |
| 728 | * |
| 729 | */ |
| 730 | static int gelic_card_kick_txdma(struct gelic_card *card, |
| 731 | struct gelic_descr *descr) |
| 732 | { |
| 733 | int status = 0; |
| 734 | |
| 735 | if (card->tx_dma_progress) |
| 736 | return 0; |
| 737 | |
| 738 | if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) { |
| 739 | card->tx_dma_progress = 1; |
| 740 | status = lv1_net_start_tx_dma(bus_id(card), dev_id(card), |
| 741 | descr->bus_addr, 0); |
| 742 | if (status) |
| 743 | dev_info(ctodev(card), "lv1_net_start_txdma failed," \ |
| 744 | "status=%d\n", status); |
| 745 | } |
| 746 | return status; |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * gelic_net_xmit - transmits a frame over the device |
| 751 | * @skb: packet to send out |
| 752 | * @netdev: interface device structure |
| 753 | * |
| 754 | * returns 0 on success, <0 on failure |
| 755 | */ |
| 756 | static int gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev) |
| 757 | { |
| 758 | struct gelic_card *card = netdev_priv(netdev); |
| 759 | struct gelic_descr *descr; |
| 760 | int result; |
| 761 | unsigned long flags; |
| 762 | |
| 763 | spin_lock_irqsave(&card->tx_dma_lock, flags); |
| 764 | |
| 765 | gelic_card_release_tx_chain(card, 0); |
| 766 | |
| 767 | descr = gelic_card_get_next_tx_descr(card); |
| 768 | if (!descr) { |
| 769 | /* |
| 770 | * no more descriptors free |
| 771 | */ |
| 772 | netif_stop_queue(netdev); |
| 773 | spin_unlock_irqrestore(&card->tx_dma_lock, flags); |
| 774 | return NETDEV_TX_BUSY; |
| 775 | } |
| 776 | |
| 777 | result = gelic_descr_prepare_tx(card, descr, skb); |
| 778 | if (result) { |
| 779 | /* |
| 780 | * DMA map failed. As chanses are that failure |
| 781 | * would continue, just release skb and return |
| 782 | */ |
| 783 | card->netdev->stats.tx_dropped++; |
| 784 | dev_kfree_skb_any(skb); |
| 785 | spin_unlock_irqrestore(&card->tx_dma_lock, flags); |
| 786 | return NETDEV_TX_OK; |
| 787 | } |
| 788 | /* |
| 789 | * link this prepared descriptor to previous one |
| 790 | * to achieve high performance |
| 791 | */ |
| 792 | descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr); |
| 793 | /* |
| 794 | * as hardware descriptor is modified in the above lines, |
| 795 | * ensure that the hardware sees it |
| 796 | */ |
| 797 | wmb(); |
| 798 | if (gelic_card_kick_txdma(card, descr)) { |
| 799 | /* |
| 800 | * kick failed. |
| 801 | * release descriptors which were just prepared |
| 802 | */ |
| 803 | card->netdev->stats.tx_dropped++; |
| 804 | gelic_descr_release_tx(card, descr); |
| 805 | gelic_descr_release_tx(card, descr->next); |
| 806 | card->tx_chain.tail = descr->next->next; |
| 807 | dev_info(ctodev(card), "%s: kick failure\n", __func__); |
| 808 | } else { |
| 809 | /* OK, DMA started/reserved */ |
| 810 | netdev->trans_start = jiffies; |
| 811 | } |
| 812 | |
| 813 | spin_unlock_irqrestore(&card->tx_dma_lock, flags); |
| 814 | return NETDEV_TX_OK; |
| 815 | } |
| 816 | |
| 817 | /** |
| 818 | * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on |
| 819 | * @descr: descriptor to process |
| 820 | * @card: card structure |
| 821 | * |
| 822 | * iommu-unmaps the skb, fills out skb structure and passes the data to the |
| 823 | * stack. The descriptor state is not changed. |
| 824 | */ |
| 825 | static void gelic_net_pass_skb_up(struct gelic_descr *descr, |
| 826 | struct gelic_card *card) |
| 827 | { |
| 828 | struct sk_buff *skb; |
| 829 | struct net_device *netdev; |
| 830 | u32 data_status, data_error; |
| 831 | |
| 832 | data_status = be32_to_cpu(descr->data_status); |
| 833 | data_error = be32_to_cpu(descr->data_error); |
| 834 | netdev = card->netdev; |
| 835 | /* unmap skb buffer */ |
| 836 | skb = descr->skb; |
| 837 | dma_unmap_single(ctodev(card), |
| 838 | be32_to_cpu(descr->buf_addr), GELIC_NET_MAX_MTU, |
| 839 | DMA_FROM_DEVICE); |
| 840 | |
| 841 | skb_put(skb, descr->valid_size ? |
| 842 | be32_to_cpu(descr->valid_size) : |
| 843 | be32_to_cpu(descr->result_size)); |
| 844 | if (!descr->valid_size) |
| 845 | dev_info(ctodev(card), "buffer full %x %x %x\n", |
| 846 | be32_to_cpu(descr->result_size), |
| 847 | be32_to_cpu(descr->buf_size), |
| 848 | be32_to_cpu(descr->dmac_cmd_status)); |
| 849 | |
| 850 | descr->skb = NULL; |
| 851 | /* |
| 852 | * the card put 2 bytes vlan tag in front |
| 853 | * of the ethernet frame |
| 854 | */ |
| 855 | skb_pull(skb, 2); |
| 856 | skb->protocol = eth_type_trans(skb, netdev); |
| 857 | |
| 858 | /* checksum offload */ |
| 859 | if (card->rx_csum) { |
| 860 | if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) && |
| 861 | (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK))) |
| 862 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 863 | else |
| 864 | skb->ip_summed = CHECKSUM_NONE; |
| 865 | } else |
| 866 | skb->ip_summed = CHECKSUM_NONE; |
| 867 | |
| 868 | /* update netdevice statistics */ |
| 869 | card->netdev->stats.rx_packets++; |
| 870 | card->netdev->stats.rx_bytes += skb->len; |
| 871 | |
| 872 | /* pass skb up to stack */ |
| 873 | netif_receive_skb(skb); |
| 874 | } |
| 875 | |
| 876 | /** |
| 877 | * gelic_card_decode_one_descr - processes an rx descriptor |
| 878 | * @card: card structure |
| 879 | * |
| 880 | * returns 1 if a packet has been sent to the stack, otherwise 0 |
| 881 | * |
| 882 | * processes an rx descriptor by iommu-unmapping the data buffer and passing |
| 883 | * the packet up to the stack |
| 884 | */ |
| 885 | static int gelic_card_decode_one_descr(struct gelic_card *card) |
| 886 | { |
| 887 | enum gelic_descr_dma_status status; |
| 888 | struct gelic_descr_chain *chain = &card->rx_chain; |
| 889 | struct gelic_descr *descr = chain->tail; |
| 890 | int dmac_chain_ended; |
| 891 | |
| 892 | status = gelic_descr_get_status(descr); |
| 893 | /* is this descriptor terminated with next_descr == NULL? */ |
| 894 | dmac_chain_ended = |
| 895 | be32_to_cpu(descr->dmac_cmd_status) & |
| 896 | GELIC_DESCR_RX_DMA_CHAIN_END; |
| 897 | |
| 898 | if (status == GELIC_DESCR_DMA_CARDOWNED) |
| 899 | return 0; |
| 900 | |
| 901 | if (status == GELIC_DESCR_DMA_NOT_IN_USE) { |
| 902 | dev_dbg(ctodev(card), "dormant descr? %p\n", descr); |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) || |
| 907 | (status == GELIC_DESCR_DMA_PROTECTION_ERROR) || |
| 908 | (status == GELIC_DESCR_DMA_FORCE_END)) { |
| 909 | dev_info(ctodev(card), "dropping RX descriptor with state %x\n", |
| 910 | status); |
| 911 | card->netdev->stats.rx_dropped++; |
| 912 | goto refill; |
| 913 | } |
| 914 | |
| 915 | if (status == GELIC_DESCR_DMA_BUFFER_FULL) { |
| 916 | /* |
| 917 | * Buffer full would occur if and only if |
| 918 | * the frame length was longer than the size of this |
| 919 | * descriptor's buffer. If the frame length was equal |
| 920 | * to or shorter than buffer'size, FRAME_END condition |
| 921 | * would occur. |
| 922 | * Anyway this frame was longer than the MTU, |
| 923 | * just drop it. |
| 924 | */ |
| 925 | dev_info(ctodev(card), "overlength frame\n"); |
| 926 | goto refill; |
| 927 | } |
| 928 | /* |
| 929 | * descriptoers any other than FRAME_END here should |
| 930 | * be treated as error. |
| 931 | */ |
| 932 | if (status != GELIC_DESCR_DMA_FRAME_END) { |
| 933 | dev_dbg(ctodev(card), "RX descriptor with state %x\n", |
| 934 | status); |
| 935 | goto refill; |
| 936 | } |
| 937 | |
| 938 | /* ok, we've got a packet in descr */ |
| 939 | gelic_net_pass_skb_up(descr, card); |
| 940 | refill: |
| 941 | /* |
| 942 | * So that always DMAC can see the end |
| 943 | * of the descriptor chain to avoid |
| 944 | * from unwanted DMAC overrun. |
| 945 | */ |
| 946 | descr->next_descr_addr = 0; |
| 947 | |
| 948 | /* change the descriptor state: */ |
| 949 | gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE); |
| 950 | |
| 951 | /* |
| 952 | * this call can fail, but for now, just leave this |
| 953 | * decriptor without skb |
| 954 | */ |
| 955 | gelic_descr_prepare_rx(card, descr); |
| 956 | |
| 957 | chain->head = descr; |
| 958 | chain->tail = descr->next; |
| 959 | |
| 960 | /* |
| 961 | * Set this descriptor the end of the chain. |
| 962 | */ |
| 963 | descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr); |
| 964 | |
| 965 | /* |
| 966 | * If dmac chain was met, DMAC stopped. |
| 967 | * thus re-enable it |
| 968 | */ |
| 969 | if (dmac_chain_ended) { |
| 970 | card->rx_dma_restart_required = 1; |
| 971 | dev_dbg(ctodev(card), "reenable rx dma scheduled\n"); |
| 972 | } |
| 973 | |
| 974 | return 1; |
| 975 | } |
| 976 | |
| 977 | /** |
| 978 | * gelic_net_poll - NAPI poll function called by the stack to return packets |
| 979 | * @netdev: interface device structure |
| 980 | * @budget: number of packets we can pass to the stack at most |
| 981 | * |
| 982 | * returns 0 if no more packets available to the driver/stack. Returns 1, |
| 983 | * if the quota is exceeded, but the driver has still packets. |
| 984 | * |
| 985 | */ |
| 986 | static int gelic_net_poll(struct napi_struct *napi, int budget) |
| 987 | { |
| 988 | struct gelic_card *card = container_of(napi, struct gelic_card, napi); |
| 989 | struct net_device *netdev = card->netdev; |
| 990 | int packets_done = 0; |
| 991 | |
| 992 | while (packets_done < budget) { |
| 993 | if (!gelic_card_decode_one_descr(card)) |
| 994 | break; |
| 995 | |
| 996 | packets_done++; |
| 997 | } |
| 998 | |
| 999 | if (packets_done < budget) { |
| 1000 | netif_rx_complete(netdev, napi); |
| 1001 | gelic_card_rx_irq_on(card); |
| 1002 | } |
| 1003 | return packets_done; |
| 1004 | } |
| 1005 | /** |
| 1006 | * gelic_net_change_mtu - changes the MTU of an interface |
| 1007 | * @netdev: interface device structure |
| 1008 | * @new_mtu: new MTU value |
| 1009 | * |
| 1010 | * returns 0 on success, <0 on failure |
| 1011 | */ |
| 1012 | static int gelic_net_change_mtu(struct net_device *netdev, int new_mtu) |
| 1013 | { |
| 1014 | /* no need to re-alloc skbs or so -- the max mtu is about 2.3k |
| 1015 | * and mtu is outbound only anyway */ |
| 1016 | if ((new_mtu < GELIC_NET_MIN_MTU) || |
| 1017 | (new_mtu > GELIC_NET_MAX_MTU)) { |
| 1018 | return -EINVAL; |
| 1019 | } |
| 1020 | netdev->mtu = new_mtu; |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | /** |
| 1025 | * gelic_card_interrupt - event handler for gelic_net |
| 1026 | */ |
| 1027 | static irqreturn_t gelic_card_interrupt(int irq, void *ptr) |
| 1028 | { |
| 1029 | unsigned long flags; |
| 1030 | struct net_device *netdev = ptr; |
| 1031 | struct gelic_card *card = netdev_priv(netdev); |
| 1032 | u64 status; |
| 1033 | |
| 1034 | status = card->irq_status; |
| 1035 | |
| 1036 | if (!status) |
| 1037 | return IRQ_NONE; |
| 1038 | |
| 1039 | if (card->rx_dma_restart_required) { |
| 1040 | card->rx_dma_restart_required = 0; |
| 1041 | gelic_card_enable_rxdmac(card); |
| 1042 | } |
| 1043 | |
| 1044 | if (status & GELIC_CARD_RXINT) { |
| 1045 | gelic_card_rx_irq_off(card); |
| 1046 | netif_rx_schedule(netdev, &card->napi); |
| 1047 | } |
| 1048 | |
| 1049 | if (status & GELIC_CARD_TXINT) { |
| 1050 | spin_lock_irqsave(&card->tx_dma_lock, flags); |
| 1051 | card->tx_dma_progress = 0; |
| 1052 | gelic_card_release_tx_chain(card, 0); |
| 1053 | /* kick outstanding tx descriptor if any */ |
| 1054 | gelic_card_kick_txdma(card, card->tx_chain.tail); |
| 1055 | spin_unlock_irqrestore(&card->tx_dma_lock, flags); |
| 1056 | } |
| 1057 | |
| 1058 | /* ether port status changed */ |
| 1059 | if (status & GELIC_CARD_PORT_STATUS_CHANGED) |
| 1060 | gelic_card_get_ether_port_status(card, 1); |
| 1061 | return IRQ_HANDLED; |
| 1062 | } |
| 1063 | |
| 1064 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1065 | /** |
| 1066 | * gelic_net_poll_controller - artificial interrupt for netconsole etc. |
| 1067 | * @netdev: interface device structure |
| 1068 | * |
| 1069 | * see Documentation/networking/netconsole.txt |
| 1070 | */ |
| 1071 | static void gelic_net_poll_controller(struct net_device *netdev) |
| 1072 | { |
| 1073 | struct gelic_card *card = netdev_priv(netdev); |
| 1074 | |
| 1075 | gelic_card_set_irq_mask(card, 0); |
| 1076 | gelic_card_interrupt(netdev->irq, netdev); |
| 1077 | gelic_card_set_irq_mask(card, card->ghiintmask); |
| 1078 | } |
| 1079 | #endif /* CONFIG_NET_POLL_CONTROLLER */ |
| 1080 | |
| 1081 | /** |
| 1082 | * gelic_card_open - open device and map dma region |
| 1083 | * @card: card structure |
| 1084 | */ |
| 1085 | static int gelic_card_open(struct gelic_card *card) |
| 1086 | { |
| 1087 | int result; |
| 1088 | |
| 1089 | result = ps3_sb_event_receive_port_setup(card->dev, PS3_BINDING_CPU_ANY, |
| 1090 | &card->netdev->irq); |
| 1091 | |
| 1092 | if (result) { |
| 1093 | dev_info(ctodev(card), |
| 1094 | "%s:%d: recieve_port_setup failed (%d)\n", |
| 1095 | __func__, __LINE__, result); |
| 1096 | result = -EPERM; |
| 1097 | goto fail_alloc_irq; |
| 1098 | } |
| 1099 | |
| 1100 | result = request_irq(card->netdev->irq, gelic_card_interrupt, |
| 1101 | IRQF_DISABLED, card->netdev->name, card->netdev); |
| 1102 | |
| 1103 | if (result) { |
| 1104 | dev_info(ctodev(card), "%s:%d: request_irq failed (%d)\n", |
| 1105 | __func__, __LINE__, result); |
| 1106 | goto fail_request_irq; |
| 1107 | } |
| 1108 | |
| 1109 | return 0; |
| 1110 | |
| 1111 | fail_request_irq: |
| 1112 | ps3_sb_event_receive_port_destroy(card->dev, card->netdev->irq); |
| 1113 | card->netdev->irq = NO_IRQ; |
| 1114 | fail_alloc_irq: |
| 1115 | return result; |
| 1116 | } |
| 1117 | |
| 1118 | |
| 1119 | /** |
| 1120 | * gelic_net_open - called upon ifonfig up |
| 1121 | * @netdev: interface device structure |
| 1122 | * |
| 1123 | * returns 0 on success, <0 on failure |
| 1124 | * |
| 1125 | * gelic_net_open allocates all the descriptors and memory needed for |
| 1126 | * operation, sets up multicast list and enables interrupts |
| 1127 | */ |
| 1128 | static int gelic_net_open(struct net_device *netdev) |
| 1129 | { |
| 1130 | struct gelic_card *card = netdev_priv(netdev); |
| 1131 | |
| 1132 | dev_dbg(ctodev(card), " -> %s:%d\n", __func__, __LINE__); |
| 1133 | |
| 1134 | gelic_card_open(card); |
| 1135 | |
| 1136 | if (gelic_card_init_chain(card, &card->tx_chain, |
| 1137 | card->descr, GELIC_NET_TX_DESCRIPTORS)) |
| 1138 | goto alloc_tx_failed; |
| 1139 | if (gelic_card_init_chain(card, &card->rx_chain, |
| 1140 | card->descr + GELIC_NET_TX_DESCRIPTORS, |
| 1141 | GELIC_NET_RX_DESCRIPTORS)) |
| 1142 | goto alloc_rx_failed; |
| 1143 | |
| 1144 | /* head of chain */ |
| 1145 | card->tx_top = card->tx_chain.head; |
| 1146 | card->rx_top = card->rx_chain.head; |
| 1147 | dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n", |
| 1148 | card->rx_top, card->tx_top, sizeof(struct gelic_descr), |
| 1149 | GELIC_NET_RX_DESCRIPTORS); |
| 1150 | /* allocate rx skbs */ |
| 1151 | if (gelic_card_alloc_rx_skbs(card)) |
| 1152 | goto alloc_skbs_failed; |
| 1153 | |
| 1154 | napi_enable(&card->napi); |
| 1155 | |
| 1156 | card->tx_dma_progress = 0; |
| 1157 | card->ghiintmask = GELIC_CARD_RXINT | GELIC_CARD_TXINT | |
| 1158 | GELIC_CARD_PORT_STATUS_CHANGED; |
| 1159 | |
| 1160 | gelic_card_set_irq_mask(card, card->ghiintmask); |
| 1161 | gelic_card_enable_rxdmac(card); |
| 1162 | |
| 1163 | netif_start_queue(netdev); |
| 1164 | gelic_card_get_ether_port_status(card, 1); |
| 1165 | |
| 1166 | return 0; |
| 1167 | |
| 1168 | alloc_skbs_failed: |
| 1169 | gelic_card_free_chain(card, card->rx_top); |
| 1170 | alloc_rx_failed: |
| 1171 | gelic_card_free_chain(card, card->tx_top); |
| 1172 | alloc_tx_failed: |
| 1173 | return -ENOMEM; |
| 1174 | } |
| 1175 | |
| 1176 | static void gelic_net_get_drvinfo(struct net_device *netdev, |
| 1177 | struct ethtool_drvinfo *info) |
| 1178 | { |
| 1179 | strncpy(info->driver, DRV_NAME, sizeof(info->driver) - 1); |
| 1180 | strncpy(info->version, DRV_VERSION, sizeof(info->version) - 1); |
| 1181 | } |
| 1182 | |
| 1183 | static int gelic_ether_get_settings(struct net_device *netdev, |
| 1184 | struct ethtool_cmd *cmd) |
| 1185 | { |
| 1186 | struct gelic_card *card = netdev_priv(netdev); |
| 1187 | |
| 1188 | gelic_card_get_ether_port_status(card, 0); |
| 1189 | |
| 1190 | if (card->ether_port_status & GELIC_LV1_ETHER_FULL_DUPLEX) |
| 1191 | cmd->duplex = DUPLEX_FULL; |
| 1192 | else |
| 1193 | cmd->duplex = DUPLEX_HALF; |
| 1194 | |
| 1195 | switch (card->ether_port_status & GELIC_LV1_ETHER_SPEED_MASK) { |
| 1196 | case GELIC_LV1_ETHER_SPEED_10: |
| 1197 | cmd->speed = SPEED_10; |
| 1198 | break; |
| 1199 | case GELIC_LV1_ETHER_SPEED_100: |
| 1200 | cmd->speed = SPEED_100; |
| 1201 | break; |
| 1202 | case GELIC_LV1_ETHER_SPEED_1000: |
| 1203 | cmd->speed = SPEED_1000; |
| 1204 | break; |
| 1205 | default: |
| 1206 | pr_info("%s: speed unknown\n", __func__); |
| 1207 | cmd->speed = SPEED_10; |
| 1208 | break; |
| 1209 | } |
| 1210 | |
| 1211 | cmd->supported = SUPPORTED_TP | SUPPORTED_Autoneg | |
| 1212 | SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full | |
| 1213 | SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full | |
| 1214 | SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full; |
| 1215 | cmd->advertising = cmd->supported; |
| 1216 | cmd->autoneg = AUTONEG_ENABLE; /* always enabled */ |
| 1217 | cmd->port = PORT_TP; |
| 1218 | |
| 1219 | return 0; |
| 1220 | } |
| 1221 | |
| 1222 | static int gelic_net_nway_reset(struct net_device *netdev) |
| 1223 | { |
| 1224 | if (netif_running(netdev)) { |
| 1225 | gelic_net_stop(netdev); |
| 1226 | gelic_net_open(netdev); |
| 1227 | } |
| 1228 | return 0; |
| 1229 | } |
| 1230 | |
| 1231 | static u32 gelic_net_get_rx_csum(struct net_device *netdev) |
| 1232 | { |
| 1233 | struct gelic_card *card = netdev_priv(netdev); |
| 1234 | |
| 1235 | return card->rx_csum; |
| 1236 | } |
| 1237 | |
| 1238 | static int gelic_net_set_rx_csum(struct net_device *netdev, u32 data) |
| 1239 | { |
| 1240 | struct gelic_card *card = netdev_priv(netdev); |
| 1241 | |
| 1242 | card->rx_csum = data; |
| 1243 | return 0; |
| 1244 | } |
| 1245 | |
| 1246 | static struct ethtool_ops gelic_net_ethtool_ops = { |
| 1247 | .get_drvinfo = gelic_net_get_drvinfo, |
| 1248 | .get_settings = gelic_ether_get_settings, |
| 1249 | .get_link = ethtool_op_get_link, |
| 1250 | .nway_reset = gelic_net_nway_reset, |
| 1251 | .get_tx_csum = ethtool_op_get_tx_csum, |
| 1252 | .set_tx_csum = ethtool_op_set_tx_csum, |
| 1253 | .get_rx_csum = gelic_net_get_rx_csum, |
| 1254 | .set_rx_csum = gelic_net_set_rx_csum, |
| 1255 | }; |
| 1256 | |
| 1257 | /** |
| 1258 | * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout |
| 1259 | * function (to be called not under interrupt status) |
| 1260 | * @work: work is context of tx timout task |
| 1261 | * |
| 1262 | * called as task when tx hangs, resets interface (if interface is up) |
| 1263 | */ |
| 1264 | static void gelic_net_tx_timeout_task(struct work_struct *work) |
| 1265 | { |
| 1266 | struct gelic_card *card = |
| 1267 | container_of(work, struct gelic_card, tx_timeout_task); |
| 1268 | struct net_device *netdev = card->netdev; |
| 1269 | |
| 1270 | dev_info(ctodev(card), "%s:Timed out. Restarting... \n", __func__); |
| 1271 | |
| 1272 | if (!(netdev->flags & IFF_UP)) |
| 1273 | goto out; |
| 1274 | |
| 1275 | netif_device_detach(netdev); |
| 1276 | gelic_net_stop(netdev); |
| 1277 | |
| 1278 | gelic_net_open(netdev); |
| 1279 | netif_device_attach(netdev); |
| 1280 | |
| 1281 | out: |
| 1282 | atomic_dec(&card->tx_timeout_task_counter); |
| 1283 | } |
| 1284 | |
| 1285 | /** |
| 1286 | * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in. |
| 1287 | * @netdev: interface device structure |
| 1288 | * |
| 1289 | * called, if tx hangs. Schedules a task that resets the interface |
| 1290 | */ |
| 1291 | static void gelic_net_tx_timeout(struct net_device *netdev) |
| 1292 | { |
| 1293 | struct gelic_card *card; |
| 1294 | |
| 1295 | card = netdev_priv(netdev); |
| 1296 | atomic_inc(&card->tx_timeout_task_counter); |
| 1297 | if (netdev->flags & IFF_UP) |
| 1298 | schedule_work(&card->tx_timeout_task); |
| 1299 | else |
| 1300 | atomic_dec(&card->tx_timeout_task_counter); |
| 1301 | } |
| 1302 | |
| 1303 | /** |
| 1304 | * gelic_ether_setup_netdev_ops - initialization of net_device operations |
| 1305 | * @netdev: net_device structure |
| 1306 | * |
| 1307 | * fills out function pointers in the net_device structure |
| 1308 | */ |
| 1309 | static void gelic_ether_setup_netdev_ops(struct net_device *netdev) |
| 1310 | { |
| 1311 | netdev->open = &gelic_net_open; |
| 1312 | netdev->stop = &gelic_net_stop; |
| 1313 | netdev->hard_start_xmit = &gelic_net_xmit; |
| 1314 | netdev->set_multicast_list = &gelic_net_set_multi; |
| 1315 | netdev->change_mtu = &gelic_net_change_mtu; |
| 1316 | /* tx watchdog */ |
| 1317 | netdev->tx_timeout = &gelic_net_tx_timeout; |
| 1318 | netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT; |
| 1319 | netdev->ethtool_ops = &gelic_net_ethtool_ops; |
| 1320 | } |
| 1321 | |
| 1322 | /** |
| 1323 | * gelic_net_setup_netdev - initialization of net_device |
| 1324 | * @card: card structure |
| 1325 | * |
| 1326 | * Returns 0 on success or <0 on failure |
| 1327 | * |
| 1328 | * gelic_net_setup_netdev initializes the net_device structure |
| 1329 | **/ |
| 1330 | static int gelic_net_setup_netdev(struct gelic_card *card) |
| 1331 | { |
| 1332 | struct net_device *netdev = card->netdev; |
| 1333 | struct sockaddr addr; |
| 1334 | unsigned int i; |
| 1335 | int status; |
| 1336 | u64 v1, v2; |
| 1337 | DECLARE_MAC_BUF(mac); |
| 1338 | |
| 1339 | SET_NETDEV_DEV(netdev, &card->dev->core); |
| 1340 | spin_lock_init(&card->tx_dma_lock); |
| 1341 | |
| 1342 | card->rx_csum = GELIC_NET_RX_CSUM_DEFAULT; |
| 1343 | |
| 1344 | gelic_ether_setup_netdev_ops(netdev); |
| 1345 | |
| 1346 | netif_napi_add(netdev, &card->napi, |
| 1347 | gelic_net_poll, GELIC_NET_NAPI_WEIGHT); |
| 1348 | |
| 1349 | netdev->features = NETIF_F_IP_CSUM; |
| 1350 | |
| 1351 | status = lv1_net_control(bus_id(card), dev_id(card), |
| 1352 | GELIC_LV1_GET_MAC_ADDRESS, |
| 1353 | 0, 0, 0, &v1, &v2); |
| 1354 | if (status || !is_valid_ether_addr((u8 *)&v1)) { |
| 1355 | dev_info(ctodev(card), |
| 1356 | "%s:lv1_net_control GET_MAC_ADDR failed %d\n", |
| 1357 | __func__, status); |
| 1358 | return -EINVAL; |
| 1359 | } |
| 1360 | v1 <<= 16; |
| 1361 | memcpy(addr.sa_data, &v1, ETH_ALEN); |
| 1362 | memcpy(netdev->dev_addr, addr.sa_data, ETH_ALEN); |
| 1363 | dev_info(ctodev(card), "MAC addr %s\n", |
| 1364 | print_mac(mac, netdev->dev_addr)); |
| 1365 | |
| 1366 | card->vlan_index = -1; /* no vlan */ |
| 1367 | for (i = 0; i < GELIC_NET_VLAN_MAX; i++) { |
| 1368 | status = lv1_net_control(bus_id(card), dev_id(card), |
| 1369 | GELIC_LV1_GET_VLAN_ID, |
| 1370 | i + 1, /* index; one based */ |
| 1371 | 0, 0, &v1, &v2); |
| 1372 | if (status == LV1_NO_ENTRY) { |
| 1373 | dev_dbg(ctodev(card), |
| 1374 | "GELIC_VLAN_ID no entry:%d, VLAN disabled\n", |
| 1375 | status); |
| 1376 | card->vlan_id[i] = 0; |
| 1377 | } else if (status) { |
| 1378 | dev_dbg(ctodev(card), |
| 1379 | "%s:get vlan id faild, status=%d\n", |
| 1380 | __func__, status); |
| 1381 | card->vlan_id[i] = 0; |
| 1382 | } else { |
| 1383 | card->vlan_id[i] = (u32)v1; |
| 1384 | dev_dbg(ctodev(card), "vlan_id:%d, %lx\n", i, v1); |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | if (card->vlan_id[GELIC_LV1_VLAN_TX_ETHERNET - 1]) { |
| 1389 | card->vlan_index = GELIC_LV1_VLAN_TX_ETHERNET - 1; |
| 1390 | netdev->hard_header_len += VLAN_HLEN; |
| 1391 | } |
| 1392 | |
| 1393 | status = register_netdev(netdev); |
| 1394 | if (status) { |
| 1395 | dev_err(ctodev(card), "%s:Couldn't register net_device: %d\n", |
| 1396 | __func__, status); |
| 1397 | return status; |
| 1398 | } |
| 1399 | |
| 1400 | return 0; |
| 1401 | } |
| 1402 | |
| 1403 | /** |
| 1404 | * gelic_alloc_card_net - allocates net_device and card structure |
| 1405 | * |
| 1406 | * returns the card structure or NULL in case of errors |
| 1407 | * |
| 1408 | * the card and net_device structures are linked to each other |
| 1409 | */ |
| 1410 | static struct gelic_card *gelic_alloc_card_net(void) |
| 1411 | { |
| 1412 | struct net_device *netdev; |
| 1413 | struct gelic_card *card; |
| 1414 | size_t alloc_size; |
| 1415 | |
| 1416 | alloc_size = sizeof(*card) + |
| 1417 | sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS + |
| 1418 | sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS; |
| 1419 | /* |
| 1420 | * we assume private data is allocated 32 bytes (or more) aligned |
| 1421 | * so that gelic_descr should be 32 bytes aligned. |
| 1422 | * Current alloc_etherdev() does do it because NETDEV_ALIGN |
| 1423 | * is 32. |
| 1424 | * check this assumption here. |
| 1425 | */ |
| 1426 | BUILD_BUG_ON(NETDEV_ALIGN < 32); |
| 1427 | BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8); |
| 1428 | BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32); |
| 1429 | |
| 1430 | netdev = alloc_etherdev(alloc_size); |
| 1431 | if (!netdev) |
| 1432 | return NULL; |
| 1433 | |
| 1434 | card = netdev_priv(netdev); |
| 1435 | card->netdev = netdev; |
| 1436 | INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task); |
| 1437 | init_waitqueue_head(&card->waitq); |
| 1438 | atomic_set(&card->tx_timeout_task_counter, 0); |
| 1439 | |
| 1440 | return card; |
| 1441 | } |
| 1442 | |
| 1443 | /** |
| 1444 | * ps3_gelic_driver_probe - add a device to the control of this driver |
| 1445 | */ |
| 1446 | static int ps3_gelic_driver_probe(struct ps3_system_bus_device *dev) |
| 1447 | { |
| 1448 | struct gelic_card *card = gelic_alloc_card_net(); |
| 1449 | int result; |
| 1450 | |
| 1451 | if (!card) { |
| 1452 | dev_info(&dev->core, "gelic_net_alloc_card failed\n"); |
| 1453 | result = -ENOMEM; |
| 1454 | goto fail_alloc_card; |
| 1455 | } |
| 1456 | |
| 1457 | ps3_system_bus_set_driver_data(dev, card); |
| 1458 | card->dev = dev; |
| 1459 | |
| 1460 | result = ps3_open_hv_device(dev); |
| 1461 | |
| 1462 | if (result) { |
| 1463 | dev_dbg(&dev->core, "ps3_open_hv_device failed\n"); |
| 1464 | goto fail_open; |
| 1465 | } |
| 1466 | |
| 1467 | result = ps3_dma_region_create(dev->d_region); |
| 1468 | |
| 1469 | if (result) { |
| 1470 | dev_dbg(&dev->core, "ps3_dma_region_create failed(%d)\n", |
| 1471 | result); |
| 1472 | BUG_ON("check region type"); |
| 1473 | goto fail_dma_region; |
| 1474 | } |
| 1475 | |
| 1476 | result = lv1_net_set_interrupt_status_indicator(bus_id(card), |
| 1477 | dev_id(card), |
| 1478 | ps3_mm_phys_to_lpar(__pa(&card->irq_status)), |
| 1479 | 0); |
| 1480 | |
| 1481 | if (result) { |
| 1482 | dev_dbg(&dev->core, |
| 1483 | "lv1_net_set_interrupt_status_indicator failed: %s\n", |
| 1484 | ps3_result(result)); |
| 1485 | result = -EIO; |
| 1486 | goto fail_status_indicator; |
| 1487 | } |
| 1488 | |
| 1489 | result = gelic_net_setup_netdev(card); |
| 1490 | |
| 1491 | if (result) { |
| 1492 | dev_dbg(&dev->core, "%s:%d: ps3_dma_region_create failed: " |
| 1493 | "(%d)\n", __func__, __LINE__, result); |
| 1494 | goto fail_setup_netdev; |
| 1495 | } |
| 1496 | |
| 1497 | return 0; |
| 1498 | |
| 1499 | fail_setup_netdev: |
| 1500 | lv1_net_set_interrupt_status_indicator(bus_id(card), |
| 1501 | dev_id(card), |
| 1502 | 0 , 0); |
| 1503 | fail_status_indicator: |
| 1504 | ps3_dma_region_free(dev->d_region); |
| 1505 | fail_dma_region: |
| 1506 | ps3_close_hv_device(dev); |
| 1507 | fail_open: |
| 1508 | ps3_system_bus_set_driver_data(dev, NULL); |
| 1509 | free_netdev(card->netdev); |
| 1510 | fail_alloc_card: |
| 1511 | return result; |
| 1512 | } |
| 1513 | |
| 1514 | /** |
| 1515 | * ps3_gelic_driver_remove - remove a device from the control of this driver |
| 1516 | */ |
| 1517 | |
| 1518 | static int ps3_gelic_driver_remove(struct ps3_system_bus_device *dev) |
| 1519 | { |
| 1520 | struct gelic_card *card = ps3_system_bus_get_driver_data(dev); |
| 1521 | |
| 1522 | wait_event(card->waitq, |
| 1523 | atomic_read(&card->tx_timeout_task_counter) == 0); |
| 1524 | |
| 1525 | lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card), |
| 1526 | 0 , 0); |
| 1527 | |
| 1528 | unregister_netdev(card->netdev); |
| 1529 | free_netdev(card->netdev); |
| 1530 | |
| 1531 | ps3_system_bus_set_driver_data(dev, NULL); |
| 1532 | |
| 1533 | ps3_dma_region_free(dev->d_region); |
| 1534 | |
| 1535 | ps3_close_hv_device(dev); |
| 1536 | |
| 1537 | return 0; |
| 1538 | } |
| 1539 | |
| 1540 | static struct ps3_system_bus_driver ps3_gelic_driver = { |
| 1541 | .match_id = PS3_MATCH_ID_GELIC, |
| 1542 | .probe = ps3_gelic_driver_probe, |
| 1543 | .remove = ps3_gelic_driver_remove, |
| 1544 | .shutdown = ps3_gelic_driver_remove, |
| 1545 | .core.name = "ps3_gelic_driver", |
| 1546 | .core.owner = THIS_MODULE, |
| 1547 | }; |
| 1548 | |
| 1549 | static int __init ps3_gelic_driver_init (void) |
| 1550 | { |
| 1551 | return firmware_has_feature(FW_FEATURE_PS3_LV1) |
| 1552 | ? ps3_system_bus_driver_register(&ps3_gelic_driver) |
| 1553 | : -ENODEV; |
| 1554 | } |
| 1555 | |
| 1556 | static void __exit ps3_gelic_driver_exit (void) |
| 1557 | { |
| 1558 | ps3_system_bus_driver_unregister(&ps3_gelic_driver); |
| 1559 | } |
| 1560 | |
| 1561 | module_init(ps3_gelic_driver_init); |
| 1562 | module_exit(ps3_gelic_driver_exit); |
| 1563 | |
| 1564 | MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC); |
| 1565 | |