blob: e5ffeab9f6709f2afe067d6429fecf6cf8cb1599 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Robert Love42e9a922008-12-09 15:10:17 -080037#include <linux/err.h>
Paul Gortmaker09703662011-05-27 09:37:25 -040038#include <linux/export.h>
Robert Love42e9a922008-12-09 15:10:17 -080039#include <asm/unaligned.h>
40
41#include <scsi/fc/fc_gs.h>
42
43#include <scsi/libfc.h>
44
Robert Love8866a5d2009-11-03 11:45:58 -080045#include "fc_libfc.h"
46
Robert Love42e9a922008-12-09 15:10:17 -080047#define FC_DISC_RETRY_LIMIT 3 /* max retries */
48#define FC_DISC_RETRY_DELAY 500UL /* (msecs) delay */
49
Robert Love42e9a922008-12-09 15:10:17 -080050static void fc_disc_gpn_ft_req(struct fc_disc *);
51static void fc_disc_gpn_ft_resp(struct fc_seq *, struct fc_frame *, void *);
Joe Eykholt786681b2009-08-25 14:01:29 -070052static void fc_disc_done(struct fc_disc *, enum fc_disc_event);
Robert Love42e9a922008-12-09 15:10:17 -080053static void fc_disc_timeout(struct work_struct *);
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -070054static int fc_disc_single(struct fc_lport *, struct fc_disc_port *);
Robert Love42e9a922008-12-09 15:10:17 -080055static void fc_disc_restart(struct fc_disc *);
56
57/**
Robert Love3a3b42b2009-11-03 11:47:39 -080058 * fc_disc_stop_rports() - Delete all the remote ports associated with the lport
59 * @disc: The discovery job to stop remote ports on
Robert Love42e9a922008-12-09 15:10:17 -080060 *
61 * Locking Note: This function expects that the lport mutex is locked before
62 * calling it.
63 */
Bart Van Asschec6b21c92012-01-13 17:26:20 -080064static void fc_disc_stop_rports(struct fc_disc *disc)
Robert Love42e9a922008-12-09 15:10:17 -080065{
66 struct fc_lport *lport;
Joe Eykholt42e90412010-07-20 15:19:37 -070067 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -080068
Joe Eykholt06852302010-07-20 15:19:53 -070069 lport = fc_disc_lport(disc);
Robert Love42e9a922008-12-09 15:10:17 -080070
Hannes Reineckea407c592016-09-30 11:01:15 +020071 rcu_read_lock();
72 list_for_each_entry_rcu(rdata, &disc->rports, peers) {
73 if (kref_get_unless_zero(&rdata->kref)) {
74 lport->tt.rport_logoff(rdata);
Hannes Reinecke944ef962016-10-18 10:01:39 +020075 kref_put(&rdata->kref, fc_rport_destroy);
Hannes Reineckea407c592016-09-30 11:01:15 +020076 }
77 }
78 rcu_read_unlock();
Robert Love42e9a922008-12-09 15:10:17 -080079}
80
81/**
Robert Love34f42a02009-02-27 10:55:45 -080082 * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN)
Joe Eykholt922611562010-07-20 15:21:12 -070083 * @disc: The discovery object to which the RSCN applies
Robert Love3a3b42b2009-11-03 11:47:39 -080084 * @fp: The RSCN frame
Robert Love42e9a922008-12-09 15:10:17 -080085 *
86 * Locking Note: This function expects that the disc_mutex is locked
87 * before it is called.
88 */
Joe Eykholt922611562010-07-20 15:21:12 -070089static void fc_disc_recv_rscn_req(struct fc_disc *disc, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -080090{
91 struct fc_lport *lport;
Robert Love42e9a922008-12-09 15:10:17 -080092 struct fc_els_rscn *rp;
93 struct fc_els_rscn_page *pp;
94 struct fc_seq_els_data rjt_data;
95 unsigned int len;
96 int redisc = 0;
97 enum fc_els_rscn_ev_qual ev_qual;
98 enum fc_els_rscn_addr_fmt fmt;
99 LIST_HEAD(disc_ports);
100 struct fc_disc_port *dp, *next;
101
Joe Eykholt06852302010-07-20 15:19:53 -0700102 lport = fc_disc_lport(disc);
Robert Love42e9a922008-12-09 15:10:17 -0800103
Robert Love74147052009-06-10 15:31:10 -0700104 FC_DISC_DBG(disc, "Received an RSCN event\n");
Robert Love42e9a922008-12-09 15:10:17 -0800105
106 /* make sure the frame contains an RSCN message */
107 rp = fc_frame_payload_get(fp, sizeof(*rp));
108 if (!rp)
109 goto reject;
110 /* make sure the page length is as expected (4 bytes) */
111 if (rp->rscn_page_len != sizeof(*pp))
112 goto reject;
113 /* get the RSCN payload length */
114 len = ntohs(rp->rscn_plen);
115 if (len < sizeof(*rp))
116 goto reject;
117 /* make sure the frame contains the expected payload */
118 rp = fc_frame_payload_get(fp, len);
119 if (!rp)
120 goto reject;
121 /* payload must be a multiple of the RSCN page size */
122 len -= sizeof(*rp);
123 if (len % sizeof(*pp))
124 goto reject;
125
126 for (pp = (void *)(rp + 1); len > 0; len -= sizeof(*pp), pp++) {
127 ev_qual = pp->rscn_page_flags >> ELS_RSCN_EV_QUAL_BIT;
128 ev_qual &= ELS_RSCN_EV_QUAL_MASK;
129 fmt = pp->rscn_page_flags >> ELS_RSCN_ADDR_FMT_BIT;
130 fmt &= ELS_RSCN_ADDR_FMT_MASK;
131 /*
132 * if we get an address format other than port
133 * (area, domain, fabric), then do a full discovery
134 */
135 switch (fmt) {
136 case ELS_ADDR_FMT_PORT:
Robert Love74147052009-06-10 15:31:10 -0700137 FC_DISC_DBG(disc, "Port address format for port "
Chris Leechce8b5df2010-04-09 14:23:10 -0700138 "(%6.6x)\n", ntoh24(pp->rscn_fid));
Robert Love42e9a922008-12-09 15:10:17 -0800139 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
140 if (!dp) {
141 redisc = 1;
142 break;
143 }
144 dp->lp = lport;
Robert Love9737e6a2009-08-25 14:02:59 -0700145 dp->port_id = ntoh24(pp->rscn_fid);
Robert Love42e9a922008-12-09 15:10:17 -0800146 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 }
Hannes Reinecke7ab24dd2016-10-18 10:01:35 +0200157 fc_seq_els_rsp_send(fp, ELS_LS_ACC, NULL);
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700158
159 /*
160 * If not doing a complete rediscovery, do GPN_ID on
161 * the individual ports mentioned in the list.
162 * If any of these get an error, do a full rediscovery.
163 * In any case, go through the list and free the entries.
164 */
165 list_for_each_entry_safe(dp, next, &disc_ports, peers) {
166 list_del(&dp->peers);
167 if (!redisc)
168 redisc = fc_disc_single(lport, dp);
169 kfree(dp);
170 }
Robert Love42e9a922008-12-09 15:10:17 -0800171 if (redisc) {
Robert Love74147052009-06-10 15:31:10 -0700172 FC_DISC_DBG(disc, "RSCN received: rediscovering\n");
Robert Love42e9a922008-12-09 15:10:17 -0800173 fc_disc_restart(disc);
174 } else {
Robert Love74147052009-06-10 15:31:10 -0700175 FC_DISC_DBG(disc, "RSCN received: not rediscovering. "
176 "redisc %d state %d in_prog %d\n",
177 redisc, lport->state, disc->pending);
Robert Love42e9a922008-12-09 15:10:17 -0800178 }
179 fc_frame_free(fp);
180 return;
181reject:
Robert Love74147052009-06-10 15:31:10 -0700182 FC_DISC_DBG(disc, "Received a bad RSCN frame\n");
Robert Love42e9a922008-12-09 15:10:17 -0800183 rjt_data.reason = ELS_RJT_LOGIC;
184 rjt_data.explan = ELS_EXPL_NONE;
Hannes Reinecke7ab24dd2016-10-18 10:01:35 +0200185 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
Robert Love42e9a922008-12-09 15:10:17 -0800186 fc_frame_free(fp);
187}
188
189/**
Robert Love34f42a02009-02-27 10:55:45 -0800190 * fc_disc_recv_req() - Handle incoming requests
Robert Love3a3b42b2009-11-03 11:47:39 -0800191 * @lport: The local port receiving the request
Joe Eykholt922611562010-07-20 15:21:12 -0700192 * @fp: The request frame
Robert Love42e9a922008-12-09 15:10:17 -0800193 *
194 * Locking Note: This function is called from the EM and will lock
195 * the disc_mutex before calling the handler for the
196 * request.
197 */
Joe Eykholt922611562010-07-20 15:21:12 -0700198static void fc_disc_recv_req(struct fc_lport *lport, struct fc_frame *fp)
Robert Love42e9a922008-12-09 15:10:17 -0800199{
200 u8 op;
201 struct fc_disc *disc = &lport->disc;
202
203 op = fc_frame_payload_op(fp);
204 switch (op) {
205 case ELS_RSCN:
206 mutex_lock(&disc->disc_mutex);
Joe Eykholt922611562010-07-20 15:21:12 -0700207 fc_disc_recv_rscn_req(disc, fp);
Robert Love42e9a922008-12-09 15:10:17 -0800208 mutex_unlock(&disc->disc_mutex);
209 break;
210 default:
Robert Love74147052009-06-10 15:31:10 -0700211 FC_DISC_DBG(disc, "Received an unsupported request, "
212 "the opcode is (%x)\n", op);
Hillf Danton83383dd2011-05-16 16:45:35 -0700213 fc_frame_free(fp);
Robert Love42e9a922008-12-09 15:10:17 -0800214 break;
215 }
216}
217
218/**
Robert Love34f42a02009-02-27 10:55:45 -0800219 * fc_disc_restart() - Restart discovery
Robert Love3a3b42b2009-11-03 11:47:39 -0800220 * @disc: The discovery object to be restarted
Robert Love42e9a922008-12-09 15:10:17 -0800221 *
222 * Locking Note: This function expects that the disc mutex
223 * is already locked.
224 */
225static void fc_disc_restart(struct fc_disc *disc)
226{
Joe Eykholt935d0fc2009-08-25 14:02:54 -0700227 if (!disc->disc_callback)
228 return;
229
Robert Love74147052009-06-10 15:31:10 -0700230 FC_DISC_DBG(disc, "Restarting discovery\n");
Robert Love42e9a922008-12-09 15:10:17 -0800231
Robert Love42e9a922008-12-09 15:10:17 -0800232 disc->requested = 1;
Joe Eykholt0f6c614982009-08-25 14:02:11 -0700233 if (disc->pending)
234 return;
235
236 /*
237 * Advance disc_id. This is an arbitrary non-zero number that will
238 * match the value in the fc_rport_priv after discovery for all
239 * freshly-discovered remote ports. Avoid wrapping to zero.
240 */
241 disc->disc_id = (disc->disc_id + 2) | 1;
Joe Eykholt3667d7e2009-08-25 14:02:38 -0700242 disc->retry_count = 0;
Joe Eykholt0f6c614982009-08-25 14:02:11 -0700243 fc_disc_gpn_ft_req(disc);
Robert Love42e9a922008-12-09 15:10:17 -0800244}
245
246/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800247 * fc_disc_start() - Start discovery on a local port
248 * @lport: The local port to have discovery started on
249 * @disc_callback: Callback function to be called when discovery is complete
Robert Love42e9a922008-12-09 15:10:17 -0800250 */
251static void fc_disc_start(void (*disc_callback)(struct fc_lport *,
252 enum fc_disc_event),
253 struct fc_lport *lport)
254{
Robert Love42e9a922008-12-09 15:10:17 -0800255 struct fc_disc *disc = &lport->disc;
256
257 /*
258 * At this point we may have a new disc job or an existing
259 * one. Either way, let's lock when we make changes to it
260 * and send the GPN_FT request.
261 */
262 mutex_lock(&disc->disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800263 disc->disc_callback = disc_callback;
Joe Eykholt29d898e2009-08-25 14:02:49 -0700264 fc_disc_restart(disc);
Robert Love42e9a922008-12-09 15:10:17 -0800265 mutex_unlock(&disc->disc_mutex);
266}
267
Robert Love42e9a922008-12-09 15:10:17 -0800268/**
Robert Love34f42a02009-02-27 10:55:45 -0800269 * fc_disc_done() - Discovery has been completed
Robert Love3a3b42b2009-11-03 11:47:39 -0800270 * @disc: The discovery context
271 * @event: The discovery completion status
Joe Eykholt786681b2009-08-25 14:01:29 -0700272 *
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700273 * Locking Note: This function expects that the disc mutex is locked before
274 * it is called. The discovery callback is then made with the lock released,
275 * and the lock is re-taken before returning from this function
Robert Love42e9a922008-12-09 15:10:17 -0800276 */
Joe Eykholt786681b2009-08-25 14:01:29 -0700277static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event)
Robert Love42e9a922008-12-09 15:10:17 -0800278{
Joe Eykholt06852302010-07-20 15:19:53 -0700279 struct fc_lport *lport = fc_disc_lport(disc);
Joe Eykholt0f6c614982009-08-25 14:02:11 -0700280 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800281
Robert Love74147052009-06-10 15:31:10 -0700282 FC_DISC_DBG(disc, "Discovery complete\n");
Robert Love42e9a922008-12-09 15:10:17 -0800283
Joe Eykholt0f6c614982009-08-25 14:02:11 -0700284 disc->pending = 0;
285 if (disc->requested) {
286 fc_disc_restart(disc);
287 return;
288 }
289
290 /*
Robert Love3a3b42b2009-11-03 11:47:39 -0800291 * Go through all remote ports. If they were found in the latest
292 * discovery, reverify or log them in. Otherwise, log them out.
Joe Eykholt0f6c614982009-08-25 14:02:11 -0700293 * Skip ports which were never discovered. These are the dNS port
294 * and ports which were created by PLOGI.
295 */
Hannes Reineckea407c592016-09-30 11:01:15 +0200296 rcu_read_lock();
Joe Eykholt42e90412010-07-20 15:19:37 -0700297 list_for_each_entry_rcu(rdata, &disc->rports, peers) {
Hannes Reineckea407c592016-09-30 11:01:15 +0200298 if (!kref_get_unless_zero(&rdata->kref))
Joe Eykholt0f6c614982009-08-25 14:02:11 -0700299 continue;
Hannes Reineckea407c592016-09-30 11:01:15 +0200300 if (rdata->disc_id) {
301 if (rdata->disc_id == disc->disc_id)
302 lport->tt.rport_login(rdata);
303 else
304 lport->tt.rport_logoff(rdata);
305 }
Hannes Reinecke944ef962016-10-18 10:01:39 +0200306 kref_put(&rdata->kref, fc_rport_destroy);
Joe Eykholt0f6c614982009-08-25 14:02:11 -0700307 }
Hannes Reineckea407c592016-09-30 11:01:15 +0200308 rcu_read_unlock();
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700309 mutex_unlock(&disc->disc_mutex);
310 disc->disc_callback(lport, event);
311 mutex_lock(&disc->disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800312}
313
314/**
Robert Love34f42a02009-02-27 10:55:45 -0800315 * fc_disc_error() - Handle error on dNS request
Robert Love3a3b42b2009-11-03 11:47:39 -0800316 * @disc: The discovery context
317 * @fp: The error code encoded as a frame pointer
Robert Love42e9a922008-12-09 15:10:17 -0800318 */
319static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp)
320{
Joe Eykholt06852302010-07-20 15:19:53 -0700321 struct fc_lport *lport = fc_disc_lport(disc);
Robert Love42e9a922008-12-09 15:10:17 -0800322 unsigned long delay = 0;
Robert Love74147052009-06-10 15:31:10 -0700323
324 FC_DISC_DBG(disc, "Error %ld, retries %d/%d\n",
325 PTR_ERR(fp), disc->retry_count,
326 FC_DISC_RETRY_LIMIT);
Robert Love42e9a922008-12-09 15:10:17 -0800327
328 if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) {
329 /*
330 * Memory allocation failure, or the exchange timed out,
331 * retry after delay.
332 */
333 if (disc->retry_count < FC_DISC_RETRY_LIMIT) {
334 /* go ahead and retry */
335 if (!fp)
336 delay = msecs_to_jiffies(FC_DISC_RETRY_DELAY);
337 else {
338 delay = msecs_to_jiffies(lport->e_d_tov);
339
340 /* timeout faster first time */
341 if (!disc->retry_count)
342 delay /= 4;
343 }
344 disc->retry_count++;
345 schedule_delayed_work(&disc->disc_work, delay);
Joe Eykholt786681b2009-08-25 14:01:29 -0700346 } else
347 fc_disc_done(disc, DISC_EV_FAILED);
Bhanu Prakash Gollapudi00832082012-02-10 17:18:57 -0800348 } else if (PTR_ERR(fp) == -FC_EX_CLOSED) {
349 /*
350 * if discovery fails due to lport reset, clear
351 * pending flag so that subsequent discovery can
352 * continue
353 */
354 disc->pending = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800355 }
356}
357
358/**
Robert Love34f42a02009-02-27 10:55:45 -0800359 * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request
Robert Love3a3b42b2009-11-03 11:47:39 -0800360 * @lport: The discovery context
Robert Love42e9a922008-12-09 15:10:17 -0800361 *
362 * Locking Note: This function expects that the disc_mutex is locked
363 * before it is called.
364 */
365static void fc_disc_gpn_ft_req(struct fc_disc *disc)
366{
367 struct fc_frame *fp;
Joe Eykholt06852302010-07-20 15:19:53 -0700368 struct fc_lport *lport = fc_disc_lport(disc);
Robert Love42e9a922008-12-09 15:10:17 -0800369
370 WARN_ON(!fc_lport_test_ready(lport));
371
372 disc->pending = 1;
373 disc->requested = 0;
374
375 disc->buf_len = 0;
376 disc->seq_count = 0;
377 fp = fc_frame_alloc(lport,
378 sizeof(struct fc_ct_hdr) +
379 sizeof(struct fc_ns_gid_ft));
380 if (!fp)
381 goto err;
382
Joe Eykholta46f3272009-08-25 14:00:55 -0700383 if (lport->tt.elsct_send(lport, 0, fp,
Robert Love42e9a922008-12-09 15:10:17 -0800384 FC_NS_GPN_FT,
385 fc_disc_gpn_ft_resp,
Joe Eykholtb94f8952009-11-03 11:50:21 -0800386 disc, 3 * lport->r_a_tov))
Robert Love42e9a922008-12-09 15:10:17 -0800387 return;
388err:
Chris Leech8f550f92009-10-21 16:28:09 -0700389 fc_disc_error(disc, NULL);
Robert Love42e9a922008-12-09 15:10:17 -0800390}
391
392/**
Joe Eykholt786681b2009-08-25 14:01:29 -0700393 * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response.
Robert Love3a3b42b2009-11-03 11:47:39 -0800394 * @lport: The local port the GPN_FT was received on
395 * @buf: The GPN_FT response buffer
396 * @len: The size of response buffer
Joe Eykholt786681b2009-08-25 14:01:29 -0700397 *
398 * Goes through the list of IDs and names resulting from a request.
Robert Love42e9a922008-12-09 15:10:17 -0800399 */
400static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len)
401{
402 struct fc_lport *lport;
403 struct fc_gpn_ft_resp *np;
404 char *bp;
405 size_t plen;
406 size_t tlen;
407 int error = 0;
Joe Eykholt795d86f2009-08-25 14:00:39 -0700408 struct fc_rport_identifiers ids;
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700409 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800410
Joe Eykholt06852302010-07-20 15:19:53 -0700411 lport = fc_disc_lport(disc);
Joe Eykholta1c1e4e2009-08-25 14:02:22 -0700412 disc->seq_count++;
Robert Love42e9a922008-12-09 15:10:17 -0800413
414 /*
415 * Handle partial name record left over from previous call.
416 */
417 bp = buf;
418 plen = len;
419 np = (struct fc_gpn_ft_resp *)bp;
420 tlen = disc->buf_len;
Joe Eykholt81a67b92009-08-25 14:02:43 -0700421 disc->buf_len = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800422 if (tlen) {
423 WARN_ON(tlen >= sizeof(*np));
424 plen = sizeof(*np) - tlen;
425 WARN_ON(plen <= 0);
426 WARN_ON(plen >= sizeof(*np));
427 if (plen > len)
428 plen = len;
429 np = &disc->partial_buf;
430 memcpy((char *)np + tlen, bp, plen);
431
432 /*
433 * Set bp so that the loop below will advance it to the
434 * first valid full name element.
435 */
436 bp -= tlen;
437 len += tlen;
438 plen += tlen;
439 disc->buf_len = (unsigned char) plen;
440 if (plen == sizeof(*np))
441 disc->buf_len = 0;
442 }
443
444 /*
445 * Handle full name records, including the one filled from above.
446 * Normally, np == bp and plen == len, but from the partial case above,
447 * bp, len describe the overall buffer, and np, plen describe the
448 * partial buffer, which if would usually be full now.
449 * After the first time through the loop, things return to "normal".
450 */
451 while (plen >= sizeof(*np)) {
Joe Eykholt795d86f2009-08-25 14:00:39 -0700452 ids.port_id = ntoh24(np->fp_fid);
453 ids.port_name = ntohll(np->fp_wwpn);
Robert Love42e9a922008-12-09 15:10:17 -0800454
Robert Love7b2787e2010-05-07 15:18:41 -0700455 if (ids.port_id != lport->port_id &&
Joe Eykholt795d86f2009-08-25 14:00:39 -0700456 ids.port_name != lport->wwpn) {
Robert Love9737e6a2009-08-25 14:02:59 -0700457 rdata = lport->tt.rport_create(lport, ids.port_id);
458 if (rdata) {
459 rdata->ids.port_name = ids.port_name;
Joe Eykholt0f6c614982009-08-25 14:02:11 -0700460 rdata->disc_id = disc->disc_id;
Robert Love9737e6a2009-08-25 14:02:59 -0700461 } else {
Robert Love74147052009-06-10 15:31:10 -0700462 printk(KERN_WARNING "libfc: Failed to allocate "
463 "memory for the newly discovered port "
Chris Leechce8b5df2010-04-09 14:23:10 -0700464 "(%6.6x)\n", ids.port_id);
Joe Eykholt81a67b92009-08-25 14:02:43 -0700465 error = -ENOMEM;
466 }
Robert Love42e9a922008-12-09 15:10:17 -0800467 }
468
469 if (np->fp_flags & FC_NS_FID_LAST) {
Joe Eykholt786681b2009-08-25 14:01:29 -0700470 fc_disc_done(disc, DISC_EV_SUCCESS);
Robert Love42e9a922008-12-09 15:10:17 -0800471 len = 0;
472 break;
473 }
474 len -= sizeof(*np);
475 bp += sizeof(*np);
476 np = (struct fc_gpn_ft_resp *)bp;
477 plen = len;
478 }
479
480 /*
481 * Save any partial record at the end of the buffer for next time.
482 */
483 if (error == 0 && len > 0 && len < sizeof(*np)) {
484 if (np != &disc->partial_buf) {
Robert Love74147052009-06-10 15:31:10 -0700485 FC_DISC_DBG(disc, "Partial buffer remains "
486 "for discovery\n");
Robert Love42e9a922008-12-09 15:10:17 -0800487 memcpy(&disc->partial_buf, np, len);
488 }
489 disc->buf_len = (unsigned char) len;
Robert Love42e9a922008-12-09 15:10:17 -0800490 }
491 return error;
492}
493
Robert Love34f42a02009-02-27 10:55:45 -0800494/**
Robert Love3a3b42b2009-11-03 11:47:39 -0800495 * fc_disc_timeout() - Handler for discovery timeouts
496 * @work: Structure holding discovery context that needs to retry discovery
Robert Love42e9a922008-12-09 15:10:17 -0800497 */
498static void fc_disc_timeout(struct work_struct *work)
499{
500 struct fc_disc *disc = container_of(work,
501 struct fc_disc,
502 disc_work.work);
503 mutex_lock(&disc->disc_mutex);
Joe Eykholt3667d7e2009-08-25 14:02:38 -0700504 fc_disc_gpn_ft_req(disc);
Robert Love42e9a922008-12-09 15:10:17 -0800505 mutex_unlock(&disc->disc_mutex);
506}
507
508/**
Robert Love34f42a02009-02-27 10:55:45 -0800509 * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT)
Robert Love3a3b42b2009-11-03 11:47:39 -0800510 * @sp: The sequence that the GPN_FT response was received on
511 * @fp: The GPN_FT response frame
512 * @lp_arg: The discovery context
Robert Love42e9a922008-12-09 15:10:17 -0800513 *
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700514 * Locking Note: This function is called without disc mutex held, and
515 * should do all its processing with the mutex held
Robert Love42e9a922008-12-09 15:10:17 -0800516 */
517static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp,
518 void *disc_arg)
519{
520 struct fc_disc *disc = disc_arg;
521 struct fc_ct_hdr *cp;
522 struct fc_frame_header *fh;
Joe Eykholta1c1e4e2009-08-25 14:02:22 -0700523 enum fc_disc_event event = DISC_EV_NONE;
Robert Love42e9a922008-12-09 15:10:17 -0800524 unsigned int seq_cnt;
Robert Love42e9a922008-12-09 15:10:17 -0800525 unsigned int len;
Joe Eykholta1c1e4e2009-08-25 14:02:22 -0700526 int error = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800527
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700528 mutex_lock(&disc->disc_mutex);
Robert Love74147052009-06-10 15:31:10 -0700529 FC_DISC_DBG(disc, "Received a GPN_FT response\n");
Robert Love42e9a922008-12-09 15:10:17 -0800530
531 if (IS_ERR(fp)) {
532 fc_disc_error(disc, fp);
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700533 mutex_unlock(&disc->disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800534 return;
535 }
536
537 WARN_ON(!fc_frame_is_linear(fp)); /* buffer must be contiguous */
538 fh = fc_frame_header_get(fp);
539 len = fr_len(fp) - sizeof(*fh);
540 seq_cnt = ntohs(fh->fh_seq_cnt);
Joe Eykholta1c1e4e2009-08-25 14:02:22 -0700541 if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 && disc->seq_count == 0) {
Robert Love42e9a922008-12-09 15:10:17 -0800542 cp = fc_frame_payload_get(fp, sizeof(*cp));
543 if (!cp) {
Robert Love74147052009-06-10 15:31:10 -0700544 FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n",
545 fr_len(fp));
Joe Eykholt883a3372009-08-25 14:02:27 -0700546 event = DISC_EV_FAILED;
Robert Love42e9a922008-12-09 15:10:17 -0800547 } else if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
548
Robert Love34f42a02009-02-27 10:55:45 -0800549 /* Accepted, parse the response. */
Robert Love42e9a922008-12-09 15:10:17 -0800550 len -= sizeof(*cp);
Joe Eykholta1c1e4e2009-08-25 14:02:22 -0700551 error = fc_disc_gpn_ft_parse(disc, cp + 1, len);
Robert Love42e9a922008-12-09 15:10:17 -0800552 } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
Robert Love74147052009-06-10 15:31:10 -0700553 FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x "
554 "(check zoning)\n", cp->ct_reason,
555 cp->ct_explan);
Joe Eykholta1c1e4e2009-08-25 14:02:22 -0700556 event = DISC_EV_FAILED;
Joe Eykholtc7626082009-08-25 14:02:33 -0700557 if (cp->ct_reason == FC_FS_RJT_UNABL &&
558 cp->ct_explan == FC_FS_EXP_FTNR)
559 event = DISC_EV_SUCCESS;
Robert Love42e9a922008-12-09 15:10:17 -0800560 } else {
Robert Love74147052009-06-10 15:31:10 -0700561 FC_DISC_DBG(disc, "GPN_FT unexpected response code "
562 "%x\n", ntohs(cp->ct_cmd));
Joe Eykholt883a3372009-08-25 14:02:27 -0700563 event = DISC_EV_FAILED;
Robert Love42e9a922008-12-09 15:10:17 -0800564 }
Joe Eykholta1c1e4e2009-08-25 14:02:22 -0700565 } else if (fr_sof(fp) == FC_SOF_N3 && seq_cnt == disc->seq_count) {
566 error = fc_disc_gpn_ft_parse(disc, fh + 1, len);
Robert Love42e9a922008-12-09 15:10:17 -0800567 } else {
Robert Love74147052009-06-10 15:31:10 -0700568 FC_DISC_DBG(disc, "GPN_FT unexpected frame - out of sequence? "
569 "seq_cnt %x expected %x sof %x eof %x\n",
570 seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp));
Joe Eykholt883a3372009-08-25 14:02:27 -0700571 event = DISC_EV_FAILED;
Robert Love42e9a922008-12-09 15:10:17 -0800572 }
Joe Eykholta1c1e4e2009-08-25 14:02:22 -0700573 if (error)
574 fc_disc_error(disc, fp);
575 else if (event != DISC_EV_NONE)
576 fc_disc_done(disc, event);
Robert Love42e9a922008-12-09 15:10:17 -0800577 fc_frame_free(fp);
Abhijeet Joglekar0d228c02009-04-21 16:26:52 -0700578 mutex_unlock(&disc->disc_mutex);
Robert Love42e9a922008-12-09 15:10:17 -0800579}
580
581/**
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700582 * fc_disc_gpn_id_resp() - Handle a response frame from Get Port Names (GPN_ID)
Robert Love3a3b42b2009-11-03 11:47:39 -0800583 * @sp: The sequence the GPN_ID is on
584 * @fp: The response frame
585 * @rdata_arg: The remote port that sent the GPN_ID response
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700586 *
587 * Locking Note: This function is called without disc mutex held.
588 */
589static void fc_disc_gpn_id_resp(struct fc_seq *sp, struct fc_frame *fp,
590 void *rdata_arg)
591{
592 struct fc_rport_priv *rdata = rdata_arg;
593 struct fc_rport_priv *new_rdata;
594 struct fc_lport *lport;
595 struct fc_disc *disc;
596 struct fc_ct_hdr *cp;
597 struct fc_ns_gid_pn *pn;
598 u64 port_name;
599
600 lport = rdata->local_port;
601 disc = &lport->disc;
602
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700603 if (PTR_ERR(fp) == -FC_EX_CLOSED)
604 goto out;
605 if (IS_ERR(fp))
606 goto redisc;
607
608 cp = fc_frame_payload_get(fp, sizeof(*cp));
609 if (!cp)
610 goto redisc;
611 if (ntohs(cp->ct_cmd) == FC_FS_ACC) {
612 if (fr_len(fp) < sizeof(struct fc_frame_header) +
613 sizeof(*cp) + sizeof(*pn))
614 goto redisc;
615 pn = (struct fc_ns_gid_pn *)(cp + 1);
616 port_name = get_unaligned_be64(&pn->fn_wwpn);
Hannes Reineckea407c592016-09-30 11:01:15 +0200617 mutex_lock(&rdata->rp_mutex);
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700618 if (rdata->ids.port_name == -1)
619 rdata->ids.port_name = port_name;
620 else if (rdata->ids.port_name != port_name) {
621 FC_DISC_DBG(disc, "GPN_ID accepted. WWPN changed. "
Chris Leech9f8f3aa2010-04-09 14:23:16 -0700622 "Port-id %6.6x wwpn %16.16llx\n",
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700623 rdata->ids.port_id, port_name);
Hannes Reineckea407c592016-09-30 11:01:15 +0200624 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700625 lport->tt.rport_logoff(rdata);
Hannes Reineckea407c592016-09-30 11:01:15 +0200626 mutex_lock(&lport->disc.disc_mutex);
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700627 new_rdata = lport->tt.rport_create(lport,
628 rdata->ids.port_id);
Hannes Reineckea407c592016-09-30 11:01:15 +0200629 mutex_unlock(&lport->disc.disc_mutex);
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700630 if (new_rdata) {
631 new_rdata->disc_id = disc->disc_id;
632 lport->tt.rport_login(new_rdata);
633 }
634 goto out;
635 }
636 rdata->disc_id = disc->disc_id;
Hannes Reineckea407c592016-09-30 11:01:15 +0200637 mutex_unlock(&rdata->rp_mutex);
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700638 lport->tt.rport_login(rdata);
639 } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) {
640 FC_DISC_DBG(disc, "GPN_ID rejected reason %x exp %x\n",
641 cp->ct_reason, cp->ct_explan);
642 lport->tt.rport_logoff(rdata);
643 } else {
644 FC_DISC_DBG(disc, "GPN_ID unexpected response code %x\n",
645 ntohs(cp->ct_cmd));
646redisc:
Hannes Reineckea407c592016-09-30 11:01:15 +0200647 mutex_lock(&disc->disc_mutex);
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700648 fc_disc_restart(disc);
Hannes Reineckea407c592016-09-30 11:01:15 +0200649 mutex_unlock(&disc->disc_mutex);
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700650 }
651out:
Hannes Reinecke944ef962016-10-18 10:01:39 +0200652 kref_put(&rdata->kref, fc_rport_destroy);
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700653}
654
655/**
656 * fc_disc_gpn_id_req() - Send Get Port Names by ID (GPN_ID) request
Robert Love3a3b42b2009-11-03 11:47:39 -0800657 * @lport: The local port to initiate discovery on
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700658 * @rdata: remote port private data
659 *
660 * Locking Note: This function expects that the disc_mutex is locked
661 * before it is called.
662 * On failure, an error code is returned.
663 */
664static int fc_disc_gpn_id_req(struct fc_lport *lport,
665 struct fc_rport_priv *rdata)
666{
667 struct fc_frame *fp;
668
669 fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) +
670 sizeof(struct fc_ns_fid));
671 if (!fp)
672 return -ENOMEM;
673 if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, FC_NS_GPN_ID,
Joe Eykholtb94f8952009-11-03 11:50:21 -0800674 fc_disc_gpn_id_resp, rdata,
675 3 * lport->r_a_tov))
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700676 return -ENOMEM;
677 kref_get(&rdata->kref);
678 return 0;
679}
680
681/**
Robert Love34f42a02009-02-27 10:55:45 -0800682 * fc_disc_single() - Discover the directory information for a single target
Robert Love3a3b42b2009-11-03 11:47:39 -0800683 * @lport: The local port the remote port is associated with
684 * @dp: The port to rediscover
Robert Love42e9a922008-12-09 15:10:17 -0800685 *
686 * Locking Note: This function expects that the disc_mutex is locked
687 * before it is called.
688 */
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700689static int fc_disc_single(struct fc_lport *lport, struct fc_disc_port *dp)
Robert Love42e9a922008-12-09 15:10:17 -0800690{
Joe Eykholtab28f1f2009-08-25 14:00:34 -0700691 struct fc_rport_priv *rdata;
Robert Love42e9a922008-12-09 15:10:17 -0800692
Robert Love9737e6a2009-08-25 14:02:59 -0700693 rdata = lport->tt.rport_create(lport, dp->port_id);
Joe Eykholt2ab7e1e2009-08-25 14:03:58 -0700694 if (!rdata)
695 return -ENOMEM;
696 rdata->disc_id = 0;
697 return fc_disc_gpn_id_req(lport, rdata);
Robert Love42e9a922008-12-09 15:10:17 -0800698}
699
700/**
Robert Love34f42a02009-02-27 10:55:45 -0800701 * fc_disc_stop() - Stop discovery for a given lport
Robert Love3a3b42b2009-11-03 11:47:39 -0800702 * @lport: The local port that discovery should stop on
Robert Love42e9a922008-12-09 15:10:17 -0800703 */
Bart Van Asschec6b21c92012-01-13 17:26:20 -0800704static void fc_disc_stop(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -0800705{
706 struct fc_disc *disc = &lport->disc;
707
Bhanu Prakash Gollapudic531b9b2010-10-08 17:12:36 -0700708 if (disc->pending)
Robert Love42e9a922008-12-09 15:10:17 -0800709 cancel_delayed_work_sync(&disc->disc_work);
Bhanu Prakash Gollapudic531b9b2010-10-08 17:12:36 -0700710 fc_disc_stop_rports(disc);
Robert Love42e9a922008-12-09 15:10:17 -0800711}
712
713/**
Robert Love34f42a02009-02-27 10:55:45 -0800714 * fc_disc_stop_final() - Stop discovery for a given lport
Robert Love3a3b42b2009-11-03 11:47:39 -0800715 * @lport: The lport that discovery should stop on
Robert Love42e9a922008-12-09 15:10:17 -0800716 *
717 * This function will block until discovery has been
718 * completely stopped and all rports have been deleted.
719 */
Bart Van Asschec6b21c92012-01-13 17:26:20 -0800720static void fc_disc_stop_final(struct fc_lport *lport)
Robert Love42e9a922008-12-09 15:10:17 -0800721{
722 fc_disc_stop(lport);
723 lport->tt.rport_flush_queue();
724}
725
726/**
Robert Love08076192013-03-25 11:00:28 -0700727 * fc_disc_config() - Configure the discovery layer for a local port
728 * @lport: The local port that needs the discovery layer to be configured
Robert Love8a9a7132013-03-25 11:00:27 -0700729 * @priv: Private data structre for users of the discovery layer
Robert Love42e9a922008-12-09 15:10:17 -0800730 */
Robert Love08076192013-03-25 11:00:28 -0700731void fc_disc_config(struct fc_lport *lport, void *priv)
Robert Love42e9a922008-12-09 15:10:17 -0800732{
Robert Love08076192013-03-25 11:00:28 -0700733 struct fc_disc *disc = &lport->disc;
Robert Love42e9a922008-12-09 15:10:17 -0800734
735 if (!lport->tt.disc_start)
736 lport->tt.disc_start = fc_disc_start;
737
738 if (!lport->tt.disc_stop)
739 lport->tt.disc_stop = fc_disc_stop;
740
741 if (!lport->tt.disc_stop_final)
742 lport->tt.disc_stop_final = fc_disc_stop_final;
743
744 if (!lport->tt.disc_recv_req)
745 lport->tt.disc_recv_req = fc_disc_recv_req;
746
Robert Love42e9a922008-12-09 15:10:17 -0800747 disc = &lport->disc;
Robert Love08076192013-03-25 11:00:28 -0700748
749 disc->priv = priv;
750}
751EXPORT_SYMBOL(fc_disc_config);
752
753/**
754 * fc_disc_init() - Initialize the discovery layer for a local port
755 * @lport: The local port that needs the discovery layer to be initialized
756 */
757void fc_disc_init(struct fc_lport *lport)
758{
759 struct fc_disc *disc = &lport->disc;
760
Robert Love42e9a922008-12-09 15:10:17 -0800761 INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout);
762 mutex_init(&disc->disc_mutex);
763 INIT_LIST_HEAD(&disc->rports);
Robert Love42e9a922008-12-09 15:10:17 -0800764}
765EXPORT_SYMBOL(fc_disc_init);