e44365193fdf581d963734700d0c5510b967cc78
[linux-2.6.git] / drivers / message / fusion / mptspi.c
1 /*
2  *  linux/drivers/message/fusion/mptspi.c
3  *      For use with LSI PCI chip/adapter(s)
4  *      running LSI Fusion MPT (Message Passing Technology) firmware.
5  *
6  *  Copyright (c) 1999-2008 LSI Corporation
7  *  (mailto:DL-MPTFusionLinux@lsi.com)
8  *
9  */
10 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
11 /*
12     This program is free software; you can redistribute it and/or modify
13     it under the terms of the GNU General Public License as published by
14     the Free Software Foundation; version 2 of the License.
15
16     This program is distributed in the hope that it will be useful,
17     but WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19     GNU General Public License for more details.
20
21     NO WARRANTY
22     THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
23     CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
24     LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
25     MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
26     solely responsible for determining the appropriateness of using and
27     distributing the Program and assumes all risks associated with its
28     exercise of rights under this Agreement, including but not limited to
29     the risks and costs of program errors, damage to or loss of data,
30     programs or equipment, and unavailability or interruption of operations.
31
32     DISCLAIMER OF LIABILITY
33     NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
34     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35     DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
36     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
37     TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
38     USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
39     HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
40
41     You should have received a copy of the GNU General Public License
42     along with this program; if not, write to the Free Software
43     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
44 */
45 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
46
47 #include <linux/module.h>
48 #include <linux/kernel.h>
49 #include <linux/slab.h>
50 #include <linux/init.h>
51 #include <linux/errno.h>
52 #include <linux/kdev_t.h>
53 #include <linux/blkdev.h>
54 #include <linux/delay.h>        /* for mdelay */
55 #include <linux/interrupt.h>    /* needed for in_interrupt() proto */
56 #include <linux/reboot.h>       /* notifier code */
57 #include <linux/workqueue.h>
58 #include <linux/raid_class.h>
59
60 #include <scsi/scsi.h>
61 #include <scsi/scsi_cmnd.h>
62 #include <scsi/scsi_device.h>
63 #include <scsi/scsi_host.h>
64 #include <scsi/scsi_tcq.h>
65 #include <scsi/scsi_transport.h>
66 #include <scsi/scsi_transport_spi.h>
67 #include <scsi/scsi_dbg.h>
68
69 #include "mptbase.h"
70 #include "mptscsih.h"
71
72 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
73 #define my_NAME         "Fusion MPT SPI Host driver"
74 #define my_VERSION      MPT_LINUX_VERSION_COMMON
75 #define MYNAM           "mptspi"
76
77 MODULE_AUTHOR(MODULEAUTHOR);
78 MODULE_DESCRIPTION(my_NAME);
79 MODULE_LICENSE("GPL");
80 MODULE_VERSION(my_VERSION);
81
82 /* Command line args */
83 static int mpt_saf_te = MPTSCSIH_SAF_TE;
84 module_param(mpt_saf_te, int, 0);
85 MODULE_PARM_DESC(mpt_saf_te, " Force enabling SEP Processor: enable=1  (default=MPTSCSIH_SAF_TE=0)");
86
87 static void mptspi_write_offset(struct scsi_target *, int);
88 static void mptspi_write_width(struct scsi_target *, int);
89 static int mptspi_write_spi_device_pg1(struct scsi_target *,
90                                        struct _CONFIG_PAGE_SCSI_DEVICE_1 *);
91
92 static struct scsi_transport_template *mptspi_transport_template = NULL;
93
94 static u8       mptspiDoneCtx = MPT_MAX_PROTOCOL_DRIVERS;
95 static u8       mptspiTaskCtx = MPT_MAX_PROTOCOL_DRIVERS;
96 static u8       mptspiInternalCtx = MPT_MAX_PROTOCOL_DRIVERS; /* Used only for internal commands */
97
98 /**
99  *      mptspi_setTargetNegoParms  - Update the target negotiation parameters
100  *      @hd: Pointer to a SCSI Host Structure
101  *      @target: per target private data
102  *      @sdev: SCSI device
103  *
104  *      Update the target negotiation parameters based on the the Inquiry
105  *      data, adapter capabilities, and NVRAM settings.
106  **/
107 static void
108 mptspi_setTargetNegoParms(MPT_SCSI_HOST *hd, VirtTarget *target,
109                             struct scsi_device *sdev)
110 {
111         MPT_ADAPTER *ioc = hd->ioc;
112         SpiCfgData *pspi_data = &ioc->spi_data;
113         int  id = (int) target->id;
114         int  nvram;
115         u8 width = MPT_NARROW;
116         u8 factor = MPT_ASYNC;
117         u8 offset = 0;
118         u8 nfactor;
119         u8 noQas = 1;
120
121         target->negoFlags = pspi_data->noQas;
122
123         if (sdev->scsi_level < SCSI_2) {
124                 width = 0;
125                 factor = MPT_ULTRA2;
126                 offset = pspi_data->maxSyncOffset;
127                 target->tflags &= ~MPT_TARGET_FLAGS_Q_YES;
128         } else {
129                 if (scsi_device_wide(sdev))
130                         width = 1;
131
132                 if (scsi_device_sync(sdev)) {
133                         factor = pspi_data->minSyncFactor;
134                         if (!scsi_device_dt(sdev))
135                                         factor = MPT_ULTRA2;
136                         else {
137                                 if (!scsi_device_ius(sdev) &&
138                                     !scsi_device_qas(sdev))
139                                         factor = MPT_ULTRA160;
140                                 else {
141                                         factor = MPT_ULTRA320;
142                                         if (scsi_device_qas(sdev)) {
143                                                 ddvprintk(ioc,
144                                                 printk(MYIOC_s_DEBUG_FMT "Enabling QAS due to "
145                                                 "byte56=%02x on id=%d!\n", ioc->name,
146                                                 scsi_device_qas(sdev), id));
147                                                 noQas = 0;
148                                         }
149                                         if (sdev->type == TYPE_TAPE &&
150                                             scsi_device_ius(sdev))
151                                                 target->negoFlags |= MPT_TAPE_NEGO_IDP;
152                                 }
153                         }
154                         offset = pspi_data->maxSyncOffset;
155
156                         /* If RAID, never disable QAS
157                          * else if non RAID, do not disable
158                          *   QAS if bit 1 is set
159                          * bit 1 QAS support, non-raid only
160                          * bit 0 IU support
161                          */
162                         if (target->raidVolume == 1)
163                                 noQas = 0;
164                 } else {
165                         factor = MPT_ASYNC;
166                         offset = 0;
167                 }
168         }
169
170         if (!sdev->tagged_supported)
171                 target->tflags &= ~MPT_TARGET_FLAGS_Q_YES;
172
173         /* Update tflags based on NVRAM settings. (SCSI only)
174          */
175         if (pspi_data->nvram && (pspi_data->nvram[id] != MPT_HOST_NVRAM_INVALID)) {
176                 nvram = pspi_data->nvram[id];
177                 nfactor = (nvram & MPT_NVRAM_SYNC_MASK) >> 8;
178
179                 if (width)
180                         width = nvram & MPT_NVRAM_WIDE_DISABLE ? 0 : 1;
181
182                 if (offset > 0) {
183                         /* Ensure factor is set to the
184                          * maximum of: adapter, nvram, inquiry
185                          */
186                         if (nfactor) {
187                                 if (nfactor < pspi_data->minSyncFactor )
188                                         nfactor = pspi_data->minSyncFactor;
189
190                                 factor = max(factor, nfactor);
191                                 if (factor == MPT_ASYNC)
192                                         offset = 0;
193                         } else {
194                                 offset = 0;
195                                 factor = MPT_ASYNC;
196                 }
197                 } else {
198                         factor = MPT_ASYNC;
199                 }
200         }
201
202         /* Make sure data is consistent
203          */
204         if ((!width) && (factor < MPT_ULTRA2))
205                 factor = MPT_ULTRA2;
206
207         /* Save the data to the target structure.
208          */
209         target->minSyncFactor = factor;
210         target->maxOffset = offset;
211         target->maxWidth = width;
212
213         target->tflags |= MPT_TARGET_FLAGS_VALID_NEGO;
214
215         /* Disable unused features.
216          */
217         if (!width)
218                 target->negoFlags |= MPT_TARGET_NO_NEGO_WIDE;
219
220         if (!offset)
221                 target->negoFlags |= MPT_TARGET_NO_NEGO_SYNC;
222
223         if ( factor > MPT_ULTRA320 )
224                 noQas = 0;
225
226         if (noQas && (pspi_data->noQas == 0)) {
227                 pspi_data->noQas |= MPT_TARGET_NO_NEGO_QAS;
228                 target->negoFlags |= MPT_TARGET_NO_NEGO_QAS;
229
230                 /* Disable QAS in a mixed configuration case
231                  */
232
233                 ddvprintk(ioc, printk(MYIOC_s_DEBUG_FMT
234                         "Disabling QAS due to noQas=%02x on id=%d!\n", ioc->name, noQas, id));
235         }
236 }
237
238 /**
239  *      mptspi_writeIOCPage4  - write IOC Page 4
240  *      @hd: Pointer to a SCSI Host Structure
241  *      @channel: channel number
242  *      @id: write IOC Page4 for this ID & Bus
243  *
244  *      Return: -EAGAIN if unable to obtain a Message Frame
245  *              or 0 if success.
246  *
247  *      Remark: We do not wait for a return, write pages sequentially.
248  **/
249 static int
250 mptspi_writeIOCPage4(MPT_SCSI_HOST *hd, u8 channel , u8 id)
251 {
252         MPT_ADAPTER             *ioc = hd->ioc;
253         Config_t                *pReq;
254         IOCPage4_t              *IOCPage4Ptr;
255         MPT_FRAME_HDR           *mf;
256         dma_addr_t               dataDma;
257         u16                      req_idx;
258         u32                      frameOffset;
259         u32                      flagsLength;
260         int                      ii;
261
262         /* Get a MF for this command.
263          */
264         if ((mf = mpt_get_msg_frame(ioc->DoneCtx, ioc)) == NULL) {
265                 dfailprintk(ioc, printk(MYIOC_s_WARN_FMT
266                                 "writeIOCPage4 : no msg frames!\n",ioc->name));
267                 return -EAGAIN;
268         }
269
270         /* Set the request and the data pointers.
271          * Place data at end of MF.
272          */
273         pReq = (Config_t *)mf;
274
275         req_idx = le16_to_cpu(mf->u.frame.hwhdr.msgctxu.fld.req_idx);
276         frameOffset = ioc->req_sz - sizeof(IOCPage4_t);
277
278         /* Complete the request frame (same for all requests).
279          */
280         pReq->Action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
281         pReq->Reserved = 0;
282         pReq->ChainOffset = 0;
283         pReq->Function = MPI_FUNCTION_CONFIG;
284         pReq->ExtPageLength = 0;
285         pReq->ExtPageType = 0;
286         pReq->MsgFlags = 0;
287         for (ii=0; ii < 8; ii++) {
288                 pReq->Reserved2[ii] = 0;
289         }
290
291         IOCPage4Ptr = ioc->spi_data.pIocPg4;
292         dataDma = ioc->spi_data.IocPg4_dma;
293         ii = IOCPage4Ptr->ActiveSEP++;
294         IOCPage4Ptr->SEP[ii].SEPTargetID = id;
295         IOCPage4Ptr->SEP[ii].SEPBus = channel;
296         pReq->Header = IOCPage4Ptr->Header;
297         pReq->PageAddress = cpu_to_le32(id | (channel << 8 ));
298
299         /* Add a SGE to the config request.
300          */
301         flagsLength = MPT_SGE_FLAGS_SSIMPLE_WRITE |
302                 (IOCPage4Ptr->Header.PageLength + ii) * 4;
303
304         ioc->add_sge((char *)&pReq->PageBufferSGE, flagsLength, dataDma);
305
306         ddvprintk(ioc, printk(MYIOC_s_DEBUG_FMT
307                 "writeIOCPage4: MaxSEP=%d ActiveSEP=%d id=%d bus=%d\n",
308                 ioc->name, IOCPage4Ptr->MaxSEP, IOCPage4Ptr->ActiveSEP, id, channel));
309
310         mpt_put_msg_frame(ioc->DoneCtx, ioc, mf);
311
312         return 0;
313 }
314
315 /**
316  *      mptspi_initTarget - Target, LUN alloc/free functionality.
317  *      @hd: Pointer to MPT_SCSI_HOST structure
318  *      @vtarget: per target private data
319  *      @sdev: SCSI device
320  *
321  *      NOTE: It's only SAFE to call this routine if data points to
322  *      sane & valid STANDARD INQUIRY data!
323  *
324  *      Allocate and initialize memory for this target.
325  *      Save inquiry data.
326  *
327  **/
328 static void
329 mptspi_initTarget(MPT_SCSI_HOST *hd, VirtTarget *vtarget,
330                     struct scsi_device *sdev)
331 {
332
333         /* Is LUN supported? If so, upper 2 bits will be 0
334         * in first byte of inquiry data.
335         */
336         if (sdev->inq_periph_qual != 0)
337                 return;
338
339         if (vtarget == NULL)
340                 return;
341
342         vtarget->type = sdev->type;
343
344         if ((sdev->type == TYPE_PROCESSOR) && (hd->ioc->spi_data.Saf_Te)) {
345                 /* Treat all Processors as SAF-TE if
346                  * command line option is set */
347                 vtarget->tflags |= MPT_TARGET_FLAGS_SAF_TE_ISSUED;
348                 mptspi_writeIOCPage4(hd, vtarget->channel, vtarget->id);
349         }else if ((sdev->type == TYPE_PROCESSOR) &&
350                 !(vtarget->tflags & MPT_TARGET_FLAGS_SAF_TE_ISSUED )) {
351                 if (sdev->inquiry_len > 49 ) {
352                         if (sdev->inquiry[44] == 'S' &&
353                             sdev->inquiry[45] == 'A' &&
354                             sdev->inquiry[46] == 'F' &&
355                             sdev->inquiry[47] == '-' &&
356                             sdev->inquiry[48] == 'T' &&
357                             sdev->inquiry[49] == 'E' ) {
358                                 vtarget->tflags |= MPT_TARGET_FLAGS_SAF_TE_ISSUED;
359                                 mptspi_writeIOCPage4(hd, vtarget->channel, vtarget->id);
360                         }
361                 }
362         }
363         mptspi_setTargetNegoParms(hd, vtarget, sdev);
364 }
365
366 /**
367  *      mptspi_is_raid - Determines whether target is belonging to volume
368  *      @hd: Pointer to a SCSI HOST structure
369  *      @id: target device id
370  *
371  *      Return:
372  *              non-zero = true
373  *              zero = false
374  *
375  */
376 static int
377 mptspi_is_raid(struct _MPT_SCSI_HOST *hd, u32 id)
378 {
379         int i, rc = 0;
380         MPT_ADAPTER *ioc = hd->ioc;
381
382         if (!ioc->raid_data.pIocPg2)
383                 goto out;
384
385         if (!ioc->raid_data.pIocPg2->NumActiveVolumes)
386                 goto out;
387         for (i=0; i < ioc->raid_data.pIocPg2->NumActiveVolumes; i++) {
388                 if (ioc->raid_data.pIocPg2->RaidVolume[i].VolumeID == id) {
389                         rc = 1;
390                         goto out;
391                 }
392         }
393
394  out:
395         return rc;
396 }
397
398 static int mptspi_target_alloc(struct scsi_target *starget)
399 {
400         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
401         struct _MPT_SCSI_HOST *hd = shost_priv(shost);
402         VirtTarget              *vtarget;
403         MPT_ADAPTER *ioc;
404
405         if (hd == NULL)
406                 return -ENODEV;
407
408         ioc = hd->ioc;
409         vtarget = kzalloc(sizeof(VirtTarget), GFP_KERNEL);
410         if (!vtarget)
411                 return -ENOMEM;
412
413         vtarget->ioc_id = ioc->id;
414         vtarget->tflags = MPT_TARGET_FLAGS_Q_YES;
415         vtarget->id = (u8)starget->id;
416         vtarget->channel = (u8)starget->channel;
417         vtarget->starget = starget;
418         starget->hostdata = vtarget;
419
420         if (starget->channel == 1) {
421                 if (mptscsih_is_phys_disk(ioc, 0, starget->id) == 0)
422                         return 0;
423                 vtarget->tflags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
424                 /* The real channel for this device is zero */
425                 vtarget->channel = 0;
426                 /* The actual physdisknum (for RAID passthrough) */
427                 vtarget->id = mptscsih_raid_id_to_num(ioc, 0,
428                     starget->id);
429         }
430
431         if (starget->channel == 0 &&
432             mptspi_is_raid(hd, starget->id)) {
433                 vtarget->raidVolume = 1;
434                 ddvprintk(ioc, printk(MYIOC_s_DEBUG_FMT
435                     "RAID Volume @ channel=%d id=%d\n", ioc->name, starget->channel,
436                     starget->id));
437         }
438
439         if (ioc->spi_data.nvram &&
440             ioc->spi_data.nvram[starget->id] != MPT_HOST_NVRAM_INVALID) {
441                 u32 nvram = ioc->spi_data.nvram[starget->id];
442                 spi_min_period(starget) = (nvram & MPT_NVRAM_SYNC_MASK) >> MPT_NVRAM_SYNC_SHIFT;
443                 spi_max_width(starget) = nvram & MPT_NVRAM_WIDE_DISABLE ? 0 : 1;
444         } else {
445                 spi_min_period(starget) = ioc->spi_data.minSyncFactor;
446                 spi_max_width(starget) = ioc->spi_data.maxBusWidth;
447         }
448         spi_max_offset(starget) = ioc->spi_data.maxSyncOffset;
449
450         spi_offset(starget) = 0;
451         spi_period(starget) = 0xFF;
452         mptspi_write_width(starget, 0);
453
454         return 0;
455 }
456
457 static void
458 mptspi_target_destroy(struct scsi_target *starget)
459 {
460         if (starget->hostdata)
461                 kfree(starget->hostdata);
462         starget->hostdata = NULL;
463 }
464
465 /**
466  *      mptspi_print_write_nego - negotiation parameters debug info that is being sent
467  *      @hd: Pointer to a SCSI HOST structure
468  *      @starget: SCSI target
469  *      @ii: negotiation parameters
470  *
471  */
472 static void
473 mptspi_print_write_nego(struct _MPT_SCSI_HOST *hd, struct scsi_target *starget, u32 ii)
474 {
475         ddvprintk(hd->ioc, printk(MYIOC_s_DEBUG_FMT "id=%d Requested = 0x%08x"
476             " ( %s factor = 0x%02x @ offset = 0x%02x %s%s%s%s%s%s%s%s)\n",
477             hd->ioc->name, starget->id, ii,
478             ii & MPI_SCSIDEVPAGE0_NP_WIDE ? "Wide ": "",
479             ((ii >> 8) & 0xFF), ((ii >> 16) & 0xFF),
480             ii & MPI_SCSIDEVPAGE0_NP_IU ? "IU ": "",
481             ii & MPI_SCSIDEVPAGE0_NP_DT ? "DT ": "",
482             ii & MPI_SCSIDEVPAGE0_NP_QAS ? "QAS ": "",
483             ii & MPI_SCSIDEVPAGE0_NP_HOLD_MCS ? "HOLDMCS ": "",
484             ii & MPI_SCSIDEVPAGE0_NP_WR_FLOW ? "WRFLOW ": "",
485             ii & MPI_SCSIDEVPAGE0_NP_RD_STRM ? "RDSTRM ": "",
486             ii & MPI_SCSIDEVPAGE0_NP_RTI ? "RTI ": "",
487             ii & MPI_SCSIDEVPAGE0_NP_PCOMP_EN ? "PCOMP ": ""));
488 }
489
490 /**
491  *      mptspi_print_read_nego - negotiation parameters debug info that is being read
492  *      @hd: Pointer to a SCSI HOST structure
493  *      @starget: SCSI target
494  *      @ii: negotiation parameters
495  *
496  */
497 static void
498 mptspi_print_read_nego(struct _MPT_SCSI_HOST *hd, struct scsi_target *starget, u32 ii)
499 {
500         ddvprintk(hd->ioc, printk(MYIOC_s_DEBUG_FMT "id=%d Read = 0x%08x"
501             " ( %s factor = 0x%02x @ offset = 0x%02x %s%s%s%s%s%s%s%s)\n",
502             hd->ioc->name, starget->id, ii,
503             ii & MPI_SCSIDEVPAGE0_NP_WIDE ? "Wide ": "",
504             ((ii >> 8) & 0xFF), ((ii >> 16) & 0xFF),
505             ii & MPI_SCSIDEVPAGE0_NP_IU ? "IU ": "",
506             ii & MPI_SCSIDEVPAGE0_NP_DT ? "DT ": "",
507             ii & MPI_SCSIDEVPAGE0_NP_QAS ? "QAS ": "",
508             ii & MPI_SCSIDEVPAGE0_NP_HOLD_MCS ? "HOLDMCS ": "",
509             ii & MPI_SCSIDEVPAGE0_NP_WR_FLOW ? "WRFLOW ": "",
510             ii & MPI_SCSIDEVPAGE0_NP_RD_STRM ? "RDSTRM ": "",
511             ii & MPI_SCSIDEVPAGE0_NP_RTI ? "RTI ": "",
512             ii & MPI_SCSIDEVPAGE0_NP_PCOMP_EN ? "PCOMP ": ""));
513 }
514
515 static int mptspi_read_spi_device_pg0(struct scsi_target *starget,
516                              struct _CONFIG_PAGE_SCSI_DEVICE_0 *pass_pg0)
517 {
518         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
519         struct _MPT_SCSI_HOST *hd = shost_priv(shost);
520         struct _MPT_ADAPTER *ioc = hd->ioc;
521         struct _CONFIG_PAGE_SCSI_DEVICE_0 *spi_dev_pg0;
522         dma_addr_t spi_dev_pg0_dma;
523         int size;
524         struct _x_config_parms cfg;
525         struct _CONFIG_PAGE_HEADER hdr;
526         int err = -EBUSY;
527
528         /* No SPI parameters for RAID devices */
529         if (starget->channel == 0 &&
530             mptspi_is_raid(hd, starget->id))
531                 return -1;
532
533         size = ioc->spi_data.sdp0length * 4;
534         /*
535         if (ioc->spi_data.sdp0length & 1)
536                 size += size + 4;
537         size += 2048;
538         */
539
540         spi_dev_pg0 = dma_alloc_coherent(&ioc->pcidev->dev, size, &spi_dev_pg0_dma, GFP_KERNEL);
541         if (spi_dev_pg0 == NULL) {
542                 starget_printk(KERN_ERR, starget, MYIOC_s_FMT
543                     "dma_alloc_coherent for parameters failed\n", ioc->name);
544                 return -EINVAL;
545         }
546
547         memset(&hdr, 0, sizeof(hdr));
548
549         hdr.PageVersion = ioc->spi_data.sdp0version;
550         hdr.PageLength = ioc->spi_data.sdp0length;
551         hdr.PageNumber = 0;
552         hdr.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
553
554         memset(&cfg, 0, sizeof(cfg));
555
556         cfg.cfghdr.hdr = &hdr;
557         cfg.physAddr = spi_dev_pg0_dma;
558         cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
559         cfg.dir = 0;
560         cfg.pageAddr = starget->id;
561
562         if (mpt_config(ioc, &cfg)) {
563                 starget_printk(KERN_ERR, starget, MYIOC_s_FMT "mpt_config failed\n", ioc->name);
564                 goto out_free;
565         }
566         err = 0;
567         memcpy(pass_pg0, spi_dev_pg0, size);
568
569         mptspi_print_read_nego(hd, starget, le32_to_cpu(spi_dev_pg0->NegotiatedParameters));
570
571  out_free:
572         dma_free_coherent(&ioc->pcidev->dev, size, spi_dev_pg0, spi_dev_pg0_dma);
573         return err;
574 }
575
576 static u32 mptspi_getRP(struct scsi_target *starget)
577 {
578         u32 nego = 0;
579
580         nego |= spi_iu(starget) ? MPI_SCSIDEVPAGE1_RP_IU : 0;
581         nego |= spi_dt(starget) ? MPI_SCSIDEVPAGE1_RP_DT : 0;
582         nego |= spi_qas(starget) ? MPI_SCSIDEVPAGE1_RP_QAS : 0;
583         nego |= spi_hold_mcs(starget) ? MPI_SCSIDEVPAGE1_RP_HOLD_MCS : 0;
584         nego |= spi_wr_flow(starget) ? MPI_SCSIDEVPAGE1_RP_WR_FLOW : 0;
585         nego |= spi_rd_strm(starget) ? MPI_SCSIDEVPAGE1_RP_RD_STRM : 0;
586         nego |= spi_rti(starget) ? MPI_SCSIDEVPAGE1_RP_RTI : 0;
587         nego |= spi_pcomp_en(starget) ? MPI_SCSIDEVPAGE1_RP_PCOMP_EN : 0;
588
589         nego |= (spi_period(starget) <<  MPI_SCSIDEVPAGE1_RP_SHIFT_MIN_SYNC_PERIOD) & MPI_SCSIDEVPAGE1_RP_MIN_SYNC_PERIOD_MASK;
590         nego |= (spi_offset(starget) << MPI_SCSIDEVPAGE1_RP_SHIFT_MAX_SYNC_OFFSET) & MPI_SCSIDEVPAGE1_RP_MAX_SYNC_OFFSET_MASK;
591         nego |= spi_width(starget) ?  MPI_SCSIDEVPAGE1_RP_WIDE : 0;
592
593         return nego;
594 }
595
596 static void mptspi_read_parameters(struct scsi_target *starget)
597 {
598         int nego;
599         struct _CONFIG_PAGE_SCSI_DEVICE_0 spi_dev_pg0;
600
601         mptspi_read_spi_device_pg0(starget, &spi_dev_pg0);
602
603         nego = le32_to_cpu(spi_dev_pg0.NegotiatedParameters);
604
605         spi_iu(starget) = (nego & MPI_SCSIDEVPAGE0_NP_IU) ? 1 : 0;
606         spi_dt(starget) = (nego & MPI_SCSIDEVPAGE0_NP_DT) ? 1 : 0;
607         spi_qas(starget) = (nego & MPI_SCSIDEVPAGE0_NP_QAS) ? 1 : 0;
608         spi_wr_flow(starget) = (nego & MPI_SCSIDEVPAGE0_NP_WR_FLOW) ? 1 : 0;
609         spi_rd_strm(starget) = (nego & MPI_SCSIDEVPAGE0_NP_RD_STRM) ? 1 : 0;
610         spi_rti(starget) = (nego & MPI_SCSIDEVPAGE0_NP_RTI) ? 1 : 0;
611         spi_pcomp_en(starget) = (nego & MPI_SCSIDEVPAGE0_NP_PCOMP_EN) ? 1 : 0;
612         spi_hold_mcs(starget) = (nego & MPI_SCSIDEVPAGE0_NP_HOLD_MCS) ? 1 : 0;
613         spi_period(starget) = (nego & MPI_SCSIDEVPAGE0_NP_NEG_SYNC_PERIOD_MASK) >> MPI_SCSIDEVPAGE0_NP_SHIFT_SYNC_PERIOD;
614         spi_offset(starget) = (nego & MPI_SCSIDEVPAGE0_NP_NEG_SYNC_OFFSET_MASK) >> MPI_SCSIDEVPAGE0_NP_SHIFT_SYNC_OFFSET;
615         spi_width(starget) = (nego & MPI_SCSIDEVPAGE0_NP_WIDE) ? 1 : 0;
616 }
617
618 int
619 mptscsih_quiesce_raid(MPT_SCSI_HOST *hd, int quiesce, u8 channel, u8 id)
620 {
621         MPT_ADAPTER     *ioc = hd->ioc;
622         MpiRaidActionRequest_t  *pReq;
623         MPT_FRAME_HDR           *mf;
624         int                     ret;
625         unsigned long           timeleft;
626
627         mutex_lock(&ioc->internal_cmds.mutex);
628
629         /* Get and Populate a free Frame
630          */
631         if ((mf = mpt_get_msg_frame(ioc->InternalCtx, ioc)) == NULL) {
632                 dfailprintk(hd->ioc, printk(MYIOC_s_WARN_FMT
633                         "%s: no msg frames!\n", ioc->name, __func__));
634                 ret = -EAGAIN;
635                 goto out;
636         }
637         pReq = (MpiRaidActionRequest_t *)mf;
638         if (quiesce)
639                 pReq->Action = MPI_RAID_ACTION_QUIESCE_PHYS_IO;
640         else
641                 pReq->Action = MPI_RAID_ACTION_ENABLE_PHYS_IO;
642         pReq->Reserved1 = 0;
643         pReq->ChainOffset = 0;
644         pReq->Function = MPI_FUNCTION_RAID_ACTION;
645         pReq->VolumeID = id;
646         pReq->VolumeBus = channel;
647         pReq->PhysDiskNum = 0;
648         pReq->MsgFlags = 0;
649         pReq->Reserved2 = 0;
650         pReq->ActionDataWord = 0; /* Reserved for this action */
651
652         ioc->add_sge((char *)&pReq->ActionDataSGE,
653                 MPT_SGE_FLAGS_SSIMPLE_READ | 0, (dma_addr_t) -1);
654
655         ddvprintk(ioc, printk(MYIOC_s_DEBUG_FMT "RAID Volume action=%x channel=%d id=%d\n",
656                         ioc->name, pReq->Action, channel, id));
657
658         INITIALIZE_MGMT_STATUS(ioc->internal_cmds.status)
659         mpt_put_msg_frame(ioc->InternalCtx, ioc, mf);
660         timeleft = wait_for_completion_timeout(&ioc->internal_cmds.done, 10*HZ);
661         if (!(ioc->internal_cmds.status & MPT_MGMT_STATUS_COMMAND_GOOD)) {
662                 ret = -ETIME;
663                 dfailprintk(ioc, printk(MYIOC_s_DEBUG_FMT "%s: TIMED OUT!\n",
664                     ioc->name, __func__));
665                 if (ioc->internal_cmds.status & MPT_MGMT_STATUS_DID_IOCRESET)
666                         goto out;
667                 if (!timeleft) {
668                         printk(MYIOC_s_WARN_FMT "Issuing Reset from %s!!\n",
669                             ioc->name, __func__);
670                         mpt_HardResetHandler(ioc, CAN_SLEEP);
671                         mpt_free_msg_frame(ioc, mf);
672                 }
673                 goto out;
674         }
675
676         ret = ioc->internal_cmds.completion_code;
677
678  out:
679         CLEAR_MGMT_STATUS(ioc->internal_cmds.status)
680         mutex_unlock(&ioc->internal_cmds.mutex);
681         return ret;
682 }
683
684 static void mptspi_dv_device(struct _MPT_SCSI_HOST *hd,
685                              struct scsi_device *sdev)
686 {
687         VirtTarget *vtarget = scsi_target(sdev)->hostdata;
688         MPT_ADAPTER *ioc = hd->ioc;
689
690         /* no DV on RAID devices */
691         if (sdev->channel == 0 &&
692             mptspi_is_raid(hd, sdev->id))
693                 return;
694
695         /* If this is a piece of a RAID, then quiesce first */
696         if (sdev->channel == 1 &&
697             mptscsih_quiesce_raid(hd, 1, vtarget->channel, vtarget->id) < 0) {
698                 starget_printk(KERN_ERR, scsi_target(sdev), MYIOC_s_FMT
699                     "Integrated RAID quiesce failed\n", ioc->name);
700                 return;
701         }
702
703         hd->spi_pending |= (1 << sdev->id);
704         spi_dv_device(sdev);
705         hd->spi_pending &= ~(1 << sdev->id);
706
707         if (sdev->channel == 1 &&
708             mptscsih_quiesce_raid(hd, 0, vtarget->channel, vtarget->id) < 0)
709                 starget_printk(KERN_ERR, scsi_target(sdev), MYIOC_s_FMT
710                     "Integrated RAID resume failed\n", ioc->name);
711
712         mptspi_read_parameters(sdev->sdev_target);
713         spi_display_xfer_agreement(sdev->sdev_target);
714         mptspi_read_parameters(sdev->sdev_target);
715 }
716
717 static int mptspi_slave_alloc(struct scsi_device *sdev)
718 {
719         MPT_SCSI_HOST *hd = shost_priv(sdev->host);
720         VirtTarget              *vtarget;
721         VirtDevice              *vdevice;
722         struct scsi_target      *starget;
723         MPT_ADAPTER *ioc = hd->ioc;
724
725         if (sdev->channel == 1 &&
726                 mptscsih_is_phys_disk(ioc, 0, sdev->id) == 0)
727                         return -ENXIO;
728
729         vdevice = kzalloc(sizeof(VirtDevice), GFP_KERNEL);
730         if (!vdevice) {
731                 printk(MYIOC_s_ERR_FMT "slave_alloc kmalloc(%zd) FAILED!\n",
732                                 ioc->name, sizeof(VirtDevice));
733                 return -ENOMEM;
734         }
735
736         vdevice->lun = sdev->lun;
737         sdev->hostdata = vdevice;
738
739         starget = scsi_target(sdev);
740         vtarget = starget->hostdata;
741         vdevice->vtarget = vtarget;
742         vtarget->num_luns++;
743
744         if (sdev->channel == 1)
745                 sdev->no_uld_attach = 1;
746
747         return 0;
748 }
749
750 static int mptspi_slave_configure(struct scsi_device *sdev)
751 {
752         struct _MPT_SCSI_HOST *hd = shost_priv(sdev->host);
753         VirtTarget *vtarget = scsi_target(sdev)->hostdata;
754         int ret;
755
756         mptspi_initTarget(hd, vtarget, sdev);
757
758         ret = mptscsih_slave_configure(sdev);
759
760         if (ret)
761                 return ret;
762
763         ddvprintk(hd->ioc, printk(MYIOC_s_DEBUG_FMT "id=%d min_period=0x%02x"
764                 " max_offset=0x%02x max_width=%d\n", hd->ioc->name,
765                 sdev->id, spi_min_period(scsi_target(sdev)),
766                 spi_max_offset(scsi_target(sdev)),
767                 spi_max_width(scsi_target(sdev))));
768
769         if ((sdev->channel == 1 ||
770              !(mptspi_is_raid(hd, sdev->id))) &&
771             !spi_initial_dv(sdev->sdev_target))
772                 mptspi_dv_device(hd, sdev);
773
774         return 0;
775 }
776
777 static int
778 mptspi_qcmd(struct scsi_cmnd *SCpnt, void (*done)(struct scsi_cmnd *))
779 {
780         struct _MPT_SCSI_HOST *hd = shost_priv(SCpnt->device->host);
781         VirtDevice      *vdevice = SCpnt->device->hostdata;
782         MPT_ADAPTER *ioc = hd->ioc;
783
784         if (!vdevice || !vdevice->vtarget) {
785                 SCpnt->result = DID_NO_CONNECT << 16;
786                 done(SCpnt);
787                 return 0;
788         }
789
790         if (SCpnt->device->channel == 1 &&
791                 mptscsih_is_phys_disk(ioc, 0, SCpnt->device->id) == 0) {
792                 SCpnt->result = DID_NO_CONNECT << 16;
793                 done(SCpnt);
794                 return 0;
795         }
796
797         if (spi_dv_pending(scsi_target(SCpnt->device)))
798                 ddvprintk(ioc, scsi_print_command(SCpnt));
799
800         return mptscsih_qcmd(SCpnt,done);
801 }
802
803 static void mptspi_slave_destroy(struct scsi_device *sdev)
804 {
805         struct scsi_target *starget = scsi_target(sdev);
806         VirtTarget *vtarget = starget->hostdata;
807         VirtDevice *vdevice = sdev->hostdata;
808
809         /* Will this be the last lun on a non-raid device? */
810         if (vtarget->num_luns == 1 && vdevice->configured_lun) {
811                 struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
812
813                 /* Async Narrow */
814                 pg1.RequestedParameters = 0;
815                 pg1.Reserved = 0;
816                 pg1.Configuration = 0;
817
818                 mptspi_write_spi_device_pg1(starget, &pg1);
819         }
820
821         mptscsih_slave_destroy(sdev);
822 }
823
824 static struct scsi_host_template mptspi_driver_template = {
825         .module                         = THIS_MODULE,
826         .proc_name                      = "mptspi",
827         .proc_info                      = mptscsih_proc_info,
828         .name                           = "MPT SPI Host",
829         .info                           = mptscsih_info,
830         .queuecommand                   = mptspi_qcmd,
831         .target_alloc                   = mptspi_target_alloc,
832         .slave_alloc                    = mptspi_slave_alloc,
833         .slave_configure                = mptspi_slave_configure,
834         .target_destroy                 = mptspi_target_destroy,
835         .slave_destroy                  = mptspi_slave_destroy,
836         .change_queue_depth             = mptscsih_change_queue_depth,
837         .eh_abort_handler               = mptscsih_abort,
838         .eh_device_reset_handler        = mptscsih_dev_reset,
839         .eh_bus_reset_handler           = mptscsih_bus_reset,
840         .eh_host_reset_handler          = mptscsih_host_reset,
841         .bios_param                     = mptscsih_bios_param,
842         .can_queue                      = MPT_SCSI_CAN_QUEUE,
843         .this_id                        = -1,
844         .sg_tablesize                   = MPT_SCSI_SG_DEPTH,
845         .max_sectors                    = 8192,
846         .cmd_per_lun                    = 7,
847         .use_clustering                 = ENABLE_CLUSTERING,
848         .shost_attrs                    = mptscsih_host_attrs,
849 };
850
851 static int mptspi_write_spi_device_pg1(struct scsi_target *starget,
852                                struct _CONFIG_PAGE_SCSI_DEVICE_1 *pass_pg1)
853 {
854         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
855         struct _MPT_SCSI_HOST *hd = shost_priv(shost);
856         struct _MPT_ADAPTER *ioc = hd->ioc;
857         struct _CONFIG_PAGE_SCSI_DEVICE_1 *pg1;
858         dma_addr_t pg1_dma;
859         int size;
860         struct _x_config_parms cfg;
861         struct _CONFIG_PAGE_HEADER hdr;
862         int err = -EBUSY;
863
864         /* don't allow updating nego parameters on RAID devices */
865         if (starget->channel == 0 &&
866             mptspi_is_raid(hd, starget->id))
867                 return -1;
868
869         size = ioc->spi_data.sdp1length * 4;
870
871         pg1 = dma_alloc_coherent(&ioc->pcidev->dev, size, &pg1_dma, GFP_KERNEL);
872         if (pg1 == NULL) {
873                 starget_printk(KERN_ERR, starget, MYIOC_s_FMT
874                     "dma_alloc_coherent for parameters failed\n", ioc->name);
875                 return -EINVAL;
876         }
877
878         memset(&hdr, 0, sizeof(hdr));
879
880         hdr.PageVersion = ioc->spi_data.sdp1version;
881         hdr.PageLength = ioc->spi_data.sdp1length;
882         hdr.PageNumber = 1;
883         hdr.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
884
885         memset(&cfg, 0, sizeof(cfg));
886
887         cfg.cfghdr.hdr = &hdr;
888         cfg.physAddr = pg1_dma;
889         cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
890         cfg.dir = 1;
891         cfg.pageAddr = starget->id;
892
893         memcpy(pg1, pass_pg1, size);
894
895         pg1->Header.PageVersion = hdr.PageVersion;
896         pg1->Header.PageLength = hdr.PageLength;
897         pg1->Header.PageNumber = hdr.PageNumber;
898         pg1->Header.PageType = hdr.PageType;
899
900         mptspi_print_write_nego(hd, starget, le32_to_cpu(pg1->RequestedParameters));
901
902         if (mpt_config(ioc, &cfg)) {
903                 starget_printk(KERN_ERR, starget, MYIOC_s_FMT
904                     "mpt_config failed\n", ioc->name);
905                 goto out_free;
906         }
907         err = 0;
908
909  out_free:
910         dma_free_coherent(&ioc->pcidev->dev, size, pg1, pg1_dma);
911         return err;
912 }
913
914 static void mptspi_write_offset(struct scsi_target *starget, int offset)
915 {
916         struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
917         u32 nego;
918
919         if (offset < 0)
920                 offset = 0;
921
922         if (offset > 255)
923                 offset = 255;
924
925         if (spi_offset(starget) == -1)
926                 mptspi_read_parameters(starget);
927
928         spi_offset(starget) = offset;
929
930         nego = mptspi_getRP(starget);
931
932         pg1.RequestedParameters = cpu_to_le32(nego);
933         pg1.Reserved = 0;
934         pg1.Configuration = 0;
935
936         mptspi_write_spi_device_pg1(starget, &pg1);
937 }
938
939 static void mptspi_write_period(struct scsi_target *starget, int period)
940 {
941         struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
942         u32 nego;
943
944         if (period < 8)
945                 period = 8;
946
947         if (period > 255)
948                 period = 255;
949
950         if (spi_period(starget) == -1)
951                 mptspi_read_parameters(starget);
952
953         if (period == 8) {
954                 spi_iu(starget) = 1;
955                 spi_dt(starget) = 1;
956         } else if (period == 9) {
957                 spi_dt(starget) = 1;
958         }
959
960         spi_period(starget) = period;
961
962         nego = mptspi_getRP(starget);
963
964         pg1.RequestedParameters = cpu_to_le32(nego);
965         pg1.Reserved = 0;
966         pg1.Configuration = 0;
967
968         mptspi_write_spi_device_pg1(starget, &pg1);
969 }
970
971 static void mptspi_write_dt(struct scsi_target *starget, int dt)
972 {
973         struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
974         u32 nego;
975
976         if (spi_period(starget) == -1)
977                 mptspi_read_parameters(starget);
978
979         if (!dt && spi_period(starget) < 10)
980                 spi_period(starget) = 10;
981
982         spi_dt(starget) = dt;
983
984         nego = mptspi_getRP(starget);
985
986
987         pg1.RequestedParameters = cpu_to_le32(nego);
988         pg1.Reserved = 0;
989         pg1.Configuration = 0;
990
991         mptspi_write_spi_device_pg1(starget, &pg1);
992 }
993
994 static void mptspi_write_iu(struct scsi_target *starget, int iu)
995 {
996         struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
997         u32 nego;
998
999         if (spi_period(starget) == -1)
1000                 mptspi_read_parameters(starget);
1001
1002         if (!iu && spi_period(starget) < 9)
1003                 spi_period(starget) = 9;
1004
1005         spi_iu(starget) = iu;
1006
1007         nego = mptspi_getRP(starget);
1008
1009         pg1.RequestedParameters = cpu_to_le32(nego);
1010         pg1.Reserved = 0;
1011         pg1.Configuration = 0;
1012
1013         mptspi_write_spi_device_pg1(starget, &pg1);
1014 }
1015
1016 #define MPTSPI_SIMPLE_TRANSPORT_PARM(parm)                              \
1017 static void mptspi_write_##parm(struct scsi_target *starget, int parm)\
1018 {                                                                       \
1019         struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;                          \
1020         u32 nego;                                                       \
1021                                                                         \
1022         spi_##parm(starget) = parm;                                     \
1023                                                                         \
1024         nego = mptspi_getRP(starget);                                   \
1025                                                                         \
1026         pg1.RequestedParameters = cpu_to_le32(nego);                    \
1027         pg1.Reserved = 0;                                               \
1028         pg1.Configuration = 0;                                          \
1029                                                                         \
1030         mptspi_write_spi_device_pg1(starget, &pg1);                             \
1031 }
1032
1033 MPTSPI_SIMPLE_TRANSPORT_PARM(rd_strm)
1034 MPTSPI_SIMPLE_TRANSPORT_PARM(wr_flow)
1035 MPTSPI_SIMPLE_TRANSPORT_PARM(rti)
1036 MPTSPI_SIMPLE_TRANSPORT_PARM(hold_mcs)
1037 MPTSPI_SIMPLE_TRANSPORT_PARM(pcomp_en)
1038
1039 static void mptspi_write_qas(struct scsi_target *starget, int qas)
1040 {
1041         struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
1042         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1043         struct _MPT_SCSI_HOST *hd = shost_priv(shost);
1044         VirtTarget *vtarget = starget->hostdata;
1045         u32 nego;
1046
1047         if ((vtarget->negoFlags & MPT_TARGET_NO_NEGO_QAS) ||
1048             hd->ioc->spi_data.noQas)
1049                 spi_qas(starget) = 0;
1050         else
1051                 spi_qas(starget) = qas;
1052
1053         nego = mptspi_getRP(starget);
1054
1055         pg1.RequestedParameters = cpu_to_le32(nego);
1056         pg1.Reserved = 0;
1057         pg1.Configuration = 0;
1058
1059         mptspi_write_spi_device_pg1(starget, &pg1);
1060 }
1061
1062 static void mptspi_write_width(struct scsi_target *starget, int width)
1063 {
1064         struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
1065         u32 nego;
1066
1067         if (!width) {
1068                 spi_dt(starget) = 0;
1069                 if (spi_period(starget) < 10)
1070                         spi_period(starget) = 10;
1071         }
1072
1073         spi_width(starget) = width;
1074
1075         nego = mptspi_getRP(starget);
1076
1077         pg1.RequestedParameters = cpu_to_le32(nego);
1078         pg1.Reserved = 0;
1079         pg1.Configuration = 0;
1080
1081         mptspi_write_spi_device_pg1(starget, &pg1);
1082 }
1083
1084 struct work_queue_wrapper {
1085         struct work_struct      work;
1086         struct _MPT_SCSI_HOST   *hd;
1087         int                     disk;
1088 };
1089
1090 static void mpt_work_wrapper(struct work_struct *work)
1091 {
1092         struct work_queue_wrapper *wqw =
1093                 container_of(work, struct work_queue_wrapper, work);
1094         struct _MPT_SCSI_HOST *hd = wqw->hd;
1095         MPT_ADAPTER *ioc = hd->ioc;
1096         struct Scsi_Host *shost = ioc->sh;
1097         struct scsi_device *sdev;
1098         int disk = wqw->disk;
1099         struct _CONFIG_PAGE_IOC_3 *pg3;
1100
1101         kfree(wqw);
1102
1103         mpt_findImVolumes(ioc);
1104         pg3 = ioc->raid_data.pIocPg3;
1105         if (!pg3)
1106                 return;
1107
1108         shost_for_each_device(sdev,shost) {
1109                 struct scsi_target *starget = scsi_target(sdev);
1110                 VirtTarget *vtarget = starget->hostdata;
1111
1112                 /* only want to search RAID components */
1113                 if (sdev->channel != 1)
1114                         continue;
1115
1116                 /* The id is the raid PhysDiskNum, even if
1117                  * starget->id is the actual target address */
1118                 if(vtarget->id != disk)
1119                         continue;
1120
1121                 starget_printk(KERN_INFO, vtarget->starget, MYIOC_s_FMT
1122                     "Integrated RAID requests DV of new device\n", ioc->name);
1123                 mptspi_dv_device(hd, sdev);
1124         }
1125         shost_printk(KERN_INFO, shost, MYIOC_s_FMT
1126             "Integrated RAID detects new device %d\n", ioc->name, disk);
1127         scsi_scan_target(&ioc->sh->shost_gendev, 1, disk, 0, 1);
1128 }
1129
1130
1131 static void mpt_dv_raid(struct _MPT_SCSI_HOST *hd, int disk)
1132 {
1133         struct work_queue_wrapper *wqw = kmalloc(sizeof(*wqw), GFP_ATOMIC);
1134         MPT_ADAPTER *ioc = hd->ioc;
1135
1136         if (!wqw) {
1137                 shost_printk(KERN_ERR, ioc->sh, MYIOC_s_FMT
1138                     "Failed to act on RAID event for physical disk %d\n",
1139                     ioc->name, disk);
1140                 return;
1141         }
1142         INIT_WORK(&wqw->work, mpt_work_wrapper);
1143         wqw->hd = hd;
1144         wqw->disk = disk;
1145
1146         schedule_work(&wqw->work);
1147 }
1148
1149 static int
1150 mptspi_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
1151 {
1152         u8 event = le32_to_cpu(pEvReply->Event) & 0xFF;
1153         struct _MPT_SCSI_HOST *hd = shost_priv(ioc->sh);
1154
1155         if (hd && event ==  MPI_EVENT_INTEGRATED_RAID) {
1156                 int reason
1157                         = (le32_to_cpu(pEvReply->Data[0]) & 0x00FF0000) >> 16;
1158
1159                 if (reason == MPI_EVENT_RAID_RC_DOMAIN_VAL_NEEDED) {
1160                         int disk = (le32_to_cpu(pEvReply->Data[0]) & 0xFF000000) >> 24;
1161                         mpt_dv_raid(hd, disk);
1162                 }
1163         }
1164         return mptscsih_event_process(ioc, pEvReply);
1165 }
1166
1167 static int
1168 mptspi_deny_binding(struct scsi_target *starget)
1169 {
1170         struct _MPT_SCSI_HOST *hd =
1171                 (struct _MPT_SCSI_HOST *)dev_to_shost(starget->dev.parent)->hostdata;
1172         return ((mptspi_is_raid(hd, starget->id)) &&
1173                 starget->channel == 0) ? 1 : 0;
1174 }
1175
1176 static struct spi_function_template mptspi_transport_functions = {
1177         .get_offset     = mptspi_read_parameters,
1178         .set_offset     = mptspi_write_offset,
1179         .show_offset    = 1,
1180         .get_period     = mptspi_read_parameters,
1181         .set_period     = mptspi_write_period,
1182         .show_period    = 1,
1183         .get_width      = mptspi_read_parameters,
1184         .set_width      = mptspi_write_width,
1185         .show_width     = 1,
1186         .get_iu         = mptspi_read_parameters,
1187         .set_iu         = mptspi_write_iu,
1188         .show_iu        = 1,
1189         .get_dt         = mptspi_read_parameters,
1190         .set_dt         = mptspi_write_dt,
1191         .show_dt        = 1,
1192         .get_qas        = mptspi_read_parameters,
1193         .set_qas        = mptspi_write_qas,
1194         .show_qas       = 1,
1195         .get_wr_flow    = mptspi_read_parameters,
1196         .set_wr_flow    = mptspi_write_wr_flow,
1197         .show_wr_flow   = 1,
1198         .get_rd_strm    = mptspi_read_parameters,
1199         .set_rd_strm    = mptspi_write_rd_strm,
1200         .show_rd_strm   = 1,
1201         .get_rti        = mptspi_read_parameters,
1202         .set_rti        = mptspi_write_rti,
1203         .show_rti       = 1,
1204         .get_pcomp_en   = mptspi_read_parameters,
1205         .set_pcomp_en   = mptspi_write_pcomp_en,
1206         .show_pcomp_en  = 1,
1207         .get_hold_mcs   = mptspi_read_parameters,
1208         .set_hold_mcs   = mptspi_write_hold_mcs,
1209         .show_hold_mcs  = 1,
1210         .deny_binding   = mptspi_deny_binding,
1211 };
1212
1213 /****************************************************************************
1214  * Supported hardware
1215  */
1216
1217 static struct pci_device_id mptspi_pci_table[] = {
1218         { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_53C1030,
1219                 PCI_ANY_ID, PCI_ANY_ID },
1220         { PCI_VENDOR_ID_ATTO, MPI_MANUFACTPAGE_DEVID_53C1030,
1221                 PCI_ANY_ID, PCI_ANY_ID },
1222         { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_53C1035,
1223                 PCI_ANY_ID, PCI_ANY_ID },
1224         {0}     /* Terminating entry */
1225 };
1226 MODULE_DEVICE_TABLE(pci, mptspi_pci_table);
1227
1228
1229 /*
1230  * renegotiate for a given target
1231  */
1232 static void
1233 mptspi_dv_renegotiate_work(struct work_struct *work)
1234 {
1235         struct work_queue_wrapper *wqw =
1236                 container_of(work, struct work_queue_wrapper, work);
1237         struct _MPT_SCSI_HOST *hd = wqw->hd;
1238         struct scsi_device *sdev;
1239         struct scsi_target *starget;
1240         struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
1241         u32 nego;
1242         MPT_ADAPTER *ioc = hd->ioc;
1243
1244         kfree(wqw);
1245
1246         if (hd->spi_pending) {
1247                 shost_for_each_device(sdev, ioc->sh) {
1248                         if  (hd->spi_pending & (1 << sdev->id))
1249                                 continue;
1250                         starget = scsi_target(sdev);
1251                         nego = mptspi_getRP(starget);
1252                         pg1.RequestedParameters = cpu_to_le32(nego);
1253                         pg1.Reserved = 0;
1254                         pg1.Configuration = 0;
1255                         mptspi_write_spi_device_pg1(starget, &pg1);
1256                 }
1257         } else {
1258                 shost_for_each_device(sdev, ioc->sh)
1259                         mptspi_dv_device(hd, sdev);
1260         }
1261 }
1262
1263 static void
1264 mptspi_dv_renegotiate(struct _MPT_SCSI_HOST *hd)
1265 {
1266         struct work_queue_wrapper *wqw = kmalloc(sizeof(*wqw), GFP_ATOMIC);
1267
1268         if (!wqw)
1269                 return;
1270
1271         INIT_WORK(&wqw->work, mptspi_dv_renegotiate_work);
1272         wqw->hd = hd;
1273
1274         schedule_work(&wqw->work);
1275 }
1276
1277 /*
1278  * spi module reset handler
1279  */
1280 static int
1281 mptspi_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
1282 {
1283         int rc;
1284
1285         rc = mptscsih_ioc_reset(ioc, reset_phase);
1286
1287         /* only try to do a renegotiation if we're properly set up
1288          * if we get an ioc fault on bringup, ioc->sh will be NULL */
1289         if (reset_phase == MPT_IOC_POST_RESET &&
1290             ioc->sh) {
1291                 struct _MPT_SCSI_HOST *hd = shost_priv(ioc->sh);
1292
1293                 mptspi_dv_renegotiate(hd);
1294         }
1295
1296         return rc;
1297 }
1298
1299 #ifdef CONFIG_PM
1300 /*
1301  * spi module resume handler
1302  */
1303 static int
1304 mptspi_resume(struct pci_dev *pdev)
1305 {
1306         MPT_ADAPTER     *ioc = pci_get_drvdata(pdev);
1307         struct _MPT_SCSI_HOST *hd = shost_priv(ioc->sh);
1308         int rc;
1309
1310         rc = mptscsih_resume(pdev);
1311         mptspi_dv_renegotiate(hd);
1312
1313         return rc;
1314 }
1315 #endif
1316
1317 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1318 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1319 /*
1320  *      mptspi_probe - Installs scsi devices per bus.
1321  *      @pdev: Pointer to pci_dev structure
1322  *
1323  *      Returns 0 for success, non-zero for failure.
1324  *
1325  */
1326 static int
1327 mptspi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1328 {
1329         struct Scsi_Host        *sh;
1330         MPT_SCSI_HOST           *hd;
1331         MPT_ADAPTER             *ioc;
1332         unsigned long            flags;
1333         int                      ii;
1334         int                      numSGE = 0;
1335         int                      scale;
1336         int                      ioc_cap;
1337         int                     error=0;
1338         int                     r;
1339
1340         if ((r = mpt_attach(pdev,id)) != 0)
1341                 return r;
1342
1343         ioc = pci_get_drvdata(pdev);
1344         ioc->DoneCtx = mptspiDoneCtx;
1345         ioc->TaskCtx = mptspiTaskCtx;
1346         ioc->InternalCtx = mptspiInternalCtx;
1347
1348         /*  Added sanity check on readiness of the MPT adapter.
1349          */
1350         if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
1351                 printk(MYIOC_s_WARN_FMT
1352                   "Skipping because it's not operational!\n",
1353                   ioc->name);
1354                 error = -ENODEV;
1355                 goto out_mptspi_probe;
1356         }
1357
1358         if (!ioc->active) {
1359                 printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
1360                   ioc->name);
1361                 error = -ENODEV;
1362                 goto out_mptspi_probe;
1363         }
1364
1365         /*  Sanity check - ensure at least 1 port is INITIATOR capable
1366          */
1367         ioc_cap = 0;
1368         for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
1369                 if (ioc->pfacts[ii].ProtocolFlags &
1370                     MPI_PORTFACTS_PROTOCOL_INITIATOR)
1371                         ioc_cap ++;
1372         }
1373
1374         if (!ioc_cap) {
1375                 printk(MYIOC_s_WARN_FMT
1376                         "Skipping ioc=%p because SCSI Initiator mode is NOT enabled!\n",
1377                         ioc->name, ioc);
1378                 return 0;
1379         }
1380
1381         sh = scsi_host_alloc(&mptspi_driver_template, sizeof(MPT_SCSI_HOST));
1382
1383         if (!sh) {
1384                 printk(MYIOC_s_WARN_FMT
1385                         "Unable to register controller with SCSI subsystem\n",
1386                         ioc->name);
1387                 error = -1;
1388                 goto out_mptspi_probe;
1389         }
1390
1391         spin_lock_irqsave(&ioc->FreeQlock, flags);
1392
1393         /* Attach the SCSI Host to the IOC structure
1394          */
1395         ioc->sh = sh;
1396
1397         sh->io_port = 0;
1398         sh->n_io_port = 0;
1399         sh->irq = 0;
1400
1401         /* set 16 byte cdb's */
1402         sh->max_cmd_len = 16;
1403
1404         /* Yikes!  This is important!
1405          * Otherwise, by default, linux
1406          * only scans target IDs 0-7!
1407          * pfactsN->MaxDevices unreliable
1408          * (not supported in early
1409          *      versions of the FW).
1410          * max_id = 1 + actual max id,
1411          * max_lun = 1 + actual last lun,
1412          *      see hosts.h :o(
1413          */
1414         sh->max_id = ioc->devices_per_bus;
1415
1416         sh->max_lun = MPT_LAST_LUN + 1;
1417         /*
1418          * If RAID Firmware Detected, setup virtual channel
1419          */
1420         if (ioc->ir_firmware)
1421                 sh->max_channel = 1;
1422         else
1423                 sh->max_channel = 0;
1424         sh->this_id = ioc->pfacts[0].PortSCSIID;
1425
1426         /* Required entry.
1427          */
1428         sh->unique_id = ioc->id;
1429
1430         /* Verify that we won't exceed the maximum
1431          * number of chain buffers
1432          * We can optimize:  ZZ = req_sz/sizeof(SGE)
1433          * For 32bit SGE's:
1434          *  numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
1435          *               + (req_sz - 64)/sizeof(SGE)
1436          * A slightly different algorithm is required for
1437          * 64bit SGEs.
1438          */
1439         scale = ioc->req_sz/ioc->SGE_size;
1440         if (ioc->sg_addr_size == sizeof(u64)) {
1441                 numSGE = (scale - 1) *
1442                   (ioc->facts.MaxChainDepth-1) + scale +
1443                   (ioc->req_sz - 60) / ioc->SGE_size;
1444         } else {
1445                 numSGE = 1 + (scale - 1) *
1446                   (ioc->facts.MaxChainDepth-1) + scale +
1447                   (ioc->req_sz - 64) / ioc->SGE_size;
1448         }
1449
1450         if (numSGE < sh->sg_tablesize) {
1451                 /* Reset this value */
1452                 dprintk(ioc, printk(MYIOC_s_DEBUG_FMT
1453                   "Resetting sg_tablesize to %d from %d\n",
1454                   ioc->name, numSGE, sh->sg_tablesize));
1455                 sh->sg_tablesize = numSGE;
1456         }
1457
1458         spin_unlock_irqrestore(&ioc->FreeQlock, flags);
1459
1460         hd = shost_priv(sh);
1461         hd->ioc = ioc;
1462
1463         /* SCSI needs scsi_cmnd lookup table!
1464          * (with size equal to req_depth*PtrSz!)
1465          */
1466         ioc->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
1467         if (!ioc->ScsiLookup) {
1468                 error = -ENOMEM;
1469                 goto out_mptspi_probe;
1470         }
1471         spin_lock_init(&ioc->scsi_lookup_lock);
1472
1473         dprintk(ioc, printk(MYIOC_s_DEBUG_FMT "ScsiLookup @ %p\n",
1474                  ioc->name, ioc->ScsiLookup));
1475
1476         ioc->spi_data.Saf_Te = mpt_saf_te;
1477         ddvprintk(ioc, printk(MYIOC_s_DEBUG_FMT
1478                 "saf_te %x\n",
1479                 ioc->name,
1480                 mpt_saf_te));
1481         ioc->spi_data.noQas = 0;
1482
1483         hd->last_queue_full = 0;
1484         hd->spi_pending = 0;
1485
1486         /* Some versions of the firmware don't support page 0; without
1487          * that we can't get the parameters */
1488         if (ioc->spi_data.sdp0length != 0)
1489                 sh->transportt = mptspi_transport_template;
1490
1491         error = scsi_add_host (sh, &ioc->pcidev->dev);
1492         if(error) {
1493                 dprintk(ioc, printk(MYIOC_s_ERR_FMT
1494                   "scsi_add_host failed\n", ioc->name));
1495                 goto out_mptspi_probe;
1496         }
1497
1498         /*
1499          * issue internal bus reset
1500          */
1501         if (ioc->spi_data.bus_reset)
1502                 mptscsih_IssueTaskMgmt(hd,
1503                     MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS,
1504                     0, 0, 0, 0, 5);
1505
1506         scsi_scan_host(sh);
1507         return 0;
1508
1509 out_mptspi_probe:
1510
1511         mptscsih_remove(pdev);
1512         return error;
1513 }
1514
1515 static struct pci_driver mptspi_driver = {
1516         .name           = "mptspi",
1517         .id_table       = mptspi_pci_table,
1518         .probe          = mptspi_probe,
1519         .remove         = __devexit_p(mptscsih_remove),
1520         .shutdown       = mptscsih_shutdown,
1521 #ifdef CONFIG_PM
1522         .suspend        = mptscsih_suspend,
1523         .resume         = mptspi_resume,
1524 #endif
1525 };
1526
1527 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1528 /**
1529  *      mptspi_init - Register MPT adapter(s) as SCSI host(s) with SCSI mid-layer.
1530  *
1531  *      Returns 0 for success, non-zero for failure.
1532  */
1533 static int __init
1534 mptspi_init(void)
1535 {
1536         int error;
1537
1538         show_mptmod_ver(my_NAME, my_VERSION);
1539
1540         mptspi_transport_template = spi_attach_transport(&mptspi_transport_functions);
1541         if (!mptspi_transport_template)
1542                 return -ENODEV;
1543
1544         mptspiDoneCtx = mpt_register(mptscsih_io_done, MPTSPI_DRIVER);
1545         mptspiTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTSPI_DRIVER);
1546         mptspiInternalCtx = mpt_register(mptscsih_scandv_complete, MPTSPI_DRIVER);
1547
1548         mpt_event_register(mptspiDoneCtx, mptspi_event_process);
1549         mpt_reset_register(mptspiDoneCtx, mptspi_ioc_reset);
1550
1551         error = pci_register_driver(&mptspi_driver);
1552         if (error)
1553                 spi_release_transport(mptspi_transport_template);
1554
1555         return error;
1556 }
1557
1558 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1559 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1560 /**
1561  *      mptspi_exit - Unregisters MPT adapter(s)
1562  */
1563 static void __exit
1564 mptspi_exit(void)
1565 {
1566         pci_unregister_driver(&mptspi_driver);
1567
1568         mpt_reset_deregister(mptspiDoneCtx);
1569         mpt_event_deregister(mptspiDoneCtx);
1570
1571         mpt_deregister(mptspiInternalCtx);
1572         mpt_deregister(mptspiTaskCtx);
1573         mpt_deregister(mptspiDoneCtx);
1574         spi_release_transport(mptspi_transport_template);
1575 }
1576
1577 module_init(mptspi_init);
1578 module_exit(mptspi_exit);