Linux 2.6.20.7
[linux/fpc-iii.git] / drivers / message / fusion / mptspi.c
blob203c661d2c79c782324b849fa57589289da95e09
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.
6 * Copyright (c) 1999-2007 LSI Logic Corporation
7 * (mailto:mpt_linux_developer@lsil.com)
9 */
10 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
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.
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.
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.
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
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
45 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
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>
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>
69 #include "mptbase.h"
70 #include "mptscsih.h"
72 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
73 #define my_NAME "Fusion MPT SPI Host driver"
74 #define my_VERSION MPT_LINUX_VERSION_COMMON
75 #define MYNAM "mptspi"
77 MODULE_AUTHOR(MODULEAUTHOR);
78 MODULE_DESCRIPTION(my_NAME);
79 MODULE_LICENSE("GPL");
80 MODULE_VERSION(my_VERSION);
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)");
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 *);
92 static struct scsi_transport_template *mptspi_transport_template = NULL;
94 static int mptspiDoneCtx = -1;
95 static int mptspiTaskCtx = -1;
96 static int mptspiInternalCtx = -1; /* Used only for internal commands */
98 static int mptspi_target_alloc(struct scsi_target *starget)
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;
104 if (hd == NULL)
105 return -ENODEV;
107 ret = mptscsih_target_alloc(starget);
108 if (ret)
109 return ret;
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;
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;
127 spi_max_offset(starget) = hd->ioc->spi_data.maxSyncOffset;
129 spi_offset(starget) = 0;
130 mptspi_write_width(starget, 0);
132 return 0;
135 static int mptspi_read_spi_device_pg0(struct scsi_target *starget,
136 struct _CONFIG_PAGE_SCSI_DEVICE_0 *pass_pg0)
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;
148 /* No SPI parameters for RAID devices */
149 if (starget->channel == 0 &&
150 (hd->ioc->raid_data.isRaid & (1 << starget->id)))
151 return -1;
153 size = ioc->spi_data.sdp0length * 4;
155 if (ioc->spi_data.sdp0length & 1)
156 size += size + 4;
157 size += 2048;
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;
166 memset(&hdr, 0, sizeof(hdr));
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;
173 memset(&cfg, 0, sizeof(cfg));
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;
181 if (mpt_config(ioc, &cfg)) {
182 starget_printk(KERN_ERR, starget, "mpt_config failed\n");
183 goto out_free;
185 err = 0;
186 memcpy(pass_pg0, pg0, size);
188 out_free:
189 dma_free_coherent(&ioc->pcidev->dev, size, pg0, pg0_dma);
190 return err;
193 static u32 mptspi_getRP(struct scsi_target *starget)
195 u32 nego = 0;
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;
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;
210 return nego;
213 static void mptspi_read_parameters(struct scsi_target *starget)
215 int nego;
216 struct _CONFIG_PAGE_SCSI_DEVICE_0 pg0;
218 mptspi_read_spi_device_pg0(starget, &pg0);
220 nego = le32_to_cpu(pg0.NegotiatedParameters);
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;
235 static int
236 mptscsih_quiesce_raid(MPT_SCSI_HOST *hd, int quiesce, int disk)
238 MpiRaidActionRequest_t *pReq;
239 MPT_FRAME_HDR *mf;
241 /* Get and Populate a free Frame
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;
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 */
263 mpt_add_sge((char *)&pReq->ActionDataSGE,
264 MPT_SGE_FLAGS_SSIMPLE_READ | 0, (dma_addr_t) -1);
266 ddvprintk((MYIOC_s_INFO_FMT "RAID Volume action %x id %d\n",
267 hd->ioc->name, action, io->id));
269 hd->pLocal = NULL;
270 hd->timer.expires = jiffies + HZ*10; /* 10 second timeout */
271 hd->scandv_wait_done = 0;
273 /* Save cmd pointer, for resource free if timeout or
274 * FW reload occurs
276 hd->cmdPtr = mf;
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);
282 if ((hd->pLocal == NULL) || (hd->pLocal->completion != 0))
283 return -1;
285 return 0;
288 static void mptspi_dv_device(struct _MPT_SCSI_HOST *hd,
289 struct scsi_device *sdev)
291 VirtTarget *vtarget = scsi_target(sdev)->hostdata;
293 /* no DV on RAID devices */
294 if (sdev->channel == 0 &&
295 (hd->ioc->raid_data.isRaid & (1 << sdev->id)))
296 return;
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;
306 spi_dv_device(sdev);
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");
313 mptspi_read_parameters(sdev->sdev_target);
314 spi_display_xfer_agreement(sdev->sdev_target);
315 mptspi_read_parameters(sdev->sdev_target);
318 static int mptspi_slave_alloc(struct scsi_device *sdev)
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;
326 if (sdev->channel == 1) {
327 physdisknum = mptscsih_raid_id_to_num(hd, sdev->id);
329 if (physdisknum < 0)
330 return physdisknum;
333 ret = mptscsih_slave_alloc(sdev);
335 if (ret)
336 return ret;
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;
348 return 0;
351 static int mptspi_slave_configure(struct scsi_device *sdev)
353 int ret = mptscsih_slave_configure(sdev);
354 struct _MPT_SCSI_HOST *hd =
355 (struct _MPT_SCSI_HOST *)sdev->host->hostdata;
357 if (ret)
358 return ret;
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);
365 return 0;
368 static void mptspi_slave_destroy(struct scsi_device *sdev)
370 struct scsi_target *starget = scsi_target(sdev);
371 VirtTarget *vtarget = starget->hostdata;
372 VirtDevice *vdevice = sdev->hostdata;
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;
378 /* Async Narrow */
379 pg1.RequestedParameters = 0;
380 pg1.Reserved = 0;
381 pg1.Configuration = 0;
383 mptspi_write_spi_device_pg1(starget, &pg1);
386 mptscsih_slave_destroy(sdev);
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,
415 static int mptspi_write_spi_device_pg1(struct scsi_target *starget,
416 struct _CONFIG_PAGE_SCSI_DEVICE_1 *pass_pg1)
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;
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;
433 size = ioc->spi_data.sdp1length * 4;
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;
441 memset(&hdr, 0, sizeof(hdr));
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;
448 memset(&cfg, 0, sizeof(cfg));
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;
456 memcpy(pg1, pass_pg1, size);
458 pg1->Header.PageVersion = hdr.PageVersion;
459 pg1->Header.PageLength = hdr.PageLength;
460 pg1->Header.PageNumber = hdr.PageNumber;
461 pg1->Header.PageType = hdr.PageType;
463 if (mpt_config(ioc, &cfg)) {
464 starget_printk(KERN_ERR, starget, "mpt_config failed\n");
465 goto out_free;
467 err = 0;
469 out_free:
470 dma_free_coherent(&ioc->pcidev->dev, size, pg1, pg1_dma);
471 return err;
474 static void mptspi_write_offset(struct scsi_target *starget, int offset)
476 struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
477 u32 nego;
479 if (offset < 0)
480 offset = 0;
482 if (offset > 255)
483 offset = 255;
485 if (spi_offset(starget) == -1)
486 mptspi_read_parameters(starget);
488 spi_offset(starget) = offset;
490 nego = mptspi_getRP(starget);
492 pg1.RequestedParameters = cpu_to_le32(nego);
493 pg1.Reserved = 0;
494 pg1.Configuration = 0;
496 mptspi_write_spi_device_pg1(starget, &pg1);
499 static void mptspi_write_period(struct scsi_target *starget, int period)
501 struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
502 u32 nego;
504 if (period < 8)
505 period = 8;
507 if (period > 255)
508 period = 255;
510 if (spi_period(starget) == -1)
511 mptspi_read_parameters(starget);
513 if (period == 8) {
514 spi_iu(starget) = 1;
515 spi_dt(starget) = 1;
516 } else if (period == 9) {
517 spi_dt(starget) = 1;
520 spi_period(starget) = period;
522 nego = mptspi_getRP(starget);
524 pg1.RequestedParameters = cpu_to_le32(nego);
525 pg1.Reserved = 0;
526 pg1.Configuration = 0;
528 mptspi_write_spi_device_pg1(starget, &pg1);
531 static void mptspi_write_dt(struct scsi_target *starget, int dt)
533 struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
534 u32 nego;
536 if (spi_period(starget) == -1)
537 mptspi_read_parameters(starget);
539 if (!dt && spi_period(starget) < 10)
540 spi_period(starget) = 10;
542 spi_dt(starget) = dt;
544 nego = mptspi_getRP(starget);
547 pg1.RequestedParameters = cpu_to_le32(nego);
548 pg1.Reserved = 0;
549 pg1.Configuration = 0;
551 mptspi_write_spi_device_pg1(starget, &pg1);
554 static void mptspi_write_iu(struct scsi_target *starget, int iu)
556 struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
557 u32 nego;
559 if (spi_period(starget) == -1)
560 mptspi_read_parameters(starget);
562 if (!iu && spi_period(starget) < 9)
563 spi_period(starget) = 9;
565 spi_iu(starget) = iu;
567 nego = mptspi_getRP(starget);
569 pg1.RequestedParameters = cpu_to_le32(nego);
570 pg1.Reserved = 0;
571 pg1.Configuration = 0;
573 mptspi_write_spi_device_pg1(starget, &pg1);
576 #define MPTSPI_SIMPLE_TRANSPORT_PARM(parm) \
577 static void mptspi_write_##parm(struct scsi_target *starget, int parm)\
579 struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1; \
580 u32 nego; \
582 spi_##parm(starget) = parm; \
584 nego = mptspi_getRP(starget); \
586 pg1.RequestedParameters = cpu_to_le32(nego); \
587 pg1.Reserved = 0; \
588 pg1.Configuration = 0; \
590 mptspi_write_spi_device_pg1(starget, &pg1); \
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)
599 static void mptspi_write_qas(struct scsi_target *starget, int qas)
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;
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;
613 nego = mptspi_getRP(starget);
615 pg1.RequestedParameters = cpu_to_le32(nego);
616 pg1.Reserved = 0;
617 pg1.Configuration = 0;
619 mptspi_write_spi_device_pg1(starget, &pg1);
622 static void mptspi_write_width(struct scsi_target *starget, int width)
624 struct _CONFIG_PAGE_SCSI_DEVICE_1 pg1;
625 u32 nego;
627 if (!width) {
628 spi_dt(starget) = 0;
629 if (spi_period(starget) < 10)
630 spi_period(starget) = 10;
633 spi_width(starget) = width;
635 nego = mptspi_getRP(starget);
637 pg1.RequestedParameters = cpu_to_le32(nego);
638 pg1.Reserved = 0;
639 pg1.Configuration = 0;
641 mptspi_write_spi_device_pg1(starget, &pg1);
644 struct work_queue_wrapper {
645 struct work_struct work;
646 struct _MPT_SCSI_HOST *hd;
647 int disk;
650 static void mpt_work_wrapper(struct work_struct *work)
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;
660 kfree(wqw);
662 mpt_findImVolumes(hd->ioc);
663 pg3 = hd->ioc->raid_data.pIocPg3;
664 if (!pg3)
665 return;
667 shost_for_each_device(sdev,shost) {
668 struct scsi_target *starget = scsi_target(sdev);
669 VirtTarget *vtarget = starget->hostdata;
671 /* only want to search RAID components */
672 if (sdev->channel != 1)
673 continue;
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;
680 starget_printk(KERN_INFO, vtarget->starget,
681 "Integrated RAID requests DV of new device\n");
682 mptspi_dv_device(hd, sdev);
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);
690 static void mpt_dv_raid(struct _MPT_SCSI_HOST *hd, int disk)
692 struct work_queue_wrapper *wqw = kmalloc(sizeof(*wqw), GFP_ATOMIC);
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;
700 INIT_WORK(&wqw->work, mpt_work_wrapper);
701 wqw->hd = hd;
702 wqw->disk = disk;
704 schedule_work(&wqw->work);
707 static int
708 mptspi_event_process(MPT_ADAPTER *ioc, EventNotificationReply_t *pEvReply)
710 u8 event = le32_to_cpu(pEvReply->Event) & 0xFF;
711 struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)ioc->sh->hostdata;
713 if (hd && event == MPI_EVENT_INTEGRATED_RAID) {
714 int reason
715 = (le32_to_cpu(pEvReply->Data[0]) & 0x00FF0000) >> 16;
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);
722 return mptscsih_event_process(ioc, pEvReply);
725 static int
726 mptspi_deny_binding(struct scsi_target *starget)
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;
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,
771 /****************************************************************************
772 * Supported hardware
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 */
782 MODULE_DEVICE_TABLE(pci, mptspi_pci_table);
786 * renegotiate for a given target
788 static void
789 mptspi_dv_renegotiate_work(struct work_struct *work)
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;
796 kfree(wqw);
798 shost_for_each_device(sdev, hd->ioc->sh)
799 mptspi_dv_device(hd, sdev);
802 static void
803 mptspi_dv_renegotiate(struct _MPT_SCSI_HOST *hd)
805 struct work_queue_wrapper *wqw = kmalloc(sizeof(*wqw), GFP_ATOMIC);
807 if (!wqw)
808 return;
810 INIT_WORK(&wqw->work, mptspi_dv_renegotiate_work);
811 wqw->hd = hd;
813 schedule_work(&wqw->work);
817 * spi module reset handler
819 static int
820 mptspi_ioc_reset(MPT_ADAPTER *ioc, int reset_phase)
822 struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)ioc->sh->hostdata;
823 int rc;
825 rc = mptscsih_ioc_reset(ioc, reset_phase);
827 if (reset_phase == MPT_IOC_POST_RESET)
828 mptspi_dv_renegotiate(hd);
830 return rc;
833 #ifdef CONFIG_PM
835 * spi module resume handler
837 static int
838 mptspi_resume(struct pci_dev *pdev)
840 MPT_ADAPTER *ioc = pci_get_drvdata(pdev);
841 struct _MPT_SCSI_HOST *hd = (struct _MPT_SCSI_HOST *)ioc->sh->hostdata;
842 int rc;
844 rc = mptscsih_resume(pdev);
845 mptspi_dv_renegotiate(hd);
847 return rc;
849 #endif
851 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
852 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
854 * mptspi_probe - Installs scsi devices per bus.
855 * @pdev: Pointer to pci_dev structure
857 * Returns 0 for success, non-zero for failure.
860 static int
861 mptspi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
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;
874 if ((r = mpt_attach(pdev,id)) != 0)
875 return r;
877 ioc = pci_get_drvdata(pdev);
878 ioc->DoneCtx = mptspiDoneCtx;
879 ioc->TaskCtx = mptspiTaskCtx;
880 ioc->InternalCtx = mptspiInternalCtx;
882 /* Added sanity check on readiness of the MPT adapter.
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;
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;
899 /* Sanity check - ensure at least 1 port is INITIATOR capable
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 ++;
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;
915 sh = scsi_host_alloc(&mptspi_driver_template, sizeof(MPT_SCSI_HOST));
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;
925 spin_lock_irqsave(&ioc->FreeQlock, flags);
927 /* Attach the SCSI Host to the IOC structure
929 ioc->sh = sh;
931 sh->io_port = 0;
932 sh->n_io_port = 0;
933 sh->irq = 0;
935 /* set 16 byte cdb's */
936 sh->max_cmd_len = 16;
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(
948 sh->max_id = MPT_MAX_SCSI_DEVICES;
950 sh->max_lun = MPT_LAST_LUN + 1;
952 * If RAID Firmware Detected, setup virtual channel
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;
961 /* Required entry.
963 sh->unique_id = ioc->id;
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.
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));
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;
995 spin_unlock_irqrestore(&ioc->FreeQlock, flags);
997 hd = (MPT_SCSI_HOST *) sh->hostdata;
998 hd->ioc = ioc;
1000 /* SCSI needs scsi_cmnd lookup table!
1001 * (with size equal to req_depth*PtrSz!)
1003 hd->ScsiLookup = kcalloc(ioc->req_depth, sizeof(void *), GFP_ATOMIC);
1004 if (!hd->ScsiLookup) {
1005 error = -ENOMEM;
1006 goto out_mptspi_probe;
1009 dprintk((MYIOC_s_INFO_FMT "ScsiLookup @ %p\n",
1010 ioc->name, hd->ScsiLookup));
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)
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;
1024 dprintk((KERN_INFO " vdev @ %p\n", hd->Targets));
1026 /* Clear the TM flags
1028 hd->tmPending = 0;
1029 hd->tmState = TM_STATE_NONE;
1030 hd->resetPending = 0;
1031 hd->abortSCpnt = NULL;
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.
1038 hd->cmdPtr = NULL;
1040 /* Initialize this SCSI Hosts' timers
1041 * To use, set the timer expires field
1042 * and add_timer
1044 init_timer(&hd->timer);
1045 hd->timer.data = (unsigned long) hd;
1046 hd->timer.function = mptscsih_timer_expired;
1048 ioc->spi_data.Saf_Te = mpt_saf_te;
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;
1057 init_waitqueue_head(&hd->scandv_waitq);
1058 hd->scandv_wait_done = 0;
1059 hd->last_queue_full = 0;
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;
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;
1074 * issue internal bus reset
1076 if (ioc->spi_data.bus_reset)
1077 mptscsih_TMHandler(hd,
1078 MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS,
1079 0, 0, 0, 0, 5);
1081 scsi_scan_host(sh);
1082 return 0;
1084 out_mptspi_probe:
1086 mptscsih_remove(pdev);
1087 return error;
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
1102 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1104 * mptspi_init - Register MPT adapter(s) as SCSI host(s) with SCSI mid-layer.
1106 * Returns 0 for success, non-zero for failure.
1108 static int __init
1109 mptspi_init(void)
1111 show_mptmod_ver(my_NAME, my_VERSION);
1113 mptspi_transport_template = spi_attach_transport(&mptspi_transport_functions);
1114 if (!mptspi_transport_template)
1115 return -ENODEV;
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);
1121 if (mpt_event_register(mptspiDoneCtx, mptspi_event_process) == 0) {
1122 devtverboseprintk((KERN_INFO MYNAM
1123 ": Registered for IOC event notifications\n"));
1126 if (mpt_reset_register(mptspiDoneCtx, mptspi_ioc_reset) == 0) {
1127 dprintk((KERN_INFO MYNAM
1128 ": Registered for IOC reset notifications\n"));
1131 return pci_register_driver(&mptspi_driver);
1134 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1135 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
1137 * mptspi_exit - Unregisters MPT adapter(s)
1139 static void __exit
1140 mptspi_exit(void)
1142 pci_unregister_driver(&mptspi_driver);
1144 mpt_reset_deregister(mptspiDoneCtx);
1145 dprintk((KERN_INFO MYNAM
1146 ": Deregistered for IOC reset notifications\n"));
1148 mpt_event_deregister(mptspiDoneCtx);
1149 dprintk((KERN_INFO MYNAM
1150 ": Deregistered for IOC event notifications\n"));
1152 mpt_deregister(mptspiInternalCtx);
1153 mpt_deregister(mptspiTaskCtx);
1154 mpt_deregister(mptspiDoneCtx);
1155 spi_release_transport(mptspi_transport_template);
1158 module_init(mptspi_init);
1159 module_exit(mptspi_exit);