fcoe.c 67 KB

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