scsi_transport_fc.c 116 KB

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