scsi_transport_fc.c 120 KB

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