netxen_nic_main.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528
  1. /*
  2. * Copyright (C) 2003 - 2009 NetXen, Inc.
  3. * Copyright (C) 2009 - QLogic Corporation.
  4. * All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * The full GNU General Public License is included in this distribution
  20. * in the file called "COPYING".
  21. *
  22. */
  23. #include <linux/slab.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/interrupt.h>
  26. #include "netxen_nic_hw.h"
  27. #include "netxen_nic.h"
  28. #include <linux/dma-mapping.h>
  29. #include <linux/if_vlan.h>
  30. #include <net/ip.h>
  31. #include <linux/ipv6.h>
  32. #include <linux/inetdevice.h>
  33. #include <linux/sysfs.h>
  34. #include <linux/aer.h>
  35. MODULE_DESCRIPTION("QLogic/NetXen (1/10) GbE Intelligent Ethernet Driver");
  36. MODULE_LICENSE("GPL");
  37. MODULE_VERSION(NETXEN_NIC_LINUX_VERSIONID);
  38. /*(DEBLOBBED)*/
  39. char netxen_nic_driver_name[] = "netxen_nic";
  40. static char netxen_nic_driver_string[] = "QLogic/NetXen Network Driver v"
  41. NETXEN_NIC_LINUX_VERSIONID;
  42. static int port_mode = NETXEN_PORT_MODE_AUTO_NEG;
  43. /* Default to restricted 1G auto-neg mode */
  44. static int wol_port_mode = 5;
  45. static int use_msi = 1;
  46. static int use_msi_x = 1;
  47. static int auto_fw_reset = AUTO_FW_RESET_ENABLED;
  48. module_param(auto_fw_reset, int, 0644);
  49. MODULE_PARM_DESC(auto_fw_reset,"Auto firmware reset (0=disabled, 1=enabled");
  50. static int netxen_nic_probe(struct pci_dev *pdev,
  51. const struct pci_device_id *ent);
  52. static void netxen_nic_remove(struct pci_dev *pdev);
  53. static int netxen_nic_open(struct net_device *netdev);
  54. static int netxen_nic_close(struct net_device *netdev);
  55. static netdev_tx_t netxen_nic_xmit_frame(struct sk_buff *,
  56. struct net_device *);
  57. static void netxen_tx_timeout(struct net_device *netdev);
  58. static void netxen_tx_timeout_task(struct work_struct *work);
  59. static void netxen_fw_poll_work(struct work_struct *work);
  60. static void netxen_schedule_work(struct netxen_adapter *adapter,
  61. work_func_t func, int delay);
  62. static void netxen_cancel_fw_work(struct netxen_adapter *adapter);
  63. static int netxen_nic_poll(struct napi_struct *napi, int budget);
  64. #ifdef CONFIG_NET_POLL_CONTROLLER
  65. static void netxen_nic_poll_controller(struct net_device *netdev);
  66. #endif
  67. static void netxen_create_sysfs_entries(struct netxen_adapter *adapter);
  68. static void netxen_remove_sysfs_entries(struct netxen_adapter *adapter);
  69. static void netxen_create_diag_entries(struct netxen_adapter *adapter);
  70. static void netxen_remove_diag_entries(struct netxen_adapter *adapter);
  71. static int nx_dev_request_aer(struct netxen_adapter *adapter);
  72. static int nx_decr_dev_ref_cnt(struct netxen_adapter *adapter);
  73. static int netxen_can_start_firmware(struct netxen_adapter *adapter);
  74. static irqreturn_t netxen_intr(int irq, void *data);
  75. static irqreturn_t netxen_msi_intr(int irq, void *data);
  76. static irqreturn_t netxen_msix_intr(int irq, void *data);
  77. static void netxen_free_ip_list(struct netxen_adapter *, bool);
  78. static void netxen_restore_indev_addr(struct net_device *dev, unsigned long);
  79. static struct rtnl_link_stats64 *netxen_nic_get_stats(struct net_device *dev,
  80. struct rtnl_link_stats64 *stats);
  81. static int netxen_nic_set_mac(struct net_device *netdev, void *p);
  82. /* PCI Device ID Table */
  83. #define ENTRY(device) \
  84. {PCI_DEVICE(PCI_VENDOR_ID_NETXEN, (device)), \
  85. .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
  86. static const struct pci_device_id netxen_pci_tbl[] = {
  87. ENTRY(PCI_DEVICE_ID_NX2031_10GXSR),
  88. ENTRY(PCI_DEVICE_ID_NX2031_10GCX4),
  89. ENTRY(PCI_DEVICE_ID_NX2031_4GCU),
  90. ENTRY(PCI_DEVICE_ID_NX2031_IMEZ),
  91. ENTRY(PCI_DEVICE_ID_NX2031_HMEZ),
  92. ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT),
  93. ENTRY(PCI_DEVICE_ID_NX2031_XG_MGMT2),
  94. ENTRY(PCI_DEVICE_ID_NX3031),
  95. {0,}
  96. };
  97. MODULE_DEVICE_TABLE(pci, netxen_pci_tbl);
  98. static uint32_t crb_cmd_producer[4] = {
  99. CRB_CMD_PRODUCER_OFFSET, CRB_CMD_PRODUCER_OFFSET_1,
  100. CRB_CMD_PRODUCER_OFFSET_2, CRB_CMD_PRODUCER_OFFSET_3
  101. };
  102. void
  103. netxen_nic_update_cmd_producer(struct netxen_adapter *adapter,
  104. struct nx_host_tx_ring *tx_ring)
  105. {
  106. NXWRIO(adapter, tx_ring->crb_cmd_producer, tx_ring->producer);
  107. }
  108. static uint32_t crb_cmd_consumer[4] = {
  109. CRB_CMD_CONSUMER_OFFSET, CRB_CMD_CONSUMER_OFFSET_1,
  110. CRB_CMD_CONSUMER_OFFSET_2, CRB_CMD_CONSUMER_OFFSET_3
  111. };
  112. static inline void
  113. netxen_nic_update_cmd_consumer(struct netxen_adapter *adapter,
  114. struct nx_host_tx_ring *tx_ring)
  115. {
  116. NXWRIO(adapter, tx_ring->crb_cmd_consumer, tx_ring->sw_consumer);
  117. }
  118. static uint32_t msi_tgt_status[8] = {
  119. ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
  120. ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
  121. ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
  122. ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
  123. };
  124. static struct netxen_legacy_intr_set legacy_intr[] = NX_LEGACY_INTR_CONFIG;
  125. static inline void netxen_nic_disable_int(struct nx_host_sds_ring *sds_ring)
  126. {
  127. struct netxen_adapter *adapter = sds_ring->adapter;
  128. NXWRIO(adapter, sds_ring->crb_intr_mask, 0);
  129. }
  130. static inline void netxen_nic_enable_int(struct nx_host_sds_ring *sds_ring)
  131. {
  132. struct netxen_adapter *adapter = sds_ring->adapter;
  133. NXWRIO(adapter, sds_ring->crb_intr_mask, 0x1);
  134. if (!NETXEN_IS_MSI_FAMILY(adapter))
  135. NXWRIO(adapter, adapter->tgt_mask_reg, 0xfbff);
  136. }
  137. static int
  138. netxen_alloc_sds_rings(struct netxen_recv_context *recv_ctx, int count)
  139. {
  140. int size = sizeof(struct nx_host_sds_ring) * count;
  141. recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
  142. return recv_ctx->sds_rings == NULL;
  143. }
  144. static void
  145. netxen_free_sds_rings(struct netxen_recv_context *recv_ctx)
  146. {
  147. kfree(recv_ctx->sds_rings);
  148. recv_ctx->sds_rings = NULL;
  149. }
  150. static int
  151. netxen_napi_add(struct netxen_adapter *adapter, struct net_device *netdev)
  152. {
  153. int ring;
  154. struct nx_host_sds_ring *sds_ring;
  155. struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
  156. if (netxen_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
  157. return -ENOMEM;
  158. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  159. sds_ring = &recv_ctx->sds_rings[ring];
  160. netif_napi_add(netdev, &sds_ring->napi,
  161. netxen_nic_poll, NAPI_POLL_WEIGHT);
  162. }
  163. return 0;
  164. }
  165. static void
  166. netxen_napi_del(struct netxen_adapter *adapter)
  167. {
  168. int ring;
  169. struct nx_host_sds_ring *sds_ring;
  170. struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
  171. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  172. sds_ring = &recv_ctx->sds_rings[ring];
  173. netif_napi_del(&sds_ring->napi);
  174. }
  175. netxen_free_sds_rings(&adapter->recv_ctx);
  176. }
  177. static void
  178. netxen_napi_enable(struct netxen_adapter *adapter)
  179. {
  180. int ring;
  181. struct nx_host_sds_ring *sds_ring;
  182. struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
  183. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  184. sds_ring = &recv_ctx->sds_rings[ring];
  185. napi_enable(&sds_ring->napi);
  186. netxen_nic_enable_int(sds_ring);
  187. }
  188. }
  189. static void
  190. netxen_napi_disable(struct netxen_adapter *adapter)
  191. {
  192. int ring;
  193. struct nx_host_sds_ring *sds_ring;
  194. struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
  195. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  196. sds_ring = &recv_ctx->sds_rings[ring];
  197. netxen_nic_disable_int(sds_ring);
  198. napi_synchronize(&sds_ring->napi);
  199. napi_disable(&sds_ring->napi);
  200. }
  201. }
  202. static int nx_set_dma_mask(struct netxen_adapter *adapter)
  203. {
  204. struct pci_dev *pdev = adapter->pdev;
  205. uint64_t mask, cmask;
  206. adapter->pci_using_dac = 0;
  207. mask = DMA_BIT_MASK(32);
  208. cmask = DMA_BIT_MASK(32);
  209. if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  210. #ifndef CONFIG_IA64
  211. mask = DMA_BIT_MASK(35);
  212. #endif
  213. } else {
  214. mask = DMA_BIT_MASK(39);
  215. cmask = mask;
  216. }
  217. if (pci_set_dma_mask(pdev, mask) == 0 &&
  218. pci_set_consistent_dma_mask(pdev, cmask) == 0) {
  219. adapter->pci_using_dac = 1;
  220. return 0;
  221. }
  222. return -EIO;
  223. }
  224. /* Update addressable range if firmware supports it */
  225. static int
  226. nx_update_dma_mask(struct netxen_adapter *adapter)
  227. {
  228. int change, shift, err;
  229. uint64_t mask, old_mask, old_cmask;
  230. struct pci_dev *pdev = adapter->pdev;
  231. change = 0;
  232. shift = NXRD32(adapter, CRB_DMA_SHIFT);
  233. if (shift > 32)
  234. return 0;
  235. if (NX_IS_REVISION_P3(adapter->ahw.revision_id) && (shift > 9))
  236. change = 1;
  237. else if ((adapter->ahw.revision_id == NX_P2_C1) && (shift <= 4))
  238. change = 1;
  239. if (change) {
  240. old_mask = pdev->dma_mask;
  241. old_cmask = pdev->dev.coherent_dma_mask;
  242. mask = DMA_BIT_MASK(32+shift);
  243. err = pci_set_dma_mask(pdev, mask);
  244. if (err)
  245. goto err_out;
  246. if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
  247. err = pci_set_consistent_dma_mask(pdev, mask);
  248. if (err)
  249. goto err_out;
  250. }
  251. dev_info(&pdev->dev, "using %d-bit dma mask\n", 32+shift);
  252. }
  253. return 0;
  254. err_out:
  255. pci_set_dma_mask(pdev, old_mask);
  256. pci_set_consistent_dma_mask(pdev, old_cmask);
  257. return err;
  258. }
  259. static int
  260. netxen_check_hw_init(struct netxen_adapter *adapter, int first_boot)
  261. {
  262. u32 val, timeout;
  263. if (first_boot == 0x55555555) {
  264. /* This is the first boot after power up */
  265. NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), NETXEN_BDINFO_MAGIC);
  266. if (!NX_IS_REVISION_P2(adapter->ahw.revision_id))
  267. return 0;
  268. /* PCI bus master workaround */
  269. first_boot = NXRD32(adapter, NETXEN_PCIE_REG(0x4));
  270. if (!(first_boot & 0x4)) {
  271. first_boot |= 0x4;
  272. NXWR32(adapter, NETXEN_PCIE_REG(0x4), first_boot);
  273. NXRD32(adapter, NETXEN_PCIE_REG(0x4));
  274. }
  275. /* This is the first boot after power up */
  276. first_boot = NXRD32(adapter, NETXEN_ROMUSB_GLB_SW_RESET);
  277. if (first_boot != 0x80000f) {
  278. /* clear the register for future unloads/loads */
  279. NXWR32(adapter, NETXEN_CAM_RAM(0x1fc), 0);
  280. return -EIO;
  281. }
  282. /* Start P2 boot loader */
  283. val = NXRD32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE);
  284. NXWR32(adapter, NETXEN_ROMUSB_GLB_PEGTUNE_DONE, val | 0x1);
  285. timeout = 0;
  286. do {
  287. msleep(1);
  288. val = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
  289. if (++timeout > 5000)
  290. return -EIO;
  291. } while (val == NETXEN_BDINFO_MAGIC);
  292. }
  293. return 0;
  294. }
  295. static void netxen_set_port_mode(struct netxen_adapter *adapter)
  296. {
  297. u32 val, data;
  298. val = adapter->ahw.board_type;
  299. if ((val == NETXEN_BRDTYPE_P3_HMEZ) ||
  300. (val == NETXEN_BRDTYPE_P3_XG_LOM)) {
  301. if (port_mode == NETXEN_PORT_MODE_802_3_AP) {
  302. data = NETXEN_PORT_MODE_802_3_AP;
  303. NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
  304. } else if (port_mode == NETXEN_PORT_MODE_XG) {
  305. data = NETXEN_PORT_MODE_XG;
  306. NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
  307. } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_1G) {
  308. data = NETXEN_PORT_MODE_AUTO_NEG_1G;
  309. NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
  310. } else if (port_mode == NETXEN_PORT_MODE_AUTO_NEG_XG) {
  311. data = NETXEN_PORT_MODE_AUTO_NEG_XG;
  312. NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
  313. } else {
  314. data = NETXEN_PORT_MODE_AUTO_NEG;
  315. NXWR32(adapter, NETXEN_PORT_MODE_ADDR, data);
  316. }
  317. if ((wol_port_mode != NETXEN_PORT_MODE_802_3_AP) &&
  318. (wol_port_mode != NETXEN_PORT_MODE_XG) &&
  319. (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_1G) &&
  320. (wol_port_mode != NETXEN_PORT_MODE_AUTO_NEG_XG)) {
  321. wol_port_mode = NETXEN_PORT_MODE_AUTO_NEG;
  322. }
  323. NXWR32(adapter, NETXEN_WOL_PORT_MODE, wol_port_mode);
  324. }
  325. }
  326. #define PCI_CAP_ID_GEN 0x10
  327. static void netxen_pcie_strap_init(struct netxen_adapter *adapter)
  328. {
  329. u32 pdevfuncsave;
  330. u32 c8c9value = 0;
  331. u32 chicken = 0;
  332. u32 control = 0;
  333. int i, pos;
  334. struct pci_dev *pdev;
  335. pdev = adapter->pdev;
  336. chicken = NXRD32(adapter, NETXEN_PCIE_REG(PCIE_CHICKEN3));
  337. /* clear chicken3.25:24 */
  338. chicken &= 0xFCFFFFFF;
  339. /*
  340. * if gen1 and B0, set F1020 - if gen 2, do nothing
  341. * if gen2 set to F1000
  342. */
  343. pos = pci_find_capability(pdev, PCI_CAP_ID_GEN);
  344. if (pos == 0xC0) {
  345. pci_read_config_dword(pdev, pos + 0x10, &control);
  346. if ((control & 0x000F0000) != 0x00020000) {
  347. /* set chicken3.24 if gen1 */
  348. chicken |= 0x01000000;
  349. }
  350. dev_info(&adapter->pdev->dev, "Gen2 strapping detected\n");
  351. c8c9value = 0xF1000;
  352. } else {
  353. /* set chicken3.24 if gen1 */
  354. chicken |= 0x01000000;
  355. dev_info(&adapter->pdev->dev, "Gen1 strapping detected\n");
  356. if (adapter->ahw.revision_id == NX_P3_B0)
  357. c8c9value = 0xF1020;
  358. else
  359. c8c9value = 0;
  360. }
  361. NXWR32(adapter, NETXEN_PCIE_REG(PCIE_CHICKEN3), chicken);
  362. if (!c8c9value)
  363. return;
  364. pdevfuncsave = pdev->devfn;
  365. if (pdevfuncsave & 0x07)
  366. return;
  367. for (i = 0; i < 8; i++) {
  368. pci_read_config_dword(pdev, pos + 8, &control);
  369. pci_read_config_dword(pdev, pos + 8, &control);
  370. pci_write_config_dword(pdev, pos + 8, c8c9value);
  371. pdev->devfn++;
  372. }
  373. pdev->devfn = pdevfuncsave;
  374. }
  375. static void netxen_set_msix_bit(struct pci_dev *pdev, int enable)
  376. {
  377. u32 control;
  378. if (pdev->msix_cap) {
  379. pci_read_config_dword(pdev, pdev->msix_cap, &control);
  380. if (enable)
  381. control |= PCI_MSIX_FLAGS_ENABLE;
  382. else
  383. control = 0;
  384. pci_write_config_dword(pdev, pdev->msix_cap, control);
  385. }
  386. }
  387. static void netxen_init_msix_entries(struct netxen_adapter *adapter, int count)
  388. {
  389. int i;
  390. for (i = 0; i < count; i++)
  391. adapter->msix_entries[i].entry = i;
  392. }
  393. static int
  394. netxen_read_mac_addr(struct netxen_adapter *adapter)
  395. {
  396. int i;
  397. unsigned char *p;
  398. u64 mac_addr;
  399. struct net_device *netdev = adapter->netdev;
  400. struct pci_dev *pdev = adapter->pdev;
  401. if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
  402. if (netxen_p3_get_mac_addr(adapter, &mac_addr) != 0)
  403. return -EIO;
  404. } else {
  405. if (netxen_get_flash_mac_addr(adapter, &mac_addr) != 0)
  406. return -EIO;
  407. }
  408. p = (unsigned char *)&mac_addr;
  409. for (i = 0; i < 6; i++)
  410. netdev->dev_addr[i] = *(p + 5 - i);
  411. memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
  412. /* set station address */
  413. if (!is_valid_ether_addr(netdev->dev_addr))
  414. dev_warn(&pdev->dev, "Bad MAC address %pM.\n", netdev->dev_addr);
  415. return 0;
  416. }
  417. static int netxen_nic_set_mac(struct net_device *netdev, void *p)
  418. {
  419. struct netxen_adapter *adapter = netdev_priv(netdev);
  420. struct sockaddr *addr = p;
  421. if (!is_valid_ether_addr(addr->sa_data))
  422. return -EADDRNOTAVAIL;
  423. if (netif_running(netdev)) {
  424. netif_device_detach(netdev);
  425. netxen_napi_disable(adapter);
  426. }
  427. memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
  428. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  429. adapter->macaddr_set(adapter, addr->sa_data);
  430. if (netif_running(netdev)) {
  431. netif_device_attach(netdev);
  432. netxen_napi_enable(adapter);
  433. }
  434. return 0;
  435. }
  436. static void netxen_set_multicast_list(struct net_device *dev)
  437. {
  438. struct netxen_adapter *adapter = netdev_priv(dev);
  439. adapter->set_multi(dev);
  440. }
  441. static netdev_features_t netxen_fix_features(struct net_device *dev,
  442. netdev_features_t features)
  443. {
  444. if (!(features & NETIF_F_RXCSUM)) {
  445. netdev_info(dev, "disabling LRO as RXCSUM is off\n");
  446. features &= ~NETIF_F_LRO;
  447. }
  448. return features;
  449. }
  450. static int netxen_set_features(struct net_device *dev,
  451. netdev_features_t features)
  452. {
  453. struct netxen_adapter *adapter = netdev_priv(dev);
  454. int hw_lro;
  455. if (!((dev->features ^ features) & NETIF_F_LRO))
  456. return 0;
  457. hw_lro = (features & NETIF_F_LRO) ? NETXEN_NIC_LRO_ENABLED
  458. : NETXEN_NIC_LRO_DISABLED;
  459. if (netxen_config_hw_lro(adapter, hw_lro))
  460. return -EIO;
  461. if (!(features & NETIF_F_LRO) && netxen_send_lro_cleanup(adapter))
  462. return -EIO;
  463. return 0;
  464. }
  465. static const struct net_device_ops netxen_netdev_ops = {
  466. .ndo_open = netxen_nic_open,
  467. .ndo_stop = netxen_nic_close,
  468. .ndo_start_xmit = netxen_nic_xmit_frame,
  469. .ndo_get_stats64 = netxen_nic_get_stats,
  470. .ndo_validate_addr = eth_validate_addr,
  471. .ndo_set_rx_mode = netxen_set_multicast_list,
  472. .ndo_set_mac_address = netxen_nic_set_mac,
  473. .ndo_change_mtu = netxen_nic_change_mtu,
  474. .ndo_tx_timeout = netxen_tx_timeout,
  475. .ndo_fix_features = netxen_fix_features,
  476. .ndo_set_features = netxen_set_features,
  477. #ifdef CONFIG_NET_POLL_CONTROLLER
  478. .ndo_poll_controller = netxen_nic_poll_controller,
  479. #endif
  480. };
  481. static inline bool netxen_function_zero(struct pci_dev *pdev)
  482. {
  483. return (PCI_FUNC(pdev->devfn) == 0) ? true : false;
  484. }
  485. static inline void netxen_set_interrupt_mode(struct netxen_adapter *adapter,
  486. u32 mode)
  487. {
  488. NXWR32(adapter, NETXEN_INTR_MODE_REG, mode);
  489. }
  490. static inline u32 netxen_get_interrupt_mode(struct netxen_adapter *adapter)
  491. {
  492. return NXRD32(adapter, NETXEN_INTR_MODE_REG);
  493. }
  494. static void
  495. netxen_initialize_interrupt_registers(struct netxen_adapter *adapter)
  496. {
  497. struct netxen_legacy_intr_set *legacy_intrp;
  498. u32 tgt_status_reg, int_state_reg;
  499. if (adapter->ahw.revision_id >= NX_P3_B0)
  500. legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
  501. else
  502. legacy_intrp = &legacy_intr[0];
  503. tgt_status_reg = legacy_intrp->tgt_status_reg;
  504. int_state_reg = ISR_INT_STATE_REG;
  505. adapter->int_vec_bit = legacy_intrp->int_vec_bit;
  506. adapter->tgt_status_reg = netxen_get_ioaddr(adapter, tgt_status_reg);
  507. adapter->tgt_mask_reg = netxen_get_ioaddr(adapter,
  508. legacy_intrp->tgt_mask_reg);
  509. adapter->pci_int_reg = netxen_get_ioaddr(adapter,
  510. legacy_intrp->pci_int_reg);
  511. adapter->isr_int_vec = netxen_get_ioaddr(adapter, ISR_INT_VECTOR);
  512. if (adapter->ahw.revision_id >= NX_P3_B1)
  513. adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
  514. int_state_reg);
  515. else
  516. adapter->crb_int_state_reg = netxen_get_ioaddr(adapter,
  517. CRB_INT_VECTOR);
  518. }
  519. static int netxen_setup_msi_interrupts(struct netxen_adapter *adapter,
  520. int num_msix)
  521. {
  522. struct pci_dev *pdev = adapter->pdev;
  523. u32 value;
  524. int err;
  525. if (adapter->msix_supported) {
  526. netxen_init_msix_entries(adapter, num_msix);
  527. err = pci_enable_msix_range(pdev, adapter->msix_entries,
  528. num_msix, num_msix);
  529. if (err > 0) {
  530. adapter->flags |= NETXEN_NIC_MSIX_ENABLED;
  531. netxen_set_msix_bit(pdev, 1);
  532. if (adapter->rss_supported)
  533. adapter->max_sds_rings = num_msix;
  534. dev_info(&pdev->dev, "using msi-x interrupts\n");
  535. return 0;
  536. }
  537. /* fall through for msi */
  538. }
  539. if (use_msi && !pci_enable_msi(pdev)) {
  540. value = msi_tgt_status[adapter->ahw.pci_func];
  541. adapter->flags |= NETXEN_NIC_MSI_ENABLED;
  542. adapter->tgt_status_reg = netxen_get_ioaddr(adapter, value);
  543. adapter->msix_entries[0].vector = pdev->irq;
  544. dev_info(&pdev->dev, "using msi interrupts\n");
  545. return 0;
  546. }
  547. dev_err(&pdev->dev, "Failed to acquire MSI-X/MSI interrupt vector\n");
  548. return -EIO;
  549. }
  550. static int netxen_setup_intr(struct netxen_adapter *adapter)
  551. {
  552. struct pci_dev *pdev = adapter->pdev;
  553. int num_msix;
  554. if (adapter->rss_supported)
  555. num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
  556. MSIX_ENTRIES_PER_ADAPTER : 2;
  557. else
  558. num_msix = 1;
  559. adapter->max_sds_rings = 1;
  560. adapter->flags &= ~(NETXEN_NIC_MSI_ENABLED | NETXEN_NIC_MSIX_ENABLED);
  561. netxen_initialize_interrupt_registers(adapter);
  562. netxen_set_msix_bit(pdev, 0);
  563. if (netxen_function_zero(pdev)) {
  564. if (!netxen_setup_msi_interrupts(adapter, num_msix))
  565. netxen_set_interrupt_mode(adapter, NETXEN_MSI_MODE);
  566. else
  567. netxen_set_interrupt_mode(adapter, NETXEN_INTX_MODE);
  568. } else {
  569. if (netxen_get_interrupt_mode(adapter) == NETXEN_MSI_MODE &&
  570. netxen_setup_msi_interrupts(adapter, num_msix)) {
  571. dev_err(&pdev->dev, "Co-existence of MSI-X/MSI and INTx interrupts is not supported\n");
  572. return -EIO;
  573. }
  574. }
  575. if (!NETXEN_IS_MSI_FAMILY(adapter)) {
  576. adapter->msix_entries[0].vector = pdev->irq;
  577. dev_info(&pdev->dev, "using legacy interrupts\n");
  578. }
  579. return 0;
  580. }
  581. static void
  582. netxen_teardown_intr(struct netxen_adapter *adapter)
  583. {
  584. if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
  585. pci_disable_msix(adapter->pdev);
  586. if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
  587. pci_disable_msi(adapter->pdev);
  588. }
  589. static void
  590. netxen_cleanup_pci_map(struct netxen_adapter *adapter)
  591. {
  592. if (adapter->ahw.db_base != NULL)
  593. iounmap(adapter->ahw.db_base);
  594. if (adapter->ahw.pci_base0 != NULL)
  595. iounmap(adapter->ahw.pci_base0);
  596. if (adapter->ahw.pci_base1 != NULL)
  597. iounmap(adapter->ahw.pci_base1);
  598. if (adapter->ahw.pci_base2 != NULL)
  599. iounmap(adapter->ahw.pci_base2);
  600. }
  601. static int
  602. netxen_setup_pci_map(struct netxen_adapter *adapter)
  603. {
  604. void __iomem *db_ptr = NULL;
  605. resource_size_t mem_base, db_base;
  606. unsigned long mem_len, db_len = 0;
  607. struct pci_dev *pdev = adapter->pdev;
  608. int pci_func = adapter->ahw.pci_func;
  609. struct netxen_hardware_context *ahw = &adapter->ahw;
  610. int err = 0;
  611. /*
  612. * Set the CRB window to invalid. If any register in window 0 is
  613. * accessed it should set the window to 0 and then reset it to 1.
  614. */
  615. adapter->ahw.crb_win = -1;
  616. adapter->ahw.ocm_win = -1;
  617. /* remap phys address */
  618. mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
  619. mem_len = pci_resource_len(pdev, 0);
  620. /* 128 Meg of memory */
  621. if (mem_len == NETXEN_PCI_128MB_SIZE) {
  622. ahw->pci_base0 = ioremap(mem_base, FIRST_PAGE_GROUP_SIZE);
  623. ahw->pci_base1 = ioremap(mem_base + SECOND_PAGE_GROUP_START,
  624. SECOND_PAGE_GROUP_SIZE);
  625. ahw->pci_base2 = ioremap(mem_base + THIRD_PAGE_GROUP_START,
  626. THIRD_PAGE_GROUP_SIZE);
  627. if (ahw->pci_base0 == NULL || ahw->pci_base1 == NULL ||
  628. ahw->pci_base2 == NULL) {
  629. dev_err(&pdev->dev, "failed to map PCI bar 0\n");
  630. err = -EIO;
  631. goto err_out;
  632. }
  633. ahw->pci_len0 = FIRST_PAGE_GROUP_SIZE;
  634. } else if (mem_len == NETXEN_PCI_32MB_SIZE) {
  635. ahw->pci_base1 = ioremap(mem_base, SECOND_PAGE_GROUP_SIZE);
  636. ahw->pci_base2 = ioremap(mem_base + THIRD_PAGE_GROUP_START -
  637. SECOND_PAGE_GROUP_START, THIRD_PAGE_GROUP_SIZE);
  638. if (ahw->pci_base1 == NULL || ahw->pci_base2 == NULL) {
  639. dev_err(&pdev->dev, "failed to map PCI bar 0\n");
  640. err = -EIO;
  641. goto err_out;
  642. }
  643. } else if (mem_len == NETXEN_PCI_2MB_SIZE) {
  644. ahw->pci_base0 = pci_ioremap_bar(pdev, 0);
  645. if (ahw->pci_base0 == NULL) {
  646. dev_err(&pdev->dev, "failed to map PCI bar 0\n");
  647. return -EIO;
  648. }
  649. ahw->pci_len0 = mem_len;
  650. } else {
  651. return -EIO;
  652. }
  653. netxen_setup_hwops(adapter);
  654. dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
  655. if (NX_IS_REVISION_P3P(adapter->ahw.revision_id)) {
  656. adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
  657. NETXEN_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(pci_func)));
  658. } else if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
  659. adapter->ahw.ocm_win_crb = netxen_get_ioaddr(adapter,
  660. NETXEN_PCIX_PS_REG(PCIE_MN_WINDOW_REG(pci_func)));
  661. }
  662. if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
  663. goto skip_doorbell;
  664. db_base = pci_resource_start(pdev, 4); /* doorbell is on bar 4 */
  665. db_len = pci_resource_len(pdev, 4);
  666. if (db_len == 0) {
  667. printk(KERN_ERR "%s: doorbell is disabled\n",
  668. netxen_nic_driver_name);
  669. err = -EIO;
  670. goto err_out;
  671. }
  672. db_ptr = ioremap(db_base, NETXEN_DB_MAPSIZE_BYTES);
  673. if (!db_ptr) {
  674. printk(KERN_ERR "%s: Failed to allocate doorbell map.",
  675. netxen_nic_driver_name);
  676. err = -EIO;
  677. goto err_out;
  678. }
  679. skip_doorbell:
  680. adapter->ahw.db_base = db_ptr;
  681. adapter->ahw.db_len = db_len;
  682. return 0;
  683. err_out:
  684. netxen_cleanup_pci_map(adapter);
  685. return err;
  686. }
  687. static void
  688. netxen_check_options(struct netxen_adapter *adapter)
  689. {
  690. u32 fw_major, fw_minor, fw_build, prev_fw_version;
  691. char brd_name[NETXEN_MAX_SHORT_NAME];
  692. char serial_num[32];
  693. int i, offset, val, err;
  694. __le32 *ptr32;
  695. struct pci_dev *pdev = adapter->pdev;
  696. adapter->driver_mismatch = 0;
  697. ptr32 = (__le32 *)&serial_num;
  698. offset = NX_FW_SERIAL_NUM_OFFSET;
  699. for (i = 0; i < 8; i++) {
  700. err = netxen_rom_fast_read(adapter, offset, &val);
  701. if (err) {
  702. dev_err(&pdev->dev, "error reading board info\n");
  703. adapter->driver_mismatch = 1;
  704. return;
  705. }
  706. ptr32[i] = cpu_to_le32(val);
  707. offset += sizeof(u32);
  708. }
  709. fw_major = NXRD32(adapter, NETXEN_FW_VERSION_MAJOR);
  710. fw_minor = NXRD32(adapter, NETXEN_FW_VERSION_MINOR);
  711. fw_build = NXRD32(adapter, NETXEN_FW_VERSION_SUB);
  712. prev_fw_version = adapter->fw_version;
  713. adapter->fw_version = NETXEN_VERSION_CODE(fw_major, fw_minor, fw_build);
  714. /* Get FW Mini Coredump template and store it */
  715. if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
  716. if (adapter->mdump.md_template == NULL ||
  717. adapter->fw_version > prev_fw_version) {
  718. kfree(adapter->mdump.md_template);
  719. adapter->mdump.md_template = NULL;
  720. err = netxen_setup_minidump(adapter);
  721. if (err)
  722. dev_err(&adapter->pdev->dev,
  723. "Failed to setup minidump rcode = %d\n", err);
  724. }
  725. }
  726. if (adapter->portnum == 0) {
  727. if (netxen_nic_get_brd_name_by_type(adapter->ahw.board_type,
  728. brd_name))
  729. strcpy(serial_num, "Unknown");
  730. pr_info("%s: %s Board S/N %s Chip rev 0x%x\n",
  731. module_name(THIS_MODULE),
  732. brd_name, serial_num, adapter->ahw.revision_id);
  733. }
  734. if (adapter->fw_version < NETXEN_VERSION_CODE(3, 4, 216)) {
  735. adapter->driver_mismatch = 1;
  736. dev_warn(&pdev->dev, "firmware version %d.%d.%d unsupported\n",
  737. fw_major, fw_minor, fw_build);
  738. return;
  739. }
  740. if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
  741. i = NXRD32(adapter, NETXEN_SRE_MISC);
  742. adapter->ahw.cut_through = (i & 0x8000) ? 1 : 0;
  743. }
  744. dev_info(&pdev->dev, "Driver v%s, firmware v%d.%d.%d [%s]\n",
  745. NETXEN_NIC_LINUX_VERSIONID, fw_major, fw_minor, fw_build,
  746. adapter->ahw.cut_through ? "cut-through" : "legacy");
  747. if (adapter->fw_version >= NETXEN_VERSION_CODE(4, 0, 222))
  748. adapter->capabilities = NXRD32(adapter, CRB_FW_CAPABILITIES_1);
  749. if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
  750. adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
  751. adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
  752. } else if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
  753. adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
  754. adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
  755. }
  756. adapter->msix_supported = 0;
  757. if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
  758. adapter->msix_supported = !!use_msi_x;
  759. adapter->rss_supported = !!use_msi_x;
  760. } else {
  761. u32 flashed_ver = 0;
  762. netxen_rom_fast_read(adapter,
  763. NX_FW_VERSION_OFFSET, (int *)&flashed_ver);
  764. flashed_ver = NETXEN_DECODE_VERSION(flashed_ver);
  765. if (flashed_ver >= NETXEN_VERSION_CODE(3, 4, 336)) {
  766. switch (adapter->ahw.board_type) {
  767. case NETXEN_BRDTYPE_P2_SB31_10G:
  768. case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
  769. adapter->msix_supported = !!use_msi_x;
  770. adapter->rss_supported = !!use_msi_x;
  771. break;
  772. default:
  773. break;
  774. }
  775. }
  776. }
  777. adapter->num_txd = MAX_CMD_DESCRIPTORS;
  778. if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  779. adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
  780. adapter->max_rds_rings = 3;
  781. } else {
  782. adapter->num_lro_rxd = 0;
  783. adapter->max_rds_rings = 2;
  784. }
  785. }
  786. static int
  787. netxen_start_firmware(struct netxen_adapter *adapter)
  788. {
  789. int val, err, first_boot;
  790. struct pci_dev *pdev = adapter->pdev;
  791. /* required for NX2031 dummy dma */
  792. err = nx_set_dma_mask(adapter);
  793. if (err)
  794. return err;
  795. err = netxen_can_start_firmware(adapter);
  796. if (err < 0)
  797. return err;
  798. if (!err)
  799. goto wait_init;
  800. first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
  801. err = netxen_check_hw_init(adapter, first_boot);
  802. if (err) {
  803. dev_err(&pdev->dev, "error in init HW init sequence\n");
  804. return err;
  805. }
  806. netxen_request_firmware(adapter);
  807. err = netxen_need_fw_reset(adapter);
  808. if (err < 0)
  809. goto err_out;
  810. if (err == 0)
  811. goto pcie_strap_init;
  812. if (first_boot != 0x55555555) {
  813. NXWR32(adapter, CRB_CMDPEG_STATE, 0);
  814. netxen_pinit_from_rom(adapter);
  815. msleep(1);
  816. }
  817. NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
  818. NXWR32(adapter, NETXEN_PEG_HALT_STATUS1, 0);
  819. NXWR32(adapter, NETXEN_PEG_HALT_STATUS2, 0);
  820. if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
  821. netxen_set_port_mode(adapter);
  822. err = netxen_load_firmware(adapter);
  823. if (err)
  824. goto err_out;
  825. netxen_release_firmware(adapter);
  826. if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  827. /* Initialize multicast addr pool owners */
  828. val = 0x7654;
  829. if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
  830. val |= 0x0f000000;
  831. NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val);
  832. }
  833. err = netxen_init_dummy_dma(adapter);
  834. if (err)
  835. goto err_out;
  836. /*
  837. * Tell the hardware our version number.
  838. */
  839. val = (_NETXEN_NIC_LINUX_MAJOR << 16)
  840. | ((_NETXEN_NIC_LINUX_MINOR << 8))
  841. | (_NETXEN_NIC_LINUX_SUBVERSION);
  842. NXWR32(adapter, CRB_DRIVER_VERSION, val);
  843. pcie_strap_init:
  844. if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
  845. netxen_pcie_strap_init(adapter);
  846. wait_init:
  847. /* Handshake with the card before we register the devices. */
  848. err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
  849. if (err) {
  850. netxen_free_dummy_dma(adapter);
  851. goto err_out;
  852. }
  853. NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_READY);
  854. nx_update_dma_mask(adapter);
  855. netxen_check_options(adapter);
  856. adapter->need_fw_reset = 0;
  857. /* fall through and release firmware */
  858. err_out:
  859. netxen_release_firmware(adapter);
  860. return err;
  861. }
  862. static int
  863. netxen_nic_request_irq(struct netxen_adapter *adapter)
  864. {
  865. irq_handler_t handler;
  866. struct nx_host_sds_ring *sds_ring;
  867. int err, ring;
  868. unsigned long flags = 0;
  869. struct net_device *netdev = adapter->netdev;
  870. struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
  871. if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
  872. handler = netxen_msix_intr;
  873. else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
  874. handler = netxen_msi_intr;
  875. else {
  876. flags |= IRQF_SHARED;
  877. handler = netxen_intr;
  878. }
  879. adapter->irq = netdev->irq;
  880. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  881. sds_ring = &recv_ctx->sds_rings[ring];
  882. sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
  883. err = request_irq(sds_ring->irq, handler,
  884. flags, sds_ring->name, sds_ring);
  885. if (err)
  886. return err;
  887. }
  888. return 0;
  889. }
  890. static void
  891. netxen_nic_free_irq(struct netxen_adapter *adapter)
  892. {
  893. int ring;
  894. struct nx_host_sds_ring *sds_ring;
  895. struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
  896. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  897. sds_ring = &recv_ctx->sds_rings[ring];
  898. free_irq(sds_ring->irq, sds_ring);
  899. }
  900. }
  901. static void
  902. netxen_nic_init_coalesce_defaults(struct netxen_adapter *adapter)
  903. {
  904. adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
  905. adapter->coal.normal.data.rx_time_us =
  906. NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
  907. adapter->coal.normal.data.rx_packets =
  908. NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
  909. adapter->coal.normal.data.tx_time_us =
  910. NETXEN_DEFAULT_INTR_COALESCE_TX_TIME_US;
  911. adapter->coal.normal.data.tx_packets =
  912. NETXEN_DEFAULT_INTR_COALESCE_TX_PACKETS;
  913. }
  914. /* with rtnl_lock */
  915. static int
  916. __netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
  917. {
  918. int err;
  919. if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
  920. return -EIO;
  921. err = adapter->init_port(adapter, adapter->physical_port);
  922. if (err) {
  923. printk(KERN_ERR "%s: Failed to initialize port %d\n",
  924. netxen_nic_driver_name, adapter->portnum);
  925. return err;
  926. }
  927. if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
  928. adapter->macaddr_set(adapter, adapter->mac_addr);
  929. adapter->set_multi(netdev);
  930. adapter->set_mtu(adapter, netdev->mtu);
  931. adapter->ahw.linkup = 0;
  932. if (adapter->max_sds_rings > 1)
  933. netxen_config_rss(adapter, 1);
  934. if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
  935. netxen_config_intr_coalesce(adapter);
  936. if (netdev->features & NETIF_F_LRO)
  937. netxen_config_hw_lro(adapter, NETXEN_NIC_LRO_ENABLED);
  938. netxen_napi_enable(adapter);
  939. if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
  940. netxen_linkevent_request(adapter, 1);
  941. else
  942. netxen_nic_set_link_parameters(adapter);
  943. set_bit(__NX_DEV_UP, &adapter->state);
  944. return 0;
  945. }
  946. /* Usage: During resume and firmware recovery module.*/
  947. static inline int
  948. netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
  949. {
  950. int err = 0;
  951. rtnl_lock();
  952. if (netif_running(netdev))
  953. err = __netxen_nic_up(adapter, netdev);
  954. rtnl_unlock();
  955. return err;
  956. }
  957. /* with rtnl_lock */
  958. static void
  959. __netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
  960. {
  961. if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
  962. return;
  963. if (!test_and_clear_bit(__NX_DEV_UP, &adapter->state))
  964. return;
  965. smp_mb();
  966. netif_carrier_off(netdev);
  967. netif_tx_disable(netdev);
  968. if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
  969. netxen_linkevent_request(adapter, 0);
  970. if (adapter->stop_port)
  971. adapter->stop_port(adapter);
  972. if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
  973. netxen_p3_free_mac_list(adapter);
  974. adapter->set_promisc(adapter, NETXEN_NIU_NON_PROMISC_MODE);
  975. netxen_napi_disable(adapter);
  976. netxen_release_tx_buffers(adapter);
  977. }
  978. /* Usage: During suspend and firmware recovery module */
  979. static inline void
  980. netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
  981. {
  982. rtnl_lock();
  983. if (netif_running(netdev))
  984. __netxen_nic_down(adapter, netdev);
  985. rtnl_unlock();
  986. }
  987. static int
  988. netxen_nic_attach(struct netxen_adapter *adapter)
  989. {
  990. struct net_device *netdev = adapter->netdev;
  991. struct pci_dev *pdev = adapter->pdev;
  992. int err, ring;
  993. struct nx_host_rds_ring *rds_ring;
  994. struct nx_host_tx_ring *tx_ring;
  995. u32 capab2;
  996. if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
  997. return 0;
  998. err = netxen_init_firmware(adapter);
  999. if (err)
  1000. return err;
  1001. adapter->flags &= ~NETXEN_FW_MSS_CAP;
  1002. if (adapter->capabilities & NX_FW_CAPABILITY_MORE_CAPS) {
  1003. capab2 = NXRD32(adapter, CRB_FW_CAPABILITIES_2);
  1004. if (capab2 & NX_FW_CAPABILITY_2_LRO_MAX_TCP_SEG)
  1005. adapter->flags |= NETXEN_FW_MSS_CAP;
  1006. }
  1007. err = netxen_napi_add(adapter, netdev);
  1008. if (err)
  1009. return err;
  1010. err = netxen_alloc_sw_resources(adapter);
  1011. if (err) {
  1012. printk(KERN_ERR "%s: Error in setting sw resources\n",
  1013. netdev->name);
  1014. return err;
  1015. }
  1016. err = netxen_alloc_hw_resources(adapter);
  1017. if (err) {
  1018. printk(KERN_ERR "%s: Error in setting hw resources\n",
  1019. netdev->name);
  1020. goto err_out_free_sw;
  1021. }
  1022. if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  1023. tx_ring = adapter->tx_ring;
  1024. tx_ring->crb_cmd_producer = netxen_get_ioaddr(adapter,
  1025. crb_cmd_producer[adapter->portnum]);
  1026. tx_ring->crb_cmd_consumer = netxen_get_ioaddr(adapter,
  1027. crb_cmd_consumer[adapter->portnum]);
  1028. tx_ring->producer = 0;
  1029. tx_ring->sw_consumer = 0;
  1030. netxen_nic_update_cmd_producer(adapter, tx_ring);
  1031. netxen_nic_update_cmd_consumer(adapter, tx_ring);
  1032. }
  1033. for (ring = 0; ring < adapter->max_rds_rings; ring++) {
  1034. rds_ring = &adapter->recv_ctx.rds_rings[ring];
  1035. netxen_post_rx_buffers(adapter, ring, rds_ring);
  1036. }
  1037. err = netxen_nic_request_irq(adapter);
  1038. if (err) {
  1039. dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
  1040. netdev->name);
  1041. goto err_out_free_rxbuf;
  1042. }
  1043. if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
  1044. netxen_nic_init_coalesce_defaults(adapter);
  1045. netxen_create_sysfs_entries(adapter);
  1046. adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
  1047. return 0;
  1048. err_out_free_rxbuf:
  1049. netxen_release_rx_buffers(adapter);
  1050. netxen_free_hw_resources(adapter);
  1051. err_out_free_sw:
  1052. netxen_free_sw_resources(adapter);
  1053. return err;
  1054. }
  1055. static void
  1056. netxen_nic_detach(struct netxen_adapter *adapter)
  1057. {
  1058. if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
  1059. return;
  1060. netxen_remove_sysfs_entries(adapter);
  1061. netxen_free_hw_resources(adapter);
  1062. netxen_release_rx_buffers(adapter);
  1063. netxen_nic_free_irq(adapter);
  1064. netxen_napi_del(adapter);
  1065. netxen_free_sw_resources(adapter);
  1066. adapter->is_up = 0;
  1067. }
  1068. int
  1069. netxen_nic_reset_context(struct netxen_adapter *adapter)
  1070. {
  1071. int err = 0;
  1072. struct net_device *netdev = adapter->netdev;
  1073. if (test_and_set_bit(__NX_RESETTING, &adapter->state))
  1074. return -EBUSY;
  1075. if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
  1076. netif_device_detach(netdev);
  1077. if (netif_running(netdev))
  1078. __netxen_nic_down(adapter, netdev);
  1079. netxen_nic_detach(adapter);
  1080. if (netif_running(netdev)) {
  1081. err = netxen_nic_attach(adapter);
  1082. if (!err)
  1083. err = __netxen_nic_up(adapter, netdev);
  1084. if (err)
  1085. goto done;
  1086. }
  1087. netif_device_attach(netdev);
  1088. }
  1089. done:
  1090. clear_bit(__NX_RESETTING, &adapter->state);
  1091. return err;
  1092. }
  1093. static int
  1094. netxen_setup_netdev(struct netxen_adapter *adapter,
  1095. struct net_device *netdev)
  1096. {
  1097. int err = 0;
  1098. struct pci_dev *pdev = adapter->pdev;
  1099. adapter->mc_enabled = 0;
  1100. if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
  1101. adapter->max_mc_count = 38;
  1102. else
  1103. adapter->max_mc_count = 16;
  1104. netdev->netdev_ops = &netxen_netdev_ops;
  1105. netdev->watchdog_timeo = 5*HZ;
  1106. netxen_nic_change_mtu(netdev, netdev->mtu);
  1107. netdev->ethtool_ops = &netxen_nic_ethtool_ops;
  1108. netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  1109. NETIF_F_RXCSUM;
  1110. if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
  1111. netdev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
  1112. netdev->vlan_features |= netdev->hw_features;
  1113. if (adapter->pci_using_dac) {
  1114. netdev->features |= NETIF_F_HIGHDMA;
  1115. netdev->vlan_features |= NETIF_F_HIGHDMA;
  1116. }
  1117. if (adapter->capabilities & NX_FW_CAPABILITY_FVLANTX)
  1118. netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
  1119. if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
  1120. netdev->hw_features |= NETIF_F_LRO;
  1121. netdev->features |= netdev->hw_features;
  1122. netdev->irq = adapter->msix_entries[0].vector;
  1123. INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
  1124. if (netxen_read_mac_addr(adapter))
  1125. dev_warn(&pdev->dev, "failed to read mac addr\n");
  1126. netif_carrier_off(netdev);
  1127. err = register_netdev(netdev);
  1128. if (err) {
  1129. dev_err(&pdev->dev, "failed to register net device\n");
  1130. return err;
  1131. }
  1132. return 0;
  1133. }
  1134. #define NETXEN_ULA_ADAPTER_KEY (0xdaddad01)
  1135. #define NETXEN_NON_ULA_ADAPTER_KEY (0xdaddad00)
  1136. static void netxen_read_ula_info(struct netxen_adapter *adapter)
  1137. {
  1138. u32 temp;
  1139. /* Print ULA info only once for an adapter */
  1140. if (adapter->portnum != 0)
  1141. return;
  1142. temp = NXRD32(adapter, NETXEN_ULA_KEY);
  1143. switch (temp) {
  1144. case NETXEN_ULA_ADAPTER_KEY:
  1145. dev_info(&adapter->pdev->dev, "ULA adapter");
  1146. break;
  1147. case NETXEN_NON_ULA_ADAPTER_KEY:
  1148. dev_info(&adapter->pdev->dev, "non ULA adapter");
  1149. break;
  1150. default:
  1151. break;
  1152. }
  1153. return;
  1154. }
  1155. #ifdef CONFIG_PCIEAER
  1156. static void netxen_mask_aer_correctable(struct netxen_adapter *adapter)
  1157. {
  1158. struct pci_dev *pdev = adapter->pdev;
  1159. struct pci_dev *root = pdev->bus->self;
  1160. u32 aer_pos;
  1161. /* root bus? */
  1162. if (!root)
  1163. return;
  1164. if (adapter->ahw.board_type != NETXEN_BRDTYPE_P3_4_GB_MM &&
  1165. adapter->ahw.board_type != NETXEN_BRDTYPE_P3_10G_TP)
  1166. return;
  1167. if (pci_pcie_type(root) != PCI_EXP_TYPE_ROOT_PORT)
  1168. return;
  1169. aer_pos = pci_find_ext_capability(root, PCI_EXT_CAP_ID_ERR);
  1170. if (!aer_pos)
  1171. return;
  1172. pci_write_config_dword(root, aer_pos + PCI_ERR_COR_MASK, 0xffff);
  1173. }
  1174. #endif
  1175. static int
  1176. netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1177. {
  1178. struct net_device *netdev = NULL;
  1179. struct netxen_adapter *adapter = NULL;
  1180. int i = 0, err;
  1181. int pci_func_id = PCI_FUNC(pdev->devfn);
  1182. uint8_t revision_id;
  1183. u32 val;
  1184. if (pdev->revision >= NX_P3_A0 && pdev->revision <= NX_P3_B1) {
  1185. pr_warn("%s: chip revisions between 0x%x-0x%x will not be enabled\n",
  1186. module_name(THIS_MODULE), NX_P3_A0, NX_P3_B1);
  1187. return -ENODEV;
  1188. }
  1189. if ((err = pci_enable_device(pdev)))
  1190. return err;
  1191. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  1192. err = -ENODEV;
  1193. goto err_out_disable_pdev;
  1194. }
  1195. if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
  1196. goto err_out_disable_pdev;
  1197. if (NX_IS_REVISION_P3(pdev->revision))
  1198. pci_enable_pcie_error_reporting(pdev);
  1199. pci_set_master(pdev);
  1200. netdev = alloc_etherdev(sizeof(struct netxen_adapter));
  1201. if(!netdev) {
  1202. err = -ENOMEM;
  1203. goto err_out_free_res;
  1204. }
  1205. SET_NETDEV_DEV(netdev, &pdev->dev);
  1206. adapter = netdev_priv(netdev);
  1207. adapter->netdev = netdev;
  1208. adapter->pdev = pdev;
  1209. adapter->ahw.pci_func = pci_func_id;
  1210. revision_id = pdev->revision;
  1211. adapter->ahw.revision_id = revision_id;
  1212. rwlock_init(&adapter->ahw.crb_lock);
  1213. spin_lock_init(&adapter->ahw.mem_lock);
  1214. spin_lock_init(&adapter->tx_clean_lock);
  1215. INIT_LIST_HEAD(&adapter->mac_list);
  1216. INIT_LIST_HEAD(&adapter->ip_list);
  1217. err = netxen_setup_pci_map(adapter);
  1218. if (err)
  1219. goto err_out_free_netdev;
  1220. /* This will be reset for mezz cards */
  1221. adapter->portnum = pci_func_id;
  1222. err = netxen_nic_get_board_info(adapter);
  1223. if (err) {
  1224. dev_err(&pdev->dev, "Error getting board config info.\n");
  1225. goto err_out_iounmap;
  1226. }
  1227. #ifdef CONFIG_PCIEAER
  1228. netxen_mask_aer_correctable(adapter);
  1229. #endif
  1230. /* Mezz cards have PCI function 0,2,3 enabled */
  1231. switch (adapter->ahw.board_type) {
  1232. case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
  1233. case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
  1234. if (pci_func_id >= 2)
  1235. adapter->portnum = pci_func_id - 2;
  1236. break;
  1237. default:
  1238. break;
  1239. }
  1240. err = netxen_check_flash_fw_compatibility(adapter);
  1241. if (err)
  1242. goto err_out_iounmap;
  1243. if (adapter->portnum == 0) {
  1244. val = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
  1245. if (val != 0xffffffff && val != 0) {
  1246. NXWR32(adapter, NX_CRB_DEV_REF_COUNT, 0);
  1247. adapter->need_fw_reset = 1;
  1248. }
  1249. }
  1250. err = netxen_start_firmware(adapter);
  1251. if (err)
  1252. goto err_out_decr_ref;
  1253. /*
  1254. * See if the firmware gave us a virtual-physical port mapping.
  1255. */
  1256. adapter->physical_port = adapter->portnum;
  1257. if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  1258. i = NXRD32(adapter, CRB_V2P(adapter->portnum));
  1259. if (i != 0x55555555)
  1260. adapter->physical_port = i;
  1261. }
  1262. netxen_nic_clear_stats(adapter);
  1263. err = netxen_setup_intr(adapter);
  1264. if (err) {
  1265. dev_err(&adapter->pdev->dev,
  1266. "Failed to setup interrupts, error = %d\n", err);
  1267. goto err_out_disable_msi;
  1268. }
  1269. netxen_read_ula_info(adapter);
  1270. err = netxen_setup_netdev(adapter, netdev);
  1271. if (err)
  1272. goto err_out_disable_msi;
  1273. pci_set_drvdata(pdev, adapter);
  1274. netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
  1275. switch (adapter->ahw.port_type) {
  1276. case NETXEN_NIC_GBE:
  1277. dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
  1278. adapter->netdev->name);
  1279. break;
  1280. case NETXEN_NIC_XGBE:
  1281. dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
  1282. adapter->netdev->name);
  1283. break;
  1284. }
  1285. netxen_create_diag_entries(adapter);
  1286. return 0;
  1287. err_out_disable_msi:
  1288. netxen_teardown_intr(adapter);
  1289. netxen_free_dummy_dma(adapter);
  1290. err_out_decr_ref:
  1291. nx_decr_dev_ref_cnt(adapter);
  1292. err_out_iounmap:
  1293. netxen_cleanup_pci_map(adapter);
  1294. err_out_free_netdev:
  1295. free_netdev(netdev);
  1296. err_out_free_res:
  1297. pci_release_regions(pdev);
  1298. err_out_disable_pdev:
  1299. pci_disable_device(pdev);
  1300. return err;
  1301. }
  1302. static
  1303. void netxen_cleanup_minidump(struct netxen_adapter *adapter)
  1304. {
  1305. kfree(adapter->mdump.md_template);
  1306. adapter->mdump.md_template = NULL;
  1307. if (adapter->mdump.md_capture_buff) {
  1308. vfree(adapter->mdump.md_capture_buff);
  1309. adapter->mdump.md_capture_buff = NULL;
  1310. }
  1311. }
  1312. static void netxen_nic_remove(struct pci_dev *pdev)
  1313. {
  1314. struct netxen_adapter *adapter;
  1315. struct net_device *netdev;
  1316. adapter = pci_get_drvdata(pdev);
  1317. if (adapter == NULL)
  1318. return;
  1319. netdev = adapter->netdev;
  1320. netxen_cancel_fw_work(adapter);
  1321. unregister_netdev(netdev);
  1322. cancel_work_sync(&adapter->tx_timeout_task);
  1323. netxen_free_ip_list(adapter, false);
  1324. netxen_nic_detach(adapter);
  1325. nx_decr_dev_ref_cnt(adapter);
  1326. if (adapter->portnum == 0)
  1327. netxen_free_dummy_dma(adapter);
  1328. clear_bit(__NX_RESETTING, &adapter->state);
  1329. netxen_teardown_intr(adapter);
  1330. netxen_set_interrupt_mode(adapter, 0);
  1331. netxen_remove_diag_entries(adapter);
  1332. netxen_cleanup_pci_map(adapter);
  1333. netxen_release_firmware(adapter);
  1334. if (NX_IS_REVISION_P3(pdev->revision)) {
  1335. netxen_cleanup_minidump(adapter);
  1336. pci_disable_pcie_error_reporting(pdev);
  1337. }
  1338. pci_release_regions(pdev);
  1339. pci_disable_device(pdev);
  1340. free_netdev(netdev);
  1341. }
  1342. static void netxen_nic_detach_func(struct netxen_adapter *adapter)
  1343. {
  1344. struct net_device *netdev = adapter->netdev;
  1345. netif_device_detach(netdev);
  1346. netxen_cancel_fw_work(adapter);
  1347. if (netif_running(netdev))
  1348. netxen_nic_down(adapter, netdev);
  1349. cancel_work_sync(&adapter->tx_timeout_task);
  1350. netxen_nic_detach(adapter);
  1351. if (adapter->portnum == 0)
  1352. netxen_free_dummy_dma(adapter);
  1353. nx_decr_dev_ref_cnt(adapter);
  1354. clear_bit(__NX_RESETTING, &adapter->state);
  1355. }
  1356. static int netxen_nic_attach_func(struct pci_dev *pdev)
  1357. {
  1358. struct netxen_adapter *adapter = pci_get_drvdata(pdev);
  1359. struct net_device *netdev = adapter->netdev;
  1360. int err;
  1361. err = pci_enable_device(pdev);
  1362. if (err)
  1363. return err;
  1364. pci_set_power_state(pdev, PCI_D0);
  1365. pci_set_master(pdev);
  1366. pci_restore_state(pdev);
  1367. adapter->ahw.crb_win = -1;
  1368. adapter->ahw.ocm_win = -1;
  1369. err = netxen_start_firmware(adapter);
  1370. if (err) {
  1371. dev_err(&pdev->dev, "failed to start firmware\n");
  1372. return err;
  1373. }
  1374. if (netif_running(netdev)) {
  1375. err = netxen_nic_attach(adapter);
  1376. if (err)
  1377. goto err_out;
  1378. err = netxen_nic_up(adapter, netdev);
  1379. if (err)
  1380. goto err_out_detach;
  1381. netxen_restore_indev_addr(netdev, NETDEV_UP);
  1382. }
  1383. netif_device_attach(netdev);
  1384. netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
  1385. return 0;
  1386. err_out_detach:
  1387. netxen_nic_detach(adapter);
  1388. err_out:
  1389. nx_decr_dev_ref_cnt(adapter);
  1390. return err;
  1391. }
  1392. static pci_ers_result_t netxen_io_error_detected(struct pci_dev *pdev,
  1393. pci_channel_state_t state)
  1394. {
  1395. struct netxen_adapter *adapter = pci_get_drvdata(pdev);
  1396. if (state == pci_channel_io_perm_failure)
  1397. return PCI_ERS_RESULT_DISCONNECT;
  1398. if (nx_dev_request_aer(adapter))
  1399. return PCI_ERS_RESULT_RECOVERED;
  1400. netxen_nic_detach_func(adapter);
  1401. pci_disable_device(pdev);
  1402. return PCI_ERS_RESULT_NEED_RESET;
  1403. }
  1404. static pci_ers_result_t netxen_io_slot_reset(struct pci_dev *pdev)
  1405. {
  1406. int err = 0;
  1407. err = netxen_nic_attach_func(pdev);
  1408. return err ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
  1409. }
  1410. static void netxen_io_resume(struct pci_dev *pdev)
  1411. {
  1412. pci_cleanup_aer_uncorrect_error_status(pdev);
  1413. }
  1414. static void netxen_nic_shutdown(struct pci_dev *pdev)
  1415. {
  1416. struct netxen_adapter *adapter = pci_get_drvdata(pdev);
  1417. netxen_nic_detach_func(adapter);
  1418. if (pci_save_state(pdev))
  1419. return;
  1420. if (netxen_nic_wol_supported(adapter)) {
  1421. pci_enable_wake(pdev, PCI_D3cold, 1);
  1422. pci_enable_wake(pdev, PCI_D3hot, 1);
  1423. }
  1424. pci_disable_device(pdev);
  1425. }
  1426. #ifdef CONFIG_PM
  1427. static int
  1428. netxen_nic_suspend(struct pci_dev *pdev, pm_message_t state)
  1429. {
  1430. struct netxen_adapter *adapter = pci_get_drvdata(pdev);
  1431. int retval;
  1432. netxen_nic_detach_func(adapter);
  1433. retval = pci_save_state(pdev);
  1434. if (retval)
  1435. return retval;
  1436. if (netxen_nic_wol_supported(adapter)) {
  1437. pci_enable_wake(pdev, PCI_D3cold, 1);
  1438. pci_enable_wake(pdev, PCI_D3hot, 1);
  1439. }
  1440. pci_disable_device(pdev);
  1441. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1442. return 0;
  1443. }
  1444. static int
  1445. netxen_nic_resume(struct pci_dev *pdev)
  1446. {
  1447. return netxen_nic_attach_func(pdev);
  1448. }
  1449. #endif
  1450. static int netxen_nic_open(struct net_device *netdev)
  1451. {
  1452. struct netxen_adapter *adapter = netdev_priv(netdev);
  1453. int err = 0;
  1454. if (adapter->driver_mismatch)
  1455. return -EIO;
  1456. err = netxen_nic_attach(adapter);
  1457. if (err)
  1458. return err;
  1459. err = __netxen_nic_up(adapter, netdev);
  1460. if (err)
  1461. goto err_out;
  1462. netif_start_queue(netdev);
  1463. return 0;
  1464. err_out:
  1465. netxen_nic_detach(adapter);
  1466. return err;
  1467. }
  1468. /*
  1469. * netxen_nic_close - Disables a network interface entry point
  1470. */
  1471. static int netxen_nic_close(struct net_device *netdev)
  1472. {
  1473. struct netxen_adapter *adapter = netdev_priv(netdev);
  1474. __netxen_nic_down(adapter, netdev);
  1475. return 0;
  1476. }
  1477. static void
  1478. netxen_tso_check(struct net_device *netdev,
  1479. struct nx_host_tx_ring *tx_ring,
  1480. struct cmd_desc_type0 *first_desc,
  1481. struct sk_buff *skb)
  1482. {
  1483. u8 opcode = TX_ETHER_PKT;
  1484. __be16 protocol = skb->protocol;
  1485. u16 flags = 0, vid = 0;
  1486. u32 producer;
  1487. int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
  1488. struct cmd_desc_type0 *hwdesc;
  1489. struct vlan_ethhdr *vh;
  1490. if (protocol == cpu_to_be16(ETH_P_8021Q)) {
  1491. vh = (struct vlan_ethhdr *)skb->data;
  1492. protocol = vh->h_vlan_encapsulated_proto;
  1493. flags = FLAGS_VLAN_TAGGED;
  1494. } else if (skb_vlan_tag_present(skb)) {
  1495. flags = FLAGS_VLAN_OOB;
  1496. vid = skb_vlan_tag_get(skb);
  1497. netxen_set_tx_vlan_tci(first_desc, vid);
  1498. vlan_oob = 1;
  1499. }
  1500. if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
  1501. skb_shinfo(skb)->gso_size > 0) {
  1502. hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1503. first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
  1504. first_desc->total_hdr_length = hdr_len;
  1505. if (vlan_oob) {
  1506. first_desc->total_hdr_length += VLAN_HLEN;
  1507. first_desc->tcp_hdr_offset = VLAN_HLEN;
  1508. first_desc->ip_hdr_offset = VLAN_HLEN;
  1509. /* Only in case of TSO on vlan device */
  1510. flags |= FLAGS_VLAN_TAGGED;
  1511. }
  1512. opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
  1513. TX_TCP_LSO6 : TX_TCP_LSO;
  1514. tso = 1;
  1515. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1516. u8 l4proto;
  1517. if (protocol == cpu_to_be16(ETH_P_IP)) {
  1518. l4proto = ip_hdr(skb)->protocol;
  1519. if (l4proto == IPPROTO_TCP)
  1520. opcode = TX_TCP_PKT;
  1521. else if(l4proto == IPPROTO_UDP)
  1522. opcode = TX_UDP_PKT;
  1523. } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
  1524. l4proto = ipv6_hdr(skb)->nexthdr;
  1525. if (l4proto == IPPROTO_TCP)
  1526. opcode = TX_TCPV6_PKT;
  1527. else if(l4proto == IPPROTO_UDP)
  1528. opcode = TX_UDPV6_PKT;
  1529. }
  1530. }
  1531. first_desc->tcp_hdr_offset += skb_transport_offset(skb);
  1532. first_desc->ip_hdr_offset += skb_network_offset(skb);
  1533. netxen_set_tx_flags_opcode(first_desc, flags, opcode);
  1534. if (!tso)
  1535. return;
  1536. /* For LSO, we need to copy the MAC/IP/TCP headers into
  1537. * the descriptor ring
  1538. */
  1539. producer = tx_ring->producer;
  1540. copied = 0;
  1541. offset = 2;
  1542. if (vlan_oob) {
  1543. /* Create a TSO vlan header template for firmware */
  1544. hwdesc = &tx_ring->desc_head[producer];
  1545. tx_ring->cmd_buf_arr[producer].skb = NULL;
  1546. copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
  1547. hdr_len + VLAN_HLEN);
  1548. vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
  1549. skb_copy_from_linear_data(skb, vh, 12);
  1550. vh->h_vlan_proto = htons(ETH_P_8021Q);
  1551. vh->h_vlan_TCI = htons(vid);
  1552. skb_copy_from_linear_data_offset(skb, 12,
  1553. (char *)vh + 16, copy_len - 16);
  1554. copied = copy_len - VLAN_HLEN;
  1555. offset = 0;
  1556. producer = get_next_index(producer, tx_ring->num_desc);
  1557. }
  1558. while (copied < hdr_len) {
  1559. copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
  1560. (hdr_len - copied));
  1561. hwdesc = &tx_ring->desc_head[producer];
  1562. tx_ring->cmd_buf_arr[producer].skb = NULL;
  1563. skb_copy_from_linear_data_offset(skb, copied,
  1564. (char *)hwdesc + offset, copy_len);
  1565. copied += copy_len;
  1566. offset = 0;
  1567. producer = get_next_index(producer, tx_ring->num_desc);
  1568. }
  1569. tx_ring->producer = producer;
  1570. barrier();
  1571. }
  1572. static int
  1573. netxen_map_tx_skb(struct pci_dev *pdev,
  1574. struct sk_buff *skb, struct netxen_cmd_buffer *pbuf)
  1575. {
  1576. struct netxen_skb_frag *nf;
  1577. struct skb_frag_struct *frag;
  1578. int i, nr_frags;
  1579. dma_addr_t map;
  1580. nr_frags = skb_shinfo(skb)->nr_frags;
  1581. nf = &pbuf->frag_array[0];
  1582. map = pci_map_single(pdev, skb->data,
  1583. skb_headlen(skb), PCI_DMA_TODEVICE);
  1584. if (pci_dma_mapping_error(pdev, map))
  1585. goto out_err;
  1586. nf->dma = map;
  1587. nf->length = skb_headlen(skb);
  1588. for (i = 0; i < nr_frags; i++) {
  1589. frag = &skb_shinfo(skb)->frags[i];
  1590. nf = &pbuf->frag_array[i+1];
  1591. map = skb_frag_dma_map(&pdev->dev, frag, 0, skb_frag_size(frag),
  1592. DMA_TO_DEVICE);
  1593. if (dma_mapping_error(&pdev->dev, map))
  1594. goto unwind;
  1595. nf->dma = map;
  1596. nf->length = skb_frag_size(frag);
  1597. }
  1598. return 0;
  1599. unwind:
  1600. while (--i >= 0) {
  1601. nf = &pbuf->frag_array[i+1];
  1602. pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
  1603. nf->dma = 0ULL;
  1604. }
  1605. nf = &pbuf->frag_array[0];
  1606. pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
  1607. nf->dma = 0ULL;
  1608. out_err:
  1609. return -ENOMEM;
  1610. }
  1611. static inline void
  1612. netxen_clear_cmddesc(u64 *desc)
  1613. {
  1614. desc[0] = 0ULL;
  1615. desc[2] = 0ULL;
  1616. }
  1617. static netdev_tx_t
  1618. netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  1619. {
  1620. struct netxen_adapter *adapter = netdev_priv(netdev);
  1621. struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
  1622. struct netxen_cmd_buffer *pbuf;
  1623. struct netxen_skb_frag *buffrag;
  1624. struct cmd_desc_type0 *hwdesc, *first_desc;
  1625. struct pci_dev *pdev;
  1626. int i, k;
  1627. int delta = 0;
  1628. struct skb_frag_struct *frag;
  1629. u32 producer;
  1630. int frag_count, no_of_desc;
  1631. u32 num_txd = tx_ring->num_desc;
  1632. frag_count = skb_shinfo(skb)->nr_frags + 1;
  1633. /* 14 frags supported for normal packet and
  1634. * 32 frags supported for TSO packet
  1635. */
  1636. if (!skb_is_gso(skb) && frag_count > NETXEN_MAX_FRAGS_PER_TX) {
  1637. for (i = 0; i < (frag_count - NETXEN_MAX_FRAGS_PER_TX); i++) {
  1638. frag = &skb_shinfo(skb)->frags[i];
  1639. delta += skb_frag_size(frag);
  1640. }
  1641. if (!__pskb_pull_tail(skb, delta))
  1642. goto drop_packet;
  1643. frag_count = 1 + skb_shinfo(skb)->nr_frags;
  1644. }
  1645. /* 4 fragments per cmd des */
  1646. no_of_desc = (frag_count + 3) >> 2;
  1647. if (unlikely(netxen_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
  1648. netif_stop_queue(netdev);
  1649. smp_mb();
  1650. if (netxen_tx_avail(tx_ring) > TX_STOP_THRESH)
  1651. netif_start_queue(netdev);
  1652. else
  1653. return NETDEV_TX_BUSY;
  1654. }
  1655. producer = tx_ring->producer;
  1656. pbuf = &tx_ring->cmd_buf_arr[producer];
  1657. pdev = adapter->pdev;
  1658. if (netxen_map_tx_skb(pdev, skb, pbuf))
  1659. goto drop_packet;
  1660. pbuf->skb = skb;
  1661. pbuf->frag_count = frag_count;
  1662. first_desc = hwdesc = &tx_ring->desc_head[producer];
  1663. netxen_clear_cmddesc((u64 *)hwdesc);
  1664. netxen_set_tx_frags_len(first_desc, frag_count, skb->len);
  1665. netxen_set_tx_port(first_desc, adapter->portnum);
  1666. for (i = 0; i < frag_count; i++) {
  1667. k = i % 4;
  1668. if ((k == 0) && (i > 0)) {
  1669. /* move to next desc.*/
  1670. producer = get_next_index(producer, num_txd);
  1671. hwdesc = &tx_ring->desc_head[producer];
  1672. netxen_clear_cmddesc((u64 *)hwdesc);
  1673. tx_ring->cmd_buf_arr[producer].skb = NULL;
  1674. }
  1675. buffrag = &pbuf->frag_array[i];
  1676. hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
  1677. switch (k) {
  1678. case 0:
  1679. hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
  1680. break;
  1681. case 1:
  1682. hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
  1683. break;
  1684. case 2:
  1685. hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
  1686. break;
  1687. case 3:
  1688. hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
  1689. break;
  1690. }
  1691. }
  1692. tx_ring->producer = get_next_index(producer, num_txd);
  1693. netxen_tso_check(netdev, tx_ring, first_desc, skb);
  1694. adapter->stats.txbytes += skb->len;
  1695. adapter->stats.xmitcalled++;
  1696. netxen_nic_update_cmd_producer(adapter, tx_ring);
  1697. return NETDEV_TX_OK;
  1698. drop_packet:
  1699. adapter->stats.txdropped++;
  1700. dev_kfree_skb_any(skb);
  1701. return NETDEV_TX_OK;
  1702. }
  1703. static int netxen_nic_check_temp(struct netxen_adapter *adapter)
  1704. {
  1705. struct net_device *netdev = adapter->netdev;
  1706. uint32_t temp, temp_state, temp_val;
  1707. int rv = 0;
  1708. temp = NXRD32(adapter, CRB_TEMP_STATE);
  1709. temp_state = nx_get_temp_state(temp);
  1710. temp_val = nx_get_temp_val(temp);
  1711. if (temp_state == NX_TEMP_PANIC) {
  1712. printk(KERN_ALERT
  1713. "%s: Device temperature %d degrees C exceeds"
  1714. " maximum allowed. Hardware has been shut down.\n",
  1715. netdev->name, temp_val);
  1716. rv = 1;
  1717. } else if (temp_state == NX_TEMP_WARN) {
  1718. if (adapter->temp == NX_TEMP_NORMAL) {
  1719. printk(KERN_ALERT
  1720. "%s: Device temperature %d degrees C "
  1721. "exceeds operating range."
  1722. " Immediate action needed.\n",
  1723. netdev->name, temp_val);
  1724. }
  1725. } else {
  1726. if (adapter->temp == NX_TEMP_WARN) {
  1727. printk(KERN_INFO
  1728. "%s: Device temperature is now %d degrees C"
  1729. " in normal range.\n", netdev->name,
  1730. temp_val);
  1731. }
  1732. }
  1733. adapter->temp = temp_state;
  1734. return rv;
  1735. }
  1736. void netxen_advert_link_change(struct netxen_adapter *adapter, int linkup)
  1737. {
  1738. struct net_device *netdev = adapter->netdev;
  1739. if (adapter->ahw.linkup && !linkup) {
  1740. printk(KERN_INFO "%s: %s NIC Link is down\n",
  1741. netxen_nic_driver_name, netdev->name);
  1742. adapter->ahw.linkup = 0;
  1743. if (netif_running(netdev)) {
  1744. netif_carrier_off(netdev);
  1745. netif_stop_queue(netdev);
  1746. }
  1747. adapter->link_changed = !adapter->has_link_events;
  1748. } else if (!adapter->ahw.linkup && linkup) {
  1749. printk(KERN_INFO "%s: %s NIC Link is up\n",
  1750. netxen_nic_driver_name, netdev->name);
  1751. adapter->ahw.linkup = 1;
  1752. if (netif_running(netdev)) {
  1753. netif_carrier_on(netdev);
  1754. netif_wake_queue(netdev);
  1755. }
  1756. adapter->link_changed = !adapter->has_link_events;
  1757. }
  1758. }
  1759. static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
  1760. {
  1761. u32 val, port, linkup;
  1762. port = adapter->physical_port;
  1763. if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
  1764. val = NXRD32(adapter, CRB_XG_STATE_P3);
  1765. val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
  1766. linkup = (val == XG_LINK_UP_P3);
  1767. } else {
  1768. val = NXRD32(adapter, CRB_XG_STATE);
  1769. val = (val >> port*8) & 0xff;
  1770. linkup = (val == XG_LINK_UP);
  1771. }
  1772. netxen_advert_link_change(adapter, linkup);
  1773. }
  1774. static void netxen_tx_timeout(struct net_device *netdev)
  1775. {
  1776. struct netxen_adapter *adapter = netdev_priv(netdev);
  1777. if (test_bit(__NX_RESETTING, &adapter->state))
  1778. return;
  1779. dev_err(&netdev->dev, "transmit timeout, resetting.\n");
  1780. schedule_work(&adapter->tx_timeout_task);
  1781. }
  1782. static void netxen_tx_timeout_task(struct work_struct *work)
  1783. {
  1784. struct netxen_adapter *adapter =
  1785. container_of(work, struct netxen_adapter, tx_timeout_task);
  1786. if (!netif_running(adapter->netdev))
  1787. return;
  1788. if (test_and_set_bit(__NX_RESETTING, &adapter->state))
  1789. return;
  1790. if (++adapter->tx_timeo_cnt >= NX_MAX_TX_TIMEOUTS)
  1791. goto request_reset;
  1792. rtnl_lock();
  1793. if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  1794. /* try to scrub interrupt */
  1795. netxen_napi_disable(adapter);
  1796. netxen_napi_enable(adapter);
  1797. netif_wake_queue(adapter->netdev);
  1798. clear_bit(__NX_RESETTING, &adapter->state);
  1799. } else {
  1800. clear_bit(__NX_RESETTING, &adapter->state);
  1801. if (netxen_nic_reset_context(adapter)) {
  1802. rtnl_unlock();
  1803. goto request_reset;
  1804. }
  1805. }
  1806. netif_trans_update(adapter->netdev);
  1807. rtnl_unlock();
  1808. return;
  1809. request_reset:
  1810. adapter->need_fw_reset = 1;
  1811. clear_bit(__NX_RESETTING, &adapter->state);
  1812. }
  1813. static struct rtnl_link_stats64 *netxen_nic_get_stats(struct net_device *netdev,
  1814. struct rtnl_link_stats64 *stats)
  1815. {
  1816. struct netxen_adapter *adapter = netdev_priv(netdev);
  1817. stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
  1818. stats->tx_packets = adapter->stats.xmitfinished;
  1819. stats->rx_bytes = adapter->stats.rxbytes;
  1820. stats->tx_bytes = adapter->stats.txbytes;
  1821. stats->rx_dropped = adapter->stats.rxdropped;
  1822. stats->tx_dropped = adapter->stats.txdropped;
  1823. return stats;
  1824. }
  1825. static irqreturn_t netxen_intr(int irq, void *data)
  1826. {
  1827. struct nx_host_sds_ring *sds_ring = data;
  1828. struct netxen_adapter *adapter = sds_ring->adapter;
  1829. u32 status = 0;
  1830. status = readl(adapter->isr_int_vec);
  1831. if (!(status & adapter->int_vec_bit))
  1832. return IRQ_NONE;
  1833. if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
  1834. /* check interrupt state machine, to be sure */
  1835. status = readl(adapter->crb_int_state_reg);
  1836. if (!ISR_LEGACY_INT_TRIGGERED(status))
  1837. return IRQ_NONE;
  1838. } else {
  1839. unsigned long our_int = 0;
  1840. our_int = readl(adapter->crb_int_state_reg);
  1841. /* not our interrupt */
  1842. if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
  1843. return IRQ_NONE;
  1844. /* claim interrupt */
  1845. writel((our_int & 0xffffffff), adapter->crb_int_state_reg);
  1846. /* clear interrupt */
  1847. netxen_nic_disable_int(sds_ring);
  1848. }
  1849. writel(0xffffffff, adapter->tgt_status_reg);
  1850. /* read twice to ensure write is flushed */
  1851. readl(adapter->isr_int_vec);
  1852. readl(adapter->isr_int_vec);
  1853. napi_schedule(&sds_ring->napi);
  1854. return IRQ_HANDLED;
  1855. }
  1856. static irqreturn_t netxen_msi_intr(int irq, void *data)
  1857. {
  1858. struct nx_host_sds_ring *sds_ring = data;
  1859. struct netxen_adapter *adapter = sds_ring->adapter;
  1860. /* clear interrupt */
  1861. writel(0xffffffff, adapter->tgt_status_reg);
  1862. napi_schedule(&sds_ring->napi);
  1863. return IRQ_HANDLED;
  1864. }
  1865. static irqreturn_t netxen_msix_intr(int irq, void *data)
  1866. {
  1867. struct nx_host_sds_ring *sds_ring = data;
  1868. napi_schedule(&sds_ring->napi);
  1869. return IRQ_HANDLED;
  1870. }
  1871. static int netxen_nic_poll(struct napi_struct *napi, int budget)
  1872. {
  1873. struct nx_host_sds_ring *sds_ring =
  1874. container_of(napi, struct nx_host_sds_ring, napi);
  1875. struct netxen_adapter *adapter = sds_ring->adapter;
  1876. int tx_complete;
  1877. int work_done;
  1878. tx_complete = netxen_process_cmd_ring(adapter);
  1879. work_done = netxen_process_rcv_ring(sds_ring, budget);
  1880. if (!tx_complete)
  1881. work_done = budget;
  1882. if (work_done < budget) {
  1883. napi_complete(&sds_ring->napi);
  1884. if (test_bit(__NX_DEV_UP, &adapter->state))
  1885. netxen_nic_enable_int(sds_ring);
  1886. }
  1887. return work_done;
  1888. }
  1889. #ifdef CONFIG_NET_POLL_CONTROLLER
  1890. static void netxen_nic_poll_controller(struct net_device *netdev)
  1891. {
  1892. int ring;
  1893. struct nx_host_sds_ring *sds_ring;
  1894. struct netxen_adapter *adapter = netdev_priv(netdev);
  1895. struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
  1896. disable_irq(adapter->irq);
  1897. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  1898. sds_ring = &recv_ctx->sds_rings[ring];
  1899. netxen_intr(adapter->irq, sds_ring);
  1900. }
  1901. enable_irq(adapter->irq);
  1902. }
  1903. #endif
  1904. static int
  1905. nx_incr_dev_ref_cnt(struct netxen_adapter *adapter)
  1906. {
  1907. int count;
  1908. if (netxen_api_lock(adapter))
  1909. return -EIO;
  1910. count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
  1911. NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
  1912. netxen_api_unlock(adapter);
  1913. return count;
  1914. }
  1915. static int
  1916. nx_decr_dev_ref_cnt(struct netxen_adapter *adapter)
  1917. {
  1918. int count, state;
  1919. if (netxen_api_lock(adapter))
  1920. return -EIO;
  1921. count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
  1922. WARN_ON(count == 0);
  1923. NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
  1924. state = NXRD32(adapter, NX_CRB_DEV_STATE);
  1925. if (count == 0 && state != NX_DEV_FAILED)
  1926. NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
  1927. netxen_api_unlock(adapter);
  1928. return count;
  1929. }
  1930. static int
  1931. nx_dev_request_aer(struct netxen_adapter *adapter)
  1932. {
  1933. u32 state;
  1934. int ret = -EINVAL;
  1935. if (netxen_api_lock(adapter))
  1936. return ret;
  1937. state = NXRD32(adapter, NX_CRB_DEV_STATE);
  1938. if (state == NX_DEV_NEED_AER)
  1939. ret = 0;
  1940. else if (state == NX_DEV_READY) {
  1941. NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_AER);
  1942. ret = 0;
  1943. }
  1944. netxen_api_unlock(adapter);
  1945. return ret;
  1946. }
  1947. int
  1948. nx_dev_request_reset(struct netxen_adapter *adapter)
  1949. {
  1950. u32 state;
  1951. int ret = -EINVAL;
  1952. if (netxen_api_lock(adapter))
  1953. return ret;
  1954. state = NXRD32(adapter, NX_CRB_DEV_STATE);
  1955. if (state == NX_DEV_NEED_RESET || state == NX_DEV_FAILED)
  1956. ret = 0;
  1957. else if (state != NX_DEV_INITALIZING && state != NX_DEV_NEED_AER) {
  1958. NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_RESET);
  1959. adapter->flags |= NETXEN_FW_RESET_OWNER;
  1960. ret = 0;
  1961. }
  1962. netxen_api_unlock(adapter);
  1963. return ret;
  1964. }
  1965. static int
  1966. netxen_can_start_firmware(struct netxen_adapter *adapter)
  1967. {
  1968. int count;
  1969. int can_start = 0;
  1970. if (netxen_api_lock(adapter)) {
  1971. nx_incr_dev_ref_cnt(adapter);
  1972. return -1;
  1973. }
  1974. count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
  1975. if ((count < 0) || (count >= NX_MAX_PCI_FUNC))
  1976. count = 0;
  1977. if (count == 0) {
  1978. can_start = 1;
  1979. NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_INITALIZING);
  1980. }
  1981. NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
  1982. netxen_api_unlock(adapter);
  1983. return can_start;
  1984. }
  1985. static void
  1986. netxen_schedule_work(struct netxen_adapter *adapter,
  1987. work_func_t func, int delay)
  1988. {
  1989. INIT_DELAYED_WORK(&adapter->fw_work, func);
  1990. schedule_delayed_work(&adapter->fw_work, delay);
  1991. }
  1992. static void
  1993. netxen_cancel_fw_work(struct netxen_adapter *adapter)
  1994. {
  1995. while (test_and_set_bit(__NX_RESETTING, &adapter->state))
  1996. msleep(10);
  1997. cancel_delayed_work_sync(&adapter->fw_work);
  1998. }
  1999. static void
  2000. netxen_attach_work(struct work_struct *work)
  2001. {
  2002. struct netxen_adapter *adapter = container_of(work,
  2003. struct netxen_adapter, fw_work.work);
  2004. struct net_device *netdev = adapter->netdev;
  2005. int err = 0;
  2006. if (netif_running(netdev)) {
  2007. err = netxen_nic_attach(adapter);
  2008. if (err)
  2009. goto done;
  2010. err = netxen_nic_up(adapter, netdev);
  2011. if (err) {
  2012. netxen_nic_detach(adapter);
  2013. goto done;
  2014. }
  2015. netxen_restore_indev_addr(netdev, NETDEV_UP);
  2016. }
  2017. netif_device_attach(netdev);
  2018. done:
  2019. adapter->fw_fail_cnt = 0;
  2020. clear_bit(__NX_RESETTING, &adapter->state);
  2021. netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
  2022. }
  2023. static void
  2024. netxen_fwinit_work(struct work_struct *work)
  2025. {
  2026. struct netxen_adapter *adapter = container_of(work,
  2027. struct netxen_adapter, fw_work.work);
  2028. int dev_state;
  2029. int count;
  2030. dev_state = NXRD32(adapter, NX_CRB_DEV_STATE);
  2031. if (adapter->flags & NETXEN_FW_RESET_OWNER) {
  2032. count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
  2033. WARN_ON(count == 0);
  2034. if (count == 1) {
  2035. if (adapter->mdump.md_enabled) {
  2036. rtnl_lock();
  2037. netxen_dump_fw(adapter);
  2038. rtnl_unlock();
  2039. }
  2040. adapter->flags &= ~NETXEN_FW_RESET_OWNER;
  2041. if (netxen_api_lock(adapter)) {
  2042. clear_bit(__NX_RESETTING, &adapter->state);
  2043. NXWR32(adapter, NX_CRB_DEV_STATE,
  2044. NX_DEV_FAILED);
  2045. return;
  2046. }
  2047. count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
  2048. NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
  2049. NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
  2050. dev_state = NX_DEV_COLD;
  2051. netxen_api_unlock(adapter);
  2052. }
  2053. }
  2054. switch (dev_state) {
  2055. case NX_DEV_COLD:
  2056. case NX_DEV_READY:
  2057. if (!netxen_start_firmware(adapter)) {
  2058. netxen_schedule_work(adapter, netxen_attach_work, 0);
  2059. return;
  2060. }
  2061. break;
  2062. case NX_DEV_NEED_RESET:
  2063. case NX_DEV_INITALIZING:
  2064. netxen_schedule_work(adapter,
  2065. netxen_fwinit_work, 2 * FW_POLL_DELAY);
  2066. return;
  2067. case NX_DEV_FAILED:
  2068. default:
  2069. nx_incr_dev_ref_cnt(adapter);
  2070. break;
  2071. }
  2072. if (netxen_api_lock(adapter)) {
  2073. clear_bit(__NX_RESETTING, &adapter->state);
  2074. return;
  2075. }
  2076. NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_FAILED);
  2077. netxen_api_unlock(adapter);
  2078. dev_err(&adapter->pdev->dev, "%s: Device initialization Failed\n",
  2079. adapter->netdev->name);
  2080. clear_bit(__NX_RESETTING, &adapter->state);
  2081. }
  2082. static void
  2083. netxen_detach_work(struct work_struct *work)
  2084. {
  2085. struct netxen_adapter *adapter = container_of(work,
  2086. struct netxen_adapter, fw_work.work);
  2087. struct net_device *netdev = adapter->netdev;
  2088. int ref_cnt = 0, delay;
  2089. u32 status;
  2090. netif_device_detach(netdev);
  2091. netxen_nic_down(adapter, netdev);
  2092. rtnl_lock();
  2093. netxen_nic_detach(adapter);
  2094. rtnl_unlock();
  2095. status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
  2096. if (status & NX_RCODE_FATAL_ERROR)
  2097. goto err_ret;
  2098. if (adapter->temp == NX_TEMP_PANIC)
  2099. goto err_ret;
  2100. if (!(adapter->flags & NETXEN_FW_RESET_OWNER))
  2101. ref_cnt = nx_decr_dev_ref_cnt(adapter);
  2102. if (ref_cnt == -EIO)
  2103. goto err_ret;
  2104. delay = (ref_cnt == 0) ? 0 : (2 * FW_POLL_DELAY);
  2105. adapter->fw_wait_cnt = 0;
  2106. netxen_schedule_work(adapter, netxen_fwinit_work, delay);
  2107. return;
  2108. err_ret:
  2109. clear_bit(__NX_RESETTING, &adapter->state);
  2110. }
  2111. static int
  2112. netxen_check_health(struct netxen_adapter *adapter)
  2113. {
  2114. u32 state, heartbit;
  2115. u32 peg_status;
  2116. struct net_device *netdev = adapter->netdev;
  2117. state = NXRD32(adapter, NX_CRB_DEV_STATE);
  2118. if (state == NX_DEV_NEED_AER)
  2119. return 0;
  2120. if (netxen_nic_check_temp(adapter))
  2121. goto detach;
  2122. if (adapter->need_fw_reset) {
  2123. if (nx_dev_request_reset(adapter))
  2124. return 0;
  2125. goto detach;
  2126. }
  2127. /* NX_DEV_NEED_RESET, this state can be marked in two cases
  2128. * 1. Tx timeout 2. Fw hang
  2129. * Send request to destroy context in case of tx timeout only
  2130. * and doesn't required in case of Fw hang
  2131. */
  2132. if (state == NX_DEV_NEED_RESET || state == NX_DEV_FAILED) {
  2133. adapter->need_fw_reset = 1;
  2134. if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
  2135. goto detach;
  2136. }
  2137. if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
  2138. return 0;
  2139. heartbit = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
  2140. if (heartbit != adapter->heartbit) {
  2141. adapter->heartbit = heartbit;
  2142. adapter->fw_fail_cnt = 0;
  2143. if (adapter->need_fw_reset)
  2144. goto detach;
  2145. return 0;
  2146. }
  2147. if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
  2148. return 0;
  2149. if (nx_dev_request_reset(adapter))
  2150. return 0;
  2151. clear_bit(__NX_FW_ATTACHED, &adapter->state);
  2152. dev_err(&netdev->dev, "firmware hang detected\n");
  2153. peg_status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
  2154. dev_err(&adapter->pdev->dev, "Dumping hw/fw registers\n"
  2155. "PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,\n"
  2156. "PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,\n"
  2157. "PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,\n"
  2158. "PEG_NET_4_PC: 0x%x\n",
  2159. peg_status,
  2160. NXRD32(adapter, NETXEN_PEG_HALT_STATUS2),
  2161. NXRD32(adapter, NETXEN_CRB_PEG_NET_0 + 0x3c),
  2162. NXRD32(adapter, NETXEN_CRB_PEG_NET_1 + 0x3c),
  2163. NXRD32(adapter, NETXEN_CRB_PEG_NET_2 + 0x3c),
  2164. NXRD32(adapter, NETXEN_CRB_PEG_NET_3 + 0x3c),
  2165. NXRD32(adapter, NETXEN_CRB_PEG_NET_4 + 0x3c));
  2166. if (NX_FWERROR_PEGSTAT1(peg_status) == 0x67)
  2167. dev_err(&adapter->pdev->dev,
  2168. "Firmware aborted with error code 0x00006700. "
  2169. "Device is being reset.\n");
  2170. detach:
  2171. if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) &&
  2172. !test_and_set_bit(__NX_RESETTING, &adapter->state))
  2173. netxen_schedule_work(adapter, netxen_detach_work, 0);
  2174. return 1;
  2175. }
  2176. static void
  2177. netxen_fw_poll_work(struct work_struct *work)
  2178. {
  2179. struct netxen_adapter *adapter = container_of(work,
  2180. struct netxen_adapter, fw_work.work);
  2181. if (test_bit(__NX_RESETTING, &adapter->state))
  2182. goto reschedule;
  2183. if (test_bit(__NX_DEV_UP, &adapter->state) &&
  2184. !(adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)) {
  2185. if (!adapter->has_link_events) {
  2186. netxen_nic_handle_phy_intr(adapter);
  2187. if (adapter->link_changed)
  2188. netxen_nic_set_link_parameters(adapter);
  2189. }
  2190. }
  2191. if (netxen_check_health(adapter))
  2192. return;
  2193. reschedule:
  2194. netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
  2195. }
  2196. static ssize_t
  2197. netxen_store_bridged_mode(struct device *dev,
  2198. struct device_attribute *attr, const char *buf, size_t len)
  2199. {
  2200. struct net_device *net = to_net_dev(dev);
  2201. struct netxen_adapter *adapter = netdev_priv(net);
  2202. unsigned long new;
  2203. int ret = -EINVAL;
  2204. if (!(adapter->capabilities & NX_FW_CAPABILITY_BDG))
  2205. goto err_out;
  2206. if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
  2207. goto err_out;
  2208. if (kstrtoul(buf, 2, &new))
  2209. goto err_out;
  2210. if (!netxen_config_bridged_mode(adapter, !!new))
  2211. ret = len;
  2212. err_out:
  2213. return ret;
  2214. }
  2215. static ssize_t
  2216. netxen_show_bridged_mode(struct device *dev,
  2217. struct device_attribute *attr, char *buf)
  2218. {
  2219. struct net_device *net = to_net_dev(dev);
  2220. struct netxen_adapter *adapter;
  2221. int bridged_mode = 0;
  2222. adapter = netdev_priv(net);
  2223. if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
  2224. bridged_mode = !!(adapter->flags & NETXEN_NIC_BRIDGE_ENABLED);
  2225. return sprintf(buf, "%d\n", bridged_mode);
  2226. }
  2227. static struct device_attribute dev_attr_bridged_mode = {
  2228. .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
  2229. .show = netxen_show_bridged_mode,
  2230. .store = netxen_store_bridged_mode,
  2231. };
  2232. static ssize_t
  2233. netxen_store_diag_mode(struct device *dev,
  2234. struct device_attribute *attr, const char *buf, size_t len)
  2235. {
  2236. struct netxen_adapter *adapter = dev_get_drvdata(dev);
  2237. unsigned long new;
  2238. if (kstrtoul(buf, 2, &new))
  2239. return -EINVAL;
  2240. if (!!new != !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
  2241. adapter->flags ^= NETXEN_NIC_DIAG_ENABLED;
  2242. return len;
  2243. }
  2244. static ssize_t
  2245. netxen_show_diag_mode(struct device *dev,
  2246. struct device_attribute *attr, char *buf)
  2247. {
  2248. struct netxen_adapter *adapter = dev_get_drvdata(dev);
  2249. return sprintf(buf, "%d\n",
  2250. !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED));
  2251. }
  2252. static struct device_attribute dev_attr_diag_mode = {
  2253. .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
  2254. .show = netxen_show_diag_mode,
  2255. .store = netxen_store_diag_mode,
  2256. };
  2257. static int
  2258. netxen_sysfs_validate_crb(struct netxen_adapter *adapter,
  2259. loff_t offset, size_t size)
  2260. {
  2261. size_t crb_size = 4;
  2262. if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
  2263. return -EIO;
  2264. if (offset < NETXEN_PCI_CRBSPACE) {
  2265. if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
  2266. return -EINVAL;
  2267. if (ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
  2268. NETXEN_PCI_CAMQM_2M_END))
  2269. crb_size = 8;
  2270. else
  2271. return -EINVAL;
  2272. }
  2273. if ((size != crb_size) || (offset & (crb_size-1)))
  2274. return -EINVAL;
  2275. return 0;
  2276. }
  2277. static ssize_t
  2278. netxen_sysfs_read_crb(struct file *filp, struct kobject *kobj,
  2279. struct bin_attribute *attr,
  2280. char *buf, loff_t offset, size_t size)
  2281. {
  2282. struct device *dev = kobj_to_dev(kobj);
  2283. struct netxen_adapter *adapter = dev_get_drvdata(dev);
  2284. u32 data;
  2285. u64 qmdata;
  2286. int ret;
  2287. ret = netxen_sysfs_validate_crb(adapter, offset, size);
  2288. if (ret != 0)
  2289. return ret;
  2290. if (NX_IS_REVISION_P3(adapter->ahw.revision_id) &&
  2291. ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
  2292. NETXEN_PCI_CAMQM_2M_END)) {
  2293. netxen_pci_camqm_read_2M(adapter, offset, &qmdata);
  2294. memcpy(buf, &qmdata, size);
  2295. } else {
  2296. data = NXRD32(adapter, offset);
  2297. memcpy(buf, &data, size);
  2298. }
  2299. return size;
  2300. }
  2301. static ssize_t
  2302. netxen_sysfs_write_crb(struct file *filp, struct kobject *kobj,
  2303. struct bin_attribute *attr,
  2304. char *buf, loff_t offset, size_t size)
  2305. {
  2306. struct device *dev = kobj_to_dev(kobj);
  2307. struct netxen_adapter *adapter = dev_get_drvdata(dev);
  2308. u32 data;
  2309. u64 qmdata;
  2310. int ret;
  2311. ret = netxen_sysfs_validate_crb(adapter, offset, size);
  2312. if (ret != 0)
  2313. return ret;
  2314. if (NX_IS_REVISION_P3(adapter->ahw.revision_id) &&
  2315. ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
  2316. NETXEN_PCI_CAMQM_2M_END)) {
  2317. memcpy(&qmdata, buf, size);
  2318. netxen_pci_camqm_write_2M(adapter, offset, qmdata);
  2319. } else {
  2320. memcpy(&data, buf, size);
  2321. NXWR32(adapter, offset, data);
  2322. }
  2323. return size;
  2324. }
  2325. static int
  2326. netxen_sysfs_validate_mem(struct netxen_adapter *adapter,
  2327. loff_t offset, size_t size)
  2328. {
  2329. if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
  2330. return -EIO;
  2331. if ((size != 8) || (offset & 0x7))
  2332. return -EIO;
  2333. return 0;
  2334. }
  2335. static ssize_t
  2336. netxen_sysfs_read_mem(struct file *filp, struct kobject *kobj,
  2337. struct bin_attribute *attr,
  2338. char *buf, loff_t offset, size_t size)
  2339. {
  2340. struct device *dev = kobj_to_dev(kobj);
  2341. struct netxen_adapter *adapter = dev_get_drvdata(dev);
  2342. u64 data;
  2343. int ret;
  2344. ret = netxen_sysfs_validate_mem(adapter, offset, size);
  2345. if (ret != 0)
  2346. return ret;
  2347. if (adapter->pci_mem_read(adapter, offset, &data))
  2348. return -EIO;
  2349. memcpy(buf, &data, size);
  2350. return size;
  2351. }
  2352. static ssize_t netxen_sysfs_write_mem(struct file *filp, struct kobject *kobj,
  2353. struct bin_attribute *attr, char *buf,
  2354. loff_t offset, size_t size)
  2355. {
  2356. struct device *dev = kobj_to_dev(kobj);
  2357. struct netxen_adapter *adapter = dev_get_drvdata(dev);
  2358. u64 data;
  2359. int ret;
  2360. ret = netxen_sysfs_validate_mem(adapter, offset, size);
  2361. if (ret != 0)
  2362. return ret;
  2363. memcpy(&data, buf, size);
  2364. if (adapter->pci_mem_write(adapter, offset, data))
  2365. return -EIO;
  2366. return size;
  2367. }
  2368. static struct bin_attribute bin_attr_crb = {
  2369. .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
  2370. .size = 0,
  2371. .read = netxen_sysfs_read_crb,
  2372. .write = netxen_sysfs_write_crb,
  2373. };
  2374. static struct bin_attribute bin_attr_mem = {
  2375. .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
  2376. .size = 0,
  2377. .read = netxen_sysfs_read_mem,
  2378. .write = netxen_sysfs_write_mem,
  2379. };
  2380. static ssize_t
  2381. netxen_sysfs_read_dimm(struct file *filp, struct kobject *kobj,
  2382. struct bin_attribute *attr,
  2383. char *buf, loff_t offset, size_t size)
  2384. {
  2385. struct device *dev = kobj_to_dev(kobj);
  2386. struct netxen_adapter *adapter = dev_get_drvdata(dev);
  2387. struct net_device *netdev = adapter->netdev;
  2388. struct netxen_dimm_cfg dimm;
  2389. u8 dw, rows, cols, banks, ranks;
  2390. u32 val;
  2391. if (size < attr->size) {
  2392. netdev_err(netdev, "Invalid size\n");
  2393. return -EINVAL;
  2394. }
  2395. memset(&dimm, 0, sizeof(struct netxen_dimm_cfg));
  2396. val = NXRD32(adapter, NETXEN_DIMM_CAPABILITY);
  2397. /* Checks if DIMM info is valid. */
  2398. if (val & NETXEN_DIMM_VALID_FLAG) {
  2399. netdev_err(netdev, "Invalid DIMM flag\n");
  2400. dimm.presence = 0xff;
  2401. goto out;
  2402. }
  2403. rows = NETXEN_DIMM_NUMROWS(val);
  2404. cols = NETXEN_DIMM_NUMCOLS(val);
  2405. ranks = NETXEN_DIMM_NUMRANKS(val);
  2406. banks = NETXEN_DIMM_NUMBANKS(val);
  2407. dw = NETXEN_DIMM_DATAWIDTH(val);
  2408. dimm.presence = (val & NETXEN_DIMM_PRESENT);
  2409. /* Checks if DIMM info is present. */
  2410. if (!dimm.presence) {
  2411. netdev_err(netdev, "DIMM not present\n");
  2412. goto out;
  2413. }
  2414. dimm.dimm_type = NETXEN_DIMM_TYPE(val);
  2415. switch (dimm.dimm_type) {
  2416. case NETXEN_DIMM_TYPE_RDIMM:
  2417. case NETXEN_DIMM_TYPE_UDIMM:
  2418. case NETXEN_DIMM_TYPE_SO_DIMM:
  2419. case NETXEN_DIMM_TYPE_Micro_DIMM:
  2420. case NETXEN_DIMM_TYPE_Mini_RDIMM:
  2421. case NETXEN_DIMM_TYPE_Mini_UDIMM:
  2422. break;
  2423. default:
  2424. netdev_err(netdev, "Invalid DIMM type %x\n", dimm.dimm_type);
  2425. goto out;
  2426. }
  2427. if (val & NETXEN_DIMM_MEMTYPE_DDR2_SDRAM)
  2428. dimm.mem_type = NETXEN_DIMM_MEM_DDR2_SDRAM;
  2429. else
  2430. dimm.mem_type = NETXEN_DIMM_MEMTYPE(val);
  2431. if (val & NETXEN_DIMM_SIZE) {
  2432. dimm.size = NETXEN_DIMM_STD_MEM_SIZE;
  2433. goto out;
  2434. }
  2435. if (!rows) {
  2436. netdev_err(netdev, "Invalid no of rows %x\n", rows);
  2437. goto out;
  2438. }
  2439. if (!cols) {
  2440. netdev_err(netdev, "Invalid no of columns %x\n", cols);
  2441. goto out;
  2442. }
  2443. if (!banks) {
  2444. netdev_err(netdev, "Invalid no of banks %x\n", banks);
  2445. goto out;
  2446. }
  2447. ranks += 1;
  2448. switch (dw) {
  2449. case 0x0:
  2450. dw = 32;
  2451. break;
  2452. case 0x1:
  2453. dw = 33;
  2454. break;
  2455. case 0x2:
  2456. dw = 36;
  2457. break;
  2458. case 0x3:
  2459. dw = 64;
  2460. break;
  2461. case 0x4:
  2462. dw = 72;
  2463. break;
  2464. case 0x5:
  2465. dw = 80;
  2466. break;
  2467. case 0x6:
  2468. dw = 128;
  2469. break;
  2470. case 0x7:
  2471. dw = 144;
  2472. break;
  2473. default:
  2474. netdev_err(netdev, "Invalid data-width %x\n", dw);
  2475. goto out;
  2476. }
  2477. dimm.size = ((1 << rows) * (1 << cols) * dw * banks * ranks) / 8;
  2478. /* Size returned in MB. */
  2479. dimm.size = (dimm.size) / 0x100000;
  2480. out:
  2481. memcpy(buf, &dimm, sizeof(struct netxen_dimm_cfg));
  2482. return sizeof(struct netxen_dimm_cfg);
  2483. }
  2484. static struct bin_attribute bin_attr_dimm = {
  2485. .attr = { .name = "dimm", .mode = (S_IRUGO | S_IWUSR) },
  2486. .size = sizeof(struct netxen_dimm_cfg),
  2487. .read = netxen_sysfs_read_dimm,
  2488. };
  2489. static void
  2490. netxen_create_sysfs_entries(struct netxen_adapter *adapter)
  2491. {
  2492. struct device *dev = &adapter->pdev->dev;
  2493. if (adapter->capabilities & NX_FW_CAPABILITY_BDG) {
  2494. /* bridged_mode control */
  2495. if (device_create_file(dev, &dev_attr_bridged_mode)) {
  2496. dev_warn(dev,
  2497. "failed to create bridged_mode sysfs entry\n");
  2498. }
  2499. }
  2500. }
  2501. static void
  2502. netxen_remove_sysfs_entries(struct netxen_adapter *adapter)
  2503. {
  2504. struct device *dev = &adapter->pdev->dev;
  2505. if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
  2506. device_remove_file(dev, &dev_attr_bridged_mode);
  2507. }
  2508. static void
  2509. netxen_create_diag_entries(struct netxen_adapter *adapter)
  2510. {
  2511. struct pci_dev *pdev = adapter->pdev;
  2512. struct device *dev;
  2513. dev = &pdev->dev;
  2514. if (device_create_file(dev, &dev_attr_diag_mode))
  2515. dev_info(dev, "failed to create diag_mode sysfs entry\n");
  2516. if (device_create_bin_file(dev, &bin_attr_crb))
  2517. dev_info(dev, "failed to create crb sysfs entry\n");
  2518. if (device_create_bin_file(dev, &bin_attr_mem))
  2519. dev_info(dev, "failed to create mem sysfs entry\n");
  2520. if (device_create_bin_file(dev, &bin_attr_dimm))
  2521. dev_info(dev, "failed to create dimm sysfs entry\n");
  2522. }
  2523. static void
  2524. netxen_remove_diag_entries(struct netxen_adapter *adapter)
  2525. {
  2526. struct pci_dev *pdev = adapter->pdev;
  2527. struct device *dev = &pdev->dev;
  2528. device_remove_file(dev, &dev_attr_diag_mode);
  2529. device_remove_bin_file(dev, &bin_attr_crb);
  2530. device_remove_bin_file(dev, &bin_attr_mem);
  2531. device_remove_bin_file(dev, &bin_attr_dimm);
  2532. }
  2533. #ifdef CONFIG_INET
  2534. #define is_netxen_netdev(dev) (dev->netdev_ops == &netxen_netdev_ops)
  2535. static int
  2536. netxen_destip_supported(struct netxen_adapter *adapter)
  2537. {
  2538. if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
  2539. return 0;
  2540. if (adapter->ahw.cut_through)
  2541. return 0;
  2542. return 1;
  2543. }
  2544. static void
  2545. netxen_free_ip_list(struct netxen_adapter *adapter, bool master)
  2546. {
  2547. struct nx_ip_list *cur, *tmp_cur;
  2548. list_for_each_entry_safe(cur, tmp_cur, &adapter->ip_list, list) {
  2549. if (master) {
  2550. if (cur->master) {
  2551. netxen_config_ipaddr(adapter, cur->ip_addr,
  2552. NX_IP_DOWN);
  2553. list_del(&cur->list);
  2554. kfree(cur);
  2555. }
  2556. } else {
  2557. netxen_config_ipaddr(adapter, cur->ip_addr, NX_IP_DOWN);
  2558. list_del(&cur->list);
  2559. kfree(cur);
  2560. }
  2561. }
  2562. }
  2563. static bool
  2564. netxen_list_config_ip(struct netxen_adapter *adapter,
  2565. struct in_ifaddr *ifa, unsigned long event)
  2566. {
  2567. struct net_device *dev;
  2568. struct nx_ip_list *cur, *tmp_cur;
  2569. struct list_head *head;
  2570. bool ret = false;
  2571. dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
  2572. if (dev == NULL)
  2573. goto out;
  2574. switch (event) {
  2575. case NX_IP_UP:
  2576. list_for_each(head, &adapter->ip_list) {
  2577. cur = list_entry(head, struct nx_ip_list, list);
  2578. if (cur->ip_addr == ifa->ifa_address)
  2579. goto out;
  2580. }
  2581. cur = kzalloc(sizeof(struct nx_ip_list), GFP_ATOMIC);
  2582. if (cur == NULL)
  2583. goto out;
  2584. if (dev->priv_flags & IFF_802_1Q_VLAN)
  2585. dev = vlan_dev_real_dev(dev);
  2586. cur->master = !!netif_is_bond_master(dev);
  2587. cur->ip_addr = ifa->ifa_address;
  2588. list_add_tail(&cur->list, &adapter->ip_list);
  2589. netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_UP);
  2590. ret = true;
  2591. break;
  2592. case NX_IP_DOWN:
  2593. list_for_each_entry_safe(cur, tmp_cur,
  2594. &adapter->ip_list, list) {
  2595. if (cur->ip_addr == ifa->ifa_address) {
  2596. list_del(&cur->list);
  2597. kfree(cur);
  2598. netxen_config_ipaddr(adapter, ifa->ifa_address,
  2599. NX_IP_DOWN);
  2600. ret = true;
  2601. break;
  2602. }
  2603. }
  2604. }
  2605. out:
  2606. return ret;
  2607. }
  2608. static void
  2609. netxen_config_indev_addr(struct netxen_adapter *adapter,
  2610. struct net_device *dev, unsigned long event)
  2611. {
  2612. struct in_device *indev;
  2613. if (!netxen_destip_supported(adapter))
  2614. return;
  2615. indev = in_dev_get(dev);
  2616. if (!indev)
  2617. return;
  2618. for_ifa(indev) {
  2619. switch (event) {
  2620. case NETDEV_UP:
  2621. netxen_list_config_ip(adapter, ifa, NX_IP_UP);
  2622. break;
  2623. case NETDEV_DOWN:
  2624. netxen_list_config_ip(adapter, ifa, NX_IP_DOWN);
  2625. break;
  2626. default:
  2627. break;
  2628. }
  2629. } endfor_ifa(indev);
  2630. in_dev_put(indev);
  2631. }
  2632. static void
  2633. netxen_restore_indev_addr(struct net_device *netdev, unsigned long event)
  2634. {
  2635. struct netxen_adapter *adapter = netdev_priv(netdev);
  2636. struct nx_ip_list *pos, *tmp_pos;
  2637. unsigned long ip_event;
  2638. ip_event = (event == NETDEV_UP) ? NX_IP_UP : NX_IP_DOWN;
  2639. netxen_config_indev_addr(adapter, netdev, event);
  2640. list_for_each_entry_safe(pos, tmp_pos, &adapter->ip_list, list) {
  2641. netxen_config_ipaddr(adapter, pos->ip_addr, ip_event);
  2642. }
  2643. }
  2644. static inline bool
  2645. netxen_config_checkdev(struct net_device *dev)
  2646. {
  2647. struct netxen_adapter *adapter;
  2648. if (!is_netxen_netdev(dev))
  2649. return false;
  2650. adapter = netdev_priv(dev);
  2651. if (!adapter)
  2652. return false;
  2653. if (!netxen_destip_supported(adapter))
  2654. return false;
  2655. if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
  2656. return false;
  2657. return true;
  2658. }
  2659. /**
  2660. * netxen_config_master - configure addresses based on master
  2661. * @dev: netxen device
  2662. * @event: netdev event
  2663. */
  2664. static void netxen_config_master(struct net_device *dev, unsigned long event)
  2665. {
  2666. struct net_device *master, *slave;
  2667. struct netxen_adapter *adapter = netdev_priv(dev);
  2668. rcu_read_lock();
  2669. master = netdev_master_upper_dev_get_rcu(dev);
  2670. /*
  2671. * This is the case where the netxen nic is being
  2672. * enslaved and is dev_open()ed in bond_enslave()
  2673. * Now we should program the bond's (and its vlans')
  2674. * addresses in the netxen NIC.
  2675. */
  2676. if (master && netif_is_bond_master(master) &&
  2677. !netif_is_bond_slave(dev)) {
  2678. netxen_config_indev_addr(adapter, master, event);
  2679. for_each_netdev_rcu(&init_net, slave)
  2680. if (slave->priv_flags & IFF_802_1Q_VLAN &&
  2681. vlan_dev_real_dev(slave) == master)
  2682. netxen_config_indev_addr(adapter, slave, event);
  2683. }
  2684. rcu_read_unlock();
  2685. /*
  2686. * This is the case where the netxen nic is being
  2687. * released and is dev_close()ed in bond_release()
  2688. * just before IFF_BONDING is stripped.
  2689. */
  2690. if (!master && dev->priv_flags & IFF_BONDING)
  2691. netxen_free_ip_list(adapter, true);
  2692. }
  2693. static int netxen_netdev_event(struct notifier_block *this,
  2694. unsigned long event, void *ptr)
  2695. {
  2696. struct netxen_adapter *adapter;
  2697. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  2698. struct net_device *orig_dev = dev;
  2699. struct net_device *slave;
  2700. recheck:
  2701. if (dev == NULL)
  2702. goto done;
  2703. if (dev->priv_flags & IFF_802_1Q_VLAN) {
  2704. dev = vlan_dev_real_dev(dev);
  2705. goto recheck;
  2706. }
  2707. if (event == NETDEV_UP || event == NETDEV_DOWN) {
  2708. /* If this is a bonding device, look for netxen-based slaves*/
  2709. if (netif_is_bond_master(dev)) {
  2710. rcu_read_lock();
  2711. for_each_netdev_in_bond_rcu(dev, slave) {
  2712. if (!netxen_config_checkdev(slave))
  2713. continue;
  2714. adapter = netdev_priv(slave);
  2715. netxen_config_indev_addr(adapter,
  2716. orig_dev, event);
  2717. }
  2718. rcu_read_unlock();
  2719. } else {
  2720. if (!netxen_config_checkdev(dev))
  2721. goto done;
  2722. adapter = netdev_priv(dev);
  2723. /* Act only if the actual netxen is the target */
  2724. if (orig_dev == dev)
  2725. netxen_config_master(dev, event);
  2726. netxen_config_indev_addr(adapter, orig_dev, event);
  2727. }
  2728. }
  2729. done:
  2730. return NOTIFY_DONE;
  2731. }
  2732. static int
  2733. netxen_inetaddr_event(struct notifier_block *this,
  2734. unsigned long event, void *ptr)
  2735. {
  2736. struct netxen_adapter *adapter;
  2737. struct net_device *dev, *slave;
  2738. struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
  2739. unsigned long ip_event;
  2740. dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
  2741. ip_event = (event == NETDEV_UP) ? NX_IP_UP : NX_IP_DOWN;
  2742. recheck:
  2743. if (dev == NULL)
  2744. goto done;
  2745. if (dev->priv_flags & IFF_802_1Q_VLAN) {
  2746. dev = vlan_dev_real_dev(dev);
  2747. goto recheck;
  2748. }
  2749. if (event == NETDEV_UP || event == NETDEV_DOWN) {
  2750. /* If this is a bonding device, look for netxen-based slaves*/
  2751. if (netif_is_bond_master(dev)) {
  2752. rcu_read_lock();
  2753. for_each_netdev_in_bond_rcu(dev, slave) {
  2754. if (!netxen_config_checkdev(slave))
  2755. continue;
  2756. adapter = netdev_priv(slave);
  2757. netxen_list_config_ip(adapter, ifa, ip_event);
  2758. }
  2759. rcu_read_unlock();
  2760. } else {
  2761. if (!netxen_config_checkdev(dev))
  2762. goto done;
  2763. adapter = netdev_priv(dev);
  2764. netxen_list_config_ip(adapter, ifa, ip_event);
  2765. }
  2766. }
  2767. done:
  2768. return NOTIFY_DONE;
  2769. }
  2770. static struct notifier_block netxen_netdev_cb = {
  2771. .notifier_call = netxen_netdev_event,
  2772. };
  2773. static struct notifier_block netxen_inetaddr_cb = {
  2774. .notifier_call = netxen_inetaddr_event,
  2775. };
  2776. #else
  2777. static void
  2778. netxen_restore_indev_addr(struct net_device *dev, unsigned long event)
  2779. { }
  2780. static void
  2781. netxen_free_ip_list(struct netxen_adapter *adapter, bool master)
  2782. { }
  2783. #endif
  2784. static const struct pci_error_handlers netxen_err_handler = {
  2785. .error_detected = netxen_io_error_detected,
  2786. .slot_reset = netxen_io_slot_reset,
  2787. .resume = netxen_io_resume,
  2788. };
  2789. static struct pci_driver netxen_driver = {
  2790. .name = netxen_nic_driver_name,
  2791. .id_table = netxen_pci_tbl,
  2792. .probe = netxen_nic_probe,
  2793. .remove = netxen_nic_remove,
  2794. #ifdef CONFIG_PM
  2795. .suspend = netxen_nic_suspend,
  2796. .resume = netxen_nic_resume,
  2797. #endif
  2798. .shutdown = netxen_nic_shutdown,
  2799. .err_handler = &netxen_err_handler
  2800. };
  2801. static int __init netxen_init_module(void)
  2802. {
  2803. printk(KERN_INFO "%s\n", netxen_nic_driver_string);
  2804. #ifdef CONFIG_INET
  2805. register_netdevice_notifier(&netxen_netdev_cb);
  2806. register_inetaddr_notifier(&netxen_inetaddr_cb);
  2807. #endif
  2808. return pci_register_driver(&netxen_driver);
  2809. }
  2810. module_init(netxen_init_module);
  2811. static void __exit netxen_exit_module(void)
  2812. {
  2813. pci_unregister_driver(&netxen_driver);
  2814. #ifdef CONFIG_INET
  2815. unregister_inetaddr_notifier(&netxen_inetaddr_cb);
  2816. unregister_netdevice_notifier(&netxen_netdev_cb);
  2817. #endif
  2818. }
  2819. module_exit(netxen_exit_module);