static void atp_is()

in atp870u.c [1769:2380]


static void atp_is(struct atp_unit *dev, unsigned char c, bool wide_chip,
		   unsigned char lvdmode)
{
	unsigned char i, j, k, rmb, n;
	unsigned short int m;
	static unsigned char mbuf[512];
	static unsigned char satn[9] = { 0, 0, 0, 0, 0, 0, 0, 6, 6 };
	static unsigned char inqd[9] = { 0x12, 0, 0, 0, 0x24, 0, 0, 0x24, 6 };
	static unsigned char synn[6] = { 0x80, 1, 3, 1, 0x19, 0x0e };
	unsigned char synu[6] = { 0x80, 1, 3, 1, 0x0a, 0x0e };
	static unsigned char synw[6] = { 0x80, 1, 3, 1, 0x19, 0x0e };
	static unsigned char synw_870[6] = { 0x80, 1, 3, 1, 0x0c, 0x07 };
	unsigned char synuw[6] = { 0x80, 1, 3, 1, 0x0a, 0x0e };
	static unsigned char wide[6] = { 0x80, 1, 2, 3, 1, 0 };
	static unsigned char u3[9] = { 0x80, 1, 6, 4, 0x09, 00, 0x0e, 0x01, 0x02 };

	for (i = 0; i < 16; i++) {
		if (!wide_chip && (i > 7))
			break;
		m = 1;
		m = m << i;
		if ((m & dev->active_id[c]) != 0) {
			continue;
		}
		if (i == dev->host_id[c]) {
			printk(KERN_INFO "         ID: %2d  Host Adapter\n", dev->host_id[c]);
			continue;
		}
		atp_writeb_io(dev, c, 0x1b, wide_chip ? 0x01 : 0x00);
		atp_writeb_io(dev, c, 1, 0x08);
		atp_writeb_io(dev, c, 2, 0x7f);
		atp_writeb_io(dev, c, 3, satn[0]);
		atp_writeb_io(dev, c, 4, satn[1]);
		atp_writeb_io(dev, c, 5, satn[2]);
		atp_writeb_io(dev, c, 6, satn[3]);
		atp_writeb_io(dev, c, 7, satn[4]);
		atp_writeb_io(dev, c, 8, satn[5]);
		atp_writeb_io(dev, c, 0x0f, 0);
		atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
		atp_writeb_io(dev, c, 0x12, 0);
		atp_writeb_io(dev, c, 0x13, satn[6]);
		atp_writeb_io(dev, c, 0x14, satn[7]);
		j = i;
		if ((j & 0x08) != 0) {
			j = (j & 0x07) | 0x40;
		}
		atp_writeb_io(dev, c, 0x15, j);
		atp_writeb_io(dev, c, 0x18, satn[8]);

		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
			cpu_relax();

		if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
			continue;

		while (atp_readb_io(dev, c, 0x17) != 0x8e)
			cpu_relax();

		dev->active_id[c] |= m;

		atp_writeb_io(dev, c, 0x10, 0x30);
		if (is885(dev) || is880(dev))
			atp_writeb_io(dev, c, 0x14, 0x00);
		else /* result of is870() merge - is this a bug? */
			atp_writeb_io(dev, c, 0x04, 0x00);

phase_cmd:
		atp_writeb_io(dev, c, 0x18, 0x08);

		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
			cpu_relax();

		j = atp_readb_io(dev, c, 0x17);
		if (j != 0x16) {
			atp_writeb_io(dev, c, 0x10, 0x41);
			goto phase_cmd;
		}
sel_ok:
		atp_writeb_io(dev, c, 3, inqd[0]);
		atp_writeb_io(dev, c, 4, inqd[1]);
		atp_writeb_io(dev, c, 5, inqd[2]);
		atp_writeb_io(dev, c, 6, inqd[3]);
		atp_writeb_io(dev, c, 7, inqd[4]);
		atp_writeb_io(dev, c, 8, inqd[5]);
		atp_writeb_io(dev, c, 0x0f, 0);
		atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
		atp_writeb_io(dev, c, 0x12, 0);
		atp_writeb_io(dev, c, 0x13, inqd[6]);
		atp_writeb_io(dev, c, 0x14, inqd[7]);
		atp_writeb_io(dev, c, 0x18, inqd[8]);

		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
			cpu_relax();

		if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
			continue;

		while (atp_readb_io(dev, c, 0x17) != 0x8e)
			cpu_relax();

		if (wide_chip)
			atp_writeb_io(dev, c, 0x1b, 0x00);

		atp_writeb_io(dev, c, 0x18, 0x08);
		j = 0;
rd_inq_data:
		k = atp_readb_io(dev, c, 0x1f);
		if ((k & 0x01) != 0) {
			mbuf[j++] = atp_readb_io(dev, c, 0x19);
			goto rd_inq_data;
		}
		if ((k & 0x80) == 0) {
			goto rd_inq_data;
		}
		j = atp_readb_io(dev, c, 0x17);
		if (j == 0x16) {
			goto inq_ok;
		}
		atp_writeb_io(dev, c, 0x10, 0x46);
		atp_writeb_io(dev, c, 0x12, 0);
		atp_writeb_io(dev, c, 0x13, 0);
		atp_writeb_io(dev, c, 0x14, 0);
		atp_writeb_io(dev, c, 0x18, 0x08);

		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
			cpu_relax();

		if (atp_readb_io(dev, c, 0x17) != 0x16)
			goto sel_ok;

inq_ok:
		mbuf[36] = 0;
		printk(KERN_INFO "         ID: %2d  %s\n", i, &mbuf[8]);
		dev->id[c][i].devtype = mbuf[0];
		rmb = mbuf[1];
		n = mbuf[7];
		if (!wide_chip)
			goto not_wide;
		if ((mbuf[7] & 0x60) == 0) {
			goto not_wide;
		}
		if (is885(dev) || is880(dev)) {
			if ((i < 8) && ((dev->global_map[c] & 0x20) == 0))
				goto not_wide;
		} else { /* result of is870() merge - is this a bug? */
			if ((dev->global_map[c] & 0x20) == 0)
				goto not_wide;
		}
		if (lvdmode == 0) {
			goto chg_wide;
		}
		if (dev->sp[c][i] != 0x04)	// force u2
		{
			goto chg_wide;
		}

		atp_writeb_io(dev, c, 0x1b, 0x01);
		atp_writeb_io(dev, c, 3, satn[0]);
		atp_writeb_io(dev, c, 4, satn[1]);
		atp_writeb_io(dev, c, 5, satn[2]);
		atp_writeb_io(dev, c, 6, satn[3]);
		atp_writeb_io(dev, c, 7, satn[4]);
		atp_writeb_io(dev, c, 8, satn[5]);
		atp_writeb_io(dev, c, 0x0f, 0);
		atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
		atp_writeb_io(dev, c, 0x12, 0);
		atp_writeb_io(dev, c, 0x13, satn[6]);
		atp_writeb_io(dev, c, 0x14, satn[7]);
		atp_writeb_io(dev, c, 0x18, satn[8]);

		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
			cpu_relax();

		if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
			continue;

		while (atp_readb_io(dev, c, 0x17) != 0x8e)
			cpu_relax();

try_u3:
		j = 0;
		atp_writeb_io(dev, c, 0x14, 0x09);
		atp_writeb_io(dev, c, 0x18, 0x20);

		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
			if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
				atp_writeb_io(dev, c, 0x19, u3[j++]);
			cpu_relax();
		}

		while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
			cpu_relax();

		j = atp_readb_io(dev, c, 0x17) & 0x0f;
		if (j == 0x0f) {
			goto u3p_in;
		}
		if (j == 0x0a) {
			goto u3p_cmd;
		}
		if (j == 0x0e) {
			goto try_u3;
		}
		continue;
u3p_out:
		atp_writeb_io(dev, c, 0x18, 0x20);
		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
			if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
				atp_writeb_io(dev, c, 0x19, 0);
			cpu_relax();
		}
		j = atp_readb_io(dev, c, 0x17) & 0x0f;
		if (j == 0x0f) {
			goto u3p_in;
		}
		if (j == 0x0a) {
			goto u3p_cmd;
		}
		if (j == 0x0e) {
			goto u3p_out;
		}
		continue;
