fcoe.c 49 KB

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