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