scsi_transport_fc.c 114 KB

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