libfcoe.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. /*
  2. * Copyright(c) 2007 - 2008 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/kernel.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/ethtool.h>
  27. #include <linux/if_ether.h>
  28. #include <linux/if_vlan.h>
  29. #include <linux/kthread.h>
  30. #include <linux/crc32.h>
  31. #include <linux/cpu.h>
  32. #include <linux/fs.h>
  33. #include <linux/sysfs.h>
  34. #include <linux/ctype.h>
  35. #include <scsi/scsi_tcq.h>
  36. #include <scsi/scsicam.h>
  37. #include <scsi/scsi_transport.h>
  38. #include <scsi/scsi_transport_fc.h>
  39. #include <net/rtnetlink.h>
  40. #include <scsi/fc/fc_encaps.h>
  41. #include <scsi/libfc.h>
  42. #include <scsi/fc_frame.h>
  43. #include <scsi/libfcoe.h>
  44. #include <scsi/fc_transport_fcoe.h>
  45. static int debug_fcoe;
  46. #define FCOE_MAX_QUEUE_DEPTH 256
  47. #define FCOE_LOW_QUEUE_DEPTH 32
  48. /* destination address mode */
  49. #define FCOE_GW_ADDR_MODE 0x00
  50. #define FCOE_FCOUI_ADDR_MODE 0x01
  51. #define FCOE_WORD_TO_BYTE 4
  52. MODULE_AUTHOR("Open-FCoE.org");
  53. MODULE_DESCRIPTION("FCoE");
  54. MODULE_LICENSE("GPL");
  55. /* fcoe host list */
  56. LIST_HEAD(fcoe_hostlist);
  57. DEFINE_RWLOCK(fcoe_hostlist_lock);
  58. DEFINE_TIMER(fcoe_timer, NULL, 0, 0);
  59. struct fcoe_percpu_s *fcoe_percpu[NR_CPUS];
  60. /* Function Prototyes */
  61. static int fcoe_check_wait_queue(struct fc_lport *);
  62. static void fcoe_recv_flogi(struct fcoe_softc *, struct fc_frame *, u8 *);
  63. #ifdef CONFIG_HOTPLUG_CPU
  64. static int fcoe_cpu_callback(struct notifier_block *, ulong, void *);
  65. #endif /* CONFIG_HOTPLUG_CPU */
  66. static int fcoe_device_notification(struct notifier_block *, ulong, void *);
  67. static void fcoe_dev_setup(void);
  68. static void fcoe_dev_cleanup(void);
  69. /* notification function from net device */
  70. static struct notifier_block fcoe_notifier = {
  71. .notifier_call = fcoe_device_notification,
  72. };
  73. #ifdef CONFIG_HOTPLUG_CPU
  74. static struct notifier_block fcoe_cpu_notifier = {
  75. .notifier_call = fcoe_cpu_callback,
  76. };
  77. /**
  78. * fcoe_create_percpu_data() - creates the associated cpu data
  79. * @cpu: index for the cpu where fcoe cpu data will be created
  80. *
  81. * create percpu stats block, from cpu add notifier
  82. *
  83. * Returns: none
  84. */
  85. static void fcoe_create_percpu_data(int cpu)
  86. {
  87. struct fc_lport *lp;
  88. struct fcoe_softc *fc;
  89. write_lock_bh(&fcoe_hostlist_lock);
  90. list_for_each_entry(fc, &fcoe_hostlist, list) {
  91. lp = fc->lp;
  92. if (lp->dev_stats[cpu] == NULL)
  93. lp->dev_stats[cpu] =
  94. kzalloc(sizeof(struct fcoe_dev_stats),
  95. GFP_KERNEL);
  96. }
  97. write_unlock_bh(&fcoe_hostlist_lock);
  98. }
  99. /**
  100. * fcoe_destroy_percpu_data() - destroys the associated cpu data
  101. * @cpu: index for the cpu where fcoe cpu data will destroyed
  102. *
  103. * destroy percpu stats block called by cpu add/remove notifier
  104. *
  105. * Retuns: none
  106. */
  107. static void fcoe_destroy_percpu_data(int cpu)
  108. {
  109. struct fc_lport *lp;
  110. struct fcoe_softc *fc;
  111. write_lock_bh(&fcoe_hostlist_lock);
  112. list_for_each_entry(fc, &fcoe_hostlist, list) {
  113. lp = fc->lp;
  114. kfree(lp->dev_stats[cpu]);
  115. lp->dev_stats[cpu] = NULL;
  116. }
  117. write_unlock_bh(&fcoe_hostlist_lock);
  118. }
  119. /**
  120. * fcoe_cpu_callback() - fcoe cpu hotplug event callback
  121. * @nfb: callback data block
  122. * @action: event triggering the callback
  123. * @hcpu: index for the cpu of this event
  124. *
  125. * this creates or destroys per cpu data for fcoe
  126. *
  127. * Returns NOTIFY_OK always.
  128. */
  129. static int fcoe_cpu_callback(struct notifier_block *nfb, unsigned long action,
  130. void *hcpu)
  131. {
  132. unsigned int cpu = (unsigned long)hcpu;
  133. switch (action) {
  134. case CPU_ONLINE:
  135. fcoe_create_percpu_data(cpu);
  136. break;
  137. case CPU_DEAD:
  138. fcoe_destroy_percpu_data(cpu);
  139. break;
  140. default:
  141. break;
  142. }
  143. return NOTIFY_OK;
  144. }
  145. #endif /* CONFIG_HOTPLUG_CPU */
  146. /**
  147. * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
  148. * @skb: the receive skb
  149. * @dev: associated net device
  150. * @ptype: context
  151. * @odldev: last device
  152. *
  153. * this function will receive the packet and build fc frame and pass it up
  154. *
  155. * Returns: 0 for success
  156. */
  157. int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
  158. struct packet_type *ptype, struct net_device *olddev)
  159. {
  160. struct fc_lport *lp;
  161. struct fcoe_rcv_info *fr;
  162. struct fcoe_softc *fc;
  163. struct fcoe_dev_stats *stats;
  164. struct fc_frame_header *fh;
  165. unsigned short oxid;
  166. int cpu_idx;
  167. struct fcoe_percpu_s *fps;
  168. fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
  169. lp = fc->lp;
  170. if (unlikely(lp == NULL)) {
  171. FC_DBG("cannot find hba structure");
  172. goto err2;
  173. }
  174. if (unlikely(debug_fcoe)) {
  175. FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p "
  176. "end:%p sum:%d dev:%s", skb->len, skb->data_len,
  177. skb->head, skb->data, skb_tail_pointer(skb),
  178. skb_end_pointer(skb), skb->csum,
  179. skb->dev ? skb->dev->name : "<NULL>");
  180. }
  181. /* check for FCOE packet type */
  182. if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
  183. FC_DBG("wrong FC type frame");
  184. goto err;
  185. }
  186. /*
  187. * Check for minimum frame length, and make sure required FCoE
  188. * and FC headers are pulled into the linear data area.
  189. */
  190. if (unlikely((skb->len < FCOE_MIN_FRAME) ||
  191. !pskb_may_pull(skb, FCOE_HEADER_LEN)))
  192. goto err;
  193. skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
  194. fh = (struct fc_frame_header *) skb_transport_header(skb);
  195. oxid = ntohs(fh->fh_ox_id);
  196. fr = fcoe_dev_from_skb(skb);
  197. fr->fr_dev = lp;
  198. fr->ptype = ptype;
  199. cpu_idx = 0;
  200. #ifdef CONFIG_SMP
  201. /*
  202. * The incoming frame exchange id(oxid) is ANDed with num of online
  203. * cpu bits to get cpu_idx and then this cpu_idx is used for selecting
  204. * a per cpu kernel thread from fcoe_percpu. In case the cpu is
  205. * offline or no kernel thread for derived cpu_idx then cpu_idx is
  206. * initialize to first online cpu index.
  207. */
  208. cpu_idx = oxid & (num_online_cpus() - 1);
  209. if (!fcoe_percpu[cpu_idx] || !cpu_online(cpu_idx))
  210. cpu_idx = first_cpu(cpu_online_map);
  211. #endif
  212. fps = fcoe_percpu[cpu_idx];
  213. spin_lock_bh(&fps->fcoe_rx_list.lock);
  214. __skb_queue_tail(&fps->fcoe_rx_list, skb);
  215. if (fps->fcoe_rx_list.qlen == 1)
  216. wake_up_process(fps->thread);
  217. spin_unlock_bh(&fps->fcoe_rx_list.lock);
  218. return 0;
  219. err:
  220. #ifdef CONFIG_SMP
  221. stats = lp->dev_stats[smp_processor_id()];
  222. #else
  223. stats = lp->dev_stats[0];
  224. #endif
  225. if (stats)
  226. stats->ErrorFrames++;
  227. err2:
  228. kfree_skb(skb);
  229. return -1;
  230. }
  231. EXPORT_SYMBOL_GPL(fcoe_rcv);
  232. /**
  233. * fcoe_start_io() - pass to netdev to start xmit for fcoe
  234. * @skb: the skb to be xmitted
  235. *
  236. * Returns: 0 for success
  237. */
  238. static inline int fcoe_start_io(struct sk_buff *skb)
  239. {
  240. int rc;
  241. skb_get(skb);
  242. rc = dev_queue_xmit(skb);
  243. if (rc != 0)
  244. return rc;
  245. kfree_skb(skb);
  246. return 0;
  247. }
  248. /**
  249. * fcoe_get_paged_crc_eof() - in case we need alloc a page for crc_eof
  250. * @skb: the skb to be xmitted
  251. * @tlen: total len
  252. *
  253. * Returns: 0 for success
  254. */
  255. static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
  256. {
  257. struct fcoe_percpu_s *fps;
  258. struct page *page;
  259. int cpu_idx;
  260. cpu_idx = get_cpu();
  261. fps = fcoe_percpu[cpu_idx];
  262. page = fps->crc_eof_page;
  263. if (!page) {
  264. page = alloc_page(GFP_ATOMIC);
  265. if (!page) {
  266. put_cpu();
  267. return -ENOMEM;
  268. }
  269. fps->crc_eof_page = page;
  270. WARN_ON(fps->crc_eof_offset != 0);
  271. }
  272. get_page(page);
  273. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
  274. fps->crc_eof_offset, tlen);
  275. skb->len += tlen;
  276. skb->data_len += tlen;
  277. skb->truesize += tlen;
  278. fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
  279. if (fps->crc_eof_offset >= PAGE_SIZE) {
  280. fps->crc_eof_page = NULL;
  281. fps->crc_eof_offset = 0;
  282. put_page(page);
  283. }
  284. put_cpu();
  285. return 0;
  286. }
  287. /**
  288. * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
  289. * @fp: the fc_frame containg data to be checksummed
  290. *
  291. * This uses crc32() to calculate the crc for fc frame
  292. * Return : 32 bit crc
  293. */
  294. u32 fcoe_fc_crc(struct fc_frame *fp)
  295. {
  296. struct sk_buff *skb = fp_skb(fp);
  297. struct skb_frag_struct *frag;
  298. unsigned char *data;
  299. unsigned long off, len, clen;
  300. u32 crc;
  301. unsigned i;
  302. crc = crc32(~0, skb->data, skb_headlen(skb));
  303. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  304. frag = &skb_shinfo(skb)->frags[i];
  305. off = frag->page_offset;
  306. len = frag->size;
  307. while (len > 0) {
  308. clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
  309. data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
  310. KM_SKB_DATA_SOFTIRQ);
  311. crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
  312. kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
  313. off += clen;
  314. len -= clen;
  315. }
  316. }
  317. return crc;
  318. }
  319. EXPORT_SYMBOL_GPL(fcoe_fc_crc);
  320. /**
  321. * fcoe_xmit() - FCoE frame transmit function
  322. * @lp: the associated local port
  323. * @fp: the fc_frame to be transmitted
  324. *
  325. * Return : 0 for success
  326. */
  327. int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
  328. {
  329. int wlen, rc = 0;
  330. u32 crc;
  331. struct ethhdr *eh;
  332. struct fcoe_crc_eof *cp;
  333. struct sk_buff *skb;
  334. struct fcoe_dev_stats *stats;
  335. struct fc_frame_header *fh;
  336. unsigned int hlen; /* header length implies the version */
  337. unsigned int tlen; /* trailer length */
  338. unsigned int elen; /* eth header, may include vlan */
  339. int flogi_in_progress = 0;
  340. struct fcoe_softc *fc;
  341. u8 sof, eof;
  342. struct fcoe_hdr *hp;
  343. WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
  344. fc = lport_priv(lp);
  345. /*
  346. * if it is a flogi then we need to learn gw-addr
  347. * and my own fcid
  348. */
  349. fh = fc_frame_header_get(fp);
  350. if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
  351. if (fc_frame_payload_op(fp) == ELS_FLOGI) {
  352. fc->flogi_oxid = ntohs(fh->fh_ox_id);
  353. fc->address_mode = FCOE_FCOUI_ADDR_MODE;
  354. fc->flogi_progress = 1;
  355. flogi_in_progress = 1;
  356. } else if (fc->flogi_progress && ntoh24(fh->fh_s_id) != 0) {
  357. /*
  358. * Here we must've gotten an SID by accepting an FLOGI
  359. * from a point-to-point connection. Switch to using
  360. * the source mac based on the SID. The destination
  361. * MAC in this case would have been set by receving the
  362. * FLOGI.
  363. */
  364. fc_fcoe_set_mac(fc->data_src_addr, fh->fh_s_id);
  365. fc->flogi_progress = 0;
  366. }
  367. }
  368. skb = fp_skb(fp);
  369. sof = fr_sof(fp);
  370. eof = fr_eof(fp);
  371. elen = (fc->real_dev->priv_flags & IFF_802_1Q_VLAN) ?
  372. sizeof(struct vlan_ethhdr) : sizeof(struct ethhdr);
  373. hlen = sizeof(struct fcoe_hdr);
  374. tlen = sizeof(struct fcoe_crc_eof);
  375. wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
  376. /* crc offload */
  377. if (likely(lp->crc_offload)) {
  378. skb->ip_summed = CHECKSUM_PARTIAL;
  379. skb->csum_start = skb_headroom(skb);
  380. skb->csum_offset = skb->len;
  381. crc = 0;
  382. } else {
  383. skb->ip_summed = CHECKSUM_NONE;
  384. crc = fcoe_fc_crc(fp);
  385. }
  386. /* copy fc crc and eof to the skb buff */
  387. if (skb_is_nonlinear(skb)) {
  388. skb_frag_t *frag;
  389. if (fcoe_get_paged_crc_eof(skb, tlen)) {
  390. kfree_skb(skb);
  391. return -ENOMEM;
  392. }
  393. frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
  394. cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
  395. + frag->page_offset;
  396. } else {
  397. cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
  398. }
  399. memset(cp, 0, sizeof(*cp));
  400. cp->fcoe_eof = eof;
  401. cp->fcoe_crc32 = cpu_to_le32(~crc);
  402. if (skb_is_nonlinear(skb)) {
  403. kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
  404. cp = NULL;
  405. }
  406. /* adjust skb netowrk/transport offsets to match mac/fcoe/fc */
  407. skb_push(skb, elen + hlen);
  408. skb_reset_mac_header(skb);
  409. skb_reset_network_header(skb);
  410. skb->mac_len = elen;
  411. skb->protocol = htons(ETH_P_FCOE);
  412. skb->dev = fc->real_dev;
  413. /* fill up mac and fcoe headers */
  414. eh = eth_hdr(skb);
  415. eh->h_proto = htons(ETH_P_FCOE);
  416. if (fc->address_mode == FCOE_FCOUI_ADDR_MODE)
  417. fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
  418. else
  419. /* insert GW address */
  420. memcpy(eh->h_dest, fc->dest_addr, ETH_ALEN);
  421. if (unlikely(flogi_in_progress))
  422. memcpy(eh->h_source, fc->ctl_src_addr, ETH_ALEN);
  423. else
  424. memcpy(eh->h_source, fc->data_src_addr, ETH_ALEN);
  425. hp = (struct fcoe_hdr *)(eh + 1);
  426. memset(hp, 0, sizeof(*hp));
  427. if (FC_FCOE_VER)
  428. FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
  429. hp->fcoe_sof = sof;
  430. #ifdef NETIF_F_FSO
  431. /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
  432. if (lp->seq_offload && fr_max_payload(fp)) {
  433. skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
  434. skb_shinfo(skb)->gso_size = fr_max_payload(fp);
  435. } else {
  436. skb_shinfo(skb)->gso_type = 0;
  437. skb_shinfo(skb)->gso_size = 0;
  438. }
  439. #endif
  440. /* update tx stats: regardless if LLD fails */
  441. stats = lp->dev_stats[smp_processor_id()];
  442. if (stats) {
  443. stats->TxFrames++;
  444. stats->TxWords += wlen;
  445. }
  446. /* send down to lld */
  447. fr_dev(fp) = lp;
  448. if (fc->fcoe_pending_queue.qlen)
  449. rc = fcoe_check_wait_queue(lp);
  450. if (rc == 0)
  451. rc = fcoe_start_io(skb);
  452. if (rc) {
  453. spin_lock_bh(&fc->fcoe_pending_queue.lock);
  454. __skb_queue_tail(&fc->fcoe_pending_queue, skb);
  455. spin_unlock_bh(&fc->fcoe_pending_queue.lock);
  456. if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
  457. lp->qfull = 1;
  458. }
  459. return 0;
  460. }
  461. EXPORT_SYMBOL_GPL(fcoe_xmit);
  462. /**
  463. * fcoe_percpu_receive_thread() - recv thread per cpu
  464. * @arg: ptr to the fcoe per cpu struct
  465. *
  466. * Return: 0 for success
  467. */
  468. int fcoe_percpu_receive_thread(void *arg)
  469. {
  470. struct fcoe_percpu_s *p = arg;
  471. u32 fr_len;
  472. struct fc_lport *lp;
  473. struct fcoe_rcv_info *fr;
  474. struct fcoe_dev_stats *stats;
  475. struct fc_frame_header *fh;
  476. struct sk_buff *skb;
  477. struct fcoe_crc_eof crc_eof;
  478. struct fc_frame *fp;
  479. u8 *mac = NULL;
  480. struct fcoe_softc *fc;
  481. struct fcoe_hdr *hp;
  482. set_user_nice(current, -20);
  483. while (!kthread_should_stop()) {
  484. spin_lock_bh(&p->fcoe_rx_list.lock);
  485. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
  486. set_current_state(TASK_INTERRUPTIBLE);
  487. spin_unlock_bh(&p->fcoe_rx_list.lock);
  488. schedule();
  489. set_current_state(TASK_RUNNING);
  490. if (kthread_should_stop())
  491. return 0;
  492. spin_lock_bh(&p->fcoe_rx_list.lock);
  493. }
  494. spin_unlock_bh(&p->fcoe_rx_list.lock);
  495. fr = fcoe_dev_from_skb(skb);
  496. lp = fr->fr_dev;
  497. if (unlikely(lp == NULL)) {
  498. FC_DBG("invalid HBA Structure");
  499. kfree_skb(skb);
  500. continue;
  501. }
  502. stats = lp->dev_stats[smp_processor_id()];
  503. if (unlikely(debug_fcoe)) {
  504. FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
  505. "tail:%p end:%p sum:%d dev:%s",
  506. skb->len, skb->data_len,
  507. skb->head, skb->data, skb_tail_pointer(skb),
  508. skb_end_pointer(skb), skb->csum,
  509. skb->dev ? skb->dev->name : "<NULL>");
  510. }
  511. /*
  512. * Save source MAC address before discarding header.
  513. */
  514. fc = lport_priv(lp);
  515. if (unlikely(fc->flogi_progress))
  516. mac = eth_hdr(skb)->h_source;
  517. if (skb_is_nonlinear(skb))
  518. skb_linearize(skb); /* not ideal */
  519. /*
  520. * Frame length checks and setting up the header pointers
  521. * was done in fcoe_rcv already.
  522. */
  523. hp = (struct fcoe_hdr *) skb_network_header(skb);
  524. fh = (struct fc_frame_header *) skb_transport_header(skb);
  525. if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
  526. if (stats) {
  527. if (stats->ErrorFrames < 5)
  528. FC_DBG("unknown FCoE version %x",
  529. FC_FCOE_DECAPS_VER(hp));
  530. stats->ErrorFrames++;
  531. }
  532. kfree_skb(skb);
  533. continue;
  534. }
  535. skb_pull(skb, sizeof(struct fcoe_hdr));
  536. fr_len = skb->len - sizeof(struct fcoe_crc_eof);
  537. if (stats) {
  538. stats->RxFrames++;
  539. stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
  540. }
  541. fp = (struct fc_frame *)skb;
  542. fc_frame_init(fp);
  543. fr_dev(fp) = lp;
  544. fr_sof(fp) = hp->fcoe_sof;
  545. /* Copy out the CRC and EOF trailer for access */
  546. if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
  547. kfree_skb(skb);
  548. continue;
  549. }
  550. fr_eof(fp) = crc_eof.fcoe_eof;
  551. fr_crc(fp) = crc_eof.fcoe_crc32;
  552. if (pskb_trim(skb, fr_len)) {
  553. kfree_skb(skb);
  554. continue;
  555. }
  556. /*
  557. * We only check CRC if no offload is available and if it is
  558. * it's solicited data, in which case, the FCP layer would
  559. * check it during the copy.
  560. */
  561. if (lp->crc_offload)
  562. fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
  563. else
  564. fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
  565. fh = fc_frame_header_get(fp);
  566. if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
  567. fh->fh_type == FC_TYPE_FCP) {
  568. fc_exch_recv(lp, lp->emp, fp);
  569. continue;
  570. }
  571. if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
  572. if (le32_to_cpu(fr_crc(fp)) !=
  573. ~crc32(~0, skb->data, fr_len)) {
  574. if (debug_fcoe || stats->InvalidCRCCount < 5)
  575. printk(KERN_WARNING "fcoe: dropping "
  576. "frame with CRC error\n");
  577. stats->InvalidCRCCount++;
  578. stats->ErrorFrames++;
  579. fc_frame_free(fp);
  580. continue;
  581. }
  582. fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
  583. }
  584. /* non flogi and non data exchanges are handled here */
  585. if (unlikely(fc->flogi_progress))
  586. fcoe_recv_flogi(fc, fp, mac);
  587. fc_exch_recv(lp, lp->emp, fp);
  588. }
  589. return 0;
  590. }
  591. /**
  592. * fcoe_recv_flogi() - flogi receive function
  593. * @fc: associated fcoe_softc
  594. * @fp: the recieved frame
  595. * @sa: the source address of this flogi
  596. *
  597. * This is responsible to parse the flogi response and sets the corresponding
  598. * mac address for the initiator, eitehr OUI based or GW based.
  599. *
  600. * Returns: none
  601. */
  602. static void fcoe_recv_flogi(struct fcoe_softc *fc, struct fc_frame *fp, u8 *sa)
  603. {
  604. struct fc_frame_header *fh;
  605. u8 op;
  606. fh = fc_frame_header_get(fp);
  607. if (fh->fh_type != FC_TYPE_ELS)
  608. return;
  609. op = fc_frame_payload_op(fp);
  610. if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
  611. fc->flogi_oxid == ntohs(fh->fh_ox_id)) {
  612. /*
  613. * FLOGI accepted.
  614. * If the src mac addr is FC_OUI-based, then we mark the
  615. * address_mode flag to use FC_OUI-based Ethernet DA.
  616. * Otherwise we use the FCoE gateway addr
  617. */
  618. if (!compare_ether_addr(sa, (u8[6]) FC_FCOE_FLOGI_MAC)) {
  619. fc->address_mode = FCOE_FCOUI_ADDR_MODE;
  620. } else {
  621. memcpy(fc->dest_addr, sa, ETH_ALEN);
  622. fc->address_mode = FCOE_GW_ADDR_MODE;
  623. }
  624. /*
  625. * Remove any previously-set unicast MAC filter.
  626. * Add secondary FCoE MAC address filter for our OUI.
  627. */
  628. rtnl_lock();
  629. if (compare_ether_addr(fc->data_src_addr, (u8[6]) { 0 }))
  630. dev_unicast_delete(fc->real_dev, fc->data_src_addr,
  631. ETH_ALEN);
  632. fc_fcoe_set_mac(fc->data_src_addr, fh->fh_d_id);
  633. dev_unicast_add(fc->real_dev, fc->data_src_addr, ETH_ALEN);
  634. rtnl_unlock();
  635. fc->flogi_progress = 0;
  636. } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
  637. /*
  638. * Save source MAC for point-to-point responses.
  639. */
  640. memcpy(fc->dest_addr, sa, ETH_ALEN);
  641. fc->address_mode = FCOE_GW_ADDR_MODE;
  642. }
  643. }
  644. /**
  645. * fcoe_watchdog() - fcoe timer callback
  646. * @vp:
  647. *
  648. * This checks the pending queue length for fcoe and set lport qfull
  649. * if the FCOE_MAX_QUEUE_DEPTH is reached. This is done for all fc_lport on the
  650. * fcoe_hostlist.
  651. *
  652. * Returns: 0 for success
  653. */
  654. void fcoe_watchdog(ulong vp)
  655. {
  656. struct fcoe_softc *fc;
  657. read_lock(&fcoe_hostlist_lock);
  658. list_for_each_entry(fc, &fcoe_hostlist, list) {
  659. if (fc->lp)
  660. fcoe_check_wait_queue(fc->lp);
  661. }
  662. read_unlock(&fcoe_hostlist_lock);
  663. fcoe_timer.expires = jiffies + (1 * HZ);
  664. add_timer(&fcoe_timer);
  665. }
  666. /**
  667. * fcoe_check_wait_queue() - put the skb into fcoe pending xmit queue
  668. * @lp: the fc_port for this skb
  669. * @skb: the associated skb to be xmitted
  670. *
  671. * This empties the wait_queue, dequeue the head of the wait_queue queue
  672. * and calls fcoe_start_io() for each packet, if all skb have been
  673. * transmitted, return qlen or -1 if a error occurs, then restore
  674. * wait_queue and try again later.
  675. *
  676. * The wait_queue is used when the skb transmit fails. skb will go
  677. * in the wait_queue which will be emptied by the time function OR
  678. * by the next skb transmit.
  679. *
  680. * Returns: 0 for success
  681. */
  682. static int fcoe_check_wait_queue(struct fc_lport *lp)
  683. {
  684. struct fcoe_softc *fc = lport_priv(lp);
  685. struct sk_buff *skb;
  686. int rc = -1;
  687. spin_lock_bh(&fc->fcoe_pending_queue.lock);
  688. if (fc->fcoe_pending_queue_active)
  689. goto out;
  690. fc->fcoe_pending_queue_active = 1;
  691. while (fc->fcoe_pending_queue.qlen) {
  692. /* keep qlen > 0 until fcoe_start_io succeeds */
  693. fc->fcoe_pending_queue.qlen++;
  694. skb = __skb_dequeue(&fc->fcoe_pending_queue);
  695. spin_unlock_bh(&fc->fcoe_pending_queue.lock);
  696. rc = fcoe_start_io(skb);
  697. spin_lock_bh(&fc->fcoe_pending_queue.lock);
  698. if (rc) {
  699. __skb_queue_head(&fc->fcoe_pending_queue, skb);
  700. /* undo temporary increment above */
  701. fc->fcoe_pending_queue.qlen--;
  702. break;
  703. }
  704. /* undo temporary increment above */
  705. fc->fcoe_pending_queue.qlen--;
  706. }
  707. if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
  708. lp->qfull = 0;
  709. fc->fcoe_pending_queue_active = 0;
  710. rc = fc->fcoe_pending_queue.qlen;
  711. out:
  712. spin_unlock_bh(&fc->fcoe_pending_queue.lock);
  713. return rc;
  714. }
  715. /**
  716. * fcoe_dev_setup() - setup link change notification interface
  717. */
  718. static void fcoe_dev_setup()
  719. {
  720. /*
  721. * here setup a interface specific wd time to
  722. * monitor the link state
  723. */
  724. register_netdevice_notifier(&fcoe_notifier);
  725. }
  726. /**
  727. * fcoe_dev_setup() - cleanup link change notification interface
  728. */
  729. static void fcoe_dev_cleanup(void)
  730. {
  731. unregister_netdevice_notifier(&fcoe_notifier);
  732. }
  733. /**
  734. * fcoe_device_notification() - netdev event notification callback
  735. * @notifier: context of the notification
  736. * @event: type of event
  737. * @ptr: fixed array for output parsed ifname
  738. *
  739. * This function is called by the ethernet driver in case of link change event
  740. *
  741. * Returns: 0 for success
  742. */
  743. static int fcoe_device_notification(struct notifier_block *notifier,
  744. ulong event, void *ptr)
  745. {
  746. struct fc_lport *lp = NULL;
  747. struct net_device *real_dev = ptr;
  748. struct fcoe_softc *fc;
  749. struct fcoe_dev_stats *stats;
  750. u32 new_link_up;
  751. u32 mfs;
  752. int rc = NOTIFY_OK;
  753. read_lock(&fcoe_hostlist_lock);
  754. list_for_each_entry(fc, &fcoe_hostlist, list) {
  755. if (fc->real_dev == real_dev) {
  756. lp = fc->lp;
  757. break;
  758. }
  759. }
  760. read_unlock(&fcoe_hostlist_lock);
  761. if (lp == NULL) {
  762. rc = NOTIFY_DONE;
  763. goto out;
  764. }
  765. new_link_up = lp->link_up;
  766. switch (event) {
  767. case NETDEV_DOWN:
  768. case NETDEV_GOING_DOWN:
  769. new_link_up = 0;
  770. break;
  771. case NETDEV_UP:
  772. case NETDEV_CHANGE:
  773. new_link_up = !fcoe_link_ok(lp);
  774. break;
  775. case NETDEV_CHANGEMTU:
  776. mfs = fc->real_dev->mtu -
  777. (sizeof(struct fcoe_hdr) +
  778. sizeof(struct fcoe_crc_eof));
  779. if (mfs >= FC_MIN_MAX_FRAME)
  780. fc_set_mfs(lp, mfs);
  781. new_link_up = !fcoe_link_ok(lp);
  782. break;
  783. case NETDEV_REGISTER:
  784. break;
  785. default:
  786. FC_DBG("unknown event %ld call", event);
  787. }
  788. if (lp->link_up != new_link_up) {
  789. if (new_link_up)
  790. fc_linkup(lp);
  791. else {
  792. stats = lp->dev_stats[smp_processor_id()];
  793. if (stats)
  794. stats->LinkFailureCount++;
  795. fc_linkdown(lp);
  796. fcoe_clean_pending_queue(lp);
  797. }
  798. }
  799. out:
  800. return rc;
  801. }
  802. /**
  803. * fcoe_if_to_netdev() - parse a name buffer to get netdev
  804. * @ifname: fixed array for output parsed ifname
  805. * @buffer: incoming buffer to be copied
  806. *
  807. * Returns: NULL or ptr to netdeive
  808. */
  809. static struct net_device *fcoe_if_to_netdev(const char *buffer)
  810. {
  811. char *cp;
  812. char ifname[IFNAMSIZ + 2];
  813. if (buffer) {
  814. strlcpy(ifname, buffer, IFNAMSIZ);
  815. cp = ifname + strlen(ifname);
  816. while (--cp >= ifname && *cp == '\n')
  817. *cp = '\0';
  818. return dev_get_by_name(&init_net, ifname);
  819. }
  820. return NULL;
  821. }
  822. /**
  823. * fcoe_netdev_to_module_owner() - finds out the nic drive moddule of the netdev
  824. * @netdev: the target netdev
  825. *
  826. * Returns: ptr to the struct module, NULL for failure
  827. */
  828. static struct module *
  829. fcoe_netdev_to_module_owner(const struct net_device *netdev)
  830. {
  831. struct device *dev;
  832. if (!netdev)
  833. return NULL;
  834. dev = netdev->dev.parent;
  835. if (!dev)
  836. return NULL;
  837. if (!dev->driver)
  838. return NULL;
  839. return dev->driver->owner;
  840. }
  841. /**
  842. * fcoe_ethdrv_get() - Hold the Ethernet driver
  843. * @netdev: the target netdev
  844. *
  845. * Holds the Ethernet driver module by try_module_get() for
  846. * the corresponding netdev.
  847. *
  848. * Returns: 0 for succsss
  849. */
  850. static int fcoe_ethdrv_get(const struct net_device *netdev)
  851. {
  852. struct module *owner;
  853. owner = fcoe_netdev_to_module_owner(netdev);
  854. if (owner) {
  855. printk(KERN_DEBUG "fcoe:hold driver module %s for %s\n",
  856. module_name(owner), netdev->name);
  857. return try_module_get(owner);
  858. }
  859. return -ENODEV;
  860. }
  861. /**
  862. * fcoe_ethdrv_put() - Release the Ethernet driver
  863. * @netdev: the target netdev
  864. *
  865. * Releases the Ethernet driver module by module_put for
  866. * the corresponding netdev.
  867. *
  868. * Returns: 0 for succsss
  869. */
  870. static int fcoe_ethdrv_put(const struct net_device *netdev)
  871. {
  872. struct module *owner;
  873. owner = fcoe_netdev_to_module_owner(netdev);
  874. if (owner) {
  875. printk(KERN_DEBUG "fcoe:release driver module %s for %s\n",
  876. module_name(owner), netdev->name);
  877. module_put(owner);
  878. return 0;
  879. }
  880. return -ENODEV;
  881. }
  882. /**
  883. * fcoe_destroy() - handles the destroy from sysfs
  884. * @buffer: expcted to be a eth if name
  885. * @kp: associated kernel param
  886. *
  887. * Returns: 0 for success
  888. */
  889. static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
  890. {
  891. int rc;
  892. struct net_device *netdev;
  893. netdev = fcoe_if_to_netdev(buffer);
  894. if (!netdev) {
  895. rc = -ENODEV;
  896. goto out_nodev;
  897. }
  898. /* look for existing lport */
  899. if (!fcoe_hostlist_lookup(netdev)) {
  900. rc = -ENODEV;
  901. goto out_putdev;
  902. }
  903. /* pass to transport */
  904. rc = fcoe_transport_release(netdev);
  905. if (rc) {
  906. printk(KERN_ERR "fcoe: fcoe_transport_release(%s) failed\n",
  907. netdev->name);
  908. rc = -EIO;
  909. goto out_putdev;
  910. }
  911. fcoe_ethdrv_put(netdev);
  912. rc = 0;
  913. out_putdev:
  914. dev_put(netdev);
  915. out_nodev:
  916. return rc;
  917. }
  918. /**
  919. * fcoe_create() - Handles the create call from sysfs
  920. * @buffer: expcted to be a eth if name
  921. * @kp: associated kernel param
  922. *
  923. * Returns: 0 for success
  924. */
  925. static int fcoe_create(const char *buffer, struct kernel_param *kp)
  926. {
  927. int rc;
  928. struct net_device *netdev;
  929. netdev = fcoe_if_to_netdev(buffer);
  930. if (!netdev) {
  931. rc = -ENODEV;
  932. goto out_nodev;
  933. }
  934. /* look for existing lport */
  935. if (fcoe_hostlist_lookup(netdev)) {
  936. rc = -EEXIST;
  937. goto out_putdev;
  938. }
  939. fcoe_ethdrv_get(netdev);
  940. /* pass to transport */
  941. rc = fcoe_transport_attach(netdev);
  942. if (rc) {
  943. printk(KERN_ERR "fcoe: fcoe_transport_attach(%s) failed\n",
  944. netdev->name);
  945. fcoe_ethdrv_put(netdev);
  946. rc = -EIO;
  947. goto out_putdev;
  948. }
  949. rc = 0;
  950. out_putdev:
  951. dev_put(netdev);
  952. out_nodev:
  953. return rc;
  954. }
  955. module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
  956. __MODULE_PARM_TYPE(create, "string");
  957. MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
  958. module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
  959. __MODULE_PARM_TYPE(destroy, "string");
  960. MODULE_PARM_DESC(destroy, "Destroy fcoe port");
  961. /**
  962. * fcoe_link_ok() - Check if link is ok for the fc_lport
  963. * @lp: ptr to the fc_lport
  964. *
  965. * Any permanently-disqualifying conditions have been previously checked.
  966. * This also updates the speed setting, which may change with link for 100/1000.
  967. *
  968. * This function should probably be checking for PAUSE support at some point
  969. * in the future. Currently Per-priority-pause is not determinable using
  970. * ethtool, so we shouldn't be restrictive until that problem is resolved.
  971. *
  972. * Returns: 0 if link is OK for use by FCoE.
  973. *
  974. */
  975. int fcoe_link_ok(struct fc_lport *lp)
  976. {
  977. struct fcoe_softc *fc = lport_priv(lp);
  978. struct net_device *dev = fc->real_dev;
  979. struct ethtool_cmd ecmd = { ETHTOOL_GSET };
  980. int rc = 0;
  981. if ((dev->flags & IFF_UP) && netif_carrier_ok(dev)) {
  982. dev = fc->phys_dev;
  983. if (dev->ethtool_ops->get_settings) {
  984. dev->ethtool_ops->get_settings(dev, &ecmd);
  985. lp->link_supported_speeds &=
  986. ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
  987. if (ecmd.supported & (SUPPORTED_1000baseT_Half |
  988. SUPPORTED_1000baseT_Full))
  989. lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
  990. if (ecmd.supported & SUPPORTED_10000baseT_Full)
  991. lp->link_supported_speeds |=
  992. FC_PORTSPEED_10GBIT;
  993. if (ecmd.speed == SPEED_1000)
  994. lp->link_speed = FC_PORTSPEED_1GBIT;
  995. if (ecmd.speed == SPEED_10000)
  996. lp->link_speed = FC_PORTSPEED_10GBIT;
  997. }
  998. } else
  999. rc = -1;
  1000. return rc;
  1001. }
  1002. EXPORT_SYMBOL_GPL(fcoe_link_ok);
  1003. /**
  1004. * fcoe_percpu_clean() - Clear the pending skbs for an lport
  1005. * @lp: the fc_lport
  1006. */
  1007. void fcoe_percpu_clean(struct fc_lport *lp)
  1008. {
  1009. int idx;
  1010. struct fcoe_percpu_s *pp;
  1011. struct fcoe_rcv_info *fr;
  1012. struct sk_buff_head *list;
  1013. struct sk_buff *skb, *next;
  1014. struct sk_buff *head;
  1015. for (idx = 0; idx < NR_CPUS; idx++) {
  1016. if (fcoe_percpu[idx]) {
  1017. pp = fcoe_percpu[idx];
  1018. spin_lock_bh(&pp->fcoe_rx_list.lock);
  1019. list = &pp->fcoe_rx_list;
  1020. head = list->next;
  1021. for (skb = head; skb != (struct sk_buff *)list;
  1022. skb = next) {
  1023. next = skb->next;
  1024. fr = fcoe_dev_from_skb(skb);
  1025. if (fr->fr_dev == lp) {
  1026. __skb_unlink(skb, list);
  1027. kfree_skb(skb);
  1028. }
  1029. }
  1030. spin_unlock_bh(&pp->fcoe_rx_list.lock);
  1031. }
  1032. }
  1033. }
  1034. EXPORT_SYMBOL_GPL(fcoe_percpu_clean);
  1035. /**
  1036. * fcoe_clean_pending_queue() - Dequeue a skb and free it
  1037. * @lp: the corresponding fc_lport
  1038. *
  1039. * Returns: none
  1040. */
  1041. void fcoe_clean_pending_queue(struct fc_lport *lp)
  1042. {
  1043. struct fcoe_softc *fc = lport_priv(lp);
  1044. struct sk_buff *skb;
  1045. spin_lock_bh(&fc->fcoe_pending_queue.lock);
  1046. while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
  1047. spin_unlock_bh(&fc->fcoe_pending_queue.lock);
  1048. kfree_skb(skb);
  1049. spin_lock_bh(&fc->fcoe_pending_queue.lock);
  1050. }
  1051. spin_unlock_bh(&fc->fcoe_pending_queue.lock);
  1052. }
  1053. EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
  1054. /**
  1055. * libfc_host_alloc() - Allocate a Scsi_Host with room for the fc_lport
  1056. * @sht: ptr to the scsi host templ
  1057. * @priv_size: size of private data after fc_lport
  1058. *
  1059. * Returns: ptr to Scsi_Host
  1060. * TODO: to libfc?
  1061. */
  1062. static inline struct Scsi_Host *
  1063. libfc_host_alloc(struct scsi_host_template *sht, int priv_size)
  1064. {
  1065. return scsi_host_alloc(sht, sizeof(struct fc_lport) + priv_size);
  1066. }
  1067. /**
  1068. * fcoe_host_alloc() - Allocate a Scsi_Host with room for the fcoe_softc
  1069. * @sht: ptr to the scsi host templ
  1070. * @priv_size: size of private data after fc_lport
  1071. *
  1072. * Returns: ptr to Scsi_Host
  1073. */
  1074. struct Scsi_Host *fcoe_host_alloc(struct scsi_host_template *sht, int priv_size)
  1075. {
  1076. return libfc_host_alloc(sht, sizeof(struct fcoe_softc) + priv_size);
  1077. }
  1078. EXPORT_SYMBOL_GPL(fcoe_host_alloc);
  1079. /**
  1080. * fcoe_reset() - Resets the fcoe
  1081. * @shost: shost the reset is from
  1082. *
  1083. * Returns: always 0
  1084. */
  1085. int fcoe_reset(struct Scsi_Host *shost)
  1086. {
  1087. struct fc_lport *lport = shost_priv(shost);
  1088. fc_lport_reset(lport);
  1089. return 0;
  1090. }
  1091. EXPORT_SYMBOL_GPL(fcoe_reset);
  1092. /**
  1093. * fcoe_wwn_from_mac() - Converts 48-bit IEEE MAC address to 64-bit FC WWN.
  1094. * @mac: mac address
  1095. * @scheme: check port
  1096. * @port: port indicator for converting
  1097. *
  1098. * Returns: u64 fc world wide name
  1099. */
  1100. u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
  1101. unsigned int scheme, unsigned int port)
  1102. {
  1103. u64 wwn;
  1104. u64 host_mac;
  1105. /* The MAC is in NO, so flip only the low 48 bits */
  1106. host_mac = ((u64) mac[0] << 40) |
  1107. ((u64) mac[1] << 32) |
  1108. ((u64) mac[2] << 24) |
  1109. ((u64) mac[3] << 16) |
  1110. ((u64) mac[4] << 8) |
  1111. (u64) mac[5];
  1112. WARN_ON(host_mac >= (1ULL << 48));
  1113. wwn = host_mac | ((u64) scheme << 60);
  1114. switch (scheme) {
  1115. case 1:
  1116. WARN_ON(port != 0);
  1117. break;
  1118. case 2:
  1119. WARN_ON(port >= 0xfff);
  1120. wwn |= (u64) port << 48;
  1121. break;
  1122. default:
  1123. WARN_ON(1);
  1124. break;
  1125. }
  1126. return wwn;
  1127. }
  1128. EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
  1129. /**
  1130. * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
  1131. * @device: this is currently ptr to net_device
  1132. *
  1133. * Returns: NULL or the located fcoe_softc
  1134. */
  1135. static struct fcoe_softc *
  1136. fcoe_hostlist_lookup_softc(const struct net_device *dev)
  1137. {
  1138. struct fcoe_softc *fc;
  1139. read_lock(&fcoe_hostlist_lock);
  1140. list_for_each_entry(fc, &fcoe_hostlist, list) {
  1141. if (fc->real_dev == dev) {
  1142. read_unlock(&fcoe_hostlist_lock);
  1143. return fc;
  1144. }
  1145. }
  1146. read_unlock(&fcoe_hostlist_lock);
  1147. return NULL;
  1148. }
  1149. /**
  1150. * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
  1151. * @netdev: ptr to net_device
  1152. *
  1153. * Returns: 0 for success
  1154. */
  1155. struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
  1156. {
  1157. struct fcoe_softc *fc;
  1158. fc = fcoe_hostlist_lookup_softc(netdev);
  1159. return (fc) ? fc->lp : NULL;
  1160. }
  1161. EXPORT_SYMBOL_GPL(fcoe_hostlist_lookup);
  1162. /**
  1163. * fcoe_hostlist_add() - Add a lport to lports list
  1164. * @lp: ptr to the fc_lport to badded
  1165. *
  1166. * Returns: 0 for success
  1167. */
  1168. int fcoe_hostlist_add(const struct fc_lport *lp)
  1169. {
  1170. struct fcoe_softc *fc;
  1171. fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
  1172. if (!fc) {
  1173. fc = lport_priv(lp);
  1174. write_lock_bh(&fcoe_hostlist_lock);
  1175. list_add_tail(&fc->list, &fcoe_hostlist);
  1176. write_unlock_bh(&fcoe_hostlist_lock);
  1177. }
  1178. return 0;
  1179. }
  1180. EXPORT_SYMBOL_GPL(fcoe_hostlist_add);
  1181. /**
  1182. * fcoe_hostlist_remove() - remove a lport from lports list
  1183. * @lp: ptr to the fc_lport to badded
  1184. *
  1185. * Returns: 0 for success
  1186. */
  1187. int fcoe_hostlist_remove(const struct fc_lport *lp)
  1188. {
  1189. struct fcoe_softc *fc;
  1190. fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
  1191. BUG_ON(!fc);
  1192. write_lock_bh(&fcoe_hostlist_lock);
  1193. list_del(&fc->list);
  1194. write_unlock_bh(&fcoe_hostlist_lock);
  1195. return 0;
  1196. }
  1197. EXPORT_SYMBOL_GPL(fcoe_hostlist_remove);
  1198. /**
  1199. * fcoe_libfc_config() - sets up libfc related properties for lport
  1200. * @lp: ptr to the fc_lport
  1201. * @tt: libfc function template
  1202. *
  1203. * Returns : 0 for success
  1204. */
  1205. int fcoe_libfc_config(struct fc_lport *lp, struct libfc_function_template *tt)
  1206. {
  1207. /* Set the function pointers set by the LLDD */
  1208. memcpy(&lp->tt, tt, sizeof(*tt));
  1209. if (fc_fcp_init(lp))
  1210. return -ENOMEM;
  1211. fc_exch_init(lp);
  1212. fc_elsct_init(lp);
  1213. fc_lport_init(lp);
  1214. fc_rport_init(lp);
  1215. fc_disc_init(lp);
  1216. return 0;
  1217. }
  1218. EXPORT_SYMBOL_GPL(fcoe_libfc_config);
  1219. /**
  1220. * fcoe_init() - fcoe module loading initialization
  1221. *
  1222. * Initialization routine
  1223. * 1. Will create fc transport software structure
  1224. * 2. initialize the link list of port information structure
  1225. *
  1226. * Returns 0 on success, negative on failure
  1227. */
  1228. static int __init fcoe_init(void)
  1229. {
  1230. int cpu;
  1231. struct fcoe_percpu_s *p;
  1232. INIT_LIST_HEAD(&fcoe_hostlist);
  1233. rwlock_init(&fcoe_hostlist_lock);
  1234. #ifdef CONFIG_HOTPLUG_CPU
  1235. register_cpu_notifier(&fcoe_cpu_notifier);
  1236. #endif /* CONFIG_HOTPLUG_CPU */
  1237. /*
  1238. * initialize per CPU interrupt thread
  1239. */
  1240. for_each_online_cpu(cpu) {
  1241. p = kzalloc(sizeof(struct fcoe_percpu_s), GFP_KERNEL);
  1242. if (p) {
  1243. p->thread = kthread_create(fcoe_percpu_receive_thread,
  1244. (void *)p,
  1245. "fcoethread/%d", cpu);
  1246. /*
  1247. * if there is no error then bind the thread to the cpu
  1248. * initialize the semaphore and skb queue head
  1249. */
  1250. if (likely(!IS_ERR(p->thread))) {
  1251. p->cpu = cpu;
  1252. fcoe_percpu[cpu] = p;
  1253. skb_queue_head_init(&p->fcoe_rx_list);
  1254. kthread_bind(p->thread, cpu);
  1255. wake_up_process(p->thread);
  1256. } else {
  1257. fcoe_percpu[cpu] = NULL;
  1258. kfree(p);
  1259. }
  1260. }
  1261. }
  1262. /*
  1263. * setup link change notification
  1264. */
  1265. fcoe_dev_setup();
  1266. setup_timer(&fcoe_timer, fcoe_watchdog, 0);
  1267. mod_timer(&fcoe_timer, jiffies + (10 * HZ));
  1268. /* initiatlize the fcoe transport */
  1269. fcoe_transport_init();
  1270. fcoe_sw_init();
  1271. return 0;
  1272. }
  1273. module_init(fcoe_init);
  1274. /**
  1275. * fcoe_exit() - fcoe module unloading cleanup
  1276. *
  1277. * Returns 0 on success, negative on failure
  1278. */
  1279. static void __exit fcoe_exit(void)
  1280. {
  1281. u32 idx;
  1282. struct fcoe_softc *fc, *tmp;
  1283. struct fcoe_percpu_s *p;
  1284. struct sk_buff *skb;
  1285. /*
  1286. * Stop all call back interfaces
  1287. */
  1288. #ifdef CONFIG_HOTPLUG_CPU
  1289. unregister_cpu_notifier(&fcoe_cpu_notifier);
  1290. #endif /* CONFIG_HOTPLUG_CPU */
  1291. fcoe_dev_cleanup();
  1292. /*
  1293. * stop timer
  1294. */
  1295. del_timer_sync(&fcoe_timer);
  1296. /* releases the associated fcoe transport for each lport */
  1297. list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
  1298. fcoe_transport_release(fc->real_dev);
  1299. for (idx = 0; idx < NR_CPUS; idx++) {
  1300. if (fcoe_percpu[idx]) {
  1301. kthread_stop(fcoe_percpu[idx]->thread);
  1302. p = fcoe_percpu[idx];
  1303. spin_lock_bh(&p->fcoe_rx_list.lock);
  1304. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
  1305. kfree_skb(skb);
  1306. spin_unlock_bh(&p->fcoe_rx_list.lock);
  1307. if (fcoe_percpu[idx]->crc_eof_page)
  1308. put_page(fcoe_percpu[idx]->crc_eof_page);
  1309. kfree(fcoe_percpu[idx]);
  1310. }
  1311. }
  1312. /* remove sw trasnport */
  1313. fcoe_sw_exit();
  1314. /* detach the transport */
  1315. fcoe_transport_exit();
  1316. }
  1317. module_exit(fcoe_exit);