scsi_transport_fc.c 121 KB

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