fcoe.c 52 KB

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