scsi_transport_fc.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455
  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-2005 James Smart, Emulex Corporation
  23. * Rewrite for host, target, device, and remote port attributes,
  24. * statistics, and service functions...
  25. *
  26. */
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <scsi/scsi_device.h>
  30. #include <scsi/scsi_host.h>
  31. #include <scsi/scsi_transport.h>
  32. #include <scsi/scsi_transport_fc.h>
  33. #include <scsi/scsi_cmnd.h>
  34. #include <linux/netlink.h>
  35. #include <net/netlink.h>
  36. #include <scsi/scsi_netlink_fc.h>
  37. #include "scsi_priv.h"
  38. static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
  39. /*
  40. * Redefine so that we can have same named attributes in the
  41. * sdev/starget/host objects.
  42. */
  43. #define FC_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
  44. struct class_device_attribute class_device_attr_##_prefix##_##_name = \
  45. __ATTR(_name,_mode,_show,_store)
  46. #define fc_enum_name_search(title, table_type, table) \
  47. static const char *get_fc_##title##_name(enum table_type table_key) \
  48. { \
  49. int i; \
  50. char *name = NULL; \
  51. \
  52. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  53. if (table[i].value == table_key) { \
  54. name = table[i].name; \
  55. break; \
  56. } \
  57. } \
  58. return name; \
  59. }
  60. #define fc_enum_name_match(title, table_type, table) \
  61. static int get_fc_##title##_match(const char *table_key, \
  62. enum table_type *value) \
  63. { \
  64. int i; \
  65. \
  66. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  67. if (strncmp(table_key, table[i].name, \
  68. table[i].matchlen) == 0) { \
  69. *value = table[i].value; \
  70. return 0; /* success */ \
  71. } \
  72. } \
  73. return 1; /* failure */ \
  74. }
  75. /* Convert fc_port_type values to ascii string name */
  76. static struct {
  77. enum fc_port_type value;
  78. char *name;
  79. } fc_port_type_names[] = {
  80. { FC_PORTTYPE_UNKNOWN, "Unknown" },
  81. { FC_PORTTYPE_OTHER, "Other" },
  82. { FC_PORTTYPE_NOTPRESENT, "Not Present" },
  83. { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
  84. { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
  85. { FC_PORTTYPE_LPORT, "LPort (private loop)" },
  86. { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection" },
  87. };
  88. fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
  89. #define FC_PORTTYPE_MAX_NAMELEN 50
  90. /* Convert fc_host_event_code values to ascii string name */
  91. static const struct {
  92. enum fc_host_event_code value;
  93. char *name;
  94. } fc_host_event_code_names[] = {
  95. { FCH_EVT_LIP, "lip" },
  96. { FCH_EVT_LINKUP, "link_up" },
  97. { FCH_EVT_LINKDOWN, "link_down" },
  98. { FCH_EVT_LIPRESET, "lip_reset" },
  99. { FCH_EVT_RSCN, "rscn" },
  100. { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
  101. { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
  102. { FCH_EVT_PORT_ONLINE, "port_online" },
  103. { FCH_EVT_PORT_OFFLINE, "port_offline" },
  104. { FCH_EVT_PORT_FABRIC, "port_fabric" },
  105. { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
  106. { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
  107. };
  108. fc_enum_name_search(host_event_code, fc_host_event_code,
  109. fc_host_event_code_names)
  110. #define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
  111. /* Convert fc_port_state values to ascii string name */
  112. static struct {
  113. enum fc_port_state value;
  114. char *name;
  115. } fc_port_state_names[] = {
  116. { FC_PORTSTATE_UNKNOWN, "Unknown" },
  117. { FC_PORTSTATE_NOTPRESENT, "Not Present" },
  118. { FC_PORTSTATE_ONLINE, "Online" },
  119. { FC_PORTSTATE_OFFLINE, "Offline" },
  120. { FC_PORTSTATE_BLOCKED, "Blocked" },
  121. { FC_PORTSTATE_BYPASSED, "Bypassed" },
  122. { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
  123. { FC_PORTSTATE_LINKDOWN, "Linkdown" },
  124. { FC_PORTSTATE_ERROR, "Error" },
  125. { FC_PORTSTATE_LOOPBACK, "Loopback" },
  126. { FC_PORTSTATE_DELETED, "Deleted" },
  127. };
  128. fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
  129. #define FC_PORTSTATE_MAX_NAMELEN 20
  130. /* Convert fc_tgtid_binding_type values to ascii string name */
  131. static const struct {
  132. enum fc_tgtid_binding_type value;
  133. char *name;
  134. int matchlen;
  135. } fc_tgtid_binding_type_names[] = {
  136. { FC_TGTID_BIND_NONE, "none", 4 },
  137. { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
  138. { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
  139. { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
  140. };
  141. fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
  142. fc_tgtid_binding_type_names)
  143. fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
  144. fc_tgtid_binding_type_names)
  145. #define FC_BINDTYPE_MAX_NAMELEN 30
  146. #define fc_bitfield_name_search(title, table) \
  147. static ssize_t \
  148. get_fc_##title##_names(u32 table_key, char *buf) \
  149. { \
  150. char *prefix = ""; \
  151. ssize_t len = 0; \
  152. int i; \
  153. \
  154. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  155. if (table[i].value & table_key) { \
  156. len += sprintf(buf + len, "%s%s", \
  157. prefix, table[i].name); \
  158. prefix = ", "; \
  159. } \
  160. } \
  161. len += sprintf(buf + len, "\n"); \
  162. return len; \
  163. }
  164. /* Convert FC_COS bit values to ascii string name */
  165. static const struct {
  166. u32 value;
  167. char *name;
  168. } fc_cos_names[] = {
  169. { FC_COS_CLASS1, "Class 1" },
  170. { FC_COS_CLASS2, "Class 2" },
  171. { FC_COS_CLASS3, "Class 3" },
  172. { FC_COS_CLASS4, "Class 4" },
  173. { FC_COS_CLASS6, "Class 6" },
  174. };
  175. fc_bitfield_name_search(cos, fc_cos_names)
  176. /* Convert FC_PORTSPEED bit values to ascii string name */
  177. static const struct {
  178. u32 value;
  179. char *name;
  180. } fc_port_speed_names[] = {
  181. { FC_PORTSPEED_1GBIT, "1 Gbit" },
  182. { FC_PORTSPEED_2GBIT, "2 Gbit" },
  183. { FC_PORTSPEED_4GBIT, "4 Gbit" },
  184. { FC_PORTSPEED_10GBIT, "10 Gbit" },
  185. { FC_PORTSPEED_8GBIT, "8 Gbit" },
  186. { FC_PORTSPEED_16GBIT, "16 Gbit" },
  187. { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
  188. };
  189. fc_bitfield_name_search(port_speed, fc_port_speed_names)
  190. static int
  191. show_fc_fc4s (char *buf, u8 *fc4_list)
  192. {
  193. int i, len=0;
  194. for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
  195. len += sprintf(buf + len , "0x%02x ", *fc4_list);
  196. len += sprintf(buf + len, "\n");
  197. return len;
  198. }
  199. /* Convert FC_RPORT_ROLE bit values to ascii string name */
  200. static const struct {
  201. u32 value;
  202. char *name;
  203. } fc_remote_port_role_names[] = {
  204. { FC_RPORT_ROLE_FCP_TARGET, "FCP Target" },
  205. { FC_RPORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
  206. { FC_RPORT_ROLE_IP_PORT, "IP Port" },
  207. };
  208. fc_bitfield_name_search(remote_port_roles, fc_remote_port_role_names)
  209. /*
  210. * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
  211. */
  212. #define FC_WELLKNOWN_PORTID_MASK 0xfffff0
  213. #define FC_WELLKNOWN_ROLE_MASK 0x00000f
  214. #define FC_FPORT_PORTID 0x00000e
  215. #define FC_FABCTLR_PORTID 0x00000d
  216. #define FC_DIRSRVR_PORTID 0x00000c
  217. #define FC_TIMESRVR_PORTID 0x00000b
  218. #define FC_MGMTSRVR_PORTID 0x00000a
  219. static void fc_timeout_deleted_rport(struct work_struct *work);
  220. static void fc_timeout_fail_rport_io(struct work_struct *work);
  221. static void fc_scsi_scan_rport(struct work_struct *work);
  222. /*
  223. * Attribute counts pre object type...
  224. * Increase these values if you add attributes
  225. */
  226. #define FC_STARGET_NUM_ATTRS 3
  227. #define FC_RPORT_NUM_ATTRS 10
  228. #define FC_HOST_NUM_ATTRS 17
  229. struct fc_internal {
  230. struct scsi_transport_template t;
  231. struct fc_function_template *f;
  232. /*
  233. * For attributes : each object has :
  234. * An array of the actual attributes structures
  235. * An array of null-terminated pointers to the attribute
  236. * structures - used for mid-layer interaction.
  237. *
  238. * The attribute containers for the starget and host are are
  239. * part of the midlayer. As the remote port is specific to the
  240. * fc transport, we must provide the attribute container.
  241. */
  242. struct class_device_attribute private_starget_attrs[
  243. FC_STARGET_NUM_ATTRS];
  244. struct class_device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
  245. struct class_device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
  246. struct class_device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
  247. struct transport_container rport_attr_cont;
  248. struct class_device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
  249. struct class_device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
  250. };
  251. #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
  252. static int fc_target_setup(struct transport_container *tc, struct device *dev,
  253. struct class_device *cdev)
  254. {
  255. struct scsi_target *starget = to_scsi_target(dev);
  256. struct fc_rport *rport = starget_to_rport(starget);
  257. /*
  258. * if parent is remote port, use values from remote port.
  259. * Otherwise, this host uses the fc_transport, but not the
  260. * remote port interface. As such, initialize to known non-values.
  261. */
  262. if (rport) {
  263. fc_starget_node_name(starget) = rport->node_name;
  264. fc_starget_port_name(starget) = rport->port_name;
  265. fc_starget_port_id(starget) = rport->port_id;
  266. } else {
  267. fc_starget_node_name(starget) = -1;
  268. fc_starget_port_name(starget) = -1;
  269. fc_starget_port_id(starget) = -1;
  270. }
  271. return 0;
  272. }
  273. static DECLARE_TRANSPORT_CLASS(fc_transport_class,
  274. "fc_transport",
  275. fc_target_setup,
  276. NULL,
  277. NULL);
  278. static int fc_host_setup(struct transport_container *tc, struct device *dev,
  279. struct class_device *cdev)
  280. {
  281. struct Scsi_Host *shost = dev_to_shost(dev);
  282. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  283. /*
  284. * Set default values easily detected by the midlayer as
  285. * failure cases. The scsi lldd is responsible for initializing
  286. * all transport attributes to valid values per host.
  287. */
  288. fc_host->node_name = -1;
  289. fc_host->port_name = -1;
  290. fc_host->permanent_port_name = -1;
  291. fc_host->supported_classes = FC_COS_UNSPECIFIED;
  292. memset(fc_host->supported_fc4s, 0,
  293. sizeof(fc_host->supported_fc4s));
  294. fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
  295. fc_host->maxframe_size = -1;
  296. memset(fc_host->serial_number, 0,
  297. sizeof(fc_host->serial_number));
  298. fc_host->port_id = -1;
  299. fc_host->port_type = FC_PORTTYPE_UNKNOWN;
  300. fc_host->port_state = FC_PORTSTATE_UNKNOWN;
  301. memset(fc_host->active_fc4s, 0,
  302. sizeof(fc_host->active_fc4s));
  303. fc_host->speed = FC_PORTSPEED_UNKNOWN;
  304. fc_host->fabric_name = -1;
  305. memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
  306. memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
  307. fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
  308. INIT_LIST_HEAD(&fc_host->rports);
  309. INIT_LIST_HEAD(&fc_host->rport_bindings);
  310. fc_host->next_rport_number = 0;
  311. fc_host->next_target_id = 0;
  312. snprintf(fc_host->work_q_name, KOBJ_NAME_LEN, "fc_wq_%d",
  313. shost->host_no);
  314. fc_host->work_q = create_singlethread_workqueue(
  315. fc_host->work_q_name);
  316. if (!fc_host->work_q)
  317. return -ENOMEM;
  318. snprintf(fc_host->devloss_work_q_name, KOBJ_NAME_LEN, "fc_dl_%d",
  319. shost->host_no);
  320. fc_host->devloss_work_q = create_singlethread_workqueue(
  321. fc_host->devloss_work_q_name);
  322. if (!fc_host->devloss_work_q) {
  323. destroy_workqueue(fc_host->work_q);
  324. fc_host->work_q = NULL;
  325. return -ENOMEM;
  326. }
  327. return 0;
  328. }
  329. static DECLARE_TRANSPORT_CLASS(fc_host_class,
  330. "fc_host",
  331. fc_host_setup,
  332. NULL,
  333. NULL);
  334. /*
  335. * Setup and Remove actions for remote ports are handled
  336. * in the service functions below.
  337. */
  338. static DECLARE_TRANSPORT_CLASS(fc_rport_class,
  339. "fc_remote_ports",
  340. NULL,
  341. NULL,
  342. NULL);
  343. /*
  344. * Module Parameters
  345. */
  346. /*
  347. * dev_loss_tmo: the default number of seconds that the FC transport
  348. * should insulate the loss of a remote port.
  349. * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
  350. */
  351. static unsigned int fc_dev_loss_tmo = 60; /* seconds */
  352. module_param_named(dev_loss_tmo, fc_dev_loss_tmo, int, S_IRUGO|S_IWUSR);
  353. MODULE_PARM_DESC(dev_loss_tmo,
  354. "Maximum number of seconds that the FC transport should"
  355. " insulate the loss of a remote port. Once this value is"
  356. " exceeded, the scsi target is removed. Value should be"
  357. " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
  358. /**
  359. * Netlink Infrastructure
  360. **/
  361. static atomic_t fc_event_seq;
  362. /**
  363. * fc_get_event_number - Obtain the next sequential FC event number
  364. *
  365. * Notes:
  366. * We could have inline'd this, but it would have required fc_event_seq to
  367. * be exposed. For now, live with the subroutine call.
  368. * Atomic used to avoid lock/unlock...
  369. **/
  370. u32
  371. fc_get_event_number(void)
  372. {
  373. return atomic_add_return(1, &fc_event_seq);
  374. }
  375. EXPORT_SYMBOL(fc_get_event_number);
  376. /**
  377. * fc_host_post_event - called to post an even on an fc_host.
  378. *
  379. * @shost: host the event occurred on
  380. * @event_number: fc event number obtained from get_fc_event_number()
  381. * @event_code: fc_host event being posted
  382. * @event_data: 32bits of data for the event being posted
  383. *
  384. * Notes:
  385. * This routine assumes no locks are held on entry.
  386. **/
  387. void
  388. fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
  389. enum fc_host_event_code event_code, u32 event_data)
  390. {
  391. struct sk_buff *skb;
  392. struct nlmsghdr *nlh;
  393. struct fc_nl_event *event;
  394. const char *name;
  395. u32 len, skblen;
  396. int err;
  397. if (!scsi_nl_sock) {
  398. err = -ENOENT;
  399. goto send_fail;
  400. }
  401. len = FC_NL_MSGALIGN(sizeof(*event));
  402. skblen = NLMSG_SPACE(len);
  403. skb = alloc_skb(skblen, GFP_KERNEL);
  404. if (!skb) {
  405. err = -ENOBUFS;
  406. goto send_fail;
  407. }
  408. nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
  409. skblen - sizeof(*nlh), 0);
  410. if (!nlh) {
  411. err = -ENOBUFS;
  412. goto send_fail_skb;
  413. }
  414. event = NLMSG_DATA(nlh);
  415. INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
  416. FC_NL_ASYNC_EVENT, len);
  417. event->seconds = get_seconds();
  418. event->vendor_id = 0;
  419. event->host_no = shost->host_no;
  420. event->event_datalen = sizeof(u32); /* bytes */
  421. event->event_num = event_number;
  422. event->event_code = event_code;
  423. event->event_data = event_data;
  424. err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
  425. GFP_KERNEL);
  426. if (err && (err != -ESRCH)) /* filter no recipient errors */
  427. /* nlmsg_multicast already kfree_skb'd */
  428. goto send_fail;
  429. return;
  430. send_fail_skb:
  431. kfree_skb(skb);
  432. send_fail:
  433. name = get_fc_host_event_code_name(event_code);
  434. printk(KERN_WARNING
  435. "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
  436. __FUNCTION__, shost->host_no,
  437. (name) ? name : "<unknown>", event_data, err);
  438. return;
  439. }
  440. EXPORT_SYMBOL(fc_host_post_event);
  441. /**
  442. * fc_host_post_vendor_event - called to post a vendor unique event on
  443. * a fc_host
  444. *
  445. * @shost: host the event occurred on
  446. * @event_number: fc event number obtained from get_fc_event_number()
  447. * @data_len: amount, in bytes, of vendor unique data
  448. * @data_buf: pointer to vendor unique data
  449. *
  450. * Notes:
  451. * This routine assumes no locks are held on entry.
  452. **/
  453. void
  454. fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
  455. u32 data_len, char * data_buf, u64 vendor_id)
  456. {
  457. struct sk_buff *skb;
  458. struct nlmsghdr *nlh;
  459. struct fc_nl_event *event;
  460. u32 len, skblen;
  461. int err;
  462. if (!scsi_nl_sock) {
  463. err = -ENOENT;
  464. goto send_vendor_fail;
  465. }
  466. len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
  467. skblen = NLMSG_SPACE(len);
  468. skb = alloc_skb(skblen, GFP_KERNEL);
  469. if (!skb) {
  470. err = -ENOBUFS;
  471. goto send_vendor_fail;
  472. }
  473. nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
  474. skblen - sizeof(*nlh), 0);
  475. if (!nlh) {
  476. err = -ENOBUFS;
  477. goto send_vendor_fail_skb;
  478. }
  479. event = NLMSG_DATA(nlh);
  480. INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
  481. FC_NL_ASYNC_EVENT, len);
  482. event->seconds = get_seconds();
  483. event->vendor_id = vendor_id;
  484. event->host_no = shost->host_no;
  485. event->event_datalen = data_len; /* bytes */
  486. event->event_num = event_number;
  487. event->event_code = FCH_EVT_VENDOR_UNIQUE;
  488. memcpy(&event->event_data, data_buf, data_len);
  489. err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
  490. GFP_KERNEL);
  491. if (err && (err != -ESRCH)) /* filter no recipient errors */
  492. /* nlmsg_multicast already kfree_skb'd */
  493. goto send_vendor_fail;
  494. return;
  495. send_vendor_fail_skb:
  496. kfree_skb(skb);
  497. send_vendor_fail:
  498. printk(KERN_WARNING
  499. "%s: Dropped Event : host %d vendor_unique - err %d\n",
  500. __FUNCTION__, shost->host_no, err);
  501. return;
  502. }
  503. EXPORT_SYMBOL(fc_host_post_vendor_event);
  504. static __init int fc_transport_init(void)
  505. {
  506. int error;
  507. atomic_set(&fc_event_seq, 0);
  508. error = transport_class_register(&fc_host_class);
  509. if (error)
  510. return error;
  511. error = transport_class_register(&fc_rport_class);
  512. if (error)
  513. return error;
  514. return transport_class_register(&fc_transport_class);
  515. }
  516. static void __exit fc_transport_exit(void)
  517. {
  518. transport_class_unregister(&fc_transport_class);
  519. transport_class_unregister(&fc_rport_class);
  520. transport_class_unregister(&fc_host_class);
  521. }
  522. /*
  523. * FC Remote Port Attribute Management
  524. */
  525. #define fc_rport_show_function(field, format_string, sz, cast) \
  526. static ssize_t \
  527. show_fc_rport_##field (struct class_device *cdev, char *buf) \
  528. { \
  529. struct fc_rport *rport = transport_class_to_rport(cdev); \
  530. struct Scsi_Host *shost = rport_to_shost(rport); \
  531. struct fc_internal *i = to_fc_internal(shost->transportt); \
  532. if ((i->f->get_rport_##field) && \
  533. !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
  534. (rport->port_state == FC_PORTSTATE_DELETED) || \
  535. (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
  536. i->f->get_rport_##field(rport); \
  537. return snprintf(buf, sz, format_string, cast rport->field); \
  538. }
  539. #define fc_rport_store_function(field) \
  540. static ssize_t \
  541. store_fc_rport_##field(struct class_device *cdev, const char *buf, \
  542. size_t count) \
  543. { \
  544. int val; \
  545. struct fc_rport *rport = transport_class_to_rport(cdev); \
  546. struct Scsi_Host *shost = rport_to_shost(rport); \
  547. struct fc_internal *i = to_fc_internal(shost->transportt); \
  548. char *cp; \
  549. if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
  550. (rport->port_state == FC_PORTSTATE_DELETED) || \
  551. (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
  552. return -EBUSY; \
  553. val = simple_strtoul(buf, &cp, 0); \
  554. if (*cp && (*cp != '\n')) \
  555. return -EINVAL; \
  556. i->f->set_rport_##field(rport, val); \
  557. return count; \
  558. }
  559. #define fc_rport_rd_attr(field, format_string, sz) \
  560. fc_rport_show_function(field, format_string, sz, ) \
  561. static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
  562. show_fc_rport_##field, NULL)
  563. #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
  564. fc_rport_show_function(field, format_string, sz, (cast)) \
  565. static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
  566. show_fc_rport_##field, NULL)
  567. #define fc_rport_rw_attr(field, format_string, sz) \
  568. fc_rport_show_function(field, format_string, sz, ) \
  569. fc_rport_store_function(field) \
  570. static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
  571. show_fc_rport_##field, \
  572. store_fc_rport_##field)
  573. #define fc_private_rport_show_function(field, format_string, sz, cast) \
  574. static ssize_t \
  575. show_fc_rport_##field (struct class_device *cdev, char *buf) \
  576. { \
  577. struct fc_rport *rport = transport_class_to_rport(cdev); \
  578. return snprintf(buf, sz, format_string, cast rport->field); \
  579. }
  580. #define fc_private_rport_rd_attr(field, format_string, sz) \
  581. fc_private_rport_show_function(field, format_string, sz, ) \
  582. static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
  583. show_fc_rport_##field, NULL)
  584. #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
  585. fc_private_rport_show_function(field, format_string, sz, (cast)) \
  586. static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
  587. show_fc_rport_##field, NULL)
  588. #define fc_private_rport_rd_enum_attr(title, maxlen) \
  589. static ssize_t \
  590. show_fc_rport_##title (struct class_device *cdev, char *buf) \
  591. { \
  592. struct fc_rport *rport = transport_class_to_rport(cdev); \
  593. const char *name; \
  594. name = get_fc_##title##_name(rport->title); \
  595. if (!name) \
  596. return -EINVAL; \
  597. return snprintf(buf, maxlen, "%s\n", name); \
  598. } \
  599. static FC_CLASS_DEVICE_ATTR(rport, title, S_IRUGO, \
  600. show_fc_rport_##title, NULL)
  601. #define SETUP_RPORT_ATTRIBUTE_RD(field) \
  602. i->private_rport_attrs[count] = class_device_attr_rport_##field; \
  603. i->private_rport_attrs[count].attr.mode = S_IRUGO; \
  604. i->private_rport_attrs[count].store = NULL; \
  605. i->rport_attrs[count] = &i->private_rport_attrs[count]; \
  606. if (i->f->show_rport_##field) \
  607. count++
  608. #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
  609. i->private_rport_attrs[count] = class_device_attr_rport_##field; \
  610. i->private_rport_attrs[count].attr.mode = S_IRUGO; \
  611. i->private_rport_attrs[count].store = NULL; \
  612. i->rport_attrs[count] = &i->private_rport_attrs[count]; \
  613. count++
  614. #define SETUP_RPORT_ATTRIBUTE_RW(field) \
  615. i->private_rport_attrs[count] = class_device_attr_rport_##field; \
  616. if (!i->f->set_rport_##field) { \
  617. i->private_rport_attrs[count].attr.mode = S_IRUGO; \
  618. i->private_rport_attrs[count].store = NULL; \
  619. } \
  620. i->rport_attrs[count] = &i->private_rport_attrs[count]; \
  621. if (i->f->show_rport_##field) \
  622. count++
  623. #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
  624. { \
  625. i->private_rport_attrs[count] = class_device_attr_rport_##field; \
  626. i->rport_attrs[count] = &i->private_rport_attrs[count]; \
  627. count++; \
  628. }
  629. /* The FC Transport Remote Port Attributes: */
  630. /* Fixed Remote Port Attributes */
  631. fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
  632. static ssize_t
  633. show_fc_rport_supported_classes (struct class_device *cdev, char *buf)
  634. {
  635. struct fc_rport *rport = transport_class_to_rport(cdev);
  636. if (rport->supported_classes == FC_COS_UNSPECIFIED)
  637. return snprintf(buf, 20, "unspecified\n");
  638. return get_fc_cos_names(rport->supported_classes, buf);
  639. }
  640. static FC_CLASS_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
  641. show_fc_rport_supported_classes, NULL);
  642. /* Dynamic Remote Port Attributes */
  643. /*
  644. * dev_loss_tmo attribute
  645. */
  646. fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
  647. static ssize_t
  648. store_fc_rport_dev_loss_tmo(struct class_device *cdev, const char *buf,
  649. size_t count)
  650. {
  651. int val;
  652. struct fc_rport *rport = transport_class_to_rport(cdev);
  653. struct Scsi_Host *shost = rport_to_shost(rport);
  654. struct fc_internal *i = to_fc_internal(shost->transportt);
  655. char *cp;
  656. if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
  657. (rport->port_state == FC_PORTSTATE_DELETED) ||
  658. (rport->port_state == FC_PORTSTATE_NOTPRESENT))
  659. return -EBUSY;
  660. val = simple_strtoul(buf, &cp, 0);
  661. if ((*cp && (*cp != '\n')) ||
  662. (val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
  663. return -EINVAL;
  664. i->f->set_rport_dev_loss_tmo(rport, val);
  665. return count;
  666. }
  667. static FC_CLASS_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
  668. show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
  669. /* Private Remote Port Attributes */
  670. fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
  671. fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
  672. fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
  673. static ssize_t
  674. show_fc_rport_roles (struct class_device *cdev, char *buf)
  675. {
  676. struct fc_rport *rport = transport_class_to_rport(cdev);
  677. /* identify any roles that are port_id specific */
  678. if ((rport->port_id != -1) &&
  679. (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
  680. FC_WELLKNOWN_PORTID_MASK) {
  681. switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
  682. case FC_FPORT_PORTID:
  683. return snprintf(buf, 30, "Fabric Port\n");
  684. case FC_FABCTLR_PORTID:
  685. return snprintf(buf, 30, "Fabric Controller\n");
  686. case FC_DIRSRVR_PORTID:
  687. return snprintf(buf, 30, "Directory Server\n");
  688. case FC_TIMESRVR_PORTID:
  689. return snprintf(buf, 30, "Time Server\n");
  690. case FC_MGMTSRVR_PORTID:
  691. return snprintf(buf, 30, "Management Server\n");
  692. default:
  693. return snprintf(buf, 30, "Unknown Fabric Entity\n");
  694. }
  695. } else {
  696. if (rport->roles == FC_RPORT_ROLE_UNKNOWN)
  697. return snprintf(buf, 20, "unknown\n");
  698. return get_fc_remote_port_roles_names(rport->roles, buf);
  699. }
  700. }
  701. static FC_CLASS_DEVICE_ATTR(rport, roles, S_IRUGO,
  702. show_fc_rport_roles, NULL);
  703. fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
  704. fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
  705. /*
  706. * fast_io_fail_tmo attribute
  707. */
  708. static ssize_t
  709. show_fc_rport_fast_io_fail_tmo (struct class_device *cdev, char *buf)
  710. {
  711. struct fc_rport *rport = transport_class_to_rport(cdev);
  712. if (rport->fast_io_fail_tmo == -1)
  713. return snprintf(buf, 5, "off\n");
  714. return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
  715. }
  716. static ssize_t
  717. store_fc_rport_fast_io_fail_tmo(struct class_device *cdev, const char *buf,
  718. size_t count)
  719. {
  720. int val;
  721. char *cp;
  722. struct fc_rport *rport = transport_class_to_rport(cdev);
  723. if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
  724. (rport->port_state == FC_PORTSTATE_DELETED) ||
  725. (rport->port_state == FC_PORTSTATE_NOTPRESENT))
  726. return -EBUSY;
  727. if (strncmp(buf, "off", 3) == 0)
  728. rport->fast_io_fail_tmo = -1;
  729. else {
  730. val = simple_strtoul(buf, &cp, 0);
  731. if ((*cp && (*cp != '\n')) ||
  732. (val < 0) || (val >= rport->dev_loss_tmo))
  733. return -EINVAL;
  734. rport->fast_io_fail_tmo = val;
  735. }
  736. return count;
  737. }
  738. static FC_CLASS_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
  739. show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
  740. /*
  741. * FC SCSI Target Attribute Management
  742. */
  743. /*
  744. * Note: in the target show function we recognize when the remote
  745. * port is in the hierarchy and do not allow the driver to get
  746. * involved in sysfs functions. The driver only gets involved if
  747. * it's the "old" style that doesn't use rports.
  748. */
  749. #define fc_starget_show_function(field, format_string, sz, cast) \
  750. static ssize_t \
  751. show_fc_starget_##field (struct class_device *cdev, char *buf) \
  752. { \
  753. struct scsi_target *starget = transport_class_to_starget(cdev); \
  754. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  755. struct fc_internal *i = to_fc_internal(shost->transportt); \
  756. struct fc_rport *rport = starget_to_rport(starget); \
  757. if (rport) \
  758. fc_starget_##field(starget) = rport->field; \
  759. else if (i->f->get_starget_##field) \
  760. i->f->get_starget_##field(starget); \
  761. return snprintf(buf, sz, format_string, \
  762. cast fc_starget_##field(starget)); \
  763. }
  764. #define fc_starget_rd_attr(field, format_string, sz) \
  765. fc_starget_show_function(field, format_string, sz, ) \
  766. static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
  767. show_fc_starget_##field, NULL)
  768. #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
  769. fc_starget_show_function(field, format_string, sz, (cast)) \
  770. static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
  771. show_fc_starget_##field, NULL)
  772. #define SETUP_STARGET_ATTRIBUTE_RD(field) \
  773. i->private_starget_attrs[count] = class_device_attr_starget_##field; \
  774. i->private_starget_attrs[count].attr.mode = S_IRUGO; \
  775. i->private_starget_attrs[count].store = NULL; \
  776. i->starget_attrs[count] = &i->private_starget_attrs[count]; \
  777. if (i->f->show_starget_##field) \
  778. count++
  779. #define SETUP_STARGET_ATTRIBUTE_RW(field) \
  780. i->private_starget_attrs[count] = class_device_attr_starget_##field; \
  781. if (!i->f->set_starget_##field) { \
  782. i->private_starget_attrs[count].attr.mode = S_IRUGO; \
  783. i->private_starget_attrs[count].store = NULL; \
  784. } \
  785. i->starget_attrs[count] = &i->private_starget_attrs[count]; \
  786. if (i->f->show_starget_##field) \
  787. count++
  788. /* The FC Transport SCSI Target Attributes: */
  789. fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
  790. fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
  791. fc_starget_rd_attr(port_id, "0x%06x\n", 20);
  792. /*
  793. * Host Attribute Management
  794. */
  795. #define fc_host_show_function(field, format_string, sz, cast) \
  796. static ssize_t \
  797. show_fc_host_##field (struct class_device *cdev, char *buf) \
  798. { \
  799. struct Scsi_Host *shost = transport_class_to_shost(cdev); \
  800. struct fc_internal *i = to_fc_internal(shost->transportt); \
  801. if (i->f->get_host_##field) \
  802. i->f->get_host_##field(shost); \
  803. return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
  804. }
  805. #define fc_host_store_function(field) \
  806. static ssize_t \
  807. store_fc_host_##field(struct class_device *cdev, const char *buf, \
  808. size_t count) \
  809. { \
  810. int val; \
  811. struct Scsi_Host *shost = transport_class_to_shost(cdev); \
  812. struct fc_internal *i = to_fc_internal(shost->transportt); \
  813. char *cp; \
  814. \
  815. val = simple_strtoul(buf, &cp, 0); \
  816. if (*cp && (*cp != '\n')) \
  817. return -EINVAL; \
  818. i->f->set_host_##field(shost, val); \
  819. return count; \
  820. }
  821. #define fc_host_store_str_function(field, slen) \
  822. static ssize_t \
  823. store_fc_host_##field(struct class_device *cdev, const char *buf, \
  824. size_t count) \
  825. { \
  826. struct Scsi_Host *shost = transport_class_to_shost(cdev); \
  827. struct fc_internal *i = to_fc_internal(shost->transportt); \
  828. unsigned int cnt=count; \
  829. \
  830. /* count may include a LF at end of string */ \
  831. if (buf[cnt-1] == '\n') \
  832. cnt--; \
  833. if (cnt > ((slen) - 1)) \
  834. return -EINVAL; \
  835. memcpy(fc_host_##field(shost), buf, cnt); \
  836. i->f->set_host_##field(shost); \
  837. return count; \
  838. }
  839. #define fc_host_rd_attr(field, format_string, sz) \
  840. fc_host_show_function(field, format_string, sz, ) \
  841. static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
  842. show_fc_host_##field, NULL)
  843. #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
  844. fc_host_show_function(field, format_string, sz, (cast)) \
  845. static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
  846. show_fc_host_##field, NULL)
  847. #define fc_host_rw_attr(field, format_string, sz) \
  848. fc_host_show_function(field, format_string, sz, ) \
  849. fc_host_store_function(field) \
  850. static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
  851. show_fc_host_##field, \
  852. store_fc_host_##field)
  853. #define fc_host_rd_enum_attr(title, maxlen) \
  854. static ssize_t \
  855. show_fc_host_##title (struct class_device *cdev, char *buf) \
  856. { \
  857. struct Scsi_Host *shost = transport_class_to_shost(cdev); \
  858. struct fc_internal *i = to_fc_internal(shost->transportt); \
  859. const char *name; \
  860. if (i->f->get_host_##title) \
  861. i->f->get_host_##title(shost); \
  862. name = get_fc_##title##_name(fc_host_##title(shost)); \
  863. if (!name) \
  864. return -EINVAL; \
  865. return snprintf(buf, maxlen, "%s\n", name); \
  866. } \
  867. static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
  868. #define SETUP_HOST_ATTRIBUTE_RD(field) \
  869. i->private_host_attrs[count] = class_device_attr_host_##field; \
  870. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  871. i->private_host_attrs[count].store = NULL; \
  872. i->host_attrs[count] = &i->private_host_attrs[count]; \
  873. if (i->f->show_host_##field) \
  874. count++
  875. #define SETUP_HOST_ATTRIBUTE_RW(field) \
  876. i->private_host_attrs[count] = class_device_attr_host_##field; \
  877. if (!i->f->set_host_##field) { \
  878. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  879. i->private_host_attrs[count].store = NULL; \
  880. } \
  881. i->host_attrs[count] = &i->private_host_attrs[count]; \
  882. if (i->f->show_host_##field) \
  883. count++
  884. #define fc_private_host_show_function(field, format_string, sz, cast) \
  885. static ssize_t \
  886. show_fc_host_##field (struct class_device *cdev, char *buf) \
  887. { \
  888. struct Scsi_Host *shost = transport_class_to_shost(cdev); \
  889. return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
  890. }
  891. #define fc_private_host_rd_attr(field, format_string, sz) \
  892. fc_private_host_show_function(field, format_string, sz, ) \
  893. static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
  894. show_fc_host_##field, NULL)
  895. #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
  896. fc_private_host_show_function(field, format_string, sz, (cast)) \
  897. static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
  898. show_fc_host_##field, NULL)
  899. #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
  900. i->private_host_attrs[count] = class_device_attr_host_##field; \
  901. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  902. i->private_host_attrs[count].store = NULL; \
  903. i->host_attrs[count] = &i->private_host_attrs[count]; \
  904. count++
  905. #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
  906. { \
  907. i->private_host_attrs[count] = class_device_attr_host_##field; \
  908. i->host_attrs[count] = &i->private_host_attrs[count]; \
  909. count++; \
  910. }
  911. /* Fixed Host Attributes */
  912. static ssize_t
  913. show_fc_host_supported_classes (struct class_device *cdev, char *buf)
  914. {
  915. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  916. if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
  917. return snprintf(buf, 20, "unspecified\n");
  918. return get_fc_cos_names(fc_host_supported_classes(shost), buf);
  919. }
  920. static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
  921. show_fc_host_supported_classes, NULL);
  922. static ssize_t
  923. show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
  924. {
  925. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  926. return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
  927. }
  928. static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
  929. show_fc_host_supported_fc4s, NULL);
  930. static ssize_t
  931. show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
  932. {
  933. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  934. if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
  935. return snprintf(buf, 20, "unknown\n");
  936. return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
  937. }
  938. static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
  939. show_fc_host_supported_speeds, NULL);
  940. fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
  941. fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
  942. fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
  943. unsigned long long);
  944. fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
  945. fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
  946. /* Dynamic Host Attributes */
  947. static ssize_t
  948. show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
  949. {
  950. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  951. struct fc_internal *i = to_fc_internal(shost->transportt);
  952. if (i->f->get_host_active_fc4s)
  953. i->f->get_host_active_fc4s(shost);
  954. return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
  955. }
  956. static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
  957. show_fc_host_active_fc4s, NULL);
  958. static ssize_t
  959. show_fc_host_speed (struct class_device *cdev, char *buf)
  960. {
  961. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  962. struct fc_internal *i = to_fc_internal(shost->transportt);
  963. if (i->f->get_host_speed)
  964. i->f->get_host_speed(shost);
  965. if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
  966. return snprintf(buf, 20, "unknown\n");
  967. return get_fc_port_speed_names(fc_host_speed(shost), buf);
  968. }
  969. static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
  970. show_fc_host_speed, NULL);
  971. fc_host_rd_attr(port_id, "0x%06x\n", 20);
  972. fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
  973. fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
  974. fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
  975. fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
  976. fc_private_host_show_function(system_hostname, "%s\n",
  977. FC_SYMBOLIC_NAME_SIZE + 1, )
  978. fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
  979. static FC_CLASS_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
  980. show_fc_host_system_hostname, store_fc_host_system_hostname);
  981. /* Private Host Attributes */
  982. static ssize_t
  983. show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
  984. {
  985. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  986. const char *name;
  987. name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
  988. if (!name)
  989. return -EINVAL;
  990. return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
  991. }
  992. #define get_list_head_entry(pos, head, member) \
  993. pos = list_entry((head)->next, typeof(*pos), member)
  994. static ssize_t
  995. store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
  996. const char *buf, size_t count)
  997. {
  998. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  999. struct fc_rport *rport;
  1000. enum fc_tgtid_binding_type val;
  1001. unsigned long flags;
  1002. if (get_fc_tgtid_bind_type_match(buf, &val))
  1003. return -EINVAL;
  1004. /* if changing bind type, purge all unused consistent bindings */
  1005. if (val != fc_host_tgtid_bind_type(shost)) {
  1006. spin_lock_irqsave(shost->host_lock, flags);
  1007. while (!list_empty(&fc_host_rport_bindings(shost))) {
  1008. get_list_head_entry(rport,
  1009. &fc_host_rport_bindings(shost), peers);
  1010. list_del(&rport->peers);
  1011. rport->port_state = FC_PORTSTATE_DELETED;
  1012. fc_queue_work(shost, &rport->rport_delete_work);
  1013. }
  1014. spin_unlock_irqrestore(shost->host_lock, flags);
  1015. }
  1016. fc_host_tgtid_bind_type(shost) = val;
  1017. return count;
  1018. }
  1019. static FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
  1020. show_fc_private_host_tgtid_bind_type,
  1021. store_fc_private_host_tgtid_bind_type);
  1022. static ssize_t
  1023. store_fc_private_host_issue_lip(struct class_device *cdev,
  1024. const char *buf, size_t count)
  1025. {
  1026. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1027. struct fc_internal *i = to_fc_internal(shost->transportt);
  1028. int ret;
  1029. /* ignore any data value written to the attribute */
  1030. if (i->f->issue_fc_host_lip) {
  1031. ret = i->f->issue_fc_host_lip(shost);
  1032. return ret ? ret: count;
  1033. }
  1034. return -ENOENT;
  1035. }
  1036. static FC_CLASS_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
  1037. store_fc_private_host_issue_lip);
  1038. /*
  1039. * Host Statistics Management
  1040. */
  1041. /* Show a given an attribute in the statistics group */
  1042. static ssize_t
  1043. fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
  1044. {
  1045. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1046. struct fc_internal *i = to_fc_internal(shost->transportt);
  1047. struct fc_host_statistics *stats;
  1048. ssize_t ret = -ENOENT;
  1049. if (offset > sizeof(struct fc_host_statistics) ||
  1050. offset % sizeof(u64) != 0)
  1051. WARN_ON(1);
  1052. if (i->f->get_fc_host_stats) {
  1053. stats = (i->f->get_fc_host_stats)(shost);
  1054. if (stats)
  1055. ret = snprintf(buf, 20, "0x%llx\n",
  1056. (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
  1057. }
  1058. return ret;
  1059. }
  1060. /* generate a read-only statistics attribute */
  1061. #define fc_host_statistic(name) \
  1062. static ssize_t show_fcstat_##name(struct class_device *cd, char *buf) \
  1063. { \
  1064. return fc_stat_show(cd, buf, \
  1065. offsetof(struct fc_host_statistics, name)); \
  1066. } \
  1067. static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
  1068. fc_host_statistic(seconds_since_last_reset);
  1069. fc_host_statistic(tx_frames);
  1070. fc_host_statistic(tx_words);
  1071. fc_host_statistic(rx_frames);
  1072. fc_host_statistic(rx_words);
  1073. fc_host_statistic(lip_count);
  1074. fc_host_statistic(nos_count);
  1075. fc_host_statistic(error_frames);
  1076. fc_host_statistic(dumped_frames);
  1077. fc_host_statistic(link_failure_count);
  1078. fc_host_statistic(loss_of_sync_count);
  1079. fc_host_statistic(loss_of_signal_count);
  1080. fc_host_statistic(prim_seq_protocol_err_count);
  1081. fc_host_statistic(invalid_tx_word_count);
  1082. fc_host_statistic(invalid_crc_count);
  1083. fc_host_statistic(fcp_input_requests);
  1084. fc_host_statistic(fcp_output_requests);
  1085. fc_host_statistic(fcp_control_requests);
  1086. fc_host_statistic(fcp_input_megabytes);
  1087. fc_host_statistic(fcp_output_megabytes);
  1088. static ssize_t
  1089. fc_reset_statistics(struct class_device *cdev, const char *buf,
  1090. size_t count)
  1091. {
  1092. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1093. struct fc_internal *i = to_fc_internal(shost->transportt);
  1094. /* ignore any data value written to the attribute */
  1095. if (i->f->reset_fc_host_stats) {
  1096. i->f->reset_fc_host_stats(shost);
  1097. return count;
  1098. }
  1099. return -ENOENT;
  1100. }
  1101. static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
  1102. fc_reset_statistics);
  1103. static struct attribute *fc_statistics_attrs[] = {
  1104. &class_device_attr_host_seconds_since_last_reset.attr,
  1105. &class_device_attr_host_tx_frames.attr,
  1106. &class_device_attr_host_tx_words.attr,
  1107. &class_device_attr_host_rx_frames.attr,
  1108. &class_device_attr_host_rx_words.attr,
  1109. &class_device_attr_host_lip_count.attr,
  1110. &class_device_attr_host_nos_count.attr,
  1111. &class_device_attr_host_error_frames.attr,
  1112. &class_device_attr_host_dumped_frames.attr,
  1113. &class_device_attr_host_link_failure_count.attr,
  1114. &class_device_attr_host_loss_of_sync_count.attr,
  1115. &class_device_attr_host_loss_of_signal_count.attr,
  1116. &class_device_attr_host_prim_seq_protocol_err_count.attr,
  1117. &class_device_attr_host_invalid_tx_word_count.attr,
  1118. &class_device_attr_host_invalid_crc_count.attr,
  1119. &class_device_attr_host_fcp_input_requests.attr,
  1120. &class_device_attr_host_fcp_output_requests.attr,
  1121. &class_device_attr_host_fcp_control_requests.attr,
  1122. &class_device_attr_host_fcp_input_megabytes.attr,
  1123. &class_device_attr_host_fcp_output_megabytes.attr,
  1124. &class_device_attr_host_reset_statistics.attr,
  1125. NULL
  1126. };
  1127. static struct attribute_group fc_statistics_group = {
  1128. .name = "statistics",
  1129. .attrs = fc_statistics_attrs,
  1130. };
  1131. static int fc_host_match(struct attribute_container *cont,
  1132. struct device *dev)
  1133. {
  1134. struct Scsi_Host *shost;
  1135. struct fc_internal *i;
  1136. if (!scsi_is_host_device(dev))
  1137. return 0;
  1138. shost = dev_to_shost(dev);
  1139. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1140. != &fc_host_class.class)
  1141. return 0;
  1142. i = to_fc_internal(shost->transportt);
  1143. return &i->t.host_attrs.ac == cont;
  1144. }
  1145. static int fc_target_match(struct attribute_container *cont,
  1146. struct device *dev)
  1147. {
  1148. struct Scsi_Host *shost;
  1149. struct fc_internal *i;
  1150. if (!scsi_is_target_device(dev))
  1151. return 0;
  1152. shost = dev_to_shost(dev->parent);
  1153. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1154. != &fc_host_class.class)
  1155. return 0;
  1156. i = to_fc_internal(shost->transportt);
  1157. return &i->t.target_attrs.ac == cont;
  1158. }
  1159. static void fc_rport_dev_release(struct device *dev)
  1160. {
  1161. struct fc_rport *rport = dev_to_rport(dev);
  1162. put_device(dev->parent);
  1163. kfree(rport);
  1164. }
  1165. int scsi_is_fc_rport(const struct device *dev)
  1166. {
  1167. return dev->release == fc_rport_dev_release;
  1168. }
  1169. EXPORT_SYMBOL(scsi_is_fc_rport);
  1170. static int fc_rport_match(struct attribute_container *cont,
  1171. struct device *dev)
  1172. {
  1173. struct Scsi_Host *shost;
  1174. struct fc_internal *i;
  1175. if (!scsi_is_fc_rport(dev))
  1176. return 0;
  1177. shost = dev_to_shost(dev->parent);
  1178. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1179. != &fc_host_class.class)
  1180. return 0;
  1181. i = to_fc_internal(shost->transportt);
  1182. return &i->rport_attr_cont.ac == cont;
  1183. }
  1184. /**
  1185. * fc_timed_out - FC Transport I/O timeout intercept handler
  1186. *
  1187. * @scmd: The SCSI command which timed out
  1188. *
  1189. * This routine protects against error handlers getting invoked while a
  1190. * rport is in a blocked state, typically due to a temporarily loss of
  1191. * connectivity. If the error handlers are allowed to proceed, requests
  1192. * to abort i/o, reset the target, etc will likely fail as there is no way
  1193. * to communicate with the device to perform the requested function. These
  1194. * failures may result in the midlayer taking the device offline, requiring
  1195. * manual intervention to restore operation.
  1196. *
  1197. * This routine, called whenever an i/o times out, validates the state of
  1198. * the underlying rport. If the rport is blocked, it returns
  1199. * EH_RESET_TIMER, which will continue to reschedule the timeout.
  1200. * Eventually, either the device will return, or devloss_tmo will fire,
  1201. * and when the timeout then fires, it will be handled normally.
  1202. * If the rport is not blocked, normal error handling continues.
  1203. *
  1204. * Notes:
  1205. * This routine assumes no locks are held on entry.
  1206. **/
  1207. static enum scsi_eh_timer_return
  1208. fc_timed_out(struct scsi_cmnd *scmd)
  1209. {
  1210. struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
  1211. if (rport->port_state == FC_PORTSTATE_BLOCKED)
  1212. return EH_RESET_TIMER;
  1213. return EH_NOT_HANDLED;
  1214. }
  1215. /*
  1216. * Must be called with shost->host_lock held
  1217. */
  1218. static int fc_user_scan(struct Scsi_Host *shost, uint channel,
  1219. uint id, uint lun)
  1220. {
  1221. struct fc_rport *rport;
  1222. list_for_each_entry(rport, &fc_host_rports(shost), peers) {
  1223. if (rport->scsi_target_id == -1)
  1224. continue;
  1225. if ((channel == SCAN_WILD_CARD || channel == rport->channel) &&
  1226. (id == SCAN_WILD_CARD || id == rport->scsi_target_id)) {
  1227. scsi_scan_target(&rport->dev, rport->channel,
  1228. rport->scsi_target_id, lun, 1);
  1229. }
  1230. }
  1231. return 0;
  1232. }
  1233. struct scsi_transport_template *
  1234. fc_attach_transport(struct fc_function_template *ft)
  1235. {
  1236. int count;
  1237. struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
  1238. GFP_KERNEL);
  1239. if (unlikely(!i))
  1240. return NULL;
  1241. i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
  1242. i->t.target_attrs.ac.class = &fc_transport_class.class;
  1243. i->t.target_attrs.ac.match = fc_target_match;
  1244. i->t.target_size = sizeof(struct fc_starget_attrs);
  1245. transport_container_register(&i->t.target_attrs);
  1246. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  1247. i->t.host_attrs.ac.class = &fc_host_class.class;
  1248. i->t.host_attrs.ac.match = fc_host_match;
  1249. i->t.host_size = sizeof(struct fc_host_attrs);
  1250. if (ft->get_fc_host_stats)
  1251. i->t.host_attrs.statistics = &fc_statistics_group;
  1252. transport_container_register(&i->t.host_attrs);
  1253. i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
  1254. i->rport_attr_cont.ac.class = &fc_rport_class.class;
  1255. i->rport_attr_cont.ac.match = fc_rport_match;
  1256. transport_container_register(&i->rport_attr_cont);
  1257. i->f = ft;
  1258. /* Transport uses the shost workq for scsi scanning */
  1259. i->t.create_work_queue = 1;
  1260. i->t.eh_timed_out = fc_timed_out;
  1261. i->t.user_scan = fc_user_scan;
  1262. /*
  1263. * Setup SCSI Target Attributes.
  1264. */
  1265. count = 0;
  1266. SETUP_STARGET_ATTRIBUTE_RD(node_name);
  1267. SETUP_STARGET_ATTRIBUTE_RD(port_name);
  1268. SETUP_STARGET_ATTRIBUTE_RD(port_id);
  1269. BUG_ON(count > FC_STARGET_NUM_ATTRS);
  1270. i->starget_attrs[count] = NULL;
  1271. /*
  1272. * Setup SCSI Host Attributes.
  1273. */
  1274. count=0;
  1275. SETUP_HOST_ATTRIBUTE_RD(node_name);
  1276. SETUP_HOST_ATTRIBUTE_RD(port_name);
  1277. SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
  1278. SETUP_HOST_ATTRIBUTE_RD(supported_classes);
  1279. SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
  1280. SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
  1281. SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
  1282. SETUP_HOST_ATTRIBUTE_RD(serial_number);
  1283. SETUP_HOST_ATTRIBUTE_RD(port_id);
  1284. SETUP_HOST_ATTRIBUTE_RD(port_type);
  1285. SETUP_HOST_ATTRIBUTE_RD(port_state);
  1286. SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
  1287. SETUP_HOST_ATTRIBUTE_RD(speed);
  1288. SETUP_HOST_ATTRIBUTE_RD(fabric_name);
  1289. SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
  1290. SETUP_HOST_ATTRIBUTE_RW(system_hostname);
  1291. /* Transport-managed attributes */
  1292. SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
  1293. if (ft->issue_fc_host_lip)
  1294. SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
  1295. BUG_ON(count > FC_HOST_NUM_ATTRS);
  1296. i->host_attrs[count] = NULL;
  1297. /*
  1298. * Setup Remote Port Attributes.
  1299. */
  1300. count=0;
  1301. SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
  1302. SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
  1303. SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
  1304. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
  1305. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
  1306. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
  1307. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
  1308. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
  1309. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
  1310. if (ft->terminate_rport_io)
  1311. SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
  1312. BUG_ON(count > FC_RPORT_NUM_ATTRS);
  1313. i->rport_attrs[count] = NULL;
  1314. return &i->t;
  1315. }
  1316. EXPORT_SYMBOL(fc_attach_transport);
  1317. void fc_release_transport(struct scsi_transport_template *t)
  1318. {
  1319. struct fc_internal *i = to_fc_internal(t);
  1320. transport_container_unregister(&i->t.target_attrs);
  1321. transport_container_unregister(&i->t.host_attrs);
  1322. transport_container_unregister(&i->rport_attr_cont);
  1323. kfree(i);
  1324. }
  1325. EXPORT_SYMBOL(fc_release_transport);
  1326. /**
  1327. * fc_queue_work - Queue work to the fc_host workqueue.
  1328. * @shost: Pointer to Scsi_Host bound to fc_host.
  1329. * @work: Work to queue for execution.
  1330. *
  1331. * Return value:
  1332. * 1 - work queued for execution
  1333. * 0 - work is already queued
  1334. * -EINVAL - work queue doesn't exist
  1335. **/
  1336. static int
  1337. fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
  1338. {
  1339. if (unlikely(!fc_host_work_q(shost))) {
  1340. printk(KERN_ERR
  1341. "ERROR: FC host '%s' attempted to queue work, "
  1342. "when no workqueue created.\n", shost->hostt->name);
  1343. dump_stack();
  1344. return -EINVAL;
  1345. }
  1346. return queue_work(fc_host_work_q(shost), work);
  1347. }
  1348. /**
  1349. * fc_flush_work - Flush a fc_host's workqueue.
  1350. * @shost: Pointer to Scsi_Host bound to fc_host.
  1351. **/
  1352. static void
  1353. fc_flush_work(struct Scsi_Host *shost)
  1354. {
  1355. if (!fc_host_work_q(shost)) {
  1356. printk(KERN_ERR
  1357. "ERROR: FC host '%s' attempted to flush work, "
  1358. "when no workqueue created.\n", shost->hostt->name);
  1359. dump_stack();
  1360. return;
  1361. }
  1362. flush_workqueue(fc_host_work_q(shost));
  1363. }
  1364. /**
  1365. * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
  1366. * @shost: Pointer to Scsi_Host bound to fc_host.
  1367. * @work: Work to queue for execution.
  1368. * @delay: jiffies to delay the work queuing
  1369. *
  1370. * Return value:
  1371. * 1 on success / 0 already queued / < 0 for error
  1372. **/
  1373. static int
  1374. fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
  1375. unsigned long delay)
  1376. {
  1377. if (unlikely(!fc_host_devloss_work_q(shost))) {
  1378. printk(KERN_ERR
  1379. "ERROR: FC host '%s' attempted to queue work, "
  1380. "when no workqueue created.\n", shost->hostt->name);
  1381. dump_stack();
  1382. return -EINVAL;
  1383. }
  1384. return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
  1385. }
  1386. /**
  1387. * fc_flush_devloss - Flush a fc_host's devloss workqueue.
  1388. * @shost: Pointer to Scsi_Host bound to fc_host.
  1389. **/
  1390. static void
  1391. fc_flush_devloss(struct Scsi_Host *shost)
  1392. {
  1393. if (!fc_host_devloss_work_q(shost)) {
  1394. printk(KERN_ERR
  1395. "ERROR: FC host '%s' attempted to flush work, "
  1396. "when no workqueue created.\n", shost->hostt->name);
  1397. dump_stack();
  1398. return;
  1399. }
  1400. flush_workqueue(fc_host_devloss_work_q(shost));
  1401. }
  1402. /**
  1403. * fc_remove_host - called to terminate any fc_transport-related elements
  1404. * for a scsi host.
  1405. * @rport: remote port to be unblocked.
  1406. *
  1407. * This routine is expected to be called immediately preceeding the
  1408. * a driver's call to scsi_remove_host().
  1409. *
  1410. * WARNING: A driver utilizing the fc_transport, which fails to call
  1411. * this routine prior to scsi_remote_host(), will leave dangling
  1412. * objects in /sys/class/fc_remote_ports. Access to any of these
  1413. * objects can result in a system crash !!!
  1414. *
  1415. * Notes:
  1416. * This routine assumes no locks are held on entry.
  1417. **/
  1418. void
  1419. fc_remove_host(struct Scsi_Host *shost)
  1420. {
  1421. struct fc_rport *rport, *next_rport;
  1422. struct workqueue_struct *work_q;
  1423. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  1424. /* Remove any remote ports */
  1425. list_for_each_entry_safe(rport, next_rport,
  1426. &fc_host->rports, peers) {
  1427. list_del(&rport->peers);
  1428. rport->port_state = FC_PORTSTATE_DELETED;
  1429. fc_queue_work(shost, &rport->rport_delete_work);
  1430. }
  1431. list_for_each_entry_safe(rport, next_rport,
  1432. &fc_host->rport_bindings, peers) {
  1433. list_del(&rport->peers);
  1434. rport->port_state = FC_PORTSTATE_DELETED;
  1435. fc_queue_work(shost, &rport->rport_delete_work);
  1436. }
  1437. /* flush all scan work items */
  1438. scsi_flush_work(shost);
  1439. /* flush all stgt delete, and rport delete work items, then kill it */
  1440. if (fc_host->work_q) {
  1441. work_q = fc_host->work_q;
  1442. fc_host->work_q = NULL;
  1443. destroy_workqueue(work_q);
  1444. }
  1445. /* flush all devloss work items, then kill it */
  1446. if (fc_host->devloss_work_q) {
  1447. work_q = fc_host->devloss_work_q;
  1448. fc_host->devloss_work_q = NULL;
  1449. destroy_workqueue(work_q);
  1450. }
  1451. }
  1452. EXPORT_SYMBOL(fc_remove_host);
  1453. /**
  1454. * fc_starget_delete - called to delete the scsi decendents of an rport
  1455. * (target and all sdevs)
  1456. *
  1457. * @work: remote port to be operated on.
  1458. **/
  1459. static void
  1460. fc_starget_delete(struct work_struct *work)
  1461. {
  1462. struct fc_rport *rport =
  1463. container_of(work, struct fc_rport, stgt_delete_work);
  1464. struct Scsi_Host *shost = rport_to_shost(rport);
  1465. struct fc_internal *i = to_fc_internal(shost->transportt);
  1466. /* Involve the LLDD if possible to terminate all io on the rport. */
  1467. if (i->f->terminate_rport_io)
  1468. i->f->terminate_rport_io(rport);
  1469. scsi_remove_target(&rport->dev);
  1470. }
  1471. /**
  1472. * fc_rport_final_delete - finish rport termination and delete it.
  1473. *
  1474. * @work: remote port to be deleted.
  1475. **/
  1476. static void
  1477. fc_rport_final_delete(struct work_struct *work)
  1478. {
  1479. struct fc_rport *rport =
  1480. container_of(work, struct fc_rport, rport_delete_work);
  1481. struct device *dev = &rport->dev;
  1482. struct Scsi_Host *shost = rport_to_shost(rport);
  1483. struct fc_internal *i = to_fc_internal(shost->transportt);
  1484. unsigned long flags;
  1485. /*
  1486. * if a scan is pending, flush the SCSI Host work_q so that
  1487. * that we can reclaim the rport scan work element.
  1488. */
  1489. if (rport->flags & FC_RPORT_SCAN_PENDING)
  1490. scsi_flush_work(shost);
  1491. /* involve the LLDD to terminate all pending i/o */
  1492. if (i->f->terminate_rport_io)
  1493. i->f->terminate_rport_io(rport);
  1494. /*
  1495. * Cancel any outstanding timers. These should really exist
  1496. * only when rmmod'ing the LLDD and we're asking for
  1497. * immediate termination of the rports
  1498. */
  1499. spin_lock_irqsave(shost->host_lock, flags);
  1500. if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
  1501. spin_unlock_irqrestore(shost->host_lock, flags);
  1502. if (!cancel_delayed_work(&rport->fail_io_work))
  1503. fc_flush_devloss(shost);
  1504. if (!cancel_delayed_work(&rport->dev_loss_work))
  1505. fc_flush_devloss(shost);
  1506. spin_lock_irqsave(shost->host_lock, flags);
  1507. rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
  1508. }
  1509. spin_unlock_irqrestore(shost->host_lock, flags);
  1510. /* Delete SCSI target and sdevs */
  1511. if (rport->scsi_target_id != -1)
  1512. fc_starget_delete(&rport->stgt_delete_work);
  1513. /*
  1514. * Notify the driver that the rport is now dead. The LLDD will
  1515. * also guarantee that any communication to the rport is terminated
  1516. */
  1517. if (i->f->dev_loss_tmo_callbk)
  1518. i->f->dev_loss_tmo_callbk(rport);
  1519. transport_remove_device(dev);
  1520. device_del(dev);
  1521. transport_destroy_device(dev);
  1522. put_device(&shost->shost_gendev); /* for fc_host->rport list */
  1523. put_device(dev); /* for self-reference */
  1524. }
  1525. /**
  1526. * fc_rport_create - allocates and creates a remote FC port.
  1527. * @shost: scsi host the remote port is connected to.
  1528. * @channel: Channel on shost port connected to.
  1529. * @ids: The world wide names, fc address, and FC4 port
  1530. * roles for the remote port.
  1531. *
  1532. * Allocates and creates the remoter port structure, including the
  1533. * class and sysfs creation.
  1534. *
  1535. * Notes:
  1536. * This routine assumes no locks are held on entry.
  1537. **/
  1538. struct fc_rport *
  1539. fc_rport_create(struct Scsi_Host *shost, int channel,
  1540. struct fc_rport_identifiers *ids)
  1541. {
  1542. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  1543. struct fc_internal *fci = to_fc_internal(shost->transportt);
  1544. struct fc_rport *rport;
  1545. struct device *dev;
  1546. unsigned long flags;
  1547. int error;
  1548. size_t size;
  1549. size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
  1550. rport = kzalloc(size, GFP_KERNEL);
  1551. if (unlikely(!rport)) {
  1552. printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
  1553. return NULL;
  1554. }
  1555. rport->maxframe_size = -1;
  1556. rport->supported_classes = FC_COS_UNSPECIFIED;
  1557. rport->dev_loss_tmo = fc_dev_loss_tmo;
  1558. memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
  1559. memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
  1560. rport->port_id = ids->port_id;
  1561. rport->roles = ids->roles;
  1562. rport->port_state = FC_PORTSTATE_ONLINE;
  1563. if (fci->f->dd_fcrport_size)
  1564. rport->dd_data = &rport[1];
  1565. rport->channel = channel;
  1566. rport->fast_io_fail_tmo = -1;
  1567. INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
  1568. INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
  1569. INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
  1570. INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
  1571. INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
  1572. spin_lock_irqsave(shost->host_lock, flags);
  1573. rport->number = fc_host->next_rport_number++;
  1574. if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
  1575. rport->scsi_target_id = fc_host->next_target_id++;
  1576. else
  1577. rport->scsi_target_id = -1;
  1578. list_add_tail(&rport->peers, &fc_host->rports);
  1579. get_device(&shost->shost_gendev); /* for fc_host->rport list */
  1580. spin_unlock_irqrestore(shost->host_lock, flags);
  1581. dev = &rport->dev;
  1582. device_initialize(dev); /* takes self reference */
  1583. dev->parent = get_device(&shost->shost_gendev); /* parent reference */
  1584. dev->release = fc_rport_dev_release;
  1585. sprintf(dev->bus_id, "rport-%d:%d-%d",
  1586. shost->host_no, channel, rport->number);
  1587. transport_setup_device(dev);
  1588. error = device_add(dev);
  1589. if (error) {
  1590. printk(KERN_ERR "FC Remote Port device_add failed\n");
  1591. goto delete_rport;
  1592. }
  1593. transport_add_device(dev);
  1594. transport_configure_device(dev);
  1595. if (rport->roles & FC_RPORT_ROLE_FCP_TARGET) {
  1596. /* initiate a scan of the target */
  1597. rport->flags |= FC_RPORT_SCAN_PENDING;
  1598. scsi_queue_work(shost, &rport->scan_work);
  1599. }
  1600. return rport;
  1601. delete_rport:
  1602. transport_destroy_device(dev);
  1603. spin_lock_irqsave(shost->host_lock, flags);
  1604. list_del(&rport->peers);
  1605. put_device(&shost->shost_gendev); /* for fc_host->rport list */
  1606. spin_unlock_irqrestore(shost->host_lock, flags);
  1607. put_device(dev->parent);
  1608. kfree(rport);
  1609. return NULL;
  1610. }
  1611. /**
  1612. * fc_remote_port_add - notifies the fc transport of the existence
  1613. * of a remote FC port.
  1614. * @shost: scsi host the remote port is connected to.
  1615. * @channel: Channel on shost port connected to.
  1616. * @ids: The world wide names, fc address, and FC4 port
  1617. * roles for the remote port.
  1618. *
  1619. * The LLDD calls this routine to notify the transport of the existence
  1620. * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
  1621. * of the port, it's FC address (port_id), and the FC4 roles that are
  1622. * active for the port.
  1623. *
  1624. * For ports that are FCP targets (aka scsi targets), the FC transport
  1625. * maintains consistent target id bindings on behalf of the LLDD.
  1626. * A consistent target id binding is an assignment of a target id to
  1627. * a remote port identifier, which persists while the scsi host is
  1628. * attached. The remote port can disappear, then later reappear, and
  1629. * it's target id assignment remains the same. This allows for shifts
  1630. * in FC addressing (if binding by wwpn or wwnn) with no apparent
  1631. * changes to the scsi subsystem which is based on scsi host number and
  1632. * target id values. Bindings are only valid during the attachment of
  1633. * the scsi host. If the host detaches, then later re-attaches, target
  1634. * id bindings may change.
  1635. *
  1636. * This routine is responsible for returning a remote port structure.
  1637. * The routine will search the list of remote ports it maintains
  1638. * internally on behalf of consistent target id mappings. If found, the
  1639. * remote port structure will be reused. Otherwise, a new remote port
  1640. * structure will be allocated.
  1641. *
  1642. * Whenever a remote port is allocated, a new fc_remote_port class
  1643. * device is created.
  1644. *
  1645. * Should not be called from interrupt context.
  1646. *
  1647. * Notes:
  1648. * This routine assumes no locks are held on entry.
  1649. **/
  1650. struct fc_rport *
  1651. fc_remote_port_add(struct Scsi_Host *shost, int channel,
  1652. struct fc_rport_identifiers *ids)
  1653. {
  1654. struct fc_internal *fci = to_fc_internal(shost->transportt);
  1655. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  1656. struct fc_rport *rport;
  1657. unsigned long flags;
  1658. int match = 0;
  1659. /* ensure any stgt delete functions are done */
  1660. fc_flush_work(shost);
  1661. /*
  1662. * Search the list of "active" rports, for an rport that has been
  1663. * deleted, but we've held off the real delete while the target
  1664. * is in a "blocked" state.
  1665. */
  1666. spin_lock_irqsave(shost->host_lock, flags);
  1667. list_for_each_entry(rport, &fc_host->rports, peers) {
  1668. if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
  1669. (rport->channel == channel)) {
  1670. switch (fc_host->tgtid_bind_type) {
  1671. case FC_TGTID_BIND_BY_WWPN:
  1672. case FC_TGTID_BIND_NONE:
  1673. if (rport->port_name == ids->port_name)
  1674. match = 1;
  1675. break;
  1676. case FC_TGTID_BIND_BY_WWNN:
  1677. if (rport->node_name == ids->node_name)
  1678. match = 1;
  1679. break;
  1680. case FC_TGTID_BIND_BY_ID:
  1681. if (rport->port_id == ids->port_id)
  1682. match = 1;
  1683. break;
  1684. }
  1685. if (match) {
  1686. memcpy(&rport->node_name, &ids->node_name,
  1687. sizeof(rport->node_name));
  1688. memcpy(&rport->port_name, &ids->port_name,
  1689. sizeof(rport->port_name));
  1690. rport->port_id = ids->port_id;
  1691. rport->port_state = FC_PORTSTATE_ONLINE;
  1692. rport->roles = ids->roles;
  1693. spin_unlock_irqrestore(shost->host_lock, flags);
  1694. if (fci->f->dd_fcrport_size)
  1695. memset(rport->dd_data, 0,
  1696. fci->f->dd_fcrport_size);
  1697. /*
  1698. * If we were not a target, cancel the
  1699. * io terminate and rport timers, and
  1700. * we're done.
  1701. *
  1702. * If we were a target, but our new role
  1703. * doesn't indicate a target, leave the
  1704. * timers running expecting the role to
  1705. * change as the target fully logs in. If
  1706. * it doesn't, the target will be torn down.
  1707. *
  1708. * If we were a target, and our role shows
  1709. * we're still a target, cancel the timers
  1710. * and kick off a scan.
  1711. */
  1712. /* was a target, not in roles */
  1713. if ((rport->scsi_target_id != -1) &&
  1714. (!(ids->roles & FC_RPORT_ROLE_FCP_TARGET)))
  1715. return rport;
  1716. /*
  1717. * Stop the fail io and dev_loss timers.
  1718. * If they flush, the port_state will
  1719. * be checked and will NOOP the function.
  1720. */
  1721. if (!cancel_delayed_work(&rport->fail_io_work))
  1722. fc_flush_devloss(shost);
  1723. if (!cancel_delayed_work(&rport->dev_loss_work))
  1724. fc_flush_devloss(shost);
  1725. spin_lock_irqsave(shost->host_lock, flags);
  1726. rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
  1727. /* if target, initiate a scan */
  1728. if (rport->scsi_target_id != -1) {
  1729. rport->flags |= FC_RPORT_SCAN_PENDING;
  1730. scsi_queue_work(shost,
  1731. &rport->scan_work);
  1732. spin_unlock_irqrestore(shost->host_lock,
  1733. flags);
  1734. scsi_target_unblock(&rport->dev);
  1735. } else
  1736. spin_unlock_irqrestore(shost->host_lock,
  1737. flags);
  1738. return rport;
  1739. }
  1740. }
  1741. }
  1742. /*
  1743. * Search the bindings array
  1744. * Note: if never a FCP target, you won't be on this list
  1745. */
  1746. if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
  1747. /* search for a matching consistent binding */
  1748. list_for_each_entry(rport, &fc_host->rport_bindings,
  1749. peers) {
  1750. if (rport->channel != channel)
  1751. continue;
  1752. switch (fc_host->tgtid_bind_type) {
  1753. case FC_TGTID_BIND_BY_WWPN:
  1754. if (rport->port_name == ids->port_name)
  1755. match = 1;
  1756. break;
  1757. case FC_TGTID_BIND_BY_WWNN:
  1758. if (rport->node_name == ids->node_name)
  1759. match = 1;
  1760. break;
  1761. case FC_TGTID_BIND_BY_ID:
  1762. if (rport->port_id == ids->port_id)
  1763. match = 1;
  1764. break;
  1765. case FC_TGTID_BIND_NONE: /* to keep compiler happy */
  1766. break;
  1767. }
  1768. if (match) {
  1769. list_move_tail(&rport->peers, &fc_host->rports);
  1770. break;
  1771. }
  1772. }
  1773. if (match) {
  1774. memcpy(&rport->node_name, &ids->node_name,
  1775. sizeof(rport->node_name));
  1776. memcpy(&rport->port_name, &ids->port_name,
  1777. sizeof(rport->port_name));
  1778. rport->port_id = ids->port_id;
  1779. rport->roles = ids->roles;
  1780. rport->port_state = FC_PORTSTATE_ONLINE;
  1781. if (fci->f->dd_fcrport_size)
  1782. memset(rport->dd_data, 0,
  1783. fci->f->dd_fcrport_size);
  1784. if (rport->roles & FC_RPORT_ROLE_FCP_TARGET) {
  1785. /* initiate a scan of the target */
  1786. rport->flags |= FC_RPORT_SCAN_PENDING;
  1787. scsi_queue_work(shost, &rport->scan_work);
  1788. spin_unlock_irqrestore(shost->host_lock, flags);
  1789. scsi_target_unblock(&rport->dev);
  1790. } else
  1791. spin_unlock_irqrestore(shost->host_lock, flags);
  1792. return rport;
  1793. }
  1794. }
  1795. spin_unlock_irqrestore(shost->host_lock, flags);
  1796. /* No consistent binding found - create new remote port entry */
  1797. rport = fc_rport_create(shost, channel, ids);
  1798. return rport;
  1799. }
  1800. EXPORT_SYMBOL(fc_remote_port_add);
  1801. /**
  1802. * fc_remote_port_delete - notifies the fc transport that a remote
  1803. * port is no longer in existence.
  1804. * @rport: The remote port that no longer exists
  1805. *
  1806. * The LLDD calls this routine to notify the transport that a remote
  1807. * port is no longer part of the topology. Note: Although a port
  1808. * may no longer be part of the topology, it may persist in the remote
  1809. * ports displayed by the fc_host. We do this under 2 conditions:
  1810. * - If the port was a scsi target, we delay its deletion by "blocking" it.
  1811. * This allows the port to temporarily disappear, then reappear without
  1812. * disrupting the SCSI device tree attached to it. During the "blocked"
  1813. * period the port will still exist.
  1814. * - If the port was a scsi target and disappears for longer than we
  1815. * expect, we'll delete the port and the tear down the SCSI device tree
  1816. * attached to it. However, we want to semi-persist the target id assigned
  1817. * to that port if it eventually does exist. The port structure will
  1818. * remain (although with minimal information) so that the target id
  1819. * bindings remails.
  1820. *
  1821. * If the remote port is not an FCP Target, it will be fully torn down
  1822. * and deallocated, including the fc_remote_port class device.
  1823. *
  1824. * If the remote port is an FCP Target, the port will be placed in a
  1825. * temporary blocked state. From the LLDD's perspective, the rport no
  1826. * longer exists. From the SCSI midlayer's perspective, the SCSI target
  1827. * exists, but all sdevs on it are blocked from further I/O. The following
  1828. * is then expected:
  1829. * If the remote port does not return (signaled by a LLDD call to
  1830. * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
  1831. * scsi target is removed - killing all outstanding i/o and removing the
  1832. * scsi devices attached ot it. The port structure will be marked Not
  1833. * Present and be partially cleared, leaving only enough information to
  1834. * recognize the remote port relative to the scsi target id binding if
  1835. * it later appears. The port will remain as long as there is a valid
  1836. * binding (e.g. until the user changes the binding type or unloads the
  1837. * scsi host with the binding).
  1838. *
  1839. * If the remote port returns within the dev_loss_tmo value (and matches
  1840. * according to the target id binding type), the port structure will be
  1841. * reused. If it is no longer a SCSI target, the target will be torn
  1842. * down. If it continues to be a SCSI target, then the target will be
  1843. * unblocked (allowing i/o to be resumed), and a scan will be activated
  1844. * to ensure that all luns are detected.
  1845. *
  1846. * Called from normal process context only - cannot be called from interrupt.
  1847. *
  1848. * Notes:
  1849. * This routine assumes no locks are held on entry.
  1850. **/
  1851. void
  1852. fc_remote_port_delete(struct fc_rport *rport)
  1853. {
  1854. struct Scsi_Host *shost = rport_to_shost(rport);
  1855. struct fc_internal *i = to_fc_internal(shost->transportt);
  1856. int timeout = rport->dev_loss_tmo;
  1857. unsigned long flags;
  1858. /*
  1859. * No need to flush the fc_host work_q's, as all adds are synchronous.
  1860. *
  1861. * We do need to reclaim the rport scan work element, so eventually
  1862. * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
  1863. * there's still a scan pending.
  1864. */
  1865. spin_lock_irqsave(shost->host_lock, flags);
  1866. if (rport->port_state != FC_PORTSTATE_ONLINE) {
  1867. spin_unlock_irqrestore(shost->host_lock, flags);
  1868. return;
  1869. }
  1870. /*
  1871. * In the past, we if this was not an FCP-Target, we would
  1872. * unconditionally just jump to deleting the rport.
  1873. * However, rports can be used as node containers by the LLDD,
  1874. * and its not appropriate to just terminate the rport at the
  1875. * first sign of a loss in connectivity. The LLDD may want to
  1876. * send ELS traffic to re-validate the login. If the rport is
  1877. * immediately deleted, it makes it inappropriate for a node
  1878. * container.
  1879. * So... we now unconditionally wait dev_loss_tmo before
  1880. * destroying an rport.
  1881. */
  1882. rport->port_state = FC_PORTSTATE_BLOCKED;
  1883. rport->flags |= FC_RPORT_DEVLOSS_PENDING;
  1884. spin_unlock_irqrestore(shost->host_lock, flags);
  1885. scsi_target_block(&rport->dev);
  1886. /* see if we need to kill io faster than waiting for device loss */
  1887. if ((rport->fast_io_fail_tmo != -1) &&
  1888. (rport->fast_io_fail_tmo < timeout) && (i->f->terminate_rport_io))
  1889. fc_queue_devloss_work(shost, &rport->fail_io_work,
  1890. rport->fast_io_fail_tmo * HZ);
  1891. /* cap the length the devices can be blocked until they are deleted */
  1892. fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
  1893. }
  1894. EXPORT_SYMBOL(fc_remote_port_delete);
  1895. /**
  1896. * fc_remote_port_rolechg - notifies the fc transport that the roles
  1897. * on a remote may have changed.
  1898. * @rport: The remote port that changed.
  1899. *
  1900. * The LLDD calls this routine to notify the transport that the roles
  1901. * on a remote port may have changed. The largest effect of this is
  1902. * if a port now becomes a FCP Target, it must be allocated a
  1903. * scsi target id. If the port is no longer a FCP target, any
  1904. * scsi target id value assigned to it will persist in case the
  1905. * role changes back to include FCP Target. No changes in the scsi
  1906. * midlayer will be invoked if the role changes (in the expectation
  1907. * that the role will be resumed. If it doesn't normal error processing
  1908. * will take place).
  1909. *
  1910. * Should not be called from interrupt context.
  1911. *
  1912. * Notes:
  1913. * This routine assumes no locks are held on entry.
  1914. **/
  1915. void
  1916. fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
  1917. {
  1918. struct Scsi_Host *shost = rport_to_shost(rport);
  1919. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  1920. unsigned long flags;
  1921. int create = 0;
  1922. spin_lock_irqsave(shost->host_lock, flags);
  1923. if (roles & FC_RPORT_ROLE_FCP_TARGET) {
  1924. if (rport->scsi_target_id == -1) {
  1925. rport->scsi_target_id = fc_host->next_target_id++;
  1926. create = 1;
  1927. } else if (!(rport->roles & FC_RPORT_ROLE_FCP_TARGET))
  1928. create = 1;
  1929. }
  1930. rport->roles = roles;
  1931. spin_unlock_irqrestore(shost->host_lock, flags);
  1932. if (create) {
  1933. /*
  1934. * There may have been a delete timer running on the
  1935. * port. Ensure that it is cancelled as we now know
  1936. * the port is an FCP Target.
  1937. * Note: we know the rport is exists and in an online
  1938. * state as the LLDD would not have had an rport
  1939. * reference to pass us.
  1940. *
  1941. * Take no action on the del_timer failure as the state
  1942. * machine state change will validate the
  1943. * transaction.
  1944. */
  1945. if (!cancel_delayed_work(&rport->fail_io_work))
  1946. fc_flush_devloss(shost);
  1947. if (!cancel_delayed_work(&rport->dev_loss_work))
  1948. fc_flush_devloss(shost);
  1949. spin_lock_irqsave(shost->host_lock, flags);
  1950. rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
  1951. spin_unlock_irqrestore(shost->host_lock, flags);
  1952. /* ensure any stgt delete functions are done */
  1953. fc_flush_work(shost);
  1954. /* initiate a scan of the target */
  1955. spin_lock_irqsave(shost->host_lock, flags);
  1956. rport->flags |= FC_RPORT_SCAN_PENDING;
  1957. scsi_queue_work(shost, &rport->scan_work);
  1958. spin_unlock_irqrestore(shost->host_lock, flags);
  1959. scsi_target_unblock(&rport->dev);
  1960. }
  1961. }
  1962. EXPORT_SYMBOL(fc_remote_port_rolechg);
  1963. /**
  1964. * fc_timeout_deleted_rport - Timeout handler for a deleted remote port,
  1965. * which we blocked, and has now failed to return
  1966. * in the allotted time.
  1967. *
  1968. * @work: rport target that failed to reappear in the allotted time.
  1969. **/
  1970. static void
  1971. fc_timeout_deleted_rport(struct work_struct *work)
  1972. {
  1973. struct fc_rport *rport =
  1974. container_of(work, struct fc_rport, dev_loss_work.work);
  1975. struct Scsi_Host *shost = rport_to_shost(rport);
  1976. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  1977. unsigned long flags;
  1978. spin_lock_irqsave(shost->host_lock, flags);
  1979. rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
  1980. /*
  1981. * If the port is ONLINE, then it came back. If it was a SCSI
  1982. * target, validate it still is. If not, tear down the
  1983. * scsi_target on it.
  1984. */
  1985. if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
  1986. (rport->scsi_target_id != -1) &&
  1987. !(rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
  1988. dev_printk(KERN_ERR, &rport->dev,
  1989. "blocked FC remote port time out: no longer"
  1990. " a FCP target, removing starget\n");
  1991. spin_unlock_irqrestore(shost->host_lock, flags);
  1992. scsi_target_unblock(&rport->dev);
  1993. fc_queue_work(shost, &rport->stgt_delete_work);
  1994. return;
  1995. }
  1996. /* NOOP state - we're flushing workq's */
  1997. if (rport->port_state != FC_PORTSTATE_BLOCKED) {
  1998. spin_unlock_irqrestore(shost->host_lock, flags);
  1999. dev_printk(KERN_ERR, &rport->dev,
  2000. "blocked FC remote port time out: leaving"
  2001. " rport%s alone\n",
  2002. (rport->scsi_target_id != -1) ? " and starget" : "");
  2003. return;
  2004. }
  2005. if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
  2006. (rport->scsi_target_id == -1)) {
  2007. list_del(&rport->peers);
  2008. rport->port_state = FC_PORTSTATE_DELETED;
  2009. dev_printk(KERN_ERR, &rport->dev,
  2010. "blocked FC remote port time out: removing"
  2011. " rport%s\n",
  2012. (rport->scsi_target_id != -1) ? " and starget" : "");
  2013. fc_queue_work(shost, &rport->rport_delete_work);
  2014. spin_unlock_irqrestore(shost->host_lock, flags);
  2015. return;
  2016. }
  2017. dev_printk(KERN_ERR, &rport->dev,
  2018. "blocked FC remote port time out: removing target and "
  2019. "saving binding\n");
  2020. list_move_tail(&rport->peers, &fc_host->rport_bindings);
  2021. /*
  2022. * Note: We do not remove or clear the hostdata area. This allows
  2023. * host-specific target data to persist along with the
  2024. * scsi_target_id. It's up to the host to manage it's hostdata area.
  2025. */
  2026. /*
  2027. * Reinitialize port attributes that may change if the port comes back.
  2028. */
  2029. rport->maxframe_size = -1;
  2030. rport->supported_classes = FC_COS_UNSPECIFIED;
  2031. rport->roles = FC_RPORT_ROLE_UNKNOWN;
  2032. rport->port_state = FC_PORTSTATE_NOTPRESENT;
  2033. /* remove the identifiers that aren't used in the consisting binding */
  2034. switch (fc_host->tgtid_bind_type) {
  2035. case FC_TGTID_BIND_BY_WWPN:
  2036. rport->node_name = -1;
  2037. rport->port_id = -1;
  2038. break;
  2039. case FC_TGTID_BIND_BY_WWNN:
  2040. rport->port_name = -1;
  2041. rport->port_id = -1;
  2042. break;
  2043. case FC_TGTID_BIND_BY_ID:
  2044. rport->node_name = -1;
  2045. rport->port_name = -1;
  2046. break;
  2047. case FC_TGTID_BIND_NONE: /* to keep compiler happy */
  2048. break;
  2049. }
  2050. /*
  2051. * As this only occurs if the remote port (scsi target)
  2052. * went away and didn't come back - we'll remove
  2053. * all attached scsi devices.
  2054. */
  2055. spin_unlock_irqrestore(shost->host_lock, flags);
  2056. scsi_target_unblock(&rport->dev);
  2057. fc_queue_work(shost, &rport->stgt_delete_work);
  2058. }
  2059. /**
  2060. * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a
  2061. * disconnected SCSI target.
  2062. *
  2063. * @work: rport to terminate io on.
  2064. *
  2065. * Notes: Only requests the failure of the io, not that all are flushed
  2066. * prior to returning.
  2067. **/
  2068. static void
  2069. fc_timeout_fail_rport_io(struct work_struct *work)
  2070. {
  2071. struct fc_rport *rport =
  2072. container_of(work, struct fc_rport, fail_io_work.work);
  2073. struct Scsi_Host *shost = rport_to_shost(rport);
  2074. struct fc_internal *i = to_fc_internal(shost->transportt);
  2075. if (rport->port_state != FC_PORTSTATE_BLOCKED)
  2076. return;
  2077. i->f->terminate_rport_io(rport);
  2078. }
  2079. /**
  2080. * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
  2081. *
  2082. * @work: remote port to be scanned.
  2083. **/
  2084. static void
  2085. fc_scsi_scan_rport(struct work_struct *work)
  2086. {
  2087. struct fc_rport *rport =
  2088. container_of(work, struct fc_rport, scan_work);
  2089. struct Scsi_Host *shost = rport_to_shost(rport);
  2090. unsigned long flags;
  2091. if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
  2092. (rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
  2093. scsi_scan_target(&rport->dev, rport->channel,
  2094. rport->scsi_target_id, SCAN_WILD_CARD, 1);
  2095. }
  2096. spin_lock_irqsave(shost->host_lock, flags);
  2097. rport->flags &= ~FC_RPORT_SCAN_PENDING;
  2098. spin_unlock_irqrestore(shost->host_lock, flags);
  2099. }
  2100. MODULE_AUTHOR("Martin Hicks");
  2101. MODULE_DESCRIPTION("FC Transport Attributes");
  2102. MODULE_LICENSE("GPL");
  2103. module_init(fc_transport_init);
  2104. module_exit(fc_transport_exit);