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