fcoe.c 69 KB

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