blob: e6b13bfecf735b017bf0420c8158dd31a3fb716a [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_lookup_rport() - lookup a remote port by port_id
Robert Love42e9a922008-12-09 15:10:17 -080057 * @lport: Fibre Channel host port instance
58 * @port_id: remote port port_id to match
59 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -070060struct fc_rport_priv *fc_disc_lookup_rport(const struct fc_lport *lport,
61 u32 port_id)
Robert Love42e9a922008-12-09 15:10:17 -080062{
63 const struct fc_disc *disc = &lport->disc;
Joe Eykholtab28f1f2009-08-25 14:00:34 -070064 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -080065
66 list_for_each_entry(rdata, &disc->rports, peers) {
Joe Eykholt9e9d0452009-08-25 14:01:18 -070067 if (rdata->ids.port_id == port_id &&
68 rdata->rp_state != RPORT_ST_DELETE)
Joe Eykholt9fb9d322009-08-25 14:00:50 -070069 return rdata;
Robert Love42e9a922008-12-09 15:10:17 -080070 }
Joe Eykholt9fb9d322009-08-25 14:00:50 -070071 return NULL;
Robert Love42e9a922008-12-09 15:10:17 -080072}
73
74/**
Robert Love34f42a02009-02-27 10:55:45 -080075 * fc_disc_stop_rports() - delete all the remote ports associated with the lport
Robert Love42e9a922008-12-09 15:10:17 -080076 * @disc: The discovery job to stop rports on
77 *
78 * Locking Note: This function expects that the lport mutex is locked before
79 * calling it.
80 */
81void fc_disc_stop_rports(struct fc_disc *disc)
82{
83 struct fc_lport *lport;
Joe Eykholtab28f1f2009-08-25 14:00:34 -070084 struct fc_rport_priv *rdata, *next;
Robert Love42e9a922008-12-09 15:10:17 -080085
86 lport = disc->lport;
87
88 mutex_lock(&disc->disc_mutex);
Joe Eykholt9e9d0452009-08-25 14:01:18 -070089 list_for_each_entry_safe(rdata, next, &disc->rports, peers)
Joe Eykholt9fb9d322009-08-25 14:00:50 -070090 lport->tt.rport_logoff(rdata);
Robert Love42e9a922008-12-09 15:10:17 -080091 mutex_unlock(&disc->disc_mutex);
92}
93
94/**
Robert Love34f42a02009-02-27 10:55:45 -080095 * fc_disc_rport_callback() - Event handler for rport events
Robert Love42e9a922008-12-09 15:10:17 -080096 * @lport: The lport which is receiving the event
Joe Eykholt9fb9d322009-08-25 14:00:50 -070097 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -080098 * @event: The event that occured
99 *
100 * Locking Note: The rport lock should not be held when calling
101 * this function.
102 */
103static void fc_disc_rport_callback(struct fc_lport *lport,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700104 struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800105 enum fc_rport_event event)
106{
Robert Love42e9a922008-12-09 15:10:17 -0800107 struct fc_disc *disc = &lport->disc;
Robert Love42e9a922008-12-09 15:10:17 -0800108
Robert Love74147052009-06-10 15:31:10 -0700109 FC_DISC_DBG(disc, "Received a %d event for port (%6x)\n", event,
Joe Eykholtf211fa52009-08-25 14:01:01 -0700110 rdata->ids.port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800111
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700112 switch (event) {
Joe Eykholt4c0f62b2009-08-25 14:01:12 -0700113 case RPORT_EV_READY:
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700114 break;
115 case RPORT_EV_LOGO:
116 case RPORT_EV_FAILED:
117 case RPORT_EV_STOP:
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700118 break;
119 default:
120 break;
Robert Love42e9a922008-12-09 15:10:17 -0800121 }
122
Robert Love42e9a922008-12-09 15:10:17 -0800123}
124
125/**
Robert Love34f42a02009-02-27 10:55:45 -0800126 * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN)
Robert Love42e9a922008-12-09 15:10:17 -0800127 * @sp: Current sequence of the RSCN exchange
128 * @fp: RSCN Frame
129 * @lport: Fibre Channel host port instance
130 *
131 * Locking Note: This function expects that the disc_mutex is locked
132 * before it is called.
133 */
134static void fc_disc_recv_rscn_req(struct fc_seq *sp, struct fc_frame *fp,
135 struct fc_disc *disc)
136{
137 struct fc_lport *lport;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700138 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800139 struct fc_els_rscn *rp;
140 struct fc_els_rscn_page *pp;
141 struct fc_seq_els_data rjt_data;
142 unsigned int len;
143 int redisc = 0;
144 enum fc_els_rscn_ev_qual ev_qual;
145 enum fc_els_rscn_addr_fmt fmt;
146 LIST_HEAD(disc_ports);
147 struct fc_disc_port *dp, *next;
148
149 lport = disc->lport;
150
Robert Love74147052009-06-10 15:31:10 -0700151 FC_DISC_DBG(disc, "Received an RSCN event\n");
Robert Love42e9a922008-12-09 15:10:17 -0800152
153 /* make sure the frame contains an RSCN message */
154 rp = fc_frame_payload_get(fp, sizeof(*rp));
155 if (!rp)
156 goto reject;
157 /* make sure the page length is as expected (4 bytes) */
158 if (rp->rscn_page_len != sizeof(*pp))
159 goto reject;
160 /* get the RSCN payload length */
161 len = ntohs(rp->rscn_plen);
162 if (len < sizeof(*rp))
163 goto reject;
164 /* make sure the frame contains the expected payload */
165 rp = fc_frame_payload_get(fp, len);
166 if (!rp)
167 goto reject;
168 /* payload must be a multiple of the RSCN page size */
169 len -= sizeof(*rp);
170 if (len % sizeof(*pp))
171 goto reject;
172
173 for (pp = (void *)(rp + 1); len > 0; len -= sizeof(*pp), pp++) {
174 ev_qual = pp->rscn_page_flags >> ELS_RSCN_EV_QUAL_BIT;
175 ev_qual &= ELS_RSCN_EV_QUAL_MASK;
176 fmt = pp->rscn_page_flags >> ELS_RSCN_ADDR_FMT_BIT;
177 fmt &= ELS_RSCN_ADDR_FMT_MASK;
178 /*
179 * if we get an address format other than port
180 * (area, domain, fabric), then do a full discovery
181 */
182 switch (fmt) {
183 case ELS_ADDR_FMT_PORT:
Robert Love74147052009-06-10 15:31:10 -0700184 FC_DISC_DBG(disc, "Port address format for port "
185 "(%6x)\n", ntoh24(pp->rscn_fid));
Robert Love42e9a922008-12-09 15:10:17 -0800186 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
187 if (!dp) {
188 redisc = 1;
189 break;
190 }
191 dp->lp = lport;
192 dp->ids.port_id = ntoh24(pp->rscn_fid);
193 dp->ids.port_name = -1;
194 dp->ids.node_name = -1;
195 dp->ids.roles = FC_RPORT_ROLE_UNKNOWN;
196 list_add_tail(&dp->peers, &disc_ports);
197 break;
198 case ELS_ADDR_FMT_AREA:
199 case ELS_ADDR_FMT_DOM:
200 case ELS_ADDR_FMT_FAB:
201 default:
Robert Love74147052009-06-10 15:31:10 -0700202 FC_DISC_DBG(disc, "Address format is (%d)\n", fmt);
Robert Love42e9a922008-12-09 15:10:17 -0800203 redisc = 1;
204 break;
205 }
206 }
207 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
208 if (redisc) {
Robert Love74147052009-06-10 15:31:10 -0700209 FC_DISC_DBG(disc, "RSCN received: rediscovering\n");
Robert Love42e9a922008-12-09 15:10:17 -0800210 fc_disc_restart(disc);
211 } else {
Robert Love74147052009-06-10 15:31:10 -0700212 FC_DISC_DBG(disc, "RSCN received: not rediscovering. "
213 "redisc %d state %d in_prog %d\n",
214 redisc, lport->state, disc->pending);
Robert Love42e9a922008-12-09 15:10:17 -0800215 list_for_each_entry_safe(dp, next, &disc_ports, peers) {
216 list_del(&dp->peers);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700217 rdata = lport->tt.rport_lookup(lport, dp->ids.port_id);
218 if (rdata) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700219 lport->tt.rport_logoff(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800220 }
221 fc_disc_single(disc, dp);
222 }
223 }
224 fc_frame_free(fp);
225 return;
226reject:
Robert Love74147052009-06-10 15:31:10 -0700227 FC_DISC_DBG(disc, "Received a bad RSCN frame\n");
Robert Love42e9a922008-12-09 15:10:17 -0800228 rjt_data.fp = NULL;
229 rjt_data.reason = ELS_RJT_LOGIC;
230 rjt_data.explan = ELS_EXPL_NONE;
231 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
232 fc_frame_free(fp);
233}
234
235/**
Robert Love34f42a02009-02-27 10:55:45 -0800236 * fc_disc_recv_req() - Handle incoming requests
Robert Love42e9a922008-12-09 15:10:17 -0800237 * @sp: Current sequence of the request exchange
238 * @fp: The frame
239 * @lport: The FC local port
240 *
241 * Locking Note: This function is called from the EM and will lock
242 * the disc_mutex before calling the handler for the
243 * request.
244 */
245static void fc_disc_recv_req(struct fc_seq *sp, struct fc_frame *fp,
246 struct fc_lport *lport)
247{
248 u8 op;
249 struct fc_disc *disc = &lport->disc;
250
251 op = fc_frame_payload_op(fp);
252 switch (op) {
253 case ELS_RSCN:
254 mutex_lock(&disc->disc_mutex);
255 fc_disc_recv_rscn_req(sp, fp, disc);
256 mutex_unlock(&disc->disc_mutex);
257 break;
258 default:
Robert Love74147052009-06-10 15:31:10 -0700259 FC_DISC_DBG(disc, "Received an unsupported request, "
260 "the opcode is (%x)\n", op);
Robert Love42e9a922008-12-09 15:10:17 -0800261 break;
262 }
263}
264
265/**
Robert Love34f42a02009-02-27 10:55:45 -0800266 * fc_disc_restart() - Restart discovery
Robert Love42e9a922008-12-09 15:10:17 -0800267 * @lport: FC discovery context
268 *
269 * Locking Note: This function expects that the disc mutex
270 * is already locked.
271 */
272static void fc_disc_restart(struct fc_disc *disc)
273{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700274 struct fc_rport_priv *rdata, *next;
Robert Love42e9a922008-12-09 15:10:17 -0800275 struct fc_lport *lport = disc->lport;
276
Robert Love74147052009-06-10 15:31:10 -0700277 FC_DISC_DBG(disc, "Restarting discovery\n");
Robert Love42e9a922008-12-09 15:10:17 -0800278
Joe Eykholt9e9d0452009-08-25 14:01:18 -0700279 list_for_each_entry_safe(rdata, next, &disc->rports, peers)
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700280 lport->tt.rport_logoff(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800281
282 disc->requested = 1;
283 if (!disc->pending)
284 fc_disc_gpn_ft_req(disc);
285}
286
287/**
Robert Love34f42a02009-02-27 10:55:45 -0800288 * fc_disc_start() - Fibre Channel Target discovery
Robert Love42e9a922008-12-09 15:10:17 -0800289 * @lport: FC local port
290 *
291 * Returns non-zero if discovery cannot be started.
292 */
293static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
294 enum fc_disc_event),
295 struct fc_lport *lport)
296{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700297 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800298 struct fc_disc *disc = &lport->disc;
299
300 /*
301 * At this point we may have a new disc job or an existing
302 * one. Either way, let's lock when we make changes to it
303 * and send the GPN_FT request.
304 */
305 mutex_lock(&disc->disc_mutex);
306
307 disc->disc_callback = disc_callback;
308
309 /*
310 * If not ready, or already running discovery, just set request flag.
311 */
312 disc->requested = 1;
313
314 if (disc->pending) {
315 mutex_unlock(&disc->disc_mutex);
316 return;
317 }
318
319 /*
320 * Handle point-to-point mode as a simple discovery
321 * of the remote port. Yucky, yucky, yuck, yuck!
322 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700323 rdata = disc->lport->ptp_rp;
324 if (rdata) {
Joe Eykholtf211fa52009-08-25 14:01:01 -0700325 kref_get(&rdata->kref);
326 if (!fc_disc_new_target(disc, rdata, &rdata->ids)) {
Joe Eykholt786681b2009-08-25 14:01:29 -0700327 fc_disc_done(disc, DISC_EV_SUCCESS);
Robert Love42e9a922008-12-09 15:10:17 -0800328 }
Joe Eykholtf211fa52009-08-25 14:01:01 -0700329 kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy);
Robert Love42e9a922008-12-09 15:10:17 -0800330 } else {
331 fc_disc_gpn_ft_req(disc); /* get ports by FC-4 type */
332 }
333
334 mutex_unlock(&disc->disc_mutex);
335}
336
337static struct fc_rport_operations fc_disc_rport_ops = {
338 .event_callback = fc_disc_rport_callback,
339};
340
341/**
Robert Love34f42a02009-02-27 10:55:45 -0800342 * fc_disc_new_target() - Handle new target found by discovery
Robert Love42e9a922008-12-09 15:10:17 -0800343 * @lport: FC local port
Joe Eykholtf211fa52009-08-25 14:01:01 -0700344 * @rdata: The previous FC remote port priv (NULL if new remote port)
Robert Love42e9a922008-12-09 15:10:17 -0800345 * @ids: Identifiers for the new FC remote port
346 *
347 * Locking Note: This function expects that the disc_mutex is locked
348 * before it is called.
349 */
350static int fc_disc_new_target(struct fc_disc *disc,
Joe Eykholtf211fa52009-08-25 14:01:01 -0700351 struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800352 struct fc_rport_identifiers *ids)
353{
354 struct fc_lport *lport = disc->lport;
Robert Love42e9a922008-12-09 15:10:17 -0800355 int error = 0;
356
Joe Eykholtf211fa52009-08-25 14:01:01 -0700357 if (rdata && ids->port_name) {
358 if (rdata->ids.port_name == -1) {
Robert Love42e9a922008-12-09 15:10:17 -0800359 /*
360 * Set WWN and fall through to notify of create.
361 */
Joe Eykholtf211fa52009-08-25 14:01:01 -0700362 rdata->ids.port_name = ids->port_name;
363 rdata->ids.node_name = ids->node_name;
364 } else if (rdata->ids.port_name != ids->port_name) {
Robert Love42e9a922008-12-09 15:10:17 -0800365 /*
366 * This is a new port with the same FCID as
367 * a previously-discovered port. Presumably the old
368 * port logged out and a new port logged in and was
369 * assigned the same FCID. This should be rare.
370 * Delete the old one and fall thru to re-create.
371 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700372 lport->tt.rport_logoff(rdata);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700373 rdata = NULL;
Robert Love42e9a922008-12-09 15:10:17 -0800374 }
375 }
376 if (((ids->port_name != -1) || (ids->port_id != -1)) &&
377 ids->port_id != fc_host_port_id(lport->host) &&
378 ids->port_name != lport->wwpn) {
Joe Eykholtf211fa52009-08-25 14:01:01 -0700379 if (!rdata) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700380 rdata = lport->tt.rport_lookup(lport, ids->port_id);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700381 if (!rdata) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700382 rdata = lport->tt.rport_create(lport, ids);
Joe Eykholtf211fa52009-08-25 14:01:01 -0700383 if (!rdata)
384 error = -ENOMEM;
Robert Love42e9a922008-12-09 15:10:17 -0800385 }
Robert Love42e9a922008-12-09 15:10:17 -0800386 }
Joe Eykholtf211fa52009-08-25 14:01:01 -0700387 if (rdata) {
Robert Loved3b33322009-02-27 10:55:29 -0800388 rdata->ops = &fc_disc_rport_ops;
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700389 lport->tt.rport_login(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800390 }
391 }
392 return error;
393}
394
395/**
Robert Love34f42a02009-02-27 10:55:45 -0800396 * fc_disc_done() - Discovery has been completed
Robert Love42e9a922008-12-09 15:10:17 -0800397 * @disc: FC discovery context
Joe Eykholt786681b2009-08-25 14:01:29 -0700398 * @event: discovery completion status
399 *
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700400 * Locking Note: This function expects that the disc mutex is locked before
401 * it is called. The discovery callback is then made with the lock released,
402 * and the lock is re-taken before returning from this function
Robert Love42e9a922008-12-09 15:10:17 -0800403 */
Joe Eykholt786681b2009-08-25 14:01:29 -0700404static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event)
Robert Love42e9a922008-12-09 15:10:17 -0800405{
406 struct fc_lport *lport = disc->lport;
407
Robert Love74147052009-06-10 15:31:10 -0700408 FC_DISC_DBG(disc, "Discovery complete\n");
Robert Love42e9a922008-12-09 15:10:17 -0800409
Robert Love42e9a922008-12-09 15:10:17 -0800410 if (disc->requested)
411 fc_disc_gpn_ft_req(disc);
412 else
413 disc->pending = 0;
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700414
415 mutex_unlock(&disc->disc_mutex);
416 disc->disc_callback(lport, event);
417 mutex_lock(&disc->disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800418}
419
420/**
Robert Love34f42a02009-02-27 10:55:45 -0800421 * fc_disc_error() - Handle error on dNS request
Robert Love42e9a922008-12-09 15:10:17 -0800422 * @disc: FC discovery context
423 * @fp: The frame pointer
424 */
425static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
426{
427 struct fc_lport *lport = disc->lport;
428 unsigned long delay = 0;
Robert Love74147052009-06-10 15:31:10 -0700429
430 FC_DISC_DBG(disc, "Error %ld, retries %d/%d\n",
431 PTR_ERR(fp), disc->retry_count,
432 FC_DISC_RETRY_LIMIT);
Robert Love42e9a922008-12-09 15:10:17 -0800433
434 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
435 /*
436 * Memory allocation failure, or the exchange timed out,
437 * retry after delay.
438 */
439 if (disc->retry_count < FC_DISC_RETRY_LIMIT) {
440 /* go ahead and retry */
441 if (!fp)
442 delay = msecs_to_jiffies(FC_DISC_RETRY_DELAY);
443 else {
444 delay = msecs_to_jiffies(lport->e_d_tov);
445
446 /* timeout faster first time */
447 if (!disc->retry_count)
448 delay /= 4;
449 }
450 disc->retry_count++;
451 schedule_delayed_work(&disc->disc_work, delay);
Joe Eykholt786681b2009-08-25 14:01:29 -0700452 } else
453 fc_disc_done(disc, DISC_EV_FAILED);
Robert Love42e9a922008-12-09 15:10:17 -0800454 }
455}
456
457/**
Robert Love34f42a02009-02-27 10:55:45 -0800458 * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
Robert Love42e9a922008-12-09 15:10:17 -0800459 * @lport: FC discovery context
460 *
461 * Locking Note: This function expects that the disc_mutex is locked
462 * before it is called.
463 */
464static void fc_disc_gpn_ft_req(struct fc_disc *disc)
465{
466 struct fc_frame *fp;
467 struct fc_lport *lport = disc->lport;
468
469 WARN_ON(!fc_lport_test_ready(lport));
470
471 disc->pending = 1;
472 disc->requested = 0;
473
474 disc->buf_len = 0;
475 disc->seq_count = 0;
476 fp = fc_frame_alloc(lport,
477 sizeof(struct fc_ct_hdr) +
478 sizeof(struct fc_ns_gid_ft));
479 if (!fp)
480 goto err;
481
Joe Eykholta46f3272009-08-25 14:00:55 -0700482 if (lport->tt.elsct_send(lport, 0, fp,
Robert Love42e9a922008-12-09 15:10:17 -0800483 FC_NS_GPN_FT,
484 fc_disc_gpn_ft_resp,
485 disc, lport->e_d_tov))
486 return;
487err:
488 fc_disc_error(disc, fp);
489}
490
491/**
Joe Eykholt786681b2009-08-25 14:01:29 -0700492 * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response.
Robert Love42e9a922008-12-09 15:10:17 -0800493 * @lport: Fibre Channel host port instance
494 * @buf: GPN_FT response buffer
495 * @len: size of response buffer
Joe Eykholt786681b2009-08-25 14:01:29 -0700496 *
497 * Goes through the list of IDs and names resulting from a request.
Robert Love42e9a922008-12-09 15:10:17 -0800498 */
499static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
500{
501 struct fc_lport *lport;
502 struct fc_gpn_ft_resp *np;
503 char *bp;
504 size_t plen;
505 size_t tlen;
506 int error = 0;
Joe Eykholt795d86f2009-08-25 14:00:39 -0700507 struct fc_rport_identifiers ids;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700508 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800509
510 lport = disc->lport;
511
512 /*
513 * Handle partial name record left over from previous call.
514 */
515 bp = buf;
516 plen = len;
517 np = (struct fc_gpn_ft_resp *)bp;
518 tlen = disc->buf_len;
519 if (tlen) {
520 WARN_ON(tlen >= sizeof(*np));
521 plen = sizeof(*np) - tlen;
522 WARN_ON(plen <= 0);
523 WARN_ON(plen >= sizeof(*np));
524 if (plen > len)
525 plen = len;
526 np = &disc->partial_buf;
527 memcpy((char *)np + tlen, bp, plen);
528
529 /*
530 * Set bp so that the loop below will advance it to the
531 * first valid full name element.
532 */
533 bp -= tlen;
534 len += tlen;
535 plen += tlen;
536 disc->buf_len = (unsigned char) plen;
537 if (plen == sizeof(*np))
538 disc->buf_len = 0;
539 }
540
541 /*
542 * Handle full name records, including the one filled from above.
543 * Normally, np == bp and plen == len, but from the partial case above,
544 * bp, len describe the overall buffer, and np, plen describe the
545 * partial buffer, which if would usually be full now.
546 * After the first time through the loop, things return to "normal".
547 */
548 while (plen >= sizeof(*np)) {
Joe Eykholt795d86f2009-08-25 14:00:39 -0700549 ids.port_id = ntoh24(np->fp_fid);
550 ids.port_name = ntohll(np->fp_wwpn);
551 ids.node_name = -1;
552 ids.roles = FC_RPORT_ROLE_UNKNOWN;
Robert Love42e9a922008-12-09 15:10:17 -0800553
Joe Eykholt795d86f2009-08-25 14:00:39 -0700554 if (ids.port_id != fc_host_port_id(lport->host) &&
555 ids.port_name != lport->wwpn) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700556 rdata = lport->tt.rport_create(lport, &ids);
557 if (rdata) {
Robert Love42e9a922008-12-09 15:10:17 -0800558 rdata->ops = &fc_disc_rport_ops;
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700559 lport->tt.rport_login(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800560 } else
Robert Love74147052009-06-10 15:31:10 -0700561 printk(KERN_WARNING "libfc: Failed to allocate "
562 "memory for the newly discovered port "
Joe Eykholt795d86f2009-08-25 14:00:39 -0700563 "(%6x)\n", ids.port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800564 }
565
566 if (np->fp_flags & FC_NS_FID_LAST) {
Joe Eykholt786681b2009-08-25 14:01:29 -0700567 fc_disc_done(disc, DISC_EV_SUCCESS);
Robert Love42e9a922008-12-09 15:10:17 -0800568 len = 0;
569 break;
570 }
571 len -= sizeof(*np);
572 bp += sizeof(*np);
573 np = (struct fc_gpn_ft_resp *)bp;
574 plen = len;
575 }
576
577 /*
578 * Save any partial record at the end of the buffer for next time.
579 */
580 if (error == 0 && len > 0 && len < sizeof(*np)) {
581 if (np != &disc->partial_buf) {
Robert Love74147052009-06-10 15:31:10 -0700582 FC_DISC_DBG(disc, "Partial buffer remains "
583 "for discovery\n");
Robert Love42e9a922008-12-09 15:10:17 -0800584 memcpy(&disc->partial_buf, np, len);
585 }
586 disc->buf_len = (unsigned char) len;
587 } else {
588 disc->buf_len = 0;
589 }
590 return error;
591}
592
Robert Love34f42a02009-02-27 10:55:45 -0800593/**
594 * fc_disc_timeout() - Retry handler for the disc component
595 * @work: Structure holding disc obj that needs retry discovery
596 *
Robert Love42e9a922008-12-09 15:10:17 -0800597 * Handle retry of memory allocation for remote ports.
598 */
599static void fc_disc_timeout(struct work_struct *work)
600{
601 struct fc_disc *disc = container_of(work,
602 struct fc_disc,
603 disc_work.work);
604 mutex_lock(&disc->disc_mutex);
605 if (disc->requested && !disc->pending)
606 fc_disc_gpn_ft_req(disc);
607 mutex_unlock(&disc->disc_mutex);
608}
609
610/**
Robert Love34f42a02009-02-27 10:55:45 -0800611 * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
Robert Love42e9a922008-12-09 15:10:17 -0800612 * @sp: Current sequence of GPN_FT exchange
613 * @fp: response frame
614 * @lp_arg: Fibre Channel host port instance
615 *
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700616 * Locking Note: This function is called without disc mutex held, and
617 * should do all its processing with the mutex held
Robert Love42e9a922008-12-09 15:10:17 -0800618 */
619static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp,
620 void *disc_arg)
621{
622 struct fc_disc *disc = disc_arg;
623 struct fc_ct_hdr *cp;
624 struct fc_frame_header *fh;
625 unsigned int seq_cnt;
626 void *buf = NULL;
627 unsigned int len;
628 int error;
629
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700630 mutex_lock(&disc->disc_mutex);
Robert Love74147052009-06-10 15:31:10 -0700631 FC_DISC_DBG(disc, "Received a GPN_FT response\n");
Robert Love42e9a922008-12-09 15:10:17 -0800632
633 if (IS_ERR(fp)) {
634 fc_disc_error(disc, fp);
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700635 mutex_unlock(&disc->disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800636 return;
637 }
638
639 WARN_ON(!fc_frame_is_linear(fp)); /* buffer must be contiguous */
640 fh = fc_frame_header_get(fp);
641 len = fr_len(fp) - sizeof(*fh);
642 seq_cnt = ntohs(fh->fh_seq_cnt);
643 if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 &&
644 disc->seq_count == 0) {
645 cp = fc_frame_payload_get(fp, sizeof(*cp));
646 if (!cp) {
Robert Love74147052009-06-10 15:31:10 -0700647 FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n",
648 fr_len(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800649 } else if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
650
Robert Love34f42a02009-02-27 10:55:45 -0800651 /* Accepted, parse the response. */
Robert Love42e9a922008-12-09 15:10:17 -0800652 buf = cp + 1;
653 len -= sizeof(*cp);
654 } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
Robert Love74147052009-06-10 15:31:10 -0700655 FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x "
656 "(check zoning)\n", cp->ct_reason,
657 cp->ct_explan);
Joe Eykholt786681b2009-08-25 14:01:29 -0700658 fc_disc_done(disc, DISC_EV_FAILED);
Robert Love42e9a922008-12-09 15:10:17 -0800659 } else {
Robert Love74147052009-06-10 15:31:10 -0700660 FC_DISC_DBG(disc, "GPN_FT unexpected response code "
661 "%x\n", ntohs(cp->ct_cmd));
Robert Love42e9a922008-12-09 15:10:17 -0800662 }
663 } else if (fr_sof(fp) == FC_SOF_N3 &&
664 seq_cnt == disc->seq_count) {
665 buf = fh + 1;
666 } else {
Robert Love74147052009-06-10 15:31:10 -0700667 FC_DISC_DBG(disc, "GPN_FT unexpected frame - out of sequence? "
668 "seq_cnt %x expected %x sof %x eof %x\n",
669 seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800670 }
671 if (buf) {
672 error = fc_disc_gpn_ft_parse(disc, buf, len);
673 if (error)
674 fc_disc_error(disc, fp);
675 else
676 disc->seq_count++;
677 }
678 fc_frame_free(fp);
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700679
680 mutex_unlock(&disc->disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800681}
682
683/**
Robert Love34f42a02009-02-27 10:55:45 -0800684 * fc_disc_single() - Discover the directory information for a single target
Robert Love42e9a922008-12-09 15:10:17 -0800685 * @lport: FC local port
686 * @dp: The port to rediscover
687 *
688 * Locking Note: This function expects that the disc_mutex is locked
689 * before it is called.
690 */
691static void fc_disc_single(struct fc_disc *disc, struct fc_disc_port *dp)
692{
693 struct fc_lport *lport;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700694 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800695
696 lport = disc->lport;
697
698 if (dp->ids.port_id == fc_host_port_id(lport->host))
699 goto out;
700
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700701 rdata = lport->tt.rport_create(lport, &dp->ids);
702 if (rdata) {
Robert Love42e9a922008-12-09 15:10:17 -0800703 rdata->ops = &fc_disc_rport_ops;
704 kfree(dp);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700705 lport->tt.rport_login(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800706 }
707 return;
708out:
709 kfree(dp);
710}
711
712/**
Robert Love34f42a02009-02-27 10:55:45 -0800713 * fc_disc_stop() - Stop discovery for a given lport
Robert Love42e9a922008-12-09 15:10:17 -0800714 * @lport: The lport that discovery should stop for
715 */
716void fc_disc_stop(struct fc_lport *lport)
717{
718 struct fc_disc *disc = &lport->disc;
719
720 if (disc) {
721 cancel_delayed_work_sync(&disc->disc_work);
722 fc_disc_stop_rports(disc);
723 }
724}
725
726/**
Robert Love34f42a02009-02-27 10:55:45 -0800727 * fc_disc_stop_final() - Stop discovery for a given lport
Robert Love42e9a922008-12-09 15:10:17 -0800728 * @lport: The lport that discovery should stop for
729 *
730 * This function will block until discovery has been
731 * completely stopped and all rports have been deleted.
732 */
733void fc_disc_stop_final(struct fc_lport *lport)
734{
735 fc_disc_stop(lport);
736 lport->tt.rport_flush_queue();
737}
738
739/**
Robert Love34f42a02009-02-27 10:55:45 -0800740 * fc_disc_init() - Initialize the discovery block
Robert Love42e9a922008-12-09 15:10:17 -0800741 * @lport: FC local port
742 */
743int fc_disc_init(struct fc_lport *lport)
744{
745 struct fc_disc *disc;
746
747 if (!lport->tt.disc_start)
748 lport->tt.disc_start = fc_disc_start;
749
750 if (!lport->tt.disc_stop)
751 lport->tt.disc_stop = fc_disc_stop;
752
753 if (!lport->tt.disc_stop_final)
754 lport->tt.disc_stop_final = fc_disc_stop_final;
755
756 if (!lport->tt.disc_recv_req)
757 lport->tt.disc_recv_req = fc_disc_recv_req;
758
759 if (!lport->tt.rport_lookup)
760 lport->tt.rport_lookup = fc_disc_lookup_rport;
761
762 disc = &lport->disc;
763 INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
764 mutex_init(&disc->disc_mutex);
765 INIT_LIST_HEAD(&disc->rports);
Robert Love42e9a922008-12-09 15:10:17 -0800766
767 disc->lport = lport;
Robert Love42e9a922008-12-09 15:10:17 -0800768
769 return 0;
770}
771EXPORT_SYMBOL(fc_disc_init);