libfcoe.c 38 KB

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