fcoe.c 74 KB

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