libfcoe.c 37 KB

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