scsi_transport_fc.c 120 KB

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