fcoe.c 67 KB

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