fc_disc.c 20 KB

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