fcoe.c 69 KB

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