fcoe.c 78 KB

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