scsi_transport_fc.c 129 KB

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