fcoe.c 74 KB

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