fcoe.c 52 KB

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