main.c 37 KB

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