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