blob: 266aa1ea01e481f996c9b0dd2d61922aecefc23d [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 Eykholt19f97e32009-08-25 14:01:55 -0700380 rdata = lport->tt.rport_create(lport, ids);
381 if (!rdata)
382 error = -ENOMEM;
Robert Love42e9a922008-12-09 15:10:17 -0800383 }
Joe Eykholtf211fa52009-08-25 14:01:01 -0700384 if (rdata) {
Robert Loved3b33322009-02-27 10:55:29 -0800385 rdata->ops = &fc_disc_rport_ops;
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700386 lport->tt.rport_login(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800387 }
388 }
389 return error;
390}
391
392/**
Robert Love34f42a02009-02-27 10:55:45 -0800393 * fc_disc_done() - Discovery has been completed
Robert Love42e9a922008-12-09 15:10:17 -0800394 * @disc: FC discovery context
Joe Eykholt786681b2009-08-25 14:01:29 -0700395 * @event: discovery completion status
396 *
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700397 * Locking Note: This function expects that the disc mutex is locked before
398 * it is called. The discovery callback is then made with the lock released,
399 * and the lock is re-taken before returning from this function
Robert Love42e9a922008-12-09 15:10:17 -0800400 */
Joe Eykholt786681b2009-08-25 14:01:29 -0700401static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event)
Robert Love42e9a922008-12-09 15:10:17 -0800402{
403 struct fc_lport *lport = disc->lport;
404
Robert Love74147052009-06-10 15:31:10 -0700405 FC_DISC_DBG(disc, "Discovery complete\n");
Robert Love42e9a922008-12-09 15:10:17 -0800406
Robert Love42e9a922008-12-09 15:10:17 -0800407 if (disc->requested)
408 fc_disc_gpn_ft_req(disc);
409 else
410 disc->pending = 0;
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700411
412 mutex_unlock(&disc->disc_mutex);
413 disc->disc_callback(lport, event);
414 mutex_lock(&disc->disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800415}
416
417/**
Robert Love34f42a02009-02-27 10:55:45 -0800418 * fc_disc_error() - Handle error on dNS request
Robert Love42e9a922008-12-09 15:10:17 -0800419 * @disc: FC discovery context
420 * @fp: The frame pointer
421 */
422static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
423{
424 struct fc_lport *lport = disc->lport;
425 unsigned long delay = 0;
Robert Love74147052009-06-10 15:31:10 -0700426
427 FC_DISC_DBG(disc, "Error %ld, retries %d/%d\n",
428 PTR_ERR(fp), disc->retry_count,
429 FC_DISC_RETRY_LIMIT);
Robert Love42e9a922008-12-09 15:10:17 -0800430
431 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
432 /*
433 * Memory allocation failure, or the exchange timed out,
434 * retry after delay.
435 */
436 if (disc->retry_count < FC_DISC_RETRY_LIMIT) {
437 /* go ahead and retry */
438 if (!fp)
439 delay = msecs_to_jiffies(FC_DISC_RETRY_DELAY);
440 else {
441 delay = msecs_to_jiffies(lport->e_d_tov);
442
443 /* timeout faster first time */
444 if (!disc->retry_count)
445 delay /= 4;
446 }
447 disc->retry_count++;
448 schedule_delayed_work(&disc->disc_work, delay);
Joe Eykholt786681b2009-08-25 14:01:29 -0700449 } else
450 fc_disc_done(disc, DISC_EV_FAILED);
Robert Love42e9a922008-12-09 15:10:17 -0800451 }
452}
453
454/**
Robert Love34f42a02009-02-27 10:55:45 -0800455 * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
Robert Love42e9a922008-12-09 15:10:17 -0800456 * @lport: FC discovery context
457 *
458 * Locking Note: This function expects that the disc_mutex is locked
459 * before it is called.
460 */
461static void fc_disc_gpn_ft_req(struct fc_disc *disc)
462{
463 struct fc_frame *fp;
464 struct fc_lport *lport = disc->lport;
465
466 WARN_ON(!fc_lport_test_ready(lport));
467
468 disc->pending = 1;
469 disc->requested = 0;
470
471 disc->buf_len = 0;
472 disc->seq_count = 0;
473 fp = fc_frame_alloc(lport,
474 sizeof(struct fc_ct_hdr) +
475 sizeof(struct fc_ns_gid_ft));
476 if (!fp)
477 goto err;
478
Joe Eykholta46f3272009-08-25 14:00:55 -0700479 if (lport->tt.elsct_send(lport, 0, fp,
Robert Love42e9a922008-12-09 15:10:17 -0800480 FC_NS_GPN_FT,
481 fc_disc_gpn_ft_resp,
482 disc, lport->e_d_tov))
483 return;
484err:
485 fc_disc_error(disc, fp);
486}
487
488/**
Joe Eykholt786681b2009-08-25 14:01:29 -0700489 * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response.
Robert Love42e9a922008-12-09 15:10:17 -0800490 * @lport: Fibre Channel host port instance
491 * @buf: GPN_FT response buffer
492 * @len: size of response buffer
Joe Eykholt786681b2009-08-25 14:01:29 -0700493 *
494 * Goes through the list of IDs and names resulting from a request.
Robert Love42e9a922008-12-09 15:10:17 -0800495 */
496static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
497{
498 struct fc_lport *lport;
499 struct fc_gpn_ft_resp *np;
500 char *bp;
501 size_t plen;
502 size_t tlen;
503 int error = 0;
Joe Eykholt795d86f2009-08-25 14:00:39 -0700504 struct fc_rport_identifiers ids;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700505 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800506
507 lport = disc->lport;
508
509 /*
510 * Handle partial name record left over from previous call.
511 */
512 bp = buf;
513 plen = len;
514 np = (struct fc_gpn_ft_resp *)bp;
515 tlen = disc->buf_len;
516 if (tlen) {
517 WARN_ON(tlen >= sizeof(*np));
518 plen = sizeof(*np) - tlen;
519 WARN_ON(plen <= 0);
520 WARN_ON(plen >= sizeof(*np));
521 if (plen > len)
522 plen = len;
523 np = &disc->partial_buf;
524 memcpy((char *)np + tlen, bp, plen);
525
526 /*
527 * Set bp so that the loop below will advance it to the
528 * first valid full name element.
529 */
530 bp -= tlen;
531 len += tlen;
532 plen += tlen;
533 disc->buf_len = (unsigned char) plen;
534 if (plen == sizeof(*np))
535 disc->buf_len = 0;
536 }
537
538 /*
539 * Handle full name records, including the one filled from above.
540 * Normally, np == bp and plen == len, but from the partial case above,
541 * bp, len describe the overall buffer, and np, plen describe the
542 * partial buffer, which if would usually be full now.
543 * After the first time through the loop, things return to "normal".
544 */
545 while (plen >= sizeof(*np)) {
Joe Eykholt795d86f2009-08-25 14:00:39 -0700546 ids.port_id = ntoh24(np->fp_fid);
547 ids.port_name = ntohll(np->fp_wwpn);
548 ids.node_name = -1;
549 ids.roles = FC_RPORT_ROLE_UNKNOWN;
Robert Love42e9a922008-12-09 15:10:17 -0800550
Joe Eykholt795d86f2009-08-25 14:00:39 -0700551 if (ids.port_id != fc_host_port_id(lport->host) &&
552 ids.port_name != lport->wwpn) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700553 rdata = lport->tt.rport_create(lport, &ids);
554 if (rdata) {
Robert Love42e9a922008-12-09 15:10:17 -0800555 rdata->ops = &fc_disc_rport_ops;
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700556 lport->tt.rport_login(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800557 } else
Robert Love74147052009-06-10 15:31:10 -0700558 printk(KERN_WARNING "libfc: Failed to allocate "
559 "memory for the newly discovered port "
Joe Eykholt795d86f2009-08-25 14:00:39 -0700560 "(%6x)\n", ids.port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800561 }
562
563 if (np->fp_flags & FC_NS_FID_LAST) {
Joe Eykholt786681b2009-08-25 14:01:29 -0700564 fc_disc_done(disc, DISC_EV_SUCCESS);
Robert Love42e9a922008-12-09 15:10:17 -0800565 len = 0;
566 break;
567 }
568 len -= sizeof(*np);
569 bp += sizeof(*np);
570 np = (struct fc_gpn_ft_resp *)bp;
571 plen = len;
572 }
573
574 /*
575 * Save any partial record at the end of the buffer for next time.
576 */
577 if (error == 0 && len > 0 && len < sizeof(*np)) {
578 if (np != &disc->partial_buf) {
Robert Love74147052009-06-10 15:31:10 -0700579 FC_DISC_DBG(disc, "Partial buffer remains "
580 "for discovery\n");
Robert Love42e9a922008-12-09 15:10:17 -0800581 memcpy(&disc->partial_buf, np, len);
582 }
583 disc->buf_len = (unsigned char) len;
584 } else {
585 disc->buf_len = 0;
586 }
587 return error;
588}
589
Robert Love34f42a02009-02-27 10:55:45 -0800590/**
591 * fc_disc_timeout() - Retry handler for the disc component
592 * @work: Structure holding disc obj that needs retry discovery
593 *
Robert Love42e9a922008-12-09 15:10:17 -0800594 * Handle retry of memory allocation for remote ports.
595 */
596static void fc_disc_timeout(struct work_struct *work)
597{
598 struct fc_disc *disc = container_of(work,
599 struct fc_disc,
600 disc_work.work);
601 mutex_lock(&disc->disc_mutex);
602 if (disc->requested && !disc->pending)
603 fc_disc_gpn_ft_req(disc);
604 mutex_unlock(&disc->disc_mutex);
605}
606
607/**
Robert Love34f42a02009-02-27 10:55:45 -0800608 * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
Robert Love42e9a922008-12-09 15:10:17 -0800609 * @sp: Current sequence of GPN_FT exchange
610 * @fp: response frame
611 * @lp_arg: Fibre Channel host port instance
612 *
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700613 * Locking Note: This function is called without disc mutex held, and
614 * should do all its processing with the mutex held
Robert Love42e9a922008-12-09 15:10:17 -0800615 */
616static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp,
617 void *disc_arg)
618{
619 struct fc_disc *disc = disc_arg;
620 struct fc_ct_hdr *cp;
621 struct fc_frame_header *fh;
622 unsigned int seq_cnt;
623 void *buf = NULL;
624 unsigned int len;
625 int error;
626
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700627 mutex_lock(&disc->disc_mutex);
Robert Love74147052009-06-10 15:31:10 -0700628 FC_DISC_DBG(disc, "Received a GPN_FT response\n");
Robert Love42e9a922008-12-09 15:10:17 -0800629
630 if (IS_ERR(fp)) {
631 fc_disc_error(disc, fp);
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700632 mutex_unlock(&disc->disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800633 return;
634 }
635
636 WARN_ON(!fc_frame_is_linear(fp)); /* buffer must be contiguous */
637 fh = fc_frame_header_get(fp);
638 len = fr_len(fp) - sizeof(*fh);
639 seq_cnt = ntohs(fh->fh_seq_cnt);
640 if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 &&
641 disc->seq_count == 0) {
642 cp = fc_frame_payload_get(fp, sizeof(*cp));
643 if (!cp) {
Robert Love74147052009-06-10 15:31:10 -0700644 FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n",
645 fr_len(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800646 } else if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
647
Robert Love34f42a02009-02-27 10:55:45 -0800648 /* Accepted, parse the response. */
Robert Love42e9a922008-12-09 15:10:17 -0800649 buf = cp + 1;
650 len -= sizeof(*cp);
651 } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
Robert Love74147052009-06-10 15:31:10 -0700652 FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x "
653 "(check zoning)\n", cp->ct_reason,
654 cp->ct_explan);
Joe Eykholt786681b2009-08-25 14:01:29 -0700655 fc_disc_done(disc, DISC_EV_FAILED);
Robert Love42e9a922008-12-09 15:10:17 -0800656 } else {
Robert Love74147052009-06-10 15:31:10 -0700657 FC_DISC_DBG(disc, "GPN_FT unexpected response code "
658 "%x\n", ntohs(cp->ct_cmd));
Robert Love42e9a922008-12-09 15:10:17 -0800659 }
660 } else if (fr_sof(fp) == FC_SOF_N3 &&
661 seq_cnt == disc->seq_count) {
662 buf = fh + 1;
663 } else {
Robert Love74147052009-06-10 15:31:10 -0700664 FC_DISC_DBG(disc, "GPN_FT unexpected frame - out of sequence? "
665 "seq_cnt %x expected %x sof %x eof %x\n",
666 seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800667 }
668 if (buf) {
669 error = fc_disc_gpn_ft_parse(disc, buf, len);
670 if (error)
671 fc_disc_error(disc, fp);
672 else
673 disc->seq_count++;
674 }
675 fc_frame_free(fp);
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700676
677 mutex_unlock(&disc->disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800678}
679
680/**
Robert Love34f42a02009-02-27 10:55:45 -0800681 * fc_disc_single() - Discover the directory information for a single target
Robert Love42e9a922008-12-09 15:10:17 -0800682 * @lport: FC local port
683 * @dp: The port to rediscover
684 *
685 * Locking Note: This function expects that the disc_mutex is locked
686 * before it is called.
687 */
688static void fc_disc_single(struct fc_disc *disc, struct fc_disc_port *dp)
689{
690 struct fc_lport *lport;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700691 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800692
693 lport = disc->lport;
694
695 if (dp->ids.port_id == fc_host_port_id(lport->host))
696 goto out;
697
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700698 rdata = lport->tt.rport_create(lport, &dp->ids);
699 if (rdata) {
Robert Love42e9a922008-12-09 15:10:17 -0800700 rdata->ops = &fc_disc_rport_ops;
701 kfree(dp);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700702 lport->tt.rport_login(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800703 }
704 return;
705out:
706 kfree(dp);
707}
708
709/**
Robert Love34f42a02009-02-27 10:55:45 -0800710 * fc_disc_stop() - Stop discovery for a given lport
Robert Love42e9a922008-12-09 15:10:17 -0800711 * @lport: The lport that discovery should stop for
712 */
713void fc_disc_stop(struct fc_lport *lport)
714{
715 struct fc_disc *disc = &lport->disc;
716
717 if (disc) {
718 cancel_delayed_work_sync(&disc->disc_work);
719 fc_disc_stop_rports(disc);
720 }
721}
722
723/**
Robert Love34f42a02009-02-27 10:55:45 -0800724 * fc_disc_stop_final() - Stop discovery for a given lport
Robert Love42e9a922008-12-09 15:10:17 -0800725 * @lport: The lport that discovery should stop for
726 *
727 * This function will block until discovery has been
728 * completely stopped and all rports have been deleted.
729 */
730void fc_disc_stop_final(struct fc_lport *lport)
731{
732 fc_disc_stop(lport);
733 lport->tt.rport_flush_queue();
734}
735
736/**
Robert Love34f42a02009-02-27 10:55:45 -0800737 * fc_disc_init() - Initialize the discovery block
Robert Love42e9a922008-12-09 15:10:17 -0800738 * @lport: FC local port
739 */
740int fc_disc_init(struct fc_lport *lport)
741{
742 struct fc_disc *disc;
743
744 if (!lport->tt.disc_start)
745 lport->tt.disc_start = fc_disc_start;
746
747 if (!lport->tt.disc_stop)
748 lport->tt.disc_stop = fc_disc_stop;
749
750 if (!lport->tt.disc_stop_final)
751 lport->tt.disc_stop_final = fc_disc_stop_final;
752
753 if (!lport->tt.disc_recv_req)
754 lport->tt.disc_recv_req = fc_disc_recv_req;
755
756 if (!lport->tt.rport_lookup)
757 lport->tt.rport_lookup = fc_disc_lookup_rport;
758
759 disc = &lport->disc;
760 INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
761 mutex_init(&disc->disc_mutex);
762 INIT_LIST_HEAD(&disc->rports);
Robert Love42e9a922008-12-09 15:10:17 -0800763
764 disc->lport = lport;
Robert Love42e9a922008-12-09 15:10:17 -0800765
766 return 0;
767}
768EXPORT_SYMBOL(fc_disc_init);