[SCSI] lpfc 8.3.2 : Addition of SLI4 Interface - Base Support
[deliverable/linux.git] / drivers / scsi / lpfc / lpfc_hbadisc.c
1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2008 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
6 * www.emulex.com *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
8 * *
9 * This program is free software; you can redistribute it and/or *
10 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
20 *******************************************************************/
21
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/kthread.h>
25 #include <linux/interrupt.h>
26
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_device.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_transport_fc.h>
31
32 #include "lpfc_hw4.h"
33 #include "lpfc_hw.h"
34 #include "lpfc_nl.h"
35 #include "lpfc_disc.h"
36 #include "lpfc_sli.h"
37 #include "lpfc_sli4.h"
38 #include "lpfc_scsi.h"
39 #include "lpfc.h"
40 #include "lpfc_logmsg.h"
41 #include "lpfc_crtn.h"
42 #include "lpfc_vport.h"
43 #include "lpfc_debugfs.h"
44
45 /* AlpaArray for assignment of scsid for scan-down and bind_method */
46 static uint8_t lpfcAlpaArray[] = {
47 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
48 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
49 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
50 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
51 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
52 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
53 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
54 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
55 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
56 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
57 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
58 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
59 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
60 };
61
62 static void lpfc_disc_timeout_handler(struct lpfc_vport *);
63 static void lpfc_disc_flush_list(struct lpfc_vport *vport);
64
65 void
66 lpfc_terminate_rport_io(struct fc_rport *rport)
67 {
68 struct lpfc_rport_data *rdata;
69 struct lpfc_nodelist * ndlp;
70 struct lpfc_hba *phba;
71
72 rdata = rport->dd_data;
73 ndlp = rdata->pnode;
74
75 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
76 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
77 printk(KERN_ERR "Cannot find remote node"
78 " to terminate I/O Data x%x\n",
79 rport->port_id);
80 return;
81 }
82
83 phba = ndlp->phba;
84
85 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
86 "rport terminate: sid:x%x did:x%x flg:x%x",
87 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
88
89 if (ndlp->nlp_sid != NLP_NO_SID) {
90 lpfc_sli_abort_iocb(ndlp->vport,
91 &phba->sli.ring[phba->sli.fcp_ring],
92 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
93 }
94 }
95
96 /*
97 * This function will be called when dev_loss_tmo fire.
98 */
99 void
100 lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
101 {
102 struct lpfc_rport_data *rdata;
103 struct lpfc_nodelist * ndlp;
104 struct lpfc_vport *vport;
105 struct lpfc_hba *phba;
106 struct lpfc_work_evt *evtp;
107 int put_node;
108 int put_rport;
109
110 rdata = rport->dd_data;
111 ndlp = rdata->pnode;
112 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
113 return;
114
115 vport = ndlp->vport;
116 phba = vport->phba;
117
118 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
119 "rport devlosscb: sid:x%x did:x%x flg:x%x",
120 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
121
122 /* Don't defer this if we are in the process of deleting the vport
123 * or unloading the driver. The unload will cleanup the node
124 * appropriately we just need to cleanup the ndlp rport info here.
125 */
126 if (vport->load_flag & FC_UNLOADING) {
127 put_node = rdata->pnode != NULL;
128 put_rport = ndlp->rport != NULL;
129 rdata->pnode = NULL;
130 ndlp->rport = NULL;
131 if (put_node)
132 lpfc_nlp_put(ndlp);
133 if (put_rport)
134 put_device(&rport->dev);
135 return;
136 }
137
138 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
139 return;
140
141 evtp = &ndlp->dev_loss_evt;
142
143 if (!list_empty(&evtp->evt_listp))
144 return;
145
146 spin_lock_irq(&phba->hbalock);
147 /* We need to hold the node by incrementing the reference
148 * count until this queued work is done
149 */
150 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
151 if (evtp->evt_arg1) {
152 evtp->evt = LPFC_EVT_DEV_LOSS;
153 list_add_tail(&evtp->evt_listp, &phba->work_list);
154 lpfc_worker_wake_up(phba);
155 }
156 spin_unlock_irq(&phba->hbalock);
157
158 return;
159 }
160
161 /*
162 * This function is called from the worker thread when dev_loss_tmo
163 * expire.
164 */
165 static void
166 lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
167 {
168 struct lpfc_rport_data *rdata;
169 struct fc_rport *rport;
170 struct lpfc_vport *vport;
171 struct lpfc_hba *phba;
172 uint8_t *name;
173 int put_node;
174 int put_rport;
175 int warn_on = 0;
176
177 rport = ndlp->rport;
178
179 if (!rport)
180 return;
181
182 rdata = rport->dd_data;
183 name = (uint8_t *) &ndlp->nlp_portname;
184 vport = ndlp->vport;
185 phba = vport->phba;
186
187 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
188 "rport devlosstmo:did:x%x type:x%x id:x%x",
189 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
190
191 /* Don't defer this if we are in the process of deleting the vport
192 * or unloading the driver. The unload will cleanup the node
193 * appropriately we just need to cleanup the ndlp rport info here.
194 */
195 if (vport->load_flag & FC_UNLOADING) {
196 if (ndlp->nlp_sid != NLP_NO_SID) {
197 /* flush the target */
198 lpfc_sli_abort_iocb(vport,
199 &phba->sli.ring[phba->sli.fcp_ring],
200 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
201 }
202 put_node = rdata->pnode != NULL;
203 put_rport = ndlp->rport != NULL;
204 rdata->pnode = NULL;
205 ndlp->rport = NULL;
206 if (put_node)
207 lpfc_nlp_put(ndlp);
208 if (put_rport)
209 put_device(&rport->dev);
210 return;
211 }
212
213 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
214 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
215 "0284 Devloss timeout Ignored on "
216 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
217 "NPort x%x\n",
218 *name, *(name+1), *(name+2), *(name+3),
219 *(name+4), *(name+5), *(name+6), *(name+7),
220 ndlp->nlp_DID);
221 return;
222 }
223
224 if (ndlp->nlp_type & NLP_FABRIC) {
225 /* We will clean up these Nodes in linkup */
226 put_node = rdata->pnode != NULL;
227 put_rport = ndlp->rport != NULL;
228 rdata->pnode = NULL;
229 ndlp->rport = NULL;
230 if (put_node)
231 lpfc_nlp_put(ndlp);
232 if (put_rport)
233 put_device(&rport->dev);
234 return;
235 }
236
237 if (ndlp->nlp_sid != NLP_NO_SID) {
238 warn_on = 1;
239 /* flush the target */
240 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
241 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
242 }
243
244 if (warn_on) {
245 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
246 "0203 Devloss timeout on "
247 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
248 "NPort x%06x Data: x%x x%x x%x\n",
249 *name, *(name+1), *(name+2), *(name+3),
250 *(name+4), *(name+5), *(name+6), *(name+7),
251 ndlp->nlp_DID, ndlp->nlp_flag,
252 ndlp->nlp_state, ndlp->nlp_rpi);
253 } else {
254 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
255 "0204 Devloss timeout on "
256 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
257 "NPort x%06x Data: x%x x%x x%x\n",
258 *name, *(name+1), *(name+2), *(name+3),
259 *(name+4), *(name+5), *(name+6), *(name+7),
260 ndlp->nlp_DID, ndlp->nlp_flag,
261 ndlp->nlp_state, ndlp->nlp_rpi);
262 }
263
264 put_node = rdata->pnode != NULL;
265 put_rport = ndlp->rport != NULL;
266 rdata->pnode = NULL;
267 ndlp->rport = NULL;
268 if (put_node)
269 lpfc_nlp_put(ndlp);
270 if (put_rport)
271 put_device(&rport->dev);
272
273 if (!(vport->load_flag & FC_UNLOADING) &&
274 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
275 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
276 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE))
277 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
278 }
279
280 /**
281 * lpfc_alloc_fast_evt - Allocates data structure for posting event
282 * @phba: Pointer to hba context object.
283 *
284 * This function is called from the functions which need to post
285 * events from interrupt context. This function allocates data
286 * structure required for posting event. It also keeps track of
287 * number of events pending and prevent event storm when there are
288 * too many events.
289 **/
290 struct lpfc_fast_path_event *
291 lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
292 struct lpfc_fast_path_event *ret;
293
294 /* If there are lot of fast event do not exhaust memory due to this */
295 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
296 return NULL;
297
298 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
299 GFP_ATOMIC);
300 if (ret)
301 atomic_inc(&phba->fast_event_count);
302 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
303 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
304 return ret;
305 }
306
307 /**
308 * lpfc_free_fast_evt - Frees event data structure
309 * @phba: Pointer to hba context object.
310 * @evt: Event object which need to be freed.
311 *
312 * This function frees the data structure required for posting
313 * events.
314 **/
315 void
316 lpfc_free_fast_evt(struct lpfc_hba *phba,
317 struct lpfc_fast_path_event *evt) {
318
319 atomic_dec(&phba->fast_event_count);
320 kfree(evt);
321 }
322
323 /**
324 * lpfc_send_fastpath_evt - Posts events generated from fast path
325 * @phba: Pointer to hba context object.
326 * @evtp: Event data structure.
327 *
328 * This function is called from worker thread, when the interrupt
329 * context need to post an event. This function posts the event
330 * to fc transport netlink interface.
331 **/
332 static void
333 lpfc_send_fastpath_evt(struct lpfc_hba *phba,
334 struct lpfc_work_evt *evtp)
335 {
336 unsigned long evt_category, evt_sub_category;
337 struct lpfc_fast_path_event *fast_evt_data;
338 char *evt_data;
339 uint32_t evt_data_size;
340 struct Scsi_Host *shost;
341
342 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
343 work_evt);
344
345 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
346 evt_sub_category = (unsigned long) fast_evt_data->un.
347 fabric_evt.subcategory;
348 shost = lpfc_shost_from_vport(fast_evt_data->vport);
349 if (evt_category == FC_REG_FABRIC_EVENT) {
350 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
351 evt_data = (char *) &fast_evt_data->un.read_check_error;
352 evt_data_size = sizeof(fast_evt_data->un.
353 read_check_error);
354 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
355 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
356 evt_data = (char *) &fast_evt_data->un.fabric_evt;
357 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
358 } else {
359 lpfc_free_fast_evt(phba, fast_evt_data);
360 return;
361 }
362 } else if (evt_category == FC_REG_SCSI_EVENT) {
363 switch (evt_sub_category) {
364 case LPFC_EVENT_QFULL:
365 case LPFC_EVENT_DEVBSY:
366 evt_data = (char *) &fast_evt_data->un.scsi_evt;
367 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
368 break;
369 case LPFC_EVENT_CHECK_COND:
370 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
371 evt_data_size = sizeof(fast_evt_data->un.
372 check_cond_evt);
373 break;
374 case LPFC_EVENT_VARQUEDEPTH:
375 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
376 evt_data_size = sizeof(fast_evt_data->un.
377 queue_depth_evt);
378 break;
379 default:
380 lpfc_free_fast_evt(phba, fast_evt_data);
381 return;
382 }
383 } else {
384 lpfc_free_fast_evt(phba, fast_evt_data);
385 return;
386 }
387
388 fc_host_post_vendor_event(shost,
389 fc_get_event_number(),
390 evt_data_size,
391 evt_data,
392 LPFC_NL_VENDOR_ID);
393
394 lpfc_free_fast_evt(phba, fast_evt_data);
395 return;
396 }
397
398 static void
399 lpfc_work_list_done(struct lpfc_hba *phba)
400 {
401 struct lpfc_work_evt *evtp = NULL;
402 struct lpfc_nodelist *ndlp;
403 int free_evt;
404
405 spin_lock_irq(&phba->hbalock);
406 while (!list_empty(&phba->work_list)) {
407 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
408 evt_listp);
409 spin_unlock_irq(&phba->hbalock);
410 free_evt = 1;
411 switch (evtp->evt) {
412 case LPFC_EVT_ELS_RETRY:
413 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
414 lpfc_els_retry_delay_handler(ndlp);
415 free_evt = 0; /* evt is part of ndlp */
416 /* decrement the node reference count held
417 * for this queued work
418 */
419 lpfc_nlp_put(ndlp);
420 break;
421 case LPFC_EVT_DEV_LOSS:
422 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
423 lpfc_dev_loss_tmo_handler(ndlp);
424 free_evt = 0;
425 /* decrement the node reference count held for
426 * this queued work
427 */
428 lpfc_nlp_put(ndlp);
429 break;
430 case LPFC_EVT_ONLINE:
431 if (phba->link_state < LPFC_LINK_DOWN)
432 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
433 else
434 *(int *) (evtp->evt_arg1) = 0;
435 complete((struct completion *)(evtp->evt_arg2));
436 break;
437 case LPFC_EVT_OFFLINE_PREP:
438 if (phba->link_state >= LPFC_LINK_DOWN)
439 lpfc_offline_prep(phba);
440 *(int *)(evtp->evt_arg1) = 0;
441 complete((struct completion *)(evtp->evt_arg2));
442 break;
443 case LPFC_EVT_OFFLINE:
444 lpfc_offline(phba);
445 lpfc_sli_brdrestart(phba);
446 *(int *)(evtp->evt_arg1) =
447 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
448 lpfc_unblock_mgmt_io(phba);
449 complete((struct completion *)(evtp->evt_arg2));
450 break;
451 case LPFC_EVT_WARM_START:
452 lpfc_offline(phba);
453 lpfc_reset_barrier(phba);
454 lpfc_sli_brdreset(phba);
455 lpfc_hba_down_post(phba);
456 *(int *)(evtp->evt_arg1) =
457 lpfc_sli_brdready(phba, HS_MBRDY);
458 lpfc_unblock_mgmt_io(phba);
459 complete((struct completion *)(evtp->evt_arg2));
460 break;
461 case LPFC_EVT_KILL:
462 lpfc_offline(phba);
463 *(int *)(evtp->evt_arg1)
464 = (phba->pport->stopped)
465 ? 0 : lpfc_sli_brdkill(phba);
466 lpfc_unblock_mgmt_io(phba);
467 complete((struct completion *)(evtp->evt_arg2));
468 break;
469 case LPFC_EVT_FASTPATH_MGMT_EVT:
470 lpfc_send_fastpath_evt(phba, evtp);
471 free_evt = 0;
472 break;
473 }
474 if (free_evt)
475 kfree(evtp);
476 spin_lock_irq(&phba->hbalock);
477 }
478 spin_unlock_irq(&phba->hbalock);
479
480 }
481
482 static void
483 lpfc_work_done(struct lpfc_hba *phba)
484 {
485 struct lpfc_sli_ring *pring;
486 uint32_t ha_copy, status, control, work_port_events;
487 struct lpfc_vport **vports;
488 struct lpfc_vport *vport;
489 int i;
490
491 spin_lock_irq(&phba->hbalock);
492 ha_copy = phba->work_ha;
493 phba->work_ha = 0;
494 spin_unlock_irq(&phba->hbalock);
495
496 /* First, try to post the next mailbox command to SLI4 device */
497 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
498 lpfc_sli4_post_async_mbox(phba);
499
500 if (ha_copy & HA_ERATT)
501 /* Handle the error attention event */
502 lpfc_handle_eratt(phba);
503
504 if (ha_copy & HA_MBATT)
505 lpfc_sli_handle_mb_event(phba);
506
507 if (ha_copy & HA_LATT)
508 lpfc_handle_latt(phba);
509
510 /* Process SLI4 events */
511 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
512 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
513 lpfc_sli4_fcp_xri_abort_event_proc(phba);
514 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
515 lpfc_sli4_els_xri_abort_event_proc(phba);
516 if (phba->hba_flag & ASYNC_EVENT)
517 lpfc_sli4_async_event_proc(phba);
518 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
519 spin_lock_irq(&phba->hbalock);
520 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
521 spin_unlock_irq(&phba->hbalock);
522 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
523 }
524 if (phba->hba_flag & HBA_RECEIVE_BUFFER)
525 lpfc_sli4_handle_received_buffer(phba);
526 }
527
528 vports = lpfc_create_vport_work_array(phba);
529 if (vports != NULL)
530 for (i = 0; i <= phba->max_vports; i++) {
531 /*
532 * We could have no vports in array if unloading, so if
533 * this happens then just use the pport
534 */
535 if (vports[i] == NULL && i == 0)
536 vport = phba->pport;
537 else
538 vport = vports[i];
539 if (vport == NULL)
540 break;
541 spin_lock_irq(&vport->work_port_lock);
542 work_port_events = vport->work_port_events;
543 vport->work_port_events &= ~work_port_events;
544 spin_unlock_irq(&vport->work_port_lock);
545 if (work_port_events & WORKER_DISC_TMO)
546 lpfc_disc_timeout_handler(vport);
547 if (work_port_events & WORKER_ELS_TMO)
548 lpfc_els_timeout_handler(vport);
549 if (work_port_events & WORKER_HB_TMO)
550 lpfc_hb_timeout_handler(phba);
551 if (work_port_events & WORKER_MBOX_TMO)
552 lpfc_mbox_timeout_handler(phba);
553 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
554 lpfc_unblock_fabric_iocbs(phba);
555 if (work_port_events & WORKER_FDMI_TMO)
556 lpfc_fdmi_timeout_handler(vport);
557 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
558 lpfc_ramp_down_queue_handler(phba);
559 if (work_port_events & WORKER_RAMP_UP_QUEUE)
560 lpfc_ramp_up_queue_handler(phba);
561 }
562 lpfc_destroy_vport_work_array(phba, vports);
563
564 pring = &phba->sli.ring[LPFC_ELS_RING];
565 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
566 status >>= (4*LPFC_ELS_RING);
567 if ((status & HA_RXMASK)
568 || (pring->flag & LPFC_DEFERRED_RING_EVENT)) {
569 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
570 pring->flag |= LPFC_DEFERRED_RING_EVENT;
571 /* Set the lpfc data pending flag */
572 set_bit(LPFC_DATA_READY, &phba->data_flags);
573 } else {
574 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
575 lpfc_sli_handle_slow_ring_event(phba, pring,
576 (status &
577 HA_RXMASK));
578 }
579 /*
580 * Turn on Ring interrupts
581 */
582 if (phba->sli_rev <= LPFC_SLI_REV3) {
583 spin_lock_irq(&phba->hbalock);
584 control = readl(phba->HCregaddr);
585 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
586 lpfc_debugfs_slow_ring_trc(phba,
587 "WRK Enable ring: cntl:x%x hacopy:x%x",
588 control, ha_copy, 0);
589
590 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
591 writel(control, phba->HCregaddr);
592 readl(phba->HCregaddr); /* flush */
593 } else {
594 lpfc_debugfs_slow_ring_trc(phba,
595 "WRK Ring ok: cntl:x%x hacopy:x%x",
596 control, ha_copy, 0);
597 }
598 spin_unlock_irq(&phba->hbalock);
599 }
600 }
601 lpfc_work_list_done(phba);
602 }
603
604 int
605 lpfc_do_work(void *p)
606 {
607 struct lpfc_hba *phba = p;
608 int rc;
609
610 set_user_nice(current, -20);
611 phba->data_flags = 0;
612
613 while (!kthread_should_stop()) {
614 /* wait and check worker queue activities */
615 rc = wait_event_interruptible(phba->work_waitq,
616 (test_and_clear_bit(LPFC_DATA_READY,
617 &phba->data_flags)
618 || kthread_should_stop()));
619 /* Signal wakeup shall terminate the worker thread */
620 if (rc) {
621 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
622 "0433 Wakeup on signal: rc=x%x\n", rc);
623 break;
624 }
625
626 /* Attend pending lpfc data processing */
627 lpfc_work_done(phba);
628 }
629 phba->worker_thread = NULL;
630 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
631 "0432 Worker thread stopped.\n");
632 return 0;
633 }
634
635 /*
636 * This is only called to handle FC worker events. Since this a rare
637 * occurance, we allocate a struct lpfc_work_evt structure here instead of
638 * embedding it in the IOCB.
639 */
640 int
641 lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
642 uint32_t evt)
643 {
644 struct lpfc_work_evt *evtp;
645 unsigned long flags;
646
647 /*
648 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
649 * be queued to worker thread for processing
650 */
651 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
652 if (!evtp)
653 return 0;
654
655 evtp->evt_arg1 = arg1;
656 evtp->evt_arg2 = arg2;
657 evtp->evt = evt;
658
659 spin_lock_irqsave(&phba->hbalock, flags);
660 list_add_tail(&evtp->evt_listp, &phba->work_list);
661 spin_unlock_irqrestore(&phba->hbalock, flags);
662
663 lpfc_worker_wake_up(phba);
664
665 return 1;
666 }
667
668 void
669 lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
670 {
671 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
672 struct lpfc_hba *phba = vport->phba;
673 struct lpfc_nodelist *ndlp, *next_ndlp;
674 int rc;
675
676 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
677 if (!NLP_CHK_NODE_ACT(ndlp))
678 continue;
679 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
680 continue;
681 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
682 ((vport->port_type == LPFC_NPIV_PORT) &&
683 (ndlp->nlp_DID == NameServer_DID)))
684 lpfc_unreg_rpi(vport, ndlp);
685
686 /* Leave Fabric nodes alone on link down */
687 if (!remove && ndlp->nlp_type & NLP_FABRIC)
688 continue;
689 rc = lpfc_disc_state_machine(vport, ndlp, NULL,
690 remove
691 ? NLP_EVT_DEVICE_RM
692 : NLP_EVT_DEVICE_RECOVERY);
693 }
694 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
695 lpfc_mbx_unreg_vpi(vport);
696 spin_lock_irq(shost->host_lock);
697 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
698 spin_unlock_irq(shost->host_lock);
699 }
700 }
701
702 void
703 lpfc_port_link_failure(struct lpfc_vport *vport)
704 {
705 /* Cleanup any outstanding RSCN activity */
706 lpfc_els_flush_rscn(vport);
707
708 /* Cleanup any outstanding ELS commands */
709 lpfc_els_flush_cmd(vport);
710
711 lpfc_cleanup_rpis(vport, 0);
712
713 /* Turn off discovery timer if its running */
714 lpfc_can_disctmo(vport);
715 }
716
717 void
718 lpfc_linkdown_port(struct lpfc_vport *vport)
719 {
720 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
721
722 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKDOWN, 0);
723
724 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
725 "Link Down: state:x%x rtry:x%x flg:x%x",
726 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
727
728 lpfc_port_link_failure(vport);
729
730 }
731
732 int
733 lpfc_linkdown(struct lpfc_hba *phba)
734 {
735 struct lpfc_vport *vport = phba->pport;
736 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
737 struct lpfc_vport **vports;
738 LPFC_MBOXQ_t *mb;
739 int i;
740
741 if (phba->link_state == LPFC_LINK_DOWN)
742 return 0;
743 spin_lock_irq(&phba->hbalock);
744 if (phba->link_state > LPFC_LINK_DOWN) {
745 phba->link_state = LPFC_LINK_DOWN;
746 phba->pport->fc_flag &= ~FC_LBIT;
747 }
748 spin_unlock_irq(&phba->hbalock);
749 vports = lpfc_create_vport_work_array(phba);
750 if (vports != NULL)
751 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
752 /* Issue a LINK DOWN event to all nodes */
753 lpfc_linkdown_port(vports[i]);
754 }
755 lpfc_destroy_vport_work_array(phba, vports);
756 /* Clean up any firmware default rpi's */
757 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
758 if (mb) {
759 lpfc_unreg_did(phba, 0xffff, 0xffffffff, mb);
760 mb->vport = vport;
761 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
762 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
763 == MBX_NOT_FINISHED) {
764 mempool_free(mb, phba->mbox_mem_pool);
765 }
766 }
767
768 /* Setup myDID for link up if we are in pt2pt mode */
769 if (phba->pport->fc_flag & FC_PT2PT) {
770 phba->pport->fc_myDID = 0;
771 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
772 if (mb) {
773 lpfc_config_link(phba, mb);
774 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
775 mb->vport = vport;
776 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
777 == MBX_NOT_FINISHED) {
778 mempool_free(mb, phba->mbox_mem_pool);
779 }
780 }
781 spin_lock_irq(shost->host_lock);
782 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
783 spin_unlock_irq(shost->host_lock);
784 }
785
786 return 0;
787 }
788
789 static void
790 lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
791 {
792 struct lpfc_nodelist *ndlp;
793
794 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
795 if (!NLP_CHK_NODE_ACT(ndlp))
796 continue;
797 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
798 continue;
799 if (ndlp->nlp_type & NLP_FABRIC) {
800 /* On Linkup its safe to clean up the ndlp
801 * from Fabric connections.
802 */
803 if (ndlp->nlp_DID != Fabric_DID)
804 lpfc_unreg_rpi(vport, ndlp);
805 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
806 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
807 /* Fail outstanding IO now since device is
808 * marked for PLOGI.
809 */
810 lpfc_unreg_rpi(vport, ndlp);
811 }
812 }
813 }
814
815 static void
816 lpfc_linkup_port(struct lpfc_vport *vport)
817 {
818 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
819 struct lpfc_hba *phba = vport->phba;
820
821 if ((vport->load_flag & FC_UNLOADING) != 0)
822 return;
823
824 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
825 "Link Up: top:x%x speed:x%x flg:x%x",
826 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
827
828 /* If NPIV is not enabled, only bring the physical port up */
829 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
830 (vport != phba->pport))
831 return;
832
833 fc_host_post_event(shost, fc_get_event_number(), FCH_EVT_LINKUP, 0);
834
835 spin_lock_irq(shost->host_lock);
836 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
837 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
838 vport->fc_flag |= FC_NDISC_ACTIVE;
839 vport->fc_ns_retry = 0;
840 spin_unlock_irq(shost->host_lock);
841
842 if (vport->fc_flag & FC_LBIT)
843 lpfc_linkup_cleanup_nodes(vport);
844
845 }
846
847 static int
848 lpfc_linkup(struct lpfc_hba *phba)
849 {
850 struct lpfc_vport **vports;
851 int i;
852
853 phba->link_state = LPFC_LINK_UP;
854
855 /* Unblock fabric iocbs if they are blocked */
856 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
857 del_timer_sync(&phba->fabric_block_timer);
858
859 vports = lpfc_create_vport_work_array(phba);
860 if (vports != NULL)
861 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++)
862 lpfc_linkup_port(vports[i]);
863 lpfc_destroy_vport_work_array(phba, vports);
864 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
865 lpfc_issue_clear_la(phba, phba->pport);
866
867 return 0;
868 }
869
870 /*
871 * This routine handles processing a CLEAR_LA mailbox
872 * command upon completion. It is setup in the LPFC_MBOXQ
873 * as the completion routine when the command is
874 * handed off to the SLI layer.
875 */
876 static void
877 lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
878 {
879 struct lpfc_vport *vport = pmb->vport;
880 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
881 struct lpfc_sli *psli = &phba->sli;
882 MAILBOX_t *mb = &pmb->mb;
883 uint32_t control;
884
885 /* Since we don't do discovery right now, turn these off here */
886 psli->ring[psli->extra_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
887 psli->ring[psli->fcp_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
888 psli->ring[psli->next_ring].flag &= ~LPFC_STOP_IOCB_EVENT;
889
890 /* Check for error */
891 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
892 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
893 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
894 "0320 CLEAR_LA mbxStatus error x%x hba "
895 "state x%x\n",
896 mb->mbxStatus, vport->port_state);
897 phba->link_state = LPFC_HBA_ERROR;
898 goto out;
899 }
900
901 if (vport->port_type == LPFC_PHYSICAL_PORT)
902 phba->link_state = LPFC_HBA_READY;
903
904 spin_lock_irq(&phba->hbalock);
905 psli->sli_flag |= LPFC_PROCESS_LA;
906 control = readl(phba->HCregaddr);
907 control |= HC_LAINT_ENA;
908 writel(control, phba->HCregaddr);
909 readl(phba->HCregaddr); /* flush */
910 spin_unlock_irq(&phba->hbalock);
911 mempool_free(pmb, phba->mbox_mem_pool);
912 return;
913
914 out:
915 /* Device Discovery completes */
916 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
917 "0225 Device Discovery completes\n");
918 mempool_free(pmb, phba->mbox_mem_pool);
919
920 spin_lock_irq(shost->host_lock);
921 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
922 spin_unlock_irq(shost->host_lock);
923
924 lpfc_can_disctmo(vport);
925
926 /* turn on Link Attention interrupts */
927
928 spin_lock_irq(&phba->hbalock);
929 psli->sli_flag |= LPFC_PROCESS_LA;
930 control = readl(phba->HCregaddr);
931 control |= HC_LAINT_ENA;
932 writel(control, phba->HCregaddr);
933 readl(phba->HCregaddr); /* flush */
934 spin_unlock_irq(&phba->hbalock);
935
936 return;
937 }
938
939
940 static void
941 lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
942 {
943 struct lpfc_vport *vport = pmb->vport;
944
945 if (pmb->mb.mbxStatus)
946 goto out;
947
948 mempool_free(pmb, phba->mbox_mem_pool);
949
950 if (phba->fc_topology == TOPOLOGY_LOOP &&
951 vport->fc_flag & FC_PUBLIC_LOOP &&
952 !(vport->fc_flag & FC_LBIT)) {
953 /* Need to wait for FAN - use discovery timer
954 * for timeout. port_state is identically
955 * LPFC_LOCAL_CFG_LINK while waiting for FAN
956 */
957 lpfc_set_disctmo(vport);
958 return;
959 }
960
961 /* Start discovery by sending a FLOGI. port_state is identically
962 * LPFC_FLOGI while waiting for FLOGI cmpl
963 */
964 if (vport->port_state != LPFC_FLOGI) {
965 lpfc_initial_flogi(vport);
966 }
967 return;
968
969 out:
970 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
971 "0306 CONFIG_LINK mbxStatus error x%x "
972 "HBA state x%x\n",
973 pmb->mb.mbxStatus, vport->port_state);
974 mempool_free(pmb, phba->mbox_mem_pool);
975
976 lpfc_linkdown(phba);
977
978 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
979 "0200 CONFIG_LINK bad hba state x%x\n",
980 vport->port_state);
981
982 lpfc_issue_clear_la(phba, vport);
983 return;
984 }
985
986 static void
987 lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
988 {
989 MAILBOX_t *mb = &pmb->mb;
990 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
991 struct lpfc_vport *vport = pmb->vport;
992
993
994 /* Check for error */
995 if (mb->mbxStatus) {
996 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
997 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
998 "0319 READ_SPARAM mbxStatus error x%x "
999 "hba state x%x>\n",
1000 mb->mbxStatus, vport->port_state);
1001 lpfc_linkdown(phba);
1002 goto out;
1003 }
1004
1005 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
1006 sizeof (struct serv_parm));
1007 if (phba->cfg_soft_wwnn)
1008 u64_to_wwn(phba->cfg_soft_wwnn,
1009 vport->fc_sparam.nodeName.u.wwn);
1010 if (phba->cfg_soft_wwpn)
1011 u64_to_wwn(phba->cfg_soft_wwpn,
1012 vport->fc_sparam.portName.u.wwn);
1013 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
1014 sizeof(vport->fc_nodename));
1015 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
1016 sizeof(vport->fc_portname));
1017 if (vport->port_type == LPFC_PHYSICAL_PORT) {
1018 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
1019 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
1020 }
1021
1022 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1023 kfree(mp);
1024 mempool_free(pmb, phba->mbox_mem_pool);
1025 return;
1026
1027 out:
1028 pmb->context1 = NULL;
1029 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1030 kfree(mp);
1031 lpfc_issue_clear_la(phba, vport);
1032 mempool_free(pmb, phba->mbox_mem_pool);
1033 return;
1034 }
1035
1036 static void
1037 lpfc_mbx_process_link_up(struct lpfc_hba *phba, READ_LA_VAR *la)
1038 {
1039 struct lpfc_vport *vport = phba->pport;
1040 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox;
1041 int i;
1042 struct lpfc_dmabuf *mp;
1043 int rc;
1044
1045 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1046 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1047
1048 spin_lock_irq(&phba->hbalock);
1049 switch (la->UlnkSpeed) {
1050 case LA_1GHZ_LINK:
1051 phba->fc_linkspeed = LA_1GHZ_LINK;
1052 break;
1053 case LA_2GHZ_LINK:
1054 phba->fc_linkspeed = LA_2GHZ_LINK;
1055 break;
1056 case LA_4GHZ_LINK:
1057 phba->fc_linkspeed = LA_4GHZ_LINK;
1058 break;
1059 case LA_8GHZ_LINK:
1060 phba->fc_linkspeed = LA_8GHZ_LINK;
1061 break;
1062 default:
1063 phba->fc_linkspeed = LA_UNKNW_LINK;
1064 break;
1065 }
1066
1067 phba->fc_topology = la->topology;
1068 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
1069
1070 if (phba->fc_topology == TOPOLOGY_LOOP) {
1071 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
1072
1073 if (phba->cfg_enable_npiv)
1074 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1075 "1309 Link Up Event npiv not supported in loop "
1076 "topology\n");
1077 /* Get Loop Map information */
1078 if (la->il)
1079 vport->fc_flag |= FC_LBIT;
1080
1081 vport->fc_myDID = la->granted_AL_PA;
1082 i = la->un.lilpBde64.tus.f.bdeSize;
1083
1084 if (i == 0) {
1085 phba->alpa_map[0] = 0;
1086 } else {
1087 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
1088 int numalpa, j, k;
1089 union {
1090 uint8_t pamap[16];
1091 struct {
1092 uint32_t wd1;
1093 uint32_t wd2;
1094 uint32_t wd3;
1095 uint32_t wd4;
1096 } pa;
1097 } un;
1098 numalpa = phba->alpa_map[0];
1099 j = 0;
1100 while (j < numalpa) {
1101 memset(un.pamap, 0, 16);
1102 for (k = 1; j < numalpa; k++) {
1103 un.pamap[k - 1] =
1104 phba->alpa_map[j + 1];
1105 j++;
1106 if (k == 16)
1107 break;
1108 }
1109 /* Link Up Event ALPA map */
1110 lpfc_printf_log(phba,
1111 KERN_WARNING,
1112 LOG_LINK_EVENT,
1113 "1304 Link Up Event "
1114 "ALPA map Data: x%x "
1115 "x%x x%x x%x\n",
1116 un.pa.wd1, un.pa.wd2,
1117 un.pa.wd3, un.pa.wd4);
1118 }
1119 }
1120 }
1121 } else {
1122 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
1123 if (phba->max_vpi && phba->cfg_enable_npiv &&
1124 (phba->sli_rev == 3))
1125 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
1126 }
1127 vport->fc_myDID = phba->fc_pref_DID;
1128 vport->fc_flag |= FC_LBIT;
1129 }
1130 spin_unlock_irq(&phba->hbalock);
1131
1132 lpfc_linkup(phba);
1133 if (sparam_mbox) {
1134 lpfc_read_sparam(phba, sparam_mbox, 0);
1135 sparam_mbox->vport = vport;
1136 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
1137 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
1138 if (rc == MBX_NOT_FINISHED) {
1139 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
1140 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1141 kfree(mp);
1142 mempool_free(sparam_mbox, phba->mbox_mem_pool);
1143 if (cfglink_mbox)
1144 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
1145 goto out;
1146 }
1147 }
1148
1149 if (cfglink_mbox) {
1150 vport->port_state = LPFC_LOCAL_CFG_LINK;
1151 lpfc_config_link(phba, cfglink_mbox);
1152 cfglink_mbox->vport = vport;
1153 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
1154 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
1155 if (rc != MBX_NOT_FINISHED)
1156 return;
1157 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
1158 }
1159 out:
1160 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1161 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1162 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
1163 vport->port_state, sparam_mbox, cfglink_mbox);
1164 lpfc_issue_clear_la(phba, vport);
1165 return;
1166 }
1167
1168 static void
1169 lpfc_enable_la(struct lpfc_hba *phba)
1170 {
1171 uint32_t control;
1172 struct lpfc_sli *psli = &phba->sli;
1173 spin_lock_irq(&phba->hbalock);
1174 psli->sli_flag |= LPFC_PROCESS_LA;
1175 if (phba->sli_rev <= LPFC_SLI_REV3) {
1176 control = readl(phba->HCregaddr);
1177 control |= HC_LAINT_ENA;
1178 writel(control, phba->HCregaddr);
1179 readl(phba->HCregaddr); /* flush */
1180 }
1181 spin_unlock_irq(&phba->hbalock);
1182 }
1183
1184 static void
1185 lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
1186 {
1187 lpfc_linkdown(phba);
1188 lpfc_enable_la(phba);
1189 /* turn on Link Attention interrupts - no CLEAR_LA needed */
1190 }
1191
1192
1193 /*
1194 * This routine handles processing a READ_LA mailbox
1195 * command upon completion. It is setup in the LPFC_MBOXQ
1196 * as the completion routine when the command is
1197 * handed off to the SLI layer.
1198 */
1199 void
1200 lpfc_mbx_cmpl_read_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1201 {
1202 struct lpfc_vport *vport = pmb->vport;
1203 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1204 READ_LA_VAR *la;
1205 MAILBOX_t *mb = &pmb->mb;
1206 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1207
1208 /* Unblock ELS traffic */
1209 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1210 /* Check for error */
1211 if (mb->mbxStatus) {
1212 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1213 "1307 READ_LA mbox error x%x state x%x\n",
1214 mb->mbxStatus, vport->port_state);
1215 lpfc_mbx_issue_link_down(phba);
1216 phba->link_state = LPFC_HBA_ERROR;
1217 goto lpfc_mbx_cmpl_read_la_free_mbuf;
1218 }
1219
1220 la = (READ_LA_VAR *) & pmb->mb.un.varReadLA;
1221
1222 memcpy(&phba->alpa_map[0], mp->virt, 128);
1223
1224 spin_lock_irq(shost->host_lock);
1225 if (la->pb)
1226 vport->fc_flag |= FC_BYPASSED_MODE;
1227 else
1228 vport->fc_flag &= ~FC_BYPASSED_MODE;
1229 spin_unlock_irq(shost->host_lock);
1230
1231 if (((phba->fc_eventTag + 1) < la->eventTag) ||
1232 (phba->fc_eventTag == la->eventTag)) {
1233 phba->fc_stat.LinkMultiEvent++;
1234 if (la->attType == AT_LINK_UP)
1235 if (phba->fc_eventTag != 0)
1236 lpfc_linkdown(phba);
1237 }
1238
1239 phba->fc_eventTag = la->eventTag;
1240 if (la->mm)
1241 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
1242 else
1243 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
1244
1245 if (la->attType == AT_LINK_UP && (!la->mm)) {
1246 phba->fc_stat.LinkUp++;
1247 if (phba->link_flag & LS_LOOPBACK_MODE) {
1248 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1249 "1306 Link Up Event in loop back mode "
1250 "x%x received Data: x%x x%x x%x x%x\n",
1251 la->eventTag, phba->fc_eventTag,
1252 la->granted_AL_PA, la->UlnkSpeed,
1253 phba->alpa_map[0]);
1254 } else {
1255 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1256 "1303 Link Up Event x%x received "
1257 "Data: x%x x%x x%x x%x x%x x%x %d\n",
1258 la->eventTag, phba->fc_eventTag,
1259 la->granted_AL_PA, la->UlnkSpeed,
1260 phba->alpa_map[0],
1261 la->mm, la->fa,
1262 phba->wait_4_mlo_maint_flg);
1263 }
1264 lpfc_mbx_process_link_up(phba, la);
1265 } else if (la->attType == AT_LINK_DOWN) {
1266 phba->fc_stat.LinkDown++;
1267 if (phba->link_flag & LS_LOOPBACK_MODE) {
1268 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1269 "1308 Link Down Event in loop back mode "
1270 "x%x received "
1271 "Data: x%x x%x x%x\n",
1272 la->eventTag, phba->fc_eventTag,
1273 phba->pport->port_state, vport->fc_flag);
1274 }
1275 else {
1276 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1277 "1305 Link Down Event x%x received "
1278 "Data: x%x x%x x%x x%x x%x\n",
1279 la->eventTag, phba->fc_eventTag,
1280 phba->pport->port_state, vport->fc_flag,
1281 la->mm, la->fa);
1282 }
1283 lpfc_mbx_issue_link_down(phba);
1284 }
1285 if (la->mm && la->attType == AT_LINK_UP) {
1286 if (phba->link_state != LPFC_LINK_DOWN) {
1287 phba->fc_stat.LinkDown++;
1288 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1289 "1312 Link Down Event x%x received "
1290 "Data: x%x x%x x%x\n",
1291 la->eventTag, phba->fc_eventTag,
1292 phba->pport->port_state, vport->fc_flag);
1293 lpfc_mbx_issue_link_down(phba);
1294 } else
1295 lpfc_enable_la(phba);
1296
1297 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1298 "1310 Menlo Maint Mode Link up Event x%x rcvd "
1299 "Data: x%x x%x x%x\n",
1300 la->eventTag, phba->fc_eventTag,
1301 phba->pport->port_state, vport->fc_flag);
1302 /*
1303 * The cmnd that triggered this will be waiting for this
1304 * signal.
1305 */
1306 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
1307 if (phba->wait_4_mlo_maint_flg) {
1308 phba->wait_4_mlo_maint_flg = 0;
1309 wake_up_interruptible(&phba->wait_4_mlo_m_q);
1310 }
1311 }
1312
1313 if (la->fa) {
1314 if (la->mm)
1315 lpfc_issue_clear_la(phba, vport);
1316 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1317 "1311 fa %d\n", la->fa);
1318 }
1319
1320 lpfc_mbx_cmpl_read_la_free_mbuf:
1321 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1322 kfree(mp);
1323 mempool_free(pmb, phba->mbox_mem_pool);
1324 return;
1325 }
1326
1327 /*
1328 * This routine handles processing a REG_LOGIN mailbox
1329 * command upon completion. It is setup in the LPFC_MBOXQ
1330 * as the completion routine when the command is
1331 * handed off to the SLI layer.
1332 */
1333 void
1334 lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1335 {
1336 struct lpfc_vport *vport = pmb->vport;
1337 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1338 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
1339
1340 pmb->context1 = NULL;
1341
1342 /* Good status, call state machine */
1343 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
1344 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1345 kfree(mp);
1346 mempool_free(pmb, phba->mbox_mem_pool);
1347 /* decrement the node reference count held for this callback
1348 * function.
1349 */
1350 lpfc_nlp_put(ndlp);
1351
1352 return;
1353 }
1354
1355 static void
1356 lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1357 {
1358 MAILBOX_t *mb = &pmb->mb;
1359 struct lpfc_vport *vport = pmb->vport;
1360 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1361
1362 switch (mb->mbxStatus) {
1363 case 0x0011:
1364 case 0x0020:
1365 case 0x9700:
1366 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1367 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
1368 mb->mbxStatus);
1369 break;
1370 }
1371 vport->unreg_vpi_cmpl = VPORT_OK;
1372 mempool_free(pmb, phba->mbox_mem_pool);
1373 /*
1374 * This shost reference might have been taken at the beginning of
1375 * lpfc_vport_delete()
1376 */
1377 if (vport->load_flag & FC_UNLOADING)
1378 scsi_host_put(shost);
1379 }
1380
1381 int
1382 lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
1383 {
1384 struct lpfc_hba *phba = vport->phba;
1385 LPFC_MBOXQ_t *mbox;
1386 int rc;
1387
1388 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1389 if (!mbox)
1390 return 1;
1391
1392 lpfc_unreg_vpi(phba, vport->vpi, mbox);
1393 mbox->vport = vport;
1394 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
1395 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
1396 if (rc == MBX_NOT_FINISHED) {
1397 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
1398 "1800 Could not issue unreg_vpi\n");
1399 mempool_free(mbox, phba->mbox_mem_pool);
1400 vport->unreg_vpi_cmpl = VPORT_ERROR;
1401 return rc;
1402 }
1403 return 0;
1404 }
1405
1406 static void
1407 lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1408 {
1409 struct lpfc_vport *vport = pmb->vport;
1410 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1411 MAILBOX_t *mb = &pmb->mb;
1412
1413 switch (mb->mbxStatus) {
1414 case 0x0011:
1415 case 0x9601:
1416 case 0x9602:
1417 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1418 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
1419 mb->mbxStatus);
1420 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1421 spin_lock_irq(shost->host_lock);
1422 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1423 spin_unlock_irq(shost->host_lock);
1424 vport->fc_myDID = 0;
1425 goto out;
1426 }
1427
1428 vport->num_disc_nodes = 0;
1429 /* go thru NPR list and issue ELS PLOGIs */
1430 if (vport->fc_npr_cnt)
1431 lpfc_els_disc_plogi(vport);
1432
1433 if (!vport->num_disc_nodes) {
1434 spin_lock_irq(shost->host_lock);
1435 vport->fc_flag &= ~FC_NDISC_ACTIVE;
1436 spin_unlock_irq(shost->host_lock);
1437 lpfc_can_disctmo(vport);
1438 }
1439 vport->port_state = LPFC_VPORT_READY;
1440
1441 out:
1442 mempool_free(pmb, phba->mbox_mem_pool);
1443 return;
1444 }
1445
1446 /*
1447 * This routine handles processing a Fabric REG_LOGIN mailbox
1448 * command upon completion. It is setup in the LPFC_MBOXQ
1449 * as the completion routine when the command is
1450 * handed off to the SLI layer.
1451 */
1452 void
1453 lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1454 {
1455 struct lpfc_vport *vport = pmb->vport;
1456 MAILBOX_t *mb = &pmb->mb;
1457 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1458 struct lpfc_nodelist *ndlp;
1459 struct lpfc_vport **vports;
1460 int i;
1461
1462 ndlp = (struct lpfc_nodelist *) pmb->context2;
1463 pmb->context1 = NULL;
1464 pmb->context2 = NULL;
1465 if (mb->mbxStatus) {
1466 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1467 kfree(mp);
1468 mempool_free(pmb, phba->mbox_mem_pool);
1469
1470 if (phba->fc_topology == TOPOLOGY_LOOP) {
1471 /* FLOGI failed, use loop map to make discovery list */
1472 lpfc_disc_list_loopmap(vport);
1473
1474 /* Start discovery */
1475 lpfc_disc_start(vport);
1476 /* Decrement the reference count to ndlp after the
1477 * reference to the ndlp are done.
1478 */
1479 lpfc_nlp_put(ndlp);
1480 return;
1481 }
1482
1483 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1484 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1485 "0258 Register Fabric login error: 0x%x\n",
1486 mb->mbxStatus);
1487 /* Decrement the reference count to ndlp after the reference
1488 * to the ndlp are done.
1489 */
1490 lpfc_nlp_put(ndlp);
1491 return;
1492 }
1493
1494 ndlp->nlp_rpi = mb->un.varWords[0];
1495 ndlp->nlp_type |= NLP_FABRIC;
1496 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1497
1498 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
1499 vports = lpfc_create_vport_work_array(phba);
1500 if (vports != NULL)
1501 for(i = 0;
1502 i <= phba->max_vpi && vports[i] != NULL;
1503 i++) {
1504 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
1505 continue;
1506 if (phba->fc_topology == TOPOLOGY_LOOP) {
1507 lpfc_vport_set_state(vports[i],
1508 FC_VPORT_LINKDOWN);
1509 continue;
1510 }
1511 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
1512 lpfc_initial_fdisc(vports[i]);
1513 else {
1514 lpfc_vport_set_state(vports[i],
1515 FC_VPORT_NO_FABRIC_SUPP);
1516 lpfc_printf_vlog(vport, KERN_ERR,
1517 LOG_ELS,
1518 "0259 No NPIV "
1519 "Fabric support\n");
1520 }
1521 }
1522 lpfc_destroy_vport_work_array(phba, vports);
1523 lpfc_do_scr_ns_plogi(phba, vport);
1524 }
1525
1526 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1527 kfree(mp);
1528 mempool_free(pmb, phba->mbox_mem_pool);
1529
1530 /* Drop the reference count from the mbox at the end after
1531 * all the current reference to the ndlp have been done.
1532 */
1533 lpfc_nlp_put(ndlp);
1534 return;
1535 }
1536
1537 /*
1538 * This routine handles processing a NameServer REG_LOGIN mailbox
1539 * command upon completion. It is setup in the LPFC_MBOXQ
1540 * as the completion routine when the command is
1541 * handed off to the SLI layer.
1542 */
1543 void
1544 lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1545 {
1546 MAILBOX_t *mb = &pmb->mb;
1547 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
1548 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
1549 struct lpfc_vport *vport = pmb->vport;
1550
1551 if (mb->mbxStatus) {
1552 out:
1553 /* decrement the node reference count held for this
1554 * callback function.
1555 */
1556 lpfc_nlp_put(ndlp);
1557 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1558 kfree(mp);
1559 mempool_free(pmb, phba->mbox_mem_pool);
1560
1561 /* If no other thread is using the ndlp, free it */
1562 lpfc_nlp_not_used(ndlp);
1563
1564 if (phba->fc_topology == TOPOLOGY_LOOP) {
1565 /*
1566 * RegLogin failed, use loop map to make discovery
1567 * list
1568 */
1569 lpfc_disc_list_loopmap(vport);
1570
1571 /* Start discovery */
1572 lpfc_disc_start(vport);
1573 return;
1574 }
1575 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
1576 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1577 "0260 Register NameServer error: 0x%x\n",
1578 mb->mbxStatus);
1579 return;
1580 }
1581
1582 pmb->context1 = NULL;
1583
1584 ndlp->nlp_rpi = mb->un.varWords[0];
1585 ndlp->nlp_type |= NLP_FABRIC;
1586 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1587
1588 if (vport->port_state < LPFC_VPORT_READY) {
1589 /* Link up discovery requires Fabric registration. */
1590 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, 0); /* Do this first! */
1591 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
1592 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
1593 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
1594 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
1595
1596 /* Issue SCR just before NameServer GID_FT Query */
1597 lpfc_issue_els_scr(vport, SCR_DID, 0);
1598 }
1599
1600 vport->fc_ns_retry = 0;
1601 /* Good status, issue CT Request to NameServer */
1602 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0)) {
1603 /* Cannot issue NameServer Query, so finish up discovery */
1604 goto out;
1605 }
1606
1607 /* decrement the node reference count held for this
1608 * callback function.
1609 */
1610 lpfc_nlp_put(ndlp);
1611 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1612 kfree(mp);
1613 mempool_free(pmb, phba->mbox_mem_pool);
1614
1615 return;
1616 }
1617
1618 static void
1619 lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1620 {
1621 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1622 struct fc_rport *rport;
1623 struct lpfc_rport_data *rdata;
1624 struct fc_rport_identifiers rport_ids;
1625 struct lpfc_hba *phba = vport->phba;
1626
1627 /* Remote port has reappeared. Re-register w/ FC transport */
1628 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
1629 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
1630 rport_ids.port_id = ndlp->nlp_DID;
1631 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
1632
1633 /*
1634 * We leave our node pointer in rport->dd_data when we unregister a
1635 * FCP target port. But fc_remote_port_add zeros the space to which
1636 * rport->dd_data points. So, if we're reusing a previously
1637 * registered port, drop the reference that we took the last time we
1638 * registered the port.
1639 */
1640 if (ndlp->rport && ndlp->rport->dd_data &&
1641 ((struct lpfc_rport_data *) ndlp->rport->dd_data)->pnode == ndlp)
1642 lpfc_nlp_put(ndlp);
1643
1644 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
1645 "rport add: did:x%x flg:x%x type x%x",
1646 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
1647
1648 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
1649 if (!rport || !get_device(&rport->dev)) {
1650 dev_printk(KERN_WARNING, &phba->pcidev->dev,
1651 "Warning: fc_remote_port_add failed\n");
1652 return;
1653 }
1654
1655 /* initialize static port data */
1656 rport->maxframe_size = ndlp->nlp_maxframe;
1657 rport->supported_classes = ndlp->nlp_class_sup;
1658 rdata = rport->dd_data;
1659 rdata->pnode = lpfc_nlp_get(ndlp);
1660
1661 if (ndlp->nlp_type & NLP_FCP_TARGET)
1662 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
1663 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
1664 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
1665
1666
1667 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
1668 fc_remote_port_rolechg(rport, rport_ids.roles);
1669
1670 if ((rport->scsi_target_id != -1) &&
1671 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
1672 ndlp->nlp_sid = rport->scsi_target_id;
1673 }
1674 return;
1675 }
1676
1677 static void
1678 lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
1679 {
1680 struct fc_rport *rport = ndlp->rport;
1681
1682 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
1683 "rport delete: did:x%x flg:x%x type x%x",
1684 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
1685
1686 fc_remote_port_delete(rport);
1687
1688 return;
1689 }
1690
1691 static void
1692 lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
1693 {
1694 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1695
1696 spin_lock_irq(shost->host_lock);
1697 switch (state) {
1698 case NLP_STE_UNUSED_NODE:
1699 vport->fc_unused_cnt += count;
1700 break;
1701 case NLP_STE_PLOGI_ISSUE:
1702 vport->fc_plogi_cnt += count;
1703 break;
1704 case NLP_STE_ADISC_ISSUE:
1705 vport->fc_adisc_cnt += count;
1706 break;
1707 case NLP_STE_REG_LOGIN_ISSUE:
1708 vport->fc_reglogin_cnt += count;
1709 break;
1710 case NLP_STE_PRLI_ISSUE:
1711 vport->fc_prli_cnt += count;
1712 break;
1713 case NLP_STE_UNMAPPED_NODE:
1714 vport->fc_unmap_cnt += count;
1715 break;
1716 case NLP_STE_MAPPED_NODE:
1717 vport->fc_map_cnt += count;
1718 break;
1719 case NLP_STE_NPR_NODE:
1720 vport->fc_npr_cnt += count;
1721 break;
1722 }
1723 spin_unlock_irq(shost->host_lock);
1724 }
1725
1726 static void
1727 lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1728 int old_state, int new_state)
1729 {
1730 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1731
1732 if (new_state == NLP_STE_UNMAPPED_NODE) {
1733 ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
1734 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1735 ndlp->nlp_type |= NLP_FC_NODE;
1736 }
1737 if (new_state == NLP_STE_MAPPED_NODE)
1738 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
1739 if (new_state == NLP_STE_NPR_NODE)
1740 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
1741
1742 /* Transport interface */
1743 if (ndlp->rport && (old_state == NLP_STE_MAPPED_NODE ||
1744 old_state == NLP_STE_UNMAPPED_NODE)) {
1745 vport->phba->nport_event_cnt++;
1746 lpfc_unregister_remote_port(ndlp);
1747 }
1748
1749 if (new_state == NLP_STE_MAPPED_NODE ||
1750 new_state == NLP_STE_UNMAPPED_NODE) {
1751 vport->phba->nport_event_cnt++;
1752 /*
1753 * Tell the fc transport about the port, if we haven't
1754 * already. If we have, and it's a scsi entity, be
1755 * sure to unblock any attached scsi devices
1756 */
1757 lpfc_register_remote_port(vport, ndlp);
1758 }
1759 if ((new_state == NLP_STE_MAPPED_NODE) &&
1760 (vport->stat_data_enabled)) {
1761 /*
1762 * A new target is discovered, if there is no buffer for
1763 * statistical data collection allocate buffer.
1764 */
1765 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
1766 sizeof(struct lpfc_scsicmd_bkt),
1767 GFP_KERNEL);
1768
1769 if (!ndlp->lat_data)
1770 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
1771 "0286 lpfc_nlp_state_cleanup failed to "
1772 "allocate statistical data buffer DID "
1773 "0x%x\n", ndlp->nlp_DID);
1774 }
1775 /*
1776 * if we added to Mapped list, but the remote port
1777 * registration failed or assigned a target id outside
1778 * our presentable range - move the node to the
1779 * Unmapped List
1780 */
1781 if (new_state == NLP_STE_MAPPED_NODE &&
1782 (!ndlp->rport ||
1783 ndlp->rport->scsi_target_id == -1 ||
1784 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
1785 spin_lock_irq(shost->host_lock);
1786 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
1787 spin_unlock_irq(shost->host_lock);
1788 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
1789 }
1790 }
1791
1792 static char *
1793 lpfc_nlp_state_name(char *buffer, size_t size, int state)
1794 {
1795 static char *states[] = {
1796 [NLP_STE_UNUSED_NODE] = "UNUSED",
1797 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
1798 [NLP_STE_ADISC_ISSUE] = "ADISC",
1799 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
1800 [NLP_STE_PRLI_ISSUE] = "PRLI",
1801 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
1802 [NLP_STE_MAPPED_NODE] = "MAPPED",
1803 [NLP_STE_NPR_NODE] = "NPR",
1804 };
1805
1806 if (state < NLP_STE_MAX_STATE && states[state])
1807 strlcpy(buffer, states[state], size);
1808 else
1809 snprintf(buffer, size, "unknown (%d)", state);
1810 return buffer;
1811 }
1812
1813 void
1814 lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1815 int state)
1816 {
1817 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1818 int old_state = ndlp->nlp_state;
1819 char name1[16], name2[16];
1820
1821 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
1822 "0904 NPort state transition x%06x, %s -> %s\n",
1823 ndlp->nlp_DID,
1824 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
1825 lpfc_nlp_state_name(name2, sizeof(name2), state));
1826
1827 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
1828 "node statechg did:x%x old:%d ste:%d",
1829 ndlp->nlp_DID, old_state, state);
1830
1831 if (old_state == NLP_STE_NPR_NODE &&
1832 state != NLP_STE_NPR_NODE)
1833 lpfc_cancel_retry_delay_tmo(vport, ndlp);
1834 if (old_state == NLP_STE_UNMAPPED_NODE) {
1835 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
1836 ndlp->nlp_type &= ~NLP_FC_NODE;
1837 }
1838
1839 if (list_empty(&ndlp->nlp_listp)) {
1840 spin_lock_irq(shost->host_lock);
1841 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
1842 spin_unlock_irq(shost->host_lock);
1843 } else if (old_state)
1844 lpfc_nlp_counters(vport, old_state, -1);
1845
1846 ndlp->nlp_state = state;
1847 lpfc_nlp_counters(vport, state, 1);
1848 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
1849 }
1850
1851 void
1852 lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1853 {
1854 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1855
1856 if (list_empty(&ndlp->nlp_listp)) {
1857 spin_lock_irq(shost->host_lock);
1858 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
1859 spin_unlock_irq(shost->host_lock);
1860 }
1861 }
1862
1863 void
1864 lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1865 {
1866 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1867
1868 lpfc_cancel_retry_delay_tmo(vport, ndlp);
1869 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
1870 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1871 spin_lock_irq(shost->host_lock);
1872 list_del_init(&ndlp->nlp_listp);
1873 spin_unlock_irq(shost->host_lock);
1874 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
1875 NLP_STE_UNUSED_NODE);
1876 }
1877
1878 static void
1879 lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1880 {
1881 lpfc_cancel_retry_delay_tmo(vport, ndlp);
1882 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
1883 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
1884 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
1885 NLP_STE_UNUSED_NODE);
1886 }
1887 /**
1888 * lpfc_initialize_node - Initialize all fields of node object
1889 * @vport: Pointer to Virtual Port object.
1890 * @ndlp: Pointer to FC node object.
1891 * @did: FC_ID of the node.
1892 *
1893 * This function is always called when node object need to be initialized.
1894 * It initializes all the fields of the node object. Although the reference
1895 * to phba from @ndlp can be obtained indirectly through it's reference to
1896 * @vport, a direct reference to phba is taken here by @ndlp. This is due
1897 * to the life-span of the @ndlp might go beyond the existence of @vport as
1898 * the final release of ndlp is determined by its reference count. And, the
1899 * operation on @ndlp needs the reference to phba.
1900 **/
1901 static inline void
1902 lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1903 uint32_t did)
1904 {
1905 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
1906 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
1907 init_timer(&ndlp->nlp_delayfunc);
1908 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
1909 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
1910 ndlp->nlp_DID = did;
1911 ndlp->vport = vport;
1912 ndlp->phba = vport->phba;
1913 ndlp->nlp_sid = NLP_NO_SID;
1914 kref_init(&ndlp->kref);
1915 NLP_INT_NODE_ACT(ndlp);
1916 atomic_set(&ndlp->cmd_pending, 0);
1917 ndlp->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
1918 }
1919
1920 struct lpfc_nodelist *
1921 lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1922 int state)
1923 {
1924 struct lpfc_hba *phba = vport->phba;
1925 uint32_t did;
1926 unsigned long flags;
1927
1928 if (!ndlp)
1929 return NULL;
1930
1931 spin_lock_irqsave(&phba->ndlp_lock, flags);
1932 /* The ndlp should not be in memory free mode */
1933 if (NLP_CHK_FREE_REQ(ndlp)) {
1934 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
1935 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
1936 "0277 lpfc_enable_node: ndlp:x%p "
1937 "usgmap:x%x refcnt:%d\n",
1938 (void *)ndlp, ndlp->nlp_usg_map,
1939 atomic_read(&ndlp->kref.refcount));
1940 return NULL;
1941 }
1942 /* The ndlp should not already be in active mode */
1943 if (NLP_CHK_NODE_ACT(ndlp)) {
1944 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
1945 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
1946 "0278 lpfc_enable_node: ndlp:x%p "
1947 "usgmap:x%x refcnt:%d\n",
1948 (void *)ndlp, ndlp->nlp_usg_map,
1949 atomic_read(&ndlp->kref.refcount));
1950 return NULL;
1951 }
1952
1953 /* Keep the original DID */
1954 did = ndlp->nlp_DID;
1955
1956 /* re-initialize ndlp except of ndlp linked list pointer */
1957 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
1958 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
1959 lpfc_initialize_node(vport, ndlp, did);
1960
1961 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
1962
1963 if (state != NLP_STE_UNUSED_NODE)
1964 lpfc_nlp_set_state(vport, ndlp, state);
1965
1966 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
1967 "node enable: did:x%x",
1968 ndlp->nlp_DID, 0, 0);
1969 return ndlp;
1970 }
1971
1972 void
1973 lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
1974 {
1975 /*
1976 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
1977 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
1978 * the ndlp from the vport. The ndlp marked as UNUSED on the list
1979 * until ALL other outstanding threads have completed. We check
1980 * that the ndlp not already in the UNUSED state before we proceed.
1981 */
1982 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
1983 return;
1984 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
1985 lpfc_nlp_put(ndlp);
1986 return;
1987 }
1988
1989 /*
1990 * Start / ReStart rescue timer for Discovery / RSCN handling
1991 */
1992 void
1993 lpfc_set_disctmo(struct lpfc_vport *vport)
1994 {
1995 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1996 struct lpfc_hba *phba = vport->phba;
1997 uint32_t tmo;
1998
1999 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
2000 /* For FAN, timeout should be greater than edtov */
2001 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
2002 } else {
2003 /* Normal discovery timeout should be > than ELS/CT timeout
2004 * FC spec states we need 3 * ratov for CT requests
2005 */
2006 tmo = ((phba->fc_ratov * 3) + 3);
2007 }
2008
2009
2010 if (!timer_pending(&vport->fc_disctmo)) {
2011 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2012 "set disc timer: tmo:x%x state:x%x flg:x%x",
2013 tmo, vport->port_state, vport->fc_flag);
2014 }
2015
2016 mod_timer(&vport->fc_disctmo, jiffies + HZ * tmo);
2017 spin_lock_irq(shost->host_lock);
2018 vport->fc_flag |= FC_DISC_TMO;
2019 spin_unlock_irq(shost->host_lock);
2020
2021 /* Start Discovery Timer state <hba_state> */
2022 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2023 "0247 Start Discovery Timer state x%x "
2024 "Data: x%x x%lx x%x x%x\n",
2025 vport->port_state, tmo,
2026 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
2027 vport->fc_adisc_cnt);
2028
2029 return;
2030 }
2031
2032 /*
2033 * Cancel rescue timer for Discovery / RSCN handling
2034 */
2035 int
2036 lpfc_can_disctmo(struct lpfc_vport *vport)
2037 {
2038 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2039 unsigned long iflags;
2040
2041 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2042 "can disc timer: state:x%x rtry:x%x flg:x%x",
2043 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
2044
2045 /* Turn off discovery timer if its running */
2046 if (vport->fc_flag & FC_DISC_TMO) {
2047 spin_lock_irqsave(shost->host_lock, iflags);
2048 vport->fc_flag &= ~FC_DISC_TMO;
2049 spin_unlock_irqrestore(shost->host_lock, iflags);
2050 del_timer_sync(&vport->fc_disctmo);
2051 spin_lock_irqsave(&vport->work_port_lock, iflags);
2052 vport->work_port_events &= ~WORKER_DISC_TMO;
2053 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
2054 }
2055
2056 /* Cancel Discovery Timer state <hba_state> */
2057 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2058 "0248 Cancel Discovery Timer state x%x "
2059 "Data: x%x x%x x%x\n",
2060 vport->port_state, vport->fc_flag,
2061 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
2062 return 0;
2063 }
2064
2065 /*
2066 * Check specified ring for outstanding IOCB on the SLI queue
2067 * Return true if iocb matches the specified nport
2068 */
2069 int
2070 lpfc_check_sli_ndlp(struct lpfc_hba *phba,
2071 struct lpfc_sli_ring *pring,
2072 struct lpfc_iocbq *iocb,
2073 struct lpfc_nodelist *ndlp)
2074 {
2075 struct lpfc_sli *psli = &phba->sli;
2076 IOCB_t *icmd = &iocb->iocb;
2077 struct lpfc_vport *vport = ndlp->vport;
2078
2079 if (iocb->vport != vport)
2080 return 0;
2081
2082 if (pring->ringno == LPFC_ELS_RING) {
2083 switch (icmd->ulpCommand) {
2084 case CMD_GEN_REQUEST64_CR:
2085 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi)
2086 return 1;
2087 case CMD_ELS_REQUEST64_CR:
2088 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
2089 return 1;
2090 case CMD_XMIT_ELS_RSP64_CX:
2091 if (iocb->context1 == (uint8_t *) ndlp)
2092 return 1;
2093 }
2094 } else if (pring->ringno == psli->extra_ring) {
2095
2096 } else if (pring->ringno == psli->fcp_ring) {
2097 /* Skip match check if waiting to relogin to FCP target */
2098 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
2099 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
2100 return 0;
2101 }
2102 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
2103 return 1;
2104 }
2105 } else if (pring->ringno == psli->next_ring) {
2106
2107 }
2108 return 0;
2109 }
2110
2111 /*
2112 * Free resources / clean up outstanding I/Os
2113 * associated with nlp_rpi in the LPFC_NODELIST entry.
2114 */
2115 static int
2116 lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
2117 {
2118 LIST_HEAD(completions);
2119 struct lpfc_sli *psli;
2120 struct lpfc_sli_ring *pring;
2121 struct lpfc_iocbq *iocb, *next_iocb;
2122 uint32_t rpi, i;
2123
2124 lpfc_fabric_abort_nport(ndlp);
2125
2126 /*
2127 * Everything that matches on txcmplq will be returned
2128 * by firmware with a no rpi error.
2129 */
2130 psli = &phba->sli;
2131 rpi = ndlp->nlp_rpi;
2132 if (rpi) {
2133 /* Now process each ring */
2134 for (i = 0; i < psli->num_rings; i++) {
2135 pring = &psli->ring[i];
2136
2137 spin_lock_irq(&phba->hbalock);
2138 list_for_each_entry_safe(iocb, next_iocb, &pring->txq,
2139 list) {
2140 /*
2141 * Check to see if iocb matches the nport we are
2142 * looking for
2143 */
2144 if ((lpfc_check_sli_ndlp(phba, pring, iocb,
2145 ndlp))) {
2146 /* It matches, so deque and call compl
2147 with an error */
2148 list_move_tail(&iocb->list,
2149 &completions);
2150 pring->txq_cnt--;
2151 }
2152 }
2153 spin_unlock_irq(&phba->hbalock);
2154 }
2155 }
2156
2157 /* Cancel all the IOCBs from the completions list */
2158 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
2159 IOERR_SLI_ABORTED);
2160
2161 return 0;
2162 }
2163
2164 /*
2165 * Free rpi associated with LPFC_NODELIST entry.
2166 * This routine is called from lpfc_freenode(), when we are removing
2167 * a LPFC_NODELIST entry. It is also called if the driver initiates a
2168 * LOGO that completes successfully, and we are waiting to PLOGI back
2169 * to the remote NPort. In addition, it is called after we receive
2170 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
2171 * we are waiting to PLOGI back to the remote NPort.
2172 */
2173 int
2174 lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2175 {
2176 struct lpfc_hba *phba = vport->phba;
2177 LPFC_MBOXQ_t *mbox;
2178 int rc;
2179
2180 if (ndlp->nlp_rpi) {
2181 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2182 if (mbox) {
2183 lpfc_unreg_login(phba, vport->vpi, ndlp->nlp_rpi, mbox);
2184 mbox->vport = vport;
2185 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2186 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2187 if (rc == MBX_NOT_FINISHED)
2188 mempool_free(mbox, phba->mbox_mem_pool);
2189 }
2190 lpfc_no_rpi(phba, ndlp);
2191 ndlp->nlp_rpi = 0;
2192 return 1;
2193 }
2194 return 0;
2195 }
2196
2197 void
2198 lpfc_unreg_all_rpis(struct lpfc_vport *vport)
2199 {
2200 struct lpfc_hba *phba = vport->phba;
2201 LPFC_MBOXQ_t *mbox;
2202 int rc;
2203
2204 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2205 if (mbox) {
2206 lpfc_unreg_login(phba, vport->vpi, 0xffff, mbox);
2207 mbox->vport = vport;
2208 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2209 mbox->context1 = NULL;
2210 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2211 if (rc != MBX_TIMEOUT)
2212 mempool_free(mbox, phba->mbox_mem_pool);
2213
2214 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
2215 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2216 "1836 Could not issue "
2217 "unreg_login(all_rpis) status %d\n", rc);
2218 }
2219 }
2220
2221 void
2222 lpfc_unreg_default_rpis(struct lpfc_vport *vport)
2223 {
2224 struct lpfc_hba *phba = vport->phba;
2225 LPFC_MBOXQ_t *mbox;
2226 int rc;
2227
2228 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2229 if (mbox) {
2230 lpfc_unreg_did(phba, vport->vpi, 0xffffffff, mbox);
2231 mbox->vport = vport;
2232 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2233 mbox->context1 = NULL;
2234 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2235 if (rc != MBX_TIMEOUT)
2236 mempool_free(mbox, phba->mbox_mem_pool);
2237
2238 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
2239 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
2240 "1815 Could not issue "
2241 "unreg_did (default rpis) status %d\n",
2242 rc);
2243 }
2244 }
2245
2246 /*
2247 * Free resources associated with LPFC_NODELIST entry
2248 * so it can be freed.
2249 */
2250 static int
2251 lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2252 {
2253 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2254 struct lpfc_hba *phba = vport->phba;
2255 LPFC_MBOXQ_t *mb, *nextmb;
2256 struct lpfc_dmabuf *mp;
2257
2258 /* Cleanup node for NPort <nlp_DID> */
2259 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2260 "0900 Cleanup node for NPort x%x "
2261 "Data: x%x x%x x%x\n",
2262 ndlp->nlp_DID, ndlp->nlp_flag,
2263 ndlp->nlp_state, ndlp->nlp_rpi);
2264 if (NLP_CHK_FREE_REQ(ndlp)) {
2265 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2266 "0280 lpfc_cleanup_node: ndlp:x%p "
2267 "usgmap:x%x refcnt:%d\n",
2268 (void *)ndlp, ndlp->nlp_usg_map,
2269 atomic_read(&ndlp->kref.refcount));
2270 lpfc_dequeue_node(vport, ndlp);
2271 } else {
2272 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
2273 "0281 lpfc_cleanup_node: ndlp:x%p "
2274 "usgmap:x%x refcnt:%d\n",
2275 (void *)ndlp, ndlp->nlp_usg_map,
2276 atomic_read(&ndlp->kref.refcount));
2277 lpfc_disable_node(vport, ndlp);
2278 }
2279
2280 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
2281 if ((mb = phba->sli.mbox_active)) {
2282 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
2283 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
2284 mb->context2 = NULL;
2285 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2286 }
2287 }
2288
2289 spin_lock_irq(&phba->hbalock);
2290 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
2291 if ((mb->mb.mbxCommand == MBX_REG_LOGIN64) &&
2292 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
2293 mp = (struct lpfc_dmabuf *) (mb->context1);
2294 if (mp) {
2295 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
2296 kfree(mp);
2297 }
2298 list_del(&mb->list);
2299 mempool_free(mb, phba->mbox_mem_pool);
2300 /* We shall not invoke the lpfc_nlp_put to decrement
2301 * the ndlp reference count as we are in the process
2302 * of lpfc_nlp_release.
2303 */
2304 }
2305 }
2306 spin_unlock_irq(&phba->hbalock);
2307
2308 lpfc_els_abort(phba, ndlp);
2309
2310 spin_lock_irq(shost->host_lock);
2311 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
2312 spin_unlock_irq(shost->host_lock);
2313
2314 ndlp->nlp_last_elscmd = 0;
2315 del_timer_sync(&ndlp->nlp_delayfunc);
2316
2317 list_del_init(&ndlp->els_retry_evt.evt_listp);
2318 list_del_init(&ndlp->dev_loss_evt.evt_listp);
2319
2320 lpfc_unreg_rpi(vport, ndlp);
2321
2322 return 0;
2323 }
2324
2325 /*
2326 * Check to see if we can free the nlp back to the freelist.
2327 * If we are in the middle of using the nlp in the discovery state
2328 * machine, defer the free till we reach the end of the state machine.
2329 */
2330 static void
2331 lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2332 {
2333 struct lpfc_hba *phba = vport->phba;
2334 struct lpfc_rport_data *rdata;
2335 LPFC_MBOXQ_t *mbox;
2336 int rc;
2337
2338 lpfc_cancel_retry_delay_tmo(vport, ndlp);
2339 if (ndlp->nlp_flag & NLP_DEFER_RM && !ndlp->nlp_rpi) {
2340 /* For this case we need to cleanup the default rpi
2341 * allocated by the firmware.
2342 */
2343 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
2344 != NULL) {
2345 rc = lpfc_reg_login(phba, vport->vpi, ndlp->nlp_DID,
2346 (uint8_t *) &vport->fc_sparam, mbox, 0);
2347 if (rc) {
2348 mempool_free(mbox, phba->mbox_mem_pool);
2349 }
2350 else {
2351 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
2352 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
2353 mbox->vport = vport;
2354 mbox->context2 = NULL;
2355 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2356 if (rc == MBX_NOT_FINISHED) {
2357 mempool_free(mbox, phba->mbox_mem_pool);
2358 }
2359 }
2360 }
2361 }
2362 lpfc_cleanup_node(vport, ndlp);
2363
2364 /*
2365 * We can get here with a non-NULL ndlp->rport because when we
2366 * unregister a rport we don't break the rport/node linkage. So if we
2367 * do, make sure we don't leaving any dangling pointers behind.
2368 */
2369 if (ndlp->rport) {
2370 rdata = ndlp->rport->dd_data;
2371 rdata->pnode = NULL;
2372 ndlp->rport = NULL;
2373 }
2374 }
2375
2376 static int
2377 lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2378 uint32_t did)
2379 {
2380 D_ID mydid, ndlpdid, matchdid;
2381
2382 if (did == Bcast_DID)
2383 return 0;
2384
2385 /* First check for Direct match */
2386 if (ndlp->nlp_DID == did)
2387 return 1;
2388
2389 /* Next check for area/domain identically equals 0 match */
2390 mydid.un.word = vport->fc_myDID;
2391 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
2392 return 0;
2393 }
2394
2395 matchdid.un.word = did;
2396 ndlpdid.un.word = ndlp->nlp_DID;
2397 if (matchdid.un.b.id == ndlpdid.un.b.id) {
2398 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
2399 (mydid.un.b.area == matchdid.un.b.area)) {
2400 if ((ndlpdid.un.b.domain == 0) &&
2401 (ndlpdid.un.b.area == 0)) {
2402 if (ndlpdid.un.b.id)
2403 return 1;
2404 }
2405 return 0;
2406 }
2407
2408 matchdid.un.word = ndlp->nlp_DID;
2409 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
2410 (mydid.un.b.area == ndlpdid.un.b.area)) {
2411 if ((matchdid.un.b.domain == 0) &&
2412 (matchdid.un.b.area == 0)) {
2413 if (matchdid.un.b.id)
2414 return 1;
2415 }
2416 }
2417 }
2418 return 0;
2419 }
2420
2421 /* Search for a nodelist entry */
2422 static struct lpfc_nodelist *
2423 __lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
2424 {
2425 struct lpfc_nodelist *ndlp;
2426 uint32_t data1;
2427
2428 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
2429 if (lpfc_matchdid(vport, ndlp, did)) {
2430 data1 = (((uint32_t) ndlp->nlp_state << 24) |
2431 ((uint32_t) ndlp->nlp_xri << 16) |
2432 ((uint32_t) ndlp->nlp_type << 8) |
2433 ((uint32_t) ndlp->nlp_rpi & 0xff));
2434 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2435 "0929 FIND node DID "
2436 "Data: x%p x%x x%x x%x\n",
2437 ndlp, ndlp->nlp_DID,
2438 ndlp->nlp_flag, data1);
2439 return ndlp;
2440 }
2441 }
2442
2443 /* FIND node did <did> NOT FOUND */
2444 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
2445 "0932 FIND node did x%x NOT FOUND.\n", did);
2446 return NULL;
2447 }
2448
2449 struct lpfc_nodelist *
2450 lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
2451 {
2452 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2453 struct lpfc_nodelist *ndlp;
2454
2455 spin_lock_irq(shost->host_lock);
2456 ndlp = __lpfc_findnode_did(vport, did);
2457 spin_unlock_irq(shost->host_lock);
2458 return ndlp;
2459 }
2460
2461 struct lpfc_nodelist *
2462 lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
2463 {
2464 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2465 struct lpfc_nodelist *ndlp;
2466
2467 ndlp = lpfc_findnode_did(vport, did);
2468 if (!ndlp) {
2469 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
2470 lpfc_rscn_payload_check(vport, did) == 0)
2471 return NULL;
2472 ndlp = (struct lpfc_nodelist *)
2473 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
2474 if (!ndlp)
2475 return NULL;
2476 lpfc_nlp_init(vport, ndlp, did);
2477 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2478 spin_lock_irq(shost->host_lock);
2479 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2480 spin_unlock_irq(shost->host_lock);
2481 return ndlp;
2482 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
2483 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
2484 if (!ndlp)
2485 return NULL;
2486 spin_lock_irq(shost->host_lock);
2487 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2488 spin_unlock_irq(shost->host_lock);
2489 return ndlp;
2490 }
2491
2492 if ((vport->fc_flag & FC_RSCN_MODE) &&
2493 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
2494 if (lpfc_rscn_payload_check(vport, did)) {
2495 /* If we've already recieved a PLOGI from this NPort
2496 * we don't need to try to discover it again.
2497 */
2498 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
2499 return NULL;
2500
2501 /* Since this node is marked for discovery,
2502 * delay timeout is not needed.
2503 */
2504 lpfc_cancel_retry_delay_tmo(vport, ndlp);
2505 spin_lock_irq(shost->host_lock);
2506 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2507 spin_unlock_irq(shost->host_lock);
2508 } else
2509 ndlp = NULL;
2510 } else {
2511 /* If we've already recieved a PLOGI from this NPort,
2512 * or we are already in the process of discovery on it,
2513 * we don't need to try to discover it again.
2514 */
2515 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
2516 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2517 ndlp->nlp_flag & NLP_RCV_PLOGI)
2518 return NULL;
2519 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2520 spin_lock_irq(shost->host_lock);
2521 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2522 spin_unlock_irq(shost->host_lock);
2523 }
2524 return ndlp;
2525 }
2526
2527 /* Build a list of nodes to discover based on the loopmap */
2528 void
2529 lpfc_disc_list_loopmap(struct lpfc_vport *vport)
2530 {
2531 struct lpfc_hba *phba = vport->phba;
2532 int j;
2533 uint32_t alpa, index;
2534
2535 if (!lpfc_is_link_up(phba))
2536 return;
2537
2538 if (phba->fc_topology != TOPOLOGY_LOOP)
2539 return;
2540
2541 /* Check for loop map present or not */
2542 if (phba->alpa_map[0]) {
2543 for (j = 1; j <= phba->alpa_map[0]; j++) {
2544 alpa = phba->alpa_map[j];
2545 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
2546 continue;
2547 lpfc_setup_disc_node(vport, alpa);
2548 }
2549 } else {
2550 /* No alpamap, so try all alpa's */
2551 for (j = 0; j < FC_MAXLOOP; j++) {
2552 /* If cfg_scan_down is set, start from highest
2553 * ALPA (0xef) to lowest (0x1).
2554 */
2555 if (vport->cfg_scan_down)
2556 index = j;
2557 else
2558 index = FC_MAXLOOP - j - 1;
2559 alpa = lpfcAlpaArray[index];
2560 if ((vport->fc_myDID & 0xff) == alpa)
2561 continue;
2562 lpfc_setup_disc_node(vport, alpa);
2563 }
2564 }
2565 return;
2566 }
2567
2568 void
2569 lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
2570 {
2571 LPFC_MBOXQ_t *mbox;
2572 struct lpfc_sli *psli = &phba->sli;
2573 struct lpfc_sli_ring *extra_ring = &psli->ring[psli->extra_ring];
2574 struct lpfc_sli_ring *fcp_ring = &psli->ring[psli->fcp_ring];
2575 struct lpfc_sli_ring *next_ring = &psli->ring[psli->next_ring];
2576 int rc;
2577
2578 /*
2579 * if it's not a physical port or if we already send
2580 * clear_la then don't send it.
2581 */
2582 if ((phba->link_state >= LPFC_CLEAR_LA) ||
2583 (vport->port_type != LPFC_PHYSICAL_PORT) ||
2584 (phba->sli_rev == LPFC_SLI_REV4))
2585 return;
2586
2587 /* Link up discovery */
2588 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
2589 phba->link_state = LPFC_CLEAR_LA;
2590 lpfc_clear_la(phba, mbox);
2591 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
2592 mbox->vport = vport;
2593 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
2594 if (rc == MBX_NOT_FINISHED) {
2595 mempool_free(mbox, phba->mbox_mem_pool);
2596 lpfc_disc_flush_list(vport);
2597 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2598 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2599 next_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
2600 phba->link_state = LPFC_HBA_ERROR;
2601 }
2602 }
2603 }
2604
2605 /* Reg_vpi to tell firmware to resume normal operations */
2606 void
2607 lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
2608 {
2609 LPFC_MBOXQ_t *regvpimbox;
2610
2611 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2612 if (regvpimbox) {
2613 lpfc_reg_vpi(vport, regvpimbox);
2614 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
2615 regvpimbox->vport = vport;
2616 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
2617 == MBX_NOT_FINISHED) {
2618 mempool_free(regvpimbox, phba->mbox_mem_pool);
2619 }
2620 }
2621 }
2622
2623 /* Start Link up / RSCN discovery on NPR nodes */
2624 void
2625 lpfc_disc_start(struct lpfc_vport *vport)
2626 {
2627 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2628 struct lpfc_hba *phba = vport->phba;
2629 uint32_t num_sent;
2630 uint32_t clear_la_pending;
2631 int did_changed;
2632
2633 if (!lpfc_is_link_up(phba))
2634 return;
2635
2636 if (phba->link_state == LPFC_CLEAR_LA)
2637 clear_la_pending = 1;
2638 else
2639 clear_la_pending = 0;
2640
2641 if (vport->port_state < LPFC_VPORT_READY)
2642 vport->port_state = LPFC_DISC_AUTH;
2643
2644 lpfc_set_disctmo(vport);
2645
2646 if (vport->fc_prevDID == vport->fc_myDID)
2647 did_changed = 0;
2648 else
2649 did_changed = 1;
2650
2651 vport->fc_prevDID = vport->fc_myDID;
2652 vport->num_disc_nodes = 0;
2653
2654 /* Start Discovery state <hba_state> */
2655 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2656 "0202 Start Discovery hba state x%x "
2657 "Data: x%x x%x x%x\n",
2658 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
2659 vport->fc_adisc_cnt);
2660
2661 /* First do ADISCs - if any */
2662 num_sent = lpfc_els_disc_adisc(vport);
2663
2664 if (num_sent)
2665 return;
2666
2667 /*
2668 * For SLI3, cmpl_reg_vpi will set port_state to READY, and
2669 * continue discovery.
2670 */
2671 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2672 !(vport->fc_flag & FC_PT2PT) &&
2673 !(vport->fc_flag & FC_RSCN_MODE) &&
2674 (phba->sli_rev < LPFC_SLI_REV4)) {
2675 lpfc_issue_reg_vpi(phba, vport);
2676 return;
2677 }
2678
2679 /*
2680 * For SLI2, we need to set port_state to READY and continue
2681 * discovery.
2682 */
2683 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
2684 /* If we get here, there is nothing to ADISC */
2685 if (vport->port_type == LPFC_PHYSICAL_PORT)
2686 lpfc_issue_clear_la(phba, vport);
2687
2688 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
2689 vport->num_disc_nodes = 0;
2690 /* go thru NPR nodes and issue ELS PLOGIs */
2691 if (vport->fc_npr_cnt)
2692 lpfc_els_disc_plogi(vport);
2693
2694 if (!vport->num_disc_nodes) {
2695 spin_lock_irq(shost->host_lock);
2696 vport->fc_flag &= ~FC_NDISC_ACTIVE;
2697 spin_unlock_irq(shost->host_lock);
2698 lpfc_can_disctmo(vport);
2699 }
2700 }
2701 vport->port_state = LPFC_VPORT_READY;
2702 } else {
2703 /* Next do PLOGIs - if any */
2704 num_sent = lpfc_els_disc_plogi(vport);
2705
2706 if (num_sent)
2707 return;
2708
2709 if (vport->fc_flag & FC_RSCN_MODE) {
2710 /* Check to see if more RSCNs came in while we
2711 * were processing this one.
2712 */
2713 if ((vport->fc_rscn_id_cnt == 0) &&
2714 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
2715 spin_lock_irq(shost->host_lock);
2716 vport->fc_flag &= ~FC_RSCN_MODE;
2717 spin_unlock_irq(shost->host_lock);
2718 lpfc_can_disctmo(vport);
2719 } else
2720 lpfc_els_handle_rscn(vport);
2721 }
2722 }
2723 return;
2724 }
2725
2726 /*
2727 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
2728 * ring the match the sppecified nodelist.
2729 */
2730 static void
2731 lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
2732 {
2733 LIST_HEAD(completions);
2734 struct lpfc_sli *psli;
2735 IOCB_t *icmd;
2736 struct lpfc_iocbq *iocb, *next_iocb;
2737 struct lpfc_sli_ring *pring;
2738
2739 psli = &phba->sli;
2740 pring = &psli->ring[LPFC_ELS_RING];
2741
2742 /* Error matching iocb on txq or txcmplq
2743 * First check the txq.
2744 */
2745 spin_lock_irq(&phba->hbalock);
2746 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
2747 if (iocb->context1 != ndlp) {
2748 continue;
2749 }
2750 icmd = &iocb->iocb;
2751 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
2752 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
2753
2754 list_move_tail(&iocb->list, &completions);
2755 pring->txq_cnt--;
2756 }
2757 }
2758
2759 /* Next check the txcmplq */
2760 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
2761 if (iocb->context1 != ndlp) {
2762 continue;
2763 }
2764 icmd = &iocb->iocb;
2765 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
2766 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
2767 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
2768 }
2769 }
2770 spin_unlock_irq(&phba->hbalock);
2771
2772 /* Cancel all the IOCBs from the completions list */
2773 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
2774 IOERR_SLI_ABORTED);
2775 }
2776
2777 static void
2778 lpfc_disc_flush_list(struct lpfc_vport *vport)
2779 {
2780 struct lpfc_nodelist *ndlp, *next_ndlp;
2781 struct lpfc_hba *phba = vport->phba;
2782
2783 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
2784 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
2785 nlp_listp) {
2786 if (!NLP_CHK_NODE_ACT(ndlp))
2787 continue;
2788 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
2789 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
2790 lpfc_free_tx(phba, ndlp);
2791 }
2792 }
2793 }
2794 }
2795
2796 void
2797 lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
2798 {
2799 lpfc_els_flush_rscn(vport);
2800 lpfc_els_flush_cmd(vport);
2801 lpfc_disc_flush_list(vport);
2802 }
2803
2804 /*****************************************************************************/
2805 /*
2806 * NAME: lpfc_disc_timeout
2807 *
2808 * FUNCTION: Fibre Channel driver discovery timeout routine.
2809 *
2810 * EXECUTION ENVIRONMENT: interrupt only
2811 *
2812 * CALLED FROM:
2813 * Timer function
2814 *
2815 * RETURNS:
2816 * none
2817 */
2818 /*****************************************************************************/
2819 void
2820 lpfc_disc_timeout(unsigned long ptr)
2821 {
2822 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
2823 struct lpfc_hba *phba = vport->phba;
2824 uint32_t tmo_posted;
2825 unsigned long flags = 0;
2826
2827 if (unlikely(!phba))
2828 return;
2829
2830 spin_lock_irqsave(&vport->work_port_lock, flags);
2831 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
2832 if (!tmo_posted)
2833 vport->work_port_events |= WORKER_DISC_TMO;
2834 spin_unlock_irqrestore(&vport->work_port_lock, flags);
2835
2836 if (!tmo_posted)
2837 lpfc_worker_wake_up(phba);
2838 return;
2839 }
2840
2841 static void
2842 lpfc_disc_timeout_handler(struct lpfc_vport *vport)
2843 {
2844 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2845 struct lpfc_hba *phba = vport->phba;
2846 struct lpfc_sli *psli = &phba->sli;
2847 struct lpfc_nodelist *ndlp, *next_ndlp;
2848 LPFC_MBOXQ_t *initlinkmbox;
2849 int rc, clrlaerr = 0;
2850
2851 if (!(vport->fc_flag & FC_DISC_TMO))
2852 return;
2853
2854 spin_lock_irq(shost->host_lock);
2855 vport->fc_flag &= ~FC_DISC_TMO;
2856 spin_unlock_irq(shost->host_lock);
2857
2858 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2859 "disc timeout: state:x%x rtry:x%x flg:x%x",
2860 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
2861
2862 switch (vport->port_state) {
2863
2864 case LPFC_LOCAL_CFG_LINK:
2865 /* port_state is identically LPFC_LOCAL_CFG_LINK while waiting for
2866 * FAN
2867 */
2868 /* FAN timeout */
2869 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
2870 "0221 FAN timeout\n");
2871 /* Start discovery by sending FLOGI, clean up old rpis */
2872 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
2873 nlp_listp) {
2874 if (!NLP_CHK_NODE_ACT(ndlp))
2875 continue;
2876 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
2877 continue;
2878 if (ndlp->nlp_type & NLP_FABRIC) {
2879 /* Clean up the ndlp on Fabric connections */
2880 lpfc_drop_node(vport, ndlp);
2881
2882 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
2883 /* Fail outstanding IO now since device
2884 * is marked for PLOGI.
2885 */
2886 lpfc_unreg_rpi(vport, ndlp);
2887 }
2888 }
2889 if (vport->port_state != LPFC_FLOGI) {
2890 lpfc_initial_flogi(vport);
2891 return;
2892 }
2893 break;
2894
2895 case LPFC_FDISC:
2896 case LPFC_FLOGI:
2897 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
2898 /* Initial FLOGI timeout */
2899 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2900 "0222 Initial %s timeout\n",
2901 vport->vpi ? "FDISC" : "FLOGI");
2902
2903 /* Assume no Fabric and go on with discovery.
2904 * Check for outstanding ELS FLOGI to abort.
2905 */
2906
2907 /* FLOGI failed, so just use loop map to make discovery list */
2908 lpfc_disc_list_loopmap(vport);
2909
2910 /* Start discovery */
2911 lpfc_disc_start(vport);
2912 break;
2913
2914 case LPFC_FABRIC_CFG_LINK:
2915 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
2916 NameServer login */
2917 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2918 "0223 Timeout while waiting for "
2919 "NameServer login\n");
2920 /* Next look for NameServer ndlp */
2921 ndlp = lpfc_findnode_did(vport, NameServer_DID);
2922 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
2923 lpfc_els_abort(phba, ndlp);
2924
2925 /* ReStart discovery */
2926 goto restart_disc;
2927
2928 case LPFC_NS_QRY:
2929 /* Check for wait for NameServer Rsp timeout */
2930 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2931 "0224 NameServer Query timeout "
2932 "Data: x%x x%x\n",
2933 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
2934
2935 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
2936 /* Try it one more time */
2937 vport->fc_ns_retry++;
2938 rc = lpfc_ns_cmd(vport, SLI_CTNS_GID_FT,
2939 vport->fc_ns_retry, 0);
2940 if (rc == 0)
2941 break;
2942 }
2943 vport->fc_ns_retry = 0;
2944
2945 restart_disc:
2946 /*
2947 * Discovery is over.
2948 * set port_state to PORT_READY if SLI2.
2949 * cmpl_reg_vpi will set port_state to READY for SLI3.
2950 */
2951 if (phba->sli_rev < LPFC_SLI_REV4) {
2952 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2953 lpfc_issue_reg_vpi(phba, vport);
2954 else { /* NPIV Not enabled */
2955 lpfc_issue_clear_la(phba, vport);
2956 vport->port_state = LPFC_VPORT_READY;
2957 }
2958 }
2959
2960 /* Setup and issue mailbox INITIALIZE LINK command */
2961 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2962 if (!initlinkmbox) {
2963 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2964 "0206 Device Discovery "
2965 "completion error\n");
2966 phba->link_state = LPFC_HBA_ERROR;
2967 break;
2968 }
2969
2970 lpfc_linkdown(phba);
2971 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
2972 phba->cfg_link_speed);
2973 initlinkmbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
2974 initlinkmbox->vport = vport;
2975 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2976 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
2977 lpfc_set_loopback_flag(phba);
2978 if (rc == MBX_NOT_FINISHED)
2979 mempool_free(initlinkmbox, phba->mbox_mem_pool);
2980
2981 break;
2982
2983 case LPFC_DISC_AUTH:
2984 /* Node Authentication timeout */
2985 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2986 "0227 Node Authentication timeout\n");
2987 lpfc_disc_flush_list(vport);
2988
2989 /*
2990 * set port_state to PORT_READY if SLI2.
2991 * cmpl_reg_vpi will set port_state to READY for SLI3.
2992 */
2993 if (phba->sli_rev < LPFC_SLI_REV4) {
2994 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2995 lpfc_issue_reg_vpi(phba, vport);
2996 else { /* NPIV Not enabled */
2997 lpfc_issue_clear_la(phba, vport);
2998 vport->port_state = LPFC_VPORT_READY;
2999 }
3000 }
3001 break;
3002
3003 case LPFC_VPORT_READY:
3004 if (vport->fc_flag & FC_RSCN_MODE) {
3005 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3006 "0231 RSCN timeout Data: x%x "
3007 "x%x\n",
3008 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
3009
3010 /* Cleanup any outstanding ELS commands */
3011 lpfc_els_flush_cmd(vport);
3012
3013 lpfc_els_flush_rscn(vport);
3014 lpfc_disc_flush_list(vport);
3015 }
3016 break;
3017
3018 default:
3019 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3020 "0273 Unexpected discovery timeout, "
3021 "vport State x%x\n", vport->port_state);
3022 break;
3023 }
3024
3025 switch (phba->link_state) {
3026 case LPFC_CLEAR_LA:
3027 /* CLEAR LA timeout */
3028 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3029 "0228 CLEAR LA timeout\n");
3030 clrlaerr = 1;
3031 break;
3032
3033 case LPFC_LINK_UP:
3034 lpfc_issue_clear_la(phba, vport);
3035 /* Drop thru */
3036 case LPFC_LINK_UNKNOWN:
3037 case LPFC_WARM_START:
3038 case LPFC_INIT_START:
3039 case LPFC_INIT_MBX_CMDS:
3040 case LPFC_LINK_DOWN:
3041 case LPFC_HBA_ERROR:
3042 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
3043 "0230 Unexpected timeout, hba link "
3044 "state x%x\n", phba->link_state);
3045 clrlaerr = 1;
3046 break;
3047
3048 case LPFC_HBA_READY:
3049 break;
3050 }
3051
3052 if (clrlaerr) {
3053 lpfc_disc_flush_list(vport);
3054 psli->ring[(psli->extra_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
3055 psli->ring[(psli->fcp_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
3056 psli->ring[(psli->next_ring)].flag &= ~LPFC_STOP_IOCB_EVENT;
3057 vport->port_state = LPFC_VPORT_READY;
3058 }
3059
3060 return;
3061 }
3062
3063 /*
3064 * This routine handles processing a NameServer REG_LOGIN mailbox
3065 * command upon completion. It is setup in the LPFC_MBOXQ
3066 * as the completion routine when the command is
3067 * handed off to the SLI layer.
3068 */
3069 void
3070 lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3071 {
3072 MAILBOX_t *mb = &pmb->mb;
3073 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3074 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3075 struct lpfc_vport *vport = pmb->vport;
3076
3077 pmb->context1 = NULL;
3078
3079 ndlp->nlp_rpi = mb->un.varWords[0];
3080 ndlp->nlp_type |= NLP_FABRIC;
3081 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
3082
3083 /*
3084 * Start issuing Fabric-Device Management Interface (FDMI) command to
3085 * 0xfffffa (FDMI well known port) or Delay issuing FDMI command if
3086 * fdmi-on=2 (supporting RPA/hostnmae)
3087 */
3088
3089 if (vport->cfg_fdmi_on == 1)
3090 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA);
3091 else
3092 mod_timer(&vport->fc_fdmitmo, jiffies + HZ * 60);
3093
3094 /* decrement the node reference count held for this callback
3095 * function.
3096 */
3097 lpfc_nlp_put(ndlp);
3098 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3099 kfree(mp);
3100 mempool_free(pmb, phba->mbox_mem_pool);
3101
3102 return;
3103 }
3104
3105 static int
3106 lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
3107 {
3108 uint16_t *rpi = param;
3109
3110 return ndlp->nlp_rpi == *rpi;
3111 }
3112
3113 static int
3114 lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
3115 {
3116 return memcmp(&ndlp->nlp_portname, param,
3117 sizeof(ndlp->nlp_portname)) == 0;
3118 }
3119
3120 static struct lpfc_nodelist *
3121 __lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
3122 {
3123 struct lpfc_nodelist *ndlp;
3124
3125 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3126 if (filter(ndlp, param))
3127 return ndlp;
3128 }
3129 return NULL;
3130 }
3131
3132 /*
3133 * This routine looks up the ndlp lists for the given RPI. If rpi found it
3134 * returns the node list element pointer else return NULL.
3135 */
3136 struct lpfc_nodelist *
3137 __lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
3138 {
3139 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
3140 }
3141
3142 /*
3143 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
3144 * returns the node element list pointer else return NULL.
3145 */
3146 struct lpfc_nodelist *
3147 lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
3148 {
3149 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3150 struct lpfc_nodelist *ndlp;
3151
3152 spin_lock_irq(shost->host_lock);
3153 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
3154 spin_unlock_irq(shost->host_lock);
3155 return ndlp;
3156 }
3157
3158 void
3159 lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
3160 uint32_t did)
3161 {
3162 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
3163
3164 lpfc_initialize_node(vport, ndlp, did);
3165 INIT_LIST_HEAD(&ndlp->nlp_listp);
3166
3167 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
3168 "node init: did:x%x",
3169 ndlp->nlp_DID, 0, 0);
3170
3171 return;
3172 }
3173
3174 /* This routine releases all resources associated with a specifc NPort's ndlp
3175 * and mempool_free's the nodelist.
3176 */
3177 static void
3178 lpfc_nlp_release(struct kref *kref)
3179 {
3180 struct lpfc_hba *phba;
3181 unsigned long flags;
3182 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
3183 kref);
3184
3185 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3186 "node release: did:x%x flg:x%x type:x%x",
3187 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
3188
3189 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
3190 "0279 lpfc_nlp_release: ndlp:x%p "
3191 "usgmap:x%x refcnt:%d\n",
3192 (void *)ndlp, ndlp->nlp_usg_map,
3193 atomic_read(&ndlp->kref.refcount));
3194
3195 /* remove ndlp from action. */
3196 lpfc_nlp_remove(ndlp->vport, ndlp);
3197
3198 /* clear the ndlp active flag for all release cases */
3199 phba = ndlp->phba;
3200 spin_lock_irqsave(&phba->ndlp_lock, flags);
3201 NLP_CLR_NODE_ACT(ndlp);
3202 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3203
3204 /* free ndlp memory for final ndlp release */
3205 if (NLP_CHK_FREE_REQ(ndlp)) {
3206 kfree(ndlp->lat_data);
3207 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
3208 }
3209 }
3210
3211 /* This routine bumps the reference count for a ndlp structure to ensure
3212 * that one discovery thread won't free a ndlp while another discovery thread
3213 * is using it.
3214 */
3215 struct lpfc_nodelist *
3216 lpfc_nlp_get(struct lpfc_nodelist *ndlp)
3217 {
3218 struct lpfc_hba *phba;
3219 unsigned long flags;
3220
3221 if (ndlp) {
3222 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3223 "node get: did:x%x flg:x%x refcnt:x%x",
3224 ndlp->nlp_DID, ndlp->nlp_flag,
3225 atomic_read(&ndlp->kref.refcount));
3226 /* The check of ndlp usage to prevent incrementing the
3227 * ndlp reference count that is in the process of being
3228 * released.
3229 */
3230 phba = ndlp->phba;
3231 spin_lock_irqsave(&phba->ndlp_lock, flags);
3232 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
3233 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3234 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3235 "0276 lpfc_nlp_get: ndlp:x%p "
3236 "usgmap:x%x refcnt:%d\n",
3237 (void *)ndlp, ndlp->nlp_usg_map,
3238 atomic_read(&ndlp->kref.refcount));
3239 return NULL;
3240 } else
3241 kref_get(&ndlp->kref);
3242 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3243 }
3244 return ndlp;
3245 }
3246
3247 /* This routine decrements the reference count for a ndlp structure. If the
3248 * count goes to 0, this indicates the the associated nodelist should be
3249 * freed. Returning 1 indicates the ndlp resource has been released; on the
3250 * other hand, returning 0 indicates the ndlp resource has not been released
3251 * yet.
3252 */
3253 int
3254 lpfc_nlp_put(struct lpfc_nodelist *ndlp)
3255 {
3256 struct lpfc_hba *phba;
3257 unsigned long flags;
3258
3259 if (!ndlp)
3260 return 1;
3261
3262 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3263 "node put: did:x%x flg:x%x refcnt:x%x",
3264 ndlp->nlp_DID, ndlp->nlp_flag,
3265 atomic_read(&ndlp->kref.refcount));
3266 phba = ndlp->phba;
3267 spin_lock_irqsave(&phba->ndlp_lock, flags);
3268 /* Check the ndlp memory free acknowledge flag to avoid the
3269 * possible race condition that kref_put got invoked again
3270 * after previous one has done ndlp memory free.
3271 */
3272 if (NLP_CHK_FREE_ACK(ndlp)) {
3273 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3274 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3275 "0274 lpfc_nlp_put: ndlp:x%p "
3276 "usgmap:x%x refcnt:%d\n",
3277 (void *)ndlp, ndlp->nlp_usg_map,
3278 atomic_read(&ndlp->kref.refcount));
3279 return 1;
3280 }
3281 /* Check the ndlp inactivate log flag to avoid the possible
3282 * race condition that kref_put got invoked again after ndlp
3283 * is already in inactivating state.
3284 */
3285 if (NLP_CHK_IACT_REQ(ndlp)) {
3286 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3287 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
3288 "0275 lpfc_nlp_put: ndlp:x%p "
3289 "usgmap:x%x refcnt:%d\n",
3290 (void *)ndlp, ndlp->nlp_usg_map,
3291 atomic_read(&ndlp->kref.refcount));
3292 return 1;
3293 }
3294 /* For last put, mark the ndlp usage flags to make sure no
3295 * other kref_get and kref_put on the same ndlp shall get
3296 * in between the process when the final kref_put has been
3297 * invoked on this ndlp.
3298 */
3299 if (atomic_read(&ndlp->kref.refcount) == 1) {
3300 /* Indicate ndlp is put to inactive state. */
3301 NLP_SET_IACT_REQ(ndlp);
3302 /* Acknowledge ndlp memory free has been seen. */
3303 if (NLP_CHK_FREE_REQ(ndlp))
3304 NLP_SET_FREE_ACK(ndlp);
3305 }
3306 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
3307 /* Note, the kref_put returns 1 when decrementing a reference
3308 * count that was 1, it invokes the release callback function,
3309 * but it still left the reference count as 1 (not actually
3310 * performs the last decrementation). Otherwise, it actually
3311 * decrements the reference count and returns 0.
3312 */
3313 return kref_put(&ndlp->kref, lpfc_nlp_release);
3314 }
3315
3316 /* This routine free's the specified nodelist if it is not in use
3317 * by any other discovery thread. This routine returns 1 if the
3318 * ndlp has been freed. A return value of 0 indicates the ndlp is
3319 * not yet been released.
3320 */
3321 int
3322 lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
3323 {
3324 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
3325 "node not used: did:x%x flg:x%x refcnt:x%x",
3326 ndlp->nlp_DID, ndlp->nlp_flag,
3327 atomic_read(&ndlp->kref.refcount));
3328 if (atomic_read(&ndlp->kref.refcount) == 1)
3329 if (lpfc_nlp_put(ndlp))
3330 return 1;
3331 return 0;
3332 }
This page took 0.175752 seconds and 5 git commands to generate.