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