scsi_transport_fc.c 117 KB

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