qedf_main.c 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * QLogic FCoE Offload Driver
  4. * Copyright (c) 2016-2018 Cavium Inc.
  5. */
  6. #include <linux/init.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/pci.h>
  10. #include <linux/device.h>
  11. #include <linux/highmem.h>
  12. #include <linux/crc32.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/list.h>
  15. #include <linux/kthread.h>
  16. #include <linux/phylink.h>
  17. #include <scsi/libfc.h>
  18. #include <scsi/scsi_host.h>
  19. #include <scsi/fc_frame.h>
  20. #include <linux/if_ether.h>
  21. #include <linux/if_vlan.h>
  22. #include <linux/cpu.h>
  23. #include "qedf.h"
  24. #include "qedf_dbg.h"
  25. #include <uapi/linux/pci_regs.h>
  26. const struct qed_fcoe_ops *qed_ops;
  27. static int qedf_probe(struct pci_dev *pdev, const struct pci_device_id *id);
  28. static void qedf_remove(struct pci_dev *pdev);
  29. static void qedf_shutdown(struct pci_dev *pdev);
  30. static void qedf_schedule_recovery_handler(void *dev);
  31. static void qedf_recovery_handler(struct work_struct *work);
  32. static int qedf_suspend(struct pci_dev *pdev, pm_message_t state);
  33. /*
  34. * Driver module parameters.
  35. */
  36. static unsigned int qedf_dev_loss_tmo = 60;
  37. module_param_named(dev_loss_tmo, qedf_dev_loss_tmo, int, S_IRUGO);
  38. MODULE_PARM_DESC(dev_loss_tmo, " dev_loss_tmo setting for attached "
  39. "remote ports (default 60)");
  40. uint qedf_debug = QEDF_LOG_INFO;
  41. module_param_named(debug, qedf_debug, uint, S_IRUGO|S_IWUSR);
  42. MODULE_PARM_DESC(debug, " Debug mask. Pass '1' to enable default debugging"
  43. " mask");
  44. static uint qedf_fipvlan_retries = 60;
  45. module_param_named(fipvlan_retries, qedf_fipvlan_retries, int, S_IRUGO);
  46. MODULE_PARM_DESC(fipvlan_retries, " Number of FIP VLAN requests to attempt "
  47. "before giving up (default 60)");
  48. static uint qedf_fallback_vlan = QEDF_FALLBACK_VLAN;
  49. module_param_named(fallback_vlan, qedf_fallback_vlan, int, S_IRUGO);
  50. MODULE_PARM_DESC(fallback_vlan, " VLAN ID to try if fip vlan request fails "
  51. "(default 1002).");
  52. static int qedf_default_prio = -1;
  53. module_param_named(default_prio, qedf_default_prio, int, S_IRUGO);
  54. MODULE_PARM_DESC(default_prio, " Override 802.1q priority for FIP and FCoE"
  55. " traffic (value between 0 and 7, default 3).");
  56. uint qedf_dump_frames;
  57. module_param_named(dump_frames, qedf_dump_frames, int, S_IRUGO | S_IWUSR);
  58. MODULE_PARM_DESC(dump_frames, " Print the skb data of FIP and FCoE frames "
  59. "(default off)");
  60. static uint qedf_queue_depth;
  61. module_param_named(queue_depth, qedf_queue_depth, int, S_IRUGO);
  62. MODULE_PARM_DESC(queue_depth, " Sets the queue depth for all LUNs discovered "
  63. "by the qedf driver. Default is 0 (use OS default).");
  64. uint qedf_io_tracing;
  65. module_param_named(io_tracing, qedf_io_tracing, int, S_IRUGO | S_IWUSR);
  66. MODULE_PARM_DESC(io_tracing, " Enable logging of SCSI requests/completions "
  67. "into trace buffer. (default off).");
  68. static uint qedf_max_lun = MAX_FIBRE_LUNS;
  69. module_param_named(max_lun, qedf_max_lun, int, S_IRUGO);
  70. MODULE_PARM_DESC(max_lun, " Sets the maximum luns per target that the driver "
  71. "supports. (default 0xffffffff)");
  72. uint qedf_link_down_tmo;
  73. module_param_named(link_down_tmo, qedf_link_down_tmo, int, S_IRUGO);
  74. MODULE_PARM_DESC(link_down_tmo, " Delays informing the fcoe transport that the "
  75. "link is down by N seconds.");
  76. bool qedf_retry_delay;
  77. module_param_named(retry_delay, qedf_retry_delay, bool, S_IRUGO | S_IWUSR);
  78. MODULE_PARM_DESC(retry_delay, " Enable/disable handling of FCP_RSP IU retry "
  79. "delay handling (default off).");
  80. static bool qedf_dcbx_no_wait;
  81. module_param_named(dcbx_no_wait, qedf_dcbx_no_wait, bool, S_IRUGO | S_IWUSR);
  82. MODULE_PARM_DESC(dcbx_no_wait, " Do not wait for DCBX convergence to start "
  83. "sending FIP VLAN requests on link up (Default: off).");
  84. static uint qedf_dp_module;
  85. module_param_named(dp_module, qedf_dp_module, uint, S_IRUGO);
  86. MODULE_PARM_DESC(dp_module, " bit flags control for verbose printk passed "
  87. "qed module during probe.");
  88. static uint qedf_dp_level = QED_LEVEL_NOTICE;
  89. module_param_named(dp_level, qedf_dp_level, uint, S_IRUGO);
  90. MODULE_PARM_DESC(dp_level, " printk verbosity control passed to qed module "
  91. "during probe (0-3: 0 more verbose).");
  92. static bool qedf_enable_recovery = true;
  93. module_param_named(enable_recovery, qedf_enable_recovery,
  94. bool, S_IRUGO | S_IWUSR);
  95. MODULE_PARM_DESC(enable_recovery, "Enable/disable recovery on driver/firmware "
  96. "interface level errors 0 = Disabled, 1 = Enabled (Default: 1).");
  97. struct workqueue_struct *qedf_io_wq;
  98. static struct fcoe_percpu_s qedf_global;
  99. static DEFINE_SPINLOCK(qedf_global_lock);
  100. static struct kmem_cache *qedf_io_work_cache;
  101. void qedf_set_vlan_id(struct qedf_ctx *qedf, int vlan_id)
  102. {
  103. int vlan_id_tmp = 0;
  104. vlan_id_tmp = vlan_id | (qedf->prio << VLAN_PRIO_SHIFT);
  105. qedf->vlan_id = vlan_id_tmp;
  106. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
  107. "Setting vlan_id=0x%04x prio=%d.\n",
  108. vlan_id_tmp, qedf->prio);
  109. }
  110. /* Returns true if we have a valid vlan, false otherwise */
  111. static bool qedf_initiate_fipvlan_req(struct qedf_ctx *qedf)
  112. {
  113. while (qedf->fipvlan_retries--) {
  114. /* This is to catch if link goes down during fipvlan retries */
  115. if (atomic_read(&qedf->link_state) == QEDF_LINK_DOWN) {
  116. QEDF_ERR(&qedf->dbg_ctx, "Link not up.\n");
  117. return false;
  118. }
  119. if (test_bit(QEDF_UNLOADING, &qedf->flags)) {
  120. QEDF_ERR(&qedf->dbg_ctx, "Driver unloading.\n");
  121. return false;
  122. }
  123. if (qedf->vlan_id > 0) {
  124. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
  125. "vlan = 0x%x already set, calling ctlr_link_up.\n",
  126. qedf->vlan_id);
  127. if (atomic_read(&qedf->link_state) == QEDF_LINK_UP)
  128. fcoe_ctlr_link_up(&qedf->ctlr);
  129. return true;
  130. }
  131. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  132. "Retry %d.\n", qedf->fipvlan_retries);
  133. init_completion(&qedf->fipvlan_compl);
  134. qedf_fcoe_send_vlan_req(qedf);
  135. wait_for_completion_timeout(&qedf->fipvlan_compl, 1 * HZ);
  136. }
  137. return false;
  138. }
  139. static void qedf_handle_link_update(struct work_struct *work)
  140. {
  141. struct qedf_ctx *qedf =
  142. container_of(work, struct qedf_ctx, link_update.work);
  143. int rc;
  144. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC, "Entered. link_state=%d.\n",
  145. atomic_read(&qedf->link_state));
  146. if (atomic_read(&qedf->link_state) == QEDF_LINK_UP) {
  147. rc = qedf_initiate_fipvlan_req(qedf);
  148. if (rc)
  149. return;
  150. if (atomic_read(&qedf->link_state) != QEDF_LINK_UP) {
  151. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
  152. "Link is down, resetting vlan_id.\n");
  153. qedf->vlan_id = 0;
  154. return;
  155. }
  156. /*
  157. * If we get here then we never received a repsonse to our
  158. * fip vlan request so set the vlan_id to the default and
  159. * tell FCoE that the link is up
  160. */
  161. QEDF_WARN(&(qedf->dbg_ctx), "Did not receive FIP VLAN "
  162. "response, falling back to default VLAN %d.\n",
  163. qedf_fallback_vlan);
  164. qedf_set_vlan_id(qedf, qedf_fallback_vlan);
  165. /*
  166. * Zero out data_src_addr so we'll update it with the new
  167. * lport port_id
  168. */
  169. eth_zero_addr(qedf->data_src_addr);
  170. fcoe_ctlr_link_up(&qedf->ctlr);
  171. } else if (atomic_read(&qedf->link_state) == QEDF_LINK_DOWN) {
  172. /*
  173. * If we hit here and link_down_tmo_valid is still 1 it means
  174. * that link_down_tmo timed out so set it to 0 to make sure any
  175. * other readers have accurate state.
  176. */
  177. atomic_set(&qedf->link_down_tmo_valid, 0);
  178. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  179. "Calling fcoe_ctlr_link_down().\n");
  180. fcoe_ctlr_link_down(&qedf->ctlr);
  181. if (qedf_wait_for_upload(qedf) == false)
  182. QEDF_ERR(&qedf->dbg_ctx,
  183. "Could not upload all sessions.\n");
  184. /* Reset the number of FIP VLAN retries */
  185. qedf->fipvlan_retries = qedf_fipvlan_retries;
  186. }
  187. }
  188. #define QEDF_FCOE_MAC_METHOD_GRANGED_MAC 1
  189. #define QEDF_FCOE_MAC_METHOD_FCF_MAP 2
  190. #define QEDF_FCOE_MAC_METHOD_FCOE_SET_MAC 3
  191. static void qedf_set_data_src_addr(struct qedf_ctx *qedf, struct fc_frame *fp)
  192. {
  193. u8 *granted_mac;
  194. struct fc_frame_header *fh = fc_frame_header_get(fp);
  195. u8 fc_map[3];
  196. int method = 0;
  197. /* Get granted MAC address from FIP FLOGI payload */
  198. granted_mac = fr_cb(fp)->granted_mac;
  199. /*
  200. * We set the source MAC for FCoE traffic based on the Granted MAC
  201. * address from the switch.
  202. *
  203. * If granted_mac is non-zero, we used that.
  204. * If the granted_mac is zeroed out, created the FCoE MAC based on
  205. * the sel_fcf->fc_map and the d_id fo the FLOGI frame.
  206. * If sel_fcf->fc_map is 0 then we use the default FCF-MAC plus the
  207. * d_id of the FLOGI frame.
  208. */
  209. if (!is_zero_ether_addr(granted_mac)) {
  210. ether_addr_copy(qedf->data_src_addr, granted_mac);
  211. method = QEDF_FCOE_MAC_METHOD_GRANGED_MAC;
  212. } else if (qedf->ctlr.sel_fcf->fc_map != 0) {
  213. hton24(fc_map, qedf->ctlr.sel_fcf->fc_map);
  214. qedf->data_src_addr[0] = fc_map[0];
  215. qedf->data_src_addr[1] = fc_map[1];
  216. qedf->data_src_addr[2] = fc_map[2];
  217. qedf->data_src_addr[3] = fh->fh_d_id[0];
  218. qedf->data_src_addr[4] = fh->fh_d_id[1];
  219. qedf->data_src_addr[5] = fh->fh_d_id[2];
  220. method = QEDF_FCOE_MAC_METHOD_FCF_MAP;
  221. } else {
  222. fc_fcoe_set_mac(qedf->data_src_addr, fh->fh_d_id);
  223. method = QEDF_FCOE_MAC_METHOD_FCOE_SET_MAC;
  224. }
  225. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  226. "QEDF data_src_mac=%pM method=%d.\n", qedf->data_src_addr, method);
  227. }
  228. static void qedf_flogi_resp(struct fc_seq *seq, struct fc_frame *fp,
  229. void *arg)
  230. {
  231. struct fc_exch *exch = fc_seq_exch(seq);
  232. struct fc_lport *lport = exch->lp;
  233. struct qedf_ctx *qedf = lport_priv(lport);
  234. if (!qedf) {
  235. QEDF_ERR(NULL, "qedf is NULL.\n");
  236. return;
  237. }
  238. /*
  239. * If ERR_PTR is set then don't try to stat anything as it will cause
  240. * a crash when we access fp.
  241. */
  242. if (IS_ERR(fp)) {
  243. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
  244. "fp has IS_ERR() set.\n");
  245. goto skip_stat;
  246. }
  247. /* Log stats for FLOGI reject */
  248. if (fc_frame_payload_op(fp) == ELS_LS_RJT)
  249. qedf->flogi_failed++;
  250. else if (fc_frame_payload_op(fp) == ELS_LS_ACC) {
  251. /* Set the source MAC we will use for FCoE traffic */
  252. qedf_set_data_src_addr(qedf, fp);
  253. qedf->flogi_pending = 0;
  254. }
  255. /* Complete flogi_compl so we can proceed to sending ADISCs */
  256. complete(&qedf->flogi_compl);
  257. skip_stat:
  258. /* Report response to libfc */
  259. fc_lport_flogi_resp(seq, fp, lport);
  260. }
  261. static struct fc_seq *qedf_elsct_send(struct fc_lport *lport, u32 did,
  262. struct fc_frame *fp, unsigned int op,
  263. void (*resp)(struct fc_seq *,
  264. struct fc_frame *,
  265. void *),
  266. void *arg, u32 timeout)
  267. {
  268. struct qedf_ctx *qedf = lport_priv(lport);
  269. /*
  270. * Intercept FLOGI for statistic purposes. Note we use the resp
  271. * callback to tell if this is really a flogi.
  272. */
  273. if (resp == fc_lport_flogi_resp) {
  274. qedf->flogi_cnt++;
  275. qedf->flogi_pending++;
  276. if (test_bit(QEDF_UNLOADING, &qedf->flags)) {
  277. QEDF_ERR(&qedf->dbg_ctx, "Driver unloading\n");
  278. qedf->flogi_pending = 0;
  279. }
  280. if (qedf->flogi_pending >= QEDF_FLOGI_RETRY_CNT) {
  281. schedule_delayed_work(&qedf->stag_work, 2);
  282. return NULL;
  283. }
  284. return fc_elsct_send(lport, did, fp, op, qedf_flogi_resp,
  285. arg, timeout);
  286. }
  287. return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
  288. }
  289. int qedf_send_flogi(struct qedf_ctx *qedf)
  290. {
  291. struct fc_lport *lport;
  292. struct fc_frame *fp;
  293. lport = qedf->lport;
  294. if (!lport->tt.elsct_send) {
  295. QEDF_ERR(&qedf->dbg_ctx, "tt.elsct_send not set.\n");
  296. return -EINVAL;
  297. }
  298. fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
  299. if (!fp) {
  300. QEDF_ERR(&(qedf->dbg_ctx), "fc_frame_alloc failed.\n");
  301. return -ENOMEM;
  302. }
  303. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
  304. "Sending FLOGI to reestablish session with switch.\n");
  305. lport->tt.elsct_send(lport, FC_FID_FLOGI, fp,
  306. ELS_FLOGI, qedf_flogi_resp, lport, lport->r_a_tov);
  307. init_completion(&qedf->flogi_compl);
  308. return 0;
  309. }
  310. /*
  311. * This function is called if link_down_tmo is in use. If we get a link up and
  312. * link_down_tmo has not expired then use just FLOGI/ADISC to recover our
  313. * sessions with targets. Otherwise, just call fcoe_ctlr_link_up().
  314. */
  315. static void qedf_link_recovery(struct work_struct *work)
  316. {
  317. struct qedf_ctx *qedf =
  318. container_of(work, struct qedf_ctx, link_recovery.work);
  319. struct fc_lport *lport = qedf->lport;
  320. struct fc_rport_priv *rdata;
  321. bool rc;
  322. int retries = 30;
  323. int rval, i;
  324. struct list_head rdata_login_list;
  325. INIT_LIST_HEAD(&rdata_login_list);
  326. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  327. "Link down tmo did not expire.\n");
  328. /*
  329. * Essentially reset the fcoe_ctlr here without affecting the state
  330. * of the libfc structs.
  331. */
  332. qedf->ctlr.state = FIP_ST_LINK_WAIT;
  333. fcoe_ctlr_link_down(&qedf->ctlr);
  334. /*
  335. * Bring the link up before we send the fipvlan request so libfcoe
  336. * can select a new fcf in parallel
  337. */
  338. fcoe_ctlr_link_up(&qedf->ctlr);
  339. /* Since the link when down and up to verify which vlan we're on */
  340. qedf->fipvlan_retries = qedf_fipvlan_retries;
  341. rc = qedf_initiate_fipvlan_req(qedf);
  342. /* If getting the VLAN fails, set the VLAN to the fallback one */
  343. if (!rc)
  344. qedf_set_vlan_id(qedf, qedf_fallback_vlan);
  345. /*
  346. * We need to wait for an FCF to be selected due to the
  347. * fcoe_ctlr_link_up other the FLOGI will be rejected.
  348. */
  349. while (retries > 0) {
  350. if (qedf->ctlr.sel_fcf) {
  351. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  352. "FCF reselected, proceeding with FLOGI.\n");
  353. break;
  354. }
  355. msleep(500);
  356. retries--;
  357. }
  358. if (retries < 1) {
  359. QEDF_ERR(&(qedf->dbg_ctx), "Exhausted retries waiting for "
  360. "FCF selection.\n");
  361. return;
  362. }
  363. rval = qedf_send_flogi(qedf);
  364. if (rval)
  365. return;
  366. /* Wait for FLOGI completion before proceeding with sending ADISCs */
  367. i = wait_for_completion_timeout(&qedf->flogi_compl,
  368. qedf->lport->r_a_tov);
  369. if (i == 0) {
  370. QEDF_ERR(&(qedf->dbg_ctx), "FLOGI timed out.\n");
  371. return;
  372. }
  373. /*
  374. * Call lport->tt.rport_login which will cause libfc to send an
  375. * ADISC since the rport is in state ready.
  376. */
  377. mutex_lock(&lport->disc.disc_mutex);
  378. list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
  379. if (kref_get_unless_zero(&rdata->kref)) {
  380. fc_rport_login(rdata);
  381. kref_put(&rdata->kref, fc_rport_destroy);
  382. }
  383. }
  384. mutex_unlock(&lport->disc.disc_mutex);
  385. }
  386. static void qedf_update_link_speed(struct qedf_ctx *qedf,
  387. struct qed_link_output *link)
  388. {
  389. __ETHTOOL_DECLARE_LINK_MODE_MASK(sup_caps);
  390. struct fc_lport *lport = qedf->lport;
  391. lport->link_speed = FC_PORTSPEED_UNKNOWN;
  392. lport->link_supported_speeds = FC_PORTSPEED_UNKNOWN;
  393. /* Set fc_host link speed */
  394. switch (link->speed) {
  395. case 10000:
  396. lport->link_speed = FC_PORTSPEED_10GBIT;
  397. break;
  398. case 25000:
  399. lport->link_speed = FC_PORTSPEED_25GBIT;
  400. break;
  401. case 40000:
  402. lport->link_speed = FC_PORTSPEED_40GBIT;
  403. break;
  404. case 50000:
  405. lport->link_speed = FC_PORTSPEED_50GBIT;
  406. break;
  407. case 100000:
  408. lport->link_speed = FC_PORTSPEED_100GBIT;
  409. break;
  410. case 20000:
  411. lport->link_speed = FC_PORTSPEED_20GBIT;
  412. break;
  413. default:
  414. lport->link_speed = FC_PORTSPEED_UNKNOWN;
  415. break;
  416. }
  417. /*
  418. * Set supported link speed by querying the supported
  419. * capabilities of the link.
  420. */
  421. phylink_zero(sup_caps);
  422. phylink_set(sup_caps, 10000baseT_Full);
  423. phylink_set(sup_caps, 10000baseKX4_Full);
  424. phylink_set(sup_caps, 10000baseR_FEC);
  425. phylink_set(sup_caps, 10000baseCR_Full);
  426. phylink_set(sup_caps, 10000baseSR_Full);
  427. phylink_set(sup_caps, 10000baseLR_Full);
  428. phylink_set(sup_caps, 10000baseLRM_Full);
  429. phylink_set(sup_caps, 10000baseKR_Full);
  430. if (linkmode_intersects(link->supported_caps, sup_caps))
  431. lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
  432. phylink_zero(sup_caps);
  433. phylink_set(sup_caps, 25000baseKR_Full);
  434. phylink_set(sup_caps, 25000baseCR_Full);
  435. phylink_set(sup_caps, 25000baseSR_Full);
  436. if (linkmode_intersects(link->supported_caps, sup_caps))
  437. lport->link_supported_speeds |= FC_PORTSPEED_25GBIT;
  438. phylink_zero(sup_caps);
  439. phylink_set(sup_caps, 40000baseLR4_Full);
  440. phylink_set(sup_caps, 40000baseKR4_Full);
  441. phylink_set(sup_caps, 40000baseCR4_Full);
  442. phylink_set(sup_caps, 40000baseSR4_Full);
  443. if (linkmode_intersects(link->supported_caps, sup_caps))
  444. lport->link_supported_speeds |= FC_PORTSPEED_40GBIT;
  445. phylink_zero(sup_caps);
  446. phylink_set(sup_caps, 50000baseKR2_Full);
  447. phylink_set(sup_caps, 50000baseCR2_Full);
  448. phylink_set(sup_caps, 50000baseSR2_Full);
  449. if (linkmode_intersects(link->supported_caps, sup_caps))
  450. lport->link_supported_speeds |= FC_PORTSPEED_50GBIT;
  451. phylink_zero(sup_caps);
  452. phylink_set(sup_caps, 100000baseKR4_Full);
  453. phylink_set(sup_caps, 100000baseSR4_Full);
  454. phylink_set(sup_caps, 100000baseCR4_Full);
  455. phylink_set(sup_caps, 100000baseLR4_ER4_Full);
  456. if (linkmode_intersects(link->supported_caps, sup_caps))
  457. lport->link_supported_speeds |= FC_PORTSPEED_100GBIT;
  458. phylink_zero(sup_caps);
  459. phylink_set(sup_caps, 20000baseKR2_Full);
  460. if (linkmode_intersects(link->supported_caps, sup_caps))
  461. lport->link_supported_speeds |= FC_PORTSPEED_20GBIT;
  462. if (lport->host && lport->host->shost_data)
  463. fc_host_supported_speeds(lport->host) =
  464. lport->link_supported_speeds;
  465. }
  466. static void qedf_bw_update(void *dev)
  467. {
  468. struct qedf_ctx *qedf = (struct qedf_ctx *)dev;
  469. struct qed_link_output link;
  470. /* Get the latest status of the link */
  471. qed_ops->common->get_link(qedf->cdev, &link);
  472. if (test_bit(QEDF_UNLOADING, &qedf->flags)) {
  473. QEDF_ERR(&qedf->dbg_ctx,
  474. "Ignore link update, driver getting unload.\n");
  475. return;
  476. }
  477. if (link.link_up) {
  478. if (atomic_read(&qedf->link_state) == QEDF_LINK_UP)
  479. qedf_update_link_speed(qedf, &link);
  480. else
  481. QEDF_ERR(&qedf->dbg_ctx,
  482. "Ignore bw update, link is down.\n");
  483. } else {
  484. QEDF_ERR(&qedf->dbg_ctx, "link_up is not set.\n");
  485. }
  486. }
  487. static void qedf_link_update(void *dev, struct qed_link_output *link)
  488. {
  489. struct qedf_ctx *qedf = (struct qedf_ctx *)dev;
  490. /*
  491. * Prevent race where we're removing the module and we get link update
  492. * for qed.
  493. */
  494. if (test_bit(QEDF_UNLOADING, &qedf->flags)) {
  495. QEDF_ERR(&qedf->dbg_ctx,
  496. "Ignore link update, driver getting unload.\n");
  497. return;
  498. }
  499. if (link->link_up) {
  500. if (atomic_read(&qedf->link_state) == QEDF_LINK_UP) {
  501. QEDF_INFO((&qedf->dbg_ctx), QEDF_LOG_DISC,
  502. "Ignoring link up event as link is already up.\n");
  503. return;
  504. }
  505. QEDF_ERR(&(qedf->dbg_ctx), "LINK UP (%d GB/s).\n",
  506. link->speed / 1000);
  507. /* Cancel any pending link down work */
  508. cancel_delayed_work(&qedf->link_update);
  509. atomic_set(&qedf->link_state, QEDF_LINK_UP);
  510. qedf_update_link_speed(qedf, link);
  511. if (atomic_read(&qedf->dcbx) == QEDF_DCBX_DONE ||
  512. qedf_dcbx_no_wait) {
  513. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  514. "DCBx done.\n");
  515. if (atomic_read(&qedf->link_down_tmo_valid) > 0)
  516. queue_delayed_work(qedf->link_update_wq,
  517. &qedf->link_recovery, 0);
  518. else
  519. queue_delayed_work(qedf->link_update_wq,
  520. &qedf->link_update, 0);
  521. atomic_set(&qedf->link_down_tmo_valid, 0);
  522. }
  523. } else {
  524. QEDF_ERR(&(qedf->dbg_ctx), "LINK DOWN.\n");
  525. atomic_set(&qedf->link_state, QEDF_LINK_DOWN);
  526. atomic_set(&qedf->dcbx, QEDF_DCBX_PENDING);
  527. /*
  528. * Flag that we're waiting for the link to come back up before
  529. * informing the fcoe layer of the event.
  530. */
  531. if (qedf_link_down_tmo > 0) {
  532. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  533. "Starting link down tmo.\n");
  534. atomic_set(&qedf->link_down_tmo_valid, 1);
  535. }
  536. qedf->vlan_id = 0;
  537. qedf_update_link_speed(qedf, link);
  538. queue_delayed_work(qedf->link_update_wq, &qedf->link_update,
  539. qedf_link_down_tmo * HZ);
  540. }
  541. }
  542. static void qedf_dcbx_handler(void *dev, struct qed_dcbx_get *get, u32 mib_type)
  543. {
  544. struct qedf_ctx *qedf = (struct qedf_ctx *)dev;
  545. u8 tmp_prio;
  546. QEDF_ERR(&(qedf->dbg_ctx), "DCBx event valid=%d enabled=%d fcoe "
  547. "prio=%d.\n", get->operational.valid, get->operational.enabled,
  548. get->operational.app_prio.fcoe);
  549. if (get->operational.enabled && get->operational.valid) {
  550. /* If DCBX was already negotiated on link up then just exit */
  551. if (atomic_read(&qedf->dcbx) == QEDF_DCBX_DONE) {
  552. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  553. "DCBX already set on link up.\n");
  554. return;
  555. }
  556. atomic_set(&qedf->dcbx, QEDF_DCBX_DONE);
  557. /*
  558. * Set the 8021q priority in the following manner:
  559. *
  560. * 1. If a modparam is set use that
  561. * 2. If the value is not between 0..7 use the default
  562. * 3. Use the priority we get from the DCBX app tag
  563. */
  564. tmp_prio = get->operational.app_prio.fcoe;
  565. if (qedf_default_prio > -1)
  566. qedf->prio = qedf_default_prio;
  567. else if (tmp_prio > 7) {
  568. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  569. "FIP/FCoE prio %d out of range, setting to %d.\n",
  570. tmp_prio, QEDF_DEFAULT_PRIO);
  571. qedf->prio = QEDF_DEFAULT_PRIO;
  572. } else
  573. qedf->prio = tmp_prio;
  574. if (atomic_read(&qedf->link_state) == QEDF_LINK_UP &&
  575. !qedf_dcbx_no_wait) {
  576. if (atomic_read(&qedf->link_down_tmo_valid) > 0)
  577. queue_delayed_work(qedf->link_update_wq,
  578. &qedf->link_recovery, 0);
  579. else
  580. queue_delayed_work(qedf->link_update_wq,
  581. &qedf->link_update, 0);
  582. atomic_set(&qedf->link_down_tmo_valid, 0);
  583. }
  584. }
  585. }
  586. static u32 qedf_get_login_failures(void *cookie)
  587. {
  588. struct qedf_ctx *qedf;
  589. qedf = (struct qedf_ctx *)cookie;
  590. return qedf->flogi_failed;
  591. }
  592. static struct qed_fcoe_cb_ops qedf_cb_ops = {
  593. .common = {
  594. .link_update = qedf_link_update,
  595. .bw_update = qedf_bw_update,
  596. .schedule_recovery_handler = qedf_schedule_recovery_handler,
  597. .dcbx_aen = qedf_dcbx_handler,
  598. .get_generic_tlv_data = qedf_get_generic_tlv_data,
  599. .get_protocol_tlv_data = qedf_get_protocol_tlv_data,
  600. .schedule_hw_err_handler = qedf_schedule_hw_err_handler,
  601. }
  602. };
  603. /*
  604. * Various transport templates.
  605. */
  606. static struct scsi_transport_template *qedf_fc_transport_template;
  607. static struct scsi_transport_template *qedf_fc_vport_transport_template;
  608. /*
  609. * SCSI EH handlers
  610. */
  611. static int qedf_eh_abort(struct scsi_cmnd *sc_cmd)
  612. {
  613. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  614. struct fc_lport *lport;
  615. struct qedf_ctx *qedf;
  616. struct qedf_ioreq *io_req;
  617. struct fc_rport_libfc_priv *rp = rport->dd_data;
  618. struct fc_rport_priv *rdata;
  619. struct qedf_rport *fcport = NULL;
  620. int rc = FAILED;
  621. int wait_count = 100;
  622. int refcount = 0;
  623. int rval;
  624. int got_ref = 0;
  625. lport = shost_priv(sc_cmd->device->host);
  626. qedf = (struct qedf_ctx *)lport_priv(lport);
  627. /* rport and tgt are allocated together, so tgt should be non-NULL */
  628. fcport = (struct qedf_rport *)&rp[1];
  629. rdata = fcport->rdata;
  630. if (!rdata || !kref_get_unless_zero(&rdata->kref)) {
  631. QEDF_ERR(&qedf->dbg_ctx, "stale rport, sc_cmd=%p\n", sc_cmd);
  632. rc = SUCCESS;
  633. goto out;
  634. }
  635. io_req = qedf_priv(sc_cmd)->io_req;
  636. if (!io_req) {
  637. QEDF_ERR(&qedf->dbg_ctx,
  638. "sc_cmd not queued with lld, sc_cmd=%p op=0x%02x, port_id=%06x\n",
  639. sc_cmd, sc_cmd->cmnd[0],
  640. rdata->ids.port_id);
  641. rc = SUCCESS;
  642. goto drop_rdata_kref;
  643. }
  644. rval = kref_get_unless_zero(&io_req->refcount); /* ID: 005 */
  645. if (rval)
  646. got_ref = 1;
  647. /* If we got a valid io_req, confirm it belongs to this sc_cmd. */
  648. if (!rval || io_req->sc_cmd != sc_cmd) {
  649. QEDF_ERR(&qedf->dbg_ctx,
  650. "Freed/Incorrect io_req, io_req->sc_cmd=%p, sc_cmd=%p, port_id=%06x, bailing out.\n",
  651. io_req->sc_cmd, sc_cmd, rdata->ids.port_id);
  652. goto drop_rdata_kref;
  653. }
  654. if (fc_remote_port_chkready(rport)) {
  655. refcount = kref_read(&io_req->refcount);
  656. QEDF_ERR(&qedf->dbg_ctx,
  657. "rport not ready, io_req=%p, xid=0x%x sc_cmd=%p op=0x%02x, refcount=%d, port_id=%06x\n",
  658. io_req, io_req->xid, sc_cmd, sc_cmd->cmnd[0],
  659. refcount, rdata->ids.port_id);
  660. goto drop_rdata_kref;
  661. }
  662. rc = fc_block_rport(rport);
  663. if (rc)
  664. goto drop_rdata_kref;
  665. if (test_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags)) {
  666. QEDF_ERR(&qedf->dbg_ctx,
  667. "Connection uploading, xid=0x%x., port_id=%06x\n",
  668. io_req->xid, rdata->ids.port_id);
  669. while (io_req->sc_cmd && (wait_count != 0)) {
  670. msleep(100);
  671. wait_count--;
  672. }
  673. if (wait_count) {
  674. QEDF_ERR(&qedf->dbg_ctx, "ABTS succeeded\n");
  675. rc = SUCCESS;
  676. } else {
  677. QEDF_ERR(&qedf->dbg_ctx, "ABTS failed\n");
  678. rc = FAILED;
  679. }
  680. goto drop_rdata_kref;
  681. }
  682. if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
  683. QEDF_ERR(&qedf->dbg_ctx, "link not ready.\n");
  684. goto drop_rdata_kref;
  685. }
  686. QEDF_ERR(&qedf->dbg_ctx,
  687. "Aborting io_req=%p sc_cmd=%p xid=0x%x fp_idx=%d, port_id=%06x.\n",
  688. io_req, sc_cmd, io_req->xid, io_req->fp_idx,
  689. rdata->ids.port_id);
  690. if (qedf->stop_io_on_error) {
  691. qedf_stop_all_io(qedf);
  692. rc = SUCCESS;
  693. goto drop_rdata_kref;
  694. }
  695. init_completion(&io_req->abts_done);
  696. rval = qedf_initiate_abts(io_req, true);
  697. if (rval) {
  698. QEDF_ERR(&(qedf->dbg_ctx), "Failed to queue ABTS.\n");
  699. /*
  700. * If we fail to queue the ABTS then return this command to
  701. * the SCSI layer as it will own and free the xid
  702. */
  703. rc = SUCCESS;
  704. qedf_scsi_done(qedf, io_req, DID_ERROR);
  705. goto drop_rdata_kref;
  706. }
  707. wait_for_completion(&io_req->abts_done);
  708. if (io_req->event == QEDF_IOREQ_EV_ABORT_SUCCESS ||
  709. io_req->event == QEDF_IOREQ_EV_ABORT_FAILED ||
  710. io_req->event == QEDF_IOREQ_EV_CLEANUP_SUCCESS) {
  711. /*
  712. * If we get a reponse to the abort this is success from
  713. * the perspective that all references to the command have
  714. * been removed from the driver and firmware
  715. */
  716. rc = SUCCESS;
  717. } else {
  718. /* If the abort and cleanup failed then return a failure */
  719. rc = FAILED;
  720. }
  721. if (rc == SUCCESS)
  722. QEDF_ERR(&(qedf->dbg_ctx), "ABTS succeeded, xid=0x%x.\n",
  723. io_req->xid);
  724. else
  725. QEDF_ERR(&(qedf->dbg_ctx), "ABTS failed, xid=0x%x.\n",
  726. io_req->xid);
  727. drop_rdata_kref:
  728. kref_put(&rdata->kref, fc_rport_destroy);
  729. out:
  730. if (got_ref)
  731. kref_put(&io_req->refcount, qedf_release_cmd);
  732. return rc;
  733. }
  734. static int qedf_eh_target_reset(struct scsi_cmnd *sc_cmd)
  735. {
  736. struct scsi_target *starget = scsi_target(sc_cmd->device);
  737. struct fc_rport *rport = starget_to_rport(starget);
  738. QEDF_ERR(NULL, "TARGET RESET Issued...");
  739. return qedf_initiate_tmf(rport, 0, FCP_TMF_TGT_RESET);
  740. }
  741. static int qedf_eh_device_reset(struct scsi_cmnd *sc_cmd)
  742. {
  743. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  744. QEDF_ERR(NULL, "LUN RESET Issued...\n");
  745. return qedf_initiate_tmf(rport, sc_cmd->device->lun, FCP_TMF_LUN_RESET);
  746. }
  747. bool qedf_wait_for_upload(struct qedf_ctx *qedf)
  748. {
  749. struct qedf_rport *fcport;
  750. int wait_cnt = 120;
  751. while (wait_cnt--) {
  752. if (atomic_read(&qedf->num_offloads))
  753. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
  754. "Waiting for all uploads to complete num_offloads = 0x%x.\n",
  755. atomic_read(&qedf->num_offloads));
  756. else
  757. return true;
  758. msleep(500);
  759. }
  760. rcu_read_lock();
  761. list_for_each_entry_rcu(fcport, &qedf->fcports, peers) {
  762. if (test_bit(QEDF_RPORT_SESSION_READY,
  763. &fcport->flags)) {
  764. if (fcport->rdata)
  765. QEDF_ERR(&qedf->dbg_ctx,
  766. "Waiting for fcport %p portid=%06x.\n",
  767. fcport, fcport->rdata->ids.port_id);
  768. } else {
  769. QEDF_ERR(&qedf->dbg_ctx,
  770. "Waiting for fcport %p.\n", fcport);
  771. }
  772. }
  773. rcu_read_unlock();
  774. return false;
  775. }
  776. /* Performs soft reset of qedf_ctx by simulating a link down/up */
  777. void qedf_ctx_soft_reset(struct fc_lport *lport)
  778. {
  779. struct qedf_ctx *qedf;
  780. struct qed_link_output if_link;
  781. qedf = lport_priv(lport);
  782. if (lport->vport) {
  783. clear_bit(QEDF_STAG_IN_PROGRESS, &qedf->flags);
  784. printk_ratelimited("Cannot issue host reset on NPIV port.\n");
  785. return;
  786. }
  787. qedf->flogi_pending = 0;
  788. /* For host reset, essentially do a soft link up/down */
  789. atomic_set(&qedf->link_state, QEDF_LINK_DOWN);
  790. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
  791. "Queuing link down work.\n");
  792. queue_delayed_work(qedf->link_update_wq, &qedf->link_update,
  793. 0);
  794. if (qedf_wait_for_upload(qedf) == false) {
  795. QEDF_ERR(&qedf->dbg_ctx, "Could not upload all sessions.\n");
  796. WARN_ON(atomic_read(&qedf->num_offloads));
  797. }
  798. /* Before setting link up query physical link state */
  799. qed_ops->common->get_link(qedf->cdev, &if_link);
  800. /* Bail if the physical link is not up */
  801. if (!if_link.link_up) {
  802. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
  803. "Physical link is not up.\n");
  804. clear_bit(QEDF_STAG_IN_PROGRESS, &qedf->flags);
  805. return;
  806. }
  807. /* Flush and wait to make sure link down is processed */
  808. flush_delayed_work(&qedf->link_update);
  809. msleep(500);
  810. atomic_set(&qedf->link_state, QEDF_LINK_UP);
  811. qedf->vlan_id = 0;
  812. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
  813. "Queue link up work.\n");
  814. queue_delayed_work(qedf->link_update_wq, &qedf->link_update,
  815. 0);
  816. clear_bit(QEDF_STAG_IN_PROGRESS, &qedf->flags);
  817. }
  818. /* Reset the host by gracefully logging out and then logging back in */
  819. static int qedf_eh_host_reset(struct scsi_cmnd *sc_cmd)
  820. {
  821. struct fc_lport *lport;
  822. struct qedf_ctx *qedf;
  823. lport = shost_priv(sc_cmd->device->host);
  824. qedf = lport_priv(lport);
  825. if (atomic_read(&qedf->link_state) == QEDF_LINK_DOWN ||
  826. test_bit(QEDF_UNLOADING, &qedf->flags))
  827. return FAILED;
  828. QEDF_ERR(&(qedf->dbg_ctx), "HOST RESET Issued...");
  829. qedf_ctx_soft_reset(lport);
  830. return SUCCESS;
  831. }
  832. static int qedf_sdev_configure(struct scsi_device *sdev,
  833. struct queue_limits *lim)
  834. {
  835. if (qedf_queue_depth) {
  836. scsi_change_queue_depth(sdev, qedf_queue_depth);
  837. }
  838. return 0;
  839. }
  840. static const struct scsi_host_template qedf_host_template = {
  841. .module = THIS_MODULE,
  842. .name = QEDF_MODULE_NAME,
  843. .this_id = -1,
  844. .cmd_per_lun = 32,
  845. .max_sectors = 0xffff,
  846. .queuecommand = qedf_queuecommand,
  847. .shost_groups = qedf_host_groups,
  848. .eh_abort_handler = qedf_eh_abort,
  849. .eh_device_reset_handler = qedf_eh_device_reset, /* lun reset */
  850. .eh_target_reset_handler = qedf_eh_target_reset, /* target reset */
  851. .eh_host_reset_handler = qedf_eh_host_reset,
  852. .sdev_configure = qedf_sdev_configure,
  853. .dma_boundary = QED_HW_DMA_BOUNDARY,
  854. .sg_tablesize = QEDF_MAX_BDS_PER_CMD,
  855. .can_queue = FCOE_PARAMS_NUM_TASKS,
  856. .change_queue_depth = scsi_change_queue_depth,
  857. .cmd_size = sizeof(struct qedf_cmd_priv),
  858. };
  859. static int qedf_get_paged_crc_eof(struct sk_buff *skb, int tlen)
  860. {
  861. int rc;
  862. spin_lock(&qedf_global_lock);
  863. rc = fcoe_get_paged_crc_eof(skb, tlen, &qedf_global);
  864. spin_unlock(&qedf_global_lock);
  865. return rc;
  866. }
  867. static struct qedf_rport *qedf_fcport_lookup(struct qedf_ctx *qedf, u32 port_id)
  868. {
  869. struct qedf_rport *fcport;
  870. struct fc_rport_priv *rdata;
  871. rcu_read_lock();
  872. list_for_each_entry_rcu(fcport, &qedf->fcports, peers) {
  873. rdata = fcport->rdata;
  874. if (rdata == NULL)
  875. continue;
  876. if (rdata->ids.port_id == port_id) {
  877. rcu_read_unlock();
  878. return fcport;
  879. }
  880. }
  881. rcu_read_unlock();
  882. /* Return NULL to caller to let them know fcport was not found */
  883. return NULL;
  884. }
  885. /* Transmits an ELS frame over an offloaded session */
  886. static int qedf_xmit_l2_frame(struct qedf_rport *fcport, struct fc_frame *fp)
  887. {
  888. struct fc_frame_header *fh;
  889. int rc = 0;
  890. fh = fc_frame_header_get(fp);
  891. if ((fh->fh_type == FC_TYPE_ELS) &&
  892. (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
  893. switch (fc_frame_payload_op(fp)) {
  894. case ELS_ADISC:
  895. qedf_send_adisc(fcport, fp);
  896. rc = 1;
  897. break;
  898. }
  899. }
  900. return rc;
  901. }
  902. /*
  903. * qedf_xmit - qedf FCoE frame transmit function
  904. */
  905. static int qedf_xmit(struct fc_lport *lport, struct fc_frame *fp)
  906. {
  907. struct fc_lport *base_lport;
  908. struct qedf_ctx *qedf;
  909. struct ethhdr *eh;
  910. struct fcoe_crc_eof *cp;
  911. struct sk_buff *skb;
  912. struct fc_frame_header *fh;
  913. struct fcoe_hdr *hp;
  914. u8 sof, eof;
  915. u32 crc;
  916. unsigned int hlen, tlen, elen;
  917. int wlen;
  918. struct fc_lport *tmp_lport;
  919. struct fc_lport *vn_port = NULL;
  920. struct qedf_rport *fcport;
  921. int rc;
  922. u16 vlan_tci = 0;
  923. qedf = (struct qedf_ctx *)lport_priv(lport);
  924. fh = fc_frame_header_get(fp);
  925. skb = fp_skb(fp);
  926. /* Filter out traffic to other NPIV ports on the same host */
  927. if (lport->vport)
  928. base_lport = shost_priv(vport_to_shost(lport->vport));
  929. else
  930. base_lport = lport;
  931. /* Flag if the destination is the base port */
  932. if (base_lport->port_id == ntoh24(fh->fh_d_id)) {
  933. vn_port = base_lport;
  934. } else {
  935. /* Got through the list of vports attached to the base_lport
  936. * and see if we have a match with the destination address.
  937. */
  938. list_for_each_entry(tmp_lport, &base_lport->vports, list) {
  939. if (tmp_lport->port_id == ntoh24(fh->fh_d_id)) {
  940. vn_port = tmp_lport;
  941. break;
  942. }
  943. }
  944. }
  945. if (vn_port && ntoh24(fh->fh_d_id) != FC_FID_FLOGI) {
  946. struct fc_rport_priv *rdata = NULL;
  947. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
  948. "Dropping FCoE frame to %06x.\n", ntoh24(fh->fh_d_id));
  949. kfree_skb(skb);
  950. rdata = fc_rport_lookup(lport, ntoh24(fh->fh_d_id));
  951. if (rdata) {
  952. rdata->retries = lport->max_rport_retry_count;
  953. kref_put(&rdata->kref, fc_rport_destroy);
  954. }
  955. return -EINVAL;
  956. }
  957. /* End NPIV filtering */
  958. if (!qedf->ctlr.sel_fcf) {
  959. kfree_skb(skb);
  960. return 0;
  961. }
  962. if (!test_bit(QEDF_LL2_STARTED, &qedf->flags)) {
  963. QEDF_WARN(&(qedf->dbg_ctx), "LL2 not started\n");
  964. kfree_skb(skb);
  965. return 0;
  966. }
  967. if (atomic_read(&qedf->link_state) != QEDF_LINK_UP) {
  968. QEDF_WARN(&(qedf->dbg_ctx), "qedf link down\n");
  969. kfree_skb(skb);
  970. return 0;
  971. }
  972. if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
  973. if (fcoe_ctlr_els_send(&qedf->ctlr, lport, skb))
  974. return 0;
  975. }
  976. /* Check to see if this needs to be sent on an offloaded session */
  977. fcport = qedf_fcport_lookup(qedf, ntoh24(fh->fh_d_id));
  978. if (fcport && test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
  979. rc = qedf_xmit_l2_frame(fcport, fp);
  980. /*
  981. * If the frame was successfully sent over the middle path
  982. * then do not try to also send it over the LL2 path
  983. */
  984. if (rc)
  985. return 0;
  986. }
  987. sof = fr_sof(fp);
  988. eof = fr_eof(fp);
  989. elen = sizeof(struct ethhdr);
  990. hlen = sizeof(struct fcoe_hdr);
  991. tlen = sizeof(struct fcoe_crc_eof);
  992. wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
  993. skb->ip_summed = CHECKSUM_NONE;
  994. crc = fcoe_fc_crc(fp);
  995. /* copy port crc and eof to the skb buff */
  996. if (skb_is_nonlinear(skb)) {
  997. skb_frag_t *frag;
  998. if (qedf_get_paged_crc_eof(skb, tlen)) {
  999. kfree_skb(skb);
  1000. return -ENOMEM;
  1001. }
  1002. frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
  1003. cp = kmap_atomic(skb_frag_page(frag)) + skb_frag_off(frag);
  1004. } else {
  1005. cp = skb_put(skb, tlen);
  1006. }
  1007. memset(cp, 0, sizeof(*cp));
  1008. cp->fcoe_eof = eof;
  1009. cp->fcoe_crc32 = cpu_to_le32(~crc);
  1010. if (skb_is_nonlinear(skb)) {
  1011. kunmap_atomic(cp);
  1012. cp = NULL;
  1013. }
  1014. /* adjust skb network/transport offsets to match mac/fcoe/port */
  1015. skb_push(skb, elen + hlen);
  1016. skb_reset_mac_header(skb);
  1017. skb_reset_network_header(skb);
  1018. skb->mac_len = elen;
  1019. skb->protocol = htons(ETH_P_FCOE);
  1020. /*
  1021. * Add VLAN tag to non-offload FCoE frame based on current stored VLAN
  1022. * for FIP/FCoE traffic.
  1023. */
  1024. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), qedf->vlan_id);
  1025. /* fill up mac and fcoe headers */
  1026. eh = eth_hdr(skb);
  1027. eh->h_proto = htons(ETH_P_FCOE);
  1028. if (qedf->ctlr.map_dest)
  1029. fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
  1030. else
  1031. /* insert GW address */
  1032. ether_addr_copy(eh->h_dest, qedf->ctlr.dest_addr);
  1033. /* Set the source MAC address */
  1034. ether_addr_copy(eh->h_source, qedf->data_src_addr);
  1035. hp = (struct fcoe_hdr *)(eh + 1);
  1036. memset(hp, 0, sizeof(*hp));
  1037. if (FC_FCOE_VER)
  1038. FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
  1039. hp->fcoe_sof = sof;
  1040. /*update tx stats */
  1041. this_cpu_inc(lport->stats->TxFrames);
  1042. this_cpu_add(lport->stats->TxWords, wlen);
  1043. /* Get VLAN ID from skb for printing purposes */
  1044. __vlan_hwaccel_get_tag(skb, &vlan_tci);
  1045. /* send down to lld */
  1046. fr_dev(fp) = lport;
  1047. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2, "FCoE frame send: "
  1048. "src=%06x dest=%06x r_ctl=%x type=%x vlan=%04x.\n",
  1049. ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id), fh->fh_r_ctl, fh->fh_type,
  1050. vlan_tci);
  1051. if (qedf_dump_frames)
  1052. print_hex_dump(KERN_WARNING, "fcoe: ", DUMP_PREFIX_OFFSET, 16,
  1053. 1, skb->data, skb->len, false);
  1054. rc = qed_ops->ll2->start_xmit(qedf->cdev, skb, 0);
  1055. if (rc) {
  1056. QEDF_ERR(&qedf->dbg_ctx, "start_xmit failed rc = %d.\n", rc);
  1057. kfree_skb(skb);
  1058. return rc;
  1059. }
  1060. return 0;
  1061. }
  1062. static int qedf_alloc_sq(struct qedf_ctx *qedf, struct qedf_rport *fcport)
  1063. {
  1064. int rval = 0;
  1065. u32 *pbl;
  1066. dma_addr_t page;
  1067. int num_pages;
  1068. /* Calculate appropriate queue and PBL sizes */
  1069. fcport->sq_mem_size = SQ_NUM_ENTRIES * sizeof(struct fcoe_wqe);
  1070. fcport->sq_mem_size = ALIGN(fcport->sq_mem_size, QEDF_PAGE_SIZE);
  1071. fcport->sq_pbl_size = (fcport->sq_mem_size / QEDF_PAGE_SIZE) *
  1072. sizeof(void *);
  1073. fcport->sq_pbl_size = fcport->sq_pbl_size + QEDF_PAGE_SIZE;
  1074. fcport->sq = dma_alloc_coherent(&qedf->pdev->dev, fcport->sq_mem_size,
  1075. &fcport->sq_dma, GFP_KERNEL);
  1076. if (!fcport->sq) {
  1077. QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate send queue.\n");
  1078. rval = 1;
  1079. goto out;
  1080. }
  1081. fcport->sq_pbl = dma_alloc_coherent(&qedf->pdev->dev,
  1082. fcport->sq_pbl_size,
  1083. &fcport->sq_pbl_dma, GFP_KERNEL);
  1084. if (!fcport->sq_pbl) {
  1085. QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate send queue PBL.\n");
  1086. rval = 1;
  1087. goto out_free_sq;
  1088. }
  1089. /* Create PBL */
  1090. num_pages = fcport->sq_mem_size / QEDF_PAGE_SIZE;
  1091. page = fcport->sq_dma;
  1092. pbl = (u32 *)fcport->sq_pbl;
  1093. while (num_pages--) {
  1094. *pbl = U64_LO(page);
  1095. pbl++;
  1096. *pbl = U64_HI(page);
  1097. pbl++;
  1098. page += QEDF_PAGE_SIZE;
  1099. }
  1100. return rval;
  1101. out_free_sq:
  1102. dma_free_coherent(&qedf->pdev->dev, fcport->sq_mem_size, fcport->sq,
  1103. fcport->sq_dma);
  1104. out:
  1105. return rval;
  1106. }
  1107. static void qedf_free_sq(struct qedf_ctx *qedf, struct qedf_rport *fcport)
  1108. {
  1109. if (fcport->sq_pbl)
  1110. dma_free_coherent(&qedf->pdev->dev, fcport->sq_pbl_size,
  1111. fcport->sq_pbl, fcport->sq_pbl_dma);
  1112. if (fcport->sq)
  1113. dma_free_coherent(&qedf->pdev->dev, fcport->sq_mem_size,
  1114. fcport->sq, fcport->sq_dma);
  1115. }
  1116. static int qedf_offload_connection(struct qedf_ctx *qedf,
  1117. struct qedf_rport *fcport)
  1118. {
  1119. struct qed_fcoe_params_offload conn_info;
  1120. u32 port_id;
  1121. int rval;
  1122. uint16_t total_sqe = (fcport->sq_mem_size / sizeof(struct fcoe_wqe));
  1123. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Offloading connection "
  1124. "portid=%06x.\n", fcport->rdata->ids.port_id);
  1125. rval = qed_ops->acquire_conn(qedf->cdev, &fcport->handle,
  1126. &fcport->fw_cid, &fcport->p_doorbell);
  1127. if (rval) {
  1128. QEDF_WARN(&(qedf->dbg_ctx), "Could not acquire connection "
  1129. "for portid=%06x.\n", fcport->rdata->ids.port_id);
  1130. rval = 1; /* For some reason qed returns 0 on failure here */
  1131. goto out;
  1132. }
  1133. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "portid=%06x "
  1134. "fw_cid=%08x handle=%d.\n", fcport->rdata->ids.port_id,
  1135. fcport->fw_cid, fcport->handle);
  1136. memset(&conn_info, 0, sizeof(struct qed_fcoe_params_offload));
  1137. /* Fill in the offload connection info */
  1138. conn_info.sq_pbl_addr = fcport->sq_pbl_dma;
  1139. conn_info.sq_curr_page_addr = (dma_addr_t)(*(u64 *)fcport->sq_pbl);
  1140. conn_info.sq_next_page_addr =
  1141. (dma_addr_t)(*(u64 *)(fcport->sq_pbl + 8));
  1142. /* Need to use our FCoE MAC for the offload session */
  1143. ether_addr_copy(conn_info.src_mac, qedf->data_src_addr);
  1144. ether_addr_copy(conn_info.dst_mac, qedf->ctlr.dest_addr);
  1145. conn_info.tx_max_fc_pay_len = fcport->rdata->maxframe_size;
  1146. conn_info.e_d_tov_timer_val = qedf->lport->e_d_tov;
  1147. conn_info.rec_tov_timer_val = 3; /* I think this is what E3 was */
  1148. conn_info.rx_max_fc_pay_len = fcport->rdata->maxframe_size;
  1149. /* Set VLAN data */
  1150. conn_info.vlan_tag = qedf->vlan_id <<
  1151. FCOE_CONN_OFFLOAD_RAMROD_DATA_VLAN_ID_SHIFT;
  1152. conn_info.vlan_tag |=
  1153. qedf->prio << FCOE_CONN_OFFLOAD_RAMROD_DATA_PRIORITY_SHIFT;
  1154. conn_info.flags |= (FCOE_CONN_OFFLOAD_RAMROD_DATA_B_VLAN_FLAG_MASK <<
  1155. FCOE_CONN_OFFLOAD_RAMROD_DATA_B_VLAN_FLAG_SHIFT);
  1156. /* Set host port source id */
  1157. port_id = fc_host_port_id(qedf->lport->host);
  1158. fcport->sid = port_id;
  1159. conn_info.s_id.addr_hi = (port_id & 0x000000FF);
  1160. conn_info.s_id.addr_mid = (port_id & 0x0000FF00) >> 8;
  1161. conn_info.s_id.addr_lo = (port_id & 0x00FF0000) >> 16;
  1162. conn_info.max_conc_seqs_c3 = fcport->rdata->max_seq;
  1163. /* Set remote port destination id */
  1164. port_id = fcport->rdata->rport->port_id;
  1165. conn_info.d_id.addr_hi = (port_id & 0x000000FF);
  1166. conn_info.d_id.addr_mid = (port_id & 0x0000FF00) >> 8;
  1167. conn_info.d_id.addr_lo = (port_id & 0x00FF0000) >> 16;
  1168. conn_info.def_q_idx = 0; /* Default index for send queue? */
  1169. /* Set FC-TAPE specific flags if needed */
  1170. if (fcport->dev_type == QEDF_RPORT_TYPE_TAPE) {
  1171. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN,
  1172. "Enable CONF, REC for portid=%06x.\n",
  1173. fcport->rdata->ids.port_id);
  1174. conn_info.flags |= 1 <<
  1175. FCOE_CONN_OFFLOAD_RAMROD_DATA_B_CONF_REQ_SHIFT;
  1176. conn_info.flags |=
  1177. ((fcport->rdata->sp_features & FC_SP_FT_SEQC) ? 1 : 0) <<
  1178. FCOE_CONN_OFFLOAD_RAMROD_DATA_B_REC_VALID_SHIFT;
  1179. }
  1180. rval = qed_ops->offload_conn(qedf->cdev, fcport->handle, &conn_info);
  1181. if (rval) {
  1182. QEDF_WARN(&(qedf->dbg_ctx), "Could not offload connection "
  1183. "for portid=%06x.\n", fcport->rdata->ids.port_id);
  1184. goto out_free_conn;
  1185. } else
  1186. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Offload "
  1187. "succeeded portid=%06x total_sqe=%d.\n",
  1188. fcport->rdata->ids.port_id, total_sqe);
  1189. spin_lock_init(&fcport->rport_lock);
  1190. atomic_set(&fcport->free_sqes, total_sqe);
  1191. return 0;
  1192. out_free_conn:
  1193. qed_ops->release_conn(qedf->cdev, fcport->handle);
  1194. out:
  1195. return rval;
  1196. }
  1197. #define QEDF_TERM_BUFF_SIZE 10
  1198. static void qedf_upload_connection(struct qedf_ctx *qedf,
  1199. struct qedf_rport *fcport)
  1200. {
  1201. void *term_params;
  1202. dma_addr_t term_params_dma;
  1203. /* Term params needs to be a DMA coherent buffer as qed shared the
  1204. * physical DMA address with the firmware. The buffer may be used in
  1205. * the receive path so we may eventually have to move this.
  1206. */
  1207. term_params = dma_alloc_coherent(&qedf->pdev->dev, QEDF_TERM_BUFF_SIZE,
  1208. &term_params_dma, GFP_KERNEL);
  1209. if (!term_params)
  1210. return;
  1211. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Uploading connection "
  1212. "port_id=%06x.\n", fcport->rdata->ids.port_id);
  1213. qed_ops->destroy_conn(qedf->cdev, fcport->handle, term_params_dma);
  1214. qed_ops->release_conn(qedf->cdev, fcport->handle);
  1215. dma_free_coherent(&qedf->pdev->dev, QEDF_TERM_BUFF_SIZE, term_params,
  1216. term_params_dma);
  1217. }
  1218. static void qedf_cleanup_fcport(struct qedf_ctx *qedf,
  1219. struct qedf_rport *fcport)
  1220. {
  1221. struct fc_rport_priv *rdata = fcport->rdata;
  1222. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Cleaning up portid=%06x.\n",
  1223. fcport->rdata->ids.port_id);
  1224. /* Flush any remaining i/o's before we upload the connection */
  1225. qedf_flush_active_ios(fcport, -1);
  1226. if (test_and_clear_bit(QEDF_RPORT_SESSION_READY, &fcport->flags))
  1227. qedf_upload_connection(qedf, fcport);
  1228. qedf_free_sq(qedf, fcport);
  1229. fcport->rdata = NULL;
  1230. fcport->qedf = NULL;
  1231. kref_put(&rdata->kref, fc_rport_destroy);
  1232. }
  1233. /*
  1234. * This event_callback is called after successful completion of libfc
  1235. * initiated target login. qedf can proceed with initiating the session
  1236. * establishment.
  1237. */
  1238. static void qedf_rport_event_handler(struct fc_lport *lport,
  1239. struct fc_rport_priv *rdata,
  1240. enum fc_rport_event event)
  1241. {
  1242. struct qedf_ctx *qedf = lport_priv(lport);
  1243. struct fc_rport *rport = rdata->rport;
  1244. struct fc_rport_libfc_priv *rp;
  1245. struct qedf_rport *fcport;
  1246. u32 port_id;
  1247. int rval;
  1248. unsigned long flags;
  1249. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "event = %d, "
  1250. "port_id = 0x%x\n", event, rdata->ids.port_id);
  1251. switch (event) {
  1252. case RPORT_EV_READY:
  1253. if (!rport) {
  1254. QEDF_WARN(&(qedf->dbg_ctx), "rport is NULL.\n");
  1255. break;
  1256. }
  1257. rp = rport->dd_data;
  1258. fcport = (struct qedf_rport *)&rp[1];
  1259. fcport->qedf = qedf;
  1260. if (atomic_read(&qedf->num_offloads) >= QEDF_MAX_SESSIONS) {
  1261. QEDF_ERR(&(qedf->dbg_ctx), "Not offloading "
  1262. "portid=0x%x as max number of offloaded sessions "
  1263. "reached.\n", rdata->ids.port_id);
  1264. return;
  1265. }
  1266. /*
  1267. * Don't try to offload the session again. Can happen when we
  1268. * get an ADISC
  1269. */
  1270. if (test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
  1271. QEDF_WARN(&(qedf->dbg_ctx), "Session already "
  1272. "offloaded, portid=0x%x.\n",
  1273. rdata->ids.port_id);
  1274. return;
  1275. }
  1276. if (rport->port_id == FC_FID_DIR_SERV) {
  1277. /*
  1278. * qedf_rport structure doesn't exist for
  1279. * directory server.
  1280. * We should not come here, as lport will
  1281. * take care of fabric login
  1282. */
  1283. QEDF_WARN(&(qedf->dbg_ctx), "rport struct does not "
  1284. "exist for dir server port_id=%x\n",
  1285. rdata->ids.port_id);
  1286. break;
  1287. }
  1288. if (rdata->spp_type != FC_TYPE_FCP) {
  1289. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  1290. "Not offloading since spp type isn't FCP\n");
  1291. break;
  1292. }
  1293. if (!(rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET)) {
  1294. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  1295. "Not FCP target so not offloading\n");
  1296. break;
  1297. }
  1298. /* Initial reference held on entry, so this can't fail */
  1299. kref_get(&rdata->kref);
  1300. fcport->rdata = rdata;
  1301. fcport->rport = rport;
  1302. rval = qedf_alloc_sq(qedf, fcport);
  1303. if (rval) {
  1304. qedf_cleanup_fcport(qedf, fcport);
  1305. break;
  1306. }
  1307. /* Set device type */
  1308. if (rdata->flags & FC_RP_FLAGS_RETRY &&
  1309. rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET &&
  1310. !(rdata->ids.roles & FC_RPORT_ROLE_FCP_INITIATOR)) {
  1311. fcport->dev_type = QEDF_RPORT_TYPE_TAPE;
  1312. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  1313. "portid=%06x is a TAPE device.\n",
  1314. rdata->ids.port_id);
  1315. } else {
  1316. fcport->dev_type = QEDF_RPORT_TYPE_DISK;
  1317. }
  1318. rval = qedf_offload_connection(qedf, fcport);
  1319. if (rval) {
  1320. qedf_cleanup_fcport(qedf, fcport);
  1321. break;
  1322. }
  1323. /* Add fcport to list of qedf_ctx list of offloaded ports */
  1324. spin_lock_irqsave(&qedf->hba_lock, flags);
  1325. list_add_rcu(&fcport->peers, &qedf->fcports);
  1326. spin_unlock_irqrestore(&qedf->hba_lock, flags);
  1327. /*
  1328. * Set the session ready bit to let everyone know that this
  1329. * connection is ready for I/O
  1330. */
  1331. set_bit(QEDF_RPORT_SESSION_READY, &fcport->flags);
  1332. atomic_inc(&qedf->num_offloads);
  1333. break;
  1334. case RPORT_EV_LOGO:
  1335. case RPORT_EV_FAILED:
  1336. case RPORT_EV_STOP:
  1337. port_id = rdata->ids.port_id;
  1338. if (port_id == FC_FID_DIR_SERV)
  1339. break;
  1340. if (rdata->spp_type != FC_TYPE_FCP) {
  1341. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  1342. "No action since spp type isn't FCP\n");
  1343. break;
  1344. }
  1345. if (!(rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET)) {
  1346. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  1347. "Not FCP target so no action\n");
  1348. break;
  1349. }
  1350. if (!rport) {
  1351. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  1352. "port_id=%x - rport notcreated Yet!!\n", port_id);
  1353. break;
  1354. }
  1355. rp = rport->dd_data;
  1356. /*
  1357. * Perform session upload. Note that rdata->peers is already
  1358. * removed from disc->rports list before we get this event.
  1359. */
  1360. fcport = (struct qedf_rport *)&rp[1];
  1361. spin_lock_irqsave(&fcport->rport_lock, flags);
  1362. /* Only free this fcport if it is offloaded already */
  1363. if (test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags) &&
  1364. !test_bit(QEDF_RPORT_UPLOADING_CONNECTION,
  1365. &fcport->flags)) {
  1366. set_bit(QEDF_RPORT_UPLOADING_CONNECTION,
  1367. &fcport->flags);
  1368. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1369. qedf_cleanup_fcport(qedf, fcport);
  1370. /*
  1371. * Remove fcport to list of qedf_ctx list of offloaded
  1372. * ports
  1373. */
  1374. spin_lock_irqsave(&qedf->hba_lock, flags);
  1375. list_del_rcu(&fcport->peers);
  1376. spin_unlock_irqrestore(&qedf->hba_lock, flags);
  1377. clear_bit(QEDF_RPORT_UPLOADING_CONNECTION,
  1378. &fcport->flags);
  1379. atomic_dec(&qedf->num_offloads);
  1380. } else {
  1381. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  1382. }
  1383. break;
  1384. case RPORT_EV_NONE:
  1385. break;
  1386. }
  1387. }
  1388. static void qedf_abort_io(struct fc_lport *lport)
  1389. {
  1390. /* NO-OP but need to fill in the template */
  1391. }
  1392. static void qedf_fcp_cleanup(struct fc_lport *lport)
  1393. {
  1394. /*
  1395. * NO-OP but need to fill in template to prevent a NULL
  1396. * function pointer dereference during link down. I/Os
  1397. * will be flushed when port is uploaded.
  1398. */
  1399. }
  1400. static struct libfc_function_template qedf_lport_template = {
  1401. .frame_send = qedf_xmit,
  1402. .fcp_abort_io = qedf_abort_io,
  1403. .fcp_cleanup = qedf_fcp_cleanup,
  1404. .rport_event_callback = qedf_rport_event_handler,
  1405. .elsct_send = qedf_elsct_send,
  1406. };
  1407. static void qedf_fcoe_ctlr_setup(struct qedf_ctx *qedf)
  1408. {
  1409. fcoe_ctlr_init(&qedf->ctlr, FIP_MODE_AUTO);
  1410. qedf->ctlr.send = qedf_fip_send;
  1411. qedf->ctlr.get_src_addr = qedf_get_src_mac;
  1412. ether_addr_copy(qedf->ctlr.ctl_src_addr, qedf->mac);
  1413. }
  1414. static void qedf_setup_fdmi(struct qedf_ctx *qedf)
  1415. {
  1416. struct fc_lport *lport = qedf->lport;
  1417. u8 buf[8];
  1418. int pos;
  1419. uint32_t i;
  1420. /*
  1421. * fdmi_enabled needs to be set for libfc
  1422. * to execute FDMI registration
  1423. */
  1424. lport->fdmi_enabled = 1;
  1425. /*
  1426. * Setup the necessary fc_host attributes to that will be used to fill
  1427. * in the FDMI information.
  1428. */
  1429. /* Get the PCI-e Device Serial Number Capability */
  1430. pos = pci_find_ext_capability(qedf->pdev, PCI_EXT_CAP_ID_DSN);
  1431. if (pos) {
  1432. pos += 4;
  1433. for (i = 0; i < 8; i++)
  1434. pci_read_config_byte(qedf->pdev, pos + i, &buf[i]);
  1435. snprintf(fc_host_serial_number(lport->host),
  1436. FC_SERIAL_NUMBER_SIZE,
  1437. "%02X%02X%02X%02X%02X%02X%02X%02X",
  1438. buf[7], buf[6], buf[5], buf[4],
  1439. buf[3], buf[2], buf[1], buf[0]);
  1440. } else
  1441. snprintf(fc_host_serial_number(lport->host),
  1442. FC_SERIAL_NUMBER_SIZE, "Unknown");
  1443. snprintf(fc_host_manufacturer(lport->host),
  1444. FC_SERIAL_NUMBER_SIZE, "%s", "Marvell Semiconductor Inc.");
  1445. if (qedf->pdev->device == QL45xxx) {
  1446. snprintf(fc_host_model(lport->host),
  1447. FC_SYMBOLIC_NAME_SIZE, "%s", "QL45xxx");
  1448. snprintf(fc_host_model_description(lport->host),
  1449. FC_SYMBOLIC_NAME_SIZE, "%s",
  1450. "Marvell FastLinQ QL45xxx FCoE Adapter");
  1451. }
  1452. if (qedf->pdev->device == QL41xxx) {
  1453. snprintf(fc_host_model(lport->host),
  1454. FC_SYMBOLIC_NAME_SIZE, "%s", "QL41xxx");
  1455. snprintf(fc_host_model_description(lport->host),
  1456. FC_SYMBOLIC_NAME_SIZE, "%s",
  1457. "Marvell FastLinQ QL41xxx FCoE Adapter");
  1458. }
  1459. snprintf(fc_host_hardware_version(lport->host),
  1460. FC_VERSION_STRING_SIZE, "Rev %d", qedf->pdev->revision);
  1461. snprintf(fc_host_driver_version(lport->host),
  1462. FC_VERSION_STRING_SIZE, "%s", QEDF_VERSION);
  1463. snprintf(fc_host_firmware_version(lport->host),
  1464. FC_VERSION_STRING_SIZE, "%d.%d.%d.%d",
  1465. FW_MAJOR_VERSION, FW_MINOR_VERSION, FW_REVISION_VERSION,
  1466. FW_ENGINEERING_VERSION);
  1467. snprintf(fc_host_vendor_identifier(lport->host),
  1468. FC_VENDOR_IDENTIFIER, "%s", "Marvell");
  1469. }
  1470. static int qedf_lport_setup(struct qedf_ctx *qedf)
  1471. {
  1472. struct fc_lport *lport = qedf->lport;
  1473. lport->link_up = 0;
  1474. lport->max_retry_count = QEDF_FLOGI_RETRY_CNT;
  1475. lport->max_rport_retry_count = QEDF_RPORT_RETRY_CNT;
  1476. lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
  1477. FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
  1478. lport->boot_time = jiffies;
  1479. lport->e_d_tov = 2 * 1000;
  1480. lport->r_a_tov = 10 * 1000;
  1481. /* Set NPIV support */
  1482. lport->does_npiv = 1;
  1483. fc_host_max_npiv_vports(lport->host) = QEDF_MAX_NPIV;
  1484. fc_set_wwnn(lport, qedf->wwnn);
  1485. fc_set_wwpn(lport, qedf->wwpn);
  1486. if (fcoe_libfc_config(lport, &qedf->ctlr, &qedf_lport_template, 0)) {
  1487. QEDF_ERR(&qedf->dbg_ctx,
  1488. "fcoe_libfc_config failed.\n");
  1489. return -ENOMEM;
  1490. }
  1491. /* Allocate the exchange manager */
  1492. fc_exch_mgr_alloc(lport, FC_CLASS_3, FCOE_PARAMS_NUM_TASKS,
  1493. 0xfffe, NULL);
  1494. if (fc_lport_init_stats(lport))
  1495. return -ENOMEM;
  1496. /* Finish lport config */
  1497. fc_lport_config(lport);
  1498. /* Set max frame size */
  1499. fc_set_mfs(lport, QEDF_MFS);
  1500. fc_host_maxframe_size(lport->host) = lport->mfs;
  1501. /* Set default dev_loss_tmo based on module parameter */
  1502. fc_host_dev_loss_tmo(lport->host) = qedf_dev_loss_tmo;
  1503. /* Set symbolic node name */
  1504. if (qedf->pdev->device == QL45xxx)
  1505. snprintf(fc_host_symbolic_name(lport->host), 256,
  1506. "Marvell FastLinQ 45xxx FCoE v%s", QEDF_VERSION);
  1507. if (qedf->pdev->device == QL41xxx)
  1508. snprintf(fc_host_symbolic_name(lport->host), 256,
  1509. "Marvell FastLinQ 41xxx FCoE v%s", QEDF_VERSION);
  1510. qedf_setup_fdmi(qedf);
  1511. return 0;
  1512. }
  1513. /*
  1514. * NPIV functions
  1515. */
  1516. static int qedf_vport_libfc_config(struct fc_vport *vport,
  1517. struct fc_lport *lport)
  1518. {
  1519. lport->link_up = 0;
  1520. lport->qfull = 0;
  1521. lport->max_retry_count = QEDF_FLOGI_RETRY_CNT;
  1522. lport->max_rport_retry_count = QEDF_RPORT_RETRY_CNT;
  1523. lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
  1524. FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
  1525. lport->boot_time = jiffies;
  1526. lport->e_d_tov = 2 * 1000;
  1527. lport->r_a_tov = 10 * 1000;
  1528. lport->does_npiv = 1; /* Temporary until we add NPIV support */
  1529. /* Allocate stats for vport */
  1530. if (fc_lport_init_stats(lport))
  1531. return -ENOMEM;
  1532. /* Finish lport config */
  1533. fc_lport_config(lport);
  1534. /* offload related configuration */
  1535. lport->crc_offload = 0;
  1536. lport->seq_offload = 0;
  1537. lport->lro_enabled = 0;
  1538. lport->lro_xid = 0;
  1539. lport->lso_max = 0;
  1540. return 0;
  1541. }
  1542. static int qedf_vport_create(struct fc_vport *vport, bool disabled)
  1543. {
  1544. struct Scsi_Host *shost = vport_to_shost(vport);
  1545. struct fc_lport *n_port = shost_priv(shost);
  1546. struct fc_lport *vn_port;
  1547. struct qedf_ctx *base_qedf = lport_priv(n_port);
  1548. struct qedf_ctx *vport_qedf;
  1549. char buf[32];
  1550. int rc = 0;
  1551. rc = fcoe_validate_vport_create(vport);
  1552. if (rc) {
  1553. fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
  1554. QEDF_WARN(&(base_qedf->dbg_ctx), "Failed to create vport, "
  1555. "WWPN (0x%s) already exists.\n", buf);
  1556. return rc;
  1557. }
  1558. if (atomic_read(&base_qedf->link_state) != QEDF_LINK_UP) {
  1559. QEDF_WARN(&(base_qedf->dbg_ctx), "Cannot create vport "
  1560. "because link is not up.\n");
  1561. return -EIO;
  1562. }
  1563. vn_port = libfc_vport_create(vport, sizeof(struct qedf_ctx));
  1564. if (!vn_port) {
  1565. QEDF_WARN(&(base_qedf->dbg_ctx), "Could not create lport "
  1566. "for vport.\n");
  1567. return -ENOMEM;
  1568. }
  1569. fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
  1570. QEDF_ERR(&(base_qedf->dbg_ctx), "Creating NPIV port, WWPN=%s.\n",
  1571. buf);
  1572. /* Copy some fields from base_qedf */
  1573. vport_qedf = lport_priv(vn_port);
  1574. memcpy(vport_qedf, base_qedf, sizeof(struct qedf_ctx));
  1575. /* Set qedf data specific to this vport */
  1576. vport_qedf->lport = vn_port;
  1577. /* Use same hba_lock as base_qedf */
  1578. vport_qedf->hba_lock = base_qedf->hba_lock;
  1579. vport_qedf->pdev = base_qedf->pdev;
  1580. vport_qedf->cmd_mgr = base_qedf->cmd_mgr;
  1581. init_completion(&vport_qedf->flogi_compl);
  1582. INIT_LIST_HEAD(&vport_qedf->fcports);
  1583. INIT_DELAYED_WORK(&vport_qedf->stag_work, qedf_stag_change_work);
  1584. rc = qedf_vport_libfc_config(vport, vn_port);
  1585. if (rc) {
  1586. QEDF_ERR(&(base_qedf->dbg_ctx), "Could not allocate memory "
  1587. "for lport stats.\n");
  1588. goto err;
  1589. }
  1590. fc_set_wwnn(vn_port, vport->node_name);
  1591. fc_set_wwpn(vn_port, vport->port_name);
  1592. vport_qedf->wwnn = vn_port->wwnn;
  1593. vport_qedf->wwpn = vn_port->wwpn;
  1594. vn_port->host->transportt = qedf_fc_vport_transport_template;
  1595. vn_port->host->can_queue = FCOE_PARAMS_NUM_TASKS;
  1596. vn_port->host->max_lun = qedf_max_lun;
  1597. vn_port->host->sg_tablesize = QEDF_MAX_BDS_PER_CMD;
  1598. vn_port->host->max_cmd_len = QEDF_MAX_CDB_LEN;
  1599. vn_port->host->max_id = QEDF_MAX_SESSIONS;
  1600. rc = scsi_add_host(vn_port->host, &vport->dev);
  1601. if (rc) {
  1602. QEDF_WARN(&base_qedf->dbg_ctx,
  1603. "Error adding Scsi_Host rc=0x%x.\n", rc);
  1604. goto err;
  1605. }
  1606. /* Set default dev_loss_tmo based on module parameter */
  1607. fc_host_dev_loss_tmo(vn_port->host) = qedf_dev_loss_tmo;
  1608. /* Init libfc stuffs */
  1609. memcpy(&vn_port->tt, &qedf_lport_template,
  1610. sizeof(qedf_lport_template));
  1611. fc_exch_init(vn_port);
  1612. fc_elsct_init(vn_port);
  1613. fc_lport_init(vn_port);
  1614. fc_disc_init(vn_port);
  1615. fc_disc_config(vn_port, vn_port);
  1616. /* Allocate the exchange manager */
  1617. shost = vport_to_shost(vport);
  1618. n_port = shost_priv(shost);
  1619. fc_exch_mgr_list_clone(n_port, vn_port);
  1620. /* Set max frame size */
  1621. fc_set_mfs(vn_port, QEDF_MFS);
  1622. fc_host_port_type(vn_port->host) = FC_PORTTYPE_UNKNOWN;
  1623. if (disabled) {
  1624. fc_vport_set_state(vport, FC_VPORT_DISABLED);
  1625. } else {
  1626. vn_port->boot_time = jiffies;
  1627. fc_fabric_login(vn_port);
  1628. fc_vport_setlink(vn_port);
  1629. }
  1630. /* Set symbolic node name */
  1631. if (base_qedf->pdev->device == QL45xxx)
  1632. snprintf(fc_host_symbolic_name(vn_port->host), 256,
  1633. "Marvell FastLinQ 45xxx FCoE v%s", QEDF_VERSION);
  1634. if (base_qedf->pdev->device == QL41xxx)
  1635. snprintf(fc_host_symbolic_name(vn_port->host), 256,
  1636. "Marvell FastLinQ 41xxx FCoE v%s", QEDF_VERSION);
  1637. /* Set supported speed */
  1638. fc_host_supported_speeds(vn_port->host) = n_port->link_supported_speeds;
  1639. /* Set speed */
  1640. vn_port->link_speed = n_port->link_speed;
  1641. /* Set port type */
  1642. fc_host_port_type(vn_port->host) = FC_PORTTYPE_NPIV;
  1643. /* Set maxframe size */
  1644. fc_host_maxframe_size(vn_port->host) = n_port->mfs;
  1645. QEDF_INFO(&(base_qedf->dbg_ctx), QEDF_LOG_NPIV, "vn_port=%p.\n",
  1646. vn_port);
  1647. /* Set up debug context for vport */
  1648. vport_qedf->dbg_ctx.host_no = vn_port->host->host_no;
  1649. vport_qedf->dbg_ctx.pdev = base_qedf->pdev;
  1650. return 0;
  1651. err:
  1652. scsi_host_put(vn_port->host);
  1653. return rc;
  1654. }
  1655. static int qedf_vport_destroy(struct fc_vport *vport)
  1656. {
  1657. struct Scsi_Host *shost = vport_to_shost(vport);
  1658. struct fc_lport *n_port = shost_priv(shost);
  1659. struct fc_lport *vn_port = vport->dd_data;
  1660. struct qedf_ctx *qedf = lport_priv(vn_port);
  1661. if (!qedf) {
  1662. QEDF_ERR(NULL, "qedf is NULL.\n");
  1663. goto out;
  1664. }
  1665. /* Set unloading bit on vport qedf_ctx to prevent more I/O */
  1666. set_bit(QEDF_UNLOADING, &qedf->flags);
  1667. mutex_lock(&n_port->lp_mutex);
  1668. list_del(&vn_port->list);
  1669. mutex_unlock(&n_port->lp_mutex);
  1670. fc_fabric_logoff(vn_port);
  1671. fc_lport_destroy(vn_port);
  1672. /* Detach from scsi-ml */
  1673. fc_remove_host(vn_port->host);
  1674. scsi_remove_host(vn_port->host);
  1675. /*
  1676. * Only try to release the exchange manager if the vn_port
  1677. * configuration is complete.
  1678. */
  1679. if (vn_port->state == LPORT_ST_READY)
  1680. fc_exch_mgr_free(vn_port);
  1681. /* Free memory used by statistical counters */
  1682. fc_lport_free_stats(vn_port);
  1683. /* Release Scsi_Host */
  1684. scsi_host_put(vn_port->host);
  1685. out:
  1686. return 0;
  1687. }
  1688. static int qedf_vport_disable(struct fc_vport *vport, bool disable)
  1689. {
  1690. struct fc_lport *lport = vport->dd_data;
  1691. if (disable) {
  1692. fc_vport_set_state(vport, FC_VPORT_DISABLED);
  1693. fc_fabric_logoff(lport);
  1694. } else {
  1695. lport->boot_time = jiffies;
  1696. fc_fabric_login(lport);
  1697. fc_vport_setlink(lport);
  1698. }
  1699. return 0;
  1700. }
  1701. /*
  1702. * During removal we need to wait for all the vports associated with a port
  1703. * to be destroyed so we avoid a race condition where libfc is still trying
  1704. * to reap vports while the driver remove function has already reaped the
  1705. * driver contexts associated with the physical port.
  1706. */
  1707. static void qedf_wait_for_vport_destroy(struct qedf_ctx *qedf)
  1708. {
  1709. struct fc_host_attrs *fc_host = shost_to_fc_host(qedf->lport->host);
  1710. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_NPIV,
  1711. "Entered.\n");
  1712. while (fc_host->npiv_vports_inuse > 0) {
  1713. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_NPIV,
  1714. "Waiting for all vports to be reaped.\n");
  1715. msleep(1000);
  1716. }
  1717. }
  1718. /**
  1719. * qedf_fcoe_reset - Resets the fcoe
  1720. *
  1721. * @shost: shost the reset is from
  1722. *
  1723. * Returns: always 0
  1724. */
  1725. static int qedf_fcoe_reset(struct Scsi_Host *shost)
  1726. {
  1727. struct fc_lport *lport = shost_priv(shost);
  1728. qedf_ctx_soft_reset(lport);
  1729. return 0;
  1730. }
  1731. static void qedf_get_host_port_id(struct Scsi_Host *shost)
  1732. {
  1733. struct fc_lport *lport = shost_priv(shost);
  1734. fc_host_port_id(shost) = lport->port_id;
  1735. }
  1736. static struct fc_host_statistics *qedf_fc_get_host_stats(struct Scsi_Host
  1737. *shost)
  1738. {
  1739. struct fc_host_statistics *qedf_stats;
  1740. struct fc_lport *lport = shost_priv(shost);
  1741. struct qedf_ctx *qedf = lport_priv(lport);
  1742. struct qed_fcoe_stats *fw_fcoe_stats;
  1743. qedf_stats = fc_get_host_stats(shost);
  1744. /* We don't collect offload stats for specific NPIV ports */
  1745. if (lport->vport)
  1746. goto out;
  1747. fw_fcoe_stats = kmalloc_obj(struct qed_fcoe_stats);
  1748. if (!fw_fcoe_stats) {
  1749. QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate memory for "
  1750. "fw_fcoe_stats.\n");
  1751. goto out;
  1752. }
  1753. mutex_lock(&qedf->stats_mutex);
  1754. /* Query firmware for offload stats */
  1755. qed_ops->get_stats(qedf->cdev, fw_fcoe_stats);
  1756. /*
  1757. * The expectation is that we add our offload stats to the stats
  1758. * being maintained by libfc each time the fc_get_host_status callback
  1759. * is invoked. The additions are not carried over for each call to
  1760. * the fc_get_host_stats callback.
  1761. */
  1762. qedf_stats->tx_frames += fw_fcoe_stats->fcoe_tx_data_pkt_cnt +
  1763. fw_fcoe_stats->fcoe_tx_xfer_pkt_cnt +
  1764. fw_fcoe_stats->fcoe_tx_other_pkt_cnt;
  1765. qedf_stats->rx_frames += fw_fcoe_stats->fcoe_rx_data_pkt_cnt +
  1766. fw_fcoe_stats->fcoe_rx_xfer_pkt_cnt +
  1767. fw_fcoe_stats->fcoe_rx_other_pkt_cnt;
  1768. qedf_stats->fcp_input_megabytes +=
  1769. do_div(fw_fcoe_stats->fcoe_rx_byte_cnt, 1000000);
  1770. qedf_stats->fcp_output_megabytes +=
  1771. do_div(fw_fcoe_stats->fcoe_tx_byte_cnt, 1000000);
  1772. qedf_stats->rx_words += fw_fcoe_stats->fcoe_rx_byte_cnt / 4;
  1773. qedf_stats->tx_words += fw_fcoe_stats->fcoe_tx_byte_cnt / 4;
  1774. qedf_stats->invalid_crc_count +=
  1775. fw_fcoe_stats->fcoe_silent_drop_pkt_crc_error_cnt;
  1776. qedf_stats->dumped_frames =
  1777. fw_fcoe_stats->fcoe_silent_drop_total_pkt_cnt;
  1778. qedf_stats->error_frames +=
  1779. fw_fcoe_stats->fcoe_silent_drop_total_pkt_cnt;
  1780. qedf_stats->fcp_input_requests += qedf->input_requests;
  1781. qedf_stats->fcp_output_requests += qedf->output_requests;
  1782. qedf_stats->fcp_control_requests += qedf->control_requests;
  1783. qedf_stats->fcp_packet_aborts += qedf->packet_aborts;
  1784. qedf_stats->fcp_frame_alloc_failures += qedf->alloc_failures;
  1785. mutex_unlock(&qedf->stats_mutex);
  1786. kfree(fw_fcoe_stats);
  1787. out:
  1788. return qedf_stats;
  1789. }
  1790. static struct fc_function_template qedf_fc_transport_fn = {
  1791. .show_host_node_name = 1,
  1792. .show_host_port_name = 1,
  1793. .show_host_supported_classes = 1,
  1794. .show_host_supported_fc4s = 1,
  1795. .show_host_active_fc4s = 1,
  1796. .show_host_maxframe_size = 1,
  1797. .get_host_port_id = qedf_get_host_port_id,
  1798. .show_host_port_id = 1,
  1799. .show_host_supported_speeds = 1,
  1800. .get_host_speed = fc_get_host_speed,
  1801. .show_host_speed = 1,
  1802. .show_host_port_type = 1,
  1803. .get_host_port_state = fc_get_host_port_state,
  1804. .show_host_port_state = 1,
  1805. .show_host_symbolic_name = 1,
  1806. /*
  1807. * Tell FC transport to allocate enough space to store the backpointer
  1808. * for the associate qedf_rport struct.
  1809. */
  1810. .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
  1811. sizeof(struct qedf_rport)),
  1812. .show_rport_maxframe_size = 1,
  1813. .show_rport_supported_classes = 1,
  1814. .show_host_fabric_name = 1,
  1815. .show_starget_node_name = 1,
  1816. .show_starget_port_name = 1,
  1817. .show_starget_port_id = 1,
  1818. .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
  1819. .show_rport_dev_loss_tmo = 1,
  1820. .get_fc_host_stats = qedf_fc_get_host_stats,
  1821. .issue_fc_host_lip = qedf_fcoe_reset,
  1822. .vport_create = qedf_vport_create,
  1823. .vport_delete = qedf_vport_destroy,
  1824. .vport_disable = qedf_vport_disable,
  1825. .bsg_request = fc_lport_bsg_request,
  1826. };
  1827. static struct fc_function_template qedf_fc_vport_transport_fn = {
  1828. .show_host_node_name = 1,
  1829. .show_host_port_name = 1,
  1830. .show_host_supported_classes = 1,
  1831. .show_host_supported_fc4s = 1,
  1832. .show_host_active_fc4s = 1,
  1833. .show_host_maxframe_size = 1,
  1834. .show_host_port_id = 1,
  1835. .show_host_supported_speeds = 1,
  1836. .get_host_speed = fc_get_host_speed,
  1837. .show_host_speed = 1,
  1838. .show_host_port_type = 1,
  1839. .get_host_port_state = fc_get_host_port_state,
  1840. .show_host_port_state = 1,
  1841. .show_host_symbolic_name = 1,
  1842. .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
  1843. sizeof(struct qedf_rport)),
  1844. .show_rport_maxframe_size = 1,
  1845. .show_rport_supported_classes = 1,
  1846. .show_host_fabric_name = 1,
  1847. .show_starget_node_name = 1,
  1848. .show_starget_port_name = 1,
  1849. .show_starget_port_id = 1,
  1850. .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
  1851. .show_rport_dev_loss_tmo = 1,
  1852. .get_fc_host_stats = fc_get_host_stats,
  1853. .issue_fc_host_lip = qedf_fcoe_reset,
  1854. .bsg_request = fc_lport_bsg_request,
  1855. };
  1856. static bool qedf_fp_has_work(struct qedf_fastpath *fp)
  1857. {
  1858. struct qedf_ctx *qedf = fp->qedf;
  1859. struct global_queue *que;
  1860. struct qed_sb_info *sb_info = fp->sb_info;
  1861. struct status_block *sb = sb_info->sb_virt;
  1862. u16 prod_idx;
  1863. /* Get the pointer to the global CQ this completion is on */
  1864. que = qedf->global_queues[fp->sb_id];
  1865. /* Be sure all responses have been written to PI */
  1866. rmb();
  1867. /* Get the current firmware producer index */
  1868. prod_idx = sb->pi_array[QEDF_FCOE_PARAMS_GL_RQ_PI];
  1869. return (que->cq_prod_idx != prod_idx);
  1870. }
  1871. /*
  1872. * Interrupt handler code.
  1873. */
  1874. /* Process completion queue and copy CQE contents for deferred processesing
  1875. *
  1876. * Return true if we should wake the I/O thread, false if not.
  1877. */
  1878. static bool qedf_process_completions(struct qedf_fastpath *fp)
  1879. {
  1880. struct qedf_ctx *qedf = fp->qedf;
  1881. struct qed_sb_info *sb_info = fp->sb_info;
  1882. struct status_block *sb = sb_info->sb_virt;
  1883. struct global_queue *que;
  1884. u16 prod_idx;
  1885. struct fcoe_cqe *cqe;
  1886. struct qedf_io_work *io_work;
  1887. unsigned int cpu;
  1888. struct qedf_ioreq *io_req = NULL;
  1889. u16 xid;
  1890. u16 new_cqes;
  1891. u32 comp_type;
  1892. /* Get the current firmware producer index */
  1893. prod_idx = sb->pi_array[QEDF_FCOE_PARAMS_GL_RQ_PI];
  1894. /* Get the pointer to the global CQ this completion is on */
  1895. que = qedf->global_queues[fp->sb_id];
  1896. /* Calculate the amount of new elements since last processing */
  1897. new_cqes = (prod_idx >= que->cq_prod_idx) ?
  1898. (prod_idx - que->cq_prod_idx) :
  1899. 0x10000 - que->cq_prod_idx + prod_idx;
  1900. /* Save producer index */
  1901. que->cq_prod_idx = prod_idx;
  1902. while (new_cqes) {
  1903. fp->completions++;
  1904. cqe = &que->cq[que->cq_cons_idx];
  1905. comp_type = (cqe->cqe_data >> FCOE_CQE_CQE_TYPE_SHIFT) &
  1906. FCOE_CQE_CQE_TYPE_MASK;
  1907. /*
  1908. * Process unsolicited CQEs directly in the interrupt handler
  1909. * sine we need the fastpath ID
  1910. */
  1911. if (comp_type == FCOE_UNSOLIC_CQE_TYPE) {
  1912. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_UNSOL,
  1913. "Unsolicated CQE.\n");
  1914. qedf_process_unsol_compl(qedf, fp->sb_id, cqe);
  1915. /*
  1916. * Don't add a work list item. Increment consumer
  1917. * consumer index and move on.
  1918. */
  1919. goto inc_idx;
  1920. }
  1921. xid = cqe->cqe_data & FCOE_CQE_TASK_ID_MASK;
  1922. io_req = &qedf->cmd_mgr->cmds[xid];
  1923. /*
  1924. * Figure out which percpu thread we should queue this I/O
  1925. * on.
  1926. */
  1927. if (!io_req)
  1928. /* If there is not io_req associated with this CQE
  1929. * just queue it on CPU 0
  1930. */
  1931. cpu = 0;
  1932. else {
  1933. cpu = io_req->cpu;
  1934. io_req->int_cpu = smp_processor_id();
  1935. }
  1936. io_work = mempool_alloc(qedf->io_mempool, GFP_ATOMIC);
  1937. if (!io_work) {
  1938. QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate "
  1939. "work for I/O completion.\n");
  1940. continue;
  1941. }
  1942. memset(io_work, 0, sizeof(struct qedf_io_work));
  1943. INIT_WORK(&io_work->work, qedf_fp_io_handler);
  1944. /* Copy contents of CQE for deferred processing */
  1945. memcpy(&io_work->cqe, cqe, sizeof(struct fcoe_cqe));
  1946. io_work->qedf = fp->qedf;
  1947. io_work->fp = NULL; /* Only used for unsolicited frames */
  1948. queue_work_on(cpu, qedf_io_wq, &io_work->work);
  1949. inc_idx:
  1950. que->cq_cons_idx++;
  1951. if (que->cq_cons_idx == fp->cq_num_entries)
  1952. que->cq_cons_idx = 0;
  1953. new_cqes--;
  1954. }
  1955. return true;
  1956. }
  1957. /* MSI-X fastpath handler code */
  1958. static irqreturn_t qedf_msix_handler(int irq, void *dev_id)
  1959. {
  1960. struct qedf_fastpath *fp = dev_id;
  1961. if (!fp) {
  1962. QEDF_ERR(NULL, "fp is null.\n");
  1963. return IRQ_HANDLED;
  1964. }
  1965. if (!fp->sb_info) {
  1966. QEDF_ERR(NULL, "fp->sb_info in null.");
  1967. return IRQ_HANDLED;
  1968. }
  1969. /*
  1970. * Disable interrupts for this status block while we process new
  1971. * completions
  1972. */
  1973. qed_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0 /*do not update*/);
  1974. while (1) {
  1975. qedf_process_completions(fp);
  1976. if (qedf_fp_has_work(fp) == 0) {
  1977. /* Update the sb information */
  1978. qed_sb_update_sb_idx(fp->sb_info);
  1979. /* Check for more work */
  1980. rmb();
  1981. if (qedf_fp_has_work(fp) == 0) {
  1982. /* Re-enable interrupts */
  1983. qed_sb_ack(fp->sb_info, IGU_INT_ENABLE, 1);
  1984. return IRQ_HANDLED;
  1985. }
  1986. }
  1987. }
  1988. /* Do we ever want to break out of above loop? */
  1989. return IRQ_HANDLED;
  1990. }
  1991. /* simd handler for MSI/INTa */
  1992. static void qedf_simd_int_handler(void *cookie)
  1993. {
  1994. /* Cookie is qedf_ctx struct */
  1995. struct qedf_ctx *qedf = (struct qedf_ctx *)cookie;
  1996. QEDF_WARN(&(qedf->dbg_ctx), "qedf=%p.\n", qedf);
  1997. }
  1998. #define QEDF_SIMD_HANDLER_NUM 0
  1999. static void qedf_sync_free_irqs(struct qedf_ctx *qedf)
  2000. {
  2001. int i;
  2002. u16 vector_idx = 0;
  2003. u32 vector;
  2004. if (qedf->int_info.msix_cnt) {
  2005. for (i = 0; i < qedf->int_info.used_cnt; i++) {
  2006. vector_idx = i * qedf->dev_info.common.num_hwfns +
  2007. qed_ops->common->get_affin_hwfn_idx(qedf->cdev);
  2008. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
  2009. "Freeing IRQ #%d vector_idx=%d.\n",
  2010. i, vector_idx);
  2011. vector = qedf->int_info.msix[vector_idx].vector;
  2012. synchronize_irq(vector);
  2013. irq_set_affinity_hint(vector, NULL);
  2014. irq_set_affinity_notifier(vector, NULL);
  2015. free_irq(vector, &qedf->fp_array[i]);
  2016. }
  2017. } else
  2018. qed_ops->common->simd_handler_clean(qedf->cdev,
  2019. QEDF_SIMD_HANDLER_NUM);
  2020. qedf->int_info.used_cnt = 0;
  2021. qed_ops->common->set_fp_int(qedf->cdev, 0);
  2022. }
  2023. static int qedf_request_msix_irq(struct qedf_ctx *qedf)
  2024. {
  2025. int i, rc, cpu;
  2026. u16 vector_idx = 0;
  2027. u32 vector;
  2028. cpu = cpumask_first(cpu_online_mask);
  2029. for (i = 0; i < qedf->num_queues; i++) {
  2030. vector_idx = i * qedf->dev_info.common.num_hwfns +
  2031. qed_ops->common->get_affin_hwfn_idx(qedf->cdev);
  2032. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
  2033. "Requesting IRQ #%d vector_idx=%d.\n",
  2034. i, vector_idx);
  2035. vector = qedf->int_info.msix[vector_idx].vector;
  2036. rc = request_irq(vector, qedf_msix_handler, 0, "qedf",
  2037. &qedf->fp_array[i]);
  2038. if (rc) {
  2039. QEDF_WARN(&(qedf->dbg_ctx), "request_irq failed.\n");
  2040. qedf_sync_free_irqs(qedf);
  2041. return rc;
  2042. }
  2043. qedf->int_info.used_cnt++;
  2044. rc = irq_set_affinity_hint(vector, get_cpu_mask(cpu));
  2045. cpu = cpumask_next(cpu, cpu_online_mask);
  2046. }
  2047. return 0;
  2048. }
  2049. static int qedf_setup_int(struct qedf_ctx *qedf)
  2050. {
  2051. int rc = 0;
  2052. /*
  2053. * Learn interrupt configuration
  2054. */
  2055. rc = qed_ops->common->set_fp_int(qedf->cdev, num_online_cpus());
  2056. if (rc <= 0)
  2057. return 0;
  2058. rc = qed_ops->common->get_fp_int(qedf->cdev, &qedf->int_info);
  2059. if (rc)
  2060. return 0;
  2061. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Number of msix_cnt = "
  2062. "0x%x num of cpus = 0x%x\n", qedf->int_info.msix_cnt,
  2063. num_online_cpus());
  2064. if (qedf->int_info.msix_cnt)
  2065. return qedf_request_msix_irq(qedf);
  2066. qed_ops->common->simd_handler_config(qedf->cdev, &qedf,
  2067. QEDF_SIMD_HANDLER_NUM, qedf_simd_int_handler);
  2068. qedf->int_info.used_cnt = 1;
  2069. QEDF_ERR(&qedf->dbg_ctx,
  2070. "Cannot load driver due to a lack of MSI-X vectors.\n");
  2071. return -EINVAL;
  2072. }
  2073. /* Main function for libfc frame reception */
  2074. static void qedf_recv_frame(struct qedf_ctx *qedf,
  2075. struct sk_buff *skb)
  2076. {
  2077. u32 fr_len;
  2078. struct fc_lport *lport;
  2079. struct fc_frame_header *fh;
  2080. struct fcoe_crc_eof crc_eof;
  2081. struct fc_frame *fp;
  2082. u8 *mac = NULL;
  2083. u8 *dest_mac = NULL;
  2084. struct fcoe_hdr *hp;
  2085. struct qedf_rport *fcport;
  2086. struct fc_lport *vn_port;
  2087. u32 f_ctl;
  2088. lport = qedf->lport;
  2089. if (lport == NULL || lport->state == LPORT_ST_DISABLED) {
  2090. QEDF_WARN(NULL, "Invalid lport struct or lport disabled.\n");
  2091. kfree_skb(skb);
  2092. return;
  2093. }
  2094. if (skb_is_nonlinear(skb))
  2095. skb_linearize(skb);
  2096. mac = eth_hdr(skb)->h_source;
  2097. dest_mac = eth_hdr(skb)->h_dest;
  2098. /* Pull the header */
  2099. hp = (struct fcoe_hdr *)skb->data;
  2100. fh = (struct fc_frame_header *) skb_transport_header(skb);
  2101. skb_pull(skb, sizeof(struct fcoe_hdr));
  2102. fr_len = skb->len - sizeof(struct fcoe_crc_eof);
  2103. fp = (struct fc_frame *)skb;
  2104. fc_frame_init(fp);
  2105. fr_dev(fp) = lport;
  2106. fr_sof(fp) = hp->fcoe_sof;
  2107. if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
  2108. QEDF_INFO(NULL, QEDF_LOG_LL2, "skb_copy_bits failed.\n");
  2109. kfree_skb(skb);
  2110. return;
  2111. }
  2112. fr_eof(fp) = crc_eof.fcoe_eof;
  2113. fr_crc(fp) = crc_eof.fcoe_crc32;
  2114. if (pskb_trim(skb, fr_len)) {
  2115. QEDF_INFO(NULL, QEDF_LOG_LL2, "pskb_trim failed.\n");
  2116. kfree_skb(skb);
  2117. return;
  2118. }
  2119. fh = fc_frame_header_get(fp);
  2120. /*
  2121. * Invalid frame filters.
  2122. */
  2123. if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
  2124. fh->fh_type == FC_TYPE_FCP) {
  2125. /* Drop FCP data. We dont this in L2 path */
  2126. kfree_skb(skb);
  2127. return;
  2128. }
  2129. if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
  2130. fh->fh_type == FC_TYPE_ELS) {
  2131. switch (fc_frame_payload_op(fp)) {
  2132. case ELS_LOGO:
  2133. if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
  2134. /* drop non-FIP LOGO */
  2135. kfree_skb(skb);
  2136. return;
  2137. }
  2138. break;
  2139. }
  2140. }
  2141. if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
  2142. /* Drop incoming ABTS */
  2143. kfree_skb(skb);
  2144. return;
  2145. }
  2146. if (ntoh24(&dest_mac[3]) != ntoh24(fh->fh_d_id)) {
  2147. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
  2148. "FC frame d_id mismatch with MAC %pM.\n", dest_mac);
  2149. kfree_skb(skb);
  2150. return;
  2151. }
  2152. if (qedf->ctlr.state) {
  2153. if (!ether_addr_equal(mac, qedf->ctlr.dest_addr)) {
  2154. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
  2155. "Wrong source address: mac:%pM dest_addr:%pM.\n",
  2156. mac, qedf->ctlr.dest_addr);
  2157. kfree_skb(skb);
  2158. return;
  2159. }
  2160. }
  2161. vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
  2162. /*
  2163. * If the destination ID from the frame header does not match what we
  2164. * have on record for lport and the search for a NPIV port came up
  2165. * empty then this is not addressed to our port so simply drop it.
  2166. */
  2167. if (lport->port_id != ntoh24(fh->fh_d_id) && !vn_port) {
  2168. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_LL2,
  2169. "Dropping frame due to destination mismatch: lport->port_id=0x%x fh->d_id=0x%x.\n",
  2170. lport->port_id, ntoh24(fh->fh_d_id));
  2171. kfree_skb(skb);
  2172. return;
  2173. }
  2174. f_ctl = ntoh24(fh->fh_f_ctl);
  2175. if ((fh->fh_type == FC_TYPE_BLS) && (f_ctl & FC_FC_SEQ_CTX) &&
  2176. (f_ctl & FC_FC_EX_CTX)) {
  2177. /* Drop incoming ABTS response that has both SEQ/EX CTX set */
  2178. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_LL2,
  2179. "Dropping ABTS response as both SEQ/EX CTX set.\n");
  2180. kfree_skb(skb);
  2181. return;
  2182. }
  2183. /*
  2184. * If a connection is uploading, drop incoming FCoE frames as there
  2185. * is a small window where we could try to return a frame while libfc
  2186. * is trying to clean things up.
  2187. */
  2188. /* Get fcport associated with d_id if it exists */
  2189. fcport = qedf_fcport_lookup(qedf, ntoh24(fh->fh_d_id));
  2190. if (fcport && test_bit(QEDF_RPORT_UPLOADING_CONNECTION,
  2191. &fcport->flags)) {
  2192. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
  2193. "Connection uploading, dropping fp=%p.\n", fp);
  2194. kfree_skb(skb);
  2195. return;
  2196. }
  2197. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2, "FCoE frame receive: "
  2198. "skb=%p fp=%p src=%06x dest=%06x r_ctl=%x fh_type=%x.\n", skb, fp,
  2199. ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id), fh->fh_r_ctl,
  2200. fh->fh_type);
  2201. if (qedf_dump_frames)
  2202. print_hex_dump(KERN_WARNING, "fcoe: ", DUMP_PREFIX_OFFSET, 16,
  2203. 1, skb->data, skb->len, false);
  2204. fc_exch_recv(lport, fp);
  2205. }
  2206. static void qedf_ll2_process_skb(struct work_struct *work)
  2207. {
  2208. struct qedf_skb_work *skb_work =
  2209. container_of(work, struct qedf_skb_work, work);
  2210. struct qedf_ctx *qedf = skb_work->qedf;
  2211. struct sk_buff *skb = skb_work->skb;
  2212. struct ethhdr *eh;
  2213. if (!qedf) {
  2214. QEDF_ERR(NULL, "qedf is NULL\n");
  2215. goto err_out;
  2216. }
  2217. eh = (struct ethhdr *)skb->data;
  2218. /* Undo VLAN encapsulation */
  2219. if (eh->h_proto == htons(ETH_P_8021Q)) {
  2220. memmove((u8 *)eh + VLAN_HLEN, eh, ETH_ALEN * 2);
  2221. eh = skb_pull(skb, VLAN_HLEN);
  2222. skb_reset_mac_header(skb);
  2223. }
  2224. /*
  2225. * Process either a FIP frame or FCoE frame based on the
  2226. * protocol value. If it's not either just drop the
  2227. * frame.
  2228. */
  2229. if (eh->h_proto == htons(ETH_P_FIP)) {
  2230. qedf_fip_recv(qedf, skb);
  2231. goto out;
  2232. } else if (eh->h_proto == htons(ETH_P_FCOE)) {
  2233. __skb_pull(skb, ETH_HLEN);
  2234. qedf_recv_frame(qedf, skb);
  2235. goto out;
  2236. } else
  2237. goto err_out;
  2238. err_out:
  2239. kfree_skb(skb);
  2240. out:
  2241. kfree(skb_work);
  2242. return;
  2243. }
  2244. static int qedf_ll2_rx(void *cookie, struct sk_buff *skb,
  2245. u32 arg1, u32 arg2)
  2246. {
  2247. struct qedf_ctx *qedf = (struct qedf_ctx *)cookie;
  2248. struct qedf_skb_work *skb_work;
  2249. if (atomic_read(&qedf->link_state) == QEDF_LINK_DOWN) {
  2250. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_LL2,
  2251. "Dropping frame as link state is down.\n");
  2252. kfree_skb(skb);
  2253. return 0;
  2254. }
  2255. skb_work = kzalloc_obj(struct qedf_skb_work, GFP_ATOMIC);
  2256. if (!skb_work) {
  2257. QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate skb_work so "
  2258. "dropping frame.\n");
  2259. kfree_skb(skb);
  2260. return 0;
  2261. }
  2262. INIT_WORK(&skb_work->work, qedf_ll2_process_skb);
  2263. skb_work->skb = skb;
  2264. skb_work->qedf = qedf;
  2265. queue_work(qedf->ll2_recv_wq, &skb_work->work);
  2266. return 0;
  2267. }
  2268. static struct qed_ll2_cb_ops qedf_ll2_cb_ops = {
  2269. .rx_cb = qedf_ll2_rx,
  2270. .tx_cb = NULL,
  2271. };
  2272. /* Main thread to process I/O completions */
  2273. void qedf_fp_io_handler(struct work_struct *work)
  2274. {
  2275. struct qedf_io_work *io_work =
  2276. container_of(work, struct qedf_io_work, work);
  2277. u32 comp_type;
  2278. /*
  2279. * Deferred part of unsolicited CQE sends
  2280. * frame to libfc.
  2281. */
  2282. comp_type = (io_work->cqe.cqe_data >>
  2283. FCOE_CQE_CQE_TYPE_SHIFT) &
  2284. FCOE_CQE_CQE_TYPE_MASK;
  2285. if (comp_type == FCOE_UNSOLIC_CQE_TYPE &&
  2286. io_work->fp)
  2287. fc_exch_recv(io_work->qedf->lport, io_work->fp);
  2288. else
  2289. qedf_process_cqe(io_work->qedf, &io_work->cqe);
  2290. kfree(io_work);
  2291. }
  2292. static int qedf_alloc_and_init_sb(struct qedf_ctx *qedf,
  2293. struct qed_sb_info *sb_info, u16 sb_id)
  2294. {
  2295. struct status_block *sb_virt;
  2296. dma_addr_t sb_phys;
  2297. int ret;
  2298. sb_virt = dma_alloc_coherent(&qedf->pdev->dev,
  2299. sizeof(struct status_block), &sb_phys, GFP_KERNEL);
  2300. if (!sb_virt) {
  2301. QEDF_ERR(&qedf->dbg_ctx,
  2302. "Status block allocation failed for id = %d.\n",
  2303. sb_id);
  2304. return -ENOMEM;
  2305. }
  2306. ret = qed_ops->common->sb_init(qedf->cdev, sb_info, sb_virt, sb_phys,
  2307. sb_id, QED_SB_TYPE_STORAGE);
  2308. if (ret) {
  2309. dma_free_coherent(&qedf->pdev->dev, sizeof(*sb_virt), sb_virt, sb_phys);
  2310. QEDF_ERR(&qedf->dbg_ctx,
  2311. "Status block initialization failed (0x%x) for id = %d.\n",
  2312. ret, sb_id);
  2313. return ret;
  2314. }
  2315. return 0;
  2316. }
  2317. static void qedf_free_sb(struct qedf_ctx *qedf, struct qed_sb_info *sb_info)
  2318. {
  2319. if (sb_info->sb_virt)
  2320. dma_free_coherent(&qedf->pdev->dev, sizeof(*sb_info->sb_virt),
  2321. (void *)sb_info->sb_virt, sb_info->sb_phys);
  2322. }
  2323. static void qedf_destroy_sb(struct qedf_ctx *qedf)
  2324. {
  2325. int id;
  2326. struct qedf_fastpath *fp = NULL;
  2327. for (id = 0; id < qedf->num_queues; id++) {
  2328. fp = &(qedf->fp_array[id]);
  2329. if (fp->sb_id == QEDF_SB_ID_NULL)
  2330. break;
  2331. qedf_free_sb(qedf, fp->sb_info);
  2332. kfree(fp->sb_info);
  2333. }
  2334. kfree(qedf->fp_array);
  2335. }
  2336. static int qedf_prepare_sb(struct qedf_ctx *qedf)
  2337. {
  2338. int id;
  2339. struct qedf_fastpath *fp;
  2340. int ret;
  2341. qedf->fp_array =
  2342. kzalloc_objs(struct qedf_fastpath, qedf->num_queues);
  2343. if (!qedf->fp_array) {
  2344. QEDF_ERR(&(qedf->dbg_ctx), "fastpath array allocation "
  2345. "failed.\n");
  2346. return -ENOMEM;
  2347. }
  2348. for (id = 0; id < qedf->num_queues; id++) {
  2349. fp = &(qedf->fp_array[id]);
  2350. fp->sb_id = QEDF_SB_ID_NULL;
  2351. fp->sb_info = kzalloc_objs(*fp->sb_info, 1);
  2352. if (!fp->sb_info) {
  2353. QEDF_ERR(&(qedf->dbg_ctx), "SB info struct "
  2354. "allocation failed.\n");
  2355. goto err;
  2356. }
  2357. ret = qedf_alloc_and_init_sb(qedf, fp->sb_info, id);
  2358. if (ret) {
  2359. QEDF_ERR(&(qedf->dbg_ctx), "SB allocation and "
  2360. "initialization failed.\n");
  2361. goto err;
  2362. }
  2363. fp->sb_id = id;
  2364. fp->qedf = qedf;
  2365. fp->cq_num_entries =
  2366. qedf->global_queues[id]->cq_mem_size /
  2367. sizeof(struct fcoe_cqe);
  2368. }
  2369. err:
  2370. return 0;
  2371. }
  2372. void qedf_process_cqe(struct qedf_ctx *qedf, struct fcoe_cqe *cqe)
  2373. {
  2374. u16 xid;
  2375. struct qedf_ioreq *io_req;
  2376. struct qedf_rport *fcport;
  2377. u32 comp_type;
  2378. u8 io_comp_type;
  2379. unsigned long flags;
  2380. comp_type = (cqe->cqe_data >> FCOE_CQE_CQE_TYPE_SHIFT) &
  2381. FCOE_CQE_CQE_TYPE_MASK;
  2382. xid = cqe->cqe_data & FCOE_CQE_TASK_ID_MASK;
  2383. io_req = &qedf->cmd_mgr->cmds[xid];
  2384. /* Completion not for a valid I/O anymore so just return */
  2385. if (!io_req) {
  2386. QEDF_ERR(&qedf->dbg_ctx,
  2387. "io_req is NULL for xid=0x%x.\n", xid);
  2388. return;
  2389. }
  2390. fcport = io_req->fcport;
  2391. if (fcport == NULL) {
  2392. QEDF_ERR(&qedf->dbg_ctx,
  2393. "fcport is NULL for xid=0x%x io_req=%p.\n",
  2394. xid, io_req);
  2395. return;
  2396. }
  2397. /*
  2398. * Check that fcport is offloaded. If it isn't then the spinlock
  2399. * isn't valid and shouldn't be taken. We should just return.
  2400. */
  2401. if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
  2402. QEDF_ERR(&qedf->dbg_ctx,
  2403. "Session not offloaded yet, fcport = %p.\n", fcport);
  2404. return;
  2405. }
  2406. spin_lock_irqsave(&fcport->rport_lock, flags);
  2407. io_comp_type = io_req->cmd_type;
  2408. spin_unlock_irqrestore(&fcport->rport_lock, flags);
  2409. switch (comp_type) {
  2410. case FCOE_GOOD_COMPLETION_CQE_TYPE:
  2411. atomic_inc(&fcport->free_sqes);
  2412. switch (io_comp_type) {
  2413. case QEDF_SCSI_CMD:
  2414. qedf_scsi_completion(qedf, cqe, io_req);
  2415. break;
  2416. case QEDF_ELS:
  2417. qedf_process_els_compl(qedf, cqe, io_req);
  2418. break;
  2419. case QEDF_TASK_MGMT_CMD:
  2420. qedf_process_tmf_compl(qedf, cqe, io_req);
  2421. break;
  2422. case QEDF_SEQ_CLEANUP:
  2423. qedf_process_seq_cleanup_compl(qedf, cqe, io_req);
  2424. break;
  2425. }
  2426. break;
  2427. case FCOE_ERROR_DETECTION_CQE_TYPE:
  2428. atomic_inc(&fcport->free_sqes);
  2429. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  2430. "Error detect CQE.\n");
  2431. qedf_process_error_detect(qedf, cqe, io_req);
  2432. break;
  2433. case FCOE_EXCH_CLEANUP_CQE_TYPE:
  2434. atomic_inc(&fcport->free_sqes);
  2435. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  2436. "Cleanup CQE.\n");
  2437. qedf_process_cleanup_compl(qedf, cqe, io_req);
  2438. break;
  2439. case FCOE_ABTS_CQE_TYPE:
  2440. atomic_inc(&fcport->free_sqes);
  2441. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  2442. "Abort CQE.\n");
  2443. qedf_process_abts_compl(qedf, cqe, io_req);
  2444. break;
  2445. case FCOE_DUMMY_CQE_TYPE:
  2446. atomic_inc(&fcport->free_sqes);
  2447. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  2448. "Dummy CQE.\n");
  2449. break;
  2450. case FCOE_LOCAL_COMP_CQE_TYPE:
  2451. atomic_inc(&fcport->free_sqes);
  2452. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  2453. "Local completion CQE.\n");
  2454. break;
  2455. case FCOE_WARNING_CQE_TYPE:
  2456. atomic_inc(&fcport->free_sqes);
  2457. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  2458. "Warning CQE.\n");
  2459. qedf_process_warning_compl(qedf, cqe, io_req);
  2460. break;
  2461. case MAX_FCOE_CQE_TYPE:
  2462. atomic_inc(&fcport->free_sqes);
  2463. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  2464. "Max FCoE CQE.\n");
  2465. break;
  2466. default:
  2467. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  2468. "Default CQE.\n");
  2469. break;
  2470. }
  2471. }
  2472. static void qedf_free_bdq(struct qedf_ctx *qedf)
  2473. {
  2474. int i;
  2475. if (qedf->bdq_pbl_list)
  2476. dma_free_coherent(&qedf->pdev->dev, QEDF_PAGE_SIZE,
  2477. qedf->bdq_pbl_list, qedf->bdq_pbl_list_dma);
  2478. if (qedf->bdq_pbl)
  2479. dma_free_coherent(&qedf->pdev->dev, qedf->bdq_pbl_mem_size,
  2480. qedf->bdq_pbl, qedf->bdq_pbl_dma);
  2481. for (i = 0; i < QEDF_BDQ_SIZE; i++) {
  2482. if (qedf->bdq[i].buf_addr) {
  2483. dma_free_coherent(&qedf->pdev->dev, QEDF_BDQ_BUF_SIZE,
  2484. qedf->bdq[i].buf_addr, qedf->bdq[i].buf_dma);
  2485. }
  2486. }
  2487. }
  2488. static void qedf_free_global_queues(struct qedf_ctx *qedf)
  2489. {
  2490. int i;
  2491. struct global_queue **gl = qedf->global_queues;
  2492. for (i = 0; i < qedf->num_queues; i++) {
  2493. if (!gl[i])
  2494. continue;
  2495. if (gl[i]->cq)
  2496. dma_free_coherent(&qedf->pdev->dev,
  2497. gl[i]->cq_mem_size, gl[i]->cq, gl[i]->cq_dma);
  2498. if (gl[i]->cq_pbl)
  2499. dma_free_coherent(&qedf->pdev->dev, gl[i]->cq_pbl_size,
  2500. gl[i]->cq_pbl, gl[i]->cq_pbl_dma);
  2501. kfree(gl[i]);
  2502. }
  2503. qedf_free_bdq(qedf);
  2504. }
  2505. static int qedf_alloc_bdq(struct qedf_ctx *qedf)
  2506. {
  2507. int i;
  2508. struct scsi_bd *pbl;
  2509. u64 *list;
  2510. /* Alloc dma memory for BDQ buffers */
  2511. for (i = 0; i < QEDF_BDQ_SIZE; i++) {
  2512. qedf->bdq[i].buf_addr = dma_alloc_coherent(&qedf->pdev->dev,
  2513. QEDF_BDQ_BUF_SIZE, &qedf->bdq[i].buf_dma, GFP_KERNEL);
  2514. if (!qedf->bdq[i].buf_addr) {
  2515. QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate BDQ "
  2516. "buffer %d.\n", i);
  2517. return -ENOMEM;
  2518. }
  2519. }
  2520. /* Alloc dma memory for BDQ page buffer list */
  2521. qedf->bdq_pbl_mem_size =
  2522. QEDF_BDQ_SIZE * sizeof(struct scsi_bd);
  2523. qedf->bdq_pbl_mem_size =
  2524. ALIGN(qedf->bdq_pbl_mem_size, QEDF_PAGE_SIZE);
  2525. qedf->bdq_pbl = dma_alloc_coherent(&qedf->pdev->dev,
  2526. qedf->bdq_pbl_mem_size, &qedf->bdq_pbl_dma, GFP_KERNEL);
  2527. if (!qedf->bdq_pbl) {
  2528. QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate BDQ PBL.\n");
  2529. return -ENOMEM;
  2530. }
  2531. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  2532. "BDQ PBL addr=0x%p dma=%pad\n",
  2533. qedf->bdq_pbl, &qedf->bdq_pbl_dma);
  2534. /*
  2535. * Populate BDQ PBL with physical and virtual address of individual
  2536. * BDQ buffers
  2537. */
  2538. pbl = (struct scsi_bd *)qedf->bdq_pbl;
  2539. for (i = 0; i < QEDF_BDQ_SIZE; i++) {
  2540. pbl->address.hi = cpu_to_le32(U64_HI(qedf->bdq[i].buf_dma));
  2541. pbl->address.lo = cpu_to_le32(U64_LO(qedf->bdq[i].buf_dma));
  2542. pbl->opaque.fcoe_opaque.hi = 0;
  2543. /* Opaque lo data is an index into the BDQ array */
  2544. pbl->opaque.fcoe_opaque.lo = cpu_to_le32(i);
  2545. pbl++;
  2546. }
  2547. /* Allocate list of PBL pages */
  2548. qedf->bdq_pbl_list = dma_alloc_coherent(&qedf->pdev->dev,
  2549. QEDF_PAGE_SIZE,
  2550. &qedf->bdq_pbl_list_dma,
  2551. GFP_KERNEL);
  2552. if (!qedf->bdq_pbl_list) {
  2553. QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate list of PBL pages.\n");
  2554. return -ENOMEM;
  2555. }
  2556. /*
  2557. * Now populate PBL list with pages that contain pointers to the
  2558. * individual buffers.
  2559. */
  2560. qedf->bdq_pbl_list_num_entries = qedf->bdq_pbl_mem_size /
  2561. QEDF_PAGE_SIZE;
  2562. list = (u64 *)qedf->bdq_pbl_list;
  2563. for (i = 0; i < qedf->bdq_pbl_list_num_entries; i++) {
  2564. *list = qedf->bdq_pbl_dma;
  2565. list++;
  2566. }
  2567. return 0;
  2568. }
  2569. static int qedf_alloc_global_queues(struct qedf_ctx *qedf)
  2570. {
  2571. u32 *list;
  2572. int i;
  2573. int status;
  2574. u32 *pbl;
  2575. dma_addr_t page;
  2576. int num_pages;
  2577. /* Allocate and map CQs, RQs */
  2578. /*
  2579. * Number of global queues (CQ / RQ). This should
  2580. * be <= number of available MSIX vectors for the PF
  2581. */
  2582. if (!qedf->num_queues) {
  2583. QEDF_ERR(&(qedf->dbg_ctx), "No MSI-X vectors available!\n");
  2584. return -ENOMEM;
  2585. }
  2586. /*
  2587. * Make sure we allocated the PBL that will contain the physical
  2588. * addresses of our queues
  2589. */
  2590. if (!qedf->p_cpuq) {
  2591. QEDF_ERR(&qedf->dbg_ctx, "p_cpuq is NULL.\n");
  2592. return -EINVAL;
  2593. }
  2594. qedf->global_queues = kzalloc((sizeof(struct global_queue *)
  2595. * qedf->num_queues), GFP_KERNEL);
  2596. if (!qedf->global_queues) {
  2597. QEDF_ERR(&(qedf->dbg_ctx), "Unable to allocate global "
  2598. "queues array ptr memory\n");
  2599. return -ENOMEM;
  2600. }
  2601. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  2602. "qedf->global_queues=%p.\n", qedf->global_queues);
  2603. /* Allocate DMA coherent buffers for BDQ */
  2604. status = qedf_alloc_bdq(qedf);
  2605. if (status) {
  2606. QEDF_ERR(&qedf->dbg_ctx, "Unable to allocate bdq.\n");
  2607. goto mem_alloc_failure;
  2608. }
  2609. /* Allocate a CQ and an associated PBL for each MSI-X vector */
  2610. for (i = 0; i < qedf->num_queues; i++) {
  2611. qedf->global_queues[i] = kzalloc_obj(struct global_queue);
  2612. if (!qedf->global_queues[i]) {
  2613. QEDF_WARN(&(qedf->dbg_ctx), "Unable to allocate "
  2614. "global queue %d.\n", i);
  2615. status = -ENOMEM;
  2616. goto mem_alloc_failure;
  2617. }
  2618. qedf->global_queues[i]->cq_mem_size =
  2619. FCOE_PARAMS_CQ_NUM_ENTRIES * sizeof(struct fcoe_cqe);
  2620. qedf->global_queues[i]->cq_mem_size =
  2621. ALIGN(qedf->global_queues[i]->cq_mem_size, QEDF_PAGE_SIZE);
  2622. qedf->global_queues[i]->cq_pbl_size =
  2623. (qedf->global_queues[i]->cq_mem_size /
  2624. PAGE_SIZE) * sizeof(void *);
  2625. qedf->global_queues[i]->cq_pbl_size =
  2626. ALIGN(qedf->global_queues[i]->cq_pbl_size, QEDF_PAGE_SIZE);
  2627. qedf->global_queues[i]->cq =
  2628. dma_alloc_coherent(&qedf->pdev->dev,
  2629. qedf->global_queues[i]->cq_mem_size,
  2630. &qedf->global_queues[i]->cq_dma,
  2631. GFP_KERNEL);
  2632. if (!qedf->global_queues[i]->cq) {
  2633. QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate cq.\n");
  2634. status = -ENOMEM;
  2635. goto mem_alloc_failure;
  2636. }
  2637. qedf->global_queues[i]->cq_pbl =
  2638. dma_alloc_coherent(&qedf->pdev->dev,
  2639. qedf->global_queues[i]->cq_pbl_size,
  2640. &qedf->global_queues[i]->cq_pbl_dma,
  2641. GFP_KERNEL);
  2642. if (!qedf->global_queues[i]->cq_pbl) {
  2643. QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate cq PBL.\n");
  2644. status = -ENOMEM;
  2645. goto mem_alloc_failure;
  2646. }
  2647. /* Create PBL */
  2648. num_pages = qedf->global_queues[i]->cq_mem_size /
  2649. QEDF_PAGE_SIZE;
  2650. page = qedf->global_queues[i]->cq_dma;
  2651. pbl = (u32 *)qedf->global_queues[i]->cq_pbl;
  2652. while (num_pages--) {
  2653. *pbl = U64_LO(page);
  2654. pbl++;
  2655. *pbl = U64_HI(page);
  2656. pbl++;
  2657. page += QEDF_PAGE_SIZE;
  2658. }
  2659. /* Set the initial consumer index for cq */
  2660. qedf->global_queues[i]->cq_cons_idx = 0;
  2661. }
  2662. list = (u32 *)qedf->p_cpuq;
  2663. /*
  2664. * The list is built as follows: CQ#0 PBL pointer, RQ#0 PBL pointer,
  2665. * CQ#1 PBL pointer, RQ#1 PBL pointer, etc. Each PBL pointer points
  2666. * to the physical address which contains an array of pointers to
  2667. * the physical addresses of the specific queue pages.
  2668. */
  2669. for (i = 0; i < qedf->num_queues; i++) {
  2670. *list = U64_LO(qedf->global_queues[i]->cq_pbl_dma);
  2671. list++;
  2672. *list = U64_HI(qedf->global_queues[i]->cq_pbl_dma);
  2673. list++;
  2674. *list = U64_LO(0);
  2675. list++;
  2676. *list = U64_HI(0);
  2677. list++;
  2678. }
  2679. return 0;
  2680. mem_alloc_failure:
  2681. qedf_free_global_queues(qedf);
  2682. return status;
  2683. }
  2684. static int qedf_set_fcoe_pf_param(struct qedf_ctx *qedf)
  2685. {
  2686. u8 sq_num_pbl_pages;
  2687. u32 sq_mem_size;
  2688. u32 cq_mem_size;
  2689. u32 cq_num_entries;
  2690. int rval;
  2691. /*
  2692. * The number of completion queues/fastpath interrupts/status blocks
  2693. * we allocation is the minimum off:
  2694. *
  2695. * Number of CPUs
  2696. * Number allocated by qed for our PCI function
  2697. */
  2698. qedf->num_queues = MIN_NUM_CPUS_MSIX(qedf);
  2699. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Number of CQs is %d.\n",
  2700. qedf->num_queues);
  2701. qedf->p_cpuq = dma_alloc_coherent(&qedf->pdev->dev,
  2702. qedf->num_queues * sizeof(struct qedf_glbl_q_params),
  2703. &qedf->hw_p_cpuq, GFP_KERNEL);
  2704. if (!qedf->p_cpuq) {
  2705. QEDF_ERR(&(qedf->dbg_ctx), "dma_alloc_coherent failed.\n");
  2706. return 1;
  2707. }
  2708. rval = qedf_alloc_global_queues(qedf);
  2709. if (rval) {
  2710. QEDF_ERR(&(qedf->dbg_ctx), "Global queue allocation "
  2711. "failed.\n");
  2712. return 1;
  2713. }
  2714. /* Calculate SQ PBL size in the same manner as in qedf_sq_alloc() */
  2715. sq_mem_size = SQ_NUM_ENTRIES * sizeof(struct fcoe_wqe);
  2716. sq_mem_size = ALIGN(sq_mem_size, QEDF_PAGE_SIZE);
  2717. sq_num_pbl_pages = (sq_mem_size / QEDF_PAGE_SIZE);
  2718. /* Calculate CQ num entries */
  2719. cq_mem_size = FCOE_PARAMS_CQ_NUM_ENTRIES * sizeof(struct fcoe_cqe);
  2720. cq_mem_size = ALIGN(cq_mem_size, QEDF_PAGE_SIZE);
  2721. cq_num_entries = cq_mem_size / sizeof(struct fcoe_cqe);
  2722. memset(&(qedf->pf_params), 0, sizeof(qedf->pf_params));
  2723. /* Setup the value for fcoe PF */
  2724. qedf->pf_params.fcoe_pf_params.num_cons = QEDF_MAX_SESSIONS;
  2725. qedf->pf_params.fcoe_pf_params.num_tasks = FCOE_PARAMS_NUM_TASKS;
  2726. qedf->pf_params.fcoe_pf_params.glbl_q_params_addr =
  2727. (u64)qedf->hw_p_cpuq;
  2728. qedf->pf_params.fcoe_pf_params.sq_num_pbl_pages = sq_num_pbl_pages;
  2729. qedf->pf_params.fcoe_pf_params.rq_buffer_log_size = 0;
  2730. qedf->pf_params.fcoe_pf_params.cq_num_entries = cq_num_entries;
  2731. qedf->pf_params.fcoe_pf_params.num_cqs = qedf->num_queues;
  2732. /* log_page_size: 12 for 4KB pages */
  2733. qedf->pf_params.fcoe_pf_params.log_page_size = ilog2(QEDF_PAGE_SIZE);
  2734. qedf->pf_params.fcoe_pf_params.mtu = 9000;
  2735. qedf->pf_params.fcoe_pf_params.gl_rq_pi = QEDF_FCOE_PARAMS_GL_RQ_PI;
  2736. qedf->pf_params.fcoe_pf_params.gl_cmd_pi = QEDF_FCOE_PARAMS_GL_CMD_PI;
  2737. /* BDQ address and size */
  2738. qedf->pf_params.fcoe_pf_params.bdq_pbl_base_addr[0] =
  2739. qedf->bdq_pbl_list_dma;
  2740. qedf->pf_params.fcoe_pf_params.bdq_pbl_num_entries[0] =
  2741. qedf->bdq_pbl_list_num_entries;
  2742. qedf->pf_params.fcoe_pf_params.rq_buffer_size = QEDF_BDQ_BUF_SIZE;
  2743. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  2744. "bdq_list=%p bdq_pbl_list_dma=%llx bdq_pbl_list_entries=%d.\n",
  2745. qedf->bdq_pbl_list,
  2746. qedf->pf_params.fcoe_pf_params.bdq_pbl_base_addr[0],
  2747. qedf->pf_params.fcoe_pf_params.bdq_pbl_num_entries[0]);
  2748. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  2749. "cq_num_entries=%d.\n",
  2750. qedf->pf_params.fcoe_pf_params.cq_num_entries);
  2751. return 0;
  2752. }
  2753. /* Free DMA coherent memory for array of queue pointers we pass to qed */
  2754. static void qedf_free_fcoe_pf_param(struct qedf_ctx *qedf)
  2755. {
  2756. size_t size = 0;
  2757. if (qedf->p_cpuq) {
  2758. size = qedf->num_queues * sizeof(struct qedf_glbl_q_params);
  2759. dma_free_coherent(&qedf->pdev->dev, size, qedf->p_cpuq,
  2760. qedf->hw_p_cpuq);
  2761. }
  2762. qedf_free_global_queues(qedf);
  2763. kfree(qedf->global_queues);
  2764. }
  2765. /*
  2766. * PCI driver functions
  2767. */
  2768. static const struct pci_device_id qedf_pci_tbl[] = {
  2769. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x165c) },
  2770. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x8080) },
  2771. {0}
  2772. };
  2773. MODULE_DEVICE_TABLE(pci, qedf_pci_tbl);
  2774. static struct pci_driver qedf_pci_driver = {
  2775. .name = QEDF_MODULE_NAME,
  2776. .id_table = qedf_pci_tbl,
  2777. .probe = qedf_probe,
  2778. .remove = qedf_remove,
  2779. .shutdown = qedf_shutdown,
  2780. .suspend = qedf_suspend,
  2781. };
  2782. static int __qedf_probe(struct pci_dev *pdev, int mode)
  2783. {
  2784. int rc = -EINVAL;
  2785. struct fc_lport *lport;
  2786. struct qedf_ctx *qedf = NULL;
  2787. struct Scsi_Host *host;
  2788. bool is_vf = false;
  2789. struct qed_ll2_params params;
  2790. char host_buf[20];
  2791. struct qed_link_params link_params;
  2792. int status;
  2793. void *task_start, *task_end;
  2794. struct qed_slowpath_params slowpath_params;
  2795. struct qed_probe_params qed_params;
  2796. u16 retry_cnt = 10;
  2797. /*
  2798. * When doing error recovery we didn't reap the lport so don't try
  2799. * to reallocate it.
  2800. */
  2801. retry_probe:
  2802. if (mode == QEDF_MODE_RECOVERY)
  2803. msleep(2000);
  2804. if (mode != QEDF_MODE_RECOVERY) {
  2805. lport = libfc_host_alloc(&qedf_host_template,
  2806. sizeof(struct qedf_ctx));
  2807. if (!lport) {
  2808. QEDF_ERR(NULL, "Could not allocate lport.\n");
  2809. rc = -ENOMEM;
  2810. goto err0;
  2811. }
  2812. fc_disc_init(lport);
  2813. /* Initialize qedf_ctx */
  2814. qedf = lport_priv(lport);
  2815. set_bit(QEDF_PROBING, &qedf->flags);
  2816. qedf->lport = lport;
  2817. qedf->ctlr.lp = lport;
  2818. qedf->pdev = pdev;
  2819. qedf->dbg_ctx.pdev = pdev;
  2820. qedf->dbg_ctx.host_no = lport->host->host_no;
  2821. spin_lock_init(&qedf->hba_lock);
  2822. INIT_LIST_HEAD(&qedf->fcports);
  2823. qedf->curr_conn_id = QEDF_MAX_SESSIONS - 1;
  2824. atomic_set(&qedf->num_offloads, 0);
  2825. qedf->stop_io_on_error = false;
  2826. pci_set_drvdata(pdev, qedf);
  2827. init_completion(&qedf->fipvlan_compl);
  2828. mutex_init(&qedf->stats_mutex);
  2829. mutex_init(&qedf->flush_mutex);
  2830. qedf->flogi_pending = 0;
  2831. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_INFO,
  2832. "QLogic FastLinQ FCoE Module qedf %s, "
  2833. "FW %d.%d.%d.%d\n", QEDF_VERSION,
  2834. FW_MAJOR_VERSION, FW_MINOR_VERSION, FW_REVISION_VERSION,
  2835. FW_ENGINEERING_VERSION);
  2836. } else {
  2837. /* Init pointers during recovery */
  2838. qedf = pci_get_drvdata(pdev);
  2839. set_bit(QEDF_PROBING, &qedf->flags);
  2840. lport = qedf->lport;
  2841. }
  2842. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC, "Probe started.\n");
  2843. host = lport->host;
  2844. /* Allocate mempool for qedf_io_work structs */
  2845. qedf->io_mempool = mempool_create_slab_pool(QEDF_IO_WORK_MIN,
  2846. qedf_io_work_cache);
  2847. if (qedf->io_mempool == NULL) {
  2848. QEDF_ERR(&(qedf->dbg_ctx), "qedf->io_mempool is NULL.\n");
  2849. goto err1;
  2850. }
  2851. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_INFO, "qedf->io_mempool=%p.\n",
  2852. qedf->io_mempool);
  2853. qedf->link_update_wq = alloc_workqueue("qedf_%u_link",
  2854. WQ_MEM_RECLAIM | WQ_PERCPU,
  2855. 1, qedf->lport->host->host_no);
  2856. INIT_DELAYED_WORK(&qedf->link_update, qedf_handle_link_update);
  2857. INIT_DELAYED_WORK(&qedf->link_recovery, qedf_link_recovery);
  2858. INIT_DELAYED_WORK(&qedf->grcdump_work, qedf_wq_grcdump);
  2859. INIT_DELAYED_WORK(&qedf->stag_work, qedf_stag_change_work);
  2860. qedf->fipvlan_retries = qedf_fipvlan_retries;
  2861. /* Set a default prio in case DCBX doesn't converge */
  2862. if (qedf_default_prio > -1) {
  2863. /*
  2864. * This is the case where we pass a modparam in so we want to
  2865. * honor it even if dcbx doesn't converge.
  2866. */
  2867. qedf->prio = qedf_default_prio;
  2868. } else
  2869. qedf->prio = QEDF_DEFAULT_PRIO;
  2870. /*
  2871. * Common probe. Takes care of basic hardware init and pci_*
  2872. * functions.
  2873. */
  2874. memset(&qed_params, 0, sizeof(qed_params));
  2875. qed_params.protocol = QED_PROTOCOL_FCOE;
  2876. qed_params.dp_module = qedf_dp_module;
  2877. qed_params.dp_level = qedf_dp_level;
  2878. qed_params.is_vf = is_vf;
  2879. qedf->cdev = qed_ops->common->probe(pdev, &qed_params);
  2880. if (!qedf->cdev) {
  2881. if ((mode == QEDF_MODE_RECOVERY) && retry_cnt) {
  2882. QEDF_ERR(&qedf->dbg_ctx,
  2883. "Retry %d initialize hardware\n", retry_cnt);
  2884. retry_cnt--;
  2885. goto retry_probe;
  2886. }
  2887. QEDF_ERR(&qedf->dbg_ctx, "common probe failed.\n");
  2888. rc = -ENODEV;
  2889. goto err1;
  2890. }
  2891. /* Learn information crucial for qedf to progress */
  2892. rc = qed_ops->fill_dev_info(qedf->cdev, &qedf->dev_info);
  2893. if (rc) {
  2894. QEDF_ERR(&(qedf->dbg_ctx), "Failed to dev info.\n");
  2895. goto err1;
  2896. }
  2897. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC,
  2898. "dev_info: num_hwfns=%d affin_hwfn_idx=%d.\n",
  2899. qedf->dev_info.common.num_hwfns,
  2900. qed_ops->common->get_affin_hwfn_idx(qedf->cdev));
  2901. /* queue allocation code should come here
  2902. * order should be
  2903. * slowpath_start
  2904. * status block allocation
  2905. * interrupt registration (to get min number of queues)
  2906. * set_fcoe_pf_param
  2907. * qed_sp_fcoe_func_start
  2908. */
  2909. rc = qedf_set_fcoe_pf_param(qedf);
  2910. if (rc) {
  2911. QEDF_ERR(&(qedf->dbg_ctx), "Cannot set fcoe pf param.\n");
  2912. goto err2;
  2913. }
  2914. qed_ops->common->update_pf_params(qedf->cdev, &qedf->pf_params);
  2915. /* Learn information crucial for qedf to progress */
  2916. rc = qed_ops->fill_dev_info(qedf->cdev, &qedf->dev_info);
  2917. if (rc) {
  2918. QEDF_ERR(&qedf->dbg_ctx, "Failed to fill dev info.\n");
  2919. goto err2;
  2920. }
  2921. if (mode != QEDF_MODE_RECOVERY) {
  2922. qedf->devlink = qed_ops->common->devlink_register(qedf->cdev);
  2923. if (IS_ERR(qedf->devlink)) {
  2924. QEDF_ERR(&qedf->dbg_ctx, "Cannot register devlink\n");
  2925. rc = PTR_ERR(qedf->devlink);
  2926. qedf->devlink = NULL;
  2927. goto err2;
  2928. }
  2929. }
  2930. /* Record BDQ producer doorbell addresses */
  2931. qedf->bdq_primary_prod = qedf->dev_info.primary_dbq_rq_addr;
  2932. qedf->bdq_secondary_prod = qedf->dev_info.secondary_bdq_rq_addr;
  2933. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  2934. "BDQ primary_prod=%p secondary_prod=%p.\n", qedf->bdq_primary_prod,
  2935. qedf->bdq_secondary_prod);
  2936. qed_ops->register_ops(qedf->cdev, &qedf_cb_ops, qedf);
  2937. rc = qedf_prepare_sb(qedf);
  2938. if (rc) {
  2939. QEDF_ERR(&(qedf->dbg_ctx), "Cannot start slowpath.\n");
  2940. goto err2;
  2941. }
  2942. /* Start the Slowpath-process */
  2943. memset(&slowpath_params, 0, sizeof(struct qed_slowpath_params));
  2944. slowpath_params.int_mode = QED_INT_MODE_MSIX;
  2945. slowpath_params.drv_major = QEDF_DRIVER_MAJOR_VER;
  2946. slowpath_params.drv_minor = QEDF_DRIVER_MINOR_VER;
  2947. slowpath_params.drv_rev = QEDF_DRIVER_REV_VER;
  2948. slowpath_params.drv_eng = QEDF_DRIVER_ENG_VER;
  2949. strscpy(slowpath_params.name, "qedf", sizeof(slowpath_params.name));
  2950. rc = qed_ops->common->slowpath_start(qedf->cdev, &slowpath_params);
  2951. if (rc) {
  2952. QEDF_ERR(&(qedf->dbg_ctx), "Cannot start slowpath.\n");
  2953. goto err2;
  2954. }
  2955. /*
  2956. * update_pf_params needs to be called before and after slowpath
  2957. * start
  2958. */
  2959. qed_ops->common->update_pf_params(qedf->cdev, &qedf->pf_params);
  2960. /* Setup interrupts */
  2961. rc = qedf_setup_int(qedf);
  2962. if (rc) {
  2963. QEDF_ERR(&qedf->dbg_ctx, "Setup interrupts failed.\n");
  2964. goto err3;
  2965. }
  2966. rc = qed_ops->start(qedf->cdev, &qedf->tasks);
  2967. if (rc) {
  2968. QEDF_ERR(&(qedf->dbg_ctx), "Cannot start FCoE function.\n");
  2969. goto err4;
  2970. }
  2971. task_start = qedf_get_task_mem(&qedf->tasks, 0);
  2972. task_end = qedf_get_task_mem(&qedf->tasks, MAX_TID_BLOCKS_FCOE - 1);
  2973. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Task context start=%p, "
  2974. "end=%p block_size=%u.\n", task_start, task_end,
  2975. qedf->tasks.size);
  2976. /*
  2977. * We need to write the number of BDs in the BDQ we've preallocated so
  2978. * the f/w will do a prefetch and we'll get an unsolicited CQE when a
  2979. * packet arrives.
  2980. */
  2981. qedf->bdq_prod_idx = QEDF_BDQ_SIZE;
  2982. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  2983. "Writing %d to primary and secondary BDQ doorbell registers.\n",
  2984. qedf->bdq_prod_idx);
  2985. writew(qedf->bdq_prod_idx, qedf->bdq_primary_prod);
  2986. readw(qedf->bdq_primary_prod);
  2987. writew(qedf->bdq_prod_idx, qedf->bdq_secondary_prod);
  2988. readw(qedf->bdq_secondary_prod);
  2989. qed_ops->common->set_power_state(qedf->cdev, PCI_D0);
  2990. /* Now that the dev_info struct has been filled in set the MAC
  2991. * address
  2992. */
  2993. ether_addr_copy(qedf->mac, qedf->dev_info.common.hw_mac);
  2994. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "MAC address is %pM.\n",
  2995. qedf->mac);
  2996. /*
  2997. * Set the WWNN and WWPN in the following way:
  2998. *
  2999. * If the info we get from qed is non-zero then use that to set the
  3000. * WWPN and WWNN. Otherwise fall back to use fcoe_wwn_from_mac() based
  3001. * on the MAC address.
  3002. */
  3003. if (qedf->dev_info.wwnn != 0 && qedf->dev_info.wwpn != 0) {
  3004. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  3005. "Setting WWPN and WWNN from qed dev_info.\n");
  3006. qedf->wwnn = qedf->dev_info.wwnn;
  3007. qedf->wwpn = qedf->dev_info.wwpn;
  3008. } else {
  3009. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  3010. "Setting WWPN and WWNN using fcoe_wwn_from_mac().\n");
  3011. qedf->wwnn = fcoe_wwn_from_mac(qedf->mac, 1, 0);
  3012. qedf->wwpn = fcoe_wwn_from_mac(qedf->mac, 2, 0);
  3013. }
  3014. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "WWNN=%016llx "
  3015. "WWPN=%016llx.\n", qedf->wwnn, qedf->wwpn);
  3016. sprintf(host_buf, "host_%d", host->host_no);
  3017. qed_ops->common->set_name(qedf->cdev, host_buf);
  3018. /* Allocate cmd mgr */
  3019. qedf->cmd_mgr = qedf_cmd_mgr_alloc(qedf);
  3020. if (!qedf->cmd_mgr) {
  3021. QEDF_ERR(&(qedf->dbg_ctx), "Failed to allocate cmd mgr.\n");
  3022. rc = -ENOMEM;
  3023. goto err5;
  3024. }
  3025. if (mode != QEDF_MODE_RECOVERY) {
  3026. host->transportt = qedf_fc_transport_template;
  3027. host->max_lun = qedf_max_lun;
  3028. host->max_cmd_len = QEDF_MAX_CDB_LEN;
  3029. host->max_id = QEDF_MAX_SESSIONS;
  3030. host->can_queue = FCOE_PARAMS_NUM_TASKS;
  3031. rc = scsi_add_host(host, &pdev->dev);
  3032. if (rc) {
  3033. QEDF_WARN(&qedf->dbg_ctx,
  3034. "Error adding Scsi_Host rc=0x%x.\n", rc);
  3035. goto err6;
  3036. }
  3037. }
  3038. memset(&params, 0, sizeof(params));
  3039. params.mtu = QEDF_LL2_BUF_SIZE;
  3040. ether_addr_copy(params.ll2_mac_address, qedf->mac);
  3041. /* Start LL2 processing thread */
  3042. qedf->ll2_recv_wq = alloc_workqueue("qedf_%d_ll2",
  3043. WQ_MEM_RECLAIM | WQ_PERCPU, 1,
  3044. host->host_no);
  3045. if (!qedf->ll2_recv_wq) {
  3046. QEDF_ERR(&(qedf->dbg_ctx), "Failed to LL2 workqueue.\n");
  3047. rc = -ENOMEM;
  3048. goto err7;
  3049. }
  3050. #ifdef CONFIG_DEBUG_FS
  3051. qedf_dbg_host_init(&(qedf->dbg_ctx), qedf_debugfs_ops,
  3052. qedf_dbg_fops);
  3053. #endif
  3054. /* Start LL2 */
  3055. qed_ops->ll2->register_cb_ops(qedf->cdev, &qedf_ll2_cb_ops, qedf);
  3056. rc = qed_ops->ll2->start(qedf->cdev, &params);
  3057. if (rc) {
  3058. QEDF_ERR(&(qedf->dbg_ctx), "Could not start Light L2.\n");
  3059. goto err7;
  3060. }
  3061. set_bit(QEDF_LL2_STARTED, &qedf->flags);
  3062. /* Set initial FIP/FCoE VLAN to NULL */
  3063. qedf->vlan_id = 0;
  3064. /*
  3065. * No need to setup fcoe_ctlr or fc_lport objects during recovery since
  3066. * they were not reaped during the unload process.
  3067. */
  3068. if (mode != QEDF_MODE_RECOVERY) {
  3069. /* Setup imbedded fcoe controller */
  3070. qedf_fcoe_ctlr_setup(qedf);
  3071. /* Setup lport */
  3072. rc = qedf_lport_setup(qedf);
  3073. if (rc) {
  3074. QEDF_ERR(&(qedf->dbg_ctx),
  3075. "qedf_lport_setup failed.\n");
  3076. goto err7;
  3077. }
  3078. }
  3079. qedf->timer_work_queue = alloc_workqueue("qedf_%u_timer",
  3080. WQ_MEM_RECLAIM | WQ_PERCPU, 1,
  3081. qedf->lport->host->host_no);
  3082. if (!qedf->timer_work_queue) {
  3083. QEDF_ERR(&(qedf->dbg_ctx), "Failed to start timer "
  3084. "workqueue.\n");
  3085. rc = -ENOMEM;
  3086. goto err7;
  3087. }
  3088. /* DPC workqueue is not reaped during recovery unload */
  3089. if (mode != QEDF_MODE_RECOVERY) {
  3090. sprintf(host_buf, "qedf_%u_dpc",
  3091. qedf->lport->host->host_no);
  3092. qedf->dpc_wq =
  3093. alloc_workqueue("%s", WQ_MEM_RECLAIM | WQ_PERCPU, 1,
  3094. host_buf);
  3095. }
  3096. INIT_DELAYED_WORK(&qedf->recovery_work, qedf_recovery_handler);
  3097. /*
  3098. * GRC dump and sysfs parameters are not reaped during the recovery
  3099. * unload process.
  3100. */
  3101. if (mode != QEDF_MODE_RECOVERY) {
  3102. qedf->grcdump_size =
  3103. qed_ops->common->dbg_all_data_size(qedf->cdev);
  3104. if (qedf->grcdump_size) {
  3105. rc = qedf_alloc_grc_dump_buf(&qedf->grcdump,
  3106. qedf->grcdump_size);
  3107. if (rc) {
  3108. QEDF_ERR(&(qedf->dbg_ctx),
  3109. "GRC Dump buffer alloc failed.\n");
  3110. qedf->grcdump = NULL;
  3111. }
  3112. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  3113. "grcdump: addr=%p, size=%u.\n",
  3114. qedf->grcdump, qedf->grcdump_size);
  3115. }
  3116. qedf_create_sysfs_ctx_attr(qedf);
  3117. /* Initialize I/O tracing for this adapter */
  3118. spin_lock_init(&qedf->io_trace_lock);
  3119. qedf->io_trace_idx = 0;
  3120. }
  3121. init_completion(&qedf->flogi_compl);
  3122. status = qed_ops->common->update_drv_state(qedf->cdev, true);
  3123. if (status)
  3124. QEDF_ERR(&(qedf->dbg_ctx),
  3125. "Failed to send drv state to MFW.\n");
  3126. memset(&link_params, 0, sizeof(struct qed_link_params));
  3127. link_params.link_up = true;
  3128. status = qed_ops->common->set_link(qedf->cdev, &link_params);
  3129. if (status)
  3130. QEDF_WARN(&(qedf->dbg_ctx), "set_link failed.\n");
  3131. /* Start/restart discovery */
  3132. if (mode == QEDF_MODE_RECOVERY)
  3133. fcoe_ctlr_link_up(&qedf->ctlr);
  3134. else
  3135. fc_fabric_login(lport);
  3136. QEDF_INFO(&qedf->dbg_ctx, QEDF_LOG_DISC, "Probe done.\n");
  3137. clear_bit(QEDF_PROBING, &qedf->flags);
  3138. /* All good */
  3139. return 0;
  3140. err7:
  3141. if (qedf->ll2_recv_wq)
  3142. destroy_workqueue(qedf->ll2_recv_wq);
  3143. fc_remove_host(qedf->lport->host);
  3144. scsi_remove_host(qedf->lport->host);
  3145. #ifdef CONFIG_DEBUG_FS
  3146. qedf_dbg_host_exit(&(qedf->dbg_ctx));
  3147. #endif
  3148. err6:
  3149. qedf_cmd_mgr_free(qedf->cmd_mgr);
  3150. err5:
  3151. qed_ops->stop(qedf->cdev);
  3152. err4:
  3153. qedf_free_fcoe_pf_param(qedf);
  3154. qedf_sync_free_irqs(qedf);
  3155. err3:
  3156. qed_ops->common->slowpath_stop(qedf->cdev);
  3157. err2:
  3158. qed_ops->common->remove(qedf->cdev);
  3159. err1:
  3160. scsi_host_put(lport->host);
  3161. err0:
  3162. return rc;
  3163. }
  3164. static int qedf_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  3165. {
  3166. return __qedf_probe(pdev, QEDF_MODE_NORMAL);
  3167. }
  3168. static void __qedf_remove(struct pci_dev *pdev, int mode)
  3169. {
  3170. struct qedf_ctx *qedf;
  3171. int rc;
  3172. int cnt = 0;
  3173. if (!pdev) {
  3174. QEDF_ERR(NULL, "pdev is NULL.\n");
  3175. return;
  3176. }
  3177. qedf = pci_get_drvdata(pdev);
  3178. /*
  3179. * Prevent race where we're in board disable work and then try to
  3180. * rmmod the module.
  3181. */
  3182. if (test_bit(QEDF_UNLOADING, &qedf->flags)) {
  3183. QEDF_ERR(&qedf->dbg_ctx, "Already removing PCI function.\n");
  3184. return;
  3185. }
  3186. stag_in_prog:
  3187. if (test_bit(QEDF_STAG_IN_PROGRESS, &qedf->flags)) {
  3188. QEDF_ERR(&qedf->dbg_ctx, "Stag in progress, cnt=%d.\n", cnt);
  3189. cnt++;
  3190. if (cnt < 5) {
  3191. msleep(500);
  3192. goto stag_in_prog;
  3193. }
  3194. }
  3195. if (mode != QEDF_MODE_RECOVERY)
  3196. set_bit(QEDF_UNLOADING, &qedf->flags);
  3197. /* Logoff the fabric to upload all connections */
  3198. if (mode == QEDF_MODE_RECOVERY)
  3199. fcoe_ctlr_link_down(&qedf->ctlr);
  3200. else
  3201. fc_fabric_logoff(qedf->lport);
  3202. if (!qedf_wait_for_upload(qedf))
  3203. QEDF_ERR(&qedf->dbg_ctx, "Could not upload all sessions.\n");
  3204. #ifdef CONFIG_DEBUG_FS
  3205. qedf_dbg_host_exit(&(qedf->dbg_ctx));
  3206. #endif
  3207. /* Stop any link update handling */
  3208. cancel_delayed_work_sync(&qedf->link_update);
  3209. destroy_workqueue(qedf->link_update_wq);
  3210. qedf->link_update_wq = NULL;
  3211. if (qedf->timer_work_queue)
  3212. destroy_workqueue(qedf->timer_work_queue);
  3213. /* Stop Light L2 */
  3214. clear_bit(QEDF_LL2_STARTED, &qedf->flags);
  3215. qed_ops->ll2->stop(qedf->cdev);
  3216. if (qedf->ll2_recv_wq)
  3217. destroy_workqueue(qedf->ll2_recv_wq);
  3218. /* Stop fastpath */
  3219. qedf_sync_free_irqs(qedf);
  3220. qedf_destroy_sb(qedf);
  3221. /*
  3222. * During recovery don't destroy OS constructs that represent the
  3223. * physical port.
  3224. */
  3225. if (mode != QEDF_MODE_RECOVERY) {
  3226. qedf_free_grc_dump_buf(&qedf->grcdump);
  3227. qedf_remove_sysfs_ctx_attr(qedf);
  3228. /* Remove all SCSI/libfc/libfcoe structures */
  3229. fcoe_ctlr_destroy(&qedf->ctlr);
  3230. fc_lport_destroy(qedf->lport);
  3231. fc_remove_host(qedf->lport->host);
  3232. scsi_remove_host(qedf->lport->host);
  3233. }
  3234. qedf_cmd_mgr_free(qedf->cmd_mgr);
  3235. if (mode != QEDF_MODE_RECOVERY) {
  3236. fc_exch_mgr_free(qedf->lport);
  3237. fc_lport_free_stats(qedf->lport);
  3238. /* Wait for all vports to be reaped */
  3239. qedf_wait_for_vport_destroy(qedf);
  3240. }
  3241. /*
  3242. * Now that all connections have been uploaded we can stop the
  3243. * rest of the qed operations
  3244. */
  3245. qed_ops->stop(qedf->cdev);
  3246. if (mode != QEDF_MODE_RECOVERY) {
  3247. if (qedf->dpc_wq) {
  3248. /* Stop general DPC handling */
  3249. destroy_workqueue(qedf->dpc_wq);
  3250. qedf->dpc_wq = NULL;
  3251. }
  3252. }
  3253. /* Final shutdown for the board */
  3254. qedf_free_fcoe_pf_param(qedf);
  3255. if (mode != QEDF_MODE_RECOVERY) {
  3256. qed_ops->common->set_power_state(qedf->cdev, PCI_D0);
  3257. pci_set_drvdata(pdev, NULL);
  3258. }
  3259. rc = qed_ops->common->update_drv_state(qedf->cdev, false);
  3260. if (rc)
  3261. QEDF_ERR(&(qedf->dbg_ctx),
  3262. "Failed to send drv state to MFW.\n");
  3263. if (mode != QEDF_MODE_RECOVERY && qedf->devlink) {
  3264. qed_ops->common->devlink_unregister(qedf->devlink);
  3265. qedf->devlink = NULL;
  3266. }
  3267. qed_ops->common->slowpath_stop(qedf->cdev);
  3268. qed_ops->common->remove(qedf->cdev);
  3269. mempool_destroy(qedf->io_mempool);
  3270. /* Only reap the Scsi_host on a real removal */
  3271. if (mode != QEDF_MODE_RECOVERY)
  3272. scsi_host_put(qedf->lport->host);
  3273. }
  3274. static void qedf_remove(struct pci_dev *pdev)
  3275. {
  3276. /* Check to make sure this function wasn't already disabled */
  3277. if (!atomic_read(&pdev->enable_cnt))
  3278. return;
  3279. __qedf_remove(pdev, QEDF_MODE_NORMAL);
  3280. }
  3281. void qedf_wq_grcdump(struct work_struct *work)
  3282. {
  3283. struct qedf_ctx *qedf =
  3284. container_of(work, struct qedf_ctx, grcdump_work.work);
  3285. QEDF_ERR(&(qedf->dbg_ctx), "Collecting GRC dump.\n");
  3286. qedf_capture_grc_dump(qedf);
  3287. }
  3288. void qedf_schedule_hw_err_handler(void *dev, enum qed_hw_err_type err_type)
  3289. {
  3290. struct qedf_ctx *qedf = dev;
  3291. QEDF_ERR(&(qedf->dbg_ctx),
  3292. "Hardware error handler scheduled, event=%d.\n",
  3293. err_type);
  3294. if (test_bit(QEDF_IN_RECOVERY, &qedf->flags)) {
  3295. QEDF_ERR(&(qedf->dbg_ctx),
  3296. "Already in recovery, not scheduling board disable work.\n");
  3297. return;
  3298. }
  3299. switch (err_type) {
  3300. case QED_HW_ERR_FAN_FAIL:
  3301. schedule_delayed_work(&qedf->board_disable_work, 0);
  3302. break;
  3303. case QED_HW_ERR_MFW_RESP_FAIL:
  3304. case QED_HW_ERR_HW_ATTN:
  3305. case QED_HW_ERR_DMAE_FAIL:
  3306. case QED_HW_ERR_FW_ASSERT:
  3307. /* Prevent HW attentions from being reasserted */
  3308. qed_ops->common->attn_clr_enable(qedf->cdev, true);
  3309. break;
  3310. case QED_HW_ERR_RAMROD_FAIL:
  3311. /* Prevent HW attentions from being reasserted */
  3312. qed_ops->common->attn_clr_enable(qedf->cdev, true);
  3313. if (qedf_enable_recovery && qedf->devlink)
  3314. qed_ops->common->report_fatal_error(qedf->devlink,
  3315. err_type);
  3316. break;
  3317. default:
  3318. break;
  3319. }
  3320. }
  3321. /*
  3322. * Protocol TLV handler
  3323. */
  3324. void qedf_get_protocol_tlv_data(void *dev, void *data)
  3325. {
  3326. struct qedf_ctx *qedf = dev;
  3327. struct qed_mfw_tlv_fcoe *fcoe = data;
  3328. struct fc_lport *lport;
  3329. struct Scsi_Host *host;
  3330. struct fc_host_attrs *fc_host;
  3331. struct fc_host_statistics *hst;
  3332. if (!qedf) {
  3333. QEDF_ERR(NULL, "qedf is null.\n");
  3334. return;
  3335. }
  3336. if (test_bit(QEDF_PROBING, &qedf->flags)) {
  3337. QEDF_ERR(&qedf->dbg_ctx, "Function is still probing.\n");
  3338. return;
  3339. }
  3340. lport = qedf->lport;
  3341. host = lport->host;
  3342. fc_host = shost_to_fc_host(host);
  3343. /* Force a refresh of the fc_host stats including offload stats */
  3344. hst = qedf_fc_get_host_stats(host);
  3345. fcoe->qos_pri_set = true;
  3346. fcoe->qos_pri = 3; /* Hard coded to 3 in driver */
  3347. fcoe->ra_tov_set = true;
  3348. fcoe->ra_tov = lport->r_a_tov;
  3349. fcoe->ed_tov_set = true;
  3350. fcoe->ed_tov = lport->e_d_tov;
  3351. fcoe->npiv_state_set = true;
  3352. fcoe->npiv_state = 1; /* NPIV always enabled */
  3353. fcoe->num_npiv_ids_set = true;
  3354. fcoe->num_npiv_ids = fc_host->npiv_vports_inuse;
  3355. /* Certain attributes we only want to set if we've selected an FCF */
  3356. if (qedf->ctlr.sel_fcf) {
  3357. fcoe->switch_name_set = true;
  3358. u64_to_wwn(qedf->ctlr.sel_fcf->switch_name, fcoe->switch_name);
  3359. }
  3360. fcoe->port_state_set = true;
  3361. /* For qedf we're either link down or fabric attach */
  3362. if (lport->link_up)
  3363. fcoe->port_state = QED_MFW_TLV_PORT_STATE_FABRIC;
  3364. else
  3365. fcoe->port_state = QED_MFW_TLV_PORT_STATE_OFFLINE;
  3366. fcoe->link_failures_set = true;
  3367. fcoe->link_failures = (u16)hst->link_failure_count;
  3368. fcoe->fcoe_txq_depth_set = true;
  3369. fcoe->fcoe_rxq_depth_set = true;
  3370. fcoe->fcoe_rxq_depth = FCOE_PARAMS_NUM_TASKS;
  3371. fcoe->fcoe_txq_depth = FCOE_PARAMS_NUM_TASKS;
  3372. fcoe->fcoe_rx_frames_set = true;
  3373. fcoe->fcoe_rx_frames = hst->rx_frames;
  3374. fcoe->fcoe_tx_frames_set = true;
  3375. fcoe->fcoe_tx_frames = hst->tx_frames;
  3376. fcoe->fcoe_rx_bytes_set = true;
  3377. fcoe->fcoe_rx_bytes = hst->fcp_input_megabytes * 1000000;
  3378. fcoe->fcoe_tx_bytes_set = true;
  3379. fcoe->fcoe_tx_bytes = hst->fcp_output_megabytes * 1000000;
  3380. fcoe->crc_count_set = true;
  3381. fcoe->crc_count = hst->invalid_crc_count;
  3382. fcoe->tx_abts_set = true;
  3383. fcoe->tx_abts = hst->fcp_packet_aborts;
  3384. fcoe->tx_lun_rst_set = true;
  3385. fcoe->tx_lun_rst = qedf->lun_resets;
  3386. fcoe->abort_task_sets_set = true;
  3387. fcoe->abort_task_sets = qedf->packet_aborts;
  3388. fcoe->scsi_busy_set = true;
  3389. fcoe->scsi_busy = qedf->busy;
  3390. fcoe->scsi_tsk_full_set = true;
  3391. fcoe->scsi_tsk_full = qedf->task_set_fulls;
  3392. }
  3393. /* Deferred work function to perform soft context reset on STAG change */
  3394. void qedf_stag_change_work(struct work_struct *work)
  3395. {
  3396. struct qedf_ctx *qedf =
  3397. container_of(work, struct qedf_ctx, stag_work.work);
  3398. if (test_bit(QEDF_IN_RECOVERY, &qedf->flags)) {
  3399. QEDF_ERR(&qedf->dbg_ctx,
  3400. "Already is in recovery, hence not calling software context reset.\n");
  3401. return;
  3402. }
  3403. if (test_bit(QEDF_UNLOADING, &qedf->flags)) {
  3404. QEDF_ERR(&qedf->dbg_ctx, "Driver unloading\n");
  3405. return;
  3406. }
  3407. set_bit(QEDF_STAG_IN_PROGRESS, &qedf->flags);
  3408. printk_ratelimited("[%s]:[%s:%d]:%d: Performing software context reset.",
  3409. dev_name(&qedf->pdev->dev), __func__, __LINE__,
  3410. qedf->dbg_ctx.host_no);
  3411. qedf_ctx_soft_reset(qedf->lport);
  3412. }
  3413. static void qedf_shutdown(struct pci_dev *pdev)
  3414. {
  3415. __qedf_remove(pdev, QEDF_MODE_NORMAL);
  3416. }
  3417. static int qedf_suspend(struct pci_dev *pdev, pm_message_t state)
  3418. {
  3419. struct qedf_ctx *qedf;
  3420. if (!pdev) {
  3421. QEDF_ERR(NULL, "pdev is NULL.\n");
  3422. return -ENODEV;
  3423. }
  3424. qedf = pci_get_drvdata(pdev);
  3425. QEDF_ERR(&qedf->dbg_ctx, "%s: Device does not support suspend operation\n", __func__);
  3426. return -EPERM;
  3427. }
  3428. /*
  3429. * Recovery handler code
  3430. */
  3431. static void qedf_schedule_recovery_handler(void *dev)
  3432. {
  3433. struct qedf_ctx *qedf = dev;
  3434. QEDF_ERR(&qedf->dbg_ctx, "Recovery handler scheduled.\n");
  3435. schedule_delayed_work(&qedf->recovery_work, 0);
  3436. }
  3437. static void qedf_recovery_handler(struct work_struct *work)
  3438. {
  3439. struct qedf_ctx *qedf =
  3440. container_of(work, struct qedf_ctx, recovery_work.work);
  3441. if (test_and_set_bit(QEDF_IN_RECOVERY, &qedf->flags))
  3442. return;
  3443. /*
  3444. * Call common_ops->recovery_prolog to allow the MFW to quiesce
  3445. * any PCI transactions.
  3446. */
  3447. qed_ops->common->recovery_prolog(qedf->cdev);
  3448. QEDF_ERR(&qedf->dbg_ctx, "Recovery work start.\n");
  3449. __qedf_remove(qedf->pdev, QEDF_MODE_RECOVERY);
  3450. /*
  3451. * Reset link and dcbx to down state since we will not get a link down
  3452. * event from the MFW but calling __qedf_remove will essentially be a
  3453. * link down event.
  3454. */
  3455. atomic_set(&qedf->link_state, QEDF_LINK_DOWN);
  3456. atomic_set(&qedf->dcbx, QEDF_DCBX_PENDING);
  3457. __qedf_probe(qedf->pdev, QEDF_MODE_RECOVERY);
  3458. clear_bit(QEDF_IN_RECOVERY, &qedf->flags);
  3459. QEDF_ERR(&qedf->dbg_ctx, "Recovery work complete.\n");
  3460. }
  3461. /* Generic TLV data callback */
  3462. void qedf_get_generic_tlv_data(void *dev, struct qed_generic_tlvs *data)
  3463. {
  3464. struct qedf_ctx *qedf;
  3465. if (!dev) {
  3466. QEDF_INFO(NULL, QEDF_LOG_EVT,
  3467. "dev is NULL so ignoring get_generic_tlv_data request.\n");
  3468. return;
  3469. }
  3470. qedf = (struct qedf_ctx *)dev;
  3471. memset(data, 0, sizeof(struct qed_generic_tlvs));
  3472. ether_addr_copy(data->mac[0], qedf->mac);
  3473. }
  3474. /*
  3475. * Module Init/Remove
  3476. */
  3477. static int __init qedf_init(void)
  3478. {
  3479. int ret;
  3480. /* If debug=1 passed, set the default log mask */
  3481. if (qedf_debug == QEDF_LOG_DEFAULT)
  3482. qedf_debug = QEDF_DEFAULT_LOG_MASK;
  3483. /*
  3484. * Check that default prio for FIP/FCoE traffic is between 0..7 if a
  3485. * value has been set
  3486. */
  3487. if (qedf_default_prio > -1)
  3488. if (qedf_default_prio > 7) {
  3489. qedf_default_prio = QEDF_DEFAULT_PRIO;
  3490. QEDF_ERR(NULL, "FCoE/FIP priority out of range, resetting to %d.\n",
  3491. QEDF_DEFAULT_PRIO);
  3492. }
  3493. /* Print driver banner */
  3494. QEDF_INFO(NULL, QEDF_LOG_INFO, "%s v%s.\n", QEDF_DESCR,
  3495. QEDF_VERSION);
  3496. /* Create kmem_cache for qedf_io_work structs */
  3497. qedf_io_work_cache = kmem_cache_create("qedf_io_work_cache",
  3498. sizeof(struct qedf_io_work), 0, SLAB_HWCACHE_ALIGN, NULL);
  3499. if (qedf_io_work_cache == NULL) {
  3500. QEDF_ERR(NULL, "qedf_io_work_cache is NULL.\n");
  3501. goto err1;
  3502. }
  3503. QEDF_INFO(NULL, QEDF_LOG_DISC, "qedf_io_work_cache=%p.\n",
  3504. qedf_io_work_cache);
  3505. qed_ops = qed_get_fcoe_ops();
  3506. if (!qed_ops) {
  3507. QEDF_ERR(NULL, "Failed to get qed fcoe operations\n");
  3508. goto err1;
  3509. }
  3510. #ifdef CONFIG_DEBUG_FS
  3511. qedf_dbg_init("qedf");
  3512. #endif
  3513. qedf_fc_transport_template =
  3514. fc_attach_transport(&qedf_fc_transport_fn);
  3515. if (!qedf_fc_transport_template) {
  3516. QEDF_ERR(NULL, "Could not register with FC transport\n");
  3517. goto err2;
  3518. }
  3519. qedf_fc_vport_transport_template =
  3520. fc_attach_transport(&qedf_fc_vport_transport_fn);
  3521. if (!qedf_fc_vport_transport_template) {
  3522. QEDF_ERR(NULL, "Could not register vport template with FC "
  3523. "transport\n");
  3524. goto err3;
  3525. }
  3526. qedf_io_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM | WQ_PERCPU, 1,
  3527. "qedf_io_wq");
  3528. if (!qedf_io_wq) {
  3529. QEDF_ERR(NULL, "Could not create qedf_io_wq.\n");
  3530. goto err4;
  3531. }
  3532. qedf_cb_ops.get_login_failures = qedf_get_login_failures;
  3533. ret = pci_register_driver(&qedf_pci_driver);
  3534. if (ret) {
  3535. QEDF_ERR(NULL, "Failed to register driver\n");
  3536. goto err5;
  3537. }
  3538. return 0;
  3539. err5:
  3540. destroy_workqueue(qedf_io_wq);
  3541. err4:
  3542. fc_release_transport(qedf_fc_vport_transport_template);
  3543. err3:
  3544. fc_release_transport(qedf_fc_transport_template);
  3545. err2:
  3546. #ifdef CONFIG_DEBUG_FS
  3547. qedf_dbg_exit();
  3548. #endif
  3549. qed_put_fcoe_ops();
  3550. err1:
  3551. return -EINVAL;
  3552. }
  3553. static void __exit qedf_cleanup(void)
  3554. {
  3555. pci_unregister_driver(&qedf_pci_driver);
  3556. destroy_workqueue(qedf_io_wq);
  3557. fc_release_transport(qedf_fc_vport_transport_template);
  3558. fc_release_transport(qedf_fc_transport_template);
  3559. #ifdef CONFIG_DEBUG_FS
  3560. qedf_dbg_exit();
  3561. #endif
  3562. qed_put_fcoe_ops();
  3563. kmem_cache_destroy(qedf_io_work_cache);
  3564. }
  3565. MODULE_LICENSE("GPL");
  3566. MODULE_DESCRIPTION("QLogic FastLinQ 4xxxx FCoE Module");
  3567. MODULE_AUTHOR("QLogic Corporation");
  3568. MODULE_VERSION(QEDF_VERSION);
  3569. module_init(qedf_init);
  3570. module_exit(qedf_cleanup);