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