fcoe.c 73 KB

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