u3p_in:
		atp_writeb_io(dev, c, 0x14, 0x09);
		atp_writeb_io(dev, c, 0x18, 0x20);
		k = 0;
u3p_in1:
		j = atp_readb_io(dev, c, 0x1f);
		if ((j & 0x01) != 0) {
			mbuf[k++] = atp_readb_io(dev, c, 0x19);
			goto u3p_in1;
		}
		if ((j & 0x80) == 0x00) {
			goto u3p_in1;
		}
		j = atp_readb_io(dev, c, 0x17) & 0x0f;
		if (j == 0x0f) {
			goto u3p_in;
		}
		if (j == 0x0a) {
			goto u3p_cmd;
		}
		if (j == 0x0e) {
			goto u3p_out;
		}
		continue;
u3p_cmd:
		atp_writeb_io(dev, c, 0x10, 0x30);
		atp_writeb_io(dev, c, 0x14, 0x00);
		atp_writeb_io(dev, c, 0x18, 0x08);

		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00);

		j = atp_readb_io(dev, c, 0x17);
		if (j != 0x16) {
			if (j == 0x4e) {
				goto u3p_out;
			}
			continue;
		}
		if (mbuf[0] != 0x01) {
			goto chg_wide;
		}
		if (mbuf[1] != 0x06) {
			goto chg_wide;
		}
		if (mbuf[2] != 0x04) {
			goto chg_wide;
		}
		if (mbuf[3] == 0x09) {
			m = 1;
			m = m << i;
			dev->wide_id[c] |= m;
			dev->id[c][i].devsp = 0xce;
#ifdef ED_DBGP
			printk("dev->id[%2d][%2d].devsp = %2x\n",
			       c, i, dev->id[c][i].devsp);
#endif
			continue;
		}
