main.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  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, 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. if (!pdev->pm_cap) {
  989. dev_err(&pdev->dev,
  990. "Can't find power management capability, aborting\n");
  991. err = -EIO;
  992. goto out_pci_release;
  993. }
  994. netdev = alloc_etherdev(sizeof(*alx));
  995. if (!netdev) {
  996. err = -ENOMEM;
  997. goto out_pci_release;
  998. }
  999. SET_NETDEV_DEV(netdev, &pdev->dev);
  1000. alx = netdev_priv(netdev);
  1001. spin_lock_init(&alx->hw.mdio_lock);
  1002. spin_lock_init(&alx->irq_lock);
  1003. alx->dev = netdev;
  1004. alx->hw.pdev = pdev;
  1005. alx->msg_enable = NETIF_MSG_LINK | NETIF_MSG_HW | NETIF_MSG_IFUP |
  1006. NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR | NETIF_MSG_WOL;
  1007. hw = &alx->hw;
  1008. pci_set_drvdata(pdev, alx);
  1009. hw->hw_addr = pci_ioremap_bar(pdev, 0);
  1010. if (!hw->hw_addr) {
  1011. dev_err(&pdev->dev, "cannot map device registers\n");
  1012. err = -EIO;
  1013. goto out_free_netdev;
  1014. }
  1015. netdev->netdev_ops = &alx_netdev_ops;
  1016. SET_ETHTOOL_OPS(netdev, &alx_ethtool_ops);
  1017. netdev->irq = pdev->irq;
  1018. netdev->watchdog_timeo = ALX_WATCHDOG_TIME;
  1019. if (ent->driver_data & ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG)
  1020. pdev->dev_flags |= PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG;
  1021. err = alx_init_sw(alx);
  1022. if (err) {
  1023. dev_err(&pdev->dev, "net device private data init failed\n");
  1024. goto out_unmap;
  1025. }
  1026. alx_reset_pcie(hw);
  1027. phy_configured = alx_phy_configured(hw);
  1028. if (!phy_configured)
  1029. alx_reset_phy(hw);
  1030. err = alx_reset_mac(hw);
  1031. if (err) {
  1032. dev_err(&pdev->dev, "MAC Reset failed, error = %d\n", err);
  1033. goto out_unmap;
  1034. }
  1035. /* setup link to put it in a known good starting state */
  1036. if (!phy_configured) {
  1037. err = alx_setup_speed_duplex(hw, hw->adv_cfg, hw->flowctrl);
  1038. if (err) {
  1039. dev_err(&pdev->dev,
  1040. "failed to configure PHY speed/duplex (err=%d)\n",
  1041. err);
  1042. goto out_unmap;
  1043. }
  1044. }
  1045. netdev->hw_features = NETIF_F_SG | NETIF_F_HW_CSUM;
  1046. if (alx_get_perm_macaddr(hw, hw->perm_addr)) {
  1047. dev_warn(&pdev->dev,
  1048. "Invalid permanent address programmed, using random one\n");
  1049. eth_hw_addr_random(netdev);
  1050. memcpy(hw->perm_addr, netdev->dev_addr, netdev->addr_len);
  1051. }
  1052. memcpy(hw->mac_addr, hw->perm_addr, ETH_ALEN);
  1053. memcpy(netdev->dev_addr, hw->mac_addr, ETH_ALEN);
  1054. memcpy(netdev->perm_addr, hw->perm_addr, ETH_ALEN);
  1055. hw->mdio.prtad = 0;
  1056. hw->mdio.mmds = 0;
  1057. hw->mdio.dev = netdev;
  1058. hw->mdio.mode_support = MDIO_SUPPORTS_C45 |
  1059. MDIO_SUPPORTS_C22 |
  1060. MDIO_EMULATE_C22;
  1061. hw->mdio.mdio_read = alx_mdio_read;
  1062. hw->mdio.mdio_write = alx_mdio_write;
  1063. if (!alx_get_phy_info(hw)) {
  1064. dev_err(&pdev->dev, "failed to identify PHY\n");
  1065. err = -EIO;
  1066. goto out_unmap;
  1067. }
  1068. INIT_WORK(&alx->link_check_wk, alx_link_check);
  1069. INIT_WORK(&alx->reset_wk, alx_reset);
  1070. netif_carrier_off(netdev);
  1071. err = register_netdev(netdev);
  1072. if (err) {
  1073. dev_err(&pdev->dev, "register netdevice failed\n");
  1074. goto out_unmap;
  1075. }
  1076. netdev_info(netdev,
  1077. "Qualcomm Atheros AR816x/AR817x Ethernet [%pM]\n",
  1078. netdev->dev_addr);
  1079. return 0;
  1080. out_unmap:
  1081. iounmap(hw->hw_addr);
  1082. out_free_netdev:
  1083. free_netdev(netdev);
  1084. out_pci_release:
  1085. pci_release_selected_regions(pdev, bars);
  1086. out_pci_disable:
  1087. pci_disable_device(pdev);
  1088. return err;
  1089. }
  1090. static void alx_remove(struct pci_dev *pdev)
  1091. {
  1092. struct alx_priv *alx = pci_get_drvdata(pdev);
  1093. struct alx_hw *hw = &alx->hw;
  1094. cancel_work_sync(&alx->link_check_wk);
  1095. cancel_work_sync(&alx->reset_wk);
  1096. /* restore permanent mac address */
  1097. alx_set_macaddr(hw, hw->perm_addr);
  1098. unregister_netdev(alx->dev);
  1099. iounmap(hw->hw_addr);
  1100. pci_release_selected_regions(pdev,
  1101. pci_select_bars(pdev, IORESOURCE_MEM));
  1102. pci_disable_pcie_error_reporting(pdev);
  1103. pci_disable_device(pdev);
  1104. pci_set_drvdata(pdev, NULL);
  1105. free_netdev(alx->dev);
  1106. }
  1107. #ifdef CONFIG_PM_SLEEP
  1108. static int alx_suspend(struct device *dev)
  1109. {
  1110. struct pci_dev *pdev = to_pci_dev(dev);
  1111. struct alx_priv *alx = pci_get_drvdata(pdev);
  1112. if (!netif_running(alx->dev))
  1113. return 0;
  1114. netif_device_detach(alx->dev);
  1115. __alx_stop(alx);
  1116. return 0;
  1117. }
  1118. static int alx_resume(struct device *dev)
  1119. {
  1120. struct pci_dev *pdev = to_pci_dev(dev);
  1121. struct alx_priv *alx = pci_get_drvdata(pdev);
  1122. if (!netif_running(alx->dev))
  1123. return 0;
  1124. netif_device_attach(alx->dev);
  1125. return __alx_open(alx, true);
  1126. }
  1127. static SIMPLE_DEV_PM_OPS(alx_pm_ops, alx_suspend, alx_resume);
  1128. #define ALX_PM_OPS (&alx_pm_ops)
  1129. #else
  1130. #define ALX_PM_OPS NULL
  1131. #endif
  1132. static pci_ers_result_t alx_pci_error_detected(struct pci_dev *pdev,
  1133. pci_channel_state_t state)
  1134. {
  1135. struct alx_priv *alx = pci_get_drvdata(pdev);
  1136. struct net_device *netdev = alx->dev;
  1137. pci_ers_result_t rc = PCI_ERS_RESULT_NEED_RESET;
  1138. dev_info(&pdev->dev, "pci error detected\n");
  1139. rtnl_lock();
  1140. if (netif_running(netdev)) {
  1141. netif_device_detach(netdev);
  1142. alx_halt(alx);
  1143. }
  1144. if (state == pci_channel_io_perm_failure)
  1145. rc = PCI_ERS_RESULT_DISCONNECT;
  1146. else
  1147. pci_disable_device(pdev);
  1148. rtnl_unlock();
  1149. return rc;
  1150. }
  1151. static pci_ers_result_t alx_pci_error_slot_reset(struct pci_dev *pdev)
  1152. {
  1153. struct alx_priv *alx = pci_get_drvdata(pdev);
  1154. struct alx_hw *hw = &alx->hw;
  1155. pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
  1156. dev_info(&pdev->dev, "pci error slot reset\n");
  1157. rtnl_lock();
  1158. if (pci_enable_device(pdev)) {
  1159. dev_err(&pdev->dev, "Failed to re-enable PCI device after reset\n");
  1160. goto out;
  1161. }
  1162. pci_set_master(pdev);
  1163. alx_reset_pcie(hw);
  1164. if (!alx_reset_mac(hw))
  1165. rc = PCI_ERS_RESULT_RECOVERED;
  1166. out:
  1167. pci_cleanup_aer_uncorrect_error_status(pdev);
  1168. rtnl_unlock();
  1169. return rc;
  1170. }
  1171. static void alx_pci_error_resume(struct pci_dev *pdev)
  1172. {
  1173. struct alx_priv *alx = pci_get_drvdata(pdev);
  1174. struct net_device *netdev = alx->dev;
  1175. dev_info(&pdev->dev, "pci error resume\n");
  1176. rtnl_lock();
  1177. if (netif_running(netdev)) {
  1178. alx_activate(alx);
  1179. netif_device_attach(netdev);
  1180. }
  1181. rtnl_unlock();
  1182. }
  1183. static const struct pci_error_handlers alx_err_handlers = {
  1184. .error_detected = alx_pci_error_detected,
  1185. .slot_reset = alx_pci_error_slot_reset,
  1186. .resume = alx_pci_error_resume,
  1187. };
  1188. static DEFINE_PCI_DEVICE_TABLE(alx_pci_tbl) = {
  1189. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_AR8161),
  1190. .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
  1191. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_E2200),
  1192. .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
  1193. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_AR8162),
  1194. .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
  1195. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_AR8171) },
  1196. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_AR8172) },
  1197. {}
  1198. };
  1199. static struct pci_driver alx_driver = {
  1200. .name = alx_drv_name,
  1201. .id_table = alx_pci_tbl,
  1202. .probe = alx_probe,
  1203. .remove = alx_remove,
  1204. .err_handler = &alx_err_handlers,
  1205. .driver.pm = ALX_PM_OPS,
  1206. };
  1207. module_pci_driver(alx_driver);
  1208. MODULE_DEVICE_TABLE(pci, alx_pci_tbl);
  1209. MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
  1210. MODULE_AUTHOR("Qualcomm Corporation, <nic-devel@qualcomm.com>");
  1211. MODULE_DESCRIPTION(
  1212. "Qualcomm Atheros(R) AR816x/AR817x PCI-E Ethernet Network Driver");
  1213. MODULE_LICENSE("GPL");