scsi_transport_fc.c 117 KB

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