fcoe.c 69 KB

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