scsi_transport_fc.c 131 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * FiberChannel transport specific attributes exported to sysfs.
  4. *
  5. * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
  6. * Copyright (C) 2004-2007 James Smart, Emulex Corporation
  7. * Rewrite for host, target, device, and remote port attributes,
  8. * statistics, and service functions...
  9. * Add vports, etc
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/slab.h>
  14. #include <linux/delay.h>
  15. #include <linux/hex.h>
  16. #include <linux/kernel.h>
  17. #include <linux/bsg-lib.h>
  18. #include <scsi/scsi_device.h>
  19. #include <scsi/scsi_host.h>
  20. #include <scsi/scsi_transport.h>
  21. #include <scsi/scsi_transport_fc.h>
  22. #include <scsi/scsi_cmnd.h>
  23. #include <net/netlink.h>
  24. #include <scsi/scsi_netlink_fc.h>
  25. #include <scsi/scsi_bsg_fc.h>
  26. #include <uapi/scsi/fc/fc_els.h>
  27. #include "scsi_priv.h"
  28. static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
  29. static void fc_vport_sched_delete(struct work_struct *work);
  30. static int fc_vport_setup(struct Scsi_Host *shost, int channel,
  31. struct device *pdev, struct fc_vport_identifiers *ids,
  32. struct fc_vport **vport);
  33. static int fc_bsg_hostadd(struct Scsi_Host *, struct fc_host_attrs *);
  34. static int fc_bsg_rportadd(struct Scsi_Host *, struct fc_rport *);
  35. static void fc_bsg_remove(struct request_queue *);
  36. static void fc_bsg_goose_queue(struct fc_rport *);
  37. static void fc_li_stats_update(u16 event_type,
  38. struct fc_fpin_stats *stats);
  39. static void fc_delivery_stats_update(u32 reason_code,
  40. struct fc_fpin_stats *stats);
  41. static void fc_cn_stats_update(u16 event_type, struct fc_fpin_stats *stats);
  42. /*
  43. * Module Parameters
  44. */
  45. /*
  46. * dev_loss_tmo: the default number of seconds that the FC transport
  47. * should insulate the loss of a remote port.
  48. * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
  49. */
  50. static unsigned int fc_dev_loss_tmo = 60; /* seconds */
  51. module_param_named(dev_loss_tmo, fc_dev_loss_tmo, uint, S_IRUGO|S_IWUSR);
  52. MODULE_PARM_DESC(dev_loss_tmo,
  53. "Maximum number of seconds that the FC transport should"
  54. " insulate the loss of a remote port. Once this value is"
  55. " exceeded, the scsi target is removed. Value should be"
  56. " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT if"
  57. " fast_io_fail_tmo is not set.");
  58. /*
  59. * Redefine so that we can have same named attributes in the
  60. * sdev/starget/host objects.
  61. */
  62. #define FC_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
  63. struct device_attribute device_attr_##_prefix##_##_name = \
  64. __ATTR(_name,_mode,_show,_store)
  65. #define fc_enum_name_search(title, table_type, table) \
  66. static const char *get_fc_##title##_name(enum table_type table_key) \
  67. { \
  68. int i; \
  69. char *name = NULL; \
  70. \
  71. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  72. if (table[i].value == table_key) { \
  73. name = table[i].name; \
  74. break; \
  75. } \
  76. } \
  77. return name; \
  78. }
  79. #define fc_enum_name_match(title, table_type, table) \
  80. static int get_fc_##title##_match(const char *table_key, \
  81. enum table_type *value) \
  82. { \
  83. int i; \
  84. \
  85. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  86. if (strncmp(table_key, table[i].name, \
  87. table[i].matchlen) == 0) { \
  88. *value = table[i].value; \
  89. return 0; /* success */ \
  90. } \
  91. } \
  92. return 1; /* failure */ \
  93. }
  94. /* Convert fc_port_type values to ascii string name */
  95. static struct {
  96. enum fc_port_type value;
  97. char *name;
  98. } fc_port_type_names[] = {
  99. { FC_PORTTYPE_UNKNOWN, "Unknown" },
  100. { FC_PORTTYPE_OTHER, "Other" },
  101. { FC_PORTTYPE_NOTPRESENT, "Not Present" },
  102. { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
  103. { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
  104. { FC_PORTTYPE_LPORT, "LPort (private loop)" },
  105. { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection)" },
  106. { FC_PORTTYPE_NPIV, "NPIV VPORT" },
  107. };
  108. fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
  109. #define FC_PORTTYPE_MAX_NAMELEN 50
  110. /* Reuse fc_port_type enum function for vport_type */
  111. #define get_fc_vport_type_name get_fc_port_type_name
  112. /* Convert fc_host_event_code values to ascii string name */
  113. static const struct {
  114. enum fc_host_event_code value;
  115. char *name;
  116. } fc_host_event_code_names[] = {
  117. { FCH_EVT_LIP, "lip" },
  118. { FCH_EVT_LINKUP, "link_up" },
  119. { FCH_EVT_LINKDOWN, "link_down" },
  120. { FCH_EVT_LIPRESET, "lip_reset" },
  121. { FCH_EVT_RSCN, "rscn" },
  122. { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
  123. { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
  124. { FCH_EVT_PORT_ONLINE, "port_online" },
  125. { FCH_EVT_PORT_OFFLINE, "port_offline" },
  126. { FCH_EVT_PORT_FABRIC, "port_fabric" },
  127. { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
  128. { FCH_EVT_LINK_FPIN, "link_FPIN" },
  129. { FCH_EVT_LINK_FPIN_ACK, "link_FPIN_ACK" },
  130. { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
  131. };
  132. fc_enum_name_search(host_event_code, fc_host_event_code,
  133. fc_host_event_code_names)
  134. #define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
  135. /* Convert fc_port_state values to ascii string name */
  136. static struct {
  137. enum fc_port_state value;
  138. char *name;
  139. int matchlen;
  140. } fc_port_state_names[] = {
  141. { FC_PORTSTATE_UNKNOWN, "Unknown", 7},
  142. { FC_PORTSTATE_NOTPRESENT, "Not Present", 11 },
  143. { FC_PORTSTATE_ONLINE, "Online", 6 },
  144. { FC_PORTSTATE_OFFLINE, "Offline", 7 },
  145. { FC_PORTSTATE_BLOCKED, "Blocked", 7 },
  146. { FC_PORTSTATE_BYPASSED, "Bypassed", 8 },
  147. { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics", 11 },
  148. { FC_PORTSTATE_LINKDOWN, "Linkdown", 8 },
  149. { FC_PORTSTATE_ERROR, "Error", 5 },
  150. { FC_PORTSTATE_LOOPBACK, "Loopback", 8 },
  151. { FC_PORTSTATE_DELETED, "Deleted", 7 },
  152. { FC_PORTSTATE_MARGINAL, "Marginal", 8 },
  153. };
  154. fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
  155. fc_enum_name_match(port_state, fc_port_state, fc_port_state_names)
  156. #define FC_PORTSTATE_MAX_NAMELEN 20
  157. /* Convert fc_vport_state values to ascii string name */
  158. static struct {
  159. enum fc_vport_state value;
  160. char *name;
  161. } fc_vport_state_names[] = {
  162. { FC_VPORT_UNKNOWN, "Unknown" },
  163. { FC_VPORT_ACTIVE, "Active" },
  164. { FC_VPORT_DISABLED, "Disabled" },
  165. { FC_VPORT_LINKDOWN, "Linkdown" },
  166. { FC_VPORT_INITIALIZING, "Initializing" },
  167. { FC_VPORT_NO_FABRIC_SUPP, "No Fabric Support" },
  168. { FC_VPORT_NO_FABRIC_RSCS, "No Fabric Resources" },
  169. { FC_VPORT_FABRIC_LOGOUT, "Fabric Logout" },
  170. { FC_VPORT_FABRIC_REJ_WWN, "Fabric Rejected WWN" },
  171. { FC_VPORT_FAILED, "VPort Failed" },
  172. };
  173. fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
  174. #define FC_VPORTSTATE_MAX_NAMELEN 24
  175. /* Reuse fc_vport_state enum function for vport_last_state */
  176. #define get_fc_vport_last_state_name get_fc_vport_state_name
  177. /* Convert fc_tgtid_binding_type values to ascii string name */
  178. static const struct {
  179. enum fc_tgtid_binding_type value;
  180. char *name;
  181. int matchlen;
  182. } fc_tgtid_binding_type_names[] = {
  183. { FC_TGTID_BIND_NONE, "none", 4 },
  184. { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
  185. { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
  186. { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
  187. };
  188. fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
  189. fc_tgtid_binding_type_names)
  190. fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
  191. fc_tgtid_binding_type_names)
  192. #define FC_BINDTYPE_MAX_NAMELEN 30
  193. #define fc_bitfield_name_search(title, table) \
  194. static ssize_t \
  195. get_fc_##title##_names(u32 table_key, char *buf) \
  196. { \
  197. char *prefix = ""; \
  198. ssize_t len = 0; \
  199. int i; \
  200. \
  201. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  202. if (table[i].value & table_key) { \
  203. len += sprintf(buf + len, "%s%s", \
  204. prefix, table[i].name); \
  205. prefix = ", "; \
  206. } \
  207. } \
  208. len += sprintf(buf + len, "\n"); \
  209. return len; \
  210. }
  211. /* Convert FC_COS bit values to ascii string name */
  212. static const struct {
  213. u32 value;
  214. char *name;
  215. } fc_cos_names[] = {
  216. { FC_COS_CLASS1, "Class 1" },
  217. { FC_COS_CLASS2, "Class 2" },
  218. { FC_COS_CLASS3, "Class 3" },
  219. { FC_COS_CLASS4, "Class 4" },
  220. { FC_COS_CLASS6, "Class 6" },
  221. };
  222. fc_bitfield_name_search(cos, fc_cos_names)
  223. /* Convert FC_PORTSPEED bit values to ascii string name */
  224. static const struct {
  225. u32 value;
  226. char *name;
  227. } fc_port_speed_names[] = {
  228. { FC_PORTSPEED_1GBIT, "1 Gbit" },
  229. { FC_PORTSPEED_2GBIT, "2 Gbit" },
  230. { FC_PORTSPEED_4GBIT, "4 Gbit" },
  231. { FC_PORTSPEED_10GBIT, "10 Gbit" },
  232. { FC_PORTSPEED_8GBIT, "8 Gbit" },
  233. { FC_PORTSPEED_16GBIT, "16 Gbit" },
  234. { FC_PORTSPEED_32GBIT, "32 Gbit" },
  235. { FC_PORTSPEED_20GBIT, "20 Gbit" },
  236. { FC_PORTSPEED_40GBIT, "40 Gbit" },
  237. { FC_PORTSPEED_50GBIT, "50 Gbit" },
  238. { FC_PORTSPEED_100GBIT, "100 Gbit" },
  239. { FC_PORTSPEED_25GBIT, "25 Gbit" },
  240. { FC_PORTSPEED_64GBIT, "64 Gbit" },
  241. { FC_PORTSPEED_128GBIT, "128 Gbit" },
  242. { FC_PORTSPEED_256GBIT, "256 Gbit" },
  243. { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
  244. };
  245. fc_bitfield_name_search(port_speed, fc_port_speed_names)
  246. static int
  247. show_fc_fc4s (char *buf, u8 *fc4_list)
  248. {
  249. int i, len=0;
  250. for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
  251. len += sprintf(buf + len , "0x%02x ", *fc4_list);
  252. len += sprintf(buf + len, "\n");
  253. return len;
  254. }
  255. /* Convert FC_PORT_ROLE bit values to ascii string name */
  256. static const struct {
  257. u32 value;
  258. char *name;
  259. } fc_port_role_names[] = {
  260. { FC_PORT_ROLE_FCP_TARGET, "FCP Target" },
  261. { FC_PORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
  262. { FC_PORT_ROLE_IP_PORT, "IP Port" },
  263. { FC_PORT_ROLE_FCP_DUMMY_INITIATOR, "FCP Dummy Initiator" },
  264. { FC_PORT_ROLE_NVME_INITIATOR, "NVMe Initiator" },
  265. { FC_PORT_ROLE_NVME_TARGET, "NVMe Target" },
  266. { FC_PORT_ROLE_NVME_DISCOVERY, "NVMe Discovery" },
  267. };
  268. fc_bitfield_name_search(port_roles, fc_port_role_names)
  269. /*
  270. * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
  271. */
  272. #define FC_WELLKNOWN_PORTID_MASK 0xfffff0
  273. #define FC_WELLKNOWN_ROLE_MASK 0x00000f
  274. #define FC_FPORT_PORTID 0x00000e
  275. #define FC_FABCTLR_PORTID 0x00000d
  276. #define FC_DIRSRVR_PORTID 0x00000c
  277. #define FC_TIMESRVR_PORTID 0x00000b
  278. #define FC_MGMTSRVR_PORTID 0x00000a
  279. static void fc_timeout_deleted_rport(struct work_struct *work);
  280. static void fc_timeout_fail_rport_io(struct work_struct *work);
  281. static void fc_scsi_scan_rport(struct work_struct *work);
  282. /*
  283. * Attribute counts pre object type...
  284. * Increase these values if you add attributes
  285. */
  286. #define FC_STARGET_NUM_ATTRS 3
  287. #define FC_RPORT_NUM_ATTRS 10
  288. #define FC_VPORT_NUM_ATTRS 9
  289. #define FC_HOST_NUM_ATTRS 29
  290. struct fc_internal {
  291. struct scsi_transport_template t;
  292. struct fc_function_template *f;
  293. /*
  294. * For attributes : each object has :
  295. * An array of the actual attributes structures
  296. * An array of null-terminated pointers to the attribute
  297. * structures - used for mid-layer interaction.
  298. *
  299. * The attribute containers for the starget and host are are
  300. * part of the midlayer. As the remote port is specific to the
  301. * fc transport, we must provide the attribute container.
  302. */
  303. struct device_attribute private_starget_attrs[
  304. FC_STARGET_NUM_ATTRS];
  305. struct device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
  306. struct device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
  307. struct device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
  308. struct transport_container rport_attr_cont;
  309. struct device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
  310. struct device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
  311. struct transport_container vport_attr_cont;
  312. struct device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
  313. struct device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
  314. };
  315. #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
  316. static int fc_target_setup(struct transport_container *tc, struct device *dev,
  317. struct device *cdev)
  318. {
  319. struct scsi_target *starget = to_scsi_target(dev);
  320. struct fc_rport *rport = starget_to_rport(starget);
  321. /*
  322. * if parent is remote port, use values from remote port.
  323. * Otherwise, this host uses the fc_transport, but not the
  324. * remote port interface. As such, initialize to known non-values.
  325. */
  326. if (rport) {
  327. fc_starget_node_name(starget) = rport->node_name;
  328. fc_starget_port_name(starget) = rport->port_name;
  329. fc_starget_port_id(starget) = rport->port_id;
  330. } else {
  331. fc_starget_node_name(starget) = -1;
  332. fc_starget_port_name(starget) = -1;
  333. fc_starget_port_id(starget) = -1;
  334. }
  335. return 0;
  336. }
  337. static DECLARE_TRANSPORT_CLASS(fc_transport_class,
  338. "fc_transport",
  339. fc_target_setup,
  340. NULL,
  341. NULL);
  342. static int fc_host_setup(struct transport_container *tc, struct device *dev,
  343. struct device *cdev)
  344. {
  345. struct Scsi_Host *shost = dev_to_shost(dev);
  346. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  347. /*
  348. * Set default values easily detected by the midlayer as
  349. * failure cases. The scsi lldd is responsible for initializing
  350. * all transport attributes to valid values per host.
  351. */
  352. fc_host->node_name = -1;
  353. fc_host->port_name = -1;
  354. fc_host->permanent_port_name = -1;
  355. fc_host->supported_classes = FC_COS_UNSPECIFIED;
  356. memset(fc_host->supported_fc4s, 0,
  357. sizeof(fc_host->supported_fc4s));
  358. fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
  359. fc_host->maxframe_size = -1;
  360. fc_host->max_npiv_vports = 0;
  361. memset(fc_host->serial_number, 0,
  362. sizeof(fc_host->serial_number));
  363. memset(fc_host->manufacturer, 0,
  364. sizeof(fc_host->manufacturer));
  365. memset(fc_host->model, 0,
  366. sizeof(fc_host->model));
  367. memset(fc_host->model_description, 0,
  368. sizeof(fc_host->model_description));
  369. memset(fc_host->hardware_version, 0,
  370. sizeof(fc_host->hardware_version));
  371. memset(fc_host->driver_version, 0,
  372. sizeof(fc_host->driver_version));
  373. memset(fc_host->firmware_version, 0,
  374. sizeof(fc_host->firmware_version));
  375. memset(fc_host->optionrom_version, 0,
  376. sizeof(fc_host->optionrom_version));
  377. fc_host->port_id = -1;
  378. fc_host->port_type = FC_PORTTYPE_UNKNOWN;
  379. fc_host->port_state = FC_PORTSTATE_UNKNOWN;
  380. memset(fc_host->active_fc4s, 0,
  381. sizeof(fc_host->active_fc4s));
  382. fc_host->speed = FC_PORTSPEED_UNKNOWN;
  383. fc_host->fabric_name = -1;
  384. memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
  385. memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
  386. memset(&fc_host->fpin_stats, 0, sizeof(fc_host->fpin_stats));
  387. fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
  388. INIT_LIST_HEAD(&fc_host->rports);
  389. INIT_LIST_HEAD(&fc_host->rport_bindings);
  390. INIT_LIST_HEAD(&fc_host->vports);
  391. fc_host->next_rport_number = 0;
  392. fc_host->next_target_id = 0;
  393. fc_host->next_vport_number = 0;
  394. fc_host->npiv_vports_inuse = 0;
  395. fc_host->work_q = alloc_workqueue("fc_wq_%d", WQ_PERCPU, 0,
  396. shost->host_no);
  397. if (!fc_host->work_q)
  398. return -ENOMEM;
  399. fc_host->dev_loss_tmo = fc_dev_loss_tmo;
  400. fc_bsg_hostadd(shost, fc_host);
  401. /* ignore any bsg add error - we just can't do sgio */
  402. return 0;
  403. }
  404. static int fc_host_remove(struct transport_container *tc, struct device *dev,
  405. struct device *cdev)
  406. {
  407. struct Scsi_Host *shost = dev_to_shost(dev);
  408. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  409. fc_bsg_remove(fc_host->rqst_q);
  410. return 0;
  411. }
  412. static DECLARE_TRANSPORT_CLASS(fc_host_class,
  413. "fc_host",
  414. fc_host_setup,
  415. fc_host_remove,
  416. NULL);
  417. /*
  418. * Setup and Remove actions for remote ports are handled
  419. * in the service functions below.
  420. */
  421. static DECLARE_TRANSPORT_CLASS(fc_rport_class,
  422. "fc_remote_ports",
  423. NULL,
  424. NULL,
  425. NULL);
  426. /*
  427. * Setup and Remove actions for virtual ports are handled
  428. * in the service functions below.
  429. */
  430. static DECLARE_TRANSPORT_CLASS(fc_vport_class,
  431. "fc_vports",
  432. NULL,
  433. NULL,
  434. NULL);
  435. /*
  436. * Netlink Infrastructure
  437. */
  438. static atomic_t fc_event_seq;
  439. /**
  440. * fc_get_event_number - Obtain the next sequential FC event number
  441. *
  442. * Notes:
  443. * We could have inlined this, but it would have required fc_event_seq to
  444. * be exposed. For now, live with the subroutine call.
  445. * Atomic used to avoid lock/unlock...
  446. */
  447. u32
  448. fc_get_event_number(void)
  449. {
  450. return atomic_add_return(1, &fc_event_seq);
  451. }
  452. EXPORT_SYMBOL(fc_get_event_number);
  453. /**
  454. * fc_host_post_fc_event - routine to do the work of posting an event
  455. * on an fc_host.
  456. * @shost: host the event occurred on
  457. * @event_number: fc event number obtained from get_fc_event_number()
  458. * @event_code: fc_host event being posted
  459. * @data_len: amount, in bytes, of event data
  460. * @data_buf: pointer to event data
  461. * @vendor_id: value for Vendor id
  462. *
  463. * Notes:
  464. * This routine assumes no locks are held on entry.
  465. */
  466. void
  467. fc_host_post_fc_event(struct Scsi_Host *shost, u32 event_number,
  468. enum fc_host_event_code event_code,
  469. u32 data_len, char *data_buf, u64 vendor_id)
  470. {
  471. struct sk_buff *skb;
  472. struct nlmsghdr *nlh;
  473. struct fc_nl_event *event;
  474. const char *name;
  475. size_t len, padding;
  476. int err;
  477. if (!data_buf || data_len < 4)
  478. data_len = 0;
  479. if (!scsi_nl_sock) {
  480. err = -ENOENT;
  481. goto send_fail;
  482. }
  483. len = FC_NL_MSGALIGN(sizeof(*event) - sizeof(event->event_data) + data_len);
  484. skb = nlmsg_new(len, GFP_KERNEL);
  485. if (!skb) {
  486. err = -ENOBUFS;
  487. goto send_fail;
  488. }
  489. nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG, len, 0);
  490. if (!nlh) {
  491. err = -ENOBUFS;
  492. goto send_fail_skb;
  493. }
  494. event = nlmsg_data(nlh);
  495. INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
  496. FC_NL_ASYNC_EVENT, len);
  497. event->seconds = ktime_get_real_seconds();
  498. event->vendor_id = vendor_id;
  499. event->host_no = shost->host_no;
  500. event->event_datalen = data_len; /* bytes */
  501. event->event_num = event_number;
  502. event->event_code = event_code;
  503. if (data_len)
  504. memcpy(event->event_data_flex, data_buf, data_len);
  505. padding = len - offsetof(typeof(*event), event_data_flex) - data_len;
  506. memset(event->event_data_flex + data_len, 0, padding);
  507. nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
  508. GFP_KERNEL);
  509. return;
  510. send_fail_skb:
  511. kfree_skb(skb);
  512. send_fail:
  513. name = get_fc_host_event_code_name(event_code);
  514. printk(KERN_WARNING
  515. "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
  516. __func__, shost->host_no,
  517. (name) ? name : "<unknown>",
  518. (data_len) ? *((u32 *)data_buf) : 0xFFFFFFFF, err);
  519. return;
  520. }
  521. EXPORT_SYMBOL(fc_host_post_fc_event);
  522. /**
  523. * fc_host_post_event - called to post an even on an fc_host.
  524. * @shost: host the event occurred on
  525. * @event_number: fc event number obtained from get_fc_event_number()
  526. * @event_code: fc_host event being posted
  527. * @event_data: 32bits of data for the event being posted
  528. *
  529. * Notes:
  530. * This routine assumes no locks are held on entry.
  531. */
  532. void
  533. fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
  534. enum fc_host_event_code event_code, u32 event_data)
  535. {
  536. fc_host_post_fc_event(shost, event_number, event_code,
  537. (u32)sizeof(u32), (char *)&event_data, 0);
  538. }
  539. EXPORT_SYMBOL(fc_host_post_event);
  540. /**
  541. * fc_host_post_vendor_event - called to post a vendor unique event
  542. * on an fc_host
  543. * @shost: host the event occurred on
  544. * @event_number: fc event number obtained from get_fc_event_number()
  545. * @data_len: amount, in bytes, of vendor unique data
  546. * @data_buf: pointer to vendor unique data
  547. * @vendor_id: Vendor id
  548. *
  549. * Notes:
  550. * This routine assumes no locks are held on entry.
  551. */
  552. void
  553. fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
  554. u32 data_len, char * data_buf, u64 vendor_id)
  555. {
  556. fc_host_post_fc_event(shost, event_number, FCH_EVT_VENDOR_UNIQUE,
  557. data_len, data_buf, vendor_id);
  558. }
  559. EXPORT_SYMBOL(fc_host_post_vendor_event);
  560. /**
  561. * fc_find_rport_by_wwpn - find the fc_rport pointer for a given wwpn
  562. * @shost: host the fc_rport is associated with
  563. * @wwpn: wwpn of the fc_rport device
  564. *
  565. * Notes:
  566. * This routine assumes no locks are held on entry.
  567. */
  568. struct fc_rport *
  569. fc_find_rport_by_wwpn(struct Scsi_Host *shost, u64 wwpn)
  570. {
  571. struct fc_rport *rport;
  572. unsigned long flags;
  573. spin_lock_irqsave(shost->host_lock, flags);
  574. list_for_each_entry(rport, &fc_host_rports(shost), peers) {
  575. if (rport->port_state != FC_PORTSTATE_ONLINE)
  576. continue;
  577. if (rport->port_name == wwpn) {
  578. spin_unlock_irqrestore(shost->host_lock, flags);
  579. return rport;
  580. }
  581. }
  582. spin_unlock_irqrestore(shost->host_lock, flags);
  583. return NULL;
  584. }
  585. EXPORT_SYMBOL(fc_find_rport_by_wwpn);
  586. static void
  587. fc_li_stats_update(u16 event_type,
  588. struct fc_fpin_stats *stats)
  589. {
  590. stats->li++;
  591. switch (event_type) {
  592. case FPIN_LI_UNKNOWN:
  593. stats->li_failure_unknown++;
  594. break;
  595. case FPIN_LI_LINK_FAILURE:
  596. stats->li_link_failure_count++;
  597. break;
  598. case FPIN_LI_LOSS_OF_SYNC:
  599. stats->li_loss_of_sync_count++;
  600. break;
  601. case FPIN_LI_LOSS_OF_SIG:
  602. stats->li_loss_of_signals_count++;
  603. break;
  604. case FPIN_LI_PRIM_SEQ_ERR:
  605. stats->li_prim_seq_err_count++;
  606. break;
  607. case FPIN_LI_INVALID_TX_WD:
  608. stats->li_invalid_tx_word_count++;
  609. break;
  610. case FPIN_LI_INVALID_CRC:
  611. stats->li_invalid_crc_count++;
  612. break;
  613. case FPIN_LI_DEVICE_SPEC:
  614. stats->li_device_specific++;
  615. break;
  616. }
  617. }
  618. static void
  619. fc_delivery_stats_update(u32 reason_code, struct fc_fpin_stats *stats)
  620. {
  621. stats->dn++;
  622. switch (reason_code) {
  623. case FPIN_DELI_UNKNOWN:
  624. stats->dn_unknown++;
  625. break;
  626. case FPIN_DELI_TIMEOUT:
  627. stats->dn_timeout++;
  628. break;
  629. case FPIN_DELI_UNABLE_TO_ROUTE:
  630. stats->dn_unable_to_route++;
  631. break;
  632. case FPIN_DELI_DEVICE_SPEC:
  633. stats->dn_device_specific++;
  634. break;
  635. }
  636. }
  637. static void
  638. fc_cn_stats_update(u16 event_type, struct fc_fpin_stats *stats)
  639. {
  640. stats->cn++;
  641. switch (event_type) {
  642. case FPIN_CONGN_CLEAR:
  643. stats->cn_clear++;
  644. break;
  645. case FPIN_CONGN_LOST_CREDIT:
  646. stats->cn_lost_credit++;
  647. break;
  648. case FPIN_CONGN_CREDIT_STALL:
  649. stats->cn_credit_stall++;
  650. break;
  651. case FPIN_CONGN_OVERSUBSCRIPTION:
  652. stats->cn_oversubscription++;
  653. break;
  654. case FPIN_CONGN_DEVICE_SPEC:
  655. stats->cn_device_specific++;
  656. }
  657. }
  658. /*
  659. * fc_fpin_li_stats_update - routine to update Link Integrity
  660. * event statistics.
  661. * @shost: host the FPIN was received on
  662. * @tlv: pointer to link integrity descriptor
  663. *
  664. */
  665. static void
  666. fc_fpin_li_stats_update(struct Scsi_Host *shost, struct fc_tlv_desc *tlv)
  667. {
  668. u8 i;
  669. struct fc_rport *rport = NULL;
  670. struct fc_rport *attach_rport = NULL;
  671. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  672. struct fc_fn_li_desc *li_desc = (struct fc_fn_li_desc *)tlv;
  673. u16 event_type = be16_to_cpu(li_desc->event_type);
  674. u64 wwpn;
  675. rport = fc_find_rport_by_wwpn(shost,
  676. be64_to_cpu(li_desc->attached_wwpn));
  677. if (rport &&
  678. (rport->roles & FC_PORT_ROLE_FCP_TARGET ||
  679. rport->roles & FC_PORT_ROLE_NVME_TARGET)) {
  680. attach_rport = rport;
  681. fc_li_stats_update(event_type, &attach_rport->fpin_stats);
  682. }
  683. if (be32_to_cpu(li_desc->pname_count) > 0) {
  684. for (i = 0;
  685. i < be32_to_cpu(li_desc->pname_count);
  686. i++) {
  687. wwpn = be64_to_cpu(li_desc->pname_list[i]);
  688. rport = fc_find_rport_by_wwpn(shost, wwpn);
  689. if (rport &&
  690. (rport->roles & FC_PORT_ROLE_FCP_TARGET ||
  691. rport->roles & FC_PORT_ROLE_NVME_TARGET)) {
  692. if (rport == attach_rport)
  693. continue;
  694. fc_li_stats_update(event_type,
  695. &rport->fpin_stats);
  696. }
  697. }
  698. }
  699. if (fc_host->port_name == be64_to_cpu(li_desc->attached_wwpn))
  700. fc_li_stats_update(event_type, &fc_host->fpin_stats);
  701. }
  702. /*
  703. * fc_fpin_delivery_stats_update - routine to update Delivery Notification
  704. * event statistics.
  705. * @shost: host the FPIN was received on
  706. * @tlv: pointer to delivery descriptor
  707. *
  708. */
  709. static void
  710. fc_fpin_delivery_stats_update(struct Scsi_Host *shost,
  711. struct fc_tlv_desc *tlv)
  712. {
  713. struct fc_rport *rport = NULL;
  714. struct fc_rport *attach_rport = NULL;
  715. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  716. struct fc_fn_deli_desc *dn_desc = (struct fc_fn_deli_desc *)tlv;
  717. u32 reason_code = be32_to_cpu(dn_desc->deli_reason_code);
  718. rport = fc_find_rport_by_wwpn(shost,
  719. be64_to_cpu(dn_desc->attached_wwpn));
  720. if (rport &&
  721. (rport->roles & FC_PORT_ROLE_FCP_TARGET ||
  722. rport->roles & FC_PORT_ROLE_NVME_TARGET)) {
  723. attach_rport = rport;
  724. fc_delivery_stats_update(reason_code,
  725. &attach_rport->fpin_stats);
  726. }
  727. if (fc_host->port_name == be64_to_cpu(dn_desc->attached_wwpn))
  728. fc_delivery_stats_update(reason_code, &fc_host->fpin_stats);
  729. }
  730. /*
  731. * fc_fpin_peer_congn_stats_update - routine to update Peer Congestion
  732. * event statistics.
  733. * @shost: host the FPIN was received on
  734. * @tlv: pointer to peer congestion descriptor
  735. *
  736. */
  737. static void
  738. fc_fpin_peer_congn_stats_update(struct Scsi_Host *shost,
  739. struct fc_tlv_desc *tlv)
  740. {
  741. u8 i;
  742. struct fc_rport *rport = NULL;
  743. struct fc_rport *attach_rport = NULL;
  744. struct fc_fn_peer_congn_desc *pc_desc =
  745. (struct fc_fn_peer_congn_desc *)tlv;
  746. u16 event_type = be16_to_cpu(pc_desc->event_type);
  747. u64 wwpn;
  748. rport = fc_find_rport_by_wwpn(shost,
  749. be64_to_cpu(pc_desc->attached_wwpn));
  750. if (rport &&
  751. (rport->roles & FC_PORT_ROLE_FCP_TARGET ||
  752. rport->roles & FC_PORT_ROLE_NVME_TARGET)) {
  753. attach_rport = rport;
  754. fc_cn_stats_update(event_type, &attach_rport->fpin_stats);
  755. }
  756. if (be32_to_cpu(pc_desc->pname_count) > 0) {
  757. for (i = 0;
  758. i < be32_to_cpu(pc_desc->pname_count);
  759. i++) {
  760. wwpn = be64_to_cpu(pc_desc->pname_list[i]);
  761. rport = fc_find_rport_by_wwpn(shost, wwpn);
  762. if (rport &&
  763. (rport->roles & FC_PORT_ROLE_FCP_TARGET ||
  764. rport->roles & FC_PORT_ROLE_NVME_TARGET)) {
  765. if (rport == attach_rport)
  766. continue;
  767. fc_cn_stats_update(event_type,
  768. &rport->fpin_stats);
  769. }
  770. }
  771. }
  772. }
  773. /*
  774. * fc_fpin_congn_stats_update - routine to update Congestion
  775. * event statistics.
  776. * @shost: host the FPIN was received on
  777. * @tlv: pointer to congestion descriptor
  778. *
  779. */
  780. static void
  781. fc_fpin_congn_stats_update(struct Scsi_Host *shost,
  782. struct fc_tlv_desc *tlv)
  783. {
  784. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  785. struct fc_fn_congn_desc *congn = (struct fc_fn_congn_desc *)tlv;
  786. fc_cn_stats_update(be16_to_cpu(congn->event_type),
  787. &fc_host->fpin_stats);
  788. }
  789. /**
  790. * fc_host_fpin_rcv - routine to process a received FPIN.
  791. * @shost: host the FPIN was received on
  792. * @fpin_len: length of FPIN payload, in bytes
  793. * @fpin_buf: pointer to FPIN payload
  794. * @event_acknowledge: 1, if LLDD handles this event.
  795. * Notes:
  796. * This routine assumes no locks are held on entry.
  797. */
  798. void
  799. fc_host_fpin_rcv(struct Scsi_Host *shost, u32 fpin_len, char *fpin_buf,
  800. u8 event_acknowledge)
  801. {
  802. struct fc_els_fpin *fpin = (struct fc_els_fpin *)fpin_buf;
  803. struct fc_tlv_desc *tlv;
  804. u32 bytes_remain;
  805. u32 dtag;
  806. enum fc_host_event_code event_code =
  807. event_acknowledge ? FCH_EVT_LINK_FPIN_ACK : FCH_EVT_LINK_FPIN;
  808. /* Update Statistics */
  809. tlv = (struct fc_tlv_desc *)&fpin->fpin_desc[0];
  810. bytes_remain = fpin_len - offsetof(struct fc_els_fpin, fpin_desc);
  811. bytes_remain = min_t(u32, bytes_remain, be32_to_cpu(fpin->desc_len));
  812. while (bytes_remain >= FC_TLV_DESC_HDR_SZ &&
  813. bytes_remain >= FC_TLV_DESC_SZ_FROM_LENGTH(tlv)) {
  814. dtag = be32_to_cpu(tlv->desc_tag);
  815. switch (dtag) {
  816. case ELS_DTAG_LNK_INTEGRITY:
  817. fc_fpin_li_stats_update(shost, tlv);
  818. break;
  819. case ELS_DTAG_DELIVERY:
  820. fc_fpin_delivery_stats_update(shost, tlv);
  821. break;
  822. case ELS_DTAG_PEER_CONGEST:
  823. fc_fpin_peer_congn_stats_update(shost, tlv);
  824. break;
  825. case ELS_DTAG_CONGESTION:
  826. fc_fpin_congn_stats_update(shost, tlv);
  827. }
  828. bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
  829. tlv = fc_tlv_next_desc(tlv);
  830. }
  831. fc_host_post_fc_event(shost, fc_get_event_number(),
  832. event_code, fpin_len, fpin_buf, 0);
  833. }
  834. EXPORT_SYMBOL(fc_host_fpin_rcv);
  835. static __init int fc_transport_init(void)
  836. {
  837. int error;
  838. atomic_set(&fc_event_seq, 0);
  839. error = transport_class_register(&fc_host_class);
  840. if (error)
  841. return error;
  842. error = transport_class_register(&fc_vport_class);
  843. if (error)
  844. goto unreg_host_class;
  845. error = transport_class_register(&fc_rport_class);
  846. if (error)
  847. goto unreg_vport_class;
  848. error = transport_class_register(&fc_transport_class);
  849. if (error)
  850. goto unreg_rport_class;
  851. return 0;
  852. unreg_rport_class:
  853. transport_class_unregister(&fc_rport_class);
  854. unreg_vport_class:
  855. transport_class_unregister(&fc_vport_class);
  856. unreg_host_class:
  857. transport_class_unregister(&fc_host_class);
  858. return error;
  859. }
  860. static void __exit fc_transport_exit(void)
  861. {
  862. transport_class_unregister(&fc_transport_class);
  863. transport_class_unregister(&fc_rport_class);
  864. transport_class_unregister(&fc_host_class);
  865. transport_class_unregister(&fc_vport_class);
  866. }
  867. /*
  868. * FC Remote Port Attribute Management
  869. */
  870. #define fc_rport_show_function(field, format_string, sz, cast) \
  871. static ssize_t \
  872. show_fc_rport_##field (struct device *dev, \
  873. struct device_attribute *attr, char *buf) \
  874. { \
  875. struct fc_rport *rport = transport_class_to_rport(dev); \
  876. struct Scsi_Host *shost = rport_to_shost(rport); \
  877. struct fc_internal *i = to_fc_internal(shost->transportt); \
  878. if ((i->f->get_rport_##field) && \
  879. !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
  880. (rport->port_state == FC_PORTSTATE_DELETED) || \
  881. (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
  882. i->f->get_rport_##field(rport); \
  883. return snprintf(buf, sz, format_string, cast rport->field); \
  884. }
  885. #define fc_rport_store_function(field) \
  886. static ssize_t \
  887. store_fc_rport_##field(struct device *dev, \
  888. struct device_attribute *attr, \
  889. const char *buf, size_t count) \
  890. { \
  891. int val; \
  892. struct fc_rport *rport = transport_class_to_rport(dev); \
  893. struct Scsi_Host *shost = rport_to_shost(rport); \
  894. struct fc_internal *i = to_fc_internal(shost->transportt); \
  895. char *cp; \
  896. if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
  897. (rport->port_state == FC_PORTSTATE_DELETED) || \
  898. (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
  899. return -EBUSY; \
  900. val = simple_strtoul(buf, &cp, 0); \
  901. if (*cp && (*cp != '\n')) \
  902. return -EINVAL; \
  903. i->f->set_rport_##field(rport, val); \
  904. return count; \
  905. }
  906. #define fc_rport_rd_attr(field, format_string, sz) \
  907. fc_rport_show_function(field, format_string, sz, ) \
  908. static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
  909. show_fc_rport_##field, NULL)
  910. #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
  911. fc_rport_show_function(field, format_string, sz, (cast)) \
  912. static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
  913. show_fc_rport_##field, NULL)
  914. #define fc_rport_rw_attr(field, format_string, sz) \
  915. fc_rport_show_function(field, format_string, sz, ) \
  916. fc_rport_store_function(field) \
  917. static FC_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
  918. show_fc_rport_##field, \
  919. store_fc_rport_##field)
  920. #define fc_private_rport_show_function(field, format_string, sz, cast) \
  921. static ssize_t \
  922. show_fc_rport_##field (struct device *dev, \
  923. struct device_attribute *attr, char *buf) \
  924. { \
  925. struct fc_rport *rport = transport_class_to_rport(dev); \
  926. return snprintf(buf, sz, format_string, cast rport->field); \
  927. }
  928. #define fc_private_rport_rd_attr(field, format_string, sz) \
  929. fc_private_rport_show_function(field, format_string, sz, ) \
  930. static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
  931. show_fc_rport_##field, NULL)
  932. #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
  933. fc_private_rport_show_function(field, format_string, sz, (cast)) \
  934. static FC_DEVICE_ATTR(rport, field, S_IRUGO, \
  935. show_fc_rport_##field, NULL)
  936. #define fc_private_rport_rd_enum_attr(title, maxlen) \
  937. static ssize_t \
  938. show_fc_rport_##title (struct device *dev, \
  939. struct device_attribute *attr, char *buf) \
  940. { \
  941. struct fc_rport *rport = transport_class_to_rport(dev); \
  942. const char *name; \
  943. name = get_fc_##title##_name(rport->title); \
  944. if (!name) \
  945. return -EINVAL; \
  946. return snprintf(buf, maxlen, "%s\n", name); \
  947. } \
  948. static FC_DEVICE_ATTR(rport, title, S_IRUGO, \
  949. show_fc_rport_##title, NULL)
  950. #define SETUP_RPORT_ATTRIBUTE_RD(field) \
  951. i->private_rport_attrs[count] = device_attr_rport_##field; \
  952. i->private_rport_attrs[count].attr.mode = S_IRUGO; \
  953. i->private_rport_attrs[count].store = NULL; \
  954. i->rport_attrs[count] = &i->private_rport_attrs[count]; \
  955. if (i->f->show_rport_##field) \
  956. count++
  957. #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
  958. i->private_rport_attrs[count] = device_attr_rport_##field; \
  959. i->private_rport_attrs[count].attr.mode = S_IRUGO; \
  960. i->private_rport_attrs[count].store = NULL; \
  961. i->rport_attrs[count] = &i->private_rport_attrs[count]; \
  962. count++
  963. #define SETUP_RPORT_ATTRIBUTE_RW(field) \
  964. i->private_rport_attrs[count] = device_attr_rport_##field; \
  965. if (!i->f->set_rport_##field) { \
  966. i->private_rport_attrs[count].attr.mode = S_IRUGO; \
  967. i->private_rport_attrs[count].store = NULL; \
  968. } \
  969. i->rport_attrs[count] = &i->private_rport_attrs[count]; \
  970. if (i->f->show_rport_##field) \
  971. count++
  972. #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
  973. { \
  974. i->private_rport_attrs[count] = device_attr_rport_##field; \
  975. i->rport_attrs[count] = &i->private_rport_attrs[count]; \
  976. count++; \
  977. }
  978. /* The FC Transport Remote Port Attributes: */
  979. /* Fixed Remote Port Attributes */
  980. fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
  981. static ssize_t
  982. show_fc_rport_supported_classes (struct device *dev,
  983. struct device_attribute *attr, char *buf)
  984. {
  985. struct fc_rport *rport = transport_class_to_rport(dev);
  986. if (rport->supported_classes == FC_COS_UNSPECIFIED)
  987. return snprintf(buf, 20, "unspecified\n");
  988. return get_fc_cos_names(rport->supported_classes, buf);
  989. }
  990. static FC_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
  991. show_fc_rport_supported_classes, NULL);
  992. /* Dynamic Remote Port Attributes */
  993. /*
  994. * dev_loss_tmo attribute
  995. */
  996. static int fc_str_to_dev_loss(const char *buf, unsigned long *val)
  997. {
  998. char *cp;
  999. *val = simple_strtoul(buf, &cp, 0);
  1000. if (*cp && (*cp != '\n'))
  1001. return -EINVAL;
  1002. /*
  1003. * Check for overflow; dev_loss_tmo is u32
  1004. */
  1005. if (*val > UINT_MAX)
  1006. return -EINVAL;
  1007. return 0;
  1008. }
  1009. static int fc_rport_set_dev_loss_tmo(struct fc_rport *rport,
  1010. unsigned long val)
  1011. {
  1012. struct Scsi_Host *shost = rport_to_shost(rport);
  1013. struct fc_internal *i = to_fc_internal(shost->transportt);
  1014. if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
  1015. (rport->port_state == FC_PORTSTATE_DELETED) ||
  1016. (rport->port_state == FC_PORTSTATE_NOTPRESENT))
  1017. return -EBUSY;
  1018. /*
  1019. * Check for overflow; dev_loss_tmo is u32
  1020. */
  1021. if (val > UINT_MAX)
  1022. return -EINVAL;
  1023. /*
  1024. * If fast_io_fail is off we have to cap
  1025. * dev_loss_tmo at SCSI_DEVICE_BLOCK_MAX_TIMEOUT
  1026. */
  1027. if (rport->fast_io_fail_tmo == -1 &&
  1028. val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
  1029. return -EINVAL;
  1030. i->f->set_rport_dev_loss_tmo(rport, val);
  1031. return 0;
  1032. }
  1033. fc_rport_show_function(dev_loss_tmo, "%u\n", 20, )
  1034. static ssize_t
  1035. store_fc_rport_dev_loss_tmo(struct device *dev, struct device_attribute *attr,
  1036. const char *buf, size_t count)
  1037. {
  1038. struct fc_rport *rport = transport_class_to_rport(dev);
  1039. unsigned long val;
  1040. int rc;
  1041. rc = fc_str_to_dev_loss(buf, &val);
  1042. if (rc)
  1043. return rc;
  1044. rc = fc_rport_set_dev_loss_tmo(rport, val);
  1045. if (rc)
  1046. return rc;
  1047. return count;
  1048. }
  1049. static FC_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
  1050. show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
  1051. /* Private Remote Port Attributes */
  1052. fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
  1053. fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
  1054. fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
  1055. static ssize_t
  1056. show_fc_rport_roles (struct device *dev, struct device_attribute *attr,
  1057. char *buf)
  1058. {
  1059. struct fc_rport *rport = transport_class_to_rport(dev);
  1060. /* identify any roles that are port_id specific */
  1061. if ((rport->port_id != -1) &&
  1062. (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
  1063. FC_WELLKNOWN_PORTID_MASK) {
  1064. switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
  1065. case FC_FPORT_PORTID:
  1066. return snprintf(buf, 30, "Fabric Port\n");
  1067. case FC_FABCTLR_PORTID:
  1068. return snprintf(buf, 30, "Fabric Controller\n");
  1069. case FC_DIRSRVR_PORTID:
  1070. return snprintf(buf, 30, "Directory Server\n");
  1071. case FC_TIMESRVR_PORTID:
  1072. return snprintf(buf, 30, "Time Server\n");
  1073. case FC_MGMTSRVR_PORTID:
  1074. return snprintf(buf, 30, "Management Server\n");
  1075. default:
  1076. return snprintf(buf, 30, "Unknown Fabric Entity\n");
  1077. }
  1078. } else {
  1079. if (rport->roles == FC_PORT_ROLE_UNKNOWN)
  1080. return snprintf(buf, 20, "unknown\n");
  1081. return get_fc_port_roles_names(rport->roles, buf);
  1082. }
  1083. }
  1084. static FC_DEVICE_ATTR(rport, roles, S_IRUGO,
  1085. show_fc_rport_roles, NULL);
  1086. static ssize_t fc_rport_set_marginal_state(struct device *dev,
  1087. struct device_attribute *attr,
  1088. const char *buf, size_t count)
  1089. {
  1090. struct fc_rport *rport = transport_class_to_rport(dev);
  1091. enum fc_port_state port_state;
  1092. int ret = 0;
  1093. ret = get_fc_port_state_match(buf, &port_state);
  1094. if (ret)
  1095. return -EINVAL;
  1096. if (port_state == FC_PORTSTATE_MARGINAL) {
  1097. /*
  1098. * Change the state to Marginal only if the
  1099. * current rport state is Online
  1100. * Allow only Online->Marginal
  1101. */
  1102. if (rport->port_state == FC_PORTSTATE_ONLINE)
  1103. rport->port_state = port_state;
  1104. else if (port_state != rport->port_state)
  1105. return -EINVAL;
  1106. } else if (port_state == FC_PORTSTATE_ONLINE) {
  1107. /*
  1108. * Change the state to Online only if the
  1109. * current rport state is Marginal
  1110. * Allow only Marginal->Online
  1111. */
  1112. if (rport->port_state == FC_PORTSTATE_MARGINAL)
  1113. rport->port_state = port_state;
  1114. else if (port_state != rport->port_state)
  1115. return -EINVAL;
  1116. } else
  1117. return -EINVAL;
  1118. return count;
  1119. }
  1120. static ssize_t
  1121. show_fc_rport_port_state(struct device *dev,
  1122. struct device_attribute *attr, char *buf)
  1123. {
  1124. const char *name;
  1125. struct fc_rport *rport = transport_class_to_rport(dev);
  1126. name = get_fc_port_state_name(rport->port_state);
  1127. if (!name)
  1128. return -EINVAL;
  1129. return snprintf(buf, 20, "%s\n", name);
  1130. }
  1131. static FC_DEVICE_ATTR(rport, port_state, 0444 | 0200,
  1132. show_fc_rport_port_state, fc_rport_set_marginal_state);
  1133. fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
  1134. /*
  1135. * fast_io_fail_tmo attribute
  1136. */
  1137. static ssize_t
  1138. show_fc_rport_fast_io_fail_tmo (struct device *dev,
  1139. struct device_attribute *attr, char *buf)
  1140. {
  1141. struct fc_rport *rport = transport_class_to_rport(dev);
  1142. if (rport->fast_io_fail_tmo == -1)
  1143. return snprintf(buf, 5, "off\n");
  1144. return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
  1145. }
  1146. static ssize_t
  1147. store_fc_rport_fast_io_fail_tmo(struct device *dev,
  1148. struct device_attribute *attr, const char *buf,
  1149. size_t count)
  1150. {
  1151. int val;
  1152. char *cp;
  1153. struct fc_rport *rport = transport_class_to_rport(dev);
  1154. if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
  1155. (rport->port_state == FC_PORTSTATE_DELETED) ||
  1156. (rport->port_state == FC_PORTSTATE_NOTPRESENT))
  1157. return -EBUSY;
  1158. if (strncmp(buf, "off", 3) == 0)
  1159. rport->fast_io_fail_tmo = -1;
  1160. else {
  1161. val = simple_strtoul(buf, &cp, 0);
  1162. if ((*cp && (*cp != '\n')) || (val < 0))
  1163. return -EINVAL;
  1164. /*
  1165. * Cap fast_io_fail by dev_loss_tmo or
  1166. * SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
  1167. */
  1168. if ((val >= rport->dev_loss_tmo) ||
  1169. (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
  1170. return -EINVAL;
  1171. rport->fast_io_fail_tmo = val;
  1172. }
  1173. return count;
  1174. }
  1175. static FC_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
  1176. show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
  1177. #define fc_rport_encryption(name) \
  1178. static ssize_t fc_rport_encinfo_##name(struct device *cd, \
  1179. struct device_attribute *attr, \
  1180. char *buf) \
  1181. { \
  1182. struct fc_rport *rport = transport_class_to_rport(cd); \
  1183. struct Scsi_Host *shost = rport_to_shost(rport); \
  1184. struct fc_internal *i = to_fc_internal(shost->transportt); \
  1185. struct fc_encryption_info *info; \
  1186. ssize_t ret = -ENOENT; \
  1187. u32 data; \
  1188. \
  1189. if (i->f->get_fc_rport_enc_info) { \
  1190. info = (i->f->get_fc_rport_enc_info)(rport); \
  1191. if (info) { \
  1192. data = info->name; \
  1193. if (!strcmp(#name, "status")) { \
  1194. ret = scnprintf(buf, \
  1195. FC_RPORT_ENCRYPTION_STATUS_MAX_LEN, \
  1196. "%s\n", \
  1197. data ? "Encrypted" : "Unencrypted"); \
  1198. } \
  1199. } \
  1200. } \
  1201. return ret; \
  1202. } \
  1203. static FC_DEVICE_ATTR(rport, encryption_##name, 0444, fc_rport_encinfo_##name, NULL) \
  1204. fc_rport_encryption(status);
  1205. static struct attribute *fc_rport_encryption_attrs[] = {
  1206. &device_attr_rport_encryption_status.attr,
  1207. NULL
  1208. };
  1209. static struct attribute_group fc_rport_encryption_group = {
  1210. .name = "encryption",
  1211. .attrs = fc_rport_encryption_attrs,
  1212. };
  1213. #define fc_rport_fpin_statistic(name) \
  1214. static ssize_t fc_rport_fpinstat_##name(struct device *cd, \
  1215. struct device_attribute *attr, \
  1216. char *buf) \
  1217. { \
  1218. struct fc_rport *rport = transport_class_to_rport(cd); \
  1219. \
  1220. return snprintf(buf, 20, "0x%llx\n", rport->fpin_stats.name); \
  1221. } \
  1222. static FC_DEVICE_ATTR(rport, fpin_##name, 0444, fc_rport_fpinstat_##name, NULL)
  1223. fc_rport_fpin_statistic(dn);
  1224. fc_rport_fpin_statistic(dn_unknown);
  1225. fc_rport_fpin_statistic(dn_timeout);
  1226. fc_rport_fpin_statistic(dn_unable_to_route);
  1227. fc_rport_fpin_statistic(dn_device_specific);
  1228. fc_rport_fpin_statistic(cn);
  1229. fc_rport_fpin_statistic(cn_clear);
  1230. fc_rport_fpin_statistic(cn_lost_credit);
  1231. fc_rport_fpin_statistic(cn_credit_stall);
  1232. fc_rport_fpin_statistic(cn_oversubscription);
  1233. fc_rport_fpin_statistic(cn_device_specific);
  1234. fc_rport_fpin_statistic(li);
  1235. fc_rport_fpin_statistic(li_failure_unknown);
  1236. fc_rport_fpin_statistic(li_link_failure_count);
  1237. fc_rport_fpin_statistic(li_loss_of_sync_count);
  1238. fc_rport_fpin_statistic(li_loss_of_signals_count);
  1239. fc_rport_fpin_statistic(li_prim_seq_err_count);
  1240. fc_rport_fpin_statistic(li_invalid_tx_word_count);
  1241. fc_rport_fpin_statistic(li_invalid_crc_count);
  1242. fc_rport_fpin_statistic(li_device_specific);
  1243. static struct attribute *fc_rport_statistics_attrs[] = {
  1244. &device_attr_rport_fpin_dn.attr,
  1245. &device_attr_rport_fpin_dn_unknown.attr,
  1246. &device_attr_rport_fpin_dn_timeout.attr,
  1247. &device_attr_rport_fpin_dn_unable_to_route.attr,
  1248. &device_attr_rport_fpin_dn_device_specific.attr,
  1249. &device_attr_rport_fpin_li.attr,
  1250. &device_attr_rport_fpin_li_failure_unknown.attr,
  1251. &device_attr_rport_fpin_li_link_failure_count.attr,
  1252. &device_attr_rport_fpin_li_loss_of_sync_count.attr,
  1253. &device_attr_rport_fpin_li_loss_of_signals_count.attr,
  1254. &device_attr_rport_fpin_li_prim_seq_err_count.attr,
  1255. &device_attr_rport_fpin_li_invalid_tx_word_count.attr,
  1256. &device_attr_rport_fpin_li_invalid_crc_count.attr,
  1257. &device_attr_rport_fpin_li_device_specific.attr,
  1258. &device_attr_rport_fpin_cn.attr,
  1259. &device_attr_rport_fpin_cn_clear.attr,
  1260. &device_attr_rport_fpin_cn_lost_credit.attr,
  1261. &device_attr_rport_fpin_cn_credit_stall.attr,
  1262. &device_attr_rport_fpin_cn_oversubscription.attr,
  1263. &device_attr_rport_fpin_cn_device_specific.attr,
  1264. NULL
  1265. };
  1266. static struct attribute_group fc_rport_statistics_group = {
  1267. .name = "statistics",
  1268. .attrs = fc_rport_statistics_attrs,
  1269. };
  1270. /*
  1271. * FC SCSI Target Attribute Management
  1272. */
  1273. /*
  1274. * Note: in the target show function we recognize when the remote
  1275. * port is in the hierarchy and do not allow the driver to get
  1276. * involved in sysfs functions. The driver only gets involved if
  1277. * it's the "old" style that doesn't use rports.
  1278. */
  1279. #define fc_starget_show_function(field, format_string, sz, cast) \
  1280. static ssize_t \
  1281. show_fc_starget_##field (struct device *dev, \
  1282. struct device_attribute *attr, char *buf) \
  1283. { \
  1284. struct scsi_target *starget = transport_class_to_starget(dev); \
  1285. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  1286. struct fc_internal *i = to_fc_internal(shost->transportt); \
  1287. struct fc_rport *rport = starget_to_rport(starget); \
  1288. if (rport) \
  1289. fc_starget_##field(starget) = rport->field; \
  1290. else if (i->f->get_starget_##field) \
  1291. i->f->get_starget_##field(starget); \
  1292. return snprintf(buf, sz, format_string, \
  1293. cast fc_starget_##field(starget)); \
  1294. }
  1295. #define fc_starget_rd_attr(field, format_string, sz) \
  1296. fc_starget_show_function(field, format_string, sz, ) \
  1297. static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
  1298. show_fc_starget_##field, NULL)
  1299. #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
  1300. fc_starget_show_function(field, format_string, sz, (cast)) \
  1301. static FC_DEVICE_ATTR(starget, field, S_IRUGO, \
  1302. show_fc_starget_##field, NULL)
  1303. #define SETUP_STARGET_ATTRIBUTE_RD(field) \
  1304. i->private_starget_attrs[count] = device_attr_starget_##field; \
  1305. i->private_starget_attrs[count].attr.mode = S_IRUGO; \
  1306. i->private_starget_attrs[count].store = NULL; \
  1307. i->starget_attrs[count] = &i->private_starget_attrs[count]; \
  1308. if (i->f->show_starget_##field) \
  1309. count++
  1310. #define SETUP_STARGET_ATTRIBUTE_RW(field) \
  1311. i->private_starget_attrs[count] = device_attr_starget_##field; \
  1312. if (!i->f->set_starget_##field) { \
  1313. i->private_starget_attrs[count].attr.mode = S_IRUGO; \
  1314. i->private_starget_attrs[count].store = NULL; \
  1315. } \
  1316. i->starget_attrs[count] = &i->private_starget_attrs[count]; \
  1317. if (i->f->show_starget_##field) \
  1318. count++
  1319. /* The FC Transport SCSI Target Attributes: */
  1320. fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
  1321. fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
  1322. fc_starget_rd_attr(port_id, "0x%06x\n", 20);
  1323. /*
  1324. * FC Virtual Port Attribute Management
  1325. */
  1326. #define fc_vport_show_function(field, format_string, sz, cast) \
  1327. static ssize_t \
  1328. show_fc_vport_##field (struct device *dev, \
  1329. struct device_attribute *attr, char *buf) \
  1330. { \
  1331. struct fc_vport *vport = transport_class_to_vport(dev); \
  1332. struct Scsi_Host *shost = vport_to_shost(vport); \
  1333. struct fc_internal *i = to_fc_internal(shost->transportt); \
  1334. if ((i->f->get_vport_##field) && \
  1335. !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
  1336. i->f->get_vport_##field(vport); \
  1337. return snprintf(buf, sz, format_string, cast vport->field); \
  1338. }
  1339. #define fc_vport_store_function(field) \
  1340. static ssize_t \
  1341. store_fc_vport_##field(struct device *dev, \
  1342. struct device_attribute *attr, \
  1343. const char *buf, size_t count) \
  1344. { \
  1345. int val; \
  1346. struct fc_vport *vport = transport_class_to_vport(dev); \
  1347. struct Scsi_Host *shost = vport_to_shost(vport); \
  1348. struct fc_internal *i = to_fc_internal(shost->transportt); \
  1349. char *cp; \
  1350. if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
  1351. return -EBUSY; \
  1352. val = simple_strtoul(buf, &cp, 0); \
  1353. if (*cp && (*cp != '\n')) \
  1354. return -EINVAL; \
  1355. i->f->set_vport_##field(vport, val); \
  1356. return count; \
  1357. }
  1358. #define fc_vport_store_str_function(field, slen) \
  1359. static ssize_t \
  1360. store_fc_vport_##field(struct device *dev, \
  1361. struct device_attribute *attr, \
  1362. const char *buf, size_t count) \
  1363. { \
  1364. struct fc_vport *vport = transport_class_to_vport(dev); \
  1365. struct Scsi_Host *shost = vport_to_shost(vport); \
  1366. struct fc_internal *i = to_fc_internal(shost->transportt); \
  1367. unsigned int cnt=count; \
  1368. \
  1369. /* count may include a LF at end of string */ \
  1370. if (buf[cnt-1] == '\n') \
  1371. cnt--; \
  1372. if (cnt > ((slen) - 1)) \
  1373. return -EINVAL; \
  1374. memcpy(vport->field, buf, cnt); \
  1375. i->f->set_vport_##field(vport); \
  1376. return count; \
  1377. }
  1378. #define fc_vport_rd_attr(field, format_string, sz) \
  1379. fc_vport_show_function(field, format_string, sz, ) \
  1380. static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
  1381. show_fc_vport_##field, NULL)
  1382. #define fc_vport_rd_attr_cast(field, format_string, sz, cast) \
  1383. fc_vport_show_function(field, format_string, sz, (cast)) \
  1384. static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
  1385. show_fc_vport_##field, NULL)
  1386. #define fc_vport_rw_attr(field, format_string, sz) \
  1387. fc_vport_show_function(field, format_string, sz, ) \
  1388. fc_vport_store_function(field) \
  1389. static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
  1390. show_fc_vport_##field, \
  1391. store_fc_vport_##field)
  1392. #define fc_private_vport_show_function(field, format_string, sz, cast) \
  1393. static ssize_t \
  1394. show_fc_vport_##field (struct device *dev, \
  1395. struct device_attribute *attr, char *buf) \
  1396. { \
  1397. struct fc_vport *vport = transport_class_to_vport(dev); \
  1398. return snprintf(buf, sz, format_string, cast vport->field); \
  1399. }
  1400. #define fc_private_vport_store_u32_function(field) \
  1401. static ssize_t \
  1402. store_fc_vport_##field(struct device *dev, \
  1403. struct device_attribute *attr, \
  1404. const char *buf, size_t count) \
  1405. { \
  1406. u32 val; \
  1407. struct fc_vport *vport = transport_class_to_vport(dev); \
  1408. char *cp; \
  1409. if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
  1410. return -EBUSY; \
  1411. val = simple_strtoul(buf, &cp, 0); \
  1412. if (*cp && (*cp != '\n')) \
  1413. return -EINVAL; \
  1414. vport->field = val; \
  1415. return count; \
  1416. }
  1417. #define fc_private_vport_rd_attr(field, format_string, sz) \
  1418. fc_private_vport_show_function(field, format_string, sz, ) \
  1419. static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
  1420. show_fc_vport_##field, NULL)
  1421. #define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \
  1422. fc_private_vport_show_function(field, format_string, sz, (cast)) \
  1423. static FC_DEVICE_ATTR(vport, field, S_IRUGO, \
  1424. show_fc_vport_##field, NULL)
  1425. #define fc_private_vport_rw_u32_attr(field, format_string, sz) \
  1426. fc_private_vport_show_function(field, format_string, sz, ) \
  1427. fc_private_vport_store_u32_function(field) \
  1428. static FC_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
  1429. show_fc_vport_##field, \
  1430. store_fc_vport_##field)
  1431. #define fc_private_vport_rd_enum_attr(title, maxlen) \
  1432. static ssize_t \
  1433. show_fc_vport_##title (struct device *dev, \
  1434. struct device_attribute *attr, \
  1435. char *buf) \
  1436. { \
  1437. struct fc_vport *vport = transport_class_to_vport(dev); \
  1438. const char *name; \
  1439. name = get_fc_##title##_name(vport->title); \
  1440. if (!name) \
  1441. return -EINVAL; \
  1442. return snprintf(buf, maxlen, "%s\n", name); \
  1443. } \
  1444. static FC_DEVICE_ATTR(vport, title, S_IRUGO, \
  1445. show_fc_vport_##title, NULL)
  1446. #define SETUP_VPORT_ATTRIBUTE_RD(field) \
  1447. i->private_vport_attrs[count] = device_attr_vport_##field; \
  1448. i->private_vport_attrs[count].attr.mode = S_IRUGO; \
  1449. i->private_vport_attrs[count].store = NULL; \
  1450. i->vport_attrs[count] = &i->private_vport_attrs[count]; \
  1451. if (i->f->get_##field) \
  1452. count++
  1453. /* NOTE: Above MACRO differs: checks function not show bit */
  1454. #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field) \
  1455. i->private_vport_attrs[count] = device_attr_vport_##field; \
  1456. i->private_vport_attrs[count].attr.mode = S_IRUGO; \
  1457. i->private_vport_attrs[count].store = NULL; \
  1458. i->vport_attrs[count] = &i->private_vport_attrs[count]; \
  1459. count++
  1460. #define SETUP_VPORT_ATTRIBUTE_WR(field) \
  1461. i->private_vport_attrs[count] = device_attr_vport_##field; \
  1462. i->vport_attrs[count] = &i->private_vport_attrs[count]; \
  1463. if (i->f->field) \
  1464. count++
  1465. /* NOTE: Above MACRO differs: checks function */
  1466. #define SETUP_VPORT_ATTRIBUTE_RW(field) \
  1467. i->private_vport_attrs[count] = device_attr_vport_##field; \
  1468. if (!i->f->set_vport_##field) { \
  1469. i->private_vport_attrs[count].attr.mode = S_IRUGO; \
  1470. i->private_vport_attrs[count].store = NULL; \
  1471. } \
  1472. i->vport_attrs[count] = &i->private_vport_attrs[count]; \
  1473. count++
  1474. /* NOTE: Above MACRO differs: does not check show bit */
  1475. #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \
  1476. { \
  1477. i->private_vport_attrs[count] = device_attr_vport_##field; \
  1478. i->vport_attrs[count] = &i->private_vport_attrs[count]; \
  1479. count++; \
  1480. }
  1481. /* The FC Transport Virtual Port Attributes: */
  1482. /* Fixed Virtual Port Attributes */
  1483. /* Dynamic Virtual Port Attributes */
  1484. /* Private Virtual Port Attributes */
  1485. fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
  1486. fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
  1487. fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
  1488. fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
  1489. static ssize_t
  1490. show_fc_vport_roles (struct device *dev, struct device_attribute *attr,
  1491. char *buf)
  1492. {
  1493. struct fc_vport *vport = transport_class_to_vport(dev);
  1494. if (vport->roles == FC_PORT_ROLE_UNKNOWN)
  1495. return snprintf(buf, 20, "unknown\n");
  1496. return get_fc_port_roles_names(vport->roles, buf);
  1497. }
  1498. static FC_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
  1499. fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
  1500. fc_private_vport_show_function(symbolic_name, "%s\n",
  1501. FC_VPORT_SYMBOLIC_NAMELEN + 1, )
  1502. fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
  1503. static FC_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
  1504. show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
  1505. static ssize_t
  1506. store_fc_vport_delete(struct device *dev, struct device_attribute *attr,
  1507. const char *buf, size_t count)
  1508. {
  1509. struct fc_vport *vport = transport_class_to_vport(dev);
  1510. struct Scsi_Host *shost = vport_to_shost(vport);
  1511. unsigned long flags;
  1512. spin_lock_irqsave(shost->host_lock, flags);
  1513. if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) {
  1514. spin_unlock_irqrestore(shost->host_lock, flags);
  1515. return -EBUSY;
  1516. }
  1517. vport->flags |= FC_VPORT_DELETING;
  1518. spin_unlock_irqrestore(shost->host_lock, flags);
  1519. fc_queue_work(shost, &vport->vport_delete_work);
  1520. return count;
  1521. }
  1522. static FC_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
  1523. NULL, store_fc_vport_delete);
  1524. /*
  1525. * Enable/Disable vport
  1526. * Write "1" to disable, write "0" to enable
  1527. */
  1528. static ssize_t
  1529. store_fc_vport_disable(struct device *dev, struct device_attribute *attr,
  1530. const char *buf,
  1531. size_t count)
  1532. {
  1533. struct fc_vport *vport = transport_class_to_vport(dev);
  1534. struct Scsi_Host *shost = vport_to_shost(vport);
  1535. struct fc_internal *i = to_fc_internal(shost->transportt);
  1536. int stat;
  1537. if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
  1538. return -EBUSY;
  1539. if (*buf == '0') {
  1540. if (vport->vport_state != FC_VPORT_DISABLED)
  1541. return -EALREADY;
  1542. } else if (*buf == '1') {
  1543. if (vport->vport_state == FC_VPORT_DISABLED)
  1544. return -EALREADY;
  1545. } else
  1546. return -EINVAL;
  1547. stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
  1548. return stat ? stat : count;
  1549. }
  1550. static FC_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
  1551. NULL, store_fc_vport_disable);
  1552. /*
  1553. * Host Attribute Management
  1554. */
  1555. #define fc_host_show_function(field, format_string, sz, cast) \
  1556. static ssize_t \
  1557. show_fc_host_##field (struct device *dev, \
  1558. struct device_attribute *attr, char *buf) \
  1559. { \
  1560. struct Scsi_Host *shost = transport_class_to_shost(dev); \
  1561. struct fc_internal *i = to_fc_internal(shost->transportt); \
  1562. if (i->f->get_host_##field) \
  1563. i->f->get_host_##field(shost); \
  1564. return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
  1565. }
  1566. #define fc_host_store_function(field) \
  1567. static ssize_t \
  1568. store_fc_host_##field(struct device *dev, \
  1569. struct device_attribute *attr, \
  1570. const char *buf, size_t count) \
  1571. { \
  1572. int val; \
  1573. struct Scsi_Host *shost = transport_class_to_shost(dev); \
  1574. struct fc_internal *i = to_fc_internal(shost->transportt); \
  1575. char *cp; \
  1576. \
  1577. val = simple_strtoul(buf, &cp, 0); \
  1578. if (*cp && (*cp != '\n')) \
  1579. return -EINVAL; \
  1580. i->f->set_host_##field(shost, val); \
  1581. return count; \
  1582. }
  1583. #define fc_host_store_str_function(field, slen) \
  1584. static ssize_t \
  1585. store_fc_host_##field(struct device *dev, \
  1586. struct device_attribute *attr, \
  1587. const char *buf, size_t count) \
  1588. { \
  1589. struct Scsi_Host *shost = transport_class_to_shost(dev); \
  1590. struct fc_internal *i = to_fc_internal(shost->transportt); \
  1591. unsigned int cnt=count; \
  1592. \
  1593. /* count may include a LF at end of string */ \
  1594. if (buf[cnt-1] == '\n') \
  1595. cnt--; \
  1596. if (cnt > ((slen) - 1)) \
  1597. return -EINVAL; \
  1598. memcpy(fc_host_##field(shost), buf, cnt); \
  1599. i->f->set_host_##field(shost); \
  1600. return count; \
  1601. }
  1602. #define fc_host_rd_attr(field, format_string, sz) \
  1603. fc_host_show_function(field, format_string, sz, ) \
  1604. static FC_DEVICE_ATTR(host, field, S_IRUGO, \
  1605. show_fc_host_##field, NULL)
  1606. #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
  1607. fc_host_show_function(field, format_string, sz, (cast)) \
  1608. static FC_DEVICE_ATTR(host, field, S_IRUGO, \
  1609. show_fc_host_##field, NULL)
  1610. #define fc_host_rw_attr(field, format_string, sz) \
  1611. fc_host_show_function(field, format_string, sz, ) \
  1612. fc_host_store_function(field) \
  1613. static FC_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
  1614. show_fc_host_##field, \
  1615. store_fc_host_##field)
  1616. #define fc_host_rd_enum_attr(title, maxlen) \
  1617. static ssize_t \
  1618. show_fc_host_##title (struct device *dev, \
  1619. struct device_attribute *attr, char *buf) \
  1620. { \
  1621. struct Scsi_Host *shost = transport_class_to_shost(dev); \
  1622. struct fc_internal *i = to_fc_internal(shost->transportt); \
  1623. const char *name; \
  1624. if (i->f->get_host_##title) \
  1625. i->f->get_host_##title(shost); \
  1626. name = get_fc_##title##_name(fc_host_##title(shost)); \
  1627. if (!name) \
  1628. return -EINVAL; \
  1629. return snprintf(buf, maxlen, "%s\n", name); \
  1630. } \
  1631. static FC_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
  1632. #define SETUP_HOST_ATTRIBUTE_RD(field) \
  1633. i->private_host_attrs[count] = device_attr_host_##field; \
  1634. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  1635. i->private_host_attrs[count].store = NULL; \
  1636. i->host_attrs[count] = &i->private_host_attrs[count]; \
  1637. if (i->f->show_host_##field) \
  1638. count++
  1639. #define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
  1640. i->private_host_attrs[count] = device_attr_host_##field; \
  1641. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  1642. i->private_host_attrs[count].store = NULL; \
  1643. i->host_attrs[count] = &i->private_host_attrs[count]; \
  1644. count++
  1645. #define SETUP_HOST_ATTRIBUTE_RW(field) \
  1646. i->private_host_attrs[count] = device_attr_host_##field; \
  1647. if (!i->f->set_host_##field) { \
  1648. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  1649. i->private_host_attrs[count].store = NULL; \
  1650. } \
  1651. i->host_attrs[count] = &i->private_host_attrs[count]; \
  1652. if (i->f->show_host_##field) \
  1653. count++
  1654. #define fc_private_host_show_function(field, format_string, sz, cast) \
  1655. static ssize_t \
  1656. show_fc_host_##field (struct device *dev, \
  1657. struct device_attribute *attr, char *buf) \
  1658. { \
  1659. struct Scsi_Host *shost = transport_class_to_shost(dev); \
  1660. return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
  1661. }
  1662. #define fc_private_host_rd_attr(field, format_string, sz) \
  1663. fc_private_host_show_function(field, format_string, sz, ) \
  1664. static FC_DEVICE_ATTR(host, field, S_IRUGO, \
  1665. show_fc_host_##field, NULL)
  1666. #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
  1667. fc_private_host_show_function(field, format_string, sz, (cast)) \
  1668. static FC_DEVICE_ATTR(host, field, S_IRUGO, \
  1669. show_fc_host_##field, NULL)
  1670. #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
  1671. i->private_host_attrs[count] = device_attr_host_##field; \
  1672. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  1673. i->private_host_attrs[count].store = NULL; \
  1674. i->host_attrs[count] = &i->private_host_attrs[count]; \
  1675. count++
  1676. #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
  1677. { \
  1678. i->private_host_attrs[count] = device_attr_host_##field; \
  1679. i->host_attrs[count] = &i->private_host_attrs[count]; \
  1680. count++; \
  1681. }
  1682. /* Fixed Host Attributes */
  1683. static ssize_t
  1684. show_fc_host_supported_classes (struct device *dev,
  1685. struct device_attribute *attr, char *buf)
  1686. {
  1687. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1688. if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
  1689. return snprintf(buf, 20, "unspecified\n");
  1690. return get_fc_cos_names(fc_host_supported_classes(shost), buf);
  1691. }
  1692. static FC_DEVICE_ATTR(host, supported_classes, S_IRUGO,
  1693. show_fc_host_supported_classes, NULL);
  1694. static ssize_t
  1695. show_fc_host_supported_fc4s (struct device *dev,
  1696. struct device_attribute *attr, char *buf)
  1697. {
  1698. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1699. return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
  1700. }
  1701. static FC_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
  1702. show_fc_host_supported_fc4s, NULL);
  1703. static ssize_t
  1704. show_fc_host_supported_speeds (struct device *dev,
  1705. struct device_attribute *attr, char *buf)
  1706. {
  1707. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1708. if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
  1709. return snprintf(buf, 20, "unknown\n");
  1710. return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
  1711. }
  1712. static FC_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
  1713. show_fc_host_supported_speeds, NULL);
  1714. fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
  1715. fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
  1716. fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
  1717. unsigned long long);
  1718. fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
  1719. fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
  1720. fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
  1721. fc_private_host_rd_attr(manufacturer, "%s\n", FC_SERIAL_NUMBER_SIZE + 1);
  1722. fc_private_host_rd_attr(model, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
  1723. fc_private_host_rd_attr(model_description, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
  1724. fc_private_host_rd_attr(hardware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
  1725. fc_private_host_rd_attr(driver_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
  1726. fc_private_host_rd_attr(firmware_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
  1727. fc_private_host_rd_attr(optionrom_version, "%s\n", FC_VERSION_STRING_SIZE + 1);
  1728. /* Dynamic Host Attributes */
  1729. static ssize_t
  1730. show_fc_host_active_fc4s (struct device *dev,
  1731. struct device_attribute *attr, char *buf)
  1732. {
  1733. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1734. struct fc_internal *i = to_fc_internal(shost->transportt);
  1735. if (i->f->get_host_active_fc4s)
  1736. i->f->get_host_active_fc4s(shost);
  1737. return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
  1738. }
  1739. static FC_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
  1740. show_fc_host_active_fc4s, NULL);
  1741. static ssize_t
  1742. show_fc_host_speed (struct device *dev,
  1743. struct device_attribute *attr, char *buf)
  1744. {
  1745. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1746. struct fc_internal *i = to_fc_internal(shost->transportt);
  1747. if (i->f->get_host_speed)
  1748. i->f->get_host_speed(shost);
  1749. if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
  1750. return snprintf(buf, 20, "unknown\n");
  1751. return get_fc_port_speed_names(fc_host_speed(shost), buf);
  1752. }
  1753. static FC_DEVICE_ATTR(host, speed, S_IRUGO,
  1754. show_fc_host_speed, NULL);
  1755. fc_host_rd_attr(port_id, "0x%06x\n", 20);
  1756. fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
  1757. fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
  1758. fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
  1759. fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
  1760. fc_private_host_show_function(system_hostname, "%s\n",
  1761. FC_SYMBOLIC_NAME_SIZE + 1, )
  1762. fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
  1763. static FC_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
  1764. show_fc_host_system_hostname, store_fc_host_system_hostname);
  1765. /* Private Host Attributes */
  1766. static ssize_t
  1767. show_fc_private_host_tgtid_bind_type(struct device *dev,
  1768. struct device_attribute *attr, char *buf)
  1769. {
  1770. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1771. const char *name;
  1772. name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
  1773. if (!name)
  1774. return -EINVAL;
  1775. return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
  1776. }
  1777. #define get_list_head_entry(pos, head, member) \
  1778. pos = list_entry((head)->next, typeof(*pos), member)
  1779. static ssize_t
  1780. store_fc_private_host_tgtid_bind_type(struct device *dev,
  1781. struct device_attribute *attr, const char *buf, size_t count)
  1782. {
  1783. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1784. struct fc_rport *rport;
  1785. enum fc_tgtid_binding_type val;
  1786. unsigned long flags;
  1787. if (get_fc_tgtid_bind_type_match(buf, &val))
  1788. return -EINVAL;
  1789. /* if changing bind type, purge all unused consistent bindings */
  1790. if (val != fc_host_tgtid_bind_type(shost)) {
  1791. spin_lock_irqsave(shost->host_lock, flags);
  1792. while (!list_empty(&fc_host_rport_bindings(shost))) {
  1793. get_list_head_entry(rport,
  1794. &fc_host_rport_bindings(shost), peers);
  1795. list_del(&rport->peers);
  1796. rport->port_state = FC_PORTSTATE_DELETED;
  1797. fc_queue_work(shost, &rport->rport_delete_work);
  1798. }
  1799. spin_unlock_irqrestore(shost->host_lock, flags);
  1800. }
  1801. fc_host_tgtid_bind_type(shost) = val;
  1802. return count;
  1803. }
  1804. static FC_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
  1805. show_fc_private_host_tgtid_bind_type,
  1806. store_fc_private_host_tgtid_bind_type);
  1807. static ssize_t
  1808. store_fc_private_host_issue_lip(struct device *dev,
  1809. struct device_attribute *attr, const char *buf, size_t count)
  1810. {
  1811. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1812. struct fc_internal *i = to_fc_internal(shost->transportt);
  1813. int ret;
  1814. /* ignore any data value written to the attribute */
  1815. if (i->f->issue_fc_host_lip) {
  1816. ret = i->f->issue_fc_host_lip(shost);
  1817. return ret ? ret: count;
  1818. }
  1819. return -ENOENT;
  1820. }
  1821. static FC_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
  1822. store_fc_private_host_issue_lip);
  1823. static ssize_t
  1824. store_fc_private_host_dev_loss_tmo(struct device *dev,
  1825. struct device_attribute *attr,
  1826. const char *buf, size_t count)
  1827. {
  1828. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1829. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  1830. struct fc_rport *rport;
  1831. unsigned long val, flags;
  1832. int rc;
  1833. rc = fc_str_to_dev_loss(buf, &val);
  1834. if (rc)
  1835. return rc;
  1836. fc_host_dev_loss_tmo(shost) = val;
  1837. spin_lock_irqsave(shost->host_lock, flags);
  1838. list_for_each_entry(rport, &fc_host->rports, peers)
  1839. fc_rport_set_dev_loss_tmo(rport, val);
  1840. spin_unlock_irqrestore(shost->host_lock, flags);
  1841. return count;
  1842. }
  1843. fc_private_host_show_function(dev_loss_tmo, "%d\n", 20, );
  1844. static FC_DEVICE_ATTR(host, dev_loss_tmo, S_IRUGO | S_IWUSR,
  1845. show_fc_host_dev_loss_tmo,
  1846. store_fc_private_host_dev_loss_tmo);
  1847. fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
  1848. /*
  1849. * Host Statistics Management
  1850. */
  1851. /* Show a given attribute in the statistics group */
  1852. static ssize_t
  1853. fc_stat_show(const struct device *dev, char *buf, unsigned long offset)
  1854. {
  1855. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1856. struct fc_internal *i = to_fc_internal(shost->transportt);
  1857. struct fc_host_statistics *stats;
  1858. ssize_t ret = -ENOENT;
  1859. if (offset > sizeof(struct fc_host_statistics) ||
  1860. offset % sizeof(u64) != 0)
  1861. WARN_ON(1);
  1862. if (i->f->get_fc_host_stats) {
  1863. stats = (i->f->get_fc_host_stats)(shost);
  1864. if (stats)
  1865. ret = snprintf(buf, 20, "0x%llx\n",
  1866. (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
  1867. }
  1868. return ret;
  1869. }
  1870. /* generate a read-only statistics attribute */
  1871. #define fc_host_statistic(name) \
  1872. static ssize_t show_fcstat_##name(struct device *cd, \
  1873. struct device_attribute *attr, \
  1874. char *buf) \
  1875. { \
  1876. return fc_stat_show(cd, buf, \
  1877. offsetof(struct fc_host_statistics, name)); \
  1878. } \
  1879. static FC_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
  1880. fc_host_statistic(seconds_since_last_reset);
  1881. fc_host_statistic(tx_frames);
  1882. fc_host_statistic(tx_words);
  1883. fc_host_statistic(rx_frames);
  1884. fc_host_statistic(rx_words);
  1885. fc_host_statistic(lip_count);
  1886. fc_host_statistic(nos_count);
  1887. fc_host_statistic(error_frames);
  1888. fc_host_statistic(dumped_frames);
  1889. fc_host_statistic(link_failure_count);
  1890. fc_host_statistic(loss_of_sync_count);
  1891. fc_host_statistic(loss_of_signal_count);
  1892. fc_host_statistic(prim_seq_protocol_err_count);
  1893. fc_host_statistic(invalid_tx_word_count);
  1894. fc_host_statistic(invalid_crc_count);
  1895. fc_host_statistic(fcp_input_requests);
  1896. fc_host_statistic(fcp_output_requests);
  1897. fc_host_statistic(fcp_control_requests);
  1898. fc_host_statistic(fcp_input_megabytes);
  1899. fc_host_statistic(fcp_output_megabytes);
  1900. fc_host_statistic(fcp_packet_alloc_failures);
  1901. fc_host_statistic(fcp_packet_aborts);
  1902. fc_host_statistic(fcp_frame_alloc_failures);
  1903. fc_host_statistic(fc_no_free_exch);
  1904. fc_host_statistic(fc_no_free_exch_xid);
  1905. fc_host_statistic(fc_xid_not_found);
  1906. fc_host_statistic(fc_xid_busy);
  1907. fc_host_statistic(fc_seq_not_found);
  1908. fc_host_statistic(fc_non_bls_resp);
  1909. fc_host_statistic(cn_sig_warn);
  1910. fc_host_statistic(cn_sig_alarm);
  1911. #define fc_host_fpin_statistic(name) \
  1912. static ssize_t fc_host_fpinstat_##name(struct device *cd, \
  1913. struct device_attribute *attr, \
  1914. char *buf) \
  1915. { \
  1916. struct Scsi_Host *shost = transport_class_to_shost(cd); \
  1917. struct fc_host_attrs *fc_host = shost_to_fc_host(shost); \
  1918. \
  1919. return snprintf(buf, 20, "0x%llx\n", fc_host->fpin_stats.name); \
  1920. } \
  1921. static FC_DEVICE_ATTR(host, fpin_##name, 0444, fc_host_fpinstat_##name, NULL)
  1922. fc_host_fpin_statistic(dn);
  1923. fc_host_fpin_statistic(dn_unknown);
  1924. fc_host_fpin_statistic(dn_timeout);
  1925. fc_host_fpin_statistic(dn_unable_to_route);
  1926. fc_host_fpin_statistic(dn_device_specific);
  1927. fc_host_fpin_statistic(cn);
  1928. fc_host_fpin_statistic(cn_clear);
  1929. fc_host_fpin_statistic(cn_lost_credit);
  1930. fc_host_fpin_statistic(cn_credit_stall);
  1931. fc_host_fpin_statistic(cn_oversubscription);
  1932. fc_host_fpin_statistic(cn_device_specific);
  1933. fc_host_fpin_statistic(li);
  1934. fc_host_fpin_statistic(li_failure_unknown);
  1935. fc_host_fpin_statistic(li_link_failure_count);
  1936. fc_host_fpin_statistic(li_loss_of_sync_count);
  1937. fc_host_fpin_statistic(li_loss_of_signals_count);
  1938. fc_host_fpin_statistic(li_prim_seq_err_count);
  1939. fc_host_fpin_statistic(li_invalid_tx_word_count);
  1940. fc_host_fpin_statistic(li_invalid_crc_count);
  1941. fc_host_fpin_statistic(li_device_specific);
  1942. static ssize_t
  1943. fc_reset_statistics(struct device *dev, struct device_attribute *attr,
  1944. const char *buf, size_t count)
  1945. {
  1946. struct Scsi_Host *shost = transport_class_to_shost(dev);
  1947. struct fc_internal *i = to_fc_internal(shost->transportt);
  1948. /* ignore any data value written to the attribute */
  1949. if (i->f->reset_fc_host_stats) {
  1950. i->f->reset_fc_host_stats(shost);
  1951. return count;
  1952. }
  1953. return -ENOENT;
  1954. }
  1955. static FC_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
  1956. fc_reset_statistics);
  1957. static struct attribute *fc_statistics_attrs[] = {
  1958. &device_attr_host_seconds_since_last_reset.attr,
  1959. &device_attr_host_tx_frames.attr,
  1960. &device_attr_host_tx_words.attr,
  1961. &device_attr_host_rx_frames.attr,
  1962. &device_attr_host_rx_words.attr,
  1963. &device_attr_host_lip_count.attr,
  1964. &device_attr_host_nos_count.attr,
  1965. &device_attr_host_error_frames.attr,
  1966. &device_attr_host_dumped_frames.attr,
  1967. &device_attr_host_link_failure_count.attr,
  1968. &device_attr_host_loss_of_sync_count.attr,
  1969. &device_attr_host_loss_of_signal_count.attr,
  1970. &device_attr_host_prim_seq_protocol_err_count.attr,
  1971. &device_attr_host_invalid_tx_word_count.attr,
  1972. &device_attr_host_invalid_crc_count.attr,
  1973. &device_attr_host_fcp_input_requests.attr,
  1974. &device_attr_host_fcp_output_requests.attr,
  1975. &device_attr_host_fcp_control_requests.attr,
  1976. &device_attr_host_fcp_input_megabytes.attr,
  1977. &device_attr_host_fcp_output_megabytes.attr,
  1978. &device_attr_host_fcp_packet_alloc_failures.attr,
  1979. &device_attr_host_fcp_packet_aborts.attr,
  1980. &device_attr_host_fcp_frame_alloc_failures.attr,
  1981. &device_attr_host_fc_no_free_exch.attr,
  1982. &device_attr_host_fc_no_free_exch_xid.attr,
  1983. &device_attr_host_fc_xid_not_found.attr,
  1984. &device_attr_host_fc_xid_busy.attr,
  1985. &device_attr_host_fc_seq_not_found.attr,
  1986. &device_attr_host_fc_non_bls_resp.attr,
  1987. &device_attr_host_cn_sig_warn.attr,
  1988. &device_attr_host_cn_sig_alarm.attr,
  1989. &device_attr_host_reset_statistics.attr,
  1990. &device_attr_host_fpin_dn.attr,
  1991. &device_attr_host_fpin_dn_unknown.attr,
  1992. &device_attr_host_fpin_dn_timeout.attr,
  1993. &device_attr_host_fpin_dn_unable_to_route.attr,
  1994. &device_attr_host_fpin_dn_device_specific.attr,
  1995. &device_attr_host_fpin_li.attr,
  1996. &device_attr_host_fpin_li_failure_unknown.attr,
  1997. &device_attr_host_fpin_li_link_failure_count.attr,
  1998. &device_attr_host_fpin_li_loss_of_sync_count.attr,
  1999. &device_attr_host_fpin_li_loss_of_signals_count.attr,
  2000. &device_attr_host_fpin_li_prim_seq_err_count.attr,
  2001. &device_attr_host_fpin_li_invalid_tx_word_count.attr,
  2002. &device_attr_host_fpin_li_invalid_crc_count.attr,
  2003. &device_attr_host_fpin_li_device_specific.attr,
  2004. &device_attr_host_fpin_cn.attr,
  2005. &device_attr_host_fpin_cn_clear.attr,
  2006. &device_attr_host_fpin_cn_lost_credit.attr,
  2007. &device_attr_host_fpin_cn_credit_stall.attr,
  2008. &device_attr_host_fpin_cn_oversubscription.attr,
  2009. &device_attr_host_fpin_cn_device_specific.attr,
  2010. NULL
  2011. };
  2012. static struct attribute_group fc_statistics_group = {
  2013. .name = "statistics",
  2014. .attrs = fc_statistics_attrs,
  2015. };
  2016. /* Host Vport Attributes */
  2017. static int
  2018. fc_parse_wwn(const char *ns, u64 *nm)
  2019. {
  2020. unsigned int i, j;
  2021. u8 wwn[8];
  2022. memset(wwn, 0, sizeof(wwn));
  2023. /* Validate and store the new name */
  2024. for (i=0, j=0; i < 16; i++) {
  2025. int value;
  2026. value = hex_to_bin(*ns++);
  2027. if (value >= 0)
  2028. j = (j << 4) | value;
  2029. else
  2030. return -EINVAL;
  2031. if (i % 2) {
  2032. wwn[i/2] = j & 0xff;
  2033. j = 0;
  2034. }
  2035. }
  2036. *nm = wwn_to_u64(wwn);
  2037. return 0;
  2038. }
  2039. /*
  2040. * "Short-cut" sysfs variable to create a new vport on a FC Host.
  2041. * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
  2042. * will default to a NPIV-based FCP_Initiator; The WWNs are specified
  2043. * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
  2044. */
  2045. static ssize_t
  2046. store_fc_host_vport_create(struct device *dev, struct device_attribute *attr,
  2047. const char *buf, size_t count)
  2048. {
  2049. struct Scsi_Host *shost = transport_class_to_shost(dev);
  2050. struct fc_vport_identifiers vid;
  2051. struct fc_vport *vport;
  2052. unsigned int cnt=count;
  2053. int stat;
  2054. memset(&vid, 0, sizeof(vid));
  2055. /* count may include a LF at end of string */
  2056. if (buf[cnt-1] == '\n')
  2057. cnt--;
  2058. /* validate we have enough characters for WWPN */
  2059. if ((cnt != (16+1+16)) || (buf[16] != ':'))
  2060. return -EINVAL;
  2061. stat = fc_parse_wwn(&buf[0], &vid.port_name);
  2062. if (stat)
  2063. return stat;
  2064. stat = fc_parse_wwn(&buf[17], &vid.node_name);
  2065. if (stat)
  2066. return stat;
  2067. vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
  2068. vid.vport_type = FC_PORTTYPE_NPIV;
  2069. /* vid.symbolic_name is already zero/NULL's */
  2070. vid.disable = false; /* always enabled */
  2071. /* we only allow support on Channel 0 !!! */
  2072. stat = fc_vport_setup(shost, 0, &shost->shost_gendev, &vid, &vport);
  2073. return stat ? stat : count;
  2074. }
  2075. static FC_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
  2076. store_fc_host_vport_create);
  2077. /*
  2078. * "Short-cut" sysfs variable to delete a vport on a FC Host.
  2079. * Vport is identified by a string containing "<WWPN>:<WWNN>".
  2080. * The WWNs are specified as hex characters, and may *not* contain
  2081. * any prefixes (e.g. 0x, x, etc)
  2082. */
  2083. static ssize_t
  2084. store_fc_host_vport_delete(struct device *dev, struct device_attribute *attr,
  2085. const char *buf, size_t count)
  2086. {
  2087. struct Scsi_Host *shost = transport_class_to_shost(dev);
  2088. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  2089. struct fc_vport *vport;
  2090. u64 wwpn, wwnn;
  2091. unsigned long flags;
  2092. unsigned int cnt=count;
  2093. int stat, match;
  2094. /* count may include a LF at end of string */
  2095. if (buf[cnt-1] == '\n')
  2096. cnt--;
  2097. /* validate we have enough characters for WWPN */
  2098. if ((cnt != (16+1+16)) || (buf[16] != ':'))
  2099. return -EINVAL;
  2100. stat = fc_parse_wwn(&buf[0], &wwpn);
  2101. if (stat)
  2102. return stat;
  2103. stat = fc_parse_wwn(&buf[17], &wwnn);
  2104. if (stat)
  2105. return stat;
  2106. spin_lock_irqsave(shost->host_lock, flags);
  2107. match = 0;
  2108. /* we only allow support on Channel 0 !!! */
  2109. list_for_each_entry(vport, &fc_host->vports, peers) {
  2110. if ((vport->channel == 0) &&
  2111. (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
  2112. if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
  2113. break;
  2114. vport->flags |= FC_VPORT_DELETING;
  2115. match = 1;
  2116. break;
  2117. }
  2118. }
  2119. spin_unlock_irqrestore(shost->host_lock, flags);
  2120. if (!match)
  2121. return -ENODEV;
  2122. stat = fc_vport_terminate(vport);
  2123. return stat ? stat : count;
  2124. }
  2125. static FC_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
  2126. store_fc_host_vport_delete);
  2127. static int fc_host_match(struct attribute_container *cont,
  2128. struct device *dev)
  2129. {
  2130. struct Scsi_Host *shost;
  2131. struct fc_internal *i;
  2132. if (!scsi_is_host_device(dev))
  2133. return 0;
  2134. shost = dev_to_shost(dev);
  2135. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  2136. != &fc_host_class.class)
  2137. return 0;
  2138. i = to_fc_internal(shost->transportt);
  2139. return &i->t.host_attrs.ac == cont;
  2140. }
  2141. static int fc_target_match(struct attribute_container *cont,
  2142. struct device *dev)
  2143. {
  2144. struct Scsi_Host *shost;
  2145. struct fc_internal *i;
  2146. if (!scsi_is_target_device(dev))
  2147. return 0;
  2148. shost = dev_to_shost(dev->parent);
  2149. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  2150. != &fc_host_class.class)
  2151. return 0;
  2152. i = to_fc_internal(shost->transportt);
  2153. return &i->t.target_attrs.ac == cont;
  2154. }
  2155. static void fc_rport_dev_release(struct device *dev)
  2156. {
  2157. struct fc_rport *rport = dev_to_rport(dev);
  2158. put_device(dev->parent);
  2159. kfree(rport);
  2160. }
  2161. int scsi_is_fc_rport(const struct device *dev)
  2162. {
  2163. return dev->release == fc_rport_dev_release;
  2164. }
  2165. EXPORT_SYMBOL(scsi_is_fc_rport);
  2166. static int fc_rport_match(struct attribute_container *cont,
  2167. struct device *dev)
  2168. {
  2169. struct Scsi_Host *shost;
  2170. struct fc_internal *i;
  2171. if (!scsi_is_fc_rport(dev))
  2172. return 0;
  2173. shost = dev_to_shost(dev->parent);
  2174. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  2175. != &fc_host_class.class)
  2176. return 0;
  2177. i = to_fc_internal(shost->transportt);
  2178. return &i->rport_attr_cont.ac == cont;
  2179. }
  2180. static void fc_vport_dev_release(struct device *dev)
  2181. {
  2182. struct fc_vport *vport = dev_to_vport(dev);
  2183. put_device(dev->parent); /* release kobj parent */
  2184. kfree(vport);
  2185. }
  2186. static int scsi_is_fc_vport(const struct device *dev)
  2187. {
  2188. return dev->release == fc_vport_dev_release;
  2189. }
  2190. static int fc_vport_match(struct attribute_container *cont,
  2191. struct device *dev)
  2192. {
  2193. struct fc_vport *vport;
  2194. struct Scsi_Host *shost;
  2195. struct fc_internal *i;
  2196. if (!scsi_is_fc_vport(dev))
  2197. return 0;
  2198. vport = dev_to_vport(dev);
  2199. shost = vport_to_shost(vport);
  2200. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  2201. != &fc_host_class.class)
  2202. return 0;
  2203. i = to_fc_internal(shost->transportt);
  2204. return &i->vport_attr_cont.ac == cont;
  2205. }
  2206. /**
  2207. * fc_eh_timed_out - FC Transport I/O timeout intercept handler
  2208. * @scmd: The SCSI command which timed out
  2209. *
  2210. * This routine protects against error handlers getting invoked while a
  2211. * rport is in a blocked state, typically due to a temporarily loss of
  2212. * connectivity. If the error handlers are allowed to proceed, requests
  2213. * to abort i/o, reset the target, etc will likely fail as there is no way
  2214. * to communicate with the device to perform the requested function. These
  2215. * failures may result in the midlayer taking the device offline, requiring
  2216. * manual intervention to restore operation.
  2217. *
  2218. * This routine, called whenever an i/o times out, validates the state of
  2219. * the underlying rport. If the rport is blocked, it returns
  2220. * EH_RESET_TIMER, which will continue to reschedule the timeout.
  2221. * Eventually, either the device will return, or devloss_tmo will fire,
  2222. * and when the timeout then fires, it will be handled normally.
  2223. * If the rport is not blocked, normal error handling continues.
  2224. *
  2225. * Notes:
  2226. * This routine assumes no locks are held on entry.
  2227. */
  2228. enum scsi_timeout_action fc_eh_timed_out(struct scsi_cmnd *scmd)
  2229. {
  2230. struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
  2231. if (rport->port_state == FC_PORTSTATE_BLOCKED)
  2232. return SCSI_EH_RESET_TIMER;
  2233. return SCSI_EH_NOT_HANDLED;
  2234. }
  2235. EXPORT_SYMBOL(fc_eh_timed_out);
  2236. /*
  2237. * Called by fc_user_scan to locate an rport on the shost that
  2238. * matches the channel and target id, and invoke scsi_scan_target()
  2239. * on the rport.
  2240. */
  2241. static void
  2242. fc_user_scan_tgt(struct Scsi_Host *shost, uint channel, uint id, u64 lun)
  2243. {
  2244. struct fc_rport *rport;
  2245. unsigned long flags;
  2246. spin_lock_irqsave(shost->host_lock, flags);
  2247. list_for_each_entry(rport, &fc_host_rports(shost), peers) {
  2248. if (rport->scsi_target_id == -1)
  2249. continue;
  2250. if ((rport->port_state != FC_PORTSTATE_ONLINE) &&
  2251. (rport->port_state != FC_PORTSTATE_MARGINAL))
  2252. continue;
  2253. if ((channel == rport->channel) &&
  2254. (id == rport->scsi_target_id)) {
  2255. spin_unlock_irqrestore(shost->host_lock, flags);
  2256. scsi_scan_target(&rport->dev, channel, id, lun,
  2257. SCSI_SCAN_MANUAL);
  2258. return;
  2259. }
  2260. }
  2261. spin_unlock_irqrestore(shost->host_lock, flags);
  2262. }
  2263. /*
  2264. * Called via sysfs scan routines. Necessary, as the FC transport
  2265. * wants to place all target objects below the rport object. So this
  2266. * routine must invoke the scsi_scan_target() routine with the rport
  2267. * object as the parent.
  2268. */
  2269. static int
  2270. fc_user_scan(struct Scsi_Host *shost, uint channel, uint id, u64 lun)
  2271. {
  2272. uint chlo, chhi;
  2273. uint tgtlo, tgthi;
  2274. if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
  2275. ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
  2276. ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
  2277. return -EINVAL;
  2278. if (channel == SCAN_WILD_CARD) {
  2279. chlo = 0;
  2280. chhi = shost->max_channel + 1;
  2281. } else {
  2282. chlo = channel;
  2283. chhi = channel + 1;
  2284. }
  2285. if (id == SCAN_WILD_CARD) {
  2286. tgtlo = 0;
  2287. tgthi = shost->max_id;
  2288. } else {
  2289. tgtlo = id;
  2290. tgthi = id + 1;
  2291. }
  2292. for ( ; chlo < chhi; chlo++)
  2293. for ( ; tgtlo < tgthi; tgtlo++)
  2294. fc_user_scan_tgt(shost, chlo, tgtlo, lun);
  2295. return 0;
  2296. }
  2297. struct scsi_transport_template *
  2298. fc_attach_transport(struct fc_function_template *ft)
  2299. {
  2300. int count;
  2301. struct fc_internal *i = kzalloc_obj(struct fc_internal);
  2302. if (unlikely(!i))
  2303. return NULL;
  2304. i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
  2305. i->t.target_attrs.ac.class = &fc_transport_class.class;
  2306. i->t.target_attrs.ac.match = fc_target_match;
  2307. i->t.target_size = sizeof(struct fc_starget_attrs);
  2308. transport_container_register(&i->t.target_attrs);
  2309. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  2310. i->t.host_attrs.ac.class = &fc_host_class.class;
  2311. i->t.host_attrs.ac.match = fc_host_match;
  2312. i->t.host_size = sizeof(struct fc_host_attrs);
  2313. if (ft->get_fc_host_stats)
  2314. i->t.host_attrs.statistics = &fc_statistics_group;
  2315. transport_container_register(&i->t.host_attrs);
  2316. i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
  2317. i->rport_attr_cont.ac.class = &fc_rport_class.class;
  2318. i->rport_attr_cont.ac.match = fc_rport_match;
  2319. if (ft->get_fc_rport_enc_info)
  2320. i->rport_attr_cont.encryption = &fc_rport_encryption_group;
  2321. i->rport_attr_cont.statistics = &fc_rport_statistics_group;
  2322. transport_container_register(&i->rport_attr_cont);
  2323. i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
  2324. i->vport_attr_cont.ac.class = &fc_vport_class.class;
  2325. i->vport_attr_cont.ac.match = fc_vport_match;
  2326. transport_container_register(&i->vport_attr_cont);
  2327. i->f = ft;
  2328. /* Transport uses the shost workq for scsi scanning */
  2329. i->t.create_work_queue = 1;
  2330. i->t.user_scan = fc_user_scan;
  2331. /*
  2332. * Setup SCSI Target Attributes.
  2333. */
  2334. count = 0;
  2335. SETUP_STARGET_ATTRIBUTE_RD(node_name);
  2336. SETUP_STARGET_ATTRIBUTE_RD(port_name);
  2337. SETUP_STARGET_ATTRIBUTE_RD(port_id);
  2338. BUG_ON(count > FC_STARGET_NUM_ATTRS);
  2339. i->starget_attrs[count] = NULL;
  2340. /*
  2341. * Setup SCSI Host Attributes.
  2342. */
  2343. count=0;
  2344. SETUP_HOST_ATTRIBUTE_RD(node_name);
  2345. SETUP_HOST_ATTRIBUTE_RD(port_name);
  2346. SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
  2347. SETUP_HOST_ATTRIBUTE_RD(supported_classes);
  2348. SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
  2349. SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
  2350. SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
  2351. if (ft->vport_create) {
  2352. SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
  2353. SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
  2354. }
  2355. SETUP_HOST_ATTRIBUTE_RD(serial_number);
  2356. SETUP_HOST_ATTRIBUTE_RD(manufacturer);
  2357. SETUP_HOST_ATTRIBUTE_RD(model);
  2358. SETUP_HOST_ATTRIBUTE_RD(model_description);
  2359. SETUP_HOST_ATTRIBUTE_RD(hardware_version);
  2360. SETUP_HOST_ATTRIBUTE_RD(driver_version);
  2361. SETUP_HOST_ATTRIBUTE_RD(firmware_version);
  2362. SETUP_HOST_ATTRIBUTE_RD(optionrom_version);
  2363. SETUP_HOST_ATTRIBUTE_RD(port_id);
  2364. SETUP_HOST_ATTRIBUTE_RD(port_type);
  2365. SETUP_HOST_ATTRIBUTE_RD(port_state);
  2366. SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
  2367. SETUP_HOST_ATTRIBUTE_RD(speed);
  2368. SETUP_HOST_ATTRIBUTE_RD(fabric_name);
  2369. SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
  2370. SETUP_HOST_ATTRIBUTE_RW(system_hostname);
  2371. /* Transport-managed attributes */
  2372. SETUP_PRIVATE_HOST_ATTRIBUTE_RW(dev_loss_tmo);
  2373. SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
  2374. if (ft->issue_fc_host_lip)
  2375. SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
  2376. if (ft->vport_create)
  2377. SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
  2378. if (ft->vport_delete)
  2379. SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
  2380. BUG_ON(count > FC_HOST_NUM_ATTRS);
  2381. i->host_attrs[count] = NULL;
  2382. /*
  2383. * Setup Remote Port Attributes.
  2384. */
  2385. count=0;
  2386. SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
  2387. SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
  2388. SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
  2389. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
  2390. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
  2391. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
  2392. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
  2393. SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(port_state);
  2394. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
  2395. SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
  2396. BUG_ON(count > FC_RPORT_NUM_ATTRS);
  2397. i->rport_attrs[count] = NULL;
  2398. /*
  2399. * Setup Virtual Port Attributes.
  2400. */
  2401. count=0;
  2402. SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
  2403. SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
  2404. SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
  2405. SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
  2406. SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
  2407. SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
  2408. SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
  2409. SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
  2410. SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
  2411. BUG_ON(count > FC_VPORT_NUM_ATTRS);
  2412. i->vport_attrs[count] = NULL;
  2413. return &i->t;
  2414. }
  2415. EXPORT_SYMBOL(fc_attach_transport);
  2416. void fc_release_transport(struct scsi_transport_template *t)
  2417. {
  2418. struct fc_internal *i = to_fc_internal(t);
  2419. transport_container_unregister(&i->t.target_attrs);
  2420. transport_container_unregister(&i->t.host_attrs);
  2421. transport_container_unregister(&i->rport_attr_cont);
  2422. transport_container_unregister(&i->vport_attr_cont);
  2423. kfree(i);
  2424. }
  2425. EXPORT_SYMBOL(fc_release_transport);
  2426. /**
  2427. * fc_queue_work - Queue work to the fc_host workqueue.
  2428. * @shost: Pointer to Scsi_Host bound to fc_host.
  2429. * @work: Work to queue for execution.
  2430. *
  2431. * Return value:
  2432. * 1 - work queued for execution
  2433. * 0 - work is already queued
  2434. * -EINVAL - work queue doesn't exist
  2435. */
  2436. static int
  2437. fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
  2438. {
  2439. if (unlikely(!fc_host_work_q(shost))) {
  2440. printk(KERN_ERR
  2441. "ERROR: FC host '%s' attempted to queue work, "
  2442. "when no workqueue created.\n", shost->hostt->name);
  2443. dump_stack();
  2444. return -EINVAL;
  2445. }
  2446. return queue_work(fc_host_work_q(shost), work);
  2447. }
  2448. /**
  2449. * fc_flush_work - Flush a fc_host's workqueue.
  2450. * @shost: Pointer to Scsi_Host bound to fc_host.
  2451. */
  2452. static void
  2453. fc_flush_work(struct Scsi_Host *shost)
  2454. {
  2455. if (!fc_host_work_q(shost)) {
  2456. printk(KERN_ERR
  2457. "ERROR: FC host '%s' attempted to flush work, "
  2458. "when no workqueue created.\n", shost->hostt->name);
  2459. dump_stack();
  2460. return;
  2461. }
  2462. flush_workqueue(fc_host_work_q(shost));
  2463. }
  2464. /**
  2465. * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
  2466. * @shost: Pointer to Scsi_Host bound to fc_host.
  2467. * @rport: rport associated with the devloss work
  2468. * @work: Work to queue for execution.
  2469. * @delay: jiffies to delay the work queuing
  2470. *
  2471. * Return value:
  2472. * 1 on success / 0 already queued / < 0 for error
  2473. */
  2474. static int
  2475. fc_queue_devloss_work(struct Scsi_Host *shost, struct fc_rport *rport,
  2476. struct delayed_work *work, unsigned long delay)
  2477. {
  2478. if (unlikely(!rport->devloss_work_q)) {
  2479. printk(KERN_ERR
  2480. "ERROR: FC host '%s' attempted to queue work, "
  2481. "when no workqueue created.\n", shost->hostt->name);
  2482. dump_stack();
  2483. return -EINVAL;
  2484. }
  2485. return queue_delayed_work(rport->devloss_work_q, work, delay);
  2486. }
  2487. /**
  2488. * fc_flush_devloss - Flush a fc_host's devloss workqueue.
  2489. * @shost: Pointer to Scsi_Host bound to fc_host.
  2490. * @rport: rport associated with the devloss work
  2491. */
  2492. static void
  2493. fc_flush_devloss(struct Scsi_Host *shost, struct fc_rport *rport)
  2494. {
  2495. if (unlikely(!rport->devloss_work_q)) {
  2496. printk(KERN_ERR
  2497. "ERROR: FC host '%s' attempted to flush work, "
  2498. "when no workqueue created.\n", shost->hostt->name);
  2499. dump_stack();
  2500. return;
  2501. }
  2502. flush_workqueue(rport->devloss_work_q);
  2503. }
  2504. /**
  2505. * fc_remove_host - called to terminate any fc_transport-related elements for a scsi host.
  2506. * @shost: Which &Scsi_Host
  2507. *
  2508. * This routine is expected to be called immediately preceding the
  2509. * a driver's call to scsi_remove_host().
  2510. *
  2511. * WARNING: A driver utilizing the fc_transport, which fails to call
  2512. * this routine prior to scsi_remove_host(), will leave dangling
  2513. * objects in /sys/class/fc_remote_ports. Access to any of these
  2514. * objects can result in a system crash !!!
  2515. *
  2516. * Notes:
  2517. * This routine assumes no locks are held on entry.
  2518. */
  2519. void
  2520. fc_remove_host(struct Scsi_Host *shost)
  2521. {
  2522. struct fc_vport *vport = NULL, *next_vport = NULL;
  2523. struct fc_rport *rport = NULL, *next_rport = NULL;
  2524. struct workqueue_struct *work_q;
  2525. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  2526. unsigned long flags;
  2527. spin_lock_irqsave(shost->host_lock, flags);
  2528. /* Remove any vports */
  2529. list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers) {
  2530. vport->flags |= FC_VPORT_DELETING;
  2531. fc_queue_work(shost, &vport->vport_delete_work);
  2532. }
  2533. /* Remove any remote ports */
  2534. list_for_each_entry_safe(rport, next_rport,
  2535. &fc_host->rports, peers) {
  2536. list_del(&rport->peers);
  2537. rport->port_state = FC_PORTSTATE_DELETED;
  2538. fc_queue_work(shost, &rport->rport_delete_work);
  2539. }
  2540. list_for_each_entry_safe(rport, next_rport,
  2541. &fc_host->rport_bindings, peers) {
  2542. list_del(&rport->peers);
  2543. rport->port_state = FC_PORTSTATE_DELETED;
  2544. fc_queue_work(shost, &rport->rport_delete_work);
  2545. }
  2546. spin_unlock_irqrestore(shost->host_lock, flags);
  2547. /* flush all scan work items */
  2548. scsi_flush_work(shost);
  2549. /* flush all stgt delete, and rport delete work items, then kill it */
  2550. if (fc_host->work_q) {
  2551. work_q = fc_host->work_q;
  2552. fc_host->work_q = NULL;
  2553. destroy_workqueue(work_q);
  2554. }
  2555. }
  2556. EXPORT_SYMBOL(fc_remove_host);
  2557. static void fc_terminate_rport_io(struct fc_rport *rport)
  2558. {
  2559. struct Scsi_Host *shost = rport_to_shost(rport);
  2560. struct fc_internal *i = to_fc_internal(shost->transportt);
  2561. /* Involve the LLDD if possible to terminate all io on the rport. */
  2562. if (i->f->terminate_rport_io)
  2563. i->f->terminate_rport_io(rport);
  2564. /*
  2565. * Must unblock to flush queued IO. scsi-ml will fail incoming reqs.
  2566. */
  2567. scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
  2568. }
  2569. /**
  2570. * fc_starget_delete - called to delete the scsi descendants of an rport
  2571. * @work: remote port to be operated on.
  2572. *
  2573. * Deletes target and all sdevs.
  2574. */
  2575. static void
  2576. fc_starget_delete(struct work_struct *work)
  2577. {
  2578. struct fc_rport *rport =
  2579. container_of(work, struct fc_rport, stgt_delete_work);
  2580. fc_terminate_rport_io(rport);
  2581. scsi_remove_target(&rport->dev);
  2582. }
  2583. /**
  2584. * fc_rport_final_delete - finish rport termination and delete it.
  2585. * @work: remote port to be deleted.
  2586. */
  2587. static void
  2588. fc_rport_final_delete(struct work_struct *work)
  2589. {
  2590. struct fc_rport *rport =
  2591. container_of(work, struct fc_rport, rport_delete_work);
  2592. struct device *dev = &rport->dev;
  2593. struct Scsi_Host *shost = rport_to_shost(rport);
  2594. struct fc_internal *i = to_fc_internal(shost->transportt);
  2595. struct workqueue_struct *work_q;
  2596. unsigned long flags;
  2597. int do_callback = 0;
  2598. fc_terminate_rport_io(rport);
  2599. /*
  2600. * if a scan is pending, flush the SCSI Host work_q so that
  2601. * that we can reclaim the rport scan work element.
  2602. */
  2603. if (rport->flags & FC_RPORT_SCAN_PENDING)
  2604. scsi_flush_work(shost);
  2605. /*
  2606. * Cancel any outstanding timers. These should really exist
  2607. * only when rmmod'ing the LLDD and we're asking for
  2608. * immediate termination of the rports
  2609. */
  2610. spin_lock_irqsave(shost->host_lock, flags);
  2611. if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
  2612. spin_unlock_irqrestore(shost->host_lock, flags);
  2613. if (!cancel_delayed_work(&rport->fail_io_work))
  2614. fc_flush_devloss(shost, rport);
  2615. if (!cancel_delayed_work(&rport->dev_loss_work))
  2616. fc_flush_devloss(shost, rport);
  2617. cancel_work_sync(&rport->scan_work);
  2618. spin_lock_irqsave(shost->host_lock, flags);
  2619. rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
  2620. }
  2621. spin_unlock_irqrestore(shost->host_lock, flags);
  2622. /* Delete SCSI target and sdevs */
  2623. if (rport->scsi_target_id != -1)
  2624. fc_starget_delete(&rport->stgt_delete_work);
  2625. /*
  2626. * Notify the driver that the rport is now dead. The LLDD will
  2627. * also guarantee that any communication to the rport is terminated
  2628. *
  2629. * Avoid this call if we already called it when we preserved the
  2630. * rport for the binding.
  2631. */
  2632. spin_lock_irqsave(shost->host_lock, flags);
  2633. if (!(rport->flags & FC_RPORT_DEVLOSS_CALLBK_DONE) &&
  2634. (i->f->dev_loss_tmo_callbk)) {
  2635. rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
  2636. do_callback = 1;
  2637. }
  2638. spin_unlock_irqrestore(shost->host_lock, flags);
  2639. if (do_callback)
  2640. i->f->dev_loss_tmo_callbk(rport);
  2641. fc_bsg_remove(rport->rqst_q);
  2642. if (rport->devloss_work_q) {
  2643. work_q = rport->devloss_work_q;
  2644. rport->devloss_work_q = NULL;
  2645. destroy_workqueue(work_q);
  2646. }
  2647. transport_remove_device(dev);
  2648. device_del(dev);
  2649. transport_destroy_device(dev);
  2650. scsi_host_put(shost); /* for fc_host->rport list */
  2651. put_device(dev); /* for self-reference */
  2652. }
  2653. /**
  2654. * fc_remote_port_create - allocates and creates a remote FC port.
  2655. * @shost: scsi host the remote port is connected to.
  2656. * @channel: Channel on shost port connected to.
  2657. * @ids: The world wide names, fc address, and FC4 port
  2658. * roles for the remote port.
  2659. *
  2660. * Allocates and creates the remoter port structure, including the
  2661. * class and sysfs creation.
  2662. *
  2663. * Notes:
  2664. * This routine assumes no locks are held on entry.
  2665. */
  2666. static struct fc_rport *
  2667. fc_remote_port_create(struct Scsi_Host *shost, int channel,
  2668. struct fc_rport_identifiers *ids)
  2669. {
  2670. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  2671. struct fc_internal *fci = to_fc_internal(shost->transportt);
  2672. struct fc_rport *rport;
  2673. struct device *dev;
  2674. unsigned long flags;
  2675. int error;
  2676. size_t size;
  2677. size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
  2678. rport = kzalloc(size, GFP_KERNEL);
  2679. if (unlikely(!rport)) {
  2680. printk(KERN_ERR "%s: allocation failure\n", __func__);
  2681. return NULL;
  2682. }
  2683. rport->maxframe_size = -1;
  2684. rport->supported_classes = FC_COS_UNSPECIFIED;
  2685. rport->dev_loss_tmo = fc_host->dev_loss_tmo;
  2686. memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
  2687. memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
  2688. rport->port_id = ids->port_id;
  2689. rport->roles = ids->roles;
  2690. rport->port_state = FC_PORTSTATE_ONLINE;
  2691. if (fci->f->dd_fcrport_size)
  2692. rport->dd_data = &rport[1];
  2693. rport->channel = channel;
  2694. rport->fast_io_fail_tmo = -1;
  2695. INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
  2696. INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
  2697. INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
  2698. INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
  2699. INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
  2700. spin_lock_irqsave(shost->host_lock, flags);
  2701. rport->number = fc_host->next_rport_number++;
  2702. if ((rport->roles & FC_PORT_ROLE_FCP_TARGET) ||
  2703. (rport->roles & FC_PORT_ROLE_FCP_DUMMY_INITIATOR))
  2704. rport->scsi_target_id = fc_host->next_target_id++;
  2705. else
  2706. rport->scsi_target_id = -1;
  2707. list_add_tail(&rport->peers, &fc_host->rports);
  2708. scsi_host_get(shost); /* for fc_host->rport list */
  2709. spin_unlock_irqrestore(shost->host_lock, flags);
  2710. rport->devloss_work_q = alloc_workqueue("fc_dl_%d_%d", WQ_PERCPU, 0,
  2711. shost->host_no, rport->number);
  2712. if (!rport->devloss_work_q) {
  2713. printk(KERN_ERR "FC Remote Port alloc_workqueue failed\n");
  2714. /*
  2715. * Note that we have not yet called device_initialize() / get_device()
  2716. * Cannot reclaim incremented rport->number because we released host_lock
  2717. */
  2718. spin_lock_irqsave(shost->host_lock, flags);
  2719. list_del(&rport->peers);
  2720. scsi_host_put(shost); /* for fc_host->rport list */
  2721. spin_unlock_irqrestore(shost->host_lock, flags);
  2722. kfree(rport);
  2723. return NULL;
  2724. }
  2725. dev = &rport->dev;
  2726. device_initialize(dev); /* takes self reference */
  2727. dev->parent = get_device(&shost->shost_gendev); /* parent reference */
  2728. dev->release = fc_rport_dev_release;
  2729. dev_set_name(dev, "rport-%d:%d-%d",
  2730. shost->host_no, channel, rport->number);
  2731. transport_setup_device(dev);
  2732. error = device_add(dev);
  2733. if (error) {
  2734. printk(KERN_ERR "FC Remote Port device_add failed\n");
  2735. goto delete_rport;
  2736. }
  2737. transport_add_device(dev);
  2738. transport_configure_device(dev);
  2739. fc_bsg_rportadd(shost, rport);
  2740. /* ignore any bsg add error - we just can't do sgio */
  2741. if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
  2742. /* initiate a scan of the target */
  2743. rport->flags |= FC_RPORT_SCAN_PENDING;
  2744. scsi_queue_work(shost, &rport->scan_work);
  2745. }
  2746. return rport;
  2747. delete_rport:
  2748. transport_destroy_device(dev);
  2749. spin_lock_irqsave(shost->host_lock, flags);
  2750. list_del(&rport->peers);
  2751. scsi_host_put(shost); /* for fc_host->rport list */
  2752. spin_unlock_irqrestore(shost->host_lock, flags);
  2753. put_device(dev->parent);
  2754. kfree(rport);
  2755. return NULL;
  2756. }
  2757. /**
  2758. * fc_remote_port_add - notify fc transport of the existence of a remote FC port.
  2759. * @shost: scsi host the remote port is connected to.
  2760. * @channel: Channel on shost port connected to.
  2761. * @ids: The world wide names, fc address, and FC4 port
  2762. * roles for the remote port.
  2763. *
  2764. * The LLDD calls this routine to notify the transport of the existence
  2765. * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
  2766. * of the port, it's FC address (port_id), and the FC4 roles that are
  2767. * active for the port.
  2768. *
  2769. * For ports that are FCP targets (aka scsi targets), the FC transport
  2770. * maintains consistent target id bindings on behalf of the LLDD.
  2771. * A consistent target id binding is an assignment of a target id to
  2772. * a remote port identifier, which persists while the scsi host is
  2773. * attached. The remote port can disappear, then later reappear, and
  2774. * it's target id assignment remains the same. This allows for shifts
  2775. * in FC addressing (if binding by wwpn or wwnn) with no apparent
  2776. * changes to the scsi subsystem which is based on scsi host number and
  2777. * target id values. Bindings are only valid during the attachment of
  2778. * the scsi host. If the host detaches, then later re-attaches, target
  2779. * id bindings may change.
  2780. *
  2781. * This routine is responsible for returning a remote port structure.
  2782. * The routine will search the list of remote ports it maintains
  2783. * internally on behalf of consistent target id mappings. If found, the
  2784. * remote port structure will be reused. Otherwise, a new remote port
  2785. * structure will be allocated.
  2786. *
  2787. * Whenever a remote port is allocated, a new fc_remote_port class
  2788. * device is created.
  2789. *
  2790. * Should not be called from interrupt context.
  2791. *
  2792. * Notes:
  2793. * This routine assumes no locks are held on entry.
  2794. */
  2795. struct fc_rport *
  2796. fc_remote_port_add(struct Scsi_Host *shost, int channel,
  2797. struct fc_rport_identifiers *ids)
  2798. {
  2799. struct fc_internal *fci = to_fc_internal(shost->transportt);
  2800. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  2801. struct fc_rport *rport;
  2802. unsigned long flags;
  2803. int match = 0;
  2804. /* ensure any stgt delete functions are done */
  2805. fc_flush_work(shost);
  2806. /*
  2807. * Search the list of "active" rports, for an rport that has been
  2808. * deleted, but we've held off the real delete while the target
  2809. * is in a "blocked" state.
  2810. */
  2811. spin_lock_irqsave(shost->host_lock, flags);
  2812. list_for_each_entry(rport, &fc_host->rports, peers) {
  2813. if ((rport->port_state == FC_PORTSTATE_BLOCKED ||
  2814. rport->port_state == FC_PORTSTATE_NOTPRESENT) &&
  2815. (rport->channel == channel)) {
  2816. switch (fc_host->tgtid_bind_type) {
  2817. case FC_TGTID_BIND_BY_WWPN:
  2818. case FC_TGTID_BIND_NONE:
  2819. if (rport->port_name == ids->port_name)
  2820. match = 1;
  2821. break;
  2822. case FC_TGTID_BIND_BY_WWNN:
  2823. if (rport->node_name == ids->node_name)
  2824. match = 1;
  2825. break;
  2826. case FC_TGTID_BIND_BY_ID:
  2827. if (rport->port_id == ids->port_id)
  2828. match = 1;
  2829. break;
  2830. }
  2831. if (match) {
  2832. memcpy(&rport->node_name, &ids->node_name,
  2833. sizeof(rport->node_name));
  2834. memcpy(&rport->port_name, &ids->port_name,
  2835. sizeof(rport->port_name));
  2836. rport->port_id = ids->port_id;
  2837. rport->port_state = FC_PORTSTATE_ONLINE;
  2838. rport->roles = ids->roles;
  2839. spin_unlock_irqrestore(shost->host_lock, flags);
  2840. if (fci->f->dd_fcrport_size)
  2841. memset(rport->dd_data, 0,
  2842. fci->f->dd_fcrport_size);
  2843. /*
  2844. * If we were not a target, cancel the
  2845. * io terminate and rport timers, and
  2846. * we're done.
  2847. *
  2848. * If we were a target, but our new role
  2849. * doesn't indicate a target, leave the
  2850. * timers running expecting the role to
  2851. * change as the target fully logs in. If
  2852. * it doesn't, the target will be torn down.
  2853. *
  2854. * If we were a target, and our role shows
  2855. * we're still a target, cancel the timers
  2856. * and kick off a scan.
  2857. */
  2858. /* was a target, not in roles */
  2859. if ((rport->scsi_target_id != -1) &&
  2860. (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
  2861. return rport;
  2862. /*
  2863. * Stop the fail io and dev_loss timers.
  2864. * If they flush, the port_state will
  2865. * be checked and will NOOP the function.
  2866. */
  2867. if (!cancel_delayed_work(&rport->fail_io_work))
  2868. fc_flush_devloss(shost, rport);
  2869. if (!cancel_delayed_work(&rport->dev_loss_work))
  2870. fc_flush_devloss(shost, rport);
  2871. spin_lock_irqsave(shost->host_lock, flags);
  2872. rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
  2873. FC_RPORT_DEVLOSS_PENDING |
  2874. FC_RPORT_DEVLOSS_CALLBK_DONE);
  2875. spin_unlock_irqrestore(shost->host_lock, flags);
  2876. /* if target, initiate a scan */
  2877. if (rport->scsi_target_id != -1) {
  2878. scsi_target_unblock(&rport->dev,
  2879. SDEV_RUNNING);
  2880. spin_lock_irqsave(shost->host_lock,
  2881. flags);
  2882. rport->flags |= FC_RPORT_SCAN_PENDING;
  2883. scsi_queue_work(shost,
  2884. &rport->scan_work);
  2885. spin_unlock_irqrestore(shost->host_lock,
  2886. flags);
  2887. }
  2888. fc_bsg_goose_queue(rport);
  2889. return rport;
  2890. }
  2891. }
  2892. }
  2893. /*
  2894. * Search the bindings array
  2895. * Note: if never a FCP target, you won't be on this list
  2896. */
  2897. if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
  2898. /* search for a matching consistent binding */
  2899. list_for_each_entry(rport, &fc_host->rport_bindings,
  2900. peers) {
  2901. if (rport->channel != channel)
  2902. continue;
  2903. switch (fc_host->tgtid_bind_type) {
  2904. case FC_TGTID_BIND_BY_WWPN:
  2905. if (rport->port_name == ids->port_name)
  2906. match = 1;
  2907. break;
  2908. case FC_TGTID_BIND_BY_WWNN:
  2909. if (rport->node_name == ids->node_name)
  2910. match = 1;
  2911. break;
  2912. case FC_TGTID_BIND_BY_ID:
  2913. if (rport->port_id == ids->port_id)
  2914. match = 1;
  2915. break;
  2916. case FC_TGTID_BIND_NONE: /* to keep compiler happy */
  2917. break;
  2918. }
  2919. if (match) {
  2920. list_move_tail(&rport->peers, &fc_host->rports);
  2921. break;
  2922. }
  2923. }
  2924. if (match) {
  2925. memcpy(&rport->node_name, &ids->node_name,
  2926. sizeof(rport->node_name));
  2927. memcpy(&rport->port_name, &ids->port_name,
  2928. sizeof(rport->port_name));
  2929. rport->port_id = ids->port_id;
  2930. rport->port_state = FC_PORTSTATE_ONLINE;
  2931. rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
  2932. if (fci->f->dd_fcrport_size)
  2933. memset(rport->dd_data, 0,
  2934. fci->f->dd_fcrport_size);
  2935. spin_unlock_irqrestore(shost->host_lock, flags);
  2936. fc_remote_port_rolechg(rport, ids->roles);
  2937. return rport;
  2938. }
  2939. }
  2940. spin_unlock_irqrestore(shost->host_lock, flags);
  2941. /* No consistent binding found - create new remote port entry */
  2942. rport = fc_remote_port_create(shost, channel, ids);
  2943. return rport;
  2944. }
  2945. EXPORT_SYMBOL(fc_remote_port_add);
  2946. /**
  2947. * fc_remote_port_delete - notifies the fc transport that a remote port is no longer in existence.
  2948. * @rport: The remote port that no longer exists
  2949. *
  2950. * The LLDD calls this routine to notify the transport that a remote
  2951. * port is no longer part of the topology. Note: Although a port
  2952. * may no longer be part of the topology, it may persist in the remote
  2953. * ports displayed by the fc_host. We do this under 2 conditions:
  2954. *
  2955. * 1) If the port was a scsi target, we delay its deletion by "blocking" it.
  2956. * This allows the port to temporarily disappear, then reappear without
  2957. * disrupting the SCSI device tree attached to it. During the "blocked"
  2958. * period the port will still exist.
  2959. *
  2960. * 2) If the port was a scsi target and disappears for longer than we
  2961. * expect, we'll delete the port and the tear down the SCSI device tree
  2962. * attached to it. However, we want to semi-persist the target id assigned
  2963. * to that port if it eventually does exist. The port structure will
  2964. * remain (although with minimal information) so that the target id
  2965. * bindings also remain.
  2966. *
  2967. * If the remote port is not an FCP Target, it will be fully torn down
  2968. * and deallocated, including the fc_remote_port class device.
  2969. *
  2970. * If the remote port is an FCP Target, the port will be placed in a
  2971. * temporary blocked state. From the LLDD's perspective, the rport no
  2972. * longer exists. From the SCSI midlayer's perspective, the SCSI target
  2973. * exists, but all sdevs on it are blocked from further I/O. The following
  2974. * is then expected.
  2975. *
  2976. * If the remote port does not return (signaled by a LLDD call to
  2977. * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
  2978. * scsi target is removed - killing all outstanding i/o and removing the
  2979. * scsi devices attached to it. The port structure will be marked Not
  2980. * Present and be partially cleared, leaving only enough information to
  2981. * recognize the remote port relative to the scsi target id binding if
  2982. * it later appears. The port will remain as long as there is a valid
  2983. * binding (e.g. until the user changes the binding type or unloads the
  2984. * scsi host with the binding).
  2985. *
  2986. * If the remote port returns within the dev_loss_tmo value (and matches
  2987. * according to the target id binding type), the port structure will be
  2988. * reused. If it is no longer a SCSI target, the target will be torn
  2989. * down. If it continues to be a SCSI target, then the target will be
  2990. * unblocked (allowing i/o to be resumed), and a scan will be activated
  2991. * to ensure that all luns are detected.
  2992. *
  2993. * Called from normal process context only - cannot be called from interrupt.
  2994. *
  2995. * Notes:
  2996. * This routine assumes no locks are held on entry.
  2997. */
  2998. void
  2999. fc_remote_port_delete(struct fc_rport *rport)
  3000. {
  3001. struct Scsi_Host *shost = rport_to_shost(rport);
  3002. unsigned long timeout = rport->dev_loss_tmo;
  3003. unsigned long flags;
  3004. /*
  3005. * No need to flush the fc_host work_q's, as all adds are synchronous.
  3006. *
  3007. * We do need to reclaim the rport scan work element, so eventually
  3008. * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
  3009. * there's still a scan pending.
  3010. */
  3011. spin_lock_irqsave(shost->host_lock, flags);
  3012. if ((rport->port_state != FC_PORTSTATE_ONLINE) &&
  3013. (rport->port_state != FC_PORTSTATE_MARGINAL)) {
  3014. spin_unlock_irqrestore(shost->host_lock, flags);
  3015. return;
  3016. }
  3017. /*
  3018. * In the past, we if this was not an FCP-Target, we would
  3019. * unconditionally just jump to deleting the rport.
  3020. * However, rports can be used as node containers by the LLDD,
  3021. * and its not appropriate to just terminate the rport at the
  3022. * first sign of a loss in connectivity. The LLDD may want to
  3023. * send ELS traffic to re-validate the login. If the rport is
  3024. * immediately deleted, it makes it inappropriate for a node
  3025. * container.
  3026. * So... we now unconditionally wait dev_loss_tmo before
  3027. * destroying an rport.
  3028. */
  3029. rport->port_state = FC_PORTSTATE_BLOCKED;
  3030. rport->flags |= FC_RPORT_DEVLOSS_PENDING;
  3031. spin_unlock_irqrestore(shost->host_lock, flags);
  3032. scsi_block_targets(shost, &rport->dev);
  3033. /* see if we need to kill io faster than waiting for device loss */
  3034. if ((rport->fast_io_fail_tmo != -1) &&
  3035. (rport->fast_io_fail_tmo < timeout))
  3036. fc_queue_devloss_work(shost, rport, &rport->fail_io_work,
  3037. rport->fast_io_fail_tmo * HZ);
  3038. /* cap the length the devices can be blocked until they are deleted */
  3039. fc_queue_devloss_work(shost, rport, &rport->dev_loss_work,
  3040. timeout * HZ);
  3041. }
  3042. EXPORT_SYMBOL(fc_remote_port_delete);
  3043. /**
  3044. * fc_remote_port_rolechg - notifies the fc transport that the roles on a remote may have changed.
  3045. * @rport: The remote port that changed.
  3046. * @roles: New roles for this port.
  3047. *
  3048. * Description: The LLDD calls this routine to notify the transport that the
  3049. * roles on a remote port may have changed. The largest effect of this is
  3050. * if a port now becomes a FCP Target, it must be allocated a
  3051. * scsi target id. If the port is no longer a FCP target, any
  3052. * scsi target id value assigned to it will persist in case the
  3053. * role changes back to include FCP Target. No changes in the scsi
  3054. * midlayer will be invoked if the role changes (in the expectation
  3055. * that the role will be resumed. If it doesn't normal error processing
  3056. * will take place).
  3057. *
  3058. * Should not be called from interrupt context.
  3059. *
  3060. * Notes:
  3061. * This routine assumes no locks are held on entry.
  3062. */
  3063. void
  3064. fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
  3065. {
  3066. struct Scsi_Host *shost = rport_to_shost(rport);
  3067. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  3068. unsigned long flags;
  3069. int create = 0;
  3070. spin_lock_irqsave(shost->host_lock, flags);
  3071. if (roles & FC_PORT_ROLE_FCP_TARGET) {
  3072. if (rport->scsi_target_id == -1) {
  3073. rport->scsi_target_id = fc_host->next_target_id++;
  3074. create = 1;
  3075. } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
  3076. create = 1;
  3077. }
  3078. rport->roles = roles;
  3079. spin_unlock_irqrestore(shost->host_lock, flags);
  3080. if (create) {
  3081. /*
  3082. * There may have been a delete timer running on the
  3083. * port. Ensure that it is cancelled as we now know
  3084. * the port is an FCP Target.
  3085. * Note: we know the rport exists and is in an online
  3086. * state as the LLDD would not have had an rport
  3087. * reference to pass us.
  3088. *
  3089. * Take no action on the timer_delete() failure as the state
  3090. * machine state change will validate the
  3091. * transaction.
  3092. */
  3093. if (!cancel_delayed_work(&rport->fail_io_work))
  3094. fc_flush_devloss(shost, rport);
  3095. if (!cancel_delayed_work(&rport->dev_loss_work))
  3096. fc_flush_devloss(shost, rport);
  3097. spin_lock_irqsave(shost->host_lock, flags);
  3098. rport->flags &= ~(FC_RPORT_FAST_FAIL_TIMEDOUT |
  3099. FC_RPORT_DEVLOSS_PENDING |
  3100. FC_RPORT_DEVLOSS_CALLBK_DONE);
  3101. spin_unlock_irqrestore(shost->host_lock, flags);
  3102. /* ensure any stgt delete functions are done */
  3103. fc_flush_work(shost);
  3104. scsi_target_unblock(&rport->dev, SDEV_RUNNING);
  3105. /* initiate a scan of the target */
  3106. spin_lock_irqsave(shost->host_lock, flags);
  3107. rport->flags |= FC_RPORT_SCAN_PENDING;
  3108. scsi_queue_work(shost, &rport->scan_work);
  3109. spin_unlock_irqrestore(shost->host_lock, flags);
  3110. }
  3111. }
  3112. EXPORT_SYMBOL(fc_remote_port_rolechg);
  3113. /**
  3114. * fc_timeout_deleted_rport - Timeout handler for a deleted remote port.
  3115. * @work: rport target that failed to reappear in the allotted time.
  3116. *
  3117. * Description: An attempt to delete a remote port blocks, and if it fails
  3118. * to return in the allotted time this gets called.
  3119. */
  3120. static void
  3121. fc_timeout_deleted_rport(struct work_struct *work)
  3122. {
  3123. struct fc_rport *rport =
  3124. container_of(work, struct fc_rport, dev_loss_work.work);
  3125. struct Scsi_Host *shost = rport_to_shost(rport);
  3126. struct fc_internal *i = to_fc_internal(shost->transportt);
  3127. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  3128. unsigned long flags;
  3129. int do_callback = 0;
  3130. spin_lock_irqsave(shost->host_lock, flags);
  3131. rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
  3132. /*
  3133. * If the port is ONLINE, then it came back. If it was a SCSI
  3134. * target, validate it still is. If not, tear down the
  3135. * scsi_target on it.
  3136. */
  3137. if (((rport->port_state == FC_PORTSTATE_ONLINE) ||
  3138. (rport->port_state == FC_PORTSTATE_MARGINAL)) &&
  3139. (rport->scsi_target_id != -1) &&
  3140. !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
  3141. dev_printk(KERN_ERR, &rport->dev,
  3142. "blocked FC remote port time out: no longer"
  3143. " a FCP target, removing starget\n");
  3144. spin_unlock_irqrestore(shost->host_lock, flags);
  3145. scsi_target_unblock(&rport->dev, SDEV_TRANSPORT_OFFLINE);
  3146. fc_queue_work(shost, &rport->stgt_delete_work);
  3147. return;
  3148. }
  3149. /* NOOP state - we're flushing workq's */
  3150. if (rport->port_state != FC_PORTSTATE_BLOCKED) {
  3151. spin_unlock_irqrestore(shost->host_lock, flags);
  3152. dev_printk(KERN_ERR, &rport->dev,
  3153. "blocked FC remote port time out: leaving"
  3154. " rport%s alone\n",
  3155. (rport->scsi_target_id != -1) ? " and starget" : "");
  3156. return;
  3157. }
  3158. if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
  3159. (rport->scsi_target_id == -1)) {
  3160. list_del(&rport->peers);
  3161. rport->port_state = FC_PORTSTATE_DELETED;
  3162. dev_printk(KERN_ERR, &rport->dev,
  3163. "blocked FC remote port time out: removing"
  3164. " rport%s\n",
  3165. (rport->scsi_target_id != -1) ? " and starget" : "");
  3166. fc_queue_work(shost, &rport->rport_delete_work);
  3167. spin_unlock_irqrestore(shost->host_lock, flags);
  3168. return;
  3169. }
  3170. dev_printk(KERN_ERR, &rport->dev,
  3171. "blocked FC remote port time out: removing target and "
  3172. "saving binding\n");
  3173. list_move_tail(&rport->peers, &fc_host->rport_bindings);
  3174. /*
  3175. * Note: We do not remove or clear the hostdata area. This allows
  3176. * host-specific target data to persist along with the
  3177. * scsi_target_id. It's up to the host to manage it's hostdata area.
  3178. */
  3179. /*
  3180. * Reinitialize port attributes that may change if the port comes back.
  3181. */
  3182. rport->maxframe_size = -1;
  3183. rport->supported_classes = FC_COS_UNSPECIFIED;
  3184. rport->roles = FC_PORT_ROLE_UNKNOWN;
  3185. rport->port_state = FC_PORTSTATE_NOTPRESENT;
  3186. rport->flags &= ~FC_RPORT_FAST_FAIL_TIMEDOUT;
  3187. /*
  3188. * Pre-emptively kill I/O rather than waiting for the work queue
  3189. * item to teardown the starget. (FCOE libFC folks prefer this
  3190. * and to have the rport_port_id still set when it's done).
  3191. */
  3192. spin_unlock_irqrestore(shost->host_lock, flags);
  3193. fc_terminate_rport_io(rport);
  3194. spin_lock_irqsave(shost->host_lock, flags);
  3195. if (rport->port_state == FC_PORTSTATE_NOTPRESENT) { /* still missing */
  3196. /* remove the identifiers that aren't used in the consisting binding */
  3197. switch (fc_host->tgtid_bind_type) {
  3198. case FC_TGTID_BIND_BY_WWPN:
  3199. rport->node_name = -1;
  3200. rport->port_id = -1;
  3201. break;
  3202. case FC_TGTID_BIND_BY_WWNN:
  3203. rport->port_name = -1;
  3204. rport->port_id = -1;
  3205. break;
  3206. case FC_TGTID_BIND_BY_ID:
  3207. rport->node_name = -1;
  3208. rport->port_name = -1;
  3209. break;
  3210. case FC_TGTID_BIND_NONE: /* to keep compiler happy */
  3211. break;
  3212. }
  3213. /*
  3214. * As this only occurs if the remote port (scsi target)
  3215. * went away and didn't come back - we'll remove
  3216. * all attached scsi devices.
  3217. */
  3218. rport->flags |= FC_RPORT_DEVLOSS_CALLBK_DONE;
  3219. fc_queue_work(shost, &rport->stgt_delete_work);
  3220. do_callback = 1;
  3221. }
  3222. spin_unlock_irqrestore(shost->host_lock, flags);
  3223. /*
  3224. * Notify the driver that the rport is now dead. The LLDD will
  3225. * also guarantee that any communication to the rport is terminated
  3226. *
  3227. * Note: we set the CALLBK_DONE flag above to correspond
  3228. */
  3229. if (do_callback && i->f->dev_loss_tmo_callbk)
  3230. i->f->dev_loss_tmo_callbk(rport);
  3231. }
  3232. /**
  3233. * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a disconnected SCSI target.
  3234. * @work: rport to terminate io on.
  3235. *
  3236. * Notes: Only requests the failure of the io, not that all are flushed
  3237. * prior to returning.
  3238. */
  3239. static void
  3240. fc_timeout_fail_rport_io(struct work_struct *work)
  3241. {
  3242. struct fc_rport *rport =
  3243. container_of(work, struct fc_rport, fail_io_work.work);
  3244. if (rport->port_state != FC_PORTSTATE_BLOCKED)
  3245. return;
  3246. rport->flags |= FC_RPORT_FAST_FAIL_TIMEDOUT;
  3247. fc_terminate_rport_io(rport);
  3248. }
  3249. /**
  3250. * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
  3251. * @work: remote port to be scanned.
  3252. */
  3253. static void
  3254. fc_scsi_scan_rport(struct work_struct *work)
  3255. {
  3256. struct fc_rport *rport =
  3257. container_of(work, struct fc_rport, scan_work);
  3258. struct Scsi_Host *shost = rport_to_shost(rport);
  3259. struct fc_internal *i = to_fc_internal(shost->transportt);
  3260. unsigned long flags;
  3261. if (((rport->port_state == FC_PORTSTATE_ONLINE) ||
  3262. (rport->port_state == FC_PORTSTATE_MARGINAL)) &&
  3263. (rport->roles & FC_PORT_ROLE_FCP_TARGET) &&
  3264. !(i->f->disable_target_scan)) {
  3265. scsi_scan_target(&rport->dev, rport->channel,
  3266. rport->scsi_target_id, SCAN_WILD_CARD,
  3267. SCSI_SCAN_RESCAN);
  3268. }
  3269. spin_lock_irqsave(shost->host_lock, flags);
  3270. rport->flags &= ~FC_RPORT_SCAN_PENDING;
  3271. spin_unlock_irqrestore(shost->host_lock, flags);
  3272. }
  3273. /**
  3274. * fc_block_rport() - Block SCSI eh thread for blocked fc_rport.
  3275. * @rport: Remote port that scsi_eh is trying to recover.
  3276. *
  3277. * This routine can be called from a FC LLD scsi_eh callback. It
  3278. * blocks the scsi_eh thread until the fc_rport leaves the
  3279. * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
  3280. * necessary to avoid the scsi_eh failing recovery actions for blocked
  3281. * rports which would lead to offlined SCSI devices.
  3282. *
  3283. * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
  3284. * FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
  3285. * passed back to scsi_eh.
  3286. */
  3287. int fc_block_rport(struct fc_rport *rport)
  3288. {
  3289. struct Scsi_Host *shost = rport_to_shost(rport);
  3290. unsigned long flags;
  3291. spin_lock_irqsave(shost->host_lock, flags);
  3292. while (rport->port_state == FC_PORTSTATE_BLOCKED &&
  3293. !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)) {
  3294. spin_unlock_irqrestore(shost->host_lock, flags);
  3295. msleep(1000);
  3296. spin_lock_irqsave(shost->host_lock, flags);
  3297. }
  3298. spin_unlock_irqrestore(shost->host_lock, flags);
  3299. if (rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT)
  3300. return FAST_IO_FAIL;
  3301. return 0;
  3302. }
  3303. EXPORT_SYMBOL(fc_block_rport);
  3304. /**
  3305. * fc_block_scsi_eh - Block SCSI eh thread for blocked fc_rport
  3306. * @cmnd: SCSI command that scsi_eh is trying to recover
  3307. *
  3308. * This routine can be called from a FC LLD scsi_eh callback. It
  3309. * blocks the scsi_eh thread until the fc_rport leaves the
  3310. * FC_PORTSTATE_BLOCKED, or the fast_io_fail_tmo fires. This is
  3311. * necessary to avoid the scsi_eh failing recovery actions for blocked
  3312. * rports which would lead to offlined SCSI devices.
  3313. *
  3314. * Returns: 0 if the fc_rport left the state FC_PORTSTATE_BLOCKED.
  3315. * FAST_IO_FAIL if the fast_io_fail_tmo fired, this should be
  3316. * passed back to scsi_eh.
  3317. */
  3318. int fc_block_scsi_eh(struct scsi_cmnd *cmnd)
  3319. {
  3320. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  3321. if (WARN_ON_ONCE(!rport))
  3322. return FAST_IO_FAIL;
  3323. return fc_block_rport(rport);
  3324. }
  3325. EXPORT_SYMBOL(fc_block_scsi_eh);
  3326. /*
  3327. * fc_eh_should_retry_cmd - Checks if the cmd should be retried or not
  3328. * @scmd: The SCSI command to be checked
  3329. *
  3330. * This checks the rport state to decide if a cmd is
  3331. * retryable.
  3332. *
  3333. * Returns: true if the rport state is not in marginal state.
  3334. */
  3335. bool fc_eh_should_retry_cmd(struct scsi_cmnd *scmd)
  3336. {
  3337. struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
  3338. if ((rport->port_state != FC_PORTSTATE_ONLINE) &&
  3339. (scsi_cmd_to_rq(scmd)->cmd_flags & REQ_FAILFAST_TRANSPORT)) {
  3340. set_host_byte(scmd, DID_TRANSPORT_MARGINAL);
  3341. return false;
  3342. }
  3343. return true;
  3344. }
  3345. EXPORT_SYMBOL_GPL(fc_eh_should_retry_cmd);
  3346. /**
  3347. * fc_vport_setup - allocates and creates a FC virtual port.
  3348. * @shost: scsi host the virtual port is connected to.
  3349. * @channel: Channel on shost port connected to.
  3350. * @pdev: parent device for vport
  3351. * @ids: The world wide names, FC4 port roles, etc for
  3352. * the virtual port.
  3353. * @ret_vport: The pointer to the created vport.
  3354. *
  3355. * Allocates and creates the vport structure, calls the parent host
  3356. * to instantiate the vport, this completes w/ class and sysfs creation.
  3357. *
  3358. * Notes:
  3359. * This routine assumes no locks are held on entry.
  3360. */
  3361. static int
  3362. fc_vport_setup(struct Scsi_Host *shost, int channel, struct device *pdev,
  3363. struct fc_vport_identifiers *ids, struct fc_vport **ret_vport)
  3364. {
  3365. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  3366. struct fc_internal *fci = to_fc_internal(shost->transportt);
  3367. struct fc_vport *vport;
  3368. struct device *dev;
  3369. unsigned long flags;
  3370. size_t size;
  3371. int error;
  3372. *ret_vport = NULL;
  3373. if ( ! fci->f->vport_create)
  3374. return -ENOENT;
  3375. size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
  3376. vport = kzalloc(size, GFP_KERNEL);
  3377. if (unlikely(!vport)) {
  3378. printk(KERN_ERR "%s: allocation failure\n", __func__);
  3379. return -ENOMEM;
  3380. }
  3381. vport->vport_state = FC_VPORT_UNKNOWN;
  3382. vport->vport_last_state = FC_VPORT_UNKNOWN;
  3383. vport->node_name = ids->node_name;
  3384. vport->port_name = ids->port_name;
  3385. vport->roles = ids->roles;
  3386. vport->vport_type = ids->vport_type;
  3387. if (fci->f->dd_fcvport_size)
  3388. vport->dd_data = &vport[1];
  3389. vport->shost = shost;
  3390. vport->channel = channel;
  3391. vport->flags = FC_VPORT_CREATING;
  3392. INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
  3393. spin_lock_irqsave(shost->host_lock, flags);
  3394. if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
  3395. spin_unlock_irqrestore(shost->host_lock, flags);
  3396. kfree(vport);
  3397. return -ENOSPC;
  3398. }
  3399. fc_host->npiv_vports_inuse++;
  3400. vport->number = fc_host->next_vport_number++;
  3401. list_add_tail(&vport->peers, &fc_host->vports);
  3402. scsi_host_get(shost); /* for fc_host->vport list */
  3403. spin_unlock_irqrestore(shost->host_lock, flags);
  3404. dev = &vport->dev;
  3405. device_initialize(dev); /* takes self reference */
  3406. dev->parent = get_device(pdev); /* takes parent reference */
  3407. dev->release = fc_vport_dev_release;
  3408. dev_set_name(dev, "vport-%d:%d-%d",
  3409. shost->host_no, channel, vport->number);
  3410. transport_setup_device(dev);
  3411. error = device_add(dev);
  3412. if (error) {
  3413. printk(KERN_ERR "FC Virtual Port device_add failed\n");
  3414. goto delete_vport;
  3415. }
  3416. transport_add_device(dev);
  3417. transport_configure_device(dev);
  3418. error = fci->f->vport_create(vport, ids->disable);
  3419. if (error) {
  3420. printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
  3421. goto delete_vport_all;
  3422. }
  3423. /*
  3424. * if the parent isn't the physical adapter's Scsi_Host, ensure
  3425. * the Scsi_Host at least contains a symlink to the vport.
  3426. */
  3427. if (pdev != &shost->shost_gendev) {
  3428. error = sysfs_create_link(&shost->shost_gendev.kobj,
  3429. &dev->kobj, dev_name(dev));
  3430. if (error)
  3431. printk(KERN_ERR
  3432. "%s: Cannot create vport symlinks for "
  3433. "%s, err=%d\n",
  3434. __func__, dev_name(dev), error);
  3435. }
  3436. spin_lock_irqsave(shost->host_lock, flags);
  3437. vport->flags &= ~FC_VPORT_CREATING;
  3438. spin_unlock_irqrestore(shost->host_lock, flags);
  3439. dev_printk(KERN_NOTICE, pdev,
  3440. "%s created via shost%d channel %d\n", dev_name(dev),
  3441. shost->host_no, channel);
  3442. *ret_vport = vport;
  3443. return 0;
  3444. delete_vport_all:
  3445. transport_remove_device(dev);
  3446. device_del(dev);
  3447. delete_vport:
  3448. transport_destroy_device(dev);
  3449. spin_lock_irqsave(shost->host_lock, flags);
  3450. list_del(&vport->peers);
  3451. scsi_host_put(shost); /* for fc_host->vport list */
  3452. fc_host->npiv_vports_inuse--;
  3453. spin_unlock_irqrestore(shost->host_lock, flags);
  3454. put_device(dev->parent);
  3455. kfree(vport);
  3456. return error;
  3457. }
  3458. /**
  3459. * fc_vport_create - Admin App or LLDD requests creation of a vport
  3460. * @shost: scsi host the virtual port is connected to.
  3461. * @channel: channel on shost port connected to.
  3462. * @ids: The world wide names, FC4 port roles, etc for
  3463. * the virtual port.
  3464. *
  3465. * Notes:
  3466. * This routine assumes no locks are held on entry.
  3467. */
  3468. struct fc_vport *
  3469. fc_vport_create(struct Scsi_Host *shost, int channel,
  3470. struct fc_vport_identifiers *ids)
  3471. {
  3472. int stat;
  3473. struct fc_vport *vport;
  3474. stat = fc_vport_setup(shost, channel, &shost->shost_gendev,
  3475. ids, &vport);
  3476. return stat ? NULL : vport;
  3477. }
  3478. EXPORT_SYMBOL(fc_vport_create);
  3479. /**
  3480. * fc_vport_terminate - Admin App or LLDD requests termination of a vport
  3481. * @vport: fc_vport to be terminated
  3482. *
  3483. * Calls the LLDD vport_delete() function, then deallocates and removes
  3484. * the vport from the shost and object tree.
  3485. *
  3486. * Notes:
  3487. * This routine assumes no locks are held on entry.
  3488. */
  3489. int
  3490. fc_vport_terminate(struct fc_vport *vport)
  3491. {
  3492. struct Scsi_Host *shost = vport_to_shost(vport);
  3493. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  3494. struct fc_internal *i = to_fc_internal(shost->transportt);
  3495. struct device *dev = &vport->dev;
  3496. unsigned long flags;
  3497. int stat;
  3498. if (i->f->vport_delete)
  3499. stat = i->f->vport_delete(vport);
  3500. else
  3501. stat = -ENOENT;
  3502. spin_lock_irqsave(shost->host_lock, flags);
  3503. vport->flags &= ~FC_VPORT_DELETING;
  3504. if (!stat) {
  3505. vport->flags |= FC_VPORT_DELETED;
  3506. list_del(&vport->peers);
  3507. fc_host->npiv_vports_inuse--;
  3508. scsi_host_put(shost); /* for fc_host->vport list */
  3509. }
  3510. spin_unlock_irqrestore(shost->host_lock, flags);
  3511. if (stat)
  3512. return stat;
  3513. if (dev->parent != &shost->shost_gendev)
  3514. sysfs_remove_link(&shost->shost_gendev.kobj, dev_name(dev));
  3515. transport_remove_device(dev);
  3516. device_del(dev);
  3517. transport_destroy_device(dev);
  3518. /*
  3519. * Removing our self-reference should mean our
  3520. * release function gets called, which will drop the remaining
  3521. * parent reference and free the data structure.
  3522. */
  3523. put_device(dev); /* for self-reference */
  3524. return 0; /* SUCCESS */
  3525. }
  3526. EXPORT_SYMBOL(fc_vport_terminate);
  3527. /**
  3528. * fc_vport_sched_delete - workq-based delete request for a vport
  3529. * @work: vport to be deleted.
  3530. */
  3531. static void
  3532. fc_vport_sched_delete(struct work_struct *work)
  3533. {
  3534. struct fc_vport *vport =
  3535. container_of(work, struct fc_vport, vport_delete_work);
  3536. int stat;
  3537. stat = fc_vport_terminate(vport);
  3538. if (stat)
  3539. dev_printk(KERN_ERR, vport->dev.parent,
  3540. "%s: %s could not be deleted created via "
  3541. "shost%d channel %d - error %d\n", __func__,
  3542. dev_name(&vport->dev), vport->shost->host_no,
  3543. vport->channel, stat);
  3544. }
  3545. /*
  3546. * BSG support
  3547. */
  3548. /**
  3549. * fc_bsg_job_timeout - handler for when a bsg request timesout
  3550. * @req: request that timed out
  3551. */
  3552. static enum blk_eh_timer_return
  3553. fc_bsg_job_timeout(struct request *req)
  3554. {
  3555. struct bsg_job *job = blk_mq_rq_to_pdu(req);
  3556. struct Scsi_Host *shost = fc_bsg_to_shost(job);
  3557. struct fc_rport *rport = fc_bsg_to_rport(job);
  3558. struct fc_internal *i = to_fc_internal(shost->transportt);
  3559. int err = 0, inflight = 0;
  3560. if (rport && rport->port_state == FC_PORTSTATE_BLOCKED)
  3561. return BLK_EH_RESET_TIMER;
  3562. inflight = bsg_job_get(job);
  3563. if (inflight && i->f->bsg_timeout) {
  3564. /* call LLDD to abort the i/o as it has timed out */
  3565. err = i->f->bsg_timeout(job);
  3566. if (err == -EAGAIN) {
  3567. bsg_job_put(job);
  3568. return BLK_EH_RESET_TIMER;
  3569. } else if (err)
  3570. printk(KERN_ERR "ERROR: FC BSG request timeout - LLD "
  3571. "abort failed with status %d\n", err);
  3572. }
  3573. /* the blk_end_sync_io() doesn't check the error */
  3574. if (inflight)
  3575. blk_mq_end_request(req, BLK_STS_IOERR);
  3576. return BLK_EH_DONE;
  3577. }
  3578. /**
  3579. * fc_bsg_host_dispatch - process fc host bsg requests and dispatch to LLDD
  3580. * @shost: scsi host rport attached to
  3581. * @job: bsg job to be processed
  3582. */
  3583. static int fc_bsg_host_dispatch(struct Scsi_Host *shost, struct bsg_job *job)
  3584. {
  3585. struct fc_internal *i = to_fc_internal(shost->transportt);
  3586. struct fc_bsg_request *bsg_request = job->request;
  3587. struct fc_bsg_reply *bsg_reply = job->reply;
  3588. int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
  3589. int ret;
  3590. /* check if we really have all the request data needed */
  3591. if (job->request_len < cmdlen) {
  3592. ret = -ENOMSG;
  3593. goto fail_host_msg;
  3594. }
  3595. /* Validate the host command */
  3596. switch (bsg_request->msgcode) {
  3597. case FC_BSG_HST_ADD_RPORT:
  3598. cmdlen += sizeof(struct fc_bsg_host_add_rport);
  3599. break;
  3600. case FC_BSG_HST_DEL_RPORT:
  3601. cmdlen += sizeof(struct fc_bsg_host_del_rport);
  3602. break;
  3603. case FC_BSG_HST_ELS_NOLOGIN:
  3604. cmdlen += sizeof(struct fc_bsg_host_els);
  3605. /* there better be a xmt and rcv payloads */
  3606. if ((!job->request_payload.payload_len) ||
  3607. (!job->reply_payload.payload_len)) {
  3608. ret = -EINVAL;
  3609. goto fail_host_msg;
  3610. }
  3611. break;
  3612. case FC_BSG_HST_CT:
  3613. cmdlen += sizeof(struct fc_bsg_host_ct);
  3614. /* there better be xmt and rcv payloads */
  3615. if ((!job->request_payload.payload_len) ||
  3616. (!job->reply_payload.payload_len)) {
  3617. ret = -EINVAL;
  3618. goto fail_host_msg;
  3619. }
  3620. break;
  3621. case FC_BSG_HST_VENDOR:
  3622. cmdlen += sizeof(struct fc_bsg_host_vendor);
  3623. if ((shost->hostt->vendor_id == 0L) ||
  3624. (bsg_request->rqst_data.h_vendor.vendor_id !=
  3625. shost->hostt->vendor_id)) {
  3626. ret = -ESRCH;
  3627. goto fail_host_msg;
  3628. }
  3629. break;
  3630. default:
  3631. ret = -EBADR;
  3632. goto fail_host_msg;
  3633. }
  3634. ret = i->f->bsg_request(job);
  3635. if (!ret)
  3636. return 0;
  3637. fail_host_msg:
  3638. /* return the errno failure code as the only status */
  3639. BUG_ON(job->reply_len < sizeof(uint32_t));
  3640. bsg_reply->reply_payload_rcv_len = 0;
  3641. bsg_reply->result = ret;
  3642. job->reply_len = sizeof(uint32_t);
  3643. bsg_job_done(job, bsg_reply->result,
  3644. bsg_reply->reply_payload_rcv_len);
  3645. return 0;
  3646. }
  3647. /*
  3648. * fc_bsg_goose_queue - restart rport queue in case it was stopped
  3649. * @rport: rport to be restarted
  3650. */
  3651. static void
  3652. fc_bsg_goose_queue(struct fc_rport *rport)
  3653. {
  3654. struct request_queue *q = rport->rqst_q;
  3655. if (q)
  3656. blk_mq_run_hw_queues(q, true);
  3657. }
  3658. /**
  3659. * fc_bsg_rport_dispatch - process rport bsg requests and dispatch to LLDD
  3660. * @shost: scsi host rport attached to
  3661. * @job: bsg job to be processed
  3662. */
  3663. static int fc_bsg_rport_dispatch(struct Scsi_Host *shost, struct bsg_job *job)
  3664. {
  3665. struct fc_internal *i = to_fc_internal(shost->transportt);
  3666. struct fc_bsg_request *bsg_request = job->request;
  3667. struct fc_bsg_reply *bsg_reply = job->reply;
  3668. int cmdlen = sizeof(uint32_t); /* start with length of msgcode */
  3669. int ret;
  3670. /* check if we really have all the request data needed */
  3671. if (job->request_len < cmdlen) {
  3672. ret = -ENOMSG;
  3673. goto fail_rport_msg;
  3674. }
  3675. /* Validate the rport command */
  3676. switch (bsg_request->msgcode) {
  3677. case FC_BSG_RPT_ELS:
  3678. cmdlen += sizeof(struct fc_bsg_rport_els);
  3679. goto check_bidi;
  3680. case FC_BSG_RPT_CT:
  3681. cmdlen += sizeof(struct fc_bsg_rport_ct);
  3682. check_bidi:
  3683. /* there better be xmt and rcv payloads */
  3684. if ((!job->request_payload.payload_len) ||
  3685. (!job->reply_payload.payload_len)) {
  3686. ret = -EINVAL;
  3687. goto fail_rport_msg;
  3688. }
  3689. break;
  3690. default:
  3691. ret = -EBADR;
  3692. goto fail_rport_msg;
  3693. }
  3694. ret = i->f->bsg_request(job);
  3695. if (!ret)
  3696. return 0;
  3697. fail_rport_msg:
  3698. /* return the errno failure code as the only status */
  3699. BUG_ON(job->reply_len < sizeof(uint32_t));
  3700. bsg_reply->reply_payload_rcv_len = 0;
  3701. bsg_reply->result = ret;
  3702. job->reply_len = sizeof(uint32_t);
  3703. bsg_job_done(job, bsg_reply->result,
  3704. bsg_reply->reply_payload_rcv_len);
  3705. return 0;
  3706. }
  3707. static int fc_bsg_dispatch(struct bsg_job *job)
  3708. {
  3709. struct Scsi_Host *shost = fc_bsg_to_shost(job);
  3710. if (scsi_is_fc_rport(job->dev))
  3711. return fc_bsg_rport_dispatch(shost, job);
  3712. else
  3713. return fc_bsg_host_dispatch(shost, job);
  3714. }
  3715. static blk_status_t fc_bsg_rport_prep(struct fc_rport *rport)
  3716. {
  3717. if (rport->port_state == FC_PORTSTATE_BLOCKED &&
  3718. !(rport->flags & FC_RPORT_FAST_FAIL_TIMEDOUT))
  3719. return BLK_STS_RESOURCE;
  3720. if ((rport->port_state != FC_PORTSTATE_ONLINE) &&
  3721. (rport->port_state != FC_PORTSTATE_MARGINAL))
  3722. return BLK_STS_IOERR;
  3723. return BLK_STS_OK;
  3724. }
  3725. static int fc_bsg_dispatch_prep(struct bsg_job *job)
  3726. {
  3727. struct fc_rport *rport = fc_bsg_to_rport(job);
  3728. blk_status_t ret;
  3729. ret = fc_bsg_rport_prep(rport);
  3730. switch (ret) {
  3731. case BLK_STS_OK:
  3732. break;
  3733. case BLK_STS_RESOURCE:
  3734. return -EAGAIN;
  3735. default:
  3736. return -EIO;
  3737. }
  3738. return fc_bsg_dispatch(job);
  3739. }
  3740. /**
  3741. * fc_bsg_hostadd - Create and add the bsg hooks so we can receive requests
  3742. * @shost: shost for fc_host
  3743. * @fc_host: fc_host adding the structures to
  3744. */
  3745. static int
  3746. fc_bsg_hostadd(struct Scsi_Host *shost, struct fc_host_attrs *fc_host)
  3747. {
  3748. struct device *dev = &shost->shost_gendev;
  3749. struct fc_internal *i = to_fc_internal(shost->transportt);
  3750. struct queue_limits lim;
  3751. struct request_queue *q;
  3752. char bsg_name[20];
  3753. fc_host->rqst_q = NULL;
  3754. if (!i->f->bsg_request)
  3755. return -ENOTSUPP;
  3756. snprintf(bsg_name, sizeof(bsg_name),
  3757. "fc_host%d", shost->host_no);
  3758. scsi_init_limits(shost, &lim);
  3759. lim.max_segments = min_not_zero(lim.max_segments, i->f->max_bsg_segments);
  3760. q = bsg_setup_queue(dev, bsg_name, &lim, fc_bsg_dispatch,
  3761. fc_bsg_job_timeout, i->f->dd_bsg_size);
  3762. if (IS_ERR(q)) {
  3763. dev_err(dev,
  3764. "fc_host%d: bsg interface failed to initialize - setup queue\n",
  3765. shost->host_no);
  3766. return PTR_ERR(q);
  3767. }
  3768. blk_queue_rq_timeout(q, FC_DEFAULT_BSG_TIMEOUT);
  3769. fc_host->rqst_q = q;
  3770. return 0;
  3771. }
  3772. /**
  3773. * fc_bsg_rportadd - Create and add the bsg hooks so we can receive requests
  3774. * @shost: shost that rport is attached to
  3775. * @rport: rport that the bsg hooks are being attached to
  3776. */
  3777. static int
  3778. fc_bsg_rportadd(struct Scsi_Host *shost, struct fc_rport *rport)
  3779. {
  3780. struct device *dev = &rport->dev;
  3781. struct fc_internal *i = to_fc_internal(shost->transportt);
  3782. struct queue_limits lim;
  3783. struct request_queue *q;
  3784. rport->rqst_q = NULL;
  3785. if (!i->f->bsg_request)
  3786. return -ENOTSUPP;
  3787. scsi_init_limits(shost, &lim);
  3788. lim.max_segments = min_not_zero(lim.max_segments, i->f->max_bsg_segments);
  3789. q = bsg_setup_queue(dev, dev_name(dev), &lim, fc_bsg_dispatch_prep,
  3790. fc_bsg_job_timeout, i->f->dd_bsg_size);
  3791. if (IS_ERR(q)) {
  3792. dev_err(dev, "failed to setup bsg queue\n");
  3793. return PTR_ERR(q);
  3794. }
  3795. blk_queue_rq_timeout(q, BLK_DEFAULT_SG_TIMEOUT);
  3796. rport->rqst_q = q;
  3797. return 0;
  3798. }
  3799. /**
  3800. * fc_bsg_remove - Deletes the bsg hooks on fchosts/rports
  3801. * @q: the request_queue that is to be torn down.
  3802. *
  3803. * Notes:
  3804. * Before unregistering the queue empty any requests that are blocked
  3805. *
  3806. *
  3807. */
  3808. static void
  3809. fc_bsg_remove(struct request_queue *q)
  3810. {
  3811. bsg_remove_queue(q);
  3812. }
  3813. /* Original Author: Martin Hicks */
  3814. MODULE_AUTHOR("James Smart");
  3815. MODULE_DESCRIPTION("FC Transport Attributes");
  3816. MODULE_LICENSE("GPL");
  3817. module_init(fc_transport_init);
  3818. module_exit(fc_transport_exit);