e4cc3dd5fc9fbeb17f2154ed544bd6e39306b074
[safe/jmp/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-2005 LSI Logic Corporation
7  *  (mailto:mpt_linux_developer@lsil.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/sched.h>
58 #include <linux/workqueue.h>
59 #include <linux/raid_class.h>
60
61 #include <scsi/scsi.h>
62 #include <scsi/scsi_cmnd.h>
63 #include <scsi/scsi_device.h>
64 #include <scsi/scsi_host.h>
65 #include <scsi/scsi_tcq.h>
66 #include <scsi/scsi_transport.h>
67 #include <scsi/scsi_transport_spi.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
81 /* Command line args */
82 static int mpt_saf_te = MPTSCSIH_SAF_TE;
83 module_param(mpt_saf_te, int, 0);
84 MODULE_PARM_DESC(mpt_saf_te, " Force enabling SEP Processor: enable=1  (default=MPTSCSIH_SAF_TE=0)");
85
86 static void mptspi_write_offset(struct scsi_target *, int);
87 static void mptspi_write_width(struct scsi_target *, int);
88 static int mptspi_write_spi_device_pg1(struct scsi_target *,
89                                        struct _CONFIG_PAGE_SCSI_DEVICE_1 *);
90
91 static struct scsi_transport_template *mptspi_transport_template = NULL;
92
93 static int      mptspiDoneCtx = -1;
94 static int      mptspiTaskCtx = -1;
95 static int      mptspiInternalCtx = -1; /* Used only for internal commands */
96
97 static int mptspi_target_alloc(struct scsi_target *starget)
98 {
99         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
100         struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)shost->hostdata;
101         int ret;
102
103         if (hd == NULL)
104                 return -ENODEV;
105
106         ret = mptscsih_target_alloc(starget);
107         if (ret)
108                 return ret;
109
110         /* if we're a device on virtual channel 1 and we're not part
111          * of an array, just return here (otherwise the setup below
112          * may actually affect a real physical device on channel 0 */
113         if (starget->channel == 1 &&
114             mptscsih_raid_id_to_num(hd, starget->id) < 0)
115                 return 0;
116
117         if (hd->ioc->spi_data.nvram &&
118             hd->ioc->spi_data.nvram[starget->id] != MPT_HOST_NVRAM_INVALID) {
119                 u32 nvram = hd->ioc->spi_data.nvram[starget->id];
120                 spi_min_period(starget) = (nvram & MPT_NVRAM_SYNC_MASK) >> MPT_NVRAM_SYNC_SHIFT;
121                 spi_max_width(starget) = nvram & MPT_NVRAM_WIDE_DISABLE ? 0 : 1;
122         } else {
123                 spi_min_period(starget) = hd->ioc->spi_data.minSyncFactor;
124                 spi_max_width(starget) = hd->ioc->spi_data.maxBusWidth;
125         }
126         spi_max_offset(starget) = hd->ioc->spi_data.maxSyncOffset;
127
128         spi_offset(starget) = 0;
129         mptspi_write_width(starget, 0);
130
131         return 0;
132 }
133
134 static int mptspi_read_spi_device_pg0(struct scsi_target *starget,
135                              struct _CONFIG_PAGE_SCSI_DEVICE_0 *pass_pg0)
136 {
137         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
138         struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)shost->hostdata;
139         struct _MPT_ADAPTER *ioc = hd->ioc;
140         struct _CONFIG_PAGE_SCSI_DEVICE_0 *pg0;
141         dma_addr_t pg0_dma;
142         int size;
143         struct _x_config_parms cfg;
144         struct _CONFIG_PAGE_HEADER hdr;
145         int err = -EBUSY;
146
147         /* No SPI parameters for RAID devices */
148         if (starget->channel == 0 &&
149             (hd->ioc->raid_data.isRaid & (1 << starget->id)))
150                 return -1;
151
152         size = ioc->spi_data.sdp0length * 4;
153         /*
154         if (ioc->spi_data.sdp0length & 1)
155                 size += size + 4;
156         size += 2048;
157         */
158
159         pg0 = dma_alloc_coherent(&ioc->pcidev->dev, size, &pg0_dma, GFP_KERNEL);
160         if (pg0 == NULL) {
161                 starget_printk(KERN_ERR, starget, "dma_alloc_coherent for parameters failed\n");
162                 return -EINVAL;
163         }
164
165         memset(&hdr, 0, sizeof(hdr));
166
167         hdr.PageVersion = ioc->spi_data.sdp0version;
168         hdr.PageLength = ioc->spi_data.sdp0length;
169         hdr.PageNumber = 0;
170         hdr.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
171
172         memset(&cfg, 0, sizeof(cfg));
173
174         cfg.cfghdr.hdr = &hdr;
175         cfg.physAddr = pg0_dma;
176         cfg.action = MPI_CONFIG_ACTION_PAGE_READ_CURRENT;
177         cfg.dir = 0;
178         cfg.pageAddr = starget->id;
179
180         if (mpt_config(ioc, &cfg)) {
181                 starget_printk(KERN_ERR, starget, "mpt_config failed\n");
182                 goto out_free;
183         }
184         err = 0;
185         memcpy(pass_pg0, pg0, size);
186
187  out_free:
188         dma_free_coherent(&ioc->pcidev->dev, size, pg0, pg0_dma);
189         return err;
190 }
191
192 static u32 mptspi_getRP(struct scsi_target *starget)
193 {
194         u32 nego = 0;
195
196         nego |= spi_iu(starget) ? MPI_SCSIDEVPAGE1_RP_IU : 0;
197         nego |= spi_dt(starget) ? MPI_SCSIDEVPAGE1_RP_DT : 0;
198         nego |= spi_qas(starget) ? MPI_SCSIDEVPAGE1_RP_QAS : 0;
199         nego |= spi_hold_mcs(starget) ? MPI_SCSIDEVPAGE1_RP_HOLD_MCS : 0;
200         nego |= spi_wr_flow(starget) ? MPI_SCSIDEVPAGE1_RP_WR_FLOW : 0;
201         nego |= spi_rd_strm(starget) ? MPI_SCSIDEVPAGE1_RP_RD_STRM : 0;
202         nego |= spi_rti(starget) ? MPI_SCSIDEVPAGE1_RP_RTI : 0;
203         nego |= spi_pcomp_en(starget) ? MPI_SCSIDEVPAGE1_RP_PCOMP_EN : 0;
204
205         nego |= (spi_period(starget) <<  MPI_SCSIDEVPAGE1_RP_SHIFT_MIN_SYNC_PERIOD) & MPI_SCSIDEVPAGE1_RP_MIN_SYNC_PERIOD_MASK;
206         nego |= (spi_offset(starget) << MPI_SCSIDEVPAGE1_RP_SHIFT_MAX_SYNC_OFFSET) & MPI_SCSIDEVPAGE1_RP_MAX_SYNC_OFFSET_MASK;
207         nego |= spi_width(starget) ?  MPI_SCSIDEVPAGE1_RP_WIDE : 0;
208
209         return nego;
210 }
211
212 static void mptspi_read_parameters(struct scsi_target *starget)
213 {
214         int nego;
215         struct _CONFIG_PAGE_SCSI_DEVICE_0 pg0;
216
217         mptspi_read_spi_device_pg0(starget, &pg0);
218
219         nego = le32_to_cpu(pg0.NegotiatedParameters);
220
221         spi_iu(starget) = (nego & MPI_SCSIDEVPAGE0_NP_IU) ? 1 : 0;
222         spi_dt(starget) = (nego & MPI_SCSIDEVPAGE0_NP_DT) ? 1 : 0;
223         spi_qas(starget) = (nego & MPI_SCSIDEVPAGE0_NP_QAS) ? 1 : 0;
224         spi_wr_flow(starget) = (nego & MPI_SCSIDEVPAGE0_NP_WR_FLOW) ? 1 : 0;
225         spi_rd_strm(starget) = (nego & MPI_SCSIDEVPAGE0_NP_RD_STRM) ? 1 : 0;
226         spi_rti(starget) = (nego & MPI_SCSIDEVPAGE0_NP_RTI) ? 1 : 0;
227         spi_pcomp_en(starget) = (nego & MPI_SCSIDEVPAGE0_NP_PCOMP_EN) ? 1 : 0;
228         spi_hold_mcs(starget) = (nego & MPI_SCSIDEVPAGE0_NP_HOLD_MCS) ? 1 : 0;
229         spi_period(starget) = (nego & MPI_SCSIDEVPAGE0_NP_NEG_SYNC_PERIOD_MASK) >> MPI_SCSIDEVPAGE0_NP_SHIFT_SYNC_PERIOD;
230         spi_offset(starget) = (nego & MPI_SCSIDEVPAGE0_NP_NEG_SYNC_OFFSET_MASK) >> MPI_SCSIDEVPAGE0_NP_SHIFT_SYNC_OFFSET;
231         spi_width(starget) = (nego & MPI_SCSIDEVPAGE0_NP_WIDE) ? 1 : 0;
232 }
233
234 static int
235 mptscsih_quiesce_raid(MPT_SCSI_HOST *hd, int quiesce, int disk)
236 {
237         MpiRaidActionRequest_t  *pReq;
238         MPT_FRAME_HDR           *mf;
239
240         /* Get and Populate a free Frame
241          */
242         if ((mf = mpt_get_msg_frame(hd->ioc->InternalCtx, hd->ioc)) == NULL) {
243                 ddvprintk((MYIOC_s_WARN_FMT "_do_raid: no msg frames!\n",
244                                         hd->ioc->name));
245                 return -EAGAIN;
246         }
247         pReq = (MpiRaidActionRequest_t *)mf;
248         if (quiesce)
249                 pReq->Action = MPI_RAID_ACTION_QUIESCE_PHYS_IO;
250         else
251                 pReq->Action = MPI_RAID_ACTION_ENABLE_PHYS_IO;
252         pReq->Reserved1 = 0;
253         pReq->ChainOffset = 0;
254         pReq->Function = MPI_FUNCTION_RAID_ACTION;
255         pReq->VolumeID = disk;
256         pReq->VolumeBus = 0;
257         pReq->PhysDiskNum = 0;
258         pReq->MsgFlags = 0;
259         pReq->Reserved2 = 0;
260         pReq->ActionDataWord = 0; /* Reserved for this action */
261
262         mpt_add_sge((char *)&pReq->ActionDataSGE,
263                 MPT_SGE_FLAGS_SSIMPLE_READ | 0, (dma_addr_t) -1);
264
265         ddvprintk((MYIOC_s_INFO_FMT "RAID Volume action %x id %d\n",
266                         hd->ioc->name, action, io->id));
267
268         hd->pLocal = NULL;
269         hd->timer.expires = jiffies + HZ*10; /* 10 second timeout */
270         hd->scandv_wait_done = 0;
271
272         /* Save cmd pointer, for resource free if timeout or
273          * FW reload occurs
274          */
275         hd->cmdPtr = mf;
276
277         add_timer(&hd->timer);
278         mpt_put_msg_frame(hd->ioc->InternalCtx, hd->ioc, mf);
279         wait_event(hd->scandv_waitq, hd->scandv_wait_done);
280
281         if ((hd->pLocal == NULL) || (hd->pLocal->completion != 0))
282                 return -1;
283
284         return 0;
285 }
286
287 static void mptspi_dv_device(struct _MPT_SCSI_HOST *hd,
288                              struct scsi_device *sdev)
289 {
290         VirtTarget *vtarget = scsi_target(sdev)->hostdata;
291
292         /* no DV on RAID devices */
293         if (sdev->channel == 0 &&
294             (hd->ioc->raid_data.isRaid & (1 << sdev->id)))
295                 return;
296
297         /* If this is a piece of a RAID, then quiesce first */
298         if (sdev->channel == 1 &&
299             mptscsih_quiesce_raid(hd, 1, vtarget->target_id) < 0) {
300                 starget_printk(KERN_ERR, scsi_target(sdev),
301                                "Integrated RAID quiesce failed\n");
302                 return;
303         }
304
305         spi_dv_device(sdev);
306
307         if (sdev->channel == 1 &&
308             mptscsih_quiesce_raid(hd, 0, vtarget->target_id) < 0)
309                 starget_printk(KERN_ERR, scsi_target(sdev),
310                                "Integrated RAID resume failed\n");
311
312         mptspi_read_parameters(sdev->sdev_target);
313         spi_display_xfer_agreement(sdev->sdev_target);
314         mptspi_read_parameters(sdev->sdev_target);
315 }
316
317 static int mptspi_slave_alloc(struct scsi_device *sdev)
318 {
319         int ret;
320         MPT_SCSI_HOST *hd = (MPT_SCSI_HOST *)sdev->host->hostdata;
321         /* gcc doesn't see that all uses of this variable occur within
322          * the if() statements, so stop it from whining */
323         int physdisknum = 0;
324
325         if (sdev->channel == 1) {
326                 physdisknum = mptscsih_raid_id_to_num(hd, sdev->id);
327
328                 if (physdisknum < 0)
329                         return physdisknum;
330         }
331
332         ret = mptscsih_slave_alloc(sdev);
333
334         if (ret)
335                 return ret;
336
337         if (sdev->channel == 1) {
338                 VirtDevice *vdev = sdev->hostdata;
339                 sdev->no_uld_attach = 1;
340                 vdev->vtarget->tflags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
341                 /* The real channel for this device is zero */
342                 vdev->vtarget->bus_id = 0;
343                 /* The actual physdisknum (for RAID passthrough) */
344                 vdev->vtarget->target_id = physdisknum;
345         }
346
347         return 0;
348 }
349
350 static int mptspi_slave_configure(struct scsi_device *sdev)
351 {
352         int ret = mptscsih_slave_configure(sdev);
353         struct _MPT_SCSI_HOST *hd =
354                 (struct _MPT_SCSI_HOST *)sdev->host->hostdata;
355
356         if (ret)
357                 return ret;
358
359         if ((sdev->channel == 1 ||
360              !(hd->ioc->raid_data.isRaid & (1 << sdev->id))) &&
361             !spi_initial_dv(sdev->sdev_target))
362                 mptspi_dv_device(hd, sdev);
363
364         return 0;
365 }
366
367 static void mptspi_slave_destroy(struct scsi_device *sdev)
368 {
369         struct scsi_target *starget = scsi_target(sdev);
370         VirtTarget *vtarget = starget->hostdata;
371         VirtDevice *vdevice = sdev->hostdata;
372
373         /* Will this be the last lun on a non-raid device? */
374         if (vtarget->num_luns == 1 && vdevice->configured_lun) {
375                 struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
376
377                 /* Async Narrow */
378                 pg1.RequestedParameters = 0;
379                 pg1.Reserved = 0;
380                 pg1.Configuration = 0;
381
382                 mptspi_write_spi_device_pg1(starget, &pg1);
383         }
384
385         mptscsih_slave_destroy(sdev);
386 }
387
388 static struct scsi_host_template mptspi_driver_template = {
389         .module                         = THIS_MODULE,
390         .proc_name                      = "mptspi",
391         .proc_info                      = mptscsih_proc_info,
392         .name                           = "MPT SPI Host",
393         .info                           = mptscsih_info,
394         .queuecommand                   = mptscsih_qcmd,
395         .target_alloc                   = mptspi_target_alloc,
396         .slave_alloc                    = mptspi_slave_alloc,
397         .slave_configure                = mptspi_slave_configure,
398         .target_destroy                 = mptscsih_target_destroy,
399         .slave_destroy                  = mptspi_slave_destroy,
400         .change_queue_depth             = mptscsih_change_queue_depth,
401         .eh_abort_handler               = mptscsih_abort,
402         .eh_device_reset_handler        = mptscsih_dev_reset,
403         .eh_bus_reset_handler           = mptscsih_bus_reset,
404         .eh_host_reset_handler          = mptscsih_host_reset,
405         .bios_param                     = mptscsih_bios_param,
406         .can_queue                      = MPT_SCSI_CAN_QUEUE,
407         .this_id                        = -1,
408         .sg_tablesize                   = MPT_SCSI_SG_DEPTH,
409         .max_sectors                    = 8192,
410         .cmd_per_lun                    = 7,
411         .use_clustering                 = ENABLE_CLUSTERING,
412 };
413
414 static int mptspi_write_spi_device_pg1(struct scsi_target *starget,
415                                struct _CONFIG_PAGE_SCSI_DEVICE_1 *pass_pg1)
416 {
417         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
418         struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)shost->hostdata;
419         struct _MPT_ADAPTER *ioc = hd->ioc;
420         struct _CONFIG_PAGE_SCSI_DEVICE_1 *pg1;
421         dma_addr_t pg1_dma;
422         int size;
423         struct _x_config_parms cfg;
424         struct _CONFIG_PAGE_HEADER hdr;
425         int err = -EBUSY;
426
427         /* don't allow updating nego parameters on RAID devices */
428         if (starget->channel == 0 &&
429             (hd->ioc->raid_data.isRaid & (1 << starget->id)))
430                 return -1;
431
432         size = ioc->spi_data.sdp1length * 4;
433
434         pg1 = dma_alloc_coherent(&ioc->pcidev->dev, size, &pg1_dma, GFP_KERNEL);
435         if (pg1 == NULL) {
436                 starget_printk(KERN_ERR, starget, "dma_alloc_coherent for parameters failed\n");
437                 return -EINVAL;
438         }
439
440         memset(&hdr, 0, sizeof(hdr));
441
442         hdr.PageVersion = ioc->spi_data.sdp1version;
443         hdr.PageLength = ioc->spi_data.sdp1length;
444         hdr.PageNumber = 1;
445         hdr.PageType = MPI_CONFIG_PAGETYPE_SCSI_DEVICE;
446
447         memset(&cfg, 0, sizeof(cfg));
448
449         cfg.cfghdr.hdr = &hdr;
450         cfg.physAddr = pg1_dma;
451         cfg.action = MPI_CONFIG_ACTION_PAGE_WRITE_CURRENT;
452         cfg.dir = 1;
453         cfg.pageAddr = starget->id;
454
455         memcpy(pg1, pass_pg1, size);
456
457         pg1->Header.PageVersion = hdr.PageVersion;
458         pg1->Header.PageLength = hdr.PageLength;
459         pg1->Header.PageNumber = hdr.PageNumber;
460         pg1->Header.PageType = hdr.PageType;
461
462         if (mpt_config(ioc, &cfg)) {
463                 starget_printk(KERN_ERR, starget, "mpt_config failed\n");
464                 goto out_free;
465         }
466         err = 0;
467
468  out_free:
469         dma_free_coherent(&ioc->pcidev->dev, size, pg1, pg1_dma);
470         return err;
471 }
472
473 static void mptspi_write_offset(struct scsi_target *starget, int offset)
474 {
475         struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
476         u32 nego;
477
478         if (offset < 0)
479                 offset = 0;
480
481         if (offset > 255)
482                 offset = 255;
483
484         if (spi_offset(starget) == -1)
485                 mptspi_read_parameters(starget);
486
487         spi_offset(starget) = offset;
488
489         nego = mptspi_getRP(starget);
490
491         pg1.RequestedParameters = cpu_to_le32(nego);
492         pg1.Reserved = 0;
493         pg1.Configuration = 0;
494
495         mptspi_write_spi_device_pg1(starget, &pg1);
496 }
497
498 static void mptspi_write_period(struct scsi_target *starget, int period)
499 {
500         struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
501         u32 nego;
502
503         if (period < 8)
504                 period = 8;
505
506         if (period > 255)
507                 period = 255;
508
509         if (spi_period(starget) == -1)
510                 mptspi_read_parameters(starget);
511
512         if (period == 8) {
513                 spi_iu(starget) = 1;
514                 spi_dt(starget) = 1;
515         } else if (period == 9) {
516                 spi_dt(starget) = 1;
517         }
518
519         spi_period(starget) = period;
520
521         nego = mptspi_getRP(starget);
522
523         pg1.RequestedParameters = cpu_to_le32(nego);
524         pg1.Reserved = 0;
525         pg1.Configuration = 0;
526
527         mptspi_write_spi_device_pg1(starget, &pg1);
528 }
529
530 static void mptspi_write_dt(struct scsi_target *starget, int dt)
531 {
532         struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
533         u32 nego;
534
535         if (spi_period(starget) == -1)
536                 mptspi_read_parameters(starget);
537
538         if (!dt && spi_period(starget) < 10)
539                 spi_period(starget) = 10;
540
541         spi_dt(starget) = dt;
542
543         nego = mptspi_getRP(starget);
544
545
546         pg1.RequestedParameters = cpu_to_le32(nego);
547         pg1.Reserved = 0;
548         pg1.Configuration = 0;
549
550         mptspi_write_spi_device_pg1(starget, &pg1);
551 }
552
553 static void mptspi_write_iu(struct scsi_target *starget, int iu)
554 {
555         struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
556         u32 nego;
557
558         if (spi_period(starget) == -1)
559                 mptspi_read_parameters(starget);
560
561         if (!iu && spi_period(starget) < 9)
562                 spi_period(starget) = 9;
563
564         spi_iu(starget) = iu;
565
566         nego = mptspi_getRP(starget);
567
568         pg1.RequestedParameters = cpu_to_le32(nego);
569         pg1.Reserved = 0;
570         pg1.Configuration = 0;
571
572         mptspi_write_spi_device_pg1(starget, &pg1);
573 }
574
575 #define MPTSPI_SIMPLE_TRANSPORT_PARM(parm)                              \
576 static void mptspi_write_##parm(struct scsi_target *starget, int parm)\
577 {                                                                       \
578         struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;                          \
579         u32 nego;                                                       \
580                                                                         \
581         spi_##parm(starget) = parm;                                     \
582                                                                         \
583         nego = mptspi_getRP(starget);                                   \
584                                                                         \
585         pg1.RequestedParameters = cpu_to_le32(nego);                    \
586         pg1.Reserved = 0;                                               \
587         pg1.Configuration = 0;                                          \
588                                                                         \
589         mptspi_write_spi_device_pg1(starget, &pg1);                             \
590 }
591
592 MPTSPI_SIMPLE_TRANSPORT_PARM(rd_strm)
593 MPTSPI_SIMPLE_TRANSPORT_PARM(wr_flow)
594 MPTSPI_SIMPLE_TRANSPORT_PARM(rti)
595 MPTSPI_SIMPLE_TRANSPORT_PARM(hold_mcs)
596 MPTSPI_SIMPLE_TRANSPORT_PARM(pcomp_en)
597
598 static void mptspi_write_qas(struct scsi_target *starget, int qas)
599 {
600         struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
601         struct Scsi_Host *shost = dev_to_shost(&starget->dev);
602         struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)shost->hostdata;
603         VirtTarget *vtarget = starget->hostdata;
604         u32 nego;
605
606         if ((vtarget->negoFlags & MPT_TARGET_NO_NEGO_QAS) ||
607             hd->ioc->spi_data.noQas)
608                 spi_qas(starget) = 0;
609         else
610                 spi_qas(starget) = qas;
611
612         nego = mptspi_getRP(starget);
613
614         pg1.RequestedParameters = cpu_to_le32(nego);
615         pg1.Reserved = 0;
616         pg1.Configuration = 0;
617
618         mptspi_write_spi_device_pg1(starget, &pg1);
619 }
620
621 static void mptspi_write_width(struct scsi_target *starget, int width)
622 {
623         struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
624         u32 nego;
625
626         if (!width) {
627                 spi_dt(starget) = 0;
628                 if (spi_period(starget) < 10)
629                         spi_period(starget) = 10;
630         }
631
632         spi_width(starget) = width;
633
634         nego = mptspi_getRP(starget);
635
636         pg1.RequestedParameters = cpu_to_le32(nego);
637         pg1.Reserved = 0;
638         pg1.Configuration = 0;
639
640         mptspi_write_spi_device_pg1(starget, &pg1);
641 }
642
643 struct work_queue_wrapper {
644         struct work_struct      work;
645         struct _MPT_SCSI_HOST   *hd;
646         int                     disk;
647 };
648
649 static void mpt_work_wrapper(void *data)
650 {
651         struct work_queue_wrapper *wqw = (struct work_queue_wrapper *)data;
652         struct _MPT_SCSI_HOST *hd = wqw->hd;
653         struct Scsi_Host *shost = hd->ioc->sh;
654         struct scsi_device *sdev;
655         int disk = wqw->disk;
656         struct _CONFIG_PAGE_IOC_3 *pg3;
657
658         kfree(wqw);
659
660         mpt_findImVolumes(hd->ioc);
661         pg3 = hd->ioc->raid_data.pIocPg3;
662         if (!pg3)
663                 return;
664
665         shost_for_each_device(sdev,shost) {
666                 struct scsi_target *starget = scsi_target(sdev);
667                 VirtTarget *vtarget = starget->hostdata;
668
669                 /* only want to search RAID components */
670                 if (sdev->channel != 1)
671                         continue;
672
673                 /* The target_id is the raid PhysDiskNum, even if
674                  * starget->id is the actual target address */
675                 if(vtarget->target_id != disk)
676                         continue;
677
678                 starget_printk(KERN_INFO, vtarget->starget,
679                                "Integrated RAID requests DV of new device\n");
680                 mptspi_dv_device(hd, sdev);
681         }
682         shost_printk(KERN_INFO, shost,
683                      "Integrated RAID detects new device %d\n", disk);
684         scsi_scan_target(&hd->ioc->sh->shost_gendev, 1, disk, 0, 1);
685 }
686
687
688 static void mpt_dv_raid(struct _MPT_SCSI_HOST *hd, int disk)
689 {
690         struct work_queue_wrapper *wqw = kmalloc(sizeof(*wqw), GFP_ATOMIC);
691
692         if (!wqw) {
693                 shost_printk(KERN_ERR, hd->ioc->sh,
694                              "Failed to act on RAID event for physical disk %d\n",
695                            disk);
696                 return;
697         }
698         INIT_WORK(&wqw->work, mpt_work_wrapper, wqw);
699         wqw->hd = hd;
700         wqw->disk = disk;
701
702         schedule_work(&wqw->work);
703 }
704
705 static int
706 mptspi_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
707 {
708         u8 event = le32_to_cpu(pEvReply->Event) & 0xFF;
709         struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)ioc->sh->hostdata;
710
711         if (hd && event ==  MPI_EVENT_INTEGRATED_RAID) {
712                 int reason
713                         = (le32_to_cpu(pEvReply->Data[0]) & 0x00FF0000) >> 16;
714
715                 if (reason == MPI_EVENT_RAID_RC_DOMAIN_VAL_NEEDED) {
716                         int disk = (le32_to_cpu(pEvReply->Data[0]) & 0xFF000000) >> 24;
717                         mpt_dv_raid(hd, disk);
718                 }
719         }
720         return mptscsih_event_process(ioc, pEvReply);
721 }
722
723 static int
724 mptspi_deny_binding(struct scsi_target *starget)
725 {
726         struct _MPT_SCSI_HOST *hd =
727                 (struct _MPT_SCSI_HOST *)dev_to_shost(starget->dev.parent)->hostdata;
728         return ((hd->ioc->raid_data.isRaid & (1 << starget->id)) &&
729                 starget->channel == 0) ? 1 : 0;
730 }
731
732 static struct spi_function_template mptspi_transport_functions = {
733         .get_offset     = mptspi_read_parameters,
734         .set_offset     = mptspi_write_offset,
735         .show_offset    = 1,
736         .get_period     = mptspi_read_parameters,
737         .set_period     = mptspi_write_period,
738         .show_period    = 1,
739         .get_width      = mptspi_read_parameters,
740         .set_width      = mptspi_write_width,
741         .show_width     = 1,
742         .get_iu         = mptspi_read_parameters,
743         .set_iu         = mptspi_write_iu,
744         .show_iu        = 1,
745         .get_dt         = mptspi_read_parameters,
746         .set_dt         = mptspi_write_dt,
747         .show_dt        = 1,
748         .get_qas        = mptspi_read_parameters,
749         .set_qas        = mptspi_write_qas,
750         .show_qas       = 1,
751         .get_wr_flow    = mptspi_read_parameters,
752         .set_wr_flow    = mptspi_write_wr_flow,
753         .show_wr_flow   = 1,
754         .get_rd_strm    = mptspi_read_parameters,
755         .set_rd_strm    = mptspi_write_rd_strm,
756         .show_rd_strm   = 1,
757         .get_rti        = mptspi_read_parameters,
758         .set_rti        = mptspi_write_rti,
759         .show_rti       = 1,
760         .get_pcomp_en   = mptspi_read_parameters,
761         .set_pcomp_en   = mptspi_write_pcomp_en,
762         .show_pcomp_en  = 1,
763         .get_hold_mcs   = mptspi_read_parameters,
764         .set_hold_mcs   = mptspi_write_hold_mcs,
765         .show_hold_mcs  = 1,
766         .deny_binding   = mptspi_deny_binding,
767 };
768
769 /****************************************************************************
770  * Supported hardware
771  */
772
773 static struct pci_device_id mptspi_pci_table[] = {
774         { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_53C1030,
775                 PCI_ANY_ID, PCI_ANY_ID },
776         { PCI_VENDOR_ID_LSI_LOGIC, MPI_MANUFACTPAGE_DEVID_53C1035,
777                 PCI_ANY_ID, PCI_ANY_ID },
778         {0}     /* Terminating entry */
779 };
780 MODULE_DEVICE_TABLE(pci, mptspi_pci_table);
781
782
783 /*
784  * renegotiate for a given target
785  */
786 static void
787 mptspi_dv_renegotiate_work(void *data)
788 {
789         struct work_queue_wrapper *wqw = (struct work_queue_wrapper *)data;
790         struct _MPT_SCSI_HOST *hd = wqw->hd;
791         struct scsi_device *sdev;
792
793         kfree(wqw);
794
795         shost_for_each_device(sdev, hd->ioc->sh)
796                 mptspi_dv_device(hd, sdev);
797 }
798
799 static void
800 mptspi_dv_renegotiate(struct _MPT_SCSI_HOST *hd)
801 {
802         struct work_queue_wrapper *wqw = kmalloc(sizeof(*wqw), GFP_ATOMIC);
803
804         if (!wqw)
805                 return;
806
807         INIT_WORK(&wqw->work, mptspi_dv_renegotiate_work, wqw);
808         wqw->hd = hd;
809
810         schedule_work(&wqw->work);
811 }
812
813 /*
814  * spi module reset handler
815  */
816 static int
817 mptspi_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
818 {
819         struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)ioc->sh->hostdata;
820         int rc;
821
822         rc = mptscsih_ioc_reset(ioc, reset_phase);
823
824         if (reset_phase == MPT_IOC_POST_RESET)
825                 mptspi_dv_renegotiate(hd);
826
827         return rc;
828 }
829
830 #ifdef CONFIG_PM
831 /*
832  * spi module resume handler
833  */
834 static int
835 mptspi_resume(struct pci_dev *pdev)
836 {
837         MPT_ADAPTER     *ioc = pci_get_drvdata(pdev);
838         struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)ioc->sh->hostdata;
839         int rc;
840
841         rc = mptscsih_resume(pdev);
842         mptspi_dv_renegotiate(hd);
843
844         return rc;
845 }
846 #endif
847
848 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
849 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
850 /*
851  *      mptspi_probe - Installs scsi devices per bus.
852  *      @pdev: Pointer to pci_dev structure
853  *
854  *      Returns 0 for success, non-zero for failure.
855  *
856  */
857 static int
858 mptspi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
859 {
860         struct Scsi_Host        *sh;
861         MPT_SCSI_HOST           *hd;
862         MPT_ADAPTER             *ioc;
863         unsigned long            flags;
864         int                      ii;
865         int                      numSGE = 0;
866         int                      scale;
867         int                      ioc_cap;
868         int                     error=0;
869         int                     r;
870
871         if ((r = mpt_attach(pdev,id)) != 0)
872                 return r;
873
874         ioc = pci_get_drvdata(pdev);
875         ioc->DoneCtx = mptspiDoneCtx;
876         ioc->TaskCtx = mptspiTaskCtx;
877         ioc->InternalCtx = mptspiInternalCtx;
878
879         /*  Added sanity check on readiness of the MPT adapter.
880          */
881         if (ioc->last_state != MPI_IOC_STATE_OPERATIONAL) {
882                 printk(MYIOC_s_WARN_FMT
883                   "Skipping because it's not operational!\n",
884                   ioc->name);
885                 error = -ENODEV;
886                 goto out_mptspi_probe;
887         }
888
889         if (!ioc->active) {
890                 printk(MYIOC_s_WARN_FMT "Skipping because it's disabled!\n",
891                   ioc->name);
892                 error = -ENODEV;
893                 goto out_mptspi_probe;
894         }
895
896         /*  Sanity check - ensure at least 1 port is INITIATOR capable
897          */
898         ioc_cap = 0;
899         for (ii=0; ii < ioc->facts.NumberOfPorts; ii++) {
900                 if (ioc->pfacts[ii].ProtocolFlags &
901                     MPI_PORTFACTS_PROTOCOL_INITIATOR)
902                         ioc_cap ++;
903         }
904
905         if (!ioc_cap) {
906                 printk(MYIOC_s_WARN_FMT
907                         "Skipping ioc=%p because SCSI Initiator mode is NOT enabled!\n",
908                         ioc->name, ioc);
909                 return 0;
910         }
911
912         sh = scsi_host_alloc(&mptspi_driver_template, sizeof(MPT_SCSI_HOST));
913
914         if (!sh) {
915                 printk(MYIOC_s_WARN_FMT
916                         "Unable to register controller with SCSI subsystem\n",
917                         ioc->name);
918                 error = -1;
919                 goto out_mptspi_probe;
920         }
921
922         spin_lock_irqsave(&ioc->FreeQlock, flags);
923
924         /* Attach the SCSI Host to the IOC structure
925          */
926         ioc->sh = sh;
927
928         sh->io_port = 0;
929         sh->n_io_port = 0;
930         sh->irq = 0;
931
932         /* set 16 byte cdb's */
933         sh->max_cmd_len = 16;
934
935         /* Yikes!  This is important!
936          * Otherwise, by default, linux
937          * only scans target IDs 0-7!
938          * pfactsN->MaxDevices unreliable
939          * (not supported in early
940          *      versions of the FW).
941          * max_id = 1 + actual max id,
942          * max_lun = 1 + actual last lun,
943          *      see hosts.h :o(
944          */
945         sh->max_id = MPT_MAX_SCSI_DEVICES;
946
947         sh->max_lun = MPT_LAST_LUN + 1;
948         /*
949          * If RAID Firmware Detected, setup virtual channel
950          */
951         if ((ioc->facts.ProductID & MPI_FW_HEADER_PID_PROD_MASK)
952             > MPI_FW_HEADER_PID_PROD_TARGET_SCSI)
953                 sh->max_channel = 1;
954         else
955                 sh->max_channel = 0;
956         sh->this_id = ioc->pfacts[0].PortSCSIID;
957
958         /* Required entry.
959          */
960         sh->unique_id = ioc->id;
961
962         /* Verify that we won't exceed the maximum
963          * number of chain buffers
964          * We can optimize:  ZZ = req_sz/sizeof(SGE)
965          * For 32bit SGE's:
966          *  numSGE = 1 + (ZZ-1)*(maxChain -1) + ZZ
967          *               + (req_sz - 64)/sizeof(SGE)
968          * A slightly different algorithm is required for
969          * 64bit SGEs.
970          */
971         scale = ioc->req_sz/(sizeof(dma_addr_t) + sizeof(u32));
972         if (sizeof(dma_addr_t) == sizeof(u64)) {
973                 numSGE = (scale - 1) *
974                   (ioc->facts.MaxChainDepth-1) + scale +
975                   (ioc->req_sz - 60) / (sizeof(dma_addr_t) +
976                   sizeof(u32));
977         } else {
978                 numSGE = 1 + (scale - 1) *
979                   (ioc->facts.MaxChainDepth-1) + scale +
980                   (ioc->req_sz - 64) / (sizeof(dma_addr_t) +
981                   sizeof(u32));
982         }
983
984         if (numSGE < sh->sg_tablesize) {
985                 /* Reset this value */
986                 dprintk((MYIOC_s_INFO_FMT
987                   "Resetting sg_tablesize to %d from %d\n",
988                   ioc->name, numSGE, sh->sg_tablesize));
989                 sh->sg_tablesize = numSGE;
990         }
991
992         spin_unlock_irqrestore(&ioc->FreeQlock, flags);
993
994         hd = (MPT_SCSI_HOST *) sh->hostdata;
995         hd->ioc = ioc;
996
997         /* SCSI needs scsi_cmnd lookup table!
998          * (with size equal to req_depth*PtrSz!)
999          */
1000         hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
1001         if (!hd->ScsiLookup) {
1002                 error = -ENOMEM;
1003                 goto out_mptspi_probe;
1004         }
1005
1006         dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
1007                  ioc->name, hd->ScsiLookup));
1008
1009         /* Allocate memory for the device structures.
1010          * A non-Null pointer at an offset
1011          * indicates a device exists.
1012          * max_id = 1 + maximum id (hosts.h)
1013          */
1014         hd->Targets = kcalloc(sh->max_id * (sh->max_channel + 1),
1015                               sizeof(void *), GFP_ATOMIC);
1016         if (!hd->Targets) {
1017                 error = -ENOMEM;
1018                 goto out_mptspi_probe;
1019         }
1020
1021         dprintk((KERN_INFO "  vdev @ %p\n", hd->Targets));
1022
1023         /* Clear the TM flags
1024          */
1025         hd->tmPending = 0;
1026         hd->tmState = TM_STATE_NONE;
1027         hd->resetPending = 0;
1028         hd->abortSCpnt = NULL;
1029
1030         /* Clear the pointer used to store
1031          * single-threaded commands, i.e., those
1032          * issued during a bus scan, dv and
1033          * configuration pages.
1034          */
1035         hd->cmdPtr = NULL;
1036
1037         /* Initialize this SCSI Hosts' timers
1038          * To use, set the timer expires field
1039          * and add_timer
1040          */
1041         init_timer(&hd->timer);
1042         hd->timer.data = (unsigned long) hd;
1043         hd->timer.function = mptscsih_timer_expired;
1044
1045         ioc->spi_data.Saf_Te = mpt_saf_te;
1046
1047         hd->negoNvram = MPT_SCSICFG_USE_NVRAM;
1048         ddvprintk((MYIOC_s_INFO_FMT
1049                 "saf_te %x\n",
1050                 ioc->name,
1051                 mpt_saf_te));
1052         ioc->spi_data.noQas = 0;
1053
1054         init_waitqueue_head(&hd->scandv_waitq);
1055         hd->scandv_wait_done = 0;
1056         hd->last_queue_full = 0;
1057
1058         /* Some versions of the firmware don't support page 0; without
1059          * that we can't get the parameters */
1060         if (hd->ioc->spi_data.sdp0length != 0)
1061                 sh->transportt = mptspi_transport_template;
1062
1063         error = scsi_add_host (sh, &ioc->pcidev->dev);
1064         if(error) {
1065                 dprintk((KERN_ERR MYNAM
1066                   "scsi_add_host failed\n"));
1067                 goto out_mptspi_probe;
1068         }
1069
1070         /*
1071          * issue internal bus reset
1072          */
1073         if (ioc->spi_data.bus_reset)
1074                 mptscsih_TMHandler(hd,
1075                     MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS,
1076                     0, 0, 0, 0, 5);
1077
1078         scsi_scan_host(sh);
1079         return 0;
1080
1081 out_mptspi_probe:
1082
1083         mptscsih_remove(pdev);
1084         return error;
1085 }
1086
1087 static struct pci_driver mptspi_driver = {
1088         .name           = "mptspi",
1089         .id_table       = mptspi_pci_table,
1090         .probe          = mptspi_probe,
1091         .remove         = __devexit_p(mptscsih_remove),
1092         .shutdown       = mptscsih_shutdown,
1093 #ifdef CONFIG_PM
1094         .suspend        = mptscsih_suspend,
1095         .resume         = mptspi_resume,
1096 #endif
1097 };
1098
1099 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1100 /**
1101  *      mptspi_init - Register MPT adapter(s) as SCSI host(s) with
1102  *      linux scsi mid-layer.
1103  *
1104  *      Returns 0 for success, non-zero for failure.
1105  */
1106 static int __init
1107 mptspi_init(void)
1108 {
1109         show_mptmod_ver(my_NAME, my_VERSION);
1110
1111         mptspi_transport_template = spi_attach_transport(&mptspi_transport_functions);
1112         if (!mptspi_transport_template)
1113                 return -ENODEV;
1114
1115         mptspiDoneCtx = mpt_register(mptscsih_io_done, MPTSPI_DRIVER);
1116         mptspiTaskCtx = mpt_register(mptscsih_taskmgmt_complete, MPTSPI_DRIVER);
1117         mptspiInternalCtx = mpt_register(mptscsih_scandv_complete, MPTSPI_DRIVER);
1118
1119         if (mpt_event_register(mptspiDoneCtx, mptspi_event_process) == 0) {
1120                 devtverboseprintk((KERN_INFO MYNAM
1121                   ": Registered for IOC event notifications\n"));
1122         }
1123
1124         if (mpt_reset_register(mptspiDoneCtx, mptspi_ioc_reset) == 0) {
1125                 dprintk((KERN_INFO MYNAM
1126                   ": Registered for IOC reset notifications\n"));
1127         }
1128
1129         return pci_register_driver(&mptspi_driver);
1130 }
1131
1132 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1133 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1134 /**
1135  *      mptspi_exit - Unregisters MPT adapter(s)
1136  *
1137  */
1138 static void __exit
1139 mptspi_exit(void)
1140 {
1141         pci_unregister_driver(&mptspi_driver);
1142
1143         mpt_reset_deregister(mptspiDoneCtx);
1144         dprintk((KERN_INFO MYNAM
1145           ": Deregistered for IOC reset notifications\n"));
1146
1147         mpt_event_deregister(mptspiDoneCtx);
1148         dprintk((KERN_INFO MYNAM
1149           ": Deregistered for IOC event notifications\n"));
1150
1151         mpt_deregister(mptspiInternalCtx);
1152         mpt_deregister(mptspiTaskCtx);
1153         mpt_deregister(mptspiDoneCtx);
1154         spi_release_transport(mptspi_transport_template);
1155 }
1156
1157 module_init(mptspi_init);
1158 module_exit(mptspi_exit);