blob: f12a2b6fa7a06995de0a2a1dbeac71495981ed6d [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
Jeff Garzik 94d0e7b2005-05-28 07:55:48 -0400616 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 goto eh_dev_reset_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
f4f051e2005-04-17 15:02:26 -0500620 if (qla2x00_device_reset(ha, fcport) == 0)
621 ret = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623#if defined(LOGOUT_AFTER_DEVICE_RESET)
f4f051e2005-04-17 15:02:26 -0500624 if (ret == SUCCESS) {
625 if (fcport->flags & FC_FABRIC_DEVICE) {
626 qla2x00_fabric_logout(ha, fcport->loop_id);
627 qla2x00_mark_device_lost(ha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629 }
630#endif
631 } else {
632 DEBUG2(printk(KERN_INFO
633 "%s failed: loop not ready\n",__func__);)
634 }
635
f4f051e2005-04-17 15:02:26 -0500636 if (ret == FAILED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 DEBUG3(printk("%s(%ld): device reset failed\n",
638 __func__, ha->host_no));
639 qla_printk(KERN_INFO, ha, "%s: device reset failed\n",
640 __func__);
641
642 goto eh_dev_reset_done;
643 }
644
645 /*
646 * If we are coming down the EH path, wait for all commands to
647 * complete for the device.
648 */
649 if (cmd->device->host->eh_active) {
f4f051e2005-04-17 15:02:26 -0500650 if (qla2x00_eh_wait_for_pending_target_commands(ha, id))
651 ret = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
f4f051e2005-04-17 15:02:26 -0500653 if (ret == FAILED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 DEBUG3(printk("%s(%ld): failed while waiting for "
655 "commands\n", __func__, ha->host_no));
656 qla_printk(KERN_INFO, ha,
657 "%s: failed while waiting for commands\n",
658 __func__);
659
660 goto eh_dev_reset_done;
661 }
662 }
663
664 qla_printk(KERN_INFO, ha,
f4f051e2005-04-17 15:02:26 -0500665 "scsi(%ld:%d:%d): DEVICE RESET SUCCEEDED.\n", ha->host_no, id, lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667eh_dev_reset_done:
f4f051e2005-04-17 15:02:26 -0500668 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671/**************************************************************************
672* qla2x00_eh_wait_for_pending_commands
673*
674* Description:
675* Waits for all the commands to come back from the specified host.
676*
677* Input:
678* ha - pointer to scsi_qla_host structure.
679*
680* Returns:
681* 1 : SUCCESS
682* 0 : FAILED
683*
684* Note:
685**************************************************************************/
686static int
687qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *ha)
688{
689 int cnt;
690 int status;
691 srb_t *sp;
692 struct scsi_cmnd *cmd;
693
694 status = 1;
695
696 /*
697 * Waiting for all commands for the designated target in the active
698 * array
699 */
700 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
701 spin_lock(&ha->hardware_lock);
702 sp = ha->outstanding_cmds[cnt];
703 if (sp) {
704 cmd = sp->cmd;
705 spin_unlock(&ha->hardware_lock);
706 status = qla2x00_eh_wait_on_command(ha, cmd);
707 if (status == 0)
708 break;
709 }
710 else {
711 spin_unlock(&ha->hardware_lock);
712 }
713 }
714 return (status);
715}
716
717
718/**************************************************************************
719* qla2xxx_eh_bus_reset
720*
721* Description:
722* The bus reset function will reset the bus and abort any executing
723* commands.
724*
725* Input:
726* cmd = Linux SCSI command packet of the command that cause the
727* bus reset.
728*
729* Returns:
730* SUCCESS/FAILURE (defined as macro in scsi.h).
731*
732**************************************************************************/
733int
734qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
735{
f4f051e2005-04-17 15:02:26 -0500736 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
bdf79622005-04-17 15:06:53 -0500737 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 srb_t *sp;
f4f051e2005-04-17 15:02:26 -0500739 int ret;
740 unsigned int id, lun;
741 unsigned long serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
f4f051e2005-04-17 15:02:26 -0500743 ret = FAILED;
744
745 id = cmd->device->id;
746 lun = cmd->device->lun;
747 serial = cmd->serial_number;
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 sp = (srb_t *) CMD_SP(cmd);
bdf79622005-04-17 15:06:53 -0500750 if (!sp || !fcport)
f4f051e2005-04-17 15:02:26 -0500751 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 qla_printk(KERN_INFO, ha,
f4f051e2005-04-17 15:02:26 -0500754 "scsi(%ld:%d:%d): LOOP RESET ISSUED.\n", ha->host_no, id, lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
757 DEBUG2(printk("%s failed:board disabled\n",__func__));
f4f051e2005-04-17 15:02:26 -0500758 goto eh_bus_reset_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
761 if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
f4f051e2005-04-17 15:02:26 -0500762 if (qla2x00_loop_reset(ha) == QLA_SUCCESS)
763 ret = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
f4f051e2005-04-17 15:02:26 -0500765 if (ret == FAILED)
766 goto eh_bus_reset_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 /* Waiting for our command in done_queue to be returned to OS.*/
769 if (cmd->device->host->eh_active)
770 if (!qla2x00_eh_wait_for_pending_commands(ha))
f4f051e2005-04-17 15:02:26 -0500771 ret = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
f4f051e2005-04-17 15:02:26 -0500773eh_bus_reset_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
f4f051e2005-04-17 15:02:26 -0500775 (ret == FAILED) ? "failed" : "succeded");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
f4f051e2005-04-17 15:02:26 -0500777 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
780/**************************************************************************
781* qla2xxx_eh_host_reset
782*
783* Description:
784* The reset function will reset the Adapter.
785*
786* Input:
787* cmd = Linux SCSI command packet of the command that cause the
788* adapter reset.
789*
790* Returns:
791* Either SUCCESS or FAILED.
792*
793* Note:
794**************************************************************************/
795int
796qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
797{
f4f051e2005-04-17 15:02:26 -0500798 scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
bdf79622005-04-17 15:06:53 -0500799 fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
f4f051e2005-04-17 15:02:26 -0500800 srb_t *sp;
801 int ret;
802 unsigned int id, lun;
803 unsigned long serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
f4f051e2005-04-17 15:02:26 -0500805 ret = FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
f4f051e2005-04-17 15:02:26 -0500807 id = cmd->device->id;
808 lun = cmd->device->lun;
809 serial = cmd->serial_number;
810
811 sp = (srb_t *) CMD_SP(cmd);
bdf79622005-04-17 15:06:53 -0500812 if (!sp || !fcport)
f4f051e2005-04-17 15:02:26 -0500813 return ret;
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 qla_printk(KERN_INFO, ha,
f4f051e2005-04-17 15:02:26 -0500816 "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no, id, lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
f4f051e2005-04-17 15:02:26 -0500819 goto eh_host_reset_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 /*
822 * Fixme-may be dpc thread is active and processing
823 * loop_resync,so wait a while for it to
824 * be completed and then issue big hammer.Otherwise
825 * it may cause I/O failure as big hammer marks the
826 * devices as lost kicking of the port_down_timer
827 * while dpc is stuck for the mailbox to complete.
828 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 qla2x00_wait_for_loop_ready(ha);
830 set_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
831 if (qla2x00_abort_isp(ha)) {
832 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
833 /* failed. schedule dpc to try */
834 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
835
836 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
f4f051e2005-04-17 15:02:26 -0500837 goto eh_host_reset_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /* Waiting for our command in done_queue to be returned to OS.*/
f4f051e2005-04-17 15:02:26 -0500842 if (qla2x00_eh_wait_for_pending_commands(ha))
843 ret = SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
f4f051e2005-04-17 15:02:26 -0500845eh_host_reset_lock:
f4f051e2005-04-17 15:02:26 -0500846 qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
847 (ret == FAILED) ? "failed" : "succeded");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
f4f051e2005-04-17 15:02:26 -0500849 return ret;
850}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852/*
853* qla2x00_loop_reset
854* Issue loop reset.
855*
856* Input:
857* ha = adapter block pointer.
858*
859* Returns:
860* 0 = success
861*/
862static int
863qla2x00_loop_reset(scsi_qla_host_t *ha)
864{
865 int status = QLA_SUCCESS;
bdf79622005-04-17 15:06:53 -0500866 struct fc_port *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 if (ha->flags.enable_lip_reset) {
869 status = qla2x00_lip_reset(ha);
870 }
871
872 if (status == QLA_SUCCESS && ha->flags.enable_target_reset) {
bdf79622005-04-17 15:06:53 -0500873 list_for_each_entry(fcport, &ha->fcports, list) {
874 if (fcport->port_type != FCT_TARGET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 continue;
876
bdf79622005-04-17 15:06:53 -0500877 status = qla2x00_target_reset(ha, fcport);
878 if (status != QLA_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881 }
882
883 if (status == QLA_SUCCESS &&
884 ((!ha->flags.enable_target_reset &&
885 !ha->flags.enable_lip_reset) ||
886 ha->flags.enable_lip_full_login)) {
887
888 status = qla2x00_full_login_lip(ha);
889 }
890
891 /* Issue marker command only when we are going to start the I/O */
892 ha->marker_needed = 1;
893
894 if (status) {
895 /* Empty */
896 DEBUG2_3(printk("%s(%ld): **** FAILED ****\n",
897 __func__,
898 ha->host_no);)
899 } else {
900 /* Empty */
901 DEBUG3(printk("%s(%ld): exiting normally.\n",
902 __func__,
903 ha->host_no);)
904 }
905
906 return(status);
907}
908
909/*
910 * qla2x00_device_reset
911 * Issue bus device reset message to the target.
912 *
913 * Input:
914 * ha = adapter block pointer.
915 * t = SCSI ID.
916 * TARGET_QUEUE_LOCK must be released.
917 * ADAPTER_STATE_LOCK must be released.
918 *
919 * Context:
920 * Kernel context.
921 */
922static int
923qla2x00_device_reset(scsi_qla_host_t *ha, fc_port_t *reset_fcport)
924{
925 /* Abort Target command will clear Reservation */
926 return qla2x00_abort_target(reset_fcport);
927}
928
f4f051e2005-04-17 15:02:26 -0500929static int
930qla2xxx_slave_alloc(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
932 scsi_qla_host_t *ha = to_qla_host(sdev->host);
bdf79622005-04-17 15:06:53 -0500933 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
934 fc_port_t *fcport;
935 int found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
bdf79622005-04-17 15:06:53 -0500937 if (!rport)
f4f051e2005-04-17 15:02:26 -0500938 return -ENXIO;
939
bdf79622005-04-17 15:06:53 -0500940 found = 0;
941 list_for_each_entry(fcport, &ha->fcports, list) {
942 if (rport->port_name ==
943 be64_to_cpu(*(uint64_t *)fcport->port_name)) {
944 found++;
945 break;
946 }
bdf79622005-04-17 15:06:53 -0500947 }
948 if (!found)
949 return -ENXIO;
950
951 sdev->hostdata = fcport;
f4f051e2005-04-17 15:02:26 -0500952
953 return 0;
954}
955
956static int
957qla2xxx_slave_configure(struct scsi_device *sdev)
958{
8482e112005-04-17 15:04:54 -0500959 scsi_qla_host_t *ha = to_qla_host(sdev->host);
960 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
961
f4f051e2005-04-17 15:02:26 -0500962 if (sdev->tagged_supported)
963 scsi_activate_tcq(sdev, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 else
f4f051e2005-04-17 15:02:26 -0500965 scsi_deactivate_tcq(sdev, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
8482e112005-04-17 15:04:54 -0500967 rport->dev_loss_tmo = ha->port_down_retry_count + 5;
968
f4f051e2005-04-17 15:02:26 -0500969 return 0;
970}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
f4f051e2005-04-17 15:02:26 -0500972static void
973qla2xxx_slave_destroy(struct scsi_device *sdev)
974{
975 sdev->hostdata = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
978/**
979 * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
980 * @ha: HA context
981 *
982 * At exit, the @ha's flags.enable_64bit_addressing set to indicated
983 * supported addressing method.
984 */
985static void
986qla2x00_config_dma_addressing(scsi_qla_host_t *ha)
987{
988 /* Assume 32bit DMA address */
989 ha->flags.enable_64bit_addressing = 0;
990 ha->calc_request_entries = qla2x00_calc_iocbs_32;
991 ha->build_scsi_iocbs = qla2x00_build_scsi_iocbs_32;
992
993 /*
994 * Given the two variants pci_set_dma_mask(), allow the compiler to
995 * assist in setting the proper dma mask.
996 */
997 if (sizeof(dma_addr_t) > 4) {
998 if (pci_set_dma_mask(ha->pdev, DMA_64BIT_MASK) == 0) {
999 ha->flags.enable_64bit_addressing = 1;
1000 ha->calc_request_entries = qla2x00_calc_iocbs_64;
1001 ha->build_scsi_iocbs = qla2x00_build_scsi_iocbs_64;
1002
1003 if (pci_set_consistent_dma_mask(ha->pdev,
1004 DMA_64BIT_MASK)) {
1005 qla_printk(KERN_DEBUG, ha,
1006 "Failed to set 64 bit PCI consistent mask; "
1007 "using 32 bit.\n");
1008 pci_set_consistent_dma_mask(ha->pdev,
1009 DMA_32BIT_MASK);
1010 }
1011 } else {
1012 qla_printk(KERN_DEBUG, ha,
1013 "Failed to set 64 bit PCI DMA mask, falling back "
1014 "to 32 bit MASK.\n");
1015 pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
1016 }
1017 } else {
1018 pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
1019 }
1020}
1021
1022static int
1023qla2x00_iospace_config(scsi_qla_host_t *ha)
1024{
1025 unsigned long pio, pio_len, pio_flags;
1026 unsigned long mmio, mmio_len, mmio_flags;
1027
1028 /* We only need PIO for Flash operations on ISP2312 v2 chips. */
1029 pio = pci_resource_start(ha->pdev, 0);
1030 pio_len = pci_resource_len(ha->pdev, 0);
1031 pio_flags = pci_resource_flags(ha->pdev, 0);
1032 if (pio_flags & IORESOURCE_IO) {
1033 if (pio_len < MIN_IOBASE_LEN) {
1034 qla_printk(KERN_WARNING, ha,
1035 "Invalid PCI I/O region size (%s)...\n",
1036 pci_name(ha->pdev));
1037 pio = 0;
1038 }
1039 } else {
1040 qla_printk(KERN_WARNING, ha,
1041 "region #0 not a PIO resource (%s)...\n",
1042 pci_name(ha->pdev));
1043 pio = 0;
1044 }
1045
1046 /* Use MMIO operations for all accesses. */
1047 mmio = pci_resource_start(ha->pdev, 1);
1048 mmio_len = pci_resource_len(ha->pdev, 1);
1049 mmio_flags = pci_resource_flags(ha->pdev, 1);
1050
1051 if (!(mmio_flags & IORESOURCE_MEM)) {
1052 qla_printk(KERN_ERR, ha,
1053 "region #0 not an MMIO resource (%s), aborting\n",
1054 pci_name(ha->pdev));
1055 goto iospace_error_exit;
1056 }
1057 if (mmio_len < MIN_IOBASE_LEN) {
1058 qla_printk(KERN_ERR, ha,
1059 "Invalid PCI mem region size (%s), aborting\n",
1060 pci_name(ha->pdev));
1061 goto iospace_error_exit;
1062 }
1063
1064 if (pci_request_regions(ha->pdev, ha->brd_info->drv_name)) {
1065 qla_printk(KERN_WARNING, ha,
1066 "Failed to reserve PIO/MMIO regions (%s)\n",
1067 pci_name(ha->pdev));
1068
1069 goto iospace_error_exit;
1070 }
1071
1072 ha->pio_address = pio;
1073 ha->pio_length = pio_len;
1074 ha->iobase = ioremap(mmio, MIN_IOBASE_LEN);
1075 if (!ha->iobase) {
1076 qla_printk(KERN_ERR, ha,
1077 "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
1078
1079 goto iospace_error_exit;
1080 }
1081
1082 return (0);
1083
1084iospace_error_exit:
1085 return (-ENOMEM);
1086}
1087
1088/*
1089 * PCI driver interface
1090 */
1091int qla2x00_probe_one(struct pci_dev *pdev, struct qla_board_info *brd_info)
1092{
1093 int ret;
1094 device_reg_t __iomem *reg;
1095 struct Scsi_Host *host;
1096 scsi_qla_host_t *ha;
1097 unsigned long flags = 0;
1098 unsigned long wait_switch = 0;
1099 char pci_info[20];
1100 char fw_str[30];
8482e112005-04-17 15:04:54 -05001101 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 if (pci_enable_device(pdev))
1104 return -1;
1105
1106 host = scsi_host_alloc(&qla2x00_driver_template,
1107 sizeof(scsi_qla_host_t));
1108 if (host == NULL) {
1109 printk(KERN_WARNING
1110 "qla2xxx: Couldn't allocate host from scsi layer!\n");
1111 goto probe_disable_device;
1112 }
1113
1114 /* Clear our data area */
1115 ha = (scsi_qla_host_t *)host->hostdata;
1116 memset(ha, 0, sizeof(scsi_qla_host_t));
1117
1118 ha->pdev = pdev;
1119 ha->host = host;
1120 ha->host_no = host->host_no;
1121 ha->brd_info = brd_info;
1122 sprintf(ha->host_str, "%s_%ld", ha->brd_info->drv_name, ha->host_no);
1123
1124 /* Configure PCI I/O space */
1125 ret = qla2x00_iospace_config(ha);
1126 if (ret != 0) {
8482e112005-04-17 15:04:54 -05001127 goto probe_alloc_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
1129
1130 /* Sanitize the information from PCI BIOS. */
1131 host->irq = pdev->irq;
1132
1133 qla_printk(KERN_INFO, ha,
1134 "Found an %s, irq %d, iobase 0x%p\n", ha->brd_info->isp_name,
1135 host->irq, ha->iobase);
1136
1137 spin_lock_init(&ha->hardware_lock);
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 ha->prev_topology = 0;
1140 ha->ports = MAX_BUSES;
1141
1142 if (IS_QLA2100(ha)) {
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001143 host->max_id = MAX_TARGETS_2100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
1145 ha->request_q_length = REQUEST_ENTRY_CNT_2100;
1146 ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1147 ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
1148 host->sg_tablesize = 32;
1149 } else if (IS_QLA2200(ha)) {
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001150 host->max_id = MAX_TARGETS_2200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1152 ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1153 ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1154 ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
1155 } else /*if (IS_QLA2300(ha))*/ {
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001156 host->max_id = MAX_TARGETS_2200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1158 ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1159 ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
1160 ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
1161 }
1162 host->can_queue = ha->request_q_length + 128;
1163
1164 /* load the F/W, read paramaters, and init the H/W */
1165 ha->instance = num_hosts;
1166
1167 init_MUTEX(&ha->mbx_cmd_sem);
1168 init_MUTEX_LOCKED(&ha->mbx_intr_sem);
1169
1170 INIT_LIST_HEAD(&ha->list);
1171 INIT_LIST_HEAD(&ha->fcports);
1172 INIT_LIST_HEAD(&ha->rscn_fcports);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 /*
1175 * These locks are used to prevent more than one CPU
1176 * from modifying the queue at the same time. The
1177 * higher level "host_lock" will reduce most
1178 * contention for these locks.
1179 */
1180 spin_lock_init(&ha->mbx_reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 ha->dpc_pid = -1;
1183 init_completion(&ha->dpc_inited);
1184 init_completion(&ha->dpc_exited);
1185
1186 qla2x00_config_dma_addressing(ha);
1187 if (qla2x00_mem_alloc(ha)) {
1188 qla_printk(KERN_WARNING, ha,
1189 "[ERROR] Failed to allocate memory for adapter\n");
1190
8482e112005-04-17 15:04:54 -05001191 goto probe_alloc_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193
8482e112005-04-17 15:04:54 -05001194 pci_set_drvdata(pdev, ha);
1195 host->this_id = 255;
1196 host->cmd_per_lun = 3;
1197 host->unique_id = ha->instance;
1198 host->max_cmd_len = MAX_CMDSZ;
1199 host->max_channel = ha->ports - 1;
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04001200 host->max_lun = MAX_LUNS;
8482e112005-04-17 15:04:54 -05001201 host->transportt = qla2xxx_transport_template;
1202 if (scsi_add_host(host, &pdev->dev))
1203 goto probe_alloc_failed;
1204
1205 qla2x00_alloc_sysfs_attr(ha);
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (qla2x00_initialize_adapter(ha) &&
1208 !(ha->device_flags & DFLG_NO_CABLE)) {
1209
1210 qla_printk(KERN_WARNING, ha,
1211 "Failed to initialize adapter\n");
1212
1213 DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
1214 "Adapter flags %x.\n",
1215 ha->host_no, ha->device_flags));
1216
1217 goto probe_failed;
1218 }
1219
8482e112005-04-17 15:04:54 -05001220 qla2x00_init_host_attr(ha);
bdf79622005-04-17 15:06:53 -05001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 /*
1223 * Startup the kernel thread for this host adapter
1224 */
1225 ha->dpc_should_die = 0;
1226 ha->dpc_pid = kernel_thread(qla2x00_do_dpc, ha, 0);
1227 if (ha->dpc_pid < 0) {
1228 qla_printk(KERN_WARNING, ha,
1229 "Unable to start DPC thread!\n");
1230
1231 goto probe_failed;
1232 }
1233 wait_for_completion(&ha->dpc_inited);
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (IS_QLA2100(ha) || IS_QLA2200(ha))
1236 ret = request_irq(host->irq, qla2100_intr_handler,
1237 SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
1238 else
1239 ret = request_irq(host->irq, qla2300_intr_handler,
1240 SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
1241 if (ret != 0) {
1242 qla_printk(KERN_WARNING, ha,
1243 "Failed to reserve interrupt %d already in use.\n",
1244 host->irq);
1245 goto probe_failed;
1246 }
1247
1248 /* Initialized the timer */
1249 qla2x00_start_timer(ha, qla2x00_timer, WATCH_INTERVAL);
1250
1251 DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
1252 ha->host_no, ha));
1253
1254 reg = ha->iobase;
1255
1256 /* Disable ISP interrupts. */
1257 qla2x00_disable_intrs(ha);
1258
1259 /* Ensure mailbox registers are free. */
1260 spin_lock_irqsave(&ha->hardware_lock, flags);
1261 WRT_REG_WORD(&reg->semaphore, 0);
1262 WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
1263 WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
1264
1265 /* Enable proper parity */
1266 if (!IS_QLA2100(ha) && !IS_QLA2200(ha)) {
1267 if (IS_QLA2300(ha))
1268 /* SRAM parity */
1269 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x1));
1270 else
1271 /* SRAM, Instruction RAM and GP RAM parity */
1272 WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x7));
1273 }
1274 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1275
1276 /* Enable chip interrupts. */
1277 qla2x00_enable_intrs(ha);
1278
1279 /* v2.19.5b6 */
1280 /*
1281 * Wait around max loop_reset_delay secs for the devices to come
1282 * on-line. We don't want Linux scanning before we are ready.
1283 *
1284 */
1285 for (wait_switch = jiffies + (ha->loop_reset_delay * HZ);
1286 time_before(jiffies,wait_switch) &&
1287 !(ha->device_flags & (DFLG_NO_CABLE | DFLG_FABRIC_DEVICES))
1288 && (ha->device_flags & SWITCH_FOUND) ;) {
1289
1290 qla2x00_check_fabric_devices(ha);
1291
1292 msleep(10);
1293 }
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 ha->flags.init_done = 1;
1296 num_hosts++;
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 qla_printk(KERN_INFO, ha, "\n"
1299 " QLogic Fibre Channel HBA Driver: %s\n"
1300 " QLogic %s - %s\n"
1301 " %s: %s @ %s hdma%c, host#=%ld, fw=%s\n", qla2x00_version_str,
1302 ha->model_number, ha->model_desc ? ha->model_desc: "",
1303 ha->brd_info->isp_name, qla2x00_get_pci_info_str(ha, pci_info),
1304 pci_name(ha->pdev), ha->flags.enable_64bit_addressing ? '+': '-',
1305 ha->host_no, qla2x00_get_fw_version_str(ha, fw_str));
1306
8482e112005-04-17 15:04:54 -05001307 /* Go with fc_rport registration. */
1308 list_for_each_entry(fcport, &ha->fcports, list)
1309 qla2x00_reg_remote_port(ha, fcport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 return 0;
1312
1313probe_failed:
bdf79622005-04-17 15:06:53 -05001314 fc_remove_host(ha->host);
1315
8482e112005-04-17 15:04:54 -05001316 scsi_remove_host(host);
1317
1318probe_alloc_failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 qla2x00_free_device(ha);
1320
1321 scsi_host_put(host);
1322
1323probe_disable_device:
1324 pci_disable_device(pdev);
1325
1326 return -1;
1327}
1328EXPORT_SYMBOL_GPL(qla2x00_probe_one);
1329
1330void qla2x00_remove_one(struct pci_dev *pdev)
1331{
1332 scsi_qla_host_t *ha;
1333
1334 ha = pci_get_drvdata(pdev);
1335
8482e112005-04-17 15:04:54 -05001336 qla2x00_free_sysfs_attr(ha);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
bdf79622005-04-17 15:06:53 -05001338 fc_remove_host(ha->host);
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 scsi_remove_host(ha->host);
1341
1342 qla2x00_free_device(ha);
1343
1344 scsi_host_put(ha->host);
1345
1346 pci_set_drvdata(pdev, NULL);
1347}
1348EXPORT_SYMBOL_GPL(qla2x00_remove_one);
1349
1350static void
1351qla2x00_free_device(scsi_qla_host_t *ha)
1352{
1353 int ret;
1354
1355 /* Abort any outstanding IO descriptors. */
1356 if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1357 qla2x00_cancel_io_descriptors(ha);
1358
1359 /* turn-off interrupts on the card */
1360 if (ha->interrupts_on)
1361 qla2x00_disable_intrs(ha);
1362
1363 /* Disable timer */
1364 if (ha->timer_active)
1365 qla2x00_stop_timer(ha);
1366
1367 /* Kill the kernel thread for this host */
1368 if (ha->dpc_pid >= 0) {
1369 ha->dpc_should_die = 1;
1370 wmb();
1371 ret = kill_proc(ha->dpc_pid, SIGHUP, 1);
1372 if (ret) {
1373 qla_printk(KERN_ERR, ha,
1374 "Unable to signal DPC thread -- (%d)\n", ret);
1375
1376 /* TODO: SOMETHING MORE??? */
1377 } else {
1378 wait_for_completion(&ha->dpc_exited);
1379 }
1380 }
1381
1382 qla2x00_mem_free(ha);
1383
1384
1385 ha->flags.online = 0;
1386
1387 /* Detach interrupts */
1388 if (ha->pdev->irq)
1389 free_irq(ha->pdev->irq, ha);
1390
1391 /* release io space registers */
1392 if (ha->iobase)
1393 iounmap(ha->iobase);
1394 pci_release_regions(ha->pdev);
1395
1396 pci_disable_device(ha->pdev);
1397}
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 * qla2x00_mark_device_lost Updates fcport state when device goes offline.
1401 *
1402 * Input: ha = adapter block pointer. fcport = port structure pointer.
1403 *
1404 * Return: None.
1405 *
1406 * Context:
1407 */
1408void qla2x00_mark_device_lost(scsi_qla_host_t *ha, fc_port_t *fcport,
1409 int do_login)
1410{
8482e112005-04-17 15:04:54 -05001411 if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
1412 fc_remote_port_block(fcport->rport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 /*
1414 * We may need to retry the login, so don't change the state of the
1415 * port but do the retries.
1416 */
1417 if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
1418 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1419
1420 if (!do_login)
1421 return;
1422
1423 if (fcport->login_retry == 0) {
1424 fcport->login_retry = ha->login_retry_count;
1425 set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
1426
1427 DEBUG(printk("scsi(%ld): Port login retry: "
1428 "%02x%02x%02x%02x%02x%02x%02x%02x, "
1429 "id = 0x%04x retry cnt=%d\n",
1430 ha->host_no,
1431 fcport->port_name[0],
1432 fcport->port_name[1],
1433 fcport->port_name[2],
1434 fcport->port_name[3],
1435 fcport->port_name[4],
1436 fcport->port_name[5],
1437 fcport->port_name[6],
1438 fcport->port_name[7],
1439 fcport->loop_id,
1440 fcport->login_retry));
1441 }
1442}
1443
1444/*
1445 * qla2x00_mark_all_devices_lost
1446 * Updates fcport state when device goes offline.
1447 *
1448 * Input:
1449 * ha = adapter block pointer.
1450 * fcport = port structure pointer.
1451 *
1452 * Return:
1453 * None.
1454 *
1455 * Context:
1456 */
1457void
1458qla2x00_mark_all_devices_lost(scsi_qla_host_t *ha)
1459{
1460 fc_port_t *fcport;
1461
1462 list_for_each_entry(fcport, &ha->fcports, list) {
1463 if (fcport->port_type != FCT_TARGET)
1464 continue;
1465
1466 /*
1467 * No point in marking the device as lost, if the device is
1468 * already DEAD.
1469 */
1470 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
1471 continue;
8482e112005-04-17 15:04:54 -05001472 if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
1473 fc_remote_port_block(fcport->rport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1475 }
1476}
1477
1478/*
1479* qla2x00_mem_alloc
1480* Allocates adapter memory.
1481*
1482* Returns:
1483* 0 = success.
1484* 1 = failure.
1485*/
1486static uint8_t
1487qla2x00_mem_alloc(scsi_qla_host_t *ha)
1488{
1489 char name[16];
1490 uint8_t status = 1;
1491 int retry= 10;
1492
1493 do {
1494 /*
1495 * This will loop only once if everything goes well, else some
1496 * number of retries will be performed to get around a kernel
1497 * bug where available mem is not allocated until after a
1498 * little delay and a retry.
1499 */
1500 ha->request_ring = dma_alloc_coherent(&ha->pdev->dev,
1501 (ha->request_q_length + 1) * sizeof(request_t),
1502 &ha->request_dma, GFP_KERNEL);
1503 if (ha->request_ring == NULL) {
1504 qla_printk(KERN_WARNING, ha,
1505 "Memory Allocation failed - request_ring\n");
1506
1507 qla2x00_mem_free(ha);
1508 msleep(100);
1509
1510 continue;
1511 }
1512
1513 ha->response_ring = dma_alloc_coherent(&ha->pdev->dev,
1514 (ha->response_q_length + 1) * sizeof(response_t),
1515 &ha->response_dma, GFP_KERNEL);
1516 if (ha->response_ring == NULL) {
1517 qla_printk(KERN_WARNING, ha,
1518 "Memory Allocation failed - response_ring\n");
1519
1520 qla2x00_mem_free(ha);
1521 msleep(100);
1522
1523 continue;
1524 }
1525
1526 ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
1527 &ha->gid_list_dma, GFP_KERNEL);
1528 if (ha->gid_list == NULL) {
1529 qla_printk(KERN_WARNING, ha,
1530 "Memory Allocation failed - gid_list\n");
1531
1532 qla2x00_mem_free(ha);
1533 msleep(100);
1534
1535 continue;
1536 }
1537
1538 ha->rlc_rsp = dma_alloc_coherent(&ha->pdev->dev,
1539 sizeof(rpt_lun_cmd_rsp_t), &ha->rlc_rsp_dma, GFP_KERNEL);
1540 if (ha->rlc_rsp == NULL) {
1541 qla_printk(KERN_WARNING, ha,
1542 "Memory Allocation failed - rlc");
1543
1544 qla2x00_mem_free(ha);
1545 msleep(100);
1546
1547 continue;
1548 }
1549
1550 snprintf(name, sizeof(name), "qla2xxx_%ld", ha->host_no);
1551 ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
1552 DMA_POOL_SIZE, 8, 0);
1553 if (ha->s_dma_pool == NULL) {
1554 qla_printk(KERN_WARNING, ha,
1555 "Memory Allocation failed - s_dma_pool\n");
1556
1557 qla2x00_mem_free(ha);
1558 msleep(100);
1559
1560 continue;
1561 }
1562
1563 /* get consistent memory allocated for init control block */
1564 ha->init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1565 &ha->init_cb_dma);
1566 if (ha->init_cb == NULL) {
1567 qla_printk(KERN_WARNING, ha,
1568 "Memory Allocation failed - init_cb\n");
1569
1570 qla2x00_mem_free(ha);
1571 msleep(100);
1572
1573 continue;
1574 }
1575 memset(ha->init_cb, 0, sizeof(init_cb_t));
1576
1577 /* Get consistent memory allocated for Get Port Database cmd */
1578 ha->iodesc_pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1579 &ha->iodesc_pd_dma);
1580 if (ha->iodesc_pd == NULL) {
1581 /* error */
1582 qla_printk(KERN_WARNING, ha,
1583 "Memory Allocation failed - iodesc_pd\n");
1584
1585 qla2x00_mem_free(ha);
1586 msleep(100);
1587
1588 continue;
1589 }
1590 memset(ha->iodesc_pd, 0, PORT_DATABASE_SIZE);
1591
1592 /* Allocate ioctl related memory. */
1593 if (qla2x00_alloc_ioctl_mem(ha)) {
1594 qla_printk(KERN_WARNING, ha,
1595 "Memory Allocation failed - ioctl_mem\n");
1596
1597 qla2x00_mem_free(ha);
1598 msleep(100);
1599
1600 continue;
1601 }
1602
1603 if (qla2x00_allocate_sp_pool(ha)) {
1604 qla_printk(KERN_WARNING, ha,
1605 "Memory Allocation failed - "
1606 "qla2x00_allocate_sp_pool()\n");
1607
1608 qla2x00_mem_free(ha);
1609 msleep(100);
1610
1611 continue;
1612 }
1613
1614 /* Allocate memory for SNS commands */
1615 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1616 /* Get consistent memory allocated for SNS commands */
1617 ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
1618 sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma,
1619 GFP_KERNEL);
1620 if (ha->sns_cmd == NULL) {
1621 /* error */
1622 qla_printk(KERN_WARNING, ha,
1623 "Memory Allocation failed - sns_cmd\n");
1624
1625 qla2x00_mem_free(ha);
1626 msleep(100);
1627
1628 continue;
1629 }
1630 memset(ha->sns_cmd, 0, sizeof(struct sns_cmd_pkt));
1631 } else {
1632 /* Get consistent memory allocated for MS IOCB */
1633 ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1634 &ha->ms_iocb_dma);
1635 if (ha->ms_iocb == NULL) {
1636 /* error */
1637 qla_printk(KERN_WARNING, ha,
1638 "Memory Allocation failed - ms_iocb\n");
1639
1640 qla2x00_mem_free(ha);
1641 msleep(100);
1642
1643 continue;
1644 }
1645 memset(ha->ms_iocb, 0, sizeof(ms_iocb_entry_t));
1646
1647 /*
1648 * Get consistent memory allocated for CT SNS
1649 * commands
1650 */
1651 ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
1652 sizeof(struct ct_sns_pkt), &ha->ct_sns_dma,
1653 GFP_KERNEL);
1654 if (ha->ct_sns == NULL) {
1655 /* error */
1656 qla_printk(KERN_WARNING, ha,
1657 "Memory Allocation failed - ct_sns\n");
1658
1659 qla2x00_mem_free(ha);
1660 msleep(100);
1661
1662 continue;
1663 }
1664 memset(ha->ct_sns, 0, sizeof(struct ct_sns_pkt));
1665 }
1666
1667 /* Done all allocations without any error. */
1668 status = 0;
1669
1670 } while (retry-- && status != 0);
1671
1672 if (status) {
1673 printk(KERN_WARNING
1674 "%s(): **** FAILED ****\n", __func__);
1675 }
1676
1677 return(status);
1678}
1679
1680/*
1681* qla2x00_mem_free
1682* Frees all adapter allocated memory.
1683*
1684* Input:
1685* ha = adapter block pointer.
1686*/
1687static void
1688qla2x00_mem_free(scsi_qla_host_t *ha)
1689{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 struct list_head *fcpl, *fcptemp;
1691 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 unsigned long wtime;/* max wait time if mbx cmd is busy. */
1693
1694 if (ha == NULL) {
1695 /* error */
1696 DEBUG2(printk("%s(): ERROR invalid ha pointer.\n", __func__));
1697 return;
1698 }
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 /* Make sure all other threads are stopped. */
1701 wtime = 60 * HZ;
1702 while (ha->dpc_wait && wtime) {
1703 set_current_state(TASK_INTERRUPTIBLE);
1704 wtime = schedule_timeout(wtime);
1705 }
1706
1707 /* free ioctl memory */
1708 qla2x00_free_ioctl_mem(ha);
1709
1710 /* free sp pool */
1711 qla2x00_free_sp_pool(ha);
1712
1713 if (ha->sns_cmd)
1714 dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
1715 ha->sns_cmd, ha->sns_cmd_dma);
1716
1717 if (ha->ct_sns)
1718 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
1719 ha->ct_sns, ha->ct_sns_dma);
1720
1721 if (ha->ms_iocb)
1722 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
1723
1724 if (ha->iodesc_pd)
1725 dma_pool_free(ha->s_dma_pool, ha->iodesc_pd, ha->iodesc_pd_dma);
1726
1727 if (ha->init_cb)
1728 dma_pool_free(ha->s_dma_pool, ha->init_cb, ha->init_cb_dma);
1729
1730 if (ha->s_dma_pool)
1731 dma_pool_destroy(ha->s_dma_pool);
1732
1733 if (ha->rlc_rsp)
1734 dma_free_coherent(&ha->pdev->dev,
1735 sizeof(rpt_lun_cmd_rsp_t), ha->rlc_rsp,
1736 ha->rlc_rsp_dma);
1737
1738 if (ha->gid_list)
1739 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
1740 ha->gid_list_dma);
1741
1742 if (ha->response_ring)
1743 dma_free_coherent(&ha->pdev->dev,
1744 (ha->response_q_length + 1) * sizeof(response_t),
1745 ha->response_ring, ha->response_dma);
1746
1747 if (ha->request_ring)
1748 dma_free_coherent(&ha->pdev->dev,
1749 (ha->request_q_length + 1) * sizeof(request_t),
1750 ha->request_ring, ha->request_dma);
1751
1752 ha->sns_cmd = NULL;
1753 ha->sns_cmd_dma = 0;
1754 ha->ct_sns = NULL;
1755 ha->ct_sns_dma = 0;
1756 ha->ms_iocb = NULL;
1757 ha->ms_iocb_dma = 0;
1758 ha->iodesc_pd = NULL;
1759 ha->iodesc_pd_dma = 0;
1760 ha->init_cb = NULL;
1761 ha->init_cb_dma = 0;
1762
1763 ha->s_dma_pool = NULL;
1764
1765 ha->rlc_rsp = NULL;
1766 ha->rlc_rsp_dma = 0;
1767 ha->gid_list = NULL;
1768 ha->gid_list_dma = 0;
1769
1770 ha->response_ring = NULL;
1771 ha->response_dma = 0;
1772 ha->request_ring = NULL;
1773 ha->request_dma = 0;
1774
1775 list_for_each_safe(fcpl, fcptemp, &ha->fcports) {
1776 fcport = list_entry(fcpl, fc_port_t, list);
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 /* fc ports */
1779 list_del_init(&fcport->list);
1780 kfree(fcport);
1781 }
1782 INIT_LIST_HEAD(&ha->fcports);
1783
1784 if (ha->fw_dump)
1785 free_pages((unsigned long)ha->fw_dump, ha->fw_dump_order);
1786
1787 if (ha->fw_dump_buffer)
1788 vfree(ha->fw_dump_buffer);
1789
1790 ha->fw_dump = NULL;
1791 ha->fw_dump_reading = 0;
1792 ha->fw_dump_buffer = NULL;
1793}
1794
1795/*
1796 * qla2x00_allocate_sp_pool
1797 * This routine is called during initialization to allocate
1798 * memory for local srb_t.
1799 *
1800 * Input:
1801 * ha = adapter block pointer.
1802 *
1803 * Context:
1804 * Kernel context.
1805 *
1806 * Note: Sets the ref_count for non Null sp to one.
1807 */
1808static int
1809qla2x00_allocate_sp_pool(scsi_qla_host_t *ha)
1810{
1811 int rval;
1812
1813 rval = QLA_SUCCESS;
1814 ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1815 mempool_free_slab, srb_cachep);
1816 if (ha->srb_mempool == NULL) {
1817 qla_printk(KERN_INFO, ha, "Unable to allocate SRB mempool.\n");
1818 rval = QLA_FUNCTION_FAILED;
1819 }
1820 return (rval);
1821}
1822
1823/*
1824 * This routine frees all adapter allocated memory.
1825 *
1826 */
1827static void
1828qla2x00_free_sp_pool( scsi_qla_host_t *ha)
1829{
1830 if (ha->srb_mempool) {
1831 mempool_destroy(ha->srb_mempool);
1832 ha->srb_mempool = NULL;
1833 }
1834}
1835
1836/**************************************************************************
1837* qla2x00_do_dpc
1838* This kernel thread is a task that is schedule by the interrupt handler
1839* to perform the background processing for interrupts.
1840*
1841* Notes:
1842* This task always run in the context of a kernel thread. It
1843* is kick-off by the driver's detect code and starts up
1844* up one per adapter. It immediately goes to sleep and waits for
1845* some fibre event. When either the interrupt handler or
1846* the timer routine detects a event it will one of the task
1847* bits then wake us up.
1848**************************************************************************/
1849static int
1850qla2x00_do_dpc(void *data)
1851{
1852 DECLARE_MUTEX_LOCKED(sem);
1853 scsi_qla_host_t *ha;
1854 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 uint8_t status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 uint16_t next_loopid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 ha = (scsi_qla_host_t *)data;
1859
1860 lock_kernel();
1861
1862 daemonize("%s_dpc", ha->host_str);
1863 allow_signal(SIGHUP);
1864
1865 ha->dpc_wait = &sem;
1866
1867 set_user_nice(current, -20);
1868
1869 unlock_kernel();
1870
1871 complete(&ha->dpc_inited);
1872
1873 while (1) {
1874 DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
1875
1876 if (down_interruptible(&sem))
1877 break;
1878
1879 if (ha->dpc_should_die)
1880 break;
1881
1882 DEBUG3(printk("qla2x00: DPC handler waking up\n"));
1883
1884 /* Initialization not yet finished. Don't do anything yet. */
1885 if (!ha->flags.init_done || ha->dpc_active)
1886 continue;
1887
1888 DEBUG3(printk("scsi(%ld): DPC handler\n", ha->host_no));
1889
1890 ha->dpc_active = 1;
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (ha->flags.mbox_busy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 ha->dpc_active = 0;
1894 continue;
1895 }
1896
1897 if (test_and_clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
1898
1899 DEBUG(printk("scsi(%ld): dpc: sched "
1900 "qla2x00_abort_isp ha = %p\n",
1901 ha->host_no, ha));
1902 if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
1903 &ha->dpc_flags))) {
1904
1905 if (qla2x00_abort_isp(ha)) {
1906 /* failed. retry later */
1907 set_bit(ISP_ABORT_NEEDED,
1908 &ha->dpc_flags);
1909 }
1910 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
1911 }
1912 DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
1913 ha->host_no));
1914 }
1915
1916 if (test_and_clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) &&
1917 (!(test_and_set_bit(RESET_ACTIVE, &ha->dpc_flags)))) {
1918
1919 DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
1920 ha->host_no));
1921
1922 qla2x00_rst_aen(ha);
1923 clear_bit(RESET_ACTIVE, &ha->dpc_flags);
1924 }
1925
1926 /* Retry each device up to login retry count */
1927 if ((test_and_clear_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
1928 !test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) &&
1929 atomic_read(&ha->loop_state) != LOOP_DOWN) {
1930
1931 DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
1932 ha->host_no));
1933
1934 next_loopid = 0;
1935 list_for_each_entry(fcport, &ha->fcports, list) {
1936 if (fcport->port_type != FCT_TARGET)
1937 continue;
1938
1939 /*
1940 * If the port is not ONLINE then try to login
1941 * to it if we haven't run out of retries.
1942 */
1943 if (atomic_read(&fcport->state) != FCS_ONLINE &&
1944 fcport->login_retry) {
1945
1946 fcport->login_retry--;
1947 if (fcport->flags & FCF_FABRIC_DEVICE) {
1948 if (fcport->flags &
1949 FCF_TAPE_PRESENT)
1950 qla2x00_fabric_logout(
1951 ha,
1952 fcport->loop_id);
1953 status = qla2x00_fabric_login(
1954 ha, fcport, &next_loopid);
1955 } else
1956 status =
1957 qla2x00_local_device_login(
1958 ha, fcport->loop_id);
1959
1960 if (status == QLA_SUCCESS) {
1961 fcport->old_loop_id = fcport->loop_id;
1962
1963 DEBUG(printk("scsi(%ld): port login OK: logged in ID 0x%x\n",
1964 ha->host_no, fcport->loop_id));
1965
1966 fcport->port_login_retry_count =
1967 ha->port_down_retry_count * PORT_RETRY_TIME;
1968 atomic_set(&fcport->state, FCS_ONLINE);
1969 atomic_set(&fcport->port_down_timer,
1970 ha->port_down_retry_count * PORT_RETRY_TIME);
1971
1972 fcport->login_retry = 0;
1973 } else if (status == 1) {
1974 set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
1975 /* retry the login again */
1976 DEBUG(printk("scsi(%ld): Retrying %d login again loop_id 0x%x\n",
1977 ha->host_no,
1978 fcport->login_retry, fcport->loop_id));
1979 } else {
1980 fcport->login_retry = 0;
1981 }
1982 }
1983 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
1984 break;
1985 }
1986 DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
1987 ha->host_no));
1988 }
1989
1990 if ((test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags)) &&
1991 atomic_read(&ha->loop_state) != LOOP_DOWN) {
1992
1993 clear_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags);
1994 DEBUG(printk("scsi(%ld): qla2x00_login_retry()\n",
1995 ha->host_no));
1996
1997 set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
1998
1999 DEBUG(printk("scsi(%ld): qla2x00_login_retry - end\n",
2000 ha->host_no));
2001 }
2002
2003 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
2004
2005 DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
2006 ha->host_no));
2007
2008 if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
2009 &ha->dpc_flags))) {
2010
2011 qla2x00_loop_resync(ha);
2012
2013 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
2014 }
2015
2016 DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
2017 ha->host_no));
2018 }
2019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (test_and_clear_bit(FCPORT_RESCAN_NEEDED, &ha->dpc_flags)) {
2021
2022 DEBUG(printk("scsi(%ld): Rescan flagged fcports...\n",
2023 ha->host_no));
2024
2025 qla2x00_rescan_fcports(ha);
2026
2027 DEBUG(printk("scsi(%ld): Rescan flagged fcports..."
2028 "end.\n",
2029 ha->host_no));
2030 }
2031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (!ha->interrupts_on)
2033 qla2x00_enable_intrs(ha);
2034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 ha->dpc_active = 0;
2036 } /* End of while(1) */
2037
2038 DEBUG(printk("scsi(%ld): DPC handler exiting\n", ha->host_no));
2039
2040 /*
2041 * Make sure that nobody tries to wake us up again.
2042 */
2043 ha->dpc_wait = NULL;
2044 ha->dpc_active = 0;
2045
2046 complete_and_exit(&ha->dpc_exited, 0);
2047}
2048
2049/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050* qla2x00_rst_aen
2051* Processes asynchronous reset.
2052*
2053* Input:
2054* ha = adapter block pointer.
2055*/
2056static void
2057qla2x00_rst_aen(scsi_qla_host_t *ha)
2058{
2059 if (ha->flags.online && !ha->flags.reset_active &&
2060 !atomic_read(&ha->loop_down_timer) &&
2061 !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags))) {
2062 do {
2063 clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
2064
2065 /*
2066 * Issue marker command only when we are going to start
2067 * the I/O.
2068 */
2069 ha->marker_needed = 1;
2070 } while (!atomic_read(&ha->loop_down_timer) &&
2071 (test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags)));
2072 }
2073}
2074
2075
2076/*
2077 * This routine will allocate SP from the free queue
2078 * input:
2079 * scsi_qla_host_t *
2080 * output:
2081 * srb_t * or NULL
2082 */
2083static srb_t *
2084qla2x00_get_new_sp(scsi_qla_host_t *ha)
2085{
2086 srb_t *sp;
2087
2088 sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
2089 if (sp)
2090 atomic_set(&sp->ref_count, 1);
2091 return (sp);
2092}
2093
f4f051e2005-04-17 15:02:26 -05002094static void
2095qla2x00_sp_free_dma(scsi_qla_host_t *ha, srb_t *sp)
2096{
2097 struct scsi_cmnd *cmd = sp->cmd;
2098
2099 if (sp->flags & SRB_DMA_VALID) {
2100 if (cmd->use_sg) {
2101 dma_unmap_sg(&ha->pdev->dev, cmd->request_buffer,
2102 cmd->use_sg, cmd->sc_data_direction);
2103 } else if (cmd->request_bufflen) {
2104 dma_unmap_single(&ha->pdev->dev, sp->dma_handle,
2105 cmd->request_bufflen, cmd->sc_data_direction);
2106 }
2107 sp->flags &= ~SRB_DMA_VALID;
2108 }
2109}
2110
2111void
2112qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *sp)
2113{
2114 struct scsi_cmnd *cmd = sp->cmd;
2115
2116 qla2x00_sp_free_dma(ha, sp);
2117
2118 CMD_SP(cmd) = NULL;
2119 mempool_free(sp, ha->srb_mempool);
2120
2121 cmd->scsi_done(cmd);
2122}
bdf79622005-04-17 15:06:53 -05002123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124/**************************************************************************
2125* qla2x00_timer
2126*
2127* Description:
2128* One second timer
2129*
2130* Context: Interrupt
2131***************************************************************************/
2132static void
2133qla2x00_timer(scsi_qla_host_t *ha)
2134{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 unsigned long cpu_flags = 0;
2136 fc_port_t *fcport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 int start_dpc = 0;
2138 int index;
2139 srb_t *sp;
f4f051e2005-04-17 15:02:26 -05002140 int t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 /*
2143 * Ports - Port down timer.
2144 *
2145 * Whenever, a port is in the LOST state we start decrementing its port
2146 * down timer every second until it reaches zero. Once it reaches zero
2147 * the port it marked DEAD.
2148 */
2149 t = 0;
2150 list_for_each_entry(fcport, &ha->fcports, list) {
2151 if (fcport->port_type != FCT_TARGET)
2152 continue;
2153
2154 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2155
2156 if (atomic_read(&fcport->port_down_timer) == 0)
2157 continue;
2158
2159 if (atomic_dec_and_test(&fcport->port_down_timer) != 0)
2160 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
2161
2162 DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
2163 "%d remainning\n",
2164 ha->host_no,
2165 t, atomic_read(&fcport->port_down_timer)));
2166 }
2167 t++;
2168 } /* End of for fcport */
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 /* Loop down handler. */
2172 if (atomic_read(&ha->loop_down_timer) > 0 &&
2173 !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) && ha->flags.online) {
2174
2175 if (atomic_read(&ha->loop_down_timer) ==
2176 ha->loop_down_abort_time) {
2177
2178 DEBUG(printk("scsi(%ld): Loop Down - aborting the "
2179 "queues before time expire\n",
2180 ha->host_no));
2181
2182 if (!IS_QLA2100(ha) && ha->link_down_timeout)
2183 atomic_set(&ha->loop_state, LOOP_DEAD);
2184
2185 /* Schedule an ISP abort to return any tape commands. */
2186 spin_lock_irqsave(&ha->hardware_lock, cpu_flags);
2187 for (index = 1; index < MAX_OUTSTANDING_COMMANDS;
2188 index++) {
bdf79622005-04-17 15:06:53 -05002189 fc_port_t *sfcp;
2190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 sp = ha->outstanding_cmds[index];
2192 if (!sp)
2193 continue;
bdf79622005-04-17 15:06:53 -05002194 sfcp = sp->fcport;
2195 if (!(sfcp->flags & FCF_TAPE_PRESENT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 continue;
2197
2198 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2199 break;
2200 }
2201 spin_unlock_irqrestore(&ha->hardware_lock, cpu_flags);
2202
2203 set_bit(ABORT_QUEUES_NEEDED, &ha->dpc_flags);
2204 start_dpc++;
2205 }
2206
2207 /* if the loop has been down for 4 minutes, reinit adapter */
2208 if (atomic_dec_and_test(&ha->loop_down_timer) != 0) {
2209 DEBUG(printk("scsi(%ld): Loop down exceed 4 mins - "
2210 "restarting queues.\n",
2211 ha->host_no));
2212
2213 set_bit(RESTART_QUEUES_NEEDED, &ha->dpc_flags);
2214 start_dpc++;
2215
2216 if (!(ha->device_flags & DFLG_NO_CABLE)) {
2217 DEBUG(printk("scsi(%ld): Loop down - "
2218 "aborting ISP.\n",
2219 ha->host_no));
2220 qla_printk(KERN_WARNING, ha,
2221 "Loop down - aborting ISP.\n");
2222
2223 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2224 }
2225 }
2226 DEBUG3(printk("scsi(%ld): Loop Down - seconds remainning %d\n",
2227 ha->host_no,
2228 atomic_read(&ha->loop_down_timer)));
2229 }
2230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 /* Schedule the DPC routine if needed */
2232 if ((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) ||
2233 test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) ||
2234 start_dpc ||
2235 test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags) ||
f4f051e2005-04-17 15:02:26 -05002236 test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 test_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
2238 ha->dpc_wait && !ha->dpc_active) {
2239
2240 up(ha->dpc_wait);
2241 }
2242
2243 qla2x00_restart_timer(ha, WATCH_INTERVAL);
2244}
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246/* XXX(hch): crude hack to emulate a down_timeout() */
2247int
2248qla2x00_down_timeout(struct semaphore *sema, unsigned long timeout)
2249{
2250 const unsigned int step = HZ/10;
2251
2252 do {
2253 if (!down_trylock(sema))
2254 return 0;
2255 set_current_state(TASK_INTERRUPTIBLE);
2256 if (schedule_timeout(step))
2257 break;
2258 } while ((timeout -= step) > 0);
2259
2260 return -ETIMEDOUT;
2261}
2262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263/**
2264 * qla2x00_module_init - Module initialization.
2265 **/
2266static int __init
2267qla2x00_module_init(void)
2268{
2269 /* Allocate cache for SRBs. */
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04002270 srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 SLAB_HWCACHE_ALIGN, NULL, NULL);
2272 if (srb_cachep == NULL) {
2273 printk(KERN_ERR
2274 "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
2275 return -ENOMEM;
2276 }
2277
2278 /* Derive version string. */
2279 strcpy(qla2x00_version_str, QLA2XXX_VERSION);
2280#if DEBUG_QLA2100
2281 strcat(qla2x00_version_str, "-debug");
2282#endif
Andrew Vasquez1c97a122005-04-21 16:13:36 -04002283 qla2xxx_transport_template =
2284 fc_attach_transport(&qla2xxx_transport_functions);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 if (!qla2xxx_transport_template)
2286 return -ENODEV;
2287
2288 printk(KERN_INFO "QLogic Fibre Channel HBA Driver\n");
2289 return 0;
2290}
2291
2292/**
2293 * qla2x00_module_exit - Module cleanup.
2294 **/
2295static void __exit
2296qla2x00_module_exit(void)
2297{
Andrew Vasquez 354d6b22005-04-23 02:47:27 -04002298 kmem_cache_destroy(srb_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 fc_release_transport(qla2xxx_transport_template);
2300}
2301
2302module_init(qla2x00_module_init);
2303module_exit(qla2x00_module_exit);
2304
2305MODULE_AUTHOR("QLogic Corporation");
2306MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
2307MODULE_LICENSE("GPL");
2308MODULE_VERSION(QLA2XXX_VERSION);