fcoe.c 67 KB

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