main.c 35 KB

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