Upgrade to 4.4.50-rt62
[kvmfornfv.git] / kernel / drivers / net / ethernet / atheros / alx / main.c
1 /*
2  * Copyright (c) 2013 Johannes Berg <johannes@sipsolutions.net>
3  *
4  *  This file is free software: you may copy, redistribute and/or modify it
5  *  under the terms of the GNU General Public License as published by the
6  *  Free Software Foundation, either version 2 of the License, or (at your
7  *  option) any later version.
8  *
9  *  This file is distributed in the hope that it will be useful, but
10  *  WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *  General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  * This file incorporates work covered by the following copyright and
18  * permission notice:
19  *
20  * Copyright (c) 2012 Qualcomm Atheros, Inc.
21  *
22  * Permission to use, copy, modify, and/or distribute this software for any
23  * purpose with or without fee is hereby granted, provided that the above
24  * copyright notice and this permission notice appear in all copies.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
27  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
28  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
29  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
30  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
31  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
32  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
33  */
34
35 #include <linux/module.h>
36 #include <linux/pci.h>
37 #include <linux/interrupt.h>
38 #include <linux/ip.h>
39 #include <linux/ipv6.h>
40 #include <linux/if_vlan.h>
41 #include <linux/mdio.h>
42 #include <linux/aer.h>
43 #include <linux/bitops.h>
44 #include <linux/netdevice.h>
45 #include <linux/etherdevice.h>
46 #include <net/ip6_checksum.h>
47 #include <linux/crc32.h>
48 #include "alx.h"
49 #include "hw.h"
50 #include "reg.h"
51
52 const char alx_drv_name[] = "alx";
53
54
55 static void alx_free_txbuf(struct alx_priv *alx, int entry)
56 {
57         struct alx_buffer *txb = &alx->txq.bufs[entry];
58
59         if (dma_unmap_len(txb, size)) {
60                 dma_unmap_single(&alx->hw.pdev->dev,
61                                  dma_unmap_addr(txb, dma),
62                                  dma_unmap_len(txb, size),
63                                  DMA_TO_DEVICE);
64                 dma_unmap_len_set(txb, size, 0);
65         }
66
67         if (txb->skb) {
68                 dev_kfree_skb_any(txb->skb);
69                 txb->skb = NULL;
70         }
71 }
72
73 static int alx_refill_rx_ring(struct alx_priv *alx, gfp_t gfp)
74 {
75         struct alx_rx_queue *rxq = &alx->rxq;
76         struct sk_buff *skb;
77         struct alx_buffer *cur_buf;
78         dma_addr_t dma;
79         u16 cur, next, count = 0;
80
81         next = cur = rxq->write_idx;
82         if (++next == alx->rx_ringsz)
83                 next = 0;
84         cur_buf = &rxq->bufs[cur];
85
86         while (!cur_buf->skb && next != rxq->read_idx) {
87                 struct alx_rfd *rfd = &rxq->rfd[cur];
88
89                 skb = __netdev_alloc_skb(alx->dev, alx->rxbuf_size + 64, gfp);
90                 if (!skb)
91                         break;
92
93                 /* Workround for the HW RX DMA overflow issue */
94                 if (((unsigned long)skb->data & 0xfff) == 0xfc0)
95                         skb_reserve(skb, 64);
96
97                 dma = dma_map_single(&alx->hw.pdev->dev,
98                                      skb->data, alx->rxbuf_size,
99                                      DMA_FROM_DEVICE);
100                 if (dma_mapping_error(&alx->hw.pdev->dev, dma)) {
101                         dev_kfree_skb(skb);
102                         break;
103                 }
104
105                 /* Unfortunately, RX descriptor buffers must be 4-byte
106                  * aligned, so we can't use IP alignment.
107                  */
108                 if (WARN_ON(dma & 3)) {
109                         dev_kfree_skb(skb);
110                         break;
111                 }
112
113                 cur_buf->skb = skb;
114                 dma_unmap_len_set(cur_buf, size, alx->rxbuf_size);
115                 dma_unmap_addr_set(cur_buf, dma, dma);
116                 rfd->addr = cpu_to_le64(dma);
117
118                 cur = next;
119                 if (++next == alx->rx_ringsz)
120                         next = 0;
121                 cur_buf = &rxq->bufs[cur];
122                 count++;
123         }
124
125         if (count) {
126                 /* flush all updates before updating hardware */
127                 wmb();
128                 rxq->write_idx = cur;
129                 alx_write_mem16(&alx->hw, ALX_RFD_PIDX, cur);
130         }
131
132         return count;
133 }
134
135 static inline int alx_tpd_avail(struct alx_priv *alx)
136 {
137         struct alx_tx_queue *txq = &alx->txq;
138
139         if (txq->write_idx >= txq->read_idx)
140                 return alx->tx_ringsz + txq->read_idx - txq->write_idx - 1;
141         return txq->read_idx - txq->write_idx - 1;
142 }
143
144 static bool alx_clean_tx_irq(struct alx_priv *alx)
145 {
146         struct alx_tx_queue *txq = &alx->txq;
147         u16 hw_read_idx, sw_read_idx;
148         unsigned int total_bytes = 0, total_packets = 0;
149         int budget = ALX_DEFAULT_TX_WORK;
150
151         sw_read_idx = txq->read_idx;
152         hw_read_idx = alx_read_mem16(&alx->hw, ALX_TPD_PRI0_CIDX);
153
154         if (sw_read_idx != hw_read_idx) {
155                 while (sw_read_idx != hw_read_idx && budget > 0) {
156                         struct sk_buff *skb;
157
158                         skb = txq->bufs[sw_read_idx].skb;
159                         if (skb) {
160                                 total_bytes += skb->len;
161                                 total_packets++;
162                                 budget--;
163                         }
164
165                         alx_free_txbuf(alx, sw_read_idx);
166
167                         if (++sw_read_idx == alx->tx_ringsz)
168                                 sw_read_idx = 0;
169                 }
170                 txq->read_idx = sw_read_idx;
171
172                 netdev_completed_queue(alx->dev, total_packets, total_bytes);
173         }
174
175         if (netif_queue_stopped(alx->dev) && netif_carrier_ok(alx->dev) &&
176             alx_tpd_avail(alx) > alx->tx_ringsz/4)
177                 netif_wake_queue(alx->dev);
178
179         return sw_read_idx == hw_read_idx;
180 }
181
182 static void alx_schedule_link_check(struct alx_priv *alx)
183 {
184         schedule_work(&alx->link_check_wk);
185 }
186
187 static void alx_schedule_reset(struct alx_priv *alx)
188 {
189         schedule_work(&alx->reset_wk);
190 }
191
192 static int alx_clean_rx_irq(struct alx_priv *alx, int budget)
193 {
194         struct alx_rx_queue *rxq = &alx->rxq;
195         struct alx_rrd *rrd;
196         struct alx_buffer *rxb;
197         struct sk_buff *skb;
198         u16 length, rfd_cleaned = 0;
199         int work = 0;
200
201         while (work < budget) {
202                 rrd = &rxq->rrd[rxq->rrd_read_idx];
203                 if (!(rrd->word3 & cpu_to_le32(1 << RRD_UPDATED_SHIFT)))
204                         break;
205                 rrd->word3 &= ~cpu_to_le32(1 << RRD_UPDATED_SHIFT);
206
207                 if (ALX_GET_FIELD(le32_to_cpu(rrd->word0),
208                                   RRD_SI) != rxq->read_idx ||
209                     ALX_GET_FIELD(le32_to_cpu(rrd->word0),
210                                   RRD_NOR) != 1) {
211                         alx_schedule_reset(alx);
212                         return work;
213                 }
214
215                 rxb = &rxq->bufs[rxq->read_idx];
216                 dma_unmap_single(&alx->hw.pdev->dev,
217                                  dma_unmap_addr(rxb, dma),
218                                  dma_unmap_len(rxb, size),
219                                  DMA_FROM_DEVICE);
220                 dma_unmap_len_set(rxb, size, 0);
221                 skb = rxb->skb;
222                 rxb->skb = NULL;
223
224                 if (rrd->word3 & cpu_to_le32(1 << RRD_ERR_RES_SHIFT) ||
225                     rrd->word3 & cpu_to_le32(1 << RRD_ERR_LEN_SHIFT)) {
226                         rrd->word3 = 0;
227                         dev_kfree_skb_any(skb);
228                         goto next_pkt;
229                 }
230
231                 length = ALX_GET_FIELD(le32_to_cpu(rrd->word3),
232                                        RRD_PKTLEN) - ETH_FCS_LEN;
233                 skb_put(skb, length);
234                 skb->protocol = eth_type_trans(skb, alx->dev);
235
236                 skb_checksum_none_assert(skb);
237                 if (alx->dev->features & NETIF_F_RXCSUM &&
238                     !(rrd->word3 & (cpu_to_le32(1 << RRD_ERR_L4_SHIFT) |
239                                     cpu_to_le32(1 << RRD_ERR_IPV4_SHIFT)))) {
240                         switch (ALX_GET_FIELD(le32_to_cpu(rrd->word2),
241                                               RRD_PID)) {
242                         case RRD_PID_IPV6UDP:
243                         case RRD_PID_IPV4UDP:
244                         case RRD_PID_IPV4TCP:
245                         case RRD_PID_IPV6TCP:
246                                 skb->ip_summed = CHECKSUM_UNNECESSARY;
247                                 break;
248                         }
249                 }
250
251                 napi_gro_receive(&alx->napi, skb);
252                 work++;
253
254 next_pkt:
255                 if (++rxq->read_idx == alx->rx_ringsz)
256                         rxq->read_idx = 0;
257                 if (++rxq->rrd_read_idx == alx->rx_ringsz)
258                         rxq->rrd_read_idx = 0;
259
260                 if (++rfd_cleaned > ALX_RX_ALLOC_THRESH)
261                         rfd_cleaned -= alx_refill_rx_ring(alx, GFP_ATOMIC);
262         }
263
264         if (rfd_cleaned)
265                 alx_refill_rx_ring(alx, GFP_ATOMIC);
266
267         return work;
268 }
269
270 static int alx_poll(struct napi_struct *napi, int budget)
271 {
272         struct alx_priv *alx = container_of(napi, struct alx_priv, napi);
273         struct alx_hw *hw = &alx->hw;
274         unsigned long flags;
275         bool tx_complete;
276         int work;
277
278         tx_complete = alx_clean_tx_irq(alx);
279         work = alx_clean_rx_irq(alx, budget);
280
281         if (!tx_complete || work == budget)
282                 return budget;
283
284         napi_complete(&alx->napi);
285
286         /* enable interrupt */
287         spin_lock_irqsave(&alx->irq_lock, flags);
288         alx->int_mask |= ALX_ISR_TX_Q0 | ALX_ISR_RX_Q0;
289         alx_write_mem32(hw, ALX_IMR, alx->int_mask);
290         spin_unlock_irqrestore(&alx->irq_lock, flags);
291
292         alx_post_write(hw);
293
294         return work;
295 }
296
297 static irqreturn_t alx_intr_handle(struct alx_priv *alx, u32 intr)
298 {
299         struct alx_hw *hw = &alx->hw;
300         bool write_int_mask = false;
301
302         spin_lock(&alx->irq_lock);
303
304         /* ACK interrupt */
305         alx_write_mem32(hw, ALX_ISR, intr | ALX_ISR_DIS);
306         intr &= alx->int_mask;
307
308         if (intr & ALX_ISR_FATAL) {
309                 netif_warn(alx, hw, alx->dev,
310                            "fatal interrupt 0x%x, resetting\n", intr);
311                 alx_schedule_reset(alx);
312                 goto out;
313         }
314
315         if (intr & ALX_ISR_ALERT)
316                 netdev_warn(alx->dev, "alert interrupt: 0x%x\n", intr);
317
318         if (intr & ALX_ISR_PHY) {
319                 /* suppress PHY interrupt, because the source
320                  * is from PHY internal. only the internal status
321                  * is cleared, the interrupt status could be cleared.
322                  */
323                 alx->int_mask &= ~ALX_ISR_PHY;
324                 write_int_mask = true;
325                 alx_schedule_link_check(alx);
326         }
327
328         if (intr & (ALX_ISR_TX_Q0 | ALX_ISR_RX_Q0)) {
329                 napi_schedule(&alx->napi);
330                 /* mask rx/tx interrupt, enable them when napi complete */
331                 alx->int_mask &= ~ALX_ISR_ALL_QUEUES;
332                 write_int_mask = true;
333         }
334
335         if (write_int_mask)
336                 alx_write_mem32(hw, ALX_IMR, alx->int_mask);
337
338         alx_write_mem32(hw, ALX_ISR, 0);
339
340  out:
341         spin_unlock(&alx->irq_lock);
342         return IRQ_HANDLED;
343 }
344
345 static irqreturn_t alx_intr_msi(int irq, void *data)
346 {
347         struct alx_priv *alx = data;
348
349         return alx_intr_handle(alx, alx_read_mem32(&alx->hw, ALX_ISR));
350 }
351
352 static irqreturn_t alx_intr_legacy(int irq, void *data)
353 {
354         struct alx_priv *alx = data;
355         struct alx_hw *hw = &alx->hw;
356         u32 intr;
357
358         intr = alx_read_mem32(hw, ALX_ISR);
359
360         if (intr & ALX_ISR_DIS || !(intr & alx->int_mask))
361                 return IRQ_NONE;
362
363         return alx_intr_handle(alx, intr);
364 }
365
366 static void alx_init_ring_ptrs(struct alx_priv *alx)
367 {
368         struct alx_hw *hw = &alx->hw;
369         u32 addr_hi = ((u64)alx->descmem.dma) >> 32;
370
371         alx->rxq.read_idx = 0;
372         alx->rxq.write_idx = 0;
373         alx->rxq.rrd_read_idx = 0;
374         alx_write_mem32(hw, ALX_RX_BASE_ADDR_HI, addr_hi);
375         alx_write_mem32(hw, ALX_RRD_ADDR_LO, alx->rxq.rrd_dma);
376         alx_write_mem32(hw, ALX_RRD_RING_SZ, alx->rx_ringsz);
377         alx_write_mem32(hw, ALX_RFD_ADDR_LO, alx->rxq.rfd_dma);
378         alx_write_mem32(hw, ALX_RFD_RING_SZ, alx->rx_ringsz);
379         alx_write_mem32(hw, ALX_RFD_BUF_SZ, alx->rxbuf_size);
380
381         alx->txq.read_idx = 0;
382         alx->txq.write_idx = 0;
383         alx_write_mem32(hw, ALX_TX_BASE_ADDR_HI, addr_hi);
384         alx_write_mem32(hw, ALX_TPD_PRI0_ADDR_LO, alx->txq.tpd_dma);
385         alx_write_mem32(hw, ALX_TPD_RING_SZ, alx->tx_ringsz);
386
387         /* load these pointers into the chip */
388         alx_write_mem32(hw, ALX_SRAM9, ALX_SRAM_LOAD_PTR);
389 }
390
391 static void alx_free_txring_buf(struct alx_priv *alx)
392 {
393         struct alx_tx_queue *txq = &alx->txq;
394         int i;
395
396         if (!txq->bufs)
397                 return;
398
399         for (i = 0; i < alx->tx_ringsz; i++)
400                 alx_free_txbuf(alx, i);
401
402         memset(txq->bufs, 0, alx->tx_ringsz * sizeof(struct alx_buffer));
403         memset(txq->tpd, 0, alx->tx_ringsz * sizeof(struct alx_txd));
404         txq->write_idx = 0;
405         txq->read_idx = 0;
406
407         netdev_reset_queue(alx->dev);
408 }
409
410 static void alx_free_rxring_buf(struct alx_priv *alx)
411 {
412         struct alx_rx_queue *rxq = &alx->rxq;
413         struct alx_buffer *cur_buf;
414         u16 i;
415
416         if (rxq == NULL)
417                 return;
418
419         for (i = 0; i < alx->rx_ringsz; i++) {
420                 cur_buf = rxq->bufs + i;
421                 if (cur_buf->skb) {
422                         dma_unmap_single(&alx->hw.pdev->dev,
423                                          dma_unmap_addr(cur_buf, dma),
424                                          dma_unmap_len(cur_buf, size),
425                                          DMA_FROM_DEVICE);
426                         dev_kfree_skb(cur_buf->skb);
427                         cur_buf->skb = NULL;
428                         dma_unmap_len_set(cur_buf, size, 0);
429                         dma_unmap_addr_set(cur_buf, dma, 0);
430                 }
431         }
432
433         rxq->write_idx = 0;
434         rxq->read_idx = 0;
435         rxq->rrd_read_idx = 0;
436 }
437
438 static void alx_free_buffers(struct alx_priv *alx)
439 {
440         alx_free_txring_buf(alx);
441         alx_free_rxring_buf(alx);
442 }
443
444 static int alx_reinit_rings(struct alx_priv *alx)
445 {
446         alx_free_buffers(alx);
447
448         alx_init_ring_ptrs(alx);
449
450         if (!alx_refill_rx_ring(alx, GFP_KERNEL))
451                 return -ENOMEM;
452
453         return 0;
454 }
455
456 static void alx_add_mc_addr(struct alx_hw *hw, const u8 *addr, u32 *mc_hash)
457 {
458         u32 crc32, bit, reg;
459
460         crc32 = ether_crc(ETH_ALEN, addr);
461         reg = (crc32 >> 31) & 0x1;
462         bit = (crc32 >> 26) & 0x1F;
463
464         mc_hash[reg] |= BIT(bit);
465 }
466
467 static void __alx_set_rx_mode(struct net_device *netdev)
468 {
469         struct alx_priv *alx = netdev_priv(netdev);
470         struct alx_hw *hw = &alx->hw;
471         struct netdev_hw_addr *ha;
472         u32 mc_hash[2] = {};
473
474         if (!(netdev->flags & IFF_ALLMULTI)) {
475                 netdev_for_each_mc_addr(ha, netdev)
476                         alx_add_mc_addr(hw, ha->addr, mc_hash);
477
478                 alx_write_mem32(hw, ALX_HASH_TBL0, mc_hash[0]);
479                 alx_write_mem32(hw, ALX_HASH_TBL1, mc_hash[1]);
480         }
481
482         hw->rx_ctrl &= ~(ALX_MAC_CTRL_MULTIALL_EN | ALX_MAC_CTRL_PROMISC_EN);
483         if (netdev->flags & IFF_PROMISC)
484                 hw->rx_ctrl |= ALX_MAC_CTRL_PROMISC_EN;
485         if (netdev->flags & IFF_ALLMULTI)
486                 hw->rx_ctrl |= ALX_MAC_CTRL_MULTIALL_EN;
487
488         alx_write_mem32(hw, ALX_MAC_CTRL, hw->rx_ctrl);
489 }
490
491 static void alx_set_rx_mode(struct net_device *netdev)
492 {
493         __alx_set_rx_mode(netdev);
494 }
495
496 static int alx_set_mac_address(struct net_device *netdev, void *data)
497 {
498         struct alx_priv *alx = netdev_priv(netdev);
499         struct alx_hw *hw = &alx->hw;
500         struct sockaddr *addr = data;
501
502         if (!is_valid_ether_addr(addr->sa_data))
503                 return -EADDRNOTAVAIL;
504
505         if (netdev->addr_assign_type & NET_ADDR_RANDOM)
506                 netdev->addr_assign_type ^= NET_ADDR_RANDOM;
507
508         memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
509         memcpy(hw->mac_addr, addr->sa_data, netdev->addr_len);
510         alx_set_macaddr(hw, hw->mac_addr);
511
512         return 0;
513 }
514
515 static int alx_alloc_descriptors(struct alx_priv *alx)
516 {
517         alx->txq.bufs = kcalloc(alx->tx_ringsz,
518                                 sizeof(struct alx_buffer),
519                                 GFP_KERNEL);
520         if (!alx->txq.bufs)
521                 return -ENOMEM;
522
523         alx->rxq.bufs = kcalloc(alx->rx_ringsz,
524                                 sizeof(struct alx_buffer),
525                                 GFP_KERNEL);
526         if (!alx->rxq.bufs)
527                 goto out_free;
528
529         /* physical tx/rx ring descriptors
530          *
531          * Allocate them as a single chunk because they must not cross a
532          * 4G boundary (hardware has a single register for high 32 bits
533          * of addresses only)
534          */
535         alx->descmem.size = sizeof(struct alx_txd) * alx->tx_ringsz +
536                             sizeof(struct alx_rrd) * alx->rx_ringsz +
537                             sizeof(struct alx_rfd) * alx->rx_ringsz;
538         alx->descmem.virt = dma_zalloc_coherent(&alx->hw.pdev->dev,
539                                                 alx->descmem.size,
540                                                 &alx->descmem.dma,
541                                                 GFP_KERNEL);
542         if (!alx->descmem.virt)
543                 goto out_free;
544
545         alx->txq.tpd = alx->descmem.virt;
546         alx->txq.tpd_dma = alx->descmem.dma;
547
548         /* alignment requirement for next block */
549         BUILD_BUG_ON(sizeof(struct alx_txd) % 8);
550
551         alx->rxq.rrd =
552                 (void *)((u8 *)alx->descmem.virt +
553                          sizeof(struct alx_txd) * alx->tx_ringsz);
554         alx->rxq.rrd_dma = alx->descmem.dma +
555                            sizeof(struct alx_txd) * alx->tx_ringsz;
556
557         /* alignment requirement for next block */
558         BUILD_BUG_ON(sizeof(struct alx_rrd) % 8);
559
560         alx->rxq.rfd =
561                 (void *)((u8 *)alx->descmem.virt +
562                          sizeof(struct alx_txd) * alx->tx_ringsz +
563                          sizeof(struct alx_rrd) * alx->rx_ringsz);
564         alx->rxq.rfd_dma = alx->descmem.dma +
565                            sizeof(struct alx_txd) * alx->tx_ringsz +
566                            sizeof(struct alx_rrd) * alx->rx_ringsz;
567
568         return 0;
569 out_free:
570         kfree(alx->txq.bufs);
571         kfree(alx->rxq.bufs);
572         return -ENOMEM;
573 }
574
575 static int alx_alloc_rings(struct alx_priv *alx)
576 {
577         int err;
578
579         err = alx_alloc_descriptors(alx);
580         if (err)
581                 return err;
582
583         alx->int_mask &= ~ALX_ISR_ALL_QUEUES;
584         alx->int_mask |= ALX_ISR_TX_Q0 | ALX_ISR_RX_Q0;
585         alx->tx_ringsz = alx->tx_ringsz;
586
587         netif_napi_add(alx->dev, &alx->napi, alx_poll, 64);
588
589         alx_reinit_rings(alx);
590         return 0;
591 }
592
593 static void alx_free_rings(struct alx_priv *alx)
594 {
595         netif_napi_del(&alx->napi);
596         alx_free_buffers(alx);
597
598         kfree(alx->txq.bufs);
599         kfree(alx->rxq.bufs);
600
601         dma_free_coherent(&alx->hw.pdev->dev,
602                           alx->descmem.size,
603                           alx->descmem.virt,
604                           alx->descmem.dma);
605 }
606
607 static void alx_config_vector_mapping(struct alx_priv *alx)
608 {
609         struct alx_hw *hw = &alx->hw;
610
611         alx_write_mem32(hw, ALX_MSI_MAP_TBL1, 0);
612         alx_write_mem32(hw, ALX_MSI_MAP_TBL2, 0);
613         alx_write_mem32(hw, ALX_MSI_ID_MAP, 0);
614 }
615
616 static void alx_irq_enable(struct alx_priv *alx)
617 {
618         struct alx_hw *hw = &alx->hw;
619
620         /* level-1 interrupt switch */
621         alx_write_mem32(hw, ALX_ISR, 0);
622         alx_write_mem32(hw, ALX_IMR, alx->int_mask);
623         alx_post_write(hw);
624 }
625
626 static void alx_irq_disable(struct alx_priv *alx)
627 {
628         struct alx_hw *hw = &alx->hw;
629
630         alx_write_mem32(hw, ALX_ISR, ALX_ISR_DIS);
631         alx_write_mem32(hw, ALX_IMR, 0);
632         alx_post_write(hw);
633
634         synchronize_irq(alx->hw.pdev->irq);
635 }
636
637 static int alx_request_irq(struct alx_priv *alx)
638 {
639         struct pci_dev *pdev = alx->hw.pdev;
640         struct alx_hw *hw = &alx->hw;
641         int err;
642         u32 msi_ctrl;
643
644         msi_ctrl = (hw->imt >> 1) << ALX_MSI_RETRANS_TM_SHIFT;
645
646         if (!pci_enable_msi(alx->hw.pdev)) {
647                 alx->msi = true;
648
649                 alx_write_mem32(hw, ALX_MSI_RETRANS_TIMER,
650                                 msi_ctrl | ALX_MSI_MASK_SEL_LINE);
651                 err = request_irq(pdev->irq, alx_intr_msi, 0,
652                                   alx->dev->name, alx);
653                 if (!err)
654                         goto out;
655                 /* fall back to legacy interrupt */
656                 pci_disable_msi(alx->hw.pdev);
657         }
658
659         alx_write_mem32(hw, ALX_MSI_RETRANS_TIMER, 0);
660         err = request_irq(pdev->irq, alx_intr_legacy, IRQF_SHARED,
661                           alx->dev->name, alx);
662 out:
663         if (!err)
664                 alx_config_vector_mapping(alx);
665         return err;
666 }
667
668 static void alx_free_irq(struct alx_priv *alx)
669 {
670         struct pci_dev *pdev = alx->hw.pdev;
671
672         free_irq(pdev->irq, alx);
673
674         if (alx->msi) {
675                 pci_disable_msi(alx->hw.pdev);
676                 alx->msi = false;
677         }
678 }
679
680 static int alx_identify_hw(struct alx_priv *alx)
681 {
682         struct alx_hw *hw = &alx->hw;
683         int rev = alx_hw_revision(hw);
684
685         if (rev > ALX_REV_C0)
686                 return -EINVAL;
687
688         hw->max_dma_chnl = rev >= ALX_REV_B0 ? 4 : 2;
689
690         return 0;
691 }
692
693 static int alx_init_sw(struct alx_priv *alx)
694 {
695         struct pci_dev *pdev = alx->hw.pdev;
696         struct alx_hw *hw = &alx->hw;
697         int err;
698
699         err = alx_identify_hw(alx);
700         if (err) {
701                 dev_err(&pdev->dev, "unrecognized chip, aborting\n");
702                 return err;
703         }
704
705         alx->hw.lnk_patch =
706                 pdev->device == ALX_DEV_ID_AR8161 &&
707                 pdev->subsystem_vendor == PCI_VENDOR_ID_ATTANSIC &&
708                 pdev->subsystem_device == 0x0091 &&
709                 pdev->revision == 0;
710
711         hw->smb_timer = 400;
712         hw->mtu = alx->dev->mtu;
713         alx->rxbuf_size = ALIGN(ALX_RAW_MTU(hw->mtu), 8);
714         alx->tx_ringsz = 256;
715         alx->rx_ringsz = 512;
716         hw->imt = 200;
717         alx->int_mask = ALX_ISR_MISC;
718         hw->dma_chnl = hw->max_dma_chnl;
719         hw->ith_tpd = alx->tx_ringsz / 3;
720         hw->link_speed = SPEED_UNKNOWN;
721         hw->duplex = DUPLEX_UNKNOWN;
722         hw->adv_cfg = ADVERTISED_Autoneg |
723                       ADVERTISED_10baseT_Half |
724                       ADVERTISED_10baseT_Full |
725                       ADVERTISED_100baseT_Full |
726                       ADVERTISED_100baseT_Half |
727                       ADVERTISED_1000baseT_Full;
728         hw->flowctrl = ALX_FC_ANEG | ALX_FC_RX | ALX_FC_TX;
729
730         hw->rx_ctrl = ALX_MAC_CTRL_WOLSPED_SWEN |
731                       ALX_MAC_CTRL_MHASH_ALG_HI5B |
732                       ALX_MAC_CTRL_BRD_EN |
733                       ALX_MAC_CTRL_PCRCE |
734                       ALX_MAC_CTRL_CRCE |
735                       ALX_MAC_CTRL_RXFC_EN |
736                       ALX_MAC_CTRL_TXFC_EN |
737                       7 << ALX_MAC_CTRL_PRMBLEN_SHIFT;
738
739         return err;
740 }
741
742
743 static netdev_features_t alx_fix_features(struct net_device *netdev,
744                                           netdev_features_t features)
745 {
746         if (netdev->mtu > ALX_MAX_TSO_PKT_SIZE)
747                 features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
748
749         return features;
750 }
751
752 static void alx_netif_stop(struct alx_priv *alx)
753 {
754         alx->dev->trans_start = jiffies;
755         if (netif_carrier_ok(alx->dev)) {
756                 netif_carrier_off(alx->dev);
757                 netif_tx_disable(alx->dev);
758                 napi_disable(&alx->napi);
759         }
760 }
761
762 static void alx_halt(struct alx_priv *alx)
763 {
764         struct alx_hw *hw = &alx->hw;
765
766         alx_netif_stop(alx);
767         hw->link_speed = SPEED_UNKNOWN;
768         hw->duplex = DUPLEX_UNKNOWN;
769
770         alx_reset_mac(hw);
771
772         /* disable l0s/l1 */
773         alx_enable_aspm(hw, false, false);
774         alx_irq_disable(alx);
775         alx_free_buffers(alx);
776 }
777
778 static void alx_configure(struct alx_priv *alx)
779 {
780         struct alx_hw *hw = &alx->hw;
781
782         alx_configure_basic(hw);
783         alx_disable_rss(hw);
784         __alx_set_rx_mode(alx->dev);
785
786         alx_write_mem32(hw, ALX_MAC_CTRL, hw->rx_ctrl);
787 }
788
789 static void alx_activate(struct alx_priv *alx)
790 {
791         /* hardware setting lost, restore it */
792         alx_reinit_rings(alx);
793         alx_configure(alx);
794
795         /* clear old interrupts */
796         alx_write_mem32(&alx->hw, ALX_ISR, ~(u32)ALX_ISR_DIS);
797
798         alx_irq_enable(alx);
799
800         alx_schedule_link_check(alx);
801 }
802
803 static void alx_reinit(struct alx_priv *alx)
804 {
805         ASSERT_RTNL();
806
807         alx_halt(alx);
808         alx_activate(alx);
809 }
810
811 static int alx_change_mtu(struct net_device *netdev, int mtu)
812 {
813         struct alx_priv *alx = netdev_priv(netdev);
814         int max_frame = mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
815
816         if ((max_frame < ALX_MIN_FRAME_SIZE) ||
817             (max_frame > ALX_MAX_FRAME_SIZE))
818                 return -EINVAL;
819
820         if (netdev->mtu == mtu)
821                 return 0;
822
823         netdev->mtu = mtu;
824         alx->hw.mtu = mtu;
825         alx->rxbuf_size = mtu > ALX_DEF_RXBUF_SIZE ?
826                            ALIGN(max_frame, 8) : ALX_DEF_RXBUF_SIZE;
827         netdev_update_features(netdev);
828         if (netif_running(netdev))
829                 alx_reinit(alx);
830         return 0;
831 }
832
833 static void alx_netif_start(struct alx_priv *alx)
834 {
835         netif_tx_wake_all_queues(alx->dev);
836         napi_enable(&alx->napi);
837         netif_carrier_on(alx->dev);
838 }
839
840 static int __alx_open(struct alx_priv *alx, bool resume)
841 {
842         int err;
843
844         if (!resume)
845                 netif_carrier_off(alx->dev);
846
847         err = alx_alloc_rings(alx);
848         if (err)
849                 return err;
850
851         alx_configure(alx);
852
853         err = alx_request_irq(alx);
854         if (err)
855                 goto out_free_rings;
856
857         /* clear old interrupts */
858         alx_write_mem32(&alx->hw, ALX_ISR, ~(u32)ALX_ISR_DIS);
859
860         alx_irq_enable(alx);
861
862         if (!resume)
863                 netif_tx_start_all_queues(alx->dev);
864
865         alx_schedule_link_check(alx);
866         return 0;
867
868 out_free_rings:
869         alx_free_rings(alx);
870         return err;
871 }
872
873 static void __alx_stop(struct alx_priv *alx)
874 {
875         alx_halt(alx);
876         alx_free_irq(alx);
877         alx_free_rings(alx);
878 }
879
880 static const char *alx_speed_desc(struct alx_hw *hw)
881 {
882         switch (alx_speed_to_ethadv(hw->link_speed, hw->duplex)) {
883         case ADVERTISED_1000baseT_Full:
884                 return "1 Gbps Full";
885         case ADVERTISED_100baseT_Full:
886                 return "100 Mbps Full";
887         case ADVERTISED_100baseT_Half:
888                 return "100 Mbps Half";
889         case ADVERTISED_10baseT_Full:
890                 return "10 Mbps Full";
891         case ADVERTISED_10baseT_Half:
892                 return "10 Mbps Half";
893         default:
894                 return "Unknown speed";
895         }
896 }
897
898 static void alx_check_link(struct alx_priv *alx)
899 {
900         struct alx_hw *hw = &alx->hw;
901         unsigned long flags;
902         int old_speed;
903         u8 old_duplex;
904         int err;
905
906         /* clear PHY internal interrupt status, otherwise the main
907          * interrupt status will be asserted forever
908          */
909         alx_clear_phy_intr(hw);
910
911         old_speed = hw->link_speed;
912         old_duplex = hw->duplex;
913         err = alx_read_phy_link(hw);
914         if (err < 0)
915                 goto reset;
916
917         spin_lock_irqsave(&alx->irq_lock, flags);
918         alx->int_mask |= ALX_ISR_PHY;
919         alx_write_mem32(hw, ALX_IMR, alx->int_mask);
920         spin_unlock_irqrestore(&alx->irq_lock, flags);
921
922         if (old_speed == hw->link_speed)
923                 return;
924
925         if (hw->link_speed != SPEED_UNKNOWN) {
926                 netif_info(alx, link, alx->dev,
927                            "NIC Up: %s\n", alx_speed_desc(hw));
928                 alx_post_phy_link(hw);
929                 alx_enable_aspm(hw, true, true);
930                 alx_start_mac(hw);
931
932                 if (old_speed == SPEED_UNKNOWN)
933                         alx_netif_start(alx);
934         } else {
935                 /* link is now down */
936                 alx_netif_stop(alx);
937                 netif_info(alx, link, alx->dev, "Link Down\n");
938                 err = alx_reset_mac(hw);
939                 if (err)
940                         goto reset;
941                 alx_irq_disable(alx);
942
943                 /* MAC reset causes all HW settings to be lost, restore all */
944                 err = alx_reinit_rings(alx);
945                 if (err)
946                         goto reset;
947                 alx_configure(alx);
948                 alx_enable_aspm(hw, false, true);
949                 alx_post_phy_link(hw);
950                 alx_irq_enable(alx);
951         }
952
953         return;
954
955 reset:
956         alx_schedule_reset(alx);
957 }
958
959 static int alx_open(struct net_device *netdev)
960 {
961         return __alx_open(netdev_priv(netdev), false);
962 }
963
964 static int alx_stop(struct net_device *netdev)
965 {
966         __alx_stop(netdev_priv(netdev));
967         return 0;
968 }
969
970 static void alx_link_check(struct work_struct *work)
971 {
972         struct alx_priv *alx;
973
974         alx = container_of(work, struct alx_priv, link_check_wk);
975
976         rtnl_lock();
977         alx_check_link(alx);
978         rtnl_unlock();
979 }
980
981 static void alx_reset(struct work_struct *work)
982 {
983         struct alx_priv *alx = container_of(work, struct alx_priv, reset_wk);
984
985         rtnl_lock();
986         alx_reinit(alx);
987         rtnl_unlock();
988 }
989
990 static int alx_tx_csum(struct sk_buff *skb, struct alx_txd *first)
991 {
992         u8 cso, css;
993
994         if (skb->ip_summed != CHECKSUM_PARTIAL)
995                 return 0;
996
997         cso = skb_checksum_start_offset(skb);
998         if (cso & 1)
999                 return -EINVAL;
1000
1001         css = cso + skb->csum_offset;
1002         first->word1 |= cpu_to_le32((cso >> 1) << TPD_CXSUMSTART_SHIFT);
1003         first->word1 |= cpu_to_le32((css >> 1) << TPD_CXSUMOFFSET_SHIFT);
1004         first->word1 |= cpu_to_le32(1 << TPD_CXSUM_EN_SHIFT);
1005
1006         return 0;
1007 }
1008
1009 static int alx_map_tx_skb(struct alx_priv *alx, struct sk_buff *skb)
1010 {
1011         struct alx_tx_queue *txq = &alx->txq;
1012         struct alx_txd *tpd, *first_tpd;
1013         dma_addr_t dma;
1014         int maplen, f, first_idx = txq->write_idx;
1015
1016         first_tpd = &txq->tpd[txq->write_idx];
1017         tpd = first_tpd;
1018
1019         maplen = skb_headlen(skb);
1020         dma = dma_map_single(&alx->hw.pdev->dev, skb->data, maplen,
1021                              DMA_TO_DEVICE);
1022         if (dma_mapping_error(&alx->hw.pdev->dev, dma))
1023                 goto err_dma;
1024
1025         dma_unmap_len_set(&txq->bufs[txq->write_idx], size, maplen);
1026         dma_unmap_addr_set(&txq->bufs[txq->write_idx], dma, dma);
1027
1028         tpd->adrl.addr = cpu_to_le64(dma);
1029         tpd->len = cpu_to_le16(maplen);
1030
1031         for (f = 0; f < skb_shinfo(skb)->nr_frags; f++) {
1032                 struct skb_frag_struct *frag;
1033
1034                 frag = &skb_shinfo(skb)->frags[f];
1035
1036                 if (++txq->write_idx == alx->tx_ringsz)
1037                         txq->write_idx = 0;
1038                 tpd = &txq->tpd[txq->write_idx];
1039
1040                 tpd->word1 = first_tpd->word1;
1041
1042                 maplen = skb_frag_size(frag);
1043                 dma = skb_frag_dma_map(&alx->hw.pdev->dev, frag, 0,
1044                                        maplen, DMA_TO_DEVICE);
1045                 if (dma_mapping_error(&alx->hw.pdev->dev, dma))
1046                         goto err_dma;
1047                 dma_unmap_len_set(&txq->bufs[txq->write_idx], size, maplen);
1048                 dma_unmap_addr_set(&txq->bufs[txq->write_idx], dma, dma);
1049
1050                 tpd->adrl.addr = cpu_to_le64(dma);
1051                 tpd->len = cpu_to_le16(maplen);
1052         }
1053
1054         /* last TPD, set EOP flag and store skb */
1055         tpd->word1 |= cpu_to_le32(1 << TPD_EOP_SHIFT);
1056         txq->bufs[txq->write_idx].skb = skb;
1057
1058         if (++txq->write_idx == alx->tx_ringsz)
1059                 txq->write_idx = 0;
1060
1061         return 0;
1062
1063 err_dma:
1064         f = first_idx;
1065         while (f != txq->write_idx) {
1066                 alx_free_txbuf(alx, f);
1067                 if (++f == alx->tx_ringsz)
1068                         f = 0;
1069         }
1070         return -ENOMEM;
1071 }
1072
1073 static netdev_tx_t alx_start_xmit(struct sk_buff *skb,
1074                                   struct net_device *netdev)
1075 {
1076         struct alx_priv *alx = netdev_priv(netdev);
1077         struct alx_tx_queue *txq = &alx->txq;
1078         struct alx_txd *first;
1079         int tpdreq = skb_shinfo(skb)->nr_frags + 1;
1080
1081         if (alx_tpd_avail(alx) < tpdreq) {
1082                 netif_stop_queue(alx->dev);
1083                 goto drop;
1084         }
1085
1086         first = &txq->tpd[txq->write_idx];
1087         memset(first, 0, sizeof(*first));
1088
1089         if (alx_tx_csum(skb, first))
1090                 goto drop;
1091
1092         if (alx_map_tx_skb(alx, skb) < 0)
1093                 goto drop;
1094
1095         netdev_sent_queue(alx->dev, skb->len);
1096
1097         /* flush updates before updating hardware */
1098         wmb();
1099         alx_write_mem16(&alx->hw, ALX_TPD_PRI0_PIDX, txq->write_idx);
1100
1101         if (alx_tpd_avail(alx) < alx->tx_ringsz/8)
1102                 netif_stop_queue(alx->dev);
1103
1104         return NETDEV_TX_OK;
1105
1106 drop:
1107         dev_kfree_skb_any(skb);
1108         return NETDEV_TX_OK;
1109 }
1110
1111 static void alx_tx_timeout(struct net_device *dev)
1112 {
1113         struct alx_priv *alx = netdev_priv(dev);
1114
1115         alx_schedule_reset(alx);
1116 }
1117
1118 static int alx_mdio_read(struct net_device *netdev,
1119                          int prtad, int devad, u16 addr)
1120 {
1121         struct alx_priv *alx = netdev_priv(netdev);
1122         struct alx_hw *hw = &alx->hw;
1123         u16 val;
1124         int err;
1125
1126         if (prtad != hw->mdio.prtad)
1127                 return -EINVAL;
1128
1129         if (devad == MDIO_DEVAD_NONE)
1130                 err = alx_read_phy_reg(hw, addr, &val);
1131         else
1132                 err = alx_read_phy_ext(hw, devad, addr, &val);
1133
1134         if (err)
1135                 return err;
1136         return val;
1137 }
1138
1139 static int alx_mdio_write(struct net_device *netdev,
1140                           int prtad, int devad, u16 addr, u16 val)
1141 {
1142         struct alx_priv *alx = netdev_priv(netdev);
1143         struct alx_hw *hw = &alx->hw;
1144
1145         if (prtad != hw->mdio.prtad)
1146                 return -EINVAL;
1147
1148         if (devad == MDIO_DEVAD_NONE)
1149                 return alx_write_phy_reg(hw, addr, val);
1150
1151         return alx_write_phy_ext(hw, devad, addr, val);
1152 }
1153
1154 static int alx_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1155 {
1156         struct alx_priv *alx = netdev_priv(netdev);
1157
1158         if (!netif_running(netdev))
1159                 return -EAGAIN;
1160
1161         return mdio_mii_ioctl(&alx->hw.mdio, if_mii(ifr), cmd);
1162 }
1163
1164 #ifdef CONFIG_NET_POLL_CONTROLLER
1165 static void alx_poll_controller(struct net_device *netdev)
1166 {
1167         struct alx_priv *alx = netdev_priv(netdev);
1168
1169         if (alx->msi)
1170                 alx_intr_msi(0, alx);
1171         else
1172                 alx_intr_legacy(0, alx);
1173 }
1174 #endif
1175
1176 static struct rtnl_link_stats64 *alx_get_stats64(struct net_device *dev,
1177                                         struct rtnl_link_stats64 *net_stats)
1178 {
1179         struct alx_priv *alx = netdev_priv(dev);
1180         struct alx_hw_stats *hw_stats = &alx->hw.stats;
1181
1182         spin_lock(&alx->stats_lock);
1183
1184         alx_update_hw_stats(&alx->hw);
1185
1186         net_stats->tx_bytes   = hw_stats->tx_byte_cnt;
1187         net_stats->rx_bytes   = hw_stats->rx_byte_cnt;
1188         net_stats->multicast  = hw_stats->rx_mcast;
1189         net_stats->collisions = hw_stats->tx_single_col +
1190                                 hw_stats->tx_multi_col +
1191                                 hw_stats->tx_late_col +
1192                                 hw_stats->tx_abort_col;
1193
1194         net_stats->rx_errors  = hw_stats->rx_frag +
1195                                 hw_stats->rx_fcs_err +
1196                                 hw_stats->rx_len_err +
1197                                 hw_stats->rx_ov_sz +
1198                                 hw_stats->rx_ov_rrd +
1199                                 hw_stats->rx_align_err +
1200                                 hw_stats->rx_ov_rxf;
1201
1202         net_stats->rx_fifo_errors   = hw_stats->rx_ov_rxf;
1203         net_stats->rx_length_errors = hw_stats->rx_len_err;
1204         net_stats->rx_crc_errors    = hw_stats->rx_fcs_err;
1205         net_stats->rx_frame_errors  = hw_stats->rx_align_err;
1206         net_stats->rx_dropped       = hw_stats->rx_ov_rrd;
1207
1208         net_stats->tx_errors = hw_stats->tx_late_col +
1209                                hw_stats->tx_abort_col +
1210                                hw_stats->tx_underrun +
1211                                hw_stats->tx_trunc;
1212
1213         net_stats->tx_aborted_errors = hw_stats->tx_abort_col;
1214         net_stats->tx_fifo_errors    = hw_stats->tx_underrun;
1215         net_stats->tx_window_errors  = hw_stats->tx_late_col;
1216
1217         net_stats->tx_packets = hw_stats->tx_ok + net_stats->tx_errors;
1218         net_stats->rx_packets = hw_stats->rx_ok + net_stats->rx_errors;
1219
1220         spin_unlock(&alx->stats_lock);
1221
1222         return net_stats;
1223 }
1224
1225 static const struct net_device_ops alx_netdev_ops = {
1226         .ndo_open               = alx_open,
1227         .ndo_stop               = alx_stop,
1228         .ndo_start_xmit         = alx_start_xmit,
1229         .ndo_get_stats64        = alx_get_stats64,
1230         .ndo_set_rx_mode        = alx_set_rx_mode,
1231         .ndo_validate_addr      = eth_validate_addr,
1232         .ndo_set_mac_address    = alx_set_mac_address,
1233         .ndo_change_mtu         = alx_change_mtu,
1234         .ndo_do_ioctl           = alx_ioctl,
1235         .ndo_tx_timeout         = alx_tx_timeout,
1236         .ndo_fix_features       = alx_fix_features,
1237 #ifdef CONFIG_NET_POLL_CONTROLLER
1238         .ndo_poll_controller    = alx_poll_controller,
1239 #endif
1240 };
1241
1242 static int alx_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1243 {
1244         struct net_device *netdev;
1245         struct alx_priv *alx;
1246         struct alx_hw *hw;
1247         bool phy_configured;
1248         int bars, err;
1249
1250         err = pci_enable_device_mem(pdev);
1251         if (err)
1252                 return err;
1253
1254         /* The alx chip can DMA to 64-bit addresses, but it uses a single
1255          * shared register for the high 32 bits, so only a single, aligned,
1256          * 4 GB physical address range can be used for descriptors.
1257          */
1258         if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
1259                 dev_dbg(&pdev->dev, "DMA to 64-BIT addresses\n");
1260         } else {
1261                 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1262                 if (err) {
1263                         dev_err(&pdev->dev, "No usable DMA config, aborting\n");
1264                         goto out_pci_disable;
1265                 }
1266         }
1267
1268         bars = pci_select_bars(pdev, IORESOURCE_MEM);
1269         err = pci_request_selected_regions(pdev, bars, alx_drv_name);
1270         if (err) {
1271                 dev_err(&pdev->dev,
1272                         "pci_request_selected_regions failed(bars:%d)\n", bars);
1273                 goto out_pci_disable;
1274         }
1275
1276         pci_enable_pcie_error_reporting(pdev);
1277         pci_set_master(pdev);
1278
1279         if (!pdev->pm_cap) {
1280                 dev_err(&pdev->dev,
1281                         "Can't find power management capability, aborting\n");
1282                 err = -EIO;
1283                 goto out_pci_release;
1284         }
1285
1286         netdev = alloc_etherdev(sizeof(*alx));
1287         if (!netdev) {
1288                 err = -ENOMEM;
1289                 goto out_pci_release;
1290         }
1291
1292         SET_NETDEV_DEV(netdev, &pdev->dev);
1293         alx = netdev_priv(netdev);
1294         spin_lock_init(&alx->hw.mdio_lock);
1295         spin_lock_init(&alx->irq_lock);
1296         spin_lock_init(&alx->stats_lock);
1297         alx->dev = netdev;
1298         alx->hw.pdev = pdev;
1299         alx->msg_enable = NETIF_MSG_LINK | NETIF_MSG_HW | NETIF_MSG_IFUP |
1300                           NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR | NETIF_MSG_WOL;
1301         hw = &alx->hw;
1302         pci_set_drvdata(pdev, alx);
1303
1304         hw->hw_addr = pci_ioremap_bar(pdev, 0);
1305         if (!hw->hw_addr) {
1306                 dev_err(&pdev->dev, "cannot map device registers\n");
1307                 err = -EIO;
1308                 goto out_free_netdev;
1309         }
1310
1311         netdev->netdev_ops = &alx_netdev_ops;
1312         netdev->ethtool_ops = &alx_ethtool_ops;
1313         netdev->irq = pdev->irq;
1314         netdev->watchdog_timeo = ALX_WATCHDOG_TIME;
1315
1316         if (ent->driver_data & ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG)
1317                 pdev->dev_flags |= PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG;
1318
1319         err = alx_init_sw(alx);
1320         if (err) {
1321                 dev_err(&pdev->dev, "net device private data init failed\n");
1322                 goto out_unmap;
1323         }
1324
1325         alx_reset_pcie(hw);
1326
1327         phy_configured = alx_phy_configured(hw);
1328
1329         if (!phy_configured)
1330                 alx_reset_phy(hw);
1331
1332         err = alx_reset_mac(hw);
1333         if (err) {
1334                 dev_err(&pdev->dev, "MAC Reset failed, error = %d\n", err);
1335                 goto out_unmap;
1336         }
1337
1338         /* setup link to put it in a known good starting state */
1339         if (!phy_configured) {
1340                 err = alx_setup_speed_duplex(hw, hw->adv_cfg, hw->flowctrl);
1341                 if (err) {
1342                         dev_err(&pdev->dev,
1343                                 "failed to configure PHY speed/duplex (err=%d)\n",
1344                                 err);
1345                         goto out_unmap;
1346                 }
1347         }
1348
1349         netdev->hw_features = NETIF_F_SG | NETIF_F_HW_CSUM;
1350
1351         if (alx_get_perm_macaddr(hw, hw->perm_addr)) {
1352                 dev_warn(&pdev->dev,
1353                          "Invalid permanent address programmed, using random one\n");
1354                 eth_hw_addr_random(netdev);
1355                 memcpy(hw->perm_addr, netdev->dev_addr, netdev->addr_len);
1356         }
1357
1358         memcpy(hw->mac_addr, hw->perm_addr, ETH_ALEN);
1359         memcpy(netdev->dev_addr, hw->mac_addr, ETH_ALEN);
1360         memcpy(netdev->perm_addr, hw->perm_addr, ETH_ALEN);
1361
1362         hw->mdio.prtad = 0;
1363         hw->mdio.mmds = 0;
1364         hw->mdio.dev = netdev;
1365         hw->mdio.mode_support = MDIO_SUPPORTS_C45 |
1366                                 MDIO_SUPPORTS_C22 |
1367                                 MDIO_EMULATE_C22;
1368         hw->mdio.mdio_read = alx_mdio_read;
1369         hw->mdio.mdio_write = alx_mdio_write;
1370
1371         if (!alx_get_phy_info(hw)) {
1372                 dev_err(&pdev->dev, "failed to identify PHY\n");
1373                 err = -EIO;
1374                 goto out_unmap;
1375         }
1376
1377         INIT_WORK(&alx->link_check_wk, alx_link_check);
1378         INIT_WORK(&alx->reset_wk, alx_reset);
1379         netif_carrier_off(netdev);
1380
1381         err = register_netdev(netdev);
1382         if (err) {
1383                 dev_err(&pdev->dev, "register netdevice failed\n");
1384                 goto out_unmap;
1385         }
1386
1387         netdev_info(netdev,
1388                     "Qualcomm Atheros AR816x/AR817x Ethernet [%pM]\n",
1389                     netdev->dev_addr);
1390
1391         return 0;
1392
1393 out_unmap:
1394         iounmap(hw->hw_addr);
1395 out_free_netdev:
1396         free_netdev(netdev);
1397 out_pci_release:
1398         pci_release_selected_regions(pdev, bars);
1399 out_pci_disable:
1400         pci_disable_device(pdev);
1401         return err;
1402 }
1403
1404 static void alx_remove(struct pci_dev *pdev)
1405 {
1406         struct alx_priv *alx = pci_get_drvdata(pdev);
1407         struct alx_hw *hw = &alx->hw;
1408
1409         cancel_work_sync(&alx->link_check_wk);
1410         cancel_work_sync(&alx->reset_wk);
1411
1412         /* restore permanent mac address */
1413         alx_set_macaddr(hw, hw->perm_addr);
1414
1415         unregister_netdev(alx->dev);
1416         iounmap(hw->hw_addr);
1417         pci_release_selected_regions(pdev,
1418                                      pci_select_bars(pdev, IORESOURCE_MEM));
1419
1420         pci_disable_pcie_error_reporting(pdev);
1421         pci_disable_device(pdev);
1422
1423         free_netdev(alx->dev);
1424 }
1425
1426 #ifdef CONFIG_PM_SLEEP
1427 static int alx_suspend(struct device *dev)
1428 {
1429         struct pci_dev *pdev = to_pci_dev(dev);
1430         struct alx_priv *alx = pci_get_drvdata(pdev);
1431
1432         if (!netif_running(alx->dev))
1433                 return 0;
1434         netif_device_detach(alx->dev);
1435         __alx_stop(alx);
1436         return 0;
1437 }
1438
1439 static int alx_resume(struct device *dev)
1440 {
1441         struct pci_dev *pdev = to_pci_dev(dev);
1442         struct alx_priv *alx = pci_get_drvdata(pdev);
1443         struct alx_hw *hw = &alx->hw;
1444
1445         alx_reset_phy(hw);
1446
1447         if (!netif_running(alx->dev))
1448                 return 0;
1449         netif_device_attach(alx->dev);
1450         return __alx_open(alx, true);
1451 }
1452
1453 static SIMPLE_DEV_PM_OPS(alx_pm_ops, alx_suspend, alx_resume);
1454 #define ALX_PM_OPS      (&alx_pm_ops)
1455 #else
1456 #define ALX_PM_OPS      NULL
1457 #endif
1458
1459
1460 static pci_ers_result_t alx_pci_error_detected(struct pci_dev *pdev,
1461                                                pci_channel_state_t state)
1462 {
1463         struct alx_priv *alx = pci_get_drvdata(pdev);
1464         struct net_device *netdev = alx->dev;
1465         pci_ers_result_t rc = PCI_ERS_RESULT_NEED_RESET;
1466
1467         dev_info(&pdev->dev, "pci error detected\n");
1468
1469         rtnl_lock();
1470
1471         if (netif_running(netdev)) {
1472                 netif_device_detach(netdev);
1473                 alx_halt(alx);
1474         }
1475
1476         if (state == pci_channel_io_perm_failure)
1477                 rc = PCI_ERS_RESULT_DISCONNECT;
1478         else
1479                 pci_disable_device(pdev);
1480
1481         rtnl_unlock();
1482
1483         return rc;
1484 }
1485
1486 static pci_ers_result_t alx_pci_error_slot_reset(struct pci_dev *pdev)
1487 {
1488         struct alx_priv *alx = pci_get_drvdata(pdev);
1489         struct alx_hw *hw = &alx->hw;
1490         pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
1491
1492         dev_info(&pdev->dev, "pci error slot reset\n");
1493
1494         rtnl_lock();
1495
1496         if (pci_enable_device(pdev)) {
1497                 dev_err(&pdev->dev, "Failed to re-enable PCI device after reset\n");
1498                 goto out;
1499         }
1500
1501         pci_set_master(pdev);
1502
1503         alx_reset_pcie(hw);
1504         if (!alx_reset_mac(hw))
1505                 rc = PCI_ERS_RESULT_RECOVERED;
1506 out:
1507         pci_cleanup_aer_uncorrect_error_status(pdev);
1508
1509         rtnl_unlock();
1510
1511         return rc;
1512 }
1513
1514 static void alx_pci_error_resume(struct pci_dev *pdev)
1515 {
1516         struct alx_priv *alx = pci_get_drvdata(pdev);
1517         struct net_device *netdev = alx->dev;
1518
1519         dev_info(&pdev->dev, "pci error resume\n");
1520
1521         rtnl_lock();
1522
1523         if (netif_running(netdev)) {
1524                 alx_activate(alx);
1525                 netif_device_attach(netdev);
1526         }
1527
1528         rtnl_unlock();
1529 }
1530
1531 static const struct pci_error_handlers alx_err_handlers = {
1532         .error_detected = alx_pci_error_detected,
1533         .slot_reset     = alx_pci_error_slot_reset,
1534         .resume         = alx_pci_error_resume,
1535 };
1536
1537 static const struct pci_device_id alx_pci_tbl[] = {
1538         { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_AR8161),
1539           .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
1540         { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_E2200),
1541           .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
1542         { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_E2400),
1543           .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
1544         { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_AR8162),
1545           .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
1546         { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_AR8171) },
1547         { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_AR8172) },
1548         {}
1549 };
1550
1551 static struct pci_driver alx_driver = {
1552         .name        = alx_drv_name,
1553         .id_table    = alx_pci_tbl,
1554         .probe       = alx_probe,
1555         .remove      = alx_remove,
1556         .err_handler = &alx_err_handlers,
1557         .driver.pm   = ALX_PM_OPS,
1558 };
1559
1560 module_pci_driver(alx_driver);
1561 MODULE_DEVICE_TABLE(pci, alx_pci_tbl);
1562 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
1563 MODULE_AUTHOR("Qualcomm Corporation, <nic-devel@qualcomm.com>");
1564 MODULE_DESCRIPTION(
1565         "Qualcomm Atheros(R) AR816x/AR817x PCI-E Ethernet Network Driver");
1566 MODULE_LICENSE("GPL");