57179dabcccf33111d6b1b89e746674dd3dd5613
[firefly-linux-kernel-4.4.55.git] / drivers / scsi / qla2xxx / qla_os.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2005 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/smp_lock.h>
12 #include <linux/delay.h>
13
14 #include <scsi/scsi_tcq.h>
15 #include <scsi/scsicam.h>
16 #include <scsi/scsi_transport.h>
17 #include <scsi/scsi_transport_fc.h>
18
19 /*
20  * Driver version
21  */
22 char qla2x00_version_str[40];
23
24 /*
25  * SRB allocation cache
26  */
27 static kmem_cache_t *srb_cachep;
28
29 /*
30  * Ioctl related information.
31  */
32 static int num_hosts;
33
34 int ql2xlogintimeout = 20;
35 module_param(ql2xlogintimeout, int, S_IRUGO|S_IRUSR);
36 MODULE_PARM_DESC(ql2xlogintimeout,
37                 "Login timeout value in seconds.");
38
39 int qlport_down_retry = 30;
40 module_param(qlport_down_retry, int, S_IRUGO|S_IRUSR);
41 MODULE_PARM_DESC(qlport_down_retry,
42                 "Maximum number of command retries to a port that returns"
43                 "a PORT-DOWN status.");
44
45 int ql2xplogiabsentdevice;
46 module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
47 MODULE_PARM_DESC(ql2xplogiabsentdevice,
48                 "Option to enable PLOGI to devices that are not present after "
49                 "a Fabric scan.  This is needed for several broken switches."
50                 "Default is 0 - no PLOGI. 1 - perfom PLOGI.");
51
52 int ql2xloginretrycount = 0;
53 module_param(ql2xloginretrycount, int, S_IRUGO|S_IRUSR);
54 MODULE_PARM_DESC(ql2xloginretrycount,
55                 "Specify an alternate value for the NVRAM login retry count.");
56
57 #if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
58 int ql2xfwloadflash;
59 module_param(ql2xfwloadflash, int, S_IRUGO|S_IRUSR);
60 MODULE_PARM_DESC(ql2xfwloadflash,
61                 "Load ISP24xx firmware image from FLASH (onboard memory).");
62 #endif
63
64 static void qla2x00_free_device(scsi_qla_host_t *);
65
66 static void qla2x00_config_dma_addressing(scsi_qla_host_t *ha);
67
68 int ql2xfdmienable;
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 int ql2xprocessrscn;
75 module_param(ql2xprocessrscn, int, S_IRUGO|S_IRUSR);
76 MODULE_PARM_DESC(ql2xprocessrscn,
77                 "Option to enable port RSCN handling via a series of less"
78                 "fabric intrusive ADISCs and PLOGIs.");
79
80 /*
81  * SCSI host template entry points
82  */
83 static int qla2xxx_slave_configure(struct scsi_device * device);
84 static int qla2xxx_slave_alloc(struct scsi_device *);
85 static void qla2xxx_slave_destroy(struct scsi_device *);
86 static int qla2x00_queuecommand(struct scsi_cmnd *cmd,
87                 void (*fn)(struct scsi_cmnd *));
88 static int qla24xx_queuecommand(struct scsi_cmnd *cmd,
89                 void (*fn)(struct scsi_cmnd *));
90 static int qla2xxx_eh_abort(struct scsi_cmnd *);
91 static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
92 static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
93 static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
94 static int qla2x00_loop_reset(scsi_qla_host_t *ha);
95 static int qla2x00_device_reset(scsi_qla_host_t *, fc_port_t *);
96
97 static int qla2x00_change_queue_depth(struct scsi_device *, int);
98 static int qla2x00_change_queue_type(struct scsi_device *, int);
99
100 static struct scsi_host_template qla2x00_driver_template = {
101         .module                 = THIS_MODULE,
102         .name                   = "qla2xxx",
103         .queuecommand           = qla2x00_queuecommand,
104
105         .eh_abort_handler       = qla2xxx_eh_abort,
106         .eh_device_reset_handler = qla2xxx_eh_device_reset,
107         .eh_bus_reset_handler   = qla2xxx_eh_bus_reset,
108         .eh_host_reset_handler  = qla2xxx_eh_host_reset,
109
110         .slave_configure        = qla2xxx_slave_configure,
111
112         .slave_alloc            = qla2xxx_slave_alloc,
113         .slave_destroy          = qla2xxx_slave_destroy,
114         .change_queue_depth     = qla2x00_change_queue_depth,
115         .change_queue_type      = qla2x00_change_queue_type,
116         .this_id                = -1,
117         .cmd_per_lun            = 3,
118         .use_clustering         = ENABLE_CLUSTERING,
119         .sg_tablesize           = SG_ALL,
120
121         /*
122          * The RISC allows for each command to transfer (2^32-1) bytes of data,
123          * which equates to 0x800000 sectors.
124          */
125         .max_sectors            = 0xFFFF,
126         .shost_attrs            = qla2x00_host_attrs,
127 };
128
129 static struct scsi_host_template qla24xx_driver_template = {
130         .module                 = THIS_MODULE,
131         .name                   = "qla2xxx",
132         .queuecommand           = qla24xx_queuecommand,
133
134         .eh_abort_handler       = qla2xxx_eh_abort,
135         .eh_device_reset_handler = qla2xxx_eh_device_reset,
136         .eh_bus_reset_handler   = qla2xxx_eh_bus_reset,
137         .eh_host_reset_handler  = qla2xxx_eh_host_reset,
138
139         .slave_configure        = qla2xxx_slave_configure,
140
141         .slave_alloc            = qla2xxx_slave_alloc,
142         .slave_destroy          = qla2xxx_slave_destroy,
143         .change_queue_depth     = qla2x00_change_queue_depth,
144         .change_queue_type      = qla2x00_change_queue_type,
145         .this_id                = -1,
146         .cmd_per_lun            = 3,
147         .use_clustering         = ENABLE_CLUSTERING,
148         .sg_tablesize           = SG_ALL,
149
150         .max_sectors            = 0xFFFF,
151         .shost_attrs            = qla2x00_host_attrs,
152 };
153
154 static struct scsi_transport_template *qla2xxx_transport_template = NULL;
155
156 /* TODO Convert to inlines
157  *
158  * Timer routines
159  */
160 #define WATCH_INTERVAL          1       /* number of seconds */
161
162 static void qla2x00_timer(scsi_qla_host_t *);
163
164 static __inline__ void qla2x00_start_timer(scsi_qla_host_t *,
165     void *, unsigned long);
166 static __inline__ void qla2x00_restart_timer(scsi_qla_host_t *, unsigned long);
167 static __inline__ void qla2x00_stop_timer(scsi_qla_host_t *);
168
169 static inline void
170 qla2x00_start_timer(scsi_qla_host_t *ha, void *func, unsigned long interval)
171 {
172         init_timer(&ha->timer);
173         ha->timer.expires = jiffies + interval * HZ;
174         ha->timer.data = (unsigned long)ha;
175         ha->timer.function = (void (*)(unsigned long))func;
176         add_timer(&ha->timer);
177         ha->timer_active = 1;
178 }
179
180 static inline void
181 qla2x00_restart_timer(scsi_qla_host_t *ha, unsigned long interval)
182 {
183         mod_timer(&ha->timer, jiffies + interval * HZ);
184 }
185
186 static __inline__ void
187 qla2x00_stop_timer(scsi_qla_host_t *ha)
188 {
189         del_timer_sync(&ha->timer);
190         ha->timer_active = 0;
191 }
192
193 static int qla2x00_do_dpc(void *data);
194
195 static void qla2x00_rst_aen(scsi_qla_host_t *);
196
197 static uint8_t qla2x00_mem_alloc(scsi_qla_host_t *);
198 static void qla2x00_mem_free(scsi_qla_host_t *ha);
199 static int qla2x00_allocate_sp_pool( scsi_qla_host_t *ha);
200 static void qla2x00_free_sp_pool(scsi_qla_host_t *ha);
201 static void qla2x00_sp_free_dma(scsi_qla_host_t *, srb_t *);
202 void qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *);
203
204 /* -------------------------------------------------------------------------- */
205
206 static char *
207 qla2x00_pci_info_str(struct scsi_qla_host *ha, char *str)
208 {
209         static char *pci_bus_modes[] = {
210                 "33", "66", "100", "133",
211         };
212         uint16_t pci_bus;
213
214         strcpy(str, "PCI");
215         pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
216         if (pci_bus) {
217                 strcat(str, "-X (");
218                 strcat(str, pci_bus_modes[pci_bus]);
219         } else {
220                 pci_bus = (ha->pci_attr & BIT_8) >> 8;
221                 strcat(str, " (");
222                 strcat(str, pci_bus_modes[pci_bus]);
223         }
224         strcat(str, " MHz)");
225
226         return (str);
227 }
228
229 static char *
230 qla24xx_pci_info_str(struct scsi_qla_host *ha, char *str)
231 {
232         static char *pci_bus_modes[] = { "33", "66", "100", "133", };
233         uint32_t pci_bus;
234         int pcie_reg;
235
236         pcie_reg = pci_find_capability(ha->pdev, PCI_CAP_ID_EXP);
237         if (pcie_reg) {
238                 char lwstr[6];
239                 uint16_t pcie_lstat, lspeed, lwidth;
240
241                 pcie_reg += 0x12;
242                 pci_read_config_word(ha->pdev, pcie_reg, &pcie_lstat);
243                 lspeed = pcie_lstat & (BIT_0 | BIT_1 | BIT_2 | BIT_3);
244                 lwidth = (pcie_lstat &
245                     (BIT_4 | BIT_5 | BIT_6 | BIT_7 | BIT_8 | BIT_9)) >> 4;
246
247                 strcpy(str, "PCIe (");
248                 if (lspeed == 1)
249                         strcat(str, "2.5Gb/s ");
250                 else
251                         strcat(str, "<unknown> ");
252                 snprintf(lwstr, sizeof(lwstr), "x%d)", lwidth);
253                 strcat(str, lwstr);
254
255                 return str;
256         }
257
258         strcpy(str, "PCI");
259         pci_bus = (ha->pci_attr & CSRX_PCIX_BUS_MODE_MASK) >> 8;
260         if (pci_bus == 0 || pci_bus == 8) {
261                 strcat(str, " (");
262                 strcat(str, pci_bus_modes[pci_bus >> 3]);
263         } else {
264                 strcat(str, "-X ");
265                 if (pci_bus & BIT_2)
266                         strcat(str, "Mode 2");
267                 else
268                         strcat(str, "Mode 1");
269                 strcat(str, " (");
270                 strcat(str, pci_bus_modes[pci_bus & ~BIT_2]);
271         }
272         strcat(str, " MHz)");
273
274         return str;
275 }
276
277 char *
278 qla2x00_fw_version_str(struct scsi_qla_host *ha, char *str)
279 {
280         char un_str[10];
281
282         sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
283             ha->fw_minor_version,
284             ha->fw_subminor_version);
285
286         if (ha->fw_attributes & BIT_9) {
287                 strcat(str, "FLX");
288                 return (str);
289         }
290
291         switch (ha->fw_attributes & 0xFF) {
292         case 0x7:
293                 strcat(str, "EF");
294                 break;
295         case 0x17:
296                 strcat(str, "TP");
297                 break;
298         case 0x37:
299                 strcat(str, "IP");
300                 break;
301         case 0x77:
302                 strcat(str, "VI");
303                 break;
304         default:
305                 sprintf(un_str, "(%x)", ha->fw_attributes);
306                 strcat(str, un_str);
307                 break;
308         }
309         if (ha->fw_attributes & 0x100)
310                 strcat(str, "X");
311
312         return (str);
313 }
314
315 char *
316 qla24xx_fw_version_str(struct scsi_qla_host *ha, char *str)
317 {
318         sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
319             ha->fw_minor_version,
320             ha->fw_subminor_version);
321
322         if (ha->fw_attributes & BIT_0)
323                 strcat(str, "[Class 2] ");
324         if (ha->fw_attributes & BIT_1)
325                 strcat(str, "[IP] ");
326         if (ha->fw_attributes & BIT_2)
327                 strcat(str, "[Multi-ID] ");
328         if (ha->fw_attributes & BIT_13)
329                 strcat(str, "[Experimental]");
330         return str;
331 }
332
333 static inline srb_t *
334 qla2x00_get_new_sp(scsi_qla_host_t *ha, fc_port_t *fcport,
335     struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
336 {
337         srb_t *sp;
338
339         sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
340         if (!sp)
341                 return sp;
342
343         atomic_set(&sp->ref_count, 1);
344         sp->ha = ha;
345         sp->fcport = fcport;
346         sp->cmd = cmd;
347         sp->flags = 0;
348         CMD_SP(cmd) = (void *)sp;
349         cmd->scsi_done = done;
350
351         return sp;
352 }
353
354 static int
355 qla2x00_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
356 {
357         scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
358         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
359         struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
360         srb_t *sp;
361         int rval;
362
363         rval = fc_remote_port_chkready(rport);
364         if (rval) {
365                 cmd->result = rval;
366                 goto qc_fail_command;
367         }
368
369         if (atomic_read(&fcport->state) != FCS_ONLINE) {
370                 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
371                     atomic_read(&ha->loop_state) == LOOP_DEAD) {
372                         cmd->result = DID_NO_CONNECT << 16;
373                         goto qc_fail_command;
374                 }
375                 goto qc_host_busy;
376         }
377
378         spin_unlock_irq(ha->host->host_lock);
379
380         sp = qla2x00_get_new_sp(ha, fcport, cmd, done);
381         if (!sp)
382                 goto qc_host_busy_lock;
383
384         rval = qla2x00_start_scsi(sp);
385         if (rval != QLA_SUCCESS)
386                 goto qc_host_busy_free_sp;
387
388         spin_lock_irq(ha->host->host_lock);
389
390         return 0;
391
392 qc_host_busy_free_sp:
393         qla2x00_sp_free_dma(ha, sp);
394         mempool_free(sp, ha->srb_mempool);
395
396 qc_host_busy_lock:
397         spin_lock_irq(ha->host->host_lock);
398
399 qc_host_busy:
400         return SCSI_MLQUEUE_HOST_BUSY;
401
402 qc_fail_command:
403         done(cmd);
404
405         return 0;
406 }
407
408
409 static int
410 qla24xx_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
411 {
412         scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
413         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
414         struct fc_rport *rport = starget_to_rport(scsi_target(cmd->device));
415         srb_t *sp;
416         int rval;
417
418         rval = fc_remote_port_chkready(rport);
419         if (rval) {
420                 cmd->result = rval;
421                 goto qc24_fail_command;
422         }
423
424         if (atomic_read(&fcport->state) != FCS_ONLINE) {
425                 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
426                     atomic_read(&ha->loop_state) == LOOP_DEAD) {
427                         cmd->result = DID_NO_CONNECT << 16;
428                         goto qc24_fail_command;
429                 }
430                 goto qc24_host_busy;
431         }
432
433         spin_unlock_irq(ha->host->host_lock);
434
435         sp = qla2x00_get_new_sp(ha, fcport, cmd, done);
436         if (!sp)
437                 goto qc24_host_busy_lock;
438
439         rval = qla24xx_start_scsi(sp);
440         if (rval != QLA_SUCCESS)
441                 goto qc24_host_busy_free_sp;
442
443         spin_lock_irq(ha->host->host_lock);
444
445         return 0;
446
447 qc24_host_busy_free_sp:
448         qla2x00_sp_free_dma(ha, sp);
449         mempool_free(sp, ha->srb_mempool);
450
451 qc24_host_busy_lock:
452         spin_lock_irq(ha->host->host_lock);
453
454 qc24_host_busy:
455         return SCSI_MLQUEUE_HOST_BUSY;
456
457 qc24_fail_command:
458         done(cmd);
459
460         return 0;
461 }
462
463
464 /*
465  * qla2x00_eh_wait_on_command
466  *    Waits for the command to be returned by the Firmware for some
467  *    max time.
468  *
469  * Input:
470  *    ha = actual ha whose done queue will contain the command
471  *            returned by firmware.
472  *    cmd = Scsi Command to wait on.
473  *    flag = Abort/Reset(Bus or Device Reset)
474  *
475  * Return:
476  *    Not Found : 0
477  *    Found : 1
478  */
479 static int
480 qla2x00_eh_wait_on_command(scsi_qla_host_t *ha, struct scsi_cmnd *cmd)
481 {
482 #define ABORT_POLLING_PERIOD    1000
483 #define ABORT_WAIT_ITER         ((10 * 1000) / (ABORT_POLLING_PERIOD))
484         unsigned long wait_iter = ABORT_WAIT_ITER;
485         int ret = QLA_SUCCESS;
486
487         while (CMD_SP(cmd)) {
488                 msleep(ABORT_POLLING_PERIOD);
489
490                 if (--wait_iter)
491                         break;
492         }
493         if (CMD_SP(cmd))
494                 ret = QLA_FUNCTION_FAILED;
495
496         return ret;
497 }
498
499 /*
500  * qla2x00_wait_for_hba_online
501  *    Wait till the HBA is online after going through
502  *    <= MAX_RETRIES_OF_ISP_ABORT  or
503  *    finally HBA is disabled ie marked offline
504  *
505  * Input:
506  *     ha - pointer to host adapter structure
507  *
508  * Note:
509  *    Does context switching-Release SPIN_LOCK
510  *    (if any) before calling this routine.
511  *
512  * Return:
513  *    Success (Adapter is online) : 0
514  *    Failed  (Adapter is offline/disabled) : 1
515  */
516 static int
517 qla2x00_wait_for_hba_online(scsi_qla_host_t *ha)
518 {
519         int             return_status;
520         unsigned long   wait_online;
521
522         wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
523         while (((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) ||
524             test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
525             test_bit(ISP_ABORT_RETRY, &ha->dpc_flags) ||
526             ha->dpc_active) && time_before(jiffies, wait_online)) {
527
528                 msleep(1000);
529         }
530         if (ha->flags.online)
531                 return_status = QLA_SUCCESS;
532         else
533                 return_status = QLA_FUNCTION_FAILED;
534
535         DEBUG2(printk("%s return_status=%d\n",__func__,return_status));
536
537         return (return_status);
538 }
539
540 /*
541  * qla2x00_wait_for_loop_ready
542  *    Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
543  *    to be in LOOP_READY state.
544  * Input:
545  *     ha - pointer to host adapter structure
546  *
547  * Note:
548  *    Does context switching-Release SPIN_LOCK
549  *    (if any) before calling this routine.
550  *
551  *
552  * Return:
553  *    Success (LOOP_READY) : 0
554  *    Failed  (LOOP_NOT_READY) : 1
555  */
556 static inline int
557 qla2x00_wait_for_loop_ready(scsi_qla_host_t *ha)
558 {
559         int      return_status = QLA_SUCCESS;
560         unsigned long loop_timeout ;
561
562         /* wait for 5 min at the max for loop to be ready */
563         loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ);
564
565         while ((!atomic_read(&ha->loop_down_timer) &&
566             atomic_read(&ha->loop_state) == LOOP_DOWN) ||
567             atomic_read(&ha->loop_state) != LOOP_READY) {
568                 msleep(1000);
569                 if (time_after_eq(jiffies, loop_timeout)) {
570                         return_status = QLA_FUNCTION_FAILED;
571                         break;
572                 }
573         }
574         return (return_status);
575 }
576
577 /**************************************************************************
578 * qla2xxx_eh_abort
579 *
580 * Description:
581 *    The abort function will abort the specified command.
582 *
583 * Input:
584 *    cmd = Linux SCSI command packet to be aborted.
585 *
586 * Returns:
587 *    Either SUCCESS or FAILED.
588 *
589 * Note:
590 **************************************************************************/
591 int
592 qla2xxx_eh_abort(struct scsi_cmnd *cmd)
593 {
594         scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
595         srb_t *sp;
596         int ret, i;
597         unsigned int id, lun;
598         unsigned long serial;
599         unsigned long flags;
600
601         if (!CMD_SP(cmd))
602                 return FAILED;
603
604         ret = FAILED;
605
606         id = cmd->device->id;
607         lun = cmd->device->lun;
608         serial = cmd->serial_number;
609
610         /* Check active list for command command. */
611         spin_lock_irqsave(&ha->hardware_lock, flags);
612         for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
613                 sp = ha->outstanding_cmds[i];
614
615                 if (sp == NULL)
616                         continue;
617
618                 if (sp->cmd != cmd)
619                         continue;
620
621                 DEBUG2(printk("%s(%ld): aborting sp %p from RISC. pid=%ld "
622                     "sp->state=%x\n", __func__, ha->host_no, sp, serial,
623                     sp->state));
624                 DEBUG3(qla2x00_print_scsi_cmd(cmd);)
625
626                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
627                 if (ha->isp_ops.abort_command(ha, sp)) {
628                         DEBUG2(printk("%s(%ld): abort_command "
629                             "mbx failed.\n", __func__, ha->host_no));
630                 } else {
631                         DEBUG3(printk("%s(%ld): abort_command "
632                             "mbx success.\n", __func__, ha->host_no));
633                         ret = SUCCESS;
634                 }
635                 spin_lock_irqsave(&ha->hardware_lock, flags);
636
637                 break;
638         }
639         spin_unlock_irqrestore(&ha->hardware_lock, flags);
640
641         /* Wait for the command to be returned. */
642         if (ret == SUCCESS) {
643                 if (qla2x00_eh_wait_on_command(ha, cmd) != QLA_SUCCESS) {
644                         qla_printk(KERN_ERR, ha,
645                             "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
646                             "%x.\n", ha->host_no, id, lun, serial, ret);
647                 }
648         }
649
650         qla_printk(KERN_INFO, ha,
651             "scsi(%ld:%d:%d): Abort command issued -- %lx %x.\n", ha->host_no,
652             id, lun, serial, ret);
653
654         return ret;
655 }
656
657 /**************************************************************************
658 * qla2x00_eh_wait_for_pending_target_commands
659 *
660 * Description:
661 *    Waits for all the commands to come back from the specified target.
662 *
663 * Input:
664 *    ha - pointer to scsi_qla_host structure.
665 *    t  - target
666 * Returns:
667 *    Either SUCCESS or FAILED.
668 *
669 * Note:
670 **************************************************************************/
671 static int
672 qla2x00_eh_wait_for_pending_target_commands(scsi_qla_host_t *ha, unsigned int t)
673 {
674         int     cnt;
675         int     status;
676         srb_t           *sp;
677         struct scsi_cmnd *cmd;
678         unsigned long flags;
679
680         status = 0;
681
682         /*
683          * Waiting for all commands for the designated target in the active
684          * array
685          */
686         for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
687                 spin_lock_irqsave(&ha->hardware_lock, flags);
688                 sp = ha->outstanding_cmds[cnt];
689                 if (sp) {
690                         cmd = sp->cmd;
691                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
692                         if (cmd->device->id == t) {
693                                 if (!qla2x00_eh_wait_on_command(ha, cmd)) {
694                                         status = 1;
695                                         break;
696                                 }
697                         }
698                 } else {
699                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
700                 }
701         }
702         return (status);
703 }
704
705
706 /**************************************************************************
707 * qla2xxx_eh_device_reset
708 *
709 * Description:
710 *    The device reset function will reset the target and abort any
711 *    executing commands.
712 *
713 *    NOTE: The use of SP is undefined within this context.  Do *NOT*
714 *          attempt to use this value, even if you determine it is
715 *          non-null.
716 *
717 * Input:
718 *    cmd = Linux SCSI command packet of the command that cause the
719 *          bus device reset.
720 *
721 * Returns:
722 *    SUCCESS/FAILURE (defined as macro in scsi.h).
723 *
724 **************************************************************************/
725 int
726 qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
727 {
728         scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
729         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
730         srb_t *sp;
731         int ret;
732         unsigned int id, lun;
733         unsigned long serial;
734
735         ret = FAILED;
736
737         id = cmd->device->id;
738         lun = cmd->device->lun;
739         serial = cmd->serial_number;
740
741         sp = (srb_t *) CMD_SP(cmd);
742         if (!sp || !fcport)
743                 return ret;
744
745         qla_printk(KERN_INFO, ha,
746             "scsi(%ld:%d:%d): DEVICE RESET ISSUED.\n", ha->host_no, id, lun);
747
748         if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
749                 goto eh_dev_reset_done;
750
751         if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
752                 if (qla2x00_device_reset(ha, fcport) == 0)
753                         ret = SUCCESS;
754
755 #if defined(LOGOUT_AFTER_DEVICE_RESET)
756                 if (ret == SUCCESS) {
757                         if (fcport->flags & FC_FABRIC_DEVICE) {
758                                 ha->isp_ops.fabric_logout(ha, fcport->loop_id);
759                                 qla2x00_mark_device_lost(ha, fcport, 0, 0);
760                         }
761                 }
762 #endif
763         } else {
764                 DEBUG2(printk(KERN_INFO
765                     "%s failed: loop not ready\n",__func__);)
766         }
767
768         if (ret == FAILED) {
769                 DEBUG3(printk("%s(%ld): device reset failed\n",
770                     __func__, ha->host_no));
771                 qla_printk(KERN_INFO, ha, "%s: device reset failed\n",
772                     __func__);
773
774                 goto eh_dev_reset_done;
775         }
776
777         /* Flush outstanding commands. */
778         if (qla2x00_eh_wait_for_pending_target_commands(ha, id))
779                 ret = FAILED;
780         if (ret == FAILED) {
781                 DEBUG3(printk("%s(%ld): failed while waiting for commands\n",
782                     __func__, ha->host_no));
783                 qla_printk(KERN_INFO, ha,
784                     "%s: failed while waiting for commands\n", __func__);
785         } else
786                 qla_printk(KERN_INFO, ha,
787                     "scsi(%ld:%d:%d): DEVICE RESET SUCCEEDED.\n", ha->host_no,
788                     id, lun);
789  eh_dev_reset_done:
790         return ret;
791 }
792
793 /**************************************************************************
794 * qla2x00_eh_wait_for_pending_commands
795 *
796 * Description:
797 *    Waits for all the commands to come back from the specified host.
798 *
799 * Input:
800 *    ha - pointer to scsi_qla_host structure.
801 *
802 * Returns:
803 *    1 : SUCCESS
804 *    0 : FAILED
805 *
806 * Note:
807 **************************************************************************/
808 static int
809 qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *ha)
810 {
811         int     cnt;
812         int     status;
813         srb_t           *sp;
814         struct scsi_cmnd *cmd;
815         unsigned long flags;
816
817         status = 1;
818
819         /*
820          * Waiting for all commands for the designated target in the active
821          * array
822          */
823         for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
824                 spin_lock_irqsave(&ha->hardware_lock, flags);
825                 sp = ha->outstanding_cmds[cnt];
826                 if (sp) {
827                         cmd = sp->cmd;
828                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
829                         status = qla2x00_eh_wait_on_command(ha, cmd);
830                         if (status == 0)
831                                 break;
832                 }
833                 else {
834                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
835                 }
836         }
837         return (status);
838 }
839
840
841 /**************************************************************************
842 * qla2xxx_eh_bus_reset
843 *
844 * Description:
845 *    The bus reset function will reset the bus and abort any executing
846 *    commands.
847 *
848 * Input:
849 *    cmd = Linux SCSI command packet of the command that cause the
850 *          bus reset.
851 *
852 * Returns:
853 *    SUCCESS/FAILURE (defined as macro in scsi.h).
854 *
855 **************************************************************************/
856 int
857 qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
858 {
859         scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
860         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
861         srb_t *sp;
862         int ret;
863         unsigned int id, lun;
864         unsigned long serial;
865
866         ret = FAILED;
867
868         id = cmd->device->id;
869         lun = cmd->device->lun;
870         serial = cmd->serial_number;
871
872         sp = (srb_t *) CMD_SP(cmd);
873         if (!sp || !fcport)
874                 return ret;
875
876         qla_printk(KERN_INFO, ha,
877             "scsi(%ld:%d:%d): LOOP RESET ISSUED.\n", ha->host_no, id, lun);
878
879         if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
880                 DEBUG2(printk("%s failed:board disabled\n",__func__));
881                 goto eh_bus_reset_done;
882         }
883
884         if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
885                 if (qla2x00_loop_reset(ha) == QLA_SUCCESS)
886                         ret = SUCCESS;
887         }
888         if (ret == FAILED)
889                 goto eh_bus_reset_done;
890
891         /* Flush outstanding commands. */
892         if (!qla2x00_eh_wait_for_pending_commands(ha))
893                 ret = FAILED;
894
895 eh_bus_reset_done:
896         qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
897             (ret == FAILED) ? "failed" : "succeded");
898
899         return ret;
900 }
901
902 /**************************************************************************
903 * qla2xxx_eh_host_reset
904 *
905 * Description:
906 *    The reset function will reset the Adapter.
907 *
908 * Input:
909 *      cmd = Linux SCSI command packet of the command that cause the
910 *            adapter reset.
911 *
912 * Returns:
913 *      Either SUCCESS or FAILED.
914 *
915 * Note:
916 **************************************************************************/
917 int
918 qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
919 {
920         scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
921         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
922         srb_t *sp;
923         int ret;
924         unsigned int id, lun;
925         unsigned long serial;
926
927         ret = FAILED;
928
929         id = cmd->device->id;
930         lun = cmd->device->lun;
931         serial = cmd->serial_number;
932
933         sp = (srb_t *) CMD_SP(cmd);
934         if (!sp || !fcport)
935                 return ret;
936
937         qla_printk(KERN_INFO, ha,
938             "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no, id, lun);
939
940         if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
941                 goto eh_host_reset_lock;
942
943         /*
944          * Fixme-may be dpc thread is active and processing
945          * loop_resync,so wait a while for it to
946          * be completed and then issue big hammer.Otherwise
947          * it may cause I/O failure as big hammer marks the
948          * devices as lost kicking of the port_down_timer
949          * while dpc is stuck for the mailbox to complete.
950          */
951         qla2x00_wait_for_loop_ready(ha);
952         set_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
953         if (qla2x00_abort_isp(ha)) {
954                 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
955                 /* failed. schedule dpc to try */
956                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
957
958                 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
959                         goto eh_host_reset_lock;
960         }
961         clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
962
963         /* Waiting for our command in done_queue to be returned to OS.*/
964         if (qla2x00_eh_wait_for_pending_commands(ha))
965                 ret = SUCCESS;
966
967 eh_host_reset_lock:
968         qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
969             (ret == FAILED) ? "failed" : "succeded");
970
971         return ret;
972 }
973
974 /*
975 * qla2x00_loop_reset
976 *      Issue loop reset.
977 *
978 * Input:
979 *      ha = adapter block pointer.
980 *
981 * Returns:
982 *      0 = success
983 */
984 static int
985 qla2x00_loop_reset(scsi_qla_host_t *ha)
986 {
987         int status = QLA_SUCCESS;
988         struct fc_port *fcport;
989
990         if (ha->flags.enable_lip_reset) {
991                 status = qla2x00_lip_reset(ha);
992         }
993
994         if (status == QLA_SUCCESS && ha->flags.enable_target_reset) {
995                 list_for_each_entry(fcport, &ha->fcports, list) {
996                         if (fcport->port_type != FCT_TARGET)
997                                 continue;
998
999                         status = qla2x00_device_reset(ha, fcport);
1000                         if (status != QLA_SUCCESS)
1001                                 break;
1002                 }
1003         }
1004
1005         if (status == QLA_SUCCESS &&
1006                 ((!ha->flags.enable_target_reset &&
1007                   !ha->flags.enable_lip_reset) ||
1008                 ha->flags.enable_lip_full_login)) {
1009
1010                 status = qla2x00_full_login_lip(ha);
1011         }
1012
1013         /* Issue marker command only when we are going to start the I/O */
1014         ha->marker_needed = 1;
1015
1016         if (status) {
1017                 /* Empty */
1018                 DEBUG2_3(printk("%s(%ld): **** FAILED ****\n",
1019                                 __func__,
1020                                 ha->host_no);)
1021         } else {
1022                 /* Empty */
1023                 DEBUG3(printk("%s(%ld): exiting normally.\n",
1024                                 __func__,
1025                                 ha->host_no);)
1026         }
1027
1028         return(status);
1029 }
1030
1031 /*
1032  * qla2x00_device_reset
1033  *      Issue bus device reset message to the target.
1034  *
1035  * Input:
1036  *      ha = adapter block pointer.
1037  *      t = SCSI ID.
1038  *      TARGET_QUEUE_LOCK must be released.
1039  *      ADAPTER_STATE_LOCK must be released.
1040  *
1041  * Context:
1042  *      Kernel context.
1043  */
1044 static int
1045 qla2x00_device_reset(scsi_qla_host_t *ha, fc_port_t *reset_fcport)
1046 {
1047         /* Abort Target command will clear Reservation */
1048         return ha->isp_ops.abort_target(reset_fcport);
1049 }
1050
1051 static int
1052 qla2xxx_slave_alloc(struct scsi_device *sdev)
1053 {
1054         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1055
1056         if (!rport || fc_remote_port_chkready(rport))
1057                 return -ENXIO;
1058
1059         sdev->hostdata = *(fc_port_t **)rport->dd_data;
1060
1061         return 0;
1062 }
1063
1064 static int
1065 qla2xxx_slave_configure(struct scsi_device *sdev)
1066 {
1067         scsi_qla_host_t *ha = to_qla_host(sdev->host);
1068         struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
1069
1070         if (sdev->tagged_supported)
1071                 scsi_activate_tcq(sdev, 32);
1072         else
1073                 scsi_deactivate_tcq(sdev, 32);
1074
1075         rport->dev_loss_tmo = ha->port_down_retry_count + 5;
1076
1077         return 0;
1078 }
1079
1080 static void
1081 qla2xxx_slave_destroy(struct scsi_device *sdev)
1082 {
1083         sdev->hostdata = NULL;
1084 }
1085
1086 static int
1087 qla2x00_change_queue_depth(struct scsi_device *sdev, int qdepth)
1088 {
1089         scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1090         return sdev->queue_depth;
1091 }
1092
1093 static int
1094 qla2x00_change_queue_type(struct scsi_device *sdev, int tag_type)
1095 {
1096         if (sdev->tagged_supported) {
1097                 scsi_set_tag_type(sdev, tag_type);
1098                 if (tag_type)
1099                         scsi_activate_tcq(sdev, sdev->queue_depth);
1100                 else
1101                         scsi_deactivate_tcq(sdev, sdev->queue_depth);
1102         } else
1103                 tag_type = 0;
1104
1105         return tag_type;
1106 }
1107
1108 /**
1109  * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
1110  * @ha: HA context
1111  *
1112  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1113  * supported addressing method.
1114  */
1115 static void
1116 qla2x00_config_dma_addressing(scsi_qla_host_t *ha)
1117 {
1118         /* Assume a 32bit DMA mask. */
1119         ha->flags.enable_64bit_addressing = 0;
1120
1121         if (!dma_set_mask(&ha->pdev->dev, DMA_64BIT_MASK)) {
1122                 /* Any upper-dword bits set? */
1123                 if (MSD(dma_get_required_mask(&ha->pdev->dev)) &&
1124                     !pci_set_consistent_dma_mask(ha->pdev, DMA_64BIT_MASK)) {
1125                         /* Ok, a 64bit DMA mask is applicable. */
1126                         ha->flags.enable_64bit_addressing = 1;
1127                         ha->isp_ops.calc_req_entries = qla2x00_calc_iocbs_64;
1128                         ha->isp_ops.build_iocbs = qla2x00_build_scsi_iocbs_64;
1129                         return;
1130                 }
1131         }
1132
1133         dma_set_mask(&ha->pdev->dev, DMA_32BIT_MASK);
1134         pci_set_consistent_dma_mask(ha->pdev, DMA_32BIT_MASK);
1135 }
1136
1137 static int
1138 qla2x00_iospace_config(scsi_qla_host_t *ha)
1139 {
1140         unsigned long   pio, pio_len, pio_flags;
1141         unsigned long   mmio, mmio_len, mmio_flags;
1142
1143         /* We only need PIO for Flash operations on ISP2312 v2 chips. */
1144         pio = pci_resource_start(ha->pdev, 0);
1145         pio_len = pci_resource_len(ha->pdev, 0);
1146         pio_flags = pci_resource_flags(ha->pdev, 0);
1147         if (pio_flags & IORESOURCE_IO) {
1148                 if (pio_len < MIN_IOBASE_LEN) {
1149                         qla_printk(KERN_WARNING, ha,
1150                             "Invalid PCI I/O region size (%s)...\n",
1151                                 pci_name(ha->pdev));
1152                         pio = 0;
1153                 }
1154         } else {
1155                 qla_printk(KERN_WARNING, ha,
1156                     "region #0 not a PIO resource (%s)...\n",
1157                     pci_name(ha->pdev));
1158                 pio = 0;
1159         }
1160
1161         /* Use MMIO operations for all accesses. */
1162         mmio = pci_resource_start(ha->pdev, 1);
1163         mmio_len = pci_resource_len(ha->pdev, 1);
1164         mmio_flags = pci_resource_flags(ha->pdev, 1);
1165
1166         if (!(mmio_flags & IORESOURCE_MEM)) {
1167                 qla_printk(KERN_ERR, ha,
1168                     "region #0 not an MMIO resource (%s), aborting\n",
1169                     pci_name(ha->pdev));
1170                 goto iospace_error_exit;
1171         }
1172         if (mmio_len < MIN_IOBASE_LEN) {
1173                 qla_printk(KERN_ERR, ha,
1174                     "Invalid PCI mem region size (%s), aborting\n",
1175                         pci_name(ha->pdev));
1176                 goto iospace_error_exit;
1177         }
1178
1179         if (pci_request_regions(ha->pdev, ha->brd_info->drv_name)) {
1180                 qla_printk(KERN_WARNING, ha,
1181                     "Failed to reserve PIO/MMIO regions (%s)\n",
1182                     pci_name(ha->pdev));
1183
1184                 goto iospace_error_exit;
1185         }
1186
1187         ha->pio_address = pio;
1188         ha->pio_length = pio_len;
1189         ha->iobase = ioremap(mmio, MIN_IOBASE_LEN);
1190         if (!ha->iobase) {
1191                 qla_printk(KERN_ERR, ha,
1192                     "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
1193
1194                 goto iospace_error_exit;
1195         }
1196
1197         return (0);
1198
1199 iospace_error_exit:
1200         return (-ENOMEM);
1201 }
1202
1203 static void
1204 qla2x00_enable_intrs(scsi_qla_host_t *ha)
1205 {
1206         unsigned long flags = 0;
1207         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1208
1209         spin_lock_irqsave(&ha->hardware_lock, flags);
1210         ha->interrupts_on = 1;
1211         /* enable risc and host interrupts */
1212         WRT_REG_WORD(&reg->ictrl, ICR_EN_INT | ICR_EN_RISC);
1213         RD_REG_WORD(&reg->ictrl);
1214         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1215
1216 }
1217
1218 static void
1219 qla2x00_disable_intrs(scsi_qla_host_t *ha)
1220 {
1221         unsigned long flags = 0;
1222         struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
1223
1224         spin_lock_irqsave(&ha->hardware_lock, flags);
1225         ha->interrupts_on = 0;
1226         /* disable risc and host interrupts */
1227         WRT_REG_WORD(&reg->ictrl, 0);
1228         RD_REG_WORD(&reg->ictrl);
1229         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1230 }
1231
1232 static void
1233 qla24xx_enable_intrs(scsi_qla_host_t *ha)
1234 {
1235         unsigned long flags = 0;
1236         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1237
1238         spin_lock_irqsave(&ha->hardware_lock, flags);
1239         ha->interrupts_on = 1;
1240         WRT_REG_DWORD(&reg->ictrl, ICRX_EN_RISC_INT);
1241         RD_REG_DWORD(&reg->ictrl);
1242         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1243 }
1244
1245 static void
1246 qla24xx_disable_intrs(scsi_qla_host_t *ha)
1247 {
1248         unsigned long flags = 0;
1249         struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
1250
1251         spin_lock_irqsave(&ha->hardware_lock, flags);
1252         ha->interrupts_on = 0;
1253         WRT_REG_DWORD(&reg->ictrl, 0);
1254         RD_REG_DWORD(&reg->ictrl);
1255         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1256 }
1257
1258 /*
1259  * PCI driver interface
1260  */
1261 int qla2x00_probe_one(struct pci_dev *pdev, struct qla_board_info *brd_info)
1262 {
1263         int     ret = -ENODEV;
1264         device_reg_t __iomem *reg;
1265         struct Scsi_Host *host;
1266         scsi_qla_host_t *ha;
1267         unsigned long   flags = 0;
1268         unsigned long   wait_switch = 0;
1269         char pci_info[20];
1270         char fw_str[30];
1271         fc_port_t *fcport;
1272         struct scsi_host_template *sht;
1273
1274         if (pci_enable_device(pdev))
1275                 goto probe_out;
1276
1277         sht = &qla2x00_driver_template;
1278         if (pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2422 ||
1279             pdev->device == PCI_DEVICE_ID_QLOGIC_ISP2432)
1280                 sht = &qla24xx_driver_template;
1281         host = scsi_host_alloc(sht, sizeof(scsi_qla_host_t));
1282         if (host == NULL) {
1283                 printk(KERN_WARNING
1284                     "qla2xxx: Couldn't allocate host from scsi layer!\n");
1285                 goto probe_disable_device;
1286         }
1287
1288         /* Clear our data area */
1289         ha = (scsi_qla_host_t *)host->hostdata;
1290         memset(ha, 0, sizeof(scsi_qla_host_t));
1291
1292         ha->pdev = pdev;
1293         ha->host = host;
1294         ha->host_no = host->host_no;
1295         ha->brd_info = brd_info;
1296         sprintf(ha->host_str, "%s_%ld", ha->brd_info->drv_name, ha->host_no);
1297
1298         ha->dpc_pid = -1;
1299
1300         /* Configure PCI I/O space */
1301         ret = qla2x00_iospace_config(ha);
1302         if (ret)
1303                 goto probe_failed;
1304
1305         qla_printk(KERN_INFO, ha,
1306             "Found an ISP%04X, irq %d, iobase 0x%p\n", pdev->device, pdev->irq,
1307             ha->iobase);
1308
1309         spin_lock_init(&ha->hardware_lock);
1310
1311         ha->prev_topology = 0;
1312         ha->ports = MAX_BUSES;
1313         ha->init_cb_size = sizeof(init_cb_t);
1314         ha->mgmt_svr_loop_id = MANAGEMENT_SERVER;
1315         ha->link_data_rate = LDR_UNKNOWN;
1316
1317         /* Assign ISP specific operations. */
1318         ha->isp_ops.pci_config          = qla2100_pci_config;
1319         ha->isp_ops.reset_chip          = qla2x00_reset_chip;
1320         ha->isp_ops.chip_diag           = qla2x00_chip_diag;
1321         ha->isp_ops.config_rings        = qla2x00_config_rings;
1322         ha->isp_ops.reset_adapter       = qla2x00_reset_adapter;
1323         ha->isp_ops.nvram_config        = qla2x00_nvram_config;
1324         ha->isp_ops.update_fw_options   = qla2x00_update_fw_options;
1325         ha->isp_ops.load_risc           = qla2x00_load_risc;
1326         ha->isp_ops.pci_info_str        = qla2x00_pci_info_str;
1327         ha->isp_ops.fw_version_str      = qla2x00_fw_version_str;
1328         ha->isp_ops.intr_handler        = qla2100_intr_handler;
1329         ha->isp_ops.enable_intrs        = qla2x00_enable_intrs;
1330         ha->isp_ops.disable_intrs       = qla2x00_disable_intrs;
1331         ha->isp_ops.abort_command       = qla2x00_abort_command;
1332         ha->isp_ops.abort_target        = qla2x00_abort_target;
1333         ha->isp_ops.fabric_login        = qla2x00_login_fabric;
1334         ha->isp_ops.fabric_logout       = qla2x00_fabric_logout;
1335         ha->isp_ops.calc_req_entries    = qla2x00_calc_iocbs_32;
1336         ha->isp_ops.build_iocbs         = qla2x00_build_scsi_iocbs_32;
1337         ha->isp_ops.prep_ms_iocb        = qla2x00_prep_ms_iocb;
1338         ha->isp_ops.prep_ms_fdmi_iocb   = qla2x00_prep_ms_fdmi_iocb;
1339         ha->isp_ops.read_nvram          = qla2x00_read_nvram_data;
1340         ha->isp_ops.write_nvram         = qla2x00_write_nvram_data;
1341         ha->isp_ops.fw_dump             = qla2100_fw_dump;
1342         ha->isp_ops.ascii_fw_dump       = qla2100_ascii_fw_dump;
1343         if (IS_QLA2100(ha)) {
1344                 host->max_id = MAX_TARGETS_2100;
1345                 ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
1346                 ha->request_q_length = REQUEST_ENTRY_CNT_2100;
1347                 ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1348                 ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
1349                 host->sg_tablesize = 32;
1350                 ha->gid_list_info_size = 4;
1351         } else if (IS_QLA2200(ha)) {
1352                 host->max_id = MAX_TARGETS_2200;
1353                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1354                 ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1355                 ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1356                 ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
1357                 ha->gid_list_info_size = 4;
1358         } else if (IS_QLA23XX(ha)) {
1359                 host->max_id = MAX_TARGETS_2200;
1360                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1361                 ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1362                 ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
1363                 ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
1364                 ha->isp_ops.pci_config = qla2300_pci_config;
1365                 ha->isp_ops.intr_handler = qla2300_intr_handler;
1366                 ha->isp_ops.fw_dump = qla2300_fw_dump;
1367                 ha->isp_ops.ascii_fw_dump = qla2300_ascii_fw_dump;
1368                 ha->isp_ops.beacon_on = qla2x00_beacon_on;
1369                 ha->isp_ops.beacon_off = qla2x00_beacon_off;
1370                 ha->isp_ops.beacon_blink = qla2x00_beacon_blink;
1371                 ha->gid_list_info_size = 6;
1372         } else if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
1373                 host->max_id = MAX_TARGETS_2200;
1374                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1375                 ha->request_q_length = REQUEST_ENTRY_CNT_24XX;
1376                 ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
1377                 ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
1378                 ha->init_cb_size = sizeof(struct init_cb_24xx);
1379                 ha->mgmt_svr_loop_id = 10;
1380                 ha->isp_ops.pci_config = qla24xx_pci_config;
1381                 ha->isp_ops.reset_chip = qla24xx_reset_chip;
1382                 ha->isp_ops.chip_diag = qla24xx_chip_diag;
1383                 ha->isp_ops.config_rings = qla24xx_config_rings;
1384                 ha->isp_ops.reset_adapter = qla24xx_reset_adapter;
1385                 ha->isp_ops.nvram_config = qla24xx_nvram_config;
1386                 ha->isp_ops.update_fw_options = qla24xx_update_fw_options;
1387                 ha->isp_ops.load_risc = qla24xx_load_risc;
1388 #if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
1389                 if (ql2xfwloadflash)
1390                         ha->isp_ops.load_risc = qla24xx_load_risc_flash;
1391 #endif
1392                 ha->isp_ops.pci_info_str = qla24xx_pci_info_str;
1393                 ha->isp_ops.fw_version_str = qla24xx_fw_version_str;
1394                 ha->isp_ops.intr_handler = qla24xx_intr_handler;
1395                 ha->isp_ops.enable_intrs = qla24xx_enable_intrs;
1396                 ha->isp_ops.disable_intrs = qla24xx_disable_intrs;
1397                 ha->isp_ops.abort_command = qla24xx_abort_command;
1398                 ha->isp_ops.abort_target = qla24xx_abort_target;
1399                 ha->isp_ops.fabric_login = qla24xx_login_fabric;
1400                 ha->isp_ops.fabric_logout = qla24xx_fabric_logout;
1401                 ha->isp_ops.prep_ms_iocb = qla24xx_prep_ms_iocb;
1402                 ha->isp_ops.prep_ms_fdmi_iocb = qla24xx_prep_ms_fdmi_iocb;
1403                 ha->isp_ops.read_nvram = qla24xx_read_nvram_data;
1404                 ha->isp_ops.write_nvram = qla24xx_write_nvram_data;
1405                 ha->isp_ops.fw_dump = qla24xx_fw_dump;
1406                 ha->isp_ops.ascii_fw_dump = qla24xx_ascii_fw_dump;
1407                 ha->isp_ops.beacon_on = qla24xx_beacon_on;
1408                 ha->isp_ops.beacon_off = qla24xx_beacon_off;
1409                 ha->isp_ops.beacon_blink = qla24xx_beacon_blink;
1410                 ha->gid_list_info_size = 8;
1411         }
1412         host->can_queue = ha->request_q_length + 128;
1413
1414         /* load the F/W, read paramaters, and init the H/W */
1415         ha->instance = num_hosts;
1416
1417         init_MUTEX(&ha->mbx_cmd_sem);
1418         init_MUTEX_LOCKED(&ha->mbx_intr_sem);
1419
1420         INIT_LIST_HEAD(&ha->list);
1421         INIT_LIST_HEAD(&ha->fcports);
1422         INIT_LIST_HEAD(&ha->rscn_fcports);
1423
1424         /*
1425          * These locks are used to prevent more than one CPU
1426          * from modifying the queue at the same time. The
1427          * higher level "host_lock" will reduce most
1428          * contention for these locks.
1429          */
1430         spin_lock_init(&ha->mbx_reg_lock);
1431
1432         init_completion(&ha->dpc_inited);
1433         init_completion(&ha->dpc_exited);
1434
1435         qla2x00_config_dma_addressing(ha);
1436         if (qla2x00_mem_alloc(ha)) {
1437                 qla_printk(KERN_WARNING, ha,
1438                     "[ERROR] Failed to allocate memory for adapter\n");
1439
1440                 ret = -ENOMEM;
1441                 goto probe_failed;
1442         }
1443
1444         if (qla2x00_initialize_adapter(ha) &&
1445             !(ha->device_flags & DFLG_NO_CABLE)) {
1446
1447                 qla_printk(KERN_WARNING, ha,
1448                     "Failed to initialize adapter\n");
1449
1450                 DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
1451                     "Adapter flags %x.\n",
1452                     ha->host_no, ha->device_flags));
1453
1454                 ret = -ENODEV;
1455                 goto probe_failed;
1456         }
1457
1458         /*
1459          * Startup the kernel thread for this host adapter
1460          */
1461         ha->dpc_should_die = 0;
1462         ha->dpc_pid = kernel_thread(qla2x00_do_dpc, ha, 0);
1463         if (ha->dpc_pid < 0) {
1464                 qla_printk(KERN_WARNING, ha,
1465                     "Unable to start DPC thread!\n");
1466
1467                 ret = -ENODEV;
1468                 goto probe_failed;
1469         }
1470         wait_for_completion(&ha->dpc_inited);
1471
1472         host->this_id = 255;
1473         host->cmd_per_lun = 3;
1474         host->unique_id = ha->instance;
1475         host->max_cmd_len = MAX_CMDSZ;
1476         host->max_channel = ha->ports - 1;
1477         host->max_lun = MAX_LUNS;
1478         host->transportt = qla2xxx_transport_template;
1479
1480         ret = request_irq(pdev->irq, ha->isp_ops.intr_handler,
1481             SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
1482         if (ret) {
1483                 qla_printk(KERN_WARNING, ha,
1484                     "Failed to reserve interrupt %d already in use.\n",
1485                     pdev->irq);
1486                 goto probe_failed;
1487         }
1488         host->irq = pdev->irq;
1489
1490         /* Initialized the timer */
1491         qla2x00_start_timer(ha, qla2x00_timer, WATCH_INTERVAL);
1492
1493         DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
1494             ha->host_no, ha));
1495
1496         ha->isp_ops.disable_intrs(ha);
1497
1498         spin_lock_irqsave(&ha->hardware_lock, flags);
1499         reg = ha->iobase;
1500         if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
1501                 WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_HOST_INT);
1502                 WRT_REG_DWORD(&reg->isp24.hccr, HCCRX_CLR_RISC_INT);
1503         } else {
1504                 WRT_REG_WORD(&reg->isp.semaphore, 0);
1505                 WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_RISC_INT);
1506                 WRT_REG_WORD(&reg->isp.hccr, HCCR_CLR_HOST_INT);
1507
1508                 /* Enable proper parity */
1509                 if (!IS_QLA2100(ha) && !IS_QLA2200(ha)) {
1510                         if (IS_QLA2300(ha))
1511                                 /* SRAM parity */
1512                                 WRT_REG_WORD(&reg->isp.hccr,
1513                                     (HCCR_ENABLE_PARITY + 0x1));
1514                         else
1515                                 /* SRAM, Instruction RAM and GP RAM parity */
1516                                 WRT_REG_WORD(&reg->isp.hccr,
1517                                     (HCCR_ENABLE_PARITY + 0x7));
1518                 }
1519         }
1520         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1521
1522         ha->isp_ops.enable_intrs(ha);
1523
1524         /* v2.19.5b6 */
1525         /*
1526          * Wait around max loop_reset_delay secs for the devices to come
1527          * on-line. We don't want Linux scanning before we are ready.
1528          *
1529          */
1530         for (wait_switch = jiffies + (ha->loop_reset_delay * HZ);
1531             time_before(jiffies,wait_switch) &&
1532              !(ha->device_flags & (DFLG_NO_CABLE | DFLG_FABRIC_DEVICES))
1533              && (ha->device_flags & SWITCH_FOUND) ;) {
1534
1535                 qla2x00_check_fabric_devices(ha);
1536
1537                 msleep(10);
1538         }
1539
1540         pci_set_drvdata(pdev, ha);
1541         ha->flags.init_done = 1;
1542         num_hosts++;
1543
1544         ret = scsi_add_host(host, &pdev->dev);
1545         if (ret)
1546                 goto probe_failed;
1547
1548         qla2x00_alloc_sysfs_attr(ha);
1549
1550         qla2x00_init_host_attr(ha);
1551
1552         qla_printk(KERN_INFO, ha, "\n"
1553             " QLogic Fibre Channel HBA Driver: %s\n"
1554             "  QLogic %s - %s\n"
1555             "  ISP%04X: %s @ %s hdma%c, host#=%ld, fw=%s\n",
1556             qla2x00_version_str, ha->model_number,
1557             ha->model_desc ? ha->model_desc: "", pdev->device,
1558             ha->isp_ops.pci_info_str(ha, pci_info), pci_name(pdev),
1559             ha->flags.enable_64bit_addressing ? '+': '-', ha->host_no,
1560             ha->isp_ops.fw_version_str(ha, fw_str));
1561
1562         /* Go with fc_rport registration. */
1563         list_for_each_entry(fcport, &ha->fcports, list)
1564                 qla2x00_reg_remote_port(ha, fcport);
1565
1566         return 0;
1567
1568 probe_failed:
1569         qla2x00_free_device(ha);
1570
1571         scsi_host_put(host);
1572
1573 probe_disable_device:
1574         pci_disable_device(pdev);
1575
1576 probe_out:
1577         return ret;
1578 }
1579 EXPORT_SYMBOL_GPL(qla2x00_probe_one);
1580
1581 void qla2x00_remove_one(struct pci_dev *pdev)
1582 {
1583         scsi_qla_host_t *ha;
1584
1585         ha = pci_get_drvdata(pdev);
1586
1587         qla2x00_free_sysfs_attr(ha);
1588
1589         fc_remove_host(ha->host);
1590
1591         scsi_remove_host(ha->host);
1592
1593         qla2x00_free_device(ha);
1594
1595         scsi_host_put(ha->host);
1596
1597         pci_set_drvdata(pdev, NULL);
1598 }
1599 EXPORT_SYMBOL_GPL(qla2x00_remove_one);
1600
1601 static void
1602 qla2x00_free_device(scsi_qla_host_t *ha)
1603 {
1604         int ret;
1605
1606         /* Abort any outstanding IO descriptors. */
1607         if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1608                 qla2x00_cancel_io_descriptors(ha);
1609
1610         /* Disable timer */
1611         if (ha->timer_active)
1612                 qla2x00_stop_timer(ha);
1613
1614         /* Kill the kernel thread for this host */
1615         if (ha->dpc_pid >= 0) {
1616                 ha->dpc_should_die = 1;
1617                 wmb();
1618                 ret = kill_proc(ha->dpc_pid, SIGHUP, 1);
1619                 if (ret) {
1620                         qla_printk(KERN_ERR, ha,
1621                             "Unable to signal DPC thread -- (%d)\n", ret);
1622
1623                         /* TODO: SOMETHING MORE??? */
1624                 } else {
1625                         wait_for_completion(&ha->dpc_exited);
1626                 }
1627         }
1628
1629         /* Stop currently executing firmware. */
1630         qla2x00_stop_firmware(ha);
1631
1632         /* turn-off interrupts on the card */
1633         if (ha->interrupts_on)
1634                 ha->isp_ops.disable_intrs(ha);
1635
1636         qla2x00_mem_free(ha);
1637
1638         ha->flags.online = 0;
1639
1640         /* Detach interrupts */
1641         if (ha->pdev->irq)
1642                 free_irq(ha->pdev->irq, ha);
1643
1644         /* release io space registers  */
1645         if (ha->iobase)
1646                 iounmap(ha->iobase);
1647         pci_release_regions(ha->pdev);
1648
1649         pci_disable_device(ha->pdev);
1650 }
1651
1652 static inline void
1653 qla2x00_schedule_rport_del(struct scsi_qla_host *ha, fc_port_t *fcport,
1654     int defer)
1655 {
1656         unsigned long flags;
1657         struct fc_rport *rport;
1658
1659         if (!fcport->rport)
1660                 return;
1661
1662         rport = fcport->rport;
1663         if (defer) {
1664                 spin_lock_irqsave(&fcport->rport_lock, flags);
1665                 fcport->drport = rport;
1666                 fcport->rport = NULL;
1667                 spin_unlock_irqrestore(&fcport->rport_lock, flags);
1668                 set_bit(FCPORT_UPDATE_NEEDED, &ha->dpc_flags);
1669         } else {
1670                 spin_lock_irqsave(&fcport->rport_lock, flags);
1671                 fcport->rport = NULL;
1672                 spin_unlock_irqrestore(&fcport->rport_lock, flags);
1673                 fc_remote_port_delete(rport);
1674         }
1675 }
1676
1677 /*
1678  * qla2x00_mark_device_lost Updates fcport state when device goes offline.
1679  *
1680  * Input: ha = adapter block pointer.  fcport = port structure pointer.
1681  *
1682  * Return: None.
1683  *
1684  * Context:
1685  */
1686 void qla2x00_mark_device_lost(scsi_qla_host_t *ha, fc_port_t *fcport,
1687     int do_login, int defer)
1688 {
1689         if (atomic_read(&fcport->state) == FCS_ONLINE)
1690                 qla2x00_schedule_rport_del(ha, fcport, defer);
1691
1692         /*
1693          * We may need to retry the login, so don't change the state of the
1694          * port but do the retries.
1695          */
1696         if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
1697                 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1698
1699         if (!do_login)
1700                 return;
1701
1702         if (fcport->login_retry == 0) {
1703                 fcport->login_retry = ha->login_retry_count;
1704                 set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
1705
1706                 DEBUG(printk("scsi(%ld): Port login retry: "
1707                     "%02x%02x%02x%02x%02x%02x%02x%02x, "
1708                     "id = 0x%04x retry cnt=%d\n",
1709                     ha->host_no,
1710                     fcport->port_name[0],
1711                     fcport->port_name[1],
1712                     fcport->port_name[2],
1713                     fcport->port_name[3],
1714                     fcport->port_name[4],
1715                     fcport->port_name[5],
1716                     fcport->port_name[6],
1717                     fcport->port_name[7],
1718                     fcport->loop_id,
1719                     fcport->login_retry));
1720         }
1721 }
1722
1723 /*
1724  * qla2x00_mark_all_devices_lost
1725  *      Updates fcport state when device goes offline.
1726  *
1727  * Input:
1728  *      ha = adapter block pointer.
1729  *      fcport = port structure pointer.
1730  *
1731  * Return:
1732  *      None.
1733  *
1734  * Context:
1735  */
1736 void
1737 qla2x00_mark_all_devices_lost(scsi_qla_host_t *ha, int defer)
1738 {
1739         fc_port_t *fcport;
1740
1741         list_for_each_entry(fcport, &ha->fcports, list) {
1742                 if (fcport->port_type != FCT_TARGET)
1743                         continue;
1744
1745                 /*
1746                  * No point in marking the device as lost, if the device is
1747                  * already DEAD.
1748                  */
1749                 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
1750                         continue;
1751                 if (atomic_read(&fcport->state) == FCS_ONLINE)
1752                         qla2x00_schedule_rport_del(ha, fcport, defer);
1753                 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1754         }
1755
1756         if (defer && ha->dpc_wait && !ha->dpc_active)
1757                 up(ha->dpc_wait);
1758 }
1759
1760 /*
1761 * qla2x00_mem_alloc
1762 *      Allocates adapter memory.
1763 *
1764 * Returns:
1765 *      0  = success.
1766 *      1  = failure.
1767 */
1768 static uint8_t
1769 qla2x00_mem_alloc(scsi_qla_host_t *ha)
1770 {
1771         char    name[16];
1772         uint8_t   status = 1;
1773         int     retry= 10;
1774
1775         do {
1776                 /*
1777                  * This will loop only once if everything goes well, else some
1778                  * number of retries will be performed to get around a kernel
1779                  * bug where available mem is not allocated until after a
1780                  * little delay and a retry.
1781                  */
1782                 ha->request_ring = dma_alloc_coherent(&ha->pdev->dev,
1783                     (ha->request_q_length + 1) * sizeof(request_t),
1784                     &ha->request_dma, GFP_KERNEL);
1785                 if (ha->request_ring == NULL) {
1786                         qla_printk(KERN_WARNING, ha,
1787                             "Memory Allocation failed - request_ring\n");
1788
1789                         qla2x00_mem_free(ha);
1790                         msleep(100);
1791
1792                         continue;
1793                 }
1794
1795                 ha->response_ring = dma_alloc_coherent(&ha->pdev->dev,
1796                     (ha->response_q_length + 1) * sizeof(response_t),
1797                     &ha->response_dma, GFP_KERNEL);
1798                 if (ha->response_ring == NULL) {
1799                         qla_printk(KERN_WARNING, ha,
1800                             "Memory Allocation failed - response_ring\n");
1801
1802                         qla2x00_mem_free(ha);
1803                         msleep(100);
1804
1805                         continue;
1806                 }
1807
1808                 ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
1809                     &ha->gid_list_dma, GFP_KERNEL);
1810                 if (ha->gid_list == NULL) {
1811                         qla_printk(KERN_WARNING, ha,
1812                             "Memory Allocation failed - gid_list\n");
1813
1814                         qla2x00_mem_free(ha);
1815                         msleep(100);
1816
1817                         continue;
1818                 }
1819
1820                 ha->rlc_rsp = dma_alloc_coherent(&ha->pdev->dev,
1821                     sizeof(rpt_lun_cmd_rsp_t), &ha->rlc_rsp_dma, GFP_KERNEL);
1822                 if (ha->rlc_rsp == NULL) {
1823                         qla_printk(KERN_WARNING, ha,
1824                                 "Memory Allocation failed - rlc");
1825
1826                         qla2x00_mem_free(ha);
1827                         msleep(100);
1828
1829                         continue;
1830                 }
1831
1832                 snprintf(name, sizeof(name), "qla2xxx_%ld", ha->host_no);
1833                 ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
1834                     DMA_POOL_SIZE, 8, 0);
1835                 if (ha->s_dma_pool == NULL) {
1836                         qla_printk(KERN_WARNING, ha,
1837                             "Memory Allocation failed - s_dma_pool\n");
1838
1839                         qla2x00_mem_free(ha);
1840                         msleep(100);
1841
1842                         continue;
1843                 }
1844
1845                 /* get consistent memory allocated for init control block */
1846                 ha->init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1847                     &ha->init_cb_dma);
1848                 if (ha->init_cb == NULL) {
1849                         qla_printk(KERN_WARNING, ha,
1850                             "Memory Allocation failed - init_cb\n");
1851
1852                         qla2x00_mem_free(ha);
1853                         msleep(100);
1854
1855                         continue;
1856                 }
1857                 memset(ha->init_cb, 0, ha->init_cb_size);
1858
1859                 /* Get consistent memory allocated for Get Port Database cmd */
1860                 ha->iodesc_pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1861                     &ha->iodesc_pd_dma);
1862                 if (ha->iodesc_pd == NULL) {
1863                         /* error */
1864                         qla_printk(KERN_WARNING, ha,
1865                             "Memory Allocation failed - iodesc_pd\n");
1866
1867                         qla2x00_mem_free(ha);
1868                         msleep(100);
1869
1870                         continue;
1871                 }
1872                 memset(ha->iodesc_pd, 0, PORT_DATABASE_SIZE);
1873
1874                 /* Allocate ioctl related memory. */
1875                 if (qla2x00_alloc_ioctl_mem(ha)) {
1876                         qla_printk(KERN_WARNING, ha,
1877                             "Memory Allocation failed - ioctl_mem\n");
1878
1879                         qla2x00_mem_free(ha);
1880                         msleep(100);
1881
1882                         continue;
1883                 }
1884
1885                 if (qla2x00_allocate_sp_pool(ha)) {
1886                         qla_printk(KERN_WARNING, ha,
1887                             "Memory Allocation failed - "
1888                             "qla2x00_allocate_sp_pool()\n");
1889
1890                         qla2x00_mem_free(ha);
1891                         msleep(100);
1892
1893                         continue;
1894                 }
1895
1896                 /* Allocate memory for SNS commands */
1897                 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1898                         /* Get consistent memory allocated for SNS commands */
1899                         ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
1900                             sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma,
1901                             GFP_KERNEL);
1902                         if (ha->sns_cmd == NULL) {
1903                                 /* error */
1904                                 qla_printk(KERN_WARNING, ha,
1905                                     "Memory Allocation failed - sns_cmd\n");
1906
1907                                 qla2x00_mem_free(ha);
1908                                 msleep(100);
1909
1910                                 continue;
1911                         }
1912                         memset(ha->sns_cmd, 0, sizeof(struct sns_cmd_pkt));
1913                 } else {
1914                         /* Get consistent memory allocated for MS IOCB */
1915                         ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1916                             &ha->ms_iocb_dma);
1917                         if (ha->ms_iocb == NULL) {
1918                                 /* error */
1919                                 qla_printk(KERN_WARNING, ha,
1920                                     "Memory Allocation failed - ms_iocb\n");
1921
1922                                 qla2x00_mem_free(ha);
1923                                 msleep(100);
1924
1925                                 continue;
1926                         }
1927                         memset(ha->ms_iocb, 0, sizeof(ms_iocb_entry_t));
1928
1929                         /*
1930                          * Get consistent memory allocated for CT SNS
1931                          * commands
1932                          */
1933                         ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
1934                             sizeof(struct ct_sns_pkt), &ha->ct_sns_dma,
1935                             GFP_KERNEL);
1936                         if (ha->ct_sns == NULL) {
1937                                 /* error */
1938                                 qla_printk(KERN_WARNING, ha,
1939                                     "Memory Allocation failed - ct_sns\n");
1940
1941                                 qla2x00_mem_free(ha);
1942                                 msleep(100);
1943
1944                                 continue;
1945                         }
1946                         memset(ha->ct_sns, 0, sizeof(struct ct_sns_pkt));
1947                 }
1948
1949                 /* Done all allocations without any error. */
1950                 status = 0;
1951
1952         } while (retry-- && status != 0);
1953
1954         if (status) {
1955                 printk(KERN_WARNING
1956                         "%s(): **** FAILED ****\n", __func__);
1957         }
1958
1959         return(status);
1960 }
1961
1962 /*
1963 * qla2x00_mem_free
1964 *      Frees all adapter allocated memory.
1965 *
1966 * Input:
1967 *      ha = adapter block pointer.
1968 */
1969 static void
1970 qla2x00_mem_free(scsi_qla_host_t *ha)
1971 {
1972         struct list_head        *fcpl, *fcptemp;
1973         fc_port_t       *fcport;
1974         unsigned int    wtime;/* max wait time if mbx cmd is busy. */
1975
1976         if (ha == NULL) {
1977                 /* error */
1978                 DEBUG2(printk("%s(): ERROR invalid ha pointer.\n", __func__));
1979                 return;
1980         }
1981
1982         /* Make sure all other threads are stopped. */
1983         wtime = 60 * 1000;
1984         while (ha->dpc_wait && wtime)
1985                 wtime = msleep_interruptible(wtime);
1986
1987         /* free ioctl memory */
1988         qla2x00_free_ioctl_mem(ha);
1989
1990         /* free sp pool */
1991         qla2x00_free_sp_pool(ha);
1992
1993         if (ha->sns_cmd)
1994                 dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
1995                     ha->sns_cmd, ha->sns_cmd_dma);
1996
1997         if (ha->ct_sns)
1998                 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
1999                     ha->ct_sns, ha->ct_sns_dma);
2000
2001         if (ha->ms_iocb)
2002                 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
2003
2004         if (ha->iodesc_pd)
2005                 dma_pool_free(ha->s_dma_pool, ha->iodesc_pd, ha->iodesc_pd_dma);
2006
2007         if (ha->init_cb)
2008                 dma_pool_free(ha->s_dma_pool, ha->init_cb, ha->init_cb_dma);
2009
2010         if (ha->s_dma_pool)
2011                 dma_pool_destroy(ha->s_dma_pool);
2012
2013         if (ha->rlc_rsp)
2014                 dma_free_coherent(&ha->pdev->dev,
2015                     sizeof(rpt_lun_cmd_rsp_t), ha->rlc_rsp,
2016                     ha->rlc_rsp_dma);
2017
2018         if (ha->gid_list)
2019                 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
2020                     ha->gid_list_dma);
2021
2022         if (ha->response_ring)
2023                 dma_free_coherent(&ha->pdev->dev,
2024                     (ha->response_q_length + 1) * sizeof(response_t),
2025                     ha->response_ring, ha->response_dma);
2026
2027         if (ha->request_ring)
2028                 dma_free_coherent(&ha->pdev->dev,
2029                     (ha->request_q_length + 1) * sizeof(request_t),
2030                     ha->request_ring, ha->request_dma);
2031
2032         ha->sns_cmd = NULL;
2033         ha->sns_cmd_dma = 0;
2034         ha->ct_sns = NULL;
2035         ha->ct_sns_dma = 0;
2036         ha->ms_iocb = NULL;
2037         ha->ms_iocb_dma = 0;
2038         ha->iodesc_pd = NULL;
2039         ha->iodesc_pd_dma = 0;
2040         ha->init_cb = NULL;
2041         ha->init_cb_dma = 0;
2042
2043         ha->s_dma_pool = NULL;
2044
2045         ha->rlc_rsp = NULL;
2046         ha->rlc_rsp_dma = 0;
2047         ha->gid_list = NULL;
2048         ha->gid_list_dma = 0;
2049
2050         ha->response_ring = NULL;
2051         ha->response_dma = 0;
2052         ha->request_ring = NULL;
2053         ha->request_dma = 0;
2054
2055         list_for_each_safe(fcpl, fcptemp, &ha->fcports) {
2056                 fcport = list_entry(fcpl, fc_port_t, list);
2057
2058                 /* fc ports */
2059                 list_del_init(&fcport->list);
2060                 kfree(fcport);
2061         }
2062         INIT_LIST_HEAD(&ha->fcports);
2063
2064         if (ha->fw_dump)
2065                 free_pages((unsigned long)ha->fw_dump, ha->fw_dump_order);
2066
2067         vfree(ha->fw_dump24);
2068
2069         vfree(ha->fw_dump_buffer);
2070
2071         ha->fw_dump = NULL;
2072         ha->fw_dump24 = NULL;
2073         ha->fw_dumped = 0;
2074         ha->fw_dump_reading = 0;
2075         ha->fw_dump_buffer = NULL;
2076 }
2077
2078 /*
2079  * qla2x00_allocate_sp_pool
2080  *       This routine is called during initialization to allocate
2081  *       memory for local srb_t.
2082  *
2083  * Input:
2084  *       ha   = adapter block pointer.
2085  *
2086  * Context:
2087  *      Kernel context.
2088  *
2089  * Note: Sets the ref_count for non Null sp to one.
2090  */
2091 static int
2092 qla2x00_allocate_sp_pool(scsi_qla_host_t *ha)
2093 {
2094         int      rval;
2095
2096         rval = QLA_SUCCESS;
2097         ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
2098             mempool_free_slab, srb_cachep);
2099         if (ha->srb_mempool == NULL) {
2100                 qla_printk(KERN_INFO, ha, "Unable to allocate SRB mempool.\n");
2101                 rval = QLA_FUNCTION_FAILED;
2102         }
2103         return (rval);
2104 }
2105
2106 /*
2107  *  This routine frees all adapter allocated memory.
2108  *
2109  */
2110 static void
2111 qla2x00_free_sp_pool( scsi_qla_host_t *ha)
2112 {
2113         if (ha->srb_mempool) {
2114                 mempool_destroy(ha->srb_mempool);
2115                 ha->srb_mempool = NULL;
2116         }
2117 }
2118
2119 /**************************************************************************
2120 * qla2x00_do_dpc
2121 *   This kernel thread is a task that is schedule by the interrupt handler
2122 *   to perform the background processing for interrupts.
2123 *
2124 * Notes:
2125 * This task always run in the context of a kernel thread.  It
2126 * is kick-off by the driver's detect code and starts up
2127 * up one per adapter. It immediately goes to sleep and waits for
2128 * some fibre event.  When either the interrupt handler or
2129 * the timer routine detects a event it will one of the task
2130 * bits then wake us up.
2131 **************************************************************************/
2132 static int
2133 qla2x00_do_dpc(void *data)
2134 {
2135         DECLARE_MUTEX_LOCKED(sem);
2136         scsi_qla_host_t *ha;
2137         fc_port_t       *fcport;
2138         uint8_t         status;
2139         uint16_t        next_loopid;
2140
2141         ha = (scsi_qla_host_t *)data;
2142
2143         lock_kernel();
2144
2145         daemonize("%s_dpc", ha->host_str);
2146         allow_signal(SIGHUP);
2147
2148         ha->dpc_wait = &sem;
2149
2150         set_user_nice(current, -20);
2151
2152         unlock_kernel();
2153
2154         complete(&ha->dpc_inited);
2155
2156         while (1) {
2157                 DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
2158
2159                 if (down_interruptible(&sem))
2160                         break;
2161
2162                 if (ha->dpc_should_die)
2163                         break;
2164
2165                 DEBUG3(printk("qla2x00: DPC handler waking up\n"));
2166
2167                 /* Initialization not yet finished. Don't do anything yet. */
2168                 if (!ha->flags.init_done || ha->dpc_active)
2169                         continue;
2170
2171                 DEBUG3(printk("scsi(%ld): DPC handler\n", ha->host_no));
2172
2173                 ha->dpc_active = 1;
2174
2175                 if (ha->flags.mbox_busy) {
2176                         ha->dpc_active = 0;
2177                         continue;
2178                 }
2179
2180                 if (test_and_clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
2181
2182                         DEBUG(printk("scsi(%ld): dpc: sched "
2183                             "qla2x00_abort_isp ha = %p\n",
2184                             ha->host_no, ha));
2185                         if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
2186                             &ha->dpc_flags))) {
2187
2188                                 if (qla2x00_abort_isp(ha)) {
2189                                         /* failed. retry later */
2190                                         set_bit(ISP_ABORT_NEEDED,
2191                                             &ha->dpc_flags);
2192                                 }
2193                                 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
2194                         }
2195                         DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
2196                             ha->host_no));
2197                 }
2198
2199                 if (test_and_clear_bit(FCPORT_UPDATE_NEEDED, &ha->dpc_flags))
2200                         qla2x00_update_fcports(ha);
2201
2202                 if (test_and_clear_bit(LOOP_RESET_NEEDED, &ha->dpc_flags)) {
2203                         DEBUG(printk("scsi(%ld): dpc: sched loop_reset()\n",
2204                             ha->host_no));
2205                         qla2x00_loop_reset(ha);
2206                 }
2207
2208                 if (test_and_clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) &&
2209                     (!(test_and_set_bit(RESET_ACTIVE, &ha->dpc_flags)))) {
2210
2211                         DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
2212                             ha->host_no));
2213
2214                         qla2x00_rst_aen(ha);
2215                         clear_bit(RESET_ACTIVE, &ha->dpc_flags);
2216                 }
2217
2218                 /* Retry each device up to login retry count */
2219                 if ((test_and_clear_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
2220                     !test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) &&
2221                     atomic_read(&ha->loop_state) != LOOP_DOWN) {
2222
2223                         DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
2224                             ha->host_no));
2225
2226                         next_loopid = 0;
2227                         list_for_each_entry(fcport, &ha->fcports, list) {
2228                                 if (fcport->port_type != FCT_TARGET)
2229                                         continue;
2230
2231                                 /*
2232                                  * If the port is not ONLINE then try to login
2233                                  * to it if we haven't run out of retries.
2234                                  */
2235                                 if (atomic_read(&fcport->state) != FCS_ONLINE &&
2236                                     fcport->login_retry) {
2237
2238                                         fcport->login_retry--;
2239                                         if (fcport->flags & FCF_FABRIC_DEVICE) {
2240                                                 if (fcport->flags &
2241                                                     FCF_TAPE_PRESENT)
2242                                                         ha->isp_ops.fabric_logout(
2243                                                             ha, fcport->loop_id,
2244                                                             fcport->d_id.b.domain,
2245                                                             fcport->d_id.b.area,
2246                                                             fcport->d_id.b.al_pa);
2247                                                 status = qla2x00_fabric_login(
2248                                                     ha, fcport, &next_loopid);
2249                                         } else
2250                                                 status =
2251                                                     qla2x00_local_device_login(
2252                                                         ha, fcport->loop_id);
2253
2254                                         if (status == QLA_SUCCESS) {
2255                                                 fcport->old_loop_id = fcport->loop_id;
2256
2257                                                 DEBUG(printk("scsi(%ld): port login OK: logged in ID 0x%x\n",
2258                                                     ha->host_no, fcport->loop_id));
2259
2260                                                 qla2x00_update_fcport(ha,
2261                                                     fcport);
2262                                         } else if (status == 1) {
2263                                                 set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
2264                                                 /* retry the login again */
2265                                                 DEBUG(printk("scsi(%ld): Retrying %d login again loop_id 0x%x\n",
2266                                                     ha->host_no,
2267                                                     fcport->login_retry, fcport->loop_id));
2268                                         } else {
2269                                                 fcport->login_retry = 0;
2270                                         }
2271                                 }
2272                                 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
2273                                         break;
2274                         }
2275                         DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
2276                             ha->host_no));
2277                 }
2278
2279                 if ((test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags)) &&
2280                     atomic_read(&ha->loop_state) != LOOP_DOWN) {
2281
2282                         clear_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags);
2283                         DEBUG(printk("scsi(%ld): qla2x00_login_retry()\n",
2284                             ha->host_no));
2285
2286                         set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2287
2288                         DEBUG(printk("scsi(%ld): qla2x00_login_retry - end\n",
2289                             ha->host_no));
2290                 }
2291
2292                 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
2293
2294                         DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
2295                             ha->host_no));
2296
2297                         if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
2298                             &ha->dpc_flags))) {
2299
2300                                 qla2x00_loop_resync(ha);
2301
2302                                 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
2303                         }
2304
2305                         DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
2306                             ha->host_no));
2307                 }
2308
2309                 if (test_and_clear_bit(FCPORT_RESCAN_NEEDED, &ha->dpc_flags)) {
2310
2311                         DEBUG(printk("scsi(%ld): Rescan flagged fcports...\n",
2312                             ha->host_no));
2313
2314                         qla2x00_rescan_fcports(ha);
2315
2316                         DEBUG(printk("scsi(%ld): Rescan flagged fcports..."
2317                             "end.\n",
2318                             ha->host_no));
2319                 }
2320
2321                 if (!ha->interrupts_on)
2322                         ha->isp_ops.enable_intrs(ha);
2323
2324                 if (test_and_clear_bit(BEACON_BLINK_NEEDED, &ha->dpc_flags))
2325                         ha->isp_ops.beacon_blink(ha);
2326
2327                 ha->dpc_active = 0;
2328         } /* End of while(1) */
2329
2330         DEBUG(printk("scsi(%ld): DPC handler exiting\n", ha->host_no));
2331
2332         /*
2333          * Make sure that nobody tries to wake us up again.
2334          */
2335         ha->dpc_wait = NULL;
2336         ha->dpc_active = 0;
2337
2338         complete_and_exit(&ha->dpc_exited, 0);
2339 }
2340
2341 /*
2342 *  qla2x00_rst_aen
2343 *      Processes asynchronous reset.
2344 *
2345 * Input:
2346 *      ha  = adapter block pointer.
2347 */
2348 static void
2349 qla2x00_rst_aen(scsi_qla_host_t *ha)
2350 {
2351         if (ha->flags.online && !ha->flags.reset_active &&
2352             !atomic_read(&ha->loop_down_timer) &&
2353             !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags))) {
2354                 do {
2355                         clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
2356
2357                         /*
2358                          * Issue marker command only when we are going to start
2359                          * the I/O.
2360                          */
2361                         ha->marker_needed = 1;
2362                 } while (!atomic_read(&ha->loop_down_timer) &&
2363                     (test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags)));
2364         }
2365 }
2366
2367 static void
2368 qla2x00_sp_free_dma(scsi_qla_host_t *ha, srb_t *sp)
2369 {
2370         struct scsi_cmnd *cmd = sp->cmd;
2371
2372         if (sp->flags & SRB_DMA_VALID) {
2373                 if (cmd->use_sg) {
2374                         dma_unmap_sg(&ha->pdev->dev, cmd->request_buffer,
2375                             cmd->use_sg, cmd->sc_data_direction);
2376                 } else if (cmd->request_bufflen) {
2377                         dma_unmap_single(&ha->pdev->dev, sp->dma_handle,
2378                             cmd->request_bufflen, cmd->sc_data_direction);
2379                 }
2380                 sp->flags &= ~SRB_DMA_VALID;
2381         }
2382         CMD_SP(cmd) = NULL;
2383 }
2384
2385 void
2386 qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *sp)
2387 {
2388         struct scsi_cmnd *cmd = sp->cmd;
2389
2390         qla2x00_sp_free_dma(ha, sp);
2391
2392         mempool_free(sp, ha->srb_mempool);
2393
2394         cmd->scsi_done(cmd);
2395 }
2396
2397 /**************************************************************************
2398 *   qla2x00_timer
2399 *
2400 * Description:
2401 *   One second timer
2402 *
2403 * Context: Interrupt
2404 ***************************************************************************/
2405 static void
2406 qla2x00_timer(scsi_qla_host_t *ha)
2407 {
2408         unsigned long   cpu_flags = 0;
2409         fc_port_t       *fcport;
2410         int             start_dpc = 0;
2411         int             index;
2412         srb_t           *sp;
2413         int             t;
2414
2415         /*
2416          * Ports - Port down timer.
2417          *
2418          * Whenever, a port is in the LOST state we start decrementing its port
2419          * down timer every second until it reaches zero. Once  it reaches zero
2420          * the port it marked DEAD.
2421          */
2422         t = 0;
2423         list_for_each_entry(fcport, &ha->fcports, list) {
2424                 if (fcport->port_type != FCT_TARGET)
2425                         continue;
2426
2427                 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2428
2429                         if (atomic_read(&fcport->port_down_timer) == 0)
2430                                 continue;
2431
2432                         if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
2433                                 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
2434
2435                         DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
2436                             "%d remaining\n",
2437                             ha->host_no,
2438                             t, atomic_read(&fcport->port_down_timer)));
2439                 }
2440                 t++;
2441         } /* End of for fcport  */
2442
2443
2444         /* Loop down handler. */
2445         if (atomic_read(&ha->loop_down_timer) > 0 &&
2446             !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) && ha->flags.online) {
2447
2448                 if (atomic_read(&ha->loop_down_timer) ==
2449                     ha->loop_down_abort_time) {
2450
2451                         DEBUG(printk("scsi(%ld): Loop Down - aborting the "
2452                             "queues before time expire\n",
2453                             ha->host_no));
2454
2455                         if (!IS_QLA2100(ha) && ha->link_down_timeout)
2456                                 atomic_set(&ha->loop_state, LOOP_DEAD);
2457
2458                         /* Schedule an ISP abort to return any tape commands. */
2459                         spin_lock_irqsave(&ha->hardware_lock, cpu_flags);
2460                         for (index = 1; index < MAX_OUTSTANDING_COMMANDS;
2461                             index++) {
2462                                 fc_port_t *sfcp;
2463
2464                                 sp = ha->outstanding_cmds[index];
2465                                 if (!sp)
2466                                         continue;
2467                                 sfcp = sp->fcport;
2468                                 if (!(sfcp->flags & FCF_TAPE_PRESENT))
2469                                         continue;
2470
2471                                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2472                                 break;
2473                         }
2474                         spin_unlock_irqrestore(&ha->hardware_lock, cpu_flags);
2475
2476                         set_bit(ABORT_QUEUES_NEEDED, &ha->dpc_flags);
2477                         start_dpc++;
2478                 }
2479
2480                 /* if the loop has been down for 4 minutes, reinit adapter */
2481                 if (atomic_dec_and_test(&ha->loop_down_timer) != 0) {
2482                         DEBUG(printk("scsi(%ld): Loop down exceed 4 mins - "
2483                             "restarting queues.\n",
2484                             ha->host_no));
2485
2486                         set_bit(RESTART_QUEUES_NEEDED, &ha->dpc_flags);
2487                         start_dpc++;
2488
2489                         if (!(ha->device_flags & DFLG_NO_CABLE)) {
2490                                 DEBUG(printk("scsi(%ld): Loop down - "
2491                                     "aborting ISP.\n",
2492                                     ha->host_no));
2493                                 qla_printk(KERN_WARNING, ha,
2494                                     "Loop down - aborting ISP.\n");
2495
2496                                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2497                         }
2498                 }
2499                 DEBUG3(printk("scsi(%ld): Loop Down - seconds remaining %d\n",
2500                     ha->host_no,
2501                     atomic_read(&ha->loop_down_timer)));
2502         }
2503
2504         /* Check if beacon LED needs to be blinked */
2505         if (ha->beacon_blink_led == 1) {
2506                 set_bit(BEACON_BLINK_NEEDED, &ha->dpc_flags);
2507                 start_dpc++;
2508         }
2509
2510         /* Schedule the DPC routine if needed */
2511         if ((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) ||
2512             test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) ||
2513             test_bit(LOOP_RESET_NEEDED, &ha->dpc_flags) ||
2514             test_bit(FCPORT_UPDATE_NEEDED, &ha->dpc_flags) ||
2515             start_dpc ||
2516             test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags) ||
2517             test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) ||
2518             test_bit(BEACON_BLINK_NEEDED, &ha->dpc_flags) ||
2519             test_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
2520             ha->dpc_wait && !ha->dpc_active) {
2521
2522                 up(ha->dpc_wait);
2523         }
2524
2525         qla2x00_restart_timer(ha, WATCH_INTERVAL);
2526 }
2527
2528 /* XXX(hch): crude hack to emulate a down_timeout() */
2529 int
2530 qla2x00_down_timeout(struct semaphore *sema, unsigned long timeout)
2531 {
2532         const unsigned int step = 100; /* msecs */
2533         unsigned int iterations = jiffies_to_msecs(timeout)/100;
2534
2535         do {
2536                 if (!down_trylock(sema))
2537                         return 0;
2538                 if (msleep_interruptible(step))
2539                         break;
2540         } while (--iterations >= 0);
2541
2542         return -ETIMEDOUT;
2543 }
2544
2545 #if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
2546
2547 #define qla2x00_release_firmware()      do { } while (0)
2548 #define qla2x00_pci_module_init()       (0)
2549 #define qla2x00_pci_module_exit()       do { } while (0)
2550
2551 #else   /* !defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE) */
2552
2553 /* Firmware interface routines. */
2554
2555 #define FW_BLOBS        6
2556 #define FW_ISP21XX      0
2557 #define FW_ISP22XX      1
2558 #define FW_ISP2300      2
2559 #define FW_ISP2322      3
2560 #define FW_ISP63XX      4
2561 #define FW_ISP24XX      5
2562
2563 static DECLARE_MUTEX(qla_fw_lock);
2564
2565 static struct fw_blob qla_fw_blobs[FW_BLOBS] = {
2566         { .name = "ql2100_fw.bin", .segs = { 0x1000, 0 }, },
2567         { .name = "ql2200_fw.bin", .segs = { 0x1000, 0 }, },
2568         { .name = "ql2300_fw.bin", .segs = { 0x800, 0 }, },
2569         { .name = "ql2322_fw.bin", .segs = { 0x800, 0x1c000, 0x1e000, 0 }, },
2570         { .name = "ql6312_fw.bin", .segs = { 0x800, 0 }, },
2571         { .name = "ql2400_fw.bin", },
2572 };
2573
2574 struct fw_blob *
2575 qla2x00_request_firmware(scsi_qla_host_t *ha)
2576 {
2577         struct fw_blob *blob;
2578
2579         blob = NULL;
2580         if (IS_QLA2100(ha)) {
2581                 blob = &qla_fw_blobs[FW_ISP21XX];
2582         } else if (IS_QLA2200(ha)) {
2583                 blob = &qla_fw_blobs[FW_ISP22XX];
2584         } else if (IS_QLA2300(ha) || IS_QLA2312(ha)) {
2585                 blob = &qla_fw_blobs[FW_ISP2300];
2586         } else if (IS_QLA2322(ha)) {
2587                 blob = &qla_fw_blobs[FW_ISP2322];
2588         } else if (IS_QLA6312(ha) || IS_QLA6322(ha)) {
2589                 blob = &qla_fw_blobs[FW_ISP63XX];
2590         } else if (IS_QLA24XX(ha)) {
2591                 blob = &qla_fw_blobs[FW_ISP24XX];
2592         }
2593
2594         down(&qla_fw_lock);
2595         if (blob->fw)
2596                 goto out;
2597
2598         if (request_firmware(&blob->fw, blob->name, &ha->pdev->dev)) {
2599                 DEBUG2(printk("scsi(%ld): Failed to load firmware image "
2600                     "(%s).\n", ha->host_no, blob->name));
2601                 blob->fw = NULL;
2602                 blob = NULL;
2603                 goto out;
2604         }
2605
2606 out:
2607         up(&qla_fw_lock);
2608         return blob;
2609 }
2610
2611 static void
2612 qla2x00_release_firmware(void)
2613 {
2614         int idx;
2615
2616         down(&qla_fw_lock);
2617         for (idx = 0; idx < FW_BLOBS; idx++)
2618                 if (qla_fw_blobs[idx].fw)
2619                         release_firmware(qla_fw_blobs[idx].fw);
2620         up(&qla_fw_lock);
2621 }
2622
2623 static struct qla_board_info qla_board_tbl = {
2624         .drv_name       = "qla2xxx",
2625 };
2626
2627 static struct pci_device_id qla2xxx_pci_tbl[] = {
2628         { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2100,
2629                 PCI_ANY_ID, PCI_ANY_ID, },
2630         { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2200,
2631                 PCI_ANY_ID, PCI_ANY_ID, },
2632         { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2300,
2633                 PCI_ANY_ID, PCI_ANY_ID, },
2634         { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2312,
2635                 PCI_ANY_ID, PCI_ANY_ID, },
2636         { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2322,
2637                 PCI_ANY_ID, PCI_ANY_ID, },
2638         { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6312,
2639                 PCI_ANY_ID, PCI_ANY_ID, },
2640         { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP6322,
2641                 PCI_ANY_ID, PCI_ANY_ID, },
2642         { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2422,
2643                 PCI_ANY_ID, PCI_ANY_ID, },
2644         { PCI_VENDOR_ID_QLOGIC, PCI_DEVICE_ID_QLOGIC_ISP2432,
2645                 PCI_ANY_ID, PCI_ANY_ID, },
2646         { 0 },
2647 };
2648 MODULE_DEVICE_TABLE(pci, qla2xxx_pci_tbl);
2649
2650 static int __devinit
2651 qla2xxx_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2652 {
2653         return qla2x00_probe_one(pdev, &qla_board_tbl);
2654 }
2655
2656 static void __devexit
2657 qla2xxx_remove_one(struct pci_dev *pdev)
2658 {
2659         qla2x00_remove_one(pdev);
2660 }
2661
2662 static struct pci_driver qla2xxx_pci_driver = {
2663         .name           = "qla2xxx",
2664         .driver         = {
2665                 .owner          = THIS_MODULE,
2666         },
2667         .id_table       = qla2xxx_pci_tbl,
2668         .probe          = qla2xxx_probe_one,
2669         .remove         = __devexit_p(qla2xxx_remove_one),
2670 };
2671
2672 static inline int
2673 qla2x00_pci_module_init(void)
2674 {
2675         return pci_module_init(&qla2xxx_pci_driver);
2676 }
2677
2678 static inline void
2679 qla2x00_pci_module_exit(void)
2680 {
2681         pci_unregister_driver(&qla2xxx_pci_driver);
2682 }
2683
2684 #endif
2685
2686 /**
2687  * qla2x00_module_init - Module initialization.
2688  **/
2689 static int __init
2690 qla2x00_module_init(void)
2691 {
2692         int ret = 0;
2693
2694         /* Allocate cache for SRBs. */
2695         srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
2696             SLAB_HWCACHE_ALIGN, NULL, NULL);
2697         if (srb_cachep == NULL) {
2698                 printk(KERN_ERR
2699                     "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
2700                 return -ENOMEM;
2701         }
2702
2703         /* Derive version string. */
2704         strcpy(qla2x00_version_str, QLA2XXX_VERSION);
2705 #if defined(CONFIG_SCSI_QLA2XXX_EMBEDDED_FIRMWARE)
2706         strcat(qla2x00_version_str, "-fw");
2707 #endif
2708 #if DEBUG_QLA2100
2709         strcat(qla2x00_version_str, "-debug");
2710 #endif
2711         qla2xxx_transport_template =
2712             fc_attach_transport(&qla2xxx_transport_functions);
2713         if (!qla2xxx_transport_template)
2714                 return -ENODEV;
2715
2716         printk(KERN_INFO "QLogic Fibre Channel HBA Driver\n");
2717         ret = qla2x00_pci_module_init();
2718         if (ret) {
2719                 kmem_cache_destroy(srb_cachep);
2720                 fc_release_transport(qla2xxx_transport_template);
2721         }
2722         return ret;
2723 }
2724
2725 /**
2726  * qla2x00_module_exit - Module cleanup.
2727  **/
2728 static void __exit
2729 qla2x00_module_exit(void)
2730 {
2731         qla2x00_pci_module_exit();
2732         qla2x00_release_firmware();
2733         kmem_cache_destroy(srb_cachep);
2734         fc_release_transport(qla2xxx_transport_template);
2735 }
2736
2737 module_init(qla2x00_module_init);
2738 module_exit(qla2x00_module_exit);
2739
2740 MODULE_AUTHOR("QLogic Corporation");
2741 MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
2742 MODULE_LICENSE("GPL");
2743 MODULE_VERSION(QLA2XXX_VERSION);