scsi_transport_fc.c 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230
  1. /*
  2. * FiberChannel transport specific attributes exported to sysfs.
  3. *
  4. * Copyright (c) 2003 Silicon Graphics, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * ========
  21. *
  22. * Copyright (C) 2004-2007 James Smart, Emulex Corporation
  23. * Rewrite for host, target, device, and remote port attributes,
  24. * statistics, and service functions...
  25. * Add vports, etc
  26. *
  27. */
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <scsi/scsi_device.h>
  31. #include <scsi/scsi_host.h>
  32. #include <scsi/scsi_transport.h>
  33. #include <scsi/scsi_transport_fc.h>
  34. #include <scsi/scsi_cmnd.h>
  35. #include <linux/netlink.h>
  36. #include <net/netlink.h>
  37. #include <scsi/scsi_netlink_fc.h>
  38. #include "scsi_priv.h"
  39. static int fc_queue_work(struct Scsi_Host *, struct work_struct *);
  40. static void fc_vport_sched_delete(struct work_struct *work);
  41. /*
  42. * This is a temporary carrier for creating a vport. It will eventually
  43. * be replaced by a real message definition for sgio or netlink.
  44. *
  45. * fc_vport_identifiers: This set of data contains all elements
  46. * to uniquely identify and instantiate a FC virtual port.
  47. *
  48. * Notes:
  49. * symbolic_name: The driver is to append the symbolic_name string data
  50. * to the symbolic_node_name data that it generates by default.
  51. * the resulting combination should then be registered with the switch.
  52. * It is expected that things like Xen may stuff a VM title into
  53. * this field.
  54. */
  55. struct fc_vport_identifiers {
  56. u64 node_name;
  57. u64 port_name;
  58. u32 roles;
  59. bool disable;
  60. enum fc_port_type vport_type; /* only FC_PORTTYPE_NPIV allowed */
  61. char symbolic_name[FC_VPORT_SYMBOLIC_NAMELEN];
  62. };
  63. static int fc_vport_create(struct Scsi_Host *shost, int channel,
  64. struct device *pdev, struct fc_vport_identifiers *ids,
  65. struct fc_vport **vport);
  66. /*
  67. * Redefine so that we can have same named attributes in the
  68. * sdev/starget/host objects.
  69. */
  70. #define FC_CLASS_DEVICE_ATTR(_prefix,_name,_mode,_show,_store) \
  71. struct class_device_attribute class_device_attr_##_prefix##_##_name = \
  72. __ATTR(_name,_mode,_show,_store)
  73. #define fc_enum_name_search(title, table_type, table) \
  74. static const char *get_fc_##title##_name(enum table_type table_key) \
  75. { \
  76. int i; \
  77. char *name = NULL; \
  78. \
  79. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  80. if (table[i].value == table_key) { \
  81. name = table[i].name; \
  82. break; \
  83. } \
  84. } \
  85. return name; \
  86. }
  87. #define fc_enum_name_match(title, table_type, table) \
  88. static int get_fc_##title##_match(const char *table_key, \
  89. enum table_type *value) \
  90. { \
  91. int i; \
  92. \
  93. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  94. if (strncmp(table_key, table[i].name, \
  95. table[i].matchlen) == 0) { \
  96. *value = table[i].value; \
  97. return 0; /* success */ \
  98. } \
  99. } \
  100. return 1; /* failure */ \
  101. }
  102. /* Convert fc_port_type values to ascii string name */
  103. static struct {
  104. enum fc_port_type value;
  105. char *name;
  106. } fc_port_type_names[] = {
  107. { FC_PORTTYPE_UNKNOWN, "Unknown" },
  108. { FC_PORTTYPE_OTHER, "Other" },
  109. { FC_PORTTYPE_NOTPRESENT, "Not Present" },
  110. { FC_PORTTYPE_NPORT, "NPort (fabric via point-to-point)" },
  111. { FC_PORTTYPE_NLPORT, "NLPort (fabric via loop)" },
  112. { FC_PORTTYPE_LPORT, "LPort (private loop)" },
  113. { FC_PORTTYPE_PTP, "Point-To-Point (direct nport connection" },
  114. { FC_PORTTYPE_NPIV, "NPIV VPORT" },
  115. };
  116. fc_enum_name_search(port_type, fc_port_type, fc_port_type_names)
  117. #define FC_PORTTYPE_MAX_NAMELEN 50
  118. /* Reuse fc_port_type enum function for vport_type */
  119. #define get_fc_vport_type_name get_fc_port_type_name
  120. /* Convert fc_host_event_code values to ascii string name */
  121. static const struct {
  122. enum fc_host_event_code value;
  123. char *name;
  124. } fc_host_event_code_names[] = {
  125. { FCH_EVT_LIP, "lip" },
  126. { FCH_EVT_LINKUP, "link_up" },
  127. { FCH_EVT_LINKDOWN, "link_down" },
  128. { FCH_EVT_LIPRESET, "lip_reset" },
  129. { FCH_EVT_RSCN, "rscn" },
  130. { FCH_EVT_ADAPTER_CHANGE, "adapter_chg" },
  131. { FCH_EVT_PORT_UNKNOWN, "port_unknown" },
  132. { FCH_EVT_PORT_ONLINE, "port_online" },
  133. { FCH_EVT_PORT_OFFLINE, "port_offline" },
  134. { FCH_EVT_PORT_FABRIC, "port_fabric" },
  135. { FCH_EVT_LINK_UNKNOWN, "link_unknown" },
  136. { FCH_EVT_VENDOR_UNIQUE, "vendor_unique" },
  137. };
  138. fc_enum_name_search(host_event_code, fc_host_event_code,
  139. fc_host_event_code_names)
  140. #define FC_HOST_EVENT_CODE_MAX_NAMELEN 30
  141. /* Convert fc_port_state values to ascii string name */
  142. static struct {
  143. enum fc_port_state value;
  144. char *name;
  145. } fc_port_state_names[] = {
  146. { FC_PORTSTATE_UNKNOWN, "Unknown" },
  147. { FC_PORTSTATE_NOTPRESENT, "Not Present" },
  148. { FC_PORTSTATE_ONLINE, "Online" },
  149. { FC_PORTSTATE_OFFLINE, "Offline" },
  150. { FC_PORTSTATE_BLOCKED, "Blocked" },
  151. { FC_PORTSTATE_BYPASSED, "Bypassed" },
  152. { FC_PORTSTATE_DIAGNOSTICS, "Diagnostics" },
  153. { FC_PORTSTATE_LINKDOWN, "Linkdown" },
  154. { FC_PORTSTATE_ERROR, "Error" },
  155. { FC_PORTSTATE_LOOPBACK, "Loopback" },
  156. { FC_PORTSTATE_DELETED, "Deleted" },
  157. };
  158. fc_enum_name_search(port_state, fc_port_state, fc_port_state_names)
  159. #define FC_PORTSTATE_MAX_NAMELEN 20
  160. /* Convert fc_vport_state values to ascii string name */
  161. static struct {
  162. enum fc_vport_state value;
  163. char *name;
  164. } fc_vport_state_names[] = {
  165. { FC_VPORT_UNKNOWN, "Unknown" },
  166. { FC_VPORT_ACTIVE, "Active" },
  167. { FC_VPORT_DISABLED, "Disabled" },
  168. { FC_VPORT_LINKDOWN, "Linkdown" },
  169. { FC_VPORT_INITIALIZING, "Initializing" },
  170. { FC_VPORT_NO_FABRIC_SUPP, "No Fabric Support" },
  171. { FC_VPORT_NO_FABRIC_RSCS, "No Fabric Resources" },
  172. { FC_VPORT_FABRIC_LOGOUT, "Fabric Logout" },
  173. { FC_VPORT_FABRIC_REJ_WWN, "Fabric Rejected WWN" },
  174. { FC_VPORT_FAILED, "VPort Failed" },
  175. };
  176. fc_enum_name_search(vport_state, fc_vport_state, fc_vport_state_names)
  177. #define FC_VPORTSTATE_MAX_NAMELEN 24
  178. /* Reuse fc_vport_state enum function for vport_last_state */
  179. #define get_fc_vport_last_state_name get_fc_vport_state_name
  180. /* Convert fc_tgtid_binding_type values to ascii string name */
  181. static const struct {
  182. enum fc_tgtid_binding_type value;
  183. char *name;
  184. int matchlen;
  185. } fc_tgtid_binding_type_names[] = {
  186. { FC_TGTID_BIND_NONE, "none", 4 },
  187. { FC_TGTID_BIND_BY_WWPN, "wwpn (World Wide Port Name)", 4 },
  188. { FC_TGTID_BIND_BY_WWNN, "wwnn (World Wide Node Name)", 4 },
  189. { FC_TGTID_BIND_BY_ID, "port_id (FC Address)", 7 },
  190. };
  191. fc_enum_name_search(tgtid_bind_type, fc_tgtid_binding_type,
  192. fc_tgtid_binding_type_names)
  193. fc_enum_name_match(tgtid_bind_type, fc_tgtid_binding_type,
  194. fc_tgtid_binding_type_names)
  195. #define FC_BINDTYPE_MAX_NAMELEN 30
  196. #define fc_bitfield_name_search(title, table) \
  197. static ssize_t \
  198. get_fc_##title##_names(u32 table_key, char *buf) \
  199. { \
  200. char *prefix = ""; \
  201. ssize_t len = 0; \
  202. int i; \
  203. \
  204. for (i = 0; i < ARRAY_SIZE(table); i++) { \
  205. if (table[i].value & table_key) { \
  206. len += sprintf(buf + len, "%s%s", \
  207. prefix, table[i].name); \
  208. prefix = ", "; \
  209. } \
  210. } \
  211. len += sprintf(buf + len, "\n"); \
  212. return len; \
  213. }
  214. /* Convert FC_COS bit values to ascii string name */
  215. static const struct {
  216. u32 value;
  217. char *name;
  218. } fc_cos_names[] = {
  219. { FC_COS_CLASS1, "Class 1" },
  220. { FC_COS_CLASS2, "Class 2" },
  221. { FC_COS_CLASS3, "Class 3" },
  222. { FC_COS_CLASS4, "Class 4" },
  223. { FC_COS_CLASS6, "Class 6" },
  224. };
  225. fc_bitfield_name_search(cos, fc_cos_names)
  226. /* Convert FC_PORTSPEED bit values to ascii string name */
  227. static const struct {
  228. u32 value;
  229. char *name;
  230. } fc_port_speed_names[] = {
  231. { FC_PORTSPEED_1GBIT, "1 Gbit" },
  232. { FC_PORTSPEED_2GBIT, "2 Gbit" },
  233. { FC_PORTSPEED_4GBIT, "4 Gbit" },
  234. { FC_PORTSPEED_10GBIT, "10 Gbit" },
  235. { FC_PORTSPEED_8GBIT, "8 Gbit" },
  236. { FC_PORTSPEED_16GBIT, "16 Gbit" },
  237. { FC_PORTSPEED_NOT_NEGOTIATED, "Not Negotiated" },
  238. };
  239. fc_bitfield_name_search(port_speed, fc_port_speed_names)
  240. static int
  241. show_fc_fc4s (char *buf, u8 *fc4_list)
  242. {
  243. int i, len=0;
  244. for (i = 0; i < FC_FC4_LIST_SIZE; i++, fc4_list++)
  245. len += sprintf(buf + len , "0x%02x ", *fc4_list);
  246. len += sprintf(buf + len, "\n");
  247. return len;
  248. }
  249. /* Convert FC_PORT_ROLE bit values to ascii string name */
  250. static const struct {
  251. u32 value;
  252. char *name;
  253. } fc_port_role_names[] = {
  254. { FC_PORT_ROLE_FCP_TARGET, "FCP Target" },
  255. { FC_PORT_ROLE_FCP_INITIATOR, "FCP Initiator" },
  256. { FC_PORT_ROLE_IP_PORT, "IP Port" },
  257. };
  258. fc_bitfield_name_search(port_roles, fc_port_role_names)
  259. /*
  260. * Define roles that are specific to port_id. Values are relative to ROLE_MASK.
  261. */
  262. #define FC_WELLKNOWN_PORTID_MASK 0xfffff0
  263. #define FC_WELLKNOWN_ROLE_MASK 0x00000f
  264. #define FC_FPORT_PORTID 0x00000e
  265. #define FC_FABCTLR_PORTID 0x00000d
  266. #define FC_DIRSRVR_PORTID 0x00000c
  267. #define FC_TIMESRVR_PORTID 0x00000b
  268. #define FC_MGMTSRVR_PORTID 0x00000a
  269. static void fc_timeout_deleted_rport(struct work_struct *work);
  270. static void fc_timeout_fail_rport_io(struct work_struct *work);
  271. static void fc_scsi_scan_rport(struct work_struct *work);
  272. /*
  273. * Attribute counts pre object type...
  274. * Increase these values if you add attributes
  275. */
  276. #define FC_STARGET_NUM_ATTRS 3
  277. #define FC_RPORT_NUM_ATTRS 10
  278. #define FC_VPORT_NUM_ATTRS 9
  279. #define FC_HOST_NUM_ATTRS 21
  280. struct fc_internal {
  281. struct scsi_transport_template t;
  282. struct fc_function_template *f;
  283. /*
  284. * For attributes : each object has :
  285. * An array of the actual attributes structures
  286. * An array of null-terminated pointers to the attribute
  287. * structures - used for mid-layer interaction.
  288. *
  289. * The attribute containers for the starget and host are are
  290. * part of the midlayer. As the remote port is specific to the
  291. * fc transport, we must provide the attribute container.
  292. */
  293. struct class_device_attribute private_starget_attrs[
  294. FC_STARGET_NUM_ATTRS];
  295. struct class_device_attribute *starget_attrs[FC_STARGET_NUM_ATTRS + 1];
  296. struct class_device_attribute private_host_attrs[FC_HOST_NUM_ATTRS];
  297. struct class_device_attribute *host_attrs[FC_HOST_NUM_ATTRS + 1];
  298. struct transport_container rport_attr_cont;
  299. struct class_device_attribute private_rport_attrs[FC_RPORT_NUM_ATTRS];
  300. struct class_device_attribute *rport_attrs[FC_RPORT_NUM_ATTRS + 1];
  301. struct transport_container vport_attr_cont;
  302. struct class_device_attribute private_vport_attrs[FC_VPORT_NUM_ATTRS];
  303. struct class_device_attribute *vport_attrs[FC_VPORT_NUM_ATTRS + 1];
  304. };
  305. #define to_fc_internal(tmpl) container_of(tmpl, struct fc_internal, t)
  306. static int fc_target_setup(struct transport_container *tc, struct device *dev,
  307. struct class_device *cdev)
  308. {
  309. struct scsi_target *starget = to_scsi_target(dev);
  310. struct fc_rport *rport = starget_to_rport(starget);
  311. /*
  312. * if parent is remote port, use values from remote port.
  313. * Otherwise, this host uses the fc_transport, but not the
  314. * remote port interface. As such, initialize to known non-values.
  315. */
  316. if (rport) {
  317. fc_starget_node_name(starget) = rport->node_name;
  318. fc_starget_port_name(starget) = rport->port_name;
  319. fc_starget_port_id(starget) = rport->port_id;
  320. } else {
  321. fc_starget_node_name(starget) = -1;
  322. fc_starget_port_name(starget) = -1;
  323. fc_starget_port_id(starget) = -1;
  324. }
  325. return 0;
  326. }
  327. static DECLARE_TRANSPORT_CLASS(fc_transport_class,
  328. "fc_transport",
  329. fc_target_setup,
  330. NULL,
  331. NULL);
  332. static int fc_host_setup(struct transport_container *tc, struct device *dev,
  333. struct class_device *cdev)
  334. {
  335. struct Scsi_Host *shost = dev_to_shost(dev);
  336. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  337. /*
  338. * Set default values easily detected by the midlayer as
  339. * failure cases. The scsi lldd is responsible for initializing
  340. * all transport attributes to valid values per host.
  341. */
  342. fc_host->node_name = -1;
  343. fc_host->port_name = -1;
  344. fc_host->permanent_port_name = -1;
  345. fc_host->supported_classes = FC_COS_UNSPECIFIED;
  346. memset(fc_host->supported_fc4s, 0,
  347. sizeof(fc_host->supported_fc4s));
  348. fc_host->supported_speeds = FC_PORTSPEED_UNKNOWN;
  349. fc_host->maxframe_size = -1;
  350. fc_host->max_npiv_vports = 0;
  351. memset(fc_host->serial_number, 0,
  352. sizeof(fc_host->serial_number));
  353. fc_host->port_id = -1;
  354. fc_host->port_type = FC_PORTTYPE_UNKNOWN;
  355. fc_host->port_state = FC_PORTSTATE_UNKNOWN;
  356. memset(fc_host->active_fc4s, 0,
  357. sizeof(fc_host->active_fc4s));
  358. fc_host->speed = FC_PORTSPEED_UNKNOWN;
  359. fc_host->fabric_name = -1;
  360. memset(fc_host->symbolic_name, 0, sizeof(fc_host->symbolic_name));
  361. memset(fc_host->system_hostname, 0, sizeof(fc_host->system_hostname));
  362. fc_host->tgtid_bind_type = FC_TGTID_BIND_BY_WWPN;
  363. INIT_LIST_HEAD(&fc_host->rports);
  364. INIT_LIST_HEAD(&fc_host->rport_bindings);
  365. INIT_LIST_HEAD(&fc_host->vports);
  366. fc_host->next_rport_number = 0;
  367. fc_host->next_target_id = 0;
  368. fc_host->next_vport_number = 0;
  369. fc_host->npiv_vports_inuse = 0;
  370. snprintf(fc_host->work_q_name, KOBJ_NAME_LEN, "fc_wq_%d",
  371. shost->host_no);
  372. fc_host->work_q = create_singlethread_workqueue(
  373. fc_host->work_q_name);
  374. if (!fc_host->work_q)
  375. return -ENOMEM;
  376. snprintf(fc_host->devloss_work_q_name, KOBJ_NAME_LEN, "fc_dl_%d",
  377. shost->host_no);
  378. fc_host->devloss_work_q = create_singlethread_workqueue(
  379. fc_host->devloss_work_q_name);
  380. if (!fc_host->devloss_work_q) {
  381. destroy_workqueue(fc_host->work_q);
  382. fc_host->work_q = NULL;
  383. return -ENOMEM;
  384. }
  385. return 0;
  386. }
  387. static DECLARE_TRANSPORT_CLASS(fc_host_class,
  388. "fc_host",
  389. fc_host_setup,
  390. NULL,
  391. NULL);
  392. /*
  393. * Setup and Remove actions for remote ports are handled
  394. * in the service functions below.
  395. */
  396. static DECLARE_TRANSPORT_CLASS(fc_rport_class,
  397. "fc_remote_ports",
  398. NULL,
  399. NULL,
  400. NULL);
  401. /*
  402. * Setup and Remove actions for virtual ports are handled
  403. * in the service functions below.
  404. */
  405. static DECLARE_TRANSPORT_CLASS(fc_vport_class,
  406. "fc_vports",
  407. NULL,
  408. NULL,
  409. NULL);
  410. /*
  411. * Module Parameters
  412. */
  413. /*
  414. * dev_loss_tmo: the default number of seconds that the FC transport
  415. * should insulate the loss of a remote port.
  416. * The maximum will be capped by the value of SCSI_DEVICE_BLOCK_MAX_TIMEOUT.
  417. */
  418. static unsigned int fc_dev_loss_tmo = 60; /* seconds */
  419. module_param_named(dev_loss_tmo, fc_dev_loss_tmo, int, S_IRUGO|S_IWUSR);
  420. MODULE_PARM_DESC(dev_loss_tmo,
  421. "Maximum number of seconds that the FC transport should"
  422. " insulate the loss of a remote port. Once this value is"
  423. " exceeded, the scsi target is removed. Value should be"
  424. " between 1 and SCSI_DEVICE_BLOCK_MAX_TIMEOUT.");
  425. /**
  426. * Netlink Infrastructure
  427. **/
  428. static atomic_t fc_event_seq;
  429. /**
  430. * fc_get_event_number - Obtain the next sequential FC event number
  431. *
  432. * Notes:
  433. * We could have inline'd this, but it would have required fc_event_seq to
  434. * be exposed. For now, live with the subroutine call.
  435. * Atomic used to avoid lock/unlock...
  436. **/
  437. u32
  438. fc_get_event_number(void)
  439. {
  440. return atomic_add_return(1, &fc_event_seq);
  441. }
  442. EXPORT_SYMBOL(fc_get_event_number);
  443. /**
  444. * fc_host_post_event - called to post an even on an fc_host.
  445. *
  446. * @shost: host the event occurred on
  447. * @event_number: fc event number obtained from get_fc_event_number()
  448. * @event_code: fc_host event being posted
  449. * @event_data: 32bits of data for the event being posted
  450. *
  451. * Notes:
  452. * This routine assumes no locks are held on entry.
  453. **/
  454. void
  455. fc_host_post_event(struct Scsi_Host *shost, u32 event_number,
  456. enum fc_host_event_code event_code, u32 event_data)
  457. {
  458. struct sk_buff *skb;
  459. struct nlmsghdr *nlh;
  460. struct fc_nl_event *event;
  461. const char *name;
  462. u32 len, skblen;
  463. int err;
  464. if (!scsi_nl_sock) {
  465. err = -ENOENT;
  466. goto send_fail;
  467. }
  468. len = FC_NL_MSGALIGN(sizeof(*event));
  469. skblen = NLMSG_SPACE(len);
  470. skb = alloc_skb(skblen, GFP_KERNEL);
  471. if (!skb) {
  472. err = -ENOBUFS;
  473. goto send_fail;
  474. }
  475. nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
  476. skblen - sizeof(*nlh), 0);
  477. if (!nlh) {
  478. err = -ENOBUFS;
  479. goto send_fail_skb;
  480. }
  481. event = NLMSG_DATA(nlh);
  482. INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
  483. FC_NL_ASYNC_EVENT, len);
  484. event->seconds = get_seconds();
  485. event->vendor_id = 0;
  486. event->host_no = shost->host_no;
  487. event->event_datalen = sizeof(u32); /* bytes */
  488. event->event_num = event_number;
  489. event->event_code = event_code;
  490. event->event_data = event_data;
  491. err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
  492. GFP_KERNEL);
  493. if (err && (err != -ESRCH)) /* filter no recipient errors */
  494. /* nlmsg_multicast already kfree_skb'd */
  495. goto send_fail;
  496. return;
  497. send_fail_skb:
  498. kfree_skb(skb);
  499. send_fail:
  500. name = get_fc_host_event_code_name(event_code);
  501. printk(KERN_WARNING
  502. "%s: Dropped Event : host %d %s data 0x%08x - err %d\n",
  503. __FUNCTION__, shost->host_no,
  504. (name) ? name : "<unknown>", event_data, err);
  505. return;
  506. }
  507. EXPORT_SYMBOL(fc_host_post_event);
  508. /**
  509. * fc_host_post_vendor_event - called to post a vendor unique event on
  510. * a fc_host
  511. *
  512. * @shost: host the event occurred on
  513. * @event_number: fc event number obtained from get_fc_event_number()
  514. * @data_len: amount, in bytes, of vendor unique data
  515. * @data_buf: pointer to vendor unique data
  516. *
  517. * Notes:
  518. * This routine assumes no locks are held on entry.
  519. **/
  520. void
  521. fc_host_post_vendor_event(struct Scsi_Host *shost, u32 event_number,
  522. u32 data_len, char * data_buf, u64 vendor_id)
  523. {
  524. struct sk_buff *skb;
  525. struct nlmsghdr *nlh;
  526. struct fc_nl_event *event;
  527. u32 len, skblen;
  528. int err;
  529. if (!scsi_nl_sock) {
  530. err = -ENOENT;
  531. goto send_vendor_fail;
  532. }
  533. len = FC_NL_MSGALIGN(sizeof(*event) + data_len);
  534. skblen = NLMSG_SPACE(len);
  535. skb = alloc_skb(skblen, GFP_KERNEL);
  536. if (!skb) {
  537. err = -ENOBUFS;
  538. goto send_vendor_fail;
  539. }
  540. nlh = nlmsg_put(skb, 0, 0, SCSI_TRANSPORT_MSG,
  541. skblen - sizeof(*nlh), 0);
  542. if (!nlh) {
  543. err = -ENOBUFS;
  544. goto send_vendor_fail_skb;
  545. }
  546. event = NLMSG_DATA(nlh);
  547. INIT_SCSI_NL_HDR(&event->snlh, SCSI_NL_TRANSPORT_FC,
  548. FC_NL_ASYNC_EVENT, len);
  549. event->seconds = get_seconds();
  550. event->vendor_id = vendor_id;
  551. event->host_no = shost->host_no;
  552. event->event_datalen = data_len; /* bytes */
  553. event->event_num = event_number;
  554. event->event_code = FCH_EVT_VENDOR_UNIQUE;
  555. memcpy(&event->event_data, data_buf, data_len);
  556. err = nlmsg_multicast(scsi_nl_sock, skb, 0, SCSI_NL_GRP_FC_EVENTS,
  557. GFP_KERNEL);
  558. if (err && (err != -ESRCH)) /* filter no recipient errors */
  559. /* nlmsg_multicast already kfree_skb'd */
  560. goto send_vendor_fail;
  561. return;
  562. send_vendor_fail_skb:
  563. kfree_skb(skb);
  564. send_vendor_fail:
  565. printk(KERN_WARNING
  566. "%s: Dropped Event : host %d vendor_unique - err %d\n",
  567. __FUNCTION__, shost->host_no, err);
  568. return;
  569. }
  570. EXPORT_SYMBOL(fc_host_post_vendor_event);
  571. static __init int fc_transport_init(void)
  572. {
  573. int error;
  574. atomic_set(&fc_event_seq, 0);
  575. error = transport_class_register(&fc_host_class);
  576. if (error)
  577. return error;
  578. error = transport_class_register(&fc_vport_class);
  579. if (error)
  580. return error;
  581. error = transport_class_register(&fc_rport_class);
  582. if (error)
  583. return error;
  584. return transport_class_register(&fc_transport_class);
  585. }
  586. static void __exit fc_transport_exit(void)
  587. {
  588. transport_class_unregister(&fc_transport_class);
  589. transport_class_unregister(&fc_rport_class);
  590. transport_class_unregister(&fc_host_class);
  591. transport_class_unregister(&fc_vport_class);
  592. }
  593. /*
  594. * FC Remote Port Attribute Management
  595. */
  596. #define fc_rport_show_function(field, format_string, sz, cast) \
  597. static ssize_t \
  598. show_fc_rport_##field (struct class_device *cdev, char *buf) \
  599. { \
  600. struct fc_rport *rport = transport_class_to_rport(cdev); \
  601. struct Scsi_Host *shost = rport_to_shost(rport); \
  602. struct fc_internal *i = to_fc_internal(shost->transportt); \
  603. if ((i->f->get_rport_##field) && \
  604. !((rport->port_state == FC_PORTSTATE_BLOCKED) || \
  605. (rport->port_state == FC_PORTSTATE_DELETED) || \
  606. (rport->port_state == FC_PORTSTATE_NOTPRESENT))) \
  607. i->f->get_rport_##field(rport); \
  608. return snprintf(buf, sz, format_string, cast rport->field); \
  609. }
  610. #define fc_rport_store_function(field) \
  611. static ssize_t \
  612. store_fc_rport_##field(struct class_device *cdev, const char *buf, \
  613. size_t count) \
  614. { \
  615. int val; \
  616. struct fc_rport *rport = transport_class_to_rport(cdev); \
  617. struct Scsi_Host *shost = rport_to_shost(rport); \
  618. struct fc_internal *i = to_fc_internal(shost->transportt); \
  619. char *cp; \
  620. if ((rport->port_state == FC_PORTSTATE_BLOCKED) || \
  621. (rport->port_state == FC_PORTSTATE_DELETED) || \
  622. (rport->port_state == FC_PORTSTATE_NOTPRESENT)) \
  623. return -EBUSY; \
  624. val = simple_strtoul(buf, &cp, 0); \
  625. if (*cp && (*cp != '\n')) \
  626. return -EINVAL; \
  627. i->f->set_rport_##field(rport, val); \
  628. return count; \
  629. }
  630. #define fc_rport_rd_attr(field, format_string, sz) \
  631. fc_rport_show_function(field, format_string, sz, ) \
  632. static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
  633. show_fc_rport_##field, NULL)
  634. #define fc_rport_rd_attr_cast(field, format_string, sz, cast) \
  635. fc_rport_show_function(field, format_string, sz, (cast)) \
  636. static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
  637. show_fc_rport_##field, NULL)
  638. #define fc_rport_rw_attr(field, format_string, sz) \
  639. fc_rport_show_function(field, format_string, sz, ) \
  640. fc_rport_store_function(field) \
  641. static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO | S_IWUSR, \
  642. show_fc_rport_##field, \
  643. store_fc_rport_##field)
  644. #define fc_private_rport_show_function(field, format_string, sz, cast) \
  645. static ssize_t \
  646. show_fc_rport_##field (struct class_device *cdev, char *buf) \
  647. { \
  648. struct fc_rport *rport = transport_class_to_rport(cdev); \
  649. return snprintf(buf, sz, format_string, cast rport->field); \
  650. }
  651. #define fc_private_rport_rd_attr(field, format_string, sz) \
  652. fc_private_rport_show_function(field, format_string, sz, ) \
  653. static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
  654. show_fc_rport_##field, NULL)
  655. #define fc_private_rport_rd_attr_cast(field, format_string, sz, cast) \
  656. fc_private_rport_show_function(field, format_string, sz, (cast)) \
  657. static FC_CLASS_DEVICE_ATTR(rport, field, S_IRUGO, \
  658. show_fc_rport_##field, NULL)
  659. #define fc_private_rport_rd_enum_attr(title, maxlen) \
  660. static ssize_t \
  661. show_fc_rport_##title (struct class_device *cdev, char *buf) \
  662. { \
  663. struct fc_rport *rport = transport_class_to_rport(cdev); \
  664. const char *name; \
  665. name = get_fc_##title##_name(rport->title); \
  666. if (!name) \
  667. return -EINVAL; \
  668. return snprintf(buf, maxlen, "%s\n", name); \
  669. } \
  670. static FC_CLASS_DEVICE_ATTR(rport, title, S_IRUGO, \
  671. show_fc_rport_##title, NULL)
  672. #define SETUP_RPORT_ATTRIBUTE_RD(field) \
  673. i->private_rport_attrs[count] = class_device_attr_rport_##field; \
  674. i->private_rport_attrs[count].attr.mode = S_IRUGO; \
  675. i->private_rport_attrs[count].store = NULL; \
  676. i->rport_attrs[count] = &i->private_rport_attrs[count]; \
  677. if (i->f->show_rport_##field) \
  678. count++
  679. #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(field) \
  680. i->private_rport_attrs[count] = class_device_attr_rport_##field; \
  681. i->private_rport_attrs[count].attr.mode = S_IRUGO; \
  682. i->private_rport_attrs[count].store = NULL; \
  683. i->rport_attrs[count] = &i->private_rport_attrs[count]; \
  684. count++
  685. #define SETUP_RPORT_ATTRIBUTE_RW(field) \
  686. i->private_rport_attrs[count] = class_device_attr_rport_##field; \
  687. if (!i->f->set_rport_##field) { \
  688. i->private_rport_attrs[count].attr.mode = S_IRUGO; \
  689. i->private_rport_attrs[count].store = NULL; \
  690. } \
  691. i->rport_attrs[count] = &i->private_rport_attrs[count]; \
  692. if (i->f->show_rport_##field) \
  693. count++
  694. #define SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(field) \
  695. { \
  696. i->private_rport_attrs[count] = class_device_attr_rport_##field; \
  697. i->rport_attrs[count] = &i->private_rport_attrs[count]; \
  698. count++; \
  699. }
  700. /* The FC Transport Remote Port Attributes: */
  701. /* Fixed Remote Port Attributes */
  702. fc_private_rport_rd_attr(maxframe_size, "%u bytes\n", 20);
  703. static ssize_t
  704. show_fc_rport_supported_classes (struct class_device *cdev, char *buf)
  705. {
  706. struct fc_rport *rport = transport_class_to_rport(cdev);
  707. if (rport->supported_classes == FC_COS_UNSPECIFIED)
  708. return snprintf(buf, 20, "unspecified\n");
  709. return get_fc_cos_names(rport->supported_classes, buf);
  710. }
  711. static FC_CLASS_DEVICE_ATTR(rport, supported_classes, S_IRUGO,
  712. show_fc_rport_supported_classes, NULL);
  713. /* Dynamic Remote Port Attributes */
  714. /*
  715. * dev_loss_tmo attribute
  716. */
  717. fc_rport_show_function(dev_loss_tmo, "%d\n", 20, )
  718. static ssize_t
  719. store_fc_rport_dev_loss_tmo(struct class_device *cdev, const char *buf,
  720. size_t count)
  721. {
  722. int val;
  723. struct fc_rport *rport = transport_class_to_rport(cdev);
  724. struct Scsi_Host *shost = rport_to_shost(rport);
  725. struct fc_internal *i = to_fc_internal(shost->transportt);
  726. char *cp;
  727. if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
  728. (rport->port_state == FC_PORTSTATE_DELETED) ||
  729. (rport->port_state == FC_PORTSTATE_NOTPRESENT))
  730. return -EBUSY;
  731. val = simple_strtoul(buf, &cp, 0);
  732. if ((*cp && (*cp != '\n')) ||
  733. (val < 0) || (val > SCSI_DEVICE_BLOCK_MAX_TIMEOUT))
  734. return -EINVAL;
  735. i->f->set_rport_dev_loss_tmo(rport, val);
  736. return count;
  737. }
  738. static FC_CLASS_DEVICE_ATTR(rport, dev_loss_tmo, S_IRUGO | S_IWUSR,
  739. show_fc_rport_dev_loss_tmo, store_fc_rport_dev_loss_tmo);
  740. /* Private Remote Port Attributes */
  741. fc_private_rport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
  742. fc_private_rport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
  743. fc_private_rport_rd_attr(port_id, "0x%06x\n", 20);
  744. static ssize_t
  745. show_fc_rport_roles (struct class_device *cdev, char *buf)
  746. {
  747. struct fc_rport *rport = transport_class_to_rport(cdev);
  748. /* identify any roles that are port_id specific */
  749. if ((rport->port_id != -1) &&
  750. (rport->port_id & FC_WELLKNOWN_PORTID_MASK) ==
  751. FC_WELLKNOWN_PORTID_MASK) {
  752. switch (rport->port_id & FC_WELLKNOWN_ROLE_MASK) {
  753. case FC_FPORT_PORTID:
  754. return snprintf(buf, 30, "Fabric Port\n");
  755. case FC_FABCTLR_PORTID:
  756. return snprintf(buf, 30, "Fabric Controller\n");
  757. case FC_DIRSRVR_PORTID:
  758. return snprintf(buf, 30, "Directory Server\n");
  759. case FC_TIMESRVR_PORTID:
  760. return snprintf(buf, 30, "Time Server\n");
  761. case FC_MGMTSRVR_PORTID:
  762. return snprintf(buf, 30, "Management Server\n");
  763. default:
  764. return snprintf(buf, 30, "Unknown Fabric Entity\n");
  765. }
  766. } else {
  767. if (rport->roles == FC_PORT_ROLE_UNKNOWN)
  768. return snprintf(buf, 20, "unknown\n");
  769. return get_fc_port_roles_names(rport->roles, buf);
  770. }
  771. }
  772. static FC_CLASS_DEVICE_ATTR(rport, roles, S_IRUGO,
  773. show_fc_rport_roles, NULL);
  774. fc_private_rport_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
  775. fc_private_rport_rd_attr(scsi_target_id, "%d\n", 20);
  776. /*
  777. * fast_io_fail_tmo attribute
  778. */
  779. static ssize_t
  780. show_fc_rport_fast_io_fail_tmo (struct class_device *cdev, char *buf)
  781. {
  782. struct fc_rport *rport = transport_class_to_rport(cdev);
  783. if (rport->fast_io_fail_tmo == -1)
  784. return snprintf(buf, 5, "off\n");
  785. return snprintf(buf, 20, "%d\n", rport->fast_io_fail_tmo);
  786. }
  787. static ssize_t
  788. store_fc_rport_fast_io_fail_tmo(struct class_device *cdev, const char *buf,
  789. size_t count)
  790. {
  791. int val;
  792. char *cp;
  793. struct fc_rport *rport = transport_class_to_rport(cdev);
  794. if ((rport->port_state == FC_PORTSTATE_BLOCKED) ||
  795. (rport->port_state == FC_PORTSTATE_DELETED) ||
  796. (rport->port_state == FC_PORTSTATE_NOTPRESENT))
  797. return -EBUSY;
  798. if (strncmp(buf, "off", 3) == 0)
  799. rport->fast_io_fail_tmo = -1;
  800. else {
  801. val = simple_strtoul(buf, &cp, 0);
  802. if ((*cp && (*cp != '\n')) ||
  803. (val < 0) || (val >= rport->dev_loss_tmo))
  804. return -EINVAL;
  805. rport->fast_io_fail_tmo = val;
  806. }
  807. return count;
  808. }
  809. static FC_CLASS_DEVICE_ATTR(rport, fast_io_fail_tmo, S_IRUGO | S_IWUSR,
  810. show_fc_rport_fast_io_fail_tmo, store_fc_rport_fast_io_fail_tmo);
  811. /*
  812. * FC SCSI Target Attribute Management
  813. */
  814. /*
  815. * Note: in the target show function we recognize when the remote
  816. * port is in the heirarchy and do not allow the driver to get
  817. * involved in sysfs functions. The driver only gets involved if
  818. * it's the "old" style that doesn't use rports.
  819. */
  820. #define fc_starget_show_function(field, format_string, sz, cast) \
  821. static ssize_t \
  822. show_fc_starget_##field (struct class_device *cdev, char *buf) \
  823. { \
  824. struct scsi_target *starget = transport_class_to_starget(cdev); \
  825. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); \
  826. struct fc_internal *i = to_fc_internal(shost->transportt); \
  827. struct fc_rport *rport = starget_to_rport(starget); \
  828. if (rport) \
  829. fc_starget_##field(starget) = rport->field; \
  830. else if (i->f->get_starget_##field) \
  831. i->f->get_starget_##field(starget); \
  832. return snprintf(buf, sz, format_string, \
  833. cast fc_starget_##field(starget)); \
  834. }
  835. #define fc_starget_rd_attr(field, format_string, sz) \
  836. fc_starget_show_function(field, format_string, sz, ) \
  837. static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
  838. show_fc_starget_##field, NULL)
  839. #define fc_starget_rd_attr_cast(field, format_string, sz, cast) \
  840. fc_starget_show_function(field, format_string, sz, (cast)) \
  841. static FC_CLASS_DEVICE_ATTR(starget, field, S_IRUGO, \
  842. show_fc_starget_##field, NULL)
  843. #define SETUP_STARGET_ATTRIBUTE_RD(field) \
  844. i->private_starget_attrs[count] = class_device_attr_starget_##field; \
  845. i->private_starget_attrs[count].attr.mode = S_IRUGO; \
  846. i->private_starget_attrs[count].store = NULL; \
  847. i->starget_attrs[count] = &i->private_starget_attrs[count]; \
  848. if (i->f->show_starget_##field) \
  849. count++
  850. #define SETUP_STARGET_ATTRIBUTE_RW(field) \
  851. i->private_starget_attrs[count] = class_device_attr_starget_##field; \
  852. if (!i->f->set_starget_##field) { \
  853. i->private_starget_attrs[count].attr.mode = S_IRUGO; \
  854. i->private_starget_attrs[count].store = NULL; \
  855. } \
  856. i->starget_attrs[count] = &i->private_starget_attrs[count]; \
  857. if (i->f->show_starget_##field) \
  858. count++
  859. /* The FC Transport SCSI Target Attributes: */
  860. fc_starget_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
  861. fc_starget_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
  862. fc_starget_rd_attr(port_id, "0x%06x\n", 20);
  863. /*
  864. * FC Virtual Port Attribute Management
  865. */
  866. #define fc_vport_show_function(field, format_string, sz, cast) \
  867. static ssize_t \
  868. show_fc_vport_##field (struct class_device *cdev, char *buf) \
  869. { \
  870. struct fc_vport *vport = transport_class_to_vport(cdev); \
  871. struct Scsi_Host *shost = vport_to_shost(vport); \
  872. struct fc_internal *i = to_fc_internal(shost->transportt); \
  873. if ((i->f->get_vport_##field) && \
  874. !(vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))) \
  875. i->f->get_vport_##field(vport); \
  876. return snprintf(buf, sz, format_string, cast vport->field); \
  877. }
  878. #define fc_vport_store_function(field) \
  879. static ssize_t \
  880. store_fc_vport_##field(struct class_device *cdev, const char *buf, \
  881. size_t count) \
  882. { \
  883. int val; \
  884. struct fc_vport *vport = transport_class_to_vport(cdev); \
  885. struct Scsi_Host *shost = vport_to_shost(vport); \
  886. struct fc_internal *i = to_fc_internal(shost->transportt); \
  887. char *cp; \
  888. if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
  889. return -EBUSY; \
  890. val = simple_strtoul(buf, &cp, 0); \
  891. if (*cp && (*cp != '\n')) \
  892. return -EINVAL; \
  893. i->f->set_vport_##field(vport, val); \
  894. return count; \
  895. }
  896. #define fc_vport_store_str_function(field, slen) \
  897. static ssize_t \
  898. store_fc_vport_##field(struct class_device *cdev, const char *buf, \
  899. size_t count) \
  900. { \
  901. struct fc_vport *vport = transport_class_to_vport(cdev); \
  902. struct Scsi_Host *shost = vport_to_shost(vport); \
  903. struct fc_internal *i = to_fc_internal(shost->transportt); \
  904. unsigned int cnt=count; \
  905. \
  906. /* count may include a LF at end of string */ \
  907. if (buf[cnt-1] == '\n') \
  908. cnt--; \
  909. if (cnt > ((slen) - 1)) \
  910. return -EINVAL; \
  911. memcpy(vport->field, buf, cnt); \
  912. i->f->set_vport_##field(vport); \
  913. return count; \
  914. }
  915. #define fc_vport_rd_attr(field, format_string, sz) \
  916. fc_vport_show_function(field, format_string, sz, ) \
  917. static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO, \
  918. show_fc_vport_##field, NULL)
  919. #define fc_vport_rd_attr_cast(field, format_string, sz, cast) \
  920. fc_vport_show_function(field, format_string, sz, (cast)) \
  921. static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO, \
  922. show_fc_vport_##field, NULL)
  923. #define fc_vport_rw_attr(field, format_string, sz) \
  924. fc_vport_show_function(field, format_string, sz, ) \
  925. fc_vport_store_function(field) \
  926. static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
  927. show_fc_vport_##field, \
  928. store_fc_vport_##field)
  929. #define fc_private_vport_show_function(field, format_string, sz, cast) \
  930. static ssize_t \
  931. show_fc_vport_##field (struct class_device *cdev, char *buf) \
  932. { \
  933. struct fc_vport *vport = transport_class_to_vport(cdev); \
  934. return snprintf(buf, sz, format_string, cast vport->field); \
  935. }
  936. #define fc_private_vport_store_u32_function(field) \
  937. static ssize_t \
  938. store_fc_vport_##field(struct class_device *cdev, const char *buf, \
  939. size_t count) \
  940. { \
  941. u32 val; \
  942. struct fc_vport *vport = transport_class_to_vport(cdev); \
  943. char *cp; \
  944. if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) \
  945. return -EBUSY; \
  946. val = simple_strtoul(buf, &cp, 0); \
  947. if (*cp && (*cp != '\n')) \
  948. return -EINVAL; \
  949. vport->field = val; \
  950. return count; \
  951. }
  952. #define fc_private_vport_rd_attr(field, format_string, sz) \
  953. fc_private_vport_show_function(field, format_string, sz, ) \
  954. static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO, \
  955. show_fc_vport_##field, NULL)
  956. #define fc_private_vport_rd_attr_cast(field, format_string, sz, cast) \
  957. fc_private_vport_show_function(field, format_string, sz, (cast)) \
  958. static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO, \
  959. show_fc_vport_##field, NULL)
  960. #define fc_private_vport_rw_u32_attr(field, format_string, sz) \
  961. fc_private_vport_show_function(field, format_string, sz, ) \
  962. fc_private_vport_store_u32_function(field) \
  963. static FC_CLASS_DEVICE_ATTR(vport, field, S_IRUGO | S_IWUSR, \
  964. show_fc_vport_##field, \
  965. store_fc_vport_##field)
  966. #define fc_private_vport_rd_enum_attr(title, maxlen) \
  967. static ssize_t \
  968. show_fc_vport_##title (struct class_device *cdev, char *buf) \
  969. { \
  970. struct fc_vport *vport = transport_class_to_vport(cdev); \
  971. const char *name; \
  972. name = get_fc_##title##_name(vport->title); \
  973. if (!name) \
  974. return -EINVAL; \
  975. return snprintf(buf, maxlen, "%s\n", name); \
  976. } \
  977. static FC_CLASS_DEVICE_ATTR(vport, title, S_IRUGO, \
  978. show_fc_vport_##title, NULL)
  979. #define SETUP_VPORT_ATTRIBUTE_RD(field) \
  980. i->private_vport_attrs[count] = class_device_attr_vport_##field; \
  981. i->private_vport_attrs[count].attr.mode = S_IRUGO; \
  982. i->private_vport_attrs[count].store = NULL; \
  983. i->vport_attrs[count] = &i->private_vport_attrs[count]; \
  984. if (i->f->get_##field) \
  985. count++
  986. /* NOTE: Above MACRO differs: checks function not show bit */
  987. #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(field) \
  988. i->private_vport_attrs[count] = class_device_attr_vport_##field; \
  989. i->private_vport_attrs[count].attr.mode = S_IRUGO; \
  990. i->private_vport_attrs[count].store = NULL; \
  991. i->vport_attrs[count] = &i->private_vport_attrs[count]; \
  992. count++
  993. #define SETUP_VPORT_ATTRIBUTE_WR(field) \
  994. i->private_vport_attrs[count] = class_device_attr_vport_##field; \
  995. i->vport_attrs[count] = &i->private_vport_attrs[count]; \
  996. if (i->f->field) \
  997. count++
  998. /* NOTE: Above MACRO differs: checks function */
  999. #define SETUP_VPORT_ATTRIBUTE_RW(field) \
  1000. i->private_vport_attrs[count] = class_device_attr_vport_##field; \
  1001. if (!i->f->set_vport_##field) { \
  1002. i->private_vport_attrs[count].attr.mode = S_IRUGO; \
  1003. i->private_vport_attrs[count].store = NULL; \
  1004. } \
  1005. i->vport_attrs[count] = &i->private_vport_attrs[count]; \
  1006. count++
  1007. /* NOTE: Above MACRO differs: does not check show bit */
  1008. #define SETUP_PRIVATE_VPORT_ATTRIBUTE_RW(field) \
  1009. { \
  1010. i->private_vport_attrs[count] = class_device_attr_vport_##field; \
  1011. i->vport_attrs[count] = &i->private_vport_attrs[count]; \
  1012. count++; \
  1013. }
  1014. /* The FC Transport Virtual Port Attributes: */
  1015. /* Fixed Virtual Port Attributes */
  1016. /* Dynamic Virtual Port Attributes */
  1017. /* Private Virtual Port Attributes */
  1018. fc_private_vport_rd_enum_attr(vport_state, FC_VPORTSTATE_MAX_NAMELEN);
  1019. fc_private_vport_rd_enum_attr(vport_last_state, FC_VPORTSTATE_MAX_NAMELEN);
  1020. fc_private_vport_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
  1021. fc_private_vport_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
  1022. static ssize_t
  1023. show_fc_vport_roles (struct class_device *cdev, char *buf)
  1024. {
  1025. struct fc_vport *vport = transport_class_to_vport(cdev);
  1026. if (vport->roles == FC_PORT_ROLE_UNKNOWN)
  1027. return snprintf(buf, 20, "unknown\n");
  1028. return get_fc_port_roles_names(vport->roles, buf);
  1029. }
  1030. static FC_CLASS_DEVICE_ATTR(vport, roles, S_IRUGO, show_fc_vport_roles, NULL);
  1031. fc_private_vport_rd_enum_attr(vport_type, FC_PORTTYPE_MAX_NAMELEN);
  1032. fc_private_vport_show_function(symbolic_name, "%s\n",
  1033. FC_VPORT_SYMBOLIC_NAMELEN + 1, )
  1034. fc_vport_store_str_function(symbolic_name, FC_VPORT_SYMBOLIC_NAMELEN)
  1035. static FC_CLASS_DEVICE_ATTR(vport, symbolic_name, S_IRUGO | S_IWUSR,
  1036. show_fc_vport_symbolic_name, store_fc_vport_symbolic_name);
  1037. static ssize_t
  1038. store_fc_vport_delete(struct class_device *cdev, const char *buf,
  1039. size_t count)
  1040. {
  1041. struct fc_vport *vport = transport_class_to_vport(cdev);
  1042. struct Scsi_Host *shost = vport_to_shost(vport);
  1043. fc_queue_work(shost, &vport->vport_delete_work);
  1044. return count;
  1045. }
  1046. static FC_CLASS_DEVICE_ATTR(vport, vport_delete, S_IWUSR,
  1047. NULL, store_fc_vport_delete);
  1048. /*
  1049. * Enable/Disable vport
  1050. * Write "1" to disable, write "0" to enable
  1051. */
  1052. static ssize_t
  1053. store_fc_vport_disable(struct class_device *cdev, const char *buf,
  1054. size_t count)
  1055. {
  1056. struct fc_vport *vport = transport_class_to_vport(cdev);
  1057. struct Scsi_Host *shost = vport_to_shost(vport);
  1058. struct fc_internal *i = to_fc_internal(shost->transportt);
  1059. int stat;
  1060. if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING))
  1061. return -EBUSY;
  1062. if (*buf == '0') {
  1063. if (vport->vport_state != FC_VPORT_DISABLED)
  1064. return -EALREADY;
  1065. } else if (*buf == '1') {
  1066. if (vport->vport_state == FC_VPORT_DISABLED)
  1067. return -EALREADY;
  1068. } else
  1069. return -EINVAL;
  1070. stat = i->f->vport_disable(vport, ((*buf == '0') ? false : true));
  1071. return stat ? stat : count;
  1072. }
  1073. static FC_CLASS_DEVICE_ATTR(vport, vport_disable, S_IWUSR,
  1074. NULL, store_fc_vport_disable);
  1075. /*
  1076. * Host Attribute Management
  1077. */
  1078. #define fc_host_show_function(field, format_string, sz, cast) \
  1079. static ssize_t \
  1080. show_fc_host_##field (struct class_device *cdev, char *buf) \
  1081. { \
  1082. struct Scsi_Host *shost = transport_class_to_shost(cdev); \
  1083. struct fc_internal *i = to_fc_internal(shost->transportt); \
  1084. if (i->f->get_host_##field) \
  1085. i->f->get_host_##field(shost); \
  1086. return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
  1087. }
  1088. #define fc_host_store_function(field) \
  1089. static ssize_t \
  1090. store_fc_host_##field(struct class_device *cdev, const char *buf, \
  1091. size_t count) \
  1092. { \
  1093. int val; \
  1094. struct Scsi_Host *shost = transport_class_to_shost(cdev); \
  1095. struct fc_internal *i = to_fc_internal(shost->transportt); \
  1096. char *cp; \
  1097. \
  1098. val = simple_strtoul(buf, &cp, 0); \
  1099. if (*cp && (*cp != '\n')) \
  1100. return -EINVAL; \
  1101. i->f->set_host_##field(shost, val); \
  1102. return count; \
  1103. }
  1104. #define fc_host_store_str_function(field, slen) \
  1105. static ssize_t \
  1106. store_fc_host_##field(struct class_device *cdev, const char *buf, \
  1107. size_t count) \
  1108. { \
  1109. struct Scsi_Host *shost = transport_class_to_shost(cdev); \
  1110. struct fc_internal *i = to_fc_internal(shost->transportt); \
  1111. unsigned int cnt=count; \
  1112. \
  1113. /* count may include a LF at end of string */ \
  1114. if (buf[cnt-1] == '\n') \
  1115. cnt--; \
  1116. if (cnt > ((slen) - 1)) \
  1117. return -EINVAL; \
  1118. memcpy(fc_host_##field(shost), buf, cnt); \
  1119. i->f->set_host_##field(shost); \
  1120. return count; \
  1121. }
  1122. #define fc_host_rd_attr(field, format_string, sz) \
  1123. fc_host_show_function(field, format_string, sz, ) \
  1124. static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
  1125. show_fc_host_##field, NULL)
  1126. #define fc_host_rd_attr_cast(field, format_string, sz, cast) \
  1127. fc_host_show_function(field, format_string, sz, (cast)) \
  1128. static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
  1129. show_fc_host_##field, NULL)
  1130. #define fc_host_rw_attr(field, format_string, sz) \
  1131. fc_host_show_function(field, format_string, sz, ) \
  1132. fc_host_store_function(field) \
  1133. static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO | S_IWUSR, \
  1134. show_fc_host_##field, \
  1135. store_fc_host_##field)
  1136. #define fc_host_rd_enum_attr(title, maxlen) \
  1137. static ssize_t \
  1138. show_fc_host_##title (struct class_device *cdev, char *buf) \
  1139. { \
  1140. struct Scsi_Host *shost = transport_class_to_shost(cdev); \
  1141. struct fc_internal *i = to_fc_internal(shost->transportt); \
  1142. const char *name; \
  1143. if (i->f->get_host_##title) \
  1144. i->f->get_host_##title(shost); \
  1145. name = get_fc_##title##_name(fc_host_##title(shost)); \
  1146. if (!name) \
  1147. return -EINVAL; \
  1148. return snprintf(buf, maxlen, "%s\n", name); \
  1149. } \
  1150. static FC_CLASS_DEVICE_ATTR(host, title, S_IRUGO, show_fc_host_##title, NULL)
  1151. #define SETUP_HOST_ATTRIBUTE_RD(field) \
  1152. i->private_host_attrs[count] = class_device_attr_host_##field; \
  1153. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  1154. i->private_host_attrs[count].store = NULL; \
  1155. i->host_attrs[count] = &i->private_host_attrs[count]; \
  1156. if (i->f->show_host_##field) \
  1157. count++
  1158. #define SETUP_HOST_ATTRIBUTE_RD_NS(field) \
  1159. i->private_host_attrs[count] = class_device_attr_host_##field; \
  1160. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  1161. i->private_host_attrs[count].store = NULL; \
  1162. i->host_attrs[count] = &i->private_host_attrs[count]; \
  1163. count++
  1164. #define SETUP_HOST_ATTRIBUTE_RW(field) \
  1165. i->private_host_attrs[count] = class_device_attr_host_##field; \
  1166. if (!i->f->set_host_##field) { \
  1167. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  1168. i->private_host_attrs[count].store = NULL; \
  1169. } \
  1170. i->host_attrs[count] = &i->private_host_attrs[count]; \
  1171. if (i->f->show_host_##field) \
  1172. count++
  1173. #define fc_private_host_show_function(field, format_string, sz, cast) \
  1174. static ssize_t \
  1175. show_fc_host_##field (struct class_device *cdev, char *buf) \
  1176. { \
  1177. struct Scsi_Host *shost = transport_class_to_shost(cdev); \
  1178. return snprintf(buf, sz, format_string, cast fc_host_##field(shost)); \
  1179. }
  1180. #define fc_private_host_rd_attr(field, format_string, sz) \
  1181. fc_private_host_show_function(field, format_string, sz, ) \
  1182. static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
  1183. show_fc_host_##field, NULL)
  1184. #define fc_private_host_rd_attr_cast(field, format_string, sz, cast) \
  1185. fc_private_host_show_function(field, format_string, sz, (cast)) \
  1186. static FC_CLASS_DEVICE_ATTR(host, field, S_IRUGO, \
  1187. show_fc_host_##field, NULL)
  1188. #define SETUP_PRIVATE_HOST_ATTRIBUTE_RD(field) \
  1189. i->private_host_attrs[count] = class_device_attr_host_##field; \
  1190. i->private_host_attrs[count].attr.mode = S_IRUGO; \
  1191. i->private_host_attrs[count].store = NULL; \
  1192. i->host_attrs[count] = &i->private_host_attrs[count]; \
  1193. count++
  1194. #define SETUP_PRIVATE_HOST_ATTRIBUTE_RW(field) \
  1195. { \
  1196. i->private_host_attrs[count] = class_device_attr_host_##field; \
  1197. i->host_attrs[count] = &i->private_host_attrs[count]; \
  1198. count++; \
  1199. }
  1200. /* Fixed Host Attributes */
  1201. static ssize_t
  1202. show_fc_host_supported_classes (struct class_device *cdev, char *buf)
  1203. {
  1204. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1205. if (fc_host_supported_classes(shost) == FC_COS_UNSPECIFIED)
  1206. return snprintf(buf, 20, "unspecified\n");
  1207. return get_fc_cos_names(fc_host_supported_classes(shost), buf);
  1208. }
  1209. static FC_CLASS_DEVICE_ATTR(host, supported_classes, S_IRUGO,
  1210. show_fc_host_supported_classes, NULL);
  1211. static ssize_t
  1212. show_fc_host_supported_fc4s (struct class_device *cdev, char *buf)
  1213. {
  1214. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1215. return (ssize_t)show_fc_fc4s(buf, fc_host_supported_fc4s(shost));
  1216. }
  1217. static FC_CLASS_DEVICE_ATTR(host, supported_fc4s, S_IRUGO,
  1218. show_fc_host_supported_fc4s, NULL);
  1219. static ssize_t
  1220. show_fc_host_supported_speeds (struct class_device *cdev, char *buf)
  1221. {
  1222. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1223. if (fc_host_supported_speeds(shost) == FC_PORTSPEED_UNKNOWN)
  1224. return snprintf(buf, 20, "unknown\n");
  1225. return get_fc_port_speed_names(fc_host_supported_speeds(shost), buf);
  1226. }
  1227. static FC_CLASS_DEVICE_ATTR(host, supported_speeds, S_IRUGO,
  1228. show_fc_host_supported_speeds, NULL);
  1229. fc_private_host_rd_attr_cast(node_name, "0x%llx\n", 20, unsigned long long);
  1230. fc_private_host_rd_attr_cast(port_name, "0x%llx\n", 20, unsigned long long);
  1231. fc_private_host_rd_attr_cast(permanent_port_name, "0x%llx\n", 20,
  1232. unsigned long long);
  1233. fc_private_host_rd_attr(maxframe_size, "%u bytes\n", 20);
  1234. fc_private_host_rd_attr(max_npiv_vports, "%u\n", 20);
  1235. fc_private_host_rd_attr(serial_number, "%s\n", (FC_SERIAL_NUMBER_SIZE +1));
  1236. /* Dynamic Host Attributes */
  1237. static ssize_t
  1238. show_fc_host_active_fc4s (struct class_device *cdev, char *buf)
  1239. {
  1240. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1241. struct fc_internal *i = to_fc_internal(shost->transportt);
  1242. if (i->f->get_host_active_fc4s)
  1243. i->f->get_host_active_fc4s(shost);
  1244. return (ssize_t)show_fc_fc4s(buf, fc_host_active_fc4s(shost));
  1245. }
  1246. static FC_CLASS_DEVICE_ATTR(host, active_fc4s, S_IRUGO,
  1247. show_fc_host_active_fc4s, NULL);
  1248. static ssize_t
  1249. show_fc_host_speed (struct class_device *cdev, char *buf)
  1250. {
  1251. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1252. struct fc_internal *i = to_fc_internal(shost->transportt);
  1253. if (i->f->get_host_speed)
  1254. i->f->get_host_speed(shost);
  1255. if (fc_host_speed(shost) == FC_PORTSPEED_UNKNOWN)
  1256. return snprintf(buf, 20, "unknown\n");
  1257. return get_fc_port_speed_names(fc_host_speed(shost), buf);
  1258. }
  1259. static FC_CLASS_DEVICE_ATTR(host, speed, S_IRUGO,
  1260. show_fc_host_speed, NULL);
  1261. fc_host_rd_attr(port_id, "0x%06x\n", 20);
  1262. fc_host_rd_enum_attr(port_type, FC_PORTTYPE_MAX_NAMELEN);
  1263. fc_host_rd_enum_attr(port_state, FC_PORTSTATE_MAX_NAMELEN);
  1264. fc_host_rd_attr_cast(fabric_name, "0x%llx\n", 20, unsigned long long);
  1265. fc_host_rd_attr(symbolic_name, "%s\n", FC_SYMBOLIC_NAME_SIZE + 1);
  1266. fc_private_host_show_function(system_hostname, "%s\n",
  1267. FC_SYMBOLIC_NAME_SIZE + 1, )
  1268. fc_host_store_str_function(system_hostname, FC_SYMBOLIC_NAME_SIZE)
  1269. static FC_CLASS_DEVICE_ATTR(host, system_hostname, S_IRUGO | S_IWUSR,
  1270. show_fc_host_system_hostname, store_fc_host_system_hostname);
  1271. /* Private Host Attributes */
  1272. static ssize_t
  1273. show_fc_private_host_tgtid_bind_type(struct class_device *cdev, char *buf)
  1274. {
  1275. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1276. const char *name;
  1277. name = get_fc_tgtid_bind_type_name(fc_host_tgtid_bind_type(shost));
  1278. if (!name)
  1279. return -EINVAL;
  1280. return snprintf(buf, FC_BINDTYPE_MAX_NAMELEN, "%s\n", name);
  1281. }
  1282. #define get_list_head_entry(pos, head, member) \
  1283. pos = list_entry((head)->next, typeof(*pos), member)
  1284. static ssize_t
  1285. store_fc_private_host_tgtid_bind_type(struct class_device *cdev,
  1286. const char *buf, size_t count)
  1287. {
  1288. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1289. struct fc_rport *rport;
  1290. enum fc_tgtid_binding_type val;
  1291. unsigned long flags;
  1292. if (get_fc_tgtid_bind_type_match(buf, &val))
  1293. return -EINVAL;
  1294. /* if changing bind type, purge all unused consistent bindings */
  1295. if (val != fc_host_tgtid_bind_type(shost)) {
  1296. spin_lock_irqsave(shost->host_lock, flags);
  1297. while (!list_empty(&fc_host_rport_bindings(shost))) {
  1298. get_list_head_entry(rport,
  1299. &fc_host_rport_bindings(shost), peers);
  1300. list_del(&rport->peers);
  1301. rport->port_state = FC_PORTSTATE_DELETED;
  1302. fc_queue_work(shost, &rport->rport_delete_work);
  1303. }
  1304. spin_unlock_irqrestore(shost->host_lock, flags);
  1305. }
  1306. fc_host_tgtid_bind_type(shost) = val;
  1307. return count;
  1308. }
  1309. static FC_CLASS_DEVICE_ATTR(host, tgtid_bind_type, S_IRUGO | S_IWUSR,
  1310. show_fc_private_host_tgtid_bind_type,
  1311. store_fc_private_host_tgtid_bind_type);
  1312. static ssize_t
  1313. store_fc_private_host_issue_lip(struct class_device *cdev,
  1314. const char *buf, size_t count)
  1315. {
  1316. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1317. struct fc_internal *i = to_fc_internal(shost->transportt);
  1318. int ret;
  1319. /* ignore any data value written to the attribute */
  1320. if (i->f->issue_fc_host_lip) {
  1321. ret = i->f->issue_fc_host_lip(shost);
  1322. return ret ? ret: count;
  1323. }
  1324. return -ENOENT;
  1325. }
  1326. static FC_CLASS_DEVICE_ATTR(host, issue_lip, S_IWUSR, NULL,
  1327. store_fc_private_host_issue_lip);
  1328. fc_private_host_rd_attr(npiv_vports_inuse, "%u\n", 20);
  1329. /*
  1330. * Host Statistics Management
  1331. */
  1332. /* Show a given an attribute in the statistics group */
  1333. static ssize_t
  1334. fc_stat_show(const struct class_device *cdev, char *buf, unsigned long offset)
  1335. {
  1336. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1337. struct fc_internal *i = to_fc_internal(shost->transportt);
  1338. struct fc_host_statistics *stats;
  1339. ssize_t ret = -ENOENT;
  1340. if (offset > sizeof(struct fc_host_statistics) ||
  1341. offset % sizeof(u64) != 0)
  1342. WARN_ON(1);
  1343. if (i->f->get_fc_host_stats) {
  1344. stats = (i->f->get_fc_host_stats)(shost);
  1345. if (stats)
  1346. ret = snprintf(buf, 20, "0x%llx\n",
  1347. (unsigned long long)*(u64 *)(((u8 *) stats) + offset));
  1348. }
  1349. return ret;
  1350. }
  1351. /* generate a read-only statistics attribute */
  1352. #define fc_host_statistic(name) \
  1353. static ssize_t show_fcstat_##name(struct class_device *cd, char *buf) \
  1354. { \
  1355. return fc_stat_show(cd, buf, \
  1356. offsetof(struct fc_host_statistics, name)); \
  1357. } \
  1358. static FC_CLASS_DEVICE_ATTR(host, name, S_IRUGO, show_fcstat_##name, NULL)
  1359. fc_host_statistic(seconds_since_last_reset);
  1360. fc_host_statistic(tx_frames);
  1361. fc_host_statistic(tx_words);
  1362. fc_host_statistic(rx_frames);
  1363. fc_host_statistic(rx_words);
  1364. fc_host_statistic(lip_count);
  1365. fc_host_statistic(nos_count);
  1366. fc_host_statistic(error_frames);
  1367. fc_host_statistic(dumped_frames);
  1368. fc_host_statistic(link_failure_count);
  1369. fc_host_statistic(loss_of_sync_count);
  1370. fc_host_statistic(loss_of_signal_count);
  1371. fc_host_statistic(prim_seq_protocol_err_count);
  1372. fc_host_statistic(invalid_tx_word_count);
  1373. fc_host_statistic(invalid_crc_count);
  1374. fc_host_statistic(fcp_input_requests);
  1375. fc_host_statistic(fcp_output_requests);
  1376. fc_host_statistic(fcp_control_requests);
  1377. fc_host_statistic(fcp_input_megabytes);
  1378. fc_host_statistic(fcp_output_megabytes);
  1379. static ssize_t
  1380. fc_reset_statistics(struct class_device *cdev, const char *buf,
  1381. size_t count)
  1382. {
  1383. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1384. struct fc_internal *i = to_fc_internal(shost->transportt);
  1385. /* ignore any data value written to the attribute */
  1386. if (i->f->reset_fc_host_stats) {
  1387. i->f->reset_fc_host_stats(shost);
  1388. return count;
  1389. }
  1390. return -ENOENT;
  1391. }
  1392. static FC_CLASS_DEVICE_ATTR(host, reset_statistics, S_IWUSR, NULL,
  1393. fc_reset_statistics);
  1394. static struct attribute *fc_statistics_attrs[] = {
  1395. &class_device_attr_host_seconds_since_last_reset.attr,
  1396. &class_device_attr_host_tx_frames.attr,
  1397. &class_device_attr_host_tx_words.attr,
  1398. &class_device_attr_host_rx_frames.attr,
  1399. &class_device_attr_host_rx_words.attr,
  1400. &class_device_attr_host_lip_count.attr,
  1401. &class_device_attr_host_nos_count.attr,
  1402. &class_device_attr_host_error_frames.attr,
  1403. &class_device_attr_host_dumped_frames.attr,
  1404. &class_device_attr_host_link_failure_count.attr,
  1405. &class_device_attr_host_loss_of_sync_count.attr,
  1406. &class_device_attr_host_loss_of_signal_count.attr,
  1407. &class_device_attr_host_prim_seq_protocol_err_count.attr,
  1408. &class_device_attr_host_invalid_tx_word_count.attr,
  1409. &class_device_attr_host_invalid_crc_count.attr,
  1410. &class_device_attr_host_fcp_input_requests.attr,
  1411. &class_device_attr_host_fcp_output_requests.attr,
  1412. &class_device_attr_host_fcp_control_requests.attr,
  1413. &class_device_attr_host_fcp_input_megabytes.attr,
  1414. &class_device_attr_host_fcp_output_megabytes.attr,
  1415. &class_device_attr_host_reset_statistics.attr,
  1416. NULL
  1417. };
  1418. static struct attribute_group fc_statistics_group = {
  1419. .name = "statistics",
  1420. .attrs = fc_statistics_attrs,
  1421. };
  1422. /* Host Vport Attributes */
  1423. static int
  1424. fc_parse_wwn(const char *ns, u64 *nm)
  1425. {
  1426. unsigned int i, j;
  1427. u8 wwn[8];
  1428. memset(wwn, 0, sizeof(wwn));
  1429. /* Validate and store the new name */
  1430. for (i=0, j=0; i < 16; i++) {
  1431. if ((*ns >= 'a') && (*ns <= 'f'))
  1432. j = ((j << 4) | ((*ns++ -'a') + 10));
  1433. else if ((*ns >= 'A') && (*ns <= 'F'))
  1434. j = ((j << 4) | ((*ns++ -'A') + 10));
  1435. else if ((*ns >= '0') && (*ns <= '9'))
  1436. j = ((j << 4) | (*ns++ -'0'));
  1437. else
  1438. return -EINVAL;
  1439. if (i % 2) {
  1440. wwn[i/2] = j & 0xff;
  1441. j = 0;
  1442. }
  1443. }
  1444. *nm = wwn_to_u64(wwn);
  1445. return 0;
  1446. }
  1447. /*
  1448. * "Short-cut" sysfs variable to create a new vport on a FC Host.
  1449. * Input is a string of the form "<WWPN>:<WWNN>". Other attributes
  1450. * will default to a NPIV-based FCP_Initiator; The WWNs are specified
  1451. * as hex characters, and may *not* contain any prefixes (e.g. 0x, x, etc)
  1452. */
  1453. static ssize_t
  1454. store_fc_host_vport_create(struct class_device *cdev, const char *buf,
  1455. size_t count)
  1456. {
  1457. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1458. struct fc_vport_identifiers vid;
  1459. struct fc_vport *vport;
  1460. unsigned int cnt=count;
  1461. int stat;
  1462. memset(&vid, 0, sizeof(vid));
  1463. /* count may include a LF at end of string */
  1464. if (buf[cnt-1] == '\n')
  1465. cnt--;
  1466. /* validate we have enough characters for WWPN */
  1467. if ((cnt != (16+1+16)) || (buf[16] != ':'))
  1468. return -EINVAL;
  1469. stat = fc_parse_wwn(&buf[0], &vid.port_name);
  1470. if (stat)
  1471. return stat;
  1472. stat = fc_parse_wwn(&buf[17], &vid.node_name);
  1473. if (stat)
  1474. return stat;
  1475. vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
  1476. vid.vport_type = FC_PORTTYPE_NPIV;
  1477. /* vid.symbolic_name is already zero/NULL's */
  1478. vid.disable = false; /* always enabled */
  1479. /* we only allow support on Channel 0 !!! */
  1480. stat = fc_vport_create(shost, 0, &shost->shost_gendev, &vid, &vport);
  1481. return stat ? stat : count;
  1482. }
  1483. static FC_CLASS_DEVICE_ATTR(host, vport_create, S_IWUSR, NULL,
  1484. store_fc_host_vport_create);
  1485. /*
  1486. * "Short-cut" sysfs variable to delete a vport on a FC Host.
  1487. * Vport is identified by a string containing "<WWPN>:<WWNN>".
  1488. * The WWNs are specified as hex characters, and may *not* contain
  1489. * any prefixes (e.g. 0x, x, etc)
  1490. */
  1491. static ssize_t
  1492. store_fc_host_vport_delete(struct class_device *cdev, const char *buf,
  1493. size_t count)
  1494. {
  1495. struct Scsi_Host *shost = transport_class_to_shost(cdev);
  1496. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  1497. struct fc_vport *vport;
  1498. u64 wwpn, wwnn;
  1499. unsigned long flags;
  1500. unsigned int cnt=count;
  1501. int stat, match;
  1502. /* count may include a LF at end of string */
  1503. if (buf[cnt-1] == '\n')
  1504. cnt--;
  1505. /* validate we have enough characters for WWPN */
  1506. if ((cnt != (16+1+16)) || (buf[16] != ':'))
  1507. return -EINVAL;
  1508. stat = fc_parse_wwn(&buf[0], &wwpn);
  1509. if (stat)
  1510. return stat;
  1511. stat = fc_parse_wwn(&buf[17], &wwnn);
  1512. if (stat)
  1513. return stat;
  1514. spin_lock_irqsave(shost->host_lock, flags);
  1515. match = 0;
  1516. /* we only allow support on Channel 0 !!! */
  1517. list_for_each_entry(vport, &fc_host->vports, peers) {
  1518. if ((vport->channel == 0) &&
  1519. (vport->port_name == wwpn) && (vport->node_name == wwnn)) {
  1520. match = 1;
  1521. break;
  1522. }
  1523. }
  1524. spin_unlock_irqrestore(shost->host_lock, flags);
  1525. if (!match)
  1526. return -ENODEV;
  1527. stat = fc_vport_terminate(vport);
  1528. return stat ? stat : count;
  1529. }
  1530. static FC_CLASS_DEVICE_ATTR(host, vport_delete, S_IWUSR, NULL,
  1531. store_fc_host_vport_delete);
  1532. static int fc_host_match(struct attribute_container *cont,
  1533. struct device *dev)
  1534. {
  1535. struct Scsi_Host *shost;
  1536. struct fc_internal *i;
  1537. if (!scsi_is_host_device(dev))
  1538. return 0;
  1539. shost = dev_to_shost(dev);
  1540. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1541. != &fc_host_class.class)
  1542. return 0;
  1543. i = to_fc_internal(shost->transportt);
  1544. return &i->t.host_attrs.ac == cont;
  1545. }
  1546. static int fc_target_match(struct attribute_container *cont,
  1547. struct device *dev)
  1548. {
  1549. struct Scsi_Host *shost;
  1550. struct fc_internal *i;
  1551. if (!scsi_is_target_device(dev))
  1552. return 0;
  1553. shost = dev_to_shost(dev->parent);
  1554. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1555. != &fc_host_class.class)
  1556. return 0;
  1557. i = to_fc_internal(shost->transportt);
  1558. return &i->t.target_attrs.ac == cont;
  1559. }
  1560. static void fc_rport_dev_release(struct device *dev)
  1561. {
  1562. struct fc_rport *rport = dev_to_rport(dev);
  1563. put_device(dev->parent);
  1564. kfree(rport);
  1565. }
  1566. int scsi_is_fc_rport(const struct device *dev)
  1567. {
  1568. return dev->release == fc_rport_dev_release;
  1569. }
  1570. EXPORT_SYMBOL(scsi_is_fc_rport);
  1571. static int fc_rport_match(struct attribute_container *cont,
  1572. struct device *dev)
  1573. {
  1574. struct Scsi_Host *shost;
  1575. struct fc_internal *i;
  1576. if (!scsi_is_fc_rport(dev))
  1577. return 0;
  1578. shost = dev_to_shost(dev->parent);
  1579. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1580. != &fc_host_class.class)
  1581. return 0;
  1582. i = to_fc_internal(shost->transportt);
  1583. return &i->rport_attr_cont.ac == cont;
  1584. }
  1585. static void fc_vport_dev_release(struct device *dev)
  1586. {
  1587. struct fc_vport *vport = dev_to_vport(dev);
  1588. put_device(dev->parent); /* release kobj parent */
  1589. kfree(vport);
  1590. }
  1591. int scsi_is_fc_vport(const struct device *dev)
  1592. {
  1593. return dev->release == fc_vport_dev_release;
  1594. }
  1595. EXPORT_SYMBOL(scsi_is_fc_vport);
  1596. static int fc_vport_match(struct attribute_container *cont,
  1597. struct device *dev)
  1598. {
  1599. struct fc_vport *vport;
  1600. struct Scsi_Host *shost;
  1601. struct fc_internal *i;
  1602. if (!scsi_is_fc_vport(dev))
  1603. return 0;
  1604. vport = dev_to_vport(dev);
  1605. shost = vport_to_shost(vport);
  1606. if (!shost->transportt || shost->transportt->host_attrs.ac.class
  1607. != &fc_host_class.class)
  1608. return 0;
  1609. i = to_fc_internal(shost->transportt);
  1610. return &i->vport_attr_cont.ac == cont;
  1611. }
  1612. /**
  1613. * fc_timed_out - FC Transport I/O timeout intercept handler
  1614. *
  1615. * @scmd: The SCSI command which timed out
  1616. *
  1617. * This routine protects against error handlers getting invoked while a
  1618. * rport is in a blocked state, typically due to a temporarily loss of
  1619. * connectivity. If the error handlers are allowed to proceed, requests
  1620. * to abort i/o, reset the target, etc will likely fail as there is no way
  1621. * to communicate with the device to perform the requested function. These
  1622. * failures may result in the midlayer taking the device offline, requiring
  1623. * manual intervention to restore operation.
  1624. *
  1625. * This routine, called whenever an i/o times out, validates the state of
  1626. * the underlying rport. If the rport is blocked, it returns
  1627. * EH_RESET_TIMER, which will continue to reschedule the timeout.
  1628. * Eventually, either the device will return, or devloss_tmo will fire,
  1629. * and when the timeout then fires, it will be handled normally.
  1630. * If the rport is not blocked, normal error handling continues.
  1631. *
  1632. * Notes:
  1633. * This routine assumes no locks are held on entry.
  1634. **/
  1635. static enum scsi_eh_timer_return
  1636. fc_timed_out(struct scsi_cmnd *scmd)
  1637. {
  1638. struct fc_rport *rport = starget_to_rport(scsi_target(scmd->device));
  1639. if (rport->port_state == FC_PORTSTATE_BLOCKED)
  1640. return EH_RESET_TIMER;
  1641. return EH_NOT_HANDLED;
  1642. }
  1643. /*
  1644. * Must be called with shost->host_lock held
  1645. */
  1646. static int fc_user_scan(struct Scsi_Host *shost, uint channel,
  1647. uint id, uint lun)
  1648. {
  1649. struct fc_rport *rport;
  1650. list_for_each_entry(rport, &fc_host_rports(shost), peers) {
  1651. if (rport->scsi_target_id == -1)
  1652. continue;
  1653. if (rport->port_state != FC_PORTSTATE_ONLINE)
  1654. continue;
  1655. if ((channel == SCAN_WILD_CARD || channel == rport->channel) &&
  1656. (id == SCAN_WILD_CARD || id == rport->scsi_target_id)) {
  1657. scsi_scan_target(&rport->dev, rport->channel,
  1658. rport->scsi_target_id, lun, 1);
  1659. }
  1660. }
  1661. return 0;
  1662. }
  1663. struct scsi_transport_template *
  1664. fc_attach_transport(struct fc_function_template *ft)
  1665. {
  1666. int count;
  1667. struct fc_internal *i = kzalloc(sizeof(struct fc_internal),
  1668. GFP_KERNEL);
  1669. if (unlikely(!i))
  1670. return NULL;
  1671. i->t.target_attrs.ac.attrs = &i->starget_attrs[0];
  1672. i->t.target_attrs.ac.class = &fc_transport_class.class;
  1673. i->t.target_attrs.ac.match = fc_target_match;
  1674. i->t.target_size = sizeof(struct fc_starget_attrs);
  1675. transport_container_register(&i->t.target_attrs);
  1676. i->t.host_attrs.ac.attrs = &i->host_attrs[0];
  1677. i->t.host_attrs.ac.class = &fc_host_class.class;
  1678. i->t.host_attrs.ac.match = fc_host_match;
  1679. i->t.host_size = sizeof(struct fc_host_attrs);
  1680. if (ft->get_fc_host_stats)
  1681. i->t.host_attrs.statistics = &fc_statistics_group;
  1682. transport_container_register(&i->t.host_attrs);
  1683. i->rport_attr_cont.ac.attrs = &i->rport_attrs[0];
  1684. i->rport_attr_cont.ac.class = &fc_rport_class.class;
  1685. i->rport_attr_cont.ac.match = fc_rport_match;
  1686. transport_container_register(&i->rport_attr_cont);
  1687. i->vport_attr_cont.ac.attrs = &i->vport_attrs[0];
  1688. i->vport_attr_cont.ac.class = &fc_vport_class.class;
  1689. i->vport_attr_cont.ac.match = fc_vport_match;
  1690. transport_container_register(&i->vport_attr_cont);
  1691. i->f = ft;
  1692. /* Transport uses the shost workq for scsi scanning */
  1693. i->t.create_work_queue = 1;
  1694. i->t.eh_timed_out = fc_timed_out;
  1695. i->t.user_scan = fc_user_scan;
  1696. /*
  1697. * Setup SCSI Target Attributes.
  1698. */
  1699. count = 0;
  1700. SETUP_STARGET_ATTRIBUTE_RD(node_name);
  1701. SETUP_STARGET_ATTRIBUTE_RD(port_name);
  1702. SETUP_STARGET_ATTRIBUTE_RD(port_id);
  1703. BUG_ON(count > FC_STARGET_NUM_ATTRS);
  1704. i->starget_attrs[count] = NULL;
  1705. /*
  1706. * Setup SCSI Host Attributes.
  1707. */
  1708. count=0;
  1709. SETUP_HOST_ATTRIBUTE_RD(node_name);
  1710. SETUP_HOST_ATTRIBUTE_RD(port_name);
  1711. SETUP_HOST_ATTRIBUTE_RD(permanent_port_name);
  1712. SETUP_HOST_ATTRIBUTE_RD(supported_classes);
  1713. SETUP_HOST_ATTRIBUTE_RD(supported_fc4s);
  1714. SETUP_HOST_ATTRIBUTE_RD(supported_speeds);
  1715. SETUP_HOST_ATTRIBUTE_RD(maxframe_size);
  1716. if (ft->vport_create) {
  1717. SETUP_HOST_ATTRIBUTE_RD_NS(max_npiv_vports);
  1718. SETUP_HOST_ATTRIBUTE_RD_NS(npiv_vports_inuse);
  1719. }
  1720. SETUP_HOST_ATTRIBUTE_RD(serial_number);
  1721. SETUP_HOST_ATTRIBUTE_RD(port_id);
  1722. SETUP_HOST_ATTRIBUTE_RD(port_type);
  1723. SETUP_HOST_ATTRIBUTE_RD(port_state);
  1724. SETUP_HOST_ATTRIBUTE_RD(active_fc4s);
  1725. SETUP_HOST_ATTRIBUTE_RD(speed);
  1726. SETUP_HOST_ATTRIBUTE_RD(fabric_name);
  1727. SETUP_HOST_ATTRIBUTE_RD(symbolic_name);
  1728. SETUP_HOST_ATTRIBUTE_RW(system_hostname);
  1729. /* Transport-managed attributes */
  1730. SETUP_PRIVATE_HOST_ATTRIBUTE_RW(tgtid_bind_type);
  1731. if (ft->issue_fc_host_lip)
  1732. SETUP_PRIVATE_HOST_ATTRIBUTE_RW(issue_lip);
  1733. if (ft->vport_create)
  1734. SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_create);
  1735. if (ft->vport_delete)
  1736. SETUP_PRIVATE_HOST_ATTRIBUTE_RW(vport_delete);
  1737. BUG_ON(count > FC_HOST_NUM_ATTRS);
  1738. i->host_attrs[count] = NULL;
  1739. /*
  1740. * Setup Remote Port Attributes.
  1741. */
  1742. count=0;
  1743. SETUP_RPORT_ATTRIBUTE_RD(maxframe_size);
  1744. SETUP_RPORT_ATTRIBUTE_RD(supported_classes);
  1745. SETUP_RPORT_ATTRIBUTE_RW(dev_loss_tmo);
  1746. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(node_name);
  1747. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_name);
  1748. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_id);
  1749. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(roles);
  1750. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(port_state);
  1751. SETUP_PRIVATE_RPORT_ATTRIBUTE_RD(scsi_target_id);
  1752. if (ft->terminate_rport_io)
  1753. SETUP_PRIVATE_RPORT_ATTRIBUTE_RW(fast_io_fail_tmo);
  1754. BUG_ON(count > FC_RPORT_NUM_ATTRS);
  1755. i->rport_attrs[count] = NULL;
  1756. /*
  1757. * Setup Virtual Port Attributes.
  1758. */
  1759. count=0;
  1760. SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_state);
  1761. SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_last_state);
  1762. SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(node_name);
  1763. SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(port_name);
  1764. SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(roles);
  1765. SETUP_PRIVATE_VPORT_ATTRIBUTE_RD(vport_type);
  1766. SETUP_VPORT_ATTRIBUTE_RW(symbolic_name);
  1767. SETUP_VPORT_ATTRIBUTE_WR(vport_delete);
  1768. SETUP_VPORT_ATTRIBUTE_WR(vport_disable);
  1769. BUG_ON(count > FC_VPORT_NUM_ATTRS);
  1770. i->vport_attrs[count] = NULL;
  1771. return &i->t;
  1772. }
  1773. EXPORT_SYMBOL(fc_attach_transport);
  1774. void fc_release_transport(struct scsi_transport_template *t)
  1775. {
  1776. struct fc_internal *i = to_fc_internal(t);
  1777. transport_container_unregister(&i->t.target_attrs);
  1778. transport_container_unregister(&i->t.host_attrs);
  1779. transport_container_unregister(&i->rport_attr_cont);
  1780. transport_container_unregister(&i->vport_attr_cont);
  1781. kfree(i);
  1782. }
  1783. EXPORT_SYMBOL(fc_release_transport);
  1784. /**
  1785. * fc_queue_work - Queue work to the fc_host workqueue.
  1786. * @shost: Pointer to Scsi_Host bound to fc_host.
  1787. * @work: Work to queue for execution.
  1788. *
  1789. * Return value:
  1790. * 1 - work queued for execution
  1791. * 0 - work is already queued
  1792. * -EINVAL - work queue doesn't exist
  1793. **/
  1794. static int
  1795. fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
  1796. {
  1797. if (unlikely(!fc_host_work_q(shost))) {
  1798. printk(KERN_ERR
  1799. "ERROR: FC host '%s' attempted to queue work, "
  1800. "when no workqueue created.\n", shost->hostt->name);
  1801. dump_stack();
  1802. return -EINVAL;
  1803. }
  1804. return queue_work(fc_host_work_q(shost), work);
  1805. }
  1806. /**
  1807. * fc_flush_work - Flush a fc_host's workqueue.
  1808. * @shost: Pointer to Scsi_Host bound to fc_host.
  1809. **/
  1810. static void
  1811. fc_flush_work(struct Scsi_Host *shost)
  1812. {
  1813. if (!fc_host_work_q(shost)) {
  1814. printk(KERN_ERR
  1815. "ERROR: FC host '%s' attempted to flush work, "
  1816. "when no workqueue created.\n", shost->hostt->name);
  1817. dump_stack();
  1818. return;
  1819. }
  1820. flush_workqueue(fc_host_work_q(shost));
  1821. }
  1822. /**
  1823. * fc_queue_devloss_work - Schedule work for the fc_host devloss workqueue.
  1824. * @shost: Pointer to Scsi_Host bound to fc_host.
  1825. * @work: Work to queue for execution.
  1826. * @delay: jiffies to delay the work queuing
  1827. *
  1828. * Return value:
  1829. * 1 on success / 0 already queued / < 0 for error
  1830. **/
  1831. static int
  1832. fc_queue_devloss_work(struct Scsi_Host *shost, struct delayed_work *work,
  1833. unsigned long delay)
  1834. {
  1835. if (unlikely(!fc_host_devloss_work_q(shost))) {
  1836. printk(KERN_ERR
  1837. "ERROR: FC host '%s' attempted to queue work, "
  1838. "when no workqueue created.\n", shost->hostt->name);
  1839. dump_stack();
  1840. return -EINVAL;
  1841. }
  1842. return queue_delayed_work(fc_host_devloss_work_q(shost), work, delay);
  1843. }
  1844. /**
  1845. * fc_flush_devloss - Flush a fc_host's devloss workqueue.
  1846. * @shost: Pointer to Scsi_Host bound to fc_host.
  1847. **/
  1848. static void
  1849. fc_flush_devloss(struct Scsi_Host *shost)
  1850. {
  1851. if (!fc_host_devloss_work_q(shost)) {
  1852. printk(KERN_ERR
  1853. "ERROR: FC host '%s' attempted to flush work, "
  1854. "when no workqueue created.\n", shost->hostt->name);
  1855. dump_stack();
  1856. return;
  1857. }
  1858. flush_workqueue(fc_host_devloss_work_q(shost));
  1859. }
  1860. /**
  1861. * fc_remove_host - called to terminate any fc_transport-related elements
  1862. * for a scsi host.
  1863. * @rport: remote port to be unblocked.
  1864. *
  1865. * This routine is expected to be called immediately preceeding the
  1866. * a driver's call to scsi_remove_host().
  1867. *
  1868. * WARNING: A driver utilizing the fc_transport, which fails to call
  1869. * this routine prior to scsi_remote_host(), will leave dangling
  1870. * objects in /sys/class/fc_remote_ports. Access to any of these
  1871. * objects can result in a system crash !!!
  1872. *
  1873. * Notes:
  1874. * This routine assumes no locks are held on entry.
  1875. **/
  1876. void
  1877. fc_remove_host(struct Scsi_Host *shost)
  1878. {
  1879. struct fc_vport *vport = NULL, *next_vport = NULL;
  1880. struct fc_rport *rport = NULL, *next_rport = NULL;
  1881. struct workqueue_struct *work_q;
  1882. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  1883. unsigned long flags;
  1884. spin_lock_irqsave(shost->host_lock, flags);
  1885. /* Remove any vports */
  1886. list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers)
  1887. fc_queue_work(shost, &vport->vport_delete_work);
  1888. /* Remove any remote ports */
  1889. list_for_each_entry_safe(rport, next_rport,
  1890. &fc_host->rports, peers) {
  1891. list_del(&rport->peers);
  1892. rport->port_state = FC_PORTSTATE_DELETED;
  1893. fc_queue_work(shost, &rport->rport_delete_work);
  1894. }
  1895. list_for_each_entry_safe(rport, next_rport,
  1896. &fc_host->rport_bindings, peers) {
  1897. list_del(&rport->peers);
  1898. rport->port_state = FC_PORTSTATE_DELETED;
  1899. fc_queue_work(shost, &rport->rport_delete_work);
  1900. }
  1901. spin_unlock_irqrestore(shost->host_lock, flags);
  1902. /* flush all scan work items */
  1903. scsi_flush_work(shost);
  1904. /* flush all stgt delete, and rport delete work items, then kill it */
  1905. if (fc_host->work_q) {
  1906. work_q = fc_host->work_q;
  1907. fc_host->work_q = NULL;
  1908. destroy_workqueue(work_q);
  1909. }
  1910. /* flush all devloss work items, then kill it */
  1911. if (fc_host->devloss_work_q) {
  1912. work_q = fc_host->devloss_work_q;
  1913. fc_host->devloss_work_q = NULL;
  1914. destroy_workqueue(work_q);
  1915. }
  1916. }
  1917. EXPORT_SYMBOL(fc_remove_host);
  1918. /**
  1919. * fc_starget_delete - called to delete the scsi decendents of an rport
  1920. * (target and all sdevs)
  1921. *
  1922. * @work: remote port to be operated on.
  1923. **/
  1924. static void
  1925. fc_starget_delete(struct work_struct *work)
  1926. {
  1927. struct fc_rport *rport =
  1928. container_of(work, struct fc_rport, stgt_delete_work);
  1929. struct Scsi_Host *shost = rport_to_shost(rport);
  1930. struct fc_internal *i = to_fc_internal(shost->transportt);
  1931. /* Involve the LLDD if possible to terminate all io on the rport. */
  1932. if (i->f->terminate_rport_io)
  1933. i->f->terminate_rport_io(rport);
  1934. scsi_remove_target(&rport->dev);
  1935. }
  1936. /**
  1937. * fc_rport_final_delete - finish rport termination and delete it.
  1938. *
  1939. * @work: remote port to be deleted.
  1940. **/
  1941. static void
  1942. fc_rport_final_delete(struct work_struct *work)
  1943. {
  1944. struct fc_rport *rport =
  1945. container_of(work, struct fc_rport, rport_delete_work);
  1946. struct device *dev = &rport->dev;
  1947. struct Scsi_Host *shost = rport_to_shost(rport);
  1948. struct fc_internal *i = to_fc_internal(shost->transportt);
  1949. unsigned long flags;
  1950. /*
  1951. * if a scan is pending, flush the SCSI Host work_q so that
  1952. * that we can reclaim the rport scan work element.
  1953. */
  1954. if (rport->flags & FC_RPORT_SCAN_PENDING)
  1955. scsi_flush_work(shost);
  1956. /* involve the LLDD to terminate all pending i/o */
  1957. if (i->f->terminate_rport_io)
  1958. i->f->terminate_rport_io(rport);
  1959. /*
  1960. * Cancel any outstanding timers. These should really exist
  1961. * only when rmmod'ing the LLDD and we're asking for
  1962. * immediate termination of the rports
  1963. */
  1964. spin_lock_irqsave(shost->host_lock, flags);
  1965. if (rport->flags & FC_RPORT_DEVLOSS_PENDING) {
  1966. spin_unlock_irqrestore(shost->host_lock, flags);
  1967. if (!cancel_delayed_work(&rport->fail_io_work))
  1968. fc_flush_devloss(shost);
  1969. if (!cancel_delayed_work(&rport->dev_loss_work))
  1970. fc_flush_devloss(shost);
  1971. spin_lock_irqsave(shost->host_lock, flags);
  1972. rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
  1973. }
  1974. spin_unlock_irqrestore(shost->host_lock, flags);
  1975. /* Delete SCSI target and sdevs */
  1976. if (rport->scsi_target_id != -1)
  1977. fc_starget_delete(&rport->stgt_delete_work);
  1978. /*
  1979. * Notify the driver that the rport is now dead. The LLDD will
  1980. * also guarantee that any communication to the rport is terminated
  1981. */
  1982. if (i->f->dev_loss_tmo_callbk)
  1983. i->f->dev_loss_tmo_callbk(rport);
  1984. transport_remove_device(dev);
  1985. device_del(dev);
  1986. transport_destroy_device(dev);
  1987. put_device(&shost->shost_gendev); /* for fc_host->rport list */
  1988. put_device(dev); /* for self-reference */
  1989. }
  1990. /**
  1991. * fc_rport_create - allocates and creates a remote FC port.
  1992. * @shost: scsi host the remote port is connected to.
  1993. * @channel: Channel on shost port connected to.
  1994. * @ids: The world wide names, fc address, and FC4 port
  1995. * roles for the remote port.
  1996. *
  1997. * Allocates and creates the remoter port structure, including the
  1998. * class and sysfs creation.
  1999. *
  2000. * Notes:
  2001. * This routine assumes no locks are held on entry.
  2002. **/
  2003. static struct fc_rport *
  2004. fc_rport_create(struct Scsi_Host *shost, int channel,
  2005. struct fc_rport_identifiers *ids)
  2006. {
  2007. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  2008. struct fc_internal *fci = to_fc_internal(shost->transportt);
  2009. struct fc_rport *rport;
  2010. struct device *dev;
  2011. unsigned long flags;
  2012. int error;
  2013. size_t size;
  2014. size = (sizeof(struct fc_rport) + fci->f->dd_fcrport_size);
  2015. rport = kzalloc(size, GFP_KERNEL);
  2016. if (unlikely(!rport)) {
  2017. printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
  2018. return NULL;
  2019. }
  2020. rport->maxframe_size = -1;
  2021. rport->supported_classes = FC_COS_UNSPECIFIED;
  2022. rport->dev_loss_tmo = fc_dev_loss_tmo;
  2023. memcpy(&rport->node_name, &ids->node_name, sizeof(rport->node_name));
  2024. memcpy(&rport->port_name, &ids->port_name, sizeof(rport->port_name));
  2025. rport->port_id = ids->port_id;
  2026. rport->roles = ids->roles;
  2027. rport->port_state = FC_PORTSTATE_ONLINE;
  2028. if (fci->f->dd_fcrport_size)
  2029. rport->dd_data = &rport[1];
  2030. rport->channel = channel;
  2031. rport->fast_io_fail_tmo = -1;
  2032. INIT_DELAYED_WORK(&rport->dev_loss_work, fc_timeout_deleted_rport);
  2033. INIT_DELAYED_WORK(&rport->fail_io_work, fc_timeout_fail_rport_io);
  2034. INIT_WORK(&rport->scan_work, fc_scsi_scan_rport);
  2035. INIT_WORK(&rport->stgt_delete_work, fc_starget_delete);
  2036. INIT_WORK(&rport->rport_delete_work, fc_rport_final_delete);
  2037. spin_lock_irqsave(shost->host_lock, flags);
  2038. rport->number = fc_host->next_rport_number++;
  2039. if (rport->roles & FC_PORT_ROLE_FCP_TARGET)
  2040. rport->scsi_target_id = fc_host->next_target_id++;
  2041. else
  2042. rport->scsi_target_id = -1;
  2043. list_add_tail(&rport->peers, &fc_host->rports);
  2044. get_device(&shost->shost_gendev); /* for fc_host->rport list */
  2045. spin_unlock_irqrestore(shost->host_lock, flags);
  2046. dev = &rport->dev;
  2047. device_initialize(dev); /* takes self reference */
  2048. dev->parent = get_device(&shost->shost_gendev); /* parent reference */
  2049. dev->release = fc_rport_dev_release;
  2050. sprintf(dev->bus_id, "rport-%d:%d-%d",
  2051. shost->host_no, channel, rport->number);
  2052. transport_setup_device(dev);
  2053. error = device_add(dev);
  2054. if (error) {
  2055. printk(KERN_ERR "FC Remote Port device_add failed\n");
  2056. goto delete_rport;
  2057. }
  2058. transport_add_device(dev);
  2059. transport_configure_device(dev);
  2060. if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
  2061. /* initiate a scan of the target */
  2062. rport->flags |= FC_RPORT_SCAN_PENDING;
  2063. scsi_queue_work(shost, &rport->scan_work);
  2064. }
  2065. return rport;
  2066. delete_rport:
  2067. transport_destroy_device(dev);
  2068. spin_lock_irqsave(shost->host_lock, flags);
  2069. list_del(&rport->peers);
  2070. put_device(&shost->shost_gendev); /* for fc_host->rport list */
  2071. spin_unlock_irqrestore(shost->host_lock, flags);
  2072. put_device(dev->parent);
  2073. kfree(rport);
  2074. return NULL;
  2075. }
  2076. /**
  2077. * fc_remote_port_add - notifies the fc transport of the existence
  2078. * of a remote FC port.
  2079. * @shost: scsi host the remote port is connected to.
  2080. * @channel: Channel on shost port connected to.
  2081. * @ids: The world wide names, fc address, and FC4 port
  2082. * roles for the remote port.
  2083. *
  2084. * The LLDD calls this routine to notify the transport of the existence
  2085. * of a remote port. The LLDD provides the unique identifiers (wwpn,wwn)
  2086. * of the port, it's FC address (port_id), and the FC4 roles that are
  2087. * active for the port.
  2088. *
  2089. * For ports that are FCP targets (aka scsi targets), the FC transport
  2090. * maintains consistent target id bindings on behalf of the LLDD.
  2091. * A consistent target id binding is an assignment of a target id to
  2092. * a remote port identifier, which persists while the scsi host is
  2093. * attached. The remote port can disappear, then later reappear, and
  2094. * it's target id assignment remains the same. This allows for shifts
  2095. * in FC addressing (if binding by wwpn or wwnn) with no apparent
  2096. * changes to the scsi subsystem which is based on scsi host number and
  2097. * target id values. Bindings are only valid during the attachment of
  2098. * the scsi host. If the host detaches, then later re-attaches, target
  2099. * id bindings may change.
  2100. *
  2101. * This routine is responsible for returning a remote port structure.
  2102. * The routine will search the list of remote ports it maintains
  2103. * internally on behalf of consistent target id mappings. If found, the
  2104. * remote port structure will be reused. Otherwise, a new remote port
  2105. * structure will be allocated.
  2106. *
  2107. * Whenever a remote port is allocated, a new fc_remote_port class
  2108. * device is created.
  2109. *
  2110. * Should not be called from interrupt context.
  2111. *
  2112. * Notes:
  2113. * This routine assumes no locks are held on entry.
  2114. **/
  2115. struct fc_rport *
  2116. fc_remote_port_add(struct Scsi_Host *shost, int channel,
  2117. struct fc_rport_identifiers *ids)
  2118. {
  2119. struct fc_internal *fci = to_fc_internal(shost->transportt);
  2120. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  2121. struct fc_rport *rport;
  2122. unsigned long flags;
  2123. int match = 0;
  2124. /* ensure any stgt delete functions are done */
  2125. fc_flush_work(shost);
  2126. /*
  2127. * Search the list of "active" rports, for an rport that has been
  2128. * deleted, but we've held off the real delete while the target
  2129. * is in a "blocked" state.
  2130. */
  2131. spin_lock_irqsave(shost->host_lock, flags);
  2132. list_for_each_entry(rport, &fc_host->rports, peers) {
  2133. if ((rport->port_state == FC_PORTSTATE_BLOCKED) &&
  2134. (rport->channel == channel)) {
  2135. switch (fc_host->tgtid_bind_type) {
  2136. case FC_TGTID_BIND_BY_WWPN:
  2137. case FC_TGTID_BIND_NONE:
  2138. if (rport->port_name == ids->port_name)
  2139. match = 1;
  2140. break;
  2141. case FC_TGTID_BIND_BY_WWNN:
  2142. if (rport->node_name == ids->node_name)
  2143. match = 1;
  2144. break;
  2145. case FC_TGTID_BIND_BY_ID:
  2146. if (rport->port_id == ids->port_id)
  2147. match = 1;
  2148. break;
  2149. }
  2150. if (match) {
  2151. memcpy(&rport->node_name, &ids->node_name,
  2152. sizeof(rport->node_name));
  2153. memcpy(&rport->port_name, &ids->port_name,
  2154. sizeof(rport->port_name));
  2155. rport->port_id = ids->port_id;
  2156. rport->port_state = FC_PORTSTATE_ONLINE;
  2157. rport->roles = ids->roles;
  2158. spin_unlock_irqrestore(shost->host_lock, flags);
  2159. if (fci->f->dd_fcrport_size)
  2160. memset(rport->dd_data, 0,
  2161. fci->f->dd_fcrport_size);
  2162. /*
  2163. * If we were not a target, cancel the
  2164. * io terminate and rport timers, and
  2165. * we're done.
  2166. *
  2167. * If we were a target, but our new role
  2168. * doesn't indicate a target, leave the
  2169. * timers running expecting the role to
  2170. * change as the target fully logs in. If
  2171. * it doesn't, the target will be torn down.
  2172. *
  2173. * If we were a target, and our role shows
  2174. * we're still a target, cancel the timers
  2175. * and kick off a scan.
  2176. */
  2177. /* was a target, not in roles */
  2178. if ((rport->scsi_target_id != -1) &&
  2179. (!(ids->roles & FC_PORT_ROLE_FCP_TARGET)))
  2180. return rport;
  2181. /*
  2182. * Stop the fail io and dev_loss timers.
  2183. * If they flush, the port_state will
  2184. * be checked and will NOOP the function.
  2185. */
  2186. if (!cancel_delayed_work(&rport->fail_io_work))
  2187. fc_flush_devloss(shost);
  2188. if (!cancel_delayed_work(&rport->dev_loss_work))
  2189. fc_flush_devloss(shost);
  2190. spin_lock_irqsave(shost->host_lock, flags);
  2191. rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
  2192. /* if target, initiate a scan */
  2193. if (rport->scsi_target_id != -1) {
  2194. rport->flags |= FC_RPORT_SCAN_PENDING;
  2195. scsi_queue_work(shost,
  2196. &rport->scan_work);
  2197. spin_unlock_irqrestore(shost->host_lock,
  2198. flags);
  2199. scsi_target_unblock(&rport->dev);
  2200. } else
  2201. spin_unlock_irqrestore(shost->host_lock,
  2202. flags);
  2203. return rport;
  2204. }
  2205. }
  2206. }
  2207. /*
  2208. * Search the bindings array
  2209. * Note: if never a FCP target, you won't be on this list
  2210. */
  2211. if (fc_host->tgtid_bind_type != FC_TGTID_BIND_NONE) {
  2212. /* search for a matching consistent binding */
  2213. list_for_each_entry(rport, &fc_host->rport_bindings,
  2214. peers) {
  2215. if (rport->channel != channel)
  2216. continue;
  2217. switch (fc_host->tgtid_bind_type) {
  2218. case FC_TGTID_BIND_BY_WWPN:
  2219. if (rport->port_name == ids->port_name)
  2220. match = 1;
  2221. break;
  2222. case FC_TGTID_BIND_BY_WWNN:
  2223. if (rport->node_name == ids->node_name)
  2224. match = 1;
  2225. break;
  2226. case FC_TGTID_BIND_BY_ID:
  2227. if (rport->port_id == ids->port_id)
  2228. match = 1;
  2229. break;
  2230. case FC_TGTID_BIND_NONE: /* to keep compiler happy */
  2231. break;
  2232. }
  2233. if (match) {
  2234. list_move_tail(&rport->peers, &fc_host->rports);
  2235. break;
  2236. }
  2237. }
  2238. if (match) {
  2239. memcpy(&rport->node_name, &ids->node_name,
  2240. sizeof(rport->node_name));
  2241. memcpy(&rport->port_name, &ids->port_name,
  2242. sizeof(rport->port_name));
  2243. rport->port_id = ids->port_id;
  2244. rport->roles = ids->roles;
  2245. rport->port_state = FC_PORTSTATE_ONLINE;
  2246. if (fci->f->dd_fcrport_size)
  2247. memset(rport->dd_data, 0,
  2248. fci->f->dd_fcrport_size);
  2249. if (rport->roles & FC_PORT_ROLE_FCP_TARGET) {
  2250. /* initiate a scan of the target */
  2251. rport->flags |= FC_RPORT_SCAN_PENDING;
  2252. scsi_queue_work(shost, &rport->scan_work);
  2253. spin_unlock_irqrestore(shost->host_lock, flags);
  2254. scsi_target_unblock(&rport->dev);
  2255. } else
  2256. spin_unlock_irqrestore(shost->host_lock, flags);
  2257. return rport;
  2258. }
  2259. }
  2260. spin_unlock_irqrestore(shost->host_lock, flags);
  2261. /* No consistent binding found - create new remote port entry */
  2262. rport = fc_rport_create(shost, channel, ids);
  2263. return rport;
  2264. }
  2265. EXPORT_SYMBOL(fc_remote_port_add);
  2266. /**
  2267. * fc_remote_port_delete - notifies the fc transport that a remote
  2268. * port is no longer in existence.
  2269. * @rport: The remote port that no longer exists
  2270. *
  2271. * The LLDD calls this routine to notify the transport that a remote
  2272. * port is no longer part of the topology. Note: Although a port
  2273. * may no longer be part of the topology, it may persist in the remote
  2274. * ports displayed by the fc_host. We do this under 2 conditions:
  2275. * - If the port was a scsi target, we delay its deletion by "blocking" it.
  2276. * This allows the port to temporarily disappear, then reappear without
  2277. * disrupting the SCSI device tree attached to it. During the "blocked"
  2278. * period the port will still exist.
  2279. * - If the port was a scsi target and disappears for longer than we
  2280. * expect, we'll delete the port and the tear down the SCSI device tree
  2281. * attached to it. However, we want to semi-persist the target id assigned
  2282. * to that port if it eventually does exist. The port structure will
  2283. * remain (although with minimal information) so that the target id
  2284. * bindings remails.
  2285. *
  2286. * If the remote port is not an FCP Target, it will be fully torn down
  2287. * and deallocated, including the fc_remote_port class device.
  2288. *
  2289. * If the remote port is an FCP Target, the port will be placed in a
  2290. * temporary blocked state. From the LLDD's perspective, the rport no
  2291. * longer exists. From the SCSI midlayer's perspective, the SCSI target
  2292. * exists, but all sdevs on it are blocked from further I/O. The following
  2293. * is then expected:
  2294. * If the remote port does not return (signaled by a LLDD call to
  2295. * fc_remote_port_add()) within the dev_loss_tmo timeout, then the
  2296. * scsi target is removed - killing all outstanding i/o and removing the
  2297. * scsi devices attached ot it. The port structure will be marked Not
  2298. * Present and be partially cleared, leaving only enough information to
  2299. * recognize the remote port relative to the scsi target id binding if
  2300. * it later appears. The port will remain as long as there is a valid
  2301. * binding (e.g. until the user changes the binding type or unloads the
  2302. * scsi host with the binding).
  2303. *
  2304. * If the remote port returns within the dev_loss_tmo value (and matches
  2305. * according to the target id binding type), the port structure will be
  2306. * reused. If it is no longer a SCSI target, the target will be torn
  2307. * down. If it continues to be a SCSI target, then the target will be
  2308. * unblocked (allowing i/o to be resumed), and a scan will be activated
  2309. * to ensure that all luns are detected.
  2310. *
  2311. * Called from normal process context only - cannot be called from interrupt.
  2312. *
  2313. * Notes:
  2314. * This routine assumes no locks are held on entry.
  2315. **/
  2316. void
  2317. fc_remote_port_delete(struct fc_rport *rport)
  2318. {
  2319. struct Scsi_Host *shost = rport_to_shost(rport);
  2320. struct fc_internal *i = to_fc_internal(shost->transportt);
  2321. int timeout = rport->dev_loss_tmo;
  2322. unsigned long flags;
  2323. /*
  2324. * No need to flush the fc_host work_q's, as all adds are synchronous.
  2325. *
  2326. * We do need to reclaim the rport scan work element, so eventually
  2327. * (in fc_rport_final_delete()) we'll flush the scsi host work_q if
  2328. * there's still a scan pending.
  2329. */
  2330. spin_lock_irqsave(shost->host_lock, flags);
  2331. if (rport->port_state != FC_PORTSTATE_ONLINE) {
  2332. spin_unlock_irqrestore(shost->host_lock, flags);
  2333. return;
  2334. }
  2335. /*
  2336. * In the past, we if this was not an FCP-Target, we would
  2337. * unconditionally just jump to deleting the rport.
  2338. * However, rports can be used as node containers by the LLDD,
  2339. * and its not appropriate to just terminate the rport at the
  2340. * first sign of a loss in connectivity. The LLDD may want to
  2341. * send ELS traffic to re-validate the login. If the rport is
  2342. * immediately deleted, it makes it inappropriate for a node
  2343. * container.
  2344. * So... we now unconditionally wait dev_loss_tmo before
  2345. * destroying an rport.
  2346. */
  2347. rport->port_state = FC_PORTSTATE_BLOCKED;
  2348. rport->flags |= FC_RPORT_DEVLOSS_PENDING;
  2349. spin_unlock_irqrestore(shost->host_lock, flags);
  2350. scsi_target_block(&rport->dev);
  2351. /* see if we need to kill io faster than waiting for device loss */
  2352. if ((rport->fast_io_fail_tmo != -1) &&
  2353. (rport->fast_io_fail_tmo < timeout) && (i->f->terminate_rport_io))
  2354. fc_queue_devloss_work(shost, &rport->fail_io_work,
  2355. rport->fast_io_fail_tmo * HZ);
  2356. /* cap the length the devices can be blocked until they are deleted */
  2357. fc_queue_devloss_work(shost, &rport->dev_loss_work, timeout * HZ);
  2358. }
  2359. EXPORT_SYMBOL(fc_remote_port_delete);
  2360. /**
  2361. * fc_remote_port_rolechg - notifies the fc transport that the roles
  2362. * on a remote may have changed.
  2363. * @rport: The remote port that changed.
  2364. *
  2365. * The LLDD calls this routine to notify the transport that the roles
  2366. * on a remote port may have changed. The largest effect of this is
  2367. * if a port now becomes a FCP Target, it must be allocated a
  2368. * scsi target id. If the port is no longer a FCP target, any
  2369. * scsi target id value assigned to it will persist in case the
  2370. * role changes back to include FCP Target. No changes in the scsi
  2371. * midlayer will be invoked if the role changes (in the expectation
  2372. * that the role will be resumed. If it doesn't normal error processing
  2373. * will take place).
  2374. *
  2375. * Should not be called from interrupt context.
  2376. *
  2377. * Notes:
  2378. * This routine assumes no locks are held on entry.
  2379. **/
  2380. void
  2381. fc_remote_port_rolechg(struct fc_rport *rport, u32 roles)
  2382. {
  2383. struct Scsi_Host *shost = rport_to_shost(rport);
  2384. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  2385. unsigned long flags;
  2386. int create = 0;
  2387. spin_lock_irqsave(shost->host_lock, flags);
  2388. if (roles & FC_PORT_ROLE_FCP_TARGET) {
  2389. if (rport->scsi_target_id == -1) {
  2390. rport->scsi_target_id = fc_host->next_target_id++;
  2391. create = 1;
  2392. } else if (!(rport->roles & FC_PORT_ROLE_FCP_TARGET))
  2393. create = 1;
  2394. }
  2395. rport->roles = roles;
  2396. spin_unlock_irqrestore(shost->host_lock, flags);
  2397. if (create) {
  2398. /*
  2399. * There may have been a delete timer running on the
  2400. * port. Ensure that it is cancelled as we now know
  2401. * the port is an FCP Target.
  2402. * Note: we know the rport is exists and in an online
  2403. * state as the LLDD would not have had an rport
  2404. * reference to pass us.
  2405. *
  2406. * Take no action on the del_timer failure as the state
  2407. * machine state change will validate the
  2408. * transaction.
  2409. */
  2410. if (!cancel_delayed_work(&rport->fail_io_work))
  2411. fc_flush_devloss(shost);
  2412. if (!cancel_delayed_work(&rport->dev_loss_work))
  2413. fc_flush_devloss(shost);
  2414. spin_lock_irqsave(shost->host_lock, flags);
  2415. rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
  2416. spin_unlock_irqrestore(shost->host_lock, flags);
  2417. /* ensure any stgt delete functions are done */
  2418. fc_flush_work(shost);
  2419. /* initiate a scan of the target */
  2420. spin_lock_irqsave(shost->host_lock, flags);
  2421. rport->flags |= FC_RPORT_SCAN_PENDING;
  2422. scsi_queue_work(shost, &rport->scan_work);
  2423. spin_unlock_irqrestore(shost->host_lock, flags);
  2424. scsi_target_unblock(&rport->dev);
  2425. }
  2426. }
  2427. EXPORT_SYMBOL(fc_remote_port_rolechg);
  2428. /**
  2429. * fc_timeout_deleted_rport - Timeout handler for a deleted remote port,
  2430. * which we blocked, and has now failed to return
  2431. * in the allotted time.
  2432. *
  2433. * @work: rport target that failed to reappear in the allotted time.
  2434. **/
  2435. static void
  2436. fc_timeout_deleted_rport(struct work_struct *work)
  2437. {
  2438. struct fc_rport *rport =
  2439. container_of(work, struct fc_rport, dev_loss_work.work);
  2440. struct Scsi_Host *shost = rport_to_shost(rport);
  2441. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  2442. unsigned long flags;
  2443. spin_lock_irqsave(shost->host_lock, flags);
  2444. rport->flags &= ~FC_RPORT_DEVLOSS_PENDING;
  2445. /*
  2446. * If the port is ONLINE, then it came back. If it was a SCSI
  2447. * target, validate it still is. If not, tear down the
  2448. * scsi_target on it.
  2449. */
  2450. if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
  2451. (rport->scsi_target_id != -1) &&
  2452. !(rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
  2453. dev_printk(KERN_ERR, &rport->dev,
  2454. "blocked FC remote port time out: no longer"
  2455. " a FCP target, removing starget\n");
  2456. spin_unlock_irqrestore(shost->host_lock, flags);
  2457. scsi_target_unblock(&rport->dev);
  2458. fc_queue_work(shost, &rport->stgt_delete_work);
  2459. return;
  2460. }
  2461. /* NOOP state - we're flushing workq's */
  2462. if (rport->port_state != FC_PORTSTATE_BLOCKED) {
  2463. spin_unlock_irqrestore(shost->host_lock, flags);
  2464. dev_printk(KERN_ERR, &rport->dev,
  2465. "blocked FC remote port time out: leaving"
  2466. " rport%s alone\n",
  2467. (rport->scsi_target_id != -1) ? " and starget" : "");
  2468. return;
  2469. }
  2470. if ((fc_host->tgtid_bind_type == FC_TGTID_BIND_NONE) ||
  2471. (rport->scsi_target_id == -1)) {
  2472. list_del(&rport->peers);
  2473. rport->port_state = FC_PORTSTATE_DELETED;
  2474. dev_printk(KERN_ERR, &rport->dev,
  2475. "blocked FC remote port time out: removing"
  2476. " rport%s\n",
  2477. (rport->scsi_target_id != -1) ? " and starget" : "");
  2478. fc_queue_work(shost, &rport->rport_delete_work);
  2479. spin_unlock_irqrestore(shost->host_lock, flags);
  2480. return;
  2481. }
  2482. dev_printk(KERN_ERR, &rport->dev,
  2483. "blocked FC remote port time out: removing target and "
  2484. "saving binding\n");
  2485. list_move_tail(&rport->peers, &fc_host->rport_bindings);
  2486. /*
  2487. * Note: We do not remove or clear the hostdata area. This allows
  2488. * host-specific target data to persist along with the
  2489. * scsi_target_id. It's up to the host to manage it's hostdata area.
  2490. */
  2491. /*
  2492. * Reinitialize port attributes that may change if the port comes back.
  2493. */
  2494. rport->maxframe_size = -1;
  2495. rport->supported_classes = FC_COS_UNSPECIFIED;
  2496. rport->roles = FC_PORT_ROLE_UNKNOWN;
  2497. rport->port_state = FC_PORTSTATE_NOTPRESENT;
  2498. /* remove the identifiers that aren't used in the consisting binding */
  2499. switch (fc_host->tgtid_bind_type) {
  2500. case FC_TGTID_BIND_BY_WWPN:
  2501. rport->node_name = -1;
  2502. rport->port_id = -1;
  2503. break;
  2504. case FC_TGTID_BIND_BY_WWNN:
  2505. rport->port_name = -1;
  2506. rport->port_id = -1;
  2507. break;
  2508. case FC_TGTID_BIND_BY_ID:
  2509. rport->node_name = -1;
  2510. rport->port_name = -1;
  2511. break;
  2512. case FC_TGTID_BIND_NONE: /* to keep compiler happy */
  2513. break;
  2514. }
  2515. /*
  2516. * As this only occurs if the remote port (scsi target)
  2517. * went away and didn't come back - we'll remove
  2518. * all attached scsi devices.
  2519. */
  2520. spin_unlock_irqrestore(shost->host_lock, flags);
  2521. scsi_target_unblock(&rport->dev);
  2522. fc_queue_work(shost, &rport->stgt_delete_work);
  2523. }
  2524. /**
  2525. * fc_timeout_fail_rport_io - Timeout handler for a fast io failing on a
  2526. * disconnected SCSI target.
  2527. *
  2528. * @work: rport to terminate io on.
  2529. *
  2530. * Notes: Only requests the failure of the io, not that all are flushed
  2531. * prior to returning.
  2532. **/
  2533. static void
  2534. fc_timeout_fail_rport_io(struct work_struct *work)
  2535. {
  2536. struct fc_rport *rport =
  2537. container_of(work, struct fc_rport, fail_io_work.work);
  2538. struct Scsi_Host *shost = rport_to_shost(rport);
  2539. struct fc_internal *i = to_fc_internal(shost->transportt);
  2540. if (rport->port_state != FC_PORTSTATE_BLOCKED)
  2541. return;
  2542. i->f->terminate_rport_io(rport);
  2543. }
  2544. /**
  2545. * fc_scsi_scan_rport - called to perform a scsi scan on a remote port.
  2546. *
  2547. * @work: remote port to be scanned.
  2548. **/
  2549. static void
  2550. fc_scsi_scan_rport(struct work_struct *work)
  2551. {
  2552. struct fc_rport *rport =
  2553. container_of(work, struct fc_rport, scan_work);
  2554. struct Scsi_Host *shost = rport_to_shost(rport);
  2555. unsigned long flags;
  2556. if ((rport->port_state == FC_PORTSTATE_ONLINE) &&
  2557. (rport->roles & FC_PORT_ROLE_FCP_TARGET)) {
  2558. scsi_scan_target(&rport->dev, rport->channel,
  2559. rport->scsi_target_id, SCAN_WILD_CARD, 1);
  2560. }
  2561. spin_lock_irqsave(shost->host_lock, flags);
  2562. rport->flags &= ~FC_RPORT_SCAN_PENDING;
  2563. spin_unlock_irqrestore(shost->host_lock, flags);
  2564. }
  2565. /**
  2566. * fc_vport_create - allocates and creates a FC virtual port.
  2567. * @shost: scsi host the virtual port is connected to.
  2568. * @channel: Channel on shost port connected to.
  2569. * @pdev: parent device for vport
  2570. * @ids: The world wide names, FC4 port roles, etc for
  2571. * the virtual port.
  2572. * @ret_vport: The pointer to the created vport.
  2573. *
  2574. * Allocates and creates the vport structure, calls the parent host
  2575. * to instantiate the vport, the completes w/ class and sysfs creation.
  2576. *
  2577. * Notes:
  2578. * This routine assumes no locks are held on entry.
  2579. **/
  2580. static int
  2581. fc_vport_create(struct Scsi_Host *shost, int channel, struct device *pdev,
  2582. struct fc_vport_identifiers *ids, struct fc_vport **ret_vport)
  2583. {
  2584. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  2585. struct fc_internal *fci = to_fc_internal(shost->transportt);
  2586. struct fc_vport *vport;
  2587. struct device *dev;
  2588. unsigned long flags;
  2589. size_t size;
  2590. int error;
  2591. *ret_vport = NULL;
  2592. if ( ! fci->f->vport_create)
  2593. return -ENOENT;
  2594. size = (sizeof(struct fc_vport) + fci->f->dd_fcvport_size);
  2595. vport = kzalloc(size, GFP_KERNEL);
  2596. if (unlikely(!vport)) {
  2597. printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
  2598. return -ENOMEM;
  2599. }
  2600. vport->vport_state = FC_VPORT_UNKNOWN;
  2601. vport->vport_last_state = FC_VPORT_UNKNOWN;
  2602. vport->node_name = ids->node_name;
  2603. vport->port_name = ids->port_name;
  2604. vport->roles = ids->roles;
  2605. vport->vport_type = ids->vport_type;
  2606. if (fci->f->dd_fcvport_size)
  2607. vport->dd_data = &vport[1];
  2608. vport->shost = shost;
  2609. vport->channel = channel;
  2610. vport->flags = FC_VPORT_CREATING;
  2611. INIT_WORK(&vport->vport_delete_work, fc_vport_sched_delete);
  2612. spin_lock_irqsave(shost->host_lock, flags);
  2613. if (fc_host->npiv_vports_inuse >= fc_host->max_npiv_vports) {
  2614. spin_unlock_irqrestore(shost->host_lock, flags);
  2615. kfree(vport);
  2616. return -ENOSPC;
  2617. }
  2618. fc_host->npiv_vports_inuse++;
  2619. vport->number = fc_host->next_vport_number++;
  2620. list_add_tail(&vport->peers, &fc_host->vports);
  2621. get_device(&shost->shost_gendev); /* for fc_host->vport list */
  2622. spin_unlock_irqrestore(shost->host_lock, flags);
  2623. dev = &vport->dev;
  2624. device_initialize(dev); /* takes self reference */
  2625. dev->parent = get_device(pdev); /* takes parent reference */
  2626. dev->release = fc_vport_dev_release;
  2627. sprintf(dev->bus_id, "vport-%d:%d-%d",
  2628. shost->host_no, channel, vport->number);
  2629. transport_setup_device(dev);
  2630. error = device_add(dev);
  2631. if (error) {
  2632. printk(KERN_ERR "FC Virtual Port device_add failed\n");
  2633. goto delete_vport;
  2634. }
  2635. transport_add_device(dev);
  2636. transport_configure_device(dev);
  2637. error = fci->f->vport_create(vport, ids->disable);
  2638. if (error) {
  2639. printk(KERN_ERR "FC Virtual Port LLDD Create failed\n");
  2640. goto delete_vport_all;
  2641. }
  2642. /*
  2643. * if the parent isn't the physical adapter's Scsi_Host, ensure
  2644. * the Scsi_Host at least contains ia symlink to the vport.
  2645. */
  2646. if (pdev != &shost->shost_gendev) {
  2647. error = sysfs_create_link(&shost->shost_gendev.kobj,
  2648. &dev->kobj, dev->bus_id);
  2649. if (error)
  2650. printk(KERN_ERR
  2651. "%s: Cannot create vport symlinks for "
  2652. "%s, err=%d\n",
  2653. __FUNCTION__, dev->bus_id, error);
  2654. }
  2655. spin_lock_irqsave(shost->host_lock, flags);
  2656. vport->flags &= ~FC_VPORT_CREATING;
  2657. spin_unlock_irqrestore(shost->host_lock, flags);
  2658. dev_printk(KERN_NOTICE, pdev,
  2659. "%s created via shost%d channel %d\n", dev->bus_id,
  2660. shost->host_no, channel);
  2661. *ret_vport = vport;
  2662. return 0;
  2663. delete_vport_all:
  2664. transport_remove_device(dev);
  2665. device_del(dev);
  2666. delete_vport:
  2667. transport_destroy_device(dev);
  2668. spin_lock_irqsave(shost->host_lock, flags);
  2669. list_del(&vport->peers);
  2670. put_device(&shost->shost_gendev); /* for fc_host->vport list */
  2671. fc_host->npiv_vports_inuse--;
  2672. spin_unlock_irqrestore(shost->host_lock, flags);
  2673. put_device(dev->parent);
  2674. kfree(vport);
  2675. return error;
  2676. }
  2677. /**
  2678. * fc_vport_terminate - Admin App or LLDD requests termination of a vport
  2679. * @vport: fc_vport to be terminated
  2680. *
  2681. * Calls the LLDD vport_delete() function, then deallocates and removes
  2682. * the vport from the shost and object tree.
  2683. *
  2684. * Notes:
  2685. * This routine assumes no locks are held on entry.
  2686. **/
  2687. int
  2688. fc_vport_terminate(struct fc_vport *vport)
  2689. {
  2690. struct Scsi_Host *shost = vport_to_shost(vport);
  2691. struct fc_host_attrs *fc_host = shost_to_fc_host(shost);
  2692. struct fc_internal *i = to_fc_internal(shost->transportt);
  2693. struct device *dev = &vport->dev;
  2694. unsigned long flags;
  2695. int stat;
  2696. spin_lock_irqsave(shost->host_lock, flags);
  2697. if (vport->flags & FC_VPORT_CREATING) {
  2698. spin_unlock_irqrestore(shost->host_lock, flags);
  2699. return -EBUSY;
  2700. }
  2701. if (vport->flags & (FC_VPORT_DEL)) {
  2702. spin_unlock_irqrestore(shost->host_lock, flags);
  2703. return -EALREADY;
  2704. }
  2705. vport->flags |= FC_VPORT_DELETING;
  2706. spin_unlock_irqrestore(shost->host_lock, flags);
  2707. if (i->f->vport_delete)
  2708. stat = i->f->vport_delete(vport);
  2709. else
  2710. stat = -ENOENT;
  2711. spin_lock_irqsave(shost->host_lock, flags);
  2712. vport->flags &= ~FC_VPORT_DELETING;
  2713. if (!stat) {
  2714. vport->flags |= FC_VPORT_DELETED;
  2715. list_del(&vport->peers);
  2716. fc_host->npiv_vports_inuse--;
  2717. put_device(&shost->shost_gendev); /* for fc_host->vport list */
  2718. }
  2719. spin_unlock_irqrestore(shost->host_lock, flags);
  2720. if (stat)
  2721. return stat;
  2722. if (dev->parent != &shost->shost_gendev)
  2723. sysfs_remove_link(&shost->shost_gendev.kobj, dev->bus_id);
  2724. transport_remove_device(dev);
  2725. device_del(dev);
  2726. transport_destroy_device(dev);
  2727. /*
  2728. * Removing our self-reference should mean our
  2729. * release function gets called, which will drop the remaining
  2730. * parent reference and free the data structure.
  2731. */
  2732. put_device(dev); /* for self-reference */
  2733. return 0; /* SUCCESS */
  2734. }
  2735. EXPORT_SYMBOL(fc_vport_terminate);
  2736. /**
  2737. * fc_vport_sched_delete - workq-based delete request for a vport
  2738. *
  2739. * @work: vport to be deleted.
  2740. **/
  2741. static void
  2742. fc_vport_sched_delete(struct work_struct *work)
  2743. {
  2744. struct fc_vport *vport =
  2745. container_of(work, struct fc_vport, vport_delete_work);
  2746. int stat;
  2747. stat = fc_vport_terminate(vport);
  2748. if (stat)
  2749. dev_printk(KERN_ERR, vport->dev.parent,
  2750. "%s: %s could not be deleted created via "
  2751. "shost%d channel %d - error %d\n", __FUNCTION__,
  2752. vport->dev.bus_id, vport->shost->host_no,
  2753. vport->channel, stat);
  2754. }
  2755. /* Original Author: Martin Hicks */
  2756. MODULE_AUTHOR("James Smart");
  2757. MODULE_DESCRIPTION("FC Transport Attributes");
  2758. MODULE_LICENSE("GPL");
  2759. module_init(fc_transport_init);
  2760. module_exit(fc_transport_exit);