fcoe.c 65 KB

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