fc_disc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. static 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. } else if (PTR_ERR(fp) == -FC_EX_CLOSED) {
  307. /*
  308. * if discovery fails due to lport reset, clear
  309. * pending flag so that subsequent discovery can
  310. * continue
  311. */
  312. disc->pending = 0;
  313. }
  314. }
  315. /**
  316. * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
  317. * @lport: The discovery context
  318. *
  319. * Locking Note: This function expects that the disc_mutex is locked
  320. * before it is called.
  321. */
  322. static void fc_disc_gpn_ft_req(struct fc_disc *disc)
  323. {
  324. struct fc_frame *fp;
  325. struct fc_lport *lport = fc_disc_lport(disc);
  326. WARN_ON(!fc_lport_test_ready(lport));
  327. disc->pending = 1;
  328. disc->requested = 0;
  329. disc->buf_len = 0;
  330. disc->seq_count = 0;
  331. fp = fc_frame_alloc(lport,
  332. sizeof(struct fc_ct_hdr) +
  333. sizeof(struct fc_ns_gid_ft));
  334. if (!fp)
  335. goto err;
  336. if (lport->tt.elsct_send(lport, 0, fp,
  337. FC_NS_GPN_FT,
  338. fc_disc_gpn_ft_resp,
  339. disc, 3 * lport->r_a_tov))
  340. return;
  341. err:
  342. fc_disc_error(disc, NULL);
  343. }
  344. /**
  345. * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response.
  346. * @lport: The local port the GPN_FT was received on
  347. * @buf: The GPN_FT response buffer
  348. * @len: The size of response buffer
  349. *
  350. * Goes through the list of IDs and names resulting from a request.
  351. */
  352. static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
  353. {
  354. struct fc_lport *lport;
  355. struct fc_gpn_ft_resp *np;
  356. char *bp;
  357. size_t plen;
  358. size_t tlen;
  359. int error = 0;
  360. struct fc_rport_identifiers ids;
  361. struct fc_rport_priv *rdata;
  362. lport = fc_disc_lport(disc);
  363. disc->seq_count++;
  364. /*
  365. * Handle partial name record left over from previous call.
  366. */
  367. bp = buf;
  368. plen = len;
  369. np = (struct fc_gpn_ft_resp *)bp;
  370. tlen = disc->buf_len;
  371. disc->buf_len = 0;
  372. if (tlen) {
  373. WARN_ON(tlen >= sizeof(*np));
  374. plen = sizeof(*np) - tlen;
  375. WARN_ON(plen <= 0);
  376. WARN_ON(plen >= sizeof(*np));
  377. if (plen > len)
  378. plen = len;
  379. np = &disc->partial_buf;
  380. memcpy((char *)np + tlen, bp, plen);
  381. /*
  382. * Set bp so that the loop below will advance it to the
  383. * first valid full name element.
  384. */
  385. bp -= tlen;
  386. len += tlen;
  387. plen += tlen;
  388. disc->buf_len = (unsigned char) plen;
  389. if (plen == sizeof(*np))
  390. disc->buf_len = 0;
  391. }
  392. /*
  393. * Handle full name records, including the one filled from above.
  394. * Normally, np == bp and plen == len, but from the partial case above,
  395. * bp, len describe the overall buffer, and np, plen describe the
  396. * partial buffer, which if would usually be full now.
  397. * After the first time through the loop, things return to "normal".
  398. */
  399. while (plen >= sizeof(*np)) {
  400. ids.port_id = ntoh24(np->fp_fid);
  401. ids.port_name = ntohll(np->fp_wwpn);
  402. if (ids.port_id != lport->port_id &&
  403. ids.port_name != lport->wwpn) {
  404. rdata = lport->tt.rport_create(lport, ids.port_id);
  405. if (rdata) {
  406. rdata->ids.port_name = ids.port_name;
  407. rdata->disc_id = disc->disc_id;
  408. } else {
  409. printk(KERN_WARNING "libfc: Failed to allocate "
  410. "memory for the newly discovered port "
  411. "(%6.6x)\n", ids.port_id);
  412. error = -ENOMEM;
  413. }
  414. }
  415. if (np->fp_flags & FC_NS_FID_LAST) {
  416. fc_disc_done(disc, DISC_EV_SUCCESS);
  417. len = 0;
  418. break;
  419. }
  420. len -= sizeof(*np);
  421. bp += sizeof(*np);
  422. np = (struct fc_gpn_ft_resp *)bp;
  423. plen = len;
  424. }
  425. /*
  426. * Save any partial record at the end of the buffer for next time.
  427. */
  428. if (error == 0 && len > 0 && len < sizeof(*np)) {
  429. if (np != &disc->partial_buf) {
  430. FC_DISC_DBG(disc, "Partial buffer remains "
  431. "for discovery\n");
  432. memcpy(&disc->partial_buf, np, len);
  433. }
  434. disc->buf_len = (unsigned char) len;
  435. }
  436. return error;
  437. }
  438. /**
  439. * fc_disc_timeout() - Handler for discovery timeouts
  440. * @work: Structure holding discovery context that needs to retry discovery
  441. */
  442. static void fc_disc_timeout(struct work_struct *work)
  443. {
  444. struct fc_disc *disc = container_of(work,
  445. struct fc_disc,
  446. disc_work.work);
  447. mutex_lock(&disc->disc_mutex);
  448. fc_disc_gpn_ft_req(disc);
  449. mutex_unlock(&disc->disc_mutex);
  450. }
  451. /**
  452. * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
  453. * @sp: The sequence that the GPN_FT response was received on
  454. * @fp: The GPN_FT response frame
  455. * @lp_arg: The discovery context
  456. *
  457. * Locking Note: This function is called without disc mutex held, and
  458. * should do all its processing with the mutex held
  459. */
  460. static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp,
  461. void *disc_arg)
  462. {
  463. struct fc_disc *disc = disc_arg;
  464. struct fc_ct_hdr *cp;
  465. struct fc_frame_header *fh;
  466. enum fc_disc_event event = DISC_EV_NONE;
  467. unsigned int seq_cnt;
  468. unsigned int len;
  469. int error = 0;
  470. mutex_lock(&disc->disc_mutex);
  471. FC_DISC_DBG(disc, "Received a GPN_FT response\n");
  472. if (IS_ERR(fp)) {
  473. fc_disc_error(disc, fp);
  474. mutex_unlock(&disc->disc_mutex);
  475. return;
  476. }
  477. WARN_ON(!fc_frame_is_linear(fp)); /* buffer must be contiguous */
  478. fh = fc_frame_header_get(fp);
  479. len = fr_len(fp) - sizeof(*fh);
  480. seq_cnt = ntohs(fh->fh_seq_cnt);
  481. if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 && disc->seq_count == 0) {
  482. cp = fc_frame_payload_get(fp, sizeof(*cp));
  483. if (!cp) {
  484. FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n",
  485. fr_len(fp));
  486. event = DISC_EV_FAILED;
  487. } else if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
  488. /* Accepted, parse the response. */
  489. len -= sizeof(*cp);
  490. error = fc_disc_gpn_ft_parse(disc, cp + 1, len);
  491. } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
  492. FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x "
  493. "(check zoning)\n", cp->ct_reason,
  494. cp->ct_explan);
  495. event = DISC_EV_FAILED;
  496. if (cp->ct_reason == FC_FS_RJT_UNABL &&
  497. cp->ct_explan == FC_FS_EXP_FTNR)
  498. event = DISC_EV_SUCCESS;
  499. } else {
  500. FC_DISC_DBG(disc, "GPN_FT unexpected response code "
  501. "%x\n", ntohs(cp->ct_cmd));
  502. event = DISC_EV_FAILED;
  503. }
  504. } else if (fr_sof(fp) == FC_SOF_N3 && seq_cnt == disc->seq_count) {
  505. error = fc_disc_gpn_ft_parse(disc, fh + 1, len);
  506. } else {
  507. FC_DISC_DBG(disc, "GPN_FT unexpected frame - out of sequence? "
  508. "seq_cnt %x expected %x sof %x eof %x\n",
  509. seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp));
  510. event = DISC_EV_FAILED;
  511. }
  512. if (error)
  513. fc_disc_error(disc, fp);
  514. else if (event != DISC_EV_NONE)
  515. fc_disc_done(disc, event);
  516. fc_frame_free(fp);
  517. mutex_unlock(&disc->disc_mutex);
  518. }
  519. /**
  520. * fc_disc_gpn_id_resp() - Handle a response frame from Get Port Names (GPN_ID)
  521. * @sp: The sequence the GPN_ID is on
  522. * @fp: The response frame
  523. * @rdata_arg: The remote port that sent the GPN_ID response
  524. *
  525. * Locking Note: This function is called without disc mutex held.
  526. */
  527. static void fc_disc_gpn_id_resp(struct fc_seq *sp, struct fc_frame *fp,
  528. void *rdata_arg)
  529. {
  530. struct fc_rport_priv *rdata = rdata_arg;
  531. struct fc_rport_priv *new_rdata;
  532. struct fc_lport *lport;
  533. struct fc_disc *disc;
  534. struct fc_ct_hdr *cp;
  535. struct fc_ns_gid_pn *pn;
  536. u64 port_name;
  537. lport = rdata->local_port;
  538. disc = &lport->disc;
  539. mutex_lock(&disc->disc_mutex);
  540. if (PTR_ERR(fp) == -FC_EX_CLOSED)
  541. goto out;
  542. if (IS_ERR(fp))
  543. goto redisc;
  544. cp = fc_frame_payload_get(fp, sizeof(*cp));
  545. if (!cp)
  546. goto redisc;
  547. if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
  548. if (fr_len(fp) < sizeof(struct fc_frame_header) +
  549. sizeof(*cp) + sizeof(*pn))
  550. goto redisc;
  551. pn = (struct fc_ns_gid_pn *)(cp + 1);
  552. port_name = get_unaligned_be64(&pn->fn_wwpn);
  553. if (rdata->ids.port_name == -1)
  554. rdata->ids.port_name = port_name;
  555. else if (rdata->ids.port_name != port_name) {
  556. FC_DISC_DBG(disc, "GPN_ID accepted. WWPN changed. "
  557. "Port-id %6.6x wwpn %16.16llx\n",
  558. rdata->ids.port_id, port_name);
  559. lport->tt.rport_logoff(rdata);
  560. new_rdata = lport->tt.rport_create(lport,
  561. rdata->ids.port_id);
  562. if (new_rdata) {
  563. new_rdata->disc_id = disc->disc_id;
  564. lport->tt.rport_login(new_rdata);
  565. }
  566. goto out;
  567. }
  568. rdata->disc_id = disc->disc_id;
  569. lport->tt.rport_login(rdata);
  570. } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
  571. FC_DISC_DBG(disc, "GPN_ID rejected reason %x exp %x\n",
  572. cp->ct_reason, cp->ct_explan);
  573. lport->tt.rport_logoff(rdata);
  574. } else {
  575. FC_DISC_DBG(disc, "GPN_ID unexpected response code %x\n",
  576. ntohs(cp->ct_cmd));
  577. redisc:
  578. fc_disc_restart(disc);
  579. }
  580. out:
  581. mutex_unlock(&disc->disc_mutex);
  582. kref_put(&rdata->kref, lport->tt.rport_destroy);
  583. }
  584. /**
  585. * fc_disc_gpn_id_req() - Send Get Port Names by ID (GPN_ID) request
  586. * @lport: The local port to initiate discovery on
  587. * @rdata: remote port private data
  588. *
  589. * Locking Note: This function expects that the disc_mutex is locked
  590. * before it is called.
  591. * On failure, an error code is returned.
  592. */
  593. static int fc_disc_gpn_id_req(struct fc_lport *lport,
  594. struct fc_rport_priv *rdata)
  595. {
  596. struct fc_frame *fp;
  597. fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
  598. sizeof(struct fc_ns_fid));
  599. if (!fp)
  600. return -ENOMEM;
  601. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, FC_NS_GPN_ID,
  602. fc_disc_gpn_id_resp, rdata,
  603. 3 * lport->r_a_tov))
  604. return -ENOMEM;
  605. kref_get(&rdata->kref);
  606. return 0;
  607. }
  608. /**
  609. * fc_disc_single() - Discover the directory information for a single target
  610. * @lport: The local port the remote port is associated with
  611. * @dp: The port to rediscover
  612. *
  613. * Locking Note: This function expects that the disc_mutex is locked
  614. * before it is called.
  615. */
  616. static int fc_disc_single(struct fc_lport *lport, struct fc_disc_port *dp)
  617. {
  618. struct fc_rport_priv *rdata;
  619. rdata = lport->tt.rport_create(lport, dp->port_id);
  620. if (!rdata)
  621. return -ENOMEM;
  622. rdata->disc_id = 0;
  623. return fc_disc_gpn_id_req(lport, rdata);
  624. }
  625. /**
  626. * fc_disc_stop() - Stop discovery for a given lport
  627. * @lport: The local port that discovery should stop on
  628. */
  629. static void fc_disc_stop(struct fc_lport *lport)
  630. {
  631. struct fc_disc *disc = &lport->disc;
  632. if (disc->pending)
  633. cancel_delayed_work_sync(&disc->disc_work);
  634. fc_disc_stop_rports(disc);
  635. }
  636. /**
  637. * fc_disc_stop_final() - Stop discovery for a given lport
  638. * @lport: The lport that discovery should stop on
  639. *
  640. * This function will block until discovery has been
  641. * completely stopped and all rports have been deleted.
  642. */
  643. static void fc_disc_stop_final(struct fc_lport *lport)
  644. {
  645. fc_disc_stop(lport);
  646. lport->tt.rport_flush_queue();
  647. }
  648. /**
  649. * fc_disc_config() - Configure the discovery layer for a local port
  650. * @lport: The local port that needs the discovery layer to be configured
  651. * @priv: Private data structre for users of the discovery layer
  652. */
  653. void fc_disc_config(struct fc_lport *lport, void *priv)
  654. {
  655. struct fc_disc *disc = &lport->disc;
  656. if (!lport->tt.disc_start)
  657. lport->tt.disc_start = fc_disc_start;
  658. if (!lport->tt.disc_stop)
  659. lport->tt.disc_stop = fc_disc_stop;
  660. if (!lport->tt.disc_stop_final)
  661. lport->tt.disc_stop_final = fc_disc_stop_final;
  662. if (!lport->tt.disc_recv_req)
  663. lport->tt.disc_recv_req = fc_disc_recv_req;
  664. disc = &lport->disc;
  665. disc->priv = priv;
  666. }
  667. EXPORT_SYMBOL(fc_disc_config);
  668. /**
  669. * fc_disc_init() - Initialize the discovery layer for a local port
  670. * @lport: The local port that needs the discovery layer to be initialized
  671. */
  672. void fc_disc_init(struct fc_lport *lport)
  673. {
  674. struct fc_disc *disc = &lport->disc;
  675. INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
  676. mutex_init(&disc->disc_mutex);
  677. INIT_LIST_HEAD(&disc->rports);
  678. }
  679. EXPORT_SYMBOL(fc_disc_init);