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