scsi_transport_fc.c 119 KB

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