chg_wide:
		atp_writeb_io(dev, c, 0x1b, 0x01);
		atp_writeb_io(dev, c, 3, satn[0]);
		atp_writeb_io(dev, c, 4, satn[1]);
		atp_writeb_io(dev, c, 5, satn[2]);
		atp_writeb_io(dev, c, 6, satn[3]);
		atp_writeb_io(dev, c, 7, satn[4]);
		atp_writeb_io(dev, c, 8, satn[5]);
		atp_writeb_io(dev, c, 0x0f, 0);
		atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
		atp_writeb_io(dev, c, 0x12, 0);
		atp_writeb_io(dev, c, 0x13, satn[6]);
		atp_writeb_io(dev, c, 0x14, satn[7]);
		atp_writeb_io(dev, c, 0x18, satn[8]);

		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
			cpu_relax();

		if (atp_readb_io(dev, c, 0x17) != 0x11 &&
		    atp_readb_io(dev, c, 0x17) != 0x8e)
			continue;

		while (atp_readb_io(dev, c, 0x17) != 0x8e)
			cpu_relax();

try_wide:
		j = 0;
		atp_writeb_io(dev, c, 0x14, 0x05);
		atp_writeb_io(dev, c, 0x18, 0x20);

		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
			if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
				atp_writeb_io(dev, c, 0x19, wide[j++]);
			cpu_relax();
		}

		while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
			cpu_relax();

		j = atp_readb_io(dev, c, 0x17) & 0x0f;
		if (j == 0x0f) {
			goto widep_in;
		}
		if (j == 0x0a) {
			goto widep_cmd;
		}
		if (j == 0x0e) {
			goto try_wide;
		}
		continue;
widep_out:
		atp_writeb_io(dev, c, 0x18, 0x20);
		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
			if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
				atp_writeb_io(dev, c, 0x19, 0);
			cpu_relax();
		}
		j = atp_readb_io(dev, c, 0x17) & 0x0f;
		if (j == 0x0f) {
			goto widep_in;
		}
		if (j == 0x0a) {
			goto widep_cmd;
		}
		if (j == 0x0e) {
			goto widep_out;
		}
		continue;
