qlcnic_main.c 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955
  1. /*
  2. * Copyright (C) 2009 - QLogic Corporation.
  3. * All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  18. * MA 02111-1307, USA.
  19. *
  20. * The full GNU General Public License is included in this distribution
  21. * in the file called "COPYING".
  22. *
  23. */
  24. #include <linux/slab.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/interrupt.h>
  27. #include "qlcnic.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 1/10 GbE Converged/Intelligent Ethernet Driver");
  36. MODULE_LICENSE("GPL");
  37. MODULE_VERSION(QLCNIC_LINUX_VERSIONID);
  38. MODULE_FIRMWARE(QLCNIC_UNIFIED_ROMIMAGE_NAME);
  39. char qlcnic_driver_name[] = "qlcnic";
  40. static const char qlcnic_driver_string[] = "QLogic 1/10 GbE "
  41. "Converged/Intelligent Ethernet Driver v" QLCNIC_LINUX_VERSIONID;
  42. static int port_mode = QLCNIC_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. module_param(use_msi, int, 0644);
  47. MODULE_PARM_DESC(use_msi, "MSI interrupt (0=disabled, 1=enabled");
  48. static int use_msi_x = 1;
  49. module_param(use_msi_x, int, 0644);
  50. MODULE_PARM_DESC(use_msi_x, "MSI-X interrupt (0=disabled, 1=enabled");
  51. static int auto_fw_reset = AUTO_FW_RESET_ENABLED;
  52. module_param(auto_fw_reset, int, 0644);
  53. MODULE_PARM_DESC(auto_fw_reset, "Auto firmware reset (0=disabled, 1=enabled");
  54. static int load_fw_file;
  55. module_param(load_fw_file, int, 0644);
  56. MODULE_PARM_DESC(load_fw_file, "Load firmware from (0=flash, 1=file");
  57. static int qlcnic_config_npars;
  58. module_param(qlcnic_config_npars, int, 0644);
  59. MODULE_PARM_DESC(qlcnic_config_npars, "Configure NPARs (0=disabled, 1=enabled");
  60. static int __devinit qlcnic_probe(struct pci_dev *pdev,
  61. const struct pci_device_id *ent);
  62. static void __devexit qlcnic_remove(struct pci_dev *pdev);
  63. static int qlcnic_open(struct net_device *netdev);
  64. static int qlcnic_close(struct net_device *netdev);
  65. static void qlcnic_tx_timeout(struct net_device *netdev);
  66. static void qlcnic_attach_work(struct work_struct *work);
  67. static void qlcnic_fwinit_work(struct work_struct *work);
  68. static void qlcnic_fw_poll_work(struct work_struct *work);
  69. static void qlcnic_schedule_work(struct qlcnic_adapter *adapter,
  70. work_func_t func, int delay);
  71. static void qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter);
  72. static int qlcnic_poll(struct napi_struct *napi, int budget);
  73. static int qlcnic_rx_poll(struct napi_struct *napi, int budget);
  74. #ifdef CONFIG_NET_POLL_CONTROLLER
  75. static void qlcnic_poll_controller(struct net_device *netdev);
  76. #endif
  77. static void qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter);
  78. static void qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter);
  79. static void qlcnic_create_diag_entries(struct qlcnic_adapter *adapter);
  80. static void qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter);
  81. static void qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding);
  82. static void qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8);
  83. static int qlcnic_can_start_firmware(struct qlcnic_adapter *adapter);
  84. static irqreturn_t qlcnic_tmp_intr(int irq, void *data);
  85. static irqreturn_t qlcnic_intr(int irq, void *data);
  86. static irqreturn_t qlcnic_msi_intr(int irq, void *data);
  87. static irqreturn_t qlcnic_msix_intr(int irq, void *data);
  88. static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev);
  89. static void qlcnic_config_indev_addr(struct net_device *dev, unsigned long);
  90. static int qlcnic_start_firmware(struct qlcnic_adapter *);
  91. static void qlcnic_dev_set_npar_ready(struct qlcnic_adapter *);
  92. static int qlcnicvf_config_led(struct qlcnic_adapter *, u32, u32);
  93. static int qlcnicvf_config_bridged_mode(struct qlcnic_adapter *, u32);
  94. static int qlcnicvf_start_firmware(struct qlcnic_adapter *);
  95. static void qlcnic_set_netdev_features(struct qlcnic_adapter *,
  96. struct qlcnic_esw_func_cfg *);
  97. /* PCI Device ID Table */
  98. #define ENTRY(device) \
  99. {PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, (device)), \
  100. .class = PCI_CLASS_NETWORK_ETHERNET << 8, .class_mask = ~0}
  101. #define PCI_DEVICE_ID_QLOGIC_QLE824X 0x8020
  102. static DEFINE_PCI_DEVICE_TABLE(qlcnic_pci_tbl) = {
  103. ENTRY(PCI_DEVICE_ID_QLOGIC_QLE824X),
  104. {0,}
  105. };
  106. MODULE_DEVICE_TABLE(pci, qlcnic_pci_tbl);
  107. void
  108. qlcnic_update_cmd_producer(struct qlcnic_adapter *adapter,
  109. struct qlcnic_host_tx_ring *tx_ring)
  110. {
  111. writel(tx_ring->producer, tx_ring->crb_cmd_producer);
  112. }
  113. static const u32 msi_tgt_status[8] = {
  114. ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
  115. ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
  116. ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
  117. ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
  118. };
  119. static const
  120. struct qlcnic_legacy_intr_set legacy_intr[] = QLCNIC_LEGACY_INTR_CONFIG;
  121. static inline void qlcnic_disable_int(struct qlcnic_host_sds_ring *sds_ring)
  122. {
  123. writel(0, sds_ring->crb_intr_mask);
  124. }
  125. static inline void qlcnic_enable_int(struct qlcnic_host_sds_ring *sds_ring)
  126. {
  127. struct qlcnic_adapter *adapter = sds_ring->adapter;
  128. writel(0x1, sds_ring->crb_intr_mask);
  129. if (!QLCNIC_IS_MSI_FAMILY(adapter))
  130. writel(0xfbff, adapter->tgt_mask_reg);
  131. }
  132. static int
  133. qlcnic_alloc_sds_rings(struct qlcnic_recv_context *recv_ctx, int count)
  134. {
  135. int size = sizeof(struct qlcnic_host_sds_ring) * count;
  136. recv_ctx->sds_rings = kzalloc(size, GFP_KERNEL);
  137. return (recv_ctx->sds_rings == NULL);
  138. }
  139. static void
  140. qlcnic_free_sds_rings(struct qlcnic_recv_context *recv_ctx)
  141. {
  142. if (recv_ctx->sds_rings != NULL)
  143. kfree(recv_ctx->sds_rings);
  144. recv_ctx->sds_rings = NULL;
  145. }
  146. static int
  147. qlcnic_napi_add(struct qlcnic_adapter *adapter, struct net_device *netdev)
  148. {
  149. int ring;
  150. struct qlcnic_host_sds_ring *sds_ring;
  151. struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
  152. if (qlcnic_alloc_sds_rings(recv_ctx, adapter->max_sds_rings))
  153. return -ENOMEM;
  154. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  155. sds_ring = &recv_ctx->sds_rings[ring];
  156. if (ring == adapter->max_sds_rings - 1)
  157. netif_napi_add(netdev, &sds_ring->napi, qlcnic_poll,
  158. QLCNIC_NETDEV_WEIGHT/adapter->max_sds_rings);
  159. else
  160. netif_napi_add(netdev, &sds_ring->napi,
  161. qlcnic_rx_poll, QLCNIC_NETDEV_WEIGHT*2);
  162. }
  163. return 0;
  164. }
  165. static void
  166. qlcnic_napi_del(struct qlcnic_adapter *adapter)
  167. {
  168. int ring;
  169. struct qlcnic_host_sds_ring *sds_ring;
  170. struct qlcnic_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. qlcnic_free_sds_rings(&adapter->recv_ctx);
  176. }
  177. static void
  178. qlcnic_napi_enable(struct qlcnic_adapter *adapter)
  179. {
  180. int ring;
  181. struct qlcnic_host_sds_ring *sds_ring;
  182. struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
  183. if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
  184. return;
  185. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  186. sds_ring = &recv_ctx->sds_rings[ring];
  187. napi_enable(&sds_ring->napi);
  188. qlcnic_enable_int(sds_ring);
  189. }
  190. }
  191. static void
  192. qlcnic_napi_disable(struct qlcnic_adapter *adapter)
  193. {
  194. int ring;
  195. struct qlcnic_host_sds_ring *sds_ring;
  196. struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
  197. if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
  198. return;
  199. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  200. sds_ring = &recv_ctx->sds_rings[ring];
  201. qlcnic_disable_int(sds_ring);
  202. napi_synchronize(&sds_ring->napi);
  203. napi_disable(&sds_ring->napi);
  204. }
  205. }
  206. static void qlcnic_clear_stats(struct qlcnic_adapter *adapter)
  207. {
  208. memset(&adapter->stats, 0, sizeof(adapter->stats));
  209. }
  210. static void qlcnic_set_port_mode(struct qlcnic_adapter *adapter)
  211. {
  212. u32 val, data;
  213. val = adapter->ahw.board_type;
  214. if ((val == QLCNIC_BRDTYPE_P3_HMEZ) ||
  215. (val == QLCNIC_BRDTYPE_P3_XG_LOM)) {
  216. if (port_mode == QLCNIC_PORT_MODE_802_3_AP) {
  217. data = QLCNIC_PORT_MODE_802_3_AP;
  218. QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
  219. } else if (port_mode == QLCNIC_PORT_MODE_XG) {
  220. data = QLCNIC_PORT_MODE_XG;
  221. QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
  222. } else if (port_mode == QLCNIC_PORT_MODE_AUTO_NEG_1G) {
  223. data = QLCNIC_PORT_MODE_AUTO_NEG_1G;
  224. QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
  225. } else if (port_mode == QLCNIC_PORT_MODE_AUTO_NEG_XG) {
  226. data = QLCNIC_PORT_MODE_AUTO_NEG_XG;
  227. QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
  228. } else {
  229. data = QLCNIC_PORT_MODE_AUTO_NEG;
  230. QLCWR32(adapter, QLCNIC_PORT_MODE_ADDR, data);
  231. }
  232. if ((wol_port_mode != QLCNIC_PORT_MODE_802_3_AP) &&
  233. (wol_port_mode != QLCNIC_PORT_MODE_XG) &&
  234. (wol_port_mode != QLCNIC_PORT_MODE_AUTO_NEG_1G) &&
  235. (wol_port_mode != QLCNIC_PORT_MODE_AUTO_NEG_XG)) {
  236. wol_port_mode = QLCNIC_PORT_MODE_AUTO_NEG;
  237. }
  238. QLCWR32(adapter, QLCNIC_WOL_PORT_MODE, wol_port_mode);
  239. }
  240. }
  241. static void qlcnic_set_msix_bit(struct pci_dev *pdev, int enable)
  242. {
  243. u32 control;
  244. int pos;
  245. pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
  246. if (pos) {
  247. pci_read_config_dword(pdev, pos, &control);
  248. if (enable)
  249. control |= PCI_MSIX_FLAGS_ENABLE;
  250. else
  251. control = 0;
  252. pci_write_config_dword(pdev, pos, control);
  253. }
  254. }
  255. static void qlcnic_init_msix_entries(struct qlcnic_adapter *adapter, int count)
  256. {
  257. int i;
  258. for (i = 0; i < count; i++)
  259. adapter->msix_entries[i].entry = i;
  260. }
  261. static int
  262. qlcnic_read_mac_addr(struct qlcnic_adapter *adapter)
  263. {
  264. u8 mac_addr[ETH_ALEN];
  265. struct net_device *netdev = adapter->netdev;
  266. struct pci_dev *pdev = adapter->pdev;
  267. if (adapter->nic_ops->get_mac_addr(adapter, mac_addr) != 0)
  268. return -EIO;
  269. memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
  270. memcpy(netdev->perm_addr, netdev->dev_addr, netdev->addr_len);
  271. memcpy(adapter->mac_addr, netdev->dev_addr, netdev->addr_len);
  272. /* set station address */
  273. if (!is_valid_ether_addr(netdev->perm_addr))
  274. dev_warn(&pdev->dev, "Bad MAC address %pM.\n",
  275. netdev->dev_addr);
  276. return 0;
  277. }
  278. static int qlcnic_set_mac(struct net_device *netdev, void *p)
  279. {
  280. struct qlcnic_adapter *adapter = netdev_priv(netdev);
  281. struct sockaddr *addr = p;
  282. if (!is_valid_ether_addr(addr->sa_data))
  283. return -EINVAL;
  284. if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
  285. netif_device_detach(netdev);
  286. qlcnic_napi_disable(adapter);
  287. }
  288. memcpy(adapter->mac_addr, addr->sa_data, netdev->addr_len);
  289. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  290. qlcnic_set_multi(adapter->netdev);
  291. if (test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
  292. netif_device_attach(netdev);
  293. qlcnic_napi_enable(adapter);
  294. }
  295. return 0;
  296. }
  297. static const struct net_device_ops qlcnic_netdev_ops = {
  298. .ndo_open = qlcnic_open,
  299. .ndo_stop = qlcnic_close,
  300. .ndo_start_xmit = qlcnic_xmit_frame,
  301. .ndo_get_stats = qlcnic_get_stats,
  302. .ndo_validate_addr = eth_validate_addr,
  303. .ndo_set_multicast_list = qlcnic_set_multi,
  304. .ndo_set_mac_address = qlcnic_set_mac,
  305. .ndo_change_mtu = qlcnic_change_mtu,
  306. .ndo_tx_timeout = qlcnic_tx_timeout,
  307. #ifdef CONFIG_NET_POLL_CONTROLLER
  308. .ndo_poll_controller = qlcnic_poll_controller,
  309. #endif
  310. };
  311. static struct qlcnic_nic_template qlcnic_ops = {
  312. .get_mac_addr = qlcnic_get_mac_address,
  313. .config_bridged_mode = qlcnic_config_bridged_mode,
  314. .config_led = qlcnic_config_led,
  315. .start_firmware = qlcnic_start_firmware
  316. };
  317. static struct qlcnic_nic_template qlcnic_vf_ops = {
  318. .get_mac_addr = qlcnic_get_mac_address,
  319. .config_bridged_mode = qlcnicvf_config_bridged_mode,
  320. .config_led = qlcnicvf_config_led,
  321. .start_firmware = qlcnicvf_start_firmware
  322. };
  323. static void
  324. qlcnic_setup_intr(struct qlcnic_adapter *adapter)
  325. {
  326. const struct qlcnic_legacy_intr_set *legacy_intrp;
  327. struct pci_dev *pdev = adapter->pdev;
  328. int err, num_msix;
  329. if (adapter->rss_supported) {
  330. num_msix = (num_online_cpus() >= MSIX_ENTRIES_PER_ADAPTER) ?
  331. MSIX_ENTRIES_PER_ADAPTER : 2;
  332. } else
  333. num_msix = 1;
  334. adapter->max_sds_rings = 1;
  335. adapter->flags &= ~(QLCNIC_MSI_ENABLED | QLCNIC_MSIX_ENABLED);
  336. legacy_intrp = &legacy_intr[adapter->ahw.pci_func];
  337. adapter->int_vec_bit = legacy_intrp->int_vec_bit;
  338. adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
  339. legacy_intrp->tgt_status_reg);
  340. adapter->tgt_mask_reg = qlcnic_get_ioaddr(adapter,
  341. legacy_intrp->tgt_mask_reg);
  342. adapter->isr_int_vec = qlcnic_get_ioaddr(adapter, ISR_INT_VECTOR);
  343. adapter->crb_int_state_reg = qlcnic_get_ioaddr(adapter,
  344. ISR_INT_STATE_REG);
  345. qlcnic_set_msix_bit(pdev, 0);
  346. if (adapter->msix_supported) {
  347. qlcnic_init_msix_entries(adapter, num_msix);
  348. err = pci_enable_msix(pdev, adapter->msix_entries, num_msix);
  349. if (err == 0) {
  350. adapter->flags |= QLCNIC_MSIX_ENABLED;
  351. qlcnic_set_msix_bit(pdev, 1);
  352. if (adapter->rss_supported)
  353. adapter->max_sds_rings = num_msix;
  354. dev_info(&pdev->dev, "using msi-x interrupts\n");
  355. return;
  356. }
  357. if (err > 0)
  358. pci_disable_msix(pdev);
  359. /* fall through for msi */
  360. }
  361. if (use_msi && !pci_enable_msi(pdev)) {
  362. adapter->flags |= QLCNIC_MSI_ENABLED;
  363. adapter->tgt_status_reg = qlcnic_get_ioaddr(adapter,
  364. msi_tgt_status[adapter->ahw.pci_func]);
  365. dev_info(&pdev->dev, "using msi interrupts\n");
  366. adapter->msix_entries[0].vector = pdev->irq;
  367. return;
  368. }
  369. dev_info(&pdev->dev, "using legacy interrupts\n");
  370. adapter->msix_entries[0].vector = pdev->irq;
  371. }
  372. static void
  373. qlcnic_teardown_intr(struct qlcnic_adapter *adapter)
  374. {
  375. if (adapter->flags & QLCNIC_MSIX_ENABLED)
  376. pci_disable_msix(adapter->pdev);
  377. if (adapter->flags & QLCNIC_MSI_ENABLED)
  378. pci_disable_msi(adapter->pdev);
  379. }
  380. static void
  381. qlcnic_cleanup_pci_map(struct qlcnic_adapter *adapter)
  382. {
  383. if (adapter->ahw.pci_base0 != NULL)
  384. iounmap(adapter->ahw.pci_base0);
  385. }
  386. static int
  387. qlcnic_init_pci_info(struct qlcnic_adapter *adapter)
  388. {
  389. struct qlcnic_pci_info *pci_info;
  390. int i, ret = 0;
  391. u8 pfn;
  392. pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
  393. if (!pci_info)
  394. return -ENOMEM;
  395. adapter->npars = kzalloc(sizeof(struct qlcnic_npar_info) *
  396. QLCNIC_MAX_PCI_FUNC, GFP_KERNEL);
  397. if (!adapter->npars) {
  398. ret = -ENOMEM;
  399. goto err_pci_info;
  400. }
  401. adapter->eswitch = kzalloc(sizeof(struct qlcnic_eswitch) *
  402. QLCNIC_NIU_MAX_XG_PORTS, GFP_KERNEL);
  403. if (!adapter->eswitch) {
  404. ret = -ENOMEM;
  405. goto err_npars;
  406. }
  407. ret = qlcnic_get_pci_info(adapter, pci_info);
  408. if (ret)
  409. goto err_eswitch;
  410. for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
  411. pfn = pci_info[i].id;
  412. if (pfn > QLCNIC_MAX_PCI_FUNC)
  413. return QL_STATUS_INVALID_PARAM;
  414. adapter->npars[pfn].active = pci_info[i].active;
  415. adapter->npars[pfn].type = pci_info[i].type;
  416. adapter->npars[pfn].phy_port = pci_info[i].default_port;
  417. adapter->npars[pfn].min_bw = pci_info[i].tx_min_bw;
  418. adapter->npars[pfn].max_bw = pci_info[i].tx_max_bw;
  419. }
  420. for (i = 0; i < QLCNIC_NIU_MAX_XG_PORTS; i++)
  421. adapter->eswitch[i].flags |= QLCNIC_SWITCH_ENABLE;
  422. kfree(pci_info);
  423. return 0;
  424. err_eswitch:
  425. kfree(adapter->eswitch);
  426. adapter->eswitch = NULL;
  427. err_npars:
  428. kfree(adapter->npars);
  429. adapter->npars = NULL;
  430. err_pci_info:
  431. kfree(pci_info);
  432. return ret;
  433. }
  434. static int
  435. qlcnic_set_function_modes(struct qlcnic_adapter *adapter)
  436. {
  437. u8 id;
  438. u32 ref_count;
  439. int i, ret = 1;
  440. u32 data = QLCNIC_MGMT_FUNC;
  441. void __iomem *priv_op = adapter->ahw.pci_base0 + QLCNIC_DRV_OP_MODE;
  442. /* If other drivers are not in use set their privilege level */
  443. ref_count = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
  444. ret = qlcnic_api_lock(adapter);
  445. if (ret)
  446. goto err_lock;
  447. if (qlcnic_config_npars) {
  448. for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
  449. id = i;
  450. if (adapter->npars[i].type != QLCNIC_TYPE_NIC ||
  451. id == adapter->ahw.pci_func)
  452. continue;
  453. data |= (qlcnic_config_npars &
  454. QLC_DEV_SET_DRV(0xf, id));
  455. }
  456. } else {
  457. data = readl(priv_op);
  458. data = (data & ~QLC_DEV_SET_DRV(0xf, adapter->ahw.pci_func)) |
  459. (QLC_DEV_SET_DRV(QLCNIC_MGMT_FUNC,
  460. adapter->ahw.pci_func));
  461. }
  462. writel(data, priv_op);
  463. qlcnic_api_unlock(adapter);
  464. err_lock:
  465. return ret;
  466. }
  467. static u32
  468. qlcnic_get_driver_mode(struct qlcnic_adapter *adapter)
  469. {
  470. void __iomem *msix_base_addr;
  471. void __iomem *priv_op;
  472. struct qlcnic_info nic_info;
  473. u32 func;
  474. u32 msix_base;
  475. u32 op_mode, priv_level;
  476. /* Determine FW API version */
  477. adapter->fw_hal_version = readl(adapter->ahw.pci_base0 + QLCNIC_FW_API);
  478. /* Find PCI function number */
  479. pci_read_config_dword(adapter->pdev, QLCNIC_MSIX_TABLE_OFFSET, &func);
  480. msix_base_addr = adapter->ahw.pci_base0 + QLCNIC_MSIX_BASE;
  481. msix_base = readl(msix_base_addr);
  482. func = (func - msix_base)/QLCNIC_MSIX_TBL_PGSIZE;
  483. adapter->ahw.pci_func = func;
  484. if (!qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw.pci_func)) {
  485. adapter->capabilities = nic_info.capabilities;
  486. if (adapter->capabilities & BIT_6)
  487. adapter->flags |= QLCNIC_ESWITCH_ENABLED;
  488. else
  489. adapter->flags &= ~QLCNIC_ESWITCH_ENABLED;
  490. }
  491. if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED)) {
  492. adapter->nic_ops = &qlcnic_ops;
  493. return adapter->fw_hal_version;
  494. }
  495. /* Determine function privilege level */
  496. priv_op = adapter->ahw.pci_base0 + QLCNIC_DRV_OP_MODE;
  497. op_mode = readl(priv_op);
  498. if (op_mode == QLC_DEV_DRV_DEFAULT)
  499. priv_level = QLCNIC_MGMT_FUNC;
  500. else
  501. priv_level = QLC_DEV_GET_DRV(op_mode, adapter->ahw.pci_func);
  502. switch (priv_level) {
  503. case QLCNIC_MGMT_FUNC:
  504. adapter->op_mode = QLCNIC_MGMT_FUNC;
  505. adapter->nic_ops = &qlcnic_ops;
  506. qlcnic_init_pci_info(adapter);
  507. /* Set privilege level for other functions */
  508. qlcnic_set_function_modes(adapter);
  509. dev_info(&adapter->pdev->dev,
  510. "HAL Version: %d, Management function\n",
  511. adapter->fw_hal_version);
  512. break;
  513. case QLCNIC_PRIV_FUNC:
  514. adapter->op_mode = QLCNIC_PRIV_FUNC;
  515. dev_info(&adapter->pdev->dev,
  516. "HAL Version: %d, Privileged function\n",
  517. adapter->fw_hal_version);
  518. adapter->nic_ops = &qlcnic_ops;
  519. break;
  520. case QLCNIC_NON_PRIV_FUNC:
  521. adapter->op_mode = QLCNIC_NON_PRIV_FUNC;
  522. dev_info(&adapter->pdev->dev,
  523. "HAL Version: %d Non Privileged function\n",
  524. adapter->fw_hal_version);
  525. adapter->nic_ops = &qlcnic_vf_ops;
  526. break;
  527. default:
  528. dev_info(&adapter->pdev->dev, "Unknown function mode: %d\n",
  529. priv_level);
  530. return 0;
  531. }
  532. return adapter->fw_hal_version;
  533. }
  534. static int
  535. qlcnic_setup_pci_map(struct qlcnic_adapter *adapter)
  536. {
  537. void __iomem *mem_ptr0 = NULL;
  538. resource_size_t mem_base;
  539. unsigned long mem_len, pci_len0 = 0;
  540. struct pci_dev *pdev = adapter->pdev;
  541. /* remap phys address */
  542. mem_base = pci_resource_start(pdev, 0); /* 0 is for BAR 0 */
  543. mem_len = pci_resource_len(pdev, 0);
  544. if (mem_len == QLCNIC_PCI_2MB_SIZE) {
  545. mem_ptr0 = pci_ioremap_bar(pdev, 0);
  546. if (mem_ptr0 == NULL) {
  547. dev_err(&pdev->dev, "failed to map PCI bar 0\n");
  548. return -EIO;
  549. }
  550. pci_len0 = mem_len;
  551. } else {
  552. return -EIO;
  553. }
  554. dev_info(&pdev->dev, "%dMB memory map\n", (int)(mem_len>>20));
  555. adapter->ahw.pci_base0 = mem_ptr0;
  556. adapter->ahw.pci_len0 = pci_len0;
  557. if (!qlcnic_get_driver_mode(adapter)) {
  558. iounmap(adapter->ahw.pci_base0);
  559. return -EIO;
  560. }
  561. adapter->ahw.ocm_win_crb = qlcnic_get_ioaddr(adapter,
  562. QLCNIC_PCIX_PS_REG(PCIX_OCM_WINDOW_REG(adapter->ahw.pci_func)));
  563. return 0;
  564. }
  565. static void get_brd_name(struct qlcnic_adapter *adapter, char *name)
  566. {
  567. struct pci_dev *pdev = adapter->pdev;
  568. int i, found = 0;
  569. for (i = 0; i < NUM_SUPPORTED_BOARDS; ++i) {
  570. if (qlcnic_boards[i].vendor == pdev->vendor &&
  571. qlcnic_boards[i].device == pdev->device &&
  572. qlcnic_boards[i].sub_vendor == pdev->subsystem_vendor &&
  573. qlcnic_boards[i].sub_device == pdev->subsystem_device) {
  574. sprintf(name, "%pM: %s" ,
  575. adapter->mac_addr,
  576. qlcnic_boards[i].short_name);
  577. found = 1;
  578. break;
  579. }
  580. }
  581. if (!found)
  582. sprintf(name, "%pM Gigabit Ethernet", adapter->mac_addr);
  583. }
  584. static void
  585. qlcnic_check_options(struct qlcnic_adapter *adapter)
  586. {
  587. u32 fw_major, fw_minor, fw_build;
  588. char brd_name[QLCNIC_MAX_BOARD_NAME_LEN];
  589. struct pci_dev *pdev = adapter->pdev;
  590. struct qlcnic_info nic_info;
  591. fw_major = QLCRD32(adapter, QLCNIC_FW_VERSION_MAJOR);
  592. fw_minor = QLCRD32(adapter, QLCNIC_FW_VERSION_MINOR);
  593. fw_build = QLCRD32(adapter, QLCNIC_FW_VERSION_SUB);
  594. adapter->fw_version = QLCNIC_VERSION_CODE(fw_major, fw_minor, fw_build);
  595. if (adapter->portnum == 0) {
  596. get_brd_name(adapter, brd_name);
  597. pr_info("%s: %s Board Chip rev 0x%x\n",
  598. module_name(THIS_MODULE),
  599. brd_name, adapter->ahw.revision_id);
  600. }
  601. dev_info(&pdev->dev, "firmware v%d.%d.%d\n",
  602. fw_major, fw_minor, fw_build);
  603. adapter->flags &= ~QLCNIC_LRO_ENABLED;
  604. if (adapter->ahw.port_type == QLCNIC_XGBE) {
  605. adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_10G;
  606. adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_10G;
  607. } else if (adapter->ahw.port_type == QLCNIC_GBE) {
  608. adapter->num_rxd = DEFAULT_RCV_DESCRIPTORS_1G;
  609. adapter->num_jumbo_rxd = MAX_JUMBO_RCV_DESCRIPTORS_1G;
  610. }
  611. if (!qlcnic_get_nic_info(adapter, &nic_info, adapter->ahw.pci_func)) {
  612. adapter->physical_port = nic_info.phys_port;
  613. adapter->switch_mode = nic_info.switch_mode;
  614. adapter->max_tx_ques = nic_info.max_tx_ques;
  615. adapter->max_rx_ques = nic_info.max_rx_ques;
  616. adapter->capabilities = nic_info.capabilities;
  617. adapter->max_mac_filters = nic_info.max_mac_filters;
  618. adapter->max_mtu = nic_info.max_mtu;
  619. }
  620. adapter->msix_supported = !!use_msi_x;
  621. adapter->rss_supported = !!use_msi_x;
  622. adapter->num_txd = MAX_CMD_DESCRIPTORS;
  623. adapter->max_rds_rings = MAX_RDS_RINGS;
  624. }
  625. static void
  626. qlcnic_set_eswitch_port_features(struct qlcnic_adapter *adapter,
  627. struct qlcnic_esw_func_cfg *esw_cfg)
  628. {
  629. adapter->flags &= ~QLCNIC_MACSPOOF;
  630. if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
  631. if (esw_cfg->mac_anti_spoof)
  632. adapter->flags |= QLCNIC_MACSPOOF;
  633. qlcnic_set_netdev_features(adapter, esw_cfg);
  634. }
  635. static int
  636. qlcnic_set_eswitch_port_config(struct qlcnic_adapter *adapter)
  637. {
  638. struct qlcnic_esw_func_cfg esw_cfg;
  639. if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
  640. return 0;
  641. esw_cfg.pci_func = adapter->ahw.pci_func;
  642. if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg))
  643. return -EIO;
  644. qlcnic_set_eswitch_port_features(adapter, &esw_cfg);
  645. return 0;
  646. }
  647. static void
  648. qlcnic_set_netdev_features(struct qlcnic_adapter *adapter,
  649. struct qlcnic_esw_func_cfg *esw_cfg)
  650. {
  651. struct net_device *netdev = adapter->netdev;
  652. unsigned long features, vlan_features;
  653. features = (NETIF_F_SG | NETIF_F_IP_CSUM |
  654. NETIF_F_IPV6_CSUM | NETIF_F_GRO);
  655. vlan_features = (NETIF_F_SG | NETIF_F_IP_CSUM |
  656. NETIF_F_IPV6_CSUM);
  657. if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO) {
  658. features |= (NETIF_F_TSO | NETIF_F_TSO6);
  659. vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
  660. }
  661. if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
  662. features |= NETIF_F_LRO;
  663. if (esw_cfg->offload_flags & BIT_0) {
  664. netdev->features |= features;
  665. adapter->rx_csum = 1;
  666. if (!(esw_cfg->offload_flags & BIT_1))
  667. netdev->features &= ~NETIF_F_TSO;
  668. if (!(esw_cfg->offload_flags & BIT_2))
  669. netdev->features &= ~NETIF_F_TSO6;
  670. } else {
  671. netdev->features &= ~features;
  672. adapter->rx_csum = 0;
  673. }
  674. netdev->vlan_features = (features & vlan_features);
  675. }
  676. static int
  677. qlcnic_set_default_offload_settings(struct qlcnic_adapter *adapter)
  678. {
  679. struct qlcnic_esw_func_cfg esw_cfg;
  680. struct qlcnic_npar_info *npar;
  681. u8 i;
  682. if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
  683. adapter->need_fw_reset ||
  684. adapter->op_mode != QLCNIC_MGMT_FUNC)
  685. return 0;
  686. for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
  687. if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
  688. continue;
  689. memset(&esw_cfg, 0, sizeof(struct qlcnic_esw_func_cfg));
  690. esw_cfg.pci_func = i;
  691. esw_cfg.offload_flags = BIT_0;
  692. esw_cfg.mac_learning = BIT_0;
  693. if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
  694. esw_cfg.offload_flags |= (BIT_1 | BIT_2);
  695. if (qlcnic_config_switch_port(adapter, &esw_cfg))
  696. return -EIO;
  697. npar = &adapter->npars[i];
  698. npar->pvid = esw_cfg.vlan_id;
  699. npar->mac_learning = esw_cfg.offload_flags;
  700. npar->mac_anti_spoof = esw_cfg.mac_anti_spoof;
  701. npar->discard_tagged = esw_cfg.discard_tagged;
  702. npar->promisc_mode = esw_cfg.promisc_mode;
  703. npar->offload_flags = esw_cfg.offload_flags;
  704. }
  705. return 0;
  706. }
  707. static int
  708. qlcnic_reset_eswitch_config(struct qlcnic_adapter *adapter,
  709. struct qlcnic_npar_info *npar, int pci_func)
  710. {
  711. struct qlcnic_esw_func_cfg esw_cfg;
  712. esw_cfg.op_mode = QLCNIC_PORT_DEFAULTS;
  713. esw_cfg.pci_func = pci_func;
  714. esw_cfg.vlan_id = npar->pvid;
  715. esw_cfg.mac_learning = npar->mac_learning;
  716. esw_cfg.discard_tagged = npar->discard_tagged;
  717. esw_cfg.mac_anti_spoof = npar->mac_anti_spoof;
  718. esw_cfg.offload_flags = npar->offload_flags;
  719. esw_cfg.promisc_mode = npar->promisc_mode;
  720. if (qlcnic_config_switch_port(adapter, &esw_cfg))
  721. return -EIO;
  722. esw_cfg.op_mode = QLCNIC_ADD_VLAN;
  723. if (qlcnic_config_switch_port(adapter, &esw_cfg))
  724. return -EIO;
  725. return 0;
  726. }
  727. static int
  728. qlcnic_reset_npar_config(struct qlcnic_adapter *adapter)
  729. {
  730. int i, err;
  731. struct qlcnic_npar_info *npar;
  732. struct qlcnic_info nic_info;
  733. if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
  734. !adapter->need_fw_reset || adapter->op_mode != QLCNIC_MGMT_FUNC)
  735. return 0;
  736. /* Set the NPAR config data after FW reset */
  737. for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
  738. npar = &adapter->npars[i];
  739. if (npar->type != QLCNIC_TYPE_NIC)
  740. continue;
  741. err = qlcnic_get_nic_info(adapter, &nic_info, i);
  742. if (err)
  743. return err;
  744. nic_info.min_tx_bw = npar->min_bw;
  745. nic_info.max_tx_bw = npar->max_bw;
  746. err = qlcnic_set_nic_info(adapter, &nic_info);
  747. if (err)
  748. return err;
  749. if (npar->enable_pm) {
  750. err = qlcnic_config_port_mirroring(adapter,
  751. npar->dest_npar, 1, i);
  752. if (err)
  753. return err;
  754. }
  755. err = qlcnic_reset_eswitch_config(adapter, npar, i);
  756. if (err)
  757. return err;
  758. }
  759. return 0;
  760. }
  761. static int qlcnic_check_npar_opertional(struct qlcnic_adapter *adapter)
  762. {
  763. u8 npar_opt_timeo = QLCNIC_DEV_NPAR_OPER_TIMEO;
  764. u32 npar_state;
  765. if (adapter->op_mode == QLCNIC_MGMT_FUNC)
  766. return 0;
  767. npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
  768. while (npar_state != QLCNIC_DEV_NPAR_OPER && --npar_opt_timeo) {
  769. msleep(1000);
  770. npar_state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
  771. }
  772. if (!npar_opt_timeo) {
  773. dev_err(&adapter->pdev->dev,
  774. "Waiting for NPAR state to opertional timeout\n");
  775. return -EIO;
  776. }
  777. return 0;
  778. }
  779. static int
  780. qlcnic_start_firmware(struct qlcnic_adapter *adapter)
  781. {
  782. int val, err, first_boot;
  783. err = qlcnic_can_start_firmware(adapter);
  784. if (err < 0)
  785. return err;
  786. else if (!err)
  787. goto wait_init;
  788. first_boot = QLCRD32(adapter, QLCNIC_CAM_RAM(0x1fc));
  789. if (first_boot == 0x55555555)
  790. /* This is the first boot after power up */
  791. QLCWR32(adapter, QLCNIC_CAM_RAM(0x1fc), QLCNIC_BDINFO_MAGIC);
  792. if (load_fw_file)
  793. qlcnic_request_firmware(adapter);
  794. else {
  795. if (qlcnic_check_flash_fw_ver(adapter))
  796. goto err_out;
  797. adapter->fw_type = QLCNIC_FLASH_ROMIMAGE;
  798. }
  799. err = qlcnic_need_fw_reset(adapter);
  800. if (err < 0)
  801. goto err_out;
  802. if (err == 0)
  803. goto wait_init;
  804. if (first_boot != 0x55555555) {
  805. QLCWR32(adapter, CRB_CMDPEG_STATE, 0);
  806. QLCWR32(adapter, CRB_RCVPEG_STATE, 0);
  807. qlcnic_pinit_from_rom(adapter);
  808. msleep(1);
  809. }
  810. QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS1, 0);
  811. QLCWR32(adapter, QLCNIC_PEG_HALT_STATUS2, 0);
  812. qlcnic_set_port_mode(adapter);
  813. err = qlcnic_load_firmware(adapter);
  814. if (err)
  815. goto err_out;
  816. qlcnic_release_firmware(adapter);
  817. val = (_QLCNIC_LINUX_MAJOR << 16)
  818. | ((_QLCNIC_LINUX_MINOR << 8))
  819. | (_QLCNIC_LINUX_SUBVERSION);
  820. QLCWR32(adapter, CRB_DRIVER_VERSION, val);
  821. wait_init:
  822. /* Handshake with the card before we register the devices. */
  823. err = qlcnic_init_firmware(adapter);
  824. if (err)
  825. goto err_out;
  826. QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_READY);
  827. qlcnic_idc_debug_info(adapter, 1);
  828. err = qlcnic_check_npar_opertional(adapter);
  829. if (err) {
  830. qlcnic_release_firmware(adapter);
  831. return err;
  832. }
  833. if (qlcnic_set_default_offload_settings(adapter))
  834. goto err_out;
  835. if (qlcnic_reset_npar_config(adapter))
  836. goto err_out;
  837. qlcnic_dev_set_npar_ready(adapter);
  838. qlcnic_check_options(adapter);
  839. adapter->need_fw_reset = 0;
  840. qlcnic_release_firmware(adapter);
  841. return 0;
  842. err_out:
  843. QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
  844. dev_err(&adapter->pdev->dev, "Device state set to failed\n");
  845. qlcnic_release_firmware(adapter);
  846. return err;
  847. }
  848. static int
  849. qlcnic_request_irq(struct qlcnic_adapter *adapter)
  850. {
  851. irq_handler_t handler;
  852. struct qlcnic_host_sds_ring *sds_ring;
  853. int err, ring;
  854. unsigned long flags = 0;
  855. struct net_device *netdev = adapter->netdev;
  856. struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
  857. if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
  858. handler = qlcnic_tmp_intr;
  859. if (!QLCNIC_IS_MSI_FAMILY(adapter))
  860. flags |= IRQF_SHARED;
  861. } else {
  862. if (adapter->flags & QLCNIC_MSIX_ENABLED)
  863. handler = qlcnic_msix_intr;
  864. else if (adapter->flags & QLCNIC_MSI_ENABLED)
  865. handler = qlcnic_msi_intr;
  866. else {
  867. flags |= IRQF_SHARED;
  868. handler = qlcnic_intr;
  869. }
  870. }
  871. adapter->irq = netdev->irq;
  872. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  873. sds_ring = &recv_ctx->sds_rings[ring];
  874. sprintf(sds_ring->name, "%s[%d]", netdev->name, ring);
  875. err = request_irq(sds_ring->irq, handler,
  876. flags, sds_ring->name, sds_ring);
  877. if (err)
  878. return err;
  879. }
  880. return 0;
  881. }
  882. static void
  883. qlcnic_free_irq(struct qlcnic_adapter *adapter)
  884. {
  885. int ring;
  886. struct qlcnic_host_sds_ring *sds_ring;
  887. struct qlcnic_recv_context *recv_ctx = &adapter->recv_ctx;
  888. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  889. sds_ring = &recv_ctx->sds_rings[ring];
  890. free_irq(sds_ring->irq, sds_ring);
  891. }
  892. }
  893. static void
  894. qlcnic_init_coalesce_defaults(struct qlcnic_adapter *adapter)
  895. {
  896. adapter->coal.flags = QLCNIC_INTR_DEFAULT;
  897. adapter->coal.normal.data.rx_time_us =
  898. QLCNIC_DEFAULT_INTR_COALESCE_RX_TIME_US;
  899. adapter->coal.normal.data.rx_packets =
  900. QLCNIC_DEFAULT_INTR_COALESCE_RX_PACKETS;
  901. adapter->coal.normal.data.tx_time_us =
  902. QLCNIC_DEFAULT_INTR_COALESCE_TX_TIME_US;
  903. adapter->coal.normal.data.tx_packets =
  904. QLCNIC_DEFAULT_INTR_COALESCE_TX_PACKETS;
  905. }
  906. static int
  907. __qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
  908. {
  909. int ring;
  910. struct qlcnic_host_rds_ring *rds_ring;
  911. if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
  912. return -EIO;
  913. if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
  914. return 0;
  915. if (qlcnic_set_eswitch_port_config(adapter))
  916. return -EIO;
  917. if (qlcnic_fw_create_ctx(adapter))
  918. return -EIO;
  919. for (ring = 0; ring < adapter->max_rds_rings; ring++) {
  920. rds_ring = &adapter->recv_ctx.rds_rings[ring];
  921. qlcnic_post_rx_buffers(adapter, ring, rds_ring);
  922. }
  923. qlcnic_set_multi(netdev);
  924. qlcnic_fw_cmd_set_mtu(adapter, netdev->mtu);
  925. adapter->ahw.linkup = 0;
  926. if (adapter->max_sds_rings > 1)
  927. qlcnic_config_rss(adapter, 1);
  928. qlcnic_config_intr_coalesce(adapter);
  929. if (netdev->features & NETIF_F_LRO)
  930. qlcnic_config_hw_lro(adapter, QLCNIC_LRO_ENABLED);
  931. qlcnic_napi_enable(adapter);
  932. qlcnic_linkevent_request(adapter, 1);
  933. adapter->reset_context = 0;
  934. set_bit(__QLCNIC_DEV_UP, &adapter->state);
  935. return 0;
  936. }
  937. /* Usage: During resume and firmware recovery module.*/
  938. static int
  939. qlcnic_up(struct qlcnic_adapter *adapter, struct net_device *netdev)
  940. {
  941. int err = 0;
  942. rtnl_lock();
  943. if (netif_running(netdev))
  944. err = __qlcnic_up(adapter, netdev);
  945. rtnl_unlock();
  946. return err;
  947. }
  948. static void
  949. __qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
  950. {
  951. if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
  952. return;
  953. if (!test_and_clear_bit(__QLCNIC_DEV_UP, &adapter->state))
  954. return;
  955. smp_mb();
  956. spin_lock(&adapter->tx_clean_lock);
  957. netif_carrier_off(netdev);
  958. netif_tx_disable(netdev);
  959. qlcnic_free_mac_list(adapter);
  960. qlcnic_nic_set_promisc(adapter, QLCNIC_NIU_NON_PROMISC_MODE);
  961. qlcnic_napi_disable(adapter);
  962. qlcnic_fw_destroy_ctx(adapter);
  963. qlcnic_reset_rx_buffers_list(adapter);
  964. qlcnic_release_tx_buffers(adapter);
  965. spin_unlock(&adapter->tx_clean_lock);
  966. }
  967. /* Usage: During suspend and firmware recovery module */
  968. static void
  969. qlcnic_down(struct qlcnic_adapter *adapter, struct net_device *netdev)
  970. {
  971. rtnl_lock();
  972. if (netif_running(netdev))
  973. __qlcnic_down(adapter, netdev);
  974. rtnl_unlock();
  975. }
  976. static int
  977. qlcnic_attach(struct qlcnic_adapter *adapter)
  978. {
  979. struct net_device *netdev = adapter->netdev;
  980. struct pci_dev *pdev = adapter->pdev;
  981. int err;
  982. if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC)
  983. return 0;
  984. err = qlcnic_napi_add(adapter, netdev);
  985. if (err)
  986. return err;
  987. err = qlcnic_alloc_sw_resources(adapter);
  988. if (err) {
  989. dev_err(&pdev->dev, "Error in setting sw resources\n");
  990. goto err_out_napi_del;
  991. }
  992. err = qlcnic_alloc_hw_resources(adapter);
  993. if (err) {
  994. dev_err(&pdev->dev, "Error in setting hw resources\n");
  995. goto err_out_free_sw;
  996. }
  997. err = qlcnic_request_irq(adapter);
  998. if (err) {
  999. dev_err(&pdev->dev, "failed to setup interrupt\n");
  1000. goto err_out_free_hw;
  1001. }
  1002. qlcnic_init_coalesce_defaults(adapter);
  1003. qlcnic_create_sysfs_entries(adapter);
  1004. adapter->is_up = QLCNIC_ADAPTER_UP_MAGIC;
  1005. return 0;
  1006. err_out_free_hw:
  1007. qlcnic_free_hw_resources(adapter);
  1008. err_out_free_sw:
  1009. qlcnic_free_sw_resources(adapter);
  1010. err_out_napi_del:
  1011. qlcnic_napi_del(adapter);
  1012. return err;
  1013. }
  1014. static void
  1015. qlcnic_detach(struct qlcnic_adapter *adapter)
  1016. {
  1017. if (adapter->is_up != QLCNIC_ADAPTER_UP_MAGIC)
  1018. return;
  1019. qlcnic_remove_sysfs_entries(adapter);
  1020. qlcnic_free_hw_resources(adapter);
  1021. qlcnic_release_rx_buffers(adapter);
  1022. qlcnic_free_irq(adapter);
  1023. qlcnic_napi_del(adapter);
  1024. qlcnic_free_sw_resources(adapter);
  1025. adapter->is_up = 0;
  1026. }
  1027. void qlcnic_diag_free_res(struct net_device *netdev, int max_sds_rings)
  1028. {
  1029. struct qlcnic_adapter *adapter = netdev_priv(netdev);
  1030. struct qlcnic_host_sds_ring *sds_ring;
  1031. int ring;
  1032. clear_bit(__QLCNIC_DEV_UP, &adapter->state);
  1033. if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
  1034. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  1035. sds_ring = &adapter->recv_ctx.sds_rings[ring];
  1036. qlcnic_disable_int(sds_ring);
  1037. }
  1038. }
  1039. qlcnic_fw_destroy_ctx(adapter);
  1040. qlcnic_detach(adapter);
  1041. adapter->diag_test = 0;
  1042. adapter->max_sds_rings = max_sds_rings;
  1043. if (qlcnic_attach(adapter))
  1044. goto out;
  1045. if (netif_running(netdev))
  1046. __qlcnic_up(adapter, netdev);
  1047. out:
  1048. netif_device_attach(netdev);
  1049. }
  1050. int qlcnic_diag_alloc_res(struct net_device *netdev, int test)
  1051. {
  1052. struct qlcnic_adapter *adapter = netdev_priv(netdev);
  1053. struct qlcnic_host_sds_ring *sds_ring;
  1054. struct qlcnic_host_rds_ring *rds_ring;
  1055. int ring;
  1056. int ret;
  1057. netif_device_detach(netdev);
  1058. if (netif_running(netdev))
  1059. __qlcnic_down(adapter, netdev);
  1060. qlcnic_detach(adapter);
  1061. adapter->max_sds_rings = 1;
  1062. adapter->diag_test = test;
  1063. ret = qlcnic_attach(adapter);
  1064. if (ret) {
  1065. netif_device_attach(netdev);
  1066. return ret;
  1067. }
  1068. ret = qlcnic_fw_create_ctx(adapter);
  1069. if (ret) {
  1070. qlcnic_detach(adapter);
  1071. netif_device_attach(netdev);
  1072. return ret;
  1073. }
  1074. for (ring = 0; ring < adapter->max_rds_rings; ring++) {
  1075. rds_ring = &adapter->recv_ctx.rds_rings[ring];
  1076. qlcnic_post_rx_buffers(adapter, ring, rds_ring);
  1077. }
  1078. if (adapter->diag_test == QLCNIC_INTERRUPT_TEST) {
  1079. for (ring = 0; ring < adapter->max_sds_rings; ring++) {
  1080. sds_ring = &adapter->recv_ctx.sds_rings[ring];
  1081. qlcnic_enable_int(sds_ring);
  1082. }
  1083. }
  1084. set_bit(__QLCNIC_DEV_UP, &adapter->state);
  1085. return 0;
  1086. }
  1087. /* Reset context in hardware only */
  1088. static int
  1089. qlcnic_reset_hw_context(struct qlcnic_adapter *adapter)
  1090. {
  1091. struct net_device *netdev = adapter->netdev;
  1092. if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
  1093. return -EBUSY;
  1094. netif_device_detach(netdev);
  1095. qlcnic_down(adapter, netdev);
  1096. qlcnic_up(adapter, netdev);
  1097. netif_device_attach(netdev);
  1098. clear_bit(__QLCNIC_RESETTING, &adapter->state);
  1099. return 0;
  1100. }
  1101. int
  1102. qlcnic_reset_context(struct qlcnic_adapter *adapter)
  1103. {
  1104. int err = 0;
  1105. struct net_device *netdev = adapter->netdev;
  1106. if (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
  1107. return -EBUSY;
  1108. if (adapter->is_up == QLCNIC_ADAPTER_UP_MAGIC) {
  1109. netif_device_detach(netdev);
  1110. if (netif_running(netdev))
  1111. __qlcnic_down(adapter, netdev);
  1112. qlcnic_detach(adapter);
  1113. if (netif_running(netdev)) {
  1114. err = qlcnic_attach(adapter);
  1115. if (!err)
  1116. __qlcnic_up(adapter, netdev);
  1117. }
  1118. netif_device_attach(netdev);
  1119. }
  1120. clear_bit(__QLCNIC_RESETTING, &adapter->state);
  1121. return err;
  1122. }
  1123. static int
  1124. qlcnic_setup_netdev(struct qlcnic_adapter *adapter,
  1125. struct net_device *netdev, u8 pci_using_dac)
  1126. {
  1127. int err;
  1128. struct pci_dev *pdev = adapter->pdev;
  1129. adapter->rx_csum = 1;
  1130. adapter->mc_enabled = 0;
  1131. adapter->max_mc_count = 38;
  1132. netdev->netdev_ops = &qlcnic_netdev_ops;
  1133. netdev->watchdog_timeo = 5*HZ;
  1134. qlcnic_change_mtu(netdev, netdev->mtu);
  1135. SET_ETHTOOL_OPS(netdev, &qlcnic_ethtool_ops);
  1136. netdev->features |= (NETIF_F_SG | NETIF_F_IP_CSUM |
  1137. NETIF_F_IPV6_CSUM | NETIF_F_GRO);
  1138. netdev->vlan_features |= (NETIF_F_SG | NETIF_F_IP_CSUM |
  1139. NETIF_F_IPV6_CSUM);
  1140. if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO) {
  1141. netdev->features |= (NETIF_F_TSO | NETIF_F_TSO6);
  1142. netdev->vlan_features |= (NETIF_F_TSO | NETIF_F_TSO6);
  1143. }
  1144. if (pci_using_dac) {
  1145. netdev->features |= NETIF_F_HIGHDMA;
  1146. netdev->vlan_features |= NETIF_F_HIGHDMA;
  1147. }
  1148. if (adapter->capabilities & QLCNIC_FW_CAPABILITY_FVLANTX)
  1149. netdev->features |= (NETIF_F_HW_VLAN_TX);
  1150. if (adapter->capabilities & QLCNIC_FW_CAPABILITY_HW_LRO)
  1151. netdev->features |= NETIF_F_LRO;
  1152. netdev->irq = adapter->msix_entries[0].vector;
  1153. if (qlcnic_read_mac_addr(adapter))
  1154. dev_warn(&pdev->dev, "failed to read mac addr\n");
  1155. netif_carrier_off(netdev);
  1156. netif_stop_queue(netdev);
  1157. err = register_netdev(netdev);
  1158. if (err) {
  1159. dev_err(&pdev->dev, "failed to register net device\n");
  1160. return err;
  1161. }
  1162. return 0;
  1163. }
  1164. static int qlcnic_set_dma_mask(struct pci_dev *pdev, u8 *pci_using_dac)
  1165. {
  1166. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
  1167. !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)))
  1168. *pci_using_dac = 1;
  1169. else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) &&
  1170. !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
  1171. *pci_using_dac = 0;
  1172. else {
  1173. dev_err(&pdev->dev, "Unable to set DMA mask, aborting\n");
  1174. return -EIO;
  1175. }
  1176. return 0;
  1177. }
  1178. static int __devinit
  1179. qlcnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1180. {
  1181. struct net_device *netdev = NULL;
  1182. struct qlcnic_adapter *adapter = NULL;
  1183. int err;
  1184. uint8_t revision_id;
  1185. uint8_t pci_using_dac;
  1186. err = pci_enable_device(pdev);
  1187. if (err)
  1188. return err;
  1189. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  1190. err = -ENODEV;
  1191. goto err_out_disable_pdev;
  1192. }
  1193. err = qlcnic_set_dma_mask(pdev, &pci_using_dac);
  1194. if (err)
  1195. goto err_out_disable_pdev;
  1196. err = pci_request_regions(pdev, qlcnic_driver_name);
  1197. if (err)
  1198. goto err_out_disable_pdev;
  1199. pci_set_master(pdev);
  1200. pci_enable_pcie_error_reporting(pdev);
  1201. netdev = alloc_etherdev(sizeof(struct qlcnic_adapter));
  1202. if (!netdev) {
  1203. dev_err(&pdev->dev, "failed to allocate net_device\n");
  1204. err = -ENOMEM;
  1205. goto err_out_free_res;
  1206. }
  1207. SET_NETDEV_DEV(netdev, &pdev->dev);
  1208. adapter = netdev_priv(netdev);
  1209. adapter->netdev = netdev;
  1210. adapter->pdev = pdev;
  1211. adapter->dev_rst_time = jiffies;
  1212. revision_id = pdev->revision;
  1213. adapter->ahw.revision_id = revision_id;
  1214. rwlock_init(&adapter->ahw.crb_lock);
  1215. mutex_init(&adapter->ahw.mem_lock);
  1216. spin_lock_init(&adapter->tx_clean_lock);
  1217. INIT_LIST_HEAD(&adapter->mac_list);
  1218. err = qlcnic_setup_pci_map(adapter);
  1219. if (err)
  1220. goto err_out_free_netdev;
  1221. /* This will be reset for mezz cards */
  1222. adapter->portnum = adapter->ahw.pci_func;
  1223. err = qlcnic_get_board_info(adapter);
  1224. if (err) {
  1225. dev_err(&pdev->dev, "Error getting board config info.\n");
  1226. goto err_out_iounmap;
  1227. }
  1228. if (qlcnic_read_mac_addr(adapter))
  1229. dev_warn(&pdev->dev, "failed to read mac addr\n");
  1230. if (qlcnic_setup_idc_param(adapter))
  1231. goto err_out_iounmap;
  1232. err = adapter->nic_ops->start_firmware(adapter);
  1233. if (err) {
  1234. dev_err(&pdev->dev, "Loading fw failed.Please Reboot\n");
  1235. goto err_out_decr_ref;
  1236. }
  1237. qlcnic_clear_stats(adapter);
  1238. qlcnic_setup_intr(adapter);
  1239. err = qlcnic_setup_netdev(adapter, netdev, pci_using_dac);
  1240. if (err)
  1241. goto err_out_disable_msi;
  1242. pci_set_drvdata(pdev, adapter);
  1243. qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
  1244. switch (adapter->ahw.port_type) {
  1245. case QLCNIC_GBE:
  1246. dev_info(&adapter->pdev->dev, "%s: GbE port initialized\n",
  1247. adapter->netdev->name);
  1248. break;
  1249. case QLCNIC_XGBE:
  1250. dev_info(&adapter->pdev->dev, "%s: XGbE port initialized\n",
  1251. adapter->netdev->name);
  1252. break;
  1253. }
  1254. qlcnic_create_diag_entries(adapter);
  1255. return 0;
  1256. err_out_disable_msi:
  1257. qlcnic_teardown_intr(adapter);
  1258. err_out_decr_ref:
  1259. qlcnic_clr_all_drv_state(adapter, 0);
  1260. err_out_iounmap:
  1261. qlcnic_cleanup_pci_map(adapter);
  1262. err_out_free_netdev:
  1263. free_netdev(netdev);
  1264. err_out_free_res:
  1265. pci_release_regions(pdev);
  1266. err_out_disable_pdev:
  1267. pci_set_drvdata(pdev, NULL);
  1268. pci_disable_device(pdev);
  1269. return err;
  1270. }
  1271. static void __devexit qlcnic_remove(struct pci_dev *pdev)
  1272. {
  1273. struct qlcnic_adapter *adapter;
  1274. struct net_device *netdev;
  1275. adapter = pci_get_drvdata(pdev);
  1276. if (adapter == NULL)
  1277. return;
  1278. netdev = adapter->netdev;
  1279. qlcnic_cancel_fw_work(adapter);
  1280. unregister_netdev(netdev);
  1281. qlcnic_detach(adapter);
  1282. if (adapter->npars != NULL)
  1283. kfree(adapter->npars);
  1284. if (adapter->eswitch != NULL)
  1285. kfree(adapter->eswitch);
  1286. qlcnic_clr_all_drv_state(adapter, 0);
  1287. clear_bit(__QLCNIC_RESETTING, &adapter->state);
  1288. qlcnic_teardown_intr(adapter);
  1289. qlcnic_remove_diag_entries(adapter);
  1290. qlcnic_cleanup_pci_map(adapter);
  1291. qlcnic_release_firmware(adapter);
  1292. pci_disable_pcie_error_reporting(pdev);
  1293. pci_release_regions(pdev);
  1294. pci_disable_device(pdev);
  1295. pci_set_drvdata(pdev, NULL);
  1296. free_netdev(netdev);
  1297. }
  1298. static int __qlcnic_shutdown(struct pci_dev *pdev)
  1299. {
  1300. struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
  1301. struct net_device *netdev = adapter->netdev;
  1302. int retval;
  1303. netif_device_detach(netdev);
  1304. qlcnic_cancel_fw_work(adapter);
  1305. if (netif_running(netdev))
  1306. qlcnic_down(adapter, netdev);
  1307. qlcnic_clr_all_drv_state(adapter, 0);
  1308. clear_bit(__QLCNIC_RESETTING, &adapter->state);
  1309. retval = pci_save_state(pdev);
  1310. if (retval)
  1311. return retval;
  1312. if (qlcnic_wol_supported(adapter)) {
  1313. pci_enable_wake(pdev, PCI_D3cold, 1);
  1314. pci_enable_wake(pdev, PCI_D3hot, 1);
  1315. }
  1316. return 0;
  1317. }
  1318. static void qlcnic_shutdown(struct pci_dev *pdev)
  1319. {
  1320. if (__qlcnic_shutdown(pdev))
  1321. return;
  1322. pci_disable_device(pdev);
  1323. }
  1324. #ifdef CONFIG_PM
  1325. static int
  1326. qlcnic_suspend(struct pci_dev *pdev, pm_message_t state)
  1327. {
  1328. int retval;
  1329. retval = __qlcnic_shutdown(pdev);
  1330. if (retval)
  1331. return retval;
  1332. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1333. return 0;
  1334. }
  1335. static int
  1336. qlcnic_resume(struct pci_dev *pdev)
  1337. {
  1338. struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
  1339. struct net_device *netdev = adapter->netdev;
  1340. int err;
  1341. err = pci_enable_device(pdev);
  1342. if (err)
  1343. return err;
  1344. pci_set_power_state(pdev, PCI_D0);
  1345. pci_set_master(pdev);
  1346. pci_restore_state(pdev);
  1347. err = adapter->nic_ops->start_firmware(adapter);
  1348. if (err) {
  1349. dev_err(&pdev->dev, "failed to start firmware\n");
  1350. return err;
  1351. }
  1352. if (netif_running(netdev)) {
  1353. err = qlcnic_up(adapter, netdev);
  1354. if (err)
  1355. goto done;
  1356. qlcnic_config_indev_addr(netdev, NETDEV_UP);
  1357. }
  1358. done:
  1359. netif_device_attach(netdev);
  1360. qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
  1361. return 0;
  1362. }
  1363. #endif
  1364. static int qlcnic_open(struct net_device *netdev)
  1365. {
  1366. struct qlcnic_adapter *adapter = netdev_priv(netdev);
  1367. int err;
  1368. err = qlcnic_attach(adapter);
  1369. if (err)
  1370. return err;
  1371. err = __qlcnic_up(adapter, netdev);
  1372. if (err)
  1373. goto err_out;
  1374. netif_start_queue(netdev);
  1375. return 0;
  1376. err_out:
  1377. qlcnic_detach(adapter);
  1378. return err;
  1379. }
  1380. /*
  1381. * qlcnic_close - Disables a network interface entry point
  1382. */
  1383. static int qlcnic_close(struct net_device *netdev)
  1384. {
  1385. struct qlcnic_adapter *adapter = netdev_priv(netdev);
  1386. __qlcnic_down(adapter, netdev);
  1387. return 0;
  1388. }
  1389. static void
  1390. qlcnic_tso_check(struct net_device *netdev,
  1391. struct qlcnic_host_tx_ring *tx_ring,
  1392. struct cmd_desc_type0 *first_desc,
  1393. struct sk_buff *skb)
  1394. {
  1395. u8 opcode = TX_ETHER_PKT;
  1396. __be16 protocol = skb->protocol;
  1397. u16 flags = 0, vid = 0;
  1398. int copied, offset, copy_len, hdr_len = 0, tso = 0, vlan_oob = 0;
  1399. struct cmd_desc_type0 *hwdesc;
  1400. struct vlan_ethhdr *vh;
  1401. struct qlcnic_adapter *adapter = netdev_priv(netdev);
  1402. u32 producer = tx_ring->producer;
  1403. if (protocol == cpu_to_be16(ETH_P_8021Q)) {
  1404. vh = (struct vlan_ethhdr *)skb->data;
  1405. protocol = vh->h_vlan_encapsulated_proto;
  1406. flags = FLAGS_VLAN_TAGGED;
  1407. } else if (vlan_tx_tag_present(skb)) {
  1408. flags = FLAGS_VLAN_OOB;
  1409. vid = vlan_tx_tag_get(skb);
  1410. qlcnic_set_tx_vlan_tci(first_desc, vid);
  1411. vlan_oob = 1;
  1412. }
  1413. if (*(skb->data) & BIT_0) {
  1414. flags |= BIT_0;
  1415. memcpy(&first_desc->eth_addr, skb->data, ETH_ALEN);
  1416. }
  1417. if ((netdev->features & (NETIF_F_TSO | NETIF_F_TSO6)) &&
  1418. skb_shinfo(skb)->gso_size > 0) {
  1419. hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  1420. first_desc->mss = cpu_to_le16(skb_shinfo(skb)->gso_size);
  1421. first_desc->total_hdr_length = hdr_len;
  1422. if (vlan_oob) {
  1423. first_desc->total_hdr_length += VLAN_HLEN;
  1424. first_desc->tcp_hdr_offset = VLAN_HLEN;
  1425. first_desc->ip_hdr_offset = VLAN_HLEN;
  1426. /* Only in case of TSO on vlan device */
  1427. flags |= FLAGS_VLAN_TAGGED;
  1428. }
  1429. opcode = (protocol == cpu_to_be16(ETH_P_IPV6)) ?
  1430. TX_TCP_LSO6 : TX_TCP_LSO;
  1431. tso = 1;
  1432. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1433. u8 l4proto;
  1434. if (protocol == cpu_to_be16(ETH_P_IP)) {
  1435. l4proto = ip_hdr(skb)->protocol;
  1436. if (l4proto == IPPROTO_TCP)
  1437. opcode = TX_TCP_PKT;
  1438. else if (l4proto == IPPROTO_UDP)
  1439. opcode = TX_UDP_PKT;
  1440. } else if (protocol == cpu_to_be16(ETH_P_IPV6)) {
  1441. l4proto = ipv6_hdr(skb)->nexthdr;
  1442. if (l4proto == IPPROTO_TCP)
  1443. opcode = TX_TCPV6_PKT;
  1444. else if (l4proto == IPPROTO_UDP)
  1445. opcode = TX_UDPV6_PKT;
  1446. }
  1447. }
  1448. first_desc->tcp_hdr_offset += skb_transport_offset(skb);
  1449. first_desc->ip_hdr_offset += skb_network_offset(skb);
  1450. qlcnic_set_tx_flags_opcode(first_desc, flags, opcode);
  1451. if (!tso)
  1452. return;
  1453. /* For LSO, we need to copy the MAC/IP/TCP headers into
  1454. * the descriptor ring
  1455. */
  1456. copied = 0;
  1457. offset = 2;
  1458. if (vlan_oob) {
  1459. /* Create a TSO vlan header template for firmware */
  1460. hwdesc = &tx_ring->desc_head[producer];
  1461. tx_ring->cmd_buf_arr[producer].skb = NULL;
  1462. copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
  1463. hdr_len + VLAN_HLEN);
  1464. vh = (struct vlan_ethhdr *)((char *)hwdesc + 2);
  1465. skb_copy_from_linear_data(skb, vh, 12);
  1466. vh->h_vlan_proto = htons(ETH_P_8021Q);
  1467. vh->h_vlan_TCI = htons(vid);
  1468. skb_copy_from_linear_data_offset(skb, 12,
  1469. (char *)vh + 16, copy_len - 16);
  1470. copied = copy_len - VLAN_HLEN;
  1471. offset = 0;
  1472. producer = get_next_index(producer, tx_ring->num_desc);
  1473. }
  1474. while (copied < hdr_len) {
  1475. copy_len = min((int)sizeof(struct cmd_desc_type0) - offset,
  1476. (hdr_len - copied));
  1477. hwdesc = &tx_ring->desc_head[producer];
  1478. tx_ring->cmd_buf_arr[producer].skb = NULL;
  1479. skb_copy_from_linear_data_offset(skb, copied,
  1480. (char *)hwdesc + offset, copy_len);
  1481. copied += copy_len;
  1482. offset = 0;
  1483. producer = get_next_index(producer, tx_ring->num_desc);
  1484. }
  1485. tx_ring->producer = producer;
  1486. barrier();
  1487. adapter->stats.lso_frames++;
  1488. }
  1489. static int
  1490. qlcnic_map_tx_skb(struct pci_dev *pdev,
  1491. struct sk_buff *skb, struct qlcnic_cmd_buffer *pbuf)
  1492. {
  1493. struct qlcnic_skb_frag *nf;
  1494. struct skb_frag_struct *frag;
  1495. int i, nr_frags;
  1496. dma_addr_t map;
  1497. nr_frags = skb_shinfo(skb)->nr_frags;
  1498. nf = &pbuf->frag_array[0];
  1499. map = pci_map_single(pdev, skb->data,
  1500. skb_headlen(skb), PCI_DMA_TODEVICE);
  1501. if (pci_dma_mapping_error(pdev, map))
  1502. goto out_err;
  1503. nf->dma = map;
  1504. nf->length = skb_headlen(skb);
  1505. for (i = 0; i < nr_frags; i++) {
  1506. frag = &skb_shinfo(skb)->frags[i];
  1507. nf = &pbuf->frag_array[i+1];
  1508. map = pci_map_page(pdev, frag->page, frag->page_offset,
  1509. frag->size, PCI_DMA_TODEVICE);
  1510. if (pci_dma_mapping_error(pdev, map))
  1511. goto unwind;
  1512. nf->dma = map;
  1513. nf->length = frag->size;
  1514. }
  1515. return 0;
  1516. unwind:
  1517. while (--i >= 0) {
  1518. nf = &pbuf->frag_array[i+1];
  1519. pci_unmap_page(pdev, nf->dma, nf->length, PCI_DMA_TODEVICE);
  1520. }
  1521. nf = &pbuf->frag_array[0];
  1522. pci_unmap_single(pdev, nf->dma, skb_headlen(skb), PCI_DMA_TODEVICE);
  1523. out_err:
  1524. return -ENOMEM;
  1525. }
  1526. static inline void
  1527. qlcnic_clear_cmddesc(u64 *desc)
  1528. {
  1529. desc[0] = 0ULL;
  1530. desc[2] = 0ULL;
  1531. }
  1532. netdev_tx_t
  1533. qlcnic_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  1534. {
  1535. struct qlcnic_adapter *adapter = netdev_priv(netdev);
  1536. struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
  1537. struct qlcnic_cmd_buffer *pbuf;
  1538. struct qlcnic_skb_frag *buffrag;
  1539. struct cmd_desc_type0 *hwdesc, *first_desc;
  1540. struct pci_dev *pdev;
  1541. int i, k;
  1542. u32 producer;
  1543. int frag_count, no_of_desc;
  1544. u32 num_txd = tx_ring->num_desc;
  1545. if (!test_bit(__QLCNIC_DEV_UP, &adapter->state)) {
  1546. netif_stop_queue(netdev);
  1547. return NETDEV_TX_BUSY;
  1548. }
  1549. if (adapter->flags & QLCNIC_MACSPOOF) {
  1550. if (compare_ether_addr(eth_hdr(skb)->h_source,
  1551. adapter->mac_addr))
  1552. goto drop_packet;
  1553. }
  1554. frag_count = skb_shinfo(skb)->nr_frags + 1;
  1555. /* 4 fragments per cmd des */
  1556. no_of_desc = (frag_count + 3) >> 2;
  1557. if (unlikely(qlcnic_tx_avail(tx_ring) <= TX_STOP_THRESH)) {
  1558. netif_stop_queue(netdev);
  1559. smp_mb();
  1560. if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH)
  1561. netif_start_queue(netdev);
  1562. else {
  1563. adapter->stats.xmit_off++;
  1564. return NETDEV_TX_BUSY;
  1565. }
  1566. }
  1567. producer = tx_ring->producer;
  1568. pbuf = &tx_ring->cmd_buf_arr[producer];
  1569. pdev = adapter->pdev;
  1570. if (qlcnic_map_tx_skb(pdev, skb, pbuf)) {
  1571. adapter->stats.tx_dma_map_error++;
  1572. goto drop_packet;
  1573. }
  1574. pbuf->skb = skb;
  1575. pbuf->frag_count = frag_count;
  1576. first_desc = hwdesc = &tx_ring->desc_head[producer];
  1577. qlcnic_clear_cmddesc((u64 *)hwdesc);
  1578. qlcnic_set_tx_frags_len(first_desc, frag_count, skb->len);
  1579. qlcnic_set_tx_port(first_desc, adapter->portnum);
  1580. for (i = 0; i < frag_count; i++) {
  1581. k = i % 4;
  1582. if ((k == 0) && (i > 0)) {
  1583. /* move to next desc.*/
  1584. producer = get_next_index(producer, num_txd);
  1585. hwdesc = &tx_ring->desc_head[producer];
  1586. qlcnic_clear_cmddesc((u64 *)hwdesc);
  1587. tx_ring->cmd_buf_arr[producer].skb = NULL;
  1588. }
  1589. buffrag = &pbuf->frag_array[i];
  1590. hwdesc->buffer_length[k] = cpu_to_le16(buffrag->length);
  1591. switch (k) {
  1592. case 0:
  1593. hwdesc->addr_buffer1 = cpu_to_le64(buffrag->dma);
  1594. break;
  1595. case 1:
  1596. hwdesc->addr_buffer2 = cpu_to_le64(buffrag->dma);
  1597. break;
  1598. case 2:
  1599. hwdesc->addr_buffer3 = cpu_to_le64(buffrag->dma);
  1600. break;
  1601. case 3:
  1602. hwdesc->addr_buffer4 = cpu_to_le64(buffrag->dma);
  1603. break;
  1604. }
  1605. }
  1606. tx_ring->producer = get_next_index(producer, num_txd);
  1607. qlcnic_tso_check(netdev, tx_ring, first_desc, skb);
  1608. qlcnic_update_cmd_producer(adapter, tx_ring);
  1609. adapter->stats.txbytes += skb->len;
  1610. adapter->stats.xmitcalled++;
  1611. return NETDEV_TX_OK;
  1612. drop_packet:
  1613. adapter->stats.txdropped++;
  1614. dev_kfree_skb_any(skb);
  1615. return NETDEV_TX_OK;
  1616. }
  1617. static int qlcnic_check_temp(struct qlcnic_adapter *adapter)
  1618. {
  1619. struct net_device *netdev = adapter->netdev;
  1620. u32 temp, temp_state, temp_val;
  1621. int rv = 0;
  1622. temp = QLCRD32(adapter, CRB_TEMP_STATE);
  1623. temp_state = qlcnic_get_temp_state(temp);
  1624. temp_val = qlcnic_get_temp_val(temp);
  1625. if (temp_state == QLCNIC_TEMP_PANIC) {
  1626. dev_err(&netdev->dev,
  1627. "Device temperature %d degrees C exceeds"
  1628. " maximum allowed. Hardware has been shut down.\n",
  1629. temp_val);
  1630. rv = 1;
  1631. } else if (temp_state == QLCNIC_TEMP_WARN) {
  1632. if (adapter->temp == QLCNIC_TEMP_NORMAL) {
  1633. dev_err(&netdev->dev,
  1634. "Device temperature %d degrees C "
  1635. "exceeds operating range."
  1636. " Immediate action needed.\n",
  1637. temp_val);
  1638. }
  1639. } else {
  1640. if (adapter->temp == QLCNIC_TEMP_WARN) {
  1641. dev_info(&netdev->dev,
  1642. "Device temperature is now %d degrees C"
  1643. " in normal range.\n", temp_val);
  1644. }
  1645. }
  1646. adapter->temp = temp_state;
  1647. return rv;
  1648. }
  1649. void qlcnic_advert_link_change(struct qlcnic_adapter *adapter, int linkup)
  1650. {
  1651. struct net_device *netdev = adapter->netdev;
  1652. if (adapter->ahw.linkup && !linkup) {
  1653. netdev_info(netdev, "NIC Link is down\n");
  1654. adapter->ahw.linkup = 0;
  1655. if (netif_running(netdev)) {
  1656. netif_carrier_off(netdev);
  1657. netif_stop_queue(netdev);
  1658. }
  1659. } else if (!adapter->ahw.linkup && linkup) {
  1660. netdev_info(netdev, "NIC Link is up\n");
  1661. adapter->ahw.linkup = 1;
  1662. if (netif_running(netdev)) {
  1663. netif_carrier_on(netdev);
  1664. netif_wake_queue(netdev);
  1665. }
  1666. }
  1667. }
  1668. static void qlcnic_tx_timeout(struct net_device *netdev)
  1669. {
  1670. struct qlcnic_adapter *adapter = netdev_priv(netdev);
  1671. if (test_bit(__QLCNIC_RESETTING, &adapter->state))
  1672. return;
  1673. dev_err(&netdev->dev, "transmit timeout, resetting.\n");
  1674. if (++adapter->tx_timeo_cnt >= QLCNIC_MAX_TX_TIMEOUTS)
  1675. adapter->need_fw_reset = 1;
  1676. else
  1677. adapter->reset_context = 1;
  1678. }
  1679. static struct net_device_stats *qlcnic_get_stats(struct net_device *netdev)
  1680. {
  1681. struct qlcnic_adapter *adapter = netdev_priv(netdev);
  1682. struct net_device_stats *stats = &netdev->stats;
  1683. memset(stats, 0, sizeof(*stats));
  1684. stats->rx_packets = adapter->stats.rx_pkts + adapter->stats.lro_pkts;
  1685. stats->tx_packets = adapter->stats.xmitfinished;
  1686. stats->rx_bytes = adapter->stats.rxbytes + adapter->stats.lrobytes;
  1687. stats->tx_bytes = adapter->stats.txbytes;
  1688. stats->rx_dropped = adapter->stats.rxdropped;
  1689. stats->tx_dropped = adapter->stats.txdropped;
  1690. return stats;
  1691. }
  1692. static irqreturn_t qlcnic_clear_legacy_intr(struct qlcnic_adapter *adapter)
  1693. {
  1694. u32 status;
  1695. status = readl(adapter->isr_int_vec);
  1696. if (!(status & adapter->int_vec_bit))
  1697. return IRQ_NONE;
  1698. /* check interrupt state machine, to be sure */
  1699. status = readl(adapter->crb_int_state_reg);
  1700. if (!ISR_LEGACY_INT_TRIGGERED(status))
  1701. return IRQ_NONE;
  1702. writel(0xffffffff, adapter->tgt_status_reg);
  1703. /* read twice to ensure write is flushed */
  1704. readl(adapter->isr_int_vec);
  1705. readl(adapter->isr_int_vec);
  1706. return IRQ_HANDLED;
  1707. }
  1708. static irqreturn_t qlcnic_tmp_intr(int irq, void *data)
  1709. {
  1710. struct qlcnic_host_sds_ring *sds_ring = data;
  1711. struct qlcnic_adapter *adapter = sds_ring->adapter;
  1712. if (adapter->flags & QLCNIC_MSIX_ENABLED)
  1713. goto done;
  1714. else if (adapter->flags & QLCNIC_MSI_ENABLED) {
  1715. writel(0xffffffff, adapter->tgt_status_reg);
  1716. goto done;
  1717. }
  1718. if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
  1719. return IRQ_NONE;
  1720. done:
  1721. adapter->diag_cnt++;
  1722. qlcnic_enable_int(sds_ring);
  1723. return IRQ_HANDLED;
  1724. }
  1725. static irqreturn_t qlcnic_intr(int irq, void *data)
  1726. {
  1727. struct qlcnic_host_sds_ring *sds_ring = data;
  1728. struct qlcnic_adapter *adapter = sds_ring->adapter;
  1729. if (qlcnic_clear_legacy_intr(adapter) == IRQ_NONE)
  1730. return IRQ_NONE;
  1731. napi_schedule(&sds_ring->napi);
  1732. return IRQ_HANDLED;
  1733. }
  1734. static irqreturn_t qlcnic_msi_intr(int irq, void *data)
  1735. {
  1736. struct qlcnic_host_sds_ring *sds_ring = data;
  1737. struct qlcnic_adapter *adapter = sds_ring->adapter;
  1738. /* clear interrupt */
  1739. writel(0xffffffff, adapter->tgt_status_reg);
  1740. napi_schedule(&sds_ring->napi);
  1741. return IRQ_HANDLED;
  1742. }
  1743. static irqreturn_t qlcnic_msix_intr(int irq, void *data)
  1744. {
  1745. struct qlcnic_host_sds_ring *sds_ring = data;
  1746. napi_schedule(&sds_ring->napi);
  1747. return IRQ_HANDLED;
  1748. }
  1749. static int qlcnic_process_cmd_ring(struct qlcnic_adapter *adapter)
  1750. {
  1751. u32 sw_consumer, hw_consumer;
  1752. int count = 0, i;
  1753. struct qlcnic_cmd_buffer *buffer;
  1754. struct pci_dev *pdev = adapter->pdev;
  1755. struct net_device *netdev = adapter->netdev;
  1756. struct qlcnic_skb_frag *frag;
  1757. int done;
  1758. struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
  1759. if (!spin_trylock(&adapter->tx_clean_lock))
  1760. return 1;
  1761. sw_consumer = tx_ring->sw_consumer;
  1762. hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
  1763. while (sw_consumer != hw_consumer) {
  1764. buffer = &tx_ring->cmd_buf_arr[sw_consumer];
  1765. if (buffer->skb) {
  1766. frag = &buffer->frag_array[0];
  1767. pci_unmap_single(pdev, frag->dma, frag->length,
  1768. PCI_DMA_TODEVICE);
  1769. frag->dma = 0ULL;
  1770. for (i = 1; i < buffer->frag_count; i++) {
  1771. frag++;
  1772. pci_unmap_page(pdev, frag->dma, frag->length,
  1773. PCI_DMA_TODEVICE);
  1774. frag->dma = 0ULL;
  1775. }
  1776. adapter->stats.xmitfinished++;
  1777. dev_kfree_skb_any(buffer->skb);
  1778. buffer->skb = NULL;
  1779. }
  1780. sw_consumer = get_next_index(sw_consumer, tx_ring->num_desc);
  1781. if (++count >= MAX_STATUS_HANDLE)
  1782. break;
  1783. }
  1784. if (count && netif_running(netdev)) {
  1785. tx_ring->sw_consumer = sw_consumer;
  1786. smp_mb();
  1787. if (netif_queue_stopped(netdev) && netif_carrier_ok(netdev)) {
  1788. if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH) {
  1789. netif_wake_queue(netdev);
  1790. adapter->stats.xmit_on++;
  1791. }
  1792. }
  1793. adapter->tx_timeo_cnt = 0;
  1794. }
  1795. /*
  1796. * If everything is freed up to consumer then check if the ring is full
  1797. * If the ring is full then check if more needs to be freed and
  1798. * schedule the call back again.
  1799. *
  1800. * This happens when there are 2 CPUs. One could be freeing and the
  1801. * other filling it. If the ring is full when we get out of here and
  1802. * the card has already interrupted the host then the host can miss the
  1803. * interrupt.
  1804. *
  1805. * There is still a possible race condition and the host could miss an
  1806. * interrupt. The card has to take care of this.
  1807. */
  1808. hw_consumer = le32_to_cpu(*(tx_ring->hw_consumer));
  1809. done = (sw_consumer == hw_consumer);
  1810. spin_unlock(&adapter->tx_clean_lock);
  1811. return done;
  1812. }
  1813. static int qlcnic_poll(struct napi_struct *napi, int budget)
  1814. {
  1815. struct qlcnic_host_sds_ring *sds_ring =
  1816. container_of(napi, struct qlcnic_host_sds_ring, napi);
  1817. struct qlcnic_adapter *adapter = sds_ring->adapter;
  1818. int tx_complete;
  1819. int work_done;
  1820. tx_complete = qlcnic_process_cmd_ring(adapter);
  1821. work_done = qlcnic_process_rcv_ring(sds_ring, budget);
  1822. if ((work_done < budget) && tx_complete) {
  1823. napi_complete(&sds_ring->napi);
  1824. if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
  1825. qlcnic_enable_int(sds_ring);
  1826. }
  1827. return work_done;
  1828. }
  1829. static int qlcnic_rx_poll(struct napi_struct *napi, int budget)
  1830. {
  1831. struct qlcnic_host_sds_ring *sds_ring =
  1832. container_of(napi, struct qlcnic_host_sds_ring, napi);
  1833. struct qlcnic_adapter *adapter = sds_ring->adapter;
  1834. int work_done;
  1835. work_done = qlcnic_process_rcv_ring(sds_ring, budget);
  1836. if (work_done < budget) {
  1837. napi_complete(&sds_ring->napi);
  1838. if (test_bit(__QLCNIC_DEV_UP, &adapter->state))
  1839. qlcnic_enable_int(sds_ring);
  1840. }
  1841. return work_done;
  1842. }
  1843. #ifdef CONFIG_NET_POLL_CONTROLLER
  1844. static void qlcnic_poll_controller(struct net_device *netdev)
  1845. {
  1846. struct qlcnic_adapter *adapter = netdev_priv(netdev);
  1847. disable_irq(adapter->irq);
  1848. qlcnic_intr(adapter->irq, adapter);
  1849. enable_irq(adapter->irq);
  1850. }
  1851. #endif
  1852. static void
  1853. qlcnic_idc_debug_info(struct qlcnic_adapter *adapter, u8 encoding)
  1854. {
  1855. u32 val;
  1856. val = adapter->portnum & 0xf;
  1857. val |= encoding << 7;
  1858. val |= (jiffies - adapter->dev_rst_time) << 8;
  1859. QLCWR32(adapter, QLCNIC_CRB_DRV_SCRATCH, val);
  1860. adapter->dev_rst_time = jiffies;
  1861. }
  1862. static int
  1863. qlcnic_set_drv_state(struct qlcnic_adapter *adapter, u8 state)
  1864. {
  1865. u32 val;
  1866. WARN_ON(state != QLCNIC_DEV_NEED_RESET &&
  1867. state != QLCNIC_DEV_NEED_QUISCENT);
  1868. if (qlcnic_api_lock(adapter))
  1869. return -EIO;
  1870. val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
  1871. if (state == QLCNIC_DEV_NEED_RESET)
  1872. QLC_DEV_SET_RST_RDY(val, adapter->portnum);
  1873. else if (state == QLCNIC_DEV_NEED_QUISCENT)
  1874. QLC_DEV_SET_QSCNT_RDY(val, adapter->portnum);
  1875. QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
  1876. qlcnic_api_unlock(adapter);
  1877. return 0;
  1878. }
  1879. static int
  1880. qlcnic_clr_drv_state(struct qlcnic_adapter *adapter)
  1881. {
  1882. u32 val;
  1883. if (qlcnic_api_lock(adapter))
  1884. return -EBUSY;
  1885. val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
  1886. QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
  1887. QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
  1888. qlcnic_api_unlock(adapter);
  1889. return 0;
  1890. }
  1891. static void
  1892. qlcnic_clr_all_drv_state(struct qlcnic_adapter *adapter, u8 failed)
  1893. {
  1894. u32 val;
  1895. if (qlcnic_api_lock(adapter))
  1896. goto err;
  1897. val = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
  1898. QLC_DEV_CLR_REF_CNT(val, adapter->portnum);
  1899. QLCWR32(adapter, QLCNIC_CRB_DEV_REF_COUNT, val);
  1900. if (failed) {
  1901. QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_FAILED);
  1902. dev_info(&adapter->pdev->dev,
  1903. "Device state set to Failed. Please Reboot\n");
  1904. } else if (!(val & 0x11111111))
  1905. QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_COLD);
  1906. val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
  1907. QLC_DEV_CLR_RST_QSCNT(val, adapter->portnum);
  1908. QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
  1909. qlcnic_api_unlock(adapter);
  1910. err:
  1911. adapter->fw_fail_cnt = 0;
  1912. clear_bit(__QLCNIC_START_FW, &adapter->state);
  1913. clear_bit(__QLCNIC_RESETTING, &adapter->state);
  1914. }
  1915. /* Grab api lock, before checking state */
  1916. static int
  1917. qlcnic_check_drv_state(struct qlcnic_adapter *adapter)
  1918. {
  1919. int act, state;
  1920. state = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
  1921. act = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
  1922. if (((state & 0x11111111) == (act & 0x11111111)) ||
  1923. ((act & 0x11111111) == ((state >> 1) & 0x11111111)))
  1924. return 0;
  1925. else
  1926. return 1;
  1927. }
  1928. static int qlcnic_check_idc_ver(struct qlcnic_adapter *adapter)
  1929. {
  1930. u32 val = QLCRD32(adapter, QLCNIC_CRB_DRV_IDC_VER);
  1931. if (val != QLCNIC_DRV_IDC_VER) {
  1932. dev_warn(&adapter->pdev->dev, "IDC Version mismatch, driver's"
  1933. " idc ver = %x; reqd = %x\n", QLCNIC_DRV_IDC_VER, val);
  1934. }
  1935. return 0;
  1936. }
  1937. static int
  1938. qlcnic_can_start_firmware(struct qlcnic_adapter *adapter)
  1939. {
  1940. u32 val, prev_state;
  1941. u8 dev_init_timeo = adapter->dev_init_timeo;
  1942. u8 portnum = adapter->portnum;
  1943. u8 ret;
  1944. if (test_and_clear_bit(__QLCNIC_START_FW, &adapter->state))
  1945. return 1;
  1946. if (qlcnic_api_lock(adapter))
  1947. return -1;
  1948. val = QLCRD32(adapter, QLCNIC_CRB_DEV_REF_COUNT);
  1949. if (!(val & (1 << (portnum * 4)))) {
  1950. QLC_DEV_SET_REF_CNT(val, portnum);
  1951. QLCWR32(adapter, QLCNIC_CRB_DEV_REF_COUNT, val);
  1952. }
  1953. prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
  1954. QLCDB(adapter, HW, "Device state = %u\n", prev_state);
  1955. switch (prev_state) {
  1956. case QLCNIC_DEV_COLD:
  1957. QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
  1958. QLCWR32(adapter, QLCNIC_CRB_DRV_IDC_VER, QLCNIC_DRV_IDC_VER);
  1959. qlcnic_idc_debug_info(adapter, 0);
  1960. qlcnic_api_unlock(adapter);
  1961. return 1;
  1962. case QLCNIC_DEV_READY:
  1963. ret = qlcnic_check_idc_ver(adapter);
  1964. qlcnic_api_unlock(adapter);
  1965. return ret;
  1966. case QLCNIC_DEV_NEED_RESET:
  1967. val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
  1968. QLC_DEV_SET_RST_RDY(val, portnum);
  1969. QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
  1970. break;
  1971. case QLCNIC_DEV_NEED_QUISCENT:
  1972. val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
  1973. QLC_DEV_SET_QSCNT_RDY(val, portnum);
  1974. QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
  1975. break;
  1976. case QLCNIC_DEV_FAILED:
  1977. dev_err(&adapter->pdev->dev, "Device in failed state.\n");
  1978. qlcnic_api_unlock(adapter);
  1979. return -1;
  1980. case QLCNIC_DEV_INITIALIZING:
  1981. case QLCNIC_DEV_QUISCENT:
  1982. break;
  1983. }
  1984. qlcnic_api_unlock(adapter);
  1985. do {
  1986. msleep(1000);
  1987. prev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
  1988. if (prev_state == QLCNIC_DEV_QUISCENT)
  1989. continue;
  1990. } while ((prev_state != QLCNIC_DEV_READY) && --dev_init_timeo);
  1991. if (!dev_init_timeo) {
  1992. dev_err(&adapter->pdev->dev,
  1993. "Waiting for device to initialize timeout\n");
  1994. return -1;
  1995. }
  1996. if (qlcnic_api_lock(adapter))
  1997. return -1;
  1998. val = QLCRD32(adapter, QLCNIC_CRB_DRV_STATE);
  1999. QLC_DEV_CLR_RST_QSCNT(val, portnum);
  2000. QLCWR32(adapter, QLCNIC_CRB_DRV_STATE, val);
  2001. ret = qlcnic_check_idc_ver(adapter);
  2002. qlcnic_api_unlock(adapter);
  2003. return ret;
  2004. }
  2005. static void
  2006. qlcnic_fwinit_work(struct work_struct *work)
  2007. {
  2008. struct qlcnic_adapter *adapter = container_of(work,
  2009. struct qlcnic_adapter, fw_work.work);
  2010. u32 dev_state = 0xf;
  2011. if (qlcnic_api_lock(adapter))
  2012. goto err_ret;
  2013. dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
  2014. if (dev_state == QLCNIC_DEV_QUISCENT) {
  2015. qlcnic_api_unlock(adapter);
  2016. qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
  2017. FW_POLL_DELAY * 2);
  2018. return;
  2019. }
  2020. if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC) {
  2021. qlcnic_api_unlock(adapter);
  2022. goto wait_npar;
  2023. }
  2024. if (adapter->fw_wait_cnt++ > adapter->reset_ack_timeo) {
  2025. dev_err(&adapter->pdev->dev, "Reset:Failed to get ack %d sec\n",
  2026. adapter->reset_ack_timeo);
  2027. goto skip_ack_check;
  2028. }
  2029. if (!qlcnic_check_drv_state(adapter)) {
  2030. skip_ack_check:
  2031. dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
  2032. if (dev_state == QLCNIC_DEV_NEED_QUISCENT) {
  2033. QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
  2034. QLCNIC_DEV_QUISCENT);
  2035. qlcnic_schedule_work(adapter, qlcnic_fwinit_work,
  2036. FW_POLL_DELAY * 2);
  2037. QLCDB(adapter, DRV, "Quiscing the driver\n");
  2038. qlcnic_idc_debug_info(adapter, 0);
  2039. qlcnic_api_unlock(adapter);
  2040. return;
  2041. }
  2042. if (dev_state == QLCNIC_DEV_NEED_RESET) {
  2043. QLCWR32(adapter, QLCNIC_CRB_DEV_STATE,
  2044. QLCNIC_DEV_INITIALIZING);
  2045. set_bit(__QLCNIC_START_FW, &adapter->state);
  2046. QLCDB(adapter, DRV, "Restarting fw\n");
  2047. qlcnic_idc_debug_info(adapter, 0);
  2048. }
  2049. qlcnic_api_unlock(adapter);
  2050. if (!adapter->nic_ops->start_firmware(adapter)) {
  2051. qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
  2052. return;
  2053. }
  2054. goto err_ret;
  2055. }
  2056. qlcnic_api_unlock(adapter);
  2057. wait_npar:
  2058. dev_state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
  2059. QLCDB(adapter, HW, "Func waiting: Device state=%u\n", dev_state);
  2060. switch (dev_state) {
  2061. case QLCNIC_DEV_READY:
  2062. if (!adapter->nic_ops->start_firmware(adapter)) {
  2063. qlcnic_schedule_work(adapter, qlcnic_attach_work, 0);
  2064. return;
  2065. }
  2066. case QLCNIC_DEV_FAILED:
  2067. break;
  2068. default:
  2069. qlcnic_schedule_work(adapter,
  2070. qlcnic_fwinit_work, FW_POLL_DELAY);
  2071. return;
  2072. }
  2073. err_ret:
  2074. dev_err(&adapter->pdev->dev, "Fwinit work failed state=%u "
  2075. "fw_wait_cnt=%u\n", dev_state, adapter->fw_wait_cnt);
  2076. netif_device_attach(adapter->netdev);
  2077. qlcnic_clr_all_drv_state(adapter, 0);
  2078. }
  2079. static void
  2080. qlcnic_detach_work(struct work_struct *work)
  2081. {
  2082. struct qlcnic_adapter *adapter = container_of(work,
  2083. struct qlcnic_adapter, fw_work.work);
  2084. struct net_device *netdev = adapter->netdev;
  2085. u32 status;
  2086. netif_device_detach(netdev);
  2087. qlcnic_down(adapter, netdev);
  2088. status = QLCRD32(adapter, QLCNIC_PEG_HALT_STATUS1);
  2089. if (status & QLCNIC_RCODE_FATAL_ERROR)
  2090. goto err_ret;
  2091. if (adapter->temp == QLCNIC_TEMP_PANIC)
  2092. goto err_ret;
  2093. if (qlcnic_set_drv_state(adapter, adapter->dev_state))
  2094. goto err_ret;
  2095. adapter->fw_wait_cnt = 0;
  2096. qlcnic_schedule_work(adapter, qlcnic_fwinit_work, FW_POLL_DELAY);
  2097. return;
  2098. err_ret:
  2099. dev_err(&adapter->pdev->dev, "detach failed; status=%d temp=%d\n",
  2100. status, adapter->temp);
  2101. netif_device_attach(netdev);
  2102. qlcnic_clr_all_drv_state(adapter, 1);
  2103. }
  2104. /*Transit NPAR state to NON Operational */
  2105. static void
  2106. qlcnic_set_npar_non_operational(struct qlcnic_adapter *adapter)
  2107. {
  2108. u32 state;
  2109. state = QLCRD32(adapter, QLCNIC_CRB_DEV_NPAR_STATE);
  2110. if (state == QLCNIC_DEV_NPAR_NON_OPER)
  2111. return;
  2112. if (qlcnic_api_lock(adapter))
  2113. return;
  2114. QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
  2115. qlcnic_api_unlock(adapter);
  2116. }
  2117. /*Transit to RESET state from READY state only */
  2118. static void
  2119. qlcnic_dev_request_reset(struct qlcnic_adapter *adapter)
  2120. {
  2121. u32 state;
  2122. adapter->need_fw_reset = 1;
  2123. if (qlcnic_api_lock(adapter))
  2124. return;
  2125. state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
  2126. if (state == QLCNIC_DEV_READY) {
  2127. QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_NEED_RESET);
  2128. QLCDB(adapter, DRV, "NEED_RESET state set\n");
  2129. qlcnic_idc_debug_info(adapter, 0);
  2130. }
  2131. QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_NON_OPER);
  2132. qlcnic_api_unlock(adapter);
  2133. }
  2134. /* Transit to NPAR READY state from NPAR NOT READY state */
  2135. static void
  2136. qlcnic_dev_set_npar_ready(struct qlcnic_adapter *adapter)
  2137. {
  2138. if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED) ||
  2139. adapter->op_mode != QLCNIC_MGMT_FUNC)
  2140. return;
  2141. if (qlcnic_api_lock(adapter))
  2142. return;
  2143. QLCWR32(adapter, QLCNIC_CRB_DEV_NPAR_STATE, QLCNIC_DEV_NPAR_OPER);
  2144. QLCDB(adapter, DRV, "NPAR operational state set\n");
  2145. qlcnic_api_unlock(adapter);
  2146. }
  2147. static void
  2148. qlcnic_schedule_work(struct qlcnic_adapter *adapter,
  2149. work_func_t func, int delay)
  2150. {
  2151. if (test_bit(__QLCNIC_AER, &adapter->state))
  2152. return;
  2153. INIT_DELAYED_WORK(&adapter->fw_work, func);
  2154. schedule_delayed_work(&adapter->fw_work, round_jiffies_relative(delay));
  2155. }
  2156. static void
  2157. qlcnic_cancel_fw_work(struct qlcnic_adapter *adapter)
  2158. {
  2159. while (test_and_set_bit(__QLCNIC_RESETTING, &adapter->state))
  2160. msleep(10);
  2161. cancel_delayed_work_sync(&adapter->fw_work);
  2162. }
  2163. static void
  2164. qlcnic_attach_work(struct work_struct *work)
  2165. {
  2166. struct qlcnic_adapter *adapter = container_of(work,
  2167. struct qlcnic_adapter, fw_work.work);
  2168. struct net_device *netdev = adapter->netdev;
  2169. if (netif_running(netdev)) {
  2170. if (qlcnic_up(adapter, netdev))
  2171. goto done;
  2172. qlcnic_config_indev_addr(netdev, NETDEV_UP);
  2173. }
  2174. done:
  2175. netif_device_attach(netdev);
  2176. adapter->fw_fail_cnt = 0;
  2177. clear_bit(__QLCNIC_RESETTING, &adapter->state);
  2178. if (!qlcnic_clr_drv_state(adapter))
  2179. qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
  2180. FW_POLL_DELAY);
  2181. }
  2182. static int
  2183. qlcnic_check_health(struct qlcnic_adapter *adapter)
  2184. {
  2185. u32 state = 0, heartbit;
  2186. struct net_device *netdev = adapter->netdev;
  2187. if (qlcnic_check_temp(adapter))
  2188. goto detach;
  2189. if (adapter->need_fw_reset)
  2190. qlcnic_dev_request_reset(adapter);
  2191. state = QLCRD32(adapter, QLCNIC_CRB_DEV_STATE);
  2192. if (state == QLCNIC_DEV_NEED_RESET ||
  2193. state == QLCNIC_DEV_NEED_QUISCENT) {
  2194. qlcnic_set_npar_non_operational(adapter);
  2195. adapter->need_fw_reset = 1;
  2196. }
  2197. heartbit = QLCRD32(adapter, QLCNIC_PEG_ALIVE_COUNTER);
  2198. if (heartbit != adapter->heartbit) {
  2199. adapter->heartbit = heartbit;
  2200. adapter->fw_fail_cnt = 0;
  2201. if (adapter->need_fw_reset)
  2202. goto detach;
  2203. if (adapter->reset_context &&
  2204. auto_fw_reset == AUTO_FW_RESET_ENABLED) {
  2205. qlcnic_reset_hw_context(adapter);
  2206. adapter->netdev->trans_start = jiffies;
  2207. }
  2208. return 0;
  2209. }
  2210. if (++adapter->fw_fail_cnt < FW_FAIL_THRESH)
  2211. return 0;
  2212. qlcnic_dev_request_reset(adapter);
  2213. if ((auto_fw_reset == AUTO_FW_RESET_ENABLED))
  2214. clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
  2215. dev_info(&netdev->dev, "firmware hang detected\n");
  2216. detach:
  2217. adapter->dev_state = (state == QLCNIC_DEV_NEED_QUISCENT) ? state :
  2218. QLCNIC_DEV_NEED_RESET;
  2219. if ((auto_fw_reset == AUTO_FW_RESET_ENABLED) &&
  2220. !test_and_set_bit(__QLCNIC_RESETTING, &adapter->state)) {
  2221. qlcnic_schedule_work(adapter, qlcnic_detach_work, 0);
  2222. QLCDB(adapter, DRV, "fw recovery scheduled.\n");
  2223. }
  2224. return 1;
  2225. }
  2226. static void
  2227. qlcnic_fw_poll_work(struct work_struct *work)
  2228. {
  2229. struct qlcnic_adapter *adapter = container_of(work,
  2230. struct qlcnic_adapter, fw_work.work);
  2231. if (test_bit(__QLCNIC_RESETTING, &adapter->state))
  2232. goto reschedule;
  2233. if (qlcnic_check_health(adapter))
  2234. return;
  2235. reschedule:
  2236. qlcnic_schedule_work(adapter, qlcnic_fw_poll_work, FW_POLL_DELAY);
  2237. }
  2238. static int qlcnic_is_first_func(struct pci_dev *pdev)
  2239. {
  2240. struct pci_dev *oth_pdev;
  2241. int val = pdev->devfn;
  2242. while (val-- > 0) {
  2243. oth_pdev = pci_get_domain_bus_and_slot(pci_domain_nr
  2244. (pdev->bus), pdev->bus->number,
  2245. PCI_DEVFN(PCI_SLOT(pdev->devfn), val));
  2246. if (!oth_pdev)
  2247. continue;
  2248. if (oth_pdev->current_state != PCI_D3cold) {
  2249. pci_dev_put(oth_pdev);
  2250. return 0;
  2251. }
  2252. pci_dev_put(oth_pdev);
  2253. }
  2254. return 1;
  2255. }
  2256. static int qlcnic_attach_func(struct pci_dev *pdev)
  2257. {
  2258. int err, first_func;
  2259. struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
  2260. struct net_device *netdev = adapter->netdev;
  2261. pdev->error_state = pci_channel_io_normal;
  2262. err = pci_enable_device(pdev);
  2263. if (err)
  2264. return err;
  2265. pci_set_power_state(pdev, PCI_D0);
  2266. pci_set_master(pdev);
  2267. pci_restore_state(pdev);
  2268. first_func = qlcnic_is_first_func(pdev);
  2269. if (qlcnic_api_lock(adapter))
  2270. return -EINVAL;
  2271. if (adapter->op_mode != QLCNIC_NON_PRIV_FUNC && first_func) {
  2272. adapter->need_fw_reset = 1;
  2273. set_bit(__QLCNIC_START_FW, &adapter->state);
  2274. QLCWR32(adapter, QLCNIC_CRB_DEV_STATE, QLCNIC_DEV_INITIALIZING);
  2275. QLCDB(adapter, DRV, "Restarting fw\n");
  2276. }
  2277. qlcnic_api_unlock(adapter);
  2278. err = adapter->nic_ops->start_firmware(adapter);
  2279. if (err)
  2280. return err;
  2281. qlcnic_clr_drv_state(adapter);
  2282. qlcnic_setup_intr(adapter);
  2283. if (netif_running(netdev)) {
  2284. err = qlcnic_attach(adapter);
  2285. if (err) {
  2286. qlcnic_clr_all_drv_state(adapter, 1);
  2287. clear_bit(__QLCNIC_AER, &adapter->state);
  2288. netif_device_attach(netdev);
  2289. return err;
  2290. }
  2291. err = qlcnic_up(adapter, netdev);
  2292. if (err)
  2293. goto done;
  2294. qlcnic_config_indev_addr(netdev, NETDEV_UP);
  2295. }
  2296. done:
  2297. netif_device_attach(netdev);
  2298. return err;
  2299. }
  2300. static pci_ers_result_t qlcnic_io_error_detected(struct pci_dev *pdev,
  2301. pci_channel_state_t state)
  2302. {
  2303. struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
  2304. struct net_device *netdev = adapter->netdev;
  2305. if (state == pci_channel_io_perm_failure)
  2306. return PCI_ERS_RESULT_DISCONNECT;
  2307. if (state == pci_channel_io_normal)
  2308. return PCI_ERS_RESULT_RECOVERED;
  2309. set_bit(__QLCNIC_AER, &adapter->state);
  2310. netif_device_detach(netdev);
  2311. cancel_delayed_work_sync(&adapter->fw_work);
  2312. if (netif_running(netdev))
  2313. qlcnic_down(adapter, netdev);
  2314. qlcnic_detach(adapter);
  2315. qlcnic_teardown_intr(adapter);
  2316. clear_bit(__QLCNIC_RESETTING, &adapter->state);
  2317. pci_save_state(pdev);
  2318. pci_disable_device(pdev);
  2319. return PCI_ERS_RESULT_NEED_RESET;
  2320. }
  2321. static pci_ers_result_t qlcnic_io_slot_reset(struct pci_dev *pdev)
  2322. {
  2323. return qlcnic_attach_func(pdev) ? PCI_ERS_RESULT_DISCONNECT :
  2324. PCI_ERS_RESULT_RECOVERED;
  2325. }
  2326. static void qlcnic_io_resume(struct pci_dev *pdev)
  2327. {
  2328. struct qlcnic_adapter *adapter = pci_get_drvdata(pdev);
  2329. pci_cleanup_aer_uncorrect_error_status(pdev);
  2330. if (QLCRD32(adapter, QLCNIC_CRB_DEV_STATE) == QLCNIC_DEV_READY &&
  2331. test_and_clear_bit(__QLCNIC_AER, &adapter->state))
  2332. qlcnic_schedule_work(adapter, qlcnic_fw_poll_work,
  2333. FW_POLL_DELAY);
  2334. }
  2335. static int
  2336. qlcnicvf_start_firmware(struct qlcnic_adapter *adapter)
  2337. {
  2338. int err;
  2339. err = qlcnic_can_start_firmware(adapter);
  2340. if (err)
  2341. return err;
  2342. err = qlcnic_check_npar_opertional(adapter);
  2343. if (err)
  2344. return err;
  2345. qlcnic_check_options(adapter);
  2346. adapter->need_fw_reset = 0;
  2347. return err;
  2348. }
  2349. static int
  2350. qlcnicvf_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
  2351. {
  2352. return -EOPNOTSUPP;
  2353. }
  2354. static int
  2355. qlcnicvf_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
  2356. {
  2357. return -EOPNOTSUPP;
  2358. }
  2359. static ssize_t
  2360. qlcnic_store_bridged_mode(struct device *dev,
  2361. struct device_attribute *attr, const char *buf, size_t len)
  2362. {
  2363. struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  2364. unsigned long new;
  2365. int ret = -EINVAL;
  2366. if (!(adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG))
  2367. goto err_out;
  2368. if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
  2369. goto err_out;
  2370. if (strict_strtoul(buf, 2, &new))
  2371. goto err_out;
  2372. if (!adapter->nic_ops->config_bridged_mode(adapter, !!new))
  2373. ret = len;
  2374. err_out:
  2375. return ret;
  2376. }
  2377. static ssize_t
  2378. qlcnic_show_bridged_mode(struct device *dev,
  2379. struct device_attribute *attr, char *buf)
  2380. {
  2381. struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  2382. int bridged_mode = 0;
  2383. if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
  2384. bridged_mode = !!(adapter->flags & QLCNIC_BRIDGE_ENABLED);
  2385. return sprintf(buf, "%d\n", bridged_mode);
  2386. }
  2387. static struct device_attribute dev_attr_bridged_mode = {
  2388. .attr = {.name = "bridged_mode", .mode = (S_IRUGO | S_IWUSR)},
  2389. .show = qlcnic_show_bridged_mode,
  2390. .store = qlcnic_store_bridged_mode,
  2391. };
  2392. static ssize_t
  2393. qlcnic_store_diag_mode(struct device *dev,
  2394. struct device_attribute *attr, const char *buf, size_t len)
  2395. {
  2396. struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  2397. unsigned long new;
  2398. if (strict_strtoul(buf, 2, &new))
  2399. return -EINVAL;
  2400. if (!!new != !!(adapter->flags & QLCNIC_DIAG_ENABLED))
  2401. adapter->flags ^= QLCNIC_DIAG_ENABLED;
  2402. return len;
  2403. }
  2404. static ssize_t
  2405. qlcnic_show_diag_mode(struct device *dev,
  2406. struct device_attribute *attr, char *buf)
  2407. {
  2408. struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  2409. return sprintf(buf, "%d\n",
  2410. !!(adapter->flags & QLCNIC_DIAG_ENABLED));
  2411. }
  2412. static struct device_attribute dev_attr_diag_mode = {
  2413. .attr = {.name = "diag_mode", .mode = (S_IRUGO | S_IWUSR)},
  2414. .show = qlcnic_show_diag_mode,
  2415. .store = qlcnic_store_diag_mode,
  2416. };
  2417. static int
  2418. qlcnic_sysfs_validate_crb(struct qlcnic_adapter *adapter,
  2419. loff_t offset, size_t size)
  2420. {
  2421. size_t crb_size = 4;
  2422. if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
  2423. return -EIO;
  2424. if (offset < QLCNIC_PCI_CRBSPACE) {
  2425. if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM,
  2426. QLCNIC_PCI_CAMQM_END))
  2427. crb_size = 8;
  2428. else
  2429. return -EINVAL;
  2430. }
  2431. if ((size != crb_size) || (offset & (crb_size-1)))
  2432. return -EINVAL;
  2433. return 0;
  2434. }
  2435. static ssize_t
  2436. qlcnic_sysfs_read_crb(struct file *filp, struct kobject *kobj,
  2437. struct bin_attribute *attr,
  2438. char *buf, loff_t offset, size_t size)
  2439. {
  2440. struct device *dev = container_of(kobj, struct device, kobj);
  2441. struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  2442. u32 data;
  2443. u64 qmdata;
  2444. int ret;
  2445. ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
  2446. if (ret != 0)
  2447. return ret;
  2448. if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
  2449. qlcnic_pci_camqm_read_2M(adapter, offset, &qmdata);
  2450. memcpy(buf, &qmdata, size);
  2451. } else {
  2452. data = QLCRD32(adapter, offset);
  2453. memcpy(buf, &data, size);
  2454. }
  2455. return size;
  2456. }
  2457. static ssize_t
  2458. qlcnic_sysfs_write_crb(struct file *filp, struct kobject *kobj,
  2459. struct bin_attribute *attr,
  2460. char *buf, loff_t offset, size_t size)
  2461. {
  2462. struct device *dev = container_of(kobj, struct device, kobj);
  2463. struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  2464. u32 data;
  2465. u64 qmdata;
  2466. int ret;
  2467. ret = qlcnic_sysfs_validate_crb(adapter, offset, size);
  2468. if (ret != 0)
  2469. return ret;
  2470. if (ADDR_IN_RANGE(offset, QLCNIC_PCI_CAMQM, QLCNIC_PCI_CAMQM_END)) {
  2471. memcpy(&qmdata, buf, size);
  2472. qlcnic_pci_camqm_write_2M(adapter, offset, qmdata);
  2473. } else {
  2474. memcpy(&data, buf, size);
  2475. QLCWR32(adapter, offset, data);
  2476. }
  2477. return size;
  2478. }
  2479. static int
  2480. qlcnic_sysfs_validate_mem(struct qlcnic_adapter *adapter,
  2481. loff_t offset, size_t size)
  2482. {
  2483. if (!(adapter->flags & QLCNIC_DIAG_ENABLED))
  2484. return -EIO;
  2485. if ((size != 8) || (offset & 0x7))
  2486. return -EIO;
  2487. return 0;
  2488. }
  2489. static ssize_t
  2490. qlcnic_sysfs_read_mem(struct file *filp, struct kobject *kobj,
  2491. struct bin_attribute *attr,
  2492. char *buf, loff_t offset, size_t size)
  2493. {
  2494. struct device *dev = container_of(kobj, struct device, kobj);
  2495. struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  2496. u64 data;
  2497. int ret;
  2498. ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
  2499. if (ret != 0)
  2500. return ret;
  2501. if (qlcnic_pci_mem_read_2M(adapter, offset, &data))
  2502. return -EIO;
  2503. memcpy(buf, &data, size);
  2504. return size;
  2505. }
  2506. static ssize_t
  2507. qlcnic_sysfs_write_mem(struct file *filp, struct kobject *kobj,
  2508. struct bin_attribute *attr,
  2509. char *buf, loff_t offset, size_t size)
  2510. {
  2511. struct device *dev = container_of(kobj, struct device, kobj);
  2512. struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  2513. u64 data;
  2514. int ret;
  2515. ret = qlcnic_sysfs_validate_mem(adapter, offset, size);
  2516. if (ret != 0)
  2517. return ret;
  2518. memcpy(&data, buf, size);
  2519. if (qlcnic_pci_mem_write_2M(adapter, offset, data))
  2520. return -EIO;
  2521. return size;
  2522. }
  2523. static struct bin_attribute bin_attr_crb = {
  2524. .attr = {.name = "crb", .mode = (S_IRUGO | S_IWUSR)},
  2525. .size = 0,
  2526. .read = qlcnic_sysfs_read_crb,
  2527. .write = qlcnic_sysfs_write_crb,
  2528. };
  2529. static struct bin_attribute bin_attr_mem = {
  2530. .attr = {.name = "mem", .mode = (S_IRUGO | S_IWUSR)},
  2531. .size = 0,
  2532. .read = qlcnic_sysfs_read_mem,
  2533. .write = qlcnic_sysfs_write_mem,
  2534. };
  2535. static int
  2536. validate_pm_config(struct qlcnic_adapter *adapter,
  2537. struct qlcnic_pm_func_cfg *pm_cfg, int count)
  2538. {
  2539. u8 src_pci_func, s_esw_id, d_esw_id;
  2540. u8 dest_pci_func;
  2541. int i;
  2542. for (i = 0; i < count; i++) {
  2543. src_pci_func = pm_cfg[i].pci_func;
  2544. dest_pci_func = pm_cfg[i].dest_npar;
  2545. if (src_pci_func >= QLCNIC_MAX_PCI_FUNC
  2546. || dest_pci_func >= QLCNIC_MAX_PCI_FUNC)
  2547. return QL_STATUS_INVALID_PARAM;
  2548. if (adapter->npars[src_pci_func].type != QLCNIC_TYPE_NIC)
  2549. return QL_STATUS_INVALID_PARAM;
  2550. if (adapter->npars[dest_pci_func].type != QLCNIC_TYPE_NIC)
  2551. return QL_STATUS_INVALID_PARAM;
  2552. s_esw_id = adapter->npars[src_pci_func].phy_port;
  2553. d_esw_id = adapter->npars[dest_pci_func].phy_port;
  2554. if (s_esw_id != d_esw_id)
  2555. return QL_STATUS_INVALID_PARAM;
  2556. }
  2557. return 0;
  2558. }
  2559. static ssize_t
  2560. qlcnic_sysfs_write_pm_config(struct file *filp, struct kobject *kobj,
  2561. struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
  2562. {
  2563. struct device *dev = container_of(kobj, struct device, kobj);
  2564. struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  2565. struct qlcnic_pm_func_cfg *pm_cfg;
  2566. u32 id, action, pci_func;
  2567. int count, rem, i, ret;
  2568. count = size / sizeof(struct qlcnic_pm_func_cfg);
  2569. rem = size % sizeof(struct qlcnic_pm_func_cfg);
  2570. if (rem)
  2571. return QL_STATUS_INVALID_PARAM;
  2572. pm_cfg = (struct qlcnic_pm_func_cfg *) buf;
  2573. ret = validate_pm_config(adapter, pm_cfg, count);
  2574. if (ret)
  2575. return ret;
  2576. for (i = 0; i < count; i++) {
  2577. pci_func = pm_cfg[i].pci_func;
  2578. action = !!pm_cfg[i].action;
  2579. id = adapter->npars[pci_func].phy_port;
  2580. ret = qlcnic_config_port_mirroring(adapter, id,
  2581. action, pci_func);
  2582. if (ret)
  2583. return ret;
  2584. }
  2585. for (i = 0; i < count; i++) {
  2586. pci_func = pm_cfg[i].pci_func;
  2587. id = adapter->npars[pci_func].phy_port;
  2588. adapter->npars[pci_func].enable_pm = !!pm_cfg[i].action;
  2589. adapter->npars[pci_func].dest_npar = id;
  2590. }
  2591. return size;
  2592. }
  2593. static ssize_t
  2594. qlcnic_sysfs_read_pm_config(struct file *filp, struct kobject *kobj,
  2595. struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
  2596. {
  2597. struct device *dev = container_of(kobj, struct device, kobj);
  2598. struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  2599. struct qlcnic_pm_func_cfg pm_cfg[QLCNIC_MAX_PCI_FUNC];
  2600. int i;
  2601. if (size != sizeof(pm_cfg))
  2602. return QL_STATUS_INVALID_PARAM;
  2603. for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
  2604. if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
  2605. continue;
  2606. pm_cfg[i].action = adapter->npars[i].enable_pm;
  2607. pm_cfg[i].dest_npar = 0;
  2608. pm_cfg[i].pci_func = i;
  2609. }
  2610. memcpy(buf, &pm_cfg, size);
  2611. return size;
  2612. }
  2613. static int
  2614. validate_esw_config(struct qlcnic_adapter *adapter,
  2615. struct qlcnic_esw_func_cfg *esw_cfg, int count)
  2616. {
  2617. u8 pci_func;
  2618. int i;
  2619. for (i = 0; i < count; i++) {
  2620. pci_func = esw_cfg[i].pci_func;
  2621. if (pci_func >= QLCNIC_MAX_PCI_FUNC)
  2622. return QL_STATUS_INVALID_PARAM;
  2623. if (adapter->op_mode == QLCNIC_MGMT_FUNC)
  2624. if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
  2625. return QL_STATUS_INVALID_PARAM;
  2626. switch (esw_cfg[i].op_mode) {
  2627. case QLCNIC_PORT_DEFAULTS:
  2628. break;
  2629. case QLCNIC_ADD_VLAN:
  2630. if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
  2631. return QL_STATUS_INVALID_PARAM;
  2632. if (!esw_cfg[i].op_type)
  2633. return QL_STATUS_INVALID_PARAM;
  2634. break;
  2635. case QLCNIC_DEL_VLAN:
  2636. if (!IS_VALID_VLAN(esw_cfg[i].vlan_id))
  2637. return QL_STATUS_INVALID_PARAM;
  2638. if (!esw_cfg[i].op_type)
  2639. return QL_STATUS_INVALID_PARAM;
  2640. break;
  2641. default:
  2642. return QL_STATUS_INVALID_PARAM;
  2643. }
  2644. }
  2645. return 0;
  2646. }
  2647. static ssize_t
  2648. qlcnic_sysfs_write_esw_config(struct file *file, struct kobject *kobj,
  2649. struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
  2650. {
  2651. struct device *dev = container_of(kobj, struct device, kobj);
  2652. struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  2653. struct qlcnic_esw_func_cfg *esw_cfg;
  2654. struct qlcnic_npar_info *npar;
  2655. int count, rem, i, ret;
  2656. u8 pci_func, op_mode = 0;
  2657. count = size / sizeof(struct qlcnic_esw_func_cfg);
  2658. rem = size % sizeof(struct qlcnic_esw_func_cfg);
  2659. if (rem)
  2660. return QL_STATUS_INVALID_PARAM;
  2661. esw_cfg = (struct qlcnic_esw_func_cfg *) buf;
  2662. ret = validate_esw_config(adapter, esw_cfg, count);
  2663. if (ret)
  2664. return ret;
  2665. for (i = 0; i < count; i++) {
  2666. if (adapter->op_mode == QLCNIC_MGMT_FUNC)
  2667. if (qlcnic_config_switch_port(adapter, &esw_cfg[i]))
  2668. return QL_STATUS_INVALID_PARAM;
  2669. if (adapter->ahw.pci_func == esw_cfg[i].pci_func)
  2670. op_mode = esw_cfg[i].op_mode;
  2671. qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]);
  2672. esw_cfg[i].op_mode = op_mode;
  2673. esw_cfg[i].pci_func = adapter->ahw.pci_func;
  2674. switch (esw_cfg[i].op_mode) {
  2675. case QLCNIC_PORT_DEFAULTS:
  2676. qlcnic_set_eswitch_port_features(adapter,
  2677. &esw_cfg[i]);
  2678. break;
  2679. }
  2680. }
  2681. if (adapter->op_mode != QLCNIC_MGMT_FUNC)
  2682. goto out;
  2683. for (i = 0; i < count; i++) {
  2684. pci_func = esw_cfg[i].pci_func;
  2685. npar = &adapter->npars[pci_func];
  2686. switch (esw_cfg[i].op_mode) {
  2687. case QLCNIC_PORT_DEFAULTS:
  2688. npar->promisc_mode = esw_cfg[i].promisc_mode;
  2689. npar->mac_learning = esw_cfg[i].mac_learning;
  2690. npar->offload_flags = esw_cfg[i].offload_flags;
  2691. npar->mac_anti_spoof = esw_cfg[i].mac_anti_spoof;
  2692. npar->discard_tagged = esw_cfg[i].discard_tagged;
  2693. break;
  2694. case QLCNIC_ADD_VLAN:
  2695. npar->pvid = esw_cfg[i].vlan_id;
  2696. break;
  2697. case QLCNIC_DEL_VLAN:
  2698. npar->pvid = 0;
  2699. break;
  2700. }
  2701. }
  2702. out:
  2703. return size;
  2704. }
  2705. static ssize_t
  2706. qlcnic_sysfs_read_esw_config(struct file *file, struct kobject *kobj,
  2707. struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
  2708. {
  2709. struct device *dev = container_of(kobj, struct device, kobj);
  2710. struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  2711. struct qlcnic_esw_func_cfg esw_cfg[QLCNIC_MAX_PCI_FUNC];
  2712. u8 i;
  2713. if (size != sizeof(esw_cfg))
  2714. return QL_STATUS_INVALID_PARAM;
  2715. for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
  2716. if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
  2717. continue;
  2718. esw_cfg[i].pci_func = i;
  2719. if (qlcnic_get_eswitch_port_config(adapter, &esw_cfg[i]))
  2720. return QL_STATUS_INVALID_PARAM;
  2721. }
  2722. memcpy(buf, &esw_cfg, size);
  2723. return size;
  2724. }
  2725. static int
  2726. validate_npar_config(struct qlcnic_adapter *adapter,
  2727. struct qlcnic_npar_func_cfg *np_cfg, int count)
  2728. {
  2729. u8 pci_func, i;
  2730. for (i = 0; i < count; i++) {
  2731. pci_func = np_cfg[i].pci_func;
  2732. if (pci_func >= QLCNIC_MAX_PCI_FUNC)
  2733. return QL_STATUS_INVALID_PARAM;
  2734. if (adapter->npars[pci_func].type != QLCNIC_TYPE_NIC)
  2735. return QL_STATUS_INVALID_PARAM;
  2736. if (!IS_VALID_BW(np_cfg[i].min_bw)
  2737. || !IS_VALID_BW(np_cfg[i].max_bw)
  2738. || !IS_VALID_RX_QUEUES(np_cfg[i].max_rx_queues)
  2739. || !IS_VALID_TX_QUEUES(np_cfg[i].max_tx_queues))
  2740. return QL_STATUS_INVALID_PARAM;
  2741. }
  2742. return 0;
  2743. }
  2744. static ssize_t
  2745. qlcnic_sysfs_write_npar_config(struct file *file, struct kobject *kobj,
  2746. struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
  2747. {
  2748. struct device *dev = container_of(kobj, struct device, kobj);
  2749. struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  2750. struct qlcnic_info nic_info;
  2751. struct qlcnic_npar_func_cfg *np_cfg;
  2752. int i, count, rem, ret;
  2753. u8 pci_func;
  2754. count = size / sizeof(struct qlcnic_npar_func_cfg);
  2755. rem = size % sizeof(struct qlcnic_npar_func_cfg);
  2756. if (rem)
  2757. return QL_STATUS_INVALID_PARAM;
  2758. np_cfg = (struct qlcnic_npar_func_cfg *) buf;
  2759. ret = validate_npar_config(adapter, np_cfg, count);
  2760. if (ret)
  2761. return ret;
  2762. for (i = 0; i < count ; i++) {
  2763. pci_func = np_cfg[i].pci_func;
  2764. ret = qlcnic_get_nic_info(adapter, &nic_info, pci_func);
  2765. if (ret)
  2766. return ret;
  2767. nic_info.pci_func = pci_func;
  2768. nic_info.min_tx_bw = np_cfg[i].min_bw;
  2769. nic_info.max_tx_bw = np_cfg[i].max_bw;
  2770. ret = qlcnic_set_nic_info(adapter, &nic_info);
  2771. if (ret)
  2772. return ret;
  2773. adapter->npars[i].min_bw = nic_info.min_tx_bw;
  2774. adapter->npars[i].max_bw = nic_info.max_tx_bw;
  2775. }
  2776. return size;
  2777. }
  2778. static ssize_t
  2779. qlcnic_sysfs_read_npar_config(struct file *file, struct kobject *kobj,
  2780. struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
  2781. {
  2782. struct device *dev = container_of(kobj, struct device, kobj);
  2783. struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  2784. struct qlcnic_info nic_info;
  2785. struct qlcnic_npar_func_cfg np_cfg[QLCNIC_MAX_PCI_FUNC];
  2786. int i, ret;
  2787. if (size != sizeof(np_cfg))
  2788. return QL_STATUS_INVALID_PARAM;
  2789. for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
  2790. if (adapter->npars[i].type != QLCNIC_TYPE_NIC)
  2791. continue;
  2792. ret = qlcnic_get_nic_info(adapter, &nic_info, i);
  2793. if (ret)
  2794. return ret;
  2795. np_cfg[i].pci_func = i;
  2796. np_cfg[i].op_mode = nic_info.op_mode;
  2797. np_cfg[i].port_num = nic_info.phys_port;
  2798. np_cfg[i].fw_capab = nic_info.capabilities;
  2799. np_cfg[i].min_bw = nic_info.min_tx_bw ;
  2800. np_cfg[i].max_bw = nic_info.max_tx_bw;
  2801. np_cfg[i].max_tx_queues = nic_info.max_tx_ques;
  2802. np_cfg[i].max_rx_queues = nic_info.max_rx_ques;
  2803. }
  2804. memcpy(buf, &np_cfg, size);
  2805. return size;
  2806. }
  2807. static ssize_t
  2808. qlcnic_sysfs_get_port_stats(struct file *file, struct kobject *kobj,
  2809. struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
  2810. {
  2811. struct device *dev = container_of(kobj, struct device, kobj);
  2812. struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  2813. struct qlcnic_esw_statistics port_stats;
  2814. int ret;
  2815. if (size != sizeof(struct qlcnic_esw_statistics))
  2816. return QL_STATUS_INVALID_PARAM;
  2817. if (offset >= QLCNIC_MAX_PCI_FUNC)
  2818. return QL_STATUS_INVALID_PARAM;
  2819. memset(&port_stats, 0, size);
  2820. ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
  2821. &port_stats.rx);
  2822. if (ret)
  2823. return ret;
  2824. ret = qlcnic_get_port_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
  2825. &port_stats.tx);
  2826. if (ret)
  2827. return ret;
  2828. memcpy(buf, &port_stats, size);
  2829. return size;
  2830. }
  2831. static ssize_t
  2832. qlcnic_sysfs_get_esw_stats(struct file *file, struct kobject *kobj,
  2833. struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
  2834. {
  2835. struct device *dev = container_of(kobj, struct device, kobj);
  2836. struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  2837. struct qlcnic_esw_statistics esw_stats;
  2838. int ret;
  2839. if (size != sizeof(struct qlcnic_esw_statistics))
  2840. return QL_STATUS_INVALID_PARAM;
  2841. if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
  2842. return QL_STATUS_INVALID_PARAM;
  2843. memset(&esw_stats, 0, size);
  2844. ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_RX_COUNTER,
  2845. &esw_stats.rx);
  2846. if (ret)
  2847. return ret;
  2848. ret = qlcnic_get_eswitch_stats(adapter, offset, QLCNIC_QUERY_TX_COUNTER,
  2849. &esw_stats.tx);
  2850. if (ret)
  2851. return ret;
  2852. memcpy(buf, &esw_stats, size);
  2853. return size;
  2854. }
  2855. static ssize_t
  2856. qlcnic_sysfs_clear_esw_stats(struct file *file, struct kobject *kobj,
  2857. struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
  2858. {
  2859. struct device *dev = container_of(kobj, struct device, kobj);
  2860. struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  2861. int ret;
  2862. if (offset >= QLCNIC_NIU_MAX_XG_PORTS)
  2863. return QL_STATUS_INVALID_PARAM;
  2864. ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
  2865. QLCNIC_QUERY_RX_COUNTER);
  2866. if (ret)
  2867. return ret;
  2868. ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_ESWITCH, offset,
  2869. QLCNIC_QUERY_TX_COUNTER);
  2870. if (ret)
  2871. return ret;
  2872. return size;
  2873. }
  2874. static ssize_t
  2875. qlcnic_sysfs_clear_port_stats(struct file *file, struct kobject *kobj,
  2876. struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
  2877. {
  2878. struct device *dev = container_of(kobj, struct device, kobj);
  2879. struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  2880. int ret;
  2881. if (offset >= QLCNIC_MAX_PCI_FUNC)
  2882. return QL_STATUS_INVALID_PARAM;
  2883. ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
  2884. QLCNIC_QUERY_RX_COUNTER);
  2885. if (ret)
  2886. return ret;
  2887. ret = qlcnic_clear_esw_stats(adapter, QLCNIC_STATS_PORT, offset,
  2888. QLCNIC_QUERY_TX_COUNTER);
  2889. if (ret)
  2890. return ret;
  2891. return size;
  2892. }
  2893. static ssize_t
  2894. qlcnic_sysfs_read_pci_config(struct file *file, struct kobject *kobj,
  2895. struct bin_attribute *attr, char *buf, loff_t offset, size_t size)
  2896. {
  2897. struct device *dev = container_of(kobj, struct device, kobj);
  2898. struct qlcnic_adapter *adapter = dev_get_drvdata(dev);
  2899. struct qlcnic_pci_func_cfg pci_cfg[QLCNIC_MAX_PCI_FUNC];
  2900. struct qlcnic_pci_info *pci_info;
  2901. int i, ret;
  2902. if (size != sizeof(pci_cfg))
  2903. return QL_STATUS_INVALID_PARAM;
  2904. pci_info = kcalloc(QLCNIC_MAX_PCI_FUNC, sizeof(*pci_info), GFP_KERNEL);
  2905. if (!pci_info)
  2906. return -ENOMEM;
  2907. ret = qlcnic_get_pci_info(adapter, pci_info);
  2908. if (ret) {
  2909. kfree(pci_info);
  2910. return ret;
  2911. }
  2912. for (i = 0; i < QLCNIC_MAX_PCI_FUNC ; i++) {
  2913. pci_cfg[i].pci_func = pci_info[i].id;
  2914. pci_cfg[i].func_type = pci_info[i].type;
  2915. pci_cfg[i].port_num = pci_info[i].default_port;
  2916. pci_cfg[i].min_bw = pci_info[i].tx_min_bw;
  2917. pci_cfg[i].max_bw = pci_info[i].tx_max_bw;
  2918. memcpy(&pci_cfg[i].def_mac_addr, &pci_info[i].mac, ETH_ALEN);
  2919. }
  2920. memcpy(buf, &pci_cfg, size);
  2921. kfree(pci_info);
  2922. return size;
  2923. }
  2924. static struct bin_attribute bin_attr_npar_config = {
  2925. .attr = {.name = "npar_config", .mode = (S_IRUGO | S_IWUSR)},
  2926. .size = 0,
  2927. .read = qlcnic_sysfs_read_npar_config,
  2928. .write = qlcnic_sysfs_write_npar_config,
  2929. };
  2930. static struct bin_attribute bin_attr_pci_config = {
  2931. .attr = {.name = "pci_config", .mode = (S_IRUGO | S_IWUSR)},
  2932. .size = 0,
  2933. .read = qlcnic_sysfs_read_pci_config,
  2934. .write = NULL,
  2935. };
  2936. static struct bin_attribute bin_attr_port_stats = {
  2937. .attr = {.name = "port_stats", .mode = (S_IRUGO | S_IWUSR)},
  2938. .size = 0,
  2939. .read = qlcnic_sysfs_get_port_stats,
  2940. .write = qlcnic_sysfs_clear_port_stats,
  2941. };
  2942. static struct bin_attribute bin_attr_esw_stats = {
  2943. .attr = {.name = "esw_stats", .mode = (S_IRUGO | S_IWUSR)},
  2944. .size = 0,
  2945. .read = qlcnic_sysfs_get_esw_stats,
  2946. .write = qlcnic_sysfs_clear_esw_stats,
  2947. };
  2948. static struct bin_attribute bin_attr_esw_config = {
  2949. .attr = {.name = "esw_config", .mode = (S_IRUGO | S_IWUSR)},
  2950. .size = 0,
  2951. .read = qlcnic_sysfs_read_esw_config,
  2952. .write = qlcnic_sysfs_write_esw_config,
  2953. };
  2954. static struct bin_attribute bin_attr_pm_config = {
  2955. .attr = {.name = "pm_config", .mode = (S_IRUGO | S_IWUSR)},
  2956. .size = 0,
  2957. .read = qlcnic_sysfs_read_pm_config,
  2958. .write = qlcnic_sysfs_write_pm_config,
  2959. };
  2960. static void
  2961. qlcnic_create_sysfs_entries(struct qlcnic_adapter *adapter)
  2962. {
  2963. struct device *dev = &adapter->pdev->dev;
  2964. if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
  2965. if (device_create_file(dev, &dev_attr_bridged_mode))
  2966. dev_warn(dev,
  2967. "failed to create bridged_mode sysfs entry\n");
  2968. }
  2969. static void
  2970. qlcnic_remove_sysfs_entries(struct qlcnic_adapter *adapter)
  2971. {
  2972. struct device *dev = &adapter->pdev->dev;
  2973. if (adapter->capabilities & QLCNIC_FW_CAPABILITY_BDG)
  2974. device_remove_file(dev, &dev_attr_bridged_mode);
  2975. }
  2976. static void
  2977. qlcnic_create_diag_entries(struct qlcnic_adapter *adapter)
  2978. {
  2979. struct device *dev = &adapter->pdev->dev;
  2980. if (device_create_bin_file(dev, &bin_attr_port_stats))
  2981. dev_info(dev, "failed to create port stats sysfs entry");
  2982. if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
  2983. return;
  2984. if (device_create_file(dev, &dev_attr_diag_mode))
  2985. dev_info(dev, "failed to create diag_mode sysfs entry\n");
  2986. if (device_create_bin_file(dev, &bin_attr_crb))
  2987. dev_info(dev, "failed to create crb sysfs entry\n");
  2988. if (device_create_bin_file(dev, &bin_attr_mem))
  2989. dev_info(dev, "failed to create mem sysfs entry\n");
  2990. if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
  2991. return;
  2992. if (device_create_bin_file(dev, &bin_attr_esw_config))
  2993. dev_info(dev, "failed to create esw config sysfs entry");
  2994. if (adapter->op_mode != QLCNIC_MGMT_FUNC)
  2995. return;
  2996. if (device_create_bin_file(dev, &bin_attr_pci_config))
  2997. dev_info(dev, "failed to create pci config sysfs entry");
  2998. if (device_create_bin_file(dev, &bin_attr_npar_config))
  2999. dev_info(dev, "failed to create npar config sysfs entry");
  3000. if (device_create_bin_file(dev, &bin_attr_pm_config))
  3001. dev_info(dev, "failed to create pm config sysfs entry");
  3002. if (device_create_bin_file(dev, &bin_attr_esw_stats))
  3003. dev_info(dev, "failed to create eswitch stats sysfs entry");
  3004. }
  3005. static void
  3006. qlcnic_remove_diag_entries(struct qlcnic_adapter *adapter)
  3007. {
  3008. struct device *dev = &adapter->pdev->dev;
  3009. device_remove_bin_file(dev, &bin_attr_port_stats);
  3010. if (adapter->op_mode == QLCNIC_NON_PRIV_FUNC)
  3011. return;
  3012. device_remove_file(dev, &dev_attr_diag_mode);
  3013. device_remove_bin_file(dev, &bin_attr_crb);
  3014. device_remove_bin_file(dev, &bin_attr_mem);
  3015. if (!(adapter->flags & QLCNIC_ESWITCH_ENABLED))
  3016. return;
  3017. device_remove_bin_file(dev, &bin_attr_esw_config);
  3018. if (adapter->op_mode != QLCNIC_MGMT_FUNC)
  3019. return;
  3020. device_remove_bin_file(dev, &bin_attr_pci_config);
  3021. device_remove_bin_file(dev, &bin_attr_npar_config);
  3022. device_remove_bin_file(dev, &bin_attr_pm_config);
  3023. device_remove_bin_file(dev, &bin_attr_esw_stats);
  3024. }
  3025. #ifdef CONFIG_INET
  3026. #define is_qlcnic_netdev(dev) (dev->netdev_ops == &qlcnic_netdev_ops)
  3027. static void
  3028. qlcnic_config_indev_addr(struct net_device *dev, unsigned long event)
  3029. {
  3030. struct in_device *indev;
  3031. struct qlcnic_adapter *adapter = netdev_priv(dev);
  3032. indev = in_dev_get(dev);
  3033. if (!indev)
  3034. return;
  3035. for_ifa(indev) {
  3036. switch (event) {
  3037. case NETDEV_UP:
  3038. qlcnic_config_ipaddr(adapter,
  3039. ifa->ifa_address, QLCNIC_IP_UP);
  3040. break;
  3041. case NETDEV_DOWN:
  3042. qlcnic_config_ipaddr(adapter,
  3043. ifa->ifa_address, QLCNIC_IP_DOWN);
  3044. break;
  3045. default:
  3046. break;
  3047. }
  3048. } endfor_ifa(indev);
  3049. in_dev_put(indev);
  3050. }
  3051. static int qlcnic_netdev_event(struct notifier_block *this,
  3052. unsigned long event, void *ptr)
  3053. {
  3054. struct qlcnic_adapter *adapter;
  3055. struct net_device *dev = (struct net_device *)ptr;
  3056. recheck:
  3057. if (dev == NULL)
  3058. goto done;
  3059. if (dev->priv_flags & IFF_802_1Q_VLAN) {
  3060. dev = vlan_dev_real_dev(dev);
  3061. goto recheck;
  3062. }
  3063. if (!is_qlcnic_netdev(dev))
  3064. goto done;
  3065. adapter = netdev_priv(dev);
  3066. if (!adapter)
  3067. goto done;
  3068. if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
  3069. goto done;
  3070. qlcnic_config_indev_addr(dev, event);
  3071. done:
  3072. return NOTIFY_DONE;
  3073. }
  3074. static int
  3075. qlcnic_inetaddr_event(struct notifier_block *this,
  3076. unsigned long event, void *ptr)
  3077. {
  3078. struct qlcnic_adapter *adapter;
  3079. struct net_device *dev;
  3080. struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
  3081. dev = ifa->ifa_dev ? ifa->ifa_dev->dev : NULL;
  3082. recheck:
  3083. if (dev == NULL || !netif_running(dev))
  3084. goto done;
  3085. if (dev->priv_flags & IFF_802_1Q_VLAN) {
  3086. dev = vlan_dev_real_dev(dev);
  3087. goto recheck;
  3088. }
  3089. if (!is_qlcnic_netdev(dev))
  3090. goto done;
  3091. adapter = netdev_priv(dev);
  3092. if (!adapter)
  3093. goto done;
  3094. if (!test_bit(__QLCNIC_DEV_UP, &adapter->state))
  3095. goto done;
  3096. switch (event) {
  3097. case NETDEV_UP:
  3098. qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_UP);
  3099. break;
  3100. case NETDEV_DOWN:
  3101. qlcnic_config_ipaddr(adapter, ifa->ifa_address, QLCNIC_IP_DOWN);
  3102. break;
  3103. default:
  3104. break;
  3105. }
  3106. done:
  3107. return NOTIFY_DONE;
  3108. }
  3109. static struct notifier_block qlcnic_netdev_cb = {
  3110. .notifier_call = qlcnic_netdev_event,
  3111. };
  3112. static struct notifier_block qlcnic_inetaddr_cb = {
  3113. .notifier_call = qlcnic_inetaddr_event,
  3114. };
  3115. #else
  3116. static void
  3117. qlcnic_config_indev_addr(struct net_device *dev, unsigned long event)
  3118. { }
  3119. #endif
  3120. static struct pci_error_handlers qlcnic_err_handler = {
  3121. .error_detected = qlcnic_io_error_detected,
  3122. .slot_reset = qlcnic_io_slot_reset,
  3123. .resume = qlcnic_io_resume,
  3124. };
  3125. static struct pci_driver qlcnic_driver = {
  3126. .name = qlcnic_driver_name,
  3127. .id_table = qlcnic_pci_tbl,
  3128. .probe = qlcnic_probe,
  3129. .remove = __devexit_p(qlcnic_remove),
  3130. #ifdef CONFIG_PM
  3131. .suspend = qlcnic_suspend,
  3132. .resume = qlcnic_resume,
  3133. #endif
  3134. .shutdown = qlcnic_shutdown,
  3135. .err_handler = &qlcnic_err_handler
  3136. };
  3137. static int __init qlcnic_init_module(void)
  3138. {
  3139. int ret;
  3140. printk(KERN_INFO "%s\n", qlcnic_driver_string);
  3141. #ifdef CONFIG_INET
  3142. register_netdevice_notifier(&qlcnic_netdev_cb);
  3143. register_inetaddr_notifier(&qlcnic_inetaddr_cb);
  3144. #endif
  3145. ret = pci_register_driver(&qlcnic_driver);
  3146. if (ret) {
  3147. #ifdef CONFIG_INET
  3148. unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
  3149. unregister_netdevice_notifier(&qlcnic_netdev_cb);
  3150. #endif
  3151. }
  3152. return ret;
  3153. }
  3154. module_init(qlcnic_init_module);
  3155. static void __exit qlcnic_exit_module(void)
  3156. {
  3157. pci_unregister_driver(&qlcnic_driver);
  3158. #ifdef CONFIG_INET
  3159. unregister_inetaddr_notifier(&qlcnic_inetaddr_cb);
  3160. unregister_netdevice_notifier(&qlcnic_netdev_cb);
  3161. #endif
  3162. }
  3163. module_exit(qlcnic_exit_module);