fcoe.c 67 KB

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