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