libfcoe.c 36 KB

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