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