main.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024
  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_halt(alx);
  1012. alx_free_irq(alx);
  1013. alx_free_rings(alx);
  1014. alx_free_napis(alx);
  1015. }
  1016. static const char *alx_speed_desc(struct alx_hw *hw)
  1017. {
  1018. switch (alx_speed_to_ethadv(hw->link_speed, hw->duplex)) {
  1019. case ADVERTISED_1000baseT_Full:
  1020. return "1 Gbps Full";
  1021. case ADVERTISED_100baseT_Full:
  1022. return "100 Mbps Full";
  1023. case ADVERTISED_100baseT_Half:
  1024. return "100 Mbps Half";
  1025. case ADVERTISED_10baseT_Full:
  1026. return "10 Mbps Full";
  1027. case ADVERTISED_10baseT_Half:
  1028. return "10 Mbps Half";
  1029. default:
  1030. return "Unknown speed";
  1031. }
  1032. }
  1033. static void alx_check_link(struct alx_priv *alx)
  1034. {
  1035. struct alx_hw *hw = &alx->hw;
  1036. unsigned long flags;
  1037. int old_speed;
  1038. u8 old_duplex;
  1039. int err;
  1040. /* clear PHY internal interrupt status, otherwise the main
  1041. * interrupt status will be asserted forever
  1042. */
  1043. alx_clear_phy_intr(hw);
  1044. old_speed = hw->link_speed;
  1045. old_duplex = hw->duplex;
  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. cancel_work_sync(&alx->link_check_wk);
  1508. cancel_work_sync(&alx->reset_wk);
  1509. /* restore permanent mac address */
  1510. alx_set_macaddr(hw, hw->perm_addr);
  1511. unregister_netdev(alx->dev);
  1512. iounmap(hw->hw_addr);
  1513. pci_release_mem_regions(pdev);
  1514. pci_disable_pcie_error_reporting(pdev);
  1515. pci_disable_device(pdev);
  1516. free_netdev(alx->dev);
  1517. }
  1518. #ifdef CONFIG_PM_SLEEP
  1519. static int alx_suspend(struct device *dev)
  1520. {
  1521. struct pci_dev *pdev = to_pci_dev(dev);
  1522. struct alx_priv *alx = pci_get_drvdata(pdev);
  1523. if (!netif_running(alx->dev))
  1524. return 0;
  1525. netif_device_detach(alx->dev);
  1526. __alx_stop(alx);
  1527. return 0;
  1528. }
  1529. static int alx_resume(struct device *dev)
  1530. {
  1531. struct pci_dev *pdev = to_pci_dev(dev);
  1532. struct alx_priv *alx = pci_get_drvdata(pdev);
  1533. struct alx_hw *hw = &alx->hw;
  1534. alx_reset_phy(hw);
  1535. if (!netif_running(alx->dev))
  1536. return 0;
  1537. netif_device_attach(alx->dev);
  1538. return __alx_open(alx, true);
  1539. }
  1540. static SIMPLE_DEV_PM_OPS(alx_pm_ops, alx_suspend, alx_resume);
  1541. #define ALX_PM_OPS (&alx_pm_ops)
  1542. #else
  1543. #define ALX_PM_OPS NULL
  1544. #endif
  1545. static pci_ers_result_t alx_pci_error_detected(struct pci_dev *pdev,
  1546. pci_channel_state_t state)
  1547. {
  1548. struct alx_priv *alx = pci_get_drvdata(pdev);
  1549. struct net_device *netdev = alx->dev;
  1550. pci_ers_result_t rc = PCI_ERS_RESULT_NEED_RESET;
  1551. dev_info(&pdev->dev, "pci error detected\n");
  1552. rtnl_lock();
  1553. if (netif_running(netdev)) {
  1554. netif_device_detach(netdev);
  1555. alx_halt(alx);
  1556. }
  1557. if (state == pci_channel_io_perm_failure)
  1558. rc = PCI_ERS_RESULT_DISCONNECT;
  1559. else
  1560. pci_disable_device(pdev);
  1561. rtnl_unlock();
  1562. return rc;
  1563. }
  1564. static pci_ers_result_t alx_pci_error_slot_reset(struct pci_dev *pdev)
  1565. {
  1566. struct alx_priv *alx = pci_get_drvdata(pdev);
  1567. struct alx_hw *hw = &alx->hw;
  1568. pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
  1569. dev_info(&pdev->dev, "pci error slot reset\n");
  1570. rtnl_lock();
  1571. if (pci_enable_device(pdev)) {
  1572. dev_err(&pdev->dev, "Failed to re-enable PCI device after reset\n");
  1573. goto out;
  1574. }
  1575. pci_set_master(pdev);
  1576. alx_reset_pcie(hw);
  1577. if (!alx_reset_mac(hw))
  1578. rc = PCI_ERS_RESULT_RECOVERED;
  1579. out:
  1580. pci_cleanup_aer_uncorrect_error_status(pdev);
  1581. rtnl_unlock();
  1582. return rc;
  1583. }
  1584. static void alx_pci_error_resume(struct pci_dev *pdev)
  1585. {
  1586. struct alx_priv *alx = pci_get_drvdata(pdev);
  1587. struct net_device *netdev = alx->dev;
  1588. dev_info(&pdev->dev, "pci error resume\n");
  1589. rtnl_lock();
  1590. if (netif_running(netdev)) {
  1591. alx_activate(alx);
  1592. netif_device_attach(netdev);
  1593. }
  1594. rtnl_unlock();
  1595. }
  1596. static const struct pci_error_handlers alx_err_handlers = {
  1597. .error_detected = alx_pci_error_detected,
  1598. .slot_reset = alx_pci_error_slot_reset,
  1599. .resume = alx_pci_error_resume,
  1600. };
  1601. static const struct pci_device_id alx_pci_tbl[] = {
  1602. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_AR8161),
  1603. .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
  1604. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_E2200),
  1605. .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
  1606. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_E2400),
  1607. .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
  1608. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_E2500),
  1609. .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
  1610. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_AR8162),
  1611. .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
  1612. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_AR8171) },
  1613. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_AR8172) },
  1614. {}
  1615. };
  1616. static struct pci_driver alx_driver = {
  1617. .name = alx_drv_name,
  1618. .id_table = alx_pci_tbl,
  1619. .probe = alx_probe,
  1620. .remove = alx_remove,
  1621. .err_handler = &alx_err_handlers,
  1622. .driver.pm = ALX_PM_OPS,
  1623. };
  1624. module_pci_driver(alx_driver);
  1625. MODULE_DEVICE_TABLE(pci, alx_pci_tbl);
  1626. MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
  1627. MODULE_AUTHOR("Qualcomm Corporation, <nic-devel@qualcomm.com>");
  1628. MODULE_DESCRIPTION(
  1629. "Qualcomm Atheros(R) AR816x/AR817x PCI-E Ethernet Network Driver");
  1630. MODULE_LICENSE("GPL");