fc_disc.c 21 KB

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