scsi_transport_fc.c 98 KB

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