fc_disc.c 21 KB

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