atl1e_main.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582
  1. /*
  2. * Copyright(c) 2007 Atheros Corporation. All rights reserved.
  3. *
  4. * Derived from Intel e1000 driver
  5. * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc., 59
  19. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. #include "atl1e.h"
  22. #define DRV_VERSION "1.0.0.7-NAPI"
  23. char atl1e_driver_name[] = "ATL1E";
  24. char atl1e_driver_version[] = DRV_VERSION;
  25. #define PCI_DEVICE_ID_ATTANSIC_L1E 0x1026
  26. /*
  27. * atl1e_pci_tbl - PCI Device ID Table
  28. *
  29. * Wildcard entries (PCI_ANY_ID) should come last
  30. * Last entry must be all 0s
  31. *
  32. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  33. * Class, Class Mask, private data (not used) }
  34. */
  35. static DEFINE_PCI_DEVICE_TABLE(atl1e_pci_tbl) = {
  36. {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATTANSIC_L1E)},
  37. {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, 0x1066)},
  38. /* required last entry */
  39. { 0 }
  40. };
  41. MODULE_DEVICE_TABLE(pci, atl1e_pci_tbl);
  42. MODULE_AUTHOR("Atheros Corporation, <xiong.huang@atheros.com>, Jie Yang <jie.yang@atheros.com>");
  43. MODULE_DESCRIPTION("Atheros 1000M Ethernet Network Driver");
  44. MODULE_LICENSE("GPL");
  45. MODULE_VERSION(DRV_VERSION);
  46. static void atl1e_setup_mac_ctrl(struct atl1e_adapter *adapter);
  47. static const u16
  48. atl1e_rx_page_vld_regs[AT_MAX_RECEIVE_QUEUE][AT_PAGE_NUM_PER_QUEUE] =
  49. {
  50. {REG_HOST_RXF0_PAGE0_VLD, REG_HOST_RXF0_PAGE1_VLD},
  51. {REG_HOST_RXF1_PAGE0_VLD, REG_HOST_RXF1_PAGE1_VLD},
  52. {REG_HOST_RXF2_PAGE0_VLD, REG_HOST_RXF2_PAGE1_VLD},
  53. {REG_HOST_RXF3_PAGE0_VLD, REG_HOST_RXF3_PAGE1_VLD}
  54. };
  55. static const u16 atl1e_rx_page_hi_addr_regs[AT_MAX_RECEIVE_QUEUE] =
  56. {
  57. REG_RXF0_BASE_ADDR_HI,
  58. REG_RXF1_BASE_ADDR_HI,
  59. REG_RXF2_BASE_ADDR_HI,
  60. REG_RXF3_BASE_ADDR_HI
  61. };
  62. static const u16
  63. atl1e_rx_page_lo_addr_regs[AT_MAX_RECEIVE_QUEUE][AT_PAGE_NUM_PER_QUEUE] =
  64. {
  65. {REG_HOST_RXF0_PAGE0_LO, REG_HOST_RXF0_PAGE1_LO},
  66. {REG_HOST_RXF1_PAGE0_LO, REG_HOST_RXF1_PAGE1_LO},
  67. {REG_HOST_RXF2_PAGE0_LO, REG_HOST_RXF2_PAGE1_LO},
  68. {REG_HOST_RXF3_PAGE0_LO, REG_HOST_RXF3_PAGE1_LO}
  69. };
  70. static const u16
  71. atl1e_rx_page_write_offset_regs[AT_MAX_RECEIVE_QUEUE][AT_PAGE_NUM_PER_QUEUE] =
  72. {
  73. {REG_HOST_RXF0_MB0_LO, REG_HOST_RXF0_MB1_LO},
  74. {REG_HOST_RXF1_MB0_LO, REG_HOST_RXF1_MB1_LO},
  75. {REG_HOST_RXF2_MB0_LO, REG_HOST_RXF2_MB1_LO},
  76. {REG_HOST_RXF3_MB0_LO, REG_HOST_RXF3_MB1_LO}
  77. };
  78. static const u16 atl1e_pay_load_size[] = {
  79. 128, 256, 512, 1024, 2048, 4096,
  80. };
  81. /**
  82. * atl1e_irq_enable - Enable default interrupt generation settings
  83. * @adapter: board private structure
  84. */
  85. static inline void atl1e_irq_enable(struct atl1e_adapter *adapter)
  86. {
  87. if (likely(atomic_dec_and_test(&adapter->irq_sem))) {
  88. AT_WRITE_REG(&adapter->hw, REG_ISR, 0);
  89. AT_WRITE_REG(&adapter->hw, REG_IMR, IMR_NORMAL_MASK);
  90. AT_WRITE_FLUSH(&adapter->hw);
  91. }
  92. }
  93. /**
  94. * atl1e_irq_disable - Mask off interrupt generation on the NIC
  95. * @adapter: board private structure
  96. */
  97. static inline void atl1e_irq_disable(struct atl1e_adapter *adapter)
  98. {
  99. atomic_inc(&adapter->irq_sem);
  100. AT_WRITE_REG(&adapter->hw, REG_IMR, 0);
  101. AT_WRITE_FLUSH(&adapter->hw);
  102. synchronize_irq(adapter->pdev->irq);
  103. }
  104. /**
  105. * atl1e_irq_reset - reset interrupt confiure on the NIC
  106. * @adapter: board private structure
  107. */
  108. static inline void atl1e_irq_reset(struct atl1e_adapter *adapter)
  109. {
  110. atomic_set(&adapter->irq_sem, 0);
  111. AT_WRITE_REG(&adapter->hw, REG_ISR, 0);
  112. AT_WRITE_REG(&adapter->hw, REG_IMR, 0);
  113. AT_WRITE_FLUSH(&adapter->hw);
  114. }
  115. /**
  116. * atl1e_phy_config - Timer Call-back
  117. * @data: pointer to netdev cast into an unsigned long
  118. */
  119. static void atl1e_phy_config(unsigned long data)
  120. {
  121. struct atl1e_adapter *adapter = (struct atl1e_adapter *) data;
  122. struct atl1e_hw *hw = &adapter->hw;
  123. unsigned long flags;
  124. spin_lock_irqsave(&adapter->mdio_lock, flags);
  125. atl1e_restart_autoneg(hw);
  126. spin_unlock_irqrestore(&adapter->mdio_lock, flags);
  127. }
  128. void atl1e_reinit_locked(struct atl1e_adapter *adapter)
  129. {
  130. WARN_ON(in_interrupt());
  131. while (test_and_set_bit(__AT_RESETTING, &adapter->flags))
  132. msleep(1);
  133. atl1e_down(adapter);
  134. atl1e_up(adapter);
  135. clear_bit(__AT_RESETTING, &adapter->flags);
  136. }
  137. static void atl1e_reset_task(struct work_struct *work)
  138. {
  139. struct atl1e_adapter *adapter;
  140. adapter = container_of(work, struct atl1e_adapter, reset_task);
  141. atl1e_reinit_locked(adapter);
  142. }
  143. static int atl1e_check_link(struct atl1e_adapter *adapter)
  144. {
  145. struct atl1e_hw *hw = &adapter->hw;
  146. struct net_device *netdev = adapter->netdev;
  147. int err = 0;
  148. u16 speed, duplex, phy_data;
  149. /* MII_BMSR must read twice */
  150. atl1e_read_phy_reg(hw, MII_BMSR, &phy_data);
  151. atl1e_read_phy_reg(hw, MII_BMSR, &phy_data);
  152. if ((phy_data & BMSR_LSTATUS) == 0) {
  153. /* link down */
  154. if (netif_carrier_ok(netdev)) { /* old link state: Up */
  155. u32 value;
  156. /* disable rx */
  157. value = AT_READ_REG(hw, REG_MAC_CTRL);
  158. value &= ~MAC_CTRL_RX_EN;
  159. AT_WRITE_REG(hw, REG_MAC_CTRL, value);
  160. adapter->link_speed = SPEED_0;
  161. netif_carrier_off(netdev);
  162. netif_stop_queue(netdev);
  163. }
  164. } else {
  165. /* Link Up */
  166. err = atl1e_get_speed_and_duplex(hw, &speed, &duplex);
  167. if (unlikely(err))
  168. return err;
  169. /* link result is our setting */
  170. if (adapter->link_speed != speed ||
  171. adapter->link_duplex != duplex) {
  172. adapter->link_speed = speed;
  173. adapter->link_duplex = duplex;
  174. atl1e_setup_mac_ctrl(adapter);
  175. netdev_info(netdev,
  176. "NIC Link is Up <%d Mbps %s Duplex>\n",
  177. adapter->link_speed,
  178. adapter->link_duplex == FULL_DUPLEX ?
  179. "Full" : "Half");
  180. }
  181. if (!netif_carrier_ok(netdev)) {
  182. /* Link down -> Up */
  183. netif_carrier_on(netdev);
  184. netif_wake_queue(netdev);
  185. }
  186. }
  187. return 0;
  188. }
  189. /**
  190. * atl1e_link_chg_task - deal with link change event Out of interrupt context
  191. * @netdev: network interface device structure
  192. */
  193. static void atl1e_link_chg_task(struct work_struct *work)
  194. {
  195. struct atl1e_adapter *adapter;
  196. unsigned long flags;
  197. adapter = container_of(work, struct atl1e_adapter, link_chg_task);
  198. spin_lock_irqsave(&adapter->mdio_lock, flags);
  199. atl1e_check_link(adapter);
  200. spin_unlock_irqrestore(&adapter->mdio_lock, flags);
  201. }
  202. static void atl1e_link_chg_event(struct atl1e_adapter *adapter)
  203. {
  204. struct net_device *netdev = adapter->netdev;
  205. u16 phy_data = 0;
  206. u16 link_up = 0;
  207. spin_lock(&adapter->mdio_lock);
  208. atl1e_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
  209. atl1e_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
  210. spin_unlock(&adapter->mdio_lock);
  211. link_up = phy_data & BMSR_LSTATUS;
  212. /* notify upper layer link down ASAP */
  213. if (!link_up) {
  214. if (netif_carrier_ok(netdev)) {
  215. /* old link state: Up */
  216. netdev_info(netdev, "NIC Link is Down\n");
  217. adapter->link_speed = SPEED_0;
  218. netif_stop_queue(netdev);
  219. }
  220. }
  221. schedule_work(&adapter->link_chg_task);
  222. }
  223. static void atl1e_del_timer(struct atl1e_adapter *adapter)
  224. {
  225. del_timer_sync(&adapter->phy_config_timer);
  226. }
  227. static void atl1e_cancel_work(struct atl1e_adapter *adapter)
  228. {
  229. cancel_work_sync(&adapter->reset_task);
  230. cancel_work_sync(&adapter->link_chg_task);
  231. }
  232. /**
  233. * atl1e_tx_timeout - Respond to a Tx Hang
  234. * @netdev: network interface device structure
  235. */
  236. static void atl1e_tx_timeout(struct net_device *netdev)
  237. {
  238. struct atl1e_adapter *adapter = netdev_priv(netdev);
  239. /* Do the reset outside of interrupt context */
  240. schedule_work(&adapter->reset_task);
  241. }
  242. /**
  243. * atl1e_set_multi - Multicast and Promiscuous mode set
  244. * @netdev: network interface device structure
  245. *
  246. * The set_multi entry point is called whenever the multicast address
  247. * list or the network interface flags are updated. This routine is
  248. * responsible for configuring the hardware for proper multicast,
  249. * promiscuous mode, and all-multi behavior.
  250. */
  251. static void atl1e_set_multi(struct net_device *netdev)
  252. {
  253. struct atl1e_adapter *adapter = netdev_priv(netdev);
  254. struct atl1e_hw *hw = &adapter->hw;
  255. struct netdev_hw_addr *ha;
  256. u32 mac_ctrl_data = 0;
  257. u32 hash_value;
  258. /* Check for Promiscuous and All Multicast modes */
  259. mac_ctrl_data = AT_READ_REG(hw, REG_MAC_CTRL);
  260. if (netdev->flags & IFF_PROMISC) {
  261. mac_ctrl_data |= MAC_CTRL_PROMIS_EN;
  262. } else if (netdev->flags & IFF_ALLMULTI) {
  263. mac_ctrl_data |= MAC_CTRL_MC_ALL_EN;
  264. mac_ctrl_data &= ~MAC_CTRL_PROMIS_EN;
  265. } else {
  266. mac_ctrl_data &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN);
  267. }
  268. AT_WRITE_REG(hw, REG_MAC_CTRL, mac_ctrl_data);
  269. /* clear the old settings from the multicast hash table */
  270. AT_WRITE_REG(hw, REG_RX_HASH_TABLE, 0);
  271. AT_WRITE_REG_ARRAY(hw, REG_RX_HASH_TABLE, 1, 0);
  272. /* comoute mc addresses' hash value ,and put it into hash table */
  273. netdev_for_each_mc_addr(ha, netdev) {
  274. hash_value = atl1e_hash_mc_addr(hw, ha->addr);
  275. atl1e_hash_set(hw, hash_value);
  276. }
  277. }
  278. static void __atl1e_rx_mode(netdev_features_t features, u32 *mac_ctrl_data)
  279. {
  280. if (features & NETIF_F_RXALL) {
  281. /* enable RX of ALL frames */
  282. *mac_ctrl_data |= MAC_CTRL_DBG;
  283. } else {
  284. /* disable RX of ALL frames */
  285. *mac_ctrl_data &= ~MAC_CTRL_DBG;
  286. }
  287. }
  288. static void atl1e_rx_mode(struct net_device *netdev,
  289. netdev_features_t features)
  290. {
  291. struct atl1e_adapter *adapter = netdev_priv(netdev);
  292. u32 mac_ctrl_data = 0;
  293. netdev_dbg(adapter->netdev, "%s\n", __func__);
  294. atl1e_irq_disable(adapter);
  295. mac_ctrl_data = AT_READ_REG(&adapter->hw, REG_MAC_CTRL);
  296. __atl1e_rx_mode(features, &mac_ctrl_data);
  297. AT_WRITE_REG(&adapter->hw, REG_MAC_CTRL, mac_ctrl_data);
  298. atl1e_irq_enable(adapter);
  299. }
  300. static void __atl1e_vlan_mode(netdev_features_t features, u32 *mac_ctrl_data)
  301. {
  302. if (features & NETIF_F_HW_VLAN_CTAG_RX) {
  303. /* enable VLAN tag insert/strip */
  304. *mac_ctrl_data |= MAC_CTRL_RMV_VLAN;
  305. } else {
  306. /* disable VLAN tag insert/strip */
  307. *mac_ctrl_data &= ~MAC_CTRL_RMV_VLAN;
  308. }
  309. }
  310. static void atl1e_vlan_mode(struct net_device *netdev,
  311. netdev_features_t features)
  312. {
  313. struct atl1e_adapter *adapter = netdev_priv(netdev);
  314. u32 mac_ctrl_data = 0;
  315. netdev_dbg(adapter->netdev, "%s\n", __func__);
  316. atl1e_irq_disable(adapter);
  317. mac_ctrl_data = AT_READ_REG(&adapter->hw, REG_MAC_CTRL);
  318. __atl1e_vlan_mode(features, &mac_ctrl_data);
  319. AT_WRITE_REG(&adapter->hw, REG_MAC_CTRL, mac_ctrl_data);
  320. atl1e_irq_enable(adapter);
  321. }
  322. static void atl1e_restore_vlan(struct atl1e_adapter *adapter)
  323. {
  324. netdev_dbg(adapter->netdev, "%s\n", __func__);
  325. atl1e_vlan_mode(adapter->netdev, adapter->netdev->features);
  326. }
  327. /**
  328. * atl1e_set_mac - Change the Ethernet Address of the NIC
  329. * @netdev: network interface device structure
  330. * @p: pointer to an address structure
  331. *
  332. * Returns 0 on success, negative on failure
  333. */
  334. static int atl1e_set_mac_addr(struct net_device *netdev, void *p)
  335. {
  336. struct atl1e_adapter *adapter = netdev_priv(netdev);
  337. struct sockaddr *addr = p;
  338. if (!is_valid_ether_addr(addr->sa_data))
  339. return -EADDRNOTAVAIL;
  340. if (netif_running(netdev))
  341. return -EBUSY;
  342. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  343. memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
  344. atl1e_hw_set_mac_addr(&adapter->hw);
  345. return 0;
  346. }
  347. static netdev_features_t atl1e_fix_features(struct net_device *netdev,
  348. netdev_features_t features)
  349. {
  350. /*
  351. * Since there is no support for separate rx/tx vlan accel
  352. * enable/disable make sure tx flag is always in same state as rx.
  353. */
  354. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  355. features |= NETIF_F_HW_VLAN_CTAG_TX;
  356. else
  357. features &= ~NETIF_F_HW_VLAN_CTAG_TX;
  358. return features;
  359. }
  360. static int atl1e_set_features(struct net_device *netdev,
  361. netdev_features_t features)
  362. {
  363. netdev_features_t changed = netdev->features ^ features;
  364. if (changed & NETIF_F_HW_VLAN_CTAG_RX)
  365. atl1e_vlan_mode(netdev, features);
  366. if (changed & NETIF_F_RXALL)
  367. atl1e_rx_mode(netdev, features);
  368. return 0;
  369. }
  370. /**
  371. * atl1e_change_mtu - Change the Maximum Transfer Unit
  372. * @netdev: network interface device structure
  373. * @new_mtu: new value for maximum frame size
  374. *
  375. * Returns 0 on success, negative on failure
  376. */
  377. static int atl1e_change_mtu(struct net_device *netdev, int new_mtu)
  378. {
  379. struct atl1e_adapter *adapter = netdev_priv(netdev);
  380. int old_mtu = netdev->mtu;
  381. int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  382. if ((max_frame < ETH_ZLEN + ETH_FCS_LEN) ||
  383. (max_frame > MAX_JUMBO_FRAME_SIZE)) {
  384. netdev_warn(adapter->netdev, "invalid MTU setting\n");
  385. return -EINVAL;
  386. }
  387. /* set MTU */
  388. if (old_mtu != new_mtu && netif_running(netdev)) {
  389. while (test_and_set_bit(__AT_RESETTING, &adapter->flags))
  390. msleep(1);
  391. netdev->mtu = new_mtu;
  392. adapter->hw.max_frame_size = new_mtu;
  393. adapter->hw.rx_jumbo_th = (max_frame + 7) >> 3;
  394. atl1e_down(adapter);
  395. atl1e_up(adapter);
  396. clear_bit(__AT_RESETTING, &adapter->flags);
  397. }
  398. return 0;
  399. }
  400. /*
  401. * caller should hold mdio_lock
  402. */
  403. static int atl1e_mdio_read(struct net_device *netdev, int phy_id, int reg_num)
  404. {
  405. struct atl1e_adapter *adapter = netdev_priv(netdev);
  406. u16 result;
  407. atl1e_read_phy_reg(&adapter->hw, reg_num & MDIO_REG_ADDR_MASK, &result);
  408. return result;
  409. }
  410. static void atl1e_mdio_write(struct net_device *netdev, int phy_id,
  411. int reg_num, int val)
  412. {
  413. struct atl1e_adapter *adapter = netdev_priv(netdev);
  414. atl1e_write_phy_reg(&adapter->hw, reg_num & MDIO_REG_ADDR_MASK, val);
  415. }
  416. static int atl1e_mii_ioctl(struct net_device *netdev,
  417. struct ifreq *ifr, int cmd)
  418. {
  419. struct atl1e_adapter *adapter = netdev_priv(netdev);
  420. struct mii_ioctl_data *data = if_mii(ifr);
  421. unsigned long flags;
  422. int retval = 0;
  423. if (!netif_running(netdev))
  424. return -EINVAL;
  425. spin_lock_irqsave(&adapter->mdio_lock, flags);
  426. switch (cmd) {
  427. case SIOCGMIIPHY:
  428. data->phy_id = 0;
  429. break;
  430. case SIOCGMIIREG:
  431. if (atl1e_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
  432. &data->val_out)) {
  433. retval = -EIO;
  434. goto out;
  435. }
  436. break;
  437. case SIOCSMIIREG:
  438. if (data->reg_num & ~(0x1F)) {
  439. retval = -EFAULT;
  440. goto out;
  441. }
  442. netdev_dbg(adapter->netdev, "<atl1e_mii_ioctl> write %x %x\n",
  443. data->reg_num, data->val_in);
  444. if (atl1e_write_phy_reg(&adapter->hw,
  445. data->reg_num, data->val_in)) {
  446. retval = -EIO;
  447. goto out;
  448. }
  449. break;
  450. default:
  451. retval = -EOPNOTSUPP;
  452. break;
  453. }
  454. out:
  455. spin_unlock_irqrestore(&adapter->mdio_lock, flags);
  456. return retval;
  457. }
  458. static int atl1e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  459. {
  460. switch (cmd) {
  461. case SIOCGMIIPHY:
  462. case SIOCGMIIREG:
  463. case SIOCSMIIREG:
  464. return atl1e_mii_ioctl(netdev, ifr, cmd);
  465. default:
  466. return -EOPNOTSUPP;
  467. }
  468. }
  469. static void atl1e_setup_pcicmd(struct pci_dev *pdev)
  470. {
  471. u16 cmd;
  472. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  473. cmd &= ~(PCI_COMMAND_INTX_DISABLE | PCI_COMMAND_IO);
  474. cmd |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  475. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  476. /*
  477. * some motherboards BIOS(PXE/EFI) driver may set PME
  478. * while they transfer control to OS (Windows/Linux)
  479. * so we should clear this bit before NIC work normally
  480. */
  481. pci_write_config_dword(pdev, REG_PM_CTRLSTAT, 0);
  482. msleep(1);
  483. }
  484. /**
  485. * atl1e_alloc_queues - Allocate memory for all rings
  486. * @adapter: board private structure to initialize
  487. *
  488. */
  489. static int atl1e_alloc_queues(struct atl1e_adapter *adapter)
  490. {
  491. return 0;
  492. }
  493. /**
  494. * atl1e_sw_init - Initialize general software structures (struct atl1e_adapter)
  495. * @adapter: board private structure to initialize
  496. *
  497. * atl1e_sw_init initializes the Adapter private data structure.
  498. * Fields are initialized based on PCI device information and
  499. * OS network device settings (MTU size).
  500. */
  501. static int atl1e_sw_init(struct atl1e_adapter *adapter)
  502. {
  503. struct atl1e_hw *hw = &adapter->hw;
  504. struct pci_dev *pdev = adapter->pdev;
  505. u32 phy_status_data = 0;
  506. adapter->wol = 0;
  507. adapter->link_speed = SPEED_0; /* hardware init */
  508. adapter->link_duplex = FULL_DUPLEX;
  509. adapter->num_rx_queues = 1;
  510. /* PCI config space info */
  511. hw->vendor_id = pdev->vendor;
  512. hw->device_id = pdev->device;
  513. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  514. hw->subsystem_id = pdev->subsystem_device;
  515. hw->revision_id = pdev->revision;
  516. pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
  517. phy_status_data = AT_READ_REG(hw, REG_PHY_STATUS);
  518. /* nic type */
  519. if (hw->revision_id >= 0xF0) {
  520. hw->nic_type = athr_l2e_revB;
  521. } else {
  522. if (phy_status_data & PHY_STATUS_100M)
  523. hw->nic_type = athr_l1e;
  524. else
  525. hw->nic_type = athr_l2e_revA;
  526. }
  527. phy_status_data = AT_READ_REG(hw, REG_PHY_STATUS);
  528. if (phy_status_data & PHY_STATUS_EMI_CA)
  529. hw->emi_ca = true;
  530. else
  531. hw->emi_ca = false;
  532. hw->phy_configured = false;
  533. hw->preamble_len = 7;
  534. hw->max_frame_size = adapter->netdev->mtu;
  535. hw->rx_jumbo_th = (hw->max_frame_size + ETH_HLEN +
  536. VLAN_HLEN + ETH_FCS_LEN + 7) >> 3;
  537. hw->rrs_type = atl1e_rrs_disable;
  538. hw->indirect_tab = 0;
  539. hw->base_cpu = 0;
  540. /* need confirm */
  541. hw->ict = 50000; /* 100ms */
  542. hw->smb_timer = 200000; /* 200ms */
  543. hw->tpd_burst = 5;
  544. hw->rrd_thresh = 1;
  545. hw->tpd_thresh = adapter->tx_ring.count / 2;
  546. hw->rx_count_down = 4; /* 2us resolution */
  547. hw->tx_count_down = hw->imt * 4 / 3;
  548. hw->dmar_block = atl1e_dma_req_1024;
  549. hw->dmaw_block = atl1e_dma_req_1024;
  550. hw->dmar_dly_cnt = 15;
  551. hw->dmaw_dly_cnt = 4;
  552. if (atl1e_alloc_queues(adapter)) {
  553. netdev_err(adapter->netdev, "Unable to allocate memory for queues\n");
  554. return -ENOMEM;
  555. }
  556. atomic_set(&adapter->irq_sem, 1);
  557. spin_lock_init(&adapter->mdio_lock);
  558. spin_lock_init(&adapter->tx_lock);
  559. set_bit(__AT_DOWN, &adapter->flags);
  560. return 0;
  561. }
  562. /**
  563. * atl1e_clean_tx_ring - Free Tx-skb
  564. * @adapter: board private structure
  565. */
  566. static void atl1e_clean_tx_ring(struct atl1e_adapter *adapter)
  567. {
  568. struct atl1e_tx_ring *tx_ring = &adapter->tx_ring;
  569. struct atl1e_tx_buffer *tx_buffer = NULL;
  570. struct pci_dev *pdev = adapter->pdev;
  571. u16 index, ring_count;
  572. if (tx_ring->desc == NULL || tx_ring->tx_buffer == NULL)
  573. return;
  574. ring_count = tx_ring->count;
  575. /* first unmmap dma */
  576. for (index = 0; index < ring_count; index++) {
  577. tx_buffer = &tx_ring->tx_buffer[index];
  578. if (tx_buffer->dma) {
  579. if (tx_buffer->flags & ATL1E_TX_PCIMAP_SINGLE)
  580. pci_unmap_single(pdev, tx_buffer->dma,
  581. tx_buffer->length, PCI_DMA_TODEVICE);
  582. else if (tx_buffer->flags & ATL1E_TX_PCIMAP_PAGE)
  583. pci_unmap_page(pdev, tx_buffer->dma,
  584. tx_buffer->length, PCI_DMA_TODEVICE);
  585. tx_buffer->dma = 0;
  586. }
  587. }
  588. /* second free skb */
  589. for (index = 0; index < ring_count; index++) {
  590. tx_buffer = &tx_ring->tx_buffer[index];
  591. if (tx_buffer->skb) {
  592. dev_kfree_skb_any(tx_buffer->skb);
  593. tx_buffer->skb = NULL;
  594. }
  595. }
  596. /* Zero out Tx-buffers */
  597. memset(tx_ring->desc, 0, sizeof(struct atl1e_tpd_desc) *
  598. ring_count);
  599. memset(tx_ring->tx_buffer, 0, sizeof(struct atl1e_tx_buffer) *
  600. ring_count);
  601. }
  602. /**
  603. * atl1e_clean_rx_ring - Free rx-reservation skbs
  604. * @adapter: board private structure
  605. */
  606. static void atl1e_clean_rx_ring(struct atl1e_adapter *adapter)
  607. {
  608. struct atl1e_rx_ring *rx_ring =
  609. &adapter->rx_ring;
  610. struct atl1e_rx_page_desc *rx_page_desc = rx_ring->rx_page_desc;
  611. u16 i, j;
  612. if (adapter->ring_vir_addr == NULL)
  613. return;
  614. /* Zero out the descriptor ring */
  615. for (i = 0; i < adapter->num_rx_queues; i++) {
  616. for (j = 0; j < AT_PAGE_NUM_PER_QUEUE; j++) {
  617. if (rx_page_desc[i].rx_page[j].addr != NULL) {
  618. memset(rx_page_desc[i].rx_page[j].addr, 0,
  619. rx_ring->real_page_size);
  620. }
  621. }
  622. }
  623. }
  624. static void atl1e_cal_ring_size(struct atl1e_adapter *adapter, u32 *ring_size)
  625. {
  626. *ring_size = ((u32)(adapter->tx_ring.count *
  627. sizeof(struct atl1e_tpd_desc) + 7
  628. /* tx ring, qword align */
  629. + adapter->rx_ring.real_page_size * AT_PAGE_NUM_PER_QUEUE *
  630. adapter->num_rx_queues + 31
  631. /* rx ring, 32 bytes align */
  632. + (1 + AT_PAGE_NUM_PER_QUEUE * adapter->num_rx_queues) *
  633. sizeof(u32) + 3));
  634. /* tx, rx cmd, dword align */
  635. }
  636. static void atl1e_init_ring_resources(struct atl1e_adapter *adapter)
  637. {
  638. struct atl1e_rx_ring *rx_ring = NULL;
  639. rx_ring = &adapter->rx_ring;
  640. rx_ring->real_page_size = adapter->rx_ring.page_size
  641. + adapter->hw.max_frame_size
  642. + ETH_HLEN + VLAN_HLEN
  643. + ETH_FCS_LEN;
  644. rx_ring->real_page_size = roundup(rx_ring->real_page_size, 32);
  645. atl1e_cal_ring_size(adapter, &adapter->ring_size);
  646. adapter->ring_vir_addr = NULL;
  647. adapter->rx_ring.desc = NULL;
  648. rwlock_init(&adapter->tx_ring.tx_lock);
  649. }
  650. /*
  651. * Read / Write Ptr Initialize:
  652. */
  653. static void atl1e_init_ring_ptrs(struct atl1e_adapter *adapter)
  654. {
  655. struct atl1e_tx_ring *tx_ring = NULL;
  656. struct atl1e_rx_ring *rx_ring = NULL;
  657. struct atl1e_rx_page_desc *rx_page_desc = NULL;
  658. int i, j;
  659. tx_ring = &adapter->tx_ring;
  660. rx_ring = &adapter->rx_ring;
  661. rx_page_desc = rx_ring->rx_page_desc;
  662. tx_ring->next_to_use = 0;
  663. atomic_set(&tx_ring->next_to_clean, 0);
  664. for (i = 0; i < adapter->num_rx_queues; i++) {
  665. rx_page_desc[i].rx_using = 0;
  666. rx_page_desc[i].rx_nxseq = 0;
  667. for (j = 0; j < AT_PAGE_NUM_PER_QUEUE; j++) {
  668. *rx_page_desc[i].rx_page[j].write_offset_addr = 0;
  669. rx_page_desc[i].rx_page[j].read_offset = 0;
  670. }
  671. }
  672. }
  673. /**
  674. * atl1e_free_ring_resources - Free Tx / RX descriptor Resources
  675. * @adapter: board private structure
  676. *
  677. * Free all transmit software resources
  678. */
  679. static void atl1e_free_ring_resources(struct atl1e_adapter *adapter)
  680. {
  681. struct pci_dev *pdev = adapter->pdev;
  682. atl1e_clean_tx_ring(adapter);
  683. atl1e_clean_rx_ring(adapter);
  684. if (adapter->ring_vir_addr) {
  685. pci_free_consistent(pdev, adapter->ring_size,
  686. adapter->ring_vir_addr, adapter->ring_dma);
  687. adapter->ring_vir_addr = NULL;
  688. }
  689. if (adapter->tx_ring.tx_buffer) {
  690. kfree(adapter->tx_ring.tx_buffer);
  691. adapter->tx_ring.tx_buffer = NULL;
  692. }
  693. }
  694. /**
  695. * atl1e_setup_mem_resources - allocate Tx / RX descriptor resources
  696. * @adapter: board private structure
  697. *
  698. * Return 0 on success, negative on failure
  699. */
  700. static int atl1e_setup_ring_resources(struct atl1e_adapter *adapter)
  701. {
  702. struct pci_dev *pdev = adapter->pdev;
  703. struct atl1e_tx_ring *tx_ring;
  704. struct atl1e_rx_ring *rx_ring;
  705. struct atl1e_rx_page_desc *rx_page_desc;
  706. int size, i, j;
  707. u32 offset = 0;
  708. int err = 0;
  709. if (adapter->ring_vir_addr != NULL)
  710. return 0; /* alloced already */
  711. tx_ring = &adapter->tx_ring;
  712. rx_ring = &adapter->rx_ring;
  713. /* real ring DMA buffer */
  714. size = adapter->ring_size;
  715. adapter->ring_vir_addr = pci_alloc_consistent(pdev,
  716. adapter->ring_size, &adapter->ring_dma);
  717. if (adapter->ring_vir_addr == NULL) {
  718. netdev_err(adapter->netdev,
  719. "pci_alloc_consistent failed, size = D%d\n", size);
  720. return -ENOMEM;
  721. }
  722. memset(adapter->ring_vir_addr, 0, adapter->ring_size);
  723. rx_page_desc = rx_ring->rx_page_desc;
  724. /* Init TPD Ring */
  725. tx_ring->dma = roundup(adapter->ring_dma, 8);
  726. offset = tx_ring->dma - adapter->ring_dma;
  727. tx_ring->desc = adapter->ring_vir_addr + offset;
  728. size = sizeof(struct atl1e_tx_buffer) * (tx_ring->count);
  729. tx_ring->tx_buffer = kzalloc(size, GFP_KERNEL);
  730. if (tx_ring->tx_buffer == NULL) {
  731. err = -ENOMEM;
  732. goto failed;
  733. }
  734. /* Init RXF-Pages */
  735. offset += (sizeof(struct atl1e_tpd_desc) * tx_ring->count);
  736. offset = roundup(offset, 32);
  737. for (i = 0; i < adapter->num_rx_queues; i++) {
  738. for (j = 0; j < AT_PAGE_NUM_PER_QUEUE; j++) {
  739. rx_page_desc[i].rx_page[j].dma =
  740. adapter->ring_dma + offset;
  741. rx_page_desc[i].rx_page[j].addr =
  742. adapter->ring_vir_addr + offset;
  743. offset += rx_ring->real_page_size;
  744. }
  745. }
  746. /* Init CMB dma address */
  747. tx_ring->cmb_dma = adapter->ring_dma + offset;
  748. tx_ring->cmb = adapter->ring_vir_addr + offset;
  749. offset += sizeof(u32);
  750. for (i = 0; i < adapter->num_rx_queues; i++) {
  751. for (j = 0; j < AT_PAGE_NUM_PER_QUEUE; j++) {
  752. rx_page_desc[i].rx_page[j].write_offset_dma =
  753. adapter->ring_dma + offset;
  754. rx_page_desc[i].rx_page[j].write_offset_addr =
  755. adapter->ring_vir_addr + offset;
  756. offset += sizeof(u32);
  757. }
  758. }
  759. if (unlikely(offset > adapter->ring_size)) {
  760. netdev_err(adapter->netdev, "offset(%d) > ring size(%d) !!\n",
  761. offset, adapter->ring_size);
  762. err = -1;
  763. goto failed;
  764. }
  765. return 0;
  766. failed:
  767. if (adapter->ring_vir_addr != NULL) {
  768. pci_free_consistent(pdev, adapter->ring_size,
  769. adapter->ring_vir_addr, adapter->ring_dma);
  770. adapter->ring_vir_addr = NULL;
  771. }
  772. return err;
  773. }
  774. static inline void atl1e_configure_des_ring(struct atl1e_adapter *adapter)
  775. {
  776. struct atl1e_hw *hw = &adapter->hw;
  777. struct atl1e_rx_ring *rx_ring = &adapter->rx_ring;
  778. struct atl1e_tx_ring *tx_ring = &adapter->tx_ring;
  779. struct atl1e_rx_page_desc *rx_page_desc = NULL;
  780. int i, j;
  781. AT_WRITE_REG(hw, REG_DESC_BASE_ADDR_HI,
  782. (u32)((adapter->ring_dma & AT_DMA_HI_ADDR_MASK) >> 32));
  783. AT_WRITE_REG(hw, REG_TPD_BASE_ADDR_LO,
  784. (u32)((tx_ring->dma) & AT_DMA_LO_ADDR_MASK));
  785. AT_WRITE_REG(hw, REG_TPD_RING_SIZE, (u16)(tx_ring->count));
  786. AT_WRITE_REG(hw, REG_HOST_TX_CMB_LO,
  787. (u32)((tx_ring->cmb_dma) & AT_DMA_LO_ADDR_MASK));
  788. rx_page_desc = rx_ring->rx_page_desc;
  789. /* RXF Page Physical address / Page Length */
  790. for (i = 0; i < AT_MAX_RECEIVE_QUEUE; i++) {
  791. AT_WRITE_REG(hw, atl1e_rx_page_hi_addr_regs[i],
  792. (u32)((adapter->ring_dma &
  793. AT_DMA_HI_ADDR_MASK) >> 32));
  794. for (j = 0; j < AT_PAGE_NUM_PER_QUEUE; j++) {
  795. u32 page_phy_addr;
  796. u32 offset_phy_addr;
  797. page_phy_addr = rx_page_desc[i].rx_page[j].dma;
  798. offset_phy_addr =
  799. rx_page_desc[i].rx_page[j].write_offset_dma;
  800. AT_WRITE_REG(hw, atl1e_rx_page_lo_addr_regs[i][j],
  801. page_phy_addr & AT_DMA_LO_ADDR_MASK);
  802. AT_WRITE_REG(hw, atl1e_rx_page_write_offset_regs[i][j],
  803. offset_phy_addr & AT_DMA_LO_ADDR_MASK);
  804. AT_WRITE_REGB(hw, atl1e_rx_page_vld_regs[i][j], 1);
  805. }
  806. }
  807. /* Page Length */
  808. AT_WRITE_REG(hw, REG_HOST_RXFPAGE_SIZE, rx_ring->page_size);
  809. /* Load all of base address above */
  810. AT_WRITE_REG(hw, REG_LOAD_PTR, 1);
  811. }
  812. static inline void atl1e_configure_tx(struct atl1e_adapter *adapter)
  813. {
  814. struct atl1e_hw *hw = &adapter->hw;
  815. u32 dev_ctrl_data = 0;
  816. u32 max_pay_load = 0;
  817. u32 jumbo_thresh = 0;
  818. u32 extra_size = 0; /* Jumbo frame threshold in QWORD unit */
  819. /* configure TXQ param */
  820. if (hw->nic_type != athr_l2e_revB) {
  821. extra_size = ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN;
  822. if (hw->max_frame_size <= 1500) {
  823. jumbo_thresh = hw->max_frame_size + extra_size;
  824. } else if (hw->max_frame_size < 6*1024) {
  825. jumbo_thresh =
  826. (hw->max_frame_size + extra_size) * 2 / 3;
  827. } else {
  828. jumbo_thresh = (hw->max_frame_size + extra_size) / 2;
  829. }
  830. AT_WRITE_REG(hw, REG_TX_EARLY_TH, (jumbo_thresh + 7) >> 3);
  831. }
  832. dev_ctrl_data = AT_READ_REG(hw, REG_DEVICE_CTRL);
  833. max_pay_load = ((dev_ctrl_data >> DEVICE_CTRL_MAX_PAYLOAD_SHIFT)) &
  834. DEVICE_CTRL_MAX_PAYLOAD_MASK;
  835. hw->dmaw_block = min_t(u32, max_pay_load, hw->dmaw_block);
  836. max_pay_load = ((dev_ctrl_data >> DEVICE_CTRL_MAX_RREQ_SZ_SHIFT)) &
  837. DEVICE_CTRL_MAX_RREQ_SZ_MASK;
  838. hw->dmar_block = min_t(u32, max_pay_load, hw->dmar_block);
  839. if (hw->nic_type != athr_l2e_revB)
  840. AT_WRITE_REGW(hw, REG_TXQ_CTRL + 2,
  841. atl1e_pay_load_size[hw->dmar_block]);
  842. /* enable TXQ */
  843. AT_WRITE_REGW(hw, REG_TXQ_CTRL,
  844. (((u16)hw->tpd_burst & TXQ_CTRL_NUM_TPD_BURST_MASK)
  845. << TXQ_CTRL_NUM_TPD_BURST_SHIFT)
  846. | TXQ_CTRL_ENH_MODE | TXQ_CTRL_EN);
  847. }
  848. static inline void atl1e_configure_rx(struct atl1e_adapter *adapter)
  849. {
  850. struct atl1e_hw *hw = &adapter->hw;
  851. u32 rxf_len = 0;
  852. u32 rxf_low = 0;
  853. u32 rxf_high = 0;
  854. u32 rxf_thresh_data = 0;
  855. u32 rxq_ctrl_data = 0;
  856. if (hw->nic_type != athr_l2e_revB) {
  857. AT_WRITE_REGW(hw, REG_RXQ_JMBOSZ_RRDTIM,
  858. (u16)((hw->rx_jumbo_th & RXQ_JMBOSZ_TH_MASK) <<
  859. RXQ_JMBOSZ_TH_SHIFT |
  860. (1 & RXQ_JMBO_LKAH_MASK) <<
  861. RXQ_JMBO_LKAH_SHIFT));
  862. rxf_len = AT_READ_REG(hw, REG_SRAM_RXF_LEN);
  863. rxf_high = rxf_len * 4 / 5;
  864. rxf_low = rxf_len / 5;
  865. rxf_thresh_data = ((rxf_high & RXQ_RXF_PAUSE_TH_HI_MASK)
  866. << RXQ_RXF_PAUSE_TH_HI_SHIFT) |
  867. ((rxf_low & RXQ_RXF_PAUSE_TH_LO_MASK)
  868. << RXQ_RXF_PAUSE_TH_LO_SHIFT);
  869. AT_WRITE_REG(hw, REG_RXQ_RXF_PAUSE_THRESH, rxf_thresh_data);
  870. }
  871. /* RRS */
  872. AT_WRITE_REG(hw, REG_IDT_TABLE, hw->indirect_tab);
  873. AT_WRITE_REG(hw, REG_BASE_CPU_NUMBER, hw->base_cpu);
  874. if (hw->rrs_type & atl1e_rrs_ipv4)
  875. rxq_ctrl_data |= RXQ_CTRL_HASH_TYPE_IPV4;
  876. if (hw->rrs_type & atl1e_rrs_ipv4_tcp)
  877. rxq_ctrl_data |= RXQ_CTRL_HASH_TYPE_IPV4_TCP;
  878. if (hw->rrs_type & atl1e_rrs_ipv6)
  879. rxq_ctrl_data |= RXQ_CTRL_HASH_TYPE_IPV6;
  880. if (hw->rrs_type & atl1e_rrs_ipv6_tcp)
  881. rxq_ctrl_data |= RXQ_CTRL_HASH_TYPE_IPV6_TCP;
  882. if (hw->rrs_type != atl1e_rrs_disable)
  883. rxq_ctrl_data |=
  884. (RXQ_CTRL_HASH_ENABLE | RXQ_CTRL_RSS_MODE_MQUESINT);
  885. rxq_ctrl_data |= RXQ_CTRL_IPV6_XSUM_VERIFY_EN | RXQ_CTRL_PBA_ALIGN_32 |
  886. RXQ_CTRL_CUT_THRU_EN | RXQ_CTRL_EN;
  887. AT_WRITE_REG(hw, REG_RXQ_CTRL, rxq_ctrl_data);
  888. }
  889. static inline void atl1e_configure_dma(struct atl1e_adapter *adapter)
  890. {
  891. struct atl1e_hw *hw = &adapter->hw;
  892. u32 dma_ctrl_data = 0;
  893. dma_ctrl_data = DMA_CTRL_RXCMB_EN;
  894. dma_ctrl_data |= (((u32)hw->dmar_block) & DMA_CTRL_DMAR_BURST_LEN_MASK)
  895. << DMA_CTRL_DMAR_BURST_LEN_SHIFT;
  896. dma_ctrl_data |= (((u32)hw->dmaw_block) & DMA_CTRL_DMAW_BURST_LEN_MASK)
  897. << DMA_CTRL_DMAW_BURST_LEN_SHIFT;
  898. dma_ctrl_data |= DMA_CTRL_DMAR_REQ_PRI | DMA_CTRL_DMAR_OUT_ORDER;
  899. dma_ctrl_data |= (((u32)hw->dmar_dly_cnt) & DMA_CTRL_DMAR_DLY_CNT_MASK)
  900. << DMA_CTRL_DMAR_DLY_CNT_SHIFT;
  901. dma_ctrl_data |= (((u32)hw->dmaw_dly_cnt) & DMA_CTRL_DMAW_DLY_CNT_MASK)
  902. << DMA_CTRL_DMAW_DLY_CNT_SHIFT;
  903. AT_WRITE_REG(hw, REG_DMA_CTRL, dma_ctrl_data);
  904. }
  905. static void atl1e_setup_mac_ctrl(struct atl1e_adapter *adapter)
  906. {
  907. u32 value;
  908. struct atl1e_hw *hw = &adapter->hw;
  909. struct net_device *netdev = adapter->netdev;
  910. /* Config MAC CTRL Register */
  911. value = MAC_CTRL_TX_EN |
  912. MAC_CTRL_RX_EN ;
  913. if (FULL_DUPLEX == adapter->link_duplex)
  914. value |= MAC_CTRL_DUPLX;
  915. value |= ((u32)((SPEED_1000 == adapter->link_speed) ?
  916. MAC_CTRL_SPEED_1000 : MAC_CTRL_SPEED_10_100) <<
  917. MAC_CTRL_SPEED_SHIFT);
  918. value |= (MAC_CTRL_TX_FLOW | MAC_CTRL_RX_FLOW);
  919. value |= (MAC_CTRL_ADD_CRC | MAC_CTRL_PAD);
  920. value |= (((u32)adapter->hw.preamble_len &
  921. MAC_CTRL_PRMLEN_MASK) << MAC_CTRL_PRMLEN_SHIFT);
  922. __atl1e_vlan_mode(netdev->features, &value);
  923. value |= MAC_CTRL_BC_EN;
  924. if (netdev->flags & IFF_PROMISC)
  925. value |= MAC_CTRL_PROMIS_EN;
  926. if (netdev->flags & IFF_ALLMULTI)
  927. value |= MAC_CTRL_MC_ALL_EN;
  928. if (netdev->features & NETIF_F_RXALL)
  929. value |= MAC_CTRL_DBG;
  930. AT_WRITE_REG(hw, REG_MAC_CTRL, value);
  931. }
  932. /**
  933. * atl1e_configure - Configure Transmit&Receive Unit after Reset
  934. * @adapter: board private structure
  935. *
  936. * Configure the Tx /Rx unit of the MAC after a reset.
  937. */
  938. static int atl1e_configure(struct atl1e_adapter *adapter)
  939. {
  940. struct atl1e_hw *hw = &adapter->hw;
  941. u32 intr_status_data = 0;
  942. /* clear interrupt status */
  943. AT_WRITE_REG(hw, REG_ISR, ~0);
  944. /* 1. set MAC Address */
  945. atl1e_hw_set_mac_addr(hw);
  946. /* 2. Init the Multicast HASH table done by set_muti */
  947. /* 3. Clear any WOL status */
  948. AT_WRITE_REG(hw, REG_WOL_CTRL, 0);
  949. /* 4. Descripter Ring BaseMem/Length/Read ptr/Write ptr
  950. * TPD Ring/SMB/RXF0 Page CMBs, they use the same
  951. * High 32bits memory */
  952. atl1e_configure_des_ring(adapter);
  953. /* 5. set Interrupt Moderator Timer */
  954. AT_WRITE_REGW(hw, REG_IRQ_MODU_TIMER_INIT, hw->imt);
  955. AT_WRITE_REGW(hw, REG_IRQ_MODU_TIMER2_INIT, hw->imt);
  956. AT_WRITE_REG(hw, REG_MASTER_CTRL, MASTER_CTRL_LED_MODE |
  957. MASTER_CTRL_ITIMER_EN | MASTER_CTRL_ITIMER2_EN);
  958. /* 6. rx/tx threshold to trig interrupt */
  959. AT_WRITE_REGW(hw, REG_TRIG_RRD_THRESH, hw->rrd_thresh);
  960. AT_WRITE_REGW(hw, REG_TRIG_TPD_THRESH, hw->tpd_thresh);
  961. AT_WRITE_REGW(hw, REG_TRIG_RXTIMER, hw->rx_count_down);
  962. AT_WRITE_REGW(hw, REG_TRIG_TXTIMER, hw->tx_count_down);
  963. /* 7. set Interrupt Clear Timer */
  964. AT_WRITE_REGW(hw, REG_CMBDISDMA_TIMER, hw->ict);
  965. /* 8. set MTU */
  966. AT_WRITE_REG(hw, REG_MTU, hw->max_frame_size + ETH_HLEN +
  967. VLAN_HLEN + ETH_FCS_LEN);
  968. /* 9. config TXQ early tx threshold */
  969. atl1e_configure_tx(adapter);
  970. /* 10. config RXQ */
  971. atl1e_configure_rx(adapter);
  972. /* 11. config DMA Engine */
  973. atl1e_configure_dma(adapter);
  974. /* 12. smb timer to trig interrupt */
  975. AT_WRITE_REG(hw, REG_SMB_STAT_TIMER, hw->smb_timer);
  976. intr_status_data = AT_READ_REG(hw, REG_ISR);
  977. if (unlikely((intr_status_data & ISR_PHY_LINKDOWN) != 0)) {
  978. netdev_err(adapter->netdev,
  979. "atl1e_configure failed, PCIE phy link down\n");
  980. return -1;
  981. }
  982. AT_WRITE_REG(hw, REG_ISR, 0x7fffffff);
  983. return 0;
  984. }
  985. /**
  986. * atl1e_get_stats - Get System Network Statistics
  987. * @netdev: network interface device structure
  988. *
  989. * Returns the address of the device statistics structure.
  990. * The statistics are actually updated from the timer callback.
  991. */
  992. static struct net_device_stats *atl1e_get_stats(struct net_device *netdev)
  993. {
  994. struct atl1e_adapter *adapter = netdev_priv(netdev);
  995. struct atl1e_hw_stats *hw_stats = &adapter->hw_stats;
  996. struct net_device_stats *net_stats = &netdev->stats;
  997. net_stats->rx_bytes = hw_stats->rx_byte_cnt;
  998. net_stats->tx_bytes = hw_stats->tx_byte_cnt;
  999. net_stats->multicast = hw_stats->rx_mcast;
  1000. net_stats->collisions = hw_stats->tx_1_col +
  1001. hw_stats->tx_2_col +
  1002. hw_stats->tx_late_col +
  1003. hw_stats->tx_abort_col;
  1004. net_stats->rx_errors = hw_stats->rx_frag +
  1005. hw_stats->rx_fcs_err +
  1006. hw_stats->rx_len_err +
  1007. hw_stats->rx_sz_ov +
  1008. hw_stats->rx_rrd_ov +
  1009. hw_stats->rx_align_err +
  1010. hw_stats->rx_rxf_ov;
  1011. net_stats->rx_fifo_errors = hw_stats->rx_rxf_ov;
  1012. net_stats->rx_length_errors = hw_stats->rx_len_err;
  1013. net_stats->rx_crc_errors = hw_stats->rx_fcs_err;
  1014. net_stats->rx_frame_errors = hw_stats->rx_align_err;
  1015. net_stats->rx_dropped = hw_stats->rx_rrd_ov;
  1016. net_stats->tx_errors = hw_stats->tx_late_col +
  1017. hw_stats->tx_abort_col +
  1018. hw_stats->tx_underrun +
  1019. hw_stats->tx_trunc;
  1020. net_stats->tx_fifo_errors = hw_stats->tx_underrun;
  1021. net_stats->tx_aborted_errors = hw_stats->tx_abort_col;
  1022. net_stats->tx_window_errors = hw_stats->tx_late_col;
  1023. net_stats->rx_packets = hw_stats->rx_ok + net_stats->rx_errors;
  1024. net_stats->tx_packets = hw_stats->tx_ok + net_stats->tx_errors;
  1025. return net_stats;
  1026. }
  1027. static void atl1e_update_hw_stats(struct atl1e_adapter *adapter)
  1028. {
  1029. u16 hw_reg_addr = 0;
  1030. unsigned long *stats_item = NULL;
  1031. /* update rx status */
  1032. hw_reg_addr = REG_MAC_RX_STATUS_BIN;
  1033. stats_item = &adapter->hw_stats.rx_ok;
  1034. while (hw_reg_addr <= REG_MAC_RX_STATUS_END) {
  1035. *stats_item += AT_READ_REG(&adapter->hw, hw_reg_addr);
  1036. stats_item++;
  1037. hw_reg_addr += 4;
  1038. }
  1039. /* update tx status */
  1040. hw_reg_addr = REG_MAC_TX_STATUS_BIN;
  1041. stats_item = &adapter->hw_stats.tx_ok;
  1042. while (hw_reg_addr <= REG_MAC_TX_STATUS_END) {
  1043. *stats_item += AT_READ_REG(&adapter->hw, hw_reg_addr);
  1044. stats_item++;
  1045. hw_reg_addr += 4;
  1046. }
  1047. }
  1048. static inline void atl1e_clear_phy_int(struct atl1e_adapter *adapter)
  1049. {
  1050. u16 phy_data;
  1051. spin_lock(&adapter->mdio_lock);
  1052. atl1e_read_phy_reg(&adapter->hw, MII_INT_STATUS, &phy_data);
  1053. spin_unlock(&adapter->mdio_lock);
  1054. }
  1055. static bool atl1e_clean_tx_irq(struct atl1e_adapter *adapter)
  1056. {
  1057. struct atl1e_tx_ring *tx_ring = &adapter->tx_ring;
  1058. struct atl1e_tx_buffer *tx_buffer = NULL;
  1059. u16 hw_next_to_clean = AT_READ_REGW(&adapter->hw, REG_TPD_CONS_IDX);
  1060. u16 next_to_clean = atomic_read(&tx_ring->next_to_clean);
  1061. while (next_to_clean != hw_next_to_clean) {
  1062. tx_buffer = &tx_ring->tx_buffer[next_to_clean];
  1063. if (tx_buffer->dma) {
  1064. if (tx_buffer->flags & ATL1E_TX_PCIMAP_SINGLE)
  1065. pci_unmap_single(adapter->pdev, tx_buffer->dma,
  1066. tx_buffer->length, PCI_DMA_TODEVICE);
  1067. else if (tx_buffer->flags & ATL1E_TX_PCIMAP_PAGE)
  1068. pci_unmap_page(adapter->pdev, tx_buffer->dma,
  1069. tx_buffer->length, PCI_DMA_TODEVICE);
  1070. tx_buffer->dma = 0;
  1071. }
  1072. if (tx_buffer->skb) {
  1073. dev_kfree_skb_irq(tx_buffer->skb);
  1074. tx_buffer->skb = NULL;
  1075. }
  1076. if (++next_to_clean == tx_ring->count)
  1077. next_to_clean = 0;
  1078. }
  1079. atomic_set(&tx_ring->next_to_clean, next_to_clean);
  1080. if (netif_queue_stopped(adapter->netdev) &&
  1081. netif_carrier_ok(adapter->netdev)) {
  1082. netif_wake_queue(adapter->netdev);
  1083. }
  1084. return true;
  1085. }
  1086. /**
  1087. * atl1e_intr - Interrupt Handler
  1088. * @irq: interrupt number
  1089. * @data: pointer to a network interface device structure
  1090. */
  1091. static irqreturn_t atl1e_intr(int irq, void *data)
  1092. {
  1093. struct net_device *netdev = data;
  1094. struct atl1e_adapter *adapter = netdev_priv(netdev);
  1095. struct atl1e_hw *hw = &adapter->hw;
  1096. int max_ints = AT_MAX_INT_WORK;
  1097. int handled = IRQ_NONE;
  1098. u32 status;
  1099. do {
  1100. status = AT_READ_REG(hw, REG_ISR);
  1101. if ((status & IMR_NORMAL_MASK) == 0 ||
  1102. (status & ISR_DIS_INT) != 0) {
  1103. if (max_ints != AT_MAX_INT_WORK)
  1104. handled = IRQ_HANDLED;
  1105. break;
  1106. }
  1107. /* link event */
  1108. if (status & ISR_GPHY)
  1109. atl1e_clear_phy_int(adapter);
  1110. /* Ack ISR */
  1111. AT_WRITE_REG(hw, REG_ISR, status | ISR_DIS_INT);
  1112. handled = IRQ_HANDLED;
  1113. /* check if PCIE PHY Link down */
  1114. if (status & ISR_PHY_LINKDOWN) {
  1115. netdev_err(adapter->netdev,
  1116. "pcie phy linkdown %x\n", status);
  1117. if (netif_running(adapter->netdev)) {
  1118. /* reset MAC */
  1119. atl1e_irq_reset(adapter);
  1120. schedule_work(&adapter->reset_task);
  1121. break;
  1122. }
  1123. }
  1124. /* check if DMA read/write error */
  1125. if (status & (ISR_DMAR_TO_RST | ISR_DMAW_TO_RST)) {
  1126. netdev_err(adapter->netdev,
  1127. "PCIE DMA RW error (status = 0x%x)\n",
  1128. status);
  1129. atl1e_irq_reset(adapter);
  1130. schedule_work(&adapter->reset_task);
  1131. break;
  1132. }
  1133. if (status & ISR_SMB)
  1134. atl1e_update_hw_stats(adapter);
  1135. /* link event */
  1136. if (status & (ISR_GPHY | ISR_MANUAL)) {
  1137. netdev->stats.tx_carrier_errors++;
  1138. atl1e_link_chg_event(adapter);
  1139. break;
  1140. }
  1141. /* transmit event */
  1142. if (status & ISR_TX_EVENT)
  1143. atl1e_clean_tx_irq(adapter);
  1144. if (status & ISR_RX_EVENT) {
  1145. /*
  1146. * disable rx interrupts, without
  1147. * the synchronize_irq bit
  1148. */
  1149. AT_WRITE_REG(hw, REG_IMR,
  1150. IMR_NORMAL_MASK & ~ISR_RX_EVENT);
  1151. AT_WRITE_FLUSH(hw);
  1152. if (likely(napi_schedule_prep(
  1153. &adapter->napi)))
  1154. __napi_schedule(&adapter->napi);
  1155. }
  1156. } while (--max_ints > 0);
  1157. /* re-enable Interrupt*/
  1158. AT_WRITE_REG(&adapter->hw, REG_ISR, 0);
  1159. return handled;
  1160. }
  1161. static inline void atl1e_rx_checksum(struct atl1e_adapter *adapter,
  1162. struct sk_buff *skb, struct atl1e_recv_ret_status *prrs)
  1163. {
  1164. u8 *packet = (u8 *)(prrs + 1);
  1165. struct iphdr *iph;
  1166. u16 head_len = ETH_HLEN;
  1167. u16 pkt_flags;
  1168. u16 err_flags;
  1169. skb_checksum_none_assert(skb);
  1170. pkt_flags = prrs->pkt_flag;
  1171. err_flags = prrs->err_flag;
  1172. if (((pkt_flags & RRS_IS_IPV4) || (pkt_flags & RRS_IS_IPV6)) &&
  1173. ((pkt_flags & RRS_IS_TCP) || (pkt_flags & RRS_IS_UDP))) {
  1174. if (pkt_flags & RRS_IS_IPV4) {
  1175. if (pkt_flags & RRS_IS_802_3)
  1176. head_len += 8;
  1177. iph = (struct iphdr *) (packet + head_len);
  1178. if (iph->frag_off != 0 && !(pkt_flags & RRS_IS_IP_DF))
  1179. goto hw_xsum;
  1180. }
  1181. if (!(err_flags & (RRS_ERR_IP_CSUM | RRS_ERR_L4_CSUM))) {
  1182. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1183. return;
  1184. }
  1185. }
  1186. hw_xsum :
  1187. return;
  1188. }
  1189. static struct atl1e_rx_page *atl1e_get_rx_page(struct atl1e_adapter *adapter,
  1190. u8 que)
  1191. {
  1192. struct atl1e_rx_page_desc *rx_page_desc =
  1193. (struct atl1e_rx_page_desc *) adapter->rx_ring.rx_page_desc;
  1194. u8 rx_using = rx_page_desc[que].rx_using;
  1195. return &(rx_page_desc[que].rx_page[rx_using]);
  1196. }
  1197. static void atl1e_clean_rx_irq(struct atl1e_adapter *adapter, u8 que,
  1198. int *work_done, int work_to_do)
  1199. {
  1200. struct net_device *netdev = adapter->netdev;
  1201. struct atl1e_rx_ring *rx_ring = &adapter->rx_ring;
  1202. struct atl1e_rx_page_desc *rx_page_desc =
  1203. (struct atl1e_rx_page_desc *) rx_ring->rx_page_desc;
  1204. struct sk_buff *skb = NULL;
  1205. struct atl1e_rx_page *rx_page = atl1e_get_rx_page(adapter, que);
  1206. u32 packet_size, write_offset;
  1207. struct atl1e_recv_ret_status *prrs;
  1208. write_offset = *(rx_page->write_offset_addr);
  1209. if (likely(rx_page->read_offset < write_offset)) {
  1210. do {
  1211. if (*work_done >= work_to_do)
  1212. break;
  1213. (*work_done)++;
  1214. /* get new packet's rrs */
  1215. prrs = (struct atl1e_recv_ret_status *) (rx_page->addr +
  1216. rx_page->read_offset);
  1217. /* check sequence number */
  1218. if (prrs->seq_num != rx_page_desc[que].rx_nxseq) {
  1219. netdev_err(netdev,
  1220. "rx sequence number error (rx=%d) (expect=%d)\n",
  1221. prrs->seq_num,
  1222. rx_page_desc[que].rx_nxseq);
  1223. rx_page_desc[que].rx_nxseq++;
  1224. /* just for debug use */
  1225. AT_WRITE_REG(&adapter->hw, REG_DEBUG_DATA0,
  1226. (((u32)prrs->seq_num) << 16) |
  1227. rx_page_desc[que].rx_nxseq);
  1228. goto fatal_err;
  1229. }
  1230. rx_page_desc[que].rx_nxseq++;
  1231. /* error packet */
  1232. if ((prrs->pkt_flag & RRS_IS_ERR_FRAME) &&
  1233. !(netdev->features & NETIF_F_RXALL)) {
  1234. if (prrs->err_flag & (RRS_ERR_BAD_CRC |
  1235. RRS_ERR_DRIBBLE | RRS_ERR_CODE |
  1236. RRS_ERR_TRUNC)) {
  1237. /* hardware error, discard this packet*/
  1238. netdev_err(netdev,
  1239. "rx packet desc error %x\n",
  1240. *((u32 *)prrs + 1));
  1241. goto skip_pkt;
  1242. }
  1243. }
  1244. packet_size = ((prrs->word1 >> RRS_PKT_SIZE_SHIFT) &
  1245. RRS_PKT_SIZE_MASK);
  1246. if (likely(!(netdev->features & NETIF_F_RXFCS)))
  1247. packet_size -= 4; /* CRC */
  1248. skb = netdev_alloc_skb_ip_align(netdev, packet_size);
  1249. if (skb == NULL)
  1250. goto skip_pkt;
  1251. memcpy(skb->data, (u8 *)(prrs + 1), packet_size);
  1252. skb_put(skb, packet_size);
  1253. skb->protocol = eth_type_trans(skb, netdev);
  1254. atl1e_rx_checksum(adapter, skb, prrs);
  1255. if (prrs->pkt_flag & RRS_IS_VLAN_TAG) {
  1256. u16 vlan_tag = (prrs->vtag >> 4) |
  1257. ((prrs->vtag & 7) << 13) |
  1258. ((prrs->vtag & 8) << 9);
  1259. netdev_dbg(netdev,
  1260. "RXD VLAN TAG<RRD>=0x%04x\n",
  1261. prrs->vtag);
  1262. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlan_tag);
  1263. }
  1264. netif_receive_skb(skb);
  1265. skip_pkt:
  1266. /* skip current packet whether it's ok or not. */
  1267. rx_page->read_offset +=
  1268. (((u32)((prrs->word1 >> RRS_PKT_SIZE_SHIFT) &
  1269. RRS_PKT_SIZE_MASK) +
  1270. sizeof(struct atl1e_recv_ret_status) + 31) &
  1271. 0xFFFFFFE0);
  1272. if (rx_page->read_offset >= rx_ring->page_size) {
  1273. /* mark this page clean */
  1274. u16 reg_addr;
  1275. u8 rx_using;
  1276. rx_page->read_offset =
  1277. *(rx_page->write_offset_addr) = 0;
  1278. rx_using = rx_page_desc[que].rx_using;
  1279. reg_addr =
  1280. atl1e_rx_page_vld_regs[que][rx_using];
  1281. AT_WRITE_REGB(&adapter->hw, reg_addr, 1);
  1282. rx_page_desc[que].rx_using ^= 1;
  1283. rx_page = atl1e_get_rx_page(adapter, que);
  1284. }
  1285. write_offset = *(rx_page->write_offset_addr);
  1286. } while (rx_page->read_offset < write_offset);
  1287. }
  1288. return;
  1289. fatal_err:
  1290. if (!test_bit(__AT_DOWN, &adapter->flags))
  1291. schedule_work(&adapter->reset_task);
  1292. }
  1293. /**
  1294. * atl1e_clean - NAPI Rx polling callback
  1295. */
  1296. static int atl1e_clean(struct napi_struct *napi, int budget)
  1297. {
  1298. struct atl1e_adapter *adapter =
  1299. container_of(napi, struct atl1e_adapter, napi);
  1300. u32 imr_data;
  1301. int work_done = 0;
  1302. /* Keep link state information with original netdev */
  1303. if (!netif_carrier_ok(adapter->netdev))
  1304. goto quit_polling;
  1305. atl1e_clean_rx_irq(adapter, 0, &work_done, budget);
  1306. /* If no Tx and not enough Rx work done, exit the polling mode */
  1307. if (work_done < budget) {
  1308. quit_polling:
  1309. napi_complete(napi);
  1310. imr_data = AT_READ_REG(&adapter->hw, REG_IMR);
  1311. AT_WRITE_REG(&adapter->hw, REG_IMR, imr_data | ISR_RX_EVENT);
  1312. /* test debug */
  1313. if (test_bit(__AT_DOWN, &adapter->flags)) {
  1314. atomic_dec(&adapter->irq_sem);
  1315. netdev_err(adapter->netdev,
  1316. "atl1e_clean is called when AT_DOWN\n");
  1317. }
  1318. /* reenable RX intr */
  1319. /*atl1e_irq_enable(adapter); */
  1320. }
  1321. return work_done;
  1322. }
  1323. #ifdef CONFIG_NET_POLL_CONTROLLER
  1324. /*
  1325. * Polling 'interrupt' - used by things like netconsole to send skbs
  1326. * without having to re-enable interrupts. It's not called while
  1327. * the interrupt routine is executing.
  1328. */
  1329. static void atl1e_netpoll(struct net_device *netdev)
  1330. {
  1331. struct atl1e_adapter *adapter = netdev_priv(netdev);
  1332. disable_irq(adapter->pdev->irq);
  1333. atl1e_intr(adapter->pdev->irq, netdev);
  1334. enable_irq(adapter->pdev->irq);
  1335. }
  1336. #endif
  1337. static inline u16 atl1e_tpd_avail(struct atl1e_adapter *adapter)
  1338. {
  1339. struct atl1e_tx_ring *tx_ring = &adapter->tx_ring;
  1340. u16 next_to_use = 0;
  1341. u16 next_to_clean = 0;
  1342. next_to_clean = atomic_read(&tx_ring->next_to_clean);
  1343. next_to_use = tx_ring->next_to_use;
  1344. return (u16)(next_to_clean > next_to_use) ?
  1345. (next_to_clean - next_to_use - 1) :
  1346. (tx_ring->count + next_to_clean - next_to_use - 1);
  1347. }
  1348. /*
  1349. * get next usable tpd
  1350. * Note: should call atl1e_tdp_avail to make sure
  1351. * there is enough tpd to use
  1352. */
  1353. static struct atl1e_tpd_desc *atl1e_get_tpd(struct atl1e_adapter *adapter)
  1354. {
  1355. struct atl1e_tx_ring *tx_ring = &adapter->tx_ring;
  1356. u16 next_to_use = 0;
  1357. next_to_use = tx_ring->next_to_use;
  1358. if (++tx_ring->next_to_use == tx_ring->count)
  1359. tx_ring->next_to_use = 0;
  1360. memset(&tx_ring->desc[next_to_use], 0, sizeof(struct atl1e_tpd_desc));
  1361. return &tx_ring->desc[next_to_use];
  1362. }
  1363. static struct atl1e_tx_buffer *
  1364. atl1e_get_tx_buffer(struct atl1e_adapter *adapter, struct atl1e_tpd_desc *tpd)
  1365. {
  1366. struct atl1e_tx_ring *tx_ring = &adapter->tx_ring;
  1367. return &tx_ring->tx_buffer[tpd - tx_ring->desc];
  1368. }
  1369. /* Calculate the transmit packet descript needed*/
  1370. static u16 atl1e_cal_tdp_req(const struct sk_buff *skb)
  1371. {
  1372. int i = 0;
  1373. u16 tpd_req = 1;
  1374. u16 fg_size = 0;
  1375. u16 proto_hdr_len = 0;
  1376. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1377. fg_size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  1378. tpd_req += ((fg_size + MAX_TX_BUF_LEN - 1) >> MAX_TX_BUF_SHIFT);
  1379. }
  1380. if (skb_is_gso(skb)) {
  1381. if (skb->protocol == htons(ETH_P_IP) ||
  1382. (skb_shinfo(skb)->gso_type == SKB_GSO_TCPV6)) {
  1383. proto_hdr_len = skb_transport_offset(skb) +
  1384. tcp_hdrlen(skb);
  1385. if (proto_hdr_len < skb_headlen(skb)) {
  1386. tpd_req += ((skb_headlen(skb) - proto_hdr_len +
  1387. MAX_TX_BUF_LEN - 1) >>
  1388. MAX_TX_BUF_SHIFT);
  1389. }
  1390. }
  1391. }
  1392. return tpd_req;
  1393. }
  1394. static int atl1e_tso_csum(struct atl1e_adapter *adapter,
  1395. struct sk_buff *skb, struct atl1e_tpd_desc *tpd)
  1396. {
  1397. unsigned short offload_type;
  1398. u8 hdr_len;
  1399. u32 real_len;
  1400. if (skb_is_gso(skb)) {
  1401. int err;
  1402. err = skb_cow_head(skb, 0);
  1403. if (err < 0)
  1404. return err;
  1405. offload_type = skb_shinfo(skb)->gso_type;
  1406. if (offload_type & SKB_GSO_TCPV4) {
  1407. real_len = (((unsigned char *)ip_hdr(skb) - skb->data)
  1408. + ntohs(ip_hdr(skb)->tot_len));
  1409. if (real_len < skb->len)
  1410. pskb_trim(skb, real_len);
  1411. hdr_len = (skb_transport_offset(skb) + tcp_hdrlen(skb));
  1412. if (unlikely(skb->len == hdr_len)) {
  1413. /* only xsum need */
  1414. netdev_warn(adapter->netdev,
  1415. "IPV4 tso with zero data??\n");
  1416. goto check_sum;
  1417. } else {
  1418. ip_hdr(skb)->check = 0;
  1419. ip_hdr(skb)->tot_len = 0;
  1420. tcp_hdr(skb)->check = ~csum_tcpudp_magic(
  1421. ip_hdr(skb)->saddr,
  1422. ip_hdr(skb)->daddr,
  1423. 0, IPPROTO_TCP, 0);
  1424. tpd->word3 |= (ip_hdr(skb)->ihl &
  1425. TDP_V4_IPHL_MASK) <<
  1426. TPD_V4_IPHL_SHIFT;
  1427. tpd->word3 |= ((tcp_hdrlen(skb) >> 2) &
  1428. TPD_TCPHDRLEN_MASK) <<
  1429. TPD_TCPHDRLEN_SHIFT;
  1430. tpd->word3 |= ((skb_shinfo(skb)->gso_size) &
  1431. TPD_MSS_MASK) << TPD_MSS_SHIFT;
  1432. tpd->word3 |= 1 << TPD_SEGMENT_EN_SHIFT;
  1433. }
  1434. return 0;
  1435. }
  1436. }
  1437. check_sum:
  1438. if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
  1439. u8 css, cso;
  1440. cso = skb_checksum_start_offset(skb);
  1441. if (unlikely(cso & 0x1)) {
  1442. netdev_err(adapter->netdev,
  1443. "payload offset should not ant event number\n");
  1444. return -1;
  1445. } else {
  1446. css = cso + skb->csum_offset;
  1447. tpd->word3 |= (cso & TPD_PLOADOFFSET_MASK) <<
  1448. TPD_PLOADOFFSET_SHIFT;
  1449. tpd->word3 |= (css & TPD_CCSUMOFFSET_MASK) <<
  1450. TPD_CCSUMOFFSET_SHIFT;
  1451. tpd->word3 |= 1 << TPD_CC_SEGMENT_EN_SHIFT;
  1452. }
  1453. }
  1454. return 0;
  1455. }
  1456. static int atl1e_tx_map(struct atl1e_adapter *adapter,
  1457. struct sk_buff *skb, struct atl1e_tpd_desc *tpd)
  1458. {
  1459. struct atl1e_tpd_desc *use_tpd = NULL;
  1460. struct atl1e_tx_buffer *tx_buffer = NULL;
  1461. u16 buf_len = skb_headlen(skb);
  1462. u16 map_len = 0;
  1463. u16 mapped_len = 0;
  1464. u16 hdr_len = 0;
  1465. u16 nr_frags;
  1466. u16 f;
  1467. int segment;
  1468. int ring_start = adapter->tx_ring.next_to_use;
  1469. int ring_end;
  1470. nr_frags = skb_shinfo(skb)->nr_frags;
  1471. segment = (tpd->word3 >> TPD_SEGMENT_EN_SHIFT) & TPD_SEGMENT_EN_MASK;
  1472. if (segment) {
  1473. /* TSO */
  1474. map_len = hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1475. use_tpd = tpd;
  1476. tx_buffer = atl1e_get_tx_buffer(adapter, use_tpd);
  1477. tx_buffer->length = map_len;
  1478. tx_buffer->dma = pci_map_single(adapter->pdev,
  1479. skb->data, hdr_len, PCI_DMA_TODEVICE);
  1480. if (dma_mapping_error(&adapter->pdev->dev, tx_buffer->dma))
  1481. return -ENOSPC;
  1482. ATL1E_SET_PCIMAP_TYPE(tx_buffer, ATL1E_TX_PCIMAP_SINGLE);
  1483. mapped_len += map_len;
  1484. use_tpd->buffer_addr = cpu_to_le64(tx_buffer->dma);
  1485. use_tpd->word2 = (use_tpd->word2 & (~TPD_BUFLEN_MASK)) |
  1486. ((cpu_to_le32(tx_buffer->length) &
  1487. TPD_BUFLEN_MASK) << TPD_BUFLEN_SHIFT);
  1488. }
  1489. while (mapped_len < buf_len) {
  1490. /* mapped_len == 0, means we should use the first tpd,
  1491. which is given by caller */
  1492. if (mapped_len == 0) {
  1493. use_tpd = tpd;
  1494. } else {
  1495. use_tpd = atl1e_get_tpd(adapter);
  1496. memcpy(use_tpd, tpd, sizeof(struct atl1e_tpd_desc));
  1497. }
  1498. tx_buffer = atl1e_get_tx_buffer(adapter, use_tpd);
  1499. tx_buffer->skb = NULL;
  1500. tx_buffer->length = map_len =
  1501. ((buf_len - mapped_len) >= MAX_TX_BUF_LEN) ?
  1502. MAX_TX_BUF_LEN : (buf_len - mapped_len);
  1503. tx_buffer->dma =
  1504. pci_map_single(adapter->pdev, skb->data + mapped_len,
  1505. map_len, PCI_DMA_TODEVICE);
  1506. if (dma_mapping_error(&adapter->pdev->dev, tx_buffer->dma)) {
  1507. /* We need to unwind the mappings we've done */
  1508. ring_end = adapter->tx_ring.next_to_use;
  1509. adapter->tx_ring.next_to_use = ring_start;
  1510. while (adapter->tx_ring.next_to_use != ring_end) {
  1511. tpd = atl1e_get_tpd(adapter);
  1512. tx_buffer = atl1e_get_tx_buffer(adapter, tpd);
  1513. pci_unmap_single(adapter->pdev, tx_buffer->dma,
  1514. tx_buffer->length, PCI_DMA_TODEVICE);
  1515. }
  1516. /* Reset the tx rings next pointer */
  1517. adapter->tx_ring.next_to_use = ring_start;
  1518. return -ENOSPC;
  1519. }
  1520. ATL1E_SET_PCIMAP_TYPE(tx_buffer, ATL1E_TX_PCIMAP_SINGLE);
  1521. mapped_len += map_len;
  1522. use_tpd->buffer_addr = cpu_to_le64(tx_buffer->dma);
  1523. use_tpd->word2 = (use_tpd->word2 & (~TPD_BUFLEN_MASK)) |
  1524. ((cpu_to_le32(tx_buffer->length) &
  1525. TPD_BUFLEN_MASK) << TPD_BUFLEN_SHIFT);
  1526. }
  1527. for (f = 0; f < nr_frags; f++) {
  1528. const struct skb_frag_struct *frag;
  1529. u16 i;
  1530. u16 seg_num;
  1531. frag = &skb_shinfo(skb)->frags[f];
  1532. buf_len = skb_frag_size(frag);
  1533. seg_num = (buf_len + MAX_TX_BUF_LEN - 1) / MAX_TX_BUF_LEN;
  1534. for (i = 0; i < seg_num; i++) {
  1535. use_tpd = atl1e_get_tpd(adapter);
  1536. memcpy(use_tpd, tpd, sizeof(struct atl1e_tpd_desc));
  1537. tx_buffer = atl1e_get_tx_buffer(adapter, use_tpd);
  1538. BUG_ON(tx_buffer->skb);
  1539. tx_buffer->skb = NULL;
  1540. tx_buffer->length =
  1541. (buf_len > MAX_TX_BUF_LEN) ?
  1542. MAX_TX_BUF_LEN : buf_len;
  1543. buf_len -= tx_buffer->length;
  1544. tx_buffer->dma = skb_frag_dma_map(&adapter->pdev->dev,
  1545. frag,
  1546. (i * MAX_TX_BUF_LEN),
  1547. tx_buffer->length,
  1548. DMA_TO_DEVICE);
  1549. if (dma_mapping_error(&adapter->pdev->dev, tx_buffer->dma)) {
  1550. /* We need to unwind the mappings we've done */
  1551. ring_end = adapter->tx_ring.next_to_use;
  1552. adapter->tx_ring.next_to_use = ring_start;
  1553. while (adapter->tx_ring.next_to_use != ring_end) {
  1554. tpd = atl1e_get_tpd(adapter);
  1555. tx_buffer = atl1e_get_tx_buffer(adapter, tpd);
  1556. dma_unmap_page(&adapter->pdev->dev, tx_buffer->dma,
  1557. tx_buffer->length, DMA_TO_DEVICE);
  1558. }
  1559. /* Reset the ring next to use pointer */
  1560. adapter->tx_ring.next_to_use = ring_start;
  1561. return -ENOSPC;
  1562. }
  1563. ATL1E_SET_PCIMAP_TYPE(tx_buffer, ATL1E_TX_PCIMAP_PAGE);
  1564. use_tpd->buffer_addr = cpu_to_le64(tx_buffer->dma);
  1565. use_tpd->word2 = (use_tpd->word2 & (~TPD_BUFLEN_MASK)) |
  1566. ((cpu_to_le32(tx_buffer->length) &
  1567. TPD_BUFLEN_MASK) << TPD_BUFLEN_SHIFT);
  1568. }
  1569. }
  1570. if ((tpd->word3 >> TPD_SEGMENT_EN_SHIFT) & TPD_SEGMENT_EN_MASK)
  1571. /* note this one is a tcp header */
  1572. tpd->word3 |= 1 << TPD_HDRFLAG_SHIFT;
  1573. /* The last tpd */
  1574. use_tpd->word3 |= 1 << TPD_EOP_SHIFT;
  1575. /* The last buffer info contain the skb address,
  1576. so it will be free after unmap */
  1577. tx_buffer->skb = skb;
  1578. return 0;
  1579. }
  1580. static void atl1e_tx_queue(struct atl1e_adapter *adapter, u16 count,
  1581. struct atl1e_tpd_desc *tpd)
  1582. {
  1583. struct atl1e_tx_ring *tx_ring = &adapter->tx_ring;
  1584. /* Force memory writes to complete before letting h/w
  1585. * know there are new descriptors to fetch. (Only
  1586. * applicable for weak-ordered memory model archs,
  1587. * such as IA-64). */
  1588. wmb();
  1589. AT_WRITE_REG(&adapter->hw, REG_MB_TPD_PROD_IDX, tx_ring->next_to_use);
  1590. }
  1591. static netdev_tx_t atl1e_xmit_frame(struct sk_buff *skb,
  1592. struct net_device *netdev)
  1593. {
  1594. struct atl1e_adapter *adapter = netdev_priv(netdev);
  1595. unsigned long flags;
  1596. u16 tpd_req = 1;
  1597. struct atl1e_tpd_desc *tpd;
  1598. if (test_bit(__AT_DOWN, &adapter->flags)) {
  1599. dev_kfree_skb_any(skb);
  1600. return NETDEV_TX_OK;
  1601. }
  1602. if (unlikely(skb->len <= 0)) {
  1603. dev_kfree_skb_any(skb);
  1604. return NETDEV_TX_OK;
  1605. }
  1606. tpd_req = atl1e_cal_tdp_req(skb);
  1607. if (!spin_trylock_irqsave(&adapter->tx_lock, flags))
  1608. return NETDEV_TX_LOCKED;
  1609. if (atl1e_tpd_avail(adapter) < tpd_req) {
  1610. /* no enough descriptor, just stop queue */
  1611. netif_stop_queue(netdev);
  1612. spin_unlock_irqrestore(&adapter->tx_lock, flags);
  1613. return NETDEV_TX_BUSY;
  1614. }
  1615. tpd = atl1e_get_tpd(adapter);
  1616. if (vlan_tx_tag_present(skb)) {
  1617. u16 vlan_tag = vlan_tx_tag_get(skb);
  1618. u16 atl1e_vlan_tag;
  1619. tpd->word3 |= 1 << TPD_INS_VL_TAG_SHIFT;
  1620. AT_VLAN_TAG_TO_TPD_TAG(vlan_tag, atl1e_vlan_tag);
  1621. tpd->word2 |= (atl1e_vlan_tag & TPD_VLANTAG_MASK) <<
  1622. TPD_VLAN_SHIFT;
  1623. }
  1624. if (skb->protocol == htons(ETH_P_8021Q))
  1625. tpd->word3 |= 1 << TPD_VL_TAGGED_SHIFT;
  1626. if (skb_network_offset(skb) != ETH_HLEN)
  1627. tpd->word3 |= 1 << TPD_ETHTYPE_SHIFT; /* 802.3 frame */
  1628. /* do TSO and check sum */
  1629. if (atl1e_tso_csum(adapter, skb, tpd) != 0) {
  1630. spin_unlock_irqrestore(&adapter->tx_lock, flags);
  1631. dev_kfree_skb_any(skb);
  1632. return NETDEV_TX_OK;
  1633. }
  1634. if (atl1e_tx_map(adapter, skb, tpd)) {
  1635. dev_kfree_skb_any(skb);
  1636. goto out;
  1637. }
  1638. atl1e_tx_queue(adapter, tpd_req, tpd);
  1639. netdev->trans_start = jiffies; /* NETIF_F_LLTX driver :( */
  1640. out:
  1641. spin_unlock_irqrestore(&adapter->tx_lock, flags);
  1642. return NETDEV_TX_OK;
  1643. }
  1644. static void atl1e_free_irq(struct atl1e_adapter *adapter)
  1645. {
  1646. struct net_device *netdev = adapter->netdev;
  1647. free_irq(adapter->pdev->irq, netdev);
  1648. }
  1649. static int atl1e_request_irq(struct atl1e_adapter *adapter)
  1650. {
  1651. struct pci_dev *pdev = adapter->pdev;
  1652. struct net_device *netdev = adapter->netdev;
  1653. int err = 0;
  1654. err = request_irq(pdev->irq, atl1e_intr, IRQF_SHARED, netdev->name,
  1655. netdev);
  1656. if (err) {
  1657. netdev_dbg(adapter->netdev,
  1658. "Unable to allocate interrupt Error: %d\n", err);
  1659. return err;
  1660. }
  1661. netdev_dbg(netdev, "atl1e_request_irq OK\n");
  1662. return err;
  1663. }
  1664. int atl1e_up(struct atl1e_adapter *adapter)
  1665. {
  1666. struct net_device *netdev = adapter->netdev;
  1667. int err = 0;
  1668. u32 val;
  1669. /* hardware has been reset, we need to reload some things */
  1670. err = atl1e_init_hw(&adapter->hw);
  1671. if (err) {
  1672. err = -EIO;
  1673. return err;
  1674. }
  1675. atl1e_init_ring_ptrs(adapter);
  1676. atl1e_set_multi(netdev);
  1677. atl1e_restore_vlan(adapter);
  1678. if (atl1e_configure(adapter)) {
  1679. err = -EIO;
  1680. goto err_up;
  1681. }
  1682. clear_bit(__AT_DOWN, &adapter->flags);
  1683. napi_enable(&adapter->napi);
  1684. atl1e_irq_enable(adapter);
  1685. val = AT_READ_REG(&adapter->hw, REG_MASTER_CTRL);
  1686. AT_WRITE_REG(&adapter->hw, REG_MASTER_CTRL,
  1687. val | MASTER_CTRL_MANUAL_INT);
  1688. err_up:
  1689. return err;
  1690. }
  1691. void atl1e_down(struct atl1e_adapter *adapter)
  1692. {
  1693. struct net_device *netdev = adapter->netdev;
  1694. /* signal that we're down so the interrupt handler does not
  1695. * reschedule our watchdog timer */
  1696. set_bit(__AT_DOWN, &adapter->flags);
  1697. netif_stop_queue(netdev);
  1698. /* reset MAC to disable all RX/TX */
  1699. atl1e_reset_hw(&adapter->hw);
  1700. msleep(1);
  1701. napi_disable(&adapter->napi);
  1702. atl1e_del_timer(adapter);
  1703. atl1e_irq_disable(adapter);
  1704. netif_carrier_off(netdev);
  1705. adapter->link_speed = SPEED_0;
  1706. adapter->link_duplex = -1;
  1707. atl1e_clean_tx_ring(adapter);
  1708. atl1e_clean_rx_ring(adapter);
  1709. }
  1710. /**
  1711. * atl1e_open - Called when a network interface is made active
  1712. * @netdev: network interface device structure
  1713. *
  1714. * Returns 0 on success, negative value on failure
  1715. *
  1716. * The open entry point is called when a network interface is made
  1717. * active by the system (IFF_UP). At this point all resources needed
  1718. * for transmit and receive operations are allocated, the interrupt
  1719. * handler is registered with the OS, the watchdog timer is started,
  1720. * and the stack is notified that the interface is ready.
  1721. */
  1722. static int atl1e_open(struct net_device *netdev)
  1723. {
  1724. struct atl1e_adapter *adapter = netdev_priv(netdev);
  1725. int err;
  1726. /* disallow open during test */
  1727. if (test_bit(__AT_TESTING, &adapter->flags))
  1728. return -EBUSY;
  1729. /* allocate rx/tx dma buffer & descriptors */
  1730. atl1e_init_ring_resources(adapter);
  1731. err = atl1e_setup_ring_resources(adapter);
  1732. if (unlikely(err))
  1733. return err;
  1734. err = atl1e_request_irq(adapter);
  1735. if (unlikely(err))
  1736. goto err_req_irq;
  1737. err = atl1e_up(adapter);
  1738. if (unlikely(err))
  1739. goto err_up;
  1740. return 0;
  1741. err_up:
  1742. atl1e_free_irq(adapter);
  1743. err_req_irq:
  1744. atl1e_free_ring_resources(adapter);
  1745. atl1e_reset_hw(&adapter->hw);
  1746. return err;
  1747. }
  1748. /**
  1749. * atl1e_close - Disables a network interface
  1750. * @netdev: network interface device structure
  1751. *
  1752. * Returns 0, this is not allowed to fail
  1753. *
  1754. * The close entry point is called when an interface is de-activated
  1755. * by the OS. The hardware is still under the drivers control, but
  1756. * needs to be disabled. A global MAC reset is issued to stop the
  1757. * hardware, and all transmit and receive resources are freed.
  1758. */
  1759. static int atl1e_close(struct net_device *netdev)
  1760. {
  1761. struct atl1e_adapter *adapter = netdev_priv(netdev);
  1762. WARN_ON(test_bit(__AT_RESETTING, &adapter->flags));
  1763. atl1e_down(adapter);
  1764. atl1e_free_irq(adapter);
  1765. atl1e_free_ring_resources(adapter);
  1766. return 0;
  1767. }
  1768. static int atl1e_suspend(struct pci_dev *pdev, pm_message_t state)
  1769. {
  1770. struct net_device *netdev = pci_get_drvdata(pdev);
  1771. struct atl1e_adapter *adapter = netdev_priv(netdev);
  1772. struct atl1e_hw *hw = &adapter->hw;
  1773. u32 ctrl = 0;
  1774. u32 mac_ctrl_data = 0;
  1775. u32 wol_ctrl_data = 0;
  1776. u16 mii_advertise_data = 0;
  1777. u16 mii_bmsr_data = 0;
  1778. u16 mii_intr_status_data = 0;
  1779. u32 wufc = adapter->wol;
  1780. u32 i;
  1781. #ifdef CONFIG_PM
  1782. int retval = 0;
  1783. #endif
  1784. if (netif_running(netdev)) {
  1785. WARN_ON(test_bit(__AT_RESETTING, &adapter->flags));
  1786. atl1e_down(adapter);
  1787. }
  1788. netif_device_detach(netdev);
  1789. #ifdef CONFIG_PM
  1790. retval = pci_save_state(pdev);
  1791. if (retval)
  1792. return retval;
  1793. #endif
  1794. if (wufc) {
  1795. /* get link status */
  1796. atl1e_read_phy_reg(hw, MII_BMSR, &mii_bmsr_data);
  1797. atl1e_read_phy_reg(hw, MII_BMSR, &mii_bmsr_data);
  1798. mii_advertise_data = ADVERTISE_10HALF;
  1799. if ((atl1e_write_phy_reg(hw, MII_CTRL1000, 0) != 0) ||
  1800. (atl1e_write_phy_reg(hw,
  1801. MII_ADVERTISE, mii_advertise_data) != 0) ||
  1802. (atl1e_phy_commit(hw)) != 0) {
  1803. netdev_dbg(adapter->netdev, "set phy register failed\n");
  1804. goto wol_dis;
  1805. }
  1806. hw->phy_configured = false; /* re-init PHY when resume */
  1807. /* turn on magic packet wol */
  1808. if (wufc & AT_WUFC_MAG)
  1809. wol_ctrl_data |= WOL_MAGIC_EN | WOL_MAGIC_PME_EN;
  1810. if (wufc & AT_WUFC_LNKC) {
  1811. /* if orignal link status is link, just wait for retrive link */
  1812. if (mii_bmsr_data & BMSR_LSTATUS) {
  1813. for (i = 0; i < AT_SUSPEND_LINK_TIMEOUT; i++) {
  1814. msleep(100);
  1815. atl1e_read_phy_reg(hw, MII_BMSR,
  1816. &mii_bmsr_data);
  1817. if (mii_bmsr_data & BMSR_LSTATUS)
  1818. break;
  1819. }
  1820. if ((mii_bmsr_data & BMSR_LSTATUS) == 0)
  1821. netdev_dbg(adapter->netdev,
  1822. "Link may change when suspend\n");
  1823. }
  1824. wol_ctrl_data |= WOL_LINK_CHG_EN | WOL_LINK_CHG_PME_EN;
  1825. /* only link up can wake up */
  1826. if (atl1e_write_phy_reg(hw, MII_INT_CTRL, 0x400) != 0) {
  1827. netdev_dbg(adapter->netdev,
  1828. "read write phy register failed\n");
  1829. goto wol_dis;
  1830. }
  1831. }
  1832. /* clear phy interrupt */
  1833. atl1e_read_phy_reg(hw, MII_INT_STATUS, &mii_intr_status_data);
  1834. /* Config MAC Ctrl register */
  1835. mac_ctrl_data = MAC_CTRL_RX_EN;
  1836. /* set to 10/100M halt duplex */
  1837. mac_ctrl_data |= MAC_CTRL_SPEED_10_100 << MAC_CTRL_SPEED_SHIFT;
  1838. mac_ctrl_data |= (((u32)adapter->hw.preamble_len &
  1839. MAC_CTRL_PRMLEN_MASK) <<
  1840. MAC_CTRL_PRMLEN_SHIFT);
  1841. __atl1e_vlan_mode(netdev->features, &mac_ctrl_data);
  1842. /* magic packet maybe Broadcast&multicast&Unicast frame */
  1843. if (wufc & AT_WUFC_MAG)
  1844. mac_ctrl_data |= MAC_CTRL_BC_EN;
  1845. netdev_dbg(adapter->netdev, "suspend MAC=0x%x\n",
  1846. mac_ctrl_data);
  1847. AT_WRITE_REG(hw, REG_WOL_CTRL, wol_ctrl_data);
  1848. AT_WRITE_REG(hw, REG_MAC_CTRL, mac_ctrl_data);
  1849. /* pcie patch */
  1850. ctrl = AT_READ_REG(hw, REG_PCIE_PHYMISC);
  1851. ctrl |= PCIE_PHYMISC_FORCE_RCV_DET;
  1852. AT_WRITE_REG(hw, REG_PCIE_PHYMISC, ctrl);
  1853. pci_enable_wake(pdev, pci_choose_state(pdev, state), 1);
  1854. goto suspend_exit;
  1855. }
  1856. wol_dis:
  1857. /* WOL disabled */
  1858. AT_WRITE_REG(hw, REG_WOL_CTRL, 0);
  1859. /* pcie patch */
  1860. ctrl = AT_READ_REG(hw, REG_PCIE_PHYMISC);
  1861. ctrl |= PCIE_PHYMISC_FORCE_RCV_DET;
  1862. AT_WRITE_REG(hw, REG_PCIE_PHYMISC, ctrl);
  1863. atl1e_force_ps(hw);
  1864. hw->phy_configured = false; /* re-init PHY when resume */
  1865. pci_enable_wake(pdev, pci_choose_state(pdev, state), 0);
  1866. suspend_exit:
  1867. if (netif_running(netdev))
  1868. atl1e_free_irq(adapter);
  1869. pci_disable_device(pdev);
  1870. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1871. return 0;
  1872. }
  1873. #ifdef CONFIG_PM
  1874. static int atl1e_resume(struct pci_dev *pdev)
  1875. {
  1876. struct net_device *netdev = pci_get_drvdata(pdev);
  1877. struct atl1e_adapter *adapter = netdev_priv(netdev);
  1878. u32 err;
  1879. pci_set_power_state(pdev, PCI_D0);
  1880. pci_restore_state(pdev);
  1881. err = pci_enable_device(pdev);
  1882. if (err) {
  1883. netdev_err(adapter->netdev,
  1884. "Cannot enable PCI device from suspend\n");
  1885. return err;
  1886. }
  1887. pci_set_master(pdev);
  1888. AT_READ_REG(&adapter->hw, REG_WOL_CTRL); /* clear WOL status */
  1889. pci_enable_wake(pdev, PCI_D3hot, 0);
  1890. pci_enable_wake(pdev, PCI_D3cold, 0);
  1891. AT_WRITE_REG(&adapter->hw, REG_WOL_CTRL, 0);
  1892. if (netif_running(netdev)) {
  1893. err = atl1e_request_irq(adapter);
  1894. if (err)
  1895. return err;
  1896. }
  1897. atl1e_reset_hw(&adapter->hw);
  1898. if (netif_running(netdev))
  1899. atl1e_up(adapter);
  1900. netif_device_attach(netdev);
  1901. return 0;
  1902. }
  1903. #endif
  1904. static void atl1e_shutdown(struct pci_dev *pdev)
  1905. {
  1906. atl1e_suspend(pdev, PMSG_SUSPEND);
  1907. }
  1908. static const struct net_device_ops atl1e_netdev_ops = {
  1909. .ndo_open = atl1e_open,
  1910. .ndo_stop = atl1e_close,
  1911. .ndo_start_xmit = atl1e_xmit_frame,
  1912. .ndo_get_stats = atl1e_get_stats,
  1913. .ndo_set_rx_mode = atl1e_set_multi,
  1914. .ndo_validate_addr = eth_validate_addr,
  1915. .ndo_set_mac_address = atl1e_set_mac_addr,
  1916. .ndo_fix_features = atl1e_fix_features,
  1917. .ndo_set_features = atl1e_set_features,
  1918. .ndo_change_mtu = atl1e_change_mtu,
  1919. .ndo_do_ioctl = atl1e_ioctl,
  1920. .ndo_tx_timeout = atl1e_tx_timeout,
  1921. #ifdef CONFIG_NET_POLL_CONTROLLER
  1922. .ndo_poll_controller = atl1e_netpoll,
  1923. #endif
  1924. };
  1925. static int atl1e_init_netdev(struct net_device *netdev, struct pci_dev *pdev)
  1926. {
  1927. SET_NETDEV_DEV(netdev, &pdev->dev);
  1928. pci_set_drvdata(pdev, netdev);
  1929. netdev->netdev_ops = &atl1e_netdev_ops;
  1930. netdev->watchdog_timeo = AT_TX_WATCHDOG;
  1931. atl1e_set_ethtool_ops(netdev);
  1932. netdev->hw_features = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_TSO |
  1933. NETIF_F_HW_VLAN_CTAG_RX;
  1934. netdev->features = netdev->hw_features | NETIF_F_LLTX |
  1935. NETIF_F_HW_VLAN_CTAG_TX;
  1936. /* not enabled by default */
  1937. netdev->hw_features |= NETIF_F_RXALL | NETIF_F_RXFCS;
  1938. return 0;
  1939. }
  1940. /**
  1941. * atl1e_probe - Device Initialization Routine
  1942. * @pdev: PCI device information struct
  1943. * @ent: entry in atl1e_pci_tbl
  1944. *
  1945. * Returns 0 on success, negative on failure
  1946. *
  1947. * atl1e_probe initializes an adapter identified by a pci_dev structure.
  1948. * The OS initialization, configuring of the adapter private structure,
  1949. * and a hardware reset occur.
  1950. */
  1951. static int atl1e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1952. {
  1953. struct net_device *netdev;
  1954. struct atl1e_adapter *adapter = NULL;
  1955. static int cards_found;
  1956. int err = 0;
  1957. err = pci_enable_device(pdev);
  1958. if (err) {
  1959. dev_err(&pdev->dev, "cannot enable PCI device\n");
  1960. return err;
  1961. }
  1962. /*
  1963. * The atl1e chip can DMA to 64-bit addresses, but it uses a single
  1964. * shared register for the high 32 bits, so only a single, aligned,
  1965. * 4 GB physical address range can be used at a time.
  1966. *
  1967. * Supporting 64-bit DMA on this hardware is more trouble than it's
  1968. * worth. It is far easier to limit to 32-bit DMA than update
  1969. * various kernel subsystems to support the mechanics required by a
  1970. * fixed-high-32-bit system.
  1971. */
  1972. if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) ||
  1973. (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)) {
  1974. dev_err(&pdev->dev, "No usable DMA configuration,aborting\n");
  1975. goto err_dma;
  1976. }
  1977. err = pci_request_regions(pdev, atl1e_driver_name);
  1978. if (err) {
  1979. dev_err(&pdev->dev, "cannot obtain PCI resources\n");
  1980. goto err_pci_reg;
  1981. }
  1982. pci_set_master(pdev);
  1983. netdev = alloc_etherdev(sizeof(struct atl1e_adapter));
  1984. if (netdev == NULL) {
  1985. err = -ENOMEM;
  1986. goto err_alloc_etherdev;
  1987. }
  1988. err = atl1e_init_netdev(netdev, pdev);
  1989. if (err) {
  1990. netdev_err(netdev, "init netdevice failed\n");
  1991. goto err_init_netdev;
  1992. }
  1993. adapter = netdev_priv(netdev);
  1994. adapter->bd_number = cards_found;
  1995. adapter->netdev = netdev;
  1996. adapter->pdev = pdev;
  1997. adapter->hw.adapter = adapter;
  1998. adapter->hw.hw_addr = pci_iomap(pdev, BAR_0, 0);
  1999. if (!adapter->hw.hw_addr) {
  2000. err = -EIO;
  2001. netdev_err(netdev, "cannot map device registers\n");
  2002. goto err_ioremap;
  2003. }
  2004. /* init mii data */
  2005. adapter->mii.dev = netdev;
  2006. adapter->mii.mdio_read = atl1e_mdio_read;
  2007. adapter->mii.mdio_write = atl1e_mdio_write;
  2008. adapter->mii.phy_id_mask = 0x1f;
  2009. adapter->mii.reg_num_mask = MDIO_REG_ADDR_MASK;
  2010. netif_napi_add(netdev, &adapter->napi, atl1e_clean, 64);
  2011. init_timer(&adapter->phy_config_timer);
  2012. adapter->phy_config_timer.function = atl1e_phy_config;
  2013. adapter->phy_config_timer.data = (unsigned long) adapter;
  2014. /* get user settings */
  2015. atl1e_check_options(adapter);
  2016. /*
  2017. * Mark all PCI regions associated with PCI device
  2018. * pdev as being reserved by owner atl1e_driver_name
  2019. * Enables bus-mastering on the device and calls
  2020. * pcibios_set_master to do the needed arch specific settings
  2021. */
  2022. atl1e_setup_pcicmd(pdev);
  2023. /* setup the private structure */
  2024. err = atl1e_sw_init(adapter);
  2025. if (err) {
  2026. netdev_err(netdev, "net device private data init failed\n");
  2027. goto err_sw_init;
  2028. }
  2029. /* Init GPHY as early as possible due to power saving issue */
  2030. atl1e_phy_init(&adapter->hw);
  2031. /* reset the controller to
  2032. * put the device in a known good starting state */
  2033. err = atl1e_reset_hw(&adapter->hw);
  2034. if (err) {
  2035. err = -EIO;
  2036. goto err_reset;
  2037. }
  2038. if (atl1e_read_mac_addr(&adapter->hw) != 0) {
  2039. err = -EIO;
  2040. netdev_err(netdev, "get mac address failed\n");
  2041. goto err_eeprom;
  2042. }
  2043. memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
  2044. netdev_dbg(netdev, "mac address : %pM\n", adapter->hw.mac_addr);
  2045. INIT_WORK(&adapter->reset_task, atl1e_reset_task);
  2046. INIT_WORK(&adapter->link_chg_task, atl1e_link_chg_task);
  2047. netif_set_gso_max_size(netdev, MAX_TSO_SEG_SIZE);
  2048. err = register_netdev(netdev);
  2049. if (err) {
  2050. netdev_err(netdev, "register netdevice failed\n");
  2051. goto err_register;
  2052. }
  2053. /* assume we have no link for now */
  2054. netif_stop_queue(netdev);
  2055. netif_carrier_off(netdev);
  2056. cards_found++;
  2057. return 0;
  2058. err_reset:
  2059. err_register:
  2060. err_sw_init:
  2061. err_eeprom:
  2062. pci_iounmap(pdev, adapter->hw.hw_addr);
  2063. err_init_netdev:
  2064. err_ioremap:
  2065. free_netdev(netdev);
  2066. err_alloc_etherdev:
  2067. pci_release_regions(pdev);
  2068. err_pci_reg:
  2069. err_dma:
  2070. pci_disable_device(pdev);
  2071. return err;
  2072. }
  2073. /**
  2074. * atl1e_remove - Device Removal Routine
  2075. * @pdev: PCI device information struct
  2076. *
  2077. * atl1e_remove is called by the PCI subsystem to alert the driver
  2078. * that it should release a PCI device. The could be caused by a
  2079. * Hot-Plug event, or because the driver is going to be removed from
  2080. * memory.
  2081. */
  2082. static void atl1e_remove(struct pci_dev *pdev)
  2083. {
  2084. struct net_device *netdev = pci_get_drvdata(pdev);
  2085. struct atl1e_adapter *adapter = netdev_priv(netdev);
  2086. /*
  2087. * flush_scheduled work may reschedule our watchdog task, so
  2088. * explicitly disable watchdog tasks from being rescheduled
  2089. */
  2090. set_bit(__AT_DOWN, &adapter->flags);
  2091. atl1e_del_timer(adapter);
  2092. atl1e_cancel_work(adapter);
  2093. unregister_netdev(netdev);
  2094. atl1e_free_ring_resources(adapter);
  2095. atl1e_force_ps(&adapter->hw);
  2096. pci_iounmap(pdev, adapter->hw.hw_addr);
  2097. pci_release_regions(pdev);
  2098. free_netdev(netdev);
  2099. pci_disable_device(pdev);
  2100. }
  2101. /**
  2102. * atl1e_io_error_detected - called when PCI error is detected
  2103. * @pdev: Pointer to PCI device
  2104. * @state: The current pci connection state
  2105. *
  2106. * This function is called after a PCI bus error affecting
  2107. * this device has been detected.
  2108. */
  2109. static pci_ers_result_t
  2110. atl1e_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  2111. {
  2112. struct net_device *netdev = pci_get_drvdata(pdev);
  2113. struct atl1e_adapter *adapter = netdev_priv(netdev);
  2114. netif_device_detach(netdev);
  2115. if (state == pci_channel_io_perm_failure)
  2116. return PCI_ERS_RESULT_DISCONNECT;
  2117. if (netif_running(netdev))
  2118. atl1e_down(adapter);
  2119. pci_disable_device(pdev);
  2120. /* Request a slot slot reset. */
  2121. return PCI_ERS_RESULT_NEED_RESET;
  2122. }
  2123. /**
  2124. * atl1e_io_slot_reset - called after the pci bus has been reset.
  2125. * @pdev: Pointer to PCI device
  2126. *
  2127. * Restart the card from scratch, as if from a cold-boot. Implementation
  2128. * resembles the first-half of the e1000_resume routine.
  2129. */
  2130. static pci_ers_result_t atl1e_io_slot_reset(struct pci_dev *pdev)
  2131. {
  2132. struct net_device *netdev = pci_get_drvdata(pdev);
  2133. struct atl1e_adapter *adapter = netdev_priv(netdev);
  2134. if (pci_enable_device(pdev)) {
  2135. netdev_err(adapter->netdev,
  2136. "Cannot re-enable PCI device after reset\n");
  2137. return PCI_ERS_RESULT_DISCONNECT;
  2138. }
  2139. pci_set_master(pdev);
  2140. pci_enable_wake(pdev, PCI_D3hot, 0);
  2141. pci_enable_wake(pdev, PCI_D3cold, 0);
  2142. atl1e_reset_hw(&adapter->hw);
  2143. return PCI_ERS_RESULT_RECOVERED;
  2144. }
  2145. /**
  2146. * atl1e_io_resume - called when traffic can start flowing again.
  2147. * @pdev: Pointer to PCI device
  2148. *
  2149. * This callback is called when the error recovery driver tells us that
  2150. * its OK to resume normal operation. Implementation resembles the
  2151. * second-half of the atl1e_resume routine.
  2152. */
  2153. static void atl1e_io_resume(struct pci_dev *pdev)
  2154. {
  2155. struct net_device *netdev = pci_get_drvdata(pdev);
  2156. struct atl1e_adapter *adapter = netdev_priv(netdev);
  2157. if (netif_running(netdev)) {
  2158. if (atl1e_up(adapter)) {
  2159. netdev_err(adapter->netdev,
  2160. "can't bring device back up after reset\n");
  2161. return;
  2162. }
  2163. }
  2164. netif_device_attach(netdev);
  2165. }
  2166. static const struct pci_error_handlers atl1e_err_handler = {
  2167. .error_detected = atl1e_io_error_detected,
  2168. .slot_reset = atl1e_io_slot_reset,
  2169. .resume = atl1e_io_resume,
  2170. };
  2171. static struct pci_driver atl1e_driver = {
  2172. .name = atl1e_driver_name,
  2173. .id_table = atl1e_pci_tbl,
  2174. .probe = atl1e_probe,
  2175. .remove = atl1e_remove,
  2176. /* Power Management Hooks */
  2177. #ifdef CONFIG_PM
  2178. .suspend = atl1e_suspend,
  2179. .resume = atl1e_resume,
  2180. #endif
  2181. .shutdown = atl1e_shutdown,
  2182. .err_handler = &atl1e_err_handler
  2183. };
  2184. module_pci_driver(atl1e_driver);