fcoe.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695
  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. unsigned int cpu;
  1061. fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
  1062. lport = fcoe->ctlr.lp;
  1063. if (unlikely(!lport)) {
  1064. FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
  1065. goto err2;
  1066. }
  1067. if (!lport->link_up)
  1068. goto err2;
  1069. FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
  1070. "data:%p tail:%p end:%p sum:%d dev:%s",
  1071. skb->len, skb->data_len, skb->head, skb->data,
  1072. skb_tail_pointer(skb), skb_end_pointer(skb),
  1073. skb->csum, skb->dev ? skb->dev->name : "<NULL>");
  1074. /* check for FCOE packet type */
  1075. if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
  1076. FCOE_NETDEV_DBG(netdev, "Wrong FC type frame");
  1077. goto err;
  1078. }
  1079. /*
  1080. * Check for minimum frame length, and make sure required FCoE
  1081. * and FC headers are pulled into the linear data area.
  1082. */
  1083. if (unlikely((skb->len < FCOE_MIN_FRAME) ||
  1084. !pskb_may_pull(skb, FCOE_HEADER_LEN)))
  1085. goto err;
  1086. skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
  1087. fh = (struct fc_frame_header *) skb_transport_header(skb);
  1088. fr = fcoe_dev_from_skb(skb);
  1089. fr->fr_dev = lport;
  1090. fr->ptype = ptype;
  1091. /*
  1092. * In case the incoming frame's exchange is originated from
  1093. * the initiator, then received frame's exchange id is ANDed
  1094. * with fc_cpu_mask bits to get the same cpu on which exchange
  1095. * was originated, otherwise just use the current cpu.
  1096. */
  1097. if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
  1098. cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
  1099. else
  1100. cpu = smp_processor_id();
  1101. fps = &per_cpu(fcoe_percpu, cpu);
  1102. spin_lock_bh(&fps->fcoe_rx_list.lock);
  1103. if (unlikely(!fps->thread)) {
  1104. /*
  1105. * The targeted CPU is not ready, let's target
  1106. * the first CPU now. For non-SMP systems this
  1107. * will check the same CPU twice.
  1108. */
  1109. FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
  1110. "ready for incoming skb- using first online "
  1111. "CPU.\n");
  1112. spin_unlock_bh(&fps->fcoe_rx_list.lock);
  1113. cpu = cpumask_first(cpu_online_mask);
  1114. fps = &per_cpu(fcoe_percpu, cpu);
  1115. spin_lock_bh(&fps->fcoe_rx_list.lock);
  1116. if (!fps->thread) {
  1117. spin_unlock_bh(&fps->fcoe_rx_list.lock);
  1118. goto err;
  1119. }
  1120. }
  1121. /*
  1122. * We now have a valid CPU that we're targeting for
  1123. * this skb. We also have this receive thread locked,
  1124. * so we're free to queue skbs into it's queue.
  1125. */
  1126. /* If this is a SCSI-FCP frame, and this is already executing on the
  1127. * correct CPU, and the queue for this CPU is empty, then go ahead
  1128. * and process the frame directly in the softirq context.
  1129. * This lets us process completions without context switching from the
  1130. * NET_RX softirq, to our receive processing thread, and then back to
  1131. * BLOCK softirq context.
  1132. */
  1133. if (fh->fh_type == FC_TYPE_FCP &&
  1134. cpu == smp_processor_id() &&
  1135. skb_queue_empty(&fps->fcoe_rx_list)) {
  1136. spin_unlock_bh(&fps->fcoe_rx_list.lock);
  1137. fcoe_recv_frame(skb);
  1138. } else {
  1139. __skb_queue_tail(&fps->fcoe_rx_list, skb);
  1140. if (fps->fcoe_rx_list.qlen == 1)
  1141. wake_up_process(fps->thread);
  1142. spin_unlock_bh(&fps->fcoe_rx_list.lock);
  1143. }
  1144. return 0;
  1145. err:
  1146. per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
  1147. put_cpu();
  1148. err2:
  1149. kfree_skb(skb);
  1150. return -1;
  1151. }
  1152. /**
  1153. * fcoe_start_io() - Start FCoE I/O
  1154. * @skb: The packet to be transmitted
  1155. *
  1156. * This routine is called from the net device to start transmitting
  1157. * FCoE packets.
  1158. *
  1159. * Returns: 0 for success
  1160. */
  1161. static inline int fcoe_start_io(struct sk_buff *skb)
  1162. {
  1163. struct sk_buff *nskb;
  1164. int rc;
  1165. nskb = skb_clone(skb, GFP_ATOMIC);
  1166. rc = dev_queue_xmit(nskb);
  1167. if (rc != 0)
  1168. return rc;
  1169. kfree_skb(skb);
  1170. return 0;
  1171. }
  1172. /**
  1173. * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
  1174. * @skb: The packet to be transmitted
  1175. * @tlen: The total length of the trailer
  1176. *
  1177. * This routine allocates a page for frame trailers. The page is re-used if
  1178. * there is enough room left on it for the current trailer. If there isn't
  1179. * enough buffer left a new page is allocated for the trailer. Reference to
  1180. * the page from this function as well as the skbs using the page fragments
  1181. * ensure that the page is freed at the appropriate time.
  1182. *
  1183. * Returns: 0 for success
  1184. */
  1185. static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
  1186. {
  1187. struct fcoe_percpu_s *fps;
  1188. struct page *page;
  1189. fps = &get_cpu_var(fcoe_percpu);
  1190. page = fps->crc_eof_page;
  1191. if (!page) {
  1192. page = alloc_page(GFP_ATOMIC);
  1193. if (!page) {
  1194. put_cpu_var(fcoe_percpu);
  1195. return -ENOMEM;
  1196. }
  1197. fps->crc_eof_page = page;
  1198. fps->crc_eof_offset = 0;
  1199. }
  1200. get_page(page);
  1201. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
  1202. fps->crc_eof_offset, tlen);
  1203. skb->len += tlen;
  1204. skb->data_len += tlen;
  1205. skb->truesize += tlen;
  1206. fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
  1207. if (fps->crc_eof_offset >= PAGE_SIZE) {
  1208. fps->crc_eof_page = NULL;
  1209. fps->crc_eof_offset = 0;
  1210. put_page(page);
  1211. }
  1212. put_cpu_var(fcoe_percpu);
  1213. return 0;
  1214. }
  1215. /**
  1216. * fcoe_fc_crc() - Calculates the CRC for a given frame
  1217. * @fp: The frame to be checksumed
  1218. *
  1219. * This uses crc32() routine to calculate the CRC for a frame
  1220. *
  1221. * Return: The 32 bit CRC value
  1222. */
  1223. u32 fcoe_fc_crc(struct fc_frame *fp)
  1224. {
  1225. struct sk_buff *skb = fp_skb(fp);
  1226. struct skb_frag_struct *frag;
  1227. unsigned char *data;
  1228. unsigned long off, len, clen;
  1229. u32 crc;
  1230. unsigned i;
  1231. crc = crc32(~0, skb->data, skb_headlen(skb));
  1232. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1233. frag = &skb_shinfo(skb)->frags[i];
  1234. off = frag->page_offset;
  1235. len = frag->size;
  1236. while (len > 0) {
  1237. clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
  1238. data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
  1239. KM_SKB_DATA_SOFTIRQ);
  1240. crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
  1241. kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
  1242. off += clen;
  1243. len -= clen;
  1244. }
  1245. }
  1246. return crc;
  1247. }
  1248. /**
  1249. * fcoe_xmit() - Transmit a FCoE frame
  1250. * @lport: The local port that the frame is to be transmitted for
  1251. * @fp: The frame to be transmitted
  1252. *
  1253. * Return: 0 for success
  1254. */
  1255. int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
  1256. {
  1257. int wlen;
  1258. u32 crc;
  1259. struct ethhdr *eh;
  1260. struct fcoe_crc_eof *cp;
  1261. struct sk_buff *skb;
  1262. struct fcoe_dev_stats *stats;
  1263. struct fc_frame_header *fh;
  1264. unsigned int hlen; /* header length implies the version */
  1265. unsigned int tlen; /* trailer length */
  1266. unsigned int elen; /* eth header, may include vlan */
  1267. struct fcoe_port *port = lport_priv(lport);
  1268. struct fcoe_interface *fcoe = port->fcoe;
  1269. u8 sof, eof;
  1270. struct fcoe_hdr *hp;
  1271. WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
  1272. fh = fc_frame_header_get(fp);
  1273. skb = fp_skb(fp);
  1274. wlen = skb->len / FCOE_WORD_TO_BYTE;
  1275. if (!lport->link_up) {
  1276. kfree_skb(skb);
  1277. return 0;
  1278. }
  1279. if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
  1280. fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
  1281. return 0;
  1282. sof = fr_sof(fp);
  1283. eof = fr_eof(fp);
  1284. elen = sizeof(struct ethhdr);
  1285. hlen = sizeof(struct fcoe_hdr);
  1286. tlen = sizeof(struct fcoe_crc_eof);
  1287. wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
  1288. /* crc offload */
  1289. if (likely(lport->crc_offload)) {
  1290. skb->ip_summed = CHECKSUM_PARTIAL;
  1291. skb->csum_start = skb_headroom(skb);
  1292. skb->csum_offset = skb->len;
  1293. crc = 0;
  1294. } else {
  1295. skb->ip_summed = CHECKSUM_NONE;
  1296. crc = fcoe_fc_crc(fp);
  1297. }
  1298. /* copy port crc and eof to the skb buff */
  1299. if (skb_is_nonlinear(skb)) {
  1300. skb_frag_t *frag;
  1301. if (fcoe_get_paged_crc_eof(skb, tlen)) {
  1302. kfree_skb(skb);
  1303. return -ENOMEM;
  1304. }
  1305. frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
  1306. cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
  1307. + frag->page_offset;
  1308. } else {
  1309. cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
  1310. }
  1311. memset(cp, 0, sizeof(*cp));
  1312. cp->fcoe_eof = eof;
  1313. cp->fcoe_crc32 = cpu_to_le32(~crc);
  1314. if (skb_is_nonlinear(skb)) {
  1315. kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
  1316. cp = NULL;
  1317. }
  1318. /* adjust skb network/transport offsets to match mac/fcoe/port */
  1319. skb_push(skb, elen + hlen);
  1320. skb_reset_mac_header(skb);
  1321. skb_reset_network_header(skb);
  1322. skb->mac_len = elen;
  1323. skb->protocol = htons(ETH_P_FCOE);
  1324. skb->dev = fcoe->netdev;
  1325. /* fill up mac and fcoe headers */
  1326. eh = eth_hdr(skb);
  1327. eh->h_proto = htons(ETH_P_FCOE);
  1328. if (fcoe->ctlr.map_dest)
  1329. fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
  1330. else
  1331. /* insert GW address */
  1332. memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
  1333. if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
  1334. memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
  1335. else
  1336. memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
  1337. hp = (struct fcoe_hdr *)(eh + 1);
  1338. memset(hp, 0, sizeof(*hp));
  1339. if (FC_FCOE_VER)
  1340. FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
  1341. hp->fcoe_sof = sof;
  1342. /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
  1343. if (lport->seq_offload && fr_max_payload(fp)) {
  1344. skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
  1345. skb_shinfo(skb)->gso_size = fr_max_payload(fp);
  1346. } else {
  1347. skb_shinfo(skb)->gso_type = 0;
  1348. skb_shinfo(skb)->gso_size = 0;
  1349. }
  1350. /* update tx stats: regardless if LLD fails */
  1351. stats = per_cpu_ptr(lport->dev_stats, get_cpu());
  1352. stats->TxFrames++;
  1353. stats->TxWords += wlen;
  1354. put_cpu();
  1355. /* send down to lld */
  1356. fr_dev(fp) = lport;
  1357. if (port->fcoe_pending_queue.qlen)
  1358. fcoe_check_wait_queue(lport, skb);
  1359. else if (fcoe_start_io(skb))
  1360. fcoe_check_wait_queue(lport, skb);
  1361. return 0;
  1362. }
  1363. /**
  1364. * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
  1365. * @skb: The completed skb (argument required by destructor)
  1366. */
  1367. static void fcoe_percpu_flush_done(struct sk_buff *skb)
  1368. {
  1369. complete(&fcoe_flush_completion);
  1370. }
  1371. /**
  1372. * fcoe_recv_frame() - process a single received frame
  1373. * @skb: frame to process
  1374. */
  1375. static void fcoe_recv_frame(struct sk_buff *skb)
  1376. {
  1377. u32 fr_len;
  1378. struct fc_lport *lport;
  1379. struct fcoe_rcv_info *fr;
  1380. struct fcoe_dev_stats *stats;
  1381. struct fc_frame_header *fh;
  1382. struct fcoe_crc_eof crc_eof;
  1383. struct fc_frame *fp;
  1384. struct fcoe_port *port;
  1385. struct fcoe_hdr *hp;
  1386. fr = fcoe_dev_from_skb(skb);
  1387. lport = fr->fr_dev;
  1388. if (unlikely(!lport)) {
  1389. if (skb->destructor != fcoe_percpu_flush_done)
  1390. FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
  1391. kfree_skb(skb);
  1392. return;
  1393. }
  1394. FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
  1395. "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
  1396. skb->len, skb->data_len,
  1397. skb->head, skb->data, skb_tail_pointer(skb),
  1398. skb_end_pointer(skb), skb->csum,
  1399. skb->dev ? skb->dev->name : "<NULL>");
  1400. port = lport_priv(lport);
  1401. if (skb_is_nonlinear(skb))
  1402. skb_linearize(skb); /* not ideal */
  1403. /*
  1404. * Frame length checks and setting up the header pointers
  1405. * was done in fcoe_rcv already.
  1406. */
  1407. hp = (struct fcoe_hdr *) skb_network_header(skb);
  1408. fh = (struct fc_frame_header *) skb_transport_header(skb);
  1409. stats = per_cpu_ptr(lport->dev_stats, get_cpu());
  1410. if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
  1411. if (stats->ErrorFrames < 5)
  1412. printk(KERN_WARNING "fcoe: FCoE version "
  1413. "mismatch: The frame has "
  1414. "version %x, but the "
  1415. "initiator supports version "
  1416. "%x\n", FC_FCOE_DECAPS_VER(hp),
  1417. FC_FCOE_VER);
  1418. goto drop;
  1419. }
  1420. skb_pull(skb, sizeof(struct fcoe_hdr));
  1421. fr_len = skb->len - sizeof(struct fcoe_crc_eof);
  1422. stats->RxFrames++;
  1423. stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
  1424. fp = (struct fc_frame *)skb;
  1425. fc_frame_init(fp);
  1426. fr_dev(fp) = lport;
  1427. fr_sof(fp) = hp->fcoe_sof;
  1428. /* Copy out the CRC and EOF trailer for access */
  1429. if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
  1430. goto drop;
  1431. fr_eof(fp) = crc_eof.fcoe_eof;
  1432. fr_crc(fp) = crc_eof.fcoe_crc32;
  1433. if (pskb_trim(skb, fr_len))
  1434. goto drop;
  1435. /*
  1436. * We only check CRC if no offload is available and if it is
  1437. * it's solicited data, in which case, the FCP layer would
  1438. * check it during the copy.
  1439. */
  1440. if (lport->crc_offload &&
  1441. skb->ip_summed == CHECKSUM_UNNECESSARY)
  1442. fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
  1443. else
  1444. fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
  1445. fh = fc_frame_header_get(fp);
  1446. if ((fh->fh_r_ctl != FC_RCTL_DD_SOL_DATA ||
  1447. fh->fh_type != FC_TYPE_FCP) &&
  1448. (fr_flags(fp) & FCPHF_CRC_UNCHECKED)) {
  1449. if (le32_to_cpu(fr_crc(fp)) !=
  1450. ~crc32(~0, skb->data, fr_len)) {
  1451. if (stats->InvalidCRCCount < 5)
  1452. printk(KERN_WARNING "fcoe: dropping "
  1453. "frame with CRC error\n");
  1454. stats->InvalidCRCCount++;
  1455. goto drop;
  1456. }
  1457. fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
  1458. }
  1459. put_cpu();
  1460. fc_exch_recv(lport, fp);
  1461. return;
  1462. drop:
  1463. stats->ErrorFrames++;
  1464. put_cpu();
  1465. kfree_skb(skb);
  1466. }
  1467. /**
  1468. * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
  1469. * @arg: The per-CPU context
  1470. *
  1471. * Return: 0 for success
  1472. */
  1473. int fcoe_percpu_receive_thread(void *arg)
  1474. {
  1475. struct fcoe_percpu_s *p = arg;
  1476. struct sk_buff *skb;
  1477. set_user_nice(current, -20);
  1478. while (!kthread_should_stop()) {
  1479. spin_lock_bh(&p->fcoe_rx_list.lock);
  1480. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
  1481. set_current_state(TASK_INTERRUPTIBLE);
  1482. spin_unlock_bh(&p->fcoe_rx_list.lock);
  1483. schedule();
  1484. set_current_state(TASK_RUNNING);
  1485. if (kthread_should_stop())
  1486. return 0;
  1487. spin_lock_bh(&p->fcoe_rx_list.lock);
  1488. }
  1489. spin_unlock_bh(&p->fcoe_rx_list.lock);
  1490. fcoe_recv_frame(skb);
  1491. }
  1492. return 0;
  1493. }
  1494. /**
  1495. * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
  1496. * @lport: The local port whose backlog is to be cleared
  1497. *
  1498. * This empties the wait_queue, dequeues the head of the wait_queue queue
  1499. * and calls fcoe_start_io() for each packet. If all skb have been
  1500. * transmitted it returns the qlen. If an error occurs it restores
  1501. * wait_queue (to try again later) and returns -1.
  1502. *
  1503. * The wait_queue is used when the skb transmit fails. The failed skb
  1504. * will go in the wait_queue which will be emptied by the timer function or
  1505. * by the next skb transmit.
  1506. */
  1507. static void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
  1508. {
  1509. struct fcoe_port *port = lport_priv(lport);
  1510. int rc;
  1511. spin_lock_bh(&port->fcoe_pending_queue.lock);
  1512. if (skb)
  1513. __skb_queue_tail(&port->fcoe_pending_queue, skb);
  1514. if (port->fcoe_pending_queue_active)
  1515. goto out;
  1516. port->fcoe_pending_queue_active = 1;
  1517. while (port->fcoe_pending_queue.qlen) {
  1518. /* keep qlen > 0 until fcoe_start_io succeeds */
  1519. port->fcoe_pending_queue.qlen++;
  1520. skb = __skb_dequeue(&port->fcoe_pending_queue);
  1521. spin_unlock_bh(&port->fcoe_pending_queue.lock);
  1522. rc = fcoe_start_io(skb);
  1523. spin_lock_bh(&port->fcoe_pending_queue.lock);
  1524. if (rc) {
  1525. __skb_queue_head(&port->fcoe_pending_queue, skb);
  1526. /* undo temporary increment above */
  1527. port->fcoe_pending_queue.qlen--;
  1528. break;
  1529. }
  1530. /* undo temporary increment above */
  1531. port->fcoe_pending_queue.qlen--;
  1532. }
  1533. if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
  1534. lport->qfull = 0;
  1535. if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
  1536. mod_timer(&port->timer, jiffies + 2);
  1537. port->fcoe_pending_queue_active = 0;
  1538. out:
  1539. if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
  1540. lport->qfull = 1;
  1541. spin_unlock_bh(&port->fcoe_pending_queue.lock);
  1542. return;
  1543. }
  1544. /**
  1545. * fcoe_dev_setup() - Setup the link change notification interface
  1546. */
  1547. static void fcoe_dev_setup(void)
  1548. {
  1549. register_netdevice_notifier(&fcoe_notifier);
  1550. }
  1551. /**
  1552. * fcoe_dev_cleanup() - Cleanup the link change notification interface
  1553. */
  1554. static void fcoe_dev_cleanup(void)
  1555. {
  1556. unregister_netdevice_notifier(&fcoe_notifier);
  1557. }
  1558. /**
  1559. * fcoe_device_notification() - Handler for net device events
  1560. * @notifier: The context of the notification
  1561. * @event: The type of event
  1562. * @ptr: The net device that the event was on
  1563. *
  1564. * This function is called by the Ethernet driver in case of link change event.
  1565. *
  1566. * Returns: 0 for success
  1567. */
  1568. static int fcoe_device_notification(struct notifier_block *notifier,
  1569. ulong event, void *ptr)
  1570. {
  1571. struct fc_lport *lport = NULL;
  1572. struct net_device *netdev = ptr;
  1573. struct fcoe_interface *fcoe;
  1574. struct fcoe_port *port;
  1575. struct fcoe_dev_stats *stats;
  1576. u32 link_possible = 1;
  1577. u32 mfs;
  1578. int rc = NOTIFY_OK;
  1579. list_for_each_entry(fcoe, &fcoe_hostlist, list) {
  1580. if (fcoe->netdev == netdev) {
  1581. lport = fcoe->ctlr.lp;
  1582. break;
  1583. }
  1584. }
  1585. if (!lport) {
  1586. rc = NOTIFY_DONE;
  1587. goto out;
  1588. }
  1589. switch (event) {
  1590. case NETDEV_DOWN:
  1591. case NETDEV_GOING_DOWN:
  1592. link_possible = 0;
  1593. break;
  1594. case NETDEV_UP:
  1595. case NETDEV_CHANGE:
  1596. break;
  1597. case NETDEV_CHANGEMTU:
  1598. if (netdev->features & NETIF_F_FCOE_MTU)
  1599. break;
  1600. mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
  1601. sizeof(struct fcoe_crc_eof));
  1602. if (mfs >= FC_MIN_MAX_FRAME)
  1603. fc_set_mfs(lport, mfs);
  1604. break;
  1605. case NETDEV_REGISTER:
  1606. break;
  1607. case NETDEV_UNREGISTER:
  1608. list_del(&fcoe->list);
  1609. port = lport_priv(fcoe->ctlr.lp);
  1610. fcoe_interface_cleanup(fcoe);
  1611. schedule_work(&port->destroy_work);
  1612. goto out;
  1613. break;
  1614. default:
  1615. FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
  1616. "from netdev netlink\n", event);
  1617. }
  1618. fcoe_link_speed_update(lport);
  1619. if (link_possible && !fcoe_link_ok(lport))
  1620. fcoe_ctlr_link_up(&fcoe->ctlr);
  1621. else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
  1622. stats = per_cpu_ptr(lport->dev_stats, get_cpu());
  1623. stats->LinkFailureCount++;
  1624. put_cpu();
  1625. fcoe_clean_pending_queue(lport);
  1626. }
  1627. out:
  1628. return rc;
  1629. }
  1630. /**
  1631. * fcoe_if_to_netdev() - Parse a name buffer to get a net device
  1632. * @buffer: The name of the net device
  1633. *
  1634. * Returns: NULL or a ptr to net_device
  1635. */
  1636. static struct net_device *fcoe_if_to_netdev(const char *buffer)
  1637. {
  1638. char *cp;
  1639. char ifname[IFNAMSIZ + 2];
  1640. if (buffer) {
  1641. strlcpy(ifname, buffer, IFNAMSIZ);
  1642. cp = ifname + strlen(ifname);
  1643. while (--cp >= ifname && *cp == '\n')
  1644. *cp = '\0';
  1645. return dev_get_by_name(&init_net, ifname);
  1646. }
  1647. return NULL;
  1648. }
  1649. /**
  1650. * fcoe_disable() - Disables a FCoE interface
  1651. * @buffer: The name of the Ethernet interface to be disabled
  1652. * @kp: The associated kernel parameter
  1653. *
  1654. * Called from sysfs.
  1655. *
  1656. * Returns: 0 for success
  1657. */
  1658. static int fcoe_disable(const char *buffer, struct kernel_param *kp)
  1659. {
  1660. struct fcoe_interface *fcoe;
  1661. struct net_device *netdev;
  1662. int rc = 0;
  1663. mutex_lock(&fcoe_config_mutex);
  1664. #ifdef CONFIG_FCOE_MODULE
  1665. /*
  1666. * Make sure the module has been initialized, and is not about to be
  1667. * removed. Module paramter sysfs files are writable before the
  1668. * module_init function is called and after module_exit.
  1669. */
  1670. if (THIS_MODULE->state != MODULE_STATE_LIVE) {
  1671. rc = -ENODEV;
  1672. goto out_nodev;
  1673. }
  1674. #endif
  1675. netdev = fcoe_if_to_netdev(buffer);
  1676. if (!netdev) {
  1677. rc = -ENODEV;
  1678. goto out_nodev;
  1679. }
  1680. if (!rtnl_trylock()) {
  1681. dev_put(netdev);
  1682. mutex_unlock(&fcoe_config_mutex);
  1683. return restart_syscall();
  1684. }
  1685. fcoe = fcoe_hostlist_lookup_port(netdev);
  1686. rtnl_unlock();
  1687. if (fcoe) {
  1688. fc_fabric_logoff(fcoe->ctlr.lp);
  1689. fcoe_ctlr_link_down(&fcoe->ctlr);
  1690. } else
  1691. rc = -ENODEV;
  1692. dev_put(netdev);
  1693. out_nodev:
  1694. mutex_unlock(&fcoe_config_mutex);
  1695. return rc;
  1696. }
  1697. /**
  1698. * fcoe_enable() - Enables a FCoE interface
  1699. * @buffer: The name of the Ethernet interface to be enabled
  1700. * @kp: The associated kernel parameter
  1701. *
  1702. * Called from sysfs.
  1703. *
  1704. * Returns: 0 for success
  1705. */
  1706. static int fcoe_enable(const char *buffer, struct kernel_param *kp)
  1707. {
  1708. struct fcoe_interface *fcoe;
  1709. struct net_device *netdev;
  1710. int rc = 0;
  1711. mutex_lock(&fcoe_config_mutex);
  1712. #ifdef CONFIG_FCOE_MODULE
  1713. /*
  1714. * Make sure the module has been initialized, and is not about to be
  1715. * removed. Module paramter sysfs files are writable before the
  1716. * module_init function is called and after module_exit.
  1717. */
  1718. if (THIS_MODULE->state != MODULE_STATE_LIVE) {
  1719. rc = -ENODEV;
  1720. goto out_nodev;
  1721. }
  1722. #endif
  1723. netdev = fcoe_if_to_netdev(buffer);
  1724. if (!netdev) {
  1725. rc = -ENODEV;
  1726. goto out_nodev;
  1727. }
  1728. if (!rtnl_trylock()) {
  1729. dev_put(netdev);
  1730. mutex_unlock(&fcoe_config_mutex);
  1731. return restart_syscall();
  1732. }
  1733. fcoe = fcoe_hostlist_lookup_port(netdev);
  1734. rtnl_unlock();
  1735. if (fcoe) {
  1736. if (!fcoe_link_ok(fcoe->ctlr.lp))
  1737. fcoe_ctlr_link_up(&fcoe->ctlr);
  1738. rc = fc_fabric_login(fcoe->ctlr.lp);
  1739. } else
  1740. rc = -ENODEV;
  1741. dev_put(netdev);
  1742. out_nodev:
  1743. mutex_unlock(&fcoe_config_mutex);
  1744. return rc;
  1745. }
  1746. /**
  1747. * fcoe_destroy() - Destroy a FCoE interface
  1748. * @buffer: The name of the Ethernet interface to be destroyed
  1749. * @kp: The associated kernel parameter
  1750. *
  1751. * Called from sysfs.
  1752. *
  1753. * Returns: 0 for success
  1754. */
  1755. static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
  1756. {
  1757. struct fcoe_interface *fcoe;
  1758. struct net_device *netdev;
  1759. int rc = 0;
  1760. mutex_lock(&fcoe_config_mutex);
  1761. #ifdef CONFIG_FCOE_MODULE
  1762. /*
  1763. * Make sure the module has been initialized, and is not about to be
  1764. * removed. Module paramter sysfs files are writable before the
  1765. * module_init function is called and after module_exit.
  1766. */
  1767. if (THIS_MODULE->state != MODULE_STATE_LIVE) {
  1768. rc = -ENODEV;
  1769. goto out_nodev;
  1770. }
  1771. #endif
  1772. netdev = fcoe_if_to_netdev(buffer);
  1773. if (!netdev) {
  1774. rc = -ENODEV;
  1775. goto out_nodev;
  1776. }
  1777. if (!rtnl_trylock()) {
  1778. dev_put(netdev);
  1779. mutex_unlock(&fcoe_config_mutex);
  1780. return restart_syscall();
  1781. }
  1782. fcoe = fcoe_hostlist_lookup_port(netdev);
  1783. if (!fcoe) {
  1784. rtnl_unlock();
  1785. rc = -ENODEV;
  1786. goto out_putdev;
  1787. }
  1788. list_del(&fcoe->list);
  1789. fcoe_interface_cleanup(fcoe);
  1790. /* RTNL mutex is dropped by fcoe_if_destroy */
  1791. fcoe_if_destroy(fcoe->ctlr.lp);
  1792. out_putdev:
  1793. dev_put(netdev);
  1794. out_nodev:
  1795. mutex_unlock(&fcoe_config_mutex);
  1796. return rc;
  1797. }
  1798. /**
  1799. * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
  1800. * @work: Handle to the FCoE port to be destroyed
  1801. */
  1802. static void fcoe_destroy_work(struct work_struct *work)
  1803. {
  1804. struct fcoe_port *port;
  1805. port = container_of(work, struct fcoe_port, destroy_work);
  1806. mutex_lock(&fcoe_config_mutex);
  1807. rtnl_lock();
  1808. /* RTNL mutex is dropped by fcoe_if_destroy */
  1809. fcoe_if_destroy(port->lport);
  1810. mutex_unlock(&fcoe_config_mutex);
  1811. }
  1812. /**
  1813. * fcoe_create() - Create a fcoe interface
  1814. * @buffer: The name of the Ethernet interface to create on
  1815. * @kp: The associated kernel param
  1816. *
  1817. * Called from sysfs.
  1818. *
  1819. * Returns: 0 for success
  1820. */
  1821. static int fcoe_create(const char *buffer, struct kernel_param *kp)
  1822. {
  1823. int rc;
  1824. struct fcoe_interface *fcoe;
  1825. struct fc_lport *lport;
  1826. struct net_device *netdev;
  1827. mutex_lock(&fcoe_config_mutex);
  1828. if (!rtnl_trylock()) {
  1829. mutex_unlock(&fcoe_config_mutex);
  1830. return restart_syscall();
  1831. }
  1832. #ifdef CONFIG_FCOE_MODULE
  1833. /*
  1834. * Make sure the module has been initialized, and is not about to be
  1835. * removed. Module paramter sysfs files are writable before the
  1836. * module_init function is called and after module_exit.
  1837. */
  1838. if (THIS_MODULE->state != MODULE_STATE_LIVE) {
  1839. rc = -ENODEV;
  1840. goto out_nomod;
  1841. }
  1842. #endif
  1843. if (!try_module_get(THIS_MODULE)) {
  1844. rc = -EINVAL;
  1845. goto out_nomod;
  1846. }
  1847. netdev = fcoe_if_to_netdev(buffer);
  1848. if (!netdev) {
  1849. rc = -ENODEV;
  1850. goto out_nodev;
  1851. }
  1852. /* look for existing lport */
  1853. if (fcoe_hostlist_lookup(netdev)) {
  1854. rc = -EEXIST;
  1855. goto out_putdev;
  1856. }
  1857. fcoe = fcoe_interface_create(netdev);
  1858. if (!fcoe) {
  1859. rc = -ENOMEM;
  1860. goto out_putdev;
  1861. }
  1862. lport = fcoe_if_create(fcoe, &netdev->dev, 0);
  1863. if (IS_ERR(lport)) {
  1864. printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
  1865. netdev->name);
  1866. rc = -EIO;
  1867. fcoe_interface_cleanup(fcoe);
  1868. goto out_free;
  1869. }
  1870. /* Make this the "master" N_Port */
  1871. fcoe->ctlr.lp = lport;
  1872. /* add to lports list */
  1873. fcoe_hostlist_add(lport);
  1874. /* start FIP Discovery and FLOGI */
  1875. lport->boot_time = jiffies;
  1876. fc_fabric_login(lport);
  1877. if (!fcoe_link_ok(lport))
  1878. fcoe_ctlr_link_up(&fcoe->ctlr);
  1879. /*
  1880. * Release from init in fcoe_interface_create(), on success lport
  1881. * should be holding a reference taken in fcoe_if_create().
  1882. */
  1883. fcoe_interface_put(fcoe);
  1884. dev_put(netdev);
  1885. rtnl_unlock();
  1886. mutex_unlock(&fcoe_config_mutex);
  1887. return 0;
  1888. out_free:
  1889. fcoe_interface_put(fcoe);
  1890. out_putdev:
  1891. dev_put(netdev);
  1892. out_nodev:
  1893. module_put(THIS_MODULE);
  1894. out_nomod:
  1895. rtnl_unlock();
  1896. mutex_unlock(&fcoe_config_mutex);
  1897. return rc;
  1898. }
  1899. /**
  1900. * fcoe_link_speed_update() - Update the supported and actual link speeds
  1901. * @lport: The local port to update speeds for
  1902. *
  1903. * Returns: 0 if the ethtool query was successful
  1904. * -1 if the ethtool query failed
  1905. */
  1906. int fcoe_link_speed_update(struct fc_lport *lport)
  1907. {
  1908. struct fcoe_port *port = lport_priv(lport);
  1909. struct net_device *netdev = port->fcoe->netdev;
  1910. struct ethtool_cmd ecmd = { ETHTOOL_GSET };
  1911. if (!dev_ethtool_get_settings(netdev, &ecmd)) {
  1912. lport->link_supported_speeds &=
  1913. ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
  1914. if (ecmd.supported & (SUPPORTED_1000baseT_Half |
  1915. SUPPORTED_1000baseT_Full))
  1916. lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
  1917. if (ecmd.supported & SUPPORTED_10000baseT_Full)
  1918. lport->link_supported_speeds |=
  1919. FC_PORTSPEED_10GBIT;
  1920. if (ecmd.speed == SPEED_1000)
  1921. lport->link_speed = FC_PORTSPEED_1GBIT;
  1922. if (ecmd.speed == SPEED_10000)
  1923. lport->link_speed = FC_PORTSPEED_10GBIT;
  1924. return 0;
  1925. }
  1926. return -1;
  1927. }
  1928. /**
  1929. * fcoe_link_ok() - Check if the link is OK for a local port
  1930. * @lport: The local port to check link on
  1931. *
  1932. * Returns: 0 if link is UP and OK, -1 if not
  1933. *
  1934. */
  1935. int fcoe_link_ok(struct fc_lport *lport)
  1936. {
  1937. struct fcoe_port *port = lport_priv(lport);
  1938. struct net_device *netdev = port->fcoe->netdev;
  1939. if (netif_oper_up(netdev))
  1940. return 0;
  1941. return -1;
  1942. }
  1943. /**
  1944. * fcoe_percpu_clean() - Clear all pending skbs for an local port
  1945. * @lport: The local port whose skbs are to be cleared
  1946. *
  1947. * Must be called with fcoe_create_mutex held to single-thread completion.
  1948. *
  1949. * This flushes the pending skbs by adding a new skb to each queue and
  1950. * waiting until they are all freed. This assures us that not only are
  1951. * there no packets that will be handled by the lport, but also that any
  1952. * threads already handling packet have returned.
  1953. */
  1954. void fcoe_percpu_clean(struct fc_lport *lport)
  1955. {
  1956. struct fcoe_percpu_s *pp;
  1957. struct fcoe_rcv_info *fr;
  1958. struct sk_buff_head *list;
  1959. struct sk_buff *skb, *next;
  1960. struct sk_buff *head;
  1961. unsigned int cpu;
  1962. for_each_possible_cpu(cpu) {
  1963. pp = &per_cpu(fcoe_percpu, cpu);
  1964. spin_lock_bh(&pp->fcoe_rx_list.lock);
  1965. list = &pp->fcoe_rx_list;
  1966. head = list->next;
  1967. for (skb = head; skb != (struct sk_buff *)list;
  1968. skb = next) {
  1969. next = skb->next;
  1970. fr = fcoe_dev_from_skb(skb);
  1971. if (fr->fr_dev == lport) {
  1972. __skb_unlink(skb, list);
  1973. kfree_skb(skb);
  1974. }
  1975. }
  1976. if (!pp->thread || !cpu_online(cpu)) {
  1977. spin_unlock_bh(&pp->fcoe_rx_list.lock);
  1978. continue;
  1979. }
  1980. skb = dev_alloc_skb(0);
  1981. if (!skb) {
  1982. spin_unlock_bh(&pp->fcoe_rx_list.lock);
  1983. continue;
  1984. }
  1985. skb->destructor = fcoe_percpu_flush_done;
  1986. __skb_queue_tail(&pp->fcoe_rx_list, skb);
  1987. if (pp->fcoe_rx_list.qlen == 1)
  1988. wake_up_process(pp->thread);
  1989. spin_unlock_bh(&pp->fcoe_rx_list.lock);
  1990. wait_for_completion(&fcoe_flush_completion);
  1991. }
  1992. }
  1993. /**
  1994. * fcoe_clean_pending_queue() - Dequeue a skb and free it
  1995. * @lport: The local port to dequeue a skb on
  1996. */
  1997. void fcoe_clean_pending_queue(struct fc_lport *lport)
  1998. {
  1999. struct fcoe_port *port = lport_priv(lport);
  2000. struct sk_buff *skb;
  2001. spin_lock_bh(&port->fcoe_pending_queue.lock);
  2002. while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
  2003. spin_unlock_bh(&port->fcoe_pending_queue.lock);
  2004. kfree_skb(skb);
  2005. spin_lock_bh(&port->fcoe_pending_queue.lock);
  2006. }
  2007. spin_unlock_bh(&port->fcoe_pending_queue.lock);
  2008. }
  2009. /**
  2010. * fcoe_reset() - Reset a local port
  2011. * @shost: The SCSI host associated with the local port to be reset
  2012. *
  2013. * Returns: Always 0 (return value required by FC transport template)
  2014. */
  2015. int fcoe_reset(struct Scsi_Host *shost)
  2016. {
  2017. struct fc_lport *lport = shost_priv(shost);
  2018. fc_lport_reset(lport);
  2019. return 0;
  2020. }
  2021. /**
  2022. * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
  2023. * @netdev: The net device used as a key
  2024. *
  2025. * Locking: Must be called with the RNL mutex held.
  2026. *
  2027. * Returns: NULL or the FCoE interface
  2028. */
  2029. static struct fcoe_interface *
  2030. fcoe_hostlist_lookup_port(const struct net_device *netdev)
  2031. {
  2032. struct fcoe_interface *fcoe;
  2033. list_for_each_entry(fcoe, &fcoe_hostlist, list) {
  2034. if (fcoe->netdev == netdev)
  2035. return fcoe;
  2036. }
  2037. return NULL;
  2038. }
  2039. /**
  2040. * fcoe_hostlist_lookup() - Find the local port associated with a
  2041. * given net device
  2042. * @netdev: The netdevice used as a key
  2043. *
  2044. * Locking: Must be called with the RTNL mutex held
  2045. *
  2046. * Returns: NULL or the local port
  2047. */
  2048. static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
  2049. {
  2050. struct fcoe_interface *fcoe;
  2051. fcoe = fcoe_hostlist_lookup_port(netdev);
  2052. return (fcoe) ? fcoe->ctlr.lp : NULL;
  2053. }
  2054. /**
  2055. * fcoe_hostlist_add() - Add the FCoE interface identified by a local
  2056. * port to the hostlist
  2057. * @lport: The local port that identifies the FCoE interface to be added
  2058. *
  2059. * Locking: must be called with the RTNL mutex held
  2060. *
  2061. * Returns: 0 for success
  2062. */
  2063. static int fcoe_hostlist_add(const struct fc_lport *lport)
  2064. {
  2065. struct fcoe_interface *fcoe;
  2066. struct fcoe_port *port;
  2067. fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
  2068. if (!fcoe) {
  2069. port = lport_priv(lport);
  2070. fcoe = port->fcoe;
  2071. list_add_tail(&fcoe->list, &fcoe_hostlist);
  2072. }
  2073. return 0;
  2074. }
  2075. /**
  2076. * fcoe_init() - Initialize fcoe.ko
  2077. *
  2078. * Returns: 0 on success, or a negative value on failure
  2079. */
  2080. static int __init fcoe_init(void)
  2081. {
  2082. struct fcoe_percpu_s *p;
  2083. unsigned int cpu;
  2084. int rc = 0;
  2085. mutex_lock(&fcoe_config_mutex);
  2086. for_each_possible_cpu(cpu) {
  2087. p = &per_cpu(fcoe_percpu, cpu);
  2088. skb_queue_head_init(&p->fcoe_rx_list);
  2089. }
  2090. for_each_online_cpu(cpu)
  2091. fcoe_percpu_thread_create(cpu);
  2092. /* Initialize per CPU interrupt thread */
  2093. rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
  2094. if (rc)
  2095. goto out_free;
  2096. /* Setup link change notification */
  2097. fcoe_dev_setup();
  2098. rc = fcoe_if_init();
  2099. if (rc)
  2100. goto out_free;
  2101. mutex_unlock(&fcoe_config_mutex);
  2102. return 0;
  2103. out_free:
  2104. for_each_online_cpu(cpu) {
  2105. fcoe_percpu_thread_destroy(cpu);
  2106. }
  2107. mutex_unlock(&fcoe_config_mutex);
  2108. return rc;
  2109. }
  2110. module_init(fcoe_init);
  2111. /**
  2112. * fcoe_exit() - Clean up fcoe.ko
  2113. *
  2114. * Returns: 0 on success or a negative value on failure
  2115. */
  2116. static void __exit fcoe_exit(void)
  2117. {
  2118. struct fcoe_interface *fcoe, *tmp;
  2119. struct fcoe_port *port;
  2120. unsigned int cpu;
  2121. mutex_lock(&fcoe_config_mutex);
  2122. fcoe_dev_cleanup();
  2123. /* releases the associated fcoe hosts */
  2124. rtnl_lock();
  2125. list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
  2126. list_del(&fcoe->list);
  2127. port = lport_priv(fcoe->ctlr.lp);
  2128. fcoe_interface_cleanup(fcoe);
  2129. schedule_work(&port->destroy_work);
  2130. }
  2131. rtnl_unlock();
  2132. unregister_hotcpu_notifier(&fcoe_cpu_notifier);
  2133. for_each_online_cpu(cpu)
  2134. fcoe_percpu_thread_destroy(cpu);
  2135. mutex_unlock(&fcoe_config_mutex);
  2136. /* flush any asyncronous interface destroys,
  2137. * this should happen after the netdev notifier is unregistered */
  2138. flush_scheduled_work();
  2139. /* That will flush out all the N_Ports on the hostlist, but now we
  2140. * may have NPIV VN_Ports scheduled for destruction */
  2141. flush_scheduled_work();
  2142. /* detach from scsi transport
  2143. * must happen after all destroys are done, therefor after the flush */
  2144. fcoe_if_exit();
  2145. }
  2146. module_exit(fcoe_exit);
  2147. /**
  2148. * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
  2149. * @seq: active sequence in the FLOGI or FDISC exchange
  2150. * @fp: response frame, or error encoded in a pointer (timeout)
  2151. * @arg: pointer the the fcoe_ctlr structure
  2152. *
  2153. * This handles MAC address managment for FCoE, then passes control on to
  2154. * the libfc FLOGI response handler.
  2155. */
  2156. static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  2157. {
  2158. struct fcoe_ctlr *fip = arg;
  2159. struct fc_exch *exch = fc_seq_exch(seq);
  2160. struct fc_lport *lport = exch->lp;
  2161. u8 *mac;
  2162. if (IS_ERR(fp))
  2163. goto done;
  2164. mac = fr_cb(fp)->granted_mac;
  2165. if (is_zero_ether_addr(mac)) {
  2166. /* pre-FIP */
  2167. if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
  2168. fc_frame_free(fp);
  2169. return;
  2170. }
  2171. }
  2172. fcoe_update_src_mac(lport, mac);
  2173. done:
  2174. fc_lport_flogi_resp(seq, fp, lport);
  2175. }
  2176. /**
  2177. * fcoe_logo_resp() - FCoE specific LOGO response handler
  2178. * @seq: active sequence in the LOGO exchange
  2179. * @fp: response frame, or error encoded in a pointer (timeout)
  2180. * @arg: pointer the the fcoe_ctlr structure
  2181. *
  2182. * This handles MAC address managment for FCoE, then passes control on to
  2183. * the libfc LOGO response handler.
  2184. */
  2185. static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  2186. {
  2187. struct fc_lport *lport = arg;
  2188. static u8 zero_mac[ETH_ALEN] = { 0 };
  2189. if (!IS_ERR(fp))
  2190. fcoe_update_src_mac(lport, zero_mac);
  2191. fc_lport_logo_resp(seq, fp, lport);
  2192. }
  2193. /**
  2194. * fcoe_elsct_send - FCoE specific ELS handler
  2195. *
  2196. * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
  2197. * using FCoE specific response handlers and passing the FIP controller as
  2198. * the argument (the lport is still available from the exchange).
  2199. *
  2200. * Most of the work here is just handed off to the libfc routine.
  2201. */
  2202. static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
  2203. struct fc_frame *fp, unsigned int op,
  2204. void (*resp)(struct fc_seq *,
  2205. struct fc_frame *,
  2206. void *),
  2207. void *arg, u32 timeout)
  2208. {
  2209. struct fcoe_port *port = lport_priv(lport);
  2210. struct fcoe_interface *fcoe = port->fcoe;
  2211. struct fcoe_ctlr *fip = &fcoe->ctlr;
  2212. struct fc_frame_header *fh = fc_frame_header_get(fp);
  2213. switch (op) {
  2214. case ELS_FLOGI:
  2215. case ELS_FDISC:
  2216. return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
  2217. fip, timeout);
  2218. case ELS_LOGO:
  2219. /* only hook onto fabric logouts, not port logouts */
  2220. if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
  2221. break;
  2222. return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
  2223. lport, timeout);
  2224. }
  2225. return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
  2226. }
  2227. /**
  2228. * fcoe_vport_create() - create an fc_host/scsi_host for a vport
  2229. * @vport: fc_vport object to create a new fc_host for
  2230. * @disabled: start the new fc_host in a disabled state by default?
  2231. *
  2232. * Returns: 0 for success
  2233. */
  2234. static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
  2235. {
  2236. struct Scsi_Host *shost = vport_to_shost(vport);
  2237. struct fc_lport *n_port = shost_priv(shost);
  2238. struct fcoe_port *port = lport_priv(n_port);
  2239. struct fcoe_interface *fcoe = port->fcoe;
  2240. struct net_device *netdev = fcoe->netdev;
  2241. struct fc_lport *vn_port;
  2242. mutex_lock(&fcoe_config_mutex);
  2243. vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
  2244. mutex_unlock(&fcoe_config_mutex);
  2245. if (IS_ERR(vn_port)) {
  2246. printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
  2247. netdev->name);
  2248. return -EIO;
  2249. }
  2250. if (disabled) {
  2251. fc_vport_set_state(vport, FC_VPORT_DISABLED);
  2252. } else {
  2253. vn_port->boot_time = jiffies;
  2254. fc_fabric_login(vn_port);
  2255. fc_vport_setlink(vn_port);
  2256. }
  2257. return 0;
  2258. }
  2259. /**
  2260. * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
  2261. * @vport: fc_vport object that is being destroyed
  2262. *
  2263. * Returns: 0 for success
  2264. */
  2265. static int fcoe_vport_destroy(struct fc_vport *vport)
  2266. {
  2267. struct Scsi_Host *shost = vport_to_shost(vport);
  2268. struct fc_lport *n_port = shost_priv(shost);
  2269. struct fc_lport *vn_port = vport->dd_data;
  2270. struct fcoe_port *port = lport_priv(vn_port);
  2271. mutex_lock(&n_port->lp_mutex);
  2272. list_del(&vn_port->list);
  2273. mutex_unlock(&n_port->lp_mutex);
  2274. schedule_work(&port->destroy_work);
  2275. return 0;
  2276. }
  2277. /**
  2278. * fcoe_vport_disable() - change vport state
  2279. * @vport: vport to bring online/offline
  2280. * @disable: should the vport be disabled?
  2281. */
  2282. static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
  2283. {
  2284. struct fc_lport *lport = vport->dd_data;
  2285. if (disable) {
  2286. fc_vport_set_state(vport, FC_VPORT_DISABLED);
  2287. fc_fabric_logoff(lport);
  2288. } else {
  2289. lport->boot_time = jiffies;
  2290. fc_fabric_login(lport);
  2291. fc_vport_setlink(lport);
  2292. }
  2293. return 0;
  2294. }
  2295. /**
  2296. * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
  2297. * @vport: fc_vport with a new symbolic name string
  2298. *
  2299. * After generating a new symbolic name string, a new RSPN_ID request is
  2300. * sent to the name server. There is no response handler, so if it fails
  2301. * for some reason it will not be retried.
  2302. */
  2303. static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
  2304. {
  2305. struct fc_lport *lport = vport->dd_data;
  2306. struct fc_frame *fp;
  2307. size_t len;
  2308. snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
  2309. "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
  2310. fcoe_netdev(lport)->name, vport->symbolic_name);
  2311. if (lport->state != LPORT_ST_READY)
  2312. return;
  2313. len = strnlen(fc_host_symbolic_name(lport->host), 255);
  2314. fp = fc_frame_alloc(lport,
  2315. sizeof(struct fc_ct_hdr) +
  2316. sizeof(struct fc_ns_rspn) + len);
  2317. if (!fp)
  2318. return;
  2319. lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
  2320. NULL, NULL, 3 * lport->r_a_tov);
  2321. }
  2322. /**
  2323. * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
  2324. * @lport: the local port
  2325. * @fc_lesb: the link error status block
  2326. */
  2327. static void fcoe_get_lesb(struct fc_lport *lport,
  2328. struct fc_els_lesb *fc_lesb)
  2329. {
  2330. unsigned int cpu;
  2331. u32 lfc, vlfc, mdac;
  2332. struct fcoe_dev_stats *devst;
  2333. struct fcoe_fc_els_lesb *lesb;
  2334. struct net_device *netdev = fcoe_netdev(lport);
  2335. lfc = 0;
  2336. vlfc = 0;
  2337. mdac = 0;
  2338. lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
  2339. memset(lesb, 0, sizeof(*lesb));
  2340. for_each_possible_cpu(cpu) {
  2341. devst = per_cpu_ptr(lport->dev_stats, cpu);
  2342. lfc += devst->LinkFailureCount;
  2343. vlfc += devst->VLinkFailureCount;
  2344. mdac += devst->MissDiscAdvCount;
  2345. }
  2346. lesb->lesb_link_fail = htonl(lfc);
  2347. lesb->lesb_vlink_fail = htonl(vlfc);
  2348. lesb->lesb_miss_fka = htonl(mdac);
  2349. lesb->lesb_fcs_error = htonl(dev_get_stats(netdev)->rx_crc_errors);
  2350. }
  2351. /**
  2352. * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
  2353. * @lport: the local port
  2354. * @port_id: the port ID
  2355. * @fp: the received frame, if any, that caused the port_id to be set.
  2356. *
  2357. * This routine handles the case where we received a FLOGI and are
  2358. * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
  2359. * so it can set the non-mapped mode and gateway address.
  2360. *
  2361. * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
  2362. */
  2363. static void fcoe_set_port_id(struct fc_lport *lport,
  2364. u32 port_id, struct fc_frame *fp)
  2365. {
  2366. struct fcoe_port *port = lport_priv(lport);
  2367. struct fcoe_interface *fcoe = port->fcoe;
  2368. if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
  2369. fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
  2370. }