blob: 448ffc388656820962685dd932980ba3f97c60ea [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
46#define FC_DISC_DELAY 3
47
Robert Love42e9a922008-12-09 15:10:17 -080048static void fc_disc_gpn_ft_req(struct fc_disc *);
49static void fc_disc_gpn_ft_resp(struct fc_seq *, struct fc_frame *, void *);
50static int fc_disc_new_target(struct fc_disc *, struct fc_rport *,
51 struct fc_rport_identifiers *);
Robert Love42e9a922008-12-09 15:10:17 -080052static void fc_disc_done(struct fc_disc *);
53static void fc_disc_timeout(struct work_struct *);
54static void fc_disc_single(struct fc_disc *, struct fc_disc_port *);
55static void fc_disc_restart(struct fc_disc *);
56
57/**
Robert Love34f42a02009-02-27 10:55:45 -080058 * fc_disc_lookup_rport() - lookup a remote port by port_id
Robert Love42e9a922008-12-09 15:10:17 -080059 * @lport: Fibre Channel host port instance
60 * @port_id: remote port port_id to match
61 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -070062struct fc_rport_priv *fc_disc_lookup_rport(const struct fc_lport *lport,
63 u32 port_id)
Robert Love42e9a922008-12-09 15:10:17 -080064{
65 const struct fc_disc *disc = &lport->disc;
Joe Eykholt9fb9d322009-08-25 14:00:50 -070066 struct fc_rport *rport;
Joe Eykholtab28f1f2009-08-25 14:00:34 -070067 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -080068
69 list_for_each_entry(rdata, &disc->rports, peers) {
70 rport = PRIV_TO_RPORT(rdata);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070071 if (rport->port_id == port_id)
72 return rdata;
Robert Love42e9a922008-12-09 15:10:17 -080073 }
Joe Eykholt9fb9d322009-08-25 14:00:50 -070074 return NULL;
Robert Love42e9a922008-12-09 15:10:17 -080075}
76
77/**
Robert Love34f42a02009-02-27 10:55:45 -080078 * fc_disc_stop_rports() - delete all the remote ports associated with the lport
Robert Love42e9a922008-12-09 15:10:17 -080079 * @disc: The discovery job to stop rports on
80 *
81 * Locking Note: This function expects that the lport mutex is locked before
82 * calling it.
83 */
84void fc_disc_stop_rports(struct fc_disc *disc)
85{
86 struct fc_lport *lport;
Joe Eykholtab28f1f2009-08-25 14:00:34 -070087 struct fc_rport_priv *rdata, *next;
Robert Love42e9a922008-12-09 15:10:17 -080088
89 lport = disc->lport;
90
91 mutex_lock(&disc->disc_mutex);
92 list_for_each_entry_safe(rdata, next, &disc->rports, peers) {
Robert Love42e9a922008-12-09 15:10:17 -080093 list_del(&rdata->peers);
Joe Eykholt9fb9d322009-08-25 14:00:50 -070094 lport->tt.rport_logoff(rdata);
Robert Love42e9a922008-12-09 15:10:17 -080095 }
96
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -070097 list_for_each_entry_safe(rdata, next, &disc->rogue_rports, peers) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -070098 lport->tt.rport_logoff(rdata);
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -070099 }
100
Robert Love42e9a922008-12-09 15:10:17 -0800101 mutex_unlock(&disc->disc_mutex);
102}
103
104/**
Robert Love34f42a02009-02-27 10:55:45 -0800105 * fc_disc_rport_callback() - Event handler for rport events
Robert Love42e9a922008-12-09 15:10:17 -0800106 * @lport: The lport which is receiving the event
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700107 * @rdata: private remote port data
Robert Love42e9a922008-12-09 15:10:17 -0800108 * @event: The event that occured
109 *
110 * Locking Note: The rport lock should not be held when calling
111 * this function.
112 */
113static void fc_disc_rport_callback(struct fc_lport *lport,
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700114 struct fc_rport_priv *rdata,
Robert Love42e9a922008-12-09 15:10:17 -0800115 enum fc_rport_event event)
116{
Robert Love42e9a922008-12-09 15:10:17 -0800117 struct fc_disc *disc = &lport->disc;
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700118 struct fc_rport *rport = PRIV_TO_RPORT(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800119
Robert Love74147052009-06-10 15:31:10 -0700120 FC_DISC_DBG(disc, "Received a %d event for port (%6x)\n", event,
121 rport->port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800122
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700123 switch (event) {
124 case RPORT_EV_CREATED:
Robert Love42e9a922008-12-09 15:10:17 -0800125 if (disc) {
Robert Love42e9a922008-12-09 15:10:17 -0800126 mutex_lock(&disc->disc_mutex);
127 list_add_tail(&rdata->peers, &disc->rports);
128 mutex_unlock(&disc->disc_mutex);
129 }
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700130 break;
131 case RPORT_EV_LOGO:
132 case RPORT_EV_FAILED:
133 case RPORT_EV_STOP:
134 mutex_lock(&disc->disc_mutex);
135 mutex_lock(&rdata->rp_mutex);
136 if (rdata->trans_state == FC_PORTSTATE_ROGUE)
137 list_del(&rdata->peers);
138 mutex_unlock(&rdata->rp_mutex);
139 mutex_unlock(&disc->disc_mutex);
140 break;
141 default:
142 break;
Robert Love42e9a922008-12-09 15:10:17 -0800143 }
144
Robert Love42e9a922008-12-09 15:10:17 -0800145}
146
147/**
Robert Love34f42a02009-02-27 10:55:45 -0800148 * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN)
Robert Love42e9a922008-12-09 15:10:17 -0800149 * @sp: Current sequence of the RSCN exchange
150 * @fp: RSCN Frame
151 * @lport: Fibre Channel host port instance
152 *
153 * Locking Note: This function expects that the disc_mutex is locked
154 * before it is called.
155 */
156static void fc_disc_recv_rscn_req(struct fc_seq *sp, struct fc_frame *fp,
157 struct fc_disc *disc)
158{
159 struct fc_lport *lport;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700160 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800161 struct fc_els_rscn *rp;
162 struct fc_els_rscn_page *pp;
163 struct fc_seq_els_data rjt_data;
164 unsigned int len;
165 int redisc = 0;
166 enum fc_els_rscn_ev_qual ev_qual;
167 enum fc_els_rscn_addr_fmt fmt;
168 LIST_HEAD(disc_ports);
169 struct fc_disc_port *dp, *next;
170
171 lport = disc->lport;
172
Robert Love74147052009-06-10 15:31:10 -0700173 FC_DISC_DBG(disc, "Received an RSCN event\n");
Robert Love42e9a922008-12-09 15:10:17 -0800174
175 /* make sure the frame contains an RSCN message */
176 rp = fc_frame_payload_get(fp, sizeof(*rp));
177 if (!rp)
178 goto reject;
179 /* make sure the page length is as expected (4 bytes) */
180 if (rp->rscn_page_len != sizeof(*pp))
181 goto reject;
182 /* get the RSCN payload length */
183 len = ntohs(rp->rscn_plen);
184 if (len < sizeof(*rp))
185 goto reject;
186 /* make sure the frame contains the expected payload */
187 rp = fc_frame_payload_get(fp, len);
188 if (!rp)
189 goto reject;
190 /* payload must be a multiple of the RSCN page size */
191 len -= sizeof(*rp);
192 if (len % sizeof(*pp))
193 goto reject;
194
195 for (pp = (void *)(rp + 1); len > 0; len -= sizeof(*pp), pp++) {
196 ev_qual = pp->rscn_page_flags >> ELS_RSCN_EV_QUAL_BIT;
197 ev_qual &= ELS_RSCN_EV_QUAL_MASK;
198 fmt = pp->rscn_page_flags >> ELS_RSCN_ADDR_FMT_BIT;
199 fmt &= ELS_RSCN_ADDR_FMT_MASK;
200 /*
201 * if we get an address format other than port
202 * (area, domain, fabric), then do a full discovery
203 */
204 switch (fmt) {
205 case ELS_ADDR_FMT_PORT:
Robert Love74147052009-06-10 15:31:10 -0700206 FC_DISC_DBG(disc, "Port address format for port "
207 "(%6x)\n", ntoh24(pp->rscn_fid));
Robert Love42e9a922008-12-09 15:10:17 -0800208 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
209 if (!dp) {
210 redisc = 1;
211 break;
212 }
213 dp->lp = lport;
214 dp->ids.port_id = ntoh24(pp->rscn_fid);
215 dp->ids.port_name = -1;
216 dp->ids.node_name = -1;
217 dp->ids.roles = FC_RPORT_ROLE_UNKNOWN;
218 list_add_tail(&dp->peers, &disc_ports);
219 break;
220 case ELS_ADDR_FMT_AREA:
221 case ELS_ADDR_FMT_DOM:
222 case ELS_ADDR_FMT_FAB:
223 default:
Robert Love74147052009-06-10 15:31:10 -0700224 FC_DISC_DBG(disc, "Address format is (%d)\n", fmt);
Robert Love42e9a922008-12-09 15:10:17 -0800225 redisc = 1;
226 break;
227 }
228 }
229 lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL);
230 if (redisc) {
Robert Love74147052009-06-10 15:31:10 -0700231 FC_DISC_DBG(disc, "RSCN received: rediscovering\n");
Robert Love42e9a922008-12-09 15:10:17 -0800232 fc_disc_restart(disc);
233 } else {
Robert Love74147052009-06-10 15:31:10 -0700234 FC_DISC_DBG(disc, "RSCN received: not rediscovering. "
235 "redisc %d state %d in_prog %d\n",
236 redisc, lport->state, disc->pending);
Robert Love42e9a922008-12-09 15:10:17 -0800237 list_for_each_entry_safe(dp, next, &disc_ports, peers) {
238 list_del(&dp->peers);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700239 rdata = lport->tt.rport_lookup(lport, dp->ids.port_id);
240 if (rdata) {
Robert Love42e9a922008-12-09 15:10:17 -0800241 list_del(&rdata->peers);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700242 lport->tt.rport_logoff(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800243 }
244 fc_disc_single(disc, dp);
245 }
246 }
247 fc_frame_free(fp);
248 return;
249reject:
Robert Love74147052009-06-10 15:31:10 -0700250 FC_DISC_DBG(disc, "Received a bad RSCN frame\n");
Robert Love42e9a922008-12-09 15:10:17 -0800251 rjt_data.fp = NULL;
252 rjt_data.reason = ELS_RJT_LOGIC;
253 rjt_data.explan = ELS_EXPL_NONE;
254 lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data);
255 fc_frame_free(fp);
256}
257
258/**
Robert Love34f42a02009-02-27 10:55:45 -0800259 * fc_disc_recv_req() - Handle incoming requests
Robert Love42e9a922008-12-09 15:10:17 -0800260 * @sp: Current sequence of the request exchange
261 * @fp: The frame
262 * @lport: The FC local port
263 *
264 * Locking Note: This function is called from the EM and will lock
265 * the disc_mutex before calling the handler for the
266 * request.
267 */
268static void fc_disc_recv_req(struct fc_seq *sp, struct fc_frame *fp,
269 struct fc_lport *lport)
270{
271 u8 op;
272 struct fc_disc *disc = &lport->disc;
273
274 op = fc_frame_payload_op(fp);
275 switch (op) {
276 case ELS_RSCN:
277 mutex_lock(&disc->disc_mutex);
278 fc_disc_recv_rscn_req(sp, fp, disc);
279 mutex_unlock(&disc->disc_mutex);
280 break;
281 default:
Robert Love74147052009-06-10 15:31:10 -0700282 FC_DISC_DBG(disc, "Received an unsupported request, "
283 "the opcode is (%x)\n", op);
Robert Love42e9a922008-12-09 15:10:17 -0800284 break;
285 }
286}
287
288/**
Robert Love34f42a02009-02-27 10:55:45 -0800289 * fc_disc_restart() - Restart discovery
Robert Love42e9a922008-12-09 15:10:17 -0800290 * @lport: FC discovery context
291 *
292 * Locking Note: This function expects that the disc mutex
293 * is already locked.
294 */
295static void fc_disc_restart(struct fc_disc *disc)
296{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700297 struct fc_rport_priv *rdata, *next;
Robert Love42e9a922008-12-09 15:10:17 -0800298 struct fc_lport *lport = disc->lport;
299
Robert Love74147052009-06-10 15:31:10 -0700300 FC_DISC_DBG(disc, "Restarting discovery\n");
Robert Love42e9a922008-12-09 15:10:17 -0800301
302 list_for_each_entry_safe(rdata, next, &disc->rports, peers) {
Robert Love42e9a922008-12-09 15:10:17 -0800303 list_del(&rdata->peers);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700304 lport->tt.rport_logoff(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800305 }
306
307 disc->requested = 1;
308 if (!disc->pending)
309 fc_disc_gpn_ft_req(disc);
310}
311
312/**
Robert Love34f42a02009-02-27 10:55:45 -0800313 * fc_disc_start() - Fibre Channel Target discovery
Robert Love42e9a922008-12-09 15:10:17 -0800314 * @lport: FC local port
315 *
316 * Returns non-zero if discovery cannot be started.
317 */
318static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
319 enum fc_disc_event),
320 struct fc_lport *lport)
321{
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700322 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800323 struct fc_rport *rport;
324 struct fc_rport_identifiers ids;
325 struct fc_disc *disc = &lport->disc;
326
327 /*
328 * At this point we may have a new disc job or an existing
329 * one. Either way, let's lock when we make changes to it
330 * and send the GPN_FT request.
331 */
332 mutex_lock(&disc->disc_mutex);
333
334 disc->disc_callback = disc_callback;
335
336 /*
337 * If not ready, or already running discovery, just set request flag.
338 */
339 disc->requested = 1;
340
341 if (disc->pending) {
342 mutex_unlock(&disc->disc_mutex);
343 return;
344 }
345
346 /*
347 * Handle point-to-point mode as a simple discovery
348 * of the remote port. Yucky, yucky, yuck, yuck!
349 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700350 rdata = disc->lport->ptp_rp;
351 if (rdata) {
352 rport = PRIV_TO_RPORT(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800353 ids.port_id = rport->port_id;
354 ids.port_name = rport->port_name;
355 ids.node_name = rport->node_name;
356 ids.roles = FC_RPORT_ROLE_UNKNOWN;
357 get_device(&rport->dev);
358
359 if (!fc_disc_new_target(disc, rport, &ids)) {
360 disc->event = DISC_EV_SUCCESS;
361 fc_disc_done(disc);
362 }
363 put_device(&rport->dev);
364 } else {
365 fc_disc_gpn_ft_req(disc); /* get ports by FC-4 type */
366 }
367
368 mutex_unlock(&disc->disc_mutex);
369}
370
371static struct fc_rport_operations fc_disc_rport_ops = {
372 .event_callback = fc_disc_rport_callback,
373};
374
375/**
Robert Love34f42a02009-02-27 10:55:45 -0800376 * fc_disc_new_target() - Handle new target found by discovery
Robert Love42e9a922008-12-09 15:10:17 -0800377 * @lport: FC local port
378 * @rport: The previous FC remote port (NULL if new remote port)
379 * @ids: Identifiers for the new FC remote port
380 *
381 * Locking Note: This function expects that the disc_mutex is locked
382 * before it is called.
383 */
384static int fc_disc_new_target(struct fc_disc *disc,
385 struct fc_rport *rport,
386 struct fc_rport_identifiers *ids)
387{
388 struct fc_lport *lport = disc->lport;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700389 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800390 int error = 0;
391
392 if (rport && ids->port_name) {
393 if (rport->port_name == -1) {
394 /*
395 * Set WWN and fall through to notify of create.
396 */
397 fc_rport_set_name(rport, ids->port_name,
398 rport->node_name);
399 } else if (rport->port_name != ids->port_name) {
400 /*
401 * This is a new port with the same FCID as
402 * a previously-discovered port. Presumably the old
403 * port logged out and a new port logged in and was
404 * assigned the same FCID. This should be rare.
405 * Delete the old one and fall thru to re-create.
406 */
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700407 rdata = rport->dd_data;
408 list_del(&rdata->peers);
409 lport->tt.rport_logoff(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800410 rport = NULL;
411 }
412 }
413 if (((ids->port_name != -1) || (ids->port_id != -1)) &&
414 ids->port_id != fc_host_port_id(lport->host) &&
415 ids->port_name != lport->wwpn) {
416 if (!rport) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700417 rdata = lport->tt.rport_lookup(lport, ids->port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800418 if (!rport) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700419 rdata = lport->tt.rport_create(lport, ids);
Robert Love42e9a922008-12-09 15:10:17 -0800420 }
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700421 if (!rdata)
Robert Love42e9a922008-12-09 15:10:17 -0800422 error = -ENOMEM;
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700423 else
424 rport = PRIV_TO_RPORT(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800425 }
426 if (rport) {
Robert Loved3b33322009-02-27 10:55:29 -0800427 rdata = rport->dd_data;
428 rdata->ops = &fc_disc_rport_ops;
429 rdata->rp_state = RPORT_ST_INIT;
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700430 list_add_tail(&rdata->peers, &disc->rogue_rports);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700431 lport->tt.rport_login(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800432 }
433 }
434 return error;
435}
436
437/**
Robert Love34f42a02009-02-27 10:55:45 -0800438 * fc_disc_done() - Discovery has been completed
Robert Love42e9a922008-12-09 15:10:17 -0800439 * @disc: FC discovery context
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700440 * Locking Note: This function expects that the disc mutex is locked before
441 * it is called. The discovery callback is then made with the lock released,
442 * and the lock is re-taken before returning from this function
Robert Love42e9a922008-12-09 15:10:17 -0800443 */
444static void fc_disc_done(struct fc_disc *disc)
445{
446 struct fc_lport *lport = disc->lport;
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700447 enum fc_disc_event event;
Robert Love42e9a922008-12-09 15:10:17 -0800448
Robert Love74147052009-06-10 15:31:10 -0700449 FC_DISC_DBG(disc, "Discovery complete\n");
Robert Love42e9a922008-12-09 15:10:17 -0800450
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700451 event = disc->event;
Robert Love42e9a922008-12-09 15:10:17 -0800452 disc->event = DISC_EV_NONE;
453
454 if (disc->requested)
455 fc_disc_gpn_ft_req(disc);
456 else
457 disc->pending = 0;
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700458
459 mutex_unlock(&disc->disc_mutex);
460 disc->disc_callback(lport, event);
461 mutex_lock(&disc->disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800462}
463
464/**
Robert Love34f42a02009-02-27 10:55:45 -0800465 * fc_disc_error() - Handle error on dNS request
Robert Love42e9a922008-12-09 15:10:17 -0800466 * @disc: FC discovery context
467 * @fp: The frame pointer
468 */
469static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
470{
471 struct fc_lport *lport = disc->lport;
472 unsigned long delay = 0;
Robert Love74147052009-06-10 15:31:10 -0700473
474 FC_DISC_DBG(disc, "Error %ld, retries %d/%d\n",
475 PTR_ERR(fp), disc->retry_count,
476 FC_DISC_RETRY_LIMIT);
Robert Love42e9a922008-12-09 15:10:17 -0800477
478 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
479 /*
480 * Memory allocation failure, or the exchange timed out,
481 * retry after delay.
482 */
483 if (disc->retry_count < FC_DISC_RETRY_LIMIT) {
484 /* go ahead and retry */
485 if (!fp)
486 delay = msecs_to_jiffies(FC_DISC_RETRY_DELAY);
487 else {
488 delay = msecs_to_jiffies(lport->e_d_tov);
489
490 /* timeout faster first time */
491 if (!disc->retry_count)
492 delay /= 4;
493 }
494 disc->retry_count++;
495 schedule_delayed_work(&disc->disc_work, delay);
496 } else {
497 /* exceeded retries */
498 disc->event = DISC_EV_FAILED;
499 fc_disc_done(disc);
500 }
501 }
502}
503
504/**
Robert Love34f42a02009-02-27 10:55:45 -0800505 * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
Robert Love42e9a922008-12-09 15:10:17 -0800506 * @lport: FC discovery context
507 *
508 * Locking Note: This function expects that the disc_mutex is locked
509 * before it is called.
510 */
511static void fc_disc_gpn_ft_req(struct fc_disc *disc)
512{
513 struct fc_frame *fp;
514 struct fc_lport *lport = disc->lport;
515
516 WARN_ON(!fc_lport_test_ready(lport));
517
518 disc->pending = 1;
519 disc->requested = 0;
520
521 disc->buf_len = 0;
522 disc->seq_count = 0;
523 fp = fc_frame_alloc(lport,
524 sizeof(struct fc_ct_hdr) +
525 sizeof(struct fc_ns_gid_ft));
526 if (!fp)
527 goto err;
528
529 if (lport->tt.elsct_send(lport, NULL, fp,
530 FC_NS_GPN_FT,
531 fc_disc_gpn_ft_resp,
532 disc, lport->e_d_tov))
533 return;
534err:
535 fc_disc_error(disc, fp);
536}
537
538/**
Robert Love34f42a02009-02-27 10:55:45 -0800539 * fc_disc_gpn_ft_parse() - Parse the list of IDs and names resulting from a request
Robert Love42e9a922008-12-09 15:10:17 -0800540 * @lport: Fibre Channel host port instance
541 * @buf: GPN_FT response buffer
542 * @len: size of response buffer
543 */
544static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
545{
546 struct fc_lport *lport;
547 struct fc_gpn_ft_resp *np;
548 char *bp;
549 size_t plen;
550 size_t tlen;
551 int error = 0;
Joe Eykholt795d86f2009-08-25 14:00:39 -0700552 struct fc_rport_identifiers ids;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700553 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800554
555 lport = disc->lport;
556
557 /*
558 * Handle partial name record left over from previous call.
559 */
560 bp = buf;
561 plen = len;
562 np = (struct fc_gpn_ft_resp *)bp;
563 tlen = disc->buf_len;
564 if (tlen) {
565 WARN_ON(tlen >= sizeof(*np));
566 plen = sizeof(*np) - tlen;
567 WARN_ON(plen <= 0);
568 WARN_ON(plen >= sizeof(*np));
569 if (plen > len)
570 plen = len;
571 np = &disc->partial_buf;
572 memcpy((char *)np + tlen, bp, plen);
573
574 /*
575 * Set bp so that the loop below will advance it to the
576 * first valid full name element.
577 */
578 bp -= tlen;
579 len += tlen;
580 plen += tlen;
581 disc->buf_len = (unsigned char) plen;
582 if (plen == sizeof(*np))
583 disc->buf_len = 0;
584 }
585
586 /*
587 * Handle full name records, including the one filled from above.
588 * Normally, np == bp and plen == len, but from the partial case above,
589 * bp, len describe the overall buffer, and np, plen describe the
590 * partial buffer, which if would usually be full now.
591 * After the first time through the loop, things return to "normal".
592 */
593 while (plen >= sizeof(*np)) {
Joe Eykholt795d86f2009-08-25 14:00:39 -0700594 ids.port_id = ntoh24(np->fp_fid);
595 ids.port_name = ntohll(np->fp_wwpn);
596 ids.node_name = -1;
597 ids.roles = FC_RPORT_ROLE_UNKNOWN;
Robert Love42e9a922008-12-09 15:10:17 -0800598
Joe Eykholt795d86f2009-08-25 14:00:39 -0700599 if (ids.port_id != fc_host_port_id(lport->host) &&
600 ids.port_name != lport->wwpn) {
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700601 rdata = lport->tt.rport_create(lport, &ids);
602 if (rdata) {
Robert Love42e9a922008-12-09 15:10:17 -0800603 rdata->ops = &fc_disc_rport_ops;
604 rdata->local_port = lport;
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700605 list_add_tail(&rdata->peers,
606 &disc->rogue_rports);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700607 lport->tt.rport_login(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800608 } else
Robert Love74147052009-06-10 15:31:10 -0700609 printk(KERN_WARNING "libfc: Failed to allocate "
610 "memory for the newly discovered port "
Joe Eykholt795d86f2009-08-25 14:00:39 -0700611 "(%6x)\n", ids.port_id);
Robert Love42e9a922008-12-09 15:10:17 -0800612 }
613
614 if (np->fp_flags & FC_NS_FID_LAST) {
615 disc->event = DISC_EV_SUCCESS;
616 fc_disc_done(disc);
617 len = 0;
618 break;
619 }
620 len -= sizeof(*np);
621 bp += sizeof(*np);
622 np = (struct fc_gpn_ft_resp *)bp;
623 plen = len;
624 }
625
626 /*
627 * Save any partial record at the end of the buffer for next time.
628 */
629 if (error == 0 && len > 0 && len < sizeof(*np)) {
630 if (np != &disc->partial_buf) {
Robert Love74147052009-06-10 15:31:10 -0700631 FC_DISC_DBG(disc, "Partial buffer remains "
632 "for discovery\n");
Robert Love42e9a922008-12-09 15:10:17 -0800633 memcpy(&disc->partial_buf, np, len);
634 }
635 disc->buf_len = (unsigned char) len;
636 } else {
637 disc->buf_len = 0;
638 }
639 return error;
640}
641
Robert Love34f42a02009-02-27 10:55:45 -0800642/**
643 * fc_disc_timeout() - Retry handler for the disc component
644 * @work: Structure holding disc obj that needs retry discovery
645 *
Robert Love42e9a922008-12-09 15:10:17 -0800646 * Handle retry of memory allocation for remote ports.
647 */
648static void fc_disc_timeout(struct work_struct *work)
649{
650 struct fc_disc *disc = container_of(work,
651 struct fc_disc,
652 disc_work.work);
653 mutex_lock(&disc->disc_mutex);
654 if (disc->requested && !disc->pending)
655 fc_disc_gpn_ft_req(disc);
656 mutex_unlock(&disc->disc_mutex);
657}
658
659/**
Robert Love34f42a02009-02-27 10:55:45 -0800660 * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
Robert Love42e9a922008-12-09 15:10:17 -0800661 * @sp: Current sequence of GPN_FT exchange
662 * @fp: response frame
663 * @lp_arg: Fibre Channel host port instance
664 *
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700665 * Locking Note: This function is called without disc mutex held, and
666 * should do all its processing with the mutex held
Robert Love42e9a922008-12-09 15:10:17 -0800667 */
668static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp,
669 void *disc_arg)
670{
671 struct fc_disc *disc = disc_arg;
672 struct fc_ct_hdr *cp;
673 struct fc_frame_header *fh;
674 unsigned int seq_cnt;
675 void *buf = NULL;
676 unsigned int len;
677 int error;
678
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700679 mutex_lock(&disc->disc_mutex);
Robert Love74147052009-06-10 15:31:10 -0700680 FC_DISC_DBG(disc, "Received a GPN_FT response\n");
Robert Love42e9a922008-12-09 15:10:17 -0800681
682 if (IS_ERR(fp)) {
683 fc_disc_error(disc, fp);
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700684 mutex_unlock(&disc->disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800685 return;
686 }
687
688 WARN_ON(!fc_frame_is_linear(fp)); /* buffer must be contiguous */
689 fh = fc_frame_header_get(fp);
690 len = fr_len(fp) - sizeof(*fh);
691 seq_cnt = ntohs(fh->fh_seq_cnt);
692 if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 &&
693 disc->seq_count == 0) {
694 cp = fc_frame_payload_get(fp, sizeof(*cp));
695 if (!cp) {
Robert Love74147052009-06-10 15:31:10 -0700696 FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n",
697 fr_len(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800698 } else if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
699
Robert Love34f42a02009-02-27 10:55:45 -0800700 /* Accepted, parse the response. */
Robert Love42e9a922008-12-09 15:10:17 -0800701 buf = cp + 1;
702 len -= sizeof(*cp);
703 } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
Robert Love74147052009-06-10 15:31:10 -0700704 FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x "
705 "(check zoning)\n", cp->ct_reason,
706 cp->ct_explan);
Robert Love42e9a922008-12-09 15:10:17 -0800707 disc->event = DISC_EV_FAILED;
708 fc_disc_done(disc);
709 } else {
Robert Love74147052009-06-10 15:31:10 -0700710 FC_DISC_DBG(disc, "GPN_FT unexpected response code "
711 "%x\n", ntohs(cp->ct_cmd));
Robert Love42e9a922008-12-09 15:10:17 -0800712 }
713 } else if (fr_sof(fp) == FC_SOF_N3 &&
714 seq_cnt == disc->seq_count) {
715 buf = fh + 1;
716 } else {
Robert Love74147052009-06-10 15:31:10 -0700717 FC_DISC_DBG(disc, "GPN_FT unexpected frame - out of sequence? "
718 "seq_cnt %x expected %x sof %x eof %x\n",
719 seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp));
Robert Love42e9a922008-12-09 15:10:17 -0800720 }
721 if (buf) {
722 error = fc_disc_gpn_ft_parse(disc, buf, len);
723 if (error)
724 fc_disc_error(disc, fp);
725 else
726 disc->seq_count++;
727 }
728 fc_frame_free(fp);
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700729
730 mutex_unlock(&disc->disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800731}
732
733/**
Robert Love34f42a02009-02-27 10:55:45 -0800734 * fc_disc_single() - Discover the directory information for a single target
Robert Love42e9a922008-12-09 15:10:17 -0800735 * @lport: FC local port
736 * @dp: The port to rediscover
737 *
738 * Locking Note: This function expects that the disc_mutex is locked
739 * before it is called.
740 */
741static void fc_disc_single(struct fc_disc *disc, struct fc_disc_port *dp)
742{
743 struct fc_lport *lport;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700744 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800745
746 lport = disc->lport;
747
748 if (dp->ids.port_id == fc_host_port_id(lport->host))
749 goto out;
750
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700751 rdata = lport->tt.rport_create(lport, &dp->ids);
752 if (rdata) {
Robert Love42e9a922008-12-09 15:10:17 -0800753 rdata->ops = &fc_disc_rport_ops;
754 kfree(dp);
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700755 list_add_tail(&rdata->peers, &disc->rogue_rports);
Joe Eykholt9fb9d322009-08-25 14:00:50 -0700756 lport->tt.rport_login(rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800757 }
758 return;
759out:
760 kfree(dp);
761}
762
763/**
Robert Love34f42a02009-02-27 10:55:45 -0800764 * fc_disc_stop() - Stop discovery for a given lport
Robert Love42e9a922008-12-09 15:10:17 -0800765 * @lport: The lport that discovery should stop for
766 */
767void fc_disc_stop(struct fc_lport *lport)
768{
769 struct fc_disc *disc = &lport->disc;
770
771 if (disc) {
772 cancel_delayed_work_sync(&disc->disc_work);
773 fc_disc_stop_rports(disc);
774 }
775}
776
777/**
Robert Love34f42a02009-02-27 10:55:45 -0800778 * fc_disc_stop_final() - Stop discovery for a given lport
Robert Love42e9a922008-12-09 15:10:17 -0800779 * @lport: The lport that discovery should stop for
780 *
781 * This function will block until discovery has been
782 * completely stopped and all rports have been deleted.
783 */
784void fc_disc_stop_final(struct fc_lport *lport)
785{
786 fc_disc_stop(lport);
787 lport->tt.rport_flush_queue();
788}
789
790/**
Robert Love34f42a02009-02-27 10:55:45 -0800791 * fc_disc_init() - Initialize the discovery block
Robert Love42e9a922008-12-09 15:10:17 -0800792 * @lport: FC local port
793 */
794int fc_disc_init(struct fc_lport *lport)
795{
796 struct fc_disc *disc;
797
798 if (!lport->tt.disc_start)
799 lport->tt.disc_start = fc_disc_start;
800
801 if (!lport->tt.disc_stop)
802 lport->tt.disc_stop = fc_disc_stop;
803
804 if (!lport->tt.disc_stop_final)
805 lport->tt.disc_stop_final = fc_disc_stop_final;
806
807 if (!lport->tt.disc_recv_req)
808 lport->tt.disc_recv_req = fc_disc_recv_req;
809
810 if (!lport->tt.rport_lookup)
811 lport->tt.rport_lookup = fc_disc_lookup_rport;
812
813 disc = &lport->disc;
814 INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
815 mutex_init(&disc->disc_mutex);
816 INIT_LIST_HEAD(&disc->rports);
Abhijeet Joglekarb4c6f542009-04-21 16:27:04 -0700817 INIT_LIST_HEAD(&disc->rogue_rports);
Robert Love42e9a922008-12-09 15:10:17 -0800818
819 disc->lport = lport;
820 disc->delay = FC_DISC_DELAY;
821 disc->event = DISC_EV_NONE;
822
823 return 0;
824}
825EXPORT_SYMBOL(fc_disc_init);