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