xref: /linux/drivers/scsi/csiostor/csio_lnode.c (revision 7a6d3b98469d7891a86fc9a70741be5d77548292)
1 /*
2  * This file is part of the Chelsio FCoE driver for Linux.
3  *
4  * Copyright (c) 2008-2012 Chelsio Communications, Inc. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 
35 #include <linux/kernel.h>
36 #include <linux/delay.h>
37 #include <linux/slab.h>
38 #include <linux/utsname.h>
39 #include <scsi/scsi_device.h>
40 #include <scsi/scsi_transport_fc.h>
41 #include <linux/unaligned.h>
42 #include <scsi/fc/fc_els.h>
43 #include <scsi/fc/fc_fs.h>
44 #include <scsi/fc/fc_gs.h>
45 #include <scsi/fc/fc_ms.h>
46 
47 #include "csio_hw.h"
48 #include "csio_mb.h"
49 #include "csio_lnode.h"
50 #include "csio_rnode.h"
51 
52 int csio_fcoe_rnodes = 1024;
53 int csio_fdmi_enable = 1;
54 
55 #define PORT_ID_PTR(_x)         ((uint8_t *)(&_x) + 1)
56 
57 /* Lnode SM declarations */
58 static void csio_lns_uninit(struct csio_lnode *, enum csio_ln_ev);
59 static void csio_lns_online(struct csio_lnode *, enum csio_ln_ev);
60 static void csio_lns_ready(struct csio_lnode *, enum csio_ln_ev);
61 static void csio_lns_offline(struct csio_lnode *, enum csio_ln_ev);
62 
63 static int csio_ln_mgmt_submit_req(struct csio_ioreq *,
64 		void (*io_cbfn) (struct csio_hw *, struct csio_ioreq *),
65 		enum fcoe_cmn_type, struct csio_dma_buf *, uint32_t);
66 
67 /* LN event mapping */
68 static enum csio_ln_ev fwevt_to_lnevt[] = {
69 	CSIO_LNE_NONE,		/* None */
70 	CSIO_LNE_NONE,		/* PLOGI_ACC_RCVD  */
71 	CSIO_LNE_NONE,		/* PLOGI_RJT_RCVD  */
72 	CSIO_LNE_NONE,		/* PLOGI_RCVD	   */
73 	CSIO_LNE_NONE,		/* PLOGO_RCVD	   */
74 	CSIO_LNE_NONE,		/* PRLI_ACC_RCVD   */
75 	CSIO_LNE_NONE,		/* PRLI_RJT_RCVD   */
76 	CSIO_LNE_NONE,		/* PRLI_RCVD	   */
77 	CSIO_LNE_NONE,		/* PRLO_RCVD	   */
78 	CSIO_LNE_NONE,		/* NPORT_ID_CHGD   */
79 	CSIO_LNE_LOGO,		/* FLOGO_RCVD	   */
80 	CSIO_LNE_LOGO,		/* CLR_VIRT_LNK_RCVD */
81 	CSIO_LNE_FAB_INIT_DONE,/* FLOGI_ACC_RCVD   */
82 	CSIO_LNE_NONE,		/* FLOGI_RJT_RCVD   */
83 	CSIO_LNE_FAB_INIT_DONE,/* FDISC_ACC_RCVD   */
84 	CSIO_LNE_NONE,		/* FDISC_RJT_RCVD   */
85 	CSIO_LNE_NONE,		/* FLOGI_TMO_MAX_RETRY */
86 	CSIO_LNE_NONE,		/* IMPL_LOGO_ADISC_ACC */
87 	CSIO_LNE_NONE,		/* IMPL_LOGO_ADISC_RJT */
88 	CSIO_LNE_NONE,		/* IMPL_LOGO_ADISC_CNFLT */
89 	CSIO_LNE_NONE,		/* PRLI_TMO		*/
90 	CSIO_LNE_NONE,		/* ADISC_TMO		*/
91 	CSIO_LNE_NONE,		/* RSCN_DEV_LOST */
92 	CSIO_LNE_NONE,		/* SCR_ACC_RCVD */
93 	CSIO_LNE_NONE,		/* ADISC_RJT_RCVD */
94 	CSIO_LNE_NONE,		/* LOGO_SNT */
95 	CSIO_LNE_NONE,		/* PROTO_ERR_IMPL_LOGO */
96 };
97 
98 #define CSIO_FWE_TO_LNE(_evt)	((_evt > PROTO_ERR_IMPL_LOGO) ?		\
99 						CSIO_LNE_NONE :	\
100 						fwevt_to_lnevt[_evt])
101 
102 #define csio_ct_rsp(cp)		(((struct fc_ct_hdr *)cp)->ct_cmd)
103 #define csio_ct_reason(cp)	(((struct fc_ct_hdr *)cp)->ct_reason)
104 #define csio_ct_expl(cp)	(((struct fc_ct_hdr *)cp)->ct_explan)
105 #define csio_ct_get_pld(cp)	((void *)(((uint8_t *)cp) + FC_CT_HDR_LEN))
106 
107 /*
108  * csio_ln_match_by_portid - lookup lnode using given portid.
109  * @hw: HW module
110  * @portid: port-id.
111  *
112  * If found, returns lnode matching given portid otherwise returns NULL.
113  */
114 static struct csio_lnode *
115 csio_ln_lookup_by_portid(struct csio_hw *hw, uint8_t portid)
116 {
117 	struct csio_lnode *ln;
118 	struct list_head *tmp;
119 
120 	/* Match siblings lnode with portid */
121 	list_for_each(tmp, &hw->sln_head) {
122 		ln = (struct csio_lnode *) tmp;
123 		if (ln->portid == portid)
124 			return ln;
125 	}
126 
127 	return NULL;
128 }
129 
130 /*
131  * csio_ln_lookup_by_vnpi - Lookup lnode using given vnp id.
132  * @hw - HW module
133  * @vnpi - vnp index.
134  * Returns - If found, returns lnode matching given vnp id
135  * otherwise returns NULL.
136  */
137 static struct csio_lnode *
138 csio_ln_lookup_by_vnpi(struct csio_hw *hw, uint32_t vnp_id)
139 {
140 	struct list_head *tmp1, *tmp2;
141 	struct csio_lnode *sln = NULL, *cln = NULL;
142 
143 	if (list_empty(&hw->sln_head)) {
144 		CSIO_INC_STATS(hw, n_lnlkup_miss);
145 		return NULL;
146 	}
147 	/* Traverse sibling lnodes */
148 	list_for_each(tmp1, &hw->sln_head) {
149 		sln = (struct csio_lnode *) tmp1;
150 
151 		/* Match sibling lnode */
152 		if (sln->vnp_flowid == vnp_id)
153 			return sln;
154 
155 		if (list_empty(&sln->cln_head))
156 			continue;
157 
158 		/* Traverse children lnodes */
159 		list_for_each(tmp2, &sln->cln_head) {
160 			cln = (struct csio_lnode *) tmp2;
161 
162 			if (cln->vnp_flowid == vnp_id)
163 				return cln;
164 		}
165 	}
166 	CSIO_INC_STATS(hw, n_lnlkup_miss);
167 	return NULL;
168 }
169 
170 /**
171  * csio_lnode_lookup_by_wwpn - Lookup lnode using given wwpn.
172  * @hw:		HW module.
173  * @wwpn:	WWPN.
174  *
175  * If found, returns lnode matching given wwpn, returns NULL otherwise.
176  */
177 struct csio_lnode *
178 csio_lnode_lookup_by_wwpn(struct csio_hw *hw, uint8_t *wwpn)
179 {
180 	struct list_head *tmp1, *tmp2;
181 	struct csio_lnode *sln = NULL, *cln = NULL;
182 
183 	if (list_empty(&hw->sln_head)) {
184 		CSIO_INC_STATS(hw, n_lnlkup_miss);
185 		return NULL;
186 	}
187 	/* Traverse sibling lnodes */
188 	list_for_each(tmp1, &hw->sln_head) {
189 		sln = (struct csio_lnode *) tmp1;
190 
191 		/* Match sibling lnode */
192 		if (!memcmp(csio_ln_wwpn(sln), wwpn, 8))
193 			return sln;
194 
195 		if (list_empty(&sln->cln_head))
196 			continue;
197 
198 		/* Traverse children lnodes */
199 		list_for_each(tmp2, &sln->cln_head) {
200 			cln = (struct csio_lnode *) tmp2;
201 
202 			if (!memcmp(csio_ln_wwpn(cln), wwpn, 8))
203 				return cln;
204 		}
205 	}
206 	return NULL;
207 }
208 
209 /* FDMI */
210 static void
211 csio_fill_ct_iu(void *buf, uint8_t type, uint8_t sub_type, uint16_t op)
212 {
213 	struct fc_ct_hdr *cmd = (struct fc_ct_hdr *)buf;
214 	cmd->ct_rev = FC_CT_REV;
215 	cmd->ct_fs_type = type;
216 	cmd->ct_fs_subtype = sub_type;
217 	cmd->ct_cmd = htons(op);
218 }
219 
220 static int
221 csio_hostname(uint8_t *buf, size_t buf_len)
222 {
223 	if (snprintf(buf, buf_len, "%s", init_utsname()->nodename) > 0)
224 		return 0;
225 	return -1;
226 }
227 
228 static int
229 csio_osname(uint8_t *buf, size_t buf_len)
230 {
231 	if (snprintf(buf, buf_len, "%s %s %s",
232 		     init_utsname()->sysname,
233 		     init_utsname()->release,
234 		     init_utsname()->version) > 0)
235 		return 0;
236 
237 	return -1;
238 }
239 
240 static inline void
241 csio_append_attrib(uint8_t **ptr, uint16_t type, void *val, size_t val_len)
242 {
243 	uint16_t len;
244 	struct fc_fdmi_attr_entry *ae = (struct fc_fdmi_attr_entry *)*ptr;
245 
246 	if (WARN_ON(val_len > U16_MAX))
247 		return;
248 
249 	len = val_len;
250 
251 	ae->type = htons(type);
252 	len += 4;		/* includes attribute type and length */
253 	len = (len + 3) & ~3;	/* should be multiple of 4 bytes */
254 	ae->len = htons(len);
255 	memcpy(ae->value, val, val_len);
256 	if (len > val_len)
257 		memset(ae->value + val_len, 0, len - val_len);
258 	*ptr += len;
259 }
260 
261 /*
262  * csio_ln_fdmi_done - FDMI registeration completion
263  * @hw: HW context
264  * @fdmi_req: fdmi request
265  */
266 static void
267 csio_ln_fdmi_done(struct csio_hw *hw, struct csio_ioreq *fdmi_req)
268 {
269 	void *cmd;
270 	struct csio_lnode *ln = fdmi_req->lnode;
271 
272 	if (fdmi_req->wr_status != FW_SUCCESS) {
273 		csio_ln_dbg(ln, "WR error:%x in processing fdmi rpa cmd\n",
274 			    fdmi_req->wr_status);
275 		CSIO_INC_STATS(ln, n_fdmi_err);
276 	}
277 
278 	cmd = fdmi_req->dma_buf.vaddr;
279 	if (ntohs(csio_ct_rsp(cmd)) != FC_FS_ACC) {
280 		csio_ln_dbg(ln, "fdmi rpa cmd rejected reason %x expl %x\n",
281 			    csio_ct_reason(cmd), csio_ct_expl(cmd));
282 	}
283 }
284 
285 /*
286  * csio_ln_fdmi_rhba_cbfn - RHBA completion
287  * @hw: HW context
288  * @fdmi_req: fdmi request
289  */
290 static void
291 csio_ln_fdmi_rhba_cbfn(struct csio_hw *hw, struct csio_ioreq *fdmi_req)
292 {
293 	void *cmd;
294 	uint8_t *pld;
295 	uint32_t len = 0;
296 	__be32 val;
297 	__be16 mfs;
298 	uint32_t numattrs = 0;
299 	struct csio_lnode *ln = fdmi_req->lnode;
300 	struct fs_fdmi_attrs *attrib_blk;
301 	struct fc_fdmi_port_name *port_name;
302 	uint8_t buf[64];
303 	uint8_t *fc4_type;
304 	unsigned long flags;
305 
306 	if (fdmi_req->wr_status != FW_SUCCESS) {
307 		csio_ln_dbg(ln, "WR error:%x in processing fdmi rhba cmd\n",
308 			    fdmi_req->wr_status);
309 		CSIO_INC_STATS(ln, n_fdmi_err);
310 	}
311 
312 	cmd = fdmi_req->dma_buf.vaddr;
313 	if (ntohs(csio_ct_rsp(cmd)) != FC_FS_ACC) {
314 		csio_ln_dbg(ln, "fdmi rhba cmd rejected reason %x expl %x\n",
315 			    csio_ct_reason(cmd), csio_ct_expl(cmd));
316 	}
317 
318 	if (!csio_is_rnode_ready(fdmi_req->rnode)) {
319 		CSIO_INC_STATS(ln, n_fdmi_err);
320 		return;
321 	}
322 
323 	/* Prepare CT hdr for RPA cmd */
324 	memset(cmd, 0, FC_CT_HDR_LEN);
325 	csio_fill_ct_iu(cmd, FC_FST_MGMT, FC_FDMI_SUBTYPE, FC_FDMI_RPA);
326 
327 	/* Prepare RPA payload */
328 	pld = (uint8_t *)csio_ct_get_pld(cmd);
329 	port_name = (struct fc_fdmi_port_name *)pld;
330 	memcpy(&port_name->portname, csio_ln_wwpn(ln), 8);
331 	pld += sizeof(*port_name);
332 
333 	/* Start appending Port attributes */
334 	attrib_blk = (struct fs_fdmi_attrs *)pld;
335 	attrib_blk->numattrs = 0;
336 	len += sizeof(attrib_blk->numattrs);
337 	pld += sizeof(attrib_blk->numattrs);
338 
339 	fc4_type = &buf[0];
340 	memset(fc4_type, 0, FC_FDMI_PORT_ATTR_FC4TYPES_LEN);
341 	fc4_type[2] = 1;
342 	fc4_type[7] = 1;
343 	csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_FC4TYPES,
344 			   fc4_type, FC_FDMI_PORT_ATTR_FC4TYPES_LEN);
345 	numattrs++;
346 	val = htonl(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
347 	csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_SUPPORTEDSPEED,
348 			   &val,
349 			   FC_FDMI_PORT_ATTR_SUPPORTEDSPEED_LEN);
350 	numattrs++;
351 
352 	if (hw->pport[ln->portid].link_speed == FW_PORT_CAP_SPEED_1G)
353 		val = htonl(FC_PORTSPEED_1GBIT);
354 	else if (hw->pport[ln->portid].link_speed == FW_PORT_CAP_SPEED_10G)
355 		val = htonl(FC_PORTSPEED_10GBIT);
356 	else if (hw->pport[ln->portid].link_speed == FW_PORT_CAP32_SPEED_25G)
357 		val = htonl(FC_PORTSPEED_25GBIT);
358 	else if (hw->pport[ln->portid].link_speed == FW_PORT_CAP32_SPEED_40G)
359 		val = htonl(FC_PORTSPEED_40GBIT);
360 	else if (hw->pport[ln->portid].link_speed == FW_PORT_CAP32_SPEED_50G)
361 		val = htonl(FC_PORTSPEED_50GBIT);
362 	else if (hw->pport[ln->portid].link_speed == FW_PORT_CAP32_SPEED_100G)
363 		val = htonl(FC_PORTSPEED_100GBIT);
364 	else
365 		val = htonl(CSIO_HBA_PORTSPEED_UNKNOWN);
366 	csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_CURRENTPORTSPEED,
367 			   &val, FC_FDMI_PORT_ATTR_CURRENTPORTSPEED_LEN);
368 	numattrs++;
369 
370 	mfs = ln->ln_sparm.csp.sp_bb_data;
371 	csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_MAXFRAMESIZE,
372 			   &mfs, sizeof(mfs));
373 	numattrs++;
374 
375 	strcpy(buf, "csiostor");
376 	csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_OSDEVICENAME, buf,
377 			   strlen(buf));
378 	numattrs++;
379 
380 	if (!csio_hostname(buf, sizeof(buf))) {
381 		csio_append_attrib(&pld, FC_FDMI_PORT_ATTR_HOSTNAME,
382 				   buf, strlen(buf));
383 		numattrs++;
384 	}
385 	attrib_blk->numattrs = htonl(numattrs);
386 	len = (uint32_t)(pld - (uint8_t *)cmd);
387 
388 	/* Submit FDMI RPA request */
389 	spin_lock_irqsave(&hw->lock, flags);
390 	if (csio_ln_mgmt_submit_req(fdmi_req, csio_ln_fdmi_done,
391 				FCOE_CT, &fdmi_req->dma_buf, len)) {
392 		CSIO_INC_STATS(ln, n_fdmi_err);
393 		csio_ln_dbg(ln, "Failed to issue fdmi rpa req\n");
394 	}
395 	spin_unlock_irqrestore(&hw->lock, flags);
396 }
397 
398 /*
399  * csio_ln_fdmi_dprt_cbfn - DPRT completion
400  * @hw: HW context
401  * @fdmi_req: fdmi request
402  */
403 static void
404 csio_ln_fdmi_dprt_cbfn(struct csio_hw *hw, struct csio_ioreq *fdmi_req)
405 {
406 	void *cmd;
407 	uint8_t *pld;
408 	uint32_t len = 0;
409 	uint32_t numattrs = 0;
410 	__be32  maxpayload = htonl(65536);
411 	struct fc_fdmi_hba_identifier *hbaid;
412 	struct csio_lnode *ln = fdmi_req->lnode;
413 	struct fc_fdmi_rpl *reg_pl;
414 	struct fs_fdmi_attrs *attrib_blk;
415 	uint8_t buf[64];
416 	unsigned long flags;
417 
418 	if (fdmi_req->wr_status != FW_SUCCESS) {
419 		csio_ln_dbg(ln, "WR error:%x in processing fdmi dprt cmd\n",
420 			    fdmi_req->wr_status);
421 		CSIO_INC_STATS(ln, n_fdmi_err);
422 	}
423 
424 	if (!csio_is_rnode_ready(fdmi_req->rnode)) {
425 		CSIO_INC_STATS(ln, n_fdmi_err);
426 		return;
427 	}
428 	cmd = fdmi_req->dma_buf.vaddr;
429 	if (ntohs(csio_ct_rsp(cmd)) != FC_FS_ACC) {
430 		csio_ln_dbg(ln, "fdmi dprt cmd rejected reason %x expl %x\n",
431 			    csio_ct_reason(cmd), csio_ct_expl(cmd));
432 	}
433 
434 	/* Prepare CT hdr for RHBA cmd */
435 	memset(cmd, 0, FC_CT_HDR_LEN);
436 	csio_fill_ct_iu(cmd, FC_FST_MGMT, FC_FDMI_SUBTYPE, FC_FDMI_RHBA);
437 	len = FC_CT_HDR_LEN;
438 
439 	/* Prepare RHBA payload */
440 	pld = (uint8_t *)csio_ct_get_pld(cmd);
441 	hbaid = (struct fc_fdmi_hba_identifier *)pld;
442 	memcpy(&hbaid->id, csio_ln_wwpn(ln), 8); /* HBA identifer */
443 	pld += sizeof(*hbaid);
444 
445 	/* Register one port per hba */
446 	reg_pl = (struct fc_fdmi_rpl *)pld;
447 	reg_pl->numport = htonl(1);
448 	memcpy(&reg_pl->port[0].portname, csio_ln_wwpn(ln), 8);
449 	pld += sizeof(*reg_pl);
450 
451 	/* Start appending HBA attributes hba */
452 	attrib_blk = (struct fs_fdmi_attrs *)pld;
453 	attrib_blk->numattrs = 0;
454 	len += sizeof(attrib_blk->numattrs);
455 	pld += sizeof(attrib_blk->numattrs);
456 
457 	csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_NODENAME, csio_ln_wwnn(ln),
458 			   FC_FDMI_HBA_ATTR_NODENAME_LEN);
459 	numattrs++;
460 
461 	memset(buf, 0, sizeof(buf));
462 
463 	strcpy(buf, "Chelsio Communications");
464 	csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_MANUFACTURER, buf,
465 			   strlen(buf));
466 	numattrs++;
467 	csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_SERIALNUMBER,
468 			   hw->vpd.sn, sizeof(hw->vpd.sn));
469 	numattrs++;
470 	csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_MODEL, hw->vpd.id,
471 			   sizeof(hw->vpd.id));
472 	numattrs++;
473 	csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_MODELDESCRIPTION,
474 			   hw->model_desc, strlen(hw->model_desc));
475 	numattrs++;
476 	csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_HARDWAREVERSION,
477 			   hw->hw_ver, sizeof(hw->hw_ver));
478 	numattrs++;
479 	csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_FIRMWAREVERSION,
480 			   hw->fwrev_str, strlen(hw->fwrev_str));
481 	numattrs++;
482 
483 	if (!csio_osname(buf, sizeof(buf))) {
484 		csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_OSNAMEVERSION,
485 				   buf, strlen(buf));
486 		numattrs++;
487 	}
488 
489 	csio_append_attrib(&pld, FC_FDMI_HBA_ATTR_MAXCTPAYLOAD,
490 			   &maxpayload, FC_FDMI_HBA_ATTR_MAXCTPAYLOAD_LEN);
491 	len = (uint32_t)(pld - (uint8_t *)cmd);
492 	numattrs++;
493 	attrib_blk->numattrs = htonl(numattrs);
494 
495 	/* Submit FDMI RHBA request */
496 	spin_lock_irqsave(&hw->lock, flags);
497 	if (csio_ln_mgmt_submit_req(fdmi_req, csio_ln_fdmi_rhba_cbfn,
498 				FCOE_CT, &fdmi_req->dma_buf, len)) {
499 		CSIO_INC_STATS(ln, n_fdmi_err);
500 		csio_ln_dbg(ln, "Failed to issue fdmi rhba req\n");
501 	}
502 	spin_unlock_irqrestore(&hw->lock, flags);
503 }
504 
505 /*
506  * csio_ln_fdmi_dhba_cbfn - DHBA completion
507  * @hw: HW context
508  * @fdmi_req: fdmi request
509  */
510 static void
511 csio_ln_fdmi_dhba_cbfn(struct csio_hw *hw, struct csio_ioreq *fdmi_req)
512 {
513 	struct csio_lnode *ln = fdmi_req->lnode;
514 	void *cmd;
515 	struct fc_fdmi_port_name *port_name;
516 	uint32_t len;
517 	unsigned long flags;
518 
519 	if (fdmi_req->wr_status != FW_SUCCESS) {
520 		csio_ln_dbg(ln, "WR error:%x in processing fdmi dhba cmd\n",
521 			    fdmi_req->wr_status);
522 		CSIO_INC_STATS(ln, n_fdmi_err);
523 	}
524 
525 	if (!csio_is_rnode_ready(fdmi_req->rnode)) {
526 		CSIO_INC_STATS(ln, n_fdmi_err);
527 		return;
528 	}
529 	cmd = fdmi_req->dma_buf.vaddr;
530 	if (ntohs(csio_ct_rsp(cmd)) != FC_FS_ACC) {
531 		csio_ln_dbg(ln, "fdmi dhba cmd rejected reason %x expl %x\n",
532 			    csio_ct_reason(cmd), csio_ct_expl(cmd));
533 	}
534 
535 	/* Send FDMI cmd to de-register any Port attributes if registered
536 	 * before
537 	 */
538 
539 	/* Prepare FDMI DPRT cmd */
540 	memset(cmd, 0, FC_CT_HDR_LEN);
541 	csio_fill_ct_iu(cmd, FC_FST_MGMT, FC_FDMI_SUBTYPE, FC_FDMI_DPRT);
542 	len = FC_CT_HDR_LEN;
543 	port_name = (struct fc_fdmi_port_name *)csio_ct_get_pld(cmd);
544 	memcpy(&port_name->portname, csio_ln_wwpn(ln), 8);
545 	len += sizeof(*port_name);
546 
547 	/* Submit FDMI request */
548 	spin_lock_irqsave(&hw->lock, flags);
549 	if (csio_ln_mgmt_submit_req(fdmi_req, csio_ln_fdmi_dprt_cbfn,
550 				FCOE_CT, &fdmi_req->dma_buf, len)) {
551 		CSIO_INC_STATS(ln, n_fdmi_err);
552 		csio_ln_dbg(ln, "Failed to issue fdmi dprt req\n");
553 	}
554 	spin_unlock_irqrestore(&hw->lock, flags);
555 }
556 
557 /**
558  * csio_ln_fdmi_start - Start an FDMI request.
559  * @ln:		lnode
560  * @context:	session context
561  *
562  * Issued with lock held.
563  */
564 int
565 csio_ln_fdmi_start(struct csio_lnode *ln, void *context)
566 {
567 	struct csio_ioreq *fdmi_req;
568 	struct csio_rnode *fdmi_rn = (struct csio_rnode *)context;
569 	void *cmd;
570 	struct fc_fdmi_hba_identifier *hbaid;
571 	uint32_t len;
572 
573 	if (!(ln->flags & CSIO_LNF_FDMI_ENABLE))
574 		return -EPROTONOSUPPORT;
575 
576 	if (!csio_is_rnode_ready(fdmi_rn))
577 		CSIO_INC_STATS(ln, n_fdmi_err);
578 
579 	/* Send FDMI cmd to de-register any HBA attributes if registered
580 	 * before
581 	 */
582 
583 	fdmi_req = ln->mgmt_req;
584 	fdmi_req->lnode = ln;
585 	fdmi_req->rnode = fdmi_rn;
586 
587 	/* Prepare FDMI DHBA cmd */
588 	cmd = fdmi_req->dma_buf.vaddr;
589 	memset(cmd, 0, FC_CT_HDR_LEN);
590 	csio_fill_ct_iu(cmd, FC_FST_MGMT, FC_FDMI_SUBTYPE, FC_FDMI_DHBA);
591 	len = FC_CT_HDR_LEN;
592 
593 	hbaid = (struct fc_fdmi_hba_identifier *)csio_ct_get_pld(cmd);
594 	memcpy(&hbaid->id, csio_ln_wwpn(ln), 8);
595 	len += sizeof(*hbaid);
596 
597 	/* Submit FDMI request */
598 	if (csio_ln_mgmt_submit_req(fdmi_req, csio_ln_fdmi_dhba_cbfn,
599 					FCOE_CT, &fdmi_req->dma_buf, len)) {
600 		CSIO_INC_STATS(ln, n_fdmi_err);
601 		csio_ln_dbg(ln, "Failed to issue fdmi dhba req\n");
602 	}
603 
604 	return 0;
605 }
606 
607 /*
608  * csio_ln_vnp_read_cbfn - vnp read completion handler.
609  * @hw: HW lnode
610  * @cbfn: Completion handler.
611  *
612  * Reads vnp response and updates ln parameters.
613  */
614 static void
615 csio_ln_vnp_read_cbfn(struct csio_hw *hw, struct csio_mb *mbp)
616 {
617 	struct csio_lnode *ln = ((struct csio_lnode *)mbp->priv);
618 	struct fw_fcoe_vnp_cmd *rsp = (struct fw_fcoe_vnp_cmd *)(mbp->mb);
619 	struct fc_els_csp *csp;
620 	struct fc_els_cssp *clsp;
621 	enum fw_retval retval;
622 	__be32 nport_id = 0;
623 
624 	retval = FW_CMD_RETVAL_G(ntohl(rsp->alloc_to_len16));
625 	if (retval != FW_SUCCESS) {
626 		csio_err(hw, "FCOE VNP read cmd returned error:0x%x\n", retval);
627 		mempool_free(mbp, hw->mb_mempool);
628 		return;
629 	}
630 
631 	spin_lock_irq(&hw->lock);
632 
633 	memcpy(ln->mac, rsp->vnport_mac, sizeof(ln->mac));
634 	memcpy(&nport_id, &rsp->vnport_mac[3], sizeof(uint8_t)*3);
635 	ln->nport_id = ntohl(nport_id);
636 	ln->nport_id = ln->nport_id >> 8;
637 
638 	/* Update WWNs */
639 	/*
640 	 * This may look like a duplication of what csio_fcoe_enable_link()
641 	 * does, but is absolutely necessary if the vnpi changes between
642 	 * a FCOE LINK UP and FCOE LINK DOWN.
643 	 */
644 	memcpy(csio_ln_wwnn(ln), rsp->vnport_wwnn, 8);
645 	memcpy(csio_ln_wwpn(ln), rsp->vnport_wwpn, 8);
646 
647 	/* Copy common sparam */
648 	csp = (struct fc_els_csp *)rsp->cmn_srv_parms;
649 	ln->ln_sparm.csp.sp_hi_ver = csp->sp_hi_ver;
650 	ln->ln_sparm.csp.sp_lo_ver = csp->sp_lo_ver;
651 	ln->ln_sparm.csp.sp_bb_cred = csp->sp_bb_cred;
652 	ln->ln_sparm.csp.sp_features = csp->sp_features;
653 	ln->ln_sparm.csp.sp_bb_data = csp->sp_bb_data;
654 	ln->ln_sparm.csp.sp_r_a_tov = csp->sp_r_a_tov;
655 	ln->ln_sparm.csp.sp_e_d_tov = csp->sp_e_d_tov;
656 
657 	/* Copy word 0 & word 1 of class sparam */
658 	clsp = (struct fc_els_cssp *)rsp->clsp_word_0_1;
659 	ln->ln_sparm.clsp[2].cp_class = clsp->cp_class;
660 	ln->ln_sparm.clsp[2].cp_init = clsp->cp_init;
661 	ln->ln_sparm.clsp[2].cp_recip = clsp->cp_recip;
662 	ln->ln_sparm.clsp[2].cp_rdfs = clsp->cp_rdfs;
663 
664 	spin_unlock_irq(&hw->lock);
665 
666 	mempool_free(mbp, hw->mb_mempool);
667 
668 	/* Send an event to update local attribs */
669 	csio_lnode_async_event(ln, CSIO_LN_FC_ATTRIB_UPDATE);
670 }
671 
672 /*
673  * csio_ln_vnp_read - Read vnp params.
674  * @ln: lnode
675  * @cbfn: Completion handler.
676  *
677  * Issued with lock held.
678  */
679 static int
680 csio_ln_vnp_read(struct csio_lnode *ln,
681 		void (*cbfn) (struct csio_hw *, struct csio_mb *))
682 {
683 	struct csio_hw *hw = ln->hwp;
684 	struct csio_mb  *mbp;
685 
686 	/* Allocate Mbox request */
687 	mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
688 	if (!mbp) {
689 		CSIO_INC_STATS(hw, n_err_nomem);
690 		return -ENOMEM;
691 	}
692 
693 	/* Prepare VNP Command */
694 	csio_fcoe_vnp_read_init_mb(ln, mbp,
695 				    CSIO_MB_DEFAULT_TMO,
696 				    ln->fcf_flowid,
697 				    ln->vnp_flowid,
698 				    cbfn);
699 
700 	/* Issue MBOX cmd */
701 	if (csio_mb_issue(hw, mbp)) {
702 		csio_err(hw, "Failed to issue mbox FCoE VNP command\n");
703 		mempool_free(mbp, hw->mb_mempool);
704 		return -EINVAL;
705 	}
706 
707 	return 0;
708 }
709 
710 /*
711  * csio_fcoe_enable_link - Enable fcoe link.
712  * @ln: lnode
713  * @enable: enable/disable
714  * Issued with lock held.
715  * Issues mbox cmd to bring up FCOE link on port associated with given ln.
716  */
717 static int
718 csio_fcoe_enable_link(struct csio_lnode *ln, bool enable)
719 {
720 	struct csio_hw *hw = ln->hwp;
721 	struct csio_mb  *mbp;
722 	enum fw_retval retval;
723 	uint8_t portid;
724 	uint8_t sub_op;
725 	struct fw_fcoe_link_cmd *lcmd;
726 	int i;
727 
728 	mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
729 	if (!mbp) {
730 		CSIO_INC_STATS(hw, n_err_nomem);
731 		return -ENOMEM;
732 	}
733 
734 	portid = ln->portid;
735 	sub_op = enable ? FCOE_LINK_UP : FCOE_LINK_DOWN;
736 
737 	csio_dbg(hw, "bringing FCOE LINK %s on Port:%d\n",
738 		 sub_op ? "UP" : "DOWN", portid);
739 
740 	csio_write_fcoe_link_cond_init_mb(ln, mbp, CSIO_MB_DEFAULT_TMO,
741 					  portid, sub_op, 0, 0, 0, NULL);
742 
743 	if (csio_mb_issue(hw, mbp)) {
744 		csio_err(hw, "failed to issue FCOE LINK cmd on port[%d]\n",
745 			portid);
746 		mempool_free(mbp, hw->mb_mempool);
747 		return -EINVAL;
748 	}
749 
750 	retval = csio_mb_fw_retval(mbp);
751 	if (retval != FW_SUCCESS) {
752 		csio_err(hw,
753 			 "FCOE LINK %s cmd on port[%d] failed with "
754 			 "ret:x%x\n", sub_op ? "UP" : "DOWN", portid, retval);
755 		mempool_free(mbp, hw->mb_mempool);
756 		return -EINVAL;
757 	}
758 
759 	if (!enable)
760 		goto out;
761 
762 	lcmd = (struct fw_fcoe_link_cmd *)mbp->mb;
763 
764 	memcpy(csio_ln_wwnn(ln), lcmd->vnport_wwnn, 8);
765 	memcpy(csio_ln_wwpn(ln), lcmd->vnport_wwpn, 8);
766 
767 	for (i = 0; i < CSIO_MAX_PPORTS; i++)
768 		if (hw->pport[i].portid == portid)
769 			memcpy(hw->pport[i].mac, lcmd->phy_mac, 6);
770 
771 out:
772 	mempool_free(mbp, hw->mb_mempool);
773 	return 0;
774 }
775 
776 /*
777  * csio_ln_read_fcf_cbfn - Read fcf parameters
778  * @ln: lnode
779  *
780  * read fcf response and Update ln fcf information.
781  */
782 static void
783 csio_ln_read_fcf_cbfn(struct csio_hw *hw, struct csio_mb *mbp)
784 {
785 	struct csio_lnode *ln = (struct csio_lnode *)mbp->priv;
786 	struct csio_fcf_info	*fcf_info;
787 	struct fw_fcoe_fcf_cmd *rsp =
788 				(struct fw_fcoe_fcf_cmd *)(mbp->mb);
789 	enum fw_retval retval;
790 
791 	retval = FW_CMD_RETVAL_G(ntohl(rsp->retval_len16));
792 	if (retval != FW_SUCCESS) {
793 		csio_ln_err(ln, "FCOE FCF cmd failed with ret x%x\n",
794 				retval);
795 		mempool_free(mbp, hw->mb_mempool);
796 		return;
797 	}
798 
799 	spin_lock_irq(&hw->lock);
800 	fcf_info = ln->fcfinfo;
801 	fcf_info->priority = FW_FCOE_FCF_CMD_PRIORITY_GET(
802 					ntohs(rsp->priority_pkd));
803 	fcf_info->vf_id = ntohs(rsp->vf_id);
804 	fcf_info->vlan_id = rsp->vlan_id;
805 	fcf_info->max_fcoe_size = ntohs(rsp->max_fcoe_size);
806 	fcf_info->fka_adv = be32_to_cpu(rsp->fka_adv);
807 	fcf_info->fcfi = FW_FCOE_FCF_CMD_FCFI_GET(ntohl(rsp->op_to_fcfi));
808 	fcf_info->fpma = FW_FCOE_FCF_CMD_FPMA_GET(rsp->fpma_to_portid);
809 	fcf_info->spma = FW_FCOE_FCF_CMD_SPMA_GET(rsp->fpma_to_portid);
810 	fcf_info->login = FW_FCOE_FCF_CMD_LOGIN_GET(rsp->fpma_to_portid);
811 	fcf_info->portid = FW_FCOE_FCF_CMD_PORTID_GET(rsp->fpma_to_portid);
812 	memcpy(fcf_info->fc_map, rsp->fc_map, sizeof(fcf_info->fc_map));
813 	memcpy(fcf_info->mac, rsp->mac, sizeof(fcf_info->mac));
814 	memcpy(fcf_info->name_id, rsp->name_id, sizeof(fcf_info->name_id));
815 	memcpy(fcf_info->fabric, rsp->fabric, sizeof(fcf_info->fabric));
816 	memcpy(fcf_info->spma_mac, rsp->spma_mac, sizeof(fcf_info->spma_mac));
817 
818 	spin_unlock_irq(&hw->lock);
819 
820 	mempool_free(mbp, hw->mb_mempool);
821 }
822 
823 /*
824  * csio_ln_read_fcf_entry - Read fcf entry.
825  * @ln: lnode
826  * @cbfn: Completion handler.
827  *
828  * Issued with lock held.
829  */
830 static int
831 csio_ln_read_fcf_entry(struct csio_lnode *ln,
832 			void (*cbfn) (struct csio_hw *, struct csio_mb *))
833 {
834 	struct csio_hw *hw = ln->hwp;
835 	struct csio_mb  *mbp;
836 
837 	mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
838 	if (!mbp) {
839 		CSIO_INC_STATS(hw, n_err_nomem);
840 		return -ENOMEM;
841 	}
842 
843 	/* Get FCoE FCF information */
844 	csio_fcoe_read_fcf_init_mb(ln, mbp, CSIO_MB_DEFAULT_TMO,
845 				      ln->portid, ln->fcf_flowid, cbfn);
846 
847 	if (csio_mb_issue(hw, mbp)) {
848 		csio_err(hw, "failed to issue FCOE FCF cmd\n");
849 		mempool_free(mbp, hw->mb_mempool);
850 		return -EINVAL;
851 	}
852 
853 	return 0;
854 }
855 
856 /*
857  * csio_handle_link_up - Logical Linkup event.
858  * @hw - HW module.
859  * @portid - Physical port number
860  * @fcfi - FCF index.
861  * @vnpi - VNP index.
862  * Returns - none.
863  *
864  * This event is received from FW, when virtual link is established between
865  * Physical port[ENode] and FCF. If its new vnpi, then local node object is
866  * created on this FCF and set to [ONLINE] state.
867  * Lnode waits for FW_RDEV_CMD event to be received indicating that
868  * Fabric login is completed and lnode moves to [READY] state.
869  *
870  * This called with hw lock held
871  */
872 static void
873 csio_handle_link_up(struct csio_hw *hw, uint8_t portid, uint32_t fcfi,
874 		    uint32_t vnpi)
875 	__must_hold(&hw->lock)
876 {
877 	struct csio_lnode *ln = NULL;
878 
879 	/* Lookup lnode based on vnpi */
880 	ln = csio_ln_lookup_by_vnpi(hw, vnpi);
881 	if (!ln) {
882 		/* Pick lnode based on portid */
883 		ln = csio_ln_lookup_by_portid(hw, portid);
884 		if (!ln) {
885 			csio_err(hw, "failed to lookup fcoe lnode on port:%d\n",
886 				portid);
887 			CSIO_DB_ASSERT(0);
888 			return;
889 		}
890 
891 		/* Check if lnode has valid vnp flowid */
892 		if (ln->vnp_flowid != CSIO_INVALID_IDX) {
893 			/* New VN-Port */
894 			spin_unlock_irq(&hw->lock);
895 			csio_lnode_alloc(hw);
896 			spin_lock_irq(&hw->lock);
897 			if (!ln) {
898 				csio_err(hw,
899 					 "failed to allocate fcoe lnode"
900 					 "for port:%d vnpi:x%x\n",
901 					 portid, vnpi);
902 				CSIO_DB_ASSERT(0);
903 				return;
904 			}
905 			ln->portid = portid;
906 		}
907 		ln->vnp_flowid = vnpi;
908 		ln->dev_num &= ~0xFFFF;
909 		ln->dev_num |= vnpi;
910 	}
911 
912 	/*Initialize fcfi */
913 	ln->fcf_flowid = fcfi;
914 
915 	csio_info(hw, "Port:%d - FCOE LINK UP\n", portid);
916 
917 	CSIO_INC_STATS(ln, n_link_up);
918 
919 	/* Send LINKUP event to SM */
920 	csio_post_event(&ln->sm, CSIO_LNE_LINKUP);
921 }
922 
923 /*
924  * csio_post_event_rns
925  * @ln - FCOE lnode
926  * @evt - Given rnode event
927  * Returns - none
928  *
929  * Posts given rnode event to all FCOE rnodes connected with given Lnode.
930  * This routine is invoked when lnode receives LINK_DOWN/DOWN_LINK/CLOSE
931  * event.
932  *
933  * This called with hw lock held
934  */
935 static void
936 csio_post_event_rns(struct csio_lnode *ln, enum csio_rn_ev evt)
937 {
938 	struct csio_rnode *rnhead = (struct csio_rnode *) &ln->rnhead;
939 	struct list_head *tmp, *next;
940 	struct csio_rnode *rn;
941 
942 	list_for_each_safe(tmp, next, &rnhead->sm.sm_list) {
943 		rn = (struct csio_rnode *) tmp;
944 		csio_post_event(&rn->sm, evt);
945 	}
946 }
947 
948 /*
949  * csio_cleanup_rns
950  * @ln - FCOE lnode
951  * Returns - none
952  *
953  * Frees all FCOE rnodes connected with given Lnode.
954  *
955  * This called with hw lock held
956  */
957 static void
958 csio_cleanup_rns(struct csio_lnode *ln)
959 {
960 	struct csio_rnode *rnhead = (struct csio_rnode *) &ln->rnhead;
961 	struct list_head *tmp, *next_rn;
962 	struct csio_rnode *rn;
963 
964 	list_for_each_safe(tmp, next_rn, &rnhead->sm.sm_list) {
965 		rn = (struct csio_rnode *) tmp;
966 		csio_put_rnode(ln, rn);
967 	}
968 
969 }
970 
971 /*
972  * csio_post_event_lns
973  * @ln - FCOE lnode
974  * @evt - Given lnode event
975  * Returns - none
976  *
977  * Posts given lnode event to all FCOE lnodes connected with given Lnode.
978  * This routine is invoked when lnode receives LINK_DOWN/DOWN_LINK/CLOSE
979  * event.
980  *
981  * This called with hw lock held
982  */
983 static void
984 csio_post_event_lns(struct csio_lnode *ln, enum csio_ln_ev evt)
985 {
986 	struct list_head *tmp;
987 	struct csio_lnode *cln, *sln;
988 
989 	/* If NPIV lnode, send evt only to that and return */
990 	if (csio_is_npiv_ln(ln)) {
991 		csio_post_event(&ln->sm, evt);
992 		return;
993 	}
994 
995 	sln = ln;
996 	/* Traverse children lnodes list and send evt */
997 	list_for_each(tmp, &sln->cln_head) {
998 		cln = (struct csio_lnode *) tmp;
999 		csio_post_event(&cln->sm, evt);
1000 	}
1001 
1002 	/* Send evt to parent lnode */
1003 	csio_post_event(&ln->sm, evt);
1004 }
1005 
1006 /*
1007  * csio_ln_down - Lcoal nport is down
1008  * @ln - FCOE Lnode
1009  * Returns - none
1010  *
1011  * Sends LINK_DOWN events to Lnode and its associated NPIVs lnodes.
1012  *
1013  * This called with hw lock held
1014  */
1015 static void
1016 csio_ln_down(struct csio_lnode *ln)
1017 {
1018 	csio_post_event_lns(ln, CSIO_LNE_LINK_DOWN);
1019 }
1020 
1021 /*
1022  * csio_handle_link_down - Logical Linkdown event.
1023  * @hw - HW module.
1024  * @portid - Physical port number
1025  * @fcfi - FCF index.
1026  * @vnpi - VNP index.
1027  * Returns - none
1028  *
1029  * This event is received from FW, when virtual link goes down between
1030  * Physical port[ENode] and FCF. Lnode and its associated NPIVs lnode hosted on
1031  * this vnpi[VN-Port] will be de-instantiated.
1032  *
1033  * This called with hw lock held
1034  */
1035 static void
1036 csio_handle_link_down(struct csio_hw *hw, uint8_t portid, uint32_t fcfi,
1037 		      uint32_t vnpi)
1038 {
1039 	struct csio_fcf_info *fp;
1040 	struct csio_lnode *ln;
1041 
1042 	/* Lookup lnode based on vnpi */
1043 	ln = csio_ln_lookup_by_vnpi(hw, vnpi);
1044 	if (ln) {
1045 		fp = ln->fcfinfo;
1046 		CSIO_INC_STATS(ln, n_link_down);
1047 
1048 		/*Warn if linkdown received if lnode is not in ready state */
1049 		if (!csio_is_lnode_ready(ln)) {
1050 			csio_ln_warn(ln,
1051 				"warn: FCOE link is already in offline "
1052 				"Ignoring Fcoe linkdown event on portid %d\n",
1053 				 portid);
1054 			CSIO_INC_STATS(ln, n_evt_drop);
1055 			return;
1056 		}
1057 
1058 		/* Verify portid */
1059 		if (fp->portid != portid) {
1060 			csio_ln_warn(ln,
1061 				"warn: FCOE linkdown recv with "
1062 				"invalid port %d\n", portid);
1063 			CSIO_INC_STATS(ln, n_evt_drop);
1064 			return;
1065 		}
1066 
1067 		/* verify fcfi */
1068 		if (ln->fcf_flowid != fcfi) {
1069 			csio_ln_warn(ln,
1070 				"warn: FCOE linkdown recv with "
1071 				"invalid fcfi x%x\n", fcfi);
1072 			CSIO_INC_STATS(ln, n_evt_drop);
1073 			return;
1074 		}
1075 
1076 		csio_info(hw, "Port:%d - FCOE LINK DOWN\n", portid);
1077 
1078 		/* Send LINK_DOWN event to lnode s/m */
1079 		csio_ln_down(ln);
1080 
1081 		return;
1082 	} else {
1083 		csio_warn(hw,
1084 			  "warn: FCOE linkdown recv with invalid vnpi x%x\n",
1085 			  vnpi);
1086 		CSIO_INC_STATS(hw, n_evt_drop);
1087 	}
1088 }
1089 
1090 /*
1091  * csio_is_lnode_ready - Checks FCOE lnode is in ready state.
1092  * @ln: Lnode module
1093  *
1094  * Returns True if FCOE lnode is in ready state.
1095  */
1096 int
1097 csio_is_lnode_ready(struct csio_lnode *ln)
1098 {
1099 	return (csio_get_state(ln) == csio_lns_ready);
1100 }
1101 
1102 /*****************************************************************************/
1103 /* START: Lnode SM                                                           */
1104 /*****************************************************************************/
1105 /*
1106  * csio_lns_uninit - The request in uninit state.
1107  * @ln - FCOE lnode.
1108  * @evt - Event to be processed.
1109  *
1110  * Process the given lnode event which is currently in "uninit" state.
1111  * Invoked with HW lock held.
1112  * Return - none.
1113  */
1114 static void
1115 csio_lns_uninit(struct csio_lnode *ln, enum csio_ln_ev evt)
1116 {
1117 	struct csio_hw *hw = csio_lnode_to_hw(ln);
1118 	struct csio_lnode *rln = hw->rln;
1119 	int rv;
1120 
1121 	CSIO_INC_STATS(ln, n_evt_sm[evt]);
1122 	switch (evt) {
1123 	case CSIO_LNE_LINKUP:
1124 		csio_set_state(&ln->sm, csio_lns_online);
1125 		/* Read FCF only for physical lnode */
1126 		if (csio_is_phys_ln(ln)) {
1127 			rv = csio_ln_read_fcf_entry(ln,
1128 					csio_ln_read_fcf_cbfn);
1129 			if (rv != 0) {
1130 				/* TODO: Send HW RESET event */
1131 				CSIO_INC_STATS(ln, n_err);
1132 				break;
1133 			}
1134 
1135 			/* Add FCF record */
1136 			list_add_tail(&ln->fcfinfo->list, &rln->fcf_lsthead);
1137 		}
1138 
1139 		rv = csio_ln_vnp_read(ln, csio_ln_vnp_read_cbfn);
1140 		if (rv != 0) {
1141 			/* TODO: Send HW RESET event */
1142 			CSIO_INC_STATS(ln, n_err);
1143 		}
1144 		break;
1145 
1146 	case CSIO_LNE_DOWN_LINK:
1147 		break;
1148 
1149 	default:
1150 		csio_ln_dbg(ln,
1151 			    "unexp ln event %d recv from did:x%x in "
1152 			    "ln state[uninit].\n", evt, ln->nport_id);
1153 		CSIO_INC_STATS(ln, n_evt_unexp);
1154 		break;
1155 	} /* switch event */
1156 }
1157 
1158 /*
1159  * csio_lns_online - The request in online state.
1160  * @ln - FCOE lnode.
1161  * @evt - Event to be processed.
1162  *
1163  * Process the given lnode event which is currently in "online" state.
1164  * Invoked with HW lock held.
1165  * Return - none.
1166  */
1167 static void
1168 csio_lns_online(struct csio_lnode *ln, enum csio_ln_ev evt)
1169 	__must_hold(&csio_lnode_to_hw(ln)->lock)
1170 {
1171 	struct csio_hw *hw = csio_lnode_to_hw(ln);
1172 
1173 	CSIO_INC_STATS(ln, n_evt_sm[evt]);
1174 	switch (evt) {
1175 	case CSIO_LNE_LINKUP:
1176 		csio_ln_warn(ln,
1177 			     "warn: FCOE link is up already "
1178 			     "Ignoring linkup on port:%d\n", ln->portid);
1179 		CSIO_INC_STATS(ln, n_evt_drop);
1180 		break;
1181 
1182 	case CSIO_LNE_FAB_INIT_DONE:
1183 		csio_set_state(&ln->sm, csio_lns_ready);
1184 
1185 		spin_unlock_irq(&hw->lock);
1186 		csio_lnode_async_event(ln, CSIO_LN_FC_LINKUP);
1187 		spin_lock_irq(&hw->lock);
1188 
1189 		break;
1190 
1191 	case CSIO_LNE_LINK_DOWN:
1192 	case CSIO_LNE_DOWN_LINK:
1193 		csio_set_state(&ln->sm, csio_lns_uninit);
1194 		if (csio_is_phys_ln(ln)) {
1195 			/* Remove FCF entry */
1196 			list_del_init(&ln->fcfinfo->list);
1197 		}
1198 		break;
1199 
1200 	default:
1201 		csio_ln_dbg(ln,
1202 			    "unexp ln event %d recv from did:x%x in "
1203 			    "ln state[uninit].\n", evt, ln->nport_id);
1204 		CSIO_INC_STATS(ln, n_evt_unexp);
1205 
1206 		break;
1207 	} /* switch event */
1208 }
1209 
1210 /*
1211  * csio_lns_ready - The request in ready state.
1212  * @ln - FCOE lnode.
1213  * @evt - Event to be processed.
1214  *
1215  * Process the given lnode event which is currently in "ready" state.
1216  * Invoked with HW lock held.
1217  * Return - none.
1218  */
1219 static void
1220 csio_lns_ready(struct csio_lnode *ln, enum csio_ln_ev evt)
1221 	__must_hold(&csio_lnode_to_hw(ln)->lock)
1222 {
1223 	struct csio_hw *hw = csio_lnode_to_hw(ln);
1224 
1225 	CSIO_INC_STATS(ln, n_evt_sm[evt]);
1226 	switch (evt) {
1227 	case CSIO_LNE_FAB_INIT_DONE:
1228 		csio_ln_dbg(ln,
1229 			    "ignoring event %d recv from did x%x"
1230 			    "in ln state[ready].\n", evt, ln->nport_id);
1231 		CSIO_INC_STATS(ln, n_evt_drop);
1232 		break;
1233 
1234 	case CSIO_LNE_LINK_DOWN:
1235 		csio_set_state(&ln->sm, csio_lns_offline);
1236 		csio_post_event_rns(ln, CSIO_RNFE_DOWN);
1237 
1238 		spin_unlock_irq(&hw->lock);
1239 		csio_lnode_async_event(ln, CSIO_LN_FC_LINKDOWN);
1240 		spin_lock_irq(&hw->lock);
1241 
1242 		if (csio_is_phys_ln(ln)) {
1243 			/* Remove FCF entry */
1244 			list_del_init(&ln->fcfinfo->list);
1245 		}
1246 		break;
1247 
1248 	case CSIO_LNE_DOWN_LINK:
1249 		csio_set_state(&ln->sm, csio_lns_offline);
1250 		csio_post_event_rns(ln, CSIO_RNFE_DOWN);
1251 
1252 		/* Host need to issue aborts in case if FW has not returned
1253 		 * WRs with status "ABORTED"
1254 		 */
1255 		spin_unlock_irq(&hw->lock);
1256 		csio_lnode_async_event(ln, CSIO_LN_FC_LINKDOWN);
1257 		spin_lock_irq(&hw->lock);
1258 
1259 		if (csio_is_phys_ln(ln)) {
1260 			/* Remove FCF entry */
1261 			list_del_init(&ln->fcfinfo->list);
1262 		}
1263 		break;
1264 
1265 	case CSIO_LNE_CLOSE:
1266 		csio_set_state(&ln->sm, csio_lns_uninit);
1267 		csio_post_event_rns(ln, CSIO_RNFE_CLOSE);
1268 		break;
1269 
1270 	case CSIO_LNE_LOGO:
1271 		csio_set_state(&ln->sm, csio_lns_offline);
1272 		csio_post_event_rns(ln, CSIO_RNFE_DOWN);
1273 		break;
1274 
1275 	default:
1276 		csio_ln_dbg(ln,
1277 			    "unexp ln event %d recv from did:x%x in "
1278 			    "ln state[uninit].\n", evt, ln->nport_id);
1279 		CSIO_INC_STATS(ln, n_evt_unexp);
1280 		CSIO_DB_ASSERT(0);
1281 		break;
1282 	} /* switch event */
1283 }
1284 
1285 /*
1286  * csio_lns_offline - The request in offline state.
1287  * @ln - FCOE lnode.
1288  * @evt - Event to be processed.
1289  *
1290  * Process the given lnode event which is currently in "offline" state.
1291  * Invoked with HW lock held.
1292  * Return - none.
1293  */
1294 static void
1295 csio_lns_offline(struct csio_lnode *ln, enum csio_ln_ev evt)
1296 {
1297 	struct csio_hw *hw = csio_lnode_to_hw(ln);
1298 	struct csio_lnode *rln = hw->rln;
1299 	int rv;
1300 
1301 	CSIO_INC_STATS(ln, n_evt_sm[evt]);
1302 	switch (evt) {
1303 	case CSIO_LNE_LINKUP:
1304 		csio_set_state(&ln->sm, csio_lns_online);
1305 		/* Read FCF only for physical lnode */
1306 		if (csio_is_phys_ln(ln)) {
1307 			rv = csio_ln_read_fcf_entry(ln,
1308 					csio_ln_read_fcf_cbfn);
1309 			if (rv != 0) {
1310 				/* TODO: Send HW RESET event */
1311 				CSIO_INC_STATS(ln, n_err);
1312 				break;
1313 			}
1314 
1315 			/* Add FCF record */
1316 			list_add_tail(&ln->fcfinfo->list, &rln->fcf_lsthead);
1317 		}
1318 
1319 		rv = csio_ln_vnp_read(ln, csio_ln_vnp_read_cbfn);
1320 		if (rv != 0) {
1321 			/* TODO: Send HW RESET event */
1322 			CSIO_INC_STATS(ln, n_err);
1323 		}
1324 		break;
1325 
1326 	case CSIO_LNE_LINK_DOWN:
1327 	case CSIO_LNE_DOWN_LINK:
1328 	case CSIO_LNE_LOGO:
1329 		csio_ln_dbg(ln,
1330 			    "ignoring event %d recv from did x%x"
1331 			    "in ln state[offline].\n", evt, ln->nport_id);
1332 		CSIO_INC_STATS(ln, n_evt_drop);
1333 		break;
1334 
1335 	case CSIO_LNE_CLOSE:
1336 		csio_set_state(&ln->sm, csio_lns_uninit);
1337 		csio_post_event_rns(ln, CSIO_RNFE_CLOSE);
1338 		break;
1339 
1340 	default:
1341 		csio_ln_dbg(ln,
1342 			    "unexp ln event %d recv from did:x%x in "
1343 			    "ln state[offline]\n", evt, ln->nport_id);
1344 		CSIO_INC_STATS(ln, n_evt_unexp);
1345 		CSIO_DB_ASSERT(0);
1346 		break;
1347 	} /* switch event */
1348 }
1349 
1350 /*****************************************************************************/
1351 /* END: Lnode SM                                                             */
1352 /*****************************************************************************/
1353 
1354 static void
1355 csio_free_fcfinfo(struct kref *kref)
1356 {
1357 	struct csio_fcf_info *fcfinfo = container_of(kref,
1358 						struct csio_fcf_info, kref);
1359 	kfree(fcfinfo);
1360 }
1361 
1362 /* Helper routines for attributes  */
1363 /*
1364  * csio_lnode_state_to_str - Get current state of FCOE lnode.
1365  * @ln - lnode
1366  * @str - state of lnode.
1367  *
1368  */
1369 void
1370 csio_lnode_state_to_str(struct csio_lnode *ln, int8_t *str)
1371 {
1372 	if (csio_get_state(ln) == csio_lns_uninit) {
1373 		strcpy(str, "UNINIT");
1374 		return;
1375 	}
1376 	if (csio_get_state(ln) == csio_lns_ready) {
1377 		strcpy(str, "READY");
1378 		return;
1379 	}
1380 	if (csio_get_state(ln) == csio_lns_offline) {
1381 		strcpy(str, "OFFLINE");
1382 		return;
1383 	}
1384 	strcpy(str, "UNKNOWN");
1385 } /* csio_lnode_state_to_str */
1386 
1387 
1388 int
1389 csio_get_phy_port_stats(struct csio_hw *hw, uint8_t portid,
1390 			struct fw_fcoe_port_stats *port_stats)
1391 {
1392 	struct csio_mb  *mbp;
1393 	struct fw_fcoe_port_cmd_params portparams;
1394 	enum fw_retval retval;
1395 	int idx;
1396 
1397 	mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
1398 	if (!mbp) {
1399 		csio_err(hw, "FCoE FCF PARAMS command out of memory!\n");
1400 		return -EINVAL;
1401 	}
1402 	portparams.portid = portid;
1403 
1404 	for (idx = 1; idx <= 3; idx++) {
1405 		portparams.idx = (idx-1)*6 + 1;
1406 		portparams.nstats = 6;
1407 		if (idx == 3)
1408 			portparams.nstats = 4;
1409 		csio_fcoe_read_portparams_init_mb(hw, mbp, CSIO_MB_DEFAULT_TMO,
1410 							&portparams, NULL);
1411 		if (csio_mb_issue(hw, mbp)) {
1412 			csio_err(hw, "Issue of FCoE port params failed!\n");
1413 			mempool_free(mbp, hw->mb_mempool);
1414 			return -EINVAL;
1415 		}
1416 		csio_mb_process_portparams_rsp(hw, mbp, &retval,
1417 						&portparams, port_stats);
1418 	}
1419 
1420 	mempool_free(mbp, hw->mb_mempool);
1421 	return 0;
1422 }
1423 
1424 /*
1425  * csio_ln_mgmt_wr_handler -Mgmt Work Request handler.
1426  * @wr - WR.
1427  * @len - WR len.
1428  * This handler is invoked when an outstanding mgmt WR is completed.
1429  * Its invoked in the context of FW event worker thread for every
1430  * mgmt event received.
1431  * Return - none.
1432  */
1433 
1434 static void
1435 csio_ln_mgmt_wr_handler(struct csio_hw *hw, void *wr, uint32_t len)
1436 {
1437 	struct csio_mgmtm *mgmtm = csio_hw_to_mgmtm(hw);
1438 	struct csio_ioreq *io_req = NULL;
1439 	struct fw_fcoe_els_ct_wr *wr_cmd;
1440 
1441 
1442 	wr_cmd = (struct fw_fcoe_els_ct_wr *) wr;
1443 
1444 	if (len < sizeof(struct fw_fcoe_els_ct_wr)) {
1445 		csio_err(mgmtm->hw,
1446 			 "Invalid ELS CT WR length recvd, len:%x\n", len);
1447 		mgmtm->stats.n_err++;
1448 		return;
1449 	}
1450 
1451 	io_req = (struct csio_ioreq *) ((uintptr_t) wr_cmd->cookie);
1452 	io_req->wr_status = csio_wr_status(wr_cmd);
1453 
1454 	/* lookup ioreq exists in our active Q */
1455 	spin_lock_irq(&hw->lock);
1456 	if (csio_mgmt_req_lookup(mgmtm, io_req) != 0) {
1457 		csio_err(mgmtm->hw,
1458 			"Error- Invalid IO handle recv in WR. handle: %p\n",
1459 			io_req);
1460 		mgmtm->stats.n_err++;
1461 		spin_unlock_irq(&hw->lock);
1462 		return;
1463 	}
1464 
1465 	mgmtm = csio_hw_to_mgmtm(hw);
1466 
1467 	/* Dequeue from active queue */
1468 	list_del_init(&io_req->sm.sm_list);
1469 	mgmtm->stats.n_active--;
1470 	spin_unlock_irq(&hw->lock);
1471 
1472 	/* io_req will be freed by completion handler */
1473 	if (io_req->io_cbfn)
1474 		io_req->io_cbfn(hw, io_req);
1475 }
1476 
1477 /**
1478  * csio_fcoe_fwevt_handler - Event handler for Firmware FCoE events.
1479  * @hw:		HW module
1480  * @cpl_op:	CPL opcode
1481  * @cmd:	FW cmd/WR.
1482  *
1483  * Process received FCoE cmd/WR event from FW.
1484  */
1485 void
1486 csio_fcoe_fwevt_handler(struct csio_hw *hw, __u8 cpl_op, __be64 *cmd)
1487 {
1488 	struct csio_lnode *ln;
1489 	struct csio_rnode *rn;
1490 	uint8_t portid, opcode = *(uint8_t *)cmd;
1491 	struct fw_fcoe_link_cmd *lcmd;
1492 	struct fw_wr_hdr *wr;
1493 	struct fw_rdev_wr *rdev_wr;
1494 	enum fw_fcoe_link_status lstatus;
1495 	uint32_t fcfi, rdev_flowid, vnpi;
1496 	enum csio_ln_ev evt;
1497 
1498 	if (cpl_op == CPL_FW6_MSG && opcode == FW_FCOE_LINK_CMD) {
1499 
1500 		lcmd = (struct fw_fcoe_link_cmd *)cmd;
1501 		lstatus = lcmd->lstatus;
1502 		portid = FW_FCOE_LINK_CMD_PORTID_GET(
1503 					ntohl(lcmd->op_to_portid));
1504 		fcfi = FW_FCOE_LINK_CMD_FCFI_GET(ntohl(lcmd->sub_opcode_fcfi));
1505 		vnpi = FW_FCOE_LINK_CMD_VNPI_GET(ntohl(lcmd->vnpi_pkd));
1506 
1507 		if (lstatus == FCOE_LINKUP) {
1508 
1509 			/* HW lock here */
1510 			spin_lock_irq(&hw->lock);
1511 			csio_handle_link_up(hw, portid, fcfi, vnpi);
1512 			spin_unlock_irq(&hw->lock);
1513 			/* HW un lock here */
1514 
1515 		} else if (lstatus == FCOE_LINKDOWN) {
1516 
1517 			/* HW lock here */
1518 			spin_lock_irq(&hw->lock);
1519 			csio_handle_link_down(hw, portid, fcfi, vnpi);
1520 			spin_unlock_irq(&hw->lock);
1521 			/* HW un lock here */
1522 		} else {
1523 			csio_warn(hw, "Unexpected FCOE LINK status:0x%x\n",
1524 				  lcmd->lstatus);
1525 			CSIO_INC_STATS(hw, n_cpl_unexp);
1526 		}
1527 	} else if (cpl_op == CPL_FW6_PLD) {
1528 		wr = (struct fw_wr_hdr *) (cmd + 4);
1529 		if (FW_WR_OP_G(be32_to_cpu(wr->hi))
1530 			== FW_RDEV_WR) {
1531 
1532 			rdev_wr = (struct fw_rdev_wr *) (cmd + 4);
1533 
1534 			rdev_flowid = FW_RDEV_WR_FLOWID_GET(
1535 					ntohl(rdev_wr->alloc_to_len16));
1536 			vnpi = FW_RDEV_WR_ASSOC_FLOWID_GET(
1537 				    ntohl(rdev_wr->flags_to_assoc_flowid));
1538 
1539 			csio_dbg(hw,
1540 				"FW_RDEV_WR: flowid:x%x ev_cause:x%x "
1541 				"vnpi:0x%x\n", rdev_flowid,
1542 				rdev_wr->event_cause, vnpi);
1543 
1544 			if (rdev_wr->protocol != PROT_FCOE) {
1545 				csio_err(hw,
1546 					"FW_RDEV_WR: invalid proto:x%x "
1547 					"received with flowid:x%x\n",
1548 					rdev_wr->protocol,
1549 					rdev_flowid);
1550 				CSIO_INC_STATS(hw, n_evt_drop);
1551 				return;
1552 			}
1553 
1554 			/* HW lock here */
1555 			spin_lock_irq(&hw->lock);
1556 			ln = csio_ln_lookup_by_vnpi(hw, vnpi);
1557 			if (!ln) {
1558 				csio_err(hw,
1559 					"FW_DEV_WR: invalid vnpi:x%x received "
1560 					"with flowid:x%x\n", vnpi, rdev_flowid);
1561 				CSIO_INC_STATS(hw, n_evt_drop);
1562 				goto out_pld;
1563 			}
1564 
1565 			rn = csio_confirm_rnode(ln, rdev_flowid,
1566 					&rdev_wr->u.fcoe_rdev);
1567 			if (!rn) {
1568 				csio_ln_dbg(ln,
1569 					"Failed to confirm rnode "
1570 					"for flowid:x%x\n", rdev_flowid);
1571 				CSIO_INC_STATS(hw, n_evt_drop);
1572 				goto out_pld;
1573 			}
1574 
1575 			/* save previous event for debugging */
1576 			ln->prev_evt = ln->cur_evt;
1577 			ln->cur_evt = rdev_wr->event_cause;
1578 			CSIO_INC_STATS(ln, n_evt_fw[rdev_wr->event_cause]);
1579 
1580 			/* Translate all the fabric events to lnode SM events */
1581 			evt = CSIO_FWE_TO_LNE(rdev_wr->event_cause);
1582 			if (evt) {
1583 				csio_ln_dbg(ln,
1584 					"Posting event to lnode event:%d "
1585 					"cause:%d flowid:x%x\n", evt,
1586 					rdev_wr->event_cause, rdev_flowid);
1587 				csio_post_event(&ln->sm, evt);
1588 			}
1589 
1590 			/* Handover event to rn SM here. */
1591 			csio_rnode_fwevt_handler(rn, rdev_wr->event_cause);
1592 out_pld:
1593 			spin_unlock_irq(&hw->lock);
1594 			return;
1595 		} else {
1596 			csio_warn(hw, "unexpected WR op(0x%x) recv\n",
1597 				  FW_WR_OP_G(be32_to_cpu((wr->hi))));
1598 			CSIO_INC_STATS(hw, n_cpl_unexp);
1599 		}
1600 	} else if (cpl_op == CPL_FW6_MSG) {
1601 		wr = (struct fw_wr_hdr *) (cmd);
1602 		if (FW_WR_OP_G(be32_to_cpu(wr->hi)) == FW_FCOE_ELS_CT_WR) {
1603 			csio_ln_mgmt_wr_handler(hw, wr,
1604 					sizeof(struct fw_fcoe_els_ct_wr));
1605 		} else {
1606 			csio_warn(hw, "unexpected WR op(0x%x) recv\n",
1607 				  FW_WR_OP_G(be32_to_cpu((wr->hi))));
1608 			CSIO_INC_STATS(hw, n_cpl_unexp);
1609 		}
1610 	} else {
1611 		csio_warn(hw, "unexpected CPL op(0x%x) recv\n", opcode);
1612 		CSIO_INC_STATS(hw, n_cpl_unexp);
1613 	}
1614 }
1615 
1616 /**
1617  * csio_lnode_start - Kickstart lnode discovery.
1618  * @ln:		lnode
1619  *
1620  * This routine kickstarts the discovery by issuing an FCOE_LINK (up) command.
1621  */
1622 int
1623 csio_lnode_start(struct csio_lnode *ln)
1624 {
1625 	int rv = 0;
1626 	if (csio_is_phys_ln(ln) && !(ln->flags & CSIO_LNF_LINK_ENABLE)) {
1627 		rv = csio_fcoe_enable_link(ln, 1);
1628 		ln->flags |= CSIO_LNF_LINK_ENABLE;
1629 	}
1630 
1631 	return rv;
1632 }
1633 
1634 /**
1635  * csio_lnode_stop - Stop the lnode.
1636  * @ln:		lnode
1637  *
1638  * This routine is invoked by HW module to stop lnode and its associated NPIV
1639  * lnodes.
1640  */
1641 void
1642 csio_lnode_stop(struct csio_lnode *ln)
1643 {
1644 	csio_post_event_lns(ln, CSIO_LNE_DOWN_LINK);
1645 	if (csio_is_phys_ln(ln) && (ln->flags & CSIO_LNF_LINK_ENABLE)) {
1646 		csio_fcoe_enable_link(ln, 0);
1647 		ln->flags &= ~CSIO_LNF_LINK_ENABLE;
1648 	}
1649 	csio_ln_dbg(ln, "stopping ln :%p\n", ln);
1650 }
1651 
1652 /**
1653  * csio_lnode_close - Close an lnode.
1654  * @ln:		lnode
1655  *
1656  * This routine is invoked by HW module to close an lnode and its
1657  * associated NPIV lnodes. Lnode and its associated NPIV lnodes are
1658  * set to uninitialized state.
1659  */
1660 void
1661 csio_lnode_close(struct csio_lnode *ln)
1662 {
1663 	csio_post_event_lns(ln, CSIO_LNE_CLOSE);
1664 	if (csio_is_phys_ln(ln))
1665 		ln->vnp_flowid = CSIO_INVALID_IDX;
1666 
1667 	csio_ln_dbg(ln, "closed ln :%p\n", ln);
1668 }
1669 
1670 /*
1671  * csio_ln_prep_ecwr - Prepare ELS/CT WR.
1672  * @io_req - IO request.
1673  * @wr_len - WR len
1674  * @immd_len - WR immediate data
1675  * @sub_op - Sub opcode
1676  * @sid - source portid.
1677  * @did - destination portid
1678  * @flow_id - flowid
1679  * @fw_wr - ELS/CT WR to be prepared.
1680  * Returns: 0 - on success
1681  */
1682 static int
1683 csio_ln_prep_ecwr(struct csio_ioreq *io_req, uint32_t wr_len,
1684 		      uint32_t immd_len, uint8_t sub_op, uint32_t sid,
1685 		      uint32_t did, uint32_t flow_id, uint8_t *fw_wr)
1686 {
1687 	struct fw_fcoe_els_ct_wr *wr;
1688 	__be32 port_id;
1689 
1690 	wr  = (struct fw_fcoe_els_ct_wr *)fw_wr;
1691 	wr->op_immdlen = cpu_to_be32(FW_WR_OP_V(FW_FCOE_ELS_CT_WR) |
1692 				     FW_FCOE_ELS_CT_WR_IMMDLEN(immd_len));
1693 
1694 	wr_len =  DIV_ROUND_UP(wr_len, 16);
1695 	wr->flowid_len16 = cpu_to_be32(FW_WR_FLOWID_V(flow_id) |
1696 				       FW_WR_LEN16_V(wr_len));
1697 	wr->els_ct_type = sub_op;
1698 	wr->ctl_pri = 0;
1699 	wr->cp_en_class = 0;
1700 	wr->cookie = io_req->fw_handle;
1701 	wr->iqid = cpu_to_be16(csio_q_physiqid(
1702 					io_req->lnode->hwp, io_req->iq_idx));
1703 	wr->fl_to_sp =  FW_FCOE_ELS_CT_WR_SP(1);
1704 	wr->tmo_val = (uint8_t) io_req->tmo;
1705 	port_id = htonl(sid);
1706 	memcpy(wr->l_id, PORT_ID_PTR(port_id), 3);
1707 	port_id = htonl(did);
1708 	memcpy(wr->r_id, PORT_ID_PTR(port_id), 3);
1709 
1710 	/* Prepare RSP SGL */
1711 	wr->rsp_dmalen = cpu_to_be32(io_req->dma_buf.len);
1712 	wr->rsp_dmaaddr = cpu_to_be64(io_req->dma_buf.paddr);
1713 	return 0;
1714 }
1715 
1716 /*
1717  * csio_ln_mgmt_submit_wr - Post elsct work request.
1718  * @mgmtm - mgmtm
1719  * @io_req - io request.
1720  * @sub_op - ELS or CT request type
1721  * @pld - Dma Payload buffer
1722  * @pld_len - Payload len
1723  * Prepares ELSCT Work request and sents it to FW.
1724  * Returns: 0 - on success
1725  */
1726 static int
1727 csio_ln_mgmt_submit_wr(struct csio_mgmtm *mgmtm, struct csio_ioreq *io_req,
1728 		uint8_t sub_op, struct csio_dma_buf *pld,
1729 		uint32_t pld_len)
1730 {
1731 	struct csio_wr_pair wrp;
1732 	struct csio_lnode *ln = io_req->lnode;
1733 	struct csio_rnode *rn = io_req->rnode;
1734 	struct	csio_hw	*hw = mgmtm->hw;
1735 	uint8_t fw_wr[64];
1736 	struct ulptx_sgl dsgl;
1737 	uint32_t wr_size = 0;
1738 	uint8_t im_len = 0;
1739 	uint32_t wr_off = 0;
1740 
1741 	int ret = 0;
1742 
1743 	/* Calculate WR Size for this ELS REQ */
1744 	wr_size = sizeof(struct fw_fcoe_els_ct_wr);
1745 
1746 	/* Send as immediate data if pld < 256 */
1747 	if (pld_len < 256) {
1748 		wr_size += ALIGN(pld_len, 8);
1749 		im_len = (uint8_t)pld_len;
1750 	} else
1751 		wr_size += sizeof(struct ulptx_sgl);
1752 
1753 	/* Roundup WR size in units of 16 bytes */
1754 	wr_size = ALIGN(wr_size, 16);
1755 
1756 	/* Get WR to send ELS REQ */
1757 	ret = csio_wr_get(hw, mgmtm->eq_idx, wr_size, &wrp);
1758 	if (ret != 0) {
1759 		csio_err(hw, "Failed to get WR for ec_req %p ret:%d\n",
1760 			io_req, ret);
1761 		return ret;
1762 	}
1763 
1764 	/* Prepare Generic WR used by all ELS/CT cmd */
1765 	csio_ln_prep_ecwr(io_req, wr_size, im_len, sub_op,
1766 				ln->nport_id, rn->nport_id,
1767 				csio_rn_flowid(rn),
1768 				&fw_wr[0]);
1769 
1770 	/* Copy ELS/CT WR CMD */
1771 	csio_wr_copy_to_wrp(&fw_wr[0], &wrp, wr_off,
1772 			sizeof(struct fw_fcoe_els_ct_wr));
1773 	wr_off += sizeof(struct fw_fcoe_els_ct_wr);
1774 
1775 	/* Copy payload to Immediate section of WR */
1776 	if (im_len)
1777 		csio_wr_copy_to_wrp(pld->vaddr, &wrp, wr_off, im_len);
1778 	else {
1779 		/* Program DSGL to dma payload */
1780 		dsgl.cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
1781 					ULPTX_MORE_F | ULPTX_NSGE_V(1));
1782 		dsgl.len0 = cpu_to_be32(pld_len);
1783 		dsgl.addr0 = cpu_to_be64(pld->paddr);
1784 		csio_wr_copy_to_wrp(&dsgl, &wrp, ALIGN(wr_off, 8),
1785 				   sizeof(struct ulptx_sgl));
1786 	}
1787 
1788 	/* Issue work request to xmit ELS/CT req to FW */
1789 	csio_wr_issue(mgmtm->hw, mgmtm->eq_idx, false);
1790 	return ret;
1791 }
1792 
1793 /*
1794  * csio_ln_mgmt_submit_req - Submit FCOE Mgmt request.
1795  * @io_req - IO Request
1796  * @io_cbfn - Completion handler.
1797  * @req_type - ELS or CT request type
1798  * @pld - Dma Payload buffer
1799  * @pld_len - Payload len
1800  *
1801  *
1802  * This API used submit managment ELS/CT request.
1803  * This called with hw lock held
1804  * Returns: 0 - on success
1805  *	    -ENOMEM	- on error.
1806  */
1807 static int
1808 csio_ln_mgmt_submit_req(struct csio_ioreq *io_req,
1809 		void (*io_cbfn) (struct csio_hw *, struct csio_ioreq *),
1810 		enum fcoe_cmn_type req_type, struct csio_dma_buf *pld,
1811 		uint32_t pld_len)
1812 {
1813 	struct csio_hw *hw = csio_lnode_to_hw(io_req->lnode);
1814 	struct csio_mgmtm *mgmtm = csio_hw_to_mgmtm(hw);
1815 	int rv;
1816 
1817 	BUG_ON(pld_len > pld->len);
1818 
1819 	io_req->io_cbfn = io_cbfn;	/* Upper layer callback handler */
1820 	io_req->fw_handle = (uintptr_t) (io_req);
1821 	io_req->eq_idx = mgmtm->eq_idx;
1822 	io_req->iq_idx = mgmtm->iq_idx;
1823 
1824 	rv = csio_ln_mgmt_submit_wr(mgmtm, io_req, req_type, pld, pld_len);
1825 	if (rv == 0) {
1826 		list_add_tail(&io_req->sm.sm_list, &mgmtm->active_q);
1827 		mgmtm->stats.n_active++;
1828 	}
1829 	return rv;
1830 }
1831 
1832 /*
1833  * csio_ln_fdmi_init - FDMI Init entry point.
1834  * @ln: lnode
1835  */
1836 static int
1837 csio_ln_fdmi_init(struct csio_lnode *ln)
1838 {
1839 	struct csio_hw *hw = csio_lnode_to_hw(ln);
1840 	struct csio_dma_buf	*dma_buf;
1841 
1842 	/* Allocate MGMT request required for FDMI */
1843 	ln->mgmt_req = kzalloc_obj(struct csio_ioreq);
1844 	if (!ln->mgmt_req) {
1845 		csio_ln_err(ln, "Failed to alloc ioreq for FDMI\n");
1846 		CSIO_INC_STATS(hw, n_err_nomem);
1847 		return -ENOMEM;
1848 	}
1849 
1850 	/* Allocate Dma buffers for FDMI response Payload */
1851 	dma_buf = &ln->mgmt_req->dma_buf;
1852 	dma_buf->len = 2048;
1853 	dma_buf->vaddr = dma_alloc_coherent(&hw->pdev->dev, dma_buf->len,
1854 						&dma_buf->paddr, GFP_KERNEL);
1855 	if (!dma_buf->vaddr) {
1856 		csio_err(hw, "Failed to alloc DMA buffer for FDMI!\n");
1857 		kfree(ln->mgmt_req);
1858 		ln->mgmt_req = NULL;
1859 		return -ENOMEM;
1860 	}
1861 
1862 	ln->flags |= CSIO_LNF_FDMI_ENABLE;
1863 	return 0;
1864 }
1865 
1866 /*
1867  * csio_ln_fdmi_exit - FDMI exit entry point.
1868  * @ln: lnode
1869  */
1870 static int
1871 csio_ln_fdmi_exit(struct csio_lnode *ln)
1872 {
1873 	struct csio_dma_buf *dma_buf;
1874 	struct csio_hw *hw = csio_lnode_to_hw(ln);
1875 
1876 	if (!ln->mgmt_req)
1877 		return 0;
1878 
1879 	dma_buf = &ln->mgmt_req->dma_buf;
1880 	if (dma_buf->vaddr)
1881 		dma_free_coherent(&hw->pdev->dev, dma_buf->len, dma_buf->vaddr,
1882 				    dma_buf->paddr);
1883 
1884 	kfree(ln->mgmt_req);
1885 	return 0;
1886 }
1887 
1888 int
1889 csio_scan_done(struct csio_lnode *ln, unsigned long ticks,
1890 		unsigned long time, unsigned long max_scan_ticks,
1891 		unsigned long delta_scan_ticks)
1892 {
1893 	int rv = 0;
1894 
1895 	if (time >= max_scan_ticks)
1896 		return 1;
1897 
1898 	if (!ln->tgt_scan_tick)
1899 		ln->tgt_scan_tick = ticks;
1900 
1901 	if (((ticks - ln->tgt_scan_tick) >= delta_scan_ticks)) {
1902 		if (!ln->last_scan_ntgts)
1903 			ln->last_scan_ntgts = ln->n_scsi_tgts;
1904 		else {
1905 			if (ln->last_scan_ntgts == ln->n_scsi_tgts)
1906 				return 1;
1907 
1908 			ln->last_scan_ntgts = ln->n_scsi_tgts;
1909 		}
1910 		ln->tgt_scan_tick = ticks;
1911 	}
1912 	return rv;
1913 }
1914 
1915 /*
1916  * csio_notify_lnodes:
1917  * @hw: HW module
1918  * @note: Notification
1919  *
1920  * Called from the HW SM to fan out notifications to the
1921  * Lnode SM. Since the HW SM is entered with lock held,
1922  * there is no need to hold locks here.
1923  *
1924  */
1925 void
1926 csio_notify_lnodes(struct csio_hw *hw, enum csio_ln_notify note)
1927 {
1928 	struct list_head *tmp;
1929 	struct csio_lnode *ln;
1930 
1931 	csio_dbg(hw, "Notifying all nodes of event %d\n", note);
1932 
1933 	/* Traverse children lnodes list and send evt */
1934 	list_for_each(tmp, &hw->sln_head) {
1935 		ln = (struct csio_lnode *) tmp;
1936 
1937 		switch (note) {
1938 		case CSIO_LN_NOTIFY_HWREADY:
1939 			csio_lnode_start(ln);
1940 			break;
1941 
1942 		case CSIO_LN_NOTIFY_HWRESET:
1943 		case CSIO_LN_NOTIFY_HWREMOVE:
1944 			csio_lnode_close(ln);
1945 			break;
1946 
1947 		case CSIO_LN_NOTIFY_HWSTOP:
1948 			csio_lnode_stop(ln);
1949 			break;
1950 
1951 		default:
1952 			break;
1953 
1954 		}
1955 	}
1956 }
1957 
1958 /*
1959  * csio_disable_lnodes:
1960  * @hw: HW module
1961  * @portid:port id
1962  * @disable: disable/enable flag.
1963  * If disable=1, disables all lnode hosted on given physical port.
1964  * otherwise enables all the lnodes on given phsysical port.
1965  * This routine need to called with hw lock held.
1966  */
1967 void
1968 csio_disable_lnodes(struct csio_hw *hw, uint8_t portid, bool disable)
1969 {
1970 	struct list_head *tmp;
1971 	struct csio_lnode *ln;
1972 
1973 	csio_dbg(hw, "Notifying event to all nodes of port:%d\n", portid);
1974 
1975 	/* Traverse sibling lnodes list and send evt */
1976 	list_for_each(tmp, &hw->sln_head) {
1977 		ln = (struct csio_lnode *) tmp;
1978 		if (ln->portid != portid)
1979 			continue;
1980 
1981 		if (disable)
1982 			csio_lnode_stop(ln);
1983 		else
1984 			csio_lnode_start(ln);
1985 	}
1986 }
1987 
1988 /*
1989  * csio_ln_init - Initialize an lnode.
1990  * @ln:		lnode
1991  *
1992  */
1993 static int
1994 csio_ln_init(struct csio_lnode *ln)
1995 {
1996 	int rv = -EINVAL;
1997 	struct csio_lnode *pln;
1998 	struct csio_hw *hw = csio_lnode_to_hw(ln);
1999 
2000 	csio_init_state(&ln->sm, csio_lns_uninit);
2001 	ln->vnp_flowid = CSIO_INVALID_IDX;
2002 	ln->fcf_flowid = CSIO_INVALID_IDX;
2003 
2004 	if (csio_is_root_ln(ln)) {
2005 
2006 		/* This is the lnode used during initialization */
2007 
2008 		ln->fcfinfo = kzalloc_obj(struct csio_fcf_info);
2009 		if (!ln->fcfinfo) {
2010 			csio_ln_err(ln, "Failed to alloc FCF record\n");
2011 			CSIO_INC_STATS(hw, n_err_nomem);
2012 			goto err;
2013 		}
2014 
2015 		INIT_LIST_HEAD(&ln->fcf_lsthead);
2016 		kref_init(&ln->fcfinfo->kref);
2017 
2018 		if (csio_fdmi_enable && csio_ln_fdmi_init(ln))
2019 			goto err;
2020 
2021 	} else { /* Either a non-root physical or a virtual lnode */
2022 
2023 		/*
2024 		 * THe rest is common for non-root physical and NPIV lnodes.
2025 		 * Just get references to all other modules
2026 		 */
2027 
2028 		if (csio_is_npiv_ln(ln)) {
2029 			/* NPIV */
2030 			pln = csio_parent_lnode(ln);
2031 			kref_get(&pln->fcfinfo->kref);
2032 			ln->fcfinfo = pln->fcfinfo;
2033 		} else {
2034 			/* Another non-root physical lnode (FCF) */
2035 			ln->fcfinfo = kzalloc_obj(struct csio_fcf_info);
2036 			if (!ln->fcfinfo) {
2037 				csio_ln_err(ln, "Failed to alloc FCF info\n");
2038 				CSIO_INC_STATS(hw, n_err_nomem);
2039 				goto err;
2040 			}
2041 
2042 			kref_init(&ln->fcfinfo->kref);
2043 
2044 			if (csio_fdmi_enable && csio_ln_fdmi_init(ln))
2045 				goto err;
2046 		}
2047 
2048 	} /* if (!csio_is_root_ln(ln)) */
2049 
2050 	return 0;
2051 err:
2052 	return rv;
2053 }
2054 
2055 static void
2056 csio_ln_exit(struct csio_lnode *ln)
2057 {
2058 	struct csio_lnode *pln;
2059 
2060 	csio_cleanup_rns(ln);
2061 	if (csio_is_npiv_ln(ln)) {
2062 		pln = csio_parent_lnode(ln);
2063 		kref_put(&pln->fcfinfo->kref, csio_free_fcfinfo);
2064 	} else {
2065 		kref_put(&ln->fcfinfo->kref, csio_free_fcfinfo);
2066 		if (csio_fdmi_enable)
2067 			csio_ln_fdmi_exit(ln);
2068 	}
2069 	ln->fcfinfo = NULL;
2070 }
2071 
2072 /*
2073  * csio_lnode_init - Initialize the members of an lnode.
2074  * @ln:		lnode
2075  */
2076 int
2077 csio_lnode_init(struct csio_lnode *ln, struct csio_hw *hw,
2078 		struct csio_lnode *pln)
2079 {
2080 	int rv = -EINVAL;
2081 
2082 	/* Link this lnode to hw */
2083 	csio_lnode_to_hw(ln)	= hw;
2084 
2085 	/* Link child to parent if child lnode */
2086 	if (pln)
2087 		ln->pln = pln;
2088 	else
2089 		ln->pln = NULL;
2090 
2091 	/* Initialize scsi_tgt and timers to zero */
2092 	ln->n_scsi_tgts = 0;
2093 	ln->last_scan_ntgts = 0;
2094 	ln->tgt_scan_tick = 0;
2095 
2096 	/* Initialize rnode list */
2097 	INIT_LIST_HEAD(&ln->rnhead);
2098 	INIT_LIST_HEAD(&ln->cln_head);
2099 
2100 	/* Initialize log level for debug */
2101 	ln->params.log_level	= hw->params.log_level;
2102 
2103 	if (csio_ln_init(ln))
2104 		goto err;
2105 
2106 	/* Add lnode to list of sibling or children lnodes */
2107 	spin_lock_irq(&hw->lock);
2108 	list_add_tail(&ln->sm.sm_list, pln ? &pln->cln_head : &hw->sln_head);
2109 	if (pln)
2110 		pln->num_vports++;
2111 	spin_unlock_irq(&hw->lock);
2112 
2113 	hw->num_lns++;
2114 
2115 	return 0;
2116 err:
2117 	csio_lnode_to_hw(ln) = NULL;
2118 	return rv;
2119 }
2120 
2121 /**
2122  * csio_lnode_exit - De-instantiate an lnode.
2123  * @ln:		lnode
2124  *
2125  */
2126 void
2127 csio_lnode_exit(struct csio_lnode *ln)
2128 {
2129 	struct csio_hw *hw = csio_lnode_to_hw(ln);
2130 
2131 	csio_ln_exit(ln);
2132 
2133 	/* Remove this lnode from hw->sln_head */
2134 	spin_lock_irq(&hw->lock);
2135 
2136 	list_del_init(&ln->sm.sm_list);
2137 
2138 	/* If it is children lnode, decrement the
2139 	 * counter in its parent lnode
2140 	 */
2141 	if (ln->pln)
2142 		ln->pln->num_vports--;
2143 
2144 	/* Update root lnode pointer */
2145 	if (list_empty(&hw->sln_head))
2146 		hw->rln = NULL;
2147 	else
2148 		hw->rln = (struct csio_lnode *)csio_list_next(&hw->sln_head);
2149 
2150 	spin_unlock_irq(&hw->lock);
2151 
2152 	csio_lnode_to_hw(ln)	= NULL;
2153 	hw->num_lns--;
2154 }
2155