libfcoe.c 36 KB

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