fcoe.c 70 KB

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