main.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040
  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(&np->napi);
  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. alx_reinit_rings(alx);
  557. return 0;
  558. }
  559. static void alx_free_rings(struct alx_priv *alx)
  560. {
  561. int i;
  562. alx_free_buffers(alx);
  563. for (i = 0; i < alx->num_txq; i++)
  564. if (alx->qnapi[i] && alx->qnapi[i]->txq)
  565. kfree(alx->qnapi[i]->txq->bufs);
  566. if (alx->qnapi[0] && alx->qnapi[0]->rxq)
  567. kfree(alx->qnapi[0]->rxq->bufs);
  568. if (!alx->descmem.virt)
  569. dma_free_coherent(&alx->hw.pdev->dev,
  570. alx->descmem.size,
  571. alx->descmem.virt,
  572. alx->descmem.dma);
  573. }
  574. static void alx_free_napis(struct alx_priv *alx)
  575. {
  576. struct alx_napi *np;
  577. int i;
  578. for (i = 0; i < alx->num_napi; i++) {
  579. np = alx->qnapi[i];
  580. if (!np)
  581. continue;
  582. netif_napi_del(&np->napi);
  583. kfree(np->txq);
  584. kfree(np->rxq);
  585. kfree(np);
  586. alx->qnapi[i] = NULL;
  587. }
  588. }
  589. static const u16 tx_pidx_reg[] = {ALX_TPD_PRI0_PIDX, ALX_TPD_PRI1_PIDX,
  590. ALX_TPD_PRI2_PIDX, ALX_TPD_PRI3_PIDX};
  591. static const u16 tx_cidx_reg[] = {ALX_TPD_PRI0_CIDX, ALX_TPD_PRI1_CIDX,
  592. ALX_TPD_PRI2_CIDX, ALX_TPD_PRI3_CIDX};
  593. static const u32 tx_vect_mask[] = {ALX_ISR_TX_Q0, ALX_ISR_TX_Q1,
  594. ALX_ISR_TX_Q2, ALX_ISR_TX_Q3};
  595. static const u32 rx_vect_mask[] = {ALX_ISR_RX_Q0, ALX_ISR_RX_Q1,
  596. ALX_ISR_RX_Q2, ALX_ISR_RX_Q3,
  597. ALX_ISR_RX_Q4, ALX_ISR_RX_Q5,
  598. ALX_ISR_RX_Q6, ALX_ISR_RX_Q7};
  599. static int alx_alloc_napis(struct alx_priv *alx)
  600. {
  601. struct alx_napi *np;
  602. struct alx_rx_queue *rxq;
  603. struct alx_tx_queue *txq;
  604. int i;
  605. alx->int_mask &= ~ALX_ISR_ALL_QUEUES;
  606. /* allocate alx_napi structures */
  607. for (i = 0; i < alx->num_napi; i++) {
  608. np = kzalloc(sizeof(struct alx_napi), GFP_KERNEL);
  609. if (!np)
  610. goto err_out;
  611. np->alx = alx;
  612. netif_napi_add(alx->dev, &np->napi, alx_poll, 64);
  613. alx->qnapi[i] = np;
  614. }
  615. /* allocate tx queues */
  616. for (i = 0; i < alx->num_txq; i++) {
  617. np = alx->qnapi[i];
  618. txq = kzalloc(sizeof(*txq), GFP_KERNEL);
  619. if (!txq)
  620. goto err_out;
  621. np->txq = txq;
  622. txq->p_reg = tx_pidx_reg[i];
  623. txq->c_reg = tx_cidx_reg[i];
  624. txq->queue_idx = i;
  625. txq->count = alx->tx_ringsz;
  626. txq->netdev = alx->dev;
  627. txq->dev = &alx->hw.pdev->dev;
  628. np->vec_mask |= tx_vect_mask[i];
  629. alx->int_mask |= tx_vect_mask[i];
  630. }
  631. /* allocate rx queues */
  632. np = alx->qnapi[0];
  633. rxq = kzalloc(sizeof(*rxq), GFP_KERNEL);
  634. if (!rxq)
  635. goto err_out;
  636. np->rxq = rxq;
  637. rxq->np = alx->qnapi[0];
  638. rxq->queue_idx = 0;
  639. rxq->count = alx->rx_ringsz;
  640. rxq->netdev = alx->dev;
  641. rxq->dev = &alx->hw.pdev->dev;
  642. np->vec_mask |= rx_vect_mask[0];
  643. alx->int_mask |= rx_vect_mask[0];
  644. return 0;
  645. err_out:
  646. netdev_err(alx->dev, "error allocating internal structures\n");
  647. alx_free_napis(alx);
  648. return -ENOMEM;
  649. }
  650. static const int txq_vec_mapping_shift[] = {
  651. 0, ALX_MSI_MAP_TBL1_TXQ0_SHIFT,
  652. 0, ALX_MSI_MAP_TBL1_TXQ1_SHIFT,
  653. 1, ALX_MSI_MAP_TBL2_TXQ2_SHIFT,
  654. 1, ALX_MSI_MAP_TBL2_TXQ3_SHIFT,
  655. };
  656. static void alx_config_vector_mapping(struct alx_priv *alx)
  657. {
  658. struct alx_hw *hw = &alx->hw;
  659. u32 tbl[2] = {0, 0};
  660. int i, vector, idx, shift;
  661. if (alx->flags & ALX_FLAG_USING_MSIX) {
  662. /* tx mappings */
  663. for (i = 0, vector = 1; i < alx->num_txq; i++, vector++) {
  664. idx = txq_vec_mapping_shift[i * 2];
  665. shift = txq_vec_mapping_shift[i * 2 + 1];
  666. tbl[idx] |= vector << shift;
  667. }
  668. /* rx mapping */
  669. tbl[0] |= 1 << ALX_MSI_MAP_TBL1_RXQ0_SHIFT;
  670. }
  671. alx_write_mem32(hw, ALX_MSI_MAP_TBL1, tbl[0]);
  672. alx_write_mem32(hw, ALX_MSI_MAP_TBL2, tbl[1]);
  673. alx_write_mem32(hw, ALX_MSI_ID_MAP, 0);
  674. }
  675. static bool alx_enable_msix(struct alx_priv *alx)
  676. {
  677. int i, err, num_vec, num_txq, num_rxq;
  678. num_txq = min_t(int, num_online_cpus(), ALX_MAX_TX_QUEUES);
  679. num_rxq = 1;
  680. num_vec = max_t(int, num_txq, num_rxq) + 1;
  681. alx->msix_entries = kcalloc(num_vec, sizeof(struct msix_entry),
  682. GFP_KERNEL);
  683. if (!alx->msix_entries) {
  684. netdev_warn(alx->dev, "Allocation of msix entries failed!\n");
  685. return false;
  686. }
  687. for (i = 0; i < num_vec; i++)
  688. alx->msix_entries[i].entry = i;
  689. err = pci_enable_msix(alx->hw.pdev, alx->msix_entries, num_vec);
  690. if (err) {
  691. kfree(alx->msix_entries);
  692. netdev_warn(alx->dev, "Enabling MSI-X interrupts failed!\n");
  693. return false;
  694. }
  695. alx->num_vec = num_vec;
  696. alx->num_napi = num_vec - 1;
  697. alx->num_txq = num_txq;
  698. alx->num_rxq = num_rxq;
  699. return true;
  700. }
  701. static int alx_request_msix(struct alx_priv *alx)
  702. {
  703. struct net_device *netdev = alx->dev;
  704. int i, err, vector = 0, free_vector = 0;
  705. err = request_irq(alx->msix_entries[0].vector, alx_intr_msix_misc,
  706. 0, netdev->name, alx);
  707. if (err)
  708. goto out_err;
  709. for (i = 0; i < alx->num_napi; i++) {
  710. struct alx_napi *np = alx->qnapi[i];
  711. vector++;
  712. if (np->txq && np->rxq)
  713. sprintf(np->irq_lbl, "%s-TxRx-%u", netdev->name,
  714. np->txq->queue_idx);
  715. else if (np->txq)
  716. sprintf(np->irq_lbl, "%s-tx-%u", netdev->name,
  717. np->txq->queue_idx);
  718. else if (np->rxq)
  719. sprintf(np->irq_lbl, "%s-rx-%u", netdev->name,
  720. np->rxq->queue_idx);
  721. else
  722. sprintf(np->irq_lbl, "%s-unused", netdev->name);
  723. np->vec_idx = vector;
  724. err = request_irq(alx->msix_entries[vector].vector,
  725. alx_intr_msix_ring, 0, np->irq_lbl, np);
  726. if (err)
  727. goto out_free;
  728. }
  729. return 0;
  730. out_free:
  731. free_irq(alx->msix_entries[free_vector++].vector, alx);
  732. vector--;
  733. for (i = 0; i < vector; i++)
  734. free_irq(alx->msix_entries[free_vector++].vector,
  735. alx->qnapi[i]);
  736. out_err:
  737. return err;
  738. }
  739. static void alx_init_intr(struct alx_priv *alx, bool msix)
  740. {
  741. if (msix) {
  742. if (alx_enable_msix(alx))
  743. alx->flags |= ALX_FLAG_USING_MSIX;
  744. }
  745. if (!(alx->flags & ALX_FLAG_USING_MSIX)) {
  746. alx->num_vec = 1;
  747. alx->num_napi = 1;
  748. alx->num_txq = 1;
  749. alx->num_rxq = 1;
  750. if (!pci_enable_msi(alx->hw.pdev))
  751. alx->flags |= ALX_FLAG_USING_MSI;
  752. }
  753. }
  754. static void alx_disable_advanced_intr(struct alx_priv *alx)
  755. {
  756. if (alx->flags & ALX_FLAG_USING_MSIX) {
  757. kfree(alx->msix_entries);
  758. pci_disable_msix(alx->hw.pdev);
  759. alx->flags &= ~ALX_FLAG_USING_MSIX;
  760. }
  761. if (alx->flags & ALX_FLAG_USING_MSI) {
  762. pci_disable_msi(alx->hw.pdev);
  763. alx->flags &= ~ALX_FLAG_USING_MSI;
  764. }
  765. }
  766. static void alx_irq_enable(struct alx_priv *alx)
  767. {
  768. struct alx_hw *hw = &alx->hw;
  769. int i;
  770. /* level-1 interrupt switch */
  771. alx_write_mem32(hw, ALX_ISR, 0);
  772. alx_write_mem32(hw, ALX_IMR, alx->int_mask);
  773. alx_post_write(hw);
  774. if (alx->flags & ALX_FLAG_USING_MSIX)
  775. /* enable all msix irqs */
  776. for (i = 0; i < alx->num_vec; i++)
  777. alx_mask_msix(hw, i, false);
  778. }
  779. static void alx_irq_disable(struct alx_priv *alx)
  780. {
  781. struct alx_hw *hw = &alx->hw;
  782. int i;
  783. alx_write_mem32(hw, ALX_ISR, ALX_ISR_DIS);
  784. alx_write_mem32(hw, ALX_IMR, 0);
  785. alx_post_write(hw);
  786. if (alx->flags & ALX_FLAG_USING_MSIX) {
  787. for (i = 0; i < alx->num_vec; i++) {
  788. alx_mask_msix(hw, i, true);
  789. synchronize_irq(alx->msix_entries[i].vector);
  790. }
  791. } else {
  792. synchronize_irq(alx->hw.pdev->irq);
  793. }
  794. }
  795. static int alx_realloc_resources(struct alx_priv *alx)
  796. {
  797. int err;
  798. alx_free_rings(alx);
  799. alx_free_napis(alx);
  800. alx_disable_advanced_intr(alx);
  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. netif_set_real_num_tx_queues(alx->dev, alx->num_txq);
  1007. netif_set_real_num_rx_queues(alx->dev, alx->num_rxq);
  1008. /* clear old interrupts */
  1009. alx_write_mem32(&alx->hw, ALX_ISR, ~(u32)ALX_ISR_DIS);
  1010. alx_irq_enable(alx);
  1011. if (!resume)
  1012. netif_tx_start_all_queues(alx->dev);
  1013. alx_schedule_link_check(alx);
  1014. return 0;
  1015. out_free_rings:
  1016. alx_free_rings(alx);
  1017. alx_free_napis(alx);
  1018. out_disable_adv_intr:
  1019. alx_disable_advanced_intr(alx);
  1020. return err;
  1021. }
  1022. static void __alx_stop(struct alx_priv *alx)
  1023. {
  1024. alx_halt(alx);
  1025. alx_free_irq(alx);
  1026. alx_free_rings(alx);
  1027. alx_free_napis(alx);
  1028. }
  1029. static const char *alx_speed_desc(struct alx_hw *hw)
  1030. {
  1031. switch (alx_speed_to_ethadv(hw->link_speed, hw->duplex)) {
  1032. case ADVERTISED_1000baseT_Full:
  1033. return "1 Gbps Full";
  1034. case ADVERTISED_100baseT_Full:
  1035. return "100 Mbps Full";
  1036. case ADVERTISED_100baseT_Half:
  1037. return "100 Mbps Half";
  1038. case ADVERTISED_10baseT_Full:
  1039. return "10 Mbps Full";
  1040. case ADVERTISED_10baseT_Half:
  1041. return "10 Mbps Half";
  1042. default:
  1043. return "Unknown speed";
  1044. }
  1045. }
  1046. static void alx_check_link(struct alx_priv *alx)
  1047. {
  1048. struct alx_hw *hw = &alx->hw;
  1049. unsigned long flags;
  1050. int old_speed;
  1051. u8 old_duplex;
  1052. int err;
  1053. /* clear PHY internal interrupt status, otherwise the main
  1054. * interrupt status will be asserted forever
  1055. */
  1056. alx_clear_phy_intr(hw);
  1057. old_speed = hw->link_speed;
  1058. old_duplex = hw->duplex;
  1059. err = alx_read_phy_link(hw);
  1060. if (err < 0)
  1061. goto reset;
  1062. spin_lock_irqsave(&alx->irq_lock, flags);
  1063. alx->int_mask |= ALX_ISR_PHY;
  1064. alx_write_mem32(hw, ALX_IMR, alx->int_mask);
  1065. spin_unlock_irqrestore(&alx->irq_lock, flags);
  1066. if (old_speed == hw->link_speed)
  1067. return;
  1068. if (hw->link_speed != SPEED_UNKNOWN) {
  1069. netif_info(alx, link, alx->dev,
  1070. "NIC Up: %s\n", alx_speed_desc(hw));
  1071. alx_post_phy_link(hw);
  1072. alx_enable_aspm(hw, true, true);
  1073. alx_start_mac(hw);
  1074. if (old_speed == SPEED_UNKNOWN)
  1075. alx_netif_start(alx);
  1076. } else {
  1077. /* link is now down */
  1078. alx_netif_stop(alx);
  1079. netif_info(alx, link, alx->dev, "Link Down\n");
  1080. err = alx_reset_mac(hw);
  1081. if (err)
  1082. goto reset;
  1083. alx_irq_disable(alx);
  1084. /* MAC reset causes all HW settings to be lost, restore all */
  1085. err = alx_reinit_rings(alx);
  1086. if (err)
  1087. goto reset;
  1088. alx_configure(alx);
  1089. alx_enable_aspm(hw, false, true);
  1090. alx_post_phy_link(hw);
  1091. alx_irq_enable(alx);
  1092. }
  1093. return;
  1094. reset:
  1095. alx_schedule_reset(alx);
  1096. }
  1097. static int alx_open(struct net_device *netdev)
  1098. {
  1099. return __alx_open(netdev_priv(netdev), false);
  1100. }
  1101. static int alx_stop(struct net_device *netdev)
  1102. {
  1103. __alx_stop(netdev_priv(netdev));
  1104. return 0;
  1105. }
  1106. static void alx_link_check(struct work_struct *work)
  1107. {
  1108. struct alx_priv *alx;
  1109. alx = container_of(work, struct alx_priv, link_check_wk);
  1110. rtnl_lock();
  1111. alx_check_link(alx);
  1112. rtnl_unlock();
  1113. }
  1114. static void alx_reset(struct work_struct *work)
  1115. {
  1116. struct alx_priv *alx = container_of(work, struct alx_priv, reset_wk);
  1117. rtnl_lock();
  1118. alx_reinit(alx);
  1119. rtnl_unlock();
  1120. }
  1121. static int alx_tpd_req(struct sk_buff *skb)
  1122. {
  1123. int num;
  1124. num = skb_shinfo(skb)->nr_frags + 1;
  1125. /* we need one extra descriptor for LSOv2 */
  1126. if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  1127. num++;
  1128. return num;
  1129. }
  1130. static int alx_tx_csum(struct sk_buff *skb, struct alx_txd *first)
  1131. {
  1132. u8 cso, css;
  1133. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1134. return 0;
  1135. cso = skb_checksum_start_offset(skb);
  1136. if (cso & 1)
  1137. return -EINVAL;
  1138. css = cso + skb->csum_offset;
  1139. first->word1 |= cpu_to_le32((cso >> 1) << TPD_CXSUMSTART_SHIFT);
  1140. first->word1 |= cpu_to_le32((css >> 1) << TPD_CXSUMOFFSET_SHIFT);
  1141. first->word1 |= cpu_to_le32(1 << TPD_CXSUM_EN_SHIFT);
  1142. return 0;
  1143. }
  1144. static int alx_tso(struct sk_buff *skb, struct alx_txd *first)
  1145. {
  1146. int err;
  1147. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1148. return 0;
  1149. if (!skb_is_gso(skb))
  1150. return 0;
  1151. err = skb_cow_head(skb, 0);
  1152. if (err < 0)
  1153. return err;
  1154. if (skb->protocol == htons(ETH_P_IP)) {
  1155. struct iphdr *iph = ip_hdr(skb);
  1156. iph->check = 0;
  1157. tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  1158. 0, IPPROTO_TCP, 0);
  1159. first->word1 |= 1 << TPD_IPV4_SHIFT;
  1160. } else if (skb_is_gso_v6(skb)) {
  1161. ipv6_hdr(skb)->payload_len = 0;
  1162. tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  1163. &ipv6_hdr(skb)->daddr,
  1164. 0, IPPROTO_TCP, 0);
  1165. /* LSOv2: the first TPD only provides the packet length */
  1166. first->adrl.l.pkt_len = skb->len;
  1167. first->word1 |= 1 << TPD_LSO_V2_SHIFT;
  1168. }
  1169. first->word1 |= 1 << TPD_LSO_EN_SHIFT;
  1170. first->word1 |= (skb_transport_offset(skb) &
  1171. TPD_L4HDROFFSET_MASK) << TPD_L4HDROFFSET_SHIFT;
  1172. first->word1 |= (skb_shinfo(skb)->gso_size &
  1173. TPD_MSS_MASK) << TPD_MSS_SHIFT;
  1174. return 1;
  1175. }
  1176. static int alx_map_tx_skb(struct alx_tx_queue *txq, struct sk_buff *skb)
  1177. {
  1178. struct alx_txd *tpd, *first_tpd;
  1179. dma_addr_t dma;
  1180. int maplen, f, first_idx = txq->write_idx;
  1181. first_tpd = &txq->tpd[txq->write_idx];
  1182. tpd = first_tpd;
  1183. if (tpd->word1 & (1 << TPD_LSO_V2_SHIFT)) {
  1184. if (++txq->write_idx == txq->count)
  1185. txq->write_idx = 0;
  1186. tpd = &txq->tpd[txq->write_idx];
  1187. tpd->len = first_tpd->len;
  1188. tpd->vlan_tag = first_tpd->vlan_tag;
  1189. tpd->word1 = first_tpd->word1;
  1190. }
  1191. maplen = skb_headlen(skb);
  1192. dma = dma_map_single(txq->dev, skb->data, maplen,
  1193. DMA_TO_DEVICE);
  1194. if (dma_mapping_error(txq->dev, dma))
  1195. goto err_dma;
  1196. dma_unmap_len_set(&txq->bufs[txq->write_idx], size, maplen);
  1197. dma_unmap_addr_set(&txq->bufs[txq->write_idx], dma, dma);
  1198. tpd->adrl.addr = cpu_to_le64(dma);
  1199. tpd->len = cpu_to_le16(maplen);
  1200. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++) {
  1201. struct skb_frag_struct *frag;
  1202. frag = &skb_shinfo(skb)->frags[f];
  1203. if (++txq->write_idx == txq->count)
  1204. txq->write_idx = 0;
  1205. tpd = &txq->tpd[txq->write_idx];
  1206. tpd->word1 = first_tpd->word1;
  1207. maplen = skb_frag_size(frag);
  1208. dma = skb_frag_dma_map(txq->dev, frag, 0,
  1209. maplen, DMA_TO_DEVICE);
  1210. if (dma_mapping_error(txq->dev, dma))
  1211. goto err_dma;
  1212. dma_unmap_len_set(&txq->bufs[txq->write_idx], size, maplen);
  1213. dma_unmap_addr_set(&txq->bufs[txq->write_idx], dma, dma);
  1214. tpd->adrl.addr = cpu_to_le64(dma);
  1215. tpd->len = cpu_to_le16(maplen);
  1216. }
  1217. /* last TPD, set EOP flag and store skb */
  1218. tpd->word1 |= cpu_to_le32(1 << TPD_EOP_SHIFT);
  1219. txq->bufs[txq->write_idx].skb = skb;
  1220. if (++txq->write_idx == txq->count)
  1221. txq->write_idx = 0;
  1222. return 0;
  1223. err_dma:
  1224. f = first_idx;
  1225. while (f != txq->write_idx) {
  1226. alx_free_txbuf(txq, f);
  1227. if (++f == txq->count)
  1228. f = 0;
  1229. }
  1230. return -ENOMEM;
  1231. }
  1232. static netdev_tx_t alx_start_xmit_ring(struct sk_buff *skb,
  1233. struct alx_tx_queue *txq)
  1234. {
  1235. struct alx_priv *alx;
  1236. struct alx_txd *first;
  1237. int tso;
  1238. alx = netdev_priv(txq->netdev);
  1239. if (alx_tpd_avail(txq) < alx_tpd_req(skb)) {
  1240. netif_tx_stop_queue(alx_get_tx_queue(txq));
  1241. goto drop;
  1242. }
  1243. first = &txq->tpd[txq->write_idx];
  1244. memset(first, 0, sizeof(*first));
  1245. tso = alx_tso(skb, first);
  1246. if (tso < 0)
  1247. goto drop;
  1248. else if (!tso && alx_tx_csum(skb, first))
  1249. goto drop;
  1250. if (alx_map_tx_skb(txq, skb) < 0)
  1251. goto drop;
  1252. netdev_tx_sent_queue(alx_get_tx_queue(txq), skb->len);
  1253. /* flush updates before updating hardware */
  1254. wmb();
  1255. alx_write_mem16(&alx->hw, txq->p_reg, txq->write_idx);
  1256. if (alx_tpd_avail(txq) < txq->count / 8)
  1257. netif_tx_stop_queue(alx_get_tx_queue(txq));
  1258. return NETDEV_TX_OK;
  1259. drop:
  1260. dev_kfree_skb_any(skb);
  1261. return NETDEV_TX_OK;
  1262. }
  1263. static netdev_tx_t alx_start_xmit(struct sk_buff *skb,
  1264. struct net_device *netdev)
  1265. {
  1266. struct alx_priv *alx = netdev_priv(netdev);
  1267. return alx_start_xmit_ring(skb, alx_tx_queue_mapping(alx, skb));
  1268. }
  1269. static void alx_tx_timeout(struct net_device *dev)
  1270. {
  1271. struct alx_priv *alx = netdev_priv(dev);
  1272. alx_schedule_reset(alx);
  1273. }
  1274. static int alx_mdio_read(struct net_device *netdev,
  1275. int prtad, int devad, u16 addr)
  1276. {
  1277. struct alx_priv *alx = netdev_priv(netdev);
  1278. struct alx_hw *hw = &alx->hw;
  1279. u16 val;
  1280. int err;
  1281. if (prtad != hw->mdio.prtad)
  1282. return -EINVAL;
  1283. if (devad == MDIO_DEVAD_NONE)
  1284. err = alx_read_phy_reg(hw, addr, &val);
  1285. else
  1286. err = alx_read_phy_ext(hw, devad, addr, &val);
  1287. if (err)
  1288. return err;
  1289. return val;
  1290. }
  1291. static int alx_mdio_write(struct net_device *netdev,
  1292. int prtad, int devad, u16 addr, u16 val)
  1293. {
  1294. struct alx_priv *alx = netdev_priv(netdev);
  1295. struct alx_hw *hw = &alx->hw;
  1296. if (prtad != hw->mdio.prtad)
  1297. return -EINVAL;
  1298. if (devad == MDIO_DEVAD_NONE)
  1299. return alx_write_phy_reg(hw, addr, val);
  1300. return alx_write_phy_ext(hw, devad, addr, val);
  1301. }
  1302. static int alx_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  1303. {
  1304. struct alx_priv *alx = netdev_priv(netdev);
  1305. if (!netif_running(netdev))
  1306. return -EAGAIN;
  1307. return mdio_mii_ioctl(&alx->hw.mdio, if_mii(ifr), cmd);
  1308. }
  1309. #ifdef CONFIG_NET_POLL_CONTROLLER
  1310. static void alx_poll_controller(struct net_device *netdev)
  1311. {
  1312. struct alx_priv *alx = netdev_priv(netdev);
  1313. int i;
  1314. if (alx->flags & ALX_FLAG_USING_MSIX) {
  1315. alx_intr_msix_misc(0, alx);
  1316. for (i = 0; i < alx->num_txq; i++)
  1317. alx_intr_msix_ring(0, alx->qnapi[i]);
  1318. } else if (alx->flags & ALX_FLAG_USING_MSI)
  1319. alx_intr_msi(0, alx);
  1320. else
  1321. alx_intr_legacy(0, alx);
  1322. }
  1323. #endif
  1324. static struct rtnl_link_stats64 *alx_get_stats64(struct net_device *dev,
  1325. struct rtnl_link_stats64 *net_stats)
  1326. {
  1327. struct alx_priv *alx = netdev_priv(dev);
  1328. struct alx_hw_stats *hw_stats = &alx->hw.stats;
  1329. spin_lock(&alx->stats_lock);
  1330. alx_update_hw_stats(&alx->hw);
  1331. net_stats->tx_bytes = hw_stats->tx_byte_cnt;
  1332. net_stats->rx_bytes = hw_stats->rx_byte_cnt;
  1333. net_stats->multicast = hw_stats->rx_mcast;
  1334. net_stats->collisions = hw_stats->tx_single_col +
  1335. hw_stats->tx_multi_col +
  1336. hw_stats->tx_late_col +
  1337. hw_stats->tx_abort_col;
  1338. net_stats->rx_errors = hw_stats->rx_frag +
  1339. hw_stats->rx_fcs_err +
  1340. hw_stats->rx_len_err +
  1341. hw_stats->rx_ov_sz +
  1342. hw_stats->rx_ov_rrd +
  1343. hw_stats->rx_align_err +
  1344. hw_stats->rx_ov_rxf;
  1345. net_stats->rx_fifo_errors = hw_stats->rx_ov_rxf;
  1346. net_stats->rx_length_errors = hw_stats->rx_len_err;
  1347. net_stats->rx_crc_errors = hw_stats->rx_fcs_err;
  1348. net_stats->rx_frame_errors = hw_stats->rx_align_err;
  1349. net_stats->rx_dropped = hw_stats->rx_ov_rrd;
  1350. net_stats->tx_errors = hw_stats->tx_late_col +
  1351. hw_stats->tx_abort_col +
  1352. hw_stats->tx_underrun +
  1353. hw_stats->tx_trunc;
  1354. net_stats->tx_aborted_errors = hw_stats->tx_abort_col;
  1355. net_stats->tx_fifo_errors = hw_stats->tx_underrun;
  1356. net_stats->tx_window_errors = hw_stats->tx_late_col;
  1357. net_stats->tx_packets = hw_stats->tx_ok + net_stats->tx_errors;
  1358. net_stats->rx_packets = hw_stats->rx_ok + net_stats->rx_errors;
  1359. spin_unlock(&alx->stats_lock);
  1360. return net_stats;
  1361. }
  1362. static const struct net_device_ops alx_netdev_ops = {
  1363. .ndo_open = alx_open,
  1364. .ndo_stop = alx_stop,
  1365. .ndo_start_xmit = alx_start_xmit,
  1366. .ndo_get_stats64 = alx_get_stats64,
  1367. .ndo_set_rx_mode = alx_set_rx_mode,
  1368. .ndo_validate_addr = eth_validate_addr,
  1369. .ndo_set_mac_address = alx_set_mac_address,
  1370. .ndo_change_mtu = alx_change_mtu,
  1371. .ndo_do_ioctl = alx_ioctl,
  1372. .ndo_tx_timeout = alx_tx_timeout,
  1373. .ndo_fix_features = alx_fix_features,
  1374. #ifdef CONFIG_NET_POLL_CONTROLLER
  1375. .ndo_poll_controller = alx_poll_controller,
  1376. #endif
  1377. };
  1378. static int alx_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1379. {
  1380. struct net_device *netdev;
  1381. struct alx_priv *alx;
  1382. struct alx_hw *hw;
  1383. bool phy_configured;
  1384. int err;
  1385. err = pci_enable_device_mem(pdev);
  1386. if (err)
  1387. return err;
  1388. /* The alx chip can DMA to 64-bit addresses, but it uses a single
  1389. * shared register for the high 32 bits, so only a single, aligned,
  1390. * 4 GB physical address range can be used for descriptors.
  1391. */
  1392. if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
  1393. dev_dbg(&pdev->dev, "DMA to 64-BIT addresses\n");
  1394. } else {
  1395. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  1396. if (err) {
  1397. dev_err(&pdev->dev, "No usable DMA config, aborting\n");
  1398. goto out_pci_disable;
  1399. }
  1400. }
  1401. err = pci_request_mem_regions(pdev, alx_drv_name);
  1402. if (err) {
  1403. dev_err(&pdev->dev,
  1404. "pci_request_mem_regions failed\n");
  1405. goto out_pci_disable;
  1406. }
  1407. pci_enable_pcie_error_reporting(pdev);
  1408. pci_set_master(pdev);
  1409. if (!pdev->pm_cap) {
  1410. dev_err(&pdev->dev,
  1411. "Can't find power management capability, aborting\n");
  1412. err = -EIO;
  1413. goto out_pci_release;
  1414. }
  1415. netdev = alloc_etherdev_mqs(sizeof(*alx),
  1416. ALX_MAX_TX_QUEUES, 1);
  1417. if (!netdev) {
  1418. err = -ENOMEM;
  1419. goto out_pci_release;
  1420. }
  1421. SET_NETDEV_DEV(netdev, &pdev->dev);
  1422. alx = netdev_priv(netdev);
  1423. spin_lock_init(&alx->hw.mdio_lock);
  1424. spin_lock_init(&alx->irq_lock);
  1425. spin_lock_init(&alx->stats_lock);
  1426. alx->dev = netdev;
  1427. alx->hw.pdev = pdev;
  1428. alx->msg_enable = NETIF_MSG_LINK | NETIF_MSG_HW | NETIF_MSG_IFUP |
  1429. NETIF_MSG_TX_ERR | NETIF_MSG_RX_ERR | NETIF_MSG_WOL;
  1430. hw = &alx->hw;
  1431. pci_set_drvdata(pdev, alx);
  1432. hw->hw_addr = pci_ioremap_bar(pdev, 0);
  1433. if (!hw->hw_addr) {
  1434. dev_err(&pdev->dev, "cannot map device registers\n");
  1435. err = -EIO;
  1436. goto out_free_netdev;
  1437. }
  1438. netdev->netdev_ops = &alx_netdev_ops;
  1439. netdev->ethtool_ops = &alx_ethtool_ops;
  1440. netdev->irq = pdev->irq;
  1441. netdev->watchdog_timeo = ALX_WATCHDOG_TIME;
  1442. if (ent->driver_data & ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG)
  1443. pdev->dev_flags |= PCI_DEV_FLAGS_MSI_INTX_DISABLE_BUG;
  1444. err = alx_init_sw(alx);
  1445. if (err) {
  1446. dev_err(&pdev->dev, "net device private data init failed\n");
  1447. goto out_unmap;
  1448. }
  1449. alx_reset_pcie(hw);
  1450. phy_configured = alx_phy_configured(hw);
  1451. if (!phy_configured)
  1452. alx_reset_phy(hw);
  1453. err = alx_reset_mac(hw);
  1454. if (err) {
  1455. dev_err(&pdev->dev, "MAC Reset failed, error = %d\n", err);
  1456. goto out_unmap;
  1457. }
  1458. /* setup link to put it in a known good starting state */
  1459. if (!phy_configured) {
  1460. err = alx_setup_speed_duplex(hw, hw->adv_cfg, hw->flowctrl);
  1461. if (err) {
  1462. dev_err(&pdev->dev,
  1463. "failed to configure PHY speed/duplex (err=%d)\n",
  1464. err);
  1465. goto out_unmap;
  1466. }
  1467. }
  1468. netdev->hw_features = NETIF_F_SG |
  1469. NETIF_F_HW_CSUM |
  1470. NETIF_F_TSO |
  1471. NETIF_F_TSO6;
  1472. if (alx_get_perm_macaddr(hw, hw->perm_addr)) {
  1473. dev_warn(&pdev->dev,
  1474. "Invalid permanent address programmed, using random one\n");
  1475. eth_hw_addr_random(netdev);
  1476. memcpy(hw->perm_addr, netdev->dev_addr, netdev->addr_len);
  1477. }
  1478. memcpy(hw->mac_addr, hw->perm_addr, ETH_ALEN);
  1479. memcpy(netdev->dev_addr, hw->mac_addr, ETH_ALEN);
  1480. memcpy(netdev->perm_addr, hw->perm_addr, ETH_ALEN);
  1481. hw->mdio.prtad = 0;
  1482. hw->mdio.mmds = 0;
  1483. hw->mdio.dev = netdev;
  1484. hw->mdio.mode_support = MDIO_SUPPORTS_C45 |
  1485. MDIO_SUPPORTS_C22 |
  1486. MDIO_EMULATE_C22;
  1487. hw->mdio.mdio_read = alx_mdio_read;
  1488. hw->mdio.mdio_write = alx_mdio_write;
  1489. if (!alx_get_phy_info(hw)) {
  1490. dev_err(&pdev->dev, "failed to identify PHY\n");
  1491. err = -EIO;
  1492. goto out_unmap;
  1493. }
  1494. INIT_WORK(&alx->link_check_wk, alx_link_check);
  1495. INIT_WORK(&alx->reset_wk, alx_reset);
  1496. netif_carrier_off(netdev);
  1497. err = register_netdev(netdev);
  1498. if (err) {
  1499. dev_err(&pdev->dev, "register netdevice failed\n");
  1500. goto out_unmap;
  1501. }
  1502. netdev_info(netdev,
  1503. "Qualcomm Atheros AR816x/AR817x Ethernet [%pM]\n",
  1504. netdev->dev_addr);
  1505. return 0;
  1506. out_unmap:
  1507. iounmap(hw->hw_addr);
  1508. out_free_netdev:
  1509. free_netdev(netdev);
  1510. out_pci_release:
  1511. pci_release_mem_regions(pdev);
  1512. out_pci_disable:
  1513. pci_disable_device(pdev);
  1514. return err;
  1515. }
  1516. static void alx_remove(struct pci_dev *pdev)
  1517. {
  1518. struct alx_priv *alx = pci_get_drvdata(pdev);
  1519. struct alx_hw *hw = &alx->hw;
  1520. cancel_work_sync(&alx->link_check_wk);
  1521. cancel_work_sync(&alx->reset_wk);
  1522. /* restore permanent mac address */
  1523. alx_set_macaddr(hw, hw->perm_addr);
  1524. unregister_netdev(alx->dev);
  1525. iounmap(hw->hw_addr);
  1526. pci_release_mem_regions(pdev);
  1527. pci_disable_pcie_error_reporting(pdev);
  1528. pci_disable_device(pdev);
  1529. free_netdev(alx->dev);
  1530. }
  1531. #ifdef CONFIG_PM_SLEEP
  1532. static int alx_suspend(struct device *dev)
  1533. {
  1534. struct pci_dev *pdev = to_pci_dev(dev);
  1535. struct alx_priv *alx = pci_get_drvdata(pdev);
  1536. if (!netif_running(alx->dev))
  1537. return 0;
  1538. netif_device_detach(alx->dev);
  1539. __alx_stop(alx);
  1540. return 0;
  1541. }
  1542. static int alx_resume(struct device *dev)
  1543. {
  1544. struct pci_dev *pdev = to_pci_dev(dev);
  1545. struct alx_priv *alx = pci_get_drvdata(pdev);
  1546. struct alx_hw *hw = &alx->hw;
  1547. alx_reset_phy(hw);
  1548. if (!netif_running(alx->dev))
  1549. return 0;
  1550. netif_device_attach(alx->dev);
  1551. return __alx_open(alx, true);
  1552. }
  1553. static SIMPLE_DEV_PM_OPS(alx_pm_ops, alx_suspend, alx_resume);
  1554. #define ALX_PM_OPS (&alx_pm_ops)
  1555. #else
  1556. #define ALX_PM_OPS NULL
  1557. #endif
  1558. static pci_ers_result_t alx_pci_error_detected(struct pci_dev *pdev,
  1559. pci_channel_state_t state)
  1560. {
  1561. struct alx_priv *alx = pci_get_drvdata(pdev);
  1562. struct net_device *netdev = alx->dev;
  1563. pci_ers_result_t rc = PCI_ERS_RESULT_NEED_RESET;
  1564. dev_info(&pdev->dev, "pci error detected\n");
  1565. rtnl_lock();
  1566. if (netif_running(netdev)) {
  1567. netif_device_detach(netdev);
  1568. alx_halt(alx);
  1569. }
  1570. if (state == pci_channel_io_perm_failure)
  1571. rc = PCI_ERS_RESULT_DISCONNECT;
  1572. else
  1573. pci_disable_device(pdev);
  1574. rtnl_unlock();
  1575. return rc;
  1576. }
  1577. static pci_ers_result_t alx_pci_error_slot_reset(struct pci_dev *pdev)
  1578. {
  1579. struct alx_priv *alx = pci_get_drvdata(pdev);
  1580. struct alx_hw *hw = &alx->hw;
  1581. pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
  1582. dev_info(&pdev->dev, "pci error slot reset\n");
  1583. rtnl_lock();
  1584. if (pci_enable_device(pdev)) {
  1585. dev_err(&pdev->dev, "Failed to re-enable PCI device after reset\n");
  1586. goto out;
  1587. }
  1588. pci_set_master(pdev);
  1589. alx_reset_pcie(hw);
  1590. if (!alx_reset_mac(hw))
  1591. rc = PCI_ERS_RESULT_RECOVERED;
  1592. out:
  1593. pci_cleanup_aer_uncorrect_error_status(pdev);
  1594. rtnl_unlock();
  1595. return rc;
  1596. }
  1597. static void alx_pci_error_resume(struct pci_dev *pdev)
  1598. {
  1599. struct alx_priv *alx = pci_get_drvdata(pdev);
  1600. struct net_device *netdev = alx->dev;
  1601. dev_info(&pdev->dev, "pci error resume\n");
  1602. rtnl_lock();
  1603. if (netif_running(netdev)) {
  1604. alx_activate(alx);
  1605. netif_device_attach(netdev);
  1606. }
  1607. rtnl_unlock();
  1608. }
  1609. static const struct pci_error_handlers alx_err_handlers = {
  1610. .error_detected = alx_pci_error_detected,
  1611. .slot_reset = alx_pci_error_slot_reset,
  1612. .resume = alx_pci_error_resume,
  1613. };
  1614. static const struct pci_device_id alx_pci_tbl[] = {
  1615. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_AR8161),
  1616. .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
  1617. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_E2200),
  1618. .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
  1619. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_E2400),
  1620. .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
  1621. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_E2500),
  1622. .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
  1623. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_AR8162),
  1624. .driver_data = ALX_DEV_QUIRK_MSI_INTX_DISABLE_BUG },
  1625. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_AR8171) },
  1626. { PCI_VDEVICE(ATTANSIC, ALX_DEV_ID_AR8172) },
  1627. {}
  1628. };
  1629. static struct pci_driver alx_driver = {
  1630. .name = alx_drv_name,
  1631. .id_table = alx_pci_tbl,
  1632. .probe = alx_probe,
  1633. .remove = alx_remove,
  1634. .err_handler = &alx_err_handlers,
  1635. .driver.pm = ALX_PM_OPS,
  1636. };
  1637. module_pci_driver(alx_driver);
  1638. MODULE_DEVICE_TABLE(pci, alx_pci_tbl);
  1639. MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
  1640. MODULE_AUTHOR("Qualcomm Corporation, <nic-devel@qualcomm.com>");
  1641. MODULE_DESCRIPTION(
  1642. "Qualcomm Atheros(R) AR816x/AR817x PCI-E Ethernet Network Driver");
  1643. MODULE_LICENSE("GPL");