fcoe.c 64 KB

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