libfcoe.c 38 KB

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