fcoe.c 51 KB

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