fcoe.c 72 KB

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