fcoe.c 71 KB

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