fcoe.c 77 KB

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