scsi_transport_fc.c 121 KB

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