fcoe.c 66 KB

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