scsi_transport_fc.c 100 KB

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