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