fcoe.c 64 KB

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