qlcnic_main.c 89 KB

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