atl1c_main.c 75 KB

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