]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/scsi/qla2xxx/qla_os.c
[SCSI] qla2xxx: Add QoS support.
[mv-sheeva.git] / drivers / scsi / qla2xxx / qla_os.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2008 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8
9 #include <linux/moduleparam.h>
10 #include <linux/vmalloc.h>
11 #include <linux/delay.h>
12 #include <linux/kthread.h>
13 #include <linux/mutex.h>
14
15 #include <scsi/scsi_tcq.h>
16 #include <scsi/scsicam.h>
17 #include <scsi/scsi_transport.h>
18 #include <scsi/scsi_transport_fc.h>
19
20 /*
21  * Driver version
22  */
23 char qla2x00_version_str[40];
24
25 /*
26  * SRB allocation cache
27  */
28 static struct kmem_cache *srb_cachep;
29
30 int ql2xlogintimeout = 20;
31 module_param(ql2xlogintimeout, int, S_IRUGO|S_IRUSR);
32 MODULE_PARM_DESC(ql2xlogintimeout,
33                 "Login timeout value in seconds.");
34
35 int qlport_down_retry;
36 module_param(qlport_down_retry, int, S_IRUGO|S_IRUSR);
37 MODULE_PARM_DESC(qlport_down_retry,
38                 "Maximum number of command retries to a port that returns "
39                 "a PORT-DOWN status.");
40
41 int ql2xplogiabsentdevice;
42 module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
43 MODULE_PARM_DESC(ql2xplogiabsentdevice,
44                 "Option to enable PLOGI to devices that are not present after "
45                 "a Fabric scan.  This is needed for several broken switches. "
46                 "Default is 0 - no PLOGI. 1 - perfom PLOGI.");
47
48 int ql2xloginretrycount = 0;
49 module_param(ql2xloginretrycount, int, S_IRUGO|S_IRUSR);
50 MODULE_PARM_DESC(ql2xloginretrycount,
51                 "Specify an alternate value for the NVRAM login retry count.");
52
53 int ql2xallocfwdump = 1;
54 module_param(ql2xallocfwdump, int, S_IRUGO|S_IRUSR);
55 MODULE_PARM_DESC(ql2xallocfwdump,
56                 "Option to enable allocation of memory for a firmware dump "
57                 "during HBA initialization.  Memory allocation requirements "
58                 "vary by ISP type.  Default is 1 - allocate memory.");
59
60 int ql2xextended_error_logging;
61 module_param(ql2xextended_error_logging, int, S_IRUGO|S_IWUSR);
62 MODULE_PARM_DESC(ql2xextended_error_logging,
63                 "Option to enable extended error logging, "
64                 "Default is 0 - no logging. 1 - log errors.");
65
66 static void qla2x00_free_device(scsi_qla_host_t *);
67
68 int ql2xfdmienable=1;
69 module_param(ql2xfdmienable, int, S_IRUGO|S_IRUSR);
70 MODULE_PARM_DESC(ql2xfdmienable,
71                 "Enables FDMI registratons "
72                 "Default is 0 - no FDMI. 1 - perfom FDMI.");
73
74 #define MAX_Q_DEPTH    32
75 static int ql2xmaxqdepth = MAX_Q_DEPTH;
76 module_param(ql2xmaxqdepth, int, S_IRUGO|S_IWUSR);
77 MODULE_PARM_DESC(ql2xmaxqdepth,
78                 "Maximum queue depth to report for target devices.");
79
80 int ql2xqfullrampup = 120;
81 module_param(ql2xqfullrampup, int, S_IRUGO|S_IWUSR);
82 MODULE_PARM_DESC(ql2xqfullrampup,
83                 "Number of seconds to wait to begin to ramp-up the queue "
84                 "depth for a device after a queue-full condition has been "
85                 "detected.  Default is 120 seconds.");
86
87 int ql2xiidmaenable=1;
88 module_param(ql2xiidmaenable, int, S_IRUGO|S_IRUSR);
89 MODULE_PARM_DESC(ql2xiidmaenable,
90                 "Enables iIDMA settings "
91                 "Default is 1 - perform iIDMA. 0 - no iIDMA.");
92
93 int ql2xmaxqueues = 1;
94 module_param(ql2xmaxqueues, int, S_IRUGO|S_IRUSR);
95 MODULE_PARM_DESC(ql2xmaxqueues,
96                 "Enables MQ settings "
97                 "Default is 1 for single queue. Set it to number \
98                         of queues in MQ mode.");
99 /*
100  * SCSI host template entry points
101  */
102 static int qla2xxx_slave_configure(struct scsi_device * device);
103 static int qla2xxx_slave_alloc(struct scsi_device *);
104 static int qla2xxx_scan_finished(struct Scsi_Host *, unsigned long time);
105 static void qla2xxx_scan_start(struct Scsi_Host *);
106 static void qla2xxx_slave_destroy(struct scsi_device *);
107 static int qla2xxx_queuecommand(struct scsi_cmnd *cmd,
108                 void (*fn)(struct scsi_cmnd *));
109 static int qla2xxx_eh_abort(struct scsi_cmnd *);
110 static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
111 static int qla2xxx_eh_target_reset(struct scsi_cmnd *);
112 static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
113 static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
114
115 static int qla2x00_change_queue_depth(struct scsi_device *, int);
116 static int qla2x00_change_queue_type(struct scsi_device *, int);
117
118 struct scsi_host_template qla2xxx_driver_template = {
119         .module                 = THIS_MODULE,
120         .name                   = QLA2XXX_DRIVER_NAME,
121         .queuecommand           = qla2xxx_queuecommand,
122
123         .eh_abort_handler       = qla2xxx_eh_abort,
124         .eh_device_reset_handler = qla2xxx_eh_device_reset,
125         .eh_target_reset_handler = qla2xxx_eh_target_reset,
126         .eh_bus_reset_handler   = qla2xxx_eh_bus_reset,
127         .eh_host_reset_handler  = qla2xxx_eh_host_reset,
128
129         .slave_configure        = qla2xxx_slave_configure,
130
131         .slave_alloc            = qla2xxx_slave_alloc,
132         .slave_destroy          = qla2xxx_slave_destroy,
133         .scan_finished          = qla2xxx_scan_finished,
134         .scan_start             = qla2xxx_scan_start,
135         .change_queue_depth     = qla2x00_change_queue_depth,
136         .change_queue_type      = qla2x00_change_queue_type,
137         .this_id                = -1,
138         .cmd_per_lun            = 3,
139         .use_clustering         = ENABLE_CLUSTERING,
140         .sg_tablesize           = SG_ALL,
141
142         .max_sectors            = 0xFFFF,
143         .shost_attrs            = qla2x00_host_attrs,
144 };
145
146 static struct scsi_transport_template *qla2xxx_transport_template = NULL;
147 struct scsi_transport_template *qla2xxx_transport_vport_template = NULL;
148
149 /* TODO Convert to inlines
150  *
151  * Timer routines
152  */
153
154 __inline__ void
155 qla2x00_start_timer(scsi_qla_host_t *vha, void *func, unsigned long interval)
156 {
157         init_timer(&vha->timer);
158         vha->timer.expires = jiffies + interval * HZ;
159         vha->timer.data = (unsigned long)vha;
160         vha->timer.function = (void (*)(unsigned long))func;
161         add_timer(&vha->timer);
162         vha->timer_active = 1;
163 }
164
165 static inline void
166 qla2x00_restart_timer(scsi_qla_host_t *vha, unsigned long interval)
167 {
168         mod_timer(&vha->timer, jiffies + interval * HZ);
169 }
170
171 static __inline__ void
172 qla2x00_stop_timer(scsi_qla_host_t *vha)
173 {
174         del_timer_sync(&vha->timer);
175         vha->timer_active = 0;
176 }
177
178 static int qla2x00_do_dpc(void *data);
179
180 static void qla2x00_rst_aen(scsi_qla_host_t *);
181
182 static int qla2x00_mem_alloc(struct qla_hw_data *, uint16_t, uint16_t,
183         struct req_que **, struct rsp_que **);
184 static void qla2x00_mem_free(struct qla_hw_data *);
185 static void qla2x00_sp_free_dma(srb_t *);
186
187 /* -------------------------------------------------------------------------- */
188 static int qla2x00_alloc_queues(struct qla_hw_data *ha)
189 {
190         ha->req_q_map = kzalloc(sizeof(struct req_que *) * ha->max_req_queues,
191                                 GFP_KERNEL);
192         if (!ha->req_q_map) {
193                 qla_printk(KERN_WARNING, ha,
194                         "Unable to allocate memory for request queue ptrs\n");
195                 goto fail_req_map;
196         }
197
198         ha->rsp_q_map = kzalloc(sizeof(struct rsp_que *) * ha->max_rsp_queues,
199                                 GFP_KERNEL);
200         if (!ha->rsp_q_map) {
201                 qla_printk(KERN_WARNING, ha,
202                         "Unable to allocate memory for response queue ptrs\n");
203                 goto fail_rsp_map;
204         }
205         set_bit(0, ha->rsp_qid_map);
206         set_bit(0, ha->req_qid_map);
207         return 1;
208
209 fail_rsp_map:
210         kfree(ha->req_q_map);
211         ha->req_q_map = NULL;
212 fail_req_map:
213         return -ENOMEM;
214 }
215
216 static void qla2x00_free_req_que(struct qla_hw_data *ha, struct req_que *req)
217 {
218         if (req && req->ring)
219                 dma_free_coherent(&ha->pdev->dev,
220                 (req->length + 1) * sizeof(request_t),
221                 req->ring, req->dma);
222
223         kfree(req);
224         req = NULL;
225 }
226
227 static void qla2x00_free_rsp_que(struct qla_hw_data *ha, struct rsp_que *rsp)
228 {
229         if (rsp && rsp->ring)
230                 dma_free_coherent(&ha->pdev->dev,
231                 (rsp->length + 1) * sizeof(response_t),
232                 rsp->ring, rsp->dma);
233
234         kfree(rsp);
235         rsp = NULL;
236 }
237
238 static void qla2x00_free_queues(struct qla_hw_data *ha)
239 {
240         struct req_que *req;
241         struct rsp_que *rsp;
242         int cnt;
243
244         for (cnt = 0; cnt < ha->max_req_queues; cnt++) {
245                 req = ha->req_q_map[cnt];
246                 qla2x00_free_req_que(ha, req);
247         }
248         kfree(ha->req_q_map);
249         ha->req_q_map = NULL;
250
251         for (cnt = 0; cnt < ha->max_rsp_queues; cnt++) {
252                 rsp = ha->rsp_q_map[cnt];
253                 qla2x00_free_rsp_que(ha, rsp);
254         }
255         kfree(ha->rsp_q_map);
256         ha->rsp_q_map = NULL;
257 }
258
259 static char *
260 qla2x00_pci_info_str(struct scsi_qla_host *vha, char *str)
261 {
262         struct qla_hw_data *ha = vha->hw;
263         static char *pci_bus_modes[] = {
264                 "33", "66", "100", "133",
265         };
266         uint16_t pci_bus;
267
268         strcpy(str, "PCI");
269         pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
270         if (pci_bus) {
271                 strcat(str, "-X (");
272                 strcat(str, pci_bus_modes[pci_bus]);
273         } else {
274                 pci_bus = (ha->pci_attr & BIT_8) >> 8;
275                 strcat(str, " (");
276                 strcat(str, pci_bus_modes[pci_bus]);
277         }
278         strcat(str, " MHz)");
279
280         return (str);
281 }
282
283 static char *
284 qla24xx_pci_info_str(struct scsi_qla_host *vha, char *str)
285 {
286         static char *pci_bus_modes[] = { "33", "66", "100", "133", };
287         struct qla_hw_data *ha = vha->hw;
288         uint32_t pci_bus;
289         int pcie_reg;
290
291         pcie_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
292         if (pcie_reg) {
293                 char lwstr[6];
294                 uint16_t pcie_lstat, lspeed, lwidth;
295
296                 pcie_reg += 0x12;
297                 pci_read_config_word(ha->pdev, pcie_reg, &pcie_lstat);
298                 lspeed = pcie_lstat & (BIT_0 | BIT_1 | BIT_2 | BIT_3);
299                 lwidth = (pcie_lstat &
300                     (BIT_4 | BIT_5 | BIT_6 | BIT_7 | BIT_8 | BIT_9)) >> 4;
301
302                 strcpy(str, "PCIe (");
303                 if (lspeed == 1)
304                         strcat(str, "2.5GT/s ");
305                 else if (lspeed == 2)
306                         strcat(str, "5.0GT/s ");
307                 else
308                         strcat(str, "<unknown> ");
309                 snprintf(lwstr, sizeof(lwstr), "x%d)", lwidth);
310                 strcat(str, lwstr);
311
312                 return str;
313         }
314
315         strcpy(str, "PCI");
316         pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
317         if (pci_bus == 0 || pci_bus == 8) {
318                 strcat(str, " (");
319                 strcat(str, pci_bus_modes[pci_bus >> 3]);
320         } else {
321                 strcat(str, "-X ");
322                 if (pci_bus & BIT_2)
323                         strcat(str, "Mode 2");
324                 else
325                         strcat(str, "Mode 1");
326                 strcat(str, " (");
327                 strcat(str, pci_bus_modes[pci_bus & ~BIT_2]);
328         }
329         strcat(str, " MHz)");
330
331         return str;
332 }
333
334 static char *
335 qla2x00_fw_version_str(struct scsi_qla_host *vha, char *str)
336 {
337         char un_str[10];
338         struct qla_hw_data *ha = vha->hw;
339
340         sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
341             ha->fw_minor_version,
342             ha->fw_subminor_version);
343
344         if (ha->fw_attributes & BIT_9) {
345                 strcat(str, "FLX");
346                 return (str);
347         }
348
349         switch (ha->fw_attributes & 0xFF) {
350         case 0x7:
351                 strcat(str, "EF");
352                 break;
353         case 0x17:
354                 strcat(str, "TP");
355                 break;
356         case 0x37:
357                 strcat(str, "IP");
358                 break;
359         case 0x77:
360                 strcat(str, "VI");
361                 break;
362         default:
363                 sprintf(un_str, "(%x)", ha->fw_attributes);
364                 strcat(str, un_str);
365                 break;
366         }
367         if (ha->fw_attributes & 0x100)
368                 strcat(str, "X");
369
370         return (str);
371 }
372
373 static char *
374 qla24xx_fw_version_str(struct scsi_qla_host *vha, char *str)
375 {
376         struct qla_hw_data *ha = vha->hw;
377
378         sprintf(str, "%d.%02d.%02d (%x)", ha->fw_major_version,
379             ha->fw_minor_version, ha->fw_subminor_version, ha->fw_attributes);
380         return str;
381 }
382
383 static inline srb_t *
384 qla2x00_get_new_sp(scsi_qla_host_t *vha, fc_port_t *fcport,
385     struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
386 {
387         srb_t *sp;
388         struct qla_hw_data *ha = vha->hw;
389
390         sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
391         if (!sp)
392                 return sp;
393
394         sp->fcport = fcport;
395         sp->cmd = cmd;
396         sp->que = ha->req_q_map[0];
397         sp->flags = 0;
398         CMD_SP(cmd) = (void *)sp;
399         cmd->scsi_done = done;
400
401         return sp;
402 }
403
404 static int
405 qla2xxx_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
406 {
407         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
408         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
409         struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
410         struct qla_hw_data *ha = vha->hw;
411         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
412         srb_t *sp;
413         int rval;
414
415         if (unlikely(pci_channel_offline(ha->pdev))) {
416                 if (ha->pdev->error_state == pci_channel_io_frozen)
417                         cmd->result = DID_REQUEUE << 16;
418                 else
419                         cmd->result = DID_NO_CONNECT << 16;
420                 goto qc24_fail_command;
421         }
422
423         rval = fc_remote_port_chkready(rport);
424         if (rval) {
425                 cmd->result = rval;
426                 goto qc24_fail_command;
427         }
428
429         /* Close window on fcport/rport state-transitioning. */
430         if (fcport->drport)
431                 goto qc24_target_busy;
432
433         if (atomic_read(&fcport->state) != FCS_ONLINE) {
434                 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
435                     atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
436                         cmd->result = DID_NO_CONNECT << 16;
437                         goto qc24_fail_command;
438                 }
439                 goto qc24_target_busy;
440         }
441
442         spin_unlock_irq(vha->host->host_lock);
443
444         sp = qla2x00_get_new_sp(base_vha, fcport, cmd, done);
445         if (!sp)
446                 goto qc24_host_busy_lock;
447
448         rval = ha->isp_ops->start_scsi(sp);
449         if (rval != QLA_SUCCESS)
450                 goto qc24_host_busy_free_sp;
451
452         spin_lock_irq(vha->host->host_lock);
453
454         return 0;
455
456 qc24_host_busy_free_sp:
457         qla2x00_sp_free_dma(sp);
458         mempool_free(sp, ha->srb_mempool);
459
460 qc24_host_busy_lock:
461         spin_lock_irq(vha->host->host_lock);
462         return SCSI_MLQUEUE_HOST_BUSY;
463
464 qc24_target_busy:
465         return SCSI_MLQUEUE_TARGET_BUSY;
466
467 qc24_fail_command:
468         done(cmd);
469
470         return 0;
471 }
472
473
474 /*
475  * qla2x00_eh_wait_on_command
476  *    Waits for the command to be returned by the Firmware for some
477  *    max time.
478  *
479  * Input:
480  *    cmd = Scsi Command to wait on.
481  *
482  * Return:
483  *    Not Found : 0
484  *    Found : 1
485  */
486 static int
487 qla2x00_eh_wait_on_command(struct scsi_cmnd *cmd)
488 {
489 #define ABORT_POLLING_PERIOD    1000
490 #define ABORT_WAIT_ITER         ((10 * 1000) / (ABORT_POLLING_PERIOD))
491         unsigned long wait_iter = ABORT_WAIT_ITER;
492         int ret = QLA_SUCCESS;
493
494         while (CMD_SP(cmd)) {
495                 msleep(ABORT_POLLING_PERIOD);
496
497                 if (--wait_iter)
498                         break;
499         }
500         if (CMD_SP(cmd))
501                 ret = QLA_FUNCTION_FAILED;
502
503         return ret;
504 }
505
506 /*
507  * qla2x00_wait_for_hba_online
508  *    Wait till the HBA is online after going through
509  *    <= MAX_RETRIES_OF_ISP_ABORT  or
510  *    finally HBA is disabled ie marked offline
511  *
512  * Input:
513  *     ha - pointer to host adapter structure
514  *
515  * Note:
516  *    Does context switching-Release SPIN_LOCK
517  *    (if any) before calling this routine.
518  *
519  * Return:
520  *    Success (Adapter is online) : 0
521  *    Failed  (Adapter is offline/disabled) : 1
522  */
523 int
524 qla2x00_wait_for_hba_online(scsi_qla_host_t *vha)
525 {
526         int             return_status;
527         unsigned long   wait_online;
528         struct qla_hw_data *ha = vha->hw;
529         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
530
531         wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
532         while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
533             test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
534             test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
535             ha->dpc_active) && time_before(jiffies, wait_online)) {
536
537                 msleep(1000);
538         }
539         if (base_vha->flags.online)
540                 return_status = QLA_SUCCESS;
541         else
542                 return_status = QLA_FUNCTION_FAILED;
543
544         return (return_status);
545 }
546
547 int
548 qla2x00_wait_for_chip_reset(scsi_qla_host_t *vha)
549 {
550         int             return_status;
551         unsigned long   wait_reset;
552         struct qla_hw_data *ha = vha->hw;
553         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
554
555         wait_reset = jiffies + (MAX_LOOP_TIMEOUT * HZ);
556         while (((test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags)) ||
557             test_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags) ||
558             test_bit(ISP_ABORT_RETRY, &base_vha->dpc_flags) ||
559             ha->dpc_active) && time_before(jiffies, wait_reset)) {
560
561                 msleep(1000);
562
563                 if (!test_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags) &&
564                     ha->flags.chip_reset_done)
565                         break;
566         }
567         if (ha->flags.chip_reset_done)
568                 return_status = QLA_SUCCESS;
569         else
570                 return_status = QLA_FUNCTION_FAILED;
571
572         return return_status;
573 }
574
575 /*
576  * qla2x00_wait_for_loop_ready
577  *    Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
578  *    to be in LOOP_READY state.
579  * Input:
580  *     ha - pointer to host adapter structure
581  *
582  * Note:
583  *    Does context switching-Release SPIN_LOCK
584  *    (if any) before calling this routine.
585  *
586  *
587  * Return:
588  *    Success (LOOP_READY) : 0
589  *    Failed  (LOOP_NOT_READY) : 1
590  */
591 static inline int
592 qla2x00_wait_for_loop_ready(scsi_qla_host_t *vha)
593 {
594         int      return_status = QLA_SUCCESS;
595         unsigned long loop_timeout ;
596         struct qla_hw_data *ha = vha->hw;
597         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
598
599         /* wait for 5 min at the max for loop to be ready */
600         loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ);
601
602         while ((!atomic_read(&base_vha->loop_down_timer) &&
603             atomic_read(&base_vha->loop_state) == LOOP_DOWN) ||
604             atomic_read(&base_vha->loop_state) != LOOP_READY) {
605                 if (atomic_read(&base_vha->loop_state) == LOOP_DEAD) {
606                         return_status = QLA_FUNCTION_FAILED;
607                         break;
608                 }
609                 msleep(1000);
610                 if (time_after_eq(jiffies, loop_timeout)) {
611                         return_status = QLA_FUNCTION_FAILED;
612                         break;
613                 }
614         }
615         return (return_status);
616 }
617
618 void
619 qla2x00_abort_fcport_cmds(fc_port_t *fcport)
620 {
621         int cnt;
622         unsigned long flags;
623         srb_t *sp;
624         scsi_qla_host_t *vha = fcport->vha;
625         struct qla_hw_data *ha = vha->hw;
626         struct req_que *req;
627
628         spin_lock_irqsave(&ha->hardware_lock, flags);
629         req = vha->req;
630         for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
631                 sp = req->outstanding_cmds[cnt];
632                 if (!sp)
633                         continue;
634                 if (sp->fcport != fcport)
635                         continue;
636
637                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
638                 if (ha->isp_ops->abort_command(sp)) {
639                         DEBUG2(qla_printk(KERN_WARNING, ha,
640                         "Abort failed --  %lx\n",
641                         sp->cmd->serial_number));
642                 } else {
643                         if (qla2x00_eh_wait_on_command(sp->cmd) !=
644                                 QLA_SUCCESS)
645                                 DEBUG2(qla_printk(KERN_WARNING, ha,
646                                 "Abort failed while waiting --  %lx\n",
647                                 sp->cmd->serial_number));
648                 }
649                 spin_lock_irqsave(&ha->hardware_lock, flags);
650         }
651         spin_unlock_irqrestore(&ha->hardware_lock, flags);
652 }
653
654 static void
655 qla2x00_block_error_handler(struct scsi_cmnd *cmnd)
656 {
657         struct Scsi_Host *shost = cmnd->device->host;
658         struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
659         unsigned long flags;
660
661         spin_lock_irqsave(shost->host_lock, flags);
662         while (rport->port_state == FC_PORTSTATE_BLOCKED) {
663                 spin_unlock_irqrestore(shost->host_lock, flags);
664                 msleep(1000);
665                 spin_lock_irqsave(shost->host_lock, flags);
666         }
667         spin_unlock_irqrestore(shost->host_lock, flags);
668         return;
669 }
670
671 /**************************************************************************
672 * qla2xxx_eh_abort
673 *
674 * Description:
675 *    The abort function will abort the specified command.
676 *
677 * Input:
678 *    cmd = Linux SCSI command packet to be aborted.
679 *
680 * Returns:
681 *    Either SUCCESS or FAILED.
682 *
683 * Note:
684 *    Only return FAILED if command not returned by firmware.
685 **************************************************************************/
686 static int
687 qla2xxx_eh_abort(struct scsi_cmnd *cmd)
688 {
689         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
690         srb_t *sp;
691         int ret, i;
692         unsigned int id, lun;
693         unsigned long serial;
694         unsigned long flags;
695         int wait = 0;
696         struct qla_hw_data *ha = vha->hw;
697         struct req_que *req;
698         srb_t *spt;
699
700         qla2x00_block_error_handler(cmd);
701
702         if (!CMD_SP(cmd))
703                 return SUCCESS;
704
705         ret = SUCCESS;
706
707         id = cmd->device->id;
708         lun = cmd->device->lun;
709         serial = cmd->serial_number;
710         spt = (srb_t *) CMD_SP(cmd);
711         if (!spt)
712                 return SUCCESS;
713         req = spt->que;
714
715         /* Check active list for command command. */
716         spin_lock_irqsave(&ha->hardware_lock, flags);
717         for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
718                 sp = req->outstanding_cmds[i];
719
720                 if (sp == NULL)
721                         continue;
722
723                 if (sp->cmd != cmd)
724                         continue;
725
726                 DEBUG2(printk("%s(%ld): aborting sp %p from RISC."
727                 " pid=%ld.\n", __func__, vha->host_no, sp, serial));
728
729                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
730                 if (ha->isp_ops->abort_command(sp)) {
731                         DEBUG2(printk("%s(%ld): abort_command "
732                         "mbx failed.\n", __func__, vha->host_no));
733                         ret = FAILED;
734                 } else {
735                         DEBUG3(printk("%s(%ld): abort_command "
736                         "mbx success.\n", __func__, vha->host_no));
737                         wait = 1;
738                 }
739                 spin_lock_irqsave(&ha->hardware_lock, flags);
740                 break;
741         }
742         spin_unlock_irqrestore(&ha->hardware_lock, flags);
743
744         /* Wait for the command to be returned. */
745         if (wait) {
746                 if (qla2x00_eh_wait_on_command(cmd) != QLA_SUCCESS) {
747                         qla_printk(KERN_ERR, ha,
748                             "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
749                             "%x.\n", vha->host_no, id, lun, serial, ret);
750                         ret = FAILED;
751                 }
752         }
753
754         qla_printk(KERN_INFO, ha,
755             "scsi(%ld:%d:%d): Abort command issued -- %d %lx %x.\n",
756             vha->host_no, id, lun, wait, serial, ret);
757
758         return ret;
759 }
760
761 enum nexus_wait_type {
762         WAIT_HOST = 0,
763         WAIT_TARGET,
764         WAIT_LUN,
765 };
766
767 static int
768 qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *vha, unsigned int t,
769         unsigned int l, srb_t *sp, enum nexus_wait_type type)
770 {
771         int cnt, match, status;
772         unsigned long flags;
773         struct qla_hw_data *ha = vha->hw;
774         struct req_que *req;
775
776         status = QLA_SUCCESS;
777         if (!sp)
778                 return status;
779
780         spin_lock_irqsave(&ha->hardware_lock, flags);
781         req = sp->que;
782         for (cnt = 1; status == QLA_SUCCESS &&
783                 cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
784                 sp = req->outstanding_cmds[cnt];
785                 if (!sp)
786                         continue;
787
788                 if (vha->vp_idx != sp->fcport->vha->vp_idx)
789                         continue;
790                 match = 0;
791                 switch (type) {
792                 case WAIT_HOST:
793                         match = 1;
794                         break;
795                 case WAIT_TARGET:
796                         match = sp->cmd->device->id == t;
797                         break;
798                 case WAIT_LUN:
799                         match = (sp->cmd->device->id == t &&
800                                 sp->cmd->device->lun == l);
801                         break;
802                 }
803                 if (!match)
804                         continue;
805
806                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
807                 status = qla2x00_eh_wait_on_command(sp->cmd);
808                 spin_lock_irqsave(&ha->hardware_lock, flags);
809         }
810         spin_unlock_irqrestore(&ha->hardware_lock, flags);
811
812         return status;
813 }
814
815 static char *reset_errors[] = {
816         "HBA not online",
817         "HBA not ready",
818         "Task management failed",
819         "Waiting for command completions",
820 };
821
822 static int
823 __qla2xxx_eh_generic_reset(char *name, enum nexus_wait_type type,
824     struct scsi_cmnd *cmd, int (*do_reset)(struct fc_port *, unsigned int, int))
825 {
826         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
827         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
828         int err;
829
830         qla2x00_block_error_handler(cmd);
831
832         if (!fcport)
833                 return FAILED;
834
835         qla_printk(KERN_INFO, vha->hw, "scsi(%ld:%d:%d): %s RESET ISSUED.\n",
836             vha->host_no, cmd->device->id, cmd->device->lun, name);
837
838         err = 0;
839         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
840                 goto eh_reset_failed;
841         err = 1;
842         if (qla2x00_wait_for_loop_ready(vha) != QLA_SUCCESS)
843                 goto eh_reset_failed;
844         err = 2;
845         if (do_reset(fcport, cmd->device->lun, cmd->request->cpu + 1)
846                 != QLA_SUCCESS)
847                 goto eh_reset_failed;
848         err = 3;
849         if (qla2x00_eh_wait_for_pending_commands(vha, cmd->device->id,
850             cmd->device->lun, (srb_t *) CMD_SP(cmd), type) != QLA_SUCCESS)
851                 goto eh_reset_failed;
852
853         qla_printk(KERN_INFO, vha->hw, "scsi(%ld:%d:%d): %s RESET SUCCEEDED.\n",
854             vha->host_no, cmd->device->id, cmd->device->lun, name);
855
856         return SUCCESS;
857
858  eh_reset_failed:
859         qla_printk(KERN_INFO, vha->hw, "scsi(%ld:%d:%d): %s RESET FAILED: %s.\n"
860             , vha->host_no, cmd->device->id, cmd->device->lun, name,
861             reset_errors[err]);
862         return FAILED;
863 }
864
865 static int
866 qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
867 {
868         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
869         struct qla_hw_data *ha = vha->hw;
870
871         return __qla2xxx_eh_generic_reset("DEVICE", WAIT_LUN, cmd,
872             ha->isp_ops->lun_reset);
873 }
874
875 static int
876 qla2xxx_eh_target_reset(struct scsi_cmnd *cmd)
877 {
878         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
879         struct qla_hw_data *ha = vha->hw;
880
881         return __qla2xxx_eh_generic_reset("TARGET", WAIT_TARGET, cmd,
882             ha->isp_ops->target_reset);
883 }
884
885 /**************************************************************************
886 * qla2xxx_eh_bus_reset
887 *
888 * Description:
889 *    The bus reset function will reset the bus and abort any executing
890 *    commands.
891 *
892 * Input:
893 *    cmd = Linux SCSI command packet of the command that cause the
894 *          bus reset.
895 *
896 * Returns:
897 *    SUCCESS/FAILURE (defined as macro in scsi.h).
898 *
899 **************************************************************************/
900 static int
901 qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
902 {
903         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
904         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
905         int ret = FAILED;
906         unsigned int id, lun;
907         unsigned long serial;
908         srb_t *sp = (srb_t *) CMD_SP(cmd);
909
910         qla2x00_block_error_handler(cmd);
911
912         id = cmd->device->id;
913         lun = cmd->device->lun;
914         serial = cmd->serial_number;
915
916         if (!fcport)
917                 return ret;
918
919         qla_printk(KERN_INFO, vha->hw,
920             "scsi(%ld:%d:%d): BUS RESET ISSUED.\n", vha->host_no, id, lun);
921
922         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS) {
923                 DEBUG2(printk("%s failed:board disabled\n",__func__));
924                 goto eh_bus_reset_done;
925         }
926
927         if (qla2x00_wait_for_loop_ready(vha) == QLA_SUCCESS) {
928                 if (qla2x00_loop_reset(vha) == QLA_SUCCESS)
929                         ret = SUCCESS;
930         }
931         if (ret == FAILED)
932                 goto eh_bus_reset_done;
933
934         /* Flush outstanding commands. */
935         if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, sp, WAIT_HOST) !=
936             QLA_SUCCESS)
937                 ret = FAILED;
938
939 eh_bus_reset_done:
940         qla_printk(KERN_INFO, vha->hw, "%s: reset %s\n", __func__,
941             (ret == FAILED) ? "failed" : "succeded");
942
943         return ret;
944 }
945
946 /**************************************************************************
947 * qla2xxx_eh_host_reset
948 *
949 * Description:
950 *    The reset function will reset the Adapter.
951 *
952 * Input:
953 *      cmd = Linux SCSI command packet of the command that cause the
954 *            adapter reset.
955 *
956 * Returns:
957 *      Either SUCCESS or FAILED.
958 *
959 * Note:
960 **************************************************************************/
961 static int
962 qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
963 {
964         scsi_qla_host_t *vha = shost_priv(cmd->device->host);
965         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
966         struct qla_hw_data *ha = vha->hw;
967         int ret = FAILED;
968         unsigned int id, lun;
969         unsigned long serial;
970         srb_t *sp = (srb_t *) CMD_SP(cmd);
971         scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
972
973         qla2x00_block_error_handler(cmd);
974
975         id = cmd->device->id;
976         lun = cmd->device->lun;
977         serial = cmd->serial_number;
978
979         if (!fcport)
980                 return ret;
981
982         qla_printk(KERN_INFO, ha,
983             "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", vha->host_no, id, lun);
984
985         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
986                 goto eh_host_reset_lock;
987
988         /*
989          * Fixme-may be dpc thread is active and processing
990          * loop_resync,so wait a while for it to
991          * be completed and then issue big hammer.Otherwise
992          * it may cause I/O failure as big hammer marks the
993          * devices as lost kicking of the port_down_timer
994          * while dpc is stuck for the mailbox to complete.
995          */
996         qla2x00_wait_for_loop_ready(vha);
997         if (vha != base_vha) {
998                 if (qla2x00_vp_abort_isp(vha))
999                         goto eh_host_reset_lock;
1000         } else {
1001                 set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1002                 if (qla2x00_abort_isp(base_vha)) {
1003                         clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1004                         /* failed. schedule dpc to try */
1005                         set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
1006
1007                         if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
1008                                 goto eh_host_reset_lock;
1009                 }
1010                 clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
1011         }
1012
1013         /* Waiting for command to be returned to OS.*/
1014         if (qla2x00_eh_wait_for_pending_commands(vha, 0, 0, sp, WAIT_HOST) ==
1015                 QLA_SUCCESS)
1016                 ret = SUCCESS;
1017
1018 eh_host_reset_lock:
1019         qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
1020             (ret == FAILED) ? "failed" : "succeded");
1021
1022         return ret;
1023 }
1024
1025 /*
1026 * qla2x00_loop_reset
1027 *      Issue loop reset.
1028 *
1029 * Input:
1030 *      ha = adapter block pointer.
1031 *
1032 * Returns:
1033 *      0 = success
1034 */
1035 int
1036 qla2x00_loop_reset(scsi_qla_host_t *vha)
1037 {
1038         int ret;
1039         struct fc_port *fcport;
1040         struct qla_hw_data *ha = vha->hw;
1041
1042         if (ha->flags.enable_lip_full_login && !vha->vp_idx &&
1043             !IS_QLA81XX(ha)) {
1044                 ret = qla2x00_full_login_lip(vha);
1045                 if (ret != QLA_SUCCESS) {
1046                         DEBUG2_3(printk("%s(%ld): failed: "
1047                             "full_login_lip=%d.\n", __func__, vha->host_no,
1048                             ret));
1049                 }
1050                 atomic_set(&vha->loop_state, LOOP_DOWN);
1051                 atomic_set(&vha->loop_down_timer, LOOP_DOWN_TIME);
1052                 qla2x00_mark_all_devices_lost(vha, 0);
1053                 qla2x00_wait_for_loop_ready(vha);
1054         }
1055
1056         if (ha->flags.enable_lip_reset && !vha->vp_idx) {
1057                 ret = qla2x00_lip_reset(vha);
1058                 if (ret != QLA_SUCCESS) {
1059                         DEBUG2_3(printk("%s(%ld): failed: "
1060                             "lip_reset=%d.\n", __func__, vha->host_no, ret));
1061                 } else
1062                         qla2x00_wait_for_loop_ready(vha);
1063         }
1064
1065         if (ha->flags.enable_target_reset) {
1066                 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1067                         if (fcport->port_type != FCT_TARGET)
1068                                 continue;
1069
1070                         ret = ha->isp_ops->target_reset(fcport, 0, 0);
1071                         if (ret != QLA_SUCCESS) {
1072                                 DEBUG2_3(printk("%s(%ld): bus_reset failed: "
1073                                     "target_reset=%d d_id=%x.\n", __func__,
1074                                     vha->host_no, ret, fcport->d_id.b24));
1075                         }
1076                 }
1077         }
1078         /* Issue marker command only when we are going to start the I/O */
1079         vha->marker_needed = 1;
1080
1081         return QLA_SUCCESS;
1082 }
1083
1084 void
1085 qla2x00_abort_all_cmds(scsi_qla_host_t *vha, int res)
1086 {
1087         int que, cnt;
1088         unsigned long flags;
1089         srb_t *sp;
1090         struct qla_hw_data *ha = vha->hw;
1091         struct req_que *req;
1092
1093         spin_lock_irqsave(&ha->hardware_lock, flags);
1094         for (que = 0; que < ha->max_req_queues; que++) {
1095                 req = ha->req_q_map[que];
1096                 if (!req)
1097                         continue;
1098                 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
1099                         sp = req->outstanding_cmds[cnt];
1100                         if (sp) {
1101                                 req->outstanding_cmds[cnt] = NULL;
1102                                 sp->cmd->result = res;
1103                                 qla2x00_sp_compl(ha, sp);
1104                         }
1105                 }
1106         }
1107         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1108 }
1109
1110 static int
1111 qla2xxx_slave_alloc(struct scsi_device *sdev)
1112 {
1113         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1114
1115         if (!rport || fc_remote_port_chkready(rport))
1116                 return -ENXIO;
1117
1118         sdev->hostdata = *(fc_port_t **)rport->dd_data;
1119
1120         return 0;
1121 }
1122
1123 static int
1124 qla2xxx_slave_configure(struct scsi_device *sdev)
1125 {
1126         scsi_qla_host_t *vha = shost_priv(sdev->host);
1127         struct qla_hw_data *ha = vha->hw;
1128         struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
1129         struct req_que *req = vha->req;
1130
1131         if (sdev->tagged_supported)
1132                 scsi_activate_tcq(sdev, req->max_q_depth);
1133         else
1134                 scsi_deactivate_tcq(sdev, req->max_q_depth);
1135
1136         rport->dev_loss_tmo = ha->port_down_retry_count;
1137
1138         return 0;
1139 }
1140
1141 static void
1142 qla2xxx_slave_destroy(struct scsi_device *sdev)
1143 {
1144         sdev->hostdata = NULL;
1145 }
1146
1147 static int
1148 qla2x00_change_queue_depth(struct scsi_device *sdev, int qdepth)
1149 {
1150         scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1151         return sdev->queue_depth;
1152 }
1153
1154 static int
1155 qla2x00_change_queue_type(struct scsi_device *sdev, int tag_type)
1156 {
1157         if (sdev->tagged_supported) {
1158                 scsi_set_tag_type(sdev, tag_type);
1159                 if (tag_type)
1160                         scsi_activate_tcq(sdev, sdev->queue_depth);
1161                 else
1162                         scsi_deactivate_tcq(sdev, sdev->queue_depth);
1163         } else
1164                 tag_type = 0;
1165
1166         return tag_type;
1167 }
1168
1169 /**
1170  * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
1171  * @ha: HA context
1172  *
1173  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1174  * supported addressing method.
1175  */
1176 static void
1177 qla2x00_config_dma_addressing(struct qla_hw_data *ha)
1178 {
1179         /* Assume a 32bit DMA mask. */
1180         ha->flags.enable_64bit_addressing = 0;
1181
1182         if (!dma_set_mask(&ha->pdev->dev, DMA_BIT_MASK(64))) {
1183                 /* Any upper-dword bits set? */
1184                 if (MSD(dma_get_required_mask(&ha->pdev->dev)) &&
1185                     !pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(64))) {
1186                         /* Ok, a 64bit DMA mask is applicable. */
1187                         ha->flags.enable_64bit_addressing = 1;
1188                         ha->isp_ops->calc_req_entries = qla2x00_calc_iocbs_64;
1189                         ha->isp_ops->build_iocbs = qla2x00_build_scsi_iocbs_64;
1190                         return;
1191                 }
1192         }
1193
1194         dma_set_mask(&ha->pdev->dev, DMA_BIT_MASK(32));
1195         pci_set_consistent_dma_mask(ha->pdev, DMA_BIT_MASK(32));
1196 }
1197
1198 static void
1199 qla2x00_enable_intrs(struct qla_hw_data *ha)
1200 {
1201         unsigned long flags = 0;
1202         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1203
1204         spin_lock_irqsave(&ha->hardware_lock, flags);
1205         ha->interrupts_on = 1;
1206         /* enable risc and host interrupts */
1207         WRT_REG_WORD(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
1208         RD_REG_WORD(&reg->ictrl);
1209         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1210
1211 }
1212
1213 static void
1214 qla2x00_disable_intrs(struct qla_hw_data *ha)
1215 {
1216         unsigned long flags = 0;
1217         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1218
1219         spin_lock_irqsave(&ha->hardware_lock, flags);
1220         ha->interrupts_on = 0;
1221         /* disable risc and host interrupts */
1222         WRT_REG_WORD(&reg->ictrl, 0);
1223         RD_REG_WORD(&reg->ictrl);
1224         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1225 }
1226
1227 static void
1228 qla24xx_enable_intrs(struct qla_hw_data *ha)
1229 {
1230         unsigned long flags = 0;
1231         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1232
1233         spin_lock_irqsave(&ha->hardware_lock, flags);
1234         ha->interrupts_on = 1;
1235         WRT_REG_DWORD(&reg->ictrl, ICRX_EN_RISC_INT);
1236         RD_REG_DWORD(&reg->ictrl);
1237         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1238 }
1239
1240 static void
1241 qla24xx_disable_intrs(struct qla_hw_data *ha)
1242 {
1243         unsigned long flags = 0;
1244         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1245
1246         if (IS_NOPOLLING_TYPE(ha))
1247                 return;
1248         spin_lock_irqsave(&ha->hardware_lock, flags);
1249         ha->interrupts_on = 0;
1250         WRT_REG_DWORD(&reg->ictrl, 0);
1251         RD_REG_DWORD(&reg->ictrl);
1252         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1253 }
1254
1255 static struct isp_operations qla2100_isp_ops = {
1256         .pci_config             = qla2100_pci_config,
1257         .reset_chip             = qla2x00_reset_chip,
1258         .chip_diag              = qla2x00_chip_diag,
1259         .config_rings           = qla2x00_config_rings,
1260         .reset_adapter          = qla2x00_reset_adapter,
1261         .nvram_config           = qla2x00_nvram_config,
1262         .update_fw_options      = qla2x00_update_fw_options,
1263         .load_risc              = qla2x00_load_risc,
1264         .pci_info_str           = qla2x00_pci_info_str,
1265         .fw_version_str         = qla2x00_fw_version_str,
1266         .intr_handler           = qla2100_intr_handler,
1267         .enable_intrs           = qla2x00_enable_intrs,
1268         .disable_intrs          = qla2x00_disable_intrs,
1269         .abort_command          = qla2x00_abort_command,
1270         .target_reset           = qla2x00_abort_target,
1271         .lun_reset              = qla2x00_lun_reset,
1272         .fabric_login           = qla2x00_login_fabric,
1273         .fabric_logout          = qla2x00_fabric_logout,
1274         .calc_req_entries       = qla2x00_calc_iocbs_32,
1275         .build_iocbs            = qla2x00_build_scsi_iocbs_32,
1276         .prep_ms_iocb           = qla2x00_prep_ms_iocb,
1277         .prep_ms_fdmi_iocb      = qla2x00_prep_ms_fdmi_iocb,
1278         .read_nvram             = qla2x00_read_nvram_data,
1279         .write_nvram            = qla2x00_write_nvram_data,
1280         .fw_dump                = qla2100_fw_dump,
1281         .beacon_on              = NULL,
1282         .beacon_off             = NULL,
1283         .beacon_blink           = NULL,
1284         .read_optrom            = qla2x00_read_optrom_data,
1285         .write_optrom           = qla2x00_write_optrom_data,
1286         .get_flash_version      = qla2x00_get_flash_version,
1287         .start_scsi             = qla2x00_start_scsi,
1288 };
1289
1290 static struct isp_operations qla2300_isp_ops = {
1291         .pci_config             = qla2300_pci_config,
1292         .reset_chip             = qla2x00_reset_chip,
1293         .chip_diag              = qla2x00_chip_diag,
1294         .config_rings           = qla2x00_config_rings,
1295         .reset_adapter          = qla2x00_reset_adapter,
1296         .nvram_config           = qla2x00_nvram_config,
1297         .update_fw_options      = qla2x00_update_fw_options,
1298         .load_risc              = qla2x00_load_risc,
1299         .pci_info_str           = qla2x00_pci_info_str,
1300         .fw_version_str         = qla2x00_fw_version_str,
1301         .intr_handler           = qla2300_intr_handler,
1302         .enable_intrs           = qla2x00_enable_intrs,
1303         .disable_intrs          = qla2x00_disable_intrs,
1304         .abort_command          = qla2x00_abort_command,
1305         .target_reset           = qla2x00_abort_target,
1306         .lun_reset              = qla2x00_lun_reset,
1307         .fabric_login           = qla2x00_login_fabric,
1308         .fabric_logout          = qla2x00_fabric_logout,
1309         .calc_req_entries       = qla2x00_calc_iocbs_32,
1310         .build_iocbs            = qla2x00_build_scsi_iocbs_32,
1311         .prep_ms_iocb           = qla2x00_prep_ms_iocb,
1312         .prep_ms_fdmi_iocb      = qla2x00_prep_ms_fdmi_iocb,
1313         .read_nvram             = qla2x00_read_nvram_data,
1314         .write_nvram            = qla2x00_write_nvram_data,
1315         .fw_dump                = qla2300_fw_dump,
1316         .beacon_on              = qla2x00_beacon_on,
1317         .beacon_off             = qla2x00_beacon_off,
1318         .beacon_blink           = qla2x00_beacon_blink,
1319         .read_optrom            = qla2x00_read_optrom_data,
1320         .write_optrom           = qla2x00_write_optrom_data,
1321         .get_flash_version      = qla2x00_get_flash_version,
1322         .start_scsi             = qla2x00_start_scsi,
1323 };
1324
1325 static struct isp_operations qla24xx_isp_ops = {
1326         .pci_config             = qla24xx_pci_config,
1327         .reset_chip             = qla24xx_reset_chip,
1328         .chip_diag              = qla24xx_chip_diag,
1329         .config_rings           = qla24xx_config_rings,
1330         .reset_adapter          = qla24xx_reset_adapter,
1331         .nvram_config           = qla24xx_nvram_config,
1332         .update_fw_options      = qla24xx_update_fw_options,
1333         .load_risc              = qla24xx_load_risc,
1334         .pci_info_str           = qla24xx_pci_info_str,
1335         .fw_version_str         = qla24xx_fw_version_str,
1336         .intr_handler           = qla24xx_intr_handler,
1337         .enable_intrs           = qla24xx_enable_intrs,
1338         .disable_intrs          = qla24xx_disable_intrs,
1339         .abort_command          = qla24xx_abort_command,
1340         .target_reset           = qla24xx_abort_target,
1341         .lun_reset              = qla24xx_lun_reset,
1342         .fabric_login           = qla24xx_login_fabric,
1343         .fabric_logout          = qla24xx_fabric_logout,
1344         .calc_req_entries       = NULL,
1345         .build_iocbs            = NULL,
1346         .prep_ms_iocb           = qla24xx_prep_ms_iocb,
1347         .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
1348         .read_nvram             = qla24xx_read_nvram_data,
1349         .write_nvram            = qla24xx_write_nvram_data,
1350         .fw_dump                = qla24xx_fw_dump,
1351         .beacon_on              = qla24xx_beacon_on,
1352         .beacon_off             = qla24xx_beacon_off,
1353         .beacon_blink           = qla24xx_beacon_blink,
1354         .read_optrom            = qla24xx_read_optrom_data,
1355         .write_optrom           = qla24xx_write_optrom_data,
1356         .get_flash_version      = qla24xx_get_flash_version,
1357         .start_scsi             = qla24xx_start_scsi,
1358 };
1359
1360 static struct isp_operations qla25xx_isp_ops = {
1361         .pci_config             = qla25xx_pci_config,
1362         .reset_chip             = qla24xx_reset_chip,
1363         .chip_diag              = qla24xx_chip_diag,
1364         .config_rings           = qla24xx_config_rings,
1365         .reset_adapter          = qla24xx_reset_adapter,
1366         .nvram_config           = qla24xx_nvram_config,
1367         .update_fw_options      = qla24xx_update_fw_options,
1368         .load_risc              = qla24xx_load_risc,
1369         .pci_info_str           = qla24xx_pci_info_str,
1370         .fw_version_str         = qla24xx_fw_version_str,
1371         .intr_handler           = qla24xx_intr_handler,
1372         .enable_intrs           = qla24xx_enable_intrs,
1373         .disable_intrs          = qla24xx_disable_intrs,
1374         .abort_command          = qla24xx_abort_command,
1375         .target_reset           = qla24xx_abort_target,
1376         .lun_reset              = qla24xx_lun_reset,
1377         .fabric_login           = qla24xx_login_fabric,
1378         .fabric_logout          = qla24xx_fabric_logout,
1379         .calc_req_entries       = NULL,
1380         .build_iocbs            = NULL,
1381         .prep_ms_iocb           = qla24xx_prep_ms_iocb,
1382         .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
1383         .read_nvram             = qla25xx_read_nvram_data,
1384         .write_nvram            = qla25xx_write_nvram_data,
1385         .fw_dump                = qla25xx_fw_dump,
1386         .beacon_on              = qla24xx_beacon_on,
1387         .beacon_off             = qla24xx_beacon_off,
1388         .beacon_blink           = qla24xx_beacon_blink,
1389         .read_optrom            = qla25xx_read_optrom_data,
1390         .write_optrom           = qla24xx_write_optrom_data,
1391         .get_flash_version      = qla24xx_get_flash_version,
1392         .start_scsi             = qla24xx_start_scsi,
1393 };
1394
1395 static struct isp_operations qla81xx_isp_ops = {
1396         .pci_config             = qla25xx_pci_config,
1397         .reset_chip             = qla24xx_reset_chip,
1398         .chip_diag              = qla24xx_chip_diag,
1399         .config_rings           = qla24xx_config_rings,
1400         .reset_adapter          = qla24xx_reset_adapter,
1401         .nvram_config           = qla81xx_nvram_config,
1402         .update_fw_options      = qla81xx_update_fw_options,
1403         .load_risc              = qla81xx_load_risc,
1404         .pci_info_str           = qla24xx_pci_info_str,
1405         .fw_version_str         = qla24xx_fw_version_str,
1406         .intr_handler           = qla24xx_intr_handler,
1407         .enable_intrs           = qla24xx_enable_intrs,
1408         .disable_intrs          = qla24xx_disable_intrs,
1409         .abort_command          = qla24xx_abort_command,
1410         .target_reset           = qla24xx_abort_target,
1411         .lun_reset              = qla24xx_lun_reset,
1412         .fabric_login           = qla24xx_login_fabric,
1413         .fabric_logout          = qla24xx_fabric_logout,
1414         .calc_req_entries       = NULL,
1415         .build_iocbs            = NULL,
1416         .prep_ms_iocb           = qla24xx_prep_ms_iocb,
1417         .prep_ms_fdmi_iocb      = qla24xx_prep_ms_fdmi_iocb,
1418         .read_nvram             = NULL,
1419         .write_nvram            = NULL,
1420         .fw_dump                = qla81xx_fw_dump,
1421         .beacon_on              = qla24xx_beacon_on,
1422         .beacon_off             = qla24xx_beacon_off,
1423         .beacon_blink           = qla24xx_beacon_blink,
1424         .read_optrom            = qla25xx_read_optrom_data,
1425         .write_optrom           = qla24xx_write_optrom_data,
1426         .get_flash_version      = qla24xx_get_flash_version,
1427         .start_scsi             = qla24xx_start_scsi,
1428 };
1429
1430 static inline void
1431 qla2x00_set_isp_flags(struct qla_hw_data *ha)
1432 {
1433         ha->device_type = DT_EXTENDED_IDS;
1434         switch (ha->pdev->device) {
1435         case PCI_DEVICE_ID_QLOGIC_ISP2100:
1436                 ha->device_type |= DT_ISP2100;
1437                 ha->device_type &= ~DT_EXTENDED_IDS;
1438                 ha->fw_srisc_address = RISC_START_ADDRESS_2100;
1439                 break;
1440         case PCI_DEVICE_ID_QLOGIC_ISP2200:
1441                 ha->device_type |= DT_ISP2200;
1442                 ha->device_type &= ~DT_EXTENDED_IDS;
1443                 ha->fw_srisc_address = RISC_START_ADDRESS_2100;
1444                 break;
1445         case PCI_DEVICE_ID_QLOGIC_ISP2300:
1446                 ha->device_type |= DT_ISP2300;
1447                 ha->device_type |= DT_ZIO_SUPPORTED;
1448                 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
1449                 break;
1450         case PCI_DEVICE_ID_QLOGIC_ISP2312:
1451                 ha->device_type |= DT_ISP2312;
1452                 ha->device_type |= DT_ZIO_SUPPORTED;
1453                 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
1454                 break;
1455         case PCI_DEVICE_ID_QLOGIC_ISP2322:
1456                 ha->device_type |= DT_ISP2322;
1457                 ha->device_type |= DT_ZIO_SUPPORTED;
1458                 if (ha->pdev->subsystem_vendor == 0x1028 &&
1459                     ha->pdev->subsystem_device == 0x0170)
1460                         ha->device_type |= DT_OEM_001;
1461                 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
1462                 break;
1463         case PCI_DEVICE_ID_QLOGIC_ISP6312:
1464                 ha->device_type |= DT_ISP6312;
1465                 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
1466                 break;
1467         case PCI_DEVICE_ID_QLOGIC_ISP6322:
1468                 ha->device_type |= DT_ISP6322;
1469                 ha->fw_srisc_address = RISC_START_ADDRESS_2300;
1470                 break;
1471         case PCI_DEVICE_ID_QLOGIC_ISP2422:
1472                 ha->device_type |= DT_ISP2422;
1473                 ha->device_type |= DT_ZIO_SUPPORTED;
1474                 ha->device_type |= DT_FWI2;
1475                 ha->device_type |= DT_IIDMA;
1476                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1477                 break;
1478         case PCI_DEVICE_ID_QLOGIC_ISP2432:
1479                 ha->device_type |= DT_ISP2432;
1480                 ha->device_type |= DT_ZIO_SUPPORTED;
1481                 ha->device_type |= DT_FWI2;
1482                 ha->device_type |= DT_IIDMA;
1483                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1484                 break;
1485         case PCI_DEVICE_ID_QLOGIC_ISP8432:
1486                 ha->device_type |= DT_ISP8432;
1487                 ha->device_type |= DT_ZIO_SUPPORTED;
1488                 ha->device_type |= DT_FWI2;
1489                 ha->device_type |= DT_IIDMA;
1490                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1491                 break;
1492         case PCI_DEVICE_ID_QLOGIC_ISP5422:
1493                 ha->device_type |= DT_ISP5422;
1494                 ha->device_type |= DT_FWI2;
1495                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1496                 break;
1497         case PCI_DEVICE_ID_QLOGIC_ISP5432:
1498                 ha->device_type |= DT_ISP5432;
1499                 ha->device_type |= DT_FWI2;
1500                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1501                 break;
1502         case PCI_DEVICE_ID_QLOGIC_ISP2532:
1503                 ha->device_type |= DT_ISP2532;
1504                 ha->device_type |= DT_ZIO_SUPPORTED;
1505                 ha->device_type |= DT_FWI2;
1506                 ha->device_type |= DT_IIDMA;
1507                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1508                 break;
1509         case PCI_DEVICE_ID_QLOGIC_ISP8001:
1510                 ha->device_type |= DT_ISP8001;
1511                 ha->device_type |= DT_ZIO_SUPPORTED;
1512                 ha->device_type |= DT_FWI2;
1513                 ha->device_type |= DT_IIDMA;
1514                 ha->fw_srisc_address = RISC_START_ADDRESS_2400;
1515                 break;
1516         }
1517 }
1518
1519 static int
1520 qla2x00_iospace_config(struct qla_hw_data *ha)
1521 {
1522         resource_size_t pio;
1523         uint16_t msix;
1524
1525         if (pci_request_selected_regions(ha->pdev, ha->bars,
1526             QLA2XXX_DRIVER_NAME)) {
1527                 qla_printk(KERN_WARNING, ha,
1528                     "Failed to reserve PIO/MMIO regions (%s)\n",
1529                     pci_name(ha->pdev));
1530
1531                 goto iospace_error_exit;
1532         }
1533         if (!(ha->bars & 1))
1534                 goto skip_pio;
1535
1536         /* We only need PIO for Flash operations on ISP2312 v2 chips. */
1537         pio = pci_resource_start(ha->pdev, 0);
1538         if (pci_resource_flags(ha->pdev, 0) & IORESOURCE_IO) {
1539                 if (pci_resource_len(ha->pdev, 0) < MIN_IOBASE_LEN) {
1540                         qla_printk(KERN_WARNING, ha,
1541                             "Invalid PCI I/O region size (%s)...\n",
1542                                 pci_name(ha->pdev));
1543                         pio = 0;
1544                 }
1545         } else {
1546                 qla_printk(KERN_WARNING, ha,
1547                     "region #0 not a PIO resource (%s)...\n",
1548                     pci_name(ha->pdev));
1549                 pio = 0;
1550         }
1551         ha->pio_address = pio;
1552
1553 skip_pio:
1554         /* Use MMIO operations for all accesses. */
1555         if (!(pci_resource_flags(ha->pdev, 1) & IORESOURCE_MEM)) {
1556                 qla_printk(KERN_ERR, ha,
1557                     "region #1 not an MMIO resource (%s), aborting\n",
1558                     pci_name(ha->pdev));
1559                 goto iospace_error_exit;
1560         }
1561         if (pci_resource_len(ha->pdev, 1) < MIN_IOBASE_LEN) {
1562                 qla_printk(KERN_ERR, ha,
1563                     "Invalid PCI mem region size (%s), aborting\n",
1564                         pci_name(ha->pdev));
1565                 goto iospace_error_exit;
1566         }
1567
1568         ha->iobase = ioremap(pci_resource_start(ha->pdev, 1), MIN_IOBASE_LEN);
1569         if (!ha->iobase) {
1570                 qla_printk(KERN_ERR, ha,
1571                     "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
1572
1573                 goto iospace_error_exit;
1574         }
1575
1576         /* Determine queue resources */
1577         ha->max_req_queues = ha->max_rsp_queues = 1;
1578         if (ql2xmaxqueues <= 1  &&
1579                 (!IS_QLA25XX(ha) && !IS_QLA81XX(ha)))
1580                 goto mqiobase_exit;
1581         ha->mqiobase = ioremap(pci_resource_start(ha->pdev, 3),
1582                         pci_resource_len(ha->pdev, 3));
1583         if (ha->mqiobase) {
1584                 /* Read MSIX vector size of the board */
1585                 pci_read_config_word(ha->pdev, QLA_PCI_MSIX_CONTROL, &msix);
1586                 ha->msix_count = msix;
1587                 if (ql2xmaxqueues > 1) {
1588                         ha->max_req_queues = ql2xmaxqueues > QLA_MQ_SIZE ?
1589                                                 QLA_MQ_SIZE : ql2xmaxqueues;
1590                         DEBUG2(qla_printk(KERN_INFO, ha, "QoS mode set, max no"
1591                         " of request queues:%d\n", ha->max_req_queues));
1592                 }
1593         } else
1594                 qla_printk(KERN_INFO, ha, "BAR 3 not enabled\n");
1595
1596 mqiobase_exit:
1597         ha->msix_count = ha->max_rsp_queues + 1;
1598         return (0);
1599
1600 iospace_error_exit:
1601         return (-ENOMEM);
1602 }
1603
1604 static void
1605 qla2xxx_scan_start(struct Scsi_Host *shost)
1606 {
1607         scsi_qla_host_t *vha = shost_priv(shost);
1608
1609         set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
1610         set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
1611         set_bit(RSCN_UPDATE, &vha->dpc_flags);
1612         set_bit(NPIV_CONFIG_NEEDED, &vha->dpc_flags);
1613 }
1614
1615 static int
1616 qla2xxx_scan_finished(struct Scsi_Host *shost, unsigned long time)
1617 {
1618         scsi_qla_host_t *vha = shost_priv(shost);
1619
1620         if (!vha->host)
1621                 return 1;
1622         if (time > vha->hw->loop_reset_delay * HZ)
1623                 return 1;
1624
1625         return atomic_read(&vha->loop_state) == LOOP_READY;
1626 }
1627
1628 /*
1629  * PCI driver interface
1630  */
1631 static int __devinit
1632 qla2x00_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
1633 {
1634         int     ret = -ENODEV;
1635         struct Scsi_Host *host;
1636         scsi_qla_host_t *base_vha = NULL;
1637         struct qla_hw_data *ha;
1638         char pci_info[30];
1639         char fw_str[30];
1640         struct scsi_host_template *sht;
1641         int bars, max_id, mem_only = 0;
1642         uint16_t req_length = 0, rsp_length = 0;
1643         struct req_que *req = NULL;
1644         struct rsp_que *rsp = NULL;
1645
1646         bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
1647         sht = &qla2xxx_driver_template;
1648         if (pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2422 ||
1649             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2432 ||
1650             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8432 ||
1651             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5422 ||
1652             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP5432 ||
1653             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2532 ||
1654             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP8001) {
1655                 bars = pci_select_bars(pdev, IORESOURCE_MEM);
1656                 mem_only = 1;
1657         }
1658
1659         if (mem_only) {
1660                 if (pci_enable_device_mem(pdev))
1661                         goto probe_out;
1662         } else {
1663                 if (pci_enable_device(pdev))
1664                         goto probe_out;
1665         }
1666
1667         /* This may fail but that's ok */
1668         pci_enable_pcie_error_reporting(pdev);
1669
1670         ha = kzalloc(sizeof(struct qla_hw_data), GFP_KERNEL);
1671         if (!ha) {
1672                 DEBUG(printk("Unable to allocate memory for ha\n"));
1673                 goto probe_out;
1674         }
1675         ha->pdev = pdev;
1676
1677         /* Clear our data area */
1678         ha->bars = bars;
1679         ha->mem_only = mem_only;
1680         spin_lock_init(&ha->hardware_lock);
1681
1682         /* Set ISP-type information. */
1683         qla2x00_set_isp_flags(ha);
1684         /* Configure PCI I/O space */
1685         ret = qla2x00_iospace_config(ha);
1686         if (ret)
1687                 goto probe_hw_failed;
1688
1689         qla_printk(KERN_INFO, ha,
1690             "Found an ISP%04X, irq %d, iobase 0x%p\n", pdev->device, pdev->irq,
1691             ha->iobase);
1692
1693         ha->prev_topology = 0;
1694         ha->init_cb_size = sizeof(init_cb_t);
1695         ha->link_data_rate = PORT_SPEED_UNKNOWN;
1696         ha->optrom_size = OPTROM_SIZE_2300;
1697
1698         /* Assign ISP specific operations. */
1699         max_id = MAX_TARGETS_2200;
1700         if (IS_QLA2100(ha)) {
1701                 max_id = MAX_TARGETS_2100;
1702                 ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
1703                 req_length = REQUEST_ENTRY_CNT_2100;
1704                 rsp_length = RESPONSE_ENTRY_CNT_2100;
1705                 ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
1706                 ha->gid_list_info_size = 4;
1707                 ha->flash_conf_off = ~0;
1708                 ha->flash_data_off = ~0;
1709                 ha->nvram_conf_off = ~0;
1710                 ha->nvram_data_off = ~0;
1711                 ha->isp_ops = &qla2100_isp_ops;
1712         } else if (IS_QLA2200(ha)) {
1713                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1714                 req_length = REQUEST_ENTRY_CNT_2200;
1715                 rsp_length = RESPONSE_ENTRY_CNT_2100;
1716                 ha->max_loop_id = SNS_LAST_LOOP_ID_2100;
1717                 ha->gid_list_info_size = 4;
1718                 ha->flash_conf_off = ~0;
1719                 ha->flash_data_off = ~0;
1720                 ha->nvram_conf_off = ~0;
1721                 ha->nvram_data_off = ~0;
1722                 ha->isp_ops = &qla2100_isp_ops;
1723         } else if (IS_QLA23XX(ha)) {
1724                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1725                 req_length = REQUEST_ENTRY_CNT_2200;
1726                 rsp_length = RESPONSE_ENTRY_CNT_2300;
1727                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
1728                 ha->gid_list_info_size = 6;
1729                 if (IS_QLA2322(ha) || IS_QLA6322(ha))
1730                         ha->optrom_size = OPTROM_SIZE_2322;
1731                 ha->flash_conf_off = ~0;
1732                 ha->flash_data_off = ~0;
1733                 ha->nvram_conf_off = ~0;
1734                 ha->nvram_data_off = ~0;
1735                 ha->isp_ops = &qla2300_isp_ops;
1736         } else if (IS_QLA24XX_TYPE(ha)) {
1737                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1738                 req_length = REQUEST_ENTRY_CNT_24XX;
1739                 rsp_length = RESPONSE_ENTRY_CNT_2300;
1740                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
1741                 ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
1742                 ha->gid_list_info_size = 8;
1743                 ha->optrom_size = OPTROM_SIZE_24XX;
1744                 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA24XX;
1745                 ha->isp_ops = &qla24xx_isp_ops;
1746                 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
1747                 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
1748                 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
1749                 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
1750         } else if (IS_QLA25XX(ha)) {
1751                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1752                 req_length = REQUEST_ENTRY_CNT_24XX;
1753                 rsp_length = RESPONSE_ENTRY_CNT_2300;
1754                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
1755                 ha->init_cb_size = sizeof(struct mid_init_cb_24xx);
1756                 ha->gid_list_info_size = 8;
1757                 ha->optrom_size = OPTROM_SIZE_25XX;
1758                 ha->nvram_npiv_size = QLA_MAX_VPORTS_QLA25XX;
1759                 ha->isp_ops = &qla25xx_isp_ops;
1760                 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF;
1761                 ha->flash_data_off = FARX_ACCESS_FLASH_DATA;
1762                 ha->nvram_conf_off = FARX_ACCESS_NVRAM_CONF;
1763                 ha->nvram_data_off = FARX_ACCESS_NVRAM_DATA;
1764         } else if (IS_QLA81XX(ha)) {
1765                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1766                 req_length = REQUEST_ENTRY_CNT_24XX;
1767                 rsp_length = RESPONSE_ENTRY_CNT_2300;
1768                 ha->max_loop_id = SNS_LAST_LOOP_ID_2300;
1769                 ha->init_cb_size = sizeof(struct mid_init_cb_81xx);
1770                 ha->gid_list_info_size = 8;
1771                 ha->optrom_size = OPTROM_SIZE_81XX;
1772                 ha->isp_ops = &qla81xx_isp_ops;
1773                 ha->flash_conf_off = FARX_ACCESS_FLASH_CONF_81XX;
1774                 ha->flash_data_off = FARX_ACCESS_FLASH_DATA_81XX;
1775                 ha->nvram_conf_off = ~0;
1776                 ha->nvram_data_off = ~0;
1777         }
1778
1779         mutex_init(&ha->vport_lock);
1780         init_completion(&ha->mbx_cmd_comp);
1781         complete(&ha->mbx_cmd_comp);
1782         init_completion(&ha->mbx_intr_comp);
1783
1784         set_bit(0, (unsigned long *) ha->vp_idx_map);
1785
1786         qla2x00_config_dma_addressing(ha);
1787         ret = qla2x00_mem_alloc(ha, req_length, rsp_length, &req, &rsp);
1788         if (!ret) {
1789                 qla_printk(KERN_WARNING, ha,
1790                     "[ERROR] Failed to allocate memory for adapter\n");
1791
1792                 goto probe_hw_failed;
1793         }
1794
1795         req->max_q_depth = MAX_Q_DEPTH;
1796         if (ql2xmaxqdepth != 0 && ql2xmaxqdepth <= 0xffffU)
1797                 req->max_q_depth = ql2xmaxqdepth;
1798
1799
1800         base_vha = qla2x00_create_host(sht, ha);
1801         if (!base_vha) {
1802                 qla_printk(KERN_WARNING, ha,
1803                     "[ERROR] Failed to allocate memory for scsi_host\n");
1804
1805                 ret = -ENOMEM;
1806                 qla2x00_mem_free(ha);
1807                 qla2x00_free_req_que(ha, req);
1808                 qla2x00_free_rsp_que(ha, rsp);
1809                 goto probe_hw_failed;
1810         }
1811
1812         pci_set_drvdata(pdev, base_vha);
1813
1814         host = base_vha->host;
1815         base_vha->req = req;
1816         host->can_queue = req->length + 128;
1817         if (IS_QLA2XXX_MIDTYPE(ha))
1818                 base_vha->mgmt_svr_loop_id = 10 + base_vha->vp_idx;
1819         else
1820                 base_vha->mgmt_svr_loop_id = MANAGEMENT_SERVER +
1821                                                 base_vha->vp_idx;
1822         if (IS_QLA2100(ha))
1823                 host->sg_tablesize = 32;
1824         host->max_id = max_id;
1825         host->this_id = 255;
1826         host->cmd_per_lun = 3;
1827         host->unique_id = host->host_no;
1828         host->max_cmd_len = MAX_CMDSZ;
1829         host->max_channel = MAX_BUSES - 1;
1830         host->max_lun = MAX_LUNS;
1831         host->transportt = qla2xxx_transport_template;
1832
1833         /* Set up the irqs */
1834         ret = qla2x00_request_irqs(ha, rsp);
1835         if (ret)
1836                 goto probe_init_failed;
1837         /* Alloc arrays of request and response ring ptrs */
1838         if (!qla2x00_alloc_queues(ha)) {
1839                 qla_printk(KERN_WARNING, ha,
1840                 "[ERROR] Failed to allocate memory for queue"
1841                 " pointers\n");
1842                 goto probe_init_failed;
1843         }
1844         ha->rsp_q_map[0] = rsp;
1845         ha->req_q_map[0] = req;
1846         rsp->req = req;
1847         req->rsp = rsp;
1848         set_bit(0, ha->req_qid_map);
1849         set_bit(0, ha->rsp_qid_map);
1850         /* FWI2-capable only. */
1851         req->req_q_in = &ha->iobase->isp24.req_q_in;
1852         req->req_q_out = &ha->iobase->isp24.req_q_out;
1853         rsp->rsp_q_in = &ha->iobase->isp24.rsp_q_in;
1854         rsp->rsp_q_out = &ha->iobase->isp24.rsp_q_out;
1855         if (ha->mqenable) {
1856                 req->req_q_in = &ha->mqiobase->isp25mq.req_q_in;
1857                 req->req_q_out = &ha->mqiobase->isp25mq.req_q_out;
1858                 rsp->rsp_q_in = &ha->mqiobase->isp25mq.rsp_q_in;
1859                 rsp->rsp_q_out =  &ha->mqiobase->isp25mq.rsp_q_out;
1860         }
1861
1862         if (qla2x00_initialize_adapter(base_vha)) {
1863                 qla_printk(KERN_WARNING, ha,
1864                     "Failed to initialize adapter\n");
1865
1866                 DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
1867                     "Adapter flags %x.\n",
1868                     base_vha->host_no, base_vha->device_flags));
1869
1870                 ret = -ENODEV;
1871                 goto probe_failed;
1872         }
1873
1874         /*
1875          * Startup the kernel thread for this host adapter
1876          */
1877         ha->dpc_thread = kthread_create(qla2x00_do_dpc, ha,
1878                         "%s_dpc", base_vha->host_str);
1879         if (IS_ERR(ha->dpc_thread)) {
1880                 qla_printk(KERN_WARNING, ha,
1881                     "Unable to start DPC thread!\n");
1882                 ret = PTR_ERR(ha->dpc_thread);
1883                 goto probe_failed;
1884         }
1885
1886         list_add_tail(&base_vha->list, &ha->vp_list);
1887         base_vha->host->irq = ha->pdev->irq;
1888
1889         /* Initialized the timer */
1890         qla2x00_start_timer(base_vha, qla2x00_timer, WATCH_INTERVAL);
1891
1892         DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
1893             base_vha->host_no, ha));
1894
1895         base_vha->flags.init_done = 1;
1896         base_vha->flags.online = 1;
1897
1898         ret = scsi_add_host(host, &pdev->dev);
1899         if (ret)
1900                 goto probe_failed;
1901
1902         ha->isp_ops->enable_intrs(ha);
1903
1904         scsi_scan_host(host);
1905
1906         qla2x00_alloc_sysfs_attr(base_vha);
1907
1908         qla2x00_init_host_attr(base_vha);
1909
1910         qla2x00_dfs_setup(base_vha);
1911
1912         qla_printk(KERN_INFO, ha, "\n"
1913             " QLogic Fibre Channel HBA Driver: %s\n"
1914             "  QLogic %s - %s\n"
1915             "  ISP%04X: %s @ %s hdma%c, host#=%ld, fw=%s\n",
1916             qla2x00_version_str, ha->model_number,
1917             ha->model_desc ? ha->model_desc : "", pdev->device,
1918             ha->isp_ops->pci_info_str(base_vha, pci_info), pci_name(pdev),
1919             ha->flags.enable_64bit_addressing ? '+' : '-', base_vha->host_no,
1920             ha->isp_ops->fw_version_str(base_vha, fw_str));
1921
1922         return 0;
1923
1924 probe_init_failed:
1925         qla2x00_free_req_que(ha, req);
1926         qla2x00_free_rsp_que(ha, rsp);
1927         ha->max_req_queues = ha->max_rsp_queues = 0;
1928
1929 probe_failed:
1930         if (base_vha->timer_active)
1931                 qla2x00_stop_timer(base_vha);
1932         base_vha->flags.online = 0;
1933         if (ha->dpc_thread) {
1934                 struct task_struct *t = ha->dpc_thread;
1935
1936                 ha->dpc_thread = NULL;
1937                 kthread_stop(t);
1938         }
1939
1940         qla2x00_free_device(base_vha);
1941
1942         scsi_host_put(base_vha->host);
1943
1944 probe_hw_failed:
1945         if (ha->iobase)
1946                 iounmap(ha->iobase);
1947
1948         pci_release_selected_regions(ha->pdev, ha->bars);
1949         kfree(ha);
1950         ha = NULL;
1951
1952 probe_out:
1953         pci_disable_device(pdev);
1954         return ret;
1955 }
1956
1957 static void
1958 qla2x00_remove_one(struct pci_dev *pdev)
1959 {
1960         scsi_qla_host_t *base_vha, *vha, *temp;
1961         struct qla_hw_data  *ha;
1962
1963         base_vha = pci_get_drvdata(pdev);
1964         ha = base_vha->hw;
1965
1966         list_for_each_entry_safe(vha, temp, &ha->vp_list, list) {
1967                 if (vha && vha->fc_vport)
1968                         fc_vport_terminate(vha->fc_vport);
1969         }
1970
1971         set_bit(UNLOADING, &base_vha->dpc_flags);
1972
1973         qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
1974
1975         qla2x00_dfs_remove(base_vha);
1976
1977         qla84xx_put_chip(base_vha);
1978
1979         /* Disable timer */
1980         if (base_vha->timer_active)
1981                 qla2x00_stop_timer(base_vha);
1982
1983         base_vha->flags.online = 0;
1984
1985         /* Kill the kernel thread for this host */
1986         if (ha->dpc_thread) {
1987                 struct task_struct *t = ha->dpc_thread;
1988
1989                 /*
1990                  * qla2xxx_wake_dpc checks for ->dpc_thread
1991                  * so we need to zero it out.
1992                  */
1993                 ha->dpc_thread = NULL;
1994                 kthread_stop(t);
1995         }
1996
1997         qla2x00_free_sysfs_attr(base_vha);
1998
1999         fc_remove_host(base_vha->host);
2000
2001         scsi_remove_host(base_vha->host);
2002
2003         qla2x00_free_device(base_vha);
2004
2005         scsi_host_put(base_vha->host);
2006
2007         if (ha->iobase)
2008                 iounmap(ha->iobase);
2009
2010         if (ha->mqiobase)
2011                 iounmap(ha->mqiobase);
2012
2013         pci_release_selected_regions(ha->pdev, ha->bars);
2014         kfree(ha);
2015         ha = NULL;
2016
2017         pci_disable_device(pdev);
2018         pci_set_drvdata(pdev, NULL);
2019 }
2020
2021 static void
2022 qla2x00_free_device(scsi_qla_host_t *vha)
2023 {
2024         struct qla_hw_data *ha = vha->hw;
2025
2026         qla25xx_delete_queues(vha);
2027
2028         if (ha->flags.fce_enabled)
2029                 qla2x00_disable_fce_trace(vha, NULL, NULL);
2030
2031         if (ha->eft)
2032                 qla2x00_disable_eft_trace(vha);
2033
2034         /* Stop currently executing firmware. */
2035         qla2x00_try_to_stop_firmware(vha);
2036
2037         /* turn-off interrupts on the card */
2038         if (ha->interrupts_on)
2039                 ha->isp_ops->disable_intrs(ha);
2040
2041         qla2x00_free_irqs(vha);
2042
2043         qla2x00_mem_free(ha);
2044
2045         qla2x00_free_queues(ha);
2046 }
2047
2048 static inline void
2049 qla2x00_schedule_rport_del(struct scsi_qla_host *vha, fc_port_t *fcport,
2050     int defer)
2051 {
2052         struct fc_rport *rport;
2053
2054         if (!fcport->rport)
2055                 return;
2056
2057         rport = fcport->rport;
2058         if (defer) {
2059                 spin_lock_irq(vha->host->host_lock);
2060                 fcport->drport = rport;
2061                 spin_unlock_irq(vha->host->host_lock);
2062                 set_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags);
2063                 qla2xxx_wake_dpc(vha);
2064         } else
2065                 fc_remote_port_delete(rport);
2066 }
2067
2068 /*
2069  * qla2x00_mark_device_lost Updates fcport state when device goes offline.
2070  *
2071  * Input: ha = adapter block pointer.  fcport = port structure pointer.
2072  *
2073  * Return: None.
2074  *
2075  * Context:
2076  */
2077 void qla2x00_mark_device_lost(scsi_qla_host_t *vha, fc_port_t *fcport,
2078     int do_login, int defer)
2079 {
2080         if (atomic_read(&fcport->state) == FCS_ONLINE &&
2081             vha->vp_idx == fcport->vp_idx) {
2082                 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2083                 qla2x00_schedule_rport_del(vha, fcport, defer);
2084         }
2085         /*
2086          * We may need to retry the login, so don't change the state of the
2087          * port but do the retries.
2088          */
2089         if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
2090                 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2091
2092         if (!do_login)
2093                 return;
2094
2095         if (fcport->login_retry == 0) {
2096                 fcport->login_retry = vha->hw->login_retry_count;
2097                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
2098
2099                 DEBUG(printk("scsi(%ld): Port login retry: "
2100                     "%02x%02x%02x%02x%02x%02x%02x%02x, "
2101                     "id = 0x%04x retry cnt=%d\n",
2102                     vha->host_no,
2103                     fcport->port_name[0],
2104                     fcport->port_name[1],
2105                     fcport->port_name[2],
2106                     fcport->port_name[3],
2107                     fcport->port_name[4],
2108                     fcport->port_name[5],
2109                     fcport->port_name[6],
2110                     fcport->port_name[7],
2111                     fcport->loop_id,
2112                     fcport->login_retry));
2113         }
2114 }
2115
2116 /*
2117  * qla2x00_mark_all_devices_lost
2118  *      Updates fcport state when device goes offline.
2119  *
2120  * Input:
2121  *      ha = adapter block pointer.
2122  *      fcport = port structure pointer.
2123  *
2124  * Return:
2125  *      None.
2126  *
2127  * Context:
2128  */
2129 void
2130 qla2x00_mark_all_devices_lost(scsi_qla_host_t *vha, int defer)
2131 {
2132         fc_port_t *fcport;
2133
2134         list_for_each_entry(fcport, &vha->vp_fcports, list) {
2135                 if (vha->vp_idx != fcport->vp_idx)
2136                         continue;
2137                 /*
2138                  * No point in marking the device as lost, if the device is
2139                  * already DEAD.
2140                  */
2141                 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
2142                         continue;
2143                 if (atomic_read(&fcport->state) == FCS_ONLINE) {
2144                         atomic_set(&fcport->state, FCS_DEVICE_LOST);
2145                         qla2x00_schedule_rport_del(vha, fcport, defer);
2146                 } else
2147                         atomic_set(&fcport->state, FCS_DEVICE_LOST);
2148         }
2149 }
2150
2151 /*
2152 * qla2x00_mem_alloc
2153 *      Allocates adapter memory.
2154 *
2155 * Returns:
2156 *      0  = success.
2157 *      !0  = failure.
2158 */
2159 static int
2160 qla2x00_mem_alloc(struct qla_hw_data *ha, uint16_t req_len, uint16_t rsp_len,
2161         struct req_que **req, struct rsp_que **rsp)
2162 {
2163         char    name[16];
2164
2165         ha->init_cb = dma_alloc_coherent(&ha->pdev->dev, ha->init_cb_size,
2166                 &ha->init_cb_dma, GFP_KERNEL);
2167         if (!ha->init_cb)
2168                 goto fail;
2169
2170         ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
2171                 &ha->gid_list_dma, GFP_KERNEL);
2172         if (!ha->gid_list)
2173                 goto fail_free_init_cb;
2174
2175         ha->srb_mempool = mempool_create_slab_pool(SRB_MIN_REQ, srb_cachep);
2176         if (!ha->srb_mempool)
2177                 goto fail_free_gid_list;
2178
2179         /* Get memory for cached NVRAM */
2180         ha->nvram = kzalloc(MAX_NVRAM_SIZE, GFP_KERNEL);
2181         if (!ha->nvram)
2182                 goto fail_free_srb_mempool;
2183
2184         snprintf(name, sizeof(name), "%s_%d", QLA2XXX_DRIVER_NAME,
2185                 ha->pdev->device);
2186         ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
2187                 DMA_POOL_SIZE, 8, 0);
2188         if (!ha->s_dma_pool)
2189                 goto fail_free_nvram;
2190
2191         /* Allocate memory for SNS commands */
2192         if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2193         /* Get consistent memory allocated for SNS commands */
2194                 ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
2195                 sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma, GFP_KERNEL);
2196                 if (!ha->sns_cmd)
2197                         goto fail_dma_pool;
2198         } else {
2199         /* Get consistent memory allocated for MS IOCB */
2200                 ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
2201                         &ha->ms_iocb_dma);
2202                 if (!ha->ms_iocb)
2203                         goto fail_dma_pool;
2204         /* Get consistent memory allocated for CT SNS commands */
2205                 ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
2206                         sizeof(struct ct_sns_pkt), &ha->ct_sns_dma, GFP_KERNEL);
2207                 if (!ha->ct_sns)
2208                         goto fail_free_ms_iocb;
2209         }
2210
2211         /* Allocate memory for request ring */
2212         *req = kzalloc(sizeof(struct req_que), GFP_KERNEL);
2213         if (!*req) {
2214                 DEBUG(printk("Unable to allocate memory for req\n"));
2215                 goto fail_req;
2216         }
2217         (*req)->length = req_len;
2218         (*req)->ring = dma_alloc_coherent(&ha->pdev->dev,
2219                 ((*req)->length + 1) * sizeof(request_t),
2220                 &(*req)->dma, GFP_KERNEL);
2221         if (!(*req)->ring) {
2222                 DEBUG(printk("Unable to allocate memory for req_ring\n"));
2223                 goto fail_req_ring;
2224         }
2225         /* Allocate memory for response ring */
2226         *rsp = kzalloc(sizeof(struct rsp_que), GFP_KERNEL);
2227         if (!*rsp) {
2228                 qla_printk(KERN_WARNING, ha,
2229                         "Unable to allocate memory for rsp\n");
2230                 goto fail_rsp;
2231         }
2232         (*rsp)->hw = ha;
2233         (*rsp)->length = rsp_len;
2234         (*rsp)->ring = dma_alloc_coherent(&ha->pdev->dev,
2235                 ((*rsp)->length + 1) * sizeof(response_t),
2236                 &(*rsp)->dma, GFP_KERNEL);
2237         if (!(*rsp)->ring) {
2238                 qla_printk(KERN_WARNING, ha,
2239                         "Unable to allocate memory for rsp_ring\n");
2240                 goto fail_rsp_ring;
2241         }
2242         (*req)->rsp = *rsp;
2243         (*rsp)->req = *req;
2244         /* Allocate memory for NVRAM data for vports */
2245         if (ha->nvram_npiv_size) {
2246                 ha->npiv_info = kzalloc(sizeof(struct qla_npiv_entry) *
2247                                         ha->nvram_npiv_size, GFP_KERNEL);
2248                 if (!ha->npiv_info) {
2249                         qla_printk(KERN_WARNING, ha,
2250                                 "Unable to allocate memory for npiv info\n");
2251                         goto fail_npiv_info;
2252                 }
2253         } else
2254                 ha->npiv_info = NULL;
2255
2256         /* Get consistent memory allocated for EX-INIT-CB. */
2257         if (IS_QLA81XX(ha)) {
2258                 ha->ex_init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
2259                     &ha->ex_init_cb_dma);
2260                 if (!ha->ex_init_cb)
2261                         goto fail_ex_init_cb;
2262         }
2263
2264         INIT_LIST_HEAD(&ha->vp_list);
2265         return 1;
2266
2267 fail_ex_init_cb:
2268         kfree(ha->npiv_info);
2269 fail_npiv_info:
2270         dma_free_coherent(&ha->pdev->dev, ((*rsp)->length + 1) *
2271                 sizeof(response_t), (*rsp)->ring, (*rsp)->dma);
2272         (*rsp)->ring = NULL;
2273         (*rsp)->dma = 0;
2274 fail_rsp_ring:
2275         kfree(*rsp);
2276 fail_rsp:
2277         dma_free_coherent(&ha->pdev->dev, ((*req)->length + 1) *
2278                 sizeof(request_t), (*req)->ring, (*req)->dma);
2279         (*req)->ring = NULL;
2280         (*req)->dma = 0;
2281 fail_req_ring:
2282         kfree(*req);
2283 fail_req:
2284         dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
2285                 ha->ct_sns, ha->ct_sns_dma);
2286         ha->ct_sns = NULL;
2287         ha->ct_sns_dma = 0;
2288 fail_free_ms_iocb:
2289         dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
2290         ha->ms_iocb = NULL;
2291         ha->ms_iocb_dma = 0;
2292 fail_dma_pool:
2293         dma_pool_destroy(ha->s_dma_pool);
2294         ha->s_dma_pool = NULL;
2295 fail_free_nvram:
2296         kfree(ha->nvram);
2297         ha->nvram = NULL;
2298 fail_free_srb_mempool:
2299         mempool_destroy(ha->srb_mempool);
2300         ha->srb_mempool = NULL;
2301 fail_free_gid_list:
2302         dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
2303         ha->gid_list_dma);
2304         ha->gid_list = NULL;
2305         ha->gid_list_dma = 0;
2306 fail_free_init_cb:
2307         dma_free_coherent(&ha->pdev->dev, ha->init_cb_size, ha->init_cb,
2308         ha->init_cb_dma);
2309         ha->init_cb = NULL;
2310         ha->init_cb_dma = 0;
2311 fail:
2312         DEBUG(printk("%s: Memory allocation failure\n", __func__));
2313         return -ENOMEM;
2314 }
2315
2316 /*
2317 * qla2x00_mem_free
2318 *      Frees all adapter allocated memory.
2319 *
2320 * Input:
2321 *      ha = adapter block pointer.
2322 */
2323 static void
2324 qla2x00_mem_free(struct qla_hw_data *ha)
2325 {
2326         if (ha->srb_mempool)
2327                 mempool_destroy(ha->srb_mempool);
2328
2329         if (ha->fce)
2330                 dma_free_coherent(&ha->pdev->dev, FCE_SIZE, ha->fce,
2331                 ha->fce_dma);
2332
2333         if (ha->fw_dump) {
2334                 if (ha->eft)
2335                         dma_free_coherent(&ha->pdev->dev,
2336                         ntohl(ha->fw_dump->eft_size), ha->eft, ha->eft_dma);
2337                 vfree(ha->fw_dump);
2338         }
2339
2340         if (ha->sns_cmd)
2341                 dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
2342                 ha->sns_cmd, ha->sns_cmd_dma);
2343
2344         if (ha->ct_sns)
2345                 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
2346                 ha->ct_sns, ha->ct_sns_dma);
2347
2348         if (ha->sfp_data)
2349                 dma_pool_free(ha->s_dma_pool, ha->sfp_data, ha->sfp_data_dma);
2350
2351         if (ha->edc_data)
2352                 dma_pool_free(ha->s_dma_pool, ha->edc_data, ha->edc_data_dma);
2353
2354         if (ha->ms_iocb)
2355                 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
2356
2357         if (ha->ex_init_cb)
2358                 dma_pool_free(ha->s_dma_pool, ha->ex_init_cb, ha->ex_init_cb_dma);
2359
2360         if (ha->s_dma_pool)
2361                 dma_pool_destroy(ha->s_dma_pool);
2362
2363         if (ha->gid_list)
2364                 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
2365                 ha->gid_list_dma);
2366
2367         if (ha->init_cb)
2368                 dma_free_coherent(&ha->pdev->dev, ha->init_cb_size,
2369                 ha->init_cb, ha->init_cb_dma);
2370         vfree(ha->optrom_buffer);
2371         kfree(ha->nvram);
2372         kfree(ha->npiv_info);
2373
2374         ha->srb_mempool = NULL;
2375         ha->eft = NULL;
2376         ha->eft_dma = 0;
2377         ha->sns_cmd = NULL;
2378         ha->sns_cmd_dma = 0;
2379         ha->ct_sns = NULL;
2380         ha->ct_sns_dma = 0;
2381         ha->ms_iocb = NULL;
2382         ha->ms_iocb_dma = 0;
2383         ha->init_cb = NULL;
2384         ha->init_cb_dma = 0;
2385         ha->ex_init_cb = NULL;
2386         ha->ex_init_cb_dma = 0;
2387
2388         ha->s_dma_pool = NULL;
2389
2390         ha->gid_list = NULL;
2391         ha->gid_list_dma = 0;
2392
2393         ha->fw_dump = NULL;
2394         ha->fw_dumped = 0;
2395         ha->fw_dump_reading = 0;
2396 }
2397
2398 struct scsi_qla_host *qla2x00_create_host(struct scsi_host_template *sht,
2399                                                 struct qla_hw_data *ha)
2400 {
2401         struct Scsi_Host *host;
2402         struct scsi_qla_host *vha = NULL;
2403
2404         host = scsi_host_alloc(sht, sizeof(scsi_qla_host_t));
2405         if (host == NULL) {
2406                 printk(KERN_WARNING
2407                 "qla2xxx: Couldn't allocate host from scsi layer!\n");
2408                 goto fail;
2409         }
2410
2411         /* Clear our data area */
2412         vha = shost_priv(host);
2413         memset(vha, 0, sizeof(scsi_qla_host_t));
2414
2415         vha->host = host;
2416         vha->host_no = host->host_no;
2417         vha->hw = ha;
2418
2419         INIT_LIST_HEAD(&vha->vp_fcports);
2420         INIT_LIST_HEAD(&vha->work_list);
2421         INIT_LIST_HEAD(&vha->list);
2422
2423         sprintf(vha->host_str, "%s_%ld", QLA2XXX_DRIVER_NAME, vha->host_no);
2424         return vha;
2425
2426 fail:
2427         return vha;
2428 }
2429
2430 static struct qla_work_evt *
2431 qla2x00_alloc_work(struct scsi_qla_host *vha, enum qla_work_type type,
2432     int locked)
2433 {
2434         struct qla_work_evt *e;
2435
2436         e = kzalloc(sizeof(struct qla_work_evt), locked ? GFP_ATOMIC:
2437             GFP_KERNEL);
2438         if (!e)
2439                 return NULL;
2440
2441         INIT_LIST_HEAD(&e->list);
2442         e->type = type;
2443         e->flags = QLA_EVT_FLAG_FREE;
2444         return e;
2445 }
2446
2447 static int
2448 qla2x00_post_work(struct scsi_qla_host *vha, struct qla_work_evt *e, int locked)
2449 {
2450         unsigned long uninitialized_var(flags);
2451         struct qla_hw_data *ha = vha->hw;
2452
2453         if (!locked)
2454                 spin_lock_irqsave(&ha->hardware_lock, flags);
2455         list_add_tail(&e->list, &vha->work_list);
2456         qla2xxx_wake_dpc(vha);
2457         if (!locked)
2458                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2459         return QLA_SUCCESS;
2460 }
2461
2462 int
2463 qla2x00_post_aen_work(struct scsi_qla_host *vha, enum fc_host_event_code code,
2464     u32 data)
2465 {
2466         struct qla_work_evt *e;
2467
2468         e = qla2x00_alloc_work(vha, QLA_EVT_AEN, 1);
2469         if (!e)
2470                 return QLA_FUNCTION_FAILED;
2471
2472         e->u.aen.code = code;
2473         e->u.aen.data = data;
2474         return qla2x00_post_work(vha, e, 1);
2475 }
2476
2477 int
2478 qla2x00_post_idc_ack_work(struct scsi_qla_host *vha, uint16_t *mb)
2479 {
2480         struct qla_work_evt *e;
2481
2482         e = qla2x00_alloc_work(vha, QLA_EVT_IDC_ACK, 1);
2483         if (!e)
2484                 return QLA_FUNCTION_FAILED;
2485
2486         memcpy(e->u.idc_ack.mb, mb, QLA_IDC_ACK_REGS * sizeof(uint16_t));
2487         return qla2x00_post_work(vha, e, 1);
2488 }
2489
2490 static void
2491 qla2x00_do_work(struct scsi_qla_host *vha)
2492 {
2493         struct qla_work_evt *e;
2494         struct qla_hw_data *ha = vha->hw;
2495
2496         spin_lock_irq(&ha->hardware_lock);
2497         while (!list_empty(&vha->work_list)) {
2498                 e = list_entry(vha->work_list.next, struct qla_work_evt, list);
2499                 list_del_init(&e->list);
2500                 spin_unlock_irq(&ha->hardware_lock);
2501
2502                 switch (e->type) {
2503                 case QLA_EVT_AEN:
2504                         fc_host_post_event(vha->host, fc_get_event_number(),
2505                             e->u.aen.code, e->u.aen.data);
2506                         break;
2507                 case QLA_EVT_IDC_ACK:
2508                         qla81xx_idc_ack(vha, e->u.idc_ack.mb);
2509                         break;
2510                 }
2511                 if (e->flags & QLA_EVT_FLAG_FREE)
2512                         kfree(e);
2513                 spin_lock_irq(&ha->hardware_lock);
2514         }
2515         spin_unlock_irq(&ha->hardware_lock);
2516 }
2517 /* Relogins all the fcports of a vport
2518  * Context: dpc thread
2519  */
2520 void qla2x00_relogin(struct scsi_qla_host *vha)
2521 {
2522         fc_port_t       *fcport;
2523         int status;
2524         uint16_t        next_loopid = 0;
2525         struct qla_hw_data *ha = vha->hw;
2526
2527         list_for_each_entry(fcport, &vha->vp_fcports, list) {
2528         /*
2529          * If the port is not ONLINE then try to login
2530          * to it if we haven't run out of retries.
2531          */
2532                 if (atomic_read(&fcport->state) !=
2533                         FCS_ONLINE && fcport->login_retry) {
2534
2535                         if (fcport->flags & FCF_FABRIC_DEVICE) {
2536                                 if (fcport->flags & FCF_TAPE_PRESENT)
2537                                         ha->isp_ops->fabric_logout(vha,
2538                                                         fcport->loop_id,
2539                                                         fcport->d_id.b.domain,
2540                                                         fcport->d_id.b.area,
2541                                                         fcport->d_id.b.al_pa);
2542
2543                                 status = qla2x00_fabric_login(vha, fcport,
2544                                                         &next_loopid);
2545                         } else
2546                                 status = qla2x00_local_device_login(vha,
2547                                                                 fcport);
2548
2549                         fcport->login_retry--;
2550                         if (status == QLA_SUCCESS) {
2551                                 fcport->old_loop_id = fcport->loop_id;
2552
2553                                 DEBUG(printk("scsi(%ld): port login OK: logged "
2554                                 "in ID 0x%x\n", vha->host_no, fcport->loop_id));
2555
2556                                 qla2x00_update_fcport(vha, fcport);
2557
2558                         } else if (status == 1) {
2559                                 set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
2560                                 /* retry the login again */
2561                                 DEBUG(printk("scsi(%ld): Retrying"
2562                                 " %d login again loop_id 0x%x\n",
2563                                 vha->host_no, fcport->login_retry,
2564                                                 fcport->loop_id));
2565                         } else {
2566                                 fcport->login_retry = 0;
2567                         }
2568
2569                         if (fcport->login_retry == 0 && status != QLA_SUCCESS)
2570                                 fcport->loop_id = FC_NO_LOOP_ID;
2571                 }
2572                 if (test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags))
2573                         break;
2574         }
2575 }
2576
2577 /**************************************************************************
2578 * qla2x00_do_dpc
2579 *   This kernel thread is a task that is schedule by the interrupt handler
2580 *   to perform the background processing for interrupts.
2581 *
2582 * Notes:
2583 * This task always run in the context of a kernel thread.  It
2584 * is kick-off by the driver's detect code and starts up
2585 * up one per adapter. It immediately goes to sleep and waits for
2586 * some fibre event.  When either the interrupt handler or
2587 * the timer routine detects a event it will one of the task
2588 * bits then wake us up.
2589 **************************************************************************/
2590 static int
2591 qla2x00_do_dpc(void *data)
2592 {
2593         int             rval;
2594         scsi_qla_host_t *base_vha;
2595         struct qla_hw_data *ha;
2596
2597         ha = (struct qla_hw_data *)data;
2598         base_vha = pci_get_drvdata(ha->pdev);
2599
2600         set_user_nice(current, -20);
2601
2602         while (!kthread_should_stop()) {
2603                 DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
2604
2605                 set_current_state(TASK_INTERRUPTIBLE);
2606                 schedule();
2607                 __set_current_state(TASK_RUNNING);
2608
2609                 DEBUG3(printk("qla2x00: DPC handler waking up\n"));
2610
2611                 /* Initialization not yet finished. Don't do anything yet. */
2612                 if (!base_vha->flags.init_done)
2613                         continue;
2614
2615                 DEBUG3(printk("scsi(%ld): DPC handler\n", base_vha->host_no));
2616
2617                 ha->dpc_active = 1;
2618
2619                 if (ha->flags.mbox_busy) {
2620                         ha->dpc_active = 0;
2621                         continue;
2622                 }
2623
2624                 qla2x00_do_work(base_vha);
2625
2626                 if (test_and_clear_bit(ISP_ABORT_NEEDED,
2627                                                 &base_vha->dpc_flags)) {
2628
2629                         DEBUG(printk("scsi(%ld): dpc: sched "
2630                             "qla2x00_abort_isp ha = %p\n",
2631                             base_vha->host_no, ha));
2632                         if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
2633                             &base_vha->dpc_flags))) {
2634
2635                                 if (qla2x00_abort_isp(base_vha)) {
2636                                         /* failed. retry later */
2637                                         set_bit(ISP_ABORT_NEEDED,
2638                                             &base_vha->dpc_flags);
2639                                 }
2640                                 clear_bit(ABORT_ISP_ACTIVE,
2641                                                 &base_vha->dpc_flags);
2642                         }
2643
2644                         DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
2645                             base_vha->host_no));
2646                 }
2647
2648                 if (test_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags)) {
2649                         qla2x00_update_fcports(base_vha);
2650                         clear_bit(FCPORT_UPDATE_NEEDED, &base_vha->dpc_flags);
2651                 }
2652
2653                 if (test_and_clear_bit(RESET_MARKER_NEEDED,
2654                                                         &base_vha->dpc_flags) &&
2655                     (!(test_and_set_bit(RESET_ACTIVE, &base_vha->dpc_flags)))) {
2656
2657                         DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
2658                             base_vha->host_no));
2659
2660                         qla2x00_rst_aen(base_vha);
2661                         clear_bit(RESET_ACTIVE, &base_vha->dpc_flags);
2662                 }
2663
2664                 /* Retry each device up to login retry count */
2665                 if ((test_and_clear_bit(RELOGIN_NEEDED,
2666                                                 &base_vha->dpc_flags)) &&
2667                     !test_bit(LOOP_RESYNC_NEEDED, &base_vha->dpc_flags) &&
2668                     atomic_read(&base_vha->loop_state) != LOOP_DOWN) {
2669
2670                         DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
2671                                         base_vha->host_no));
2672                         qla2x00_relogin(base_vha);
2673
2674                         DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
2675                             base_vha->host_no));
2676                 }
2677
2678                 if (test_and_clear_bit(LOOP_RESYNC_NEEDED,
2679                                                         &base_vha->dpc_flags)) {
2680
2681                         DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
2682                                 base_vha->host_no));
2683
2684                         if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
2685                             &base_vha->dpc_flags))) {
2686
2687                                 rval = qla2x00_loop_resync(base_vha);
2688
2689                                 clear_bit(LOOP_RESYNC_ACTIVE,
2690                                                 &base_vha->dpc_flags);
2691                         }
2692
2693                         DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
2694                             base_vha->host_no));
2695                 }
2696
2697                 if (test_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags) &&
2698                     atomic_read(&base_vha->loop_state) == LOOP_READY) {
2699                         clear_bit(NPIV_CONFIG_NEEDED, &base_vha->dpc_flags);
2700                         qla2xxx_flash_npiv_conf(base_vha);
2701                 }
2702
2703                 if (!ha->interrupts_on)
2704                         ha->isp_ops->enable_intrs(ha);
2705
2706                 if (test_and_clear_bit(BEACON_BLINK_NEEDED,
2707                                         &base_vha->dpc_flags))
2708                         ha->isp_ops->beacon_blink(base_vha);
2709
2710                 qla2x00_do_dpc_all_vps(base_vha);
2711
2712                 ha->dpc_active = 0;
2713         } /* End of while(1) */
2714
2715         DEBUG(printk("scsi(%ld): DPC handler exiting\n", base_vha->host_no));
2716
2717         /*
2718          * Make sure that nobody tries to wake us up again.
2719          */
2720         ha->dpc_active = 0;
2721
2722         return 0;
2723 }
2724
2725 void
2726 qla2xxx_wake_dpc(struct scsi_qla_host *vha)
2727 {
2728         struct qla_hw_data *ha = vha->hw;
2729         struct task_struct *t = ha->dpc_thread;
2730
2731         if (!test_bit(UNLOADING, &vha->dpc_flags) && t)
2732                 wake_up_process(t);
2733 }
2734
2735 /*
2736 *  qla2x00_rst_aen
2737 *      Processes asynchronous reset.
2738 *
2739 * Input:
2740 *      ha  = adapter block pointer.
2741 */
2742 static void
2743 qla2x00_rst_aen(scsi_qla_host_t *vha)
2744 {
2745         if (vha->flags.online && !vha->flags.reset_active &&
2746             !atomic_read(&vha->loop_down_timer) &&
2747             !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))) {
2748                 do {
2749                         clear_bit(RESET_MARKER_NEEDED, &vha->dpc_flags);
2750
2751                         /*
2752                          * Issue marker command only when we are going to start
2753                          * the I/O.
2754                          */
2755                         vha->marker_needed = 1;
2756                 } while (!atomic_read(&vha->loop_down_timer) &&
2757                     (test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags)));
2758         }
2759 }
2760
2761 static void
2762 qla2x00_sp_free_dma(srb_t *sp)
2763 {
2764         struct scsi_cmnd *cmd = sp->cmd;
2765
2766         if (sp->flags & SRB_DMA_VALID) {
2767                 scsi_dma_unmap(cmd);
2768                 sp->flags &= ~SRB_DMA_VALID;
2769         }
2770         CMD_SP(cmd) = NULL;
2771 }
2772
2773 void
2774 qla2x00_sp_compl(struct qla_hw_data *ha, srb_t *sp)
2775 {
2776         struct scsi_cmnd *cmd = sp->cmd;
2777
2778         qla2x00_sp_free_dma(sp);
2779
2780         mempool_free(sp, ha->srb_mempool);
2781
2782         cmd->scsi_done(cmd);
2783 }
2784
2785 /**************************************************************************
2786 *   qla2x00_timer
2787 *
2788 * Description:
2789 *   One second timer
2790 *
2791 * Context: Interrupt
2792 ***************************************************************************/
2793 void
2794 qla2x00_timer(scsi_qla_host_t *vha)
2795 {
2796         unsigned long   cpu_flags = 0;
2797         fc_port_t       *fcport;
2798         int             start_dpc = 0;
2799         int             index;
2800         srb_t           *sp;
2801         int             t;
2802         struct qla_hw_data *ha = vha->hw;
2803         struct req_que *req;
2804         /*
2805          * Ports - Port down timer.
2806          *
2807          * Whenever, a port is in the LOST state we start decrementing its port
2808          * down timer every second until it reaches zero. Once  it reaches zero
2809          * the port it marked DEAD.
2810          */
2811         t = 0;
2812         list_for_each_entry(fcport, &vha->vp_fcports, list) {
2813                 if (fcport->port_type != FCT_TARGET)
2814                         continue;
2815
2816                 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2817
2818                         if (atomic_read(&fcport->port_down_timer) == 0)
2819                                 continue;
2820
2821                         if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
2822                                 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
2823
2824                         DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
2825                             "%d remaining\n",
2826                             vha->host_no,
2827                             t, atomic_read(&fcport->port_down_timer)));
2828                 }
2829                 t++;
2830         } /* End of for fcport  */
2831
2832
2833         /* Loop down handler. */
2834         if (atomic_read(&vha->loop_down_timer) > 0 &&
2835             !(test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags))
2836                 && vha->flags.online) {
2837
2838                 if (atomic_read(&vha->loop_down_timer) ==
2839                     vha->loop_down_abort_time) {
2840
2841                         DEBUG(printk("scsi(%ld): Loop Down - aborting the "
2842                             "queues before time expire\n",
2843                             vha->host_no));
2844
2845                         if (!IS_QLA2100(ha) && vha->link_down_timeout)
2846                                 atomic_set(&vha->loop_state, LOOP_DEAD);
2847
2848                         /* Schedule an ISP abort to return any tape commands. */
2849                         /* NPIV - scan physical port only */
2850                         if (!vha->vp_idx) {
2851                                 spin_lock_irqsave(&ha->hardware_lock,
2852                                     cpu_flags);
2853                                 req = ha->req_q_map[0];
2854                                 for (index = 1;
2855                                     index < MAX_OUTSTANDING_COMMANDS;
2856                                     index++) {
2857                                         fc_port_t *sfcp;
2858
2859                                         sp = req->outstanding_cmds[index];
2860                                         if (!sp)
2861                                                 continue;
2862                                         sfcp = sp->fcport;
2863                                         if (!(sfcp->flags & FCF_TAPE_PRESENT))
2864                                                 continue;
2865
2866                                         set_bit(ISP_ABORT_NEEDED,
2867                                                         &vha->dpc_flags);
2868                                         break;
2869                                 }
2870                                 spin_unlock_irqrestore(&ha->hardware_lock,
2871                                                                 cpu_flags);
2872                         }
2873                         start_dpc++;
2874                 }
2875
2876                 /* if the loop has been down for 4 minutes, reinit adapter */
2877                 if (atomic_dec_and_test(&vha->loop_down_timer) != 0) {
2878                         if (!(vha->device_flags & DFLG_NO_CABLE) &&
2879                             !vha->vp_idx) {
2880                                 DEBUG(printk("scsi(%ld): Loop down - "
2881                                     "aborting ISP.\n",
2882                                     vha->host_no));
2883                                 qla_printk(KERN_WARNING, ha,
2884                                     "Loop down - aborting ISP.\n");
2885
2886                                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
2887                         }
2888                 }
2889                 DEBUG3(printk("scsi(%ld): Loop Down - seconds remaining %d\n",
2890                     vha->host_no,
2891                     atomic_read(&vha->loop_down_timer)));
2892         }
2893
2894         /* Check if beacon LED needs to be blinked */
2895         if (ha->beacon_blink_led == 1) {
2896                 set_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags);
2897                 start_dpc++;
2898         }
2899
2900         /* Process any deferred work. */
2901         if (!list_empty(&vha->work_list))
2902                 start_dpc++;
2903
2904         /* Schedule the DPC routine if needed */
2905         if ((test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
2906             test_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags) ||
2907             test_bit(FCPORT_UPDATE_NEEDED, &vha->dpc_flags) ||
2908             start_dpc ||
2909             test_bit(RESET_MARKER_NEEDED, &vha->dpc_flags) ||
2910             test_bit(BEACON_BLINK_NEEDED, &vha->dpc_flags) ||
2911             test_bit(VP_DPC_NEEDED, &vha->dpc_flags) ||
2912             test_bit(RELOGIN_NEEDED, &vha->dpc_flags)))
2913                 qla2xxx_wake_dpc(vha);
2914
2915         qla2x00_restart_timer(vha, WATCH_INTERVAL);
2916 }
2917
2918 /* Firmware interface routines. */
2919
2920 #define FW_BLOBS        7
2921 #define FW_ISP21XX      0
2922 #define FW_ISP22XX      1
2923 #define FW_ISP2300      2
2924 #define FW_ISP2322      3
2925 #define FW_ISP24XX      4
2926 #define FW_ISP25XX      5
2927 #define FW_ISP81XX      6
2928
2929 #define FW_FILE_ISP21XX "ql2100_fw.bin"
2930 #define FW_FILE_ISP22XX "ql2200_fw.bin"
2931 #define FW_FILE_ISP2300 "ql2300_fw.bin"
2932 #define FW_FILE_ISP2322 "ql2322_fw.bin"
2933 #define FW_FILE_ISP24XX "ql2400_fw.bin"
2934 #define FW_FILE_ISP25XX "ql2500_fw.bin"
2935 #define FW_FILE_ISP81XX "ql8100_fw.bin"
2936
2937 static DEFINE_MUTEX(qla_fw_lock);
2938
2939 static struct fw_blob qla_fw_blobs[FW_BLOBS] = {
2940         { .name = FW_FILE_ISP21XX, .segs = { 0x1000, 0 }, },
2941         { .name = FW_FILE_ISP22XX, .segs = { 0x1000, 0 }, },
2942         { .name = FW_FILE_ISP2300, .segs = { 0x800, 0 }, },
2943         { .name = FW_FILE_ISP2322, .segs = { 0x800, 0x1c000, 0x1e000, 0 }, },
2944         { .name = FW_FILE_ISP24XX, },
2945         { .name = FW_FILE_ISP25XX, },
2946         { .name = FW_FILE_ISP81XX, },
2947 };
2948
2949 struct fw_blob *
2950 qla2x00_request_firmware(scsi_qla_host_t *vha)
2951 {
2952         struct qla_hw_data *ha = vha->hw;
2953         struct fw_blob *blob;
2954
2955         blob = NULL;
2956         if (IS_QLA2100(ha)) {
2957                 blob = &qla_fw_blobs[FW_ISP21XX];
2958         } else if (IS_QLA2200(ha)) {
2959                 blob = &qla_fw_blobs[FW_ISP22XX];
2960         } else if (IS_QLA2300(ha) || IS_QLA2312(ha) || IS_QLA6312(ha)) {
2961                 blob = &qla_fw_blobs[FW_ISP2300];
2962         } else if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
2963                 blob = &qla_fw_blobs[FW_ISP2322];
2964         } else if (IS_QLA24XX_TYPE(ha)) {
2965                 blob = &qla_fw_blobs[FW_ISP24XX];
2966         } else if (IS_QLA25XX(ha)) {
2967                 blob = &qla_fw_blobs[FW_ISP25XX];
2968         } else if (IS_QLA81XX(ha)) {
2969                 blob = &qla_fw_blobs[FW_ISP81XX];
2970         }
2971
2972         mutex_lock(&qla_fw_lock);
2973         if (blob->fw)
2974                 goto out;
2975
2976         if (request_firmware(&blob->fw, blob->name, &ha->pdev->dev)) {
2977                 DEBUG2(printk("scsi(%ld): Failed to load firmware image "
2978                     "(%s).\n", vha->host_no, blob->name));
2979                 blob->fw = NULL;
2980                 blob = NULL;
2981                 goto out;
2982         }
2983
2984 out:
2985         mutex_unlock(&qla_fw_lock);
2986         return blob;
2987 }
2988
2989 static void
2990 qla2x00_release_firmware(void)
2991 {
2992         int idx;
2993
2994         mutex_lock(&qla_fw_lock);
2995         for (idx = 0; idx < FW_BLOBS; idx++)
2996                 if (qla_fw_blobs[idx].fw)
2997                         release_firmware(qla_fw_blobs[idx].fw);
2998         mutex_unlock(&qla_fw_lock);
2999 }
3000
3001 static pci_ers_result_t
3002 qla2xxx_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
3003 {
3004         scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3005
3006         switch (state) {
3007         case pci_channel_io_normal:
3008                 return PCI_ERS_RESULT_CAN_RECOVER;
3009         case pci_channel_io_frozen:
3010                 pci_disable_device(pdev);
3011                 return PCI_ERS_RESULT_NEED_RESET;
3012         case pci_channel_io_perm_failure:
3013                 qla2x00_abort_all_cmds(base_vha, DID_NO_CONNECT << 16);
3014                 return PCI_ERS_RESULT_DISCONNECT;
3015         }
3016         return PCI_ERS_RESULT_NEED_RESET;
3017 }
3018
3019 static pci_ers_result_t
3020 qla2xxx_pci_mmio_enabled(struct pci_dev *pdev)
3021 {
3022         int risc_paused = 0;
3023         uint32_t stat;
3024         unsigned long flags;
3025         scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3026         struct qla_hw_data *ha = base_vha->hw;
3027         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
3028         struct device_reg_24xx __iomem *reg24 = &ha->iobase->isp24;
3029
3030         spin_lock_irqsave(&ha->hardware_lock, flags);
3031         if (IS_QLA2100(ha) || IS_QLA2200(ha)){
3032                 stat = RD_REG_DWORD(&reg->hccr);
3033                 if (stat & HCCR_RISC_PAUSE)
3034                         risc_paused = 1;
3035         } else if (IS_QLA23XX(ha)) {
3036                 stat = RD_REG_DWORD(&reg->u.isp2300.host_status);
3037                 if (stat & HSR_RISC_PAUSED)
3038                         risc_paused = 1;
3039         } else if (IS_FWI2_CAPABLE(ha)) {
3040                 stat = RD_REG_DWORD(&reg24->host_status);
3041                 if (stat & HSRX_RISC_PAUSED)
3042                         risc_paused = 1;
3043         }
3044         spin_unlock_irqrestore(&ha->hardware_lock, flags);
3045
3046         if (risc_paused) {
3047                 qla_printk(KERN_INFO, ha, "RISC paused -- mmio_enabled, "
3048                     "Dumping firmware!\n");
3049                 ha->isp_ops->fw_dump(base_vha, 0);
3050
3051                 return PCI_ERS_RESULT_NEED_RESET;
3052         } else
3053                 return PCI_ERS_RESULT_RECOVERED;
3054 }
3055
3056 static pci_ers_result_t
3057 qla2xxx_pci_slot_reset(struct pci_dev *pdev)
3058 {
3059         pci_ers_result_t ret = PCI_ERS_RESULT_DISCONNECT;
3060         scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3061         struct qla_hw_data *ha = base_vha->hw;
3062         int rc;
3063
3064         if (ha->mem_only)
3065                 rc = pci_enable_device_mem(pdev);
3066         else
3067                 rc = pci_enable_device(pdev);
3068
3069         if (rc) {
3070                 qla_printk(KERN_WARNING, ha,
3071                     "Can't re-enable PCI device after reset.\n");
3072
3073                 return ret;
3074         }
3075         pci_set_master(pdev);
3076
3077         if (ha->isp_ops->pci_config(base_vha))
3078                 return ret;
3079
3080         set_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
3081         if (qla2x00_abort_isp(base_vha) == QLA_SUCCESS)
3082                 ret =  PCI_ERS_RESULT_RECOVERED;
3083         clear_bit(ABORT_ISP_ACTIVE, &base_vha->dpc_flags);
3084
3085         return ret;
3086 }
3087
3088 static void
3089 qla2xxx_pci_resume(struct pci_dev *pdev)
3090 {
3091         scsi_qla_host_t *base_vha = pci_get_drvdata(pdev);
3092         struct qla_hw_data *ha = base_vha->hw;
3093         int ret;
3094
3095         ret = qla2x00_wait_for_hba_online(base_vha);
3096         if (ret != QLA_SUCCESS) {
3097                 qla_printk(KERN_ERR, ha,
3098                     "the device failed to resume I/O "
3099                     "from slot/link_reset");
3100         }
3101         pci_cleanup_aer_uncorrect_error_status(pdev);
3102 }
3103
3104 static struct pci_error_handlers qla2xxx_err_handler = {
3105         .error_detected = qla2xxx_pci_error_detected,
3106         .mmio_enabled = qla2xxx_pci_mmio_enabled,
3107         .slot_reset = qla2xxx_pci_slot_reset,
3108         .resume = qla2xxx_pci_resume,
3109 };
3110
3111 static struct pci_device_id qla2xxx_pci_tbl[] = {
3112         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2100) },
3113         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2200) },
3114         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2300) },
3115         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2312) },
3116         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2322) },
3117         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6312) },
3118         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6322) },
3119         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2422) },
3120         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2432) },
3121         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8432) },
3122         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5422) },
3123         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP5432) },
3124         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2532) },
3125         { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP8001) },
3126         { 0 },
3127 };
3128 MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
3129
3130 static struct pci_driver qla2xxx_pci_driver = {
3131         .name           = QLA2XXX_DRIVER_NAME,
3132         .driver         = {
3133                 .owner          = THIS_MODULE,
3134         },
3135         .id_table       = qla2xxx_pci_tbl,
3136         .probe          = qla2x00_probe_one,
3137         .remove         = qla2x00_remove_one,
3138         .err_handler    = &qla2xxx_err_handler,
3139 };
3140
3141 /**
3142  * qla2x00_module_init - Module initialization.
3143  **/
3144 static int __init
3145 qla2x00_module_init(void)
3146 {
3147         int ret = 0;
3148
3149         /* Allocate cache for SRBs. */
3150         srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
3151             SLAB_HWCACHE_ALIGN, NULL);
3152         if (srb_cachep == NULL) {
3153                 printk(KERN_ERR
3154                     "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
3155                 return -ENOMEM;
3156         }
3157
3158         /* Derive version string. */
3159         strcpy(qla2x00_version_str, QLA2XXX_VERSION);
3160         if (ql2xextended_error_logging)
3161                 strcat(qla2x00_version_str, "-debug");
3162
3163         qla2xxx_transport_template =
3164             fc_attach_transport(&qla2xxx_transport_functions);
3165         if (!qla2xxx_transport_template) {
3166                 kmem_cache_destroy(srb_cachep);
3167                 return -ENODEV;
3168         }
3169         qla2xxx_transport_vport_template =
3170             fc_attach_transport(&qla2xxx_transport_vport_functions);
3171         if (!qla2xxx_transport_vport_template) {
3172                 kmem_cache_destroy(srb_cachep);
3173                 fc_release_transport(qla2xxx_transport_template);
3174                 return -ENODEV;
3175         }
3176
3177         printk(KERN_INFO "QLogic Fibre Channel HBA Driver: %s\n",
3178             qla2x00_version_str);
3179         ret = pci_register_driver(&qla2xxx_pci_driver);
3180         if (ret) {
3181                 kmem_cache_destroy(srb_cachep);
3182                 fc_release_transport(qla2xxx_transport_template);
3183                 fc_release_transport(qla2xxx_transport_vport_template);
3184         }
3185         return ret;
3186 }
3187
3188 /**
3189  * qla2x00_module_exit - Module cleanup.
3190  **/
3191 static void __exit
3192 qla2x00_module_exit(void)
3193 {
3194         pci_unregister_driver(&qla2xxx_pci_driver);
3195         qla2x00_release_firmware();
3196         kmem_cache_destroy(srb_cachep);
3197         fc_release_transport(qla2xxx_transport_template);
3198         fc_release_transport(qla2xxx_transport_vport_template);
3199 }
3200
3201 module_init(qla2x00_module_init);
3202 module_exit(qla2x00_module_exit);
3203
3204 MODULE_AUTHOR("QLogic Corporation");
3205 MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
3206 MODULE_LICENSE("GPL");
3207 MODULE_VERSION(QLA2XXX_VERSION);
3208 MODULE_FIRMWARE(FW_FILE_ISP21XX);
3209 MODULE_FIRMWARE(FW_FILE_ISP22XX);
3210 MODULE_FIRMWARE(FW_FILE_ISP2300);
3211 MODULE_FIRMWARE(FW_FILE_ISP2322);
3212 MODULE_FIRMWARE(FW_FILE_ISP24XX);
3213 MODULE_FIRMWARE(FW_FILE_ISP25XX);
3214 MODULE_FIRMWARE(FW_FILE_ISP81XX);