scsi_transport_fc.c 115 KB

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