2 ******************************************************************************************
4 ** FILE NAME : arcmsr.c
6 ** Description: SCSI RAID Device Driver for
7 ** ARECA (ARC1110/1120/1160/1210/1220/1260) SATA RAID HOST Adapter
8 ** ARCMSR RAID Host adapter[RAID controller:INTEL 331(PCI-X) 341(PCI-EXPRESS) chip set]
9 ******************************************************************************************
10 ************************************************************************
12 ** Copyright (c) 2004-2006 ARECA Co. Ltd.
\r
13 ** Erich Chen, Taipei Taiwan All rights reserved.
\r
15 ** Redistribution and use in source and binary forms,with or without
16 ** modification,are permitted provided that the following conditions
18 ** 1. Redistributions of source code must retain the above copyright
19 ** notice,this list of conditions and the following disclaimer.
20 ** 2. Redistributions in binary form must reproduce the above copyright
21 ** notice,this list of conditions and the following disclaimer in the
22 ** documentation and/or other materials provided with the distribution.
23 ** 3. The name of the author may not be used to endorse or promote products
24 ** derived from this software without specific prior written permission.
26 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 ** IMPLIED WARRANTIES,INCLUDING,BUT NOT LIMITED TO,THE IMPLIED WARRANTIES
28 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,INDIRECT,
30 ** INCIDENTAL,SPECIAL,EXEMPLARY,OR CONSEQUENTIAL DAMAGES(INCLUDING,BUT
31 ** NOT LIMITED TO,PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA,OR PROFITS; OR BUSINESS INTERRUPTION)HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY,WHETHER IN CONTRACT,STRICT LIABILITY,OR TORT
34 **(INCLUDING NEGLIGENCE OR OTHERWISE)ARISING IN ANY WAY OUT OF THE USE OF
35 ** THIS SOFTWARE,EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 **************************************************************************
39 ** REV# DATE NAME DESCRIPTION
40 ** 1.00.00.00 3/31/2004 Erich Chen First release
41 ** 1.20.00.02 11/29/2004 Erich Chen bug fix with arcmsr_bus_reset when PHY error
42 ******************************************************************************************
45 #define ARCMSR_DEBUG0 0
47 **********************************
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/malloc.h>
52 #include <sys/kernel.h>
54 #include <sys/queue.h>
56 #include <sys/devicestat.h>
57 #include <sys/kthread.h>
58 #include <sys/module.h>
61 #include <sys/sysctl.h>
63 #include <sys/ioccom.h>
65 #include <vm/vm_param.h>
70 #include <machine/bus.h>
71 #include <machine/resource.h>
72 #include <machine/atomic.h>
77 #include <cam/cam_ccb.h>
78 #include <cam/cam_sim.h>
79 #include <cam/cam_xpt_sim.h>
80 #include <cam/cam_debug.h>
81 #include <cam/scsi/scsi_all.h>
82 #include <cam/scsi/scsi_message.h>
84 **************************************************************************
85 ** Define the OS version specific locks
86 **************************************************************************
88 #if __FreeBSD_version >= 500005
89 #include <sys/selinfo.h>
90 #include <sys/mutex.h>
91 #include <dev/pci/pcivar.h>
92 #include <dev/pci/pcireg.h>
93 #define ARCMSR_LOCK_INIT(l, s) mtx_init(l, s,NULL, MTX_DEF|MTX_RECURSE)
94 #define ARCMSR_LOCK_ACQUIRE(l) mtx_lock(l)
95 #define ARCMSR_LOCK_RELEASE(l) mtx_unlock(l)
96 typedef struct mtx arcmsr_lock_t;
98 #include <sys/select.h>
99 #include <pci/pcivar.h>
100 #include <pci/pcireg.h>
101 #define ARCMSR_LOCK_INIT(l, s) simple_lock_init(l)
102 #define ARCMSR_LOCK_ACQUIRE(l) simple_lock(l)
103 #define ARCMSR_LOCK_RELEASE(l) simple_unlock(l)
104 typedef struct simplelock arcmsr_lock_t;
106 #include <dev/arcmsr/arcmsr.h>
108 **************************************************************************
109 ** __FreeBSD_version 502010
110 **************************************************************************
112 static VOID arcmsr_interrupt(VOID *arg);
113 static LONG arcmsr_probe(device_t dev);
114 static LONG arcmsr_attach(device_t dev);
115 static LONG arcmsr_detach(device_t dev);
116 static VOID arcmsr_shutdown(device_t dev);
118 ULONG arcmsr_make_timespec(ULONG year,ULONG mon,ULONG day,ULONG hour,ULONG min,ULONG sec);
119 ULONG arcmsr_getcmos_time(VOID);
121 LONG arcmsr_queue_dpc(PACB pACB,DPCFUN dpcfun,VOID *arg);
122 LONG arcmsr_iop_ioctlcmd(PACB pACB,ULONG ioctl_cmd,caddr_t arg);
123 BOOLEAN arcmsr_seek_cmd2abort(union ccb * pabortccb);
124 BOOLEAN arcmsr_wait_msgint_ready(PACB pACB);
125 PSRB arcmsr_get_freesrb(PACB pACB);
126 VOID arcmsr_free_resource(PACB pACB);
127 VOID arcmsr_bus_reset(PACB pACB);
128 VOID arcmsr_stop_adapter_bgrb(PACB pACB);
129 VOID arcmsr_start_adapter_bgrb(PACB pACB);
130 VOID arcmsr_iop_init(PACB pACB);
131 VOID arcmsr_do_dpcQ(PACB pACB);
132 VOID arcmsr_flush_adapter_cache(PACB pACB);
133 VOID arcmsr_do_thread_works(VOID *arg);
134 VOID arcmsr_queue_wait2go_srb(PACB pACB,PSRB pSRB);
135 VOID arcmsr_post_wait2go_srb(PACB pACB);
136 VOID arcmsr_post_Qbuffer(PACB pACB);
137 VOID arcmsr_abort_allcmd(PACB pACB);
138 VOID arcmsr_srb_complete(PSRB pSRB);
139 VOID arcmsr_iop_reset(PACB pACB);
140 VOID arcmsr_report_SenseInfoBuffer(PSRB pSRB);
141 VOID arcmsr_build_srb(PSRB pSRB, bus_dma_segment_t *dm_segs, LONG nseg);
143 *****************************************************************************************
144 ** Character device switch table
147 ** d_close_t *d_close;
149 ** d_write_t *d_write;
150 ** d_ioctl_t *d_ioctl;
153 ** d_strategy_t *d_strategy;
154 ** const char *d_name; "" base device name, e.g. 'vn'
157 ** d_psize_t *d_psize;
160 ** d_kqfilter_t *d_kqfilter; "" additions below are not binary compatible with 4.2 and below
162 ******************************************************************************************
165 **************************************************************************
166 ** Insert a delay in micro-seconds and milli-seconds.
167 ** static void MDELAY(LONG ms) { while (ms--) UDELAY(1000); }
168 **************************************************************************
170 static VOID UDELAY(LONG us) { DELAY(us); }
172 **************************************************************************
174 **************************************************************************
176 static bus_dmamap_callback_t arcmsr_map_freesrb;
177 static bus_dmamap_callback_t arcmsr_executesrb;
179 **************************************************************************
181 **************************************************************************
183 static d_open_t arcmsr_open;
184 static d_close_t arcmsr_close;
185 static d_ioctl_t arcmsr_ioctl;
187 static device_method_t arcmsr_methods[]={
188 DEVMETHOD(device_probe, arcmsr_probe),
189 DEVMETHOD(device_attach, arcmsr_attach),
190 DEVMETHOD(device_detach, arcmsr_detach),
191 DEVMETHOD(device_shutdown, arcmsr_shutdown),
195 static driver_t arcmsr_driver={
196 "arcmsr",arcmsr_methods,sizeof(struct _ACB)
199 static devclass_t arcmsr_devclass;
200 DRIVER_MODULE(arcmsr,pci,arcmsr_driver,arcmsr_devclass,0,0);
202 #if __FreeBSD_version >= 502010
203 static struct cdevsw arcmsr_cdevsw={
204 .d_version = D_VERSION,
205 .d_flags = D_NEEDGIANT,
206 .d_open = arcmsr_open, /* open */
207 .d_close = arcmsr_close, /* close */
208 .d_ioctl = arcmsr_ioctl, /* ioctl */
209 .d_name = "arcmsr", /* name */
212 #define ARCMSR_CDEV_MAJOR 180
214 static struct cdevsw arcmsr_cdevsw = {
215 arcmsr_open, /* open */
216 arcmsr_close, /* close */
219 arcmsr_ioctl, /* ioctl */
222 nostrategy, /* strategy */
224 ARCMSR_CDEV_MAJOR, /* major */
231 #if __FreeBSD_version < 500005
232 static int arcmsr_open(dev_t dev, int flags, int fmt, struct proc *proc)
234 #if __FreeBSD_version < 503000
235 static int arcmsr_open(dev_t dev, int flags, int fmt, struct thread *proc)
237 static int arcmsr_open(struct cdev *dev, int flags, int fmt, d_thread_t *proc)
241 #if __FreeBSD_version < 503000
242 PACB pACB=dev->si_drv1;
244 int unit = minor(dev);
245 PACB pACB = devclass_get_softc(arcmsr_devclass, unit);
252 /* Check to make sure the device isn't already open */
253 if (pACB->acb_flags & ACB_F_IOCTL_OPEN)
257 pACB->acb_flags |= ACB_F_IOCTL_OPEN;
261 **************************************************************************
262 **************************************************************************
264 #if __FreeBSD_version < 500005
265 static int arcmsr_close(dev_t dev, int flags, int fmt, struct proc *proc)
267 #if __FreeBSD_version < 503000
268 static int arcmsr_close(dev_t dev, int flags, int fmt, struct thread *proc)
270 static int arcmsr_close(struct cdev *dev, int flags, int fmt, d_thread_t *proc)
274 #if __FreeBSD_version < 503000
275 PACB pACB=dev->si_drv1;
277 int unit = minor(dev);
278 PACB pACB = devclass_get_softc(arcmsr_devclass, unit);
285 pACB->acb_flags &= ~ACB_F_IOCTL_OPEN;
289 **************************************************************************
294 **************************************************************************
296 #if __FreeBSD_version < 500005
297 static int arcmsr_ioctl(dev_t dev, u_long ioctl_cmd, caddr_t arg, int flags, struct proc *proc)
299 #if __FreeBSD_version < 503000
300 static int arcmsr_ioctl(dev_t dev, u_long ioctl_cmd, caddr_t arg, int flags, struct thread *proc)
302 static int arcmsr_ioctl(struct cdev *dev, u_long ioctl_cmd, caddr_t arg,int flags, d_thread_t *proc)
306 #if __FreeBSD_version < 503000
307 PACB pACB=dev->si_drv1;
309 int unit = minor(dev);
310 PACB pACB = devclass_get_softc(arcmsr_devclass, unit);
317 return(arcmsr_iop_ioctlcmd(pACB,ioctl_cmd,arg));
320 **************************************************************************
321 **************************************************************************
323 LONG arcmsr_queue_dpc(PACB pACB,DPCFUN dpcfun,VOID *arg)
329 printf("arcmsr_queue_dpc................. \n");
333 index_pointer=(pACB->dpcQ_tail + 1) % ARCMSR_MAX_DPC;
334 if(index_pointer==pACB->dpcQ_head)
337 printf("DPC Queue full!\n");
340 pACB->dpcQ[pACB->dpcQ_tail].dpcfun=dpcfun;
341 pACB->dpcQ[pACB->dpcQ_tail].arg=arg;
342 pACB->dpcQ_tail=index_pointer;
344 *********************************************************
345 *********************************************************
347 wakeup(pACB->kthread_proc);
353 **************************************************************************
355 ** execute dpc routine by kernel thread
356 ***************************************************************************
358 VOID arcmsr_do_dpcQ(PACB pACB)
361 printf("arcmsr_do_dpcQ................. \n");
364 ******************************************
365 ******************************************
367 while (pACB->dpcQ_head!=pACB->dpcQ_tail)
372 /* got a "dpc routine" */
374 dpc=pACB->dpcQ[pACB->dpcQ_head];
376 pACB->dpcQ_head %=ARCMSR_MAX_DPC;
378 /* execute this "dpc routine" */
385 **********************************************************************
386 ** <second> bit 05,04,03,02,01,00: 0 - 59
387 ** <minute> bit 11,10,09,08,07,06: 0 - 59
388 ** <month> bit 15,14,13,12: 1 - 12
389 ** <hour> bit 21,20,19,18,17,16: 0 - 59
390 ** <day> bit 26,25,24,23,22: 1 - 31
391 ** <year> bit 31,30,29,28,27: 0=2000,31=2031
392 **********************************************************************
394 ULONG arcmsr_make_timespec(ULONG year,ULONG mon,ULONG day,ULONG hour,ULONG min,ULONG sec)
396 return((year<<27)|(day<<22)|(hour<<16)|(mon<<12)|(min<<6)|(sec));
399 ********************************************************************
400 ********************************************************************
402 ULONG arcmsr_getcmos_time(VOID)
404 ULONG year,mon,day,hour,min,sec;
407 printf("arcmsr_getcmos_time \n");
409 sec=bcd2bin(rtcin(RTC_SEC));
410 min=bcd2bin(rtcin(RTC_MIN));
411 hour=bcd2bin(rtcin(RTC_HRS));
412 day=bcd2bin(rtcin(RTC_DAY));
413 mon=bcd2bin(rtcin(RTC_MONTH));
414 year=bcd2bin(rtcin(RTC_YEAR));
415 if((year +=1900) < 1970)
417 return arcmsr_make_timespec(year,mon,day,hour,min,sec);
421 *********************************************************************************
422 ** Asynchronous notification handler.
423 *********************************************************************************
425 static VOID arcmsr_async(VOID *cb_arg, ULONG code, struct cam_path *path, VOID *arg)
428 UCHAR target_id,target_lun;
432 printf("arcmsr_async.......................................... \n");
436 sim=(struct cam_sim *) cb_arg;
437 pACB =(PACB) cam_sim_softc(sim);
441 target_id=xpt_path_target_id(path);
442 target_lun=xpt_path_lun_id(path);
443 if((target_id > ARCMSR_MAX_TARGETID) || (target_lun > ARCMSR_MAX_TARGETLUN))
447 printf("%s:scsi id%d lun%d device lost \n",device_get_name(pACB->pci_dev),target_id,target_lun);
455 **************************************************************************
456 * arcmsr_do_thread_works
457 * execute programs schedule by kernel thread
458 * execute programs schedule by kernel thread
459 * :do background rebuilding
461 * tsleep(void *ident,int priority,const char *wmesg,int timo)
463 * General sleep call. Suspends the current process until a wakeup is
464 * performed on the specified identifier. The process will then be made
465 * runnable with the specified priority. Sleeps at most timo/hz seconds
466 * (0 means no timeout). If pri includes PCATCH flag, signals are checked
467 * before and after sleeping, else signals are not checked. Returns 0 if
468 * awakened, EWOULDBLOCK if the timeout expires. If PCATCH is set and a
469 * signal needs to be delivered, ERESTART is returned if the current system
470 * call should be restarted if possible, and EINTR is returned if the system
471 * call should be interrupted by the signal (return EINTR).
473 * await(int priority, int timo)
474 * await() - wait for async condition to occur. The process blocks until
475 * wakeup() is called on the most recent asleep() address. If wakeup is called
476 * priority to await(), await() winds up being a NOP.
478 * If await() is called more then once (without an intervening asleep() call),
479 * await() is still effectively a NOP but it calls mi_switch() to give other
480 * processes some cpu before returning. The process is left runnable.
482 * <<<<<<<< EXPERIMENTAL, UNTESTED >>>>>>>>>>
483 * asleep(void *ident, int priority, const char *wmesg, int timo)
484 * asleep() - async sleep call. Place process on wait queue and return
485 * immediately without blocking. The process stays runnable until await()
486 * is called. If ident is NULL, remove process from wait queue if it is still
489 * Only the most recent sleep condition is effective when making successive
490 * calls to asleep() or when calling tsleep().
492 * The timeout, if any, is not initiated until await() is called. The sleep
493 * priority, signal, and timeout is specified in the asleep() call but may be
494 * overriden in the await() call.
496 * <<<<<<<< EXPERIMENTAL, UNTESTED >>>>>>>>>>
497 * :do background rebuilding
498 ***************************************************************************
500 VOID arcmsr_do_thread_works(VOID *arg)
502 PACB pACB=(PACB) arg;
503 ARCMSR_LOCK_INIT(&pACB->arcmsr_kthread_lock, "arcmsr kthread lock");
506 printf("arcmsr_do_thread_works................. \n");
509 ARCMSR_LOCK_ACQUIRE(&pACB->arcmsr_kthread_lock);
512 tsleep((caddr_t)pACB->kthread_proc, PRIBIO | PWAIT, "arcmsr", hz/4);/*.25 sec*/
514 ** if do_dpcQ_semaphore is signal
515 ** do following works
517 arcmsr_do_dpcQ(pACB); /*see if there were some dpc routine need to execute*/
518 if(pACB->acb_flags & ACB_F_STOP_THREAD)
520 ARCMSR_LOCK_RELEASE(&pACB->arcmsr_kthread_lock);
528 ************************************************************************
531 ************************************************************************
533 VOID arcmsr_flush_adapter_cache(PACB pACB)
536 printf("arcmsr_flush_adapter_cache..............\n");
538 CHIP_REG_WRITE32(&pACB->pmu->inbound_msgaddr0,ARCMSR_INBOUND_MESG0_FLUSH_CACHE);
542 **********************************************************************
546 **********************************************************************
548 BOOLEAN arcmsr_wait_msgint_ready(PACB pACB)
554 for(Index=0; Index < 500000; Index++)
556 if(CHIP_REG_READ32(&pACB->pmu->outbound_intstatus) & ARCMSR_MU_OUTBOUND_MESSAGE0_INT)
558 CHIP_REG_WRITE32(&pACB->pmu->outbound_intstatus, ARCMSR_MU_OUTBOUND_MESSAGE0_INT);/*clear interrupt*/
564 }while(Retries++ < 24);/*max 2 minutes*/
568 **********************************************************************
570 ** Q back this SRB into ACB ArraySRB
572 **********************************************************************
574 VOID arcmsr_srb_complete(PSRB pSRB)
577 PACB pACB=pSRB->pACB;
578 union ccb *pccb=pSRB->pccb;
581 printf("arcmsr_srb_complete: pSRB=%p srb_doneindex=%x srb_startindex=%x\n",pSRB,pACB->srb_doneindex,pACB->srb_startindex);
584 if ((pccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
588 if ((pccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
590 op = BUS_DMASYNC_POSTREAD;
594 op = BUS_DMASYNC_POSTWRITE;
596 bus_dmamap_sync(pACB->buffer_dmat, pSRB->dmamap, op);
597 bus_dmamap_unload(pACB->buffer_dmat, pSRB->dmamap);
600 atomic_subtract_int(&pACB->srboutstandingcount,1);
601 pSRB->startdone=ARCMSR_SRB_DONE;
603 pACB->psrbringQ[pACB->srb_doneindex]=pSRB;
604 pACB->srb_doneindex++;
605 pACB->srb_doneindex %= ARCMSR_MAX_FREESRB_NUM;
611 **********************************************************************
612 ** if scsi error do auto request sense
613 **********************************************************************
615 VOID arcmsr_report_SenseInfoBuffer(PSRB pSRB)
617 union ccb *pccb=pSRB->pccb;
618 PSENSE_DATA psenseBuffer=(PSENSE_DATA)&pccb->csio.sense_data;
620 printf("arcmsr_report_SenseInfoBuffer...........\n");
623 pccb->ccb_h.status|=CAM_REQ_CMP;
626 memset(psenseBuffer, 0, sizeof(pccb->csio.sense_data));
627 memcpy(psenseBuffer,pSRB->arcmsr_cdb.SenseData,get_min(sizeof(struct _SENSE_DATA),sizeof(pccb->csio.sense_data)));
628 psenseBuffer->ErrorCode=0x70;
629 psenseBuffer->Valid=1;
630 pccb->ccb_h.status|=CAM_AUTOSNS_VALID;
635 *********************************************************************
636 ** to insert pSRB into tail of pACB wait exec srbQ
637 *********************************************************************
639 VOID arcmsr_queue_wait2go_srb(PACB pACB,PSRB pSRB)
644 printf("arcmsr_qtail_wait2go_srb:......................................... \n");
650 if(pACB->psrbwait2go[i]==NULL)
652 pACB->psrbwait2go[i]=pSRB;
653 atomic_add_int(&pACB->srbwait2gocount,1);
658 i%=ARCMSR_MAX_OUTSTANDING_CMD;
663 *********************************************************************
665 *********************************************************************
667 VOID arcmsr_abort_allcmd(PACB pACB)
669 CHIP_REG_WRITE32(&pACB->pmu->inbound_msgaddr0,ARCMSR_INBOUND_MESG0_ABORT_CMD);
674 ****************************************************************************
675 ** Routine Description: Reset 80331 iop.
677 ** Return Value: Nothing.
678 ****************************************************************************
680 VOID arcmsr_iop_reset(PACB pACB)
683 ULONG intmask_org,mask;
687 printf("arcmsr_iop_reset: reset iop controller......................................\n");
689 if(pACB->srboutstandingcount!=0)
691 /* Q back all outstanding srb into wait exec psrb Q*/
693 printf("arcmsr_iop_reset: srboutstandingcount=%d ...\n",pACB->srboutstandingcount);
695 /* disable all outbound interrupt */
696 intmask_org=CHIP_REG_READ32(&pACB->pmu->outbound_intmask);
697 CHIP_REG_WRITE32(&pACB->pmu->outbound_intmask,intmask_org|ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE);
698 /* talk to iop 331 outstanding command aborted*/
699 arcmsr_abort_allcmd(pACB);
700 if(arcmsr_wait_msgint_ready(pACB)!=TRUE)
702 printf("arcmsr_iop_reset: wait 'abort all outstanding command' timeout.................in \n");
704 /*clear all outbound posted Q*/
705 for(i=0;i<ARCMSR_MAX_OUTSTANDING_CMD;i++)
707 CHIP_REG_READ32(&pACB->pmu->outbound_queueport);
709 pfreesrb=pACB->pfreesrb;
710 for(i=0;i<ARCMSR_MAX_FREESRB_NUM;i++)
713 if(pSRB->startdone==ARCMSR_SRB_START)
715 pSRB->startdone=ARCMSR_SRB_ABORTED;
716 pSRB->pccb->ccb_h.status=CAM_REQ_ABORTED;
717 arcmsr_srb_complete(pSRB);
720 /* enable all outbound interrupt */
721 mask=~(ARCMSR_MU_OUTBOUND_POSTQUEUE_INTMASKENABLE|ARCMSR_MU_OUTBOUND_DOORBELL_INTMASKENABLE|ARCMSR_MU_OUTBOUND_MESSAGE0_INTMASKENABLE);
722 CHIP_REG_WRITE32(&pACB->pmu->outbound_intmask,intmask_org & mask);
723 atomic_set_int(&pACB->srboutstandingcount,0);
724 /* post abort all outstanding command message to RAID controller */
727 while(pACB->srbwait2gocount > 0)
729 pSRB=pACB->psrbwait2go[i];
733 printf("arcmsr_iop_reset:abort command... srbwait2gocount=%d ...\n",pACB->srbwait2gocount);
735 pACB->psrbwait2go[i]=NULL;
736 pSRB->startdone=ARCMSR_SRB_ABORTED;
737 pSRB->pccb->ccb_h.status=CAM_REQ_ABORTED;
738 arcmsr_srb_complete(pSRB);
739 atomic_subtract_int(&pACB->srbwait2gocount,1);
742 i%=ARCMSR_MAX_OUTSTANDING_CMD;
747 **********************************************************************
749 ** PAGE_SIZE=4096 or 8192,PAGE_SHIFT=12
750 **********************************************************************
752 VOID arcmsr_build_srb(PSRB pSRB, bus_dma_segment_t *dm_segs, LONG nseg)
754 PARCMSR_CDB pARCMSR_CDB=&pSRB->arcmsr_cdb;
755 PCHAR psge=(PCHAR)&pARCMSR_CDB->u;
756 ULONG address_lo,address_hi;
757 union ccb *pccb=pSRB->pccb;
758 struct ccb_scsiio *pcsio=&pccb->csio;
759 LONG arccdbsize=0x30;
762 printf("arcmsr_build_srb........................... \n");
764 memset(pARCMSR_CDB,0,sizeof(struct _ARCMSR_CDB));
766 pARCMSR_CDB->TargetID=pccb->ccb_h.target_id;
767 pARCMSR_CDB->LUN=pccb->ccb_h.target_lun;
768 pARCMSR_CDB->Function=1;
769 pARCMSR_CDB->CdbLength=(UCHAR)pcsio->cdb_len;
770 pARCMSR_CDB->Context=(CPT2INT)pARCMSR_CDB;
771 bcopy(pcsio->cdb_io.cdb_bytes, pARCMSR_CDB->Cdb, pcsio->cdb_len);
774 PACB pACB=pSRB->pACB;
776 LONG length,i,cdb_sgcount=0;
778 /* map stor port SG list to our iop SG List.*/
781 /* Get the physical address of the current data pointer */
782 length=(ULONG) dm_segs[i].ds_len;
783 address_lo=dma_addr_lo32(dm_segs[i].ds_addr);
784 address_hi=dma_addr_hi32(dm_segs[i].ds_addr);
787 PSG32ENTRY pdma_sg=(PSG32ENTRY)psge;
788 pdma_sg->address=address_lo;
789 pdma_sg->length=length;
790 psge += sizeof(SG32ENTRY);
791 arccdbsize += sizeof(SG32ENTRY);
795 LONG sg64s_size=0,tmplength=length;
798 printf("arcmsr_build_srb: !!!!!!!!!!!......address_hi=%x.... \n",address_hi);
802 LONG64 span4G,length0;
803 PSG64ENTRY pdma_sg=(PSG64ENTRY)psge;
805 span4G=(LONG64)address_lo + tmplength;
806 pdma_sg->addresshigh=address_hi;
807 pdma_sg->address=address_lo;
808 if(span4G > 0x100000000)
810 /*see if cross 4G boundary*/
811 length0=0x100000000-address_lo;
812 pdma_sg->length=(ULONG)length0|IS_SG64_ADDR;
813 address_hi=address_hi+1;
815 tmplength=tmplength-(LONG)length0;
816 sg64s_size += sizeof(SG64ENTRY);
817 psge += sizeof(SG64ENTRY);
822 pdma_sg->length=tmplength|IS_SG64_ADDR;
823 sg64s_size += sizeof(SG64ENTRY);
824 psge += sizeof(SG64ENTRY);
828 arccdbsize += sg64s_size;
832 pARCMSR_CDB->sgcount=(UCHAR)cdb_sgcount;
833 pARCMSR_CDB->DataLength=pcsio->dxfer_len;
834 if( arccdbsize > 256)
836 pARCMSR_CDB->Flags|=ARCMSR_CDB_FLAG_SGL_BSIZE;
838 if((pccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
840 op=BUS_DMASYNC_PREREAD;
844 op=BUS_DMASYNC_PREWRITE;
845 pARCMSR_CDB->Flags|=ARCMSR_CDB_FLAG_WRITE;
846 pSRB->srb_flags|=SRB_FLAG_WRITE;
848 bus_dmamap_sync(pACB->buffer_dmat, pSRB->dmamap, op);
851 printf("arcmsr_build_srb: pSRB=%p cmd=%x xferlength=%d arccdbsize=%d sgcount=%d\n",pSRB,pcsio->cdb_io.cdb_bytes[0],pARCMSR_CDB->DataLength,arccdbsize,pARCMSR_CDB->sgcount);
856 **************************************************************************
858 ** arcmsr_post_srb - Send a protocol specific ARC send postcard to a AIOC .
859 ** handle: Handle of registered ARC protocol driver
860 ** adapter_id: AIOC unique identifier(integer)
861 ** pPOSTCARD_SEND: Pointer to ARC send postcard
863 ** This routine posts a ARC send postcard to the request post FIFO of a
864 ** specific ARC adapter.
866 **************************************************************************
868 static VOID arcmsr_post_srb(PACB pACB,PSRB pSRB)
870 ULONG cdb_shifted_phyaddr=(ULONG) pSRB->cdb_shifted_phyaddr;
871 PARCMSR_CDB pARCMSR_CDB=(PARCMSR_CDB)&pSRB->arcmsr_cdb;
874 printf("arcmsr_post_srb: pSRB=%p cdb_shifted_phyaddr=%x\n",pSRB,cdb_shifted_phyaddr);
876 atomic_add_int(&pACB->srboutstandingcount,1);
877 pSRB->startdone=ARCMSR_SRB_START;
878 if(pARCMSR_CDB->Flags & ARCMSR_CDB_FLAG_SGL_BSIZE)
880 CHIP_REG_WRITE32(&pACB->pmu->inbound_queueport,cdb_shifted_phyaddr|ARCMSR_SRBPOST_FLAG_SGL_BSIZE);
884 CHIP_REG_WRITE32(&pACB->pmu->inbound_queueport,cdb_shifted_phyaddr);
889 **************************************************************************
892 **************************************************************************
894 VOID arcmsr_post_wait2go_srb(PACB pACB)
900 printf("arcmsr_post_wait2go_srb:srbwait2gocount=%d srboutstandingcount=%d\n",pACB->srbwait2gocount,pACB->srboutstandingcount);
903 while((pACB->srbwait2gocount > 0) && (pACB->srboutstandingcount < ARCMSR_MAX_OUTSTANDING_CMD))
905 pSRB=pACB->psrbwait2go[i];
908 pACB->psrbwait2go[i]=NULL;
909 arcmsr_post_srb(pACB,pSRB);
910 atomic_subtract_int(&pACB->srbwait2gocount,1);
913 i%=ARCMSR_MAX_OUTSTANDING_CMD;
919 **********************************************************************
920 ** Function: arcmsr_post_Qbuffer
922 **********************************************************************
924 VOID arcmsr_post_Qbuffer(PACB pACB)
928 PQBUFFER pwbuffer=(PQBUFFER)&pACB->pmu->ioctl_wbuffer;
929 PUCHAR iop_data=(PUCHAR)pwbuffer->data;
933 while((pACB->wqbuf_firstindex!=pACB->wqbuf_lastindex) && (allxfer_len<124))
935 pQbuffer=&pACB->wqbuffer[pACB->wqbuf_firstindex];
936 memcpy(iop_data,pQbuffer,1);
937 pACB->wqbuf_firstindex++;
938 pACB->wqbuf_firstindex %= ARCMSR_MAX_QBUFFER; /*if last index number set it to 0 */
942 pwbuffer->data_len=allxfer_len;
944 ** push inbound doorbell and wait reply at hwinterrupt routine for next Qbuffer post
946 CHIP_REG_WRITE32(&pACB->pmu->inbound_doorbell,ARCMSR_INBOUND_DRIVER_DATA_WRITE_OK);
951 ************************************************************************
954 ************************************************************************
956 VOID arcmsr_stop_adapter_bgrb(PACB pACB)
959 printf("arcmsr_stop_adapter_bgrb..............\n");
961 pACB->acb_flags |= ACB_F_MSG_STOP_BGRB;
962 pACB->acb_flags &= ~ACB_F_MSG_START_BGRB;
963 CHIP_REG_WRITE32(&pACB->pmu->inbound_msgaddr0,ARCMSR_INBOUND_MESG0_STOP_BGRB);
967 ************************************************************************
970 ************************************************************************
972 static VOID arcmsr_poll(struct cam_sim * psim)
974 arcmsr_interrupt(cam_sim_softc(psim));
978 **********************************************************************
979 ** Function: arcmsr_interrupt
981 ** CAM Status field values
983 ** CAM_REQ_INPROG, CCB request is in progress
984 ** CAM_REQ_CMP, CCB request completed without error
985 ** CAM_REQ_ABORTED, CCB request aborted by the host
986 ** CAM_UA_ABORT, Unable to abort CCB request
987 ** CAM_REQ_CMP_ERR, CCB request completed with an error
988 ** CAM_BUSY, CAM subsytem is busy
989 ** CAM_REQ_INVALID, CCB request was invalid
990 ** CAM_PATH_INVALID, Supplied Path ID is invalid
991 ** CAM_DEV_NOT_THERE, SCSI Device Not Installed/there
992 ** CAM_UA_TERMIO, Unable to terminate I/O CCB request
993 ** CAM_SEL_TIMEOUT, Target Selection Timeout
994 ** CAM_CMD_TIMEOUT, Command timeout
995 ** CAM_SCSI_STATUS_ERROR, SCSI error, look at error code in CCB
996 ** CAM_MSG_REJECT_REC, Message Reject Received
997 ** CAM_SCSI_BUS_RESET, SCSI Bus Reset Sent/Received
998 ** CAM_UNCOR_PARITY, Uncorrectable parity error occurred
999 ** CAM_AUTOSENSE_FAIL=0x10, Autosense: request sense cmd fail
1000 ** CAM_NO_HBA, No HBA Detected error
1001 ** CAM_DATA_RUN_ERR, Data Overrun error
1002 ** CAM_UNEXP_BUSFREE, Unexpected Bus Free
1003 ** CAM_SEQUENCE_FAIL, Target Bus Phase Sequence Failure
1004 ** CAM_CCB_LEN_ERR, CCB length supplied is inadequate
1005 ** CAM_PROVIDE_FAIL, Unable to provide requested capability
1006 ** CAM_BDR_SENT, A SCSI BDR msg was sent to target
1007 ** CAM_REQ_TERMIO, CCB request terminated by the host
1008 ** CAM_UNREC_HBA_ERROR, Unrecoverable Host Bus Adapter Error
1009 ** CAM_REQ_TOO_BIG, The request was too large for this host
1011 ** * This request should be requeued to preserve
1012 ** * transaction ordering. This typically occurs
1013 ** * when the SIM recognizes an error that should
1014 ** * freeze the queue and must place additional
1015 ** * requests for the target at the sim level
1016 ** * back into the XPT queue.
1018 ** CAM_IDE=0x33, Initiator Detected Error
1019 ** CAM_RESRC_UNAVAIL, Resource Unavailable
1020 ** CAM_UNACKED_EVENT, Unacknowledged Event by Host
1021 ** CAM_MESSAGE_RECV, Message Received in Host Target Mode
1022 ** CAM_INVALID_CDB, Invalid CDB received in Host Target Mode
1023 ** CAM_LUN_INVALID, Lun supplied is invalid
1024 ** CAM_TID_INVALID, Target ID supplied is invalid
1025 ** CAM_FUNC_NOTAVAIL, The requested function is not available
1026 ** CAM_NO_NEXUS, Nexus is not established
1027 ** CAM_IID_INVALID, The initiator ID is invalid
1028 ** CAM_CDB_RECVD, The SCSI CDB has been received
1029 ** CAM_LUN_ALRDY_ENA, The LUN is already eanbeld for target mode
1030 ** CAM_SCSI_BUSY, SCSI Bus Busy
1032 ** CAM_DEV_QFRZN=0x40, The DEV queue is frozen w/this err
1034 ** Autosense data valid for target
1035 ** CAM_AUTOSNS_VALID=0x80,
1036 ** CAM_RELEASE_SIMQ=0x100, SIM ready to take more commands
1037 ** CAM_SIM_QUEUED =0x200, SIM has this command in it's queue
1039 ** CAM_STATUS_MASK=0x3F, Mask bits for just the status #
1041 ** Target Specific Adjunct Status
1042 ** CAM_SENT_SENSE=0x40000000 sent sense with status
1044 **********************************************************************
1046 static VOID arcmsr_interrupt(VOID *arg)
1048 PACB pACB=(PACB)arg;
1050 ULONG flagpsrb,outbound_intstatus,outbound_doorbell;
1053 printf("arcmsr_interrupt..............\n");
1056 *********************************************
1057 ** check outbound intstatus À˹µL¶l®t«öªù¹a
1058 *********************************************
1060 outbound_intstatus=CHIP_REG_READ32(&pACB->pmu->outbound_intstatus) & pACB->outbound_int_enable;
1061 CHIP_REG_WRITE32(&pACB->pmu->outbound_intstatus, outbound_intstatus);/*clear interrupt*/
1062 if(outbound_intstatus & ARCMSR_MU_OUTBOUND_DOORBELL_INT)
1065 printf("arcmsr_interrupt:..........ARCMSR_MU_OUTBOUND_DOORBELL_INT\n");
1068 *********************************************
1069 ** DOORBELL ¥m¾´! ¬O§_¦³¶l¥ónñ¦¬
1070 *********************************************
1072 outbound_doorbell=CHIP_REG_READ32(&pACB->pmu->outbound_doorbell);
1073 CHIP_REG_WRITE32(&pACB->pmu->outbound_doorbell,outbound_doorbell);/*clear interrupt */
1074 if(outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_WRITE_OK)
1076 PQBUFFER prbuffer=(PQBUFFER)&pACB->pmu->ioctl_rbuffer;
1077 PUCHAR iop_data=(PUCHAR)prbuffer->data;
1079 LONG my_empty_len,iop_len,rqbuf_firstindex,rqbuf_lastindex;
1081 /*check this iop data if overflow my rqbuffer*/
1083 rqbuf_lastindex=pACB->rqbuf_lastindex;
1084 rqbuf_firstindex=pACB->rqbuf_firstindex;
1085 iop_len=prbuffer->data_len;
1086 my_empty_len=(rqbuf_firstindex-rqbuf_lastindex-1)&(ARCMSR_MAX_QBUFFER-1);
1087 if(my_empty_len>=iop_len)
1091 pQbuffer=&pACB->rqbuffer[pACB->rqbuf_lastindex];
1092 memcpy(pQbuffer,iop_data,1);
1093 pACB->rqbuf_lastindex++;
1094 pACB->rqbuf_lastindex %= ARCMSR_MAX_QBUFFER;/*if last index number set it to 0 */
1098 CHIP_REG_WRITE32(&pACB->pmu->inbound_doorbell, ARCMSR_INBOUND_DRIVER_DATA_READ_OK);/*signature, let IOP331 know data has been readed */
1102 pACB->acb_flags|=ACB_F_IOPDATA_OVERFLOW;
1106 if(outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_READ_OK)
1110 *********************************************
1111 ** ¬Ý¬Ý¬O§_ÁÙ¦³¶l¥ón¶¶¹D±H¥X
1112 *********************************************
1115 if(pACB->wqbuf_firstindex!=pACB->wqbuf_lastindex)
1118 PQBUFFER pwbuffer=(PQBUFFER)&pACB->pmu->ioctl_wbuffer;
1119 PUCHAR iop_data=(PUCHAR)pwbuffer->data;
1122 while((pACB->wqbuf_firstindex!=pACB->wqbuf_lastindex) && (allxfer_len<124))
1124 pQbuffer=&pACB->wqbuffer[pACB->wqbuf_firstindex];
1125 memcpy(iop_data,pQbuffer,1);
1126 pACB->wqbuf_firstindex++;
1127 pACB->wqbuf_firstindex %= ARCMSR_MAX_QBUFFER; /*if last index number set it to 0 */
1131 pwbuffer->data_len=allxfer_len;
1133 ** push inbound doorbell tell iop driver data write ok and wait reply on next hwinterrupt for next Qbuffer post
1135 CHIP_REG_WRITE32(&pACB->pmu->inbound_doorbell,ARCMSR_INBOUND_DRIVER_DATA_WRITE_OK);
1139 pACB->acb_flags |= ACB_F_IOCTL_WQBUFFER_CLEARED;
1144 if(outbound_intstatus & ARCMSR_MU_OUTBOUND_POSTQUEUE_INT)
1147 *****************************************************************************
1148 ** areca cdb command done
1149 *****************************************************************************
1153 if((flagpsrb=CHIP_REG_READ32(&pACB->pmu->outbound_queueport)) == 0xFFFFFFFF)
1155 break;/*chip FIFO no srb for completion already*/
1157 /* check if command done with no error*/
1158 pSRB=(PSRB)(CINT2P)(pACB->vir2phy_offset+(flagpsrb << 5));/*frame must be 32 bytes aligned*/
1159 if((pSRB->pACB!=pACB) || (pSRB->startdone!=ARCMSR_SRB_START))
1161 if(pSRB->startdone==ARCMSR_SRB_ABORTED)
1163 pSRB->pccb->ccb_h.status=CAM_REQ_ABORTED;
1164 arcmsr_srb_complete(pSRB);
1167 printf("arcmsr_interrupt:got an illegal srb command done ...pACB=%p pSRB=%p srboutstandingcount=%d .....\n",pACB,pSRB,pACB->srboutstandingcount);
1170 if((flagpsrb & ARCMSR_SRBREPLY_FLAG_ERROR)==0)
1172 pSRB->pccb->ccb_h.status=CAM_REQ_CMP;
1173 arcmsr_srb_complete(pSRB);
1177 switch(pSRB->arcmsr_cdb.DeviceStatus)
1179 case ARCMSR_DEV_SELECT_TIMEOUT:
1182 printf("pSRB=%p ......ARCMSR_DEV_SELECT_TIMEOUT\n",pSRB);
1184 pSRB->pccb->ccb_h.status=CAM_SEL_TIMEOUT;
1185 arcmsr_srb_complete(pSRB);
1188 case ARCMSR_DEV_ABORTED:
1191 printf("pSRB=%p ......ARCMSR_DEV_ABORTED\n",pSRB);
1193 pSRB->pccb->ccb_h.status=CAM_DEV_NOT_THERE;
1194 arcmsr_srb_complete(pSRB);
1197 case ARCMSR_DEV_INIT_FAIL:
1200 printf("pSRB=%p .....ARCMSR_DEV_INIT_FAIL\n",pSRB);
1202 pSRB->pccb->ccb_h.status=CAM_DEV_NOT_THERE;
1203 arcmsr_srb_complete(pSRB);
1206 case SCSISTAT_CHECK_CONDITION:
1209 printf("pSRB=%p .....SCSISTAT_CHECK_CONDITION\n",pSRB);
1211 arcmsr_report_SenseInfoBuffer(pSRB);
1212 arcmsr_srb_complete(pSRB);
1216 /* error occur Q all error srb to errorsrbpending Q*/
1217 printf("arcmsr_interrupt:command error done ......but got unknow DeviceStatus=%x....\n",pSRB->arcmsr_cdb.DeviceStatus);
1218 pSRB->pccb->ccb_h.status=CAM_UNCOR_PARITY;/*unknow error or crc error just for retry*/
1219 arcmsr_srb_complete(pSRB);
1223 } /*drain reply FIFO*/
1225 if(pACB->srbwait2gocount != 0)
1227 arcmsr_post_wait2go_srb(pACB);/*try to post all pending srb*/
1232 ***********************************************************************
1234 **int copyin __P((const void *udaddr, void *kaddr, size_t len));
1235 **int copyout __P((const void *kaddr, void *udaddr, size_t len));
1237 **ENOENT "" No such file or directory ""
1238 **ENOIOCTL "" ioctl not handled by this layer ""
1239 **ENOMEM "" Cannot allocate memory ""
1240 **EINVAL "" Invalid argument ""
1241 ************************************************************************
1243 LONG arcmsr_iop_ioctlcmd(PACB pACB,ULONG ioctl_cmd,caddr_t arg)
1245 PCMD_IO_CONTROL pccbioctl=(PCMD_IO_CONTROL) arg;
1248 printf("arcmsr_iop_ioctlcmd................. \n");
1251 if(memcmp(pccbioctl->Signature,"ARCMSR",6)!=0)
1257 case ARCMSR_IOCTL_READ_RQBUFFER:
1260 PCMD_IOCTL_FIELD pccbioctlfld=(PCMD_IOCTL_FIELD)arg;
1261 PUCHAR pQbuffer,ptmpQbuffer=pccbioctlfld->ioctldatabuffer;
1265 while((pACB->rqbuf_firstindex!=pACB->rqbuf_lastindex) && (allxfer_len<1031))
1267 /*copy READ QBUFFER to srb*/
1268 pQbuffer=&pACB->rqbuffer[pACB->rqbuf_firstindex];
1269 memcpy(ptmpQbuffer,pQbuffer,1);
1270 pACB->rqbuf_firstindex++;
1271 pACB->rqbuf_firstindex %= ARCMSR_MAX_QBUFFER; /*if last index number set it to 0 */
1275 if(pACB->acb_flags & ACB_F_IOPDATA_OVERFLOW)
1277 PQBUFFER prbuffer=(PQBUFFER)&pACB->pmu->ioctl_rbuffer;
1279 PUCHAR iop_data=(PUCHAR)prbuffer->data;
1282 pACB->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1283 iop_len=(LONG)prbuffer->data_len;
1284 /*this iop data does no chance to make me overflow again here, so just do it*/
1287 pQbuffer=&pACB->rqbuffer[pACB->rqbuf_lastindex];
1288 memcpy(pQbuffer,iop_data,1);
1289 pACB->rqbuf_lastindex++;
1290 pACB->rqbuf_lastindex %= ARCMSR_MAX_QBUFFER;/*if last index number set it to 0 */
1294 CHIP_REG_WRITE32(&pACB->pmu->inbound_doorbell, ARCMSR_INBOUND_DRIVER_DATA_READ_OK);/*signature, let IOP331 know data has been readed */
1296 pccbioctl->Length=allxfer_len;
1297 pccbioctl->ReturnCode=ARCMSR_IOCTL_RETURNCODE_OK;
1299 return ARC_IOCTL_SUCCESS;
1302 case ARCMSR_IOCTL_WRITE_WQBUFFER:
1305 PCMD_IOCTL_FIELD pccbioctlfld=(PCMD_IOCTL_FIELD)arg;
1306 LONG my_empty_len,user_len,wqbuf_firstindex,wqbuf_lastindex;
1307 PUCHAR pQbuffer,ptmpuserbuffer=pccbioctlfld->ioctldatabuffer;
1310 user_len=pccbioctl->Length;
1312 /*check if data xfer length of this request will overflow my array qbuffer */
1313 wqbuf_lastindex=pACB->wqbuf_lastindex;
1314 wqbuf_firstindex=pACB->wqbuf_firstindex;
1315 my_empty_len=(wqbuf_firstindex-wqbuf_lastindex-1)&(ARCMSR_MAX_QBUFFER-1);
1316 if(my_empty_len>=user_len)
1320 /*copy srb data to wqbuffer*/
1321 pQbuffer=&pACB->wqbuffer[pACB->wqbuf_lastindex];
1322 memcpy(pQbuffer,ptmpuserbuffer,1);
1323 pACB->wqbuf_lastindex++;
1324 pACB->wqbuf_lastindex %= ARCMSR_MAX_QBUFFER;/*if last index number set it to 0 */
1328 /*post fist Qbuffer*/
1329 if(pACB->acb_flags & ACB_F_IOCTL_WQBUFFER_CLEARED)
1331 pACB->acb_flags &=~ACB_F_IOCTL_WQBUFFER_CLEARED;
1332 arcmsr_post_Qbuffer(pACB);
1334 pccbioctl->ReturnCode=ARCMSR_IOCTL_RETURNCODE_OK;
1338 pccbioctl->ReturnCode=ARCMSR_IOCTL_RETURNCODE_ERROR;
1341 return ARC_IOCTL_SUCCESS;
1344 case ARCMSR_IOCTL_CLEAR_RQBUFFER:
1347 PUCHAR pQbuffer=pACB->rqbuffer;
1349 if(pACB->acb_flags & ACB_F_IOPDATA_OVERFLOW)
1351 pACB->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1352 CHIP_REG_WRITE32(&pACB->pmu->inbound_doorbell, ARCMSR_INBOUND_DRIVER_DATA_READ_OK);/*signature, let IOP331 know data has been readed */
1354 pACB->acb_flags |= ACB_F_IOCTL_RQBUFFER_CLEARED;
1355 pACB->rqbuf_firstindex=0;
1356 pACB->rqbuf_lastindex=0;
1357 memset(pQbuffer, 0, ARCMSR_MAX_QBUFFER);
1358 pccbioctl->ReturnCode=ARCMSR_IOCTL_RETURNCODE_OK;
1360 return ARC_IOCTL_SUCCESS;
1363 case ARCMSR_IOCTL_CLEAR_WQBUFFER:
1366 PUCHAR pQbuffer=pACB->wqbuffer;
1369 if(pACB->acb_flags & ACB_F_IOPDATA_OVERFLOW)
1371 pACB->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1372 CHIP_REG_WRITE32(&pACB->pmu->inbound_doorbell, ARCMSR_INBOUND_DRIVER_DATA_READ_OK);/*signature, let IOP331 know data has been readed */
1374 pACB->acb_flags |= ACB_F_IOCTL_WQBUFFER_CLEARED;
1375 pACB->wqbuf_firstindex=0;
1376 pACB->wqbuf_lastindex=0;
1377 memset(pQbuffer, 0, ARCMSR_MAX_QBUFFER);
1378 pccbioctl->ReturnCode=ARCMSR_IOCTL_RETURNCODE_OK;
1380 return ARC_IOCTL_SUCCESS;
1383 case ARCMSR_IOCTL_CLEAR_ALLQBUFFER:
1389 if(pACB->acb_flags & ACB_F_IOPDATA_OVERFLOW)
1391 pACB->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
1392 CHIP_REG_WRITE32(&pACB->pmu->inbound_doorbell, ARCMSR_INBOUND_DRIVER_DATA_READ_OK);/*signature, let IOP331 know data has been readed */
1394 pACB->acb_flags |= (ACB_F_IOCTL_WQBUFFER_CLEARED|ACB_F_IOCTL_RQBUFFER_CLEARED);
1395 pACB->rqbuf_firstindex=0;
1396 pACB->rqbuf_lastindex=0;
1397 pACB->wqbuf_firstindex=0;
1398 pACB->wqbuf_lastindex=0;
1399 pQbuffer=pACB->rqbuffer;
1400 memset(pQbuffer, 0, sizeof(struct _QBUFFER));
1401 pQbuffer=pACB->wqbuffer;
1402 memset(pQbuffer, 0, sizeof(struct _QBUFFER));
1403 pccbioctl->ReturnCode=ARCMSR_IOCTL_RETURNCODE_OK;
1405 return ARC_IOCTL_SUCCESS;
1408 case ARCMSR_IOCTL_RETURN_CODE_3F:
1410 pccbioctl->ReturnCode=ARCMSR_IOCTL_RETURNCODE_3F;
1411 return ARC_IOCTL_SUCCESS;
1414 case ARCMSR_IOCTL_SAY_HELLO:
1416 PCMD_IOCTL_FIELD pccbioctlfld=(PCMD_IOCTL_FIELD)arg;
1417 PCHAR hello_string="Hello! I am ARCMSR";
1418 PCHAR puserbuffer=(PUCHAR)pccbioctlfld->ioctldatabuffer;
1420 if(memcpy(puserbuffer,hello_string,(SHORT)strlen(hello_string)))
1422 pccbioctl->ReturnCode=ARCMSR_IOCTL_RETURNCODE_ERROR;
1425 pccbioctl->ReturnCode=ARCMSR_IOCTL_RETURNCODE_OK;
1426 return ARC_IOCTL_SUCCESS;
1433 **************************************************************************
1435 **************************************************************************
1437 PSRB arcmsr_get_freesrb(PACB pACB)
1441 LONG srb_startindex,srb_doneindex;
1444 printf("arcmsr_get_freesrb: srb_startindex=%d srb_doneindex=%d\n",pACB->srb_startindex,pACB->srb_doneindex);
1448 srb_doneindex=pACB->srb_doneindex;
1449 srb_startindex=pACB->srb_startindex;
1450 pSRB=pACB->psrbringQ[srb_startindex];
1452 srb_startindex %= ARCMSR_MAX_FREESRB_NUM;
1453 if(srb_doneindex!=srb_startindex)
1455 pACB->srb_startindex=srb_startindex;
1465 *********************************************************************
1469 *********************************************************************
1471 static VOID arcmsr_executesrb(VOID *arg,bus_dma_segment_t *dm_segs,LONG nseg,LONG error)
1473 PSRB pSRB=(PSRB)arg;
1478 printf("arcmsr_executesrb........................................ \n" );
1482 pACB=(PACB)pSRB->pACB;
1487 printf("arcmsr_executesrb:%d Unexepected error %x returned from " "bus_dmamap_load\n",pACB->pci_unit,error);
1489 if(pccb->ccb_h.status == CAM_REQ_INPROG)
1491 xpt_freeze_devq(pccb->ccb_h.path,/*count*/1);
1492 pccb->ccb_h.status=CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
1497 arcmsr_build_srb(pSRB,dm_segs,nseg);
1498 if((pccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG)
1502 bus_dmamap_unload(pACB->buffer_dmat,pSRB->dmamap);
1507 pccb->ccb_h.status |= CAM_SIM_QUEUED;
1508 if(pACB->srboutstandingcount < ARCMSR_MAX_OUTSTANDING_CMD)
1511 ******************************************************************
1512 ** and we can make sure there were no pending srb in this duration
1513 ******************************************************************
1515 arcmsr_post_srb(pACB,pSRB);
1520 ******************************************************************
1521 ** Q of srbwaitexec will be post out when any outstanding command complete
1522 ******************************************************************
1524 arcmsr_queue_wait2go_srb(pACB,pSRB);
1529 *****************************************************************************************
1531 *****************************************************************************************
1533 BOOLEAN arcmsr_seek_cmd2abort(union ccb * pabortccb)
1536 PACB pACB=(PACB) pabortccb->ccb_h.arcmsr_ccbacb_ptr;
1537 ULONG s,intmask_org,mask;
1541 printf("arcmsr_seek_cmd2abort.................. \n");
1546 ** It is the upper layer do abort command this lock just prior to calling us.
1547 ** First determine if we currently own this command.
1548 ** Start by searching the device queue. If not found
1549 ** at all,and the system wanted us to just abort the
1550 ** command return success.
1552 if(pACB->srboutstandingcount!=0)
1554 /* Q back all outstanding srb into wait exec psrb Q*/
1555 pfreesrb=pACB->pfreesrb;
1556 for(i=0;i<ARCMSR_MAX_FREESRB_NUM;i++)
1559 if(pSRB->startdone==ARCMSR_SRB_START)
1561 if(pSRB->pccb==pabortccb)
1563 /* disable all outbound interrupt */
1564 intmask_org=CHIP_REG_READ32(&pACB->pmu->outbound_intmask);
1565 CHIP_REG_WRITE32(&pACB->pmu->outbound_intmask,intmask_org|ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE);
1566 /* talk to iop 331 outstanding command aborted*/
1567 arcmsr_abort_allcmd(pACB);
1568 if(arcmsr_wait_msgint_ready(pACB)!=TRUE)
1570 printf("arcmsr_seek_cmd2abort: wait 'abort all outstanding command' timeout.................in \n");
1572 /*clear all outbound posted Q*/
1573 for(i=0;i<ARCMSR_MAX_OUTSTANDING_CMD;i++)
1575 CHIP_REG_READ32(&pACB->pmu->outbound_queueport);
1577 pfreesrb=pACB->pfreesrb;
1578 for(i=0;i<ARCMSR_MAX_FREESRB_NUM;i++)
1581 if(pSRB->startdone==ARCMSR_SRB_START)
1583 pSRB->startdone=ARCMSR_SRB_ABORTED;
1584 pSRB->pccb->ccb_h.status=CAM_REQ_ABORTED;
1585 arcmsr_srb_complete(pSRB);
1588 /* enable all outbound interrupt */
1589 mask=~(ARCMSR_MU_OUTBOUND_POSTQUEUE_INTMASKENABLE|ARCMSR_MU_OUTBOUND_DOORBELL_INTMASKENABLE|ARCMSR_MU_OUTBOUND_MESSAGE0_INTMASKENABLE);
1590 CHIP_REG_WRITE32(&pACB->pmu->outbound_intmask,intmask_org & mask);
1598 ** seek this command at our command list
1599 ** if command found then remove,abort it and free this SRB
1601 if(pACB->srbwait2gocount!=0)
1603 for(i=0;i<ARCMSR_MAX_OUTSTANDING_CMD;i++)
1605 pSRB=pACB->psrbwait2go[i];
1608 if(pSRB->pccb==pabortccb)
1610 pACB->psrbwait2go[i]=NULL;
1611 pSRB->startdone=ARCMSR_SRB_ABORTED;
1612 pSRB->pccb->ccb_h.status=CAM_REQ_ABORTED;
1613 arcmsr_srb_complete(pSRB);
1614 atomic_subtract_int(&pACB->srbwait2gocount,1);
1625 ****************************************************************************
1627 ****************************************************************************
1629 VOID arcmsr_bus_reset(PACB pACB)
1632 printf("arcmsr_bus_reset.......................... \n");
1635 arcmsr_iop_reset(pACB);
1639 *********************************************************************
1641 ** CAM Status field values
1643 ** CAM_REQ_INPROG, CCB request is in progress
1644 ** CAM_REQ_CMP, CCB request completed without error
1645 ** CAM_REQ_ABORTED, CCB request aborted by the host
1646 ** CAM_UA_ABORT, Unable to abort CCB request
1647 ** CAM_REQ_CMP_ERR, CCB request completed with an error
1648 ** CAM_BUSY, CAM subsytem is busy
1649 ** CAM_REQ_INVALID, CCB request was invalid
1650 ** CAM_PATH_INVALID, Supplied Path ID is invalid
1651 ** CAM_DEV_NOT_THERE, SCSI Device Not Installed/there
1652 ** CAM_UA_TERMIO, Unable to terminate I/O CCB request
1653 ** CAM_SEL_TIMEOUT, Target Selection Timeout
1654 ** CAM_CMD_TIMEOUT, Command timeout
1655 ** CAM_SCSI_STATUS_ERROR, SCSI error, look at error code in CCB
1656 ** CAM_MSG_REJECT_REC, Message Reject Received
1657 ** CAM_SCSI_BUS_RESET, SCSI Bus Reset Sent/Received
1658 ** CAM_UNCOR_PARITY, Uncorrectable parity error occurred
1659 ** CAM_AUTOSENSE_FAIL=0x10, Autosense: request sense cmd fail
1660 ** CAM_NO_HBA, No HBA Detected error
1661 ** CAM_DATA_RUN_ERR, Data Overrun error
1662 ** CAM_UNEXP_BUSFREE, Unexpected Bus Free
1663 ** CAM_SEQUENCE_FAIL, Target Bus Phase Sequence Failure
1664 ** CAM_CCB_LEN_ERR, CCB length supplied is inadequate
1665 ** CAM_PROVIDE_FAIL, Unable to provide requested capability
1666 ** CAM_BDR_SENT, A SCSI BDR msg was sent to target
1667 ** CAM_REQ_TERMIO, CCB request terminated by the host
1668 ** CAM_UNREC_HBA_ERROR, Unrecoverable Host Bus Adapter Error
1669 ** CAM_REQ_TOO_BIG, The request was too large for this host
1671 ** * This request should be requeued to preserve
1672 ** * transaction ordering. This typically occurs
1673 ** * when the SIM recognizes an error that should
1674 ** * freeze the queue and must place additional
1675 ** * requests for the target at the sim level
1676 ** * back into the XPT queue.
1678 ** CAM_IDE=0x33, Initiator Detected Error
1679 ** CAM_RESRC_UNAVAIL, Resource Unavailable
1680 ** CAM_UNACKED_EVENT, Unacknowledged Event by Host
1681 ** CAM_MESSAGE_RECV, Message Received in Host Target Mode
1682 ** CAM_INVALID_CDB, Invalid CDB received in Host Target Mode
1683 ** CAM_LUN_INVALID, Lun supplied is invalid
1684 ** CAM_TID_INVALID, Target ID supplied is invalid
1685 ** CAM_FUNC_NOTAVAIL, The requested function is not available
1686 ** CAM_NO_NEXUS, Nexus is not established
1687 ** CAM_IID_INVALID, The initiator ID is invalid
1688 ** CAM_CDB_RECVD, The SCSI CDB has been received
1689 ** CAM_LUN_ALRDY_ENA, The LUN is already eanbeld for target mode
1690 ** CAM_SCSI_BUSY, SCSI Bus Busy
1692 ** CAM_DEV_QFRZN=0x40, The DEV queue is frozen w/this err
1694 ** Autosense data valid for target
1695 ** CAM_AUTOSNS_VALID=0x80,
1696 ** CAM_RELEASE_SIMQ=0x100, SIM ready to take more commands
1697 ** CAM_SIM_QUEUED =0x200, SIM has this command in it's queue
1699 ** CAM_STATUS_MASK=0x3F, Mask bits for just the status #
1701 ** Target Specific Adjunct Status
1702 ** CAM_SENT_SENSE=0x40000000 sent sense with status
1706 ** struct ccb_hdr ccb_h; For convenience
1707 ** struct ccb_scsiio csio;
1708 ** struct ccb_getdev cgd;
1709 ** struct ccb_getdevlist cgdl;
1710 ** struct ccb_pathinq cpi;
1711 ** struct ccb_relsim crs;
1712 ** struct ccb_setasync csa;
1713 ** struct ccb_setdev csd;
1714 ** struct ccb_pathstats cpis;
1715 ** struct ccb_getdevstats cgds;
1716 ** struct ccb_dev_match cdm;
1717 ** struct ccb_trans_settings cts;
1718 ** struct ccb_calc_geometry ccg;
1719 ** struct ccb_abort cab;
1720 ** struct ccb_resetbus crb;
1721 ** struct ccb_resetdev crd;
1722 ** struct ccb_termio tio;
1723 ** struct ccb_accept_tio atio;
1724 ** struct ccb_scsiio ctio;
1725 ** struct ccb_en_lun cel;
1726 ** struct ccb_immed_notify cin;
1727 ** struct ccb_notify_ack cna;
1728 ** struct ccb_eng_inq cei;
1729 ** struct ccb_eng_exec cee;
1730 ** struct ccb_rescan crcn;
1731 ** struct ccb_debug cdbg;
1735 ** cam_pinfo pinfo; "" Info for priority scheduling
1736 ** camq_entry xpt_links; "" For chaining in the XPT layer
1737 ** camq_entry sim_links; "" For chaining in the SIM layer
1738 ** camq_entry periph_links; "" For chaining in the type driver
1739 ** u_int32_t retry_count;
1740 ** void (*cbfcnp)(struct cam_periph *, union ccb *);"" Callback on completion function
1741 ** xpt_opcode func_code; "" XPT function code
1742 ** u_int32_t status; "" Status returned by CAM subsystem
1743 ** struct cam_path *path; "" Compiled path for this ccb
1744 ** path_id_t path_id; "" Path ID for the request
1745 ** target_id_t target_id; "" Target device ID
1746 ** lun_id_t target_lun; "" Target LUN number
1748 ** ccb_ppriv_area periph_priv;
1749 ** ccb_spriv_area sim_priv;
1750 ** u_int32_t timeout; "" Timeout value
1751 ** struct callout_handle timeout_ch; "" Callout handle used for timeouts
1755 ** u_int8_t *cdb_ptr; "" Pointer to the CDB bytes to send
1756 ** u_int8_t cdb_bytes[IOCDBLEN]; "" Area for the CDB send
1759 ** SCSI I/O Request CCB used for the XPT_SCSI_IO and XPT_CONT_TARGET_IO
1762 **struct ccb_scsiio {
1763 ** struct ccb_hdr ccb_h;
1764 ** union ccb *next_ccb; "" Ptr for next CCB for action
1765 ** u_int8_t *req_map; "" Ptr to mapping info
1766 ** u_int8_t *data_ptr; "" Ptr to the data buf/SG list
1767 ** u_int32_t dxfer_len; "" Data transfer length
1768 ** struct scsi_sense_data sense_data; "" Autosense storage
1769 ** u_int8_t sense_len; "" Number of bytes to autosense
1770 ** u_int8_t cdb_len; "" Number of bytes for the CDB
1771 ** u_int16_t sglist_cnt; "" Number of SG list entries
1772 ** u_int8_t scsi_status; "" Returned SCSI status
1773 ** u_int8_t sense_resid; "" Autosense resid length: 2's comp
1774 ** u_int32_t resid; "" Transfer residual length: 2's comp
1775 ** cdb_t cdb_io; "" Union for CDB bytes/pointer
1776 ** u_int8_t *msg_ptr; "" Pointer to the message buffer
1777 ** u_int16_t msg_len; "" Number of bytes for the Message
1778 ** u_int8_t tag_action; "" What to do for tag queueing
1779 **#define CAM_TAG_ACTION_NONE 0x00 "" The tag action should be either the define below (to send a non-tagged transaction) or one of the defined scsi tag messages from scsi_message.h.
1780 ** u_int tag_id; "" tag id from initator (target mode)
1781 ** u_int init_id; "" initiator id of who selected
1783 *********************************************************************
1785 static VOID arcmsr_action(struct cam_sim * psim,union ccb * pccb)
1790 printf("arcmsr_action ..................................\n" );
1793 pACB=(PACB) cam_sim_softc(psim);
1796 pccb->ccb_h.status=CAM_REQ_INVALID;
1800 switch (pccb->ccb_h.func_code)
1806 printf("arcmsr_action: XPT_SCSI_IO......................\n" );
1809 if((pSRB=arcmsr_get_freesrb(pACB)) == NULL)
1811 pccb->ccb_h.status=CAM_RESRC_UNAVAIL;
1815 pccb->ccb_h.arcmsr_ccbsrb_ptr=pSRB;
1816 pccb->ccb_h.arcmsr_ccbacb_ptr=pACB;
1818 if((pccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
1820 if((pccb->ccb_h.flags & CAM_SCATTER_VALID) == 0)
1822 if((pccb->ccb_h.flags & CAM_DATA_PHYS) == 0)
1827 error = bus_dmamap_load(pACB->buffer_dmat,pSRB->dmamap,pccb->csio.data_ptr,pccb->csio.dxfer_len,arcmsr_executesrb,pSRB,/*flags*/0);
1828 if(error == EINPROGRESS)
1830 xpt_freeze_simq(pACB->psim,1);
1831 pccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1837 panic("arcmsr: CAM_DATA_PHYS not supported");
1842 struct bus_dma_segment *segs;
1844 if((pccb->ccb_h.flags & CAM_SG_LIST_PHYS) == 0 || (pccb->ccb_h.flags & CAM_DATA_PHYS) != 0)
1846 pccb->ccb_h.status=CAM_PROVIDE_FAIL;
1848 free(pSRB,M_DEVBUF);
1851 segs=(struct bus_dma_segment *)pccb->csio.data_ptr;
1852 arcmsr_executesrb(pSRB,segs,pccb->csio.sglist_cnt,0);
1857 arcmsr_executesrb(pSRB,NULL,0,0);
1864 printf("arcmsr_action: XPT_TARGET_IO\n" );
1867 ** target mode not yet support vendor specific commands.
1869 pccb->ccb_h.status=CAM_REQ_CMP;
1875 struct ccb_pathinq *cpi=&pccb->cpi;
1878 printf("arcmsr_action: XPT_PATH_INQ\n" );
1881 cpi->hba_inquiry=PI_SDTR_ABLE | PI_TAG_ABLE;
1885 cpi->max_target=ARCMSR_MAX_TARGETID;
1886 cpi->max_lun=ARCMSR_MAX_TARGETLUN; /* 7 or 0 */
1887 cpi->initiator_id=ARCMSR_SCSI_INITIATOR_ID;
1888 cpi->bus_id=cam_sim_bus(psim);
1889 strncpy(cpi->sim_vid,"FreeBSD",SIM_IDLEN);
1890 strncpy(cpi->hba_vid,"ARCMSR",HBA_IDLEN);
1891 strncpy(cpi->dev_name,cam_sim_name(psim),DEV_IDLEN);
1892 cpi->unit_number=cam_sim_unit(psim);
1893 cpi->ccb_h.status=CAM_REQ_CMP;
1894 cpi->transport = XPORT_SPI;
1895 cpi->transport_version = 2;
1896 cpi->protocol = PROTO_SCSI;
1897 cpi->protocol_version = SCSI_REV_2;
1903 union ccb *pabort_ccb;
1906 printf("arcmsr_action: XPT_ABORT\n" );
1908 pabort_ccb=pccb->cab.abort_ccb;
1909 switch (pabort_ccb->ccb_h.func_code)
1911 case XPT_ACCEPT_TARGET_IO:
1912 case XPT_IMMED_NOTIFY:
1913 case XPT_CONT_TARGET_IO:
1914 if(arcmsr_seek_cmd2abort(pabort_ccb)==TRUE)
1916 pabort_ccb->ccb_h.status=CAM_REQ_ABORTED;
1917 xpt_done(pabort_ccb);
1918 pccb->ccb_h.status=CAM_REQ_CMP;
1922 xpt_print_path(pabort_ccb->ccb_h.path);
1923 printf("Not found\n");
1924 pccb->ccb_h.status=CAM_PATH_INVALID;
1928 pccb->ccb_h.status=CAM_UA_ABORT;
1931 pccb->ccb_h.status=CAM_REQ_INVALID;
1943 printf("arcmsr_action: XPT_RESET_BUS\n" );
1945 arcmsr_bus_reset(pACB);
1946 for (i=0; i < 500; i++)
1950 pccb->ccb_h.status=CAM_REQ_CMP;
1957 printf("arcmsr_action: XPT_TERM_IO\n" );
1959 pccb->ccb_h.status=CAM_REQ_INVALID;
1963 case XPT_GET_TRAN_SETTINGS:
1965 struct ccb_trans_settings *cts = &pccb->cts;
1967 struct ccb_trans_settings_scsi *scsi =
1968 &cts->proto_specific.scsi;
1969 struct ccb_trans_settings_spi *spi =
1970 &cts->xport_specific.spi;
1972 cts->protocol = PROTO_SCSI;
1973 cts->protocol_version = SCSI_REV_2;
1974 cts->transport = XPORT_SPI;
1975 cts->transport_version = 2;
1979 printf("arcmsr_action: XPT_GET_TRAN_SETTINGS\n" );
1983 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
1985 spi->sync_offset=32;
1986 spi->bus_width=MSG_EXT_WDTR_BUS_16_BIT;
1987 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
1988 spi->valid = CTS_SPI_VALID_SYNC_RATE
1989 | CTS_SPI_VALID_SYNC_OFFSET
1990 | CTS_SPI_VALID_BUS_WIDTH;
1991 scsi->valid = CTS_SCSI_VALID_TQ;
1993 pccb->ccb_h.status=CAM_REQ_CMP;
1997 case XPT_SET_TRAN_SETTINGS:
2000 printf("arcmsr_action: XPT_SET_TRAN_SETTINGS\n" );
2002 pccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
2006 case XPT_CALC_GEOMETRY:
2008 struct ccb_calc_geometry *ccg;
2010 ULONG secs_per_cylinder;
2013 printf("arcmsr_action: XPT_CALC_GEOMETRY\n" );
2016 size_mb=ccg->volume_size/((1024L * 1024L)/ccg->block_size);
2020 ccg->secs_per_track=63;
2025 ccg->secs_per_track=32;
2027 secs_per_cylinder=ccg->heads * ccg->secs_per_track;
2028 ccg->cylinders=ccg->volume_size / secs_per_cylinder;
2029 pccb->ccb_h.status=CAM_REQ_CMP;
2035 printf("arcmsr_action: invalid XPT function CAM_REQ_INVALID\n" );
2037 pccb->ccb_h.status=CAM_REQ_INVALID;
2044 **********************************************************************
2046 ** start background rebuild
2048 **********************************************************************
2050 VOID arcmsr_start_adapter_bgrb(PACB pACB)
2053 printf("arcmsr_start_adapter_bgrb.................................. \n");
2055 pACB->acb_flags |= ACB_F_MSG_START_BGRB;
2056 pACB->acb_flags &= ~ACB_F_MSG_STOP_BGRB;
2057 CHIP_REG_WRITE32(&pACB->pmu->inbound_msgaddr0,ARCMSR_INBOUND_MESG0_START_BGRB);
2061 **********************************************************************
2063 ** start background rebuild
2065 **********************************************************************
2067 VOID arcmsr_iop_init(PACB pACB)
2069 ULONG intmask_org,mask,outbound_doorbell,firmware_state=0;
2072 printf("arcmsr_iop_init.................................. \n");
2076 firmware_state=CHIP_REG_READ32(&pACB->pmu->outbound_msgaddr1);
\r
2077 }while((firmware_state & ARCMSR_OUTBOUND_MESG1_FIRMWARE_OK)==0);
2078 /* disable all outbound interrupt */
2079 intmask_org=CHIP_REG_READ32(&pACB->pmu->outbound_intmask);
2080 CHIP_REG_WRITE32(&pACB->pmu->outbound_intmask,intmask_org|ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE);
2081 /*start background rebuild*/
2082 arcmsr_start_adapter_bgrb(pACB);
2083 if(arcmsr_wait_msgint_ready(pACB)!=TRUE)
2085 printf("arcmsr_HwInitialize: wait 'start adapter background rebuild' timeout................. \n");
2087 /* clear Qbuffer if door bell ringed */
2088 outbound_doorbell=CHIP_REG_READ32(&pACB->pmu->outbound_doorbell);
2089 if(outbound_doorbell & ARCMSR_OUTBOUND_IOP331_DATA_WRITE_OK)
2091 CHIP_REG_WRITE32(&pACB->pmu->outbound_doorbell,outbound_doorbell);/*clear interrupt */
2092 CHIP_REG_WRITE32(&pACB->pmu->inbound_doorbell,ARCMSR_INBOUND_DRIVER_DATA_READ_OK);
2094 /* enable outbound Post Queue,outbound message0,outbell doorbell Interrupt */
2095 mask=~(ARCMSR_MU_OUTBOUND_POSTQUEUE_INTMASKENABLE|ARCMSR_MU_OUTBOUND_DOORBELL_INTMASKENABLE|ARCMSR_MU_OUTBOUND_MESSAGE0_INTMASKENABLE);
2096 CHIP_REG_WRITE32(&pACB->pmu->outbound_intmask,intmask_org & mask);
2097 pACB->outbound_int_enable = ~(intmask_org & mask) & 0x000000ff;
2098 pACB->acb_flags |=ACB_F_IOP_INITED;
2102 **********************************************************************
2106 **********************************************************************
2108 static void arcmsr_map_freesrb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2111 PSRB psrb_tmp,pfreesrb;
2115 pfreesrb=(PSRB)pACB->uncacheptr;
\r
2116 cdb_phyaddr=segs->ds_addr; /* We suppose bus_addr_t high part always 0 here*/
\r
2117 if(((CPT2INT)pACB->uncacheptr & 0x1F)!=0)
\r
2119 pfreesrb=pfreesrb+(0x20-((CPT2INT)pfreesrb & 0x1F));
\r
2120 cdb_phyaddr=cdb_phyaddr+(0x20-((CPT2INT)cdb_phyaddr & 0x1F));
\r
2123 ********************************************************************
2124 ** here we need to tell iop 331 our freesrb.HighPart
2125 ** if freesrb.HighPart is not zero
2126 ********************************************************************
2128 for(i=0;i<ARCMSR_MAX_FREESRB_NUM;i++)
2130 psrb_tmp=&pfreesrb[i];
2131 if(((CPT2INT)psrb_tmp & 0x1F)==0) /*srb address must 32 (0x20) boundary*/
2133 if(bus_dmamap_create(pACB->buffer_dmat, /*flags*/0, &psrb_tmp->dmamap)!=0)
2135 pACB->acb_flags |= ACB_F_MAPFREESRB_FAILD;
2136 printf(" arcmsr_map_freesrb: (pSRB->dmamap) bus_dmamap_create ..............error\n");
2139 psrb_tmp->cdb_shifted_phyaddr=cdb_phyaddr >> 5;
2140 psrb_tmp->pACB=pACB;
2141 pACB->psrbringQ[i]=psrb_tmp;
2142 cdb_phyaddr=cdb_phyaddr+sizeof(struct _SRB);
2146 pACB->acb_flags |= ACB_F_MAPFREESRB_FAILD;
2147 printf(" arcmsr_map_freesrb:pfreesrb=%p i=%d this srb cross 32 bytes boundary ignored ......psrb_tmp=%p \n",pfreesrb,i,psrb_tmp);
2151 pACB->pfreesrb=pfreesrb;
2152 pACB->vir2phy_offset=(CPT2INT)psrb_tmp-(cdb_phyaddr-sizeof(struct _SRB));
2156 ************************************************************************
2159 ************************************************************************
2161 VOID arcmsr_free_resource(PACB pACB)
2163 /* remove the control device */
2164 if (pACB->ioctl_dev != NULL)
2166 destroy_dev(pACB->ioctl_dev);
2168 bus_dmamap_unload(pACB->srb_dmat, pACB->srb_dmamap);
2169 bus_dmamap_destroy(pACB->srb_dmat, pACB->srb_dmamap);
2170 bus_dma_tag_destroy(pACB->srb_dmat);
2171 bus_dma_tag_destroy(pACB->buffer_dmat);
2172 bus_dma_tag_destroy(pACB->parent_dmat);
2176 ************************************************************************
2177 ** PCI config header registers for all devices
2179 ** #define PCIR_COMMAND 0x04
2180 ** #define PCIM_CMD_PORTEN 0x0001
2181 ** #define PCIM_CMD_MEMEN 0x0002
2182 ** #define PCIM_CMD_BUSMASTEREN 0x0004
2183 ** #define PCIM_CMD_MWRICEN 0x0010
2184 ** #define PCIM_CMD_PERRESPEN 0x0040
2186 ** Function : arcmsr_initialize
2187 ** Purpose : initialize the internal structures for a given SCSI host
2188 ** Inputs : host - pointer to this host adapter's structure
2189 ** Preconditions : when this function is called,the chip_type
2190 ** field of the pACB structure MUST have been set.
2192 ** 10h Base Address register #0
2193 ** 14h Base Address register #1
2194 ** 18h Base Address register #2
2195 ** 1Ch Base Address register #3
2196 ** 20h Base Address register #4
2197 ** 24h Base Address register #5
2198 ************************************************************************
2200 static LONG arcmsr_initialize(device_t dev)
2202 PACB pACB=device_get_softc(dev);
2203 LONG rid=PCI_BASE_ADDR0;
2204 vm_offset_t mem_base;
2208 printf("arcmsr_initialize..............................\n");
2210 #if __FreeBSD_version >= 502010
2211 if (bus_dma_tag_create( /*parent*/NULL,
2214 /*lowaddr*/BUS_SPACE_MAXADDR,
2215 /*highaddr*/BUS_SPACE_MAXADDR,
2218 /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
2219 /*nsegments*/BUS_SPACE_UNRESTRICTED,
2220 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
2224 &pACB->parent_dmat) != 0)
2226 if (bus_dma_tag_create( /*parent*/NULL,
2229 /*lowaddr*/BUS_SPACE_MAXADDR,
2230 /*highaddr*/BUS_SPACE_MAXADDR,
2233 /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
2234 /*nsegments*/BUS_SPACE_UNRESTRICTED,
2235 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
2237 &pACB->parent_dmat) != 0)
2240 printf("arcmsr_initialize: bus_dma_tag_create .......................failure!\n");
2243 /* Create a single tag describing a region large enough to hold all of the s/g lists we will need. */
2244 #if __FreeBSD_version >= 502010
2245 if(bus_dma_tag_create( /*parent_dmat*/pACB->parent_dmat,
2248 /*lowaddr*/BUS_SPACE_MAXADDR,
2249 /*highaddr*/BUS_SPACE_MAXADDR,
2252 /*maxsize*/MAXBSIZE,
2253 /*nsegments*/ARCMSR_MAX_SG_ENTRIES,
2254 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
2255 /*flags*/BUS_DMA_ALLOCNOW,
2256 /*lockfunc*/busdma_lock_mutex,
2258 &pACB->buffer_dmat) != 0)
2260 if(bus_dma_tag_create( /*parent_dmat*/pACB->parent_dmat,
2263 /*lowaddr*/BUS_SPACE_MAXADDR,
2264 /*highaddr*/BUS_SPACE_MAXADDR,
2267 /*maxsize*/MAXBSIZE,
2268 /*nsegments*/ARCMSR_MAX_SG_ENTRIES,
2269 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
2270 /*flags*/BUS_DMA_ALLOCNOW,
2271 &pACB->buffer_dmat) != 0)
2274 bus_dma_tag_destroy(pACB->parent_dmat);
2275 printf("arcmsr_initialize: bus_dma_tag_create ............................failure!\n");
2278 /* DMA tag for our srb structures.... Allocate the pfreesrb memory */
2279 #if __FreeBSD_version >= 502010
2280 if (bus_dma_tag_create( /*parent_dmat*/pACB->parent_dmat,
2283 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
2284 /*highaddr*/BUS_SPACE_MAXADDR,
2287 /*maxsize*/((sizeof(struct _SRB) * ARCMSR_MAX_FREESRB_NUM)+0x20),
2289 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
2293 &pACB->srb_dmat) != 0)
2295 if (bus_dma_tag_create( /*parent_dmat*/pACB->parent_dmat,
2298 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
2299 /*highaddr*/BUS_SPACE_MAXADDR,
2302 /*maxsize*/((sizeof(struct _SRB) * ARCMSR_MAX_FREESRB_NUM)+0x20),
2304 /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
2306 &pACB->srb_dmat) != 0)
2309 bus_dma_tag_destroy(pACB->buffer_dmat);
2310 bus_dma_tag_destroy(pACB->parent_dmat);
2311 printf("arcmsr_initialize: pACB->srb_dmat bus_dma_tag_create .....................failure!\n");
2314 /* Allocation for our srbs */
2315 if (bus_dmamem_alloc(pACB->srb_dmat, (void **)&pACB->uncacheptr, BUS_DMA_WAITOK | BUS_DMA_COHERENT, &pACB->srb_dmamap) != 0)
2317 bus_dma_tag_destroy(pACB->srb_dmat);
2318 bus_dma_tag_destroy(pACB->buffer_dmat);
2319 bus_dma_tag_destroy(pACB->parent_dmat);
2320 printf("arcmsr_initialize: pACB->srb_dmat bus_dma_tag_create ...............failure!\n");
2323 /* And permanently map them */
2324 if(bus_dmamap_load(pACB->srb_dmat, pACB->srb_dmamap,pACB->uncacheptr,(sizeof(struct _SRB) * ARCMSR_MAX_FREESRB_NUM)+0x20,arcmsr_map_freesrb, pACB, /*flags*/0))
2326 bus_dma_tag_destroy(pACB->srb_dmat);
2327 bus_dma_tag_destroy(pACB->buffer_dmat);
2328 bus_dma_tag_destroy(pACB->parent_dmat);
2329 printf("arcmsr_initialize: bus_dmamap_load................... failure!\n");
2332 pci_command=pci_read_config(dev,PCIR_COMMAND,2);
2333 pci_command |= PCIM_CMD_BUSMASTEREN;
2334 pci_command |= PCIM_CMD_PERRESPEN;
2335 pci_command |= PCIM_CMD_MWRICEN;
2336 /* Enable Busmaster/Mem */
2337 pci_command |= PCIM_CMD_MEMEN;
2338 pci_write_config(dev,PCIR_COMMAND,pci_command,2);
2339 pACB->sys_res_arcmsr=bus_alloc_resource(dev,SYS_RES_MEMORY,&rid,0,~0,0x1000,RF_ACTIVE);
2340 if(pACB->sys_res_arcmsr == NULL)
2342 arcmsr_free_resource(pACB);
2343 printf("arcmsr_initialize: bus_alloc_resource .....................failure!\n");
2346 if(rman_get_start(pACB->sys_res_arcmsr) <= 0)
2348 arcmsr_free_resource(pACB);
2349 printf("arcmsr_initialize: rman_get_start ...........................failure!\n");
2352 mem_base=(vm_offset_t) rman_get_virtual(pACB->sys_res_arcmsr);
2355 arcmsr_free_resource(pACB);
2356 printf("arcmsr_initialize: rman_get_virtual ..........................failure!\n");
2359 if(pACB->acb_flags & ACB_F_MAPFREESRB_FAILD)
2361 arcmsr_free_resource(pACB);
2362 printf("arcmsr_initialize: arman_get_virtual ..........................failure!\n");
2365 pACB->btag=rman_get_bustag(pACB->sys_res_arcmsr);
2366 pACB->bhandle=rman_get_bushandle(pACB->sys_res_arcmsr);
2367 pACB->pmu=(PMU)mem_base;
2368 pACB->acb_flags |= (ACB_F_IOCTL_WQBUFFER_CLEARED|ACB_F_IOCTL_RQBUFFER_CLEARED);
2369 pACB->acb_flags &= ~ACB_F_SCSISTOPADAPTER;
2370 arcmsr_iop_init(pACB);
2374 ************************************************************************
2376 ** attach and init a host adapter
2378 ************************************************************************
2380 static LONG arcmsr_attach(device_t dev)
2382 PACB pACB=device_get_softc(dev);
2383 LONG unit=device_get_unit(dev);
2384 struct ccb_setasync csa;
2385 struct cam_devq *devq; /* Device Queue to use for this SIM */
2386 struct resource *irqres;
2390 printf("arcmsr_attach .............................\n" );
2393 if(arcmsr_initialize(dev))
2395 printf("arcmsr_attach: arcmsr_initialize failure!\n");
2398 /* After setting up the adapter,map our interrupt */
2400 irqres=bus_alloc_resource(dev,SYS_RES_IRQ,&rid,0,~0,1,RF_SHAREABLE | RF_ACTIVE);
2401 if(irqres == NULL || bus_setup_intr(dev,irqres,INTR_TYPE_CAM,arcmsr_interrupt,pACB,&pACB->ih))
2403 arcmsr_free_resource(pACB);
2404 printf("arcmsr%d: unable to register interrupt handler!\n",unit);
2407 pACB->irqres=irqres;
2409 pACB->pci_unit=unit;
2411 * Now let the CAM generic SCSI layer find the SCSI devices on
2412 * the bus * start queue to reset to the idle loop. *
2413 * Create device queue of SIM(s) * (MAX_START_JOB - 1) :
2414 * max_sim_transactions
2416 devq=cam_simq_alloc(ARCMSR_MAX_START_JOB);
2419 arcmsr_free_resource(pACB);
2420 bus_release_resource(dev, SYS_RES_IRQ, 0, pACB->irqres);
2421 printf("arcmsr_attach: cam_simq_alloc failure!\n");
2424 pACB->psim=cam_sim_alloc(arcmsr_action,arcmsr_poll,"arcmsr",pACB,pACB->pci_unit,1,ARCMSR_MAX_OUTSTANDING_CMD,devq);
2425 if(pACB->psim == NULL)
2427 arcmsr_free_resource(pACB);
2428 bus_release_resource(dev, SYS_RES_IRQ, 0, pACB->irqres);
2429 cam_simq_free(devq);
2430 printf("arcmsr_attach: cam_sim_alloc ..................failure!\n");
2433 if(xpt_bus_register(pACB->psim,0) != CAM_SUCCESS)
2435 arcmsr_free_resource(pACB);
2436 bus_release_resource(dev, SYS_RES_IRQ, 0, pACB->irqres);
2437 cam_sim_free(pACB->psim,/*free_devq*/TRUE);
2438 printf("arcmsr_attach: xpt_bus_register .......................failure!\n");
2441 if(xpt_create_path(&pACB->ppath,/* periph */ NULL,cam_sim_path(pACB->psim),CAM_TARGET_WILDCARD,CAM_LUN_WILDCARD) != CAM_REQ_CMP)
2443 arcmsr_free_resource(pACB);
2444 bus_release_resource(dev, SYS_RES_IRQ, 0, pACB->irqres);
2445 xpt_bus_deregister(cam_sim_path(pACB->psim));
2446 cam_sim_free(pACB->psim,/* free_simq */ TRUE);
2447 printf("arcmsr_attach: xpt_create_path .....................failure!\n");
2451 ****************************************************
2453 xpt_setup_ccb(&csa.ccb_h,pACB->ppath,/*priority*/5);
2454 csa.ccb_h.func_code=XPT_SASYNC_CB;
2455 csa.event_enable=AC_FOUND_DEVICE|AC_LOST_DEVICE;
2456 csa.callback=arcmsr_async;
2457 csa.callback_arg=pACB->psim;
2458 xpt_action((union ccb *)&csa);
2459 /* Create the control device. */
2460 pACB->ioctl_dev=make_dev(&arcmsr_cdevsw, unit, UID_ROOT, GID_WHEEL /* GID_OPERATOR */, S_IRUSR | S_IWUSR, "arcmsr%d", unit);
2461 #if __FreeBSD_version < 503000
2462 pACB->ioctl_dev->si_drv1=pACB;
2464 #if __FreeBSD_version > 500005
2465 (void)make_dev_alias(pACB->ioctl_dev, "arc%d", unit);
2469 #if __FreeBSD_version > 500005
2470 if(kthread_create(arcmsr_do_thread_works, pACB, &pACB->kthread_proc,0,"arcmsr%d: kthread",pACB->pci_unit))
2472 device_printf(pACB->pci_dev,"cannot create kernel thread for this host adapetr\n");
2473 xpt_bus_deregister(cam_sim_path(pACB->psim));
2474 cam_sim_free(pACB->psim,/* free_simq */ TRUE);
2475 panic("arcmsr plunge kernel thread fail");
2478 if(kthread_create(arcmsr_do_thread_works, pACB, &pACB->kthread_proc,"arcmsr%d: kthread", pACB->pci_unit))
2480 device_printf(pACB->pci_dev,"cannot create kernel thread for this host adapetr\n");
2481 xpt_bus_deregister(cam_sim_path(pACB->psim));
2482 cam_sim_free(pACB->psim,/* free_simq */ TRUE);
2483 panic("arcmsr plunge kernel thread fail");
2490 ************************************************************************
2494 ************************************************************************
2496 static LONG arcmsr_probe(device_t dev)
2500 printf("arcmsr_probe................. \n");
2502 switch(id=pci_get_devid(dev))
2504 case PCIDevVenIDARC1110:
2505 device_set_desc(dev,"ARECA ARC1110 PCI-X 4 PORTS SATA RAID CONTROLLER \n" ARCMSR_DRIVER_VERSION );
2507 case PCIDevVenIDARC1120:
2508 device_set_desc(dev,"ARECA ARC1120 PCI-X 8 PORTS SATA RAID CONTROLLER (RAID6-ENGINE Inside) \n" ARCMSR_DRIVER_VERSION);
2510 case PCIDevVenIDARC1130:
2511 device_set_desc(dev,"ARECA ARC1130 PCI-X 12 PORTS SATA RAID CONTROLLER (RAID6-ENGINE Inside) \n" ARCMSR_DRIVER_VERSION);
2513 case PCIDevVenIDARC1160:
2514 device_set_desc(dev,"ARECA ARC1160 PCI-X 16 PORTS SATA RAID CONTROLLER (RAID6-ENGINE Inside) \n" ARCMSR_DRIVER_VERSION);
2516 case PCIDevVenIDARC1210:
2517 device_set_desc(dev,"ARECA ARC1210 PCI-EXPRESS 4 PORTS SATA RAID CONTROLLER \n" ARCMSR_DRIVER_VERSION);
2519 case PCIDevVenIDARC1220:
2520 device_set_desc(dev,"ARECA ARC1220 PCI-EXPRESS 8 PORTS SATA RAID CONTROLLER (RAID6-ENGINE Inside) \n" ARCMSR_DRIVER_VERSION);
2522 case PCIDevVenIDARC1230:
2523 device_set_desc(dev,"ARECA ARC1230 PCI-EXPRESS 12 PORTS SATA RAID CONTROLLER (RAID6-ENGINE Inside) \n" ARCMSR_DRIVER_VERSION);
2525 case PCIDevVenIDARC1260:
2526 device_set_desc(dev,"ARECA ARC1260 PCI-EXPRESS 16 PORTS SATA RAID CONTROLLER (RAID6-ENGINE Inside) \n" ARCMSR_DRIVER_VERSION);
2532 ************************************************************************
2536 ************************************************************************
2538 static VOID arcmsr_shutdown(device_t dev)
2540 LONG i,abort_cmd_cnt=0;
2541 ULONG s,intmask_org;
2543 PACB pACB=device_get_softc(dev);
2546 printf("arcmsr_shutdown................. \n");
2549 /* disable all outbound interrupt */
2550 intmask_org=CHIP_REG_READ32(&pACB->pmu->outbound_intmask);
2551 CHIP_REG_WRITE32(&pACB->pmu->outbound_intmask,(intmask_org|ARCMSR_MU_OUTBOUND_ALL_INTMASKENABLE));
2552 /* stop adapter background rebuild */
2553 arcmsr_stop_adapter_bgrb(pACB);
2554 if(arcmsr_wait_msgint_ready(pACB)!=TRUE)
2556 printf("arcmsr_pcidev_disattach: wait 'stop adapter rebuild' timeout.... \n");
2558 arcmsr_flush_adapter_cache(pACB);
2559 if(arcmsr_wait_msgint_ready(pACB)!=TRUE)
2561 printf("arcmsr_pcidev_disattach: wait 'flush adapter cache' timeout.... \n");
2563 /* abort all outstanding command */
2564 pACB->acb_flags |= ACB_F_SCSISTOPADAPTER;
2565 pACB->acb_flags &= ~ACB_F_IOP_INITED;
2566 if(pACB->srboutstandingcount!=0)
2570 printf("arcmsr_pcidev_disattach: .....pACB->srboutstandingcount!=0 \n");
2572 /* Q back all outstanding srb into wait exec psrb Q*/
2573 pfreesrb=pACB->pfreesrb;
2574 for(i=0;i<ARCMSR_MAX_FREESRB_NUM;i++)
2577 if(pSRB->startdone==ARCMSR_SRB_START)
2579 pSRB->srb_flags|=SRB_FLAG_MASTER_ABORTED;
2580 pSRB->pccb->ccb_h.status=CAM_REQ_ABORTED;
2584 if(abort_cmd_cnt!=0)
2587 printf("arcmsr_pcidev_disattach: .....abort_cmd_cnt!=0 \n");
2589 arcmsr_abort_allcmd(pACB);
2590 if(arcmsr_wait_msgint_ready(pACB)!=TRUE)
2592 printf("arcmsr_pcidev_disattach: wait 'abort all outstanding command' timeout.................in \n");
2595 atomic_set_int(&pACB->srboutstandingcount,0);
2597 if(pACB->srbwait2gocount!=0)
2598 { /*remove first wait2go srb and abort it*/
2599 for(i=0;i<ARCMSR_MAX_OUTSTANDING_CMD;i++)
2601 pSRB=pACB->psrbwait2go[i];
2604 pACB->psrbwait2go[i]=NULL;
2605 atomic_subtract_int(&pACB->srbwait2gocount,1);
2606 pSRB->pccb->ccb_h.status=CAM_REQ_ABORTED;
2607 arcmsr_srb_complete(pSRB);
2613 pACB->acb_flags |= ACB_F_STOP_THREAD;
2614 wakeup(pACB->kthread_proc);/* signal to kernel thread do_dpcQ: "stop thread" */
2619 ************************************************************************
2623 ************************************************************************
2625 static LONG arcmsr_detach(device_t dev)
2627 PACB pACB=device_get_softc(dev);
2629 arcmsr_shutdown(dev);
2630 arcmsr_free_resource(pACB);
2631 bus_release_resource(dev, SYS_RES_MEMORY, PCI_BASE_ADDR0, pACB->sys_res_arcmsr);
2632 bus_teardown_intr(dev, pACB->irqres, pACB->ih);
2633 bus_release_resource(dev, SYS_RES_IRQ, 0, pACB->irqres);
2634 xpt_async(AC_LOST_DEVICE, pACB->ppath, NULL);
2635 xpt_free_path(pACB->ppath);
2636 xpt_bus_deregister(cam_sim_path(pACB->psim));
2637 cam_sim_free(pACB->psim, TRUE);