libfcoe.c 37 KB

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