fcoe.c 72 KB

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