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