fc_disc.c 22 KB

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