Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright(c) 2007 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 | * PORT LOCKING NOTES |
| 22 | * |
| 23 | * These comments only apply to the 'port code' which consists of the lport, |
| 24 | * disc and rport blocks. |
| 25 | * |
| 26 | * MOTIVATION |
| 27 | * |
| 28 | * The lport, disc and rport blocks all have mutexes that are used to protect |
| 29 | * those objects. The main motivation for these locks is to prevent from |
| 30 | * having an lport reset just before we send a frame. In that scenario the |
| 31 | * lport's FID would get set to zero and then we'd send a frame with an |
| 32 | * invalid SID. We also need to ensure that states don't change unexpectedly |
| 33 | * while processing another state. |
| 34 | * |
| 35 | * HEIRARCHY |
| 36 | * |
| 37 | * The following heirarchy defines the locking rules. A greater lock |
| 38 | * may be held before acquiring a lesser lock, but a lesser lock should never |
| 39 | * be held while attempting to acquire a greater lock. Here is the heirarchy- |
| 40 | * |
| 41 | * lport > disc, lport > rport, disc > rport |
| 42 | * |
| 43 | * CALLBACKS |
| 44 | * |
| 45 | * The callbacks cause complications with this scheme. There is a callback |
| 46 | * from the rport (to either lport or disc) and a callback from disc |
| 47 | * (to the lport). |
| 48 | * |
| 49 | * As rports exit the rport state machine a callback is made to the owner of |
| 50 | * the rport to notify success or failure. Since the callback is likely to |
| 51 | * cause the lport or disc to grab its lock we cannot hold the rport lock |
| 52 | * while making the callback. To ensure that the rport is not free'd while |
| 53 | * processing the callback the rport callbacks are serialized through a |
| 54 | * single-threaded workqueue. An rport would never be free'd while in a |
| 55 | * callback handler becuase no other rport work in this queue can be executed |
| 56 | * at the same time. |
| 57 | * |
| 58 | * When discovery succeeds or fails a callback is made to the lport as |
| 59 | * notification. Currently, succesful discovery causes the lport to take no |
| 60 | * action. A failure will cause the lport to reset. There is likely a circular |
| 61 | * locking problem with this implementation. |
| 62 | */ |
| 63 | |
| 64 | /* |
| 65 | * LPORT LOCKING |
| 66 | * |
| 67 | * The critical sections protected by the lport's mutex are quite broad and |
| 68 | * may be improved upon in the future. The lport code and its locking doesn't |
| 69 | * influence the I/O path, so excessive locking doesn't penalize I/O |
| 70 | * performance. |
| 71 | * |
| 72 | * The strategy is to lock whenever processing a request or response. Note |
| 73 | * that every _enter_* function corresponds to a state change. They generally |
| 74 | * change the lports state and then send a request out on the wire. We lock |
| 75 | * before calling any of these functions to protect that state change. This |
| 76 | * means that the entry points into the lport block manage the locks while |
| 77 | * the state machine can transition between states (i.e. _enter_* functions) |
| 78 | * while always staying protected. |
| 79 | * |
| 80 | * When handling responses we also hold the lport mutex broadly. When the |
| 81 | * lport receives the response frame it locks the mutex and then calls the |
| 82 | * appropriate handler for the particuar response. Generally a response will |
| 83 | * trigger a state change and so the lock must already be held. |
| 84 | * |
| 85 | * Retries also have to consider the locking. The retries occur from a work |
| 86 | * context and the work function will lock the lport and then retry the state |
| 87 | * (i.e. _enter_* function). |
| 88 | */ |
| 89 | |
| 90 | #include <linux/timer.h> |
| 91 | #include <asm/unaligned.h> |
| 92 | |
| 93 | #include <scsi/fc/fc_gs.h> |
| 94 | |
| 95 | #include <scsi/libfc.h> |
| 96 | #include <scsi/fc_encode.h> |
Steve Ma | a51ab39 | 2009-11-03 11:47:34 -0800 | [diff] [blame^] | 97 | #include <linux/scatterlist.h> |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 98 | |
Robert Love | 8866a5d | 2009-11-03 11:45:58 -0800 | [diff] [blame] | 99 | #include "fc_libfc.h" |
| 100 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 101 | /* Fabric IDs to use for point-to-point mode, chosen on whims. */ |
| 102 | #define FC_LOCAL_PTP_FID_LO 0x010101 |
| 103 | #define FC_LOCAL_PTP_FID_HI 0x010102 |
| 104 | |
| 105 | #define DNS_DELAY 3 /* Discovery delay after RSCN (in seconds)*/ |
| 106 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 107 | static void fc_lport_error(struct fc_lport *, struct fc_frame *); |
| 108 | |
| 109 | static void fc_lport_enter_reset(struct fc_lport *); |
| 110 | static void fc_lport_enter_flogi(struct fc_lport *); |
| 111 | static void fc_lport_enter_dns(struct fc_lport *); |
Chris Leech | c914f7d | 2009-11-03 11:47:12 -0800 | [diff] [blame] | 112 | static void fc_lport_enter_ns(struct fc_lport *, enum fc_lport_state); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 113 | static void fc_lport_enter_scr(struct fc_lport *); |
| 114 | static void fc_lport_enter_ready(struct fc_lport *); |
| 115 | static void fc_lport_enter_logo(struct fc_lport *); |
| 116 | |
| 117 | static const char *fc_lport_state_names[] = { |
Joe Eykholt | b1d9fd5 | 2009-07-29 17:04:22 -0700 | [diff] [blame] | 118 | [LPORT_ST_DISABLED] = "disabled", |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 119 | [LPORT_ST_FLOGI] = "FLOGI", |
| 120 | [LPORT_ST_DNS] = "dNS", |
Chris Leech | c9c7bd7 | 2009-11-03 11:46:51 -0800 | [diff] [blame] | 121 | [LPORT_ST_RNN_ID] = "RNN_ID", |
Chris Leech | 5baa17c | 2009-11-03 11:46:56 -0800 | [diff] [blame] | 122 | [LPORT_ST_RSNN_NN] = "RSNN_NN", |
Chris Leech | c9866a5 | 2009-11-03 11:47:01 -0800 | [diff] [blame] | 123 | [LPORT_ST_RSPN_ID] = "RSPN_ID", |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 124 | [LPORT_ST_RFT_ID] = "RFT_ID", |
| 125 | [LPORT_ST_SCR] = "SCR", |
| 126 | [LPORT_ST_READY] = "Ready", |
| 127 | [LPORT_ST_LOGO] = "LOGO", |
| 128 | [LPORT_ST_RESET] = "reset", |
| 129 | }; |
| 130 | |
Steve Ma | a51ab39 | 2009-11-03 11:47:34 -0800 | [diff] [blame^] | 131 | /** |
| 132 | * struct fc_bsg_info - FC Passthrough managemet structure |
| 133 | * @job: The passthrough job |
| 134 | * @lport: The local port to pass through a command |
| 135 | * @rsp_code: The expected response code |
| 136 | * @sg: job->reply_payload.sg_list |
| 137 | * @nents: job->reply_payload.sg_cnt |
| 138 | * @offset: The offset into the response data |
| 139 | */ |
| 140 | struct fc_bsg_info { |
| 141 | struct fc_bsg_job *job; |
| 142 | struct fc_lport *lport; |
| 143 | u16 rsp_code; |
| 144 | struct scatterlist *sg; |
| 145 | u32 nents; |
| 146 | size_t offset; |
| 147 | }; |
| 148 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 149 | static int fc_frame_drop(struct fc_lport *lport, struct fc_frame *fp) |
| 150 | { |
| 151 | fc_frame_free(fp); |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 156 | * fc_lport_rport_callback() - Event handler for rport events |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 157 | * @lport: The lport which is receiving the event |
Joe Eykholt | 9fb9d32 | 2009-08-25 14:00:50 -0700 | [diff] [blame] | 158 | * @rdata: private remote port data |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 159 | * @event: The event that occured |
| 160 | * |
| 161 | * Locking Note: The rport lock should not be held when calling |
| 162 | * this function. |
| 163 | */ |
| 164 | static void fc_lport_rport_callback(struct fc_lport *lport, |
Joe Eykholt | 9fb9d32 | 2009-08-25 14:00:50 -0700 | [diff] [blame] | 165 | struct fc_rport_priv *rdata, |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 166 | enum fc_rport_event event) |
| 167 | { |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 168 | FC_LPORT_DBG(lport, "Received a %d event for port (%6x)\n", event, |
Joe Eykholt | f211fa5 | 2009-08-25 14:01:01 -0700 | [diff] [blame] | 169 | rdata->ids.port_id); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 170 | |
Joe Eykholt | b5cbf08 | 2009-08-25 14:01:44 -0700 | [diff] [blame] | 171 | mutex_lock(&lport->lp_mutex); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 172 | switch (event) { |
Joe Eykholt | 4c0f62b | 2009-08-25 14:01:12 -0700 | [diff] [blame] | 173 | case RPORT_EV_READY: |
Joe Eykholt | b5cbf08 | 2009-08-25 14:01:44 -0700 | [diff] [blame] | 174 | if (lport->state == LPORT_ST_DNS) { |
| 175 | lport->dns_rp = rdata; |
Chris Leech | c914f7d | 2009-11-03 11:47:12 -0800 | [diff] [blame] | 176 | fc_lport_enter_ns(lport, LPORT_ST_RNN_ID); |
Joe Eykholt | b5cbf08 | 2009-08-25 14:01:44 -0700 | [diff] [blame] | 177 | } else { |
| 178 | FC_LPORT_DBG(lport, "Received an READY event " |
| 179 | "on port (%6x) for the directory " |
| 180 | "server, but the lport is not " |
| 181 | "in the DNS state, it's in the " |
| 182 | "%d state", rdata->ids.port_id, |
| 183 | lport->state); |
| 184 | lport->tt.rport_logoff(rdata); |
| 185 | } |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 186 | break; |
| 187 | case RPORT_EV_LOGO: |
| 188 | case RPORT_EV_FAILED: |
| 189 | case RPORT_EV_STOP: |
Joe Eykholt | b5cbf08 | 2009-08-25 14:01:44 -0700 | [diff] [blame] | 190 | lport->dns_rp = NULL; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 191 | break; |
| 192 | case RPORT_EV_NONE: |
| 193 | break; |
| 194 | } |
Joe Eykholt | b5cbf08 | 2009-08-25 14:01:44 -0700 | [diff] [blame] | 195 | mutex_unlock(&lport->lp_mutex); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 199 | * fc_lport_state() - Return a string which represents the lport's state |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 200 | * @lport: The lport whose state is to converted to a string |
| 201 | */ |
| 202 | static const char *fc_lport_state(struct fc_lport *lport) |
| 203 | { |
| 204 | const char *cp; |
| 205 | |
| 206 | cp = fc_lport_state_names[lport->state]; |
| 207 | if (!cp) |
| 208 | cp = "unknown"; |
| 209 | return cp; |
| 210 | } |
| 211 | |
| 212 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 213 | * fc_lport_ptp_setup() - Create an rport for point-to-point mode |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 214 | * @lport: The lport to attach the ptp rport to |
| 215 | * @fid: The FID of the ptp rport |
| 216 | * @remote_wwpn: The WWPN of the ptp rport |
| 217 | * @remote_wwnn: The WWNN of the ptp rport |
| 218 | */ |
| 219 | static void fc_lport_ptp_setup(struct fc_lport *lport, |
| 220 | u32 remote_fid, u64 remote_wwpn, |
| 221 | u64 remote_wwnn) |
| 222 | { |
Joe Eykholt | 48f0090 | 2009-08-25 14:01:50 -0700 | [diff] [blame] | 223 | mutex_lock(&lport->disc.disc_mutex); |
| 224 | if (lport->ptp_rp) |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 225 | lport->tt.rport_logoff(lport->ptp_rp); |
Robert Love | 9737e6a | 2009-08-25 14:02:59 -0700 | [diff] [blame] | 226 | lport->ptp_rp = lport->tt.rport_create(lport, remote_fid); |
| 227 | lport->ptp_rp->ids.port_name = remote_wwpn; |
| 228 | lport->ptp_rp->ids.node_name = remote_wwnn; |
Joe Eykholt | 48f0090 | 2009-08-25 14:01:50 -0700 | [diff] [blame] | 229 | mutex_unlock(&lport->disc.disc_mutex); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 230 | |
| 231 | lport->tt.rport_login(lport->ptp_rp); |
| 232 | |
| 233 | fc_lport_enter_ready(lport); |
| 234 | } |
| 235 | |
| 236 | void fc_get_host_port_type(struct Scsi_Host *shost) |
| 237 | { |
| 238 | /* TODO - currently just NPORT */ |
| 239 | fc_host_port_type(shost) = FC_PORTTYPE_NPORT; |
| 240 | } |
| 241 | EXPORT_SYMBOL(fc_get_host_port_type); |
| 242 | |
| 243 | void fc_get_host_port_state(struct Scsi_Host *shost) |
| 244 | { |
| 245 | struct fc_lport *lp = shost_priv(shost); |
| 246 | |
Chris Leech | 8faecdd | 2009-11-03 11:46:19 -0800 | [diff] [blame] | 247 | mutex_lock(&lp->lp_mutex); |
| 248 | if (!lp->link_up) |
| 249 | fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 250 | else |
Chris Leech | 8faecdd | 2009-11-03 11:46:19 -0800 | [diff] [blame] | 251 | switch (lp->state) { |
| 252 | case LPORT_ST_READY: |
| 253 | fc_host_port_state(shost) = FC_PORTSTATE_ONLINE; |
| 254 | break; |
| 255 | default: |
| 256 | fc_host_port_state(shost) = FC_PORTSTATE_OFFLINE; |
| 257 | } |
| 258 | mutex_unlock(&lp->lp_mutex); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 259 | } |
| 260 | EXPORT_SYMBOL(fc_get_host_port_state); |
| 261 | |
| 262 | void fc_get_host_speed(struct Scsi_Host *shost) |
| 263 | { |
| 264 | struct fc_lport *lport = shost_priv(shost); |
| 265 | |
| 266 | fc_host_speed(shost) = lport->link_speed; |
| 267 | } |
| 268 | EXPORT_SYMBOL(fc_get_host_speed); |
| 269 | |
| 270 | struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *shost) |
| 271 | { |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 272 | struct fc_host_statistics *fcoe_stats; |
| 273 | struct fc_lport *lp = shost_priv(shost); |
| 274 | struct timespec v0, v1; |
Robert Love | 582b45b | 2009-03-31 15:51:50 -0700 | [diff] [blame] | 275 | unsigned int cpu; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 276 | |
| 277 | fcoe_stats = &lp->host_stats; |
| 278 | memset(fcoe_stats, 0, sizeof(struct fc_host_statistics)); |
| 279 | |
| 280 | jiffies_to_timespec(jiffies, &v0); |
| 281 | jiffies_to_timespec(lp->boot_time, &v1); |
| 282 | fcoe_stats->seconds_since_last_reset = (v0.tv_sec - v1.tv_sec); |
| 283 | |
Robert Love | 582b45b | 2009-03-31 15:51:50 -0700 | [diff] [blame] | 284 | for_each_possible_cpu(cpu) { |
| 285 | struct fcoe_dev_stats *stats; |
| 286 | |
| 287 | stats = per_cpu_ptr(lp->dev_stats, cpu); |
| 288 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 289 | fcoe_stats->tx_frames += stats->TxFrames; |
| 290 | fcoe_stats->tx_words += stats->TxWords; |
| 291 | fcoe_stats->rx_frames += stats->RxFrames; |
| 292 | fcoe_stats->rx_words += stats->RxWords; |
| 293 | fcoe_stats->error_frames += stats->ErrorFrames; |
| 294 | fcoe_stats->invalid_crc_count += stats->InvalidCRCCount; |
| 295 | fcoe_stats->fcp_input_requests += stats->InputRequests; |
| 296 | fcoe_stats->fcp_output_requests += stats->OutputRequests; |
| 297 | fcoe_stats->fcp_control_requests += stats->ControlRequests; |
| 298 | fcoe_stats->fcp_input_megabytes += stats->InputMegabytes; |
| 299 | fcoe_stats->fcp_output_megabytes += stats->OutputMegabytes; |
| 300 | fcoe_stats->link_failure_count += stats->LinkFailureCount; |
| 301 | } |
| 302 | fcoe_stats->lip_count = -1; |
| 303 | fcoe_stats->nos_count = -1; |
| 304 | fcoe_stats->loss_of_sync_count = -1; |
| 305 | fcoe_stats->loss_of_signal_count = -1; |
| 306 | fcoe_stats->prim_seq_protocol_err_count = -1; |
| 307 | fcoe_stats->dumped_frames = -1; |
| 308 | return fcoe_stats; |
| 309 | } |
| 310 | EXPORT_SYMBOL(fc_get_host_stats); |
| 311 | |
| 312 | /* |
| 313 | * Fill in FLOGI command for request. |
| 314 | */ |
| 315 | static void |
| 316 | fc_lport_flogi_fill(struct fc_lport *lport, struct fc_els_flogi *flogi, |
| 317 | unsigned int op) |
| 318 | { |
| 319 | struct fc_els_csp *sp; |
| 320 | struct fc_els_cssp *cp; |
| 321 | |
| 322 | memset(flogi, 0, sizeof(*flogi)); |
| 323 | flogi->fl_cmd = (u8) op; |
| 324 | put_unaligned_be64(lport->wwpn, &flogi->fl_wwpn); |
| 325 | put_unaligned_be64(lport->wwnn, &flogi->fl_wwnn); |
| 326 | sp = &flogi->fl_csp; |
| 327 | sp->sp_hi_ver = 0x20; |
| 328 | sp->sp_lo_ver = 0x20; |
| 329 | sp->sp_bb_cred = htons(10); /* this gets set by gateway */ |
| 330 | sp->sp_bb_data = htons((u16) lport->mfs); |
| 331 | cp = &flogi->fl_cssp[3 - 1]; /* class 3 parameters */ |
| 332 | cp->cp_class = htons(FC_CPC_VALID | FC_CPC_SEQ); |
| 333 | if (op != ELS_FLOGI) { |
| 334 | sp->sp_features = htons(FC_SP_FT_CIRO); |
| 335 | sp->sp_tot_seq = htons(255); /* seq. we accept */ |
| 336 | sp->sp_rel_off = htons(0x1f); |
| 337 | sp->sp_e_d_tov = htonl(lport->e_d_tov); |
| 338 | |
| 339 | cp->cp_rdfs = htons((u16) lport->mfs); |
| 340 | cp->cp_con_seq = htons(255); |
| 341 | cp->cp_open_seq = 1; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * Add a supported FC-4 type. |
| 347 | */ |
| 348 | static void fc_lport_add_fc4_type(struct fc_lport *lport, enum fc_fh_type type) |
| 349 | { |
| 350 | __be32 *mp; |
| 351 | |
| 352 | mp = &lport->fcts.ff_type_map[type / FC_NS_BPW]; |
| 353 | *mp = htonl(ntohl(*mp) | 1UL << (type % FC_NS_BPW)); |
| 354 | } |
| 355 | |
| 356 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 357 | * fc_lport_recv_rlir_req() - Handle received Registered Link Incident Report. |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 358 | * @lport: Fibre Channel local port recieving the RLIR |
| 359 | * @sp: current sequence in the RLIR exchange |
| 360 | * @fp: RLIR request frame |
| 361 | * |
Joe Eykholt | 1b69bc0 | 2009-10-21 16:27:17 -0700 | [diff] [blame] | 362 | * Locking Note: The lport lock is expected to be held before calling |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 363 | * this function. |
| 364 | */ |
| 365 | static void fc_lport_recv_rlir_req(struct fc_seq *sp, struct fc_frame *fp, |
| 366 | struct fc_lport *lport) |
| 367 | { |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 368 | FC_LPORT_DBG(lport, "Received RLIR request while in state %s\n", |
| 369 | fc_lport_state(lport)); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 370 | |
| 371 | lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL); |
| 372 | fc_frame_free(fp); |
| 373 | } |
| 374 | |
| 375 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 376 | * fc_lport_recv_echo_req() - Handle received ECHO request |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 377 | * @lport: Fibre Channel local port recieving the ECHO |
| 378 | * @sp: current sequence in the ECHO exchange |
| 379 | * @fp: ECHO request frame |
| 380 | * |
Joe Eykholt | 1b69bc0 | 2009-10-21 16:27:17 -0700 | [diff] [blame] | 381 | * Locking Note: The lport lock is expected to be held before calling |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 382 | * this function. |
| 383 | */ |
| 384 | static void fc_lport_recv_echo_req(struct fc_seq *sp, struct fc_frame *in_fp, |
| 385 | struct fc_lport *lport) |
| 386 | { |
| 387 | struct fc_frame *fp; |
| 388 | struct fc_exch *ep = fc_seq_exch(sp); |
| 389 | unsigned int len; |
| 390 | void *pp; |
| 391 | void *dp; |
| 392 | u32 f_ctl; |
| 393 | |
Joe Eykholt | 1b69bc0 | 2009-10-21 16:27:17 -0700 | [diff] [blame] | 394 | FC_LPORT_DBG(lport, "Received ECHO request while in state %s\n", |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 395 | fc_lport_state(lport)); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 396 | |
| 397 | len = fr_len(in_fp) - sizeof(struct fc_frame_header); |
| 398 | pp = fc_frame_payload_get(in_fp, len); |
| 399 | |
| 400 | if (len < sizeof(__be32)) |
| 401 | len = sizeof(__be32); |
| 402 | |
| 403 | fp = fc_frame_alloc(lport, len); |
| 404 | if (fp) { |
| 405 | dp = fc_frame_payload_get(fp, len); |
| 406 | memcpy(dp, pp, len); |
Joe Eykholt | 1b69bc0 | 2009-10-21 16:27:17 -0700 | [diff] [blame] | 407 | *((__be32 *)dp) = htonl(ELS_LS_ACC << 24); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 408 | sp = lport->tt.seq_start_next(sp); |
| 409 | f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ; |
| 410 | fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid, |
| 411 | FC_TYPE_ELS, f_ctl, 0); |
| 412 | lport->tt.seq_send(lport, sp, fp); |
| 413 | } |
| 414 | fc_frame_free(in_fp); |
| 415 | } |
| 416 | |
| 417 | /** |
Joe Eykholt | 1b69bc0 | 2009-10-21 16:27:17 -0700 | [diff] [blame] | 418 | * fc_lport_recv_rnid_req() - Handle received Request Node ID data request |
| 419 | * @sp: The sequence in the RNID exchange |
| 420 | * @fp: The RNID request frame |
| 421 | * @lport: The local port recieving the RNID |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 422 | * |
Joe Eykholt | 1b69bc0 | 2009-10-21 16:27:17 -0700 | [diff] [blame] | 423 | * Locking Note: The lport lock is expected to be held before calling |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 424 | * this function. |
| 425 | */ |
| 426 | static void fc_lport_recv_rnid_req(struct fc_seq *sp, struct fc_frame *in_fp, |
| 427 | struct fc_lport *lport) |
| 428 | { |
| 429 | struct fc_frame *fp; |
| 430 | struct fc_exch *ep = fc_seq_exch(sp); |
| 431 | struct fc_els_rnid *req; |
| 432 | struct { |
| 433 | struct fc_els_rnid_resp rnid; |
| 434 | struct fc_els_rnid_cid cid; |
| 435 | struct fc_els_rnid_gen gen; |
| 436 | } *rp; |
| 437 | struct fc_seq_els_data rjt_data; |
| 438 | u8 fmt; |
| 439 | size_t len; |
| 440 | u32 f_ctl; |
| 441 | |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 442 | FC_LPORT_DBG(lport, "Received RNID request while in state %s\n", |
| 443 | fc_lport_state(lport)); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 444 | |
| 445 | req = fc_frame_payload_get(in_fp, sizeof(*req)); |
| 446 | if (!req) { |
| 447 | rjt_data.fp = NULL; |
| 448 | rjt_data.reason = ELS_RJT_LOGIC; |
| 449 | rjt_data.explan = ELS_EXPL_NONE; |
| 450 | lport->tt.seq_els_rsp_send(sp, ELS_LS_RJT, &rjt_data); |
| 451 | } else { |
| 452 | fmt = req->rnid_fmt; |
| 453 | len = sizeof(*rp); |
| 454 | if (fmt != ELS_RNIDF_GEN || |
| 455 | ntohl(lport->rnid_gen.rnid_atype) == 0) { |
| 456 | fmt = ELS_RNIDF_NONE; /* nothing to provide */ |
| 457 | len -= sizeof(rp->gen); |
| 458 | } |
| 459 | fp = fc_frame_alloc(lport, len); |
| 460 | if (fp) { |
| 461 | rp = fc_frame_payload_get(fp, len); |
| 462 | memset(rp, 0, len); |
| 463 | rp->rnid.rnid_cmd = ELS_LS_ACC; |
| 464 | rp->rnid.rnid_fmt = fmt; |
| 465 | rp->rnid.rnid_cid_len = sizeof(rp->cid); |
| 466 | rp->cid.rnid_wwpn = htonll(lport->wwpn); |
| 467 | rp->cid.rnid_wwnn = htonll(lport->wwnn); |
| 468 | if (fmt == ELS_RNIDF_GEN) { |
| 469 | rp->rnid.rnid_sid_len = sizeof(rp->gen); |
| 470 | memcpy(&rp->gen, &lport->rnid_gen, |
| 471 | sizeof(rp->gen)); |
| 472 | } |
| 473 | sp = lport->tt.seq_start_next(sp); |
| 474 | f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ; |
| 475 | f_ctl |= FC_FC_END_SEQ | FC_FC_SEQ_INIT; |
| 476 | fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid, |
| 477 | FC_TYPE_ELS, f_ctl, 0); |
| 478 | lport->tt.seq_send(lport, sp, fp); |
| 479 | } |
| 480 | } |
| 481 | fc_frame_free(in_fp); |
| 482 | } |
| 483 | |
| 484 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 485 | * fc_lport_recv_logo_req() - Handle received fabric LOGO request |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 486 | * @lport: Fibre Channel local port recieving the LOGO |
| 487 | * @sp: current sequence in the LOGO exchange |
| 488 | * @fp: LOGO request frame |
| 489 | * |
| 490 | * Locking Note: The lport lock is exected to be held before calling |
| 491 | * this function. |
| 492 | */ |
| 493 | static void fc_lport_recv_logo_req(struct fc_seq *sp, struct fc_frame *fp, |
| 494 | struct fc_lport *lport) |
| 495 | { |
| 496 | lport->tt.seq_els_rsp_send(sp, ELS_LS_ACC, NULL); |
| 497 | fc_lport_enter_reset(lport); |
| 498 | fc_frame_free(fp); |
| 499 | } |
| 500 | |
| 501 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 502 | * fc_fabric_login() - Start the lport state machine |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 503 | * @lport: The lport that should log into the fabric |
| 504 | * |
| 505 | * Locking Note: This function should not be called |
| 506 | * with the lport lock held. |
| 507 | */ |
| 508 | int fc_fabric_login(struct fc_lport *lport) |
| 509 | { |
| 510 | int rc = -1; |
| 511 | |
| 512 | mutex_lock(&lport->lp_mutex); |
Joe Eykholt | b1d9fd5 | 2009-07-29 17:04:22 -0700 | [diff] [blame] | 513 | if (lport->state == LPORT_ST_DISABLED) { |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 514 | fc_lport_enter_reset(lport); |
| 515 | rc = 0; |
| 516 | } |
| 517 | mutex_unlock(&lport->lp_mutex); |
| 518 | |
| 519 | return rc; |
| 520 | } |
| 521 | EXPORT_SYMBOL(fc_fabric_login); |
| 522 | |
| 523 | /** |
Chris Leech | 8faecdd | 2009-11-03 11:46:19 -0800 | [diff] [blame] | 524 | * __fc_linkup() - Handler for transport linkup events |
| 525 | * @lport: The lport whose link is up |
| 526 | * |
| 527 | * Locking: must be called with the lp_mutex held |
| 528 | */ |
| 529 | void __fc_linkup(struct fc_lport *lport) |
| 530 | { |
| 531 | if (!lport->link_up) { |
| 532 | lport->link_up = 1; |
| 533 | |
| 534 | if (lport->state == LPORT_ST_RESET) |
| 535 | fc_lport_enter_flogi(lport); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 540 | * fc_linkup() - Handler for transport linkup events |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 541 | * @lport: The lport whose link is up |
| 542 | */ |
| 543 | void fc_linkup(struct fc_lport *lport) |
| 544 | { |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 545 | printk(KERN_INFO "libfc: Link up on port (%6x)\n", |
| 546 | fc_host_port_id(lport->host)); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 547 | |
| 548 | mutex_lock(&lport->lp_mutex); |
Chris Leech | 8faecdd | 2009-11-03 11:46:19 -0800 | [diff] [blame] | 549 | __fc_linkup(lport); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 550 | mutex_unlock(&lport->lp_mutex); |
| 551 | } |
| 552 | EXPORT_SYMBOL(fc_linkup); |
| 553 | |
| 554 | /** |
Chris Leech | 8faecdd | 2009-11-03 11:46:19 -0800 | [diff] [blame] | 555 | * __fc_linkdown() - Handler for transport linkdown events |
| 556 | * @lport: The lport whose link is down |
| 557 | * |
| 558 | * Locking: must be called with the lp_mutex held |
| 559 | */ |
| 560 | void __fc_linkdown(struct fc_lport *lport) |
| 561 | { |
| 562 | if (lport->link_up) { |
| 563 | lport->link_up = 0; |
| 564 | fc_lport_enter_reset(lport); |
| 565 | lport->tt.fcp_cleanup(lport); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 570 | * fc_linkdown() - Handler for transport linkdown events |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 571 | * @lport: The lport whose link is down |
| 572 | */ |
| 573 | void fc_linkdown(struct fc_lport *lport) |
| 574 | { |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 575 | printk(KERN_INFO "libfc: Link down on port (%6x)\n", |
| 576 | fc_host_port_id(lport->host)); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 577 | |
Chris Leech | 8faecdd | 2009-11-03 11:46:19 -0800 | [diff] [blame] | 578 | mutex_lock(&lport->lp_mutex); |
| 579 | __fc_linkdown(lport); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 580 | mutex_unlock(&lport->lp_mutex); |
| 581 | } |
| 582 | EXPORT_SYMBOL(fc_linkdown); |
| 583 | |
| 584 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 585 | * fc_fabric_logoff() - Logout of the fabric |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 586 | * @lport: fc_lport pointer to logoff the fabric |
| 587 | * |
| 588 | * Return value: |
| 589 | * 0 for success, -1 for failure |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 590 | */ |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 591 | int fc_fabric_logoff(struct fc_lport *lport) |
| 592 | { |
| 593 | lport->tt.disc_stop_final(lport); |
| 594 | mutex_lock(&lport->lp_mutex); |
Abhijeet Joglekar | a0fd2e4 | 2009-04-21 16:27:09 -0700 | [diff] [blame] | 595 | if (lport->dns_rp) |
| 596 | lport->tt.rport_logoff(lport->dns_rp); |
| 597 | mutex_unlock(&lport->lp_mutex); |
| 598 | lport->tt.rport_flush_queue(); |
| 599 | mutex_lock(&lport->lp_mutex); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 600 | fc_lport_enter_logo(lport); |
| 601 | mutex_unlock(&lport->lp_mutex); |
Steve Ma | f7db2c1 | 2009-02-27 10:55:13 -0800 | [diff] [blame] | 602 | cancel_delayed_work_sync(&lport->retry_work); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 603 | return 0; |
| 604 | } |
| 605 | EXPORT_SYMBOL(fc_fabric_logoff); |
| 606 | |
| 607 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 608 | * fc_lport_destroy() - unregister a fc_lport |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 609 | * @lport: fc_lport pointer to unregister |
| 610 | * |
| 611 | * Return value: |
| 612 | * None |
| 613 | * Note: |
| 614 | * exit routine for fc_lport instance |
| 615 | * clean-up all the allocated memory |
| 616 | * and free up other system resources. |
| 617 | * |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 618 | */ |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 619 | int fc_lport_destroy(struct fc_lport *lport) |
| 620 | { |
Abhijeet Joglekar | bbf1566 | 2009-04-21 16:27:14 -0700 | [diff] [blame] | 621 | mutex_lock(&lport->lp_mutex); |
Joe Eykholt | b1d9fd5 | 2009-07-29 17:04:22 -0700 | [diff] [blame] | 622 | lport->state = LPORT_ST_DISABLED; |
Abhijeet Joglekar | bbf1566 | 2009-04-21 16:27:14 -0700 | [diff] [blame] | 623 | lport->link_up = 0; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 624 | lport->tt.frame_send = fc_frame_drop; |
Abhijeet Joglekar | bbf1566 | 2009-04-21 16:27:14 -0700 | [diff] [blame] | 625 | mutex_unlock(&lport->lp_mutex); |
| 626 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 627 | lport->tt.fcp_abort_io(lport); |
Joe Eykholt | e9ba8b4 | 2009-07-29 17:04:33 -0700 | [diff] [blame] | 628 | lport->tt.disc_stop_final(lport); |
Abhijeet Joglekar | 1f6ff36 | 2009-02-27 10:54:35 -0800 | [diff] [blame] | 629 | lport->tt.exch_mgr_reset(lport, 0, 0); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 630 | return 0; |
| 631 | } |
| 632 | EXPORT_SYMBOL(fc_lport_destroy); |
| 633 | |
| 634 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 635 | * fc_set_mfs() - sets up the mfs for the corresponding fc_lport |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 636 | * @lport: fc_lport pointer to unregister |
| 637 | * @mfs: the new mfs for fc_lport |
| 638 | * |
| 639 | * Set mfs for the given fc_lport to the new mfs. |
| 640 | * |
| 641 | * Return: 0 for success |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 642 | */ |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 643 | int fc_set_mfs(struct fc_lport *lport, u32 mfs) |
| 644 | { |
| 645 | unsigned int old_mfs; |
| 646 | int rc = -EINVAL; |
| 647 | |
| 648 | mutex_lock(&lport->lp_mutex); |
| 649 | |
| 650 | old_mfs = lport->mfs; |
| 651 | |
| 652 | if (mfs >= FC_MIN_MAX_FRAME) { |
| 653 | mfs &= ~3; |
| 654 | if (mfs > FC_MAX_FRAME) |
| 655 | mfs = FC_MAX_FRAME; |
| 656 | mfs -= sizeof(struct fc_frame_header); |
| 657 | lport->mfs = mfs; |
| 658 | rc = 0; |
| 659 | } |
| 660 | |
| 661 | if (!rc && mfs < old_mfs) |
| 662 | fc_lport_enter_reset(lport); |
| 663 | |
| 664 | mutex_unlock(&lport->lp_mutex); |
| 665 | |
| 666 | return rc; |
| 667 | } |
| 668 | EXPORT_SYMBOL(fc_set_mfs); |
| 669 | |
| 670 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 671 | * fc_lport_disc_callback() - Callback for discovery events |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 672 | * @lport: FC local port |
| 673 | * @event: The discovery event |
| 674 | */ |
| 675 | void fc_lport_disc_callback(struct fc_lport *lport, enum fc_disc_event event) |
| 676 | { |
| 677 | switch (event) { |
| 678 | case DISC_EV_SUCCESS: |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 679 | FC_LPORT_DBG(lport, "Discovery succeeded\n"); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 680 | break; |
| 681 | case DISC_EV_FAILED: |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 682 | printk(KERN_ERR "libfc: Discovery failed for port (%6x)\n", |
| 683 | fc_host_port_id(lport->host)); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 684 | mutex_lock(&lport->lp_mutex); |
| 685 | fc_lport_enter_reset(lport); |
| 686 | mutex_unlock(&lport->lp_mutex); |
| 687 | break; |
| 688 | case DISC_EV_NONE: |
| 689 | WARN_ON(1); |
| 690 | break; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 695 | * fc_rport_enter_ready() - Enter the ready state and start discovery |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 696 | * @lport: Fibre Channel local port that is ready |
| 697 | * |
| 698 | * Locking Note: The lport lock is expected to be held before calling |
| 699 | * this routine. |
| 700 | */ |
| 701 | static void fc_lport_enter_ready(struct fc_lport *lport) |
| 702 | { |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 703 | FC_LPORT_DBG(lport, "Entered READY from state %s\n", |
| 704 | fc_lport_state(lport)); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 705 | |
| 706 | fc_lport_state_enter(lport, LPORT_ST_READY); |
Chris Leech | 8faecdd | 2009-11-03 11:46:19 -0800 | [diff] [blame] | 707 | if (lport->vport) |
| 708 | fc_vport_set_state(lport->vport, FC_VPORT_ACTIVE); |
| 709 | fc_vports_linkchange(lport); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 710 | |
Joe Eykholt | 29d898e | 2009-08-25 14:02:49 -0700 | [diff] [blame] | 711 | if (!lport->ptp_rp) |
| 712 | lport->tt.disc_start(fc_lport_disc_callback, lport); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 716 | * fc_lport_recv_flogi_req() - Receive a FLOGI request |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 717 | * @sp_in: The sequence the FLOGI is on |
| 718 | * @rx_fp: The frame the FLOGI is in |
| 719 | * @lport: The lport that recieved the request |
| 720 | * |
| 721 | * A received FLOGI request indicates a point-to-point connection. |
| 722 | * Accept it with the common service parameters indicating our N port. |
| 723 | * Set up to do a PLOGI if we have the higher-number WWPN. |
| 724 | * |
Joe Eykholt | 1b69bc0 | 2009-10-21 16:27:17 -0700 | [diff] [blame] | 725 | * Locking Note: The lport lock is expected to be held before calling |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 726 | * this function. |
| 727 | */ |
| 728 | static void fc_lport_recv_flogi_req(struct fc_seq *sp_in, |
| 729 | struct fc_frame *rx_fp, |
| 730 | struct fc_lport *lport) |
| 731 | { |
| 732 | struct fc_frame *fp; |
| 733 | struct fc_frame_header *fh; |
| 734 | struct fc_seq *sp; |
| 735 | struct fc_exch *ep; |
| 736 | struct fc_els_flogi *flp; |
| 737 | struct fc_els_flogi *new_flp; |
| 738 | u64 remote_wwpn; |
| 739 | u32 remote_fid; |
| 740 | u32 local_fid; |
| 741 | u32 f_ctl; |
| 742 | |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 743 | FC_LPORT_DBG(lport, "Received FLOGI request while in state %s\n", |
| 744 | fc_lport_state(lport)); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 745 | |
| 746 | fh = fc_frame_header_get(rx_fp); |
| 747 | remote_fid = ntoh24(fh->fh_s_id); |
| 748 | flp = fc_frame_payload_get(rx_fp, sizeof(*flp)); |
| 749 | if (!flp) |
| 750 | goto out; |
| 751 | remote_wwpn = get_unaligned_be64(&flp->fl_wwpn); |
| 752 | if (remote_wwpn == lport->wwpn) { |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 753 | printk(KERN_WARNING "libfc: Received FLOGI from port " |
| 754 | "with same WWPN %llx\n", remote_wwpn); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 755 | goto out; |
| 756 | } |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 757 | FC_LPORT_DBG(lport, "FLOGI from port WWPN %llx\n", remote_wwpn); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 758 | |
| 759 | /* |
| 760 | * XXX what is the right thing to do for FIDs? |
| 761 | * The originator might expect our S_ID to be 0xfffffe. |
| 762 | * But if so, both of us could end up with the same FID. |
| 763 | */ |
| 764 | local_fid = FC_LOCAL_PTP_FID_LO; |
| 765 | if (remote_wwpn < lport->wwpn) { |
| 766 | local_fid = FC_LOCAL_PTP_FID_HI; |
| 767 | if (!remote_fid || remote_fid == local_fid) |
| 768 | remote_fid = FC_LOCAL_PTP_FID_LO; |
| 769 | } else if (!remote_fid) { |
| 770 | remote_fid = FC_LOCAL_PTP_FID_HI; |
| 771 | } |
| 772 | |
| 773 | fc_host_port_id(lport->host) = local_fid; |
| 774 | |
| 775 | fp = fc_frame_alloc(lport, sizeof(*flp)); |
| 776 | if (fp) { |
| 777 | sp = lport->tt.seq_start_next(fr_seq(rx_fp)); |
| 778 | new_flp = fc_frame_payload_get(fp, sizeof(*flp)); |
| 779 | fc_lport_flogi_fill(lport, new_flp, ELS_FLOGI); |
| 780 | new_flp->fl_cmd = (u8) ELS_LS_ACC; |
| 781 | |
| 782 | /* |
| 783 | * Send the response. If this fails, the originator should |
| 784 | * repeat the sequence. |
| 785 | */ |
| 786 | f_ctl = FC_FC_EX_CTX | FC_FC_LAST_SEQ | FC_FC_END_SEQ; |
| 787 | ep = fc_seq_exch(sp); |
| 788 | fc_fill_fc_hdr(fp, FC_RCTL_ELS_REP, ep->did, ep->sid, |
| 789 | FC_TYPE_ELS, f_ctl, 0); |
| 790 | lport->tt.seq_send(lport, sp, fp); |
| 791 | |
| 792 | } else { |
| 793 | fc_lport_error(lport, fp); |
| 794 | } |
| 795 | fc_lport_ptp_setup(lport, remote_fid, remote_wwpn, |
| 796 | get_unaligned_be64(&flp->fl_wwnn)); |
| 797 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 798 | out: |
| 799 | sp = fr_seq(rx_fp); |
| 800 | fc_frame_free(rx_fp); |
| 801 | } |
| 802 | |
| 803 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 804 | * fc_lport_recv_req() - The generic lport request handler |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 805 | * @lport: The lport that received the request |
| 806 | * @sp: The sequence the request is on |
| 807 | * @fp: The frame the request is in |
| 808 | * |
| 809 | * This function will see if the lport handles the request or |
| 810 | * if an rport should handle the request. |
| 811 | * |
| 812 | * Locking Note: This function should not be called with the lport |
| 813 | * lock held becuase it will grab the lock. |
| 814 | */ |
| 815 | static void fc_lport_recv_req(struct fc_lport *lport, struct fc_seq *sp, |
| 816 | struct fc_frame *fp) |
| 817 | { |
| 818 | struct fc_frame_header *fh = fc_frame_header_get(fp); |
| 819 | void (*recv) (struct fc_seq *, struct fc_frame *, struct fc_lport *); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 820 | |
| 821 | mutex_lock(&lport->lp_mutex); |
| 822 | |
| 823 | /* |
| 824 | * Handle special ELS cases like FLOGI, LOGO, and |
| 825 | * RSCN here. These don't require a session. |
| 826 | * Even if we had a session, it might not be ready. |
| 827 | */ |
Joe Eykholt | e9ba8b4 | 2009-07-29 17:04:33 -0700 | [diff] [blame] | 828 | if (!lport->link_up) |
| 829 | fc_frame_free(fp); |
| 830 | else if (fh->fh_type == FC_TYPE_ELS && |
| 831 | fh->fh_r_ctl == FC_RCTL_ELS_REQ) { |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 832 | /* |
| 833 | * Check opcode. |
| 834 | */ |
Joe Eykholt | 131203a | 2009-08-25 14:03:10 -0700 | [diff] [blame] | 835 | recv = lport->tt.rport_recv_req; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 836 | switch (fc_frame_payload_op(fp)) { |
| 837 | case ELS_FLOGI: |
| 838 | recv = fc_lport_recv_flogi_req; |
| 839 | break; |
| 840 | case ELS_LOGO: |
| 841 | fh = fc_frame_header_get(fp); |
| 842 | if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) |
| 843 | recv = fc_lport_recv_logo_req; |
| 844 | break; |
| 845 | case ELS_RSCN: |
| 846 | recv = lport->tt.disc_recv_req; |
| 847 | break; |
| 848 | case ELS_ECHO: |
| 849 | recv = fc_lport_recv_echo_req; |
| 850 | break; |
| 851 | case ELS_RLIR: |
| 852 | recv = fc_lport_recv_rlir_req; |
| 853 | break; |
| 854 | case ELS_RNID: |
| 855 | recv = fc_lport_recv_rnid_req; |
| 856 | break; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 857 | } |
| 858 | |
Joe Eykholt | 131203a | 2009-08-25 14:03:10 -0700 | [diff] [blame] | 859 | recv(sp, fp, lport); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 860 | } else { |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 861 | FC_LPORT_DBG(lport, "dropping invalid frame (eof %x)\n", |
| 862 | fr_eof(fp)); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 863 | fc_frame_free(fp); |
| 864 | } |
| 865 | mutex_unlock(&lport->lp_mutex); |
| 866 | |
| 867 | /* |
| 868 | * The common exch_done for all request may not be good |
| 869 | * if any request requires longer hold on exhange. XXX |
| 870 | */ |
| 871 | lport->tt.exch_done(sp); |
| 872 | } |
| 873 | |
| 874 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 875 | * fc_lport_reset() - Reset an lport |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 876 | * @lport: The lport which should be reset |
| 877 | * |
| 878 | * Locking Note: This functions should not be called with the |
| 879 | * lport lock held. |
| 880 | */ |
| 881 | int fc_lport_reset(struct fc_lport *lport) |
| 882 | { |
Steve Ma | f7db2c1 | 2009-02-27 10:55:13 -0800 | [diff] [blame] | 883 | cancel_delayed_work_sync(&lport->retry_work); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 884 | mutex_lock(&lport->lp_mutex); |
| 885 | fc_lport_enter_reset(lport); |
| 886 | mutex_unlock(&lport->lp_mutex); |
| 887 | return 0; |
| 888 | } |
| 889 | EXPORT_SYMBOL(fc_lport_reset); |
| 890 | |
| 891 | /** |
Joe Eykholt | 1190d92 | 2009-07-29 17:04:27 -0700 | [diff] [blame] | 892 | * fc_lport_reset_locked() - Reset the local port |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 893 | * @lport: Fibre Channel local port to be reset |
| 894 | * |
| 895 | * Locking Note: The lport lock is expected to be held before calling |
| 896 | * this routine. |
| 897 | */ |
Joe Eykholt | 1190d92 | 2009-07-29 17:04:27 -0700 | [diff] [blame] | 898 | static void fc_lport_reset_locked(struct fc_lport *lport) |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 899 | { |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 900 | if (lport->dns_rp) |
| 901 | lport->tt.rport_logoff(lport->dns_rp); |
| 902 | |
Joe Eykholt | 48f0090 | 2009-08-25 14:01:50 -0700 | [diff] [blame] | 903 | lport->ptp_rp = NULL; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 904 | |
| 905 | lport->tt.disc_stop(lport); |
| 906 | |
Abhijeet Joglekar | 1f6ff36 | 2009-02-27 10:54:35 -0800 | [diff] [blame] | 907 | lport->tt.exch_mgr_reset(lport, 0, 0); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 908 | fc_host_fabric_name(lport->host) = 0; |
| 909 | fc_host_port_id(lport->host) = 0; |
Joe Eykholt | 1190d92 | 2009-07-29 17:04:27 -0700 | [diff] [blame] | 910 | } |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 911 | |
Joe Eykholt | 1190d92 | 2009-07-29 17:04:27 -0700 | [diff] [blame] | 912 | /** |
| 913 | * fc_lport_enter_reset() - Reset the local port |
| 914 | * @lport: Fibre Channel local port to be reset |
| 915 | * |
| 916 | * Locking Note: The lport lock is expected to be held before calling |
| 917 | * this routine. |
| 918 | */ |
| 919 | static void fc_lport_enter_reset(struct fc_lport *lport) |
| 920 | { |
| 921 | FC_LPORT_DBG(lport, "Entered RESET state from %s state\n", |
| 922 | fc_lport_state(lport)); |
| 923 | |
Chris Leech | 8faecdd | 2009-11-03 11:46:19 -0800 | [diff] [blame] | 924 | if (lport->vport) { |
| 925 | if (lport->link_up) |
| 926 | fc_vport_set_state(lport->vport, FC_VPORT_INITIALIZING); |
| 927 | else |
| 928 | fc_vport_set_state(lport->vport, FC_VPORT_LINKDOWN); |
| 929 | } |
Joe Eykholt | 1190d92 | 2009-07-29 17:04:27 -0700 | [diff] [blame] | 930 | fc_lport_state_enter(lport, LPORT_ST_RESET); |
Chris Leech | 8faecdd | 2009-11-03 11:46:19 -0800 | [diff] [blame] | 931 | fc_vports_linkchange(lport); |
Joe Eykholt | 1190d92 | 2009-07-29 17:04:27 -0700 | [diff] [blame] | 932 | fc_lport_reset_locked(lport); |
Vasu Dev | bc0e17f | 2009-02-27 10:54:57 -0800 | [diff] [blame] | 933 | if (lport->link_up) |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 934 | fc_lport_enter_flogi(lport); |
| 935 | } |
| 936 | |
| 937 | /** |
Joe Eykholt | 1190d92 | 2009-07-29 17:04:27 -0700 | [diff] [blame] | 938 | * fc_lport_enter_disabled() - disable the local port |
| 939 | * @lport: Fibre Channel local port to be reset |
| 940 | * |
| 941 | * Locking Note: The lport lock is expected to be held before calling |
| 942 | * this routine. |
| 943 | */ |
| 944 | static void fc_lport_enter_disabled(struct fc_lport *lport) |
| 945 | { |
| 946 | FC_LPORT_DBG(lport, "Entered disabled state from %s state\n", |
| 947 | fc_lport_state(lport)); |
| 948 | |
| 949 | fc_lport_state_enter(lport, LPORT_ST_DISABLED); |
Chris Leech | 8faecdd | 2009-11-03 11:46:19 -0800 | [diff] [blame] | 950 | fc_vports_linkchange(lport); |
Joe Eykholt | 1190d92 | 2009-07-29 17:04:27 -0700 | [diff] [blame] | 951 | fc_lport_reset_locked(lport); |
| 952 | } |
| 953 | |
| 954 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 955 | * fc_lport_error() - Handler for any errors |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 956 | * @lport: The fc_lport object |
| 957 | * @fp: The frame pointer |
| 958 | * |
| 959 | * If the error was caused by a resource allocation failure |
| 960 | * then wait for half a second and retry, otherwise retry |
| 961 | * after the e_d_tov time. |
| 962 | */ |
| 963 | static void fc_lport_error(struct fc_lport *lport, struct fc_frame *fp) |
| 964 | { |
| 965 | unsigned long delay = 0; |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 966 | FC_LPORT_DBG(lport, "Error %ld in state %s, retries %d\n", |
| 967 | PTR_ERR(fp), fc_lport_state(lport), |
| 968 | lport->retry_count); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 969 | |
| 970 | if (!fp || PTR_ERR(fp) == -FC_EX_TIMEOUT) { |
| 971 | /* |
| 972 | * Memory allocation failure, or the exchange timed out. |
| 973 | * Retry after delay |
| 974 | */ |
| 975 | if (lport->retry_count < lport->max_retry_count) { |
| 976 | lport->retry_count++; |
| 977 | if (!fp) |
| 978 | delay = msecs_to_jiffies(500); |
| 979 | else |
| 980 | delay = msecs_to_jiffies(lport->e_d_tov); |
| 981 | |
| 982 | schedule_delayed_work(&lport->retry_work, delay); |
| 983 | } else { |
| 984 | switch (lport->state) { |
Joe Eykholt | b1d9fd5 | 2009-07-29 17:04:22 -0700 | [diff] [blame] | 985 | case LPORT_ST_DISABLED: |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 986 | case LPORT_ST_READY: |
| 987 | case LPORT_ST_RESET: |
Chris Leech | c9c7bd7 | 2009-11-03 11:46:51 -0800 | [diff] [blame] | 988 | case LPORT_ST_RNN_ID: |
Chris Leech | 5baa17c | 2009-11-03 11:46:56 -0800 | [diff] [blame] | 989 | case LPORT_ST_RSNN_NN: |
Chris Leech | c9866a5 | 2009-11-03 11:47:01 -0800 | [diff] [blame] | 990 | case LPORT_ST_RSPN_ID: |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 991 | case LPORT_ST_RFT_ID: |
| 992 | case LPORT_ST_SCR: |
| 993 | case LPORT_ST_DNS: |
| 994 | case LPORT_ST_FLOGI: |
| 995 | case LPORT_ST_LOGO: |
| 996 | fc_lport_enter_reset(lport); |
| 997 | break; |
| 998 | } |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | /** |
Chris Leech | 7cccc15 | 2009-11-03 11:47:07 -0800 | [diff] [blame] | 1004 | * fc_lport_ns_resp() - Handle response to a name server |
| 1005 | * registration exchange |
| 1006 | * @sp: current sequence in exchange |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1007 | * @fp: response frame |
| 1008 | * @lp_arg: Fibre Channel host port instance |
| 1009 | * |
| 1010 | * Locking Note: This function will be called without the lport lock |
| 1011 | * held, but it will lock, call an _enter_* function or fc_lport_error |
| 1012 | * and then unlock the lport. |
| 1013 | */ |
Chris Leech | 7cccc15 | 2009-11-03 11:47:07 -0800 | [diff] [blame] | 1014 | static void fc_lport_ns_resp(struct fc_seq *sp, struct fc_frame *fp, |
| 1015 | void *lp_arg) |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1016 | { |
| 1017 | struct fc_lport *lport = lp_arg; |
| 1018 | struct fc_frame_header *fh; |
| 1019 | struct fc_ct_hdr *ct; |
| 1020 | |
Chris Leech | 7cccc15 | 2009-11-03 11:47:07 -0800 | [diff] [blame] | 1021 | FC_LPORT_DBG(lport, "Received a ns %s\n", fc_els_resp_type(fp)); |
Joe Eykholt | f657d29 | 2009-08-25 14:03:21 -0700 | [diff] [blame] | 1022 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1023 | if (fp == ERR_PTR(-FC_EX_CLOSED)) |
| 1024 | return; |
| 1025 | |
| 1026 | mutex_lock(&lport->lp_mutex); |
| 1027 | |
Chris Leech | 7cccc15 | 2009-11-03 11:47:07 -0800 | [diff] [blame] | 1028 | if (lport->state < LPORT_ST_RNN_ID || lport->state > LPORT_ST_RFT_ID) { |
| 1029 | FC_LPORT_DBG(lport, "Received a name server response, " |
| 1030 | "but in state %s\n", fc_lport_state(lport)); |
Abhijeet Joglekar | 76f6804 | 2009-04-21 16:26:58 -0700 | [diff] [blame] | 1031 | if (IS_ERR(fp)) |
| 1032 | goto err; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1033 | goto out; |
| 1034 | } |
| 1035 | |
Abhijeet Joglekar | 76f6804 | 2009-04-21 16:26:58 -0700 | [diff] [blame] | 1036 | if (IS_ERR(fp)) { |
| 1037 | fc_lport_error(lport, fp); |
| 1038 | goto err; |
| 1039 | } |
| 1040 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1041 | fh = fc_frame_header_get(fp); |
| 1042 | ct = fc_frame_payload_get(fp, sizeof(*ct)); |
| 1043 | |
| 1044 | if (fh && ct && fh->fh_type == FC_TYPE_CT && |
| 1045 | ct->ct_fs_type == FC_FST_DIR && |
| 1046 | ct->ct_fs_subtype == FC_NS_SUBTYPE && |
| 1047 | ntohs(ct->ct_cmd) == FC_FS_ACC) |
Chris Leech | 7cccc15 | 2009-11-03 11:47:07 -0800 | [diff] [blame] | 1048 | switch (lport->state) { |
| 1049 | case LPORT_ST_RNN_ID: |
Chris Leech | c914f7d | 2009-11-03 11:47:12 -0800 | [diff] [blame] | 1050 | fc_lport_enter_ns(lport, LPORT_ST_RSNN_NN); |
Chris Leech | 7cccc15 | 2009-11-03 11:47:07 -0800 | [diff] [blame] | 1051 | break; |
| 1052 | case LPORT_ST_RSNN_NN: |
Chris Leech | c914f7d | 2009-11-03 11:47:12 -0800 | [diff] [blame] | 1053 | fc_lport_enter_ns(lport, LPORT_ST_RSPN_ID); |
Chris Leech | 7cccc15 | 2009-11-03 11:47:07 -0800 | [diff] [blame] | 1054 | break; |
| 1055 | case LPORT_ST_RSPN_ID: |
Chris Leech | c914f7d | 2009-11-03 11:47:12 -0800 | [diff] [blame] | 1056 | fc_lport_enter_ns(lport, LPORT_ST_RFT_ID); |
Chris Leech | 7cccc15 | 2009-11-03 11:47:07 -0800 | [diff] [blame] | 1057 | break; |
| 1058 | case LPORT_ST_RFT_ID: |
| 1059 | fc_lport_enter_scr(lport); |
| 1060 | break; |
| 1061 | default: |
| 1062 | /* should have already been caught by state checks */ |
| 1063 | break; |
| 1064 | } |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1065 | else |
| 1066 | fc_lport_error(lport, fp); |
| 1067 | out: |
| 1068 | fc_frame_free(fp); |
| 1069 | err: |
| 1070 | mutex_unlock(&lport->lp_mutex); |
| 1071 | } |
| 1072 | |
| 1073 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 1074 | * fc_lport_scr_resp() - Handle response to State Change Register (SCR) request |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1075 | * @sp: current sequence in SCR exchange |
| 1076 | * @fp: response frame |
| 1077 | * @lp_arg: Fibre Channel lport port instance that sent the registration request |
| 1078 | * |
| 1079 | * Locking Note: This function will be called without the lport lock |
| 1080 | * held, but it will lock, call an _enter_* function or fc_lport_error |
| 1081 | * and then unlock the lport. |
| 1082 | */ |
| 1083 | static void fc_lport_scr_resp(struct fc_seq *sp, struct fc_frame *fp, |
| 1084 | void *lp_arg) |
| 1085 | { |
| 1086 | struct fc_lport *lport = lp_arg; |
| 1087 | u8 op; |
| 1088 | |
Joe Eykholt | f657d29 | 2009-08-25 14:03:21 -0700 | [diff] [blame] | 1089 | FC_LPORT_DBG(lport, "Received a SCR %s\n", fc_els_resp_type(fp)); |
| 1090 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1091 | if (fp == ERR_PTR(-FC_EX_CLOSED)) |
| 1092 | return; |
| 1093 | |
| 1094 | mutex_lock(&lport->lp_mutex); |
| 1095 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1096 | if (lport->state != LPORT_ST_SCR) { |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 1097 | FC_LPORT_DBG(lport, "Received a SCR response, but in state " |
| 1098 | "%s\n", fc_lport_state(lport)); |
Abhijeet Joglekar | 76f6804 | 2009-04-21 16:26:58 -0700 | [diff] [blame] | 1099 | if (IS_ERR(fp)) |
| 1100 | goto err; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1101 | goto out; |
| 1102 | } |
| 1103 | |
Abhijeet Joglekar | 76f6804 | 2009-04-21 16:26:58 -0700 | [diff] [blame] | 1104 | if (IS_ERR(fp)) { |
| 1105 | fc_lport_error(lport, fp); |
| 1106 | goto err; |
| 1107 | } |
| 1108 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1109 | op = fc_frame_payload_op(fp); |
| 1110 | if (op == ELS_LS_ACC) |
| 1111 | fc_lport_enter_ready(lport); |
| 1112 | else |
| 1113 | fc_lport_error(lport, fp); |
| 1114 | |
| 1115 | out: |
| 1116 | fc_frame_free(fp); |
| 1117 | err: |
| 1118 | mutex_unlock(&lport->lp_mutex); |
| 1119 | } |
| 1120 | |
| 1121 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 1122 | * fc_lport_enter_scr() - Send a State Change Register (SCR) request |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1123 | * @lport: Fibre Channel local port to register for state changes |
| 1124 | * |
| 1125 | * Locking Note: The lport lock is expected to be held before calling |
| 1126 | * this routine. |
| 1127 | */ |
| 1128 | static void fc_lport_enter_scr(struct fc_lport *lport) |
| 1129 | { |
| 1130 | struct fc_frame *fp; |
| 1131 | |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 1132 | FC_LPORT_DBG(lport, "Entered SCR state from %s state\n", |
| 1133 | fc_lport_state(lport)); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1134 | |
| 1135 | fc_lport_state_enter(lport, LPORT_ST_SCR); |
| 1136 | |
| 1137 | fp = fc_frame_alloc(lport, sizeof(struct fc_els_scr)); |
| 1138 | if (!fp) { |
| 1139 | fc_lport_error(lport, fp); |
| 1140 | return; |
| 1141 | } |
| 1142 | |
Joe Eykholt | a46f327 | 2009-08-25 14:00:55 -0700 | [diff] [blame] | 1143 | if (!lport->tt.elsct_send(lport, FC_FID_FCTRL, fp, ELS_SCR, |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1144 | fc_lport_scr_resp, lport, lport->e_d_tov)) |
Chris Leech | 8f550f9 | 2009-10-21 16:28:09 -0700 | [diff] [blame] | 1145 | fc_lport_error(lport, NULL); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | /** |
Chris Leech | c914f7d | 2009-11-03 11:47:12 -0800 | [diff] [blame] | 1149 | * fc_lport_enter_ns() - register some object with the name server |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1150 | * @lport: Fibre Channel local port to register |
| 1151 | * |
| 1152 | * Locking Note: The lport lock is expected to be held before calling |
| 1153 | * this routine. |
| 1154 | */ |
Chris Leech | c914f7d | 2009-11-03 11:47:12 -0800 | [diff] [blame] | 1155 | static void fc_lport_enter_ns(struct fc_lport *lport, enum fc_lport_state state) |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1156 | { |
| 1157 | struct fc_frame *fp; |
Chris Leech | c914f7d | 2009-11-03 11:47:12 -0800 | [diff] [blame] | 1158 | enum fc_ns_req cmd; |
| 1159 | int size = sizeof(struct fc_ct_hdr); |
Chris Leech | c9866a5 | 2009-11-03 11:47:01 -0800 | [diff] [blame] | 1160 | size_t len; |
| 1161 | |
Chris Leech | c914f7d | 2009-11-03 11:47:12 -0800 | [diff] [blame] | 1162 | FC_LPORT_DBG(lport, "Entered %s state from %s state\n", |
| 1163 | fc_lport_state_names[state], |
Chris Leech | c9866a5 | 2009-11-03 11:47:01 -0800 | [diff] [blame] | 1164 | fc_lport_state(lport)); |
| 1165 | |
Chris Leech | c914f7d | 2009-11-03 11:47:12 -0800 | [diff] [blame] | 1166 | fc_lport_state_enter(lport, state); |
Chris Leech | c9866a5 | 2009-11-03 11:47:01 -0800 | [diff] [blame] | 1167 | |
Chris Leech | c914f7d | 2009-11-03 11:47:12 -0800 | [diff] [blame] | 1168 | switch (state) { |
| 1169 | case LPORT_ST_RNN_ID: |
| 1170 | cmd = FC_NS_RNN_ID; |
| 1171 | size += sizeof(struct fc_ns_rn_id); |
| 1172 | break; |
| 1173 | case LPORT_ST_RSNN_NN: |
| 1174 | len = strnlen(fc_host_symbolic_name(lport->host), 255); |
| 1175 | /* if there is no symbolic name, skip to RFT_ID */ |
| 1176 | if (!len) |
| 1177 | return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID); |
| 1178 | cmd = FC_NS_RSNN_NN; |
| 1179 | size += sizeof(struct fc_ns_rsnn) + len; |
| 1180 | break; |
| 1181 | case LPORT_ST_RSPN_ID: |
| 1182 | len = strnlen(fc_host_symbolic_name(lport->host), 255); |
| 1183 | /* if there is no symbolic name, skip to RFT_ID */ |
| 1184 | if (!len) |
| 1185 | return fc_lport_enter_ns(lport, LPORT_ST_RFT_ID); |
| 1186 | cmd = FC_NS_RSPN_ID; |
| 1187 | size += sizeof(struct fc_ns_rspn) + len; |
| 1188 | break; |
| 1189 | case LPORT_ST_RFT_ID: |
| 1190 | cmd = FC_NS_RFT_ID; |
| 1191 | size += sizeof(struct fc_ns_rft); |
| 1192 | break; |
| 1193 | default: |
| 1194 | fc_lport_error(lport, NULL); |
| 1195 | return; |
| 1196 | } |
| 1197 | |
| 1198 | fp = fc_frame_alloc(lport, size); |
Chris Leech | c9866a5 | 2009-11-03 11:47:01 -0800 | [diff] [blame] | 1199 | if (!fp) { |
| 1200 | fc_lport_error(lport, fp); |
| 1201 | return; |
| 1202 | } |
| 1203 | |
Chris Leech | c914f7d | 2009-11-03 11:47:12 -0800 | [diff] [blame] | 1204 | if (!lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, cmd, |
Chris Leech | 7cccc15 | 2009-11-03 11:47:07 -0800 | [diff] [blame] | 1205 | fc_lport_ns_resp, |
Chris Leech | c9c7bd7 | 2009-11-03 11:46:51 -0800 | [diff] [blame] | 1206 | lport, lport->e_d_tov)) |
| 1207 | fc_lport_error(lport, fp); |
| 1208 | } |
| 1209 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1210 | static struct fc_rport_operations fc_lport_rport_ops = { |
| 1211 | .event_callback = fc_lport_rport_callback, |
| 1212 | }; |
| 1213 | |
| 1214 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 1215 | * fc_rport_enter_dns() - Create a rport to the name server |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1216 | * @lport: Fibre Channel local port requesting a rport for the name server |
| 1217 | * |
| 1218 | * Locking Note: The lport lock is expected to be held before calling |
| 1219 | * this routine. |
| 1220 | */ |
| 1221 | static void fc_lport_enter_dns(struct fc_lport *lport) |
| 1222 | { |
Joe Eykholt | ab28f1f | 2009-08-25 14:00:34 -0700 | [diff] [blame] | 1223 | struct fc_rport_priv *rdata; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1224 | |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 1225 | FC_LPORT_DBG(lport, "Entered DNS state from %s state\n", |
| 1226 | fc_lport_state(lport)); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1227 | |
| 1228 | fc_lport_state_enter(lport, LPORT_ST_DNS); |
| 1229 | |
Joe Eykholt | 48f0090 | 2009-08-25 14:01:50 -0700 | [diff] [blame] | 1230 | mutex_lock(&lport->disc.disc_mutex); |
Robert Love | 9737e6a | 2009-08-25 14:02:59 -0700 | [diff] [blame] | 1231 | rdata = lport->tt.rport_create(lport, FC_FID_DIR_SERV); |
Joe Eykholt | 48f0090 | 2009-08-25 14:01:50 -0700 | [diff] [blame] | 1232 | mutex_unlock(&lport->disc.disc_mutex); |
Joe Eykholt | 9fb9d32 | 2009-08-25 14:00:50 -0700 | [diff] [blame] | 1233 | if (!rdata) |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1234 | goto err; |
| 1235 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1236 | rdata->ops = &fc_lport_rport_ops; |
Joe Eykholt | 9fb9d32 | 2009-08-25 14:00:50 -0700 | [diff] [blame] | 1237 | lport->tt.rport_login(rdata); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1238 | return; |
| 1239 | |
| 1240 | err: |
| 1241 | fc_lport_error(lport, NULL); |
| 1242 | } |
| 1243 | |
| 1244 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 1245 | * fc_lport_timeout() - Handler for the retry_work timer. |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1246 | * @work: The work struct of the fc_lport |
| 1247 | */ |
| 1248 | static void fc_lport_timeout(struct work_struct *work) |
| 1249 | { |
| 1250 | struct fc_lport *lport = |
| 1251 | container_of(work, struct fc_lport, |
| 1252 | retry_work.work); |
| 1253 | |
| 1254 | mutex_lock(&lport->lp_mutex); |
| 1255 | |
| 1256 | switch (lport->state) { |
Joe Eykholt | b1d9fd5 | 2009-07-29 17:04:22 -0700 | [diff] [blame] | 1257 | case LPORT_ST_DISABLED: |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1258 | WARN_ON(1); |
| 1259 | break; |
Joe Eykholt | 22655ac | 2009-10-21 16:27:22 -0700 | [diff] [blame] | 1260 | case LPORT_ST_READY: |
| 1261 | WARN_ON(1); |
| 1262 | break; |
| 1263 | case LPORT_ST_RESET: |
| 1264 | break; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1265 | case LPORT_ST_FLOGI: |
| 1266 | fc_lport_enter_flogi(lport); |
| 1267 | break; |
| 1268 | case LPORT_ST_DNS: |
| 1269 | fc_lport_enter_dns(lport); |
| 1270 | break; |
Chris Leech | c9c7bd7 | 2009-11-03 11:46:51 -0800 | [diff] [blame] | 1271 | case LPORT_ST_RNN_ID: |
Chris Leech | 5baa17c | 2009-11-03 11:46:56 -0800 | [diff] [blame] | 1272 | case LPORT_ST_RSNN_NN: |
Chris Leech | c9866a5 | 2009-11-03 11:47:01 -0800 | [diff] [blame] | 1273 | case LPORT_ST_RSPN_ID: |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1274 | case LPORT_ST_RFT_ID: |
Chris Leech | c914f7d | 2009-11-03 11:47:12 -0800 | [diff] [blame] | 1275 | fc_lport_enter_ns(lport, lport->state); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1276 | break; |
| 1277 | case LPORT_ST_SCR: |
| 1278 | fc_lport_enter_scr(lport); |
| 1279 | break; |
| 1280 | case LPORT_ST_LOGO: |
| 1281 | fc_lport_enter_logo(lport); |
| 1282 | break; |
| 1283 | } |
| 1284 | |
| 1285 | mutex_unlock(&lport->lp_mutex); |
| 1286 | } |
| 1287 | |
| 1288 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 1289 | * fc_lport_logo_resp() - Handle response to LOGO request |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1290 | * @sp: current sequence in LOGO exchange |
| 1291 | * @fp: response frame |
| 1292 | * @lp_arg: Fibre Channel lport port instance that sent the LOGO request |
| 1293 | * |
| 1294 | * Locking Note: This function will be called without the lport lock |
| 1295 | * held, but it will lock, call an _enter_* function or fc_lport_error |
| 1296 | * and then unlock the lport. |
| 1297 | */ |
Chris Leech | 11b5618 | 2009-11-03 11:46:29 -0800 | [diff] [blame] | 1298 | void fc_lport_logo_resp(struct fc_seq *sp, struct fc_frame *fp, |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1299 | void *lp_arg) |
| 1300 | { |
| 1301 | struct fc_lport *lport = lp_arg; |
| 1302 | u8 op; |
| 1303 | |
Joe Eykholt | f657d29 | 2009-08-25 14:03:21 -0700 | [diff] [blame] | 1304 | FC_LPORT_DBG(lport, "Received a LOGO %s\n", fc_els_resp_type(fp)); |
| 1305 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1306 | if (fp == ERR_PTR(-FC_EX_CLOSED)) |
| 1307 | return; |
| 1308 | |
| 1309 | mutex_lock(&lport->lp_mutex); |
| 1310 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1311 | if (lport->state != LPORT_ST_LOGO) { |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 1312 | FC_LPORT_DBG(lport, "Received a LOGO response, but in state " |
| 1313 | "%s\n", fc_lport_state(lport)); |
Abhijeet Joglekar | 76f6804 | 2009-04-21 16:26:58 -0700 | [diff] [blame] | 1314 | if (IS_ERR(fp)) |
| 1315 | goto err; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1316 | goto out; |
| 1317 | } |
| 1318 | |
Abhijeet Joglekar | 76f6804 | 2009-04-21 16:26:58 -0700 | [diff] [blame] | 1319 | if (IS_ERR(fp)) { |
| 1320 | fc_lport_error(lport, fp); |
| 1321 | goto err; |
| 1322 | } |
| 1323 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1324 | op = fc_frame_payload_op(fp); |
| 1325 | if (op == ELS_LS_ACC) |
Joe Eykholt | 1190d92 | 2009-07-29 17:04:27 -0700 | [diff] [blame] | 1326 | fc_lport_enter_disabled(lport); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1327 | else |
| 1328 | fc_lport_error(lport, fp); |
| 1329 | |
| 1330 | out: |
| 1331 | fc_frame_free(fp); |
| 1332 | err: |
| 1333 | mutex_unlock(&lport->lp_mutex); |
| 1334 | } |
Chris Leech | 11b5618 | 2009-11-03 11:46:29 -0800 | [diff] [blame] | 1335 | EXPORT_SYMBOL(fc_lport_logo_resp); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1336 | |
| 1337 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 1338 | * fc_rport_enter_logo() - Logout of the fabric |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1339 | * @lport: Fibre Channel local port to be logged out |
| 1340 | * |
| 1341 | * Locking Note: The lport lock is expected to be held before calling |
| 1342 | * this routine. |
| 1343 | */ |
| 1344 | static void fc_lport_enter_logo(struct fc_lport *lport) |
| 1345 | { |
| 1346 | struct fc_frame *fp; |
| 1347 | struct fc_els_logo *logo; |
| 1348 | |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 1349 | FC_LPORT_DBG(lport, "Entered LOGO state from %s state\n", |
| 1350 | fc_lport_state(lport)); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1351 | |
| 1352 | fc_lport_state_enter(lport, LPORT_ST_LOGO); |
Chris Leech | 8faecdd | 2009-11-03 11:46:19 -0800 | [diff] [blame] | 1353 | fc_vports_linkchange(lport); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1354 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1355 | fp = fc_frame_alloc(lport, sizeof(*logo)); |
| 1356 | if (!fp) { |
| 1357 | fc_lport_error(lport, fp); |
| 1358 | return; |
| 1359 | } |
| 1360 | |
Joe Eykholt | a46f327 | 2009-08-25 14:00:55 -0700 | [diff] [blame] | 1361 | if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, ELS_LOGO, |
| 1362 | fc_lport_logo_resp, lport, lport->e_d_tov)) |
Chris Leech | 8f550f9 | 2009-10-21 16:28:09 -0700 | [diff] [blame] | 1363 | fc_lport_error(lport, NULL); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 1367 | * fc_lport_flogi_resp() - Handle response to FLOGI request |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1368 | * @sp: current sequence in FLOGI exchange |
| 1369 | * @fp: response frame |
| 1370 | * @lp_arg: Fibre Channel lport port instance that sent the FLOGI request |
| 1371 | * |
| 1372 | * Locking Note: This function will be called without the lport lock |
| 1373 | * held, but it will lock, call an _enter_* function or fc_lport_error |
| 1374 | * and then unlock the lport. |
| 1375 | */ |
Chris Leech | 11b5618 | 2009-11-03 11:46:29 -0800 | [diff] [blame] | 1376 | void fc_lport_flogi_resp(struct fc_seq *sp, struct fc_frame *fp, |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1377 | void *lp_arg) |
| 1378 | { |
| 1379 | struct fc_lport *lport = lp_arg; |
| 1380 | struct fc_frame_header *fh; |
| 1381 | struct fc_els_flogi *flp; |
| 1382 | u32 did; |
| 1383 | u16 csp_flags; |
| 1384 | unsigned int r_a_tov; |
| 1385 | unsigned int e_d_tov; |
| 1386 | u16 mfs; |
| 1387 | |
Joe Eykholt | f657d29 | 2009-08-25 14:03:21 -0700 | [diff] [blame] | 1388 | FC_LPORT_DBG(lport, "Received a FLOGI %s\n", fc_els_resp_type(fp)); |
| 1389 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1390 | if (fp == ERR_PTR(-FC_EX_CLOSED)) |
| 1391 | return; |
| 1392 | |
| 1393 | mutex_lock(&lport->lp_mutex); |
| 1394 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1395 | if (lport->state != LPORT_ST_FLOGI) { |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 1396 | FC_LPORT_DBG(lport, "Received a FLOGI response, but in state " |
| 1397 | "%s\n", fc_lport_state(lport)); |
Abhijeet Joglekar | 76f6804 | 2009-04-21 16:26:58 -0700 | [diff] [blame] | 1398 | if (IS_ERR(fp)) |
| 1399 | goto err; |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1400 | goto out; |
| 1401 | } |
| 1402 | |
Abhijeet Joglekar | 76f6804 | 2009-04-21 16:26:58 -0700 | [diff] [blame] | 1403 | if (IS_ERR(fp)) { |
| 1404 | fc_lport_error(lport, fp); |
| 1405 | goto err; |
| 1406 | } |
| 1407 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1408 | fh = fc_frame_header_get(fp); |
| 1409 | did = ntoh24(fh->fh_d_id); |
| 1410 | if (fc_frame_payload_op(fp) == ELS_LS_ACC && did != 0) { |
| 1411 | |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 1412 | printk(KERN_INFO "libfc: Assigned FID (%6x) in FLOGI response\n", |
| 1413 | did); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1414 | fc_host_port_id(lport->host) = did; |
| 1415 | |
| 1416 | flp = fc_frame_payload_get(fp, sizeof(*flp)); |
| 1417 | if (flp) { |
| 1418 | mfs = ntohs(flp->fl_csp.sp_bb_data) & |
| 1419 | FC_SP_BB_DATA_MASK; |
| 1420 | if (mfs >= FC_SP_MIN_MAX_PAYLOAD && |
| 1421 | mfs < lport->mfs) |
| 1422 | lport->mfs = mfs; |
| 1423 | csp_flags = ntohs(flp->fl_csp.sp_features); |
| 1424 | r_a_tov = ntohl(flp->fl_csp.sp_r_a_tov); |
| 1425 | e_d_tov = ntohl(flp->fl_csp.sp_e_d_tov); |
| 1426 | if (csp_flags & FC_SP_FT_EDTR) |
| 1427 | e_d_tov /= 1000000; |
Chris Leech | db36c06 | 2009-11-03 11:46:24 -0800 | [diff] [blame] | 1428 | |
| 1429 | lport->npiv_enabled = !!(csp_flags & FC_SP_FT_NPIV_ACC); |
| 1430 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1431 | if ((csp_flags & FC_SP_FT_FPORT) == 0) { |
| 1432 | if (e_d_tov > lport->e_d_tov) |
| 1433 | lport->e_d_tov = e_d_tov; |
| 1434 | lport->r_a_tov = 2 * e_d_tov; |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 1435 | printk(KERN_INFO "libfc: Port (%6x) entered " |
| 1436 | "point to point mode\n", did); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1437 | fc_lport_ptp_setup(lport, ntoh24(fh->fh_s_id), |
| 1438 | get_unaligned_be64( |
| 1439 | &flp->fl_wwpn), |
| 1440 | get_unaligned_be64( |
| 1441 | &flp->fl_wwnn)); |
| 1442 | } else { |
| 1443 | lport->e_d_tov = e_d_tov; |
| 1444 | lport->r_a_tov = r_a_tov; |
| 1445 | fc_host_fabric_name(lport->host) = |
| 1446 | get_unaligned_be64(&flp->fl_wwnn); |
| 1447 | fc_lport_enter_dns(lport); |
| 1448 | } |
| 1449 | } |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1450 | } else { |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 1451 | FC_LPORT_DBG(lport, "Bad FLOGI response\n"); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | out: |
| 1455 | fc_frame_free(fp); |
| 1456 | err: |
| 1457 | mutex_unlock(&lport->lp_mutex); |
| 1458 | } |
Chris Leech | 11b5618 | 2009-11-03 11:46:29 -0800 | [diff] [blame] | 1459 | EXPORT_SYMBOL(fc_lport_flogi_resp); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1460 | |
| 1461 | /** |
Robert Love | 34f42a0 | 2009-02-27 10:55:45 -0800 | [diff] [blame] | 1462 | * fc_rport_enter_flogi() - Send a FLOGI request to the fabric manager |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1463 | * @lport: Fibre Channel local port to be logged in to the fabric |
| 1464 | * |
| 1465 | * Locking Note: The lport lock is expected to be held before calling |
| 1466 | * this routine. |
| 1467 | */ |
| 1468 | void fc_lport_enter_flogi(struct fc_lport *lport) |
| 1469 | { |
| 1470 | struct fc_frame *fp; |
| 1471 | |
Robert Love | 7414705 | 2009-06-10 15:31:10 -0700 | [diff] [blame] | 1472 | FC_LPORT_DBG(lport, "Entered FLOGI state from %s state\n", |
| 1473 | fc_lport_state(lport)); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1474 | |
| 1475 | fc_lport_state_enter(lport, LPORT_ST_FLOGI); |
| 1476 | |
| 1477 | fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi)); |
| 1478 | if (!fp) |
| 1479 | return fc_lport_error(lport, fp); |
| 1480 | |
Chris Leech | db36c06 | 2009-11-03 11:46:24 -0800 | [diff] [blame] | 1481 | if (!lport->tt.elsct_send(lport, FC_FID_FLOGI, fp, |
| 1482 | lport->vport ? ELS_FDISC : ELS_FLOGI, |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1483 | fc_lport_flogi_resp, lport, lport->e_d_tov)) |
Chris Leech | 8f550f9 | 2009-10-21 16:28:09 -0700 | [diff] [blame] | 1484 | fc_lport_error(lport, NULL); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | /* Configure a fc_lport */ |
| 1488 | int fc_lport_config(struct fc_lport *lport) |
| 1489 | { |
| 1490 | INIT_DELAYED_WORK(&lport->retry_work, fc_lport_timeout); |
| 1491 | mutex_init(&lport->lp_mutex); |
| 1492 | |
Joe Eykholt | b1d9fd5 | 2009-07-29 17:04:22 -0700 | [diff] [blame] | 1493 | fc_lport_state_enter(lport, LPORT_ST_DISABLED); |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1494 | |
| 1495 | fc_lport_add_fc4_type(lport, FC_TYPE_FCP); |
| 1496 | fc_lport_add_fc4_type(lport, FC_TYPE_CT); |
| 1497 | |
| 1498 | return 0; |
| 1499 | } |
| 1500 | EXPORT_SYMBOL(fc_lport_config); |
| 1501 | |
| 1502 | int fc_lport_init(struct fc_lport *lport) |
| 1503 | { |
| 1504 | if (!lport->tt.lport_recv) |
| 1505 | lport->tt.lport_recv = fc_lport_recv_req; |
| 1506 | |
| 1507 | if (!lport->tt.lport_reset) |
| 1508 | lport->tt.lport_reset = fc_lport_reset; |
| 1509 | |
| 1510 | fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT; |
| 1511 | fc_host_node_name(lport->host) = lport->wwnn; |
| 1512 | fc_host_port_name(lport->host) = lport->wwpn; |
| 1513 | fc_host_supported_classes(lport->host) = FC_COS_CLASS3; |
| 1514 | memset(fc_host_supported_fc4s(lport->host), 0, |
| 1515 | sizeof(fc_host_supported_fc4s(lport->host))); |
| 1516 | fc_host_supported_fc4s(lport->host)[2] = 1; |
| 1517 | fc_host_supported_fc4s(lport->host)[7] = 1; |
| 1518 | |
| 1519 | /* This value is also unchanging */ |
| 1520 | memset(fc_host_active_fc4s(lport->host), 0, |
| 1521 | sizeof(fc_host_active_fc4s(lport->host))); |
| 1522 | fc_host_active_fc4s(lport->host)[2] = 1; |
| 1523 | fc_host_active_fc4s(lport->host)[7] = 1; |
| 1524 | fc_host_maxframe_size(lport->host) = lport->mfs; |
| 1525 | fc_host_supported_speeds(lport->host) = 0; |
| 1526 | if (lport->link_supported_speeds & FC_PORTSPEED_1GBIT) |
| 1527 | fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_1GBIT; |
| 1528 | if (lport->link_supported_speeds & FC_PORTSPEED_10GBIT) |
| 1529 | fc_host_supported_speeds(lport->host) |= FC_PORTSPEED_10GBIT; |
| 1530 | |
Robert Love | 42e9a92 | 2008-12-09 15:10:17 -0800 | [diff] [blame] | 1531 | return 0; |
| 1532 | } |
| 1533 | EXPORT_SYMBOL(fc_lport_init); |
Steve Ma | a51ab39 | 2009-11-03 11:47:34 -0800 | [diff] [blame^] | 1534 | |
| 1535 | /** |
| 1536 | * fc_lport_bsg_resp() - The common response handler for fc pass-thru requests |
| 1537 | * @sp: current sequence in the fc pass-thru request exchange |
| 1538 | * @fp: received response frame |
| 1539 | * @info_arg: pointer to struct fc_bsg_info |
| 1540 | */ |
| 1541 | static void fc_lport_bsg_resp(struct fc_seq *sp, struct fc_frame *fp, |
| 1542 | void *info_arg) |
| 1543 | { |
| 1544 | struct fc_bsg_info *info = info_arg; |
| 1545 | struct fc_bsg_job *job = info->job; |
| 1546 | struct fc_lport *lport = info->lport; |
| 1547 | struct fc_frame_header *fh; |
| 1548 | size_t len; |
| 1549 | void *buf; |
| 1550 | |
| 1551 | if (IS_ERR(fp)) { |
| 1552 | job->reply->result = (PTR_ERR(fp) == -FC_EX_CLOSED) ? |
| 1553 | -ECONNABORTED : -ETIMEDOUT; |
| 1554 | job->reply_len = sizeof(uint32_t); |
| 1555 | job->state_flags |= FC_RQST_STATE_DONE; |
| 1556 | job->job_done(job); |
| 1557 | kfree(info); |
| 1558 | return; |
| 1559 | } |
| 1560 | |
| 1561 | mutex_lock(&lport->lp_mutex); |
| 1562 | fh = fc_frame_header_get(fp); |
| 1563 | len = fr_len(fp) - sizeof(*fh); |
| 1564 | buf = fc_frame_payload_get(fp, 0); |
| 1565 | |
| 1566 | if (fr_sof(fp) == FC_SOF_I3 && !ntohs(fh->fh_seq_cnt)) { |
| 1567 | /* Get the response code from the first frame payload */ |
| 1568 | unsigned short cmd = (info->rsp_code == FC_FS_ACC) ? |
| 1569 | ntohs(((struct fc_ct_hdr *)buf)->ct_cmd) : |
| 1570 | (unsigned short)fc_frame_payload_op(fp); |
| 1571 | |
| 1572 | /* Save the reply status of the job */ |
| 1573 | job->reply->reply_data.ctels_reply.status = |
| 1574 | (cmd == info->rsp_code) ? |
| 1575 | FC_CTELS_STATUS_OK : FC_CTELS_STATUS_REJECT; |
| 1576 | } |
| 1577 | |
| 1578 | job->reply->reply_payload_rcv_len += |
| 1579 | fc_copy_buffer_to_sglist(buf, len, info->sg, &info->nents, |
| 1580 | &info->offset, KM_BIO_SRC_IRQ, NULL); |
| 1581 | |
| 1582 | if (fr_eof(fp) == FC_EOF_T && |
| 1583 | (ntoh24(fh->fh_f_ctl) & (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) == |
| 1584 | (FC_FC_LAST_SEQ | FC_FC_END_SEQ)) { |
| 1585 | if (job->reply->reply_payload_rcv_len > |
| 1586 | job->reply_payload.payload_len) |
| 1587 | job->reply->reply_payload_rcv_len = |
| 1588 | job->reply_payload.payload_len; |
| 1589 | job->reply->result = 0; |
| 1590 | job->state_flags |= FC_RQST_STATE_DONE; |
| 1591 | job->job_done(job); |
| 1592 | kfree(info); |
| 1593 | } |
| 1594 | fc_frame_free(fp); |
| 1595 | mutex_unlock(&lport->lp_mutex); |
| 1596 | } |
| 1597 | |
| 1598 | /** |
| 1599 | * fc_lport_els_request() - Send ELS pass-thru request |
| 1600 | * @job: The bsg fc pass-thru job structure |
| 1601 | * @lport: The local port sending the request |
| 1602 | * @did: The destination port id. |
| 1603 | * |
| 1604 | * Locking Note: The lport lock is expected to be held before calling |
| 1605 | * this routine. |
| 1606 | */ |
| 1607 | static int fc_lport_els_request(struct fc_bsg_job *job, |
| 1608 | struct fc_lport *lport, |
| 1609 | u32 did, u32 tov) |
| 1610 | { |
| 1611 | struct fc_bsg_info *info; |
| 1612 | struct fc_frame *fp; |
| 1613 | struct fc_frame_header *fh; |
| 1614 | char *pp; |
| 1615 | int len; |
| 1616 | |
| 1617 | fp = fc_frame_alloc(lport, sizeof(struct fc_frame_header) + |
| 1618 | job->request_payload.payload_len); |
| 1619 | if (!fp) |
| 1620 | return -ENOMEM; |
| 1621 | |
| 1622 | len = job->request_payload.payload_len; |
| 1623 | pp = fc_frame_payload_get(fp, len); |
| 1624 | |
| 1625 | sg_copy_to_buffer(job->request_payload.sg_list, |
| 1626 | job->request_payload.sg_cnt, |
| 1627 | pp, len); |
| 1628 | |
| 1629 | fh = fc_frame_header_get(fp); |
| 1630 | fh->fh_r_ctl = FC_RCTL_ELS_REQ; |
| 1631 | hton24(fh->fh_d_id, did); |
| 1632 | hton24(fh->fh_s_id, fc_host_port_id(lport->host)); |
| 1633 | fh->fh_type = FC_TYPE_ELS; |
| 1634 | hton24(fh->fh_f_ctl, FC_FC_FIRST_SEQ | |
| 1635 | FC_FC_END_SEQ | FC_FC_SEQ_INIT); |
| 1636 | fh->fh_cs_ctl = 0; |
| 1637 | fh->fh_df_ctl = 0; |
| 1638 | fh->fh_parm_offset = 0; |
| 1639 | |
| 1640 | info = kzalloc(sizeof(struct fc_bsg_info), GFP_KERNEL); |
| 1641 | if (!info) { |
| 1642 | fc_frame_free(fp); |
| 1643 | return -ENOMEM; |
| 1644 | } |
| 1645 | |
| 1646 | info->job = job; |
| 1647 | info->lport = lport; |
| 1648 | info->rsp_code = ELS_LS_ACC; |
| 1649 | info->nents = job->reply_payload.sg_cnt; |
| 1650 | info->sg = job->reply_payload.sg_list; |
| 1651 | |
| 1652 | if (!lport->tt.exch_seq_send(lport, fp, fc_lport_bsg_resp, |
| 1653 | NULL, info, tov)) |
| 1654 | return -ECOMM; |
| 1655 | return 0; |
| 1656 | } |
| 1657 | |
| 1658 | /** |
| 1659 | * fc_lport_ct_request() - Send CT pass-thru request |
| 1660 | * @job: The bsg fc pass-thru job structure |
| 1661 | * @lport: The local port sending the request |
| 1662 | * @did: The destination FC-ID |
| 1663 | * @tov: The time to wait for a response |
| 1664 | * |
| 1665 | * Locking Note: The lport lock is expected to be held before calling |
| 1666 | * this routine. |
| 1667 | */ |
| 1668 | static int fc_lport_ct_request(struct fc_bsg_job *job, |
| 1669 | struct fc_lport *lport, u32 did, u32 tov) |
| 1670 | { |
| 1671 | struct fc_bsg_info *info; |
| 1672 | struct fc_frame *fp; |
| 1673 | struct fc_frame_header *fh; |
| 1674 | struct fc_ct_req *ct; |
| 1675 | size_t len; |
| 1676 | |
| 1677 | fp = fc_frame_alloc(lport, sizeof(struct fc_ct_hdr) + |
| 1678 | job->request_payload.payload_len); |
| 1679 | if (!fp) |
| 1680 | return -ENOMEM; |
| 1681 | |
| 1682 | len = job->request_payload.payload_len; |
| 1683 | ct = fc_frame_payload_get(fp, len); |
| 1684 | |
| 1685 | sg_copy_to_buffer(job->request_payload.sg_list, |
| 1686 | job->request_payload.sg_cnt, |
| 1687 | ct, len); |
| 1688 | |
| 1689 | fh = fc_frame_header_get(fp); |
| 1690 | fh->fh_r_ctl = FC_RCTL_DD_UNSOL_CTL; |
| 1691 | hton24(fh->fh_d_id, did); |
| 1692 | hton24(fh->fh_s_id, fc_host_port_id(lport->host)); |
| 1693 | fh->fh_type = FC_TYPE_CT; |
| 1694 | hton24(fh->fh_f_ctl, FC_FC_FIRST_SEQ | |
| 1695 | FC_FC_END_SEQ | FC_FC_SEQ_INIT); |
| 1696 | fh->fh_cs_ctl = 0; |
| 1697 | fh->fh_df_ctl = 0; |
| 1698 | fh->fh_parm_offset = 0; |
| 1699 | |
| 1700 | info = kzalloc(sizeof(struct fc_bsg_info), GFP_KERNEL); |
| 1701 | if (!info) { |
| 1702 | fc_frame_free(fp); |
| 1703 | return -ENOMEM; |
| 1704 | } |
| 1705 | |
| 1706 | info->job = job; |
| 1707 | info->lport = lport; |
| 1708 | info->rsp_code = FC_FS_ACC; |
| 1709 | info->nents = job->reply_payload.sg_cnt; |
| 1710 | info->sg = job->reply_payload.sg_list; |
| 1711 | |
| 1712 | if (!lport->tt.exch_seq_send(lport, fp, fc_lport_bsg_resp, |
| 1713 | NULL, info, tov)) |
| 1714 | return -ECOMM; |
| 1715 | return 0; |
| 1716 | } |
| 1717 | |
| 1718 | /** |
| 1719 | * fc_lport_bsg_request() - The common entry point for sending |
| 1720 | * fc pass-thru requests |
| 1721 | * @job: The fc pass-thru job structure |
| 1722 | */ |
| 1723 | int fc_lport_bsg_request(struct fc_bsg_job *job) |
| 1724 | { |
| 1725 | struct request *rsp = job->req->next_rq; |
| 1726 | struct Scsi_Host *shost = job->shost; |
| 1727 | struct fc_lport *lport = shost_priv(shost); |
| 1728 | struct fc_rport *rport; |
| 1729 | struct fc_rport_priv *rdata; |
| 1730 | int rc = -EINVAL; |
| 1731 | u32 did; |
| 1732 | |
| 1733 | job->reply->reply_payload_rcv_len = 0; |
| 1734 | rsp->resid_len = job->reply_payload.payload_len; |
| 1735 | |
| 1736 | mutex_lock(&lport->lp_mutex); |
| 1737 | |
| 1738 | switch (job->request->msgcode) { |
| 1739 | case FC_BSG_RPT_ELS: |
| 1740 | rport = job->rport; |
| 1741 | if (!rport) |
| 1742 | break; |
| 1743 | |
| 1744 | rdata = rport->dd_data; |
| 1745 | rc = fc_lport_els_request(job, lport, rport->port_id, |
| 1746 | rdata->e_d_tov); |
| 1747 | break; |
| 1748 | |
| 1749 | case FC_BSG_RPT_CT: |
| 1750 | rport = job->rport; |
| 1751 | if (!rport) |
| 1752 | break; |
| 1753 | |
| 1754 | rdata = rport->dd_data; |
| 1755 | rc = fc_lport_ct_request(job, lport, rport->port_id, |
| 1756 | rdata->e_d_tov); |
| 1757 | break; |
| 1758 | |
| 1759 | case FC_BSG_HST_CT: |
| 1760 | did = ntoh24(job->request->rqst_data.h_ct.port_id); |
| 1761 | if (did == FC_FID_DIR_SERV) |
| 1762 | rdata = lport->dns_rp; |
| 1763 | else |
| 1764 | rdata = lport->tt.rport_lookup(lport, did); |
| 1765 | |
| 1766 | if (!rdata) |
| 1767 | break; |
| 1768 | |
| 1769 | rc = fc_lport_ct_request(job, lport, did, rdata->e_d_tov); |
| 1770 | break; |
| 1771 | |
| 1772 | case FC_BSG_HST_ELS_NOLOGIN: |
| 1773 | did = ntoh24(job->request->rqst_data.h_els.port_id); |
| 1774 | rc = fc_lport_els_request(job, lport, did, lport->e_d_tov); |
| 1775 | break; |
| 1776 | } |
| 1777 | |
| 1778 | mutex_unlock(&lport->lp_mutex); |
| 1779 | return rc; |
| 1780 | } |
| 1781 | EXPORT_SYMBOL(fc_lport_bsg_request); |