fc_disc.c 22 KB

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