libfcoe.c 37 KB

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