fcoe.c 73 KB

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