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