fcoe.c 74 KB

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