main.c 47 KB

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