fcoe.c 64 KB

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