fcoe.c 69 KB

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