fcoe.c 52 KB

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