]>
Commit | Line | Data |
---|---|---|
42e9a92f RL |
1 | /* |
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 | ||
42e9a92f RL |
46 | static void fc_disc_gpn_ft_req(struct fc_disc *); |
47 | static void fc_disc_gpn_ft_resp(struct fc_seq *, struct fc_frame *, void *); | |
f211fa51 | 48 | static int fc_disc_new_target(struct fc_disc *, struct fc_rport_priv *, |
42e9a92f | 49 | struct fc_rport_identifiers *); |
786681b9 | 50 | static void fc_disc_done(struct fc_disc *, enum fc_disc_event); |
42e9a92f RL |
51 | static void fc_disc_timeout(struct work_struct *); |
52 | static void fc_disc_single(struct fc_disc *, struct fc_disc_port *); | |
53 | static void fc_disc_restart(struct fc_disc *); | |
54 | ||
42e9a92f | 55 | /** |
34f42a07 | 56 | * fc_disc_stop_rports() - delete all the remote ports associated with the lport |
42e9a92f RL |
57 | * @disc: The discovery job to stop rports on |
58 | * | |
59 | * Locking Note: This function expects that the lport mutex is locked before | |
60 | * calling it. | |
61 | */ | |
62 | void fc_disc_stop_rports(struct fc_disc *disc) | |
63 | { | |
64 | struct fc_lport *lport; | |
ab28f1fd | 65 | struct fc_rport_priv *rdata, *next; |
42e9a92f RL |
66 | |
67 | lport = disc->lport; | |
68 | ||
69 | mutex_lock(&disc->disc_mutex); | |
9e9d0452 | 70 | list_for_each_entry_safe(rdata, next, &disc->rports, peers) |
9fb9d328 | 71 | lport->tt.rport_logoff(rdata); |
42e9a92f RL |
72 | mutex_unlock(&disc->disc_mutex); |
73 | } | |
74 | ||
42e9a92f | 75 | /** |
34f42a07 | 76 | * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN) |
42e9a92f RL |
77 | * @sp: Current sequence of the RSCN exchange |
78 | * @fp: RSCN Frame | |
79 | * @lport: Fibre Channel host port instance | |
80 | * | |
81 | * Locking Note: This function expects that the disc_mutex is locked | |
82 | * before it is called. | |
83 | */ | |
84 | static void fc_disc_recv_rscn_req(struct fc_seq *sp, struct fc_frame *fp, | |
85 | struct fc_disc *disc) | |
86 | { | |
87 | struct fc_lport *lport; | |
ab28f1fd | 88 | struct fc_rport_priv *rdata; |
42e9a92f RL |
89 | struct fc_els_rscn *rp; |
90 | struct fc_els_rscn_page *pp; | |
91 | struct fc_seq_els_data rjt_data; | |
92 | unsigned int len; | |
93 | int redisc = 0; | |
94 | enum fc_els_rscn_ev_qual ev_qual; | |
95 | enum fc_els_rscn_addr_fmt fmt; | |
96 | LIST_HEAD(disc_ports); | |
97 | struct fc_disc_port *dp, *next; | |
98 | ||
99 | lport = disc->lport; | |
100 | ||
7414705e | 101 | FC_DISC_DBG(disc, "Received an RSCN event\n"); |
42e9a92f RL |
102 | |
103 | /* make sure the frame contains an RSCN message */ | |
104 | rp = fc_frame_payload_get(fp, sizeof(*rp)); | |
105 | if (!rp) | |
106 | goto reject; | |
107 | /* make sure the page length is as expected (4 bytes) */ | |
108 | if (rp->rscn_page_len != sizeof(*pp)) | |
109 | goto reject; | |
110 | /* get the RSCN payload length */ | |
111 | len = ntohs(rp->rscn_plen); | |
112 | if (len < sizeof(*rp)) | |
113 | goto reject; | |
114 | /* make sure the frame contains the expected payload */ | |
115 | rp = fc_frame_payload_get(fp, len); | |
116 | if (!rp) | |
117 | goto reject; | |
118 | /* payload must be a multiple of the RSCN page size */ | |
119 | len -= sizeof(*rp); | |
120 | if (len % sizeof(*pp)) | |
121 | goto reject; | |
122 | ||
123 | for (pp = (void *)(rp + 1); len > 0; len -= sizeof(*pp), pp++) { | |
124 | ev_qual = pp->rscn_page_flags >> ELS_RSCN_EV_QUAL_BIT; | |
125 | ev_qual &= ELS_RSCN_EV_QUAL_MASK; | |
126 | fmt = pp->rscn_page_flags >> ELS_RSCN_ADDR_FMT_BIT; | |
127 | fmt &= ELS_RSCN_ADDR_FMT_MASK; | |
128 | /* | |
129 | * if we get an address format other than port | |
130 | * (area, domain, fabric), then do a full discovery | |
131 | */ | |
132 | switch (fmt) { | |
133 | case ELS_ADDR_FMT_PORT: | |
7414705e RL |
134 | FC_DISC_DBG(disc, "Port address format for port " |
135 | "(%6x)\n", ntoh24(pp->rscn_fid)); | |
42e9a92f RL |
136 | dp = kzalloc(sizeof(*dp), GFP_KERNEL); |
137 | if (!dp) { | |
138 | redisc = 1; | |
139 | break; | |
140 | } | |
141 | dp->lp = lport; | |
142 | dp->ids.port_id = ntoh24(pp->rscn_fid); | |
143 | dp->ids.port_name = -1; | |
144 | dp->ids.node_name = -1; | |
145 | dp->ids.roles = FC_RPORT_ROLE_UNKNOWN; | |
146 | list_add_tail(&dp->peers, &disc_ports); | |
147 | break; | |
148 | case ELS_ADDR_FMT_AREA: | |
149 | case ELS_ADDR_FMT_DOM: | |
150 | case ELS_ADDR_FMT_FAB: | |
151 | default: | |
7414705e | 152 | FC_DISC_DBG(disc, "Address format is (%d)\n", fmt); |
42e9a92f RL |
153 | redisc = 1; |
154 | break; | |
155 | } | |
156 | } | |
157 | lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL); | |
158 | if (redisc) { | |
7414705e | 159 | FC_DISC_DBG(disc, "RSCN received: rediscovering\n"); |
42e9a92f RL |
160 | fc_disc_restart(disc); |
161 | } else { | |
7414705e RL |
162 | FC_DISC_DBG(disc, "RSCN received: not rediscovering. " |
163 | "redisc %d state %d in_prog %d\n", | |
164 | redisc, lport->state, disc->pending); | |
42e9a92f RL |
165 | list_for_each_entry_safe(dp, next, &disc_ports, peers) { |
166 | list_del(&dp->peers); | |
9fb9d328 JE |
167 | rdata = lport->tt.rport_lookup(lport, dp->ids.port_id); |
168 | if (rdata) { | |
9fb9d328 | 169 | lport->tt.rport_logoff(rdata); |
42e9a92f RL |
170 | } |
171 | fc_disc_single(disc, dp); | |
172 | } | |
173 | } | |
174 | fc_frame_free(fp); | |
175 | return; | |
176 | reject: | |
7414705e | 177 | FC_DISC_DBG(disc, "Received a bad RSCN frame\n"); |
42e9a92f RL |
178 | rjt_data.fp = NULL; |
179 | rjt_data.reason = ELS_RJT_LOGIC; | |
180 | rjt_data.explan = ELS_EXPL_NONE; | |
181 | lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data); | |
182 | fc_frame_free(fp); | |
183 | } | |
184 | ||
185 | /** | |
34f42a07 | 186 | * fc_disc_recv_req() - Handle incoming requests |
42e9a92f RL |
187 | * @sp: Current sequence of the request exchange |
188 | * @fp: The frame | |
189 | * @lport: The FC local port | |
190 | * | |
191 | * Locking Note: This function is called from the EM and will lock | |
192 | * the disc_mutex before calling the handler for the | |
193 | * request. | |
194 | */ | |
195 | static void fc_disc_recv_req(struct fc_seq *sp, struct fc_frame *fp, | |
196 | struct fc_lport *lport) | |
197 | { | |
198 | u8 op; | |
199 | struct fc_disc *disc = &lport->disc; | |
200 | ||
201 | op = fc_frame_payload_op(fp); | |
202 | switch (op) { | |
203 | case ELS_RSCN: | |
204 | mutex_lock(&disc->disc_mutex); | |
205 | fc_disc_recv_rscn_req(sp, fp, disc); | |
206 | mutex_unlock(&disc->disc_mutex); | |
207 | break; | |
208 | default: | |
7414705e RL |
209 | FC_DISC_DBG(disc, "Received an unsupported request, " |
210 | "the opcode is (%x)\n", op); | |
42e9a92f RL |
211 | break; |
212 | } | |
213 | } | |
214 | ||
215 | /** | |
34f42a07 | 216 | * fc_disc_restart() - Restart discovery |
42e9a92f RL |
217 | * @lport: FC discovery context |
218 | * | |
219 | * Locking Note: This function expects that the disc mutex | |
220 | * is already locked. | |
221 | */ | |
222 | static void fc_disc_restart(struct fc_disc *disc) | |
223 | { | |
ab28f1fd | 224 | struct fc_rport_priv *rdata, *next; |
42e9a92f RL |
225 | struct fc_lport *lport = disc->lport; |
226 | ||
7414705e | 227 | FC_DISC_DBG(disc, "Restarting discovery\n"); |
42e9a92f | 228 | |
9e9d0452 | 229 | list_for_each_entry_safe(rdata, next, &disc->rports, peers) |
9fb9d328 | 230 | lport->tt.rport_logoff(rdata); |
42e9a92f RL |
231 | |
232 | disc->requested = 1; | |
233 | if (!disc->pending) | |
234 | fc_disc_gpn_ft_req(disc); | |
235 | } | |
236 | ||
237 | /** | |
34f42a07 | 238 | * fc_disc_start() - Fibre Channel Target discovery |
42e9a92f RL |
239 | * @lport: FC local port |
240 | * | |
241 | * Returns non-zero if discovery cannot be started. | |
242 | */ | |
243 | static void fc_disc_start(void (*disc_callback)(struct fc_lport *, | |
244 | enum fc_disc_event), | |
245 | struct fc_lport *lport) | |
246 | { | |
9fb9d328 | 247 | struct fc_rport_priv *rdata; |
42e9a92f RL |
248 | struct fc_disc *disc = &lport->disc; |
249 | ||
250 | /* | |
251 | * At this point we may have a new disc job or an existing | |
252 | * one. Either way, let's lock when we make changes to it | |
253 | * and send the GPN_FT request. | |
254 | */ | |
255 | mutex_lock(&disc->disc_mutex); | |
256 | ||
257 | disc->disc_callback = disc_callback; | |
258 | ||
259 | /* | |
260 | * If not ready, or already running discovery, just set request flag. | |
261 | */ | |
262 | disc->requested = 1; | |
263 | ||
264 | if (disc->pending) { | |
265 | mutex_unlock(&disc->disc_mutex); | |
266 | return; | |
267 | } | |
268 | ||
269 | /* | |
270 | * Handle point-to-point mode as a simple discovery | |
271 | * of the remote port. Yucky, yucky, yuck, yuck! | |
272 | */ | |
9fb9d328 JE |
273 | rdata = disc->lport->ptp_rp; |
274 | if (rdata) { | |
f211fa51 JE |
275 | kref_get(&rdata->kref); |
276 | if (!fc_disc_new_target(disc, rdata, &rdata->ids)) { | |
786681b9 | 277 | fc_disc_done(disc, DISC_EV_SUCCESS); |
42e9a92f | 278 | } |
f211fa51 | 279 | kref_put(&rdata->kref, rdata->local_port->tt.rport_destroy); |
42e9a92f RL |
280 | } else { |
281 | fc_disc_gpn_ft_req(disc); /* get ports by FC-4 type */ | |
282 | } | |
283 | ||
284 | mutex_unlock(&disc->disc_mutex); | |
285 | } | |
286 | ||
42e9a92f | 287 | /** |
34f42a07 | 288 | * fc_disc_new_target() - Handle new target found by discovery |
42e9a92f | 289 | * @lport: FC local port |
f211fa51 | 290 | * @rdata: The previous FC remote port priv (NULL if new remote port) |
42e9a92f RL |
291 | * @ids: Identifiers for the new FC remote port |
292 | * | |
293 | * Locking Note: This function expects that the disc_mutex is locked | |
294 | * before it is called. | |
295 | */ | |
296 | static int fc_disc_new_target(struct fc_disc *disc, | |
f211fa51 | 297 | struct fc_rport_priv *rdata, |
42e9a92f RL |
298 | struct fc_rport_identifiers *ids) |
299 | { | |
300 | struct fc_lport *lport = disc->lport; | |
42e9a92f RL |
301 | int error = 0; |
302 | ||
f211fa51 JE |
303 | if (rdata && ids->port_name) { |
304 | if (rdata->ids.port_name == -1) { | |
42e9a92f RL |
305 | /* |
306 | * Set WWN and fall through to notify of create. | |
307 | */ | |
f211fa51 JE |
308 | rdata->ids.port_name = ids->port_name; |
309 | rdata->ids.node_name = ids->node_name; | |
310 | } else if (rdata->ids.port_name != ids->port_name) { | |
42e9a92f RL |
311 | /* |
312 | * This is a new port with the same FCID as | |
313 | * a previously-discovered port. Presumably the old | |
314 | * port logged out and a new port logged in and was | |
315 | * assigned the same FCID. This should be rare. | |
316 | * Delete the old one and fall thru to re-create. | |
317 | */ | |
9fb9d328 | 318 | lport->tt.rport_logoff(rdata); |
f211fa51 | 319 | rdata = NULL; |
42e9a92f RL |
320 | } |
321 | } | |
322 | if (((ids->port_name != -1) || (ids->port_id != -1)) && | |
323 | ids->port_id != fc_host_port_id(lport->host) && | |
324 | ids->port_name != lport->wwpn) { | |
f211fa51 | 325 | if (!rdata) { |
19f97e3c JE |
326 | rdata = lport->tt.rport_create(lport, ids); |
327 | if (!rdata) | |
328 | error = -ENOMEM; | |
42e9a92f | 329 | } |
8345592b | 330 | if (rdata) |
9fb9d328 | 331 | lport->tt.rport_login(rdata); |
42e9a92f RL |
332 | } |
333 | return error; | |
334 | } | |
335 | ||
42e9a92f | 336 | /** |
34f42a07 | 337 | * fc_disc_done() - Discovery has been completed |
42e9a92f | 338 | * @disc: FC discovery context |
786681b9 JE |
339 | * @event: discovery completion status |
340 | * | |
0d228c0f AJ |
341 | * Locking Note: This function expects that the disc mutex is locked before |
342 | * it is called. The discovery callback is then made with the lock released, | |
343 | * and the lock is re-taken before returning from this function | |
42e9a92f | 344 | */ |
786681b9 | 345 | static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event) |
42e9a92f RL |
346 | { |
347 | struct fc_lport *lport = disc->lport; | |
348 | ||
7414705e | 349 | FC_DISC_DBG(disc, "Discovery complete\n"); |
42e9a92f | 350 | |
42e9a92f RL |
351 | if (disc->requested) |
352 | fc_disc_gpn_ft_req(disc); | |
353 | else | |
354 | disc->pending = 0; | |
0d228c0f AJ |
355 | |
356 | mutex_unlock(&disc->disc_mutex); | |
357 | disc->disc_callback(lport, event); | |
358 | mutex_lock(&disc->disc_mutex); | |
42e9a92f RL |
359 | } |
360 | ||
361 | /** | |
34f42a07 | 362 | * fc_disc_error() - Handle error on dNS request |
42e9a92f RL |
363 | * @disc: FC discovery context |
364 | * @fp: The frame pointer | |
365 | */ | |
366 | static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp) | |
367 | { | |
368 | struct fc_lport *lport = disc->lport; | |
369 | unsigned long delay = 0; | |
7414705e RL |
370 | |
371 | FC_DISC_DBG(disc, "Error %ld, retries %d/%d\n", | |
372 | PTR_ERR(fp), disc->retry_count, | |
373 | FC_DISC_RETRY_LIMIT); | |
42e9a92f RL |
374 | |
375 | if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) { | |
376 | /* | |
377 | * Memory allocation failure, or the exchange timed out, | |
378 | * retry after delay. | |
379 | */ | |
380 | if (disc->retry_count < FC_DISC_RETRY_LIMIT) { | |
381 | /* go ahead and retry */ | |
382 | if (!fp) | |
383 | delay = msecs_to_jiffies(FC_DISC_RETRY_DELAY); | |
384 | else { | |
385 | delay = msecs_to_jiffies(lport->e_d_tov); | |
386 | ||
387 | /* timeout faster first time */ | |
388 | if (!disc->retry_count) | |
389 | delay /= 4; | |
390 | } | |
391 | disc->retry_count++; | |
392 | schedule_delayed_work(&disc->disc_work, delay); | |
786681b9 JE |
393 | } else |
394 | fc_disc_done(disc, DISC_EV_FAILED); | |
42e9a92f RL |
395 | } |
396 | } | |
397 | ||
398 | /** | |
34f42a07 | 399 | * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request |
42e9a92f RL |
400 | * @lport: FC discovery context |
401 | * | |
402 | * Locking Note: This function expects that the disc_mutex is locked | |
403 | * before it is called. | |
404 | */ | |
405 | static void fc_disc_gpn_ft_req(struct fc_disc *disc) | |
406 | { | |
407 | struct fc_frame *fp; | |
408 | struct fc_lport *lport = disc->lport; | |
409 | ||
410 | WARN_ON(!fc_lport_test_ready(lport)); | |
411 | ||
412 | disc->pending = 1; | |
413 | disc->requested = 0; | |
414 | ||
415 | disc->buf_len = 0; | |
416 | disc->seq_count = 0; | |
417 | fp = fc_frame_alloc(lport, | |
418 | sizeof(struct fc_ct_hdr) + | |
419 | sizeof(struct fc_ns_gid_ft)); | |
420 | if (!fp) | |
421 | goto err; | |
422 | ||
a46f327a | 423 | if (lport->tt.elsct_send(lport, 0, fp, |
42e9a92f RL |
424 | FC_NS_GPN_FT, |
425 | fc_disc_gpn_ft_resp, | |
426 | disc, lport->e_d_tov)) | |
427 | return; | |
428 | err: | |
429 | fc_disc_error(disc, fp); | |
430 | } | |
431 | ||
432 | /** | |
786681b9 | 433 | * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response. |
42e9a92f RL |
434 | * @lport: Fibre Channel host port instance |
435 | * @buf: GPN_FT response buffer | |
436 | * @len: size of response buffer | |
786681b9 JE |
437 | * |
438 | * Goes through the list of IDs and names resulting from a request. | |
42e9a92f RL |
439 | */ |
440 | static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len) | |
441 | { | |
442 | struct fc_lport *lport; | |
443 | struct fc_gpn_ft_resp *np; | |
444 | char *bp; | |
445 | size_t plen; | |
446 | size_t tlen; | |
447 | int error = 0; | |
795d86f5 | 448 | struct fc_rport_identifiers ids; |
ab28f1fd | 449 | struct fc_rport_priv *rdata; |
42e9a92f RL |
450 | |
451 | lport = disc->lport; | |
452 | ||
453 | /* | |
454 | * Handle partial name record left over from previous call. | |
455 | */ | |
456 | bp = buf; | |
457 | plen = len; | |
458 | np = (struct fc_gpn_ft_resp *)bp; | |
459 | tlen = disc->buf_len; | |
460 | if (tlen) { | |
461 | WARN_ON(tlen >= sizeof(*np)); | |
462 | plen = sizeof(*np) - tlen; | |
463 | WARN_ON(plen <= 0); | |
464 | WARN_ON(plen >= sizeof(*np)); | |
465 | if (plen > len) | |
466 | plen = len; | |
467 | np = &disc->partial_buf; | |
468 | memcpy((char *)np + tlen, bp, plen); | |
469 | ||
470 | /* | |
471 | * Set bp so that the loop below will advance it to the | |
472 | * first valid full name element. | |
473 | */ | |
474 | bp -= tlen; | |
475 | len += tlen; | |
476 | plen += tlen; | |
477 | disc->buf_len = (unsigned char) plen; | |
478 | if (plen == sizeof(*np)) | |
479 | disc->buf_len = 0; | |
480 | } | |
481 | ||
482 | /* | |
483 | * Handle full name records, including the one filled from above. | |
484 | * Normally, np == bp and plen == len, but from the partial case above, | |
485 | * bp, len describe the overall buffer, and np, plen describe the | |
486 | * partial buffer, which if would usually be full now. | |
487 | * After the first time through the loop, things return to "normal". | |
488 | */ | |
489 | while (plen >= sizeof(*np)) { | |
795d86f5 JE |
490 | ids.port_id = ntoh24(np->fp_fid); |
491 | ids.port_name = ntohll(np->fp_wwpn); | |
492 | ids.node_name = -1; | |
493 | ids.roles = FC_RPORT_ROLE_UNKNOWN; | |
494 | ||
495 | if (ids.port_id != fc_host_port_id(lport->host) && | |
496 | ids.port_name != lport->wwpn) { | |
9fb9d328 | 497 | rdata = lport->tt.rport_create(lport, &ids); |
8345592b | 498 | if (rdata) |
9fb9d328 | 499 | lport->tt.rport_login(rdata); |
8345592b | 500 | else |
7414705e RL |
501 | printk(KERN_WARNING "libfc: Failed to allocate " |
502 | "memory for the newly discovered port " | |
795d86f5 | 503 | "(%6x)\n", ids.port_id); |
42e9a92f RL |
504 | } |
505 | ||
506 | if (np->fp_flags & FC_NS_FID_LAST) { | |
786681b9 | 507 | fc_disc_done(disc, DISC_EV_SUCCESS); |
42e9a92f RL |
508 | len = 0; |
509 | break; | |
510 | } | |
511 | len -= sizeof(*np); | |
512 | bp += sizeof(*np); | |
513 | np = (struct fc_gpn_ft_resp *)bp; | |
514 | plen = len; | |
515 | } | |
516 | ||
517 | /* | |
518 | * Save any partial record at the end of the buffer for next time. | |
519 | */ | |
520 | if (error == 0 && len > 0 && len < sizeof(*np)) { | |
521 | if (np != &disc->partial_buf) { | |
7414705e RL |
522 | FC_DISC_DBG(disc, "Partial buffer remains " |
523 | "for discovery\n"); | |
42e9a92f RL |
524 | memcpy(&disc->partial_buf, np, len); |
525 | } | |
526 | disc->buf_len = (unsigned char) len; | |
527 | } else { | |
528 | disc->buf_len = 0; | |
529 | } | |
530 | return error; | |
531 | } | |
532 | ||
34f42a07 RL |
533 | /** |
534 | * fc_disc_timeout() - Retry handler for the disc component | |
535 | * @work: Structure holding disc obj that needs retry discovery | |
536 | * | |
42e9a92f RL |
537 | * Handle retry of memory allocation for remote ports. |
538 | */ | |
539 | static void fc_disc_timeout(struct work_struct *work) | |
540 | { | |
541 | struct fc_disc *disc = container_of(work, | |
542 | struct fc_disc, | |
543 | disc_work.work); | |
544 | mutex_lock(&disc->disc_mutex); | |
545 | if (disc->requested && !disc->pending) | |
546 | fc_disc_gpn_ft_req(disc); | |
547 | mutex_unlock(&disc->disc_mutex); | |
548 | } | |
549 | ||
550 | /** | |
34f42a07 | 551 | * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT) |
42e9a92f RL |
552 | * @sp: Current sequence of GPN_FT exchange |
553 | * @fp: response frame | |
554 | * @lp_arg: Fibre Channel host port instance | |
555 | * | |
0d228c0f AJ |
556 | * Locking Note: This function is called without disc mutex held, and |
557 | * should do all its processing with the mutex held | |
42e9a92f RL |
558 | */ |
559 | static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp, | |
560 | void *disc_arg) | |
561 | { | |
562 | struct fc_disc *disc = disc_arg; | |
563 | struct fc_ct_hdr *cp; | |
564 | struct fc_frame_header *fh; | |
565 | unsigned int seq_cnt; | |
566 | void *buf = NULL; | |
567 | unsigned int len; | |
568 | int error; | |
569 | ||
0d228c0f | 570 | mutex_lock(&disc->disc_mutex); |
7414705e | 571 | FC_DISC_DBG(disc, "Received a GPN_FT response\n"); |
42e9a92f RL |
572 | |
573 | if (IS_ERR(fp)) { | |
574 | fc_disc_error(disc, fp); | |
0d228c0f | 575 | mutex_unlock(&disc->disc_mutex); |
42e9a92f RL |
576 | return; |
577 | } | |
578 | ||
579 | WARN_ON(!fc_frame_is_linear(fp)); /* buffer must be contiguous */ | |
580 | fh = fc_frame_header_get(fp); | |
581 | len = fr_len(fp) - sizeof(*fh); | |
582 | seq_cnt = ntohs(fh->fh_seq_cnt); | |
583 | if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 && | |
584 | disc->seq_count == 0) { | |
585 | cp = fc_frame_payload_get(fp, sizeof(*cp)); | |
586 | if (!cp) { | |
7414705e RL |
587 | FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n", |
588 | fr_len(fp)); | |
42e9a92f RL |
589 | } else if (ntohs(cp->ct_cmd) == FC_FS_ACC) { |
590 | ||
34f42a07 | 591 | /* Accepted, parse the response. */ |
42e9a92f RL |
592 | buf = cp + 1; |
593 | len -= sizeof(*cp); | |
594 | } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) { | |
7414705e RL |
595 | FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x " |
596 | "(check zoning)\n", cp->ct_reason, | |
597 | cp->ct_explan); | |
786681b9 | 598 | fc_disc_done(disc, DISC_EV_FAILED); |
42e9a92f | 599 | } else { |
7414705e RL |
600 | FC_DISC_DBG(disc, "GPN_FT unexpected response code " |
601 | "%x\n", ntohs(cp->ct_cmd)); | |
42e9a92f RL |
602 | } |
603 | } else if (fr_sof(fp) == FC_SOF_N3 && | |
604 | seq_cnt == disc->seq_count) { | |
605 | buf = fh + 1; | |
606 | } else { | |
7414705e RL |
607 | FC_DISC_DBG(disc, "GPN_FT unexpected frame - out of sequence? " |
608 | "seq_cnt %x expected %x sof %x eof %x\n", | |
609 | seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp)); | |
42e9a92f RL |
610 | } |
611 | if (buf) { | |
612 | error = fc_disc_gpn_ft_parse(disc, buf, len); | |
613 | if (error) | |
614 | fc_disc_error(disc, fp); | |
615 | else | |
616 | disc->seq_count++; | |
617 | } | |
618 | fc_frame_free(fp); | |
0d228c0f AJ |
619 | |
620 | mutex_unlock(&disc->disc_mutex); | |
42e9a92f RL |
621 | } |
622 | ||
623 | /** | |
34f42a07 | 624 | * fc_disc_single() - Discover the directory information for a single target |
42e9a92f RL |
625 | * @lport: FC local port |
626 | * @dp: The port to rediscover | |
627 | * | |
628 | * Locking Note: This function expects that the disc_mutex is locked | |
629 | * before it is called. | |
630 | */ | |
631 | static void fc_disc_single(struct fc_disc *disc, struct fc_disc_port *dp) | |
632 | { | |
633 | struct fc_lport *lport; | |
ab28f1fd | 634 | struct fc_rport_priv *rdata; |
42e9a92f RL |
635 | |
636 | lport = disc->lport; | |
637 | ||
638 | if (dp->ids.port_id == fc_host_port_id(lport->host)) | |
639 | goto out; | |
640 | ||
9fb9d328 JE |
641 | rdata = lport->tt.rport_create(lport, &dp->ids); |
642 | if (rdata) { | |
42e9a92f | 643 | kfree(dp); |
9fb9d328 | 644 | lport->tt.rport_login(rdata); |
42e9a92f RL |
645 | } |
646 | return; | |
647 | out: | |
648 | kfree(dp); | |
649 | } | |
650 | ||
651 | /** | |
34f42a07 | 652 | * fc_disc_stop() - Stop discovery for a given lport |
42e9a92f RL |
653 | * @lport: The lport that discovery should stop for |
654 | */ | |
655 | void fc_disc_stop(struct fc_lport *lport) | |
656 | { | |
657 | struct fc_disc *disc = &lport->disc; | |
658 | ||
659 | if (disc) { | |
660 | cancel_delayed_work_sync(&disc->disc_work); | |
661 | fc_disc_stop_rports(disc); | |
662 | } | |
663 | } | |
664 | ||
665 | /** | |
34f42a07 | 666 | * fc_disc_stop_final() - Stop discovery for a given lport |
42e9a92f RL |
667 | * @lport: The lport that discovery should stop for |
668 | * | |
669 | * This function will block until discovery has been | |
670 | * completely stopped and all rports have been deleted. | |
671 | */ | |
672 | void fc_disc_stop_final(struct fc_lport *lport) | |
673 | { | |
674 | fc_disc_stop(lport); | |
675 | lport->tt.rport_flush_queue(); | |
676 | } | |
677 | ||
678 | /** | |
34f42a07 | 679 | * fc_disc_init() - Initialize the discovery block |
42e9a92f RL |
680 | * @lport: FC local port |
681 | */ | |
682 | int fc_disc_init(struct fc_lport *lport) | |
683 | { | |
684 | struct fc_disc *disc; | |
685 | ||
686 | if (!lport->tt.disc_start) | |
687 | lport->tt.disc_start = fc_disc_start; | |
688 | ||
689 | if (!lport->tt.disc_stop) | |
690 | lport->tt.disc_stop = fc_disc_stop; | |
691 | ||
692 | if (!lport->tt.disc_stop_final) | |
693 | lport->tt.disc_stop_final = fc_disc_stop_final; | |
694 | ||
695 | if (!lport->tt.disc_recv_req) | |
696 | lport->tt.disc_recv_req = fc_disc_recv_req; | |
697 | ||
42e9a92f RL |
698 | disc = &lport->disc; |
699 | INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout); | |
700 | mutex_init(&disc->disc_mutex); | |
701 | INIT_LIST_HEAD(&disc->rports); | |
702 | ||
703 | disc->lport = lport; | |
42e9a92f RL |
704 | |
705 | return 0; | |
706 | } | |
707 | EXPORT_SYMBOL(fc_disc_init); |