fcoe.c 57 KB

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