fcoe.c 64 KB

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