qlcnic_main.c 89 KB

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