xref: /openbmc/linux/drivers/scsi/aic94xx/aic94xx_seq.c (revision 4b4193256c8d3bc3a5397b5cd9494c2ad386317d)
165c85c83SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22908d778SJames Bottomley /*
32908d778SJames Bottomley  * Aic94xx SAS/SATA driver sequencer interface.
42908d778SJames Bottomley  *
52908d778SJames Bottomley  * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
62908d778SJames Bottomley  * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
72908d778SJames Bottomley  *
82908d778SJames Bottomley  * Parts of this code adapted from David Chaw's adp94xx_seq.c.
92908d778SJames Bottomley  */
102908d778SJames Bottomley 
112908d778SJames Bottomley #include <linux/delay.h>
125a0e3ad6STejun Heo #include <linux/gfp.h>
132908d778SJames Bottomley #include <linux/pci.h>
14bc229b36SJames Bottomley #include <linux/module.h>
152908d778SJames Bottomley #include <linux/firmware.h>
162908d778SJames Bottomley #include "aic94xx_reg.h"
172908d778SJames Bottomley #include "aic94xx_hwi.h"
182908d778SJames Bottomley 
192908d778SJames Bottomley #include "aic94xx_seq.h"
202908d778SJames Bottomley #include "aic94xx_dump.h"
212908d778SJames Bottomley 
222908d778SJames Bottomley /* It takes no more than 0.05 us for an instruction
232908d778SJames Bottomley  * to complete. So waiting for 1 us should be more than
242908d778SJames Bottomley  * plenty.
252908d778SJames Bottomley  */
262908d778SJames Bottomley #define PAUSE_DELAY 1
272908d778SJames Bottomley #define PAUSE_TRIES 1000
282908d778SJames Bottomley 
292908d778SJames Bottomley static const struct firmware *sequencer_fw;
302908d778SJames Bottomley static u16 cseq_vecs[CSEQ_NUM_VECS], lseq_vecs[LSEQ_NUM_VECS], mode2_task,
312908d778SJames Bottomley 	cseq_idle_loop, lseq_idle_loop;
320bc202e0SDavid Woodhouse static const u8 *cseq_code, *lseq_code;
332908d778SJames Bottomley static u32 cseq_code_size, lseq_code_size;
342908d778SJames Bottomley 
352908d778SJames Bottomley static u16 first_scb_site_no = 0xFFFF;
362908d778SJames Bottomley static u16 last_scb_site_no;
372908d778SJames Bottomley 
382908d778SJames Bottomley /* ---------- Pause/Unpause CSEQ/LSEQ ---------- */
392908d778SJames Bottomley 
402908d778SJames Bottomley /**
412908d778SJames Bottomley  * asd_pause_cseq - pause the central sequencer
422908d778SJames Bottomley  * @asd_ha: pointer to host adapter structure
432908d778SJames Bottomley  *
442908d778SJames Bottomley  * Return 0 on success, negative on failure.
452908d778SJames Bottomley  */
asd_pause_cseq(struct asd_ha_struct * asd_ha)4681e56dedSAdrian Bunk static int asd_pause_cseq(struct asd_ha_struct *asd_ha)
472908d778SJames Bottomley {
482908d778SJames Bottomley 	int	count = PAUSE_TRIES;
492908d778SJames Bottomley 	u32	arp2ctl;
502908d778SJames Bottomley 
512908d778SJames Bottomley 	arp2ctl = asd_read_reg_dword(asd_ha, CARP2CTL);
522908d778SJames Bottomley 	if (arp2ctl & PAUSED)
532908d778SJames Bottomley 		return 0;
542908d778SJames Bottomley 
552908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, CARP2CTL, arp2ctl | EPAUSE);
562908d778SJames Bottomley 	do {
572908d778SJames Bottomley 		arp2ctl = asd_read_reg_dword(asd_ha, CARP2CTL);
582908d778SJames Bottomley 		if (arp2ctl & PAUSED)
592908d778SJames Bottomley 			return 0;
602908d778SJames Bottomley 		udelay(PAUSE_DELAY);
612908d778SJames Bottomley 	} while (--count > 0);
622908d778SJames Bottomley 
632908d778SJames Bottomley 	ASD_DPRINTK("couldn't pause CSEQ\n");
642908d778SJames Bottomley 	return -1;
652908d778SJames Bottomley }
662908d778SJames Bottomley 
672908d778SJames Bottomley /**
682908d778SJames Bottomley  * asd_unpause_cseq - unpause the central sequencer.
692908d778SJames Bottomley  * @asd_ha: pointer to host adapter structure.
702908d778SJames Bottomley  *
712908d778SJames Bottomley  * Return 0 on success, negative on error.
722908d778SJames Bottomley  */
asd_unpause_cseq(struct asd_ha_struct * asd_ha)7381e56dedSAdrian Bunk static int asd_unpause_cseq(struct asd_ha_struct *asd_ha)
742908d778SJames Bottomley {
752908d778SJames Bottomley 	u32	arp2ctl;
762908d778SJames Bottomley 	int	count = PAUSE_TRIES;
772908d778SJames Bottomley 
782908d778SJames Bottomley 	arp2ctl = asd_read_reg_dword(asd_ha, CARP2CTL);
792908d778SJames Bottomley 	if (!(arp2ctl & PAUSED))
802908d778SJames Bottomley 		return 0;
812908d778SJames Bottomley 
822908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, CARP2CTL, arp2ctl & ~EPAUSE);
832908d778SJames Bottomley 	do {
842908d778SJames Bottomley 		arp2ctl = asd_read_reg_dword(asd_ha, CARP2CTL);
852908d778SJames Bottomley 		if (!(arp2ctl & PAUSED))
862908d778SJames Bottomley 			return 0;
872908d778SJames Bottomley 		udelay(PAUSE_DELAY);
882908d778SJames Bottomley 	} while (--count > 0);
892908d778SJames Bottomley 
902908d778SJames Bottomley 	ASD_DPRINTK("couldn't unpause the CSEQ\n");
912908d778SJames Bottomley 	return -1;
922908d778SJames Bottomley }
932908d778SJames Bottomley 
942908d778SJames Bottomley /**
952908d778SJames Bottomley  * asd_seq_pause_lseq - pause a link sequencer
962908d778SJames Bottomley  * @asd_ha: pointer to a host adapter structure
972908d778SJames Bottomley  * @lseq: link sequencer of interest
982908d778SJames Bottomley  *
992908d778SJames Bottomley  * Return 0 on success, negative on error.
1002908d778SJames Bottomley  */
asd_seq_pause_lseq(struct asd_ha_struct * asd_ha,int lseq)10181e56dedSAdrian Bunk static int asd_seq_pause_lseq(struct asd_ha_struct *asd_ha, int lseq)
1022908d778SJames Bottomley {
1032908d778SJames Bottomley 	u32    arp2ctl;
1042908d778SJames Bottomley 	int    count = PAUSE_TRIES;
1052908d778SJames Bottomley 
1062908d778SJames Bottomley 	arp2ctl = asd_read_reg_dword(asd_ha, LmARP2CTL(lseq));
1072908d778SJames Bottomley 	if (arp2ctl & PAUSED)
1082908d778SJames Bottomley 		return 0;
1092908d778SJames Bottomley 
1102908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmARP2CTL(lseq), arp2ctl | EPAUSE);
1112908d778SJames Bottomley 	do {
1122908d778SJames Bottomley 		arp2ctl = asd_read_reg_dword(asd_ha, LmARP2CTL(lseq));
1132908d778SJames Bottomley 		if (arp2ctl & PAUSED)
1142908d778SJames Bottomley 			return 0;
1152908d778SJames Bottomley 		udelay(PAUSE_DELAY);
1162908d778SJames Bottomley 	} while (--count > 0);
1172908d778SJames Bottomley 
1182908d778SJames Bottomley 	ASD_DPRINTK("couldn't pause LSEQ %d\n", lseq);
1192908d778SJames Bottomley 	return -1;
1202908d778SJames Bottomley }
1212908d778SJames Bottomley 
1222908d778SJames Bottomley /**
1232908d778SJames Bottomley  * asd_pause_lseq - pause the link sequencer(s)
1242908d778SJames Bottomley  * @asd_ha: pointer to host adapter structure
1252908d778SJames Bottomley  * @lseq_mask: mask of link sequencers of interest
1262908d778SJames Bottomley  *
1272908d778SJames Bottomley  * Return 0 on success, negative on failure.
1282908d778SJames Bottomley  */
asd_pause_lseq(struct asd_ha_struct * asd_ha,u8 lseq_mask)12981e56dedSAdrian Bunk static int asd_pause_lseq(struct asd_ha_struct *asd_ha, u8 lseq_mask)
1302908d778SJames Bottomley {
1312908d778SJames Bottomley 	int lseq;
1322908d778SJames Bottomley 	int err = 0;
1332908d778SJames Bottomley 
1342908d778SJames Bottomley 	for_each_sequencer(lseq_mask, lseq_mask, lseq) {
1352908d778SJames Bottomley 		err = asd_seq_pause_lseq(asd_ha, lseq);
1362908d778SJames Bottomley 		if (err)
1372908d778SJames Bottomley 			return err;
1382908d778SJames Bottomley 	}
1392908d778SJames Bottomley 
1402908d778SJames Bottomley 	return err;
1412908d778SJames Bottomley }
1422908d778SJames Bottomley 
1432908d778SJames Bottomley /**
1442908d778SJames Bottomley  * asd_seq_unpause_lseq - unpause a link sequencer
1452908d778SJames Bottomley  * @asd_ha: pointer to host adapter structure
1462908d778SJames Bottomley  * @lseq: link sequencer of interest
1472908d778SJames Bottomley  *
1482908d778SJames Bottomley  * Return 0 on success, negative on error.
1492908d778SJames Bottomley  */
asd_seq_unpause_lseq(struct asd_ha_struct * asd_ha,int lseq)15081e56dedSAdrian Bunk static int asd_seq_unpause_lseq(struct asd_ha_struct *asd_ha, int lseq)
1512908d778SJames Bottomley {
1522908d778SJames Bottomley 	u32 arp2ctl;
1532908d778SJames Bottomley 	int count = PAUSE_TRIES;
1542908d778SJames Bottomley 
1552908d778SJames Bottomley 	arp2ctl = asd_read_reg_dword(asd_ha, LmARP2CTL(lseq));
1562908d778SJames Bottomley 	if (!(arp2ctl & PAUSED))
1572908d778SJames Bottomley 		return 0;
1582908d778SJames Bottomley 
1592908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmARP2CTL(lseq), arp2ctl & ~EPAUSE);
1602908d778SJames Bottomley 	do {
1612908d778SJames Bottomley 		arp2ctl = asd_read_reg_dword(asd_ha, LmARP2CTL(lseq));
1622908d778SJames Bottomley 		if (!(arp2ctl & PAUSED))
1632908d778SJames Bottomley 			return 0;
1642908d778SJames Bottomley 		udelay(PAUSE_DELAY);
1652908d778SJames Bottomley 	} while (--count > 0);
1662908d778SJames Bottomley 
1672908d778SJames Bottomley 	ASD_DPRINTK("couldn't unpause LSEQ %d\n", lseq);
1682908d778SJames Bottomley 	return 0;
1692908d778SJames Bottomley }
1702908d778SJames Bottomley 
1712908d778SJames Bottomley 
1722908d778SJames Bottomley /* ---------- Downloading CSEQ/LSEQ microcode ---------- */
1732908d778SJames Bottomley 
asd_verify_cseq(struct asd_ha_struct * asd_ha,const u8 * _prog,u32 size)1742908d778SJames Bottomley static int asd_verify_cseq(struct asd_ha_struct *asd_ha, const u8 *_prog,
1752908d778SJames Bottomley 			   u32 size)
1762908d778SJames Bottomley {
1772908d778SJames Bottomley 	u32 addr = CSEQ_RAM_REG_BASE_ADR;
1782908d778SJames Bottomley 	const u32 *prog = (u32 *) _prog;
1792908d778SJames Bottomley 	u32 i;
1802908d778SJames Bottomley 
1812908d778SJames Bottomley 	for (i = 0; i < size; i += 4, prog++, addr += 4) {
1822908d778SJames Bottomley 		u32 val = asd_read_reg_dword(asd_ha, addr);
1832908d778SJames Bottomley 
1842908d778SJames Bottomley 		if (le32_to_cpu(*prog) != val) {
1852908d778SJames Bottomley 			asd_printk("%s: cseq verify failed at %u "
1862908d778SJames Bottomley 				   "read:0x%x, wanted:0x%x\n",
1872908d778SJames Bottomley 				   pci_name(asd_ha->pcidev),
1882908d778SJames Bottomley 				   i, val, le32_to_cpu(*prog));
1892908d778SJames Bottomley 			return -1;
1902908d778SJames Bottomley 		}
1912908d778SJames Bottomley 	}
1922908d778SJames Bottomley 	ASD_DPRINTK("verified %d bytes, passed\n", size);
1932908d778SJames Bottomley 	return 0;
1942908d778SJames Bottomley }
1952908d778SJames Bottomley 
1962908d778SJames Bottomley /**
1972908d778SJames Bottomley  * asd_verify_lseq - verify the microcode of a link sequencer
1982908d778SJames Bottomley  * @asd_ha: pointer to host adapter structure
1992908d778SJames Bottomley  * @_prog: pointer to the microcode
2002908d778SJames Bottomley  * @size: size of the microcode in bytes
2012908d778SJames Bottomley  * @lseq: link sequencer of interest
2022908d778SJames Bottomley  *
2032908d778SJames Bottomley  * The link sequencer code is accessed in 4 KB pages, which are selected
2042908d778SJames Bottomley  * by setting LmRAMPAGE (bits 8 and 9) of the LmBISTCTL1 register.
2052908d778SJames Bottomley  * The 10 KB LSEQm instruction code is mapped, page at a time, at
2062908d778SJames Bottomley  * LmSEQRAM address.
2072908d778SJames Bottomley  */
asd_verify_lseq(struct asd_ha_struct * asd_ha,const u8 * _prog,u32 size,int lseq)2082908d778SJames Bottomley static int asd_verify_lseq(struct asd_ha_struct *asd_ha, const u8 *_prog,
2092908d778SJames Bottomley 			   u32 size, int lseq)
2102908d778SJames Bottomley {
2112908d778SJames Bottomley #define LSEQ_CODEPAGE_SIZE 4096
2122908d778SJames Bottomley 	int pages =  (size + LSEQ_CODEPAGE_SIZE - 1) / LSEQ_CODEPAGE_SIZE;
2132908d778SJames Bottomley 	u32 page;
2142908d778SJames Bottomley 	const u32 *prog = (u32 *) _prog;
2152908d778SJames Bottomley 
2162908d778SJames Bottomley 	for (page = 0; page < pages; page++) {
2172908d778SJames Bottomley 		u32 i;
2182908d778SJames Bottomley 
2192908d778SJames Bottomley 		asd_write_reg_dword(asd_ha, LmBISTCTL1(lseq),
2202908d778SJames Bottomley 				    page << LmRAMPAGE_LSHIFT);
2212908d778SJames Bottomley 		for (i = 0; size > 0 && i < LSEQ_CODEPAGE_SIZE;
2222908d778SJames Bottomley 		     i += 4, prog++, size-=4) {
2232908d778SJames Bottomley 
2242908d778SJames Bottomley 			u32 val = asd_read_reg_dword(asd_ha, LmSEQRAM(lseq)+i);
2252908d778SJames Bottomley 
2262908d778SJames Bottomley 			if (le32_to_cpu(*prog) != val) {
2272908d778SJames Bottomley 				asd_printk("%s: LSEQ%d verify failed "
2282908d778SJames Bottomley 					   "page:%d, offs:%d\n",
2292908d778SJames Bottomley 					   pci_name(asd_ha->pcidev),
2302908d778SJames Bottomley 					   lseq, page, i);
2312908d778SJames Bottomley 				return -1;
2322908d778SJames Bottomley 			}
2332908d778SJames Bottomley 		}
2342908d778SJames Bottomley 	}
2352908d778SJames Bottomley 	ASD_DPRINTK("LSEQ%d verified %d bytes, passed\n", lseq,
2362908d778SJames Bottomley 		    (int)((u8 *)prog-_prog));
2372908d778SJames Bottomley 	return 0;
2382908d778SJames Bottomley }
2392908d778SJames Bottomley 
2402908d778SJames Bottomley /**
2412908d778SJames Bottomley  * asd_verify_seq -- verify CSEQ/LSEQ microcode
2422908d778SJames Bottomley  * @asd_ha: pointer to host adapter structure
2432908d778SJames Bottomley  * @prog: pointer to microcode
2442908d778SJames Bottomley  * @size: size of the microcode
2452908d778SJames Bottomley  * @lseq_mask: if 0, verify CSEQ microcode, else mask of LSEQs of interest
2462908d778SJames Bottomley  *
2472908d778SJames Bottomley  * Return 0 if microcode is correct, negative on mismatch.
2482908d778SJames Bottomley  */
asd_verify_seq(struct asd_ha_struct * asd_ha,const u8 * prog,u32 size,u8 lseq_mask)2492908d778SJames Bottomley static int asd_verify_seq(struct asd_ha_struct *asd_ha, const u8 *prog,
2502908d778SJames Bottomley 			      u32 size, u8 lseq_mask)
2512908d778SJames Bottomley {
2522908d778SJames Bottomley 	if (lseq_mask == 0)
2532908d778SJames Bottomley 		return asd_verify_cseq(asd_ha, prog, size);
2542908d778SJames Bottomley 	else {
2552908d778SJames Bottomley 		int lseq, err;
2562908d778SJames Bottomley 
2572908d778SJames Bottomley 		for_each_sequencer(lseq_mask, lseq_mask, lseq) {
2582908d778SJames Bottomley 			err = asd_verify_lseq(asd_ha, prog, size, lseq);
2592908d778SJames Bottomley 			if (err)
2602908d778SJames Bottomley 				return err;
2612908d778SJames Bottomley 		}
2622908d778SJames Bottomley 	}
2632908d778SJames Bottomley 
2642908d778SJames Bottomley 	return 0;
2652908d778SJames Bottomley }
2662908d778SJames Bottomley #define ASD_DMA_MODE_DOWNLOAD
2672908d778SJames Bottomley #ifdef ASD_DMA_MODE_DOWNLOAD
2682908d778SJames Bottomley /* This is the size of the CSEQ Mapped instruction page */
2692908d778SJames Bottomley #define MAX_DMA_OVLY_COUNT ((1U << 14)-1)
asd_download_seq(struct asd_ha_struct * asd_ha,const u8 * const prog,u32 size,u8 lseq_mask)2702908d778SJames Bottomley static int asd_download_seq(struct asd_ha_struct *asd_ha,
2712908d778SJames Bottomley 			    const u8 * const prog, u32 size, u8 lseq_mask)
2722908d778SJames Bottomley {
2732908d778SJames Bottomley 	u32 comstaten;
2742908d778SJames Bottomley 	u32 reg;
2752908d778SJames Bottomley 	int page;
2762908d778SJames Bottomley 	const int pages = (size + MAX_DMA_OVLY_COUNT - 1) / MAX_DMA_OVLY_COUNT;
2772908d778SJames Bottomley 	struct asd_dma_tok *token;
2782908d778SJames Bottomley 	int err = 0;
2792908d778SJames Bottomley 
2802908d778SJames Bottomley 	if (size % 4) {
2812908d778SJames Bottomley 		asd_printk("sequencer program not multiple of 4\n");
2822908d778SJames Bottomley 		return -1;
2832908d778SJames Bottomley 	}
2842908d778SJames Bottomley 
2852908d778SJames Bottomley 	asd_pause_cseq(asd_ha);
2862908d778SJames Bottomley 	asd_pause_lseq(asd_ha, 0xFF);
2872908d778SJames Bottomley 
2882908d778SJames Bottomley 	/* save, disable and clear interrupts */
2892908d778SJames Bottomley 	comstaten = asd_read_reg_dword(asd_ha, COMSTATEN);
2902908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, COMSTATEN, 0);
2912908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, COMSTAT, COMSTAT_MASK);
2922908d778SJames Bottomley 
2932908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, CHIMINTEN, RST_CHIMINTEN);
2942908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, CHIMINT, CHIMINT_MASK);
2952908d778SJames Bottomley 
2962908d778SJames Bottomley 	token = asd_alloc_coherent(asd_ha, MAX_DMA_OVLY_COUNT, GFP_KERNEL);
2972908d778SJames Bottomley 	if (!token) {
2982908d778SJames Bottomley 		asd_printk("out of memory for dma SEQ download\n");
2992908d778SJames Bottomley 		err = -ENOMEM;
3002908d778SJames Bottomley 		goto out;
3012908d778SJames Bottomley 	}
3022908d778SJames Bottomley 	ASD_DPRINTK("dma-ing %d bytes\n", size);
3032908d778SJames Bottomley 
3042908d778SJames Bottomley 	for (page = 0; page < pages; page++) {
3052908d778SJames Bottomley 		int i;
3062908d778SJames Bottomley 		u32 left = min(size-page*MAX_DMA_OVLY_COUNT,
3072908d778SJames Bottomley 			       (u32)MAX_DMA_OVLY_COUNT);
3082908d778SJames Bottomley 
3092908d778SJames Bottomley 		memcpy(token->vaddr, prog + page*MAX_DMA_OVLY_COUNT, left);
3102908d778SJames Bottomley 		asd_write_reg_addr(asd_ha, OVLYDMAADR, token->dma_handle);
3112908d778SJames Bottomley 		asd_write_reg_dword(asd_ha, OVLYDMACNT, left);
3122908d778SJames Bottomley 		reg = !page ? RESETOVLYDMA : 0;
3132908d778SJames Bottomley 		reg |= (STARTOVLYDMA | OVLYHALTERR);
3142908d778SJames Bottomley 		reg |= (lseq_mask ? (((u32)lseq_mask) << 8) : OVLYCSEQ);
3152908d778SJames Bottomley 		/* Start DMA. */
3162908d778SJames Bottomley 		asd_write_reg_dword(asd_ha, OVLYDMACTL, reg);
3172908d778SJames Bottomley 
3182908d778SJames Bottomley 		for (i = PAUSE_TRIES*100; i > 0; i--) {
3192908d778SJames Bottomley 			u32 dmadone = asd_read_reg_dword(asd_ha, OVLYDMACTL);
3202908d778SJames Bottomley 			if (!(dmadone & OVLYDMAACT))
3212908d778SJames Bottomley 				break;
3222908d778SJames Bottomley 			udelay(PAUSE_DELAY);
3232908d778SJames Bottomley 		}
3242908d778SJames Bottomley 	}
3252908d778SJames Bottomley 
3262908d778SJames Bottomley 	reg = asd_read_reg_dword(asd_ha, COMSTAT);
3272908d778SJames Bottomley 	if (!(reg & OVLYDMADONE) || (reg & OVLYERR)
3282908d778SJames Bottomley 	    || (asd_read_reg_dword(asd_ha, CHIMINT) & DEVEXCEPT_MASK)){
3292908d778SJames Bottomley 		asd_printk("%s: error DMA-ing sequencer code\n",
3302908d778SJames Bottomley 			   pci_name(asd_ha->pcidev));
3312908d778SJames Bottomley 		err = -ENODEV;
3322908d778SJames Bottomley 	}
3332908d778SJames Bottomley 
3342908d778SJames Bottomley 	asd_free_coherent(asd_ha, token);
3352908d778SJames Bottomley  out:
3362908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, COMSTATEN, comstaten);
3372908d778SJames Bottomley 
3382908d778SJames Bottomley 	return err ? : asd_verify_seq(asd_ha, prog, size, lseq_mask);
3392908d778SJames Bottomley }
3402908d778SJames Bottomley #else /* ASD_DMA_MODE_DOWNLOAD */
asd_download_seq(struct asd_ha_struct * asd_ha,const u8 * _prog,u32 size,u8 lseq_mask)3412908d778SJames Bottomley static int asd_download_seq(struct asd_ha_struct *asd_ha, const u8 *_prog,
3422908d778SJames Bottomley 			    u32 size, u8 lseq_mask)
3432908d778SJames Bottomley {
3442908d778SJames Bottomley 	int i;
3452908d778SJames Bottomley 	u32 reg = 0;
3462908d778SJames Bottomley 	const u32 *prog = (u32 *) _prog;
3472908d778SJames Bottomley 
3482908d778SJames Bottomley 	if (size % 4) {
3492908d778SJames Bottomley 		asd_printk("sequencer program not multiple of 4\n");
3502908d778SJames Bottomley 		return -1;
3512908d778SJames Bottomley 	}
3522908d778SJames Bottomley 
3532908d778SJames Bottomley 	asd_pause_cseq(asd_ha);
3542908d778SJames Bottomley 	asd_pause_lseq(asd_ha, 0xFF);
3552908d778SJames Bottomley 
3562908d778SJames Bottomley 	reg |= (lseq_mask ? (((u32)lseq_mask) << 8) : OVLYCSEQ);
3572908d778SJames Bottomley 	reg |= PIOCMODE;
3582908d778SJames Bottomley 
3592908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, OVLYDMACNT, size);
3602908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, OVLYDMACTL, reg);
3612908d778SJames Bottomley 
3622908d778SJames Bottomley 	ASD_DPRINTK("downloading %s sequencer%s in PIO mode...\n",
3632908d778SJames Bottomley 		    lseq_mask ? "LSEQ" : "CSEQ", lseq_mask ? "s" : "");
3642908d778SJames Bottomley 
3652908d778SJames Bottomley 	for (i = 0; i < size; i += 4, prog++)
3662908d778SJames Bottomley 		asd_write_reg_dword(asd_ha, SPIODATA, *prog);
3672908d778SJames Bottomley 
3682908d778SJames Bottomley 	reg = (reg & ~PIOCMODE) | OVLYHALTERR;
3692908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, OVLYDMACTL, reg);
3702908d778SJames Bottomley 
3712908d778SJames Bottomley 	return asd_verify_seq(asd_ha, _prog, size, lseq_mask);
3722908d778SJames Bottomley }
3732908d778SJames Bottomley #endif /* ASD_DMA_MODE_DOWNLOAD */
3742908d778SJames Bottomley 
3752908d778SJames Bottomley /**
3762908d778SJames Bottomley  * asd_seq_download_seqs - download the sequencer microcode
3772908d778SJames Bottomley  * @asd_ha: pointer to host adapter structure
3782908d778SJames Bottomley  *
3792908d778SJames Bottomley  * Download the central and link sequencer microcode.
3802908d778SJames Bottomley  */
asd_seq_download_seqs(struct asd_ha_struct * asd_ha)3812908d778SJames Bottomley static int asd_seq_download_seqs(struct asd_ha_struct *asd_ha)
3822908d778SJames Bottomley {
3832908d778SJames Bottomley 	int 	err;
3842908d778SJames Bottomley 
3852908d778SJames Bottomley 	if (!asd_ha->hw_prof.enabled_phys) {
3862908d778SJames Bottomley 		asd_printk("%s: no enabled phys!\n", pci_name(asd_ha->pcidev));
3872908d778SJames Bottomley 		return -ENODEV;
3882908d778SJames Bottomley 	}
3892908d778SJames Bottomley 
3902908d778SJames Bottomley 	/* Download the CSEQ */
3912908d778SJames Bottomley 	ASD_DPRINTK("downloading CSEQ...\n");
3922908d778SJames Bottomley 	err = asd_download_seq(asd_ha, cseq_code, cseq_code_size, 0);
3932908d778SJames Bottomley 	if (err) {
3942908d778SJames Bottomley 		asd_printk("CSEQ download failed:%d\n", err);
3952908d778SJames Bottomley 		return err;
3962908d778SJames Bottomley 	}
3972908d778SJames Bottomley 
3982908d778SJames Bottomley 	/* Download the Link Sequencers code. All of the Link Sequencers
3992908d778SJames Bottomley 	 * microcode can be downloaded at the same time.
4002908d778SJames Bottomley 	 */
4012908d778SJames Bottomley 	ASD_DPRINTK("downloading LSEQs...\n");
4022908d778SJames Bottomley 	err = asd_download_seq(asd_ha, lseq_code, lseq_code_size,
4032908d778SJames Bottomley 			       asd_ha->hw_prof.enabled_phys);
4042908d778SJames Bottomley 	if (err) {
4052908d778SJames Bottomley 		/* Try it one at a time */
4062908d778SJames Bottomley 		u8 lseq;
4072908d778SJames Bottomley 		u8 lseq_mask = asd_ha->hw_prof.enabled_phys;
4082908d778SJames Bottomley 
4092908d778SJames Bottomley 		for_each_sequencer(lseq_mask, lseq_mask, lseq) {
4102908d778SJames Bottomley 			err = asd_download_seq(asd_ha, lseq_code,
4112908d778SJames Bottomley 					       lseq_code_size, 1<<lseq);
4122908d778SJames Bottomley 			if (err)
4132908d778SJames Bottomley 				break;
4142908d778SJames Bottomley 		}
4152908d778SJames Bottomley 	}
4162908d778SJames Bottomley 	if (err)
4172908d778SJames Bottomley 		asd_printk("LSEQs download failed:%d\n", err);
4182908d778SJames Bottomley 
4192908d778SJames Bottomley 	return err;
4202908d778SJames Bottomley }
4212908d778SJames Bottomley 
4222908d778SJames Bottomley /* ---------- Initializing the chip, chip memory, etc. ---------- */
4232908d778SJames Bottomley 
4242908d778SJames Bottomley /**
4252908d778SJames Bottomley  * asd_init_cseq_mip - initialize CSEQ mode independent pages 4-7
4262908d778SJames Bottomley  * @asd_ha: pointer to host adapter structure
4272908d778SJames Bottomley  */
asd_init_cseq_mip(struct asd_ha_struct * asd_ha)4282908d778SJames Bottomley static void asd_init_cseq_mip(struct asd_ha_struct *asd_ha)
4292908d778SJames Bottomley {
4302908d778SJames Bottomley 	/* CSEQ Mode Independent, page 4 setup. */
4312908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_Q_EXE_HEAD, 0xFFFF);
4322908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_Q_EXE_TAIL, 0xFFFF);
4332908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_Q_DONE_HEAD, 0xFFFF);
4342908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_Q_DONE_TAIL, 0xFFFF);
4352908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_Q_SEND_HEAD, 0xFFFF);
4362908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_Q_SEND_TAIL, 0xFFFF);
4372908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_Q_DMA2CHIM_HEAD, 0xFFFF);
4382908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_Q_DMA2CHIM_TAIL, 0xFFFF);
4392908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_Q_COPY_HEAD, 0xFFFF);
4402908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_Q_COPY_TAIL, 0xFFFF);
4412908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_REG0, 0);
4422908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_REG1, 0);
4432908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, CSEQ_REG2, 0);
4442908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, CSEQ_LINK_CTL_Q_MAP, 0);
4452908d778SJames Bottomley 	{
4462908d778SJames Bottomley 		u8 con = asd_read_reg_byte(asd_ha, CCONEXIST);
4472908d778SJames Bottomley 		u8 val = hweight8(con);
4482908d778SJames Bottomley 		asd_write_reg_byte(asd_ha, CSEQ_MAX_CSEQ_MODE, (val<<4)|val);
4492908d778SJames Bottomley 	}
4502908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_FREE_LIST_HACK_COUNT, 0);
4512908d778SJames Bottomley 
4522908d778SJames Bottomley 	/* CSEQ Mode independent, page 5 setup. */
4532908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, CSEQ_EST_NEXUS_REQ_QUEUE, 0);
4542908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, CSEQ_EST_NEXUS_REQ_QUEUE+4, 0);
4552908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, CSEQ_EST_NEXUS_REQ_COUNT, 0);
4562908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, CSEQ_EST_NEXUS_REQ_COUNT+4, 0);
4572908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_Q_EST_NEXUS_HEAD, 0xFFFF);
4582908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_Q_EST_NEXUS_TAIL, 0xFFFF);
4592908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_NEED_EST_NEXUS_SCB, 0);
4602908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, CSEQ_EST_NEXUS_REQ_HEAD, 0);
4612908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, CSEQ_EST_NEXUS_REQ_TAIL, 0);
4622908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, CSEQ_EST_NEXUS_SCB_OFFSET, 0);
4632908d778SJames Bottomley 
4642908d778SJames Bottomley 	/* CSEQ Mode independent, page 6 setup. */
4652908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_INT_ROUT_RET_ADDR0, 0);
4662908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_INT_ROUT_RET_ADDR1, 0);
4672908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_INT_ROUT_SCBPTR, 0);
4682908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, CSEQ_INT_ROUT_MODE, 0);
4692908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, CSEQ_ISR_SCRATCH_FLAGS, 0);
4702908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_ISR_SAVE_SINDEX, 0);
4712908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_ISR_SAVE_DINDEX, 0);
4722908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_Q_MONIRTT_HEAD, 0xFFFF);
4732908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_Q_MONIRTT_TAIL, 0xFFFF);
4742908d778SJames Bottomley 	/* Calculate the free scb mask. */
4752908d778SJames Bottomley 	{
4762908d778SJames Bottomley 		u16 cmdctx = asd_get_cmdctx_size(asd_ha);
4772908d778SJames Bottomley 		cmdctx = (~((cmdctx/128)-1)) >> 8;
4782908d778SJames Bottomley 		asd_write_reg_byte(asd_ha, CSEQ_FREE_SCB_MASK, (u8)cmdctx);
4792908d778SJames Bottomley 	}
4802908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_BUILTIN_FREE_SCB_HEAD,
4812908d778SJames Bottomley 			   first_scb_site_no);
4822908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_BUILTIN_FREE_SCB_TAIL,
4832908d778SJames Bottomley 			   last_scb_site_no);
4842908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_EXTENDED_FREE_SCB_HEAD, 0xFFFF);
4852908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_EXTENDED_FREE_SCB_TAIL, 0xFFFF);
4862908d778SJames Bottomley 
4872908d778SJames Bottomley 	/* CSEQ Mode independent, page 7 setup. */
4882908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, CSEQ_EMPTY_REQ_QUEUE, 0);
4892908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, CSEQ_EMPTY_REQ_QUEUE+4, 0);
4902908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, CSEQ_EMPTY_REQ_COUNT, 0);
4912908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, CSEQ_EMPTY_REQ_COUNT+4, 0);
4922908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_Q_EMPTY_HEAD, 0xFFFF);
4932908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_Q_EMPTY_TAIL, 0xFFFF);
4942908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_NEED_EMPTY_SCB, 0);
4952908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, CSEQ_EMPTY_REQ_HEAD, 0);
4962908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, CSEQ_EMPTY_REQ_TAIL, 0);
4972908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, CSEQ_EMPTY_SCB_OFFSET, 0);
4982908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_PRIMITIVE_DATA, 0);
4992908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, CSEQ_TIMEOUT_CONST, 0);
5002908d778SJames Bottomley }
5012908d778SJames Bottomley 
5022908d778SJames Bottomley /**
5032908d778SJames Bottomley  * asd_init_cseq_mdp - initialize CSEQ Mode dependent pages
5042908d778SJames Bottomley  * @asd_ha: pointer to host adapter structure
5052908d778SJames Bottomley  */
asd_init_cseq_mdp(struct asd_ha_struct * asd_ha)5062908d778SJames Bottomley static void asd_init_cseq_mdp(struct asd_ha_struct *asd_ha)
5072908d778SJames Bottomley {
5082908d778SJames Bottomley 	int	i;
5092908d778SJames Bottomley 	int	moffs;
5102908d778SJames Bottomley 
5112908d778SJames Bottomley 	moffs = CSEQ_PAGE_SIZE * 2;
5122908d778SJames Bottomley 
5132908d778SJames Bottomley 	/* CSEQ Mode dependent, modes 0-7, page 0 setup. */
5142908d778SJames Bottomley 	for (i = 0; i < 8; i++) {
5152908d778SJames Bottomley 		asd_write_reg_word(asd_ha, i*moffs+CSEQ_LRM_SAVE_SINDEX, 0);
5162908d778SJames Bottomley 		asd_write_reg_word(asd_ha, i*moffs+CSEQ_LRM_SAVE_SCBPTR, 0);
5172908d778SJames Bottomley 		asd_write_reg_word(asd_ha, i*moffs+CSEQ_Q_LINK_HEAD, 0xFFFF);
5182908d778SJames Bottomley 		asd_write_reg_word(asd_ha, i*moffs+CSEQ_Q_LINK_TAIL, 0xFFFF);
5192908d778SJames Bottomley 		asd_write_reg_byte(asd_ha, i*moffs+CSEQ_LRM_SAVE_SCRPAGE, 0);
5202908d778SJames Bottomley 	}
5212908d778SJames Bottomley 
5222908d778SJames Bottomley 	/* CSEQ Mode dependent, mode 0-7, page 1 and 2 shall be ignored. */
5232908d778SJames Bottomley 
5242908d778SJames Bottomley 	/* CSEQ Mode dependent, mode 8, page 0 setup. */
5252908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_RET_ADDR, 0xFFFF);
5262908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_RET_SCBPTR, 0);
5272908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_SAVE_SCBPTR, 0);
5282908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_EMPTY_TRANS_CTX, 0);
5292908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_RESP_LEN, 0);
5302908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_TMF_SCBPTR, 0);
5312908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_GLOBAL_PREV_SCB, 0);
5322908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_GLOBAL_HEAD, 0);
5332908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_CLEAR_LU_HEAD, 0);
5342908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, CSEQ_TMF_OPCODE, 0);
5352908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, CSEQ_SCRATCH_FLAGS, 0);
5362908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_HSB_SITE, 0);
5372908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_FIRST_INV_SCB_SITE,
5382908d778SJames Bottomley 			   (u16)last_scb_site_no+1);
5392908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CSEQ_FIRST_INV_DDB_SITE,
5402908d778SJames Bottomley 			   (u16)asd_ha->hw_prof.max_ddbs);
5412908d778SJames Bottomley 
5422908d778SJames Bottomley 	/* CSEQ Mode dependent, mode 8, page 1 setup. */
5432908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, CSEQ_LUN_TO_CLEAR, 0);
5442908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, CSEQ_LUN_TO_CLEAR + 4, 0);
5452908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, CSEQ_LUN_TO_CHECK, 0);
5462908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, CSEQ_LUN_TO_CHECK + 4, 0);
5472908d778SJames Bottomley 
5482908d778SJames Bottomley 	/* CSEQ Mode dependent, mode 8, page 2 setup. */
5492908d778SJames Bottomley 	/* Tell the sequencer the bus address of the first SCB. */
5502908d778SJames Bottomley 	asd_write_reg_addr(asd_ha, CSEQ_HQ_NEW_POINTER,
5512908d778SJames Bottomley 			   asd_ha->seq.next_scb.dma_handle);
5522908d778SJames Bottomley 	ASD_DPRINTK("First SCB dma_handle: 0x%llx\n",
5532908d778SJames Bottomley 		    (unsigned long long)asd_ha->seq.next_scb.dma_handle);
5542908d778SJames Bottomley 
5552908d778SJames Bottomley 	/* Tell the sequencer the first Done List entry address. */
5562908d778SJames Bottomley 	asd_write_reg_addr(asd_ha, CSEQ_HQ_DONE_BASE,
5572908d778SJames Bottomley 			   asd_ha->seq.actual_dl->dma_handle);
5582908d778SJames Bottomley 
5592908d778SJames Bottomley 	/* Initialize the Q_DONE_POINTER with the least significant
5602908d778SJames Bottomley 	 * 4 bytes of the first Done List address. */
5612908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, CSEQ_HQ_DONE_POINTER,
5622908d778SJames Bottomley 			    ASD_BUSADDR_LO(asd_ha->seq.actual_dl->dma_handle));
5632908d778SJames Bottomley 
5642908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, CSEQ_HQ_DONE_PASS, ASD_DEF_DL_TOGGLE);
5652908d778SJames Bottomley 
5662908d778SJames Bottomley 	/* CSEQ Mode dependent, mode 8, page 3 shall be ignored. */
5672908d778SJames Bottomley }
5682908d778SJames Bottomley 
5692908d778SJames Bottomley /**
5702908d778SJames Bottomley  * asd_init_cseq_scratch -- setup and init CSEQ
5712908d778SJames Bottomley  * @asd_ha: pointer to host adapter structure
5722908d778SJames Bottomley  *
573421f91d2SUwe Kleine-König  * Setup and initialize Central sequencers. Initialize the mode
5742908d778SJames Bottomley  * independent and dependent scratch page to the default settings.
5752908d778SJames Bottomley  */
asd_init_cseq_scratch(struct asd_ha_struct * asd_ha)5762908d778SJames Bottomley static void asd_init_cseq_scratch(struct asd_ha_struct *asd_ha)
5772908d778SJames Bottomley {
5782908d778SJames Bottomley 	asd_init_cseq_mip(asd_ha);
5792908d778SJames Bottomley 	asd_init_cseq_mdp(asd_ha);
5802908d778SJames Bottomley }
5812908d778SJames Bottomley 
5822908d778SJames Bottomley /**
5832908d778SJames Bottomley  * asd_init_lseq_mip -- initialize LSEQ Mode independent pages 0-3
5842908d778SJames Bottomley  * @asd_ha: pointer to host adapter structure
585*966fdadfSLee Jones  * @lseq:  link sequencer
5862908d778SJames Bottomley  */
asd_init_lseq_mip(struct asd_ha_struct * asd_ha,u8 lseq)5872908d778SJames Bottomley static void asd_init_lseq_mip(struct asd_ha_struct *asd_ha, u8 lseq)
5882908d778SJames Bottomley {
5892908d778SJames Bottomley 	int i;
5902908d778SJames Bottomley 
5912908d778SJames Bottomley 	/* LSEQ Mode independent page 0 setup. */
5922908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_Q_TGTXFR_HEAD(lseq), 0xFFFF);
5932908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_Q_TGTXFR_TAIL(lseq), 0xFFFF);
5942908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_LINK_NUMBER(lseq), lseq);
5952908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_SCRATCH_FLAGS(lseq),
5962908d778SJames Bottomley 			   ASD_NOTIFY_ENABLE_SPINUP);
5972908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_CONNECTION_STATE(lseq),0x08000000);
5982908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_CONCTL(lseq), 0);
5992908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_CONSTAT(lseq), 0);
6002908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_CONNECTION_MODES(lseq), 0);
6012908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_REG1_ISR(lseq), 0);
6022908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_REG2_ISR(lseq), 0);
6032908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_REG3_ISR(lseq), 0);
6042908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_REG0_ISR(lseq), 0);
6052908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_REG0_ISR(lseq)+4, 0);
6062908d778SJames Bottomley 
6072908d778SJames Bottomley 	/* LSEQ Mode independent page 1 setup. */
6082908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_EST_NEXUS_SCBPTR0(lseq), 0xFFFF);
6092908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_EST_NEXUS_SCBPTR1(lseq), 0xFFFF);
6102908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_EST_NEXUS_SCBPTR2(lseq), 0xFFFF);
6112908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_EST_NEXUS_SCBPTR3(lseq), 0xFFFF);
6122908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_EST_NEXUS_SCB_OPCODE0(lseq), 0);
6132908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_EST_NEXUS_SCB_OPCODE1(lseq), 0);
6142908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_EST_NEXUS_SCB_OPCODE2(lseq), 0);
6152908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_EST_NEXUS_SCB_OPCODE3(lseq), 0);
6162908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_EST_NEXUS_SCB_HEAD(lseq), 0);
6172908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_EST_NEXUS_SCB_TAIL(lseq), 0);
6182908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_EST_NEXUS_BUF_AVAIL(lseq), 0);
6192908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_TIMEOUT_CONST(lseq), 0);
6202908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_ISR_SAVE_SINDEX(lseq), 0);
6212908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_ISR_SAVE_DINDEX(lseq), 0);
6222908d778SJames Bottomley 
6232908d778SJames Bottomley 	/* LSEQ Mode Independent page 2 setup. */
6242908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_EMPTY_SCB_PTR0(lseq), 0xFFFF);
6252908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_EMPTY_SCB_PTR1(lseq), 0xFFFF);
6262908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_EMPTY_SCB_PTR2(lseq), 0xFFFF);
6272908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_EMPTY_SCB_PTR3(lseq), 0xFFFF);
6282908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_EMPTY_SCB_OPCD0(lseq), 0);
6292908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_EMPTY_SCB_OPCD1(lseq), 0);
6302908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_EMPTY_SCB_OPCD2(lseq), 0);
6312908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_EMPTY_SCB_OPCD3(lseq), 0);
6322908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_EMPTY_SCB_HEAD(lseq), 0);
6332908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_EMPTY_SCB_TAIL(lseq), 0);
6342908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_EMPTY_BUFS_AVAIL(lseq), 0);
6352908d778SJames Bottomley 	for (i = 0; i < 12; i += 4)
6362908d778SJames Bottomley 		asd_write_reg_dword(asd_ha, LmSEQ_ATA_SCR_REGS(lseq) + i, 0);
6372908d778SJames Bottomley 
6382908d778SJames Bottomley 	/* LSEQ Mode Independent page 3 setup. */
6392908d778SJames Bottomley 
6402908d778SJames Bottomley 	/* Device present timer timeout */
6412908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_DEV_PRES_TMR_TOUT_CONST(lseq),
6422908d778SJames Bottomley 			    ASD_DEV_PRESENT_TIMEOUT);
6432908d778SJames Bottomley 
6442908d778SJames Bottomley 	/* SATA interlock timer disabled */
6452908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_SATA_INTERLOCK_TIMEOUT(lseq),
6462908d778SJames Bottomley 			    ASD_SATA_INTERLOCK_TIMEOUT);
6472908d778SJames Bottomley 
6482908d778SJames Bottomley 	/* STP shutdown timer timeout constant, IGNORED by the sequencer,
6492908d778SJames Bottomley 	 * always 0. */
6502908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_STP_SHUTDOWN_TIMEOUT(lseq),
6512908d778SJames Bottomley 			    ASD_STP_SHUTDOWN_TIMEOUT);
6522908d778SJames Bottomley 
6532908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_SRST_ASSERT_TIMEOUT(lseq),
6542908d778SJames Bottomley 			    ASD_SRST_ASSERT_TIMEOUT);
6552908d778SJames Bottomley 
6562908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_RCV_FIS_TIMEOUT(lseq),
6572908d778SJames Bottomley 			    ASD_RCV_FIS_TIMEOUT);
6582908d778SJames Bottomley 
6592908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_ONE_MILLISEC_TIMEOUT(lseq),
6602908d778SJames Bottomley 			    ASD_ONE_MILLISEC_TIMEOUT);
6612908d778SJames Bottomley 
6622908d778SJames Bottomley 	/* COM_INIT timer */
6632908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_TEN_MS_COMINIT_TIMEOUT(lseq),
6642908d778SJames Bottomley 			    ASD_TEN_MILLISEC_TIMEOUT);
6652908d778SJames Bottomley 
6662908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_SMP_RCV_TIMEOUT(lseq),
6672908d778SJames Bottomley 			    ASD_SMP_RCV_TIMEOUT);
6682908d778SJames Bottomley }
6692908d778SJames Bottomley 
6702908d778SJames Bottomley /**
6712908d778SJames Bottomley  * asd_init_lseq_mdp -- initialize LSEQ mode dependent pages.
6722908d778SJames Bottomley  * @asd_ha: pointer to host adapter structure
673*966fdadfSLee Jones  * @lseq:  link sequencer
6742908d778SJames Bottomley  */
asd_init_lseq_mdp(struct asd_ha_struct * asd_ha,int lseq)6752908d778SJames Bottomley static void asd_init_lseq_mdp(struct asd_ha_struct *asd_ha,  int lseq)
6762908d778SJames Bottomley {
6772908d778SJames Bottomley 	int    i;
6782908d778SJames Bottomley 	u32    moffs;
6792908d778SJames Bottomley 	u16 ret_addr[] = {
6802908d778SJames Bottomley 		0xFFFF,		  /* mode 0 */
6812908d778SJames Bottomley 		0xFFFF,		  /* mode 1 */
6822908d778SJames Bottomley 		mode2_task,	  /* mode 2 */
6832908d778SJames Bottomley 		0,
6842908d778SJames Bottomley 		0xFFFF,		  /* mode 4/5 */
6852908d778SJames Bottomley 		0xFFFF,		  /* mode 4/5 */
6862908d778SJames Bottomley 	};
6872908d778SJames Bottomley 
6882908d778SJames Bottomley 	/*
6892908d778SJames Bottomley 	 * Mode 0,1,2 and 4/5 have common field on page 0 for the first
6902908d778SJames Bottomley 	 * 14 bytes.
6912908d778SJames Bottomley 	 */
6922908d778SJames Bottomley 	for (i = 0; i < 3; i++) {
6932908d778SJames Bottomley 		moffs = i * LSEQ_MODE_SCRATCH_SIZE;
6942908d778SJames Bottomley 		asd_write_reg_word(asd_ha, LmSEQ_RET_ADDR(lseq)+moffs,
6952908d778SJames Bottomley 				   ret_addr[i]);
6962908d778SJames Bottomley 		asd_write_reg_word(asd_ha, LmSEQ_REG0_MODE(lseq)+moffs, 0);
6972908d778SJames Bottomley 		asd_write_reg_word(asd_ha, LmSEQ_MODE_FLAGS(lseq)+moffs, 0);
6982908d778SJames Bottomley 		asd_write_reg_word(asd_ha, LmSEQ_RET_ADDR2(lseq)+moffs,0xFFFF);
6992908d778SJames Bottomley 		asd_write_reg_word(asd_ha, LmSEQ_RET_ADDR1(lseq)+moffs,0xFFFF);
7002908d778SJames Bottomley 		asd_write_reg_byte(asd_ha, LmSEQ_OPCODE_TO_CSEQ(lseq)+moffs,0);
7012908d778SJames Bottomley 		asd_write_reg_word(asd_ha, LmSEQ_DATA_TO_CSEQ(lseq)+moffs,0);
7022908d778SJames Bottomley 	}
7032908d778SJames Bottomley 	/*
7042908d778SJames Bottomley 	 *  Mode 5 page 0 overlaps the same scratch page with Mode 0 page 3.
7052908d778SJames Bottomley 	 */
7062908d778SJames Bottomley 	asd_write_reg_word(asd_ha,
7072908d778SJames Bottomley 			 LmSEQ_RET_ADDR(lseq)+LSEQ_MODE5_PAGE0_OFFSET,
7082908d778SJames Bottomley 			   ret_addr[5]);
7092908d778SJames Bottomley 	asd_write_reg_word(asd_ha,
7102908d778SJames Bottomley 			 LmSEQ_REG0_MODE(lseq)+LSEQ_MODE5_PAGE0_OFFSET,0);
7112908d778SJames Bottomley 	asd_write_reg_word(asd_ha,
7122908d778SJames Bottomley 			 LmSEQ_MODE_FLAGS(lseq)+LSEQ_MODE5_PAGE0_OFFSET, 0);
7132908d778SJames Bottomley 	asd_write_reg_word(asd_ha,
7142908d778SJames Bottomley 			 LmSEQ_RET_ADDR2(lseq)+LSEQ_MODE5_PAGE0_OFFSET,0xFFFF);
7152908d778SJames Bottomley 	asd_write_reg_word(asd_ha,
7162908d778SJames Bottomley 			 LmSEQ_RET_ADDR1(lseq)+LSEQ_MODE5_PAGE0_OFFSET,0xFFFF);
7172908d778SJames Bottomley 	asd_write_reg_byte(asd_ha,
7182908d778SJames Bottomley 		         LmSEQ_OPCODE_TO_CSEQ(lseq)+LSEQ_MODE5_PAGE0_OFFSET,0);
7192908d778SJames Bottomley 	asd_write_reg_word(asd_ha,
7202908d778SJames Bottomley 		         LmSEQ_DATA_TO_CSEQ(lseq)+LSEQ_MODE5_PAGE0_OFFSET, 0);
7212908d778SJames Bottomley 
7222908d778SJames Bottomley 	/* LSEQ Mode dependent 0, page 0 setup. */
7232908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_FIRST_INV_DDB_SITE(lseq),
7242908d778SJames Bottomley 			   (u16)asd_ha->hw_prof.max_ddbs);
7252908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_EMPTY_TRANS_CTX(lseq), 0);
7262908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_RESP_LEN(lseq), 0);
7272908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_FIRST_INV_SCB_SITE(lseq),
7282908d778SJames Bottomley 			   (u16)last_scb_site_no+1);
7292908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_INTEN_SAVE(lseq),
730355edd2eSAl Viro 			    (u16) ((LmM0INTEN_MASK & 0xFFFF0000) >> 16));
7312908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_INTEN_SAVE(lseq) + 2,
7322908d778SJames Bottomley 			    (u16) LmM0INTEN_MASK & 0xFFFF);
7332908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_LINK_RST_FRM_LEN(lseq), 0);
7342908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_LINK_RST_PROTOCOL(lseq), 0);
7352908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_RESP_STATUS(lseq), 0);
7362908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_LAST_LOADED_SGE(lseq), 0);
7372908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_SAVE_SCBPTR(lseq), 0);
7382908d778SJames Bottomley 
7392908d778SJames Bottomley 	/* LSEQ mode dependent, mode 1, page 0 setup. */
7402908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_Q_XMIT_HEAD(lseq), 0xFFFF);
7412908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_M1_EMPTY_TRANS_CTX(lseq), 0);
7422908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_INI_CONN_TAG(lseq), 0);
7432908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_FAILED_OPEN_STATUS(lseq), 0);
7442908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_XMIT_REQUEST_TYPE(lseq), 0);
7452908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_M1_RESP_STATUS(lseq), 0);
7462908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_M1_LAST_LOADED_SGE(lseq), 0);
7472908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_M1_SAVE_SCBPTR(lseq), 0);
7482908d778SJames Bottomley 
7492908d778SJames Bottomley 	/* LSEQ Mode dependent mode 2, page 0 setup */
7502908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_PORT_COUNTER(lseq), 0);
7512908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_PM_TABLE_PTR(lseq), 0);
7522908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_SATA_INTERLOCK_TMR_SAVE(lseq), 0);
7532908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_IP_BITL(lseq), 0);
7542908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_COPY_SMP_CONN_TAG(lseq), 0);
7552908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_P0M2_OFFS1AH(lseq), 0);
7562908d778SJames Bottomley 
7572908d778SJames Bottomley 	/* LSEQ Mode dependent, mode 4/5, page 0 setup. */
7582908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_SAVED_OOB_STATUS(lseq), 0);
7592908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_SAVED_OOB_MODE(lseq), 0);
7602908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_Q_LINK_HEAD(lseq), 0xFFFF);
7612908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_LINK_RST_ERR(lseq), 0);
7622908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_SAVED_OOB_SIGNALS(lseq), 0);
7632908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_SAS_RESET_MODE(lseq), 0);
7642908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_LINK_RESET_RETRY_COUNT(lseq), 0);
7652908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_NUM_LINK_RESET_RETRIES(lseq), 0);
7662908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_OOB_INT_ENABLES(lseq), 0);
7672908d778SJames Bottomley 	/*
7682908d778SJames Bottomley 	 * Set the desired interval between transmissions of the NOTIFY
769421f91d2SUwe Kleine-König 	 * (ENABLE SPINUP) primitive.  Must be initialized to val - 1.
7702908d778SJames Bottomley 	 */
7712908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_NOTIFY_TIMER_TIMEOUT(lseq),
7722908d778SJames Bottomley 			   ASD_NOTIFY_TIMEOUT - 1);
7732908d778SJames Bottomley 	/* No delay for the first NOTIFY to be sent to the attached target. */
7742908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_NOTIFY_TIMER_DOWN_COUNT(lseq),
7752908d778SJames Bottomley 			   ASD_NOTIFY_DOWN_COUNT);
776083d1631SWu, Gilbert 	asd_write_reg_word(asd_ha, LmSEQ_NOTIFY_TIMER_INITIAL_COUNT(lseq),
777083d1631SWu, Gilbert 			   ASD_NOTIFY_DOWN_COUNT);
7782908d778SJames Bottomley 
7792908d778SJames Bottomley 	/* LSEQ Mode dependent, mode 0 and 1, page 1 setup. */
7802908d778SJames Bottomley 	for (i = 0; i < 2; i++)	{
7812908d778SJames Bottomley 		int j;
7822908d778SJames Bottomley 		/* Start from Page 1 of Mode 0 and 1. */
7832908d778SJames Bottomley 		moffs = LSEQ_PAGE_SIZE + i*LSEQ_MODE_SCRATCH_SIZE;
784b595076aSUwe Kleine-König 		/* All the fields of page 1 can be initialized to 0. */
7852908d778SJames Bottomley 		for (j = 0; j < LSEQ_PAGE_SIZE; j += 4)
7862908d778SJames Bottomley 			asd_write_reg_dword(asd_ha, LmSCRATCH(lseq)+moffs+j,0);
7872908d778SJames Bottomley 	}
7882908d778SJames Bottomley 
7892908d778SJames Bottomley 	/* LSEQ Mode dependent, mode 2, page 1 setup. */
7902908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_INVALID_DWORD_COUNT(lseq), 0);
7912908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_DISPARITY_ERROR_COUNT(lseq), 0);
7922908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_LOSS_OF_SYNC_COUNT(lseq), 0);
7932908d778SJames Bottomley 
7942908d778SJames Bottomley 	/* LSEQ Mode dependent, mode 4/5, page 1. */
7952908d778SJames Bottomley 	for (i = 0; i < LSEQ_PAGE_SIZE; i+=4)
7962908d778SJames Bottomley 		asd_write_reg_dword(asd_ha, LmSEQ_FRAME_TYPE_MASK(lseq)+i, 0);
7972908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_FRAME_TYPE_MASK(lseq), 0xFF);
7982908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_HASHED_DEST_ADDR_MASK(lseq), 0xFF);
7992908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_HASHED_DEST_ADDR_MASK(lseq)+1,0xFF);
8002908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_HASHED_DEST_ADDR_MASK(lseq)+2,0xFF);
8012908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_HASHED_SRC_ADDR_MASK(lseq), 0xFF);
8022908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_HASHED_SRC_ADDR_MASK(lseq)+1, 0xFF);
8032908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_HASHED_SRC_ADDR_MASK(lseq)+2, 0xFF);
8042908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_DATA_OFFSET(lseq), 0xFFFFFFFF);
8052908d778SJames Bottomley 
8062908d778SJames Bottomley 	/* LSEQ Mode dependent, mode 0, page 2 setup. */
8072908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_SMP_RCV_TIMER_TERM_TS(lseq), 0);
8082908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_DEVICE_BITS(lseq), 0);
8092908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmSEQ_SDB_DDB(lseq), 0);
8102908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_SDB_NUM_TAGS(lseq), 0);
8112908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSEQ_SDB_CURR_TAG(lseq), 0);
8122908d778SJames Bottomley 
8132908d778SJames Bottomley 	/* LSEQ Mode Dependent 1, page 2 setup. */
8142908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_TX_ID_ADDR_FRAME(lseq), 0);
8152908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_TX_ID_ADDR_FRAME(lseq)+4, 0);
8162908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_OPEN_TIMER_TERM_TS(lseq), 0);
8172908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_SRST_AS_TIMER_TERM_TS(lseq), 0);
8182908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_LAST_LOADED_SG_EL(lseq), 0);
8192908d778SJames Bottomley 
8202908d778SJames Bottomley 	/* LSEQ Mode Dependent 2, page 2 setup. */
8212908d778SJames Bottomley 	/* The LmSEQ_STP_SHUTDOWN_TIMER_TERM_TS is IGNORED by the sequencer,
8222908d778SJames Bottomley 	 * i.e. always 0. */
8232908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_STP_SHUTDOWN_TIMER_TERM_TS(lseq),0);
8242908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_CLOSE_TIMER_TERM_TS(lseq), 0);
8252908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_BREAK_TIMER_TERM_TS(lseq), 0);
8262908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_DWS_RESET_TIMER_TERM_TS(lseq), 0);
8272908d778SJames Bottomley 	asd_write_reg_dword(asd_ha,LmSEQ_SATA_INTERLOCK_TIMER_TERM_TS(lseq),0);
8282908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_MCTL_TIMER_TERM_TS(lseq), 0);
8292908d778SJames Bottomley 
8302908d778SJames Bottomley 	/* LSEQ Mode Dependent 4/5, page 2 setup. */
8312908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_COMINIT_TIMER_TERM_TS(lseq), 0);
8322908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_RCV_ID_TIMER_TERM_TS(lseq), 0);
8332908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_RCV_FIS_TIMER_TERM_TS(lseq), 0);
8342908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmSEQ_DEV_PRES_TIMER_TERM_TS(lseq),	0);
8352908d778SJames Bottomley }
8362908d778SJames Bottomley 
8372908d778SJames Bottomley /**
8382908d778SJames Bottomley  * asd_init_lseq_scratch -- setup and init link sequencers
8392908d778SJames Bottomley  * @asd_ha: pointer to host adapter struct
8402908d778SJames Bottomley  */
asd_init_lseq_scratch(struct asd_ha_struct * asd_ha)8412908d778SJames Bottomley static void asd_init_lseq_scratch(struct asd_ha_struct *asd_ha)
8422908d778SJames Bottomley {
8432908d778SJames Bottomley 	u8 lseq;
8442908d778SJames Bottomley 	u8 lseq_mask;
8452908d778SJames Bottomley 
8462908d778SJames Bottomley 	lseq_mask = asd_ha->hw_prof.enabled_phys;
8472908d778SJames Bottomley 	for_each_sequencer(lseq_mask, lseq_mask, lseq) {
8482908d778SJames Bottomley 		asd_init_lseq_mip(asd_ha, lseq);
8492908d778SJames Bottomley 		asd_init_lseq_mdp(asd_ha, lseq);
8502908d778SJames Bottomley 	}
8512908d778SJames Bottomley }
8522908d778SJames Bottomley 
8532908d778SJames Bottomley /**
8542908d778SJames Bottomley  * asd_init_scb_sites -- initialize sequencer SCB sites (memory).
8552908d778SJames Bottomley  * @asd_ha: pointer to host adapter structure
8562908d778SJames Bottomley  *
8572908d778SJames Bottomley  * This should be done before initializing common CSEQ and LSEQ
8582908d778SJames Bottomley  * scratch since those areas depend on some computed values here,
8592908d778SJames Bottomley  * last_scb_site_no, etc.
8602908d778SJames Bottomley  */
asd_init_scb_sites(struct asd_ha_struct * asd_ha)8612908d778SJames Bottomley static void asd_init_scb_sites(struct asd_ha_struct *asd_ha)
8622908d778SJames Bottomley {
8632908d778SJames Bottomley 	u16	site_no;
8642908d778SJames Bottomley 	u16     max_scbs = 0;
8652908d778SJames Bottomley 
8662908d778SJames Bottomley 	for (site_no = asd_ha->hw_prof.max_scbs-1;
8672908d778SJames Bottomley 	     site_no != (u16) -1;
8682908d778SJames Bottomley 	     site_no--) {
8692908d778SJames Bottomley 		u16	i;
8702908d778SJames Bottomley 
8712908d778SJames Bottomley 		/* Initialize all fields in the SCB site to 0. */
8722908d778SJames Bottomley 		for (i = 0; i < ASD_SCB_SIZE; i += 4)
8732908d778SJames Bottomley 			asd_scbsite_write_dword(asd_ha, site_no, i, 0);
8742908d778SJames Bottomley 
8753b709df5SDarrick J. Wong 		/* Initialize SCB Site Opcode field to invalid. */
8763b709df5SDarrick J. Wong 		asd_scbsite_write_byte(asd_ha, site_no,
8773b709df5SDarrick J. Wong 				       offsetof(struct scb_header, opcode),
8783b709df5SDarrick J. Wong 				       0xFF);
8793b709df5SDarrick J. Wong 
8803b709df5SDarrick J. Wong 		/* Initialize SCB Site Flags field to mean a response
8813b709df5SDarrick J. Wong 		 * frame has been received.  This means inadvertent
8823b709df5SDarrick J. Wong 		 * frames received to be dropped. */
8833b709df5SDarrick J. Wong 		asd_scbsite_write_byte(asd_ha, site_no, 0x49, 0x01);
8843b709df5SDarrick J. Wong 
8852908d778SJames Bottomley 		/* Workaround needed by SEQ to fix a SATA issue is to exclude
8862908d778SJames Bottomley 		 * certain SCB sites from the free list. */
8872908d778SJames Bottomley 		if (!SCB_SITE_VALID(site_no))
8882908d778SJames Bottomley 			continue;
8892908d778SJames Bottomley 
8902908d778SJames Bottomley 		if (last_scb_site_no == 0)
8912908d778SJames Bottomley 			last_scb_site_no = site_no;
8922908d778SJames Bottomley 
8932908d778SJames Bottomley 		/* For every SCB site, we need to initialize the
8942908d778SJames Bottomley 		 * following fields: Q_NEXT, SCB_OPCODE, SCB_FLAGS,
8952908d778SJames Bottomley 		 * and SG Element Flag. */
8962908d778SJames Bottomley 
8972908d778SJames Bottomley 		/* Q_NEXT field of the last SCB is invalidated. */
8982908d778SJames Bottomley 		asd_scbsite_write_word(asd_ha, site_no, 0, first_scb_site_no);
8992908d778SJames Bottomley 
9002908d778SJames Bottomley 		first_scb_site_no = site_no;
9012908d778SJames Bottomley 		max_scbs++;
9022908d778SJames Bottomley 	}
9032908d778SJames Bottomley 	asd_ha->hw_prof.max_scbs = max_scbs;
9042908d778SJames Bottomley 	ASD_DPRINTK("max_scbs:%d\n", asd_ha->hw_prof.max_scbs);
9052908d778SJames Bottomley 	ASD_DPRINTK("first_scb_site_no:0x%x\n", first_scb_site_no);
9062908d778SJames Bottomley 	ASD_DPRINTK("last_scb_site_no:0x%x\n", last_scb_site_no);
9072908d778SJames Bottomley }
9082908d778SJames Bottomley 
9092908d778SJames Bottomley /**
9102908d778SJames Bottomley  * asd_init_cseq_cio - initialize CSEQ CIO registers
9112908d778SJames Bottomley  * @asd_ha: pointer to host adapter structure
9122908d778SJames Bottomley  */
asd_init_cseq_cio(struct asd_ha_struct * asd_ha)9132908d778SJames Bottomley static void asd_init_cseq_cio(struct asd_ha_struct *asd_ha)
9142908d778SJames Bottomley {
9152908d778SJames Bottomley 	int i;
9162908d778SJames Bottomley 
9172908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, CSEQCOMINTEN, 0);
9182908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, CSEQDLCTL, ASD_DL_SIZE_BITS);
9192908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, CSEQDLOFFS, 0);
9202908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, CSEQDLOFFS+1, 0);
9212908d778SJames Bottomley 	asd_ha->seq.scbpro = 0;
9222908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, SCBPRO, 0);
9232908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, CSEQCON, 0);
9242908d778SJames Bottomley 
925b595076aSUwe Kleine-König 	/* Initialize CSEQ Mode 11 Interrupt Vectors.
9262908d778SJames Bottomley 	 * The addresses are 16 bit wide and in dword units.
9272908d778SJames Bottomley 	 * The values of their macros are in byte units.
9282908d778SJames Bottomley 	 * Thus we have to divide by 4. */
9292908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CM11INTVEC0, cseq_vecs[0]);
9302908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CM11INTVEC1, cseq_vecs[1]);
9312908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CM11INTVEC2, cseq_vecs[2]);
9322908d778SJames Bottomley 
9332908d778SJames Bottomley 	/* Enable ARP2HALTC (ARP2 Halted from Halt Code Write). */
9342908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, CARP2INTEN, EN_ARP2HALTC);
9352908d778SJames Bottomley 
9362908d778SJames Bottomley 	/* Initialize CSEQ Scratch Page to 0x04. */
9372908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, CSCRATCHPAGE, 0x04);
9382908d778SJames Bottomley 
9392908d778SJames Bottomley 	/* Initialize CSEQ Mode[0-8] Dependent registers. */
9402908d778SJames Bottomley 	/* Initialize Scratch Page to 0. */
9412908d778SJames Bottomley 	for (i = 0; i < 9; i++)
9422908d778SJames Bottomley 		asd_write_reg_byte(asd_ha, CMnSCRATCHPAGE(i), 0);
9432908d778SJames Bottomley 
9442908d778SJames Bottomley 	/* Reset the ARP2 Program Count. */
9452908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CPRGMCNT, cseq_idle_loop);
9462908d778SJames Bottomley 
9472908d778SJames Bottomley 	for (i = 0; i < 8; i++) {
948b595076aSUwe Kleine-König 		/* Initialize Mode n Link m Interrupt Enable. */
9492908d778SJames Bottomley 		asd_write_reg_dword(asd_ha, CMnINTEN(i), EN_CMnRSPMBXF);
9502908d778SJames Bottomley 		/* Initialize Mode n Request Mailbox. */
9512908d778SJames Bottomley 		asd_write_reg_dword(asd_ha, CMnREQMBX(i), 0);
9522908d778SJames Bottomley 	}
9532908d778SJames Bottomley }
9542908d778SJames Bottomley 
9552908d778SJames Bottomley /**
9562908d778SJames Bottomley  * asd_init_lseq_cio -- initialize LmSEQ CIO registers
9572908d778SJames Bottomley  * @asd_ha: pointer to host adapter structure
958*966fdadfSLee Jones  * @lseq:  link sequencer
9592908d778SJames Bottomley  */
asd_init_lseq_cio(struct asd_ha_struct * asd_ha,int lseq)9602908d778SJames Bottomley static void asd_init_lseq_cio(struct asd_ha_struct *asd_ha, int lseq)
9612908d778SJames Bottomley {
9622908d778SJames Bottomley 	u8  *sas_addr;
9632908d778SJames Bottomley 	int  i;
9642908d778SJames Bottomley 
9652908d778SJames Bottomley 	/* Enable ARP2HALTC (ARP2 Halted from Halt Code Write). */
9662908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmARP2INTEN(lseq), EN_ARP2HALTC);
9672908d778SJames Bottomley 
9682908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmSCRATCHPAGE(lseq), 0);
9692908d778SJames Bottomley 
9702908d778SJames Bottomley 	/* Initialize Mode 0,1, and 2 SCRATCHPAGE to 0. */
9712908d778SJames Bottomley 	for (i = 0; i < 3; i++)
9722908d778SJames Bottomley 		asd_write_reg_byte(asd_ha, LmMnSCRATCHPAGE(lseq, i), 0);
9732908d778SJames Bottomley 
9742908d778SJames Bottomley 	/* Initialize Mode 5 SCRATCHPAGE to 0. */
9752908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmMnSCRATCHPAGE(lseq, 5), 0);
9762908d778SJames Bottomley 
9772908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmRSPMBX(lseq), 0);
9782908d778SJames Bottomley 	/* Initialize Mode 0,1,2 and 5 Interrupt Enable and
9792908d778SJames Bottomley 	 * Interrupt registers. */
9802908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmMnINTEN(lseq, 0), LmM0INTEN_MASK);
9812908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmMnINT(lseq, 0), 0xFFFFFFFF);
9822908d778SJames Bottomley 	/* Mode 1 */
9832908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmMnINTEN(lseq, 1), LmM1INTEN_MASK);
9842908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmMnINT(lseq, 1), 0xFFFFFFFF);
9852908d778SJames Bottomley 	/* Mode 2 */
9862908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmMnINTEN(lseq, 2), LmM2INTEN_MASK);
9872908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmMnINT(lseq, 2), 0xFFFFFFFF);
9882908d778SJames Bottomley 	/* Mode 5 */
9892908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmMnINTEN(lseq, 5), LmM5INTEN_MASK);
9902908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmMnINT(lseq, 5), 0xFFFFFFFF);
9912908d778SJames Bottomley 
9922908d778SJames Bottomley 	/* Enable HW Timer status. */
9932908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmHWTSTATEN(lseq), LmHWTSTATEN_MASK);
9942908d778SJames Bottomley 
9952908d778SJames Bottomley 	/* Enable Primitive Status 0 and 1. */
9962908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmPRIMSTAT0EN(lseq), LmPRIMSTAT0EN_MASK);
9972908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmPRIMSTAT1EN(lseq), LmPRIMSTAT1EN_MASK);
9982908d778SJames Bottomley 
9992908d778SJames Bottomley 	/* Enable Frame Error. */
10002908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmFRMERREN(lseq), LmFRMERREN_MASK);
10012908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmMnHOLDLVL(lseq, 0), 0x50);
10022908d778SJames Bottomley 
10032908d778SJames Bottomley 	/* Initialize Mode 0 Transfer Level to 512. */
10042908d778SJames Bottomley 	asd_write_reg_byte(asd_ha,  LmMnXFRLVL(lseq, 0), LmMnXFRLVL_512);
10052908d778SJames Bottomley 	/* Initialize Mode 1 Transfer Level to 256. */
10062908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmMnXFRLVL(lseq, 1), LmMnXFRLVL_256);
10072908d778SJames Bottomley 
10082908d778SJames Bottomley 	/* Initialize Program Count. */
10092908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmPRGMCNT(lseq), lseq_idle_loop);
10102908d778SJames Bottomley 
10112908d778SJames Bottomley 	/* Enable Blind SG Move. */
10122908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmMODECTL(lseq), LmBLIND48);
10132908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmM3SATATIMER(lseq),
10142908d778SJames Bottomley 			   ASD_SATA_INTERLOCK_TIMEOUT);
10152908d778SJames Bottomley 
10162908d778SJames Bottomley 	(void) asd_read_reg_dword(asd_ha, LmREQMBX(lseq));
10172908d778SJames Bottomley 
10182908d778SJames Bottomley 	/* Clear Primitive Status 0 and 1. */
10192908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmPRMSTAT0(lseq), 0xFFFFFFFF);
10202908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmPRMSTAT1(lseq), 0xFFFFFFFF);
10212908d778SJames Bottomley 
10222908d778SJames Bottomley 	/* Clear HW Timer status. */
10232908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmHWTSTAT(lseq), 0xFF);
10242908d778SJames Bottomley 
10252908d778SJames Bottomley 	/* Clear DMA Errors for Mode 0 and 1. */
10262908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmMnDMAERRS(lseq, 0), 0xFF);
10272908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmMnDMAERRS(lseq, 1), 0xFF);
10282908d778SJames Bottomley 
10292908d778SJames Bottomley 	/* Clear SG DMA Errors for Mode 0 and 1. */
10302908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmMnSGDMAERRS(lseq, 0), 0xFF);
10312908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmMnSGDMAERRS(lseq, 1), 0xFF);
10322908d778SJames Bottomley 
10332908d778SJames Bottomley 	/* Clear Mode 0 Buffer Parity Error. */
10342908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmMnBUFSTAT(lseq, 0), LmMnBUFPERR);
10352908d778SJames Bottomley 
10362908d778SJames Bottomley 	/* Clear Mode 0 Frame Error register. */
10372908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmMnFRMERR(lseq, 0), 0xFFFFFFFF);
10382908d778SJames Bottomley 
10392908d778SJames Bottomley 	/* Reset LSEQ external interrupt arbiter. */
10402908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmARP2INTCTL(lseq), RSTINTCTL);
10412908d778SJames Bottomley 
10422908d778SJames Bottomley 	/* Set the Phy SAS for the LmSEQ WWN. */
10432908d778SJames Bottomley 	sas_addr = asd_ha->phys[lseq].phy_desc->sas_addr;
10442908d778SJames Bottomley 	for (i = 0; i < SAS_ADDR_SIZE; i++)
10452908d778SJames Bottomley 		asd_write_reg_byte(asd_ha, LmWWN(lseq) + i, sas_addr[i]);
10462908d778SJames Bottomley 
10472908d778SJames Bottomley 	/* Set the Transmit Size to 1024 bytes, 0 = 256 Dwords. */
10482908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmMnXMTSIZE(lseq, 1), 0);
10492908d778SJames Bottomley 
10502908d778SJames Bottomley 	/* Set the Bus Inactivity Time Limit Timer. */
10512908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmBITL_TIMER(lseq), 9);
10522908d778SJames Bottomley 
10532908d778SJames Bottomley 	/* Enable SATA Port Multiplier. */
10542908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmMnSATAFS(lseq, 1), 0x80);
10552908d778SJames Bottomley 
10562908d778SJames Bottomley 	/* Initialize Interrupt Vector[0-10] address in Mode 3.
10572908d778SJames Bottomley 	 * See the comment on CSEQ_INT_* */
10582908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmM3INTVEC0(lseq), lseq_vecs[0]);
10592908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmM3INTVEC1(lseq), lseq_vecs[1]);
10602908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmM3INTVEC2(lseq), lseq_vecs[2]);
10612908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmM3INTVEC3(lseq), lseq_vecs[3]);
10622908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmM3INTVEC4(lseq), lseq_vecs[4]);
10632908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmM3INTVEC5(lseq), lseq_vecs[5]);
10642908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmM3INTVEC6(lseq), lseq_vecs[6]);
10652908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmM3INTVEC7(lseq), lseq_vecs[7]);
10662908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmM3INTVEC8(lseq), lseq_vecs[8]);
10672908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmM3INTVEC9(lseq), lseq_vecs[9]);
10682908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmM3INTVEC10(lseq), lseq_vecs[10]);
10692908d778SJames Bottomley 	/*
10702908d778SJames Bottomley 	 * Program the Link LED control, applicable only for
10712908d778SJames Bottomley 	 * Chip Rev. B or later.
10722908d778SJames Bottomley 	 */
10732908d778SJames Bottomley 	asd_write_reg_dword(asd_ha, LmCONTROL(lseq),
10742908d778SJames Bottomley 			    (LEDTIMER | LEDMODE_TXRX | LEDTIMERS_100ms));
10752908d778SJames Bottomley 
10762908d778SJames Bottomley 	/* Set the Align Rate for SAS and STP mode. */
10772908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmM1SASALIGN(lseq), SAS_ALIGN_DEFAULT);
10782908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, LmM1STPALIGN(lseq), STP_ALIGN_DEFAULT);
10792908d778SJames Bottomley }
10802908d778SJames Bottomley 
10812908d778SJames Bottomley 
10822908d778SJames Bottomley /**
10832908d778SJames Bottomley  * asd_post_init_cseq -- clear CSEQ Mode n Int. status and Response mailbox
10842908d778SJames Bottomley  * @asd_ha: pointer to host adapter struct
10852908d778SJames Bottomley  */
asd_post_init_cseq(struct asd_ha_struct * asd_ha)10862908d778SJames Bottomley static void asd_post_init_cseq(struct asd_ha_struct *asd_ha)
10872908d778SJames Bottomley {
10882908d778SJames Bottomley 	int i;
10892908d778SJames Bottomley 
10902908d778SJames Bottomley 	for (i = 0; i < 8; i++)
10912908d778SJames Bottomley 		asd_write_reg_dword(asd_ha, CMnINT(i), 0xFFFFFFFF);
10922908d778SJames Bottomley 	for (i = 0; i < 8; i++)
10932908d778SJames Bottomley 		asd_read_reg_dword(asd_ha, CMnRSPMBX(i));
10942908d778SJames Bottomley 	/* Reset the external interrupt arbiter. */
10952908d778SJames Bottomley 	asd_write_reg_byte(asd_ha, CARP2INTCTL, RSTINTCTL);
10962908d778SJames Bottomley }
10972908d778SJames Bottomley 
10982908d778SJames Bottomley /**
10992908d778SJames Bottomley  * asd_init_ddb_0 -- initialize DDB 0
11002908d778SJames Bottomley  * @asd_ha: pointer to host adapter structure
11012908d778SJames Bottomley  *
11022908d778SJames Bottomley  * Initialize DDB site 0 which is used internally by the sequencer.
11032908d778SJames Bottomley  */
asd_init_ddb_0(struct asd_ha_struct * asd_ha)11042908d778SJames Bottomley static void asd_init_ddb_0(struct asd_ha_struct *asd_ha)
11052908d778SJames Bottomley {
11062908d778SJames Bottomley 	int	i;
11072908d778SJames Bottomley 
11082908d778SJames Bottomley 	/* Zero out the DDB explicitly */
11092908d778SJames Bottomley 	for (i = 0; i < sizeof(struct asd_ddb_seq_shared); i+=4)
11102908d778SJames Bottomley 		asd_ddbsite_write_dword(asd_ha, 0, i, 0);
11112908d778SJames Bottomley 
11122908d778SJames Bottomley 	asd_ddbsite_write_word(asd_ha, 0,
11132908d778SJames Bottomley 		 offsetof(struct asd_ddb_seq_shared, q_free_ddb_head), 0);
11142908d778SJames Bottomley 	asd_ddbsite_write_word(asd_ha, 0,
11152908d778SJames Bottomley 		 offsetof(struct asd_ddb_seq_shared, q_free_ddb_tail),
11162908d778SJames Bottomley 			       asd_ha->hw_prof.max_ddbs-1);
11172908d778SJames Bottomley 	asd_ddbsite_write_word(asd_ha, 0,
11182908d778SJames Bottomley 		 offsetof(struct asd_ddb_seq_shared, q_free_ddb_cnt), 0);
11192908d778SJames Bottomley 	asd_ddbsite_write_word(asd_ha, 0,
11202908d778SJames Bottomley 		 offsetof(struct asd_ddb_seq_shared, q_used_ddb_head), 0xFFFF);
11212908d778SJames Bottomley 	asd_ddbsite_write_word(asd_ha, 0,
11222908d778SJames Bottomley 		 offsetof(struct asd_ddb_seq_shared, q_used_ddb_tail), 0xFFFF);
11232908d778SJames Bottomley 	asd_ddbsite_write_word(asd_ha, 0,
11242908d778SJames Bottomley 		 offsetof(struct asd_ddb_seq_shared, shared_mem_lock), 0);
11252908d778SJames Bottomley 	asd_ddbsite_write_word(asd_ha, 0,
11262908d778SJames Bottomley 		 offsetof(struct asd_ddb_seq_shared, smp_conn_tag), 0);
11272908d778SJames Bottomley 	asd_ddbsite_write_word(asd_ha, 0,
11282908d778SJames Bottomley 		 offsetof(struct asd_ddb_seq_shared, est_nexus_buf_cnt), 0);
11292908d778SJames Bottomley 	asd_ddbsite_write_word(asd_ha, 0,
11302908d778SJames Bottomley 		 offsetof(struct asd_ddb_seq_shared, est_nexus_buf_thresh),
11312908d778SJames Bottomley 			       asd_ha->hw_prof.num_phys * 2);
11322908d778SJames Bottomley 	asd_ddbsite_write_byte(asd_ha, 0,
11332908d778SJames Bottomley 		 offsetof(struct asd_ddb_seq_shared, settable_max_contexts),0);
11342908d778SJames Bottomley 	asd_ddbsite_write_byte(asd_ha, 0,
11352908d778SJames Bottomley 	       offsetof(struct asd_ddb_seq_shared, conn_not_active), 0xFF);
11362908d778SJames Bottomley 	asd_ddbsite_write_byte(asd_ha, 0,
11372908d778SJames Bottomley 	       offsetof(struct asd_ddb_seq_shared, phy_is_up), 0x00);
11382908d778SJames Bottomley 	/* DDB 0 is reserved */
11392908d778SJames Bottomley 	set_bit(0, asd_ha->hw_prof.ddb_bitmap);
11402908d778SJames Bottomley }
11412908d778SJames Bottomley 
asd_seq_init_ddb_sites(struct asd_ha_struct * asd_ha)11423b709df5SDarrick J. Wong static void asd_seq_init_ddb_sites(struct asd_ha_struct *asd_ha)
11433b709df5SDarrick J. Wong {
11443b709df5SDarrick J. Wong 	unsigned int i;
11453b709df5SDarrick J. Wong 	unsigned int ddb_site;
11463b709df5SDarrick J. Wong 
11473b709df5SDarrick J. Wong 	for (ddb_site = 0 ; ddb_site < ASD_MAX_DDBS; ddb_site++)
11483b709df5SDarrick J. Wong 		for (i = 0; i < sizeof(struct asd_ddb_ssp_smp_target_port); i+= 4)
11493b709df5SDarrick J. Wong 			asd_ddbsite_write_dword(asd_ha, ddb_site, i, 0);
11503b709df5SDarrick J. Wong }
11513b709df5SDarrick J. Wong 
11522908d778SJames Bottomley /**
11532908d778SJames Bottomley  * asd_seq_setup_seqs -- setup and initialize central and link sequencers
11542908d778SJames Bottomley  * @asd_ha: pointer to host adapter structure
11552908d778SJames Bottomley  */
asd_seq_setup_seqs(struct asd_ha_struct * asd_ha)11562908d778SJames Bottomley static void asd_seq_setup_seqs(struct asd_ha_struct *asd_ha)
11572908d778SJames Bottomley {
11582908d778SJames Bottomley 	int 		lseq;
11592908d778SJames Bottomley 	u8		lseq_mask;
11602908d778SJames Bottomley 
11613b709df5SDarrick J. Wong 	/* Initialize DDB sites */
11623b709df5SDarrick J. Wong 	asd_seq_init_ddb_sites(asd_ha);
11633b709df5SDarrick J. Wong 
11642908d778SJames Bottomley 	/* Initialize SCB sites. Done first to compute some values which
11652908d778SJames Bottomley 	 * the rest of the init code depends on. */
11662908d778SJames Bottomley 	asd_init_scb_sites(asd_ha);
11672908d778SJames Bottomley 
11682908d778SJames Bottomley 	/* Initialize CSEQ Scratch RAM registers. */
11692908d778SJames Bottomley 	asd_init_cseq_scratch(asd_ha);
11702908d778SJames Bottomley 
11712908d778SJames Bottomley 	/* Initialize LmSEQ Scratch RAM registers. */
11722908d778SJames Bottomley 	asd_init_lseq_scratch(asd_ha);
11732908d778SJames Bottomley 
11742908d778SJames Bottomley 	/* Initialize CSEQ CIO registers. */
11752908d778SJames Bottomley 	asd_init_cseq_cio(asd_ha);
11762908d778SJames Bottomley 
11772908d778SJames Bottomley 	asd_init_ddb_0(asd_ha);
11782908d778SJames Bottomley 
11792908d778SJames Bottomley 	/* Initialize LmSEQ CIO registers. */
11802908d778SJames Bottomley 	lseq_mask = asd_ha->hw_prof.enabled_phys;
11812908d778SJames Bottomley 	for_each_sequencer(lseq_mask, lseq_mask, lseq)
11822908d778SJames Bottomley 		asd_init_lseq_cio(asd_ha, lseq);
11832908d778SJames Bottomley 	asd_post_init_cseq(asd_ha);
11842908d778SJames Bottomley }
11852908d778SJames Bottomley 
11862908d778SJames Bottomley 
11872908d778SJames Bottomley /**
11882908d778SJames Bottomley  * asd_seq_start_cseq -- start the central sequencer, CSEQ
11892908d778SJames Bottomley  * @asd_ha: pointer to host adapter structure
11902908d778SJames Bottomley  */
asd_seq_start_cseq(struct asd_ha_struct * asd_ha)11912908d778SJames Bottomley static int asd_seq_start_cseq(struct asd_ha_struct *asd_ha)
11922908d778SJames Bottomley {
11932908d778SJames Bottomley 	/* Reset the ARP2 instruction to location zero. */
11942908d778SJames Bottomley 	asd_write_reg_word(asd_ha, CPRGMCNT, cseq_idle_loop);
11952908d778SJames Bottomley 
11962908d778SJames Bottomley 	/* Unpause the CSEQ  */
11972908d778SJames Bottomley 	return asd_unpause_cseq(asd_ha);
11982908d778SJames Bottomley }
11992908d778SJames Bottomley 
12002908d778SJames Bottomley /**
12012908d778SJames Bottomley  * asd_seq_start_lseq -- start a link sequencer
12022908d778SJames Bottomley  * @asd_ha: pointer to host adapter structure
12032908d778SJames Bottomley  * @lseq: the link sequencer of interest
12042908d778SJames Bottomley  */
asd_seq_start_lseq(struct asd_ha_struct * asd_ha,int lseq)12052908d778SJames Bottomley static int asd_seq_start_lseq(struct asd_ha_struct *asd_ha, int lseq)
12062908d778SJames Bottomley {
12072908d778SJames Bottomley 	/* Reset the ARP2 instruction to location zero. */
12082908d778SJames Bottomley 	asd_write_reg_word(asd_ha, LmPRGMCNT(lseq), lseq_idle_loop);
12092908d778SJames Bottomley 
12102908d778SJames Bottomley 	/* Unpause the LmSEQ  */
12112908d778SJames Bottomley 	return asd_seq_unpause_lseq(asd_ha, lseq);
12122908d778SJames Bottomley }
12132908d778SJames Bottomley 
asd_release_firmware(void)1214bf2a1928SDarrick J. Wong int asd_release_firmware(void)
1215bf2a1928SDarrick J. Wong {
1216bf2a1928SDarrick J. Wong 	release_firmware(sequencer_fw);
1217bf2a1928SDarrick J. Wong 	return 0;
1218bf2a1928SDarrick J. Wong }
1219bf2a1928SDarrick J. Wong 
asd_request_firmware(struct asd_ha_struct * asd_ha)12202908d778SJames Bottomley static int asd_request_firmware(struct asd_ha_struct *asd_ha)
12212908d778SJames Bottomley {
12222908d778SJames Bottomley 	int err, i;
12230bc202e0SDavid Woodhouse 	struct sequencer_file_header header;
12240bc202e0SDavid Woodhouse 	const struct sequencer_file_header *hdr_ptr;
12252908d778SJames Bottomley 	u32 csum = 0;
12262908d778SJames Bottomley 	u16 *ptr_cseq_vecs, *ptr_lseq_vecs;
12272908d778SJames Bottomley 
12282908d778SJames Bottomley 	if (sequencer_fw)
12292908d778SJames Bottomley 		/* already loaded */
12302908d778SJames Bottomley 		return 0;
12312908d778SJames Bottomley 
12322908d778SJames Bottomley 	err = request_firmware(&sequencer_fw,
12332908d778SJames Bottomley 			       SAS_RAZOR_SEQUENCER_FW_FILE,
12342908d778SJames Bottomley 			       &asd_ha->pcidev->dev);
12352908d778SJames Bottomley 	if (err)
12362908d778SJames Bottomley 		return err;
12372908d778SJames Bottomley 
12380bc202e0SDavid Woodhouse 	hdr_ptr = (const struct sequencer_file_header *)sequencer_fw->data;
12392908d778SJames Bottomley 
12402908d778SJames Bottomley 	header.csum = le32_to_cpu(hdr_ptr->csum);
12412908d778SJames Bottomley 	header.major = le32_to_cpu(hdr_ptr->major);
12422908d778SJames Bottomley 	header.minor = le32_to_cpu(hdr_ptr->minor);
12432908d778SJames Bottomley 	header.cseq_table_offset = le32_to_cpu(hdr_ptr->cseq_table_offset);
12442908d778SJames Bottomley 	header.cseq_table_size = le32_to_cpu(hdr_ptr->cseq_table_size);
12452908d778SJames Bottomley 	header.lseq_table_offset = le32_to_cpu(hdr_ptr->lseq_table_offset);
12462908d778SJames Bottomley 	header.lseq_table_size = le32_to_cpu(hdr_ptr->lseq_table_size);
12472908d778SJames Bottomley 	header.cseq_code_offset = le32_to_cpu(hdr_ptr->cseq_code_offset);
12482908d778SJames Bottomley 	header.cseq_code_size = le32_to_cpu(hdr_ptr->cseq_code_size);
12492908d778SJames Bottomley 	header.lseq_code_offset = le32_to_cpu(hdr_ptr->lseq_code_offset);
12502908d778SJames Bottomley 	header.lseq_code_size = le32_to_cpu(hdr_ptr->lseq_code_size);
12512908d778SJames Bottomley 	header.mode2_task = le16_to_cpu(hdr_ptr->mode2_task);
12522908d778SJames Bottomley 	header.cseq_idle_loop = le16_to_cpu(hdr_ptr->cseq_idle_loop);
12532908d778SJames Bottomley 	header.lseq_idle_loop = le16_to_cpu(hdr_ptr->lseq_idle_loop);
12542908d778SJames Bottomley 
12552908d778SJames Bottomley 	for (i = sizeof(header.csum); i < sequencer_fw->size; i++)
12562908d778SJames Bottomley 		csum += sequencer_fw->data[i];
12572908d778SJames Bottomley 
12582908d778SJames Bottomley 	if (csum != header.csum) {
12592908d778SJames Bottomley 		asd_printk("Firmware file checksum mismatch\n");
12602908d778SJames Bottomley 		return -EINVAL;
12612908d778SJames Bottomley 	}
12622908d778SJames Bottomley 
12632908d778SJames Bottomley 	if (header.cseq_table_size != CSEQ_NUM_VECS ||
12642908d778SJames Bottomley 	    header.lseq_table_size != LSEQ_NUM_VECS) {
12652908d778SJames Bottomley 		asd_printk("Firmware file table size mismatch\n");
12662908d778SJames Bottomley 		return -EINVAL;
12672908d778SJames Bottomley 	}
12682908d778SJames Bottomley 
1269a29fdd3cSJames Bottomley 	asd_printk("Found sequencer Firmware version %d.%d (%s)\n",
1270a29fdd3cSJames Bottomley 		   header.major, header.minor, hdr_ptr->version);
1271a29fdd3cSJames Bottomley 
1272a29fdd3cSJames Bottomley 	if (header.major != SAS_RAZOR_SEQUENCER_FW_MAJOR) {
1273a29fdd3cSJames Bottomley 		asd_printk("Firmware Major Version Mismatch;"
1274a29fdd3cSJames Bottomley 			   "driver requires version %d.X",
1275a29fdd3cSJames Bottomley 			   SAS_RAZOR_SEQUENCER_FW_MAJOR);
1276a29fdd3cSJames Bottomley 		return -EINVAL;
1277a29fdd3cSJames Bottomley 	}
1278a29fdd3cSJames Bottomley 
12792908d778SJames Bottomley 	ptr_cseq_vecs = (u16 *)&sequencer_fw->data[header.cseq_table_offset];
12802908d778SJames Bottomley 	ptr_lseq_vecs = (u16 *)&sequencer_fw->data[header.lseq_table_offset];
12812908d778SJames Bottomley 	mode2_task = header.mode2_task;
12822908d778SJames Bottomley 	cseq_idle_loop = header.cseq_idle_loop;
12832908d778SJames Bottomley 	lseq_idle_loop = header.lseq_idle_loop;
12842908d778SJames Bottomley 
12852908d778SJames Bottomley 	for (i = 0; i < CSEQ_NUM_VECS; i++)
12862908d778SJames Bottomley 		cseq_vecs[i] = le16_to_cpu(ptr_cseq_vecs[i]);
12872908d778SJames Bottomley 
12882908d778SJames Bottomley 	for (i = 0; i < LSEQ_NUM_VECS; i++)
12892908d778SJames Bottomley 		lseq_vecs[i] = le16_to_cpu(ptr_lseq_vecs[i]);
12902908d778SJames Bottomley 
12912908d778SJames Bottomley 	cseq_code = &sequencer_fw->data[header.cseq_code_offset];
12922908d778SJames Bottomley 	cseq_code_size = header.cseq_code_size;
12932908d778SJames Bottomley 	lseq_code = &sequencer_fw->data[header.lseq_code_offset];
12942908d778SJames Bottomley 	lseq_code_size = header.lseq_code_size;
12952908d778SJames Bottomley 
12962908d778SJames Bottomley 	return 0;
12972908d778SJames Bottomley }
12982908d778SJames Bottomley 
asd_init_seqs(struct asd_ha_struct * asd_ha)12992908d778SJames Bottomley int asd_init_seqs(struct asd_ha_struct *asd_ha)
13002908d778SJames Bottomley {
13012908d778SJames Bottomley 	int err;
13022908d778SJames Bottomley 
13032908d778SJames Bottomley 	err = asd_request_firmware(asd_ha);
13042908d778SJames Bottomley 
13052908d778SJames Bottomley 	if (err) {
13062908d778SJames Bottomley 		asd_printk("Failed to load sequencer firmware file %s, error %d\n",
13072908d778SJames Bottomley 			   SAS_RAZOR_SEQUENCER_FW_FILE, err);
13082908d778SJames Bottomley 		return err;
13092908d778SJames Bottomley 	}
13102908d778SJames Bottomley 
13112908d778SJames Bottomley 	err = asd_seq_download_seqs(asd_ha);
13122908d778SJames Bottomley 	if (err) {
13132908d778SJames Bottomley 		asd_printk("couldn't download sequencers for %s\n",
13142908d778SJames Bottomley 			   pci_name(asd_ha->pcidev));
13152908d778SJames Bottomley 		return err;
13162908d778SJames Bottomley 	}
13172908d778SJames Bottomley 
13182908d778SJames Bottomley 	asd_seq_setup_seqs(asd_ha);
13192908d778SJames Bottomley 
13202908d778SJames Bottomley 	return 0;
13212908d778SJames Bottomley }
13222908d778SJames Bottomley 
asd_start_seqs(struct asd_ha_struct * asd_ha)13232908d778SJames Bottomley int asd_start_seqs(struct asd_ha_struct *asd_ha)
13242908d778SJames Bottomley {
13252908d778SJames Bottomley 	int err;
13262908d778SJames Bottomley 	u8  lseq_mask;
13272908d778SJames Bottomley 	int lseq;
13282908d778SJames Bottomley 
13292908d778SJames Bottomley 	err = asd_seq_start_cseq(asd_ha);
13302908d778SJames Bottomley 	if (err) {
13312908d778SJames Bottomley 		asd_printk("couldn't start CSEQ for %s\n",
13322908d778SJames Bottomley 			   pci_name(asd_ha->pcidev));
13332908d778SJames Bottomley 		return err;
13342908d778SJames Bottomley 	}
13352908d778SJames Bottomley 
13362908d778SJames Bottomley 	lseq_mask = asd_ha->hw_prof.enabled_phys;
13372908d778SJames Bottomley 	for_each_sequencer(lseq_mask, lseq_mask, lseq) {
13382908d778SJames Bottomley 		err = asd_seq_start_lseq(asd_ha, lseq);
13392908d778SJames Bottomley 		if (err) {
1340c19ca6cbSMasanari Iida 			asd_printk("couldn't start LSEQ %d for %s\n", lseq,
13412908d778SJames Bottomley 				   pci_name(asd_ha->pcidev));
13422908d778SJames Bottomley 			return err;
13432908d778SJames Bottomley 		}
13442908d778SJames Bottomley 	}
13452908d778SJames Bottomley 
13462908d778SJames Bottomley 	return 0;
13472908d778SJames Bottomley }
13482908d778SJames Bottomley 
13492908d778SJames Bottomley /**
13502908d778SJames Bottomley  * asd_update_port_links -- update port_map_by_links and phy_is_up
1351*966fdadfSLee Jones  * @asd_ha: pointer to host adapter structure
1352*966fdadfSLee Jones  * @phy: pointer to the phy which has been added to a port
13532908d778SJames Bottomley  *
13542908d778SJames Bottomley  * 1) When a link reset has completed and we got BYTES DMAED with a
13552908d778SJames Bottomley  * valid frame we call this function for that phy, to indicate that
13562908d778SJames Bottomley  * the phy is up, i.e. we update the phy_is_up in DDB 0.  The
13572908d778SJames Bottomley  * sequencer checks phy_is_up when pending SCBs are to be sent, and
13582908d778SJames Bottomley  * when an open address frame has been received.
13592908d778SJames Bottomley  *
13602908d778SJames Bottomley  * 2) When we know of ports, we call this function to update the map
13612908d778SJames Bottomley  * of phys participaing in that port, i.e. we update the
13622908d778SJames Bottomley  * port_map_by_links in DDB 0.  When a HARD_RESET primitive has been
13632908d778SJames Bottomley  * received, the sequencer disables all phys in that port.
13642908d778SJames Bottomley  * port_map_by_links is also used as the conn_mask byte in the
13652908d778SJames Bottomley  * initiator/target port DDB.
13662908d778SJames Bottomley  */
asd_update_port_links(struct asd_ha_struct * asd_ha,struct asd_phy * phy)13673f048109Smalahal@us.ibm.com void asd_update_port_links(struct asd_ha_struct *asd_ha, struct asd_phy *phy)
13682908d778SJames Bottomley {
13693f048109Smalahal@us.ibm.com 	const u8 phy_mask = (u8) phy->asd_port->phy_mask;
13702908d778SJames Bottomley 	u8  phy_is_up;
13712908d778SJames Bottomley 	u8  mask;
13722908d778SJames Bottomley 	int i, err;
137357ba07dcSDarrick J. Wong 	unsigned long flags;
13742908d778SJames Bottomley 
137557ba07dcSDarrick J. Wong 	spin_lock_irqsave(&asd_ha->hw_prof.ddb_lock, flags);
13762908d778SJames Bottomley 	for_each_phy(phy_mask, mask, i)
13772908d778SJames Bottomley 		asd_ddbsite_write_byte(asd_ha, 0,
13782908d778SJames Bottomley 				       offsetof(struct asd_ddb_seq_shared,
13792908d778SJames Bottomley 						port_map_by_links)+i,phy_mask);
13802908d778SJames Bottomley 
13812908d778SJames Bottomley 	for (i = 0; i < 12; i++) {
13822908d778SJames Bottomley 		phy_is_up = asd_ddbsite_read_byte(asd_ha, 0,
13832908d778SJames Bottomley 			  offsetof(struct asd_ddb_seq_shared, phy_is_up));
13842908d778SJames Bottomley 		err = asd_ddbsite_update_byte(asd_ha, 0,
13852908d778SJames Bottomley 				offsetof(struct asd_ddb_seq_shared, phy_is_up),
13862908d778SJames Bottomley 				phy_is_up,
13872908d778SJames Bottomley 				phy_is_up | phy_mask);
13882908d778SJames Bottomley 		if (!err)
13892908d778SJames Bottomley 			break;
13902908d778SJames Bottomley 		else if (err == -EFAULT) {
13912908d778SJames Bottomley 			asd_printk("phy_is_up: parity error in DDB 0\n");
13922908d778SJames Bottomley 			break;
13932908d778SJames Bottomley 		}
13942908d778SJames Bottomley 	}
139557ba07dcSDarrick J. Wong 	spin_unlock_irqrestore(&asd_ha->hw_prof.ddb_lock, flags);
13962908d778SJames Bottomley 
13972908d778SJames Bottomley 	if (err)
13982908d778SJames Bottomley 		asd_printk("couldn't update DDB 0:error:%d\n", err);
13992908d778SJames Bottomley }
1400bc229b36SJames Bottomley 
1401bc229b36SJames Bottomley MODULE_FIRMWARE(SAS_RAZOR_SEQUENCER_FW_FILE);
1402