fc_disc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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. #define FC_DISC_RETRY_LIMIT 3 /* max retries */
  38. #define FC_DISC_RETRY_DELAY 500UL /* (msecs) delay */
  39. #define FC_DISC_DELAY 3
  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 int fc_disc_new_target(struct fc_disc *, struct fc_rport_priv *,
  43. struct fc_rport_identifiers *);
  44. static void fc_disc_done(struct fc_disc *, enum fc_disc_event);
  45. static void fc_disc_timeout(struct work_struct *);
  46. static void fc_disc_single(struct fc_disc *, struct fc_disc_port *);
  47. static void fc_disc_restart(struct fc_disc *);
  48. /**
  49. * fc_disc_lookup_rport() - lookup a remote port by port_id
  50. * @lport: Fibre Channel host port instance
  51. * @port_id: remote port port_id to match
  52. */
  53. struct fc_rport_priv *fc_disc_lookup_rport(const struct fc_lport *lport,
  54. u32 port_id)
  55. {
  56. const struct fc_disc *disc = &lport->disc;
  57. struct fc_rport_priv *rdata;
  58. list_for_each_entry(rdata, &disc->rports, peers) {
  59. if (rdata->ids.port_id == port_id &&
  60. rdata->rp_state != RPORT_ST_DELETE)
  61. return rdata;
  62. }
  63. return NULL;
  64. }
  65. /**
  66. * fc_disc_stop_rports() - delete all the remote ports associated with the lport
  67. * @disc: The discovery job to stop rports on
  68. *
  69. * Locking Note: This function expects that the lport mutex is locked before
  70. * calling it.
  71. */
  72. void fc_disc_stop_rports(struct fc_disc *disc)
  73. {
  74. struct fc_lport *lport;
  75. struct fc_rport_priv *rdata, *next;
  76. lport = disc->lport;
  77. mutex_lock(&disc->disc_mutex);
  78. list_for_each_entry_safe(rdata, next, &disc->rports, peers)
  79. lport->tt.rport_logoff(rdata);
  80. mutex_unlock(&disc->disc_mutex);
  81. }
  82. /**
  83. * fc_disc_rport_callback() - Event handler for rport events
  84. * @lport: The lport which is receiving the event
  85. * @rdata: private remote port data
  86. * @event: The event that occured
  87. *
  88. * Locking Note: The rport lock should not be held when calling
  89. * this function.
  90. */
  91. static void fc_disc_rport_callback(struct fc_lport *lport,
  92. struct fc_rport_priv *rdata,
  93. enum fc_rport_event event)
  94. {
  95. struct fc_disc *disc = &lport->disc;
  96. FC_DISC_DBG(disc, "Received a %d event for port (%6x)\n", event,
  97. rdata->ids.port_id);
  98. switch (event) {
  99. case RPORT_EV_READY:
  100. break;
  101. case RPORT_EV_LOGO:
  102. case RPORT_EV_FAILED:
  103. case RPORT_EV_STOP:
  104. mutex_lock(&disc->disc_mutex);
  105. list_del(&rdata->peers);
  106. mutex_unlock(&disc->disc_mutex);
  107. break;
  108. default:
  109. break;
  110. }
  111. }
  112. /**
  113. * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN)
  114. * @sp: Current sequence of the RSCN exchange
  115. * @fp: RSCN Frame
  116. * @lport: Fibre Channel host port instance
  117. *
  118. * Locking Note: This function expects that the disc_mutex is locked
  119. * before it is called.
  120. */
  121. static void fc_disc_recv_rscn_req(struct fc_seq *sp, struct fc_frame *fp,
  122. struct fc_disc *disc)
  123. {
  124. struct fc_lport *lport;
  125. struct fc_rport_priv *rdata;
  126. struct fc_els_rscn *rp;
  127. struct fc_els_rscn_page *pp;
  128. struct fc_seq_els_data rjt_data;
  129. unsigned int len;
  130. int redisc = 0;
  131. enum fc_els_rscn_ev_qual ev_qual;
  132. enum fc_els_rscn_addr_fmt fmt;
  133. LIST_HEAD(disc_ports);
  134. struct fc_disc_port *dp, *next;
  135. lport = disc->lport;
  136. FC_DISC_DBG(disc, "Received an RSCN event\n");
  137. /* make sure the frame contains an RSCN message */
  138. rp = fc_frame_payload_get(fp, sizeof(*rp));
  139. if (!rp)
  140. goto reject;
  141. /* make sure the page length is as expected (4 bytes) */
  142. if (rp->rscn_page_len != sizeof(*pp))
  143. goto reject;
  144. /* get the RSCN payload length */
  145. len = ntohs(rp->rscn_plen);
  146. if (len < sizeof(*rp))
  147. goto reject;
  148. /* make sure the frame contains the expected payload */
  149. rp = fc_frame_payload_get(fp, len);
  150. if (!rp)
  151. goto reject;
  152. /* payload must be a multiple of the RSCN page size */
  153. len -= sizeof(*rp);
  154. if (len % sizeof(*pp))
  155. goto reject;
  156. for (pp = (void *)(rp + 1); len > 0; len -= sizeof(*pp), pp++) {
  157. ev_qual = pp->rscn_page_flags >> ELS_RSCN_EV_QUAL_BIT;
  158. ev_qual &= ELS_RSCN_EV_QUAL_MASK;
  159. fmt = pp->rscn_page_flags >> ELS_RSCN_ADDR_FMT_BIT;
  160. fmt &= ELS_RSCN_ADDR_FMT_MASK;
  161. /*
  162. * if we get an address format other than port
  163. * (area, domain, fabric), then do a full discovery
  164. */
  165. switch (fmt) {
  166. case ELS_ADDR_FMT_PORT:
  167. FC_DISC_DBG(disc, "Port address format for port "
  168. "(%6x)\n", ntoh24(pp->rscn_fid));
  169. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  170. if (!dp) {
  171. redisc = 1;
  172. break;
  173. }
  174. dp->lp = lport;
  175. dp->ids.port_id = ntoh24(pp->rscn_fid);
  176. dp->ids.port_name = -1;
  177. dp->ids.node_name = -1;
  178. dp->ids.roles = FC_RPORT_ROLE_UNKNOWN;
  179. list_add_tail(&dp->peers, &disc_ports);
  180. break;
  181. case ELS_ADDR_FMT_AREA:
  182. case ELS_ADDR_FMT_DOM:
  183. case ELS_ADDR_FMT_FAB:
  184. default:
  185. FC_DISC_DBG(disc, "Address format is (%d)\n", fmt);
  186. redisc = 1;
  187. break;
  188. }
  189. }
  190. lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
  191. if (redisc) {
  192. FC_DISC_DBG(disc, "RSCN received: rediscovering\n");
  193. fc_disc_restart(disc);
  194. } else {
  195. FC_DISC_DBG(disc, "RSCN received: not rediscovering. "
  196. "redisc %d state %d in_prog %d\n",
  197. redisc, lport->state, disc->pending);
  198. list_for_each_entry_safe(dp, next, &disc_ports, peers) {
  199. list_del(&dp->peers);
  200. rdata = lport->tt.rport_lookup(lport, dp->ids.port_id);
  201. if (rdata) {
  202. lport->tt.rport_logoff(rdata);
  203. }
  204. fc_disc_single(disc, dp);
  205. }
  206. }
  207. fc_frame_free(fp);
  208. return;
  209. reject:
  210. FC_DISC_DBG(disc, "Received a bad RSCN frame\n");
  211. rjt_data.fp = NULL;
  212. rjt_data.reason = ELS_RJT_LOGIC;
  213. rjt_data.explan = ELS_EXPL_NONE;
  214. lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
  215. fc_frame_free(fp);
  216. }
  217. /**
  218. * fc_disc_recv_req() - Handle incoming requests
  219. * @sp: Current sequence of the request exchange
  220. * @fp: The frame
  221. * @lport: The FC local port
  222. *
  223. * Locking Note: This function is called from the EM and will lock
  224. * the disc_mutex before calling the handler for the
  225. * request.
  226. */
  227. static void fc_disc_recv_req(struct fc_seq *sp, struct fc_frame *fp,
  228. struct fc_lport *lport)
  229. {
  230. u8 op;
  231. struct fc_disc *disc = &lport->disc;
  232. op = fc_frame_payload_op(fp);
  233. switch (op) {
  234. case ELS_RSCN:
  235. mutex_lock(&disc->disc_mutex);
  236. fc_disc_recv_rscn_req(sp, fp, disc);
  237. mutex_unlock(&disc->disc_mutex);
  238. break;
  239. default:
  240. FC_DISC_DBG(disc, "Received an unsupported request, "
  241. "the opcode is (%x)\n", op);
  242. break;
  243. }
  244. }
  245. /**
  246. * fc_disc_restart() - Restart discovery
  247. * @lport: FC discovery context
  248. *
  249. * Locking Note: This function expects that the disc mutex
  250. * is already locked.
  251. */
  252. static void fc_disc_restart(struct fc_disc *disc)
  253. {
  254. struct fc_rport_priv *rdata, *next;
  255. struct fc_lport *lport = disc->lport;
  256. FC_DISC_DBG(disc, "Restarting discovery\n");
  257. list_for_each_entry_safe(rdata, next, &disc->rports, peers)
  258. lport->tt.rport_logoff(rdata);
  259. disc->requested = 1;
  260. if (!disc->pending)
  261. fc_disc_gpn_ft_req(disc);
  262. }
  263. /**
  264. * fc_disc_start() - Fibre Channel Target discovery
  265. * @lport: FC local port
  266. *
  267. * Returns non-zero if discovery cannot be started.
  268. */
  269. static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
  270. enum fc_disc_event),
  271. struct fc_lport *lport)
  272. {
  273. struct fc_rport_priv *rdata;
  274. struct fc_disc *disc = &lport->disc;
  275. /*
  276. * At this point we may have a new disc job or an existing
  277. * one. Either way, let's lock when we make changes to it
  278. * and send the GPN_FT request.
  279. */
  280. mutex_lock(&disc->disc_mutex);
  281. disc->disc_callback = disc_callback;
  282. /*
  283. * If not ready, or already running discovery, just set request flag.
  284. */
  285. disc->requested = 1;
  286. if (disc->pending) {
  287. mutex_unlock(&disc->disc_mutex);
  288. return;
  289. }
  290. /*
  291. * Handle point-to-point mode as a simple discovery
  292. * of the remote port. Yucky, yucky, yuck, yuck!
  293. */
  294. rdata = disc->lport->ptp_rp;
  295. if (rdata) {
  296. kref_get(&rdata->kref);
  297. if (!fc_disc_new_target(disc, rdata, &rdata->ids)) {
  298. fc_disc_done(disc, DISC_EV_SUCCESS);
  299. }
  300. kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
  301. } else {
  302. fc_disc_gpn_ft_req(disc); /* get ports by FC-4 type */
  303. }
  304. mutex_unlock(&disc->disc_mutex);
  305. }
  306. static struct fc_rport_operations fc_disc_rport_ops = {
  307. .event_callback = fc_disc_rport_callback,
  308. };
  309. /**
  310. * fc_disc_new_target() - Handle new target found by discovery
  311. * @lport: FC local port
  312. * @rdata: The previous FC remote port priv (NULL if new remote port)
  313. * @ids: Identifiers for the new FC remote port
  314. *
  315. * Locking Note: This function expects that the disc_mutex is locked
  316. * before it is called.
  317. */
  318. static int fc_disc_new_target(struct fc_disc *disc,
  319. struct fc_rport_priv *rdata,
  320. struct fc_rport_identifiers *ids)
  321. {
  322. struct fc_lport *lport = disc->lport;
  323. int error = 0;
  324. if (rdata && ids->port_name) {
  325. if (rdata->ids.port_name == -1) {
  326. /*
  327. * Set WWN and fall through to notify of create.
  328. */
  329. rdata->ids.port_name = ids->port_name;
  330. rdata->ids.node_name = ids->node_name;
  331. } else if (rdata->ids.port_name != ids->port_name) {
  332. /*
  333. * This is a new port with the same FCID as
  334. * a previously-discovered port. Presumably the old
  335. * port logged out and a new port logged in and was
  336. * assigned the same FCID. This should be rare.
  337. * Delete the old one and fall thru to re-create.
  338. */
  339. lport->tt.rport_logoff(rdata);
  340. rdata = NULL;
  341. }
  342. }
  343. if (((ids->port_name != -1) || (ids->port_id != -1)) &&
  344. ids->port_id != fc_host_port_id(lport->host) &&
  345. ids->port_name != lport->wwpn) {
  346. if (!rdata) {
  347. rdata = lport->tt.rport_lookup(lport, ids->port_id);
  348. if (!rdata) {
  349. rdata = lport->tt.rport_create(lport, ids);
  350. if (!rdata)
  351. error = -ENOMEM;
  352. else
  353. list_add_tail(&rdata->peers,
  354. &disc->rports);
  355. }
  356. }
  357. if (rdata) {
  358. rdata->ops = &fc_disc_rport_ops;
  359. lport->tt.rport_login(rdata);
  360. }
  361. }
  362. return error;
  363. }
  364. /**
  365. * fc_disc_done() - Discovery has been completed
  366. * @disc: FC discovery context
  367. * @event: discovery completion status
  368. *
  369. * Locking Note: This function expects that the disc mutex is locked before
  370. * it is called. The discovery callback is then made with the lock released,
  371. * and the lock is re-taken before returning from this function
  372. */
  373. static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event)
  374. {
  375. struct fc_lport *lport = disc->lport;
  376. FC_DISC_DBG(disc, "Discovery complete\n");
  377. if (disc->requested)
  378. fc_disc_gpn_ft_req(disc);
  379. else
  380. disc->pending = 0;
  381. mutex_unlock(&disc->disc_mutex);
  382. disc->disc_callback(lport, event);
  383. mutex_lock(&disc->disc_mutex);
  384. }
  385. /**
  386. * fc_disc_error() - Handle error on dNS request
  387. * @disc: FC discovery context
  388. * @fp: The frame pointer
  389. */
  390. static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
  391. {
  392. struct fc_lport *lport = disc->lport;
  393. unsigned long delay = 0;
  394. FC_DISC_DBG(disc, "Error %ld, retries %d/%d\n",
  395. PTR_ERR(fp), disc->retry_count,
  396. FC_DISC_RETRY_LIMIT);
  397. if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
  398. /*
  399. * Memory allocation failure, or the exchange timed out,
  400. * retry after delay.
  401. */
  402. if (disc->retry_count < FC_DISC_RETRY_LIMIT) {
  403. /* go ahead and retry */
  404. if (!fp)
  405. delay = msecs_to_jiffies(FC_DISC_RETRY_DELAY);
  406. else {
  407. delay = msecs_to_jiffies(lport->e_d_tov);
  408. /* timeout faster first time */
  409. if (!disc->retry_count)
  410. delay /= 4;
  411. }
  412. disc->retry_count++;
  413. schedule_delayed_work(&disc->disc_work, delay);
  414. } else
  415. fc_disc_done(disc, DISC_EV_FAILED);
  416. }
  417. }
  418. /**
  419. * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
  420. * @lport: FC discovery context
  421. *
  422. * Locking Note: This function expects that the disc_mutex is locked
  423. * before it is called.
  424. */
  425. static void fc_disc_gpn_ft_req(struct fc_disc *disc)
  426. {
  427. struct fc_frame *fp;
  428. struct fc_lport *lport = disc->lport;
  429. WARN_ON(!fc_lport_test_ready(lport));
  430. disc->pending = 1;
  431. disc->requested = 0;
  432. disc->buf_len = 0;
  433. disc->seq_count = 0;
  434. fp = fc_frame_alloc(lport,
  435. sizeof(struct fc_ct_hdr) +
  436. sizeof(struct fc_ns_gid_ft));
  437. if (!fp)
  438. goto err;
  439. if (lport->tt.elsct_send(lport, 0, fp,
  440. FC_NS_GPN_FT,
  441. fc_disc_gpn_ft_resp,
  442. disc, lport->e_d_tov))
  443. return;
  444. err:
  445. fc_disc_error(disc, fp);
  446. }
  447. /**
  448. * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response.
  449. * @lport: Fibre Channel host port instance
  450. * @buf: GPN_FT response buffer
  451. * @len: size of response buffer
  452. *
  453. * Goes through the list of IDs and names resulting from a request.
  454. */
  455. static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
  456. {
  457. struct fc_lport *lport;
  458. struct fc_gpn_ft_resp *np;
  459. char *bp;
  460. size_t plen;
  461. size_t tlen;
  462. int error = 0;
  463. struct fc_rport_identifiers ids;
  464. struct fc_rport_priv *rdata;
  465. lport = disc->lport;
  466. /*
  467. * Handle partial name record left over from previous call.
  468. */
  469. bp = buf;
  470. plen = len;
  471. np = (struct fc_gpn_ft_resp *)bp;
  472. tlen = disc->buf_len;
  473. if (tlen) {
  474. WARN_ON(tlen >= sizeof(*np));
  475. plen = sizeof(*np) - tlen;
  476. WARN_ON(plen <= 0);
  477. WARN_ON(plen >= sizeof(*np));
  478. if (plen > len)
  479. plen = len;
  480. np = &disc->partial_buf;
  481. memcpy((char *)np + tlen, bp, plen);
  482. /*
  483. * Set bp so that the loop below will advance it to the
  484. * first valid full name element.
  485. */
  486. bp -= tlen;
  487. len += tlen;
  488. plen += tlen;
  489. disc->buf_len = (unsigned char) plen;
  490. if (plen == sizeof(*np))
  491. disc->buf_len = 0;
  492. }
  493. /*
  494. * Handle full name records, including the one filled from above.
  495. * Normally, np == bp and plen == len, but from the partial case above,
  496. * bp, len describe the overall buffer, and np, plen describe the
  497. * partial buffer, which if would usually be full now.
  498. * After the first time through the loop, things return to "normal".
  499. */
  500. while (plen >= sizeof(*np)) {
  501. ids.port_id = ntoh24(np->fp_fid);
  502. ids.port_name = ntohll(np->fp_wwpn);
  503. ids.node_name = -1;
  504. ids.roles = FC_RPORT_ROLE_UNKNOWN;
  505. if (ids.port_id != fc_host_port_id(lport->host) &&
  506. ids.port_name != lport->wwpn) {
  507. rdata = lport->tt.rport_create(lport, &ids);
  508. if (rdata) {
  509. rdata->ops = &fc_disc_rport_ops;
  510. list_add_tail(&rdata->peers, &disc->rports);
  511. lport->tt.rport_login(rdata);
  512. } else
  513. printk(KERN_WARNING "libfc: Failed to allocate "
  514. "memory for the newly discovered port "
  515. "(%6x)\n", ids.port_id);
  516. }
  517. if (np->fp_flags & FC_NS_FID_LAST) {
  518. fc_disc_done(disc, DISC_EV_SUCCESS);
  519. len = 0;
  520. break;
  521. }
  522. len -= sizeof(*np);
  523. bp += sizeof(*np);
  524. np = (struct fc_gpn_ft_resp *)bp;
  525. plen = len;
  526. }
  527. /*
  528. * Save any partial record at the end of the buffer for next time.
  529. */
  530. if (error == 0 && len > 0 && len < sizeof(*np)) {
  531. if (np != &disc->partial_buf) {
  532. FC_DISC_DBG(disc, "Partial buffer remains "
  533. "for discovery\n");
  534. memcpy(&disc->partial_buf, np, len);
  535. }
  536. disc->buf_len = (unsigned char) len;
  537. } else {
  538. disc->buf_len = 0;
  539. }
  540. return error;
  541. }
  542. /**
  543. * fc_disc_timeout() - Retry handler for the disc component
  544. * @work: Structure holding disc obj that needs retry discovery
  545. *
  546. * Handle retry of memory allocation for remote ports.
  547. */
  548. static void fc_disc_timeout(struct work_struct *work)
  549. {
  550. struct fc_disc *disc = container_of(work,
  551. struct fc_disc,
  552. disc_work.work);
  553. mutex_lock(&disc->disc_mutex);
  554. if (disc->requested && !disc->pending)
  555. fc_disc_gpn_ft_req(disc);
  556. mutex_unlock(&disc->disc_mutex);
  557. }
  558. /**
  559. * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
  560. * @sp: Current sequence of GPN_FT exchange
  561. * @fp: response frame
  562. * @lp_arg: Fibre Channel host port instance
  563. *
  564. * Locking Note: This function is called without disc mutex held, and
  565. * should do all its processing with the mutex held
  566. */
  567. static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp,
  568. void *disc_arg)
  569. {
  570. struct fc_disc *disc = disc_arg;
  571. struct fc_ct_hdr *cp;
  572. struct fc_frame_header *fh;
  573. unsigned int seq_cnt;
  574. void *buf = NULL;
  575. unsigned int len;
  576. int error;
  577. mutex_lock(&disc->disc_mutex);
  578. FC_DISC_DBG(disc, "Received a GPN_FT response\n");
  579. if (IS_ERR(fp)) {
  580. fc_disc_error(disc, fp);
  581. mutex_unlock(&disc->disc_mutex);
  582. return;
  583. }
  584. WARN_ON(!fc_frame_is_linear(fp)); /* buffer must be contiguous */
  585. fh = fc_frame_header_get(fp);
  586. len = fr_len(fp) - sizeof(*fh);
  587. seq_cnt = ntohs(fh->fh_seq_cnt);
  588. if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 &&
  589. disc->seq_count == 0) {
  590. cp = fc_frame_payload_get(fp, sizeof(*cp));
  591. if (!cp) {
  592. FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n",
  593. fr_len(fp));
  594. } else if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
  595. /* Accepted, parse the response. */
  596. buf = cp + 1;
  597. len -= sizeof(*cp);
  598. } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
  599. FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x "
  600. "(check zoning)\n", cp->ct_reason,
  601. cp->ct_explan);
  602. fc_disc_done(disc, DISC_EV_FAILED);
  603. } else {
  604. FC_DISC_DBG(disc, "GPN_FT unexpected response code "
  605. "%x\n", ntohs(cp->ct_cmd));
  606. }
  607. } else if (fr_sof(fp) == FC_SOF_N3 &&
  608. seq_cnt == disc->seq_count) {
  609. buf = fh + 1;
  610. } else {
  611. FC_DISC_DBG(disc, "GPN_FT unexpected frame - out of sequence? "
  612. "seq_cnt %x expected %x sof %x eof %x\n",
  613. seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp));
  614. }
  615. if (buf) {
  616. error = fc_disc_gpn_ft_parse(disc, buf, len);
  617. if (error)
  618. fc_disc_error(disc, fp);
  619. else
  620. disc->seq_count++;
  621. }
  622. fc_frame_free(fp);
  623. mutex_unlock(&disc->disc_mutex);
  624. }
  625. /**
  626. * fc_disc_single() - Discover the directory information for a single target
  627. * @lport: FC local port
  628. * @dp: The port to rediscover
  629. *
  630. * Locking Note: This function expects that the disc_mutex is locked
  631. * before it is called.
  632. */
  633. static void fc_disc_single(struct fc_disc *disc, struct fc_disc_port *dp)
  634. {
  635. struct fc_lport *lport;
  636. struct fc_rport_priv *rdata;
  637. lport = disc->lport;
  638. if (dp->ids.port_id == fc_host_port_id(lport->host))
  639. goto out;
  640. rdata = lport->tt.rport_create(lport, &dp->ids);
  641. if (rdata) {
  642. rdata->ops = &fc_disc_rport_ops;
  643. kfree(dp);
  644. list_add_tail(&rdata->peers, &disc->rports);
  645. lport->tt.rport_login(rdata);
  646. }
  647. return;
  648. out:
  649. kfree(dp);
  650. }
  651. /**
  652. * fc_disc_stop() - Stop discovery for a given lport
  653. * @lport: The lport that discovery should stop for
  654. */
  655. void fc_disc_stop(struct fc_lport *lport)
  656. {
  657. struct fc_disc *disc = &lport->disc;
  658. if (disc) {
  659. cancel_delayed_work_sync(&disc->disc_work);
  660. fc_disc_stop_rports(disc);
  661. }
  662. }
  663. /**
  664. * fc_disc_stop_final() - Stop discovery for a given lport
  665. * @lport: The lport that discovery should stop for
  666. *
  667. * This function will block until discovery has been
  668. * completely stopped and all rports have been deleted.
  669. */
  670. void fc_disc_stop_final(struct fc_lport *lport)
  671. {
  672. fc_disc_stop(lport);
  673. lport->tt.rport_flush_queue();
  674. }
  675. /**
  676. * fc_disc_init() - Initialize the discovery block
  677. * @lport: FC local port
  678. */
  679. int fc_disc_init(struct fc_lport *lport)
  680. {
  681. struct fc_disc *disc;
  682. if (!lport->tt.disc_start)
  683. lport->tt.disc_start = fc_disc_start;
  684. if (!lport->tt.disc_stop)
  685. lport->tt.disc_stop = fc_disc_stop;
  686. if (!lport->tt.disc_stop_final)
  687. lport->tt.disc_stop_final = fc_disc_stop_final;
  688. if (!lport->tt.disc_recv_req)
  689. lport->tt.disc_recv_req = fc_disc_recv_req;
  690. if (!lport->tt.rport_lookup)
  691. lport->tt.rport_lookup = fc_disc_lookup_rport;
  692. disc = &lport->disc;
  693. INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
  694. mutex_init(&disc->disc_mutex);
  695. INIT_LIST_HEAD(&disc->rports);
  696. disc->lport = lport;
  697. disc->delay = FC_DISC_DELAY;
  698. return 0;
  699. }
  700. EXPORT_SYMBOL(fc_disc_init);