scsi_transport_fc.c 73 KB

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