GNU Linux-libre 5.10.215-gnu1
[releases.git] / drivers / scsi / qla2xxx / qla_init.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * QLogic Fibre Channel HBA Driver
4  * Copyright (c)  2003-2014 QLogic Corporation
5  */
6 #include "qla_def.h"
7 #include "qla_gbl.h"
8
9 #include <linux/delay.h>
10 #include <linux/slab.h>
11 #include <linux/vmalloc.h>
12
13 #include "qla_devtbl.h"
14
15 #ifdef CONFIG_SPARC
16 #include <asm/prom.h>
17 #endif
18
19 #include "qla_target.h"
20
21 /*
22 *  QLogic ISP2x00 Hardware Support Function Prototypes.
23 */
24 static int qla2x00_isp_firmware(scsi_qla_host_t *);
25 static int qla2x00_setup_chip(scsi_qla_host_t *);
26 static int qla2x00_fw_ready(scsi_qla_host_t *);
27 static int qla2x00_configure_hba(scsi_qla_host_t *);
28 static int qla2x00_configure_loop(scsi_qla_host_t *);
29 static int qla2x00_configure_local_loop(scsi_qla_host_t *);
30 static int qla2x00_configure_fabric(scsi_qla_host_t *);
31 static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *);
32 static int qla2x00_restart_isp(scsi_qla_host_t *);
33
34 static struct qla_chip_state_84xx *qla84xx_get_chip(struct scsi_qla_host *);
35 static int qla84xx_init_chip(scsi_qla_host_t *);
36 static int qla25xx_init_queues(struct qla_hw_data *);
37 static int qla24xx_post_prli_work(struct scsi_qla_host*, fc_port_t *);
38 static void qla24xx_handle_gpdb_event(scsi_qla_host_t *vha,
39                                       struct event_arg *ea);
40 static void qla24xx_handle_prli_done_event(struct scsi_qla_host *,
41     struct event_arg *);
42 static void __qla24xx_handle_gpdb_event(scsi_qla_host_t *, struct event_arg *);
43
44 /* SRB Extensions ---------------------------------------------------------- */
45
46 void
47 qla2x00_sp_timeout(struct timer_list *t)
48 {
49         srb_t *sp = from_timer(sp, t, u.iocb_cmd.timer);
50         struct srb_iocb *iocb;
51
52         WARN_ON(irqs_disabled());
53         iocb = &sp->u.iocb_cmd;
54         iocb->timeout(sp);
55 }
56
57 void qla2x00_sp_free(srb_t *sp)
58 {
59         struct srb_iocb *iocb = &sp->u.iocb_cmd;
60
61         del_timer(&iocb->timer);
62         qla2x00_rel_sp(sp);
63 }
64
65 void qla2xxx_rel_done_warning(srb_t *sp, int res)
66 {
67         WARN_ONCE(1, "Calling done() of an already freed srb %p object\n", sp);
68 }
69
70 void qla2xxx_rel_free_warning(srb_t *sp)
71 {
72         WARN_ONCE(1, "Calling free() of an already freed srb %p object\n", sp);
73 }
74
75 /* Asynchronous Login/Logout Routines -------------------------------------- */
76
77 unsigned long
78 qla2x00_get_async_timeout(struct scsi_qla_host *vha)
79 {
80         unsigned long tmo;
81         struct qla_hw_data *ha = vha->hw;
82
83         /* Firmware should use switch negotiated r_a_tov for timeout. */
84         tmo = ha->r_a_tov / 10 * 2;
85         if (IS_QLAFX00(ha)) {
86                 tmo = FX00_DEF_RATOV * 2;
87         } else if (!IS_FWI2_CAPABLE(ha)) {
88                 /*
89                  * Except for earlier ISPs where the timeout is seeded from the
90                  * initialization control block.
91                  */
92                 tmo = ha->login_timeout;
93         }
94         return tmo;
95 }
96
97 static void qla24xx_abort_iocb_timeout(void *data)
98 {
99         srb_t *sp = data;
100         struct srb_iocb *abt = &sp->u.iocb_cmd;
101         struct qla_qpair *qpair = sp->qpair;
102         u32 handle;
103         unsigned long flags;
104
105         if (sp->cmd_sp)
106                 ql_dbg(ql_dbg_async, sp->vha, 0x507c,
107                     "Abort timeout - cmd hdl=%x, cmd type=%x hdl=%x, type=%x\n",
108                     sp->cmd_sp->handle, sp->cmd_sp->type,
109                     sp->handle, sp->type);
110         else
111                 ql_dbg(ql_dbg_async, sp->vha, 0x507c,
112                     "Abort timeout 2 - hdl=%x, type=%x\n",
113                     sp->handle, sp->type);
114
115         spin_lock_irqsave(qpair->qp_lock_ptr, flags);
116         for (handle = 1; handle < qpair->req->num_outstanding_cmds; handle++) {
117                 if (sp->cmd_sp && (qpair->req->outstanding_cmds[handle] ==
118                     sp->cmd_sp))
119                         qpair->req->outstanding_cmds[handle] = NULL;
120
121                 /* removing the abort */
122                 if (qpair->req->outstanding_cmds[handle] == sp) {
123                         qpair->req->outstanding_cmds[handle] = NULL;
124                         break;
125                 }
126         }
127         spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
128
129         if (sp->cmd_sp)
130                 sp->cmd_sp->done(sp->cmd_sp, QLA_OS_TIMER_EXPIRED);
131
132         abt->u.abt.comp_status = cpu_to_le16(CS_TIMEOUT);
133         sp->done(sp, QLA_OS_TIMER_EXPIRED);
134 }
135
136 static void qla24xx_abort_sp_done(srb_t *sp, int res)
137 {
138         struct srb_iocb *abt = &sp->u.iocb_cmd;
139
140         del_timer(&sp->u.iocb_cmd.timer);
141         if (sp->flags & SRB_WAKEUP_ON_COMP)
142                 complete(&abt->u.abt.comp);
143         else
144                 sp->free(sp);
145 }
146
147 int qla24xx_async_abort_cmd(srb_t *cmd_sp, bool wait)
148 {
149         scsi_qla_host_t *vha = cmd_sp->vha;
150         struct srb_iocb *abt_iocb;
151         srb_t *sp;
152         int rval = QLA_FUNCTION_FAILED;
153
154         sp = qla2xxx_get_qpair_sp(cmd_sp->vha, cmd_sp->qpair, cmd_sp->fcport,
155                                   GFP_ATOMIC);
156         if (!sp)
157                 return rval;
158
159         abt_iocb = &sp->u.iocb_cmd;
160         sp->type = SRB_ABT_CMD;
161         sp->name = "abort";
162         sp->qpair = cmd_sp->qpair;
163         sp->cmd_sp = cmd_sp;
164         if (wait)
165                 sp->flags = SRB_WAKEUP_ON_COMP;
166
167         abt_iocb->timeout = qla24xx_abort_iocb_timeout;
168         init_completion(&abt_iocb->u.abt.comp);
169         /* FW can send 2 x ABTS's timeout/20s */
170         qla2x00_init_timer(sp, 42);
171
172         abt_iocb->u.abt.cmd_hndl = cmd_sp->handle;
173         abt_iocb->u.abt.req_que_no = cpu_to_le16(cmd_sp->qpair->req->id);
174
175         sp->done = qla24xx_abort_sp_done;
176
177         ql_dbg(ql_dbg_async, vha, 0x507c,
178                "Abort command issued - hdl=%x, type=%x\n", cmd_sp->handle,
179                cmd_sp->type);
180
181         rval = qla2x00_start_sp(sp);
182         if (rval != QLA_SUCCESS) {
183                 sp->free(sp);
184                 return rval;
185         }
186
187         if (wait) {
188                 wait_for_completion(&abt_iocb->u.abt.comp);
189                 rval = abt_iocb->u.abt.comp_status == CS_COMPLETE ?
190                         QLA_SUCCESS : QLA_FUNCTION_FAILED;
191                 sp->free(sp);
192         }
193
194         return rval;
195 }
196
197 void
198 qla2x00_async_iocb_timeout(void *data)
199 {
200         srb_t *sp = data;
201         fc_port_t *fcport = sp->fcport;
202         struct srb_iocb *lio = &sp->u.iocb_cmd;
203         int rc, h;
204         unsigned long flags;
205
206         if (fcport) {
207                 ql_dbg(ql_dbg_disc, fcport->vha, 0x2071,
208                     "Async-%s timeout - hdl=%x portid=%06x %8phC.\n",
209                     sp->name, sp->handle, fcport->d_id.b24, fcport->port_name);
210
211                 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
212         } else {
213                 pr_info("Async-%s timeout - hdl=%x.\n",
214                     sp->name, sp->handle);
215         }
216
217         switch (sp->type) {
218         case SRB_LOGIN_CMD:
219                 rc = qla24xx_async_abort_cmd(sp, false);
220                 if (rc) {
221                         /* Retry as needed. */
222                         lio->u.logio.data[0] = MBS_COMMAND_ERROR;
223                         lio->u.logio.data[1] =
224                                 lio->u.logio.flags & SRB_LOGIN_RETRIED ?
225                                 QLA_LOGIO_LOGIN_RETRIED : 0;
226                         spin_lock_irqsave(sp->qpair->qp_lock_ptr, flags);
227                         for (h = 1; h < sp->qpair->req->num_outstanding_cmds;
228                             h++) {
229                                 if (sp->qpair->req->outstanding_cmds[h] ==
230                                     sp) {
231                                         sp->qpair->req->outstanding_cmds[h] =
232                                             NULL;
233                                         break;
234                                 }
235                         }
236                         spin_unlock_irqrestore(sp->qpair->qp_lock_ptr, flags);
237                         sp->done(sp, QLA_FUNCTION_TIMEOUT);
238                 }
239                 break;
240         case SRB_LOGOUT_CMD:
241         case SRB_CT_PTHRU_CMD:
242         case SRB_MB_IOCB:
243         case SRB_NACK_PLOGI:
244         case SRB_NACK_PRLI:
245         case SRB_NACK_LOGO:
246         case SRB_CTRL_VP:
247         default:
248                 rc = qla24xx_async_abort_cmd(sp, false);
249                 if (rc) {
250                         spin_lock_irqsave(sp->qpair->qp_lock_ptr, flags);
251                         for (h = 1; h < sp->qpair->req->num_outstanding_cmds;
252                             h++) {
253                                 if (sp->qpair->req->outstanding_cmds[h] ==
254                                     sp) {
255                                         sp->qpair->req->outstanding_cmds[h] =
256                                             NULL;
257                                         break;
258                                 }
259                         }
260                         spin_unlock_irqrestore(sp->qpair->qp_lock_ptr, flags);
261                         sp->done(sp, QLA_FUNCTION_TIMEOUT);
262                 }
263                 break;
264         }
265 }
266
267 static void qla2x00_async_login_sp_done(srb_t *sp, int res)
268 {
269         struct scsi_qla_host *vha = sp->vha;
270         struct srb_iocb *lio = &sp->u.iocb_cmd;
271         struct event_arg ea;
272
273         ql_dbg(ql_dbg_disc, vha, 0x20dd,
274             "%s %8phC res %d \n", __func__, sp->fcport->port_name, res);
275
276         sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
277
278         if (!test_bit(UNLOADING, &vha->dpc_flags)) {
279                 memset(&ea, 0, sizeof(ea));
280                 ea.fcport = sp->fcport;
281                 ea.data[0] = lio->u.logio.data[0];
282                 ea.data[1] = lio->u.logio.data[1];
283                 ea.iop[0] = lio->u.logio.iop[0];
284                 ea.iop[1] = lio->u.logio.iop[1];
285                 ea.sp = sp;
286                 qla24xx_handle_plogi_done_event(vha, &ea);
287         }
288
289         sp->free(sp);
290 }
291
292 static inline bool
293 fcport_is_smaller(fc_port_t *fcport)
294 {
295         if (wwn_to_u64(fcport->port_name) <
296             wwn_to_u64(fcport->vha->port_name))
297                 return true;
298         else
299                 return false;
300 }
301
302 static inline bool
303 fcport_is_bigger(fc_port_t *fcport)
304 {
305         return !fcport_is_smaller(fcport);
306 }
307
308 int
309 qla2x00_async_login(struct scsi_qla_host *vha, fc_port_t *fcport,
310     uint16_t *data)
311 {
312         srb_t *sp;
313         struct srb_iocb *lio;
314         int rval = QLA_FUNCTION_FAILED;
315
316         if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT) ||
317             fcport->loop_id == FC_NO_LOOP_ID) {
318                 ql_log(ql_log_warn, vha, 0xffff,
319                     "%s: %8phC - not sending command.\n",
320                     __func__, fcport->port_name);
321                 return rval;
322         }
323
324         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
325         if (!sp)
326                 goto done;
327
328         qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_PEND);
329         fcport->flags |= FCF_ASYNC_SENT;
330         fcport->logout_completed = 0;
331
332         sp->type = SRB_LOGIN_CMD;
333         sp->name = "login";
334         sp->gen1 = fcport->rscn_gen;
335         sp->gen2 = fcport->login_gen;
336
337         lio = &sp->u.iocb_cmd;
338         lio->timeout = qla2x00_async_iocb_timeout;
339         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
340
341         sp->done = qla2x00_async_login_sp_done;
342         if (N2N_TOPO(fcport->vha->hw) && fcport_is_bigger(fcport))
343                 lio->u.logio.flags |= SRB_LOGIN_PRLI_ONLY;
344         else
345                 lio->u.logio.flags |= SRB_LOGIN_COND_PLOGI;
346
347         if (NVME_TARGET(vha->hw, fcport))
348                 lio->u.logio.flags |= SRB_LOGIN_SKIP_PRLI;
349
350         ql_dbg(ql_dbg_disc, vha, 0x2072,
351             "Async-login - %8phC hdl=%x, loopid=%x portid=%02x%02x%02x "
352                 "retries=%d.\n", fcport->port_name, sp->handle, fcport->loop_id,
353             fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa,
354             fcport->login_retry);
355
356         rval = qla2x00_start_sp(sp);
357         if (rval != QLA_SUCCESS) {
358                 fcport->flags |= FCF_LOGIN_NEEDED;
359                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
360                 goto done_free_sp;
361         }
362
363         return rval;
364
365 done_free_sp:
366         sp->free(sp);
367         fcport->flags &= ~FCF_ASYNC_SENT;
368 done:
369         fcport->flags &= ~FCF_ASYNC_ACTIVE;
370         return rval;
371 }
372
373 static void qla2x00_async_logout_sp_done(srb_t *sp, int res)
374 {
375         sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
376         sp->fcport->login_gen++;
377         qlt_logo_completion_handler(sp->fcport, res);
378         sp->free(sp);
379 }
380
381 int
382 qla2x00_async_logout(struct scsi_qla_host *vha, fc_port_t *fcport)
383 {
384         srb_t *sp;
385         struct srb_iocb *lio;
386         int rval = QLA_FUNCTION_FAILED;
387
388         fcport->flags |= FCF_ASYNC_SENT;
389         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
390         if (!sp)
391                 goto done;
392
393         sp->type = SRB_LOGOUT_CMD;
394         sp->name = "logout";
395
396         lio = &sp->u.iocb_cmd;
397         lio->timeout = qla2x00_async_iocb_timeout;
398         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
399
400         sp->done = qla2x00_async_logout_sp_done;
401
402         ql_dbg(ql_dbg_disc, vha, 0x2070,
403             "Async-logout - hdl=%x loop-id=%x portid=%02x%02x%02x %8phC.\n",
404             sp->handle, fcport->loop_id, fcport->d_id.b.domain,
405                 fcport->d_id.b.area, fcport->d_id.b.al_pa,
406                 fcport->port_name);
407
408         rval = qla2x00_start_sp(sp);
409         if (rval != QLA_SUCCESS)
410                 goto done_free_sp;
411         return rval;
412
413 done_free_sp:
414         sp->free(sp);
415 done:
416         fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
417         return rval;
418 }
419
420 void
421 qla2x00_async_prlo_done(struct scsi_qla_host *vha, fc_port_t *fcport,
422     uint16_t *data)
423 {
424         fcport->flags &= ~FCF_ASYNC_ACTIVE;
425         /* Don't re-login in target mode */
426         if (!fcport->tgt_session)
427                 qla2x00_mark_device_lost(vha, fcport, 1);
428         qlt_logo_completion_handler(fcport, data[0]);
429 }
430
431 static void qla2x00_async_prlo_sp_done(srb_t *sp, int res)
432 {
433         struct srb_iocb *lio = &sp->u.iocb_cmd;
434         struct scsi_qla_host *vha = sp->vha;
435
436         sp->fcport->flags &= ~FCF_ASYNC_ACTIVE;
437         if (!test_bit(UNLOADING, &vha->dpc_flags))
438                 qla2x00_post_async_prlo_done_work(sp->fcport->vha, sp->fcport,
439                     lio->u.logio.data);
440         sp->free(sp);
441 }
442
443 int
444 qla2x00_async_prlo(struct scsi_qla_host *vha, fc_port_t *fcport)
445 {
446         srb_t *sp;
447         struct srb_iocb *lio;
448         int rval;
449
450         rval = QLA_FUNCTION_FAILED;
451         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
452         if (!sp)
453                 goto done;
454
455         sp->type = SRB_PRLO_CMD;
456         sp->name = "prlo";
457
458         lio = &sp->u.iocb_cmd;
459         lio->timeout = qla2x00_async_iocb_timeout;
460         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
461
462         sp->done = qla2x00_async_prlo_sp_done;
463
464         ql_dbg(ql_dbg_disc, vha, 0x2070,
465             "Async-prlo - hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
466             sp->handle, fcport->loop_id, fcport->d_id.b.domain,
467             fcport->d_id.b.area, fcport->d_id.b.al_pa);
468
469         rval = qla2x00_start_sp(sp);
470         if (rval != QLA_SUCCESS)
471                 goto done_free_sp;
472
473         return rval;
474
475 done_free_sp:
476         sp->free(sp);
477 done:
478         fcport->flags &= ~FCF_ASYNC_ACTIVE;
479         return rval;
480 }
481
482 static
483 void qla24xx_handle_adisc_event(scsi_qla_host_t *vha, struct event_arg *ea)
484 {
485         struct fc_port *fcport = ea->fcport;
486         unsigned long flags;
487
488         ql_dbg(ql_dbg_disc, vha, 0x20d2,
489             "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d lid %d\n",
490             __func__, fcport->port_name, fcport->disc_state,
491             fcport->fw_login_state, ea->rc, fcport->login_gen, ea->sp->gen2,
492             fcport->rscn_gen, ea->sp->gen1, fcport->loop_id);
493
494         WARN_ONCE(!qla2xxx_is_valid_mbs(ea->data[0]), "mbs: %#x\n",
495                   ea->data[0]);
496
497         if (ea->data[0] != MBS_COMMAND_COMPLETE) {
498                 ql_dbg(ql_dbg_disc, vha, 0x2066,
499                     "%s %8phC: adisc fail: post delete\n",
500                     __func__, ea->fcport->port_name);
501
502                 spin_lock_irqsave(&vha->work_lock, flags);
503                 /* deleted = 0 & logout_on_delete = force fw cleanup */
504                 if (fcport->deleted == QLA_SESS_DELETED)
505                         fcport->deleted = 0;
506
507                 fcport->logout_on_delete = 1;
508                 spin_unlock_irqrestore(&vha->work_lock, flags);
509
510                 qlt_schedule_sess_for_deletion(ea->fcport);
511                 return;
512         }
513
514         if (ea->fcport->disc_state == DSC_DELETE_PEND)
515                 return;
516
517         if (ea->sp->gen2 != ea->fcport->login_gen) {
518                 /* target side must have changed it. */
519                 ql_dbg(ql_dbg_disc, vha, 0x20d3,
520                     "%s %8phC generation changed\n",
521                     __func__, ea->fcport->port_name);
522                 return;
523         } else if (ea->sp->gen1 != ea->fcport->rscn_gen) {
524                 qla_rscn_replay(fcport);
525                 qlt_schedule_sess_for_deletion(fcport);
526                 return;
527         }
528
529         __qla24xx_handle_gpdb_event(vha, ea);
530 }
531
532 static int qla_post_els_plogi_work(struct scsi_qla_host *vha, fc_port_t *fcport)
533 {
534         struct qla_work_evt *e;
535
536         e = qla2x00_alloc_work(vha, QLA_EVT_ELS_PLOGI);
537         if (!e)
538                 return QLA_FUNCTION_FAILED;
539
540         e->u.fcport.fcport = fcport;
541         fcport->flags |= FCF_ASYNC_ACTIVE;
542         qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_PEND);
543         return qla2x00_post_work(vha, e);
544 }
545
546 static void qla2x00_async_adisc_sp_done(srb_t *sp, int res)
547 {
548         struct scsi_qla_host *vha = sp->vha;
549         struct event_arg ea;
550         struct srb_iocb *lio = &sp->u.iocb_cmd;
551
552         ql_dbg(ql_dbg_disc, vha, 0x2066,
553             "Async done-%s res %x %8phC\n",
554             sp->name, res, sp->fcport->port_name);
555
556         sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
557
558         memset(&ea, 0, sizeof(ea));
559         ea.rc = res;
560         ea.data[0] = lio->u.logio.data[0];
561         ea.data[1] = lio->u.logio.data[1];
562         ea.iop[0] = lio->u.logio.iop[0];
563         ea.iop[1] = lio->u.logio.iop[1];
564         ea.fcport = sp->fcport;
565         ea.sp = sp;
566
567         qla24xx_handle_adisc_event(vha, &ea);
568
569         sp->free(sp);
570 }
571
572 int
573 qla2x00_async_adisc(struct scsi_qla_host *vha, fc_port_t *fcport,
574     uint16_t *data)
575 {
576         srb_t *sp;
577         struct srb_iocb *lio;
578         int rval = QLA_FUNCTION_FAILED;
579
580         if (IS_SESSION_DELETED(fcport)) {
581                 ql_log(ql_log_warn, vha, 0xffff,
582                        "%s: %8phC is being delete - not sending command.\n",
583                        __func__, fcport->port_name);
584                 fcport->flags &= ~FCF_ASYNC_ACTIVE;
585                 return rval;
586         }
587
588         if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
589                 return rval;
590
591         fcport->flags |= FCF_ASYNC_SENT;
592         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
593         if (!sp)
594                 goto done;
595
596         sp->type = SRB_ADISC_CMD;
597         sp->name = "adisc";
598
599         lio = &sp->u.iocb_cmd;
600         lio->timeout = qla2x00_async_iocb_timeout;
601         sp->gen1 = fcport->rscn_gen;
602         sp->gen2 = fcport->login_gen;
603         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
604
605         sp->done = qla2x00_async_adisc_sp_done;
606         if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
607                 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
608
609         ql_dbg(ql_dbg_disc, vha, 0x206f,
610             "Async-adisc - hdl=%x loopid=%x portid=%06x %8phC.\n",
611             sp->handle, fcport->loop_id, fcport->d_id.b24, fcport->port_name);
612
613         rval = qla2x00_start_sp(sp);
614         if (rval != QLA_SUCCESS)
615                 goto done_free_sp;
616
617         return rval;
618
619 done_free_sp:
620         sp->free(sp);
621 done:
622         fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
623         qla2x00_post_async_adisc_work(vha, fcport, data);
624         return rval;
625 }
626
627 static bool qla2x00_is_reserved_id(scsi_qla_host_t *vha, uint16_t loop_id)
628 {
629         struct qla_hw_data *ha = vha->hw;
630
631         if (IS_FWI2_CAPABLE(ha))
632                 return loop_id > NPH_LAST_HANDLE;
633
634         return (loop_id > ha->max_loop_id && loop_id < SNS_FIRST_LOOP_ID) ||
635                 loop_id == MANAGEMENT_SERVER || loop_id == BROADCAST;
636 }
637
638 /**
639  * qla2x00_find_new_loop_id - scan through our port list and find a new usable loop ID
640  * @vha: adapter state pointer.
641  * @dev: port structure pointer.
642  *
643  * Returns:
644  *      qla2x00 local function return status code.
645  *
646  * Context:
647  *      Kernel context.
648  */
649 static int qla2x00_find_new_loop_id(scsi_qla_host_t *vha, fc_port_t *dev)
650 {
651         int     rval;
652         struct qla_hw_data *ha = vha->hw;
653         unsigned long flags = 0;
654
655         rval = QLA_SUCCESS;
656
657         spin_lock_irqsave(&ha->vport_slock, flags);
658
659         dev->loop_id = find_first_zero_bit(ha->loop_id_map, LOOPID_MAP_SIZE);
660         if (dev->loop_id >= LOOPID_MAP_SIZE ||
661             qla2x00_is_reserved_id(vha, dev->loop_id)) {
662                 dev->loop_id = FC_NO_LOOP_ID;
663                 rval = QLA_FUNCTION_FAILED;
664         } else {
665                 set_bit(dev->loop_id, ha->loop_id_map);
666         }
667         spin_unlock_irqrestore(&ha->vport_slock, flags);
668
669         if (rval == QLA_SUCCESS)
670                 ql_dbg(ql_dbg_disc, dev->vha, 0x2086,
671                        "Assigning new loopid=%x, portid=%x.\n",
672                        dev->loop_id, dev->d_id.b24);
673         else
674                 ql_log(ql_log_warn, dev->vha, 0x2087,
675                        "No loop_id's available, portid=%x.\n",
676                        dev->d_id.b24);
677
678         return rval;
679 }
680
681 void qla2x00_clear_loop_id(fc_port_t *fcport)
682 {
683         struct qla_hw_data *ha = fcport->vha->hw;
684
685         if (fcport->loop_id == FC_NO_LOOP_ID ||
686             qla2x00_is_reserved_id(fcport->vha, fcport->loop_id))
687                 return;
688
689         clear_bit(fcport->loop_id, ha->loop_id_map);
690         fcport->loop_id = FC_NO_LOOP_ID;
691 }
692
693 static void qla24xx_handle_gnl_done_event(scsi_qla_host_t *vha,
694         struct event_arg *ea)
695 {
696         fc_port_t *fcport, *conflict_fcport;
697         struct get_name_list_extended *e;
698         u16 i, n, found = 0, loop_id;
699         port_id_t id;
700         u64 wwn;
701         u16 data[2];
702         u8 current_login_state, nvme_cls;
703
704         fcport = ea->fcport;
705         ql_dbg(ql_dbg_disc, vha, 0xffff,
706             "%s %8phC DS %d LS rc %d %d login %d|%d rscn %d|%d lid %d\n",
707             __func__, fcport->port_name, fcport->disc_state,
708             fcport->fw_login_state, ea->rc,
709             fcport->login_gen, fcport->last_login_gen,
710             fcport->rscn_gen, fcport->last_rscn_gen, vha->loop_id);
711
712         if (fcport->disc_state == DSC_DELETE_PEND)
713                 return;
714
715         if (ea->rc) { /* rval */
716                 if (fcport->login_retry == 0) {
717                         ql_dbg(ql_dbg_disc, vha, 0x20de,
718                             "GNL failed Port login retry %8phN, retry cnt=%d.\n",
719                             fcport->port_name, fcport->login_retry);
720                 }
721                 return;
722         }
723
724         if (fcport->last_rscn_gen != fcport->rscn_gen) {
725                 qla_rscn_replay(fcport);
726                 qlt_schedule_sess_for_deletion(fcport);
727                 return;
728         } else if (fcport->last_login_gen != fcport->login_gen) {
729                 ql_dbg(ql_dbg_disc, vha, 0x20e0,
730                     "%s %8phC login gen changed\n",
731                     __func__, fcport->port_name);
732                 return;
733         }
734
735         n = ea->data[0] / sizeof(struct get_name_list_extended);
736
737         ql_dbg(ql_dbg_disc, vha, 0x20e1,
738             "%s %d %8phC n %d %02x%02x%02x lid %d \n",
739             __func__, __LINE__, fcport->port_name, n,
740             fcport->d_id.b.domain, fcport->d_id.b.area,
741             fcport->d_id.b.al_pa, fcport->loop_id);
742
743         for (i = 0; i < n; i++) {
744                 e = &vha->gnl.l[i];
745                 wwn = wwn_to_u64(e->port_name);
746                 id.b.domain = e->port_id[2];
747                 id.b.area = e->port_id[1];
748                 id.b.al_pa = e->port_id[0];
749                 id.b.rsvd_1 = 0;
750
751                 if (memcmp((u8 *)&wwn, fcport->port_name, WWN_SIZE))
752                         continue;
753
754                 if (IS_SW_RESV_ADDR(id))
755                         continue;
756
757                 found = 1;
758
759                 loop_id = le16_to_cpu(e->nport_handle);
760                 loop_id = (loop_id & 0x7fff);
761                 nvme_cls = e->current_login_state >> 4;
762                 current_login_state = e->current_login_state & 0xf;
763
764                 if (PRLI_PHASE(nvme_cls)) {
765                         current_login_state = nvme_cls;
766                         fcport->fc4_type &= ~FS_FC4TYPE_FCP;
767                         fcport->fc4_type |= FS_FC4TYPE_NVME;
768                 } else if (PRLI_PHASE(current_login_state)) {
769                         fcport->fc4_type |= FS_FC4TYPE_FCP;
770                         fcport->fc4_type &= ~FS_FC4TYPE_NVME;
771                 }
772
773                 ql_dbg(ql_dbg_disc, vha, 0x20e2,
774                     "%s found %8phC CLS [%x|%x] fc4_type %d ID[%06x|%06x] lid[%d|%d]\n",
775                     __func__, fcport->port_name,
776                     e->current_login_state, fcport->fw_login_state,
777                     fcport->fc4_type, id.b24, fcport->d_id.b24,
778                     loop_id, fcport->loop_id);
779
780                 switch (fcport->disc_state) {
781                 case DSC_DELETE_PEND:
782                 case DSC_DELETED:
783                         break;
784                 default:
785                         if ((id.b24 != fcport->d_id.b24 &&
786                             fcport->d_id.b24 &&
787                             fcport->loop_id != FC_NO_LOOP_ID) ||
788                             (fcport->loop_id != FC_NO_LOOP_ID &&
789                                 fcport->loop_id != loop_id)) {
790                                 ql_dbg(ql_dbg_disc, vha, 0x20e3,
791                                     "%s %d %8phC post del sess\n",
792                                     __func__, __LINE__, fcport->port_name);
793                                 if (fcport->n2n_flag)
794                                         fcport->d_id.b24 = 0;
795                                 qlt_schedule_sess_for_deletion(fcport);
796                                 return;
797                         }
798                         break;
799                 }
800
801                 fcport->loop_id = loop_id;
802                 if (fcport->n2n_flag)
803                         fcport->d_id.b24 = id.b24;
804
805                 wwn = wwn_to_u64(fcport->port_name);
806                 qlt_find_sess_invalidate_other(vha, wwn,
807                         id, loop_id, &conflict_fcport);
808
809                 if (conflict_fcport) {
810                         /*
811                          * Another share fcport share the same loop_id &
812                          * nport id. Conflict fcport needs to finish
813                          * cleanup before this fcport can proceed to login.
814                          */
815                         conflict_fcport->conflict = fcport;
816                         fcport->login_pause = 1;
817                 }
818
819                 switch (vha->hw->current_topology) {
820                 default:
821                         switch (current_login_state) {
822                         case DSC_LS_PRLI_COMP:
823                                 ql_dbg(ql_dbg_disc + ql_dbg_verbose,
824                                     vha, 0x20e4, "%s %d %8phC post gpdb\n",
825                                     __func__, __LINE__, fcport->port_name);
826
827                                 if ((e->prli_svc_param_word_3[0] & BIT_4) == 0)
828                                         fcport->port_type = FCT_INITIATOR;
829                                 else
830                                         fcport->port_type = FCT_TARGET;
831                                 data[0] = data[1] = 0;
832                                 qla2x00_post_async_adisc_work(vha, fcport,
833                                     data);
834                                 break;
835                         case DSC_LS_PORT_UNAVAIL:
836                         default:
837                                 if (fcport->loop_id == FC_NO_LOOP_ID) {
838                                         qla2x00_find_new_loop_id(vha, fcport);
839                                         fcport->fw_login_state =
840                                             DSC_LS_PORT_UNAVAIL;
841                                 }
842                                 ql_dbg(ql_dbg_disc, vha, 0x20e5,
843                                     "%s %d %8phC\n", __func__, __LINE__,
844                                     fcport->port_name);
845                                 qla24xx_fcport_handle_login(vha, fcport);
846                                 break;
847                         }
848                         break;
849                 case ISP_CFG_N:
850                         fcport->fw_login_state = current_login_state;
851                         fcport->d_id = id;
852                         switch (current_login_state) {
853                         case DSC_LS_PRLI_PEND:
854                                 /*
855                                  * In the middle of PRLI. Let it finish.
856                                  * Allow relogin code to recheck state again
857                                  * with GNL. Push disc_state back to DELETED
858                                  * so GNL can go out again
859                                  */
860                                 qla2x00_set_fcport_disc_state(fcport,
861                                     DSC_DELETED);
862                                 break;
863                         case DSC_LS_PRLI_COMP:
864                                 if ((e->prli_svc_param_word_3[0] & BIT_4) == 0)
865                                         fcport->port_type = FCT_INITIATOR;
866                                 else
867                                         fcport->port_type = FCT_TARGET;
868
869                                 data[0] = data[1] = 0;
870                                 qla2x00_post_async_adisc_work(vha, fcport,
871                                     data);
872                                 break;
873                         case DSC_LS_PLOGI_COMP:
874                                 if (fcport_is_bigger(fcport)) {
875                                         /* local adapter is smaller */
876                                         if (fcport->loop_id != FC_NO_LOOP_ID)
877                                                 qla2x00_clear_loop_id(fcport);
878
879                                         fcport->loop_id = loop_id;
880                                         qla24xx_fcport_handle_login(vha,
881                                             fcport);
882                                         break;
883                                 }
884                                 fallthrough;
885                         default:
886                                 if (fcport_is_smaller(fcport)) {
887                                         /* local adapter is bigger */
888                                         if (fcport->loop_id != FC_NO_LOOP_ID)
889                                                 qla2x00_clear_loop_id(fcport);
890
891                                         fcport->loop_id = loop_id;
892                                         qla24xx_fcport_handle_login(vha,
893                                             fcport);
894                                 }
895                                 break;
896                         }
897                         break;
898                 } /* switch (ha->current_topology) */
899         }
900
901         if (!found) {
902                 switch (vha->hw->current_topology) {
903                 case ISP_CFG_F:
904                 case ISP_CFG_FL:
905                         for (i = 0; i < n; i++) {
906                                 e = &vha->gnl.l[i];
907                                 id.b.domain = e->port_id[0];
908                                 id.b.area = e->port_id[1];
909                                 id.b.al_pa = e->port_id[2];
910                                 id.b.rsvd_1 = 0;
911                                 loop_id = le16_to_cpu(e->nport_handle);
912
913                                 if (fcport->d_id.b24 == id.b24) {
914                                         conflict_fcport =
915                                             qla2x00_find_fcport_by_wwpn(vha,
916                                                 e->port_name, 0);
917                                         if (conflict_fcport) {
918                                                 ql_dbg(ql_dbg_disc + ql_dbg_verbose,
919                                                     vha, 0x20e5,
920                                                     "%s %d %8phC post del sess\n",
921                                                     __func__, __LINE__,
922                                                     conflict_fcport->port_name);
923                                                 qlt_schedule_sess_for_deletion
924                                                         (conflict_fcport);
925                                         }
926                                 }
927                                 /*
928                                  * FW already picked this loop id for
929                                  * another fcport
930                                  */
931                                 if (fcport->loop_id == loop_id)
932                                         fcport->loop_id = FC_NO_LOOP_ID;
933                         }
934                         qla24xx_fcport_handle_login(vha, fcport);
935                         break;
936                 case ISP_CFG_N:
937                         qla2x00_set_fcport_disc_state(fcport, DSC_DELETED);
938                         if (time_after_eq(jiffies, fcport->dm_login_expire)) {
939                                 if (fcport->n2n_link_reset_cnt < 2) {
940                                         fcport->n2n_link_reset_cnt++;
941                                         /*
942                                          * remote port is not sending PLOGI.
943                                          * Reset link to kick start his state
944                                          * machine
945                                          */
946                                         set_bit(N2N_LINK_RESET,
947                                             &vha->dpc_flags);
948                                 } else {
949                                         if (fcport->n2n_chip_reset < 1) {
950                                                 ql_log(ql_log_info, vha, 0x705d,
951                                                     "Chip reset to bring laser down");
952                                                 set_bit(ISP_ABORT_NEEDED,
953                                                     &vha->dpc_flags);
954                                                 fcport->n2n_chip_reset++;
955                                         } else {
956                                                 ql_log(ql_log_info, vha, 0x705d,
957                                                     "Remote port %8ph is not coming back\n",
958                                                     fcport->port_name);
959                                                 fcport->scan_state = 0;
960                                         }
961                                 }
962                                 qla2xxx_wake_dpc(vha);
963                         } else {
964                                 /*
965                                  * report port suppose to do PLOGI. Give him
966                                  * more time. FW will catch it.
967                                  */
968                                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
969                         }
970                         break;
971                 case ISP_CFG_NL:
972                         qla24xx_fcport_handle_login(vha, fcport);
973                         break;
974                 default:
975                         break;
976                 }
977         }
978 } /* gnl_event */
979
980 static void qla24xx_async_gnl_sp_done(srb_t *sp, int res)
981 {
982         struct scsi_qla_host *vha = sp->vha;
983         unsigned long flags;
984         struct fc_port *fcport = NULL, *tf;
985         u16 i, n = 0, loop_id;
986         struct event_arg ea;
987         struct get_name_list_extended *e;
988         u64 wwn;
989         struct list_head h;
990         bool found = false;
991
992         ql_dbg(ql_dbg_disc, vha, 0x20e7,
993             "Async done-%s res %x mb[1]=%x mb[2]=%x \n",
994             sp->name, res, sp->u.iocb_cmd.u.mbx.in_mb[1],
995             sp->u.iocb_cmd.u.mbx.in_mb[2]);
996
997
998         sp->fcport->flags &= ~(FCF_ASYNC_SENT|FCF_ASYNC_ACTIVE);
999         memset(&ea, 0, sizeof(ea));
1000         ea.sp = sp;
1001         ea.rc = res;
1002
1003         if (sp->u.iocb_cmd.u.mbx.in_mb[1] >=
1004             sizeof(struct get_name_list_extended)) {
1005                 n = sp->u.iocb_cmd.u.mbx.in_mb[1] /
1006                     sizeof(struct get_name_list_extended);
1007                 ea.data[0] = sp->u.iocb_cmd.u.mbx.in_mb[1]; /* amnt xfered */
1008         }
1009
1010         for (i = 0; i < n; i++) {
1011                 e = &vha->gnl.l[i];
1012                 loop_id = le16_to_cpu(e->nport_handle);
1013                 /* mask out reserve bit */
1014                 loop_id = (loop_id & 0x7fff);
1015                 set_bit(loop_id, vha->hw->loop_id_map);
1016                 wwn = wwn_to_u64(e->port_name);
1017
1018                 ql_dbg(ql_dbg_disc, vha, 0x20e8,
1019                     "%s %8phC %02x:%02x:%02x CLS %x/%x lid %x \n",
1020                     __func__, &wwn, e->port_id[2], e->port_id[1],
1021                     e->port_id[0], e->current_login_state, e->last_login_state,
1022                     (loop_id & 0x7fff));
1023         }
1024
1025         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1026
1027         INIT_LIST_HEAD(&h);
1028         fcport = tf = NULL;
1029         if (!list_empty(&vha->gnl.fcports))
1030                 list_splice_init(&vha->gnl.fcports, &h);
1031         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1032
1033         list_for_each_entry_safe(fcport, tf, &h, gnl_entry) {
1034                 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1035                 list_del_init(&fcport->gnl_entry);
1036                 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
1037                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1038                 ea.fcport = fcport;
1039
1040                 qla24xx_handle_gnl_done_event(vha, &ea);
1041         }
1042
1043         /* create new fcport if fw has knowledge of new sessions */
1044         for (i = 0; i < n; i++) {
1045                 port_id_t id;
1046                 u64 wwnn;
1047
1048                 e = &vha->gnl.l[i];
1049                 wwn = wwn_to_u64(e->port_name);
1050
1051                 found = false;
1052                 list_for_each_entry_safe(fcport, tf, &vha->vp_fcports, list) {
1053                         if (!memcmp((u8 *)&wwn, fcport->port_name,
1054                             WWN_SIZE)) {
1055                                 found = true;
1056                                 break;
1057                         }
1058                 }
1059
1060                 id.b.domain = e->port_id[2];
1061                 id.b.area = e->port_id[1];
1062                 id.b.al_pa = e->port_id[0];
1063                 id.b.rsvd_1 = 0;
1064
1065                 if (!found && wwn && !IS_SW_RESV_ADDR(id)) {
1066                         ql_dbg(ql_dbg_disc, vha, 0x2065,
1067                             "%s %d %8phC %06x post new sess\n",
1068                             __func__, __LINE__, (u8 *)&wwn, id.b24);
1069                         wwnn = wwn_to_u64(e->node_name);
1070                         qla24xx_post_newsess_work(vha, &id, (u8 *)&wwn,
1071                             (u8 *)&wwnn, NULL, 0);
1072                 }
1073         }
1074
1075         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1076         vha->gnl.sent = 0;
1077         if (!list_empty(&vha->gnl.fcports)) {
1078                 /* retrigger gnl */
1079                 list_for_each_entry_safe(fcport, tf, &vha->gnl.fcports,
1080                     gnl_entry) {
1081                         list_del_init(&fcport->gnl_entry);
1082                         fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
1083                         if (qla24xx_post_gnl_work(vha, fcport) == QLA_SUCCESS)
1084                                 break;
1085                 }
1086         }
1087         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1088
1089         sp->free(sp);
1090 }
1091
1092 int qla24xx_async_gnl(struct scsi_qla_host *vha, fc_port_t *fcport)
1093 {
1094         srb_t *sp;
1095         struct srb_iocb *mbx;
1096         int rval = QLA_FUNCTION_FAILED;
1097         unsigned long flags;
1098         u16 *mb;
1099
1100         if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
1101                 return rval;
1102
1103         ql_dbg(ql_dbg_disc, vha, 0x20d9,
1104             "Async-gnlist WWPN %8phC \n", fcport->port_name);
1105
1106         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1107         fcport->flags |= FCF_ASYNC_SENT;
1108         qla2x00_set_fcport_disc_state(fcport, DSC_GNL);
1109         fcport->last_rscn_gen = fcport->rscn_gen;
1110         fcport->last_login_gen = fcport->login_gen;
1111
1112         list_add_tail(&fcport->gnl_entry, &vha->gnl.fcports);
1113         if (vha->gnl.sent) {
1114                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1115                 return QLA_SUCCESS;
1116         }
1117         vha->gnl.sent = 1;
1118         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1119
1120         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1121         if (!sp)
1122                 goto done;
1123
1124         sp->type = SRB_MB_IOCB;
1125         sp->name = "gnlist";
1126         sp->gen1 = fcport->rscn_gen;
1127         sp->gen2 = fcport->login_gen;
1128
1129         mbx = &sp->u.iocb_cmd;
1130         mbx->timeout = qla2x00_async_iocb_timeout;
1131         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
1132
1133         mb = sp->u.iocb_cmd.u.mbx.out_mb;
1134         mb[0] = MBC_PORT_NODE_NAME_LIST;
1135         mb[1] = BIT_2 | BIT_3;
1136         mb[2] = MSW(vha->gnl.ldma);
1137         mb[3] = LSW(vha->gnl.ldma);
1138         mb[6] = MSW(MSD(vha->gnl.ldma));
1139         mb[7] = LSW(MSD(vha->gnl.ldma));
1140         mb[8] = vha->gnl.size;
1141         mb[9] = vha->vp_idx;
1142
1143         sp->done = qla24xx_async_gnl_sp_done;
1144
1145         ql_dbg(ql_dbg_disc, vha, 0x20da,
1146             "Async-%s - OUT WWPN %8phC hndl %x\n",
1147             sp->name, fcport->port_name, sp->handle);
1148
1149         rval = qla2x00_start_sp(sp);
1150         if (rval != QLA_SUCCESS)
1151                 goto done_free_sp;
1152
1153         return rval;
1154
1155 done_free_sp:
1156         sp->free(sp);
1157 done:
1158         fcport->flags &= ~(FCF_ASYNC_ACTIVE | FCF_ASYNC_SENT);
1159         return rval;
1160 }
1161
1162 int qla24xx_post_gnl_work(struct scsi_qla_host *vha, fc_port_t *fcport)
1163 {
1164         struct qla_work_evt *e;
1165
1166         e = qla2x00_alloc_work(vha, QLA_EVT_GNL);
1167         if (!e)
1168                 return QLA_FUNCTION_FAILED;
1169
1170         e->u.fcport.fcport = fcport;
1171         fcport->flags |= FCF_ASYNC_ACTIVE;
1172         return qla2x00_post_work(vha, e);
1173 }
1174
1175 static void qla24xx_async_gpdb_sp_done(srb_t *sp, int res)
1176 {
1177         struct scsi_qla_host *vha = sp->vha;
1178         struct qla_hw_data *ha = vha->hw;
1179         fc_port_t *fcport = sp->fcport;
1180         u16 *mb = sp->u.iocb_cmd.u.mbx.in_mb;
1181         struct event_arg ea;
1182
1183         ql_dbg(ql_dbg_disc, vha, 0x20db,
1184             "Async done-%s res %x, WWPN %8phC mb[1]=%x mb[2]=%x \n",
1185             sp->name, res, fcport->port_name, mb[1], mb[2]);
1186
1187         fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
1188
1189         if (res == QLA_FUNCTION_TIMEOUT)
1190                 goto done;
1191
1192         memset(&ea, 0, sizeof(ea));
1193         ea.fcport = fcport;
1194         ea.sp = sp;
1195
1196         qla24xx_handle_gpdb_event(vha, &ea);
1197
1198 done:
1199         dma_pool_free(ha->s_dma_pool, sp->u.iocb_cmd.u.mbx.in,
1200                 sp->u.iocb_cmd.u.mbx.in_dma);
1201
1202         sp->free(sp);
1203 }
1204
1205 static int qla24xx_post_prli_work(struct scsi_qla_host *vha, fc_port_t *fcport)
1206 {
1207         struct qla_work_evt *e;
1208
1209         if (vha->host->active_mode == MODE_TARGET)
1210                 return QLA_FUNCTION_FAILED;
1211
1212         e = qla2x00_alloc_work(vha, QLA_EVT_PRLI);
1213         if (!e)
1214                 return QLA_FUNCTION_FAILED;
1215
1216         e->u.fcport.fcport = fcport;
1217
1218         return qla2x00_post_work(vha, e);
1219 }
1220
1221 static void qla2x00_async_prli_sp_done(srb_t *sp, int res)
1222 {
1223         struct scsi_qla_host *vha = sp->vha;
1224         struct srb_iocb *lio = &sp->u.iocb_cmd;
1225         struct event_arg ea;
1226
1227         ql_dbg(ql_dbg_disc, vha, 0x2129,
1228             "%s %8phC res %d \n", __func__,
1229             sp->fcport->port_name, res);
1230
1231         sp->fcport->flags &= ~FCF_ASYNC_SENT;
1232
1233         if (!test_bit(UNLOADING, &vha->dpc_flags)) {
1234                 memset(&ea, 0, sizeof(ea));
1235                 ea.fcport = sp->fcport;
1236                 ea.data[0] = lio->u.logio.data[0];
1237                 ea.data[1] = lio->u.logio.data[1];
1238                 ea.iop[0] = lio->u.logio.iop[0];
1239                 ea.iop[1] = lio->u.logio.iop[1];
1240                 ea.sp = sp;
1241
1242                 qla24xx_handle_prli_done_event(vha, &ea);
1243         }
1244
1245         sp->free(sp);
1246 }
1247
1248 int
1249 qla24xx_async_prli(struct scsi_qla_host *vha, fc_port_t *fcport)
1250 {
1251         srb_t *sp;
1252         struct srb_iocb *lio;
1253         int rval = QLA_FUNCTION_FAILED;
1254
1255         if (!vha->flags.online) {
1256                 ql_dbg(ql_dbg_disc, vha, 0xffff, "%s %d %8phC exit\n",
1257                     __func__, __LINE__, fcport->port_name);
1258                 return rval;
1259         }
1260
1261         if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND ||
1262             fcport->fw_login_state == DSC_LS_PRLI_PEND) &&
1263             qla_dual_mode_enabled(vha)) {
1264                 ql_dbg(ql_dbg_disc, vha, 0xffff, "%s %d %8phC exit\n",
1265                     __func__, __LINE__, fcport->port_name);
1266                 return rval;
1267         }
1268
1269         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1270         if (!sp)
1271                 return rval;
1272
1273         fcport->flags |= FCF_ASYNC_SENT;
1274         fcport->logout_completed = 0;
1275
1276         sp->type = SRB_PRLI_CMD;
1277         sp->name = "prli";
1278
1279         lio = &sp->u.iocb_cmd;
1280         lio->timeout = qla2x00_async_iocb_timeout;
1281         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
1282
1283         sp->done = qla2x00_async_prli_sp_done;
1284         lio->u.logio.flags = 0;
1285
1286         if (NVME_TARGET(vha->hw, fcport))
1287                 lio->u.logio.flags |= SRB_LOGIN_NVME_PRLI;
1288
1289         ql_dbg(ql_dbg_disc, vha, 0x211b,
1290             "Async-prli - %8phC hdl=%x, loopid=%x portid=%06x retries=%d fc4type %x priority %x %s.\n",
1291             fcport->port_name, sp->handle, fcport->loop_id, fcport->d_id.b24,
1292             fcport->login_retry, fcport->fc4_type, vha->hw->fc4_type_priority,
1293             NVME_TARGET(vha->hw, fcport) ? "nvme" : "fcp");
1294
1295         rval = qla2x00_start_sp(sp);
1296         if (rval != QLA_SUCCESS) {
1297                 fcport->flags |= FCF_LOGIN_NEEDED;
1298                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1299                 goto done_free_sp;
1300         }
1301
1302         return rval;
1303
1304 done_free_sp:
1305         sp->free(sp);
1306         fcport->flags &= ~FCF_ASYNC_SENT;
1307         return rval;
1308 }
1309
1310 int qla24xx_post_gpdb_work(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
1311 {
1312         struct qla_work_evt *e;
1313
1314         e = qla2x00_alloc_work(vha, QLA_EVT_GPDB);
1315         if (!e)
1316                 return QLA_FUNCTION_FAILED;
1317
1318         e->u.fcport.fcport = fcport;
1319         e->u.fcport.opt = opt;
1320         fcport->flags |= FCF_ASYNC_ACTIVE;
1321         return qla2x00_post_work(vha, e);
1322 }
1323
1324 int qla24xx_async_gpdb(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
1325 {
1326         srb_t *sp;
1327         struct srb_iocb *mbx;
1328         int rval = QLA_FUNCTION_FAILED;
1329         u16 *mb;
1330         dma_addr_t pd_dma;
1331         struct port_database_24xx *pd;
1332         struct qla_hw_data *ha = vha->hw;
1333
1334         if (IS_SESSION_DELETED(fcport)) {
1335                 ql_log(ql_log_warn, vha, 0xffff,
1336                        "%s: %8phC is being delete - not sending command.\n",
1337                        __func__, fcport->port_name);
1338                 fcport->flags &= ~FCF_ASYNC_ACTIVE;
1339                 return rval;
1340         }
1341
1342         if (!vha->flags.online || fcport->flags & FCF_ASYNC_SENT) {
1343                 ql_log(ql_log_warn, vha, 0xffff,
1344                     "%s: %8phC online %d flags %x - not sending command.\n",
1345                     __func__, fcport->port_name, vha->flags.online, fcport->flags);
1346                 goto done;
1347         }
1348
1349         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1350         if (!sp)
1351                 goto done;
1352
1353         qla2x00_set_fcport_disc_state(fcport, DSC_GPDB);
1354
1355         fcport->flags |= FCF_ASYNC_SENT;
1356         sp->type = SRB_MB_IOCB;
1357         sp->name = "gpdb";
1358         sp->gen1 = fcport->rscn_gen;
1359         sp->gen2 = fcport->login_gen;
1360
1361         mbx = &sp->u.iocb_cmd;
1362         mbx->timeout = qla2x00_async_iocb_timeout;
1363         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
1364
1365         pd = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
1366         if (pd == NULL) {
1367                 ql_log(ql_log_warn, vha, 0xd043,
1368                     "Failed to allocate port database structure.\n");
1369                 goto done_free_sp;
1370         }
1371
1372         mb = sp->u.iocb_cmd.u.mbx.out_mb;
1373         mb[0] = MBC_GET_PORT_DATABASE;
1374         mb[1] = fcport->loop_id;
1375         mb[2] = MSW(pd_dma);
1376         mb[3] = LSW(pd_dma);
1377         mb[6] = MSW(MSD(pd_dma));
1378         mb[7] = LSW(MSD(pd_dma));
1379         mb[9] = vha->vp_idx;
1380         mb[10] = opt;
1381
1382         mbx->u.mbx.in = pd;
1383         mbx->u.mbx.in_dma = pd_dma;
1384
1385         sp->done = qla24xx_async_gpdb_sp_done;
1386
1387         ql_dbg(ql_dbg_disc, vha, 0x20dc,
1388             "Async-%s %8phC hndl %x opt %x\n",
1389             sp->name, fcport->port_name, sp->handle, opt);
1390
1391         rval = qla2x00_start_sp(sp);
1392         if (rval != QLA_SUCCESS)
1393                 goto done_free_sp;
1394         return rval;
1395
1396 done_free_sp:
1397         if (pd)
1398                 dma_pool_free(ha->s_dma_pool, pd, pd_dma);
1399
1400         sp->free(sp);
1401         fcport->flags &= ~FCF_ASYNC_SENT;
1402 done:
1403         fcport->flags &= ~FCF_ASYNC_ACTIVE;
1404         qla24xx_post_gpdb_work(vha, fcport, opt);
1405         return rval;
1406 }
1407
1408 static
1409 void __qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
1410 {
1411         unsigned long flags;
1412
1413         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1414         ea->fcport->login_gen++;
1415         ea->fcport->logout_on_delete = 1;
1416
1417         if (!ea->fcport->login_succ && !IS_SW_RESV_ADDR(ea->fcport->d_id)) {
1418                 vha->fcport_count++;
1419                 ea->fcport->login_succ = 1;
1420
1421                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1422                 qla24xx_sched_upd_fcport(ea->fcport);
1423                 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1424         } else if (ea->fcport->login_succ) {
1425                 /*
1426                  * We have an existing session. A late RSCN delivery
1427                  * must have triggered the session to be re-validate.
1428                  * Session is still valid.
1429                  */
1430                 ql_dbg(ql_dbg_disc, vha, 0x20d6,
1431                     "%s %d %8phC session revalidate success\n",
1432                     __func__, __LINE__, ea->fcport->port_name);
1433                 qla2x00_set_fcport_disc_state(ea->fcport, DSC_LOGIN_COMPLETE);
1434         }
1435         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1436 }
1437
1438 static
1439 void qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
1440 {
1441         fc_port_t *fcport = ea->fcport;
1442         struct port_database_24xx *pd;
1443         struct srb *sp = ea->sp;
1444         uint8_t ls;
1445
1446         pd = (struct port_database_24xx *)sp->u.iocb_cmd.u.mbx.in;
1447
1448         fcport->flags &= ~FCF_ASYNC_SENT;
1449
1450         ql_dbg(ql_dbg_disc, vha, 0x20d2,
1451             "%s %8phC DS %d LS %d fc4_type %x rc %d\n", __func__,
1452             fcport->port_name, fcport->disc_state, pd->current_login_state,
1453             fcport->fc4_type, ea->rc);
1454
1455         if (fcport->disc_state == DSC_DELETE_PEND)
1456                 return;
1457
1458         if (NVME_TARGET(vha->hw, fcport))
1459                 ls = pd->current_login_state >> 4;
1460         else
1461                 ls = pd->current_login_state & 0xf;
1462
1463         if (ea->sp->gen2 != fcport->login_gen) {
1464                 /* target side must have changed it. */
1465
1466                 ql_dbg(ql_dbg_disc, vha, 0x20d3,
1467                     "%s %8phC generation changed\n",
1468                     __func__, fcport->port_name);
1469                 return;
1470         } else if (ea->sp->gen1 != fcport->rscn_gen) {
1471                 qla_rscn_replay(fcport);
1472                 qlt_schedule_sess_for_deletion(fcport);
1473                 return;
1474         }
1475
1476         switch (ls) {
1477         case PDS_PRLI_COMPLETE:
1478                 __qla24xx_parse_gpdb(vha, fcport, pd);
1479                 break;
1480         case PDS_PLOGI_PENDING:
1481         case PDS_PLOGI_COMPLETE:
1482         case PDS_PRLI_PENDING:
1483         case PDS_PRLI2_PENDING:
1484                 /* Set discovery state back to GNL to Relogin attempt */
1485                 if (qla_dual_mode_enabled(vha) ||
1486                     qla_ini_mode_enabled(vha)) {
1487                         qla2x00_set_fcport_disc_state(fcport, DSC_GNL);
1488                         set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1489                 }
1490                 return;
1491         case PDS_LOGO_PENDING:
1492         case PDS_PORT_UNAVAILABLE:
1493         default:
1494                 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC post del sess\n",
1495                     __func__, __LINE__, fcport->port_name);
1496                 qlt_schedule_sess_for_deletion(fcport);
1497                 return;
1498         }
1499         __qla24xx_handle_gpdb_event(vha, ea);
1500 } /* gpdb event */
1501
1502 static void qla_chk_n2n_b4_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1503 {
1504         u8 login = 0;
1505         int rc;
1506
1507         ql_dbg(ql_dbg_disc, vha, 0x307b,
1508             "%s %8phC DS %d LS %d lid %d retries=%d\n",
1509             __func__, fcport->port_name, fcport->disc_state,
1510             fcport->fw_login_state, fcport->loop_id, fcport->login_retry);
1511
1512         if (qla_tgt_mode_enabled(vha))
1513                 return;
1514
1515         if (qla_dual_mode_enabled(vha)) {
1516                 if (N2N_TOPO(vha->hw)) {
1517                         u64 mywwn, wwn;
1518
1519                         mywwn = wwn_to_u64(vha->port_name);
1520                         wwn = wwn_to_u64(fcport->port_name);
1521                         if (mywwn > wwn)
1522                                 login = 1;
1523                         else if ((fcport->fw_login_state == DSC_LS_PLOGI_COMP)
1524                             && time_after_eq(jiffies,
1525                                     fcport->plogi_nack_done_deadline))
1526                                 login = 1;
1527                 } else {
1528                         login = 1;
1529                 }
1530         } else {
1531                 /* initiator mode */
1532                 login = 1;
1533         }
1534
1535         if (login && fcport->login_retry) {
1536                 fcport->login_retry--;
1537                 if (fcport->loop_id == FC_NO_LOOP_ID) {
1538                         fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
1539                         rc = qla2x00_find_new_loop_id(vha, fcport);
1540                         if (rc) {
1541                                 ql_dbg(ql_dbg_disc, vha, 0x20e6,
1542                                     "%s %d %8phC post del sess - out of loopid\n",
1543                                     __func__, __LINE__, fcport->port_name);
1544                                 fcport->scan_state = 0;
1545                                 qlt_schedule_sess_for_deletion(fcport);
1546                                 return;
1547                         }
1548                 }
1549                 ql_dbg(ql_dbg_disc, vha, 0x20bf,
1550                     "%s %d %8phC post login\n",
1551                     __func__, __LINE__, fcport->port_name);
1552                 qla2x00_post_async_login_work(vha, fcport, NULL);
1553         }
1554 }
1555
1556 int qla24xx_fcport_handle_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1557 {
1558         u16 data[2];
1559         u64 wwn;
1560         u16 sec;
1561
1562         ql_dbg(ql_dbg_disc, vha, 0x20d8,
1563             "%s %8phC DS %d LS %d P %d fl %x confl %p rscn %d|%d login %d lid %d scan %d\n",
1564             __func__, fcport->port_name, fcport->disc_state,
1565             fcport->fw_login_state, fcport->login_pause, fcport->flags,
1566             fcport->conflict, fcport->last_rscn_gen, fcport->rscn_gen,
1567             fcport->login_gen, fcport->loop_id, fcport->scan_state);
1568
1569         if (fcport->scan_state != QLA_FCPORT_FOUND ||
1570             fcport->disc_state == DSC_DELETE_PEND)
1571                 return 0;
1572
1573         if ((fcport->loop_id != FC_NO_LOOP_ID) &&
1574             qla_dual_mode_enabled(vha) &&
1575             ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
1576              (fcport->fw_login_state == DSC_LS_PRLI_PEND)))
1577                 return 0;
1578
1579         if (fcport->fw_login_state == DSC_LS_PLOGI_COMP &&
1580             !N2N_TOPO(vha->hw)) {
1581                 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1582                         set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1583                         return 0;
1584                 }
1585         }
1586
1587         /* Target won't initiate port login if fabric is present */
1588         if (vha->host->active_mode == MODE_TARGET && !N2N_TOPO(vha->hw))
1589                 return 0;
1590
1591         if (fcport->flags & (FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE)) {
1592                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1593                 return 0;
1594         }
1595
1596         switch (fcport->disc_state) {
1597         case DSC_DELETED:
1598                 wwn = wwn_to_u64(fcport->node_name);
1599                 switch (vha->hw->current_topology) {
1600                 case ISP_CFG_N:
1601                         if (fcport_is_smaller(fcport)) {
1602                                 /* this adapter is bigger */
1603                                 if (fcport->login_retry) {
1604                                         if (fcport->loop_id == FC_NO_LOOP_ID) {
1605                                                 qla2x00_find_new_loop_id(vha,
1606                                                     fcport);
1607                                                 fcport->fw_login_state =
1608                                                     DSC_LS_PORT_UNAVAIL;
1609                                         }
1610                                         fcport->login_retry--;
1611                                         qla_post_els_plogi_work(vha, fcport);
1612                                 } else {
1613                                         ql_log(ql_log_info, vha, 0x705d,
1614                                             "Unable to reach remote port %8phC",
1615                                             fcport->port_name);
1616                                 }
1617                         } else {
1618                                 qla24xx_post_gnl_work(vha, fcport);
1619                         }
1620                         break;
1621                 default:
1622                         if (wwn == 0)    {
1623                                 ql_dbg(ql_dbg_disc, vha, 0xffff,
1624                                     "%s %d %8phC post GNNID\n",
1625                                     __func__, __LINE__, fcport->port_name);
1626                                 qla24xx_post_gnnid_work(vha, fcport);
1627                         } else if (fcport->loop_id == FC_NO_LOOP_ID) {
1628                                 ql_dbg(ql_dbg_disc, vha, 0x20bd,
1629                                     "%s %d %8phC post gnl\n",
1630                                     __func__, __LINE__, fcport->port_name);
1631                                 qla24xx_post_gnl_work(vha, fcport);
1632                         } else {
1633                                 qla_chk_n2n_b4_login(vha, fcport);
1634                         }
1635                         break;
1636                 }
1637                 break;
1638
1639         case DSC_GNL:
1640                 switch (vha->hw->current_topology) {
1641                 case ISP_CFG_N:
1642                         if ((fcport->current_login_state & 0xf) == 0x6) {
1643                                 ql_dbg(ql_dbg_disc, vha, 0x2118,
1644                                     "%s %d %8phC post GPDB work\n",
1645                                     __func__, __LINE__, fcport->port_name);
1646                                 fcport->chip_reset =
1647                                         vha->hw->base_qpair->chip_reset;
1648                                 qla24xx_post_gpdb_work(vha, fcport, 0);
1649                         }  else {
1650                                 ql_dbg(ql_dbg_disc, vha, 0x2118,
1651                                     "%s %d %8phC post %s PRLI\n",
1652                                     __func__, __LINE__, fcport->port_name,
1653                                     NVME_TARGET(vha->hw, fcport) ? "NVME" :
1654                                     "FC");
1655                                 qla24xx_post_prli_work(vha, fcport);
1656                         }
1657                         break;
1658                 default:
1659                         if (fcport->login_pause) {
1660                                 ql_dbg(ql_dbg_disc, vha, 0x20d8,
1661                                     "%s %d %8phC exit\n",
1662                                     __func__, __LINE__,
1663                                     fcport->port_name);
1664                                 fcport->last_rscn_gen = fcport->rscn_gen;
1665                                 fcport->last_login_gen = fcport->login_gen;
1666                                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1667                                 break;
1668                         }
1669                         qla_chk_n2n_b4_login(vha, fcport);
1670                         break;
1671                 }
1672                 break;
1673
1674         case DSC_LOGIN_FAILED:
1675                 if (N2N_TOPO(vha->hw))
1676                         qla_chk_n2n_b4_login(vha, fcport);
1677                 else
1678                         qlt_schedule_sess_for_deletion(fcport);
1679                 break;
1680
1681         case DSC_LOGIN_COMPLETE:
1682                 /* recheck login state */
1683                 data[0] = data[1] = 0;
1684                 qla2x00_post_async_adisc_work(vha, fcport, data);
1685                 break;
1686
1687         case DSC_LOGIN_PEND:
1688                 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP)
1689                         qla24xx_post_prli_work(vha, fcport);
1690                 break;
1691
1692         case DSC_UPD_FCPORT:
1693                 sec =  jiffies_to_msecs(jiffies -
1694                     fcport->jiffies_at_registration)/1000;
1695                 if (fcport->sec_since_registration < sec && sec &&
1696                     !(sec % 60)) {
1697                         fcport->sec_since_registration = sec;
1698                         ql_dbg(ql_dbg_disc, fcport->vha, 0xffff,
1699                             "%s %8phC - Slow Rport registration(%d Sec)\n",
1700                             __func__, fcport->port_name, sec);
1701                 }
1702
1703                 if (fcport->next_disc_state != DSC_DELETE_PEND)
1704                         fcport->next_disc_state = DSC_ADISC;
1705                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1706                 break;
1707
1708         default:
1709                 break;
1710         }
1711
1712         return 0;
1713 }
1714
1715 int qla24xx_post_newsess_work(struct scsi_qla_host *vha, port_id_t *id,
1716     u8 *port_name, u8 *node_name, void *pla, u8 fc4_type)
1717 {
1718         struct qla_work_evt *e;
1719
1720         e = qla2x00_alloc_work(vha, QLA_EVT_NEW_SESS);
1721         if (!e)
1722                 return QLA_FUNCTION_FAILED;
1723
1724         e->u.new_sess.id = *id;
1725         e->u.new_sess.pla = pla;
1726         e->u.new_sess.fc4_type = fc4_type;
1727         memcpy(e->u.new_sess.port_name, port_name, WWN_SIZE);
1728         if (node_name)
1729                 memcpy(e->u.new_sess.node_name, node_name, WWN_SIZE);
1730
1731         return qla2x00_post_work(vha, e);
1732 }
1733
1734 void qla2x00_handle_rscn(scsi_qla_host_t *vha, struct event_arg *ea)
1735 {
1736         fc_port_t *fcport;
1737         unsigned long flags;
1738
1739         switch (ea->id.b.rsvd_1) {
1740         case RSCN_PORT_ADDR:
1741                 fcport = qla2x00_find_fcport_by_nportid(vha, &ea->id, 1);
1742                 if (fcport) {
1743                         if (fcport->flags & FCF_FCP2_DEVICE &&
1744                             atomic_read(&fcport->state) == FCS_ONLINE) {
1745                                 ql_dbg(ql_dbg_disc, vha, 0x2115,
1746                                        "Delaying session delete for FCP2 portid=%06x %8phC ",
1747                                         fcport->d_id.b24, fcport->port_name);
1748                                 return;
1749                         }
1750                         fcport->scan_needed = 1;
1751                         fcport->rscn_gen++;
1752                 }
1753                 break;
1754         case RSCN_AREA_ADDR:
1755                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1756                         if (fcport->flags & FCF_FCP2_DEVICE &&
1757                             atomic_read(&fcport->state) == FCS_ONLINE)
1758                                 continue;
1759
1760                         if ((ea->id.b24 & 0xffff00) == (fcport->d_id.b24 & 0xffff00)) {
1761                                 fcport->scan_needed = 1;
1762                                 fcport->rscn_gen++;
1763                         }
1764                 }
1765                 break;
1766         case RSCN_DOM_ADDR:
1767                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1768                         if (fcport->flags & FCF_FCP2_DEVICE &&
1769                             atomic_read(&fcport->state) == FCS_ONLINE)
1770                                 continue;
1771
1772                         if ((ea->id.b24 & 0xff0000) == (fcport->d_id.b24 & 0xff0000)) {
1773                                 fcport->scan_needed = 1;
1774                                 fcport->rscn_gen++;
1775                         }
1776                 }
1777                 break;
1778         case RSCN_FAB_ADDR:
1779         default:
1780                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1781                         if (fcport->flags & FCF_FCP2_DEVICE &&
1782                             atomic_read(&fcport->state) == FCS_ONLINE)
1783                                 continue;
1784
1785                         fcport->scan_needed = 1;
1786                         fcport->rscn_gen++;
1787                 }
1788                 break;
1789         }
1790
1791         spin_lock_irqsave(&vha->work_lock, flags);
1792         if (vha->scan.scan_flags == 0) {
1793                 ql_dbg(ql_dbg_disc, vha, 0xffff, "%s: schedule\n", __func__);
1794                 vha->scan.scan_flags |= SF_QUEUED;
1795                 schedule_delayed_work(&vha->scan.scan_work, 5);
1796         }
1797         spin_unlock_irqrestore(&vha->work_lock, flags);
1798 }
1799
1800 void qla24xx_handle_relogin_event(scsi_qla_host_t *vha,
1801         struct event_arg *ea)
1802 {
1803         fc_port_t *fcport = ea->fcport;
1804
1805         if (test_bit(UNLOADING, &vha->dpc_flags))
1806                 return;
1807
1808         ql_dbg(ql_dbg_disc, vha, 0x2102,
1809             "%s %8phC DS %d LS %d P %d del %d cnfl %p rscn %d|%d login %d|%d fl %x\n",
1810             __func__, fcport->port_name, fcport->disc_state,
1811             fcport->fw_login_state, fcport->login_pause,
1812             fcport->deleted, fcport->conflict,
1813             fcport->last_rscn_gen, fcport->rscn_gen,
1814             fcport->last_login_gen, fcport->login_gen,
1815             fcport->flags);
1816
1817         if (fcport->last_rscn_gen != fcport->rscn_gen) {
1818                 ql_dbg(ql_dbg_disc, vha, 0x20e9, "%s %d %8phC post gnl\n",
1819                     __func__, __LINE__, fcport->port_name);
1820                 qla24xx_post_gnl_work(vha, fcport);
1821                 return;
1822         }
1823
1824         qla24xx_fcport_handle_login(vha, fcport);
1825 }
1826
1827 void qla_handle_els_plogi_done(scsi_qla_host_t *vha,
1828                                       struct event_arg *ea)
1829 {
1830         /* for pure Target Mode, PRLI will not be initiated */
1831         if (vha->host->active_mode == MODE_TARGET)
1832                 return;
1833
1834         ql_dbg(ql_dbg_disc, vha, 0x2118,
1835             "%s %d %8phC post PRLI\n",
1836             __func__, __LINE__, ea->fcport->port_name);
1837         qla24xx_post_prli_work(vha, ea->fcport);
1838 }
1839
1840 /*
1841  * RSCN(s) came in for this fcport, but the RSCN(s) was not able
1842  * to be consumed by the fcport
1843  */
1844 void qla_rscn_replay(fc_port_t *fcport)
1845 {
1846         struct event_arg ea;
1847
1848         switch (fcport->disc_state) {
1849         case DSC_DELETE_PEND:
1850                 return;
1851         default:
1852                 break;
1853         }
1854
1855         if (fcport->scan_needed) {
1856                 memset(&ea, 0, sizeof(ea));
1857                 ea.id = fcport->d_id;
1858                 ea.id.b.rsvd_1 = RSCN_PORT_ADDR;
1859                 qla2x00_handle_rscn(fcport->vha, &ea);
1860         }
1861 }
1862
1863 static void
1864 qla2x00_tmf_iocb_timeout(void *data)
1865 {
1866         srb_t *sp = data;
1867         struct srb_iocb *tmf = &sp->u.iocb_cmd;
1868         int rc, h;
1869         unsigned long flags;
1870
1871         rc = qla24xx_async_abort_cmd(sp, false);
1872         if (rc) {
1873                 spin_lock_irqsave(sp->qpair->qp_lock_ptr, flags);
1874                 for (h = 1; h < sp->qpair->req->num_outstanding_cmds; h++) {
1875                         if (sp->qpair->req->outstanding_cmds[h] == sp) {
1876                                 sp->qpair->req->outstanding_cmds[h] = NULL;
1877                                 break;
1878                         }
1879                 }
1880                 spin_unlock_irqrestore(sp->qpair->qp_lock_ptr, flags);
1881                 tmf->u.tmf.comp_status = cpu_to_le16(CS_TIMEOUT);
1882                 tmf->u.tmf.data = QLA_FUNCTION_FAILED;
1883                 complete(&tmf->u.tmf.comp);
1884         }
1885 }
1886
1887 static void qla2x00_tmf_sp_done(srb_t *sp, int res)
1888 {
1889         struct srb_iocb *tmf = &sp->u.iocb_cmd;
1890
1891         complete(&tmf->u.tmf.comp);
1892 }
1893
1894 int
1895 qla2x00_async_tm_cmd(fc_port_t *fcport, uint32_t flags, uint32_t lun,
1896         uint32_t tag)
1897 {
1898         struct scsi_qla_host *vha = fcport->vha;
1899         struct srb_iocb *tm_iocb;
1900         srb_t *sp;
1901         int rval = QLA_FUNCTION_FAILED;
1902
1903         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1904         if (!sp)
1905                 goto done;
1906
1907         tm_iocb = &sp->u.iocb_cmd;
1908         sp->type = SRB_TM_CMD;
1909         sp->name = "tmf";
1910
1911         tm_iocb->timeout = qla2x00_tmf_iocb_timeout;
1912         init_completion(&tm_iocb->u.tmf.comp);
1913         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1914
1915         tm_iocb->u.tmf.flags = flags;
1916         tm_iocb->u.tmf.lun = lun;
1917         tm_iocb->u.tmf.data = tag;
1918         sp->done = qla2x00_tmf_sp_done;
1919
1920         ql_dbg(ql_dbg_taskm, vha, 0x802f,
1921             "Async-tmf hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
1922             sp->handle, fcport->loop_id, fcport->d_id.b.domain,
1923             fcport->d_id.b.area, fcport->d_id.b.al_pa);
1924
1925         rval = qla2x00_start_sp(sp);
1926         if (rval != QLA_SUCCESS)
1927                 goto done_free_sp;
1928         wait_for_completion(&tm_iocb->u.tmf.comp);
1929
1930         rval = tm_iocb->u.tmf.data;
1931
1932         if (rval != QLA_SUCCESS) {
1933                 ql_log(ql_log_warn, vha, 0x8030,
1934                     "TM IOCB failed (%x).\n", rval);
1935         }
1936
1937         if (!test_bit(UNLOADING, &vha->dpc_flags) && !IS_QLAFX00(vha->hw)) {
1938                 flags = tm_iocb->u.tmf.flags;
1939                 lun = (uint16_t)tm_iocb->u.tmf.lun;
1940
1941                 /* Issue Marker IOCB */
1942                 qla2x00_marker(vha, vha->hw->base_qpair,
1943                     fcport->loop_id, lun,
1944                     flags == TCF_LUN_RESET ? MK_SYNC_ID_LUN : MK_SYNC_ID);
1945         }
1946
1947 done_free_sp:
1948         sp->free(sp);
1949         fcport->flags &= ~FCF_ASYNC_SENT;
1950 done:
1951         return rval;
1952 }
1953
1954 int
1955 qla24xx_async_abort_command(srb_t *sp)
1956 {
1957         unsigned long   flags = 0;
1958
1959         uint32_t        handle;
1960         fc_port_t       *fcport = sp->fcport;
1961         struct qla_qpair *qpair = sp->qpair;
1962         struct scsi_qla_host *vha = fcport->vha;
1963         struct req_que *req = qpair->req;
1964
1965         spin_lock_irqsave(qpair->qp_lock_ptr, flags);
1966         for (handle = 1; handle < req->num_outstanding_cmds; handle++) {
1967                 if (req->outstanding_cmds[handle] == sp)
1968                         break;
1969         }
1970         spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
1971
1972         if (handle == req->num_outstanding_cmds) {
1973                 /* Command not found. */
1974                 return QLA_FUNCTION_FAILED;
1975         }
1976         if (sp->type == SRB_FXIOCB_DCMD)
1977                 return qlafx00_fx_disc(vha, &vha->hw->mr.fcport,
1978                     FXDISC_ABORT_IOCTL);
1979
1980         return qla24xx_async_abort_cmd(sp, true);
1981 }
1982
1983 static void
1984 qla24xx_handle_prli_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
1985 {
1986         WARN_ONCE(!qla2xxx_is_valid_mbs(ea->data[0]), "mbs: %#x\n",
1987                   ea->data[0]);
1988
1989         switch (ea->data[0]) {
1990         case MBS_COMMAND_COMPLETE:
1991                 ql_dbg(ql_dbg_disc, vha, 0x2118,
1992                     "%s %d %8phC post gpdb\n",
1993                     __func__, __LINE__, ea->fcport->port_name);
1994
1995                 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1996                 ea->fcport->logout_on_delete = 1;
1997                 ea->fcport->nvme_prli_service_param = ea->iop[0];
1998                 if (ea->iop[0] & NVME_PRLI_SP_FIRST_BURST)
1999                         ea->fcport->nvme_first_burst_size =
2000                             (ea->iop[1] & 0xffff) * 512;
2001                 else
2002                         ea->fcport->nvme_first_burst_size = 0;
2003                 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
2004                 break;
2005         default:
2006                 if ((ea->iop[0] == LSC_SCODE_ELS_REJECT) &&
2007                     (ea->iop[1] == 0x50000)) {   /* reson 5=busy expl:0x0 */
2008                         set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
2009                         ea->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
2010                         break;
2011                 }
2012
2013                 ql_dbg(ql_dbg_disc, vha, 0x2118,
2014                        "%s %d %8phC priority %s, fc4type %x\n",
2015                        __func__, __LINE__, ea->fcport->port_name,
2016                        vha->hw->fc4_type_priority == FC4_PRIORITY_FCP ?
2017                        "FCP" : "NVMe", ea->fcport->fc4_type);
2018
2019                 if (N2N_TOPO(vha->hw)) {
2020                         if (vha->hw->fc4_type_priority == FC4_PRIORITY_NVME) {
2021                                 ea->fcport->fc4_type &= ~FS_FC4TYPE_NVME;
2022                                 ea->fcport->fc4_type |= FS_FC4TYPE_FCP;
2023                         } else {
2024                                 ea->fcport->fc4_type &= ~FS_FC4TYPE_FCP;
2025                                 ea->fcport->fc4_type |= FS_FC4TYPE_NVME;
2026                         }
2027
2028                         if (ea->fcport->n2n_link_reset_cnt < 3) {
2029                                 ea->fcport->n2n_link_reset_cnt++;
2030                                 vha->relogin_jif = jiffies + 2 * HZ;
2031                                 /*
2032                                  * PRLI failed. Reset link to kick start
2033                                  * state machine
2034                                  */
2035                                 set_bit(N2N_LINK_RESET, &vha->dpc_flags);
2036                         } else {
2037                                 ql_log(ql_log_warn, vha, 0x2119,
2038                                        "%s %d %8phC Unable to reconnect\n",
2039                                        __func__, __LINE__,
2040                                        ea->fcport->port_name);
2041                         }
2042                 } else {
2043                         /*
2044                          * switch connect. login failed. Take connection down
2045                          * and allow relogin to retrigger
2046                          */
2047                         if (NVME_FCP_TARGET(ea->fcport)) {
2048                                 ql_dbg(ql_dbg_disc, vha, 0x2118,
2049                                        "%s %d %8phC post %s prli\n",
2050                                        __func__, __LINE__,
2051                                        ea->fcport->port_name,
2052                                        (ea->fcport->fc4_type & FS_FC4TYPE_NVME)
2053                                        ? "NVMe" : "FCP");
2054                                 if (vha->hw->fc4_type_priority == FC4_PRIORITY_NVME)
2055                                         ea->fcport->fc4_type &= ~FS_FC4TYPE_NVME;
2056                                 else
2057                                         ea->fcport->fc4_type &= ~FS_FC4TYPE_FCP;
2058                         }
2059
2060                         ea->fcport->flags &= ~FCF_ASYNC_SENT;
2061                         ea->fcport->keep_nport_handle = 0;
2062                         ea->fcport->logout_on_delete = 1;
2063                         qlt_schedule_sess_for_deletion(ea->fcport);
2064                 }
2065                 break;
2066         }
2067 }
2068
2069 void
2070 qla24xx_handle_plogi_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
2071 {
2072         port_id_t cid;  /* conflict Nport id */
2073         u16 lid;
2074         struct fc_port *conflict_fcport;
2075         unsigned long flags;
2076         struct fc_port *fcport = ea->fcport;
2077
2078         ql_dbg(ql_dbg_disc, vha, 0xffff,
2079             "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d data %x|%x iop %x|%x\n",
2080             __func__, fcport->port_name, fcport->disc_state,
2081             fcport->fw_login_state, ea->rc, ea->sp->gen2, fcport->login_gen,
2082             ea->sp->gen1, fcport->rscn_gen,
2083             ea->data[0], ea->data[1], ea->iop[0], ea->iop[1]);
2084
2085         if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
2086             (fcport->fw_login_state == DSC_LS_PRLI_PEND)) {
2087                 ql_dbg(ql_dbg_disc, vha, 0x20ea,
2088                     "%s %d %8phC Remote is trying to login\n",
2089                     __func__, __LINE__, fcport->port_name);
2090                 return;
2091         }
2092
2093         if ((fcport->disc_state == DSC_DELETE_PEND) ||
2094             (fcport->disc_state == DSC_DELETED)) {
2095                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
2096                 return;
2097         }
2098
2099         if (ea->sp->gen2 != fcport->login_gen) {
2100                 /* target side must have changed it. */
2101                 ql_dbg(ql_dbg_disc, vha, 0x20d3,
2102                     "%s %8phC generation changed\n",
2103                     __func__, fcport->port_name);
2104                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
2105                 return;
2106         } else if (ea->sp->gen1 != fcport->rscn_gen) {
2107                 ql_dbg(ql_dbg_disc, vha, 0x20d3,
2108                     "%s %8phC RSCN generation changed\n",
2109                     __func__, fcport->port_name);
2110                 qla_rscn_replay(fcport);
2111                 qlt_schedule_sess_for_deletion(fcport);
2112                 return;
2113         }
2114
2115         WARN_ONCE(!qla2xxx_is_valid_mbs(ea->data[0]), "mbs: %#x\n",
2116                   ea->data[0]);
2117
2118         switch (ea->data[0]) {
2119         case MBS_COMMAND_COMPLETE:
2120                 /*
2121                  * Driver must validate login state - If PRLI not complete,
2122                  * force a relogin attempt via implicit LOGO, PLOGI, and PRLI
2123                  * requests.
2124                  */
2125                 if (NVME_TARGET(vha->hw, ea->fcport)) {
2126                         ql_dbg(ql_dbg_disc, vha, 0x2117,
2127                                 "%s %d %8phC post prli\n",
2128                                 __func__, __LINE__, ea->fcport->port_name);
2129                         qla24xx_post_prli_work(vha, ea->fcport);
2130                 } else {
2131                         ql_dbg(ql_dbg_disc, vha, 0x20ea,
2132                             "%s %d %8phC LoopID 0x%x in use with %06x. post gpdb\n",
2133                             __func__, __LINE__, ea->fcport->port_name,
2134                             ea->fcport->loop_id, ea->fcport->d_id.b24);
2135
2136                         set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
2137                         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
2138                         ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
2139                         ea->fcport->logout_on_delete = 1;
2140                         ea->fcport->send_els_logo = 0;
2141                         ea->fcport->fw_login_state = DSC_LS_PRLI_COMP;
2142                         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
2143
2144                         qla24xx_post_gpdb_work(vha, ea->fcport, 0);
2145                 }
2146                 break;
2147         case MBS_COMMAND_ERROR:
2148                 ql_dbg(ql_dbg_disc, vha, 0x20eb, "%s %d %8phC cmd error %x\n",
2149                     __func__, __LINE__, ea->fcport->port_name, ea->data[1]);
2150
2151                 qlt_schedule_sess_for_deletion(ea->fcport);
2152                 break;
2153         case MBS_LOOP_ID_USED:
2154                 /* data[1] = IO PARAM 1 = nport ID  */
2155                 cid.b.domain = (ea->iop[1] >> 16) & 0xff;
2156                 cid.b.area   = (ea->iop[1] >>  8) & 0xff;
2157                 cid.b.al_pa  = ea->iop[1] & 0xff;
2158                 cid.b.rsvd_1 = 0;
2159
2160                 ql_dbg(ql_dbg_disc, vha, 0x20ec,
2161                     "%s %d %8phC lid %#x in use with pid %06x post gnl\n",
2162                     __func__, __LINE__, ea->fcport->port_name,
2163                     ea->fcport->loop_id, cid.b24);
2164
2165                 set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
2166                 ea->fcport->loop_id = FC_NO_LOOP_ID;
2167                 qla24xx_post_gnl_work(vha, ea->fcport);
2168                 break;
2169         case MBS_PORT_ID_USED:
2170                 lid = ea->iop[1] & 0xffff;
2171                 qlt_find_sess_invalidate_other(vha,
2172                     wwn_to_u64(ea->fcport->port_name),
2173                     ea->fcport->d_id, lid, &conflict_fcport);
2174
2175                 if (conflict_fcport) {
2176                         /*
2177                          * Another fcport share the same loop_id/nport id.
2178                          * Conflict fcport needs to finish cleanup before this
2179                          * fcport can proceed to login.
2180                          */
2181                         conflict_fcport->conflict = ea->fcport;
2182                         ea->fcport->login_pause = 1;
2183
2184                         ql_dbg(ql_dbg_disc, vha, 0x20ed,
2185                             "%s %d %8phC NPortId %06x inuse with loopid 0x%x. post gidpn\n",
2186                             __func__, __LINE__, ea->fcport->port_name,
2187                             ea->fcport->d_id.b24, lid);
2188                 } else {
2189                         ql_dbg(ql_dbg_disc, vha, 0x20ed,
2190                             "%s %d %8phC NPortId %06x inuse with loopid 0x%x. sched delete\n",
2191                             __func__, __LINE__, ea->fcport->port_name,
2192                             ea->fcport->d_id.b24, lid);
2193
2194                         qla2x00_clear_loop_id(ea->fcport);
2195                         set_bit(lid, vha->hw->loop_id_map);
2196                         ea->fcport->loop_id = lid;
2197                         ea->fcport->keep_nport_handle = 0;
2198                         ea->fcport->logout_on_delete = 1;
2199                         qlt_schedule_sess_for_deletion(ea->fcport);
2200                 }
2201                 break;
2202         }
2203         return;
2204 }
2205
2206 /****************************************************************************/
2207 /*                QLogic ISP2x00 Hardware Support Functions.                */
2208 /****************************************************************************/
2209
2210 static int
2211 qla83xx_nic_core_fw_load(scsi_qla_host_t *vha)
2212 {
2213         int rval = QLA_SUCCESS;
2214         struct qla_hw_data *ha = vha->hw;
2215         uint32_t idc_major_ver, idc_minor_ver;
2216         uint16_t config[4];
2217
2218         qla83xx_idc_lock(vha, 0);
2219
2220         /* SV: TODO: Assign initialization timeout from
2221          * flash-info / other param
2222          */
2223         ha->fcoe_dev_init_timeout = QLA83XX_IDC_INITIALIZATION_TIMEOUT;
2224         ha->fcoe_reset_timeout = QLA83XX_IDC_RESET_ACK_TIMEOUT;
2225
2226         /* Set our fcoe function presence */
2227         if (__qla83xx_set_drv_presence(vha) != QLA_SUCCESS) {
2228                 ql_dbg(ql_dbg_p3p, vha, 0xb077,
2229                     "Error while setting DRV-Presence.\n");
2230                 rval = QLA_FUNCTION_FAILED;
2231                 goto exit;
2232         }
2233
2234         /* Decide the reset ownership */
2235         qla83xx_reset_ownership(vha);
2236
2237         /*
2238          * On first protocol driver load:
2239          * Init-Owner: Set IDC-Major-Version and Clear IDC-Lock-Recovery
2240          * register.
2241          * Others: Check compatibility with current IDC Major version.
2242          */
2243         qla83xx_rd_reg(vha, QLA83XX_IDC_MAJOR_VERSION, &idc_major_ver);
2244         if (ha->flags.nic_core_reset_owner) {
2245                 /* Set IDC Major version */
2246                 idc_major_ver = QLA83XX_SUPP_IDC_MAJOR_VERSION;
2247                 qla83xx_wr_reg(vha, QLA83XX_IDC_MAJOR_VERSION, idc_major_ver);
2248
2249                 /* Clearing IDC-Lock-Recovery register */
2250                 qla83xx_wr_reg(vha, QLA83XX_IDC_LOCK_RECOVERY, 0);
2251         } else if (idc_major_ver != QLA83XX_SUPP_IDC_MAJOR_VERSION) {
2252                 /*
2253                  * Clear further IDC participation if we are not compatible with
2254                  * the current IDC Major Version.
2255                  */
2256                 ql_log(ql_log_warn, vha, 0xb07d,
2257                     "Failing load, idc_major_ver=%d, expected_major_ver=%d.\n",
2258                     idc_major_ver, QLA83XX_SUPP_IDC_MAJOR_VERSION);
2259                 __qla83xx_clear_drv_presence(vha);
2260                 rval = QLA_FUNCTION_FAILED;
2261                 goto exit;
2262         }
2263         /* Each function sets its supported Minor version. */
2264         qla83xx_rd_reg(vha, QLA83XX_IDC_MINOR_VERSION, &idc_minor_ver);
2265         idc_minor_ver |= (QLA83XX_SUPP_IDC_MINOR_VERSION << (ha->portnum * 2));
2266         qla83xx_wr_reg(vha, QLA83XX_IDC_MINOR_VERSION, idc_minor_ver);
2267
2268         if (ha->flags.nic_core_reset_owner) {
2269                 memset(config, 0, sizeof(config));
2270                 if (!qla81xx_get_port_config(vha, config))
2271                         qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
2272                             QLA8XXX_DEV_READY);
2273         }
2274
2275         rval = qla83xx_idc_state_handler(vha);
2276
2277 exit:
2278         qla83xx_idc_unlock(vha, 0);
2279
2280         return rval;
2281 }
2282
2283 static void qla_enable_fce_trace(scsi_qla_host_t *vha)
2284 {
2285         int rval;
2286         struct qla_hw_data *ha = vha->hw;
2287
2288         if (ha->fce) {
2289                 ha->flags.fce_enabled = 1;
2290                 memset(ha->fce, 0, fce_calc_size(ha->fce_bufs));
2291                 rval = qla2x00_enable_fce_trace(vha,
2292                     ha->fce_dma, ha->fce_bufs, ha->fce_mb, &ha->fce_bufs);
2293
2294                 if (rval) {
2295                         ql_log(ql_log_warn, vha, 0x8033,
2296                             "Unable to reinitialize FCE (%d).\n", rval);
2297                         ha->flags.fce_enabled = 0;
2298                 }
2299         }
2300 }
2301
2302 static void qla_enable_eft_trace(scsi_qla_host_t *vha)
2303 {
2304         int rval;
2305         struct qla_hw_data *ha = vha->hw;
2306
2307         if (ha->eft) {
2308                 memset(ha->eft, 0, EFT_SIZE);
2309                 rval = qla2x00_enable_eft_trace(vha, ha->eft_dma, EFT_NUM_BUFFERS);
2310
2311                 if (rval) {
2312                         ql_log(ql_log_warn, vha, 0x8034,
2313                             "Unable to reinitialize EFT (%d).\n", rval);
2314                 }
2315         }
2316 }
2317 /*
2318 * qla2x00_initialize_adapter
2319 *      Initialize board.
2320 *
2321 * Input:
2322 *      ha = adapter block pointer.
2323 *
2324 * Returns:
2325 *      0 = success
2326 */
2327 int
2328 qla2x00_initialize_adapter(scsi_qla_host_t *vha)
2329 {
2330         int     rval;
2331         struct qla_hw_data *ha = vha->hw;
2332         struct req_que *req = ha->req_q_map[0];
2333         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2334
2335         memset(&vha->qla_stats, 0, sizeof(vha->qla_stats));
2336         memset(&vha->fc_host_stat, 0, sizeof(vha->fc_host_stat));
2337
2338         /* Clear adapter flags. */
2339         vha->flags.online = 0;
2340         ha->flags.chip_reset_done = 0;
2341         vha->flags.reset_active = 0;
2342         ha->flags.pci_channel_io_perm_failure = 0;
2343         ha->flags.eeh_busy = 0;
2344         vha->qla_stats.jiffies_at_last_reset = get_jiffies_64();
2345         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
2346         atomic_set(&vha->loop_state, LOOP_DOWN);
2347         vha->device_flags = DFLG_NO_CABLE;
2348         vha->dpc_flags = 0;
2349         vha->flags.management_server_logged_in = 0;
2350         vha->marker_needed = 0;
2351         ha->isp_abort_cnt = 0;
2352         ha->beacon_blink_led = 0;
2353
2354         set_bit(0, ha->req_qid_map);
2355         set_bit(0, ha->rsp_qid_map);
2356
2357         ql_dbg(ql_dbg_init, vha, 0x0040,
2358             "Configuring PCI space...\n");
2359         rval = ha->isp_ops->pci_config(vha);
2360         if (rval) {
2361                 ql_log(ql_log_warn, vha, 0x0044,
2362                     "Unable to configure PCI space.\n");
2363                 return (rval);
2364         }
2365
2366         ha->isp_ops->reset_chip(vha);
2367
2368         /* Check for secure flash support */
2369         if (IS_QLA28XX(ha)) {
2370                 if (rd_reg_word(&reg->mailbox12) & BIT_0)
2371                         ha->flags.secure_adapter = 1;
2372                 ql_log(ql_log_info, vha, 0xffff, "Secure Adapter: %s\n",
2373                     (ha->flags.secure_adapter) ? "Yes" : "No");
2374         }
2375
2376
2377         rval = qla2xxx_get_flash_info(vha);
2378         if (rval) {
2379                 ql_log(ql_log_fatal, vha, 0x004f,
2380                     "Unable to validate FLASH data.\n");
2381                 return rval;
2382         }
2383
2384         if (IS_QLA8044(ha)) {
2385                 qla8044_read_reset_template(vha);
2386
2387                 /* NOTE: If ql2xdontresethba==1, set IDC_CTRL DONTRESET_BIT0.
2388                  * If DONRESET_BIT0 is set, drivers should not set dev_state
2389                  * to NEED_RESET. But if NEED_RESET is set, drivers should
2390                  * should honor the reset. */
2391                 if (ql2xdontresethba == 1)
2392                         qla8044_set_idc_dontreset(vha);
2393         }
2394
2395         ha->isp_ops->get_flash_version(vha, req->ring);
2396         ql_dbg(ql_dbg_init, vha, 0x0061,
2397             "Configure NVRAM parameters...\n");
2398
2399         /* Let priority default to FCP, can be overridden by nvram_config */
2400         ha->fc4_type_priority = FC4_PRIORITY_FCP;
2401
2402         ha->isp_ops->nvram_config(vha);
2403
2404         if (ha->fc4_type_priority != FC4_PRIORITY_FCP &&
2405             ha->fc4_type_priority != FC4_PRIORITY_NVME)
2406                 ha->fc4_type_priority = FC4_PRIORITY_FCP;
2407
2408         ql_log(ql_log_info, vha, 0xffff, "FC4 priority set to %s\n",
2409                ha->fc4_type_priority == FC4_PRIORITY_FCP ? "FCP" : "NVMe");
2410
2411         if (ha->flags.disable_serdes) {
2412                 /* Mask HBA via NVRAM settings? */
2413                 ql_log(ql_log_info, vha, 0x0077,
2414                     "Masking HBA WWPN %8phN (via NVRAM).\n", vha->port_name);
2415                 return QLA_FUNCTION_FAILED;
2416         }
2417
2418         ql_dbg(ql_dbg_init, vha, 0x0078,
2419             "Verifying loaded RISC code...\n");
2420
2421         /* If smartsan enabled then require fdmi and rdp enabled */
2422         if (ql2xsmartsan) {
2423                 ql2xfdmienable = 1;
2424                 ql2xrdpenable = 1;
2425         }
2426
2427         if (qla2x00_isp_firmware(vha) != QLA_SUCCESS) {
2428                 rval = ha->isp_ops->chip_diag(vha);
2429                 if (rval)
2430                         return (rval);
2431                 rval = qla2x00_setup_chip(vha);
2432                 if (rval)
2433                         return (rval);
2434         }
2435
2436         if (IS_QLA84XX(ha)) {
2437                 ha->cs84xx = qla84xx_get_chip(vha);
2438                 if (!ha->cs84xx) {
2439                         ql_log(ql_log_warn, vha, 0x00d0,
2440                             "Unable to configure ISP84XX.\n");
2441                         return QLA_FUNCTION_FAILED;
2442                 }
2443         }
2444
2445         if (qla_ini_mode_enabled(vha) || qla_dual_mode_enabled(vha))
2446                 rval = qla2x00_init_rings(vha);
2447
2448         /* No point in continuing if firmware initialization failed. */
2449         if (rval != QLA_SUCCESS)
2450                 return rval;
2451
2452         ha->flags.chip_reset_done = 1;
2453
2454         if (rval == QLA_SUCCESS && IS_QLA84XX(ha)) {
2455                 /* Issue verify 84xx FW IOCB to complete 84xx initialization */
2456                 rval = qla84xx_init_chip(vha);
2457                 if (rval != QLA_SUCCESS) {
2458                         ql_log(ql_log_warn, vha, 0x00d4,
2459                             "Unable to initialize ISP84XX.\n");
2460                         qla84xx_put_chip(vha);
2461                 }
2462         }
2463
2464         /* Load the NIC Core f/w if we are the first protocol driver. */
2465         if (IS_QLA8031(ha)) {
2466                 rval = qla83xx_nic_core_fw_load(vha);
2467                 if (rval)
2468                         ql_log(ql_log_warn, vha, 0x0124,
2469                             "Error in initializing NIC Core f/w.\n");
2470         }
2471
2472         if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha))
2473                 qla24xx_read_fcp_prio_cfg(vha);
2474
2475         if (IS_P3P_TYPE(ha))
2476                 qla82xx_set_driver_version(vha, QLA2XXX_VERSION);
2477         else
2478                 qla25xx_set_driver_version(vha, QLA2XXX_VERSION);
2479
2480         return (rval);
2481 }
2482
2483 /**
2484  * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
2485  * @vha: HA context
2486  *
2487  * Returns 0 on success.
2488  */
2489 int
2490 qla2100_pci_config(scsi_qla_host_t *vha)
2491 {
2492         uint16_t w;
2493         unsigned long flags;
2494         struct qla_hw_data *ha = vha->hw;
2495         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2496
2497         pci_set_master(ha->pdev);
2498         pci_try_set_mwi(ha->pdev);
2499
2500         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2501         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2502         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2503
2504         pci_disable_rom(ha->pdev);
2505
2506         /* Get PCI bus information. */
2507         spin_lock_irqsave(&ha->hardware_lock, flags);
2508         ha->pci_attr = rd_reg_word(&reg->ctrl_status);
2509         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2510
2511         return QLA_SUCCESS;
2512 }
2513
2514 /**
2515  * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
2516  * @vha: HA context
2517  *
2518  * Returns 0 on success.
2519  */
2520 int
2521 qla2300_pci_config(scsi_qla_host_t *vha)
2522 {
2523         uint16_t        w;
2524         unsigned long   flags = 0;
2525         uint32_t        cnt;
2526         struct qla_hw_data *ha = vha->hw;
2527         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2528
2529         pci_set_master(ha->pdev);
2530         pci_try_set_mwi(ha->pdev);
2531
2532         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2533         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2534
2535         if (IS_QLA2322(ha) || IS_QLA6322(ha))
2536                 w &= ~PCI_COMMAND_INTX_DISABLE;
2537         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2538
2539         /*
2540          * If this is a 2300 card and not 2312, reset the
2541          * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
2542          * the 2310 also reports itself as a 2300 so we need to get the
2543          * fb revision level -- a 6 indicates it really is a 2300 and
2544          * not a 2310.
2545          */
2546         if (IS_QLA2300(ha)) {
2547                 spin_lock_irqsave(&ha->hardware_lock, flags);
2548
2549                 /* Pause RISC. */
2550                 wrt_reg_word(&reg->hccr, HCCR_PAUSE_RISC);
2551                 for (cnt = 0; cnt < 30000; cnt++) {
2552                         if ((rd_reg_word(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
2553                                 break;
2554
2555                         udelay(10);
2556                 }
2557
2558                 /* Select FPM registers. */
2559                 wrt_reg_word(&reg->ctrl_status, 0x20);
2560                 rd_reg_word(&reg->ctrl_status);
2561
2562                 /* Get the fb rev level */
2563                 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
2564
2565                 if (ha->fb_rev == FPM_2300)
2566                         pci_clear_mwi(ha->pdev);
2567
2568                 /* Deselect FPM registers. */
2569                 wrt_reg_word(&reg->ctrl_status, 0x0);
2570                 rd_reg_word(&reg->ctrl_status);
2571
2572                 /* Release RISC module. */
2573                 wrt_reg_word(&reg->hccr, HCCR_RELEASE_RISC);
2574                 for (cnt = 0; cnt < 30000; cnt++) {
2575                         if ((rd_reg_word(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
2576                                 break;
2577
2578                         udelay(10);
2579                 }
2580
2581                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2582         }
2583
2584         pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2585
2586         pci_disable_rom(ha->pdev);
2587
2588         /* Get PCI bus information. */
2589         spin_lock_irqsave(&ha->hardware_lock, flags);
2590         ha->pci_attr = rd_reg_word(&reg->ctrl_status);
2591         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2592
2593         return QLA_SUCCESS;
2594 }
2595
2596 /**
2597  * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
2598  * @vha: HA context
2599  *
2600  * Returns 0 on success.
2601  */
2602 int
2603 qla24xx_pci_config(scsi_qla_host_t *vha)
2604 {
2605         uint16_t w;
2606         unsigned long flags = 0;
2607         struct qla_hw_data *ha = vha->hw;
2608         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2609
2610         pci_set_master(ha->pdev);
2611         pci_try_set_mwi(ha->pdev);
2612
2613         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2614         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2615         w &= ~PCI_COMMAND_INTX_DISABLE;
2616         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2617
2618         pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2619
2620         /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
2621         if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
2622                 pcix_set_mmrbc(ha->pdev, 2048);
2623
2624         /* PCIe -- adjust Maximum Read Request Size (2048). */
2625         if (pci_is_pcie(ha->pdev))
2626                 pcie_set_readrq(ha->pdev, 4096);
2627
2628         pci_disable_rom(ha->pdev);
2629
2630         ha->chip_revision = ha->pdev->revision;
2631
2632         /* Get PCI bus information. */
2633         spin_lock_irqsave(&ha->hardware_lock, flags);
2634         ha->pci_attr = rd_reg_dword(&reg->ctrl_status);
2635         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2636
2637         return QLA_SUCCESS;
2638 }
2639
2640 /**
2641  * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
2642  * @vha: HA context
2643  *
2644  * Returns 0 on success.
2645  */
2646 int
2647 qla25xx_pci_config(scsi_qla_host_t *vha)
2648 {
2649         uint16_t w;
2650         struct qla_hw_data *ha = vha->hw;
2651
2652         pci_set_master(ha->pdev);
2653         pci_try_set_mwi(ha->pdev);
2654
2655         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2656         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2657         w &= ~PCI_COMMAND_INTX_DISABLE;
2658         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2659
2660         /* PCIe -- adjust Maximum Read Request Size (2048). */
2661         if (pci_is_pcie(ha->pdev))
2662                 pcie_set_readrq(ha->pdev, 4096);
2663
2664         pci_disable_rom(ha->pdev);
2665
2666         ha->chip_revision = ha->pdev->revision;
2667
2668         return QLA_SUCCESS;
2669 }
2670
2671 /**
2672  * qla2x00_isp_firmware() - Choose firmware image.
2673  * @vha: HA context
2674  *
2675  * Returns 0 on success.
2676  */
2677 static int
2678 qla2x00_isp_firmware(scsi_qla_host_t *vha)
2679 {
2680         int  rval;
2681         uint16_t loop_id, topo, sw_cap;
2682         uint8_t domain, area, al_pa;
2683         struct qla_hw_data *ha = vha->hw;
2684
2685         /* Assume loading risc code */
2686         rval = QLA_FUNCTION_FAILED;
2687
2688         if (ha->flags.disable_risc_code_load) {
2689                 ql_log(ql_log_info, vha, 0x0079, "RISC CODE NOT loaded.\n");
2690
2691                 /* Verify checksum of loaded RISC code. */
2692                 rval = qla2x00_verify_checksum(vha, ha->fw_srisc_address);
2693                 if (rval == QLA_SUCCESS) {
2694                         /* And, verify we are not in ROM code. */
2695                         rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
2696                             &area, &domain, &topo, &sw_cap);
2697                 }
2698         }
2699
2700         if (rval)
2701                 ql_dbg(ql_dbg_init, vha, 0x007a,
2702                     "**** Load RISC code ****.\n");
2703
2704         return (rval);
2705 }
2706
2707 /**
2708  * qla2x00_reset_chip() - Reset ISP chip.
2709  * @vha: HA context
2710  *
2711  * Returns 0 on success.
2712  */
2713 int
2714 qla2x00_reset_chip(scsi_qla_host_t *vha)
2715 {
2716         unsigned long   flags = 0;
2717         struct qla_hw_data *ha = vha->hw;
2718         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2719         uint32_t        cnt;
2720         uint16_t        cmd;
2721         int rval = QLA_FUNCTION_FAILED;
2722
2723         if (unlikely(pci_channel_offline(ha->pdev)))
2724                 return rval;
2725
2726         ha->isp_ops->disable_intrs(ha);
2727
2728         spin_lock_irqsave(&ha->hardware_lock, flags);
2729
2730         /* Turn off master enable */
2731         cmd = 0;
2732         pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
2733         cmd &= ~PCI_COMMAND_MASTER;
2734         pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2735
2736         if (!IS_QLA2100(ha)) {
2737                 /* Pause RISC. */
2738                 wrt_reg_word(&reg->hccr, HCCR_PAUSE_RISC);
2739                 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
2740                         for (cnt = 0; cnt < 30000; cnt++) {
2741                                 if ((rd_reg_word(&reg->hccr) &
2742                                     HCCR_RISC_PAUSE) != 0)
2743                                         break;
2744                                 udelay(100);
2745                         }
2746                 } else {
2747                         rd_reg_word(&reg->hccr);        /* PCI Posting. */
2748                         udelay(10);
2749                 }
2750
2751                 /* Select FPM registers. */
2752                 wrt_reg_word(&reg->ctrl_status, 0x20);
2753                 rd_reg_word(&reg->ctrl_status);         /* PCI Posting. */
2754
2755                 /* FPM Soft Reset. */
2756                 wrt_reg_word(&reg->fpm_diag_config, 0x100);
2757                 rd_reg_word(&reg->fpm_diag_config);     /* PCI Posting. */
2758
2759                 /* Toggle Fpm Reset. */
2760                 if (!IS_QLA2200(ha)) {
2761                         wrt_reg_word(&reg->fpm_diag_config, 0x0);
2762                         rd_reg_word(&reg->fpm_diag_config); /* PCI Posting. */
2763                 }
2764
2765                 /* Select frame buffer registers. */
2766                 wrt_reg_word(&reg->ctrl_status, 0x10);
2767                 rd_reg_word(&reg->ctrl_status);         /* PCI Posting. */
2768
2769                 /* Reset frame buffer FIFOs. */
2770                 if (IS_QLA2200(ha)) {
2771                         WRT_FB_CMD_REG(ha, reg, 0xa000);
2772                         RD_FB_CMD_REG(ha, reg);         /* PCI Posting. */
2773                 } else {
2774                         WRT_FB_CMD_REG(ha, reg, 0x00fc);
2775
2776                         /* Read back fb_cmd until zero or 3 seconds max */
2777                         for (cnt = 0; cnt < 3000; cnt++) {
2778                                 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
2779                                         break;
2780                                 udelay(100);
2781                         }
2782                 }
2783
2784                 /* Select RISC module registers. */
2785                 wrt_reg_word(&reg->ctrl_status, 0);
2786                 rd_reg_word(&reg->ctrl_status);         /* PCI Posting. */
2787
2788                 /* Reset RISC processor. */
2789                 wrt_reg_word(&reg->hccr, HCCR_RESET_RISC);
2790                 rd_reg_word(&reg->hccr);                /* PCI Posting. */
2791
2792                 /* Release RISC processor. */
2793                 wrt_reg_word(&reg->hccr, HCCR_RELEASE_RISC);
2794                 rd_reg_word(&reg->hccr);                /* PCI Posting. */
2795         }
2796
2797         wrt_reg_word(&reg->hccr, HCCR_CLR_RISC_INT);
2798         wrt_reg_word(&reg->hccr, HCCR_CLR_HOST_INT);
2799
2800         /* Reset ISP chip. */
2801         wrt_reg_word(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2802
2803         /* Wait for RISC to recover from reset. */
2804         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2805                 /*
2806                  * It is necessary to for a delay here since the card doesn't
2807                  * respond to PCI reads during a reset. On some architectures
2808                  * this will result in an MCA.
2809                  */
2810                 udelay(20);
2811                 for (cnt = 30000; cnt; cnt--) {
2812                         if ((rd_reg_word(&reg->ctrl_status) &
2813                             CSR_ISP_SOFT_RESET) == 0)
2814                                 break;
2815                         udelay(100);
2816                 }
2817         } else
2818                 udelay(10);
2819
2820         /* Reset RISC processor. */
2821         wrt_reg_word(&reg->hccr, HCCR_RESET_RISC);
2822
2823         wrt_reg_word(&reg->semaphore, 0);
2824
2825         /* Release RISC processor. */
2826         wrt_reg_word(&reg->hccr, HCCR_RELEASE_RISC);
2827         rd_reg_word(&reg->hccr);                        /* PCI Posting. */
2828
2829         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2830                 for (cnt = 0; cnt < 30000; cnt++) {
2831                         if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
2832                                 break;
2833
2834                         udelay(100);
2835                 }
2836         } else
2837                 udelay(100);
2838
2839         /* Turn on master enable */
2840         cmd |= PCI_COMMAND_MASTER;
2841         pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2842
2843         /* Disable RISC pause on FPM parity error. */
2844         if (!IS_QLA2100(ha)) {
2845                 wrt_reg_word(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
2846                 rd_reg_word(&reg->hccr);                /* PCI Posting. */
2847         }
2848
2849         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2850
2851         return QLA_SUCCESS;
2852 }
2853
2854 /**
2855  * qla81xx_reset_mpi() - Reset's MPI FW via Write MPI Register MBC.
2856  * @vha: HA context
2857  *
2858  * Returns 0 on success.
2859  */
2860 static int
2861 qla81xx_reset_mpi(scsi_qla_host_t *vha)
2862 {
2863         uint16_t mb[4] = {0x1010, 0, 1, 0};
2864
2865         if (!IS_QLA81XX(vha->hw))
2866                 return QLA_SUCCESS;
2867
2868         return qla81xx_write_mpi_register(vha, mb);
2869 }
2870
2871 /**
2872  * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
2873  * @vha: HA context
2874  *
2875  * Returns 0 on success.
2876  */
2877 static inline int
2878 qla24xx_reset_risc(scsi_qla_host_t *vha)
2879 {
2880         unsigned long flags = 0;
2881         struct qla_hw_data *ha = vha->hw;
2882         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2883         uint32_t cnt;
2884         uint16_t wd;
2885         static int abts_cnt; /* ISP abort retry counts */
2886         int rval = QLA_SUCCESS;
2887
2888         spin_lock_irqsave(&ha->hardware_lock, flags);
2889
2890         /* Reset RISC. */
2891         wrt_reg_dword(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
2892         for (cnt = 0; cnt < 30000; cnt++) {
2893                 if ((rd_reg_dword(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
2894                         break;
2895
2896                 udelay(10);
2897         }
2898
2899         if (!(rd_reg_dword(&reg->ctrl_status) & CSRX_DMA_ACTIVE))
2900                 set_bit(DMA_SHUTDOWN_CMPL, &ha->fw_dump_cap_flags);
2901
2902         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017e,
2903             "HCCR: 0x%x, Control Status %x, DMA active status:0x%x\n",
2904             rd_reg_dword(&reg->hccr),
2905             rd_reg_dword(&reg->ctrl_status),
2906             (rd_reg_dword(&reg->ctrl_status) & CSRX_DMA_ACTIVE));
2907
2908         wrt_reg_dword(&reg->ctrl_status,
2909             CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
2910         pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
2911
2912         udelay(100);
2913
2914         /* Wait for firmware to complete NVRAM accesses. */
2915         rd_reg_word(&reg->mailbox0);
2916         for (cnt = 10000; rd_reg_word(&reg->mailbox0) != 0 &&
2917             rval == QLA_SUCCESS; cnt--) {
2918                 barrier();
2919                 if (cnt)
2920                         udelay(5);
2921                 else
2922                         rval = QLA_FUNCTION_TIMEOUT;
2923         }
2924
2925         if (rval == QLA_SUCCESS)
2926                 set_bit(ISP_MBX_RDY, &ha->fw_dump_cap_flags);
2927
2928         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017f,
2929             "HCCR: 0x%x, MailBox0 Status 0x%x\n",
2930             rd_reg_dword(&reg->hccr),
2931             rd_reg_word(&reg->mailbox0));
2932
2933         /* Wait for soft-reset to complete. */
2934         rd_reg_dword(&reg->ctrl_status);
2935         for (cnt = 0; cnt < 60; cnt++) {
2936                 barrier();
2937                 if ((rd_reg_dword(&reg->ctrl_status) &
2938                     CSRX_ISP_SOFT_RESET) == 0)
2939                         break;
2940
2941                 udelay(5);
2942         }
2943         if (!(rd_reg_dword(&reg->ctrl_status) & CSRX_ISP_SOFT_RESET))
2944                 set_bit(ISP_SOFT_RESET_CMPL, &ha->fw_dump_cap_flags);
2945
2946         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015d,
2947             "HCCR: 0x%x, Soft Reset status: 0x%x\n",
2948             rd_reg_dword(&reg->hccr),
2949             rd_reg_dword(&reg->ctrl_status));
2950
2951         /* If required, do an MPI FW reset now */
2952         if (test_and_clear_bit(MPI_RESET_NEEDED, &vha->dpc_flags)) {
2953                 if (qla81xx_reset_mpi(vha) != QLA_SUCCESS) {
2954                         if (++abts_cnt < 5) {
2955                                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2956                                 set_bit(MPI_RESET_NEEDED, &vha->dpc_flags);
2957                         } else {
2958                                 /*
2959                                  * We exhausted the ISP abort retries. We have to
2960                                  * set the board offline.
2961                                  */
2962                                 abts_cnt = 0;
2963                                 vha->flags.online = 0;
2964                         }
2965                 }
2966         }
2967
2968         wrt_reg_dword(&reg->hccr, HCCRX_SET_RISC_RESET);
2969         rd_reg_dword(&reg->hccr);
2970
2971         wrt_reg_dword(&reg->hccr, HCCRX_REL_RISC_PAUSE);
2972         rd_reg_dword(&reg->hccr);
2973
2974         wrt_reg_dword(&reg->hccr, HCCRX_CLR_RISC_RESET);
2975         rd_reg_dword(&reg->hccr);
2976
2977         rd_reg_word(&reg->mailbox0);
2978         for (cnt = 60; rd_reg_word(&reg->mailbox0) != 0 &&
2979             rval == QLA_SUCCESS; cnt--) {
2980                 barrier();
2981                 if (cnt)
2982                         udelay(5);
2983                 else
2984                         rval = QLA_FUNCTION_TIMEOUT;
2985         }
2986         if (rval == QLA_SUCCESS)
2987                 set_bit(RISC_RDY_AFT_RESET, &ha->fw_dump_cap_flags);
2988
2989         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015e,
2990             "Host Risc 0x%x, mailbox0 0x%x\n",
2991             rd_reg_dword(&reg->hccr),
2992              rd_reg_word(&reg->mailbox0));
2993
2994         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2995
2996         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015f,
2997             "Driver in %s mode\n",
2998             IS_NOPOLLING_TYPE(ha) ? "Interrupt" : "Polling");
2999
3000         if (IS_NOPOLLING_TYPE(ha))
3001                 ha->isp_ops->enable_intrs(ha);
3002
3003         return rval;
3004 }
3005
3006 static void
3007 qla25xx_read_risc_sema_reg(scsi_qla_host_t *vha, uint32_t *data)
3008 {
3009         struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
3010
3011         wrt_reg_dword(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
3012         *data = rd_reg_dword(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFSET);
3013 }
3014
3015 static void
3016 qla25xx_write_risc_sema_reg(scsi_qla_host_t *vha, uint32_t data)
3017 {
3018         struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
3019
3020         wrt_reg_dword(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
3021         wrt_reg_dword(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFSET, data);
3022 }
3023
3024 static void
3025 qla25xx_manipulate_risc_semaphore(scsi_qla_host_t *vha)
3026 {
3027         uint32_t wd32 = 0;
3028         uint delta_msec = 100;
3029         uint elapsed_msec = 0;
3030         uint timeout_msec;
3031         ulong n;
3032
3033         if (vha->hw->pdev->subsystem_device != 0x0175 &&
3034             vha->hw->pdev->subsystem_device != 0x0240)
3035                 return;
3036
3037         wrt_reg_dword(&vha->hw->iobase->isp24.hccr, HCCRX_SET_RISC_PAUSE);
3038         udelay(100);
3039
3040 attempt:
3041         timeout_msec = TIMEOUT_SEMAPHORE;
3042         n = timeout_msec / delta_msec;
3043         while (n--) {
3044                 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_SET);
3045                 qla25xx_read_risc_sema_reg(vha, &wd32);
3046                 if (wd32 & RISC_SEMAPHORE)
3047                         break;
3048                 msleep(delta_msec);
3049                 elapsed_msec += delta_msec;
3050                 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
3051                         goto force;
3052         }
3053
3054         if (!(wd32 & RISC_SEMAPHORE))
3055                 goto force;
3056
3057         if (!(wd32 & RISC_SEMAPHORE_FORCE))
3058                 goto acquired;
3059
3060         qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_CLR);
3061         timeout_msec = TIMEOUT_SEMAPHORE_FORCE;
3062         n = timeout_msec / delta_msec;
3063         while (n--) {
3064                 qla25xx_read_risc_sema_reg(vha, &wd32);
3065                 if (!(wd32 & RISC_SEMAPHORE_FORCE))
3066                         break;
3067                 msleep(delta_msec);
3068                 elapsed_msec += delta_msec;
3069                 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
3070                         goto force;
3071         }
3072
3073         if (wd32 & RISC_SEMAPHORE_FORCE)
3074                 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_CLR);
3075
3076         goto attempt;
3077
3078 force:
3079         qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_SET);
3080
3081 acquired:
3082         return;
3083 }
3084
3085 /**
3086  * qla24xx_reset_chip() - Reset ISP24xx chip.
3087  * @vha: HA context
3088  *
3089  * Returns 0 on success.
3090  */
3091 int
3092 qla24xx_reset_chip(scsi_qla_host_t *vha)
3093 {
3094         struct qla_hw_data *ha = vha->hw;
3095         int rval = QLA_FUNCTION_FAILED;
3096
3097         if (pci_channel_offline(ha->pdev) &&
3098             ha->flags.pci_channel_io_perm_failure) {
3099                 return rval;
3100         }
3101
3102         ha->isp_ops->disable_intrs(ha);
3103
3104         qla25xx_manipulate_risc_semaphore(vha);
3105
3106         /* Perform RISC reset. */
3107         rval = qla24xx_reset_risc(vha);
3108
3109         return rval;
3110 }
3111
3112 /**
3113  * qla2x00_chip_diag() - Test chip for proper operation.
3114  * @vha: HA context
3115  *
3116  * Returns 0 on success.
3117  */
3118 int
3119 qla2x00_chip_diag(scsi_qla_host_t *vha)
3120 {
3121         int             rval;
3122         struct qla_hw_data *ha = vha->hw;
3123         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3124         unsigned long   flags = 0;
3125         uint16_t        data;
3126         uint32_t        cnt;
3127         uint16_t        mb[5];
3128         struct req_que *req = ha->req_q_map[0];
3129
3130         /* Assume a failed state */
3131         rval = QLA_FUNCTION_FAILED;
3132
3133         ql_dbg(ql_dbg_init, vha, 0x007b, "Testing device at %p.\n",
3134                &reg->flash_address);
3135
3136         spin_lock_irqsave(&ha->hardware_lock, flags);
3137
3138         /* Reset ISP chip. */
3139         wrt_reg_word(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
3140
3141         /*
3142          * We need to have a delay here since the card will not respond while
3143          * in reset causing an MCA on some architectures.
3144          */
3145         udelay(20);
3146         data = qla2x00_debounce_register(&reg->ctrl_status);
3147         for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
3148                 udelay(5);
3149                 data = rd_reg_word(&reg->ctrl_status);
3150                 barrier();
3151         }
3152
3153         if (!cnt)
3154                 goto chip_diag_failed;
3155
3156         ql_dbg(ql_dbg_init, vha, 0x007c,
3157             "Reset register cleared by chip reset.\n");
3158
3159         /* Reset RISC processor. */
3160         wrt_reg_word(&reg->hccr, HCCR_RESET_RISC);
3161         wrt_reg_word(&reg->hccr, HCCR_RELEASE_RISC);
3162
3163         /* Workaround for QLA2312 PCI parity error */
3164         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
3165                 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
3166                 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
3167                         udelay(5);
3168                         data = RD_MAILBOX_REG(ha, reg, 0);
3169                         barrier();
3170                 }
3171         } else
3172                 udelay(10);
3173
3174         if (!cnt)
3175                 goto chip_diag_failed;
3176
3177         /* Check product ID of chip */
3178         ql_dbg(ql_dbg_init, vha, 0x007d, "Checking product ID of chip.\n");
3179
3180         mb[1] = RD_MAILBOX_REG(ha, reg, 1);
3181         mb[2] = RD_MAILBOX_REG(ha, reg, 2);
3182         mb[3] = RD_MAILBOX_REG(ha, reg, 3);
3183         mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
3184         if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
3185             mb[3] != PROD_ID_3) {
3186                 ql_log(ql_log_warn, vha, 0x0062,
3187                     "Wrong product ID = 0x%x,0x%x,0x%x.\n",
3188                     mb[1], mb[2], mb[3]);
3189
3190                 goto chip_diag_failed;
3191         }
3192         ha->product_id[0] = mb[1];
3193         ha->product_id[1] = mb[2];
3194         ha->product_id[2] = mb[3];
3195         ha->product_id[3] = mb[4];
3196
3197         /* Adjust fw RISC transfer size */
3198         if (req->length > 1024)
3199                 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
3200         else
3201                 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
3202                     req->length;
3203
3204         if (IS_QLA2200(ha) &&
3205             RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
3206                 /* Limit firmware transfer size with a 2200A */
3207                 ql_dbg(ql_dbg_init, vha, 0x007e, "Found QLA2200A Chip.\n");
3208
3209                 ha->device_type |= DT_ISP2200A;
3210                 ha->fw_transfer_size = 128;
3211         }
3212
3213         /* Wrap Incoming Mailboxes Test. */
3214         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3215
3216         ql_dbg(ql_dbg_init, vha, 0x007f, "Checking mailboxes.\n");
3217         rval = qla2x00_mbx_reg_test(vha);
3218         if (rval)
3219                 ql_log(ql_log_warn, vha, 0x0080,
3220                     "Failed mailbox send register test.\n");
3221         else
3222                 /* Flag a successful rval */
3223                 rval = QLA_SUCCESS;
3224         spin_lock_irqsave(&ha->hardware_lock, flags);
3225
3226 chip_diag_failed:
3227         if (rval)
3228                 ql_log(ql_log_info, vha, 0x0081,
3229                     "Chip diagnostics **** FAILED ****.\n");
3230
3231         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3232
3233         return (rval);
3234 }
3235
3236 /**
3237  * qla24xx_chip_diag() - Test ISP24xx for proper operation.
3238  * @vha: HA context
3239  *
3240  * Returns 0 on success.
3241  */
3242 int
3243 qla24xx_chip_diag(scsi_qla_host_t *vha)
3244 {
3245         int rval;
3246         struct qla_hw_data *ha = vha->hw;
3247         struct req_que *req = ha->req_q_map[0];
3248
3249         if (IS_P3P_TYPE(ha))
3250                 return QLA_SUCCESS;
3251
3252         ha->fw_transfer_size = REQUEST_ENTRY_SIZE * req->length;
3253
3254         rval = qla2x00_mbx_reg_test(vha);
3255         if (rval) {
3256                 ql_log(ql_log_warn, vha, 0x0082,
3257                     "Failed mailbox send register test.\n");
3258         } else {
3259                 /* Flag a successful rval */
3260                 rval = QLA_SUCCESS;
3261         }
3262
3263         return rval;
3264 }
3265
3266 static void
3267 qla2x00_alloc_fce_trace(scsi_qla_host_t *vha)
3268 {
3269         dma_addr_t tc_dma;
3270         void *tc;
3271         struct qla_hw_data *ha = vha->hw;
3272
3273         if (!IS_FWI2_CAPABLE(ha))
3274                 return;
3275
3276         if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
3277             !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
3278                 return;
3279
3280         if (ha->fce) {
3281                 ql_dbg(ql_dbg_init, vha, 0x00bd,
3282                        "%s: FCE Mem is already allocated.\n",
3283                        __func__);
3284                 return;
3285         }
3286
3287         /* Allocate memory for Fibre Channel Event Buffer. */
3288         tc = dma_alloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
3289                                 GFP_KERNEL);
3290         if (!tc) {
3291                 ql_log(ql_log_warn, vha, 0x00be,
3292                        "Unable to allocate (%d KB) for FCE.\n",
3293                        FCE_SIZE / 1024);
3294                 return;
3295         }
3296
3297         ql_dbg(ql_dbg_init, vha, 0x00c0,
3298                "Allocated (%d KB) for FCE...\n", FCE_SIZE / 1024);
3299
3300         ha->fce_dma = tc_dma;
3301         ha->fce = tc;
3302         ha->fce_bufs = FCE_NUM_BUFFERS;
3303 }
3304
3305 static void
3306 qla2x00_alloc_eft_trace(scsi_qla_host_t *vha)
3307 {
3308         dma_addr_t tc_dma;
3309         void *tc;
3310         struct qla_hw_data *ha = vha->hw;
3311
3312         if (!IS_FWI2_CAPABLE(ha))
3313                 return;
3314
3315         if (ha->eft) {
3316                 ql_dbg(ql_dbg_init, vha, 0x00bd,
3317                     "%s: EFT Mem is already allocated.\n",
3318                     __func__);
3319                 return;
3320         }
3321
3322         /* Allocate memory for Extended Trace Buffer. */
3323         tc = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
3324                                 GFP_KERNEL);
3325         if (!tc) {
3326                 ql_log(ql_log_warn, vha, 0x00c1,
3327                        "Unable to allocate (%d KB) for EFT.\n",
3328                        EFT_SIZE / 1024);
3329                 return;
3330         }
3331
3332         ql_dbg(ql_dbg_init, vha, 0x00c3,
3333                "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
3334
3335         ha->eft_dma = tc_dma;
3336         ha->eft = tc;
3337 }
3338
3339 void
3340 qla2x00_alloc_fw_dump(scsi_qla_host_t *vha)
3341 {
3342         uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
3343             eft_size, fce_size, mq_size;
3344         struct qla_hw_data *ha = vha->hw;
3345         struct req_que *req = ha->req_q_map[0];
3346         struct rsp_que *rsp = ha->rsp_q_map[0];
3347         struct qla2xxx_fw_dump *fw_dump;
3348
3349         if (ha->fw_dump) {
3350                 ql_dbg(ql_dbg_init, vha, 0x00bd,
3351                     "Firmware dump already allocated.\n");
3352                 return;
3353         }
3354
3355         ha->fw_dumped = 0;
3356         ha->fw_dump_cap_flags = 0;
3357         dump_size = fixed_size = mem_size = eft_size = fce_size = mq_size = 0;
3358         req_q_size = rsp_q_size = 0;
3359
3360         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3361                 fixed_size = sizeof(struct qla2100_fw_dump);
3362         } else if (IS_QLA23XX(ha)) {
3363                 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
3364                 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
3365                     sizeof(uint16_t);
3366         } else if (IS_FWI2_CAPABLE(ha)) {
3367                 if (IS_QLA83XX(ha))
3368                         fixed_size = offsetof(struct qla83xx_fw_dump, ext_mem);
3369                 else if (IS_QLA81XX(ha))
3370                         fixed_size = offsetof(struct qla81xx_fw_dump, ext_mem);
3371                 else if (IS_QLA25XX(ha))
3372                         fixed_size = offsetof(struct qla25xx_fw_dump, ext_mem);
3373                 else
3374                         fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
3375
3376                 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
3377                     sizeof(uint32_t);
3378                 if (ha->mqenable) {
3379                         if (!IS_QLA83XX(ha))
3380                                 mq_size = sizeof(struct qla2xxx_mq_chain);
3381                         /*
3382                          * Allocate maximum buffer size for all queues - Q0.
3383                          * Resizing must be done at end-of-dump processing.
3384                          */
3385                         mq_size += (ha->max_req_queues - 1) *
3386                             (req->length * sizeof(request_t));
3387                         mq_size += (ha->max_rsp_queues - 1) *
3388                             (rsp->length * sizeof(response_t));
3389                 }
3390                 if (ha->tgt.atio_ring)
3391                         mq_size += ha->tgt.atio_q_length * sizeof(request_t);
3392
3393                 qla2x00_alloc_fce_trace(vha);
3394                 if (ha->fce)
3395                         fce_size = sizeof(struct qla2xxx_fce_chain) + FCE_SIZE;
3396                 qla2x00_alloc_eft_trace(vha);
3397                 if (ha->eft)
3398                         eft_size = EFT_SIZE;
3399         }
3400
3401         if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
3402                 struct fwdt *fwdt = ha->fwdt;
3403                 uint j;
3404
3405                 for (j = 0; j < 2; j++, fwdt++) {
3406                         if (!fwdt->template) {
3407                                 ql_dbg(ql_dbg_init, vha, 0x00ba,
3408                                     "-> fwdt%u no template\n", j);
3409                                 continue;
3410                         }
3411                         ql_dbg(ql_dbg_init, vha, 0x00fa,
3412                             "-> fwdt%u calculating fwdump size...\n", j);
3413                         fwdt->dump_size = qla27xx_fwdt_calculate_dump_size(
3414                             vha, fwdt->template);
3415                         ql_dbg(ql_dbg_init, vha, 0x00fa,
3416                             "-> fwdt%u calculated fwdump size = %#lx bytes\n",
3417                             j, fwdt->dump_size);
3418                         dump_size += fwdt->dump_size;
3419                 }
3420                 /* Add space for spare MPI fw dump. */
3421                 dump_size += ha->fwdt[1].dump_size;
3422         } else {
3423                 req_q_size = req->length * sizeof(request_t);
3424                 rsp_q_size = rsp->length * sizeof(response_t);
3425                 dump_size = offsetof(struct qla2xxx_fw_dump, isp);
3426                 dump_size += fixed_size + mem_size + req_q_size + rsp_q_size
3427                         + eft_size;
3428                 ha->chain_offset = dump_size;
3429                 dump_size += mq_size + fce_size;
3430                 if (ha->exchoffld_buf)
3431                         dump_size += sizeof(struct qla2xxx_offld_chain) +
3432                                 ha->exchoffld_size;
3433                 if (ha->exlogin_buf)
3434                         dump_size += sizeof(struct qla2xxx_offld_chain) +
3435                                 ha->exlogin_size;
3436         }
3437
3438         if (!ha->fw_dump_len || dump_size > ha->fw_dump_alloc_len) {
3439
3440                 ql_dbg(ql_dbg_init, vha, 0x00c5,
3441                     "%s dump_size %d fw_dump_len %d fw_dump_alloc_len %d\n",
3442                     __func__, dump_size, ha->fw_dump_len,
3443                     ha->fw_dump_alloc_len);
3444
3445                 fw_dump = vmalloc(dump_size);
3446                 if (!fw_dump) {
3447                         ql_log(ql_log_warn, vha, 0x00c4,
3448                             "Unable to allocate (%d KB) for firmware dump.\n",
3449                             dump_size / 1024);
3450                 } else {
3451                         mutex_lock(&ha->optrom_mutex);
3452                         if (ha->fw_dumped) {
3453                                 memcpy(fw_dump, ha->fw_dump, ha->fw_dump_len);
3454                                 vfree(ha->fw_dump);
3455                                 ha->fw_dump = fw_dump;
3456                                 ha->fw_dump_alloc_len =  dump_size;
3457                                 ql_dbg(ql_dbg_init, vha, 0x00c5,
3458                                     "Re-Allocated (%d KB) and save firmware dump.\n",
3459                                     dump_size / 1024);
3460                         } else {
3461                                 if (ha->fw_dump)
3462                                         vfree(ha->fw_dump);
3463                                 ha->fw_dump = fw_dump;
3464
3465                                 ha->fw_dump_len = ha->fw_dump_alloc_len =
3466                                     dump_size;
3467                                 ql_dbg(ql_dbg_init, vha, 0x00c5,
3468                                     "Allocated (%d KB) for firmware dump.\n",
3469                                     dump_size / 1024);
3470
3471                                 if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
3472                                         ha->mpi_fw_dump = (char *)fw_dump +
3473                                                 ha->fwdt[1].dump_size;
3474                                         mutex_unlock(&ha->optrom_mutex);
3475                                         return;
3476                                 }
3477
3478                                 ha->fw_dump->signature[0] = 'Q';
3479                                 ha->fw_dump->signature[1] = 'L';
3480                                 ha->fw_dump->signature[2] = 'G';
3481                                 ha->fw_dump->signature[3] = 'C';
3482                                 ha->fw_dump->version = htonl(1);
3483
3484                                 ha->fw_dump->fixed_size = htonl(fixed_size);
3485                                 ha->fw_dump->mem_size = htonl(mem_size);
3486                                 ha->fw_dump->req_q_size = htonl(req_q_size);
3487                                 ha->fw_dump->rsp_q_size = htonl(rsp_q_size);
3488
3489                                 ha->fw_dump->eft_size = htonl(eft_size);
3490                                 ha->fw_dump->eft_addr_l =
3491                                     htonl(LSD(ha->eft_dma));
3492                                 ha->fw_dump->eft_addr_h =
3493                                     htonl(MSD(ha->eft_dma));
3494
3495                                 ha->fw_dump->header_size =
3496                                         htonl(offsetof
3497                                             (struct qla2xxx_fw_dump, isp));
3498                         }
3499                         mutex_unlock(&ha->optrom_mutex);
3500                 }
3501         }
3502 }
3503
3504 static int
3505 qla81xx_mpi_sync(scsi_qla_host_t *vha)
3506 {
3507 #define MPS_MASK        0xe0
3508         int rval;
3509         uint16_t dc;
3510         uint32_t dw;
3511
3512         if (!IS_QLA81XX(vha->hw))
3513                 return QLA_SUCCESS;
3514
3515         rval = qla2x00_write_ram_word(vha, 0x7c00, 1);
3516         if (rval != QLA_SUCCESS) {
3517                 ql_log(ql_log_warn, vha, 0x0105,
3518                     "Unable to acquire semaphore.\n");
3519                 goto done;
3520         }
3521
3522         pci_read_config_word(vha->hw->pdev, 0x54, &dc);
3523         rval = qla2x00_read_ram_word(vha, 0x7a15, &dw);
3524         if (rval != QLA_SUCCESS) {
3525                 ql_log(ql_log_warn, vha, 0x0067, "Unable to read sync.\n");
3526                 goto done_release;
3527         }
3528
3529         dc &= MPS_MASK;
3530         if (dc == (dw & MPS_MASK))
3531                 goto done_release;
3532
3533         dw &= ~MPS_MASK;
3534         dw |= dc;
3535         rval = qla2x00_write_ram_word(vha, 0x7a15, dw);
3536         if (rval != QLA_SUCCESS) {
3537                 ql_log(ql_log_warn, vha, 0x0114, "Unable to gain sync.\n");
3538         }
3539
3540 done_release:
3541         rval = qla2x00_write_ram_word(vha, 0x7c00, 0);
3542         if (rval != QLA_SUCCESS) {
3543                 ql_log(ql_log_warn, vha, 0x006d,
3544                     "Unable to release semaphore.\n");
3545         }
3546
3547 done:
3548         return rval;
3549 }
3550
3551 int
3552 qla2x00_alloc_outstanding_cmds(struct qla_hw_data *ha, struct req_que *req)
3553 {
3554         /* Don't try to reallocate the array */
3555         if (req->outstanding_cmds)
3556                 return QLA_SUCCESS;
3557
3558         if (!IS_FWI2_CAPABLE(ha))
3559                 req->num_outstanding_cmds = DEFAULT_OUTSTANDING_COMMANDS;
3560         else {
3561                 if (ha->cur_fw_xcb_count <= ha->cur_fw_iocb_count)
3562                         req->num_outstanding_cmds = ha->cur_fw_xcb_count;
3563                 else
3564                         req->num_outstanding_cmds = ha->cur_fw_iocb_count;
3565         }
3566
3567         req->outstanding_cmds = kcalloc(req->num_outstanding_cmds,
3568                                         sizeof(srb_t *),
3569                                         GFP_KERNEL);
3570
3571         if (!req->outstanding_cmds) {
3572                 /*
3573                  * Try to allocate a minimal size just so we can get through
3574                  * initialization.
3575                  */
3576                 req->num_outstanding_cmds = MIN_OUTSTANDING_COMMANDS;
3577                 req->outstanding_cmds = kcalloc(req->num_outstanding_cmds,
3578                                                 sizeof(srb_t *),
3579                                                 GFP_KERNEL);
3580
3581                 if (!req->outstanding_cmds) {
3582                         ql_log(ql_log_fatal, NULL, 0x0126,
3583                             "Failed to allocate memory for "
3584                             "outstanding_cmds for req_que %p.\n", req);
3585                         req->num_outstanding_cmds = 0;
3586                         return QLA_FUNCTION_FAILED;
3587                 }
3588         }
3589
3590         return QLA_SUCCESS;
3591 }
3592
3593 #define PRINT_FIELD(_field, _flag, _str) {              \
3594         if (a0->_field & _flag) {\
3595                 if (p) {\
3596                         strcat(ptr, "|");\
3597                         ptr++;\
3598                         leftover--;\
3599                 } \
3600                 len = snprintf(ptr, leftover, "%s", _str);      \
3601                 p = 1;\
3602                 leftover -= len;\
3603                 ptr += len; \
3604         } \
3605 }
3606
3607 static void qla2xxx_print_sfp_info(struct scsi_qla_host *vha)
3608 {
3609 #define STR_LEN 64
3610         struct sff_8247_a0 *a0 = (struct sff_8247_a0 *)vha->hw->sfp_data;
3611         u8 str[STR_LEN], *ptr, p;
3612         int leftover, len;
3613
3614         memset(str, 0, STR_LEN);
3615         snprintf(str, SFF_VEN_NAME_LEN+1, a0->vendor_name);
3616         ql_dbg(ql_dbg_init, vha, 0x015a,
3617             "SFP MFG Name: %s\n", str);
3618
3619         memset(str, 0, STR_LEN);
3620         snprintf(str, SFF_PART_NAME_LEN+1, a0->vendor_pn);
3621         ql_dbg(ql_dbg_init, vha, 0x015c,
3622             "SFP Part Name: %s\n", str);
3623
3624         /* media */
3625         memset(str, 0, STR_LEN);
3626         ptr = str;
3627         leftover = STR_LEN;
3628         p = len = 0;
3629         PRINT_FIELD(fc_med_cc9, FC_MED_TW, "Twin AX");
3630         PRINT_FIELD(fc_med_cc9, FC_MED_TP, "Twisted Pair");
3631         PRINT_FIELD(fc_med_cc9, FC_MED_MI, "Min Coax");
3632         PRINT_FIELD(fc_med_cc9, FC_MED_TV, "Video Coax");
3633         PRINT_FIELD(fc_med_cc9, FC_MED_M6, "MultiMode 62.5um");
3634         PRINT_FIELD(fc_med_cc9, FC_MED_M5, "MultiMode 50um");
3635         PRINT_FIELD(fc_med_cc9, FC_MED_SM, "SingleMode");
3636         ql_dbg(ql_dbg_init, vha, 0x0160,
3637             "SFP Media: %s\n", str);
3638
3639         /* link length */
3640         memset(str, 0, STR_LEN);
3641         ptr = str;
3642         leftover = STR_LEN;
3643         p = len = 0;
3644         PRINT_FIELD(fc_ll_cc7, FC_LL_VL, "Very Long");
3645         PRINT_FIELD(fc_ll_cc7, FC_LL_S, "Short");
3646         PRINT_FIELD(fc_ll_cc7, FC_LL_I, "Intermediate");
3647         PRINT_FIELD(fc_ll_cc7, FC_LL_L, "Long");
3648         PRINT_FIELD(fc_ll_cc7, FC_LL_M, "Medium");
3649         ql_dbg(ql_dbg_init, vha, 0x0196,
3650             "SFP Link Length: %s\n", str);
3651
3652         memset(str, 0, STR_LEN);
3653         ptr = str;
3654         leftover = STR_LEN;
3655         p = len = 0;
3656         PRINT_FIELD(fc_ll_cc7, FC_LL_SA, "Short Wave (SA)");
3657         PRINT_FIELD(fc_ll_cc7, FC_LL_LC, "Long Wave(LC)");
3658         PRINT_FIELD(fc_tec_cc8, FC_TEC_SN, "Short Wave (SN)");
3659         PRINT_FIELD(fc_tec_cc8, FC_TEC_SL, "Short Wave (SL)");
3660         PRINT_FIELD(fc_tec_cc8, FC_TEC_LL, "Long Wave (LL)");
3661         ql_dbg(ql_dbg_init, vha, 0x016e,
3662             "SFP FC Link Tech: %s\n", str);
3663
3664         if (a0->length_km)
3665                 ql_dbg(ql_dbg_init, vha, 0x016f,
3666                     "SFP Distant: %d km\n", a0->length_km);
3667         if (a0->length_100m)
3668                 ql_dbg(ql_dbg_init, vha, 0x0170,
3669                     "SFP Distant: %d m\n", a0->length_100m*100);
3670         if (a0->length_50um_10m)
3671                 ql_dbg(ql_dbg_init, vha, 0x0189,
3672                     "SFP Distant (WL=50um): %d m\n", a0->length_50um_10m * 10);
3673         if (a0->length_62um_10m)
3674                 ql_dbg(ql_dbg_init, vha, 0x018a,
3675                   "SFP Distant (WL=62.5um): %d m\n", a0->length_62um_10m * 10);
3676         if (a0->length_om4_10m)
3677                 ql_dbg(ql_dbg_init, vha, 0x0194,
3678                     "SFP Distant (OM4): %d m\n", a0->length_om4_10m * 10);
3679         if (a0->length_om3_10m)
3680                 ql_dbg(ql_dbg_init, vha, 0x0195,
3681                     "SFP Distant (OM3): %d m\n", a0->length_om3_10m * 10);
3682 }
3683
3684
3685 /**
3686  * qla24xx_detect_sfp()
3687  *
3688  * @vha: adapter state pointer.
3689  *
3690  * @return
3691  *      0 -- Configure firmware to use short-range settings -- normal
3692  *           buffer-to-buffer credits.
3693  *
3694  *      1 -- Configure firmware to use long-range settings -- extra
3695  *           buffer-to-buffer credits should be allocated with
3696  *           ha->lr_distance containing distance settings from NVRAM or SFP
3697  *           (if supported).
3698  */
3699 int
3700 qla24xx_detect_sfp(scsi_qla_host_t *vha)
3701 {
3702         int rc, used_nvram;
3703         struct sff_8247_a0 *a;
3704         struct qla_hw_data *ha = vha->hw;
3705         struct nvram_81xx *nv = ha->nvram;
3706 #define LR_DISTANCE_UNKNOWN     2
3707         static const char * const types[] = { "Short", "Long" };
3708         static const char * const lengths[] = { "(10km)", "(5km)", "" };
3709         u8 ll = 0;
3710
3711         /* Seed with NVRAM settings. */
3712         used_nvram = 0;
3713         ha->flags.lr_detected = 0;
3714         if (IS_BPM_RANGE_CAPABLE(ha) &&
3715             (nv->enhanced_features & NEF_LR_DIST_ENABLE)) {
3716                 used_nvram = 1;
3717                 ha->flags.lr_detected = 1;
3718                 ha->lr_distance =
3719                     (nv->enhanced_features >> LR_DIST_NV_POS)
3720                      & LR_DIST_NV_MASK;
3721         }
3722
3723         if (!IS_BPM_ENABLED(vha))
3724                 goto out;
3725         /* Determine SR/LR capabilities of SFP/Transceiver. */
3726         rc = qla2x00_read_sfp_dev(vha, NULL, 0);
3727         if (rc)
3728                 goto out;
3729
3730         used_nvram = 0;
3731         a = (struct sff_8247_a0 *)vha->hw->sfp_data;
3732         qla2xxx_print_sfp_info(vha);
3733
3734         ha->flags.lr_detected = 0;
3735         ll = a->fc_ll_cc7;
3736         if (ll & FC_LL_VL || ll & FC_LL_L) {
3737                 /* Long range, track length. */
3738                 ha->flags.lr_detected = 1;
3739
3740                 if (a->length_km > 5 || a->length_100m > 50)
3741                         ha->lr_distance = LR_DISTANCE_10K;
3742                 else
3743                         ha->lr_distance = LR_DISTANCE_5K;
3744         }
3745
3746 out:
3747         ql_dbg(ql_dbg_async, vha, 0x507b,
3748             "SFP detect: %s-Range SFP %s (nvr=%x ll=%x lr=%x lrd=%x).\n",
3749             types[ha->flags.lr_detected],
3750             ha->flags.lr_detected ? lengths[ha->lr_distance] :
3751                lengths[LR_DISTANCE_UNKNOWN],
3752             used_nvram, ll, ha->flags.lr_detected, ha->lr_distance);
3753         return ha->flags.lr_detected;
3754 }
3755
3756 void qla_init_iocb_limit(scsi_qla_host_t *vha)
3757 {
3758         u16 i, num_qps;
3759         u32 limit;
3760         struct qla_hw_data *ha = vha->hw;
3761
3762         num_qps = ha->num_qpairs + 1;
3763         limit = (ha->orig_fw_iocb_count * QLA_IOCB_PCT_LIMIT) / 100;
3764
3765         ha->base_qpair->fwres.iocbs_total = ha->orig_fw_iocb_count;
3766         ha->base_qpair->fwres.iocbs_limit = limit;
3767         ha->base_qpair->fwres.iocbs_qp_limit = limit / num_qps;
3768         ha->base_qpair->fwres.iocbs_used = 0;
3769         for (i = 0; i < ha->max_qpairs; i++) {
3770                 if (ha->queue_pair_map[i])  {
3771                         ha->queue_pair_map[i]->fwres.iocbs_total =
3772                                 ha->orig_fw_iocb_count;
3773                         ha->queue_pair_map[i]->fwres.iocbs_limit = limit;
3774                         ha->queue_pair_map[i]->fwres.iocbs_qp_limit =
3775                                 limit / num_qps;
3776                         ha->queue_pair_map[i]->fwres.iocbs_used = 0;
3777                 }
3778         }
3779 }
3780
3781 /**
3782  * qla2x00_setup_chip() - Load and start RISC firmware.
3783  * @vha: HA context
3784  *
3785  * Returns 0 on success.
3786  */
3787 static int
3788 qla2x00_setup_chip(scsi_qla_host_t *vha)
3789 {
3790         int rval;
3791         uint32_t srisc_address = 0;
3792         struct qla_hw_data *ha = vha->hw;
3793         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3794         unsigned long flags;
3795         int done_once = 0;
3796
3797         if (IS_P3P_TYPE(ha)) {
3798                 rval = ha->isp_ops->load_risc(vha, &srisc_address);
3799                 if (rval == QLA_SUCCESS) {
3800                         qla2x00_stop_firmware(vha);
3801                         goto enable_82xx_npiv;
3802                 } else
3803                         goto failed;
3804         }
3805
3806         if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3807                 /* Disable SRAM, Instruction RAM and GP RAM parity.  */
3808                 spin_lock_irqsave(&ha->hardware_lock, flags);
3809                 wrt_reg_word(&reg->hccr, (HCCR_ENABLE_PARITY + 0x0));
3810                 rd_reg_word(&reg->hccr);
3811                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3812         }
3813
3814         qla81xx_mpi_sync(vha);
3815
3816 execute_fw_with_lr:
3817         /* Load firmware sequences */
3818         rval = ha->isp_ops->load_risc(vha, &srisc_address);
3819         if (rval == QLA_SUCCESS) {
3820                 ql_dbg(ql_dbg_init, vha, 0x00c9,
3821                     "Verifying Checksum of loaded RISC code.\n");
3822
3823                 rval = qla2x00_verify_checksum(vha, srisc_address);
3824                 if (rval == QLA_SUCCESS) {
3825                         /* Start firmware execution. */
3826                         ql_dbg(ql_dbg_init, vha, 0x00ca,
3827                             "Starting firmware.\n");
3828
3829                         if (ql2xexlogins)
3830                                 ha->flags.exlogins_enabled = 1;
3831
3832                         if (qla_is_exch_offld_enabled(vha))
3833                                 ha->flags.exchoffld_enabled = 1;
3834
3835                         rval = qla2x00_execute_fw(vha, srisc_address);
3836                         /* Retrieve firmware information. */
3837                         if (rval == QLA_SUCCESS) {
3838                                 /* Enable BPM support? */
3839                                 if (!done_once++ && qla24xx_detect_sfp(vha)) {
3840                                         ql_dbg(ql_dbg_init, vha, 0x00ca,
3841                                             "Re-starting firmware -- BPM.\n");
3842                                         /* Best-effort - re-init. */
3843                                         ha->isp_ops->reset_chip(vha);
3844                                         ha->isp_ops->chip_diag(vha);
3845                                         goto execute_fw_with_lr;
3846                                 }
3847
3848                                 if (IS_ZIO_THRESHOLD_CAPABLE(ha))
3849                                         qla27xx_set_zio_threshold(vha,
3850                                             ha->last_zio_threshold);
3851
3852                                 rval = qla2x00_set_exlogins_buffer(vha);
3853                                 if (rval != QLA_SUCCESS)
3854                                         goto failed;
3855
3856                                 rval = qla2x00_set_exchoffld_buffer(vha);
3857                                 if (rval != QLA_SUCCESS)
3858                                         goto failed;
3859
3860 enable_82xx_npiv:
3861                                 if (IS_P3P_TYPE(ha))
3862                                         qla82xx_check_md_needed(vha);
3863                                 else
3864                                         rval = qla2x00_get_fw_version(vha);
3865                                 if (rval != QLA_SUCCESS)
3866                                         goto failed;
3867                                 ha->flags.npiv_supported = 0;
3868                                 if (IS_QLA2XXX_MIDTYPE(ha) &&
3869                                          (ha->fw_attributes & BIT_2)) {
3870                                         ha->flags.npiv_supported = 1;
3871                                         if ((!ha->max_npiv_vports) ||
3872                                             ((ha->max_npiv_vports + 1) %
3873                                             MIN_MULTI_ID_FABRIC))
3874                                                 ha->max_npiv_vports =
3875                                                     MIN_MULTI_ID_FABRIC - 1;
3876                                 }
3877                                 qla2x00_get_resource_cnts(vha);
3878                                 qla_init_iocb_limit(vha);
3879
3880                                 /*
3881                                  * Allocate the array of outstanding commands
3882                                  * now that we know the firmware resources.
3883                                  */
3884                                 rval = qla2x00_alloc_outstanding_cmds(ha,
3885                                     vha->req);
3886                                 if (rval != QLA_SUCCESS)
3887                                         goto failed;
3888
3889                                 if (ql2xallocfwdump && !(IS_P3P_TYPE(ha)))
3890                                         qla2x00_alloc_fw_dump(vha);
3891
3892                                 qla_enable_fce_trace(vha);
3893                                 qla_enable_eft_trace(vha);
3894                         } else {
3895                                 goto failed;
3896                         }
3897                 } else {
3898                         ql_log(ql_log_fatal, vha, 0x00cd,
3899                             "ISP Firmware failed checksum.\n");
3900                         goto failed;
3901                 }
3902
3903                 /* Enable PUREX PASSTHRU */
3904                 if (ql2xrdpenable || ha->flags.scm_supported_f)
3905                         qla25xx_set_els_cmds_supported(vha);
3906         } else
3907                 goto failed;
3908
3909         if (!IS_FWI2_CAPABLE(ha) && !IS_QLA2100(ha) && !IS_QLA2200(ha)) {
3910                 /* Enable proper parity. */
3911                 spin_lock_irqsave(&ha->hardware_lock, flags);
3912                 if (IS_QLA2300(ha))
3913                         /* SRAM parity */
3914                         wrt_reg_word(&reg->hccr, HCCR_ENABLE_PARITY + 0x1);
3915                 else
3916                         /* SRAM, Instruction RAM and GP RAM parity */
3917                         wrt_reg_word(&reg->hccr, HCCR_ENABLE_PARITY + 0x7);
3918                 rd_reg_word(&reg->hccr);
3919                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3920         }
3921
3922         if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
3923                 ha->flags.fac_supported = 1;
3924         else if (rval == QLA_SUCCESS && IS_FAC_REQUIRED(ha)) {
3925                 uint32_t size;
3926
3927                 rval = qla81xx_fac_get_sector_size(vha, &size);
3928                 if (rval == QLA_SUCCESS) {
3929                         ha->flags.fac_supported = 1;
3930                         ha->fdt_block_size = size << 2;
3931                 } else {
3932                         ql_log(ql_log_warn, vha, 0x00ce,
3933                             "Unsupported FAC firmware (%d.%02d.%02d).\n",
3934                             ha->fw_major_version, ha->fw_minor_version,
3935                             ha->fw_subminor_version);
3936
3937                         if (IS_QLA83XX(ha)) {
3938                                 ha->flags.fac_supported = 0;
3939                                 rval = QLA_SUCCESS;
3940                         }
3941                 }
3942         }
3943 failed:
3944         if (rval) {
3945                 ql_log(ql_log_fatal, vha, 0x00cf,
3946                     "Setup chip ****FAILED****.\n");
3947         }
3948
3949         return (rval);
3950 }
3951
3952 /**
3953  * qla2x00_init_response_q_entries() - Initializes response queue entries.
3954  * @rsp: response queue
3955  *
3956  * Beginning of request ring has initialization control block already built
3957  * by nvram config routine.
3958  *
3959  * Returns 0 on success.
3960  */
3961 void
3962 qla2x00_init_response_q_entries(struct rsp_que *rsp)
3963 {
3964         uint16_t cnt;
3965         response_t *pkt;
3966
3967         rsp->ring_ptr = rsp->ring;
3968         rsp->ring_index    = 0;
3969         rsp->status_srb = NULL;
3970         pkt = rsp->ring_ptr;
3971         for (cnt = 0; cnt < rsp->length; cnt++) {
3972                 pkt->signature = RESPONSE_PROCESSED;
3973                 pkt++;
3974         }
3975 }
3976
3977 /**
3978  * qla2x00_update_fw_options() - Read and process firmware options.
3979  * @vha: HA context
3980  *
3981  * Returns 0 on success.
3982  */
3983 void
3984 qla2x00_update_fw_options(scsi_qla_host_t *vha)
3985 {
3986         uint16_t swing, emphasis, tx_sens, rx_sens;
3987         struct qla_hw_data *ha = vha->hw;
3988
3989         memset(ha->fw_options, 0, sizeof(ha->fw_options));
3990         qla2x00_get_fw_options(vha, ha->fw_options);
3991
3992         if (IS_QLA2100(ha) || IS_QLA2200(ha))
3993                 return;
3994
3995         /* Serial Link options. */
3996         ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0115,
3997             "Serial link options.\n");
3998         ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0109,
3999             ha->fw_seriallink_options, sizeof(ha->fw_seriallink_options));
4000
4001         ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
4002         if (ha->fw_seriallink_options[3] & BIT_2) {
4003                 ha->fw_options[1] |= FO1_SET_EMPHASIS_SWING;
4004
4005                 /*  1G settings */
4006                 swing = ha->fw_seriallink_options[2] & (BIT_2 | BIT_1 | BIT_0);
4007                 emphasis = (ha->fw_seriallink_options[2] &
4008                     (BIT_4 | BIT_3)) >> 3;
4009                 tx_sens = ha->fw_seriallink_options[0] &
4010                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
4011                 rx_sens = (ha->fw_seriallink_options[0] &
4012                     (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
4013                 ha->fw_options[10] = (emphasis << 14) | (swing << 8);
4014                 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
4015                         if (rx_sens == 0x0)
4016                                 rx_sens = 0x3;
4017                         ha->fw_options[10] |= (tx_sens << 4) | rx_sens;
4018                 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
4019                         ha->fw_options[10] |= BIT_5 |
4020                             ((rx_sens & (BIT_1 | BIT_0)) << 2) |
4021                             (tx_sens & (BIT_1 | BIT_0));
4022
4023                 /*  2G settings */
4024                 swing = (ha->fw_seriallink_options[2] &
4025                     (BIT_7 | BIT_6 | BIT_5)) >> 5;
4026                 emphasis = ha->fw_seriallink_options[3] & (BIT_1 | BIT_0);
4027                 tx_sens = ha->fw_seriallink_options[1] &
4028                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
4029                 rx_sens = (ha->fw_seriallink_options[1] &
4030                     (BIT_7 | BIT_6 | BIT_5 | BIT_4)) >> 4;
4031                 ha->fw_options[11] = (emphasis << 14) | (swing << 8);
4032                 if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
4033                         if (rx_sens == 0x0)
4034                                 rx_sens = 0x3;
4035                         ha->fw_options[11] |= (tx_sens << 4) | rx_sens;
4036                 } else if (IS_QLA2322(ha) || IS_QLA6322(ha))
4037                         ha->fw_options[11] |= BIT_5 |
4038                             ((rx_sens & (BIT_1 | BIT_0)) << 2) |
4039                             (tx_sens & (BIT_1 | BIT_0));
4040         }
4041
4042         /* FCP2 options. */
4043         /*  Return command IOCBs without waiting for an ABTS to complete. */
4044         ha->fw_options[3] |= BIT_13;
4045
4046         /* LED scheme. */
4047         if (ha->flags.enable_led_scheme)
4048                 ha->fw_options[2] |= BIT_12;
4049
4050         /* Detect ISP6312. */
4051         if (IS_QLA6312(ha))
4052                 ha->fw_options[2] |= BIT_13;
4053
4054         /* Set Retry FLOGI in case of P2P connection */
4055         if (ha->operating_mode == P2P) {
4056                 ha->fw_options[2] |= BIT_3;
4057                 ql_dbg(ql_dbg_disc, vha, 0x2100,
4058                     "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
4059                         __func__, ha->fw_options[2]);
4060         }
4061
4062         /* Update firmware options. */
4063         qla2x00_set_fw_options(vha, ha->fw_options);
4064 }
4065
4066 void
4067 qla24xx_update_fw_options(scsi_qla_host_t *vha)
4068 {
4069         int rval;
4070         struct qla_hw_data *ha = vha->hw;
4071
4072         if (IS_P3P_TYPE(ha))
4073                 return;
4074
4075         /*  Hold status IOCBs until ABTS response received. */
4076         if (ql2xfwholdabts)
4077                 ha->fw_options[3] |= BIT_12;
4078
4079         /* Set Retry FLOGI in case of P2P connection */
4080         if (ha->operating_mode == P2P) {
4081                 ha->fw_options[2] |= BIT_3;
4082                 ql_dbg(ql_dbg_disc, vha, 0x2101,
4083                     "(%s): Setting FLOGI retry BIT in fw_options[2]: 0x%x\n",
4084                         __func__, ha->fw_options[2]);
4085         }
4086
4087         /* Move PUREX, ABTS RX & RIDA to ATIOQ */
4088         if (ql2xmvasynctoatio &&
4089             (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha))) {
4090                 if (qla_tgt_mode_enabled(vha) ||
4091                     qla_dual_mode_enabled(vha))
4092                         ha->fw_options[2] |= BIT_11;
4093                 else
4094                         ha->fw_options[2] &= ~BIT_11;
4095         }
4096
4097         if (IS_QLA25XX(ha) || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
4098             IS_QLA28XX(ha)) {
4099                 /*
4100                  * Tell FW to track each exchange to prevent
4101                  * driver from using stale exchange.
4102                  */
4103                 if (qla_tgt_mode_enabled(vha) ||
4104                     qla_dual_mode_enabled(vha))
4105                         ha->fw_options[2] |= BIT_4;
4106                 else
4107                         ha->fw_options[2] &= ~BIT_4;
4108
4109                 /* Reserve 1/2 of emergency exchanges for ELS.*/
4110                 if (qla2xuseresexchforels)
4111                         ha->fw_options[2] |= BIT_8;
4112                 else
4113                         ha->fw_options[2] &= ~BIT_8;
4114         }
4115
4116         if (ql2xrdpenable || ha->flags.scm_supported_f)
4117                 ha->fw_options[1] |= ADD_FO1_ENABLE_PUREX_IOCB;
4118
4119         /* Enable Async 8130/8131 events -- transceiver insertion/removal */
4120         if (IS_BPM_RANGE_CAPABLE(ha))
4121                 ha->fw_options[3] |= BIT_10;
4122
4123         ql_dbg(ql_dbg_init, vha, 0x00e8,
4124             "%s, add FW options 1-3 = 0x%04x 0x%04x 0x%04x mode %x\n",
4125             __func__, ha->fw_options[1], ha->fw_options[2],
4126             ha->fw_options[3], vha->host->active_mode);
4127
4128         if (ha->fw_options[1] || ha->fw_options[2] || ha->fw_options[3])
4129                 qla2x00_set_fw_options(vha, ha->fw_options);
4130
4131         /* Update Serial Link options. */
4132         if ((le16_to_cpu(ha->fw_seriallink_options24[0]) & BIT_0) == 0)
4133                 return;
4134
4135         rval = qla2x00_set_serdes_params(vha,
4136             le16_to_cpu(ha->fw_seriallink_options24[1]),
4137             le16_to_cpu(ha->fw_seriallink_options24[2]),
4138             le16_to_cpu(ha->fw_seriallink_options24[3]));
4139         if (rval != QLA_SUCCESS) {
4140                 ql_log(ql_log_warn, vha, 0x0104,
4141                     "Unable to update Serial Link options (%x).\n", rval);
4142         }
4143 }
4144
4145 void
4146 qla2x00_config_rings(struct scsi_qla_host *vha)
4147 {
4148         struct qla_hw_data *ha = vha->hw;
4149         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
4150         struct req_que *req = ha->req_q_map[0];
4151         struct rsp_que *rsp = ha->rsp_q_map[0];
4152
4153         /* Setup ring parameters in initialization control block. */
4154         ha->init_cb->request_q_outpointer = cpu_to_le16(0);
4155         ha->init_cb->response_q_inpointer = cpu_to_le16(0);
4156         ha->init_cb->request_q_length = cpu_to_le16(req->length);
4157         ha->init_cb->response_q_length = cpu_to_le16(rsp->length);
4158         put_unaligned_le64(req->dma, &ha->init_cb->request_q_address);
4159         put_unaligned_le64(rsp->dma, &ha->init_cb->response_q_address);
4160
4161         wrt_reg_word(ISP_REQ_Q_IN(ha, reg), 0);
4162         wrt_reg_word(ISP_REQ_Q_OUT(ha, reg), 0);
4163         wrt_reg_word(ISP_RSP_Q_IN(ha, reg), 0);
4164         wrt_reg_word(ISP_RSP_Q_OUT(ha, reg), 0);
4165         rd_reg_word(ISP_RSP_Q_OUT(ha, reg));            /* PCI Posting. */
4166 }
4167
4168 void
4169 qla24xx_config_rings(struct scsi_qla_host *vha)
4170 {
4171         struct qla_hw_data *ha = vha->hw;
4172         device_reg_t *reg = ISP_QUE_REG(ha, 0);
4173         struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
4174         struct qla_msix_entry *msix;
4175         struct init_cb_24xx *icb;
4176         uint16_t rid = 0;
4177         struct req_que *req = ha->req_q_map[0];
4178         struct rsp_que *rsp = ha->rsp_q_map[0];
4179
4180         /* Setup ring parameters in initialization control block. */
4181         icb = (struct init_cb_24xx *)ha->init_cb;
4182         icb->request_q_outpointer = cpu_to_le16(0);
4183         icb->response_q_inpointer = cpu_to_le16(0);
4184         icb->request_q_length = cpu_to_le16(req->length);
4185         icb->response_q_length = cpu_to_le16(rsp->length);
4186         put_unaligned_le64(req->dma, &icb->request_q_address);
4187         put_unaligned_le64(rsp->dma, &icb->response_q_address);
4188
4189         /* Setup ATIO queue dma pointers for target mode */
4190         icb->atio_q_inpointer = cpu_to_le16(0);
4191         icb->atio_q_length = cpu_to_le16(ha->tgt.atio_q_length);
4192         put_unaligned_le64(ha->tgt.atio_dma, &icb->atio_q_address);
4193
4194         if (IS_SHADOW_REG_CAPABLE(ha))
4195                 icb->firmware_options_2 |= cpu_to_le32(BIT_30|BIT_29);
4196
4197         if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
4198             IS_QLA28XX(ha)) {
4199                 icb->qos = cpu_to_le16(QLA_DEFAULT_QUE_QOS);
4200                 icb->rid = cpu_to_le16(rid);
4201                 if (ha->flags.msix_enabled) {
4202                         msix = &ha->msix_entries[1];
4203                         ql_dbg(ql_dbg_init, vha, 0x0019,
4204                             "Registering vector 0x%x for base que.\n",
4205                             msix->entry);
4206                         icb->msix = cpu_to_le16(msix->entry);
4207                 }
4208                 /* Use alternate PCI bus number */
4209                 if (MSB(rid))
4210                         icb->firmware_options_2 |= cpu_to_le32(BIT_19);
4211                 /* Use alternate PCI devfn */
4212                 if (LSB(rid))
4213                         icb->firmware_options_2 |= cpu_to_le32(BIT_18);
4214
4215                 /* Use Disable MSIX Handshake mode for capable adapters */
4216                 if ((ha->fw_attributes & BIT_6) && (IS_MSIX_NACK_CAPABLE(ha)) &&
4217                     (ha->flags.msix_enabled)) {
4218                         icb->firmware_options_2 &= cpu_to_le32(~BIT_22);
4219                         ha->flags.disable_msix_handshake = 1;
4220                         ql_dbg(ql_dbg_init, vha, 0x00fe,
4221                             "MSIX Handshake Disable Mode turned on.\n");
4222                 } else {
4223                         icb->firmware_options_2 |= cpu_to_le32(BIT_22);
4224                 }
4225                 icb->firmware_options_2 |= cpu_to_le32(BIT_23);
4226
4227                 wrt_reg_dword(&reg->isp25mq.req_q_in, 0);
4228                 wrt_reg_dword(&reg->isp25mq.req_q_out, 0);
4229                 wrt_reg_dword(&reg->isp25mq.rsp_q_in, 0);
4230                 wrt_reg_dword(&reg->isp25mq.rsp_q_out, 0);
4231         } else {
4232                 wrt_reg_dword(&reg->isp24.req_q_in, 0);
4233                 wrt_reg_dword(&reg->isp24.req_q_out, 0);
4234                 wrt_reg_dword(&reg->isp24.rsp_q_in, 0);
4235                 wrt_reg_dword(&reg->isp24.rsp_q_out, 0);
4236         }
4237
4238         qlt_24xx_config_rings(vha);
4239
4240         /* If the user has configured the speed, set it here */
4241         if (ha->set_data_rate) {
4242                 ql_dbg(ql_dbg_init, vha, 0x00fd,
4243                     "Speed set by user : %s Gbps \n",
4244                     qla2x00_get_link_speed_str(ha, ha->set_data_rate));
4245                 icb->firmware_options_3 = cpu_to_le32(ha->set_data_rate << 13);
4246         }
4247
4248         /* PCI posting */
4249         rd_reg_word(&ioreg->hccr);
4250 }
4251
4252 /**
4253  * qla2x00_init_rings() - Initializes firmware.
4254  * @vha: HA context
4255  *
4256  * Beginning of request ring has initialization control block already built
4257  * by nvram config routine.
4258  *
4259  * Returns 0 on success.
4260  */
4261 int
4262 qla2x00_init_rings(scsi_qla_host_t *vha)
4263 {
4264         int     rval;
4265         unsigned long flags = 0;
4266         int cnt, que;
4267         struct qla_hw_data *ha = vha->hw;
4268         struct req_que *req;
4269         struct rsp_que *rsp;
4270         struct mid_init_cb_24xx *mid_init_cb =
4271             (struct mid_init_cb_24xx *) ha->init_cb;
4272
4273         spin_lock_irqsave(&ha->hardware_lock, flags);
4274
4275         /* Clear outstanding commands array. */
4276         for (que = 0; que < ha->max_req_queues; que++) {
4277                 req = ha->req_q_map[que];
4278                 if (!req || !test_bit(que, ha->req_qid_map))
4279                         continue;
4280                 req->out_ptr = (uint16_t *)(req->ring + req->length);
4281                 *req->out_ptr = 0;
4282                 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++)
4283                         req->outstanding_cmds[cnt] = NULL;
4284
4285                 req->current_outstanding_cmd = 1;
4286
4287                 /* Initialize firmware. */
4288                 req->ring_ptr  = req->ring;
4289                 req->ring_index    = 0;
4290                 req->cnt      = req->length;
4291         }
4292
4293         for (que = 0; que < ha->max_rsp_queues; que++) {
4294                 rsp = ha->rsp_q_map[que];
4295                 if (!rsp || !test_bit(que, ha->rsp_qid_map))
4296                         continue;
4297                 rsp->in_ptr = (uint16_t *)(rsp->ring + rsp->length);
4298                 *rsp->in_ptr = 0;
4299                 /* Initialize response queue entries */
4300                 if (IS_QLAFX00(ha))
4301                         qlafx00_init_response_q_entries(rsp);
4302                 else
4303                         qla2x00_init_response_q_entries(rsp);
4304         }
4305
4306         ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
4307         ha->tgt.atio_ring_index = 0;
4308         /* Initialize ATIO queue entries */
4309         qlt_init_atio_q_entries(vha);
4310
4311         ha->isp_ops->config_rings(vha);
4312
4313         spin_unlock_irqrestore(&ha->hardware_lock, flags);
4314
4315         ql_dbg(ql_dbg_init, vha, 0x00d1, "Issue init firmware.\n");
4316
4317         if (IS_QLAFX00(ha)) {
4318                 rval = qlafx00_init_firmware(vha, ha->init_cb_size);
4319                 goto next_check;
4320         }
4321
4322         /* Update any ISP specific firmware options before initialization. */
4323         ha->isp_ops->update_fw_options(vha);
4324
4325         if (ha->flags.npiv_supported) {
4326                 if (ha->operating_mode == LOOP && !IS_CNA_CAPABLE(ha))
4327                         ha->max_npiv_vports = MIN_MULTI_ID_FABRIC - 1;
4328                 mid_init_cb->count = cpu_to_le16(ha->max_npiv_vports);
4329         }
4330
4331         if (IS_FWI2_CAPABLE(ha)) {
4332                 mid_init_cb->options = cpu_to_le16(BIT_1);
4333                 mid_init_cb->init_cb.execution_throttle =
4334                     cpu_to_le16(ha->cur_fw_xcb_count);
4335                 ha->flags.dport_enabled =
4336                         (le32_to_cpu(mid_init_cb->init_cb.firmware_options_1) &
4337                          BIT_7) != 0;
4338                 ql_dbg(ql_dbg_init, vha, 0x0191, "DPORT Support: %s.\n",
4339                     (ha->flags.dport_enabled) ? "enabled" : "disabled");
4340                 /* FA-WWPN Status */
4341                 ha->flags.fawwpn_enabled =
4342                         (le32_to_cpu(mid_init_cb->init_cb.firmware_options_1) &
4343                          BIT_6) != 0;
4344                 ql_dbg(ql_dbg_init, vha, 0x00bc, "FA-WWPN Support: %s.\n",
4345                     (ha->flags.fawwpn_enabled) ? "enabled" : "disabled");
4346                 /* Init_cb will be reused for other command(s).  Save a backup copy of port_name */
4347                 memcpy(ha->port_name, ha->init_cb->port_name, WWN_SIZE);
4348         }
4349
4350         QLA_FW_STARTED(ha);
4351         rval = qla2x00_init_firmware(vha, ha->init_cb_size);
4352 next_check:
4353         if (rval) {
4354                 QLA_FW_STOPPED(ha);
4355                 ql_log(ql_log_fatal, vha, 0x00d2,
4356                     "Init Firmware **** FAILED ****.\n");
4357         } else {
4358                 ql_dbg(ql_dbg_init, vha, 0x00d3,
4359                     "Init Firmware -- success.\n");
4360                 vha->u_ql2xexchoffld = vha->u_ql2xiniexchg = 0;
4361         }
4362
4363         return (rval);
4364 }
4365
4366 /**
4367  * qla2x00_fw_ready() - Waits for firmware ready.
4368  * @vha: HA context
4369  *
4370  * Returns 0 on success.
4371  */
4372 static int
4373 qla2x00_fw_ready(scsi_qla_host_t *vha)
4374 {
4375         int             rval;
4376         unsigned long   wtime, mtime, cs84xx_time;
4377         uint16_t        min_wait;       /* Minimum wait time if loop is down */
4378         uint16_t        wait_time;      /* Wait time if loop is coming ready */
4379         uint16_t        state[6];
4380         struct qla_hw_data *ha = vha->hw;
4381
4382         if (IS_QLAFX00(vha->hw))
4383                 return qlafx00_fw_ready(vha);
4384
4385         rval = QLA_SUCCESS;
4386
4387         /* Time to wait for loop down */
4388         if (IS_P3P_TYPE(ha))
4389                 min_wait = 30;
4390         else
4391                 min_wait = 20;
4392
4393         /*
4394          * Firmware should take at most one RATOV to login, plus 5 seconds for
4395          * our own processing.
4396          */
4397         if ((wait_time = (ha->retry_count*ha->login_timeout) + 5) < min_wait) {
4398                 wait_time = min_wait;
4399         }
4400
4401         /* Min wait time if loop down */
4402         mtime = jiffies + (min_wait * HZ);
4403
4404         /* wait time before firmware ready */
4405         wtime = jiffies + (wait_time * HZ);
4406
4407         /* Wait for ISP to finish LIP */
4408         if (!vha->flags.init_done)
4409                 ql_log(ql_log_info, vha, 0x801e,
4410                     "Waiting for LIP to complete.\n");
4411
4412         do {
4413                 memset(state, -1, sizeof(state));
4414                 rval = qla2x00_get_firmware_state(vha, state);
4415                 if (rval == QLA_SUCCESS) {
4416                         if (state[0] < FSTATE_LOSS_OF_SYNC) {
4417                                 vha->device_flags &= ~DFLG_NO_CABLE;
4418                         }
4419                         if (IS_QLA84XX(ha) && state[0] != FSTATE_READY) {
4420                                 ql_dbg(ql_dbg_taskm, vha, 0x801f,
4421                                     "fw_state=%x 84xx=%x.\n", state[0],
4422                                     state[2]);
4423                                 if ((state[2] & FSTATE_LOGGED_IN) &&
4424                                      (state[2] & FSTATE_WAITING_FOR_VERIFY)) {
4425                                         ql_dbg(ql_dbg_taskm, vha, 0x8028,
4426                                             "Sending verify iocb.\n");
4427
4428                                         cs84xx_time = jiffies;
4429                                         rval = qla84xx_init_chip(vha);
4430                                         if (rval != QLA_SUCCESS) {
4431                                                 ql_log(ql_log_warn,
4432                                                     vha, 0x8007,
4433                                                     "Init chip failed.\n");
4434                                                 break;
4435                                         }
4436
4437                                         /* Add time taken to initialize. */
4438                                         cs84xx_time = jiffies - cs84xx_time;
4439                                         wtime += cs84xx_time;
4440                                         mtime += cs84xx_time;
4441                                         ql_dbg(ql_dbg_taskm, vha, 0x8008,
4442                                             "Increasing wait time by %ld. "
4443                                             "New time %ld.\n", cs84xx_time,
4444                                             wtime);
4445                                 }
4446                         } else if (state[0] == FSTATE_READY) {
4447                                 ql_dbg(ql_dbg_taskm, vha, 0x8037,
4448                                     "F/W Ready - OK.\n");
4449
4450                                 qla2x00_get_retry_cnt(vha, &ha->retry_count,
4451                                     &ha->login_timeout, &ha->r_a_tov);
4452
4453                                 rval = QLA_SUCCESS;
4454                                 break;
4455                         }
4456
4457                         rval = QLA_FUNCTION_FAILED;
4458
4459                         if (atomic_read(&vha->loop_down_timer) &&
4460                             state[0] != FSTATE_READY) {
4461                                 /* Loop down. Timeout on min_wait for states
4462                                  * other than Wait for Login.
4463                                  */
4464                                 if (time_after_eq(jiffies, mtime)) {
4465                                         ql_log(ql_log_info, vha, 0x8038,
4466                                             "Cable is unplugged...\n");
4467
4468                                         vha->device_flags |= DFLG_NO_CABLE;
4469                                         break;
4470                                 }
4471                         }
4472                 } else {
4473                         /* Mailbox cmd failed. Timeout on min_wait. */
4474                         if (time_after_eq(jiffies, mtime) ||
4475                                 ha->flags.isp82xx_fw_hung)
4476                                 break;
4477                 }
4478
4479                 if (time_after_eq(jiffies, wtime))
4480                         break;
4481
4482                 /* Delay for a while */
4483                 msleep(500);
4484         } while (1);
4485
4486         ql_dbg(ql_dbg_taskm, vha, 0x803a,
4487             "fw_state=%x (%x, %x, %x, %x %x) curr time=%lx.\n", state[0],
4488             state[1], state[2], state[3], state[4], state[5], jiffies);
4489
4490         if (rval && !(vha->device_flags & DFLG_NO_CABLE)) {
4491                 ql_log(ql_log_warn, vha, 0x803b,
4492                     "Firmware ready **** FAILED ****.\n");
4493         }
4494
4495         return (rval);
4496 }
4497
4498 /*
4499 *  qla2x00_configure_hba
4500 *      Setup adapter context.
4501 *
4502 * Input:
4503 *      ha = adapter state pointer.
4504 *
4505 * Returns:
4506 *      0 = success
4507 *
4508 * Context:
4509 *      Kernel context.
4510 */
4511 static int
4512 qla2x00_configure_hba(scsi_qla_host_t *vha)
4513 {
4514         int       rval;
4515         uint16_t      loop_id;
4516         uint16_t      topo;
4517         uint16_t      sw_cap;
4518         uint8_t       al_pa;
4519         uint8_t       area;
4520         uint8_t       domain;
4521         char            connect_type[22];
4522         struct qla_hw_data *ha = vha->hw;
4523         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
4524         port_id_t id;
4525         unsigned long flags;
4526
4527         /* Get host addresses. */
4528         rval = qla2x00_get_adapter_id(vha,
4529             &loop_id, &al_pa, &area, &domain, &topo, &sw_cap);
4530         if (rval != QLA_SUCCESS) {
4531                 if (LOOP_TRANSITION(vha) || atomic_read(&ha->loop_down_timer) ||
4532                     IS_CNA_CAPABLE(ha) ||
4533                     (rval == QLA_COMMAND_ERROR && loop_id == 0x7)) {
4534                         ql_dbg(ql_dbg_disc, vha, 0x2008,
4535                             "Loop is in a transition state.\n");
4536                 } else {
4537                         ql_log(ql_log_warn, vha, 0x2009,
4538                             "Unable to get host loop ID.\n");
4539                         if (IS_FWI2_CAPABLE(ha) && (vha == base_vha) &&
4540                             (rval == QLA_COMMAND_ERROR && loop_id == 0x1b)) {
4541                                 ql_log(ql_log_warn, vha, 0x1151,
4542                                     "Doing link init.\n");
4543                                 if (qla24xx_link_initialize(vha) == QLA_SUCCESS)
4544                                         return rval;
4545                         }
4546                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
4547                 }
4548                 return (rval);
4549         }
4550
4551         if (topo == 4) {
4552                 ql_log(ql_log_info, vha, 0x200a,
4553                     "Cannot get topology - retrying.\n");
4554                 return (QLA_FUNCTION_FAILED);
4555         }
4556
4557         vha->loop_id = loop_id;
4558
4559         /* initialize */
4560         ha->min_external_loopid = SNS_FIRST_LOOP_ID;
4561         ha->operating_mode = LOOP;
4562         ha->switch_cap = 0;
4563
4564         switch (topo) {
4565         case 0:
4566                 ql_dbg(ql_dbg_disc, vha, 0x200b, "HBA in NL topology.\n");
4567                 ha->current_topology = ISP_CFG_NL;
4568                 strcpy(connect_type, "(Loop)");
4569                 break;
4570
4571         case 1:
4572                 ql_dbg(ql_dbg_disc, vha, 0x200c, "HBA in FL topology.\n");
4573                 ha->switch_cap = sw_cap;
4574                 ha->current_topology = ISP_CFG_FL;
4575                 strcpy(connect_type, "(FL_Port)");
4576                 break;
4577
4578         case 2:
4579                 ql_dbg(ql_dbg_disc, vha, 0x200d, "HBA in N P2P topology.\n");
4580                 ha->operating_mode = P2P;
4581                 ha->current_topology = ISP_CFG_N;
4582                 strcpy(connect_type, "(N_Port-to-N_Port)");
4583                 break;
4584
4585         case 3:
4586                 ql_dbg(ql_dbg_disc, vha, 0x200e, "HBA in F P2P topology.\n");
4587                 ha->switch_cap = sw_cap;
4588                 ha->operating_mode = P2P;
4589                 ha->current_topology = ISP_CFG_F;
4590                 strcpy(connect_type, "(F_Port)");
4591                 break;
4592
4593         default:
4594                 ql_dbg(ql_dbg_disc, vha, 0x200f,
4595                     "HBA in unknown topology %x, using NL.\n", topo);
4596                 ha->current_topology = ISP_CFG_NL;
4597                 strcpy(connect_type, "(Loop)");
4598                 break;
4599         }
4600
4601         /* Save Host port and loop ID. */
4602         /* byte order - Big Endian */
4603         id.b.domain = domain;
4604         id.b.area = area;
4605         id.b.al_pa = al_pa;
4606         id.b.rsvd_1 = 0;
4607         spin_lock_irqsave(&ha->hardware_lock, flags);
4608         if (!(topo == 2 && ha->flags.n2n_bigger))
4609                 qlt_update_host_map(vha, id);
4610         spin_unlock_irqrestore(&ha->hardware_lock, flags);
4611
4612         if (!vha->flags.init_done)
4613                 ql_log(ql_log_info, vha, 0x2010,
4614                     "Topology - %s, Host Loop address 0x%x.\n",
4615                     connect_type, vha->loop_id);
4616
4617         return(rval);
4618 }
4619
4620 inline void
4621 qla2x00_set_model_info(scsi_qla_host_t *vha, uint8_t *model, size_t len,
4622                        const char *def)
4623 {
4624         char *st, *en;
4625         uint16_t index;
4626         uint64_t zero[2] = { 0 };
4627         struct qla_hw_data *ha = vha->hw;
4628         int use_tbl = !IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
4629             !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha);
4630
4631         if (len > sizeof(zero))
4632                 len = sizeof(zero);
4633         if (memcmp(model, &zero, len) != 0) {
4634                 memcpy(ha->model_number, model, len);
4635                 st = en = ha->model_number;
4636                 en += len - 1;
4637                 while (en > st) {
4638                         if (*en != 0x20 && *en != 0x00)
4639                                 break;
4640                         *en-- = '\0';
4641                 }
4642
4643                 index = (ha->pdev->subsystem_device & 0xff);
4644                 if (use_tbl &&
4645                     ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
4646                     index < QLA_MODEL_NAMES)
4647                         strlcpy(ha->model_desc,
4648                             qla2x00_model_name[index * 2 + 1],
4649                             sizeof(ha->model_desc));
4650         } else {
4651                 index = (ha->pdev->subsystem_device & 0xff);
4652                 if (use_tbl &&
4653                     ha->pdev->subsystem_vendor == PCI_VENDOR_ID_QLOGIC &&
4654                     index < QLA_MODEL_NAMES) {
4655                         strlcpy(ha->model_number,
4656                                 qla2x00_model_name[index * 2],
4657                                 sizeof(ha->model_number));
4658                         strlcpy(ha->model_desc,
4659                             qla2x00_model_name[index * 2 + 1],
4660                             sizeof(ha->model_desc));
4661                 } else {
4662                         strlcpy(ha->model_number, def,
4663                                 sizeof(ha->model_number));
4664                 }
4665         }
4666         if (IS_FWI2_CAPABLE(ha))
4667                 qla2xxx_get_vpd_field(vha, "\x82", ha->model_desc,
4668                     sizeof(ha->model_desc));
4669 }
4670
4671 /* On sparc systems, obtain port and node WWN from firmware
4672  * properties.
4673  */
4674 static void qla2xxx_nvram_wwn_from_ofw(scsi_qla_host_t *vha, nvram_t *nv)
4675 {
4676 #ifdef CONFIG_SPARC
4677         struct qla_hw_data *ha = vha->hw;
4678         struct pci_dev *pdev = ha->pdev;
4679         struct device_node *dp = pci_device_to_OF_node(pdev);
4680         const u8 *val;
4681         int len;
4682
4683         val = of_get_property(dp, "port-wwn", &len);
4684         if (val && len >= WWN_SIZE)
4685                 memcpy(nv->port_name, val, WWN_SIZE);
4686
4687         val = of_get_property(dp, "node-wwn", &len);
4688         if (val && len >= WWN_SIZE)
4689                 memcpy(nv->node_name, val, WWN_SIZE);
4690 #endif
4691 }
4692
4693 /*
4694 * NVRAM configuration for ISP 2xxx
4695 *
4696 * Input:
4697 *      ha                = adapter block pointer.
4698 *
4699 * Output:
4700 *      initialization control block in response_ring
4701 *      host adapters parameters in host adapter block
4702 *
4703 * Returns:
4704 *      0 = success.
4705 */
4706 int
4707 qla2x00_nvram_config(scsi_qla_host_t *vha)
4708 {
4709         int             rval;
4710         uint8_t         chksum = 0;
4711         uint16_t        cnt;
4712         uint8_t         *dptr1, *dptr2;
4713         struct qla_hw_data *ha = vha->hw;
4714         init_cb_t       *icb = ha->init_cb;
4715         nvram_t         *nv = ha->nvram;
4716         uint8_t         *ptr = ha->nvram;
4717         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
4718
4719         rval = QLA_SUCCESS;
4720
4721         /* Determine NVRAM starting address. */
4722         ha->nvram_size = sizeof(*nv);
4723         ha->nvram_base = 0;
4724         if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha))
4725                 if ((rd_reg_word(&reg->ctrl_status) >> 14) == 1)
4726                         ha->nvram_base = 0x80;
4727
4728         /* Get NVRAM data and calculate checksum. */
4729         ha->isp_ops->read_nvram(vha, ptr, ha->nvram_base, ha->nvram_size);
4730         for (cnt = 0, chksum = 0; cnt < ha->nvram_size; cnt++)
4731                 chksum += *ptr++;
4732
4733         ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x010f,
4734             "Contents of NVRAM.\n");
4735         ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0110,
4736             nv, ha->nvram_size);
4737
4738         /* Bad NVRAM data, set defaults parameters. */
4739         if (chksum || memcmp("ISP ", nv->id, sizeof(nv->id)) ||
4740             nv->nvram_version < 1) {
4741                 /* Reset NVRAM data. */
4742                 ql_log(ql_log_warn, vha, 0x0064,
4743                     "Inconsistent NVRAM detected: checksum=%#x id=%.4s version=%#x.\n",
4744                     chksum, nv->id, nv->nvram_version);
4745                 ql_log(ql_log_warn, vha, 0x0065,
4746                     "Falling back to "
4747                     "functioning (yet invalid -- WWPN) defaults.\n");
4748
4749                 /*
4750                  * Set default initialization control block.
4751                  */
4752                 memset(nv, 0, ha->nvram_size);
4753                 nv->parameter_block_version = ICB_VERSION;
4754
4755                 if (IS_QLA23XX(ha)) {
4756                         nv->firmware_options[0] = BIT_2 | BIT_1;
4757                         nv->firmware_options[1] = BIT_7 | BIT_5;
4758                         nv->add_firmware_options[0] = BIT_5;
4759                         nv->add_firmware_options[1] = BIT_5 | BIT_4;
4760                         nv->frame_payload_size = cpu_to_le16(2048);
4761                         nv->special_options[1] = BIT_7;
4762                 } else if (IS_QLA2200(ha)) {
4763                         nv->firmware_options[0] = BIT_2 | BIT_1;
4764                         nv->firmware_options[1] = BIT_7 | BIT_5;
4765                         nv->add_firmware_options[0] = BIT_5;
4766                         nv->add_firmware_options[1] = BIT_5 | BIT_4;
4767                         nv->frame_payload_size = cpu_to_le16(1024);
4768                 } else if (IS_QLA2100(ha)) {
4769                         nv->firmware_options[0] = BIT_3 | BIT_1;
4770                         nv->firmware_options[1] = BIT_5;
4771                         nv->frame_payload_size = cpu_to_le16(1024);
4772                 }
4773
4774                 nv->max_iocb_allocation = cpu_to_le16(256);
4775                 nv->execution_throttle = cpu_to_le16(16);
4776                 nv->retry_count = 8;
4777                 nv->retry_delay = 1;
4778
4779                 nv->port_name[0] = 33;
4780                 nv->port_name[3] = 224;
4781                 nv->port_name[4] = 139;
4782
4783                 qla2xxx_nvram_wwn_from_ofw(vha, nv);
4784
4785                 nv->login_timeout = 4;
4786
4787                 /*
4788                  * Set default host adapter parameters
4789                  */
4790                 nv->host_p[1] = BIT_2;
4791                 nv->reset_delay = 5;
4792                 nv->port_down_retry_count = 8;
4793                 nv->max_luns_per_target = cpu_to_le16(8);
4794                 nv->link_down_timeout = 60;
4795
4796                 rval = 1;
4797         }
4798
4799         /* Reset Initialization control block */
4800         memset(icb, 0, ha->init_cb_size);
4801
4802         /*
4803          * Setup driver NVRAM options.
4804          */
4805         nv->firmware_options[0] |= (BIT_6 | BIT_1);
4806         nv->firmware_options[0] &= ~(BIT_5 | BIT_4);
4807         nv->firmware_options[1] |= (BIT_5 | BIT_0);
4808         nv->firmware_options[1] &= ~BIT_4;
4809
4810         if (IS_QLA23XX(ha)) {
4811                 nv->firmware_options[0] |= BIT_2;
4812                 nv->firmware_options[0] &= ~BIT_3;
4813                 nv->special_options[0] &= ~BIT_6;
4814                 nv->add_firmware_options[1] |= BIT_5 | BIT_4;
4815
4816                 if (IS_QLA2300(ha)) {
4817                         if (ha->fb_rev == FPM_2310) {
4818                                 strcpy(ha->model_number, "QLA2310");
4819                         } else {
4820                                 strcpy(ha->model_number, "QLA2300");
4821                         }
4822                 } else {
4823                         qla2x00_set_model_info(vha, nv->model_number,
4824                             sizeof(nv->model_number), "QLA23xx");
4825                 }
4826         } else if (IS_QLA2200(ha)) {
4827                 nv->firmware_options[0] |= BIT_2;
4828                 /*
4829                  * 'Point-to-point preferred, else loop' is not a safe
4830                  * connection mode setting.
4831                  */
4832                 if ((nv->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) ==
4833                     (BIT_5 | BIT_4)) {
4834                         /* Force 'loop preferred, else point-to-point'. */
4835                         nv->add_firmware_options[0] &= ~(BIT_6 | BIT_5 | BIT_4);
4836                         nv->add_firmware_options[0] |= BIT_5;
4837                 }
4838                 strcpy(ha->model_number, "QLA22xx");
4839         } else /*if (IS_QLA2100(ha))*/ {
4840                 strcpy(ha->model_number, "QLA2100");
4841         }
4842
4843         /*
4844          * Copy over NVRAM RISC parameter block to initialization control block.
4845          */
4846         dptr1 = (uint8_t *)icb;
4847         dptr2 = (uint8_t *)&nv->parameter_block_version;
4848         cnt = (uint8_t *)&icb->request_q_outpointer - (uint8_t *)&icb->version;
4849         while (cnt--)
4850                 *dptr1++ = *dptr2++;
4851
4852         /* Copy 2nd half. */
4853         dptr1 = (uint8_t *)icb->add_firmware_options;
4854         cnt = (uint8_t *)icb->reserved_3 - (uint8_t *)icb->add_firmware_options;
4855         while (cnt--)
4856                 *dptr1++ = *dptr2++;
4857         ha->frame_payload_size = le16_to_cpu(icb->frame_payload_size);
4858         /* Use alternate WWN? */
4859         if (nv->host_p[1] & BIT_7) {
4860                 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
4861                 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
4862         }
4863
4864         /* Prepare nodename */
4865         if ((icb->firmware_options[1] & BIT_6) == 0) {
4866                 /*
4867                  * Firmware will apply the following mask if the nodename was
4868                  * not provided.
4869                  */
4870                 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
4871                 icb->node_name[0] &= 0xF0;
4872         }
4873
4874         /*
4875          * Set host adapter parameters.
4876          */
4877
4878         /*
4879          * BIT_7 in the host-parameters section allows for modification to
4880          * internal driver logging.
4881          */
4882         if (nv->host_p[0] & BIT_7)
4883                 ql2xextended_error_logging = QL_DBG_DEFAULT1_MASK;
4884         ha->flags.disable_risc_code_load = ((nv->host_p[0] & BIT_4) ? 1 : 0);
4885         /* Always load RISC code on non ISP2[12]00 chips. */
4886         if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
4887                 ha->flags.disable_risc_code_load = 0;
4888         ha->flags.enable_lip_reset = ((nv->host_p[1] & BIT_1) ? 1 : 0);
4889         ha->flags.enable_lip_full_login = ((nv->host_p[1] & BIT_2) ? 1 : 0);
4890         ha->flags.enable_target_reset = ((nv->host_p[1] & BIT_3) ? 1 : 0);
4891         ha->flags.enable_led_scheme = (nv->special_options[1] & BIT_4) ? 1 : 0;
4892         ha->flags.disable_serdes = 0;
4893
4894         ha->operating_mode =
4895             (icb->add_firmware_options[0] & (BIT_6 | BIT_5 | BIT_4)) >> 4;
4896
4897         memcpy(ha->fw_seriallink_options, nv->seriallink_options,
4898             sizeof(ha->fw_seriallink_options));
4899
4900         /* save HBA serial number */
4901         ha->serial0 = icb->port_name[5];
4902         ha->serial1 = icb->port_name[6];
4903         ha->serial2 = icb->port_name[7];
4904         memcpy(vha->node_name, icb->node_name, WWN_SIZE);
4905         memcpy(vha->port_name, icb->port_name, WWN_SIZE);
4906
4907         icb->execution_throttle = cpu_to_le16(0xFFFF);
4908
4909         ha->retry_count = nv->retry_count;
4910
4911         /* Set minimum login_timeout to 4 seconds. */
4912         if (nv->login_timeout != ql2xlogintimeout)
4913                 nv->login_timeout = ql2xlogintimeout;
4914         if (nv->login_timeout < 4)
4915                 nv->login_timeout = 4;
4916         ha->login_timeout = nv->login_timeout;
4917
4918         /* Set minimum RATOV to 100 tenths of a second. */
4919         ha->r_a_tov = 100;
4920
4921         ha->loop_reset_delay = nv->reset_delay;
4922
4923         /* Link Down Timeout = 0:
4924          *
4925          *      When Port Down timer expires we will start returning
4926          *      I/O's to OS with "DID_NO_CONNECT".
4927          *
4928          * Link Down Timeout != 0:
4929          *
4930          *       The driver waits for the link to come up after link down
4931          *       before returning I/Os to OS with "DID_NO_CONNECT".
4932          */
4933         if (nv->link_down_timeout == 0) {
4934                 ha->loop_down_abort_time =
4935                     (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
4936         } else {
4937                 ha->link_down_timeout =  nv->link_down_timeout;
4938                 ha->loop_down_abort_time =
4939                     (LOOP_DOWN_TIME - ha->link_down_timeout);
4940         }
4941
4942         /*
4943          * Need enough time to try and get the port back.
4944          */
4945         ha->port_down_retry_count = nv->port_down_retry_count;
4946         if (qlport_down_retry)
4947                 ha->port_down_retry_count = qlport_down_retry;
4948         /* Set login_retry_count */
4949         ha->login_retry_count  = nv->retry_count;
4950         if (ha->port_down_retry_count == nv->port_down_retry_count &&
4951             ha->port_down_retry_count > 3)
4952                 ha->login_retry_count = ha->port_down_retry_count;
4953         else if (ha->port_down_retry_count > (int)ha->login_retry_count)
4954                 ha->login_retry_count = ha->port_down_retry_count;
4955         if (ql2xloginretrycount)
4956                 ha->login_retry_count = ql2xloginretrycount;
4957
4958         icb->lun_enables = cpu_to_le16(0);
4959         icb->command_resource_count = 0;
4960         icb->immediate_notify_resource_count = 0;
4961         icb->timeout = cpu_to_le16(0);
4962
4963         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
4964                 /* Enable RIO */
4965                 icb->firmware_options[0] &= ~BIT_3;
4966                 icb->add_firmware_options[0] &=
4967                     ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
4968                 icb->add_firmware_options[0] |= BIT_2;
4969                 icb->response_accumulation_timer = 3;
4970                 icb->interrupt_delay_timer = 5;
4971
4972                 vha->flags.process_response_queue = 1;
4973         } else {
4974                 /* Enable ZIO. */
4975                 if (!vha->flags.init_done) {
4976                         ha->zio_mode = icb->add_firmware_options[0] &
4977                             (BIT_3 | BIT_2 | BIT_1 | BIT_0);
4978                         ha->zio_timer = icb->interrupt_delay_timer ?
4979                             icb->interrupt_delay_timer : 2;
4980                 }
4981                 icb->add_firmware_options[0] &=
4982                     ~(BIT_3 | BIT_2 | BIT_1 | BIT_0);
4983                 vha->flags.process_response_queue = 0;
4984                 if (ha->zio_mode != QLA_ZIO_DISABLED) {
4985                         ha->zio_mode = QLA_ZIO_MODE_6;
4986
4987                         ql_log(ql_log_info, vha, 0x0068,
4988                             "ZIO mode %d enabled; timer delay (%d us).\n",
4989                             ha->zio_mode, ha->zio_timer * 100);
4990
4991                         icb->add_firmware_options[0] |= (uint8_t)ha->zio_mode;
4992                         icb->interrupt_delay_timer = (uint8_t)ha->zio_timer;
4993                         vha->flags.process_response_queue = 1;
4994                 }
4995         }
4996
4997         if (rval) {
4998                 ql_log(ql_log_warn, vha, 0x0069,
4999                     "NVRAM configuration failed.\n");
5000         }
5001         return (rval);
5002 }
5003
5004 static void
5005 qla2x00_rport_del(void *data)
5006 {
5007         fc_port_t *fcport = data;
5008         struct fc_rport *rport;
5009         unsigned long flags;
5010
5011         spin_lock_irqsave(fcport->vha->host->host_lock, flags);
5012         rport = fcport->drport ? fcport->drport : fcport->rport;
5013         fcport->drport = NULL;
5014         spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
5015         if (rport) {
5016                 ql_dbg(ql_dbg_disc, fcport->vha, 0x210b,
5017                     "%s %8phN. rport %p roles %x\n",
5018                     __func__, fcport->port_name, rport,
5019                     rport->roles);
5020
5021                 fc_remote_port_delete(rport);
5022         }
5023 }
5024
5025 void qla2x00_set_fcport_state(fc_port_t *fcport, int state)
5026 {
5027         int old_state;
5028
5029         old_state = atomic_read(&fcport->state);
5030         atomic_set(&fcport->state, state);
5031
5032         /* Don't print state transitions during initial allocation of fcport */
5033         if (old_state && old_state != state) {
5034                 ql_dbg(ql_dbg_disc, fcport->vha, 0x207d,
5035                        "FCPort %8phC state transitioned from %s to %s - portid=%02x%02x%02x.\n",
5036                        fcport->port_name, port_state_str[old_state],
5037                        port_state_str[state], fcport->d_id.b.domain,
5038                        fcport->d_id.b.area, fcport->d_id.b.al_pa);
5039         }
5040 }
5041
5042 /**
5043  * qla2x00_alloc_fcport() - Allocate a generic fcport.
5044  * @vha: HA context
5045  * @flags: allocation flags
5046  *
5047  * Returns a pointer to the allocated fcport, or NULL, if none available.
5048  */
5049 fc_port_t *
5050 qla2x00_alloc_fcport(scsi_qla_host_t *vha, gfp_t flags)
5051 {
5052         fc_port_t *fcport;
5053
5054         fcport = kzalloc(sizeof(fc_port_t), flags);
5055         if (!fcport)
5056                 return NULL;
5057
5058         fcport->ct_desc.ct_sns = dma_alloc_coherent(&vha->hw->pdev->dev,
5059                 sizeof(struct ct_sns_pkt), &fcport->ct_desc.ct_sns_dma,
5060                 flags);
5061         if (!fcport->ct_desc.ct_sns) {
5062                 ql_log(ql_log_warn, vha, 0xd049,
5063                     "Failed to allocate ct_sns request.\n");
5064                 kfree(fcport);
5065                 return NULL;
5066         }
5067
5068         /* Setup fcport template structure. */
5069         fcport->vha = vha;
5070         fcport->port_type = FCT_UNKNOWN;
5071         fcport->loop_id = FC_NO_LOOP_ID;
5072         qla2x00_set_fcport_state(fcport, FCS_UNCONFIGURED);
5073         fcport->supported_classes = FC_COS_UNSPECIFIED;
5074         fcport->fp_speed = PORT_SPEED_UNKNOWN;
5075
5076         fcport->disc_state = DSC_DELETED;
5077         fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
5078         fcport->deleted = QLA_SESS_DELETED;
5079         fcport->login_retry = vha->hw->login_retry_count;
5080         fcport->chip_reset = vha->hw->base_qpair->chip_reset;
5081         fcport->logout_on_delete = 1;
5082
5083         if (!fcport->ct_desc.ct_sns) {
5084                 ql_log(ql_log_warn, vha, 0xd049,
5085                     "Failed to allocate ct_sns request.\n");
5086                 kfree(fcport);
5087                 return NULL;
5088         }
5089
5090         INIT_WORK(&fcport->del_work, qla24xx_delete_sess_fn);
5091         INIT_WORK(&fcport->free_work, qlt_free_session_done);
5092         INIT_WORK(&fcport->reg_work, qla_register_fcport_fn);
5093         INIT_LIST_HEAD(&fcport->gnl_entry);
5094         INIT_LIST_HEAD(&fcport->list);
5095
5096         return fcport;
5097 }
5098
5099 void
5100 qla2x00_free_fcport(fc_port_t *fcport)
5101 {
5102         if (fcport->ct_desc.ct_sns) {
5103                 dma_free_coherent(&fcport->vha->hw->pdev->dev,
5104                         sizeof(struct ct_sns_pkt), fcport->ct_desc.ct_sns,
5105                         fcport->ct_desc.ct_sns_dma);
5106
5107                 fcport->ct_desc.ct_sns = NULL;
5108         }
5109         list_del(&fcport->list);
5110         qla2x00_clear_loop_id(fcport);
5111         kfree(fcport);
5112 }
5113
5114 static void qla_get_login_template(scsi_qla_host_t *vha)
5115 {
5116         struct qla_hw_data *ha = vha->hw;
5117         int rval;
5118         u32 *bp, sz;
5119         __be32 *q;
5120
5121         memset(ha->init_cb, 0, ha->init_cb_size);
5122         sz = min_t(int, sizeof(struct fc_els_flogi), ha->init_cb_size);
5123         rval = qla24xx_get_port_login_templ(vha, ha->init_cb_dma,
5124                                             ha->init_cb, sz);
5125         if (rval != QLA_SUCCESS) {
5126                 ql_dbg(ql_dbg_init, vha, 0x00d1,
5127                        "PLOGI ELS param read fail.\n");
5128                 return;
5129         }
5130         q = (__be32 *)&ha->plogi_els_payld.fl_csp;
5131
5132         bp = (uint32_t *)ha->init_cb;
5133         cpu_to_be32_array(q, bp, sz / 4);
5134         ha->flags.plogi_template_valid = 1;
5135 }
5136
5137 /*
5138  * qla2x00_configure_loop
5139  *      Updates Fibre Channel Device Database with what is actually on loop.
5140  *
5141  * Input:
5142  *      ha                = adapter block pointer.
5143  *
5144  * Returns:
5145  *      0 = success.
5146  *      1 = error.
5147  *      2 = database was full and device was not configured.
5148  */
5149 static int
5150 qla2x00_configure_loop(scsi_qla_host_t *vha)
5151 {
5152         int  rval;
5153         unsigned long flags, save_flags;
5154         struct qla_hw_data *ha = vha->hw;
5155
5156         rval = QLA_SUCCESS;
5157
5158         /* Get Initiator ID */
5159         if (test_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags)) {
5160                 rval = qla2x00_configure_hba(vha);
5161                 if (rval != QLA_SUCCESS) {
5162                         ql_dbg(ql_dbg_disc, vha, 0x2013,
5163                             "Unable to configure HBA.\n");
5164                         return (rval);
5165                 }
5166         }
5167
5168         save_flags = flags = vha->dpc_flags;
5169         ql_dbg(ql_dbg_disc, vha, 0x2014,
5170             "Configure loop -- dpc flags = 0x%lx.\n", flags);
5171
5172         /*
5173          * If we have both an RSCN and PORT UPDATE pending then handle them
5174          * both at the same time.
5175          */
5176         clear_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5177         clear_bit(RSCN_UPDATE, &vha->dpc_flags);
5178
5179         qla2x00_get_data_rate(vha);
5180         qla_get_login_template(vha);
5181
5182         /* Determine what we need to do */
5183         if ((ha->current_topology == ISP_CFG_FL ||
5184             ha->current_topology == ISP_CFG_F) &&
5185             (test_bit(LOCAL_LOOP_UPDATE, &flags))) {
5186
5187                 set_bit(RSCN_UPDATE, &flags);
5188                 clear_bit(LOCAL_LOOP_UPDATE, &flags);
5189
5190         } else if (ha->current_topology == ISP_CFG_NL ||
5191                    ha->current_topology == ISP_CFG_N) {
5192                 clear_bit(RSCN_UPDATE, &flags);
5193                 set_bit(LOCAL_LOOP_UPDATE, &flags);
5194         } else if (!vha->flags.online ||
5195             (test_bit(ABORT_ISP_ACTIVE, &flags))) {
5196                 set_bit(RSCN_UPDATE, &flags);
5197                 set_bit(LOCAL_LOOP_UPDATE, &flags);
5198         }
5199
5200         if (test_bit(LOCAL_LOOP_UPDATE, &flags)) {
5201                 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
5202                         ql_dbg(ql_dbg_disc, vha, 0x2015,
5203                             "Loop resync needed, failing.\n");
5204                         rval = QLA_FUNCTION_FAILED;
5205                 } else
5206                         rval = qla2x00_configure_local_loop(vha);
5207         }
5208
5209         if (rval == QLA_SUCCESS && test_bit(RSCN_UPDATE, &flags)) {
5210                 if (LOOP_TRANSITION(vha)) {
5211                         ql_dbg(ql_dbg_disc, vha, 0x2099,
5212                             "Needs RSCN update and loop transition.\n");
5213                         rval = QLA_FUNCTION_FAILED;
5214                 }
5215                 else
5216                         rval = qla2x00_configure_fabric(vha);
5217         }
5218
5219         if (rval == QLA_SUCCESS) {
5220                 if (atomic_read(&vha->loop_down_timer) ||
5221                     test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
5222                         rval = QLA_FUNCTION_FAILED;
5223                 } else {
5224                         atomic_set(&vha->loop_state, LOOP_READY);
5225                         ql_dbg(ql_dbg_disc, vha, 0x2069,
5226                             "LOOP READY.\n");
5227                         ha->flags.fw_init_done = 1;
5228
5229                         /*
5230                          * Process any ATIO queue entries that came in
5231                          * while we weren't online.
5232                          */
5233                         if (qla_tgt_mode_enabled(vha) ||
5234                             qla_dual_mode_enabled(vha)) {
5235                                 spin_lock_irqsave(&ha->tgt.atio_lock, flags);
5236                                 qlt_24xx_process_atio_queue(vha, 0);
5237                                 spin_unlock_irqrestore(&ha->tgt.atio_lock,
5238                                     flags);
5239                         }
5240                 }
5241         }
5242
5243         if (rval) {
5244                 ql_dbg(ql_dbg_disc, vha, 0x206a,
5245                     "%s *** FAILED ***.\n", __func__);
5246         } else {
5247                 ql_dbg(ql_dbg_disc, vha, 0x206b,
5248                     "%s: exiting normally.\n", __func__);
5249         }
5250
5251         /* Restore state if a resync event occurred during processing */
5252         if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags)) {
5253                 if (test_bit(LOCAL_LOOP_UPDATE, &save_flags))
5254                         set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5255                 if (test_bit(RSCN_UPDATE, &save_flags)) {
5256                         set_bit(RSCN_UPDATE, &vha->dpc_flags);
5257                 }
5258         }
5259
5260         return (rval);
5261 }
5262
5263 static int qla2x00_configure_n2n_loop(scsi_qla_host_t *vha)
5264 {
5265         unsigned long flags;
5266         fc_port_t *fcport;
5267
5268         if (test_and_clear_bit(N2N_LOGIN_NEEDED, &vha->dpc_flags))
5269                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
5270
5271         list_for_each_entry(fcport, &vha->vp_fcports, list) {
5272                 if (fcport->n2n_flag) {
5273                         qla24xx_fcport_handle_login(vha, fcport);
5274                         return QLA_SUCCESS;
5275                 }
5276         }
5277
5278         spin_lock_irqsave(&vha->work_lock, flags);
5279         vha->scan.scan_retry++;
5280         spin_unlock_irqrestore(&vha->work_lock, flags);
5281
5282         if (vha->scan.scan_retry < MAX_SCAN_RETRIES) {
5283                 set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5284                 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5285         }
5286         return QLA_FUNCTION_FAILED;
5287 }
5288
5289 static void
5290 qla_reinitialize_link(scsi_qla_host_t *vha)
5291 {
5292         int rval;
5293
5294         atomic_set(&vha->loop_state, LOOP_DOWN);
5295         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
5296         rval = qla2x00_full_login_lip(vha);
5297         if (rval == QLA_SUCCESS) {
5298                 ql_dbg(ql_dbg_disc, vha, 0xd050, "Link reinitialized\n");
5299         } else {
5300                 ql_dbg(ql_dbg_disc, vha, 0xd051,
5301                         "Link reinitialization failed (%d)\n", rval);
5302         }
5303 }
5304
5305 /*
5306  * qla2x00_configure_local_loop
5307  *      Updates Fibre Channel Device Database with local loop devices.
5308  *
5309  * Input:
5310  *      ha = adapter block pointer.
5311  *
5312  * Returns:
5313  *      0 = success.
5314  */
5315 static int
5316 qla2x00_configure_local_loop(scsi_qla_host_t *vha)
5317 {
5318         int             rval, rval2;
5319         int             found_devs;
5320         int             found;
5321         fc_port_t       *fcport, *new_fcport;
5322         uint16_t        index;
5323         uint16_t        entries;
5324         struct gid_list_info *gid;
5325         uint16_t        loop_id;
5326         uint8_t         domain, area, al_pa;
5327         struct qla_hw_data *ha = vha->hw;
5328         unsigned long flags;
5329
5330         /* Inititae N2N login. */
5331         if (N2N_TOPO(ha))
5332                 return qla2x00_configure_n2n_loop(vha);
5333
5334         found_devs = 0;
5335         new_fcport = NULL;
5336         entries = MAX_FIBRE_DEVICES_LOOP;
5337
5338         /* Get list of logged in devices. */
5339         memset(ha->gid_list, 0, qla2x00_gid_list_size(ha));
5340         rval = qla2x00_get_id_list(vha, ha->gid_list, ha->gid_list_dma,
5341             &entries);
5342         if (rval != QLA_SUCCESS)
5343                 goto err;
5344
5345         ql_dbg(ql_dbg_disc, vha, 0x2011,
5346             "Entries in ID list (%d).\n", entries);
5347         ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x2075,
5348             ha->gid_list, entries * sizeof(*ha->gid_list));
5349
5350         if (entries == 0) {
5351                 spin_lock_irqsave(&vha->work_lock, flags);
5352                 vha->scan.scan_retry++;
5353                 spin_unlock_irqrestore(&vha->work_lock, flags);
5354
5355                 if (vha->scan.scan_retry < MAX_SCAN_RETRIES) {
5356                         u8 loop_map_entries = 0;
5357                         int rc;
5358
5359                         rc = qla2x00_get_fcal_position_map(vha, NULL,
5360                                                 &loop_map_entries);
5361                         if (rc == QLA_SUCCESS && loop_map_entries > 1) {
5362                                 /*
5363                                  * There are devices that are still not logged
5364                                  * in. Reinitialize to give them a chance.
5365                                  */
5366                                 qla_reinitialize_link(vha);
5367                                 return QLA_FUNCTION_FAILED;
5368                         }
5369                         set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
5370                         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5371                 }
5372         } else {
5373                 vha->scan.scan_retry = 0;
5374         }
5375
5376         list_for_each_entry(fcport, &vha->vp_fcports, list) {
5377                 fcport->scan_state = QLA_FCPORT_SCAN;
5378         }
5379
5380         /* Allocate temporary fcport for any new fcports discovered. */
5381         new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
5382         if (new_fcport == NULL) {
5383                 ql_log(ql_log_warn, vha, 0x2012,
5384                     "Memory allocation failed for fcport.\n");
5385                 rval = QLA_MEMORY_ALLOC_FAILED;
5386                 goto err;
5387         }
5388         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
5389
5390         /* Add devices to port list. */
5391         gid = ha->gid_list;
5392         for (index = 0; index < entries; index++) {
5393                 domain = gid->domain;
5394                 area = gid->area;
5395                 al_pa = gid->al_pa;
5396                 if (IS_QLA2100(ha) || IS_QLA2200(ha))
5397                         loop_id = gid->loop_id_2100;
5398                 else
5399                         loop_id = le16_to_cpu(gid->loop_id);
5400                 gid = (void *)gid + ha->gid_list_info_size;
5401
5402                 /* Bypass reserved domain fields. */
5403                 if ((domain & 0xf0) == 0xf0)
5404                         continue;
5405
5406                 /* Bypass if not same domain and area of adapter. */
5407                 if (area && domain && ((area != vha->d_id.b.area) ||
5408                     (domain != vha->d_id.b.domain)) &&
5409                     (ha->current_topology == ISP_CFG_NL))
5410                         continue;
5411
5412
5413                 /* Bypass invalid local loop ID. */
5414                 if (loop_id > LAST_LOCAL_LOOP_ID)
5415                         continue;
5416
5417                 memset(new_fcport->port_name, 0, WWN_SIZE);
5418
5419                 /* Fill in member data. */
5420                 new_fcport->d_id.b.domain = domain;
5421                 new_fcport->d_id.b.area = area;
5422                 new_fcport->d_id.b.al_pa = al_pa;
5423                 new_fcport->loop_id = loop_id;
5424                 new_fcport->scan_state = QLA_FCPORT_FOUND;
5425
5426                 rval2 = qla2x00_get_port_database(vha, new_fcport, 0);
5427                 if (rval2 != QLA_SUCCESS) {
5428                         ql_dbg(ql_dbg_disc, vha, 0x2097,
5429                             "Failed to retrieve fcport information "
5430                             "-- get_port_database=%x, loop_id=0x%04x.\n",
5431                             rval2, new_fcport->loop_id);
5432                         /* Skip retry if N2N */
5433                         if (ha->current_topology != ISP_CFG_N) {
5434                                 ql_dbg(ql_dbg_disc, vha, 0x2105,
5435                                     "Scheduling resync.\n");
5436                                 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5437                                 continue;
5438                         }
5439                 }
5440
5441                 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5442                 /* Check for matching device in port list. */
5443                 found = 0;
5444                 fcport = NULL;
5445                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
5446                         if (memcmp(new_fcport->port_name, fcport->port_name,
5447                             WWN_SIZE))
5448                                 continue;
5449
5450                         fcport->flags &= ~FCF_FABRIC_DEVICE;
5451                         fcport->loop_id = new_fcport->loop_id;
5452                         fcport->port_type = new_fcport->port_type;
5453                         fcport->d_id.b24 = new_fcport->d_id.b24;
5454                         memcpy(fcport->node_name, new_fcport->node_name,
5455                             WWN_SIZE);
5456                         fcport->scan_state = QLA_FCPORT_FOUND;
5457                         if (fcport->login_retry == 0) {
5458                                 fcport->login_retry = vha->hw->login_retry_count;
5459                                 ql_dbg(ql_dbg_disc, vha, 0x2135,
5460                                     "Port login retry %8phN, lid 0x%04x retry cnt=%d.\n",
5461                                     fcport->port_name, fcport->loop_id,
5462                                     fcport->login_retry);
5463                         }
5464                         found++;
5465                         break;
5466                 }
5467
5468                 if (!found) {
5469                         /* New device, add to fcports list. */
5470                         list_add_tail(&new_fcport->list, &vha->vp_fcports);
5471
5472                         /* Allocate a new replacement fcport. */
5473                         fcport = new_fcport;
5474
5475                         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5476
5477                         new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
5478
5479                         if (new_fcport == NULL) {
5480                                 ql_log(ql_log_warn, vha, 0xd031,
5481                                     "Failed to allocate memory for fcport.\n");
5482                                 rval = QLA_MEMORY_ALLOC_FAILED;
5483                                 goto err;
5484                         }
5485                         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
5486                         new_fcport->flags &= ~FCF_FABRIC_DEVICE;
5487                 }
5488
5489                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
5490
5491                 /* Base iIDMA settings on HBA port speed. */
5492                 fcport->fp_speed = ha->link_data_rate;
5493
5494                 found_devs++;
5495         }
5496
5497         list_for_each_entry(fcport, &vha->vp_fcports, list) {
5498                 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5499                         break;
5500
5501                 if (fcport->scan_state == QLA_FCPORT_SCAN) {
5502                         if ((qla_dual_mode_enabled(vha) ||
5503                             qla_ini_mode_enabled(vha)) &&
5504                             atomic_read(&fcport->state) == FCS_ONLINE) {
5505                                 qla2x00_mark_device_lost(vha, fcport,
5506                                         ql2xplogiabsentdevice);
5507                                 if (fcport->loop_id != FC_NO_LOOP_ID &&
5508                                     (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
5509                                     fcport->port_type != FCT_INITIATOR &&
5510                                     fcport->port_type != FCT_BROADCAST) {
5511                                         ql_dbg(ql_dbg_disc, vha, 0x20f0,
5512                                             "%s %d %8phC post del sess\n",
5513                                             __func__, __LINE__,
5514                                             fcport->port_name);
5515
5516                                         qlt_schedule_sess_for_deletion(fcport);
5517                                         continue;
5518                                 }
5519                         }
5520                 }
5521
5522                 if (fcport->scan_state == QLA_FCPORT_FOUND)
5523                         qla24xx_fcport_handle_login(vha, fcport);
5524         }
5525
5526         qla2x00_free_fcport(new_fcport);
5527
5528         return rval;
5529
5530 err:
5531         ql_dbg(ql_dbg_disc, vha, 0x2098,
5532                "Configure local loop error exit: rval=%x.\n", rval);
5533         return rval;
5534 }
5535
5536 static void
5537 qla2x00_iidma_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
5538 {
5539         int rval;
5540         uint16_t mb[MAILBOX_REGISTER_COUNT];
5541         struct qla_hw_data *ha = vha->hw;
5542
5543         if (!IS_IIDMA_CAPABLE(ha))
5544                 return;
5545
5546         if (atomic_read(&fcport->state) != FCS_ONLINE)
5547                 return;
5548
5549         if (fcport->fp_speed == PORT_SPEED_UNKNOWN ||
5550             fcport->fp_speed > ha->link_data_rate ||
5551             !ha->flags.gpsc_supported)
5552                 return;
5553
5554         rval = qla2x00_set_idma_speed(vha, fcport->loop_id, fcport->fp_speed,
5555             mb);
5556         if (rval != QLA_SUCCESS) {
5557                 ql_dbg(ql_dbg_disc, vha, 0x2004,
5558                     "Unable to adjust iIDMA %8phN -- %04x %x %04x %04x.\n",
5559                     fcport->port_name, rval, fcport->fp_speed, mb[0], mb[1]);
5560         } else {
5561                 ql_dbg(ql_dbg_disc, vha, 0x2005,
5562                     "iIDMA adjusted to %s GB/s (%X) on %8phN.\n",
5563                     qla2x00_get_link_speed_str(ha, fcport->fp_speed),
5564                     fcport->fp_speed, fcport->port_name);
5565         }
5566 }
5567
5568 void qla_do_iidma_work(struct scsi_qla_host *vha, fc_port_t *fcport)
5569 {
5570         qla2x00_iidma_fcport(vha, fcport);
5571         qla24xx_update_fcport_fcp_prio(vha, fcport);
5572 }
5573
5574 int qla_post_iidma_work(struct scsi_qla_host *vha, fc_port_t *fcport)
5575 {
5576         struct qla_work_evt *e;
5577
5578         e = qla2x00_alloc_work(vha, QLA_EVT_IIDMA);
5579         if (!e)
5580                 return QLA_FUNCTION_FAILED;
5581
5582         e->u.fcport.fcport = fcport;
5583         return qla2x00_post_work(vha, e);
5584 }
5585
5586 /* qla2x00_reg_remote_port is reserved for Initiator Mode only.*/
5587 static void
5588 qla2x00_reg_remote_port(scsi_qla_host_t *vha, fc_port_t *fcport)
5589 {
5590         struct fc_rport_identifiers rport_ids;
5591         struct fc_rport *rport;
5592         unsigned long flags;
5593
5594         if (atomic_read(&fcport->state) == FCS_ONLINE)
5595                 return;
5596
5597         rport_ids.node_name = wwn_to_u64(fcport->node_name);
5598         rport_ids.port_name = wwn_to_u64(fcport->port_name);
5599         rport_ids.port_id = fcport->d_id.b.domain << 16 |
5600             fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
5601         rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
5602         fcport->rport = rport = fc_remote_port_add(vha->host, 0, &rport_ids);
5603         if (!rport) {
5604                 ql_log(ql_log_warn, vha, 0x2006,
5605                     "Unable to allocate fc remote port.\n");
5606                 return;
5607         }
5608
5609         spin_lock_irqsave(fcport->vha->host->host_lock, flags);
5610         *((fc_port_t **)rport->dd_data) = fcport;
5611         spin_unlock_irqrestore(fcport->vha->host->host_lock, flags);
5612
5613         rport->supported_classes = fcport->supported_classes;
5614
5615         rport_ids.roles = FC_PORT_ROLE_UNKNOWN;
5616         if (fcport->port_type == FCT_INITIATOR)
5617                 rport_ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
5618         if (fcport->port_type == FCT_TARGET)
5619                 rport_ids.roles |= FC_PORT_ROLE_FCP_TARGET;
5620         if (fcport->port_type & FCT_NVME_INITIATOR)
5621                 rport_ids.roles |= FC_PORT_ROLE_NVME_INITIATOR;
5622         if (fcport->port_type & FCT_NVME_TARGET)
5623                 rport_ids.roles |= FC_PORT_ROLE_NVME_TARGET;
5624         if (fcport->port_type & FCT_NVME_DISCOVERY)
5625                 rport_ids.roles |= FC_PORT_ROLE_NVME_DISCOVERY;
5626
5627         ql_dbg(ql_dbg_disc, vha, 0x20ee,
5628             "%s %8phN. rport %p is %s mode\n",
5629             __func__, fcport->port_name, rport,
5630             (fcport->port_type == FCT_TARGET) ? "tgt" :
5631             ((fcport->port_type & FCT_NVME) ? "nvme" : "ini"));
5632
5633         fc_remote_port_rolechg(rport, rport_ids.roles);
5634 }
5635
5636 /*
5637  * qla2x00_update_fcport
5638  *      Updates device on list.
5639  *
5640  * Input:
5641  *      ha = adapter block pointer.
5642  *      fcport = port structure pointer.
5643  *
5644  * Return:
5645  *      0  - Success
5646  *  BIT_0 - error
5647  *
5648  * Context:
5649  *      Kernel context.
5650  */
5651 void
5652 qla2x00_update_fcport(scsi_qla_host_t *vha, fc_port_t *fcport)
5653 {
5654         unsigned long flags;
5655
5656         if (IS_SW_RESV_ADDR(fcport->d_id))
5657                 return;
5658
5659         ql_dbg(ql_dbg_disc, vha, 0x20ef, "%s %8phC\n",
5660             __func__, fcport->port_name);
5661
5662         qla2x00_set_fcport_disc_state(fcport, DSC_UPD_FCPORT);
5663         fcport->login_retry = vha->hw->login_retry_count;
5664         fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
5665
5666         spin_lock_irqsave(&vha->work_lock, flags);
5667         fcport->deleted = 0;
5668         spin_unlock_irqrestore(&vha->work_lock, flags);
5669
5670         if (vha->hw->current_topology == ISP_CFG_NL)
5671                 fcport->logout_on_delete = 0;
5672         else
5673                 fcport->logout_on_delete = 1;
5674         fcport->n2n_chip_reset = fcport->n2n_link_reset_cnt = 0;
5675
5676         switch (vha->hw->current_topology) {
5677         case ISP_CFG_N:
5678         case ISP_CFG_NL:
5679                 fcport->keep_nport_handle = 1;
5680                 break;
5681         default:
5682                 break;
5683         }
5684
5685         qla2x00_iidma_fcport(vha, fcport);
5686
5687         qla2x00_dfs_create_rport(vha, fcport);
5688
5689         if (NVME_TARGET(vha->hw, fcport)) {
5690                 qla_nvme_register_remote(vha, fcport);
5691                 qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_COMPLETE);
5692                 qla2x00_set_fcport_state(fcport, FCS_ONLINE);
5693                 return;
5694         }
5695
5696         qla24xx_update_fcport_fcp_prio(vha, fcport);
5697
5698         switch (vha->host->active_mode) {
5699         case MODE_INITIATOR:
5700                 qla2x00_reg_remote_port(vha, fcport);
5701                 break;
5702         case MODE_TARGET:
5703                 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5704                         !vha->vha_tgt.qla_tgt->tgt_stopped)
5705                         qlt_fc_port_added(vha, fcport);
5706                 break;
5707         case MODE_DUAL:
5708                 qla2x00_reg_remote_port(vha, fcport);
5709                 if (!vha->vha_tgt.qla_tgt->tgt_stop &&
5710                         !vha->vha_tgt.qla_tgt->tgt_stopped)
5711                         qlt_fc_port_added(vha, fcport);
5712                 break;
5713         default:
5714                 break;
5715         }
5716
5717         qla2x00_set_fcport_state(fcport, FCS_ONLINE);
5718
5719         if (IS_IIDMA_CAPABLE(vha->hw) && vha->hw->flags.gpsc_supported) {
5720                 if (fcport->id_changed) {
5721                         fcport->id_changed = 0;
5722                         ql_dbg(ql_dbg_disc, vha, 0x20d7,
5723                             "%s %d %8phC post gfpnid fcp_cnt %d\n",
5724                             __func__, __LINE__, fcport->port_name,
5725                             vha->fcport_count);
5726                         qla24xx_post_gfpnid_work(vha, fcport);
5727                 } else {
5728                         ql_dbg(ql_dbg_disc, vha, 0x20d7,
5729                             "%s %d %8phC post gpsc fcp_cnt %d\n",
5730                             __func__, __LINE__, fcport->port_name,
5731                             vha->fcport_count);
5732                         qla24xx_post_gpsc_work(vha, fcport);
5733                 }
5734         }
5735
5736         qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_COMPLETE);
5737 }
5738
5739 void qla_register_fcport_fn(struct work_struct *work)
5740 {
5741         fc_port_t *fcport = container_of(work, struct fc_port, reg_work);
5742         u32 rscn_gen = fcport->rscn_gen;
5743         u16 data[2];
5744
5745         if (IS_SW_RESV_ADDR(fcport->d_id))
5746                 return;
5747
5748         qla2x00_update_fcport(fcport->vha, fcport);
5749
5750         if (rscn_gen != fcport->rscn_gen) {
5751                 /* RSCN(s) came in while registration */
5752                 switch (fcport->next_disc_state) {
5753                 case DSC_DELETE_PEND:
5754                         qlt_schedule_sess_for_deletion(fcport);
5755                         break;
5756                 case DSC_ADISC:
5757                         data[0] = data[1] = 0;
5758                         qla2x00_post_async_adisc_work(fcport->vha, fcport,
5759                             data);
5760                         break;
5761                 default:
5762                         break;
5763                 }
5764         }
5765 }
5766
5767 /*
5768  * qla2x00_configure_fabric
5769  *      Setup SNS devices with loop ID's.
5770  *
5771  * Input:
5772  *      ha = adapter block pointer.
5773  *
5774  * Returns:
5775  *      0 = success.
5776  *      BIT_0 = error
5777  */
5778 static int
5779 qla2x00_configure_fabric(scsi_qla_host_t *vha)
5780 {
5781         int     rval;
5782         fc_port_t       *fcport;
5783         uint16_t        mb[MAILBOX_REGISTER_COUNT];
5784         uint16_t        loop_id;
5785         LIST_HEAD(new_fcports);
5786         struct qla_hw_data *ha = vha->hw;
5787         int             discovery_gen;
5788
5789         /* If FL port exists, then SNS is present */
5790         if (IS_FWI2_CAPABLE(ha))
5791                 loop_id = NPH_F_PORT;
5792         else
5793                 loop_id = SNS_FL_PORT;
5794         rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_node_name, 1);
5795         if (rval != QLA_SUCCESS) {
5796                 ql_dbg(ql_dbg_disc, vha, 0x20a0,
5797                     "MBX_GET_PORT_NAME failed, No FL Port.\n");
5798
5799                 vha->device_flags &= ~SWITCH_FOUND;
5800                 return (QLA_SUCCESS);
5801         }
5802         vha->device_flags |= SWITCH_FOUND;
5803
5804         rval = qla2x00_get_port_name(vha, loop_id, vha->fabric_port_name, 0);
5805         if (rval != QLA_SUCCESS)
5806                 ql_dbg(ql_dbg_disc, vha, 0x20ff,
5807                     "Failed to get Fabric Port Name\n");
5808
5809         if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
5810                 rval = qla2x00_send_change_request(vha, 0x3, 0);
5811                 if (rval != QLA_SUCCESS)
5812                         ql_log(ql_log_warn, vha, 0x121,
5813                             "Failed to enable receiving of RSCN requests: 0x%x.\n",
5814                             rval);
5815         }
5816
5817         do {
5818                 qla2x00_mgmt_svr_login(vha);
5819
5820                 /* Ensure we are logged into the SNS. */
5821                 loop_id = NPH_SNS_LID(ha);
5822                 rval = ha->isp_ops->fabric_login(vha, loop_id, 0xff, 0xff,
5823                     0xfc, mb, BIT_1|BIT_0);
5824                 if (rval != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
5825                         ql_dbg(ql_dbg_disc, vha, 0x20a1,
5826                             "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x mb[2]=%x mb[6]=%x mb[7]=%x (%x).\n",
5827                             loop_id, mb[0], mb[1], mb[2], mb[6], mb[7], rval);
5828                         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5829                         return rval;
5830                 }
5831
5832                 /* FDMI support. */
5833                 if (ql2xfdmienable &&
5834                     test_and_clear_bit(REGISTER_FDMI_NEEDED, &vha->dpc_flags))
5835                         qla2x00_fdmi_register(vha);
5836
5837                 if (test_and_clear_bit(REGISTER_FC4_NEEDED, &vha->dpc_flags)) {
5838                         if (qla2x00_rft_id(vha)) {
5839                                 /* EMPTY */
5840                                 ql_dbg(ql_dbg_disc, vha, 0x20a2,
5841                                     "Register FC-4 TYPE failed.\n");
5842                                 if (test_bit(LOOP_RESYNC_NEEDED,
5843                                     &vha->dpc_flags))
5844                                         break;
5845                         }
5846                         if (qla2x00_rff_id(vha, FC4_TYPE_FCP_SCSI)) {
5847                                 /* EMPTY */
5848                                 ql_dbg(ql_dbg_disc, vha, 0x209a,
5849                                     "Register FC-4 Features failed.\n");
5850                                 if (test_bit(LOOP_RESYNC_NEEDED,
5851                                     &vha->dpc_flags))
5852                                         break;
5853                         }
5854                         if (vha->flags.nvme_enabled) {
5855                                 if (qla2x00_rff_id(vha, FC_TYPE_NVME)) {
5856                                         ql_dbg(ql_dbg_disc, vha, 0x2049,
5857                                             "Register NVME FC Type Features failed.\n");
5858                                 }
5859                         }
5860                         if (qla2x00_rnn_id(vha)) {
5861                                 /* EMPTY */
5862                                 ql_dbg(ql_dbg_disc, vha, 0x2104,
5863                                     "Register Node Name failed.\n");
5864                                 if (test_bit(LOOP_RESYNC_NEEDED,
5865                                     &vha->dpc_flags))
5866                                         break;
5867                         } else if (qla2x00_rsnn_nn(vha)) {
5868                                 /* EMPTY */
5869                                 ql_dbg(ql_dbg_disc, vha, 0x209b,
5870                                     "Register Symbolic Node Name failed.\n");
5871                                 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5872                                         break;
5873                         }
5874                 }
5875
5876
5877                 /* Mark the time right before querying FW for connected ports.
5878                  * This process is long, asynchronous and by the time it's done,
5879                  * collected information might not be accurate anymore. E.g.
5880                  * disconnected port might have re-connected and a brand new
5881                  * session has been created. In this case session's generation
5882                  * will be newer than discovery_gen. */
5883                 qlt_do_generation_tick(vha, &discovery_gen);
5884
5885                 if (USE_ASYNC_SCAN(ha)) {
5886                         rval = qla24xx_async_gpnft(vha, FC4_TYPE_FCP_SCSI,
5887                             NULL);
5888                         if (rval)
5889                                 set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
5890                 } else  {
5891                         list_for_each_entry(fcport, &vha->vp_fcports, list)
5892                                 fcport->scan_state = QLA_FCPORT_SCAN;
5893
5894                         rval = qla2x00_find_all_fabric_devs(vha);
5895                 }
5896                 if (rval != QLA_SUCCESS)
5897                         break;
5898         } while (0);
5899
5900         if (!vha->nvme_local_port && vha->flags.nvme_enabled)
5901                 qla_nvme_register_hba(vha);
5902
5903         if (rval)
5904                 ql_dbg(ql_dbg_disc, vha, 0x2068,
5905                     "Configure fabric error exit rval=%d.\n", rval);
5906
5907         return (rval);
5908 }
5909
5910 /*
5911  * qla2x00_find_all_fabric_devs
5912  *
5913  * Input:
5914  *      ha = adapter block pointer.
5915  *      dev = database device entry pointer.
5916  *
5917  * Returns:
5918  *      0 = success.
5919  *
5920  * Context:
5921  *      Kernel context.
5922  */
5923 static int
5924 qla2x00_find_all_fabric_devs(scsi_qla_host_t *vha)
5925 {
5926         int             rval;
5927         uint16_t        loop_id;
5928         fc_port_t       *fcport, *new_fcport;
5929         int             found;
5930
5931         sw_info_t       *swl;
5932         int             swl_idx;
5933         int             first_dev, last_dev;
5934         port_id_t       wrap = {}, nxt_d_id;
5935         struct qla_hw_data *ha = vha->hw;
5936         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
5937         unsigned long flags;
5938
5939         rval = QLA_SUCCESS;
5940
5941         /* Try GID_PT to get device list, else GAN. */
5942         if (!ha->swl)
5943                 ha->swl = kcalloc(ha->max_fibre_devices, sizeof(sw_info_t),
5944                     GFP_KERNEL);
5945         swl = ha->swl;
5946         if (!swl) {
5947                 /*EMPTY*/
5948                 ql_dbg(ql_dbg_disc, vha, 0x209c,
5949                     "GID_PT allocations failed, fallback on GA_NXT.\n");
5950         } else {
5951                 memset(swl, 0, ha->max_fibre_devices * sizeof(sw_info_t));
5952                 if (qla2x00_gid_pt(vha, swl) != QLA_SUCCESS) {
5953                         swl = NULL;
5954                         if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5955                                 return rval;
5956                 } else if (qla2x00_gpn_id(vha, swl) != QLA_SUCCESS) {
5957                         swl = NULL;
5958                         if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5959                                 return rval;
5960                 } else if (qla2x00_gnn_id(vha, swl) != QLA_SUCCESS) {
5961                         swl = NULL;
5962                         if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5963                                 return rval;
5964                 } else if (qla2x00_gfpn_id(vha, swl) != QLA_SUCCESS) {
5965                         swl = NULL;
5966                         if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5967                                 return rval;
5968                 }
5969
5970                 /* If other queries succeeded probe for FC-4 type */
5971                 if (swl) {
5972                         qla2x00_gff_id(vha, swl);
5973                         if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
5974                                 return rval;
5975                 }
5976         }
5977         swl_idx = 0;
5978
5979         /* Allocate temporary fcport for any new fcports discovered. */
5980         new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
5981         if (new_fcport == NULL) {
5982                 ql_log(ql_log_warn, vha, 0x209d,
5983                     "Failed to allocate memory for fcport.\n");
5984                 return (QLA_MEMORY_ALLOC_FAILED);
5985         }
5986         new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
5987         /* Set start port ID scan at adapter ID. */
5988         first_dev = 1;
5989         last_dev = 0;
5990
5991         /* Starting free loop ID. */
5992         loop_id = ha->min_external_loopid;
5993         for (; loop_id <= ha->max_loop_id; loop_id++) {
5994                 if (qla2x00_is_reserved_id(vha, loop_id))
5995                         continue;
5996
5997                 if (ha->current_topology == ISP_CFG_FL &&
5998                     (atomic_read(&vha->loop_down_timer) ||
5999                      LOOP_TRANSITION(vha))) {
6000                         atomic_set(&vha->loop_down_timer, 0);
6001                         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
6002                         set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
6003                         break;
6004                 }
6005
6006                 if (swl != NULL) {
6007                         if (last_dev) {
6008                                 wrap.b24 = new_fcport->d_id.b24;
6009                         } else {
6010                                 new_fcport->d_id.b24 = swl[swl_idx].d_id.b24;
6011                                 memcpy(new_fcport->node_name,
6012                                     swl[swl_idx].node_name, WWN_SIZE);
6013                                 memcpy(new_fcport->port_name,
6014                                     swl[swl_idx].port_name, WWN_SIZE);
6015                                 memcpy(new_fcport->fabric_port_name,
6016                                     swl[swl_idx].fabric_port_name, WWN_SIZE);
6017                                 new_fcport->fp_speed = swl[swl_idx].fp_speed;
6018                                 new_fcport->fc4_type = swl[swl_idx].fc4_type;
6019
6020                                 new_fcport->nvme_flag = 0;
6021                                 if (vha->flags.nvme_enabled &&
6022                                     swl[swl_idx].fc4_type & FS_FC4TYPE_NVME) {
6023                                         ql_log(ql_log_info, vha, 0x2131,
6024                                             "FOUND: NVME port %8phC as FC Type 28h\n",
6025                                             new_fcport->port_name);
6026                                 }
6027
6028                                 if (swl[swl_idx].d_id.b.rsvd_1 != 0) {
6029                                         last_dev = 1;
6030                                 }
6031                                 swl_idx++;
6032                         }
6033                 } else {
6034                         /* Send GA_NXT to the switch */
6035                         rval = qla2x00_ga_nxt(vha, new_fcport);
6036                         if (rval != QLA_SUCCESS) {
6037                                 ql_log(ql_log_warn, vha, 0x209e,
6038                                     "SNS scan failed -- assuming "
6039                                     "zero-entry result.\n");
6040                                 rval = QLA_SUCCESS;
6041                                 break;
6042                         }
6043                 }
6044
6045                 /* If wrap on switch device list, exit. */
6046                 if (first_dev) {
6047                         wrap.b24 = new_fcport->d_id.b24;
6048                         first_dev = 0;
6049                 } else if (new_fcport->d_id.b24 == wrap.b24) {
6050                         ql_dbg(ql_dbg_disc, vha, 0x209f,
6051                             "Device wrap (%02x%02x%02x).\n",
6052                             new_fcport->d_id.b.domain,
6053                             new_fcport->d_id.b.area,
6054                             new_fcport->d_id.b.al_pa);
6055                         break;
6056                 }
6057
6058                 /* Bypass if same physical adapter. */
6059                 if (new_fcport->d_id.b24 == base_vha->d_id.b24)
6060                         continue;
6061
6062                 /* Bypass virtual ports of the same host. */
6063                 if (qla2x00_is_a_vp_did(vha, new_fcport->d_id.b24))
6064                         continue;
6065
6066                 /* Bypass if same domain and area of adapter. */
6067                 if (((new_fcport->d_id.b24 & 0xffff00) ==
6068                     (vha->d_id.b24 & 0xffff00)) && ha->current_topology ==
6069                         ISP_CFG_FL)
6070                             continue;
6071
6072                 /* Bypass reserved domain fields. */
6073                 if ((new_fcport->d_id.b.domain & 0xf0) == 0xf0)
6074                         continue;
6075
6076                 /* Bypass ports whose FCP-4 type is not FCP_SCSI */
6077                 if (ql2xgffidenable &&
6078                     (!(new_fcport->fc4_type & FS_FC4TYPE_FCP) &&
6079                     new_fcport->fc4_type != 0))
6080                         continue;
6081
6082                 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
6083
6084                 /* Locate matching device in database. */
6085                 found = 0;
6086                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
6087                         if (memcmp(new_fcport->port_name, fcport->port_name,
6088                             WWN_SIZE))
6089                                 continue;
6090
6091                         fcport->scan_state = QLA_FCPORT_FOUND;
6092
6093                         found++;
6094
6095                         /* Update port state. */
6096                         memcpy(fcport->fabric_port_name,
6097                             new_fcport->fabric_port_name, WWN_SIZE);
6098                         fcport->fp_speed = new_fcport->fp_speed;
6099
6100                         /*
6101                          * If address the same and state FCS_ONLINE
6102                          * (or in target mode), nothing changed.
6103                          */
6104                         if (fcport->d_id.b24 == new_fcport->d_id.b24 &&
6105                             (atomic_read(&fcport->state) == FCS_ONLINE ||
6106                              (vha->host->active_mode == MODE_TARGET))) {
6107                                 break;
6108                         }
6109
6110                         /*
6111                          * If device was not a fabric device before.
6112                          */
6113                         if ((fcport->flags & FCF_FABRIC_DEVICE) == 0) {
6114                                 fcport->d_id.b24 = new_fcport->d_id.b24;
6115                                 qla2x00_clear_loop_id(fcport);
6116                                 fcport->flags |= (FCF_FABRIC_DEVICE |
6117                                     FCF_LOGIN_NEEDED);
6118                                 break;
6119                         }
6120
6121                         /*
6122                          * Port ID changed or device was marked to be updated;
6123                          * Log it out if still logged in and mark it for
6124                          * relogin later.
6125                          */
6126                         if (qla_tgt_mode_enabled(base_vha)) {
6127                                 ql_dbg(ql_dbg_tgt_mgt, vha, 0xf080,
6128                                          "port changed FC ID, %8phC"
6129                                          " old %x:%x:%x (loop_id 0x%04x)-> new %x:%x:%x\n",
6130                                          fcport->port_name,
6131                                          fcport->d_id.b.domain,
6132                                          fcport->d_id.b.area,
6133                                          fcport->d_id.b.al_pa,
6134                                          fcport->loop_id,
6135                                          new_fcport->d_id.b.domain,
6136                                          new_fcport->d_id.b.area,
6137                                          new_fcport->d_id.b.al_pa);
6138                                 fcport->d_id.b24 = new_fcport->d_id.b24;
6139                                 break;
6140                         }
6141
6142                         fcport->d_id.b24 = new_fcport->d_id.b24;
6143                         fcport->flags |= FCF_LOGIN_NEEDED;
6144                         break;
6145                 }
6146
6147                 if (found && NVME_TARGET(vha->hw, fcport)) {
6148                         if (fcport->disc_state == DSC_DELETE_PEND) {
6149                                 qla2x00_set_fcport_disc_state(fcport, DSC_GNL);
6150                                 vha->fcport_count--;
6151                                 fcport->login_succ = 0;
6152                         }
6153                 }
6154
6155                 if (found) {
6156                         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6157                         continue;
6158                 }
6159                 /* If device was not in our fcports list, then add it. */
6160                 new_fcport->scan_state = QLA_FCPORT_FOUND;
6161                 list_add_tail(&new_fcport->list, &vha->vp_fcports);
6162
6163                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
6164
6165
6166                 /* Allocate a new replacement fcport. */
6167                 nxt_d_id.b24 = new_fcport->d_id.b24;
6168                 new_fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
6169                 if (new_fcport == NULL) {
6170                         ql_log(ql_log_warn, vha, 0xd032,
6171                             "Memory allocation failed for fcport.\n");
6172                         return (QLA_MEMORY_ALLOC_FAILED);
6173                 }
6174                 new_fcport->flags |= (FCF_FABRIC_DEVICE | FCF_LOGIN_NEEDED);
6175                 new_fcport->d_id.b24 = nxt_d_id.b24;
6176         }
6177
6178         qla2x00_free_fcport(new_fcport);
6179
6180         /*
6181          * Logout all previous fabric dev marked lost, except FCP2 devices.
6182          */
6183         list_for_each_entry(fcport, &vha->vp_fcports, list) {
6184                 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
6185                         break;
6186
6187                 if ((fcport->flags & FCF_FABRIC_DEVICE) == 0)
6188                         continue;
6189
6190                 if (fcport->scan_state == QLA_FCPORT_SCAN) {
6191                         if ((qla_dual_mode_enabled(vha) ||
6192                             qla_ini_mode_enabled(vha)) &&
6193                             atomic_read(&fcport->state) == FCS_ONLINE) {
6194                                 qla2x00_mark_device_lost(vha, fcport,
6195                                         ql2xplogiabsentdevice);
6196                                 if (fcport->loop_id != FC_NO_LOOP_ID &&
6197                                     (fcport->flags & FCF_FCP2_DEVICE) == 0 &&
6198                                     fcport->port_type != FCT_INITIATOR &&
6199                                     fcport->port_type != FCT_BROADCAST) {
6200                                         ql_dbg(ql_dbg_disc, vha, 0x20f0,
6201                                             "%s %d %8phC post del sess\n",
6202                                             __func__, __LINE__,
6203                                             fcport->port_name);
6204                                         qlt_schedule_sess_for_deletion(fcport);
6205                                         continue;
6206                                 }
6207                         }
6208                 }
6209
6210                 if (fcport->scan_state == QLA_FCPORT_FOUND &&
6211                     (fcport->flags & FCF_LOGIN_NEEDED) != 0)
6212                         qla24xx_fcport_handle_login(vha, fcport);
6213         }
6214         return (rval);
6215 }
6216
6217 /* FW does not set aside Loop id for MGMT Server/FFFFFAh */
6218 int
6219 qla2x00_reserve_mgmt_server_loop_id(scsi_qla_host_t *vha)
6220 {
6221         int loop_id = FC_NO_LOOP_ID;
6222         int lid = NPH_MGMT_SERVER - vha->vp_idx;
6223         unsigned long flags;
6224         struct qla_hw_data *ha = vha->hw;
6225
6226         if (vha->vp_idx == 0) {
6227                 set_bit(NPH_MGMT_SERVER, ha->loop_id_map);
6228                 return NPH_MGMT_SERVER;
6229         }
6230
6231         /* pick id from high and work down to low */
6232         spin_lock_irqsave(&ha->vport_slock, flags);
6233         for (; lid > 0; lid--) {
6234                 if (!test_bit(lid, vha->hw->loop_id_map)) {
6235                         set_bit(lid, vha->hw->loop_id_map);
6236                         loop_id = lid;
6237                         break;
6238                 }
6239         }
6240         spin_unlock_irqrestore(&ha->vport_slock, flags);
6241
6242         return loop_id;
6243 }
6244
6245 /*
6246  * qla2x00_fabric_login
6247  *      Issue fabric login command.
6248  *
6249  * Input:
6250  *      ha = adapter block pointer.
6251  *      device = pointer to FC device type structure.
6252  *
6253  * Returns:
6254  *      0 - Login successfully
6255  *      1 - Login failed
6256  *      2 - Initiator device
6257  *      3 - Fatal error
6258  */
6259 int
6260 qla2x00_fabric_login(scsi_qla_host_t *vha, fc_port_t *fcport,
6261     uint16_t *next_loopid)
6262 {
6263         int     rval;
6264         int     retry;
6265         uint16_t tmp_loopid;
6266         uint16_t mb[MAILBOX_REGISTER_COUNT];
6267         struct qla_hw_data *ha = vha->hw;
6268
6269         retry = 0;
6270         tmp_loopid = 0;
6271
6272         for (;;) {
6273                 ql_dbg(ql_dbg_disc, vha, 0x2000,
6274                     "Trying Fabric Login w/loop id 0x%04x for port "
6275                     "%02x%02x%02x.\n",
6276                     fcport->loop_id, fcport->d_id.b.domain,
6277                     fcport->d_id.b.area, fcport->d_id.b.al_pa);
6278
6279                 /* Login fcport on switch. */
6280                 rval = ha->isp_ops->fabric_login(vha, fcport->loop_id,
6281                     fcport->d_id.b.domain, fcport->d_id.b.area,
6282                     fcport->d_id.b.al_pa, mb, BIT_0);
6283                 if (rval != QLA_SUCCESS) {
6284                         return rval;
6285                 }
6286                 if (mb[0] == MBS_PORT_ID_USED) {
6287                         /*
6288                          * Device has another loop ID.  The firmware team
6289                          * recommends the driver perform an implicit login with
6290                          * the specified ID again. The ID we just used is save
6291                          * here so we return with an ID that can be tried by
6292                          * the next login.
6293                          */
6294                         retry++;
6295                         tmp_loopid = fcport->loop_id;
6296                         fcport->loop_id = mb[1];
6297
6298                         ql_dbg(ql_dbg_disc, vha, 0x2001,
6299                             "Fabric Login: port in use - next loop "
6300                             "id=0x%04x, port id= %02x%02x%02x.\n",
6301                             fcport->loop_id, fcport->d_id.b.domain,
6302                             fcport->d_id.b.area, fcport->d_id.b.al_pa);
6303
6304                 } else if (mb[0] == MBS_COMMAND_COMPLETE) {
6305                         /*
6306                          * Login succeeded.
6307                          */
6308                         if (retry) {
6309                                 /* A retry occurred before. */
6310                                 *next_loopid = tmp_loopid;
6311                         } else {
6312                                 /*
6313                                  * No retry occurred before. Just increment the
6314                                  * ID value for next login.
6315                                  */
6316                                 *next_loopid = (fcport->loop_id + 1);
6317                         }
6318
6319                         if (mb[1] & BIT_0) {
6320                                 fcport->port_type = FCT_INITIATOR;
6321                         } else {
6322                                 fcport->port_type = FCT_TARGET;
6323                                 if (mb[1] & BIT_1) {
6324                                         fcport->flags |= FCF_FCP2_DEVICE;
6325                                 }
6326                         }
6327
6328                         if (mb[10] & BIT_0)
6329                                 fcport->supported_classes |= FC_COS_CLASS2;
6330                         if (mb[10] & BIT_1)
6331                                 fcport->supported_classes |= FC_COS_CLASS3;
6332
6333                         if (IS_FWI2_CAPABLE(ha)) {
6334                                 if (mb[10] & BIT_7)
6335                                         fcport->flags |=
6336                                             FCF_CONF_COMP_SUPPORTED;
6337                         }
6338
6339                         rval = QLA_SUCCESS;
6340                         break;
6341                 } else if (mb[0] == MBS_LOOP_ID_USED) {
6342                         /*
6343                          * Loop ID already used, try next loop ID.
6344                          */
6345                         fcport->loop_id++;
6346                         rval = qla2x00_find_new_loop_id(vha, fcport);
6347                         if (rval != QLA_SUCCESS) {
6348                                 /* Ran out of loop IDs to use */
6349                                 break;
6350                         }
6351                 } else if (mb[0] == MBS_COMMAND_ERROR) {
6352                         /*
6353                          * Firmware possibly timed out during login. If NO
6354                          * retries are left to do then the device is declared
6355                          * dead.
6356                          */
6357                         *next_loopid = fcport->loop_id;
6358                         ha->isp_ops->fabric_logout(vha, fcport->loop_id,
6359                             fcport->d_id.b.domain, fcport->d_id.b.area,
6360                             fcport->d_id.b.al_pa);
6361                         qla2x00_mark_device_lost(vha, fcport, 1);
6362
6363                         rval = 1;
6364                         break;
6365                 } else {
6366                         /*
6367                          * unrecoverable / not handled error
6368                          */
6369                         ql_dbg(ql_dbg_disc, vha, 0x2002,
6370                             "Failed=%x port_id=%02x%02x%02x loop_id=%x "
6371                             "jiffies=%lx.\n", mb[0], fcport->d_id.b.domain,
6372                             fcport->d_id.b.area, fcport->d_id.b.al_pa,
6373                             fcport->loop_id, jiffies);
6374
6375                         *next_loopid = fcport->loop_id;
6376                         ha->isp_ops->fabric_logout(vha, fcport->loop_id,
6377                             fcport->d_id.b.domain, fcport->d_id.b.area,
6378                             fcport->d_id.b.al_pa);
6379                         qla2x00_clear_loop_id(fcport);
6380                         fcport->login_retry = 0;
6381
6382                         rval = 3;
6383                         break;
6384                 }
6385         }
6386
6387         return (rval);
6388 }
6389
6390 /*
6391  * qla2x00_local_device_login
6392  *      Issue local device login command.
6393  *
6394  * Input:
6395  *      ha = adapter block pointer.
6396  *      loop_id = loop id of device to login to.
6397  *
6398  * Returns (Where's the #define!!!!):
6399  *      0 - Login successfully
6400  *      1 - Login failed
6401  *      3 - Fatal error
6402  */
6403 int
6404 qla2x00_local_device_login(scsi_qla_host_t *vha, fc_port_t *fcport)
6405 {
6406         int             rval;
6407         uint16_t        mb[MAILBOX_REGISTER_COUNT];
6408
6409         memset(mb, 0, sizeof(mb));
6410         rval = qla2x00_login_local_device(vha, fcport, mb, BIT_0);
6411         if (rval == QLA_SUCCESS) {
6412                 /* Interrogate mailbox registers for any errors */
6413                 if (mb[0] == MBS_COMMAND_ERROR)
6414                         rval = 1;
6415                 else if (mb[0] == MBS_COMMAND_PARAMETER_ERROR)
6416                         /* device not in PCB table */
6417                         rval = 3;
6418         }
6419
6420         return (rval);
6421 }
6422
6423 /*
6424  *  qla2x00_loop_resync
6425  *      Resync with fibre channel devices.
6426  *
6427  * Input:
6428  *      ha = adapter block pointer.
6429  *
6430  * Returns:
6431  *      0 = success
6432  */
6433 int
6434 qla2x00_loop_resync(scsi_qla_host_t *vha)
6435 {
6436         int rval = QLA_SUCCESS;
6437         uint32_t wait_time;
6438
6439         clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
6440         if (vha->flags.online) {
6441                 if (!(rval = qla2x00_fw_ready(vha))) {
6442                         /* Wait at most MAX_TARGET RSCNs for a stable link. */
6443                         wait_time = 256;
6444                         do {
6445                                 if (!IS_QLAFX00(vha->hw)) {
6446                                         /*
6447                                          * Issue a marker after FW becomes
6448                                          * ready.
6449                                          */
6450                                         qla2x00_marker(vha, vha->hw->base_qpair,
6451                                             0, 0, MK_SYNC_ALL);
6452                                         vha->marker_needed = 0;
6453                                 }
6454
6455                                 /* Remap devices on Loop. */
6456                                 clear_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
6457
6458                                 if (IS_QLAFX00(vha->hw))
6459                                         qlafx00_configure_devices(vha);
6460                                 else
6461                                         qla2x00_configure_loop(vha);
6462
6463                                 wait_time--;
6464                         } while (!atomic_read(&vha->loop_down_timer) &&
6465                                 !(test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
6466                                 && wait_time && (test_bit(LOOP_RESYNC_NEEDED,
6467                                 &vha->dpc_flags)));
6468                 }
6469         }
6470
6471         if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags))
6472                 return (QLA_FUNCTION_FAILED);
6473
6474         if (rval)
6475                 ql_dbg(ql_dbg_disc, vha, 0x206c,
6476                     "%s *** FAILED ***.\n", __func__);
6477
6478         return (rval);
6479 }
6480
6481 /*
6482 * qla2x00_perform_loop_resync
6483 * Description: This function will set the appropriate flags and call
6484 *              qla2x00_loop_resync. If successful loop will be resynced
6485 * Arguments : scsi_qla_host_t pointer
6486 * returm    : Success or Failure
6487 */
6488
6489 int qla2x00_perform_loop_resync(scsi_qla_host_t *ha)
6490 {
6491         int32_t rval = 0;
6492
6493         if (!test_and_set_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags)) {
6494                 /*Configure the flags so that resync happens properly*/
6495                 atomic_set(&ha->loop_down_timer, 0);
6496                 if (!(ha->device_flags & DFLG_NO_CABLE)) {
6497                         atomic_set(&ha->loop_state, LOOP_UP);
6498                         set_bit(LOCAL_LOOP_UPDATE, &ha->dpc_flags);
6499                         set_bit(REGISTER_FC4_NEEDED, &ha->dpc_flags);
6500                         set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
6501
6502                         rval = qla2x00_loop_resync(ha);
6503                 } else
6504                         atomic_set(&ha->loop_state, LOOP_DEAD);
6505
6506                 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
6507         }
6508
6509         return rval;
6510 }
6511
6512 void
6513 qla2x00_update_fcports(scsi_qla_host_t *base_vha)
6514 {
6515         fc_port_t *fcport;
6516         struct scsi_qla_host *vha;
6517         struct qla_hw_data *ha = base_vha->hw;
6518         unsigned long flags;
6519
6520         spin_lock_irqsave(&ha->vport_slock, flags);
6521         /* Go with deferred removal of rport references. */
6522         list_for_each_entry(vha, &base_vha->hw->vp_list, list) {
6523                 atomic_inc(&vha->vref_count);
6524                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
6525                         if (fcport->drport &&
6526                             atomic_read(&fcport->state) != FCS_UNCONFIGURED) {
6527                                 spin_unlock_irqrestore(&ha->vport_slock, flags);
6528                                 qla2x00_rport_del(fcport);
6529
6530                                 spin_lock_irqsave(&ha->vport_slock, flags);
6531                         }
6532                 }
6533                 atomic_dec(&vha->vref_count);
6534                 wake_up(&vha->vref_waitq);
6535         }
6536         spin_unlock_irqrestore(&ha->vport_slock, flags);
6537 }
6538
6539 /* Assumes idc_lock always held on entry */
6540 void
6541 qla83xx_reset_ownership(scsi_qla_host_t *vha)
6542 {
6543         struct qla_hw_data *ha = vha->hw;
6544         uint32_t drv_presence, drv_presence_mask;
6545         uint32_t dev_part_info1, dev_part_info2, class_type;
6546         uint32_t class_type_mask = 0x3;
6547         uint16_t fcoe_other_function = 0xffff, i;
6548
6549         if (IS_QLA8044(ha)) {
6550                 drv_presence = qla8044_rd_direct(vha,
6551                     QLA8044_CRB_DRV_ACTIVE_INDEX);
6552                 dev_part_info1 = qla8044_rd_direct(vha,
6553                     QLA8044_CRB_DEV_PART_INFO_INDEX);
6554                 dev_part_info2 = qla8044_rd_direct(vha,
6555                     QLA8044_CRB_DEV_PART_INFO2);
6556         } else {
6557                 qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6558                 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO1, &dev_part_info1);
6559                 qla83xx_rd_reg(vha, QLA83XX_DEV_PARTINFO2, &dev_part_info2);
6560         }
6561         for (i = 0; i < 8; i++) {
6562                 class_type = ((dev_part_info1 >> (i * 4)) & class_type_mask);
6563                 if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
6564                     (i != ha->portnum)) {
6565                         fcoe_other_function = i;
6566                         break;
6567                 }
6568         }
6569         if (fcoe_other_function == 0xffff) {
6570                 for (i = 0; i < 8; i++) {
6571                         class_type = ((dev_part_info2 >> (i * 4)) &
6572                             class_type_mask);
6573                         if ((class_type == QLA83XX_CLASS_TYPE_FCOE) &&
6574                             ((i + 8) != ha->portnum)) {
6575                                 fcoe_other_function = i + 8;
6576                                 break;
6577                         }
6578                 }
6579         }
6580         /*
6581          * Prepare drv-presence mask based on fcoe functions present.
6582          * However consider only valid physical fcoe function numbers (0-15).
6583          */
6584         drv_presence_mask = ~((1 << (ha->portnum)) |
6585                         ((fcoe_other_function == 0xffff) ?
6586                          0 : (1 << (fcoe_other_function))));
6587
6588         /* We are the reset owner iff:
6589          *    - No other protocol drivers present.
6590          *    - This is the lowest among fcoe functions. */
6591         if (!(drv_presence & drv_presence_mask) &&
6592                         (ha->portnum < fcoe_other_function)) {
6593                 ql_dbg(ql_dbg_p3p, vha, 0xb07f,
6594                     "This host is Reset owner.\n");
6595                 ha->flags.nic_core_reset_owner = 1;
6596         }
6597 }
6598
6599 static int
6600 __qla83xx_set_drv_ack(scsi_qla_host_t *vha)
6601 {
6602         int rval = QLA_SUCCESS;
6603         struct qla_hw_data *ha = vha->hw;
6604         uint32_t drv_ack;
6605
6606         rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
6607         if (rval == QLA_SUCCESS) {
6608                 drv_ack |= (1 << ha->portnum);
6609                 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
6610         }
6611
6612         return rval;
6613 }
6614
6615 static int
6616 __qla83xx_clear_drv_ack(scsi_qla_host_t *vha)
6617 {
6618         int rval = QLA_SUCCESS;
6619         struct qla_hw_data *ha = vha->hw;
6620         uint32_t drv_ack;
6621
6622         rval = qla83xx_rd_reg(vha, QLA83XX_IDC_DRIVER_ACK, &drv_ack);
6623         if (rval == QLA_SUCCESS) {
6624                 drv_ack &= ~(1 << ha->portnum);
6625                 rval = qla83xx_wr_reg(vha, QLA83XX_IDC_DRIVER_ACK, drv_ack);
6626         }
6627
6628         return rval;
6629 }
6630
6631 static const char *
6632 qla83xx_dev_state_to_string(uint32_t dev_state)
6633 {
6634         switch (dev_state) {
6635         case QLA8XXX_DEV_COLD:
6636                 return "COLD/RE-INIT";
6637         case QLA8XXX_DEV_INITIALIZING:
6638                 return "INITIALIZING";
6639         case QLA8XXX_DEV_READY:
6640                 return "READY";
6641         case QLA8XXX_DEV_NEED_RESET:
6642                 return "NEED RESET";
6643         case QLA8XXX_DEV_NEED_QUIESCENT:
6644                 return "NEED QUIESCENT";
6645         case QLA8XXX_DEV_FAILED:
6646                 return "FAILED";
6647         case QLA8XXX_DEV_QUIESCENT:
6648                 return "QUIESCENT";
6649         default:
6650                 return "Unknown";
6651         }
6652 }
6653
6654 /* Assumes idc-lock always held on entry */
6655 void
6656 qla83xx_idc_audit(scsi_qla_host_t *vha, int audit_type)
6657 {
6658         struct qla_hw_data *ha = vha->hw;
6659         uint32_t idc_audit_reg = 0, duration_secs = 0;
6660
6661         switch (audit_type) {
6662         case IDC_AUDIT_TIMESTAMP:
6663                 ha->idc_audit_ts = (jiffies_to_msecs(jiffies) / 1000);
6664                 idc_audit_reg = (ha->portnum) |
6665                     (IDC_AUDIT_TIMESTAMP << 7) | (ha->idc_audit_ts << 8);
6666                 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6667                 break;
6668
6669         case IDC_AUDIT_COMPLETION:
6670                 duration_secs = ((jiffies_to_msecs(jiffies) -
6671                     jiffies_to_msecs(ha->idc_audit_ts)) / 1000);
6672                 idc_audit_reg = (ha->portnum) |
6673                     (IDC_AUDIT_COMPLETION << 7) | (duration_secs << 8);
6674                 qla83xx_wr_reg(vha, QLA83XX_IDC_AUDIT, idc_audit_reg);
6675                 break;
6676
6677         default:
6678                 ql_log(ql_log_warn, vha, 0xb078,
6679                     "Invalid audit type specified.\n");
6680                 break;
6681         }
6682 }
6683
6684 /* Assumes idc_lock always held on entry */
6685 static int
6686 qla83xx_initiating_reset(scsi_qla_host_t *vha)
6687 {
6688         struct qla_hw_data *ha = vha->hw;
6689         uint32_t  idc_control, dev_state;
6690
6691         __qla83xx_get_idc_control(vha, &idc_control);
6692         if ((idc_control & QLA83XX_IDC_RESET_DISABLED)) {
6693                 ql_log(ql_log_info, vha, 0xb080,
6694                     "NIC Core reset has been disabled. idc-control=0x%x\n",
6695                     idc_control);
6696                 return QLA_FUNCTION_FAILED;
6697         }
6698
6699         /* Set NEED-RESET iff in READY state and we are the reset-owner */
6700         qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6701         if (ha->flags.nic_core_reset_owner && dev_state == QLA8XXX_DEV_READY) {
6702                 qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
6703                     QLA8XXX_DEV_NEED_RESET);
6704                 ql_log(ql_log_info, vha, 0xb056, "HW State: NEED RESET.\n");
6705                 qla83xx_idc_audit(vha, IDC_AUDIT_TIMESTAMP);
6706         } else {
6707                 const char *state = qla83xx_dev_state_to_string(dev_state);
6708
6709                 ql_log(ql_log_info, vha, 0xb057, "HW State: %s.\n", state);
6710
6711                 /* SV: XXX: Is timeout required here? */
6712                 /* Wait for IDC state change READY -> NEED_RESET */
6713                 while (dev_state == QLA8XXX_DEV_READY) {
6714                         qla83xx_idc_unlock(vha, 0);
6715                         msleep(200);
6716                         qla83xx_idc_lock(vha, 0);
6717                         qla83xx_rd_reg(vha, QLA83XX_IDC_DEV_STATE, &dev_state);
6718                 }
6719         }
6720
6721         /* Send IDC ack by writing to drv-ack register */
6722         __qla83xx_set_drv_ack(vha);
6723
6724         return QLA_SUCCESS;
6725 }
6726
6727 int
6728 __qla83xx_set_idc_control(scsi_qla_host_t *vha, uint32_t idc_control)
6729 {
6730         return qla83xx_wr_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6731 }
6732
6733 int
6734 __qla83xx_get_idc_control(scsi_qla_host_t *vha, uint32_t *idc_control)
6735 {
6736         return qla83xx_rd_reg(vha, QLA83XX_IDC_CONTROL, idc_control);
6737 }
6738
6739 static int
6740 qla83xx_check_driver_presence(scsi_qla_host_t *vha)
6741 {
6742         uint32_t drv_presence = 0;
6743         struct qla_hw_data *ha = vha->hw;
6744
6745         qla83xx_rd_reg(vha, QLA83XX_IDC_DRV_PRESENCE, &drv_presence);
6746         if (drv_presence & (1 << ha->portnum))
6747                 return QLA_SUCCESS;
6748         else
6749                 return QLA_TEST_FAILED;
6750 }
6751
6752 int
6753 qla83xx_nic_core_reset(scsi_qla_host_t *vha)
6754 {
6755         int rval = QLA_SUCCESS;
6756         struct qla_hw_data *ha = vha->hw;
6757
6758         ql_dbg(ql_dbg_p3p, vha, 0xb058,
6759             "Entered  %s().\n", __func__);
6760
6761         if (vha->device_flags & DFLG_DEV_FAILED) {
6762                 ql_log(ql_log_warn, vha, 0xb059,
6763                     "Device in unrecoverable FAILED state.\n");
6764                 return QLA_FUNCTION_FAILED;
6765         }
6766
6767         qla83xx_idc_lock(vha, 0);
6768
6769         if (qla83xx_check_driver_presence(vha) != QLA_SUCCESS) {
6770                 ql_log(ql_log_warn, vha, 0xb05a,
6771                     "Function=0x%x has been removed from IDC participation.\n",
6772                     ha->portnum);
6773                 rval = QLA_FUNCTION_FAILED;
6774                 goto exit;
6775         }
6776
6777         qla83xx_reset_ownership(vha);
6778
6779         rval = qla83xx_initiating_reset(vha);
6780
6781         /*
6782          * Perform reset if we are the reset-owner,
6783          * else wait till IDC state changes to READY/FAILED.
6784          */
6785         if (rval == QLA_SUCCESS) {
6786                 rval = qla83xx_idc_state_handler(vha);
6787
6788                 if (rval == QLA_SUCCESS)
6789                         ha->flags.nic_core_hung = 0;
6790                 __qla83xx_clear_drv_ack(vha);
6791         }
6792
6793 exit:
6794         qla83xx_idc_unlock(vha, 0);
6795
6796         ql_dbg(ql_dbg_p3p, vha, 0xb05b, "Exiting %s.\n", __func__);
6797
6798         return rval;
6799 }
6800
6801 int
6802 qla2xxx_mctp_dump(scsi_qla_host_t *vha)
6803 {
6804         struct qla_hw_data *ha = vha->hw;
6805         int rval = QLA_FUNCTION_FAILED;
6806
6807         if (!IS_MCTP_CAPABLE(ha)) {
6808                 /* This message can be removed from the final version */
6809                 ql_log(ql_log_info, vha, 0x506d,
6810                     "This board is not MCTP capable\n");
6811                 return rval;
6812         }
6813
6814         if (!ha->mctp_dump) {
6815                 ha->mctp_dump = dma_alloc_coherent(&ha->pdev->dev,
6816                     MCTP_DUMP_SIZE, &ha->mctp_dump_dma, GFP_KERNEL);
6817
6818                 if (!ha->mctp_dump) {
6819                         ql_log(ql_log_warn, vha, 0x506e,
6820                             "Failed to allocate memory for mctp dump\n");
6821                         return rval;
6822                 }
6823         }
6824
6825 #define MCTP_DUMP_STR_ADDR      0x00000000
6826         rval = qla2x00_dump_mctp_data(vha, ha->mctp_dump_dma,
6827             MCTP_DUMP_STR_ADDR, MCTP_DUMP_SIZE/4);
6828         if (rval != QLA_SUCCESS) {
6829                 ql_log(ql_log_warn, vha, 0x506f,
6830                     "Failed to capture mctp dump\n");
6831         } else {
6832                 ql_log(ql_log_info, vha, 0x5070,
6833                     "Mctp dump capture for host (%ld/%p).\n",
6834                     vha->host_no, ha->mctp_dump);
6835                 ha->mctp_dumped = 1;
6836         }
6837
6838         if (!ha->flags.nic_core_reset_hdlr_active && !ha->portnum) {
6839                 ha->flags.nic_core_reset_hdlr_active = 1;
6840                 rval = qla83xx_restart_nic_firmware(vha);
6841                 if (rval)
6842                         /* NIC Core reset failed. */
6843                         ql_log(ql_log_warn, vha, 0x5071,
6844                             "Failed to restart nic firmware\n");
6845                 else
6846                         ql_dbg(ql_dbg_p3p, vha, 0xb084,
6847                             "Restarted NIC firmware successfully.\n");
6848                 ha->flags.nic_core_reset_hdlr_active = 0;
6849         }
6850
6851         return rval;
6852
6853 }
6854
6855 /*
6856 * qla2x00_quiesce_io
6857 * Description: This function will block the new I/Os
6858 *              Its not aborting any I/Os as context
6859 *              is not destroyed during quiescence
6860 * Arguments: scsi_qla_host_t
6861 * return   : void
6862 */
6863 void
6864 qla2x00_quiesce_io(scsi_qla_host_t *vha)
6865 {
6866         struct qla_hw_data *ha = vha->hw;
6867         struct scsi_qla_host *vp;
6868
6869         ql_dbg(ql_dbg_dpc, vha, 0x401d,
6870             "Quiescing I/O - ha=%p.\n", ha);
6871
6872         atomic_set(&ha->loop_down_timer, LOOP_DOWN_TIME);
6873         if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6874                 atomic_set(&vha->loop_state, LOOP_DOWN);
6875                 qla2x00_mark_all_devices_lost(vha);
6876                 list_for_each_entry(vp, &ha->vp_list, list)
6877                         qla2x00_mark_all_devices_lost(vp);
6878         } else {
6879                 if (!atomic_read(&vha->loop_down_timer))
6880                         atomic_set(&vha->loop_down_timer,
6881                                         LOOP_DOWN_TIME);
6882         }
6883         /* Wait for pending cmds to complete */
6884         WARN_ON_ONCE(qla2x00_eh_wait_for_pending_commands(vha, 0, 0, WAIT_HOST)
6885                      != QLA_SUCCESS);
6886 }
6887
6888 void
6889 qla2x00_abort_isp_cleanup(scsi_qla_host_t *vha)
6890 {
6891         struct qla_hw_data *ha = vha->hw;
6892         struct scsi_qla_host *vp;
6893         unsigned long flags;
6894         fc_port_t *fcport;
6895         u16 i;
6896
6897         /* For ISP82XX, driver waits for completion of the commands.
6898          * online flag should be set.
6899          */
6900         if (!(IS_P3P_TYPE(ha)))
6901                 vha->flags.online = 0;
6902         ha->flags.chip_reset_done = 0;
6903         clear_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
6904         vha->qla_stats.total_isp_aborts++;
6905
6906         ql_log(ql_log_info, vha, 0x00af,
6907             "Performing ISP error recovery - ha=%p.\n", ha);
6908
6909         ha->flags.purge_mbox = 1;
6910         /* For ISP82XX, reset_chip is just disabling interrupts.
6911          * Driver waits for the completion of the commands.
6912          * the interrupts need to be enabled.
6913          */
6914         if (!(IS_P3P_TYPE(ha)))
6915                 ha->isp_ops->reset_chip(vha);
6916
6917         ha->link_data_rate = PORT_SPEED_UNKNOWN;
6918         SAVE_TOPO(ha);
6919         ha->flags.rida_fmt2 = 0;
6920         ha->flags.n2n_ae = 0;
6921         ha->flags.lip_ae = 0;
6922         ha->current_topology = 0;
6923         QLA_FW_STOPPED(ha);
6924         ha->flags.fw_init_done = 0;
6925         ha->chip_reset++;
6926         ha->base_qpair->chip_reset = ha->chip_reset;
6927         for (i = 0; i < ha->max_qpairs; i++) {
6928                 if (ha->queue_pair_map[i])
6929                         ha->queue_pair_map[i]->chip_reset =
6930                                 ha->base_qpair->chip_reset;
6931         }
6932
6933         /* purge MBox commands */
6934         spin_lock_irqsave(&ha->hardware_lock, flags);
6935         if (test_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags)) {
6936                 clear_bit(MBX_INTR_WAIT, &ha->mbx_cmd_flags);
6937                 complete(&ha->mbx_intr_comp);
6938         }
6939         spin_unlock_irqrestore(&ha->hardware_lock, flags);
6940
6941         i = 0;
6942         while (atomic_read(&ha->num_pend_mbx_stage2) ||
6943             atomic_read(&ha->num_pend_mbx_stage1)) {
6944                 msleep(20);
6945                 i++;
6946                 if (i > 50)
6947                         break;
6948         }
6949         ha->flags.purge_mbox = 0;
6950
6951         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
6952         if (atomic_read(&vha->loop_state) != LOOP_DOWN) {
6953                 atomic_set(&vha->loop_state, LOOP_DOWN);
6954                 qla2x00_mark_all_devices_lost(vha);
6955
6956                 spin_lock_irqsave(&ha->vport_slock, flags);
6957                 list_for_each_entry(vp, &ha->vp_list, list) {
6958                         atomic_inc(&vp->vref_count);
6959                         spin_unlock_irqrestore(&ha->vport_slock, flags);
6960
6961                         qla2x00_mark_all_devices_lost(vp);
6962
6963                         spin_lock_irqsave(&ha->vport_slock, flags);
6964                         atomic_dec(&vp->vref_count);
6965                 }
6966                 spin_unlock_irqrestore(&ha->vport_slock, flags);
6967         } else {
6968                 if (!atomic_read(&vha->loop_down_timer))
6969                         atomic_set(&vha->loop_down_timer,
6970                             LOOP_DOWN_TIME);
6971         }
6972
6973         /* Clear all async request states across all VPs. */
6974         list_for_each_entry(fcport, &vha->vp_fcports, list) {
6975                 fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6976                 fcport->scan_state = 0;
6977         }
6978         spin_lock_irqsave(&ha->vport_slock, flags);
6979         list_for_each_entry(vp, &ha->vp_list, list) {
6980                 atomic_inc(&vp->vref_count);
6981                 spin_unlock_irqrestore(&ha->vport_slock, flags);
6982
6983                 list_for_each_entry(fcport, &vp->vp_fcports, list)
6984                         fcport->flags &= ~(FCF_LOGIN_NEEDED | FCF_ASYNC_SENT);
6985
6986                 spin_lock_irqsave(&ha->vport_slock, flags);
6987                 atomic_dec(&vp->vref_count);
6988         }
6989         spin_unlock_irqrestore(&ha->vport_slock, flags);
6990
6991         /* Make sure for ISP 82XX IO DMA is complete */
6992         if (IS_P3P_TYPE(ha)) {
6993                 qla82xx_chip_reset_cleanup(vha);
6994                 ql_log(ql_log_info, vha, 0x00b4,
6995                        "Done chip reset cleanup.\n");
6996
6997                 /* Done waiting for pending commands. Reset online flag */
6998                 vha->flags.online = 0;
6999         }
7000
7001         /* Requeue all commands in outstanding command list. */
7002         qla2x00_abort_all_cmds(vha, DID_RESET << 16);
7003         /* memory barrier */
7004         wmb();
7005 }
7006
7007 /*
7008 *  qla2x00_abort_isp
7009 *      Resets ISP and aborts all outstanding commands.
7010 *
7011 * Input:
7012 *      ha           = adapter block pointer.
7013 *
7014 * Returns:
7015 *      0 = success
7016 */
7017 int
7018 qla2x00_abort_isp(scsi_qla_host_t *vha)
7019 {
7020         uint8_t        status = 0;
7021         struct qla_hw_data *ha = vha->hw;
7022         struct scsi_qla_host *vp;
7023         struct req_que *req = ha->req_q_map[0];
7024         unsigned long flags;
7025
7026         if (vha->flags.online) {
7027                 qla2x00_abort_isp_cleanup(vha);
7028
7029                 if (qla2x00_isp_reg_stat(ha)) {
7030                         ql_log(ql_log_info, vha, 0x803f,
7031                                "ISP Abort - ISP reg disconnect, exiting.\n");
7032                         return status;
7033                 }
7034
7035                 if (test_and_clear_bit(ISP_ABORT_TO_ROM, &vha->dpc_flags)) {
7036                         ha->flags.chip_reset_done = 1;
7037                         vha->flags.online = 1;
7038                         status = 0;
7039                         clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
7040                         return status;
7041                 }
7042
7043                 if (IS_QLA8031(ha)) {
7044                         ql_dbg(ql_dbg_p3p, vha, 0xb05c,
7045                             "Clearing fcoe driver presence.\n");
7046                         if (qla83xx_clear_drv_presence(vha) != QLA_SUCCESS)
7047                                 ql_dbg(ql_dbg_p3p, vha, 0xb073,
7048                                     "Error while clearing DRV-Presence.\n");
7049                 }
7050
7051                 if (unlikely(pci_channel_offline(ha->pdev) &&
7052                     ha->flags.pci_channel_io_perm_failure)) {
7053                         clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
7054                         status = 0;
7055                         return status;
7056                 }
7057
7058                 switch (vha->qlini_mode) {
7059                 case QLA2XXX_INI_MODE_DISABLED:
7060                         if (!qla_tgt_mode_enabled(vha))
7061                                 return 0;
7062                         break;
7063                 case QLA2XXX_INI_MODE_DUAL:
7064                         if (!qla_dual_mode_enabled(vha) &&
7065                             !qla_ini_mode_enabled(vha))
7066                                 return 0;
7067                         break;
7068                 case QLA2XXX_INI_MODE_ENABLED:
7069                 default:
7070                         break;
7071                 }
7072
7073                 ha->isp_ops->get_flash_version(vha, req->ring);
7074
7075                 if (qla2x00_isp_reg_stat(ha)) {
7076                         ql_log(ql_log_info, vha, 0x803f,
7077                                "ISP Abort - ISP reg disconnect pre nvram config, exiting.\n");
7078                         return status;
7079                 }
7080                 ha->isp_ops->nvram_config(vha);
7081
7082                 if (qla2x00_isp_reg_stat(ha)) {
7083                         ql_log(ql_log_info, vha, 0x803f,
7084                                "ISP Abort - ISP reg disconnect post nvmram config, exiting.\n");
7085                         return status;
7086                 }
7087                 if (!qla2x00_restart_isp(vha)) {
7088                         clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
7089
7090                         if (!atomic_read(&vha->loop_down_timer)) {
7091                                 /*
7092                                  * Issue marker command only when we are going
7093                                  * to start the I/O .
7094                                  */
7095                                 vha->marker_needed = 1;
7096                         }
7097
7098                         vha->flags.online = 1;
7099
7100                         ha->isp_ops->enable_intrs(ha);
7101
7102                         ha->isp_abort_cnt = 0;
7103                         clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
7104
7105                         if (IS_QLA81XX(ha) || IS_QLA8031(ha))
7106                                 qla2x00_get_fw_version(vha);
7107
7108                 } else {        /* failed the ISP abort */
7109                         vha->flags.online = 1;
7110                         if (test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
7111                                 if (ha->isp_abort_cnt == 0) {
7112                                         ql_log(ql_log_fatal, vha, 0x8035,
7113                                             "ISP error recover failed - "
7114                                             "board disabled.\n");
7115                                         /*
7116                                          * The next call disables the board
7117                                          * completely.
7118                                          */
7119                                         qla2x00_abort_isp_cleanup(vha);
7120                                         vha->flags.online = 0;
7121                                         clear_bit(ISP_ABORT_RETRY,
7122                                             &vha->dpc_flags);
7123                                         status = 0;
7124                                 } else { /* schedule another ISP abort */
7125                                         ha->isp_abort_cnt--;
7126                                         ql_dbg(ql_dbg_taskm, vha, 0x8020,
7127                                             "ISP abort - retry remaining %d.\n",
7128                                             ha->isp_abort_cnt);
7129                                         status = 1;
7130                                 }
7131                         } else {
7132                                 ha->isp_abort_cnt = MAX_RETRIES_OF_ISP_ABORT;
7133                                 ql_dbg(ql_dbg_taskm, vha, 0x8021,
7134                                     "ISP error recovery - retrying (%d) "
7135                                     "more times.\n", ha->isp_abort_cnt);
7136                                 set_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
7137                                 status = 1;
7138                         }
7139                 }
7140
7141         }
7142
7143         if (!status) {
7144                 ql_dbg(ql_dbg_taskm, vha, 0x8022, "%s succeeded.\n", __func__);
7145                 qla2x00_configure_hba(vha);
7146                 spin_lock_irqsave(&ha->vport_slock, flags);
7147                 list_for_each_entry(vp, &ha->vp_list, list) {
7148                         if (vp->vp_idx) {
7149                                 atomic_inc(&vp->vref_count);
7150                                 spin_unlock_irqrestore(&ha->vport_slock, flags);
7151
7152                                 qla2x00_vp_abort_isp(vp);
7153
7154                                 spin_lock_irqsave(&ha->vport_slock, flags);
7155                                 atomic_dec(&vp->vref_count);
7156                         }
7157                 }
7158                 spin_unlock_irqrestore(&ha->vport_slock, flags);
7159
7160                 if (IS_QLA8031(ha)) {
7161                         ql_dbg(ql_dbg_p3p, vha, 0xb05d,
7162                             "Setting back fcoe driver presence.\n");
7163                         if (qla83xx_set_drv_presence(vha) != QLA_SUCCESS)
7164                                 ql_dbg(ql_dbg_p3p, vha, 0xb074,
7165                                     "Error while setting DRV-Presence.\n");
7166                 }
7167         } else {
7168                 ql_log(ql_log_warn, vha, 0x8023, "%s **** FAILED ****.\n",
7169                        __func__);
7170         }
7171
7172         return(status);
7173 }
7174
7175 /*
7176 *  qla2x00_restart_isp
7177 *      restarts the ISP after a reset
7178 *
7179 * Input:
7180 *      ha = adapter block pointer.
7181 *
7182 * Returns:
7183 *      0 = success
7184 */
7185 static int
7186 qla2x00_restart_isp(scsi_qla_host_t *vha)
7187 {
7188         int status;
7189         struct qla_hw_data *ha = vha->hw;
7190
7191         /* If firmware needs to be loaded */
7192         if (qla2x00_isp_firmware(vha)) {
7193                 vha->flags.online = 0;
7194                 status = ha->isp_ops->chip_diag(vha);
7195                 if (status)
7196                         return status;
7197                 status = qla2x00_setup_chip(vha);
7198                 if (status)
7199                         return status;
7200         }
7201
7202         status = qla2x00_init_rings(vha);
7203         if (status)
7204                 return status;
7205
7206         clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
7207         ha->flags.chip_reset_done = 1;
7208
7209         /* Initialize the queues in use */
7210         qla25xx_init_queues(ha);
7211
7212         status = qla2x00_fw_ready(vha);
7213         if (status) {
7214                 /* if no cable then assume it's good */
7215                 return vha->device_flags & DFLG_NO_CABLE ? 0 : status;
7216         }
7217
7218         /* Issue a marker after FW becomes ready. */
7219         qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL);
7220         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
7221
7222         return 0;
7223 }
7224
7225 static int
7226 qla25xx_init_queues(struct qla_hw_data *ha)
7227 {
7228         struct rsp_que *rsp = NULL;
7229         struct req_que *req = NULL;
7230         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
7231         int ret = -1;
7232         int i;
7233
7234         for (i = 1; i < ha->max_rsp_queues; i++) {
7235                 rsp = ha->rsp_q_map[i];
7236                 if (rsp && test_bit(i, ha->rsp_qid_map)) {
7237                         rsp->options &= ~BIT_0;
7238                         ret = qla25xx_init_rsp_que(base_vha, rsp);
7239                         if (ret != QLA_SUCCESS)
7240                                 ql_dbg(ql_dbg_init, base_vha, 0x00ff,
7241                                     "%s Rsp que: %d init failed.\n",
7242                                     __func__, rsp->id);
7243                         else
7244                                 ql_dbg(ql_dbg_init, base_vha, 0x0100,
7245                                     "%s Rsp que: %d inited.\n",
7246                                     __func__, rsp->id);
7247                 }
7248         }
7249         for (i = 1; i < ha->max_req_queues; i++) {
7250                 req = ha->req_q_map[i];
7251                 if (req && test_bit(i, ha->req_qid_map)) {
7252                         /* Clear outstanding commands array. */
7253                         req->options &= ~BIT_0;
7254                         ret = qla25xx_init_req_que(base_vha, req);
7255                         if (ret != QLA_SUCCESS)
7256                                 ql_dbg(ql_dbg_init, base_vha, 0x0101,
7257                                     "%s Req que: %d init failed.\n",
7258                                     __func__, req->id);
7259                         else
7260                                 ql_dbg(ql_dbg_init, base_vha, 0x0102,
7261                                     "%s Req que: %d inited.\n",
7262                                     __func__, req->id);
7263                 }
7264         }
7265         return ret;
7266 }
7267
7268 /*
7269 * qla2x00_reset_adapter
7270 *      Reset adapter.
7271 *
7272 * Input:
7273 *      ha = adapter block pointer.
7274 */
7275 int
7276 qla2x00_reset_adapter(scsi_qla_host_t *vha)
7277 {
7278         unsigned long flags = 0;
7279         struct qla_hw_data *ha = vha->hw;
7280         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
7281
7282         vha->flags.online = 0;
7283         ha->isp_ops->disable_intrs(ha);
7284
7285         spin_lock_irqsave(&ha->hardware_lock, flags);
7286         wrt_reg_word(&reg->hccr, HCCR_RESET_RISC);
7287         rd_reg_word(&reg->hccr);                        /* PCI Posting. */
7288         wrt_reg_word(&reg->hccr, HCCR_RELEASE_RISC);
7289         rd_reg_word(&reg->hccr);                        /* PCI Posting. */
7290         spin_unlock_irqrestore(&ha->hardware_lock, flags);
7291
7292         return QLA_SUCCESS;
7293 }
7294
7295 int
7296 qla24xx_reset_adapter(scsi_qla_host_t *vha)
7297 {
7298         unsigned long flags = 0;
7299         struct qla_hw_data *ha = vha->hw;
7300         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
7301
7302         if (IS_P3P_TYPE(ha))
7303                 return QLA_SUCCESS;
7304
7305         vha->flags.online = 0;
7306         ha->isp_ops->disable_intrs(ha);
7307
7308         spin_lock_irqsave(&ha->hardware_lock, flags);
7309         wrt_reg_dword(&reg->hccr, HCCRX_SET_RISC_RESET);
7310         rd_reg_dword(&reg->hccr);
7311         wrt_reg_dword(&reg->hccr, HCCRX_REL_RISC_PAUSE);
7312         rd_reg_dword(&reg->hccr);
7313         spin_unlock_irqrestore(&ha->hardware_lock, flags);
7314
7315         if (IS_NOPOLLING_TYPE(ha))
7316                 ha->isp_ops->enable_intrs(ha);
7317
7318         return QLA_SUCCESS;
7319 }
7320
7321 /* On sparc systems, obtain port and node WWN from firmware
7322  * properties.
7323  */
7324 static void qla24xx_nvram_wwn_from_ofw(scsi_qla_host_t *vha,
7325         struct nvram_24xx *nv)
7326 {
7327 #ifdef CONFIG_SPARC
7328         struct qla_hw_data *ha = vha->hw;
7329         struct pci_dev *pdev = ha->pdev;
7330         struct device_node *dp = pci_device_to_OF_node(pdev);
7331         const u8 *val;
7332         int len;
7333
7334         val = of_get_property(dp, "port-wwn", &len);
7335         if (val && len >= WWN_SIZE)
7336                 memcpy(nv->port_name, val, WWN_SIZE);
7337
7338         val = of_get_property(dp, "node-wwn", &len);
7339         if (val && len >= WWN_SIZE)
7340                 memcpy(nv->node_name, val, WWN_SIZE);
7341 #endif
7342 }
7343
7344 int
7345 qla24xx_nvram_config(scsi_qla_host_t *vha)
7346 {
7347         int   rval;
7348         struct init_cb_24xx *icb;
7349         struct nvram_24xx *nv;
7350         __le32 *dptr;
7351         uint8_t  *dptr1, *dptr2;
7352         uint32_t chksum;
7353         uint16_t cnt;
7354         struct qla_hw_data *ha = vha->hw;
7355
7356         rval = QLA_SUCCESS;
7357         icb = (struct init_cb_24xx *)ha->init_cb;
7358         nv = ha->nvram;
7359
7360         /* Determine NVRAM starting address. */
7361         if (ha->port_no == 0) {
7362                 ha->nvram_base = FA_NVRAM_FUNC0_ADDR;
7363                 ha->vpd_base = FA_NVRAM_VPD0_ADDR;
7364         } else {
7365                 ha->nvram_base = FA_NVRAM_FUNC1_ADDR;
7366                 ha->vpd_base = FA_NVRAM_VPD1_ADDR;
7367         }
7368
7369         ha->nvram_size = sizeof(*nv);
7370         ha->vpd_size = FA_NVRAM_VPD_SIZE;
7371
7372         /* Get VPD data into cache */
7373         ha->vpd = ha->nvram + VPD_OFFSET;
7374         ha->isp_ops->read_nvram(vha, ha->vpd,
7375             ha->nvram_base - FA_NVRAM_FUNC0_ADDR, FA_NVRAM_VPD_SIZE * 4);
7376
7377         /* Get NVRAM data into cache and calculate checksum. */
7378         dptr = (__force __le32 *)nv;
7379         ha->isp_ops->read_nvram(vha, dptr, ha->nvram_base, ha->nvram_size);
7380         for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
7381                 chksum += le32_to_cpu(*dptr);
7382
7383         ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x006a,
7384             "Contents of NVRAM\n");
7385         ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010d,
7386             nv, ha->nvram_size);
7387
7388         /* Bad NVRAM data, set defaults parameters. */
7389         if (chksum || memcmp("ISP ", nv->id, sizeof(nv->id)) ||
7390             le16_to_cpu(nv->nvram_version) < ICB_VERSION) {
7391                 /* Reset NVRAM data. */
7392                 ql_log(ql_log_warn, vha, 0x006b,
7393                     "Inconsistent NVRAM checksum=%#x id=%.4s version=%#x.\n",
7394                     chksum, nv->id, nv->nvram_version);
7395                 ql_dump_buffer(ql_dbg_init, vha, 0x006b, nv, sizeof(*nv));
7396                 ql_log(ql_log_warn, vha, 0x006c,
7397                     "Falling back to functioning (yet invalid -- WWPN) "
7398                     "defaults.\n");
7399
7400                 /*
7401                  * Set default initialization control block.
7402                  */
7403                 memset(nv, 0, ha->nvram_size);
7404                 nv->nvram_version = cpu_to_le16(ICB_VERSION);
7405                 nv->version = cpu_to_le16(ICB_VERSION);
7406                 nv->frame_payload_size = cpu_to_le16(2048);
7407                 nv->execution_throttle = cpu_to_le16(0xFFFF);
7408                 nv->exchange_count = cpu_to_le16(0);
7409                 nv->hard_address = cpu_to_le16(124);
7410                 nv->port_name[0] = 0x21;
7411                 nv->port_name[1] = 0x00 + ha->port_no + 1;
7412                 nv->port_name[2] = 0x00;
7413                 nv->port_name[3] = 0xe0;
7414                 nv->port_name[4] = 0x8b;
7415                 nv->port_name[5] = 0x1c;
7416                 nv->port_name[6] = 0x55;
7417                 nv->port_name[7] = 0x86;
7418                 nv->node_name[0] = 0x20;
7419                 nv->node_name[1] = 0x00;
7420                 nv->node_name[2] = 0x00;
7421                 nv->node_name[3] = 0xe0;
7422                 nv->node_name[4] = 0x8b;
7423                 nv->node_name[5] = 0x1c;
7424                 nv->node_name[6] = 0x55;
7425                 nv->node_name[7] = 0x86;
7426                 qla24xx_nvram_wwn_from_ofw(vha, nv);
7427                 nv->login_retry_count = cpu_to_le16(8);
7428                 nv->interrupt_delay_timer = cpu_to_le16(0);
7429                 nv->login_timeout = cpu_to_le16(0);
7430                 nv->firmware_options_1 =
7431                     cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
7432                 nv->firmware_options_2 = cpu_to_le32(2 << 4);
7433                 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
7434                 nv->firmware_options_3 = cpu_to_le32(2 << 13);
7435                 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
7436                 nv->efi_parameters = cpu_to_le32(0);
7437                 nv->reset_delay = 5;
7438                 nv->max_luns_per_target = cpu_to_le16(128);
7439                 nv->port_down_retry_count = cpu_to_le16(30);
7440                 nv->link_down_timeout = cpu_to_le16(30);
7441
7442                 rval = 1;
7443         }
7444
7445         if (qla_tgt_mode_enabled(vha)) {
7446                 /* Don't enable full login after initial LIP */
7447                 nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
7448                 /* Don't enable LIP full login for initiator */
7449                 nv->host_p &= cpu_to_le32(~BIT_10);
7450         }
7451
7452         qlt_24xx_config_nvram_stage1(vha, nv);
7453
7454         /* Reset Initialization control block */
7455         memset(icb, 0, ha->init_cb_size);
7456
7457         /* Copy 1st segment. */
7458         dptr1 = (uint8_t *)icb;
7459         dptr2 = (uint8_t *)&nv->version;
7460         cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
7461         while (cnt--)
7462                 *dptr1++ = *dptr2++;
7463
7464         icb->login_retry_count = nv->login_retry_count;
7465         icb->link_down_on_nos = nv->link_down_on_nos;
7466
7467         /* Copy 2nd segment. */
7468         dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
7469         dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
7470         cnt = (uint8_t *)&icb->reserved_3 -
7471             (uint8_t *)&icb->interrupt_delay_timer;
7472         while (cnt--)
7473                 *dptr1++ = *dptr2++;
7474         ha->frame_payload_size = le16_to_cpu(icb->frame_payload_size);
7475         /*
7476          * Setup driver NVRAM options.
7477          */
7478         qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
7479             "QLA2462");
7480
7481         qlt_24xx_config_nvram_stage2(vha, icb);
7482
7483         if (nv->host_p & cpu_to_le32(BIT_15)) {
7484                 /* Use alternate WWN? */
7485                 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
7486                 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
7487         }
7488
7489         /* Prepare nodename */
7490         if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
7491                 /*
7492                  * Firmware will apply the following mask if the nodename was
7493                  * not provided.
7494                  */
7495                 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
7496                 icb->node_name[0] &= 0xF0;
7497         }
7498
7499         /* Set host adapter parameters. */
7500         ha->flags.disable_risc_code_load = 0;
7501         ha->flags.enable_lip_reset = 0;
7502         ha->flags.enable_lip_full_login =
7503             le32_to_cpu(nv->host_p) & BIT_10 ? 1 : 0;
7504         ha->flags.enable_target_reset =
7505             le32_to_cpu(nv->host_p) & BIT_11 ? 1 : 0;
7506         ha->flags.enable_led_scheme = 0;
7507         ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1 : 0;
7508
7509         ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
7510             (BIT_6 | BIT_5 | BIT_4)) >> 4;
7511
7512         memcpy(ha->fw_seriallink_options24, nv->seriallink_options,
7513             sizeof(ha->fw_seriallink_options24));
7514
7515         /* save HBA serial number */
7516         ha->serial0 = icb->port_name[5];
7517         ha->serial1 = icb->port_name[6];
7518         ha->serial2 = icb->port_name[7];
7519         memcpy(vha->node_name, icb->node_name, WWN_SIZE);
7520         memcpy(vha->port_name, icb->port_name, WWN_SIZE);
7521
7522         icb->execution_throttle = cpu_to_le16(0xFFFF);
7523
7524         ha->retry_count = le16_to_cpu(nv->login_retry_count);
7525
7526         /* Set minimum login_timeout to 4 seconds. */
7527         if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
7528                 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
7529         if (le16_to_cpu(nv->login_timeout) < 4)
7530                 nv->login_timeout = cpu_to_le16(4);
7531         ha->login_timeout = le16_to_cpu(nv->login_timeout);
7532
7533         /* Set minimum RATOV to 100 tenths of a second. */
7534         ha->r_a_tov = 100;
7535
7536         ha->loop_reset_delay = nv->reset_delay;
7537
7538         /* Link Down Timeout = 0:
7539          *
7540          *      When Port Down timer expires we will start returning
7541          *      I/O's to OS with "DID_NO_CONNECT".
7542          *
7543          * Link Down Timeout != 0:
7544          *
7545          *       The driver waits for the link to come up after link down
7546          *       before returning I/Os to OS with "DID_NO_CONNECT".
7547          */
7548         if (le16_to_cpu(nv->link_down_timeout) == 0) {
7549                 ha->loop_down_abort_time =
7550                     (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
7551         } else {
7552                 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
7553                 ha->loop_down_abort_time =
7554                     (LOOP_DOWN_TIME - ha->link_down_timeout);
7555         }
7556
7557         /* Need enough time to try and get the port back. */
7558         ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
7559         if (qlport_down_retry)
7560                 ha->port_down_retry_count = qlport_down_retry;
7561
7562         /* Set login_retry_count */
7563         ha->login_retry_count  = le16_to_cpu(nv->login_retry_count);
7564         if (ha->port_down_retry_count ==
7565             le16_to_cpu(nv->port_down_retry_count) &&
7566             ha->port_down_retry_count > 3)
7567                 ha->login_retry_count = ha->port_down_retry_count;
7568         else if (ha->port_down_retry_count > (int)ha->login_retry_count)
7569                 ha->login_retry_count = ha->port_down_retry_count;
7570         if (ql2xloginretrycount)
7571                 ha->login_retry_count = ql2xloginretrycount;
7572
7573         /* N2N: driver will initiate Login instead of FW */
7574         icb->firmware_options_3 |= cpu_to_le32(BIT_8);
7575
7576         /* Enable ZIO. */
7577         if (!vha->flags.init_done) {
7578                 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
7579                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
7580                 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
7581                     le16_to_cpu(icb->interrupt_delay_timer) : 2;
7582         }
7583         icb->firmware_options_2 &= cpu_to_le32(
7584             ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
7585         if (ha->zio_mode != QLA_ZIO_DISABLED) {
7586                 ha->zio_mode = QLA_ZIO_MODE_6;
7587
7588                 ql_log(ql_log_info, vha, 0x006f,
7589                     "ZIO mode %d enabled; timer delay (%d us).\n",
7590                     ha->zio_mode, ha->zio_timer * 100);
7591
7592                 icb->firmware_options_2 |= cpu_to_le32(
7593                     (uint32_t)ha->zio_mode);
7594                 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
7595         }
7596
7597         if (rval) {
7598                 ql_log(ql_log_warn, vha, 0x0070,
7599                     "NVRAM configuration failed.\n");
7600         }
7601         return (rval);
7602 }
7603
7604 static void
7605 qla27xx_print_image(struct scsi_qla_host *vha, char *name,
7606     struct qla27xx_image_status *image_status)
7607 {
7608         ql_dbg(ql_dbg_init, vha, 0x018b,
7609             "%s %s: mask=%#02x gen=%#04x ver=%u.%u map=%#01x sum=%#08x sig=%#08x\n",
7610             name, "status",
7611             image_status->image_status_mask,
7612             le16_to_cpu(image_status->generation),
7613             image_status->ver_major,
7614             image_status->ver_minor,
7615             image_status->bitmap,
7616             le32_to_cpu(image_status->checksum),
7617             le32_to_cpu(image_status->signature));
7618 }
7619
7620 static bool
7621 qla28xx_check_aux_image_status_signature(
7622     struct qla27xx_image_status *image_status)
7623 {
7624         ulong signature = le32_to_cpu(image_status->signature);
7625
7626         return signature != QLA28XX_AUX_IMG_STATUS_SIGN;
7627 }
7628
7629 static bool
7630 qla27xx_check_image_status_signature(struct qla27xx_image_status *image_status)
7631 {
7632         ulong signature = le32_to_cpu(image_status->signature);
7633
7634         return
7635             signature != QLA27XX_IMG_STATUS_SIGN &&
7636             signature != QLA28XX_IMG_STATUS_SIGN;
7637 }
7638
7639 static ulong
7640 qla27xx_image_status_checksum(struct qla27xx_image_status *image_status)
7641 {
7642         __le32 *p = (__force __le32 *)image_status;
7643         uint n = sizeof(*image_status) / sizeof(*p);
7644         uint32_t sum = 0;
7645
7646         for ( ; n--; p++)
7647                 sum += le32_to_cpup(p);
7648
7649         return sum;
7650 }
7651
7652 static inline uint
7653 qla28xx_component_bitmask(struct qla27xx_image_status *aux, uint bitmask)
7654 {
7655         return aux->bitmap & bitmask ?
7656             QLA27XX_SECONDARY_IMAGE : QLA27XX_PRIMARY_IMAGE;
7657 }
7658
7659 static void
7660 qla28xx_component_status(
7661     struct active_regions *active_regions, struct qla27xx_image_status *aux)
7662 {
7663         active_regions->aux.board_config =
7664             qla28xx_component_bitmask(aux, QLA28XX_AUX_IMG_BOARD_CONFIG);
7665
7666         active_regions->aux.vpd_nvram =
7667             qla28xx_component_bitmask(aux, QLA28XX_AUX_IMG_VPD_NVRAM);
7668
7669         active_regions->aux.npiv_config_0_1 =
7670             qla28xx_component_bitmask(aux, QLA28XX_AUX_IMG_NPIV_CONFIG_0_1);
7671
7672         active_regions->aux.npiv_config_2_3 =
7673             qla28xx_component_bitmask(aux, QLA28XX_AUX_IMG_NPIV_CONFIG_2_3);
7674 }
7675
7676 static int
7677 qla27xx_compare_image_generation(
7678     struct qla27xx_image_status *pri_image_status,
7679     struct qla27xx_image_status *sec_image_status)
7680 {
7681         /* calculate generation delta as uint16 (this accounts for wrap) */
7682         int16_t delta =
7683             le16_to_cpu(pri_image_status->generation) -
7684             le16_to_cpu(sec_image_status->generation);
7685
7686         ql_dbg(ql_dbg_init, NULL, 0x0180, "generation delta = %d\n", delta);
7687
7688         return delta;
7689 }
7690
7691 void
7692 qla28xx_get_aux_images(
7693         struct scsi_qla_host *vha, struct active_regions *active_regions)
7694 {
7695         struct qla_hw_data *ha = vha->hw;
7696         struct qla27xx_image_status pri_aux_image_status, sec_aux_image_status;
7697         bool valid_pri_image = false, valid_sec_image = false;
7698         bool active_pri_image = false, active_sec_image = false;
7699
7700         if (!ha->flt_region_aux_img_status_pri) {
7701                 ql_dbg(ql_dbg_init, vha, 0x018a, "Primary aux image not addressed\n");
7702                 goto check_sec_image;
7703         }
7704
7705         qla24xx_read_flash_data(vha, (uint32_t *)&pri_aux_image_status,
7706             ha->flt_region_aux_img_status_pri,
7707             sizeof(pri_aux_image_status) >> 2);
7708         qla27xx_print_image(vha, "Primary aux image", &pri_aux_image_status);
7709
7710         if (qla28xx_check_aux_image_status_signature(&pri_aux_image_status)) {
7711                 ql_dbg(ql_dbg_init, vha, 0x018b,
7712                     "Primary aux image signature (%#x) not valid\n",
7713                     le32_to_cpu(pri_aux_image_status.signature));
7714                 goto check_sec_image;
7715         }
7716
7717         if (qla27xx_image_status_checksum(&pri_aux_image_status)) {
7718                 ql_dbg(ql_dbg_init, vha, 0x018c,
7719                     "Primary aux image checksum failed\n");
7720                 goto check_sec_image;
7721         }
7722
7723         valid_pri_image = true;
7724
7725         if (pri_aux_image_status.image_status_mask & 1) {
7726                 ql_dbg(ql_dbg_init, vha, 0x018d,
7727                     "Primary aux image is active\n");
7728                 active_pri_image = true;
7729         }
7730
7731 check_sec_image:
7732         if (!ha->flt_region_aux_img_status_sec) {
7733                 ql_dbg(ql_dbg_init, vha, 0x018a,
7734                     "Secondary aux image not addressed\n");
7735                 goto check_valid_image;
7736         }
7737
7738         qla24xx_read_flash_data(vha, (uint32_t *)&sec_aux_image_status,
7739             ha->flt_region_aux_img_status_sec,
7740             sizeof(sec_aux_image_status) >> 2);
7741         qla27xx_print_image(vha, "Secondary aux image", &sec_aux_image_status);
7742
7743         if (qla28xx_check_aux_image_status_signature(&sec_aux_image_status)) {
7744                 ql_dbg(ql_dbg_init, vha, 0x018b,
7745                     "Secondary aux image signature (%#x) not valid\n",
7746                     le32_to_cpu(sec_aux_image_status.signature));
7747                 goto check_valid_image;
7748         }
7749
7750         if (qla27xx_image_status_checksum(&sec_aux_image_status)) {
7751                 ql_dbg(ql_dbg_init, vha, 0x018c,
7752                     "Secondary aux image checksum failed\n");
7753                 goto check_valid_image;
7754         }
7755
7756         valid_sec_image = true;
7757
7758         if (sec_aux_image_status.image_status_mask & 1) {
7759                 ql_dbg(ql_dbg_init, vha, 0x018d,
7760                     "Secondary aux image is active\n");
7761                 active_sec_image = true;
7762         }
7763
7764 check_valid_image:
7765         if (valid_pri_image && active_pri_image &&
7766             valid_sec_image && active_sec_image) {
7767                 if (qla27xx_compare_image_generation(&pri_aux_image_status,
7768                     &sec_aux_image_status) >= 0) {
7769                         qla28xx_component_status(active_regions,
7770                             &pri_aux_image_status);
7771                 } else {
7772                         qla28xx_component_status(active_regions,
7773                             &sec_aux_image_status);
7774                 }
7775         } else if (valid_pri_image && active_pri_image) {
7776                 qla28xx_component_status(active_regions, &pri_aux_image_status);
7777         } else if (valid_sec_image && active_sec_image) {
7778                 qla28xx_component_status(active_regions, &sec_aux_image_status);
7779         }
7780
7781         ql_dbg(ql_dbg_init, vha, 0x018f,
7782             "aux images active: BCFG=%u VPD/NVR=%u NPIV0/1=%u NPIV2/3=%u\n",
7783             active_regions->aux.board_config,
7784             active_regions->aux.vpd_nvram,
7785             active_regions->aux.npiv_config_0_1,
7786             active_regions->aux.npiv_config_2_3);
7787 }
7788
7789 void
7790 qla27xx_get_active_image(struct scsi_qla_host *vha,
7791     struct active_regions *active_regions)
7792 {
7793         struct qla_hw_data *ha = vha->hw;
7794         struct qla27xx_image_status pri_image_status, sec_image_status;
7795         bool valid_pri_image = false, valid_sec_image = false;
7796         bool active_pri_image = false, active_sec_image = false;
7797
7798         if (!ha->flt_region_img_status_pri) {
7799                 ql_dbg(ql_dbg_init, vha, 0x018a, "Primary image not addressed\n");
7800                 goto check_sec_image;
7801         }
7802
7803         if (qla24xx_read_flash_data(vha, (uint32_t *)&pri_image_status,
7804             ha->flt_region_img_status_pri, sizeof(pri_image_status) >> 2) !=
7805             QLA_SUCCESS) {
7806                 WARN_ON_ONCE(true);
7807                 goto check_sec_image;
7808         }
7809         qla27xx_print_image(vha, "Primary image", &pri_image_status);
7810
7811         if (qla27xx_check_image_status_signature(&pri_image_status)) {
7812                 ql_dbg(ql_dbg_init, vha, 0x018b,
7813                     "Primary image signature (%#x) not valid\n",
7814                     le32_to_cpu(pri_image_status.signature));
7815                 goto check_sec_image;
7816         }
7817
7818         if (qla27xx_image_status_checksum(&pri_image_status)) {
7819                 ql_dbg(ql_dbg_init, vha, 0x018c,
7820                     "Primary image checksum failed\n");
7821                 goto check_sec_image;
7822         }
7823
7824         valid_pri_image = true;
7825
7826         if (pri_image_status.image_status_mask & 1) {
7827                 ql_dbg(ql_dbg_init, vha, 0x018d,
7828                     "Primary image is active\n");
7829                 active_pri_image = true;
7830         }
7831
7832 check_sec_image:
7833         if (!ha->flt_region_img_status_sec) {
7834                 ql_dbg(ql_dbg_init, vha, 0x018a, "Secondary image not addressed\n");
7835                 goto check_valid_image;
7836         }
7837
7838         qla24xx_read_flash_data(vha, (uint32_t *)(&sec_image_status),
7839             ha->flt_region_img_status_sec, sizeof(sec_image_status) >> 2);
7840         qla27xx_print_image(vha, "Secondary image", &sec_image_status);
7841
7842         if (qla27xx_check_image_status_signature(&sec_image_status)) {
7843                 ql_dbg(ql_dbg_init, vha, 0x018b,
7844                     "Secondary image signature (%#x) not valid\n",
7845                     le32_to_cpu(sec_image_status.signature));
7846                 goto check_valid_image;
7847         }
7848
7849         if (qla27xx_image_status_checksum(&sec_image_status)) {
7850                 ql_dbg(ql_dbg_init, vha, 0x018c,
7851                     "Secondary image checksum failed\n");
7852                 goto check_valid_image;
7853         }
7854
7855         valid_sec_image = true;
7856
7857         if (sec_image_status.image_status_mask & 1) {
7858                 ql_dbg(ql_dbg_init, vha, 0x018d,
7859                     "Secondary image is active\n");
7860                 active_sec_image = true;
7861         }
7862
7863 check_valid_image:
7864         if (valid_pri_image && active_pri_image)
7865                 active_regions->global = QLA27XX_PRIMARY_IMAGE;
7866
7867         if (valid_sec_image && active_sec_image) {
7868                 if (!active_regions->global ||
7869                     qla27xx_compare_image_generation(
7870                         &pri_image_status, &sec_image_status) < 0) {
7871                         active_regions->global = QLA27XX_SECONDARY_IMAGE;
7872                 }
7873         }
7874
7875         ql_dbg(ql_dbg_init, vha, 0x018f, "active image %s (%u)\n",
7876             active_regions->global == QLA27XX_DEFAULT_IMAGE ?
7877                 "default (boot/fw)" :
7878             active_regions->global == QLA27XX_PRIMARY_IMAGE ?
7879                 "primary" :
7880             active_regions->global == QLA27XX_SECONDARY_IMAGE ?
7881                 "secondary" : "invalid",
7882             active_regions->global);
7883 }
7884
7885 bool qla24xx_risc_firmware_invalid(uint32_t *dword)
7886 {
7887         return
7888             !(dword[4] | dword[5] | dword[6] | dword[7]) ||
7889             !(~dword[4] | ~dword[5] | ~dword[6] | ~dword[7]);
7890 }
7891
7892 static int
7893 qla24xx_load_risc_flash(scsi_qla_host_t *vha, uint32_t *srisc_addr,
7894     uint32_t faddr)
7895 {
7896         int rval;
7897         uint templates, segments, fragment;
7898         ulong i;
7899         uint j;
7900         ulong dlen;
7901         uint32_t *dcode;
7902         uint32_t risc_addr, risc_size, risc_attr = 0;
7903         struct qla_hw_data *ha = vha->hw;
7904         struct req_que *req = ha->req_q_map[0];
7905         struct fwdt *fwdt = ha->fwdt;
7906
7907         ql_dbg(ql_dbg_init, vha, 0x008b,
7908             "FW: Loading firmware from flash (%x).\n", faddr);
7909
7910         dcode = (uint32_t *)req->ring;
7911         qla24xx_read_flash_data(vha, dcode, faddr, 8);
7912         if (qla24xx_risc_firmware_invalid(dcode)) {
7913                 ql_log(ql_log_fatal, vha, 0x008c,
7914                     "Unable to verify the integrity of flash firmware "
7915                     "image.\n");
7916                 ql_log(ql_log_fatal, vha, 0x008d,
7917                     "Firmware data: %08x %08x %08x %08x.\n",
7918                     dcode[0], dcode[1], dcode[2], dcode[3]);
7919
7920                 return QLA_FUNCTION_FAILED;
7921         }
7922
7923         dcode = (uint32_t *)req->ring;
7924         *srisc_addr = 0;
7925         segments = FA_RISC_CODE_SEGMENTS;
7926         for (j = 0; j < segments; j++) {
7927                 ql_dbg(ql_dbg_init, vha, 0x008d,
7928                     "-> Loading segment %u...\n", j);
7929                 qla24xx_read_flash_data(vha, dcode, faddr, 10);
7930                 risc_addr = be32_to_cpu((__force __be32)dcode[2]);
7931                 risc_size = be32_to_cpu((__force __be32)dcode[3]);
7932                 if (!*srisc_addr) {
7933                         *srisc_addr = risc_addr;
7934                         risc_attr = be32_to_cpu((__force __be32)dcode[9]);
7935                 }
7936
7937                 dlen = ha->fw_transfer_size >> 2;
7938                 for (fragment = 0; risc_size; fragment++) {
7939                         if (dlen > risc_size)
7940                                 dlen = risc_size;
7941
7942                         ql_dbg(ql_dbg_init, vha, 0x008e,
7943                             "-> Loading fragment %u: %#x <- %#x (%#lx dwords)...\n",
7944                             fragment, risc_addr, faddr, dlen);
7945                         qla24xx_read_flash_data(vha, dcode, faddr, dlen);
7946                         for (i = 0; i < dlen; i++)
7947                                 dcode[i] = swab32(dcode[i]);
7948
7949                         rval = qla2x00_load_ram(vha, req->dma, risc_addr, dlen);
7950                         if (rval) {
7951                                 ql_log(ql_log_fatal, vha, 0x008f,
7952                                     "-> Failed load firmware fragment %u.\n",
7953                                     fragment);
7954                                 return QLA_FUNCTION_FAILED;
7955                         }
7956
7957                         faddr += dlen;
7958                         risc_addr += dlen;
7959                         risc_size -= dlen;
7960                 }
7961         }
7962
7963         if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
7964                 return QLA_SUCCESS;
7965
7966         templates = (risc_attr & BIT_9) ? 2 : 1;
7967         ql_dbg(ql_dbg_init, vha, 0x0160, "-> templates = %u\n", templates);
7968         for (j = 0; j < templates; j++, fwdt++) {
7969                 if (fwdt->template)
7970                         vfree(fwdt->template);
7971                 fwdt->template = NULL;
7972                 fwdt->length = 0;
7973
7974                 dcode = (uint32_t *)req->ring;
7975                 qla24xx_read_flash_data(vha, dcode, faddr, 7);
7976                 risc_size = be32_to_cpu((__force __be32)dcode[2]);
7977                 ql_dbg(ql_dbg_init, vha, 0x0161,
7978                     "-> fwdt%u template array at %#x (%#x dwords)\n",
7979                     j, faddr, risc_size);
7980                 if (!risc_size || !~risc_size) {
7981                         ql_dbg(ql_dbg_init, vha, 0x0162,
7982                             "-> fwdt%u failed to read array\n", j);
7983                         goto failed;
7984                 }
7985
7986                 /* skip header and ignore checksum */
7987                 faddr += 7;
7988                 risc_size -= 8;
7989
7990                 ql_dbg(ql_dbg_init, vha, 0x0163,
7991                     "-> fwdt%u template allocate template %#x words...\n",
7992                     j, risc_size);
7993                 fwdt->template = vmalloc(risc_size * sizeof(*dcode));
7994                 if (!fwdt->template) {
7995                         ql_log(ql_log_warn, vha, 0x0164,
7996                             "-> fwdt%u failed allocate template.\n", j);
7997                         goto failed;
7998                 }
7999
8000                 dcode = fwdt->template;
8001                 qla24xx_read_flash_data(vha, dcode, faddr, risc_size);
8002
8003                 if (!qla27xx_fwdt_template_valid(dcode)) {
8004                         ql_log(ql_log_warn, vha, 0x0165,
8005                             "-> fwdt%u failed template validate\n", j);
8006                         goto failed;
8007                 }
8008
8009                 dlen = qla27xx_fwdt_template_size(dcode);
8010                 ql_dbg(ql_dbg_init, vha, 0x0166,
8011                     "-> fwdt%u template size %#lx bytes (%#lx words)\n",
8012                     j, dlen, dlen / sizeof(*dcode));
8013                 if (dlen > risc_size * sizeof(*dcode)) {
8014                         ql_log(ql_log_warn, vha, 0x0167,
8015                             "-> fwdt%u template exceeds array (%-lu bytes)\n",
8016                             j, dlen - risc_size * sizeof(*dcode));
8017                         goto failed;
8018                 }
8019
8020                 fwdt->length = dlen;
8021                 ql_dbg(ql_dbg_init, vha, 0x0168,
8022                     "-> fwdt%u loaded template ok\n", j);
8023
8024                 faddr += risc_size + 1;
8025         }
8026
8027         return QLA_SUCCESS;
8028
8029 failed:
8030         if (fwdt->template)
8031                 vfree(fwdt->template);
8032         fwdt->template = NULL;
8033         fwdt->length = 0;
8034
8035         return QLA_SUCCESS;
8036 }
8037
8038 #define QLA_FW_URL "http://ldriver.qlogic.com/firmware/"
8039
8040 int
8041 qla2x00_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
8042 {
8043         int     rval;
8044         int     i, fragment;
8045         uint16_t *wcode;
8046         __be16   *fwcode;
8047         uint32_t risc_addr, risc_size, fwclen, wlen, *seg;
8048         struct fw_blob *blob;
8049         struct qla_hw_data *ha = vha->hw;
8050         struct req_que *req = ha->req_q_map[0];
8051
8052         /* Load firmware blob. */
8053         blob = qla2x00_request_firmware(vha);
8054         if (!blob) {
8055                 ql_log(ql_log_info, vha, 0x0083,
8056                     "Firmware image unavailable.\n");
8057                 ql_log(ql_log_info, vha, 0x0084,
8058                     "Firmware images can be retrieved from: "QLA_FW_URL ".\n");
8059                 return QLA_FUNCTION_FAILED;
8060         }
8061
8062         rval = QLA_SUCCESS;
8063
8064         wcode = (uint16_t *)req->ring;
8065         *srisc_addr = 0;
8066         fwcode = (__force __be16 *)blob->fw->data;
8067         fwclen = 0;
8068
8069         /* Validate firmware image by checking version. */
8070         if (blob->fw->size < 8 * sizeof(uint16_t)) {
8071                 ql_log(ql_log_fatal, vha, 0x0085,
8072                     "Unable to verify integrity of firmware image (%zd).\n",
8073                     blob->fw->size);
8074                 goto fail_fw_integrity;
8075         }
8076         for (i = 0; i < 4; i++)
8077                 wcode[i] = be16_to_cpu(fwcode[i + 4]);
8078         if ((wcode[0] == 0xffff && wcode[1] == 0xffff && wcode[2] == 0xffff &&
8079             wcode[3] == 0xffff) || (wcode[0] == 0 && wcode[1] == 0 &&
8080                 wcode[2] == 0 && wcode[3] == 0)) {
8081                 ql_log(ql_log_fatal, vha, 0x0086,
8082                     "Unable to verify integrity of firmware image.\n");
8083                 ql_log(ql_log_fatal, vha, 0x0087,
8084                     "Firmware data: %04x %04x %04x %04x.\n",
8085                     wcode[0], wcode[1], wcode[2], wcode[3]);
8086                 goto fail_fw_integrity;
8087         }
8088
8089         seg = blob->segs;
8090         while (*seg && rval == QLA_SUCCESS) {
8091                 risc_addr = *seg;
8092                 *srisc_addr = *srisc_addr == 0 ? *seg : *srisc_addr;
8093                 risc_size = be16_to_cpu(fwcode[3]);
8094
8095                 /* Validate firmware image size. */
8096                 fwclen += risc_size * sizeof(uint16_t);
8097                 if (blob->fw->size < fwclen) {
8098                         ql_log(ql_log_fatal, vha, 0x0088,
8099                             "Unable to verify integrity of firmware image "
8100                             "(%zd).\n", blob->fw->size);
8101                         goto fail_fw_integrity;
8102                 }
8103
8104                 fragment = 0;
8105                 while (risc_size > 0 && rval == QLA_SUCCESS) {
8106                         wlen = (uint16_t)(ha->fw_transfer_size >> 1);
8107                         if (wlen > risc_size)
8108                                 wlen = risc_size;
8109                         ql_dbg(ql_dbg_init, vha, 0x0089,
8110                             "Loading risc segment@ risc addr %x number of "
8111                             "words 0x%x.\n", risc_addr, wlen);
8112
8113                         for (i = 0; i < wlen; i++)
8114                                 wcode[i] = swab16((__force u32)fwcode[i]);
8115
8116                         rval = qla2x00_load_ram(vha, req->dma, risc_addr,
8117                             wlen);
8118                         if (rval) {
8119                                 ql_log(ql_log_fatal, vha, 0x008a,
8120                                     "Failed to load segment %d of firmware.\n",
8121                                     fragment);
8122                                 break;
8123                         }
8124
8125                         fwcode += wlen;
8126                         risc_addr += wlen;
8127                         risc_size -= wlen;
8128                         fragment++;
8129                 }
8130
8131                 /* Next segment. */
8132                 seg++;
8133         }
8134         return rval;
8135
8136 fail_fw_integrity:
8137         return QLA_FUNCTION_FAILED;
8138 }
8139
8140 static int
8141 qla24xx_load_risc_blob(scsi_qla_host_t *vha, uint32_t *srisc_addr)
8142 {
8143         int     rval;
8144         uint templates, segments, fragment;
8145         uint32_t *dcode;
8146         ulong dlen;
8147         uint32_t risc_addr, risc_size, risc_attr = 0;
8148         ulong i;
8149         uint j;
8150         struct fw_blob *blob;
8151         __be32 *fwcode;
8152         struct qla_hw_data *ha = vha->hw;
8153         struct req_que *req = ha->req_q_map[0];
8154         struct fwdt *fwdt = ha->fwdt;
8155
8156         ql_dbg(ql_dbg_init, vha, 0x0090,
8157             "-> FW: Loading via request-firmware.\n");
8158
8159         blob = qla2x00_request_firmware(vha);
8160         if (!blob) {
8161                 ql_log(ql_log_warn, vha, 0x0092,
8162                     "-> Firmware file not found.\n");
8163
8164                 return QLA_FUNCTION_FAILED;
8165         }
8166
8167         fwcode = (__force __be32 *)blob->fw->data;
8168         dcode = (__force uint32_t *)fwcode;
8169         if (qla24xx_risc_firmware_invalid(dcode)) {
8170                 ql_log(ql_log_fatal, vha, 0x0093,
8171                     "Unable to verify integrity of firmware image (%zd).\n",
8172                     blob->fw->size);
8173                 ql_log(ql_log_fatal, vha, 0x0095,
8174                     "Firmware data: %08x %08x %08x %08x.\n",
8175                     dcode[0], dcode[1], dcode[2], dcode[3]);
8176                 return QLA_FUNCTION_FAILED;
8177         }
8178
8179         dcode = (uint32_t *)req->ring;
8180         *srisc_addr = 0;
8181         segments = FA_RISC_CODE_SEGMENTS;
8182         for (j = 0; j < segments; j++) {
8183                 ql_dbg(ql_dbg_init, vha, 0x0096,
8184                     "-> Loading segment %u...\n", j);
8185                 risc_addr = be32_to_cpu(fwcode[2]);
8186                 risc_size = be32_to_cpu(fwcode[3]);
8187
8188                 if (!*srisc_addr) {
8189                         *srisc_addr = risc_addr;
8190                         risc_attr = be32_to_cpu(fwcode[9]);
8191                 }
8192
8193                 dlen = ha->fw_transfer_size >> 2;
8194                 for (fragment = 0; risc_size; fragment++) {
8195                         if (dlen > risc_size)
8196                                 dlen = risc_size;
8197
8198                         ql_dbg(ql_dbg_init, vha, 0x0097,
8199                             "-> Loading fragment %u: %#x <- %#x (%#lx words)...\n",
8200                             fragment, risc_addr,
8201                             (uint32_t)(fwcode - (typeof(fwcode))blob->fw->data),
8202                             dlen);
8203
8204                         for (i = 0; i < dlen; i++)
8205                                 dcode[i] = swab32((__force u32)fwcode[i]);
8206
8207                         rval = qla2x00_load_ram(vha, req->dma, risc_addr, dlen);
8208                         if (rval) {
8209                                 ql_log(ql_log_fatal, vha, 0x0098,
8210                                     "-> Failed load firmware fragment %u.\n",
8211                                     fragment);
8212                                 return QLA_FUNCTION_FAILED;
8213                         }
8214
8215                         fwcode += dlen;
8216                         risc_addr += dlen;
8217                         risc_size -= dlen;
8218                 }
8219         }
8220
8221         if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
8222                 return QLA_SUCCESS;
8223
8224         templates = (risc_attr & BIT_9) ? 2 : 1;
8225         ql_dbg(ql_dbg_init, vha, 0x0170, "-> templates = %u\n", templates);
8226         for (j = 0; j < templates; j++, fwdt++) {
8227                 if (fwdt->template)
8228                         vfree(fwdt->template);
8229                 fwdt->template = NULL;
8230                 fwdt->length = 0;
8231
8232                 risc_size = be32_to_cpu(fwcode[2]);
8233                 ql_dbg(ql_dbg_init, vha, 0x0171,
8234                     "-> fwdt%u template array at %#x (%#x dwords)\n",
8235                     j, (uint32_t)((void *)fwcode - (void *)blob->fw->data),
8236                     risc_size);
8237                 if (!risc_size || !~risc_size) {
8238                         ql_dbg(ql_dbg_init, vha, 0x0172,
8239                             "-> fwdt%u failed to read array\n", j);
8240                         goto failed;
8241                 }
8242
8243                 /* skip header and ignore checksum */
8244                 fwcode += 7;
8245                 risc_size -= 8;
8246
8247                 ql_dbg(ql_dbg_init, vha, 0x0173,
8248                     "-> fwdt%u template allocate template %#x words...\n",
8249                     j, risc_size);
8250                 fwdt->template = vmalloc(risc_size * sizeof(*dcode));
8251                 if (!fwdt->template) {
8252                         ql_log(ql_log_warn, vha, 0x0174,
8253                             "-> fwdt%u failed allocate template.\n", j);
8254                         goto failed;
8255                 }
8256
8257                 dcode = fwdt->template;
8258                 for (i = 0; i < risc_size; i++)
8259                         dcode[i] = (__force u32)fwcode[i];
8260
8261                 if (!qla27xx_fwdt_template_valid(dcode)) {
8262                         ql_log(ql_log_warn, vha, 0x0175,
8263                             "-> fwdt%u failed template validate\n", j);
8264                         goto failed;
8265                 }
8266
8267                 dlen = qla27xx_fwdt_template_size(dcode);
8268                 ql_dbg(ql_dbg_init, vha, 0x0176,
8269                     "-> fwdt%u template size %#lx bytes (%#lx words)\n",
8270                     j, dlen, dlen / sizeof(*dcode));
8271                 if (dlen > risc_size * sizeof(*dcode)) {
8272                         ql_log(ql_log_warn, vha, 0x0177,
8273                             "-> fwdt%u template exceeds array (%-lu bytes)\n",
8274                             j, dlen - risc_size * sizeof(*dcode));
8275                         goto failed;
8276                 }
8277
8278                 fwdt->length = dlen;
8279                 ql_dbg(ql_dbg_init, vha, 0x0178,
8280                     "-> fwdt%u loaded template ok\n", j);
8281
8282                 fwcode += risc_size + 1;
8283         }
8284
8285         return QLA_SUCCESS;
8286
8287 failed:
8288         if (fwdt->template)
8289                 vfree(fwdt->template);
8290         fwdt->template = NULL;
8291         fwdt->length = 0;
8292
8293         return QLA_SUCCESS;
8294 }
8295
8296 int
8297 qla24xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
8298 {
8299         int rval;
8300
8301         if (ql2xfwloadbin == 1)
8302                 return qla81xx_load_risc(vha, srisc_addr);
8303
8304         /*
8305          * FW Load priority:
8306          * 1) Firmware via request-firmware interface (.bin file).
8307          * 2) Firmware residing in flash.
8308          */
8309         rval = qla24xx_load_risc_blob(vha, srisc_addr);
8310         if (rval == QLA_SUCCESS)
8311                 return rval;
8312
8313         return qla24xx_load_risc_flash(vha, srisc_addr,
8314             vha->hw->flt_region_fw);
8315 }
8316
8317 int
8318 qla81xx_load_risc(scsi_qla_host_t *vha, uint32_t *srisc_addr)
8319 {
8320         int rval;
8321         struct qla_hw_data *ha = vha->hw;
8322         struct active_regions active_regions = { };
8323
8324         if (ql2xfwloadbin == 2)
8325                 goto try_blob_fw;
8326
8327         /* FW Load priority:
8328          * 1) Firmware residing in flash.
8329          * 2) Firmware via request-firmware interface (.bin file).
8330          * 3) Golden-Firmware residing in flash -- (limited operation).
8331          */
8332
8333         if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
8334                 goto try_primary_fw;
8335
8336         qla27xx_get_active_image(vha, &active_regions);
8337
8338         if (active_regions.global != QLA27XX_SECONDARY_IMAGE)
8339                 goto try_primary_fw;
8340
8341         ql_dbg(ql_dbg_init, vha, 0x008b,
8342             "Loading secondary firmware image.\n");
8343         rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw_sec);
8344         if (!rval)
8345                 return rval;
8346
8347 try_primary_fw:
8348         ql_dbg(ql_dbg_init, vha, 0x008b,
8349             "Loading primary firmware image.\n");
8350         rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_fw);
8351         if (!rval)
8352                 return rval;
8353
8354 try_blob_fw:
8355         rval = qla24xx_load_risc_blob(vha, srisc_addr);
8356         if (!rval || !ha->flt_region_gold_fw)
8357                 return rval;
8358
8359         ql_log(ql_log_info, vha, 0x0099,
8360             "Attempting to fallback to golden firmware.\n");
8361         rval = qla24xx_load_risc_flash(vha, srisc_addr, ha->flt_region_gold_fw);
8362         if (rval)
8363                 return rval;
8364
8365         ql_log(ql_log_info, vha, 0x009a, "Need firmware flash update.\n");
8366         ha->flags.running_gold_fw = 1;
8367         return rval;
8368 }
8369
8370 void
8371 qla2x00_try_to_stop_firmware(scsi_qla_host_t *vha)
8372 {
8373         int ret, retries;
8374         struct qla_hw_data *ha = vha->hw;
8375
8376         if (ha->flags.pci_channel_io_perm_failure)
8377                 return;
8378         if (!IS_FWI2_CAPABLE(ha))
8379                 return;
8380         if (!ha->fw_major_version)
8381                 return;
8382         if (!ha->flags.fw_started)
8383                 return;
8384
8385         ret = qla2x00_stop_firmware(vha);
8386         for (retries = 5; ret != QLA_SUCCESS && ret != QLA_FUNCTION_TIMEOUT &&
8387             ret != QLA_INVALID_COMMAND && retries ; retries--) {
8388                 ha->isp_ops->reset_chip(vha);
8389                 if (ha->isp_ops->chip_diag(vha) != QLA_SUCCESS)
8390                         continue;
8391                 if (qla2x00_setup_chip(vha) != QLA_SUCCESS)
8392                         continue;
8393                 ql_log(ql_log_info, vha, 0x8015,
8394                     "Attempting retry of stop-firmware command.\n");
8395                 ret = qla2x00_stop_firmware(vha);
8396         }
8397
8398         QLA_FW_STOPPED(ha);
8399         ha->flags.fw_init_done = 0;
8400 }
8401
8402 int
8403 qla24xx_configure_vhba(scsi_qla_host_t *vha)
8404 {
8405         int rval = QLA_SUCCESS;
8406         int rval2;
8407         uint16_t mb[MAILBOX_REGISTER_COUNT];
8408         struct qla_hw_data *ha = vha->hw;
8409         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
8410
8411         if (!vha->vp_idx)
8412                 return -EINVAL;
8413
8414         rval = qla2x00_fw_ready(base_vha);
8415
8416         if (rval == QLA_SUCCESS) {
8417                 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8418                 qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL);
8419         }
8420
8421         vha->flags.management_server_logged_in = 0;
8422
8423         /* Login to SNS first */
8424         rval2 = ha->isp_ops->fabric_login(vha, NPH_SNS, 0xff, 0xff, 0xfc, mb,
8425             BIT_1);
8426         if (rval2 != QLA_SUCCESS || mb[0] != MBS_COMMAND_COMPLETE) {
8427                 if (rval2 == QLA_MEMORY_ALLOC_FAILED)
8428                         ql_dbg(ql_dbg_init, vha, 0x0120,
8429                             "Failed SNS login: loop_id=%x, rval2=%d\n",
8430                             NPH_SNS, rval2);
8431                 else
8432                         ql_dbg(ql_dbg_init, vha, 0x0103,
8433                             "Failed SNS login: loop_id=%x mb[0]=%x mb[1]=%x "
8434                             "mb[2]=%x mb[6]=%x mb[7]=%x.\n",
8435                             NPH_SNS, mb[0], mb[1], mb[2], mb[6], mb[7]);
8436                 return (QLA_FUNCTION_FAILED);
8437         }
8438
8439         atomic_set(&vha->loop_down_timer, 0);
8440         atomic_set(&vha->loop_state, LOOP_UP);
8441         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
8442         set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
8443         rval = qla2x00_loop_resync(base_vha);
8444
8445         return rval;
8446 }
8447
8448 /* 84XX Support **************************************************************/
8449
8450 static LIST_HEAD(qla_cs84xx_list);
8451 static DEFINE_MUTEX(qla_cs84xx_mutex);
8452
8453 static struct qla_chip_state_84xx *
8454 qla84xx_get_chip(struct scsi_qla_host *vha)
8455 {
8456         struct qla_chip_state_84xx *cs84xx;
8457         struct qla_hw_data *ha = vha->hw;
8458
8459         mutex_lock(&qla_cs84xx_mutex);
8460
8461         /* Find any shared 84xx chip. */
8462         list_for_each_entry(cs84xx, &qla_cs84xx_list, list) {
8463                 if (cs84xx->bus == ha->pdev->bus) {
8464                         kref_get(&cs84xx->kref);
8465                         goto done;
8466                 }
8467         }
8468
8469         cs84xx = kzalloc(sizeof(*cs84xx), GFP_KERNEL);
8470         if (!cs84xx)
8471                 goto done;
8472
8473         kref_init(&cs84xx->kref);
8474         spin_lock_init(&cs84xx->access_lock);
8475         mutex_init(&cs84xx->fw_update_mutex);
8476         cs84xx->bus = ha->pdev->bus;
8477
8478         list_add_tail(&cs84xx->list, &qla_cs84xx_list);
8479 done:
8480         mutex_unlock(&qla_cs84xx_mutex);
8481         return cs84xx;
8482 }
8483
8484 static void
8485 __qla84xx_chip_release(struct kref *kref)
8486 {
8487         struct qla_chip_state_84xx *cs84xx =
8488             container_of(kref, struct qla_chip_state_84xx, kref);
8489
8490         mutex_lock(&qla_cs84xx_mutex);
8491         list_del(&cs84xx->list);
8492         mutex_unlock(&qla_cs84xx_mutex);
8493         kfree(cs84xx);
8494 }
8495
8496 void
8497 qla84xx_put_chip(struct scsi_qla_host *vha)
8498 {
8499         struct qla_hw_data *ha = vha->hw;
8500
8501         if (ha->cs84xx)
8502                 kref_put(&ha->cs84xx->kref, __qla84xx_chip_release);
8503 }
8504
8505 static int
8506 qla84xx_init_chip(scsi_qla_host_t *vha)
8507 {
8508         int rval;
8509         uint16_t status[2];
8510         struct qla_hw_data *ha = vha->hw;
8511
8512         mutex_lock(&ha->cs84xx->fw_update_mutex);
8513
8514         rval = qla84xx_verify_chip(vha, status);
8515
8516         mutex_unlock(&ha->cs84xx->fw_update_mutex);
8517
8518         return rval != QLA_SUCCESS || status[0] ? QLA_FUNCTION_FAILED :
8519             QLA_SUCCESS;
8520 }
8521
8522 /* 81XX Support **************************************************************/
8523
8524 int
8525 qla81xx_nvram_config(scsi_qla_host_t *vha)
8526 {
8527         int   rval;
8528         struct init_cb_81xx *icb;
8529         struct nvram_81xx *nv;
8530         __le32 *dptr;
8531         uint8_t  *dptr1, *dptr2;
8532         uint32_t chksum;
8533         uint16_t cnt;
8534         struct qla_hw_data *ha = vha->hw;
8535         uint32_t faddr;
8536         struct active_regions active_regions = { };
8537
8538         rval = QLA_SUCCESS;
8539         icb = (struct init_cb_81xx *)ha->init_cb;
8540         nv = ha->nvram;
8541
8542         /* Determine NVRAM starting address. */
8543         ha->nvram_size = sizeof(*nv);
8544         ha->vpd_size = FA_NVRAM_VPD_SIZE;
8545         if (IS_P3P_TYPE(ha) || IS_QLA8031(ha))
8546                 ha->vpd_size = FA_VPD_SIZE_82XX;
8547
8548         if (IS_QLA28XX(ha) || IS_QLA27XX(ha))
8549                 qla28xx_get_aux_images(vha, &active_regions);
8550
8551         /* Get VPD data into cache */
8552         ha->vpd = ha->nvram + VPD_OFFSET;
8553
8554         faddr = ha->flt_region_vpd;
8555         if (IS_QLA28XX(ha)) {
8556                 if (active_regions.aux.vpd_nvram == QLA27XX_SECONDARY_IMAGE)
8557                         faddr = ha->flt_region_vpd_sec;
8558                 ql_dbg(ql_dbg_init, vha, 0x0110,
8559                     "Loading %s nvram image.\n",
8560                     active_regions.aux.vpd_nvram == QLA27XX_PRIMARY_IMAGE ?
8561                     "primary" : "secondary");
8562         }
8563         ha->isp_ops->read_optrom(vha, ha->vpd, faddr << 2, ha->vpd_size);
8564
8565         /* Get NVRAM data into cache and calculate checksum. */
8566         faddr = ha->flt_region_nvram;
8567         if (IS_QLA28XX(ha)) {
8568                 if (active_regions.aux.vpd_nvram == QLA27XX_SECONDARY_IMAGE)
8569                         faddr = ha->flt_region_nvram_sec;
8570         }
8571         ql_dbg(ql_dbg_init, vha, 0x0110,
8572             "Loading %s nvram image.\n",
8573             active_regions.aux.vpd_nvram == QLA27XX_PRIMARY_IMAGE ?
8574             "primary" : "secondary");
8575         ha->isp_ops->read_optrom(vha, ha->nvram, faddr << 2, ha->nvram_size);
8576
8577         dptr = (__force __le32 *)nv;
8578         for (cnt = 0, chksum = 0; cnt < ha->nvram_size >> 2; cnt++, dptr++)
8579                 chksum += le32_to_cpu(*dptr);
8580
8581         ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x0111,
8582             "Contents of NVRAM:\n");
8583         ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0112,
8584             nv, ha->nvram_size);
8585
8586         /* Bad NVRAM data, set defaults parameters. */
8587         if (chksum || memcmp("ISP ", nv->id, sizeof(nv->id)) ||
8588             le16_to_cpu(nv->nvram_version) < ICB_VERSION) {
8589                 /* Reset NVRAM data. */
8590                 ql_log(ql_log_info, vha, 0x0073,
8591                     "Inconsistent NVRAM checksum=%#x id=%.4s version=%#x.\n",
8592                     chksum, nv->id, le16_to_cpu(nv->nvram_version));
8593                 ql_dump_buffer(ql_dbg_init, vha, 0x0073, nv, sizeof(*nv));
8594                 ql_log(ql_log_info, vha, 0x0074,
8595                     "Falling back to functioning (yet invalid -- WWPN) "
8596                     "defaults.\n");
8597
8598                 /*
8599                  * Set default initialization control block.
8600                  */
8601                 memset(nv, 0, ha->nvram_size);
8602                 nv->nvram_version = cpu_to_le16(ICB_VERSION);
8603                 nv->version = cpu_to_le16(ICB_VERSION);
8604                 nv->frame_payload_size = cpu_to_le16(2048);
8605                 nv->execution_throttle = cpu_to_le16(0xFFFF);
8606                 nv->exchange_count = cpu_to_le16(0);
8607                 nv->port_name[0] = 0x21;
8608                 nv->port_name[1] = 0x00 + ha->port_no + 1;
8609                 nv->port_name[2] = 0x00;
8610                 nv->port_name[3] = 0xe0;
8611                 nv->port_name[4] = 0x8b;
8612                 nv->port_name[5] = 0x1c;
8613                 nv->port_name[6] = 0x55;
8614                 nv->port_name[7] = 0x86;
8615                 nv->node_name[0] = 0x20;
8616                 nv->node_name[1] = 0x00;
8617                 nv->node_name[2] = 0x00;
8618                 nv->node_name[3] = 0xe0;
8619                 nv->node_name[4] = 0x8b;
8620                 nv->node_name[5] = 0x1c;
8621                 nv->node_name[6] = 0x55;
8622                 nv->node_name[7] = 0x86;
8623                 nv->login_retry_count = cpu_to_le16(8);
8624                 nv->interrupt_delay_timer = cpu_to_le16(0);
8625                 nv->login_timeout = cpu_to_le16(0);
8626                 nv->firmware_options_1 =
8627                     cpu_to_le32(BIT_14|BIT_13|BIT_2|BIT_1);
8628                 nv->firmware_options_2 = cpu_to_le32(2 << 4);
8629                 nv->firmware_options_2 |= cpu_to_le32(BIT_12);
8630                 nv->firmware_options_3 = cpu_to_le32(2 << 13);
8631                 nv->host_p = cpu_to_le32(BIT_11|BIT_10);
8632                 nv->efi_parameters = cpu_to_le32(0);
8633                 nv->reset_delay = 5;
8634                 nv->max_luns_per_target = cpu_to_le16(128);
8635                 nv->port_down_retry_count = cpu_to_le16(30);
8636                 nv->link_down_timeout = cpu_to_le16(180);
8637                 nv->enode_mac[0] = 0x00;
8638                 nv->enode_mac[1] = 0xC0;
8639                 nv->enode_mac[2] = 0xDD;
8640                 nv->enode_mac[3] = 0x04;
8641                 nv->enode_mac[4] = 0x05;
8642                 nv->enode_mac[5] = 0x06 + ha->port_no + 1;
8643
8644                 rval = 1;
8645         }
8646
8647         if (IS_T10_PI_CAPABLE(ha))
8648                 nv->frame_payload_size &= cpu_to_le16(~7);
8649
8650         qlt_81xx_config_nvram_stage1(vha, nv);
8651
8652         /* Reset Initialization control block */
8653         memset(icb, 0, ha->init_cb_size);
8654
8655         /* Copy 1st segment. */
8656         dptr1 = (uint8_t *)icb;
8657         dptr2 = (uint8_t *)&nv->version;
8658         cnt = (uint8_t *)&icb->response_q_inpointer - (uint8_t *)&icb->version;
8659         while (cnt--)
8660                 *dptr1++ = *dptr2++;
8661
8662         icb->login_retry_count = nv->login_retry_count;
8663
8664         /* Copy 2nd segment. */
8665         dptr1 = (uint8_t *)&icb->interrupt_delay_timer;
8666         dptr2 = (uint8_t *)&nv->interrupt_delay_timer;
8667         cnt = (uint8_t *)&icb->reserved_5 -
8668             (uint8_t *)&icb->interrupt_delay_timer;
8669         while (cnt--)
8670                 *dptr1++ = *dptr2++;
8671
8672         memcpy(icb->enode_mac, nv->enode_mac, sizeof(icb->enode_mac));
8673         /* Some boards (with valid NVRAMs) still have NULL enode_mac!! */
8674         if (!memcmp(icb->enode_mac, "\0\0\0\0\0\0", sizeof(icb->enode_mac))) {
8675                 icb->enode_mac[0] = 0x00;
8676                 icb->enode_mac[1] = 0xC0;
8677                 icb->enode_mac[2] = 0xDD;
8678                 icb->enode_mac[3] = 0x04;
8679                 icb->enode_mac[4] = 0x05;
8680                 icb->enode_mac[5] = 0x06 + ha->port_no + 1;
8681         }
8682
8683         /* Use extended-initialization control block. */
8684         memcpy(ha->ex_init_cb, &nv->ex_version, sizeof(*ha->ex_init_cb));
8685         ha->frame_payload_size = le16_to_cpu(icb->frame_payload_size);
8686         /*
8687          * Setup driver NVRAM options.
8688          */
8689         qla2x00_set_model_info(vha, nv->model_name, sizeof(nv->model_name),
8690             "QLE8XXX");
8691
8692         qlt_81xx_config_nvram_stage2(vha, icb);
8693
8694         /* Use alternate WWN? */
8695         if (nv->host_p & cpu_to_le32(BIT_15)) {
8696                 memcpy(icb->node_name, nv->alternate_node_name, WWN_SIZE);
8697                 memcpy(icb->port_name, nv->alternate_port_name, WWN_SIZE);
8698         }
8699
8700         /* Prepare nodename */
8701         if ((icb->firmware_options_1 & cpu_to_le32(BIT_14)) == 0) {
8702                 /*
8703                  * Firmware will apply the following mask if the nodename was
8704                  * not provided.
8705                  */
8706                 memcpy(icb->node_name, icb->port_name, WWN_SIZE);
8707                 icb->node_name[0] &= 0xF0;
8708         }
8709
8710         if (IS_QLA28XX(ha) || IS_QLA27XX(ha)) {
8711                 if ((nv->enhanced_features & BIT_7) == 0)
8712                         ha->flags.scm_supported_a = 1;
8713         }
8714
8715         /* Set host adapter parameters. */
8716         ha->flags.disable_risc_code_load = 0;
8717         ha->flags.enable_lip_reset = 0;
8718         ha->flags.enable_lip_full_login =
8719             le32_to_cpu(nv->host_p) & BIT_10 ? 1 : 0;
8720         ha->flags.enable_target_reset =
8721             le32_to_cpu(nv->host_p) & BIT_11 ? 1 : 0;
8722         ha->flags.enable_led_scheme = 0;
8723         ha->flags.disable_serdes = le32_to_cpu(nv->host_p) & BIT_5 ? 1 : 0;
8724
8725         ha->operating_mode = (le32_to_cpu(icb->firmware_options_2) &
8726             (BIT_6 | BIT_5 | BIT_4)) >> 4;
8727
8728         /* save HBA serial number */
8729         ha->serial0 = icb->port_name[5];
8730         ha->serial1 = icb->port_name[6];
8731         ha->serial2 = icb->port_name[7];
8732         memcpy(vha->node_name, icb->node_name, WWN_SIZE);
8733         memcpy(vha->port_name, icb->port_name, WWN_SIZE);
8734
8735         icb->execution_throttle = cpu_to_le16(0xFFFF);
8736
8737         ha->retry_count = le16_to_cpu(nv->login_retry_count);
8738
8739         /* Set minimum login_timeout to 4 seconds. */
8740         if (le16_to_cpu(nv->login_timeout) < ql2xlogintimeout)
8741                 nv->login_timeout = cpu_to_le16(ql2xlogintimeout);
8742         if (le16_to_cpu(nv->login_timeout) < 4)
8743                 nv->login_timeout = cpu_to_le16(4);
8744         ha->login_timeout = le16_to_cpu(nv->login_timeout);
8745
8746         /* Set minimum RATOV to 100 tenths of a second. */
8747         ha->r_a_tov = 100;
8748
8749         ha->loop_reset_delay = nv->reset_delay;
8750
8751         /* Link Down Timeout = 0:
8752          *
8753          *      When Port Down timer expires we will start returning
8754          *      I/O's to OS with "DID_NO_CONNECT".
8755          *
8756          * Link Down Timeout != 0:
8757          *
8758          *       The driver waits for the link to come up after link down
8759          *       before returning I/Os to OS with "DID_NO_CONNECT".
8760          */
8761         if (le16_to_cpu(nv->link_down_timeout) == 0) {
8762                 ha->loop_down_abort_time =
8763                     (LOOP_DOWN_TIME - LOOP_DOWN_TIMEOUT);
8764         } else {
8765                 ha->link_down_timeout = le16_to_cpu(nv->link_down_timeout);
8766                 ha->loop_down_abort_time =
8767                     (LOOP_DOWN_TIME - ha->link_down_timeout);
8768         }
8769
8770         /* Need enough time to try and get the port back. */
8771         ha->port_down_retry_count = le16_to_cpu(nv->port_down_retry_count);
8772         if (qlport_down_retry)
8773                 ha->port_down_retry_count = qlport_down_retry;
8774
8775         /* Set login_retry_count */
8776         ha->login_retry_count  = le16_to_cpu(nv->login_retry_count);
8777         if (ha->port_down_retry_count ==
8778             le16_to_cpu(nv->port_down_retry_count) &&
8779             ha->port_down_retry_count > 3)
8780                 ha->login_retry_count = ha->port_down_retry_count;
8781         else if (ha->port_down_retry_count > (int)ha->login_retry_count)
8782                 ha->login_retry_count = ha->port_down_retry_count;
8783         if (ql2xloginretrycount)
8784                 ha->login_retry_count = ql2xloginretrycount;
8785
8786         /* if not running MSI-X we need handshaking on interrupts */
8787         if (!vha->hw->flags.msix_enabled &&
8788             (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)))
8789                 icb->firmware_options_2 |= cpu_to_le32(BIT_22);
8790
8791         /* Enable ZIO. */
8792         if (!vha->flags.init_done) {
8793                 ha->zio_mode = le32_to_cpu(icb->firmware_options_2) &
8794                     (BIT_3 | BIT_2 | BIT_1 | BIT_0);
8795                 ha->zio_timer = le16_to_cpu(icb->interrupt_delay_timer) ?
8796                     le16_to_cpu(icb->interrupt_delay_timer) : 2;
8797         }
8798         icb->firmware_options_2 &= cpu_to_le32(
8799             ~(BIT_3 | BIT_2 | BIT_1 | BIT_0));
8800         vha->flags.process_response_queue = 0;
8801         if (ha->zio_mode != QLA_ZIO_DISABLED) {
8802                 ha->zio_mode = QLA_ZIO_MODE_6;
8803
8804                 ql_log(ql_log_info, vha, 0x0075,
8805                     "ZIO mode %d enabled; timer delay (%d us).\n",
8806                     ha->zio_mode,
8807                     ha->zio_timer * 100);
8808
8809                 icb->firmware_options_2 |= cpu_to_le32(
8810                     (uint32_t)ha->zio_mode);
8811                 icb->interrupt_delay_timer = cpu_to_le16(ha->zio_timer);
8812                 vha->flags.process_response_queue = 1;
8813         }
8814
8815          /* enable RIDA Format2 */
8816         icb->firmware_options_3 |= cpu_to_le32(BIT_0);
8817
8818         /* N2N: driver will initiate Login instead of FW */
8819         icb->firmware_options_3 |= cpu_to_le32(BIT_8);
8820
8821         /* Determine NVMe/FCP priority for target ports */
8822         ha->fc4_type_priority = qla2xxx_get_fc4_priority(vha);
8823
8824         if (rval) {
8825                 ql_log(ql_log_warn, vha, 0x0076,
8826                     "NVRAM configuration failed.\n");
8827         }
8828         return (rval);
8829 }
8830
8831 int
8832 qla82xx_restart_isp(scsi_qla_host_t *vha)
8833 {
8834         int status, rval;
8835         struct qla_hw_data *ha = vha->hw;
8836         struct scsi_qla_host *vp;
8837         unsigned long flags;
8838
8839         status = qla2x00_init_rings(vha);
8840         if (!status) {
8841                 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8842                 ha->flags.chip_reset_done = 1;
8843
8844                 status = qla2x00_fw_ready(vha);
8845                 if (!status) {
8846                         /* Issue a marker after FW becomes ready. */
8847                         qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL);
8848                         vha->flags.online = 1;
8849                         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
8850                 }
8851
8852                 /* if no cable then assume it's good */
8853                 if ((vha->device_flags & DFLG_NO_CABLE))
8854                         status = 0;
8855         }
8856
8857         if (!status) {
8858                 clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
8859
8860                 if (!atomic_read(&vha->loop_down_timer)) {
8861                         /*
8862                          * Issue marker command only when we are going
8863                          * to start the I/O .
8864                          */
8865                         vha->marker_needed = 1;
8866                 }
8867
8868                 ha->isp_ops->enable_intrs(ha);
8869
8870                 ha->isp_abort_cnt = 0;
8871                 clear_bit(ISP_ABORT_RETRY, &vha->dpc_flags);
8872
8873                 /* Update the firmware version */
8874                 status = qla82xx_check_md_needed(vha);
8875
8876                 if (ha->fce) {
8877                         ha->flags.fce_enabled = 1;
8878                         memset(ha->fce, 0,
8879                             fce_calc_size(ha->fce_bufs));
8880                         rval = qla2x00_enable_fce_trace(vha,
8881                             ha->fce_dma, ha->fce_bufs, ha->fce_mb,
8882                             &ha->fce_bufs);
8883                         if (rval) {
8884                                 ql_log(ql_log_warn, vha, 0x8001,
8885                                     "Unable to reinitialize FCE (%d).\n",
8886                                     rval);
8887                                 ha->flags.fce_enabled = 0;
8888                         }
8889                 }
8890
8891                 if (ha->eft) {
8892                         memset(ha->eft, 0, EFT_SIZE);
8893                         rval = qla2x00_enable_eft_trace(vha,
8894                             ha->eft_dma, EFT_NUM_BUFFERS);
8895                         if (rval) {
8896                                 ql_log(ql_log_warn, vha, 0x8010,
8897                                     "Unable to reinitialize EFT (%d).\n",
8898                                     rval);
8899                         }
8900                 }
8901         }
8902
8903         if (!status) {
8904                 ql_dbg(ql_dbg_taskm, vha, 0x8011,
8905                     "qla82xx_restart_isp succeeded.\n");
8906
8907                 spin_lock_irqsave(&ha->vport_slock, flags);
8908                 list_for_each_entry(vp, &ha->vp_list, list) {
8909                         if (vp->vp_idx) {
8910                                 atomic_inc(&vp->vref_count);
8911                                 spin_unlock_irqrestore(&ha->vport_slock, flags);
8912
8913                                 qla2x00_vp_abort_isp(vp);
8914
8915                                 spin_lock_irqsave(&ha->vport_slock, flags);
8916                                 atomic_dec(&vp->vref_count);
8917                         }
8918                 }
8919                 spin_unlock_irqrestore(&ha->vport_slock, flags);
8920
8921         } else {
8922                 ql_log(ql_log_warn, vha, 0x8016,
8923                     "qla82xx_restart_isp **** FAILED ****.\n");
8924         }
8925
8926         return status;
8927 }
8928
8929 /*
8930  * qla24xx_get_fcp_prio
8931  *      Gets the fcp cmd priority value for the logged in port.
8932  *      Looks for a match of the port descriptors within
8933  *      each of the fcp prio config entries. If a match is found,
8934  *      the tag (priority) value is returned.
8935  *
8936  * Input:
8937  *      vha = scsi host structure pointer.
8938  *      fcport = port structure pointer.
8939  *
8940  * Return:
8941  *      non-zero (if found)
8942  *      -1 (if not found)
8943  *
8944  * Context:
8945  *      Kernel context
8946  */
8947 static int
8948 qla24xx_get_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
8949 {
8950         int i, entries;
8951         uint8_t pid_match, wwn_match;
8952         int priority;
8953         uint32_t pid1, pid2;
8954         uint64_t wwn1, wwn2;
8955         struct qla_fcp_prio_entry *pri_entry;
8956         struct qla_hw_data *ha = vha->hw;
8957
8958         if (!ha->fcp_prio_cfg || !ha->flags.fcp_prio_enabled)
8959                 return -1;
8960
8961         priority = -1;
8962         entries = ha->fcp_prio_cfg->num_entries;
8963         pri_entry = &ha->fcp_prio_cfg->entry[0];
8964
8965         for (i = 0; i < entries; i++) {
8966                 pid_match = wwn_match = 0;
8967
8968                 if (!(pri_entry->flags & FCP_PRIO_ENTRY_VALID)) {
8969                         pri_entry++;
8970                         continue;
8971                 }
8972
8973                 /* check source pid for a match */
8974                 if (pri_entry->flags & FCP_PRIO_ENTRY_SPID_VALID) {
8975                         pid1 = pri_entry->src_pid & INVALID_PORT_ID;
8976                         pid2 = vha->d_id.b24 & INVALID_PORT_ID;
8977                         if (pid1 == INVALID_PORT_ID)
8978                                 pid_match++;
8979                         else if (pid1 == pid2)
8980                                 pid_match++;
8981                 }
8982
8983                 /* check destination pid for a match */
8984                 if (pri_entry->flags & FCP_PRIO_ENTRY_DPID_VALID) {
8985                         pid1 = pri_entry->dst_pid & INVALID_PORT_ID;
8986                         pid2 = fcport->d_id.b24 & INVALID_PORT_ID;
8987                         if (pid1 == INVALID_PORT_ID)
8988                                 pid_match++;
8989                         else if (pid1 == pid2)
8990                                 pid_match++;
8991                 }
8992
8993                 /* check source WWN for a match */
8994                 if (pri_entry->flags & FCP_PRIO_ENTRY_SWWN_VALID) {
8995                         wwn1 = wwn_to_u64(vha->port_name);
8996                         wwn2 = wwn_to_u64(pri_entry->src_wwpn);
8997                         if (wwn2 == (uint64_t)-1)
8998                                 wwn_match++;
8999                         else if (wwn1 == wwn2)
9000                                 wwn_match++;
9001                 }
9002
9003                 /* check destination WWN for a match */
9004                 if (pri_entry->flags & FCP_PRIO_ENTRY_DWWN_VALID) {
9005                         wwn1 = wwn_to_u64(fcport->port_name);
9006                         wwn2 = wwn_to_u64(pri_entry->dst_wwpn);
9007                         if (wwn2 == (uint64_t)-1)
9008                                 wwn_match++;
9009                         else if (wwn1 == wwn2)
9010                                 wwn_match++;
9011                 }
9012
9013                 if (pid_match == 2 || wwn_match == 2) {
9014                         /* Found a matching entry */
9015                         if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
9016                                 priority = pri_entry->tag;
9017                         break;
9018                 }
9019
9020                 pri_entry++;
9021         }
9022
9023         return priority;
9024 }
9025
9026 /*
9027  * qla24xx_update_fcport_fcp_prio
9028  *      Activates fcp priority for the logged in fc port
9029  *
9030  * Input:
9031  *      vha = scsi host structure pointer.
9032  *      fcp = port structure pointer.
9033  *
9034  * Return:
9035  *      QLA_SUCCESS or QLA_FUNCTION_FAILED
9036  *
9037  * Context:
9038  *      Kernel context.
9039  */
9040 int
9041 qla24xx_update_fcport_fcp_prio(scsi_qla_host_t *vha, fc_port_t *fcport)
9042 {
9043         int ret;
9044         int priority;
9045         uint16_t mb[5];
9046
9047         if (fcport->port_type != FCT_TARGET ||
9048             fcport->loop_id == FC_NO_LOOP_ID)
9049                 return QLA_FUNCTION_FAILED;
9050
9051         priority = qla24xx_get_fcp_prio(vha, fcport);
9052         if (priority < 0)
9053                 return QLA_FUNCTION_FAILED;
9054
9055         if (IS_P3P_TYPE(vha->hw)) {
9056                 fcport->fcp_prio = priority & 0xf;
9057                 return QLA_SUCCESS;
9058         }
9059
9060         ret = qla24xx_set_fcp_prio(vha, fcport->loop_id, priority, mb);
9061         if (ret == QLA_SUCCESS) {
9062                 if (fcport->fcp_prio != priority)
9063                         ql_dbg(ql_dbg_user, vha, 0x709e,
9064                             "Updated FCP_CMND priority - value=%d loop_id=%d "
9065                             "port_id=%02x%02x%02x.\n", priority,
9066                             fcport->loop_id, fcport->d_id.b.domain,
9067                             fcport->d_id.b.area, fcport->d_id.b.al_pa);
9068                 fcport->fcp_prio = priority & 0xf;
9069         } else
9070                 ql_dbg(ql_dbg_user, vha, 0x704f,
9071                     "Unable to update FCP_CMND priority - ret=0x%x for "
9072                     "loop_id=%d port_id=%02x%02x%02x.\n", ret, fcport->loop_id,
9073                     fcport->d_id.b.domain, fcport->d_id.b.area,
9074                     fcport->d_id.b.al_pa);
9075         return  ret;
9076 }
9077
9078 /*
9079  * qla24xx_update_all_fcp_prio
9080  *      Activates fcp priority for all the logged in ports
9081  *
9082  * Input:
9083  *      ha = adapter block pointer.
9084  *
9085  * Return:
9086  *      QLA_SUCCESS or QLA_FUNCTION_FAILED
9087  *
9088  * Context:
9089  *      Kernel context.
9090  */
9091 int
9092 qla24xx_update_all_fcp_prio(scsi_qla_host_t *vha)
9093 {
9094         int ret;
9095         fc_port_t *fcport;
9096
9097         ret = QLA_FUNCTION_FAILED;
9098         /* We need to set priority for all logged in ports */
9099         list_for_each_entry(fcport, &vha->vp_fcports, list)
9100                 ret = qla24xx_update_fcport_fcp_prio(vha, fcport);
9101
9102         return ret;
9103 }
9104
9105 struct qla_qpair *qla2xxx_create_qpair(struct scsi_qla_host *vha, int qos,
9106         int vp_idx, bool startqp)
9107 {
9108         int rsp_id = 0;
9109         int  req_id = 0;
9110         int i;
9111         struct qla_hw_data *ha = vha->hw;
9112         uint16_t qpair_id = 0;
9113         struct qla_qpair *qpair = NULL;
9114         struct qla_msix_entry *msix;
9115
9116         if (!(ha->fw_attributes & BIT_6) || !ha->flags.msix_enabled) {
9117                 ql_log(ql_log_warn, vha, 0x00181,
9118                     "FW/Driver is not multi-queue capable.\n");
9119                 return NULL;
9120         }
9121
9122         if (ql2xmqsupport || ql2xnvmeenable) {
9123                 qpair = kzalloc(sizeof(struct qla_qpair), GFP_KERNEL);
9124                 if (qpair == NULL) {
9125                         ql_log(ql_log_warn, vha, 0x0182,
9126                             "Failed to allocate memory for queue pair.\n");
9127                         return NULL;
9128                 }
9129
9130                 qpair->hw = vha->hw;
9131                 qpair->vha = vha;
9132                 qpair->qp_lock_ptr = &qpair->qp_lock;
9133                 spin_lock_init(&qpair->qp_lock);
9134                 qpair->use_shadow_reg = IS_SHADOW_REG_CAPABLE(ha) ? 1 : 0;
9135
9136                 /* Assign available que pair id */
9137                 mutex_lock(&ha->mq_lock);
9138                 qpair_id = find_first_zero_bit(ha->qpair_qid_map, ha->max_qpairs);
9139                 if (ha->num_qpairs >= ha->max_qpairs) {
9140                         mutex_unlock(&ha->mq_lock);
9141                         ql_log(ql_log_warn, vha, 0x0183,
9142                             "No resources to create additional q pair.\n");
9143                         goto fail_qid_map;
9144                 }
9145                 ha->num_qpairs++;
9146                 set_bit(qpair_id, ha->qpair_qid_map);
9147                 ha->queue_pair_map[qpair_id] = qpair;
9148                 qpair->id = qpair_id;
9149                 qpair->vp_idx = vp_idx;
9150                 qpair->fw_started = ha->flags.fw_started;
9151                 INIT_LIST_HEAD(&qpair->hints_list);
9152                 qpair->chip_reset = ha->base_qpair->chip_reset;
9153                 qpair->enable_class_2 = ha->base_qpair->enable_class_2;
9154                 qpair->enable_explicit_conf =
9155                     ha->base_qpair->enable_explicit_conf;
9156
9157                 for (i = 0; i < ha->msix_count; i++) {
9158                         msix = &ha->msix_entries[i];
9159                         if (msix->in_use)
9160                                 continue;
9161                         qpair->msix = msix;
9162                         ql_dbg(ql_dbg_multiq, vha, 0xc00f,
9163                             "Vector %x selected for qpair\n", msix->vector);
9164                         break;
9165                 }
9166                 if (!qpair->msix) {
9167                         ql_log(ql_log_warn, vha, 0x0184,
9168                             "Out of MSI-X vectors!.\n");
9169                         goto fail_msix;
9170                 }
9171
9172                 qpair->msix->in_use = 1;
9173                 list_add_tail(&qpair->qp_list_elem, &vha->qp_list);
9174                 qpair->pdev = ha->pdev;
9175                 if (IS_QLA27XX(ha) || IS_QLA83XX(ha) || IS_QLA28XX(ha))
9176                         qpair->reqq_start_iocbs = qla_83xx_start_iocbs;
9177
9178                 mutex_unlock(&ha->mq_lock);
9179
9180                 /* Create response queue first */
9181                 rsp_id = qla25xx_create_rsp_que(ha, 0, 0, 0, qpair, startqp);
9182                 if (!rsp_id) {
9183                         ql_log(ql_log_warn, vha, 0x0185,
9184                             "Failed to create response queue.\n");
9185                         goto fail_rsp;
9186                 }
9187
9188                 qpair->rsp = ha->rsp_q_map[rsp_id];
9189
9190                 /* Create request queue */
9191                 req_id = qla25xx_create_req_que(ha, 0, vp_idx, 0, rsp_id, qos,
9192                     startqp);
9193                 if (!req_id) {
9194                         ql_log(ql_log_warn, vha, 0x0186,
9195                             "Failed to create request queue.\n");
9196                         goto fail_req;
9197                 }
9198
9199                 qpair->req = ha->req_q_map[req_id];
9200                 qpair->rsp->req = qpair->req;
9201                 qpair->rsp->qpair = qpair;
9202                 /* init qpair to this cpu. Will adjust at run time. */
9203                 qla_cpu_update(qpair, raw_smp_processor_id());
9204
9205                 if (IS_T10_PI_CAPABLE(ha) && ql2xenabledif) {
9206                         if (ha->fw_attributes & BIT_4)
9207                                 qpair->difdix_supported = 1;
9208                 }
9209
9210                 qpair->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
9211                 if (!qpair->srb_mempool) {
9212                         ql_log(ql_log_warn, vha, 0xd036,
9213                             "Failed to create srb mempool for qpair %d\n",
9214                             qpair->id);
9215                         goto fail_mempool;
9216                 }
9217
9218                 /* Mark as online */
9219                 qpair->online = 1;
9220
9221                 if (!vha->flags.qpairs_available)
9222                         vha->flags.qpairs_available = 1;
9223
9224                 ql_dbg(ql_dbg_multiq, vha, 0xc00d,
9225                     "Request/Response queue pair created, id %d\n",
9226                     qpair->id);
9227                 ql_dbg(ql_dbg_init, vha, 0x0187,
9228                     "Request/Response queue pair created, id %d\n",
9229                     qpair->id);
9230         }
9231         return qpair;
9232
9233 fail_mempool:
9234 fail_req:
9235         qla25xx_delete_rsp_que(vha, qpair->rsp);
9236 fail_rsp:
9237         mutex_lock(&ha->mq_lock);
9238         qpair->msix->in_use = 0;
9239         list_del(&qpair->qp_list_elem);
9240         if (list_empty(&vha->qp_list))
9241                 vha->flags.qpairs_available = 0;
9242 fail_msix:
9243         ha->queue_pair_map[qpair_id] = NULL;
9244         clear_bit(qpair_id, ha->qpair_qid_map);
9245         ha->num_qpairs--;
9246         mutex_unlock(&ha->mq_lock);
9247 fail_qid_map:
9248         kfree(qpair);
9249         return NULL;
9250 }
9251
9252 int qla2xxx_delete_qpair(struct scsi_qla_host *vha, struct qla_qpair *qpair)
9253 {
9254         int ret = QLA_FUNCTION_FAILED;
9255         struct qla_hw_data *ha = qpair->hw;
9256
9257         qpair->delete_in_progress = 1;
9258
9259         ret = qla25xx_delete_req_que(vha, qpair->req);
9260         if (ret != QLA_SUCCESS)
9261                 goto fail;
9262
9263         ret = qla25xx_delete_rsp_que(vha, qpair->rsp);
9264         if (ret != QLA_SUCCESS)
9265                 goto fail;
9266
9267         mutex_lock(&ha->mq_lock);
9268         ha->queue_pair_map[qpair->id] = NULL;
9269         clear_bit(qpair->id, ha->qpair_qid_map);
9270         ha->num_qpairs--;
9271         list_del(&qpair->qp_list_elem);
9272         if (list_empty(&vha->qp_list)) {
9273                 vha->flags.qpairs_available = 0;
9274                 vha->flags.qpairs_req_created = 0;
9275                 vha->flags.qpairs_rsp_created = 0;
9276         }
9277         mempool_destroy(qpair->srb_mempool);
9278         kfree(qpair);
9279         mutex_unlock(&ha->mq_lock);
9280
9281         return QLA_SUCCESS;
9282 fail:
9283         return ret;
9284 }