netxen_nic_main.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526
  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. MODULE_FIRMWARE(NX_UNIFIED_ROMIMAGE_NAME);
  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. if (netxen_rom_fast_read(adapter, offset, &val) == -1) {
  701. dev_err(&pdev->dev, "error reading board info\n");
  702. adapter->driver_mismatch = 1;
  703. return;
  704. }
  705. ptr32[i] = cpu_to_le32(val);
  706. offset += sizeof(u32);
  707. }
  708. fw_major = NXRD32(adapter, NETXEN_FW_VERSION_MAJOR);
  709. fw_minor = NXRD32(adapter, NETXEN_FW_VERSION_MINOR);
  710. fw_build = NXRD32(adapter, NETXEN_FW_VERSION_SUB);
  711. prev_fw_version = adapter->fw_version;
  712. adapter->fw_version = NETXEN_VERSION_CODE(fw_major, fw_minor, fw_build);
  713. /* Get FW Mini Coredump template and store it */
  714. if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
  715. if (adapter->mdump.md_template == NULL ||
  716. adapter->fw_version > prev_fw_version) {
  717. kfree(adapter->mdump.md_template);
  718. adapter->mdump.md_template = NULL;
  719. err = netxen_setup_minidump(adapter);
  720. if (err)
  721. dev_err(&adapter->pdev->dev,
  722. "Failed to setup minidump rcode = %d\n", err);
  723. }
  724. }
  725. if (adapter->portnum == 0) {
  726. if (netxen_nic_get_brd_name_by_type(adapter->ahw.board_type,
  727. brd_name))
  728. strcpy(serial_num, "Unknown");
  729. pr_info("%s: %s Board S/N %s Chip rev 0x%x\n",
  730. module_name(THIS_MODULE),
  731. brd_name, serial_num, adapter->ahw.revision_id);
  732. }
  733. if (adapter->fw_version < NETXEN_VERSION_CODE(3, 4, 216)) {
  734. adapter->driver_mismatch = 1;
  735. dev_warn(&pdev->dev, "firmware version %d.%d.%d unsupported\n",
  736. fw_major, fw_minor, fw_build);
  737. return;
  738. }
  739. if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
  740. i = NXRD32(adapter, NETXEN_SRE_MISC);
  741. adapter->ahw.cut_through = (i & 0x8000) ? 1 : 0;
  742. }
  743. dev_info(&pdev->dev, "Driver v%s, firmware v%d.%d.%d [%s]\n",
  744. NETXEN_NIC_LINUX_VERSIONID, fw_major, fw_minor, fw_build,
  745. adapter->ahw.cut_through ? "cut-through" : "legacy");
  746. if (adapter->fw_version >= NETXEN_VERSION_CODE(4, 0, 222))
  747. adapter->capabilities = NXRD32(adapter, CRB_FW_CAPABILITIES_1);
  748. if (adapter->ahw.port_type == NETXEN_NIC_XGBE) {
  749. adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
  750. adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
  751. } else if (adapter->ahw.port_type == NETXEN_NIC_GBE) {
  752. adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
  753. adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
  754. }
  755. adapter->msix_supported = 0;
  756. if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
  757. adapter->msix_supported = !!use_msi_x;
  758. adapter->rss_supported = !!use_msi_x;
  759. } else {
  760. u32 flashed_ver = 0;
  761. netxen_rom_fast_read(adapter,
  762. NX_FW_VERSION_OFFSET, (int *)&flashed_ver);
  763. flashed_ver = NETXEN_DECODE_VERSION(flashed_ver);
  764. if (flashed_ver >= NETXEN_VERSION_CODE(3, 4, 336)) {
  765. switch (adapter->ahw.board_type) {
  766. case NETXEN_BRDTYPE_P2_SB31_10G:
  767. case NETXEN_BRDTYPE_P2_SB31_10G_CX4:
  768. adapter->msix_supported = !!use_msi_x;
  769. adapter->rss_supported = !!use_msi_x;
  770. break;
  771. default:
  772. break;
  773. }
  774. }
  775. }
  776. adapter->num_txd = MAX_CMD_DESCRIPTORS;
  777. if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  778. adapter->num_lro_rxd = MAX_LRO_RCV_DESCRIPTORS;
  779. adapter->max_rds_rings = 3;
  780. } else {
  781. adapter->num_lro_rxd = 0;
  782. adapter->max_rds_rings = 2;
  783. }
  784. }
  785. static int
  786. netxen_start_firmware(struct netxen_adapter *adapter)
  787. {
  788. int val, err, first_boot;
  789. struct pci_dev *pdev = adapter->pdev;
  790. /* required for NX2031 dummy dma */
  791. err = nx_set_dma_mask(adapter);
  792. if (err)
  793. return err;
  794. err = netxen_can_start_firmware(adapter);
  795. if (err < 0)
  796. return err;
  797. if (!err)
  798. goto wait_init;
  799. first_boot = NXRD32(adapter, NETXEN_CAM_RAM(0x1fc));
  800. err = netxen_check_hw_init(adapter, first_boot);
  801. if (err) {
  802. dev_err(&pdev->dev, "error in init HW init sequence\n");
  803. return err;
  804. }
  805. netxen_request_firmware(adapter);
  806. err = netxen_need_fw_reset(adapter);
  807. if (err < 0)
  808. goto err_out;
  809. if (err == 0)
  810. goto pcie_strap_init;
  811. if (first_boot != 0x55555555) {
  812. NXWR32(adapter, CRB_CMDPEG_STATE, 0);
  813. netxen_pinit_from_rom(adapter);
  814. msleep(1);
  815. }
  816. NXWR32(adapter, CRB_DMA_SHIFT, 0x55555555);
  817. NXWR32(adapter, NETXEN_PEG_HALT_STATUS1, 0);
  818. NXWR32(adapter, NETXEN_PEG_HALT_STATUS2, 0);
  819. if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
  820. netxen_set_port_mode(adapter);
  821. err = netxen_load_firmware(adapter);
  822. if (err)
  823. goto err_out;
  824. netxen_release_firmware(adapter);
  825. if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  826. /* Initialize multicast addr pool owners */
  827. val = 0x7654;
  828. if (adapter->ahw.port_type == NETXEN_NIC_XGBE)
  829. val |= 0x0f000000;
  830. NXWR32(adapter, NETXEN_MAC_ADDR_CNTL_REG, val);
  831. }
  832. err = netxen_init_dummy_dma(adapter);
  833. if (err)
  834. goto err_out;
  835. /*
  836. * Tell the hardware our version number.
  837. */
  838. val = (_NETXEN_NIC_LINUX_MAJOR << 16)
  839. | ((_NETXEN_NIC_LINUX_MINOR << 8))
  840. | (_NETXEN_NIC_LINUX_SUBVERSION);
  841. NXWR32(adapter, CRB_DRIVER_VERSION, val);
  842. pcie_strap_init:
  843. if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
  844. netxen_pcie_strap_init(adapter);
  845. wait_init:
  846. /* Handshake with the card before we register the devices. */
  847. err = netxen_phantom_init(adapter, NETXEN_NIC_PEG_TUNE);
  848. if (err) {
  849. netxen_free_dummy_dma(adapter);
  850. goto err_out;
  851. }
  852. NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_READY);
  853. nx_update_dma_mask(adapter);
  854. netxen_check_options(adapter);
  855. adapter->need_fw_reset = 0;
  856. /* fall through and release firmware */
  857. err_out:
  858. netxen_release_firmware(adapter);
  859. return err;
  860. }
  861. static int
  862. netxen_nic_request_irq(struct netxen_adapter *adapter)
  863. {
  864. irq_handler_t handler;
  865. struct nx_host_sds_ring *sds_ring;
  866. int err, ring;
  867. unsigned long flags = 0;
  868. struct net_device *netdev = adapter->netdev;
  869. struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
  870. if (adapter->flags & NETXEN_NIC_MSIX_ENABLED)
  871. handler = netxen_msix_intr;
  872. else if (adapter->flags & NETXEN_NIC_MSI_ENABLED)
  873. handler = netxen_msi_intr;
  874. else {
  875. flags |= IRQF_SHARED;
  876. handler = netxen_intr;
  877. }
  878. adapter->irq = netdev->irq;
  879. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  880. sds_ring = &recv_ctx->sds_rings[ring];
  881. sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
  882. err = request_irq(sds_ring->irq, handler,
  883. flags, sds_ring->name, sds_ring);
  884. if (err)
  885. return err;
  886. }
  887. return 0;
  888. }
  889. static void
  890. netxen_nic_free_irq(struct netxen_adapter *adapter)
  891. {
  892. int ring;
  893. struct nx_host_sds_ring *sds_ring;
  894. struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
  895. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  896. sds_ring = &recv_ctx->sds_rings[ring];
  897. free_irq(sds_ring->irq, sds_ring);
  898. }
  899. }
  900. static void
  901. netxen_nic_init_coalesce_defaults(struct netxen_adapter *adapter)
  902. {
  903. adapter->coal.flags = NETXEN_NIC_INTR_DEFAULT;
  904. adapter->coal.normal.data.rx_time_us =
  905. NETXEN_DEFAULT_INTR_COALESCE_RX_TIME_US;
  906. adapter->coal.normal.data.rx_packets =
  907. NETXEN_DEFAULT_INTR_COALESCE_RX_PACKETS;
  908. adapter->coal.normal.data.tx_time_us =
  909. NETXEN_DEFAULT_INTR_COALESCE_TX_TIME_US;
  910. adapter->coal.normal.data.tx_packets =
  911. NETXEN_DEFAULT_INTR_COALESCE_TX_PACKETS;
  912. }
  913. /* with rtnl_lock */
  914. static int
  915. __netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
  916. {
  917. int err;
  918. if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
  919. return -EIO;
  920. err = adapter->init_port(adapter, adapter->physical_port);
  921. if (err) {
  922. printk(KERN_ERR "%s: Failed to initialize port %d\n",
  923. netxen_nic_driver_name, adapter->portnum);
  924. return err;
  925. }
  926. if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
  927. adapter->macaddr_set(adapter, adapter->mac_addr);
  928. adapter->set_multi(netdev);
  929. adapter->set_mtu(adapter, netdev->mtu);
  930. adapter->ahw.linkup = 0;
  931. if (adapter->max_sds_rings > 1)
  932. netxen_config_rss(adapter, 1);
  933. if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
  934. netxen_config_intr_coalesce(adapter);
  935. if (netdev->features & NETIF_F_LRO)
  936. netxen_config_hw_lro(adapter, NETXEN_NIC_LRO_ENABLED);
  937. netxen_napi_enable(adapter);
  938. if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
  939. netxen_linkevent_request(adapter, 1);
  940. else
  941. netxen_nic_set_link_parameters(adapter);
  942. set_bit(__NX_DEV_UP, &adapter->state);
  943. return 0;
  944. }
  945. /* Usage: During resume and firmware recovery module.*/
  946. static inline int
  947. netxen_nic_up(struct netxen_adapter *adapter, struct net_device *netdev)
  948. {
  949. int err = 0;
  950. rtnl_lock();
  951. if (netif_running(netdev))
  952. err = __netxen_nic_up(adapter, netdev);
  953. rtnl_unlock();
  954. return err;
  955. }
  956. /* with rtnl_lock */
  957. static void
  958. __netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
  959. {
  960. if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
  961. return;
  962. if (!test_and_clear_bit(__NX_DEV_UP, &adapter->state))
  963. return;
  964. smp_mb();
  965. netif_carrier_off(netdev);
  966. netif_tx_disable(netdev);
  967. if (adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)
  968. netxen_linkevent_request(adapter, 0);
  969. if (adapter->stop_port)
  970. adapter->stop_port(adapter);
  971. if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
  972. netxen_p3_free_mac_list(adapter);
  973. adapter->set_promisc(adapter, NETXEN_NIU_NON_PROMISC_MODE);
  974. netxen_napi_disable(adapter);
  975. netxen_release_tx_buffers(adapter);
  976. }
  977. /* Usage: During suspend and firmware recovery module */
  978. static inline void
  979. netxen_nic_down(struct netxen_adapter *adapter, struct net_device *netdev)
  980. {
  981. rtnl_lock();
  982. if (netif_running(netdev))
  983. __netxen_nic_down(adapter, netdev);
  984. rtnl_unlock();
  985. }
  986. static int
  987. netxen_nic_attach(struct netxen_adapter *adapter)
  988. {
  989. struct net_device *netdev = adapter->netdev;
  990. struct pci_dev *pdev = adapter->pdev;
  991. int err, ring;
  992. struct nx_host_rds_ring *rds_ring;
  993. struct nx_host_tx_ring *tx_ring;
  994. u32 capab2;
  995. if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC)
  996. return 0;
  997. err = netxen_init_firmware(adapter);
  998. if (err)
  999. return err;
  1000. adapter->flags &= ~NETXEN_FW_MSS_CAP;
  1001. if (adapter->capabilities & NX_FW_CAPABILITY_MORE_CAPS) {
  1002. capab2 = NXRD32(adapter, CRB_FW_CAPABILITIES_2);
  1003. if (capab2 & NX_FW_CAPABILITY_2_LRO_MAX_TCP_SEG)
  1004. adapter->flags |= NETXEN_FW_MSS_CAP;
  1005. }
  1006. err = netxen_napi_add(adapter, netdev);
  1007. if (err)
  1008. return err;
  1009. err = netxen_alloc_sw_resources(adapter);
  1010. if (err) {
  1011. printk(KERN_ERR "%s: Error in setting sw resources\n",
  1012. netdev->name);
  1013. return err;
  1014. }
  1015. err = netxen_alloc_hw_resources(adapter);
  1016. if (err) {
  1017. printk(KERN_ERR "%s: Error in setting hw resources\n",
  1018. netdev->name);
  1019. goto err_out_free_sw;
  1020. }
  1021. if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  1022. tx_ring = adapter->tx_ring;
  1023. tx_ring->crb_cmd_producer = netxen_get_ioaddr(adapter,
  1024. crb_cmd_producer[adapter->portnum]);
  1025. tx_ring->crb_cmd_consumer = netxen_get_ioaddr(adapter,
  1026. crb_cmd_consumer[adapter->portnum]);
  1027. tx_ring->producer = 0;
  1028. tx_ring->sw_consumer = 0;
  1029. netxen_nic_update_cmd_producer(adapter, tx_ring);
  1030. netxen_nic_update_cmd_consumer(adapter, tx_ring);
  1031. }
  1032. for (ring = 0; ring < adapter->max_rds_rings; ring++) {
  1033. rds_ring = &adapter->recv_ctx.rds_rings[ring];
  1034. netxen_post_rx_buffers(adapter, ring, rds_ring);
  1035. }
  1036. err = netxen_nic_request_irq(adapter);
  1037. if (err) {
  1038. dev_err(&pdev->dev, "%s: failed to setup interrupt\n",
  1039. netdev->name);
  1040. goto err_out_free_rxbuf;
  1041. }
  1042. if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
  1043. netxen_nic_init_coalesce_defaults(adapter);
  1044. netxen_create_sysfs_entries(adapter);
  1045. adapter->is_up = NETXEN_ADAPTER_UP_MAGIC;
  1046. return 0;
  1047. err_out_free_rxbuf:
  1048. netxen_release_rx_buffers(adapter);
  1049. netxen_free_hw_resources(adapter);
  1050. err_out_free_sw:
  1051. netxen_free_sw_resources(adapter);
  1052. return err;
  1053. }
  1054. static void
  1055. netxen_nic_detach(struct netxen_adapter *adapter)
  1056. {
  1057. if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
  1058. return;
  1059. netxen_remove_sysfs_entries(adapter);
  1060. netxen_free_hw_resources(adapter);
  1061. netxen_release_rx_buffers(adapter);
  1062. netxen_nic_free_irq(adapter);
  1063. netxen_napi_del(adapter);
  1064. netxen_free_sw_resources(adapter);
  1065. adapter->is_up = 0;
  1066. }
  1067. int
  1068. netxen_nic_reset_context(struct netxen_adapter *adapter)
  1069. {
  1070. int err = 0;
  1071. struct net_device *netdev = adapter->netdev;
  1072. if (test_and_set_bit(__NX_RESETTING, &adapter->state))
  1073. return -EBUSY;
  1074. if (adapter->is_up == NETXEN_ADAPTER_UP_MAGIC) {
  1075. netif_device_detach(netdev);
  1076. if (netif_running(netdev))
  1077. __netxen_nic_down(adapter, netdev);
  1078. netxen_nic_detach(adapter);
  1079. if (netif_running(netdev)) {
  1080. err = netxen_nic_attach(adapter);
  1081. if (!err)
  1082. err = __netxen_nic_up(adapter, netdev);
  1083. if (err)
  1084. goto done;
  1085. }
  1086. netif_device_attach(netdev);
  1087. }
  1088. done:
  1089. clear_bit(__NX_RESETTING, &adapter->state);
  1090. return err;
  1091. }
  1092. static int
  1093. netxen_setup_netdev(struct netxen_adapter *adapter,
  1094. struct net_device *netdev)
  1095. {
  1096. int err = 0;
  1097. struct pci_dev *pdev = adapter->pdev;
  1098. adapter->mc_enabled = 0;
  1099. if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
  1100. adapter->max_mc_count = 38;
  1101. else
  1102. adapter->max_mc_count = 16;
  1103. netdev->netdev_ops = &netxen_netdev_ops;
  1104. netdev->watchdog_timeo = 5*HZ;
  1105. netxen_nic_change_mtu(netdev, netdev->mtu);
  1106. netdev->ethtool_ops = &netxen_nic_ethtool_ops;
  1107. netdev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  1108. NETIF_F_RXCSUM;
  1109. if (NX_IS_REVISION_P3(adapter->ahw.revision_id))
  1110. netdev->hw_features |= NETIF_F_IPV6_CSUM | NETIF_F_TSO6;
  1111. netdev->vlan_features |= netdev->hw_features;
  1112. if (adapter->pci_using_dac) {
  1113. netdev->features |= NETIF_F_HIGHDMA;
  1114. netdev->vlan_features |= NETIF_F_HIGHDMA;
  1115. }
  1116. if (adapter->capabilities & NX_FW_CAPABILITY_FVLANTX)
  1117. netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
  1118. if (adapter->capabilities & NX_FW_CAPABILITY_HW_LRO)
  1119. netdev->hw_features |= NETIF_F_LRO;
  1120. netdev->features |= netdev->hw_features;
  1121. netdev->irq = adapter->msix_entries[0].vector;
  1122. INIT_WORK(&adapter->tx_timeout_task, netxen_tx_timeout_task);
  1123. if (netxen_read_mac_addr(adapter))
  1124. dev_warn(&pdev->dev, "failed to read mac addr\n");
  1125. netif_carrier_off(netdev);
  1126. err = register_netdev(netdev);
  1127. if (err) {
  1128. dev_err(&pdev->dev, "failed to register net device\n");
  1129. return err;
  1130. }
  1131. return 0;
  1132. }
  1133. #define NETXEN_ULA_ADAPTER_KEY (0xdaddad01)
  1134. #define NETXEN_NON_ULA_ADAPTER_KEY (0xdaddad00)
  1135. static void netxen_read_ula_info(struct netxen_adapter *adapter)
  1136. {
  1137. u32 temp;
  1138. /* Print ULA info only once for an adapter */
  1139. if (adapter->portnum != 0)
  1140. return;
  1141. temp = NXRD32(adapter, NETXEN_ULA_KEY);
  1142. switch (temp) {
  1143. case NETXEN_ULA_ADAPTER_KEY:
  1144. dev_info(&adapter->pdev->dev, "ULA adapter");
  1145. break;
  1146. case NETXEN_NON_ULA_ADAPTER_KEY:
  1147. dev_info(&adapter->pdev->dev, "non ULA adapter");
  1148. break;
  1149. default:
  1150. break;
  1151. }
  1152. return;
  1153. }
  1154. #ifdef CONFIG_PCIEAER
  1155. static void netxen_mask_aer_correctable(struct netxen_adapter *adapter)
  1156. {
  1157. struct pci_dev *pdev = adapter->pdev;
  1158. struct pci_dev *root = pdev->bus->self;
  1159. u32 aer_pos;
  1160. /* root bus? */
  1161. if (!root)
  1162. return;
  1163. if (adapter->ahw.board_type != NETXEN_BRDTYPE_P3_4_GB_MM &&
  1164. adapter->ahw.board_type != NETXEN_BRDTYPE_P3_10G_TP)
  1165. return;
  1166. if (pci_pcie_type(root) != PCI_EXP_TYPE_ROOT_PORT)
  1167. return;
  1168. aer_pos = pci_find_ext_capability(root, PCI_EXT_CAP_ID_ERR);
  1169. if (!aer_pos)
  1170. return;
  1171. pci_write_config_dword(root, aer_pos + PCI_ERR_COR_MASK, 0xffff);
  1172. }
  1173. #endif
  1174. static int
  1175. netxen_nic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1176. {
  1177. struct net_device *netdev = NULL;
  1178. struct netxen_adapter *adapter = NULL;
  1179. int i = 0, err;
  1180. int pci_func_id = PCI_FUNC(pdev->devfn);
  1181. uint8_t revision_id;
  1182. u32 val;
  1183. if (pdev->revision >= NX_P3_A0 && pdev->revision <= NX_P3_B1) {
  1184. pr_warn("%s: chip revisions between 0x%x-0x%x will not be enabled\n",
  1185. module_name(THIS_MODULE), NX_P3_A0, NX_P3_B1);
  1186. return -ENODEV;
  1187. }
  1188. if ((err = pci_enable_device(pdev)))
  1189. return err;
  1190. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  1191. err = -ENODEV;
  1192. goto err_out_disable_pdev;
  1193. }
  1194. if ((err = pci_request_regions(pdev, netxen_nic_driver_name)))
  1195. goto err_out_disable_pdev;
  1196. if (NX_IS_REVISION_P3(pdev->revision))
  1197. pci_enable_pcie_error_reporting(pdev);
  1198. pci_set_master(pdev);
  1199. netdev = alloc_etherdev(sizeof(struct netxen_adapter));
  1200. if(!netdev) {
  1201. err = -ENOMEM;
  1202. goto err_out_free_res;
  1203. }
  1204. SET_NETDEV_DEV(netdev, &pdev->dev);
  1205. adapter = netdev_priv(netdev);
  1206. adapter->netdev = netdev;
  1207. adapter->pdev = pdev;
  1208. adapter->ahw.pci_func = pci_func_id;
  1209. revision_id = pdev->revision;
  1210. adapter->ahw.revision_id = revision_id;
  1211. rwlock_init(&adapter->ahw.crb_lock);
  1212. spin_lock_init(&adapter->ahw.mem_lock);
  1213. spin_lock_init(&adapter->tx_clean_lock);
  1214. INIT_LIST_HEAD(&adapter->mac_list);
  1215. INIT_LIST_HEAD(&adapter->ip_list);
  1216. err = netxen_setup_pci_map(adapter);
  1217. if (err)
  1218. goto err_out_free_netdev;
  1219. /* This will be reset for mezz cards */
  1220. adapter->portnum = pci_func_id;
  1221. err = netxen_nic_get_board_info(adapter);
  1222. if (err) {
  1223. dev_err(&pdev->dev, "Error getting board config info.\n");
  1224. goto err_out_iounmap;
  1225. }
  1226. #ifdef CONFIG_PCIEAER
  1227. netxen_mask_aer_correctable(adapter);
  1228. #endif
  1229. /* Mezz cards have PCI function 0,2,3 enabled */
  1230. switch (adapter->ahw.board_type) {
  1231. case NETXEN_BRDTYPE_P2_SB31_10G_IMEZ:
  1232. case NETXEN_BRDTYPE_P2_SB31_10G_HMEZ:
  1233. if (pci_func_id >= 2)
  1234. adapter->portnum = pci_func_id - 2;
  1235. break;
  1236. default:
  1237. break;
  1238. }
  1239. err = netxen_check_flash_fw_compatibility(adapter);
  1240. if (err)
  1241. goto err_out_iounmap;
  1242. if (adapter->portnum == 0) {
  1243. val = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
  1244. if (val != 0xffffffff && val != 0) {
  1245. NXWR32(adapter, NX_CRB_DEV_REF_COUNT, 0);
  1246. adapter->need_fw_reset = 1;
  1247. }
  1248. }
  1249. err = netxen_start_firmware(adapter);
  1250. if (err)
  1251. goto err_out_decr_ref;
  1252. /*
  1253. * See if the firmware gave us a virtual-physical port mapping.
  1254. */
  1255. adapter->physical_port = adapter->portnum;
  1256. if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  1257. i = NXRD32(adapter, CRB_V2P(adapter->portnum));
  1258. if (i != 0x55555555)
  1259. adapter->physical_port = i;
  1260. }
  1261. netxen_nic_clear_stats(adapter);
  1262. err = netxen_setup_intr(adapter);
  1263. if (err) {
  1264. dev_err(&adapter->pdev->dev,
  1265. "Failed to setup interrupts, error = %d\n", err);
  1266. goto err_out_disable_msi;
  1267. }
  1268. netxen_read_ula_info(adapter);
  1269. err = netxen_setup_netdev(adapter, netdev);
  1270. if (err)
  1271. goto err_out_disable_msi;
  1272. pci_set_drvdata(pdev, adapter);
  1273. netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
  1274. switch (adapter->ahw.port_type) {
  1275. case NETXEN_NIC_GBE:
  1276. dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
  1277. adapter->netdev->name);
  1278. break;
  1279. case NETXEN_NIC_XGBE:
  1280. dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
  1281. adapter->netdev->name);
  1282. break;
  1283. }
  1284. netxen_create_diag_entries(adapter);
  1285. return 0;
  1286. err_out_disable_msi:
  1287. netxen_teardown_intr(adapter);
  1288. netxen_free_dummy_dma(adapter);
  1289. err_out_decr_ref:
  1290. nx_decr_dev_ref_cnt(adapter);
  1291. err_out_iounmap:
  1292. netxen_cleanup_pci_map(adapter);
  1293. err_out_free_netdev:
  1294. free_netdev(netdev);
  1295. err_out_free_res:
  1296. pci_release_regions(pdev);
  1297. err_out_disable_pdev:
  1298. pci_disable_device(pdev);
  1299. return err;
  1300. }
  1301. static
  1302. void netxen_cleanup_minidump(struct netxen_adapter *adapter)
  1303. {
  1304. kfree(adapter->mdump.md_template);
  1305. adapter->mdump.md_template = NULL;
  1306. if (adapter->mdump.md_capture_buff) {
  1307. vfree(adapter->mdump.md_capture_buff);
  1308. adapter->mdump.md_capture_buff = NULL;
  1309. }
  1310. }
  1311. static void netxen_nic_remove(struct pci_dev *pdev)
  1312. {
  1313. struct netxen_adapter *adapter;
  1314. struct net_device *netdev;
  1315. adapter = pci_get_drvdata(pdev);
  1316. if (adapter == NULL)
  1317. return;
  1318. netdev = adapter->netdev;
  1319. netxen_cancel_fw_work(adapter);
  1320. unregister_netdev(netdev);
  1321. cancel_work_sync(&adapter->tx_timeout_task);
  1322. netxen_free_ip_list(adapter, false);
  1323. netxen_nic_detach(adapter);
  1324. nx_decr_dev_ref_cnt(adapter);
  1325. if (adapter->portnum == 0)
  1326. netxen_free_dummy_dma(adapter);
  1327. clear_bit(__NX_RESETTING, &adapter->state);
  1328. netxen_teardown_intr(adapter);
  1329. netxen_set_interrupt_mode(adapter, 0);
  1330. netxen_remove_diag_entries(adapter);
  1331. netxen_cleanup_pci_map(adapter);
  1332. netxen_release_firmware(adapter);
  1333. if (NX_IS_REVISION_P3(pdev->revision)) {
  1334. netxen_cleanup_minidump(adapter);
  1335. pci_disable_pcie_error_reporting(pdev);
  1336. }
  1337. pci_release_regions(pdev);
  1338. pci_disable_device(pdev);
  1339. free_netdev(netdev);
  1340. }
  1341. static void netxen_nic_detach_func(struct netxen_adapter *adapter)
  1342. {
  1343. struct net_device *netdev = adapter->netdev;
  1344. netif_device_detach(netdev);
  1345. netxen_cancel_fw_work(adapter);
  1346. if (netif_running(netdev))
  1347. netxen_nic_down(adapter, netdev);
  1348. cancel_work_sync(&adapter->tx_timeout_task);
  1349. netxen_nic_detach(adapter);
  1350. if (adapter->portnum == 0)
  1351. netxen_free_dummy_dma(adapter);
  1352. nx_decr_dev_ref_cnt(adapter);
  1353. clear_bit(__NX_RESETTING, &adapter->state);
  1354. }
  1355. static int netxen_nic_attach_func(struct pci_dev *pdev)
  1356. {
  1357. struct netxen_adapter *adapter = pci_get_drvdata(pdev);
  1358. struct net_device *netdev = adapter->netdev;
  1359. int err;
  1360. err = pci_enable_device(pdev);
  1361. if (err)
  1362. return err;
  1363. pci_set_power_state(pdev, PCI_D0);
  1364. pci_set_master(pdev);
  1365. pci_restore_state(pdev);
  1366. adapter->ahw.crb_win = -1;
  1367. adapter->ahw.ocm_win = -1;
  1368. err = netxen_start_firmware(adapter);
  1369. if (err) {
  1370. dev_err(&pdev->dev, "failed to start firmware\n");
  1371. return err;
  1372. }
  1373. if (netif_running(netdev)) {
  1374. err = netxen_nic_attach(adapter);
  1375. if (err)
  1376. goto err_out;
  1377. err = netxen_nic_up(adapter, netdev);
  1378. if (err)
  1379. goto err_out_detach;
  1380. netxen_restore_indev_addr(netdev, NETDEV_UP);
  1381. }
  1382. netif_device_attach(netdev);
  1383. netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
  1384. return 0;
  1385. err_out_detach:
  1386. netxen_nic_detach(adapter);
  1387. err_out:
  1388. nx_decr_dev_ref_cnt(adapter);
  1389. return err;
  1390. }
  1391. static pci_ers_result_t netxen_io_error_detected(struct pci_dev *pdev,
  1392. pci_channel_state_t state)
  1393. {
  1394. struct netxen_adapter *adapter = pci_get_drvdata(pdev);
  1395. if (state == pci_channel_io_perm_failure)
  1396. return PCI_ERS_RESULT_DISCONNECT;
  1397. if (nx_dev_request_aer(adapter))
  1398. return PCI_ERS_RESULT_RECOVERED;
  1399. netxen_nic_detach_func(adapter);
  1400. pci_disable_device(pdev);
  1401. return PCI_ERS_RESULT_NEED_RESET;
  1402. }
  1403. static pci_ers_result_t netxen_io_slot_reset(struct pci_dev *pdev)
  1404. {
  1405. int err = 0;
  1406. err = netxen_nic_attach_func(pdev);
  1407. return err ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
  1408. }
  1409. static void netxen_io_resume(struct pci_dev *pdev)
  1410. {
  1411. pci_cleanup_aer_uncorrect_error_status(pdev);
  1412. }
  1413. static void netxen_nic_shutdown(struct pci_dev *pdev)
  1414. {
  1415. struct netxen_adapter *adapter = pci_get_drvdata(pdev);
  1416. netxen_nic_detach_func(adapter);
  1417. if (pci_save_state(pdev))
  1418. return;
  1419. if (netxen_nic_wol_supported(adapter)) {
  1420. pci_enable_wake(pdev, PCI_D3cold, 1);
  1421. pci_enable_wake(pdev, PCI_D3hot, 1);
  1422. }
  1423. pci_disable_device(pdev);
  1424. }
  1425. #ifdef CONFIG_PM
  1426. static int
  1427. netxen_nic_suspend(struct pci_dev *pdev, pm_message_t state)
  1428. {
  1429. struct netxen_adapter *adapter = pci_get_drvdata(pdev);
  1430. int retval;
  1431. netxen_nic_detach_func(adapter);
  1432. retval = pci_save_state(pdev);
  1433. if (retval)
  1434. return retval;
  1435. if (netxen_nic_wol_supported(adapter)) {
  1436. pci_enable_wake(pdev, PCI_D3cold, 1);
  1437. pci_enable_wake(pdev, PCI_D3hot, 1);
  1438. }
  1439. pci_disable_device(pdev);
  1440. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1441. return 0;
  1442. }
  1443. static int
  1444. netxen_nic_resume(struct pci_dev *pdev)
  1445. {
  1446. return netxen_nic_attach_func(pdev);
  1447. }
  1448. #endif
  1449. static int netxen_nic_open(struct net_device *netdev)
  1450. {
  1451. struct netxen_adapter *adapter = netdev_priv(netdev);
  1452. int err = 0;
  1453. if (adapter->driver_mismatch)
  1454. return -EIO;
  1455. err = netxen_nic_attach(adapter);
  1456. if (err)
  1457. return err;
  1458. err = __netxen_nic_up(adapter, netdev);
  1459. if (err)
  1460. goto err_out;
  1461. netif_start_queue(netdev);
  1462. return 0;
  1463. err_out:
  1464. netxen_nic_detach(adapter);
  1465. return err;
  1466. }
  1467. /*
  1468. * netxen_nic_close - Disables a network interface entry point
  1469. */
  1470. static int netxen_nic_close(struct net_device *netdev)
  1471. {
  1472. struct netxen_adapter *adapter = netdev_priv(netdev);
  1473. __netxen_nic_down(adapter, netdev);
  1474. return 0;
  1475. }
  1476. static void
  1477. netxen_tso_check(struct net_device *netdev,
  1478. struct nx_host_tx_ring *tx_ring,
  1479. struct cmd_desc_type0 *first_desc,
  1480. struct sk_buff *skb)
  1481. {
  1482. u8 opcode = TX_ETHER_PKT;
  1483. __be16 protocol = skb->protocol;
  1484. u16 flags = 0, vid = 0;
  1485. u32 producer;
  1486. int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
  1487. struct cmd_desc_type0 *hwdesc;
  1488. struct vlan_ethhdr *vh;
  1489. if (protocol == cpu_to_be16(ETH_P_8021Q)) {
  1490. vh = (struct vlan_ethhdr *)skb->data;
  1491. protocol = vh->h_vlan_encapsulated_proto;
  1492. flags = FLAGS_VLAN_TAGGED;
  1493. } else if (skb_vlan_tag_present(skb)) {
  1494. flags = FLAGS_VLAN_OOB;
  1495. vid = skb_vlan_tag_get(skb);
  1496. netxen_set_tx_vlan_tci(first_desc, vid);
  1497. vlan_oob = 1;
  1498. }
  1499. if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
  1500. skb_shinfo(skb)->gso_size > 0) {
  1501. hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1502. first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
  1503. first_desc->total_hdr_length = hdr_len;
  1504. if (vlan_oob) {
  1505. first_desc->total_hdr_length += VLAN_HLEN;
  1506. first_desc->tcp_hdr_offset = VLAN_HLEN;
  1507. first_desc->ip_hdr_offset = VLAN_HLEN;
  1508. /* Only in case of TSO on vlan device */
  1509. flags |= FLAGS_VLAN_TAGGED;
  1510. }
  1511. opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
  1512. TX_TCP_LSO6 : TX_TCP_LSO;
  1513. tso = 1;
  1514. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1515. u8 l4proto;
  1516. if (protocol == cpu_to_be16(ETH_P_IP)) {
  1517. l4proto = ip_hdr(skb)->protocol;
  1518. if (l4proto == IPPROTO_TCP)
  1519. opcode = TX_TCP_PKT;
  1520. else if(l4proto == IPPROTO_UDP)
  1521. opcode = TX_UDP_PKT;
  1522. } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
  1523. l4proto = ipv6_hdr(skb)->nexthdr;
  1524. if (l4proto == IPPROTO_TCP)
  1525. opcode = TX_TCPV6_PKT;
  1526. else if(l4proto == IPPROTO_UDP)
  1527. opcode = TX_UDPV6_PKT;
  1528. }
  1529. }
  1530. first_desc->tcp_hdr_offset += skb_transport_offset(skb);
  1531. first_desc->ip_hdr_offset += skb_network_offset(skb);
  1532. netxen_set_tx_flags_opcode(first_desc, flags, opcode);
  1533. if (!tso)
  1534. return;
  1535. /* For LSO, we need to copy the MAC/IP/TCP headers into
  1536. * the descriptor ring
  1537. */
  1538. producer = tx_ring->producer;
  1539. copied = 0;
  1540. offset = 2;
  1541. if (vlan_oob) {
  1542. /* Create a TSO vlan header template for firmware */
  1543. hwdesc = &tx_ring->desc_head[producer];
  1544. tx_ring->cmd_buf_arr[producer].skb = NULL;
  1545. copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
  1546. hdr_len + VLAN_HLEN);
  1547. vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
  1548. skb_copy_from_linear_data(skb, vh, 12);
  1549. vh->h_vlan_proto = htons(ETH_P_8021Q);
  1550. vh->h_vlan_TCI = htons(vid);
  1551. skb_copy_from_linear_data_offset(skb, 12,
  1552. (char *)vh + 16, copy_len - 16);
  1553. copied = copy_len - VLAN_HLEN;
  1554. offset = 0;
  1555. producer = get_next_index(producer, tx_ring->num_desc);
  1556. }
  1557. while (copied < hdr_len) {
  1558. copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
  1559. (hdr_len - copied));
  1560. hwdesc = &tx_ring->desc_head[producer];
  1561. tx_ring->cmd_buf_arr[producer].skb = NULL;
  1562. skb_copy_from_linear_data_offset(skb, copied,
  1563. (char *)hwdesc + offset, copy_len);
  1564. copied += copy_len;
  1565. offset = 0;
  1566. producer = get_next_index(producer, tx_ring->num_desc);
  1567. }
  1568. tx_ring->producer = producer;
  1569. barrier();
  1570. }
  1571. static int
  1572. netxen_map_tx_skb(struct pci_dev *pdev,
  1573. struct sk_buff *skb, struct netxen_cmd_buffer *pbuf)
  1574. {
  1575. struct netxen_skb_frag *nf;
  1576. struct skb_frag_struct *frag;
  1577. int i, nr_frags;
  1578. dma_addr_t map;
  1579. nr_frags = skb_shinfo(skb)->nr_frags;
  1580. nf = &pbuf->frag_array[0];
  1581. map = pci_map_single(pdev, skb->data,
  1582. skb_headlen(skb), PCI_DMA_TODEVICE);
  1583. if (pci_dma_mapping_error(pdev, map))
  1584. goto out_err;
  1585. nf->dma = map;
  1586. nf->length = skb_headlen(skb);
  1587. for (i = 0; i < nr_frags; i++) {
  1588. frag = &skb_shinfo(skb)->frags[i];
  1589. nf = &pbuf->frag_array[i+1];
  1590. map = skb_frag_dma_map(&pdev->dev, frag, 0, skb_frag_size(frag),
  1591. DMA_TO_DEVICE);
  1592. if (dma_mapping_error(&pdev->dev, map))
  1593. goto unwind;
  1594. nf->dma = map;
  1595. nf->length = skb_frag_size(frag);
  1596. }
  1597. return 0;
  1598. unwind:
  1599. while (--i >= 0) {
  1600. nf = &pbuf->frag_array[i+1];
  1601. pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
  1602. nf->dma = 0ULL;
  1603. }
  1604. nf = &pbuf->frag_array[0];
  1605. pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
  1606. nf->dma = 0ULL;
  1607. out_err:
  1608. return -ENOMEM;
  1609. }
  1610. static inline void
  1611. netxen_clear_cmddesc(u64 *desc)
  1612. {
  1613. desc[0] = 0ULL;
  1614. desc[2] = 0ULL;
  1615. }
  1616. static netdev_tx_t
  1617. netxen_nic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  1618. {
  1619. struct netxen_adapter *adapter = netdev_priv(netdev);
  1620. struct nx_host_tx_ring *tx_ring = adapter->tx_ring;
  1621. struct netxen_cmd_buffer *pbuf;
  1622. struct netxen_skb_frag *buffrag;
  1623. struct cmd_desc_type0 *hwdesc, *first_desc;
  1624. struct pci_dev *pdev;
  1625. int i, k;
  1626. int delta = 0;
  1627. struct skb_frag_struct *frag;
  1628. u32 producer;
  1629. int frag_count, no_of_desc;
  1630. u32 num_txd = tx_ring->num_desc;
  1631. frag_count = skb_shinfo(skb)->nr_frags + 1;
  1632. /* 14 frags supported for normal packet and
  1633. * 32 frags supported for TSO packet
  1634. */
  1635. if (!skb_is_gso(skb) && frag_count > NETXEN_MAX_FRAGS_PER_TX) {
  1636. for (i = 0; i < (frag_count - NETXEN_MAX_FRAGS_PER_TX); i++) {
  1637. frag = &skb_shinfo(skb)->frags[i];
  1638. delta += skb_frag_size(frag);
  1639. }
  1640. if (!__pskb_pull_tail(skb, delta))
  1641. goto drop_packet;
  1642. frag_count = 1 + skb_shinfo(skb)->nr_frags;
  1643. }
  1644. /* 4 fragments per cmd des */
  1645. no_of_desc = (frag_count + 3) >> 2;
  1646. if (unlikely(netxen_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
  1647. netif_stop_queue(netdev);
  1648. smp_mb();
  1649. if (netxen_tx_avail(tx_ring) > TX_STOP_THRESH)
  1650. netif_start_queue(netdev);
  1651. else
  1652. return NETDEV_TX_BUSY;
  1653. }
  1654. producer = tx_ring->producer;
  1655. pbuf = &tx_ring->cmd_buf_arr[producer];
  1656. pdev = adapter->pdev;
  1657. if (netxen_map_tx_skb(pdev, skb, pbuf))
  1658. goto drop_packet;
  1659. pbuf->skb = skb;
  1660. pbuf->frag_count = frag_count;
  1661. first_desc = hwdesc = &tx_ring->desc_head[producer];
  1662. netxen_clear_cmddesc((u64 *)hwdesc);
  1663. netxen_set_tx_frags_len(first_desc, frag_count, skb->len);
  1664. netxen_set_tx_port(first_desc, adapter->portnum);
  1665. for (i = 0; i < frag_count; i++) {
  1666. k = i % 4;
  1667. if ((k == 0) && (i > 0)) {
  1668. /* move to next desc.*/
  1669. producer = get_next_index(producer, num_txd);
  1670. hwdesc = &tx_ring->desc_head[producer];
  1671. netxen_clear_cmddesc((u64 *)hwdesc);
  1672. tx_ring->cmd_buf_arr[producer].skb = NULL;
  1673. }
  1674. buffrag = &pbuf->frag_array[i];
  1675. hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
  1676. switch (k) {
  1677. case 0:
  1678. hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
  1679. break;
  1680. case 1:
  1681. hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
  1682. break;
  1683. case 2:
  1684. hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
  1685. break;
  1686. case 3:
  1687. hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
  1688. break;
  1689. }
  1690. }
  1691. tx_ring->producer = get_next_index(producer, num_txd);
  1692. netxen_tso_check(netdev, tx_ring, first_desc, skb);
  1693. adapter->stats.txbytes += skb->len;
  1694. adapter->stats.xmitcalled++;
  1695. netxen_nic_update_cmd_producer(adapter, tx_ring);
  1696. return NETDEV_TX_OK;
  1697. drop_packet:
  1698. adapter->stats.txdropped++;
  1699. dev_kfree_skb_any(skb);
  1700. return NETDEV_TX_OK;
  1701. }
  1702. static int netxen_nic_check_temp(struct netxen_adapter *adapter)
  1703. {
  1704. struct net_device *netdev = adapter->netdev;
  1705. uint32_t temp, temp_state, temp_val;
  1706. int rv = 0;
  1707. temp = NXRD32(adapter, CRB_TEMP_STATE);
  1708. temp_state = nx_get_temp_state(temp);
  1709. temp_val = nx_get_temp_val(temp);
  1710. if (temp_state == NX_TEMP_PANIC) {
  1711. printk(KERN_ALERT
  1712. "%s: Device temperature %d degrees C exceeds"
  1713. " maximum allowed. Hardware has been shut down.\n",
  1714. netdev->name, temp_val);
  1715. rv = 1;
  1716. } else if (temp_state == NX_TEMP_WARN) {
  1717. if (adapter->temp == NX_TEMP_NORMAL) {
  1718. printk(KERN_ALERT
  1719. "%s: Device temperature %d degrees C "
  1720. "exceeds operating range."
  1721. " Immediate action needed.\n",
  1722. netdev->name, temp_val);
  1723. }
  1724. } else {
  1725. if (adapter->temp == NX_TEMP_WARN) {
  1726. printk(KERN_INFO
  1727. "%s: Device temperature is now %d degrees C"
  1728. " in normal range.\n", netdev->name,
  1729. temp_val);
  1730. }
  1731. }
  1732. adapter->temp = temp_state;
  1733. return rv;
  1734. }
  1735. void netxen_advert_link_change(struct netxen_adapter *adapter, int linkup)
  1736. {
  1737. struct net_device *netdev = adapter->netdev;
  1738. if (adapter->ahw.linkup && !linkup) {
  1739. printk(KERN_INFO "%s: %s NIC Link is down\n",
  1740. netxen_nic_driver_name, netdev->name);
  1741. adapter->ahw.linkup = 0;
  1742. if (netif_running(netdev)) {
  1743. netif_carrier_off(netdev);
  1744. netif_stop_queue(netdev);
  1745. }
  1746. adapter->link_changed = !adapter->has_link_events;
  1747. } else if (!adapter->ahw.linkup && linkup) {
  1748. printk(KERN_INFO "%s: %s NIC Link is up\n",
  1749. netxen_nic_driver_name, netdev->name);
  1750. adapter->ahw.linkup = 1;
  1751. if (netif_running(netdev)) {
  1752. netif_carrier_on(netdev);
  1753. netif_wake_queue(netdev);
  1754. }
  1755. adapter->link_changed = !adapter->has_link_events;
  1756. }
  1757. }
  1758. static void netxen_nic_handle_phy_intr(struct netxen_adapter *adapter)
  1759. {
  1760. u32 val, port, linkup;
  1761. port = adapter->physical_port;
  1762. if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
  1763. val = NXRD32(adapter, CRB_XG_STATE_P3);
  1764. val = XG_LINK_STATE_P3(adapter->ahw.pci_func, val);
  1765. linkup = (val == XG_LINK_UP_P3);
  1766. } else {
  1767. val = NXRD32(adapter, CRB_XG_STATE);
  1768. val = (val >> port*8) & 0xff;
  1769. linkup = (val == XG_LINK_UP);
  1770. }
  1771. netxen_advert_link_change(adapter, linkup);
  1772. }
  1773. static void netxen_tx_timeout(struct net_device *netdev)
  1774. {
  1775. struct netxen_adapter *adapter = netdev_priv(netdev);
  1776. if (test_bit(__NX_RESETTING, &adapter->state))
  1777. return;
  1778. dev_err(&netdev->dev, "transmit timeout, resetting.\n");
  1779. schedule_work(&adapter->tx_timeout_task);
  1780. }
  1781. static void netxen_tx_timeout_task(struct work_struct *work)
  1782. {
  1783. struct netxen_adapter *adapter =
  1784. container_of(work, struct netxen_adapter, tx_timeout_task);
  1785. if (!netif_running(adapter->netdev))
  1786. return;
  1787. if (test_and_set_bit(__NX_RESETTING, &adapter->state))
  1788. return;
  1789. if (++adapter->tx_timeo_cnt >= NX_MAX_TX_TIMEOUTS)
  1790. goto request_reset;
  1791. rtnl_lock();
  1792. if (NX_IS_REVISION_P2(adapter->ahw.revision_id)) {
  1793. /* try to scrub interrupt */
  1794. netxen_napi_disable(adapter);
  1795. netxen_napi_enable(adapter);
  1796. netif_wake_queue(adapter->netdev);
  1797. clear_bit(__NX_RESETTING, &adapter->state);
  1798. } else {
  1799. clear_bit(__NX_RESETTING, &adapter->state);
  1800. if (netxen_nic_reset_context(adapter)) {
  1801. rtnl_unlock();
  1802. goto request_reset;
  1803. }
  1804. }
  1805. adapter->netdev->trans_start = jiffies;
  1806. rtnl_unlock();
  1807. return;
  1808. request_reset:
  1809. adapter->need_fw_reset = 1;
  1810. clear_bit(__NX_RESETTING, &adapter->state);
  1811. }
  1812. static struct rtnl_link_stats64 *netxen_nic_get_stats(struct net_device *netdev,
  1813. struct rtnl_link_stats64 *stats)
  1814. {
  1815. struct netxen_adapter *adapter = netdev_priv(netdev);
  1816. stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
  1817. stats->tx_packets = adapter->stats.xmitfinished;
  1818. stats->rx_bytes = adapter->stats.rxbytes;
  1819. stats->tx_bytes = adapter->stats.txbytes;
  1820. stats->rx_dropped = adapter->stats.rxdropped;
  1821. stats->tx_dropped = adapter->stats.txdropped;
  1822. return stats;
  1823. }
  1824. static irqreturn_t netxen_intr(int irq, void *data)
  1825. {
  1826. struct nx_host_sds_ring *sds_ring = data;
  1827. struct netxen_adapter *adapter = sds_ring->adapter;
  1828. u32 status = 0;
  1829. status = readl(adapter->isr_int_vec);
  1830. if (!(status & adapter->int_vec_bit))
  1831. return IRQ_NONE;
  1832. if (NX_IS_REVISION_P3(adapter->ahw.revision_id)) {
  1833. /* check interrupt state machine, to be sure */
  1834. status = readl(adapter->crb_int_state_reg);
  1835. if (!ISR_LEGACY_INT_TRIGGERED(status))
  1836. return IRQ_NONE;
  1837. } else {
  1838. unsigned long our_int = 0;
  1839. our_int = readl(adapter->crb_int_state_reg);
  1840. /* not our interrupt */
  1841. if (!test_and_clear_bit((7 + adapter->portnum), &our_int))
  1842. return IRQ_NONE;
  1843. /* claim interrupt */
  1844. writel((our_int & 0xffffffff), adapter->crb_int_state_reg);
  1845. /* clear interrupt */
  1846. netxen_nic_disable_int(sds_ring);
  1847. }
  1848. writel(0xffffffff, adapter->tgt_status_reg);
  1849. /* read twice to ensure write is flushed */
  1850. readl(adapter->isr_int_vec);
  1851. readl(adapter->isr_int_vec);
  1852. napi_schedule(&sds_ring->napi);
  1853. return IRQ_HANDLED;
  1854. }
  1855. static irqreturn_t netxen_msi_intr(int irq, void *data)
  1856. {
  1857. struct nx_host_sds_ring *sds_ring = data;
  1858. struct netxen_adapter *adapter = sds_ring->adapter;
  1859. /* clear interrupt */
  1860. writel(0xffffffff, adapter->tgt_status_reg);
  1861. napi_schedule(&sds_ring->napi);
  1862. return IRQ_HANDLED;
  1863. }
  1864. static irqreturn_t netxen_msix_intr(int irq, void *data)
  1865. {
  1866. struct nx_host_sds_ring *sds_ring = data;
  1867. napi_schedule(&sds_ring->napi);
  1868. return IRQ_HANDLED;
  1869. }
  1870. static int netxen_nic_poll(struct napi_struct *napi, int budget)
  1871. {
  1872. struct nx_host_sds_ring *sds_ring =
  1873. container_of(napi, struct nx_host_sds_ring, napi);
  1874. struct netxen_adapter *adapter = sds_ring->adapter;
  1875. int tx_complete;
  1876. int work_done;
  1877. tx_complete = netxen_process_cmd_ring(adapter);
  1878. work_done = netxen_process_rcv_ring(sds_ring, budget);
  1879. if (!tx_complete)
  1880. work_done = budget;
  1881. if (work_done < budget) {
  1882. napi_complete(&sds_ring->napi);
  1883. if (test_bit(__NX_DEV_UP, &adapter->state))
  1884. netxen_nic_enable_int(sds_ring);
  1885. }
  1886. return work_done;
  1887. }
  1888. #ifdef CONFIG_NET_POLL_CONTROLLER
  1889. static void netxen_nic_poll_controller(struct net_device *netdev)
  1890. {
  1891. int ring;
  1892. struct nx_host_sds_ring *sds_ring;
  1893. struct netxen_adapter *adapter = netdev_priv(netdev);
  1894. struct netxen_recv_context *recv_ctx = &adapter->recv_ctx;
  1895. disable_irq(adapter->irq);
  1896. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  1897. sds_ring = &recv_ctx->sds_rings[ring];
  1898. netxen_intr(adapter->irq, sds_ring);
  1899. }
  1900. enable_irq(adapter->irq);
  1901. }
  1902. #endif
  1903. static int
  1904. nx_incr_dev_ref_cnt(struct netxen_adapter *adapter)
  1905. {
  1906. int count;
  1907. if (netxen_api_lock(adapter))
  1908. return -EIO;
  1909. count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
  1910. NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
  1911. netxen_api_unlock(adapter);
  1912. return count;
  1913. }
  1914. static int
  1915. nx_decr_dev_ref_cnt(struct netxen_adapter *adapter)
  1916. {
  1917. int count, state;
  1918. if (netxen_api_lock(adapter))
  1919. return -EIO;
  1920. count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
  1921. WARN_ON(count == 0);
  1922. NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
  1923. state = NXRD32(adapter, NX_CRB_DEV_STATE);
  1924. if (count == 0 && state != NX_DEV_FAILED)
  1925. NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
  1926. netxen_api_unlock(adapter);
  1927. return count;
  1928. }
  1929. static int
  1930. nx_dev_request_aer(struct netxen_adapter *adapter)
  1931. {
  1932. u32 state;
  1933. int ret = -EINVAL;
  1934. if (netxen_api_lock(adapter))
  1935. return ret;
  1936. state = NXRD32(adapter, NX_CRB_DEV_STATE);
  1937. if (state == NX_DEV_NEED_AER)
  1938. ret = 0;
  1939. else if (state == NX_DEV_READY) {
  1940. NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_AER);
  1941. ret = 0;
  1942. }
  1943. netxen_api_unlock(adapter);
  1944. return ret;
  1945. }
  1946. int
  1947. nx_dev_request_reset(struct netxen_adapter *adapter)
  1948. {
  1949. u32 state;
  1950. int ret = -EINVAL;
  1951. if (netxen_api_lock(adapter))
  1952. return ret;
  1953. state = NXRD32(adapter, NX_CRB_DEV_STATE);
  1954. if (state == NX_DEV_NEED_RESET || state == NX_DEV_FAILED)
  1955. ret = 0;
  1956. else if (state != NX_DEV_INITALIZING && state != NX_DEV_NEED_AER) {
  1957. NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_NEED_RESET);
  1958. adapter->flags |= NETXEN_FW_RESET_OWNER;
  1959. ret = 0;
  1960. }
  1961. netxen_api_unlock(adapter);
  1962. return ret;
  1963. }
  1964. static int
  1965. netxen_can_start_firmware(struct netxen_adapter *adapter)
  1966. {
  1967. int count;
  1968. int can_start = 0;
  1969. if (netxen_api_lock(adapter)) {
  1970. nx_incr_dev_ref_cnt(adapter);
  1971. return -1;
  1972. }
  1973. count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
  1974. if ((count < 0) || (count >= NX_MAX_PCI_FUNC))
  1975. count = 0;
  1976. if (count == 0) {
  1977. can_start = 1;
  1978. NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_INITALIZING);
  1979. }
  1980. NXWR32(adapter, NX_CRB_DEV_REF_COUNT, ++count);
  1981. netxen_api_unlock(adapter);
  1982. return can_start;
  1983. }
  1984. static void
  1985. netxen_schedule_work(struct netxen_adapter *adapter,
  1986. work_func_t func, int delay)
  1987. {
  1988. INIT_DELAYED_WORK(&adapter->fw_work, func);
  1989. schedule_delayed_work(&adapter->fw_work, delay);
  1990. }
  1991. static void
  1992. netxen_cancel_fw_work(struct netxen_adapter *adapter)
  1993. {
  1994. while (test_and_set_bit(__NX_RESETTING, &adapter->state))
  1995. msleep(10);
  1996. cancel_delayed_work_sync(&adapter->fw_work);
  1997. }
  1998. static void
  1999. netxen_attach_work(struct work_struct *work)
  2000. {
  2001. struct netxen_adapter *adapter = container_of(work,
  2002. struct netxen_adapter, fw_work.work);
  2003. struct net_device *netdev = adapter->netdev;
  2004. int err = 0;
  2005. if (netif_running(netdev)) {
  2006. err = netxen_nic_attach(adapter);
  2007. if (err)
  2008. goto done;
  2009. err = netxen_nic_up(adapter, netdev);
  2010. if (err) {
  2011. netxen_nic_detach(adapter);
  2012. goto done;
  2013. }
  2014. netxen_restore_indev_addr(netdev, NETDEV_UP);
  2015. }
  2016. netif_device_attach(netdev);
  2017. done:
  2018. adapter->fw_fail_cnt = 0;
  2019. clear_bit(__NX_RESETTING, &adapter->state);
  2020. netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
  2021. }
  2022. static void
  2023. netxen_fwinit_work(struct work_struct *work)
  2024. {
  2025. struct netxen_adapter *adapter = container_of(work,
  2026. struct netxen_adapter, fw_work.work);
  2027. int dev_state;
  2028. int count;
  2029. dev_state = NXRD32(adapter, NX_CRB_DEV_STATE);
  2030. if (adapter->flags & NETXEN_FW_RESET_OWNER) {
  2031. count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
  2032. WARN_ON(count == 0);
  2033. if (count == 1) {
  2034. if (adapter->mdump.md_enabled) {
  2035. rtnl_lock();
  2036. netxen_dump_fw(adapter);
  2037. rtnl_unlock();
  2038. }
  2039. adapter->flags &= ~NETXEN_FW_RESET_OWNER;
  2040. if (netxen_api_lock(adapter)) {
  2041. clear_bit(__NX_RESETTING, &adapter->state);
  2042. NXWR32(adapter, NX_CRB_DEV_STATE,
  2043. NX_DEV_FAILED);
  2044. return;
  2045. }
  2046. count = NXRD32(adapter, NX_CRB_DEV_REF_COUNT);
  2047. NXWR32(adapter, NX_CRB_DEV_REF_COUNT, --count);
  2048. NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_COLD);
  2049. dev_state = NX_DEV_COLD;
  2050. netxen_api_unlock(adapter);
  2051. }
  2052. }
  2053. switch (dev_state) {
  2054. case NX_DEV_COLD:
  2055. case NX_DEV_READY:
  2056. if (!netxen_start_firmware(adapter)) {
  2057. netxen_schedule_work(adapter, netxen_attach_work, 0);
  2058. return;
  2059. }
  2060. break;
  2061. case NX_DEV_NEED_RESET:
  2062. case NX_DEV_INITALIZING:
  2063. netxen_schedule_work(adapter,
  2064. netxen_fwinit_work, 2 * FW_POLL_DELAY);
  2065. return;
  2066. case NX_DEV_FAILED:
  2067. default:
  2068. nx_incr_dev_ref_cnt(adapter);
  2069. break;
  2070. }
  2071. if (netxen_api_lock(adapter)) {
  2072. clear_bit(__NX_RESETTING, &adapter->state);
  2073. return;
  2074. }
  2075. NXWR32(adapter, NX_CRB_DEV_STATE, NX_DEV_FAILED);
  2076. netxen_api_unlock(adapter);
  2077. dev_err(&adapter->pdev->dev, "%s: Device initialization Failed\n",
  2078. adapter->netdev->name);
  2079. clear_bit(__NX_RESETTING, &adapter->state);
  2080. }
  2081. static void
  2082. netxen_detach_work(struct work_struct *work)
  2083. {
  2084. struct netxen_adapter *adapter = container_of(work,
  2085. struct netxen_adapter, fw_work.work);
  2086. struct net_device *netdev = adapter->netdev;
  2087. int ref_cnt = 0, delay;
  2088. u32 status;
  2089. netif_device_detach(netdev);
  2090. netxen_nic_down(adapter, netdev);
  2091. rtnl_lock();
  2092. netxen_nic_detach(adapter);
  2093. rtnl_unlock();
  2094. status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
  2095. if (status & NX_RCODE_FATAL_ERROR)
  2096. goto err_ret;
  2097. if (adapter->temp == NX_TEMP_PANIC)
  2098. goto err_ret;
  2099. if (!(adapter->flags & NETXEN_FW_RESET_OWNER))
  2100. ref_cnt = nx_decr_dev_ref_cnt(adapter);
  2101. if (ref_cnt == -EIO)
  2102. goto err_ret;
  2103. delay = (ref_cnt == 0) ? 0 : (2 * FW_POLL_DELAY);
  2104. adapter->fw_wait_cnt = 0;
  2105. netxen_schedule_work(adapter, netxen_fwinit_work, delay);
  2106. return;
  2107. err_ret:
  2108. clear_bit(__NX_RESETTING, &adapter->state);
  2109. }
  2110. static int
  2111. netxen_check_health(struct netxen_adapter *adapter)
  2112. {
  2113. u32 state, heartbit;
  2114. u32 peg_status;
  2115. struct net_device *netdev = adapter->netdev;
  2116. state = NXRD32(adapter, NX_CRB_DEV_STATE);
  2117. if (state == NX_DEV_NEED_AER)
  2118. return 0;
  2119. if (netxen_nic_check_temp(adapter))
  2120. goto detach;
  2121. if (adapter->need_fw_reset) {
  2122. if (nx_dev_request_reset(adapter))
  2123. return 0;
  2124. goto detach;
  2125. }
  2126. /* NX_DEV_NEED_RESET, this state can be marked in two cases
  2127. * 1. Tx timeout 2. Fw hang
  2128. * Send request to destroy context in case of tx timeout only
  2129. * and doesn't required in case of Fw hang
  2130. */
  2131. if (state == NX_DEV_NEED_RESET || state == NX_DEV_FAILED) {
  2132. adapter->need_fw_reset = 1;
  2133. if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
  2134. goto detach;
  2135. }
  2136. if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
  2137. return 0;
  2138. heartbit = NXRD32(adapter, NETXEN_PEG_ALIVE_COUNTER);
  2139. if (heartbit != adapter->heartbit) {
  2140. adapter->heartbit = heartbit;
  2141. adapter->fw_fail_cnt = 0;
  2142. if (adapter->need_fw_reset)
  2143. goto detach;
  2144. return 0;
  2145. }
  2146. if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
  2147. return 0;
  2148. if (nx_dev_request_reset(adapter))
  2149. return 0;
  2150. clear_bit(__NX_FW_ATTACHED, &adapter->state);
  2151. dev_err(&netdev->dev, "firmware hang detected\n");
  2152. peg_status = NXRD32(adapter, NETXEN_PEG_HALT_STATUS1);
  2153. dev_err(&adapter->pdev->dev, "Dumping hw/fw registers\n"
  2154. "PEG_HALT_STATUS1: 0x%x, PEG_HALT_STATUS2: 0x%x,\n"
  2155. "PEG_NET_0_PC: 0x%x, PEG_NET_1_PC: 0x%x,\n"
  2156. "PEG_NET_2_PC: 0x%x, PEG_NET_3_PC: 0x%x,\n"
  2157. "PEG_NET_4_PC: 0x%x\n",
  2158. peg_status,
  2159. NXRD32(adapter, NETXEN_PEG_HALT_STATUS2),
  2160. NXRD32(adapter, NETXEN_CRB_PEG_NET_0 + 0x3c),
  2161. NXRD32(adapter, NETXEN_CRB_PEG_NET_1 + 0x3c),
  2162. NXRD32(adapter, NETXEN_CRB_PEG_NET_2 + 0x3c),
  2163. NXRD32(adapter, NETXEN_CRB_PEG_NET_3 + 0x3c),
  2164. NXRD32(adapter, NETXEN_CRB_PEG_NET_4 + 0x3c));
  2165. if (NX_FWERROR_PEGSTAT1(peg_status) == 0x67)
  2166. dev_err(&adapter->pdev->dev,
  2167. "Firmware aborted with error code 0x00006700. "
  2168. "Device is being reset.\n");
  2169. detach:
  2170. if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) &&
  2171. !test_and_set_bit(__NX_RESETTING, &adapter->state))
  2172. netxen_schedule_work(adapter, netxen_detach_work, 0);
  2173. return 1;
  2174. }
  2175. static void
  2176. netxen_fw_poll_work(struct work_struct *work)
  2177. {
  2178. struct netxen_adapter *adapter = container_of(work,
  2179. struct netxen_adapter, fw_work.work);
  2180. if (test_bit(__NX_RESETTING, &adapter->state))
  2181. goto reschedule;
  2182. if (test_bit(__NX_DEV_UP, &adapter->state) &&
  2183. !(adapter->capabilities & NX_FW_CAPABILITY_LINK_NOTIFICATION)) {
  2184. if (!adapter->has_link_events) {
  2185. netxen_nic_handle_phy_intr(adapter);
  2186. if (adapter->link_changed)
  2187. netxen_nic_set_link_parameters(adapter);
  2188. }
  2189. }
  2190. if (netxen_check_health(adapter))
  2191. return;
  2192. reschedule:
  2193. netxen_schedule_work(adapter, netxen_fw_poll_work, FW_POLL_DELAY);
  2194. }
  2195. static ssize_t
  2196. netxen_store_bridged_mode(struct device *dev,
  2197. struct device_attribute *attr, const char *buf, size_t len)
  2198. {
  2199. struct net_device *net = to_net_dev(dev);
  2200. struct netxen_adapter *adapter = netdev_priv(net);
  2201. unsigned long new;
  2202. int ret = -EINVAL;
  2203. if (!(adapter->capabilities & NX_FW_CAPABILITY_BDG))
  2204. goto err_out;
  2205. if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
  2206. goto err_out;
  2207. if (kstrtoul(buf, 2, &new))
  2208. goto err_out;
  2209. if (!netxen_config_bridged_mode(adapter, !!new))
  2210. ret = len;
  2211. err_out:
  2212. return ret;
  2213. }
  2214. static ssize_t
  2215. netxen_show_bridged_mode(struct device *dev,
  2216. struct device_attribute *attr, char *buf)
  2217. {
  2218. struct net_device *net = to_net_dev(dev);
  2219. struct netxen_adapter *adapter;
  2220. int bridged_mode = 0;
  2221. adapter = netdev_priv(net);
  2222. if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
  2223. bridged_mode = !!(adapter->flags & NETXEN_NIC_BRIDGE_ENABLED);
  2224. return sprintf(buf, "%d\n", bridged_mode);
  2225. }
  2226. static struct device_attribute dev_attr_bridged_mode = {
  2227. .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
  2228. .show = netxen_show_bridged_mode,
  2229. .store = netxen_store_bridged_mode,
  2230. };
  2231. static ssize_t
  2232. netxen_store_diag_mode(struct device *dev,
  2233. struct device_attribute *attr, const char *buf, size_t len)
  2234. {
  2235. struct netxen_adapter *adapter = dev_get_drvdata(dev);
  2236. unsigned long new;
  2237. if (kstrtoul(buf, 2, &new))
  2238. return -EINVAL;
  2239. if (!!new != !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
  2240. adapter->flags ^= NETXEN_NIC_DIAG_ENABLED;
  2241. return len;
  2242. }
  2243. static ssize_t
  2244. netxen_show_diag_mode(struct device *dev,
  2245. struct device_attribute *attr, char *buf)
  2246. {
  2247. struct netxen_adapter *adapter = dev_get_drvdata(dev);
  2248. return sprintf(buf, "%d\n",
  2249. !!(adapter->flags & NETXEN_NIC_DIAG_ENABLED));
  2250. }
  2251. static struct device_attribute dev_attr_diag_mode = {
  2252. .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
  2253. .show = netxen_show_diag_mode,
  2254. .store = netxen_store_diag_mode,
  2255. };
  2256. static int
  2257. netxen_sysfs_validate_crb(struct netxen_adapter *adapter,
  2258. loff_t offset, size_t size)
  2259. {
  2260. size_t crb_size = 4;
  2261. if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
  2262. return -EIO;
  2263. if (offset < NETXEN_PCI_CRBSPACE) {
  2264. if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
  2265. return -EINVAL;
  2266. if (ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
  2267. NETXEN_PCI_CAMQM_2M_END))
  2268. crb_size = 8;
  2269. else
  2270. return -EINVAL;
  2271. }
  2272. if ((size != crb_size) || (offset & (crb_size-1)))
  2273. return -EINVAL;
  2274. return 0;
  2275. }
  2276. static ssize_t
  2277. netxen_sysfs_read_crb(struct file *filp, struct kobject *kobj,
  2278. struct bin_attribute *attr,
  2279. char *buf, loff_t offset, size_t size)
  2280. {
  2281. struct device *dev = container_of(kobj, struct device, kobj);
  2282. struct netxen_adapter *adapter = dev_get_drvdata(dev);
  2283. u32 data;
  2284. u64 qmdata;
  2285. int ret;
  2286. ret = netxen_sysfs_validate_crb(adapter, offset, size);
  2287. if (ret != 0)
  2288. return ret;
  2289. if (NX_IS_REVISION_P3(adapter->ahw.revision_id) &&
  2290. ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
  2291. NETXEN_PCI_CAMQM_2M_END)) {
  2292. netxen_pci_camqm_read_2M(adapter, offset, &qmdata);
  2293. memcpy(buf, &qmdata, size);
  2294. } else {
  2295. data = NXRD32(adapter, offset);
  2296. memcpy(buf, &data, size);
  2297. }
  2298. return size;
  2299. }
  2300. static ssize_t
  2301. netxen_sysfs_write_crb(struct file *filp, struct kobject *kobj,
  2302. struct bin_attribute *attr,
  2303. char *buf, loff_t offset, size_t size)
  2304. {
  2305. struct device *dev = container_of(kobj, struct device, kobj);
  2306. struct netxen_adapter *adapter = dev_get_drvdata(dev);
  2307. u32 data;
  2308. u64 qmdata;
  2309. int ret;
  2310. ret = netxen_sysfs_validate_crb(adapter, offset, size);
  2311. if (ret != 0)
  2312. return ret;
  2313. if (NX_IS_REVISION_P3(adapter->ahw.revision_id) &&
  2314. ADDR_IN_RANGE(offset, NETXEN_PCI_CAMQM,
  2315. NETXEN_PCI_CAMQM_2M_END)) {
  2316. memcpy(&qmdata, buf, size);
  2317. netxen_pci_camqm_write_2M(adapter, offset, qmdata);
  2318. } else {
  2319. memcpy(&data, buf, size);
  2320. NXWR32(adapter, offset, data);
  2321. }
  2322. return size;
  2323. }
  2324. static int
  2325. netxen_sysfs_validate_mem(struct netxen_adapter *adapter,
  2326. loff_t offset, size_t size)
  2327. {
  2328. if (!(adapter->flags & NETXEN_NIC_DIAG_ENABLED))
  2329. return -EIO;
  2330. if ((size != 8) || (offset & 0x7))
  2331. return -EIO;
  2332. return 0;
  2333. }
  2334. static ssize_t
  2335. netxen_sysfs_read_mem(struct file *filp, struct kobject *kobj,
  2336. struct bin_attribute *attr,
  2337. char *buf, loff_t offset, size_t size)
  2338. {
  2339. struct device *dev = container_of(kobj, struct device, kobj);
  2340. struct netxen_adapter *adapter = dev_get_drvdata(dev);
  2341. u64 data;
  2342. int ret;
  2343. ret = netxen_sysfs_validate_mem(adapter, offset, size);
  2344. if (ret != 0)
  2345. return ret;
  2346. if (adapter->pci_mem_read(adapter, offset, &data))
  2347. return -EIO;
  2348. memcpy(buf, &data, size);
  2349. return size;
  2350. }
  2351. static ssize_t netxen_sysfs_write_mem(struct file *filp, struct kobject *kobj,
  2352. struct bin_attribute *attr, char *buf,
  2353. loff_t offset, size_t size)
  2354. {
  2355. struct device *dev = container_of(kobj, struct device, kobj);
  2356. struct netxen_adapter *adapter = dev_get_drvdata(dev);
  2357. u64 data;
  2358. int ret;
  2359. ret = netxen_sysfs_validate_mem(adapter, offset, size);
  2360. if (ret != 0)
  2361. return ret;
  2362. memcpy(&data, buf, size);
  2363. if (adapter->pci_mem_write(adapter, offset, data))
  2364. return -EIO;
  2365. return size;
  2366. }
  2367. static struct bin_attribute bin_attr_crb = {
  2368. .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
  2369. .size = 0,
  2370. .read = netxen_sysfs_read_crb,
  2371. .write = netxen_sysfs_write_crb,
  2372. };
  2373. static struct bin_attribute bin_attr_mem = {
  2374. .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
  2375. .size = 0,
  2376. .read = netxen_sysfs_read_mem,
  2377. .write = netxen_sysfs_write_mem,
  2378. };
  2379. static ssize_t
  2380. netxen_sysfs_read_dimm(struct file *filp, struct kobject *kobj,
  2381. struct bin_attribute *attr,
  2382. char *buf, loff_t offset, size_t size)
  2383. {
  2384. struct device *dev = container_of(kobj, struct device, kobj);
  2385. struct netxen_adapter *adapter = dev_get_drvdata(dev);
  2386. struct net_device *netdev = adapter->netdev;
  2387. struct netxen_dimm_cfg dimm;
  2388. u8 dw, rows, cols, banks, ranks;
  2389. u32 val;
  2390. if (size < attr->size) {
  2391. netdev_err(netdev, "Invalid size\n");
  2392. return -EINVAL;
  2393. }
  2394. memset(&dimm, 0, sizeof(struct netxen_dimm_cfg));
  2395. val = NXRD32(adapter, NETXEN_DIMM_CAPABILITY);
  2396. /* Checks if DIMM info is valid. */
  2397. if (val & NETXEN_DIMM_VALID_FLAG) {
  2398. netdev_err(netdev, "Invalid DIMM flag\n");
  2399. dimm.presence = 0xff;
  2400. goto out;
  2401. }
  2402. rows = NETXEN_DIMM_NUMROWS(val);
  2403. cols = NETXEN_DIMM_NUMCOLS(val);
  2404. ranks = NETXEN_DIMM_NUMRANKS(val);
  2405. banks = NETXEN_DIMM_NUMBANKS(val);
  2406. dw = NETXEN_DIMM_DATAWIDTH(val);
  2407. dimm.presence = (val & NETXEN_DIMM_PRESENT);
  2408. /* Checks if DIMM info is present. */
  2409. if (!dimm.presence) {
  2410. netdev_err(netdev, "DIMM not present\n");
  2411. goto out;
  2412. }
  2413. dimm.dimm_type = NETXEN_DIMM_TYPE(val);
  2414. switch (dimm.dimm_type) {
  2415. case NETXEN_DIMM_TYPE_RDIMM:
  2416. case NETXEN_DIMM_TYPE_UDIMM:
  2417. case NETXEN_DIMM_TYPE_SO_DIMM:
  2418. case NETXEN_DIMM_TYPE_Micro_DIMM:
  2419. case NETXEN_DIMM_TYPE_Mini_RDIMM:
  2420. case NETXEN_DIMM_TYPE_Mini_UDIMM:
  2421. break;
  2422. default:
  2423. netdev_err(netdev, "Invalid DIMM type %x\n", dimm.dimm_type);
  2424. goto out;
  2425. }
  2426. if (val & NETXEN_DIMM_MEMTYPE_DDR2_SDRAM)
  2427. dimm.mem_type = NETXEN_DIMM_MEM_DDR2_SDRAM;
  2428. else
  2429. dimm.mem_type = NETXEN_DIMM_MEMTYPE(val);
  2430. if (val & NETXEN_DIMM_SIZE) {
  2431. dimm.size = NETXEN_DIMM_STD_MEM_SIZE;
  2432. goto out;
  2433. }
  2434. if (!rows) {
  2435. netdev_err(netdev, "Invalid no of rows %x\n", rows);
  2436. goto out;
  2437. }
  2438. if (!cols) {
  2439. netdev_err(netdev, "Invalid no of columns %x\n", cols);
  2440. goto out;
  2441. }
  2442. if (!banks) {
  2443. netdev_err(netdev, "Invalid no of banks %x\n", banks);
  2444. goto out;
  2445. }
  2446. ranks += 1;
  2447. switch (dw) {
  2448. case 0x0:
  2449. dw = 32;
  2450. break;
  2451. case 0x1:
  2452. dw = 33;
  2453. break;
  2454. case 0x2:
  2455. dw = 36;
  2456. break;
  2457. case 0x3:
  2458. dw = 64;
  2459. break;
  2460. case 0x4:
  2461. dw = 72;
  2462. break;
  2463. case 0x5:
  2464. dw = 80;
  2465. break;
  2466. case 0x6:
  2467. dw = 128;
  2468. break;
  2469. case 0x7:
  2470. dw = 144;
  2471. break;
  2472. default:
  2473. netdev_err(netdev, "Invalid data-width %x\n", dw);
  2474. goto out;
  2475. }
  2476. dimm.size = ((1 << rows) * (1 << cols) * dw * banks * ranks) / 8;
  2477. /* Size returned in MB. */
  2478. dimm.size = (dimm.size) / 0x100000;
  2479. out:
  2480. memcpy(buf, &dimm, sizeof(struct netxen_dimm_cfg));
  2481. return sizeof(struct netxen_dimm_cfg);
  2482. }
  2483. static struct bin_attribute bin_attr_dimm = {
  2484. .attr = { .name = "dimm", .mode = (S_IRUGO | S_IWUSR) },
  2485. .size = sizeof(struct netxen_dimm_cfg),
  2486. .read = netxen_sysfs_read_dimm,
  2487. };
  2488. static void
  2489. netxen_create_sysfs_entries(struct netxen_adapter *adapter)
  2490. {
  2491. struct device *dev = &adapter->pdev->dev;
  2492. if (adapter->capabilities & NX_FW_CAPABILITY_BDG) {
  2493. /* bridged_mode control */
  2494. if (device_create_file(dev, &dev_attr_bridged_mode)) {
  2495. dev_warn(dev,
  2496. "failed to create bridged_mode sysfs entry\n");
  2497. }
  2498. }
  2499. }
  2500. static void
  2501. netxen_remove_sysfs_entries(struct netxen_adapter *adapter)
  2502. {
  2503. struct device *dev = &adapter->pdev->dev;
  2504. if (adapter->capabilities & NX_FW_CAPABILITY_BDG)
  2505. device_remove_file(dev, &dev_attr_bridged_mode);
  2506. }
  2507. static void
  2508. netxen_create_diag_entries(struct netxen_adapter *adapter)
  2509. {
  2510. struct pci_dev *pdev = adapter->pdev;
  2511. struct device *dev;
  2512. dev = &pdev->dev;
  2513. if (device_create_file(dev, &dev_attr_diag_mode))
  2514. dev_info(dev, "failed to create diag_mode sysfs entry\n");
  2515. if (device_create_bin_file(dev, &bin_attr_crb))
  2516. dev_info(dev, "failed to create crb sysfs entry\n");
  2517. if (device_create_bin_file(dev, &bin_attr_mem))
  2518. dev_info(dev, "failed to create mem sysfs entry\n");
  2519. if (device_create_bin_file(dev, &bin_attr_dimm))
  2520. dev_info(dev, "failed to create dimm sysfs entry\n");
  2521. }
  2522. static void
  2523. netxen_remove_diag_entries(struct netxen_adapter *adapter)
  2524. {
  2525. struct pci_dev *pdev = adapter->pdev;
  2526. struct device *dev = &pdev->dev;
  2527. device_remove_file(dev, &dev_attr_diag_mode);
  2528. device_remove_bin_file(dev, &bin_attr_crb);
  2529. device_remove_bin_file(dev, &bin_attr_mem);
  2530. device_remove_bin_file(dev, &bin_attr_dimm);
  2531. }
  2532. #ifdef CONFIG_INET
  2533. #define is_netxen_netdev(dev) (dev->netdev_ops == &netxen_netdev_ops)
  2534. static int
  2535. netxen_destip_supported(struct netxen_adapter *adapter)
  2536. {
  2537. if (NX_IS_REVISION_P2(adapter->ahw.revision_id))
  2538. return 0;
  2539. if (adapter->ahw.cut_through)
  2540. return 0;
  2541. return 1;
  2542. }
  2543. static void
  2544. netxen_free_ip_list(struct netxen_adapter *adapter, bool master)
  2545. {
  2546. struct nx_ip_list *cur, *tmp_cur;
  2547. list_for_each_entry_safe(cur, tmp_cur, &adapter->ip_list, list) {
  2548. if (master) {
  2549. if (cur->master) {
  2550. netxen_config_ipaddr(adapter, cur->ip_addr,
  2551. NX_IP_DOWN);
  2552. list_del(&cur->list);
  2553. kfree(cur);
  2554. }
  2555. } else {
  2556. netxen_config_ipaddr(adapter, cur->ip_addr, NX_IP_DOWN);
  2557. list_del(&cur->list);
  2558. kfree(cur);
  2559. }
  2560. }
  2561. }
  2562. static bool
  2563. netxen_list_config_ip(struct netxen_adapter *adapter,
  2564. struct in_ifaddr *ifa, unsigned long event)
  2565. {
  2566. struct net_device *dev;
  2567. struct nx_ip_list *cur, *tmp_cur;
  2568. struct list_head *head;
  2569. bool ret = false;
  2570. dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
  2571. if (dev == NULL)
  2572. goto out;
  2573. switch (event) {
  2574. case NX_IP_UP:
  2575. list_for_each(head, &adapter->ip_list) {
  2576. cur = list_entry(head, struct nx_ip_list, list);
  2577. if (cur->ip_addr == ifa->ifa_address)
  2578. goto out;
  2579. }
  2580. cur = kzalloc(sizeof(struct nx_ip_list), GFP_ATOMIC);
  2581. if (cur == NULL)
  2582. goto out;
  2583. if (dev->priv_flags & IFF_802_1Q_VLAN)
  2584. dev = vlan_dev_real_dev(dev);
  2585. cur->master = !!netif_is_bond_master(dev);
  2586. cur->ip_addr = ifa->ifa_address;
  2587. list_add_tail(&cur->list, &adapter->ip_list);
  2588. netxen_config_ipaddr(adapter, ifa->ifa_address, NX_IP_UP);
  2589. ret = true;
  2590. break;
  2591. case NX_IP_DOWN:
  2592. list_for_each_entry_safe(cur, tmp_cur,
  2593. &adapter->ip_list, list) {
  2594. if (cur->ip_addr == ifa->ifa_address) {
  2595. list_del(&cur->list);
  2596. kfree(cur);
  2597. netxen_config_ipaddr(adapter, ifa->ifa_address,
  2598. NX_IP_DOWN);
  2599. ret = true;
  2600. break;
  2601. }
  2602. }
  2603. }
  2604. out:
  2605. return ret;
  2606. }
  2607. static void
  2608. netxen_config_indev_addr(struct netxen_adapter *adapter,
  2609. struct net_device *dev, unsigned long event)
  2610. {
  2611. struct in_device *indev;
  2612. if (!netxen_destip_supported(adapter))
  2613. return;
  2614. indev = in_dev_get(dev);
  2615. if (!indev)
  2616. return;
  2617. for_ifa(indev) {
  2618. switch (event) {
  2619. case NETDEV_UP:
  2620. netxen_list_config_ip(adapter, ifa, NX_IP_UP);
  2621. break;
  2622. case NETDEV_DOWN:
  2623. netxen_list_config_ip(adapter, ifa, NX_IP_DOWN);
  2624. break;
  2625. default:
  2626. break;
  2627. }
  2628. } endfor_ifa(indev);
  2629. in_dev_put(indev);
  2630. }
  2631. static void
  2632. netxen_restore_indev_addr(struct net_device *netdev, unsigned long event)
  2633. {
  2634. struct netxen_adapter *adapter = netdev_priv(netdev);
  2635. struct nx_ip_list *pos, *tmp_pos;
  2636. unsigned long ip_event;
  2637. ip_event = (event == NETDEV_UP) ? NX_IP_UP : NX_IP_DOWN;
  2638. netxen_config_indev_addr(adapter, netdev, event);
  2639. list_for_each_entry_safe(pos, tmp_pos, &adapter->ip_list, list) {
  2640. netxen_config_ipaddr(adapter, pos->ip_addr, ip_event);
  2641. }
  2642. }
  2643. static inline bool
  2644. netxen_config_checkdev(struct net_device *dev)
  2645. {
  2646. struct netxen_adapter *adapter;
  2647. if (!is_netxen_netdev(dev))
  2648. return false;
  2649. adapter = netdev_priv(dev);
  2650. if (!adapter)
  2651. return false;
  2652. if (!netxen_destip_supported(adapter))
  2653. return false;
  2654. if (adapter->is_up != NETXEN_ADAPTER_UP_MAGIC)
  2655. return false;
  2656. return true;
  2657. }
  2658. /**
  2659. * netxen_config_master - configure addresses based on master
  2660. * @dev: netxen device
  2661. * @event: netdev event
  2662. */
  2663. static void netxen_config_master(struct net_device *dev, unsigned long event)
  2664. {
  2665. struct net_device *master, *slave;
  2666. struct netxen_adapter *adapter = netdev_priv(dev);
  2667. rcu_read_lock();
  2668. master = netdev_master_upper_dev_get_rcu(dev);
  2669. /*
  2670. * This is the case where the netxen nic is being
  2671. * enslaved and is dev_open()ed in bond_enslave()
  2672. * Now we should program the bond's (and its vlans')
  2673. * addresses in the netxen NIC.
  2674. */
  2675. if (master && netif_is_bond_master(master) &&
  2676. !netif_is_bond_slave(dev)) {
  2677. netxen_config_indev_addr(adapter, master, event);
  2678. for_each_netdev_rcu(&init_net, slave)
  2679. if (slave->priv_flags & IFF_802_1Q_VLAN &&
  2680. vlan_dev_real_dev(slave) == master)
  2681. netxen_config_indev_addr(adapter, slave, event);
  2682. }
  2683. rcu_read_unlock();
  2684. /*
  2685. * This is the case where the netxen nic is being
  2686. * released and is dev_close()ed in bond_release()
  2687. * just before IFF_BONDING is stripped.
  2688. */
  2689. if (!master && dev->priv_flags & IFF_BONDING)
  2690. netxen_free_ip_list(adapter, true);
  2691. }
  2692. static int netxen_netdev_event(struct notifier_block *this,
  2693. unsigned long event, void *ptr)
  2694. {
  2695. struct netxen_adapter *adapter;
  2696. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  2697. struct net_device *orig_dev = dev;
  2698. struct net_device *slave;
  2699. recheck:
  2700. if (dev == NULL)
  2701. goto done;
  2702. if (dev->priv_flags & IFF_802_1Q_VLAN) {
  2703. dev = vlan_dev_real_dev(dev);
  2704. goto recheck;
  2705. }
  2706. if (event == NETDEV_UP || event == NETDEV_DOWN) {
  2707. /* If this is a bonding device, look for netxen-based slaves*/
  2708. if (netif_is_bond_master(dev)) {
  2709. rcu_read_lock();
  2710. for_each_netdev_in_bond_rcu(dev, slave) {
  2711. if (!netxen_config_checkdev(slave))
  2712. continue;
  2713. adapter = netdev_priv(slave);
  2714. netxen_config_indev_addr(adapter,
  2715. orig_dev, event);
  2716. }
  2717. rcu_read_unlock();
  2718. } else {
  2719. if (!netxen_config_checkdev(dev))
  2720. goto done;
  2721. adapter = netdev_priv(dev);
  2722. /* Act only if the actual netxen is the target */
  2723. if (orig_dev == dev)
  2724. netxen_config_master(dev, event);
  2725. netxen_config_indev_addr(adapter, orig_dev, event);
  2726. }
  2727. }
  2728. done:
  2729. return NOTIFY_DONE;
  2730. }
  2731. static int
  2732. netxen_inetaddr_event(struct notifier_block *this,
  2733. unsigned long event, void *ptr)
  2734. {
  2735. struct netxen_adapter *adapter;
  2736. struct net_device *dev, *slave;
  2737. struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
  2738. unsigned long ip_event;
  2739. dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
  2740. ip_event = (event == NETDEV_UP) ? NX_IP_UP : NX_IP_DOWN;
  2741. recheck:
  2742. if (dev == NULL)
  2743. goto done;
  2744. if (dev->priv_flags & IFF_802_1Q_VLAN) {
  2745. dev = vlan_dev_real_dev(dev);
  2746. goto recheck;
  2747. }
  2748. if (event == NETDEV_UP || event == NETDEV_DOWN) {
  2749. /* If this is a bonding device, look for netxen-based slaves*/
  2750. if (netif_is_bond_master(dev)) {
  2751. rcu_read_lock();
  2752. for_each_netdev_in_bond_rcu(dev, slave) {
  2753. if (!netxen_config_checkdev(slave))
  2754. continue;
  2755. adapter = netdev_priv(slave);
  2756. netxen_list_config_ip(adapter, ifa, ip_event);
  2757. }
  2758. rcu_read_unlock();
  2759. } else {
  2760. if (!netxen_config_checkdev(dev))
  2761. goto done;
  2762. adapter = netdev_priv(dev);
  2763. netxen_list_config_ip(adapter, ifa, ip_event);
  2764. }
  2765. }
  2766. done:
  2767. return NOTIFY_DONE;
  2768. }
  2769. static struct notifier_block netxen_netdev_cb = {
  2770. .notifier_call = netxen_netdev_event,
  2771. };
  2772. static struct notifier_block netxen_inetaddr_cb = {
  2773. .notifier_call = netxen_inetaddr_event,
  2774. };
  2775. #else
  2776. static void
  2777. netxen_restore_indev_addr(struct net_device *dev, unsigned long event)
  2778. { }
  2779. static void
  2780. netxen_free_ip_list(struct netxen_adapter *adapter, bool master)
  2781. { }
  2782. #endif
  2783. static const struct pci_error_handlers netxen_err_handler = {
  2784. .error_detected = netxen_io_error_detected,
  2785. .slot_reset = netxen_io_slot_reset,
  2786. .resume = netxen_io_resume,
  2787. };
  2788. static struct pci_driver netxen_driver = {
  2789. .name = netxen_nic_driver_name,
  2790. .id_table = netxen_pci_tbl,
  2791. .probe = netxen_nic_probe,
  2792. .remove = netxen_nic_remove,
  2793. #ifdef CONFIG_PM
  2794. .suspend = netxen_nic_suspend,
  2795. .resume = netxen_nic_resume,
  2796. #endif
  2797. .shutdown = netxen_nic_shutdown,
  2798. .err_handler = &netxen_err_handler
  2799. };
  2800. static int __init netxen_init_module(void)
  2801. {
  2802. printk(KERN_INFO "%s\n", netxen_nic_driver_string);
  2803. #ifdef CONFIG_INET
  2804. register_netdevice_notifier(&netxen_netdev_cb);
  2805. register_inetaddr_notifier(&netxen_inetaddr_cb);
  2806. #endif
  2807. return pci_register_driver(&netxen_driver);
  2808. }
  2809. module_init(netxen_init_module);
  2810. static void __exit netxen_exit_module(void)
  2811. {
  2812. pci_unregister_driver(&netxen_driver);
  2813. #ifdef CONFIG_INET
  2814. unregister_inetaddr_notifier(&netxen_inetaddr_cb);
  2815. unregister_netdevice_notifier(&netxen_netdev_cb);
  2816. #endif
  2817. }
  2818. module_exit(netxen_exit_module);