atp870u.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318
  1. /*
  2. * Copyright (C) 1997 Wu Ching Chen
  3. * 2.1.x update (C) 1998 Krzysztof G. Baranowski
  4. * 2.5.x update (C) 2002 Red Hat
  5. * 2.6.x update (C) 2004 Red Hat
  6. *
  7. * Marcelo Tosatti <marcelo@conectiva.com.br> : SMP fixes
  8. *
  9. * Wu Ching Chen : NULL pointer fixes 2000/06/02
  10. * support atp876 chip
  11. * enable 32 bit fifo transfer
  12. * support cdrom & remove device run ultra speed
  13. * fix disconnect bug 2000/12/21
  14. * support atp880 chip lvd u160 2001/05/15
  15. * fix prd table bug 2001/09/12 (7.1)
  16. *
  17. * atp885 support add by ACARD Hao Ping Lian 2005/01/05
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/kernel.h>
  23. #include <linux/types.h>
  24. #include <linux/string.h>
  25. #include <linux/ioport.h>
  26. #include <linux/delay.h>
  27. #include <linux/proc_fs.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/pci.h>
  30. #include <linux/blkdev.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/slab.h>
  33. #include <asm/io.h>
  34. #include <scsi/scsi.h>
  35. #include <scsi/scsi_cmnd.h>
  36. #include <scsi/scsi_device.h>
  37. #include <scsi/scsi_host.h>
  38. #include "atp870u.h"
  39. static struct scsi_host_template atp870u_template;
  40. static void send_s870(struct atp_unit *dev,unsigned char c);
  41. static void atp_is(struct atp_unit *dev, unsigned char c, bool wide_chip, unsigned char lvdmode);
  42. static inline void atp_writeb_base(struct atp_unit *atp, u8 reg, u8 val)
  43. {
  44. outb(val, atp->baseport + reg);
  45. }
  46. static inline void atp_writew_base(struct atp_unit *atp, u8 reg, u16 val)
  47. {
  48. outw(val, atp->baseport + reg);
  49. }
  50. static inline void atp_writeb_io(struct atp_unit *atp, u8 channel, u8 reg, u8 val)
  51. {
  52. outb(val, atp->ioport[channel] + reg);
  53. }
  54. static inline void atp_writew_io(struct atp_unit *atp, u8 channel, u8 reg, u16 val)
  55. {
  56. outw(val, atp->ioport[channel] + reg);
  57. }
  58. static inline void atp_writeb_pci(struct atp_unit *atp, u8 channel, u8 reg, u8 val)
  59. {
  60. outb(val, atp->pciport[channel] + reg);
  61. }
  62. static inline void atp_writel_pci(struct atp_unit *atp, u8 channel, u8 reg, u32 val)
  63. {
  64. outl(val, atp->pciport[channel] + reg);
  65. }
  66. static inline u8 atp_readb_base(struct atp_unit *atp, u8 reg)
  67. {
  68. return inb(atp->baseport + reg);
  69. }
  70. static inline u16 atp_readw_base(struct atp_unit *atp, u8 reg)
  71. {
  72. return inw(atp->baseport + reg);
  73. }
  74. static inline u32 atp_readl_base(struct atp_unit *atp, u8 reg)
  75. {
  76. return inl(atp->baseport + reg);
  77. }
  78. static inline u8 atp_readb_io(struct atp_unit *atp, u8 channel, u8 reg)
  79. {
  80. return inb(atp->ioport[channel] + reg);
  81. }
  82. static inline u16 atp_readw_io(struct atp_unit *atp, u8 channel, u8 reg)
  83. {
  84. return inw(atp->ioport[channel] + reg);
  85. }
  86. static inline u8 atp_readb_pci(struct atp_unit *atp, u8 channel, u8 reg)
  87. {
  88. return inb(atp->pciport[channel] + reg);
  89. }
  90. static inline bool is880(struct atp_unit *atp)
  91. {
  92. return atp->pdev->device == ATP880_DEVID1 ||
  93. atp->pdev->device == ATP880_DEVID2;
  94. }
  95. static inline bool is885(struct atp_unit *atp)
  96. {
  97. return atp->pdev->device == ATP885_DEVID;
  98. }
  99. static irqreturn_t atp870u_intr_handle(int irq, void *dev_id)
  100. {
  101. unsigned long flags;
  102. unsigned short int id;
  103. unsigned char i, j, c, target_id, lun,cmdp;
  104. unsigned char *prd;
  105. struct scsi_cmnd *workreq;
  106. unsigned long adrcnt, k;
  107. #ifdef ED_DBGP
  108. unsigned long l;
  109. #endif
  110. struct Scsi_Host *host = dev_id;
  111. struct atp_unit *dev = (struct atp_unit *)&host->hostdata;
  112. for (c = 0; c < 2; c++) {
  113. j = atp_readb_io(dev, c, 0x1f);
  114. if ((j & 0x80) != 0)
  115. break;
  116. dev->in_int[c] = 0;
  117. }
  118. if ((j & 0x80) == 0)
  119. return IRQ_NONE;
  120. #ifdef ED_DBGP
  121. printk("atp870u_intr_handle enter\n");
  122. #endif
  123. dev->in_int[c] = 1;
  124. cmdp = atp_readb_io(dev, c, 0x10);
  125. if (dev->working[c] != 0) {
  126. if (is885(dev)) {
  127. if ((atp_readb_io(dev, c, 0x16) & 0x80) == 0)
  128. atp_writeb_io(dev, c, 0x16, (atp_readb_io(dev, c, 0x16) | 0x80));
  129. }
  130. if ((atp_readb_pci(dev, c, 0x00) & 0x08) != 0)
  131. {
  132. for (k=0; k < 1000; k++) {
  133. if ((atp_readb_pci(dev, c, 2) & 0x08) == 0)
  134. break;
  135. if ((atp_readb_pci(dev, c, 2) & 0x01) == 0)
  136. break;
  137. }
  138. }
  139. atp_writeb_pci(dev, c, 0, 0x00);
  140. i = atp_readb_io(dev, c, 0x17);
  141. if (is885(dev))
  142. atp_writeb_pci(dev, c, 2, 0x06);
  143. target_id = atp_readb_io(dev, c, 0x15);
  144. /*
  145. * Remap wide devices onto id numbers
  146. */
  147. if ((target_id & 0x40) != 0) {
  148. target_id = (target_id & 0x07) | 0x08;
  149. } else {
  150. target_id &= 0x07;
  151. }
  152. if ((j & 0x40) != 0) {
  153. if (dev->last_cmd[c] == 0xff) {
  154. dev->last_cmd[c] = target_id;
  155. }
  156. dev->last_cmd[c] |= 0x40;
  157. }
  158. if (is885(dev))
  159. dev->r1f[c][target_id] |= j;
  160. #ifdef ED_DBGP
  161. printk("atp870u_intr_handle status = %x\n",i);
  162. #endif
  163. if (i == 0x85) {
  164. if ((dev->last_cmd[c] & 0xf0) != 0x40) {
  165. dev->last_cmd[c] = 0xff;
  166. }
  167. if (is885(dev)) {
  168. adrcnt = 0;
  169. ((unsigned char *) &adrcnt)[2] = atp_readb_io(dev, c, 0x12);
  170. ((unsigned char *) &adrcnt)[1] = atp_readb_io(dev, c, 0x13);
  171. ((unsigned char *) &adrcnt)[0] = atp_readb_io(dev, c, 0x14);
  172. if (dev->id[c][target_id].last_len != adrcnt)
  173. {
  174. k = dev->id[c][target_id].last_len;
  175. k -= adrcnt;
  176. dev->id[c][target_id].tran_len = k;
  177. dev->id[c][target_id].last_len = adrcnt;
  178. }
  179. #ifdef ED_DBGP
  180. printk("dev->id[c][target_id].last_len = %d dev->id[c][target_id].tran_len = %d\n",dev->id[c][target_id].last_len,dev->id[c][target_id].tran_len);
  181. #endif
  182. }
  183. /*
  184. * Flip wide
  185. */
  186. if (dev->wide_id[c] != 0) {
  187. atp_writeb_io(dev, c, 0x1b, 0x01);
  188. while ((atp_readb_io(dev, c, 0x1b) & 0x01) != 0x01)
  189. atp_writeb_io(dev, c, 0x1b, 0x01);
  190. }
  191. /*
  192. * Issue more commands
  193. */
  194. spin_lock_irqsave(dev->host->host_lock, flags);
  195. if (((dev->quhd[c] != dev->quend[c]) || (dev->last_cmd[c] != 0xff)) &&
  196. (dev->in_snd[c] == 0)) {
  197. #ifdef ED_DBGP
  198. printk("Call sent_s870\n");
  199. #endif
  200. send_s870(dev,c);
  201. }
  202. spin_unlock_irqrestore(dev->host->host_lock, flags);
  203. /*
  204. * Done
  205. */
  206. dev->in_int[c] = 0;
  207. #ifdef ED_DBGP
  208. printk("Status 0x85 return\n");
  209. #endif
  210. return IRQ_HANDLED;
  211. }
  212. if (i == 0x40) {
  213. dev->last_cmd[c] |= 0x40;
  214. dev->in_int[c] = 0;
  215. return IRQ_HANDLED;
  216. }
  217. if (i == 0x21) {
  218. if ((dev->last_cmd[c] & 0xf0) != 0x40) {
  219. dev->last_cmd[c] = 0xff;
  220. }
  221. adrcnt = 0;
  222. ((unsigned char *) &adrcnt)[2] = atp_readb_io(dev, c, 0x12);
  223. ((unsigned char *) &adrcnt)[1] = atp_readb_io(dev, c, 0x13);
  224. ((unsigned char *) &adrcnt)[0] = atp_readb_io(dev, c, 0x14);
  225. k = dev->id[c][target_id].last_len;
  226. k -= adrcnt;
  227. dev->id[c][target_id].tran_len = k;
  228. dev->id[c][target_id].last_len = adrcnt;
  229. atp_writeb_io(dev, c, 0x10, 0x41);
  230. atp_writeb_io(dev, c, 0x18, 0x08);
  231. dev->in_int[c] = 0;
  232. return IRQ_HANDLED;
  233. }
  234. if (is885(dev)) {
  235. if ((i == 0x4c) || (i == 0x4d) || (i == 0x8c) || (i == 0x8d)) {
  236. if ((i == 0x4c) || (i == 0x8c))
  237. i=0x48;
  238. else
  239. i=0x49;
  240. }
  241. }
  242. if ((i == 0x80) || (i == 0x8f)) {
  243. #ifdef ED_DBGP
  244. printk(KERN_DEBUG "Device reselect\n");
  245. #endif
  246. lun = 0;
  247. if (cmdp == 0x44 || i == 0x80)
  248. lun = atp_readb_io(dev, c, 0x1d) & 0x07;
  249. else {
  250. if ((dev->last_cmd[c] & 0xf0) != 0x40) {
  251. dev->last_cmd[c] = 0xff;
  252. }
  253. if (cmdp == 0x41) {
  254. #ifdef ED_DBGP
  255. printk("cmdp = 0x41\n");
  256. #endif
  257. adrcnt = 0;
  258. ((unsigned char *) &adrcnt)[2] = atp_readb_io(dev, c, 0x12);
  259. ((unsigned char *) &adrcnt)[1] = atp_readb_io(dev, c, 0x13);
  260. ((unsigned char *) &adrcnt)[0] = atp_readb_io(dev, c, 0x14);
  261. k = dev->id[c][target_id].last_len;
  262. k -= adrcnt;
  263. dev->id[c][target_id].tran_len = k;
  264. dev->id[c][target_id].last_len = adrcnt;
  265. atp_writeb_io(dev, c, 0x18, 0x08);
  266. dev->in_int[c] = 0;
  267. return IRQ_HANDLED;
  268. } else {
  269. #ifdef ED_DBGP
  270. printk("cmdp != 0x41\n");
  271. #endif
  272. atp_writeb_io(dev, c, 0x10, 0x46);
  273. dev->id[c][target_id].dirct = 0x00;
  274. atp_writeb_io(dev, c, 0x12, 0x00);
  275. atp_writeb_io(dev, c, 0x13, 0x00);
  276. atp_writeb_io(dev, c, 0x14, 0x00);
  277. atp_writeb_io(dev, c, 0x18, 0x08);
  278. dev->in_int[c] = 0;
  279. return IRQ_HANDLED;
  280. }
  281. }
  282. if (dev->last_cmd[c] != 0xff) {
  283. dev->last_cmd[c] |= 0x40;
  284. }
  285. if (is885(dev)) {
  286. j = atp_readb_base(dev, 0x29) & 0xfe;
  287. atp_writeb_base(dev, 0x29, j);
  288. } else
  289. atp_writeb_io(dev, c, 0x10, 0x45);
  290. target_id = atp_readb_io(dev, c, 0x16);
  291. /*
  292. * Remap wide identifiers
  293. */
  294. if ((target_id & 0x10) != 0) {
  295. target_id = (target_id & 0x07) | 0x08;
  296. } else {
  297. target_id &= 0x07;
  298. }
  299. if (is885(dev))
  300. atp_writeb_io(dev, c, 0x10, 0x45);
  301. workreq = dev->id[c][target_id].curr_req;
  302. #ifdef ED_DBGP
  303. scmd_printk(KERN_DEBUG, workreq, "CDB");
  304. for (l = 0; l < workreq->cmd_len; l++)
  305. printk(KERN_DEBUG " %x",workreq->cmnd[l]);
  306. printk("\n");
  307. #endif
  308. atp_writeb_io(dev, c, 0x0f, lun);
  309. atp_writeb_io(dev, c, 0x11, dev->id[c][target_id].devsp);
  310. adrcnt = dev->id[c][target_id].tran_len;
  311. k = dev->id[c][target_id].last_len;
  312. atp_writeb_io(dev, c, 0x12, ((unsigned char *) &k)[2]);
  313. atp_writeb_io(dev, c, 0x13, ((unsigned char *) &k)[1]);
  314. atp_writeb_io(dev, c, 0x14, ((unsigned char *) &k)[0]);
  315. #ifdef ED_DBGP
  316. printk("k %x, k[0] 0x%x k[1] 0x%x k[2] 0x%x\n", k, atp_readb_io(dev, c, 0x14), atp_readb_io(dev, c, 0x13), atp_readb_io(dev, c, 0x12));
  317. #endif
  318. /* Remap wide */
  319. j = target_id;
  320. if (target_id > 7) {
  321. j = (j & 0x07) | 0x40;
  322. }
  323. /* Add direction */
  324. j |= dev->id[c][target_id].dirct;
  325. atp_writeb_io(dev, c, 0x15, j);
  326. atp_writeb_io(dev, c, 0x16, 0x80);
  327. /* enable 32 bit fifo transfer */
  328. if (is885(dev)) {
  329. i = atp_readb_pci(dev, c, 1) & 0xf3;
  330. //j=workreq->cmnd[0];
  331. if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
  332. i |= 0x0c;
  333. }
  334. atp_writeb_pci(dev, c, 1, i);
  335. } else if (is880(dev)) {
  336. if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a))
  337. atp_writeb_base(dev, 0x3b, (atp_readb_base(dev, 0x3b) & 0x3f) | 0xc0);
  338. else
  339. atp_writeb_base(dev, 0x3b, atp_readb_base(dev, 0x3b) & 0x3f);
  340. } else {
  341. if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a))
  342. atp_writeb_base(dev, 0x3a, (atp_readb_base(dev, 0x3a) & 0xf3) | 0x08);
  343. else
  344. atp_writeb_base(dev, 0x3a, atp_readb_base(dev, 0x3a) & 0xf3);
  345. }
  346. j = 0;
  347. id = 1;
  348. id = id << target_id;
  349. /*
  350. * Is this a wide device
  351. */
  352. if ((id & dev->wide_id[c]) != 0) {
  353. j |= 0x01;
  354. }
  355. atp_writeb_io(dev, c, 0x1b, j);
  356. while ((atp_readb_io(dev, c, 0x1b) & 0x01) != j)
  357. atp_writeb_io(dev, c, 0x1b, j);
  358. if (dev->id[c][target_id].last_len == 0) {
  359. atp_writeb_io(dev, c, 0x18, 0x08);
  360. dev->in_int[c] = 0;
  361. #ifdef ED_DBGP
  362. printk("dev->id[c][target_id].last_len = 0\n");
  363. #endif
  364. return IRQ_HANDLED;
  365. }
  366. #ifdef ED_DBGP
  367. printk("target_id = %d adrcnt = %d\n",target_id,adrcnt);
  368. #endif
  369. prd = dev->id[c][target_id].prd_pos;
  370. while (adrcnt != 0) {
  371. id = ((unsigned short int *)prd)[2];
  372. if (id == 0) {
  373. k = 0x10000;
  374. } else {
  375. k = id;
  376. }
  377. if (k > adrcnt) {
  378. ((unsigned short int *)prd)[2] = (unsigned short int)
  379. (k - adrcnt);
  380. ((unsigned long *)prd)[0] += adrcnt;
  381. adrcnt = 0;
  382. dev->id[c][target_id].prd_pos = prd;
  383. } else {
  384. adrcnt -= k;
  385. dev->id[c][target_id].prdaddr += 0x08;
  386. prd += 0x08;
  387. if (adrcnt == 0) {
  388. dev->id[c][target_id].prd_pos = prd;
  389. }
  390. }
  391. }
  392. atp_writel_pci(dev, c, 0x04, dev->id[c][target_id].prdaddr);
  393. #ifdef ED_DBGP
  394. printk("dev->id[%d][%d].prdaddr 0x%8x\n", c, target_id, dev->id[c][target_id].prdaddr);
  395. #endif
  396. if (!is885(dev)) {
  397. atp_writeb_pci(dev, c, 2, 0x06);
  398. atp_writeb_pci(dev, c, 2, 0x00);
  399. }
  400. /*
  401. * Check transfer direction
  402. */
  403. if (dev->id[c][target_id].dirct != 0) {
  404. atp_writeb_io(dev, c, 0x18, 0x08);
  405. atp_writeb_pci(dev, c, 0, 0x01);
  406. dev->in_int[c] = 0;
  407. #ifdef ED_DBGP
  408. printk("status 0x80 return dirct != 0\n");
  409. #endif
  410. return IRQ_HANDLED;
  411. }
  412. atp_writeb_io(dev, c, 0x18, 0x08);
  413. atp_writeb_pci(dev, c, 0, 0x09);
  414. dev->in_int[c] = 0;
  415. #ifdef ED_DBGP
  416. printk("status 0x80 return dirct = 0\n");
  417. #endif
  418. return IRQ_HANDLED;
  419. }
  420. /*
  421. * Current scsi request on this target
  422. */
  423. workreq = dev->id[c][target_id].curr_req;
  424. if (i == 0x42 || i == 0x16) {
  425. if ((dev->last_cmd[c] & 0xf0) != 0x40) {
  426. dev->last_cmd[c] = 0xff;
  427. }
  428. if (i == 0x16) {
  429. workreq->result = atp_readb_io(dev, c, 0x0f);
  430. if (((dev->r1f[c][target_id] & 0x10) != 0) && is885(dev)) {
  431. printk(KERN_WARNING "AEC67162 CRC ERROR !\n");
  432. workreq->result = 0x02;
  433. }
  434. } else
  435. workreq->result = 0x02;
  436. if (is885(dev)) {
  437. j = atp_readb_base(dev, 0x29) | 0x01;
  438. atp_writeb_base(dev, 0x29, j);
  439. }
  440. /*
  441. * Complete the command
  442. */
  443. scsi_dma_unmap(workreq);
  444. spin_lock_irqsave(dev->host->host_lock, flags);
  445. (*workreq->scsi_done) (workreq);
  446. #ifdef ED_DBGP
  447. printk("workreq->scsi_done\n");
  448. #endif
  449. /*
  450. * Clear it off the queue
  451. */
  452. dev->id[c][target_id].curr_req = NULL;
  453. dev->working[c]--;
  454. spin_unlock_irqrestore(dev->host->host_lock, flags);
  455. /*
  456. * Take it back wide
  457. */
  458. if (dev->wide_id[c] != 0) {
  459. atp_writeb_io(dev, c, 0x1b, 0x01);
  460. while ((atp_readb_io(dev, c, 0x1b) & 0x01) != 0x01)
  461. atp_writeb_io(dev, c, 0x1b, 0x01);
  462. }
  463. /*
  464. * If there is stuff to send and nothing going then send it
  465. */
  466. spin_lock_irqsave(dev->host->host_lock, flags);
  467. if (((dev->last_cmd[c] != 0xff) || (dev->quhd[c] != dev->quend[c])) &&
  468. (dev->in_snd[c] == 0)) {
  469. #ifdef ED_DBGP
  470. printk("Call sent_s870(scsi_done)\n");
  471. #endif
  472. send_s870(dev,c);
  473. }
  474. spin_unlock_irqrestore(dev->host->host_lock, flags);
  475. dev->in_int[c] = 0;
  476. return IRQ_HANDLED;
  477. }
  478. if ((dev->last_cmd[c] & 0xf0) != 0x40) {
  479. dev->last_cmd[c] = 0xff;
  480. }
  481. if (i == 0x4f) {
  482. i = 0x89;
  483. }
  484. i &= 0x0f;
  485. if (i == 0x09) {
  486. atp_writel_pci(dev, c, 4, dev->id[c][target_id].prdaddr);
  487. atp_writeb_pci(dev, c, 2, 0x06);
  488. atp_writeb_pci(dev, c, 2, 0x00);
  489. atp_writeb_io(dev, c, 0x10, 0x41);
  490. if (is885(dev)) {
  491. k = dev->id[c][target_id].last_len;
  492. atp_writeb_io(dev, c, 0x12, ((unsigned char *) (&k))[2]);
  493. atp_writeb_io(dev, c, 0x13, ((unsigned char *) (&k))[1]);
  494. atp_writeb_io(dev, c, 0x14, ((unsigned char *) (&k))[0]);
  495. dev->id[c][target_id].dirct = 0x00;
  496. } else {
  497. dev->id[c][target_id].dirct = 0x00;
  498. }
  499. atp_writeb_io(dev, c, 0x18, 0x08);
  500. atp_writeb_pci(dev, c, 0, 0x09);
  501. dev->in_int[c] = 0;
  502. return IRQ_HANDLED;
  503. }
  504. if (i == 0x08) {
  505. atp_writel_pci(dev, c, 4, dev->id[c][target_id].prdaddr);
  506. atp_writeb_pci(dev, c, 2, 0x06);
  507. atp_writeb_pci(dev, c, 2, 0x00);
  508. atp_writeb_io(dev, c, 0x10, 0x41);
  509. if (is885(dev)) {
  510. k = dev->id[c][target_id].last_len;
  511. atp_writeb_io(dev, c, 0x12, ((unsigned char *) (&k))[2]);
  512. atp_writeb_io(dev, c, 0x13, ((unsigned char *) (&k))[1]);
  513. atp_writeb_io(dev, c, 0x14, ((unsigned char *) (&k))[0]);
  514. }
  515. atp_writeb_io(dev, c, 0x15, atp_readb_io(dev, c, 0x15) | 0x20);
  516. dev->id[c][target_id].dirct = 0x20;
  517. atp_writeb_io(dev, c, 0x18, 0x08);
  518. atp_writeb_pci(dev, c, 0, 0x01);
  519. dev->in_int[c] = 0;
  520. return IRQ_HANDLED;
  521. }
  522. if (i == 0x0a)
  523. atp_writeb_io(dev, c, 0x10, 0x30);
  524. else
  525. atp_writeb_io(dev, c, 0x10, 0x46);
  526. dev->id[c][target_id].dirct = 0x00;
  527. atp_writeb_io(dev, c, 0x12, 0x00);
  528. atp_writeb_io(dev, c, 0x13, 0x00);
  529. atp_writeb_io(dev, c, 0x14, 0x00);
  530. atp_writeb_io(dev, c, 0x18, 0x08);
  531. }
  532. dev->in_int[c] = 0;
  533. return IRQ_HANDLED;
  534. }
  535. /**
  536. * atp870u_queuecommand - Queue SCSI command
  537. * @req_p: request block
  538. * @done: completion function
  539. *
  540. * Queue a command to the ATP queue. Called with the host lock held.
  541. */
  542. static int atp870u_queuecommand_lck(struct scsi_cmnd *req_p,
  543. void (*done) (struct scsi_cmnd *))
  544. {
  545. unsigned char c;
  546. unsigned int m;
  547. struct atp_unit *dev;
  548. struct Scsi_Host *host;
  549. c = scmd_channel(req_p);
  550. req_p->sense_buffer[0]=0;
  551. scsi_set_resid(req_p, 0);
  552. if (scmd_channel(req_p) > 1) {
  553. req_p->result = 0x00040000;
  554. done(req_p);
  555. #ifdef ED_DBGP
  556. printk("atp870u_queuecommand : req_p->device->channel > 1\n");
  557. #endif
  558. return 0;
  559. }
  560. host = req_p->device->host;
  561. dev = (struct atp_unit *)&host->hostdata;
  562. m = 1;
  563. m = m << scmd_id(req_p);
  564. /*
  565. * Fake a timeout for missing targets
  566. */
  567. if ((m & dev->active_id[c]) == 0) {
  568. req_p->result = 0x00040000;
  569. done(req_p);
  570. return 0;
  571. }
  572. if (done) {
  573. req_p->scsi_done = done;
  574. } else {
  575. #ifdef ED_DBGP
  576. printk( "atp870u_queuecommand: done can't be NULL\n");
  577. #endif
  578. req_p->result = 0;
  579. done(req_p);
  580. return 0;
  581. }
  582. /*
  583. * Count new command
  584. */
  585. dev->quend[c]++;
  586. if (dev->quend[c] >= qcnt) {
  587. dev->quend[c] = 0;
  588. }
  589. /*
  590. * Check queue state
  591. */
  592. if (dev->quhd[c] == dev->quend[c]) {
  593. if (dev->quend[c] == 0) {
  594. dev->quend[c] = qcnt;
  595. }
  596. #ifdef ED_DBGP
  597. printk("atp870u_queuecommand : dev->quhd[c] == dev->quend[c]\n");
  598. #endif
  599. dev->quend[c]--;
  600. req_p->result = 0x00020000;
  601. done(req_p);
  602. return 0;
  603. }
  604. dev->quereq[c][dev->quend[c]] = req_p;
  605. #ifdef ED_DBGP
  606. printk("dev->ioport[c] = %x atp_readb_io(dev, c, 0x1c) = %x dev->in_int[%d] = %d dev->in_snd[%d] = %d\n",dev->ioport[c],atp_readb_io(dev, c, 0x1c),c,dev->in_int[c],c,dev->in_snd[c]);
  607. #endif
  608. if ((atp_readb_io(dev, c, 0x1c) == 0) && (dev->in_int[c] == 0) && (dev->in_snd[c] == 0)) {
  609. #ifdef ED_DBGP
  610. printk("Call sent_s870(atp870u_queuecommand)\n");
  611. #endif
  612. send_s870(dev,c);
  613. }
  614. #ifdef ED_DBGP
  615. printk("atp870u_queuecommand : exit\n");
  616. #endif
  617. return 0;
  618. }
  619. static DEF_SCSI_QCMD(atp870u_queuecommand)
  620. /**
  621. * send_s870 - send a command to the controller
  622. * @host: host
  623. *
  624. * On entry there is work queued to be done. We move some of that work to the
  625. * controller itself.
  626. *
  627. * Caller holds the host lock.
  628. */
  629. static void send_s870(struct atp_unit *dev,unsigned char c)
  630. {
  631. struct scsi_cmnd *workreq = NULL;
  632. unsigned int i;//,k;
  633. unsigned char j, target_id;
  634. unsigned char *prd;
  635. unsigned short int w;
  636. unsigned long l, bttl = 0;
  637. unsigned long sg_count;
  638. if (dev->in_snd[c] != 0) {
  639. #ifdef ED_DBGP
  640. printk("cmnd in_snd\n");
  641. #endif
  642. return;
  643. }
  644. #ifdef ED_DBGP
  645. printk("Sent_s870 enter\n");
  646. #endif
  647. dev->in_snd[c] = 1;
  648. if ((dev->last_cmd[c] != 0xff) && ((dev->last_cmd[c] & 0x40) != 0)) {
  649. dev->last_cmd[c] &= 0x0f;
  650. workreq = dev->id[c][dev->last_cmd[c]].curr_req;
  651. if (!workreq) {
  652. dev->last_cmd[c] = 0xff;
  653. if (dev->quhd[c] == dev->quend[c]) {
  654. dev->in_snd[c] = 0;
  655. return;
  656. }
  657. }
  658. }
  659. if (!workreq) {
  660. if ((dev->last_cmd[c] != 0xff) && (dev->working[c] != 0)) {
  661. dev->in_snd[c] = 0;
  662. return;
  663. }
  664. dev->working[c]++;
  665. j = dev->quhd[c];
  666. dev->quhd[c]++;
  667. if (dev->quhd[c] >= qcnt)
  668. dev->quhd[c] = 0;
  669. workreq = dev->quereq[c][dev->quhd[c]];
  670. if (dev->id[c][scmd_id(workreq)].curr_req != NULL) {
  671. dev->quhd[c] = j;
  672. dev->working[c]--;
  673. dev->in_snd[c] = 0;
  674. return;
  675. }
  676. dev->id[c][scmd_id(workreq)].curr_req = workreq;
  677. dev->last_cmd[c] = scmd_id(workreq);
  678. }
  679. if ((atp_readb_io(dev, c, 0x1f) & 0xb0) != 0 || atp_readb_io(dev, c, 0x1c) != 0) {
  680. #ifdef ED_DBGP
  681. printk("Abort to Send\n");
  682. #endif
  683. dev->last_cmd[c] |= 0x40;
  684. dev->in_snd[c] = 0;
  685. return;
  686. }
  687. #ifdef ED_DBGP
  688. printk("OK to Send\n");
  689. scmd_printk(KERN_DEBUG, workreq, "CDB");
  690. for(i=0;i<workreq->cmd_len;i++) {
  691. printk(" %x",workreq->cmnd[i]);
  692. }
  693. printk("\n");
  694. #endif
  695. l = scsi_bufflen(workreq);
  696. if (is885(dev)) {
  697. j = atp_readb_base(dev, 0x29) & 0xfe;
  698. atp_writeb_base(dev, 0x29, j);
  699. dev->r1f[c][scmd_id(workreq)] = 0;
  700. }
  701. if (workreq->cmnd[0] == READ_CAPACITY) {
  702. if (l > 8)
  703. l = 8;
  704. }
  705. if (workreq->cmnd[0] == 0x00) {
  706. l = 0;
  707. }
  708. j = 0;
  709. target_id = scmd_id(workreq);
  710. /*
  711. * Wide ?
  712. */
  713. w = 1;
  714. w = w << target_id;
  715. if ((w & dev->wide_id[c]) != 0) {
  716. j |= 0x01;
  717. }
  718. atp_writeb_io(dev, c, 0x1b, j);
  719. while ((atp_readb_io(dev, c, 0x1b) & 0x01) != j) {
  720. atp_writeb_pci(dev, c, 0x1b, j);
  721. #ifdef ED_DBGP
  722. printk("send_s870 while loop 1\n");
  723. #endif
  724. }
  725. /*
  726. * Write the command
  727. */
  728. atp_writeb_io(dev, c, 0x00, workreq->cmd_len);
  729. atp_writeb_io(dev, c, 0x01, 0x2c);
  730. if (is885(dev))
  731. atp_writeb_io(dev, c, 0x02, 0x7f);
  732. else
  733. atp_writeb_io(dev, c, 0x02, 0xcf);
  734. for (i = 0; i < workreq->cmd_len; i++)
  735. atp_writeb_io(dev, c, 0x03 + i, workreq->cmnd[i]);
  736. atp_writeb_io(dev, c, 0x0f, workreq->device->lun);
  737. /*
  738. * Write the target
  739. */
  740. atp_writeb_io(dev, c, 0x11, dev->id[c][target_id].devsp);
  741. #ifdef ED_DBGP
  742. printk("dev->id[%d][%d].devsp = %2x\n",c,target_id,dev->id[c][target_id].devsp);
  743. #endif
  744. sg_count = scsi_dma_map(workreq);
  745. /*
  746. * Write transfer size
  747. */
  748. atp_writeb_io(dev, c, 0x12, ((unsigned char *) (&l))[2]);
  749. atp_writeb_io(dev, c, 0x13, ((unsigned char *) (&l))[1]);
  750. atp_writeb_io(dev, c, 0x14, ((unsigned char *) (&l))[0]);
  751. j = target_id;
  752. dev->id[c][j].last_len = l;
  753. dev->id[c][j].tran_len = 0;
  754. #ifdef ED_DBGP
  755. printk("dev->id[%2d][%2d].last_len = %d\n",c,j,dev->id[c][j].last_len);
  756. #endif
  757. /*
  758. * Flip the wide bits
  759. */
  760. if ((j & 0x08) != 0) {
  761. j = (j & 0x07) | 0x40;
  762. }
  763. /*
  764. * Check transfer direction
  765. */
  766. if (workreq->sc_data_direction == DMA_TO_DEVICE)
  767. atp_writeb_io(dev, c, 0x15, j | 0x20);
  768. else
  769. atp_writeb_io(dev, c, 0x15, j);
  770. atp_writeb_io(dev, c, 0x16, atp_readb_io(dev, c, 0x16) | 0x80);
  771. atp_writeb_io(dev, c, 0x16, 0x80);
  772. dev->id[c][target_id].dirct = 0;
  773. if (l == 0) {
  774. if (atp_readb_io(dev, c, 0x1c) == 0) {
  775. #ifdef ED_DBGP
  776. printk("change SCSI_CMD_REG 0x08\n");
  777. #endif
  778. atp_writeb_io(dev, c, 0x18, 0x08);
  779. } else
  780. dev->last_cmd[c] |= 0x40;
  781. dev->in_snd[c] = 0;
  782. return;
  783. }
  784. prd = dev->id[c][target_id].prd_table;
  785. dev->id[c][target_id].prd_pos = prd;
  786. /*
  787. * Now write the request list. Either as scatter/gather or as
  788. * a linear chain.
  789. */
  790. if (l) {
  791. struct scatterlist *sgpnt;
  792. i = 0;
  793. scsi_for_each_sg(workreq, sgpnt, sg_count, j) {
  794. bttl = sg_dma_address(sgpnt);
  795. l=sg_dma_len(sgpnt);
  796. #ifdef ED_DBGP
  797. printk("1. bttl %x, l %x\n",bttl, l);
  798. #endif
  799. while (l > 0x10000) {
  800. (((u16 *) (prd))[i + 3]) = 0x0000;
  801. (((u16 *) (prd))[i + 2]) = 0x0000;
  802. (((u32 *) (prd))[i >> 1]) = cpu_to_le32(bttl);
  803. l -= 0x10000;
  804. bttl += 0x10000;
  805. i += 0x04;
  806. }
  807. (((u32 *) (prd))[i >> 1]) = cpu_to_le32(bttl);
  808. (((u16 *) (prd))[i + 2]) = cpu_to_le16(l);
  809. (((u16 *) (prd))[i + 3]) = 0;
  810. i += 0x04;
  811. }
  812. (((u16 *) (prd))[i - 1]) = cpu_to_le16(0x8000);
  813. #ifdef ED_DBGP
  814. printk("prd %4x %4x %4x %4x\n",(((unsigned short int *)prd)[0]),(((unsigned short int *)prd)[1]),(((unsigned short int *)prd)[2]),(((unsigned short int *)prd)[3]));
  815. printk("2. bttl %x, l %x\n",bttl, l);
  816. #endif
  817. }
  818. #ifdef ED_DBGP
  819. printk("send_s870: prdaddr_2 0x%8x target_id %d\n", dev->id[c][target_id].prdaddr,target_id);
  820. #endif
  821. dev->id[c][target_id].prdaddr = dev->id[c][target_id].prd_bus;
  822. atp_writel_pci(dev, c, 4, dev->id[c][target_id].prdaddr);
  823. atp_writeb_pci(dev, c, 2, 0x06);
  824. atp_writeb_pci(dev, c, 2, 0x00);
  825. if (is885(dev)) {
  826. j = atp_readb_pci(dev, c, 1) & 0xf3;
  827. if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) ||
  828. (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
  829. j |= 0x0c;
  830. }
  831. atp_writeb_pci(dev, c, 1, j);
  832. } else if (is880(dev)) {
  833. if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a))
  834. atp_writeb_base(dev, 0x3b, (atp_readb_base(dev, 0x3b) & 0x3f) | 0xc0);
  835. else
  836. atp_writeb_base(dev, 0x3b, atp_readb_base(dev, 0x3b) & 0x3f);
  837. } else {
  838. if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a))
  839. atp_writeb_base(dev, 0x3a, (atp_readb_base(dev, 0x3a) & 0xf3) | 0x08);
  840. else
  841. atp_writeb_base(dev, 0x3a, atp_readb_base(dev, 0x3a) & 0xf3);
  842. }
  843. if(workreq->sc_data_direction == DMA_TO_DEVICE) {
  844. dev->id[c][target_id].dirct = 0x20;
  845. if (atp_readb_io(dev, c, 0x1c) == 0) {
  846. atp_writeb_io(dev, c, 0x18, 0x08);
  847. atp_writeb_pci(dev, c, 0, 0x01);
  848. #ifdef ED_DBGP
  849. printk( "start DMA(to target)\n");
  850. #endif
  851. } else {
  852. dev->last_cmd[c] |= 0x40;
  853. }
  854. dev->in_snd[c] = 0;
  855. return;
  856. }
  857. if (atp_readb_io(dev, c, 0x1c) == 0) {
  858. atp_writeb_io(dev, c, 0x18, 0x08);
  859. atp_writeb_pci(dev, c, 0, 0x09);
  860. #ifdef ED_DBGP
  861. printk( "start DMA(to host)\n");
  862. #endif
  863. } else {
  864. dev->last_cmd[c] |= 0x40;
  865. }
  866. dev->in_snd[c] = 0;
  867. return;
  868. }
  869. static unsigned char fun_scam(struct atp_unit *dev, unsigned short int *val)
  870. {
  871. unsigned short int i, k;
  872. unsigned char j;
  873. atp_writew_io(dev, 0, 0x1c, *val);
  874. for (i = 0; i < 10; i++) { /* stable >= bus settle delay(400 ns) */
  875. k = atp_readw_io(dev, 0, 0x1c);
  876. j = (unsigned char) (k >> 8);
  877. if ((k & 0x8000) != 0) /* DB7 all release? */
  878. i = 0;
  879. }
  880. *val |= 0x4000; /* assert DB6 */
  881. atp_writew_io(dev, 0, 0x1c, *val);
  882. *val &= 0xdfff; /* assert DB5 */
  883. atp_writew_io(dev, 0, 0x1c, *val);
  884. for (i = 0; i < 10; i++) { /* stable >= bus settle delay(400 ns) */
  885. if ((atp_readw_io(dev, 0, 0x1c) & 0x2000) != 0) /* DB5 all release? */
  886. i = 0;
  887. }
  888. *val |= 0x8000; /* no DB4-0, assert DB7 */
  889. *val &= 0xe0ff;
  890. atp_writew_io(dev, 0, 0x1c, *val);
  891. *val &= 0xbfff; /* release DB6 */
  892. atp_writew_io(dev, 0, 0x1c, *val);
  893. for (i = 0; i < 10; i++) { /* stable >= bus settle delay(400 ns) */
  894. if ((atp_readw_io(dev, 0, 0x1c) & 0x4000) != 0) /* DB6 all release? */
  895. i = 0;
  896. }
  897. return j;
  898. }
  899. static void tscam(struct Scsi_Host *host, bool wide_chip, u8 scam_on)
  900. {
  901. unsigned char i, j, k;
  902. unsigned long n;
  903. unsigned short int m, assignid_map, val;
  904. unsigned char mbuf[33], quintet[2];
  905. struct atp_unit *dev = (struct atp_unit *)&host->hostdata;
  906. static unsigned char g2q_tab[8] = {
  907. 0x38, 0x31, 0x32, 0x2b, 0x34, 0x2d, 0x2e, 0x27
  908. };
  909. /* I can't believe we need this before we've even done anything. Remove it
  910. * and see if anyone bitches.
  911. for (i = 0; i < 0x10; i++) {
  912. udelay(0xffff);
  913. }
  914. */
  915. atp_writeb_io(dev, 0, 1, 0x08);
  916. atp_writeb_io(dev, 0, 2, 0x7f);
  917. atp_writeb_io(dev, 0, 0x11, 0x20);
  918. if ((scam_on & 0x40) == 0) {
  919. return;
  920. }
  921. m = 1;
  922. m <<= dev->host_id[0];
  923. j = 16;
  924. if (!wide_chip) {
  925. m |= 0xff00;
  926. j = 8;
  927. }
  928. assignid_map = m;
  929. atp_writeb_io(dev, 0, 0x02, 0x02); /* 2*2=4ms,3EH 2/32*3E=3.9ms */
  930. atp_writeb_io(dev, 0, 0x03, 0);
  931. atp_writeb_io(dev, 0, 0x04, 0);
  932. atp_writeb_io(dev, 0, 0x05, 0);
  933. atp_writeb_io(dev, 0, 0x06, 0);
  934. atp_writeb_io(dev, 0, 0x07, 0);
  935. atp_writeb_io(dev, 0, 0x08, 0);
  936. for (i = 0; i < j; i++) {
  937. m = 1;
  938. m = m << i;
  939. if ((m & assignid_map) != 0) {
  940. continue;
  941. }
  942. atp_writeb_io(dev, 0, 0x0f, 0);
  943. atp_writeb_io(dev, 0, 0x12, 0);
  944. atp_writeb_io(dev, 0, 0x13, 0);
  945. atp_writeb_io(dev, 0, 0x14, 0);
  946. if (i > 7) {
  947. k = (i & 0x07) | 0x40;
  948. } else {
  949. k = i;
  950. }
  951. atp_writeb_io(dev, 0, 0x15, k);
  952. if (wide_chip)
  953. atp_writeb_io(dev, 0, 0x1b, 0x01);
  954. else
  955. atp_writeb_io(dev, 0, 0x1b, 0x00);
  956. do {
  957. atp_writeb_io(dev, 0, 0x18, 0x09);
  958. while ((atp_readb_io(dev, 0, 0x1f) & 0x80) == 0x00)
  959. cpu_relax();
  960. k = atp_readb_io(dev, 0, 0x17);
  961. if ((k == 0x85) || (k == 0x42))
  962. break;
  963. if (k != 0x16)
  964. atp_writeb_io(dev, 0, 0x10, 0x41);
  965. } while (k != 0x16);
  966. if ((k == 0x85) || (k == 0x42))
  967. continue;
  968. assignid_map |= m;
  969. }
  970. atp_writeb_io(dev, 0, 0x02, 0x7f);
  971. atp_writeb_io(dev, 0, 0x1b, 0x02);
  972. udelay(2);
  973. val = 0x0080; /* bsy */
  974. atp_writew_io(dev, 0, 0x1c, val);
  975. val |= 0x0040; /* sel */
  976. atp_writew_io(dev, 0, 0x1c, val);
  977. val |= 0x0004; /* msg */
  978. atp_writew_io(dev, 0, 0x1c, val);
  979. udelay(2); /* 2 deskew delay(45ns*2=90ns) */
  980. val &= 0x007f; /* no bsy */
  981. atp_writew_io(dev, 0, 0x1c, val);
  982. mdelay(128);
  983. val &= 0x00fb; /* after 1ms no msg */
  984. atp_writew_io(dev, 0, 0x1c, val);
  985. while ((atp_readb_io(dev, 0, 0x1c) & 0x04) != 0)
  986. ;
  987. udelay(2);
  988. udelay(100);
  989. for (n = 0; n < 0x30000; n++)
  990. if ((atp_readb_io(dev, 0, 0x1c) & 0x80) != 0) /* bsy ? */
  991. break;
  992. if (n < 0x30000)
  993. for (n = 0; n < 0x30000; n++)
  994. if ((atp_readb_io(dev, 0, 0x1c) & 0x81) == 0x0081) {
  995. udelay(2);
  996. val |= 0x8003; /* io,cd,db7 */
  997. atp_writew_io(dev, 0, 0x1c, val);
  998. udelay(2);
  999. val &= 0x00bf; /* no sel */
  1000. atp_writew_io(dev, 0, 0x1c, val);
  1001. udelay(2);
  1002. break;
  1003. }
  1004. while (1) {
  1005. /*
  1006. * The funny division into multiple delays is to accomodate
  1007. * arches like ARM where udelay() multiplies its argument by
  1008. * a large number to initialize a loop counter. To avoid
  1009. * overflow, the maximum supported udelay is 2000 microseconds.
  1010. *
  1011. * XXX it would be more polite to find a way to use msleep()
  1012. */
  1013. mdelay(2);
  1014. udelay(48);
  1015. if ((atp_readb_io(dev, 0, 0x1c) & 0x80) == 0x00) { /* bsy ? */
  1016. atp_writew_io(dev, 0, 0x1c, 0);
  1017. atp_writeb_io(dev, 0, 0x1b, 0);
  1018. atp_writeb_io(dev, 0, 0x15, 0);
  1019. atp_writeb_io(dev, 0, 0x18, 0x09);
  1020. while ((atp_readb_io(dev, 0, 0x1f) & 0x80) == 0)
  1021. cpu_relax();
  1022. atp_readb_io(dev, 0, 0x17);
  1023. return;
  1024. }
  1025. val &= 0x00ff; /* synchronization */
  1026. val |= 0x3f00;
  1027. fun_scam(dev, &val);
  1028. udelay(2);
  1029. val &= 0x00ff; /* isolation */
  1030. val |= 0x2000;
  1031. fun_scam(dev, &val);
  1032. udelay(2);
  1033. i = 8;
  1034. j = 0;
  1035. while (1) {
  1036. if ((atp_readw_io(dev, 0, 0x1c) & 0x2000) == 0)
  1037. continue;
  1038. udelay(2);
  1039. val &= 0x00ff; /* get ID_STRING */
  1040. val |= 0x2000;
  1041. k = fun_scam(dev, &val);
  1042. if ((k & 0x03) == 0)
  1043. break;
  1044. mbuf[j] <<= 0x01;
  1045. mbuf[j] &= 0xfe;
  1046. if ((k & 0x02) != 0)
  1047. mbuf[j] |= 0x01;
  1048. i--;
  1049. if (i > 0)
  1050. continue;
  1051. j++;
  1052. i = 8;
  1053. }
  1054. /* isolation complete.. */
  1055. /* mbuf[32]=0;
  1056. printk(" \n%x %x %x %s\n ",assignid_map,mbuf[0],mbuf[1],&mbuf[2]); */
  1057. i = 15;
  1058. j = mbuf[0];
  1059. if ((j & 0x20) != 0) { /* bit5=1:ID up to 7 */
  1060. i = 7;
  1061. }
  1062. if ((j & 0x06) != 0) { /* IDvalid? */
  1063. k = mbuf[1];
  1064. while (1) {
  1065. m = 1;
  1066. m <<= k;
  1067. if ((m & assignid_map) == 0)
  1068. break;
  1069. if (k > 0)
  1070. k--;
  1071. else
  1072. break;
  1073. }
  1074. }
  1075. if ((m & assignid_map) != 0) { /* srch from max acceptable ID# */
  1076. k = i; /* max acceptable ID# */
  1077. while (1) {
  1078. m = 1;
  1079. m <<= k;
  1080. if ((m & assignid_map) == 0)
  1081. break;
  1082. if (k > 0)
  1083. k--;
  1084. else
  1085. break;
  1086. }
  1087. }
  1088. /* k=binID#, */
  1089. assignid_map |= m;
  1090. if (k < 8) {
  1091. quintet[0] = 0x38; /* 1st dft ID<8 */
  1092. } else {
  1093. quintet[0] = 0x31; /* 1st ID>=8 */
  1094. }
  1095. k &= 0x07;
  1096. quintet[1] = g2q_tab[k];
  1097. val &= 0x00ff; /* AssignID 1stQuintet,AH=001xxxxx */
  1098. m = quintet[0] << 8;
  1099. val |= m;
  1100. fun_scam(dev, &val);
  1101. val &= 0x00ff; /* AssignID 2ndQuintet,AH=001xxxxx */
  1102. m = quintet[1] << 8;
  1103. val |= m;
  1104. fun_scam(dev, &val);
  1105. }
  1106. }
  1107. static void atp870u_free_tables(struct Scsi_Host *host)
  1108. {
  1109. struct atp_unit *atp_dev = (struct atp_unit *)&host->hostdata;
  1110. int j, k;
  1111. for (j=0; j < 2; j++) {
  1112. for (k = 0; k < 16; k++) {
  1113. if (!atp_dev->id[j][k].prd_table)
  1114. continue;
  1115. pci_free_consistent(atp_dev->pdev, 1024, atp_dev->id[j][k].prd_table, atp_dev->id[j][k].prd_bus);
  1116. atp_dev->id[j][k].prd_table = NULL;
  1117. }
  1118. }
  1119. }
  1120. static int atp870u_init_tables(struct Scsi_Host *host)
  1121. {
  1122. struct atp_unit *atp_dev = (struct atp_unit *)&host->hostdata;
  1123. int c,k;
  1124. for(c=0;c < 2;c++) {
  1125. for(k=0;k<16;k++) {
  1126. atp_dev->id[c][k].prd_table = pci_alloc_consistent(atp_dev->pdev, 1024, &(atp_dev->id[c][k].prd_bus));
  1127. if (!atp_dev->id[c][k].prd_table) {
  1128. printk("atp870u_init_tables fail\n");
  1129. atp870u_free_tables(host);
  1130. return -ENOMEM;
  1131. }
  1132. atp_dev->id[c][k].prdaddr = atp_dev->id[c][k].prd_bus;
  1133. atp_dev->id[c][k].devsp=0x20;
  1134. atp_dev->id[c][k].devtype = 0x7f;
  1135. atp_dev->id[c][k].curr_req = NULL;
  1136. }
  1137. atp_dev->active_id[c] = 0;
  1138. atp_dev->wide_id[c] = 0;
  1139. atp_dev->host_id[c] = 0x07;
  1140. atp_dev->quhd[c] = 0;
  1141. atp_dev->quend[c] = 0;
  1142. atp_dev->last_cmd[c] = 0xff;
  1143. atp_dev->in_snd[c] = 0;
  1144. atp_dev->in_int[c] = 0;
  1145. for (k = 0; k < qcnt; k++) {
  1146. atp_dev->quereq[c][k] = NULL;
  1147. }
  1148. for (k = 0; k < 16; k++) {
  1149. atp_dev->id[c][k].curr_req = NULL;
  1150. atp_dev->sp[c][k] = 0x04;
  1151. }
  1152. }
  1153. return 0;
  1154. }
  1155. static void atp_set_host_id(struct atp_unit *atp, u8 c, u8 host_id)
  1156. {
  1157. atp_writeb_io(atp, c, 0, host_id | 0x08);
  1158. atp_writeb_io(atp, c, 0x18, 0);
  1159. while ((atp_readb_io(atp, c, 0x1f) & 0x80) == 0)
  1160. mdelay(1);
  1161. atp_readb_io(atp, c, 0x17);
  1162. atp_writeb_io(atp, c, 1, 8);
  1163. atp_writeb_io(atp, c, 2, 0x7f);
  1164. atp_writeb_io(atp, c, 0x11, 0x20);
  1165. }
  1166. static void atp870_init(struct Scsi_Host *shpnt)
  1167. {
  1168. struct atp_unit *atpdev = shost_priv(shpnt);
  1169. struct pci_dev *pdev = atpdev->pdev;
  1170. unsigned char k, host_id;
  1171. u8 scam_on;
  1172. bool wide_chip =
  1173. (pdev->device == PCI_DEVICE_ID_ARTOP_AEC7610 &&
  1174. pdev->revision == 4) ||
  1175. (pdev->device == PCI_DEVICE_ID_ARTOP_AEC7612UW) ||
  1176. (pdev->device == PCI_DEVICE_ID_ARTOP_AEC7612SUW);
  1177. pci_read_config_byte(pdev, 0x49, &host_id);
  1178. dev_info(&pdev->dev, "ACARD AEC-671X PCI Ultra/W SCSI-2/3 Host Adapter: IO:%lx, IRQ:%d.\n",
  1179. shpnt->io_port, shpnt->irq);
  1180. atpdev->ioport[0] = shpnt->io_port;
  1181. atpdev->pciport[0] = shpnt->io_port + 0x20;
  1182. host_id &= 0x07;
  1183. atpdev->host_id[0] = host_id;
  1184. scam_on = atp_readb_pci(atpdev, 0, 2);
  1185. atpdev->global_map[0] = atp_readb_base(atpdev, 0x2d);
  1186. atpdev->ultra_map[0] = atp_readw_base(atpdev, 0x2e);
  1187. if (atpdev->ultra_map[0] == 0) {
  1188. scam_on = 0x00;
  1189. atpdev->global_map[0] = 0x20;
  1190. atpdev->ultra_map[0] = 0xffff;
  1191. }
  1192. if (pdev->revision > 0x07) /* check if atp876 chip */
  1193. atp_writeb_base(atpdev, 0x3e, 0x00); /* enable terminator */
  1194. k = (atp_readb_base(atpdev, 0x3a) & 0xf3) | 0x10;
  1195. atp_writeb_base(atpdev, 0x3a, k);
  1196. atp_writeb_base(atpdev, 0x3a, k & 0xdf);
  1197. mdelay(32);
  1198. atp_writeb_base(atpdev, 0x3a, k);
  1199. mdelay(32);
  1200. atp_set_host_id(atpdev, 0, host_id);
  1201. tscam(shpnt, wide_chip, scam_on);
  1202. atp_writeb_base(atpdev, 0x3a, atp_readb_base(atpdev, 0x3a) | 0x10);
  1203. atp_is(atpdev, 0, wide_chip, 0);
  1204. atp_writeb_base(atpdev, 0x3a, atp_readb_base(atpdev, 0x3a) & 0xef);
  1205. atp_writeb_base(atpdev, 0x3b, atp_readb_base(atpdev, 0x3b) | 0x20);
  1206. shpnt->max_id = wide_chip ? 16 : 8;
  1207. shpnt->this_id = host_id;
  1208. }
  1209. static void atp880_init(struct Scsi_Host *shpnt)
  1210. {
  1211. struct atp_unit *atpdev = shost_priv(shpnt);
  1212. struct pci_dev *pdev = atpdev->pdev;
  1213. unsigned char k, m, host_id;
  1214. unsigned int n;
  1215. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x80);
  1216. atpdev->ioport[0] = shpnt->io_port + 0x40;
  1217. atpdev->pciport[0] = shpnt->io_port + 0x28;
  1218. host_id = atp_readb_base(atpdev, 0x39) >> 4;
  1219. dev_info(&pdev->dev, "ACARD AEC-67160 PCI Ultra3 LVD Host Adapter: IO:%lx, IRQ:%d.\n",
  1220. shpnt->io_port, shpnt->irq);
  1221. atpdev->host_id[0] = host_id;
  1222. atpdev->global_map[0] = atp_readb_base(atpdev, 0x35);
  1223. atpdev->ultra_map[0] = atp_readw_base(atpdev, 0x3c);
  1224. n = 0x3f09;
  1225. while (n < 0x4000) {
  1226. m = 0;
  1227. atp_writew_base(atpdev, 0x34, n);
  1228. n += 0x0002;
  1229. if (atp_readb_base(atpdev, 0x30) == 0xff)
  1230. break;
  1231. atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
  1232. atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
  1233. atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
  1234. atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
  1235. atp_writew_base(atpdev, 0x34, n);
  1236. n += 0x0002;
  1237. atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
  1238. atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
  1239. atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
  1240. atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
  1241. atp_writew_base(atpdev, 0x34, n);
  1242. n += 0x0002;
  1243. atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
  1244. atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
  1245. atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
  1246. atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
  1247. atp_writew_base(atpdev, 0x34, n);
  1248. n += 0x0002;
  1249. atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
  1250. atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
  1251. atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
  1252. atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
  1253. n += 0x0018;
  1254. }
  1255. atp_writew_base(atpdev, 0x34, 0);
  1256. atpdev->ultra_map[0] = 0;
  1257. atpdev->async[0] = 0;
  1258. for (k = 0; k < 16; k++) {
  1259. n = 1 << k;
  1260. if (atpdev->sp[0][k] > 1)
  1261. atpdev->ultra_map[0] |= n;
  1262. else
  1263. if (atpdev->sp[0][k] == 0)
  1264. atpdev->async[0] |= n;
  1265. }
  1266. atpdev->async[0] = ~(atpdev->async[0]);
  1267. atp_writeb_base(atpdev, 0x35, atpdev->global_map[0]);
  1268. k = atp_readb_base(atpdev, 0x38) & 0x80;
  1269. atp_writeb_base(atpdev, 0x38, k);
  1270. atp_writeb_base(atpdev, 0x3b, 0x20);
  1271. mdelay(32);
  1272. atp_writeb_base(atpdev, 0x3b, 0);
  1273. mdelay(32);
  1274. atp_readb_io(atpdev, 0, 0x1b);
  1275. atp_readb_io(atpdev, 0, 0x17);
  1276. atp_set_host_id(atpdev, 0, host_id);
  1277. tscam(shpnt, true, atp_readb_base(atpdev, 0x22));
  1278. atp_is(atpdev, 0, true, atp_readb_base(atpdev, 0x3f) & 0x40);
  1279. atp_writeb_base(atpdev, 0x38, 0xb0);
  1280. shpnt->max_id = 16;
  1281. shpnt->this_id = host_id;
  1282. }
  1283. static void atp885_init(struct Scsi_Host *shpnt)
  1284. {
  1285. struct atp_unit *atpdev = shost_priv(shpnt);
  1286. struct pci_dev *pdev = atpdev->pdev;
  1287. unsigned char k, m, c;
  1288. unsigned int n;
  1289. unsigned char setupdata[2][16];
  1290. dev_info(&pdev->dev, "ACARD AEC-67162 PCI Ultra3 LVD Host Adapter: IO:%lx, IRQ:%d.\n",
  1291. shpnt->io_port, shpnt->irq);
  1292. atpdev->ioport[0] = shpnt->io_port + 0x80;
  1293. atpdev->ioport[1] = shpnt->io_port + 0xc0;
  1294. atpdev->pciport[0] = shpnt->io_port + 0x40;
  1295. atpdev->pciport[1] = shpnt->io_port + 0x50;
  1296. c = atp_readb_base(atpdev, 0x29);
  1297. atp_writeb_base(atpdev, 0x29, c | 0x04);
  1298. n = 0x1f80;
  1299. while (n < 0x2000) {
  1300. atp_writew_base(atpdev, 0x3c, n);
  1301. if (atp_readl_base(atpdev, 0x38) == 0xffffffff)
  1302. break;
  1303. for (m = 0; m < 2; m++) {
  1304. atpdev->global_map[m] = 0;
  1305. for (k = 0; k < 4; k++) {
  1306. atp_writew_base(atpdev, 0x3c, n++);
  1307. ((unsigned long *)&setupdata[m][0])[k] = atp_readl_base(atpdev, 0x38);
  1308. }
  1309. for (k = 0; k < 4; k++) {
  1310. atp_writew_base(atpdev, 0x3c, n++);
  1311. ((unsigned long *)&atpdev->sp[m][0])[k] = atp_readl_base(atpdev, 0x38);
  1312. }
  1313. n += 8;
  1314. }
  1315. }
  1316. c = atp_readb_base(atpdev, 0x29);
  1317. atp_writeb_base(atpdev, 0x29, c & 0xfb);
  1318. for (c = 0; c < 2; c++) {
  1319. atpdev->ultra_map[c] = 0;
  1320. atpdev->async[c] = 0;
  1321. for (k = 0; k < 16; k++) {
  1322. n = 1 << k;
  1323. if (atpdev->sp[c][k] > 1)
  1324. atpdev->ultra_map[c] |= n;
  1325. else
  1326. if (atpdev->sp[c][k] == 0)
  1327. atpdev->async[c] |= n;
  1328. }
  1329. atpdev->async[c] = ~(atpdev->async[c]);
  1330. if (atpdev->global_map[c] == 0) {
  1331. k = setupdata[c][1];
  1332. if ((k & 0x40) != 0)
  1333. atpdev->global_map[c] |= 0x20;
  1334. k &= 0x07;
  1335. atpdev->global_map[c] |= k;
  1336. if ((setupdata[c][2] & 0x04) != 0)
  1337. atpdev->global_map[c] |= 0x08;
  1338. atpdev->host_id[c] = setupdata[c][0] & 0x07;
  1339. }
  1340. }
  1341. k = atp_readb_base(atpdev, 0x28) & 0x8f;
  1342. k |= 0x10;
  1343. atp_writeb_base(atpdev, 0x28, k);
  1344. atp_writeb_pci(atpdev, 0, 1, 0x80);
  1345. atp_writeb_pci(atpdev, 1, 1, 0x80);
  1346. mdelay(100);
  1347. atp_writeb_pci(atpdev, 0, 1, 0);
  1348. atp_writeb_pci(atpdev, 1, 1, 0);
  1349. mdelay(1000);
  1350. atp_readb_io(atpdev, 0, 0x1b);
  1351. atp_readb_io(atpdev, 0, 0x17);
  1352. atp_readb_io(atpdev, 1, 0x1b);
  1353. atp_readb_io(atpdev, 1, 0x17);
  1354. k = atpdev->host_id[0];
  1355. if (k > 7)
  1356. k = (k & 0x07) | 0x40;
  1357. atp_set_host_id(atpdev, 0, k);
  1358. k = atpdev->host_id[1];
  1359. if (k > 7)
  1360. k = (k & 0x07) | 0x40;
  1361. atp_set_host_id(atpdev, 1, k);
  1362. mdelay(600); /* this delay used to be called tscam_885() */
  1363. dev_info(&pdev->dev, "Scanning Channel A SCSI Device ...\n");
  1364. atp_is(atpdev, 0, true, atp_readb_io(atpdev, 0, 0x1b) >> 7);
  1365. atp_writeb_io(atpdev, 0, 0x16, 0x80);
  1366. dev_info(&pdev->dev, "Scanning Channel B SCSI Device ...\n");
  1367. atp_is(atpdev, 1, true, atp_readb_io(atpdev, 1, 0x1b) >> 7);
  1368. atp_writeb_io(atpdev, 1, 0x16, 0x80);
  1369. k = atp_readb_base(atpdev, 0x28) & 0xcf;
  1370. k |= 0xc0;
  1371. atp_writeb_base(atpdev, 0x28, k);
  1372. k = atp_readb_base(atpdev, 0x1f) | 0x80;
  1373. atp_writeb_base(atpdev, 0x1f, k);
  1374. k = atp_readb_base(atpdev, 0x29) | 0x01;
  1375. atp_writeb_base(atpdev, 0x29, k);
  1376. shpnt->max_id = 16;
  1377. shpnt->max_lun = (atpdev->global_map[0] & 0x07) + 1;
  1378. shpnt->max_channel = 1;
  1379. shpnt->this_id = atpdev->host_id[0];
  1380. }
  1381. /* return non-zero on detection */
  1382. static int atp870u_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1383. {
  1384. struct Scsi_Host *shpnt = NULL;
  1385. struct atp_unit *atpdev;
  1386. int err;
  1387. if (ent->device == PCI_DEVICE_ID_ARTOP_AEC7610 && pdev->revision < 2) {
  1388. dev_err(&pdev->dev, "ATP850S chips (AEC6710L/F cards) are not supported.\n");
  1389. return -ENODEV;
  1390. }
  1391. err = pci_enable_device(pdev);
  1392. if (err)
  1393. goto fail;
  1394. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
  1395. printk(KERN_ERR "atp870u: DMA mask required but not available.\n");
  1396. err = -EIO;
  1397. goto disable_device;
  1398. }
  1399. err = pci_request_regions(pdev, "atp870u");
  1400. if (err)
  1401. goto disable_device;
  1402. pci_set_master(pdev);
  1403. err = -ENOMEM;
  1404. shpnt = scsi_host_alloc(&atp870u_template, sizeof(struct atp_unit));
  1405. if (!shpnt)
  1406. goto release_region;
  1407. atpdev = shost_priv(shpnt);
  1408. atpdev->host = shpnt;
  1409. atpdev->pdev = pdev;
  1410. pci_set_drvdata(pdev, atpdev);
  1411. shpnt->io_port = pci_resource_start(pdev, 0);
  1412. shpnt->io_port &= 0xfffffff8;
  1413. shpnt->n_io_port = pci_resource_len(pdev, 0);
  1414. atpdev->baseport = shpnt->io_port;
  1415. shpnt->unique_id = shpnt->io_port;
  1416. shpnt->irq = pdev->irq;
  1417. err = atp870u_init_tables(shpnt);
  1418. if (err) {
  1419. dev_err(&pdev->dev, "Unable to allocate tables for Acard controller\n");
  1420. goto unregister;
  1421. }
  1422. if (is880(atpdev))
  1423. atp880_init(shpnt);
  1424. else if (is885(atpdev))
  1425. atp885_init(shpnt);
  1426. else
  1427. atp870_init(shpnt);
  1428. err = request_irq(shpnt->irq, atp870u_intr_handle, IRQF_SHARED, "atp870u", shpnt);
  1429. if (err) {
  1430. dev_err(&pdev->dev, "Unable to allocate IRQ %d.\n", shpnt->irq);
  1431. goto free_tables;
  1432. }
  1433. err = scsi_add_host(shpnt, &pdev->dev);
  1434. if (err)
  1435. goto scsi_add_fail;
  1436. scsi_scan_host(shpnt);
  1437. return 0;
  1438. scsi_add_fail:
  1439. free_irq(shpnt->irq, shpnt);
  1440. free_tables:
  1441. atp870u_free_tables(shpnt);
  1442. unregister:
  1443. scsi_host_put(shpnt);
  1444. release_region:
  1445. pci_release_regions(pdev);
  1446. disable_device:
  1447. pci_disable_device(pdev);
  1448. fail:
  1449. return err;
  1450. }
  1451. /* The abort command does not leave the device in a clean state where
  1452. it is available to be used again. Until this gets worked out, we will
  1453. leave it commented out. */
  1454. static int atp870u_abort(struct scsi_cmnd * SCpnt)
  1455. {
  1456. unsigned char j, k, c;
  1457. struct scsi_cmnd *workrequ;
  1458. struct atp_unit *dev;
  1459. struct Scsi_Host *host;
  1460. host = SCpnt->device->host;
  1461. dev = (struct atp_unit *)&host->hostdata;
  1462. c = scmd_channel(SCpnt);
  1463. printk(" atp870u: abort Channel = %x \n", c);
  1464. printk("working=%x last_cmd=%x ", dev->working[c], dev->last_cmd[c]);
  1465. printk(" quhdu=%x quendu=%x ", dev->quhd[c], dev->quend[c]);
  1466. for (j = 0; j < 0x18; j++) {
  1467. printk(" r%2x=%2x", j, atp_readb_io(dev, c, j));
  1468. }
  1469. printk(" r1c=%2x", atp_readb_io(dev, c, 0x1c));
  1470. printk(" r1f=%2x in_snd=%2x ", atp_readb_io(dev, c, 0x1f), dev->in_snd[c]);
  1471. printk(" d00=%2x", atp_readb_pci(dev, c, 0x00));
  1472. printk(" d02=%2x", atp_readb_pci(dev, c, 0x02));
  1473. for(j=0;j<16;j++) {
  1474. if (dev->id[c][j].curr_req != NULL) {
  1475. workrequ = dev->id[c][j].curr_req;
  1476. printk("\n que cdb= ");
  1477. for (k=0; k < workrequ->cmd_len; k++) {
  1478. printk(" %2x ",workrequ->cmnd[k]);
  1479. }
  1480. printk(" last_lenu= %x ",(unsigned int)dev->id[c][j].last_len);
  1481. }
  1482. }
  1483. return SUCCESS;
  1484. }
  1485. static const char *atp870u_info(struct Scsi_Host *notused)
  1486. {
  1487. static char buffer[128];
  1488. strcpy(buffer, "ACARD AEC-6710/6712/67160 PCI Ultra/W/LVD SCSI-3 Adapter Driver V2.6+ac ");
  1489. return buffer;
  1490. }
  1491. static int atp870u_show_info(struct seq_file *m, struct Scsi_Host *HBAptr)
  1492. {
  1493. seq_puts(m, "ACARD AEC-671X Driver Version: 2.6+ac\n\n"
  1494. "Adapter Configuration:\n");
  1495. seq_printf(m, " Base IO: %#.4lx\n", HBAptr->io_port);
  1496. seq_printf(m, " IRQ: %d\n", HBAptr->irq);
  1497. return 0;
  1498. }
  1499. static int atp870u_biosparam(struct scsi_device *disk, struct block_device *dev,
  1500. sector_t capacity, int *ip)
  1501. {
  1502. int heads, sectors, cylinders;
  1503. heads = 64;
  1504. sectors = 32;
  1505. cylinders = (unsigned long)capacity / (heads * sectors);
  1506. if (cylinders > 1024) {
  1507. heads = 255;
  1508. sectors = 63;
  1509. cylinders = (unsigned long)capacity / (heads * sectors);
  1510. }
  1511. ip[0] = heads;
  1512. ip[1] = sectors;
  1513. ip[2] = cylinders;
  1514. return 0;
  1515. }
  1516. static void atp870u_remove (struct pci_dev *pdev)
  1517. {
  1518. struct atp_unit *devext = pci_get_drvdata(pdev);
  1519. struct Scsi_Host *pshost = devext->host;
  1520. scsi_remove_host(pshost);
  1521. free_irq(pshost->irq, pshost);
  1522. pci_release_regions(pdev);
  1523. pci_disable_device(pdev);
  1524. atp870u_free_tables(pshost);
  1525. scsi_host_put(pshost);
  1526. }
  1527. MODULE_LICENSE("GPL");
  1528. static struct scsi_host_template atp870u_template = {
  1529. .module = THIS_MODULE,
  1530. .name = "atp870u" /* name */,
  1531. .proc_name = "atp870u",
  1532. .show_info = atp870u_show_info,
  1533. .info = atp870u_info /* info */,
  1534. .queuecommand = atp870u_queuecommand /* queuecommand */,
  1535. .eh_abort_handler = atp870u_abort /* abort */,
  1536. .bios_param = atp870u_biosparam /* biosparm */,
  1537. .can_queue = qcnt /* can_queue */,
  1538. .this_id = 7 /* SCSI ID */,
  1539. .sg_tablesize = ATP870U_SCATTER /*SG_ALL*/ /*SG_NONE*/,
  1540. .use_clustering = ENABLE_CLUSTERING,
  1541. .max_sectors = ATP870U_MAX_SECTORS,
  1542. };
  1543. static struct pci_device_id atp870u_id_table[] = {
  1544. { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP885_DEVID) },
  1545. { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP880_DEVID1) },
  1546. { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP880_DEVID2) },
  1547. { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7610) },
  1548. { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612UW) },
  1549. { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612U) },
  1550. { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612S) },
  1551. { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612D) },
  1552. { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612SUW) },
  1553. { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_8060) },
  1554. { 0, },
  1555. };
  1556. MODULE_DEVICE_TABLE(pci, atp870u_id_table);
  1557. static struct pci_driver atp870u_driver = {
  1558. .id_table = atp870u_id_table,
  1559. .name = "atp870u",
  1560. .probe = atp870u_probe,
  1561. .remove = atp870u_remove,
  1562. };
  1563. module_pci_driver(atp870u_driver);
  1564. static void atp_is(struct atp_unit *dev, unsigned char c, bool wide_chip, unsigned char lvdmode)
  1565. {
  1566. unsigned char i, j, k, rmb, n;
  1567. unsigned short int m;
  1568. static unsigned char mbuf[512];
  1569. static unsigned char satn[9] = { 0, 0, 0, 0, 0, 0, 0, 6, 6 };
  1570. static unsigned char inqd[9] = { 0x12, 0, 0, 0, 0x24, 0, 0, 0x24, 6 };
  1571. static unsigned char synn[6] = { 0x80, 1, 3, 1, 0x19, 0x0e };
  1572. unsigned char synu[6] = { 0x80, 1, 3, 1, 0x0a, 0x0e };
  1573. static unsigned char synw[6] = { 0x80, 1, 3, 1, 0x19, 0x0e };
  1574. static unsigned char synw_870[6] = { 0x80, 1, 3, 1, 0x0c, 0x07 };
  1575. unsigned char synuw[6] = { 0x80, 1, 3, 1, 0x0a, 0x0e };
  1576. static unsigned char wide[6] = { 0x80, 1, 2, 3, 1, 0 };
  1577. static unsigned char u3[9] = { 0x80, 1, 6, 4, 0x09, 00, 0x0e, 0x01, 0x02 };
  1578. for (i = 0; i < 16; i++) {
  1579. if (!wide_chip && (i > 7))
  1580. break;
  1581. m = 1;
  1582. m = m << i;
  1583. if ((m & dev->active_id[c]) != 0) {
  1584. continue;
  1585. }
  1586. if (i == dev->host_id[c]) {
  1587. printk(KERN_INFO " ID: %2d Host Adapter\n", dev->host_id[c]);
  1588. continue;
  1589. }
  1590. atp_writeb_io(dev, c, 0x1b, wide_chip ? 0x01 : 0x00);
  1591. atp_writeb_io(dev, c, 1, 0x08);
  1592. atp_writeb_io(dev, c, 2, 0x7f);
  1593. atp_writeb_io(dev, c, 3, satn[0]);
  1594. atp_writeb_io(dev, c, 4, satn[1]);
  1595. atp_writeb_io(dev, c, 5, satn[2]);
  1596. atp_writeb_io(dev, c, 6, satn[3]);
  1597. atp_writeb_io(dev, c, 7, satn[4]);
  1598. atp_writeb_io(dev, c, 8, satn[5]);
  1599. atp_writeb_io(dev, c, 0x0f, 0);
  1600. atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
  1601. atp_writeb_io(dev, c, 0x12, 0);
  1602. atp_writeb_io(dev, c, 0x13, satn[6]);
  1603. atp_writeb_io(dev, c, 0x14, satn[7]);
  1604. j = i;
  1605. if ((j & 0x08) != 0) {
  1606. j = (j & 0x07) | 0x40;
  1607. }
  1608. atp_writeb_io(dev, c, 0x15, j);
  1609. atp_writeb_io(dev, c, 0x18, satn[8]);
  1610. while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
  1611. cpu_relax();
  1612. if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
  1613. continue;
  1614. while (atp_readb_io(dev, c, 0x17) != 0x8e)
  1615. cpu_relax();
  1616. dev->active_id[c] |= m;
  1617. atp_writeb_io(dev, c, 0x10, 0x30);
  1618. if (is885(dev) || is880(dev))
  1619. atp_writeb_io(dev, c, 0x14, 0x00);
  1620. else /* result of is870() merge - is this a bug? */
  1621. atp_writeb_io(dev, c, 0x04, 0x00);
  1622. phase_cmd:
  1623. atp_writeb_io(dev, c, 0x18, 0x08);
  1624. while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
  1625. cpu_relax();
  1626. j = atp_readb_io(dev, c, 0x17);
  1627. if (j != 0x16) {
  1628. atp_writeb_io(dev, c, 0x10, 0x41);
  1629. goto phase_cmd;
  1630. }
  1631. sel_ok:
  1632. atp_writeb_io(dev, c, 3, inqd[0]);
  1633. atp_writeb_io(dev, c, 4, inqd[1]);
  1634. atp_writeb_io(dev, c, 5, inqd[2]);
  1635. atp_writeb_io(dev, c, 6, inqd[3]);
  1636. atp_writeb_io(dev, c, 7, inqd[4]);
  1637. atp_writeb_io(dev, c, 8, inqd[5]);
  1638. atp_writeb_io(dev, c, 0x0f, 0);
  1639. atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
  1640. atp_writeb_io(dev, c, 0x12, 0);
  1641. atp_writeb_io(dev, c, 0x13, inqd[6]);
  1642. atp_writeb_io(dev, c, 0x14, inqd[7]);
  1643. atp_writeb_io(dev, c, 0x18, inqd[8]);
  1644. while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
  1645. cpu_relax();
  1646. if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
  1647. continue;
  1648. while (atp_readb_io(dev, c, 0x17) != 0x8e)
  1649. cpu_relax();
  1650. if (wide_chip)
  1651. atp_writeb_io(dev, c, 0x1b, 0x00);
  1652. atp_writeb_io(dev, c, 0x18, 0x08);
  1653. j = 0;
  1654. rd_inq_data:
  1655. k = atp_readb_io(dev, c, 0x1f);
  1656. if ((k & 0x01) != 0) {
  1657. mbuf[j++] = atp_readb_io(dev, c, 0x19);
  1658. goto rd_inq_data;
  1659. }
  1660. if ((k & 0x80) == 0) {
  1661. goto rd_inq_data;
  1662. }
  1663. j = atp_readb_io(dev, c, 0x17);
  1664. if (j == 0x16) {
  1665. goto inq_ok;
  1666. }
  1667. atp_writeb_io(dev, c, 0x10, 0x46);
  1668. atp_writeb_io(dev, c, 0x12, 0);
  1669. atp_writeb_io(dev, c, 0x13, 0);
  1670. atp_writeb_io(dev, c, 0x14, 0);
  1671. atp_writeb_io(dev, c, 0x18, 0x08);
  1672. while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
  1673. cpu_relax();
  1674. if (atp_readb_io(dev, c, 0x17) != 0x16)
  1675. goto sel_ok;
  1676. inq_ok:
  1677. mbuf[36] = 0;
  1678. printk(KERN_INFO " ID: %2d %s\n", i, &mbuf[8]);
  1679. dev->id[c][i].devtype = mbuf[0];
  1680. rmb = mbuf[1];
  1681. n = mbuf[7];
  1682. if (!wide_chip)
  1683. goto not_wide;
  1684. if ((mbuf[7] & 0x60) == 0) {
  1685. goto not_wide;
  1686. }
  1687. if (is885(dev) || is880(dev)) {
  1688. if ((i < 8) && ((dev->global_map[c] & 0x20) == 0))
  1689. goto not_wide;
  1690. } else { /* result of is870() merge - is this a bug? */
  1691. if ((dev->global_map[c] & 0x20) == 0)
  1692. goto not_wide;
  1693. }
  1694. if (lvdmode == 0) {
  1695. goto chg_wide;
  1696. }
  1697. if (dev->sp[c][i] != 0x04) // force u2
  1698. {
  1699. goto chg_wide;
  1700. }
  1701. atp_writeb_io(dev, c, 0x1b, 0x01);
  1702. atp_writeb_io(dev, c, 3, satn[0]);
  1703. atp_writeb_io(dev, c, 4, satn[1]);
  1704. atp_writeb_io(dev, c, 5, satn[2]);
  1705. atp_writeb_io(dev, c, 6, satn[3]);
  1706. atp_writeb_io(dev, c, 7, satn[4]);
  1707. atp_writeb_io(dev, c, 8, satn[5]);
  1708. atp_writeb_io(dev, c, 0x0f, 0);
  1709. atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
  1710. atp_writeb_io(dev, c, 0x12, 0);
  1711. atp_writeb_io(dev, c, 0x13, satn[6]);
  1712. atp_writeb_io(dev, c, 0x14, satn[7]);
  1713. atp_writeb_io(dev, c, 0x18, satn[8]);
  1714. while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
  1715. cpu_relax();
  1716. if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
  1717. continue;
  1718. while (atp_readb_io(dev, c, 0x17) != 0x8e)
  1719. cpu_relax();
  1720. try_u3:
  1721. j = 0;
  1722. atp_writeb_io(dev, c, 0x14, 0x09);
  1723. atp_writeb_io(dev, c, 0x18, 0x20);
  1724. while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
  1725. if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
  1726. atp_writeb_io(dev, c, 0x19, u3[j++]);
  1727. cpu_relax();
  1728. }
  1729. while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
  1730. cpu_relax();
  1731. j = atp_readb_io(dev, c, 0x17) & 0x0f;
  1732. if (j == 0x0f) {
  1733. goto u3p_in;
  1734. }
  1735. if (j == 0x0a) {
  1736. goto u3p_cmd;
  1737. }
  1738. if (j == 0x0e) {
  1739. goto try_u3;
  1740. }
  1741. continue;
  1742. u3p_out:
  1743. atp_writeb_io(dev, c, 0x18, 0x20);
  1744. while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
  1745. if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
  1746. atp_writeb_io(dev, c, 0x19, 0);
  1747. cpu_relax();
  1748. }
  1749. j = atp_readb_io(dev, c, 0x17) & 0x0f;
  1750. if (j == 0x0f) {
  1751. goto u3p_in;
  1752. }
  1753. if (j == 0x0a) {
  1754. goto u3p_cmd;
  1755. }
  1756. if (j == 0x0e) {
  1757. goto u3p_out;
  1758. }
  1759. continue;
  1760. u3p_in:
  1761. atp_writeb_io(dev, c, 0x14, 0x09);
  1762. atp_writeb_io(dev, c, 0x18, 0x20);
  1763. k = 0;
  1764. u3p_in1:
  1765. j = atp_readb_io(dev, c, 0x1f);
  1766. if ((j & 0x01) != 0) {
  1767. mbuf[k++] = atp_readb_io(dev, c, 0x19);
  1768. goto u3p_in1;
  1769. }
  1770. if ((j & 0x80) == 0x00) {
  1771. goto u3p_in1;
  1772. }
  1773. j = atp_readb_io(dev, c, 0x17) & 0x0f;
  1774. if (j == 0x0f) {
  1775. goto u3p_in;
  1776. }
  1777. if (j == 0x0a) {
  1778. goto u3p_cmd;
  1779. }
  1780. if (j == 0x0e) {
  1781. goto u3p_out;
  1782. }
  1783. continue;
  1784. u3p_cmd:
  1785. atp_writeb_io(dev, c, 0x10, 0x30);
  1786. atp_writeb_io(dev, c, 0x14, 0x00);
  1787. atp_writeb_io(dev, c, 0x18, 0x08);
  1788. while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00);
  1789. j = atp_readb_io(dev, c, 0x17);
  1790. if (j != 0x16) {
  1791. if (j == 0x4e) {
  1792. goto u3p_out;
  1793. }
  1794. continue;
  1795. }
  1796. if (mbuf[0] != 0x01) {
  1797. goto chg_wide;
  1798. }
  1799. if (mbuf[1] != 0x06) {
  1800. goto chg_wide;
  1801. }
  1802. if (mbuf[2] != 0x04) {
  1803. goto chg_wide;
  1804. }
  1805. if (mbuf[3] == 0x09) {
  1806. m = 1;
  1807. m = m << i;
  1808. dev->wide_id[c] |= m;
  1809. dev->id[c][i].devsp = 0xce;
  1810. #ifdef ED_DBGP
  1811. printk("dev->id[%2d][%2d].devsp = %2x\n",c,i,dev->id[c][i].devsp);
  1812. #endif
  1813. continue;
  1814. }
  1815. chg_wide:
  1816. atp_writeb_io(dev, c, 0x1b, 0x01);
  1817. atp_writeb_io(dev, c, 3, satn[0]);
  1818. atp_writeb_io(dev, c, 4, satn[1]);
  1819. atp_writeb_io(dev, c, 5, satn[2]);
  1820. atp_writeb_io(dev, c, 6, satn[3]);
  1821. atp_writeb_io(dev, c, 7, satn[4]);
  1822. atp_writeb_io(dev, c, 8, satn[5]);
  1823. atp_writeb_io(dev, c, 0x0f, 0);
  1824. atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
  1825. atp_writeb_io(dev, c, 0x12, 0);
  1826. atp_writeb_io(dev, c, 0x13, satn[6]);
  1827. atp_writeb_io(dev, c, 0x14, satn[7]);
  1828. atp_writeb_io(dev, c, 0x18, satn[8]);
  1829. while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
  1830. cpu_relax();
  1831. if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
  1832. continue;
  1833. while (atp_readb_io(dev, c, 0x17) != 0x8e)
  1834. cpu_relax();
  1835. try_wide:
  1836. j = 0;
  1837. atp_writeb_io(dev, c, 0x14, 0x05);
  1838. atp_writeb_io(dev, c, 0x18, 0x20);
  1839. while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
  1840. if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
  1841. atp_writeb_io(dev, c, 0x19, wide[j++]);
  1842. cpu_relax();
  1843. }
  1844. while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
  1845. cpu_relax();
  1846. j = atp_readb_io(dev, c, 0x17) & 0x0f;
  1847. if (j == 0x0f) {
  1848. goto widep_in;
  1849. }
  1850. if (j == 0x0a) {
  1851. goto widep_cmd;
  1852. }
  1853. if (j == 0x0e) {
  1854. goto try_wide;
  1855. }
  1856. continue;
  1857. widep_out:
  1858. atp_writeb_io(dev, c, 0x18, 0x20);
  1859. while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
  1860. if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
  1861. atp_writeb_io(dev, c, 0x19, 0);
  1862. cpu_relax();
  1863. }
  1864. j = atp_readb_io(dev, c, 0x17) & 0x0f;
  1865. if (j == 0x0f) {
  1866. goto widep_in;
  1867. }
  1868. if (j == 0x0a) {
  1869. goto widep_cmd;
  1870. }
  1871. if (j == 0x0e) {
  1872. goto widep_out;
  1873. }
  1874. continue;
  1875. widep_in:
  1876. atp_writeb_io(dev, c, 0x14, 0xff);
  1877. atp_writeb_io(dev, c, 0x18, 0x20);
  1878. k = 0;
  1879. widep_in1:
  1880. j = atp_readb_io(dev, c, 0x1f);
  1881. if ((j & 0x01) != 0) {
  1882. mbuf[k++] = atp_readb_io(dev, c, 0x19);
  1883. goto widep_in1;
  1884. }
  1885. if ((j & 0x80) == 0x00) {
  1886. goto widep_in1;
  1887. }
  1888. j = atp_readb_io(dev, c, 0x17) & 0x0f;
  1889. if (j == 0x0f) {
  1890. goto widep_in;
  1891. }
  1892. if (j == 0x0a) {
  1893. goto widep_cmd;
  1894. }
  1895. if (j == 0x0e) {
  1896. goto widep_out;
  1897. }
  1898. continue;
  1899. widep_cmd:
  1900. atp_writeb_io(dev, c, 0x10, 0x30);
  1901. atp_writeb_io(dev, c, 0x14, 0x00);
  1902. atp_writeb_io(dev, c, 0x18, 0x08);
  1903. while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
  1904. cpu_relax();
  1905. j = atp_readb_io(dev, c, 0x17);
  1906. if (j != 0x16) {
  1907. if (j == 0x4e) {
  1908. goto widep_out;
  1909. }
  1910. continue;
  1911. }
  1912. if (mbuf[0] != 0x01) {
  1913. goto not_wide;
  1914. }
  1915. if (mbuf[1] != 0x02) {
  1916. goto not_wide;
  1917. }
  1918. if (mbuf[2] != 0x03) {
  1919. goto not_wide;
  1920. }
  1921. if (mbuf[3] != 0x01) {
  1922. goto not_wide;
  1923. }
  1924. m = 1;
  1925. m = m << i;
  1926. dev->wide_id[c] |= m;
  1927. not_wide:
  1928. if ((dev->id[c][i].devtype == 0x00) || (dev->id[c][i].devtype == 0x07) || ((dev->id[c][i].devtype == 0x05) && ((n & 0x10) != 0))) {
  1929. m = 1;
  1930. m = m << i;
  1931. if ((dev->async[c] & m) != 0) {
  1932. goto set_sync;
  1933. }
  1934. }
  1935. continue;
  1936. set_sync:
  1937. if ((!is885(dev) && !is880(dev)) || (dev->sp[c][i] == 0x02)) {
  1938. synu[4] = 0x0c;
  1939. synuw[4] = 0x0c;
  1940. } else {
  1941. if (dev->sp[c][i] >= 0x03) {
  1942. synu[4] = 0x0a;
  1943. synuw[4] = 0x0a;
  1944. }
  1945. }
  1946. j = 0;
  1947. if ((m & dev->wide_id[c]) != 0) {
  1948. j |= 0x01;
  1949. }
  1950. atp_writeb_io(dev, c, 0x1b, j);
  1951. atp_writeb_io(dev, c, 3, satn[0]);
  1952. atp_writeb_io(dev, c, 4, satn[1]);
  1953. atp_writeb_io(dev, c, 5, satn[2]);
  1954. atp_writeb_io(dev, c, 6, satn[3]);
  1955. atp_writeb_io(dev, c, 7, satn[4]);
  1956. atp_writeb_io(dev, c, 8, satn[5]);
  1957. atp_writeb_io(dev, c, 0x0f, 0);
  1958. atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
  1959. atp_writeb_io(dev, c, 0x12, 0);
  1960. atp_writeb_io(dev, c, 0x13, satn[6]);
  1961. atp_writeb_io(dev, c, 0x14, satn[7]);
  1962. atp_writeb_io(dev, c, 0x18, satn[8]);
  1963. while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
  1964. cpu_relax();
  1965. if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
  1966. continue;
  1967. while (atp_readb_io(dev, c, 0x17) != 0x8e)
  1968. cpu_relax();
  1969. try_sync:
  1970. j = 0;
  1971. atp_writeb_io(dev, c, 0x14, 0x06);
  1972. atp_writeb_io(dev, c, 0x18, 0x20);
  1973. while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
  1974. if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0) {
  1975. if ((m & dev->wide_id[c]) != 0) {
  1976. if (is885(dev) || is880(dev)) {
  1977. if ((m & dev->ultra_map[c]) != 0) {
  1978. atp_writeb_io(dev, c, 0x19, synuw[j++]);
  1979. } else {
  1980. atp_writeb_io(dev, c, 0x19, synw[j++]);
  1981. }
  1982. } else
  1983. atp_writeb_io(dev, c, 0x19, synw_870[j++]);
  1984. } else {
  1985. if ((m & dev->ultra_map[c]) != 0) {
  1986. atp_writeb_io(dev, c, 0x19, synu[j++]);
  1987. } else {
  1988. atp_writeb_io(dev, c, 0x19, synn[j++]);
  1989. }
  1990. }
  1991. }
  1992. }
  1993. while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
  1994. cpu_relax();
  1995. j = atp_readb_io(dev, c, 0x17) & 0x0f;
  1996. if (j == 0x0f) {
  1997. goto phase_ins;
  1998. }
  1999. if (j == 0x0a) {
  2000. goto phase_cmds;
  2001. }
  2002. if (j == 0x0e) {
  2003. goto try_sync;
  2004. }
  2005. continue;
  2006. phase_outs:
  2007. atp_writeb_io(dev, c, 0x18, 0x20);
  2008. while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00) {
  2009. if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0x00)
  2010. atp_writeb_io(dev, c, 0x19, 0x00);
  2011. cpu_relax();
  2012. }
  2013. j = atp_readb_io(dev, c, 0x17);
  2014. if (j == 0x85) {
  2015. goto tar_dcons;
  2016. }
  2017. j &= 0x0f;
  2018. if (j == 0x0f) {
  2019. goto phase_ins;
  2020. }
  2021. if (j == 0x0a) {
  2022. goto phase_cmds;
  2023. }
  2024. if (j == 0x0e) {
  2025. goto phase_outs;
  2026. }
  2027. continue;
  2028. phase_ins:
  2029. if (is885(dev) || is880(dev))
  2030. atp_writeb_io(dev, c, 0x14, 0x06);
  2031. else
  2032. atp_writeb_io(dev, c, 0x14, 0xff);
  2033. atp_writeb_io(dev, c, 0x18, 0x20);
  2034. k = 0;
  2035. phase_ins1:
  2036. j = atp_readb_io(dev, c, 0x1f);
  2037. if ((j & 0x01) != 0x00) {
  2038. mbuf[k++] = atp_readb_io(dev, c, 0x19);
  2039. goto phase_ins1;
  2040. }
  2041. if ((j & 0x80) == 0x00) {
  2042. goto phase_ins1;
  2043. }
  2044. while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00);
  2045. j = atp_readb_io(dev, c, 0x17);
  2046. if (j == 0x85) {
  2047. goto tar_dcons;
  2048. }
  2049. j &= 0x0f;
  2050. if (j == 0x0f) {
  2051. goto phase_ins;
  2052. }
  2053. if (j == 0x0a) {
  2054. goto phase_cmds;
  2055. }
  2056. if (j == 0x0e) {
  2057. goto phase_outs;
  2058. }
  2059. continue;
  2060. phase_cmds:
  2061. atp_writeb_io(dev, c, 0x10, 0x30);
  2062. tar_dcons:
  2063. atp_writeb_io(dev, c, 0x14, 0x00);
  2064. atp_writeb_io(dev, c, 0x18, 0x08);
  2065. while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
  2066. cpu_relax();
  2067. j = atp_readb_io(dev, c, 0x17);
  2068. if (j != 0x16) {
  2069. continue;
  2070. }
  2071. if (mbuf[0] != 0x01) {
  2072. continue;
  2073. }
  2074. if (mbuf[1] != 0x03) {
  2075. continue;
  2076. }
  2077. if (mbuf[4] == 0x00) {
  2078. continue;
  2079. }
  2080. if (mbuf[3] > 0x64) {
  2081. continue;
  2082. }
  2083. if (is885(dev) || is880(dev)) {
  2084. if (mbuf[4] > 0x0e) {
  2085. mbuf[4] = 0x0e;
  2086. }
  2087. } else {
  2088. if (mbuf[4] > 0x0c) {
  2089. mbuf[4] = 0x0c;
  2090. }
  2091. }
  2092. dev->id[c][i].devsp = mbuf[4];
  2093. if (is885(dev) || is880(dev))
  2094. if (mbuf[3] < 0x0c) {
  2095. j = 0xb0;
  2096. goto set_syn_ok;
  2097. }
  2098. if ((mbuf[3] < 0x0d) && (rmb == 0)) {
  2099. j = 0xa0;
  2100. goto set_syn_ok;
  2101. }
  2102. if (mbuf[3] < 0x1a) {
  2103. j = 0x20;
  2104. goto set_syn_ok;
  2105. }
  2106. if (mbuf[3] < 0x33) {
  2107. j = 0x40;
  2108. goto set_syn_ok;
  2109. }
  2110. if (mbuf[3] < 0x4c) {
  2111. j = 0x50;
  2112. goto set_syn_ok;
  2113. }
  2114. j = 0x60;
  2115. set_syn_ok:
  2116. dev->id[c][i].devsp = (dev->id[c][i].devsp & 0x0f) | j;
  2117. #ifdef ED_DBGP
  2118. printk("dev->id[%2d][%2d].devsp = %2x\n",c,i,dev->id[c][i].devsp);
  2119. #endif
  2120. }
  2121. }