Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 36 | #include <linux/slab.h> |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 37 | #include <linux/err.h> |
Paul Gortmaker | 0970366 | 2011-05-27 09:37:25 -0400 | [diff] [blame] | 38 | #include <linux/export.h> |
Ingo Molnar | b2d0910 | 2017-02-04 01:27:20 +0100 | [diff] [blame] | 39 | #include <linux/rculist.h> |
| 40 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 41 | #include <asm/unaligned.h> |
| 42 | |
| 43 | #include <scsi/fc/fc_gs.h> |
| 44 | |
| 45 | #include <scsi/libfc.h> |
| 46 | |
Robert Love | 8866a5d | 2009-11-03 11:45:58 -0800 | [diff] [blame] | 47 | #include "fc_libfc.h" |
| 48 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 49 | #define FC_DISC_RETRY_LIMIT 3 /* max retries */ |
| 50 | #define FC_DISC_RETRY_DELAY 500UL /* (msecs) delay */ |
| 51 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 52 | static void fc_disc_gpn_ft_req(struct fc_disc *); |
| 53 | static void fc_disc_gpn_ft_resp(struct fc_seq *, struct fc_frame *, void *); |
Joe Eykholt | 786681b | 2009-08-25 14:01:29 -0700 | [diff] [blame] | 54 | static void fc_disc_done(struct fc_disc *, enum fc_disc_event); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 55 | static void fc_disc_timeout(struct work_struct *); |
Joe Eykholt | 2ab7e1e | 2009-08-25 14:03:58 -0700 | [diff] [blame] | 56 | static int fc_disc_single(struct fc_lport *, struct fc_disc_port *); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 57 | static void fc_disc_restart(struct fc_disc *); |
| 58 | |
| 59 | /** |
Robert Love | 3a3b42b | 2009-11-03 11:47:39 -0800 | [diff] [blame] | 60 | * fc_disc_stop_rports() - Delete all the remote ports associated with the lport |
| 61 | * @disc: The discovery job to stop remote ports on |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 62 | * |
| 63 | * Locking Note: This function expects that the lport mutex is locked before |
| 64 | * calling it. |
| 65 | */ |
Bart Van Assche | c6b21c9 | 2012-01-13 17:26:20 -0800 | [diff] [blame] | 66 | static void fc_disc_stop_rports(struct fc_disc *disc) |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 67 | { |
| 68 | struct fc_lport *lport; |
Joe Eykholt | 42e9041 | 2010-07-20 15:19:37 -0700 | [diff] [blame] | 69 | struct fc_rport_priv *rdata; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 70 | |
Joe Eykholt | 0685230 | 2010-07-20 15:19:53 -0700 | [diff] [blame] | 71 | lport = fc_disc_lport(disc); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 72 | |
Hannes Reinecke | a407c59 | 2016-09-30 11:01:15 +0200 | [diff] [blame] | 73 | rcu_read_lock(); |
| 74 | list_for_each_entry_rcu(rdata, &disc->rports, peers) { |
| 75 | if (kref_get_unless_zero(&rdata->kref)) { |
Hannes Reinecke | c96c792 | 2016-10-18 10:01:44 +0200 | [diff] [blame] | 76 | fc_rport_logoff(rdata); |
Hannes Reinecke | 944ef96 | 2016-10-18 10:01:39 +0200 | [diff] [blame] | 77 | kref_put(&rdata->kref, fc_rport_destroy); |
Hannes Reinecke | a407c59 | 2016-09-30 11:01:15 +0200 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | rcu_read_unlock(); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 84 | * fc_disc_recv_rscn_req() - Handle Registered State Change Notification (RSCN) |
Joe Eykholt | 92261156 | 2010-07-20 15:21:12 -0700 | [diff] [blame] | 85 | * @disc: The discovery object to which the RSCN applies |
Robert Love | 3a3b42b | 2009-11-03 11:47:39 -0800 | [diff] [blame] | 86 | * @fp: The RSCN frame |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 87 | * |
| 88 | * Locking Note: This function expects that the disc_mutex is locked |
| 89 | * before it is called. |
| 90 | */ |
Joe Eykholt | 92261156 | 2010-07-20 15:21:12 -0700 | [diff] [blame] | 91 | static void fc_disc_recv_rscn_req(struct fc_disc *disc, struct fc_frame *fp) |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 92 | { |
| 93 | struct fc_lport *lport; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 94 | struct fc_els_rscn *rp; |
| 95 | struct fc_els_rscn_page *pp; |
| 96 | struct fc_seq_els_data rjt_data; |
| 97 | unsigned int len; |
| 98 | int redisc = 0; |
| 99 | enum fc_els_rscn_ev_qual ev_qual; |
| 100 | enum fc_els_rscn_addr_fmt fmt; |
| 101 | LIST_HEAD(disc_ports); |
| 102 | struct fc_disc_port *dp, *next; |
| 103 | |
Joe Eykholt | 0685230 | 2010-07-20 15:19:53 -0700 | [diff] [blame] | 104 | lport = fc_disc_lport(disc); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 105 | |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 106 | FC_DISC_DBG(disc, "Received an RSCN event\n"); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 107 | |
| 108 | /* make sure the frame contains an RSCN message */ |
| 109 | rp = fc_frame_payload_get(fp, sizeof(*rp)); |
| 110 | if (!rp) |
| 111 | goto reject; |
| 112 | /* make sure the page length is as expected (4 bytes) */ |
| 113 | if (rp->rscn_page_len != sizeof(*pp)) |
| 114 | goto reject; |
| 115 | /* get the RSCN payload length */ |
| 116 | len = ntohs(rp->rscn_plen); |
| 117 | if (len < sizeof(*rp)) |
| 118 | goto reject; |
| 119 | /* make sure the frame contains the expected payload */ |
| 120 | rp = fc_frame_payload_get(fp, len); |
| 121 | if (!rp) |
| 122 | goto reject; |
| 123 | /* payload must be a multiple of the RSCN page size */ |
| 124 | len -= sizeof(*rp); |
| 125 | if (len % sizeof(*pp)) |
| 126 | goto reject; |
| 127 | |
| 128 | for (pp = (void *)(rp + 1); len > 0; len -= sizeof(*pp), pp++) { |
| 129 | ev_qual = pp->rscn_page_flags >> ELS_RSCN_EV_QUAL_BIT; |
| 130 | ev_qual &= ELS_RSCN_EV_QUAL_MASK; |
| 131 | fmt = pp->rscn_page_flags >> ELS_RSCN_ADDR_FMT_BIT; |
| 132 | fmt &= ELS_RSCN_ADDR_FMT_MASK; |
| 133 | /* |
| 134 | * if we get an address format other than port |
| 135 | * (area, domain, fabric), then do a full discovery |
| 136 | */ |
| 137 | switch (fmt) { |
| 138 | case ELS_ADDR_FMT_PORT: |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 139 | FC_DISC_DBG(disc, "Port address format for port " |
Chris Leech | ce8b5df | 2010-04-09 14:23:10 -0700 | [diff] [blame] | 140 | "(%6.6x)\n", ntoh24(pp->rscn_fid)); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 141 | dp = kzalloc(sizeof(*dp), GFP_KERNEL); |
| 142 | if (!dp) { |
| 143 | redisc = 1; |
| 144 | break; |
| 145 | } |
| 146 | dp->lp = lport; |
Robert Love | 9737e6a | 2009-08-25 14:02:59 -0700 | [diff] [blame] | 147 | dp->port_id = ntoh24(pp->rscn_fid); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 148 | list_add_tail(&dp->peers, &disc_ports); |
| 149 | break; |
| 150 | case ELS_ADDR_FMT_AREA: |
| 151 | case ELS_ADDR_FMT_DOM: |
| 152 | case ELS_ADDR_FMT_FAB: |
| 153 | default: |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 154 | FC_DISC_DBG(disc, "Address format is (%d)\n", fmt); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 155 | redisc = 1; |
| 156 | break; |
| 157 | } |
| 158 | } |
Hannes Reinecke | 7ab24dd | 2016-10-18 10:01:35 +0200 | [diff] [blame] | 159 | fc_seq_els_rsp_send(fp, ELS_LS_ACC, NULL); |
Joe Eykholt | 2ab7e1e | 2009-08-25 14:03:58 -0700 | [diff] [blame] | 160 | |
| 161 | /* |
| 162 | * If not doing a complete rediscovery, do GPN_ID on |
| 163 | * the individual ports mentioned in the list. |
| 164 | * If any of these get an error, do a full rediscovery. |
| 165 | * In any case, go through the list and free the entries. |
| 166 | */ |
| 167 | list_for_each_entry_safe(dp, next, &disc_ports, peers) { |
| 168 | list_del(&dp->peers); |
| 169 | if (!redisc) |
| 170 | redisc = fc_disc_single(lport, dp); |
| 171 | kfree(dp); |
| 172 | } |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 173 | if (redisc) { |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 174 | FC_DISC_DBG(disc, "RSCN received: rediscovering\n"); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 175 | fc_disc_restart(disc); |
| 176 | } else { |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 177 | FC_DISC_DBG(disc, "RSCN received: not rediscovering. " |
| 178 | "redisc %d state %d in_prog %d\n", |
| 179 | redisc, lport->state, disc->pending); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 180 | } |
| 181 | fc_frame_free(fp); |
| 182 | return; |
| 183 | reject: |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 184 | FC_DISC_DBG(disc, "Received a bad RSCN frame\n"); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 185 | rjt_data.reason = ELS_RJT_LOGIC; |
| 186 | rjt_data.explan = ELS_EXPL_NONE; |
Hannes Reinecke | 7ab24dd | 2016-10-18 10:01:35 +0200 | [diff] [blame] | 187 | fc_seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 188 | fc_frame_free(fp); |
| 189 | } |
| 190 | |
| 191 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 192 | * fc_disc_recv_req() - Handle incoming requests |
Robert Love | 3a3b42b | 2009-11-03 11:47:39 -0800 | [diff] [blame] | 193 | * @lport: The local port receiving the request |
Joe Eykholt | 92261156 | 2010-07-20 15:21:12 -0700 | [diff] [blame] | 194 | * @fp: The request frame |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 195 | * |
| 196 | * Locking Note: This function is called from the EM and will lock |
| 197 | * the disc_mutex before calling the handler for the |
| 198 | * request. |
| 199 | */ |
Joe Eykholt | 92261156 | 2010-07-20 15:21:12 -0700 | [diff] [blame] | 200 | static void fc_disc_recv_req(struct fc_lport *lport, struct fc_frame *fp) |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 201 | { |
| 202 | u8 op; |
| 203 | struct fc_disc *disc = &lport->disc; |
| 204 | |
| 205 | op = fc_frame_payload_op(fp); |
| 206 | switch (op) { |
| 207 | case ELS_RSCN: |
| 208 | mutex_lock(&disc->disc_mutex); |
Joe Eykholt | 92261156 | 2010-07-20 15:21:12 -0700 | [diff] [blame] | 209 | fc_disc_recv_rscn_req(disc, fp); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 210 | mutex_unlock(&disc->disc_mutex); |
| 211 | break; |
| 212 | default: |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 213 | FC_DISC_DBG(disc, "Received an unsupported request, " |
| 214 | "the opcode is (%x)\n", op); |
Hillf Danton | 83383dd | 2011-05-16 16:45:35 -0700 | [diff] [blame] | 215 | fc_frame_free(fp); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 216 | break; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 221 | * fc_disc_restart() - Restart discovery |
Robert Love | 3a3b42b | 2009-11-03 11:47:39 -0800 | [diff] [blame] | 222 | * @disc: The discovery object to be restarted |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 223 | * |
| 224 | * Locking Note: This function expects that the disc mutex |
| 225 | * is already locked. |
| 226 | */ |
| 227 | static void fc_disc_restart(struct fc_disc *disc) |
| 228 | { |
Joe Eykholt | 935d0fc | 2009-08-25 14:02:54 -0700 | [diff] [blame] | 229 | if (!disc->disc_callback) |
| 230 | return; |
| 231 | |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 232 | FC_DISC_DBG(disc, "Restarting discovery\n"); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 233 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 234 | disc->requested = 1; |
Joe Eykholt | 0f6c61498 | 2009-08-25 14:02:11 -0700 | [diff] [blame] | 235 | if (disc->pending) |
| 236 | return; |
| 237 | |
| 238 | /* |
| 239 | * Advance disc_id. This is an arbitrary non-zero number that will |
| 240 | * match the value in the fc_rport_priv after discovery for all |
| 241 | * freshly-discovered remote ports. Avoid wrapping to zero. |
| 242 | */ |
| 243 | disc->disc_id = (disc->disc_id + 2) | 1; |
Joe Eykholt | 3667d7e | 2009-08-25 14:02:38 -0700 | [diff] [blame] | 244 | disc->retry_count = 0; |
Joe Eykholt | 0f6c61498 | 2009-08-25 14:02:11 -0700 | [diff] [blame] | 245 | fc_disc_gpn_ft_req(disc); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** |
Robert Love | 3a3b42b | 2009-11-03 11:47:39 -0800 | [diff] [blame] | 249 | * fc_disc_start() - Start discovery on a local port |
| 250 | * @lport: The local port to have discovery started on |
| 251 | * @disc_callback: Callback function to be called when discovery is complete |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 252 | */ |
| 253 | static void fc_disc_start(void (*disc_callback)(struct fc_lport *, |
| 254 | enum fc_disc_event), |
| 255 | struct fc_lport *lport) |
| 256 | { |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 257 | struct fc_disc *disc = &lport->disc; |
| 258 | |
| 259 | /* |
| 260 | * At this point we may have a new disc job or an existing |
| 261 | * one. Either way, let's lock when we make changes to it |
| 262 | * and send the GPN_FT request. |
| 263 | */ |
| 264 | mutex_lock(&disc->disc_mutex); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 265 | disc->disc_callback = disc_callback; |
Joe Eykholt | 29d898e | 2009-08-25 14:02:49 -0700 | [diff] [blame] | 266 | fc_disc_restart(disc); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 267 | mutex_unlock(&disc->disc_mutex); |
| 268 | } |
| 269 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 270 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 271 | * fc_disc_done() - Discovery has been completed |
Robert Love | 3a3b42b | 2009-11-03 11:47:39 -0800 | [diff] [blame] | 272 | * @disc: The discovery context |
| 273 | * @event: The discovery completion status |
Joe Eykholt | 786681b | 2009-08-25 14:01:29 -0700 | [diff] [blame] | 274 | * |
Abhijeet Joglekar | 0d228c0 | 2009-04-21 16:26:52 -0700 | [diff] [blame] | 275 | * Locking Note: This function expects that the disc mutex is locked before |
| 276 | * it is called. The discovery callback is then made with the lock released, |
| 277 | * and the lock is re-taken before returning from this function |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 278 | */ |
Joe Eykholt | 786681b | 2009-08-25 14:01:29 -0700 | [diff] [blame] | 279 | static void fc_disc_done(struct fc_disc *disc, enum fc_disc_event event) |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 280 | { |
Joe Eykholt | 0685230 | 2010-07-20 15:19:53 -0700 | [diff] [blame] | 281 | struct fc_lport *lport = fc_disc_lport(disc); |
Joe Eykholt | 0f6c61498 | 2009-08-25 14:02:11 -0700 | [diff] [blame] | 282 | struct fc_rport_priv *rdata; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 283 | |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 284 | FC_DISC_DBG(disc, "Discovery complete\n"); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 285 | |
Joe Eykholt | 0f6c61498 | 2009-08-25 14:02:11 -0700 | [diff] [blame] | 286 | disc->pending = 0; |
| 287 | if (disc->requested) { |
| 288 | fc_disc_restart(disc); |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | /* |
Robert Love | 3a3b42b | 2009-11-03 11:47:39 -0800 | [diff] [blame] | 293 | * Go through all remote ports. If they were found in the latest |
| 294 | * discovery, reverify or log them in. Otherwise, log them out. |
Joe Eykholt | 0f6c61498 | 2009-08-25 14:02:11 -0700 | [diff] [blame] | 295 | * Skip ports which were never discovered. These are the dNS port |
| 296 | * and ports which were created by PLOGI. |
| 297 | */ |
Hannes Reinecke | a407c59 | 2016-09-30 11:01:15 +0200 | [diff] [blame] | 298 | rcu_read_lock(); |
Joe Eykholt | 42e9041 | 2010-07-20 15:19:37 -0700 | [diff] [blame] | 299 | list_for_each_entry_rcu(rdata, &disc->rports, peers) { |
Hannes Reinecke | a407c59 | 2016-09-30 11:01:15 +0200 | [diff] [blame] | 300 | if (!kref_get_unless_zero(&rdata->kref)) |
Joe Eykholt | 0f6c61498 | 2009-08-25 14:02:11 -0700 | [diff] [blame] | 301 | continue; |
Hannes Reinecke | a407c59 | 2016-09-30 11:01:15 +0200 | [diff] [blame] | 302 | if (rdata->disc_id) { |
| 303 | if (rdata->disc_id == disc->disc_id) |
Hannes Reinecke | 05d7d3b | 2016-10-18 10:01:43 +0200 | [diff] [blame] | 304 | fc_rport_login(rdata); |
Hannes Reinecke | a407c59 | 2016-09-30 11:01:15 +0200 | [diff] [blame] | 305 | else |
Hannes Reinecke | c96c792 | 2016-10-18 10:01:44 +0200 | [diff] [blame] | 306 | fc_rport_logoff(rdata); |
Hannes Reinecke | a407c59 | 2016-09-30 11:01:15 +0200 | [diff] [blame] | 307 | } |
Hannes Reinecke | 944ef96 | 2016-10-18 10:01:39 +0200 | [diff] [blame] | 308 | kref_put(&rdata->kref, fc_rport_destroy); |
Joe Eykholt | 0f6c61498 | 2009-08-25 14:02:11 -0700 | [diff] [blame] | 309 | } |
Hannes Reinecke | a407c59 | 2016-09-30 11:01:15 +0200 | [diff] [blame] | 310 | rcu_read_unlock(); |
Abhijeet Joglekar | 0d228c0 | 2009-04-21 16:26:52 -0700 | [diff] [blame] | 311 | mutex_unlock(&disc->disc_mutex); |
| 312 | disc->disc_callback(lport, event); |
| 313 | mutex_lock(&disc->disc_mutex); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 317 | * fc_disc_error() - Handle error on dNS request |
Robert Love | 3a3b42b | 2009-11-03 11:47:39 -0800 | [diff] [blame] | 318 | * @disc: The discovery context |
| 319 | * @fp: The error code encoded as a frame pointer |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 320 | */ |
| 321 | static void fc_disc_error(struct fc_disc *disc, struct fc_frame *fp) |
| 322 | { |
Joe Eykholt | 0685230 | 2010-07-20 15:19:53 -0700 | [diff] [blame] | 323 | struct fc_lport *lport = fc_disc_lport(disc); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 324 | unsigned long delay = 0; |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 325 | |
| 326 | FC_DISC_DBG(disc, "Error %ld, retries %d/%d\n", |
| 327 | PTR_ERR(fp), disc->retry_count, |
| 328 | FC_DISC_RETRY_LIMIT); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 329 | |
| 330 | if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) { |
| 331 | /* |
| 332 | * Memory allocation failure, or the exchange timed out, |
| 333 | * retry after delay. |
| 334 | */ |
| 335 | if (disc->retry_count < FC_DISC_RETRY_LIMIT) { |
| 336 | /* go ahead and retry */ |
| 337 | if (!fp) |
| 338 | delay = msecs_to_jiffies(FC_DISC_RETRY_DELAY); |
| 339 | else { |
| 340 | delay = msecs_to_jiffies(lport->e_d_tov); |
| 341 | |
| 342 | /* timeout faster first time */ |
| 343 | if (!disc->retry_count) |
| 344 | delay /= 4; |
| 345 | } |
| 346 | disc->retry_count++; |
| 347 | schedule_delayed_work(&disc->disc_work, delay); |
Joe Eykholt | 786681b | 2009-08-25 14:01:29 -0700 | [diff] [blame] | 348 | } else |
| 349 | fc_disc_done(disc, DISC_EV_FAILED); |
Bhanu Prakash Gollapudi | 0083208 | 2012-02-10 17:18:57 -0800 | [diff] [blame] | 350 | } else if (PTR_ERR(fp) == -FC_EX_CLOSED) { |
| 351 | /* |
| 352 | * if discovery fails due to lport reset, clear |
| 353 | * pending flag so that subsequent discovery can |
| 354 | * continue |
| 355 | */ |
| 356 | disc->pending = 0; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 361 | * fc_disc_gpn_ft_req() - Send Get Port Names by FC-4 type (GPN_FT) request |
Robert Love | 3a3b42b | 2009-11-03 11:47:39 -0800 | [diff] [blame] | 362 | * @lport: The discovery context |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 363 | * |
| 364 | * Locking Note: This function expects that the disc_mutex is locked |
| 365 | * before it is called. |
| 366 | */ |
| 367 | static void fc_disc_gpn_ft_req(struct fc_disc *disc) |
| 368 | { |
| 369 | struct fc_frame *fp; |
Joe Eykholt | 0685230 | 2010-07-20 15:19:53 -0700 | [diff] [blame] | 370 | struct fc_lport *lport = fc_disc_lport(disc); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 371 | |
| 372 | WARN_ON(!fc_lport_test_ready(lport)); |
| 373 | |
| 374 | disc->pending = 1; |
| 375 | disc->requested = 0; |
| 376 | |
| 377 | disc->buf_len = 0; |
| 378 | disc->seq_count = 0; |
| 379 | fp = fc_frame_alloc(lport, |
| 380 | sizeof(struct fc_ct_hdr) + |
| 381 | sizeof(struct fc_ns_gid_ft)); |
| 382 | if (!fp) |
| 383 | goto err; |
| 384 | |
Joe Eykholt | a46f327 | 2009-08-25 14:00:55 -0700 | [diff] [blame] | 385 | if (lport->tt.elsct_send(lport, 0, fp, |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 386 | FC_NS_GPN_FT, |
| 387 | fc_disc_gpn_ft_resp, |
Joe Eykholt | b94f895 | 2009-11-03 11:50:21 -0800 | [diff] [blame] | 388 | disc, 3 * lport->r_a_tov)) |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 389 | return; |
| 390 | err: |
Chris Leech | 8f550f9 | 2009-10-21 16:28:09 -0700 | [diff] [blame] | 391 | fc_disc_error(disc, NULL); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /** |
Joe Eykholt | 786681b | 2009-08-25 14:01:29 -0700 | [diff] [blame] | 395 | * fc_disc_gpn_ft_parse() - Parse the body of the dNS GPN_FT response. |
Robert Love | 3a3b42b | 2009-11-03 11:47:39 -0800 | [diff] [blame] | 396 | * @lport: The local port the GPN_FT was received on |
| 397 | * @buf: The GPN_FT response buffer |
| 398 | * @len: The size of response buffer |
Joe Eykholt | 786681b | 2009-08-25 14:01:29 -0700 | [diff] [blame] | 399 | * |
| 400 | * Goes through the list of IDs and names resulting from a request. |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 401 | */ |
| 402 | static int fc_disc_gpn_ft_parse(struct fc_disc *disc, void *buf, size_t len) |
| 403 | { |
| 404 | struct fc_lport *lport; |
| 405 | struct fc_gpn_ft_resp *np; |
| 406 | char *bp; |
| 407 | size_t plen; |
| 408 | size_t tlen; |
| 409 | int error = 0; |
Joe Eykholt | 795d86f | 2009-08-25 14:00:39 -0700 | [diff] [blame] | 410 | struct fc_rport_identifiers ids; |
Joe Eykholt | ab28f1f | 2009-08-25 14:00:34 -0700 | [diff] [blame] | 411 | struct fc_rport_priv *rdata; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 412 | |
Joe Eykholt | 0685230 | 2010-07-20 15:19:53 -0700 | [diff] [blame] | 413 | lport = fc_disc_lport(disc); |
Joe Eykholt | a1c1e4e | 2009-08-25 14:02:22 -0700 | [diff] [blame] | 414 | disc->seq_count++; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 415 | |
| 416 | /* |
| 417 | * Handle partial name record left over from previous call. |
| 418 | */ |
| 419 | bp = buf; |
| 420 | plen = len; |
| 421 | np = (struct fc_gpn_ft_resp *)bp; |
| 422 | tlen = disc->buf_len; |
Joe Eykholt | 81a67b9 | 2009-08-25 14:02:43 -0700 | [diff] [blame] | 423 | disc->buf_len = 0; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 424 | if (tlen) { |
| 425 | WARN_ON(tlen >= sizeof(*np)); |
| 426 | plen = sizeof(*np) - tlen; |
| 427 | WARN_ON(plen <= 0); |
| 428 | WARN_ON(plen >= sizeof(*np)); |
| 429 | if (plen > len) |
| 430 | plen = len; |
| 431 | np = &disc->partial_buf; |
| 432 | memcpy((char *)np + tlen, bp, plen); |
| 433 | |
| 434 | /* |
| 435 | * Set bp so that the loop below will advance it to the |
| 436 | * first valid full name element. |
| 437 | */ |
| 438 | bp -= tlen; |
| 439 | len += tlen; |
| 440 | plen += tlen; |
| 441 | disc->buf_len = (unsigned char) plen; |
| 442 | if (plen == sizeof(*np)) |
| 443 | disc->buf_len = 0; |
| 444 | } |
| 445 | |
| 446 | /* |
| 447 | * Handle full name records, including the one filled from above. |
| 448 | * Normally, np == bp and plen == len, but from the partial case above, |
| 449 | * bp, len describe the overall buffer, and np, plen describe the |
| 450 | * partial buffer, which if would usually be full now. |
| 451 | * After the first time through the loop, things return to "normal". |
| 452 | */ |
| 453 | while (plen >= sizeof(*np)) { |
Joe Eykholt | 795d86f | 2009-08-25 14:00:39 -0700 | [diff] [blame] | 454 | ids.port_id = ntoh24(np->fp_fid); |
| 455 | ids.port_name = ntohll(np->fp_wwpn); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 456 | |
Robert Love | 7b2787e | 2010-05-07 15:18:41 -0700 | [diff] [blame] | 457 | if (ids.port_id != lport->port_id && |
Joe Eykholt | 795d86f | 2009-08-25 14:00:39 -0700 | [diff] [blame] | 458 | ids.port_name != lport->wwpn) { |
Hannes Reinecke | 2580064 | 2016-10-18 10:01:42 +0200 | [diff] [blame] | 459 | rdata = fc_rport_create(lport, ids.port_id); |
Robert Love | 9737e6a | 2009-08-25 14:02:59 -0700 | [diff] [blame] | 460 | if (rdata) { |
| 461 | rdata->ids.port_name = ids.port_name; |
Joe Eykholt | 0f6c61498 | 2009-08-25 14:02:11 -0700 | [diff] [blame] | 462 | rdata->disc_id = disc->disc_id; |
Robert Love | 9737e6a | 2009-08-25 14:02:59 -0700 | [diff] [blame] | 463 | } else { |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 464 | printk(KERN_WARNING "libfc: Failed to allocate " |
| 465 | "memory for the newly discovered port " |
Chris Leech | ce8b5df | 2010-04-09 14:23:10 -0700 | [diff] [blame] | 466 | "(%6.6x)\n", ids.port_id); |
Joe Eykholt | 81a67b9 | 2009-08-25 14:02:43 -0700 | [diff] [blame] | 467 | error = -ENOMEM; |
| 468 | } |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | if (np->fp_flags & FC_NS_FID_LAST) { |
Joe Eykholt | 786681b | 2009-08-25 14:01:29 -0700 | [diff] [blame] | 472 | fc_disc_done(disc, DISC_EV_SUCCESS); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 473 | len = 0; |
| 474 | break; |
| 475 | } |
| 476 | len -= sizeof(*np); |
| 477 | bp += sizeof(*np); |
| 478 | np = (struct fc_gpn_ft_resp *)bp; |
| 479 | plen = len; |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * Save any partial record at the end of the buffer for next time. |
| 484 | */ |
| 485 | if (error == 0 && len > 0 && len < sizeof(*np)) { |
| 486 | if (np != &disc->partial_buf) { |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 487 | FC_DISC_DBG(disc, "Partial buffer remains " |
| 488 | "for discovery\n"); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 489 | memcpy(&disc->partial_buf, np, len); |
| 490 | } |
| 491 | disc->buf_len = (unsigned char) len; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 492 | } |
| 493 | return error; |
| 494 | } |
| 495 | |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 496 | /** |
Robert Love | 3a3b42b | 2009-11-03 11:47:39 -0800 | [diff] [blame] | 497 | * fc_disc_timeout() - Handler for discovery timeouts |
| 498 | * @work: Structure holding discovery context that needs to retry discovery |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 499 | */ |
| 500 | static void fc_disc_timeout(struct work_struct *work) |
| 501 | { |
| 502 | struct fc_disc *disc = container_of(work, |
| 503 | struct fc_disc, |
| 504 | disc_work.work); |
| 505 | mutex_lock(&disc->disc_mutex); |
Joe Eykholt | 3667d7e | 2009-08-25 14:02:38 -0700 | [diff] [blame] | 506 | fc_disc_gpn_ft_req(disc); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 507 | mutex_unlock(&disc->disc_mutex); |
| 508 | } |
| 509 | |
| 510 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 511 | * fc_disc_gpn_ft_resp() - Handle a response frame from Get Port Names (GPN_FT) |
Robert Love | 3a3b42b | 2009-11-03 11:47:39 -0800 | [diff] [blame] | 512 | * @sp: The sequence that the GPN_FT response was received on |
| 513 | * @fp: The GPN_FT response frame |
| 514 | * @lp_arg: The discovery context |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 515 | * |
Abhijeet Joglekar | 0d228c0 | 2009-04-21 16:26:52 -0700 | [diff] [blame] | 516 | * Locking Note: This function is called without disc mutex held, and |
| 517 | * should do all its processing with the mutex held |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 518 | */ |
| 519 | static void fc_disc_gpn_ft_resp(struct fc_seq *sp, struct fc_frame *fp, |
| 520 | void *disc_arg) |
| 521 | { |
| 522 | struct fc_disc *disc = disc_arg; |
| 523 | struct fc_ct_hdr *cp; |
| 524 | struct fc_frame_header *fh; |
Joe Eykholt | a1c1e4e | 2009-08-25 14:02:22 -0700 | [diff] [blame] | 525 | enum fc_disc_event event = DISC_EV_NONE; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 526 | unsigned int seq_cnt; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 527 | unsigned int len; |
Joe Eykholt | a1c1e4e | 2009-08-25 14:02:22 -0700 | [diff] [blame] | 528 | int error = 0; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 529 | |
Abhijeet Joglekar | 0d228c0 | 2009-04-21 16:26:52 -0700 | [diff] [blame] | 530 | mutex_lock(&disc->disc_mutex); |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 531 | FC_DISC_DBG(disc, "Received a GPN_FT response\n"); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 532 | |
| 533 | if (IS_ERR(fp)) { |
| 534 | fc_disc_error(disc, fp); |
Abhijeet Joglekar | 0d228c0 | 2009-04-21 16:26:52 -0700 | [diff] [blame] | 535 | mutex_unlock(&disc->disc_mutex); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 536 | return; |
| 537 | } |
| 538 | |
| 539 | WARN_ON(!fc_frame_is_linear(fp)); /* buffer must be contiguous */ |
| 540 | fh = fc_frame_header_get(fp); |
| 541 | len = fr_len(fp) - sizeof(*fh); |
| 542 | seq_cnt = ntohs(fh->fh_seq_cnt); |
Joe Eykholt | a1c1e4e | 2009-08-25 14:02:22 -0700 | [diff] [blame] | 543 | if (fr_sof(fp) == FC_SOF_I3 && seq_cnt == 0 && disc->seq_count == 0) { |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 544 | cp = fc_frame_payload_get(fp, sizeof(*cp)); |
| 545 | if (!cp) { |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 546 | FC_DISC_DBG(disc, "GPN_FT response too short, len %d\n", |
| 547 | fr_len(fp)); |
Joe Eykholt | 883a337 | 2009-08-25 14:02:27 -0700 | [diff] [blame] | 548 | event = DISC_EV_FAILED; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 549 | } else if (ntohs(cp->ct_cmd) == FC_FS_ACC) { |
| 550 | |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 551 | /* Accepted, parse the response. */ |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 552 | len -= sizeof(*cp); |
Joe Eykholt | a1c1e4e | 2009-08-25 14:02:22 -0700 | [diff] [blame] | 553 | error = fc_disc_gpn_ft_parse(disc, cp + 1, len); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 554 | } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) { |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 555 | FC_DISC_DBG(disc, "GPN_FT rejected reason %x exp %x " |
| 556 | "(check zoning)\n", cp->ct_reason, |
| 557 | cp->ct_explan); |
Joe Eykholt | a1c1e4e | 2009-08-25 14:02:22 -0700 | [diff] [blame] | 558 | event = DISC_EV_FAILED; |
Joe Eykholt | c762608 | 2009-08-25 14:02:33 -0700 | [diff] [blame] | 559 | if (cp->ct_reason == FC_FS_RJT_UNABL && |
| 560 | cp->ct_explan == FC_FS_EXP_FTNR) |
| 561 | event = DISC_EV_SUCCESS; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 562 | } else { |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 563 | FC_DISC_DBG(disc, "GPN_FT unexpected response code " |
| 564 | "%x\n", ntohs(cp->ct_cmd)); |
Joe Eykholt | 883a337 | 2009-08-25 14:02:27 -0700 | [diff] [blame] | 565 | event = DISC_EV_FAILED; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 566 | } |
Joe Eykholt | a1c1e4e | 2009-08-25 14:02:22 -0700 | [diff] [blame] | 567 | } else if (fr_sof(fp) == FC_SOF_N3 && seq_cnt == disc->seq_count) { |
| 568 | error = fc_disc_gpn_ft_parse(disc, fh + 1, len); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 569 | } else { |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 570 | FC_DISC_DBG(disc, "GPN_FT unexpected frame - out of sequence? " |
| 571 | "seq_cnt %x expected %x sof %x eof %x\n", |
| 572 | seq_cnt, disc->seq_count, fr_sof(fp), fr_eof(fp)); |
Joe Eykholt | 883a337 | 2009-08-25 14:02:27 -0700 | [diff] [blame] | 573 | event = DISC_EV_FAILED; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 574 | } |
Joe Eykholt | a1c1e4e | 2009-08-25 14:02:22 -0700 | [diff] [blame] | 575 | if (error) |
Dan Carpenter | 6f37e21 | 2017-07-12 10:30:22 +0300 | [diff] [blame] | 576 | fc_disc_error(disc, ERR_PTR(error)); |
Joe Eykholt | a1c1e4e | 2009-08-25 14:02:22 -0700 | [diff] [blame] | 577 | else if (event != DISC_EV_NONE) |
| 578 | fc_disc_done(disc, event); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 579 | fc_frame_free(fp); |
Abhijeet Joglekar | 0d228c0 | 2009-04-21 16:26:52 -0700 | [diff] [blame] | 580 | mutex_unlock(&disc->disc_mutex); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /** |
Joe Eykholt | 2ab7e1e | 2009-08-25 14:03:58 -0700 | [diff] [blame] | 584 | * fc_disc_gpn_id_resp() - Handle a response frame from Get Port Names (GPN_ID) |
Robert Love | 3a3b42b | 2009-11-03 11:47:39 -0800 | [diff] [blame] | 585 | * @sp: The sequence the GPN_ID is on |
| 586 | * @fp: The response frame |
| 587 | * @rdata_arg: The remote port that sent the GPN_ID response |
Joe Eykholt | 2ab7e1e | 2009-08-25 14:03:58 -0700 | [diff] [blame] | 588 | * |
| 589 | * Locking Note: This function is called without disc mutex held. |
| 590 | */ |
| 591 | static void fc_disc_gpn_id_resp(struct fc_seq *sp, struct fc_frame *fp, |
| 592 | void *rdata_arg) |
| 593 | { |
| 594 | struct fc_rport_priv *rdata = rdata_arg; |
| 595 | struct fc_rport_priv *new_rdata; |
| 596 | struct fc_lport *lport; |
| 597 | struct fc_disc *disc; |
| 598 | struct fc_ct_hdr *cp; |
| 599 | struct fc_ns_gid_pn *pn; |
| 600 | u64 port_name; |
| 601 | |
| 602 | lport = rdata->local_port; |
| 603 | disc = &lport->disc; |
| 604 | |
Joe Eykholt | 2ab7e1e | 2009-08-25 14:03:58 -0700 | [diff] [blame] | 605 | if (PTR_ERR(fp) == -FC_EX_CLOSED) |
| 606 | goto out; |
| 607 | if (IS_ERR(fp)) |
| 608 | goto redisc; |
| 609 | |
| 610 | cp = fc_frame_payload_get(fp, sizeof(*cp)); |
| 611 | if (!cp) |
| 612 | goto redisc; |
| 613 | if (ntohs(cp->ct_cmd) == FC_FS_ACC) { |
| 614 | if (fr_len(fp) < sizeof(struct fc_frame_header) + |
| 615 | sizeof(*cp) + sizeof(*pn)) |
| 616 | goto redisc; |
| 617 | pn = (struct fc_ns_gid_pn *)(cp + 1); |
| 618 | port_name = get_unaligned_be64(&pn->fn_wwpn); |
Hannes Reinecke | a407c59 | 2016-09-30 11:01:15 +0200 | [diff] [blame] | 619 | mutex_lock(&rdata->rp_mutex); |
Joe Eykholt | 2ab7e1e | 2009-08-25 14:03:58 -0700 | [diff] [blame] | 620 | if (rdata->ids.port_name == -1) |
| 621 | rdata->ids.port_name = port_name; |
| 622 | else if (rdata->ids.port_name != port_name) { |
| 623 | FC_DISC_DBG(disc, "GPN_ID accepted. WWPN changed. " |
Chris Leech | 9f8f3aa | 2010-04-09 14:23:16 -0700 | [diff] [blame] | 624 | "Port-id %6.6x wwpn %16.16llx\n", |
Joe Eykholt | 2ab7e1e | 2009-08-25 14:03:58 -0700 | [diff] [blame] | 625 | rdata->ids.port_id, port_name); |
Hannes Reinecke | a407c59 | 2016-09-30 11:01:15 +0200 | [diff] [blame] | 626 | mutex_unlock(&rdata->rp_mutex); |
Hannes Reinecke | c96c792 | 2016-10-18 10:01:44 +0200 | [diff] [blame] | 627 | fc_rport_logoff(rdata); |
Hannes Reinecke | a407c59 | 2016-09-30 11:01:15 +0200 | [diff] [blame] | 628 | mutex_lock(&lport->disc.disc_mutex); |
Hannes Reinecke | 2580064 | 2016-10-18 10:01:42 +0200 | [diff] [blame] | 629 | new_rdata = fc_rport_create(lport, rdata->ids.port_id); |
Hannes Reinecke | a407c59 | 2016-09-30 11:01:15 +0200 | [diff] [blame] | 630 | mutex_unlock(&lport->disc.disc_mutex); |
Joe Eykholt | 2ab7e1e | 2009-08-25 14:03:58 -0700 | [diff] [blame] | 631 | if (new_rdata) { |
| 632 | new_rdata->disc_id = disc->disc_id; |
Hannes Reinecke | 05d7d3b | 2016-10-18 10:01:43 +0200 | [diff] [blame] | 633 | fc_rport_login(new_rdata); |
Joe Eykholt | 2ab7e1e | 2009-08-25 14:03:58 -0700 | [diff] [blame] | 634 | } |
| 635 | goto out; |
| 636 | } |
| 637 | rdata->disc_id = disc->disc_id; |
Hannes Reinecke | a407c59 | 2016-09-30 11:01:15 +0200 | [diff] [blame] | 638 | mutex_unlock(&rdata->rp_mutex); |
Hannes Reinecke | 05d7d3b | 2016-10-18 10:01:43 +0200 | [diff] [blame] | 639 | fc_rport_login(rdata); |
Joe Eykholt | 2ab7e1e | 2009-08-25 14:03:58 -0700 | [diff] [blame] | 640 | } else if (ntohs(cp->ct_cmd) == FC_FS_RJT) { |
| 641 | FC_DISC_DBG(disc, "GPN_ID rejected reason %x exp %x\n", |
| 642 | cp->ct_reason, cp->ct_explan); |
Hannes Reinecke | c96c792 | 2016-10-18 10:01:44 +0200 | [diff] [blame] | 643 | fc_rport_logoff(rdata); |
Joe Eykholt | 2ab7e1e | 2009-08-25 14:03:58 -0700 | [diff] [blame] | 644 | } else { |
| 645 | FC_DISC_DBG(disc, "GPN_ID unexpected response code %x\n", |
| 646 | ntohs(cp->ct_cmd)); |
| 647 | redisc: |
Hannes Reinecke | a407c59 | 2016-09-30 11:01:15 +0200 | [diff] [blame] | 648 | mutex_lock(&disc->disc_mutex); |
Joe Eykholt | 2ab7e1e | 2009-08-25 14:03:58 -0700 | [diff] [blame] | 649 | fc_disc_restart(disc); |
Hannes Reinecke | a407c59 | 2016-09-30 11:01:15 +0200 | [diff] [blame] | 650 | mutex_unlock(&disc->disc_mutex); |
Joe Eykholt | 2ab7e1e | 2009-08-25 14:03:58 -0700 | [diff] [blame] | 651 | } |
| 652 | out: |
Hannes Reinecke | 944ef96 | 2016-10-18 10:01:39 +0200 | [diff] [blame] | 653 | kref_put(&rdata->kref, fc_rport_destroy); |
Joe Eykholt | 2ab7e1e | 2009-08-25 14:03:58 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | /** |
| 657 | * fc_disc_gpn_id_req() - Send Get Port Names by ID (GPN_ID) request |
Robert Love | 3a3b42b | 2009-11-03 11:47:39 -0800 | [diff] [blame] | 658 | * @lport: The local port to initiate discovery on |
Joe Eykholt | 2ab7e1e | 2009-08-25 14:03:58 -0700 | [diff] [blame] | 659 | * @rdata: remote port private data |
| 660 | * |
| 661 | * Locking Note: This function expects that the disc_mutex is locked |
| 662 | * before it is called. |
| 663 | * On failure, an error code is returned. |
| 664 | */ |
| 665 | static int fc_disc_gpn_id_req(struct fc_lport *lport, |
| 666 | struct fc_rport_priv *rdata) |
| 667 | { |
| 668 | struct fc_frame *fp; |
| 669 | |
| 670 | fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) + |
| 671 | sizeof(struct fc_ns_fid)); |
| 672 | if (!fp) |
| 673 | return -ENOMEM; |
| 674 | if (!lport->tt.elsct_send(lport, rdata->ids.port_id, fp, FC_NS_GPN_ID, |
Joe Eykholt | b94f895 | 2009-11-03 11:50:21 -0800 | [diff] [blame] | 675 | fc_disc_gpn_id_resp, rdata, |
| 676 | 3 * lport->r_a_tov)) |
Joe Eykholt | 2ab7e1e | 2009-08-25 14:03:58 -0700 | [diff] [blame] | 677 | return -ENOMEM; |
| 678 | kref_get(&rdata->kref); |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 683 | * fc_disc_single() - Discover the directory information for a single target |
Robert Love | 3a3b42b | 2009-11-03 11:47:39 -0800 | [diff] [blame] | 684 | * @lport: The local port the remote port is associated with |
| 685 | * @dp: The port to rediscover |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 686 | * |
| 687 | * Locking Note: This function expects that the disc_mutex is locked |
| 688 | * before it is called. |
| 689 | */ |
Joe Eykholt | 2ab7e1e | 2009-08-25 14:03:58 -0700 | [diff] [blame] | 690 | static int fc_disc_single(struct fc_lport *lport, struct fc_disc_port *dp) |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 691 | { |
Joe Eykholt | ab28f1f | 2009-08-25 14:00:34 -0700 | [diff] [blame] | 692 | struct fc_rport_priv *rdata; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 693 | |
Hannes Reinecke | 2580064 | 2016-10-18 10:01:42 +0200 | [diff] [blame] | 694 | rdata = fc_rport_create(lport, dp->port_id); |
Joe Eykholt | 2ab7e1e | 2009-08-25 14:03:58 -0700 | [diff] [blame] | 695 | if (!rdata) |
| 696 | return -ENOMEM; |
| 697 | rdata->disc_id = 0; |
| 698 | return fc_disc_gpn_id_req(lport, rdata); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 702 | * fc_disc_stop() - Stop discovery for a given lport |
Robert Love | 3a3b42b | 2009-11-03 11:47:39 -0800 | [diff] [blame] | 703 | * @lport: The local port that discovery should stop on |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 704 | */ |
Bart Van Assche | c6b21c9 | 2012-01-13 17:26:20 -0800 | [diff] [blame] | 705 | static void fc_disc_stop(struct fc_lport *lport) |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 706 | { |
| 707 | struct fc_disc *disc = &lport->disc; |
| 708 | |
Bhanu Prakash Gollapudi | c531b9b | 2010-10-08 17:12:36 -0700 | [diff] [blame] | 709 | if (disc->pending) |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 710 | cancel_delayed_work_sync(&disc->disc_work); |
Bhanu Prakash Gollapudi | c531b9b | 2010-10-08 17:12:36 -0700 | [diff] [blame] | 711 | fc_disc_stop_rports(disc); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 715 | * fc_disc_stop_final() - Stop discovery for a given lport |
Robert Love | 3a3b42b | 2009-11-03 11:47:39 -0800 | [diff] [blame] | 716 | * @lport: The lport that discovery should stop on |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 717 | * |
| 718 | * This function will block until discovery has been |
| 719 | * completely stopped and all rports have been deleted. |
| 720 | */ |
Bart Van Assche | c6b21c9 | 2012-01-13 17:26:20 -0800 | [diff] [blame] | 721 | static void fc_disc_stop_final(struct fc_lport *lport) |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 722 | { |
| 723 | fc_disc_stop(lport); |
Hannes Reinecke | 5922a95 | 2016-10-18 10:01:46 +0200 | [diff] [blame] | 724 | fc_rport_flush_queue(); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /** |
Robert Love | 0807619 | 2013-03-25 11:00:28 -0700 | [diff] [blame] | 728 | * fc_disc_config() - Configure the discovery layer for a local port |
| 729 | * @lport: The local port that needs the discovery layer to be configured |
Robert Love | 8a9a713 | 2013-03-25 11:00:27 -0700 | [diff] [blame] | 730 | * @priv: Private data structre for users of the discovery layer |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 731 | */ |
Robert Love | 0807619 | 2013-03-25 11:00:28 -0700 | [diff] [blame] | 732 | void fc_disc_config(struct fc_lport *lport, void *priv) |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 733 | { |
Colin Ian King | bc2e129 | 2018-02-06 14:21:57 +0000 | [diff] [blame^] | 734 | struct fc_disc *disc; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 735 | |
| 736 | if (!lport->tt.disc_start) |
| 737 | lport->tt.disc_start = fc_disc_start; |
| 738 | |
| 739 | if (!lport->tt.disc_stop) |
| 740 | lport->tt.disc_stop = fc_disc_stop; |
| 741 | |
| 742 | if (!lport->tt.disc_stop_final) |
| 743 | lport->tt.disc_stop_final = fc_disc_stop_final; |
| 744 | |
| 745 | if (!lport->tt.disc_recv_req) |
| 746 | lport->tt.disc_recv_req = fc_disc_recv_req; |
| 747 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 748 | disc = &lport->disc; |
Robert Love | 0807619 | 2013-03-25 11:00:28 -0700 | [diff] [blame] | 749 | |
| 750 | disc->priv = priv; |
| 751 | } |
| 752 | EXPORT_SYMBOL(fc_disc_config); |
| 753 | |
| 754 | /** |
| 755 | * fc_disc_init() - Initialize the discovery layer for a local port |
| 756 | * @lport: The local port that needs the discovery layer to be initialized |
| 757 | */ |
| 758 | void fc_disc_init(struct fc_lport *lport) |
| 759 | { |
| 760 | struct fc_disc *disc = &lport->disc; |
| 761 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 762 | INIT_DELAYED_WORK(&disc->disc_work, fc_disc_timeout); |
| 763 | mutex_init(&disc->disc_mutex); |
| 764 | INIT_LIST_HEAD(&disc->rports); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 765 | } |
| 766 | EXPORT_SYMBOL(fc_disc_init); |