libfcoe.c 36 KB

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