blob: 1693998aa727fb77e7169d3a62661e2d7d440554 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * QLOGIC LINUX SOFTWARE
3 *
4 * QLogic ISP2x00 device driver for Linux 2.6.x
5 * Copyright (C) 2003-2004 QLogic Corporation
6 * (www.qlogic.com)
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 */
19#include "qla_def.h"
20
21#include <linux/moduleparam.h>
22#include <linux/vmalloc.h>
23#include <linux/smp_lock.h>
24#include <linux/delay.h>
25
26#include <scsi/scsi_tcq.h>
27#include <scsi/scsicam.h>
28#include <scsi/scsi_transport.h>
29#include <scsi/scsi_transport_fc.h>
30
31/*
32 * Driver version
33 */
34char qla2x00_version_str[40];
35
36/*
37 * SRB allocation cache
38 */
Andrew Vasquez 354d6b22005-04-23 02:47:27 -040039static kmem_cache_t *srb_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/*
42 * Ioctl related information.
43 */
Andrew Vasquez 354d6b22005-04-23 02:47:27 -040044static int num_hosts;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46int ql2xlogintimeout = 20;
47module_param(ql2xlogintimeout, int, S_IRUGO|S_IRUSR);
48MODULE_PARM_DESC(ql2xlogintimeout,
49 "Login timeout value in seconds.");
50
8482e112005-04-17 15:04:54 -050051int qlport_down_retry = 30;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052module_param(qlport_down_retry, int, S_IRUGO|S_IRUSR);
53MODULE_PARM_DESC(qlport_down_retry,
54 "Maximum number of command retries to a port that returns"
55 "a PORT-DOWN status.");
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057int ql2xplogiabsentdevice;
58module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
59MODULE_PARM_DESC(ql2xplogiabsentdevice,
60 "Option to enable PLOGI to devices that are not present after "
61 "a Fabric scan. This is needed for several broken switches."
62 "Default is 0 - no PLOGI. 1 - perfom PLOGI.");
63
64int ql2xenablezio = 0;
65module_param(ql2xenablezio, int, S_IRUGO|S_IRUSR);
66MODULE_PARM_DESC(ql2xenablezio,
67 "Option to enable ZIO:If 1 then enable it otherwise"
68 " use the default set in the NVRAM."
69 " Default is 0 : disabled");
70
71int ql2xintrdelaytimer = 10;
72module_param(ql2xintrdelaytimer, int, S_IRUGO|S_IRUSR);
73MODULE_PARM_DESC(ql2xintrdelaytimer,
74 "ZIO: Waiting time for Firmware before it generates an "
75 "interrupt to the host to notify completion of request.");
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077int ql2xloginretrycount = 0;
78module_param(ql2xloginretrycount, int, S_IRUGO|S_IRUSR);
79MODULE_PARM_DESC(ql2xloginretrycount,
80 "Specify an alternate value for the NVRAM login retry count.");
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082static void qla2x00_free_device(scsi_qla_host_t *);
83
84static void qla2x00_config_dma_addressing(scsi_qla_host_t *ha);
85
86/*
87 * SCSI host template entry points
88 */
89static int qla2xxx_slave_configure(struct scsi_device * device);
f4f051e2005-04-17 15:02:26 -050090static int qla2xxx_slave_alloc(struct scsi_device *);
91static void qla2xxx_slave_destroy(struct scsi_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int qla2x00_queuecommand(struct scsi_cmnd *cmd,
93 void (*fn)(struct scsi_cmnd *));
94static int qla2xxx_eh_abort(struct scsi_cmnd *);
95static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
96static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
97static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
98static int qla2x00_loop_reset(scsi_qla_host_t *ha);
99static int qla2x00_device_reset(scsi_qla_host_t *, fc_port_t *);
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static struct scsi_host_template qla2x00_driver_template = {
102 .module = THIS_MODULE,
103 .name = "qla2xxx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .queuecommand = qla2x00_queuecommand,
105
106 .eh_abort_handler = qla2xxx_eh_abort,
107 .eh_device_reset_handler = qla2xxx_eh_device_reset,
108 .eh_bus_reset_handler = qla2xxx_eh_bus_reset,
109 .eh_host_reset_handler = qla2xxx_eh_host_reset,
110
111 .slave_configure = qla2xxx_slave_configure,
112
f4f051e2005-04-17 15:02:26 -0500113 .slave_alloc = qla2xxx_slave_alloc,
114 .slave_destroy = qla2xxx_slave_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .this_id = -1,
116 .cmd_per_lun = 3,
117 .use_clustering = ENABLE_CLUSTERING,
118 .sg_tablesize = SG_ALL,
119
120 /*
121 * The RISC allows for each command to transfer (2^32-1) bytes of data,
122 * which equates to 0x800000 sectors.
123 */
124 .max_sectors = 0xFFFF,
125};
126
127static struct scsi_transport_template *qla2xxx_transport_template = NULL;
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/* TODO Convert to inlines
130 *
131 * Timer routines
132 */
133#define WATCH_INTERVAL 1 /* number of seconds */
134
135static void qla2x00_timer(scsi_qla_host_t *);
136
137static __inline__ void qla2x00_start_timer(scsi_qla_host_t *,
138 void *, unsigned long);
139static __inline__ void qla2x00_restart_timer(scsi_qla_host_t *, unsigned long);
140static __inline__ void qla2x00_stop_timer(scsi_qla_host_t *);
141
142static inline void
143qla2x00_start_timer(scsi_qla_host_t *ha, void *func, unsigned long interval)
144{
145 init_timer(&ha->timer);
146 ha->timer.expires = jiffies + interval * HZ;
147 ha->timer.data = (unsigned long)ha;
148 ha->timer.function = (void (*)(unsigned long))func;
149 add_timer(&ha->timer);
150 ha->timer_active = 1;
151}
152
153static inline void
154qla2x00_restart_timer(scsi_qla_host_t *ha, unsigned long interval)
155{
156 mod_timer(&ha->timer, jiffies + interval * HZ);
157}
158
159static __inline__ void
160qla2x00_stop_timer(scsi_qla_host_t *ha)
161{
162 del_timer_sync(&ha->timer);
163 ha->timer_active = 0;
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static int qla2x00_do_dpc(void *data);
167
168static void qla2x00_rst_aen(scsi_qla_host_t *);
169
170static uint8_t qla2x00_mem_alloc(scsi_qla_host_t *);
171static void qla2x00_mem_free(scsi_qla_host_t *ha);
172static int qla2x00_allocate_sp_pool( scsi_qla_host_t *ha);
173static void qla2x00_free_sp_pool(scsi_qla_host_t *ha);
f4f051e2005-04-17 15:02:26 -0500174static srb_t *qla2x00_get_new_sp(scsi_qla_host_t *);
175static void qla2x00_sp_free_dma(scsi_qla_host_t *, srb_t *);
176void qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/* -------------------------------------------------------------------------- */
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static char *
181qla2x00_get_pci_info_str(struct scsi_qla_host *ha, char *str)
182{
183 static char *pci_bus_modes[] = {
184 "33", "66", "100", "133",
185 };
186 uint16_t pci_bus;
187
188 strcpy(str, "PCI");
189 pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
190 if (pci_bus) {
191 strcat(str, "-X (");
192 strcat(str, pci_bus_modes[pci_bus]);
193 } else {
194 pci_bus = (ha->pci_attr & BIT_8) >> 8;
195 strcat(str, " (");
196 strcat(str, pci_bus_modes[pci_bus]);
197 }
198 strcat(str, " MHz)");
199
200 return (str);
201}
202
203char *
204qla2x00_get_fw_version_str(struct scsi_qla_host *ha, char *str)
205{
206 char un_str[10];
207
208 sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
209 ha->fw_minor_version,
210 ha->fw_subminor_version);
211
212 if (ha->fw_attributes & BIT_9) {
213 strcat(str, "FLX");
214 return (str);
215 }
216
217 switch (ha->fw_attributes & 0xFF) {
218 case 0x7:
219 strcat(str, "EF");
220 break;
221 case 0x17:
222 strcat(str, "TP");
223 break;
224 case 0x37:
225 strcat(str, "IP");
226 break;
227 case 0x77:
228 strcat(str, "VI");
229 break;
230 default:
231 sprintf(un_str, "(%x)", ha->fw_attributes);
232 strcat(str, un_str);
233 break;
234 }
235 if (ha->fw_attributes & 0x100)
236 strcat(str, "X");
237
238 return (str);
239}
240
241/**************************************************************************
242* qla2x00_queuecommand
243*
244* Description:
245* Queue a command to the controller.
246*
247* Input:
248* cmd - pointer to Scsi cmd structure
249* fn - pointer to Scsi done function
250*
251* Returns:
252* 0 - Always
253*
254* Note:
255* The mid-level driver tries to ensures that queuecommand never gets invoked
256* concurrently with itself or the interrupt handler (although the
257* interrupt handler may call this routine as part of request-completion
258* handling).
259**************************************************************************/
260static int
f4f051e2005-04-17 15:02:26 -0500261qla2x00_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
f4f051e2005-04-17 15:02:26 -0500263 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
bdf79622005-04-17 15:06:53 -0500264 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
f4f051e2005-04-17 15:02:26 -0500265 srb_t *sp;
266 int rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
bdf79622005-04-17 15:06:53 -0500268 if (!fcport) {
f4f051e2005-04-17 15:02:26 -0500269 cmd->result = DID_NO_CONNECT << 16;
270 goto qc_fail_command;
271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
f4f051e2005-04-17 15:02:26 -0500273 if (atomic_read(&fcport->state) != FCS_ONLINE) {
274 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
275 atomic_read(&ha->loop_state) == LOOP_DEAD) {
276 cmd->result = DID_NO_CONNECT << 16;
277 goto qc_fail_command;
278 }
279 goto qc_host_busy;
280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 spin_unlock_irq(ha->host->host_lock);
283
f4f051e2005-04-17 15:02:26 -0500284 /* Allocate a command packet from the "sp" pool. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if ((sp = qla2x00_get_new_sp(ha)) == NULL) {
f4f051e2005-04-17 15:02:26 -0500286 goto qc_host_busy_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288
f4f051e2005-04-17 15:02:26 -0500289 sp->ha = ha;
bdf79622005-04-17 15:06:53 -0500290 sp->fcport = fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 sp->cmd = cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 sp->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
f4f051e2005-04-17 15:02:26 -0500294 CMD_SP(cmd) = (void *)sp;
295 cmd->scsi_done = done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
f4f051e2005-04-17 15:02:26 -0500297 rval = qla2x00_start_scsi(sp);
298 if (rval != QLA_SUCCESS)
299 goto qc_host_busy_free_sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
f4f051e2005-04-17 15:02:26 -0500301 /* Manage unprocessed RIO/ZIO commands in response queue. */
302 if (ha->flags.online && ha->flags.process_response_queue &&
303 ha->response_ring_ptr->signature != RESPONSE_PROCESSED) {
304 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
f4f051e2005-04-17 15:02:26 -0500306 spin_lock_irqsave(&ha->hardware_lock, flags);
307 qla2x00_process_response_queue(ha);
308 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 spin_lock_irq(ha->host->host_lock);
312
f4f051e2005-04-17 15:02:26 -0500313 return 0;
314
315qc_host_busy_free_sp:
316 qla2x00_sp_free_dma(ha, sp);
317 CMD_SP(cmd) = NULL;
318 mempool_free(sp, ha->srb_mempool);
319
320qc_host_busy_lock:
321 spin_lock_irq(ha->host->host_lock);
322
323qc_host_busy:
324 return SCSI_MLQUEUE_HOST_BUSY;
325
326qc_fail_command:
327 done(cmd);
328
329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332/*
333 * qla2x00_eh_wait_on_command
334 * Waits for the command to be returned by the Firmware for some
335 * max time.
336 *
337 * Input:
338 * ha = actual ha whose done queue will contain the command
339 * returned by firmware.
340 * cmd = Scsi Command to wait on.
341 * flag = Abort/Reset(Bus or Device Reset)
342 *
343 * Return:
344 * Not Found : 0
345 * Found : 1
346 */
347static int
348qla2x00_eh_wait_on_command(scsi_qla_host_t *ha, struct scsi_cmnd *cmd)
349{
350#define ABORT_POLLING_PERIOD HZ
f4f051e2005-04-17 15:02:26 -0500351#define ABORT_WAIT_ITER ((10 * HZ) / (ABORT_POLLING_PERIOD))
352 unsigned long wait_iter = ABORT_WAIT_ITER;
353 int ret = QLA_SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
f4f051e2005-04-17 15:02:26 -0500355 while (CMD_SP(cmd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 set_current_state(TASK_UNINTERRUPTIBLE);
357 schedule_timeout(ABORT_POLLING_PERIOD);
358
f4f051e2005-04-17 15:02:26 -0500359 if (--wait_iter)
360 break;
361 }
362 if (CMD_SP(cmd))
363 ret = QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
f4f051e2005-04-17 15:02:26 -0500365 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
368/*
369 * qla2x00_wait_for_hba_online
370 * Wait till the HBA is online after going through
371 * <= MAX_RETRIES_OF_ISP_ABORT or
372 * finally HBA is disabled ie marked offline
373 *
374 * Input:
375 * ha - pointer to host adapter structure
376 *
377 * Note:
378 * Does context switching-Release SPIN_LOCK
379 * (if any) before calling this routine.
380 *
381 * Return:
382 * Success (Adapter is online) : 0
383 * Failed (Adapter is offline/disabled) : 1
384 */
385static int
386qla2x00_wait_for_hba_online(scsi_qla_host_t *ha)
387{
388 int return_status;
389 unsigned long wait_online;
390
391 wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ);
392 while (((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) ||
393 test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
394 test_bit(ISP_ABORT_RETRY, &ha->dpc_flags) ||
395 ha->dpc_active) && time_before(jiffies, wait_online)) {
396
397 msleep(1000);
398 }
399 if (ha->flags.online)
400 return_status = QLA_SUCCESS;
401 else
402 return_status = QLA_FUNCTION_FAILED;
403
404 DEBUG2(printk("%s return_status=%d\n",__func__,return_status));
405
406 return (return_status);
407}
408
409/*
410 * qla2x00_wait_for_loop_ready
411 * Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
412 * to be in LOOP_READY state.
413 * Input:
414 * ha - pointer to host adapter structure
415 *
416 * Note:
417 * Does context switching-Release SPIN_LOCK
418 * (if any) before calling this routine.
419 *
420 *
421 * Return:
422 * Success (LOOP_READY) : 0
423 * Failed (LOOP_NOT_READY) : 1
424 */
425static inline int
426qla2x00_wait_for_loop_ready(scsi_qla_host_t *ha)
427{
428 int return_status = QLA_SUCCESS;
429 unsigned long loop_timeout ;
430
431 /* wait for 5 min at the max for loop to be ready */
432 loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ);
433
434 while ((!atomic_read(&ha->loop_down_timer) &&
435 atomic_read(&ha->loop_state) == LOOP_DOWN) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 atomic_read(&ha->loop_state) != LOOP_READY) {
437 msleep(1000);
438 if (time_after_eq(jiffies, loop_timeout)) {
439 return_status = QLA_FUNCTION_FAILED;
440 break;
441 }
442 }
443 return (return_status);
444}
445
446/**************************************************************************
447* qla2xxx_eh_abort
448*
449* Description:
450* The abort function will abort the specified command.
451*
452* Input:
453* cmd = Linux SCSI command packet to be aborted.
454*
455* Returns:
456* Either SUCCESS or FAILED.
457*
458* Note:
459**************************************************************************/
460int
461qla2xxx_eh_abort(struct scsi_cmnd *cmd)
462{
f4f051e2005-04-17 15:02:26 -0500463 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
464 srb_t *sp;
465 int ret, i;
466 unsigned int id, lun;
467 unsigned long serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
f4f051e2005-04-17 15:02:26 -0500469 if (!CMD_SP(cmd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
f4f051e2005-04-17 15:02:26 -0500472 ret = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
f4f051e2005-04-17 15:02:26 -0500474 id = cmd->device->id;
475 lun = cmd->device->lun;
476 serial = cmd->serial_number;
477
478 /* Check active list for command command. */
f4f051e2005-04-17 15:02:26 -0500479 spin_lock(&ha->hardware_lock);
480 for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
481 sp = ha->outstanding_cmds[i];
482
483 if (sp == NULL)
484 continue;
485
486 if (sp->cmd != cmd)
487 continue;
488
489 DEBUG2(printk("%s(%ld): aborting sp %p from RISC. pid=%ld "
490 "sp->state=%x\n", __func__, ha->host_no, sp, serial,
491 sp->state));
492 DEBUG3(qla2x00_print_scsi_cmd(cmd);)
493
494 spin_unlock(&ha->hardware_lock);
495 if (qla2x00_abort_command(ha, sp)) {
496 DEBUG2(printk("%s(%ld): abort_command "
497 "mbx failed.\n", __func__, ha->host_no));
498 } else {
499 DEBUG3(printk("%s(%ld): abort_command "
500 "mbx success.\n", __func__, ha->host_no));
501 ret = SUCCESS;
502 }
503 spin_lock(&ha->hardware_lock);
504
505 break;
506 }
507
508 /* Wait for the command to be returned. */
509 if (ret == SUCCESS) {
510 spin_unlock(&ha->hardware_lock);
511 if (qla2x00_eh_wait_on_command(ha, cmd) != QLA_SUCCESS) {
512 qla_printk(KERN_ERR, ha,
513 "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
514 "%x.\n", ha->host_no, id, lun, serial, ret);
515 }
516 spin_lock(&ha->hardware_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
f4f051e2005-04-17 15:02:26 -0500519 qla_printk(KERN_INFO, ha,
520 "scsi(%ld:%d:%d): Abort command issued -- %lx %x.\n", ha->host_no,
521 id, lun, serial, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
f4f051e2005-04-17 15:02:26 -0500523 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
526/**************************************************************************
527* qla2x00_eh_wait_for_pending_target_commands
528*
529* Description:
530* Waits for all the commands to come back from the specified target.
531*
532* Input:
533* ha - pointer to scsi_qla_host structure.
534* t - target
535* Returns:
536* Either SUCCESS or FAILED.
537*
538* Note:
539**************************************************************************/
540static int
541qla2x00_eh_wait_for_pending_target_commands(scsi_qla_host_t *ha, unsigned int t)
542{
543 int cnt;
544 int status;
545 srb_t *sp;
546 struct scsi_cmnd *cmd;
547
548 status = 0;
549
550 /*
551 * Waiting for all commands for the designated target in the active
552 * array
553 */
554 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
555 spin_lock(&ha->hardware_lock);
556 sp = ha->outstanding_cmds[cnt];
557 if (sp) {
558 cmd = sp->cmd;
559 spin_unlock(&ha->hardware_lock);
560 if (cmd->device->id == t) {
561 if (!qla2x00_eh_wait_on_command(ha, cmd)) {
562 status = 1;
563 break;
564 }
565 }
f4f051e2005-04-17 15:02:26 -0500566 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 spin_unlock(&ha->hardware_lock);
568 }
569 }
570 return (status);
571}
572
573
574/**************************************************************************
575* qla2xxx_eh_device_reset
576*
577* Description:
578* The device reset function will reset the target and abort any
579* executing commands.
580*
581* NOTE: The use of SP is undefined within this context. Do *NOT*
582* attempt to use this value, even if you determine it is
583* non-null.
584*
585* Input:
586* cmd = Linux SCSI command packet of the command that cause the
587* bus device reset.
588*
589* Returns:
590* SUCCESS/FAILURE (defined as macro in scsi.h).
591*
592**************************************************************************/
593int
594qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
595{
f4f051e2005-04-17 15:02:26 -0500596 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
bdf79622005-04-17 15:06:53 -0500597 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
f4f051e2005-04-17 15:02:26 -0500598 srb_t *sp;
599 int ret;
600 unsigned int id, lun;
601 unsigned long serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
f4f051e2005-04-17 15:02:26 -0500603 ret = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
f4f051e2005-04-17 15:02:26 -0500605 id = cmd->device->id;
606 lun = cmd->device->lun;
607 serial = cmd->serial_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
f4f051e2005-04-17 15:02:26 -0500609 sp = (srb_t *) CMD_SP(cmd);
bdf79622005-04-17 15:06:53 -0500610 if (!sp || !fcport)
f4f051e2005-04-17 15:02:26 -0500611 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 qla_printk(KERN_INFO, ha,
f4f051e2005-04-17 15:02:26 -0500614 "scsi(%ld:%d:%d): DEVICE RESET ISSUED.\n", ha->host_no, id, lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 spin_unlock_irq(ha->host->host_lock);
617
618 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 spin_lock_irq(ha->host->host_lock);
620 goto eh_dev_reset_done;
621 }
622
623 if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
f4f051e2005-04-17 15:02:26 -0500624 if (qla2x00_device_reset(ha, fcport) == 0)
625 ret = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627#if defined(LOGOUT_AFTER_DEVICE_RESET)
f4f051e2005-04-17 15:02:26 -0500628 if (ret == SUCCESS) {
629 if (fcport->flags & FC_FABRIC_DEVICE) {
630 qla2x00_fabric_logout(ha, fcport->loop_id);
631 qla2x00_mark_device_lost(ha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633 }
634#endif
635 } else {
636 DEBUG2(printk(KERN_INFO
637 "%s failed: loop not ready\n",__func__);)
638 }
639
f4f051e2005-04-17 15:02:26 -0500640 if (ret == FAILED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 DEBUG3(printk("%s(%ld): device reset failed\n",
642 __func__, ha->host_no));
643 qla_printk(KERN_INFO, ha, "%s: device reset failed\n",
644 __func__);
645
646 goto eh_dev_reset_done;
647 }
648
649 /*
650 * If we are coming down the EH path, wait for all commands to
651 * complete for the device.
652 */
653 if (cmd->device->host->eh_active) {
f4f051e2005-04-17 15:02:26 -0500654 if (qla2x00_eh_wait_for_pending_target_commands(ha, id))
655 ret = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
f4f051e2005-04-17 15:02:26 -0500657 if (ret == FAILED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 DEBUG3(printk("%s(%ld): failed while waiting for "
659 "commands\n", __func__, ha->host_no));
660 qla_printk(KERN_INFO, ha,
661 "%s: failed while waiting for commands\n",
662 __func__);
663
664 goto eh_dev_reset_done;
665 }
666 }
667
668 qla_printk(KERN_INFO, ha,
f4f051e2005-04-17 15:02:26 -0500669 "scsi(%ld:%d:%d): DEVICE RESET SUCCEEDED.\n", ha->host_no, id, lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671eh_dev_reset_done:
f4f051e2005-04-17 15:02:26 -0500672 spin_lock_irq(ha->host->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
f4f051e2005-04-17 15:02:26 -0500674 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677/**************************************************************************
678* qla2x00_eh_wait_for_pending_commands
679*
680* Description:
681* Waits for all the commands to come back from the specified host.
682*
683* Input:
684* ha - pointer to scsi_qla_host structure.
685*
686* Returns:
687* 1 : SUCCESS
688* 0 : FAILED
689*
690* Note:
691**************************************************************************/
692static int
693qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *ha)
694{
695 int cnt;
696 int status;
697 srb_t *sp;
698 struct scsi_cmnd *cmd;
699
700 status = 1;
701
702 /*
703 * Waiting for all commands for the designated target in the active
704 * array
705 */
706 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
707 spin_lock(&ha->hardware_lock);
708 sp = ha->outstanding_cmds[cnt];
709 if (sp) {
710 cmd = sp->cmd;
711 spin_unlock(&ha->hardware_lock);
712 status = qla2x00_eh_wait_on_command(ha, cmd);
713 if (status == 0)
714 break;
715 }
716 else {
717 spin_unlock(&ha->hardware_lock);
718 }
719 }
720 return (status);
721}
722
723
724/**************************************************************************
725* qla2xxx_eh_bus_reset
726*
727* Description:
728* The bus reset function will reset the bus and abort any executing
729* commands.
730*
731* Input:
732* cmd = Linux SCSI command packet of the command that cause the
733* bus reset.
734*
735* Returns:
736* SUCCESS/FAILURE (defined as macro in scsi.h).
737*
738**************************************************************************/
739int
740qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
741{
f4f051e2005-04-17 15:02:26 -0500742 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
bdf79622005-04-17 15:06:53 -0500743 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 srb_t *sp;
f4f051e2005-04-17 15:02:26 -0500745 int ret;
746 unsigned int id, lun;
747 unsigned long serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
f4f051e2005-04-17 15:02:26 -0500749 ret = FAILED;
750
751 id = cmd->device->id;
752 lun = cmd->device->lun;
753 serial = cmd->serial_number;
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 sp = (srb_t *) CMD_SP(cmd);
bdf79622005-04-17 15:06:53 -0500756 if (!sp || !fcport)
f4f051e2005-04-17 15:02:26 -0500757 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 qla_printk(KERN_INFO, ha,
f4f051e2005-04-17 15:02:26 -0500760 "scsi(%ld:%d:%d): LOOP RESET ISSUED.\n", ha->host_no, id, lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 spin_unlock_irq(ha->host->host_lock);
763
764 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
765 DEBUG2(printk("%s failed:board disabled\n",__func__));
f4f051e2005-04-17 15:02:26 -0500766 goto eh_bus_reset_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768
769 if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
f4f051e2005-04-17 15:02:26 -0500770 if (qla2x00_loop_reset(ha) == QLA_SUCCESS)
771 ret = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
f4f051e2005-04-17 15:02:26 -0500773 if (ret == FAILED)
774 goto eh_bus_reset_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 /* Waiting for our command in done_queue to be returned to OS.*/
777 if (cmd->device->host->eh_active)
778 if (!qla2x00_eh_wait_for_pending_commands(ha))
f4f051e2005-04-17 15:02:26 -0500779 ret = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
f4f051e2005-04-17 15:02:26 -0500781eh_bus_reset_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
f4f051e2005-04-17 15:02:26 -0500783 (ret == FAILED) ? "failed" : "succeded");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
f4f051e2005-04-17 15:02:26 -0500785 spin_lock_irq(ha->host->host_lock);
786
787 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
790/**************************************************************************
791* qla2xxx_eh_host_reset
792*
793* Description:
794* The reset function will reset the Adapter.
795*
796* Input:
797* cmd = Linux SCSI command packet of the command that cause the
798* adapter reset.
799*
800* Returns:
801* Either SUCCESS or FAILED.
802*
803* Note:
804**************************************************************************/
805int
806qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
807{
f4f051e2005-04-17 15:02:26 -0500808 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
bdf79622005-04-17 15:06:53 -0500809 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
f4f051e2005-04-17 15:02:26 -0500810 srb_t *sp;
811 int ret;
812 unsigned int id, lun;
813 unsigned long serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
f4f051e2005-04-17 15:02:26 -0500815 ret = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
f4f051e2005-04-17 15:02:26 -0500817 id = cmd->device->id;
818 lun = cmd->device->lun;
819 serial = cmd->serial_number;
820
821 sp = (srb_t *) CMD_SP(cmd);
bdf79622005-04-17 15:06:53 -0500822 if (!sp || !fcport)
f4f051e2005-04-17 15:02:26 -0500823 return ret;
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 qla_printk(KERN_INFO, ha,
f4f051e2005-04-17 15:02:26 -0500826 "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no, id, lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 spin_unlock_irq(ha->host->host_lock);
829
830 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
f4f051e2005-04-17 15:02:26 -0500831 goto eh_host_reset_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 /*
834 * Fixme-may be dpc thread is active and processing
835 * loop_resync,so wait a while for it to
836 * be completed and then issue big hammer.Otherwise
837 * it may cause I/O failure as big hammer marks the
838 * devices as lost kicking of the port_down_timer
839 * while dpc is stuck for the mailbox to complete.
840 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 qla2x00_wait_for_loop_ready(ha);
842 set_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
843 if (qla2x00_abort_isp(ha)) {
844 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
845 /* failed. schedule dpc to try */
846 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
847
848 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
f4f051e2005-04-17 15:02:26 -0500849 goto eh_host_reset_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /* Waiting for our command in done_queue to be returned to OS.*/
f4f051e2005-04-17 15:02:26 -0500854 if (qla2x00_eh_wait_for_pending_commands(ha))
855 ret = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
f4f051e2005-04-17 15:02:26 -0500857eh_host_reset_lock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 spin_lock_irq(ha->host->host_lock);
859
f4f051e2005-04-17 15:02:26 -0500860 qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
861 (ret == FAILED) ? "failed" : "succeded");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
f4f051e2005-04-17 15:02:26 -0500863 return ret;
864}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866/*
867* qla2x00_loop_reset
868* Issue loop reset.
869*
870* Input:
871* ha = adapter block pointer.
872*
873* Returns:
874* 0 = success
875*/
876static int
877qla2x00_loop_reset(scsi_qla_host_t *ha)
878{
879 int status = QLA_SUCCESS;
bdf79622005-04-17 15:06:53 -0500880 struct fc_port *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 if (ha->flags.enable_lip_reset) {
883 status = qla2x00_lip_reset(ha);
884 }
885
886 if (status == QLA_SUCCESS && ha->flags.enable_target_reset) {
bdf79622005-04-17 15:06:53 -0500887 list_for_each_entry(fcport, &ha->fcports, list) {
888 if (fcport->port_type != FCT_TARGET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 continue;
890
bdf79622005-04-17 15:06:53 -0500891 status = qla2x00_target_reset(ha, fcport);
892 if (status != QLA_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895 }
896
897 if (status == QLA_SUCCESS &&
898 ((!ha->flags.enable_target_reset &&
899 !ha->flags.enable_lip_reset) ||
900 ha->flags.enable_lip_full_login)) {
901
902 status = qla2x00_full_login_lip(ha);
903 }
904
905 /* Issue marker command only when we are going to start the I/O */
906 ha->marker_needed = 1;
907
908 if (status) {
909 /* Empty */
910 DEBUG2_3(printk("%s(%ld): **** FAILED ****\n",
911 __func__,
912 ha->host_no);)
913 } else {
914 /* Empty */
915 DEBUG3(printk("%s(%ld): exiting normally.\n",
916 __func__,
917 ha->host_no);)
918 }
919
920 return(status);
921}
922
923/*
924 * qla2x00_device_reset
925 * Issue bus device reset message to the target.
926 *
927 * Input:
928 * ha = adapter block pointer.
929 * t = SCSI ID.
930 * TARGET_QUEUE_LOCK must be released.
931 * ADAPTER_STATE_LOCK must be released.
932 *
933 * Context:
934 * Kernel context.
935 */
936static int
937qla2x00_device_reset(scsi_qla_host_t *ha, fc_port_t *reset_fcport)
938{
939 /* Abort Target command will clear Reservation */
940 return qla2x00_abort_target(reset_fcport);
941}
942
f4f051e2005-04-17 15:02:26 -0500943static int
944qla2xxx_slave_alloc(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
946 scsi_qla_host_t *ha = to_qla_host(sdev->host);
bdf79622005-04-17 15:06:53 -0500947 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
948 fc_port_t *fcport;
949 int found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
bdf79622005-04-17 15:06:53 -0500951 if (!rport)
f4f051e2005-04-17 15:02:26 -0500952 return -ENXIO;
953
bdf79622005-04-17 15:06:53 -0500954 found = 0;
955 list_for_each_entry(fcport, &ha->fcports, list) {
956 if (rport->port_name ==
957 be64_to_cpu(*(uint64_t *)fcport->port_name)) {
958 found++;
959 break;
960 }
bdf79622005-04-17 15:06:53 -0500961 }
962 if (!found)
963 return -ENXIO;
964
965 sdev->hostdata = fcport;
f4f051e2005-04-17 15:02:26 -0500966
967 return 0;
968}
969
970static int
971qla2xxx_slave_configure(struct scsi_device *sdev)
972{
8482e112005-04-17 15:04:54 -0500973 scsi_qla_host_t *ha = to_qla_host(sdev->host);
974 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
975
f4f051e2005-04-17 15:02:26 -0500976 if (sdev->tagged_supported)
977 scsi_activate_tcq(sdev, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 else
f4f051e2005-04-17 15:02:26 -0500979 scsi_deactivate_tcq(sdev, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
8482e112005-04-17 15:04:54 -0500981 rport->dev_loss_tmo = ha->port_down_retry_count + 5;
982
f4f051e2005-04-17 15:02:26 -0500983 return 0;
984}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
f4f051e2005-04-17 15:02:26 -0500986static void
987qla2xxx_slave_destroy(struct scsi_device *sdev)
988{
989 sdev->hostdata = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
992/**
993 * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
994 * @ha: HA context
995 *
996 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
997 * supported addressing method.
998 */
999static void
1000qla2x00_config_dma_addressing(scsi_qla_host_t *ha)
1001{
1002 /* Assume 32bit DMA address */
1003 ha->flags.enable_64bit_addressing = 0;
1004 ha->calc_request_entries = qla2x00_calc_iocbs_32;
1005 ha->build_scsi_iocbs = qla2x00_build_scsi_iocbs_32;
1006
1007 /*
1008 * Given the two variants pci_set_dma_mask(), allow the compiler to
1009 * assist in setting the proper dma mask.
1010 */
1011 if (sizeof(dma_addr_t) > 4) {
1012 if (pci_set_dma_mask(ha->pdev, DMA_64BIT_MASK) == 0) {
1013 ha->flags.enable_64bit_addressing = 1;
1014 ha->calc_request_entries = qla2x00_calc_iocbs_64;
1015 ha->build_scsi_iocbs = qla2x00_build_scsi_iocbs_64;
1016
1017 if (pci_set_consistent_dma_mask(ha->pdev,
1018 DMA_64BIT_MASK)) {
1019 qla_printk(KERN_DEBUG, ha,
1020 "Failed to set 64 bit PCI consistent mask; "
1021 "using 32 bit.\n");
1022 pci_set_consistent_dma_mask(ha->pdev,
1023 DMA_32BIT_MASK);
1024 }
1025 } else {
1026 qla_printk(KERN_DEBUG, ha,
1027 "Failed to set 64 bit PCI DMA mask, falling back "
1028 "to 32 bit MASK.\n");
1029 pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
1030 }
1031 } else {
1032 pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
1033 }
1034}
1035
1036static int
1037qla2x00_iospace_config(scsi_qla_host_t *ha)
1038{
1039 unsigned long pio, pio_len, pio_flags;
1040 unsigned long mmio, mmio_len, mmio_flags;
1041
1042 /* We only need PIO for Flash operations on ISP2312 v2 chips. */
1043 pio = pci_resource_start(ha->pdev, 0);
1044 pio_len = pci_resource_len(ha->pdev, 0);
1045 pio_flags = pci_resource_flags(ha->pdev, 0);
1046 if (pio_flags & IORESOURCE_IO) {
1047 if (pio_len < MIN_IOBASE_LEN) {
1048 qla_printk(KERN_WARNING, ha,
1049 "Invalid PCI I/O region size (%s)...\n",
1050 pci_name(ha->pdev));
1051 pio = 0;
1052 }
1053 } else {
1054 qla_printk(KERN_WARNING, ha,
1055 "region #0 not a PIO resource (%s)...\n",
1056 pci_name(ha->pdev));
1057 pio = 0;
1058 }
1059
1060 /* Use MMIO operations for all accesses. */
1061 mmio = pci_resource_start(ha->pdev, 1);
1062 mmio_len = pci_resource_len(ha->pdev, 1);
1063 mmio_flags = pci_resource_flags(ha->pdev, 1);
1064
1065 if (!(mmio_flags & IORESOURCE_MEM)) {
1066 qla_printk(KERN_ERR, ha,
1067 "region #0 not an MMIO resource (%s), aborting\n",
1068 pci_name(ha->pdev));
1069 goto iospace_error_exit;
1070 }
1071 if (mmio_len < MIN_IOBASE_LEN) {
1072 qla_printk(KERN_ERR, ha,
1073 "Invalid PCI mem region size (%s), aborting\n",
1074 pci_name(ha->pdev));
1075 goto iospace_error_exit;
1076 }
1077
1078 if (pci_request_regions(ha->pdev, ha->brd_info->drv_name)) {
1079 qla_printk(KERN_WARNING, ha,
1080 "Failed to reserve PIO/MMIO regions (%s)\n",
1081 pci_name(ha->pdev));
1082
1083 goto iospace_error_exit;
1084 }
1085
1086 ha->pio_address = pio;
1087 ha->pio_length = pio_len;
1088 ha->iobase = ioremap(mmio, MIN_IOBASE_LEN);
1089 if (!ha->iobase) {
1090 qla_printk(KERN_ERR, ha,
1091 "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
1092
1093 goto iospace_error_exit;
1094 }
1095
1096 return (0);
1097
1098iospace_error_exit:
1099 return (-ENOMEM);
1100}
1101
1102/*
1103 * PCI driver interface
1104 */
1105int qla2x00_probe_one(struct pci_dev *pdev, struct qla_board_info *brd_info)
1106{
1107 int ret;
1108 device_reg_t __iomem *reg;
1109 struct Scsi_Host *host;
1110 scsi_qla_host_t *ha;
1111 unsigned long flags = 0;
1112 unsigned long wait_switch = 0;
1113 char pci_info[20];
1114 char fw_str[30];
8482e112005-04-17 15:04:54 -05001115 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 if (pci_enable_device(pdev))
1118 return -1;
1119
1120 host = scsi_host_alloc(&qla2x00_driver_template,
1121 sizeof(scsi_qla_host_t));
1122 if (host == NULL) {
1123 printk(KERN_WARNING
1124 "qla2xxx: Couldn't allocate host from scsi layer!\n");
1125 goto probe_disable_device;
1126 }
1127
1128 /* Clear our data area */
1129 ha = (scsi_qla_host_t *)host->hostdata;
1130 memset(ha, 0, sizeof(scsi_qla_host_t));
1131
1132 ha->pdev = pdev;
1133 ha->host = host;
1134 ha->host_no = host->host_no;
1135 ha->brd_info = brd_info;
1136 sprintf(ha->host_str, "%s_%ld", ha->brd_info->drv_name, ha->host_no);
1137
1138 /* Configure PCI I/O space */
1139 ret = qla2x00_iospace_config(ha);
1140 if (ret != 0) {
8482e112005-04-17 15:04:54 -05001141 goto probe_alloc_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143
1144 /* Sanitize the information from PCI BIOS. */
1145 host->irq = pdev->irq;
1146
1147 qla_printk(KERN_INFO, ha,
1148 "Found an %s, irq %d, iobase 0x%p\n", ha->brd_info->isp_name,
1149 host->irq, ha->iobase);
1150
1151 spin_lock_init(&ha->hardware_lock);
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 ha->prev_topology = 0;
1154 ha->ports = MAX_BUSES;
1155
1156 if (IS_QLA2100(ha)) {
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001157 host->max_id = MAX_TARGETS_2100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
1159 ha->request_q_length = REQUEST_ENTRY_CNT_2100;
1160 ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1161 ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
1162 host->sg_tablesize = 32;
1163 } else if (IS_QLA2200(ha)) {
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001164 host->max_id = MAX_TARGETS_2200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1166 ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1167 ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1168 ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
1169 } else /*if (IS_QLA2300(ha))*/ {
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001170 host->max_id = MAX_TARGETS_2200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1172 ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1173 ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
1174 ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
1175 }
1176 host->can_queue = ha->request_q_length + 128;
1177
1178 /* load the F/W, read paramaters, and init the H/W */
1179 ha->instance = num_hosts;
1180
1181 init_MUTEX(&ha->mbx_cmd_sem);
1182 init_MUTEX_LOCKED(&ha->mbx_intr_sem);
1183
1184 INIT_LIST_HEAD(&ha->list);
1185 INIT_LIST_HEAD(&ha->fcports);
1186 INIT_LIST_HEAD(&ha->rscn_fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 /*
1189 * These locks are used to prevent more than one CPU
1190 * from modifying the queue at the same time. The
1191 * higher level "host_lock" will reduce most
1192 * contention for these locks.
1193 */
1194 spin_lock_init(&ha->mbx_reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 ha->dpc_pid = -1;
1197 init_completion(&ha->dpc_inited);
1198 init_completion(&ha->dpc_exited);
1199
1200 qla2x00_config_dma_addressing(ha);
1201 if (qla2x00_mem_alloc(ha)) {
1202 qla_printk(KERN_WARNING, ha,
1203 "[ERROR] Failed to allocate memory for adapter\n");
1204
8482e112005-04-17 15:04:54 -05001205 goto probe_alloc_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 }
1207
8482e112005-04-17 15:04:54 -05001208 pci_set_drvdata(pdev, ha);
1209 host->this_id = 255;
1210 host->cmd_per_lun = 3;
1211 host->unique_id = ha->instance;
1212 host->max_cmd_len = MAX_CMDSZ;
1213 host->max_channel = ha->ports - 1;
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001214 host->max_lun = MAX_LUNS;
8482e112005-04-17 15:04:54 -05001215 host->transportt = qla2xxx_transport_template;
1216 if (scsi_add_host(host, &pdev->dev))
1217 goto probe_alloc_failed;
1218
1219 qla2x00_alloc_sysfs_attr(ha);
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (qla2x00_initialize_adapter(ha) &&
1222 !(ha->device_flags & DFLG_NO_CABLE)) {
1223
1224 qla_printk(KERN_WARNING, ha,
1225 "Failed to initialize adapter\n");
1226
1227 DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
1228 "Adapter flags %x.\n",
1229 ha->host_no, ha->device_flags));
1230
1231 goto probe_failed;
1232 }
1233
8482e112005-04-17 15:04:54 -05001234 qla2x00_init_host_attr(ha);
bdf79622005-04-17 15:06:53 -05001235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /*
1237 * Startup the kernel thread for this host adapter
1238 */
1239 ha->dpc_should_die = 0;
1240 ha->dpc_pid = kernel_thread(qla2x00_do_dpc, ha, 0);
1241 if (ha->dpc_pid < 0) {
1242 qla_printk(KERN_WARNING, ha,
1243 "Unable to start DPC thread!\n");
1244
1245 goto probe_failed;
1246 }
1247 wait_for_completion(&ha->dpc_inited);
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1250 ret = request_irq(host->irq, qla2100_intr_handler,
1251 SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
1252 else
1253 ret = request_irq(host->irq, qla2300_intr_handler,
1254 SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
1255 if (ret != 0) {
1256 qla_printk(KERN_WARNING, ha,
1257 "Failed to reserve interrupt %d already in use.\n",
1258 host->irq);
1259 goto probe_failed;
1260 }
1261
1262 /* Initialized the timer */
1263 qla2x00_start_timer(ha, qla2x00_timer, WATCH_INTERVAL);
1264
1265 DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
1266 ha->host_no, ha));
1267
1268 reg = ha->iobase;
1269
1270 /* Disable ISP interrupts. */
1271 qla2x00_disable_intrs(ha);
1272
1273 /* Ensure mailbox registers are free. */
1274 spin_lock_irqsave(&ha->hardware_lock, flags);
1275 WRT_REG_WORD(&reg->semaphore, 0);
1276 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
1277 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
1278
1279 /* Enable proper parity */
1280 if (!IS_QLA2100(ha) && !IS_QLA2200(ha)) {
1281 if (IS_QLA2300(ha))
1282 /* SRAM parity */
1283 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x1));
1284 else
1285 /* SRAM, Instruction RAM and GP RAM parity */
1286 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x7));
1287 }
1288 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1289
1290 /* Enable chip interrupts. */
1291 qla2x00_enable_intrs(ha);
1292
1293 /* v2.19.5b6 */
1294 /*
1295 * Wait around max loop_reset_delay secs for the devices to come
1296 * on-line. We don't want Linux scanning before we are ready.
1297 *
1298 */
1299 for (wait_switch = jiffies + (ha->loop_reset_delay * HZ);
1300 time_before(jiffies,wait_switch) &&
1301 !(ha->device_flags & (DFLG_NO_CABLE | DFLG_FABRIC_DEVICES))
1302 && (ha->device_flags & SWITCH_FOUND) ;) {
1303
1304 qla2x00_check_fabric_devices(ha);
1305
1306 msleep(10);
1307 }
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 ha->flags.init_done = 1;
1310 num_hosts++;
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 qla_printk(KERN_INFO, ha, "\n"
1313 " QLogic Fibre Channel HBA Driver: %s\n"
1314 " QLogic %s - %s\n"
1315 " %s: %s @ %s hdma%c, host#=%ld, fw=%s\n", qla2x00_version_str,
1316 ha->model_number, ha->model_desc ? ha->model_desc: "",
1317 ha->brd_info->isp_name, qla2x00_get_pci_info_str(ha, pci_info),
1318 pci_name(ha->pdev), ha->flags.enable_64bit_addressing ? '+': '-',
1319 ha->host_no, qla2x00_get_fw_version_str(ha, fw_str));
1320
8482e112005-04-17 15:04:54 -05001321 /* Go with fc_rport registration. */
1322 list_for_each_entry(fcport, &ha->fcports, list)
1323 qla2x00_reg_remote_port(ha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 return 0;
1326
1327probe_failed:
bdf79622005-04-17 15:06:53 -05001328 fc_remove_host(ha->host);
1329
8482e112005-04-17 15:04:54 -05001330 scsi_remove_host(host);
1331
1332probe_alloc_failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 qla2x00_free_device(ha);
1334
1335 scsi_host_put(host);
1336
1337probe_disable_device:
1338 pci_disable_device(pdev);
1339
1340 return -1;
1341}
1342EXPORT_SYMBOL_GPL(qla2x00_probe_one);
1343
1344void qla2x00_remove_one(struct pci_dev *pdev)
1345{
1346 scsi_qla_host_t *ha;
1347
1348 ha = pci_get_drvdata(pdev);
1349
8482e112005-04-17 15:04:54 -05001350 qla2x00_free_sysfs_attr(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
bdf79622005-04-17 15:06:53 -05001352 fc_remove_host(ha->host);
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 scsi_remove_host(ha->host);
1355
1356 qla2x00_free_device(ha);
1357
1358 scsi_host_put(ha->host);
1359
1360 pci_set_drvdata(pdev, NULL);
1361}
1362EXPORT_SYMBOL_GPL(qla2x00_remove_one);
1363
1364static void
1365qla2x00_free_device(scsi_qla_host_t *ha)
1366{
1367 int ret;
1368
1369 /* Abort any outstanding IO descriptors. */
1370 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1371 qla2x00_cancel_io_descriptors(ha);
1372
1373 /* turn-off interrupts on the card */
1374 if (ha->interrupts_on)
1375 qla2x00_disable_intrs(ha);
1376
1377 /* Disable timer */
1378 if (ha->timer_active)
1379 qla2x00_stop_timer(ha);
1380
1381 /* Kill the kernel thread for this host */
1382 if (ha->dpc_pid >= 0) {
1383 ha->dpc_should_die = 1;
1384 wmb();
1385 ret = kill_proc(ha->dpc_pid, SIGHUP, 1);
1386 if (ret) {
1387 qla_printk(KERN_ERR, ha,
1388 "Unable to signal DPC thread -- (%d)\n", ret);
1389
1390 /* TODO: SOMETHING MORE??? */
1391 } else {
1392 wait_for_completion(&ha->dpc_exited);
1393 }
1394 }
1395
1396 qla2x00_mem_free(ha);
1397
1398
1399 ha->flags.online = 0;
1400
1401 /* Detach interrupts */
1402 if (ha->pdev->irq)
1403 free_irq(ha->pdev->irq, ha);
1404
1405 /* release io space registers */
1406 if (ha->iobase)
1407 iounmap(ha->iobase);
1408 pci_release_regions(ha->pdev);
1409
1410 pci_disable_device(ha->pdev);
1411}
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 * qla2x00_mark_device_lost Updates fcport state when device goes offline.
1415 *
1416 * Input: ha = adapter block pointer. fcport = port structure pointer.
1417 *
1418 * Return: None.
1419 *
1420 * Context:
1421 */
1422void qla2x00_mark_device_lost(scsi_qla_host_t *ha, fc_port_t *fcport,
1423 int do_login)
1424{
8482e112005-04-17 15:04:54 -05001425 if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
1426 fc_remote_port_block(fcport->rport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 /*
1428 * We may need to retry the login, so don't change the state of the
1429 * port but do the retries.
1430 */
1431 if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
1432 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1433
1434 if (!do_login)
1435 return;
1436
1437 if (fcport->login_retry == 0) {
1438 fcport->login_retry = ha->login_retry_count;
1439 set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
1440
1441 DEBUG(printk("scsi(%ld): Port login retry: "
1442 "%02x%02x%02x%02x%02x%02x%02x%02x, "
1443 "id = 0x%04x retry cnt=%d\n",
1444 ha->host_no,
1445 fcport->port_name[0],
1446 fcport->port_name[1],
1447 fcport->port_name[2],
1448 fcport->port_name[3],
1449 fcport->port_name[4],
1450 fcport->port_name[5],
1451 fcport->port_name[6],
1452 fcport->port_name[7],
1453 fcport->loop_id,
1454 fcport->login_retry));
1455 }
1456}
1457
1458/*
1459 * qla2x00_mark_all_devices_lost
1460 * Updates fcport state when device goes offline.
1461 *
1462 * Input:
1463 * ha = adapter block pointer.
1464 * fcport = port structure pointer.
1465 *
1466 * Return:
1467 * None.
1468 *
1469 * Context:
1470 */
1471void
1472qla2x00_mark_all_devices_lost(scsi_qla_host_t *ha)
1473{
1474 fc_port_t *fcport;
1475
1476 list_for_each_entry(fcport, &ha->fcports, list) {
1477 if (fcport->port_type != FCT_TARGET)
1478 continue;
1479
1480 /*
1481 * No point in marking the device as lost, if the device is
1482 * already DEAD.
1483 */
1484 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
1485 continue;
8482e112005-04-17 15:04:54 -05001486 if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
1487 fc_remote_port_block(fcport->rport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1489 }
1490}
1491
1492/*
1493* qla2x00_mem_alloc
1494* Allocates adapter memory.
1495*
1496* Returns:
1497* 0 = success.
1498* 1 = failure.
1499*/
1500static uint8_t
1501qla2x00_mem_alloc(scsi_qla_host_t *ha)
1502{
1503 char name[16];
1504 uint8_t status = 1;
1505 int retry= 10;
1506
1507 do {
1508 /*
1509 * This will loop only once if everything goes well, else some
1510 * number of retries will be performed to get around a kernel
1511 * bug where available mem is not allocated until after a
1512 * little delay and a retry.
1513 */
1514 ha->request_ring = dma_alloc_coherent(&ha->pdev->dev,
1515 (ha->request_q_length + 1) * sizeof(request_t),
1516 &ha->request_dma, GFP_KERNEL);
1517 if (ha->request_ring == NULL) {
1518 qla_printk(KERN_WARNING, ha,
1519 "Memory Allocation failed - request_ring\n");
1520
1521 qla2x00_mem_free(ha);
1522 msleep(100);
1523
1524 continue;
1525 }
1526
1527 ha->response_ring = dma_alloc_coherent(&ha->pdev->dev,
1528 (ha->response_q_length + 1) * sizeof(response_t),
1529 &ha->response_dma, GFP_KERNEL);
1530 if (ha->response_ring == NULL) {
1531 qla_printk(KERN_WARNING, ha,
1532 "Memory Allocation failed - response_ring\n");
1533
1534 qla2x00_mem_free(ha);
1535 msleep(100);
1536
1537 continue;
1538 }
1539
1540 ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
1541 &ha->gid_list_dma, GFP_KERNEL);
1542 if (ha->gid_list == NULL) {
1543 qla_printk(KERN_WARNING, ha,
1544 "Memory Allocation failed - gid_list\n");
1545
1546 qla2x00_mem_free(ha);
1547 msleep(100);
1548
1549 continue;
1550 }
1551
1552 ha->rlc_rsp = dma_alloc_coherent(&ha->pdev->dev,
1553 sizeof(rpt_lun_cmd_rsp_t), &ha->rlc_rsp_dma, GFP_KERNEL);
1554 if (ha->rlc_rsp == NULL) {
1555 qla_printk(KERN_WARNING, ha,
1556 "Memory Allocation failed - rlc");
1557
1558 qla2x00_mem_free(ha);
1559 msleep(100);
1560
1561 continue;
1562 }
1563
1564 snprintf(name, sizeof(name), "qla2xxx_%ld", ha->host_no);
1565 ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
1566 DMA_POOL_SIZE, 8, 0);
1567 if (ha->s_dma_pool == NULL) {
1568 qla_printk(KERN_WARNING, ha,
1569 "Memory Allocation failed - s_dma_pool\n");
1570
1571 qla2x00_mem_free(ha);
1572 msleep(100);
1573
1574 continue;
1575 }
1576
1577 /* get consistent memory allocated for init control block */
1578 ha->init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1579 &ha->init_cb_dma);
1580 if (ha->init_cb == NULL) {
1581 qla_printk(KERN_WARNING, ha,
1582 "Memory Allocation failed - init_cb\n");
1583
1584 qla2x00_mem_free(ha);
1585 msleep(100);
1586
1587 continue;
1588 }
1589 memset(ha->init_cb, 0, sizeof(init_cb_t));
1590
1591 /* Get consistent memory allocated for Get Port Database cmd */
1592 ha->iodesc_pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1593 &ha->iodesc_pd_dma);
1594 if (ha->iodesc_pd == NULL) {
1595 /* error */
1596 qla_printk(KERN_WARNING, ha,
1597 "Memory Allocation failed - iodesc_pd\n");
1598
1599 qla2x00_mem_free(ha);
1600 msleep(100);
1601
1602 continue;
1603 }
1604 memset(ha->iodesc_pd, 0, PORT_DATABASE_SIZE);
1605
1606 /* Allocate ioctl related memory. */
1607 if (qla2x00_alloc_ioctl_mem(ha)) {
1608 qla_printk(KERN_WARNING, ha,
1609 "Memory Allocation failed - ioctl_mem\n");
1610
1611 qla2x00_mem_free(ha);
1612 msleep(100);
1613
1614 continue;
1615 }
1616
1617 if (qla2x00_allocate_sp_pool(ha)) {
1618 qla_printk(KERN_WARNING, ha,
1619 "Memory Allocation failed - "
1620 "qla2x00_allocate_sp_pool()\n");
1621
1622 qla2x00_mem_free(ha);
1623 msleep(100);
1624
1625 continue;
1626 }
1627
1628 /* Allocate memory for SNS commands */
1629 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1630 /* Get consistent memory allocated for SNS commands */
1631 ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
1632 sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma,
1633 GFP_KERNEL);
1634 if (ha->sns_cmd == NULL) {
1635 /* error */
1636 qla_printk(KERN_WARNING, ha,
1637 "Memory Allocation failed - sns_cmd\n");
1638
1639 qla2x00_mem_free(ha);
1640 msleep(100);
1641
1642 continue;
1643 }
1644 memset(ha->sns_cmd, 0, sizeof(struct sns_cmd_pkt));
1645 } else {
1646 /* Get consistent memory allocated for MS IOCB */
1647 ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1648 &ha->ms_iocb_dma);
1649 if (ha->ms_iocb == NULL) {
1650 /* error */
1651 qla_printk(KERN_WARNING, ha,
1652 "Memory Allocation failed - ms_iocb\n");
1653
1654 qla2x00_mem_free(ha);
1655 msleep(100);
1656
1657 continue;
1658 }
1659 memset(ha->ms_iocb, 0, sizeof(ms_iocb_entry_t));
1660
1661 /*
1662 * Get consistent memory allocated for CT SNS
1663 * commands
1664 */
1665 ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
1666 sizeof(struct ct_sns_pkt), &ha->ct_sns_dma,
1667 GFP_KERNEL);
1668 if (ha->ct_sns == NULL) {
1669 /* error */
1670 qla_printk(KERN_WARNING, ha,
1671 "Memory Allocation failed - ct_sns\n");
1672
1673 qla2x00_mem_free(ha);
1674 msleep(100);
1675
1676 continue;
1677 }
1678 memset(ha->ct_sns, 0, sizeof(struct ct_sns_pkt));
1679 }
1680
1681 /* Done all allocations without any error. */
1682 status = 0;
1683
1684 } while (retry-- && status != 0);
1685
1686 if (status) {
1687 printk(KERN_WARNING
1688 "%s(): **** FAILED ****\n", __func__);
1689 }
1690
1691 return(status);
1692}
1693
1694/*
1695* qla2x00_mem_free
1696* Frees all adapter allocated memory.
1697*
1698* Input:
1699* ha = adapter block pointer.
1700*/
1701static void
1702qla2x00_mem_free(scsi_qla_host_t *ha)
1703{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 struct list_head *fcpl, *fcptemp;
1705 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 unsigned long wtime;/* max wait time if mbx cmd is busy. */
1707
1708 if (ha == NULL) {
1709 /* error */
1710 DEBUG2(printk("%s(): ERROR invalid ha pointer.\n", __func__));
1711 return;
1712 }
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 /* Make sure all other threads are stopped. */
1715 wtime = 60 * HZ;
1716 while (ha->dpc_wait && wtime) {
1717 set_current_state(TASK_INTERRUPTIBLE);
1718 wtime = schedule_timeout(wtime);
1719 }
1720
1721 /* free ioctl memory */
1722 qla2x00_free_ioctl_mem(ha);
1723
1724 /* free sp pool */
1725 qla2x00_free_sp_pool(ha);
1726
1727 if (ha->sns_cmd)
1728 dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
1729 ha->sns_cmd, ha->sns_cmd_dma);
1730
1731 if (ha->ct_sns)
1732 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
1733 ha->ct_sns, ha->ct_sns_dma);
1734
1735 if (ha->ms_iocb)
1736 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
1737
1738 if (ha->iodesc_pd)
1739 dma_pool_free(ha->s_dma_pool, ha->iodesc_pd, ha->iodesc_pd_dma);
1740
1741 if (ha->init_cb)
1742 dma_pool_free(ha->s_dma_pool, ha->init_cb, ha->init_cb_dma);
1743
1744 if (ha->s_dma_pool)
1745 dma_pool_destroy(ha->s_dma_pool);
1746
1747 if (ha->rlc_rsp)
1748 dma_free_coherent(&ha->pdev->dev,
1749 sizeof(rpt_lun_cmd_rsp_t), ha->rlc_rsp,
1750 ha->rlc_rsp_dma);
1751
1752 if (ha->gid_list)
1753 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
1754 ha->gid_list_dma);
1755
1756 if (ha->response_ring)
1757 dma_free_coherent(&ha->pdev->dev,
1758 (ha->response_q_length + 1) * sizeof(response_t),
1759 ha->response_ring, ha->response_dma);
1760
1761 if (ha->request_ring)
1762 dma_free_coherent(&ha->pdev->dev,
1763 (ha->request_q_length + 1) * sizeof(request_t),
1764 ha->request_ring, ha->request_dma);
1765
1766 ha->sns_cmd = NULL;
1767 ha->sns_cmd_dma = 0;
1768 ha->ct_sns = NULL;
1769 ha->ct_sns_dma = 0;
1770 ha->ms_iocb = NULL;
1771 ha->ms_iocb_dma = 0;
1772 ha->iodesc_pd = NULL;
1773 ha->iodesc_pd_dma = 0;
1774 ha->init_cb = NULL;
1775 ha->init_cb_dma = 0;
1776
1777 ha->s_dma_pool = NULL;
1778
1779 ha->rlc_rsp = NULL;
1780 ha->rlc_rsp_dma = 0;
1781 ha->gid_list = NULL;
1782 ha->gid_list_dma = 0;
1783
1784 ha->response_ring = NULL;
1785 ha->response_dma = 0;
1786 ha->request_ring = NULL;
1787 ha->request_dma = 0;
1788
1789 list_for_each_safe(fcpl, fcptemp, &ha->fcports) {
1790 fcport = list_entry(fcpl, fc_port_t, list);
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 /* fc ports */
1793 list_del_init(&fcport->list);
1794 kfree(fcport);
1795 }
1796 INIT_LIST_HEAD(&ha->fcports);
1797
1798 if (ha->fw_dump)
1799 free_pages((unsigned long)ha->fw_dump, ha->fw_dump_order);
1800
1801 if (ha->fw_dump_buffer)
1802 vfree(ha->fw_dump_buffer);
1803
1804 ha->fw_dump = NULL;
1805 ha->fw_dump_reading = 0;
1806 ha->fw_dump_buffer = NULL;
1807}
1808
1809/*
1810 * qla2x00_allocate_sp_pool
1811 * This routine is called during initialization to allocate
1812 * memory for local srb_t.
1813 *
1814 * Input:
1815 * ha = adapter block pointer.
1816 *
1817 * Context:
1818 * Kernel context.
1819 *
1820 * Note: Sets the ref_count for non Null sp to one.
1821 */
1822static int
1823qla2x00_allocate_sp_pool(scsi_qla_host_t *ha)
1824{
1825 int rval;
1826
1827 rval = QLA_SUCCESS;
1828 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1829 mempool_free_slab, srb_cachep);
1830 if (ha->srb_mempool == NULL) {
1831 qla_printk(KERN_INFO, ha, "Unable to allocate SRB mempool.\n");
1832 rval = QLA_FUNCTION_FAILED;
1833 }
1834 return (rval);
1835}
1836
1837/*
1838 * This routine frees all adapter allocated memory.
1839 *
1840 */
1841static void
1842qla2x00_free_sp_pool( scsi_qla_host_t *ha)
1843{
1844 if (ha->srb_mempool) {
1845 mempool_destroy(ha->srb_mempool);
1846 ha->srb_mempool = NULL;
1847 }
1848}
1849
1850/**************************************************************************
1851* qla2x00_do_dpc
1852* This kernel thread is a task that is schedule by the interrupt handler
1853* to perform the background processing for interrupts.
1854*
1855* Notes:
1856* This task always run in the context of a kernel thread. It
1857* is kick-off by the driver's detect code and starts up
1858* up one per adapter. It immediately goes to sleep and waits for
1859* some fibre event. When either the interrupt handler or
1860* the timer routine detects a event it will one of the task
1861* bits then wake us up.
1862**************************************************************************/
1863static int
1864qla2x00_do_dpc(void *data)
1865{
1866 DECLARE_MUTEX_LOCKED(sem);
1867 scsi_qla_host_t *ha;
1868 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 uint8_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 uint16_t next_loopid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 ha = (scsi_qla_host_t *)data;
1873
1874 lock_kernel();
1875
1876 daemonize("%s_dpc", ha->host_str);
1877 allow_signal(SIGHUP);
1878
1879 ha->dpc_wait = &sem;
1880
1881 set_user_nice(current, -20);
1882
1883 unlock_kernel();
1884
1885 complete(&ha->dpc_inited);
1886
1887 while (1) {
1888 DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
1889
1890 if (down_interruptible(&sem))
1891 break;
1892
1893 if (ha->dpc_should_die)
1894 break;
1895
1896 DEBUG3(printk("qla2x00: DPC handler waking up\n"));
1897
1898 /* Initialization not yet finished. Don't do anything yet. */
1899 if (!ha->flags.init_done || ha->dpc_active)
1900 continue;
1901
1902 DEBUG3(printk("scsi(%ld): DPC handler\n", ha->host_no));
1903
1904 ha->dpc_active = 1;
1905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (ha->flags.mbox_busy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 ha->dpc_active = 0;
1908 continue;
1909 }
1910
1911 if (test_and_clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
1912
1913 DEBUG(printk("scsi(%ld): dpc: sched "
1914 "qla2x00_abort_isp ha = %p\n",
1915 ha->host_no, ha));
1916 if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
1917 &ha->dpc_flags))) {
1918
1919 if (qla2x00_abort_isp(ha)) {
1920 /* failed. retry later */
1921 set_bit(ISP_ABORT_NEEDED,
1922 &ha->dpc_flags);
1923 }
1924 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
1925 }
1926 DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
1927 ha->host_no));
1928 }
1929
1930 if (test_and_clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) &&
1931 (!(test_and_set_bit(RESET_ACTIVE, &ha->dpc_flags)))) {
1932
1933 DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
1934 ha->host_no));
1935
1936 qla2x00_rst_aen(ha);
1937 clear_bit(RESET_ACTIVE, &ha->dpc_flags);
1938 }
1939
1940 /* Retry each device up to login retry count */
1941 if ((test_and_clear_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
1942 !test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) &&
1943 atomic_read(&ha->loop_state) != LOOP_DOWN) {
1944
1945 DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
1946 ha->host_no));
1947
1948 next_loopid = 0;
1949 list_for_each_entry(fcport, &ha->fcports, list) {
1950 if (fcport->port_type != FCT_TARGET)
1951 continue;
1952
1953 /*
1954 * If the port is not ONLINE then try to login
1955 * to it if we haven't run out of retries.
1956 */
1957 if (atomic_read(&fcport->state) != FCS_ONLINE &&
1958 fcport->login_retry) {
1959
1960 fcport->login_retry--;
1961 if (fcport->flags & FCF_FABRIC_DEVICE) {
1962 if (fcport->flags &
1963 FCF_TAPE_PRESENT)
1964 qla2x00_fabric_logout(
1965 ha,
1966 fcport->loop_id);
1967 status = qla2x00_fabric_login(
1968 ha, fcport, &next_loopid);
1969 } else
1970 status =
1971 qla2x00_local_device_login(
1972 ha, fcport->loop_id);
1973
1974 if (status == QLA_SUCCESS) {
1975 fcport->old_loop_id = fcport->loop_id;
1976
1977 DEBUG(printk("scsi(%ld): port login OK: logged in ID 0x%x\n",
1978 ha->host_no, fcport->loop_id));
1979
1980 fcport->port_login_retry_count =
1981 ha->port_down_retry_count * PORT_RETRY_TIME;
1982 atomic_set(&fcport->state, FCS_ONLINE);
1983 atomic_set(&fcport->port_down_timer,
1984 ha->port_down_retry_count * PORT_RETRY_TIME);
1985
1986 fcport->login_retry = 0;
1987 } else if (status == 1) {
1988 set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
1989 /* retry the login again */
1990 DEBUG(printk("scsi(%ld): Retrying %d login again loop_id 0x%x\n",
1991 ha->host_no,
1992 fcport->login_retry, fcport->loop_id));
1993 } else {
1994 fcport->login_retry = 0;
1995 }
1996 }
1997 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
1998 break;
1999 }
2000 DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
2001 ha->host_no));
2002 }
2003
2004 if ((test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags)) &&
2005 atomic_read(&ha->loop_state) != LOOP_DOWN) {
2006
2007 clear_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags);
2008 DEBUG(printk("scsi(%ld): qla2x00_login_retry()\n",
2009 ha->host_no));
2010
2011 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2012
2013 DEBUG(printk("scsi(%ld): qla2x00_login_retry - end\n",
2014 ha->host_no));
2015 }
2016
2017 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
2018
2019 DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
2020 ha->host_no));
2021
2022 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
2023 &ha->dpc_flags))) {
2024
2025 qla2x00_loop_resync(ha);
2026
2027 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
2028 }
2029
2030 DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
2031 ha->host_no));
2032 }
2033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (test_and_clear_bit(FCPORT_RESCAN_NEEDED, &ha->dpc_flags)) {
2035
2036 DEBUG(printk("scsi(%ld): Rescan flagged fcports...\n",
2037 ha->host_no));
2038
2039 qla2x00_rescan_fcports(ha);
2040
2041 DEBUG(printk("scsi(%ld): Rescan flagged fcports..."
2042 "end.\n",
2043 ha->host_no));
2044 }
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 if (!ha->interrupts_on)
2047 qla2x00_enable_intrs(ha);
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 ha->dpc_active = 0;
2050 } /* End of while(1) */
2051
2052 DEBUG(printk("scsi(%ld): DPC handler exiting\n", ha->host_no));
2053
2054 /*
2055 * Make sure that nobody tries to wake us up again.
2056 */
2057 ha->dpc_wait = NULL;
2058 ha->dpc_active = 0;
2059
2060 complete_and_exit(&ha->dpc_exited, 0);
2061}
2062
2063/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064* qla2x00_rst_aen
2065* Processes asynchronous reset.
2066*
2067* Input:
2068* ha = adapter block pointer.
2069*/
2070static void
2071qla2x00_rst_aen(scsi_qla_host_t *ha)
2072{
2073 if (ha->flags.online && !ha->flags.reset_active &&
2074 !atomic_read(&ha->loop_down_timer) &&
2075 !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags))) {
2076 do {
2077 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
2078
2079 /*
2080 * Issue marker command only when we are going to start
2081 * the I/O.
2082 */
2083 ha->marker_needed = 1;
2084 } while (!atomic_read(&ha->loop_down_timer) &&
2085 (test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags)));
2086 }
2087}
2088
2089
2090/*
2091 * This routine will allocate SP from the free queue
2092 * input:
2093 * scsi_qla_host_t *
2094 * output:
2095 * srb_t * or NULL
2096 */
2097static srb_t *
2098qla2x00_get_new_sp(scsi_qla_host_t *ha)
2099{
2100 srb_t *sp;
2101
2102 sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
2103 if (sp)
2104 atomic_set(&sp->ref_count, 1);
2105 return (sp);
2106}
2107
f4f051e2005-04-17 15:02:26 -05002108static void
2109qla2x00_sp_free_dma(scsi_qla_host_t *ha, srb_t *sp)
2110{
2111 struct scsi_cmnd *cmd = sp->cmd;
2112
2113 if (sp->flags & SRB_DMA_VALID) {
2114 if (cmd->use_sg) {
2115 dma_unmap_sg(&ha->pdev->dev, cmd->request_buffer,
2116 cmd->use_sg, cmd->sc_data_direction);
2117 } else if (cmd->request_bufflen) {
2118 dma_unmap_single(&ha->pdev->dev, sp->dma_handle,
2119 cmd->request_bufflen, cmd->sc_data_direction);
2120 }
2121 sp->flags &= ~SRB_DMA_VALID;
2122 }
2123}
2124
2125void
2126qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *sp)
2127{
2128 struct scsi_cmnd *cmd = sp->cmd;
2129
2130 qla2x00_sp_free_dma(ha, sp);
2131
2132 CMD_SP(cmd) = NULL;
2133 mempool_free(sp, ha->srb_mempool);
2134
2135 cmd->scsi_done(cmd);
2136}
bdf79622005-04-17 15:06:53 -05002137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138/**************************************************************************
2139* qla2x00_timer
2140*
2141* Description:
2142* One second timer
2143*
2144* Context: Interrupt
2145***************************************************************************/
2146static void
2147qla2x00_timer(scsi_qla_host_t *ha)
2148{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 unsigned long cpu_flags = 0;
2150 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 int start_dpc = 0;
2152 int index;
2153 srb_t *sp;
f4f051e2005-04-17 15:02:26 -05002154 int t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 /*
2157 * Ports - Port down timer.
2158 *
2159 * Whenever, a port is in the LOST state we start decrementing its port
2160 * down timer every second until it reaches zero. Once it reaches zero
2161 * the port it marked DEAD.
2162 */
2163 t = 0;
2164 list_for_each_entry(fcport, &ha->fcports, list) {
2165 if (fcport->port_type != FCT_TARGET)
2166 continue;
2167
2168 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2169
2170 if (atomic_read(&fcport->port_down_timer) == 0)
2171 continue;
2172
2173 if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
2174 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
2175
2176 DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
2177 "%d remainning\n",
2178 ha->host_no,
2179 t, atomic_read(&fcport->port_down_timer)));
2180 }
2181 t++;
2182 } /* End of for fcport */
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 /* Loop down handler. */
2186 if (atomic_read(&ha->loop_down_timer) > 0 &&
2187 !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) && ha->flags.online) {
2188
2189 if (atomic_read(&ha->loop_down_timer) ==
2190 ha->loop_down_abort_time) {
2191
2192 DEBUG(printk("scsi(%ld): Loop Down - aborting the "
2193 "queues before time expire\n",
2194 ha->host_no));
2195
2196 if (!IS_QLA2100(ha) && ha->link_down_timeout)
2197 atomic_set(&ha->loop_state, LOOP_DEAD);
2198
2199 /* Schedule an ISP abort to return any tape commands. */
2200 spin_lock_irqsave(&ha->hardware_lock, cpu_flags);
2201 for (index = 1; index < MAX_OUTSTANDING_COMMANDS;
2202 index++) {
bdf79622005-04-17 15:06:53 -05002203 fc_port_t *sfcp;
2204
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 sp = ha->outstanding_cmds[index];
2206 if (!sp)
2207 continue;
bdf79622005-04-17 15:06:53 -05002208 sfcp = sp->fcport;
2209 if (!(sfcp->flags & FCF_TAPE_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 continue;
2211
2212 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2213 break;
2214 }
2215 spin_unlock_irqrestore(&ha->hardware_lock, cpu_flags);
2216
2217 set_bit(ABORT_QUEUES_NEEDED, &ha->dpc_flags);
2218 start_dpc++;
2219 }
2220
2221 /* if the loop has been down for 4 minutes, reinit adapter */
2222 if (atomic_dec_and_test(&ha->loop_down_timer) != 0) {
2223 DEBUG(printk("scsi(%ld): Loop down exceed 4 mins - "
2224 "restarting queues.\n",
2225 ha->host_no));
2226
2227 set_bit(RESTART_QUEUES_NEEDED, &ha->dpc_flags);
2228 start_dpc++;
2229
2230 if (!(ha->device_flags & DFLG_NO_CABLE)) {
2231 DEBUG(printk("scsi(%ld): Loop down - "
2232 "aborting ISP.\n",
2233 ha->host_no));
2234 qla_printk(KERN_WARNING, ha,
2235 "Loop down - aborting ISP.\n");
2236
2237 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2238 }
2239 }
2240 DEBUG3(printk("scsi(%ld): Loop Down - seconds remainning %d\n",
2241 ha->host_no,
2242 atomic_read(&ha->loop_down_timer)));
2243 }
2244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 /* Schedule the DPC routine if needed */
2246 if ((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) ||
2247 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) ||
2248 start_dpc ||
2249 test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags) ||
f4f051e2005-04-17 15:02:26 -05002250 test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 test_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
2252 ha->dpc_wait && !ha->dpc_active) {
2253
2254 up(ha->dpc_wait);
2255 }
2256
2257 qla2x00_restart_timer(ha, WATCH_INTERVAL);
2258}
2259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260/* XXX(hch): crude hack to emulate a down_timeout() */
2261int
2262qla2x00_down_timeout(struct semaphore *sema, unsigned long timeout)
2263{
2264 const unsigned int step = HZ/10;
2265
2266 do {
2267 if (!down_trylock(sema))
2268 return 0;
2269 set_current_state(TASK_INTERRUPTIBLE);
2270 if (schedule_timeout(step))
2271 break;
2272 } while ((timeout -= step) > 0);
2273
2274 return -ETIMEDOUT;
2275}
2276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277/**
2278 * qla2x00_module_init - Module initialization.
2279 **/
2280static int __init
2281qla2x00_module_init(void)
2282{
2283 /* Allocate cache for SRBs. */
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04002284 srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 SLAB_HWCACHE_ALIGN, NULL, NULL);
2286 if (srb_cachep == NULL) {
2287 printk(KERN_ERR
2288 "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
2289 return -ENOMEM;
2290 }
2291
2292 /* Derive version string. */
2293 strcpy(qla2x00_version_str, QLA2XXX_VERSION);
2294#if DEBUG_QLA2100
2295 strcat(qla2x00_version_str, "-debug");
2296#endif
Andrew Vasquez1c97a122005-04-21 16:13:36 -04002297 qla2xxx_transport_template =
2298 fc_attach_transport(&qla2xxx_transport_functions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 if (!qla2xxx_transport_template)
2300 return -ENODEV;
2301
2302 printk(KERN_INFO "QLogic Fibre Channel HBA Driver\n");
2303 return 0;
2304}
2305
2306/**
2307 * qla2x00_module_exit - Module cleanup.
2308 **/
2309static void __exit
2310qla2x00_module_exit(void)
2311{
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04002312 kmem_cache_destroy(srb_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 fc_release_transport(qla2xxx_transport_template);
2314}
2315
2316module_init(qla2x00_module_init);
2317module_exit(qla2x00_module_exit);
2318
2319MODULE_AUTHOR("QLogic Corporation");
2320MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
2321MODULE_LICENSE("GPL");
2322MODULE_VERSION(QLA2XXX_VERSION);