vserver 1.9.3
[linux-2.6.git] / drivers / scsi / ipr.c
1 /*
2  * ipr.c -- driver for IBM Power Linux RAID adapters
3  *
4  * Written By: Brian King <brking@us.ibm.com>, IBM Corporation
5  *
6  * Copyright (C) 2003, 2004 IBM Corporation
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 /*
25  * Notes:
26  *
27  * This driver is used to control the following SCSI adapters:
28  *
29  * IBM iSeries: 5702, 5703, 2780, 5709, 570A, 570B
30  *
31  * IBM pSeries: PCI-X Dual Channel Ultra 320 SCSI RAID Adapter
32  *              PCI-X Dual Channel Ultra 320 SCSI Adapter
33  *              PCI-X Dual Channel Ultra 320 SCSI RAID Enablement Card
34  *              Embedded SCSI adapter on p615 and p655 systems
35  *
36  * Supported Hardware Features:
37  *      - Ultra 320 SCSI controller
38  *      - PCI-X host interface
39  *      - Embedded PowerPC RISC Processor and Hardware XOR DMA Engine
40  *      - Non-Volatile Write Cache
41  *      - Supports attachment of non-RAID disks, tape, and optical devices
42  *      - RAID Levels 0, 5, 10
43  *      - Hot spare
44  *      - Background Parity Checking
45  *      - Background Data Scrubbing
46  *      - Ability to increase the capacity of an existing RAID 5 disk array
47  *              by adding disks
48  *
49  * Driver Features:
50  *      - Tagged command queuing
51  *      - Adapter microcode download
52  *      - PCI hot plug
53  *      - SCSI device hot plug
54  *
55  */
56
57 #include <linux/config.h>
58 #include <linux/fs.h>
59 #include <linux/init.h>
60 #include <linux/types.h>
61 #include <linux/errno.h>
62 #include <linux/kernel.h>
63 #include <linux/ioport.h>
64 #include <linux/delay.h>
65 #include <linux/pci.h>
66 #include <linux/wait.h>
67 #include <linux/spinlock.h>
68 #include <linux/sched.h>
69 #include <linux/interrupt.h>
70 #include <linux/blkdev.h>
71 #include <linux/firmware.h>
72 #include <linux/module.h>
73 #include <linux/moduleparam.h>
74 #include <asm/io.h>
75 #include <asm/irq.h>
76 #include <asm/processor.h>
77 #include <scsi/scsi.h>
78 #include <scsi/scsi_host.h>
79 #include <scsi/scsi_tcq.h>
80 #include <scsi/scsi_eh.h>
81 #include <scsi/scsi_cmnd.h>
82 #include <scsi/scsi_request.h>
83 #include "ipr.h"
84
85 /*
86  *   Global Data
87  */
88 static struct list_head ipr_ioa_head = LIST_HEAD_INIT(ipr_ioa_head);
89 static unsigned int ipr_log_level = IPR_DEFAULT_LOG_LEVEL;
90 static unsigned int ipr_max_speed = 1;
91 static int ipr_testmode = 0;
92 static spinlock_t ipr_driver_lock = SPIN_LOCK_UNLOCKED;
93
94 /* This table describes the differences between DMA controller chips */
95 static const struct ipr_chip_cfg_t ipr_chip_cfg[] = {
96         { /* Gemstone and Citrine */
97                 .mailbox = 0x0042C,
98                 .cache_line_size = 0x20,
99                 {
100                         .set_interrupt_mask_reg = 0x0022C,
101                         .clr_interrupt_mask_reg = 0x00230,
102                         .sense_interrupt_mask_reg = 0x0022C,
103                         .clr_interrupt_reg = 0x00228,
104                         .sense_interrupt_reg = 0x00224,
105                         .ioarrin_reg = 0x00404,
106                         .sense_uproc_interrupt_reg = 0x00214,
107                         .set_uproc_interrupt_reg = 0x00214,
108                         .clr_uproc_interrupt_reg = 0x00218
109                 }
110         },
111         { /* Snipe */
112                 .mailbox = 0x0052C,
113                 .cache_line_size = 0x20,
114                 {
115                         .set_interrupt_mask_reg = 0x00288,
116                         .clr_interrupt_mask_reg = 0x0028C,
117                         .sense_interrupt_mask_reg = 0x00288,
118                         .clr_interrupt_reg = 0x00284,
119                         .sense_interrupt_reg = 0x00280,
120                         .ioarrin_reg = 0x00504,
121                         .sense_uproc_interrupt_reg = 0x00290,
122                         .set_uproc_interrupt_reg = 0x00290,
123                         .clr_uproc_interrupt_reg = 0x00294
124                 }
125         },
126 };
127
128 static int ipr_max_bus_speeds [] = {
129         IPR_80MBs_SCSI_RATE, IPR_U160_SCSI_RATE, IPR_U320_SCSI_RATE
130 };
131
132 MODULE_AUTHOR("Brian King <brking@us.ibm.com>");
133 MODULE_DESCRIPTION("IBM Power RAID SCSI Adapter Driver");
134 module_param_named(max_speed, ipr_max_speed, uint, 0);
135 MODULE_PARM_DESC(max_speed, "Maximum bus speed (0-2). Default: 1=U160. Speeds: 0=80 MB/s, 1=U160, 2=U320");
136 module_param_named(log_level, ipr_log_level, uint, 0);
137 MODULE_PARM_DESC(log_level, "Set to 0 - 4 for increasing verbosity of device driver");
138 module_param_named(testmode, ipr_testmode, int, 0);
139 MODULE_PARM_DESC(testmode, "DANGEROUS!!! Allows unsupported configurations");
140 MODULE_LICENSE("GPL");
141 MODULE_VERSION(IPR_DRIVER_VERSION);
142
143 static const char *ipr_gpdd_dev_end_states[] = {
144         "Command complete",
145         "Terminated by host",
146         "Terminated by device reset",
147         "Terminated by bus reset",
148         "Unknown",
149         "Command not started"
150 };
151
152 static const char *ipr_gpdd_dev_bus_phases[] = {
153         "Bus free",
154         "Arbitration",
155         "Selection",
156         "Message out",
157         "Command",
158         "Message in",
159         "Data out",
160         "Data in",
161         "Status",
162         "Reselection",
163         "Unknown"
164 };
165
166 /*  A constant array of IOASCs/URCs/Error Messages */
167 static const
168 struct ipr_error_table_t ipr_error_table[] = {
169         {0x00000000, 1, 1,
170         "8155: An unknown error was received"},
171         {0x00330000, 0, 0,
172         "Soft underlength error"},
173         {0x005A0000, 0, 0,
174         "Command to be cancelled not found"},
175         {0x00808000, 0, 0,
176         "Qualified success"},
177         {0x01080000, 1, 1,
178         "FFFE: Soft device bus error recovered by the IOA"},
179         {0x01170600, 0, 1,
180         "FFF9: Device sector reassign successful"},
181         {0x01170900, 0, 1,
182         "FFF7: Media error recovered by device rewrite procedures"},
183         {0x01180200, 0, 1,
184         "7001: IOA sector reassignment successful"},
185         {0x01180500, 0, 1,
186         "FFF9: Soft media error. Sector reassignment recommended"},
187         {0x01180600, 0, 1,
188         "FFF7: Media error recovered by IOA rewrite procedures"},
189         {0x01418000, 0, 1,
190         "FF3D: Soft PCI bus error recovered by the IOA"},
191         {0x01440000, 1, 1,
192         "FFF6: Device hardware error recovered by the IOA"},
193         {0x01448100, 0, 1,
194         "FFF6: Device hardware error recovered by the device"},
195         {0x01448200, 1, 1,
196         "FF3D: Soft IOA error recovered by the IOA"},
197         {0x01448300, 0, 1,
198         "FFFA: Undefined device response recovered by the IOA"},
199         {0x014A0000, 1, 1,
200         "FFF6: Device bus error, message or command phase"},
201         {0x015D0000, 0, 1,
202         "FFF6: Failure prediction threshold exceeded"},
203         {0x015D9200, 0, 1,
204         "8009: Impending cache battery pack failure"},
205         {0x02040400, 0, 0,
206         "34FF: Disk device format in progress"},
207         {0x023F0000, 0, 0,
208         "Synchronization required"},
209         {0x024E0000, 0, 0,
210         "No ready, IOA shutdown"},
211         {0x025A0000, 0, 0,
212         "Not ready, IOA has been shutdown"},
213         {0x02670100, 0, 1,
214         "3020: Storage subsystem configuration error"},
215         {0x03110B00, 0, 0,
216         "FFF5: Medium error, data unreadable, recommend reassign"},
217         {0x03110C00, 0, 0,
218         "7000: Medium error, data unreadable, do not reassign"},
219         {0x03310000, 0, 1,
220         "FFF3: Disk media format bad"},
221         {0x04050000, 0, 1,
222         "3002: Addressed device failed to respond to selection"},
223         {0x04080000, 1, 1,
224         "3100: Device bus error"},
225         {0x04080100, 0, 1,
226         "3109: IOA timed out a device command"},
227         {0x04088000, 0, 0,
228         "3120: SCSI bus is not operational"},
229         {0x04118000, 0, 1,
230         "9000: IOA reserved area data check"},
231         {0x04118100, 0, 1,
232         "9001: IOA reserved area invalid data pattern"},
233         {0x04118200, 0, 1,
234         "9002: IOA reserved area LRC error"},
235         {0x04320000, 0, 1,
236         "102E: Out of alternate sectors for disk storage"},
237         {0x04330000, 1, 1,
238         "FFF4: Data transfer underlength error"},
239         {0x04338000, 1, 1,
240         "FFF4: Data transfer overlength error"},
241         {0x043E0100, 0, 1,
242         "3400: Logical unit failure"},
243         {0x04408500, 0, 1,
244         "FFF4: Device microcode is corrupt"},
245         {0x04418000, 1, 1,
246         "8150: PCI bus error"},
247         {0x04430000, 1, 0,
248         "Unsupported device bus message received"},
249         {0x04440000, 1, 1,
250         "FFF4: Disk device problem"},
251         {0x04448200, 1, 1,
252         "8150: Permanent IOA failure"},
253         {0x04448300, 0, 1,
254         "3010: Disk device returned wrong response to IOA"},
255         {0x04448400, 0, 1,
256         "8151: IOA microcode error"},
257         {0x04448500, 0, 0,
258         "Device bus status error"},
259         {0x04448600, 0, 1,
260         "8157: IOA error requiring IOA reset to recover"},
261         {0x04490000, 0, 0,
262         "Message reject received from the device"},
263         {0x04449200, 0, 1,
264         "8008: A permanent cache battery pack failure occurred"},
265         {0x0444A000, 0, 1,
266         "9090: Disk unit has been modified after the last known status"},
267         {0x0444A200, 0, 1,
268         "9081: IOA detected device error"},
269         {0x0444A300, 0, 1,
270         "9082: IOA detected device error"},
271         {0x044A0000, 1, 1,
272         "3110: Device bus error, message or command phase"},
273         {0x04670400, 0, 1,
274         "9091: Incorrect hardware configuration change has been detected"},
275         {0x046E0000, 0, 1,
276         "FFF4: Command to logical unit failed"},
277         {0x05240000, 1, 0,
278         "Illegal request, invalid request type or request packet"},
279         {0x05250000, 0, 0,
280         "Illegal request, invalid resource handle"},
281         {0x05260000, 0, 0,
282         "Illegal request, invalid field in parameter list"},
283         {0x05260100, 0, 0,
284         "Illegal request, parameter not supported"},
285         {0x05260200, 0, 0,
286         "Illegal request, parameter value invalid"},
287         {0x052C0000, 0, 0,
288         "Illegal request, command sequence error"},
289         {0x06040500, 0, 1,
290         "9031: Array protection temporarily suspended, protection resuming"},
291         {0x06040600, 0, 1,
292         "9040: Array protection temporarily suspended, protection resuming"},
293         {0x06290000, 0, 1,
294         "FFFB: SCSI bus was reset"},
295         {0x06290500, 0, 0,
296         "FFFE: SCSI bus transition to single ended"},
297         {0x06290600, 0, 0,
298         "FFFE: SCSI bus transition to LVD"},
299         {0x06298000, 0, 1,
300         "FFFB: SCSI bus was reset by another initiator"},
301         {0x063F0300, 0, 1,
302         "3029: A device replacement has occurred"},
303         {0x064C8000, 0, 1,
304         "9051: IOA cache data exists for a missing or failed device"},
305         {0x06670100, 0, 1,
306         "9025: Disk unit is not supported at its physical location"},
307         {0x06670600, 0, 1,
308         "3020: IOA detected a SCSI bus configuration error"},
309         {0x06678000, 0, 1,
310         "3150: SCSI bus configuration error"},
311         {0x06690200, 0, 1,
312         "9041: Array protection temporarily suspended"},
313         {0x066B0200, 0, 1,
314         "9030: Array no longer protected due to missing or failed disk unit"},
315         {0x07270000, 0, 0,
316         "Failure due to other device"},
317         {0x07278000, 0, 1,
318         "9008: IOA does not support functions expected by devices"},
319         {0x07278100, 0, 1,
320         "9010: Cache data associated with attached devices cannot be found"},
321         {0x07278200, 0, 1,
322         "9011: Cache data belongs to devices other than those attached"},
323         {0x07278400, 0, 1,
324         "9020: Array missing 2 or more devices with only 1 device present"},
325         {0x07278500, 0, 1,
326         "9021: Array missing 2 or more devices with 2 or more devices present"},
327         {0x07278600, 0, 1,
328         "9022: Exposed array is missing a required device"},
329         {0x07278700, 0, 1,
330         "9023: Array member(s) not at required physical locations"},
331         {0x07278800, 0, 1,
332         "9024: Array not functional due to present hardware configuration"},
333         {0x07278900, 0, 1,
334         "9026: Array not functional due to present hardware configuration"},
335         {0x07278A00, 0, 1,
336         "9027: Array is missing a device and parity is out of sync"},
337         {0x07278B00, 0, 1,
338         "9028: Maximum number of arrays already exist"},
339         {0x07278C00, 0, 1,
340         "9050: Required cache data cannot be located for a disk unit"},
341         {0x07278D00, 0, 1,
342         "9052: Cache data exists for a device that has been modified"},
343         {0x07278F00, 0, 1,
344         "9054: IOA resources not available due to previous problems"},
345         {0x07279100, 0, 1,
346         "9092: Disk unit requires initialization before use"},
347         {0x07279200, 0, 1,
348         "9029: Incorrect hardware configuration change has been detected"},
349         {0x07279600, 0, 1,
350         "9060: One or more disk pairs are missing from an array"},
351         {0x07279700, 0, 1,
352         "9061: One or more disks are missing from an array"},
353         {0x07279800, 0, 1,
354         "9062: One or more disks are missing from an array"},
355         {0x07279900, 0, 1,
356         "9063: Maximum number of functional arrays has been exceeded"},
357         {0x0B260000, 0, 0,
358         "Aborted command, invalid descriptor"},
359         {0x0B5A0000, 0, 0,
360         "Command terminated by host"}
361 };
362
363 static const struct ipr_ses_table_entry ipr_ses_table[] = {
364         { "2104-DL1        ", "XXXXXXXXXXXXXXXX", 80 },
365         { "2104-TL1        ", "XXXXXXXXXXXXXXXX", 80 },
366         { "HSBP07M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 7 slot */
367         { "HSBP05M P U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Hidive 5 slot */
368         { "HSBP05M S U2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* Bowtie */
369         { "HSBP06E ASU2SCSI", "XXXXXXXXXXXXXXXX", 80 }, /* MartinFenning */
370         { "2104-DU3        ", "XXXXXXXXXXXXXXXX", 160 },
371         { "2104-TU3        ", "XXXXXXXXXXXXXXXX", 160 },
372         { "HSBP04C RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
373         { "HSBP06E RSU2SCSI", "XXXXXXX*XXXXXXXX", 160 },
374         { "St  V1S2        ", "XXXXXXXXXXXXXXXX", 160 },
375         { "HSBPD4M  PU3SCSI", "XXXXXXX*XXXXXXXX", 160 },
376         { "VSBPD1H   U3SCSI", "XXXXXXX*XXXXXXXX", 160 }
377 };
378
379 /*
380  *  Function Prototypes
381  */
382 static int ipr_reset_alert(struct ipr_cmnd *);
383 static void ipr_process_ccn(struct ipr_cmnd *);
384 static void ipr_process_error(struct ipr_cmnd *);
385 static void ipr_reset_ioa_job(struct ipr_cmnd *);
386 static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *,
387                                    enum ipr_shutdown_type);
388
389 #ifdef CONFIG_SCSI_IPR_TRACE
390 /**
391  * ipr_trc_hook - Add a trace entry to the driver trace
392  * @ipr_cmd:    ipr command struct
393  * @type:               trace type
394  * @add_data:   additional data
395  *
396  * Return value:
397  *      none
398  **/
399 static void ipr_trc_hook(struct ipr_cmnd *ipr_cmd,
400                          u8 type, u32 add_data)
401 {
402         struct ipr_trace_entry *trace_entry;
403         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
404
405         trace_entry = &ioa_cfg->trace[ioa_cfg->trace_index++];
406         trace_entry->time = jiffies;
407         trace_entry->op_code = ipr_cmd->ioarcb.cmd_pkt.cdb[0];
408         trace_entry->type = type;
409         trace_entry->cmd_index = ipr_cmd->cmd_index;
410         trace_entry->res_handle = ipr_cmd->ioarcb.res_handle;
411         trace_entry->u.add_data = add_data;
412 }
413 #else
414 #define ipr_trc_hook(ipr_cmd, type, add_data) do { } while(0)
415 #endif
416
417 /**
418  * ipr_reinit_ipr_cmnd - Re-initialize an IPR Cmnd block for reuse
419  * @ipr_cmd:    ipr command struct
420  *
421  * Return value:
422  *      none
423  **/
424 static void ipr_reinit_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
425 {
426         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
427         struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
428
429         memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
430         ioarcb->write_data_transfer_length = 0;
431         ioarcb->read_data_transfer_length = 0;
432         ioarcb->write_ioadl_len = 0;
433         ioarcb->read_ioadl_len = 0;
434         ioasa->ioasc = 0;
435         ioasa->residual_data_len = 0;
436
437         ipr_cmd->scsi_cmd = NULL;
438         ipr_cmd->sense_buffer[0] = 0;
439         ipr_cmd->dma_use_sg = 0;
440 }
441
442 /**
443  * ipr_init_ipr_cmnd - Initialize an IPR Cmnd block
444  * @ipr_cmd:    ipr command struct
445  *
446  * Return value:
447  *      none
448  **/
449 static void ipr_init_ipr_cmnd(struct ipr_cmnd *ipr_cmd)
450 {
451         ipr_reinit_ipr_cmnd(ipr_cmd);
452         ipr_cmd->u.scratch = 0;
453         ipr_cmd->sibling = NULL;
454         init_timer(&ipr_cmd->timer);
455 }
456
457 /**
458  * ipr_get_free_ipr_cmnd - Get a free IPR Cmnd block
459  * @ioa_cfg:    ioa config struct
460  *
461  * Return value:
462  *      pointer to ipr command struct
463  **/
464 static
465 struct ipr_cmnd *ipr_get_free_ipr_cmnd(struct ipr_ioa_cfg *ioa_cfg)
466 {
467         struct ipr_cmnd *ipr_cmd;
468
469         ipr_cmd = list_entry(ioa_cfg->free_q.next, struct ipr_cmnd, queue);
470         list_del(&ipr_cmd->queue);
471         ipr_init_ipr_cmnd(ipr_cmd);
472
473         return ipr_cmd;
474 }
475
476 /**
477  * ipr_unmap_sglist - Unmap scatterlist if mapped
478  * @ioa_cfg:    ioa config struct
479  * @ipr_cmd:    ipr command struct
480  *
481  * Return value:
482  *      nothing
483  **/
484 static void ipr_unmap_sglist(struct ipr_ioa_cfg *ioa_cfg,
485                              struct ipr_cmnd *ipr_cmd)
486 {
487         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
488
489         if (ipr_cmd->dma_use_sg) {
490                 if (scsi_cmd->use_sg > 0) {
491                         pci_unmap_sg(ioa_cfg->pdev, scsi_cmd->request_buffer,
492                                      scsi_cmd->use_sg,
493                                      scsi_cmd->sc_data_direction);
494                 } else {
495                         pci_unmap_single(ioa_cfg->pdev, ipr_cmd->dma_handle,
496                                          scsi_cmd->request_bufflen,
497                                          scsi_cmd->sc_data_direction);
498                 }
499         }
500 }
501
502 /**
503  * ipr_mask_and_clear_interrupts - Mask all and clear specified interrupts
504  * @ioa_cfg:    ioa config struct
505  * @clr_ints:     interrupts to clear
506  *
507  * This function masks all interrupts on the adapter, then clears the
508  * interrupts specified in the mask
509  *
510  * Return value:
511  *      none
512  **/
513 static void ipr_mask_and_clear_interrupts(struct ipr_ioa_cfg *ioa_cfg,
514                                           u32 clr_ints)
515 {
516         volatile u32 int_reg;
517
518         /* Stop new interrupts */
519         ioa_cfg->allow_interrupts = 0;
520
521         /* Set interrupt mask to stop all new interrupts */
522         writel(~0, ioa_cfg->regs.set_interrupt_mask_reg);
523
524         /* Clear any pending interrupts */
525         writel(clr_ints, ioa_cfg->regs.clr_interrupt_reg);
526         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
527 }
528
529 /**
530  * ipr_save_pcix_cmd_reg - Save PCI-X command register
531  * @ioa_cfg:    ioa config struct
532  *
533  * Return value:
534  *      0 on success / -EIO on failure
535  **/
536 static int ipr_save_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
537 {
538         int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
539
540         if (pcix_cmd_reg == 0) {
541                 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
542                 return -EIO;
543         }
544
545         if (pci_read_config_word(ioa_cfg->pdev, pcix_cmd_reg,
546                                  &ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
547                 dev_err(&ioa_cfg->pdev->dev, "Failed to save PCI-X command register\n");
548                 return -EIO;
549         }
550
551         ioa_cfg->saved_pcix_cmd_reg |= PCI_X_CMD_DPERR_E | PCI_X_CMD_ERO;
552         return 0;
553 }
554
555 /**
556  * ipr_set_pcix_cmd_reg - Setup PCI-X command register
557  * @ioa_cfg:    ioa config struct
558  *
559  * Return value:
560  *      0 on success / -EIO on failure
561  **/
562 static int ipr_set_pcix_cmd_reg(struct ipr_ioa_cfg *ioa_cfg)
563 {
564         int pcix_cmd_reg = pci_find_capability(ioa_cfg->pdev, PCI_CAP_ID_PCIX);
565
566         if (pcix_cmd_reg) {
567                 if (pci_write_config_word(ioa_cfg->pdev, pcix_cmd_reg,
568                                           ioa_cfg->saved_pcix_cmd_reg) != PCIBIOS_SUCCESSFUL) {
569                         dev_err(&ioa_cfg->pdev->dev, "Failed to setup PCI-X command register\n");
570                         return -EIO;
571                 }
572         } else {
573                 dev_err(&ioa_cfg->pdev->dev,
574                         "Failed to setup PCI-X command register\n");
575                 return -EIO;
576         }
577
578         return 0;
579 }
580
581 /**
582  * ipr_scsi_eh_done - mid-layer done function for aborted ops
583  * @ipr_cmd:    ipr command struct
584  *
585  * This function is invoked by the interrupt handler for
586  * ops generated by the SCSI mid-layer which are being aborted.
587  *
588  * Return value:
589  *      none
590  **/
591 static void ipr_scsi_eh_done(struct ipr_cmnd *ipr_cmd)
592 {
593         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
594         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
595
596         scsi_cmd->result |= (DID_ERROR << 16);
597
598         ipr_unmap_sglist(ioa_cfg, ipr_cmd);
599         scsi_cmd->scsi_done(scsi_cmd);
600         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
601 }
602
603 /**
604  * ipr_fail_all_ops - Fails all outstanding ops.
605  * @ioa_cfg:    ioa config struct
606  *
607  * This function fails all outstanding ops.
608  *
609  * Return value:
610  *      none
611  **/
612 static void ipr_fail_all_ops(struct ipr_ioa_cfg *ioa_cfg)
613 {
614         struct ipr_cmnd *ipr_cmd, *temp;
615
616         ENTER;
617         list_for_each_entry_safe(ipr_cmd, temp, &ioa_cfg->pending_q, queue) {
618                 list_del(&ipr_cmd->queue);
619
620                 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_IOA_WAS_RESET);
621                 ipr_cmd->ioasa.ilid = cpu_to_be32(IPR_DRIVER_ILID);
622
623                 if (ipr_cmd->scsi_cmd)
624                         ipr_cmd->done = ipr_scsi_eh_done;
625
626                 ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, IPR_IOASC_IOA_WAS_RESET);
627                 del_timer(&ipr_cmd->timer);
628                 ipr_cmd->done(ipr_cmd);
629         }
630
631         LEAVE;
632 }
633
634 /**
635  * ipr_do_req -  Send driver initiated requests.
636  * @ipr_cmd:            ipr command struct
637  * @done:                       done function
638  * @timeout_func:       timeout function
639  * @timeout:            timeout value
640  *
641  * This function sends the specified command to the adapter with the
642  * timeout given. The done function is invoked on command completion.
643  *
644  * Return value:
645  *      none
646  **/
647 static void ipr_do_req(struct ipr_cmnd *ipr_cmd,
648                        void (*done) (struct ipr_cmnd *),
649                        void (*timeout_func) (struct ipr_cmnd *), u32 timeout)
650 {
651         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
652
653         list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
654
655         ipr_cmd->done = done;
656
657         ipr_cmd->timer.data = (unsigned long) ipr_cmd;
658         ipr_cmd->timer.expires = jiffies + timeout;
659         ipr_cmd->timer.function = (void (*)(unsigned long))timeout_func;
660
661         add_timer(&ipr_cmd->timer);
662
663         ipr_trc_hook(ipr_cmd, IPR_TRACE_START, 0);
664
665         mb();
666         writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
667                ioa_cfg->regs.ioarrin_reg);
668 }
669
670 /**
671  * ipr_internal_cmd_done - Op done function for an internally generated op.
672  * @ipr_cmd:    ipr command struct
673  *
674  * This function is the op done function for an internally generated,
675  * blocking op. It simply wakes the sleeping thread.
676  *
677  * Return value:
678  *      none
679  **/
680 static void ipr_internal_cmd_done(struct ipr_cmnd *ipr_cmd)
681 {
682         if (ipr_cmd->sibling)
683                 ipr_cmd->sibling = NULL;
684         else
685                 complete(&ipr_cmd->completion);
686 }
687
688 /**
689  * ipr_send_blocking_cmd - Send command and sleep on its completion.
690  * @ipr_cmd:    ipr command struct
691  * @timeout_func:       function to invoke if command times out
692  * @timeout:    timeout
693  *
694  * Return value:
695  *      none
696  **/
697 static void ipr_send_blocking_cmd(struct ipr_cmnd *ipr_cmd,
698                                   void (*timeout_func) (struct ipr_cmnd *ipr_cmd),
699                                   u32 timeout)
700 {
701         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
702
703         init_completion(&ipr_cmd->completion);
704         ipr_do_req(ipr_cmd, ipr_internal_cmd_done, timeout_func, timeout);
705
706         spin_unlock_irq(ioa_cfg->host->host_lock);
707         wait_for_completion(&ipr_cmd->completion);
708         spin_lock_irq(ioa_cfg->host->host_lock);
709 }
710
711 /**
712  * ipr_send_hcam - Send an HCAM to the adapter.
713  * @ioa_cfg:    ioa config struct
714  * @type:               HCAM type
715  * @hostrcb:    hostrcb struct
716  *
717  * This function will send a Host Controlled Async command to the adapter.
718  * If HCAMs are currently not allowed to be issued to the adapter, it will
719  * place the hostrcb on the free queue.
720  *
721  * Return value:
722  *      none
723  **/
724 static void ipr_send_hcam(struct ipr_ioa_cfg *ioa_cfg, u8 type,
725                           struct ipr_hostrcb *hostrcb)
726 {
727         struct ipr_cmnd *ipr_cmd;
728         struct ipr_ioarcb *ioarcb;
729
730         if (ioa_cfg->allow_cmds) {
731                 ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
732                 list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
733                 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_pending_q);
734
735                 ipr_cmd->u.hostrcb = hostrcb;
736                 ioarcb = &ipr_cmd->ioarcb;
737
738                 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
739                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_HCAM;
740                 ioarcb->cmd_pkt.cdb[0] = IPR_HOST_CONTROLLED_ASYNC;
741                 ioarcb->cmd_pkt.cdb[1] = type;
742                 ioarcb->cmd_pkt.cdb[7] = (sizeof(hostrcb->hcam) >> 8) & 0xff;
743                 ioarcb->cmd_pkt.cdb[8] = sizeof(hostrcb->hcam) & 0xff;
744
745                 ioarcb->read_data_transfer_length = cpu_to_be32(sizeof(hostrcb->hcam));
746                 ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
747                 ipr_cmd->ioadl[0].flags_and_data_len =
748                         cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | sizeof(hostrcb->hcam));
749                 ipr_cmd->ioadl[0].address = cpu_to_be32(hostrcb->hostrcb_dma);
750
751                 if (type == IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE)
752                         ipr_cmd->done = ipr_process_ccn;
753                 else
754                         ipr_cmd->done = ipr_process_error;
755
756                 ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_IOA_RES_ADDR);
757
758                 mb();
759                 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
760                        ioa_cfg->regs.ioarrin_reg);
761         } else {
762                 list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
763         }
764 }
765
766 /**
767  * ipr_init_res_entry - Initialize a resource entry struct.
768  * @res:        resource entry struct
769  *
770  * Return value:
771  *      none
772  **/
773 static void ipr_init_res_entry(struct ipr_resource_entry *res)
774 {
775         res->needs_sync_complete = 1;
776         res->in_erp = 0;
777         res->add_to_ml = 0;
778         res->del_from_ml = 0;
779         res->resetting_device = 0;
780         res->tcq_active = 0;
781         res->qdepth = IPR_MAX_CMD_PER_LUN;
782         res->sdev = NULL;
783 }
784
785 /**
786  * ipr_handle_config_change - Handle a config change from the adapter
787  * @ioa_cfg:    ioa config struct
788  * @hostrcb:    hostrcb
789  *
790  * Return value:
791  *      none
792  **/
793 static void ipr_handle_config_change(struct ipr_ioa_cfg *ioa_cfg,
794                               struct ipr_hostrcb *hostrcb)
795 {
796         struct ipr_resource_entry *res = NULL;
797         struct ipr_config_table_entry *cfgte;
798         u32 is_ndn = 1;
799
800         cfgte = &hostrcb->hcam.u.ccn.cfgte;
801
802         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
803                 if (!memcmp(&res->cfgte.res_addr, &cfgte->res_addr,
804                             sizeof(cfgte->res_addr))) {
805                         is_ndn = 0;
806                         break;
807                 }
808         }
809
810         if (is_ndn) {
811                 if (list_empty(&ioa_cfg->free_res_q)) {
812                         ipr_send_hcam(ioa_cfg,
813                                       IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE,
814                                       hostrcb);
815                         return;
816                 }
817
818                 res = list_entry(ioa_cfg->free_res_q.next,
819                                  struct ipr_resource_entry, queue);
820
821                 list_del(&res->queue);
822                 ipr_init_res_entry(res);
823                 list_add_tail(&res->queue, &ioa_cfg->used_res_q);
824         }
825
826         memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
827
828         if (hostrcb->hcam.notify_type == IPR_HOST_RCB_NOTIF_TYPE_REM_ENTRY) {
829                 if (res->sdev) {
830                         res->sdev->hostdata = NULL;
831                         res->del_from_ml = 1;
832                         if (ioa_cfg->allow_ml_add_del)
833                                 schedule_work(&ioa_cfg->work_q);
834                 } else
835                         list_move_tail(&res->queue, &ioa_cfg->free_res_q);
836         } else if (!res->sdev) {
837                 res->add_to_ml = 1;
838                 if (ioa_cfg->allow_ml_add_del)
839                         schedule_work(&ioa_cfg->work_q);
840         }
841
842         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
843 }
844
845 /**
846  * ipr_process_ccn - Op done function for a CCN.
847  * @ipr_cmd:    ipr command struct
848  *
849  * This function is the op done function for a configuration
850  * change notification host controlled async from the adapter.
851  *
852  * Return value:
853  *      none
854  **/
855 static void ipr_process_ccn(struct ipr_cmnd *ipr_cmd)
856 {
857         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
858         struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
859         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
860
861         list_del(&hostrcb->queue);
862         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
863
864         if (ioasc) {
865                 if (ioasc != IPR_IOASC_IOA_WAS_RESET)
866                         dev_err(&ioa_cfg->pdev->dev,
867                                 "Host RCB failed with IOASC: 0x%08X\n", ioasc);
868
869                 ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
870         } else {
871                 ipr_handle_config_change(ioa_cfg, hostrcb);
872         }
873 }
874
875 /**
876  * ipr_log_vpd - Log the passed VPD to the error log.
877  * @vpids:                      vendor/product id struct
878  * @serial_num:         serial number string
879  *
880  * Return value:
881  *      none
882  **/
883 static void ipr_log_vpd(struct ipr_std_inq_vpids *vpids, u8 *serial_num)
884 {
885         char buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN
886                     + IPR_SERIAL_NUM_LEN];
887
888         memcpy(buffer, vpids->vendor_id, IPR_VENDOR_ID_LEN);
889         memcpy(buffer + IPR_VENDOR_ID_LEN, vpids->product_id,
890                IPR_PROD_ID_LEN);
891         buffer[IPR_VENDOR_ID_LEN + IPR_PROD_ID_LEN] = '\0';
892         ipr_err("Vendor/Product ID: %s\n", buffer);
893
894         memcpy(buffer, serial_num, IPR_SERIAL_NUM_LEN);
895         buffer[IPR_SERIAL_NUM_LEN] = '\0';
896         ipr_err("    Serial Number: %s\n", buffer);
897 }
898
899 /**
900  * ipr_log_cache_error - Log a cache error.
901  * @ioa_cfg:    ioa config struct
902  * @hostrcb:    hostrcb struct
903  *
904  * Return value:
905  *      none
906  **/
907 static void ipr_log_cache_error(struct ipr_ioa_cfg *ioa_cfg,
908                                 struct ipr_hostrcb *hostrcb)
909 {
910         struct ipr_hostrcb_type_02_error *error =
911                 &hostrcb->hcam.u.error.u.type_02_error;
912
913         ipr_err("-----Current Configuration-----\n");
914         ipr_err("Cache Directory Card Information:\n");
915         ipr_log_vpd(&error->ioa_vpids, error->ioa_sn);
916         ipr_err("Adapter Card Information:\n");
917         ipr_log_vpd(&error->cfc_vpids, error->cfc_sn);
918
919         ipr_err("-----Expected Configuration-----\n");
920         ipr_err("Cache Directory Card Information:\n");
921         ipr_log_vpd(&error->ioa_last_attached_to_cfc_vpids,
922                     error->ioa_last_attached_to_cfc_sn);
923         ipr_err("Adapter Card Information:\n");
924         ipr_log_vpd(&error->cfc_last_attached_to_ioa_vpids,
925                     error->cfc_last_attached_to_ioa_sn);
926
927         ipr_err("Additional IOA Data: %08X %08X %08X\n",
928                      be32_to_cpu(error->ioa_data[0]),
929                      be32_to_cpu(error->ioa_data[1]),
930                      be32_to_cpu(error->ioa_data[2]));
931 }
932
933 /**
934  * ipr_log_config_error - Log a configuration error.
935  * @ioa_cfg:    ioa config struct
936  * @hostrcb:    hostrcb struct
937  *
938  * Return value:
939  *      none
940  **/
941 static void ipr_log_config_error(struct ipr_ioa_cfg *ioa_cfg,
942                                  struct ipr_hostrcb *hostrcb)
943 {
944         int errors_logged, i;
945         struct ipr_hostrcb_device_data_entry *dev_entry;
946         struct ipr_hostrcb_type_03_error *error;
947
948         error = &hostrcb->hcam.u.error.u.type_03_error;
949         errors_logged = be32_to_cpu(error->errors_logged);
950
951         ipr_err("Device Errors Detected/Logged: %d/%d\n",
952                 be32_to_cpu(error->errors_detected), errors_logged);
953
954         dev_entry = error->dev_entry;
955
956         for (i = 0; i < errors_logged; i++, dev_entry++) {
957                 ipr_err_separator;
958
959                 if (dev_entry->dev_res_addr.bus >= IPR_MAX_NUM_BUSES) {
960                         ipr_err("Device %d: missing\n", i + 1);
961                 } else {
962                         ipr_err("Device %d: %d:%d:%d:%d\n", i + 1,
963                                 ioa_cfg->host->host_no, dev_entry->dev_res_addr.bus,
964                                 dev_entry->dev_res_addr.target, dev_entry->dev_res_addr.lun);
965                 }
966                 ipr_log_vpd(&dev_entry->dev_vpids, dev_entry->dev_sn);
967
968                 ipr_err("-----New Device Information-----\n");
969                 ipr_log_vpd(&dev_entry->new_dev_vpids, dev_entry->new_dev_sn);
970
971                 ipr_err("Cache Directory Card Information:\n");
972                 ipr_log_vpd(&dev_entry->ioa_last_with_dev_vpids,
973                             dev_entry->ioa_last_with_dev_sn);
974
975                 ipr_err("Adapter Card Information:\n");
976                 ipr_log_vpd(&dev_entry->cfc_last_with_dev_vpids,
977                             dev_entry->cfc_last_with_dev_sn);
978
979                 ipr_err("Additional IOA Data: %08X %08X %08X %08X %08X\n",
980                         be32_to_cpu(dev_entry->ioa_data[0]),
981                         be32_to_cpu(dev_entry->ioa_data[1]),
982                         be32_to_cpu(dev_entry->ioa_data[2]),
983                         be32_to_cpu(dev_entry->ioa_data[3]),
984                         be32_to_cpu(dev_entry->ioa_data[4]));
985         }
986 }
987
988 /**
989  * ipr_log_array_error - Log an array configuration error.
990  * @ioa_cfg:    ioa config struct
991  * @hostrcb:    hostrcb struct
992  *
993  * Return value:
994  *      none
995  **/
996 static void ipr_log_array_error(struct ipr_ioa_cfg *ioa_cfg,
997                                 struct ipr_hostrcb *hostrcb)
998 {
999         int i;
1000         struct ipr_hostrcb_type_04_error *error;
1001         struct ipr_hostrcb_array_data_entry *array_entry;
1002         u8 zero_sn[IPR_SERIAL_NUM_LEN];
1003
1004         memset(zero_sn, '0', IPR_SERIAL_NUM_LEN);
1005
1006         error = &hostrcb->hcam.u.error.u.type_04_error;
1007
1008         ipr_err_separator;
1009
1010         ipr_err("RAID %s Array Configuration: %d:%d:%d:%d\n",
1011                 error->protection_level,
1012                 ioa_cfg->host->host_no,
1013                 error->last_func_vset_res_addr.bus,
1014                 error->last_func_vset_res_addr.target,
1015                 error->last_func_vset_res_addr.lun);
1016
1017         ipr_err_separator;
1018
1019         array_entry = error->array_member;
1020
1021         for (i = 0; i < 18; i++) {
1022                 if (!memcmp(array_entry->serial_num, zero_sn, IPR_SERIAL_NUM_LEN))
1023                         continue;
1024
1025                 if (error->exposed_mode_adn == i) {
1026                         ipr_err("Exposed Array Member %d:\n", i);
1027                 } else {
1028                         ipr_err("Array Member %d:\n", i);
1029                 }
1030
1031                 ipr_log_vpd(&array_entry->vpids, array_entry->serial_num);
1032
1033                 if (array_entry->dev_res_addr.bus >= IPR_MAX_NUM_BUSES) {
1034                         ipr_err("Current Location: unknown\n");
1035                 } else {
1036                         ipr_err("Current Location: %d:%d:%d:%d\n",
1037                                 ioa_cfg->host->host_no,
1038                                 array_entry->dev_res_addr.bus,
1039                                 array_entry->dev_res_addr.target,
1040                                 array_entry->dev_res_addr.lun);
1041                 }
1042
1043                 if (array_entry->dev_res_addr.bus >= IPR_MAX_NUM_BUSES) {
1044                         ipr_err("Expected Location: unknown\n");
1045                 } else {
1046                         ipr_err("Expected Location: %d:%d:%d:%d\n",
1047                                 ioa_cfg->host->host_no,
1048                                 array_entry->expected_dev_res_addr.bus,
1049                                 array_entry->expected_dev_res_addr.target,
1050                                 array_entry->expected_dev_res_addr.lun);
1051                 }
1052
1053                 ipr_err_separator;
1054
1055                 if (i == 9)
1056                         array_entry = error->array_member2;
1057                 else
1058                         array_entry++;
1059         }
1060 }
1061
1062 /**
1063  * ipr_log_generic_error - Log an adapter error.
1064  * @ioa_cfg:    ioa config struct
1065  * @hostrcb:    hostrcb struct
1066  *
1067  * Return value:
1068  *      none
1069  **/
1070 static void ipr_log_generic_error(struct ipr_ioa_cfg *ioa_cfg,
1071                                   struct ipr_hostrcb *hostrcb)
1072 {
1073         int i;
1074         int ioa_data_len = be32_to_cpu(hostrcb->hcam.length);
1075
1076         if (ioa_data_len == 0)
1077                 return;
1078
1079         ipr_err("IOA Error Data:\n");
1080         ipr_err("Offset    0 1 2 3  4 5 6 7  8 9 A B  C D E F\n");
1081
1082         for (i = 0; i < ioa_data_len / 4; i += 4) {
1083                 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
1084                         be32_to_cpu(hostrcb->hcam.u.raw.data[i]),
1085                         be32_to_cpu(hostrcb->hcam.u.raw.data[i+1]),
1086                         be32_to_cpu(hostrcb->hcam.u.raw.data[i+2]),
1087                         be32_to_cpu(hostrcb->hcam.u.raw.data[i+3]));
1088         }
1089 }
1090
1091 /**
1092  * ipr_get_error - Find the specfied IOASC in the ipr_error_table.
1093  * @ioasc:      IOASC
1094  *
1095  * This function will return the index of into the ipr_error_table
1096  * for the specified IOASC. If the IOASC is not in the table,
1097  * 0 will be returned, which points to the entry used for unknown errors.
1098  *
1099  * Return value:
1100  *      index into the ipr_error_table
1101  **/
1102 static u32 ipr_get_error(u32 ioasc)
1103 {
1104         int i;
1105
1106         for (i = 0; i < ARRAY_SIZE(ipr_error_table); i++)
1107                 if (ipr_error_table[i].ioasc == ioasc)
1108                         return i;
1109
1110         return 0;
1111 }
1112
1113 /**
1114  * ipr_handle_log_data - Log an adapter error.
1115  * @ioa_cfg:    ioa config struct
1116  * @hostrcb:    hostrcb struct
1117  *
1118  * This function logs an adapter error to the system.
1119  *
1120  * Return value:
1121  *      none
1122  **/
1123 static void ipr_handle_log_data(struct ipr_ioa_cfg *ioa_cfg,
1124                                 struct ipr_hostrcb *hostrcb)
1125 {
1126         u32 ioasc;
1127         int error_index;
1128
1129         if (hostrcb->hcam.notify_type != IPR_HOST_RCB_NOTIF_TYPE_ERROR_LOG_ENTRY)
1130                 return;
1131
1132         if (hostrcb->hcam.notifications_lost == IPR_HOST_RCB_NOTIFICATIONS_LOST)
1133                 dev_err(&ioa_cfg->pdev->dev, "Error notifications lost\n");
1134
1135         ioasc = be32_to_cpu(hostrcb->hcam.u.error.failing_dev_ioasc);
1136
1137         if (ioasc == IPR_IOASC_BUS_WAS_RESET ||
1138             ioasc == IPR_IOASC_BUS_WAS_RESET_BY_OTHER) {
1139                 /* Tell the midlayer we had a bus reset so it will handle the UA properly */
1140                 scsi_report_bus_reset(ioa_cfg->host,
1141                                       hostrcb->hcam.u.error.failing_dev_res_addr.bus);
1142         }
1143
1144         error_index = ipr_get_error(ioasc);
1145
1146         if (!ipr_error_table[error_index].log_hcam)
1147                 return;
1148
1149         if (ipr_is_device(&hostrcb->hcam.u.error.failing_dev_res_addr)) {
1150                 ipr_res_err(ioa_cfg, hostrcb->hcam.u.error.failing_dev_res_addr,
1151                             "%s\n", ipr_error_table[error_index].error);
1152         } else {
1153                 dev_err(&ioa_cfg->pdev->dev, "%s\n",
1154                         ipr_error_table[error_index].error);
1155         }
1156
1157         /* Set indication we have logged an error */
1158         ioa_cfg->errors_logged++;
1159
1160         if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
1161                 return;
1162
1163         switch (hostrcb->hcam.overlay_id) {
1164         case IPR_HOST_RCB_OVERLAY_ID_1:
1165                 ipr_log_generic_error(ioa_cfg, hostrcb);
1166                 break;
1167         case IPR_HOST_RCB_OVERLAY_ID_2:
1168                 ipr_log_cache_error(ioa_cfg, hostrcb);
1169                 break;
1170         case IPR_HOST_RCB_OVERLAY_ID_3:
1171                 ipr_log_config_error(ioa_cfg, hostrcb);
1172                 break;
1173         case IPR_HOST_RCB_OVERLAY_ID_4:
1174         case IPR_HOST_RCB_OVERLAY_ID_6:
1175                 ipr_log_array_error(ioa_cfg, hostrcb);
1176                 break;
1177         case IPR_HOST_RCB_OVERLAY_ID_DEFAULT:
1178                 ipr_log_generic_error(ioa_cfg, hostrcb);
1179                 break;
1180         default:
1181                 dev_err(&ioa_cfg->pdev->dev,
1182                         "Unknown error received. Overlay ID: %d\n",
1183                         hostrcb->hcam.overlay_id);
1184                 break;
1185         }
1186 }
1187
1188 /**
1189  * ipr_process_error - Op done function for an adapter error log.
1190  * @ipr_cmd:    ipr command struct
1191  *
1192  * This function is the op done function for an error log host
1193  * controlled async from the adapter. It will log the error and
1194  * send the HCAM back to the adapter.
1195  *
1196  * Return value:
1197  *      none
1198  **/
1199 static void ipr_process_error(struct ipr_cmnd *ipr_cmd)
1200 {
1201         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1202         struct ipr_hostrcb *hostrcb = ipr_cmd->u.hostrcb;
1203         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
1204
1205         list_del(&hostrcb->queue);
1206         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
1207
1208         if (!ioasc) {
1209                 ipr_handle_log_data(ioa_cfg, hostrcb);
1210         } else if (ioasc != IPR_IOASC_IOA_WAS_RESET) {
1211                 dev_err(&ioa_cfg->pdev->dev,
1212                         "Host RCB failed with IOASC: 0x%08X\n", ioasc);
1213         }
1214
1215         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
1216 }
1217
1218 /**
1219  * ipr_timeout -  An internally generated op has timed out.
1220  * @ipr_cmd:    ipr command struct
1221  *
1222  * This function blocks host requests and initiates an
1223  * adapter reset.
1224  *
1225  * Return value:
1226  *      none
1227  **/
1228 static void ipr_timeout(struct ipr_cmnd *ipr_cmd)
1229 {
1230         unsigned long lock_flags = 0;
1231         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
1232
1233         ENTER;
1234         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1235
1236         ioa_cfg->errors_logged++;
1237         dev_err(&ioa_cfg->pdev->dev,
1238                 "Adapter being reset due to command timeout.\n");
1239
1240         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
1241                 ioa_cfg->sdt_state = GET_DUMP;
1242
1243         if (!ioa_cfg->in_reset_reload || ioa_cfg->reset_cmd == ipr_cmd)
1244                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1245
1246         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1247         LEAVE;
1248 }
1249
1250 /**
1251  * ipr_reset_reload - Reset/Reload the IOA
1252  * @ioa_cfg:            ioa config struct
1253  * @shutdown_type:      shutdown type
1254  *
1255  * This function resets the adapter and re-initializes it.
1256  * This function assumes that all new host commands have been stopped.
1257  * Return value:
1258  *      SUCCESS / FAILED
1259  **/
1260 static int ipr_reset_reload(struct ipr_ioa_cfg *ioa_cfg,
1261                             enum ipr_shutdown_type shutdown_type)
1262 {
1263         if (!ioa_cfg->in_reset_reload)
1264                 ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
1265
1266         spin_unlock_irq(ioa_cfg->host->host_lock);
1267         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
1268         spin_lock_irq(ioa_cfg->host->host_lock);
1269
1270         /* If we got hit with a host reset while we were already resetting
1271          the adapter for some reason, and the reset failed. */
1272         if (ioa_cfg->ioa_is_dead) {
1273                 ipr_trace;
1274                 return FAILED;
1275         }
1276
1277         return SUCCESS;
1278 }
1279
1280 /**
1281  * ipr_find_ses_entry - Find matching SES in SES table
1282  * @res:        resource entry struct of SES
1283  *
1284  * Return value:
1285  *      pointer to SES table entry / NULL on failure
1286  **/
1287 static const struct ipr_ses_table_entry *
1288 ipr_find_ses_entry(struct ipr_resource_entry *res)
1289 {
1290         int i, j, matches;
1291         const struct ipr_ses_table_entry *ste = ipr_ses_table;
1292
1293         for (i = 0; i < ARRAY_SIZE(ipr_ses_table); i++, ste++) {
1294                 for (j = 0, matches = 0; j < IPR_PROD_ID_LEN; j++) {
1295                         if (ste->compare_product_id_byte[j] == 'X') {
1296                                 if (res->cfgte.std_inq_data.vpids.product_id[j] == ste->product_id[j])
1297                                         matches++;
1298                                 else
1299                                         break;
1300                         } else
1301                                 matches++;
1302                 }
1303
1304                 if (matches == IPR_PROD_ID_LEN)
1305                         return ste;
1306         }
1307
1308         return NULL;
1309 }
1310
1311 /**
1312  * ipr_get_max_scsi_speed - Determine max SCSI speed for a given bus
1313  * @ioa_cfg:    ioa config struct
1314  * @bus:                SCSI bus
1315  * @bus_width:  bus width
1316  *
1317  * Return value:
1318  *      SCSI bus speed in units of 100KHz, 1600 is 160 MHz
1319  *      For a 2-byte wide SCSI bus, the maximum transfer speed is
1320  *      twice the maximum transfer rate (e.g. for a wide enabled bus,
1321  *      max 160MHz = max 320MB/sec).
1322  **/
1323 static u32 ipr_get_max_scsi_speed(struct ipr_ioa_cfg *ioa_cfg, u8 bus, u8 bus_width)
1324 {
1325         struct ipr_resource_entry *res;
1326         const struct ipr_ses_table_entry *ste;
1327         u32 max_xfer_rate = IPR_MAX_SCSI_RATE(bus_width);
1328
1329         /* Loop through each config table entry in the config table buffer */
1330         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1331                 if (!(IPR_IS_SES_DEVICE(res->cfgte.std_inq_data)))
1332                         continue;
1333
1334                 if (bus != res->cfgte.res_addr.bus)
1335                         continue;
1336
1337                 if (!(ste = ipr_find_ses_entry(res)))
1338                         continue;
1339
1340                 max_xfer_rate = (ste->max_bus_speed_limit * 10) / (bus_width / 8);
1341         }
1342
1343         return max_xfer_rate;
1344 }
1345
1346 /**
1347  * ipr_wait_iodbg_ack - Wait for an IODEBUG ACK from the IOA
1348  * @ioa_cfg:            ioa config struct
1349  * @max_delay:          max delay in micro-seconds to wait
1350  *
1351  * Waits for an IODEBUG ACK from the IOA, doing busy looping.
1352  *
1353  * Return value:
1354  *      0 on success / other on failure
1355  **/
1356 static int ipr_wait_iodbg_ack(struct ipr_ioa_cfg *ioa_cfg, int max_delay)
1357 {
1358         volatile u32 pcii_reg;
1359         int delay = 1;
1360
1361         /* Read interrupt reg until IOA signals IO Debug Acknowledge */
1362         while (delay < max_delay) {
1363                 pcii_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
1364
1365                 if (pcii_reg & IPR_PCII_IO_DEBUG_ACKNOWLEDGE)
1366                         return 0;
1367
1368                 /* udelay cannot be used if delay is more than a few milliseconds */
1369                 if ((delay / 1000) > MAX_UDELAY_MS)
1370                         mdelay(delay / 1000);
1371                 else
1372                         udelay(delay);
1373
1374                 delay += delay;
1375         }
1376         return -EIO;
1377 }
1378
1379 /**
1380  * ipr_get_ldump_data_section - Dump IOA memory
1381  * @ioa_cfg:                    ioa config struct
1382  * @start_addr:                 adapter address to dump
1383  * @dest:                               destination kernel buffer
1384  * @length_in_words:    length to dump in 4 byte words
1385  *
1386  * Return value:
1387  *      0 on success / -EIO on failure
1388  **/
1389 static int ipr_get_ldump_data_section(struct ipr_ioa_cfg *ioa_cfg,
1390                                       u32 start_addr,
1391                                       u32 *dest, u32 length_in_words)
1392 {
1393         volatile u32 temp_pcii_reg;
1394         int i, delay = 0;
1395
1396         /* Write IOA interrupt reg starting LDUMP state  */
1397         writel((IPR_UPROCI_RESET_ALERT | IPR_UPROCI_IO_DEBUG_ALERT),
1398                ioa_cfg->regs.set_uproc_interrupt_reg);
1399
1400         /* Wait for IO debug acknowledge */
1401         if (ipr_wait_iodbg_ack(ioa_cfg,
1402                                IPR_LDUMP_MAX_LONG_ACK_DELAY_IN_USEC)) {
1403                 dev_err(&ioa_cfg->pdev->dev,
1404                         "IOA dump long data transfer timeout\n");
1405                 return -EIO;
1406         }
1407
1408         /* Signal LDUMP interlocked - clear IO debug ack */
1409         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1410                ioa_cfg->regs.clr_interrupt_reg);
1411
1412         /* Write Mailbox with starting address */
1413         writel(start_addr, ioa_cfg->ioa_mailbox);
1414
1415         /* Signal address valid - clear IOA Reset alert */
1416         writel(IPR_UPROCI_RESET_ALERT,
1417                ioa_cfg->regs.clr_uproc_interrupt_reg);
1418
1419         for (i = 0; i < length_in_words; i++) {
1420                 /* Wait for IO debug acknowledge */
1421                 if (ipr_wait_iodbg_ack(ioa_cfg,
1422                                        IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC)) {
1423                         dev_err(&ioa_cfg->pdev->dev,
1424                                 "IOA dump short data transfer timeout\n");
1425                         return -EIO;
1426                 }
1427
1428                 /* Read data from mailbox and increment destination pointer */
1429                 *dest = cpu_to_be32(readl(ioa_cfg->ioa_mailbox));
1430                 dest++;
1431
1432                 /* For all but the last word of data, signal data received */
1433                 if (i < (length_in_words - 1)) {
1434                         /* Signal dump data received - Clear IO debug Ack */
1435                         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1436                                ioa_cfg->regs.clr_interrupt_reg);
1437                 }
1438         }
1439
1440         /* Signal end of block transfer. Set reset alert then clear IO debug ack */
1441         writel(IPR_UPROCI_RESET_ALERT,
1442                ioa_cfg->regs.set_uproc_interrupt_reg);
1443
1444         writel(IPR_UPROCI_IO_DEBUG_ALERT,
1445                ioa_cfg->regs.clr_uproc_interrupt_reg);
1446
1447         /* Signal dump data received - Clear IO debug Ack */
1448         writel(IPR_PCII_IO_DEBUG_ACKNOWLEDGE,
1449                ioa_cfg->regs.clr_interrupt_reg);
1450
1451         /* Wait for IOA to signal LDUMP exit - IOA reset alert will be cleared */
1452         while (delay < IPR_LDUMP_MAX_SHORT_ACK_DELAY_IN_USEC) {
1453                 temp_pcii_reg =
1454                     readl(ioa_cfg->regs.sense_uproc_interrupt_reg);
1455
1456                 if (!(temp_pcii_reg & IPR_UPROCI_RESET_ALERT))
1457                         return 0;
1458
1459                 udelay(10);
1460                 delay += 10;
1461         }
1462
1463         return 0;
1464 }
1465
1466 #ifdef CONFIG_SCSI_IPR_DUMP
1467 /**
1468  * ipr_sdt_copy - Copy Smart Dump Table to kernel buffer
1469  * @ioa_cfg:            ioa config struct
1470  * @pci_address:        adapter address
1471  * @length:                     length of data to copy
1472  *
1473  * Copy data from PCI adapter to kernel buffer.
1474  * Note: length MUST be a 4 byte multiple
1475  * Return value:
1476  *      0 on success / other on failure
1477  **/
1478 static int ipr_sdt_copy(struct ipr_ioa_cfg *ioa_cfg,
1479                         unsigned long pci_address, u32 length)
1480 {
1481         int bytes_copied = 0;
1482         int cur_len, rc, rem_len, rem_page_len;
1483         u32 *page;
1484         unsigned long lock_flags = 0;
1485         struct ipr_ioa_dump *ioa_dump = &ioa_cfg->dump->ioa_dump;
1486
1487         while (bytes_copied < length &&
1488                (ioa_dump->hdr.len + bytes_copied) < IPR_MAX_IOA_DUMP_SIZE) {
1489                 if (ioa_dump->page_offset >= PAGE_SIZE ||
1490                     ioa_dump->page_offset == 0) {
1491                         page = (u32 *)__get_free_page(GFP_ATOMIC);
1492
1493                         if (!page) {
1494                                 ipr_trace;
1495                                 return bytes_copied;
1496                         }
1497
1498                         ioa_dump->page_offset = 0;
1499                         ioa_dump->ioa_data[ioa_dump->next_page_index] = page;
1500                         ioa_dump->next_page_index++;
1501                 } else
1502                         page = ioa_dump->ioa_data[ioa_dump->next_page_index - 1];
1503
1504                 rem_len = length - bytes_copied;
1505                 rem_page_len = PAGE_SIZE - ioa_dump->page_offset;
1506                 cur_len = min(rem_len, rem_page_len);
1507
1508                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1509                 if (ioa_cfg->sdt_state == ABORT_DUMP) {
1510                         rc = -EIO;
1511                 } else {
1512                         rc = ipr_get_ldump_data_section(ioa_cfg,
1513                                                         pci_address + bytes_copied,
1514                                                         &page[ioa_dump->page_offset / 4],
1515                                                         (cur_len / sizeof(u32)));
1516                 }
1517                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1518
1519                 if (!rc) {
1520                         ioa_dump->page_offset += cur_len;
1521                         bytes_copied += cur_len;
1522                 } else {
1523                         ipr_trace;
1524                         break;
1525                 }
1526                 schedule();
1527         }
1528
1529         return bytes_copied;
1530 }
1531
1532 /**
1533  * ipr_init_dump_entry_hdr - Initialize a dump entry header.
1534  * @hdr:        dump entry header struct
1535  *
1536  * Return value:
1537  *      nothing
1538  **/
1539 static void ipr_init_dump_entry_hdr(struct ipr_dump_entry_header *hdr)
1540 {
1541         hdr->eye_catcher = IPR_DUMP_EYE_CATCHER;
1542         hdr->num_elems = 1;
1543         hdr->offset = sizeof(*hdr);
1544         hdr->status = IPR_DUMP_STATUS_SUCCESS;
1545 }
1546
1547 /**
1548  * ipr_dump_ioa_type_data - Fill in the adapter type in the dump.
1549  * @ioa_cfg:    ioa config struct
1550  * @driver_dump:        driver dump struct
1551  *
1552  * Return value:
1553  *      nothing
1554  **/
1555 static void ipr_dump_ioa_type_data(struct ipr_ioa_cfg *ioa_cfg,
1556                                    struct ipr_driver_dump *driver_dump)
1557 {
1558         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
1559
1560         ipr_init_dump_entry_hdr(&driver_dump->ioa_type_entry.hdr);
1561         driver_dump->ioa_type_entry.hdr.len =
1562                 sizeof(struct ipr_dump_ioa_type_entry) -
1563                 sizeof(struct ipr_dump_entry_header);
1564         driver_dump->ioa_type_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
1565         driver_dump->ioa_type_entry.hdr.id = IPR_DUMP_DRIVER_TYPE_ID;
1566         driver_dump->ioa_type_entry.type = ioa_cfg->type;
1567         driver_dump->ioa_type_entry.fw_version = (ucode_vpd->major_release << 24) |
1568                 (ucode_vpd->card_type << 16) | (ucode_vpd->minor_release[0] << 8) |
1569                 ucode_vpd->minor_release[1];
1570         driver_dump->hdr.num_entries++;
1571 }
1572
1573 /**
1574  * ipr_dump_version_data - Fill in the driver version in the dump.
1575  * @ioa_cfg:    ioa config struct
1576  * @driver_dump:        driver dump struct
1577  *
1578  * Return value:
1579  *      nothing
1580  **/
1581 static void ipr_dump_version_data(struct ipr_ioa_cfg *ioa_cfg,
1582                                   struct ipr_driver_dump *driver_dump)
1583 {
1584         ipr_init_dump_entry_hdr(&driver_dump->version_entry.hdr);
1585         driver_dump->version_entry.hdr.len =
1586                 sizeof(struct ipr_dump_version_entry) -
1587                 sizeof(struct ipr_dump_entry_header);
1588         driver_dump->version_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
1589         driver_dump->version_entry.hdr.id = IPR_DUMP_DRIVER_VERSION_ID;
1590         strcpy(driver_dump->version_entry.version, IPR_DRIVER_VERSION);
1591         driver_dump->hdr.num_entries++;
1592 }
1593
1594 /**
1595  * ipr_dump_trace_data - Fill in the IOA trace in the dump.
1596  * @ioa_cfg:    ioa config struct
1597  * @driver_dump:        driver dump struct
1598  *
1599  * Return value:
1600  *      nothing
1601  **/
1602 static void ipr_dump_trace_data(struct ipr_ioa_cfg *ioa_cfg,
1603                                    struct ipr_driver_dump *driver_dump)
1604 {
1605         ipr_init_dump_entry_hdr(&driver_dump->trace_entry.hdr);
1606         driver_dump->trace_entry.hdr.len =
1607                 sizeof(struct ipr_dump_trace_entry) -
1608                 sizeof(struct ipr_dump_entry_header);
1609         driver_dump->trace_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
1610         driver_dump->trace_entry.hdr.id = IPR_DUMP_TRACE_ID;
1611         memcpy(driver_dump->trace_entry.trace, ioa_cfg->trace, IPR_TRACE_SIZE);
1612         driver_dump->hdr.num_entries++;
1613 }
1614
1615 /**
1616  * ipr_dump_location_data - Fill in the IOA location in the dump.
1617  * @ioa_cfg:    ioa config struct
1618  * @driver_dump:        driver dump struct
1619  *
1620  * Return value:
1621  *      nothing
1622  **/
1623 static void ipr_dump_location_data(struct ipr_ioa_cfg *ioa_cfg,
1624                                    struct ipr_driver_dump *driver_dump)
1625 {
1626         ipr_init_dump_entry_hdr(&driver_dump->location_entry.hdr);
1627         driver_dump->location_entry.hdr.len =
1628                 sizeof(struct ipr_dump_location_entry) -
1629                 sizeof(struct ipr_dump_entry_header);
1630         driver_dump->location_entry.hdr.data_type = IPR_DUMP_DATA_TYPE_ASCII;
1631         driver_dump->location_entry.hdr.id = IPR_DUMP_LOCATION_ID;
1632         strcpy(driver_dump->location_entry.location, ioa_cfg->pdev->dev.bus_id);
1633         driver_dump->hdr.num_entries++;
1634 }
1635
1636 /**
1637  * ipr_get_ioa_dump - Perform a dump of the driver and adapter.
1638  * @ioa_cfg:    ioa config struct
1639  * @dump:               dump struct
1640  *
1641  * Return value:
1642  *      nothing
1643  **/
1644 static void ipr_get_ioa_dump(struct ipr_ioa_cfg *ioa_cfg, struct ipr_dump *dump)
1645 {
1646         unsigned long start_addr, sdt_word;
1647         unsigned long lock_flags = 0;
1648         struct ipr_driver_dump *driver_dump = &dump->driver_dump;
1649         struct ipr_ioa_dump *ioa_dump = &dump->ioa_dump;
1650         u32 num_entries, start_off, end_off;
1651         u32 bytes_to_copy, bytes_copied, rc;
1652         struct ipr_sdt *sdt;
1653         int i;
1654
1655         ENTER;
1656
1657         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1658
1659         if (ioa_cfg->sdt_state != GET_DUMP) {
1660                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1661                 return;
1662         }
1663
1664         start_addr = readl(ioa_cfg->ioa_mailbox);
1665
1666         if (!ipr_sdt_is_fmt2(start_addr)) {
1667                 dev_err(&ioa_cfg->pdev->dev,
1668                         "Invalid dump table format: %lx\n", start_addr);
1669                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1670                 return;
1671         }
1672
1673         dev_err(&ioa_cfg->pdev->dev, "Dump of IOA initiated\n");
1674
1675         driver_dump->hdr.eye_catcher = IPR_DUMP_EYE_CATCHER;
1676
1677         /* Initialize the overall dump header */
1678         driver_dump->hdr.len = sizeof(struct ipr_driver_dump);
1679         driver_dump->hdr.num_entries = 1;
1680         driver_dump->hdr.first_entry_offset = sizeof(struct ipr_dump_header);
1681         driver_dump->hdr.status = IPR_DUMP_STATUS_SUCCESS;
1682         driver_dump->hdr.os = IPR_DUMP_OS_LINUX;
1683         driver_dump->hdr.driver_name = IPR_DUMP_DRIVER_NAME;
1684
1685         ipr_dump_version_data(ioa_cfg, driver_dump);
1686         ipr_dump_location_data(ioa_cfg, driver_dump);
1687         ipr_dump_ioa_type_data(ioa_cfg, driver_dump);
1688         ipr_dump_trace_data(ioa_cfg, driver_dump);
1689
1690         /* Update dump_header */
1691         driver_dump->hdr.len += sizeof(struct ipr_dump_entry_header);
1692
1693         /* IOA Dump entry */
1694         ipr_init_dump_entry_hdr(&ioa_dump->hdr);
1695         ioa_dump->format = IPR_SDT_FMT2;
1696         ioa_dump->hdr.len = 0;
1697         ioa_dump->hdr.data_type = IPR_DUMP_DATA_TYPE_BINARY;
1698         ioa_dump->hdr.id = IPR_DUMP_IOA_DUMP_ID;
1699
1700         /* First entries in sdt are actually a list of dump addresses and
1701          lengths to gather the real dump data.  sdt represents the pointer
1702          to the ioa generated dump table.  Dump data will be extracted based
1703          on entries in this table */
1704         sdt = &ioa_dump->sdt;
1705
1706         rc = ipr_get_ldump_data_section(ioa_cfg, start_addr, (u32 *)sdt,
1707                                         sizeof(struct ipr_sdt) / sizeof(u32));
1708
1709         /* Smart Dump table is ready to use and the first entry is valid */
1710         if (rc || (be32_to_cpu(sdt->hdr.state) != IPR_FMT2_SDT_READY_TO_USE)) {
1711                 dev_err(&ioa_cfg->pdev->dev,
1712                         "Dump of IOA failed. Dump table not valid: %d, %X.\n",
1713                         rc, be32_to_cpu(sdt->hdr.state));
1714                 driver_dump->hdr.status = IPR_DUMP_STATUS_FAILED;
1715                 ioa_cfg->sdt_state = DUMP_OBTAINED;
1716                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1717                 return;
1718         }
1719
1720         num_entries = be32_to_cpu(sdt->hdr.num_entries_used);
1721
1722         if (num_entries > IPR_NUM_SDT_ENTRIES)
1723                 num_entries = IPR_NUM_SDT_ENTRIES;
1724
1725         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1726
1727         for (i = 0; i < num_entries; i++) {
1728                 if (ioa_dump->hdr.len > IPR_MAX_IOA_DUMP_SIZE) {
1729                         driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
1730                         break;
1731                 }
1732
1733                 if (sdt->entry[i].flags & IPR_SDT_VALID_ENTRY) {
1734                         sdt_word = be32_to_cpu(sdt->entry[i].bar_str_offset);
1735                         start_off = sdt_word & IPR_FMT2_MBX_ADDR_MASK;
1736                         end_off = be32_to_cpu(sdt->entry[i].end_offset);
1737
1738                         if (ipr_sdt_is_fmt2(sdt_word) && sdt_word) {
1739                                 bytes_to_copy = end_off - start_off;
1740                                 if (bytes_to_copy > IPR_MAX_IOA_DUMP_SIZE) {
1741                                         sdt->entry[i].flags &= ~IPR_SDT_VALID_ENTRY;
1742                                         continue;
1743                                 }
1744
1745                                 /* Copy data from adapter to driver buffers */
1746                                 bytes_copied = ipr_sdt_copy(ioa_cfg, sdt_word,
1747                                                             bytes_to_copy);
1748
1749                                 ioa_dump->hdr.len += bytes_copied;
1750
1751                                 if (bytes_copied != bytes_to_copy) {
1752                                         driver_dump->hdr.status = IPR_DUMP_STATUS_QUAL_SUCCESS;
1753                                         break;
1754                                 }
1755                         }
1756                 }
1757         }
1758
1759         dev_err(&ioa_cfg->pdev->dev, "Dump of IOA completed.\n");
1760
1761         /* Update dump_header */
1762         driver_dump->hdr.len += ioa_dump->hdr.len;
1763         wmb();
1764         ioa_cfg->sdt_state = DUMP_OBTAINED;
1765         LEAVE;
1766 }
1767
1768 #else
1769 #define ipr_get_ioa_dump(ioa_cfg, dump) do { } while(0)
1770 #endif
1771
1772 /**
1773  * ipr_release_dump - Free adapter dump memory
1774  * @kref:       kref struct
1775  *
1776  * Return value:
1777  *      nothing
1778  **/
1779 static void ipr_release_dump(struct kref *kref)
1780 {
1781         struct ipr_dump *dump = container_of(kref,struct ipr_dump,kref);
1782         struct ipr_ioa_cfg *ioa_cfg = dump->ioa_cfg;
1783         unsigned long lock_flags = 0;
1784         int i;
1785
1786         ENTER;
1787         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1788         ioa_cfg->dump = NULL;
1789         ioa_cfg->sdt_state = INACTIVE;
1790         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1791
1792         for (i = 0; i < dump->ioa_dump.next_page_index; i++)
1793                 free_page((unsigned long) dump->ioa_dump.ioa_data[i]);
1794
1795         kfree(dump);
1796         LEAVE;
1797 }
1798
1799 /**
1800  * ipr_worker_thread - Worker thread
1801  * @data:               ioa config struct
1802  *
1803  * Called at task level from a work thread. This function takes care
1804  * of adding and removing device from the mid-layer as configuration
1805  * changes are detected by the adapter.
1806  *
1807  * Return value:
1808  *      nothing
1809  **/
1810 static void ipr_worker_thread(void *data)
1811 {
1812         unsigned long lock_flags;
1813         struct ipr_resource_entry *res;
1814         struct scsi_device *sdev;
1815         struct ipr_dump *dump;
1816         struct ipr_ioa_cfg *ioa_cfg = data;
1817         u8 bus, target, lun;
1818         int did_work;
1819
1820         ENTER;
1821         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1822
1823         if (ioa_cfg->sdt_state == GET_DUMP) {
1824                 dump = ioa_cfg->dump;
1825                 if (!dump) {
1826                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1827                         return;
1828                 }
1829                 kref_get(&dump->kref);
1830                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1831                 ipr_get_ioa_dump(ioa_cfg, dump);
1832                 kref_put(&dump->kref, ipr_release_dump);
1833
1834                 spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1835                 if (ioa_cfg->sdt_state == DUMP_OBTAINED)
1836                         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
1837                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1838                 return;
1839         }
1840
1841 restart:
1842         do {
1843                 did_work = 0;
1844                 if (!ioa_cfg->allow_cmds || !ioa_cfg->allow_ml_add_del) {
1845                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1846                         return;
1847                 }
1848
1849                 list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1850                         if (res->del_from_ml && res->sdev) {
1851                                 did_work = 1;
1852                                 sdev = res->sdev;
1853                                 if (!scsi_device_get(sdev)) {
1854                                         res->sdev = NULL;
1855                                         list_move_tail(&res->queue, &ioa_cfg->free_res_q);
1856                                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1857                                         scsi_remove_device(sdev);
1858                                         scsi_device_put(sdev);
1859                                         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1860                                 }
1861                                 break;
1862                         }
1863                 }
1864         } while(did_work);
1865
1866         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
1867                 if (res->add_to_ml) {
1868                         bus = res->cfgte.res_addr.bus;
1869                         target = res->cfgte.res_addr.target;
1870                         lun = res->cfgte.res_addr.lun;
1871                         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1872                         scsi_add_device(ioa_cfg->host, bus, target, lun);
1873                         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1874                         goto restart;
1875                 }
1876         }
1877
1878         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1879         LEAVE;
1880 }
1881
1882 #ifdef CONFIG_SCSI_IPR_TRACE
1883 /**
1884  * ipr_read_trace - Dump the adapter trace
1885  * @kobj:               kobject struct
1886  * @buf:                buffer
1887  * @off:                offset
1888  * @count:              buffer size
1889  *
1890  * Return value:
1891  *      number of bytes printed to buffer
1892  **/
1893 static ssize_t ipr_read_trace(struct kobject *kobj, char *buf,
1894                               loff_t off, size_t count)
1895 {
1896         struct class_device *cdev = container_of(kobj,struct class_device,kobj);
1897         struct Scsi_Host *shost = class_to_shost(cdev);
1898         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
1899         unsigned long lock_flags = 0;
1900         int size = IPR_TRACE_SIZE;
1901         char *src = (char *)ioa_cfg->trace;
1902
1903         if (off > size)
1904                 return 0;
1905         if (off + count > size) {
1906                 size -= off;
1907                 count = size;
1908         }
1909
1910         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1911         memcpy(buf, &src[off], count);
1912         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1913         return count;
1914 }
1915
1916 static struct bin_attribute ipr_trace_attr = {
1917         .attr = {
1918                 .name = "trace",
1919                 .mode = S_IRUGO,
1920         },
1921         .size = 0,
1922         .read = ipr_read_trace,
1923 };
1924 #endif
1925
1926 /**
1927  * ipr_show_fw_version - Show the firmware version
1928  * @class_dev:  class device struct
1929  * @buf:                buffer
1930  *
1931  * Return value:
1932  *      number of bytes printed to buffer
1933  **/
1934 static ssize_t ipr_show_fw_version(struct class_device *class_dev, char *buf)
1935 {
1936         struct Scsi_Host *shost = class_to_shost(class_dev);
1937         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
1938         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
1939         unsigned long lock_flags = 0;
1940         int len;
1941
1942         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1943         len = snprintf(buf, PAGE_SIZE, "%02X%02X%02X%02X\n",
1944                        ucode_vpd->major_release, ucode_vpd->card_type,
1945                        ucode_vpd->minor_release[0],
1946                        ucode_vpd->minor_release[1]);
1947         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1948         return len;
1949 }
1950
1951 static struct class_device_attribute ipr_fw_version_attr = {
1952         .attr = {
1953                 .name =         "fw_version",
1954                 .mode =         S_IRUGO,
1955         },
1956         .show = ipr_show_fw_version,
1957 };
1958
1959 /**
1960  * ipr_show_log_level - Show the adapter's error logging level
1961  * @class_dev:  class device struct
1962  * @buf:                buffer
1963  *
1964  * Return value:
1965  *      number of bytes printed to buffer
1966  **/
1967 static ssize_t ipr_show_log_level(struct class_device *class_dev, char *buf)
1968 {
1969         struct Scsi_Host *shost = class_to_shost(class_dev);
1970         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
1971         unsigned long lock_flags = 0;
1972         int len;
1973
1974         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1975         len = snprintf(buf, PAGE_SIZE, "%d\n", ioa_cfg->log_level);
1976         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1977         return len;
1978 }
1979
1980 /**
1981  * ipr_store_log_level - Change the adapter's error logging level
1982  * @class_dev:  class device struct
1983  * @buf:                buffer
1984  *
1985  * Return value:
1986  *      number of bytes printed to buffer
1987  **/
1988 static ssize_t ipr_store_log_level(struct class_device *class_dev,
1989                                    const char *buf, size_t count)
1990 {
1991         struct Scsi_Host *shost = class_to_shost(class_dev);
1992         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
1993         unsigned long lock_flags = 0;
1994
1995         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
1996         ioa_cfg->log_level = simple_strtoul(buf, NULL, 10);
1997         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
1998         return strlen(buf);
1999 }
2000
2001 static struct class_device_attribute ipr_log_level_attr = {
2002         .attr = {
2003                 .name =         "log_level",
2004                 .mode =         S_IRUGO | S_IWUSR,
2005         },
2006         .show = ipr_show_log_level,
2007         .store = ipr_store_log_level
2008 };
2009
2010 /**
2011  * ipr_store_diagnostics - IOA Diagnostics interface
2012  * @class_dev:  class_device struct
2013  * @buf:                buffer
2014  * @count:              buffer size
2015  *
2016  * This function will reset the adapter and wait a reasonable
2017  * amount of time for any errors that the adapter might log.
2018  *
2019  * Return value:
2020  *      count on success / other on failure
2021  **/
2022 static ssize_t ipr_store_diagnostics(struct class_device *class_dev,
2023                                      const char *buf, size_t count)
2024 {
2025         struct Scsi_Host *shost = class_to_shost(class_dev);
2026         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2027         unsigned long lock_flags = 0;
2028         int rc = count;
2029
2030         if (!capable(CAP_SYS_ADMIN))
2031                 return -EACCES;
2032
2033         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2034         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2035         ioa_cfg->errors_logged = 0;
2036         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2037
2038         if (ioa_cfg->in_reset_reload) {
2039                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2040                 wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2041
2042                 /* Wait for a second for any errors to be logged */
2043                 msleep(1000);
2044         } else {
2045                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2046                 return -EIO;
2047         }
2048
2049         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2050         if (ioa_cfg->in_reset_reload || ioa_cfg->errors_logged)
2051                 rc = -EIO;
2052         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2053
2054         return rc;
2055 }
2056
2057 static struct class_device_attribute ipr_diagnostics_attr = {
2058         .attr = {
2059                 .name =         "run_diagnostics",
2060                 .mode =         S_IWUSR,
2061         },
2062         .store = ipr_store_diagnostics
2063 };
2064
2065 /**
2066  * ipr_store_reset_adapter - Reset the adapter
2067  * @class_dev:  class_device struct
2068  * @buf:                buffer
2069  * @count:              buffer size
2070  *
2071  * This function will reset the adapter.
2072  *
2073  * Return value:
2074  *      count on success / other on failure
2075  **/
2076 static ssize_t ipr_store_reset_adapter(struct class_device *class_dev,
2077                                        const char *buf, size_t count)
2078 {
2079         struct Scsi_Host *shost = class_to_shost(class_dev);
2080         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2081         unsigned long lock_flags;
2082         int result = count;
2083
2084         if (!capable(CAP_SYS_ADMIN))
2085                 return -EACCES;
2086
2087         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2088         if (!ioa_cfg->in_reset_reload)
2089                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2090         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2091         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2092
2093         return result;
2094 }
2095
2096 static struct class_device_attribute ipr_ioa_reset_attr = {
2097         .attr = {
2098                 .name =         "reset_host",
2099                 .mode =         S_IWUSR,
2100         },
2101         .store = ipr_store_reset_adapter
2102 };
2103
2104 /**
2105  * ipr_alloc_ucode_buffer - Allocates a microcode download buffer
2106  * @buf_len:            buffer length
2107  *
2108  * Allocates a DMA'able buffer in chunks and assembles a scatter/gather
2109  * list to use for microcode download
2110  *
2111  * Return value:
2112  *      pointer to sglist / NULL on failure
2113  **/
2114 static struct ipr_sglist *ipr_alloc_ucode_buffer(int buf_len)
2115 {
2116         int sg_size, order, bsize_elem, num_elem, i, j;
2117         struct ipr_sglist *sglist;
2118         struct scatterlist *scatterlist;
2119         struct page *page;
2120
2121         /* Get the minimum size per scatter/gather element */
2122         sg_size = buf_len / (IPR_MAX_SGLIST - 1);
2123
2124         /* Get the actual size per element */
2125         order = get_order(sg_size);
2126
2127         /* Determine the actual number of bytes per element */
2128         bsize_elem = PAGE_SIZE * (1 << order);
2129
2130         /* Determine the actual number of sg entries needed */
2131         if (buf_len % bsize_elem)
2132                 num_elem = (buf_len / bsize_elem) + 1;
2133         else
2134                 num_elem = buf_len / bsize_elem;
2135
2136         /* Allocate a scatter/gather list for the DMA */
2137         sglist = kmalloc(sizeof(struct ipr_sglist) +
2138                          (sizeof(struct scatterlist) * (num_elem - 1)),
2139                          GFP_KERNEL);
2140
2141         if (sglist == NULL) {
2142                 ipr_trace;
2143                 return NULL;
2144         }
2145
2146         memset(sglist, 0, sizeof(struct ipr_sglist) +
2147                (sizeof(struct scatterlist) * (num_elem - 1)));
2148
2149         scatterlist = sglist->scatterlist;
2150
2151         sglist->order = order;
2152         sglist->num_sg = num_elem;
2153
2154         /* Allocate a bunch of sg elements */
2155         for (i = 0; i < num_elem; i++) {
2156                 page = alloc_pages(GFP_KERNEL, order);
2157                 if (!page) {
2158                         ipr_trace;
2159
2160                         /* Free up what we already allocated */
2161                         for (j = i - 1; j >= 0; j--)
2162                                 __free_pages(scatterlist[j].page, order);
2163                         kfree(sglist);
2164                         return NULL;
2165                 }
2166
2167                 scatterlist[i].page = page;
2168         }
2169
2170         return sglist;
2171 }
2172
2173 /**
2174  * ipr_free_ucode_buffer - Frees a microcode download buffer
2175  * @p_dnld:             scatter/gather list pointer
2176  *
2177  * Free a DMA'able ucode download buffer previously allocated with
2178  * ipr_alloc_ucode_buffer
2179  *
2180  * Return value:
2181  *      nothing
2182  **/
2183 static void ipr_free_ucode_buffer(struct ipr_sglist *sglist)
2184 {
2185         int i;
2186
2187         for (i = 0; i < sglist->num_sg; i++)
2188                 __free_pages(sglist->scatterlist[i].page, sglist->order);
2189
2190         kfree(sglist);
2191 }
2192
2193 /**
2194  * ipr_copy_ucode_buffer - Copy user buffer to kernel buffer
2195  * @sglist:             scatter/gather list pointer
2196  * @buffer:             buffer pointer
2197  * @len:                buffer length
2198  *
2199  * Copy a microcode image from a user buffer into a buffer allocated by
2200  * ipr_alloc_ucode_buffer
2201  *
2202  * Return value:
2203  *      0 on success / other on failure
2204  **/
2205 static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
2206                                  u8 *buffer, u32 len)
2207 {
2208         int bsize_elem, i, result = 0;
2209         struct scatterlist *scatterlist;
2210         void *kaddr;
2211
2212         /* Determine the actual number of bytes per element */
2213         bsize_elem = PAGE_SIZE * (1 << sglist->order);
2214
2215         scatterlist = sglist->scatterlist;
2216
2217         for (i = 0; i < (len / bsize_elem); i++, buffer += bsize_elem) {
2218                 kaddr = kmap(scatterlist[i].page);
2219                 memcpy(kaddr, buffer, bsize_elem);
2220                 kunmap(scatterlist[i].page);
2221
2222                 scatterlist[i].length = bsize_elem;
2223
2224                 if (result != 0) {
2225                         ipr_trace;
2226                         return result;
2227                 }
2228         }
2229
2230         if (len % bsize_elem) {
2231                 kaddr = kmap(scatterlist[i].page);
2232                 memcpy(kaddr, buffer, len % bsize_elem);
2233                 kunmap(scatterlist[i].page);
2234
2235                 scatterlist[i].length = len % bsize_elem;
2236         }
2237
2238         sglist->buffer_len = len;
2239         return result;
2240 }
2241
2242 /**
2243  * ipr_map_ucode_buffer - Map a microcode download buffer
2244  * @ipr_cmd:    ipr command struct
2245  * @sglist:             scatter/gather list
2246  * @len:                total length of download buffer
2247  *
2248  * Maps a microcode download scatter/gather list for DMA and
2249  * builds the IOADL.
2250  *
2251  * Return value:
2252  *      0 on success / -EIO on failure
2253  **/
2254 static int ipr_map_ucode_buffer(struct ipr_cmnd *ipr_cmd,
2255                                 struct ipr_sglist *sglist, int len)
2256 {
2257         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
2258         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
2259         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
2260         struct scatterlist *scatterlist = sglist->scatterlist;
2261         int i;
2262
2263         ipr_cmd->dma_use_sg = pci_map_sg(ioa_cfg->pdev, scatterlist,
2264                                          sglist->num_sg, DMA_TO_DEVICE);
2265
2266         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
2267         ioarcb->write_data_transfer_length = cpu_to_be32(len);
2268         ioarcb->write_ioadl_len =
2269                 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
2270
2271         for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
2272                 ioadl[i].flags_and_data_len =
2273                         cpu_to_be32(IPR_IOADL_FLAGS_WRITE | sg_dma_len(&scatterlist[i]));
2274                 ioadl[i].address =
2275                         cpu_to_be32(sg_dma_address(&scatterlist[i]));
2276         }
2277
2278         if (likely(ipr_cmd->dma_use_sg)) {
2279                 ioadl[i-1].flags_and_data_len |=
2280                         cpu_to_be32(IPR_IOADL_FLAGS_LAST);
2281         }
2282         else {
2283                 dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
2284                 return -EIO;
2285         }
2286
2287         return 0;
2288 }
2289
2290 /**
2291  * ipr_store_update_fw - Update the firmware on the adapter
2292  * @class_dev:  class_device struct
2293  * @buf:                buffer
2294  * @count:              buffer size
2295  *
2296  * This function will update the firmware on the adapter.
2297  *
2298  * Return value:
2299  *      count on success / other on failure
2300  **/
2301 static ssize_t ipr_store_update_fw(struct class_device *class_dev,
2302                                        const char *buf, size_t count)
2303 {
2304         struct Scsi_Host *shost = class_to_shost(class_dev);
2305         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2306         struct ipr_ucode_image_header *image_hdr;
2307         const struct firmware *fw_entry;
2308         struct ipr_sglist *sglist;
2309         unsigned long lock_flags;
2310         char fname[100];
2311         char *src;
2312         int len, result, dnld_size;
2313
2314         if (!capable(CAP_SYS_ADMIN))
2315                 return -EACCES;
2316
2317         len = snprintf(fname, 99, "%s", buf);
2318         fname[len-1] = '\0';
2319
2320         if(request_firmware(&fw_entry, fname, &ioa_cfg->pdev->dev)) {
2321                 dev_err(&ioa_cfg->pdev->dev, "Firmware file %s not found\n", fname);
2322                 return -EIO;
2323         }
2324
2325         image_hdr = (struct ipr_ucode_image_header *)fw_entry->data;
2326
2327         if (be32_to_cpu(image_hdr->header_length) > fw_entry->size ||
2328             (ioa_cfg->vpd_cbs->page3_data.card_type &&
2329              ioa_cfg->vpd_cbs->page3_data.card_type != image_hdr->card_type)) {
2330                 dev_err(&ioa_cfg->pdev->dev, "Invalid microcode buffer\n");
2331                 release_firmware(fw_entry);
2332                 return -EINVAL;
2333         }
2334
2335         src = (u8 *)image_hdr + be32_to_cpu(image_hdr->header_length);
2336         dnld_size = fw_entry->size - be32_to_cpu(image_hdr->header_length);
2337         sglist = ipr_alloc_ucode_buffer(dnld_size);
2338
2339         if (!sglist) {
2340                 dev_err(&ioa_cfg->pdev->dev, "Microcode buffer allocation failed\n");
2341                 release_firmware(fw_entry);
2342                 return -ENOMEM;
2343         }
2344
2345         result = ipr_copy_ucode_buffer(sglist, src, dnld_size);
2346
2347         if (result) {
2348                 dev_err(&ioa_cfg->pdev->dev,
2349                         "Microcode buffer copy to DMA buffer failed\n");
2350                 ipr_free_ucode_buffer(sglist);
2351                 release_firmware(fw_entry);
2352                 return result;
2353         }
2354
2355         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2356
2357         if (ioa_cfg->ucode_sglist) {
2358                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2359                 dev_err(&ioa_cfg->pdev->dev,
2360                         "Microcode download already in progress\n");
2361                 ipr_free_ucode_buffer(sglist);
2362                 release_firmware(fw_entry);
2363                 return -EIO;
2364         }
2365
2366         ioa_cfg->ucode_sglist = sglist;
2367         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NORMAL);
2368         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2369         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
2370
2371         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2372         ioa_cfg->ucode_sglist = NULL;
2373         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2374
2375         ipr_free_ucode_buffer(sglist);
2376         release_firmware(fw_entry);
2377
2378         return count;
2379 }
2380
2381 static struct class_device_attribute ipr_update_fw_attr = {
2382         .attr = {
2383                 .name =         "update_fw",
2384                 .mode =         S_IWUSR,
2385         },
2386         .store = ipr_store_update_fw
2387 };
2388
2389 static struct class_device_attribute *ipr_ioa_attrs[] = {
2390         &ipr_fw_version_attr,
2391         &ipr_log_level_attr,
2392         &ipr_diagnostics_attr,
2393         &ipr_ioa_reset_attr,
2394         &ipr_update_fw_attr,
2395         NULL,
2396 };
2397
2398 #ifdef CONFIG_SCSI_IPR_DUMP
2399 /**
2400  * ipr_read_dump - Dump the adapter
2401  * @kobj:               kobject struct
2402  * @buf:                buffer
2403  * @off:                offset
2404  * @count:              buffer size
2405  *
2406  * Return value:
2407  *      number of bytes printed to buffer
2408  **/
2409 static ssize_t ipr_read_dump(struct kobject *kobj, char *buf,
2410                               loff_t off, size_t count)
2411 {
2412         struct class_device *cdev = container_of(kobj,struct class_device,kobj);
2413         struct Scsi_Host *shost = class_to_shost(cdev);
2414         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2415         struct ipr_dump *dump;
2416         unsigned long lock_flags = 0;
2417         char *src;
2418         int len;
2419         size_t rc = count;
2420
2421         if (!capable(CAP_SYS_ADMIN))
2422                 return -EACCES;
2423
2424         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2425         dump = ioa_cfg->dump;
2426
2427         if (ioa_cfg->sdt_state != DUMP_OBTAINED || !dump) {
2428                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2429                 return 0;
2430         }
2431         kref_get(&dump->kref);
2432         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2433
2434         if (off > dump->driver_dump.hdr.len) {
2435                 kref_put(&dump->kref, ipr_release_dump);
2436                 return 0;
2437         }
2438
2439         if (off + count > dump->driver_dump.hdr.len) {
2440                 count = dump->driver_dump.hdr.len - off;
2441                 rc = count;
2442         }
2443
2444         if (count && off < sizeof(dump->driver_dump)) {
2445                 if (off + count > sizeof(dump->driver_dump))
2446                         len = sizeof(dump->driver_dump) - off;
2447                 else
2448                         len = count;
2449                 src = (u8 *)&dump->driver_dump + off;
2450                 memcpy(buf, src, len);
2451                 buf += len;
2452                 off += len;
2453                 count -= len;
2454         }
2455
2456         off -= sizeof(dump->driver_dump);
2457
2458         if (count && off < offsetof(struct ipr_ioa_dump, ioa_data)) {
2459                 if (off + count > offsetof(struct ipr_ioa_dump, ioa_data))
2460                         len = offsetof(struct ipr_ioa_dump, ioa_data) - off;
2461                 else
2462                         len = count;
2463                 src = (u8 *)&dump->ioa_dump + off;
2464                 memcpy(buf, src, len);
2465                 buf += len;
2466                 off += len;
2467                 count -= len;
2468         }
2469
2470         off -= offsetof(struct ipr_ioa_dump, ioa_data);
2471
2472         while (count) {
2473                 if ((off & PAGE_MASK) != ((off + count) & PAGE_MASK))
2474                         len = PAGE_ALIGN(off) - off;
2475                 else
2476                         len = count;
2477                 src = (u8 *)dump->ioa_dump.ioa_data[(off & PAGE_MASK) >> PAGE_SHIFT];
2478                 src += off & ~PAGE_MASK;
2479                 memcpy(buf, src, len);
2480                 buf += len;
2481                 off += len;
2482                 count -= len;
2483         }
2484
2485         kref_put(&dump->kref, ipr_release_dump);
2486         return rc;
2487 }
2488
2489 /**
2490  * ipr_alloc_dump - Prepare for adapter dump
2491  * @ioa_cfg:    ioa config struct
2492  *
2493  * Return value:
2494  *      0 on success / other on failure
2495  **/
2496 static int ipr_alloc_dump(struct ipr_ioa_cfg *ioa_cfg)
2497 {
2498         struct ipr_dump *dump;
2499         unsigned long lock_flags = 0;
2500
2501         ENTER;
2502         dump = kmalloc(sizeof(struct ipr_dump), GFP_KERNEL);
2503
2504         if (!dump) {
2505                 ipr_err("Dump memory allocation failed\n");
2506                 return -ENOMEM;
2507         }
2508
2509         memset(dump, 0, sizeof(struct ipr_dump));
2510         kref_init(&dump->kref);
2511         dump->ioa_cfg = ioa_cfg;
2512
2513         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2514
2515         if (INACTIVE != ioa_cfg->sdt_state) {
2516                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2517                 kfree(dump);
2518                 return 0;
2519         }
2520
2521         ioa_cfg->dump = dump;
2522         ioa_cfg->sdt_state = WAIT_FOR_DUMP;
2523         if (ioa_cfg->ioa_is_dead && !ioa_cfg->dump_taken) {
2524                 ioa_cfg->dump_taken = 1;
2525                 schedule_work(&ioa_cfg->work_q);
2526         }
2527         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2528
2529         LEAVE;
2530         return 0;
2531 }
2532
2533 /**
2534  * ipr_free_dump - Free adapter dump memory
2535  * @ioa_cfg:    ioa config struct
2536  *
2537  * Return value:
2538  *      0 on success / other on failure
2539  **/
2540 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg)
2541 {
2542         struct ipr_dump *dump;
2543         unsigned long lock_flags = 0;
2544
2545         ENTER;
2546
2547         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2548         dump = ioa_cfg->dump;
2549         if (!dump) {
2550                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2551                 return 0;
2552         }
2553
2554         ioa_cfg->dump = NULL;
2555         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2556
2557         kref_put(&dump->kref, ipr_release_dump);
2558
2559         LEAVE;
2560         return 0;
2561 }
2562
2563 /**
2564  * ipr_write_dump - Setup dump state of adapter
2565  * @kobj:               kobject struct
2566  * @buf:                buffer
2567  * @off:                offset
2568  * @count:              buffer size
2569  *
2570  * Return value:
2571  *      number of bytes printed to buffer
2572  **/
2573 static ssize_t ipr_write_dump(struct kobject *kobj, char *buf,
2574                               loff_t off, size_t count)
2575 {
2576         struct class_device *cdev = container_of(kobj,struct class_device,kobj);
2577         struct Scsi_Host *shost = class_to_shost(cdev);
2578         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)shost->hostdata;
2579         int rc;
2580
2581         if (!capable(CAP_SYS_ADMIN))
2582                 return -EACCES;
2583
2584         if (buf[0] == '1')
2585                 rc = ipr_alloc_dump(ioa_cfg);
2586         else if (buf[0] == '0')
2587                 rc = ipr_free_dump(ioa_cfg);
2588         else
2589                 return -EINVAL;
2590
2591         if (rc)
2592                 return rc;
2593         else
2594                 return count;
2595 }
2596
2597 static struct bin_attribute ipr_dump_attr = {
2598         .attr = {
2599                 .name = "dump",
2600                 .mode = S_IRUSR | S_IWUSR,
2601         },
2602         .size = 0,
2603         .read = ipr_read_dump,
2604         .write = ipr_write_dump
2605 };
2606 #else
2607 static int ipr_free_dump(struct ipr_ioa_cfg *ioa_cfg) { return 0; };
2608 #endif
2609
2610 /**
2611  * ipr_store_queue_depth - Change the device's queue depth
2612  * @dev:        device struct
2613  * @buf:        buffer
2614  *
2615  * Return value:
2616  *      number of bytes printed to buffer
2617  **/
2618 static ssize_t ipr_store_queue_depth(struct device *dev,
2619                                     const char *buf, size_t count)
2620 {
2621         struct scsi_device *sdev = to_scsi_device(dev);
2622         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
2623         struct ipr_resource_entry *res;
2624         int qdepth = simple_strtoul(buf, NULL, 10);
2625         int tagged = 0;
2626         unsigned long lock_flags = 0;
2627         ssize_t len = -ENXIO;
2628
2629         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2630         res = (struct ipr_resource_entry *)sdev->hostdata;
2631         if (res) {
2632                 res->qdepth = qdepth;
2633
2634                 if (ipr_is_gscsi(res) && res->tcq_active)
2635                         tagged = MSG_ORDERED_TAG;
2636
2637                 len = strlen(buf);
2638         }
2639
2640         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2641         scsi_adjust_queue_depth(sdev, tagged, qdepth);
2642         return len;
2643 }
2644
2645 static struct device_attribute ipr_queue_depth_attr = {
2646         .attr = {
2647                 .name =         "queue_depth",
2648                 .mode =         S_IRUSR | S_IWUSR,
2649         },
2650         .store = ipr_store_queue_depth
2651 };
2652
2653 /**
2654  * ipr_show_tcq_enable - Show if the device is enabled for tcqing
2655  * @dev:        device struct
2656  * @buf:        buffer
2657  *
2658  * Return value:
2659  *      number of bytes printed to buffer
2660  **/
2661 static ssize_t ipr_show_tcq_enable(struct device *dev, char *buf)
2662 {
2663         struct scsi_device *sdev = to_scsi_device(dev);
2664         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
2665         struct ipr_resource_entry *res;
2666         unsigned long lock_flags = 0;
2667         ssize_t len = -ENXIO;
2668
2669         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2670         res = (struct ipr_resource_entry *)sdev->hostdata;
2671         if (res)
2672                 len = snprintf(buf, PAGE_SIZE, "%d\n", res->tcq_active);
2673         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2674         return len;
2675 }
2676
2677 /**
2678  * ipr_store_tcq_enable - Change the device's TCQing state
2679  * @dev:        device struct
2680  * @buf:        buffer
2681  *
2682  * Return value:
2683  *      number of bytes printed to buffer
2684  **/
2685 static ssize_t ipr_store_tcq_enable(struct device *dev,
2686                                     const char *buf, size_t count)
2687 {
2688         struct scsi_device *sdev = to_scsi_device(dev);
2689         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
2690         struct ipr_resource_entry *res;
2691         unsigned long lock_flags = 0;
2692         int tcq_active = simple_strtoul(buf, NULL, 10);
2693         ssize_t len = -ENXIO;
2694
2695         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2696
2697         res = (struct ipr_resource_entry *)sdev->hostdata;
2698
2699         if (res) {
2700                 if (ipr_is_gscsi(res) && sdev->tagged_supported) {
2701                         if (tcq_active) {
2702                                 res->tcq_active = 1;
2703                                 scsi_activate_tcq(sdev, res->qdepth);
2704                         } else {
2705                                 res->tcq_active = 0;
2706                                 scsi_deactivate_tcq(sdev, res->qdepth);
2707                         }
2708
2709                         len = strlen(buf);
2710                 } else if (tcq_active) {
2711                         len = -EINVAL;
2712                 }
2713         }
2714
2715         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2716         return len;
2717 }
2718
2719 static struct device_attribute ipr_tcqing_attr = {
2720         .attr = {
2721                 .name =         "tcq_enable",
2722                 .mode =         S_IRUSR | S_IWUSR,
2723         },
2724         .store = ipr_store_tcq_enable,
2725         .show = ipr_show_tcq_enable
2726 };
2727
2728 /**
2729  * ipr_show_adapter_handle - Show the adapter's resource handle for this device
2730  * @dev:        device struct
2731  * @buf:        buffer
2732  *
2733  * Return value:
2734  *      number of bytes printed to buffer
2735  **/
2736 static ssize_t ipr_show_adapter_handle(struct device *dev, char *buf)
2737 {
2738         struct scsi_device *sdev = to_scsi_device(dev);
2739         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)sdev->host->hostdata;
2740         struct ipr_resource_entry *res;
2741         unsigned long lock_flags = 0;
2742         ssize_t len = -ENXIO;
2743
2744         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2745         res = (struct ipr_resource_entry *)sdev->hostdata;
2746         if (res)
2747                 len = snprintf(buf, PAGE_SIZE, "%08X\n", res->cfgte.res_handle);
2748         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2749         return len;
2750 }
2751
2752 static struct device_attribute ipr_adapter_handle_attr = {
2753         .attr = {
2754                 .name =         "adapter_handle",
2755                 .mode =         S_IRUSR,
2756         },
2757         .show = ipr_show_adapter_handle
2758 };
2759
2760 static struct device_attribute *ipr_dev_attrs[] = {
2761         &ipr_queue_depth_attr,
2762         &ipr_tcqing_attr,
2763         &ipr_adapter_handle_attr,
2764         NULL,
2765 };
2766
2767 /**
2768  * ipr_biosparam - Return the HSC mapping
2769  * @sdev:                       scsi device struct
2770  * @block_device:       block device pointer
2771  * @capacity:           capacity of the device
2772  * @parm:                       Array containing returned HSC values.
2773  *
2774  * This function generates the HSC parms that fdisk uses.
2775  * We want to make sure we return something that places partitions
2776  * on 4k boundaries for best performance with the IOA.
2777  *
2778  * Return value:
2779  *      0 on success
2780  **/
2781 static int ipr_biosparam(struct scsi_device *sdev,
2782                          struct block_device *block_device,
2783                          sector_t capacity, int *parm)
2784 {
2785         int heads, sectors;
2786         sector_t cylinders;
2787
2788         heads = 128;
2789         sectors = 32;
2790
2791         cylinders = capacity;
2792         sector_div(cylinders, (128 * 32));
2793
2794         /* return result */
2795         parm[0] = heads;
2796         parm[1] = sectors;
2797         parm[2] = cylinders;
2798
2799         return 0;
2800 }
2801
2802 /**
2803  * ipr_slave_destroy - Unconfigure a SCSI device
2804  * @sdev:       scsi device struct
2805  *
2806  * Return value:
2807  *      nothing
2808  **/
2809 static void ipr_slave_destroy(struct scsi_device *sdev)
2810 {
2811         struct ipr_resource_entry *res;
2812         struct ipr_ioa_cfg *ioa_cfg;
2813         unsigned long lock_flags = 0;
2814
2815         ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
2816
2817         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2818         res = (struct ipr_resource_entry *) sdev->hostdata;
2819         if (res) {
2820                 sdev->hostdata = NULL;
2821                 res->sdev = NULL;
2822         }
2823         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2824 }
2825
2826 /**
2827  * ipr_slave_configure - Configure a SCSI device
2828  * @sdev:       scsi device struct
2829  *
2830  * This function configures the specified scsi device.
2831  *
2832  * Return value:
2833  *      0 on success
2834  **/
2835 static int ipr_slave_configure(struct scsi_device *sdev)
2836 {
2837         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
2838         struct ipr_resource_entry *res;
2839         unsigned long lock_flags = 0;
2840
2841         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2842         res = sdev->hostdata;
2843         if (res) {
2844                 if (ipr_is_af_dasd_device(res))
2845                         sdev->type = TYPE_RAID;
2846                 if (ipr_is_af_dasd_device(res) || ipr_is_ioa_resource(res))
2847                         sdev->scsi_level = 4;
2848                 if (ipr_is_vset_device(res))
2849                         sdev->timeout = IPR_VSET_RW_TIMEOUT;
2850                 if (IPR_IS_DASD_DEVICE(res->cfgte.std_inq_data))
2851                         sdev->allow_restart = 1;
2852                 scsi_adjust_queue_depth(sdev, 0, res->qdepth);
2853         }
2854         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2855         return 0;
2856 }
2857
2858 /**
2859  * ipr_slave_alloc - Prepare for commands to a device.
2860  * @sdev:       scsi device struct
2861  *
2862  * This function saves a pointer to the resource entry
2863  * in the scsi device struct if the device exists. We
2864  * can then use this pointer in ipr_queuecommand when
2865  * handling new commands.
2866  *
2867  * Return value:
2868  *      0 on success
2869  **/
2870 static int ipr_slave_alloc(struct scsi_device *sdev)
2871 {
2872         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *) sdev->host->hostdata;
2873         struct ipr_resource_entry *res;
2874         unsigned long lock_flags;
2875
2876         sdev->hostdata = NULL;
2877
2878         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
2879
2880         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
2881                 if ((res->cfgte.res_addr.bus == sdev->channel) &&
2882                     (res->cfgte.res_addr.target == sdev->id) &&
2883                     (res->cfgte.res_addr.lun == sdev->lun)) {
2884                         res->sdev = sdev;
2885                         res->add_to_ml = 0;
2886                         res->in_erp = 0;
2887                         sdev->hostdata = res;
2888                         res->needs_sync_complete = 1;
2889                         break;
2890                 }
2891         }
2892
2893         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
2894
2895         return 0;
2896 }
2897
2898 /**
2899  * ipr_eh_host_reset - Reset the host adapter
2900  * @scsi_cmd:   scsi command struct
2901  *
2902  * Return value:
2903  *      SUCCESS / FAILED
2904  **/
2905 static int ipr_eh_host_reset(struct scsi_cmnd * scsi_cmd)
2906 {
2907         struct ipr_ioa_cfg *ioa_cfg;
2908         int rc;
2909
2910         ENTER;
2911         ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
2912
2913         dev_err(&ioa_cfg->pdev->dev,
2914                 "Adapter being reset as a result of error recovery.\n");
2915
2916         if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
2917                 ioa_cfg->sdt_state = GET_DUMP;
2918
2919         rc = ipr_reset_reload(ioa_cfg, IPR_SHUTDOWN_ABBREV);
2920
2921         LEAVE;
2922         return rc;
2923 }
2924
2925 /**
2926  * ipr_eh_dev_reset - Reset the device
2927  * @scsi_cmd:   scsi command struct
2928  *
2929  * This function issues a device reset to the affected device.
2930  * A LUN reset will be sent to the device first. If that does
2931  * not work, a target reset will be sent.
2932  *
2933  * Return value:
2934  *      SUCCESS / FAILED
2935  **/
2936 static int ipr_eh_dev_reset(struct scsi_cmnd * scsi_cmd)
2937 {
2938         struct ipr_cmnd *ipr_cmd;
2939         struct ipr_ioa_cfg *ioa_cfg;
2940         struct ipr_resource_entry *res;
2941         struct ipr_cmd_pkt *cmd_pkt;
2942         u32 ioasc;
2943
2944         ENTER;
2945         ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
2946         res = scsi_cmd->device->hostdata;
2947
2948         if (!res || (!ipr_is_gscsi(res) && !ipr_is_vset_device(res)))
2949                 return FAILED;
2950
2951         /*
2952          * If we are currently going through reset/reload, return failed. This will force the
2953          * mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
2954          * reset to complete
2955          */
2956         if (ioa_cfg->in_reset_reload)
2957                 return FAILED;
2958         if (ioa_cfg->ioa_is_dead)
2959                 return FAILED;
2960
2961         list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
2962                 if (ipr_cmd->ioarcb.res_handle == res->cfgte.res_handle) {
2963                         if (ipr_cmd->scsi_cmd)
2964                                 ipr_cmd->done = ipr_scsi_eh_done;
2965                 }
2966         }
2967
2968         res->resetting_device = 1;
2969
2970         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
2971
2972         ipr_cmd->ioarcb.res_handle = res->cfgte.res_handle;
2973         cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
2974         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
2975         cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
2976
2977         ipr_sdev_err(scsi_cmd->device, "Resetting device\n");
2978         ipr_send_blocking_cmd(ipr_cmd, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
2979
2980         ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
2981
2982         res->resetting_device = 0;
2983
2984         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
2985
2986         LEAVE;
2987         return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
2988 }
2989
2990 /**
2991  * ipr_bus_reset_done - Op done function for bus reset.
2992  * @ipr_cmd:    ipr command struct
2993  *
2994  * This function is the op done function for a bus reset
2995  *
2996  * Return value:
2997  *      none
2998  **/
2999 static void ipr_bus_reset_done(struct ipr_cmnd *ipr_cmd)
3000 {
3001         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3002         struct ipr_resource_entry *res;
3003
3004         ENTER;
3005         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
3006                 if (!memcmp(&res->cfgte.res_handle, &ipr_cmd->ioarcb.res_handle,
3007                             sizeof(res->cfgte.res_handle))) {
3008                         scsi_report_bus_reset(ioa_cfg->host, res->cfgte.res_addr.bus);
3009                         break;
3010                 }
3011         }
3012
3013         /*
3014          * If abort has not completed, indicate the reset has, else call the
3015          * abort's done function to wake the sleeping eh thread
3016          */
3017         if (ipr_cmd->sibling->sibling)
3018                 ipr_cmd->sibling->sibling = NULL;
3019         else
3020                 ipr_cmd->sibling->done(ipr_cmd->sibling);
3021
3022         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3023         LEAVE;
3024 }
3025
3026 /**
3027  * ipr_abort_timeout - An abort task has timed out
3028  * @ipr_cmd:    ipr command struct
3029  *
3030  * This function handles when an abort task times out. If this
3031  * happens we issue a bus reset since we have resources tied
3032  * up that must be freed before returning to the midlayer.
3033  *
3034  * Return value:
3035  *      none
3036  **/
3037 static void ipr_abort_timeout(struct ipr_cmnd *ipr_cmd)
3038 {
3039         struct ipr_cmnd *reset_cmd;
3040         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3041         struct ipr_cmd_pkt *cmd_pkt;
3042         unsigned long lock_flags = 0;
3043
3044         ENTER;
3045         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3046         if (ipr_cmd->completion.done || ioa_cfg->in_reset_reload) {
3047                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3048                 return;
3049         }
3050
3051         ipr_sdev_err(ipr_cmd->u.sdev, "Abort timed out. Resetting bus\n");
3052         reset_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3053         ipr_cmd->sibling = reset_cmd;
3054         reset_cmd->sibling = ipr_cmd;
3055         reset_cmd->ioarcb.res_handle = ipr_cmd->ioarcb.res_handle;
3056         cmd_pkt = &reset_cmd->ioarcb.cmd_pkt;
3057         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3058         cmd_pkt->cdb[0] = IPR_RESET_DEVICE;
3059         cmd_pkt->cdb[2] = IPR_RESET_TYPE_SELECT | IPR_BUS_RESET;
3060
3061         ipr_do_req(reset_cmd, ipr_bus_reset_done, ipr_timeout, IPR_DEVICE_RESET_TIMEOUT);
3062         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3063         LEAVE;
3064 }
3065
3066 /**
3067  * ipr_cancel_op - Cancel specified op
3068  * @scsi_cmd:   scsi command struct
3069  *
3070  * This function cancels specified op.
3071  *
3072  * Return value:
3073  *      SUCCESS / FAILED
3074  **/
3075 static int ipr_cancel_op(struct scsi_cmnd * scsi_cmd)
3076 {
3077         struct ipr_cmnd *ipr_cmd;
3078         struct ipr_ioa_cfg *ioa_cfg;
3079         struct ipr_resource_entry *res;
3080         struct ipr_cmd_pkt *cmd_pkt;
3081         u32 ioasc;
3082         int op_found = 0;
3083
3084         ENTER;
3085         ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
3086         res = scsi_cmd->device->hostdata;
3087
3088         if (!res || (!ipr_is_gscsi(res) && !ipr_is_vset_device(res)))
3089                 return FAILED;
3090
3091         list_for_each_entry(ipr_cmd, &ioa_cfg->pending_q, queue) {
3092                 if (ipr_cmd->scsi_cmd == scsi_cmd) {
3093                         ipr_cmd->done = ipr_scsi_eh_done;
3094                         op_found = 1;
3095                         break;
3096                 }
3097         }
3098
3099         if (!op_found)
3100                 return SUCCESS;
3101
3102         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3103         ipr_cmd->ioarcb.res_handle = res->cfgte.res_handle;
3104         cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
3105         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3106         cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
3107         ipr_cmd->u.sdev = scsi_cmd->device;
3108
3109         ipr_sdev_err(scsi_cmd->device, "Aborting command: %02X\n", scsi_cmd->cmnd[0]);
3110         ipr_send_blocking_cmd(ipr_cmd, ipr_abort_timeout, IPR_CANCEL_ALL_TIMEOUT);
3111         ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3112
3113         /*
3114          * If the abort task timed out and we sent a bus reset, we will get
3115          * one the following responses to the abort
3116          */
3117         if (ioasc == IPR_IOASC_BUS_WAS_RESET || ioasc == IPR_IOASC_SYNC_REQUIRED) {
3118                 ioasc = 0;
3119                 ipr_trace;
3120         }
3121
3122         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3123         res->needs_sync_complete = 1;
3124
3125         LEAVE;
3126         return (IPR_IOASC_SENSE_KEY(ioasc) ? FAILED : SUCCESS);
3127 }
3128
3129 /**
3130  * ipr_eh_abort - Abort a single op
3131  * @scsi_cmd:   scsi command struct
3132  *
3133  * Return value:
3134  *      SUCCESS / FAILED
3135  **/
3136 static int ipr_eh_abort(struct scsi_cmnd * scsi_cmd)
3137 {
3138         struct ipr_ioa_cfg *ioa_cfg;
3139
3140         ENTER;
3141         ioa_cfg = (struct ipr_ioa_cfg *) scsi_cmd->device->host->hostdata;
3142
3143         /* If we are currently going through reset/reload, return failed. This will force the
3144            mid-layer to call ipr_eh_host_reset, which will then go to sleep and wait for the
3145            reset to complete */
3146         if (ioa_cfg->in_reset_reload)
3147                 return FAILED;
3148         if (ioa_cfg->ioa_is_dead)
3149                 return FAILED;
3150         if (!scsi_cmd->device->hostdata)
3151                 return FAILED;
3152
3153         LEAVE;
3154         return ipr_cancel_op(scsi_cmd);
3155 }
3156
3157 /**
3158  * ipr_handle_other_interrupt - Handle "other" interrupts
3159  * @ioa_cfg:    ioa config struct
3160  * @int_reg:    interrupt register
3161  *
3162  * Return value:
3163  *      IRQ_NONE / IRQ_HANDLED
3164  **/
3165 static irqreturn_t ipr_handle_other_interrupt(struct ipr_ioa_cfg *ioa_cfg,
3166                                               volatile u32 int_reg)
3167 {
3168         irqreturn_t rc = IRQ_HANDLED;
3169
3170         if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
3171                 /* Mask the interrupt */
3172                 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.set_interrupt_mask_reg);
3173
3174                 /* Clear the interrupt */
3175                 writel(IPR_PCII_IOA_TRANS_TO_OPER, ioa_cfg->regs.clr_interrupt_reg);
3176                 int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
3177
3178                 list_del(&ioa_cfg->reset_cmd->queue);
3179                 del_timer(&ioa_cfg->reset_cmd->timer);
3180                 ipr_reset_ioa_job(ioa_cfg->reset_cmd);
3181         } else {
3182                 if (int_reg & IPR_PCII_IOA_UNIT_CHECKED)
3183                         ioa_cfg->ioa_unit_checked = 1;
3184                 else
3185                         dev_err(&ioa_cfg->pdev->dev,
3186                                 "Permanent IOA failure. 0x%08X\n", int_reg);
3187
3188                 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
3189                         ioa_cfg->sdt_state = GET_DUMP;
3190
3191                 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
3192                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3193         }
3194
3195         return rc;
3196 }
3197
3198 /**
3199  * ipr_isr - Interrupt service routine
3200  * @irq:        irq number
3201  * @devp:       pointer to ioa config struct
3202  * @regs:       pt_regs struct
3203  *
3204  * Return value:
3205  *      IRQ_NONE / IRQ_HANDLED
3206  **/
3207 static irqreturn_t ipr_isr(int irq, void *devp, struct pt_regs *regs)
3208 {
3209         struct ipr_ioa_cfg *ioa_cfg = (struct ipr_ioa_cfg *)devp;
3210         unsigned long lock_flags = 0;
3211         volatile u32 int_reg, int_mask_reg;
3212         u32 ioasc;
3213         u16 cmd_index;
3214         struct ipr_cmnd *ipr_cmd;
3215         irqreturn_t rc = IRQ_NONE;
3216
3217         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
3218
3219         /* If interrupts are disabled, ignore the interrupt */
3220         if (!ioa_cfg->allow_interrupts) {
3221                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3222                 return IRQ_NONE;
3223         }
3224
3225         int_mask_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
3226         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
3227
3228         /* If an interrupt on the adapter did not occur, ignore it */
3229         if (unlikely((int_reg & IPR_PCII_OPER_INTERRUPTS) == 0)) {
3230                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3231                 return IRQ_NONE;
3232         }
3233
3234         while (1) {
3235                 ipr_cmd = NULL;
3236
3237                 while ((be32_to_cpu(*ioa_cfg->hrrq_curr) & IPR_HRRQ_TOGGLE_BIT) ==
3238                        ioa_cfg->toggle_bit) {
3239
3240                         cmd_index = (be32_to_cpu(*ioa_cfg->hrrq_curr) &
3241                                      IPR_HRRQ_REQ_RESP_HANDLE_MASK) >> IPR_HRRQ_REQ_RESP_HANDLE_SHIFT;
3242
3243                         if (unlikely(cmd_index >= IPR_NUM_CMD_BLKS)) {
3244                                 ioa_cfg->errors_logged++;
3245                                 dev_err(&ioa_cfg->pdev->dev, "Invalid response handle from IOA\n");
3246
3247                                 if (WAIT_FOR_DUMP == ioa_cfg->sdt_state)
3248                                         ioa_cfg->sdt_state = GET_DUMP;
3249
3250                                 ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
3251                                 spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3252                                 return IRQ_HANDLED;
3253                         }
3254
3255                         ipr_cmd = ioa_cfg->ipr_cmnd_list[cmd_index];
3256
3257                         ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3258
3259                         ipr_trc_hook(ipr_cmd, IPR_TRACE_FINISH, ioasc);
3260
3261                         list_del(&ipr_cmd->queue);
3262                         del_timer(&ipr_cmd->timer);
3263                         ipr_cmd->done(ipr_cmd);
3264
3265                         rc = IRQ_HANDLED;
3266
3267                         if (ioa_cfg->hrrq_curr < ioa_cfg->hrrq_end) {
3268                                 ioa_cfg->hrrq_curr++;
3269                         } else {
3270                                 ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
3271                                 ioa_cfg->toggle_bit ^= 1u;
3272                         }
3273                 }
3274
3275                 if (ipr_cmd != NULL) {
3276                         /* Clear the PCI interrupt */
3277                         writel(IPR_PCII_HRRQ_UPDATED, ioa_cfg->regs.clr_interrupt_reg);
3278                         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg) & ~int_mask_reg;
3279                 } else
3280                         break;
3281         }
3282
3283         if (unlikely(rc == IRQ_NONE))
3284                 rc = ipr_handle_other_interrupt(ioa_cfg, int_reg);
3285
3286         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
3287         return rc;
3288 }
3289
3290 /**
3291  * ipr_build_ioadl - Build a scatter/gather list and map the buffer
3292  * @ioa_cfg:    ioa config struct
3293  * @ipr_cmd:    ipr command struct
3294  *
3295  * Return value:
3296  *      0 on success / -1 on failure
3297  **/
3298 static int ipr_build_ioadl(struct ipr_ioa_cfg *ioa_cfg,
3299                            struct ipr_cmnd *ipr_cmd)
3300 {
3301         int i;
3302         struct scatterlist *sglist;
3303         u32 length;
3304         u32 ioadl_flags = 0;
3305         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3306         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
3307         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
3308
3309         length = scsi_cmd->request_bufflen;
3310
3311         if (length == 0)
3312                 return 0;
3313
3314         if (scsi_cmd->use_sg) {
3315                 ipr_cmd->dma_use_sg = pci_map_sg(ioa_cfg->pdev,
3316                                                  scsi_cmd->request_buffer,
3317                                                  scsi_cmd->use_sg,
3318                                                  scsi_cmd->sc_data_direction);
3319
3320                 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
3321                         ioadl_flags = IPR_IOADL_FLAGS_WRITE;
3322                         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3323                         ioarcb->write_data_transfer_length = cpu_to_be32(length);
3324                         ioarcb->write_ioadl_len =
3325                                 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
3326                 } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
3327                         ioadl_flags = IPR_IOADL_FLAGS_READ;
3328                         ioarcb->read_data_transfer_length = cpu_to_be32(length);
3329                         ioarcb->read_ioadl_len =
3330                                 cpu_to_be32(sizeof(struct ipr_ioadl_desc) * ipr_cmd->dma_use_sg);
3331                 }
3332
3333                 sglist = scsi_cmd->request_buffer;
3334
3335                 for (i = 0; i < ipr_cmd->dma_use_sg; i++) {
3336                         ioadl[i].flags_and_data_len =
3337                                 cpu_to_be32(ioadl_flags | sg_dma_len(&sglist[i]));
3338                         ioadl[i].address =
3339                                 cpu_to_be32(sg_dma_address(&sglist[i]));
3340                 }
3341
3342                 if (likely(ipr_cmd->dma_use_sg)) {
3343                         ioadl[i-1].flags_and_data_len |=
3344                                 cpu_to_be32(IPR_IOADL_FLAGS_LAST);
3345                         return 0;
3346                 } else
3347                         dev_err(&ioa_cfg->pdev->dev, "pci_map_sg failed!\n");
3348         } else {
3349                 if (scsi_cmd->sc_data_direction == DMA_TO_DEVICE) {
3350                         ioadl_flags = IPR_IOADL_FLAGS_WRITE;
3351                         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
3352                         ioarcb->write_data_transfer_length = cpu_to_be32(length);
3353                         ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
3354                 } else if (scsi_cmd->sc_data_direction == DMA_FROM_DEVICE) {
3355                         ioadl_flags = IPR_IOADL_FLAGS_READ;
3356                         ioarcb->read_data_transfer_length = cpu_to_be32(length);
3357                         ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
3358                 }
3359
3360                 ipr_cmd->dma_handle = pci_map_single(ioa_cfg->pdev,
3361                                                      scsi_cmd->request_buffer, length,
3362                                                      scsi_cmd->sc_data_direction);
3363
3364                 if (likely(!pci_dma_mapping_error(ipr_cmd->dma_handle))) {
3365                         ipr_cmd->dma_use_sg = 1;
3366                         ioadl[0].flags_and_data_len =
3367                                 cpu_to_be32(ioadl_flags | length | IPR_IOADL_FLAGS_LAST);
3368                         ioadl[0].address = cpu_to_be32(ipr_cmd->dma_handle);
3369                         return 0;
3370                 } else
3371                         dev_err(&ioa_cfg->pdev->dev, "pci_map_single failed!\n");
3372         }
3373
3374         return -1;
3375 }
3376
3377 /**
3378  * ipr_get_task_attributes - Translate SPI Q-Tag to task attributes
3379  * @scsi_cmd:   scsi command struct
3380  *
3381  * Return value:
3382  *      task attributes
3383  **/
3384 static u8 ipr_get_task_attributes(struct scsi_cmnd *scsi_cmd)
3385 {
3386         u8 tag[2];
3387         u8 rc = IPR_FLAGS_LO_UNTAGGED_TASK;
3388
3389         if (scsi_populate_tag_msg(scsi_cmd, tag)) {
3390                 switch (tag[0]) {
3391                 case MSG_SIMPLE_TAG:
3392                         rc = IPR_FLAGS_LO_SIMPLE_TASK;
3393                         break;
3394                 case MSG_HEAD_TAG:
3395                         rc = IPR_FLAGS_LO_HEAD_OF_Q_TASK;
3396                         break;
3397                 case MSG_ORDERED_TAG:
3398                         rc = IPR_FLAGS_LO_ORDERED_TASK;
3399                         break;
3400                 };
3401         }
3402
3403         return rc;
3404 }
3405
3406 /**
3407  * ipr_erp_done - Process completion of ERP for a device
3408  * @ipr_cmd:            ipr command struct
3409  *
3410  * This function copies the sense buffer into the scsi_cmd
3411  * struct and pushes the scsi_done function.
3412  *
3413  * Return value:
3414  *      nothing
3415  **/
3416 static void ipr_erp_done(struct ipr_cmnd *ipr_cmd)
3417 {
3418         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3419         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
3420         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3421         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3422
3423         if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
3424                 scsi_cmd->result |= (DID_ERROR << 16);
3425                 ipr_sdev_err(scsi_cmd->device,
3426                              "Request Sense failed with IOASC: 0x%08X\n", ioasc);
3427         } else {
3428                 memcpy(scsi_cmd->sense_buffer, ipr_cmd->sense_buffer,
3429                        SCSI_SENSE_BUFFERSIZE);
3430         }
3431
3432         if (res) {
3433                 res->needs_sync_complete = 1;
3434                 res->in_erp = 0;
3435         }
3436         ipr_unmap_sglist(ioa_cfg, ipr_cmd);
3437         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3438         scsi_cmd->scsi_done(scsi_cmd);
3439 }
3440
3441 /**
3442  * ipr_reinit_ipr_cmnd_for_erp - Re-initialize a cmnd block to be used for ERP
3443  * @ipr_cmd:    ipr command struct
3444  *
3445  * Return value:
3446  *      none
3447  **/
3448 static void ipr_reinit_ipr_cmnd_for_erp(struct ipr_cmnd *ipr_cmd)
3449 {
3450         struct ipr_ioarcb *ioarcb;
3451         struct ipr_ioasa *ioasa;
3452
3453         ioarcb = &ipr_cmd->ioarcb;
3454         ioasa = &ipr_cmd->ioasa;
3455
3456         memset(&ioarcb->cmd_pkt, 0, sizeof(struct ipr_cmd_pkt));
3457         ioarcb->write_data_transfer_length = 0;
3458         ioarcb->read_data_transfer_length = 0;
3459         ioarcb->write_ioadl_len = 0;
3460         ioarcb->read_ioadl_len = 0;
3461         ioasa->ioasc = 0;
3462         ioasa->residual_data_len = 0;
3463 }
3464
3465 /**
3466  * ipr_erp_request_sense - Send request sense to a device
3467  * @ipr_cmd:    ipr command struct
3468  *
3469  * This function sends a request sense to a device as a result
3470  * of a check condition.
3471  *
3472  * Return value:
3473  *      nothing
3474  **/
3475 static void ipr_erp_request_sense(struct ipr_cmnd *ipr_cmd)
3476 {
3477         struct ipr_cmd_pkt *cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
3478         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3479
3480         if (IPR_IOASC_SENSE_KEY(ioasc) > 0) {
3481                 ipr_erp_done(ipr_cmd);
3482                 return;
3483         }
3484
3485         ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
3486
3487         cmd_pkt->request_type = IPR_RQTYPE_SCSICDB;
3488         cmd_pkt->cdb[0] = REQUEST_SENSE;
3489         cmd_pkt->cdb[4] = SCSI_SENSE_BUFFERSIZE;
3490         cmd_pkt->flags_hi |= IPR_FLAGS_HI_SYNC_OVERRIDE;
3491         cmd_pkt->flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
3492         cmd_pkt->timeout = cpu_to_be16(IPR_REQUEST_SENSE_TIMEOUT / HZ);
3493
3494         ipr_cmd->ioadl[0].flags_and_data_len =
3495                 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | SCSI_SENSE_BUFFERSIZE);
3496         ipr_cmd->ioadl[0].address =
3497                 cpu_to_be32(ipr_cmd->sense_buffer_dma);
3498
3499         ipr_cmd->ioarcb.read_ioadl_len =
3500                 cpu_to_be32(sizeof(struct ipr_ioadl_desc));
3501         ipr_cmd->ioarcb.read_data_transfer_length =
3502                 cpu_to_be32(SCSI_SENSE_BUFFERSIZE);
3503
3504         ipr_do_req(ipr_cmd, ipr_erp_done, ipr_timeout,
3505                    IPR_REQUEST_SENSE_TIMEOUT * 2);
3506 }
3507
3508 /**
3509  * ipr_erp_cancel_all - Send cancel all to a device
3510  * @ipr_cmd:    ipr command struct
3511  *
3512  * This function sends a cancel all to a device to clear the
3513  * queue. If we are running TCQ on the device, QERR is set to 1,
3514  * which means all outstanding ops have been dropped on the floor.
3515  * Cancel all will return them to us.
3516  *
3517  * Return value:
3518  *      nothing
3519  **/
3520 static void ipr_erp_cancel_all(struct ipr_cmnd *ipr_cmd)
3521 {
3522         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3523         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
3524         struct ipr_cmd_pkt *cmd_pkt;
3525
3526         res->in_erp = 1;
3527
3528         ipr_reinit_ipr_cmnd_for_erp(ipr_cmd);
3529
3530         if (!res->tcq_active) {
3531                 ipr_erp_request_sense(ipr_cmd);
3532                 return;
3533         }
3534
3535         cmd_pkt = &ipr_cmd->ioarcb.cmd_pkt;
3536         cmd_pkt->request_type = IPR_RQTYPE_IOACMD;
3537         cmd_pkt->cdb[0] = IPR_CANCEL_ALL_REQUESTS;
3538
3539         ipr_do_req(ipr_cmd, ipr_erp_request_sense, ipr_timeout,
3540                    IPR_CANCEL_ALL_TIMEOUT);
3541 }
3542
3543 /**
3544  * ipr_dump_ioasa - Dump contents of IOASA
3545  * @ioa_cfg:    ioa config struct
3546  * @ipr_cmd:    ipr command struct
3547  *
3548  * This function is invoked by the interrupt handler when ops
3549  * fail. It will log the IOASA if appropriate. Only called
3550  * for GPDD ops.
3551  *
3552  * Return value:
3553  *      none
3554  **/
3555 static void ipr_dump_ioasa(struct ipr_ioa_cfg *ioa_cfg,
3556                            struct ipr_cmnd *ipr_cmd)
3557 {
3558         int i;
3559         u16 data_len;
3560         u32 ioasc;
3561         struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
3562         u32 *ioasa_data = (u32 *)ioasa;
3563         int error_index;
3564
3565         ioasc = be32_to_cpu(ioasa->ioasc) & IPR_IOASC_IOASC_MASK;
3566
3567         if (0 == ioasc)
3568                 return;
3569
3570         if (ioa_cfg->log_level < IPR_DEFAULT_LOG_LEVEL)
3571                 return;
3572
3573         error_index = ipr_get_error(ioasc);
3574
3575         if (ioa_cfg->log_level < IPR_MAX_LOG_LEVEL) {
3576                 /* Don't log an error if the IOA already logged one */
3577                 if (ioasa->ilid != 0)
3578                         return;
3579
3580                 if (ipr_error_table[error_index].log_ioasa == 0)
3581                         return;
3582         }
3583
3584         ipr_sdev_err(ipr_cmd->scsi_cmd->device, "%s\n",
3585                      ipr_error_table[error_index].error);
3586
3587         if ((ioasa->u.gpdd.end_state <= ARRAY_SIZE(ipr_gpdd_dev_end_states)) &&
3588             (ioasa->u.gpdd.bus_phase <=  ARRAY_SIZE(ipr_gpdd_dev_bus_phases))) {
3589                 ipr_sdev_err(ipr_cmd->scsi_cmd->device,
3590                              "Device End state: %s Phase: %s\n",
3591                              ipr_gpdd_dev_end_states[ioasa->u.gpdd.end_state],
3592                              ipr_gpdd_dev_bus_phases[ioasa->u.gpdd.bus_phase]);
3593         }
3594
3595         if (sizeof(struct ipr_ioasa) < be16_to_cpu(ioasa->ret_stat_len))
3596                 data_len = sizeof(struct ipr_ioasa);
3597         else
3598                 data_len = be16_to_cpu(ioasa->ret_stat_len);
3599
3600         ipr_err("IOASA Dump:\n");
3601
3602         for (i = 0; i < data_len / 4; i += 4) {
3603                 ipr_err("%08X: %08X %08X %08X %08X\n", i*4,
3604                         be32_to_cpu(ioasa_data[i]),
3605                         be32_to_cpu(ioasa_data[i+1]),
3606                         be32_to_cpu(ioasa_data[i+2]),
3607                         be32_to_cpu(ioasa_data[i+3]));
3608         }
3609 }
3610
3611 /**
3612  * ipr_gen_sense - Generate SCSI sense data from an IOASA
3613  * @ioasa:              IOASA
3614  * @sense_buf:  sense data buffer
3615  *
3616  * Return value:
3617  *      none
3618  **/
3619 static void ipr_gen_sense(struct ipr_cmnd *ipr_cmd)
3620 {
3621         u32 failing_lba;
3622         u8 *sense_buf = ipr_cmd->scsi_cmd->sense_buffer;
3623         struct ipr_resource_entry *res = ipr_cmd->scsi_cmd->device->hostdata;
3624         struct ipr_ioasa *ioasa = &ipr_cmd->ioasa;
3625         u32 ioasc = be32_to_cpu(ioasa->ioasc);
3626
3627         memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
3628
3629         if (ioasc >= IPR_FIRST_DRIVER_IOASC)
3630                 return;
3631
3632         ipr_cmd->scsi_cmd->result = SAM_STAT_CHECK_CONDITION;
3633
3634         if (ipr_is_vset_device(res) &&
3635             ioasc == IPR_IOASC_MED_DO_NOT_REALLOC &&
3636             ioasa->u.vset.failing_lba_hi != 0) {
3637                 sense_buf[0] = 0x72;
3638                 sense_buf[1] = IPR_IOASC_SENSE_KEY(ioasc);
3639                 sense_buf[2] = IPR_IOASC_SENSE_CODE(ioasc);
3640                 sense_buf[3] = IPR_IOASC_SENSE_QUAL(ioasc);
3641
3642                 sense_buf[7] = 12;
3643                 sense_buf[8] = 0;
3644                 sense_buf[9] = 0x0A;
3645                 sense_buf[10] = 0x80;
3646
3647                 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_hi);
3648
3649                 sense_buf[12] = (failing_lba & 0xff000000) >> 24;
3650                 sense_buf[13] = (failing_lba & 0x00ff0000) >> 16;
3651                 sense_buf[14] = (failing_lba & 0x0000ff00) >> 8;
3652                 sense_buf[15] = failing_lba & 0x000000ff;
3653
3654                 failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
3655
3656                 sense_buf[16] = (failing_lba & 0xff000000) >> 24;
3657                 sense_buf[17] = (failing_lba & 0x00ff0000) >> 16;
3658                 sense_buf[18] = (failing_lba & 0x0000ff00) >> 8;
3659                 sense_buf[19] = failing_lba & 0x000000ff;
3660         } else {
3661                 sense_buf[0] = 0x70;
3662                 sense_buf[2] = IPR_IOASC_SENSE_KEY(ioasc);
3663                 sense_buf[12] = IPR_IOASC_SENSE_CODE(ioasc);
3664                 sense_buf[13] = IPR_IOASC_SENSE_QUAL(ioasc);
3665
3666                 /* Illegal request */
3667                 if ((IPR_IOASC_SENSE_KEY(ioasc) == 0x05) &&
3668                     (be32_to_cpu(ioasa->ioasc_specific) & IPR_FIELD_POINTER_VALID)) {
3669                         sense_buf[7] = 10;      /* additional length */
3670
3671                         /* IOARCB was in error */
3672                         if (IPR_IOASC_SENSE_CODE(ioasc) == 0x24)
3673                                 sense_buf[15] = 0xC0;
3674                         else    /* Parameter data was invalid */
3675                                 sense_buf[15] = 0x80;
3676
3677                         sense_buf[16] =
3678                             ((IPR_FIELD_POINTER_MASK &
3679                               be32_to_cpu(ioasa->ioasc_specific)) >> 8) & 0xff;
3680                         sense_buf[17] =
3681                             (IPR_FIELD_POINTER_MASK &
3682                              be32_to_cpu(ioasa->ioasc_specific)) & 0xff;
3683                 } else {
3684                         if (ioasc == IPR_IOASC_MED_DO_NOT_REALLOC) {
3685                                 if (ipr_is_vset_device(res))
3686                                         failing_lba = be32_to_cpu(ioasa->u.vset.failing_lba_lo);
3687                                 else
3688                                         failing_lba = be32_to_cpu(ioasa->u.dasd.failing_lba);
3689
3690                                 sense_buf[0] |= 0x80;   /* Or in the Valid bit */
3691                                 sense_buf[3] = (failing_lba & 0xff000000) >> 24;
3692                                 sense_buf[4] = (failing_lba & 0x00ff0000) >> 16;
3693                                 sense_buf[5] = (failing_lba & 0x0000ff00) >> 8;
3694                                 sense_buf[6] = failing_lba & 0x000000ff;
3695                         }
3696
3697                         sense_buf[7] = 6;       /* additional length */
3698                 }
3699         }
3700 }
3701
3702 /**
3703  * ipr_erp_start - Process an error response for a SCSI op
3704  * @ioa_cfg:    ioa config struct
3705  * @ipr_cmd:    ipr command struct
3706  *
3707  * This function determines whether or not to initiate ERP
3708  * on the affected device.
3709  *
3710  * Return value:
3711  *      nothing
3712  **/
3713 static void ipr_erp_start(struct ipr_ioa_cfg *ioa_cfg,
3714                               struct ipr_cmnd *ipr_cmd)
3715 {
3716         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3717         struct ipr_resource_entry *res = scsi_cmd->device->hostdata;
3718         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3719
3720         if (!res) {
3721                 ipr_scsi_eh_done(ipr_cmd);
3722                 return;
3723         }
3724
3725         if (ipr_is_gscsi(res))
3726                 ipr_dump_ioasa(ioa_cfg, ipr_cmd);
3727         else
3728                 ipr_gen_sense(ipr_cmd);
3729
3730         switch (ioasc & IPR_IOASC_IOASC_MASK) {
3731         case IPR_IOASC_ABORTED_CMD_TERM_BY_HOST:
3732                 scsi_cmd->result |= (DID_IMM_RETRY << 16);
3733                 break;
3734         case IPR_IOASC_IR_RESOURCE_HANDLE:
3735                 scsi_cmd->result |= (DID_NO_CONNECT << 16);
3736                 break;
3737         case IPR_IOASC_HW_SEL_TIMEOUT:
3738                 scsi_cmd->result |= (DID_NO_CONNECT << 16);
3739                 res->needs_sync_complete = 1;
3740                 break;
3741         case IPR_IOASC_SYNC_REQUIRED:
3742                 if (!res->in_erp)
3743                         res->needs_sync_complete = 1;
3744                 scsi_cmd->result |= (DID_IMM_RETRY << 16);
3745                 break;
3746         case IPR_IOASC_MED_DO_NOT_REALLOC: /* prevent retries */
3747                 scsi_cmd->result |= (DID_PASSTHROUGH << 16);
3748                 break;
3749         case IPR_IOASC_BUS_WAS_RESET:
3750         case IPR_IOASC_BUS_WAS_RESET_BY_OTHER:
3751                 /*
3752                  * Report the bus reset and ask for a retry. The device
3753                  * will give CC/UA the next command.
3754                  */
3755                 if (!res->resetting_device)
3756                         scsi_report_bus_reset(ioa_cfg->host, scsi_cmd->device->channel);
3757                 scsi_cmd->result |= (DID_ERROR << 16);
3758                 res->needs_sync_complete = 1;
3759                 break;
3760         case IPR_IOASC_HW_DEV_BUS_STATUS:
3761                 scsi_cmd->result |= IPR_IOASC_SENSE_STATUS(ioasc);
3762                 if (IPR_IOASC_SENSE_STATUS(ioasc) == SAM_STAT_CHECK_CONDITION) {
3763                         ipr_erp_cancel_all(ipr_cmd);
3764                         return;
3765                 }
3766                 res->needs_sync_complete = 1;
3767                 break;
3768         case IPR_IOASC_NR_INIT_CMD_REQUIRED:
3769                 break;
3770         default:
3771                 scsi_cmd->result |= (DID_ERROR << 16);
3772                 if (!ipr_is_vset_device(res))
3773                         res->needs_sync_complete = 1;
3774                 break;
3775         }
3776
3777         ipr_unmap_sglist(ioa_cfg, ipr_cmd);
3778         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3779         scsi_cmd->scsi_done(scsi_cmd);
3780 }
3781
3782 /**
3783  * ipr_scsi_done - mid-layer done function
3784  * @ipr_cmd:    ipr command struct
3785  *
3786  * This function is invoked by the interrupt handler for
3787  * ops generated by the SCSI mid-layer
3788  *
3789  * Return value:
3790  *      none
3791  **/
3792 static void ipr_scsi_done(struct ipr_cmnd *ipr_cmd)
3793 {
3794         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
3795         struct scsi_cmnd *scsi_cmd = ipr_cmd->scsi_cmd;
3796         u32 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
3797
3798         scsi_cmd->resid = be32_to_cpu(ipr_cmd->ioasa.residual_data_len);
3799
3800         if (likely(IPR_IOASC_SENSE_KEY(ioasc) == 0)) {
3801                 ipr_unmap_sglist(ioa_cfg, ipr_cmd);
3802                 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3803                 scsi_cmd->scsi_done(scsi_cmd);
3804         } else
3805                 ipr_erp_start(ioa_cfg, ipr_cmd);
3806 }
3807
3808 /**
3809  * ipr_save_ioafp_mode_select - Save adapters mode select data
3810  * @ioa_cfg:    ioa config struct
3811  * @scsi_cmd:   scsi command struct
3812  *
3813  * This function saves mode select data for the adapter to
3814  * use following an adapter reset.
3815  *
3816  * Return value:
3817  *      0 on success / SCSI_MLQUEUE_HOST_BUSY on failure
3818  **/
3819 static int ipr_save_ioafp_mode_select(struct ipr_ioa_cfg *ioa_cfg,
3820                                        struct scsi_cmnd *scsi_cmd)
3821 {
3822         if (!ioa_cfg->saved_mode_pages) {
3823                 ioa_cfg->saved_mode_pages  = kmalloc(sizeof(struct ipr_mode_pages),
3824                                                      GFP_ATOMIC);
3825                 if (!ioa_cfg->saved_mode_pages) {
3826                         dev_err(&ioa_cfg->pdev->dev,
3827                                 "IOA mode select buffer allocation failed\n");
3828                         return SCSI_MLQUEUE_HOST_BUSY;
3829                 }
3830         }
3831
3832         memcpy(ioa_cfg->saved_mode_pages, scsi_cmd->buffer, scsi_cmd->cmnd[4]);
3833         ioa_cfg->saved_mode_page_len = scsi_cmd->cmnd[4];
3834         return 0;
3835 }
3836
3837 /**
3838  * ipr_queuecommand - Queue a mid-layer request
3839  * @scsi_cmd:   scsi command struct
3840  * @done:               done function
3841  *
3842  * This function queues a request generated by the mid-layer.
3843  *
3844  * Return value:
3845  *      0 on success
3846  *      SCSI_MLQUEUE_DEVICE_BUSY if device is busy
3847  *      SCSI_MLQUEUE_HOST_BUSY if host is busy
3848  **/
3849 static int ipr_queuecommand(struct scsi_cmnd *scsi_cmd,
3850                             void (*done) (struct scsi_cmnd *))
3851 {
3852         struct ipr_ioa_cfg *ioa_cfg;
3853         struct ipr_resource_entry *res;
3854         struct ipr_ioarcb *ioarcb;
3855         struct ipr_cmnd *ipr_cmd;
3856         int rc = 0;
3857
3858         scsi_cmd->scsi_done = done;
3859         ioa_cfg = (struct ipr_ioa_cfg *)scsi_cmd->device->host->hostdata;
3860         res = scsi_cmd->device->hostdata;
3861         scsi_cmd->result = (DID_OK << 16);
3862
3863         /*
3864          * We are currently blocking all devices due to a host reset
3865          * We have told the host to stop giving us new requests, but
3866          * ERP ops don't count. FIXME
3867          */
3868         if (unlikely(!ioa_cfg->allow_cmds && !ioa_cfg->ioa_is_dead))
3869                 return SCSI_MLQUEUE_HOST_BUSY;
3870
3871         /*
3872          * FIXME - Create scsi_set_host_offline interface
3873          *  and the ioa_is_dead check can be removed
3874          */
3875         if (unlikely(ioa_cfg->ioa_is_dead || !res)) {
3876                 memset(scsi_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
3877                 scsi_cmd->result = (DID_NO_CONNECT << 16);
3878                 scsi_cmd->scsi_done(scsi_cmd);
3879                 return 0;
3880         }
3881
3882         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
3883         ioarcb = &ipr_cmd->ioarcb;
3884         list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
3885
3886         memcpy(ioarcb->cmd_pkt.cdb, scsi_cmd->cmnd, scsi_cmd->cmd_len);
3887         ipr_cmd->scsi_cmd = scsi_cmd;
3888         ioarcb->res_handle = res->cfgte.res_handle;
3889         ipr_cmd->done = ipr_scsi_done;
3890         ipr_trc_hook(ipr_cmd, IPR_TRACE_START, IPR_GET_PHYS_LOC(res->cfgte.res_addr));
3891
3892         if (ipr_is_gscsi(res) || ipr_is_vset_device(res)) {
3893                 if (scsi_cmd->underflow == 0)
3894                         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_ULEN_CHK;
3895
3896                 if (res->needs_sync_complete) {
3897                         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_SYNC_COMPLETE;
3898                         res->needs_sync_complete = 0;
3899                 }
3900
3901                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_NO_LINK_DESC;
3902                 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_DELAY_AFTER_RST;
3903                 ioarcb->cmd_pkt.flags_lo |= IPR_FLAGS_LO_ALIGNED_BFR;
3904                 ioarcb->cmd_pkt.flags_lo |= ipr_get_task_attributes(scsi_cmd);
3905         }
3906
3907         if (!ipr_is_gscsi(res) && scsi_cmd->cmnd[0] >= 0xC0)
3908                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
3909
3910         if (ipr_is_ioa_resource(res) && scsi_cmd->cmnd[0] == MODE_SELECT)
3911                 rc = ipr_save_ioafp_mode_select(ioa_cfg, scsi_cmd);
3912
3913         if (likely(rc == 0))
3914                 rc = ipr_build_ioadl(ioa_cfg, ipr_cmd);
3915
3916         if (likely(rc == 0)) {
3917                 mb();
3918                 writel(be32_to_cpu(ipr_cmd->ioarcb.ioarcb_host_pci_addr),
3919                        ioa_cfg->regs.ioarrin_reg);
3920         } else {
3921                  list_move_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
3922                  return SCSI_MLQUEUE_HOST_BUSY;
3923         }
3924
3925         return 0;
3926 }
3927
3928 /**
3929  * ipr_info - Get information about the card/driver
3930  * @scsi_host:  scsi host struct
3931  *
3932  * Return value:
3933  *      pointer to buffer with description string
3934  **/
3935 static const char * ipr_ioa_info(struct Scsi_Host *host)
3936 {
3937         static char buffer[512];
3938         struct ipr_ioa_cfg *ioa_cfg;
3939         unsigned long lock_flags = 0;
3940
3941         ioa_cfg = (struct ipr_ioa_cfg *) host->hostdata;
3942
3943         spin_lock_irqsave(host->host_lock, lock_flags);
3944         sprintf(buffer, "IBM %X Storage Adapter", ioa_cfg->type);
3945         spin_unlock_irqrestore(host->host_lock, lock_flags);
3946
3947         return buffer;
3948 }
3949
3950 static struct scsi_host_template driver_template = {
3951         .module = THIS_MODULE,
3952         .name = "IPR",
3953         .info = ipr_ioa_info,
3954         .queuecommand = ipr_queuecommand,
3955         .eh_abort_handler = ipr_eh_abort,
3956         .eh_device_reset_handler = ipr_eh_dev_reset,
3957         .eh_host_reset_handler = ipr_eh_host_reset,
3958         .slave_alloc = ipr_slave_alloc,
3959         .slave_configure = ipr_slave_configure,
3960         .slave_destroy = ipr_slave_destroy,
3961         .bios_param = ipr_biosparam,
3962         .can_queue = IPR_MAX_COMMANDS,
3963         .this_id = -1,
3964         .sg_tablesize = IPR_MAX_SGLIST,
3965         .max_sectors = IPR_MAX_SECTORS,
3966         .cmd_per_lun = IPR_MAX_CMD_PER_LUN,
3967         .use_clustering = ENABLE_CLUSTERING,
3968         .shost_attrs = ipr_ioa_attrs,
3969         .sdev_attrs = ipr_dev_attrs,
3970         .proc_name = IPR_NAME
3971 };
3972
3973 #ifdef CONFIG_PPC_PSERIES
3974 static const u16 ipr_blocked_processors[] = {
3975         PV_NORTHSTAR,
3976         PV_PULSAR,
3977         PV_POWER4,
3978         PV_ICESTAR,
3979         PV_SSTAR,
3980         PV_POWER4p,
3981         PV_630,
3982         PV_630p
3983 };
3984
3985 /**
3986  * ipr_invalid_adapter - Determine if this adapter is supported on this hardware
3987  * @ioa_cfg:    ioa cfg struct
3988  *
3989  * Adapters that use Gemstone revision < 3.1 do not work reliably on
3990  * certain pSeries hardware. This function determines if the given
3991  * adapter is in one of these confgurations or not.
3992  *
3993  * Return value:
3994  *      1 if adapter is not supported / 0 if adapter is supported
3995  **/
3996 static int ipr_invalid_adapter(struct ipr_ioa_cfg *ioa_cfg)
3997 {
3998         u8 rev_id;
3999         int i;
4000
4001         if (ioa_cfg->type == 0x5702) {
4002                 if (pci_read_config_byte(ioa_cfg->pdev, PCI_REVISION_ID,
4003                                          &rev_id) == PCIBIOS_SUCCESSFUL) {
4004                         if (rev_id < 4) {
4005                                 for (i = 0; i < ARRAY_SIZE(ipr_blocked_processors); i++){
4006                                         if (__is_processor(ipr_blocked_processors[i]))
4007                                                 return 1;
4008                                 }
4009                         }
4010                 }
4011         }
4012         return 0;
4013 }
4014 #else
4015 #define ipr_invalid_adapter(ioa_cfg) 0
4016 #endif
4017
4018 /**
4019  * ipr_ioa_bringdown_done - IOA bring down completion.
4020  * @ipr_cmd:    ipr command struct
4021  *
4022  * This function processes the completion of an adapter bring down.
4023  * It wakes any reset sleepers.
4024  *
4025  * Return value:
4026  *      IPR_RC_JOB_RETURN
4027  **/
4028 static int ipr_ioa_bringdown_done(struct ipr_cmnd *ipr_cmd)
4029 {
4030         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4031
4032         ENTER;
4033         ioa_cfg->in_reset_reload = 0;
4034         ioa_cfg->reset_retries = 0;
4035         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4036         wake_up_all(&ioa_cfg->reset_wait_q);
4037
4038         spin_unlock_irq(ioa_cfg->host->host_lock);
4039         scsi_unblock_requests(ioa_cfg->host);
4040         spin_lock_irq(ioa_cfg->host->host_lock);
4041         LEAVE;
4042
4043         return IPR_RC_JOB_RETURN;
4044 }
4045
4046 /**
4047  * ipr_ioa_reset_done - IOA reset completion.
4048  * @ipr_cmd:    ipr command struct
4049  *
4050  * This function processes the completion of an adapter reset.
4051  * It schedules any necessary mid-layer add/removes and
4052  * wakes any reset sleepers.
4053  *
4054  * Return value:
4055  *      IPR_RC_JOB_RETURN
4056  **/
4057 static int ipr_ioa_reset_done(struct ipr_cmnd *ipr_cmd)
4058 {
4059         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4060         struct ipr_resource_entry *res;
4061         struct ipr_hostrcb *hostrcb, *temp;
4062         int i = 0;
4063
4064         ENTER;
4065         ioa_cfg->in_reset_reload = 0;
4066         ioa_cfg->allow_cmds = 1;
4067         ioa_cfg->reset_cmd = NULL;
4068
4069         list_for_each_entry(res, &ioa_cfg->used_res_q, queue) {
4070                 if (ioa_cfg->allow_ml_add_del && (res->add_to_ml || res->del_from_ml)) {
4071                         ipr_trace;
4072                         schedule_work(&ioa_cfg->work_q);
4073                         break;
4074                 }
4075         }
4076
4077         list_for_each_entry_safe(hostrcb, temp, &ioa_cfg->hostrcb_free_q, queue) {
4078                 list_del(&hostrcb->queue);
4079                 if (i++ < IPR_NUM_LOG_HCAMS)
4080                         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_LOG_DATA, hostrcb);
4081                 else
4082                         ipr_send_hcam(ioa_cfg, IPR_HCAM_CDB_OP_CODE_CONFIG_CHANGE, hostrcb);
4083         }
4084
4085         dev_info(&ioa_cfg->pdev->dev, "IOA initialized.\n");
4086
4087         ioa_cfg->reset_retries = 0;
4088         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
4089         wake_up_all(&ioa_cfg->reset_wait_q);
4090
4091         spin_unlock_irq(ioa_cfg->host->host_lock);
4092         scsi_unblock_requests(ioa_cfg->host);
4093         spin_lock_irq(ioa_cfg->host->host_lock);
4094
4095         if (!ioa_cfg->allow_cmds)
4096                 scsi_block_requests(ioa_cfg->host);
4097
4098         LEAVE;
4099         return IPR_RC_JOB_RETURN;
4100 }
4101
4102 /**
4103  * ipr_set_sup_dev_dflt - Initialize a Set Supported Device buffer
4104  * @supported_dev:      supported device struct
4105  * @vpids:                      vendor product id struct
4106  *
4107  * Return value:
4108  *      none
4109  **/
4110 static void ipr_set_sup_dev_dflt(struct ipr_supported_device *supported_dev,
4111                                  struct ipr_std_inq_vpids *vpids)
4112 {
4113         memset(supported_dev, 0, sizeof(struct ipr_supported_device));
4114         memcpy(&supported_dev->vpids, vpids, sizeof(struct ipr_std_inq_vpids));
4115         supported_dev->num_records = 1;
4116         supported_dev->data_length =
4117                 cpu_to_be16(sizeof(struct ipr_supported_device));
4118         supported_dev->reserved = 0;
4119 }
4120
4121 /**
4122  * ipr_set_supported_devs - Send Set Supported Devices for a device
4123  * @ipr_cmd:    ipr command struct
4124  *
4125  * This function send a Set Supported Devices to the adapter
4126  *
4127  * Return value:
4128  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4129  **/
4130 static int ipr_set_supported_devs(struct ipr_cmnd *ipr_cmd)
4131 {
4132         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4133         struct ipr_supported_device *supp_dev = &ioa_cfg->vpd_cbs->supp_dev;
4134         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4135         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4136         struct ipr_resource_entry *res = ipr_cmd->u.res;
4137
4138         ipr_cmd->job_step = ipr_ioa_reset_done;
4139
4140         list_for_each_entry_continue(res, &ioa_cfg->used_res_q, queue) {
4141                 if (!ipr_is_af_dasd_device(res))
4142                         continue;
4143
4144                 ipr_cmd->u.res = res;
4145                 ipr_set_sup_dev_dflt(supp_dev, &res->cfgte.std_inq_data.vpids);
4146
4147                 ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4148                 ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
4149                 ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4150
4151                 ioarcb->cmd_pkt.cdb[0] = IPR_SET_SUPPORTED_DEVICES;
4152                 ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_supported_device) >> 8) & 0xff;
4153                 ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_supported_device) & 0xff;
4154
4155                 ioadl->flags_and_data_len = cpu_to_be32(IPR_IOADL_FLAGS_WRITE_LAST |
4156                                                         sizeof(struct ipr_supported_device));
4157                 ioadl->address = cpu_to_be32(ioa_cfg->vpd_cbs_dma +
4158                                              offsetof(struct ipr_misc_cbs, supp_dev));
4159                 ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4160                 ioarcb->write_data_transfer_length =
4161                         cpu_to_be32(sizeof(struct ipr_supported_device));
4162
4163                 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
4164                            IPR_SET_SUP_DEVICE_TIMEOUT);
4165
4166                 ipr_cmd->job_step = ipr_set_supported_devs;
4167                 return IPR_RC_JOB_RETURN;
4168         }
4169
4170         return IPR_RC_JOB_CONTINUE;
4171 }
4172
4173 /**
4174  * ipr_get_mode_page - Locate specified mode page
4175  * @mode_pages: mode page buffer
4176  * @page_code:  page code to find
4177  * @len:                minimum required length for mode page
4178  *
4179  * Return value:
4180  *      pointer to mode page / NULL on failure
4181  **/
4182 static void *ipr_get_mode_page(struct ipr_mode_pages *mode_pages,
4183                                u32 page_code, u32 len)
4184 {
4185         struct ipr_mode_page_hdr *mode_hdr;
4186         u32 page_length;
4187         u32 length;
4188
4189         if (!mode_pages || (mode_pages->hdr.length == 0))
4190                 return NULL;
4191
4192         length = (mode_pages->hdr.length + 1) - 4 - mode_pages->hdr.block_desc_len;
4193         mode_hdr = (struct ipr_mode_page_hdr *)
4194                 (mode_pages->data + mode_pages->hdr.block_desc_len);
4195
4196         while (length) {
4197                 if (IPR_GET_MODE_PAGE_CODE(mode_hdr) == page_code) {
4198                         if (mode_hdr->page_length >= (len - sizeof(struct ipr_mode_page_hdr)))
4199                                 return mode_hdr;
4200                         break;
4201                 } else {
4202                         page_length = (sizeof(struct ipr_mode_page_hdr) +
4203                                        mode_hdr->page_length);
4204                         length -= page_length;
4205                         mode_hdr = (struct ipr_mode_page_hdr *)
4206                                 ((unsigned long)mode_hdr + page_length);
4207                 }
4208         }
4209         return NULL;
4210 }
4211
4212 /**
4213  * ipr_check_term_power - Check for term power errors
4214  * @ioa_cfg:    ioa config struct
4215  * @mode_pages: IOAFP mode pages buffer
4216  *
4217  * Check the IOAFP's mode page 28 for term power errors
4218  *
4219  * Return value:
4220  *      nothing
4221  **/
4222 static void ipr_check_term_power(struct ipr_ioa_cfg *ioa_cfg,
4223                                  struct ipr_mode_pages *mode_pages)
4224 {
4225         int i;
4226         int entry_length;
4227         struct ipr_dev_bus_entry *bus;
4228         struct ipr_mode_page28 *mode_page;
4229
4230         mode_page = ipr_get_mode_page(mode_pages, 0x28,
4231                                       sizeof(struct ipr_mode_page28));
4232
4233         entry_length = mode_page->entry_length;
4234
4235         bus = mode_page->bus;
4236
4237         for (i = 0; i < mode_page->num_entries; i++) {
4238                 if (bus->flags & IPR_SCSI_ATTR_NO_TERM_PWR) {
4239                         dev_err(&ioa_cfg->pdev->dev,
4240                                 "Term power is absent on scsi bus %d\n",
4241                                 bus->res_addr.bus);
4242                 }
4243
4244                 bus = (struct ipr_dev_bus_entry *)((char *)bus + entry_length);
4245         }
4246 }
4247
4248 /**
4249  * ipr_scsi_bus_speed_limit - Limit the SCSI speed based on SES table
4250  * @ioa_cfg:    ioa config struct
4251  *
4252  * Looks through the config table checking for SES devices. If
4253  * the SES device is in the SES table indicating a maximum SCSI
4254  * bus speed, the speed is limited for the bus.
4255  *
4256  * Return value:
4257  *      none
4258  **/
4259 static void ipr_scsi_bus_speed_limit(struct ipr_ioa_cfg *ioa_cfg)
4260 {
4261         u32 max_xfer_rate;
4262         int i;
4263
4264         for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
4265                 max_xfer_rate = ipr_get_max_scsi_speed(ioa_cfg, i,
4266                                                        ioa_cfg->bus_attr[i].bus_width);
4267
4268                 if (max_xfer_rate < ioa_cfg->bus_attr[i].max_xfer_rate)
4269                         ioa_cfg->bus_attr[i].max_xfer_rate = max_xfer_rate;
4270         }
4271 }
4272
4273 /**
4274  * ipr_modify_ioafp_mode_page_28 - Modify IOAFP Mode Page 28
4275  * @ioa_cfg:    ioa config struct
4276  * @mode_pages: mode page 28 buffer
4277  *
4278  * Updates mode page 28 based on driver configuration
4279  *
4280  * Return value:
4281  *      none
4282  **/
4283 static void ipr_modify_ioafp_mode_page_28(struct ipr_ioa_cfg *ioa_cfg,
4284                                                 struct ipr_mode_pages *mode_pages)
4285 {
4286         int i, entry_length;
4287         struct ipr_dev_bus_entry *bus;
4288         struct ipr_bus_attributes *bus_attr;
4289         struct ipr_mode_page28 *mode_page;
4290
4291         mode_page = ipr_get_mode_page(mode_pages, 0x28,
4292                                       sizeof(struct ipr_mode_page28));
4293
4294         entry_length = mode_page->entry_length;
4295
4296         /* Loop for each device bus entry */
4297         for (i = 0, bus = mode_page->bus;
4298              i < mode_page->num_entries;
4299              i++, bus = (struct ipr_dev_bus_entry *)((u8 *)bus + entry_length)) {
4300                 if (bus->res_addr.bus > IPR_MAX_NUM_BUSES) {
4301                         dev_err(&ioa_cfg->pdev->dev,
4302                                 "Invalid resource address reported: 0x%08X\n",
4303                                 IPR_GET_PHYS_LOC(bus->res_addr));
4304                         continue;
4305                 }
4306
4307                 bus_attr = &ioa_cfg->bus_attr[i];
4308                 bus->extended_reset_delay = IPR_EXTENDED_RESET_DELAY;
4309                 bus->bus_width = bus_attr->bus_width;
4310                 bus->max_xfer_rate = cpu_to_be32(bus_attr->max_xfer_rate);
4311                 bus->flags &= ~IPR_SCSI_ATTR_QAS_MASK;
4312                 if (bus_attr->qas_enabled)
4313                         bus->flags |= IPR_SCSI_ATTR_ENABLE_QAS;
4314                 else
4315                         bus->flags |= IPR_SCSI_ATTR_DISABLE_QAS;
4316         }
4317 }
4318
4319 /**
4320  * ipr_build_mode_select - Build a mode select command
4321  * @ipr_cmd:    ipr command struct
4322  * @res_handle: resource handle to send command to
4323  * @parm:               Byte 2 of Mode Sense command
4324  * @dma_addr:   DMA buffer address
4325  * @xfer_len:   data transfer length
4326  *
4327  * Return value:
4328  *      none
4329  **/
4330 static void ipr_build_mode_select(struct ipr_cmnd *ipr_cmd,
4331                                   u32 res_handle, u8 parm, u32 dma_addr,
4332                                   u8 xfer_len)
4333 {
4334         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4335         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4336
4337         ioarcb->res_handle = res_handle;
4338         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
4339         ioarcb->cmd_pkt.flags_hi |= IPR_FLAGS_HI_WRITE_NOT_READ;
4340         ioarcb->cmd_pkt.cdb[0] = MODE_SELECT;
4341         ioarcb->cmd_pkt.cdb[1] = parm;
4342         ioarcb->cmd_pkt.cdb[4] = xfer_len;
4343
4344         ioadl->flags_and_data_len =
4345                 cpu_to_be32(IPR_IOADL_FLAGS_WRITE_LAST | xfer_len);
4346         ioadl->address = cpu_to_be32(dma_addr);
4347         ioarcb->write_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4348         ioarcb->write_data_transfer_length = cpu_to_be32(xfer_len);
4349 }
4350
4351 /**
4352  * ipr_ioafp_mode_select_page28 - Issue Mode Select Page 28 to IOA
4353  * @ipr_cmd:    ipr command struct
4354  *
4355  * This function sets up the SCSI bus attributes and sends
4356  * a Mode Select for Page 28 to activate them.
4357  *
4358  * Return value:
4359  *      IPR_RC_JOB_RETURN
4360  **/
4361 static int ipr_ioafp_mode_select_page28(struct ipr_cmnd *ipr_cmd)
4362 {
4363         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4364         struct ipr_mode_pages *mode_pages = &ioa_cfg->vpd_cbs->mode_pages;
4365         int length;
4366
4367         ENTER;
4368         if (ioa_cfg->saved_mode_pages) {
4369                 memcpy(mode_pages, ioa_cfg->saved_mode_pages,
4370                        ioa_cfg->saved_mode_page_len);
4371                 length = ioa_cfg->saved_mode_page_len;
4372         } else {
4373                 ipr_scsi_bus_speed_limit(ioa_cfg);
4374                 ipr_check_term_power(ioa_cfg, mode_pages);
4375                 ipr_modify_ioafp_mode_page_28(ioa_cfg, mode_pages);
4376                 length = mode_pages->hdr.length + 1;
4377                 mode_pages->hdr.length = 0;
4378         }
4379
4380         ipr_build_mode_select(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE), 0x11,
4381                               ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, mode_pages),
4382                               length);
4383
4384         ipr_cmd->job_step = ipr_set_supported_devs;
4385         ipr_cmd->u.res = list_entry(ioa_cfg->used_res_q.next,
4386                                     struct ipr_resource_entry, queue);
4387
4388         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4389
4390         LEAVE;
4391         return IPR_RC_JOB_RETURN;
4392 }
4393
4394 /**
4395  * ipr_build_mode_sense - Builds a mode sense command
4396  * @ipr_cmd:    ipr command struct
4397  * @res:                resource entry struct
4398  * @parm:               Byte 2 of mode sense command
4399  * @dma_addr:   DMA address of mode sense buffer
4400  * @xfer_len:   Size of DMA buffer
4401  *
4402  * Return value:
4403  *      none
4404  **/
4405 static void ipr_build_mode_sense(struct ipr_cmnd *ipr_cmd,
4406                                  u32 res_handle,
4407                                  u8 parm, u32 dma_addr, u8 xfer_len)
4408 {
4409         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4410         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4411
4412         ioarcb->res_handle = res_handle;
4413         ioarcb->cmd_pkt.cdb[0] = MODE_SENSE;
4414         ioarcb->cmd_pkt.cdb[2] = parm;
4415         ioarcb->cmd_pkt.cdb[4] = xfer_len;
4416         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
4417
4418         ioadl->flags_and_data_len =
4419                 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | xfer_len);
4420         ioadl->address = cpu_to_be32(dma_addr);
4421         ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4422         ioarcb->read_data_transfer_length = cpu_to_be32(xfer_len);
4423 }
4424
4425 /**
4426  * ipr_ioafp_mode_sense_page28 - Issue Mode Sense Page 28 to IOA
4427  * @ipr_cmd:    ipr command struct
4428  *
4429  * This function send a Page 28 mode sense to the IOA to
4430  * retrieve SCSI bus attributes.
4431  *
4432  * Return value:
4433  *      IPR_RC_JOB_RETURN
4434  **/
4435 static int ipr_ioafp_mode_sense_page28(struct ipr_cmnd *ipr_cmd)
4436 {
4437         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4438
4439         ENTER;
4440         ipr_build_mode_sense(ipr_cmd, cpu_to_be32(IPR_IOA_RES_HANDLE),
4441                              0x28, ioa_cfg->vpd_cbs_dma +
4442                              offsetof(struct ipr_misc_cbs, mode_pages),
4443                              sizeof(struct ipr_mode_pages));
4444
4445         ipr_cmd->job_step = ipr_ioafp_mode_select_page28;
4446
4447         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4448
4449         LEAVE;
4450         return IPR_RC_JOB_RETURN;
4451 }
4452
4453 /**
4454  * ipr_init_res_table - Initialize the resource table
4455  * @ipr_cmd:    ipr command struct
4456  *
4457  * This function looks through the existing resource table, comparing
4458  * it with the config table. This function will take care of old/new
4459  * devices and schedule adding/removing them from the mid-layer
4460  * as appropriate.
4461  *
4462  * Return value:
4463  *      IPR_RC_JOB_CONTINUE
4464  **/
4465 static int ipr_init_res_table(struct ipr_cmnd *ipr_cmd)
4466 {
4467         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4468         struct ipr_resource_entry *res, *temp;
4469         struct ipr_config_table_entry *cfgte;
4470         int found, i;
4471         LIST_HEAD(old_res);
4472
4473         ENTER;
4474         if (ioa_cfg->cfg_table->hdr.flags & IPR_UCODE_DOWNLOAD_REQ)
4475                 dev_err(&ioa_cfg->pdev->dev, "Microcode download required\n");
4476
4477         list_for_each_entry_safe(res, temp, &ioa_cfg->used_res_q, queue)
4478                 list_move_tail(&res->queue, &old_res);
4479
4480         for (i = 0; i < ioa_cfg->cfg_table->hdr.num_entries; i++) {
4481                 cfgte = &ioa_cfg->cfg_table->dev[i];
4482                 found = 0;
4483
4484                 list_for_each_entry_safe(res, temp, &old_res, queue) {
4485                         if (!memcmp(&res->cfgte.res_addr,
4486                                     &cfgte->res_addr, sizeof(cfgte->res_addr))) {
4487                                 list_move_tail(&res->queue, &ioa_cfg->used_res_q);
4488                                 found = 1;
4489                                 break;
4490                         }
4491                 }
4492
4493                 if (!found) {
4494                         if (list_empty(&ioa_cfg->free_res_q)) {
4495                                 dev_err(&ioa_cfg->pdev->dev, "Too many devices attached\n");
4496                                 break;
4497                         }
4498
4499                         found = 1;
4500                         res = list_entry(ioa_cfg->free_res_q.next,
4501                                          struct ipr_resource_entry, queue);
4502                         list_move_tail(&res->queue, &ioa_cfg->used_res_q);
4503                         ipr_init_res_entry(res);
4504                         res->add_to_ml = 1;
4505                 }
4506
4507                 if (found)
4508                         memcpy(&res->cfgte, cfgte, sizeof(struct ipr_config_table_entry));
4509         }
4510
4511         list_for_each_entry_safe(res, temp, &old_res, queue) {
4512                 if (res->sdev) {
4513                         res->del_from_ml = 1;
4514                         list_move_tail(&res->queue, &ioa_cfg->used_res_q);
4515                 } else {
4516                         list_move_tail(&res->queue, &ioa_cfg->free_res_q);
4517                 }
4518         }
4519
4520         ipr_cmd->job_step = ipr_ioafp_mode_sense_page28;
4521
4522         LEAVE;
4523         return IPR_RC_JOB_CONTINUE;
4524 }
4525
4526 /**
4527  * ipr_ioafp_query_ioa_cfg - Send a Query IOA Config to the adapter.
4528  * @ipr_cmd:    ipr command struct
4529  *
4530  * This function sends a Query IOA Configuration command
4531  * to the adapter to retrieve the IOA configuration table.
4532  *
4533  * Return value:
4534  *      IPR_RC_JOB_RETURN
4535  **/
4536 static int ipr_ioafp_query_ioa_cfg(struct ipr_cmnd *ipr_cmd)
4537 {
4538         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4539         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4540         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4541         struct ipr_inquiry_page3 *ucode_vpd = &ioa_cfg->vpd_cbs->page3_data;
4542
4543         ENTER;
4544         dev_info(&ioa_cfg->pdev->dev, "Adapter firmware version: %02X%02X%02X%02X\n",
4545                  ucode_vpd->major_release, ucode_vpd->card_type,
4546                  ucode_vpd->minor_release[0], ucode_vpd->minor_release[1]);
4547         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4548         ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4549
4550         ioarcb->cmd_pkt.cdb[0] = IPR_QUERY_IOA_CONFIG;
4551         ioarcb->cmd_pkt.cdb[7] = (sizeof(struct ipr_config_table) >> 8) & 0xff;
4552         ioarcb->cmd_pkt.cdb[8] = sizeof(struct ipr_config_table) & 0xff;
4553
4554         ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4555         ioarcb->read_data_transfer_length =
4556                 cpu_to_be32(sizeof(struct ipr_config_table));
4557
4558         ioadl->address = cpu_to_be32(ioa_cfg->cfg_table_dma);
4559         ioadl->flags_and_data_len =
4560                 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | sizeof(struct ipr_config_table));
4561
4562         ipr_cmd->job_step = ipr_init_res_table;
4563
4564         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4565
4566         LEAVE;
4567         return IPR_RC_JOB_RETURN;
4568 }
4569
4570 /**
4571  * ipr_ioafp_inquiry - Send an Inquiry to the adapter.
4572  * @ipr_cmd:    ipr command struct
4573  *
4574  * This utility function sends an inquiry to the adapter.
4575  *
4576  * Return value:
4577  *      none
4578  **/
4579 static void ipr_ioafp_inquiry(struct ipr_cmnd *ipr_cmd, u8 flags, u8 page,
4580                               u32 dma_addr, u8 xfer_len)
4581 {
4582         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4583         struct ipr_ioadl_desc *ioadl = ipr_cmd->ioadl;
4584
4585         ENTER;
4586         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
4587         ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4588
4589         ioarcb->cmd_pkt.cdb[0] = INQUIRY;
4590         ioarcb->cmd_pkt.cdb[1] = flags;
4591         ioarcb->cmd_pkt.cdb[2] = page;
4592         ioarcb->cmd_pkt.cdb[4] = xfer_len;
4593
4594         ioarcb->read_ioadl_len = cpu_to_be32(sizeof(struct ipr_ioadl_desc));
4595         ioarcb->read_data_transfer_length = cpu_to_be32(xfer_len);
4596
4597         ioadl->address = cpu_to_be32(dma_addr);
4598         ioadl->flags_and_data_len =
4599                 cpu_to_be32(IPR_IOADL_FLAGS_READ_LAST | xfer_len);
4600
4601         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4602         LEAVE;
4603 }
4604
4605 /**
4606  * ipr_ioafp_page3_inquiry - Send a Page 3 Inquiry to the adapter.
4607  * @ipr_cmd:    ipr command struct
4608  *
4609  * This function sends a Page 3 inquiry to the adapter
4610  * to retrieve software VPD information.
4611  *
4612  * Return value:
4613  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4614  **/
4615 static int ipr_ioafp_page3_inquiry(struct ipr_cmnd *ipr_cmd)
4616 {
4617         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4618         char type[5];
4619
4620         ENTER;
4621
4622         /* Grab the type out of the VPD and store it away */
4623         memcpy(type, ioa_cfg->vpd_cbs->ioa_vpd.std_inq_data.vpids.product_id, 4);
4624         type[4] = '\0';
4625         ioa_cfg->type = simple_strtoul((char *)type, NULL, 16);
4626
4627         ipr_cmd->job_step = ipr_ioafp_query_ioa_cfg;
4628
4629         ipr_ioafp_inquiry(ipr_cmd, 1, 3,
4630                           ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, page3_data),
4631                           sizeof(struct ipr_inquiry_page3));
4632
4633         LEAVE;
4634         return IPR_RC_JOB_RETURN;
4635 }
4636
4637 /**
4638  * ipr_ioafp_std_inquiry - Send a Standard Inquiry to the adapter.
4639  * @ipr_cmd:    ipr command struct
4640  *
4641  * This function sends a standard inquiry to the adapter.
4642  *
4643  * Return value:
4644  *      IPR_RC_JOB_RETURN
4645  **/
4646 static int ipr_ioafp_std_inquiry(struct ipr_cmnd *ipr_cmd)
4647 {
4648         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4649
4650         ENTER;
4651         ipr_cmd->job_step = ipr_ioafp_page3_inquiry;
4652
4653         ipr_ioafp_inquiry(ipr_cmd, 0, 0,
4654                           ioa_cfg->vpd_cbs_dma + offsetof(struct ipr_misc_cbs, ioa_vpd),
4655                           sizeof(struct ipr_ioa_vpd));
4656
4657         LEAVE;
4658         return IPR_RC_JOB_RETURN;
4659 }
4660
4661 /**
4662  * ipr_ioafp_indentify_hrrq - Send Identify Host RRQ.
4663  * @ipr_cmd:    ipr command struct
4664  *
4665  * This function send an Identify Host Request Response Queue
4666  * command to establish the HRRQ with the adapter.
4667  *
4668  * Return value:
4669  *      IPR_RC_JOB_RETURN
4670  **/
4671 static int ipr_ioafp_indentify_hrrq(struct ipr_cmnd *ipr_cmd)
4672 {
4673         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4674         struct ipr_ioarcb *ioarcb = &ipr_cmd->ioarcb;
4675
4676         ENTER;
4677         dev_info(&ioa_cfg->pdev->dev, "Starting IOA initialization sequence.\n");
4678
4679         ioarcb->cmd_pkt.cdb[0] = IPR_ID_HOST_RR_Q;
4680         ioarcb->res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
4681
4682         ioarcb->cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
4683         ioarcb->cmd_pkt.cdb[2] =
4684                 ((u32) ioa_cfg->host_rrq_dma >> 24) & 0xff;
4685         ioarcb->cmd_pkt.cdb[3] =
4686                 ((u32) ioa_cfg->host_rrq_dma >> 16) & 0xff;
4687         ioarcb->cmd_pkt.cdb[4] =
4688                 ((u32) ioa_cfg->host_rrq_dma >> 8) & 0xff;
4689         ioarcb->cmd_pkt.cdb[5] =
4690                 ((u32) ioa_cfg->host_rrq_dma) & 0xff;
4691         ioarcb->cmd_pkt.cdb[7] =
4692                 ((sizeof(u32) * IPR_NUM_CMD_BLKS) >> 8) & 0xff;
4693         ioarcb->cmd_pkt.cdb[8] =
4694                 (sizeof(u32) * IPR_NUM_CMD_BLKS) & 0xff;
4695
4696         ipr_cmd->job_step = ipr_ioafp_std_inquiry;
4697
4698         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, IPR_INTERNAL_TIMEOUT);
4699
4700         LEAVE;
4701         return IPR_RC_JOB_RETURN;
4702 }
4703
4704 /**
4705  * ipr_reset_timer_done - Adapter reset timer function
4706  * @ipr_cmd:    ipr command struct
4707  *
4708  * Description: This function is used in adapter reset processing
4709  * for timing events. If the reset_cmd pointer in the IOA
4710  * config struct is not this adapter's we are doing nested
4711  * resets and fail_all_ops will take care of freeing the
4712  * command block.
4713  *
4714  * Return value:
4715  *      none
4716  **/
4717 static void ipr_reset_timer_done(struct ipr_cmnd *ipr_cmd)
4718 {
4719         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4720         unsigned long lock_flags = 0;
4721
4722         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
4723
4724         if (ioa_cfg->reset_cmd == ipr_cmd) {
4725                 list_del(&ipr_cmd->queue);
4726                 ipr_cmd->done(ipr_cmd);
4727         }
4728
4729         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
4730 }
4731
4732 /**
4733  * ipr_reset_start_timer - Start a timer for adapter reset job
4734  * @ipr_cmd:    ipr command struct
4735  * @timeout:    timeout value
4736  *
4737  * Description: This function is used in adapter reset processing
4738  * for timing events. If the reset_cmd pointer in the IOA
4739  * config struct is not this adapter's we are doing nested
4740  * resets and fail_all_ops will take care of freeing the
4741  * command block.
4742  *
4743  * Return value:
4744  *      none
4745  **/
4746 static void ipr_reset_start_timer(struct ipr_cmnd *ipr_cmd,
4747                                   unsigned long timeout)
4748 {
4749         list_add_tail(&ipr_cmd->queue, &ipr_cmd->ioa_cfg->pending_q);
4750         ipr_cmd->done = ipr_reset_ioa_job;
4751
4752         ipr_cmd->timer.data = (unsigned long) ipr_cmd;
4753         ipr_cmd->timer.expires = jiffies + timeout;
4754         ipr_cmd->timer.function = (void (*)(unsigned long))ipr_reset_timer_done;
4755         add_timer(&ipr_cmd->timer);
4756 }
4757
4758 /**
4759  * ipr_init_ioa_mem - Initialize ioa_cfg control block
4760  * @ioa_cfg:    ioa cfg struct
4761  *
4762  * Return value:
4763  *      nothing
4764  **/
4765 static void ipr_init_ioa_mem(struct ipr_ioa_cfg *ioa_cfg)
4766 {
4767         memset(ioa_cfg->host_rrq, 0, sizeof(u32) * IPR_NUM_CMD_BLKS);
4768
4769         /* Initialize Host RRQ pointers */
4770         ioa_cfg->hrrq_start = ioa_cfg->host_rrq;
4771         ioa_cfg->hrrq_end = &ioa_cfg->host_rrq[IPR_NUM_CMD_BLKS - 1];
4772         ioa_cfg->hrrq_curr = ioa_cfg->hrrq_start;
4773         ioa_cfg->toggle_bit = 1;
4774
4775         /* Zero out config table */
4776         memset(ioa_cfg->cfg_table, 0, sizeof(struct ipr_config_table));
4777 }
4778
4779 /**
4780  * ipr_reset_enable_ioa - Enable the IOA following a reset.
4781  * @ipr_cmd:    ipr command struct
4782  *
4783  * This function reinitializes some control blocks and
4784  * enables destructive diagnostics on the adapter.
4785  *
4786  * Return value:
4787  *      IPR_RC_JOB_RETURN
4788  **/
4789 static int ipr_reset_enable_ioa(struct ipr_cmnd *ipr_cmd)
4790 {
4791         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4792         volatile u32 int_reg;
4793
4794         ENTER;
4795         ipr_cmd->job_step = ipr_ioafp_indentify_hrrq;
4796         ipr_init_ioa_mem(ioa_cfg);
4797
4798         ioa_cfg->allow_interrupts = 1;
4799         int_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
4800
4801         if (int_reg & IPR_PCII_IOA_TRANS_TO_OPER) {
4802                 writel((IPR_PCII_ERROR_INTERRUPTS | IPR_PCII_HRRQ_UPDATED),
4803                        ioa_cfg->regs.clr_interrupt_mask_reg);
4804                 int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
4805                 return IPR_RC_JOB_CONTINUE;
4806         }
4807
4808         /* Enable destructive diagnostics on IOA */
4809         writel(IPR_DOORBELL, ioa_cfg->regs.set_uproc_interrupt_reg);
4810
4811         writel(IPR_PCII_OPER_INTERRUPTS, ioa_cfg->regs.clr_interrupt_mask_reg);
4812         int_reg = readl(ioa_cfg->regs.sense_interrupt_mask_reg);
4813
4814         dev_info(&ioa_cfg->pdev->dev, "Initializing IOA.\n");
4815
4816         ipr_cmd->timer.data = (unsigned long) ipr_cmd;
4817         ipr_cmd->timer.expires = jiffies + IPR_OPERATIONAL_TIMEOUT;
4818         ipr_cmd->timer.function = (void (*)(unsigned long))ipr_timeout;
4819         ipr_cmd->done = ipr_reset_ioa_job;
4820         add_timer(&ipr_cmd->timer);
4821         list_add_tail(&ipr_cmd->queue, &ioa_cfg->pending_q);
4822
4823         LEAVE;
4824         return IPR_RC_JOB_RETURN;
4825 }
4826
4827 /**
4828  * ipr_reset_wait_for_dump - Wait for a dump to timeout.
4829  * @ipr_cmd:    ipr command struct
4830  *
4831  * This function is invoked when an adapter dump has run out
4832  * of processing time.
4833  *
4834  * Return value:
4835  *      IPR_RC_JOB_CONTINUE
4836  **/
4837 static int ipr_reset_wait_for_dump(struct ipr_cmnd *ipr_cmd)
4838 {
4839         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4840
4841         if (ioa_cfg->sdt_state == GET_DUMP)
4842                 ioa_cfg->sdt_state = ABORT_DUMP;
4843
4844         ipr_cmd->job_step = ipr_reset_alert;
4845
4846         return IPR_RC_JOB_CONTINUE;
4847 }
4848
4849 /**
4850  * ipr_unit_check_no_data - Log a unit check/no data error log
4851  * @ioa_cfg:            ioa config struct
4852  *
4853  * Logs an error indicating the adapter unit checked, but for some
4854  * reason, we were unable to fetch the unit check buffer.
4855  *
4856  * Return value:
4857  *      nothing
4858  **/
4859 static void ipr_unit_check_no_data(struct ipr_ioa_cfg *ioa_cfg)
4860 {
4861         ioa_cfg->errors_logged++;
4862         dev_err(&ioa_cfg->pdev->dev, "IOA unit check with no data\n");
4863 }
4864
4865 /**
4866  * ipr_get_unit_check_buffer - Get the unit check buffer from the IOA
4867  * @ioa_cfg:            ioa config struct
4868  *
4869  * Fetches the unit check buffer from the adapter by clocking the data
4870  * through the mailbox register.
4871  *
4872  * Return value:
4873  *      nothing
4874  **/
4875 static void ipr_get_unit_check_buffer(struct ipr_ioa_cfg *ioa_cfg)
4876 {
4877         unsigned long mailbox;
4878         struct ipr_hostrcb *hostrcb;
4879         struct ipr_uc_sdt sdt;
4880         int rc, length;
4881
4882         mailbox = readl(ioa_cfg->ioa_mailbox);
4883
4884         if (!ipr_sdt_is_fmt2(mailbox)) {
4885                 ipr_unit_check_no_data(ioa_cfg);
4886                 return;
4887         }
4888
4889         memset(&sdt, 0, sizeof(struct ipr_uc_sdt));
4890         rc = ipr_get_ldump_data_section(ioa_cfg, mailbox, (u32 *) &sdt,
4891                                         (sizeof(struct ipr_uc_sdt)) / sizeof(u32));
4892
4893         if (rc || (be32_to_cpu(sdt.hdr.state) != IPR_FMT2_SDT_READY_TO_USE) ||
4894             !(sdt.entry[0].flags & IPR_SDT_VALID_ENTRY)) {
4895                 ipr_unit_check_no_data(ioa_cfg);
4896                 return;
4897         }
4898
4899         /* Find length of the first sdt entry (UC buffer) */
4900         length = (be32_to_cpu(sdt.entry[0].end_offset) -
4901                   be32_to_cpu(sdt.entry[0].bar_str_offset)) & IPR_FMT2_MBX_ADDR_MASK;
4902
4903         hostrcb = list_entry(ioa_cfg->hostrcb_free_q.next,
4904                              struct ipr_hostrcb, queue);
4905         list_del(&hostrcb->queue);
4906         memset(&hostrcb->hcam, 0, sizeof(hostrcb->hcam));
4907
4908         rc = ipr_get_ldump_data_section(ioa_cfg,
4909                                         be32_to_cpu(sdt.entry[0].bar_str_offset),
4910                                         (u32 *)&hostrcb->hcam,
4911                                         min(length, (int)sizeof(hostrcb->hcam)) / sizeof(u32));
4912
4913         if (!rc)
4914                 ipr_handle_log_data(ioa_cfg, hostrcb);
4915         else
4916                 ipr_unit_check_no_data(ioa_cfg);
4917
4918         list_add_tail(&hostrcb->queue, &ioa_cfg->hostrcb_free_q);
4919 }
4920
4921 /**
4922  * ipr_reset_restore_cfg_space - Restore PCI config space.
4923  * @ipr_cmd:    ipr command struct
4924  *
4925  * Description: This function restores the saved PCI config space of
4926  * the adapter, fails all outstanding ops back to the callers, and
4927  * fetches the dump/unit check if applicable to this reset.
4928  *
4929  * Return value:
4930  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4931  **/
4932 static int ipr_reset_restore_cfg_space(struct ipr_cmnd *ipr_cmd)
4933 {
4934         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4935         int rc;
4936
4937         ENTER;
4938         rc = pci_restore_state(ioa_cfg->pdev, ioa_cfg->pci_cfg_buf);
4939
4940         if (rc != PCIBIOS_SUCCESSFUL) {
4941                 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
4942                 return IPR_RC_JOB_CONTINUE;
4943         }
4944
4945         if (ipr_set_pcix_cmd_reg(ioa_cfg)) {
4946                 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
4947                 return IPR_RC_JOB_CONTINUE;
4948         }
4949
4950         ipr_fail_all_ops(ioa_cfg);
4951
4952         if (ioa_cfg->ioa_unit_checked) {
4953                 ioa_cfg->ioa_unit_checked = 0;
4954                 ipr_get_unit_check_buffer(ioa_cfg);
4955                 ipr_cmd->job_step = ipr_reset_alert;
4956                 ipr_reset_start_timer(ipr_cmd, 0);
4957                 return IPR_RC_JOB_RETURN;
4958         }
4959
4960         if (ioa_cfg->in_ioa_bringdown) {
4961                 ipr_cmd->job_step = ipr_ioa_bringdown_done;
4962         } else {
4963                 ipr_cmd->job_step = ipr_reset_enable_ioa;
4964
4965                 if (GET_DUMP == ioa_cfg->sdt_state) {
4966                         ipr_reset_start_timer(ipr_cmd, IPR_DUMP_TIMEOUT);
4967                         ipr_cmd->job_step = ipr_reset_wait_for_dump;
4968                         schedule_work(&ioa_cfg->work_q);
4969                         return IPR_RC_JOB_RETURN;
4970                 }
4971         }
4972
4973         ENTER;
4974         return IPR_RC_JOB_CONTINUE;
4975 }
4976
4977 /**
4978  * ipr_reset_start_bist - Run BIST on the adapter.
4979  * @ipr_cmd:    ipr command struct
4980  *
4981  * Description: This function runs BIST on the adapter, then delays 2 seconds.
4982  *
4983  * Return value:
4984  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
4985  **/
4986 static int ipr_reset_start_bist(struct ipr_cmnd *ipr_cmd)
4987 {
4988         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
4989         int rc;
4990
4991         ENTER;
4992         rc = pci_write_config_byte(ioa_cfg->pdev, PCI_BIST, PCI_BIST_START);
4993
4994         if (rc != PCIBIOS_SUCCESSFUL) {
4995                 ipr_cmd->ioasa.ioasc = cpu_to_be32(IPR_IOASC_PCI_ACCESS_ERROR);
4996                 rc = IPR_RC_JOB_CONTINUE;
4997         } else {
4998                 ipr_cmd->job_step = ipr_reset_restore_cfg_space;
4999                 ipr_reset_start_timer(ipr_cmd, IPR_WAIT_FOR_BIST_TIMEOUT);
5000                 rc = IPR_RC_JOB_RETURN;
5001         }
5002
5003         LEAVE;
5004         return rc;
5005 }
5006
5007 /**
5008  * ipr_reset_allowed - Query whether or not IOA can be reset
5009  * @ioa_cfg:    ioa config struct
5010  *
5011  * Return value:
5012  *      0 if reset not allowed / non-zero if reset is allowed
5013  **/
5014 static int ipr_reset_allowed(struct ipr_ioa_cfg *ioa_cfg)
5015 {
5016         volatile u32 temp_reg;
5017
5018         temp_reg = readl(ioa_cfg->regs.sense_interrupt_reg);
5019         return ((temp_reg & IPR_PCII_CRITICAL_OPERATION) == 0);
5020 }
5021
5022 /**
5023  * ipr_reset_wait_to_start_bist - Wait for permission to reset IOA.
5024  * @ipr_cmd:    ipr command struct
5025  *
5026  * Description: This function waits for adapter permission to run BIST,
5027  * then runs BIST. If the adapter does not give permission after a
5028  * reasonable time, we will reset the adapter anyway. The impact of
5029  * resetting the adapter without warning the adapter is the risk of
5030  * losing the persistent error log on the adapter. If the adapter is
5031  * reset while it is writing to the flash on the adapter, the flash
5032  * segment will have bad ECC and be zeroed.
5033  *
5034  * Return value:
5035  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5036  **/
5037 static int ipr_reset_wait_to_start_bist(struct ipr_cmnd *ipr_cmd)
5038 {
5039         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5040         int rc = IPR_RC_JOB_RETURN;
5041
5042         if (!ipr_reset_allowed(ioa_cfg) && ipr_cmd->u.time_left) {
5043                 ipr_cmd->u.time_left -= IPR_CHECK_FOR_RESET_TIMEOUT;
5044                 ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
5045         } else {
5046                 ipr_cmd->job_step = ipr_reset_start_bist;
5047                 rc = IPR_RC_JOB_CONTINUE;
5048         }
5049
5050         return rc;
5051 }
5052
5053 /**
5054  * ipr_reset_alert_part2 - Alert the adapter of a pending reset
5055  * @ipr_cmd:    ipr command struct
5056  *
5057  * Description: This function alerts the adapter that it will be reset.
5058  * If memory space is not currently enabled, proceed directly
5059  * to running BIST on the adapter. The timer must always be started
5060  * so we guarantee we do not run BIST from ipr_isr.
5061  *
5062  * Return value:
5063  *      IPR_RC_JOB_RETURN
5064  **/
5065 static int ipr_reset_alert(struct ipr_cmnd *ipr_cmd)
5066 {
5067         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5068         u16 cmd_reg;
5069         int rc;
5070
5071         ENTER;
5072         rc = pci_read_config_word(ioa_cfg->pdev, PCI_COMMAND, &cmd_reg);
5073
5074         if ((rc == PCIBIOS_SUCCESSFUL) && (cmd_reg & PCI_COMMAND_MEMORY)) {
5075                 ipr_mask_and_clear_interrupts(ioa_cfg, ~0);
5076                 writel(IPR_UPROCI_RESET_ALERT, ioa_cfg->regs.set_uproc_interrupt_reg);
5077                 ipr_cmd->job_step = ipr_reset_wait_to_start_bist;
5078         } else {
5079                 ipr_cmd->job_step = ipr_reset_start_bist;
5080         }
5081
5082         ipr_cmd->u.time_left = IPR_WAIT_FOR_RESET_TIMEOUT;
5083         ipr_reset_start_timer(ipr_cmd, IPR_CHECK_FOR_RESET_TIMEOUT);
5084
5085         LEAVE;
5086         return IPR_RC_JOB_RETURN;
5087 }
5088
5089 /**
5090  * ipr_reset_ucode_download_done - Microcode download completion
5091  * @ipr_cmd:    ipr command struct
5092  *
5093  * Description: This function unmaps the microcode download buffer.
5094  *
5095  * Return value:
5096  *      IPR_RC_JOB_CONTINUE
5097  **/
5098 static int ipr_reset_ucode_download_done(struct ipr_cmnd *ipr_cmd)
5099 {
5100         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5101         struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
5102
5103         pci_unmap_sg(ioa_cfg->pdev, sglist->scatterlist,
5104                      sglist->num_sg, DMA_TO_DEVICE);
5105
5106         ipr_cmd->job_step = ipr_reset_alert;
5107         return IPR_RC_JOB_CONTINUE;
5108 }
5109
5110 /**
5111  * ipr_reset_ucode_download - Download microcode to the adapter
5112  * @ipr_cmd:    ipr command struct
5113  *
5114  * Description: This function checks to see if it there is microcode
5115  * to download to the adapter. If there is, a download is performed.
5116  *
5117  * Return value:
5118  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5119  **/
5120 static int ipr_reset_ucode_download(struct ipr_cmnd *ipr_cmd)
5121 {
5122         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5123         struct ipr_sglist *sglist = ioa_cfg->ucode_sglist;
5124
5125         ENTER;
5126         ipr_cmd->job_step = ipr_reset_alert;
5127
5128         if (!sglist)
5129                 return IPR_RC_JOB_CONTINUE;
5130
5131         ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5132         ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_SCSICDB;
5133         ipr_cmd->ioarcb.cmd_pkt.cdb[0] = WRITE_BUFFER;
5134         ipr_cmd->ioarcb.cmd_pkt.cdb[1] = IPR_WR_BUF_DOWNLOAD_AND_SAVE;
5135         ipr_cmd->ioarcb.cmd_pkt.cdb[6] = (sglist->buffer_len & 0xff0000) >> 16;
5136         ipr_cmd->ioarcb.cmd_pkt.cdb[7] = (sglist->buffer_len & 0x00ff00) >> 8;
5137         ipr_cmd->ioarcb.cmd_pkt.cdb[8] = sglist->buffer_len & 0x0000ff;
5138
5139         if (ipr_map_ucode_buffer(ipr_cmd, sglist, sglist->buffer_len)) {
5140                 dev_err(&ioa_cfg->pdev->dev,
5141                         "Failed to map microcode download buffer\n");
5142                 return IPR_RC_JOB_CONTINUE;
5143         }
5144
5145         ipr_cmd->job_step = ipr_reset_ucode_download_done;
5146
5147         ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout,
5148                    IPR_WRITE_BUFFER_TIMEOUT);
5149
5150         LEAVE;
5151         return IPR_RC_JOB_RETURN;
5152 }
5153
5154 /**
5155  * ipr_reset_shutdown_ioa - Shutdown the adapter
5156  * @ipr_cmd:    ipr command struct
5157  *
5158  * Description: This function issues an adapter shutdown of the
5159  * specified type to the specified adapter as part of the
5160  * adapter reset job.
5161  *
5162  * Return value:
5163  *      IPR_RC_JOB_CONTINUE / IPR_RC_JOB_RETURN
5164  **/
5165 static int ipr_reset_shutdown_ioa(struct ipr_cmnd *ipr_cmd)
5166 {
5167         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5168         enum ipr_shutdown_type shutdown_type = ipr_cmd->u.shutdown_type;
5169         unsigned long timeout;
5170         int rc = IPR_RC_JOB_CONTINUE;
5171
5172         ENTER;
5173         if (shutdown_type != IPR_SHUTDOWN_NONE && !ioa_cfg->ioa_is_dead) {
5174                 ipr_cmd->ioarcb.res_handle = cpu_to_be32(IPR_IOA_RES_HANDLE);
5175                 ipr_cmd->ioarcb.cmd_pkt.request_type = IPR_RQTYPE_IOACMD;
5176                 ipr_cmd->ioarcb.cmd_pkt.cdb[0] = IPR_IOA_SHUTDOWN;
5177                 ipr_cmd->ioarcb.cmd_pkt.cdb[1] = shutdown_type;
5178
5179                 if (shutdown_type == IPR_SHUTDOWN_ABBREV)
5180                         timeout = IPR_ABBREV_SHUTDOWN_TIMEOUT;
5181                 else if (shutdown_type == IPR_SHUTDOWN_PREPARE_FOR_NORMAL)
5182                         timeout = IPR_INTERNAL_TIMEOUT;
5183                 else
5184                         timeout = IPR_SHUTDOWN_TIMEOUT;
5185
5186                 ipr_do_req(ipr_cmd, ipr_reset_ioa_job, ipr_timeout, timeout);
5187
5188                 rc = IPR_RC_JOB_RETURN;
5189                 ipr_cmd->job_step = ipr_reset_ucode_download;
5190         } else
5191                 ipr_cmd->job_step = ipr_reset_alert;
5192
5193         LEAVE;
5194         return rc;
5195 }
5196
5197 /**
5198  * ipr_reset_ioa_job - Adapter reset job
5199  * @ipr_cmd:    ipr command struct
5200  *
5201  * Description: This function is the job router for the adapter reset job.
5202  *
5203  * Return value:
5204  *      none
5205  **/
5206 static void ipr_reset_ioa_job(struct ipr_cmnd *ipr_cmd)
5207 {
5208         u32 rc, ioasc;
5209         unsigned long scratch = ipr_cmd->u.scratch;
5210         struct ipr_ioa_cfg *ioa_cfg = ipr_cmd->ioa_cfg;
5211
5212         do {
5213                 ioasc = be32_to_cpu(ipr_cmd->ioasa.ioasc);
5214
5215                 if (ioa_cfg->reset_cmd != ipr_cmd) {
5216                         /*
5217                          * We are doing nested adapter resets and this is
5218                          * not the current reset job.
5219                          */
5220                         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5221                         return;
5222                 }
5223
5224                 if (IPR_IOASC_SENSE_KEY(ioasc)) {
5225                         dev_err(&ioa_cfg->pdev->dev,
5226                                 "0x%02X failed with IOASC: 0x%08X\n",
5227                                 ipr_cmd->ioarcb.cmd_pkt.cdb[0], ioasc);
5228
5229                         ipr_initiate_ioa_reset(ioa_cfg, IPR_SHUTDOWN_NONE);
5230                         list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5231                         return;
5232                 }
5233
5234                 ipr_reinit_ipr_cmnd(ipr_cmd);
5235                 ipr_cmd->u.scratch = scratch;
5236                 rc = ipr_cmd->job_step(ipr_cmd);
5237         } while(rc == IPR_RC_JOB_CONTINUE);
5238 }
5239
5240 /**
5241  * _ipr_initiate_ioa_reset - Initiate an adapter reset
5242  * @ioa_cfg:            ioa config struct
5243  * @job_step:           first job step of reset job
5244  * @shutdown_type:      shutdown type
5245  *
5246  * Description: This function will initiate the reset of the given adapter
5247  * starting at the selected job step.
5248  * If the caller needs to wait on the completion of the reset,
5249  * the caller must sleep on the reset_wait_q.
5250  *
5251  * Return value:
5252  *      none
5253  **/
5254 static void _ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
5255                                     int (*job_step) (struct ipr_cmnd *),
5256                                     enum ipr_shutdown_type shutdown_type)
5257 {
5258         struct ipr_cmnd *ipr_cmd;
5259
5260         ioa_cfg->in_reset_reload = 1;
5261         ioa_cfg->allow_cmds = 0;
5262         scsi_block_requests(ioa_cfg->host);
5263
5264         ipr_cmd = ipr_get_free_ipr_cmnd(ioa_cfg);
5265         ioa_cfg->reset_cmd = ipr_cmd;
5266         ipr_cmd->job_step = job_step;
5267         ipr_cmd->u.shutdown_type = shutdown_type;
5268
5269         ipr_reset_ioa_job(ipr_cmd);
5270 }
5271
5272 /**
5273  * ipr_initiate_ioa_reset - Initiate an adapter reset
5274  * @ioa_cfg:            ioa config struct
5275  * @shutdown_type:      shutdown type
5276  *
5277  * Description: This function will initiate the reset of the given adapter.
5278  * If the caller needs to wait on the completion of the reset,
5279  * the caller must sleep on the reset_wait_q.
5280  *
5281  * Return value:
5282  *      none
5283  **/
5284 static void ipr_initiate_ioa_reset(struct ipr_ioa_cfg *ioa_cfg,
5285                                    enum ipr_shutdown_type shutdown_type)
5286 {
5287         if (ioa_cfg->ioa_is_dead)
5288                 return;
5289
5290         if (ioa_cfg->in_reset_reload && ioa_cfg->sdt_state == GET_DUMP)
5291                 ioa_cfg->sdt_state = ABORT_DUMP;
5292
5293         if (ioa_cfg->reset_retries++ > IPR_NUM_RESET_RELOAD_RETRIES) {
5294                 dev_err(&ioa_cfg->pdev->dev,
5295                         "IOA taken offline - error recovery failed\n");
5296
5297                 ioa_cfg->reset_retries = 0;
5298                 ioa_cfg->ioa_is_dead = 1;
5299
5300                 if (ioa_cfg->in_ioa_bringdown) {
5301                         ioa_cfg->reset_cmd = NULL;
5302                         ioa_cfg->in_reset_reload = 0;
5303                         ipr_fail_all_ops(ioa_cfg);
5304                         wake_up_all(&ioa_cfg->reset_wait_q);
5305
5306                         spin_unlock_irq(ioa_cfg->host->host_lock);
5307                         scsi_unblock_requests(ioa_cfg->host);
5308                         spin_lock_irq(ioa_cfg->host->host_lock);
5309                         return;
5310                 } else {
5311                         ioa_cfg->in_ioa_bringdown = 1;
5312                         shutdown_type = IPR_SHUTDOWN_NONE;
5313                 }
5314         }
5315
5316         _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_shutdown_ioa,
5317                                 shutdown_type);
5318 }
5319
5320 /**
5321  * ipr_probe_ioa_part2 - Initializes IOAs found in ipr_probe_ioa(..)
5322  * @ioa_cfg:    ioa cfg struct
5323  *
5324  * Description: This is the second phase of adapter intialization
5325  * This function takes care of initilizing the adapter to the point
5326  * where it can accept new commands.
5327
5328  * Return value:
5329  *      0 on sucess / -EIO on failure
5330  **/
5331 static int __devinit ipr_probe_ioa_part2(struct ipr_ioa_cfg *ioa_cfg)
5332 {
5333         int rc = 0;
5334         unsigned long host_lock_flags = 0;
5335
5336         ENTER;
5337         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
5338         dev_dbg(&ioa_cfg->pdev->dev, "ioa_cfg adx: 0x%p\n", ioa_cfg);
5339         _ipr_initiate_ioa_reset(ioa_cfg, ipr_reset_enable_ioa, IPR_SHUTDOWN_NONE);
5340
5341         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
5342         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5343         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
5344
5345         if (ioa_cfg->ioa_is_dead) {
5346                 rc = -EIO;
5347         } else if (ipr_invalid_adapter(ioa_cfg)) {
5348                 if (!ipr_testmode)
5349                         rc = -EIO;
5350
5351                 dev_err(&ioa_cfg->pdev->dev,
5352                         "Adapter not supported in this hardware configuration.\n");
5353         }
5354
5355         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
5356
5357         LEAVE;
5358         return rc;
5359 }
5360
5361 /**
5362  * ipr_free_cmd_blks - Frees command blocks allocated for an adapter
5363  * @ioa_cfg:    ioa config struct
5364  *
5365  * Return value:
5366  *      none
5367  **/
5368 static void ipr_free_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
5369 {
5370         int i;
5371
5372         for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
5373                 if (ioa_cfg->ipr_cmnd_list[i])
5374                         pci_pool_free(ioa_cfg->ipr_cmd_pool,
5375                                       ioa_cfg->ipr_cmnd_list[i],
5376                                       ioa_cfg->ipr_cmnd_list_dma[i]);
5377
5378                 ioa_cfg->ipr_cmnd_list[i] = NULL;
5379         }
5380
5381         if (ioa_cfg->ipr_cmd_pool)
5382                 pci_pool_destroy (ioa_cfg->ipr_cmd_pool);
5383
5384         ioa_cfg->ipr_cmd_pool = NULL;
5385 }
5386
5387 /**
5388  * ipr_free_mem - Frees memory allocated for an adapter
5389  * @ioa_cfg:    ioa cfg struct
5390  *
5391  * Return value:
5392  *      nothing
5393  **/
5394 static void ipr_free_mem(struct ipr_ioa_cfg *ioa_cfg)
5395 {
5396         int i;
5397
5398         kfree(ioa_cfg->res_entries);
5399         pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_misc_cbs),
5400                             ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
5401         ipr_free_cmd_blks(ioa_cfg);
5402         pci_free_consistent(ioa_cfg->pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
5403                             ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
5404         pci_free_consistent(ioa_cfg->pdev, sizeof(struct ipr_config_table),
5405                             ioa_cfg->cfg_table,
5406                             ioa_cfg->cfg_table_dma);
5407
5408         for (i = 0; i < IPR_NUM_HCAMS; i++) {
5409                 pci_free_consistent(ioa_cfg->pdev,
5410                                     sizeof(struct ipr_hostrcb),
5411                                     ioa_cfg->hostrcb[i],
5412                                     ioa_cfg->hostrcb_dma[i]);
5413         }
5414
5415         ipr_free_dump(ioa_cfg);
5416         kfree(ioa_cfg->saved_mode_pages);
5417         kfree(ioa_cfg->trace);
5418 }
5419
5420 /**
5421  * ipr_free_all_resources - Free all allocated resources for an adapter.
5422  * @ipr_cmd:    ipr command struct
5423  *
5424  * This function frees all allocated resources for the
5425  * specified adapter.
5426  *
5427  * Return value:
5428  *      none
5429  **/
5430 static void ipr_free_all_resources(struct ipr_ioa_cfg *ioa_cfg)
5431 {
5432         struct pci_dev *pdev = ioa_cfg->pdev;
5433
5434         ENTER;
5435         free_irq(pdev->irq, ioa_cfg);
5436         iounmap((void *) ioa_cfg->hdw_dma_regs);
5437         pci_release_regions(pdev);
5438         ipr_free_mem(ioa_cfg);
5439         scsi_host_put(ioa_cfg->host);
5440         pci_disable_device(pdev);
5441         LEAVE;
5442 }
5443
5444 /**
5445  * ipr_alloc_cmd_blks - Allocate command blocks for an adapter
5446  * @ioa_cfg:    ioa config struct
5447  *
5448  * Return value:
5449  *      0 on success / -ENOMEM on allocation failure
5450  **/
5451 static int __devinit ipr_alloc_cmd_blks(struct ipr_ioa_cfg *ioa_cfg)
5452 {
5453         struct ipr_cmnd *ipr_cmd;
5454         struct ipr_ioarcb *ioarcb;
5455         dma_addr_t dma_addr;
5456         int i;
5457
5458         ioa_cfg->ipr_cmd_pool = pci_pool_create (IPR_NAME, ioa_cfg->pdev,
5459                                                  sizeof(struct ipr_cmnd), 8, 0);
5460
5461         if (!ioa_cfg->ipr_cmd_pool)
5462                 return -ENOMEM;
5463
5464         for (i = 0; i < IPR_NUM_CMD_BLKS; i++) {
5465                 ipr_cmd = pci_pool_alloc (ioa_cfg->ipr_cmd_pool, SLAB_KERNEL, &dma_addr);
5466
5467                 if (!ipr_cmd) {
5468                         ipr_free_cmd_blks(ioa_cfg);
5469                         return -ENOMEM;
5470                 }
5471
5472                 memset(ipr_cmd, 0, sizeof(*ipr_cmd));
5473                 ioa_cfg->ipr_cmnd_list[i] = ipr_cmd;
5474                 ioa_cfg->ipr_cmnd_list_dma[i] = dma_addr;
5475
5476                 ioarcb = &ipr_cmd->ioarcb;
5477                 ioarcb->ioarcb_host_pci_addr = cpu_to_be32(dma_addr);
5478                 ioarcb->host_response_handle = cpu_to_be32(i << 2);
5479                 ioarcb->write_ioadl_addr =
5480                         cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioadl));
5481                 ioarcb->read_ioadl_addr = ioarcb->write_ioadl_addr;
5482                 ioarcb->ioasa_host_pci_addr =
5483                         cpu_to_be32(dma_addr + offsetof(struct ipr_cmnd, ioasa));
5484                 ioarcb->ioasa_len = cpu_to_be16(sizeof(struct ipr_ioasa));
5485                 ipr_cmd->cmd_index = i;
5486                 ipr_cmd->ioa_cfg = ioa_cfg;
5487                 ipr_cmd->sense_buffer_dma = dma_addr +
5488                         offsetof(struct ipr_cmnd, sense_buffer);
5489
5490                 list_add_tail(&ipr_cmd->queue, &ioa_cfg->free_q);
5491         }
5492
5493         return 0;
5494 }
5495
5496 /**
5497  * ipr_alloc_mem - Allocate memory for an adapter
5498  * @ioa_cfg:    ioa config struct
5499  *
5500  * Return value:
5501  *      0 on success / non-zero for error
5502  **/
5503 static int __devinit ipr_alloc_mem(struct ipr_ioa_cfg *ioa_cfg)
5504 {
5505         struct pci_dev *pdev = ioa_cfg->pdev;
5506         int i, rc = -ENOMEM;
5507
5508         ENTER;
5509         ioa_cfg->res_entries = kmalloc(sizeof(struct ipr_resource_entry) *
5510                                        IPR_MAX_PHYSICAL_DEVS, GFP_KERNEL);
5511
5512         if (!ioa_cfg->res_entries)
5513                 goto out;
5514
5515         memset(ioa_cfg->res_entries, 0,
5516                sizeof(struct ipr_resource_entry) * IPR_MAX_PHYSICAL_DEVS);
5517
5518         for (i = 0; i < IPR_MAX_PHYSICAL_DEVS; i++)
5519                 list_add_tail(&ioa_cfg->res_entries[i].queue, &ioa_cfg->free_res_q);
5520
5521         ioa_cfg->vpd_cbs = pci_alloc_consistent(ioa_cfg->pdev,
5522                                                 sizeof(struct ipr_misc_cbs),
5523                                                 &ioa_cfg->vpd_cbs_dma);
5524
5525         if (!ioa_cfg->vpd_cbs)
5526                 goto out_free_res_entries;
5527
5528         if (ipr_alloc_cmd_blks(ioa_cfg))
5529                 goto out_free_vpd_cbs;
5530
5531         ioa_cfg->host_rrq = pci_alloc_consistent(ioa_cfg->pdev,
5532                                                  sizeof(u32) * IPR_NUM_CMD_BLKS,
5533                                                  &ioa_cfg->host_rrq_dma);
5534
5535         if (!ioa_cfg->host_rrq)
5536                 goto out_ipr_free_cmd_blocks;
5537
5538         ioa_cfg->cfg_table = pci_alloc_consistent(ioa_cfg->pdev,
5539                                                   sizeof(struct ipr_config_table),
5540                                                   &ioa_cfg->cfg_table_dma);
5541
5542         if (!ioa_cfg->cfg_table)
5543                 goto out_free_host_rrq;
5544
5545         for (i = 0; i < IPR_NUM_HCAMS; i++) {
5546                 ioa_cfg->hostrcb[i] = pci_alloc_consistent(ioa_cfg->pdev,
5547                                                            sizeof(struct ipr_hostrcb),
5548                                                            &ioa_cfg->hostrcb_dma[i]);
5549
5550                 if (!ioa_cfg->hostrcb[i])
5551                         goto out_free_hostrcb_dma;
5552
5553                 ioa_cfg->hostrcb[i]->hostrcb_dma =
5554                         ioa_cfg->hostrcb_dma[i] + offsetof(struct ipr_hostrcb, hcam);
5555                 list_add_tail(&ioa_cfg->hostrcb[i]->queue, &ioa_cfg->hostrcb_free_q);
5556         }
5557
5558         ioa_cfg->trace = kmalloc(sizeof(struct ipr_trace_entry) *
5559                                  IPR_NUM_TRACE_ENTRIES, GFP_KERNEL);
5560
5561         if (!ioa_cfg->trace)
5562                 goto out_free_hostrcb_dma;
5563
5564         memset(ioa_cfg->trace, 0,
5565                sizeof(struct ipr_trace_entry) * IPR_NUM_TRACE_ENTRIES);
5566
5567         rc = 0;
5568 out:
5569         LEAVE;
5570         return rc;
5571
5572 out_free_hostrcb_dma:
5573         while (i-- > 0) {
5574                 pci_free_consistent(pdev, sizeof(struct ipr_hostrcb),
5575                                     ioa_cfg->hostrcb[i],
5576                                     ioa_cfg->hostrcb_dma[i]);
5577         }
5578         pci_free_consistent(pdev, sizeof(struct ipr_config_table),
5579                             ioa_cfg->cfg_table, ioa_cfg->cfg_table_dma);
5580 out_free_host_rrq:
5581         pci_free_consistent(pdev, sizeof(u32) * IPR_NUM_CMD_BLKS,
5582                             ioa_cfg->host_rrq, ioa_cfg->host_rrq_dma);
5583 out_ipr_free_cmd_blocks:
5584         ipr_free_cmd_blks(ioa_cfg);
5585 out_free_vpd_cbs:
5586         pci_free_consistent(pdev, sizeof(struct ipr_misc_cbs),
5587                             ioa_cfg->vpd_cbs, ioa_cfg->vpd_cbs_dma);
5588 out_free_res_entries:
5589         kfree(ioa_cfg->res_entries);
5590         goto out;
5591 }
5592
5593 /**
5594  * ipr_initialize_bus_attr - Initialize SCSI bus attributes to default values
5595  * @ioa_cfg:    ioa config struct
5596  *
5597  * Return value:
5598  *      none
5599  **/
5600 static void __devinit ipr_initialize_bus_attr(struct ipr_ioa_cfg *ioa_cfg)
5601 {
5602         int i;
5603
5604         for (i = 0; i < IPR_MAX_NUM_BUSES; i++) {
5605                 ioa_cfg->bus_attr[i].bus = i;
5606                 ioa_cfg->bus_attr[i].qas_enabled = 0;
5607                 ioa_cfg->bus_attr[i].bus_width = IPR_DEFAULT_BUS_WIDTH;
5608                 if (ipr_max_speed < ARRAY_SIZE(ipr_max_bus_speeds))
5609                         ioa_cfg->bus_attr[i].max_xfer_rate = ipr_max_bus_speeds[ipr_max_speed];
5610                 else
5611                         ioa_cfg->bus_attr[i].max_xfer_rate = IPR_U160_SCSI_RATE;
5612         }
5613 }
5614
5615 /**
5616  * ipr_init_ioa_cfg - Initialize IOA config struct
5617  * @ioa_cfg:    ioa config struct
5618  * @host:               scsi host struct
5619  * @pdev:               PCI dev struct
5620  *
5621  * Return value:
5622  *      none
5623  **/
5624 static void __devinit ipr_init_ioa_cfg(struct ipr_ioa_cfg *ioa_cfg,
5625                                        struct Scsi_Host *host, struct pci_dev *pdev)
5626 {
5627         ioa_cfg->host = host;
5628         ioa_cfg->pdev = pdev;
5629         ioa_cfg->log_level = ipr_log_level;
5630         sprintf(ioa_cfg->eye_catcher, IPR_EYECATCHER);
5631         sprintf(ioa_cfg->trace_start, IPR_TRACE_START_LABEL);
5632         sprintf(ioa_cfg->ipr_free_label, IPR_FREEQ_LABEL);
5633         sprintf(ioa_cfg->ipr_pending_label, IPR_PENDQ_LABEL);
5634         sprintf(ioa_cfg->cfg_table_start, IPR_CFG_TBL_START);
5635         sprintf(ioa_cfg->resource_table_label, IPR_RES_TABLE_LABEL);
5636         sprintf(ioa_cfg->ipr_hcam_label, IPR_HCAM_LABEL);
5637         sprintf(ioa_cfg->ipr_cmd_label, IPR_CMD_LABEL);
5638
5639         INIT_LIST_HEAD(&ioa_cfg->free_q);
5640         INIT_LIST_HEAD(&ioa_cfg->pending_q);
5641         INIT_LIST_HEAD(&ioa_cfg->hostrcb_free_q);
5642         INIT_LIST_HEAD(&ioa_cfg->hostrcb_pending_q);
5643         INIT_LIST_HEAD(&ioa_cfg->free_res_q);
5644         INIT_LIST_HEAD(&ioa_cfg->used_res_q);
5645         INIT_WORK(&ioa_cfg->work_q, ipr_worker_thread, ioa_cfg);
5646         init_waitqueue_head(&ioa_cfg->reset_wait_q);
5647         ioa_cfg->sdt_state = INACTIVE;
5648
5649         ipr_initialize_bus_attr(ioa_cfg);
5650
5651         host->max_id = IPR_MAX_NUM_TARGETS_PER_BUS;
5652         host->max_lun = IPR_MAX_NUM_LUNS_PER_TARGET;
5653         host->max_channel = IPR_MAX_BUS_TO_SCAN;
5654         host->unique_id = host->host_no;
5655         host->max_cmd_len = IPR_MAX_CDB_LEN;
5656         pci_set_drvdata(pdev, ioa_cfg);
5657
5658         memcpy(&ioa_cfg->regs, &ioa_cfg->chip_cfg->regs, sizeof(ioa_cfg->regs));
5659
5660         ioa_cfg->regs.set_interrupt_mask_reg += ioa_cfg->hdw_dma_regs;
5661         ioa_cfg->regs.clr_interrupt_mask_reg += ioa_cfg->hdw_dma_regs;
5662         ioa_cfg->regs.sense_interrupt_mask_reg += ioa_cfg->hdw_dma_regs;
5663         ioa_cfg->regs.clr_interrupt_reg += ioa_cfg->hdw_dma_regs;
5664         ioa_cfg->regs.sense_interrupt_reg += ioa_cfg->hdw_dma_regs;
5665         ioa_cfg->regs.ioarrin_reg += ioa_cfg->hdw_dma_regs;
5666         ioa_cfg->regs.sense_uproc_interrupt_reg += ioa_cfg->hdw_dma_regs;
5667         ioa_cfg->regs.set_uproc_interrupt_reg += ioa_cfg->hdw_dma_regs;
5668         ioa_cfg->regs.clr_uproc_interrupt_reg += ioa_cfg->hdw_dma_regs;
5669 }
5670
5671 /**
5672  * ipr_probe_ioa - Allocates memory and does first stage of initialization
5673  * @pdev:               PCI device struct
5674  * @dev_id:             PCI device id struct
5675  *
5676  * Return value:
5677  *      0 on success / non-zero on failure
5678  **/
5679 static int __devinit ipr_probe_ioa(struct pci_dev *pdev,
5680                                    const struct pci_device_id *dev_id)
5681 {
5682         struct ipr_ioa_cfg *ioa_cfg;
5683         struct Scsi_Host *host;
5684         unsigned long ipr_regs, ipr_regs_pci;
5685         u32 rc = PCIBIOS_SUCCESSFUL;
5686
5687         ENTER;
5688
5689         if ((rc = pci_enable_device(pdev))) {
5690                 dev_err(&pdev->dev, "Cannot enable adapter\n");
5691                 goto out;
5692         }
5693
5694         dev_info(&pdev->dev, "Found IOA with IRQ: %d\n", pdev->irq);
5695
5696         host = scsi_host_alloc(&driver_template, sizeof(*ioa_cfg));
5697
5698         if (!host) {
5699                 dev_err(&pdev->dev, "call to scsi_host_alloc failed!\n");
5700                 rc = -ENOMEM;
5701                 goto out_disable;
5702         }
5703
5704         ioa_cfg = (struct ipr_ioa_cfg *)host->hostdata;
5705         memset(ioa_cfg, 0, sizeof(struct ipr_ioa_cfg));
5706
5707         ioa_cfg->chip_cfg = (const struct ipr_chip_cfg_t *)dev_id->driver_data;
5708
5709         ipr_regs_pci = pci_resource_start(pdev, 0);
5710
5711         rc = pci_request_regions(pdev, IPR_NAME);
5712         if (rc < 0) {
5713                 dev_err(&pdev->dev,
5714                         "Couldn't register memory range of registers\n");
5715                 goto out_scsi_host_put;
5716         }
5717
5718         ipr_regs = (unsigned long)ioremap(ipr_regs_pci,
5719                                           pci_resource_len(pdev, 0));
5720
5721         if (!ipr_regs) {
5722                 dev_err(&pdev->dev,
5723                         "Couldn't map memory range of registers\n");
5724                 rc = -ENOMEM;
5725                 goto out_release_regions;
5726         }
5727
5728         ioa_cfg->hdw_dma_regs = ipr_regs;
5729         ioa_cfg->hdw_dma_regs_pci = ipr_regs_pci;
5730         ioa_cfg->ioa_mailbox = ioa_cfg->chip_cfg->mailbox + ipr_regs;
5731
5732         ipr_init_ioa_cfg(ioa_cfg, host, pdev);
5733
5734         pci_set_master(pdev);
5735
5736         rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
5737         if (rc < 0) {
5738                 dev_err(&pdev->dev, "Failed to set PCI DMA mask\n");
5739                 goto cleanup_nomem;
5740         }
5741
5742         rc = pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
5743                                    ioa_cfg->chip_cfg->cache_line_size);
5744
5745         if (rc != PCIBIOS_SUCCESSFUL) {
5746                 dev_err(&pdev->dev, "Write of cache line size failed\n");
5747                 rc = -EIO;
5748                 goto cleanup_nomem;
5749         }
5750
5751         /* Save away PCI config space for use following IOA reset */
5752         rc = pci_save_state(pdev, ioa_cfg->pci_cfg_buf);
5753
5754         if (rc != PCIBIOS_SUCCESSFUL) {
5755                 dev_err(&pdev->dev, "Failed to save PCI config space\n");
5756                 rc = -EIO;
5757                 goto cleanup_nomem;
5758         }
5759
5760         if ((rc = ipr_save_pcix_cmd_reg(ioa_cfg)))
5761                 goto cleanup_nomem;
5762
5763         if ((rc = ipr_set_pcix_cmd_reg(ioa_cfg)))
5764                 goto cleanup_nomem;
5765
5766         rc = ipr_alloc_mem(ioa_cfg);
5767         if (rc < 0) {
5768                 dev_err(&pdev->dev,
5769                         "Couldn't allocate enough memory for device driver!\n");
5770                 goto cleanup_nomem;
5771         }
5772
5773         ipr_mask_and_clear_interrupts(ioa_cfg, ~IPR_PCII_IOA_TRANS_TO_OPER);
5774         rc = request_irq(pdev->irq, ipr_isr, SA_SHIRQ, IPR_NAME, ioa_cfg);
5775
5776         if (rc) {
5777                 dev_err(&pdev->dev, "Couldn't register IRQ %d! rc=%d\n",
5778                         pdev->irq, rc);
5779                 goto cleanup_nolog;
5780         }
5781
5782         spin_lock(&ipr_driver_lock);
5783         list_add_tail(&ioa_cfg->queue, &ipr_ioa_head);
5784         spin_unlock(&ipr_driver_lock);
5785
5786         LEAVE;
5787 out:
5788         return rc;
5789
5790 cleanup_nolog:
5791         ipr_free_mem(ioa_cfg);
5792 cleanup_nomem:
5793         iounmap((void *) ipr_regs);
5794 out_release_regions:
5795         pci_release_regions(pdev);
5796 out_scsi_host_put:
5797         scsi_host_put(host);
5798 out_disable:
5799         pci_disable_device(pdev);
5800         goto out;
5801 }
5802
5803 /**
5804  * ipr_scan_vsets - Scans for VSET devices
5805  * @ioa_cfg:    ioa config struct
5806  *
5807  * Description: Since the VSET resources do not follow SAM in that we can have
5808  * sparse LUNs with no LUN 0, we have to scan for these ourselves.
5809  *
5810  * Return value:
5811  *      none
5812  **/
5813 static void ipr_scan_vsets(struct ipr_ioa_cfg *ioa_cfg)
5814 {
5815         int target, lun;
5816
5817         for (target = 0; target < IPR_MAX_NUM_TARGETS_PER_BUS; target++)
5818                 for (lun = 0; lun < IPR_MAX_NUM_VSET_LUNS_PER_TARGET; lun++ )
5819                         scsi_add_device(ioa_cfg->host, IPR_VSET_BUS, target, lun);
5820 }
5821
5822 /**
5823  * ipr_initiate_ioa_bringdown - Bring down an adapter
5824  * @ioa_cfg:            ioa config struct
5825  * @shutdown_type:      shutdown type
5826  *
5827  * Description: This function will initiate bringing down the adapter.
5828  * This consists of issuing an IOA shutdown to the adapter
5829  * to flush the cache, and running BIST.
5830  * If the caller needs to wait on the completion of the reset,
5831  * the caller must sleep on the reset_wait_q.
5832  *
5833  * Return value:
5834  *      none
5835  **/
5836 static void ipr_initiate_ioa_bringdown(struct ipr_ioa_cfg *ioa_cfg,
5837                                        enum ipr_shutdown_type shutdown_type)
5838 {
5839         ENTER;
5840         if (ioa_cfg->sdt_state == WAIT_FOR_DUMP)
5841                 ioa_cfg->sdt_state = ABORT_DUMP;
5842         ioa_cfg->reset_retries = 0;
5843         ioa_cfg->in_ioa_bringdown = 1;
5844         ipr_initiate_ioa_reset(ioa_cfg, shutdown_type);
5845         LEAVE;
5846 }
5847
5848 /**
5849  * __ipr_remove - Remove a single adapter
5850  * @pdev:       pci device struct
5851  *
5852  * Adapter hot plug remove entry point.
5853  *
5854  * Return value:
5855  *      none
5856  **/
5857 static void __ipr_remove(struct pci_dev *pdev)
5858 {
5859         unsigned long host_lock_flags = 0;
5860         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
5861         ENTER;
5862
5863         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
5864         ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
5865
5866         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
5867         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5868         spin_lock_irqsave(ioa_cfg->host->host_lock, host_lock_flags);
5869
5870         spin_lock(&ipr_driver_lock);
5871         list_del(&ioa_cfg->queue);
5872         spin_unlock(&ipr_driver_lock);
5873
5874         if (ioa_cfg->sdt_state == ABORT_DUMP)
5875                 ioa_cfg->sdt_state = WAIT_FOR_DUMP;
5876         spin_unlock_irqrestore(ioa_cfg->host->host_lock, host_lock_flags);
5877
5878         ipr_free_all_resources(ioa_cfg);
5879
5880         LEAVE;
5881 }
5882
5883 /**
5884  * ipr_remove - IOA hot plug remove entry point
5885  * @pdev:       pci device struct
5886  *
5887  * Adapter hot plug remove entry point.
5888  *
5889  * Return value:
5890  *      none
5891  **/
5892 static void ipr_remove(struct pci_dev *pdev)
5893 {
5894         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(pdev);
5895
5896         ENTER;
5897
5898         ioa_cfg->allow_cmds = 0;
5899         flush_scheduled_work();
5900         ipr_remove_trace_file(&ioa_cfg->host->shost_classdev.kobj,
5901                               &ipr_trace_attr);
5902         ipr_remove_dump_file(&ioa_cfg->host->shost_classdev.kobj,
5903                              &ipr_dump_attr);
5904         scsi_remove_host(ioa_cfg->host);
5905
5906         __ipr_remove(pdev);
5907
5908         LEAVE;
5909 }
5910
5911 /**
5912  * ipr_probe - Adapter hot plug add entry point
5913  *
5914  * Return value:
5915  *      0 on success / non-zero on failure
5916  **/
5917 static int __devinit ipr_probe(struct pci_dev *pdev,
5918                                const struct pci_device_id *dev_id)
5919 {
5920         struct ipr_ioa_cfg *ioa_cfg;
5921         int rc;
5922
5923         rc = ipr_probe_ioa(pdev, dev_id);
5924
5925         if (rc)
5926                 return rc;
5927
5928         ioa_cfg = pci_get_drvdata(pdev);
5929         rc = ipr_probe_ioa_part2(ioa_cfg);
5930
5931         if (rc) {
5932                 __ipr_remove(pdev);
5933                 return rc;
5934         }
5935
5936         rc = scsi_add_host(ioa_cfg->host, &pdev->dev);
5937
5938         if (rc) {
5939                 __ipr_remove(pdev);
5940                 return rc;
5941         }
5942
5943         rc = ipr_create_trace_file(&ioa_cfg->host->shost_classdev.kobj,
5944                                    &ipr_trace_attr);
5945
5946         if (rc) {
5947                 scsi_remove_host(ioa_cfg->host);
5948                 __ipr_remove(pdev);
5949                 return rc;
5950         }
5951
5952         rc = ipr_create_dump_file(&ioa_cfg->host->shost_classdev.kobj,
5953                                    &ipr_dump_attr);
5954
5955         if (rc) {
5956                 ipr_remove_trace_file(&ioa_cfg->host->shost_classdev.kobj,
5957                                       &ipr_trace_attr);
5958                 scsi_remove_host(ioa_cfg->host);
5959                 __ipr_remove(pdev);
5960                 return rc;
5961         }
5962
5963         scsi_scan_host(ioa_cfg->host);
5964         ipr_scan_vsets(ioa_cfg);
5965         scsi_add_device(ioa_cfg->host, IPR_IOA_BUS, IPR_IOA_TARGET, IPR_IOA_LUN);
5966         ioa_cfg->allow_ml_add_del = 1;
5967         schedule_work(&ioa_cfg->work_q);
5968         return 0;
5969 }
5970
5971 /**
5972  * ipr_shutdown - Shutdown handler.
5973  * @dev:        device struct
5974  *
5975  * This function is invoked upon system shutdown/reboot. It will issue
5976  * an adapter shutdown to the adapter to flush the write cache.
5977  *
5978  * Return value:
5979  *      none
5980  **/
5981 static void ipr_shutdown(struct device *dev)
5982 {
5983         struct ipr_ioa_cfg *ioa_cfg = pci_get_drvdata(to_pci_dev(dev));
5984         unsigned long lock_flags = 0;
5985
5986         spin_lock_irqsave(ioa_cfg->host->host_lock, lock_flags);
5987         ipr_initiate_ioa_bringdown(ioa_cfg, IPR_SHUTDOWN_NORMAL);
5988         spin_unlock_irqrestore(ioa_cfg->host->host_lock, lock_flags);
5989         wait_event(ioa_cfg->reset_wait_q, !ioa_cfg->in_reset_reload);
5990 }
5991
5992 static struct pci_device_id ipr_pci_table[] __devinitdata = {
5993         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
5994                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5702,
5995                 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
5996         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
5997                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_572E,
5998                 0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
5999         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6000                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_5703,
6001               0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6002         { PCI_VENDOR_ID_MYLEX, PCI_DEVICE_ID_IBM_GEMSTONE,
6003                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_573D,
6004               0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6005         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_CITRINE,
6006                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_571B,
6007               0, 0, (kernel_ulong_t)&ipr_chip_cfg[0] },
6008         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE,
6009                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_2780,
6010                 0, 0, (kernel_ulong_t)&ipr_chip_cfg[1] },
6011         { PCI_VENDOR_ID_IBM, PCI_DEVICE_ID_IBM_SNIPE,
6012                 PCI_VENDOR_ID_IBM, IPR_SUBS_DEV_ID_570F,
6013                 0, 0, (kernel_ulong_t)&ipr_chip_cfg[1] },
6014         { }
6015 };
6016 MODULE_DEVICE_TABLE(pci, ipr_pci_table);
6017
6018 static struct pci_driver ipr_driver = {
6019         .name = IPR_NAME,
6020         .id_table = ipr_pci_table,
6021         .probe = ipr_probe,
6022         .remove = ipr_remove,
6023         .driver = {
6024                 .shutdown = ipr_shutdown,
6025         },
6026 };
6027
6028 /**
6029  * ipr_init - Module entry point
6030  *
6031  * Return value:
6032  *      0 on success / negative value on failure
6033  **/
6034 static int __init ipr_init(void)
6035 {
6036         ipr_info("IBM Power RAID SCSI Device Driver version: %s %s\n",
6037                  IPR_DRIVER_VERSION, IPR_DRIVER_DATE);
6038
6039         return pci_module_init(&ipr_driver);
6040 }
6041
6042 /**
6043  * ipr_exit - Module unload
6044  *
6045  * Module unload entry point.
6046  *
6047  * Return value:
6048  *      none
6049  **/
6050 static void __exit ipr_exit(void)
6051 {
6052         pci_unregister_driver(&ipr_driver);
6053 }
6054
6055 module_init(ipr_init);
6056 module_exit(ipr_exit);