widep_in:
		atp_writeb_io(dev, c, 0x14, 0xff);
		atp_writeb_io(dev, c, 0x18, 0x20);
		k = 0;
widep_in1:
		j = atp_readb_io(dev, c, 0x1f);
		if ((j & 0x01) != 0) {
			mbuf[k++] = atp_readb_io(dev, c, 0x19);
			goto widep_in1;
		}
		if ((j & 0x80) == 0x00) {
			goto widep_in1;
		}
		j = atp_readb_io(dev, c, 0x17) & 0x0f;
		if (j == 0x0f) {
			goto widep_in;
		}
		if (j == 0x0a) {
			goto widep_cmd;
		}
		if (j == 0x0e) {
			goto widep_out;
		}
		continue;
widep_cmd:
		atp_writeb_io(dev, c, 0x10, 0x30);
		atp_writeb_io(dev, c, 0x14, 0x00);
		atp_writeb_io(dev, c, 0x18, 0x08);

		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
			cpu_relax();

		j = atp_readb_io(dev, c, 0x17);
		if (j != 0x16) {
			if (j == 0x4e) {
				goto widep_out;
			}
			continue;
		}
		if (mbuf[0] != 0x01) {
			goto not_wide;
		}
		if (mbuf[1] != 0x02) {
			goto not_wide;
		}
		if (mbuf[2] != 0x03) {
			goto not_wide;
		}
		if (mbuf[3] != 0x01) {
			goto not_wide;
		}
		m = 1;
		m = m << i;
		dev->wide_id[c] |= m;
not_wide:
		if ((dev->id[c][i].devtype == 0x00) ||
		    (dev->id[c][i].devtype == 0x07) ||
		    ((dev->id[c][i].devtype == 0x05) && ((n & 0x10) != 0))) {
			m = 1;
			m = m << i;
			if ((dev->async[c] & m) != 0) {
				goto set_sync;
			}
		}
		continue;
set_sync:
		if ((!is885(dev) && !is880(dev)) || (dev->sp[c][i] == 0x02)) {
			synu[4] = 0x0c;
			synuw[4] = 0x0c;
		} else {
			if (dev->sp[c][i] >= 0x03) {
				synu[4] = 0x0a;
				synuw[4] = 0x0a;
			}
		}
		j = 0;
		if ((m & dev->wide_id[c]) != 0) {
			j |= 0x01;
		}
		atp_writeb_io(dev, c, 0x1b, j);
		atp_writeb_io(dev, c, 3, satn[0]);
		atp_writeb_io(dev, c, 4, satn[1]);
		atp_writeb_io(dev, c, 5, satn[2]);
		atp_writeb_io(dev, c, 6, satn[3]);
		atp_writeb_io(dev, c, 7, satn[4]);
		atp_writeb_io(dev, c, 8, satn[5]);
		atp_writeb_io(dev, c, 0x0f, 0);
		atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
		atp_writeb_io(dev, c, 0x12, 0);
		atp_writeb_io(dev, c, 0x13, satn[6]);
		atp_writeb_io(dev, c, 0x14, satn[7]);
		atp_writeb_io(dev, c, 0x18, satn[8]);

		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
			cpu_relax();

		if (atp_readb_io(dev, c, 0x17) != 0x11 &&
		    atp_readb_io(dev, c, 0x17) != 0x8e)
			continue;

		while (atp_readb_io(dev, c, 0x17) != 0x8e)
			cpu_relax();

