fcoe.c 67 KB

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