atl1c_main.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913
  1. /*
  2. * Copyright(c) 2008 - 2009 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 "atl1c.h"
  22. #define ATL1C_DRV_VERSION "1.0.1.0-NAPI"
  23. char atl1c_driver_name[] = "atl1c";
  24. char atl1c_driver_version[] = ATL1C_DRV_VERSION;
  25. #define PCI_DEVICE_ID_ATTANSIC_L2C 0x1062
  26. #define PCI_DEVICE_ID_ATTANSIC_L1C 0x1063
  27. #define PCI_DEVICE_ID_ATHEROS_L2C_B 0x2060 /* AR8152 v1.1 Fast 10/100 */
  28. #define PCI_DEVICE_ID_ATHEROS_L2C_B2 0x2062 /* AR8152 v2.0 Fast 10/100 */
  29. #define PCI_DEVICE_ID_ATHEROS_L1D 0x1073 /* AR8151 v1.0 Gigabit 1000 */
  30. #define PCI_DEVICE_ID_ATHEROS_L1D_2_0 0x1083 /* AR8151 v2.0 Gigabit 1000 */
  31. #define L2CB_V10 0xc0
  32. #define L2CB_V11 0xc1
  33. /*
  34. * atl1c_pci_tbl - PCI Device ID Table
  35. *
  36. * Wildcard entries (PCI_ANY_ID) should come last
  37. * Last entry must be all 0s
  38. *
  39. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  40. * Class, Class Mask, private data (not used) }
  41. */
  42. static DEFINE_PCI_DEVICE_TABLE(atl1c_pci_tbl) = {
  43. {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATTANSIC_L1C)},
  44. {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATTANSIC_L2C)},
  45. {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATHEROS_L2C_B)},
  46. {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATHEROS_L2C_B2)},
  47. {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATHEROS_L1D)},
  48. {PCI_DEVICE(PCI_VENDOR_ID_ATTANSIC, PCI_DEVICE_ID_ATHEROS_L1D_2_0)},
  49. /* required last entry */
  50. { 0 }
  51. };
  52. MODULE_DEVICE_TABLE(pci, atl1c_pci_tbl);
  53. MODULE_AUTHOR("Jie Yang <jie.yang@atheros.com>");
  54. MODULE_DESCRIPTION("Atheros 1000M Ethernet Network Driver");
  55. MODULE_LICENSE("GPL");
  56. MODULE_VERSION(ATL1C_DRV_VERSION);
  57. static int atl1c_stop_mac(struct atl1c_hw *hw);
  58. static void atl1c_enable_rx_ctrl(struct atl1c_hw *hw);
  59. static void atl1c_enable_tx_ctrl(struct atl1c_hw *hw);
  60. static void atl1c_disable_l0s_l1(struct atl1c_hw *hw);
  61. static void atl1c_set_aspm(struct atl1c_hw *hw, bool linkup);
  62. static void atl1c_setup_mac_ctrl(struct atl1c_adapter *adapter);
  63. static void atl1c_clean_rx_irq(struct atl1c_adapter *adapter, u8 que,
  64. int *work_done, int work_to_do);
  65. static int atl1c_up(struct atl1c_adapter *adapter);
  66. static void atl1c_down(struct atl1c_adapter *adapter);
  67. static const u16 atl1c_pay_load_size[] = {
  68. 128, 256, 512, 1024, 2048, 4096,
  69. };
  70. static const u16 atl1c_rfd_prod_idx_regs[AT_MAX_RECEIVE_QUEUE] =
  71. {
  72. REG_MB_RFD0_PROD_IDX,
  73. REG_MB_RFD1_PROD_IDX,
  74. REG_MB_RFD2_PROD_IDX,
  75. REG_MB_RFD3_PROD_IDX
  76. };
  77. static const u16 atl1c_rfd_addr_lo_regs[AT_MAX_RECEIVE_QUEUE] =
  78. {
  79. REG_RFD0_HEAD_ADDR_LO,
  80. REG_RFD1_HEAD_ADDR_LO,
  81. REG_RFD2_HEAD_ADDR_LO,
  82. REG_RFD3_HEAD_ADDR_LO
  83. };
  84. static const u16 atl1c_rrd_addr_lo_regs[AT_MAX_RECEIVE_QUEUE] =
  85. {
  86. REG_RRD0_HEAD_ADDR_LO,
  87. REG_RRD1_HEAD_ADDR_LO,
  88. REG_RRD2_HEAD_ADDR_LO,
  89. REG_RRD3_HEAD_ADDR_LO
  90. };
  91. static const u32 atl1c_default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE |
  92. NETIF_MSG_LINK | NETIF_MSG_TIMER | NETIF_MSG_IFDOWN | NETIF_MSG_IFUP;
  93. static void atl1c_pcie_patch(struct atl1c_hw *hw)
  94. {
  95. u32 data;
  96. AT_READ_REG(hw, REG_PCIE_PHYMISC, &data);
  97. data |= PCIE_PHYMISC_FORCE_RCV_DET;
  98. AT_WRITE_REG(hw, REG_PCIE_PHYMISC, data);
  99. if (hw->nic_type == athr_l2c_b && hw->revision_id == L2CB_V10) {
  100. AT_READ_REG(hw, REG_PCIE_PHYMISC2, &data);
  101. data &= ~(PCIE_PHYMISC2_SERDES_CDR_MASK <<
  102. PCIE_PHYMISC2_SERDES_CDR_SHIFT);
  103. data |= 3 << PCIE_PHYMISC2_SERDES_CDR_SHIFT;
  104. data &= ~(PCIE_PHYMISC2_SERDES_TH_MASK <<
  105. PCIE_PHYMISC2_SERDES_TH_SHIFT);
  106. data |= 3 << PCIE_PHYMISC2_SERDES_TH_SHIFT;
  107. AT_WRITE_REG(hw, REG_PCIE_PHYMISC2, data);
  108. }
  109. }
  110. /* FIXME: no need any more ? */
  111. /*
  112. * atl1c_init_pcie - init PCIE module
  113. */
  114. static void atl1c_reset_pcie(struct atl1c_hw *hw, u32 flag)
  115. {
  116. u32 data;
  117. u32 pci_cmd;
  118. struct pci_dev *pdev = hw->adapter->pdev;
  119. AT_READ_REG(hw, PCI_COMMAND, &pci_cmd);
  120. pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
  121. pci_cmd |= (PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
  122. PCI_COMMAND_IO);
  123. AT_WRITE_REG(hw, PCI_COMMAND, pci_cmd);
  124. /*
  125. * Clear any PowerSaveing Settings
  126. */
  127. pci_enable_wake(pdev, PCI_D3hot, 0);
  128. pci_enable_wake(pdev, PCI_D3cold, 0);
  129. /*
  130. * Mask some pcie error bits
  131. */
  132. AT_READ_REG(hw, REG_PCIE_UC_SEVERITY, &data);
  133. data &= ~PCIE_UC_SERVRITY_DLP;
  134. data &= ~PCIE_UC_SERVRITY_FCP;
  135. AT_WRITE_REG(hw, REG_PCIE_UC_SEVERITY, data);
  136. AT_READ_REG(hw, REG_LTSSM_ID_CTRL, &data);
  137. data &= ~LTSSM_ID_EN_WRO;
  138. AT_WRITE_REG(hw, REG_LTSSM_ID_CTRL, data);
  139. atl1c_pcie_patch(hw);
  140. if (flag & ATL1C_PCIE_L0S_L1_DISABLE)
  141. atl1c_disable_l0s_l1(hw);
  142. if (flag & ATL1C_PCIE_PHY_RESET)
  143. AT_WRITE_REG(hw, REG_GPHY_CTRL, GPHY_CTRL_DEFAULT);
  144. else
  145. AT_WRITE_REG(hw, REG_GPHY_CTRL,
  146. GPHY_CTRL_DEFAULT | GPHY_CTRL_EXT_RESET);
  147. msleep(5);
  148. }
  149. /*
  150. * atl1c_irq_enable - Enable default interrupt generation settings
  151. * @adapter: board private structure
  152. */
  153. static inline void atl1c_irq_enable(struct atl1c_adapter *adapter)
  154. {
  155. if (likely(atomic_dec_and_test(&adapter->irq_sem))) {
  156. AT_WRITE_REG(&adapter->hw, REG_ISR, 0x7FFFFFFF);
  157. AT_WRITE_REG(&adapter->hw, REG_IMR, adapter->hw.intr_mask);
  158. AT_WRITE_FLUSH(&adapter->hw);
  159. }
  160. }
  161. /*
  162. * atl1c_irq_disable - Mask off interrupt generation on the NIC
  163. * @adapter: board private structure
  164. */
  165. static inline void atl1c_irq_disable(struct atl1c_adapter *adapter)
  166. {
  167. atomic_inc(&adapter->irq_sem);
  168. AT_WRITE_REG(&adapter->hw, REG_IMR, 0);
  169. AT_WRITE_REG(&adapter->hw, REG_ISR, ISR_DIS_INT);
  170. AT_WRITE_FLUSH(&adapter->hw);
  171. synchronize_irq(adapter->pdev->irq);
  172. }
  173. /*
  174. * atl1c_irq_reset - reset interrupt confiure on the NIC
  175. * @adapter: board private structure
  176. */
  177. static inline void atl1c_irq_reset(struct atl1c_adapter *adapter)
  178. {
  179. atomic_set(&adapter->irq_sem, 1);
  180. atl1c_irq_enable(adapter);
  181. }
  182. /*
  183. * atl1c_wait_until_idle - wait up to AT_HW_MAX_IDLE_DELAY reads
  184. * of the idle status register until the device is actually idle
  185. */
  186. static u32 atl1c_wait_until_idle(struct atl1c_hw *hw)
  187. {
  188. int timeout;
  189. u32 data;
  190. for (timeout = 0; timeout < AT_HW_MAX_IDLE_DELAY; timeout++) {
  191. AT_READ_REG(hw, REG_IDLE_STATUS, &data);
  192. if ((data & IDLE_STATUS_MASK) == 0)
  193. return 0;
  194. msleep(1);
  195. }
  196. return data;
  197. }
  198. /*
  199. * atl1c_phy_config - Timer Call-back
  200. * @data: pointer to netdev cast into an unsigned long
  201. */
  202. static void atl1c_phy_config(unsigned long data)
  203. {
  204. struct atl1c_adapter *adapter = (struct atl1c_adapter *) data;
  205. struct atl1c_hw *hw = &adapter->hw;
  206. unsigned long flags;
  207. spin_lock_irqsave(&adapter->mdio_lock, flags);
  208. atl1c_restart_autoneg(hw);
  209. spin_unlock_irqrestore(&adapter->mdio_lock, flags);
  210. }
  211. void atl1c_reinit_locked(struct atl1c_adapter *adapter)
  212. {
  213. WARN_ON(in_interrupt());
  214. atl1c_down(adapter);
  215. atl1c_up(adapter);
  216. clear_bit(__AT_RESETTING, &adapter->flags);
  217. }
  218. static void atl1c_check_link_status(struct atl1c_adapter *adapter)
  219. {
  220. struct atl1c_hw *hw = &adapter->hw;
  221. struct net_device *netdev = adapter->netdev;
  222. struct pci_dev *pdev = adapter->pdev;
  223. int err;
  224. unsigned long flags;
  225. u16 speed, duplex, phy_data;
  226. spin_lock_irqsave(&adapter->mdio_lock, flags);
  227. /* MII_BMSR must read twise */
  228. atl1c_read_phy_reg(hw, MII_BMSR, &phy_data);
  229. atl1c_read_phy_reg(hw, MII_BMSR, &phy_data);
  230. spin_unlock_irqrestore(&adapter->mdio_lock, flags);
  231. if ((phy_data & BMSR_LSTATUS) == 0) {
  232. /* link down */
  233. hw->hibernate = true;
  234. if (atl1c_stop_mac(hw) != 0)
  235. if (netif_msg_hw(adapter))
  236. dev_warn(&pdev->dev, "stop mac failed\n");
  237. atl1c_set_aspm(hw, false);
  238. netif_carrier_off(netdev);
  239. netif_stop_queue(netdev);
  240. atl1c_phy_reset(hw);
  241. atl1c_phy_init(&adapter->hw);
  242. } else {
  243. /* Link Up */
  244. hw->hibernate = false;
  245. spin_lock_irqsave(&adapter->mdio_lock, flags);
  246. err = atl1c_get_speed_and_duplex(hw, &speed, &duplex);
  247. spin_unlock_irqrestore(&adapter->mdio_lock, flags);
  248. if (unlikely(err))
  249. return;
  250. /* link result is our setting */
  251. if (adapter->link_speed != speed ||
  252. adapter->link_duplex != duplex) {
  253. adapter->link_speed = speed;
  254. adapter->link_duplex = duplex;
  255. atl1c_set_aspm(hw, true);
  256. atl1c_enable_tx_ctrl(hw);
  257. atl1c_enable_rx_ctrl(hw);
  258. atl1c_setup_mac_ctrl(adapter);
  259. if (netif_msg_link(adapter))
  260. dev_info(&pdev->dev,
  261. "%s: %s NIC Link is Up<%d Mbps %s>\n",
  262. atl1c_driver_name, netdev->name,
  263. adapter->link_speed,
  264. adapter->link_duplex == FULL_DUPLEX ?
  265. "Full Duplex" : "Half Duplex");
  266. }
  267. if (!netif_carrier_ok(netdev))
  268. netif_carrier_on(netdev);
  269. }
  270. }
  271. static void atl1c_link_chg_event(struct atl1c_adapter *adapter)
  272. {
  273. struct net_device *netdev = adapter->netdev;
  274. struct pci_dev *pdev = adapter->pdev;
  275. u16 phy_data;
  276. u16 link_up;
  277. spin_lock(&adapter->mdio_lock);
  278. atl1c_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
  279. atl1c_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
  280. spin_unlock(&adapter->mdio_lock);
  281. link_up = phy_data & BMSR_LSTATUS;
  282. /* notify upper layer link down ASAP */
  283. if (!link_up) {
  284. if (netif_carrier_ok(netdev)) {
  285. /* old link state: Up */
  286. netif_carrier_off(netdev);
  287. if (netif_msg_link(adapter))
  288. dev_info(&pdev->dev,
  289. "%s: %s NIC Link is Down\n",
  290. atl1c_driver_name, netdev->name);
  291. adapter->link_speed = SPEED_0;
  292. }
  293. }
  294. set_bit(ATL1C_WORK_EVENT_LINK_CHANGE, &adapter->work_event);
  295. schedule_work(&adapter->common_task);
  296. }
  297. static void atl1c_common_task(struct work_struct *work)
  298. {
  299. struct atl1c_adapter *adapter;
  300. struct net_device *netdev;
  301. adapter = container_of(work, struct atl1c_adapter, common_task);
  302. netdev = adapter->netdev;
  303. if (test_and_clear_bit(ATL1C_WORK_EVENT_RESET, &adapter->work_event)) {
  304. netif_device_detach(netdev);
  305. atl1c_down(adapter);
  306. atl1c_up(adapter);
  307. netif_device_attach(netdev);
  308. }
  309. if (test_and_clear_bit(ATL1C_WORK_EVENT_LINK_CHANGE,
  310. &adapter->work_event))
  311. atl1c_check_link_status(adapter);
  312. }
  313. static void atl1c_del_timer(struct atl1c_adapter *adapter)
  314. {
  315. del_timer_sync(&adapter->phy_config_timer);
  316. }
  317. /*
  318. * atl1c_tx_timeout - Respond to a Tx Hang
  319. * @netdev: network interface device structure
  320. */
  321. static void atl1c_tx_timeout(struct net_device *netdev)
  322. {
  323. struct atl1c_adapter *adapter = netdev_priv(netdev);
  324. /* Do the reset outside of interrupt context */
  325. set_bit(ATL1C_WORK_EVENT_RESET, &adapter->work_event);
  326. schedule_work(&adapter->common_task);
  327. }
  328. /*
  329. * atl1c_set_multi - Multicast and Promiscuous mode set
  330. * @netdev: network interface device structure
  331. *
  332. * The set_multi entry point is called whenever the multicast address
  333. * list or the network interface flags are updated. This routine is
  334. * responsible for configuring the hardware for proper multicast,
  335. * promiscuous mode, and all-multi behavior.
  336. */
  337. static void atl1c_set_multi(struct net_device *netdev)
  338. {
  339. struct atl1c_adapter *adapter = netdev_priv(netdev);
  340. struct atl1c_hw *hw = &adapter->hw;
  341. struct netdev_hw_addr *ha;
  342. u32 mac_ctrl_data;
  343. u32 hash_value;
  344. /* Check for Promiscuous and All Multicast modes */
  345. AT_READ_REG(hw, REG_MAC_CTRL, &mac_ctrl_data);
  346. if (netdev->flags & IFF_PROMISC) {
  347. mac_ctrl_data |= MAC_CTRL_PROMIS_EN;
  348. } else if (netdev->flags & IFF_ALLMULTI) {
  349. mac_ctrl_data |= MAC_CTRL_MC_ALL_EN;
  350. mac_ctrl_data &= ~MAC_CTRL_PROMIS_EN;
  351. } else {
  352. mac_ctrl_data &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN);
  353. }
  354. AT_WRITE_REG(hw, REG_MAC_CTRL, mac_ctrl_data);
  355. /* clear the old settings from the multicast hash table */
  356. AT_WRITE_REG(hw, REG_RX_HASH_TABLE, 0);
  357. AT_WRITE_REG_ARRAY(hw, REG_RX_HASH_TABLE, 1, 0);
  358. /* comoute mc addresses' hash value ,and put it into hash table */
  359. netdev_for_each_mc_addr(ha, netdev) {
  360. hash_value = atl1c_hash_mc_addr(hw, ha->addr);
  361. atl1c_hash_set(hw, hash_value);
  362. }
  363. }
  364. static void atl1c_vlan_rx_register(struct net_device *netdev,
  365. struct vlan_group *grp)
  366. {
  367. struct atl1c_adapter *adapter = netdev_priv(netdev);
  368. struct pci_dev *pdev = adapter->pdev;
  369. u32 mac_ctrl_data = 0;
  370. if (netif_msg_pktdata(adapter))
  371. dev_dbg(&pdev->dev, "atl1c_vlan_rx_register\n");
  372. atl1c_irq_disable(adapter);
  373. adapter->vlgrp = grp;
  374. AT_READ_REG(&adapter->hw, REG_MAC_CTRL, &mac_ctrl_data);
  375. if (grp) {
  376. /* enable VLAN tag insert/strip */
  377. mac_ctrl_data |= MAC_CTRL_RMV_VLAN;
  378. } else {
  379. /* disable VLAN tag insert/strip */
  380. mac_ctrl_data &= ~MAC_CTRL_RMV_VLAN;
  381. }
  382. AT_WRITE_REG(&adapter->hw, REG_MAC_CTRL, mac_ctrl_data);
  383. atl1c_irq_enable(adapter);
  384. }
  385. static void atl1c_restore_vlan(struct atl1c_adapter *adapter)
  386. {
  387. struct pci_dev *pdev = adapter->pdev;
  388. if (netif_msg_pktdata(adapter))
  389. dev_dbg(&pdev->dev, "atl1c_restore_vlan !");
  390. atl1c_vlan_rx_register(adapter->netdev, adapter->vlgrp);
  391. }
  392. /*
  393. * atl1c_set_mac - Change the Ethernet Address of the NIC
  394. * @netdev: network interface device structure
  395. * @p: pointer to an address structure
  396. *
  397. * Returns 0 on success, negative on failure
  398. */
  399. static int atl1c_set_mac_addr(struct net_device *netdev, void *p)
  400. {
  401. struct atl1c_adapter *adapter = netdev_priv(netdev);
  402. struct sockaddr *addr = p;
  403. if (!is_valid_ether_addr(addr->sa_data))
  404. return -EADDRNOTAVAIL;
  405. if (netif_running(netdev))
  406. return -EBUSY;
  407. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  408. memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
  409. atl1c_hw_set_mac_addr(&adapter->hw);
  410. return 0;
  411. }
  412. static void atl1c_set_rxbufsize(struct atl1c_adapter *adapter,
  413. struct net_device *dev)
  414. {
  415. int mtu = dev->mtu;
  416. adapter->rx_buffer_len = mtu > AT_RX_BUF_SIZE ?
  417. roundup(mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN, 8) : AT_RX_BUF_SIZE;
  418. }
  419. static u32 atl1c_fix_features(struct net_device *netdev, u32 features)
  420. {
  421. if (netdev->mtu > MAX_TSO_FRAME_SIZE)
  422. features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
  423. return features;
  424. }
  425. /*
  426. * atl1c_change_mtu - Change the Maximum Transfer Unit
  427. * @netdev: network interface device structure
  428. * @new_mtu: new value for maximum frame size
  429. *
  430. * Returns 0 on success, negative on failure
  431. */
  432. static int atl1c_change_mtu(struct net_device *netdev, int new_mtu)
  433. {
  434. struct atl1c_adapter *adapter = netdev_priv(netdev);
  435. int old_mtu = netdev->mtu;
  436. int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  437. if ((max_frame < ETH_ZLEN + ETH_FCS_LEN) ||
  438. (max_frame > MAX_JUMBO_FRAME_SIZE)) {
  439. if (netif_msg_link(adapter))
  440. dev_warn(&adapter->pdev->dev, "invalid MTU setting\n");
  441. return -EINVAL;
  442. }
  443. /* set MTU */
  444. if (old_mtu != new_mtu && netif_running(netdev)) {
  445. while (test_and_set_bit(__AT_RESETTING, &adapter->flags))
  446. msleep(1);
  447. netdev->mtu = new_mtu;
  448. adapter->hw.max_frame_size = new_mtu;
  449. atl1c_set_rxbufsize(adapter, netdev);
  450. atl1c_down(adapter);
  451. netdev_update_features(netdev);
  452. atl1c_up(adapter);
  453. clear_bit(__AT_RESETTING, &adapter->flags);
  454. if (adapter->hw.ctrl_flags & ATL1C_FPGA_VERSION) {
  455. u32 phy_data;
  456. AT_READ_REG(&adapter->hw, 0x1414, &phy_data);
  457. phy_data |= 0x10000000;
  458. AT_WRITE_REG(&adapter->hw, 0x1414, phy_data);
  459. }
  460. }
  461. return 0;
  462. }
  463. /*
  464. * caller should hold mdio_lock
  465. */
  466. static int atl1c_mdio_read(struct net_device *netdev, int phy_id, int reg_num)
  467. {
  468. struct atl1c_adapter *adapter = netdev_priv(netdev);
  469. u16 result;
  470. atl1c_read_phy_reg(&adapter->hw, reg_num & MDIO_REG_ADDR_MASK, &result);
  471. return result;
  472. }
  473. static void atl1c_mdio_write(struct net_device *netdev, int phy_id,
  474. int reg_num, int val)
  475. {
  476. struct atl1c_adapter *adapter = netdev_priv(netdev);
  477. atl1c_write_phy_reg(&adapter->hw, reg_num & MDIO_REG_ADDR_MASK, val);
  478. }
  479. /*
  480. * atl1c_mii_ioctl -
  481. * @netdev:
  482. * @ifreq:
  483. * @cmd:
  484. */
  485. static int atl1c_mii_ioctl(struct net_device *netdev,
  486. struct ifreq *ifr, int cmd)
  487. {
  488. struct atl1c_adapter *adapter = netdev_priv(netdev);
  489. struct pci_dev *pdev = adapter->pdev;
  490. struct mii_ioctl_data *data = if_mii(ifr);
  491. unsigned long flags;
  492. int retval = 0;
  493. if (!netif_running(netdev))
  494. return -EINVAL;
  495. spin_lock_irqsave(&adapter->mdio_lock, flags);
  496. switch (cmd) {
  497. case SIOCGMIIPHY:
  498. data->phy_id = 0;
  499. break;
  500. case SIOCGMIIREG:
  501. if (atl1c_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
  502. &data->val_out)) {
  503. retval = -EIO;
  504. goto out;
  505. }
  506. break;
  507. case SIOCSMIIREG:
  508. if (data->reg_num & ~(0x1F)) {
  509. retval = -EFAULT;
  510. goto out;
  511. }
  512. dev_dbg(&pdev->dev, "<atl1c_mii_ioctl> write %x %x",
  513. data->reg_num, data->val_in);
  514. if (atl1c_write_phy_reg(&adapter->hw,
  515. data->reg_num, data->val_in)) {
  516. retval = -EIO;
  517. goto out;
  518. }
  519. break;
  520. default:
  521. retval = -EOPNOTSUPP;
  522. break;
  523. }
  524. out:
  525. spin_unlock_irqrestore(&adapter->mdio_lock, flags);
  526. return retval;
  527. }
  528. /*
  529. * atl1c_ioctl -
  530. * @netdev:
  531. * @ifreq:
  532. * @cmd:
  533. */
  534. static int atl1c_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  535. {
  536. switch (cmd) {
  537. case SIOCGMIIPHY:
  538. case SIOCGMIIREG:
  539. case SIOCSMIIREG:
  540. return atl1c_mii_ioctl(netdev, ifr, cmd);
  541. default:
  542. return -EOPNOTSUPP;
  543. }
  544. }
  545. /*
  546. * atl1c_alloc_queues - Allocate memory for all rings
  547. * @adapter: board private structure to initialize
  548. *
  549. */
  550. static int __devinit atl1c_alloc_queues(struct atl1c_adapter *adapter)
  551. {
  552. return 0;
  553. }
  554. static void atl1c_set_mac_type(struct atl1c_hw *hw)
  555. {
  556. switch (hw->device_id) {
  557. case PCI_DEVICE_ID_ATTANSIC_L2C:
  558. hw->nic_type = athr_l2c;
  559. break;
  560. case PCI_DEVICE_ID_ATTANSIC_L1C:
  561. hw->nic_type = athr_l1c;
  562. break;
  563. case PCI_DEVICE_ID_ATHEROS_L2C_B:
  564. hw->nic_type = athr_l2c_b;
  565. break;
  566. case PCI_DEVICE_ID_ATHEROS_L2C_B2:
  567. hw->nic_type = athr_l2c_b2;
  568. break;
  569. case PCI_DEVICE_ID_ATHEROS_L1D:
  570. hw->nic_type = athr_l1d;
  571. break;
  572. case PCI_DEVICE_ID_ATHEROS_L1D_2_0:
  573. hw->nic_type = athr_l1d_2;
  574. break;
  575. default:
  576. break;
  577. }
  578. }
  579. static int atl1c_setup_mac_funcs(struct atl1c_hw *hw)
  580. {
  581. u32 phy_status_data;
  582. u32 link_ctrl_data;
  583. atl1c_set_mac_type(hw);
  584. AT_READ_REG(hw, REG_PHY_STATUS, &phy_status_data);
  585. AT_READ_REG(hw, REG_LINK_CTRL, &link_ctrl_data);
  586. hw->ctrl_flags = ATL1C_INTR_MODRT_ENABLE |
  587. ATL1C_TXQ_MODE_ENHANCE;
  588. if (link_ctrl_data & LINK_CTRL_L0S_EN)
  589. hw->ctrl_flags |= ATL1C_ASPM_L0S_SUPPORT;
  590. if (link_ctrl_data & LINK_CTRL_L1_EN)
  591. hw->ctrl_flags |= ATL1C_ASPM_L1_SUPPORT;
  592. if (link_ctrl_data & LINK_CTRL_EXT_SYNC)
  593. hw->ctrl_flags |= ATL1C_LINK_EXT_SYNC;
  594. hw->ctrl_flags |= ATL1C_ASPM_CTRL_MON;
  595. if (hw->nic_type == athr_l1c ||
  596. hw->nic_type == athr_l1d ||
  597. hw->nic_type == athr_l1d_2)
  598. hw->link_cap_flags |= ATL1C_LINK_CAP_1000M;
  599. return 0;
  600. }
  601. /*
  602. * atl1c_sw_init - Initialize general software structures (struct atl1c_adapter)
  603. * @adapter: board private structure to initialize
  604. *
  605. * atl1c_sw_init initializes the Adapter private data structure.
  606. * Fields are initialized based on PCI device information and
  607. * OS network device settings (MTU size).
  608. */
  609. static int __devinit atl1c_sw_init(struct atl1c_adapter *adapter)
  610. {
  611. struct atl1c_hw *hw = &adapter->hw;
  612. struct pci_dev *pdev = adapter->pdev;
  613. u32 revision;
  614. adapter->wol = 0;
  615. device_set_wakeup_enable(&pdev->dev, false);
  616. adapter->link_speed = SPEED_0;
  617. adapter->link_duplex = FULL_DUPLEX;
  618. adapter->num_rx_queues = AT_DEF_RECEIVE_QUEUE;
  619. adapter->tpd_ring[0].count = 1024;
  620. adapter->rfd_ring[0].count = 512;
  621. hw->vendor_id = pdev->vendor;
  622. hw->device_id = pdev->device;
  623. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  624. hw->subsystem_id = pdev->subsystem_device;
  625. AT_READ_REG(hw, PCI_CLASS_REVISION, &revision);
  626. hw->revision_id = revision & 0xFF;
  627. /* before link up, we assume hibernate is true */
  628. hw->hibernate = true;
  629. hw->media_type = MEDIA_TYPE_AUTO_SENSOR;
  630. if (atl1c_setup_mac_funcs(hw) != 0) {
  631. dev_err(&pdev->dev, "set mac function pointers failed\n");
  632. return -1;
  633. }
  634. hw->intr_mask = IMR_NORMAL_MASK;
  635. hw->phy_configured = false;
  636. hw->preamble_len = 7;
  637. hw->max_frame_size = adapter->netdev->mtu;
  638. if (adapter->num_rx_queues < 2) {
  639. hw->rss_type = atl1c_rss_disable;
  640. hw->rss_mode = atl1c_rss_mode_disable;
  641. } else {
  642. hw->rss_type = atl1c_rss_ipv4;
  643. hw->rss_mode = atl1c_rss_mul_que_mul_int;
  644. hw->rss_hash_bits = 16;
  645. }
  646. hw->autoneg_advertised = ADVERTISED_Autoneg;
  647. hw->indirect_tab = 0xE4E4E4E4;
  648. hw->base_cpu = 0;
  649. hw->ict = 50000; /* 100ms */
  650. hw->smb_timer = 200000; /* 400ms */
  651. hw->cmb_tpd = 4;
  652. hw->cmb_tx_timer = 1; /* 2 us */
  653. hw->rx_imt = 200;
  654. hw->tx_imt = 1000;
  655. hw->tpd_burst = 5;
  656. hw->rfd_burst = 8;
  657. hw->dma_order = atl1c_dma_ord_out;
  658. hw->dmar_block = atl1c_dma_req_1024;
  659. hw->dmaw_block = atl1c_dma_req_1024;
  660. hw->dmar_dly_cnt = 15;
  661. hw->dmaw_dly_cnt = 4;
  662. if (atl1c_alloc_queues(adapter)) {
  663. dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
  664. return -ENOMEM;
  665. }
  666. /* TODO */
  667. atl1c_set_rxbufsize(adapter, adapter->netdev);
  668. atomic_set(&adapter->irq_sem, 1);
  669. spin_lock_init(&adapter->mdio_lock);
  670. spin_lock_init(&adapter->tx_lock);
  671. set_bit(__AT_DOWN, &adapter->flags);
  672. return 0;
  673. }
  674. static inline void atl1c_clean_buffer(struct pci_dev *pdev,
  675. struct atl1c_buffer *buffer_info, int in_irq)
  676. {
  677. u16 pci_driection;
  678. if (buffer_info->flags & ATL1C_BUFFER_FREE)
  679. return;
  680. if (buffer_info->dma) {
  681. if (buffer_info->flags & ATL1C_PCIMAP_FROMDEVICE)
  682. pci_driection = PCI_DMA_FROMDEVICE;
  683. else
  684. pci_driection = PCI_DMA_TODEVICE;
  685. if (buffer_info->flags & ATL1C_PCIMAP_SINGLE)
  686. pci_unmap_single(pdev, buffer_info->dma,
  687. buffer_info->length, pci_driection);
  688. else if (buffer_info->flags & ATL1C_PCIMAP_PAGE)
  689. pci_unmap_page(pdev, buffer_info->dma,
  690. buffer_info->length, pci_driection);
  691. }
  692. if (buffer_info->skb) {
  693. if (in_irq)
  694. dev_kfree_skb_irq(buffer_info->skb);
  695. else
  696. dev_kfree_skb(buffer_info->skb);
  697. }
  698. buffer_info->dma = 0;
  699. buffer_info->skb = NULL;
  700. ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_FREE);
  701. }
  702. /*
  703. * atl1c_clean_tx_ring - Free Tx-skb
  704. * @adapter: board private structure
  705. */
  706. static void atl1c_clean_tx_ring(struct atl1c_adapter *adapter,
  707. enum atl1c_trans_queue type)
  708. {
  709. struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
  710. struct atl1c_buffer *buffer_info;
  711. struct pci_dev *pdev = adapter->pdev;
  712. u16 index, ring_count;
  713. ring_count = tpd_ring->count;
  714. for (index = 0; index < ring_count; index++) {
  715. buffer_info = &tpd_ring->buffer_info[index];
  716. atl1c_clean_buffer(pdev, buffer_info, 0);
  717. }
  718. /* Zero out Tx-buffers */
  719. memset(tpd_ring->desc, 0, sizeof(struct atl1c_tpd_desc) *
  720. ring_count);
  721. atomic_set(&tpd_ring->next_to_clean, 0);
  722. tpd_ring->next_to_use = 0;
  723. }
  724. /*
  725. * atl1c_clean_rx_ring - Free rx-reservation skbs
  726. * @adapter: board private structure
  727. */
  728. static void atl1c_clean_rx_ring(struct atl1c_adapter *adapter)
  729. {
  730. struct atl1c_rfd_ring *rfd_ring = adapter->rfd_ring;
  731. struct atl1c_rrd_ring *rrd_ring = adapter->rrd_ring;
  732. struct atl1c_buffer *buffer_info;
  733. struct pci_dev *pdev = adapter->pdev;
  734. int i, j;
  735. for (i = 0; i < adapter->num_rx_queues; i++) {
  736. for (j = 0; j < rfd_ring[i].count; j++) {
  737. buffer_info = &rfd_ring[i].buffer_info[j];
  738. atl1c_clean_buffer(pdev, buffer_info, 0);
  739. }
  740. /* zero out the descriptor ring */
  741. memset(rfd_ring[i].desc, 0, rfd_ring[i].size);
  742. rfd_ring[i].next_to_clean = 0;
  743. rfd_ring[i].next_to_use = 0;
  744. rrd_ring[i].next_to_use = 0;
  745. rrd_ring[i].next_to_clean = 0;
  746. }
  747. }
  748. /*
  749. * Read / Write Ptr Initialize:
  750. */
  751. static void atl1c_init_ring_ptrs(struct atl1c_adapter *adapter)
  752. {
  753. struct atl1c_tpd_ring *tpd_ring = adapter->tpd_ring;
  754. struct atl1c_rfd_ring *rfd_ring = adapter->rfd_ring;
  755. struct atl1c_rrd_ring *rrd_ring = adapter->rrd_ring;
  756. struct atl1c_buffer *buffer_info;
  757. int i, j;
  758. for (i = 0; i < AT_MAX_TRANSMIT_QUEUE; i++) {
  759. tpd_ring[i].next_to_use = 0;
  760. atomic_set(&tpd_ring[i].next_to_clean, 0);
  761. buffer_info = tpd_ring[i].buffer_info;
  762. for (j = 0; j < tpd_ring->count; j++)
  763. ATL1C_SET_BUFFER_STATE(&buffer_info[i],
  764. ATL1C_BUFFER_FREE);
  765. }
  766. for (i = 0; i < adapter->num_rx_queues; i++) {
  767. rfd_ring[i].next_to_use = 0;
  768. rfd_ring[i].next_to_clean = 0;
  769. rrd_ring[i].next_to_use = 0;
  770. rrd_ring[i].next_to_clean = 0;
  771. for (j = 0; j < rfd_ring[i].count; j++) {
  772. buffer_info = &rfd_ring[i].buffer_info[j];
  773. ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_FREE);
  774. }
  775. }
  776. }
  777. /*
  778. * atl1c_free_ring_resources - Free Tx / RX descriptor Resources
  779. * @adapter: board private structure
  780. *
  781. * Free all transmit software resources
  782. */
  783. static void atl1c_free_ring_resources(struct atl1c_adapter *adapter)
  784. {
  785. struct pci_dev *pdev = adapter->pdev;
  786. pci_free_consistent(pdev, adapter->ring_header.size,
  787. adapter->ring_header.desc,
  788. adapter->ring_header.dma);
  789. adapter->ring_header.desc = NULL;
  790. /* Note: just free tdp_ring.buffer_info,
  791. * it contain rfd_ring.buffer_info, do not double free */
  792. if (adapter->tpd_ring[0].buffer_info) {
  793. kfree(adapter->tpd_ring[0].buffer_info);
  794. adapter->tpd_ring[0].buffer_info = NULL;
  795. }
  796. }
  797. /*
  798. * atl1c_setup_mem_resources - allocate Tx / RX descriptor resources
  799. * @adapter: board private structure
  800. *
  801. * Return 0 on success, negative on failure
  802. */
  803. static int atl1c_setup_ring_resources(struct atl1c_adapter *adapter)
  804. {
  805. struct pci_dev *pdev = adapter->pdev;
  806. struct atl1c_tpd_ring *tpd_ring = adapter->tpd_ring;
  807. struct atl1c_rfd_ring *rfd_ring = adapter->rfd_ring;
  808. struct atl1c_rrd_ring *rrd_ring = adapter->rrd_ring;
  809. struct atl1c_ring_header *ring_header = &adapter->ring_header;
  810. int num_rx_queues = adapter->num_rx_queues;
  811. int size;
  812. int i;
  813. int count = 0;
  814. int rx_desc_count = 0;
  815. u32 offset = 0;
  816. rrd_ring[0].count = rfd_ring[0].count;
  817. for (i = 1; i < AT_MAX_TRANSMIT_QUEUE; i++)
  818. tpd_ring[i].count = tpd_ring[0].count;
  819. for (i = 1; i < adapter->num_rx_queues; i++)
  820. rfd_ring[i].count = rrd_ring[i].count = rfd_ring[0].count;
  821. /* 2 tpd queue, one high priority queue,
  822. * another normal priority queue */
  823. size = sizeof(struct atl1c_buffer) * (tpd_ring->count * 2 +
  824. rfd_ring->count * num_rx_queues);
  825. tpd_ring->buffer_info = kzalloc(size, GFP_KERNEL);
  826. if (unlikely(!tpd_ring->buffer_info)) {
  827. dev_err(&pdev->dev, "kzalloc failed, size = %d\n",
  828. size);
  829. goto err_nomem;
  830. }
  831. for (i = 0; i < AT_MAX_TRANSMIT_QUEUE; i++) {
  832. tpd_ring[i].buffer_info =
  833. (struct atl1c_buffer *) (tpd_ring->buffer_info + count);
  834. count += tpd_ring[i].count;
  835. }
  836. for (i = 0; i < num_rx_queues; i++) {
  837. rfd_ring[i].buffer_info =
  838. (struct atl1c_buffer *) (tpd_ring->buffer_info + count);
  839. count += rfd_ring[i].count;
  840. rx_desc_count += rfd_ring[i].count;
  841. }
  842. /*
  843. * real ring DMA buffer
  844. * each ring/block may need up to 8 bytes for alignment, hence the
  845. * additional bytes tacked onto the end.
  846. */
  847. ring_header->size = size =
  848. sizeof(struct atl1c_tpd_desc) * tpd_ring->count * 2 +
  849. sizeof(struct atl1c_rx_free_desc) * rx_desc_count +
  850. sizeof(struct atl1c_recv_ret_status) * rx_desc_count +
  851. sizeof(struct atl1c_hw_stats) +
  852. 8 * 4 + 8 * 2 * num_rx_queues;
  853. ring_header->desc = pci_alloc_consistent(pdev, ring_header->size,
  854. &ring_header->dma);
  855. if (unlikely(!ring_header->desc)) {
  856. dev_err(&pdev->dev, "pci_alloc_consistend failed\n");
  857. goto err_nomem;
  858. }
  859. memset(ring_header->desc, 0, ring_header->size);
  860. /* init TPD ring */
  861. tpd_ring[0].dma = roundup(ring_header->dma, 8);
  862. offset = tpd_ring[0].dma - ring_header->dma;
  863. for (i = 0; i < AT_MAX_TRANSMIT_QUEUE; i++) {
  864. tpd_ring[i].dma = ring_header->dma + offset;
  865. tpd_ring[i].desc = (u8 *) ring_header->desc + offset;
  866. tpd_ring[i].size =
  867. sizeof(struct atl1c_tpd_desc) * tpd_ring[i].count;
  868. offset += roundup(tpd_ring[i].size, 8);
  869. }
  870. /* init RFD ring */
  871. for (i = 0; i < num_rx_queues; i++) {
  872. rfd_ring[i].dma = ring_header->dma + offset;
  873. rfd_ring[i].desc = (u8 *) ring_header->desc + offset;
  874. rfd_ring[i].size = sizeof(struct atl1c_rx_free_desc) *
  875. rfd_ring[i].count;
  876. offset += roundup(rfd_ring[i].size, 8);
  877. }
  878. /* init RRD ring */
  879. for (i = 0; i < num_rx_queues; i++) {
  880. rrd_ring[i].dma = ring_header->dma + offset;
  881. rrd_ring[i].desc = (u8 *) ring_header->desc + offset;
  882. rrd_ring[i].size = sizeof(struct atl1c_recv_ret_status) *
  883. rrd_ring[i].count;
  884. offset += roundup(rrd_ring[i].size, 8);
  885. }
  886. adapter->smb.dma = ring_header->dma + offset;
  887. adapter->smb.smb = (u8 *)ring_header->desc + offset;
  888. return 0;
  889. err_nomem:
  890. kfree(tpd_ring->buffer_info);
  891. return -ENOMEM;
  892. }
  893. static void atl1c_configure_des_ring(struct atl1c_adapter *adapter)
  894. {
  895. struct atl1c_hw *hw = &adapter->hw;
  896. struct atl1c_rfd_ring *rfd_ring = (struct atl1c_rfd_ring *)
  897. adapter->rfd_ring;
  898. struct atl1c_rrd_ring *rrd_ring = (struct atl1c_rrd_ring *)
  899. adapter->rrd_ring;
  900. struct atl1c_tpd_ring *tpd_ring = (struct atl1c_tpd_ring *)
  901. adapter->tpd_ring;
  902. struct atl1c_cmb *cmb = (struct atl1c_cmb *) &adapter->cmb;
  903. struct atl1c_smb *smb = (struct atl1c_smb *) &adapter->smb;
  904. int i;
  905. u32 data;
  906. /* TPD */
  907. AT_WRITE_REG(hw, REG_TX_BASE_ADDR_HI,
  908. (u32)((tpd_ring[atl1c_trans_normal].dma &
  909. AT_DMA_HI_ADDR_MASK) >> 32));
  910. /* just enable normal priority TX queue */
  911. AT_WRITE_REG(hw, REG_NTPD_HEAD_ADDR_LO,
  912. (u32)(tpd_ring[atl1c_trans_normal].dma &
  913. AT_DMA_LO_ADDR_MASK));
  914. AT_WRITE_REG(hw, REG_HTPD_HEAD_ADDR_LO,
  915. (u32)(tpd_ring[atl1c_trans_high].dma &
  916. AT_DMA_LO_ADDR_MASK));
  917. AT_WRITE_REG(hw, REG_TPD_RING_SIZE,
  918. (u32)(tpd_ring[0].count & TPD_RING_SIZE_MASK));
  919. /* RFD */
  920. AT_WRITE_REG(hw, REG_RX_BASE_ADDR_HI,
  921. (u32)((rfd_ring[0].dma & AT_DMA_HI_ADDR_MASK) >> 32));
  922. for (i = 0; i < adapter->num_rx_queues; i++)
  923. AT_WRITE_REG(hw, atl1c_rfd_addr_lo_regs[i],
  924. (u32)(rfd_ring[i].dma & AT_DMA_LO_ADDR_MASK));
  925. AT_WRITE_REG(hw, REG_RFD_RING_SIZE,
  926. rfd_ring[0].count & RFD_RING_SIZE_MASK);
  927. AT_WRITE_REG(hw, REG_RX_BUF_SIZE,
  928. adapter->rx_buffer_len & RX_BUF_SIZE_MASK);
  929. /* RRD */
  930. for (i = 0; i < adapter->num_rx_queues; i++)
  931. AT_WRITE_REG(hw, atl1c_rrd_addr_lo_regs[i],
  932. (u32)(rrd_ring[i].dma & AT_DMA_LO_ADDR_MASK));
  933. AT_WRITE_REG(hw, REG_RRD_RING_SIZE,
  934. (rrd_ring[0].count & RRD_RING_SIZE_MASK));
  935. /* CMB */
  936. AT_WRITE_REG(hw, REG_CMB_BASE_ADDR_LO, cmb->dma & AT_DMA_LO_ADDR_MASK);
  937. /* SMB */
  938. AT_WRITE_REG(hw, REG_SMB_BASE_ADDR_HI,
  939. (u32)((smb->dma & AT_DMA_HI_ADDR_MASK) >> 32));
  940. AT_WRITE_REG(hw, REG_SMB_BASE_ADDR_LO,
  941. (u32)(smb->dma & AT_DMA_LO_ADDR_MASK));
  942. if (hw->nic_type == athr_l2c_b) {
  943. AT_WRITE_REG(hw, REG_SRAM_RXF_LEN, 0x02a0L);
  944. AT_WRITE_REG(hw, REG_SRAM_TXF_LEN, 0x0100L);
  945. AT_WRITE_REG(hw, REG_SRAM_RXF_ADDR, 0x029f0000L);
  946. AT_WRITE_REG(hw, REG_SRAM_RFD0_INFO, 0x02bf02a0L);
  947. AT_WRITE_REG(hw, REG_SRAM_TXF_ADDR, 0x03bf02c0L);
  948. AT_WRITE_REG(hw, REG_SRAM_TRD_ADDR, 0x03df03c0L);
  949. AT_WRITE_REG(hw, REG_TXF_WATER_MARK, 0); /* TX watermark, to enter l1 state.*/
  950. AT_WRITE_REG(hw, REG_RXD_DMA_CTRL, 0); /* RXD threshold.*/
  951. }
  952. if (hw->nic_type == athr_l2c_b || hw->nic_type == athr_l1d_2) {
  953. /* Power Saving for L2c_B */
  954. AT_READ_REG(hw, REG_SERDES_LOCK, &data);
  955. data |= SERDES_MAC_CLK_SLOWDOWN;
  956. data |= SERDES_PYH_CLK_SLOWDOWN;
  957. AT_WRITE_REG(hw, REG_SERDES_LOCK, data);
  958. }
  959. /* Load all of base address above */
  960. AT_WRITE_REG(hw, REG_LOAD_PTR, 1);
  961. }
  962. static void atl1c_configure_tx(struct atl1c_adapter *adapter)
  963. {
  964. struct atl1c_hw *hw = &adapter->hw;
  965. u32 dev_ctrl_data;
  966. u32 max_pay_load;
  967. u16 tx_offload_thresh;
  968. u32 txq_ctrl_data;
  969. u32 max_pay_load_data;
  970. tx_offload_thresh = MAX_TX_OFFLOAD_THRESH;
  971. AT_WRITE_REG(hw, REG_TX_TSO_OFFLOAD_THRESH,
  972. (tx_offload_thresh >> 3) & TX_TSO_OFFLOAD_THRESH_MASK);
  973. AT_READ_REG(hw, REG_DEVICE_CTRL, &dev_ctrl_data);
  974. max_pay_load = (dev_ctrl_data >> DEVICE_CTRL_MAX_PAYLOAD_SHIFT) &
  975. DEVICE_CTRL_MAX_PAYLOAD_MASK;
  976. hw->dmaw_block = min_t(u32, max_pay_load, hw->dmaw_block);
  977. max_pay_load = (dev_ctrl_data >> DEVICE_CTRL_MAX_RREQ_SZ_SHIFT) &
  978. DEVICE_CTRL_MAX_RREQ_SZ_MASK;
  979. hw->dmar_block = min_t(u32, max_pay_load, hw->dmar_block);
  980. txq_ctrl_data = (hw->tpd_burst & TXQ_NUM_TPD_BURST_MASK) <<
  981. TXQ_NUM_TPD_BURST_SHIFT;
  982. if (hw->ctrl_flags & ATL1C_TXQ_MODE_ENHANCE)
  983. txq_ctrl_data |= TXQ_CTRL_ENH_MODE;
  984. max_pay_load_data = (atl1c_pay_load_size[hw->dmar_block] &
  985. TXQ_TXF_BURST_NUM_MASK) << TXQ_TXF_BURST_NUM_SHIFT;
  986. if (hw->nic_type == athr_l2c_b || hw->nic_type == athr_l2c_b2)
  987. max_pay_load_data >>= 1;
  988. txq_ctrl_data |= max_pay_load_data;
  989. AT_WRITE_REG(hw, REG_TXQ_CTRL, txq_ctrl_data);
  990. }
  991. static void atl1c_configure_rx(struct atl1c_adapter *adapter)
  992. {
  993. struct atl1c_hw *hw = &adapter->hw;
  994. u32 rxq_ctrl_data;
  995. rxq_ctrl_data = (hw->rfd_burst & RXQ_RFD_BURST_NUM_MASK) <<
  996. RXQ_RFD_BURST_NUM_SHIFT;
  997. if (hw->ctrl_flags & ATL1C_RX_IPV6_CHKSUM)
  998. rxq_ctrl_data |= IPV6_CHKSUM_CTRL_EN;
  999. if (hw->rss_type == atl1c_rss_ipv4)
  1000. rxq_ctrl_data |= RSS_HASH_IPV4;
  1001. if (hw->rss_type == atl1c_rss_ipv4_tcp)
  1002. rxq_ctrl_data |= RSS_HASH_IPV4_TCP;
  1003. if (hw->rss_type == atl1c_rss_ipv6)
  1004. rxq_ctrl_data |= RSS_HASH_IPV6;
  1005. if (hw->rss_type == atl1c_rss_ipv6_tcp)
  1006. rxq_ctrl_data |= RSS_HASH_IPV6_TCP;
  1007. if (hw->rss_type != atl1c_rss_disable)
  1008. rxq_ctrl_data |= RRS_HASH_CTRL_EN;
  1009. rxq_ctrl_data |= (hw->rss_mode & RSS_MODE_MASK) <<
  1010. RSS_MODE_SHIFT;
  1011. rxq_ctrl_data |= (hw->rss_hash_bits & RSS_HASH_BITS_MASK) <<
  1012. RSS_HASH_BITS_SHIFT;
  1013. if (hw->ctrl_flags & ATL1C_ASPM_CTRL_MON)
  1014. rxq_ctrl_data |= (ASPM_THRUPUT_LIMIT_1M &
  1015. ASPM_THRUPUT_LIMIT_MASK) << ASPM_THRUPUT_LIMIT_SHIFT;
  1016. AT_WRITE_REG(hw, REG_RXQ_CTRL, rxq_ctrl_data);
  1017. }
  1018. static void atl1c_configure_rss(struct atl1c_adapter *adapter)
  1019. {
  1020. struct atl1c_hw *hw = &adapter->hw;
  1021. AT_WRITE_REG(hw, REG_IDT_TABLE, hw->indirect_tab);
  1022. AT_WRITE_REG(hw, REG_BASE_CPU_NUMBER, hw->base_cpu);
  1023. }
  1024. static void atl1c_configure_dma(struct atl1c_adapter *adapter)
  1025. {
  1026. struct atl1c_hw *hw = &adapter->hw;
  1027. u32 dma_ctrl_data;
  1028. dma_ctrl_data = DMA_CTRL_DMAR_REQ_PRI;
  1029. if (hw->ctrl_flags & ATL1C_CMB_ENABLE)
  1030. dma_ctrl_data |= DMA_CTRL_CMB_EN;
  1031. if (hw->ctrl_flags & ATL1C_SMB_ENABLE)
  1032. dma_ctrl_data |= DMA_CTRL_SMB_EN;
  1033. else
  1034. dma_ctrl_data |= MAC_CTRL_SMB_DIS;
  1035. switch (hw->dma_order) {
  1036. case atl1c_dma_ord_in:
  1037. dma_ctrl_data |= DMA_CTRL_DMAR_IN_ORDER;
  1038. break;
  1039. case atl1c_dma_ord_enh:
  1040. dma_ctrl_data |= DMA_CTRL_DMAR_ENH_ORDER;
  1041. break;
  1042. case atl1c_dma_ord_out:
  1043. dma_ctrl_data |= DMA_CTRL_DMAR_OUT_ORDER;
  1044. break;
  1045. default:
  1046. break;
  1047. }
  1048. dma_ctrl_data |= (((u32)hw->dmar_block) & DMA_CTRL_DMAR_BURST_LEN_MASK)
  1049. << DMA_CTRL_DMAR_BURST_LEN_SHIFT;
  1050. dma_ctrl_data |= (((u32)hw->dmaw_block) & DMA_CTRL_DMAW_BURST_LEN_MASK)
  1051. << DMA_CTRL_DMAW_BURST_LEN_SHIFT;
  1052. dma_ctrl_data |= (((u32)hw->dmar_dly_cnt) & DMA_CTRL_DMAR_DLY_CNT_MASK)
  1053. << DMA_CTRL_DMAR_DLY_CNT_SHIFT;
  1054. dma_ctrl_data |= (((u32)hw->dmaw_dly_cnt) & DMA_CTRL_DMAW_DLY_CNT_MASK)
  1055. << DMA_CTRL_DMAW_DLY_CNT_SHIFT;
  1056. AT_WRITE_REG(hw, REG_DMA_CTRL, dma_ctrl_data);
  1057. }
  1058. /*
  1059. * Stop the mac, transmit and receive units
  1060. * hw - Struct containing variables accessed by shared code
  1061. * return : 0 or idle status (if error)
  1062. */
  1063. static int atl1c_stop_mac(struct atl1c_hw *hw)
  1064. {
  1065. u32 data;
  1066. AT_READ_REG(hw, REG_RXQ_CTRL, &data);
  1067. data &= ~(RXQ1_CTRL_EN | RXQ2_CTRL_EN |
  1068. RXQ3_CTRL_EN | RXQ_CTRL_EN);
  1069. AT_WRITE_REG(hw, REG_RXQ_CTRL, data);
  1070. AT_READ_REG(hw, REG_TXQ_CTRL, &data);
  1071. data &= ~TXQ_CTRL_EN;
  1072. AT_WRITE_REG(hw, REG_TWSI_CTRL, data);
  1073. atl1c_wait_until_idle(hw);
  1074. AT_READ_REG(hw, REG_MAC_CTRL, &data);
  1075. data &= ~(MAC_CTRL_TX_EN | MAC_CTRL_RX_EN);
  1076. AT_WRITE_REG(hw, REG_MAC_CTRL, data);
  1077. return (int)atl1c_wait_until_idle(hw);
  1078. }
  1079. static void atl1c_enable_rx_ctrl(struct atl1c_hw *hw)
  1080. {
  1081. u32 data;
  1082. AT_READ_REG(hw, REG_RXQ_CTRL, &data);
  1083. switch (hw->adapter->num_rx_queues) {
  1084. case 4:
  1085. data |= (RXQ3_CTRL_EN | RXQ2_CTRL_EN | RXQ1_CTRL_EN);
  1086. break;
  1087. case 3:
  1088. data |= (RXQ2_CTRL_EN | RXQ1_CTRL_EN);
  1089. break;
  1090. case 2:
  1091. data |= RXQ1_CTRL_EN;
  1092. break;
  1093. default:
  1094. break;
  1095. }
  1096. data |= RXQ_CTRL_EN;
  1097. AT_WRITE_REG(hw, REG_RXQ_CTRL, data);
  1098. }
  1099. static void atl1c_enable_tx_ctrl(struct atl1c_hw *hw)
  1100. {
  1101. u32 data;
  1102. AT_READ_REG(hw, REG_TXQ_CTRL, &data);
  1103. data |= TXQ_CTRL_EN;
  1104. AT_WRITE_REG(hw, REG_TXQ_CTRL, data);
  1105. }
  1106. /*
  1107. * Reset the transmit and receive units; mask and clear all interrupts.
  1108. * hw - Struct containing variables accessed by shared code
  1109. * return : 0 or idle status (if error)
  1110. */
  1111. static int atl1c_reset_mac(struct atl1c_hw *hw)
  1112. {
  1113. struct atl1c_adapter *adapter = (struct atl1c_adapter *)hw->adapter;
  1114. struct pci_dev *pdev = adapter->pdev;
  1115. u32 master_ctrl_data = 0;
  1116. AT_WRITE_REG(hw, REG_IMR, 0);
  1117. AT_WRITE_REG(hw, REG_ISR, ISR_DIS_INT);
  1118. atl1c_stop_mac(hw);
  1119. /*
  1120. * Issue Soft Reset to the MAC. This will reset the chip's
  1121. * transmit, receive, DMA. It will not effect
  1122. * the current PCI configuration. The global reset bit is self-
  1123. * clearing, and should clear within a microsecond.
  1124. */
  1125. AT_READ_REG(hw, REG_MASTER_CTRL, &master_ctrl_data);
  1126. master_ctrl_data |= MASTER_CTRL_OOB_DIS_OFF;
  1127. AT_WRITE_REGW(hw, REG_MASTER_CTRL, ((master_ctrl_data | MASTER_CTRL_SOFT_RST)
  1128. & 0xFFFF));
  1129. AT_WRITE_FLUSH(hw);
  1130. msleep(10);
  1131. /* Wait at least 10ms for All module to be Idle */
  1132. if (atl1c_wait_until_idle(hw)) {
  1133. dev_err(&pdev->dev,
  1134. "MAC state machine can't be idle since"
  1135. " disabled for 10ms second\n");
  1136. return -1;
  1137. }
  1138. return 0;
  1139. }
  1140. static void atl1c_disable_l0s_l1(struct atl1c_hw *hw)
  1141. {
  1142. u32 pm_ctrl_data;
  1143. AT_READ_REG(hw, REG_PM_CTRL, &pm_ctrl_data);
  1144. pm_ctrl_data &= ~(PM_CTRL_L1_ENTRY_TIMER_MASK <<
  1145. PM_CTRL_L1_ENTRY_TIMER_SHIFT);
  1146. pm_ctrl_data &= ~PM_CTRL_CLK_SWH_L1;
  1147. pm_ctrl_data &= ~PM_CTRL_ASPM_L0S_EN;
  1148. pm_ctrl_data &= ~PM_CTRL_ASPM_L1_EN;
  1149. pm_ctrl_data &= ~PM_CTRL_MAC_ASPM_CHK;
  1150. pm_ctrl_data &= ~PM_CTRL_SERDES_PD_EX_L1;
  1151. pm_ctrl_data |= PM_CTRL_SERDES_BUDS_RX_L1_EN;
  1152. pm_ctrl_data |= PM_CTRL_SERDES_PLL_L1_EN;
  1153. pm_ctrl_data |= PM_CTRL_SERDES_L1_EN;
  1154. AT_WRITE_REG(hw, REG_PM_CTRL, pm_ctrl_data);
  1155. }
  1156. /*
  1157. * Set ASPM state.
  1158. * Enable/disable L0s/L1 depend on link state.
  1159. */
  1160. static void atl1c_set_aspm(struct atl1c_hw *hw, bool linkup)
  1161. {
  1162. u32 pm_ctrl_data;
  1163. u32 link_ctrl_data;
  1164. u32 link_l1_timer = 0xF;
  1165. AT_READ_REG(hw, REG_PM_CTRL, &pm_ctrl_data);
  1166. AT_READ_REG(hw, REG_LINK_CTRL, &link_ctrl_data);
  1167. pm_ctrl_data &= ~PM_CTRL_SERDES_PD_EX_L1;
  1168. pm_ctrl_data &= ~(PM_CTRL_L1_ENTRY_TIMER_MASK <<
  1169. PM_CTRL_L1_ENTRY_TIMER_SHIFT);
  1170. pm_ctrl_data &= ~(PM_CTRL_LCKDET_TIMER_MASK <<
  1171. PM_CTRL_LCKDET_TIMER_SHIFT);
  1172. pm_ctrl_data |= AT_LCKDET_TIMER << PM_CTRL_LCKDET_TIMER_SHIFT;
  1173. if (hw->nic_type == athr_l2c_b || hw->nic_type == athr_l1d ||
  1174. hw->nic_type == athr_l2c_b2 || hw->nic_type == athr_l1d_2) {
  1175. link_ctrl_data &= ~LINK_CTRL_EXT_SYNC;
  1176. if (!(hw->ctrl_flags & ATL1C_APS_MODE_ENABLE)) {
  1177. if (hw->nic_type == athr_l2c_b && hw->revision_id == L2CB_V10)
  1178. link_ctrl_data |= LINK_CTRL_EXT_SYNC;
  1179. }
  1180. AT_WRITE_REG(hw, REG_LINK_CTRL, link_ctrl_data);
  1181. pm_ctrl_data |= PM_CTRL_RCVR_WT_TIMER;
  1182. pm_ctrl_data &= ~(PM_CTRL_PM_REQ_TIMER_MASK <<
  1183. PM_CTRL_PM_REQ_TIMER_SHIFT);
  1184. pm_ctrl_data |= AT_ASPM_L1_TIMER <<
  1185. PM_CTRL_PM_REQ_TIMER_SHIFT;
  1186. pm_ctrl_data &= ~PM_CTRL_SA_DLY_EN;
  1187. pm_ctrl_data &= ~PM_CTRL_HOTRST;
  1188. pm_ctrl_data |= 1 << PM_CTRL_L1_ENTRY_TIMER_SHIFT;
  1189. pm_ctrl_data |= PM_CTRL_SERDES_PD_EX_L1;
  1190. }
  1191. pm_ctrl_data |= PM_CTRL_MAC_ASPM_CHK;
  1192. if (linkup) {
  1193. pm_ctrl_data &= ~PM_CTRL_ASPM_L1_EN;
  1194. pm_ctrl_data &= ~PM_CTRL_ASPM_L0S_EN;
  1195. if (hw->ctrl_flags & ATL1C_ASPM_L1_SUPPORT)
  1196. pm_ctrl_data |= PM_CTRL_ASPM_L1_EN;
  1197. if (hw->ctrl_flags & ATL1C_ASPM_L0S_SUPPORT)
  1198. pm_ctrl_data |= PM_CTRL_ASPM_L0S_EN;
  1199. if (hw->nic_type == athr_l2c_b || hw->nic_type == athr_l1d ||
  1200. hw->nic_type == athr_l2c_b2 || hw->nic_type == athr_l1d_2) {
  1201. if (hw->nic_type == athr_l2c_b)
  1202. if (!(hw->ctrl_flags & ATL1C_APS_MODE_ENABLE))
  1203. pm_ctrl_data &= ~PM_CTRL_ASPM_L0S_EN;
  1204. pm_ctrl_data &= ~PM_CTRL_SERDES_L1_EN;
  1205. pm_ctrl_data &= ~PM_CTRL_SERDES_PLL_L1_EN;
  1206. pm_ctrl_data &= ~PM_CTRL_SERDES_BUDS_RX_L1_EN;
  1207. pm_ctrl_data |= PM_CTRL_CLK_SWH_L1;
  1208. if (hw->adapter->link_speed == SPEED_100 ||
  1209. hw->adapter->link_speed == SPEED_1000) {
  1210. pm_ctrl_data &= ~(PM_CTRL_L1_ENTRY_TIMER_MASK <<
  1211. PM_CTRL_L1_ENTRY_TIMER_SHIFT);
  1212. if (hw->nic_type == athr_l2c_b)
  1213. link_l1_timer = 7;
  1214. else if (hw->nic_type == athr_l2c_b2 ||
  1215. hw->nic_type == athr_l1d_2)
  1216. link_l1_timer = 4;
  1217. pm_ctrl_data |= link_l1_timer <<
  1218. PM_CTRL_L1_ENTRY_TIMER_SHIFT;
  1219. }
  1220. } else {
  1221. pm_ctrl_data |= PM_CTRL_SERDES_L1_EN;
  1222. pm_ctrl_data |= PM_CTRL_SERDES_PLL_L1_EN;
  1223. pm_ctrl_data |= PM_CTRL_SERDES_BUDS_RX_L1_EN;
  1224. pm_ctrl_data &= ~PM_CTRL_CLK_SWH_L1;
  1225. pm_ctrl_data &= ~PM_CTRL_ASPM_L0S_EN;
  1226. pm_ctrl_data &= ~PM_CTRL_ASPM_L1_EN;
  1227. }
  1228. } else {
  1229. pm_ctrl_data &= ~PM_CTRL_SERDES_L1_EN;
  1230. pm_ctrl_data &= ~PM_CTRL_ASPM_L0S_EN;
  1231. pm_ctrl_data &= ~PM_CTRL_SERDES_PLL_L1_EN;
  1232. pm_ctrl_data |= PM_CTRL_CLK_SWH_L1;
  1233. if (hw->ctrl_flags & ATL1C_ASPM_L1_SUPPORT)
  1234. pm_ctrl_data |= PM_CTRL_ASPM_L1_EN;
  1235. else
  1236. pm_ctrl_data &= ~PM_CTRL_ASPM_L1_EN;
  1237. }
  1238. AT_WRITE_REG(hw, REG_PM_CTRL, pm_ctrl_data);
  1239. return;
  1240. }
  1241. static void atl1c_setup_mac_ctrl(struct atl1c_adapter *adapter)
  1242. {
  1243. struct atl1c_hw *hw = &adapter->hw;
  1244. struct net_device *netdev = adapter->netdev;
  1245. u32 mac_ctrl_data;
  1246. mac_ctrl_data = MAC_CTRL_TX_EN | MAC_CTRL_RX_EN;
  1247. mac_ctrl_data |= (MAC_CTRL_TX_FLOW | MAC_CTRL_RX_FLOW);
  1248. if (adapter->link_duplex == FULL_DUPLEX) {
  1249. hw->mac_duplex = true;
  1250. mac_ctrl_data |= MAC_CTRL_DUPLX;
  1251. }
  1252. if (adapter->link_speed == SPEED_1000)
  1253. hw->mac_speed = atl1c_mac_speed_1000;
  1254. else
  1255. hw->mac_speed = atl1c_mac_speed_10_100;
  1256. mac_ctrl_data |= (hw->mac_speed & MAC_CTRL_SPEED_MASK) <<
  1257. MAC_CTRL_SPEED_SHIFT;
  1258. mac_ctrl_data |= (MAC_CTRL_ADD_CRC | MAC_CTRL_PAD);
  1259. mac_ctrl_data |= ((hw->preamble_len & MAC_CTRL_PRMLEN_MASK) <<
  1260. MAC_CTRL_PRMLEN_SHIFT);
  1261. if (adapter->vlgrp)
  1262. mac_ctrl_data |= MAC_CTRL_RMV_VLAN;
  1263. mac_ctrl_data |= MAC_CTRL_BC_EN;
  1264. if (netdev->flags & IFF_PROMISC)
  1265. mac_ctrl_data |= MAC_CTRL_PROMIS_EN;
  1266. if (netdev->flags & IFF_ALLMULTI)
  1267. mac_ctrl_data |= MAC_CTRL_MC_ALL_EN;
  1268. mac_ctrl_data |= MAC_CTRL_SINGLE_PAUSE_EN;
  1269. if (hw->nic_type == athr_l1d || hw->nic_type == athr_l2c_b2 ||
  1270. hw->nic_type == athr_l1d_2) {
  1271. mac_ctrl_data |= MAC_CTRL_SPEED_MODE_SW;
  1272. mac_ctrl_data |= MAC_CTRL_HASH_ALG_CRC32;
  1273. }
  1274. AT_WRITE_REG(hw, REG_MAC_CTRL, mac_ctrl_data);
  1275. }
  1276. /*
  1277. * atl1c_configure - Configure Transmit&Receive Unit after Reset
  1278. * @adapter: board private structure
  1279. *
  1280. * Configure the Tx /Rx unit of the MAC after a reset.
  1281. */
  1282. static int atl1c_configure(struct atl1c_adapter *adapter)
  1283. {
  1284. struct atl1c_hw *hw = &adapter->hw;
  1285. u32 master_ctrl_data = 0;
  1286. u32 intr_modrt_data;
  1287. u32 data;
  1288. /* clear interrupt status */
  1289. AT_WRITE_REG(hw, REG_ISR, 0xFFFFFFFF);
  1290. /* Clear any WOL status */
  1291. AT_WRITE_REG(hw, REG_WOL_CTRL, 0);
  1292. /* set Interrupt Clear Timer
  1293. * HW will enable self to assert interrupt event to system after
  1294. * waiting x-time for software to notify it accept interrupt.
  1295. */
  1296. data = CLK_GATING_EN_ALL;
  1297. if (hw->ctrl_flags & ATL1C_CLK_GATING_EN) {
  1298. if (hw->nic_type == athr_l2c_b)
  1299. data &= ~CLK_GATING_RXMAC_EN;
  1300. } else
  1301. data = 0;
  1302. AT_WRITE_REG(hw, REG_CLK_GATING_CTRL, data);
  1303. AT_WRITE_REG(hw, REG_INT_RETRIG_TIMER,
  1304. hw->ict & INT_RETRIG_TIMER_MASK);
  1305. atl1c_configure_des_ring(adapter);
  1306. if (hw->ctrl_flags & ATL1C_INTR_MODRT_ENABLE) {
  1307. intr_modrt_data = (hw->tx_imt & IRQ_MODRT_TIMER_MASK) <<
  1308. IRQ_MODRT_TX_TIMER_SHIFT;
  1309. intr_modrt_data |= (hw->rx_imt & IRQ_MODRT_TIMER_MASK) <<
  1310. IRQ_MODRT_RX_TIMER_SHIFT;
  1311. AT_WRITE_REG(hw, REG_IRQ_MODRT_TIMER_INIT, intr_modrt_data);
  1312. master_ctrl_data |=
  1313. MASTER_CTRL_TX_ITIMER_EN | MASTER_CTRL_RX_ITIMER_EN;
  1314. }
  1315. if (hw->ctrl_flags & ATL1C_INTR_CLEAR_ON_READ)
  1316. master_ctrl_data |= MASTER_CTRL_INT_RDCLR;
  1317. master_ctrl_data |= MASTER_CTRL_SA_TIMER_EN;
  1318. AT_WRITE_REG(hw, REG_MASTER_CTRL, master_ctrl_data);
  1319. if (hw->ctrl_flags & ATL1C_CMB_ENABLE) {
  1320. AT_WRITE_REG(hw, REG_CMB_TPD_THRESH,
  1321. hw->cmb_tpd & CMB_TPD_THRESH_MASK);
  1322. AT_WRITE_REG(hw, REG_CMB_TX_TIMER,
  1323. hw->cmb_tx_timer & CMB_TX_TIMER_MASK);
  1324. }
  1325. if (hw->ctrl_flags & ATL1C_SMB_ENABLE)
  1326. AT_WRITE_REG(hw, REG_SMB_STAT_TIMER,
  1327. hw->smb_timer & SMB_STAT_TIMER_MASK);
  1328. /* set MTU */
  1329. AT_WRITE_REG(hw, REG_MTU, hw->max_frame_size + ETH_HLEN +
  1330. VLAN_HLEN + ETH_FCS_LEN);
  1331. /* HDS, disable */
  1332. AT_WRITE_REG(hw, REG_HDS_CTRL, 0);
  1333. atl1c_configure_tx(adapter);
  1334. atl1c_configure_rx(adapter);
  1335. atl1c_configure_rss(adapter);
  1336. atl1c_configure_dma(adapter);
  1337. return 0;
  1338. }
  1339. static void atl1c_update_hw_stats(struct atl1c_adapter *adapter)
  1340. {
  1341. u16 hw_reg_addr = 0;
  1342. unsigned long *stats_item = NULL;
  1343. u32 data;
  1344. /* update rx status */
  1345. hw_reg_addr = REG_MAC_RX_STATUS_BIN;
  1346. stats_item = &adapter->hw_stats.rx_ok;
  1347. while (hw_reg_addr <= REG_MAC_RX_STATUS_END) {
  1348. AT_READ_REG(&adapter->hw, hw_reg_addr, &data);
  1349. *stats_item += data;
  1350. stats_item++;
  1351. hw_reg_addr += 4;
  1352. }
  1353. /* update tx status */
  1354. hw_reg_addr = REG_MAC_TX_STATUS_BIN;
  1355. stats_item = &adapter->hw_stats.tx_ok;
  1356. while (hw_reg_addr <= REG_MAC_TX_STATUS_END) {
  1357. AT_READ_REG(&adapter->hw, hw_reg_addr, &data);
  1358. *stats_item += data;
  1359. stats_item++;
  1360. hw_reg_addr += 4;
  1361. }
  1362. }
  1363. /*
  1364. * atl1c_get_stats - Get System Network Statistics
  1365. * @netdev: network interface device structure
  1366. *
  1367. * Returns the address of the device statistics structure.
  1368. * The statistics are actually updated from the timer callback.
  1369. */
  1370. static struct net_device_stats *atl1c_get_stats(struct net_device *netdev)
  1371. {
  1372. struct atl1c_adapter *adapter = netdev_priv(netdev);
  1373. struct atl1c_hw_stats *hw_stats = &adapter->hw_stats;
  1374. struct net_device_stats *net_stats = &netdev->stats;
  1375. atl1c_update_hw_stats(adapter);
  1376. net_stats->rx_packets = hw_stats->rx_ok;
  1377. net_stats->tx_packets = hw_stats->tx_ok;
  1378. net_stats->rx_bytes = hw_stats->rx_byte_cnt;
  1379. net_stats->tx_bytes = hw_stats->tx_byte_cnt;
  1380. net_stats->multicast = hw_stats->rx_mcast;
  1381. net_stats->collisions = hw_stats->tx_1_col +
  1382. hw_stats->tx_2_col * 2 +
  1383. hw_stats->tx_late_col + hw_stats->tx_abort_col;
  1384. net_stats->rx_errors = hw_stats->rx_frag + hw_stats->rx_fcs_err +
  1385. hw_stats->rx_len_err + hw_stats->rx_sz_ov +
  1386. hw_stats->rx_rrd_ov + hw_stats->rx_align_err;
  1387. net_stats->rx_fifo_errors = hw_stats->rx_rxf_ov;
  1388. net_stats->rx_length_errors = hw_stats->rx_len_err;
  1389. net_stats->rx_crc_errors = hw_stats->rx_fcs_err;
  1390. net_stats->rx_frame_errors = hw_stats->rx_align_err;
  1391. net_stats->rx_over_errors = hw_stats->rx_rrd_ov + hw_stats->rx_rxf_ov;
  1392. net_stats->rx_missed_errors = hw_stats->rx_rrd_ov + hw_stats->rx_rxf_ov;
  1393. net_stats->tx_errors = hw_stats->tx_late_col + hw_stats->tx_abort_col +
  1394. hw_stats->tx_underrun + hw_stats->tx_trunc;
  1395. net_stats->tx_fifo_errors = hw_stats->tx_underrun;
  1396. net_stats->tx_aborted_errors = hw_stats->tx_abort_col;
  1397. net_stats->tx_window_errors = hw_stats->tx_late_col;
  1398. return net_stats;
  1399. }
  1400. static inline void atl1c_clear_phy_int(struct atl1c_adapter *adapter)
  1401. {
  1402. u16 phy_data;
  1403. spin_lock(&adapter->mdio_lock);
  1404. atl1c_read_phy_reg(&adapter->hw, MII_ISR, &phy_data);
  1405. spin_unlock(&adapter->mdio_lock);
  1406. }
  1407. static bool atl1c_clean_tx_irq(struct atl1c_adapter *adapter,
  1408. enum atl1c_trans_queue type)
  1409. {
  1410. struct atl1c_tpd_ring *tpd_ring = (struct atl1c_tpd_ring *)
  1411. &adapter->tpd_ring[type];
  1412. struct atl1c_buffer *buffer_info;
  1413. struct pci_dev *pdev = adapter->pdev;
  1414. u16 next_to_clean = atomic_read(&tpd_ring->next_to_clean);
  1415. u16 hw_next_to_clean;
  1416. u16 shift;
  1417. u32 data;
  1418. if (type == atl1c_trans_high)
  1419. shift = MB_HTPD_CONS_IDX_SHIFT;
  1420. else
  1421. shift = MB_NTPD_CONS_IDX_SHIFT;
  1422. AT_READ_REG(&adapter->hw, REG_MB_PRIO_CONS_IDX, &data);
  1423. hw_next_to_clean = (data >> shift) & MB_PRIO_PROD_IDX_MASK;
  1424. while (next_to_clean != hw_next_to_clean) {
  1425. buffer_info = &tpd_ring->buffer_info[next_to_clean];
  1426. atl1c_clean_buffer(pdev, buffer_info, 1);
  1427. if (++next_to_clean == tpd_ring->count)
  1428. next_to_clean = 0;
  1429. atomic_set(&tpd_ring->next_to_clean, next_to_clean);
  1430. }
  1431. if (netif_queue_stopped(adapter->netdev) &&
  1432. netif_carrier_ok(adapter->netdev)) {
  1433. netif_wake_queue(adapter->netdev);
  1434. }
  1435. return true;
  1436. }
  1437. /*
  1438. * atl1c_intr - Interrupt Handler
  1439. * @irq: interrupt number
  1440. * @data: pointer to a network interface device structure
  1441. * @pt_regs: CPU registers structure
  1442. */
  1443. static irqreturn_t atl1c_intr(int irq, void *data)
  1444. {
  1445. struct net_device *netdev = data;
  1446. struct atl1c_adapter *adapter = netdev_priv(netdev);
  1447. struct pci_dev *pdev = adapter->pdev;
  1448. struct atl1c_hw *hw = &adapter->hw;
  1449. int max_ints = AT_MAX_INT_WORK;
  1450. int handled = IRQ_NONE;
  1451. u32 status;
  1452. u32 reg_data;
  1453. do {
  1454. AT_READ_REG(hw, REG_ISR, &reg_data);
  1455. status = reg_data & hw->intr_mask;
  1456. if (status == 0 || (status & ISR_DIS_INT) != 0) {
  1457. if (max_ints != AT_MAX_INT_WORK)
  1458. handled = IRQ_HANDLED;
  1459. break;
  1460. }
  1461. /* link event */
  1462. if (status & ISR_GPHY)
  1463. atl1c_clear_phy_int(adapter);
  1464. /* Ack ISR */
  1465. AT_WRITE_REG(hw, REG_ISR, status | ISR_DIS_INT);
  1466. if (status & ISR_RX_PKT) {
  1467. if (likely(napi_schedule_prep(&adapter->napi))) {
  1468. hw->intr_mask &= ~ISR_RX_PKT;
  1469. AT_WRITE_REG(hw, REG_IMR, hw->intr_mask);
  1470. __napi_schedule(&adapter->napi);
  1471. }
  1472. }
  1473. if (status & ISR_TX_PKT)
  1474. atl1c_clean_tx_irq(adapter, atl1c_trans_normal);
  1475. handled = IRQ_HANDLED;
  1476. /* check if PCIE PHY Link down */
  1477. if (status & ISR_ERROR) {
  1478. if (netif_msg_hw(adapter))
  1479. dev_err(&pdev->dev,
  1480. "atl1c hardware error (status = 0x%x)\n",
  1481. status & ISR_ERROR);
  1482. /* reset MAC */
  1483. adapter->work_event |= ATL1C_WORK_EVENT_RESET;
  1484. schedule_work(&adapter->common_task);
  1485. return IRQ_HANDLED;
  1486. }
  1487. if (status & ISR_OVER)
  1488. if (netif_msg_intr(adapter))
  1489. dev_warn(&pdev->dev,
  1490. "TX/RX overflow (status = 0x%x)\n",
  1491. status & ISR_OVER);
  1492. /* link event */
  1493. if (status & (ISR_GPHY | ISR_MANUAL)) {
  1494. netdev->stats.tx_carrier_errors++;
  1495. atl1c_link_chg_event(adapter);
  1496. break;
  1497. }
  1498. } while (--max_ints > 0);
  1499. /* re-enable Interrupt*/
  1500. AT_WRITE_REG(&adapter->hw, REG_ISR, 0);
  1501. return handled;
  1502. }
  1503. static inline void atl1c_rx_checksum(struct atl1c_adapter *adapter,
  1504. struct sk_buff *skb, struct atl1c_recv_ret_status *prrs)
  1505. {
  1506. /*
  1507. * The pid field in RRS in not correct sometimes, so we
  1508. * cannot figure out if the packet is fragmented or not,
  1509. * so we tell the KERNEL CHECKSUM_NONE
  1510. */
  1511. skb_checksum_none_assert(skb);
  1512. }
  1513. static int atl1c_alloc_rx_buffer(struct atl1c_adapter *adapter, const int ringid)
  1514. {
  1515. struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring[ringid];
  1516. struct pci_dev *pdev = adapter->pdev;
  1517. struct atl1c_buffer *buffer_info, *next_info;
  1518. struct sk_buff *skb;
  1519. void *vir_addr = NULL;
  1520. u16 num_alloc = 0;
  1521. u16 rfd_next_to_use, next_next;
  1522. struct atl1c_rx_free_desc *rfd_desc;
  1523. next_next = rfd_next_to_use = rfd_ring->next_to_use;
  1524. if (++next_next == rfd_ring->count)
  1525. next_next = 0;
  1526. buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
  1527. next_info = &rfd_ring->buffer_info[next_next];
  1528. while (next_info->flags & ATL1C_BUFFER_FREE) {
  1529. rfd_desc = ATL1C_RFD_DESC(rfd_ring, rfd_next_to_use);
  1530. skb = dev_alloc_skb(adapter->rx_buffer_len);
  1531. if (unlikely(!skb)) {
  1532. if (netif_msg_rx_err(adapter))
  1533. dev_warn(&pdev->dev, "alloc rx buffer failed\n");
  1534. break;
  1535. }
  1536. /*
  1537. * Make buffer alignment 2 beyond a 16 byte boundary
  1538. * this will result in a 16 byte aligned IP header after
  1539. * the 14 byte MAC header is removed
  1540. */
  1541. vir_addr = skb->data;
  1542. ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
  1543. buffer_info->skb = skb;
  1544. buffer_info->length = adapter->rx_buffer_len;
  1545. buffer_info->dma = pci_map_single(pdev, vir_addr,
  1546. buffer_info->length,
  1547. PCI_DMA_FROMDEVICE);
  1548. ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_SINGLE,
  1549. ATL1C_PCIMAP_FROMDEVICE);
  1550. rfd_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
  1551. rfd_next_to_use = next_next;
  1552. if (++next_next == rfd_ring->count)
  1553. next_next = 0;
  1554. buffer_info = &rfd_ring->buffer_info[rfd_next_to_use];
  1555. next_info = &rfd_ring->buffer_info[next_next];
  1556. num_alloc++;
  1557. }
  1558. if (num_alloc) {
  1559. /* TODO: update mailbox here */
  1560. wmb();
  1561. rfd_ring->next_to_use = rfd_next_to_use;
  1562. AT_WRITE_REG(&adapter->hw, atl1c_rfd_prod_idx_regs[ringid],
  1563. rfd_ring->next_to_use & MB_RFDX_PROD_IDX_MASK);
  1564. }
  1565. return num_alloc;
  1566. }
  1567. static void atl1c_clean_rrd(struct atl1c_rrd_ring *rrd_ring,
  1568. struct atl1c_recv_ret_status *rrs, u16 num)
  1569. {
  1570. u16 i;
  1571. /* the relationship between rrd and rfd is one map one */
  1572. for (i = 0; i < num; i++, rrs = ATL1C_RRD_DESC(rrd_ring,
  1573. rrd_ring->next_to_clean)) {
  1574. rrs->word3 &= ~RRS_RXD_UPDATED;
  1575. if (++rrd_ring->next_to_clean == rrd_ring->count)
  1576. rrd_ring->next_to_clean = 0;
  1577. }
  1578. }
  1579. static void atl1c_clean_rfd(struct atl1c_rfd_ring *rfd_ring,
  1580. struct atl1c_recv_ret_status *rrs, u16 num)
  1581. {
  1582. u16 i;
  1583. u16 rfd_index;
  1584. struct atl1c_buffer *buffer_info = rfd_ring->buffer_info;
  1585. rfd_index = (rrs->word0 >> RRS_RX_RFD_INDEX_SHIFT) &
  1586. RRS_RX_RFD_INDEX_MASK;
  1587. for (i = 0; i < num; i++) {
  1588. buffer_info[rfd_index].skb = NULL;
  1589. ATL1C_SET_BUFFER_STATE(&buffer_info[rfd_index],
  1590. ATL1C_BUFFER_FREE);
  1591. if (++rfd_index == rfd_ring->count)
  1592. rfd_index = 0;
  1593. }
  1594. rfd_ring->next_to_clean = rfd_index;
  1595. }
  1596. static void atl1c_clean_rx_irq(struct atl1c_adapter *adapter, u8 que,
  1597. int *work_done, int work_to_do)
  1598. {
  1599. u16 rfd_num, rfd_index;
  1600. u16 count = 0;
  1601. u16 length;
  1602. struct pci_dev *pdev = adapter->pdev;
  1603. struct net_device *netdev = adapter->netdev;
  1604. struct atl1c_rfd_ring *rfd_ring = &adapter->rfd_ring[que];
  1605. struct atl1c_rrd_ring *rrd_ring = &adapter->rrd_ring[que];
  1606. struct sk_buff *skb;
  1607. struct atl1c_recv_ret_status *rrs;
  1608. struct atl1c_buffer *buffer_info;
  1609. while (1) {
  1610. if (*work_done >= work_to_do)
  1611. break;
  1612. rrs = ATL1C_RRD_DESC(rrd_ring, rrd_ring->next_to_clean);
  1613. if (likely(RRS_RXD_IS_VALID(rrs->word3))) {
  1614. rfd_num = (rrs->word0 >> RRS_RX_RFD_CNT_SHIFT) &
  1615. RRS_RX_RFD_CNT_MASK;
  1616. if (unlikely(rfd_num != 1))
  1617. /* TODO support mul rfd*/
  1618. if (netif_msg_rx_err(adapter))
  1619. dev_warn(&pdev->dev,
  1620. "Multi rfd not support yet!\n");
  1621. goto rrs_checked;
  1622. } else {
  1623. break;
  1624. }
  1625. rrs_checked:
  1626. atl1c_clean_rrd(rrd_ring, rrs, rfd_num);
  1627. if (rrs->word3 & (RRS_RX_ERR_SUM | RRS_802_3_LEN_ERR)) {
  1628. atl1c_clean_rfd(rfd_ring, rrs, rfd_num);
  1629. if (netif_msg_rx_err(adapter))
  1630. dev_warn(&pdev->dev,
  1631. "wrong packet! rrs word3 is %x\n",
  1632. rrs->word3);
  1633. continue;
  1634. }
  1635. length = le16_to_cpu((rrs->word3 >> RRS_PKT_SIZE_SHIFT) &
  1636. RRS_PKT_SIZE_MASK);
  1637. /* Good Receive */
  1638. if (likely(rfd_num == 1)) {
  1639. rfd_index = (rrs->word0 >> RRS_RX_RFD_INDEX_SHIFT) &
  1640. RRS_RX_RFD_INDEX_MASK;
  1641. buffer_info = &rfd_ring->buffer_info[rfd_index];
  1642. pci_unmap_single(pdev, buffer_info->dma,
  1643. buffer_info->length, PCI_DMA_FROMDEVICE);
  1644. skb = buffer_info->skb;
  1645. } else {
  1646. /* TODO */
  1647. if (netif_msg_rx_err(adapter))
  1648. dev_warn(&pdev->dev,
  1649. "Multi rfd not support yet!\n");
  1650. break;
  1651. }
  1652. atl1c_clean_rfd(rfd_ring, rrs, rfd_num);
  1653. skb_put(skb, length - ETH_FCS_LEN);
  1654. skb->protocol = eth_type_trans(skb, netdev);
  1655. atl1c_rx_checksum(adapter, skb, rrs);
  1656. if (unlikely(adapter->vlgrp) && rrs->word3 & RRS_VLAN_INS) {
  1657. u16 vlan;
  1658. AT_TAG_TO_VLAN(rrs->vlan_tag, vlan);
  1659. vlan = le16_to_cpu(vlan);
  1660. vlan_hwaccel_receive_skb(skb, adapter->vlgrp, vlan);
  1661. } else
  1662. netif_receive_skb(skb);
  1663. (*work_done)++;
  1664. count++;
  1665. }
  1666. if (count)
  1667. atl1c_alloc_rx_buffer(adapter, que);
  1668. }
  1669. /*
  1670. * atl1c_clean - NAPI Rx polling callback
  1671. * @adapter: board private structure
  1672. */
  1673. static int atl1c_clean(struct napi_struct *napi, int budget)
  1674. {
  1675. struct atl1c_adapter *adapter =
  1676. container_of(napi, struct atl1c_adapter, napi);
  1677. int work_done = 0;
  1678. /* Keep link state information with original netdev */
  1679. if (!netif_carrier_ok(adapter->netdev))
  1680. goto quit_polling;
  1681. /* just enable one RXQ */
  1682. atl1c_clean_rx_irq(adapter, 0, &work_done, budget);
  1683. if (work_done < budget) {
  1684. quit_polling:
  1685. napi_complete(napi);
  1686. adapter->hw.intr_mask |= ISR_RX_PKT;
  1687. AT_WRITE_REG(&adapter->hw, REG_IMR, adapter->hw.intr_mask);
  1688. }
  1689. return work_done;
  1690. }
  1691. #ifdef CONFIG_NET_POLL_CONTROLLER
  1692. /*
  1693. * Polling 'interrupt' - used by things like netconsole to send skbs
  1694. * without having to re-enable interrupts. It's not called while
  1695. * the interrupt routine is executing.
  1696. */
  1697. static void atl1c_netpoll(struct net_device *netdev)
  1698. {
  1699. struct atl1c_adapter *adapter = netdev_priv(netdev);
  1700. disable_irq(adapter->pdev->irq);
  1701. atl1c_intr(adapter->pdev->irq, netdev);
  1702. enable_irq(adapter->pdev->irq);
  1703. }
  1704. #endif
  1705. static inline u16 atl1c_tpd_avail(struct atl1c_adapter *adapter, enum atl1c_trans_queue type)
  1706. {
  1707. struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
  1708. u16 next_to_use = 0;
  1709. u16 next_to_clean = 0;
  1710. next_to_clean = atomic_read(&tpd_ring->next_to_clean);
  1711. next_to_use = tpd_ring->next_to_use;
  1712. return (u16)(next_to_clean > next_to_use) ?
  1713. (next_to_clean - next_to_use - 1) :
  1714. (tpd_ring->count + next_to_clean - next_to_use - 1);
  1715. }
  1716. /*
  1717. * get next usable tpd
  1718. * Note: should call atl1c_tdp_avail to make sure
  1719. * there is enough tpd to use
  1720. */
  1721. static struct atl1c_tpd_desc *atl1c_get_tpd(struct atl1c_adapter *adapter,
  1722. enum atl1c_trans_queue type)
  1723. {
  1724. struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
  1725. struct atl1c_tpd_desc *tpd_desc;
  1726. u16 next_to_use = 0;
  1727. next_to_use = tpd_ring->next_to_use;
  1728. if (++tpd_ring->next_to_use == tpd_ring->count)
  1729. tpd_ring->next_to_use = 0;
  1730. tpd_desc = ATL1C_TPD_DESC(tpd_ring, next_to_use);
  1731. memset(tpd_desc, 0, sizeof(struct atl1c_tpd_desc));
  1732. return tpd_desc;
  1733. }
  1734. static struct atl1c_buffer *
  1735. atl1c_get_tx_buffer(struct atl1c_adapter *adapter, struct atl1c_tpd_desc *tpd)
  1736. {
  1737. struct atl1c_tpd_ring *tpd_ring = adapter->tpd_ring;
  1738. return &tpd_ring->buffer_info[tpd -
  1739. (struct atl1c_tpd_desc *)tpd_ring->desc];
  1740. }
  1741. /* Calculate the transmit packet descript needed*/
  1742. static u16 atl1c_cal_tpd_req(const struct sk_buff *skb)
  1743. {
  1744. u16 tpd_req;
  1745. u16 proto_hdr_len = 0;
  1746. tpd_req = skb_shinfo(skb)->nr_frags + 1;
  1747. if (skb_is_gso(skb)) {
  1748. proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1749. if (proto_hdr_len < skb_headlen(skb))
  1750. tpd_req++;
  1751. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  1752. tpd_req++;
  1753. }
  1754. return tpd_req;
  1755. }
  1756. static int atl1c_tso_csum(struct atl1c_adapter *adapter,
  1757. struct sk_buff *skb,
  1758. struct atl1c_tpd_desc **tpd,
  1759. enum atl1c_trans_queue type)
  1760. {
  1761. struct pci_dev *pdev = adapter->pdev;
  1762. u8 hdr_len;
  1763. u32 real_len;
  1764. unsigned short offload_type;
  1765. int err;
  1766. if (skb_is_gso(skb)) {
  1767. if (skb_header_cloned(skb)) {
  1768. err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  1769. if (unlikely(err))
  1770. return -1;
  1771. }
  1772. offload_type = skb_shinfo(skb)->gso_type;
  1773. if (offload_type & SKB_GSO_TCPV4) {
  1774. real_len = (((unsigned char *)ip_hdr(skb) - skb->data)
  1775. + ntohs(ip_hdr(skb)->tot_len));
  1776. if (real_len < skb->len)
  1777. pskb_trim(skb, real_len);
  1778. hdr_len = (skb_transport_offset(skb) + tcp_hdrlen(skb));
  1779. if (unlikely(skb->len == hdr_len)) {
  1780. /* only xsum need */
  1781. if (netif_msg_tx_queued(adapter))
  1782. dev_warn(&pdev->dev,
  1783. "IPV4 tso with zero data??\n");
  1784. goto check_sum;
  1785. } else {
  1786. ip_hdr(skb)->check = 0;
  1787. tcp_hdr(skb)->check = ~csum_tcpudp_magic(
  1788. ip_hdr(skb)->saddr,
  1789. ip_hdr(skb)->daddr,
  1790. 0, IPPROTO_TCP, 0);
  1791. (*tpd)->word1 |= 1 << TPD_IPV4_PACKET_SHIFT;
  1792. }
  1793. }
  1794. if (offload_type & SKB_GSO_TCPV6) {
  1795. struct atl1c_tpd_ext_desc *etpd =
  1796. *(struct atl1c_tpd_ext_desc **)(tpd);
  1797. memset(etpd, 0, sizeof(struct atl1c_tpd_ext_desc));
  1798. *tpd = atl1c_get_tpd(adapter, type);
  1799. ipv6_hdr(skb)->payload_len = 0;
  1800. /* check payload == 0 byte ? */
  1801. hdr_len = (skb_transport_offset(skb) + tcp_hdrlen(skb));
  1802. if (unlikely(skb->len == hdr_len)) {
  1803. /* only xsum need */
  1804. if (netif_msg_tx_queued(adapter))
  1805. dev_warn(&pdev->dev,
  1806. "IPV6 tso with zero data??\n");
  1807. goto check_sum;
  1808. } else
  1809. tcp_hdr(skb)->check = ~csum_ipv6_magic(
  1810. &ipv6_hdr(skb)->saddr,
  1811. &ipv6_hdr(skb)->daddr,
  1812. 0, IPPROTO_TCP, 0);
  1813. etpd->word1 |= 1 << TPD_LSO_EN_SHIFT;
  1814. etpd->word1 |= 1 << TPD_LSO_VER_SHIFT;
  1815. etpd->pkt_len = cpu_to_le32(skb->len);
  1816. (*tpd)->word1 |= 1 << TPD_LSO_VER_SHIFT;
  1817. }
  1818. (*tpd)->word1 |= 1 << TPD_LSO_EN_SHIFT;
  1819. (*tpd)->word1 |= (skb_transport_offset(skb) & TPD_TCPHDR_OFFSET_MASK) <<
  1820. TPD_TCPHDR_OFFSET_SHIFT;
  1821. (*tpd)->word1 |= (skb_shinfo(skb)->gso_size & TPD_MSS_MASK) <<
  1822. TPD_MSS_SHIFT;
  1823. return 0;
  1824. }
  1825. check_sum:
  1826. if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
  1827. u8 css, cso;
  1828. cso = skb_checksum_start_offset(skb);
  1829. if (unlikely(cso & 0x1)) {
  1830. if (netif_msg_tx_err(adapter))
  1831. dev_err(&adapter->pdev->dev,
  1832. "payload offset should not an event number\n");
  1833. return -1;
  1834. } else {
  1835. css = cso + skb->csum_offset;
  1836. (*tpd)->word1 |= ((cso >> 1) & TPD_PLOADOFFSET_MASK) <<
  1837. TPD_PLOADOFFSET_SHIFT;
  1838. (*tpd)->word1 |= ((css >> 1) & TPD_CCSUM_OFFSET_MASK) <<
  1839. TPD_CCSUM_OFFSET_SHIFT;
  1840. (*tpd)->word1 |= 1 << TPD_CCSUM_EN_SHIFT;
  1841. }
  1842. }
  1843. return 0;
  1844. }
  1845. static void atl1c_tx_map(struct atl1c_adapter *adapter,
  1846. struct sk_buff *skb, struct atl1c_tpd_desc *tpd,
  1847. enum atl1c_trans_queue type)
  1848. {
  1849. struct atl1c_tpd_desc *use_tpd = NULL;
  1850. struct atl1c_buffer *buffer_info = NULL;
  1851. u16 buf_len = skb_headlen(skb);
  1852. u16 map_len = 0;
  1853. u16 mapped_len = 0;
  1854. u16 hdr_len = 0;
  1855. u16 nr_frags;
  1856. u16 f;
  1857. int tso;
  1858. nr_frags = skb_shinfo(skb)->nr_frags;
  1859. tso = (tpd->word1 >> TPD_LSO_EN_SHIFT) & TPD_LSO_EN_MASK;
  1860. if (tso) {
  1861. /* TSO */
  1862. map_len = hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1863. use_tpd = tpd;
  1864. buffer_info = atl1c_get_tx_buffer(adapter, use_tpd);
  1865. buffer_info->length = map_len;
  1866. buffer_info->dma = pci_map_single(adapter->pdev,
  1867. skb->data, hdr_len, PCI_DMA_TODEVICE);
  1868. ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
  1869. ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_SINGLE,
  1870. ATL1C_PCIMAP_TODEVICE);
  1871. mapped_len += map_len;
  1872. use_tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
  1873. use_tpd->buffer_len = cpu_to_le16(buffer_info->length);
  1874. }
  1875. if (mapped_len < buf_len) {
  1876. /* mapped_len == 0, means we should use the first tpd,
  1877. which is given by caller */
  1878. if (mapped_len == 0)
  1879. use_tpd = tpd;
  1880. else {
  1881. use_tpd = atl1c_get_tpd(adapter, type);
  1882. memcpy(use_tpd, tpd, sizeof(struct atl1c_tpd_desc));
  1883. }
  1884. buffer_info = atl1c_get_tx_buffer(adapter, use_tpd);
  1885. buffer_info->length = buf_len - mapped_len;
  1886. buffer_info->dma =
  1887. pci_map_single(adapter->pdev, skb->data + mapped_len,
  1888. buffer_info->length, PCI_DMA_TODEVICE);
  1889. ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
  1890. ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_SINGLE,
  1891. ATL1C_PCIMAP_TODEVICE);
  1892. use_tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
  1893. use_tpd->buffer_len = cpu_to_le16(buffer_info->length);
  1894. }
  1895. for (f = 0; f < nr_frags; f++) {
  1896. struct skb_frag_struct *frag;
  1897. frag = &skb_shinfo(skb)->frags[f];
  1898. use_tpd = atl1c_get_tpd(adapter, type);
  1899. memcpy(use_tpd, tpd, sizeof(struct atl1c_tpd_desc));
  1900. buffer_info = atl1c_get_tx_buffer(adapter, use_tpd);
  1901. buffer_info->length = frag->size;
  1902. buffer_info->dma =
  1903. pci_map_page(adapter->pdev, frag->page,
  1904. frag->page_offset,
  1905. buffer_info->length,
  1906. PCI_DMA_TODEVICE);
  1907. ATL1C_SET_BUFFER_STATE(buffer_info, ATL1C_BUFFER_BUSY);
  1908. ATL1C_SET_PCIMAP_TYPE(buffer_info, ATL1C_PCIMAP_PAGE,
  1909. ATL1C_PCIMAP_TODEVICE);
  1910. use_tpd->buffer_addr = cpu_to_le64(buffer_info->dma);
  1911. use_tpd->buffer_len = cpu_to_le16(buffer_info->length);
  1912. }
  1913. /* The last tpd */
  1914. use_tpd->word1 |= 1 << TPD_EOP_SHIFT;
  1915. /* The last buffer info contain the skb address,
  1916. so it will be free after unmap */
  1917. buffer_info->skb = skb;
  1918. }
  1919. static void atl1c_tx_queue(struct atl1c_adapter *adapter, struct sk_buff *skb,
  1920. struct atl1c_tpd_desc *tpd, enum atl1c_trans_queue type)
  1921. {
  1922. struct atl1c_tpd_ring *tpd_ring = &adapter->tpd_ring[type];
  1923. u32 prod_data;
  1924. AT_READ_REG(&adapter->hw, REG_MB_PRIO_PROD_IDX, &prod_data);
  1925. switch (type) {
  1926. case atl1c_trans_high:
  1927. prod_data &= 0xFFFF0000;
  1928. prod_data |= tpd_ring->next_to_use & 0xFFFF;
  1929. break;
  1930. case atl1c_trans_normal:
  1931. prod_data &= 0x0000FFFF;
  1932. prod_data |= (tpd_ring->next_to_use & 0xFFFF) << 16;
  1933. break;
  1934. default:
  1935. break;
  1936. }
  1937. wmb();
  1938. AT_WRITE_REG(&adapter->hw, REG_MB_PRIO_PROD_IDX, prod_data);
  1939. }
  1940. static netdev_tx_t atl1c_xmit_frame(struct sk_buff *skb,
  1941. struct net_device *netdev)
  1942. {
  1943. struct atl1c_adapter *adapter = netdev_priv(netdev);
  1944. unsigned long flags;
  1945. u16 tpd_req = 1;
  1946. struct atl1c_tpd_desc *tpd;
  1947. enum atl1c_trans_queue type = atl1c_trans_normal;
  1948. if (test_bit(__AT_DOWN, &adapter->flags)) {
  1949. dev_kfree_skb_any(skb);
  1950. return NETDEV_TX_OK;
  1951. }
  1952. tpd_req = atl1c_cal_tpd_req(skb);
  1953. if (!spin_trylock_irqsave(&adapter->tx_lock, flags)) {
  1954. if (netif_msg_pktdata(adapter))
  1955. dev_info(&adapter->pdev->dev, "tx locked\n");
  1956. return NETDEV_TX_LOCKED;
  1957. }
  1958. if (atl1c_tpd_avail(adapter, type) < tpd_req) {
  1959. /* no enough descriptor, just stop queue */
  1960. netif_stop_queue(netdev);
  1961. spin_unlock_irqrestore(&adapter->tx_lock, flags);
  1962. return NETDEV_TX_BUSY;
  1963. }
  1964. tpd = atl1c_get_tpd(adapter, type);
  1965. /* do TSO and check sum */
  1966. if (atl1c_tso_csum(adapter, skb, &tpd, type) != 0) {
  1967. spin_unlock_irqrestore(&adapter->tx_lock, flags);
  1968. dev_kfree_skb_any(skb);
  1969. return NETDEV_TX_OK;
  1970. }
  1971. if (unlikely(vlan_tx_tag_present(skb))) {
  1972. u16 vlan = vlan_tx_tag_get(skb);
  1973. __le16 tag;
  1974. vlan = cpu_to_le16(vlan);
  1975. AT_VLAN_TO_TAG(vlan, tag);
  1976. tpd->word1 |= 1 << TPD_INS_VTAG_SHIFT;
  1977. tpd->vlan_tag = tag;
  1978. }
  1979. if (skb_network_offset(skb) != ETH_HLEN)
  1980. tpd->word1 |= 1 << TPD_ETH_TYPE_SHIFT; /* Ethernet frame */
  1981. atl1c_tx_map(adapter, skb, tpd, type);
  1982. atl1c_tx_queue(adapter, skb, tpd, type);
  1983. spin_unlock_irqrestore(&adapter->tx_lock, flags);
  1984. return NETDEV_TX_OK;
  1985. }
  1986. static void atl1c_free_irq(struct atl1c_adapter *adapter)
  1987. {
  1988. struct net_device *netdev = adapter->netdev;
  1989. free_irq(adapter->pdev->irq, netdev);
  1990. if (adapter->have_msi)
  1991. pci_disable_msi(adapter->pdev);
  1992. }
  1993. static int atl1c_request_irq(struct atl1c_adapter *adapter)
  1994. {
  1995. struct pci_dev *pdev = adapter->pdev;
  1996. struct net_device *netdev = adapter->netdev;
  1997. int flags = 0;
  1998. int err = 0;
  1999. adapter->have_msi = true;
  2000. err = pci_enable_msi(adapter->pdev);
  2001. if (err) {
  2002. if (netif_msg_ifup(adapter))
  2003. dev_err(&pdev->dev,
  2004. "Unable to allocate MSI interrupt Error: %d\n",
  2005. err);
  2006. adapter->have_msi = false;
  2007. } else
  2008. netdev->irq = pdev->irq;
  2009. if (!adapter->have_msi)
  2010. flags |= IRQF_SHARED;
  2011. err = request_irq(adapter->pdev->irq, atl1c_intr, flags,
  2012. netdev->name, netdev);
  2013. if (err) {
  2014. if (netif_msg_ifup(adapter))
  2015. dev_err(&pdev->dev,
  2016. "Unable to allocate interrupt Error: %d\n",
  2017. err);
  2018. if (adapter->have_msi)
  2019. pci_disable_msi(adapter->pdev);
  2020. return err;
  2021. }
  2022. if (netif_msg_ifup(adapter))
  2023. dev_dbg(&pdev->dev, "atl1c_request_irq OK\n");
  2024. return err;
  2025. }
  2026. static int atl1c_up(struct atl1c_adapter *adapter)
  2027. {
  2028. struct net_device *netdev = adapter->netdev;
  2029. int num;
  2030. int err;
  2031. int i;
  2032. netif_carrier_off(netdev);
  2033. atl1c_init_ring_ptrs(adapter);
  2034. atl1c_set_multi(netdev);
  2035. atl1c_restore_vlan(adapter);
  2036. for (i = 0; i < adapter->num_rx_queues; i++) {
  2037. num = atl1c_alloc_rx_buffer(adapter, i);
  2038. if (unlikely(num == 0)) {
  2039. err = -ENOMEM;
  2040. goto err_alloc_rx;
  2041. }
  2042. }
  2043. if (atl1c_configure(adapter)) {
  2044. err = -EIO;
  2045. goto err_up;
  2046. }
  2047. err = atl1c_request_irq(adapter);
  2048. if (unlikely(err))
  2049. goto err_up;
  2050. clear_bit(__AT_DOWN, &adapter->flags);
  2051. napi_enable(&adapter->napi);
  2052. atl1c_irq_enable(adapter);
  2053. atl1c_check_link_status(adapter);
  2054. netif_start_queue(netdev);
  2055. return err;
  2056. err_up:
  2057. err_alloc_rx:
  2058. atl1c_clean_rx_ring(adapter);
  2059. return err;
  2060. }
  2061. static void atl1c_down(struct atl1c_adapter *adapter)
  2062. {
  2063. struct net_device *netdev = adapter->netdev;
  2064. atl1c_del_timer(adapter);
  2065. adapter->work_event = 0; /* clear all event */
  2066. /* signal that we're down so the interrupt handler does not
  2067. * reschedule our watchdog timer */
  2068. set_bit(__AT_DOWN, &adapter->flags);
  2069. netif_carrier_off(netdev);
  2070. napi_disable(&adapter->napi);
  2071. atl1c_irq_disable(adapter);
  2072. atl1c_free_irq(adapter);
  2073. /* reset MAC to disable all RX/TX */
  2074. atl1c_reset_mac(&adapter->hw);
  2075. msleep(1);
  2076. adapter->link_speed = SPEED_0;
  2077. adapter->link_duplex = -1;
  2078. atl1c_clean_tx_ring(adapter, atl1c_trans_normal);
  2079. atl1c_clean_tx_ring(adapter, atl1c_trans_high);
  2080. atl1c_clean_rx_ring(adapter);
  2081. }
  2082. /*
  2083. * atl1c_open - Called when a network interface is made active
  2084. * @netdev: network interface device structure
  2085. *
  2086. * Returns 0 on success, negative value on failure
  2087. *
  2088. * The open entry point is called when a network interface is made
  2089. * active by the system (IFF_UP). At this point all resources needed
  2090. * for transmit and receive operations are allocated, the interrupt
  2091. * handler is registered with the OS, the watchdog timer is started,
  2092. * and the stack is notified that the interface is ready.
  2093. */
  2094. static int atl1c_open(struct net_device *netdev)
  2095. {
  2096. struct atl1c_adapter *adapter = netdev_priv(netdev);
  2097. int err;
  2098. /* disallow open during test */
  2099. if (test_bit(__AT_TESTING, &adapter->flags))
  2100. return -EBUSY;
  2101. /* allocate rx/tx dma buffer & descriptors */
  2102. err = atl1c_setup_ring_resources(adapter);
  2103. if (unlikely(err))
  2104. return err;
  2105. err = atl1c_up(adapter);
  2106. if (unlikely(err))
  2107. goto err_up;
  2108. if (adapter->hw.ctrl_flags & ATL1C_FPGA_VERSION) {
  2109. u32 phy_data;
  2110. AT_READ_REG(&adapter->hw, REG_MDIO_CTRL, &phy_data);
  2111. phy_data |= MDIO_AP_EN;
  2112. AT_WRITE_REG(&adapter->hw, REG_MDIO_CTRL, phy_data);
  2113. }
  2114. return 0;
  2115. err_up:
  2116. atl1c_free_irq(adapter);
  2117. atl1c_free_ring_resources(adapter);
  2118. atl1c_reset_mac(&adapter->hw);
  2119. return err;
  2120. }
  2121. /*
  2122. * atl1c_close - Disables a network interface
  2123. * @netdev: network interface device structure
  2124. *
  2125. * Returns 0, this is not allowed to fail
  2126. *
  2127. * The close entry point is called when an interface is de-activated
  2128. * by the OS. The hardware is still under the drivers control, but
  2129. * needs to be disabled. A global MAC reset is issued to stop the
  2130. * hardware, and all transmit and receive resources are freed.
  2131. */
  2132. static int atl1c_close(struct net_device *netdev)
  2133. {
  2134. struct atl1c_adapter *adapter = netdev_priv(netdev);
  2135. WARN_ON(test_bit(__AT_RESETTING, &adapter->flags));
  2136. atl1c_down(adapter);
  2137. atl1c_free_ring_resources(adapter);
  2138. return 0;
  2139. }
  2140. static int atl1c_suspend(struct device *dev)
  2141. {
  2142. struct pci_dev *pdev = to_pci_dev(dev);
  2143. struct net_device *netdev = pci_get_drvdata(pdev);
  2144. struct atl1c_adapter *adapter = netdev_priv(netdev);
  2145. struct atl1c_hw *hw = &adapter->hw;
  2146. u32 mac_ctrl_data = 0;
  2147. u32 master_ctrl_data = 0;
  2148. u32 wol_ctrl_data = 0;
  2149. u16 mii_intr_status_data = 0;
  2150. u32 wufc = adapter->wol;
  2151. atl1c_disable_l0s_l1(hw);
  2152. if (netif_running(netdev)) {
  2153. WARN_ON(test_bit(__AT_RESETTING, &adapter->flags));
  2154. atl1c_down(adapter);
  2155. }
  2156. netif_device_detach(netdev);
  2157. if (wufc)
  2158. if (atl1c_phy_power_saving(hw) != 0)
  2159. dev_dbg(&pdev->dev, "phy power saving failed");
  2160. AT_READ_REG(hw, REG_MASTER_CTRL, &master_ctrl_data);
  2161. AT_READ_REG(hw, REG_MAC_CTRL, &mac_ctrl_data);
  2162. master_ctrl_data &= ~MASTER_CTRL_CLK_SEL_DIS;
  2163. mac_ctrl_data &= ~(MAC_CTRL_PRMLEN_MASK << MAC_CTRL_PRMLEN_SHIFT);
  2164. mac_ctrl_data |= (((u32)adapter->hw.preamble_len &
  2165. MAC_CTRL_PRMLEN_MASK) <<
  2166. MAC_CTRL_PRMLEN_SHIFT);
  2167. mac_ctrl_data &= ~(MAC_CTRL_SPEED_MASK << MAC_CTRL_SPEED_SHIFT);
  2168. mac_ctrl_data &= ~MAC_CTRL_DUPLX;
  2169. if (wufc) {
  2170. mac_ctrl_data |= MAC_CTRL_RX_EN;
  2171. if (adapter->link_speed == SPEED_1000 ||
  2172. adapter->link_speed == SPEED_0) {
  2173. mac_ctrl_data |= atl1c_mac_speed_1000 <<
  2174. MAC_CTRL_SPEED_SHIFT;
  2175. mac_ctrl_data |= MAC_CTRL_DUPLX;
  2176. } else
  2177. mac_ctrl_data |= atl1c_mac_speed_10_100 <<
  2178. MAC_CTRL_SPEED_SHIFT;
  2179. if (adapter->link_duplex == DUPLEX_FULL)
  2180. mac_ctrl_data |= MAC_CTRL_DUPLX;
  2181. /* turn on magic packet wol */
  2182. if (wufc & AT_WUFC_MAG)
  2183. wol_ctrl_data |= WOL_MAGIC_EN | WOL_MAGIC_PME_EN;
  2184. if (wufc & AT_WUFC_LNKC) {
  2185. wol_ctrl_data |= WOL_LINK_CHG_EN | WOL_LINK_CHG_PME_EN;
  2186. /* only link up can wake up */
  2187. if (atl1c_write_phy_reg(hw, MII_IER, IER_LINK_UP) != 0) {
  2188. dev_dbg(&pdev->dev, "%s: read write phy "
  2189. "register failed.\n",
  2190. atl1c_driver_name);
  2191. }
  2192. }
  2193. /* clear phy interrupt */
  2194. atl1c_read_phy_reg(hw, MII_ISR, &mii_intr_status_data);
  2195. /* Config MAC Ctrl register */
  2196. if (adapter->vlgrp)
  2197. mac_ctrl_data |= MAC_CTRL_RMV_VLAN;
  2198. /* magic packet maybe Broadcast&multicast&Unicast frame */
  2199. if (wufc & AT_WUFC_MAG)
  2200. mac_ctrl_data |= MAC_CTRL_BC_EN;
  2201. dev_dbg(&pdev->dev,
  2202. "%s: suspend MAC=0x%x\n",
  2203. atl1c_driver_name, mac_ctrl_data);
  2204. AT_WRITE_REG(hw, REG_MASTER_CTRL, master_ctrl_data);
  2205. AT_WRITE_REG(hw, REG_WOL_CTRL, wol_ctrl_data);
  2206. AT_WRITE_REG(hw, REG_MAC_CTRL, mac_ctrl_data);
  2207. AT_WRITE_REG(hw, REG_GPHY_CTRL, GPHY_CTRL_DEFAULT |
  2208. GPHY_CTRL_EXT_RESET);
  2209. } else {
  2210. AT_WRITE_REG(hw, REG_GPHY_CTRL, GPHY_CTRL_POWER_SAVING);
  2211. master_ctrl_data |= MASTER_CTRL_CLK_SEL_DIS;
  2212. mac_ctrl_data |= atl1c_mac_speed_10_100 << MAC_CTRL_SPEED_SHIFT;
  2213. mac_ctrl_data |= MAC_CTRL_DUPLX;
  2214. AT_WRITE_REG(hw, REG_MASTER_CTRL, master_ctrl_data);
  2215. AT_WRITE_REG(hw, REG_MAC_CTRL, mac_ctrl_data);
  2216. AT_WRITE_REG(hw, REG_WOL_CTRL, 0);
  2217. hw->phy_configured = false; /* re-init PHY when resume */
  2218. }
  2219. return 0;
  2220. }
  2221. #ifdef CONFIG_PM_SLEEP
  2222. static int atl1c_resume(struct device *dev)
  2223. {
  2224. struct pci_dev *pdev = to_pci_dev(dev);
  2225. struct net_device *netdev = pci_get_drvdata(pdev);
  2226. struct atl1c_adapter *adapter = netdev_priv(netdev);
  2227. AT_WRITE_REG(&adapter->hw, REG_WOL_CTRL, 0);
  2228. atl1c_reset_pcie(&adapter->hw, ATL1C_PCIE_L0S_L1_DISABLE |
  2229. ATL1C_PCIE_PHY_RESET);
  2230. atl1c_phy_reset(&adapter->hw);
  2231. atl1c_reset_mac(&adapter->hw);
  2232. atl1c_phy_init(&adapter->hw);
  2233. #if 0
  2234. AT_READ_REG(&adapter->hw, REG_PM_CTRLSTAT, &pm_data);
  2235. pm_data &= ~PM_CTRLSTAT_PME_EN;
  2236. AT_WRITE_REG(&adapter->hw, REG_PM_CTRLSTAT, pm_data);
  2237. #endif
  2238. netif_device_attach(netdev);
  2239. if (netif_running(netdev))
  2240. atl1c_up(adapter);
  2241. return 0;
  2242. }
  2243. #endif
  2244. static void atl1c_shutdown(struct pci_dev *pdev)
  2245. {
  2246. struct net_device *netdev = pci_get_drvdata(pdev);
  2247. struct atl1c_adapter *adapter = netdev_priv(netdev);
  2248. atl1c_suspend(&pdev->dev);
  2249. pci_wake_from_d3(pdev, adapter->wol);
  2250. pci_set_power_state(pdev, PCI_D3hot);
  2251. }
  2252. static const struct net_device_ops atl1c_netdev_ops = {
  2253. .ndo_open = atl1c_open,
  2254. .ndo_stop = atl1c_close,
  2255. .ndo_validate_addr = eth_validate_addr,
  2256. .ndo_start_xmit = atl1c_xmit_frame,
  2257. .ndo_set_mac_address = atl1c_set_mac_addr,
  2258. .ndo_set_multicast_list = atl1c_set_multi,
  2259. .ndo_change_mtu = atl1c_change_mtu,
  2260. .ndo_fix_features = atl1c_fix_features,
  2261. .ndo_do_ioctl = atl1c_ioctl,
  2262. .ndo_tx_timeout = atl1c_tx_timeout,
  2263. .ndo_get_stats = atl1c_get_stats,
  2264. .ndo_vlan_rx_register = atl1c_vlan_rx_register,
  2265. #ifdef CONFIG_NET_POLL_CONTROLLER
  2266. .ndo_poll_controller = atl1c_netpoll,
  2267. #endif
  2268. };
  2269. static int atl1c_init_netdev(struct net_device *netdev, struct pci_dev *pdev)
  2270. {
  2271. SET_NETDEV_DEV(netdev, &pdev->dev);
  2272. pci_set_drvdata(pdev, netdev);
  2273. netdev->irq = pdev->irq;
  2274. netdev->netdev_ops = &atl1c_netdev_ops;
  2275. netdev->watchdog_timeo = AT_TX_WATCHDOG;
  2276. atl1c_set_ethtool_ops(netdev);
  2277. /* TODO: add when ready */
  2278. netdev->hw_features = NETIF_F_SG |
  2279. NETIF_F_HW_CSUM |
  2280. NETIF_F_HW_VLAN_TX |
  2281. NETIF_F_TSO |
  2282. NETIF_F_TSO6;
  2283. netdev->features = netdev->hw_features |
  2284. NETIF_F_HW_VLAN_RX;
  2285. return 0;
  2286. }
  2287. /*
  2288. * atl1c_probe - Device Initialization Routine
  2289. * @pdev: PCI device information struct
  2290. * @ent: entry in atl1c_pci_tbl
  2291. *
  2292. * Returns 0 on success, negative on failure
  2293. *
  2294. * atl1c_probe initializes an adapter identified by a pci_dev structure.
  2295. * The OS initialization, configuring of the adapter private structure,
  2296. * and a hardware reset occur.
  2297. */
  2298. static int __devinit atl1c_probe(struct pci_dev *pdev,
  2299. const struct pci_device_id *ent)
  2300. {
  2301. struct net_device *netdev;
  2302. struct atl1c_adapter *adapter;
  2303. static int cards_found;
  2304. int err = 0;
  2305. /* enable device (incl. PCI PM wakeup and hotplug setup) */
  2306. err = pci_enable_device_mem(pdev);
  2307. if (err) {
  2308. dev_err(&pdev->dev, "cannot enable PCI device\n");
  2309. return err;
  2310. }
  2311. /*
  2312. * The atl1c chip can DMA to 64-bit addresses, but it uses a single
  2313. * shared register for the high 32 bits, so only a single, aligned,
  2314. * 4 GB physical address range can be used at a time.
  2315. *
  2316. * Supporting 64-bit DMA on this hardware is more trouble than it's
  2317. * worth. It is far easier to limit to 32-bit DMA than update
  2318. * various kernel subsystems to support the mechanics required by a
  2319. * fixed-high-32-bit system.
  2320. */
  2321. if ((pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) ||
  2322. (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)) {
  2323. dev_err(&pdev->dev, "No usable DMA configuration,aborting\n");
  2324. goto err_dma;
  2325. }
  2326. err = pci_request_regions(pdev, atl1c_driver_name);
  2327. if (err) {
  2328. dev_err(&pdev->dev, "cannot obtain PCI resources\n");
  2329. goto err_pci_reg;
  2330. }
  2331. pci_set_master(pdev);
  2332. netdev = alloc_etherdev(sizeof(struct atl1c_adapter));
  2333. if (netdev == NULL) {
  2334. err = -ENOMEM;
  2335. dev_err(&pdev->dev, "etherdev alloc failed\n");
  2336. goto err_alloc_etherdev;
  2337. }
  2338. err = atl1c_init_netdev(netdev, pdev);
  2339. if (err) {
  2340. dev_err(&pdev->dev, "init netdevice failed\n");
  2341. goto err_init_netdev;
  2342. }
  2343. adapter = netdev_priv(netdev);
  2344. adapter->bd_number = cards_found;
  2345. adapter->netdev = netdev;
  2346. adapter->pdev = pdev;
  2347. adapter->hw.adapter = adapter;
  2348. adapter->msg_enable = netif_msg_init(-1, atl1c_default_msg);
  2349. adapter->hw.hw_addr = ioremap(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
  2350. if (!adapter->hw.hw_addr) {
  2351. err = -EIO;
  2352. dev_err(&pdev->dev, "cannot map device registers\n");
  2353. goto err_ioremap;
  2354. }
  2355. netdev->base_addr = (unsigned long)adapter->hw.hw_addr;
  2356. /* init mii data */
  2357. adapter->mii.dev = netdev;
  2358. adapter->mii.mdio_read = atl1c_mdio_read;
  2359. adapter->mii.mdio_write = atl1c_mdio_write;
  2360. adapter->mii.phy_id_mask = 0x1f;
  2361. adapter->mii.reg_num_mask = MDIO_REG_ADDR_MASK;
  2362. netif_napi_add(netdev, &adapter->napi, atl1c_clean, 64);
  2363. setup_timer(&adapter->phy_config_timer, atl1c_phy_config,
  2364. (unsigned long)adapter);
  2365. /* setup the private structure */
  2366. err = atl1c_sw_init(adapter);
  2367. if (err) {
  2368. dev_err(&pdev->dev, "net device private data init failed\n");
  2369. goto err_sw_init;
  2370. }
  2371. atl1c_reset_pcie(&adapter->hw, ATL1C_PCIE_L0S_L1_DISABLE |
  2372. ATL1C_PCIE_PHY_RESET);
  2373. /* Init GPHY as early as possible due to power saving issue */
  2374. atl1c_phy_reset(&adapter->hw);
  2375. err = atl1c_reset_mac(&adapter->hw);
  2376. if (err) {
  2377. err = -EIO;
  2378. goto err_reset;
  2379. }
  2380. /* reset the controller to
  2381. * put the device in a known good starting state */
  2382. err = atl1c_phy_init(&adapter->hw);
  2383. if (err) {
  2384. err = -EIO;
  2385. goto err_reset;
  2386. }
  2387. if (atl1c_read_mac_addr(&adapter->hw) != 0) {
  2388. err = -EIO;
  2389. dev_err(&pdev->dev, "get mac address failed\n");
  2390. goto err_eeprom;
  2391. }
  2392. memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
  2393. memcpy(netdev->perm_addr, adapter->hw.mac_addr, netdev->addr_len);
  2394. if (netif_msg_probe(adapter))
  2395. dev_dbg(&pdev->dev, "mac address : %pM\n",
  2396. adapter->hw.mac_addr);
  2397. atl1c_hw_set_mac_addr(&adapter->hw);
  2398. INIT_WORK(&adapter->common_task, atl1c_common_task);
  2399. adapter->work_event = 0;
  2400. err = register_netdev(netdev);
  2401. if (err) {
  2402. dev_err(&pdev->dev, "register netdevice failed\n");
  2403. goto err_register;
  2404. }
  2405. if (netif_msg_probe(adapter))
  2406. dev_info(&pdev->dev, "version %s\n", ATL1C_DRV_VERSION);
  2407. cards_found++;
  2408. return 0;
  2409. err_reset:
  2410. err_register:
  2411. err_sw_init:
  2412. err_eeprom:
  2413. iounmap(adapter->hw.hw_addr);
  2414. err_init_netdev:
  2415. err_ioremap:
  2416. free_netdev(netdev);
  2417. err_alloc_etherdev:
  2418. pci_release_regions(pdev);
  2419. err_pci_reg:
  2420. err_dma:
  2421. pci_disable_device(pdev);
  2422. return err;
  2423. }
  2424. /*
  2425. * atl1c_remove - Device Removal Routine
  2426. * @pdev: PCI device information struct
  2427. *
  2428. * atl1c_remove is called by the PCI subsystem to alert the driver
  2429. * that it should release a PCI device. The could be caused by a
  2430. * Hot-Plug event, or because the driver is going to be removed from
  2431. * memory.
  2432. */
  2433. static void __devexit atl1c_remove(struct pci_dev *pdev)
  2434. {
  2435. struct net_device *netdev = pci_get_drvdata(pdev);
  2436. struct atl1c_adapter *adapter = netdev_priv(netdev);
  2437. unregister_netdev(netdev);
  2438. atl1c_phy_disable(&adapter->hw);
  2439. iounmap(adapter->hw.hw_addr);
  2440. pci_release_regions(pdev);
  2441. pci_disable_device(pdev);
  2442. free_netdev(netdev);
  2443. }
  2444. /*
  2445. * atl1c_io_error_detected - called when PCI error is detected
  2446. * @pdev: Pointer to PCI device
  2447. * @state: The current pci connection state
  2448. *
  2449. * This function is called after a PCI bus error affecting
  2450. * this device has been detected.
  2451. */
  2452. static pci_ers_result_t atl1c_io_error_detected(struct pci_dev *pdev,
  2453. pci_channel_state_t state)
  2454. {
  2455. struct net_device *netdev = pci_get_drvdata(pdev);
  2456. struct atl1c_adapter *adapter = netdev_priv(netdev);
  2457. netif_device_detach(netdev);
  2458. if (state == pci_channel_io_perm_failure)
  2459. return PCI_ERS_RESULT_DISCONNECT;
  2460. if (netif_running(netdev))
  2461. atl1c_down(adapter);
  2462. pci_disable_device(pdev);
  2463. /* Request a slot slot reset. */
  2464. return PCI_ERS_RESULT_NEED_RESET;
  2465. }
  2466. /*
  2467. * atl1c_io_slot_reset - called after the pci bus has been reset.
  2468. * @pdev: Pointer to PCI device
  2469. *
  2470. * Restart the card from scratch, as if from a cold-boot. Implementation
  2471. * resembles the first-half of the e1000_resume routine.
  2472. */
  2473. static pci_ers_result_t atl1c_io_slot_reset(struct pci_dev *pdev)
  2474. {
  2475. struct net_device *netdev = pci_get_drvdata(pdev);
  2476. struct atl1c_adapter *adapter = netdev_priv(netdev);
  2477. if (pci_enable_device(pdev)) {
  2478. if (netif_msg_hw(adapter))
  2479. dev_err(&pdev->dev,
  2480. "Cannot re-enable PCI device after reset\n");
  2481. return PCI_ERS_RESULT_DISCONNECT;
  2482. }
  2483. pci_set_master(pdev);
  2484. pci_enable_wake(pdev, PCI_D3hot, 0);
  2485. pci_enable_wake(pdev, PCI_D3cold, 0);
  2486. atl1c_reset_mac(&adapter->hw);
  2487. return PCI_ERS_RESULT_RECOVERED;
  2488. }
  2489. /*
  2490. * atl1c_io_resume - called when traffic can start flowing again.
  2491. * @pdev: Pointer to PCI device
  2492. *
  2493. * This callback is called when the error recovery driver tells us that
  2494. * its OK to resume normal operation. Implementation resembles the
  2495. * second-half of the atl1c_resume routine.
  2496. */
  2497. static void atl1c_io_resume(struct pci_dev *pdev)
  2498. {
  2499. struct net_device *netdev = pci_get_drvdata(pdev);
  2500. struct atl1c_adapter *adapter = netdev_priv(netdev);
  2501. if (netif_running(netdev)) {
  2502. if (atl1c_up(adapter)) {
  2503. if (netif_msg_hw(adapter))
  2504. dev_err(&pdev->dev,
  2505. "Cannot bring device back up after reset\n");
  2506. return;
  2507. }
  2508. }
  2509. netif_device_attach(netdev);
  2510. }
  2511. static struct pci_error_handlers atl1c_err_handler = {
  2512. .error_detected = atl1c_io_error_detected,
  2513. .slot_reset = atl1c_io_slot_reset,
  2514. .resume = atl1c_io_resume,
  2515. };
  2516. static SIMPLE_DEV_PM_OPS(atl1c_pm_ops, atl1c_suspend, atl1c_resume);
  2517. static struct pci_driver atl1c_driver = {
  2518. .name = atl1c_driver_name,
  2519. .id_table = atl1c_pci_tbl,
  2520. .probe = atl1c_probe,
  2521. .remove = __devexit_p(atl1c_remove),
  2522. .shutdown = atl1c_shutdown,
  2523. .err_handler = &atl1c_err_handler,
  2524. .driver.pm = &atl1c_pm_ops,
  2525. };
  2526. /*
  2527. * atl1c_init_module - Driver Registration Routine
  2528. *
  2529. * atl1c_init_module is the first routine called when the driver is
  2530. * loaded. All it does is register with the PCI subsystem.
  2531. */
  2532. static int __init atl1c_init_module(void)
  2533. {
  2534. return pci_register_driver(&atl1c_driver);
  2535. }
  2536. /*
  2537. * atl1c_exit_module - Driver Exit Cleanup Routine
  2538. *
  2539. * atl1c_exit_module is called just before the driver is removed
  2540. * from memory.
  2541. */
  2542. static void __exit atl1c_exit_module(void)
  2543. {
  2544. pci_unregister_driver(&atl1c_driver);
  2545. }
  2546. module_init(atl1c_init_module);
  2547. module_exit(atl1c_exit_module);