fc_disc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Maintained at www.Open-FCoE.org
  18. */
  19. /*
  20. * Target Discovery
  21. *
  22. * This block discovers all FC-4 remote ports, including FCP initiators. It
  23. * also handles RSCN events and re-discovery if necessary.
  24. */
  25. /*
  26. * DISC LOCKING
  27. *
  28. * The disc mutex is can be locked when acquiring rport locks, but may not
  29. * be held when acquiring the lport lock. Refer to fc_lport.c for more
  30. * details.
  31. */
  32. #include <linux/timer.h>
  33. #include <linux/slab.h>
  34. #include <linux/err.h>
  35. #include <linux/export.h>
  36. #include <asm/unaligned.h>
  37. #include <scsi/fc/fc_gs.h>
  38. #include <scsi/libfc.h>
  39. #include "fc_libfc.h"
  40. #define FC_DISC_RETRY_LIMIT 3 /* max retries */
  41. #define FC_DISC_RETRY_DELAY 500UL /* (msecs) delay */
  42. static void fc_disc_gpn_ft_req(struct fc_disc *);
  43. static void fc_disc_gpn_ft_resp(struct fc_seq *, struct fc_frame *, void *);
  44. static void fc_disc_done(struct fc_disc *, enum fc_disc_event);
  45. static void fc_disc_timeout(struct work_struct *);
  46. static int fc_disc_single(struct fc_lport *, struct fc_disc_port *);
  47. static void fc_disc_restart(struct fc_disc *);
  48. /**
  49. * fc_disc_stop_rports() - Delete all the remote ports associated with the lport
  50. * @disc: The discovery job to stop remote ports on
  51. *
  52. * Locking Note: This function expects that the lport mutex is locked before
  53. * calling it.
  54. */
  55. void fc_disc_stop_rports(struct fc_disc *disc)
  56. {
  57. struct fc_lport *lport;
  58. struct fc_rport_priv *rdata;
  59. lport = fc_disc_lport(disc);
  60. mutex_lock(&disc->disc_mutex);
  61. list_for_each_entry_rcu(rdata, &disc->rports, peers)
  62. lport->tt.rport_logoff(rdata);
  63. mutex_unlock(&disc->disc_mutex);
  64. }
  65. /**
  66. * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN)
  67. * @disc: The discovery object to which the RSCN applies
  68. * @fp: The RSCN frame
  69. *
  70. * Locking Note: This function expects that the disc_mutex is locked
  71. * before it is called.
  72. */
  73. static void fc_disc_recv_rscn_req(struct fc_disc *disc, struct fc_frame *fp)
  74. {
  75. struct fc_lport *lport;
  76. struct fc_els_rscn *rp;
  77. struct fc_els_rscn_page *pp;
  78. struct fc_seq_els_data rjt_data;
  79. unsigned int len;
  80. int redisc = 0;
  81. enum fc_els_rscn_ev_qual ev_qual;
  82. enum fc_els_rscn_addr_fmt fmt;
  83. LIST_HEAD(disc_ports);
  84. struct fc_disc_port *dp, *next;
  85. lport = fc_disc_lport(disc);
  86. FC_DISC_DBG(disc, "Received an RSCN event\n");
  87. /* make sure the frame contains an RSCN message */
  88. rp = fc_frame_payload_get(fp, sizeof(*rp));
  89. if (!rp)
  90. goto reject;
  91. /* make sure the page length is as expected (4 bytes) */
  92. if (rp->rscn_page_len != sizeof(*pp))
  93. goto reject;
  94. /* get the RSCN payload length */
  95. len = ntohs(rp->rscn_plen);
  96. if (len < sizeof(*rp))
  97. goto reject;
  98. /* make sure the frame contains the expected payload */
  99. rp = fc_frame_payload_get(fp, len);
  100. if (!rp)
  101. goto reject;
  102. /* payload must be a multiple of the RSCN page size */
  103. len -= sizeof(*rp);
  104. if (len % sizeof(*pp))
  105. goto reject;
  106. for (pp = (void *)(rp + 1); len > 0; len -= sizeof(*pp), pp++) {
  107. ev_qual = pp->rscn_page_flags >> ELS_RSCN_EV_QUAL_BIT;
  108. ev_qual &= ELS_RSCN_EV_QUAL_MASK;
  109. fmt = pp->rscn_page_flags >> ELS_RSCN_ADDR_FMT_BIT;
  110. fmt &= ELS_RSCN_ADDR_FMT_MASK;
  111. /*
  112. * if we get an address format other than port
  113. * (area, domain, fabric), then do a full discovery
  114. */
  115. switch (fmt) {
  116. case ELS_ADDR_FMT_PORT:
  117. FC_DISC_DBG(disc, "Port address format for port "
  118. "(%6.6x)\n", ntoh24(pp->rscn_fid));
  119. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  120. if (!dp) {
  121. redisc = 1;
  122. break;
  123. }
  124. dp->lp = lport;
  125. dp->port_id = ntoh24(pp->rscn_fid);
  126. list_add_tail(&dp->peers, &disc_ports);
  127. break;
  128. case ELS_ADDR_FMT_AREA:
  129. case ELS_ADDR_FMT_DOM:
  130. case ELS_ADDR_FMT_FAB:
  131. default:
  132. FC_DISC_DBG(disc, "Address format is (%d)\n", fmt);
  133. redisc = 1;
  134. break;
  135. }
  136. }
  137. lport->tt.seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
  138. /*
  139. * If not doing a complete rediscovery, do GPN_ID on
  140. * the individual ports mentioned in the list.
  141. * If any of these get an error, do a full rediscovery.
  142. * In any case, go through the list and free the entries.
  143. */
  144. list_for_each_entry_safe(dp, next, &disc_ports, peers) {
  145. list_del(&dp->peers);
  146. if (!redisc)
  147. redisc = fc_disc_single(lport, dp);
  148. kfree(dp);
  149. }
  150. if (redisc) {
  151. FC_DISC_DBG(disc, "RSCN received: rediscovering\n");
  152. fc_disc_restart(disc);
  153. } else {
  154. FC_DISC_DBG(disc, "RSCN received: not rediscovering. "
  155. "redisc %d state %d in_prog %d\n",
  156. redisc, lport->state, disc->pending);
  157. }
  158. fc_frame_free(fp);
  159. return;
  160. reject:
  161. FC_DISC_DBG(disc, "Received a bad RSCN frame\n");
  162. rjt_data.reason = ELS_RJT_LOGIC;
  163. rjt_data.explan = ELS_EXPL_NONE;
  164. lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
  165. fc_frame_free(fp);
  166. }
  167. /**
  168. * fc_disc_recv_req() - Handle incoming requests
  169. * @lport: The local port receiving the request
  170. * @fp: The request frame
  171. *
  172. * Locking Note: This function is called from the EM and will lock
  173. * the disc_mutex before calling the handler for the
  174. * request.
  175. */
  176. static void fc_disc_recv_req(struct fc_lport *lport, struct fc_frame *fp)
  177. {
  178. u8 op;
  179. struct fc_disc *disc = &lport->disc;
  180. op = fc_frame_payload_op(fp);
  181. switch (op) {
  182. case ELS_RSCN:
  183. mutex_lock(&disc->disc_mutex);
  184. fc_disc_recv_rscn_req(disc, fp);
  185. mutex_unlock(&disc->disc_mutex);
  186. break;
  187. default:
  188. FC_DISC_DBG(disc, "Received an unsupported request, "
  189. "the opcode is (%x)\n", op);
  190. fc_frame_free(fp);
  191. break;
  192. }
  193. }
  194. /**
  195. * fc_disc_restart() - Restart discovery
  196. * @disc: The discovery object to be restarted
  197. *
  198. * Locking Note: This function expects that the disc mutex
  199. * is already locked.
  200. */
  201. static void fc_disc_restart(struct fc_disc *disc)
  202. {
  203. if (!disc->disc_callback)
  204. return;
  205. FC_DISC_DBG(disc, "Restarting discovery\n");
  206. disc->requested = 1;
  207. if (disc->pending)
  208. return;
  209. /*
  210. * Advance disc_id. This is an arbitrary non-zero number that will
  211. * match the value in the fc_rport_priv after discovery for all
  212. * freshly-discovered remote ports. Avoid wrapping to zero.
  213. */
  214. disc->disc_id = (disc->disc_id + 2) | 1;
  215. disc->retry_count = 0;
  216. fc_disc_gpn_ft_req(disc);
  217. }
  218. /**
  219. * fc_disc_start() - Start discovery on a local port
  220. * @lport: The local port to have discovery started on
  221. * @disc_callback: Callback function to be called when discovery is complete
  222. */
  223. static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
  224. enum fc_disc_event),
  225. struct fc_lport *lport)
  226. {
  227. struct fc_disc *disc = &lport->disc;
  228. /*
  229. * At this point we may have a new disc job or an existing
  230. * one. Either way, let's lock when we make changes to it
  231. * and send the GPN_FT request.
  232. */
  233. mutex_lock(&disc->disc_mutex);
  234. disc->disc_callback = disc_callback;
  235. fc_disc_restart(disc);
  236. mutex_unlock(&disc->disc_mutex);
  237. }
  238. /**
  239. * fc_disc_done() - Discovery has been completed
  240. * @disc: The discovery context
  241. * @event: The discovery completion status
  242. *
  243. * Locking Note: This function expects that the disc mutex is locked before
  244. * it is called. The discovery callback is then made with the lock released,
  245. * and the lock is re-taken before returning from this function
  246. */
  247. static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event)
  248. {
  249. struct fc_lport *lport = fc_disc_lport(disc);
  250. struct fc_rport_priv *rdata;
  251. FC_DISC_DBG(disc, "Discovery complete\n");
  252. disc->pending = 0;
  253. if (disc->requested) {
  254. fc_disc_restart(disc);
  255. return;
  256. }
  257. /*
  258. * Go through all remote ports. If they were found in the latest
  259. * discovery, reverify or log them in. Otherwise, log them out.
  260. * Skip ports which were never discovered. These are the dNS port
  261. * and ports which were created by PLOGI.
  262. */
  263. list_for_each_entry_rcu(rdata, &disc->rports, peers) {
  264. if (!rdata->disc_id)
  265. continue;
  266. if (rdata->disc_id == disc->disc_id)
  267. lport->tt.rport_login(rdata);
  268. else
  269. lport->tt.rport_logoff(rdata);
  270. }
  271. mutex_unlock(&disc->disc_mutex);
  272. disc->disc_callback(lport, event);
  273. mutex_lock(&disc->disc_mutex);
  274. }
  275. /**
  276. * fc_disc_error() - Handle error on dNS request
  277. * @disc: The discovery context
  278. * @fp: The error code encoded as a frame pointer
  279. */
  280. static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
  281. {
  282. struct fc_lport *lport = fc_disc_lport(disc);
  283. unsigned long delay = 0;
  284. FC_DISC_DBG(disc, "Error %ld, retries %d/%d\n",
  285. PTR_ERR(fp), disc->retry_count,
  286. FC_DISC_RETRY_LIMIT);
  287. if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
  288. /*
  289. * Memory allocation failure, or the exchange timed out,
  290. * retry after delay.
  291. */
  292. if (disc->retry_count < FC_DISC_RETRY_LIMIT) {
  293. /* go ahead and retry */
  294. if (!fp)
  295. delay = msecs_to_jiffies(FC_DISC_RETRY_DELAY);
  296. else {
  297. delay = msecs_to_jiffies(lport->e_d_tov);
  298. /* timeout faster first time */
  299. if (!disc->retry_count)
  300. delay /= 4;
  301. }
  302. disc->retry_count++;
  303. schedule_delayed_work(&disc->disc_work, delay);
  304. } else
  305. fc_disc_done(disc, DISC_EV_FAILED);
  306. }
  307. }
  308. /**
  309. * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
  310. * @lport: The discovery context
  311. *
  312. * Locking Note: This function expects that the disc_mutex is locked
  313. * before it is called.
  314. */
  315. static void fc_disc_gpn_ft_req(struct fc_disc *disc)
  316. {
  317. struct fc_frame *fp;
  318. struct fc_lport *lport = fc_disc_lport(disc);
  319. WARN_ON(!fc_lport_test_ready(lport));
  320. disc->pending = 1;
  321. disc->requested = 0;
  322. disc->buf_len = 0;
  323. disc->seq_count = 0;
  324. fp = fc_frame_alloc(lport,
  325. sizeof(struct fc_ct_hdr) +
  326. sizeof(struct fc_ns_gid_ft));
  327. if (!fp)
  328. goto err;
  329. if (lport->tt.elsct_send(lport, 0, fp,
  330. FC_NS_GPN_FT,
  331. fc_disc_gpn_ft_resp,
  332. disc, 3 * lport->r_a_tov))
  333. return;
  334. err:
  335. fc_disc_error(disc, NULL);
  336. }
  337. /**
  338. * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response.
  339. * @lport: The local port the GPN_FT was received on
  340. * @buf: The GPN_FT response buffer
  341. * @len: The size of response buffer
  342. *
  343. * Goes through the list of IDs and names resulting from a request.
  344. */
  345. static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
  346. {
  347. struct fc_lport *lport;
  348. struct fc_gpn_ft_resp *np;
  349. char *bp;
  350. size_t plen;
  351. size_t tlen;
  352. int error = 0;
  353. struct fc_rport_identifiers ids;
  354. struct fc_rport_priv *rdata;
  355. lport = fc_disc_lport(disc);
  356. disc->seq_count++;
  357. /*
  358. * Handle partial name record left over from previous call.
  359. */
  360. bp = buf;
  361. plen = len;
  362. np = (struct fc_gpn_ft_resp *)bp;
  363. tlen = disc->buf_len;
  364. disc->buf_len = 0;
  365. if (tlen) {
  366. WARN_ON(tlen >= sizeof(*np));
  367. plen = sizeof(*np) - tlen;
  368. WARN_ON(plen <= 0);
  369. WARN_ON(plen >= sizeof(*np));
  370. if (plen > len)
  371. plen = len;
  372. np = &disc->partial_buf;
  373. memcpy((char *)np + tlen, bp, plen);
  374. /*
  375. * Set bp so that the loop below will advance it to the
  376. * first valid full name element.
  377. */
  378. bp -= tlen;
  379. len += tlen;
  380. plen += tlen;
  381. disc->buf_len = (unsigned char) plen;
  382. if (plen == sizeof(*np))
  383. disc->buf_len = 0;
  384. }
  385. /*
  386. * Handle full name records, including the one filled from above.
  387. * Normally, np == bp and plen == len, but from the partial case above,
  388. * bp, len describe the overall buffer, and np, plen describe the
  389. * partial buffer, which if would usually be full now.
  390. * After the first time through the loop, things return to "normal".
  391. */
  392. while (plen >= sizeof(*np)) {
  393. ids.port_id = ntoh24(np->fp_fid);
  394. ids.port_name = ntohll(np->fp_wwpn);
  395. if (ids.port_id != lport->port_id &&
  396. ids.port_name != lport->wwpn) {
  397. rdata = lport->tt.rport_create(lport, ids.port_id);
  398. if (rdata) {
  399. rdata->ids.port_name = ids.port_name;
  400. rdata->disc_id = disc->disc_id;
  401. } else {
  402. printk(KERN_WARNING "libfc: Failed to allocate "
  403. "memory for the newly discovered port "
  404. "(%6.6x)\n", ids.port_id);
  405. error = -ENOMEM;
  406. }
  407. }
  408. if (np->fp_flags & FC_NS_FID_LAST) {
  409. fc_disc_done(disc, DISC_EV_SUCCESS);
  410. len = 0;
  411. break;
  412. }
  413. len -= sizeof(*np);
  414. bp += sizeof(*np);
  415. np = (struct fc_gpn_ft_resp *)bp;
  416. plen = len;
  417. }
  418. /*
  419. * Save any partial record at the end of the buffer for next time.
  420. */
  421. if (error == 0 && len > 0 && len < sizeof(*np)) {
  422. if (np != &disc->partial_buf) {
  423. FC_DISC_DBG(disc, "Partial buffer remains "
  424. "for discovery\n");
  425. memcpy(&disc->partial_buf, np, len);
  426. }
  427. disc->buf_len = (unsigned char) len;
  428. }
  429. return error;
  430. }
  431. /**
  432. * fc_disc_timeout() - Handler for discovery timeouts
  433. * @work: Structure holding discovery context that needs to retry discovery
  434. */
  435. static void fc_disc_timeout(struct work_struct *work)
  436. {
  437. struct fc_disc *disc = container_of(work,
  438. struct fc_disc,
  439. disc_work.work);
  440. mutex_lock(&disc->disc_mutex);
  441. fc_disc_gpn_ft_req(disc);
  442. mutex_unlock(&disc->disc_mutex);
  443. }
  444. /**
  445. * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
  446. * @sp: The sequence that the GPN_FT response was received on
  447. * @fp: The GPN_FT response frame
  448. * @lp_arg: The discovery context
  449. *
  450. * Locking Note: This function is called without disc mutex held, and
  451. * should do all its processing with the mutex held
  452. */
  453. static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp,
  454. void *disc_arg)
  455. {
  456. struct fc_disc *disc = disc_arg;
  457. struct fc_ct_hdr *cp;
  458. struct fc_frame_header *fh;
  459. enum fc_disc_event event = DISC_EV_NONE;
  460. unsigned int seq_cnt;
  461. unsigned int len;
  462. int error = 0;
  463. mutex_lock(&disc->disc_mutex);
  464. FC_DISC_DBG(disc, "Received a GPN_FT response\n");
  465. if (IS_ERR(fp)) {
  466. fc_disc_error(disc, fp);
  467. mutex_unlock(&disc->disc_mutex);
  468. return;
  469. }
  470. WARN_ON(!fc_frame_is_linear(fp)); /* buffer must be contiguous */
  471. fh = fc_frame_header_get(fp);
  472. len = fr_len(fp) - sizeof(*fh);
  473. seq_cnt = ntohs(fh->fh_seq_cnt);
  474. if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 && disc->seq_count == 0) {
  475. cp = fc_frame_payload_get(fp, sizeof(*cp));
  476. if (!cp) {
  477. FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n",
  478. fr_len(fp));
  479. event = DISC_EV_FAILED;
  480. } else if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
  481. /* Accepted, parse the response. */
  482. len -= sizeof(*cp);
  483. error = fc_disc_gpn_ft_parse(disc, cp + 1, len);
  484. } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
  485. FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x "
  486. "(check zoning)\n", cp->ct_reason,
  487. cp->ct_explan);
  488. event = DISC_EV_FAILED;
  489. if (cp->ct_reason == FC_FS_RJT_UNABL &&
  490. cp->ct_explan == FC_FS_EXP_FTNR)
  491. event = DISC_EV_SUCCESS;
  492. } else {
  493. FC_DISC_DBG(disc, "GPN_FT unexpected response code "
  494. "%x\n", ntohs(cp->ct_cmd));
  495. event = DISC_EV_FAILED;
  496. }
  497. } else if (fr_sof(fp) == FC_SOF_N3 && seq_cnt == disc->seq_count) {
  498. error = fc_disc_gpn_ft_parse(disc, fh + 1, len);
  499. } else {
  500. FC_DISC_DBG(disc, "GPN_FT unexpected frame - out of sequence? "
  501. "seq_cnt %x expected %x sof %x eof %x\n",
  502. seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp));
  503. event = DISC_EV_FAILED;
  504. }
  505. if (error)
  506. fc_disc_error(disc, fp);
  507. else if (event != DISC_EV_NONE)
  508. fc_disc_done(disc, event);
  509. fc_frame_free(fp);
  510. mutex_unlock(&disc->disc_mutex);
  511. }
  512. /**
  513. * fc_disc_gpn_id_resp() - Handle a response frame from Get Port Names (GPN_ID)
  514. * @sp: The sequence the GPN_ID is on
  515. * @fp: The response frame
  516. * @rdata_arg: The remote port that sent the GPN_ID response
  517. *
  518. * Locking Note: This function is called without disc mutex held.
  519. */
  520. static void fc_disc_gpn_id_resp(struct fc_seq *sp, struct fc_frame *fp,
  521. void *rdata_arg)
  522. {
  523. struct fc_rport_priv *rdata = rdata_arg;
  524. struct fc_rport_priv *new_rdata;
  525. struct fc_lport *lport;
  526. struct fc_disc *disc;
  527. struct fc_ct_hdr *cp;
  528. struct fc_ns_gid_pn *pn;
  529. u64 port_name;
  530. lport = rdata->local_port;
  531. disc = &lport->disc;
  532. mutex_lock(&disc->disc_mutex);
  533. if (PTR_ERR(fp) == -FC_EX_CLOSED)
  534. goto out;
  535. if (IS_ERR(fp))
  536. goto redisc;
  537. cp = fc_frame_payload_get(fp, sizeof(*cp));
  538. if (!cp)
  539. goto redisc;
  540. if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
  541. if (fr_len(fp) < sizeof(struct fc_frame_header) +
  542. sizeof(*cp) + sizeof(*pn))
  543. goto redisc;
  544. pn = (struct fc_ns_gid_pn *)(cp + 1);
  545. port_name = get_unaligned_be64(&pn->fn_wwpn);
  546. if (rdata->ids.port_name == -1)
  547. rdata->ids.port_name = port_name;
  548. else if (rdata->ids.port_name != port_name) {
  549. FC_DISC_DBG(disc, "GPN_ID accepted. WWPN changed. "
  550. "Port-id %6.6x wwpn %16.16llx\n",
  551. rdata->ids.port_id, port_name);
  552. lport->tt.rport_logoff(rdata);
  553. new_rdata = lport->tt.rport_create(lport,
  554. rdata->ids.port_id);
  555. if (new_rdata) {
  556. new_rdata->disc_id = disc->disc_id;
  557. lport->tt.rport_login(new_rdata);
  558. }
  559. goto out;
  560. }
  561. rdata->disc_id = disc->disc_id;
  562. lport->tt.rport_login(rdata);
  563. } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
  564. FC_DISC_DBG(disc, "GPN_ID rejected reason %x exp %x\n",
  565. cp->ct_reason, cp->ct_explan);
  566. lport->tt.rport_logoff(rdata);
  567. } else {
  568. FC_DISC_DBG(disc, "GPN_ID unexpected response code %x\n",
  569. ntohs(cp->ct_cmd));
  570. redisc:
  571. fc_disc_restart(disc);
  572. }
  573. out:
  574. mutex_unlock(&disc->disc_mutex);
  575. kref_put(&rdata->kref, lport->tt.rport_destroy);
  576. }
  577. /**
  578. * fc_disc_gpn_id_req() - Send Get Port Names by ID (GPN_ID) request
  579. * @lport: The local port to initiate discovery on
  580. * @rdata: remote port private data
  581. *
  582. * Locking Note: This function expects that the disc_mutex is locked
  583. * before it is called.
  584. * On failure, an error code is returned.
  585. */
  586. static int fc_disc_gpn_id_req(struct fc_lport *lport,
  587. struct fc_rport_priv *rdata)
  588. {
  589. struct fc_frame *fp;
  590. fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
  591. sizeof(struct fc_ns_fid));
  592. if (!fp)
  593. return -ENOMEM;
  594. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, FC_NS_GPN_ID,
  595. fc_disc_gpn_id_resp, rdata,
  596. 3 * lport->r_a_tov))
  597. return -ENOMEM;
  598. kref_get(&rdata->kref);
  599. return 0;
  600. }
  601. /**
  602. * fc_disc_single() - Discover the directory information for a single target
  603. * @lport: The local port the remote port is associated with
  604. * @dp: The port to rediscover
  605. *
  606. * Locking Note: This function expects that the disc_mutex is locked
  607. * before it is called.
  608. */
  609. static int fc_disc_single(struct fc_lport *lport, struct fc_disc_port *dp)
  610. {
  611. struct fc_rport_priv *rdata;
  612. rdata = lport->tt.rport_create(lport, dp->port_id);
  613. if (!rdata)
  614. return -ENOMEM;
  615. rdata->disc_id = 0;
  616. return fc_disc_gpn_id_req(lport, rdata);
  617. }
  618. /**
  619. * fc_disc_stop() - Stop discovery for a given lport
  620. * @lport: The local port that discovery should stop on
  621. */
  622. void fc_disc_stop(struct fc_lport *lport)
  623. {
  624. struct fc_disc *disc = &lport->disc;
  625. if (disc->pending)
  626. cancel_delayed_work_sync(&disc->disc_work);
  627. fc_disc_stop_rports(disc);
  628. }
  629. /**
  630. * fc_disc_stop_final() - Stop discovery for a given lport
  631. * @lport: The lport that discovery should stop on
  632. *
  633. * This function will block until discovery has been
  634. * completely stopped and all rports have been deleted.
  635. */
  636. void fc_disc_stop_final(struct fc_lport *lport)
  637. {
  638. fc_disc_stop(lport);
  639. lport->tt.rport_flush_queue();
  640. }
  641. /**
  642. * fc_disc_init() - Initialize the discovery layer for a local port
  643. * @lport: The local port that needs the discovery layer to be initialized
  644. */
  645. int fc_disc_init(struct fc_lport *lport)
  646. {
  647. struct fc_disc *disc;
  648. if (!lport->tt.disc_start)
  649. lport->tt.disc_start = fc_disc_start;
  650. if (!lport->tt.disc_stop)
  651. lport->tt.disc_stop = fc_disc_stop;
  652. if (!lport->tt.disc_stop_final)
  653. lport->tt.disc_stop_final = fc_disc_stop_final;
  654. if (!lport->tt.disc_recv_req)
  655. lport->tt.disc_recv_req = fc_disc_recv_req;
  656. disc = &lport->disc;
  657. INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
  658. mutex_init(&disc->disc_mutex);
  659. INIT_LIST_HEAD(&disc->rports);
  660. disc->priv = lport;
  661. return 0;
  662. }
  663. EXPORT_SYMBOL(fc_disc_init);