fcoe.c 67 KB

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