scsi_transport_fc.c 100 KB

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