libfcoe.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. /*
  2. * Copyright (c) 2008-2009 Cisco Systems, Inc. All rights reserved.
  3. * Copyright (c) 2009 Intel Corporation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. * Maintained at www.Open-FCoE.org
  19. */
  20. #include <linux/types.h>
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/list.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/timer.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/ethtool.h>
  29. #include <linux/if_ether.h>
  30. #include <linux/if_vlan.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/errno.h>
  33. #include <linux/bitops.h>
  34. #include <net/rtnetlink.h>
  35. #include <scsi/fc/fc_els.h>
  36. #include <scsi/fc/fc_fs.h>
  37. #include <scsi/fc/fc_fip.h>
  38. #include <scsi/fc/fc_encaps.h>
  39. #include <scsi/fc/fc_fcoe.h>
  40. #include <scsi/libfc.h>
  41. #include <scsi/libfcoe.h>
  42. MODULE_AUTHOR("Open-FCoE.org");
  43. MODULE_DESCRIPTION("FIP discovery protocol support for FCoE HBAs");
  44. MODULE_LICENSE("GPL v2");
  45. #define FCOE_CTLR_MIN_FKA 500 /* min keep alive (mS) */
  46. #define FCOE_CTLR_DEF_FKA FIP_DEF_FKA /* default keep alive (mS) */
  47. static void fcoe_ctlr_timeout(unsigned long);
  48. static void fcoe_ctlr_link_work(struct work_struct *);
  49. static void fcoe_ctlr_recv_work(struct work_struct *);
  50. static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
  51. static u32 fcoe_ctlr_debug; /* 1 for basic, 2 for noisy debug */
  52. #define FIP_DBG_LVL(level, fmt, args...) \
  53. do { \
  54. if (fcoe_ctlr_debug >= (level)) \
  55. FC_DBG(fmt, ##args); \
  56. } while (0)
  57. #define FIP_DBG(fmt, args...) FIP_DBG_LVL(1, fmt, ##args)
  58. /*
  59. * Return non-zero if FCF fcoe_size has been validated.
  60. */
  61. static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
  62. {
  63. return (fcf->flags & FIP_FL_SOL) != 0;
  64. }
  65. /*
  66. * Return non-zero if the FCF is usable.
  67. */
  68. static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
  69. {
  70. u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
  71. return (fcf->flags & flags) == flags;
  72. }
  73. /**
  74. * fcoe_ctlr_init() - Initialize the FCoE Controller instance.
  75. * @fip: FCoE controller.
  76. */
  77. void fcoe_ctlr_init(struct fcoe_ctlr *fip)
  78. {
  79. fip->state = FIP_ST_LINK_WAIT;
  80. INIT_LIST_HEAD(&fip->fcfs);
  81. spin_lock_init(&fip->lock);
  82. fip->flogi_oxid = FC_XID_UNKNOWN;
  83. setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
  84. INIT_WORK(&fip->link_work, fcoe_ctlr_link_work);
  85. INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
  86. skb_queue_head_init(&fip->fip_recv_list);
  87. }
  88. EXPORT_SYMBOL(fcoe_ctlr_init);
  89. /**
  90. * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller.
  91. * @fip: FCoE controller.
  92. *
  93. * Called with &fcoe_ctlr lock held.
  94. */
  95. static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
  96. {
  97. struct fcoe_fcf *fcf;
  98. struct fcoe_fcf *next;
  99. fip->sel_fcf = NULL;
  100. list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
  101. list_del(&fcf->list);
  102. kfree(fcf);
  103. }
  104. fip->fcf_count = 0;
  105. fip->sel_time = 0;
  106. }
  107. /**
  108. * fcoe_ctrl_destroy() - Disable and tear-down the FCoE controller.
  109. * @fip: FCoE controller.
  110. *
  111. * This is called by FCoE drivers before freeing the &fcoe_ctlr.
  112. *
  113. * The receive handler will have been deleted before this to guarantee
  114. * that no more recv_work will be scheduled.
  115. *
  116. * The timer routine will simply return once we set FIP_ST_DISABLED.
  117. * This guarantees that no further timeouts or work will be scheduled.
  118. */
  119. void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
  120. {
  121. flush_work(&fip->recv_work);
  122. spin_lock_bh(&fip->lock);
  123. fip->state = FIP_ST_DISABLED;
  124. fcoe_ctlr_reset_fcfs(fip);
  125. spin_unlock_bh(&fip->lock);
  126. del_timer_sync(&fip->timer);
  127. flush_work(&fip->link_work);
  128. }
  129. EXPORT_SYMBOL(fcoe_ctlr_destroy);
  130. /**
  131. * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port.
  132. * @fip: FCoE controller.
  133. *
  134. * Returns the maximum packet size including the FCoE header and trailer,
  135. * but not including any Ethernet or VLAN headers.
  136. */
  137. static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
  138. {
  139. /*
  140. * Determine the max FCoE frame size allowed, including
  141. * FCoE header and trailer.
  142. * Note: lp->mfs is currently the payload size, not the frame size.
  143. */
  144. return fip->lp->mfs + sizeof(struct fc_frame_header) +
  145. sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
  146. }
  147. /**
  148. * fcoe_ctlr_solicit() - Send a solicitation.
  149. * @fip: FCoE controller.
  150. * @fcf: Destination FCF. If NULL, a multicast solicitation is sent.
  151. */
  152. static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
  153. {
  154. struct sk_buff *skb;
  155. struct fip_sol {
  156. struct ethhdr eth;
  157. struct fip_header fip;
  158. struct {
  159. struct fip_mac_desc mac;
  160. struct fip_wwn_desc wwnn;
  161. struct fip_size_desc size;
  162. } __attribute__((packed)) desc;
  163. } __attribute__((packed)) *sol;
  164. u32 fcoe_size;
  165. skb = dev_alloc_skb(sizeof(*sol));
  166. if (!skb)
  167. return;
  168. sol = (struct fip_sol *)skb->data;
  169. memset(sol, 0, sizeof(*sol));
  170. memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
  171. memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
  172. sol->eth.h_proto = htons(ETH_P_FIP);
  173. sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
  174. sol->fip.fip_op = htons(FIP_OP_DISC);
  175. sol->fip.fip_subcode = FIP_SC_SOL;
  176. sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
  177. sol->fip.fip_flags = htons(FIP_FL_FPMA);
  178. if (fip->spma)
  179. sol->fip.fip_flags |= htons(FIP_FL_SPMA);
  180. sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
  181. sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
  182. memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
  183. sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
  184. sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
  185. put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
  186. fcoe_size = fcoe_ctlr_fcoe_size(fip);
  187. sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
  188. sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
  189. sol->desc.size.fd_size = htons(fcoe_size);
  190. skb_put(skb, sizeof(*sol));
  191. skb->protocol = htons(ETH_P_802_3);
  192. skb_reset_mac_header(skb);
  193. skb_reset_network_header(skb);
  194. fip->send(fip, skb);
  195. if (!fcf)
  196. fip->sol_time = jiffies;
  197. }
  198. /**
  199. * fcoe_ctlr_link_up() - Start FCoE controller.
  200. * @fip: FCoE controller.
  201. *
  202. * Called from the LLD when the network link is ready.
  203. */
  204. void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
  205. {
  206. spin_lock_bh(&fip->lock);
  207. if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
  208. fip->last_link = 1;
  209. fip->link = 1;
  210. spin_unlock_bh(&fip->lock);
  211. fc_linkup(fip->lp);
  212. } else if (fip->state == FIP_ST_LINK_WAIT) {
  213. fip->state = FIP_ST_AUTO;
  214. fip->last_link = 1;
  215. fip->link = 1;
  216. spin_unlock_bh(&fip->lock);
  217. FIP_DBG("%s", "setting AUTO mode.\n");
  218. fc_linkup(fip->lp);
  219. fcoe_ctlr_solicit(fip, NULL);
  220. } else
  221. spin_unlock_bh(&fip->lock);
  222. }
  223. EXPORT_SYMBOL(fcoe_ctlr_link_up);
  224. /**
  225. * fcoe_ctlr_reset() - Reset FIP.
  226. * @fip: FCoE controller.
  227. * @new_state: FIP state to be entered.
  228. *
  229. * Returns non-zero if the link was up and now isn't.
  230. */
  231. static int fcoe_ctlr_reset(struct fcoe_ctlr *fip, enum fip_state new_state)
  232. {
  233. struct fc_lport *lp = fip->lp;
  234. int link_dropped;
  235. spin_lock_bh(&fip->lock);
  236. fcoe_ctlr_reset_fcfs(fip);
  237. del_timer(&fip->timer);
  238. fip->state = new_state;
  239. fip->ctlr_ka_time = 0;
  240. fip->port_ka_time = 0;
  241. fip->sol_time = 0;
  242. fip->flogi_oxid = FC_XID_UNKNOWN;
  243. fip->map_dest = 0;
  244. fip->last_link = 0;
  245. link_dropped = fip->link;
  246. fip->link = 0;
  247. spin_unlock_bh(&fip->lock);
  248. if (link_dropped)
  249. fc_linkdown(lp);
  250. if (new_state == FIP_ST_ENABLED) {
  251. fcoe_ctlr_solicit(fip, NULL);
  252. fc_linkup(lp);
  253. link_dropped = 0;
  254. }
  255. return link_dropped;
  256. }
  257. /**
  258. * fcoe_ctlr_link_down() - Stop FCoE controller.
  259. * @fip: FCoE controller.
  260. *
  261. * Returns non-zero if the link was up and now isn't.
  262. *
  263. * Called from the LLD when the network link is not ready.
  264. * There may be multiple calls while the link is down.
  265. */
  266. int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
  267. {
  268. return fcoe_ctlr_reset(fip, FIP_ST_LINK_WAIT);
  269. }
  270. EXPORT_SYMBOL(fcoe_ctlr_link_down);
  271. /**
  272. * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF.
  273. * @fip: FCoE controller.
  274. * @ports: 0 for controller keep-alive, 1 for port keep-alive.
  275. * @sa: source MAC address.
  276. *
  277. * A controller keep-alive is sent every fka_period (typically 8 seconds).
  278. * The source MAC is the native MAC address.
  279. *
  280. * A port keep-alive is sent every 90 seconds while logged in.
  281. * The source MAC is the assigned mapped source address.
  282. * The destination is the FCF's F-port.
  283. */
  284. static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip, int ports, u8 *sa)
  285. {
  286. struct sk_buff *skb;
  287. struct fip_kal {
  288. struct ethhdr eth;
  289. struct fip_header fip;
  290. struct fip_mac_desc mac;
  291. } __attribute__((packed)) *kal;
  292. struct fip_vn_desc *vn;
  293. u32 len;
  294. struct fc_lport *lp;
  295. struct fcoe_fcf *fcf;
  296. fcf = fip->sel_fcf;
  297. lp = fip->lp;
  298. if (!fcf || !fc_host_port_id(lp->host))
  299. return;
  300. len = fcoe_ctlr_fcoe_size(fip) + sizeof(struct ethhdr);
  301. BUG_ON(len < sizeof(*kal) + sizeof(*vn));
  302. skb = dev_alloc_skb(len);
  303. if (!skb)
  304. return;
  305. kal = (struct fip_kal *)skb->data;
  306. memset(kal, 0, len);
  307. memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
  308. memcpy(kal->eth.h_source, sa, ETH_ALEN);
  309. kal->eth.h_proto = htons(ETH_P_FIP);
  310. kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
  311. kal->fip.fip_op = htons(FIP_OP_CTRL);
  312. kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
  313. kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
  314. ports * sizeof(*vn)) / FIP_BPW);
  315. kal->fip.fip_flags = htons(FIP_FL_FPMA);
  316. if (fip->spma)
  317. kal->fip.fip_flags |= htons(FIP_FL_SPMA);
  318. kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
  319. kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
  320. memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
  321. if (ports) {
  322. vn = (struct fip_vn_desc *)(kal + 1);
  323. vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
  324. vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
  325. memcpy(vn->fd_mac, fip->data_src_addr, ETH_ALEN);
  326. hton24(vn->fd_fc_id, fc_host_port_id(lp->host));
  327. put_unaligned_be64(lp->wwpn, &vn->fd_wwpn);
  328. }
  329. skb_put(skb, len);
  330. skb->protocol = htons(ETH_P_802_3);
  331. skb_reset_mac_header(skb);
  332. skb_reset_network_header(skb);
  333. fip->send(fip, skb);
  334. }
  335. /**
  336. * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it.
  337. * @fip: FCoE controller.
  338. * @dtype: FIP descriptor type for the frame.
  339. * @skb: FCoE ELS frame including FC header but no FCoE headers.
  340. *
  341. * Returns non-zero error code on failure.
  342. *
  343. * The caller must check that the length is a multiple of 4.
  344. *
  345. * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
  346. * Headroom includes the FIP encapsulation description, FIP header, and
  347. * Ethernet header. The tailroom is for the FIP MAC descriptor.
  348. */
  349. static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip,
  350. u8 dtype, struct sk_buff *skb)
  351. {
  352. struct fip_encaps_head {
  353. struct ethhdr eth;
  354. struct fip_header fip;
  355. struct fip_encaps encaps;
  356. } __attribute__((packed)) *cap;
  357. struct fip_mac_desc *mac;
  358. struct fcoe_fcf *fcf;
  359. size_t dlen;
  360. fcf = fip->sel_fcf;
  361. if (!fcf)
  362. return -ENODEV;
  363. dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
  364. cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
  365. memset(cap, 0, sizeof(*cap));
  366. memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
  367. memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
  368. cap->eth.h_proto = htons(ETH_P_FIP);
  369. cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
  370. cap->fip.fip_op = htons(FIP_OP_LS);
  371. cap->fip.fip_subcode = FIP_SC_REQ;
  372. cap->fip.fip_dl_len = htons((dlen + sizeof(*mac)) / FIP_BPW);
  373. cap->fip.fip_flags = htons(FIP_FL_FPMA);
  374. if (fip->spma)
  375. cap->fip.fip_flags |= htons(FIP_FL_SPMA);
  376. cap->encaps.fd_desc.fip_dtype = dtype;
  377. cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
  378. mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
  379. memset(mac, 0, sizeof(mac));
  380. mac->fd_desc.fip_dtype = FIP_DT_MAC;
  381. mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
  382. if (dtype != FIP_DT_FLOGI)
  383. memcpy(mac->fd_mac, fip->data_src_addr, ETH_ALEN);
  384. else if (fip->spma)
  385. memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
  386. skb->protocol = htons(ETH_P_802_3);
  387. skb_reset_mac_header(skb);
  388. skb_reset_network_header(skb);
  389. return 0;
  390. }
  391. /**
  392. * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
  393. * @fip: FCoE controller.
  394. * @skb: FCoE ELS frame including FC header but no FCoE headers.
  395. *
  396. * Returns a non-zero error code if the frame should not be sent.
  397. * Returns zero if the caller should send the frame with FCoE encapsulation.
  398. *
  399. * The caller must check that the length is a multiple of 4.
  400. * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
  401. */
  402. int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
  403. {
  404. struct fc_frame_header *fh;
  405. u16 old_xid;
  406. u8 op;
  407. if (fip->state == FIP_ST_NON_FIP)
  408. return 0;
  409. fh = (struct fc_frame_header *)skb->data;
  410. op = *(u8 *)(fh + 1);
  411. switch (op) {
  412. case ELS_FLOGI:
  413. old_xid = fip->flogi_oxid;
  414. fip->flogi_oxid = ntohs(fh->fh_ox_id);
  415. if (fip->state == FIP_ST_AUTO) {
  416. if (old_xid == FC_XID_UNKNOWN)
  417. fip->flogi_count = 0;
  418. fip->flogi_count++;
  419. if (fip->flogi_count < 3)
  420. goto drop;
  421. fip->map_dest = 1;
  422. return 0;
  423. }
  424. op = FIP_DT_FLOGI;
  425. break;
  426. case ELS_FDISC:
  427. if (ntoh24(fh->fh_s_id))
  428. return 0;
  429. op = FIP_DT_FDISC;
  430. break;
  431. case ELS_LOGO:
  432. if (fip->state != FIP_ST_ENABLED)
  433. return 0;
  434. if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
  435. return 0;
  436. op = FIP_DT_LOGO;
  437. break;
  438. case ELS_LS_ACC:
  439. if (fip->flogi_oxid == FC_XID_UNKNOWN)
  440. return 0;
  441. if (!ntoh24(fh->fh_s_id))
  442. return 0;
  443. if (fip->state == FIP_ST_AUTO)
  444. return 0;
  445. /*
  446. * Here we must've gotten an SID by accepting an FLOGI
  447. * from a point-to-point connection. Switch to using
  448. * the source mac based on the SID. The destination
  449. * MAC in this case would have been set by receving the
  450. * FLOGI.
  451. */
  452. fip->flogi_oxid = FC_XID_UNKNOWN;
  453. fc_fcoe_set_mac(fip->data_src_addr, fh->fh_s_id);
  454. return 0;
  455. default:
  456. if (fip->state != FIP_ST_ENABLED)
  457. goto drop;
  458. return 0;
  459. }
  460. if (fcoe_ctlr_encaps(fip, op, skb))
  461. goto drop;
  462. fip->send(fip, skb);
  463. return -EINPROGRESS;
  464. drop:
  465. kfree_skb(skb);
  466. return -EINVAL;
  467. }
  468. EXPORT_SYMBOL(fcoe_ctlr_els_send);
  469. /*
  470. * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller.
  471. * @fip: FCoE controller.
  472. *
  473. * Called with lock held.
  474. *
  475. * An FCF is considered old if we have missed three advertisements.
  476. * That is, there have been no valid advertisement from it for three
  477. * times its keep-alive period including fuzz.
  478. *
  479. * In addition, determine the time when an FCF selection can occur.
  480. */
  481. static void fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
  482. {
  483. struct fcoe_fcf *fcf;
  484. struct fcoe_fcf *next;
  485. unsigned long sel_time = 0;
  486. list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
  487. if (time_after(jiffies, fcf->time + fcf->fka_period * 3 +
  488. msecs_to_jiffies(FIP_FCF_FUZZ * 3))) {
  489. if (fip->sel_fcf == fcf)
  490. fip->sel_fcf = NULL;
  491. list_del(&fcf->list);
  492. WARN_ON(!fip->fcf_count);
  493. fip->fcf_count--;
  494. kfree(fcf);
  495. } else if (fcoe_ctlr_mtu_valid(fcf) &&
  496. (!sel_time || time_before(sel_time, fcf->time))) {
  497. sel_time = fcf->time;
  498. }
  499. }
  500. if (sel_time) {
  501. sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
  502. fip->sel_time = sel_time;
  503. if (time_before(sel_time, fip->timer.expires))
  504. mod_timer(&fip->timer, sel_time);
  505. } else {
  506. fip->sel_time = 0;
  507. }
  508. }
  509. /**
  510. * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry.
  511. * @skb: received FIP advertisement frame
  512. * @fcf: resulting FCF entry.
  513. *
  514. * Returns zero on a valid parsed advertisement,
  515. * otherwise returns non zero value.
  516. */
  517. static int fcoe_ctlr_parse_adv(struct sk_buff *skb, struct fcoe_fcf *fcf)
  518. {
  519. struct fip_header *fiph;
  520. struct fip_desc *desc = NULL;
  521. struct fip_wwn_desc *wwn;
  522. struct fip_fab_desc *fab;
  523. struct fip_fka_desc *fka;
  524. unsigned long t;
  525. size_t rlen;
  526. size_t dlen;
  527. memset(fcf, 0, sizeof(*fcf));
  528. fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
  529. fiph = (struct fip_header *)skb->data;
  530. fcf->flags = ntohs(fiph->fip_flags);
  531. rlen = ntohs(fiph->fip_dl_len) * 4;
  532. if (rlen + sizeof(*fiph) > skb->len)
  533. return -EINVAL;
  534. desc = (struct fip_desc *)(fiph + 1);
  535. while (rlen > 0) {
  536. dlen = desc->fip_dlen * FIP_BPW;
  537. if (dlen < sizeof(*desc) || dlen > rlen)
  538. return -EINVAL;
  539. switch (desc->fip_dtype) {
  540. case FIP_DT_PRI:
  541. if (dlen != sizeof(struct fip_pri_desc))
  542. goto len_err;
  543. fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
  544. break;
  545. case FIP_DT_MAC:
  546. if (dlen != sizeof(struct fip_mac_desc))
  547. goto len_err;
  548. memcpy(fcf->fcf_mac,
  549. ((struct fip_mac_desc *)desc)->fd_mac,
  550. ETH_ALEN);
  551. if (!is_valid_ether_addr(fcf->fcf_mac)) {
  552. FIP_DBG("invalid MAC addr in FIP adv\n");
  553. return -EINVAL;
  554. }
  555. break;
  556. case FIP_DT_NAME:
  557. if (dlen != sizeof(struct fip_wwn_desc))
  558. goto len_err;
  559. wwn = (struct fip_wwn_desc *)desc;
  560. fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
  561. break;
  562. case FIP_DT_FAB:
  563. if (dlen != sizeof(struct fip_fab_desc))
  564. goto len_err;
  565. fab = (struct fip_fab_desc *)desc;
  566. fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
  567. fcf->vfid = ntohs(fab->fd_vfid);
  568. fcf->fc_map = ntoh24(fab->fd_map);
  569. break;
  570. case FIP_DT_FKA:
  571. if (dlen != sizeof(struct fip_fka_desc))
  572. goto len_err;
  573. fka = (struct fip_fka_desc *)desc;
  574. t = ntohl(fka->fd_fka_period);
  575. if (t >= FCOE_CTLR_MIN_FKA)
  576. fcf->fka_period = msecs_to_jiffies(t);
  577. break;
  578. case FIP_DT_MAP_OUI:
  579. case FIP_DT_FCOE_SIZE:
  580. case FIP_DT_FLOGI:
  581. case FIP_DT_FDISC:
  582. case FIP_DT_LOGO:
  583. case FIP_DT_ELP:
  584. default:
  585. FIP_DBG("unexpected descriptor type %x in FIP adv\n",
  586. desc->fip_dtype);
  587. /* standard says ignore unknown descriptors >= 128 */
  588. if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
  589. return -EINVAL;
  590. continue;
  591. }
  592. desc = (struct fip_desc *)((char *)desc + dlen);
  593. rlen -= dlen;
  594. }
  595. if (!fcf->fc_map || (fcf->fc_map & 0x10000))
  596. return -EINVAL;
  597. if (!fcf->switch_name || !fcf->fabric_name)
  598. return -EINVAL;
  599. return 0;
  600. len_err:
  601. FIP_DBG("FIP length error in descriptor type %x len %zu\n",
  602. desc->fip_dtype, dlen);
  603. return -EINVAL;
  604. }
  605. /**
  606. * fcoe_ctlr_recv_adv() - Handle an incoming advertisement.
  607. * @fip: FCoE controller.
  608. * @skb: Received FIP packet.
  609. */
  610. static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
  611. {
  612. struct fcoe_fcf *fcf;
  613. struct fcoe_fcf new;
  614. struct fcoe_fcf *found;
  615. unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
  616. int first = 0;
  617. int mtu_valid;
  618. if (fcoe_ctlr_parse_adv(skb, &new))
  619. return;
  620. spin_lock_bh(&fip->lock);
  621. first = list_empty(&fip->fcfs);
  622. found = NULL;
  623. list_for_each_entry(fcf, &fip->fcfs, list) {
  624. if (fcf->switch_name == new.switch_name &&
  625. fcf->fabric_name == new.fabric_name &&
  626. fcf->fc_map == new.fc_map &&
  627. compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
  628. found = fcf;
  629. break;
  630. }
  631. }
  632. if (!found) {
  633. if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
  634. goto out;
  635. fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
  636. if (!fcf)
  637. goto out;
  638. fip->fcf_count++;
  639. memcpy(fcf, &new, sizeof(new));
  640. list_add(&fcf->list, &fip->fcfs);
  641. } else {
  642. /*
  643. * Flags in advertisements are ignored once the FCF is
  644. * selected. Flags in unsolicited advertisements are
  645. * ignored after a usable solicited advertisement
  646. * has been received.
  647. */
  648. if (fcf == fip->sel_fcf) {
  649. fip->ctlr_ka_time -= fcf->fka_period;
  650. fip->ctlr_ka_time += new.fka_period;
  651. if (time_before(fip->ctlr_ka_time, fip->timer.expires))
  652. mod_timer(&fip->timer, fip->ctlr_ka_time);
  653. } else if (!fcoe_ctlr_fcf_usable(fcf))
  654. fcf->flags = new.flags;
  655. fcf->fka_period = new.fka_period;
  656. memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
  657. }
  658. mtu_valid = fcoe_ctlr_mtu_valid(fcf);
  659. fcf->time = jiffies;
  660. FIP_DBG_LVL(found ? 2 : 1, "%s FCF for fab %llx map %x val %d\n",
  661. found ? "old" : "new",
  662. fcf->fabric_name, fcf->fc_map, mtu_valid);
  663. /*
  664. * If this advertisement is not solicited and our max receive size
  665. * hasn't been verified, send a solicited advertisement.
  666. */
  667. if (!mtu_valid)
  668. fcoe_ctlr_solicit(fip, fcf);
  669. /*
  670. * If its been a while since we did a solicit, and this is
  671. * the first advertisement we've received, do a multicast
  672. * solicitation to gather as many advertisements as we can
  673. * before selection occurs.
  674. */
  675. if (first && time_after(jiffies, fip->sol_time + sol_tov))
  676. fcoe_ctlr_solicit(fip, NULL);
  677. /*
  678. * If this is the first validated FCF, note the time and
  679. * set a timer to trigger selection.
  680. */
  681. if (mtu_valid && !fip->sel_time && fcoe_ctlr_fcf_usable(fcf)) {
  682. fip->sel_time = jiffies +
  683. msecs_to_jiffies(FCOE_CTLR_START_DELAY);
  684. if (!timer_pending(&fip->timer) ||
  685. time_before(fip->sel_time, fip->timer.expires))
  686. mod_timer(&fip->timer, fip->sel_time);
  687. }
  688. out:
  689. spin_unlock_bh(&fip->lock);
  690. }
  691. /**
  692. * fcoe_ctlr_recv_els() - Handle an incoming FIP-encapsulated ELS frame.
  693. * @fip: FCoE controller.
  694. * @skb: Received FIP packet.
  695. */
  696. static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
  697. {
  698. struct fc_lport *lp = fip->lp;
  699. struct fip_header *fiph;
  700. struct fc_frame *fp;
  701. struct fc_frame_header *fh = NULL;
  702. struct fip_desc *desc;
  703. struct fip_encaps *els;
  704. struct fcoe_dev_stats *stats;
  705. enum fip_desc_type els_dtype = 0;
  706. u8 els_op;
  707. u8 sub;
  708. u8 granted_mac[ETH_ALEN] = { 0 };
  709. size_t els_len = 0;
  710. size_t rlen;
  711. size_t dlen;
  712. fiph = (struct fip_header *)skb->data;
  713. sub = fiph->fip_subcode;
  714. if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
  715. goto drop;
  716. rlen = ntohs(fiph->fip_dl_len) * 4;
  717. if (rlen + sizeof(*fiph) > skb->len)
  718. goto drop;
  719. desc = (struct fip_desc *)(fiph + 1);
  720. while (rlen > 0) {
  721. dlen = desc->fip_dlen * FIP_BPW;
  722. if (dlen < sizeof(*desc) || dlen > rlen)
  723. goto drop;
  724. switch (desc->fip_dtype) {
  725. case FIP_DT_MAC:
  726. if (dlen != sizeof(struct fip_mac_desc))
  727. goto len_err;
  728. memcpy(granted_mac,
  729. ((struct fip_mac_desc *)desc)->fd_mac,
  730. ETH_ALEN);
  731. if (!is_valid_ether_addr(granted_mac)) {
  732. FIP_DBG("invalid MAC addrs in FIP ELS\n");
  733. goto drop;
  734. }
  735. break;
  736. case FIP_DT_FLOGI:
  737. case FIP_DT_FDISC:
  738. case FIP_DT_LOGO:
  739. case FIP_DT_ELP:
  740. if (fh)
  741. goto drop;
  742. if (dlen < sizeof(*els) + sizeof(*fh) + 1)
  743. goto len_err;
  744. els_len = dlen - sizeof(*els);
  745. els = (struct fip_encaps *)desc;
  746. fh = (struct fc_frame_header *)(els + 1);
  747. els_dtype = desc->fip_dtype;
  748. break;
  749. default:
  750. FIP_DBG("unexpected descriptor type %x "
  751. "in FIP adv\n", desc->fip_dtype);
  752. /* standard says ignore unknown descriptors >= 128 */
  753. if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
  754. goto drop;
  755. continue;
  756. }
  757. desc = (struct fip_desc *)((char *)desc + dlen);
  758. rlen -= dlen;
  759. }
  760. if (!fh)
  761. goto drop;
  762. els_op = *(u8 *)(fh + 1);
  763. if (els_dtype == FIP_DT_FLOGI && sub == FIP_SC_REP &&
  764. fip->flogi_oxid == ntohs(fh->fh_ox_id) &&
  765. els_op == ELS_LS_ACC && is_valid_ether_addr(granted_mac)) {
  766. fip->flogi_oxid = FC_XID_UNKNOWN;
  767. fip->update_mac(fip, fip->data_src_addr, granted_mac);
  768. memcpy(fip->data_src_addr, granted_mac, ETH_ALEN);
  769. }
  770. /*
  771. * Convert skb into an fc_frame containing only the ELS.
  772. */
  773. skb_pull(skb, (u8 *)fh - skb->data);
  774. skb_trim(skb, els_len);
  775. fp = (struct fc_frame *)skb;
  776. fc_frame_init(fp);
  777. fr_sof(fp) = FC_SOF_I3;
  778. fr_eof(fp) = FC_EOF_T;
  779. fr_dev(fp) = lp;
  780. stats = fc_lport_get_stats(lp);
  781. stats->RxFrames++;
  782. stats->RxWords += skb->len / FIP_BPW;
  783. fc_exch_recv(lp, lp->emp, fp);
  784. return;
  785. len_err:
  786. FIP_DBG("FIP length error in descriptor type %x len %zu\n",
  787. desc->fip_dtype, dlen);
  788. drop:
  789. kfree_skb(skb);
  790. }
  791. /**
  792. * fcoe_ctlr_recv_els() - Handle an incoming link reset frame.
  793. * @fip: FCoE controller.
  794. * @fh: Received FIP header.
  795. *
  796. * There may be multiple VN_Port descriptors.
  797. * The overall length has already been checked.
  798. */
  799. static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
  800. struct fip_header *fh)
  801. {
  802. struct fip_desc *desc;
  803. struct fip_mac_desc *mp;
  804. struct fip_wwn_desc *wp;
  805. struct fip_vn_desc *vp;
  806. size_t rlen;
  807. size_t dlen;
  808. struct fcoe_fcf *fcf = fip->sel_fcf;
  809. struct fc_lport *lp = fip->lp;
  810. u32 desc_mask;
  811. FIP_DBG("Clear Virtual Link received\n");
  812. if (!fcf)
  813. return;
  814. if (!fcf || !fc_host_port_id(lp->host))
  815. return;
  816. /*
  817. * mask of required descriptors. Validating each one clears its bit.
  818. */
  819. desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
  820. rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
  821. desc = (struct fip_desc *)(fh + 1);
  822. while (rlen >= sizeof(*desc)) {
  823. dlen = desc->fip_dlen * FIP_BPW;
  824. if (dlen > rlen)
  825. return;
  826. switch (desc->fip_dtype) {
  827. case FIP_DT_MAC:
  828. mp = (struct fip_mac_desc *)desc;
  829. if (dlen < sizeof(*mp))
  830. return;
  831. if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
  832. return;
  833. desc_mask &= ~BIT(FIP_DT_MAC);
  834. break;
  835. case FIP_DT_NAME:
  836. wp = (struct fip_wwn_desc *)desc;
  837. if (dlen < sizeof(*wp))
  838. return;
  839. if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
  840. return;
  841. desc_mask &= ~BIT(FIP_DT_NAME);
  842. break;
  843. case FIP_DT_VN_ID:
  844. vp = (struct fip_vn_desc *)desc;
  845. if (dlen < sizeof(*vp))
  846. return;
  847. if (compare_ether_addr(vp->fd_mac,
  848. fip->data_src_addr) == 0 &&
  849. get_unaligned_be64(&vp->fd_wwpn) == lp->wwpn &&
  850. ntoh24(vp->fd_fc_id) == fc_host_port_id(lp->host))
  851. desc_mask &= ~BIT(FIP_DT_VN_ID);
  852. break;
  853. default:
  854. /* standard says ignore unknown descriptors >= 128 */
  855. if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
  856. return;
  857. break;
  858. }
  859. desc = (struct fip_desc *)((char *)desc + dlen);
  860. rlen -= dlen;
  861. }
  862. /*
  863. * reset only if all required descriptors were present and valid.
  864. */
  865. if (desc_mask) {
  866. FIP_DBG("missing descriptors mask %x\n", desc_mask);
  867. } else {
  868. FIP_DBG("performing Clear Virtual Link\n");
  869. fcoe_ctlr_reset(fip, FIP_ST_ENABLED);
  870. }
  871. }
  872. /**
  873. * fcoe_ctlr_recv() - Receive a FIP frame.
  874. * @fip: FCoE controller.
  875. * @skb: Received FIP packet.
  876. *
  877. * This is called from NET_RX_SOFTIRQ.
  878. */
  879. void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
  880. {
  881. spin_lock_bh(&fip->fip_recv_list.lock);
  882. __skb_queue_tail(&fip->fip_recv_list, skb);
  883. spin_unlock_bh(&fip->fip_recv_list.lock);
  884. schedule_work(&fip->recv_work);
  885. }
  886. EXPORT_SYMBOL(fcoe_ctlr_recv);
  887. /**
  888. * fcoe_ctlr_recv_handler() - Receive a FIP frame.
  889. * @fip: FCoE controller.
  890. * @skb: Received FIP packet.
  891. *
  892. * Returns non-zero if the frame is dropped.
  893. */
  894. static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
  895. {
  896. struct fip_header *fiph;
  897. struct ethhdr *eh;
  898. enum fip_state state;
  899. u16 op;
  900. u8 sub;
  901. if (skb_linearize(skb))
  902. goto drop;
  903. if (skb->len < sizeof(*fiph))
  904. goto drop;
  905. eh = eth_hdr(skb);
  906. if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
  907. compare_ether_addr(eh->h_dest, FIP_ALL_ENODE_MACS))
  908. goto drop;
  909. fiph = (struct fip_header *)skb->data;
  910. op = ntohs(fiph->fip_op);
  911. sub = fiph->fip_subcode;
  912. FIP_DBG_LVL(2, "ver %x op %x/%x dl %x fl %x\n",
  913. FIP_VER_DECAPS(fiph->fip_ver), op, sub,
  914. ntohs(fiph->fip_dl_len), ntohs(fiph->fip_flags));
  915. if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
  916. goto drop;
  917. if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
  918. goto drop;
  919. spin_lock_bh(&fip->lock);
  920. state = fip->state;
  921. if (state == FIP_ST_AUTO) {
  922. fip->map_dest = 0;
  923. fip->state = FIP_ST_ENABLED;
  924. state = FIP_ST_ENABLED;
  925. FIP_DBG("using FIP mode\n");
  926. }
  927. spin_unlock_bh(&fip->lock);
  928. if (state != FIP_ST_ENABLED)
  929. goto drop;
  930. if (op == FIP_OP_LS) {
  931. fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
  932. return 0;
  933. }
  934. if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
  935. fcoe_ctlr_recv_adv(fip, skb);
  936. else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
  937. fcoe_ctlr_recv_clr_vlink(fip, fiph);
  938. kfree_skb(skb);
  939. return 0;
  940. drop:
  941. kfree_skb(skb);
  942. return -1;
  943. }
  944. /**
  945. * fcoe_ctlr_select() - Select the best FCF, if possible.
  946. * @fip: FCoE controller.
  947. *
  948. * If there are conflicting advertisements, no FCF can be chosen.
  949. *
  950. * Called with lock held.
  951. */
  952. static void fcoe_ctlr_select(struct fcoe_ctlr *fip)
  953. {
  954. struct fcoe_fcf *fcf;
  955. struct fcoe_fcf *best = NULL;
  956. list_for_each_entry(fcf, &fip->fcfs, list) {
  957. FIP_DBG("consider FCF for fab %llx VFID %d map %x val %d\n",
  958. fcf->fabric_name, fcf->vfid,
  959. fcf->fc_map, fcoe_ctlr_mtu_valid(fcf));
  960. if (!fcoe_ctlr_fcf_usable(fcf)) {
  961. FIP_DBG("FCF for fab %llx map %x %svalid %savailable\n",
  962. fcf->fabric_name, fcf->fc_map,
  963. (fcf->flags & FIP_FL_SOL) ? "" : "in",
  964. (fcf->flags & FIP_FL_AVAIL) ? "" : "un");
  965. continue;
  966. }
  967. if (!best) {
  968. best = fcf;
  969. continue;
  970. }
  971. if (fcf->fabric_name != best->fabric_name ||
  972. fcf->vfid != best->vfid ||
  973. fcf->fc_map != best->fc_map) {
  974. FIP_DBG("conflicting fabric, VFID, or FC-MAP\n");
  975. return;
  976. }
  977. if (fcf->pri < best->pri)
  978. best = fcf;
  979. }
  980. fip->sel_fcf = best;
  981. }
  982. /**
  983. * fcoe_ctlr_timeout() - FIP timer function.
  984. * @arg: &fcoe_ctlr pointer.
  985. *
  986. * Ages FCFs. Triggers FCF selection if possible. Sends keep-alives.
  987. */
  988. static void fcoe_ctlr_timeout(unsigned long arg)
  989. {
  990. struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
  991. struct fcoe_fcf *sel;
  992. struct fcoe_fcf *fcf;
  993. unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
  994. DECLARE_MAC_BUF(buf);
  995. u8 send_ctlr_ka;
  996. u8 send_port_ka;
  997. spin_lock_bh(&fip->lock);
  998. if (fip->state == FIP_ST_DISABLED) {
  999. spin_unlock_bh(&fip->lock);
  1000. return;
  1001. }
  1002. fcf = fip->sel_fcf;
  1003. fcoe_ctlr_age_fcfs(fip);
  1004. sel = fip->sel_fcf;
  1005. if (!sel && fip->sel_time && time_after_eq(jiffies, fip->sel_time)) {
  1006. fcoe_ctlr_select(fip);
  1007. sel = fip->sel_fcf;
  1008. fip->sel_time = 0;
  1009. }
  1010. if (sel != fcf) {
  1011. fcf = sel; /* the old FCF may have been freed */
  1012. if (sel) {
  1013. printk(KERN_INFO "host%d: FIP selected "
  1014. "Fibre-Channel Forwarder MAC %s\n",
  1015. fip->lp->host->host_no,
  1016. print_mac(buf, sel->fcf_mac));
  1017. memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
  1018. fip->port_ka_time = jiffies +
  1019. msecs_to_jiffies(FIP_VN_KA_PERIOD);
  1020. fip->ctlr_ka_time = jiffies + sel->fka_period;
  1021. fip->link = 1;
  1022. } else {
  1023. printk(KERN_NOTICE "host%d: "
  1024. "FIP Fibre-Channel Forwarder timed out. "
  1025. "Starting FCF discovery.\n",
  1026. fip->lp->host->host_no);
  1027. fip->link = 0;
  1028. }
  1029. schedule_work(&fip->link_work);
  1030. }
  1031. send_ctlr_ka = 0;
  1032. send_port_ka = 0;
  1033. if (sel) {
  1034. if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
  1035. fip->ctlr_ka_time = jiffies + sel->fka_period;
  1036. send_ctlr_ka = 1;
  1037. }
  1038. if (time_after(next_timer, fip->ctlr_ka_time))
  1039. next_timer = fip->ctlr_ka_time;
  1040. if (time_after_eq(jiffies, fip->port_ka_time)) {
  1041. fip->port_ka_time += jiffies +
  1042. msecs_to_jiffies(FIP_VN_KA_PERIOD);
  1043. send_port_ka = 1;
  1044. }
  1045. if (time_after(next_timer, fip->port_ka_time))
  1046. next_timer = fip->port_ka_time;
  1047. mod_timer(&fip->timer, next_timer);
  1048. } else if (fip->sel_time) {
  1049. next_timer = fip->sel_time +
  1050. msecs_to_jiffies(FCOE_CTLR_START_DELAY);
  1051. mod_timer(&fip->timer, next_timer);
  1052. }
  1053. spin_unlock_bh(&fip->lock);
  1054. if (send_ctlr_ka)
  1055. fcoe_ctlr_send_keep_alive(fip, 0, fip->ctl_src_addr);
  1056. if (send_port_ka)
  1057. fcoe_ctlr_send_keep_alive(fip, 1, fip->data_src_addr);
  1058. }
  1059. /**
  1060. * fcoe_ctlr_link_work() - worker thread function for link changes.
  1061. * @work: pointer to link_work member inside &fcoe_ctlr.
  1062. *
  1063. * See if the link status has changed and if so, report it.
  1064. *
  1065. * This is here because fc_linkup() and fc_linkdown() must not
  1066. * be called from the timer directly, since they use a mutex.
  1067. */
  1068. static void fcoe_ctlr_link_work(struct work_struct *work)
  1069. {
  1070. struct fcoe_ctlr *fip;
  1071. int link;
  1072. int last_link;
  1073. fip = container_of(work, struct fcoe_ctlr, link_work);
  1074. spin_lock_bh(&fip->lock);
  1075. last_link = fip->last_link;
  1076. link = fip->link;
  1077. fip->last_link = link;
  1078. spin_unlock_bh(&fip->lock);
  1079. if (last_link != link) {
  1080. if (link)
  1081. fc_linkup(fip->lp);
  1082. else
  1083. fcoe_ctlr_reset(fip, FIP_ST_LINK_WAIT);
  1084. }
  1085. }
  1086. /**
  1087. * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames.
  1088. * @recv_work: pointer to recv_work member inside &fcoe_ctlr.
  1089. */
  1090. static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
  1091. {
  1092. struct fcoe_ctlr *fip;
  1093. struct sk_buff *skb;
  1094. fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
  1095. spin_lock_bh(&fip->fip_recv_list.lock);
  1096. while ((skb = __skb_dequeue(&fip->fip_recv_list))) {
  1097. spin_unlock_bh(&fip->fip_recv_list.lock);
  1098. fcoe_ctlr_recv_handler(fip, skb);
  1099. spin_lock_bh(&fip->fip_recv_list.lock);
  1100. }
  1101. spin_unlock_bh(&fip->fip_recv_list.lock);
  1102. }
  1103. /**
  1104. * fcoe_ctlr_recv_flogi() - snoop Pre-FIP receipt of FLOGI response or request.
  1105. * @fip: FCoE controller.
  1106. * @fp: FC frame.
  1107. * @sa: Ethernet source MAC address from received FCoE frame.
  1108. *
  1109. * Snoop potential response to FLOGI or even incoming FLOGI.
  1110. *
  1111. * The caller has checked that we are waiting for login as indicated
  1112. * by fip->flogi_oxid != FC_XID_UNKNOWN.
  1113. *
  1114. * The caller is responsible for freeing the frame.
  1115. *
  1116. * Return non-zero if the frame should not be delivered to libfc.
  1117. */
  1118. int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_frame *fp, u8 *sa)
  1119. {
  1120. struct fc_frame_header *fh;
  1121. u8 op;
  1122. u8 mac[ETH_ALEN];
  1123. fh = fc_frame_header_get(fp);
  1124. if (fh->fh_type != FC_TYPE_ELS)
  1125. return 0;
  1126. op = fc_frame_payload_op(fp);
  1127. if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
  1128. fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
  1129. spin_lock_bh(&fip->lock);
  1130. if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
  1131. spin_unlock_bh(&fip->lock);
  1132. return -EINVAL;
  1133. }
  1134. fip->state = FIP_ST_NON_FIP;
  1135. FIP_DBG("received FLOGI LS_ACC using non-FIP mode\n");
  1136. /*
  1137. * FLOGI accepted.
  1138. * If the src mac addr is FC_OUI-based, then we mark the
  1139. * address_mode flag to use FC_OUI-based Ethernet DA.
  1140. * Otherwise we use the FCoE gateway addr
  1141. */
  1142. if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
  1143. fip->map_dest = 1;
  1144. } else {
  1145. memcpy(fip->dest_addr, sa, ETH_ALEN);
  1146. fip->map_dest = 0;
  1147. }
  1148. fip->flogi_oxid = FC_XID_UNKNOWN;
  1149. memcpy(mac, fip->data_src_addr, ETH_ALEN);
  1150. fc_fcoe_set_mac(fip->data_src_addr, fh->fh_d_id);
  1151. spin_unlock_bh(&fip->lock);
  1152. fip->update_mac(fip, mac, fip->data_src_addr);
  1153. } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
  1154. /*
  1155. * Save source MAC for point-to-point responses.
  1156. */
  1157. spin_lock_bh(&fip->lock);
  1158. if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
  1159. memcpy(fip->dest_addr, sa, ETH_ALEN);
  1160. fip->map_dest = 0;
  1161. if (fip->state == FIP_ST_NON_FIP)
  1162. FIP_DBG("received FLOGI REQ, "
  1163. "using non-FIP mode\n");
  1164. fip->state = FIP_ST_NON_FIP;
  1165. }
  1166. spin_unlock_bh(&fip->lock);
  1167. }
  1168. return 0;
  1169. }
  1170. EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
  1171. /**
  1172. * fcoe_wwn_from_mac() - Converts 48-bit IEEE MAC address to 64-bit FC WWN.
  1173. * @mac: mac address
  1174. * @scheme: check port
  1175. * @port: port indicator for converting
  1176. *
  1177. * Returns: u64 fc world wide name
  1178. */
  1179. u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
  1180. unsigned int scheme, unsigned int port)
  1181. {
  1182. u64 wwn;
  1183. u64 host_mac;
  1184. /* The MAC is in NO, so flip only the low 48 bits */
  1185. host_mac = ((u64) mac[0] << 40) |
  1186. ((u64) mac[1] << 32) |
  1187. ((u64) mac[2] << 24) |
  1188. ((u64) mac[3] << 16) |
  1189. ((u64) mac[4] << 8) |
  1190. (u64) mac[5];
  1191. WARN_ON(host_mac >= (1ULL << 48));
  1192. wwn = host_mac | ((u64) scheme << 60);
  1193. switch (scheme) {
  1194. case 1:
  1195. WARN_ON(port != 0);
  1196. break;
  1197. case 2:
  1198. WARN_ON(port >= 0xfff);
  1199. wwn |= (u64) port << 48;
  1200. break;
  1201. default:
  1202. WARN_ON(1);
  1203. break;
  1204. }
  1205. return wwn;
  1206. }
  1207. EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
  1208. /**
  1209. * fcoe_libfc_config() - sets up libfc related properties for lport
  1210. * @lp: ptr to the fc_lport
  1211. * @tt: libfc function template
  1212. *
  1213. * Returns : 0 for success
  1214. */
  1215. int fcoe_libfc_config(struct fc_lport *lp, struct libfc_function_template *tt)
  1216. {
  1217. /* Set the function pointers set by the LLDD */
  1218. memcpy(&lp->tt, tt, sizeof(*tt));
  1219. if (fc_fcp_init(lp))
  1220. return -ENOMEM;
  1221. fc_exch_init(lp);
  1222. fc_elsct_init(lp);
  1223. fc_lport_init(lp);
  1224. fc_rport_init(lp);
  1225. fc_disc_init(lp);
  1226. return 0;
  1227. }
  1228. EXPORT_SYMBOL_GPL(fcoe_libfc_config);