scsi_transport_fc.c 73 KB

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