blob: ddf494424ff4efd12e759d9cc090b8c6b956d51c [file] [log] [blame]
Robert Love42e9a922008-12-09 15:10:17 -08001/*
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/*
21 * Target Discovery
22 *
23 * This block discovers all FC-4 remote ports, including FCP initiators. It
24 * also handles RSCN events and re-discovery if necessary.
25 */
26
27/*
28 * DISC LOCKING
29 *
30 * The disc mutex is can be locked when acquiring rport locks, but may not
31 * be held when acquiring the lport lock. Refer to fc_lport.c for more
32 * details.
33 */
34
35#include <linux/timer.h>
36#include <linux/err.h>
37#include <asm/unaligned.h>
38
39#include <scsi/fc/fc_gs.h>
40
41#include <scsi/libfc.h>
42
43#define FC_DISC_RETRY_LIMIT 3 /* max retries */
44#define FC_DISC_RETRY_DELAY 500UL /* (msecs) delay */
45
Robert Love42e9a922008-12-09 15:10:17 -080046static void fc_disc_gpn_ft_req(struct fc_disc *);
47static void fc_disc_gpn_ft_resp(struct fc_seq *, struct fc_frame *, void *);
Joe Eykholtf211fa52009-08-25 14:01:01 -070048static int fc_disc_new_target(struct fc_disc *, struct fc_rport_priv *,
Robert Love42e9a922008-12-09 15:10:17 -080049 struct fc_rport_identifiers *);
Joe Eykholt786681b2009-08-25 14:01:29 -070050static void fc_disc_done(struct fc_disc *, enum fc_disc_event);
Robert Love42e9a922008-12-09 15:10:17 -080051static void fc_disc_timeout(struct work_struct *);
52static void fc_disc_single(struct fc_disc *, struct fc_disc_port *);
53static void fc_disc_restart(struct fc_disc *);
54
55/**
Robert Love34f42a02009-02-27 10:55:45 -080056 * fc_disc_stop_rports() - delete all the remote ports associated with the lport
Robert Love42e9a922008-12-09 15:10:17 -080057 * @disc: The discovery job to stop rports on
58 *
59 * Locking Note: This function expects that the lport mutex is locked before
60 * calling it.
61 */
62void fc_disc_stop_rports(struct fc_disc *disc)
63{
64 struct fc_lport *lport;
Joe Eykholtab28f1f2009-08-25 14:00:34 -070065 struct fc_rport_priv *rdata, *next;
Robert Love42e9a922008-12-09 15:10:17 -080066
67 lport = disc->lport;
68
69 mutex_lock(&disc->disc_mutex);
Joe Eykholt9e9d0452009-08-25 14:01:18 -070070 list_for_each_entry_safe(rdata, next, &disc->rports, peers)
Joe Eykholt9fb9d322009-08-25 14:00:50 -070071 lport->tt.rport_logoff(rdata);
Robert Love42e9a922008-12-09 15:10:17 -080072 mutex_unlock(&disc->disc_mutex);
73}
74
75/**
Robert Love34f42a02009-02-27 10:55:45 -080076 * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN)
Robert Love42e9a922008-12-09 15:10:17 -080077 * @sp: Current sequence of the RSCN exchange
78 * @fp: RSCN Frame
79 * @lport: Fibre Channel host port instance
80 *
81 * Locking Note: This function expects that the disc_mutex is locked
82 * before it is called.
83 */
84static void fc_disc_recv_rscn_req(struct fc_seq *sp, struct fc_frame *fp,
85 struct fc_disc *disc)
86{
87 struct fc_lport *lport;
Joe Eykholtab28f1f2009-08-25 14:00:34 -070088 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -080089 struct fc_els_rscn *rp;
90 struct fc_els_rscn_page *pp;
91 struct fc_seq_els_data rjt_data;
92 unsigned int len;
93 int redisc = 0;
94 enum fc_els_rscn_ev_qual ev_qual;
95 enum fc_els_rscn_addr_fmt fmt;
96 LIST_HEAD(disc_ports);
97 struct fc_disc_port *dp, *next;
98
99 lport = disc->lport;
100
Robert Love74147052009-06-10 15:31:10 -0700101 FC_DISC_DBG(disc, "Received an RSCN event\n");
Robert Love42e9a922008-12-09 15:10:17 -0800102
103 /* make sure the frame contains an RSCN message */
104 rp = fc_frame_payload_get(fp, sizeof(*rp));
105 if (!rp)
106 goto reject;
107 /* make sure the page length is as expected (4 bytes) */
108 if (rp->rscn_page_len != sizeof(*pp))
109 goto reject;
110 /* get the RSCN payload length */
111 len = ntohs(rp->rscn_plen);
112 if (len < sizeof(*rp))
113 goto reject;
114 /* make sure the frame contains the expected payload */
115 rp = fc_frame_payload_get(fp, len);
116 if (!rp)
117 goto reject;
118 /* payload must be a multiple of the RSCN page size */
119 len -= sizeof(*rp);
120 if (len % sizeof(*pp))
121 goto reject;
122
123 for (pp = (void *)(rp + 1); len > 0; len -= sizeof(*pp), pp++) {
124 ev_qual = pp->rscn_page_flags >> ELS_RSCN_EV_QUAL_BIT;
125 ev_qual &= ELS_RSCN_EV_QUAL_MASK;
126 fmt = pp->rscn_page_flags >> ELS_RSCN_ADDR_FMT_BIT;
127 fmt &= ELS_RSCN_ADDR_FMT_MASK;
128 /*
129 * if we get an address format other than port
130 * (area, domain, fabric), then do a full discovery
131 */
132 switch (fmt) {
133 case ELS_ADDR_FMT_PORT:
Robert Love74147052009-06-10 15:31:10 -0700134 FC_DISC_DBG(disc, "Port address format for port "
135 "(%6x)\n", ntoh24(pp->rscn_fid));
Robert Love42e9a922008-12-09 15:10:17 -0800136 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
137 if (!dp) {
138 redisc = 1;
139 break;
140 }
141 dp->lp = lport;
142 dp->ids.port_id = ntoh24(pp->rscn_fid);
143 dp->ids.port_name = -1;
144 dp->ids.node_name = -1;
145 dp->ids.roles = FC_RPORT_ROLE_UNKNOWN;
146 list_add_tail(&dp->peers, &disc_ports);
147 break;
148 case ELS_ADDR_FMT_AREA:
149 case ELS_ADDR_FMT_DOM:
150 case ELS_ADDR_FMT_FAB:
151 default:
Robert Love74147052009-06-10 15:31:10 -0700152 FC_DISC_DBG(disc, "Address format is (%d)\n", fmt);
Robert Love42e9a922008-12-09 15:10:17 -0800153 redisc = 1;
154 break;
155 }
156 }
157 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
158 if (redisc) {
Robert Love74147052009-06-10 15:31:10 -0700159 FC_DISC_DBG(disc, "RSCN received: rediscovering\n");
Robert Love42e9a922008-12-09 15:10:17 -0800160 fc_disc_restart(disc);
161 } else {
Robert Love74147052009-06-10 15:31:10 -0700162 FC_DISC_DBG(disc, "RSCN received: not rediscovering. "
163 "redisc %d state %d in_prog %d\n",
164 redisc, lport->state, disc->pending);
Robert Love42e9a922008-12-09 15:10:17 -0800165 list_for_each_entry_safe(dp, next, &disc_ports, peers) {
166 list_del(&dp->peers);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700167 rdata = lport->tt.rport_lookup(lport, dp->ids.port_id);
168 if (rdata) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700169 lport->tt.rport_logoff(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800170 }
171 fc_disc_single(disc, dp);
172 }
173 }
174 fc_frame_free(fp);
175 return;
176reject:
Robert Love74147052009-06-10 15:31:10 -0700177 FC_DISC_DBG(disc, "Received a bad RSCN frame\n");
Robert Love42e9a922008-12-09 15:10:17 -0800178 rjt_data.fp = NULL;
179 rjt_data.reason = ELS_RJT_LOGIC;
180 rjt_data.explan = ELS_EXPL_NONE;
181 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
182 fc_frame_free(fp);
183}
184
185/**
Robert Love34f42a02009-02-27 10:55:45 -0800186 * fc_disc_recv_req() - Handle incoming requests
Robert Love42e9a922008-12-09 15:10:17 -0800187 * @sp: Current sequence of the request exchange
188 * @fp: The frame
189 * @lport: The FC local port
190 *
191 * Locking Note: This function is called from the EM and will lock
192 * the disc_mutex before calling the handler for the
193 * request.
194 */
195static void fc_disc_recv_req(struct fc_seq *sp, struct fc_frame *fp,
196 struct fc_lport *lport)
197{
198 u8 op;
199 struct fc_disc *disc = &lport->disc;
200
201 op = fc_frame_payload_op(fp);
202 switch (op) {
203 case ELS_RSCN:
204 mutex_lock(&disc->disc_mutex);
205 fc_disc_recv_rscn_req(sp, fp, disc);
206 mutex_unlock(&disc->disc_mutex);
207 break;
208 default:
Robert Love74147052009-06-10 15:31:10 -0700209 FC_DISC_DBG(disc, "Received an unsupported request, "
210 "the opcode is (%x)\n", op);
Robert Love42e9a922008-12-09 15:10:17 -0800211 break;
212 }
213}
214
215/**
Robert Love34f42a02009-02-27 10:55:45 -0800216 * fc_disc_restart() - Restart discovery
Robert Love42e9a922008-12-09 15:10:17 -0800217 * @lport: FC discovery context
218 *
219 * Locking Note: This function expects that the disc mutex
220 * is already locked.
221 */
222static void fc_disc_restart(struct fc_disc *disc)
223{
Robert Love74147052009-06-10 15:31:10 -0700224 FC_DISC_DBG(disc, "Restarting discovery\n");
Robert Love42e9a922008-12-09 15:10:17 -0800225
Robert Love42e9a922008-12-09 15:10:17 -0800226 disc->requested = 1;
Joe Eykholt0f6c614982009-08-25 14:02:11 -0700227 if (disc->pending)
228 return;
229
230 /*
231 * Advance disc_id. This is an arbitrary non-zero number that will
232 * match the value in the fc_rport_priv after discovery for all
233 * freshly-discovered remote ports. Avoid wrapping to zero.
234 */
235 disc->disc_id = (disc->disc_id + 2) | 1;
236 fc_disc_gpn_ft_req(disc);
Robert Love42e9a922008-12-09 15:10:17 -0800237}
238
239/**
Robert Love34f42a02009-02-27 10:55:45 -0800240 * fc_disc_start() - Fibre Channel Target discovery
Robert Love42e9a922008-12-09 15:10:17 -0800241 * @lport: FC local port
242 *
243 * Returns non-zero if discovery cannot be started.
244 */
245static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
246 enum fc_disc_event),
247 struct fc_lport *lport)
248{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700249 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800250 struct fc_disc *disc = &lport->disc;
251
252 /*
253 * At this point we may have a new disc job or an existing
254 * one. Either way, let's lock when we make changes to it
255 * and send the GPN_FT request.
256 */
257 mutex_lock(&disc->disc_mutex);
258
259 disc->disc_callback = disc_callback;
260
261 /*
262 * If not ready, or already running discovery, just set request flag.
263 */
264 disc->requested = 1;
265
266 if (disc->pending) {
267 mutex_unlock(&disc->disc_mutex);
268 return;
269 }
270
271 /*
272 * Handle point-to-point mode as a simple discovery
273 * of the remote port. Yucky, yucky, yuck, yuck!
274 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700275 rdata = disc->lport->ptp_rp;
276 if (rdata) {
Joe Eykholtf211fa52009-08-25 14:01:01 -0700277 kref_get(&rdata->kref);
278 if (!fc_disc_new_target(disc, rdata, &rdata->ids)) {
Joe Eykholt786681b2009-08-25 14:01:29 -0700279 fc_disc_done(disc, DISC_EV_SUCCESS);
Robert Love42e9a922008-12-09 15:10:17 -0800280 }
Joe Eykholtf211fa52009-08-25 14:01:01 -0700281 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800282 } else {
Joe Eykholt0f6c614982009-08-25 14:02:11 -0700283 disc->disc_id = (disc->disc_id + 2) | 1;
Robert Love42e9a922008-12-09 15:10:17 -0800284 fc_disc_gpn_ft_req(disc); /* get ports by FC-4 type */
285 }
286
287 mutex_unlock(&disc->disc_mutex);
288}
289
Robert Love42e9a922008-12-09 15:10:17 -0800290/**
Robert Love34f42a02009-02-27 10:55:45 -0800291 * fc_disc_new_target() - Handle new target found by discovery
Robert Love42e9a922008-12-09 15:10:17 -0800292 * @lport: FC local port
Joe Eykholtf211fa52009-08-25 14:01:01 -0700293 * @rdata: The previous FC remote port priv (NULL if new remote port)
Robert Love42e9a922008-12-09 15:10:17 -0800294 * @ids: Identifiers for the new FC remote port
295 *
296 * Locking Note: This function expects that the disc_mutex is locked
297 * before it is called.
298 */
299static int fc_disc_new_target(struct fc_disc *disc,
Joe Eykholtf211fa52009-08-25 14:01:01 -0700300 struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800301 struct fc_rport_identifiers *ids)
302{
303 struct fc_lport *lport = disc->lport;
Robert Love42e9a922008-12-09 15:10:17 -0800304 int error = 0;
305
Joe Eykholtf211fa52009-08-25 14:01:01 -0700306 if (rdata && ids->port_name) {
307 if (rdata->ids.port_name == -1) {
Robert Love42e9a922008-12-09 15:10:17 -0800308 /*
309 * Set WWN and fall through to notify of create.
310 */
Joe Eykholtf211fa52009-08-25 14:01:01 -0700311 rdata->ids.port_name = ids->port_name;
312 rdata->ids.node_name = ids->node_name;
313 } else if (rdata->ids.port_name != ids->port_name) {
Robert Love42e9a922008-12-09 15:10:17 -0800314 /*
315 * This is a new port with the same FCID as
316 * a previously-discovered port. Presumably the old
317 * port logged out and a new port logged in and was
318 * assigned the same FCID. This should be rare.
319 * Delete the old one and fall thru to re-create.
320 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700321 lport->tt.rport_logoff(rdata);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700322 rdata = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800323 }
324 }
325 if (((ids->port_name != -1) || (ids->port_id != -1)) &&
326 ids->port_id != fc_host_port_id(lport->host) &&
327 ids->port_name != lport->wwpn) {
Joe Eykholtf211fa52009-08-25 14:01:01 -0700328 if (!rdata) {
Joe Eykholt19f97e32009-08-25 14:01:55 -0700329 rdata = lport->tt.rport_create(lport, ids);
330 if (!rdata)
331 error = -ENOMEM;
Robert Love42e9a922008-12-09 15:10:17 -0800332 }
Joe Eykholt83455922009-08-25 14:02:01 -0700333 if (rdata)
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700334 lport->tt.rport_login(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800335 }
336 return error;
337}
338
339/**
Robert Love34f42a02009-02-27 10:55:45 -0800340 * fc_disc_done() - Discovery has been completed
Robert Love42e9a922008-12-09 15:10:17 -0800341 * @disc: FC discovery context
Joe Eykholt786681b2009-08-25 14:01:29 -0700342 * @event: discovery completion status
343 *
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700344 * Locking Note: This function expects that the disc mutex is locked before
345 * it is called. The discovery callback is then made with the lock released,
346 * and the lock is re-taken before returning from this function
Robert Love42e9a922008-12-09 15:10:17 -0800347 */
Joe Eykholt786681b2009-08-25 14:01:29 -0700348static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event)
Robert Love42e9a922008-12-09 15:10:17 -0800349{
350 struct fc_lport *lport = disc->lport;
Joe Eykholt0f6c614982009-08-25 14:02:11 -0700351 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800352
Robert Love74147052009-06-10 15:31:10 -0700353 FC_DISC_DBG(disc, "Discovery complete\n");
Robert Love42e9a922008-12-09 15:10:17 -0800354
Joe Eykholt0f6c614982009-08-25 14:02:11 -0700355 disc->pending = 0;
356 if (disc->requested) {
357 fc_disc_restart(disc);
358 return;
359 }
360
361 /*
362 * Go through all remote ports. If they were found in the latest
363 * discovery, reverify or log them in. Otherwise, log them out.
364 * Skip ports which were never discovered. These are the dNS port
365 * and ports which were created by PLOGI.
366 */
367 list_for_each_entry(rdata, &disc->rports, peers) {
368 if (!rdata->disc_id)
369 continue;
370 if (rdata->disc_id == disc->disc_id)
371 lport->tt.rport_login(rdata);
372 else
373 lport->tt.rport_logoff(rdata);
374 }
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700375
376 mutex_unlock(&disc->disc_mutex);
377 disc->disc_callback(lport, event);
378 mutex_lock(&disc->disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800379}
380
381/**
Robert Love34f42a02009-02-27 10:55:45 -0800382 * fc_disc_error() - Handle error on dNS request
Robert Love42e9a922008-12-09 15:10:17 -0800383 * @disc: FC discovery context
384 * @fp: The frame pointer
385 */
386static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
387{
388 struct fc_lport *lport = disc->lport;
389 unsigned long delay = 0;
Robert Love74147052009-06-10 15:31:10 -0700390
391 FC_DISC_DBG(disc, "Error %ld, retries %d/%d\n",
392 PTR_ERR(fp), disc->retry_count,
393 FC_DISC_RETRY_LIMIT);
Robert Love42e9a922008-12-09 15:10:17 -0800394
395 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
396 /*
397 * Memory allocation failure, or the exchange timed out,
398 * retry after delay.
399 */
400 if (disc->retry_count < FC_DISC_RETRY_LIMIT) {
401 /* go ahead and retry */
402 if (!fp)
403 delay = msecs_to_jiffies(FC_DISC_RETRY_DELAY);
404 else {
405 delay = msecs_to_jiffies(lport->e_d_tov);
406
407 /* timeout faster first time */
408 if (!disc->retry_count)
409 delay /= 4;
410 }
411 disc->retry_count++;
412 schedule_delayed_work(&disc->disc_work, delay);
Joe Eykholt786681b2009-08-25 14:01:29 -0700413 } else
414 fc_disc_done(disc, DISC_EV_FAILED);
Robert Love42e9a922008-12-09 15:10:17 -0800415 }
416}
417
418/**
Robert Love34f42a02009-02-27 10:55:45 -0800419 * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
Robert Love42e9a922008-12-09 15:10:17 -0800420 * @lport: FC discovery context
421 *
422 * Locking Note: This function expects that the disc_mutex is locked
423 * before it is called.
424 */
425static void fc_disc_gpn_ft_req(struct fc_disc *disc)
426{
427 struct fc_frame *fp;
428 struct fc_lport *lport = disc->lport;
429
430 WARN_ON(!fc_lport_test_ready(lport));
431
432 disc->pending = 1;
433 disc->requested = 0;
434
435 disc->buf_len = 0;
436 disc->seq_count = 0;
437 fp = fc_frame_alloc(lport,
438 sizeof(struct fc_ct_hdr) +
439 sizeof(struct fc_ns_gid_ft));
440 if (!fp)
441 goto err;
442
Joe Eykholta46f3272009-08-25 14:00:55 -0700443 if (lport->tt.elsct_send(lport, 0, fp,
Robert Love42e9a922008-12-09 15:10:17 -0800444 FC_NS_GPN_FT,
445 fc_disc_gpn_ft_resp,
446 disc, lport->e_d_tov))
447 return;
448err:
449 fc_disc_error(disc, fp);
450}
451
452/**
Joe Eykholt786681b2009-08-25 14:01:29 -0700453 * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response.
Robert Love42e9a922008-12-09 15:10:17 -0800454 * @lport: Fibre Channel host port instance
455 * @buf: GPN_FT response buffer
456 * @len: size of response buffer
Joe Eykholt786681b2009-08-25 14:01:29 -0700457 *
458 * Goes through the list of IDs and names resulting from a request.
Robert Love42e9a922008-12-09 15:10:17 -0800459 */
460static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
461{
462 struct fc_lport *lport;
463 struct fc_gpn_ft_resp *np;
464 char *bp;
465 size_t plen;
466 size_t tlen;
467 int error = 0;
Joe Eykholt795d86f2009-08-25 14:00:39 -0700468 struct fc_rport_identifiers ids;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700469 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800470
471 lport = disc->lport;
472
473 /*
474 * Handle partial name record left over from previous call.
475 */
476 bp = buf;
477 plen = len;
478 np = (struct fc_gpn_ft_resp *)bp;
479 tlen = disc->buf_len;
480 if (tlen) {
481 WARN_ON(tlen >= sizeof(*np));
482 plen = sizeof(*np) - tlen;
483 WARN_ON(plen <= 0);
484 WARN_ON(plen >= sizeof(*np));
485 if (plen > len)
486 plen = len;
487 np = &disc->partial_buf;
488 memcpy((char *)np + tlen, bp, plen);
489
490 /*
491 * Set bp so that the loop below will advance it to the
492 * first valid full name element.
493 */
494 bp -= tlen;
495 len += tlen;
496 plen += tlen;
497 disc->buf_len = (unsigned char) plen;
498 if (plen == sizeof(*np))
499 disc->buf_len = 0;
500 }
501
502 /*
503 * Handle full name records, including the one filled from above.
504 * Normally, np == bp and plen == len, but from the partial case above,
505 * bp, len describe the overall buffer, and np, plen describe the
506 * partial buffer, which if would usually be full now.
507 * After the first time through the loop, things return to "normal".
508 */
509 while (plen >= sizeof(*np)) {
Joe Eykholt795d86f2009-08-25 14:00:39 -0700510 ids.port_id = ntoh24(np->fp_fid);
511 ids.port_name = ntohll(np->fp_wwpn);
512 ids.node_name = -1;
513 ids.roles = FC_RPORT_ROLE_UNKNOWN;
Robert Love42e9a922008-12-09 15:10:17 -0800514
Joe Eykholt795d86f2009-08-25 14:00:39 -0700515 if (ids.port_id != fc_host_port_id(lport->host) &&
516 ids.port_name != lport->wwpn) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700517 rdata = lport->tt.rport_create(lport, &ids);
Joe Eykholt83455922009-08-25 14:02:01 -0700518 if (rdata)
Joe Eykholt0f6c614982009-08-25 14:02:11 -0700519 rdata->disc_id = disc->disc_id;
Joe Eykholt83455922009-08-25 14:02:01 -0700520 else
Robert Love74147052009-06-10 15:31:10 -0700521 printk(KERN_WARNING "libfc: Failed to allocate "
522 "memory for the newly discovered port "
Joe Eykholt795d86f2009-08-25 14:00:39 -0700523 "(%6x)\n", ids.port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800524 }
525
526 if (np->fp_flags & FC_NS_FID_LAST) {
Joe Eykholt786681b2009-08-25 14:01:29 -0700527 fc_disc_done(disc, DISC_EV_SUCCESS);
Robert Love42e9a922008-12-09 15:10:17 -0800528 len = 0;
529 break;
530 }
531 len -= sizeof(*np);
532 bp += sizeof(*np);
533 np = (struct fc_gpn_ft_resp *)bp;
534 plen = len;
535 }
536
537 /*
538 * Save any partial record at the end of the buffer for next time.
539 */
540 if (error == 0 && len > 0 && len < sizeof(*np)) {
541 if (np != &disc->partial_buf) {
Robert Love74147052009-06-10 15:31:10 -0700542 FC_DISC_DBG(disc, "Partial buffer remains "
543 "for discovery\n");
Robert Love42e9a922008-12-09 15:10:17 -0800544 memcpy(&disc->partial_buf, np, len);
545 }
546 disc->buf_len = (unsigned char) len;
547 } else {
548 disc->buf_len = 0;
549 }
550 return error;
551}
552
Robert Love34f42a02009-02-27 10:55:45 -0800553/**
554 * fc_disc_timeout() - Retry handler for the disc component
555 * @work: Structure holding disc obj that needs retry discovery
556 *
Robert Love42e9a922008-12-09 15:10:17 -0800557 * Handle retry of memory allocation for remote ports.
558 */
559static void fc_disc_timeout(struct work_struct *work)
560{
561 struct fc_disc *disc = container_of(work,
562 struct fc_disc,
563 disc_work.work);
564 mutex_lock(&disc->disc_mutex);
565 if (disc->requested && !disc->pending)
566 fc_disc_gpn_ft_req(disc);
567 mutex_unlock(&disc->disc_mutex);
568}
569
570/**
Robert Love34f42a02009-02-27 10:55:45 -0800571 * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
Robert Love42e9a922008-12-09 15:10:17 -0800572 * @sp: Current sequence of GPN_FT exchange
573 * @fp: response frame
574 * @lp_arg: Fibre Channel host port instance
575 *
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700576 * Locking Note: This function is called without disc mutex held, and
577 * should do all its processing with the mutex held
Robert Love42e9a922008-12-09 15:10:17 -0800578 */
579static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp,
580 void *disc_arg)
581{
582 struct fc_disc *disc = disc_arg;
583 struct fc_ct_hdr *cp;
584 struct fc_frame_header *fh;
585 unsigned int seq_cnt;
586 void *buf = NULL;
587 unsigned int len;
588 int error;
589
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700590 mutex_lock(&disc->disc_mutex);
Robert Love74147052009-06-10 15:31:10 -0700591 FC_DISC_DBG(disc, "Received a GPN_FT response\n");
Robert Love42e9a922008-12-09 15:10:17 -0800592
593 if (IS_ERR(fp)) {
594 fc_disc_error(disc, fp);
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700595 mutex_unlock(&disc->disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800596 return;
597 }
598
599 WARN_ON(!fc_frame_is_linear(fp)); /* buffer must be contiguous */
600 fh = fc_frame_header_get(fp);
601 len = fr_len(fp) - sizeof(*fh);
602 seq_cnt = ntohs(fh->fh_seq_cnt);
603 if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 &&
604 disc->seq_count == 0) {
605 cp = fc_frame_payload_get(fp, sizeof(*cp));
606 if (!cp) {
Robert Love74147052009-06-10 15:31:10 -0700607 FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n",
608 fr_len(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800609 } else if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
610
Robert Love34f42a02009-02-27 10:55:45 -0800611 /* Accepted, parse the response. */
Robert Love42e9a922008-12-09 15:10:17 -0800612 buf = cp + 1;
613 len -= sizeof(*cp);
614 } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
Robert Love74147052009-06-10 15:31:10 -0700615 FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x "
616 "(check zoning)\n", cp->ct_reason,
617 cp->ct_explan);
Joe Eykholt786681b2009-08-25 14:01:29 -0700618 fc_disc_done(disc, DISC_EV_FAILED);
Robert Love42e9a922008-12-09 15:10:17 -0800619 } else {
Robert Love74147052009-06-10 15:31:10 -0700620 FC_DISC_DBG(disc, "GPN_FT unexpected response code "
621 "%x\n", ntohs(cp->ct_cmd));
Robert Love42e9a922008-12-09 15:10:17 -0800622 }
623 } else if (fr_sof(fp) == FC_SOF_N3 &&
624 seq_cnt == disc->seq_count) {
625 buf = fh + 1;
626 } else {
Robert Love74147052009-06-10 15:31:10 -0700627 FC_DISC_DBG(disc, "GPN_FT unexpected frame - out of sequence? "
628 "seq_cnt %x expected %x sof %x eof %x\n",
629 seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800630 }
631 if (buf) {
632 error = fc_disc_gpn_ft_parse(disc, buf, len);
633 if (error)
634 fc_disc_error(disc, fp);
635 else
636 disc->seq_count++;
637 }
638 fc_frame_free(fp);
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700639
640 mutex_unlock(&disc->disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800641}
642
643/**
Robert Love34f42a02009-02-27 10:55:45 -0800644 * fc_disc_single() - Discover the directory information for a single target
Robert Love42e9a922008-12-09 15:10:17 -0800645 * @lport: FC local port
646 * @dp: The port to rediscover
647 *
648 * Locking Note: This function expects that the disc_mutex is locked
649 * before it is called.
650 */
651static void fc_disc_single(struct fc_disc *disc, struct fc_disc_port *dp)
652{
653 struct fc_lport *lport;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700654 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800655
656 lport = disc->lport;
657
658 if (dp->ids.port_id == fc_host_port_id(lport->host))
659 goto out;
660
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700661 rdata = lport->tt.rport_create(lport, &dp->ids);
662 if (rdata) {
Joe Eykholt0f6c614982009-08-25 14:02:11 -0700663 rdata->disc_id = disc->disc_id;
Robert Love42e9a922008-12-09 15:10:17 -0800664 kfree(dp);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700665 lport->tt.rport_login(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800666 }
667 return;
668out:
669 kfree(dp);
670}
671
672/**
Robert Love34f42a02009-02-27 10:55:45 -0800673 * fc_disc_stop() - Stop discovery for a given lport
Robert Love42e9a922008-12-09 15:10:17 -0800674 * @lport: The lport that discovery should stop for
675 */
676void fc_disc_stop(struct fc_lport *lport)
677{
678 struct fc_disc *disc = &lport->disc;
679
680 if (disc) {
681 cancel_delayed_work_sync(&disc->disc_work);
682 fc_disc_stop_rports(disc);
683 }
684}
685
686/**
Robert Love34f42a02009-02-27 10:55:45 -0800687 * fc_disc_stop_final() - Stop discovery for a given lport
Robert Love42e9a922008-12-09 15:10:17 -0800688 * @lport: The lport that discovery should stop for
689 *
690 * This function will block until discovery has been
691 * completely stopped and all rports have been deleted.
692 */
693void fc_disc_stop_final(struct fc_lport *lport)
694{
695 fc_disc_stop(lport);
696 lport->tt.rport_flush_queue();
697}
698
699/**
Robert Love34f42a02009-02-27 10:55:45 -0800700 * fc_disc_init() - Initialize the discovery block
Robert Love42e9a922008-12-09 15:10:17 -0800701 * @lport: FC local port
702 */
703int fc_disc_init(struct fc_lport *lport)
704{
705 struct fc_disc *disc;
706
707 if (!lport->tt.disc_start)
708 lport->tt.disc_start = fc_disc_start;
709
710 if (!lport->tt.disc_stop)
711 lport->tt.disc_stop = fc_disc_stop;
712
713 if (!lport->tt.disc_stop_final)
714 lport->tt.disc_stop_final = fc_disc_stop_final;
715
716 if (!lport->tt.disc_recv_req)
717 lport->tt.disc_recv_req = fc_disc_recv_req;
718
Robert Love42e9a922008-12-09 15:10:17 -0800719 disc = &lport->disc;
720 INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
721 mutex_init(&disc->disc_mutex);
722 INIT_LIST_HEAD(&disc->rports);
Robert Love42e9a922008-12-09 15:10:17 -0800723
724 disc->lport = lport;
Robert Love42e9a922008-12-09 15:10:17 -0800725
726 return 0;
727}
728EXPORT_SYMBOL(fc_disc_init);