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