scsi: qla2xxx: Use tabs to indent code
[muen/linux.git] / drivers / scsi / qla2xxx / qla_init.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2014 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8 #include "qla_gbl.h"
9
10 #include <linux/delay.h>
11 #include <linux/slab.h>
12 #include <linux/vmalloc.h>
13
14 #include "qla_devtbl.h"
15
16 #ifdef CONFIG_SPARC
17 #include <asm/prom.h>
18 #endif
19
20 #include <target/target_core_base.h>
21 #include "qla_target.h"
22
23 /*
24 *  QLogic ISP2x00 Hardware Support Function Prototypes.
25 */
26 static int qla2x00_isp_firmware(scsi_qla_host_t *);
27 static int qla2x00_setup_chip(scsi_qla_host_t *);
28 static int qla2x00_fw_ready(scsi_qla_host_t *);
29 static int qla2x00_configure_hba(scsi_qla_host_t *);
30 static int qla2x00_configure_loop(scsi_qla_host_t *);
31 static int qla2x00_configure_local_loop(scsi_qla_host_t *);
32 static int qla2x00_configure_fabric(scsi_qla_host_t *);
33 static int qla2x00_find_all_fabric_devs(scsi_qla_host_t *);
34 static int qla2x00_restart_isp(scsi_qla_host_t *);
35
36 static struct qla_chip_state_84xx *qla84xx_get_chip(struct scsi_qla_host *);
37 static int qla84xx_init_chip(scsi_qla_host_t *);
38 static int qla25xx_init_queues(struct qla_hw_data *);
39 static int qla24xx_post_prli_work(struct scsi_qla_host*, fc_port_t *);
40 static void qla24xx_handle_plogi_done_event(struct scsi_qla_host *,
41     struct event_arg *);
42 static void qla24xx_handle_prli_done_event(struct scsi_qla_host *,
43     struct event_arg *);
44 static void __qla24xx_handle_gpdb_event(scsi_qla_host_t *, struct event_arg *);
45
46 /* SRB Extensions ---------------------------------------------------------- */
47
48 void
49 qla2x00_sp_timeout(struct timer_list *t)
50 {
51         srb_t *sp = from_timer(sp, t, u.iocb_cmd.timer);
52         struct srb_iocb *iocb;
53         struct req_que *req;
54         unsigned long flags;
55         struct qla_hw_data *ha = sp->vha->hw;
56
57         WARN_ON_ONCE(irqs_disabled());
58         spin_lock_irqsave(&ha->hardware_lock, flags);
59         req = sp->qpair->req;
60         req->outstanding_cmds[sp->handle] = NULL;
61         iocb = &sp->u.iocb_cmd;
62         spin_unlock_irqrestore(&ha->hardware_lock, flags);
63         iocb->timeout(sp);
64 }
65
66 void
67 qla2x00_sp_free(void *ptr)
68 {
69         srb_t *sp = ptr;
70         struct srb_iocb *iocb = &sp->u.iocb_cmd;
71
72         del_timer(&iocb->timer);
73         qla2x00_rel_sp(sp);
74 }
75
76 /* Asynchronous Login/Logout Routines -------------------------------------- */
77
78 unsigned long
79 qla2x00_get_async_timeout(struct scsi_qla_host *vha)
80 {
81         unsigned long tmo;
82         struct qla_hw_data *ha = vha->hw;
83
84         /* Firmware should use switch negotiated r_a_tov for timeout. */
85         tmo = ha->r_a_tov / 10 * 2;
86         if (IS_QLAFX00(ha)) {
87                 tmo = FX00_DEF_RATOV * 2;
88         } else if (!IS_FWI2_CAPABLE(ha)) {
89                 /*
90                  * Except for earlier ISPs where the timeout is seeded from the
91                  * initialization control block.
92                  */
93                 tmo = ha->login_timeout;
94         }
95         return tmo;
96 }
97
98 void
99 qla2x00_async_iocb_timeout(void *data)
100 {
101         srb_t *sp = data;
102         fc_port_t *fcport = sp->fcport;
103         struct srb_iocb *lio = &sp->u.iocb_cmd;
104         int rc, h;
105         unsigned long flags;
106
107         if (fcport) {
108                 ql_dbg(ql_dbg_disc, fcport->vha, 0x2071,
109                     "Async-%s timeout - hdl=%x portid=%06x %8phC.\n",
110                     sp->name, sp->handle, fcport->d_id.b24, fcport->port_name);
111
112                 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
113         } else {
114                 pr_info("Async-%s timeout - hdl=%x.\n",
115                     sp->name, sp->handle);
116         }
117
118         switch (sp->type) {
119         case SRB_LOGIN_CMD:
120                 rc = qla24xx_async_abort_cmd(sp, false);
121                 if (rc) {
122                         /* Retry as needed. */
123                         lio->u.logio.data[0] = MBS_COMMAND_ERROR;
124                         lio->u.logio.data[1] =
125                                 lio->u.logio.flags & SRB_LOGIN_RETRIED ?
126                                 QLA_LOGIO_LOGIN_RETRIED : 0;
127                         spin_lock_irqsave(sp->qpair->qp_lock_ptr, flags);
128                         for (h = 1; h < sp->qpair->req->num_outstanding_cmds;
129                             h++) {
130                                 if (sp->qpair->req->outstanding_cmds[h] ==
131                                     sp) {
132                                         sp->qpair->req->outstanding_cmds[h] =
133                                             NULL;
134                                         break;
135                                 }
136                         }
137                         spin_unlock_irqrestore(sp->qpair->qp_lock_ptr, flags);
138                         sp->done(sp, QLA_FUNCTION_TIMEOUT);
139                 }
140                 break;
141         case SRB_LOGOUT_CMD:
142         case SRB_CT_PTHRU_CMD:
143         case SRB_MB_IOCB:
144         case SRB_NACK_PLOGI:
145         case SRB_NACK_PRLI:
146         case SRB_NACK_LOGO:
147         case SRB_CTRL_VP:
148                 rc = qla24xx_async_abort_cmd(sp, false);
149                 if (rc) {
150                         spin_lock_irqsave(sp->qpair->qp_lock_ptr, flags);
151                         for (h = 1; h < sp->qpair->req->num_outstanding_cmds;
152                             h++) {
153                                 if (sp->qpair->req->outstanding_cmds[h] ==
154                                     sp) {
155                                         sp->qpair->req->outstanding_cmds[h] =
156                                             NULL;
157                                         break;
158                                 }
159                         }
160                         spin_unlock_irqrestore(sp->qpair->qp_lock_ptr, flags);
161                         sp->done(sp, QLA_FUNCTION_TIMEOUT);
162                 }
163                 break;
164         }
165 }
166
167 static void
168 qla2x00_async_login_sp_done(void *ptr, int res)
169 {
170         srb_t *sp = ptr;
171         struct scsi_qla_host *vha = sp->vha;
172         struct srb_iocb *lio = &sp->u.iocb_cmd;
173         struct event_arg ea;
174
175         ql_dbg(ql_dbg_disc, vha, 0x20dd,
176             "%s %8phC res %d \n", __func__, sp->fcport->port_name, res);
177
178         sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
179
180         if (!test_bit(UNLOADING, &vha->dpc_flags)) {
181                 memset(&ea, 0, sizeof(ea));
182                 ea.event = FCME_PLOGI_DONE;
183                 ea.fcport = sp->fcport;
184                 ea.data[0] = lio->u.logio.data[0];
185                 ea.data[1] = lio->u.logio.data[1];
186                 ea.iop[0] = lio->u.logio.iop[0];
187                 ea.iop[1] = lio->u.logio.iop[1];
188                 ea.sp = sp;
189                 qla2x00_fcport_event_handler(vha, &ea);
190         }
191
192         sp->free(sp);
193 }
194
195 static inline bool
196 fcport_is_smaller(fc_port_t *fcport)
197 {
198         if (wwn_to_u64(fcport->port_name) <
199             wwn_to_u64(fcport->vha->port_name))
200                 return true;
201         else
202                 return false;
203 }
204
205 static inline bool
206 fcport_is_bigger(fc_port_t *fcport)
207 {
208         return !fcport_is_smaller(fcport);
209 }
210
211 int
212 qla2x00_async_login(struct scsi_qla_host *vha, fc_port_t *fcport,
213     uint16_t *data)
214 {
215         srb_t *sp;
216         struct srb_iocb *lio;
217         int rval = QLA_FUNCTION_FAILED;
218
219         if (!vha->flags.online)
220                 goto done;
221
222         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
223         if (!sp)
224                 goto done;
225
226         fcport->flags |= FCF_ASYNC_SENT;
227         fcport->logout_completed = 0;
228
229         fcport->disc_state = DSC_LOGIN_PEND;
230         sp->type = SRB_LOGIN_CMD;
231         sp->name = "login";
232         sp->gen1 = fcport->rscn_gen;
233         sp->gen2 = fcport->login_gen;
234
235         lio = &sp->u.iocb_cmd;
236         lio->timeout = qla2x00_async_iocb_timeout;
237         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
238
239         sp->done = qla2x00_async_login_sp_done;
240         if (N2N_TOPO(fcport->vha->hw) && fcport_is_bigger(fcport))
241                 lio->u.logio.flags |= SRB_LOGIN_PRLI_ONLY;
242         else
243                 lio->u.logio.flags |= SRB_LOGIN_COND_PLOGI;
244
245         if (fcport->fc4f_nvme)
246                 lio->u.logio.flags |= SRB_LOGIN_SKIP_PRLI;
247
248         ql_dbg(ql_dbg_disc, vha, 0x2072,
249             "Async-login - %8phC hdl=%x, loopid=%x portid=%02x%02x%02x "
250                 "retries=%d.\n", fcport->port_name, sp->handle, fcport->loop_id,
251             fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa,
252             fcport->login_retry);
253
254         rval = qla2x00_start_sp(sp);
255         if (rval != QLA_SUCCESS) {
256                 fcport->flags |= FCF_LOGIN_NEEDED;
257                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
258                 goto done_free_sp;
259         }
260
261         return rval;
262
263 done_free_sp:
264         sp->free(sp);
265         fcport->flags &= ~FCF_ASYNC_SENT;
266 done:
267         fcport->flags &= ~FCF_ASYNC_ACTIVE;
268         return rval;
269 }
270
271 static void
272 qla2x00_async_logout_sp_done(void *ptr, int res)
273 {
274         srb_t *sp = ptr;
275
276         sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
277         sp->fcport->login_gen++;
278         qlt_logo_completion_handler(sp->fcport, res);
279         sp->free(sp);
280 }
281
282 int
283 qla2x00_async_logout(struct scsi_qla_host *vha, fc_port_t *fcport)
284 {
285         srb_t *sp;
286         struct srb_iocb *lio;
287         int rval = QLA_FUNCTION_FAILED;
288
289         if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
290                 return rval;
291
292         fcport->flags |= FCF_ASYNC_SENT;
293         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
294         if (!sp)
295                 goto done;
296
297         sp->type = SRB_LOGOUT_CMD;
298         sp->name = "logout";
299
300         lio = &sp->u.iocb_cmd;
301         lio->timeout = qla2x00_async_iocb_timeout;
302         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
303
304         sp->done = qla2x00_async_logout_sp_done;
305
306         ql_dbg(ql_dbg_disc, vha, 0x2070,
307             "Async-logout - hdl=%x loop-id=%x portid=%02x%02x%02x %8phC.\n",
308             sp->handle, fcport->loop_id, fcport->d_id.b.domain,
309                 fcport->d_id.b.area, fcport->d_id.b.al_pa,
310                 fcport->port_name);
311
312         rval = qla2x00_start_sp(sp);
313         if (rval != QLA_SUCCESS)
314                 goto done_free_sp;
315         return rval;
316
317 done_free_sp:
318         sp->free(sp);
319 done:
320         fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
321         return rval;
322 }
323
324 void
325 qla2x00_async_prlo_done(struct scsi_qla_host *vha, fc_port_t *fcport,
326     uint16_t *data)
327 {
328         fcport->flags &= ~FCF_ASYNC_ACTIVE;
329         /* Don't re-login in target mode */
330         if (!fcport->tgt_session)
331                 qla2x00_mark_device_lost(vha, fcport, 1, 0);
332         qlt_logo_completion_handler(fcport, data[0]);
333 }
334
335 static void
336 qla2x00_async_prlo_sp_done(void *s, int res)
337 {
338         srb_t *sp = (srb_t *)s;
339         struct srb_iocb *lio = &sp->u.iocb_cmd;
340         struct scsi_qla_host *vha = sp->vha;
341
342         sp->fcport->flags &= ~FCF_ASYNC_ACTIVE;
343         if (!test_bit(UNLOADING, &vha->dpc_flags))
344                 qla2x00_post_async_prlo_done_work(sp->fcport->vha, sp->fcport,
345                     lio->u.logio.data);
346         sp->free(sp);
347 }
348
349 int
350 qla2x00_async_prlo(struct scsi_qla_host *vha, fc_port_t *fcport)
351 {
352         srb_t *sp;
353         struct srb_iocb *lio;
354         int rval;
355
356         rval = QLA_FUNCTION_FAILED;
357         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
358         if (!sp)
359                 goto done;
360
361         sp->type = SRB_PRLO_CMD;
362         sp->name = "prlo";
363
364         lio = &sp->u.iocb_cmd;
365         lio->timeout = qla2x00_async_iocb_timeout;
366         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
367
368         sp->done = qla2x00_async_prlo_sp_done;
369
370         ql_dbg(ql_dbg_disc, vha, 0x2070,
371             "Async-prlo - hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
372             sp->handle, fcport->loop_id, fcport->d_id.b.domain,
373             fcport->d_id.b.area, fcport->d_id.b.al_pa);
374
375         rval = qla2x00_start_sp(sp);
376         if (rval != QLA_SUCCESS)
377                 goto done_free_sp;
378
379         return rval;
380
381 done_free_sp:
382         sp->free(sp);
383 done:
384         fcport->flags &= ~FCF_ASYNC_ACTIVE;
385         return rval;
386 }
387
388 static
389 void qla24xx_handle_adisc_event(scsi_qla_host_t *vha, struct event_arg *ea)
390 {
391         struct fc_port *fcport = ea->fcport;
392
393         ql_dbg(ql_dbg_disc, vha, 0x20d2,
394             "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d lid %d\n",
395             __func__, fcport->port_name, fcport->disc_state,
396             fcport->fw_login_state, ea->rc, fcport->login_gen, ea->sp->gen2,
397             fcport->rscn_gen, ea->sp->gen1, fcport->loop_id);
398
399         if (ea->data[0] != MBS_COMMAND_COMPLETE) {
400                 ql_dbg(ql_dbg_disc, vha, 0x2066,
401                     "%s %8phC: adisc fail: post delete\n",
402                     __func__, ea->fcport->port_name);
403                 /* deleted = 0 & logout_on_delete = force fw cleanup */
404                 fcport->deleted = 0;
405                 fcport->logout_on_delete = 1;
406                 qlt_schedule_sess_for_deletion(ea->fcport);
407                 return;
408         }
409
410         if (ea->fcport->disc_state == DSC_DELETE_PEND)
411                 return;
412
413         if (ea->sp->gen2 != ea->fcport->login_gen) {
414                 /* target side must have changed it. */
415                 ql_dbg(ql_dbg_disc, vha, 0x20d3,
416                     "%s %8phC generation changed\n",
417                     __func__, ea->fcport->port_name);
418                 return;
419         } else if (ea->sp->gen1 != ea->fcport->rscn_gen) {
420                 qla_rscn_replay(fcport);
421                 qlt_schedule_sess_for_deletion(fcport);
422                 return;
423         }
424
425         __qla24xx_handle_gpdb_event(vha, ea);
426 }
427
428 static int qla_post_els_plogi_work(struct scsi_qla_host *vha, fc_port_t *fcport)
429 {
430         struct qla_work_evt *e;
431
432         e = qla2x00_alloc_work(vha, QLA_EVT_ELS_PLOGI);
433         if (!e)
434                 return QLA_FUNCTION_FAILED;
435
436         e->u.fcport.fcport = fcport;
437         fcport->flags |= FCF_ASYNC_ACTIVE;
438         return qla2x00_post_work(vha, e);
439 }
440
441 static void
442 qla2x00_async_adisc_sp_done(void *ptr, int res)
443 {
444         srb_t *sp = ptr;
445         struct scsi_qla_host *vha = sp->vha;
446         struct event_arg ea;
447         struct srb_iocb *lio = &sp->u.iocb_cmd;
448
449         ql_dbg(ql_dbg_disc, vha, 0x2066,
450             "Async done-%s res %x %8phC\n",
451             sp->name, res, sp->fcport->port_name);
452
453         sp->fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
454
455         memset(&ea, 0, sizeof(ea));
456         ea.event = FCME_ADISC_DONE;
457         ea.rc = res;
458         ea.data[0] = lio->u.logio.data[0];
459         ea.data[1] = lio->u.logio.data[1];
460         ea.iop[0] = lio->u.logio.iop[0];
461         ea.iop[1] = lio->u.logio.iop[1];
462         ea.fcport = sp->fcport;
463         ea.sp = sp;
464
465         qla2x00_fcport_event_handler(vha, &ea);
466
467         sp->free(sp);
468 }
469
470 int
471 qla2x00_async_adisc(struct scsi_qla_host *vha, fc_port_t *fcport,
472     uint16_t *data)
473 {
474         srb_t *sp;
475         struct srb_iocb *lio;
476         int rval = QLA_FUNCTION_FAILED;
477
478         if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
479                 return rval;
480
481         fcport->flags |= FCF_ASYNC_SENT;
482         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
483         if (!sp)
484                 goto done;
485
486         sp->type = SRB_ADISC_CMD;
487         sp->name = "adisc";
488
489         lio = &sp->u.iocb_cmd;
490         lio->timeout = qla2x00_async_iocb_timeout;
491         sp->gen1 = fcport->rscn_gen;
492         sp->gen2 = fcport->login_gen;
493         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
494
495         sp->done = qla2x00_async_adisc_sp_done;
496         if (data[1] & QLA_LOGIO_LOGIN_RETRIED)
497                 lio->u.logio.flags |= SRB_LOGIN_RETRIED;
498
499         ql_dbg(ql_dbg_disc, vha, 0x206f,
500             "Async-adisc - hdl=%x loopid=%x portid=%06x %8phC.\n",
501             sp->handle, fcport->loop_id, fcport->d_id.b24, fcport->port_name);
502
503         rval = qla2x00_start_sp(sp);
504         if (rval != QLA_SUCCESS)
505                 goto done_free_sp;
506
507         return rval;
508
509 done_free_sp:
510         sp->free(sp);
511 done:
512         fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
513         qla2x00_post_async_adisc_work(vha, fcport, data);
514         return rval;
515 }
516
517 static void qla24xx_handle_gnl_done_event(scsi_qla_host_t *vha,
518         struct event_arg *ea)
519 {
520         fc_port_t *fcport, *conflict_fcport;
521         struct get_name_list_extended *e;
522         u16 i, n, found = 0, loop_id;
523         port_id_t id;
524         u64 wwn;
525         u16 data[2];
526         u8 current_login_state;
527
528         fcport = ea->fcport;
529         ql_dbg(ql_dbg_disc, vha, 0xffff,
530             "%s %8phC DS %d LS rc %d %d login %d|%d rscn %d|%d lid %d\n",
531             __func__, fcport->port_name, fcport->disc_state,
532             fcport->fw_login_state, ea->rc,
533             fcport->login_gen, fcport->last_login_gen,
534             fcport->rscn_gen, fcport->last_rscn_gen, vha->loop_id);
535
536         if (fcport->disc_state == DSC_DELETE_PEND)
537                 return;
538
539         if (ea->rc) { /* rval */
540                 if (fcport->login_retry == 0) {
541                         ql_dbg(ql_dbg_disc, vha, 0x20de,
542                             "GNL failed Port login retry %8phN, retry cnt=%d.\n",
543                             fcport->port_name, fcport->login_retry);
544                 }
545                 return;
546         }
547
548         if (fcport->last_rscn_gen != fcport->rscn_gen) {
549                 qla_rscn_replay(fcport);
550                 qlt_schedule_sess_for_deletion(fcport);
551                 return;
552         } else if (fcport->last_login_gen != fcport->login_gen) {
553                 ql_dbg(ql_dbg_disc, vha, 0x20e0,
554                     "%s %8phC login gen changed\n",
555                     __func__, fcport->port_name);
556                 return;
557         }
558
559         n = ea->data[0] / sizeof(struct get_name_list_extended);
560
561         ql_dbg(ql_dbg_disc, vha, 0x20e1,
562             "%s %d %8phC n %d %02x%02x%02x lid %d \n",
563             __func__, __LINE__, fcport->port_name, n,
564             fcport->d_id.b.domain, fcport->d_id.b.area,
565             fcport->d_id.b.al_pa, fcport->loop_id);
566
567         for (i = 0; i < n; i++) {
568                 e = &vha->gnl.l[i];
569                 wwn = wwn_to_u64(e->port_name);
570                 id.b.domain = e->port_id[2];
571                 id.b.area = e->port_id[1];
572                 id.b.al_pa = e->port_id[0];
573                 id.b.rsvd_1 = 0;
574
575                 if (memcmp((u8 *)&wwn, fcport->port_name, WWN_SIZE))
576                         continue;
577
578                 if (IS_SW_RESV_ADDR(id))
579                         continue;
580
581                 found = 1;
582
583                 loop_id = le16_to_cpu(e->nport_handle);
584                 loop_id = (loop_id & 0x7fff);
585                 if  (fcport->fc4f_nvme)
586                         current_login_state = e->current_login_state >> 4;
587                 else
588                         current_login_state = e->current_login_state & 0xf;
589
590
591                 ql_dbg(ql_dbg_disc, vha, 0x20e2,
592                     "%s found %8phC CLS [%x|%x] nvme %d ID[%02x%02x%02x|%02x%02x%02x] lid[%d|%d]\n",
593                     __func__, fcport->port_name,
594                     e->current_login_state, fcport->fw_login_state,
595                     fcport->fc4f_nvme, id.b.domain, id.b.area, id.b.al_pa,
596                     fcport->d_id.b.domain, fcport->d_id.b.area,
597                     fcport->d_id.b.al_pa, loop_id, fcport->loop_id);
598
599                 switch (fcport->disc_state) {
600                 case DSC_DELETE_PEND:
601                 case DSC_DELETED:
602                         break;
603                 default:
604                         if ((id.b24 != fcport->d_id.b24 &&
605                             fcport->d_id.b24) ||
606                             (fcport->loop_id != FC_NO_LOOP_ID &&
607                                 fcport->loop_id != loop_id)) {
608                                 ql_dbg(ql_dbg_disc, vha, 0x20e3,
609                                     "%s %d %8phC post del sess\n",
610                                     __func__, __LINE__, fcport->port_name);
611                                 qlt_schedule_sess_for_deletion(fcport);
612                                 return;
613                         }
614                         break;
615                 }
616
617                 fcport->loop_id = loop_id;
618
619                 wwn = wwn_to_u64(fcport->port_name);
620                 qlt_find_sess_invalidate_other(vha, wwn,
621                         id, loop_id, &conflict_fcport);
622
623                 if (conflict_fcport) {
624                         /*
625                          * Another share fcport share the same loop_id &
626                          * nport id. Conflict fcport needs to finish
627                          * cleanup before this fcport can proceed to login.
628                          */
629                         conflict_fcport->conflict = fcport;
630                         fcport->login_pause = 1;
631                 }
632
633                 switch (vha->hw->current_topology) {
634                 default:
635                         switch (current_login_state) {
636                         case DSC_LS_PRLI_COMP:
637                                 ql_dbg(ql_dbg_disc + ql_dbg_verbose,
638                                     vha, 0x20e4, "%s %d %8phC post gpdb\n",
639                                     __func__, __LINE__, fcport->port_name);
640
641                                 if ((e->prli_svc_param_word_3[0] & BIT_4) == 0)
642                                         fcport->port_type = FCT_INITIATOR;
643                                 else
644                                         fcport->port_type = FCT_TARGET;
645                                 data[0] = data[1] = 0;
646                                 qla2x00_post_async_adisc_work(vha, fcport,
647                                     data);
648                                 break;
649                         case DSC_LS_PORT_UNAVAIL:
650                         default:
651                                 if (fcport->loop_id == FC_NO_LOOP_ID) {
652                                         qla2x00_find_new_loop_id(vha, fcport);
653                                         fcport->fw_login_state =
654                                             DSC_LS_PORT_UNAVAIL;
655                                 }
656                                 ql_dbg(ql_dbg_disc, vha, 0x20e5,
657                                     "%s %d %8phC\n", __func__, __LINE__,
658                                     fcport->port_name);
659                                 qla24xx_fcport_handle_login(vha, fcport);
660                                 break;
661                         }
662                         break;
663                 case ISP_CFG_N:
664                         fcport->fw_login_state = current_login_state;
665                         fcport->d_id = id;
666                         switch (current_login_state) {
667                         case DSC_LS_PRLI_COMP:
668                                 if ((e->prli_svc_param_word_3[0] & BIT_4) == 0)
669                                         fcport->port_type = FCT_INITIATOR;
670                                 else
671                                         fcport->port_type = FCT_TARGET;
672
673                                 data[0] = data[1] = 0;
674                                 qla2x00_post_async_adisc_work(vha, fcport,
675                                     data);
676                                 break;
677                         case DSC_LS_PLOGI_COMP:
678                                 if (fcport_is_bigger(fcport)) {
679                                         /* local adapter is smaller */
680                                         if (fcport->loop_id != FC_NO_LOOP_ID)
681                                                 qla2x00_clear_loop_id(fcport);
682
683                                         fcport->loop_id = loop_id;
684                                         qla24xx_fcport_handle_login(vha,
685                                             fcport);
686                                         break;
687                                 }
688                                 /* fall through */
689                         default:
690                                 if (fcport_is_smaller(fcport)) {
691                                         /* local adapter is bigger */
692                                         if (fcport->loop_id != FC_NO_LOOP_ID)
693                                                 qla2x00_clear_loop_id(fcport);
694
695                                         fcport->loop_id = loop_id;
696                                         qla24xx_fcport_handle_login(vha,
697                                             fcport);
698                                 }
699                                 break;
700                         }
701                         break;
702                 } /* switch (ha->current_topology) */
703         }
704
705         if (!found) {
706                 switch (vha->hw->current_topology) {
707                 case ISP_CFG_F:
708                 case ISP_CFG_FL:
709                         for (i = 0; i < n; i++) {
710                                 e = &vha->gnl.l[i];
711                                 id.b.domain = e->port_id[0];
712                                 id.b.area = e->port_id[1];
713                                 id.b.al_pa = e->port_id[2];
714                                 id.b.rsvd_1 = 0;
715                                 loop_id = le16_to_cpu(e->nport_handle);
716
717                                 if (fcport->d_id.b24 == id.b24) {
718                                         conflict_fcport =
719                                             qla2x00_find_fcport_by_wwpn(vha,
720                                                 e->port_name, 0);
721                                         if (conflict_fcport) {
722                                                 ql_dbg(ql_dbg_disc + ql_dbg_verbose,
723                                                     vha, 0x20e5,
724                                                     "%s %d %8phC post del sess\n",
725                                                     __func__, __LINE__,
726                                                     conflict_fcport->port_name);
727                                                 qlt_schedule_sess_for_deletion
728                                                         (conflict_fcport);
729                                         }
730                                 }
731                                 /*
732                                  * FW already picked this loop id for
733                                  * another fcport
734                                  */
735                                 if (fcport->loop_id == loop_id)
736                                         fcport->loop_id = FC_NO_LOOP_ID;
737                         }
738                         qla24xx_fcport_handle_login(vha, fcport);
739                         break;
740                 case ISP_CFG_N:
741                         fcport->disc_state = DSC_DELETED;
742                         if (time_after_eq(jiffies, fcport->dm_login_expire)) {
743                                 if (fcport->n2n_link_reset_cnt < 2) {
744                                         fcport->n2n_link_reset_cnt++;
745                                         /*
746                                          * remote port is not sending PLOGI.
747                                          * Reset link to kick start his state
748                                          * machine
749                                          */
750                                         set_bit(N2N_LINK_RESET,
751                                             &vha->dpc_flags);
752                                 } else {
753                                         if (fcport->n2n_chip_reset < 1) {
754                                                 ql_log(ql_log_info, vha, 0x705d,
755                                                     "Chip reset to bring laser down");
756                                                 set_bit(ISP_ABORT_NEEDED,
757                                                     &vha->dpc_flags);
758                                                 fcport->n2n_chip_reset++;
759                                         } else {
760                                                 ql_log(ql_log_info, vha, 0x705d,
761                                                     "Remote port %8ph is not coming back\n",
762                                                     fcport->port_name);
763                                                 fcport->scan_state = 0;
764                                         }
765                                 }
766                                 qla2xxx_wake_dpc(vha);
767                         } else {
768                                 /*
769                                  * report port suppose to do PLOGI. Give him
770                                  * more time. FW will catch it.
771                                  */
772                                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
773                         }
774                         break;
775                 default:
776                         break;
777                 }
778         }
779 } /* gnl_event */
780
781 static void
782 qla24xx_async_gnl_sp_done(void *s, int res)
783 {
784         struct srb *sp = s;
785         struct scsi_qla_host *vha = sp->vha;
786         unsigned long flags;
787         struct fc_port *fcport = NULL, *tf;
788         u16 i, n = 0, loop_id;
789         struct event_arg ea;
790         struct get_name_list_extended *e;
791         u64 wwn;
792         struct list_head h;
793         bool found = false;
794
795         ql_dbg(ql_dbg_disc, vha, 0x20e7,
796             "Async done-%s res %x mb[1]=%x mb[2]=%x \n",
797             sp->name, res, sp->u.iocb_cmd.u.mbx.in_mb[1],
798             sp->u.iocb_cmd.u.mbx.in_mb[2]);
799
800         if (res == QLA_FUNCTION_TIMEOUT)
801                 return;
802
803         sp->fcport->flags &= ~(FCF_ASYNC_SENT|FCF_ASYNC_ACTIVE);
804         memset(&ea, 0, sizeof(ea));
805         ea.sp = sp;
806         ea.rc = res;
807         ea.event = FCME_GNL_DONE;
808
809         if (sp->u.iocb_cmd.u.mbx.in_mb[1] >=
810             sizeof(struct get_name_list_extended)) {
811                 n = sp->u.iocb_cmd.u.mbx.in_mb[1] /
812                     sizeof(struct get_name_list_extended);
813                 ea.data[0] = sp->u.iocb_cmd.u.mbx.in_mb[1]; /* amnt xfered */
814         }
815
816         for (i = 0; i < n; i++) {
817                 e = &vha->gnl.l[i];
818                 loop_id = le16_to_cpu(e->nport_handle);
819                 /* mask out reserve bit */
820                 loop_id = (loop_id & 0x7fff);
821                 set_bit(loop_id, vha->hw->loop_id_map);
822                 wwn = wwn_to_u64(e->port_name);
823
824                 ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x20e8,
825                     "%s %8phC %02x:%02x:%02x state %d/%d lid %x \n",
826                     __func__, (void *)&wwn, e->port_id[2], e->port_id[1],
827                     e->port_id[0], e->current_login_state, e->last_login_state,
828                     (loop_id & 0x7fff));
829         }
830
831         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
832
833         INIT_LIST_HEAD(&h);
834         fcport = tf = NULL;
835         if (!list_empty(&vha->gnl.fcports))
836                 list_splice_init(&vha->gnl.fcports, &h);
837         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
838
839         list_for_each_entry_safe(fcport, tf, &h, gnl_entry) {
840                 list_del_init(&fcport->gnl_entry);
841                 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
842                 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
843                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
844                 ea.fcport = fcport;
845
846                 qla2x00_fcport_event_handler(vha, &ea);
847         }
848
849         /* create new fcport if fw has knowledge of new sessions */
850         for (i = 0; i < n; i++) {
851                 port_id_t id;
852                 u64 wwnn;
853
854                 e = &vha->gnl.l[i];
855                 wwn = wwn_to_u64(e->port_name);
856
857                 found = false;
858                 list_for_each_entry_safe(fcport, tf, &vha->vp_fcports, list) {
859                         if (!memcmp((u8 *)&wwn, fcport->port_name,
860                             WWN_SIZE)) {
861                                 found = true;
862                                 break;
863                         }
864                 }
865
866                 id.b.domain = e->port_id[2];
867                 id.b.area = e->port_id[1];
868                 id.b.al_pa = e->port_id[0];
869                 id.b.rsvd_1 = 0;
870
871                 if (!found && wwn && !IS_SW_RESV_ADDR(id)) {
872                         ql_dbg(ql_dbg_disc, vha, 0x2065,
873                             "%s %d %8phC %06x post new sess\n",
874                             __func__, __LINE__, (u8 *)&wwn, id.b24);
875                         wwnn = wwn_to_u64(e->node_name);
876                         qla24xx_post_newsess_work(vha, &id, (u8 *)&wwn,
877                             (u8 *)&wwnn, NULL, FC4_TYPE_UNKNOWN);
878                 }
879         }
880
881         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
882         vha->gnl.sent = 0;
883         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
884
885         sp->free(sp);
886 }
887
888 int qla24xx_async_gnl(struct scsi_qla_host *vha, fc_port_t *fcport)
889 {
890         srb_t *sp;
891         struct srb_iocb *mbx;
892         int rval = QLA_FUNCTION_FAILED;
893         unsigned long flags;
894         u16 *mb;
895
896         if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
897                 return rval;
898
899         ql_dbg(ql_dbg_disc, vha, 0x20d9,
900             "Async-gnlist WWPN %8phC \n", fcport->port_name);
901
902         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
903         fcport->flags |= FCF_ASYNC_SENT;
904         fcport->disc_state = DSC_GNL;
905         fcport->last_rscn_gen = fcport->rscn_gen;
906         fcport->last_login_gen = fcport->login_gen;
907
908         list_add_tail(&fcport->gnl_entry, &vha->gnl.fcports);
909         if (vha->gnl.sent) {
910                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
911                 return QLA_SUCCESS;
912         }
913         vha->gnl.sent = 1;
914         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
915
916         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
917         if (!sp)
918                 goto done;
919
920         sp->type = SRB_MB_IOCB;
921         sp->name = "gnlist";
922         sp->gen1 = fcport->rscn_gen;
923         sp->gen2 = fcport->login_gen;
924
925         mbx = &sp->u.iocb_cmd;
926         mbx->timeout = qla2x00_async_iocb_timeout;
927         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
928
929         mb = sp->u.iocb_cmd.u.mbx.out_mb;
930         mb[0] = MBC_PORT_NODE_NAME_LIST;
931         mb[1] = BIT_2 | BIT_3;
932         mb[2] = MSW(vha->gnl.ldma);
933         mb[3] = LSW(vha->gnl.ldma);
934         mb[6] = MSW(MSD(vha->gnl.ldma));
935         mb[7] = LSW(MSD(vha->gnl.ldma));
936         mb[8] = vha->gnl.size;
937         mb[9] = vha->vp_idx;
938
939         sp->done = qla24xx_async_gnl_sp_done;
940
941         ql_dbg(ql_dbg_disc, vha, 0x20da,
942             "Async-%s - OUT WWPN %8phC hndl %x\n",
943             sp->name, fcport->port_name, sp->handle);
944
945         rval = qla2x00_start_sp(sp);
946         if (rval != QLA_SUCCESS)
947                 goto done_free_sp;
948
949         return rval;
950
951 done_free_sp:
952         sp->free(sp);
953         fcport->flags &= ~FCF_ASYNC_SENT;
954 done:
955         return rval;
956 }
957
958 int qla24xx_post_gnl_work(struct scsi_qla_host *vha, fc_port_t *fcport)
959 {
960         struct qla_work_evt *e;
961
962         e = qla2x00_alloc_work(vha, QLA_EVT_GNL);
963         if (!e)
964                 return QLA_FUNCTION_FAILED;
965
966         e->u.fcport.fcport = fcport;
967         fcport->flags |= FCF_ASYNC_ACTIVE;
968         return qla2x00_post_work(vha, e);
969 }
970
971 static
972 void qla24xx_async_gpdb_sp_done(void *s, int res)
973 {
974         struct srb *sp = s;
975         struct scsi_qla_host *vha = sp->vha;
976         struct qla_hw_data *ha = vha->hw;
977         fc_port_t *fcport = sp->fcport;
978         u16 *mb = sp->u.iocb_cmd.u.mbx.in_mb;
979         struct event_arg ea;
980
981         ql_dbg(ql_dbg_disc, vha, 0x20db,
982             "Async done-%s res %x, WWPN %8phC mb[1]=%x mb[2]=%x \n",
983             sp->name, res, fcport->port_name, mb[1], mb[2]);
984
985         if (res == QLA_FUNCTION_TIMEOUT) {
986                 dma_pool_free(sp->vha->hw->s_dma_pool, sp->u.iocb_cmd.u.mbx.in,
987                         sp->u.iocb_cmd.u.mbx.in_dma);
988                 return;
989         }
990
991         fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
992         memset(&ea, 0, sizeof(ea));
993         ea.event = FCME_GPDB_DONE;
994         ea.fcport = fcport;
995         ea.sp = sp;
996
997         qla2x00_fcport_event_handler(vha, &ea);
998
999         dma_pool_free(ha->s_dma_pool, sp->u.iocb_cmd.u.mbx.in,
1000                 sp->u.iocb_cmd.u.mbx.in_dma);
1001
1002         sp->free(sp);
1003 }
1004
1005 static int qla24xx_post_prli_work(struct scsi_qla_host *vha, fc_port_t *fcport)
1006 {
1007         struct qla_work_evt *e;
1008
1009         e = qla2x00_alloc_work(vha, QLA_EVT_PRLI);
1010         if (!e)
1011                 return QLA_FUNCTION_FAILED;
1012
1013         e->u.fcport.fcport = fcport;
1014
1015         return qla2x00_post_work(vha, e);
1016 }
1017
1018 static void
1019 qla2x00_async_prli_sp_done(void *ptr, int res)
1020 {
1021         srb_t *sp = ptr;
1022         struct scsi_qla_host *vha = sp->vha;
1023         struct srb_iocb *lio = &sp->u.iocb_cmd;
1024         struct event_arg ea;
1025
1026         ql_dbg(ql_dbg_disc, vha, 0x2129,
1027             "%s %8phC res %d \n", __func__,
1028             sp->fcport->port_name, res);
1029
1030         sp->fcport->flags &= ~FCF_ASYNC_SENT;
1031
1032         if (!test_bit(UNLOADING, &vha->dpc_flags)) {
1033                 memset(&ea, 0, sizeof(ea));
1034                 ea.event = FCME_PRLI_DONE;
1035                 ea.fcport = sp->fcport;
1036                 ea.data[0] = lio->u.logio.data[0];
1037                 ea.data[1] = lio->u.logio.data[1];
1038                 ea.iop[0] = lio->u.logio.iop[0];
1039                 ea.iop[1] = lio->u.logio.iop[1];
1040                 ea.sp = sp;
1041
1042                 qla2x00_fcport_event_handler(vha, &ea);
1043         }
1044
1045         sp->free(sp);
1046 }
1047
1048 int
1049 qla24xx_async_prli(struct scsi_qla_host *vha, fc_port_t *fcport)
1050 {
1051         srb_t *sp;
1052         struct srb_iocb *lio;
1053         int rval = QLA_FUNCTION_FAILED;
1054
1055         if (!vha->flags.online)
1056                 return rval;
1057
1058         if (fcport->fw_login_state == DSC_LS_PLOGI_PEND ||
1059             fcport->fw_login_state == DSC_LS_PRLI_PEND)
1060                 return rval;
1061
1062         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1063         if (!sp)
1064                 return rval;
1065
1066         fcport->flags |= FCF_ASYNC_SENT;
1067         fcport->logout_completed = 0;
1068
1069         sp->type = SRB_PRLI_CMD;
1070         sp->name = "prli";
1071
1072         lio = &sp->u.iocb_cmd;
1073         lio->timeout = qla2x00_async_iocb_timeout;
1074         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
1075
1076         sp->done = qla2x00_async_prli_sp_done;
1077         lio->u.logio.flags = 0;
1078
1079         if  (fcport->fc4f_nvme)
1080                 lio->u.logio.flags |= SRB_LOGIN_NVME_PRLI;
1081
1082         ql_dbg(ql_dbg_disc, vha, 0x211b,
1083             "Async-prli - %8phC hdl=%x, loopid=%x portid=%06x retries=%d %s.\n",
1084             fcport->port_name, sp->handle, fcport->loop_id, fcport->d_id.b24,
1085             fcport->login_retry, fcport->fc4f_nvme ? "nvme" : "fc");
1086
1087         rval = qla2x00_start_sp(sp);
1088         if (rval != QLA_SUCCESS) {
1089                 fcport->flags |= FCF_LOGIN_NEEDED;
1090                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1091                 goto done_free_sp;
1092         }
1093
1094         return rval;
1095
1096 done_free_sp:
1097         sp->free(sp);
1098         fcport->flags &= ~FCF_ASYNC_SENT;
1099         return rval;
1100 }
1101
1102 int qla24xx_post_gpdb_work(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
1103 {
1104         struct qla_work_evt *e;
1105
1106         e = qla2x00_alloc_work(vha, QLA_EVT_GPDB);
1107         if (!e)
1108                 return QLA_FUNCTION_FAILED;
1109
1110         e->u.fcport.fcport = fcport;
1111         e->u.fcport.opt = opt;
1112         fcport->flags |= FCF_ASYNC_ACTIVE;
1113         return qla2x00_post_work(vha, e);
1114 }
1115
1116 int qla24xx_async_gpdb(struct scsi_qla_host *vha, fc_port_t *fcport, u8 opt)
1117 {
1118         srb_t *sp;
1119         struct srb_iocb *mbx;
1120         int rval = QLA_FUNCTION_FAILED;
1121         u16 *mb;
1122         dma_addr_t pd_dma;
1123         struct port_database_24xx *pd;
1124         struct qla_hw_data *ha = vha->hw;
1125
1126         if (!vha->flags.online || (fcport->flags & FCF_ASYNC_SENT))
1127                 return rval;
1128
1129         fcport->disc_state = DSC_GPDB;
1130
1131         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1132         if (!sp)
1133                 goto done;
1134
1135         fcport->flags |= FCF_ASYNC_SENT;
1136         sp->type = SRB_MB_IOCB;
1137         sp->name = "gpdb";
1138         sp->gen1 = fcport->rscn_gen;
1139         sp->gen2 = fcport->login_gen;
1140
1141         mbx = &sp->u.iocb_cmd;
1142         mbx->timeout = qla2x00_async_iocb_timeout;
1143         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha) + 2);
1144
1145         pd = dma_pool_zalloc(ha->s_dma_pool, GFP_KERNEL, &pd_dma);
1146         if (pd == NULL) {
1147                 ql_log(ql_log_warn, vha, 0xd043,
1148                     "Failed to allocate port database structure.\n");
1149                 goto done_free_sp;
1150         }
1151
1152         mb = sp->u.iocb_cmd.u.mbx.out_mb;
1153         mb[0] = MBC_GET_PORT_DATABASE;
1154         mb[1] = fcport->loop_id;
1155         mb[2] = MSW(pd_dma);
1156         mb[3] = LSW(pd_dma);
1157         mb[6] = MSW(MSD(pd_dma));
1158         mb[7] = LSW(MSD(pd_dma));
1159         mb[9] = vha->vp_idx;
1160         mb[10] = opt;
1161
1162         mbx->u.mbx.in = (void *)pd;
1163         mbx->u.mbx.in_dma = pd_dma;
1164
1165         sp->done = qla24xx_async_gpdb_sp_done;
1166
1167         ql_dbg(ql_dbg_disc, vha, 0x20dc,
1168             "Async-%s %8phC hndl %x opt %x\n",
1169             sp->name, fcport->port_name, sp->handle, opt);
1170
1171         rval = qla2x00_start_sp(sp);
1172         if (rval != QLA_SUCCESS)
1173                 goto done_free_sp;
1174         return rval;
1175
1176 done_free_sp:
1177         if (pd)
1178                 dma_pool_free(ha->s_dma_pool, pd, pd_dma);
1179
1180         sp->free(sp);
1181         fcport->flags &= ~FCF_ASYNC_SENT;
1182 done:
1183         qla24xx_post_gpdb_work(vha, fcport, opt);
1184         return rval;
1185 }
1186
1187 static
1188 void __qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
1189 {
1190         unsigned long flags;
1191
1192         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1193         ea->fcport->login_gen++;
1194         ea->fcport->deleted = 0;
1195         ea->fcport->logout_on_delete = 1;
1196
1197         if (!ea->fcport->login_succ && !IS_SW_RESV_ADDR(ea->fcport->d_id)) {
1198                 vha->fcport_count++;
1199                 ea->fcport->login_succ = 1;
1200
1201                 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1202                 qla24xx_sched_upd_fcport(ea->fcport);
1203                 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1204         } else if (ea->fcport->login_succ) {
1205                 /*
1206                  * We have an existing session. A late RSCN delivery
1207                  * must have triggered the session to be re-validate.
1208                  * Session is still valid.
1209                  */
1210                 ql_dbg(ql_dbg_disc, vha, 0x20d6,
1211                     "%s %d %8phC session revalidate success\n",
1212                     __func__, __LINE__, ea->fcport->port_name);
1213                 ea->fcport->disc_state = DSC_LOGIN_COMPLETE;
1214         }
1215         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1216 }
1217
1218 static
1219 void qla24xx_handle_gpdb_event(scsi_qla_host_t *vha, struct event_arg *ea)
1220 {
1221         fc_port_t *fcport = ea->fcport;
1222         struct port_database_24xx *pd;
1223         struct srb *sp = ea->sp;
1224         uint8_t ls;
1225
1226         pd = (struct port_database_24xx *)sp->u.iocb_cmd.u.mbx.in;
1227
1228         fcport->flags &= ~FCF_ASYNC_SENT;
1229
1230         ql_dbg(ql_dbg_disc, vha, 0x20d2,
1231             "%s %8phC DS %d LS %d nvme %x rc %d\n", __func__, fcport->port_name,
1232             fcport->disc_state, pd->current_login_state, fcport->fc4f_nvme,
1233             ea->rc);
1234
1235         if (fcport->disc_state == DSC_DELETE_PEND)
1236                 return;
1237
1238         if (fcport->fc4f_nvme)
1239                 ls = pd->current_login_state >> 4;
1240         else
1241                 ls = pd->current_login_state & 0xf;
1242
1243         if (ea->sp->gen2 != fcport->login_gen) {
1244                 /* target side must have changed it. */
1245
1246                 ql_dbg(ql_dbg_disc, vha, 0x20d3,
1247                     "%s %8phC generation changed\n",
1248                     __func__, fcport->port_name);
1249                 return;
1250         } else if (ea->sp->gen1 != fcport->rscn_gen) {
1251                 qla_rscn_replay(fcport);
1252                 qlt_schedule_sess_for_deletion(fcport);
1253                 return;
1254         }
1255
1256         switch (ls) {
1257         case PDS_PRLI_COMPLETE:
1258                 __qla24xx_parse_gpdb(vha, fcport, pd);
1259                 break;
1260         case PDS_PLOGI_PENDING:
1261         case PDS_PLOGI_COMPLETE:
1262         case PDS_PRLI_PENDING:
1263         case PDS_PRLI2_PENDING:
1264                 /* Set discovery state back to GNL to Relogin attempt */
1265                 if (qla_dual_mode_enabled(vha) ||
1266                     qla_ini_mode_enabled(vha)) {
1267                         fcport->disc_state = DSC_GNL;
1268                         set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1269                 }
1270                 return;
1271         case PDS_LOGO_PENDING:
1272         case PDS_PORT_UNAVAILABLE:
1273         default:
1274                 ql_dbg(ql_dbg_disc, vha, 0x20d5, "%s %d %8phC post del sess\n",
1275                     __func__, __LINE__, fcport->port_name);
1276                 qlt_schedule_sess_for_deletion(fcport);
1277                 return;
1278         }
1279         __qla24xx_handle_gpdb_event(vha, ea);
1280 } /* gpdb event */
1281
1282 static void qla_chk_n2n_b4_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1283 {
1284         u8 login = 0;
1285         int rc;
1286
1287         if (qla_tgt_mode_enabled(vha))
1288                 return;
1289
1290         if (qla_dual_mode_enabled(vha)) {
1291                 if (N2N_TOPO(vha->hw)) {
1292                         u64 mywwn, wwn;
1293
1294                         mywwn = wwn_to_u64(vha->port_name);
1295                         wwn = wwn_to_u64(fcport->port_name);
1296                         if (mywwn > wwn)
1297                                 login = 1;
1298                         else if ((fcport->fw_login_state == DSC_LS_PLOGI_COMP)
1299                             && time_after_eq(jiffies,
1300                                     fcport->plogi_nack_done_deadline))
1301                                 login = 1;
1302                 } else {
1303                         login = 1;
1304                 }
1305         } else {
1306                 /* initiator mode */
1307                 login = 1;
1308         }
1309
1310         if (login && fcport->login_retry) {
1311                 fcport->login_retry--;
1312                 if (fcport->loop_id == FC_NO_LOOP_ID) {
1313                         fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
1314                         rc = qla2x00_find_new_loop_id(vha, fcport);
1315                         if (rc) {
1316                                 ql_dbg(ql_dbg_disc, vha, 0x20e6,
1317                                     "%s %d %8phC post del sess - out of loopid\n",
1318                                     __func__, __LINE__, fcport->port_name);
1319                                 fcport->scan_state = 0;
1320                                 qlt_schedule_sess_for_deletion(fcport);
1321                                 return;
1322                         }
1323                 }
1324                 ql_dbg(ql_dbg_disc, vha, 0x20bf,
1325                     "%s %d %8phC post login\n",
1326                     __func__, __LINE__, fcport->port_name);
1327                 qla2x00_post_async_login_work(vha, fcport, NULL);
1328         }
1329 }
1330
1331 int qla24xx_fcport_handle_login(struct scsi_qla_host *vha, fc_port_t *fcport)
1332 {
1333         u16 data[2];
1334         u64 wwn;
1335         u16 sec;
1336
1337         ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x20d8,
1338             "%s %8phC DS %d LS %d P %d fl %x confl %p rscn %d|%d login %d lid %d scan %d\n",
1339             __func__, fcport->port_name, fcport->disc_state,
1340             fcport->fw_login_state, fcport->login_pause, fcport->flags,
1341             fcport->conflict, fcport->last_rscn_gen, fcport->rscn_gen,
1342             fcport->login_gen, fcport->loop_id, fcport->scan_state);
1343
1344         if (fcport->scan_state != QLA_FCPORT_FOUND)
1345                 return 0;
1346
1347         if ((fcport->loop_id != FC_NO_LOOP_ID) &&
1348             ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
1349              (fcport->fw_login_state == DSC_LS_PRLI_PEND)))
1350                 return 0;
1351
1352         if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
1353                 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1354                         set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1355                         return 0;
1356                 }
1357         }
1358
1359         /* for pure Target Mode. Login will not be initiated */
1360         if (vha->host->active_mode == MODE_TARGET)
1361                 return 0;
1362
1363         if (fcport->flags & FCF_ASYNC_SENT) {
1364                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1365                 return 0;
1366         }
1367
1368         switch (fcport->disc_state) {
1369         case DSC_DELETED:
1370                 wwn = wwn_to_u64(fcport->node_name);
1371                 switch (vha->hw->current_topology) {
1372                 case ISP_CFG_N:
1373                         if (fcport_is_smaller(fcport)) {
1374                                 /* this adapter is bigger */
1375                                 if (fcport->login_retry) {
1376                                         if (fcport->loop_id == FC_NO_LOOP_ID) {
1377                                                 qla2x00_find_new_loop_id(vha,
1378                                                     fcport);
1379                                                 fcport->fw_login_state =
1380                                                     DSC_LS_PORT_UNAVAIL;
1381                                         }
1382                                         fcport->login_retry--;
1383                                         qla_post_els_plogi_work(vha, fcport);
1384                                 } else {
1385                                         ql_log(ql_log_info, vha, 0x705d,
1386                                             "Unable to reach remote port %8phC",
1387                                             fcport->port_name);
1388                                 }
1389                         } else {
1390                                 qla24xx_post_gnl_work(vha, fcport);
1391                         }
1392                         break;
1393                 default:
1394                         if (wwn == 0)    {
1395                                 ql_dbg(ql_dbg_disc, vha, 0xffff,
1396                                     "%s %d %8phC post GNNID\n",
1397                                     __func__, __LINE__, fcport->port_name);
1398                                 qla24xx_post_gnnid_work(vha, fcport);
1399                         } else if (fcport->loop_id == FC_NO_LOOP_ID) {
1400                                 ql_dbg(ql_dbg_disc, vha, 0x20bd,
1401                                     "%s %d %8phC post gnl\n",
1402                                     __func__, __LINE__, fcport->port_name);
1403                                 qla24xx_post_gnl_work(vha, fcport);
1404                         } else {
1405                                 qla_chk_n2n_b4_login(vha, fcport);
1406                         }
1407                         break;
1408                 }
1409                 break;
1410
1411         case DSC_GNL:
1412                 switch (vha->hw->current_topology) {
1413                 case ISP_CFG_N:
1414                         if ((fcport->current_login_state & 0xf) == 0x6) {
1415                                 ql_dbg(ql_dbg_disc, vha, 0x2118,
1416                                     "%s %d %8phC post GPDB work\n",
1417                                     __func__, __LINE__, fcport->port_name);
1418                                 fcport->chip_reset =
1419                                         vha->hw->base_qpair->chip_reset;
1420                                 qla24xx_post_gpdb_work(vha, fcport, 0);
1421                         }  else {
1422                                 ql_dbg(ql_dbg_disc, vha, 0x2118,
1423                                     "%s %d %8phC post NVMe PRLI\n",
1424                                     __func__, __LINE__, fcport->port_name);
1425                                 qla24xx_post_prli_work(vha, fcport);
1426                         }
1427                         break;
1428                 default:
1429                         if (fcport->login_pause) {
1430                                 fcport->last_rscn_gen = fcport->rscn_gen;
1431                                 fcport->last_login_gen = fcport->login_gen;
1432                                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1433                                 break;
1434                         }
1435                         qla_chk_n2n_b4_login(vha, fcport);
1436                         break;
1437                 }
1438                 break;
1439
1440         case DSC_LOGIN_FAILED:
1441                 if (N2N_TOPO(vha->hw))
1442                         qla_chk_n2n_b4_login(vha, fcport);
1443                 else
1444                         qlt_schedule_sess_for_deletion(fcport);
1445                 break;
1446
1447         case DSC_LOGIN_COMPLETE:
1448                 /* recheck login state */
1449                 data[0] = data[1] = 0;
1450                 qla2x00_post_async_adisc_work(vha, fcport, data);
1451                 break;
1452
1453         case DSC_LOGIN_PEND:
1454                 if (fcport->fw_login_state == DSC_LS_PLOGI_COMP)
1455                         qla24xx_post_prli_work(vha, fcport);
1456                 break;
1457
1458         case DSC_UPD_FCPORT:
1459                 sec =  jiffies_to_msecs(jiffies -
1460                     fcport->jiffies_at_registration)/1000;
1461                 if (fcport->sec_since_registration < sec && sec &&
1462                     !(sec % 60)) {
1463                         fcport->sec_since_registration = sec;
1464                         ql_dbg(ql_dbg_disc, fcport->vha, 0xffff,
1465                             "%s %8phC - Slow Rport registration(%d Sec)\n",
1466                             __func__, fcport->port_name, sec);
1467                 }
1468
1469                 if (fcport->next_disc_state != DSC_DELETE_PEND)
1470                         fcport->next_disc_state = DSC_ADISC;
1471                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1472                 break;
1473
1474         default:
1475                 break;
1476         }
1477
1478         return 0;
1479 }
1480
1481 int qla24xx_post_newsess_work(struct scsi_qla_host *vha, port_id_t *id,
1482     u8 *port_name, u8 *node_name, void *pla, u8 fc4_type)
1483 {
1484         struct qla_work_evt *e;
1485         e = qla2x00_alloc_work(vha, QLA_EVT_NEW_SESS);
1486         if (!e)
1487                 return QLA_FUNCTION_FAILED;
1488
1489         e->u.new_sess.id = *id;
1490         e->u.new_sess.pla = pla;
1491         e->u.new_sess.fc4_type = fc4_type;
1492         memcpy(e->u.new_sess.port_name, port_name, WWN_SIZE);
1493         if (node_name)
1494                 memcpy(e->u.new_sess.node_name, node_name, WWN_SIZE);
1495
1496         return qla2x00_post_work(vha, e);
1497 }
1498
1499 static
1500 void qla24xx_handle_relogin_event(scsi_qla_host_t *vha,
1501         struct event_arg *ea)
1502 {
1503         fc_port_t *fcport = ea->fcport;
1504
1505         ql_dbg(ql_dbg_disc, vha, 0x2102,
1506             "%s %8phC DS %d LS %d P %d del %d cnfl %p rscn %d|%d login %d|%d fl %x\n",
1507             __func__, fcport->port_name, fcport->disc_state,
1508             fcport->fw_login_state, fcport->login_pause,
1509             fcport->deleted, fcport->conflict,
1510             fcport->last_rscn_gen, fcport->rscn_gen,
1511             fcport->last_login_gen, fcport->login_gen,
1512             fcport->flags);
1513
1514         if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
1515             (fcport->fw_login_state == DSC_LS_PRLI_PEND))
1516                 return;
1517
1518         if (fcport->fw_login_state == DSC_LS_PLOGI_COMP) {
1519                 if (time_before_eq(jiffies, fcport->plogi_nack_done_deadline)) {
1520                         set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1521                         return;
1522                 }
1523         }
1524
1525         if (fcport->last_rscn_gen != fcport->rscn_gen) {
1526                 ql_dbg(ql_dbg_disc, vha, 0x20e9, "%s %d %8phC post gidpn\n",
1527                     __func__, __LINE__, fcport->port_name);
1528
1529                 return;
1530         }
1531
1532         qla24xx_fcport_handle_login(vha, fcport);
1533 }
1534
1535
1536 static void qla_handle_els_plogi_done(scsi_qla_host_t *vha,
1537                                       struct event_arg *ea)
1538 {
1539         ql_dbg(ql_dbg_disc, vha, 0x2118,
1540             "%s %d %8phC post PRLI\n",
1541             __func__, __LINE__, ea->fcport->port_name);
1542         qla24xx_post_prli_work(vha, ea->fcport);
1543 }
1544
1545 void qla2x00_fcport_event_handler(scsi_qla_host_t *vha, struct event_arg *ea)
1546 {
1547         fc_port_t *fcport;
1548
1549         switch (ea->event) {
1550         case FCME_RELOGIN:
1551                 if (test_bit(UNLOADING, &vha->dpc_flags))
1552                         return;
1553
1554                 qla24xx_handle_relogin_event(vha, ea);
1555                 break;
1556         case FCME_RSCN:
1557                 if (test_bit(UNLOADING, &vha->dpc_flags))
1558                         return;
1559                 {
1560                         unsigned long flags;
1561                         fcport = qla2x00_find_fcport_by_nportid
1562                                 (vha, &ea->id, 1);
1563                         if (fcport) {
1564                                 fcport->scan_needed = 1;
1565                                 fcport->rscn_gen++;
1566                         }
1567
1568                         spin_lock_irqsave(&vha->work_lock, flags);
1569                         if (vha->scan.scan_flags == 0) {
1570                                 ql_dbg(ql_dbg_disc, vha, 0xffff,
1571                                     "%s: schedule\n", __func__);
1572                                 vha->scan.scan_flags |= SF_QUEUED;
1573                                 schedule_delayed_work(&vha->scan.scan_work, 5);
1574                         }
1575                         spin_unlock_irqrestore(&vha->work_lock, flags);
1576                 }
1577                 break;
1578         case FCME_GNL_DONE:
1579                 qla24xx_handle_gnl_done_event(vha, ea);
1580                 break;
1581         case FCME_GPSC_DONE:
1582                 qla24xx_handle_gpsc_event(vha, ea);
1583                 break;
1584         case FCME_PLOGI_DONE:   /* Initiator side sent LLIOCB */
1585                 qla24xx_handle_plogi_done_event(vha, ea);
1586                 break;
1587         case FCME_PRLI_DONE:
1588                 qla24xx_handle_prli_done_event(vha, ea);
1589                 break;
1590         case FCME_GPDB_DONE:
1591                 qla24xx_handle_gpdb_event(vha, ea);
1592                 break;
1593         case FCME_GPNID_DONE:
1594                 qla24xx_handle_gpnid_event(vha, ea);
1595                 break;
1596         case FCME_GFFID_DONE:
1597                 qla24xx_handle_gffid_event(vha, ea);
1598                 break;
1599         case FCME_ADISC_DONE:
1600                 qla24xx_handle_adisc_event(vha, ea);
1601                 break;
1602         case FCME_GNNID_DONE:
1603                 qla24xx_handle_gnnid_event(vha, ea);
1604                 break;
1605         case FCME_GFPNID_DONE:
1606                 qla24xx_handle_gfpnid_event(vha, ea);
1607                 break;
1608         case FCME_ELS_PLOGI_DONE:
1609                 qla_handle_els_plogi_done(vha, ea);
1610                 break;
1611         default:
1612                 BUG_ON(1);
1613                 break;
1614         }
1615 }
1616
1617 /*
1618  * RSCN(s) came in for this fcport, but the RSCN(s) was not able
1619  * to be consumed by the fcport
1620  */
1621 void qla_rscn_replay(fc_port_t *fcport)
1622 {
1623         struct event_arg ea;
1624
1625         switch (fcport->disc_state) {
1626         case DSC_DELETE_PEND:
1627                 return;
1628         default:
1629                 break;
1630         }
1631
1632         if (fcport->scan_needed) {
1633                 memset(&ea, 0, sizeof(ea));
1634                 ea.event = FCME_RSCN;
1635                 ea.id = fcport->d_id;
1636                 ea.id.b.rsvd_1 = RSCN_PORT_ADDR;
1637                 qla2x00_fcport_event_handler(fcport->vha, &ea);
1638         }
1639 }
1640
1641 static void
1642 qla2x00_tmf_iocb_timeout(void *data)
1643 {
1644         srb_t *sp = data;
1645         struct srb_iocb *tmf = &sp->u.iocb_cmd;
1646
1647         tmf->u.tmf.comp_status = CS_TIMEOUT;
1648         complete(&tmf->u.tmf.comp);
1649 }
1650
1651 static void
1652 qla2x00_tmf_sp_done(void *ptr, int res)
1653 {
1654         srb_t *sp = ptr;
1655         struct srb_iocb *tmf = &sp->u.iocb_cmd;
1656
1657         complete(&tmf->u.tmf.comp);
1658 }
1659
1660 int
1661 qla2x00_async_tm_cmd(fc_port_t *fcport, uint32_t flags, uint32_t lun,
1662         uint32_t tag)
1663 {
1664         struct scsi_qla_host *vha = fcport->vha;
1665         struct srb_iocb *tm_iocb;
1666         srb_t *sp;
1667         int rval = QLA_FUNCTION_FAILED;
1668
1669         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1670         if (!sp)
1671                 goto done;
1672
1673         tm_iocb = &sp->u.iocb_cmd;
1674         sp->type = SRB_TM_CMD;
1675         sp->name = "tmf";
1676
1677         tm_iocb->timeout = qla2x00_tmf_iocb_timeout;
1678         init_completion(&tm_iocb->u.tmf.comp);
1679         qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha));
1680
1681         tm_iocb->u.tmf.flags = flags;
1682         tm_iocb->u.tmf.lun = lun;
1683         tm_iocb->u.tmf.data = tag;
1684         sp->done = qla2x00_tmf_sp_done;
1685
1686         ql_dbg(ql_dbg_taskm, vha, 0x802f,
1687             "Async-tmf hdl=%x loop-id=%x portid=%02x%02x%02x.\n",
1688             sp->handle, fcport->loop_id, fcport->d_id.b.domain,
1689             fcport->d_id.b.area, fcport->d_id.b.al_pa);
1690
1691         rval = qla2x00_start_sp(sp);
1692         if (rval != QLA_SUCCESS)
1693                 goto done_free_sp;
1694         wait_for_completion(&tm_iocb->u.tmf.comp);
1695
1696         rval = tm_iocb->u.tmf.data;
1697
1698         if (rval != QLA_SUCCESS) {
1699                 ql_log(ql_log_warn, vha, 0x8030,
1700                     "TM IOCB failed (%x).\n", rval);
1701         }
1702
1703         if (!test_bit(UNLOADING, &vha->dpc_flags) && !IS_QLAFX00(vha->hw)) {
1704                 flags = tm_iocb->u.tmf.flags;
1705                 lun = (uint16_t)tm_iocb->u.tmf.lun;
1706
1707                 /* Issue Marker IOCB */
1708                 qla2x00_marker(vha, vha->hw->base_qpair,
1709                     fcport->loop_id, lun,
1710                     flags == TCF_LUN_RESET ? MK_SYNC_ID_LUN : MK_SYNC_ID);
1711         }
1712
1713 done_free_sp:
1714         sp->free(sp);
1715         fcport->flags &= ~FCF_ASYNC_SENT;
1716 done:
1717         return rval;
1718 }
1719
1720 static void
1721 qla24xx_abort_iocb_timeout(void *data)
1722 {
1723         srb_t *sp = data;
1724         struct srb_iocb *abt = &sp->u.iocb_cmd;
1725
1726         abt->u.abt.comp_status = CS_TIMEOUT;
1727         sp->done(sp, QLA_FUNCTION_TIMEOUT);
1728 }
1729
1730 static void
1731 qla24xx_abort_sp_done(void *ptr, int res)
1732 {
1733         srb_t *sp = ptr;
1734         struct srb_iocb *abt = &sp->u.iocb_cmd;
1735
1736         if (del_timer(&sp->u.iocb_cmd.timer)) {
1737                 if (sp->flags & SRB_WAKEUP_ON_COMP)
1738                         complete(&abt->u.abt.comp);
1739                 else
1740                         sp->free(sp);
1741         }
1742 }
1743
1744 int
1745 qla24xx_async_abort_cmd(srb_t *cmd_sp, bool wait)
1746 {
1747         scsi_qla_host_t *vha = cmd_sp->vha;
1748         struct srb_iocb *abt_iocb;
1749         srb_t *sp;
1750         int rval = QLA_FUNCTION_FAILED;
1751
1752         sp = qla2xxx_get_qpair_sp(cmd_sp->vha, cmd_sp->qpair, cmd_sp->fcport,
1753             GFP_ATOMIC);
1754         if (!sp)
1755                 goto done;
1756
1757         abt_iocb = &sp->u.iocb_cmd;
1758         sp->type = SRB_ABT_CMD;
1759         sp->name = "abort";
1760         sp->qpair = cmd_sp->qpair;
1761         if (wait)
1762                 sp->flags = SRB_WAKEUP_ON_COMP;
1763
1764         abt_iocb->timeout = qla24xx_abort_iocb_timeout;
1765         init_completion(&abt_iocb->u.abt.comp);
1766         /* FW can send 2 x ABTS's timeout/20s */
1767         qla2x00_init_timer(sp, 42);
1768
1769         abt_iocb->u.abt.cmd_hndl = cmd_sp->handle;
1770         abt_iocb->u.abt.req_que_no = cpu_to_le16(cmd_sp->qpair->req->id);
1771
1772         sp->done = qla24xx_abort_sp_done;
1773
1774         ql_dbg(ql_dbg_async, vha, 0x507c,
1775             "Abort command issued - hdl=%x, type=%x\n",
1776             cmd_sp->handle, cmd_sp->type);
1777
1778         rval = qla2x00_start_sp(sp);
1779         if (rval != QLA_SUCCESS)
1780                 goto done_free_sp;
1781
1782         if (wait) {
1783                 wait_for_completion(&abt_iocb->u.abt.comp);
1784                 rval = abt_iocb->u.abt.comp_status == CS_COMPLETE ?
1785                         QLA_SUCCESS : QLA_FUNCTION_FAILED;
1786         } else {
1787                 goto done;
1788         }
1789
1790 done_free_sp:
1791         sp->free(sp);
1792 done:
1793         return rval;
1794 }
1795
1796 int
1797 qla24xx_async_abort_command(srb_t *sp)
1798 {
1799         unsigned long   flags = 0;
1800
1801         uint32_t        handle;
1802         fc_port_t       *fcport = sp->fcport;
1803         struct qla_qpair *qpair = sp->qpair;
1804         struct scsi_qla_host *vha = fcport->vha;
1805         struct req_que *req = qpair->req;
1806
1807         spin_lock_irqsave(qpair->qp_lock_ptr, flags);
1808         for (handle = 1; handle < req->num_outstanding_cmds; handle++) {
1809                 if (req->outstanding_cmds[handle] == sp)
1810                         break;
1811         }
1812         spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
1813
1814         if (handle == req->num_outstanding_cmds) {
1815                 /* Command not found. */
1816                 return QLA_FUNCTION_FAILED;
1817         }
1818         if (sp->type == SRB_FXIOCB_DCMD)
1819                 return qlafx00_fx_disc(vha, &vha->hw->mr.fcport,
1820                     FXDISC_ABORT_IOCTL);
1821
1822         return qla24xx_async_abort_cmd(sp, true);
1823 }
1824
1825 static void
1826 qla24xx_handle_prli_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
1827 {
1828         switch (ea->data[0]) {
1829         case MBS_COMMAND_COMPLETE:
1830                 ql_dbg(ql_dbg_disc, vha, 0x2118,
1831                     "%s %d %8phC post gpdb\n",
1832                     __func__, __LINE__, ea->fcport->port_name);
1833
1834                 ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1835                 ea->fcport->logout_on_delete = 1;
1836                 ea->fcport->nvme_prli_service_param = ea->iop[0];
1837                 if (ea->iop[0] & NVME_PRLI_SP_FIRST_BURST)
1838                         ea->fcport->nvme_first_burst_size =
1839                             (ea->iop[1] & 0xffff) * 512;
1840                 else
1841                         ea->fcport->nvme_first_burst_size = 0;
1842                 qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1843                 break;
1844         default:
1845                 if ((ea->iop[0] == LSC_SCODE_ELS_REJECT) &&
1846                     (ea->iop[1] == 0x50000)) {   /* reson 5=busy expl:0x0 */
1847                         set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1848                         ea->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
1849                         break;
1850                 }
1851
1852                 if (ea->fcport->n2n_flag) {
1853                         ql_dbg(ql_dbg_disc, vha, 0x2118,
1854                                 "%s %d %8phC post fc4 prli\n",
1855                                 __func__, __LINE__, ea->fcport->port_name);
1856                         ea->fcport->fc4f_nvme = 0;
1857                         ea->fcport->n2n_flag = 0;
1858                         qla24xx_post_prli_work(vha, ea->fcport);
1859                 }
1860                 ql_dbg(ql_dbg_disc, vha, 0x2119,
1861                     "%s %d %8phC unhandle event of %x\n",
1862                     __func__, __LINE__, ea->fcport->port_name, ea->data[0]);
1863                 break;
1864         }
1865 }
1866
1867 static void
1868 qla24xx_handle_plogi_done_event(struct scsi_qla_host *vha, struct event_arg *ea)
1869 {
1870         port_id_t cid;  /* conflict Nport id */
1871         u16 lid;
1872         struct fc_port *conflict_fcport;
1873         unsigned long flags;
1874         struct fc_port *fcport = ea->fcport;
1875
1876         ql_dbg(ql_dbg_disc, vha, 0xffff,
1877             "%s %8phC DS %d LS %d rc %d login %d|%d rscn %d|%d data %x|%x iop %x|%x\n",
1878             __func__, fcport->port_name, fcport->disc_state,
1879             fcport->fw_login_state, ea->rc, ea->sp->gen2, fcport->login_gen,
1880             ea->sp->gen1, fcport->rscn_gen,
1881             ea->data[0], ea->data[1], ea->iop[0], ea->iop[1]);
1882
1883         if ((fcport->fw_login_state == DSC_LS_PLOGI_PEND) ||
1884             (fcport->fw_login_state == DSC_LS_PRLI_PEND)) {
1885                 ql_dbg(ql_dbg_disc, vha, 0x20ea,
1886                     "%s %d %8phC Remote is trying to login\n",
1887                     __func__, __LINE__, fcport->port_name);
1888                 return;
1889         }
1890
1891         if (fcport->disc_state == DSC_DELETE_PEND)
1892                 return;
1893
1894         if (ea->sp->gen2 != fcport->login_gen) {
1895                 /* target side must have changed it. */
1896                 ql_dbg(ql_dbg_disc, vha, 0x20d3,
1897                     "%s %8phC generation changed\n",
1898                     __func__, fcport->port_name);
1899                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1900                 return;
1901         } else if (ea->sp->gen1 != fcport->rscn_gen) {
1902                 ql_dbg(ql_dbg_disc, vha, 0x20d3,
1903                     "%s %8phC RSCN generation changed\n",
1904                     __func__, fcport->port_name);
1905                 qla_rscn_replay(fcport);
1906                 qlt_schedule_sess_for_deletion(fcport);
1907                 return;
1908         }
1909
1910         switch (ea->data[0]) {
1911         case MBS_COMMAND_COMPLETE:
1912                 /*
1913                  * Driver must validate login state - If PRLI not complete,
1914                  * force a relogin attempt via implicit LOGO, PLOGI, and PRLI
1915                  * requests.
1916                  */
1917                 if (ea->fcport->fc4f_nvme) {
1918                         ql_dbg(ql_dbg_disc, vha, 0x2117,
1919                                 "%s %d %8phC post prli\n",
1920                                 __func__, __LINE__, ea->fcport->port_name);
1921                         qla24xx_post_prli_work(vha, ea->fcport);
1922                 } else {
1923                         ql_dbg(ql_dbg_disc, vha, 0x20ea,
1924                             "%s %d %8phC LoopID 0x%x in use with %06x. post gnl\n",
1925                             __func__, __LINE__, ea->fcport->port_name,
1926                             ea->fcport->loop_id, ea->fcport->d_id.b24);
1927
1928                         set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
1929                         spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1930                         ea->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
1931                         ea->fcport->logout_on_delete = 1;
1932                         ea->fcport->send_els_logo = 0;
1933                         ea->fcport->fw_login_state = DSC_LS_PRLI_COMP;
1934                         spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1935
1936                         qla24xx_post_gpdb_work(vha, ea->fcport, 0);
1937                 }
1938                 break;
1939         case MBS_COMMAND_ERROR:
1940                 ql_dbg(ql_dbg_disc, vha, 0x20eb, "%s %d %8phC cmd error %x\n",
1941                     __func__, __LINE__, ea->fcport->port_name, ea->data[1]);
1942
1943                 ea->fcport->flags &= ~FCF_ASYNC_SENT;
1944                 ea->fcport->disc_state = DSC_LOGIN_FAILED;
1945                 if (ea->data[1] & QLA_LOGIO_LOGIN_RETRIED)
1946                         set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1947                 else
1948                         qla2x00_mark_device_lost(vha, ea->fcport, 1, 0);
1949                 break;
1950         case MBS_LOOP_ID_USED:
1951                 /* data[1] = IO PARAM 1 = nport ID  */
1952                 cid.b.domain = (ea->iop[1] >> 16) & 0xff;
1953                 cid.b.area   = (ea->iop[1] >>  8) & 0xff;
1954                 cid.b.al_pa  = ea->iop[1] & 0xff;
1955                 cid.b.rsvd_1 = 0;
1956
1957                 ql_dbg(ql_dbg_disc, vha, 0x20ec,
1958                     "%s %d %8phC lid %#x in use with pid %06x post gnl\n",
1959                     __func__, __LINE__, ea->fcport->port_name,
1960                     ea->fcport->loop_id, cid.b24);
1961
1962                 set_bit(ea->fcport->loop_id, vha->hw->loop_id_map);
1963                 ea->fcport->loop_id = FC_NO_LOOP_ID;
1964                 qla24xx_post_gnl_work(vha, ea->fcport);
1965                 break;
1966         case MBS_PORT_ID_USED:
1967                 lid = ea->iop[1] & 0xffff;
1968                 qlt_find_sess_invalidate_other(vha,
1969                     wwn_to_u64(ea->fcport->port_name),
1970                     ea->fcport->d_id, lid, &conflict_fcport);
1971
1972                 if (conflict_fcport) {
1973                         /*
1974                          * Another fcport share the same loop_id/nport id.
1975                          * Conflict fcport needs to finish cleanup before this
1976                          * fcport can proceed to login.
1977                          */
1978                         conflict_fcport->conflict = ea->fcport;
1979                         ea->fcport->login_pause = 1;
1980
1981                         ql_dbg(ql_dbg_disc, vha, 0x20ed,
1982                             "%s %d %8phC NPortId %06x inuse with loopid 0x%x. post gidpn\n",
1983                             __func__, __LINE__, ea->fcport->port_name,
1984                             ea->fcport->d_id.b24, lid);
1985                 } else {
1986                         ql_dbg(ql_dbg_disc, vha, 0x20ed,
1987                             "%s %d %8phC NPortId %06x inuse with loopid 0x%x. sched delete\n",
1988                             __func__, __LINE__, ea->fcport->port_name,
1989                             ea->fcport->d_id.b24, lid);
1990
1991                         qla2x00_clear_loop_id(ea->fcport);
1992                         set_bit(lid, vha->hw->loop_id_map);
1993                         ea->fcport->loop_id = lid;
1994                         ea->fcport->keep_nport_handle = 0;
1995                         qlt_schedule_sess_for_deletion(ea->fcport);
1996                 }
1997                 break;
1998         }
1999         return;
2000 }
2001
2002 void
2003 qla2x00_async_logout_done(struct scsi_qla_host *vha, fc_port_t *fcport,
2004     uint16_t *data)
2005 {
2006         qlt_logo_completion_handler(fcport, data[0]);
2007         fcport->login_gen++;
2008         fcport->flags &= ~FCF_ASYNC_ACTIVE;
2009         return;
2010 }
2011
2012 /****************************************************************************/
2013 /*                QLogic ISP2x00 Hardware Support Functions.                */
2014 /****************************************************************************/
2015
2016 static int
2017 qla83xx_nic_core_fw_load(scsi_qla_host_t *vha)
2018 {
2019         int rval = QLA_SUCCESS;
2020         struct qla_hw_data *ha = vha->hw;
2021         uint32_t idc_major_ver, idc_minor_ver;
2022         uint16_t config[4];
2023
2024         qla83xx_idc_lock(vha, 0);
2025
2026         /* SV: TODO: Assign initialization timeout from
2027          * flash-info / other param
2028          */
2029         ha->fcoe_dev_init_timeout = QLA83XX_IDC_INITIALIZATION_TIMEOUT;
2030         ha->fcoe_reset_timeout = QLA83XX_IDC_RESET_ACK_TIMEOUT;
2031
2032         /* Set our fcoe function presence */
2033         if (__qla83xx_set_drv_presence(vha) != QLA_SUCCESS) {
2034                 ql_dbg(ql_dbg_p3p, vha, 0xb077,
2035                     "Error while setting DRV-Presence.\n");
2036                 rval = QLA_FUNCTION_FAILED;
2037                 goto exit;
2038         }
2039
2040         /* Decide the reset ownership */
2041         qla83xx_reset_ownership(vha);
2042
2043         /*
2044          * On first protocol driver load:
2045          * Init-Owner: Set IDC-Major-Version and Clear IDC-Lock-Recovery
2046          * register.
2047          * Others: Check compatibility with current IDC Major version.
2048          */
2049         qla83xx_rd_reg(vha, QLA83XX_IDC_MAJOR_VERSION, &idc_major_ver);
2050         if (ha->flags.nic_core_reset_owner) {
2051                 /* Set IDC Major version */
2052                 idc_major_ver = QLA83XX_SUPP_IDC_MAJOR_VERSION;
2053                 qla83xx_wr_reg(vha, QLA83XX_IDC_MAJOR_VERSION, idc_major_ver);
2054
2055                 /* Clearing IDC-Lock-Recovery register */
2056                 qla83xx_wr_reg(vha, QLA83XX_IDC_LOCK_RECOVERY, 0);
2057         } else if (idc_major_ver != QLA83XX_SUPP_IDC_MAJOR_VERSION) {
2058                 /*
2059                  * Clear further IDC participation if we are not compatible with
2060                  * the current IDC Major Version.
2061                  */
2062                 ql_log(ql_log_warn, vha, 0xb07d,
2063                     "Failing load, idc_major_ver=%d, expected_major_ver=%d.\n",
2064                     idc_major_ver, QLA83XX_SUPP_IDC_MAJOR_VERSION);
2065                 __qla83xx_clear_drv_presence(vha);
2066                 rval = QLA_FUNCTION_FAILED;
2067                 goto exit;
2068         }
2069         /* Each function sets its supported Minor version. */
2070         qla83xx_rd_reg(vha, QLA83XX_IDC_MINOR_VERSION, &idc_minor_ver);
2071         idc_minor_ver |= (QLA83XX_SUPP_IDC_MINOR_VERSION << (ha->portnum * 2));
2072         qla83xx_wr_reg(vha, QLA83XX_IDC_MINOR_VERSION, idc_minor_ver);
2073
2074         if (ha->flags.nic_core_reset_owner) {
2075                 memset(config, 0, sizeof(config));
2076                 if (!qla81xx_get_port_config(vha, config))
2077                         qla83xx_wr_reg(vha, QLA83XX_IDC_DEV_STATE,
2078                             QLA8XXX_DEV_READY);
2079         }
2080
2081         rval = qla83xx_idc_state_handler(vha);
2082
2083 exit:
2084         qla83xx_idc_unlock(vha, 0);
2085
2086         return rval;
2087 }
2088
2089 /*
2090 * qla2x00_initialize_adapter
2091 *      Initialize board.
2092 *
2093 * Input:
2094 *      ha = adapter block pointer.
2095 *
2096 * Returns:
2097 *      0 = success
2098 */
2099 int
2100 qla2x00_initialize_adapter(scsi_qla_host_t *vha)
2101 {
2102         int     rval;
2103         struct qla_hw_data *ha = vha->hw;
2104         struct req_que *req = ha->req_q_map[0];
2105         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2106
2107         memset(&vha->qla_stats, 0, sizeof(vha->qla_stats));
2108         memset(&vha->fc_host_stat, 0, sizeof(vha->fc_host_stat));
2109
2110         /* Clear adapter flags. */
2111         vha->flags.online = 0;
2112         ha->flags.chip_reset_done = 0;
2113         vha->flags.reset_active = 0;
2114         ha->flags.pci_channel_io_perm_failure = 0;
2115         ha->flags.eeh_busy = 0;
2116         vha->qla_stats.jiffies_at_last_reset = get_jiffies_64();
2117         atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
2118         atomic_set(&vha->loop_state, LOOP_DOWN);
2119         vha->device_flags = DFLG_NO_CABLE;
2120         vha->dpc_flags = 0;
2121         vha->flags.management_server_logged_in = 0;
2122         vha->marker_needed = 0;
2123         ha->isp_abort_cnt = 0;
2124         ha->beacon_blink_led = 0;
2125
2126         set_bit(0, ha->req_qid_map);
2127         set_bit(0, ha->rsp_qid_map);
2128
2129         ql_dbg(ql_dbg_init, vha, 0x0040,
2130             "Configuring PCI space...\n");
2131         rval = ha->isp_ops->pci_config(vha);
2132         if (rval) {
2133                 ql_log(ql_log_warn, vha, 0x0044,
2134                     "Unable to configure PCI space.\n");
2135                 return (rval);
2136         }
2137
2138         ha->isp_ops->reset_chip(vha);
2139
2140         /* Check for secure flash support */
2141         if (IS_QLA28XX(ha)) {
2142                 if (RD_REG_DWORD(&reg->mailbox12) & BIT_0) {
2143                         ql_log(ql_log_info, vha, 0xffff, "Adapter is Secure\n");
2144                         ha->flags.secure_adapter = 1;
2145                 }
2146         }
2147
2148
2149         rval = qla2xxx_get_flash_info(vha);
2150         if (rval) {
2151                 ql_log(ql_log_fatal, vha, 0x004f,
2152                     "Unable to validate FLASH data.\n");
2153                 return rval;
2154         }
2155
2156         if (IS_QLA8044(ha)) {
2157                 qla8044_read_reset_template(vha);
2158
2159                 /* NOTE: If ql2xdontresethba==1, set IDC_CTRL DONTRESET_BIT0.
2160                  * If DONRESET_BIT0 is set, drivers should not set dev_state
2161                  * to NEED_RESET. But if NEED_RESET is set, drivers should
2162                  * should honor the reset. */
2163                 if (ql2xdontresethba == 1)
2164                         qla8044_set_idc_dontreset(vha);
2165         }
2166
2167         ha->isp_ops->get_flash_version(vha, req->ring);
2168         ql_dbg(ql_dbg_init, vha, 0x0061,
2169             "Configure NVRAM parameters...\n");
2170
2171         ha->isp_ops->nvram_config(vha);
2172
2173         if (ha->flags.disable_serdes) {
2174                 /* Mask HBA via NVRAM settings? */
2175                 ql_log(ql_log_info, vha, 0x0077,
2176                     "Masking HBA WWPN %8phN (via NVRAM).\n", vha->port_name);
2177                 return QLA_FUNCTION_FAILED;
2178         }
2179
2180         ql_dbg(ql_dbg_init, vha, 0x0078,
2181             "Verifying loaded RISC code...\n");
2182
2183         if (qla2x00_isp_firmware(vha) != QLA_SUCCESS) {
2184                 rval = ha->isp_ops->chip_diag(vha);
2185                 if (rval)
2186                         return (rval);
2187                 rval = qla2x00_setup_chip(vha);
2188                 if (rval)
2189                         return (rval);
2190         }
2191
2192         if (IS_QLA84XX(ha)) {
2193                 ha->cs84xx = qla84xx_get_chip(vha);
2194                 if (!ha->cs84xx) {
2195                         ql_log(ql_log_warn, vha, 0x00d0,
2196                             "Unable to configure ISP84XX.\n");
2197                         return QLA_FUNCTION_FAILED;
2198                 }
2199         }
2200
2201         if (qla_ini_mode_enabled(vha) || qla_dual_mode_enabled(vha))
2202                 rval = qla2x00_init_rings(vha);
2203
2204         ha->flags.chip_reset_done = 1;
2205
2206         if (rval == QLA_SUCCESS && IS_QLA84XX(ha)) {
2207                 /* Issue verify 84xx FW IOCB to complete 84xx initialization */
2208                 rval = qla84xx_init_chip(vha);
2209                 if (rval != QLA_SUCCESS) {
2210                         ql_log(ql_log_warn, vha, 0x00d4,
2211                             "Unable to initialize ISP84XX.\n");
2212                         qla84xx_put_chip(vha);
2213                 }
2214         }
2215
2216         /* Load the NIC Core f/w if we are the first protocol driver. */
2217         if (IS_QLA8031(ha)) {
2218                 rval = qla83xx_nic_core_fw_load(vha);
2219                 if (rval)
2220                         ql_log(ql_log_warn, vha, 0x0124,
2221                             "Error in initializing NIC Core f/w.\n");
2222         }
2223
2224         if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha))
2225                 qla24xx_read_fcp_prio_cfg(vha);
2226
2227         if (IS_P3P_TYPE(ha))
2228                 qla82xx_set_driver_version(vha, QLA2XXX_VERSION);
2229         else
2230                 qla25xx_set_driver_version(vha, QLA2XXX_VERSION);
2231
2232         return (rval);
2233 }
2234
2235 /**
2236  * qla2100_pci_config() - Setup ISP21xx PCI configuration registers.
2237  * @vha: HA context
2238  *
2239  * Returns 0 on success.
2240  */
2241 int
2242 qla2100_pci_config(scsi_qla_host_t *vha)
2243 {
2244         uint16_t w;
2245         unsigned long flags;
2246         struct qla_hw_data *ha = vha->hw;
2247         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2248
2249         pci_set_master(ha->pdev);
2250         pci_try_set_mwi(ha->pdev);
2251
2252         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2253         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2254         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2255
2256         pci_disable_rom(ha->pdev);
2257
2258         /* Get PCI bus information. */
2259         spin_lock_irqsave(&ha->hardware_lock, flags);
2260         ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
2261         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2262
2263         return QLA_SUCCESS;
2264 }
2265
2266 /**
2267  * qla2300_pci_config() - Setup ISP23xx PCI configuration registers.
2268  * @vha: HA context
2269  *
2270  * Returns 0 on success.
2271  */
2272 int
2273 qla2300_pci_config(scsi_qla_host_t *vha)
2274 {
2275         uint16_t        w;
2276         unsigned long   flags = 0;
2277         uint32_t        cnt;
2278         struct qla_hw_data *ha = vha->hw;
2279         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2280
2281         pci_set_master(ha->pdev);
2282         pci_try_set_mwi(ha->pdev);
2283
2284         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2285         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2286
2287         if (IS_QLA2322(ha) || IS_QLA6322(ha))
2288                 w &= ~PCI_COMMAND_INTX_DISABLE;
2289         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2290
2291         /*
2292          * If this is a 2300 card and not 2312, reset the
2293          * COMMAND_INVALIDATE due to a bug in the 2300. Unfortunately,
2294          * the 2310 also reports itself as a 2300 so we need to get the
2295          * fb revision level -- a 6 indicates it really is a 2300 and
2296          * not a 2310.
2297          */
2298         if (IS_QLA2300(ha)) {
2299                 spin_lock_irqsave(&ha->hardware_lock, flags);
2300
2301                 /* Pause RISC. */
2302                 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
2303                 for (cnt = 0; cnt < 30000; cnt++) {
2304                         if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
2305                                 break;
2306
2307                         udelay(10);
2308                 }
2309
2310                 /* Select FPM registers. */
2311                 WRT_REG_WORD(&reg->ctrl_status, 0x20);
2312                 RD_REG_WORD(&reg->ctrl_status);
2313
2314                 /* Get the fb rev level */
2315                 ha->fb_rev = RD_FB_CMD_REG(ha, reg);
2316
2317                 if (ha->fb_rev == FPM_2300)
2318                         pci_clear_mwi(ha->pdev);
2319
2320                 /* Deselect FPM registers. */
2321                 WRT_REG_WORD(&reg->ctrl_status, 0x0);
2322                 RD_REG_WORD(&reg->ctrl_status);
2323
2324                 /* Release RISC module. */
2325                 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2326                 for (cnt = 0; cnt < 30000; cnt++) {
2327                         if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) == 0)
2328                                 break;
2329
2330                         udelay(10);
2331                 }
2332
2333                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2334         }
2335
2336         pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2337
2338         pci_disable_rom(ha->pdev);
2339
2340         /* Get PCI bus information. */
2341         spin_lock_irqsave(&ha->hardware_lock, flags);
2342         ha->pci_attr = RD_REG_WORD(&reg->ctrl_status);
2343         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2344
2345         return QLA_SUCCESS;
2346 }
2347
2348 /**
2349  * qla24xx_pci_config() - Setup ISP24xx PCI configuration registers.
2350  * @vha: HA context
2351  *
2352  * Returns 0 on success.
2353  */
2354 int
2355 qla24xx_pci_config(scsi_qla_host_t *vha)
2356 {
2357         uint16_t w;
2358         unsigned long flags = 0;
2359         struct qla_hw_data *ha = vha->hw;
2360         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2361
2362         pci_set_master(ha->pdev);
2363         pci_try_set_mwi(ha->pdev);
2364
2365         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2366         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2367         w &= ~PCI_COMMAND_INTX_DISABLE;
2368         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2369
2370         pci_write_config_byte(ha->pdev, PCI_LATENCY_TIMER, 0x80);
2371
2372         /* PCI-X -- adjust Maximum Memory Read Byte Count (2048). */
2373         if (pci_find_capability(ha->pdev, PCI_CAP_ID_PCIX))
2374                 pcix_set_mmrbc(ha->pdev, 2048);
2375
2376         /* PCIe -- adjust Maximum Read Request Size (2048). */
2377         if (pci_is_pcie(ha->pdev))
2378                 pcie_set_readrq(ha->pdev, 4096);
2379
2380         pci_disable_rom(ha->pdev);
2381
2382         ha->chip_revision = ha->pdev->revision;
2383
2384         /* Get PCI bus information. */
2385         spin_lock_irqsave(&ha->hardware_lock, flags);
2386         ha->pci_attr = RD_REG_DWORD(&reg->ctrl_status);
2387         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2388
2389         return QLA_SUCCESS;
2390 }
2391
2392 /**
2393  * qla25xx_pci_config() - Setup ISP25xx PCI configuration registers.
2394  * @vha: HA context
2395  *
2396  * Returns 0 on success.
2397  */
2398 int
2399 qla25xx_pci_config(scsi_qla_host_t *vha)
2400 {
2401         uint16_t w;
2402         struct qla_hw_data *ha = vha->hw;
2403
2404         pci_set_master(ha->pdev);
2405         pci_try_set_mwi(ha->pdev);
2406
2407         pci_read_config_word(ha->pdev, PCI_COMMAND, &w);
2408         w |= (PCI_COMMAND_PARITY | PCI_COMMAND_SERR);
2409         w &= ~PCI_COMMAND_INTX_DISABLE;
2410         pci_write_config_word(ha->pdev, PCI_COMMAND, w);
2411
2412         /* PCIe -- adjust Maximum Read Request Size (2048). */
2413         if (pci_is_pcie(ha->pdev))
2414                 pcie_set_readrq(ha->pdev, 4096);
2415
2416         pci_disable_rom(ha->pdev);
2417
2418         ha->chip_revision = ha->pdev->revision;
2419
2420         return QLA_SUCCESS;
2421 }
2422
2423 /**
2424  * qla2x00_isp_firmware() - Choose firmware image.
2425  * @vha: HA context
2426  *
2427  * Returns 0 on success.
2428  */
2429 static int
2430 qla2x00_isp_firmware(scsi_qla_host_t *vha)
2431 {
2432         int  rval;
2433         uint16_t loop_id, topo, sw_cap;
2434         uint8_t domain, area, al_pa;
2435         struct qla_hw_data *ha = vha->hw;
2436
2437         /* Assume loading risc code */
2438         rval = QLA_FUNCTION_FAILED;
2439
2440         if (ha->flags.disable_risc_code_load) {
2441                 ql_log(ql_log_info, vha, 0x0079, "RISC CODE NOT loaded.\n");
2442
2443                 /* Verify checksum of loaded RISC code. */
2444                 rval = qla2x00_verify_checksum(vha, ha->fw_srisc_address);
2445                 if (rval == QLA_SUCCESS) {
2446                         /* And, verify we are not in ROM code. */
2447                         rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
2448                             &area, &domain, &topo, &sw_cap);
2449                 }
2450         }
2451
2452         if (rval)
2453                 ql_dbg(ql_dbg_init, vha, 0x007a,
2454                     "**** Load RISC code ****.\n");
2455
2456         return (rval);
2457 }
2458
2459 /**
2460  * qla2x00_reset_chip() - Reset ISP chip.
2461  * @vha: HA context
2462  *
2463  * Returns 0 on success.
2464  */
2465 int
2466 qla2x00_reset_chip(scsi_qla_host_t *vha)
2467 {
2468         unsigned long   flags = 0;
2469         struct qla_hw_data *ha = vha->hw;
2470         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2471         uint32_t        cnt;
2472         uint16_t        cmd;
2473         int rval = QLA_FUNCTION_FAILED;
2474
2475         if (unlikely(pci_channel_offline(ha->pdev)))
2476                 return rval;
2477
2478         ha->isp_ops->disable_intrs(ha);
2479
2480         spin_lock_irqsave(&ha->hardware_lock, flags);
2481
2482         /* Turn off master enable */
2483         cmd = 0;
2484         pci_read_config_word(ha->pdev, PCI_COMMAND, &cmd);
2485         cmd &= ~PCI_COMMAND_MASTER;
2486         pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2487
2488         if (!IS_QLA2100(ha)) {
2489                 /* Pause RISC. */
2490                 WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
2491                 if (IS_QLA2200(ha) || IS_QLA2300(ha)) {
2492                         for (cnt = 0; cnt < 30000; cnt++) {
2493                                 if ((RD_REG_WORD(&reg->hccr) &
2494                                     HCCR_RISC_PAUSE) != 0)
2495                                         break;
2496                                 udelay(100);
2497                         }
2498                 } else {
2499                         RD_REG_WORD(&reg->hccr);        /* PCI Posting. */
2500                         udelay(10);
2501                 }
2502
2503                 /* Select FPM registers. */
2504                 WRT_REG_WORD(&reg->ctrl_status, 0x20);
2505                 RD_REG_WORD(&reg->ctrl_status);         /* PCI Posting. */
2506
2507                 /* FPM Soft Reset. */
2508                 WRT_REG_WORD(&reg->fpm_diag_config, 0x100);
2509                 RD_REG_WORD(&reg->fpm_diag_config);     /* PCI Posting. */
2510
2511                 /* Toggle Fpm Reset. */
2512                 if (!IS_QLA2200(ha)) {
2513                         WRT_REG_WORD(&reg->fpm_diag_config, 0x0);
2514                         RD_REG_WORD(&reg->fpm_diag_config); /* PCI Posting. */
2515                 }
2516
2517                 /* Select frame buffer registers. */
2518                 WRT_REG_WORD(&reg->ctrl_status, 0x10);
2519                 RD_REG_WORD(&reg->ctrl_status);         /* PCI Posting. */
2520
2521                 /* Reset frame buffer FIFOs. */
2522                 if (IS_QLA2200(ha)) {
2523                         WRT_FB_CMD_REG(ha, reg, 0xa000);
2524                         RD_FB_CMD_REG(ha, reg);         /* PCI Posting. */
2525                 } else {
2526                         WRT_FB_CMD_REG(ha, reg, 0x00fc);
2527
2528                         /* Read back fb_cmd until zero or 3 seconds max */
2529                         for (cnt = 0; cnt < 3000; cnt++) {
2530                                 if ((RD_FB_CMD_REG(ha, reg) & 0xff) == 0)
2531                                         break;
2532                                 udelay(100);
2533                         }
2534                 }
2535
2536                 /* Select RISC module registers. */
2537                 WRT_REG_WORD(&reg->ctrl_status, 0);
2538                 RD_REG_WORD(&reg->ctrl_status);         /* PCI Posting. */
2539
2540                 /* Reset RISC processor. */
2541                 WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2542                 RD_REG_WORD(&reg->hccr);                /* PCI Posting. */
2543
2544                 /* Release RISC processor. */
2545                 WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2546                 RD_REG_WORD(&reg->hccr);                /* PCI Posting. */
2547         }
2548
2549         WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
2550         WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
2551
2552         /* Reset ISP chip. */
2553         WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2554
2555         /* Wait for RISC to recover from reset. */
2556         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2557                 /*
2558                  * It is necessary to for a delay here since the card doesn't
2559                  * respond to PCI reads during a reset. On some architectures
2560                  * this will result in an MCA.
2561                  */
2562                 udelay(20);
2563                 for (cnt = 30000; cnt; cnt--) {
2564                         if ((RD_REG_WORD(&reg->ctrl_status) &
2565                             CSR_ISP_SOFT_RESET) == 0)
2566                                 break;
2567                         udelay(100);
2568                 }
2569         } else
2570                 udelay(10);
2571
2572         /* Reset RISC processor. */
2573         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2574
2575         WRT_REG_WORD(&reg->semaphore, 0);
2576
2577         /* Release RISC processor. */
2578         WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2579         RD_REG_WORD(&reg->hccr);                        /* PCI Posting. */
2580
2581         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2582                 for (cnt = 0; cnt < 30000; cnt++) {
2583                         if (RD_MAILBOX_REG(ha, reg, 0) != MBS_BUSY)
2584                                 break;
2585
2586                         udelay(100);
2587                 }
2588         } else
2589                 udelay(100);
2590
2591         /* Turn on master enable */
2592         cmd |= PCI_COMMAND_MASTER;
2593         pci_write_config_word(ha->pdev, PCI_COMMAND, cmd);
2594
2595         /* Disable RISC pause on FPM parity error. */
2596         if (!IS_QLA2100(ha)) {
2597                 WRT_REG_WORD(&reg->hccr, HCCR_DISABLE_PARITY_PAUSE);
2598                 RD_REG_WORD(&reg->hccr);                /* PCI Posting. */
2599         }
2600
2601         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2602
2603         return QLA_SUCCESS;
2604 }
2605
2606 /**
2607  * qla81xx_reset_mpi() - Reset's MPI FW via Write MPI Register MBC.
2608  * @vha: HA context
2609  *
2610  * Returns 0 on success.
2611  */
2612 static int
2613 qla81xx_reset_mpi(scsi_qla_host_t *vha)
2614 {
2615         uint16_t mb[4] = {0x1010, 0, 1, 0};
2616
2617         if (!IS_QLA81XX(vha->hw))
2618                 return QLA_SUCCESS;
2619
2620         return qla81xx_write_mpi_register(vha, mb);
2621 }
2622
2623 /**
2624  * qla24xx_reset_risc() - Perform full reset of ISP24xx RISC.
2625  * @vha: HA context
2626  *
2627  * Returns 0 on success.
2628  */
2629 static inline int
2630 qla24xx_reset_risc(scsi_qla_host_t *vha)
2631 {
2632         unsigned long flags = 0;
2633         struct qla_hw_data *ha = vha->hw;
2634         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
2635         uint32_t cnt;
2636         uint16_t wd;
2637         static int abts_cnt; /* ISP abort retry counts */
2638         int rval = QLA_SUCCESS;
2639
2640         spin_lock_irqsave(&ha->hardware_lock, flags);
2641
2642         /* Reset RISC. */
2643         WRT_REG_DWORD(&reg->ctrl_status, CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
2644         for (cnt = 0; cnt < 30000; cnt++) {
2645                 if ((RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE) == 0)
2646                         break;
2647
2648                 udelay(10);
2649         }
2650
2651         if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE))
2652                 set_bit(DMA_SHUTDOWN_CMPL, &ha->fw_dump_cap_flags);
2653
2654         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017e,
2655             "HCCR: 0x%x, Control Status %x, DMA active status:0x%x\n",
2656             RD_REG_DWORD(&reg->hccr),
2657             RD_REG_DWORD(&reg->ctrl_status),
2658             (RD_REG_DWORD(&reg->ctrl_status) & CSRX_DMA_ACTIVE));
2659
2660         WRT_REG_DWORD(&reg->ctrl_status,
2661             CSRX_ISP_SOFT_RESET|CSRX_DMA_SHUTDOWN|MWB_4096_BYTES);
2662         pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
2663
2664         udelay(100);
2665
2666         /* Wait for firmware to complete NVRAM accesses. */
2667         RD_REG_WORD(&reg->mailbox0);
2668         for (cnt = 10000; RD_REG_WORD(&reg->mailbox0) != 0 &&
2669             rval == QLA_SUCCESS; cnt--) {
2670                 barrier();
2671                 if (cnt)
2672                         udelay(5);
2673                 else
2674                         rval = QLA_FUNCTION_TIMEOUT;
2675         }
2676
2677         if (rval == QLA_SUCCESS)
2678                 set_bit(ISP_MBX_RDY, &ha->fw_dump_cap_flags);
2679
2680         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x017f,
2681             "HCCR: 0x%x, MailBox0 Status 0x%x\n",
2682             RD_REG_DWORD(&reg->hccr),
2683             RD_REG_DWORD(&reg->mailbox0));
2684
2685         /* Wait for soft-reset to complete. */
2686         RD_REG_DWORD(&reg->ctrl_status);
2687         for (cnt = 0; cnt < 60; cnt++) {
2688                 barrier();
2689                 if ((RD_REG_DWORD(&reg->ctrl_status) &
2690                     CSRX_ISP_SOFT_RESET) == 0)
2691                         break;
2692
2693                 udelay(5);
2694         }
2695         if (!(RD_REG_DWORD(&reg->ctrl_status) & CSRX_ISP_SOFT_RESET))
2696                 set_bit(ISP_SOFT_RESET_CMPL, &ha->fw_dump_cap_flags);
2697
2698         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015d,
2699             "HCCR: 0x%x, Soft Reset status: 0x%x\n",
2700             RD_REG_DWORD(&reg->hccr),
2701             RD_REG_DWORD(&reg->ctrl_status));
2702
2703         /* If required, do an MPI FW reset now */
2704         if (test_and_clear_bit(MPI_RESET_NEEDED, &vha->dpc_flags)) {
2705                 if (qla81xx_reset_mpi(vha) != QLA_SUCCESS) {
2706                         if (++abts_cnt < 5) {
2707                                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2708                                 set_bit(MPI_RESET_NEEDED, &vha->dpc_flags);
2709                         } else {
2710                                 /*
2711                                  * We exhausted the ISP abort retries. We have to
2712                                  * set the board offline.
2713                                  */
2714                                 abts_cnt = 0;
2715                                 vha->flags.online = 0;
2716                         }
2717                 }
2718         }
2719
2720         WRT_REG_DWORD(&reg->hccr, HCCRX_SET_RISC_RESET);
2721         RD_REG_DWORD(&reg->hccr);
2722
2723         WRT_REG_DWORD(&reg->hccr, HCCRX_REL_RISC_PAUSE);
2724         RD_REG_DWORD(&reg->hccr);
2725
2726         WRT_REG_DWORD(&reg->hccr, HCCRX_CLR_RISC_RESET);
2727         RD_REG_DWORD(&reg->hccr);
2728
2729         RD_REG_WORD(&reg->mailbox0);
2730         for (cnt = 60; RD_REG_WORD(&reg->mailbox0) != 0 &&
2731             rval == QLA_SUCCESS; cnt--) {
2732                 barrier();
2733                 if (cnt)
2734                         udelay(5);
2735                 else
2736                         rval = QLA_FUNCTION_TIMEOUT;
2737         }
2738         if (rval == QLA_SUCCESS)
2739                 set_bit(RISC_RDY_AFT_RESET, &ha->fw_dump_cap_flags);
2740
2741         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015e,
2742             "Host Risc 0x%x, mailbox0 0x%x\n",
2743             RD_REG_DWORD(&reg->hccr),
2744              RD_REG_WORD(&reg->mailbox0));
2745
2746         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2747
2748         ql_dbg(ql_dbg_init + ql_dbg_verbose, vha, 0x015f,
2749             "Driver in %s mode\n",
2750             IS_NOPOLLING_TYPE(ha) ? "Interrupt" : "Polling");
2751
2752         if (IS_NOPOLLING_TYPE(ha))
2753                 ha->isp_ops->enable_intrs(ha);
2754
2755         return rval;
2756 }
2757
2758 static void
2759 qla25xx_read_risc_sema_reg(scsi_qla_host_t *vha, uint32_t *data)
2760 {
2761         struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2762
2763         WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2764         *data = RD_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET);
2765
2766 }
2767
2768 static void
2769 qla25xx_write_risc_sema_reg(scsi_qla_host_t *vha, uint32_t data)
2770 {
2771         struct device_reg_24xx __iomem *reg = &vha->hw->iobase->isp24;
2772
2773         WRT_REG_DWORD(&reg->iobase_addr, RISC_REGISTER_BASE_OFFSET);
2774         WRT_REG_DWORD(&reg->iobase_window + RISC_REGISTER_WINDOW_OFFET, data);
2775 }
2776
2777 static void
2778 qla25xx_manipulate_risc_semaphore(scsi_qla_host_t *vha)
2779 {
2780         uint32_t wd32 = 0;
2781         uint delta_msec = 100;
2782         uint elapsed_msec = 0;
2783         uint timeout_msec;
2784         ulong n;
2785
2786         if (vha->hw->pdev->subsystem_device != 0x0175 &&
2787             vha->hw->pdev->subsystem_device != 0x0240)
2788                 return;
2789
2790         WRT_REG_DWORD(&vha->hw->iobase->isp24.hccr, HCCRX_SET_RISC_PAUSE);
2791         udelay(100);
2792
2793 attempt:
2794         timeout_msec = TIMEOUT_SEMAPHORE;
2795         n = timeout_msec / delta_msec;
2796         while (n--) {
2797                 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_SET);
2798                 qla25xx_read_risc_sema_reg(vha, &wd32);
2799                 if (wd32 & RISC_SEMAPHORE)
2800                         break;
2801                 msleep(delta_msec);
2802                 elapsed_msec += delta_msec;
2803                 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2804                         goto force;
2805         }
2806
2807         if (!(wd32 & RISC_SEMAPHORE))
2808                 goto force;
2809
2810         if (!(wd32 & RISC_SEMAPHORE_FORCE))
2811                 goto acquired;
2812
2813         qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_CLR);
2814         timeout_msec = TIMEOUT_SEMAPHORE_FORCE;
2815         n = timeout_msec / delta_msec;
2816         while (n--) {
2817                 qla25xx_read_risc_sema_reg(vha, &wd32);
2818                 if (!(wd32 & RISC_SEMAPHORE_FORCE))
2819                         break;
2820                 msleep(delta_msec);
2821                 elapsed_msec += delta_msec;
2822                 if (elapsed_msec > TIMEOUT_TOTAL_ELAPSED)
2823                         goto force;
2824         }
2825
2826         if (wd32 & RISC_SEMAPHORE_FORCE)
2827                 qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_CLR);
2828
2829         goto attempt;
2830
2831 force:
2832         qla25xx_write_risc_sema_reg(vha, RISC_SEMAPHORE_FORCE_SET);
2833
2834 acquired:
2835         return;
2836 }
2837
2838 /**
2839  * qla24xx_reset_chip() - Reset ISP24xx chip.
2840  * @vha: HA context
2841  *
2842  * Returns 0 on success.
2843  */
2844 int
2845 qla24xx_reset_chip(scsi_qla_host_t *vha)
2846 {
2847         struct qla_hw_data *ha = vha->hw;
2848         int rval = QLA_FUNCTION_FAILED;
2849
2850         if (pci_channel_offline(ha->pdev) &&
2851             ha->flags.pci_channel_io_perm_failure) {
2852                 return rval;
2853         }
2854
2855         ha->isp_ops->disable_intrs(ha);
2856
2857         qla25xx_manipulate_risc_semaphore(vha);
2858
2859         /* Perform RISC reset. */
2860         rval = qla24xx_reset_risc(vha);
2861
2862         return rval;
2863 }
2864
2865 /**
2866  * qla2x00_chip_diag() - Test chip for proper operation.
2867  * @vha: HA context
2868  *
2869  * Returns 0 on success.
2870  */
2871 int
2872 qla2x00_chip_diag(scsi_qla_host_t *vha)
2873 {
2874         int             rval;
2875         struct qla_hw_data *ha = vha->hw;
2876         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
2877         unsigned long   flags = 0;
2878         uint16_t        data;
2879         uint32_t        cnt;
2880         uint16_t        mb[5];
2881         struct req_que *req = ha->req_q_map[0];
2882
2883         /* Assume a failed state */
2884         rval = QLA_FUNCTION_FAILED;
2885
2886         ql_dbg(ql_dbg_init, vha, 0x007b, "Testing device at %p.\n",
2887                &reg->flash_address);
2888
2889         spin_lock_irqsave(&ha->hardware_lock, flags);
2890
2891         /* Reset ISP chip. */
2892         WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
2893
2894         /*
2895          * We need to have a delay here since the card will not respond while
2896          * in reset causing an MCA on some architectures.
2897          */
2898         udelay(20);
2899         data = qla2x00_debounce_register(&reg->ctrl_status);
2900         for (cnt = 6000000 ; cnt && (data & CSR_ISP_SOFT_RESET); cnt--) {
2901                 udelay(5);
2902                 data = RD_REG_WORD(&reg->ctrl_status);
2903                 barrier();
2904         }
2905
2906         if (!cnt)
2907                 goto chip_diag_failed;
2908
2909         ql_dbg(ql_dbg_init, vha, 0x007c,
2910             "Reset register cleared by chip reset.\n");
2911
2912         /* Reset RISC processor. */
2913         WRT_REG_WORD(&reg->hccr, HCCR_RESET_RISC);
2914         WRT_REG_WORD(&reg->hccr, HCCR_RELEASE_RISC);
2915
2916         /* Workaround for QLA2312 PCI parity error */
2917         if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
2918                 data = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 0));
2919                 for (cnt = 6000000; cnt && (data == MBS_BUSY); cnt--) {
2920                         udelay(5);
2921                         data = RD_MAILBOX_REG(ha, reg, 0);
2922                         barrier();
2923                 }
2924         } else
2925                 udelay(10);
2926
2927         if (!cnt)
2928                 goto chip_diag_failed;
2929
2930         /* Check product ID of chip */
2931         ql_dbg(ql_dbg_init, vha, 0x007d, "Checking product ID of chip.\n");
2932
2933         mb[1] = RD_MAILBOX_REG(ha, reg, 1);
2934         mb[2] = RD_MAILBOX_REG(ha, reg, 2);
2935         mb[3] = RD_MAILBOX_REG(ha, reg, 3);
2936         mb[4] = qla2x00_debounce_register(MAILBOX_REG(ha, reg, 4));
2937         if (mb[1] != PROD_ID_1 || (mb[2] != PROD_ID_2 && mb[2] != PROD_ID_2a) ||
2938             mb[3] != PROD_ID_3) {
2939                 ql_log(ql_log_warn, vha, 0x0062,
2940                     "Wrong product ID = 0x%x,0x%x,0x%x.\n",
2941                     mb[1], mb[2], mb[3]);
2942
2943                 goto chip_diag_failed;
2944         }
2945         ha->product_id[0] = mb[1];
2946         ha->product_id[1] = mb[2];
2947         ha->product_id[2] = mb[3];
2948         ha->product_id[3] = mb[4];
2949
2950         /* Adjust fw RISC transfer size */
2951         if (req->length > 1024)
2952                 ha->fw_transfer_size = REQUEST_ENTRY_SIZE * 1024;
2953         else
2954                 ha->fw_transfer_size = REQUEST_ENTRY_SIZE *
2955                     req->length;
2956
2957         if (IS_QLA2200(ha) &&
2958             RD_MAILBOX_REG(ha, reg, 7) == QLA2200A_RISC_ROM_VER) {
2959                 /* Limit firmware transfer size with a 2200A */
2960                 ql_dbg(ql_dbg_init, vha, 0x007e, "Found QLA2200A Chip.\n");
2961
2962                 ha->device_type |= DT_ISP2200A;
2963                 ha->fw_transfer_size = 128;
2964         }
2965
2966         /* Wrap Incoming Mailboxes Test. */
2967         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2968
2969         ql_dbg(ql_dbg_init, vha, 0x007f, "Checking mailboxes.\n");
2970         rval = qla2x00_mbx_reg_test(vha);
2971         if (rval)
2972                 ql_log(ql_log_warn, vha, 0x0080,
2973                     "Failed mailbox send register test.\n");
2974         else
2975                 /* Flag a successful rval */
2976                 rval = QLA_SUCCESS;
2977         spin_lock_irqsave(&ha->hardware_lock, flags);
2978
2979 chip_diag_failed:
2980         if (rval)
2981                 ql_log(ql_log_info, vha, 0x0081,
2982                     "Chip diagnostics **** FAILED ****.\n");
2983
2984         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2985
2986         return (rval);
2987 }
2988
2989 /**
2990  * qla24xx_chip_diag() - Test ISP24xx for proper operation.
2991  * @vha: HA context
2992  *
2993  * Returns 0 on success.
2994  */
2995 int
2996 qla24xx_chip_diag(scsi_qla_host_t *vha)
2997 {
2998         int rval;
2999         struct qla_hw_data *ha = vha->hw;
3000         struct req_que *req = ha->req_q_map[0];
3001
3002         if (IS_P3P_TYPE(ha))
3003                 return QLA_SUCCESS;
3004
3005         ha->fw_transfer_size = REQUEST_ENTRY_SIZE * req->length;
3006
3007         rval = qla2x00_mbx_reg_test(vha);
3008         if (rval) {
3009                 ql_log(ql_log_warn, vha, 0x0082,
3010                     "Failed mailbox send register test.\n");
3011         } else {
3012                 /* Flag a successful rval */
3013                 rval = QLA_SUCCESS;
3014         }
3015
3016         return rval;
3017 }
3018
3019 static void
3020 qla2x00_alloc_offload_mem(scsi_qla_host_t *vha)
3021 {
3022         int rval;
3023         dma_addr_t tc_dma;
3024         void *tc;
3025         struct qla_hw_data *ha = vha->hw;
3026
3027         if (ha->eft) {
3028                 ql_dbg(ql_dbg_init, vha, 0x00bd,
3029                     "%s: Offload Mem is already allocated.\n",
3030                     __func__);
3031                 return;
3032         }
3033
3034         if (IS_FWI2_CAPABLE(ha)) {
3035                 /* Allocate memory for Fibre Channel Event Buffer. */
3036                 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
3037                     !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
3038                         goto try_eft;
3039
3040                 if (ha->fce)
3041                         dma_free_coherent(&ha->pdev->dev,
3042                             FCE_SIZE, ha->fce, ha->fce_dma);
3043
3044                 /* Allocate memory for Fibre Channel Event Buffer. */
3045                 tc = dma_alloc_coherent(&ha->pdev->dev, FCE_SIZE, &tc_dma,
3046                                         GFP_KERNEL);
3047                 if (!tc) {
3048                         ql_log(ql_log_warn, vha, 0x00be,
3049                             "Unable to allocate (%d KB) for FCE.\n",
3050                             FCE_SIZE / 1024);
3051                         goto try_eft;
3052                 }
3053
3054                 rval = qla2x00_enable_fce_trace(vha, tc_dma, FCE_NUM_BUFFERS,
3055                     ha->fce_mb, &ha->fce_bufs);
3056                 if (rval) {
3057                         ql_log(ql_log_warn, vha, 0x00bf,
3058                             "Unable to initialize FCE (%d).\n", rval);
3059                         dma_free_coherent(&ha->pdev->dev, FCE_SIZE, tc,
3060                             tc_dma);
3061                         ha->flags.fce_enabled = 0;
3062                         goto try_eft;
3063                 }
3064                 ql_dbg(ql_dbg_init, vha, 0x00c0,
3065                     "Allocate (%d KB) for FCE...\n", FCE_SIZE / 1024);
3066
3067                 ha->flags.fce_enabled = 1;
3068                 ha->fce_dma = tc_dma;
3069                 ha->fce = tc;
3070
3071 try_eft:
3072                 if (ha->eft)
3073                         dma_free_coherent(&ha->pdev->dev,
3074                             EFT_SIZE, ha->eft, ha->eft_dma);
3075
3076                 /* Allocate memory for Extended Trace Buffer. */
3077                 tc = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
3078                                         GFP_KERNEL);
3079                 if (!tc) {
3080                         ql_log(ql_log_warn, vha, 0x00c1,
3081                             "Unable to allocate (%d KB) for EFT.\n",
3082                             EFT_SIZE / 1024);
3083                         goto eft_err;
3084                 }
3085
3086                 rval = qla2x00_enable_eft_trace(vha, tc_dma, EFT_NUM_BUFFERS);
3087                 if (rval) {
3088                         ql_log(ql_log_warn, vha, 0x00c2,
3089                             "Unable to initialize EFT (%d).\n", rval);
3090                         dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
3091                             tc_dma);
3092                         goto eft_err;
3093                 }
3094                 ql_dbg(ql_dbg_init, vha, 0x00c3,
3095                     "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
3096
3097                 ha->eft_dma = tc_dma;
3098                 ha->eft = tc;
3099         }
3100
3101 eft_err:
3102         return;
3103 }
3104
3105 void
3106 qla2x00_alloc_fw_dump(scsi_qla_host_t *vha)
3107 {
3108         int rval;
3109         uint32_t dump_size, fixed_size, mem_size, req_q_size, rsp_q_size,
3110             eft_size, fce_size, mq_size;
3111         struct qla_hw_data *ha = vha->hw;
3112         struct req_que *req = ha->req_q_map[0];
3113         struct rsp_que *rsp = ha->rsp_q_map[0];
3114         struct qla2xxx_fw_dump *fw_dump;
3115         dma_addr_t tc_dma;
3116         void *tc;
3117
3118         dump_size = fixed_size = mem_size = eft_size = fce_size = mq_size = 0;
3119         req_q_size = rsp_q_size = 0;
3120
3121         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
3122                 fixed_size = sizeof(struct qla2100_fw_dump);
3123         } else if (IS_QLA23XX(ha)) {
3124                 fixed_size = offsetof(struct qla2300_fw_dump, data_ram);
3125                 mem_size = (ha->fw_memory_size - 0x11000 + 1) *
3126                     sizeof(uint16_t);
3127         } else if (IS_FWI2_CAPABLE(ha)) {
3128                 if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha))
3129                         fixed_size = offsetof(struct qla83xx_fw_dump, ext_mem);
3130                 else if (IS_QLA81XX(ha))
3131                         fixed_size = offsetof(struct qla81xx_fw_dump, ext_mem);
3132                 else if (IS_QLA25XX(ha))
3133                         fixed_size = offsetof(struct qla25xx_fw_dump, ext_mem);
3134                 else
3135                         fixed_size = offsetof(struct qla24xx_fw_dump, ext_mem);
3136
3137                 mem_size = (ha->fw_memory_size - 0x100000 + 1) *
3138                     sizeof(uint32_t);
3139                 if (ha->mqenable) {
3140                         if (!IS_QLA83XX(ha) && !IS_QLA27XX(ha) &&
3141                             !IS_QLA28XX(ha))
3142                                 mq_size = sizeof(struct qla2xxx_mq_chain);
3143                         /*
3144                          * Allocate maximum buffer size for all queues - Q0.
3145                          * Resizing must be done at end-of-dump processing.
3146                          */
3147                         mq_size += (ha->max_req_queues - 1) *
3148                             (req->length * sizeof(request_t));
3149                         mq_size += (ha->max_rsp_queues - 1) *
3150                             (rsp->length * sizeof(response_t));
3151                 }
3152                 if (ha->tgt.atio_ring)
3153                         mq_size += ha->tgt.atio_q_length * sizeof(request_t);
3154                 /* Allocate memory for Fibre Channel Event Buffer. */
3155                 if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
3156                     !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
3157                         goto try_eft;
3158
3159                 fce_size = sizeof(struct qla2xxx_fce_chain) + FCE_SIZE;
3160 try_eft:
3161                 if (ha->eft)
3162                         dma_free_coherent(&ha->pdev->dev,
3163                             EFT_SIZE, ha->eft, ha->eft_dma);
3164
3165                 /* Allocate memory for Extended Trace Buffer. */
3166                 tc = dma_alloc_coherent(&ha->pdev->dev, EFT_SIZE, &tc_dma,
3167                                          GFP_KERNEL);
3168                 if (!tc) {
3169                         ql_log(ql_log_warn, vha, 0x00c1,
3170                             "Unable to allocate (%d KB) for EFT.\n",
3171                             EFT_SIZE / 1024);
3172                         goto allocate;
3173                 }
3174
3175                 rval = qla2x00_enable_eft_trace(vha, tc_dma, EFT_NUM_BUFFERS);
3176                 if (rval) {
3177                         ql_log(ql_log_warn, vha, 0x00c2,
3178                             "Unable to initialize EFT (%d).\n", rval);
3179                         dma_free_coherent(&ha->pdev->dev, EFT_SIZE, tc,
3180                             tc_dma);
3181                 }
3182                 ql_dbg(ql_dbg_init, vha, 0x00c3,
3183                     "Allocated (%d KB) EFT ...\n", EFT_SIZE / 1024);
3184                 eft_size = EFT_SIZE;
3185         }
3186
3187         if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
3188                 struct fwdt *fwdt = ha->fwdt;
3189                 uint j;
3190
3191                 for (j = 0; j < 2; j++, fwdt++) {
3192                         if (!fwdt->template) {
3193                                 ql_log(ql_log_warn, vha, 0x00ba,
3194                                     "-> fwdt%u no template\n", j);
3195                                 continue;
3196                         }
3197                         ql_dbg(ql_dbg_init, vha, 0x00fa,
3198                             "-> fwdt%u calculating fwdump size...\n", j);
3199                         fwdt->dump_size = qla27xx_fwdt_calculate_dump_size(
3200                             vha, fwdt->template);
3201                         ql_dbg(ql_dbg_init, vha, 0x00fa,
3202                             "-> fwdt%u calculated fwdump size = %#lx bytes\n",
3203                             j, fwdt->dump_size);
3204                         dump_size += fwdt->dump_size;
3205                 }
3206                 goto allocate;
3207         }
3208
3209         req_q_size = req->length * sizeof(request_t);
3210         rsp_q_size = rsp->length * sizeof(response_t);
3211         dump_size = offsetof(struct qla2xxx_fw_dump, isp);
3212         dump_size += fixed_size + mem_size + req_q_size + rsp_q_size + eft_size;
3213         ha->chain_offset = dump_size;
3214         dump_size += mq_size + fce_size;
3215
3216         if (ha->exchoffld_buf)
3217                 dump_size += sizeof(struct qla2xxx_offld_chain) +
3218                         ha->exchoffld_size;
3219         if (ha->exlogin_buf)
3220                 dump_size += sizeof(struct qla2xxx_offld_chain) +
3221                         ha->exlogin_size;
3222
3223 allocate:
3224         if (!ha->fw_dump_len || dump_size > ha->fw_dump_alloc_len) {
3225
3226                 ql_dbg(ql_dbg_init, vha, 0x00c5,
3227                     "%s dump_size %d fw_dump_len %d fw_dump_alloc_len %d\n",
3228                     __func__, dump_size, ha->fw_dump_len,
3229                     ha->fw_dump_alloc_len);
3230
3231                 fw_dump = vmalloc(dump_size);
3232                 if (!fw_dump) {
3233                         ql_log(ql_log_warn, vha, 0x00c4,
3234                             "Unable to allocate (%d KB) for firmware dump.\n",
3235                             dump_size / 1024);
3236                 } else {
3237                         mutex_lock(&ha->optrom_mutex);
3238                         if (ha->fw_dumped) {
3239                                 memcpy(fw_dump, ha->fw_dump, ha->fw_dump_len);