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