atl1c_main.c 76 KB

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