scsi_transport_fc.c 73 KB

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