fcoe.c 70 KB

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