fc_disc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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/err.h>
  34. #include <asm/unaligned.h>
  35. #include <scsi/fc/fc_gs.h>
  36. #include <scsi/libfc.h>
  37. #include "fc_libfc.h"
  38. #define FC_DISC_RETRY_LIMIT 3 /* max retries */
  39. #define FC_DISC_RETRY_DELAY 500UL /* (msecs) delay */
  40. static void fc_disc_gpn_ft_req(struct fc_disc *);
  41. static void fc_disc_gpn_ft_resp(struct fc_seq *, struct fc_frame *, void *);
  42. static void fc_disc_done(struct fc_disc *, enum fc_disc_event);
  43. static void fc_disc_timeout(struct work_struct *);
  44. static int fc_disc_single(struct fc_lport *, struct fc_disc_port *);
  45. static void fc_disc_restart(struct fc_disc *);
  46. /**
  47. * fc_disc_stop_rports() - Delete all the remote ports associated with the lport
  48. * @disc: The discovery job to stop remote ports on
  49. *
  50. * Locking Note: This function expects that the lport mutex is locked before
  51. * calling it.
  52. */
  53. void fc_disc_stop_rports(struct fc_disc *disc)
  54. {
  55. struct fc_lport *lport;
  56. struct fc_rport_priv *rdata, *next;
  57. lport = disc->lport;
  58. mutex_lock(&disc->disc_mutex);
  59. list_for_each_entry_safe(rdata, next, &disc->rports, peers)
  60. lport->tt.rport_logoff(rdata);
  61. mutex_unlock(&disc->disc_mutex);
  62. }
  63. /**
  64. * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN)
  65. * @sp: The sequence of the RSCN exchange
  66. * @fp: The RSCN frame
  67. * @lport: The local port that the request will be sent on
  68. *
  69. * Locking Note: This function expects that the disc_mutex is locked
  70. * before it is called.
  71. */
  72. static void fc_disc_recv_rscn_req(struct fc_seq *sp, struct fc_frame *fp,
  73. struct fc_disc *disc)
  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 = disc->lport;
  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. "(%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(sp, 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.fp = NULL;
  163. rjt_data.reason = ELS_RJT_LOGIC;
  164. rjt_data.explan = ELS_EXPL_NONE;
  165. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  166. fc_frame_free(fp);
  167. }
  168. /**
  169. * fc_disc_recv_req() - Handle incoming requests
  170. * @sp: The sequence of the request exchange
  171. * @fp: The request frame
  172. * @lport: The local port receiving the request
  173. *
  174. * Locking Note: This function is called from the EM and will lock
  175. * the disc_mutex before calling the handler for the
  176. * request.
  177. */
  178. static void fc_disc_recv_req(struct fc_seq *sp, struct fc_frame *fp,
  179. struct fc_lport *lport)
  180. {
  181. u8 op;
  182. struct fc_disc *disc = &lport->disc;
  183. op = fc_frame_payload_op(fp);
  184. switch (op) {
  185. case ELS_RSCN:
  186. mutex_lock(&disc->disc_mutex);
  187. fc_disc_recv_rscn_req(sp, fp, disc);
  188. mutex_unlock(&disc->disc_mutex);
  189. break;
  190. default:
  191. FC_DISC_DBG(disc, "Received an unsupported request, "
  192. "the opcode is (%x)\n", op);
  193. break;
  194. }
  195. }
  196. /**
  197. * fc_disc_restart() - Restart discovery
  198. * @disc: The discovery object to be restarted
  199. *
  200. * Locking Note: This function expects that the disc mutex
  201. * is already locked.
  202. */
  203. static void fc_disc_restart(struct fc_disc *disc)
  204. {
  205. if (!disc->disc_callback)
  206. return;
  207. FC_DISC_DBG(disc, "Restarting discovery\n");
  208. disc->requested = 1;
  209. if (disc->pending)
  210. return;
  211. /*
  212. * Advance disc_id. This is an arbitrary non-zero number that will
  213. * match the value in the fc_rport_priv after discovery for all
  214. * freshly-discovered remote ports. Avoid wrapping to zero.
  215. */
  216. disc->disc_id = (disc->disc_id + 2) | 1;
  217. disc->retry_count = 0;
  218. fc_disc_gpn_ft_req(disc);
  219. }
  220. /**
  221. * fc_disc_start() - Start discovery on a local port
  222. * @lport: The local port to have discovery started on
  223. * @disc_callback: Callback function to be called when discovery is complete
  224. */
  225. static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
  226. enum fc_disc_event),
  227. struct fc_lport *lport)
  228. {
  229. struct fc_disc *disc = &lport->disc;
  230. /*
  231. * At this point we may have a new disc job or an existing
  232. * one. Either way, let's lock when we make changes to it
  233. * and send the GPN_FT request.
  234. */
  235. mutex_lock(&disc->disc_mutex);
  236. disc->disc_callback = disc_callback;
  237. fc_disc_restart(disc);
  238. mutex_unlock(&disc->disc_mutex);
  239. }
  240. /**
  241. * fc_disc_done() - Discovery has been completed
  242. * @disc: The discovery context
  243. * @event: The discovery completion status
  244. *
  245. * Locking Note: This function expects that the disc mutex is locked before
  246. * it is called. The discovery callback is then made with the lock released,
  247. * and the lock is re-taken before returning from this function
  248. */
  249. static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event)
  250. {
  251. struct fc_lport *lport = disc->lport;
  252. struct fc_rport_priv *rdata;
  253. FC_DISC_DBG(disc, "Discovery complete\n");
  254. disc->pending = 0;
  255. if (disc->requested) {
  256. fc_disc_restart(disc);
  257. return;
  258. }
  259. /*
  260. * Go through all remote ports. If they were found in the latest
  261. * discovery, reverify or log them in. Otherwise, log them out.
  262. * Skip ports which were never discovered. These are the dNS port
  263. * and ports which were created by PLOGI.
  264. */
  265. list_for_each_entry(rdata, &disc->rports, peers) {
  266. if (!rdata->disc_id)
  267. continue;
  268. if (rdata->disc_id == disc->disc_id)
  269. lport->tt.rport_login(rdata);
  270. else
  271. lport->tt.rport_logoff(rdata);
  272. }
  273. mutex_unlock(&disc->disc_mutex);
  274. disc->disc_callback(lport, event);
  275. mutex_lock(&disc->disc_mutex);
  276. }
  277. /**
  278. * fc_disc_error() - Handle error on dNS request
  279. * @disc: The discovery context
  280. * @fp: The error code encoded as a frame pointer
  281. */
  282. static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
  283. {
  284. struct fc_lport *lport = disc->lport;
  285. unsigned long delay = 0;
  286. FC_DISC_DBG(disc, "Error %ld, retries %d/%d\n",
  287. PTR_ERR(fp), disc->retry_count,
  288. FC_DISC_RETRY_LIMIT);
  289. if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
  290. /*
  291. * Memory allocation failure, or the exchange timed out,
  292. * retry after delay.
  293. */
  294. if (disc->retry_count < FC_DISC_RETRY_LIMIT) {
  295. /* go ahead and retry */
  296. if (!fp)
  297. delay = msecs_to_jiffies(FC_DISC_RETRY_DELAY);
  298. else {
  299. delay = msecs_to_jiffies(lport->e_d_tov);
  300. /* timeout faster first time */
  301. if (!disc->retry_count)
  302. delay /= 4;
  303. }
  304. disc->retry_count++;
  305. schedule_delayed_work(&disc->disc_work, delay);
  306. } else
  307. fc_disc_done(disc, DISC_EV_FAILED);
  308. }
  309. }
  310. /**
  311. * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
  312. * @lport: The discovery context
  313. *
  314. * Locking Note: This function expects that the disc_mutex is locked
  315. * before it is called.
  316. */
  317. static void fc_disc_gpn_ft_req(struct fc_disc *disc)
  318. {
  319. struct fc_frame *fp;
  320. struct fc_lport *lport = disc->lport;
  321. WARN_ON(!fc_lport_test_ready(lport));
  322. disc->pending = 1;
  323. disc->requested = 0;
  324. disc->buf_len = 0;
  325. disc->seq_count = 0;
  326. fp = fc_frame_alloc(lport,
  327. sizeof(struct fc_ct_hdr) +
  328. sizeof(struct fc_ns_gid_ft));
  329. if (!fp)
  330. goto err;
  331. if (lport->tt.elsct_send(lport, 0, fp,
  332. FC_NS_GPN_FT,
  333. fc_disc_gpn_ft_resp,
  334. disc, 3 * lport->r_a_tov))
  335. return;
  336. err:
  337. fc_disc_error(disc, NULL);
  338. }
  339. /**
  340. * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response.
  341. * @lport: The local port the GPN_FT was received on
  342. * @buf: The GPN_FT response buffer
  343. * @len: The size of response buffer
  344. *
  345. * Goes through the list of IDs and names resulting from a request.
  346. */
  347. static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
  348. {
  349. struct fc_lport *lport;
  350. struct fc_gpn_ft_resp *np;
  351. char *bp;
  352. size_t plen;
  353. size_t tlen;
  354. int error = 0;
  355. struct fc_rport_identifiers ids;
  356. struct fc_rport_priv *rdata;
  357. lport = disc->lport;
  358. disc->seq_count++;
  359. /*
  360. * Handle partial name record left over from previous call.
  361. */
  362. bp = buf;
  363. plen = len;
  364. np = (struct fc_gpn_ft_resp *)bp;
  365. tlen = disc->buf_len;
  366. disc->buf_len = 0;
  367. if (tlen) {
  368. WARN_ON(tlen >= sizeof(*np));
  369. plen = sizeof(*np) - tlen;
  370. WARN_ON(plen <= 0);
  371. WARN_ON(plen >= sizeof(*np));
  372. if (plen > len)
  373. plen = len;
  374. np = &disc->partial_buf;
  375. memcpy((char *)np + tlen, bp, plen);
  376. /*
  377. * Set bp so that the loop below will advance it to the
  378. * first valid full name element.
  379. */
  380. bp -= tlen;
  381. len += tlen;
  382. plen += tlen;
  383. disc->buf_len = (unsigned char) plen;
  384. if (plen == sizeof(*np))
  385. disc->buf_len = 0;
  386. }
  387. /*
  388. * Handle full name records, including the one filled from above.
  389. * Normally, np == bp and plen == len, but from the partial case above,
  390. * bp, len describe the overall buffer, and np, plen describe the
  391. * partial buffer, which if would usually be full now.
  392. * After the first time through the loop, things return to "normal".
  393. */
  394. while (plen >= sizeof(*np)) {
  395. ids.port_id = ntoh24(np->fp_fid);
  396. ids.port_name = ntohll(np->fp_wwpn);
  397. if (ids.port_id != fc_host_port_id(lport->host) &&
  398. ids.port_name != lport->wwpn) {
  399. rdata = lport->tt.rport_create(lport, ids.port_id);
  400. if (rdata) {
  401. rdata->ids.port_name = ids.port_name;
  402. rdata->disc_id = disc->disc_id;
  403. } else {
  404. printk(KERN_WARNING "libfc: Failed to allocate "
  405. "memory for the newly discovered port "
  406. "(%6x)\n", ids.port_id);
  407. error = -ENOMEM;
  408. }
  409. }
  410. if (np->fp_flags & FC_NS_FID_LAST) {
  411. fc_disc_done(disc, DISC_EV_SUCCESS);
  412. len = 0;
  413. break;
  414. }
  415. len -= sizeof(*np);
  416. bp += sizeof(*np);
  417. np = (struct fc_gpn_ft_resp *)bp;
  418. plen = len;
  419. }
  420. /*
  421. * Save any partial record at the end of the buffer for next time.
  422. */
  423. if (error == 0 && len > 0 && len < sizeof(*np)) {
  424. if (np != &disc->partial_buf) {
  425. FC_DISC_DBG(disc, "Partial buffer remains "
  426. "for discovery\n");
  427. memcpy(&disc->partial_buf, np, len);
  428. }
  429. disc->buf_len = (unsigned char) len;
  430. }
  431. return error;
  432. }
  433. /**
  434. * fc_disc_timeout() - Handler for discovery timeouts
  435. * @work: Structure holding discovery context that needs to retry discovery
  436. */
  437. static void fc_disc_timeout(struct work_struct *work)
  438. {
  439. struct fc_disc *disc = container_of(work,
  440. struct fc_disc,
  441. disc_work.work);
  442. mutex_lock(&disc->disc_mutex);
  443. fc_disc_gpn_ft_req(disc);
  444. mutex_unlock(&disc->disc_mutex);
  445. }
  446. /**
  447. * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
  448. * @sp: The sequence that the GPN_FT response was received on
  449. * @fp: The GPN_FT response frame
  450. * @lp_arg: The discovery context
  451. *
  452. * Locking Note: This function is called without disc mutex held, and
  453. * should do all its processing with the mutex held
  454. */
  455. static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp,
  456. void *disc_arg)
  457. {
  458. struct fc_disc *disc = disc_arg;
  459. struct fc_ct_hdr *cp;
  460. struct fc_frame_header *fh;
  461. enum fc_disc_event event = DISC_EV_NONE;
  462. unsigned int seq_cnt;
  463. unsigned int len;
  464. int error = 0;
  465. mutex_lock(&disc->disc_mutex);
  466. FC_DISC_DBG(disc, "Received a GPN_FT response\n");
  467. if (IS_ERR(fp)) {
  468. fc_disc_error(disc, fp);
  469. mutex_unlock(&disc->disc_mutex);
  470. return;
  471. }
  472. WARN_ON(!fc_frame_is_linear(fp)); /* buffer must be contiguous */
  473. fh = fc_frame_header_get(fp);
  474. len = fr_len(fp) - sizeof(*fh);
  475. seq_cnt = ntohs(fh->fh_seq_cnt);
  476. if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 && disc->seq_count == 0) {
  477. cp = fc_frame_payload_get(fp, sizeof(*cp));
  478. if (!cp) {
  479. FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n",
  480. fr_len(fp));
  481. event = DISC_EV_FAILED;
  482. } else if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
  483. /* Accepted, parse the response. */
  484. len -= sizeof(*cp);
  485. error = fc_disc_gpn_ft_parse(disc, cp + 1, len);
  486. } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
  487. FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x "
  488. "(check zoning)\n", cp->ct_reason,
  489. cp->ct_explan);
  490. event = DISC_EV_FAILED;
  491. if (cp->ct_reason == FC_FS_RJT_UNABL &&
  492. cp->ct_explan == FC_FS_EXP_FTNR)
  493. event = DISC_EV_SUCCESS;
  494. } else {
  495. FC_DISC_DBG(disc, "GPN_FT unexpected response code "
  496. "%x\n", ntohs(cp->ct_cmd));
  497. event = DISC_EV_FAILED;
  498. }
  499. } else if (fr_sof(fp) == FC_SOF_N3 && seq_cnt == disc->seq_count) {
  500. error = fc_disc_gpn_ft_parse(disc, fh + 1, len);
  501. } else {
  502. FC_DISC_DBG(disc, "GPN_FT unexpected frame - out of sequence? "
  503. "seq_cnt %x expected %x sof %x eof %x\n",
  504. seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp));
  505. event = DISC_EV_FAILED;
  506. }
  507. if (error)
  508. fc_disc_error(disc, fp);
  509. else if (event != DISC_EV_NONE)
  510. fc_disc_done(disc, event);
  511. fc_frame_free(fp);
  512. mutex_unlock(&disc->disc_mutex);
  513. }
  514. /**
  515. * fc_disc_gpn_id_resp() - Handle a response frame from Get Port Names (GPN_ID)
  516. * @sp: The sequence the GPN_ID is on
  517. * @fp: The response frame
  518. * @rdata_arg: The remote port that sent the GPN_ID response
  519. *
  520. * Locking Note: This function is called without disc mutex held.
  521. */
  522. static void fc_disc_gpn_id_resp(struct fc_seq *sp, struct fc_frame *fp,
  523. void *rdata_arg)
  524. {
  525. struct fc_rport_priv *rdata = rdata_arg;
  526. struct fc_rport_priv *new_rdata;
  527. struct fc_lport *lport;
  528. struct fc_disc *disc;
  529. struct fc_ct_hdr *cp;
  530. struct fc_ns_gid_pn *pn;
  531. u64 port_name;
  532. lport = rdata->local_port;
  533. disc = &lport->disc;
  534. mutex_lock(&disc->disc_mutex);
  535. if (PTR_ERR(fp) == -FC_EX_CLOSED)
  536. goto out;
  537. if (IS_ERR(fp))
  538. goto redisc;
  539. cp = fc_frame_payload_get(fp, sizeof(*cp));
  540. if (!cp)
  541. goto redisc;
  542. if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
  543. if (fr_len(fp) < sizeof(struct fc_frame_header) +
  544. sizeof(*cp) + sizeof(*pn))
  545. goto redisc;
  546. pn = (struct fc_ns_gid_pn *)(cp + 1);
  547. port_name = get_unaligned_be64(&pn->fn_wwpn);
  548. if (rdata->ids.port_name == -1)
  549. rdata->ids.port_name = port_name;
  550. else if (rdata->ids.port_name != port_name) {
  551. FC_DISC_DBG(disc, "GPN_ID accepted. WWPN changed. "
  552. "Port-id %x wwpn %llx\n",
  553. rdata->ids.port_id, port_name);
  554. lport->tt.rport_logoff(rdata);
  555. new_rdata = lport->tt.rport_create(lport,
  556. rdata->ids.port_id);
  557. if (new_rdata) {
  558. new_rdata->disc_id = disc->disc_id;
  559. lport->tt.rport_login(new_rdata);
  560. }
  561. goto out;
  562. }
  563. rdata->disc_id = disc->disc_id;
  564. lport->tt.rport_login(rdata);
  565. } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
  566. FC_DISC_DBG(disc, "GPN_ID rejected reason %x exp %x\n",
  567. cp->ct_reason, cp->ct_explan);
  568. lport->tt.rport_logoff(rdata);
  569. } else {
  570. FC_DISC_DBG(disc, "GPN_ID unexpected response code %x\n",
  571. ntohs(cp->ct_cmd));
  572. redisc:
  573. fc_disc_restart(disc);
  574. }
  575. out:
  576. mutex_unlock(&disc->disc_mutex);
  577. kref_put(&rdata->kref, lport->tt.rport_destroy);
  578. }
  579. /**
  580. * fc_disc_gpn_id_req() - Send Get Port Names by ID (GPN_ID) request
  581. * @lport: The local port to initiate discovery on
  582. * @rdata: remote port private data
  583. *
  584. * Locking Note: This function expects that the disc_mutex is locked
  585. * before it is called.
  586. * On failure, an error code is returned.
  587. */
  588. static int fc_disc_gpn_id_req(struct fc_lport *lport,
  589. struct fc_rport_priv *rdata)
  590. {
  591. struct fc_frame *fp;
  592. fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
  593. sizeof(struct fc_ns_fid));
  594. if (!fp)
  595. return -ENOMEM;
  596. if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, FC_NS_GPN_ID,
  597. fc_disc_gpn_id_resp, rdata,
  598. 3 * lport->r_a_tov))
  599. return -ENOMEM;
  600. kref_get(&rdata->kref);
  601. return 0;
  602. }
  603. /**
  604. * fc_disc_single() - Discover the directory information for a single target
  605. * @lport: The local port the remote port is associated with
  606. * @dp: The port to rediscover
  607. *
  608. * Locking Note: This function expects that the disc_mutex is locked
  609. * before it is called.
  610. */
  611. static int fc_disc_single(struct fc_lport *lport, struct fc_disc_port *dp)
  612. {
  613. struct fc_rport_priv *rdata;
  614. rdata = lport->tt.rport_create(lport, dp->port_id);
  615. if (!rdata)
  616. return -ENOMEM;
  617. rdata->disc_id = 0;
  618. return fc_disc_gpn_id_req(lport, rdata);
  619. }
  620. /**
  621. * fc_disc_stop() - Stop discovery for a given lport
  622. * @lport: The local port that discovery should stop on
  623. */
  624. void fc_disc_stop(struct fc_lport *lport)
  625. {
  626. struct fc_disc *disc = &lport->disc;
  627. if (disc) {
  628. cancel_delayed_work_sync(&disc->disc_work);
  629. fc_disc_stop_rports(disc);
  630. }
  631. }
  632. /**
  633. * fc_disc_stop_final() - Stop discovery for a given lport
  634. * @lport: The lport that discovery should stop on
  635. *
  636. * This function will block until discovery has been
  637. * completely stopped and all rports have been deleted.
  638. */
  639. void fc_disc_stop_final(struct fc_lport *lport)
  640. {
  641. fc_disc_stop(lport);
  642. lport->tt.rport_flush_queue();
  643. }
  644. /**
  645. * fc_disc_init() - Initialize the discovery layer for a local port
  646. * @lport: The local port that needs the discovery layer to be initialized
  647. */
  648. int fc_disc_init(struct fc_lport *lport)
  649. {
  650. struct fc_disc *disc;
  651. if (!lport->tt.disc_start)
  652. lport->tt.disc_start = fc_disc_start;
  653. if (!lport->tt.disc_stop)
  654. lport->tt.disc_stop = fc_disc_stop;
  655. if (!lport->tt.disc_stop_final)
  656. lport->tt.disc_stop_final = fc_disc_stop_final;
  657. if (!lport->tt.disc_recv_req)
  658. lport->tt.disc_recv_req = fc_disc_recv_req;
  659. disc = &lport->disc;
  660. INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
  661. mutex_init(&disc->disc_mutex);
  662. INIT_LIST_HEAD(&disc->rports);
  663. disc->lport = lport;
  664. return 0;
  665. }
  666. EXPORT_SYMBOL(fc_disc_init);