scsi_transport_fc.c 118 KB

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