main.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044
  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->flags & ALX_FLAG_USING_MSIX) {
  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->flags & ALX_FLAG_USING_MSIX) {
  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 bool alx_enable_msix(struct alx_priv *alx)
  675. {
  676. int i, 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. alx->msix_entries = kcalloc(num_vec, sizeof(struct msix_entry),
  681. GFP_KERNEL);
  682. if (!alx->msix_entries) {
  683. netdev_warn(alx->dev, "Allocation of msix entries failed!\n");
  684. return false;
  685. }
  686. for (i = 0; i < num_vec; i++)
  687. alx->msix_entries[i].entry = i;
  688. err = pci_enable_msix(alx->hw.pdev, alx->msix_entries, num_vec);
  689. if (err) {
  690. kfree(alx->msix_entries);
  691. netdev_warn(alx->dev, "Enabling MSI-X interrupts failed!\n");
  692. return false;
  693. }
  694. alx->num_vec = num_vec;
  695. alx->num_napi = num_vec - 1;
  696. alx->num_txq = num_txq;
  697. alx->num_rxq = num_rxq;
  698. return true;
  699. }
  700. static int alx_request_msix(struct alx_priv *alx)
  701. {
  702. struct net_device *netdev = alx->dev;
  703. int i, err, vector = 0, free_vector = 0;
  704. err = request_irq(alx->msix_entries[0].vector, alx_intr_msix_misc,
  705. 0, netdev->name, alx);
  706. if (err)
  707. goto out_err;
  708. for (i = 0; i < alx->num_napi; i++) {
  709. struct alx_napi *np = alx->qnapi[i];
  710. vector++;
  711. if (np->txq && np->rxq)
  712. sprintf(np->irq_lbl, "%s-TxRx-%u", netdev->name,
  713. np->txq->queue_idx);
  714. else if (np->txq)
  715. sprintf(np->irq_lbl, "%s-tx-%u", netdev->name,
  716. np->txq->queue_idx);
  717. else if (np->rxq)
  718. sprintf(np->irq_lbl, "%s-rx-%u", netdev->name,
  719. np->rxq->queue_idx);
  720. else
  721. sprintf(np->irq_lbl, "%s-unused", netdev->name);
  722. np->vec_idx = vector;
  723. err = request_irq(alx->msix_entries[vector].vector,
  724. alx_intr_msix_ring, 0, np->irq_lbl, np);
  725. if (err)
  726. goto out_free;
  727. }
  728. return 0;
  729. out_free:
  730. free_irq(alx->msix_entries[free_vector++].vector, alx);
  731. vector--;
  732. for (i = 0; i < vector; i++)
  733. free_irq(alx->msix_entries[free_vector++].vector,
  734. alx->qnapi[i]);
  735. out_err:
  736. return err;
  737. }
  738. static void alx_init_intr(struct alx_priv *alx, bool msix)
  739. {
  740. if (msix) {
  741. if (alx_enable_msix(alx))
  742. alx->flags |= ALX_FLAG_USING_MSIX;
  743. }
  744. if (!(alx->flags & ALX_FLAG_USING_MSIX)) {
  745. alx->num_vec = 1;
  746. alx->num_napi = 1;
  747. alx->num_txq = 1;
  748. alx->num_rxq = 1;
  749. if (!pci_enable_msi(alx->hw.pdev))
  750. alx->flags |= ALX_FLAG_USING_MSI;
  751. }
  752. }
  753. static void alx_disable_advanced_intr(struct alx_priv *alx)
  754. {
  755. if (alx->flags & ALX_FLAG_USING_MSIX) {
  756. kfree(alx->msix_entries);
  757. pci_disable_msix(alx->hw.pdev);
  758. alx->flags &= ~ALX_FLAG_USING_MSIX;
  759. }
  760. if (alx->flags & ALX_FLAG_USING_MSI) {
  761. pci_disable_msi(alx->hw.pdev);
  762. alx->flags &= ~ALX_FLAG_USING_MSI;
  763. }
  764. }
  765. static void alx_irq_enable(struct alx_priv *alx)
  766. {
  767. struct alx_hw *hw = &alx->hw;
  768. int i;
  769. /* level-1 interrupt switch */
  770. alx_write_mem32(hw, ALX_ISR, 0);
  771. alx_write_mem32(hw, ALX_IMR, alx->int_mask);
  772. alx_post_write(hw);
  773. if (alx->flags & ALX_FLAG_USING_MSIX)
  774. /* enable all msix irqs */
  775. for (i = 0; i < alx->num_vec; i++)
  776. alx_mask_msix(hw, i, false);
  777. }
  778. static void alx_irq_disable(struct alx_priv *alx)
  779. {
  780. struct alx_hw *hw = &alx->hw;
  781. int i;
  782. alx_write_mem32(hw, ALX_ISR, ALX_ISR_DIS);
  783. alx_write_mem32(hw, ALX_IMR, 0);
  784. alx_post_write(hw);
  785. if (alx->flags & ALX_FLAG_USING_MSIX) {
  786. for (i = 0; i < alx->num_vec; i++) {
  787. alx_mask_msix(hw, i, true);
  788. synchronize_irq(alx->msix_entries[i].vector);
  789. }
  790. } else {
  791. synchronize_irq(alx->hw.pdev->irq);
  792. }
  793. }
  794. static int alx_realloc_resources(struct alx_priv *alx)
  795. {
  796. int err;
  797. alx_free_rings(alx);
  798. alx_free_napis(alx);
  799. alx_disable_advanced_intr(alx);
  800. alx_init_intr(alx, false);
  801. err = alx_alloc_napis(alx);
  802. if (err)
  803. return err;
  804. err = alx_alloc_rings(alx);
  805. if (err)
  806. return err;
  807. return 0;
  808. }
  809. static int alx_request_irq(struct alx_priv *alx)
  810. {
  811. struct pci_dev *pdev = alx->hw.pdev;
  812. struct alx_hw *hw = &alx->hw;
  813. int err;
  814. u32 msi_ctrl;
  815. msi_ctrl = (hw->imt >> 1) << ALX_MSI_RETRANS_TM_SHIFT;
  816. if (alx->flags & ALX_FLAG_USING_MSIX) {
  817. alx_write_mem32(hw, ALX_MSI_RETRANS_TIMER, msi_ctrl);
  818. err = alx_request_msix(alx);
  819. if (!err)
  820. goto out;
  821. /* msix request failed, realloc resources */
  822. err = alx_realloc_resources(alx);
  823. if (err)
  824. goto out;
  825. }
  826. if (alx->flags & ALX_FLAG_USING_MSI) {
  827. alx_write_mem32(hw, ALX_MSI_RETRANS_TIMER,
  828. msi_ctrl | ALX_MSI_MASK_SEL_LINE);
  829. err = request_irq(pdev->irq, alx_intr_msi, 0,
  830. alx->dev->name, alx);
  831. if (!err)
  832. goto out;
  833. /* fall back to legacy interrupt */
  834. alx->flags &= ~ALX_FLAG_USING_MSI;
  835. pci_disable_msi(alx->hw.pdev);
  836. }
  837. alx_write_mem32(hw, ALX_MSI_RETRANS_TIMER, 0);
  838. err = request_irq(pdev->irq, alx_intr_legacy, IRQF_SHARED,
  839. alx->dev->name, alx);
  840. out:
  841. if (!err)
  842. alx_config_vector_mapping(alx);
  843. else
  844. netdev_err(alx->dev, "IRQ registration failed!\n");
  845. return err;
  846. }
  847. static void alx_free_irq(struct alx_priv *alx)
  848. {
  849. struct pci_dev *pdev = alx->hw.pdev;
  850. int i, vector = 0;
  851. if (alx->flags & ALX_FLAG_USING_MSIX) {
  852. free_irq(alx->msix_entries[vector++].vector, alx);
  853. for (i = 0; i < alx->num_napi; i++)
  854. free_irq(alx->msix_entries[vector++].vector,
  855. alx->qnapi[i]);
  856. } else {
  857. free_irq(pdev->irq, alx);
  858. }
  859. alx_disable_advanced_intr(alx);
  860. }
  861. static int alx_identify_hw(struct alx_priv *alx)
  862. {
  863. struct alx_hw *hw = &alx->hw;
  864. int rev = alx_hw_revision(hw);
  865. if (rev > ALX_REV_C0)
  866. return -EINVAL;
  867. hw->max_dma_chnl = rev >= ALX_REV_B0 ? 4 : 2;
  868. return 0;
  869. }
  870. static int alx_init_sw(struct alx_priv *alx)
  871. {
  872. struct pci_dev *pdev = alx->hw.pdev;
  873. struct alx_hw *hw = &alx->hw;
  874. int err;
  875. err = alx_identify_hw(alx);
  876. if (err) {
  877. dev_err(&pdev->dev, "unrecognized chip, aborting\n");
  878. return err;
  879. }
  880. alx->hw.lnk_patch =
  881. pdev->device == ALX_DEV_ID_AR8161 &&
  882. pdev->subsystem_vendor == PCI_VENDOR_ID_ATTANSIC &&
  883. pdev->subsystem_device == 0x0091 &&
  884. pdev->revision == 0;
  885. hw->smb_timer = 400;
  886. hw->mtu = alx->dev->mtu;
  887. alx->rxbuf_size = ALX_MAX_FRAME_LEN(hw->mtu);
  888. /* MTU range: 34 - 9256 */
  889. alx->dev->min_mtu = 34;
  890. alx->dev->max_mtu = ALX_MAX_FRAME_LEN(ALX_MAX_FRAME_SIZE);
  891. alx->tx_ringsz = 256;
  892. alx->rx_ringsz = 512;
  893. hw->imt = 200;
  894. alx->int_mask = ALX_ISR_MISC;
  895. hw->dma_chnl = hw->max_dma_chnl;
  896. hw->ith_tpd = alx->tx_ringsz / 3;
  897. hw->link_speed = SPEED_UNKNOWN;
  898. hw->duplex = DUPLEX_UNKNOWN;
  899. hw->adv_cfg = ADVERTISED_Autoneg |
  900. ADVERTISED_10baseT_Half |
  901. ADVERTISED_10baseT_Full |
  902. ADVERTISED_100baseT_Full |
  903. ADVERTISED_100baseT_Half |
  904. ADVERTISED_1000baseT_Full;
  905. hw->flowctrl = ALX_FC_ANEG | ALX_FC_RX | ALX_FC_TX;
  906. hw->rx_ctrl = ALX_MAC_CTRL_WOLSPED_SWEN |
  907. ALX_MAC_CTRL_MHASH_ALG_HI5B |
  908. ALX_MAC_CTRL_BRD_EN |
  909. ALX_MAC_CTRL_PCRCE |
  910. ALX_MAC_CTRL_CRCE |
  911. ALX_MAC_CTRL_RXFC_EN |
  912. ALX_MAC_CTRL_TXFC_EN |
  913. 7 << ALX_MAC_CTRL_PRMBLEN_SHIFT;
  914. return err;
  915. }
  916. static netdev_features_t alx_fix_features(struct net_device *netdev,
  917. netdev_features_t features)
  918. {
  919. if (netdev->mtu > ALX_MAX_TSO_PKT_SIZE)
  920. features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
  921. return features;
  922. }
  923. static void alx_netif_stop(struct alx_priv *alx)
  924. {
  925. int i;
  926. netif_trans_update(alx->dev);
  927. if (netif_carrier_ok(alx->dev)) {
  928. netif_carrier_off(alx->dev);
  929. netif_tx_disable(alx->dev);
  930. for (i = 0; i < alx->num_napi; i++)
  931. napi_disable(&alx->qnapi[i]->napi);
  932. }
  933. }
  934. static void alx_halt(struct alx_priv *alx)
  935. {
  936. struct alx_hw *hw = &alx->hw;
  937. alx_netif_stop(alx);
  938. hw->link_speed = SPEED_UNKNOWN;
  939. hw->duplex = DUPLEX_UNKNOWN;
  940. alx_reset_mac(hw);
  941. /* disable l0s/l1 */
  942. alx_enable_aspm(hw, false, false);
  943. alx_irq_disable(alx);
  944. alx_free_buffers(alx);
  945. }
  946. static void alx_configure(struct alx_priv *alx)
  947. {
  948. struct alx_hw *hw = &alx->hw;
  949. alx_configure_basic(hw);
  950. alx_disable_rss(hw);
  951. __alx_set_rx_mode(alx->dev);
  952. alx_write_mem32(hw, ALX_MAC_CTRL, hw->rx_ctrl);
  953. }
  954. static void alx_activate(struct alx_priv *alx)
  955. {
  956. /* hardware setting lost, restore it */
  957. alx_reinit_rings(alx);
  958. alx_configure(alx);
  959. /* clear old interrupts */
  960. alx_write_mem32(&alx->hw, ALX_ISR, ~(u32)ALX_ISR_DIS);
  961. alx_irq_enable(alx);
  962. alx_schedule_link_check(alx);
  963. }
  964. static void alx_reinit(struct alx_priv *alx)
  965. {
  966. ASSERT_RTNL();
  967. alx_halt(alx);
  968. alx_activate(alx);
  969. }
  970. static int alx_change_mtu(struct net_device *netdev, int mtu)
  971. {
  972. struct alx_priv *alx = netdev_priv(netdev);
  973. int max_frame = ALX_MAX_FRAME_LEN(mtu);
  974. netdev->mtu = mtu;
  975. alx->hw.mtu = mtu;
  976. alx->rxbuf_size = max(max_frame, ALX_DEF_RXBUF_SIZE);
  977. netdev_update_features(netdev);
  978. if (netif_running(netdev))
  979. alx_reinit(alx);
  980. return 0;
  981. }
  982. static void alx_netif_start(struct alx_priv *alx)
  983. {
  984. int i;
  985. netif_tx_wake_all_queues(alx->dev);
  986. for (i = 0; i < alx->num_napi; i++)
  987. napi_enable(&alx->qnapi[i]->napi);
  988. netif_carrier_on(alx->dev);
  989. }
  990. static int __alx_open(struct alx_priv *alx, bool resume)
  991. {
  992. int err;
  993. alx_init_intr(alx, true);
  994. if (!resume)
  995. netif_carrier_off(alx->dev);
  996. err = alx_alloc_napis(alx);
  997. if (err)
  998. goto out_disable_adv_intr;
  999. err = alx_alloc_rings(alx);
  1000. if (err)
  1001. goto out_free_rings;
  1002. alx_configure(alx);
  1003. err = alx_request_irq(alx);
  1004. if (err)
  1005. goto out_free_rings;
  1006. /* must be called after alx_request_irq because the chip stops working
  1007. * if we copy the dma addresses in alx_init_ring_ptrs twice when
  1008. * requesting msi-x interrupts failed
  1009. */
  1010. alx_reinit_rings(alx);
  1011. netif_set_real_num_tx_queues(alx->dev, alx->num_txq);
  1012. netif_set_real_num_rx_queues(alx->dev, alx->num_rxq);
  1013. /* clear old interrupts */
  1014. alx_write_mem32(&alx->hw, ALX_ISR, ~(u32)ALX_ISR_DIS);
  1015. alx_irq_enable(alx);
  1016. if (!resume)
  1017. netif_tx_start_all_queues(alx->dev);
  1018. alx_schedule_link_check(alx);
  1019. return 0;
  1020. out_free_rings:
  1021. alx_free_rings(alx);
  1022. alx_free_napis(alx);
  1023. out_disable_adv_intr:
  1024. alx_disable_advanced_intr(alx);
  1025. return err;
  1026. }
  1027. static void __alx_stop(struct alx_priv *alx)
  1028. {
  1029. alx_halt(alx);
  1030. alx_free_irq(alx);
  1031. alx_free_rings(alx);
  1032. alx_free_napis(alx);
  1033. }
  1034. static const char *alx_speed_desc(struct alx_hw *hw)
  1035. {
  1036. switch (alx_speed_to_ethadv(hw->link_speed, hw->duplex)) {
  1037. case ADVERTISED_1000baseT_Full:
  1038. return "1 Gbps Full";
  1039. case ADVERTISED_100baseT_Full:
  1040. return "100 Mbps Full";
  1041. case ADVERTISED_100baseT_Half:
  1042. return "100 Mbps Half";
  1043. case ADVERTISED_10baseT_Full:
  1044. return "10 Mbps Full";
  1045. case ADVERTISED_10baseT_Half:
  1046. return "10 Mbps Half";
  1047. default:
  1048. return "Unknown speed";
  1049. }
  1050. }
  1051. static void alx_check_link(struct alx_priv *alx)
  1052. {
  1053. struct alx_hw *hw = &alx->hw;
  1054. unsigned long flags;
  1055. int old_speed;
  1056. u8 old_duplex;
  1057. int err;
  1058. /* clear PHY internal interrupt status, otherwise the main
  1059. * interrupt status will be asserted forever
  1060. */
  1061. alx_clear_phy_intr(hw);
  1062. old_speed = hw->link_speed;
  1063. old_duplex = hw->duplex;
  1064. err = alx_read_phy_link(hw);
  1065. if (err < 0)
  1066. goto reset;
  1067. spin_lock_irqsave(&alx->irq_lock, flags);
  1068. alx->int_mask |= ALX_ISR_PHY;
  1069. alx_write_mem32(hw, ALX_IMR, alx->int_mask);
  1070. spin_unlock_irqrestore(&alx->irq_lock, flags);
  1071. if (old_speed == hw->link_speed)
  1072. return;
  1073. if (hw->link_speed != SPEED_UNKNOWN) {
  1074. netif_info(alx, link, alx->dev,
  1075. "NIC Up: %s\n", alx_speed_desc(hw));
  1076. alx_post_phy_link(hw);
  1077. alx_enable_aspm(hw, true, true);
  1078. alx_start_mac(hw);
  1079. if (old_speed == SPEED_UNKNOWN)
  1080. alx_netif_start(alx);
  1081. } else {
  1082. /* link is now down */
  1083. alx_netif_stop(alx);
  1084. netif_info(alx, link, alx->dev, "Link Down\n");
  1085. err = alx_reset_mac(hw);
  1086. if (err)
  1087. goto reset;
  1088. alx_irq_disable(alx);
  1089. /* MAC reset causes all HW settings to be lost, restore all */
  1090. err = alx_reinit_rings(alx);
  1091. if (err)
  1092. goto reset;
  1093. alx_configure(alx);
  1094. alx_enable_aspm(hw, false, true);
  1095. alx_post_phy_link(hw);
  1096. alx_irq_enable(alx);
  1097. }
  1098. return;
  1099. reset:
  1100. alx_schedule_reset(alx);
  1101. }
  1102. static int alx_open(struct net_device *netdev)
  1103. {
  1104. return __alx_open(netdev_priv(netdev), false);
  1105. }
  1106. static int alx_stop(struct net_device *netdev)
  1107. {
  1108. __alx_stop(netdev_priv(netdev));
  1109. return 0;
  1110. }
  1111. static void alx_link_check(struct work_struct *work)
  1112. {
  1113. struct alx_priv *alx;
  1114. alx = container_of(work, struct alx_priv, link_check_wk);
  1115. rtnl_lock();
  1116. alx_check_link(alx);
  1117. rtnl_unlock();
  1118. }
  1119. static void alx_reset(struct work_struct *work)
  1120. {
  1121. struct alx_priv *alx = container_of(work, struct alx_priv, reset_wk);
  1122. rtnl_lock();
  1123. alx_reinit(alx);
  1124. rtnl_unlock();
  1125. }
  1126. static int alx_tpd_req(struct sk_buff *skb)
  1127. {
  1128. int num;
  1129. num = skb_shinfo(skb)->nr_frags + 1;
  1130. /* we need one extra descriptor for LSOv2 */
  1131. if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  1132. num++;
  1133. return num;
  1134. }
  1135. static int alx_tx_csum(struct sk_buff *skb, struct alx_txd *first)
  1136. {
  1137. u8 cso, css;
  1138. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1139. return 0;
  1140. cso = skb_checksum_start_offset(skb);
  1141. if (cso & 1)
  1142. return -EINVAL;
  1143. css = cso + skb->csum_offset;
  1144. first->word1 |= cpu_to_le32((cso >> 1) << TPD_CXSUMSTART_SHIFT);
  1145. first->word1 |= cpu_to_le32((css >> 1) << TPD_CXSUMOFFSET_SHIFT);
  1146. first->word1 |= cpu_to_le32(1 << TPD_CXSUM_EN_SHIFT);
  1147. return 0;
  1148. }
  1149. static int alx_tso(struct sk_buff *skb, struct alx_txd *first)
  1150. {
  1151. int err;
  1152. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1153. return 0;
  1154. if (!skb_is_gso(skb))
  1155. return 0;
  1156. err = skb_cow_head(skb, 0);
  1157. if (err < 0)
  1158. return err;
  1159. if (skb->protocol == htons(ETH_P_IP)) {
  1160. struct iphdr *iph = ip_hdr(skb);
  1161. iph->check = 0;
  1162. tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  1163. 0, IPPROTO_TCP, 0);
  1164. first->word1 |= 1 << TPD_IPV4_SHIFT;
  1165. } else if (skb_is_gso_v6(skb)) {
  1166. ipv6_hdr(skb)->payload_len = 0;
  1167. tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  1168. &ipv6_hdr(skb)->daddr,
  1169. 0, IPPROTO_TCP, 0);
  1170. /* LSOv2: the first TPD only provides the packet length */
  1171. first->adrl.l.pkt_len = skb->len;
  1172. first->word1 |= 1 << TPD_LSO_V2_SHIFT;
  1173. }
  1174. first->word1 |= 1 << TPD_LSO_EN_SHIFT;
  1175. first->word1 |= (skb_transport_offset(skb) &
  1176. TPD_L4HDROFFSET_MASK) << TPD_L4HDROFFSET_SHIFT;
  1177. first->word1 |= (skb_shinfo(skb)->gso_size &
  1178. TPD_MSS_MASK) << TPD_MSS_SHIFT;
  1179. return 1;
  1180. }
  1181. static int alx_map_tx_skb(struct alx_tx_queue *txq, struct sk_buff *skb)
  1182. {
  1183. struct alx_txd *tpd, *first_tpd;
  1184. dma_addr_t dma;
  1185. int maplen, f, first_idx = txq->write_idx;
  1186. first_tpd = &txq->tpd[txq->write_idx];
  1187. tpd = first_tpd;
  1188. if (tpd->word1 & (1 << TPD_LSO_V2_SHIFT)) {
  1189. if (++txq->write_idx == txq->count)
  1190. txq->write_idx = 0;
  1191. tpd = &txq->tpd[txq->write_idx];
  1192. tpd->len = first_tpd->len;
  1193. tpd->vlan_tag = first_tpd->vlan_tag;
  1194. tpd->word1 = first_tpd->word1;
  1195. }
  1196. maplen = skb_headlen(skb);
  1197. dma = dma_map_single(txq->dev, skb->data, maplen,
  1198. DMA_TO_DEVICE);
  1199. if (dma_mapping_error(txq->dev, dma))
  1200. goto err_dma;
  1201. dma_unmap_len_set(&txq->bufs[txq->write_idx], size, maplen);
  1202. dma_unmap_addr_set(&txq->bufs[txq->write_idx], dma, dma);
  1203. tpd->adrl.addr = cpu_to_le64(dma);
  1204. tpd->len = cpu_to_le16(maplen);
  1205. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++) {
  1206. struct skb_frag_struct *frag;
  1207. frag = &skb_shinfo(skb)->frags[f];
  1208. if (++txq->write_idx == txq->count)
  1209. txq->write_idx = 0;
  1210. tpd = &txq->tpd[txq->write_idx];
  1211. tpd->word1 = first_tpd->word1;
  1212. maplen = skb_frag_size(frag);
  1213. dma = skb_frag_dma_map(txq->dev, frag, 0,
  1214. maplen, DMA_TO_DEVICE);
  1215. if (dma_mapping_error(txq->dev, dma))
  1216. goto err_dma;
  1217. dma_unmap_len_set(&txq->bufs[txq->write_idx], size, maplen);
  1218. dma_unmap_addr_set(&txq->bufs[txq->write_idx], dma, dma);
  1219. tpd->adrl.addr = cpu_to_le64(dma);
  1220. tpd->len = cpu_to_le16(maplen);
  1221. }
  1222. /* last TPD, set EOP flag and store skb */
  1223. tpd->word1 |= cpu_to_le32(1 << TPD_EOP_SHIFT);
  1224. txq->bufs[txq->write_idx].skb = skb;
  1225. if (++txq->write_idx == txq->count)
  1226. txq->write_idx = 0;
  1227. return 0;
  1228. err_dma:
  1229. f = first_idx;
  1230. while (f != txq->write_idx) {
  1231. alx_free_txbuf(txq, f);
  1232. if (++f == txq->count)
  1233. f = 0;
  1234. }
  1235. return -ENOMEM;
  1236. }
  1237. static netdev_tx_t alx_start_xmit_ring(struct sk_buff *skb,
  1238. struct alx_tx_queue *txq)
  1239. {
  1240. struct alx_priv *alx;
  1241. struct alx_txd *first;
  1242. int tso;
  1243. alx = netdev_priv(txq->netdev);
  1244. if (alx_tpd_avail(txq) < alx_tpd_req(skb)) {
  1245. netif_tx_stop_queue(alx_get_tx_queue(txq));
  1246. goto drop;
  1247. }
  1248. first = &txq->tpd[txq->write_idx];
  1249. memset(first, 0, sizeof(*first));
  1250. tso = alx_tso(skb, first);
  1251. if (tso < 0)
  1252. goto drop;
  1253. else if (!tso && alx_tx_csum(skb, first))
  1254. goto drop;
  1255. if (alx_map_tx_skb(txq, skb) < 0)
  1256. goto drop;
  1257. netdev_tx_sent_queue(alx_get_tx_queue(txq), skb->len);
  1258. /* flush updates before updating hardware */
  1259. wmb();
  1260. alx_write_mem16(&alx->hw, txq->p_reg, txq->write_idx);
  1261. if (alx_tpd_avail(txq) < txq->count / 8)
  1262. netif_tx_stop_queue(alx_get_tx_queue(txq));
  1263. return NETDEV_TX_OK;
  1264. drop:
  1265. dev_kfree_skb_any(skb);
  1266. return NETDEV_TX_OK;
  1267. }
  1268. static netdev_tx_t alx_start_xmit(struct sk_buff *skb,
  1269. struct net_device *netdev)
  1270. {
  1271. struct alx_priv *alx = netdev_priv(netdev);
  1272. return alx_start_xmit_ring(skb, alx_tx_queue_mapping(alx, skb));
  1273. }
  1274. static void alx_tx_timeout(struct net_device *dev)
  1275. {
  1276. struct alx_priv *alx = netdev_priv(dev);
  1277. alx_schedule_reset(alx);
  1278. }
  1279. static int alx_mdio_read(struct net_device *netdev,
  1280. int prtad, int devad, u16 addr)
  1281. {
  1282. struct alx_priv *alx = netdev_priv(netdev);
  1283. struct alx_hw *hw = &alx->hw;
  1284. u16 val;
  1285. int err;
  1286. if (prtad != hw->mdio.prtad)
  1287. return -EINVAL;
  1288. if (devad == MDIO_DEVAD_NONE)
  1289. err = alx_read_phy_reg(hw, addr, &val);
  1290. else
  1291. err = alx_read_phy_ext(hw, devad, addr, &val);
  1292. if (err)
  1293. return err;
  1294. return val;
  1295. }
  1296. static int alx_mdio_write(struct net_device *netdev,
  1297. int prtad, int devad, u16 addr, u16 val)
  1298. {
  1299. struct alx_priv *alx = netdev_priv(netdev);
  1300. struct alx_hw *hw = &alx->hw;
  1301. if (prtad != hw->mdio.prtad)
  1302. return -EINVAL;
  1303. if (devad == MDIO_DEVAD_NONE)
  1304. return alx_write_phy_reg(hw, addr, val);
  1305. return alx_write_phy_ext(hw, devad, addr, val);
  1306. }
  1307. static int alx_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  1308. {
  1309. struct alx_priv *alx = netdev_priv(netdev);
  1310. if (!netif_running(netdev))
  1311. return -EAGAIN;
  1312. return mdio_mii_ioctl(&alx->hw.mdio, if_mii(ifr), cmd);
  1313. }
  1314. #ifdef CONFIG_NET_POLL_CONTROLLER
  1315. static void alx_poll_controller(struct net_device *netdev)
  1316. {
  1317. struct alx_priv *alx = netdev_priv(netdev);
  1318. int i;
  1319. if (alx->flags & ALX_FLAG_USING_MSIX) {
  1320. alx_intr_msix_misc(0, alx);
  1321. for (i = 0; i < alx->num_txq; i++)
  1322. alx_intr_msix_ring(0, alx->qnapi[i]);
  1323. } else if (alx->flags & ALX_FLAG_USING_MSI)
  1324. alx_intr_msi(0, alx);
  1325. else
  1326. alx_intr_legacy(0, alx);
  1327. }
  1328. #endif
  1329. static void alx_get_stats64(struct net_device *dev,
  1330. struct rtnl_link_stats64 *net_stats)
  1331. {
  1332. struct alx_priv *alx = netdev_priv(dev);
  1333. struct alx_hw_stats *hw_stats = &alx->hw.stats;
  1334. spin_lock(&alx->stats_lock);
  1335. alx_update_hw_stats(&alx->hw);
  1336. net_stats->tx_bytes = hw_stats->tx_byte_cnt;
  1337. net_stats->rx_bytes = hw_stats->rx_byte_cnt;
  1338. net_stats->multicast = hw_stats->rx_mcast;
  1339. net_stats->collisions = hw_stats->tx_single_col +
  1340. hw_stats->tx_multi_col +
  1341. hw_stats->tx_late_col +
  1342. hw_stats->tx_abort_col;
  1343. net_stats->rx_errors = hw_stats->rx_frag +
  1344. hw_stats->rx_fcs_err +
  1345. hw_stats->rx_len_err +
  1346. hw_stats->rx_ov_sz +
  1347. hw_stats->rx_ov_rrd +
  1348. hw_stats->rx_align_err +
  1349. hw_stats->rx_ov_rxf;
  1350. net_stats->rx_fifo_errors = hw_stats->rx_ov_rxf;
  1351. net_stats->rx_length_errors = hw_stats->rx_len_err;
  1352. net_stats->rx_crc_errors = hw_stats->rx_fcs_err;
  1353. net_stats->rx_frame_errors = hw_stats->rx_align_err;
  1354. net_stats->rx_dropped = hw_stats->rx_ov_rrd;
  1355. net_stats->tx_errors = hw_stats->tx_late_col +
  1356. hw_stats->tx_abort_col +
  1357. hw_stats->tx_underrun +
  1358. hw_stats->tx_trunc;
  1359. net_stats->tx_aborted_errors = hw_stats->tx_abort_col;
  1360. net_stats->tx_fifo_errors = hw_stats->tx_underrun;
  1361. net_stats->tx_window_errors = hw_stats->tx_late_col;
  1362. net_stats->tx_packets = hw_stats->tx_ok + net_stats->tx_errors;
  1363. net_stats->rx_packets = hw_stats->rx_ok + net_stats->rx_errors;
  1364. spin_unlock(&alx->stats_lock);
  1365. }
  1366. static const struct net_device_ops alx_netdev_ops = {
  1367. .ndo_open = alx_open,
  1368. .ndo_stop = alx_stop,
  1369. .ndo_start_xmit = alx_start_xmit,
  1370. .ndo_get_stats64 = alx_get_stats64,
  1371. .ndo_set_rx_mode = alx_set_rx_mode,
  1372. .ndo_validate_addr = eth_validate_addr,
  1373. .ndo_set_mac_address = alx_set_mac_address,
  1374. .ndo_change_mtu = alx_change_mtu,
  1375. .ndo_do_ioctl = alx_ioctl,
  1376. .ndo_tx_timeout = alx_tx_timeout,
  1377. .ndo_fix_features = alx_fix_features,
  1378. #ifdef CONFIG_NET_POLL_CONTROLLER
  1379. .ndo_poll_controller = alx_poll_controller,
  1380. #endif
  1381. };
  1382. static int alx_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1383. {
  1384. struct net_device *netdev;
  1385. struct alx_priv *alx;
  1386. struct alx_hw *hw;
  1387. bool phy_configured;
  1388. int err;
  1389. err = pci_enable_device_mem(pdev);
  1390. if (err)
  1391. return err;
  1392. /* The alx chip can DMA to 64-bit addresses, but it uses a single
  1393. * shared register for the high 32 bits, so only a single, aligned,
  1394. * 4 GB physical address range can be used for descriptors.
  1395. */
  1396. if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
  1397. dev_dbg(&pdev->dev, "DMA to 64-BIT addresses\n");
  1398. } else {
  1399. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  1400. if (err) {
  1401. dev_err(&pdev->dev, "No usable DMA config, aborting\n");
  1402. goto out_pci_disable;
  1403. }
  1404. }
  1405. err = pci_request_mem_regions(pdev, alx_drv_name);
  1406. if (err) {
  1407. dev_err(&pdev->dev,
  1408. "pci_request_mem_regions failed\n");
  1409. goto out_pci_disable;
  1410. }
  1411. pci_enable_pcie_error_reporting(pdev);
  1412. pci_set_master(pdev);
  1413. if (!pdev->pm_cap) {
  1414. dev_err(&pdev->dev,
  1415. "Can't find power management capability, aborting\n");
  1416. err = -EIO;
  1417. goto out_pci_release;
  1418. }
  1419. netdev = alloc_etherdev_mqs(sizeof(*alx),
  1420. ALX_MAX_TX_QUEUES, 1);
  1421. if (!netdev) {
  1422. err = -ENOMEM;
  1423. goto out_pci_release;
  1424. }
  1425. SET_NETDEV_DEV(netdev, &pdev->dev);
  1426. alx = netdev_priv(netdev);
  1427. spin_lock_init(&alx->hw.mdio_lock);
  1428. spin_lock_init(&alx->irq_lock);
  1429. spin_lock_init(&alx->stats_lock);
  1430. alx->dev = netdev;
  1431. alx->hw.pdev = pdev;
  1432. alx->msg_enable = NETIF_MSG_LINK | NETIF_MSG_HW | NETIF_MSG_IFUP |
  1433. NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR | NETIF_MSG_WOL;
  1434. hw = &alx->hw;
  1435. pci_set_drvdata(pdev, alx);
  1436. hw->hw_addr = pci_ioremap_bar(pdev, 0);
  1437. if (!hw->hw_addr) {
  1438. dev_err(&pdev->dev, "cannot map device registers\n");
  1439. err = -EIO;
  1440. goto out_free_netdev;
  1441. }
  1442. netdev->netdev_ops = &alx_netdev_ops;
  1443. netdev->ethtool_ops = &alx_ethtool_ops;
  1444. netdev->irq = pdev->irq;
  1445. netdev->watchdog_timeo = ALX_WATCHDOG_TIME;
  1446. if (ent->driver_data & ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG)
  1447. pdev->dev_flags |= PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG;
  1448. err = alx_init_sw(alx);
  1449. if (err) {
  1450. dev_err(&pdev->dev, "net device private data init failed\n");
  1451. goto out_unmap;
  1452. }
  1453. alx_reset_pcie(hw);
  1454. phy_configured = alx_phy_configured(hw);
  1455. if (!phy_configured)
  1456. alx_reset_phy(hw);
  1457. err = alx_reset_mac(hw);
  1458. if (err) {
  1459. dev_err(&pdev->dev, "MAC Reset failed, error = %d\n", err);
  1460. goto out_unmap;
  1461. }
  1462. /* setup link to put it in a known good starting state */
  1463. if (!phy_configured) {
  1464. err = alx_setup_speed_duplex(hw, hw->adv_cfg, hw->flowctrl);
  1465. if (err) {
  1466. dev_err(&pdev->dev,
  1467. "failed to configure PHY speed/duplex (err=%d)\n",
  1468. err);
  1469. goto out_unmap;
  1470. }
  1471. }
  1472. netdev->hw_features = NETIF_F_SG |
  1473. NETIF_F_HW_CSUM |
  1474. NETIF_F_RXCSUM |
  1475. NETIF_F_TSO |
  1476. NETIF_F_TSO6;
  1477. if (alx_get_perm_macaddr(hw, hw->perm_addr)) {
  1478. dev_warn(&pdev->dev,
  1479. "Invalid permanent address programmed, using random one\n");
  1480. eth_hw_addr_random(netdev);
  1481. memcpy(hw->perm_addr, netdev->dev_addr, netdev->addr_len);
  1482. }
  1483. memcpy(hw->mac_addr, hw->perm_addr, ETH_ALEN);
  1484. memcpy(netdev->dev_addr, hw->mac_addr, ETH_ALEN);
  1485. memcpy(netdev->perm_addr, hw->perm_addr, ETH_ALEN);
  1486. hw->mdio.prtad = 0;
  1487. hw->mdio.mmds = 0;
  1488. hw->mdio.dev = netdev;
  1489. hw->mdio.mode_support = MDIO_SUPPORTS_C45 |
  1490. MDIO_SUPPORTS_C22 |
  1491. MDIO_EMULATE_C22;
  1492. hw->mdio.mdio_read = alx_mdio_read;
  1493. hw->mdio.mdio_write = alx_mdio_write;
  1494. if (!alx_get_phy_info(hw)) {
  1495. dev_err(&pdev->dev, "failed to identify PHY\n");
  1496. err = -EIO;
  1497. goto out_unmap;
  1498. }
  1499. INIT_WORK(&alx->link_check_wk, alx_link_check);
  1500. INIT_WORK(&alx->reset_wk, alx_reset);
  1501. netif_carrier_off(netdev);
  1502. err = register_netdev(netdev);
  1503. if (err) {
  1504. dev_err(&pdev->dev, "register netdevice failed\n");
  1505. goto out_unmap;
  1506. }
  1507. netdev_info(netdev,
  1508. "Qualcomm Atheros AR816x/AR817x Ethernet [%pM]\n",
  1509. netdev->dev_addr);
  1510. return 0;
  1511. out_unmap:
  1512. iounmap(hw->hw_addr);
  1513. out_free_netdev:
  1514. free_netdev(netdev);
  1515. out_pci_release:
  1516. pci_release_mem_regions(pdev);
  1517. out_pci_disable:
  1518. pci_disable_device(pdev);
  1519. return err;
  1520. }
  1521. static void alx_remove(struct pci_dev *pdev)
  1522. {
  1523. struct alx_priv *alx = pci_get_drvdata(pdev);
  1524. struct alx_hw *hw = &alx->hw;
  1525. cancel_work_sync(&alx->link_check_wk);
  1526. cancel_work_sync(&alx->reset_wk);
  1527. /* restore permanent mac address */
  1528. alx_set_macaddr(hw, hw->perm_addr);
  1529. unregister_netdev(alx->dev);
  1530. iounmap(hw->hw_addr);
  1531. pci_release_mem_regions(pdev);
  1532. pci_disable_pcie_error_reporting(pdev);
  1533. pci_disable_device(pdev);
  1534. free_netdev(alx->dev);
  1535. }
  1536. #ifdef CONFIG_PM_SLEEP
  1537. static int alx_suspend(struct device *dev)
  1538. {
  1539. struct pci_dev *pdev = to_pci_dev(dev);
  1540. struct alx_priv *alx = pci_get_drvdata(pdev);
  1541. if (!netif_running(alx->dev))
  1542. return 0;
  1543. netif_device_detach(alx->dev);
  1544. __alx_stop(alx);
  1545. return 0;
  1546. }
  1547. static int alx_resume(struct device *dev)
  1548. {
  1549. struct pci_dev *pdev = to_pci_dev(dev);
  1550. struct alx_priv *alx = pci_get_drvdata(pdev);
  1551. struct alx_hw *hw = &alx->hw;
  1552. alx_reset_phy(hw);
  1553. if (!netif_running(alx->dev))
  1554. return 0;
  1555. netif_device_attach(alx->dev);
  1556. return __alx_open(alx, true);
  1557. }
  1558. static SIMPLE_DEV_PM_OPS(alx_pm_ops, alx_suspend, alx_resume);
  1559. #define ALX_PM_OPS (&alx_pm_ops)
  1560. #else
  1561. #define ALX_PM_OPS NULL
  1562. #endif
  1563. static pci_ers_result_t alx_pci_error_detected(struct pci_dev *pdev,
  1564. pci_channel_state_t state)
  1565. {
  1566. struct alx_priv *alx = pci_get_drvdata(pdev);
  1567. struct net_device *netdev = alx->dev;
  1568. pci_ers_result_t rc = PCI_ERS_RESULT_NEED_RESET;
  1569. dev_info(&pdev->dev, "pci error detected\n");
  1570. rtnl_lock();
  1571. if (netif_running(netdev)) {
  1572. netif_device_detach(netdev);
  1573. alx_halt(alx);
  1574. }
  1575. if (state == pci_channel_io_perm_failure)
  1576. rc = PCI_ERS_RESULT_DISCONNECT;
  1577. else
  1578. pci_disable_device(pdev);
  1579. rtnl_unlock();
  1580. return rc;
  1581. }
  1582. static pci_ers_result_t alx_pci_error_slot_reset(struct pci_dev *pdev)
  1583. {
  1584. struct alx_priv *alx = pci_get_drvdata(pdev);
  1585. struct alx_hw *hw = &alx->hw;
  1586. pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
  1587. dev_info(&pdev->dev, "pci error slot reset\n");
  1588. rtnl_lock();
  1589. if (pci_enable_device(pdev)) {
  1590. dev_err(&pdev->dev, "Failed to re-enable PCI device after reset\n");
  1591. goto out;
  1592. }
  1593. pci_set_master(pdev);
  1594. alx_reset_pcie(hw);
  1595. if (!alx_reset_mac(hw))
  1596. rc = PCI_ERS_RESULT_RECOVERED;
  1597. out:
  1598. pci_cleanup_aer_uncorrect_error_status(pdev);
  1599. rtnl_unlock();
  1600. return rc;
  1601. }
  1602. static void alx_pci_error_resume(struct pci_dev *pdev)
  1603. {
  1604. struct alx_priv *alx = pci_get_drvdata(pdev);
  1605. struct net_device *netdev = alx->dev;
  1606. dev_info(&pdev->dev, "pci error resume\n");
  1607. rtnl_lock();
  1608. if (netif_running(netdev)) {
  1609. alx_activate(alx);
  1610. netif_device_attach(netdev);
  1611. }
  1612. rtnl_unlock();
  1613. }
  1614. static const struct pci_error_handlers alx_err_handlers = {
  1615. .error_detected = alx_pci_error_detected,
  1616. .slot_reset = alx_pci_error_slot_reset,
  1617. .resume = alx_pci_error_resume,
  1618. };
  1619. static const struct pci_device_id alx_pci_tbl[] = {
  1620. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_AR8161),
  1621. .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
  1622. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_E2200),
  1623. .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
  1624. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_E2400),
  1625. .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
  1626. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_E2500),
  1627. .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
  1628. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_AR8162),
  1629. .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
  1630. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_AR8171) },
  1631. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_AR8172) },
  1632. {}
  1633. };
  1634. static struct pci_driver alx_driver = {
  1635. .name = alx_drv_name,
  1636. .id_table = alx_pci_tbl,
  1637. .probe = alx_probe,
  1638. .remove = alx_remove,
  1639. .err_handler = &alx_err_handlers,
  1640. .driver.pm = ALX_PM_OPS,
  1641. };
  1642. module_pci_driver(alx_driver);
  1643. MODULE_DEVICE_TABLE(pci, alx_pci_tbl);
  1644. MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
  1645. MODULE_AUTHOR("Qualcomm Corporation, <nic-devel@qualcomm.com>");
  1646. MODULE_DESCRIPTION(
  1647. "Qualcomm Atheros(R) AR816x/AR817x PCI-E Ethernet Network Driver");
  1648. MODULE_LICENSE("GPL");