libfcoe.c 38 KB

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