fcoe.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966
  1. /*
  2. * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Maintained at www.Open-FCoE.org
  18. */
  19. #include <linux/module.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/ethtool.h>
  24. #include <linux/if_ether.h>
  25. #include <linux/if_vlan.h>
  26. #include <linux/crc32.h>
  27. #include <linux/slab.h>
  28. #include <linux/cpu.h>
  29. #include <linux/fs.h>
  30. #include <linux/sysfs.h>
  31. #include <linux/ctype.h>
  32. #include <linux/workqueue.h>
  33. #include <net/dcbnl.h>
  34. #include <net/dcbevent.h>
  35. #include <scsi/scsi_tcq.h>
  36. #include <scsi/scsicam.h>
  37. #include <scsi/scsi_transport.h>
  38. #include <scsi/scsi_transport_fc.h>
  39. #include <net/rtnetlink.h>
  40. #include <scsi/fc/fc_encaps.h>
  41. #include <scsi/fc/fc_fip.h>
  42. #include <scsi/fc/fc_fcoe.h>
  43. #include <scsi/libfc.h>
  44. #include <scsi/fc_frame.h>
  45. #include <scsi/libfcoe.h>
  46. #include "fcoe.h"
  47. MODULE_AUTHOR("Open-FCoE.org");
  48. MODULE_DESCRIPTION("FCoE");
  49. MODULE_LICENSE("GPL v2");
  50. /* Performance tuning parameters for fcoe */
  51. static unsigned int fcoe_ddp_min = 4096;
  52. module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
  53. MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
  54. "Direct Data Placement (DDP).");
  55. unsigned int fcoe_debug_logging;
  56. module_param_named(debug_logging, fcoe_debug_logging, int, S_IRUGO|S_IWUSR);
  57. MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
  58. static DEFINE_MUTEX(fcoe_config_mutex);
  59. static struct workqueue_struct *fcoe_wq;
  60. /* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
  61. static DECLARE_COMPLETION(fcoe_flush_completion);
  62. /* fcoe host list */
  63. /* must only by accessed under the RTNL mutex */
  64. static LIST_HEAD(fcoe_hostlist);
  65. static DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
  66. /* Function Prototypes */
  67. static int fcoe_reset(struct Scsi_Host *);
  68. static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
  69. static int fcoe_rcv(struct sk_buff *, struct net_device *,
  70. struct packet_type *, struct net_device *);
  71. static int fcoe_percpu_receive_thread(void *);
  72. static void fcoe_percpu_clean(struct fc_lport *);
  73. static int fcoe_link_ok(struct fc_lport *);
  74. static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
  75. static int fcoe_hostlist_add(const struct fc_lport *);
  76. static void fcoe_hostlist_del(const struct fc_lport *);
  77. static int fcoe_device_notification(struct notifier_block *, ulong, void *);
  78. static void fcoe_dev_setup(void);
  79. static void fcoe_dev_cleanup(void);
  80. static struct fcoe_interface
  81. *fcoe_hostlist_lookup_port(const struct net_device *);
  82. static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
  83. struct packet_type *, struct net_device *);
  84. static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
  85. static void fcoe_update_src_mac(struct fc_lport *, u8 *);
  86. static u8 *fcoe_get_src_mac(struct fc_lport *);
  87. static void fcoe_destroy_work(struct work_struct *);
  88. static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
  89. unsigned int);
  90. static int fcoe_ddp_done(struct fc_lport *, u16);
  91. static int fcoe_ddp_target(struct fc_lport *, u16, struct scatterlist *,
  92. unsigned int);
  93. static int fcoe_cpu_callback(struct notifier_block *, unsigned long, void *);
  94. static int fcoe_dcb_app_notification(struct notifier_block *notifier,
  95. ulong event, void *ptr);
  96. static bool fcoe_match(struct net_device *netdev);
  97. static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode);
  98. static int fcoe_destroy(struct net_device *netdev);
  99. static int fcoe_enable(struct net_device *netdev);
  100. static int fcoe_disable(struct net_device *netdev);
  101. /* fcoe_syfs control interface handlers */
  102. static int fcoe_ctlr_alloc(struct net_device *netdev);
  103. static int fcoe_ctlr_enabled(struct fcoe_ctlr_device *cdev);
  104. static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
  105. u32 did, struct fc_frame *,
  106. unsigned int op,
  107. void (*resp)(struct fc_seq *,
  108. struct fc_frame *,
  109. void *),
  110. void *, u32 timeout);
  111. static void fcoe_recv_frame(struct sk_buff *skb);
  112. /* notification function for packets from net device */
  113. static struct notifier_block fcoe_notifier = {
  114. .notifier_call = fcoe_device_notification,
  115. };
  116. /* notification function for CPU hotplug events */
  117. static struct notifier_block fcoe_cpu_notifier = {
  118. .notifier_call = fcoe_cpu_callback,
  119. };
  120. /* notification function for DCB events */
  121. static struct notifier_block dcb_notifier = {
  122. .notifier_call = fcoe_dcb_app_notification,
  123. };
  124. static struct scsi_transport_template *fcoe_nport_scsi_transport;
  125. static struct scsi_transport_template *fcoe_vport_scsi_transport;
  126. static int fcoe_vport_destroy(struct fc_vport *);
  127. static int fcoe_vport_create(struct fc_vport *, bool disabled);
  128. static int fcoe_vport_disable(struct fc_vport *, bool disable);
  129. static void fcoe_set_vport_symbolic_name(struct fc_vport *);
  130. static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
  131. static void fcoe_fcf_get_vlan_id(struct fcoe_fcf_device *);
  132. static struct fcoe_sysfs_function_template fcoe_sysfs_templ = {
  133. .set_fcoe_ctlr_mode = fcoe_ctlr_set_fip_mode,
  134. .set_fcoe_ctlr_enabled = fcoe_ctlr_enabled,
  135. .get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
  136. .get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
  137. .get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
  138. .get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
  139. .get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
  140. .get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
  141. .get_fcoe_fcf_selected = fcoe_fcf_get_selected,
  142. .get_fcoe_fcf_vlan_id = fcoe_fcf_get_vlan_id,
  143. };
  144. static struct libfc_function_template fcoe_libfc_fcn_templ = {
  145. .frame_send = fcoe_xmit,
  146. .ddp_setup = fcoe_ddp_setup,
  147. .ddp_done = fcoe_ddp_done,
  148. .ddp_target = fcoe_ddp_target,
  149. .elsct_send = fcoe_elsct_send,
  150. .get_lesb = fcoe_get_lesb,
  151. .lport_set_port_id = fcoe_set_port_id,
  152. };
  153. static struct fc_function_template fcoe_nport_fc_functions = {
  154. .show_host_node_name = 1,
  155. .show_host_port_name = 1,
  156. .show_host_supported_classes = 1,
  157. .show_host_supported_fc4s = 1,
  158. .show_host_active_fc4s = 1,
  159. .show_host_maxframe_size = 1,
  160. .show_host_serial_number = 1,
  161. .show_host_manufacturer = 1,
  162. .show_host_model = 1,
  163. .show_host_model_description = 1,
  164. .show_host_hardware_version = 1,
  165. .show_host_driver_version = 1,
  166. .show_host_firmware_version = 1,
  167. .show_host_optionrom_version = 1,
  168. .show_host_port_id = 1,
  169. .show_host_supported_speeds = 1,
  170. .get_host_speed = fc_get_host_speed,
  171. .show_host_speed = 1,
  172. .show_host_port_type = 1,
  173. .get_host_port_state = fc_get_host_port_state,
  174. .show_host_port_state = 1,
  175. .show_host_symbolic_name = 1,
  176. .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
  177. .show_rport_maxframe_size = 1,
  178. .show_rport_supported_classes = 1,
  179. .show_host_fabric_name = 1,
  180. .show_starget_node_name = 1,
  181. .show_starget_port_name = 1,
  182. .show_starget_port_id = 1,
  183. .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
  184. .show_rport_dev_loss_tmo = 1,
  185. .get_fc_host_stats = fc_get_host_stats,
  186. .issue_fc_host_lip = fcoe_reset,
  187. .terminate_rport_io = fc_rport_terminate_io,
  188. .vport_create = fcoe_vport_create,
  189. .vport_delete = fcoe_vport_destroy,
  190. .vport_disable = fcoe_vport_disable,
  191. .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
  192. .bsg_request = fc_lport_bsg_request,
  193. };
  194. static struct fc_function_template fcoe_vport_fc_functions = {
  195. .show_host_node_name = 1,
  196. .show_host_port_name = 1,
  197. .show_host_supported_classes = 1,
  198. .show_host_supported_fc4s = 1,
  199. .show_host_active_fc4s = 1,
  200. .show_host_maxframe_size = 1,
  201. .show_host_serial_number = 1,
  202. .show_host_manufacturer = 1,
  203. .show_host_model = 1,
  204. .show_host_model_description = 1,
  205. .show_host_hardware_version = 1,
  206. .show_host_driver_version = 1,
  207. .show_host_firmware_version = 1,
  208. .show_host_optionrom_version = 1,
  209. .show_host_port_id = 1,
  210. .show_host_supported_speeds = 1,
  211. .get_host_speed = fc_get_host_speed,
  212. .show_host_speed = 1,
  213. .show_host_port_type = 1,
  214. .get_host_port_state = fc_get_host_port_state,
  215. .show_host_port_state = 1,
  216. .show_host_symbolic_name = 1,
  217. .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
  218. .show_rport_maxframe_size = 1,
  219. .show_rport_supported_classes = 1,
  220. .show_host_fabric_name = 1,
  221. .show_starget_node_name = 1,
  222. .show_starget_port_name = 1,
  223. .show_starget_port_id = 1,
  224. .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
  225. .show_rport_dev_loss_tmo = 1,
  226. .get_fc_host_stats = fc_get_host_stats,
  227. .issue_fc_host_lip = fcoe_reset,
  228. .terminate_rport_io = fc_rport_terminate_io,
  229. .bsg_request = fc_lport_bsg_request,
  230. };
  231. static struct scsi_host_template fcoe_shost_template = {
  232. .module = THIS_MODULE,
  233. .name = "FCoE Driver",
  234. .proc_name = FCOE_NAME,
  235. .queuecommand = fc_queuecommand,
  236. .eh_abort_handler = fc_eh_abort,
  237. .eh_device_reset_handler = fc_eh_device_reset,
  238. .eh_host_reset_handler = fc_eh_host_reset,
  239. .slave_alloc = fc_slave_alloc,
  240. .change_queue_depth = scsi_change_queue_depth,
  241. .this_id = -1,
  242. .cmd_per_lun = 3,
  243. .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
  244. .use_clustering = ENABLE_CLUSTERING,
  245. .sg_tablesize = SG_ALL,
  246. .max_sectors = 0xffff,
  247. .use_blk_tags = 1,
  248. .track_queue_depth = 1,
  249. };
  250. /**
  251. * fcoe_interface_setup() - Setup a FCoE interface
  252. * @fcoe: The new FCoE interface
  253. * @netdev: The net device that the fcoe interface is on
  254. *
  255. * Returns : 0 for success
  256. * Locking: must be called with the RTNL mutex held
  257. */
  258. static int fcoe_interface_setup(struct fcoe_interface *fcoe,
  259. struct net_device *netdev)
  260. {
  261. struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
  262. struct netdev_hw_addr *ha;
  263. struct net_device *real_dev;
  264. u8 flogi_maddr[ETH_ALEN];
  265. const struct net_device_ops *ops;
  266. fcoe->netdev = netdev;
  267. /* Let LLD initialize for FCoE */
  268. ops = netdev->netdev_ops;
  269. if (ops->ndo_fcoe_enable) {
  270. if (ops->ndo_fcoe_enable(netdev))
  271. FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
  272. " specific feature for LLD.\n");
  273. }
  274. /* Do not support for bonding device */
  275. if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
  276. FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
  277. return -EOPNOTSUPP;
  278. }
  279. /* look for SAN MAC address, if multiple SAN MACs exist, only
  280. * use the first one for SPMA */
  281. real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
  282. vlan_dev_real_dev(netdev) : netdev;
  283. fcoe->realdev = real_dev;
  284. rcu_read_lock();
  285. for_each_dev_addr(real_dev, ha) {
  286. if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
  287. (is_valid_ether_addr(ha->addr))) {
  288. memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
  289. fip->spma = 1;
  290. break;
  291. }
  292. }
  293. rcu_read_unlock();
  294. /* setup Source Mac Address */
  295. if (!fip->spma)
  296. memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
  297. /*
  298. * Add FCoE MAC address as second unicast MAC address
  299. * or enter promiscuous mode if not capable of listening
  300. * for multiple unicast MACs.
  301. */
  302. memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
  303. dev_uc_add(netdev, flogi_maddr);
  304. if (fip->spma)
  305. dev_uc_add(netdev, fip->ctl_src_addr);
  306. if (fip->mode == FIP_MODE_VN2VN) {
  307. dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
  308. dev_mc_add(netdev, FIP_ALL_P2P_MACS);
  309. } else
  310. dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
  311. /*
  312. * setup the receive function from ethernet driver
  313. * on the ethertype for the given device
  314. */
  315. fcoe->fcoe_packet_type.func = fcoe_rcv;
  316. fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
  317. fcoe->fcoe_packet_type.dev = netdev;
  318. dev_add_pack(&fcoe->fcoe_packet_type);
  319. fcoe->fip_packet_type.func = fcoe_fip_recv;
  320. fcoe->fip_packet_type.type = htons(ETH_P_FIP);
  321. fcoe->fip_packet_type.dev = netdev;
  322. dev_add_pack(&fcoe->fip_packet_type);
  323. return 0;
  324. }
  325. /**
  326. * fcoe_interface_create() - Create a FCoE interface on a net device
  327. * @netdev: The net device to create the FCoE interface on
  328. * @fip_mode: The mode to use for FIP
  329. *
  330. * Returns: pointer to a struct fcoe_interface or NULL on error
  331. */
  332. static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
  333. enum fip_state fip_mode)
  334. {
  335. struct fcoe_ctlr_device *ctlr_dev;
  336. struct fcoe_ctlr *ctlr;
  337. struct fcoe_interface *fcoe;
  338. int size;
  339. int err;
  340. if (!try_module_get(THIS_MODULE)) {
  341. FCOE_NETDEV_DBG(netdev,
  342. "Could not get a reference to the module\n");
  343. fcoe = ERR_PTR(-EBUSY);
  344. goto out;
  345. }
  346. size = sizeof(struct fcoe_ctlr) + sizeof(struct fcoe_interface);
  347. ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &fcoe_sysfs_templ,
  348. size);
  349. if (!ctlr_dev) {
  350. FCOE_DBG("Failed to add fcoe_ctlr_device\n");
  351. fcoe = ERR_PTR(-ENOMEM);
  352. goto out_putmod;
  353. }
  354. ctlr = fcoe_ctlr_device_priv(ctlr_dev);
  355. ctlr->cdev = ctlr_dev;
  356. fcoe = fcoe_ctlr_priv(ctlr);
  357. dev_hold(netdev);
  358. /*
  359. * Initialize FIP.
  360. */
  361. fcoe_ctlr_init(ctlr, fip_mode);
  362. ctlr->send = fcoe_fip_send;
  363. ctlr->update_mac = fcoe_update_src_mac;
  364. ctlr->get_src_addr = fcoe_get_src_mac;
  365. err = fcoe_interface_setup(fcoe, netdev);
  366. if (err) {
  367. fcoe_ctlr_destroy(ctlr);
  368. fcoe_ctlr_device_delete(ctlr_dev);
  369. dev_put(netdev);
  370. fcoe = ERR_PTR(err);
  371. goto out_putmod;
  372. }
  373. goto out;
  374. out_putmod:
  375. module_put(THIS_MODULE);
  376. out:
  377. return fcoe;
  378. }
  379. /**
  380. * fcoe_interface_remove() - remove FCoE interface from netdev
  381. * @fcoe: The FCoE interface to be cleaned up
  382. *
  383. * Caller must be holding the RTNL mutex
  384. */
  385. static void fcoe_interface_remove(struct fcoe_interface *fcoe)
  386. {
  387. struct net_device *netdev = fcoe->netdev;
  388. struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
  389. u8 flogi_maddr[ETH_ALEN];
  390. const struct net_device_ops *ops;
  391. /*
  392. * Don't listen for Ethernet packets anymore.
  393. * synchronize_net() ensures that the packet handlers are not running
  394. * on another CPU. dev_remove_pack() would do that, this calls the
  395. * unsyncronized version __dev_remove_pack() to avoid multiple delays.
  396. */
  397. __dev_remove_pack(&fcoe->fcoe_packet_type);
  398. __dev_remove_pack(&fcoe->fip_packet_type);
  399. synchronize_net();
  400. /* Delete secondary MAC addresses */
  401. memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
  402. dev_uc_del(netdev, flogi_maddr);
  403. if (fip->spma)
  404. dev_uc_del(netdev, fip->ctl_src_addr);
  405. if (fip->mode == FIP_MODE_VN2VN) {
  406. dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
  407. dev_mc_del(netdev, FIP_ALL_P2P_MACS);
  408. } else
  409. dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
  410. /* Tell the LLD we are done w/ FCoE */
  411. ops = netdev->netdev_ops;
  412. if (ops->ndo_fcoe_disable) {
  413. if (ops->ndo_fcoe_disable(netdev))
  414. FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
  415. " specific feature for LLD.\n");
  416. }
  417. fcoe->removed = 1;
  418. }
  419. /**
  420. * fcoe_interface_cleanup() - Clean up a FCoE interface
  421. * @fcoe: The FCoE interface to be cleaned up
  422. */
  423. static void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
  424. {
  425. struct net_device *netdev = fcoe->netdev;
  426. struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
  427. rtnl_lock();
  428. if (!fcoe->removed)
  429. fcoe_interface_remove(fcoe);
  430. rtnl_unlock();
  431. /* Release the self-reference taken during fcoe_interface_create() */
  432. /* tear-down the FCoE controller */
  433. fcoe_ctlr_destroy(fip);
  434. scsi_host_put(fip->lp->host);
  435. dev_put(netdev);
  436. module_put(THIS_MODULE);
  437. }
  438. /**
  439. * fcoe_fip_recv() - Handler for received FIP frames
  440. * @skb: The receive skb
  441. * @netdev: The associated net device
  442. * @ptype: The packet_type structure which was used to register this handler
  443. * @orig_dev: The original net_device the the skb was received on.
  444. * (in case dev is a bond)
  445. *
  446. * Returns: 0 for success
  447. */
  448. static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
  449. struct packet_type *ptype,
  450. struct net_device *orig_dev)
  451. {
  452. struct fcoe_interface *fcoe;
  453. struct fcoe_ctlr *ctlr;
  454. fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
  455. ctlr = fcoe_to_ctlr(fcoe);
  456. fcoe_ctlr_recv(ctlr, skb);
  457. return 0;
  458. }
  459. /**
  460. * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
  461. * @port: The FCoE port
  462. * @skb: The FIP/FCoE packet to be sent
  463. */
  464. static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
  465. {
  466. if (port->fcoe_pending_queue.qlen)
  467. fcoe_check_wait_queue(port->lport, skb);
  468. else if (fcoe_start_io(skb))
  469. fcoe_check_wait_queue(port->lport, skb);
  470. }
  471. /**
  472. * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
  473. * @fip: The FCoE controller
  474. * @skb: The FIP packet to be sent
  475. */
  476. static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
  477. {
  478. skb->dev = fcoe_from_ctlr(fip)->netdev;
  479. fcoe_port_send(lport_priv(fip->lp), skb);
  480. }
  481. /**
  482. * fcoe_update_src_mac() - Update the Ethernet MAC filters
  483. * @lport: The local port to update the source MAC on
  484. * @addr: Unicast MAC address to add
  485. *
  486. * Remove any previously-set unicast MAC filter.
  487. * Add secondary FCoE MAC address filter for our OUI.
  488. */
  489. static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
  490. {
  491. struct fcoe_port *port = lport_priv(lport);
  492. struct fcoe_interface *fcoe = port->priv;
  493. if (!is_zero_ether_addr(port->data_src_addr))
  494. dev_uc_del(fcoe->netdev, port->data_src_addr);
  495. if (!is_zero_ether_addr(addr))
  496. dev_uc_add(fcoe->netdev, addr);
  497. memcpy(port->data_src_addr, addr, ETH_ALEN);
  498. }
  499. /**
  500. * fcoe_get_src_mac() - return the Ethernet source address for an lport
  501. * @lport: libfc lport
  502. */
  503. static u8 *fcoe_get_src_mac(struct fc_lport *lport)
  504. {
  505. struct fcoe_port *port = lport_priv(lport);
  506. return port->data_src_addr;
  507. }
  508. /**
  509. * fcoe_lport_config() - Set up a local port
  510. * @lport: The local port to be setup
  511. *
  512. * Returns: 0 for success
  513. */
  514. static int fcoe_lport_config(struct fc_lport *lport)
  515. {
  516. lport->link_up = 0;
  517. lport->qfull = 0;
  518. lport->max_retry_count = 3;
  519. lport->max_rport_retry_count = 3;
  520. lport->e_d_tov = 2 * 1000; /* FC-FS default */
  521. lport->r_a_tov = 2 * 2 * 1000;
  522. lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
  523. FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
  524. lport->does_npiv = 1;
  525. fc_lport_init_stats(lport);
  526. /* lport fc_lport related configuration */
  527. fc_lport_config(lport);
  528. /* offload related configuration */
  529. lport->crc_offload = 0;
  530. lport->seq_offload = 0;
  531. lport->lro_enabled = 0;
  532. lport->lro_xid = 0;
  533. lport->lso_max = 0;
  534. return 0;
  535. }
  536. /**
  537. * fcoe_netdev_features_change - Updates the lport's offload flags based
  538. * on the LLD netdev's FCoE feature flags
  539. */
  540. static void fcoe_netdev_features_change(struct fc_lport *lport,
  541. struct net_device *netdev)
  542. {
  543. mutex_lock(&lport->lp_mutex);
  544. if (netdev->features & NETIF_F_SG)
  545. lport->sg_supp = 1;
  546. else
  547. lport->sg_supp = 0;
  548. if (netdev->features & NETIF_F_FCOE_CRC) {
  549. lport->crc_offload = 1;
  550. FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
  551. } else {
  552. lport->crc_offload = 0;
  553. }
  554. if (netdev->features & NETIF_F_FSO) {
  555. lport->seq_offload = 1;
  556. lport->lso_max = netdev->gso_max_size;
  557. FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
  558. lport->lso_max);
  559. } else {
  560. lport->seq_offload = 0;
  561. lport->lso_max = 0;
  562. }
  563. if (netdev->fcoe_ddp_xid) {
  564. lport->lro_enabled = 1;
  565. lport->lro_xid = netdev->fcoe_ddp_xid;
  566. FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
  567. lport->lro_xid);
  568. } else {
  569. lport->lro_enabled = 0;
  570. lport->lro_xid = 0;
  571. }
  572. mutex_unlock(&lport->lp_mutex);
  573. }
  574. /**
  575. * fcoe_netdev_config() - Set up net devive for SW FCoE
  576. * @lport: The local port that is associated with the net device
  577. * @netdev: The associated net device
  578. *
  579. * Must be called after fcoe_lport_config() as it will use local port mutex
  580. *
  581. * Returns: 0 for success
  582. */
  583. static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
  584. {
  585. u32 mfs;
  586. u64 wwnn, wwpn;
  587. struct fcoe_interface *fcoe;
  588. struct fcoe_ctlr *ctlr;
  589. struct fcoe_port *port;
  590. /* Setup lport private data to point to fcoe softc */
  591. port = lport_priv(lport);
  592. fcoe = port->priv;
  593. ctlr = fcoe_to_ctlr(fcoe);
  594. /*
  595. * Determine max frame size based on underlying device and optional
  596. * user-configured limit. If the MFS is too low, fcoe_link_ok()
  597. * will return 0, so do this first.
  598. */
  599. mfs = netdev->mtu;
  600. if (netdev->features & NETIF_F_FCOE_MTU) {
  601. mfs = FCOE_MTU;
  602. FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
  603. }
  604. mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
  605. if (fc_set_mfs(lport, mfs))
  606. return -EINVAL;
  607. /* offload features support */
  608. fcoe_netdev_features_change(lport, netdev);
  609. skb_queue_head_init(&port->fcoe_pending_queue);
  610. port->fcoe_pending_queue_active = 0;
  611. setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
  612. fcoe_link_speed_update(lport);
  613. if (!lport->vport) {
  614. if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
  615. wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr, 1, 0);
  616. fc_set_wwnn(lport, wwnn);
  617. if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
  618. wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
  619. 2, 0);
  620. fc_set_wwpn(lport, wwpn);
  621. }
  622. return 0;
  623. }
  624. /**
  625. * fcoe_shost_config() - Set up the SCSI host associated with a local port
  626. * @lport: The local port
  627. * @dev: The device associated with the SCSI host
  628. *
  629. * Must be called after fcoe_lport_config() and fcoe_netdev_config()
  630. *
  631. * Returns: 0 for success
  632. */
  633. static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
  634. {
  635. int rc = 0;
  636. /* lport scsi host config */
  637. lport->host->max_lun = FCOE_MAX_LUN;
  638. lport->host->max_id = FCOE_MAX_FCP_TARGET;
  639. lport->host->max_channel = 0;
  640. lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
  641. if (lport->vport)
  642. lport->host->transportt = fcoe_vport_scsi_transport;
  643. else
  644. lport->host->transportt = fcoe_nport_scsi_transport;
  645. /* add the new host to the SCSI-ml */
  646. rc = scsi_add_host(lport->host, dev);
  647. if (rc) {
  648. FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
  649. "error on scsi_add_host\n");
  650. return rc;
  651. }
  652. if (!lport->vport)
  653. fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
  654. snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
  655. "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
  656. fcoe_netdev(lport)->name);
  657. return 0;
  658. }
  659. /**
  660. * fcoe_fdmi_info() - Get FDMI related info from net devive for SW FCoE
  661. * @lport: The local port that is associated with the net device
  662. * @netdev: The associated net device
  663. *
  664. * Must be called after fcoe_shost_config() as it will use local port mutex
  665. *
  666. */
  667. static void fcoe_fdmi_info(struct fc_lport *lport, struct net_device *netdev)
  668. {
  669. struct fcoe_interface *fcoe;
  670. struct fcoe_port *port;
  671. struct net_device *realdev;
  672. int rc;
  673. port = lport_priv(lport);
  674. fcoe = port->priv;
  675. realdev = fcoe->realdev;
  676. if (!realdev)
  677. return;
  678. /* No FDMI state m/c for NPIV ports */
  679. if (lport->vport)
  680. return;
  681. if (realdev->netdev_ops->ndo_fcoe_get_hbainfo) {
  682. struct netdev_fcoe_hbainfo *fdmi;
  683. fdmi = kzalloc(sizeof(*fdmi), GFP_KERNEL);
  684. if (!fdmi)
  685. return;
  686. rc = realdev->netdev_ops->ndo_fcoe_get_hbainfo(realdev,
  687. fdmi);
  688. if (rc) {
  689. printk(KERN_INFO "fcoe: Failed to retrieve FDMI "
  690. "information from netdev.\n");
  691. return;
  692. }
  693. snprintf(fc_host_serial_number(lport->host),
  694. FC_SERIAL_NUMBER_SIZE,
  695. "%s",
  696. fdmi->serial_number);
  697. snprintf(fc_host_manufacturer(lport->host),
  698. FC_SERIAL_NUMBER_SIZE,
  699. "%s",
  700. fdmi->manufacturer);
  701. snprintf(fc_host_model(lport->host),
  702. FC_SYMBOLIC_NAME_SIZE,
  703. "%s",
  704. fdmi->model);
  705. snprintf(fc_host_model_description(lport->host),
  706. FC_SYMBOLIC_NAME_SIZE,
  707. "%s",
  708. fdmi->model_description);
  709. snprintf(fc_host_hardware_version(lport->host),
  710. FC_VERSION_STRING_SIZE,
  711. "%s",
  712. fdmi->hardware_version);
  713. snprintf(fc_host_driver_version(lport->host),
  714. FC_VERSION_STRING_SIZE,
  715. "%s",
  716. fdmi->driver_version);
  717. snprintf(fc_host_optionrom_version(lport->host),
  718. FC_VERSION_STRING_SIZE,
  719. "%s",
  720. fdmi->optionrom_version);
  721. snprintf(fc_host_firmware_version(lport->host),
  722. FC_VERSION_STRING_SIZE,
  723. "%s",
  724. fdmi->firmware_version);
  725. /* Enable FDMI lport states */
  726. lport->fdmi_enabled = 1;
  727. kfree(fdmi);
  728. } else {
  729. lport->fdmi_enabled = 0;
  730. printk(KERN_INFO "fcoe: No FDMI support.\n");
  731. }
  732. }
  733. /**
  734. * fcoe_oem_match() - The match routine for the offloaded exchange manager
  735. * @fp: The I/O frame
  736. *
  737. * This routine will be associated with an exchange manager (EM). When
  738. * the libfc exchange handling code is looking for an EM to use it will
  739. * call this routine and pass it the frame that it wishes to send. This
  740. * routine will return True if the associated EM is to be used and False
  741. * if the echange code should continue looking for an EM.
  742. *
  743. * The offload EM that this routine is associated with will handle any
  744. * packets that are for SCSI read requests.
  745. *
  746. * This has been enhanced to work when FCoE stack is operating in target
  747. * mode.
  748. *
  749. * Returns: True for read types I/O, otherwise returns false.
  750. */
  751. static bool fcoe_oem_match(struct fc_frame *fp)
  752. {
  753. struct fc_frame_header *fh = fc_frame_header_get(fp);
  754. struct fcp_cmnd *fcp;
  755. if (fc_fcp_is_read(fr_fsp(fp)) &&
  756. (fr_fsp(fp)->data_len > fcoe_ddp_min))
  757. return true;
  758. else if ((fr_fsp(fp) == NULL) &&
  759. (fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) &&
  760. (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)) {
  761. fcp = fc_frame_payload_get(fp, sizeof(*fcp));
  762. if ((fcp->fc_flags & FCP_CFL_WRDATA) &&
  763. (ntohl(fcp->fc_dl) > fcoe_ddp_min))
  764. return true;
  765. }
  766. return false;
  767. }
  768. /**
  769. * fcoe_em_config() - Allocate and configure an exchange manager
  770. * @lport: The local port that the new EM will be associated with
  771. *
  772. * Returns: 0 on success
  773. */
  774. static inline int fcoe_em_config(struct fc_lport *lport)
  775. {
  776. struct fcoe_port *port = lport_priv(lport);
  777. struct fcoe_interface *fcoe = port->priv;
  778. struct fcoe_interface *oldfcoe = NULL;
  779. struct net_device *old_real_dev, *cur_real_dev;
  780. u16 min_xid = FCOE_MIN_XID;
  781. u16 max_xid = FCOE_MAX_XID;
  782. /*
  783. * Check if need to allocate an em instance for
  784. * offload exchange ids to be shared across all VN_PORTs/lport.
  785. */
  786. if (!lport->lro_enabled || !lport->lro_xid ||
  787. (lport->lro_xid >= max_xid)) {
  788. lport->lro_xid = 0;
  789. goto skip_oem;
  790. }
  791. /*
  792. * Reuse existing offload em instance in case
  793. * it is already allocated on real eth device
  794. */
  795. if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
  796. cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
  797. else
  798. cur_real_dev = fcoe->netdev;
  799. list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
  800. if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
  801. old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
  802. else
  803. old_real_dev = oldfcoe->netdev;
  804. if (cur_real_dev == old_real_dev) {
  805. fcoe->oem = oldfcoe->oem;
  806. break;
  807. }
  808. }
  809. if (fcoe->oem) {
  810. if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
  811. printk(KERN_ERR "fcoe_em_config: failed to add "
  812. "offload em:%p on interface:%s\n",
  813. fcoe->oem, fcoe->netdev->name);
  814. return -ENOMEM;
  815. }
  816. } else {
  817. fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
  818. FCOE_MIN_XID, lport->lro_xid,
  819. fcoe_oem_match);
  820. if (!fcoe->oem) {
  821. printk(KERN_ERR "fcoe_em_config: failed to allocate "
  822. "em for offload exches on interface:%s\n",
  823. fcoe->netdev->name);
  824. return -ENOMEM;
  825. }
  826. }
  827. /*
  828. * Exclude offload EM xid range from next EM xid range.
  829. */
  830. min_xid += lport->lro_xid + 1;
  831. skip_oem:
  832. if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
  833. printk(KERN_ERR "fcoe_em_config: failed to "
  834. "allocate em on interface %s\n", fcoe->netdev->name);
  835. return -ENOMEM;
  836. }
  837. return 0;
  838. }
  839. /**
  840. * fcoe_if_destroy() - Tear down a SW FCoE instance
  841. * @lport: The local port to be destroyed
  842. *
  843. */
  844. static void fcoe_if_destroy(struct fc_lport *lport)
  845. {
  846. struct fcoe_port *port = lport_priv(lport);
  847. struct fcoe_interface *fcoe = port->priv;
  848. struct net_device *netdev = fcoe->netdev;
  849. FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
  850. /* Logout of the fabric */
  851. fc_fabric_logoff(lport);
  852. /* Cleanup the fc_lport */
  853. fc_lport_destroy(lport);
  854. /* Stop the transmit retry timer */
  855. del_timer_sync(&port->timer);
  856. /* Free existing transmit skbs */
  857. fcoe_clean_pending_queue(lport);
  858. rtnl_lock();
  859. if (!is_zero_ether_addr(port->data_src_addr))
  860. dev_uc_del(netdev, port->data_src_addr);
  861. if (lport->vport)
  862. synchronize_net();
  863. else
  864. fcoe_interface_remove(fcoe);
  865. rtnl_unlock();
  866. /* Free queued packets for the per-CPU receive threads */
  867. fcoe_percpu_clean(lport);
  868. /* Detach from the scsi-ml */
  869. fc_remove_host(lport->host);
  870. scsi_remove_host(lport->host);
  871. /* Destroy lport scsi_priv */
  872. fc_fcp_destroy(lport);
  873. /* There are no more rports or I/O, free the EM */
  874. fc_exch_mgr_free(lport);
  875. /* Free memory used by statistical counters */
  876. fc_lport_free_stats(lport);
  877. /*
  878. * Release the Scsi_Host for vport but hold on to
  879. * master lport until it fcoe interface fully cleaned-up.
  880. */
  881. if (lport->vport)
  882. scsi_host_put(lport->host);
  883. }
  884. /**
  885. * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
  886. * @lport: The local port to setup DDP for
  887. * @xid: The exchange ID for this DDP transfer
  888. * @sgl: The scatterlist describing this transfer
  889. * @sgc: The number of sg items
  890. *
  891. * Returns: 0 if the DDP context was not configured
  892. */
  893. static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
  894. struct scatterlist *sgl, unsigned int sgc)
  895. {
  896. struct net_device *netdev = fcoe_netdev(lport);
  897. if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
  898. return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
  899. xid, sgl,
  900. sgc);
  901. return 0;
  902. }
  903. /**
  904. * fcoe_ddp_target() - Call a LLD's ddp_target through the net device
  905. * @lport: The local port to setup DDP for
  906. * @xid: The exchange ID for this DDP transfer
  907. * @sgl: The scatterlist describing this transfer
  908. * @sgc: The number of sg items
  909. *
  910. * Returns: 0 if the DDP context was not configured
  911. */
  912. static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
  913. struct scatterlist *sgl, unsigned int sgc)
  914. {
  915. struct net_device *netdev = fcoe_netdev(lport);
  916. if (netdev->netdev_ops->ndo_fcoe_ddp_target)
  917. return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
  918. sgl, sgc);
  919. return 0;
  920. }
  921. /**
  922. * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
  923. * @lport: The local port to complete DDP on
  924. * @xid: The exchange ID for this DDP transfer
  925. *
  926. * Returns: the length of data that have been completed by DDP
  927. */
  928. static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
  929. {
  930. struct net_device *netdev = fcoe_netdev(lport);
  931. if (netdev->netdev_ops->ndo_fcoe_ddp_done)
  932. return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
  933. return 0;
  934. }
  935. /**
  936. * fcoe_if_create() - Create a FCoE instance on an interface
  937. * @fcoe: The FCoE interface to create a local port on
  938. * @parent: The device pointer to be the parent in sysfs for the SCSI host
  939. * @npiv: Indicates if the port is a vport or not
  940. *
  941. * Creates a fc_lport instance and a Scsi_Host instance and configure them.
  942. *
  943. * Returns: The allocated fc_lport or an error pointer
  944. */
  945. static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
  946. struct device *parent, int npiv)
  947. {
  948. struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
  949. struct net_device *netdev = fcoe->netdev;
  950. struct fc_lport *lport, *n_port;
  951. struct fcoe_port *port;
  952. struct Scsi_Host *shost;
  953. int rc;
  954. /*
  955. * parent is only a vport if npiv is 1,
  956. * but we'll only use vport in that case so go ahead and set it
  957. */
  958. struct fc_vport *vport = dev_to_vport(parent);
  959. FCOE_NETDEV_DBG(netdev, "Create Interface\n");
  960. if (!npiv)
  961. lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
  962. else
  963. lport = libfc_vport_create(vport, sizeof(*port));
  964. if (!lport) {
  965. FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
  966. rc = -ENOMEM;
  967. goto out;
  968. }
  969. port = lport_priv(lport);
  970. port->lport = lport;
  971. port->priv = fcoe;
  972. port->get_netdev = fcoe_netdev;
  973. port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
  974. port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
  975. INIT_WORK(&port->destroy_work, fcoe_destroy_work);
  976. /*
  977. * Need to add the lport to the hostlist
  978. * so we catch NETDEV_CHANGE events.
  979. */
  980. fcoe_hostlist_add(lport);
  981. /* configure a fc_lport including the exchange manager */
  982. rc = fcoe_lport_config(lport);
  983. if (rc) {
  984. FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
  985. "interface\n");
  986. goto out_host_put;
  987. }
  988. if (npiv) {
  989. FCOE_NETDEV_DBG(netdev, "Setting vport names, "
  990. "%16.16llx %16.16llx\n",
  991. vport->node_name, vport->port_name);
  992. fc_set_wwnn(lport, vport->node_name);
  993. fc_set_wwpn(lport, vport->port_name);
  994. }
  995. /* configure lport network properties */
  996. rc = fcoe_netdev_config(lport, netdev);
  997. if (rc) {
  998. FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
  999. "interface\n");
  1000. goto out_lp_destroy;
  1001. }
  1002. /* configure lport scsi host properties */
  1003. rc = fcoe_shost_config(lport, parent);
  1004. if (rc) {
  1005. FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
  1006. "interface\n");
  1007. goto out_lp_destroy;
  1008. }
  1009. /* Initialize the library */
  1010. rc = fcoe_libfc_config(lport, ctlr, &fcoe_libfc_fcn_templ, 1);
  1011. if (rc) {
  1012. FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
  1013. "interface\n");
  1014. goto out_lp_destroy;
  1015. }
  1016. /* Initialized FDMI information */
  1017. fcoe_fdmi_info(lport, netdev);
  1018. /*
  1019. * fcoe_em_alloc() and fcoe_hostlist_add() both
  1020. * need to be atomic with respect to other changes to the
  1021. * hostlist since fcoe_em_alloc() looks for an existing EM
  1022. * instance on host list updated by fcoe_hostlist_add().
  1023. *
  1024. * This is currently handled through the fcoe_config_mutex
  1025. * begin held.
  1026. */
  1027. if (!npiv)
  1028. /* lport exch manager allocation */
  1029. rc = fcoe_em_config(lport);
  1030. else {
  1031. shost = vport_to_shost(vport);
  1032. n_port = shost_priv(shost);
  1033. rc = fc_exch_mgr_list_clone(n_port, lport);
  1034. }
  1035. if (rc) {
  1036. FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
  1037. goto out_lp_destroy;
  1038. }
  1039. return lport;
  1040. out_lp_destroy:
  1041. fc_exch_mgr_free(lport);
  1042. out_host_put:
  1043. fcoe_hostlist_del(lport);
  1044. scsi_host_put(lport->host);
  1045. out:
  1046. return ERR_PTR(rc);
  1047. }
  1048. /**
  1049. * fcoe_if_init() - Initialization routine for fcoe.ko
  1050. *
  1051. * Attaches the SW FCoE transport to the FC transport
  1052. *
  1053. * Returns: 0 on success
  1054. */
  1055. static int __init fcoe_if_init(void)
  1056. {
  1057. /* attach to scsi transport */
  1058. fcoe_nport_scsi_transport =
  1059. fc_attach_transport(&fcoe_nport_fc_functions);
  1060. fcoe_vport_scsi_transport =
  1061. fc_attach_transport(&fcoe_vport_fc_functions);
  1062. if (!fcoe_nport_scsi_transport) {
  1063. printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
  1064. return -ENODEV;
  1065. }
  1066. return 0;
  1067. }
  1068. /**
  1069. * fcoe_if_exit() - Tear down fcoe.ko
  1070. *
  1071. * Detaches the SW FCoE transport from the FC transport
  1072. *
  1073. * Returns: 0 on success
  1074. */
  1075. static int __exit fcoe_if_exit(void)
  1076. {
  1077. fc_release_transport(fcoe_nport_scsi_transport);
  1078. fc_release_transport(fcoe_vport_scsi_transport);
  1079. fcoe_nport_scsi_transport = NULL;
  1080. fcoe_vport_scsi_transport = NULL;
  1081. return 0;
  1082. }
  1083. /**
  1084. * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
  1085. * @cpu: The CPU index of the CPU to create a receive thread for
  1086. */
  1087. static void fcoe_percpu_thread_create(unsigned int cpu)
  1088. {
  1089. struct fcoe_percpu_s *p;
  1090. struct task_struct *thread;
  1091. p = &per_cpu(fcoe_percpu, cpu);
  1092. thread = kthread_create_on_node(fcoe_percpu_receive_thread,
  1093. (void *)p, cpu_to_node(cpu),
  1094. "fcoethread/%d", cpu);
  1095. if (likely(!IS_ERR(thread))) {
  1096. kthread_bind(thread, cpu);
  1097. wake_up_process(thread);
  1098. spin_lock_bh(&p->fcoe_rx_list.lock);
  1099. p->thread = thread;
  1100. spin_unlock_bh(&p->fcoe_rx_list.lock);
  1101. }
  1102. }
  1103. /**
  1104. * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
  1105. * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
  1106. *
  1107. * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
  1108. * current CPU's Rx thread. If the thread being destroyed is bound to
  1109. * the CPU processing this context the skbs will be freed.
  1110. */
  1111. static void fcoe_percpu_thread_destroy(unsigned int cpu)
  1112. {
  1113. struct fcoe_percpu_s *p;
  1114. struct task_struct *thread;
  1115. struct page *crc_eof;
  1116. struct sk_buff *skb;
  1117. #ifdef CONFIG_SMP
  1118. struct fcoe_percpu_s *p0;
  1119. unsigned targ_cpu = get_cpu();
  1120. #endif /* CONFIG_SMP */
  1121. FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
  1122. /* Prevent any new skbs from being queued for this CPU. */
  1123. p = &per_cpu(fcoe_percpu, cpu);
  1124. spin_lock_bh(&p->fcoe_rx_list.lock);
  1125. thread = p->thread;
  1126. p->thread = NULL;
  1127. crc_eof = p->crc_eof_page;
  1128. p->crc_eof_page = NULL;
  1129. p->crc_eof_offset = 0;
  1130. spin_unlock_bh(&p->fcoe_rx_list.lock);
  1131. #ifdef CONFIG_SMP
  1132. /*
  1133. * Don't bother moving the skb's if this context is running
  1134. * on the same CPU that is having its thread destroyed. This
  1135. * can easily happen when the module is removed.
  1136. */
  1137. if (cpu != targ_cpu) {
  1138. p0 = &per_cpu(fcoe_percpu, targ_cpu);
  1139. spin_lock_bh(&p0->fcoe_rx_list.lock);
  1140. if (p0->thread) {
  1141. FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
  1142. cpu, targ_cpu);
  1143. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
  1144. __skb_queue_tail(&p0->fcoe_rx_list, skb);
  1145. spin_unlock_bh(&p0->fcoe_rx_list.lock);
  1146. } else {
  1147. /*
  1148. * The targeted CPU is not initialized and cannot accept
  1149. * new skbs. Unlock the targeted CPU and drop the skbs
  1150. * on the CPU that is going offline.
  1151. */
  1152. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
  1153. kfree_skb(skb);
  1154. spin_unlock_bh(&p0->fcoe_rx_list.lock);
  1155. }
  1156. } else {
  1157. /*
  1158. * This scenario occurs when the module is being removed
  1159. * and all threads are being destroyed. skbs will continue
  1160. * to be shifted from the CPU thread that is being removed
  1161. * to the CPU thread associated with the CPU that is processing
  1162. * the module removal. Once there is only one CPU Rx thread it
  1163. * will reach this case and we will drop all skbs and later
  1164. * stop the thread.
  1165. */
  1166. spin_lock_bh(&p->fcoe_rx_list.lock);
  1167. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
  1168. kfree_skb(skb);
  1169. spin_unlock_bh(&p->fcoe_rx_list.lock);
  1170. }
  1171. put_cpu();
  1172. #else
  1173. /*
  1174. * This a non-SMP scenario where the singular Rx thread is
  1175. * being removed. Free all skbs and stop the thread.
  1176. */
  1177. spin_lock_bh(&p->fcoe_rx_list.lock);
  1178. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
  1179. kfree_skb(skb);
  1180. spin_unlock_bh(&p->fcoe_rx_list.lock);
  1181. #endif
  1182. if (thread)
  1183. kthread_stop(thread);
  1184. if (crc_eof)
  1185. put_page(crc_eof);
  1186. }
  1187. /**
  1188. * fcoe_cpu_callback() - Handler for CPU hotplug events
  1189. * @nfb: The callback data block
  1190. * @action: The event triggering the callback
  1191. * @hcpu: The index of the CPU that the event is for
  1192. *
  1193. * This creates or destroys per-CPU data for fcoe
  1194. *
  1195. * Returns NOTIFY_OK always.
  1196. */
  1197. static int fcoe_cpu_callback(struct notifier_block *nfb,
  1198. unsigned long action, void *hcpu)
  1199. {
  1200. unsigned cpu = (unsigned long)hcpu;
  1201. switch (action) {
  1202. case CPU_ONLINE:
  1203. case CPU_ONLINE_FROZEN:
  1204. FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
  1205. fcoe_percpu_thread_create(cpu);
  1206. break;
  1207. case CPU_DEAD:
  1208. case CPU_DEAD_FROZEN:
  1209. FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
  1210. fcoe_percpu_thread_destroy(cpu);
  1211. break;
  1212. default:
  1213. break;
  1214. }
  1215. return NOTIFY_OK;
  1216. }
  1217. /**
  1218. * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
  1219. * command.
  1220. *
  1221. * This routine selects next CPU based on cpumask to distribute
  1222. * incoming requests in round robin.
  1223. *
  1224. * Returns: int CPU number
  1225. */
  1226. static inline unsigned int fcoe_select_cpu(void)
  1227. {
  1228. static unsigned int selected_cpu;
  1229. selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
  1230. if (selected_cpu >= nr_cpu_ids)
  1231. selected_cpu = cpumask_first(cpu_online_mask);
  1232. return selected_cpu;
  1233. }
  1234. /**
  1235. * fcoe_rcv() - Receive packets from a net device
  1236. * @skb: The received packet
  1237. * @netdev: The net device that the packet was received on
  1238. * @ptype: The packet type context
  1239. * @olddev: The last device net device
  1240. *
  1241. * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
  1242. * FC frame and passes the frame to libfc.
  1243. *
  1244. * Returns: 0 for success
  1245. */
  1246. static int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
  1247. struct packet_type *ptype, struct net_device *olddev)
  1248. {
  1249. struct fc_lport *lport;
  1250. struct fcoe_rcv_info *fr;
  1251. struct fcoe_ctlr *ctlr;
  1252. struct fcoe_interface *fcoe;
  1253. struct fc_frame_header *fh;
  1254. struct fcoe_percpu_s *fps;
  1255. struct ethhdr *eh;
  1256. unsigned int cpu;
  1257. fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
  1258. ctlr = fcoe_to_ctlr(fcoe);
  1259. lport = ctlr->lp;
  1260. if (unlikely(!lport)) {
  1261. FCOE_NETDEV_DBG(netdev, "Cannot find hba structure\n");
  1262. goto err2;
  1263. }
  1264. if (!lport->link_up)
  1265. goto err2;
  1266. FCOE_NETDEV_DBG(netdev,
  1267. "skb_info: len:%d data_len:%d head:%p data:%p tail:%p end:%p sum:%d dev:%s\n",
  1268. skb->len, skb->data_len, skb->head, skb->data,
  1269. skb_tail_pointer(skb), skb_end_pointer(skb),
  1270. skb->csum, skb->dev ? skb->dev->name : "<NULL>");
  1271. skb = skb_share_check(skb, GFP_ATOMIC);
  1272. if (skb == NULL)
  1273. return NET_RX_DROP;
  1274. eh = eth_hdr(skb);
  1275. if (is_fip_mode(ctlr) &&
  1276. !ether_addr_equal(eh->h_source, ctlr->dest_addr)) {
  1277. FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
  1278. eh->h_source);
  1279. goto err;
  1280. }
  1281. /*
  1282. * Check for minimum frame length, and make sure required FCoE
  1283. * and FC headers are pulled into the linear data area.
  1284. */
  1285. if (unlikely((skb->len < FCOE_MIN_FRAME) ||
  1286. !pskb_may_pull(skb, FCOE_HEADER_LEN)))
  1287. goto err;
  1288. skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
  1289. fh = (struct fc_frame_header *) skb_transport_header(skb);
  1290. if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
  1291. FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
  1292. eh->h_dest);
  1293. goto err;
  1294. }
  1295. fr = fcoe_dev_from_skb(skb);
  1296. fr->fr_dev = lport;
  1297. /*
  1298. * In case the incoming frame's exchange is originated from
  1299. * the initiator, then received frame's exchange id is ANDed
  1300. * with fc_cpu_mask bits to get the same cpu on which exchange
  1301. * was originated, otherwise select cpu using rx exchange id
  1302. * or fcoe_select_cpu().
  1303. */
  1304. if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
  1305. cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
  1306. else {
  1307. if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
  1308. cpu = fcoe_select_cpu();
  1309. else
  1310. cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
  1311. }
  1312. if (cpu >= nr_cpu_ids)
  1313. goto err;
  1314. fps = &per_cpu(fcoe_percpu, cpu);
  1315. spin_lock(&fps->fcoe_rx_list.lock);
  1316. if (unlikely(!fps->thread)) {
  1317. /*
  1318. * The targeted CPU is not ready, let's target
  1319. * the first CPU now. For non-SMP systems this
  1320. * will check the same CPU twice.
  1321. */
  1322. FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
  1323. "ready for incoming skb- using first online "
  1324. "CPU.\n");
  1325. spin_unlock(&fps->fcoe_rx_list.lock);
  1326. cpu = cpumask_first(cpu_online_mask);
  1327. fps = &per_cpu(fcoe_percpu, cpu);
  1328. spin_lock(&fps->fcoe_rx_list.lock);
  1329. if (!fps->thread) {
  1330. spin_unlock(&fps->fcoe_rx_list.lock);
  1331. goto err;
  1332. }
  1333. }
  1334. /*
  1335. * We now have a valid CPU that we're targeting for
  1336. * this skb. We also have this receive thread locked,
  1337. * so we're free to queue skbs into it's queue.
  1338. */
  1339. /*
  1340. * Note: We used to have a set of conditions under which we would
  1341. * call fcoe_recv_frame directly, rather than queuing to the rx list
  1342. * as it could save a few cycles, but doing so is prohibited, as
  1343. * fcoe_recv_frame has several paths that may sleep, which is forbidden
  1344. * in softirq context.
  1345. */
  1346. __skb_queue_tail(&fps->fcoe_rx_list, skb);
  1347. if (fps->thread->state == TASK_INTERRUPTIBLE)
  1348. wake_up_process(fps->thread);
  1349. spin_unlock(&fps->fcoe_rx_list.lock);
  1350. return NET_RX_SUCCESS;
  1351. err:
  1352. per_cpu_ptr(lport->stats, get_cpu())->ErrorFrames++;
  1353. put_cpu();
  1354. err2:
  1355. kfree_skb(skb);
  1356. return NET_RX_DROP;
  1357. }
  1358. /**
  1359. * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
  1360. * @skb: The packet to be transmitted
  1361. * @tlen: The total length of the trailer
  1362. *
  1363. * Returns: 0 for success
  1364. */
  1365. static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
  1366. {
  1367. struct fcoe_percpu_s *fps;
  1368. int rc;
  1369. fps = &get_cpu_var(fcoe_percpu);
  1370. rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
  1371. put_cpu_var(fcoe_percpu);
  1372. return rc;
  1373. }
  1374. /**
  1375. * fcoe_xmit() - Transmit a FCoE frame
  1376. * @lport: The local port that the frame is to be transmitted for
  1377. * @fp: The frame to be transmitted
  1378. *
  1379. * Return: 0 for success
  1380. */
  1381. static int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
  1382. {
  1383. int wlen;
  1384. u32 crc;
  1385. struct ethhdr *eh;
  1386. struct fcoe_crc_eof *cp;
  1387. struct sk_buff *skb;
  1388. struct fc_stats *stats;
  1389. struct fc_frame_header *fh;
  1390. unsigned int hlen; /* header length implies the version */
  1391. unsigned int tlen; /* trailer length */
  1392. unsigned int elen; /* eth header, may include vlan */
  1393. struct fcoe_port *port = lport_priv(lport);
  1394. struct fcoe_interface *fcoe = port->priv;
  1395. struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
  1396. u8 sof, eof;
  1397. struct fcoe_hdr *hp;
  1398. WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
  1399. fh = fc_frame_header_get(fp);
  1400. skb = fp_skb(fp);
  1401. wlen = skb->len / FCOE_WORD_TO_BYTE;
  1402. if (!lport->link_up) {
  1403. kfree_skb(skb);
  1404. return 0;
  1405. }
  1406. if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
  1407. fcoe_ctlr_els_send(ctlr, lport, skb))
  1408. return 0;
  1409. sof = fr_sof(fp);
  1410. eof = fr_eof(fp);
  1411. elen = sizeof(struct ethhdr);
  1412. hlen = sizeof(struct fcoe_hdr);
  1413. tlen = sizeof(struct fcoe_crc_eof);
  1414. wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
  1415. /* crc offload */
  1416. if (likely(lport->crc_offload)) {
  1417. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1418. skb->csum_start = skb_headroom(skb);
  1419. skb->csum_offset = skb->len;
  1420. crc = 0;
  1421. } else {
  1422. skb->ip_summed = CHECKSUM_NONE;
  1423. crc = fcoe_fc_crc(fp);
  1424. }
  1425. /* copy port crc and eof to the skb buff */
  1426. if (skb_is_nonlinear(skb)) {
  1427. skb_frag_t *frag;
  1428. if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
  1429. kfree_skb(skb);
  1430. return -ENOMEM;
  1431. }
  1432. frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
  1433. cp = kmap_atomic(skb_frag_page(frag))
  1434. + frag->page_offset;
  1435. } else {
  1436. cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
  1437. }
  1438. memset(cp, 0, sizeof(*cp));
  1439. cp->fcoe_eof = eof;
  1440. cp->fcoe_crc32 = cpu_to_le32(~crc);
  1441. if (skb_is_nonlinear(skb)) {
  1442. kunmap_atomic(cp);
  1443. cp = NULL;
  1444. }
  1445. /* adjust skb network/transport offsets to match mac/fcoe/port */
  1446. skb_push(skb, elen + hlen);
  1447. skb_reset_mac_header(skb);
  1448. skb_reset_network_header(skb);
  1449. skb->mac_len = elen;
  1450. skb->protocol = htons(ETH_P_FCOE);
  1451. skb->priority = fcoe->priority;
  1452. if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN &&
  1453. fcoe->realdev->features & NETIF_F_HW_VLAN_CTAG_TX) {
  1454. /* must set skb->dev before calling vlan_put_tag */
  1455. skb->dev = fcoe->realdev;
  1456. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  1457. vlan_dev_vlan_id(fcoe->netdev));
  1458. } else
  1459. skb->dev = fcoe->netdev;
  1460. /* fill up mac and fcoe headers */
  1461. eh = eth_hdr(skb);
  1462. eh->h_proto = htons(ETH_P_FCOE);
  1463. memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
  1464. if (ctlr->map_dest)
  1465. memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
  1466. if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
  1467. memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
  1468. else
  1469. memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
  1470. hp = (struct fcoe_hdr *)(eh + 1);
  1471. memset(hp, 0, sizeof(*hp));
  1472. if (FC_FCOE_VER)
  1473. FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
  1474. hp->fcoe_sof = sof;
  1475. /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
  1476. if (lport->seq_offload && fr_max_payload(fp)) {
  1477. skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
  1478. skb_shinfo(skb)->gso_size = fr_max_payload(fp);
  1479. } else {
  1480. skb_shinfo(skb)->gso_type = 0;
  1481. skb_shinfo(skb)->gso_size = 0;
  1482. }
  1483. /* update tx stats: regardless if LLD fails */
  1484. stats = per_cpu_ptr(lport->stats, get_cpu());
  1485. stats->TxFrames++;
  1486. stats->TxWords += wlen;
  1487. put_cpu();
  1488. /* send down to lld */
  1489. fr_dev(fp) = lport;
  1490. fcoe_port_send(port, skb);
  1491. return 0;
  1492. }
  1493. /**
  1494. * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
  1495. * @skb: The completed skb (argument required by destructor)
  1496. */
  1497. static void fcoe_percpu_flush_done(struct sk_buff *skb)
  1498. {
  1499. complete(&fcoe_flush_completion);
  1500. }
  1501. /**
  1502. * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
  1503. * @lport: The local port the frame was received on
  1504. * @fp: The received frame
  1505. *
  1506. * Return: 0 on passing filtering checks
  1507. */
  1508. static inline int fcoe_filter_frames(struct fc_lport *lport,
  1509. struct fc_frame *fp)
  1510. {
  1511. struct fcoe_ctlr *ctlr;
  1512. struct fcoe_interface *fcoe;
  1513. struct fc_frame_header *fh;
  1514. struct sk_buff *skb = (struct sk_buff *)fp;
  1515. struct fc_stats *stats;
  1516. /*
  1517. * We only check CRC if no offload is available and if it is
  1518. * it's solicited data, in which case, the FCP layer would
  1519. * check it during the copy.
  1520. */
  1521. if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
  1522. fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
  1523. else
  1524. fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
  1525. fh = (struct fc_frame_header *) skb_transport_header(skb);
  1526. fh = fc_frame_header_get(fp);
  1527. if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
  1528. return 0;
  1529. fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
  1530. ctlr = fcoe_to_ctlr(fcoe);
  1531. if (is_fip_mode(ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
  1532. ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
  1533. FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
  1534. return -EINVAL;
  1535. }
  1536. if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
  1537. le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
  1538. fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
  1539. return 0;
  1540. }
  1541. stats = per_cpu_ptr(lport->stats, get_cpu());
  1542. stats->InvalidCRCCount++;
  1543. if (stats->InvalidCRCCount < 5)
  1544. printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
  1545. put_cpu();
  1546. return -EINVAL;
  1547. }
  1548. /**
  1549. * fcoe_recv_frame() - process a single received frame
  1550. * @skb: frame to process
  1551. */
  1552. static void fcoe_recv_frame(struct sk_buff *skb)
  1553. {
  1554. u32 fr_len;
  1555. struct fc_lport *lport;
  1556. struct fcoe_rcv_info *fr;
  1557. struct fc_stats *stats;
  1558. struct fcoe_crc_eof crc_eof;
  1559. struct fc_frame *fp;
  1560. struct fcoe_port *port;
  1561. struct fcoe_hdr *hp;
  1562. fr = fcoe_dev_from_skb(skb);
  1563. lport = fr->fr_dev;
  1564. if (unlikely(!lport)) {
  1565. if (skb->destructor != fcoe_percpu_flush_done)
  1566. FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb\n");
  1567. kfree_skb(skb);
  1568. return;
  1569. }
  1570. FCOE_NETDEV_DBG(skb->dev,
  1571. "skb_info: len:%d data_len:%d head:%p data:%p tail:%p end:%p sum:%d dev:%s\n",
  1572. skb->len, skb->data_len,
  1573. skb->head, skb->data, skb_tail_pointer(skb),
  1574. skb_end_pointer(skb), skb->csum,
  1575. skb->dev ? skb->dev->name : "<NULL>");
  1576. port = lport_priv(lport);
  1577. skb_linearize(skb); /* check for skb_is_nonlinear is within skb_linearize */
  1578. /*
  1579. * Frame length checks and setting up the header pointers
  1580. * was done in fcoe_rcv already.
  1581. */
  1582. hp = (struct fcoe_hdr *) skb_network_header(skb);
  1583. stats = per_cpu_ptr(lport->stats, get_cpu());
  1584. if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
  1585. if (stats->ErrorFrames < 5)
  1586. printk(KERN_WARNING "fcoe: FCoE version "
  1587. "mismatch: The frame has "
  1588. "version %x, but the "
  1589. "initiator supports version "
  1590. "%x\n", FC_FCOE_DECAPS_VER(hp),
  1591. FC_FCOE_VER);
  1592. goto drop;
  1593. }
  1594. skb_pull(skb, sizeof(struct fcoe_hdr));
  1595. fr_len = skb->len - sizeof(struct fcoe_crc_eof);
  1596. stats->RxFrames++;
  1597. stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
  1598. fp = (struct fc_frame *)skb;
  1599. fc_frame_init(fp);
  1600. fr_dev(fp) = lport;
  1601. fr_sof(fp) = hp->fcoe_sof;
  1602. /* Copy out the CRC and EOF trailer for access */
  1603. if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
  1604. goto drop;
  1605. fr_eof(fp) = crc_eof.fcoe_eof;
  1606. fr_crc(fp) = crc_eof.fcoe_crc32;
  1607. if (pskb_trim(skb, fr_len))
  1608. goto drop;
  1609. if (!fcoe_filter_frames(lport, fp)) {
  1610. put_cpu();
  1611. fc_exch_recv(lport, fp);
  1612. return;
  1613. }
  1614. drop:
  1615. stats->ErrorFrames++;
  1616. put_cpu();
  1617. kfree_skb(skb);
  1618. }
  1619. /**
  1620. * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
  1621. * @arg: The per-CPU context
  1622. *
  1623. * Return: 0 for success
  1624. */
  1625. static int fcoe_percpu_receive_thread(void *arg)
  1626. {
  1627. struct fcoe_percpu_s *p = arg;
  1628. struct sk_buff *skb;
  1629. struct sk_buff_head tmp;
  1630. skb_queue_head_init(&tmp);
  1631. set_user_nice(current, MIN_NICE);
  1632. retry:
  1633. while (!kthread_should_stop()) {
  1634. spin_lock_bh(&p->fcoe_rx_list.lock);
  1635. skb_queue_splice_init(&p->fcoe_rx_list, &tmp);
  1636. if (!skb_queue_len(&tmp)) {
  1637. set_current_state(TASK_INTERRUPTIBLE);
  1638. spin_unlock_bh(&p->fcoe_rx_list.lock);
  1639. schedule();
  1640. goto retry;
  1641. }
  1642. spin_unlock_bh(&p->fcoe_rx_list.lock);
  1643. while ((skb = __skb_dequeue(&tmp)) != NULL)
  1644. fcoe_recv_frame(skb);
  1645. }
  1646. return 0;
  1647. }
  1648. /**
  1649. * fcoe_dev_setup() - Setup the link change notification interface
  1650. */
  1651. static void fcoe_dev_setup(void)
  1652. {
  1653. register_dcbevent_notifier(&dcb_notifier);
  1654. register_netdevice_notifier(&fcoe_notifier);
  1655. }
  1656. /**
  1657. * fcoe_dev_cleanup() - Cleanup the link change notification interface
  1658. */
  1659. static void fcoe_dev_cleanup(void)
  1660. {
  1661. unregister_dcbevent_notifier(&dcb_notifier);
  1662. unregister_netdevice_notifier(&fcoe_notifier);
  1663. }
  1664. static struct fcoe_interface *
  1665. fcoe_hostlist_lookup_realdev_port(struct net_device *netdev)
  1666. {
  1667. struct fcoe_interface *fcoe;
  1668. struct net_device *real_dev;
  1669. list_for_each_entry(fcoe, &fcoe_hostlist, list) {
  1670. if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
  1671. real_dev = vlan_dev_real_dev(fcoe->netdev);
  1672. else
  1673. real_dev = fcoe->netdev;
  1674. if (netdev == real_dev)
  1675. return fcoe;
  1676. }
  1677. return NULL;
  1678. }
  1679. static int fcoe_dcb_app_notification(struct notifier_block *notifier,
  1680. ulong event, void *ptr)
  1681. {
  1682. struct dcb_app_type *entry = ptr;
  1683. struct fcoe_ctlr *ctlr;
  1684. struct fcoe_interface *fcoe;
  1685. struct net_device *netdev;
  1686. int prio;
  1687. if (entry->app.selector != DCB_APP_IDTYPE_ETHTYPE)
  1688. return NOTIFY_OK;
  1689. netdev = dev_get_by_index(&init_net, entry->ifindex);
  1690. if (!netdev)
  1691. return NOTIFY_OK;
  1692. fcoe = fcoe_hostlist_lookup_realdev_port(netdev);
  1693. dev_put(netdev);
  1694. if (!fcoe)
  1695. return NOTIFY_OK;
  1696. ctlr = fcoe_to_ctlr(fcoe);
  1697. if (entry->dcbx & DCB_CAP_DCBX_VER_CEE)
  1698. prio = ffs(entry->app.priority) - 1;
  1699. else
  1700. prio = entry->app.priority;
  1701. if (prio < 0)
  1702. return NOTIFY_OK;
  1703. if (entry->app.protocol == ETH_P_FIP ||
  1704. entry->app.protocol == ETH_P_FCOE)
  1705. ctlr->priority = prio;
  1706. if (entry->app.protocol == ETH_P_FCOE)
  1707. fcoe->priority = prio;
  1708. return NOTIFY_OK;
  1709. }
  1710. /**
  1711. * fcoe_device_notification() - Handler for net device events
  1712. * @notifier: The context of the notification
  1713. * @event: The type of event
  1714. * @ptr: The net device that the event was on
  1715. *
  1716. * This function is called by the Ethernet driver in case of link change event.
  1717. *
  1718. * Returns: 0 for success
  1719. */
  1720. static int fcoe_device_notification(struct notifier_block *notifier,
  1721. ulong event, void *ptr)
  1722. {
  1723. struct fcoe_ctlr_device *cdev;
  1724. struct fc_lport *lport = NULL;
  1725. struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
  1726. struct fcoe_ctlr *ctlr;
  1727. struct fcoe_interface *fcoe;
  1728. struct fcoe_port *port;
  1729. struct fc_stats *stats;
  1730. u32 link_possible = 1;
  1731. u32 mfs;
  1732. int rc = NOTIFY_OK;
  1733. list_for_each_entry(fcoe, &fcoe_hostlist, list) {
  1734. if (fcoe->netdev == netdev) {
  1735. ctlr = fcoe_to_ctlr(fcoe);
  1736. lport = ctlr->lp;
  1737. break;
  1738. }
  1739. }
  1740. if (!lport) {
  1741. rc = NOTIFY_DONE;
  1742. goto out;
  1743. }
  1744. switch (event) {
  1745. case NETDEV_DOWN:
  1746. case NETDEV_GOING_DOWN:
  1747. link_possible = 0;
  1748. break;
  1749. case NETDEV_UP:
  1750. case NETDEV_CHANGE:
  1751. break;
  1752. case NETDEV_CHANGEMTU:
  1753. if (netdev->features & NETIF_F_FCOE_MTU)
  1754. break;
  1755. mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
  1756. sizeof(struct fcoe_crc_eof));
  1757. if (mfs >= FC_MIN_MAX_FRAME)
  1758. fc_set_mfs(lport, mfs);
  1759. break;
  1760. case NETDEV_REGISTER:
  1761. break;
  1762. case NETDEV_UNREGISTER:
  1763. list_del(&fcoe->list);
  1764. port = lport_priv(ctlr->lp);
  1765. queue_work(fcoe_wq, &port->destroy_work);
  1766. goto out;
  1767. break;
  1768. case NETDEV_FEAT_CHANGE:
  1769. fcoe_netdev_features_change(lport, netdev);
  1770. break;
  1771. default:
  1772. FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
  1773. "from netdev netlink\n", event);
  1774. }
  1775. fcoe_link_speed_update(lport);
  1776. cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
  1777. if (link_possible && !fcoe_link_ok(lport)) {
  1778. switch (cdev->enabled) {
  1779. case FCOE_CTLR_DISABLED:
  1780. pr_info("Link up while interface is disabled.\n");
  1781. break;
  1782. case FCOE_CTLR_ENABLED:
  1783. case FCOE_CTLR_UNUSED:
  1784. fcoe_ctlr_link_up(ctlr);
  1785. };
  1786. } else if (fcoe_ctlr_link_down(ctlr)) {
  1787. switch (cdev->enabled) {
  1788. case FCOE_CTLR_DISABLED:
  1789. pr_info("Link down while interface is disabled.\n");
  1790. break;
  1791. case FCOE_CTLR_ENABLED:
  1792. case FCOE_CTLR_UNUSED:
  1793. stats = per_cpu_ptr(lport->stats, get_cpu());
  1794. stats->LinkFailureCount++;
  1795. put_cpu();
  1796. fcoe_clean_pending_queue(lport);
  1797. };
  1798. }
  1799. out:
  1800. return rc;
  1801. }
  1802. /**
  1803. * fcoe_disable() - Disables a FCoE interface
  1804. * @netdev : The net_device object the Ethernet interface to create on
  1805. *
  1806. * Called from fcoe transport.
  1807. *
  1808. * Returns: 0 for success
  1809. *
  1810. * Deprecated: use fcoe_ctlr_enabled()
  1811. */
  1812. static int fcoe_disable(struct net_device *netdev)
  1813. {
  1814. struct fcoe_ctlr *ctlr;
  1815. struct fcoe_interface *fcoe;
  1816. int rc = 0;
  1817. mutex_lock(&fcoe_config_mutex);
  1818. rtnl_lock();
  1819. fcoe = fcoe_hostlist_lookup_port(netdev);
  1820. rtnl_unlock();
  1821. if (fcoe) {
  1822. ctlr = fcoe_to_ctlr(fcoe);
  1823. fcoe_ctlr_link_down(ctlr);
  1824. fcoe_clean_pending_queue(ctlr->lp);
  1825. } else
  1826. rc = -ENODEV;
  1827. mutex_unlock(&fcoe_config_mutex);
  1828. return rc;
  1829. }
  1830. /**
  1831. * fcoe_enable() - Enables a FCoE interface
  1832. * @netdev : The net_device object the Ethernet interface to create on
  1833. *
  1834. * Called from fcoe transport.
  1835. *
  1836. * Returns: 0 for success
  1837. */
  1838. static int fcoe_enable(struct net_device *netdev)
  1839. {
  1840. struct fcoe_ctlr *ctlr;
  1841. struct fcoe_interface *fcoe;
  1842. int rc = 0;
  1843. mutex_lock(&fcoe_config_mutex);
  1844. rtnl_lock();
  1845. fcoe = fcoe_hostlist_lookup_port(netdev);
  1846. rtnl_unlock();
  1847. if (!fcoe) {
  1848. rc = -ENODEV;
  1849. goto out;
  1850. }
  1851. ctlr = fcoe_to_ctlr(fcoe);
  1852. if (!fcoe_link_ok(ctlr->lp))
  1853. fcoe_ctlr_link_up(ctlr);
  1854. out:
  1855. mutex_unlock(&fcoe_config_mutex);
  1856. return rc;
  1857. }
  1858. /**
  1859. * fcoe_ctlr_enabled() - Enable or disable an FCoE Controller
  1860. * @cdev: The FCoE Controller that is being enabled or disabled
  1861. *
  1862. * fcoe_sysfs will ensure that the state of 'enabled' has
  1863. * changed, so no checking is necessary here. This routine simply
  1864. * calls fcoe_enable or fcoe_disable, both of which are deprecated.
  1865. * When those routines are removed the functionality can be merged
  1866. * here.
  1867. */
  1868. static int fcoe_ctlr_enabled(struct fcoe_ctlr_device *cdev)
  1869. {
  1870. struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
  1871. struct fc_lport *lport = ctlr->lp;
  1872. struct net_device *netdev = fcoe_netdev(lport);
  1873. switch (cdev->enabled) {
  1874. case FCOE_CTLR_ENABLED:
  1875. return fcoe_enable(netdev);
  1876. case FCOE_CTLR_DISABLED:
  1877. return fcoe_disable(netdev);
  1878. case FCOE_CTLR_UNUSED:
  1879. default:
  1880. return -ENOTSUPP;
  1881. };
  1882. }
  1883. /**
  1884. * fcoe_destroy() - Destroy a FCoE interface
  1885. * @netdev : The net_device object the Ethernet interface to create on
  1886. *
  1887. * Called from fcoe transport
  1888. *
  1889. * Returns: 0 for success
  1890. */
  1891. static int fcoe_destroy(struct net_device *netdev)
  1892. {
  1893. struct fcoe_ctlr *ctlr;
  1894. struct fcoe_interface *fcoe;
  1895. struct fc_lport *lport;
  1896. struct fcoe_port *port;
  1897. int rc = 0;
  1898. mutex_lock(&fcoe_config_mutex);
  1899. rtnl_lock();
  1900. fcoe = fcoe_hostlist_lookup_port(netdev);
  1901. if (!fcoe) {
  1902. rc = -ENODEV;
  1903. goto out_nodev;
  1904. }
  1905. ctlr = fcoe_to_ctlr(fcoe);
  1906. lport = ctlr->lp;
  1907. port = lport_priv(lport);
  1908. list_del(&fcoe->list);
  1909. queue_work(fcoe_wq, &port->destroy_work);
  1910. out_nodev:
  1911. rtnl_unlock();
  1912. mutex_unlock(&fcoe_config_mutex);
  1913. return rc;
  1914. }
  1915. /**
  1916. * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
  1917. * @work: Handle to the FCoE port to be destroyed
  1918. */
  1919. static void fcoe_destroy_work(struct work_struct *work)
  1920. {
  1921. struct fcoe_ctlr_device *cdev;
  1922. struct fcoe_ctlr *ctlr;
  1923. struct fcoe_port *port;
  1924. struct fcoe_interface *fcoe;
  1925. struct Scsi_Host *shost;
  1926. struct fc_host_attrs *fc_host;
  1927. unsigned long flags;
  1928. struct fc_vport *vport;
  1929. struct fc_vport *next_vport;
  1930. port = container_of(work, struct fcoe_port, destroy_work);
  1931. shost = port->lport->host;
  1932. fc_host = shost_to_fc_host(shost);
  1933. /* Loop through all the vports and mark them for deletion */
  1934. spin_lock_irqsave(shost->host_lock, flags);
  1935. list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers) {
  1936. if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) {
  1937. continue;
  1938. } else {
  1939. vport->flags |= FC_VPORT_DELETING;
  1940. queue_work(fc_host_work_q(shost),
  1941. &vport->vport_delete_work);
  1942. }
  1943. }
  1944. spin_unlock_irqrestore(shost->host_lock, flags);
  1945. flush_workqueue(fc_host_work_q(shost));
  1946. mutex_lock(&fcoe_config_mutex);
  1947. fcoe = port->priv;
  1948. ctlr = fcoe_to_ctlr(fcoe);
  1949. cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
  1950. fcoe_if_destroy(port->lport);
  1951. fcoe_interface_cleanup(fcoe);
  1952. mutex_unlock(&fcoe_config_mutex);
  1953. fcoe_ctlr_device_delete(cdev);
  1954. }
  1955. /**
  1956. * fcoe_match() - Check if the FCoE is supported on the given netdevice
  1957. * @netdev : The net_device object the Ethernet interface to create on
  1958. *
  1959. * Called from fcoe transport.
  1960. *
  1961. * Returns: always returns true as this is the default FCoE transport,
  1962. * i.e., support all netdevs.
  1963. */
  1964. static bool fcoe_match(struct net_device *netdev)
  1965. {
  1966. return true;
  1967. }
  1968. /**
  1969. * fcoe_dcb_create() - Initialize DCB attributes and hooks
  1970. * @netdev: The net_device object of the L2 link that should be queried
  1971. * @port: The fcoe_port to bind FCoE APP priority with
  1972. * @
  1973. */
  1974. static void fcoe_dcb_create(struct fcoe_interface *fcoe)
  1975. {
  1976. #ifdef CONFIG_DCB
  1977. int dcbx;
  1978. u8 fup, up;
  1979. struct net_device *netdev = fcoe->realdev;
  1980. struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
  1981. struct dcb_app app = {
  1982. .priority = 0,
  1983. .protocol = ETH_P_FCOE
  1984. };
  1985. /* setup DCB priority attributes. */
  1986. if (netdev && netdev->dcbnl_ops && netdev->dcbnl_ops->getdcbx) {
  1987. dcbx = netdev->dcbnl_ops->getdcbx(netdev);
  1988. if (dcbx & DCB_CAP_DCBX_VER_IEEE) {
  1989. app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
  1990. up = dcb_ieee_getapp_mask(netdev, &app);
  1991. app.protocol = ETH_P_FIP;
  1992. fup = dcb_ieee_getapp_mask(netdev, &app);
  1993. } else {
  1994. app.selector = DCB_APP_IDTYPE_ETHTYPE;
  1995. up = dcb_getapp(netdev, &app);
  1996. app.protocol = ETH_P_FIP;
  1997. fup = dcb_getapp(netdev, &app);
  1998. }
  1999. fcoe->priority = ffs(up) ? ffs(up) - 1 : 0;
  2000. ctlr->priority = ffs(fup) ? ffs(fup) - 1 : fcoe->priority;
  2001. }
  2002. #endif
  2003. }
  2004. enum fcoe_create_link_state {
  2005. FCOE_CREATE_LINK_DOWN,
  2006. FCOE_CREATE_LINK_UP,
  2007. };
  2008. /**
  2009. * _fcoe_create() - (internal) Create a fcoe interface
  2010. * @netdev : The net_device object the Ethernet interface to create on
  2011. * @fip_mode: The FIP mode for this creation
  2012. * @link_state: The ctlr link state on creation
  2013. *
  2014. * Called from either the libfcoe 'create' module parameter
  2015. * via fcoe_create or from fcoe_syfs's ctlr_create file.
  2016. *
  2017. * libfcoe's 'create' module parameter is deprecated so some
  2018. * consolidation of code can be done when that interface is
  2019. * removed.
  2020. */
  2021. static int _fcoe_create(struct net_device *netdev, enum fip_state fip_mode,
  2022. enum fcoe_create_link_state link_state)
  2023. {
  2024. int rc = 0;
  2025. struct fcoe_ctlr_device *ctlr_dev;
  2026. struct fcoe_ctlr *ctlr;
  2027. struct fcoe_interface *fcoe;
  2028. struct fc_lport *lport;
  2029. mutex_lock(&fcoe_config_mutex);
  2030. rtnl_lock();
  2031. /* look for existing lport */
  2032. if (fcoe_hostlist_lookup(netdev)) {
  2033. rc = -EEXIST;
  2034. goto out_nodev;
  2035. }
  2036. fcoe = fcoe_interface_create(netdev, fip_mode);
  2037. if (IS_ERR(fcoe)) {
  2038. rc = PTR_ERR(fcoe);
  2039. goto out_nodev;
  2040. }
  2041. ctlr = fcoe_to_ctlr(fcoe);
  2042. ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
  2043. lport = fcoe_if_create(fcoe, &ctlr_dev->dev, 0);
  2044. if (IS_ERR(lport)) {
  2045. printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
  2046. netdev->name);
  2047. rc = -EIO;
  2048. rtnl_unlock();
  2049. fcoe_interface_cleanup(fcoe);
  2050. mutex_unlock(&fcoe_config_mutex);
  2051. fcoe_ctlr_device_delete(ctlr_dev);
  2052. goto out;
  2053. }
  2054. /* Make this the "master" N_Port */
  2055. ctlr->lp = lport;
  2056. /* setup DCB priority attributes. */
  2057. fcoe_dcb_create(fcoe);
  2058. /* start FIP Discovery and FLOGI */
  2059. lport->boot_time = jiffies;
  2060. fc_fabric_login(lport);
  2061. /*
  2062. * If the fcoe_ctlr_device is to be set to DISABLED
  2063. * it must be done after the lport is added to the
  2064. * hostlist, but before the rtnl_lock is released.
  2065. * This is because the rtnl_lock protects the
  2066. * hostlist that fcoe_device_notification uses. If
  2067. * the FCoE Controller is intended to be created
  2068. * DISABLED then 'enabled' needs to be considered
  2069. * handling link events. 'enabled' must be set
  2070. * before the lport can be found in the hostlist
  2071. * when a link up event is received.
  2072. */
  2073. if (link_state == FCOE_CREATE_LINK_UP)
  2074. ctlr_dev->enabled = FCOE_CTLR_ENABLED;
  2075. else
  2076. ctlr_dev->enabled = FCOE_CTLR_DISABLED;
  2077. if (link_state == FCOE_CREATE_LINK_UP &&
  2078. !fcoe_link_ok(lport)) {
  2079. rtnl_unlock();
  2080. fcoe_ctlr_link_up(ctlr);
  2081. mutex_unlock(&fcoe_config_mutex);
  2082. return rc;
  2083. }
  2084. out_nodev:
  2085. rtnl_unlock();
  2086. mutex_unlock(&fcoe_config_mutex);
  2087. out:
  2088. return rc;
  2089. }
  2090. /**
  2091. * fcoe_create() - Create a fcoe interface
  2092. * @netdev : The net_device object the Ethernet interface to create on
  2093. * @fip_mode: The FIP mode for this creation
  2094. *
  2095. * Called from fcoe transport
  2096. *
  2097. * Returns: 0 for success
  2098. */
  2099. static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
  2100. {
  2101. return _fcoe_create(netdev, fip_mode, FCOE_CREATE_LINK_UP);
  2102. }
  2103. /**
  2104. * fcoe_ctlr_alloc() - Allocate a fcoe interface from fcoe_sysfs
  2105. * @netdev: The net_device to be used by the allocated FCoE Controller
  2106. *
  2107. * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
  2108. * in a link_down state. The allows the user an opportunity to configure
  2109. * the FCoE Controller from sysfs before enabling the FCoE Controller.
  2110. *
  2111. * Creating in with this routine starts the FCoE Controller in Fabric
  2112. * mode. The user can change to VN2VN or another mode before enabling.
  2113. */
  2114. static int fcoe_ctlr_alloc(struct net_device *netdev)
  2115. {
  2116. return _fcoe_create(netdev, FIP_MODE_FABRIC,
  2117. FCOE_CREATE_LINK_DOWN);
  2118. }
  2119. /**
  2120. * fcoe_link_ok() - Check if the link is OK for a local port
  2121. * @lport: The local port to check link on
  2122. *
  2123. * Returns: 0 if link is UP and OK, -1 if not
  2124. *
  2125. */
  2126. static int fcoe_link_ok(struct fc_lport *lport)
  2127. {
  2128. struct net_device *netdev = fcoe_netdev(lport);
  2129. if (netif_oper_up(netdev))
  2130. return 0;
  2131. return -1;
  2132. }
  2133. /**
  2134. * fcoe_percpu_clean() - Clear all pending skbs for an local port
  2135. * @lport: The local port whose skbs are to be cleared
  2136. *
  2137. * Must be called with fcoe_create_mutex held to single-thread completion.
  2138. *
  2139. * This flushes the pending skbs by adding a new skb to each queue and
  2140. * waiting until they are all freed. This assures us that not only are
  2141. * there no packets that will be handled by the lport, but also that any
  2142. * threads already handling packet have returned.
  2143. */
  2144. static void fcoe_percpu_clean(struct fc_lport *lport)
  2145. {
  2146. struct fcoe_percpu_s *pp;
  2147. struct sk_buff *skb;
  2148. unsigned int cpu;
  2149. for_each_possible_cpu(cpu) {
  2150. pp = &per_cpu(fcoe_percpu, cpu);
  2151. if (!pp->thread || !cpu_online(cpu))
  2152. continue;
  2153. skb = dev_alloc_skb(0);
  2154. if (!skb)
  2155. continue;
  2156. skb->destructor = fcoe_percpu_flush_done;
  2157. spin_lock_bh(&pp->fcoe_rx_list.lock);
  2158. __skb_queue_tail(&pp->fcoe_rx_list, skb);
  2159. if (pp->fcoe_rx_list.qlen == 1)
  2160. wake_up_process(pp->thread);
  2161. spin_unlock_bh(&pp->fcoe_rx_list.lock);
  2162. wait_for_completion(&fcoe_flush_completion);
  2163. }
  2164. }
  2165. /**
  2166. * fcoe_reset() - Reset a local port
  2167. * @shost: The SCSI host associated with the local port to be reset
  2168. *
  2169. * Returns: Always 0 (return value required by FC transport template)
  2170. */
  2171. static int fcoe_reset(struct Scsi_Host *shost)
  2172. {
  2173. struct fc_lport *lport = shost_priv(shost);
  2174. struct fcoe_port *port = lport_priv(lport);
  2175. struct fcoe_interface *fcoe = port->priv;
  2176. struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
  2177. struct fcoe_ctlr_device *cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
  2178. fcoe_ctlr_link_down(ctlr);
  2179. fcoe_clean_pending_queue(ctlr->lp);
  2180. if (cdev->enabled != FCOE_CTLR_DISABLED &&
  2181. !fcoe_link_ok(ctlr->lp))
  2182. fcoe_ctlr_link_up(ctlr);
  2183. return 0;
  2184. }
  2185. /**
  2186. * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
  2187. * @netdev: The net device used as a key
  2188. *
  2189. * Locking: Must be called with the RNL mutex held.
  2190. *
  2191. * Returns: NULL or the FCoE interface
  2192. */
  2193. static struct fcoe_interface *
  2194. fcoe_hostlist_lookup_port(const struct net_device *netdev)
  2195. {
  2196. struct fcoe_interface *fcoe;
  2197. list_for_each_entry(fcoe, &fcoe_hostlist, list) {
  2198. if (fcoe->netdev == netdev)
  2199. return fcoe;
  2200. }
  2201. return NULL;
  2202. }
  2203. /**
  2204. * fcoe_hostlist_lookup() - Find the local port associated with a
  2205. * given net device
  2206. * @netdev: The netdevice used as a key
  2207. *
  2208. * Locking: Must be called with the RTNL mutex held
  2209. *
  2210. * Returns: NULL or the local port
  2211. */
  2212. static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
  2213. {
  2214. struct fcoe_ctlr *ctlr;
  2215. struct fcoe_interface *fcoe;
  2216. fcoe = fcoe_hostlist_lookup_port(netdev);
  2217. ctlr = fcoe_to_ctlr(fcoe);
  2218. return (fcoe) ? ctlr->lp : NULL;
  2219. }
  2220. /**
  2221. * fcoe_hostlist_add() - Add the FCoE interface identified by a local
  2222. * port to the hostlist
  2223. * @lport: The local port that identifies the FCoE interface to be added
  2224. *
  2225. * Locking: must be called with the RTNL mutex held
  2226. *
  2227. * Returns: 0 for success
  2228. */
  2229. static int fcoe_hostlist_add(const struct fc_lport *lport)
  2230. {
  2231. struct fcoe_interface *fcoe;
  2232. struct fcoe_port *port;
  2233. fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
  2234. if (!fcoe) {
  2235. port = lport_priv(lport);
  2236. fcoe = port->priv;
  2237. list_add_tail(&fcoe->list, &fcoe_hostlist);
  2238. }
  2239. return 0;
  2240. }
  2241. /**
  2242. * fcoe_hostlist_del() - Remove the FCoE interface identified by a local
  2243. * port to the hostlist
  2244. * @lport: The local port that identifies the FCoE interface to be added
  2245. *
  2246. * Locking: must be called with the RTNL mutex held
  2247. *
  2248. */
  2249. static void fcoe_hostlist_del(const struct fc_lport *lport)
  2250. {
  2251. struct fcoe_interface *fcoe;
  2252. struct fcoe_port *port;
  2253. port = lport_priv(lport);
  2254. fcoe = port->priv;
  2255. list_del(&fcoe->list);
  2256. return;
  2257. }
  2258. static struct fcoe_transport fcoe_sw_transport = {
  2259. .name = {FCOE_TRANSPORT_DEFAULT},
  2260. .attached = false,
  2261. .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
  2262. .match = fcoe_match,
  2263. .alloc = fcoe_ctlr_alloc,
  2264. .create = fcoe_create,
  2265. .destroy = fcoe_destroy,
  2266. .enable = fcoe_enable,
  2267. .disable = fcoe_disable,
  2268. };
  2269. /**
  2270. * fcoe_init() - Initialize fcoe.ko
  2271. *
  2272. * Returns: 0 on success, or a negative value on failure
  2273. */
  2274. static int __init fcoe_init(void)
  2275. {
  2276. struct fcoe_percpu_s *p;
  2277. unsigned int cpu;
  2278. int rc = 0;
  2279. fcoe_wq = alloc_workqueue("fcoe", 0, 0);
  2280. if (!fcoe_wq)
  2281. return -ENOMEM;
  2282. /* register as a fcoe transport */
  2283. rc = fcoe_transport_attach(&fcoe_sw_transport);
  2284. if (rc) {
  2285. printk(KERN_ERR "failed to register an fcoe transport, check "
  2286. "if libfcoe is loaded\n");
  2287. return rc;
  2288. }
  2289. mutex_lock(&fcoe_config_mutex);
  2290. for_each_possible_cpu(cpu) {
  2291. p = &per_cpu(fcoe_percpu, cpu);
  2292. skb_queue_head_init(&p->fcoe_rx_list);
  2293. }
  2294. cpu_notifier_register_begin();
  2295. for_each_online_cpu(cpu)
  2296. fcoe_percpu_thread_create(cpu);
  2297. /* Initialize per CPU interrupt thread */
  2298. rc = __register_hotcpu_notifier(&fcoe_cpu_notifier);
  2299. if (rc)
  2300. goto out_free;
  2301. cpu_notifier_register_done();
  2302. /* Setup link change notification */
  2303. fcoe_dev_setup();
  2304. rc = fcoe_if_init();
  2305. if (rc)
  2306. goto out_free;
  2307. mutex_unlock(&fcoe_config_mutex);
  2308. return 0;
  2309. out_free:
  2310. for_each_online_cpu(cpu) {
  2311. fcoe_percpu_thread_destroy(cpu);
  2312. }
  2313. cpu_notifier_register_done();
  2314. mutex_unlock(&fcoe_config_mutex);
  2315. destroy_workqueue(fcoe_wq);
  2316. return rc;
  2317. }
  2318. module_init(fcoe_init);
  2319. /**
  2320. * fcoe_exit() - Clean up fcoe.ko
  2321. *
  2322. * Returns: 0 on success or a negative value on failure
  2323. */
  2324. static void __exit fcoe_exit(void)
  2325. {
  2326. struct fcoe_interface *fcoe, *tmp;
  2327. struct fcoe_ctlr *ctlr;
  2328. struct fcoe_port *port;
  2329. unsigned int cpu;
  2330. mutex_lock(&fcoe_config_mutex);
  2331. fcoe_dev_cleanup();
  2332. /* releases the associated fcoe hosts */
  2333. rtnl_lock();
  2334. list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
  2335. ctlr = fcoe_to_ctlr(fcoe);
  2336. port = lport_priv(ctlr->lp);
  2337. fcoe_hostlist_del(port->lport);
  2338. queue_work(fcoe_wq, &port->destroy_work);
  2339. }
  2340. rtnl_unlock();
  2341. cpu_notifier_register_begin();
  2342. for_each_online_cpu(cpu)
  2343. fcoe_percpu_thread_destroy(cpu);
  2344. __unregister_hotcpu_notifier(&fcoe_cpu_notifier);
  2345. cpu_notifier_register_done();
  2346. mutex_unlock(&fcoe_config_mutex);
  2347. /*
  2348. * destroy_work's may be chained but destroy_workqueue()
  2349. * can take care of them. Just kill the fcoe_wq.
  2350. */
  2351. destroy_workqueue(fcoe_wq);
  2352. /*
  2353. * Detaching from the scsi transport must happen after all
  2354. * destroys are done on the fcoe_wq. destroy_workqueue will
  2355. * enusre the fcoe_wq is flushed.
  2356. */
  2357. fcoe_if_exit();
  2358. /* detach from fcoe transport */
  2359. fcoe_transport_detach(&fcoe_sw_transport);
  2360. }
  2361. module_exit(fcoe_exit);
  2362. /**
  2363. * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
  2364. * @seq: active sequence in the FLOGI or FDISC exchange
  2365. * @fp: response frame, or error encoded in a pointer (timeout)
  2366. * @arg: pointer the the fcoe_ctlr structure
  2367. *
  2368. * This handles MAC address management for FCoE, then passes control on to
  2369. * the libfc FLOGI response handler.
  2370. */
  2371. static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  2372. {
  2373. struct fcoe_ctlr *fip = arg;
  2374. struct fc_exch *exch = fc_seq_exch(seq);
  2375. struct fc_lport *lport = exch->lp;
  2376. u8 *mac;
  2377. if (IS_ERR(fp))
  2378. goto done;
  2379. mac = fr_cb(fp)->granted_mac;
  2380. /* pre-FIP */
  2381. if (is_zero_ether_addr(mac))
  2382. fcoe_ctlr_recv_flogi(fip, lport, fp);
  2383. if (!is_zero_ether_addr(mac))
  2384. fcoe_update_src_mac(lport, mac);
  2385. done:
  2386. fc_lport_flogi_resp(seq, fp, lport);
  2387. }
  2388. /**
  2389. * fcoe_logo_resp() - FCoE specific LOGO response handler
  2390. * @seq: active sequence in the LOGO exchange
  2391. * @fp: response frame, or error encoded in a pointer (timeout)
  2392. * @arg: pointer the the fcoe_ctlr structure
  2393. *
  2394. * This handles MAC address management for FCoE, then passes control on to
  2395. * the libfc LOGO response handler.
  2396. */
  2397. static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  2398. {
  2399. struct fc_lport *lport = arg;
  2400. static u8 zero_mac[ETH_ALEN] = { 0 };
  2401. if (!IS_ERR(fp))
  2402. fcoe_update_src_mac(lport, zero_mac);
  2403. fc_lport_logo_resp(seq, fp, lport);
  2404. }
  2405. /**
  2406. * fcoe_elsct_send - FCoE specific ELS handler
  2407. *
  2408. * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
  2409. * using FCoE specific response handlers and passing the FIP controller as
  2410. * the argument (the lport is still available from the exchange).
  2411. *
  2412. * Most of the work here is just handed off to the libfc routine.
  2413. */
  2414. static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
  2415. struct fc_frame *fp, unsigned int op,
  2416. void (*resp)(struct fc_seq *,
  2417. struct fc_frame *,
  2418. void *),
  2419. void *arg, u32 timeout)
  2420. {
  2421. struct fcoe_port *port = lport_priv(lport);
  2422. struct fcoe_interface *fcoe = port->priv;
  2423. struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
  2424. struct fc_frame_header *fh = fc_frame_header_get(fp);
  2425. switch (op) {
  2426. case ELS_FLOGI:
  2427. case ELS_FDISC:
  2428. if (lport->point_to_multipoint)
  2429. break;
  2430. return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
  2431. fip, timeout);
  2432. case ELS_LOGO:
  2433. /* only hook onto fabric logouts, not port logouts */
  2434. if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
  2435. break;
  2436. return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
  2437. lport, timeout);
  2438. }
  2439. return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
  2440. }
  2441. /**
  2442. * fcoe_vport_create() - create an fc_host/scsi_host for a vport
  2443. * @vport: fc_vport object to create a new fc_host for
  2444. * @disabled: start the new fc_host in a disabled state by default?
  2445. *
  2446. * Returns: 0 for success
  2447. */
  2448. static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
  2449. {
  2450. struct Scsi_Host *shost = vport_to_shost(vport);
  2451. struct fc_lport *n_port = shost_priv(shost);
  2452. struct fcoe_port *port = lport_priv(n_port);
  2453. struct fcoe_interface *fcoe = port->priv;
  2454. struct net_device *netdev = fcoe->netdev;
  2455. struct fc_lport *vn_port;
  2456. int rc;
  2457. char buf[32];
  2458. rc = fcoe_validate_vport_create(vport);
  2459. if (rc) {
  2460. fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
  2461. printk(KERN_ERR "fcoe: Failed to create vport, "
  2462. "WWPN (0x%s) already exists\n",
  2463. buf);
  2464. return rc;
  2465. }
  2466. mutex_lock(&fcoe_config_mutex);
  2467. rtnl_lock();
  2468. vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
  2469. rtnl_unlock();
  2470. mutex_unlock(&fcoe_config_mutex);
  2471. if (IS_ERR(vn_port)) {
  2472. printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
  2473. netdev->name);
  2474. return -EIO;
  2475. }
  2476. if (disabled) {
  2477. fc_vport_set_state(vport, FC_VPORT_DISABLED);
  2478. } else {
  2479. vn_port->boot_time = jiffies;
  2480. fc_fabric_login(vn_port);
  2481. fc_vport_setlink(vn_port);
  2482. }
  2483. return 0;
  2484. }
  2485. /**
  2486. * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
  2487. * @vport: fc_vport object that is being destroyed
  2488. *
  2489. * Returns: 0 for success
  2490. */
  2491. static int fcoe_vport_destroy(struct fc_vport *vport)
  2492. {
  2493. struct Scsi_Host *shost = vport_to_shost(vport);
  2494. struct fc_lport *n_port = shost_priv(shost);
  2495. struct fc_lport *vn_port = vport->dd_data;
  2496. mutex_lock(&n_port->lp_mutex);
  2497. list_del(&vn_port->list);
  2498. mutex_unlock(&n_port->lp_mutex);
  2499. mutex_lock(&fcoe_config_mutex);
  2500. fcoe_if_destroy(vn_port);
  2501. mutex_unlock(&fcoe_config_mutex);
  2502. return 0;
  2503. }
  2504. /**
  2505. * fcoe_vport_disable() - change vport state
  2506. * @vport: vport to bring online/offline
  2507. * @disable: should the vport be disabled?
  2508. */
  2509. static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
  2510. {
  2511. struct fc_lport *lport = vport->dd_data;
  2512. if (disable) {
  2513. fc_vport_set_state(vport, FC_VPORT_DISABLED);
  2514. fc_fabric_logoff(lport);
  2515. } else {
  2516. lport->boot_time = jiffies;
  2517. fc_fabric_login(lport);
  2518. fc_vport_setlink(lport);
  2519. }
  2520. return 0;
  2521. }
  2522. /**
  2523. * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
  2524. * @vport: fc_vport with a new symbolic name string
  2525. *
  2526. * After generating a new symbolic name string, a new RSPN_ID request is
  2527. * sent to the name server. There is no response handler, so if it fails
  2528. * for some reason it will not be retried.
  2529. */
  2530. static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
  2531. {
  2532. struct fc_lport *lport = vport->dd_data;
  2533. struct fc_frame *fp;
  2534. size_t len;
  2535. snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
  2536. "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
  2537. fcoe_netdev(lport)->name, vport->symbolic_name);
  2538. if (lport->state != LPORT_ST_READY)
  2539. return;
  2540. len = strnlen(fc_host_symbolic_name(lport->host), 255);
  2541. fp = fc_frame_alloc(lport,
  2542. sizeof(struct fc_ct_hdr) +
  2543. sizeof(struct fc_ns_rspn) + len);
  2544. if (!fp)
  2545. return;
  2546. lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
  2547. NULL, NULL, 3 * lport->r_a_tov);
  2548. }
  2549. static void fcoe_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
  2550. {
  2551. struct fcoe_ctlr_device *ctlr_dev =
  2552. fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
  2553. struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
  2554. struct fcoe_interface *fcoe = fcoe_ctlr_priv(ctlr);
  2555. fcf_dev->vlan_id = vlan_dev_vlan_id(fcoe->netdev);
  2556. }
  2557. /**
  2558. * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
  2559. * @lport: the local port
  2560. * @port_id: the port ID
  2561. * @fp: the received frame, if any, that caused the port_id to be set.
  2562. *
  2563. * This routine handles the case where we received a FLOGI and are
  2564. * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
  2565. * so it can set the non-mapped mode and gateway address.
  2566. *
  2567. * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
  2568. */
  2569. static void fcoe_set_port_id(struct fc_lport *lport,
  2570. u32 port_id, struct fc_frame *fp)
  2571. {
  2572. struct fcoe_port *port = lport_priv(lport);
  2573. struct fcoe_interface *fcoe = port->priv;
  2574. struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
  2575. if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
  2576. fcoe_ctlr_recv_flogi(ctlr, lport, fp);
  2577. }