fc_disc.c 21 KB

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