try_sync:
		j = 0;
		atp_writeb_io(dev, c, 0x14, 0x06);
		atp_writeb_io(dev, c, 0x18, 0x20);

		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
			if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0) {
				if ((m & dev->wide_id[c]) != 0) {
					if (is885(dev) || is880(dev)) {
						if ((m & dev->ultra_map[c]) != 0) {
							atp_writeb_io(dev, c, 0x19, synuw[j++]);
						} else {
							atp_writeb_io(dev, c, 0x19, synw[j++]);
						}
					} else
						atp_writeb_io(dev, c, 0x19, synw_870[j++]);
				} else {
					if ((m & dev->ultra_map[c]) != 0) {
						atp_writeb_io(dev, c, 0x19, synu[j++]);
					} else {
						atp_writeb_io(dev, c, 0x19, synn[j++]);
					}
				}
			}
		}

		while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
			cpu_relax();

		j = atp_readb_io(dev, c, 0x17) & 0x0f;
		if (j == 0x0f) {
			goto phase_ins;
		}
		if (j == 0x0a) {
			goto phase_cmds;
		}
		if (j == 0x0e) {
			goto try_sync;
		}
		continue;
phase_outs:
		atp_writeb_io(dev, c, 0x18, 0x20);
		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00) {
			if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0x00)
				atp_writeb_io(dev, c, 0x19, 0x00);
			cpu_relax();
		}
		j = atp_readb_io(dev, c, 0x17);
		if (j == 0x85) {
			goto tar_dcons;
		}
		j &= 0x0f;
		if (j == 0x0f) {
			goto phase_ins;
		}
		if (j == 0x0a) {
			goto phase_cmds;
		}
		if (j == 0x0e) {
			goto phase_outs;
		}
		continue;
phase_ins:
		if (is885(dev) || is880(dev))
			atp_writeb_io(dev, c, 0x14, 0x06);
		else
			atp_writeb_io(dev, c, 0x14, 0xff);
		atp_writeb_io(dev, c, 0x18, 0x20);
		k = 0;
phase_ins1:
		j = atp_readb_io(dev, c, 0x1f);
		if ((j & 0x01) != 0x00) {
			mbuf[k++] = atp_readb_io(dev, c, 0x19);
			goto phase_ins1;
		}
		if ((j & 0x80) == 0x00) {
			goto phase_ins1;
		}

		while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00);

		j = atp_readb_io(dev, c, 0x17);
		if (j == 0x85) {
			goto tar_dcons;
		}
		j &= 0x0f;
		if (j == 0x0f) {
			goto phase_ins;
		}
		if (j == 0x0a) {
			goto phase_cmds;
		}
		if (j == 0x0e) {
			goto phase_outs;
		}
		continue;
phase_cmds:
		atp_writeb_io(dev, c, 0x10, 0x30);
tar_dcons:
		atp_writeb_io(dev, c, 0x14, 0x00);
		atp_writeb_io(dev, c, 0x18, 0x08);

		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
			cpu_relax();

		j = atp_readb_io(dev, c, 0x17);
		if (j != 0x16) {
			continue;
		}
		if (mbuf[0] != 0x01) {
			continue;
		}
		if (mbuf[1] != 0x03) {
			continue;
		}
		if (mbuf[4] == 0x00) {
			continue;
		}
		if (mbuf[3] > 0x64) {
			continue;
		}
		if (is885(dev) || is880(dev)) {
			if (mbuf[4] > 0x0e) {
				mbuf[4] = 0x0e;
			}
		} else {
			if (mbuf[4] > 0x0c) {
				mbuf[4] = 0x0c;
			}
		}
		dev->id[c][i].devsp = mbuf[4];
		if (is885(dev) || is880(dev))
			if (mbuf[3] < 0x0c) {
				j = 0xb0;
				goto set_syn_ok;
			}
		if ((mbuf[3] < 0x0d) && (rmb == 0)) {
			j = 0xa0;
			goto set_syn_ok;
		}
		if (mbuf[3] < 0x1a) {
			j = 0x20;
			goto set_syn_ok;
		}
		if (mbuf[3] < 0x33) {
			j = 0x40;
			goto set_syn_ok;
		}
		if (mbuf[3] < 0x4c) {
			j = 0x50;
			goto set_syn_ok;
		}
		j = 0x60;
set_syn_ok:
		dev->id[c][i].devsp = (dev->id[c][i].devsp & 0x0f) | j;
#ifdef ED_DBGP
		printk("dev->id[%2d][%2d].devsp = %2x\n",
		       c,i,dev->id[c][i].devsp);
#endif
	}
}