scsi_transport_fc.c 120 KB

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