ibmvfc.c 141 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060
  1. /*
  2. * ibmvfc.c -- driver for IBM Power Virtual Fibre Channel Adapter
  3. *
  4. * Written By: Brian King <brking@linux.vnet.ibm.com>, IBM Corporation
  5. *
  6. * Copyright (C) IBM Corporation, 2008
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/dmapool.h>
  27. #include <linux/delay.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/kthread.h>
  30. #include <linux/slab.h>
  31. #include <linux/of.h>
  32. #include <linux/pm.h>
  33. #include <linux/stringify.h>
  34. #include <asm/firmware.h>
  35. #include <asm/irq.h>
  36. #include <asm/vio.h>
  37. #include <scsi/scsi.h>
  38. #include <scsi/scsi_cmnd.h>
  39. #include <scsi/scsi_host.h>
  40. #include <scsi/scsi_device.h>
  41. #include <scsi/scsi_tcq.h>
  42. #include <scsi/scsi_transport_fc.h>
  43. #include <scsi/scsi_bsg_fc.h>
  44. #include "ibmvfc.h"
  45. static unsigned int init_timeout = IBMVFC_INIT_TIMEOUT;
  46. static unsigned int default_timeout = IBMVFC_DEFAULT_TIMEOUT;
  47. static u64 max_lun = IBMVFC_MAX_LUN;
  48. static unsigned int max_targets = IBMVFC_MAX_TARGETS;
  49. static unsigned int max_requests = IBMVFC_MAX_REQUESTS_DEFAULT;
  50. static unsigned int disc_threads = IBMVFC_MAX_DISC_THREADS;
  51. static unsigned int ibmvfc_debug = IBMVFC_DEBUG;
  52. static unsigned int log_level = IBMVFC_DEFAULT_LOG_LEVEL;
  53. static LIST_HEAD(ibmvfc_head);
  54. static DEFINE_SPINLOCK(ibmvfc_driver_lock);
  55. static struct scsi_transport_template *ibmvfc_transport_template;
  56. MODULE_DESCRIPTION("IBM Virtual Fibre Channel Driver");
  57. MODULE_AUTHOR("Brian King <brking@linux.vnet.ibm.com>");
  58. MODULE_LICENSE("GPL");
  59. MODULE_VERSION(IBMVFC_DRIVER_VERSION);
  60. module_param_named(init_timeout, init_timeout, uint, S_IRUGO | S_IWUSR);
  61. MODULE_PARM_DESC(init_timeout, "Initialization timeout in seconds. "
  62. "[Default=" __stringify(IBMVFC_INIT_TIMEOUT) "]");
  63. module_param_named(default_timeout, default_timeout, uint, S_IRUGO | S_IWUSR);
  64. MODULE_PARM_DESC(default_timeout,
  65. "Default timeout in seconds for initialization and EH commands. "
  66. "[Default=" __stringify(IBMVFC_DEFAULT_TIMEOUT) "]");
  67. module_param_named(max_requests, max_requests, uint, S_IRUGO);
  68. MODULE_PARM_DESC(max_requests, "Maximum requests for this adapter. "
  69. "[Default=" __stringify(IBMVFC_MAX_REQUESTS_DEFAULT) "]");
  70. module_param_named(max_lun, max_lun, ullong, S_IRUGO);
  71. MODULE_PARM_DESC(max_lun, "Maximum allowed LUN. "
  72. "[Default=" __stringify(IBMVFC_MAX_LUN) "]");
  73. module_param_named(max_targets, max_targets, uint, S_IRUGO);
  74. MODULE_PARM_DESC(max_targets, "Maximum allowed targets. "
  75. "[Default=" __stringify(IBMVFC_MAX_TARGETS) "]");
  76. module_param_named(disc_threads, disc_threads, uint, S_IRUGO);
  77. MODULE_PARM_DESC(disc_threads, "Number of device discovery threads to use. "
  78. "[Default=" __stringify(IBMVFC_MAX_DISC_THREADS) "]");
  79. module_param_named(debug, ibmvfc_debug, uint, S_IRUGO | S_IWUSR);
  80. MODULE_PARM_DESC(debug, "Enable driver debug information. "
  81. "[Default=" __stringify(IBMVFC_DEBUG) "]");
  82. module_param_named(log_level, log_level, uint, 0);
  83. MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver. "
  84. "[Default=" __stringify(IBMVFC_DEFAULT_LOG_LEVEL) "]");
  85. static const struct {
  86. u16 status;
  87. u16 error;
  88. u8 result;
  89. u8 retry;
  90. int log;
  91. char *name;
  92. } cmd_status [] = {
  93. { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_ESTABLISH, DID_ERROR, 1, 1, "unable to establish" },
  94. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_FAULT, DID_OK, 1, 0, "transport fault" },
  95. { IBMVFC_FABRIC_MAPPED, IBMVFC_CMD_TIMEOUT, DID_TIME_OUT, 1, 1, "command timeout" },
  96. { IBMVFC_FABRIC_MAPPED, IBMVFC_ENETDOWN, DID_TRANSPORT_DISRUPTED, 1, 1, "network down" },
  97. { IBMVFC_FABRIC_MAPPED, IBMVFC_HW_FAILURE, DID_ERROR, 1, 1, "hardware failure" },
  98. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DOWN_ERR, DID_REQUEUE, 0, 0, "link down" },
  99. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_DEAD_ERR, DID_ERROR, 0, 0, "link dead" },
  100. { IBMVFC_FABRIC_MAPPED, IBMVFC_UNABLE_TO_REGISTER, DID_ERROR, 1, 1, "unable to register" },
  101. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_BUSY, DID_BUS_BUSY, 1, 0, "transport busy" },
  102. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_DEAD, DID_ERROR, 0, 1, "transport dead" },
  103. { IBMVFC_FABRIC_MAPPED, IBMVFC_CONFIG_ERROR, DID_ERROR, 1, 1, "configuration error" },
  104. { IBMVFC_FABRIC_MAPPED, IBMVFC_NAME_SERVER_FAIL, DID_ERROR, 1, 1, "name server failure" },
  105. { IBMVFC_FABRIC_MAPPED, IBMVFC_LINK_HALTED, DID_REQUEUE, 1, 0, "link halted" },
  106. { IBMVFC_FABRIC_MAPPED, IBMVFC_XPORT_GENERAL, DID_OK, 1, 0, "general transport error" },
  107. { IBMVFC_VIOS_FAILURE, IBMVFC_CRQ_FAILURE, DID_REQUEUE, 1, 1, "CRQ failure" },
  108. { IBMVFC_VIOS_FAILURE, IBMVFC_SW_FAILURE, DID_ERROR, 0, 1, "software failure" },
  109. { IBMVFC_VIOS_FAILURE, IBMVFC_INVALID_PARAMETER, DID_ERROR, 0, 1, "invalid parameter" },
  110. { IBMVFC_VIOS_FAILURE, IBMVFC_MISSING_PARAMETER, DID_ERROR, 0, 1, "missing parameter" },
  111. { IBMVFC_VIOS_FAILURE, IBMVFC_HOST_IO_BUS, DID_ERROR, 1, 1, "host I/O bus failure" },
  112. { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED, DID_ERROR, 0, 1, "transaction cancelled" },
  113. { IBMVFC_VIOS_FAILURE, IBMVFC_TRANS_CANCELLED_IMPLICIT, DID_ERROR, 0, 1, "transaction cancelled implicit" },
  114. { IBMVFC_VIOS_FAILURE, IBMVFC_INSUFFICIENT_RESOURCE, DID_REQUEUE, 1, 1, "insufficient resources" },
  115. { IBMVFC_VIOS_FAILURE, IBMVFC_PLOGI_REQUIRED, DID_ERROR, 0, 1, "port login required" },
  116. { IBMVFC_VIOS_FAILURE, IBMVFC_COMMAND_FAILED, DID_ERROR, 1, 1, "command failed" },
  117. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_ELS_CMD_CODE, DID_ERROR, 0, 1, "invalid ELS command code" },
  118. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_VERSION, DID_ERROR, 0, 1, "invalid version level" },
  119. { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_ERROR, DID_ERROR, 1, 1, "logical error" },
  120. { IBMVFC_FC_FAILURE, IBMVFC_INVALID_CT_IU_SIZE, DID_ERROR, 0, 1, "invalid CT_IU size" },
  121. { IBMVFC_FC_FAILURE, IBMVFC_LOGICAL_BUSY, DID_REQUEUE, 1, 0, "logical busy" },
  122. { IBMVFC_FC_FAILURE, IBMVFC_PROTOCOL_ERROR, DID_ERROR, 1, 1, "protocol error" },
  123. { IBMVFC_FC_FAILURE, IBMVFC_UNABLE_TO_PERFORM_REQ, DID_ERROR, 1, 1, "unable to perform request" },
  124. { IBMVFC_FC_FAILURE, IBMVFC_CMD_NOT_SUPPORTED, DID_ERROR, 0, 0, "command not supported" },
  125. { IBMVFC_FC_FAILURE, IBMVFC_SERVER_NOT_AVAIL, DID_ERROR, 0, 1, "server not available" },
  126. { IBMVFC_FC_FAILURE, IBMVFC_CMD_IN_PROGRESS, DID_ERROR, 0, 1, "command already in progress" },
  127. { IBMVFC_FC_FAILURE, IBMVFC_VENDOR_SPECIFIC, DID_ERROR, 1, 1, "vendor specific" },
  128. { IBMVFC_FC_SCSI_ERROR, 0, DID_OK, 1, 0, "SCSI error" },
  129. };
  130. static void ibmvfc_npiv_login(struct ibmvfc_host *);
  131. static void ibmvfc_tgt_send_prli(struct ibmvfc_target *);
  132. static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *);
  133. static void ibmvfc_tgt_query_target(struct ibmvfc_target *);
  134. static void ibmvfc_npiv_logout(struct ibmvfc_host *);
  135. static const char *unknown_error = "unknown error";
  136. #ifdef CONFIG_SCSI_IBMVFC_TRACE
  137. /**
  138. * ibmvfc_trc_start - Log a start trace entry
  139. * @evt: ibmvfc event struct
  140. *
  141. **/
  142. static void ibmvfc_trc_start(struct ibmvfc_event *evt)
  143. {
  144. struct ibmvfc_host *vhost = evt->vhost;
  145. struct ibmvfc_cmd *vfc_cmd = &evt->iu.cmd;
  146. struct ibmvfc_mad_common *mad = &evt->iu.mad_common;
  147. struct ibmvfc_trace_entry *entry;
  148. entry = &vhost->trace[vhost->trace_index++];
  149. entry->evt = evt;
  150. entry->time = jiffies;
  151. entry->fmt = evt->crq.format;
  152. entry->type = IBMVFC_TRC_START;
  153. switch (entry->fmt) {
  154. case IBMVFC_CMD_FORMAT:
  155. entry->op_code = vfc_cmd->iu.cdb[0];
  156. entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
  157. entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
  158. entry->tmf_flags = vfc_cmd->iu.tmf_flags;
  159. entry->u.start.xfer_len = be32_to_cpu(vfc_cmd->iu.xfer_len);
  160. break;
  161. case IBMVFC_MAD_FORMAT:
  162. entry->op_code = be32_to_cpu(mad->opcode);
  163. break;
  164. default:
  165. break;
  166. };
  167. }
  168. /**
  169. * ibmvfc_trc_end - Log an end trace entry
  170. * @evt: ibmvfc event struct
  171. *
  172. **/
  173. static void ibmvfc_trc_end(struct ibmvfc_event *evt)
  174. {
  175. struct ibmvfc_host *vhost = evt->vhost;
  176. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  177. struct ibmvfc_mad_common *mad = &evt->xfer_iu->mad_common;
  178. struct ibmvfc_trace_entry *entry = &vhost->trace[vhost->trace_index++];
  179. entry->evt = evt;
  180. entry->time = jiffies;
  181. entry->fmt = evt->crq.format;
  182. entry->type = IBMVFC_TRC_END;
  183. switch (entry->fmt) {
  184. case IBMVFC_CMD_FORMAT:
  185. entry->op_code = vfc_cmd->iu.cdb[0];
  186. entry->scsi_id = be64_to_cpu(vfc_cmd->tgt_scsi_id);
  187. entry->lun = scsilun_to_int(&vfc_cmd->iu.lun);
  188. entry->tmf_flags = vfc_cmd->iu.tmf_flags;
  189. entry->u.end.status = be16_to_cpu(vfc_cmd->status);
  190. entry->u.end.error = be16_to_cpu(vfc_cmd->error);
  191. entry->u.end.fcp_rsp_flags = vfc_cmd->rsp.flags;
  192. entry->u.end.rsp_code = vfc_cmd->rsp.data.info.rsp_code;
  193. entry->u.end.scsi_status = vfc_cmd->rsp.scsi_status;
  194. break;
  195. case IBMVFC_MAD_FORMAT:
  196. entry->op_code = be32_to_cpu(mad->opcode);
  197. entry->u.end.status = be16_to_cpu(mad->status);
  198. break;
  199. default:
  200. break;
  201. };
  202. }
  203. #else
  204. #define ibmvfc_trc_start(evt) do { } while (0)
  205. #define ibmvfc_trc_end(evt) do { } while (0)
  206. #endif
  207. /**
  208. * ibmvfc_get_err_index - Find the index into cmd_status for the fcp response
  209. * @status: status / error class
  210. * @error: error
  211. *
  212. * Return value:
  213. * index into cmd_status / -EINVAL on failure
  214. **/
  215. static int ibmvfc_get_err_index(u16 status, u16 error)
  216. {
  217. int i;
  218. for (i = 0; i < ARRAY_SIZE(cmd_status); i++)
  219. if ((cmd_status[i].status & status) == cmd_status[i].status &&
  220. cmd_status[i].error == error)
  221. return i;
  222. return -EINVAL;
  223. }
  224. /**
  225. * ibmvfc_get_cmd_error - Find the error description for the fcp response
  226. * @status: status / error class
  227. * @error: error
  228. *
  229. * Return value:
  230. * error description string
  231. **/
  232. static const char *ibmvfc_get_cmd_error(u16 status, u16 error)
  233. {
  234. int rc = ibmvfc_get_err_index(status, error);
  235. if (rc >= 0)
  236. return cmd_status[rc].name;
  237. return unknown_error;
  238. }
  239. /**
  240. * ibmvfc_get_err_result - Find the scsi status to return for the fcp response
  241. * @vfc_cmd: ibmvfc command struct
  242. *
  243. * Return value:
  244. * SCSI result value to return for completed command
  245. **/
  246. static int ibmvfc_get_err_result(struct ibmvfc_cmd *vfc_cmd)
  247. {
  248. int err;
  249. struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
  250. int fc_rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
  251. if ((rsp->flags & FCP_RSP_LEN_VALID) &&
  252. ((fc_rsp_len && fc_rsp_len != 4 && fc_rsp_len != 8) ||
  253. rsp->data.info.rsp_code))
  254. return DID_ERROR << 16;
  255. err = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
  256. if (err >= 0)
  257. return rsp->scsi_status | (cmd_status[err].result << 16);
  258. return rsp->scsi_status | (DID_ERROR << 16);
  259. }
  260. /**
  261. * ibmvfc_retry_cmd - Determine if error status is retryable
  262. * @status: status / error class
  263. * @error: error
  264. *
  265. * Return value:
  266. * 1 if error should be retried / 0 if it should not
  267. **/
  268. static int ibmvfc_retry_cmd(u16 status, u16 error)
  269. {
  270. int rc = ibmvfc_get_err_index(status, error);
  271. if (rc >= 0)
  272. return cmd_status[rc].retry;
  273. return 1;
  274. }
  275. static const char *unknown_fc_explain = "unknown fc explain";
  276. static const struct {
  277. u16 fc_explain;
  278. char *name;
  279. } ls_explain [] = {
  280. { 0x00, "no additional explanation" },
  281. { 0x01, "service parameter error - options" },
  282. { 0x03, "service parameter error - initiator control" },
  283. { 0x05, "service parameter error - recipient control" },
  284. { 0x07, "service parameter error - received data field size" },
  285. { 0x09, "service parameter error - concurrent seq" },
  286. { 0x0B, "service parameter error - credit" },
  287. { 0x0D, "invalid N_Port/F_Port_Name" },
  288. { 0x0E, "invalid node/Fabric Name" },
  289. { 0x0F, "invalid common service parameters" },
  290. { 0x11, "invalid association header" },
  291. { 0x13, "association header required" },
  292. { 0x15, "invalid originator S_ID" },
  293. { 0x17, "invalid OX_ID-RX-ID combination" },
  294. { 0x19, "command (request) already in progress" },
  295. { 0x1E, "N_Port Login requested" },
  296. { 0x1F, "Invalid N_Port_ID" },
  297. };
  298. static const struct {
  299. u16 fc_explain;
  300. char *name;
  301. } gs_explain [] = {
  302. { 0x00, "no additional explanation" },
  303. { 0x01, "port identifier not registered" },
  304. { 0x02, "port name not registered" },
  305. { 0x03, "node name not registered" },
  306. { 0x04, "class of service not registered" },
  307. { 0x06, "initial process associator not registered" },
  308. { 0x07, "FC-4 TYPEs not registered" },
  309. { 0x08, "symbolic port name not registered" },
  310. { 0x09, "symbolic node name not registered" },
  311. { 0x0A, "port type not registered" },
  312. { 0xF0, "authorization exception" },
  313. { 0xF1, "authentication exception" },
  314. { 0xF2, "data base full" },
  315. { 0xF3, "data base empty" },
  316. { 0xF4, "processing request" },
  317. { 0xF5, "unable to verify connection" },
  318. { 0xF6, "devices not in a common zone" },
  319. };
  320. /**
  321. * ibmvfc_get_ls_explain - Return the FC Explain description text
  322. * @status: FC Explain status
  323. *
  324. * Returns:
  325. * error string
  326. **/
  327. static const char *ibmvfc_get_ls_explain(u16 status)
  328. {
  329. int i;
  330. for (i = 0; i < ARRAY_SIZE(ls_explain); i++)
  331. if (ls_explain[i].fc_explain == status)
  332. return ls_explain[i].name;
  333. return unknown_fc_explain;
  334. }
  335. /**
  336. * ibmvfc_get_gs_explain - Return the FC Explain description text
  337. * @status: FC Explain status
  338. *
  339. * Returns:
  340. * error string
  341. **/
  342. static const char *ibmvfc_get_gs_explain(u16 status)
  343. {
  344. int i;
  345. for (i = 0; i < ARRAY_SIZE(gs_explain); i++)
  346. if (gs_explain[i].fc_explain == status)
  347. return gs_explain[i].name;
  348. return unknown_fc_explain;
  349. }
  350. static const struct {
  351. enum ibmvfc_fc_type fc_type;
  352. char *name;
  353. } fc_type [] = {
  354. { IBMVFC_FABRIC_REJECT, "fabric reject" },
  355. { IBMVFC_PORT_REJECT, "port reject" },
  356. { IBMVFC_LS_REJECT, "ELS reject" },
  357. { IBMVFC_FABRIC_BUSY, "fabric busy" },
  358. { IBMVFC_PORT_BUSY, "port busy" },
  359. { IBMVFC_BASIC_REJECT, "basic reject" },
  360. };
  361. static const char *unknown_fc_type = "unknown fc type";
  362. /**
  363. * ibmvfc_get_fc_type - Return the FC Type description text
  364. * @status: FC Type error status
  365. *
  366. * Returns:
  367. * error string
  368. **/
  369. static const char *ibmvfc_get_fc_type(u16 status)
  370. {
  371. int i;
  372. for (i = 0; i < ARRAY_SIZE(fc_type); i++)
  373. if (fc_type[i].fc_type == status)
  374. return fc_type[i].name;
  375. return unknown_fc_type;
  376. }
  377. /**
  378. * ibmvfc_set_tgt_action - Set the next init action for the target
  379. * @tgt: ibmvfc target struct
  380. * @action: action to perform
  381. *
  382. **/
  383. static void ibmvfc_set_tgt_action(struct ibmvfc_target *tgt,
  384. enum ibmvfc_target_action action)
  385. {
  386. switch (tgt->action) {
  387. case IBMVFC_TGT_ACTION_DEL_RPORT:
  388. if (action == IBMVFC_TGT_ACTION_DELETED_RPORT)
  389. tgt->action = action;
  390. case IBMVFC_TGT_ACTION_DELETED_RPORT:
  391. break;
  392. default:
  393. if (action == IBMVFC_TGT_ACTION_DEL_RPORT)
  394. tgt->add_rport = 0;
  395. tgt->action = action;
  396. break;
  397. }
  398. }
  399. /**
  400. * ibmvfc_set_host_state - Set the state for the host
  401. * @vhost: ibmvfc host struct
  402. * @state: state to set host to
  403. *
  404. * Returns:
  405. * 0 if state changed / non-zero if not changed
  406. **/
  407. static int ibmvfc_set_host_state(struct ibmvfc_host *vhost,
  408. enum ibmvfc_host_state state)
  409. {
  410. int rc = 0;
  411. switch (vhost->state) {
  412. case IBMVFC_HOST_OFFLINE:
  413. rc = -EINVAL;
  414. break;
  415. default:
  416. vhost->state = state;
  417. break;
  418. };
  419. return rc;
  420. }
  421. /**
  422. * ibmvfc_set_host_action - Set the next init action for the host
  423. * @vhost: ibmvfc host struct
  424. * @action: action to perform
  425. *
  426. **/
  427. static void ibmvfc_set_host_action(struct ibmvfc_host *vhost,
  428. enum ibmvfc_host_action action)
  429. {
  430. switch (action) {
  431. case IBMVFC_HOST_ACTION_ALLOC_TGTS:
  432. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT)
  433. vhost->action = action;
  434. break;
  435. case IBMVFC_HOST_ACTION_LOGO_WAIT:
  436. if (vhost->action == IBMVFC_HOST_ACTION_LOGO)
  437. vhost->action = action;
  438. break;
  439. case IBMVFC_HOST_ACTION_INIT_WAIT:
  440. if (vhost->action == IBMVFC_HOST_ACTION_INIT)
  441. vhost->action = action;
  442. break;
  443. case IBMVFC_HOST_ACTION_QUERY:
  444. switch (vhost->action) {
  445. case IBMVFC_HOST_ACTION_INIT_WAIT:
  446. case IBMVFC_HOST_ACTION_NONE:
  447. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  448. vhost->action = action;
  449. break;
  450. default:
  451. break;
  452. };
  453. break;
  454. case IBMVFC_HOST_ACTION_TGT_INIT:
  455. if (vhost->action == IBMVFC_HOST_ACTION_ALLOC_TGTS)
  456. vhost->action = action;
  457. break;
  458. case IBMVFC_HOST_ACTION_INIT:
  459. case IBMVFC_HOST_ACTION_TGT_DEL:
  460. switch (vhost->action) {
  461. case IBMVFC_HOST_ACTION_RESET:
  462. case IBMVFC_HOST_ACTION_REENABLE:
  463. break;
  464. default:
  465. vhost->action = action;
  466. break;
  467. };
  468. break;
  469. case IBMVFC_HOST_ACTION_LOGO:
  470. case IBMVFC_HOST_ACTION_QUERY_TGTS:
  471. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  472. case IBMVFC_HOST_ACTION_NONE:
  473. case IBMVFC_HOST_ACTION_RESET:
  474. case IBMVFC_HOST_ACTION_REENABLE:
  475. default:
  476. vhost->action = action;
  477. break;
  478. };
  479. }
  480. /**
  481. * ibmvfc_reinit_host - Re-start host initialization (no NPIV Login)
  482. * @vhost: ibmvfc host struct
  483. *
  484. * Return value:
  485. * nothing
  486. **/
  487. static void ibmvfc_reinit_host(struct ibmvfc_host *vhost)
  488. {
  489. if (vhost->action == IBMVFC_HOST_ACTION_NONE) {
  490. if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  491. scsi_block_requests(vhost->host);
  492. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  493. }
  494. } else
  495. vhost->reinit = 1;
  496. wake_up(&vhost->work_wait_q);
  497. }
  498. /**
  499. * ibmvfc_link_down - Handle a link down event from the adapter
  500. * @vhost: ibmvfc host struct
  501. * @state: ibmvfc host state to enter
  502. *
  503. **/
  504. static void ibmvfc_link_down(struct ibmvfc_host *vhost,
  505. enum ibmvfc_host_state state)
  506. {
  507. struct ibmvfc_target *tgt;
  508. ENTER;
  509. scsi_block_requests(vhost->host);
  510. list_for_each_entry(tgt, &vhost->targets, queue)
  511. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  512. ibmvfc_set_host_state(vhost, state);
  513. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
  514. vhost->events_to_log |= IBMVFC_AE_LINKDOWN;
  515. wake_up(&vhost->work_wait_q);
  516. LEAVE;
  517. }
  518. /**
  519. * ibmvfc_init_host - Start host initialization
  520. * @vhost: ibmvfc host struct
  521. *
  522. * Return value:
  523. * nothing
  524. **/
  525. static void ibmvfc_init_host(struct ibmvfc_host *vhost)
  526. {
  527. struct ibmvfc_target *tgt;
  528. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
  529. if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
  530. dev_err(vhost->dev,
  531. "Host initialization retries exceeded. Taking adapter offline\n");
  532. ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
  533. return;
  534. }
  535. }
  536. if (!ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  537. memset(vhost->async_crq.msgs, 0, PAGE_SIZE);
  538. vhost->async_crq.cur = 0;
  539. list_for_each_entry(tgt, &vhost->targets, queue)
  540. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  541. scsi_block_requests(vhost->host);
  542. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
  543. vhost->job_step = ibmvfc_npiv_login;
  544. wake_up(&vhost->work_wait_q);
  545. }
  546. }
  547. /**
  548. * ibmvfc_send_crq - Send a CRQ
  549. * @vhost: ibmvfc host struct
  550. * @word1: the first 64 bits of the data
  551. * @word2: the second 64 bits of the data
  552. *
  553. * Return value:
  554. * 0 on success / other on failure
  555. **/
  556. static int ibmvfc_send_crq(struct ibmvfc_host *vhost, u64 word1, u64 word2)
  557. {
  558. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  559. return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
  560. }
  561. /**
  562. * ibmvfc_send_crq_init - Send a CRQ init message
  563. * @vhost: ibmvfc host struct
  564. *
  565. * Return value:
  566. * 0 on success / other on failure
  567. **/
  568. static int ibmvfc_send_crq_init(struct ibmvfc_host *vhost)
  569. {
  570. ibmvfc_dbg(vhost, "Sending CRQ init\n");
  571. return ibmvfc_send_crq(vhost, 0xC001000000000000LL, 0);
  572. }
  573. /**
  574. * ibmvfc_send_crq_init_complete - Send a CRQ init complete message
  575. * @vhost: ibmvfc host struct
  576. *
  577. * Return value:
  578. * 0 on success / other on failure
  579. **/
  580. static int ibmvfc_send_crq_init_complete(struct ibmvfc_host *vhost)
  581. {
  582. ibmvfc_dbg(vhost, "Sending CRQ init complete\n");
  583. return ibmvfc_send_crq(vhost, 0xC002000000000000LL, 0);
  584. }
  585. /**
  586. * ibmvfc_release_crq_queue - Deallocates data and unregisters CRQ
  587. * @vhost: ibmvfc host struct
  588. *
  589. * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
  590. * the crq with the hypervisor.
  591. **/
  592. static void ibmvfc_release_crq_queue(struct ibmvfc_host *vhost)
  593. {
  594. long rc = 0;
  595. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  596. struct ibmvfc_crq_queue *crq = &vhost->crq;
  597. ibmvfc_dbg(vhost, "Releasing CRQ\n");
  598. free_irq(vdev->irq, vhost);
  599. tasklet_kill(&vhost->tasklet);
  600. do {
  601. if (rc)
  602. msleep(100);
  603. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  604. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  605. vhost->state = IBMVFC_NO_CRQ;
  606. vhost->logged_in = 0;
  607. dma_unmap_single(vhost->dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
  608. free_page((unsigned long)crq->msgs);
  609. }
  610. /**
  611. * ibmvfc_reenable_crq_queue - reenables the CRQ
  612. * @vhost: ibmvfc host struct
  613. *
  614. * Return value:
  615. * 0 on success / other on failure
  616. **/
  617. static int ibmvfc_reenable_crq_queue(struct ibmvfc_host *vhost)
  618. {
  619. int rc = 0;
  620. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  621. /* Re-enable the CRQ */
  622. do {
  623. if (rc)
  624. msleep(100);
  625. rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
  626. } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
  627. if (rc)
  628. dev_err(vhost->dev, "Error enabling adapter (rc=%d)\n", rc);
  629. return rc;
  630. }
  631. /**
  632. * ibmvfc_reset_crq - resets a crq after a failure
  633. * @vhost: ibmvfc host struct
  634. *
  635. * Return value:
  636. * 0 on success / other on failure
  637. **/
  638. static int ibmvfc_reset_crq(struct ibmvfc_host *vhost)
  639. {
  640. int rc = 0;
  641. unsigned long flags;
  642. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  643. struct ibmvfc_crq_queue *crq = &vhost->crq;
  644. /* Close the CRQ */
  645. do {
  646. if (rc)
  647. msleep(100);
  648. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  649. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  650. spin_lock_irqsave(vhost->host->host_lock, flags);
  651. vhost->state = IBMVFC_NO_CRQ;
  652. vhost->logged_in = 0;
  653. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  654. /* Clean out the queue */
  655. memset(crq->msgs, 0, PAGE_SIZE);
  656. crq->cur = 0;
  657. /* And re-open it again */
  658. rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
  659. crq->msg_token, PAGE_SIZE);
  660. if (rc == H_CLOSED)
  661. /* Adapter is good, but other end is not ready */
  662. dev_warn(vhost->dev, "Partner adapter not ready\n");
  663. else if (rc != 0)
  664. dev_warn(vhost->dev, "Couldn't register crq (rc=%d)\n", rc);
  665. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  666. return rc;
  667. }
  668. /**
  669. * ibmvfc_valid_event - Determines if event is valid.
  670. * @pool: event_pool that contains the event
  671. * @evt: ibmvfc event to be checked for validity
  672. *
  673. * Return value:
  674. * 1 if event is valid / 0 if event is not valid
  675. **/
  676. static int ibmvfc_valid_event(struct ibmvfc_event_pool *pool,
  677. struct ibmvfc_event *evt)
  678. {
  679. int index = evt - pool->events;
  680. if (index < 0 || index >= pool->size) /* outside of bounds */
  681. return 0;
  682. if (evt != pool->events + index) /* unaligned */
  683. return 0;
  684. return 1;
  685. }
  686. /**
  687. * ibmvfc_free_event - Free the specified event
  688. * @evt: ibmvfc_event to be freed
  689. *
  690. **/
  691. static void ibmvfc_free_event(struct ibmvfc_event *evt)
  692. {
  693. struct ibmvfc_host *vhost = evt->vhost;
  694. struct ibmvfc_event_pool *pool = &vhost->pool;
  695. BUG_ON(!ibmvfc_valid_event(pool, evt));
  696. BUG_ON(atomic_inc_return(&evt->free) != 1);
  697. list_add_tail(&evt->queue, &vhost->free);
  698. }
  699. /**
  700. * ibmvfc_scsi_eh_done - EH done function for queuecommand commands
  701. * @evt: ibmvfc event struct
  702. *
  703. * This function does not setup any error status, that must be done
  704. * before this function gets called.
  705. **/
  706. static void ibmvfc_scsi_eh_done(struct ibmvfc_event *evt)
  707. {
  708. struct scsi_cmnd *cmnd = evt->cmnd;
  709. if (cmnd) {
  710. scsi_dma_unmap(cmnd);
  711. cmnd->scsi_done(cmnd);
  712. }
  713. if (evt->eh_comp)
  714. complete(evt->eh_comp);
  715. ibmvfc_free_event(evt);
  716. }
  717. /**
  718. * ibmvfc_fail_request - Fail request with specified error code
  719. * @evt: ibmvfc event struct
  720. * @error_code: error code to fail request with
  721. *
  722. * Return value:
  723. * none
  724. **/
  725. static void ibmvfc_fail_request(struct ibmvfc_event *evt, int error_code)
  726. {
  727. if (evt->cmnd) {
  728. evt->cmnd->result = (error_code << 16);
  729. evt->done = ibmvfc_scsi_eh_done;
  730. } else
  731. evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_DRIVER_FAILED);
  732. list_del(&evt->queue);
  733. del_timer(&evt->timer);
  734. ibmvfc_trc_end(evt);
  735. evt->done(evt);
  736. }
  737. /**
  738. * ibmvfc_purge_requests - Our virtual adapter just shut down. Purge any sent requests
  739. * @vhost: ibmvfc host struct
  740. * @error_code: error code to fail requests with
  741. *
  742. * Return value:
  743. * none
  744. **/
  745. static void ibmvfc_purge_requests(struct ibmvfc_host *vhost, int error_code)
  746. {
  747. struct ibmvfc_event *evt, *pos;
  748. ibmvfc_dbg(vhost, "Purging all requests\n");
  749. list_for_each_entry_safe(evt, pos, &vhost->sent, queue)
  750. ibmvfc_fail_request(evt, error_code);
  751. }
  752. /**
  753. * ibmvfc_hard_reset_host - Reset the connection to the server by breaking the CRQ
  754. * @vhost: struct ibmvfc host to reset
  755. **/
  756. static void ibmvfc_hard_reset_host(struct ibmvfc_host *vhost)
  757. {
  758. ibmvfc_purge_requests(vhost, DID_ERROR);
  759. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  760. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
  761. }
  762. /**
  763. * __ibmvfc_reset_host - Reset the connection to the server (no locking)
  764. * @vhost: struct ibmvfc host to reset
  765. **/
  766. static void __ibmvfc_reset_host(struct ibmvfc_host *vhost)
  767. {
  768. if (vhost->logged_in && vhost->action != IBMVFC_HOST_ACTION_LOGO_WAIT &&
  769. !ibmvfc_set_host_state(vhost, IBMVFC_INITIALIZING)) {
  770. scsi_block_requests(vhost->host);
  771. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO);
  772. vhost->job_step = ibmvfc_npiv_logout;
  773. wake_up(&vhost->work_wait_q);
  774. } else
  775. ibmvfc_hard_reset_host(vhost);
  776. }
  777. /**
  778. * ibmvfc_reset_host - Reset the connection to the server
  779. * @vhost: ibmvfc host struct
  780. **/
  781. static void ibmvfc_reset_host(struct ibmvfc_host *vhost)
  782. {
  783. unsigned long flags;
  784. spin_lock_irqsave(vhost->host->host_lock, flags);
  785. __ibmvfc_reset_host(vhost);
  786. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  787. }
  788. /**
  789. * ibmvfc_retry_host_init - Retry host initialization if allowed
  790. * @vhost: ibmvfc host struct
  791. *
  792. * Returns: 1 if init will be retried / 0 if not
  793. *
  794. **/
  795. static int ibmvfc_retry_host_init(struct ibmvfc_host *vhost)
  796. {
  797. int retry = 0;
  798. if (vhost->action == IBMVFC_HOST_ACTION_INIT_WAIT) {
  799. vhost->delay_init = 1;
  800. if (++vhost->init_retries > IBMVFC_MAX_HOST_INIT_RETRIES) {
  801. dev_err(vhost->dev,
  802. "Host initialization retries exceeded. Taking adapter offline\n");
  803. ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
  804. } else if (vhost->init_retries == IBMVFC_MAX_HOST_INIT_RETRIES)
  805. __ibmvfc_reset_host(vhost);
  806. else {
  807. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
  808. retry = 1;
  809. }
  810. }
  811. wake_up(&vhost->work_wait_q);
  812. return retry;
  813. }
  814. /**
  815. * __ibmvfc_get_target - Find the specified scsi_target (no locking)
  816. * @starget: scsi target struct
  817. *
  818. * Return value:
  819. * ibmvfc_target struct / NULL if not found
  820. **/
  821. static struct ibmvfc_target *__ibmvfc_get_target(struct scsi_target *starget)
  822. {
  823. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  824. struct ibmvfc_host *vhost = shost_priv(shost);
  825. struct ibmvfc_target *tgt;
  826. list_for_each_entry(tgt, &vhost->targets, queue)
  827. if (tgt->target_id == starget->id) {
  828. kref_get(&tgt->kref);
  829. return tgt;
  830. }
  831. return NULL;
  832. }
  833. /**
  834. * ibmvfc_get_target - Find the specified scsi_target
  835. * @starget: scsi target struct
  836. *
  837. * Return value:
  838. * ibmvfc_target struct / NULL if not found
  839. **/
  840. static struct ibmvfc_target *ibmvfc_get_target(struct scsi_target *starget)
  841. {
  842. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  843. struct ibmvfc_target *tgt;
  844. unsigned long flags;
  845. spin_lock_irqsave(shost->host_lock, flags);
  846. tgt = __ibmvfc_get_target(starget);
  847. spin_unlock_irqrestore(shost->host_lock, flags);
  848. return tgt;
  849. }
  850. /**
  851. * ibmvfc_get_host_speed - Get host port speed
  852. * @shost: scsi host struct
  853. *
  854. * Return value:
  855. * none
  856. **/
  857. static void ibmvfc_get_host_speed(struct Scsi_Host *shost)
  858. {
  859. struct ibmvfc_host *vhost = shost_priv(shost);
  860. unsigned long flags;
  861. spin_lock_irqsave(shost->host_lock, flags);
  862. if (vhost->state == IBMVFC_ACTIVE) {
  863. switch (be64_to_cpu(vhost->login_buf->resp.link_speed) / 100) {
  864. case 1:
  865. fc_host_speed(shost) = FC_PORTSPEED_1GBIT;
  866. break;
  867. case 2:
  868. fc_host_speed(shost) = FC_PORTSPEED_2GBIT;
  869. break;
  870. case 4:
  871. fc_host_speed(shost) = FC_PORTSPEED_4GBIT;
  872. break;
  873. case 8:
  874. fc_host_speed(shost) = FC_PORTSPEED_8GBIT;
  875. break;
  876. case 10:
  877. fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
  878. break;
  879. case 16:
  880. fc_host_speed(shost) = FC_PORTSPEED_16GBIT;
  881. break;
  882. default:
  883. ibmvfc_log(vhost, 3, "Unknown port speed: %lld Gbit\n",
  884. be64_to_cpu(vhost->login_buf->resp.link_speed) / 100);
  885. fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
  886. break;
  887. }
  888. } else
  889. fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
  890. spin_unlock_irqrestore(shost->host_lock, flags);
  891. }
  892. /**
  893. * ibmvfc_get_host_port_state - Get host port state
  894. * @shost: scsi host struct
  895. *
  896. * Return value:
  897. * none
  898. **/
  899. static void ibmvfc_get_host_port_state(struct Scsi_Host *shost)
  900. {
  901. struct ibmvfc_host *vhost = shost_priv(shost);
  902. unsigned long flags;
  903. spin_lock_irqsave(shost->host_lock, flags);
  904. switch (vhost->state) {
  905. case IBMVFC_INITIALIZING:
  906. case IBMVFC_ACTIVE:
  907. fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
  908. break;
  909. case IBMVFC_LINK_DOWN:
  910. fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
  911. break;
  912. case IBMVFC_LINK_DEAD:
  913. case IBMVFC_HOST_OFFLINE:
  914. fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE;
  915. break;
  916. case IBMVFC_HALTED:
  917. fc_host_port_state(shost) = FC_PORTSTATE_BLOCKED;
  918. break;
  919. case IBMVFC_NO_CRQ:
  920. fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
  921. break;
  922. default:
  923. ibmvfc_log(vhost, 3, "Unknown port state: %d\n", vhost->state);
  924. fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
  925. break;
  926. }
  927. spin_unlock_irqrestore(shost->host_lock, flags);
  928. }
  929. /**
  930. * ibmvfc_set_rport_dev_loss_tmo - Set rport's device loss timeout
  931. * @rport: rport struct
  932. * @timeout: timeout value
  933. *
  934. * Return value:
  935. * none
  936. **/
  937. static void ibmvfc_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  938. {
  939. if (timeout)
  940. rport->dev_loss_tmo = timeout;
  941. else
  942. rport->dev_loss_tmo = 1;
  943. }
  944. /**
  945. * ibmvfc_release_tgt - Free memory allocated for a target
  946. * @kref: kref struct
  947. *
  948. **/
  949. static void ibmvfc_release_tgt(struct kref *kref)
  950. {
  951. struct ibmvfc_target *tgt = container_of(kref, struct ibmvfc_target, kref);
  952. kfree(tgt);
  953. }
  954. /**
  955. * ibmvfc_get_starget_node_name - Get SCSI target's node name
  956. * @starget: scsi target struct
  957. *
  958. * Return value:
  959. * none
  960. **/
  961. static void ibmvfc_get_starget_node_name(struct scsi_target *starget)
  962. {
  963. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  964. fc_starget_port_name(starget) = tgt ? tgt->ids.node_name : 0;
  965. if (tgt)
  966. kref_put(&tgt->kref, ibmvfc_release_tgt);
  967. }
  968. /**
  969. * ibmvfc_get_starget_port_name - Get SCSI target's port name
  970. * @starget: scsi target struct
  971. *
  972. * Return value:
  973. * none
  974. **/
  975. static void ibmvfc_get_starget_port_name(struct scsi_target *starget)
  976. {
  977. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  978. fc_starget_port_name(starget) = tgt ? tgt->ids.port_name : 0;
  979. if (tgt)
  980. kref_put(&tgt->kref, ibmvfc_release_tgt);
  981. }
  982. /**
  983. * ibmvfc_get_starget_port_id - Get SCSI target's port ID
  984. * @starget: scsi target struct
  985. *
  986. * Return value:
  987. * none
  988. **/
  989. static void ibmvfc_get_starget_port_id(struct scsi_target *starget)
  990. {
  991. struct ibmvfc_target *tgt = ibmvfc_get_target(starget);
  992. fc_starget_port_id(starget) = tgt ? tgt->scsi_id : -1;
  993. if (tgt)
  994. kref_put(&tgt->kref, ibmvfc_release_tgt);
  995. }
  996. /**
  997. * ibmvfc_wait_while_resetting - Wait while the host resets
  998. * @vhost: ibmvfc host struct
  999. *
  1000. * Return value:
  1001. * 0 on success / other on failure
  1002. **/
  1003. static int ibmvfc_wait_while_resetting(struct ibmvfc_host *vhost)
  1004. {
  1005. long timeout = wait_event_timeout(vhost->init_wait_q,
  1006. ((vhost->state == IBMVFC_ACTIVE ||
  1007. vhost->state == IBMVFC_HOST_OFFLINE ||
  1008. vhost->state == IBMVFC_LINK_DEAD) &&
  1009. vhost->action == IBMVFC_HOST_ACTION_NONE),
  1010. (init_timeout * HZ));
  1011. return timeout ? 0 : -EIO;
  1012. }
  1013. /**
  1014. * ibmvfc_issue_fc_host_lip - Re-initiate link initialization
  1015. * @shost: scsi host struct
  1016. *
  1017. * Return value:
  1018. * 0 on success / other on failure
  1019. **/
  1020. static int ibmvfc_issue_fc_host_lip(struct Scsi_Host *shost)
  1021. {
  1022. struct ibmvfc_host *vhost = shost_priv(shost);
  1023. dev_err(vhost->dev, "Initiating host LIP. Resetting connection\n");
  1024. ibmvfc_reset_host(vhost);
  1025. return ibmvfc_wait_while_resetting(vhost);
  1026. }
  1027. /**
  1028. * ibmvfc_gather_partition_info - Gather info about the LPAR
  1029. *
  1030. * Return value:
  1031. * none
  1032. **/
  1033. static void ibmvfc_gather_partition_info(struct ibmvfc_host *vhost)
  1034. {
  1035. struct device_node *rootdn;
  1036. const char *name;
  1037. const unsigned int *num;
  1038. rootdn = of_find_node_by_path("/");
  1039. if (!rootdn)
  1040. return;
  1041. name = of_get_property(rootdn, "ibm,partition-name", NULL);
  1042. if (name)
  1043. strncpy(vhost->partition_name, name, sizeof(vhost->partition_name));
  1044. num = of_get_property(rootdn, "ibm,partition-no", NULL);
  1045. if (num)
  1046. vhost->partition_number = *num;
  1047. of_node_put(rootdn);
  1048. }
  1049. /**
  1050. * ibmvfc_set_login_info - Setup info for NPIV login
  1051. * @vhost: ibmvfc host struct
  1052. *
  1053. * Return value:
  1054. * none
  1055. **/
  1056. static void ibmvfc_set_login_info(struct ibmvfc_host *vhost)
  1057. {
  1058. struct ibmvfc_npiv_login *login_info = &vhost->login_info;
  1059. struct device_node *of_node = vhost->dev->of_node;
  1060. const char *location;
  1061. memset(login_info, 0, sizeof(*login_info));
  1062. login_info->ostype = cpu_to_be32(IBMVFC_OS_LINUX);
  1063. login_info->max_dma_len = cpu_to_be64(IBMVFC_MAX_SECTORS << 9);
  1064. login_info->max_payload = cpu_to_be32(sizeof(struct ibmvfc_fcp_cmd_iu));
  1065. login_info->max_response = cpu_to_be32(sizeof(struct ibmvfc_fcp_rsp));
  1066. login_info->partition_num = cpu_to_be32(vhost->partition_number);
  1067. login_info->vfc_frame_version = cpu_to_be32(1);
  1068. login_info->fcp_version = cpu_to_be16(3);
  1069. login_info->flags = cpu_to_be16(IBMVFC_FLUSH_ON_HALT);
  1070. if (vhost->client_migrated)
  1071. login_info->flags |= cpu_to_be16(IBMVFC_CLIENT_MIGRATED);
  1072. login_info->max_cmds = cpu_to_be32(max_requests + IBMVFC_NUM_INTERNAL_REQ);
  1073. login_info->capabilities = cpu_to_be64(IBMVFC_CAN_MIGRATE);
  1074. login_info->async.va = cpu_to_be64(vhost->async_crq.msg_token);
  1075. login_info->async.len = cpu_to_be32(vhost->async_crq.size * sizeof(*vhost->async_crq.msgs));
  1076. strncpy(login_info->partition_name, vhost->partition_name, IBMVFC_MAX_NAME);
  1077. strncpy(login_info->device_name,
  1078. dev_name(&vhost->host->shost_gendev), IBMVFC_MAX_NAME);
  1079. location = of_get_property(of_node, "ibm,loc-code", NULL);
  1080. location = location ? location : dev_name(vhost->dev);
  1081. strncpy(login_info->drc_name, location, IBMVFC_MAX_NAME);
  1082. }
  1083. /**
  1084. * ibmvfc_init_event_pool - Allocates and initializes the event pool for a host
  1085. * @vhost: ibmvfc host who owns the event pool
  1086. *
  1087. * Returns zero on success.
  1088. **/
  1089. static int ibmvfc_init_event_pool(struct ibmvfc_host *vhost)
  1090. {
  1091. int i;
  1092. struct ibmvfc_event_pool *pool = &vhost->pool;
  1093. ENTER;
  1094. pool->size = max_requests + IBMVFC_NUM_INTERNAL_REQ;
  1095. pool->events = kcalloc(pool->size, sizeof(*pool->events), GFP_KERNEL);
  1096. if (!pool->events)
  1097. return -ENOMEM;
  1098. pool->iu_storage = dma_alloc_coherent(vhost->dev,
  1099. pool->size * sizeof(*pool->iu_storage),
  1100. &pool->iu_token, 0);
  1101. if (!pool->iu_storage) {
  1102. kfree(pool->events);
  1103. return -ENOMEM;
  1104. }
  1105. for (i = 0; i < pool->size; ++i) {
  1106. struct ibmvfc_event *evt = &pool->events[i];
  1107. atomic_set(&evt->free, 1);
  1108. evt->crq.valid = 0x80;
  1109. evt->crq.ioba = cpu_to_be64(pool->iu_token + (sizeof(*evt->xfer_iu) * i));
  1110. evt->xfer_iu = pool->iu_storage + i;
  1111. evt->vhost = vhost;
  1112. evt->ext_list = NULL;
  1113. list_add_tail(&evt->queue, &vhost->free);
  1114. }
  1115. LEAVE;
  1116. return 0;
  1117. }
  1118. /**
  1119. * ibmvfc_free_event_pool - Frees memory of the event pool of a host
  1120. * @vhost: ibmvfc host who owns the event pool
  1121. *
  1122. **/
  1123. static void ibmvfc_free_event_pool(struct ibmvfc_host *vhost)
  1124. {
  1125. int i;
  1126. struct ibmvfc_event_pool *pool = &vhost->pool;
  1127. ENTER;
  1128. for (i = 0; i < pool->size; ++i) {
  1129. list_del(&pool->events[i].queue);
  1130. BUG_ON(atomic_read(&pool->events[i].free) != 1);
  1131. if (pool->events[i].ext_list)
  1132. dma_pool_free(vhost->sg_pool,
  1133. pool->events[i].ext_list,
  1134. pool->events[i].ext_list_token);
  1135. }
  1136. kfree(pool->events);
  1137. dma_free_coherent(vhost->dev,
  1138. pool->size * sizeof(*pool->iu_storage),
  1139. pool->iu_storage, pool->iu_token);
  1140. LEAVE;
  1141. }
  1142. /**
  1143. * ibmvfc_get_event - Gets the next free event in pool
  1144. * @vhost: ibmvfc host struct
  1145. *
  1146. * Returns a free event from the pool.
  1147. **/
  1148. static struct ibmvfc_event *ibmvfc_get_event(struct ibmvfc_host *vhost)
  1149. {
  1150. struct ibmvfc_event *evt;
  1151. BUG_ON(list_empty(&vhost->free));
  1152. evt = list_entry(vhost->free.next, struct ibmvfc_event, queue);
  1153. atomic_set(&evt->free, 0);
  1154. list_del(&evt->queue);
  1155. return evt;
  1156. }
  1157. /**
  1158. * ibmvfc_init_event - Initialize fields in an event struct that are always
  1159. * required.
  1160. * @evt: The event
  1161. * @done: Routine to call when the event is responded to
  1162. * @format: SRP or MAD format
  1163. **/
  1164. static void ibmvfc_init_event(struct ibmvfc_event *evt,
  1165. void (*done) (struct ibmvfc_event *), u8 format)
  1166. {
  1167. evt->cmnd = NULL;
  1168. evt->sync_iu = NULL;
  1169. evt->crq.format = format;
  1170. evt->done = done;
  1171. evt->eh_comp = NULL;
  1172. }
  1173. /**
  1174. * ibmvfc_map_sg_list - Initialize scatterlist
  1175. * @scmd: scsi command struct
  1176. * @nseg: number of scatterlist segments
  1177. * @md: memory descriptor list to initialize
  1178. **/
  1179. static void ibmvfc_map_sg_list(struct scsi_cmnd *scmd, int nseg,
  1180. struct srp_direct_buf *md)
  1181. {
  1182. int i;
  1183. struct scatterlist *sg;
  1184. scsi_for_each_sg(scmd, sg, nseg, i) {
  1185. md[i].va = cpu_to_be64(sg_dma_address(sg));
  1186. md[i].len = cpu_to_be32(sg_dma_len(sg));
  1187. md[i].key = 0;
  1188. }
  1189. }
  1190. /**
  1191. * ibmvfc_map_sg_data - Maps dma for a scatterlist and initializes decriptor fields
  1192. * @scmd: Scsi_Cmnd with the scatterlist
  1193. * @evt: ibmvfc event struct
  1194. * @vfc_cmd: vfc_cmd that contains the memory descriptor
  1195. * @dev: device for which to map dma memory
  1196. *
  1197. * Returns:
  1198. * 0 on success / non-zero on failure
  1199. **/
  1200. static int ibmvfc_map_sg_data(struct scsi_cmnd *scmd,
  1201. struct ibmvfc_event *evt,
  1202. struct ibmvfc_cmd *vfc_cmd, struct device *dev)
  1203. {
  1204. int sg_mapped;
  1205. struct srp_direct_buf *data = &vfc_cmd->ioba;
  1206. struct ibmvfc_host *vhost = dev_get_drvdata(dev);
  1207. sg_mapped = scsi_dma_map(scmd);
  1208. if (!sg_mapped) {
  1209. vfc_cmd->flags |= cpu_to_be16(IBMVFC_NO_MEM_DESC);
  1210. return 0;
  1211. } else if (unlikely(sg_mapped < 0)) {
  1212. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1213. scmd_printk(KERN_ERR, scmd, "Failed to map DMA buffer for command\n");
  1214. return sg_mapped;
  1215. }
  1216. if (scmd->sc_data_direction == DMA_TO_DEVICE) {
  1217. vfc_cmd->flags |= cpu_to_be16(IBMVFC_WRITE);
  1218. vfc_cmd->iu.add_cdb_len |= IBMVFC_WRDATA;
  1219. } else {
  1220. vfc_cmd->flags |= cpu_to_be16(IBMVFC_READ);
  1221. vfc_cmd->iu.add_cdb_len |= IBMVFC_RDDATA;
  1222. }
  1223. if (sg_mapped == 1) {
  1224. ibmvfc_map_sg_list(scmd, sg_mapped, data);
  1225. return 0;
  1226. }
  1227. vfc_cmd->flags |= cpu_to_be16(IBMVFC_SCATTERLIST);
  1228. if (!evt->ext_list) {
  1229. evt->ext_list = dma_pool_alloc(vhost->sg_pool, GFP_ATOMIC,
  1230. &evt->ext_list_token);
  1231. if (!evt->ext_list) {
  1232. scsi_dma_unmap(scmd);
  1233. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1234. scmd_printk(KERN_ERR, scmd, "Can't allocate memory for scatterlist\n");
  1235. return -ENOMEM;
  1236. }
  1237. }
  1238. ibmvfc_map_sg_list(scmd, sg_mapped, evt->ext_list);
  1239. data->va = cpu_to_be64(evt->ext_list_token);
  1240. data->len = cpu_to_be32(sg_mapped * sizeof(struct srp_direct_buf));
  1241. data->key = 0;
  1242. return 0;
  1243. }
  1244. /**
  1245. * ibmvfc_timeout - Internal command timeout handler
  1246. * @evt: struct ibmvfc_event that timed out
  1247. *
  1248. * Called when an internally generated command times out
  1249. **/
  1250. static void ibmvfc_timeout(struct ibmvfc_event *evt)
  1251. {
  1252. struct ibmvfc_host *vhost = evt->vhost;
  1253. dev_err(vhost->dev, "Command timed out (%p). Resetting connection\n", evt);
  1254. ibmvfc_reset_host(vhost);
  1255. }
  1256. /**
  1257. * ibmvfc_send_event - Transforms event to u64 array and calls send_crq()
  1258. * @evt: event to be sent
  1259. * @vhost: ibmvfc host struct
  1260. * @timeout: timeout in seconds - 0 means do not time command
  1261. *
  1262. * Returns the value returned from ibmvfc_send_crq(). (Zero for success)
  1263. **/
  1264. static int ibmvfc_send_event(struct ibmvfc_event *evt,
  1265. struct ibmvfc_host *vhost, unsigned long timeout)
  1266. {
  1267. __be64 *crq_as_u64 = (__be64 *) &evt->crq;
  1268. int rc;
  1269. /* Copy the IU into the transfer area */
  1270. *evt->xfer_iu = evt->iu;
  1271. if (evt->crq.format == IBMVFC_CMD_FORMAT)
  1272. evt->xfer_iu->cmd.tag = cpu_to_be64((u64)evt);
  1273. else if (evt->crq.format == IBMVFC_MAD_FORMAT)
  1274. evt->xfer_iu->mad_common.tag = cpu_to_be64((u64)evt);
  1275. else
  1276. BUG();
  1277. list_add_tail(&evt->queue, &vhost->sent);
  1278. init_timer(&evt->timer);
  1279. if (timeout) {
  1280. evt->timer.data = (unsigned long) evt;
  1281. evt->timer.expires = jiffies + (timeout * HZ);
  1282. evt->timer.function = (void (*)(unsigned long))ibmvfc_timeout;
  1283. add_timer(&evt->timer);
  1284. }
  1285. mb();
  1286. if ((rc = ibmvfc_send_crq(vhost, be64_to_cpu(crq_as_u64[0]),
  1287. be64_to_cpu(crq_as_u64[1])))) {
  1288. list_del(&evt->queue);
  1289. del_timer(&evt->timer);
  1290. /* If send_crq returns H_CLOSED, return SCSI_MLQUEUE_HOST_BUSY.
  1291. * Firmware will send a CRQ with a transport event (0xFF) to
  1292. * tell this client what has happened to the transport. This
  1293. * will be handled in ibmvfc_handle_crq()
  1294. */
  1295. if (rc == H_CLOSED) {
  1296. if (printk_ratelimit())
  1297. dev_warn(vhost->dev, "Send warning. Receive queue closed, will retry.\n");
  1298. if (evt->cmnd)
  1299. scsi_dma_unmap(evt->cmnd);
  1300. ibmvfc_free_event(evt);
  1301. return SCSI_MLQUEUE_HOST_BUSY;
  1302. }
  1303. dev_err(vhost->dev, "Send error (rc=%d)\n", rc);
  1304. if (evt->cmnd) {
  1305. evt->cmnd->result = DID_ERROR << 16;
  1306. evt->done = ibmvfc_scsi_eh_done;
  1307. } else
  1308. evt->xfer_iu->mad_common.status = cpu_to_be16(IBMVFC_MAD_CRQ_ERROR);
  1309. evt->done(evt);
  1310. } else
  1311. ibmvfc_trc_start(evt);
  1312. return 0;
  1313. }
  1314. /**
  1315. * ibmvfc_log_error - Log an error for the failed command if appropriate
  1316. * @evt: ibmvfc event to log
  1317. *
  1318. **/
  1319. static void ibmvfc_log_error(struct ibmvfc_event *evt)
  1320. {
  1321. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  1322. struct ibmvfc_host *vhost = evt->vhost;
  1323. struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
  1324. struct scsi_cmnd *cmnd = evt->cmnd;
  1325. const char *err = unknown_error;
  1326. int index = ibmvfc_get_err_index(be16_to_cpu(vfc_cmd->status), be16_to_cpu(vfc_cmd->error));
  1327. int logerr = 0;
  1328. int rsp_code = 0;
  1329. if (index >= 0) {
  1330. logerr = cmd_status[index].log;
  1331. err = cmd_status[index].name;
  1332. }
  1333. if (!logerr && (vhost->log_level <= (IBMVFC_DEFAULT_LOG_LEVEL + 1)))
  1334. return;
  1335. if (rsp->flags & FCP_RSP_LEN_VALID)
  1336. rsp_code = rsp->data.info.rsp_code;
  1337. scmd_printk(KERN_ERR, cmnd, "Command (%02X) failed: %s (%x:%x) "
  1338. "flags: %x fcp_rsp: %x, resid=%d, scsi_status: %x\n",
  1339. cmnd->cmnd[0], err, vfc_cmd->status, vfc_cmd->error,
  1340. rsp->flags, rsp_code, scsi_get_resid(cmnd), rsp->scsi_status);
  1341. }
  1342. /**
  1343. * ibmvfc_relogin - Log back into the specified device
  1344. * @sdev: scsi device struct
  1345. *
  1346. **/
  1347. static void ibmvfc_relogin(struct scsi_device *sdev)
  1348. {
  1349. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  1350. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1351. struct ibmvfc_target *tgt;
  1352. list_for_each_entry(tgt, &vhost->targets, queue) {
  1353. if (rport == tgt->rport) {
  1354. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  1355. break;
  1356. }
  1357. }
  1358. ibmvfc_reinit_host(vhost);
  1359. }
  1360. /**
  1361. * ibmvfc_scsi_done - Handle responses from commands
  1362. * @evt: ibmvfc event to be handled
  1363. *
  1364. * Used as a callback when sending scsi cmds.
  1365. **/
  1366. static void ibmvfc_scsi_done(struct ibmvfc_event *evt)
  1367. {
  1368. struct ibmvfc_cmd *vfc_cmd = &evt->xfer_iu->cmd;
  1369. struct ibmvfc_fcp_rsp *rsp = &vfc_cmd->rsp;
  1370. struct scsi_cmnd *cmnd = evt->cmnd;
  1371. u32 rsp_len = 0;
  1372. u32 sense_len = be32_to_cpu(rsp->fcp_sense_len);
  1373. if (cmnd) {
  1374. if (be16_to_cpu(vfc_cmd->response_flags) & IBMVFC_ADAPTER_RESID_VALID)
  1375. scsi_set_resid(cmnd, be32_to_cpu(vfc_cmd->adapter_resid));
  1376. else if (rsp->flags & FCP_RESID_UNDER)
  1377. scsi_set_resid(cmnd, be32_to_cpu(rsp->fcp_resid));
  1378. else
  1379. scsi_set_resid(cmnd, 0);
  1380. if (vfc_cmd->status) {
  1381. cmnd->result = ibmvfc_get_err_result(vfc_cmd);
  1382. if (rsp->flags & FCP_RSP_LEN_VALID)
  1383. rsp_len = be32_to_cpu(rsp->fcp_rsp_len);
  1384. if ((sense_len + rsp_len) > SCSI_SENSE_BUFFERSIZE)
  1385. sense_len = SCSI_SENSE_BUFFERSIZE - rsp_len;
  1386. if ((rsp->flags & FCP_SNS_LEN_VALID) && rsp->fcp_sense_len && rsp_len <= 8)
  1387. memcpy(cmnd->sense_buffer, rsp->data.sense + rsp_len, sense_len);
  1388. if ((be16_to_cpu(vfc_cmd->status) & IBMVFC_VIOS_FAILURE) &&
  1389. (be16_to_cpu(vfc_cmd->error) == IBMVFC_PLOGI_REQUIRED))
  1390. ibmvfc_relogin(cmnd->device);
  1391. if (!cmnd->result && (!scsi_get_resid(cmnd) || (rsp->flags & FCP_RESID_OVER)))
  1392. cmnd->result = (DID_ERROR << 16);
  1393. ibmvfc_log_error(evt);
  1394. }
  1395. if (!cmnd->result &&
  1396. (scsi_bufflen(cmnd) - scsi_get_resid(cmnd) < cmnd->underflow))
  1397. cmnd->result = (DID_ERROR << 16);
  1398. scsi_dma_unmap(cmnd);
  1399. cmnd->scsi_done(cmnd);
  1400. }
  1401. if (evt->eh_comp)
  1402. complete(evt->eh_comp);
  1403. ibmvfc_free_event(evt);
  1404. }
  1405. /**
  1406. * ibmvfc_host_chkready - Check if the host can accept commands
  1407. * @vhost: struct ibmvfc host
  1408. *
  1409. * Returns:
  1410. * 1 if host can accept command / 0 if not
  1411. **/
  1412. static inline int ibmvfc_host_chkready(struct ibmvfc_host *vhost)
  1413. {
  1414. int result = 0;
  1415. switch (vhost->state) {
  1416. case IBMVFC_LINK_DEAD:
  1417. case IBMVFC_HOST_OFFLINE:
  1418. result = DID_NO_CONNECT << 16;
  1419. break;
  1420. case IBMVFC_NO_CRQ:
  1421. case IBMVFC_INITIALIZING:
  1422. case IBMVFC_HALTED:
  1423. case IBMVFC_LINK_DOWN:
  1424. result = DID_REQUEUE << 16;
  1425. break;
  1426. case IBMVFC_ACTIVE:
  1427. result = 0;
  1428. break;
  1429. };
  1430. return result;
  1431. }
  1432. /**
  1433. * ibmvfc_queuecommand - The queuecommand function of the scsi template
  1434. * @cmnd: struct scsi_cmnd to be executed
  1435. * @done: Callback function to be called when cmnd is completed
  1436. *
  1437. * Returns:
  1438. * 0 on success / other on failure
  1439. **/
  1440. static int ibmvfc_queuecommand_lck(struct scsi_cmnd *cmnd,
  1441. void (*done) (struct scsi_cmnd *))
  1442. {
  1443. struct ibmvfc_host *vhost = shost_priv(cmnd->device->host);
  1444. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  1445. struct ibmvfc_cmd *vfc_cmd;
  1446. struct ibmvfc_event *evt;
  1447. u8 tag[2];
  1448. int rc;
  1449. if (unlikely((rc = fc_remote_port_chkready(rport))) ||
  1450. unlikely((rc = ibmvfc_host_chkready(vhost)))) {
  1451. cmnd->result = rc;
  1452. done(cmnd);
  1453. return 0;
  1454. }
  1455. cmnd->result = (DID_OK << 16);
  1456. evt = ibmvfc_get_event(vhost);
  1457. ibmvfc_init_event(evt, ibmvfc_scsi_done, IBMVFC_CMD_FORMAT);
  1458. evt->cmnd = cmnd;
  1459. cmnd->scsi_done = done;
  1460. vfc_cmd = &evt->iu.cmd;
  1461. memset(vfc_cmd, 0, sizeof(*vfc_cmd));
  1462. vfc_cmd->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
  1463. vfc_cmd->resp.len = cpu_to_be32(sizeof(vfc_cmd->rsp));
  1464. vfc_cmd->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
  1465. vfc_cmd->payload_len = cpu_to_be32(sizeof(vfc_cmd->iu));
  1466. vfc_cmd->resp_len = cpu_to_be32(sizeof(vfc_cmd->rsp));
  1467. vfc_cmd->cancel_key = cpu_to_be32((unsigned long)cmnd->device->hostdata);
  1468. vfc_cmd->tgt_scsi_id = cpu_to_be64(rport->port_id);
  1469. vfc_cmd->iu.xfer_len = cpu_to_be32(scsi_bufflen(cmnd));
  1470. int_to_scsilun(cmnd->device->lun, &vfc_cmd->iu.lun);
  1471. memcpy(vfc_cmd->iu.cdb, cmnd->cmnd, cmnd->cmd_len);
  1472. if (scsi_populate_tag_msg(cmnd, tag)) {
  1473. vfc_cmd->task_tag = cpu_to_be64(tag[1]);
  1474. switch (tag[0]) {
  1475. case MSG_SIMPLE_TAG:
  1476. vfc_cmd->iu.pri_task_attr = IBMVFC_SIMPLE_TASK;
  1477. break;
  1478. case MSG_HEAD_TAG:
  1479. vfc_cmd->iu.pri_task_attr = IBMVFC_HEAD_OF_QUEUE;
  1480. break;
  1481. case MSG_ORDERED_TAG:
  1482. vfc_cmd->iu.pri_task_attr = IBMVFC_ORDERED_TASK;
  1483. break;
  1484. };
  1485. }
  1486. if (likely(!(rc = ibmvfc_map_sg_data(cmnd, evt, vfc_cmd, vhost->dev))))
  1487. return ibmvfc_send_event(evt, vhost, 0);
  1488. ibmvfc_free_event(evt);
  1489. if (rc == -ENOMEM)
  1490. return SCSI_MLQUEUE_HOST_BUSY;
  1491. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1492. scmd_printk(KERN_ERR, cmnd,
  1493. "Failed to map DMA buffer for command. rc=%d\n", rc);
  1494. cmnd->result = DID_ERROR << 16;
  1495. done(cmnd);
  1496. return 0;
  1497. }
  1498. static DEF_SCSI_QCMD(ibmvfc_queuecommand)
  1499. /**
  1500. * ibmvfc_sync_completion - Signal that a synchronous command has completed
  1501. * @evt: ibmvfc event struct
  1502. *
  1503. **/
  1504. static void ibmvfc_sync_completion(struct ibmvfc_event *evt)
  1505. {
  1506. /* copy the response back */
  1507. if (evt->sync_iu)
  1508. *evt->sync_iu = *evt->xfer_iu;
  1509. complete(&evt->comp);
  1510. }
  1511. /**
  1512. * ibmvfc_bsg_timeout_done - Completion handler for cancelling BSG commands
  1513. * @evt: struct ibmvfc_event
  1514. *
  1515. **/
  1516. static void ibmvfc_bsg_timeout_done(struct ibmvfc_event *evt)
  1517. {
  1518. struct ibmvfc_host *vhost = evt->vhost;
  1519. ibmvfc_free_event(evt);
  1520. vhost->aborting_passthru = 0;
  1521. dev_info(vhost->dev, "Passthru command cancelled\n");
  1522. }
  1523. /**
  1524. * ibmvfc_bsg_timeout - Handle a BSG timeout
  1525. * @job: struct fc_bsg_job that timed out
  1526. *
  1527. * Returns:
  1528. * 0 on success / other on failure
  1529. **/
  1530. static int ibmvfc_bsg_timeout(struct fc_bsg_job *job)
  1531. {
  1532. struct ibmvfc_host *vhost = shost_priv(job->shost);
  1533. unsigned long port_id = (unsigned long)job->dd_data;
  1534. struct ibmvfc_event *evt;
  1535. struct ibmvfc_tmf *tmf;
  1536. unsigned long flags;
  1537. int rc;
  1538. ENTER;
  1539. spin_lock_irqsave(vhost->host->host_lock, flags);
  1540. if (vhost->aborting_passthru || vhost->state != IBMVFC_ACTIVE) {
  1541. __ibmvfc_reset_host(vhost);
  1542. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1543. return 0;
  1544. }
  1545. vhost->aborting_passthru = 1;
  1546. evt = ibmvfc_get_event(vhost);
  1547. ibmvfc_init_event(evt, ibmvfc_bsg_timeout_done, IBMVFC_MAD_FORMAT);
  1548. tmf = &evt->iu.tmf;
  1549. memset(tmf, 0, sizeof(*tmf));
  1550. tmf->common.version = cpu_to_be32(1);
  1551. tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
  1552. tmf->common.length = cpu_to_be16(sizeof(*tmf));
  1553. tmf->scsi_id = cpu_to_be64(port_id);
  1554. tmf->cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
  1555. tmf->my_cancel_key = cpu_to_be32(IBMVFC_INTERNAL_CANCEL_KEY);
  1556. rc = ibmvfc_send_event(evt, vhost, default_timeout);
  1557. if (rc != 0) {
  1558. vhost->aborting_passthru = 0;
  1559. dev_err(vhost->dev, "Failed to send cancel event. rc=%d\n", rc);
  1560. rc = -EIO;
  1561. } else
  1562. dev_info(vhost->dev, "Cancelling passthru command to port id 0x%lx\n",
  1563. port_id);
  1564. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1565. LEAVE;
  1566. return rc;
  1567. }
  1568. /**
  1569. * ibmvfc_bsg_plogi - PLOGI into a target to handle a BSG command
  1570. * @vhost: struct ibmvfc_host to send command
  1571. * @port_id: port ID to send command
  1572. *
  1573. * Returns:
  1574. * 0 on success / other on failure
  1575. **/
  1576. static int ibmvfc_bsg_plogi(struct ibmvfc_host *vhost, unsigned int port_id)
  1577. {
  1578. struct ibmvfc_port_login *plogi;
  1579. struct ibmvfc_target *tgt;
  1580. struct ibmvfc_event *evt;
  1581. union ibmvfc_iu rsp_iu;
  1582. unsigned long flags;
  1583. int rc = 0, issue_login = 1;
  1584. ENTER;
  1585. spin_lock_irqsave(vhost->host->host_lock, flags);
  1586. list_for_each_entry(tgt, &vhost->targets, queue) {
  1587. if (tgt->scsi_id == port_id) {
  1588. issue_login = 0;
  1589. break;
  1590. }
  1591. }
  1592. if (!issue_login)
  1593. goto unlock_out;
  1594. if (unlikely((rc = ibmvfc_host_chkready(vhost))))
  1595. goto unlock_out;
  1596. evt = ibmvfc_get_event(vhost);
  1597. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
  1598. plogi = &evt->iu.plogi;
  1599. memset(plogi, 0, sizeof(*plogi));
  1600. plogi->common.version = cpu_to_be32(1);
  1601. plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
  1602. plogi->common.length = cpu_to_be16(sizeof(*plogi));
  1603. plogi->scsi_id = cpu_to_be64(port_id);
  1604. evt->sync_iu = &rsp_iu;
  1605. init_completion(&evt->comp);
  1606. rc = ibmvfc_send_event(evt, vhost, default_timeout);
  1607. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1608. if (rc)
  1609. return -EIO;
  1610. wait_for_completion(&evt->comp);
  1611. if (rsp_iu.plogi.common.status)
  1612. rc = -EIO;
  1613. spin_lock_irqsave(vhost->host->host_lock, flags);
  1614. ibmvfc_free_event(evt);
  1615. unlock_out:
  1616. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1617. LEAVE;
  1618. return rc;
  1619. }
  1620. /**
  1621. * ibmvfc_bsg_request - Handle a BSG request
  1622. * @job: struct fc_bsg_job to be executed
  1623. *
  1624. * Returns:
  1625. * 0 on success / other on failure
  1626. **/
  1627. static int ibmvfc_bsg_request(struct fc_bsg_job *job)
  1628. {
  1629. struct ibmvfc_host *vhost = shost_priv(job->shost);
  1630. struct fc_rport *rport = job->rport;
  1631. struct ibmvfc_passthru_mad *mad;
  1632. struct ibmvfc_event *evt;
  1633. union ibmvfc_iu rsp_iu;
  1634. unsigned long flags, port_id = -1;
  1635. unsigned int code = job->request->msgcode;
  1636. int rc = 0, req_seg, rsp_seg, issue_login = 0;
  1637. u32 fc_flags, rsp_len;
  1638. ENTER;
  1639. job->reply->reply_payload_rcv_len = 0;
  1640. if (rport)
  1641. port_id = rport->port_id;
  1642. switch (code) {
  1643. case FC_BSG_HST_ELS_NOLOGIN:
  1644. port_id = (job->request->rqst_data.h_els.port_id[0] << 16) |
  1645. (job->request->rqst_data.h_els.port_id[1] << 8) |
  1646. job->request->rqst_data.h_els.port_id[2];
  1647. case FC_BSG_RPT_ELS:
  1648. fc_flags = IBMVFC_FC_ELS;
  1649. break;
  1650. case FC_BSG_HST_CT:
  1651. issue_login = 1;
  1652. port_id = (job->request->rqst_data.h_ct.port_id[0] << 16) |
  1653. (job->request->rqst_data.h_ct.port_id[1] << 8) |
  1654. job->request->rqst_data.h_ct.port_id[2];
  1655. case FC_BSG_RPT_CT:
  1656. fc_flags = IBMVFC_FC_CT_IU;
  1657. break;
  1658. default:
  1659. return -ENOTSUPP;
  1660. };
  1661. if (port_id == -1)
  1662. return -EINVAL;
  1663. if (!mutex_trylock(&vhost->passthru_mutex))
  1664. return -EBUSY;
  1665. job->dd_data = (void *)port_id;
  1666. req_seg = dma_map_sg(vhost->dev, job->request_payload.sg_list,
  1667. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1668. if (!req_seg) {
  1669. mutex_unlock(&vhost->passthru_mutex);
  1670. return -ENOMEM;
  1671. }
  1672. rsp_seg = dma_map_sg(vhost->dev, job->reply_payload.sg_list,
  1673. job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1674. if (!rsp_seg) {
  1675. dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
  1676. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1677. mutex_unlock(&vhost->passthru_mutex);
  1678. return -ENOMEM;
  1679. }
  1680. if (req_seg > 1 || rsp_seg > 1) {
  1681. rc = -EINVAL;
  1682. goto out;
  1683. }
  1684. if (issue_login)
  1685. rc = ibmvfc_bsg_plogi(vhost, port_id);
  1686. spin_lock_irqsave(vhost->host->host_lock, flags);
  1687. if (unlikely(rc || (rport && (rc = fc_remote_port_chkready(rport)))) ||
  1688. unlikely((rc = ibmvfc_host_chkready(vhost)))) {
  1689. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1690. goto out;
  1691. }
  1692. evt = ibmvfc_get_event(vhost);
  1693. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
  1694. mad = &evt->iu.passthru;
  1695. memset(mad, 0, sizeof(*mad));
  1696. mad->common.version = cpu_to_be32(1);
  1697. mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
  1698. mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
  1699. mad->cmd_ioba.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) +
  1700. offsetof(struct ibmvfc_passthru_mad, iu));
  1701. mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
  1702. mad->iu.cmd_len = cpu_to_be32(job->request_payload.payload_len);
  1703. mad->iu.rsp_len = cpu_to_be32(job->reply_payload.payload_len);
  1704. mad->iu.flags = cpu_to_be32(fc_flags);
  1705. mad->iu.cancel_key = cpu_to_be32(IBMVFC_PASSTHRU_CANCEL_KEY);
  1706. mad->iu.cmd.va = cpu_to_be64(sg_dma_address(job->request_payload.sg_list));
  1707. mad->iu.cmd.len = cpu_to_be32(sg_dma_len(job->request_payload.sg_list));
  1708. mad->iu.rsp.va = cpu_to_be64(sg_dma_address(job->reply_payload.sg_list));
  1709. mad->iu.rsp.len = cpu_to_be32(sg_dma_len(job->reply_payload.sg_list));
  1710. mad->iu.scsi_id = cpu_to_be64(port_id);
  1711. mad->iu.tag = cpu_to_be64((u64)evt);
  1712. rsp_len = be32_to_cpu(mad->iu.rsp.len);
  1713. evt->sync_iu = &rsp_iu;
  1714. init_completion(&evt->comp);
  1715. rc = ibmvfc_send_event(evt, vhost, 0);
  1716. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1717. if (rc) {
  1718. rc = -EIO;
  1719. goto out;
  1720. }
  1721. wait_for_completion(&evt->comp);
  1722. if (rsp_iu.passthru.common.status)
  1723. rc = -EIO;
  1724. else
  1725. job->reply->reply_payload_rcv_len = rsp_len;
  1726. spin_lock_irqsave(vhost->host->host_lock, flags);
  1727. ibmvfc_free_event(evt);
  1728. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1729. job->reply->result = rc;
  1730. job->job_done(job);
  1731. rc = 0;
  1732. out:
  1733. dma_unmap_sg(vhost->dev, job->request_payload.sg_list,
  1734. job->request_payload.sg_cnt, DMA_TO_DEVICE);
  1735. dma_unmap_sg(vhost->dev, job->reply_payload.sg_list,
  1736. job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
  1737. mutex_unlock(&vhost->passthru_mutex);
  1738. LEAVE;
  1739. return rc;
  1740. }
  1741. /**
  1742. * ibmvfc_reset_device - Reset the device with the specified reset type
  1743. * @sdev: scsi device to reset
  1744. * @type: reset type
  1745. * @desc: reset type description for log messages
  1746. *
  1747. * Returns:
  1748. * 0 on success / other on failure
  1749. **/
  1750. static int ibmvfc_reset_device(struct scsi_device *sdev, int type, char *desc)
  1751. {
  1752. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  1753. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1754. struct ibmvfc_cmd *tmf;
  1755. struct ibmvfc_event *evt = NULL;
  1756. union ibmvfc_iu rsp_iu;
  1757. struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
  1758. int rsp_rc = -EBUSY;
  1759. unsigned long flags;
  1760. int rsp_code = 0;
  1761. spin_lock_irqsave(vhost->host->host_lock, flags);
  1762. if (vhost->state == IBMVFC_ACTIVE) {
  1763. evt = ibmvfc_get_event(vhost);
  1764. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
  1765. tmf = &evt->iu.cmd;
  1766. memset(tmf, 0, sizeof(*tmf));
  1767. tmf->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
  1768. tmf->resp.len = cpu_to_be32(sizeof(tmf->rsp));
  1769. tmf->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
  1770. tmf->payload_len = cpu_to_be32(sizeof(tmf->iu));
  1771. tmf->resp_len = cpu_to_be32(sizeof(tmf->rsp));
  1772. tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
  1773. tmf->tgt_scsi_id = cpu_to_be64(rport->port_id);
  1774. int_to_scsilun(sdev->lun, &tmf->iu.lun);
  1775. tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
  1776. tmf->iu.tmf_flags = type;
  1777. evt->sync_iu = &rsp_iu;
  1778. init_completion(&evt->comp);
  1779. rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
  1780. }
  1781. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1782. if (rsp_rc != 0) {
  1783. sdev_printk(KERN_ERR, sdev, "Failed to send %s reset event. rc=%d\n",
  1784. desc, rsp_rc);
  1785. return -EIO;
  1786. }
  1787. sdev_printk(KERN_INFO, sdev, "Resetting %s\n", desc);
  1788. wait_for_completion(&evt->comp);
  1789. if (rsp_iu.cmd.status)
  1790. rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
  1791. if (rsp_code) {
  1792. if (fc_rsp->flags & FCP_RSP_LEN_VALID)
  1793. rsp_code = fc_rsp->data.info.rsp_code;
  1794. sdev_printk(KERN_ERR, sdev, "%s reset failed: %s (%x:%x) "
  1795. "flags: %x fcp_rsp: %x, scsi_status: %x\n", desc,
  1796. ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
  1797. rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
  1798. fc_rsp->scsi_status);
  1799. rsp_rc = -EIO;
  1800. } else
  1801. sdev_printk(KERN_INFO, sdev, "%s reset successful\n", desc);
  1802. spin_lock_irqsave(vhost->host->host_lock, flags);
  1803. ibmvfc_free_event(evt);
  1804. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1805. return rsp_rc;
  1806. }
  1807. /**
  1808. * ibmvfc_match_rport - Match function for specified remote port
  1809. * @evt: ibmvfc event struct
  1810. * @device: device to match (rport)
  1811. *
  1812. * Returns:
  1813. * 1 if event matches rport / 0 if event does not match rport
  1814. **/
  1815. static int ibmvfc_match_rport(struct ibmvfc_event *evt, void *rport)
  1816. {
  1817. struct fc_rport *cmd_rport;
  1818. if (evt->cmnd) {
  1819. cmd_rport = starget_to_rport(scsi_target(evt->cmnd->device));
  1820. if (cmd_rport == rport)
  1821. return 1;
  1822. }
  1823. return 0;
  1824. }
  1825. /**
  1826. * ibmvfc_match_target - Match function for specified target
  1827. * @evt: ibmvfc event struct
  1828. * @device: device to match (starget)
  1829. *
  1830. * Returns:
  1831. * 1 if event matches starget / 0 if event does not match starget
  1832. **/
  1833. static int ibmvfc_match_target(struct ibmvfc_event *evt, void *device)
  1834. {
  1835. if (evt->cmnd && scsi_target(evt->cmnd->device) == device)
  1836. return 1;
  1837. return 0;
  1838. }
  1839. /**
  1840. * ibmvfc_match_lun - Match function for specified LUN
  1841. * @evt: ibmvfc event struct
  1842. * @device: device to match (sdev)
  1843. *
  1844. * Returns:
  1845. * 1 if event matches sdev / 0 if event does not match sdev
  1846. **/
  1847. static int ibmvfc_match_lun(struct ibmvfc_event *evt, void *device)
  1848. {
  1849. if (evt->cmnd && evt->cmnd->device == device)
  1850. return 1;
  1851. return 0;
  1852. }
  1853. /**
  1854. * ibmvfc_wait_for_ops - Wait for ops to complete
  1855. * @vhost: ibmvfc host struct
  1856. * @device: device to match (starget or sdev)
  1857. * @match: match function
  1858. *
  1859. * Returns:
  1860. * SUCCESS / FAILED
  1861. **/
  1862. static int ibmvfc_wait_for_ops(struct ibmvfc_host *vhost, void *device,
  1863. int (*match) (struct ibmvfc_event *, void *))
  1864. {
  1865. struct ibmvfc_event *evt;
  1866. DECLARE_COMPLETION_ONSTACK(comp);
  1867. int wait;
  1868. unsigned long flags;
  1869. signed long timeout = IBMVFC_ABORT_WAIT_TIMEOUT * HZ;
  1870. ENTER;
  1871. do {
  1872. wait = 0;
  1873. spin_lock_irqsave(vhost->host->host_lock, flags);
  1874. list_for_each_entry(evt, &vhost->sent, queue) {
  1875. if (match(evt, device)) {
  1876. evt->eh_comp = &comp;
  1877. wait++;
  1878. }
  1879. }
  1880. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1881. if (wait) {
  1882. timeout = wait_for_completion_timeout(&comp, timeout);
  1883. if (!timeout) {
  1884. wait = 0;
  1885. spin_lock_irqsave(vhost->host->host_lock, flags);
  1886. list_for_each_entry(evt, &vhost->sent, queue) {
  1887. if (match(evt, device)) {
  1888. evt->eh_comp = NULL;
  1889. wait++;
  1890. }
  1891. }
  1892. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1893. if (wait)
  1894. dev_err(vhost->dev, "Timed out waiting for aborted commands\n");
  1895. LEAVE;
  1896. return wait ? FAILED : SUCCESS;
  1897. }
  1898. }
  1899. } while (wait);
  1900. LEAVE;
  1901. return SUCCESS;
  1902. }
  1903. /**
  1904. * ibmvfc_cancel_all - Cancel all outstanding commands to the device
  1905. * @sdev: scsi device to cancel commands
  1906. * @type: type of error recovery being performed
  1907. *
  1908. * This sends a cancel to the VIOS for the specified device. This does
  1909. * NOT send any abort to the actual device. That must be done separately.
  1910. *
  1911. * Returns:
  1912. * 0 on success / other on failure
  1913. **/
  1914. static int ibmvfc_cancel_all(struct scsi_device *sdev, int type)
  1915. {
  1916. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  1917. struct scsi_target *starget = scsi_target(sdev);
  1918. struct fc_rport *rport = starget_to_rport(starget);
  1919. struct ibmvfc_tmf *tmf;
  1920. struct ibmvfc_event *evt, *found_evt;
  1921. union ibmvfc_iu rsp;
  1922. int rsp_rc = -EBUSY;
  1923. unsigned long flags;
  1924. u16 status;
  1925. ENTER;
  1926. spin_lock_irqsave(vhost->host->host_lock, flags);
  1927. found_evt = NULL;
  1928. list_for_each_entry(evt, &vhost->sent, queue) {
  1929. if (evt->cmnd && evt->cmnd->device == sdev) {
  1930. found_evt = evt;
  1931. break;
  1932. }
  1933. }
  1934. if (!found_evt) {
  1935. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  1936. sdev_printk(KERN_INFO, sdev, "No events found to cancel\n");
  1937. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1938. return 0;
  1939. }
  1940. if (vhost->logged_in) {
  1941. evt = ibmvfc_get_event(vhost);
  1942. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_MAD_FORMAT);
  1943. tmf = &evt->iu.tmf;
  1944. memset(tmf, 0, sizeof(*tmf));
  1945. tmf->common.version = cpu_to_be32(1);
  1946. tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
  1947. tmf->common.length = cpu_to_be16(sizeof(*tmf));
  1948. tmf->scsi_id = cpu_to_be64(rport->port_id);
  1949. int_to_scsilun(sdev->lun, &tmf->lun);
  1950. if (!(be64_to_cpu(vhost->login_buf->resp.capabilities) & IBMVFC_CAN_SUPPRESS_ABTS))
  1951. type &= ~IBMVFC_TMF_SUPPRESS_ABTS;
  1952. if (vhost->state == IBMVFC_ACTIVE)
  1953. tmf->flags = cpu_to_be32((type | IBMVFC_TMF_LUA_VALID));
  1954. else
  1955. tmf->flags = cpu_to_be32(((type & IBMVFC_TMF_SUPPRESS_ABTS) | IBMVFC_TMF_LUA_VALID));
  1956. tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
  1957. tmf->my_cancel_key = cpu_to_be32((unsigned long)starget->hostdata);
  1958. evt->sync_iu = &rsp;
  1959. init_completion(&evt->comp);
  1960. rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
  1961. }
  1962. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1963. if (rsp_rc != 0) {
  1964. sdev_printk(KERN_ERR, sdev, "Failed to send cancel event. rc=%d\n", rsp_rc);
  1965. /* If failure is received, the host adapter is most likely going
  1966. through reset, return success so the caller will wait for the command
  1967. being cancelled to get returned */
  1968. return 0;
  1969. }
  1970. sdev_printk(KERN_INFO, sdev, "Cancelling outstanding commands.\n");
  1971. wait_for_completion(&evt->comp);
  1972. status = be16_to_cpu(rsp.mad_common.status);
  1973. spin_lock_irqsave(vhost->host->host_lock, flags);
  1974. ibmvfc_free_event(evt);
  1975. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  1976. if (status != IBMVFC_MAD_SUCCESS) {
  1977. sdev_printk(KERN_WARNING, sdev, "Cancel failed with rc=%x\n", status);
  1978. switch (status) {
  1979. case IBMVFC_MAD_DRIVER_FAILED:
  1980. case IBMVFC_MAD_CRQ_ERROR:
  1981. /* Host adapter most likely going through reset, return success to
  1982. the caller will wait for the command being cancelled to get returned */
  1983. return 0;
  1984. default:
  1985. return -EIO;
  1986. };
  1987. }
  1988. sdev_printk(KERN_INFO, sdev, "Successfully cancelled outstanding commands\n");
  1989. return 0;
  1990. }
  1991. /**
  1992. * ibmvfc_match_key - Match function for specified cancel key
  1993. * @evt: ibmvfc event struct
  1994. * @key: cancel key to match
  1995. *
  1996. * Returns:
  1997. * 1 if event matches key / 0 if event does not match key
  1998. **/
  1999. static int ibmvfc_match_key(struct ibmvfc_event *evt, void *key)
  2000. {
  2001. unsigned long cancel_key = (unsigned long)key;
  2002. if (evt->crq.format == IBMVFC_CMD_FORMAT &&
  2003. be32_to_cpu(evt->iu.cmd.cancel_key) == cancel_key)
  2004. return 1;
  2005. return 0;
  2006. }
  2007. /**
  2008. * ibmvfc_match_evt - Match function for specified event
  2009. * @evt: ibmvfc event struct
  2010. * @match: event to match
  2011. *
  2012. * Returns:
  2013. * 1 if event matches key / 0 if event does not match key
  2014. **/
  2015. static int ibmvfc_match_evt(struct ibmvfc_event *evt, void *match)
  2016. {
  2017. if (evt == match)
  2018. return 1;
  2019. return 0;
  2020. }
  2021. /**
  2022. * ibmvfc_abort_task_set - Abort outstanding commands to the device
  2023. * @sdev: scsi device to abort commands
  2024. *
  2025. * This sends an Abort Task Set to the VIOS for the specified device. This does
  2026. * NOT send any cancel to the VIOS. That must be done separately.
  2027. *
  2028. * Returns:
  2029. * 0 on success / other on failure
  2030. **/
  2031. static int ibmvfc_abort_task_set(struct scsi_device *sdev)
  2032. {
  2033. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2034. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  2035. struct ibmvfc_cmd *tmf;
  2036. struct ibmvfc_event *evt, *found_evt;
  2037. union ibmvfc_iu rsp_iu;
  2038. struct ibmvfc_fcp_rsp *fc_rsp = &rsp_iu.cmd.rsp;
  2039. int rc, rsp_rc = -EBUSY;
  2040. unsigned long flags, timeout = IBMVFC_ABORT_TIMEOUT;
  2041. int rsp_code = 0;
  2042. spin_lock_irqsave(vhost->host->host_lock, flags);
  2043. found_evt = NULL;
  2044. list_for_each_entry(evt, &vhost->sent, queue) {
  2045. if (evt->cmnd && evt->cmnd->device == sdev) {
  2046. found_evt = evt;
  2047. break;
  2048. }
  2049. }
  2050. if (!found_evt) {
  2051. if (vhost->log_level > IBMVFC_DEFAULT_LOG_LEVEL)
  2052. sdev_printk(KERN_INFO, sdev, "No events found to abort\n");
  2053. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2054. return 0;
  2055. }
  2056. if (vhost->state == IBMVFC_ACTIVE) {
  2057. evt = ibmvfc_get_event(vhost);
  2058. ibmvfc_init_event(evt, ibmvfc_sync_completion, IBMVFC_CMD_FORMAT);
  2059. tmf = &evt->iu.cmd;
  2060. memset(tmf, 0, sizeof(*tmf));
  2061. tmf->resp.va = cpu_to_be64(be64_to_cpu(evt->crq.ioba) + offsetof(struct ibmvfc_cmd, rsp));
  2062. tmf->resp.len = cpu_to_be32(sizeof(tmf->rsp));
  2063. tmf->frame_type = cpu_to_be32(IBMVFC_SCSI_FCP_TYPE);
  2064. tmf->payload_len = cpu_to_be32(sizeof(tmf->iu));
  2065. tmf->resp_len = cpu_to_be32(sizeof(tmf->rsp));
  2066. tmf->cancel_key = cpu_to_be32((unsigned long)sdev->hostdata);
  2067. tmf->tgt_scsi_id = cpu_to_be64(rport->port_id);
  2068. int_to_scsilun(sdev->lun, &tmf->iu.lun);
  2069. tmf->flags = cpu_to_be16((IBMVFC_NO_MEM_DESC | IBMVFC_TMF));
  2070. tmf->iu.tmf_flags = IBMVFC_ABORT_TASK_SET;
  2071. evt->sync_iu = &rsp_iu;
  2072. init_completion(&evt->comp);
  2073. rsp_rc = ibmvfc_send_event(evt, vhost, default_timeout);
  2074. }
  2075. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2076. if (rsp_rc != 0) {
  2077. sdev_printk(KERN_ERR, sdev, "Failed to send abort. rc=%d\n", rsp_rc);
  2078. return -EIO;
  2079. }
  2080. sdev_printk(KERN_INFO, sdev, "Aborting outstanding commands\n");
  2081. timeout = wait_for_completion_timeout(&evt->comp, timeout);
  2082. if (!timeout) {
  2083. rc = ibmvfc_cancel_all(sdev, 0);
  2084. if (!rc) {
  2085. rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
  2086. if (rc == SUCCESS)
  2087. rc = 0;
  2088. }
  2089. if (rc) {
  2090. sdev_printk(KERN_INFO, sdev, "Cancel failed, resetting host\n");
  2091. ibmvfc_reset_host(vhost);
  2092. rsp_rc = -EIO;
  2093. rc = ibmvfc_wait_for_ops(vhost, sdev->hostdata, ibmvfc_match_key);
  2094. if (rc == SUCCESS)
  2095. rsp_rc = 0;
  2096. rc = ibmvfc_wait_for_ops(vhost, evt, ibmvfc_match_evt);
  2097. if (rc != SUCCESS) {
  2098. spin_lock_irqsave(vhost->host->host_lock, flags);
  2099. ibmvfc_hard_reset_host(vhost);
  2100. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2101. rsp_rc = 0;
  2102. }
  2103. goto out;
  2104. }
  2105. }
  2106. if (rsp_iu.cmd.status)
  2107. rsp_code = ibmvfc_get_err_result(&rsp_iu.cmd);
  2108. if (rsp_code) {
  2109. if (fc_rsp->flags & FCP_RSP_LEN_VALID)
  2110. rsp_code = fc_rsp->data.info.rsp_code;
  2111. sdev_printk(KERN_ERR, sdev, "Abort failed: %s (%x:%x) "
  2112. "flags: %x fcp_rsp: %x, scsi_status: %x\n",
  2113. ibmvfc_get_cmd_error(be16_to_cpu(rsp_iu.cmd.status), be16_to_cpu(rsp_iu.cmd.error)),
  2114. rsp_iu.cmd.status, rsp_iu.cmd.error, fc_rsp->flags, rsp_code,
  2115. fc_rsp->scsi_status);
  2116. rsp_rc = -EIO;
  2117. } else
  2118. sdev_printk(KERN_INFO, sdev, "Abort successful\n");
  2119. out:
  2120. spin_lock_irqsave(vhost->host->host_lock, flags);
  2121. ibmvfc_free_event(evt);
  2122. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2123. return rsp_rc;
  2124. }
  2125. /**
  2126. * ibmvfc_eh_abort_handler - Abort a command
  2127. * @cmd: scsi command to abort
  2128. *
  2129. * Returns:
  2130. * SUCCESS / FAST_IO_FAIL / FAILED
  2131. **/
  2132. static int ibmvfc_eh_abort_handler(struct scsi_cmnd *cmd)
  2133. {
  2134. struct scsi_device *sdev = cmd->device;
  2135. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2136. int cancel_rc, block_rc;
  2137. int rc = FAILED;
  2138. ENTER;
  2139. block_rc = fc_block_scsi_eh(cmd);
  2140. ibmvfc_wait_while_resetting(vhost);
  2141. if (block_rc != FAST_IO_FAIL) {
  2142. cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_ABORT_TASK_SET);
  2143. ibmvfc_abort_task_set(sdev);
  2144. } else
  2145. cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
  2146. if (!cancel_rc)
  2147. rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
  2148. if (block_rc == FAST_IO_FAIL && rc != FAILED)
  2149. rc = FAST_IO_FAIL;
  2150. LEAVE;
  2151. return rc;
  2152. }
  2153. /**
  2154. * ibmvfc_eh_device_reset_handler - Reset a single LUN
  2155. * @cmd: scsi command struct
  2156. *
  2157. * Returns:
  2158. * SUCCESS / FAST_IO_FAIL / FAILED
  2159. **/
  2160. static int ibmvfc_eh_device_reset_handler(struct scsi_cmnd *cmd)
  2161. {
  2162. struct scsi_device *sdev = cmd->device;
  2163. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2164. int cancel_rc, block_rc, reset_rc = 0;
  2165. int rc = FAILED;
  2166. ENTER;
  2167. block_rc = fc_block_scsi_eh(cmd);
  2168. ibmvfc_wait_while_resetting(vhost);
  2169. if (block_rc != FAST_IO_FAIL) {
  2170. cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_LUN_RESET);
  2171. reset_rc = ibmvfc_reset_device(sdev, IBMVFC_LUN_RESET, "LUN");
  2172. } else
  2173. cancel_rc = ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
  2174. if (!cancel_rc && !reset_rc)
  2175. rc = ibmvfc_wait_for_ops(vhost, sdev, ibmvfc_match_lun);
  2176. if (block_rc == FAST_IO_FAIL && rc != FAILED)
  2177. rc = FAST_IO_FAIL;
  2178. LEAVE;
  2179. return rc;
  2180. }
  2181. /**
  2182. * ibmvfc_dev_cancel_all_noreset - Device iterated cancel all function
  2183. * @sdev: scsi device struct
  2184. * @data: return code
  2185. *
  2186. **/
  2187. static void ibmvfc_dev_cancel_all_noreset(struct scsi_device *sdev, void *data)
  2188. {
  2189. unsigned long *rc = data;
  2190. *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
  2191. }
  2192. /**
  2193. * ibmvfc_dev_cancel_all_reset - Device iterated cancel all function
  2194. * @sdev: scsi device struct
  2195. * @data: return code
  2196. *
  2197. **/
  2198. static void ibmvfc_dev_cancel_all_reset(struct scsi_device *sdev, void *data)
  2199. {
  2200. unsigned long *rc = data;
  2201. *rc |= ibmvfc_cancel_all(sdev, IBMVFC_TMF_TGT_RESET);
  2202. }
  2203. /**
  2204. * ibmvfc_eh_target_reset_handler - Reset the target
  2205. * @cmd: scsi command struct
  2206. *
  2207. * Returns:
  2208. * SUCCESS / FAST_IO_FAIL / FAILED
  2209. **/
  2210. static int ibmvfc_eh_target_reset_handler(struct scsi_cmnd *cmd)
  2211. {
  2212. struct scsi_device *sdev = cmd->device;
  2213. struct ibmvfc_host *vhost = shost_priv(sdev->host);
  2214. struct scsi_target *starget = scsi_target(sdev);
  2215. int block_rc;
  2216. int reset_rc = 0;
  2217. int rc = FAILED;
  2218. unsigned long cancel_rc = 0;
  2219. ENTER;
  2220. block_rc = fc_block_scsi_eh(cmd);
  2221. ibmvfc_wait_while_resetting(vhost);
  2222. if (block_rc != FAST_IO_FAIL) {
  2223. starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_reset);
  2224. reset_rc = ibmvfc_reset_device(sdev, IBMVFC_TARGET_RESET, "target");
  2225. } else
  2226. starget_for_each_device(starget, &cancel_rc, ibmvfc_dev_cancel_all_noreset);
  2227. if (!cancel_rc && !reset_rc)
  2228. rc = ibmvfc_wait_for_ops(vhost, starget, ibmvfc_match_target);
  2229. if (block_rc == FAST_IO_FAIL && rc != FAILED)
  2230. rc = FAST_IO_FAIL;
  2231. LEAVE;
  2232. return rc;
  2233. }
  2234. /**
  2235. * ibmvfc_eh_host_reset_handler - Reset the connection to the server
  2236. * @cmd: struct scsi_cmnd having problems
  2237. *
  2238. **/
  2239. static int ibmvfc_eh_host_reset_handler(struct scsi_cmnd *cmd)
  2240. {
  2241. int rc, block_rc;
  2242. struct ibmvfc_host *vhost = shost_priv(cmd->device->host);
  2243. block_rc = fc_block_scsi_eh(cmd);
  2244. dev_err(vhost->dev, "Resetting connection due to error recovery\n");
  2245. rc = ibmvfc_issue_fc_host_lip(vhost->host);
  2246. if (block_rc == FAST_IO_FAIL)
  2247. return FAST_IO_FAIL;
  2248. return rc ? FAILED : SUCCESS;
  2249. }
  2250. /**
  2251. * ibmvfc_terminate_rport_io - Terminate all pending I/O to the rport.
  2252. * @rport: rport struct
  2253. *
  2254. * Return value:
  2255. * none
  2256. **/
  2257. static void ibmvfc_terminate_rport_io(struct fc_rport *rport)
  2258. {
  2259. struct Scsi_Host *shost = rport_to_shost(rport);
  2260. struct ibmvfc_host *vhost = shost_priv(shost);
  2261. struct fc_rport *dev_rport;
  2262. struct scsi_device *sdev;
  2263. unsigned long rc;
  2264. ENTER;
  2265. shost_for_each_device(sdev, shost) {
  2266. dev_rport = starget_to_rport(scsi_target(sdev));
  2267. if (dev_rport != rport)
  2268. continue;
  2269. ibmvfc_cancel_all(sdev, IBMVFC_TMF_SUPPRESS_ABTS);
  2270. }
  2271. rc = ibmvfc_wait_for_ops(vhost, rport, ibmvfc_match_rport);
  2272. if (rc == FAILED)
  2273. ibmvfc_issue_fc_host_lip(shost);
  2274. LEAVE;
  2275. }
  2276. static const struct ibmvfc_async_desc ae_desc [] = {
  2277. { "PLOGI", IBMVFC_AE_ELS_PLOGI, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2278. { "LOGO", IBMVFC_AE_ELS_LOGO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2279. { "PRLO", IBMVFC_AE_ELS_PRLO, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2280. { "N-Port SCN", IBMVFC_AE_SCN_NPORT, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2281. { "Group SCN", IBMVFC_AE_SCN_GROUP, IBMVFC_DEFAULT_LOG_LEVEL + 1 },
  2282. { "Domain SCN", IBMVFC_AE_SCN_DOMAIN, IBMVFC_DEFAULT_LOG_LEVEL },
  2283. { "Fabric SCN", IBMVFC_AE_SCN_FABRIC, IBMVFC_DEFAULT_LOG_LEVEL },
  2284. { "Link Up", IBMVFC_AE_LINK_UP, IBMVFC_DEFAULT_LOG_LEVEL },
  2285. { "Link Down", IBMVFC_AE_LINK_DOWN, IBMVFC_DEFAULT_LOG_LEVEL },
  2286. { "Link Dead", IBMVFC_AE_LINK_DEAD, IBMVFC_DEFAULT_LOG_LEVEL },
  2287. { "Halt", IBMVFC_AE_HALT, IBMVFC_DEFAULT_LOG_LEVEL },
  2288. { "Resume", IBMVFC_AE_RESUME, IBMVFC_DEFAULT_LOG_LEVEL },
  2289. { "Adapter Failed", IBMVFC_AE_ADAPTER_FAILED, IBMVFC_DEFAULT_LOG_LEVEL },
  2290. };
  2291. static const struct ibmvfc_async_desc unknown_ae = {
  2292. "Unknown async", 0, IBMVFC_DEFAULT_LOG_LEVEL
  2293. };
  2294. /**
  2295. * ibmvfc_get_ae_desc - Get text description for async event
  2296. * @ae: async event
  2297. *
  2298. **/
  2299. static const struct ibmvfc_async_desc *ibmvfc_get_ae_desc(u64 ae)
  2300. {
  2301. int i;
  2302. for (i = 0; i < ARRAY_SIZE(ae_desc); i++)
  2303. if (ae_desc[i].ae == ae)
  2304. return &ae_desc[i];
  2305. return &unknown_ae;
  2306. }
  2307. static const struct {
  2308. enum ibmvfc_ae_link_state state;
  2309. const char *desc;
  2310. } link_desc [] = {
  2311. { IBMVFC_AE_LS_LINK_UP, " link up" },
  2312. { IBMVFC_AE_LS_LINK_BOUNCED, " link bounced" },
  2313. { IBMVFC_AE_LS_LINK_DOWN, " link down" },
  2314. { IBMVFC_AE_LS_LINK_DEAD, " link dead" },
  2315. };
  2316. /**
  2317. * ibmvfc_get_link_state - Get text description for link state
  2318. * @state: link state
  2319. *
  2320. **/
  2321. static const char *ibmvfc_get_link_state(enum ibmvfc_ae_link_state state)
  2322. {
  2323. int i;
  2324. for (i = 0; i < ARRAY_SIZE(link_desc); i++)
  2325. if (link_desc[i].state == state)
  2326. return link_desc[i].desc;
  2327. return "";
  2328. }
  2329. /**
  2330. * ibmvfc_handle_async - Handle an async event from the adapter
  2331. * @crq: crq to process
  2332. * @vhost: ibmvfc host struct
  2333. *
  2334. **/
  2335. static void ibmvfc_handle_async(struct ibmvfc_async_crq *crq,
  2336. struct ibmvfc_host *vhost)
  2337. {
  2338. const struct ibmvfc_async_desc *desc = ibmvfc_get_ae_desc(be64_to_cpu(crq->event));
  2339. struct ibmvfc_target *tgt;
  2340. ibmvfc_log(vhost, desc->log_level, "%s event received. scsi_id: %llx, wwpn: %llx,"
  2341. " node_name: %llx%s\n", desc->desc, crq->scsi_id, crq->wwpn, crq->node_name,
  2342. ibmvfc_get_link_state(crq->link_state));
  2343. switch (be64_to_cpu(crq->event)) {
  2344. case IBMVFC_AE_RESUME:
  2345. switch (crq->link_state) {
  2346. case IBMVFC_AE_LS_LINK_DOWN:
  2347. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  2348. break;
  2349. case IBMVFC_AE_LS_LINK_DEAD:
  2350. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  2351. break;
  2352. case IBMVFC_AE_LS_LINK_UP:
  2353. case IBMVFC_AE_LS_LINK_BOUNCED:
  2354. default:
  2355. vhost->events_to_log |= IBMVFC_AE_LINKUP;
  2356. vhost->delay_init = 1;
  2357. __ibmvfc_reset_host(vhost);
  2358. break;
  2359. };
  2360. break;
  2361. case IBMVFC_AE_LINK_UP:
  2362. vhost->events_to_log |= IBMVFC_AE_LINKUP;
  2363. vhost->delay_init = 1;
  2364. __ibmvfc_reset_host(vhost);
  2365. break;
  2366. case IBMVFC_AE_SCN_FABRIC:
  2367. case IBMVFC_AE_SCN_DOMAIN:
  2368. vhost->events_to_log |= IBMVFC_AE_RSCN;
  2369. if (vhost->state < IBMVFC_HALTED) {
  2370. vhost->delay_init = 1;
  2371. __ibmvfc_reset_host(vhost);
  2372. }
  2373. break;
  2374. case IBMVFC_AE_SCN_NPORT:
  2375. case IBMVFC_AE_SCN_GROUP:
  2376. vhost->events_to_log |= IBMVFC_AE_RSCN;
  2377. ibmvfc_reinit_host(vhost);
  2378. break;
  2379. case IBMVFC_AE_ELS_LOGO:
  2380. case IBMVFC_AE_ELS_PRLO:
  2381. case IBMVFC_AE_ELS_PLOGI:
  2382. list_for_each_entry(tgt, &vhost->targets, queue) {
  2383. if (!crq->scsi_id && !crq->wwpn && !crq->node_name)
  2384. break;
  2385. if (crq->scsi_id && cpu_to_be64(tgt->scsi_id) != crq->scsi_id)
  2386. continue;
  2387. if (crq->wwpn && cpu_to_be64(tgt->ids.port_name) != crq->wwpn)
  2388. continue;
  2389. if (crq->node_name && cpu_to_be64(tgt->ids.node_name) != crq->node_name)
  2390. continue;
  2391. if (tgt->need_login && be64_to_cpu(crq->event) == IBMVFC_AE_ELS_LOGO)
  2392. tgt->logo_rcvd = 1;
  2393. if (!tgt->need_login || be64_to_cpu(crq->event) == IBMVFC_AE_ELS_PLOGI) {
  2394. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  2395. ibmvfc_reinit_host(vhost);
  2396. }
  2397. }
  2398. break;
  2399. case IBMVFC_AE_LINK_DOWN:
  2400. case IBMVFC_AE_ADAPTER_FAILED:
  2401. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  2402. break;
  2403. case IBMVFC_AE_LINK_DEAD:
  2404. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  2405. break;
  2406. case IBMVFC_AE_HALT:
  2407. ibmvfc_link_down(vhost, IBMVFC_HALTED);
  2408. break;
  2409. default:
  2410. dev_err(vhost->dev, "Unknown async event received: %lld\n", crq->event);
  2411. break;
  2412. };
  2413. }
  2414. /**
  2415. * ibmvfc_handle_crq - Handles and frees received events in the CRQ
  2416. * @crq: Command/Response queue
  2417. * @vhost: ibmvfc host struct
  2418. *
  2419. **/
  2420. static void ibmvfc_handle_crq(struct ibmvfc_crq *crq, struct ibmvfc_host *vhost)
  2421. {
  2422. long rc;
  2423. struct ibmvfc_event *evt = (struct ibmvfc_event *)be64_to_cpu(crq->ioba);
  2424. switch (crq->valid) {
  2425. case IBMVFC_CRQ_INIT_RSP:
  2426. switch (crq->format) {
  2427. case IBMVFC_CRQ_INIT:
  2428. dev_info(vhost->dev, "Partner initialized\n");
  2429. /* Send back a response */
  2430. rc = ibmvfc_send_crq_init_complete(vhost);
  2431. if (rc == 0)
  2432. ibmvfc_init_host(vhost);
  2433. else
  2434. dev_err(vhost->dev, "Unable to send init rsp. rc=%ld\n", rc);
  2435. break;
  2436. case IBMVFC_CRQ_INIT_COMPLETE:
  2437. dev_info(vhost->dev, "Partner initialization complete\n");
  2438. ibmvfc_init_host(vhost);
  2439. break;
  2440. default:
  2441. dev_err(vhost->dev, "Unknown crq message type: %d\n", crq->format);
  2442. }
  2443. return;
  2444. case IBMVFC_CRQ_XPORT_EVENT:
  2445. vhost->state = IBMVFC_NO_CRQ;
  2446. vhost->logged_in = 0;
  2447. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  2448. if (crq->format == IBMVFC_PARTITION_MIGRATED) {
  2449. /* We need to re-setup the interpartition connection */
  2450. dev_info(vhost->dev, "Re-enabling adapter\n");
  2451. vhost->client_migrated = 1;
  2452. ibmvfc_purge_requests(vhost, DID_REQUEUE);
  2453. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  2454. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_REENABLE);
  2455. } else {
  2456. dev_err(vhost->dev, "Virtual adapter failed (rc=%d)\n", crq->format);
  2457. ibmvfc_purge_requests(vhost, DID_ERROR);
  2458. ibmvfc_link_down(vhost, IBMVFC_LINK_DOWN);
  2459. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_RESET);
  2460. }
  2461. return;
  2462. case IBMVFC_CRQ_CMD_RSP:
  2463. break;
  2464. default:
  2465. dev_err(vhost->dev, "Got an invalid message type 0x%02x\n", crq->valid);
  2466. return;
  2467. }
  2468. if (crq->format == IBMVFC_ASYNC_EVENT)
  2469. return;
  2470. /* The only kind of payload CRQs we should get are responses to
  2471. * things we send. Make sure this response is to something we
  2472. * actually sent
  2473. */
  2474. if (unlikely(!ibmvfc_valid_event(&vhost->pool, evt))) {
  2475. dev_err(vhost->dev, "Returned correlation_token 0x%08llx is invalid!\n",
  2476. crq->ioba);
  2477. return;
  2478. }
  2479. if (unlikely(atomic_read(&evt->free))) {
  2480. dev_err(vhost->dev, "Received duplicate correlation_token 0x%08llx!\n",
  2481. crq->ioba);
  2482. return;
  2483. }
  2484. del_timer(&evt->timer);
  2485. list_del(&evt->queue);
  2486. ibmvfc_trc_end(evt);
  2487. evt->done(evt);
  2488. }
  2489. /**
  2490. * ibmvfc_scan_finished - Check if the device scan is done.
  2491. * @shost: scsi host struct
  2492. * @time: current elapsed time
  2493. *
  2494. * Returns:
  2495. * 0 if scan is not done / 1 if scan is done
  2496. **/
  2497. static int ibmvfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
  2498. {
  2499. unsigned long flags;
  2500. struct ibmvfc_host *vhost = shost_priv(shost);
  2501. int done = 0;
  2502. spin_lock_irqsave(shost->host_lock, flags);
  2503. if (time >= (init_timeout * HZ)) {
  2504. dev_info(vhost->dev, "Scan taking longer than %d seconds, "
  2505. "continuing initialization\n", init_timeout);
  2506. done = 1;
  2507. }
  2508. if (vhost->scan_complete)
  2509. done = 1;
  2510. spin_unlock_irqrestore(shost->host_lock, flags);
  2511. return done;
  2512. }
  2513. /**
  2514. * ibmvfc_slave_alloc - Setup the device's task set value
  2515. * @sdev: struct scsi_device device to configure
  2516. *
  2517. * Set the device's task set value so that error handling works as
  2518. * expected.
  2519. *
  2520. * Returns:
  2521. * 0 on success / -ENXIO if device does not exist
  2522. **/
  2523. static int ibmvfc_slave_alloc(struct scsi_device *sdev)
  2524. {
  2525. struct Scsi_Host *shost = sdev->host;
  2526. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  2527. struct ibmvfc_host *vhost = shost_priv(shost);
  2528. unsigned long flags = 0;
  2529. if (!rport || fc_remote_port_chkready(rport))
  2530. return -ENXIO;
  2531. spin_lock_irqsave(shost->host_lock, flags);
  2532. sdev->hostdata = (void *)(unsigned long)vhost->task_set++;
  2533. spin_unlock_irqrestore(shost->host_lock, flags);
  2534. return 0;
  2535. }
  2536. /**
  2537. * ibmvfc_target_alloc - Setup the target's task set value
  2538. * @starget: struct scsi_target
  2539. *
  2540. * Set the target's task set value so that error handling works as
  2541. * expected.
  2542. *
  2543. * Returns:
  2544. * 0 on success / -ENXIO if device does not exist
  2545. **/
  2546. static int ibmvfc_target_alloc(struct scsi_target *starget)
  2547. {
  2548. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  2549. struct ibmvfc_host *vhost = shost_priv(shost);
  2550. unsigned long flags = 0;
  2551. spin_lock_irqsave(shost->host_lock, flags);
  2552. starget->hostdata = (void *)(unsigned long)vhost->task_set++;
  2553. spin_unlock_irqrestore(shost->host_lock, flags);
  2554. return 0;
  2555. }
  2556. /**
  2557. * ibmvfc_slave_configure - Configure the device
  2558. * @sdev: struct scsi_device device to configure
  2559. *
  2560. * Enable allow_restart for a device if it is a disk. Adjust the
  2561. * queue_depth here also.
  2562. *
  2563. * Returns:
  2564. * 0
  2565. **/
  2566. static int ibmvfc_slave_configure(struct scsi_device *sdev)
  2567. {
  2568. struct Scsi_Host *shost = sdev->host;
  2569. unsigned long flags = 0;
  2570. spin_lock_irqsave(shost->host_lock, flags);
  2571. if (sdev->type == TYPE_DISK)
  2572. sdev->allow_restart = 1;
  2573. if (sdev->tagged_supported) {
  2574. scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
  2575. scsi_activate_tcq(sdev, sdev->queue_depth);
  2576. } else
  2577. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  2578. spin_unlock_irqrestore(shost->host_lock, flags);
  2579. return 0;
  2580. }
  2581. /**
  2582. * ibmvfc_change_queue_depth - Change the device's queue depth
  2583. * @sdev: scsi device struct
  2584. * @qdepth: depth to set
  2585. * @reason: calling context
  2586. *
  2587. * Return value:
  2588. * actual depth set
  2589. **/
  2590. static int ibmvfc_change_queue_depth(struct scsi_device *sdev, int qdepth,
  2591. int reason)
  2592. {
  2593. if (reason != SCSI_QDEPTH_DEFAULT)
  2594. return -EOPNOTSUPP;
  2595. if (qdepth > IBMVFC_MAX_CMDS_PER_LUN)
  2596. qdepth = IBMVFC_MAX_CMDS_PER_LUN;
  2597. scsi_adjust_queue_depth(sdev, 0, qdepth);
  2598. return sdev->queue_depth;
  2599. }
  2600. /**
  2601. * ibmvfc_change_queue_type - Change the device's queue type
  2602. * @sdev: scsi device struct
  2603. * @tag_type: type of tags to use
  2604. *
  2605. * Return value:
  2606. * actual queue type set
  2607. **/
  2608. static int ibmvfc_change_queue_type(struct scsi_device *sdev, int tag_type)
  2609. {
  2610. if (sdev->tagged_supported) {
  2611. scsi_set_tag_type(sdev, tag_type);
  2612. if (tag_type)
  2613. scsi_activate_tcq(sdev, sdev->queue_depth);
  2614. else
  2615. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  2616. } else
  2617. tag_type = 0;
  2618. return tag_type;
  2619. }
  2620. static ssize_t ibmvfc_show_host_partition_name(struct device *dev,
  2621. struct device_attribute *attr, char *buf)
  2622. {
  2623. struct Scsi_Host *shost = class_to_shost(dev);
  2624. struct ibmvfc_host *vhost = shost_priv(shost);
  2625. return snprintf(buf, PAGE_SIZE, "%s\n",
  2626. vhost->login_buf->resp.partition_name);
  2627. }
  2628. static ssize_t ibmvfc_show_host_device_name(struct device *dev,
  2629. struct device_attribute *attr, char *buf)
  2630. {
  2631. struct Scsi_Host *shost = class_to_shost(dev);
  2632. struct ibmvfc_host *vhost = shost_priv(shost);
  2633. return snprintf(buf, PAGE_SIZE, "%s\n",
  2634. vhost->login_buf->resp.device_name);
  2635. }
  2636. static ssize_t ibmvfc_show_host_loc_code(struct device *dev,
  2637. struct device_attribute *attr, char *buf)
  2638. {
  2639. struct Scsi_Host *shost = class_to_shost(dev);
  2640. struct ibmvfc_host *vhost = shost_priv(shost);
  2641. return snprintf(buf, PAGE_SIZE, "%s\n",
  2642. vhost->login_buf->resp.port_loc_code);
  2643. }
  2644. static ssize_t ibmvfc_show_host_drc_name(struct device *dev,
  2645. struct device_attribute *attr, char *buf)
  2646. {
  2647. struct Scsi_Host *shost = class_to_shost(dev);
  2648. struct ibmvfc_host *vhost = shost_priv(shost);
  2649. return snprintf(buf, PAGE_SIZE, "%s\n",
  2650. vhost->login_buf->resp.drc_name);
  2651. }
  2652. static ssize_t ibmvfc_show_host_npiv_version(struct device *dev,
  2653. struct device_attribute *attr, char *buf)
  2654. {
  2655. struct Scsi_Host *shost = class_to_shost(dev);
  2656. struct ibmvfc_host *vhost = shost_priv(shost);
  2657. return snprintf(buf, PAGE_SIZE, "%d\n", vhost->login_buf->resp.version);
  2658. }
  2659. static ssize_t ibmvfc_show_host_capabilities(struct device *dev,
  2660. struct device_attribute *attr, char *buf)
  2661. {
  2662. struct Scsi_Host *shost = class_to_shost(dev);
  2663. struct ibmvfc_host *vhost = shost_priv(shost);
  2664. return snprintf(buf, PAGE_SIZE, "%llx\n", vhost->login_buf->resp.capabilities);
  2665. }
  2666. /**
  2667. * ibmvfc_show_log_level - Show the adapter's error logging level
  2668. * @dev: class device struct
  2669. * @buf: buffer
  2670. *
  2671. * Return value:
  2672. * number of bytes printed to buffer
  2673. **/
  2674. static ssize_t ibmvfc_show_log_level(struct device *dev,
  2675. struct device_attribute *attr, char *buf)
  2676. {
  2677. struct Scsi_Host *shost = class_to_shost(dev);
  2678. struct ibmvfc_host *vhost = shost_priv(shost);
  2679. unsigned long flags = 0;
  2680. int len;
  2681. spin_lock_irqsave(shost->host_lock, flags);
  2682. len = snprintf(buf, PAGE_SIZE, "%d\n", vhost->log_level);
  2683. spin_unlock_irqrestore(shost->host_lock, flags);
  2684. return len;
  2685. }
  2686. /**
  2687. * ibmvfc_store_log_level - Change the adapter's error logging level
  2688. * @dev: class device struct
  2689. * @buf: buffer
  2690. *
  2691. * Return value:
  2692. * number of bytes printed to buffer
  2693. **/
  2694. static ssize_t ibmvfc_store_log_level(struct device *dev,
  2695. struct device_attribute *attr,
  2696. const char *buf, size_t count)
  2697. {
  2698. struct Scsi_Host *shost = class_to_shost(dev);
  2699. struct ibmvfc_host *vhost = shost_priv(shost);
  2700. unsigned long flags = 0;
  2701. spin_lock_irqsave(shost->host_lock, flags);
  2702. vhost->log_level = simple_strtoul(buf, NULL, 10);
  2703. spin_unlock_irqrestore(shost->host_lock, flags);
  2704. return strlen(buf);
  2705. }
  2706. static DEVICE_ATTR(partition_name, S_IRUGO, ibmvfc_show_host_partition_name, NULL);
  2707. static DEVICE_ATTR(device_name, S_IRUGO, ibmvfc_show_host_device_name, NULL);
  2708. static DEVICE_ATTR(port_loc_code, S_IRUGO, ibmvfc_show_host_loc_code, NULL);
  2709. static DEVICE_ATTR(drc_name, S_IRUGO, ibmvfc_show_host_drc_name, NULL);
  2710. static DEVICE_ATTR(npiv_version, S_IRUGO, ibmvfc_show_host_npiv_version, NULL);
  2711. static DEVICE_ATTR(capabilities, S_IRUGO, ibmvfc_show_host_capabilities, NULL);
  2712. static DEVICE_ATTR(log_level, S_IRUGO | S_IWUSR,
  2713. ibmvfc_show_log_level, ibmvfc_store_log_level);
  2714. #ifdef CONFIG_SCSI_IBMVFC_TRACE
  2715. /**
  2716. * ibmvfc_read_trace - Dump the adapter trace
  2717. * @filp: open sysfs file
  2718. * @kobj: kobject struct
  2719. * @bin_attr: bin_attribute struct
  2720. * @buf: buffer
  2721. * @off: offset
  2722. * @count: buffer size
  2723. *
  2724. * Return value:
  2725. * number of bytes printed to buffer
  2726. **/
  2727. static ssize_t ibmvfc_read_trace(struct file *filp, struct kobject *kobj,
  2728. struct bin_attribute *bin_attr,
  2729. char *buf, loff_t off, size_t count)
  2730. {
  2731. struct device *dev = container_of(kobj, struct device, kobj);
  2732. struct Scsi_Host *shost = class_to_shost(dev);
  2733. struct ibmvfc_host *vhost = shost_priv(shost);
  2734. unsigned long flags = 0;
  2735. int size = IBMVFC_TRACE_SIZE;
  2736. char *src = (char *)vhost->trace;
  2737. if (off > size)
  2738. return 0;
  2739. if (off + count > size) {
  2740. size -= off;
  2741. count = size;
  2742. }
  2743. spin_lock_irqsave(shost->host_lock, flags);
  2744. memcpy(buf, &src[off], count);
  2745. spin_unlock_irqrestore(shost->host_lock, flags);
  2746. return count;
  2747. }
  2748. static struct bin_attribute ibmvfc_trace_attr = {
  2749. .attr = {
  2750. .name = "trace",
  2751. .mode = S_IRUGO,
  2752. },
  2753. .size = 0,
  2754. .read = ibmvfc_read_trace,
  2755. };
  2756. #endif
  2757. static struct device_attribute *ibmvfc_attrs[] = {
  2758. &dev_attr_partition_name,
  2759. &dev_attr_device_name,
  2760. &dev_attr_port_loc_code,
  2761. &dev_attr_drc_name,
  2762. &dev_attr_npiv_version,
  2763. &dev_attr_capabilities,
  2764. &dev_attr_log_level,
  2765. NULL
  2766. };
  2767. static struct scsi_host_template driver_template = {
  2768. .module = THIS_MODULE,
  2769. .name = "IBM POWER Virtual FC Adapter",
  2770. .proc_name = IBMVFC_NAME,
  2771. .queuecommand = ibmvfc_queuecommand,
  2772. .eh_abort_handler = ibmvfc_eh_abort_handler,
  2773. .eh_device_reset_handler = ibmvfc_eh_device_reset_handler,
  2774. .eh_target_reset_handler = ibmvfc_eh_target_reset_handler,
  2775. .eh_host_reset_handler = ibmvfc_eh_host_reset_handler,
  2776. .slave_alloc = ibmvfc_slave_alloc,
  2777. .slave_configure = ibmvfc_slave_configure,
  2778. .target_alloc = ibmvfc_target_alloc,
  2779. .scan_finished = ibmvfc_scan_finished,
  2780. .change_queue_depth = ibmvfc_change_queue_depth,
  2781. .change_queue_type = ibmvfc_change_queue_type,
  2782. .cmd_per_lun = 16,
  2783. .can_queue = IBMVFC_MAX_REQUESTS_DEFAULT,
  2784. .this_id = -1,
  2785. .sg_tablesize = SG_ALL,
  2786. .max_sectors = IBMVFC_MAX_SECTORS,
  2787. .use_clustering = ENABLE_CLUSTERING,
  2788. .shost_attrs = ibmvfc_attrs,
  2789. };
  2790. /**
  2791. * ibmvfc_next_async_crq - Returns the next entry in async queue
  2792. * @vhost: ibmvfc host struct
  2793. *
  2794. * Returns:
  2795. * Pointer to next entry in queue / NULL if empty
  2796. **/
  2797. static struct ibmvfc_async_crq *ibmvfc_next_async_crq(struct ibmvfc_host *vhost)
  2798. {
  2799. struct ibmvfc_async_crq_queue *async_crq = &vhost->async_crq;
  2800. struct ibmvfc_async_crq *crq;
  2801. crq = &async_crq->msgs[async_crq->cur];
  2802. if (crq->valid & 0x80) {
  2803. if (++async_crq->cur == async_crq->size)
  2804. async_crq->cur = 0;
  2805. rmb();
  2806. } else
  2807. crq = NULL;
  2808. return crq;
  2809. }
  2810. /**
  2811. * ibmvfc_next_crq - Returns the next entry in message queue
  2812. * @vhost: ibmvfc host struct
  2813. *
  2814. * Returns:
  2815. * Pointer to next entry in queue / NULL if empty
  2816. **/
  2817. static struct ibmvfc_crq *ibmvfc_next_crq(struct ibmvfc_host *vhost)
  2818. {
  2819. struct ibmvfc_crq_queue *queue = &vhost->crq;
  2820. struct ibmvfc_crq *crq;
  2821. crq = &queue->msgs[queue->cur];
  2822. if (crq->valid & 0x80) {
  2823. if (++queue->cur == queue->size)
  2824. queue->cur = 0;
  2825. rmb();
  2826. } else
  2827. crq = NULL;
  2828. return crq;
  2829. }
  2830. /**
  2831. * ibmvfc_interrupt - Interrupt handler
  2832. * @irq: number of irq to handle, not used
  2833. * @dev_instance: ibmvfc_host that received interrupt
  2834. *
  2835. * Returns:
  2836. * IRQ_HANDLED
  2837. **/
  2838. static irqreturn_t ibmvfc_interrupt(int irq, void *dev_instance)
  2839. {
  2840. struct ibmvfc_host *vhost = (struct ibmvfc_host *)dev_instance;
  2841. unsigned long flags;
  2842. spin_lock_irqsave(vhost->host->host_lock, flags);
  2843. vio_disable_interrupts(to_vio_dev(vhost->dev));
  2844. tasklet_schedule(&vhost->tasklet);
  2845. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2846. return IRQ_HANDLED;
  2847. }
  2848. /**
  2849. * ibmvfc_tasklet - Interrupt handler tasklet
  2850. * @data: ibmvfc host struct
  2851. *
  2852. * Returns:
  2853. * Nothing
  2854. **/
  2855. static void ibmvfc_tasklet(void *data)
  2856. {
  2857. struct ibmvfc_host *vhost = data;
  2858. struct vio_dev *vdev = to_vio_dev(vhost->dev);
  2859. struct ibmvfc_crq *crq;
  2860. struct ibmvfc_async_crq *async;
  2861. unsigned long flags;
  2862. int done = 0;
  2863. spin_lock_irqsave(vhost->host->host_lock, flags);
  2864. while (!done) {
  2865. /* Pull all the valid messages off the async CRQ */
  2866. while ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
  2867. ibmvfc_handle_async(async, vhost);
  2868. async->valid = 0;
  2869. wmb();
  2870. }
  2871. /* Pull all the valid messages off the CRQ */
  2872. while ((crq = ibmvfc_next_crq(vhost)) != NULL) {
  2873. ibmvfc_handle_crq(crq, vhost);
  2874. crq->valid = 0;
  2875. wmb();
  2876. }
  2877. vio_enable_interrupts(vdev);
  2878. if ((async = ibmvfc_next_async_crq(vhost)) != NULL) {
  2879. vio_disable_interrupts(vdev);
  2880. ibmvfc_handle_async(async, vhost);
  2881. async->valid = 0;
  2882. wmb();
  2883. } else if ((crq = ibmvfc_next_crq(vhost)) != NULL) {
  2884. vio_disable_interrupts(vdev);
  2885. ibmvfc_handle_crq(crq, vhost);
  2886. crq->valid = 0;
  2887. wmb();
  2888. } else
  2889. done = 1;
  2890. }
  2891. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  2892. }
  2893. /**
  2894. * ibmvfc_init_tgt - Set the next init job step for the target
  2895. * @tgt: ibmvfc target struct
  2896. * @job_step: job step to perform
  2897. *
  2898. **/
  2899. static void ibmvfc_init_tgt(struct ibmvfc_target *tgt,
  2900. void (*job_step) (struct ibmvfc_target *))
  2901. {
  2902. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT);
  2903. tgt->job_step = job_step;
  2904. wake_up(&tgt->vhost->work_wait_q);
  2905. }
  2906. /**
  2907. * ibmvfc_retry_tgt_init - Attempt to retry a step in target initialization
  2908. * @tgt: ibmvfc target struct
  2909. * @job_step: initialization job step
  2910. *
  2911. * Returns: 1 if step will be retried / 0 if not
  2912. *
  2913. **/
  2914. static int ibmvfc_retry_tgt_init(struct ibmvfc_target *tgt,
  2915. void (*job_step) (struct ibmvfc_target *))
  2916. {
  2917. if (++tgt->init_retries > IBMVFC_MAX_TGT_INIT_RETRIES) {
  2918. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  2919. wake_up(&tgt->vhost->work_wait_q);
  2920. return 0;
  2921. } else
  2922. ibmvfc_init_tgt(tgt, job_step);
  2923. return 1;
  2924. }
  2925. /* Defined in FC-LS */
  2926. static const struct {
  2927. int code;
  2928. int retry;
  2929. int logged_in;
  2930. } prli_rsp [] = {
  2931. { 0, 1, 0 },
  2932. { 1, 0, 1 },
  2933. { 2, 1, 0 },
  2934. { 3, 1, 0 },
  2935. { 4, 0, 0 },
  2936. { 5, 0, 0 },
  2937. { 6, 0, 1 },
  2938. { 7, 0, 0 },
  2939. { 8, 1, 0 },
  2940. };
  2941. /**
  2942. * ibmvfc_get_prli_rsp - Find PRLI response index
  2943. * @flags: PRLI response flags
  2944. *
  2945. **/
  2946. static int ibmvfc_get_prli_rsp(u16 flags)
  2947. {
  2948. int i;
  2949. int code = (flags & 0x0f00) >> 8;
  2950. for (i = 0; i < ARRAY_SIZE(prli_rsp); i++)
  2951. if (prli_rsp[i].code == code)
  2952. return i;
  2953. return 0;
  2954. }
  2955. /**
  2956. * ibmvfc_tgt_prli_done - Completion handler for Process Login
  2957. * @evt: ibmvfc event struct
  2958. *
  2959. **/
  2960. static void ibmvfc_tgt_prli_done(struct ibmvfc_event *evt)
  2961. {
  2962. struct ibmvfc_target *tgt = evt->tgt;
  2963. struct ibmvfc_host *vhost = evt->vhost;
  2964. struct ibmvfc_process_login *rsp = &evt->xfer_iu->prli;
  2965. struct ibmvfc_prli_svc_parms *parms = &rsp->parms;
  2966. u32 status = be16_to_cpu(rsp->common.status);
  2967. int index, level = IBMVFC_DEFAULT_LOG_LEVEL;
  2968. vhost->discovery_threads--;
  2969. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  2970. switch (status) {
  2971. case IBMVFC_MAD_SUCCESS:
  2972. tgt_dbg(tgt, "Process Login succeeded: %X %02X %04X\n",
  2973. parms->type, parms->flags, parms->service_parms);
  2974. if (parms->type == IBMVFC_SCSI_FCP_TYPE) {
  2975. index = ibmvfc_get_prli_rsp(be16_to_cpu(parms->flags));
  2976. if (prli_rsp[index].logged_in) {
  2977. if (be16_to_cpu(parms->flags) & IBMVFC_PRLI_EST_IMG_PAIR) {
  2978. tgt->need_login = 0;
  2979. tgt->ids.roles = 0;
  2980. if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_TARGET_FUNC)
  2981. tgt->ids.roles |= FC_PORT_ROLE_FCP_TARGET;
  2982. if (be32_to_cpu(parms->service_parms) & IBMVFC_PRLI_INITIATOR_FUNC)
  2983. tgt->ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
  2984. tgt->add_rport = 1;
  2985. } else
  2986. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  2987. } else if (prli_rsp[index].retry)
  2988. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
  2989. else
  2990. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  2991. } else
  2992. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  2993. break;
  2994. case IBMVFC_MAD_DRIVER_FAILED:
  2995. break;
  2996. case IBMVFC_MAD_CRQ_ERROR:
  2997. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
  2998. break;
  2999. case IBMVFC_MAD_FAILED:
  3000. default:
  3001. if ((be16_to_cpu(rsp->status) & IBMVFC_VIOS_FAILURE) &&
  3002. be16_to_cpu(rsp->error) == IBMVFC_PLOGI_REQUIRED)
  3003. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  3004. else if (tgt->logo_rcvd)
  3005. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  3006. else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
  3007. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_prli);
  3008. else
  3009. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3010. tgt_log(tgt, level, "Process Login failed: %s (%x:%x) rc=0x%02X\n",
  3011. ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
  3012. rsp->status, rsp->error, status);
  3013. break;
  3014. };
  3015. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3016. ibmvfc_free_event(evt);
  3017. wake_up(&vhost->work_wait_q);
  3018. }
  3019. /**
  3020. * ibmvfc_tgt_send_prli - Send a process login
  3021. * @tgt: ibmvfc target struct
  3022. *
  3023. **/
  3024. static void ibmvfc_tgt_send_prli(struct ibmvfc_target *tgt)
  3025. {
  3026. struct ibmvfc_process_login *prli;
  3027. struct ibmvfc_host *vhost = tgt->vhost;
  3028. struct ibmvfc_event *evt;
  3029. if (vhost->discovery_threads >= disc_threads)
  3030. return;
  3031. kref_get(&tgt->kref);
  3032. evt = ibmvfc_get_event(vhost);
  3033. vhost->discovery_threads++;
  3034. ibmvfc_init_event(evt, ibmvfc_tgt_prli_done, IBMVFC_MAD_FORMAT);
  3035. evt->tgt = tgt;
  3036. prli = &evt->iu.prli;
  3037. memset(prli, 0, sizeof(*prli));
  3038. prli->common.version = cpu_to_be32(1);
  3039. prli->common.opcode = cpu_to_be32(IBMVFC_PROCESS_LOGIN);
  3040. prli->common.length = cpu_to_be16(sizeof(*prli));
  3041. prli->scsi_id = cpu_to_be64(tgt->scsi_id);
  3042. prli->parms.type = IBMVFC_SCSI_FCP_TYPE;
  3043. prli->parms.flags = cpu_to_be16(IBMVFC_PRLI_EST_IMG_PAIR);
  3044. prli->parms.service_parms = cpu_to_be32(IBMVFC_PRLI_INITIATOR_FUNC);
  3045. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3046. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3047. vhost->discovery_threads--;
  3048. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3049. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3050. } else
  3051. tgt_dbg(tgt, "Sent process login\n");
  3052. }
  3053. /**
  3054. * ibmvfc_tgt_plogi_done - Completion handler for Port Login
  3055. * @evt: ibmvfc event struct
  3056. *
  3057. **/
  3058. static void ibmvfc_tgt_plogi_done(struct ibmvfc_event *evt)
  3059. {
  3060. struct ibmvfc_target *tgt = evt->tgt;
  3061. struct ibmvfc_host *vhost = evt->vhost;
  3062. struct ibmvfc_port_login *rsp = &evt->xfer_iu->plogi;
  3063. u32 status = be16_to_cpu(rsp->common.status);
  3064. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  3065. vhost->discovery_threads--;
  3066. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3067. switch (status) {
  3068. case IBMVFC_MAD_SUCCESS:
  3069. tgt_dbg(tgt, "Port Login succeeded\n");
  3070. if (tgt->ids.port_name &&
  3071. tgt->ids.port_name != wwn_to_u64(rsp->service_parms.port_name)) {
  3072. vhost->reinit = 1;
  3073. tgt_dbg(tgt, "Port re-init required\n");
  3074. break;
  3075. }
  3076. tgt->ids.node_name = wwn_to_u64(rsp->service_parms.node_name);
  3077. tgt->ids.port_name = wwn_to_u64(rsp->service_parms.port_name);
  3078. tgt->ids.port_id = tgt->scsi_id;
  3079. memcpy(&tgt->service_parms, &rsp->service_parms,
  3080. sizeof(tgt->service_parms));
  3081. memcpy(&tgt->service_parms_change, &rsp->service_parms_change,
  3082. sizeof(tgt->service_parms_change));
  3083. ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_prli);
  3084. break;
  3085. case IBMVFC_MAD_DRIVER_FAILED:
  3086. break;
  3087. case IBMVFC_MAD_CRQ_ERROR:
  3088. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  3089. break;
  3090. case IBMVFC_MAD_FAILED:
  3091. default:
  3092. if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
  3093. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_send_plogi);
  3094. else
  3095. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3096. tgt_log(tgt, level, "Port Login failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
  3097. ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)), rsp->status, rsp->error,
  3098. ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)), rsp->fc_type,
  3099. ibmvfc_get_ls_explain(be16_to_cpu(rsp->fc_explain)), rsp->fc_explain, status);
  3100. break;
  3101. };
  3102. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3103. ibmvfc_free_event(evt);
  3104. wake_up(&vhost->work_wait_q);
  3105. }
  3106. /**
  3107. * ibmvfc_tgt_send_plogi - Send PLOGI to the specified target
  3108. * @tgt: ibmvfc target struct
  3109. *
  3110. **/
  3111. static void ibmvfc_tgt_send_plogi(struct ibmvfc_target *tgt)
  3112. {
  3113. struct ibmvfc_port_login *plogi;
  3114. struct ibmvfc_host *vhost = tgt->vhost;
  3115. struct ibmvfc_event *evt;
  3116. if (vhost->discovery_threads >= disc_threads)
  3117. return;
  3118. kref_get(&tgt->kref);
  3119. tgt->logo_rcvd = 0;
  3120. evt = ibmvfc_get_event(vhost);
  3121. vhost->discovery_threads++;
  3122. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3123. ibmvfc_init_event(evt, ibmvfc_tgt_plogi_done, IBMVFC_MAD_FORMAT);
  3124. evt->tgt = tgt;
  3125. plogi = &evt->iu.plogi;
  3126. memset(plogi, 0, sizeof(*plogi));
  3127. plogi->common.version = cpu_to_be32(1);
  3128. plogi->common.opcode = cpu_to_be32(IBMVFC_PORT_LOGIN);
  3129. plogi->common.length = cpu_to_be16(sizeof(*plogi));
  3130. plogi->scsi_id = cpu_to_be64(tgt->scsi_id);
  3131. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3132. vhost->discovery_threads--;
  3133. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3134. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3135. } else
  3136. tgt_dbg(tgt, "Sent port login\n");
  3137. }
  3138. /**
  3139. * ibmvfc_tgt_implicit_logout_done - Completion handler for Implicit Logout MAD
  3140. * @evt: ibmvfc event struct
  3141. *
  3142. **/
  3143. static void ibmvfc_tgt_implicit_logout_done(struct ibmvfc_event *evt)
  3144. {
  3145. struct ibmvfc_target *tgt = evt->tgt;
  3146. struct ibmvfc_host *vhost = evt->vhost;
  3147. struct ibmvfc_implicit_logout *rsp = &evt->xfer_iu->implicit_logout;
  3148. u32 status = be16_to_cpu(rsp->common.status);
  3149. vhost->discovery_threads--;
  3150. ibmvfc_free_event(evt);
  3151. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3152. switch (status) {
  3153. case IBMVFC_MAD_SUCCESS:
  3154. tgt_dbg(tgt, "Implicit Logout succeeded\n");
  3155. break;
  3156. case IBMVFC_MAD_DRIVER_FAILED:
  3157. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3158. wake_up(&vhost->work_wait_q);
  3159. return;
  3160. case IBMVFC_MAD_FAILED:
  3161. default:
  3162. tgt_err(tgt, "Implicit Logout failed: rc=0x%02X\n", status);
  3163. break;
  3164. };
  3165. if (vhost->action == IBMVFC_HOST_ACTION_TGT_INIT)
  3166. ibmvfc_init_tgt(tgt, ibmvfc_tgt_send_plogi);
  3167. else if (vhost->action == IBMVFC_HOST_ACTION_QUERY_TGTS &&
  3168. tgt->scsi_id != tgt->new_scsi_id)
  3169. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3170. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3171. wake_up(&vhost->work_wait_q);
  3172. }
  3173. /**
  3174. * ibmvfc_tgt_implicit_logout - Initiate an Implicit Logout for specified target
  3175. * @tgt: ibmvfc target struct
  3176. *
  3177. **/
  3178. static void ibmvfc_tgt_implicit_logout(struct ibmvfc_target *tgt)
  3179. {
  3180. struct ibmvfc_implicit_logout *mad;
  3181. struct ibmvfc_host *vhost = tgt->vhost;
  3182. struct ibmvfc_event *evt;
  3183. if (vhost->discovery_threads >= disc_threads)
  3184. return;
  3185. kref_get(&tgt->kref);
  3186. evt = ibmvfc_get_event(vhost);
  3187. vhost->discovery_threads++;
  3188. ibmvfc_init_event(evt, ibmvfc_tgt_implicit_logout_done, IBMVFC_MAD_FORMAT);
  3189. evt->tgt = tgt;
  3190. mad = &evt->iu.implicit_logout;
  3191. memset(mad, 0, sizeof(*mad));
  3192. mad->common.version = cpu_to_be32(1);
  3193. mad->common.opcode = cpu_to_be32(IBMVFC_IMPLICIT_LOGOUT);
  3194. mad->common.length = cpu_to_be16(sizeof(*mad));
  3195. mad->old_scsi_id = cpu_to_be64(tgt->scsi_id);
  3196. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3197. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3198. vhost->discovery_threads--;
  3199. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3200. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3201. } else
  3202. tgt_dbg(tgt, "Sent Implicit Logout\n");
  3203. }
  3204. /**
  3205. * ibmvfc_adisc_needs_plogi - Does device need PLOGI?
  3206. * @mad: ibmvfc passthru mad struct
  3207. * @tgt: ibmvfc target struct
  3208. *
  3209. * Returns:
  3210. * 1 if PLOGI needed / 0 if PLOGI not needed
  3211. **/
  3212. static int ibmvfc_adisc_needs_plogi(struct ibmvfc_passthru_mad *mad,
  3213. struct ibmvfc_target *tgt)
  3214. {
  3215. if (memcmp(&mad->fc_iu.response[2], &tgt->ids.port_name,
  3216. sizeof(tgt->ids.port_name)))
  3217. return 1;
  3218. if (memcmp(&mad->fc_iu.response[4], &tgt->ids.node_name,
  3219. sizeof(tgt->ids.node_name)))
  3220. return 1;
  3221. if (be32_to_cpu(mad->fc_iu.response[6]) != tgt->scsi_id)
  3222. return 1;
  3223. return 0;
  3224. }
  3225. /**
  3226. * ibmvfc_tgt_adisc_done - Completion handler for ADISC
  3227. * @evt: ibmvfc event struct
  3228. *
  3229. **/
  3230. static void ibmvfc_tgt_adisc_done(struct ibmvfc_event *evt)
  3231. {
  3232. struct ibmvfc_target *tgt = evt->tgt;
  3233. struct ibmvfc_host *vhost = evt->vhost;
  3234. struct ibmvfc_passthru_mad *mad = &evt->xfer_iu->passthru;
  3235. u32 status = be16_to_cpu(mad->common.status);
  3236. u8 fc_reason, fc_explain;
  3237. vhost->discovery_threads--;
  3238. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3239. del_timer(&tgt->timer);
  3240. switch (status) {
  3241. case IBMVFC_MAD_SUCCESS:
  3242. tgt_dbg(tgt, "ADISC succeeded\n");
  3243. if (ibmvfc_adisc_needs_plogi(mad, tgt))
  3244. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3245. break;
  3246. case IBMVFC_MAD_DRIVER_FAILED:
  3247. break;
  3248. case IBMVFC_MAD_FAILED:
  3249. default:
  3250. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3251. fc_reason = (be32_to_cpu(mad->fc_iu.response[1]) & 0x00ff0000) >> 16;
  3252. fc_explain = (be32_to_cpu(mad->fc_iu.response[1]) & 0x0000ff00) >> 8;
  3253. tgt_info(tgt, "ADISC failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
  3254. ibmvfc_get_cmd_error(be16_to_cpu(mad->iu.status), be16_to_cpu(mad->iu.error)),
  3255. mad->iu.status, mad->iu.error,
  3256. ibmvfc_get_fc_type(fc_reason), fc_reason,
  3257. ibmvfc_get_ls_explain(fc_explain), fc_explain, status);
  3258. break;
  3259. };
  3260. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3261. ibmvfc_free_event(evt);
  3262. wake_up(&vhost->work_wait_q);
  3263. }
  3264. /**
  3265. * ibmvfc_init_passthru - Initialize an event struct for FC passthru
  3266. * @evt: ibmvfc event struct
  3267. *
  3268. **/
  3269. static void ibmvfc_init_passthru(struct ibmvfc_event *evt)
  3270. {
  3271. struct ibmvfc_passthru_mad *mad = &evt->iu.passthru;
  3272. memset(mad, 0, sizeof(*mad));
  3273. mad->common.version = cpu_to_be32(1);
  3274. mad->common.opcode = cpu_to_be32(IBMVFC_PASSTHRU);
  3275. mad->common.length = cpu_to_be16(sizeof(*mad) - sizeof(mad->fc_iu) - sizeof(mad->iu));
  3276. mad->cmd_ioba.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
  3277. offsetof(struct ibmvfc_passthru_mad, iu));
  3278. mad->cmd_ioba.len = cpu_to_be32(sizeof(mad->iu));
  3279. mad->iu.cmd_len = cpu_to_be32(sizeof(mad->fc_iu.payload));
  3280. mad->iu.rsp_len = cpu_to_be32(sizeof(mad->fc_iu.response));
  3281. mad->iu.cmd.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
  3282. offsetof(struct ibmvfc_passthru_mad, fc_iu) +
  3283. offsetof(struct ibmvfc_passthru_fc_iu, payload));
  3284. mad->iu.cmd.len = cpu_to_be32(sizeof(mad->fc_iu.payload));
  3285. mad->iu.rsp.va = cpu_to_be64((u64)be64_to_cpu(evt->crq.ioba) +
  3286. offsetof(struct ibmvfc_passthru_mad, fc_iu) +
  3287. offsetof(struct ibmvfc_passthru_fc_iu, response));
  3288. mad->iu.rsp.len = cpu_to_be32(sizeof(mad->fc_iu.response));
  3289. }
  3290. /**
  3291. * ibmvfc_tgt_adisc_cancel_done - Completion handler when cancelling an ADISC
  3292. * @evt: ibmvfc event struct
  3293. *
  3294. * Just cleanup this event struct. Everything else is handled by
  3295. * the ADISC completion handler. If the ADISC never actually comes
  3296. * back, we still have the timer running on the ADISC event struct
  3297. * which will fire and cause the CRQ to get reset.
  3298. *
  3299. **/
  3300. static void ibmvfc_tgt_adisc_cancel_done(struct ibmvfc_event *evt)
  3301. {
  3302. struct ibmvfc_host *vhost = evt->vhost;
  3303. struct ibmvfc_target *tgt = evt->tgt;
  3304. tgt_dbg(tgt, "ADISC cancel complete\n");
  3305. vhost->abort_threads--;
  3306. ibmvfc_free_event(evt);
  3307. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3308. wake_up(&vhost->work_wait_q);
  3309. }
  3310. /**
  3311. * ibmvfc_adisc_timeout - Handle an ADISC timeout
  3312. * @tgt: ibmvfc target struct
  3313. *
  3314. * If an ADISC times out, send a cancel. If the cancel times
  3315. * out, reset the CRQ. When the ADISC comes back as cancelled,
  3316. * log back into the target.
  3317. **/
  3318. static void ibmvfc_adisc_timeout(struct ibmvfc_target *tgt)
  3319. {
  3320. struct ibmvfc_host *vhost = tgt->vhost;
  3321. struct ibmvfc_event *evt;
  3322. struct ibmvfc_tmf *tmf;
  3323. unsigned long flags;
  3324. int rc;
  3325. tgt_dbg(tgt, "ADISC timeout\n");
  3326. spin_lock_irqsave(vhost->host->host_lock, flags);
  3327. if (vhost->abort_threads >= disc_threads ||
  3328. tgt->action != IBMVFC_TGT_ACTION_INIT_WAIT ||
  3329. vhost->state != IBMVFC_INITIALIZING ||
  3330. vhost->action != IBMVFC_HOST_ACTION_QUERY_TGTS) {
  3331. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3332. return;
  3333. }
  3334. vhost->abort_threads++;
  3335. kref_get(&tgt->kref);
  3336. evt = ibmvfc_get_event(vhost);
  3337. ibmvfc_init_event(evt, ibmvfc_tgt_adisc_cancel_done, IBMVFC_MAD_FORMAT);
  3338. evt->tgt = tgt;
  3339. tmf = &evt->iu.tmf;
  3340. memset(tmf, 0, sizeof(*tmf));
  3341. tmf->common.version = cpu_to_be32(1);
  3342. tmf->common.opcode = cpu_to_be32(IBMVFC_TMF_MAD);
  3343. tmf->common.length = cpu_to_be16(sizeof(*tmf));
  3344. tmf->scsi_id = cpu_to_be64(tgt->scsi_id);
  3345. tmf->cancel_key = cpu_to_be32(tgt->cancel_key);
  3346. rc = ibmvfc_send_event(evt, vhost, default_timeout);
  3347. if (rc) {
  3348. tgt_err(tgt, "Failed to send cancel event for ADISC. rc=%d\n", rc);
  3349. vhost->abort_threads--;
  3350. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3351. __ibmvfc_reset_host(vhost);
  3352. } else
  3353. tgt_dbg(tgt, "Attempting to cancel ADISC\n");
  3354. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3355. }
  3356. /**
  3357. * ibmvfc_tgt_adisc - Initiate an ADISC for specified target
  3358. * @tgt: ibmvfc target struct
  3359. *
  3360. * When sending an ADISC we end up with two timers running. The
  3361. * first timer is the timer in the ibmvfc target struct. If this
  3362. * fires, we send a cancel to the target. The second timer is the
  3363. * timer on the ibmvfc event for the ADISC, which is longer. If that
  3364. * fires, it means the ADISC timed out and our attempt to cancel it
  3365. * also failed, so we need to reset the CRQ.
  3366. **/
  3367. static void ibmvfc_tgt_adisc(struct ibmvfc_target *tgt)
  3368. {
  3369. struct ibmvfc_passthru_mad *mad;
  3370. struct ibmvfc_host *vhost = tgt->vhost;
  3371. struct ibmvfc_event *evt;
  3372. if (vhost->discovery_threads >= disc_threads)
  3373. return;
  3374. kref_get(&tgt->kref);
  3375. evt = ibmvfc_get_event(vhost);
  3376. vhost->discovery_threads++;
  3377. ibmvfc_init_event(evt, ibmvfc_tgt_adisc_done, IBMVFC_MAD_FORMAT);
  3378. evt->tgt = tgt;
  3379. ibmvfc_init_passthru(evt);
  3380. mad = &evt->iu.passthru;
  3381. mad->iu.flags = cpu_to_be32(IBMVFC_FC_ELS);
  3382. mad->iu.scsi_id = cpu_to_be64(tgt->scsi_id);
  3383. mad->iu.cancel_key = cpu_to_be32(tgt->cancel_key);
  3384. mad->fc_iu.payload[0] = cpu_to_be32(IBMVFC_ADISC);
  3385. memcpy(&mad->fc_iu.payload[2], &vhost->login_buf->resp.port_name,
  3386. sizeof(vhost->login_buf->resp.port_name));
  3387. memcpy(&mad->fc_iu.payload[4], &vhost->login_buf->resp.node_name,
  3388. sizeof(vhost->login_buf->resp.node_name));
  3389. mad->fc_iu.payload[6] = cpu_to_be32(be64_to_cpu(vhost->login_buf->resp.scsi_id) & 0x00ffffff);
  3390. if (timer_pending(&tgt->timer))
  3391. mod_timer(&tgt->timer, jiffies + (IBMVFC_ADISC_TIMEOUT * HZ));
  3392. else {
  3393. tgt->timer.data = (unsigned long) tgt;
  3394. tgt->timer.expires = jiffies + (IBMVFC_ADISC_TIMEOUT * HZ);
  3395. tgt->timer.function = (void (*)(unsigned long))ibmvfc_adisc_timeout;
  3396. add_timer(&tgt->timer);
  3397. }
  3398. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3399. if (ibmvfc_send_event(evt, vhost, IBMVFC_ADISC_PLUS_CANCEL_TIMEOUT)) {
  3400. vhost->discovery_threads--;
  3401. del_timer(&tgt->timer);
  3402. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3403. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3404. } else
  3405. tgt_dbg(tgt, "Sent ADISC\n");
  3406. }
  3407. /**
  3408. * ibmvfc_tgt_query_target_done - Completion handler for Query Target MAD
  3409. * @evt: ibmvfc event struct
  3410. *
  3411. **/
  3412. static void ibmvfc_tgt_query_target_done(struct ibmvfc_event *evt)
  3413. {
  3414. struct ibmvfc_target *tgt = evt->tgt;
  3415. struct ibmvfc_host *vhost = evt->vhost;
  3416. struct ibmvfc_query_tgt *rsp = &evt->xfer_iu->query_tgt;
  3417. u32 status = be16_to_cpu(rsp->common.status);
  3418. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  3419. vhost->discovery_threads--;
  3420. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3421. switch (status) {
  3422. case IBMVFC_MAD_SUCCESS:
  3423. tgt_dbg(tgt, "Query Target succeeded\n");
  3424. tgt->new_scsi_id = be64_to_cpu(rsp->scsi_id);
  3425. if (be64_to_cpu(rsp->scsi_id) != tgt->scsi_id)
  3426. ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
  3427. else
  3428. ibmvfc_init_tgt(tgt, ibmvfc_tgt_adisc);
  3429. break;
  3430. case IBMVFC_MAD_DRIVER_FAILED:
  3431. break;
  3432. case IBMVFC_MAD_CRQ_ERROR:
  3433. ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
  3434. break;
  3435. case IBMVFC_MAD_FAILED:
  3436. default:
  3437. if ((be16_to_cpu(rsp->status) & IBMVFC_FABRIC_MAPPED) == IBMVFC_FABRIC_MAPPED &&
  3438. be16_to_cpu(rsp->error) == IBMVFC_UNABLE_TO_PERFORM_REQ &&
  3439. be16_to_cpu(rsp->fc_explain) == IBMVFC_PORT_NAME_NOT_REG)
  3440. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3441. else if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
  3442. level += ibmvfc_retry_tgt_init(tgt, ibmvfc_tgt_query_target);
  3443. else
  3444. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DEL_RPORT);
  3445. tgt_log(tgt, level, "Query Target failed: %s (%x:%x) %s (%x) %s (%x) rc=0x%02X\n",
  3446. ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
  3447. rsp->status, rsp->error, ibmvfc_get_fc_type(be16_to_cpu(rsp->fc_type)),
  3448. rsp->fc_type, ibmvfc_get_gs_explain(be16_to_cpu(rsp->fc_explain)),
  3449. rsp->fc_explain, status);
  3450. break;
  3451. };
  3452. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3453. ibmvfc_free_event(evt);
  3454. wake_up(&vhost->work_wait_q);
  3455. }
  3456. /**
  3457. * ibmvfc_tgt_query_target - Initiate a Query Target for specified target
  3458. * @tgt: ibmvfc target struct
  3459. *
  3460. **/
  3461. static void ibmvfc_tgt_query_target(struct ibmvfc_target *tgt)
  3462. {
  3463. struct ibmvfc_query_tgt *query_tgt;
  3464. struct ibmvfc_host *vhost = tgt->vhost;
  3465. struct ibmvfc_event *evt;
  3466. if (vhost->discovery_threads >= disc_threads)
  3467. return;
  3468. kref_get(&tgt->kref);
  3469. evt = ibmvfc_get_event(vhost);
  3470. vhost->discovery_threads++;
  3471. evt->tgt = tgt;
  3472. ibmvfc_init_event(evt, ibmvfc_tgt_query_target_done, IBMVFC_MAD_FORMAT);
  3473. query_tgt = &evt->iu.query_tgt;
  3474. memset(query_tgt, 0, sizeof(*query_tgt));
  3475. query_tgt->common.version = cpu_to_be32(1);
  3476. query_tgt->common.opcode = cpu_to_be32(IBMVFC_QUERY_TARGET);
  3477. query_tgt->common.length = cpu_to_be16(sizeof(*query_tgt));
  3478. query_tgt->wwpn = cpu_to_be64(tgt->ids.port_name);
  3479. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_INIT_WAIT);
  3480. if (ibmvfc_send_event(evt, vhost, default_timeout)) {
  3481. vhost->discovery_threads--;
  3482. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_NONE);
  3483. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3484. } else
  3485. tgt_dbg(tgt, "Sent Query Target\n");
  3486. }
  3487. /**
  3488. * ibmvfc_alloc_target - Allocate and initialize an ibmvfc target
  3489. * @vhost: ibmvfc host struct
  3490. * @scsi_id: SCSI ID to allocate target for
  3491. *
  3492. * Returns:
  3493. * 0 on success / other on failure
  3494. **/
  3495. static int ibmvfc_alloc_target(struct ibmvfc_host *vhost, u64 scsi_id)
  3496. {
  3497. struct ibmvfc_target *tgt;
  3498. unsigned long flags;
  3499. spin_lock_irqsave(vhost->host->host_lock, flags);
  3500. list_for_each_entry(tgt, &vhost->targets, queue) {
  3501. if (tgt->scsi_id == scsi_id) {
  3502. if (tgt->need_login)
  3503. ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
  3504. goto unlock_out;
  3505. }
  3506. }
  3507. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3508. tgt = mempool_alloc(vhost->tgt_pool, GFP_NOIO);
  3509. if (!tgt) {
  3510. dev_err(vhost->dev, "Target allocation failure for scsi id %08llx\n",
  3511. scsi_id);
  3512. return -ENOMEM;
  3513. }
  3514. memset(tgt, 0, sizeof(*tgt));
  3515. tgt->scsi_id = scsi_id;
  3516. tgt->new_scsi_id = scsi_id;
  3517. tgt->vhost = vhost;
  3518. tgt->need_login = 1;
  3519. tgt->cancel_key = vhost->task_set++;
  3520. init_timer(&tgt->timer);
  3521. kref_init(&tgt->kref);
  3522. ibmvfc_init_tgt(tgt, ibmvfc_tgt_implicit_logout);
  3523. spin_lock_irqsave(vhost->host->host_lock, flags);
  3524. list_add_tail(&tgt->queue, &vhost->targets);
  3525. unlock_out:
  3526. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3527. return 0;
  3528. }
  3529. /**
  3530. * ibmvfc_alloc_targets - Allocate and initialize ibmvfc targets
  3531. * @vhost: ibmvfc host struct
  3532. *
  3533. * Returns:
  3534. * 0 on success / other on failure
  3535. **/
  3536. static int ibmvfc_alloc_targets(struct ibmvfc_host *vhost)
  3537. {
  3538. int i, rc;
  3539. for (i = 0, rc = 0; !rc && i < vhost->num_targets; i++)
  3540. rc = ibmvfc_alloc_target(vhost,
  3541. be32_to_cpu(vhost->disc_buf->scsi_id[i]) &
  3542. IBMVFC_DISC_TGT_SCSI_ID_MASK);
  3543. return rc;
  3544. }
  3545. /**
  3546. * ibmvfc_discover_targets_done - Completion handler for discover targets MAD
  3547. * @evt: ibmvfc event struct
  3548. *
  3549. **/
  3550. static void ibmvfc_discover_targets_done(struct ibmvfc_event *evt)
  3551. {
  3552. struct ibmvfc_host *vhost = evt->vhost;
  3553. struct ibmvfc_discover_targets *rsp = &evt->xfer_iu->discover_targets;
  3554. u32 mad_status = be16_to_cpu(rsp->common.status);
  3555. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  3556. switch (mad_status) {
  3557. case IBMVFC_MAD_SUCCESS:
  3558. ibmvfc_dbg(vhost, "Discover Targets succeeded\n");
  3559. vhost->num_targets = be32_to_cpu(rsp->num_written);
  3560. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_ALLOC_TGTS);
  3561. break;
  3562. case IBMVFC_MAD_FAILED:
  3563. level += ibmvfc_retry_host_init(vhost);
  3564. ibmvfc_log(vhost, level, "Discover Targets failed: %s (%x:%x)\n",
  3565. ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
  3566. rsp->status, rsp->error);
  3567. break;
  3568. case IBMVFC_MAD_DRIVER_FAILED:
  3569. break;
  3570. default:
  3571. dev_err(vhost->dev, "Invalid Discover Targets response: 0x%x\n", mad_status);
  3572. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3573. break;
  3574. }
  3575. ibmvfc_free_event(evt);
  3576. wake_up(&vhost->work_wait_q);
  3577. }
  3578. /**
  3579. * ibmvfc_discover_targets - Send Discover Targets MAD
  3580. * @vhost: ibmvfc host struct
  3581. *
  3582. **/
  3583. static void ibmvfc_discover_targets(struct ibmvfc_host *vhost)
  3584. {
  3585. struct ibmvfc_discover_targets *mad;
  3586. struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
  3587. ibmvfc_init_event(evt, ibmvfc_discover_targets_done, IBMVFC_MAD_FORMAT);
  3588. mad = &evt->iu.discover_targets;
  3589. memset(mad, 0, sizeof(*mad));
  3590. mad->common.version = cpu_to_be32(1);
  3591. mad->common.opcode = cpu_to_be32(IBMVFC_DISC_TARGETS);
  3592. mad->common.length = cpu_to_be16(sizeof(*mad));
  3593. mad->bufflen = cpu_to_be32(vhost->disc_buf_sz);
  3594. mad->buffer.va = cpu_to_be64(vhost->disc_buf_dma);
  3595. mad->buffer.len = cpu_to_be32(vhost->disc_buf_sz);
  3596. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
  3597. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  3598. ibmvfc_dbg(vhost, "Sent discover targets\n");
  3599. else
  3600. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3601. }
  3602. /**
  3603. * ibmvfc_npiv_login_done - Completion handler for NPIV Login
  3604. * @evt: ibmvfc event struct
  3605. *
  3606. **/
  3607. static void ibmvfc_npiv_login_done(struct ibmvfc_event *evt)
  3608. {
  3609. struct ibmvfc_host *vhost = evt->vhost;
  3610. u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_login.common.status);
  3611. struct ibmvfc_npiv_login_resp *rsp = &vhost->login_buf->resp;
  3612. unsigned int npiv_max_sectors;
  3613. int level = IBMVFC_DEFAULT_LOG_LEVEL;
  3614. switch (mad_status) {
  3615. case IBMVFC_MAD_SUCCESS:
  3616. ibmvfc_free_event(evt);
  3617. break;
  3618. case IBMVFC_MAD_FAILED:
  3619. if (ibmvfc_retry_cmd(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)))
  3620. level += ibmvfc_retry_host_init(vhost);
  3621. else
  3622. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3623. ibmvfc_log(vhost, level, "NPIV Login failed: %s (%x:%x)\n",
  3624. ibmvfc_get_cmd_error(be16_to_cpu(rsp->status), be16_to_cpu(rsp->error)),
  3625. rsp->status, rsp->error);
  3626. ibmvfc_free_event(evt);
  3627. return;
  3628. case IBMVFC_MAD_CRQ_ERROR:
  3629. ibmvfc_retry_host_init(vhost);
  3630. case IBMVFC_MAD_DRIVER_FAILED:
  3631. ibmvfc_free_event(evt);
  3632. return;
  3633. default:
  3634. dev_err(vhost->dev, "Invalid NPIV Login response: 0x%x\n", mad_status);
  3635. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3636. ibmvfc_free_event(evt);
  3637. return;
  3638. }
  3639. vhost->client_migrated = 0;
  3640. if (!(be32_to_cpu(rsp->flags) & IBMVFC_NATIVE_FC)) {
  3641. dev_err(vhost->dev, "Virtual adapter does not support FC. %x\n",
  3642. rsp->flags);
  3643. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3644. wake_up(&vhost->work_wait_q);
  3645. return;
  3646. }
  3647. if (be32_to_cpu(rsp->max_cmds) <= IBMVFC_NUM_INTERNAL_REQ) {
  3648. dev_err(vhost->dev, "Virtual adapter supported queue depth too small: %d\n",
  3649. rsp->max_cmds);
  3650. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3651. wake_up(&vhost->work_wait_q);
  3652. return;
  3653. }
  3654. vhost->logged_in = 1;
  3655. npiv_max_sectors = min((uint)(be64_to_cpu(rsp->max_dma_len) >> 9), IBMVFC_MAX_SECTORS);
  3656. dev_info(vhost->dev, "Host partition: %s, device: %s %s %s max sectors %u\n",
  3657. rsp->partition_name, rsp->device_name, rsp->port_loc_code,
  3658. rsp->drc_name, npiv_max_sectors);
  3659. fc_host_fabric_name(vhost->host) = be64_to_cpu(rsp->node_name);
  3660. fc_host_node_name(vhost->host) = be64_to_cpu(rsp->node_name);
  3661. fc_host_port_name(vhost->host) = be64_to_cpu(rsp->port_name);
  3662. fc_host_port_id(vhost->host) = be64_to_cpu(rsp->scsi_id);
  3663. fc_host_port_type(vhost->host) = FC_PORTTYPE_NPIV;
  3664. fc_host_supported_classes(vhost->host) = 0;
  3665. if (be32_to_cpu(rsp->service_parms.class1_parms[0]) & 0x80000000)
  3666. fc_host_supported_classes(vhost->host) |= FC_COS_CLASS1;
  3667. if (be32_to_cpu(rsp->service_parms.class2_parms[0]) & 0x80000000)
  3668. fc_host_supported_classes(vhost->host) |= FC_COS_CLASS2;
  3669. if (be32_to_cpu(rsp->service_parms.class3_parms[0]) & 0x80000000)
  3670. fc_host_supported_classes(vhost->host) |= FC_COS_CLASS3;
  3671. fc_host_maxframe_size(vhost->host) =
  3672. be16_to_cpu(rsp->service_parms.common.bb_rcv_sz) & 0x0fff;
  3673. vhost->host->can_queue = be32_to_cpu(rsp->max_cmds) - IBMVFC_NUM_INTERNAL_REQ;
  3674. vhost->host->max_sectors = npiv_max_sectors;
  3675. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  3676. wake_up(&vhost->work_wait_q);
  3677. }
  3678. /**
  3679. * ibmvfc_npiv_login - Sends NPIV login
  3680. * @vhost: ibmvfc host struct
  3681. *
  3682. **/
  3683. static void ibmvfc_npiv_login(struct ibmvfc_host *vhost)
  3684. {
  3685. struct ibmvfc_npiv_login_mad *mad;
  3686. struct ibmvfc_event *evt = ibmvfc_get_event(vhost);
  3687. ibmvfc_gather_partition_info(vhost);
  3688. ibmvfc_set_login_info(vhost);
  3689. ibmvfc_init_event(evt, ibmvfc_npiv_login_done, IBMVFC_MAD_FORMAT);
  3690. memcpy(vhost->login_buf, &vhost->login_info, sizeof(vhost->login_info));
  3691. mad = &evt->iu.npiv_login;
  3692. memset(mad, 0, sizeof(struct ibmvfc_npiv_login_mad));
  3693. mad->common.version = cpu_to_be32(1);
  3694. mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGIN);
  3695. mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_login_mad));
  3696. mad->buffer.va = cpu_to_be64(vhost->login_buf_dma);
  3697. mad->buffer.len = cpu_to_be32(sizeof(*vhost->login_buf));
  3698. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT_WAIT);
  3699. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  3700. ibmvfc_dbg(vhost, "Sent NPIV login\n");
  3701. else
  3702. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3703. };
  3704. /**
  3705. * ibmvfc_npiv_logout_done - Completion handler for NPIV Logout
  3706. * @vhost: ibmvfc host struct
  3707. *
  3708. **/
  3709. static void ibmvfc_npiv_logout_done(struct ibmvfc_event *evt)
  3710. {
  3711. struct ibmvfc_host *vhost = evt->vhost;
  3712. u32 mad_status = be16_to_cpu(evt->xfer_iu->npiv_logout.common.status);
  3713. ibmvfc_free_event(evt);
  3714. switch (mad_status) {
  3715. case IBMVFC_MAD_SUCCESS:
  3716. if (list_empty(&vhost->sent) &&
  3717. vhost->action == IBMVFC_HOST_ACTION_LOGO_WAIT) {
  3718. ibmvfc_init_host(vhost);
  3719. return;
  3720. }
  3721. break;
  3722. case IBMVFC_MAD_FAILED:
  3723. case IBMVFC_MAD_NOT_SUPPORTED:
  3724. case IBMVFC_MAD_CRQ_ERROR:
  3725. case IBMVFC_MAD_DRIVER_FAILED:
  3726. default:
  3727. ibmvfc_dbg(vhost, "NPIV Logout failed. 0x%X\n", mad_status);
  3728. break;
  3729. }
  3730. ibmvfc_hard_reset_host(vhost);
  3731. }
  3732. /**
  3733. * ibmvfc_npiv_logout - Issue an NPIV Logout
  3734. * @vhost: ibmvfc host struct
  3735. *
  3736. **/
  3737. static void ibmvfc_npiv_logout(struct ibmvfc_host *vhost)
  3738. {
  3739. struct ibmvfc_npiv_logout_mad *mad;
  3740. struct ibmvfc_event *evt;
  3741. evt = ibmvfc_get_event(vhost);
  3742. ibmvfc_init_event(evt, ibmvfc_npiv_logout_done, IBMVFC_MAD_FORMAT);
  3743. mad = &evt->iu.npiv_logout;
  3744. memset(mad, 0, sizeof(*mad));
  3745. mad->common.version = cpu_to_be32(1);
  3746. mad->common.opcode = cpu_to_be32(IBMVFC_NPIV_LOGOUT);
  3747. mad->common.length = cpu_to_be16(sizeof(struct ibmvfc_npiv_logout_mad));
  3748. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_LOGO_WAIT);
  3749. if (!ibmvfc_send_event(evt, vhost, default_timeout))
  3750. ibmvfc_dbg(vhost, "Sent NPIV logout\n");
  3751. else
  3752. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3753. }
  3754. /**
  3755. * ibmvfc_dev_init_to_do - Is there target initialization work to do?
  3756. * @vhost: ibmvfc host struct
  3757. *
  3758. * Returns:
  3759. * 1 if work to do / 0 if not
  3760. **/
  3761. static int ibmvfc_dev_init_to_do(struct ibmvfc_host *vhost)
  3762. {
  3763. struct ibmvfc_target *tgt;
  3764. list_for_each_entry(tgt, &vhost->targets, queue) {
  3765. if (tgt->action == IBMVFC_TGT_ACTION_INIT ||
  3766. tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
  3767. return 1;
  3768. }
  3769. return 0;
  3770. }
  3771. /**
  3772. * __ibmvfc_work_to_do - Is there task level work to do? (no locking)
  3773. * @vhost: ibmvfc host struct
  3774. *
  3775. * Returns:
  3776. * 1 if work to do / 0 if not
  3777. **/
  3778. static int __ibmvfc_work_to_do(struct ibmvfc_host *vhost)
  3779. {
  3780. struct ibmvfc_target *tgt;
  3781. if (kthread_should_stop())
  3782. return 1;
  3783. switch (vhost->action) {
  3784. case IBMVFC_HOST_ACTION_NONE:
  3785. case IBMVFC_HOST_ACTION_INIT_WAIT:
  3786. case IBMVFC_HOST_ACTION_LOGO_WAIT:
  3787. return 0;
  3788. case IBMVFC_HOST_ACTION_TGT_INIT:
  3789. case IBMVFC_HOST_ACTION_QUERY_TGTS:
  3790. if (vhost->discovery_threads == disc_threads)
  3791. return 0;
  3792. list_for_each_entry(tgt, &vhost->targets, queue)
  3793. if (tgt->action == IBMVFC_TGT_ACTION_INIT)
  3794. return 1;
  3795. list_for_each_entry(tgt, &vhost->targets, queue)
  3796. if (tgt->action == IBMVFC_TGT_ACTION_INIT_WAIT)
  3797. return 0;
  3798. return 1;
  3799. case IBMVFC_HOST_ACTION_LOGO:
  3800. case IBMVFC_HOST_ACTION_INIT:
  3801. case IBMVFC_HOST_ACTION_ALLOC_TGTS:
  3802. case IBMVFC_HOST_ACTION_TGT_DEL:
  3803. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  3804. case IBMVFC_HOST_ACTION_QUERY:
  3805. case IBMVFC_HOST_ACTION_RESET:
  3806. case IBMVFC_HOST_ACTION_REENABLE:
  3807. default:
  3808. break;
  3809. };
  3810. return 1;
  3811. }
  3812. /**
  3813. * ibmvfc_work_to_do - Is there task level work to do?
  3814. * @vhost: ibmvfc host struct
  3815. *
  3816. * Returns:
  3817. * 1 if work to do / 0 if not
  3818. **/
  3819. static int ibmvfc_work_to_do(struct ibmvfc_host *vhost)
  3820. {
  3821. unsigned long flags;
  3822. int rc;
  3823. spin_lock_irqsave(vhost->host->host_lock, flags);
  3824. rc = __ibmvfc_work_to_do(vhost);
  3825. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3826. return rc;
  3827. }
  3828. /**
  3829. * ibmvfc_log_ae - Log async events if necessary
  3830. * @vhost: ibmvfc host struct
  3831. * @events: events to log
  3832. *
  3833. **/
  3834. static void ibmvfc_log_ae(struct ibmvfc_host *vhost, int events)
  3835. {
  3836. if (events & IBMVFC_AE_RSCN)
  3837. fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_RSCN, 0);
  3838. if ((events & IBMVFC_AE_LINKDOWN) &&
  3839. vhost->state >= IBMVFC_HALTED)
  3840. fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
  3841. if ((events & IBMVFC_AE_LINKUP) &&
  3842. vhost->state == IBMVFC_INITIALIZING)
  3843. fc_host_post_event(vhost->host, fc_get_event_number(), FCH_EVT_LINKUP, 0);
  3844. }
  3845. /**
  3846. * ibmvfc_tgt_add_rport - Tell the FC transport about a new remote port
  3847. * @tgt: ibmvfc target struct
  3848. *
  3849. **/
  3850. static void ibmvfc_tgt_add_rport(struct ibmvfc_target *tgt)
  3851. {
  3852. struct ibmvfc_host *vhost = tgt->vhost;
  3853. struct fc_rport *rport;
  3854. unsigned long flags;
  3855. tgt_dbg(tgt, "Adding rport\n");
  3856. rport = fc_remote_port_add(vhost->host, 0, &tgt->ids);
  3857. spin_lock_irqsave(vhost->host->host_lock, flags);
  3858. if (rport && tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
  3859. tgt_dbg(tgt, "Deleting rport\n");
  3860. list_del(&tgt->queue);
  3861. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
  3862. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3863. fc_remote_port_delete(rport);
  3864. del_timer_sync(&tgt->timer);
  3865. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3866. return;
  3867. } else if (rport && tgt->action == IBMVFC_TGT_ACTION_DELETED_RPORT) {
  3868. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3869. return;
  3870. }
  3871. if (rport) {
  3872. tgt_dbg(tgt, "rport add succeeded\n");
  3873. tgt->rport = rport;
  3874. rport->maxframe_size = be16_to_cpu(tgt->service_parms.common.bb_rcv_sz) & 0x0fff;
  3875. rport->supported_classes = 0;
  3876. tgt->target_id = rport->scsi_target_id;
  3877. if (be32_to_cpu(tgt->service_parms.class1_parms[0]) & 0x80000000)
  3878. rport->supported_classes |= FC_COS_CLASS1;
  3879. if (be32_to_cpu(tgt->service_parms.class2_parms[0]) & 0x80000000)
  3880. rport->supported_classes |= FC_COS_CLASS2;
  3881. if (be32_to_cpu(tgt->service_parms.class3_parms[0]) & 0x80000000)
  3882. rport->supported_classes |= FC_COS_CLASS3;
  3883. if (rport->rqst_q)
  3884. blk_queue_max_segments(rport->rqst_q, 1);
  3885. } else
  3886. tgt_dbg(tgt, "rport add failed\n");
  3887. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3888. }
  3889. /**
  3890. * ibmvfc_do_work - Do task level work
  3891. * @vhost: ibmvfc host struct
  3892. *
  3893. **/
  3894. static void ibmvfc_do_work(struct ibmvfc_host *vhost)
  3895. {
  3896. struct ibmvfc_target *tgt;
  3897. unsigned long flags;
  3898. struct fc_rport *rport;
  3899. int rc;
  3900. ibmvfc_log_ae(vhost, vhost->events_to_log);
  3901. spin_lock_irqsave(vhost->host->host_lock, flags);
  3902. vhost->events_to_log = 0;
  3903. switch (vhost->action) {
  3904. case IBMVFC_HOST_ACTION_NONE:
  3905. case IBMVFC_HOST_ACTION_LOGO_WAIT:
  3906. case IBMVFC_HOST_ACTION_INIT_WAIT:
  3907. break;
  3908. case IBMVFC_HOST_ACTION_RESET:
  3909. vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
  3910. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3911. rc = ibmvfc_reset_crq(vhost);
  3912. spin_lock_irqsave(vhost->host->host_lock, flags);
  3913. if (rc == H_CLOSED)
  3914. vio_enable_interrupts(to_vio_dev(vhost->dev));
  3915. if (rc || (rc = ibmvfc_send_crq_init(vhost)) ||
  3916. (rc = vio_enable_interrupts(to_vio_dev(vhost->dev)))) {
  3917. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3918. dev_err(vhost->dev, "Error after reset (rc=%d)\n", rc);
  3919. }
  3920. break;
  3921. case IBMVFC_HOST_ACTION_REENABLE:
  3922. vhost->action = IBMVFC_HOST_ACTION_TGT_DEL;
  3923. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3924. rc = ibmvfc_reenable_crq_queue(vhost);
  3925. spin_lock_irqsave(vhost->host->host_lock, flags);
  3926. if (rc || (rc = ibmvfc_send_crq_init(vhost))) {
  3927. ibmvfc_link_down(vhost, IBMVFC_LINK_DEAD);
  3928. dev_err(vhost->dev, "Error after enable (rc=%d)\n", rc);
  3929. }
  3930. break;
  3931. case IBMVFC_HOST_ACTION_LOGO:
  3932. vhost->job_step(vhost);
  3933. break;
  3934. case IBMVFC_HOST_ACTION_INIT:
  3935. BUG_ON(vhost->state != IBMVFC_INITIALIZING);
  3936. if (vhost->delay_init) {
  3937. vhost->delay_init = 0;
  3938. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3939. ssleep(15);
  3940. return;
  3941. } else
  3942. vhost->job_step(vhost);
  3943. break;
  3944. case IBMVFC_HOST_ACTION_QUERY:
  3945. list_for_each_entry(tgt, &vhost->targets, queue)
  3946. ibmvfc_init_tgt(tgt, ibmvfc_tgt_query_target);
  3947. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY_TGTS);
  3948. break;
  3949. case IBMVFC_HOST_ACTION_QUERY_TGTS:
  3950. list_for_each_entry(tgt, &vhost->targets, queue) {
  3951. if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
  3952. tgt->job_step(tgt);
  3953. break;
  3954. }
  3955. }
  3956. if (!ibmvfc_dev_init_to_do(vhost))
  3957. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL);
  3958. break;
  3959. case IBMVFC_HOST_ACTION_TGT_DEL:
  3960. case IBMVFC_HOST_ACTION_TGT_DEL_FAILED:
  3961. list_for_each_entry(tgt, &vhost->targets, queue) {
  3962. if (tgt->action == IBMVFC_TGT_ACTION_DEL_RPORT) {
  3963. tgt_dbg(tgt, "Deleting rport\n");
  3964. rport = tgt->rport;
  3965. tgt->rport = NULL;
  3966. list_del(&tgt->queue);
  3967. ibmvfc_set_tgt_action(tgt, IBMVFC_TGT_ACTION_DELETED_RPORT);
  3968. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3969. if (rport)
  3970. fc_remote_port_delete(rport);
  3971. del_timer_sync(&tgt->timer);
  3972. kref_put(&tgt->kref, ibmvfc_release_tgt);
  3973. return;
  3974. }
  3975. }
  3976. if (vhost->state == IBMVFC_INITIALIZING) {
  3977. if (vhost->action == IBMVFC_HOST_ACTION_TGT_DEL_FAILED) {
  3978. if (vhost->reinit) {
  3979. vhost->reinit = 0;
  3980. scsi_block_requests(vhost->host);
  3981. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_QUERY);
  3982. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3983. } else {
  3984. ibmvfc_set_host_state(vhost, IBMVFC_ACTIVE);
  3985. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  3986. wake_up(&vhost->init_wait_q);
  3987. schedule_work(&vhost->rport_add_work_q);
  3988. vhost->init_retries = 0;
  3989. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  3990. scsi_unblock_requests(vhost->host);
  3991. }
  3992. return;
  3993. } else {
  3994. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_INIT);
  3995. vhost->job_step = ibmvfc_discover_targets;
  3996. }
  3997. } else {
  3998. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_NONE);
  3999. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4000. scsi_unblock_requests(vhost->host);
  4001. wake_up(&vhost->init_wait_q);
  4002. return;
  4003. }
  4004. break;
  4005. case IBMVFC_HOST_ACTION_ALLOC_TGTS:
  4006. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_INIT);
  4007. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4008. ibmvfc_alloc_targets(vhost);
  4009. spin_lock_irqsave(vhost->host->host_lock, flags);
  4010. break;
  4011. case IBMVFC_HOST_ACTION_TGT_INIT:
  4012. list_for_each_entry(tgt, &vhost->targets, queue) {
  4013. if (tgt->action == IBMVFC_TGT_ACTION_INIT) {
  4014. tgt->job_step(tgt);
  4015. break;
  4016. }
  4017. }
  4018. if (!ibmvfc_dev_init_to_do(vhost))
  4019. ibmvfc_set_host_action(vhost, IBMVFC_HOST_ACTION_TGT_DEL_FAILED);
  4020. break;
  4021. default:
  4022. break;
  4023. };
  4024. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4025. }
  4026. /**
  4027. * ibmvfc_work - Do task level work
  4028. * @data: ibmvfc host struct
  4029. *
  4030. * Returns:
  4031. * zero
  4032. **/
  4033. static int ibmvfc_work(void *data)
  4034. {
  4035. struct ibmvfc_host *vhost = data;
  4036. int rc;
  4037. set_user_nice(current, MIN_NICE);
  4038. while (1) {
  4039. rc = wait_event_interruptible(vhost->work_wait_q,
  4040. ibmvfc_work_to_do(vhost));
  4041. BUG_ON(rc);
  4042. if (kthread_should_stop())
  4043. break;
  4044. ibmvfc_do_work(vhost);
  4045. }
  4046. ibmvfc_dbg(vhost, "ibmvfc kthread exiting...\n");
  4047. return 0;
  4048. }
  4049. /**
  4050. * ibmvfc_init_crq - Initializes and registers CRQ with hypervisor
  4051. * @vhost: ibmvfc host struct
  4052. *
  4053. * Allocates a page for messages, maps it for dma, and registers
  4054. * the crq with the hypervisor.
  4055. *
  4056. * Return value:
  4057. * zero on success / other on failure
  4058. **/
  4059. static int ibmvfc_init_crq(struct ibmvfc_host *vhost)
  4060. {
  4061. int rc, retrc = -ENOMEM;
  4062. struct device *dev = vhost->dev;
  4063. struct vio_dev *vdev = to_vio_dev(dev);
  4064. struct ibmvfc_crq_queue *crq = &vhost->crq;
  4065. ENTER;
  4066. crq->msgs = (struct ibmvfc_crq *)get_zeroed_page(GFP_KERNEL);
  4067. if (!crq->msgs)
  4068. return -ENOMEM;
  4069. crq->size = PAGE_SIZE / sizeof(*crq->msgs);
  4070. crq->msg_token = dma_map_single(dev, crq->msgs,
  4071. PAGE_SIZE, DMA_BIDIRECTIONAL);
  4072. if (dma_mapping_error(dev, crq->msg_token))
  4073. goto map_failed;
  4074. retrc = rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
  4075. crq->msg_token, PAGE_SIZE);
  4076. if (rc == H_RESOURCE)
  4077. /* maybe kexecing and resource is busy. try a reset */
  4078. retrc = rc = ibmvfc_reset_crq(vhost);
  4079. if (rc == H_CLOSED)
  4080. dev_warn(dev, "Partner adapter not ready\n");
  4081. else if (rc) {
  4082. dev_warn(dev, "Error %d opening adapter\n", rc);
  4083. goto reg_crq_failed;
  4084. }
  4085. retrc = 0;
  4086. tasklet_init(&vhost->tasklet, (void *)ibmvfc_tasklet, (unsigned long)vhost);
  4087. if ((rc = request_irq(vdev->irq, ibmvfc_interrupt, 0, IBMVFC_NAME, vhost))) {
  4088. dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n", vdev->irq, rc);
  4089. goto req_irq_failed;
  4090. }
  4091. if ((rc = vio_enable_interrupts(vdev))) {
  4092. dev_err(dev, "Error %d enabling interrupts\n", rc);
  4093. goto req_irq_failed;
  4094. }
  4095. crq->cur = 0;
  4096. LEAVE;
  4097. return retrc;
  4098. req_irq_failed:
  4099. tasklet_kill(&vhost->tasklet);
  4100. do {
  4101. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  4102. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  4103. reg_crq_failed:
  4104. dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
  4105. map_failed:
  4106. free_page((unsigned long)crq->msgs);
  4107. return retrc;
  4108. }
  4109. /**
  4110. * ibmvfc_free_mem - Free memory for vhost
  4111. * @vhost: ibmvfc host struct
  4112. *
  4113. * Return value:
  4114. * none
  4115. **/
  4116. static void ibmvfc_free_mem(struct ibmvfc_host *vhost)
  4117. {
  4118. struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
  4119. ENTER;
  4120. mempool_destroy(vhost->tgt_pool);
  4121. kfree(vhost->trace);
  4122. dma_free_coherent(vhost->dev, vhost->disc_buf_sz, vhost->disc_buf,
  4123. vhost->disc_buf_dma);
  4124. dma_free_coherent(vhost->dev, sizeof(*vhost->login_buf),
  4125. vhost->login_buf, vhost->login_buf_dma);
  4126. dma_pool_destroy(vhost->sg_pool);
  4127. dma_unmap_single(vhost->dev, async_q->msg_token,
  4128. async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
  4129. free_page((unsigned long)async_q->msgs);
  4130. LEAVE;
  4131. }
  4132. /**
  4133. * ibmvfc_alloc_mem - Allocate memory for vhost
  4134. * @vhost: ibmvfc host struct
  4135. *
  4136. * Return value:
  4137. * 0 on success / non-zero on failure
  4138. **/
  4139. static int ibmvfc_alloc_mem(struct ibmvfc_host *vhost)
  4140. {
  4141. struct ibmvfc_async_crq_queue *async_q = &vhost->async_crq;
  4142. struct device *dev = vhost->dev;
  4143. ENTER;
  4144. async_q->msgs = (struct ibmvfc_async_crq *)get_zeroed_page(GFP_KERNEL);
  4145. if (!async_q->msgs) {
  4146. dev_err(dev, "Couldn't allocate async queue.\n");
  4147. goto nomem;
  4148. }
  4149. async_q->size = PAGE_SIZE / sizeof(struct ibmvfc_async_crq);
  4150. async_q->msg_token = dma_map_single(dev, async_q->msgs,
  4151. async_q->size * sizeof(*async_q->msgs),
  4152. DMA_BIDIRECTIONAL);
  4153. if (dma_mapping_error(dev, async_q->msg_token)) {
  4154. dev_err(dev, "Failed to map async queue\n");
  4155. goto free_async_crq;
  4156. }
  4157. vhost->sg_pool = dma_pool_create(IBMVFC_NAME, dev,
  4158. SG_ALL * sizeof(struct srp_direct_buf),
  4159. sizeof(struct srp_direct_buf), 0);
  4160. if (!vhost->sg_pool) {
  4161. dev_err(dev, "Failed to allocate sg pool\n");
  4162. goto unmap_async_crq;
  4163. }
  4164. vhost->login_buf = dma_alloc_coherent(dev, sizeof(*vhost->login_buf),
  4165. &vhost->login_buf_dma, GFP_KERNEL);
  4166. if (!vhost->login_buf) {
  4167. dev_err(dev, "Couldn't allocate NPIV login buffer\n");
  4168. goto free_sg_pool;
  4169. }
  4170. vhost->disc_buf_sz = sizeof(vhost->disc_buf->scsi_id[0]) * max_targets;
  4171. vhost->disc_buf = dma_alloc_coherent(dev, vhost->disc_buf_sz,
  4172. &vhost->disc_buf_dma, GFP_KERNEL);
  4173. if (!vhost->disc_buf) {
  4174. dev_err(dev, "Couldn't allocate Discover Targets buffer\n");
  4175. goto free_login_buffer;
  4176. }
  4177. vhost->trace = kcalloc(IBMVFC_NUM_TRACE_ENTRIES,
  4178. sizeof(struct ibmvfc_trace_entry), GFP_KERNEL);
  4179. if (!vhost->trace)
  4180. goto free_disc_buffer;
  4181. vhost->tgt_pool = mempool_create_kmalloc_pool(IBMVFC_TGT_MEMPOOL_SZ,
  4182. sizeof(struct ibmvfc_target));
  4183. if (!vhost->tgt_pool) {
  4184. dev_err(dev, "Couldn't allocate target memory pool\n");
  4185. goto free_trace;
  4186. }
  4187. LEAVE;
  4188. return 0;
  4189. free_trace:
  4190. kfree(vhost->trace);
  4191. free_disc_buffer:
  4192. dma_free_coherent(dev, vhost->disc_buf_sz, vhost->disc_buf,
  4193. vhost->disc_buf_dma);
  4194. free_login_buffer:
  4195. dma_free_coherent(dev, sizeof(*vhost->login_buf),
  4196. vhost->login_buf, vhost->login_buf_dma);
  4197. free_sg_pool:
  4198. dma_pool_destroy(vhost->sg_pool);
  4199. unmap_async_crq:
  4200. dma_unmap_single(dev, async_q->msg_token,
  4201. async_q->size * sizeof(*async_q->msgs), DMA_BIDIRECTIONAL);
  4202. free_async_crq:
  4203. free_page((unsigned long)async_q->msgs);
  4204. nomem:
  4205. LEAVE;
  4206. return -ENOMEM;
  4207. }
  4208. /**
  4209. * ibmvfc_rport_add_thread - Worker thread for rport adds
  4210. * @work: work struct
  4211. *
  4212. **/
  4213. static void ibmvfc_rport_add_thread(struct work_struct *work)
  4214. {
  4215. struct ibmvfc_host *vhost = container_of(work, struct ibmvfc_host,
  4216. rport_add_work_q);
  4217. struct ibmvfc_target *tgt;
  4218. struct fc_rport *rport;
  4219. unsigned long flags;
  4220. int did_work;
  4221. ENTER;
  4222. spin_lock_irqsave(vhost->host->host_lock, flags);
  4223. do {
  4224. did_work = 0;
  4225. if (vhost->state != IBMVFC_ACTIVE)
  4226. break;
  4227. list_for_each_entry(tgt, &vhost->targets, queue) {
  4228. if (tgt->add_rport) {
  4229. did_work = 1;
  4230. tgt->add_rport = 0;
  4231. kref_get(&tgt->kref);
  4232. rport = tgt->rport;
  4233. if (!rport) {
  4234. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4235. ibmvfc_tgt_add_rport(tgt);
  4236. } else if (get_device(&rport->dev)) {
  4237. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4238. tgt_dbg(tgt, "Setting rport roles\n");
  4239. fc_remote_port_rolechg(rport, tgt->ids.roles);
  4240. put_device(&rport->dev);
  4241. }
  4242. kref_put(&tgt->kref, ibmvfc_release_tgt);
  4243. spin_lock_irqsave(vhost->host->host_lock, flags);
  4244. break;
  4245. }
  4246. }
  4247. } while(did_work);
  4248. if (vhost->state == IBMVFC_ACTIVE)
  4249. vhost->scan_complete = 1;
  4250. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4251. LEAVE;
  4252. }
  4253. /**
  4254. * ibmvfc_probe - Adapter hot plug add entry point
  4255. * @vdev: vio device struct
  4256. * @id: vio device id struct
  4257. *
  4258. * Return value:
  4259. * 0 on success / non-zero on failure
  4260. **/
  4261. static int ibmvfc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  4262. {
  4263. struct ibmvfc_host *vhost;
  4264. struct Scsi_Host *shost;
  4265. struct device *dev = &vdev->dev;
  4266. int rc = -ENOMEM;
  4267. ENTER;
  4268. shost = scsi_host_alloc(&driver_template, sizeof(*vhost));
  4269. if (!shost) {
  4270. dev_err(dev, "Couldn't allocate host data\n");
  4271. goto out;
  4272. }
  4273. shost->transportt = ibmvfc_transport_template;
  4274. shost->can_queue = max_requests;
  4275. shost->max_lun = max_lun;
  4276. shost->max_id = max_targets;
  4277. shost->max_sectors = IBMVFC_MAX_SECTORS;
  4278. shost->max_cmd_len = IBMVFC_MAX_CDB_LEN;
  4279. shost->unique_id = shost->host_no;
  4280. vhost = shost_priv(shost);
  4281. INIT_LIST_HEAD(&vhost->sent);
  4282. INIT_LIST_HEAD(&vhost->free);
  4283. INIT_LIST_HEAD(&vhost->targets);
  4284. sprintf(vhost->name, IBMVFC_NAME);
  4285. vhost->host = shost;
  4286. vhost->dev = dev;
  4287. vhost->partition_number = -1;
  4288. vhost->log_level = log_level;
  4289. vhost->task_set = 1;
  4290. strcpy(vhost->partition_name, "UNKNOWN");
  4291. init_waitqueue_head(&vhost->work_wait_q);
  4292. init_waitqueue_head(&vhost->init_wait_q);
  4293. INIT_WORK(&vhost->rport_add_work_q, ibmvfc_rport_add_thread);
  4294. mutex_init(&vhost->passthru_mutex);
  4295. if ((rc = ibmvfc_alloc_mem(vhost)))
  4296. goto free_scsi_host;
  4297. vhost->work_thread = kthread_run(ibmvfc_work, vhost, "%s_%d", IBMVFC_NAME,
  4298. shost->host_no);
  4299. if (IS_ERR(vhost->work_thread)) {
  4300. dev_err(dev, "Couldn't create kernel thread: %ld\n",
  4301. PTR_ERR(vhost->work_thread));
  4302. goto free_host_mem;
  4303. }
  4304. if ((rc = ibmvfc_init_crq(vhost))) {
  4305. dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
  4306. goto kill_kthread;
  4307. }
  4308. if ((rc = ibmvfc_init_event_pool(vhost))) {
  4309. dev_err(dev, "Couldn't initialize event pool. rc=%d\n", rc);
  4310. goto release_crq;
  4311. }
  4312. if ((rc = scsi_add_host(shost, dev)))
  4313. goto release_event_pool;
  4314. fc_host_dev_loss_tmo(shost) = IBMVFC_DEV_LOSS_TMO;
  4315. if ((rc = ibmvfc_create_trace_file(&shost->shost_dev.kobj,
  4316. &ibmvfc_trace_attr))) {
  4317. dev_err(dev, "Failed to create trace file. rc=%d\n", rc);
  4318. goto remove_shost;
  4319. }
  4320. if (shost_to_fc_host(shost)->rqst_q)
  4321. blk_queue_max_segments(shost_to_fc_host(shost)->rqst_q, 1);
  4322. dev_set_drvdata(dev, vhost);
  4323. spin_lock(&ibmvfc_driver_lock);
  4324. list_add_tail(&vhost->queue, &ibmvfc_head);
  4325. spin_unlock(&ibmvfc_driver_lock);
  4326. ibmvfc_send_crq_init(vhost);
  4327. scsi_scan_host(shost);
  4328. return 0;
  4329. remove_shost:
  4330. scsi_remove_host(shost);
  4331. release_event_pool:
  4332. ibmvfc_free_event_pool(vhost);
  4333. release_crq:
  4334. ibmvfc_release_crq_queue(vhost);
  4335. kill_kthread:
  4336. kthread_stop(vhost->work_thread);
  4337. free_host_mem:
  4338. ibmvfc_free_mem(vhost);
  4339. free_scsi_host:
  4340. scsi_host_put(shost);
  4341. out:
  4342. LEAVE;
  4343. return rc;
  4344. }
  4345. /**
  4346. * ibmvfc_remove - Adapter hot plug remove entry point
  4347. * @vdev: vio device struct
  4348. *
  4349. * Return value:
  4350. * 0
  4351. **/
  4352. static int ibmvfc_remove(struct vio_dev *vdev)
  4353. {
  4354. struct ibmvfc_host *vhost = dev_get_drvdata(&vdev->dev);
  4355. unsigned long flags;
  4356. ENTER;
  4357. ibmvfc_remove_trace_file(&vhost->host->shost_dev.kobj, &ibmvfc_trace_attr);
  4358. spin_lock_irqsave(vhost->host->host_lock, flags);
  4359. ibmvfc_link_down(vhost, IBMVFC_HOST_OFFLINE);
  4360. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4361. ibmvfc_wait_while_resetting(vhost);
  4362. ibmvfc_release_crq_queue(vhost);
  4363. kthread_stop(vhost->work_thread);
  4364. fc_remove_host(vhost->host);
  4365. scsi_remove_host(vhost->host);
  4366. spin_lock_irqsave(vhost->host->host_lock, flags);
  4367. ibmvfc_purge_requests(vhost, DID_ERROR);
  4368. ibmvfc_free_event_pool(vhost);
  4369. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4370. ibmvfc_free_mem(vhost);
  4371. spin_lock(&ibmvfc_driver_lock);
  4372. list_del(&vhost->queue);
  4373. spin_unlock(&ibmvfc_driver_lock);
  4374. scsi_host_put(vhost->host);
  4375. LEAVE;
  4376. return 0;
  4377. }
  4378. /**
  4379. * ibmvfc_resume - Resume from suspend
  4380. * @dev: device struct
  4381. *
  4382. * We may have lost an interrupt across suspend/resume, so kick the
  4383. * interrupt handler
  4384. *
  4385. */
  4386. static int ibmvfc_resume(struct device *dev)
  4387. {
  4388. unsigned long flags;
  4389. struct ibmvfc_host *vhost = dev_get_drvdata(dev);
  4390. struct vio_dev *vdev = to_vio_dev(dev);
  4391. spin_lock_irqsave(vhost->host->host_lock, flags);
  4392. vio_disable_interrupts(vdev);
  4393. tasklet_schedule(&vhost->tasklet);
  4394. spin_unlock_irqrestore(vhost->host->host_lock, flags);
  4395. return 0;
  4396. }
  4397. /**
  4398. * ibmvfc_get_desired_dma - Calculate DMA resources needed by the driver
  4399. * @vdev: vio device struct
  4400. *
  4401. * Return value:
  4402. * Number of bytes the driver will need to DMA map at the same time in
  4403. * order to perform well.
  4404. */
  4405. static unsigned long ibmvfc_get_desired_dma(struct vio_dev *vdev)
  4406. {
  4407. unsigned long pool_dma = max_requests * sizeof(union ibmvfc_iu);
  4408. return pool_dma + ((512 * 1024) * driver_template.cmd_per_lun);
  4409. }
  4410. static struct vio_device_id ibmvfc_device_table[] = {
  4411. {"fcp", "IBM,vfc-client"},
  4412. { "", "" }
  4413. };
  4414. MODULE_DEVICE_TABLE(vio, ibmvfc_device_table);
  4415. static struct dev_pm_ops ibmvfc_pm_ops = {
  4416. .resume = ibmvfc_resume
  4417. };
  4418. static struct vio_driver ibmvfc_driver = {
  4419. .id_table = ibmvfc_device_table,
  4420. .probe = ibmvfc_probe,
  4421. .remove = ibmvfc_remove,
  4422. .get_desired_dma = ibmvfc_get_desired_dma,
  4423. .name = IBMVFC_NAME,
  4424. .pm = &ibmvfc_pm_ops,
  4425. };
  4426. static struct fc_function_template ibmvfc_transport_functions = {
  4427. .show_host_fabric_name = 1,
  4428. .show_host_node_name = 1,
  4429. .show_host_port_name = 1,
  4430. .show_host_supported_classes = 1,
  4431. .show_host_port_type = 1,
  4432. .show_host_port_id = 1,
  4433. .show_host_maxframe_size = 1,
  4434. .get_host_port_state = ibmvfc_get_host_port_state,
  4435. .show_host_port_state = 1,
  4436. .get_host_speed = ibmvfc_get_host_speed,
  4437. .show_host_speed = 1,
  4438. .issue_fc_host_lip = ibmvfc_issue_fc_host_lip,
  4439. .terminate_rport_io = ibmvfc_terminate_rport_io,
  4440. .show_rport_maxframe_size = 1,
  4441. .show_rport_supported_classes = 1,
  4442. .set_rport_dev_loss_tmo = ibmvfc_set_rport_dev_loss_tmo,
  4443. .show_rport_dev_loss_tmo = 1,
  4444. .get_starget_node_name = ibmvfc_get_starget_node_name,
  4445. .show_starget_node_name = 1,
  4446. .get_starget_port_name = ibmvfc_get_starget_port_name,
  4447. .show_starget_port_name = 1,
  4448. .get_starget_port_id = ibmvfc_get_starget_port_id,
  4449. .show_starget_port_id = 1,
  4450. .bsg_request = ibmvfc_bsg_request,
  4451. .bsg_timeout = ibmvfc_bsg_timeout,
  4452. };
  4453. /**
  4454. * ibmvfc_module_init - Initialize the ibmvfc module
  4455. *
  4456. * Return value:
  4457. * 0 on success / other on failure
  4458. **/
  4459. static int __init ibmvfc_module_init(void)
  4460. {
  4461. int rc;
  4462. if (!firmware_has_feature(FW_FEATURE_VIO))
  4463. return -ENODEV;
  4464. printk(KERN_INFO IBMVFC_NAME": IBM Virtual Fibre Channel Driver version: %s %s\n",
  4465. IBMVFC_DRIVER_VERSION, IBMVFC_DRIVER_DATE);
  4466. ibmvfc_transport_template = fc_attach_transport(&ibmvfc_transport_functions);
  4467. if (!ibmvfc_transport_template)
  4468. return -ENOMEM;
  4469. rc = vio_register_driver(&ibmvfc_driver);
  4470. if (rc)
  4471. fc_release_transport(ibmvfc_transport_template);
  4472. return rc;
  4473. }
  4474. /**
  4475. * ibmvfc_module_exit - Teardown the ibmvfc module
  4476. *
  4477. * Return value:
  4478. * nothing
  4479. **/
  4480. static void __exit ibmvfc_module_exit(void)
  4481. {
  4482. vio_unregister_driver(&ibmvfc_driver);
  4483. fc_release_transport(ibmvfc_transport_template);
  4484. }
  4485. module_init(ibmvfc_module_init);
  4486. module_exit(ibmvfc_module_exit);