qlcnic_main.c 93 KB

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