initio.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996
  1. /**************************************************************************
  2. * Initio 9100 device driver for Linux.
  3. *
  4. * Copyright (c) 1994-1998 Initio Corporation
  5. * Copyright (c) 1998 Bas Vermeulen <bvermeul@blackstar.xs4all.nl>
  6. * Copyright (c) 2004 Christoph Hellwig <hch@lst.de>
  7. * Copyright (c) 2007 Red Hat
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2, or (at your option)
  12. * any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; see the file COPYING. If not, write to
  21. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. *
  24. *************************************************************************
  25. *
  26. * DESCRIPTION:
  27. *
  28. * This is the Linux low-level SCSI driver for Initio INI-9X00U/UW SCSI host
  29. * adapters
  30. *
  31. * 08/06/97 hc - v1.01h
  32. * - Support inic-940 and inic-935
  33. * 09/26/97 hc - v1.01i
  34. * - Make correction from J.W. Schultz suggestion
  35. * 10/13/97 hc - Support reset function
  36. * 10/21/97 hc - v1.01j
  37. * - Support 32 LUN (SCSI 3)
  38. * 01/14/98 hc - v1.01k
  39. * - Fix memory allocation problem
  40. * 03/04/98 hc - v1.01l
  41. * - Fix tape rewind which will hang the system problem
  42. * - Set can_queue to initio_num_scb
  43. * 06/25/98 hc - v1.01m
  44. * - Get it work for kernel version >= 2.1.75
  45. * - Dynamic assign SCSI bus reset holding time in initio_init()
  46. * 07/02/98 hc - v1.01n
  47. * - Support 0002134A
  48. * 08/07/98 hc - v1.01o
  49. * - Change the initio_abort_srb routine to use scsi_done. <01>
  50. * 09/07/98 hl - v1.02
  51. * - Change the INI9100U define and proc_dir_entry to
  52. * reflect the newer Kernel 2.1.118, but the v1.o1o
  53. * should work with Kernel 2.1.118.
  54. * 09/20/98 wh - v1.02a
  55. * - Support Abort command.
  56. * - Handle reset routine.
  57. * 09/21/98 hl - v1.03
  58. * - remove comments.
  59. * 12/09/98 bv - v1.03a
  60. * - Removed unused code
  61. * 12/13/98 bv - v1.03b
  62. * - Remove cli() locking for kernels >= 2.1.95. This uses
  63. * spinlocks to serialize access to the pSRB_head and
  64. * pSRB_tail members of the HCS structure.
  65. * 09/01/99 bv - v1.03d
  66. * - Fixed a deadlock problem in SMP.
  67. * 21/01/99 bv - v1.03e
  68. * - Add support for the Domex 3192U PCI SCSI
  69. * This is a slightly modified patch by
  70. * Brian Macy <bmacy@sunshinecomputing.com>
  71. * 22/02/99 bv - v1.03f
  72. * - Didn't detect the INIC-950 in 2.0.x correctly.
  73. * Now fixed.
  74. * 05/07/99 bv - v1.03g
  75. * - Changed the assumption that HZ = 100
  76. * 10/17/03 mc - v1.04
  77. * - added new DMA API support
  78. * 06/01/04 jmd - v1.04a
  79. * - Re-add reset_bus support
  80. **************************************************************************/
  81. #include <linux/module.h>
  82. #include <linux/errno.h>
  83. #include <linux/delay.h>
  84. #include <linux/pci.h>
  85. #include <linux/init.h>
  86. #include <linux/blkdev.h>
  87. #include <linux/spinlock.h>
  88. #include <linux/stat.h>
  89. #include <linux/kernel.h>
  90. #include <linux/proc_fs.h>
  91. #include <linux/string.h>
  92. #include <linux/interrupt.h>
  93. #include <linux/ioport.h>
  94. #include <linux/slab.h>
  95. #include <linux/jiffies.h>
  96. #include <linux/dma-mapping.h>
  97. #include <asm/io.h>
  98. #include <scsi/scsi.h>
  99. #include <scsi/scsi_cmnd.h>
  100. #include <scsi/scsi_device.h>
  101. #include <scsi/scsi_host.h>
  102. #include <scsi/scsi_tcq.h>
  103. #include "initio.h"
  104. #define SENSE_SIZE 14
  105. #define i91u_MAXQUEUE 2
  106. #define i91u_REVID "Initio INI-9X00U/UW SCSI device driver; Revision: 1.04a"
  107. #ifdef DEBUG_i91u
  108. static unsigned int i91u_debug = DEBUG_DEFAULT;
  109. #endif
  110. static int initio_tag_enable = 1;
  111. #ifdef DEBUG_i91u
  112. static int setup_debug = 0;
  113. #endif
  114. static void i91uSCBPost(u8 * pHcb, u8 * pScb);
  115. #define DEBUG_INTERRUPT 0
  116. #define DEBUG_QUEUE 0
  117. #define DEBUG_STATE 0
  118. #define INT_DISC 0
  119. /*--- forward references ---*/
  120. static struct scsi_ctrl_blk *initio_find_busy_scb(struct initio_host * host, u16 tarlun);
  121. static struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host);
  122. static int tulip_main(struct initio_host * host);
  123. static int initio_next_state(struct initio_host * host);
  124. static int initio_state_1(struct initio_host * host);
  125. static int initio_state_2(struct initio_host * host);
  126. static int initio_state_3(struct initio_host * host);
  127. static int initio_state_4(struct initio_host * host);
  128. static int initio_state_5(struct initio_host * host);
  129. static int initio_state_6(struct initio_host * host);
  130. static int initio_state_7(struct initio_host * host);
  131. static int initio_xfer_data_in(struct initio_host * host);
  132. static int initio_xfer_data_out(struct initio_host * host);
  133. static int initio_xpad_in(struct initio_host * host);
  134. static int initio_xpad_out(struct initio_host * host);
  135. static int initio_status_msg(struct initio_host * host);
  136. static int initio_msgin(struct initio_host * host);
  137. static int initio_msgin_sync(struct initio_host * host);
  138. static int initio_msgin_accept(struct initio_host * host);
  139. static int initio_msgout_reject(struct initio_host * host);
  140. static int initio_msgin_extend(struct initio_host * host);
  141. static int initio_msgout_ide(struct initio_host * host);
  142. static int initio_msgout_abort_targ(struct initio_host * host);
  143. static int initio_msgout_abort_tag(struct initio_host * host);
  144. static int initio_bus_device_reset(struct initio_host * host);
  145. static void initio_select_atn(struct initio_host * host, struct scsi_ctrl_blk * scb);
  146. static void initio_select_atn3(struct initio_host * host, struct scsi_ctrl_blk * scb);
  147. static void initio_select_atn_stop(struct initio_host * host, struct scsi_ctrl_blk * scb);
  148. static int int_initio_busfree(struct initio_host * host);
  149. static int int_initio_scsi_rst(struct initio_host * host);
  150. static int int_initio_bad_seq(struct initio_host * host);
  151. static int int_initio_resel(struct initio_host * host);
  152. static int initio_sync_done(struct initio_host * host);
  153. static int wdtr_done(struct initio_host * host);
  154. static int wait_tulip(struct initio_host * host);
  155. static int initio_wait_done_disc(struct initio_host * host);
  156. static int initio_wait_disc(struct initio_host * host);
  157. static void tulip_scsi(struct initio_host * host);
  158. static int initio_post_scsi_rst(struct initio_host * host);
  159. static void initio_se2_ew_en(unsigned long base);
  160. static void initio_se2_ew_ds(unsigned long base);
  161. static int initio_se2_rd_all(unsigned long base);
  162. static void initio_se2_update_all(unsigned long base); /* setup default pattern */
  163. static void initio_read_eeprom(unsigned long base);
  164. /* ---- INTERNAL VARIABLES ---- */
  165. static NVRAM i91unvram;
  166. static NVRAM *i91unvramp;
  167. static u8 i91udftNvRam[64] =
  168. {
  169. /*----------- header -----------*/
  170. 0x25, 0xc9, /* Signature */
  171. 0x40, /* Size */
  172. 0x01, /* Revision */
  173. /* -- Host Adapter Structure -- */
  174. 0x95, /* ModelByte0 */
  175. 0x00, /* ModelByte1 */
  176. 0x00, /* ModelInfo */
  177. 0x01, /* NumOfCh */
  178. NBC1_DEFAULT, /* BIOSConfig1 */
  179. 0, /* BIOSConfig2 */
  180. 0, /* HAConfig1 */
  181. 0, /* HAConfig2 */
  182. /* SCSI channel 0 and target Structure */
  183. 7, /* SCSIid */
  184. NCC1_DEFAULT, /* SCSIconfig1 */
  185. 0, /* SCSIconfig2 */
  186. 0x10, /* NumSCSItarget */
  187. NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
  188. NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
  189. NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
  190. NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
  191. /* SCSI channel 1 and target Structure */
  192. 7, /* SCSIid */
  193. NCC1_DEFAULT, /* SCSIconfig1 */
  194. 0, /* SCSIconfig2 */
  195. 0x10, /* NumSCSItarget */
  196. NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
  197. NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
  198. NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
  199. NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT, NTC_DEFAULT,
  200. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  201. 0, 0}; /* - CheckSum - */
  202. static u8 initio_rate_tbl[8] = /* fast 20 */
  203. {
  204. /* nanosecond divide by 4 */
  205. 12, /* 50ns, 20M */
  206. 18, /* 75ns, 13.3M */
  207. 25, /* 100ns, 10M */
  208. 31, /* 125ns, 8M */
  209. 37, /* 150ns, 6.6M */
  210. 43, /* 175ns, 5.7M */
  211. 50, /* 200ns, 5M */
  212. 62 /* 250ns, 4M */
  213. };
  214. static void initio_do_pause(unsigned amount)
  215. {
  216. /* Pause for amount jiffies */
  217. unsigned long the_time = jiffies + amount;
  218. while (time_before_eq(jiffies, the_time))
  219. cpu_relax();
  220. }
  221. /*-- forward reference --*/
  222. /******************************************************************
  223. Input: instruction for Serial E2PROM
  224. EX: se2_rd(0 call se2_instr() to send address and read command
  225. StartBit OP_Code Address Data
  226. --------- -------- ------------------ -------
  227. 1 1 , 0 A5,A4,A3,A2,A1,A0 D15-D0
  228. +-----------------------------------------------------
  229. |
  230. CS -----+
  231. +--+ +--+ +--+ +--+ +--+
  232. ^ | ^ | ^ | ^ | ^ |
  233. | | | | | | | | | |
  234. CLK -------+ +--+ +--+ +--+ +--+ +--
  235. (leading edge trigger)
  236. +--1-----1--+
  237. | SB OP | OP A5 A4
  238. DI ----+ +--0------------------
  239. (address and cmd sent to nvram)
  240. -------------------------------------------+
  241. |
  242. DO +---
  243. (data sent from nvram)
  244. ******************************************************************/
  245. /**
  246. * initio_se2_instr - bitbang an instruction
  247. * @base: Base of InitIO controller
  248. * @instr: Instruction for serial E2PROM
  249. *
  250. * Bitbang an instruction out to the serial E2Prom
  251. */
  252. static void initio_se2_instr(unsigned long base, u8 instr)
  253. {
  254. int i;
  255. u8 b;
  256. outb(SE2CS | SE2DO, base + TUL_NVRAM); /* cs+start bit */
  257. udelay(30);
  258. outb(SE2CS | SE2CLK | SE2DO, base + TUL_NVRAM); /* +CLK */
  259. udelay(30);
  260. for (i = 0; i < 8; i++) {
  261. if (instr & 0x80)
  262. b = SE2CS | SE2DO; /* -CLK+dataBit */
  263. else
  264. b = SE2CS; /* -CLK */
  265. outb(b, base + TUL_NVRAM);
  266. udelay(30);
  267. outb(b | SE2CLK, base + TUL_NVRAM); /* +CLK */
  268. udelay(30);
  269. instr <<= 1;
  270. }
  271. outb(SE2CS, base + TUL_NVRAM); /* -CLK */
  272. udelay(30);
  273. }
  274. /**
  275. * initio_se2_ew_en - Enable erase/write
  276. * @base: Base address of InitIO controller
  277. *
  278. * Enable erase/write state of serial EEPROM
  279. */
  280. void initio_se2_ew_en(unsigned long base)
  281. {
  282. initio_se2_instr(base, 0x30); /* EWEN */
  283. outb(0, base + TUL_NVRAM); /* -CS */
  284. udelay(30);
  285. }
  286. /**
  287. * initio_se2_ew_ds - Disable erase/write
  288. * @base: Base address of InitIO controller
  289. *
  290. * Disable erase/write state of serial EEPROM
  291. */
  292. void initio_se2_ew_ds(unsigned long base)
  293. {
  294. initio_se2_instr(base, 0); /* EWDS */
  295. outb(0, base + TUL_NVRAM); /* -CS */
  296. udelay(30);
  297. }
  298. /**
  299. * initio_se2_rd - read E2PROM word
  300. * @base: Base of InitIO controller
  301. * @addr: Address of word in E2PROM
  302. *
  303. * Read a word from the NV E2PROM device
  304. */
  305. static u16 initio_se2_rd(unsigned long base, u8 addr)
  306. {
  307. u8 instr, rb;
  308. u16 val = 0;
  309. int i;
  310. instr = (u8) (addr | 0x80);
  311. initio_se2_instr(base, instr); /* READ INSTR */
  312. for (i = 15; i >= 0; i--) {
  313. outb(SE2CS | SE2CLK, base + TUL_NVRAM); /* +CLK */
  314. udelay(30);
  315. outb(SE2CS, base + TUL_NVRAM); /* -CLK */
  316. /* sample data after the following edge of clock */
  317. rb = inb(base + TUL_NVRAM);
  318. rb &= SE2DI;
  319. val += (rb << i);
  320. udelay(30); /* 6/20/95 */
  321. }
  322. outb(0, base + TUL_NVRAM); /* no chip select */
  323. udelay(30);
  324. return val;
  325. }
  326. /**
  327. * initio_se2_wr - read E2PROM word
  328. * @base: Base of InitIO controller
  329. * @addr: Address of word in E2PROM
  330. * @val: Value to write
  331. *
  332. * Write a word to the NV E2PROM device. Used when recovering from
  333. * a problem with the NV.
  334. */
  335. static void initio_se2_wr(unsigned long base, u8 addr, u16 val)
  336. {
  337. u8 rb;
  338. u8 instr;
  339. int i;
  340. instr = (u8) (addr | 0x40);
  341. initio_se2_instr(base, instr); /* WRITE INSTR */
  342. for (i = 15; i >= 0; i--) {
  343. if (val & 0x8000)
  344. outb(SE2CS | SE2DO, base + TUL_NVRAM); /* -CLK+dataBit 1 */
  345. else
  346. outb(SE2CS, base + TUL_NVRAM); /* -CLK+dataBit 0 */
  347. udelay(30);
  348. outb(SE2CS | SE2CLK, base + TUL_NVRAM); /* +CLK */
  349. udelay(30);
  350. val <<= 1;
  351. }
  352. outb(SE2CS, base + TUL_NVRAM); /* -CLK */
  353. udelay(30);
  354. outb(0, base + TUL_NVRAM); /* -CS */
  355. udelay(30);
  356. outb(SE2CS, base + TUL_NVRAM); /* +CS */
  357. udelay(30);
  358. for (;;) {
  359. outb(SE2CS | SE2CLK, base + TUL_NVRAM); /* +CLK */
  360. udelay(30);
  361. outb(SE2CS, base + TUL_NVRAM); /* -CLK */
  362. udelay(30);
  363. if ((rb = inb(base + TUL_NVRAM)) & SE2DI)
  364. break; /* write complete */
  365. }
  366. outb(0, base + TUL_NVRAM); /* -CS */
  367. }
  368. /**
  369. * initio_se2_rd_all - read hostadapter NV configuration
  370. * @base: Base address of InitIO controller
  371. *
  372. * Reads the E2PROM data into main memory. Ensures that the checksum
  373. * and header marker are valid. Returns 1 on success -1 on error.
  374. */
  375. static int initio_se2_rd_all(unsigned long base)
  376. {
  377. int i;
  378. u16 chksum = 0;
  379. u16 *np;
  380. i91unvramp = &i91unvram;
  381. np = (u16 *) i91unvramp;
  382. for (i = 0; i < 32; i++)
  383. *np++ = initio_se2_rd(base, i);
  384. /* Is signature "ini" ok ? */
  385. if (i91unvramp->NVM_Signature != INI_SIGNATURE)
  386. return -1;
  387. /* Is ckecksum ok ? */
  388. np = (u16 *) i91unvramp;
  389. for (i = 0; i < 31; i++)
  390. chksum += *np++;
  391. if (i91unvramp->NVM_CheckSum != chksum)
  392. return -1;
  393. return 1;
  394. }
  395. /**
  396. * initio_se2_update_all - Update E2PROM
  397. * @base: Base of InitIO controller
  398. *
  399. * Update the E2PROM by wrting any changes into the E2PROM
  400. * chip, rewriting the checksum.
  401. */
  402. static void initio_se2_update_all(unsigned long base)
  403. { /* setup default pattern */
  404. int i;
  405. u16 chksum = 0;
  406. u16 *np, *np1;
  407. i91unvramp = &i91unvram;
  408. /* Calculate checksum first */
  409. np = (u16 *) i91udftNvRam;
  410. for (i = 0; i < 31; i++)
  411. chksum += *np++;
  412. *np = chksum;
  413. initio_se2_ew_en(base); /* Enable write */
  414. np = (u16 *) i91udftNvRam;
  415. np1 = (u16 *) i91unvramp;
  416. for (i = 0; i < 32; i++, np++, np1++) {
  417. if (*np != *np1)
  418. initio_se2_wr(base, i, *np);
  419. }
  420. initio_se2_ew_ds(base); /* Disable write */
  421. }
  422. /**
  423. * initio_read_eeprom - Retrieve configuration
  424. * @base: Base of InitIO Host Adapter
  425. *
  426. * Retrieve the host adapter configuration data from E2Prom. If the
  427. * data is invalid then the defaults are used and are also restored
  428. * into the E2PROM. This forms the access point for the SCSI driver
  429. * into the E2PROM layer, the other functions for the E2PROM are all
  430. * internal use.
  431. *
  432. * Must be called single threaded, uses a shared global area.
  433. */
  434. static void initio_read_eeprom(unsigned long base)
  435. {
  436. u8 gctrl;
  437. i91unvramp = &i91unvram;
  438. /* Enable EEProm programming */
  439. gctrl = inb(base + TUL_GCTRL);
  440. outb(gctrl | TUL_GCTRL_EEPROM_BIT, base + TUL_GCTRL);
  441. if (initio_se2_rd_all(base) != 1) {
  442. initio_se2_update_all(base); /* setup default pattern */
  443. initio_se2_rd_all(base); /* load again */
  444. }
  445. /* Disable EEProm programming */
  446. gctrl = inb(base + TUL_GCTRL);
  447. outb(gctrl & ~TUL_GCTRL_EEPROM_BIT, base + TUL_GCTRL);
  448. }
  449. /**
  450. * initio_stop_bm - stop bus master
  451. * @host: InitIO we are stopping
  452. *
  453. * Stop any pending DMA operation, aborting the DMA if necessary
  454. */
  455. static void initio_stop_bm(struct initio_host * host)
  456. {
  457. if (inb(host->addr + TUL_XStatus) & XPEND) { /* if DMA xfer is pending, abort DMA xfer */
  458. outb(TAX_X_ABT | TAX_X_CLR_FIFO, host->addr + TUL_XCmd);
  459. /* wait Abort DMA xfer done */
  460. while ((inb(host->addr + TUL_Int) & XABT) == 0)
  461. cpu_relax();
  462. }
  463. outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
  464. }
  465. /**
  466. * initio_reset_scsi - Reset SCSI host controller
  467. * @host: InitIO host to reset
  468. * @seconds: Recovery time
  469. *
  470. * Perform a full reset of the SCSI subsystem.
  471. */
  472. static int initio_reset_scsi(struct initio_host * host, int seconds)
  473. {
  474. outb(TSC_RST_BUS, host->addr + TUL_SCtrl0);
  475. while (!((host->jsint = inb(host->addr + TUL_SInt)) & TSS_SCSIRST_INT))
  476. cpu_relax();
  477. /* reset tulip chip */
  478. outb(0, host->addr + TUL_SSignal);
  479. /* Stall for a while, wait for target's firmware ready,make it 2 sec ! */
  480. /* SONY 5200 tape drive won't work if only stall for 1 sec */
  481. /* FIXME: this is a very long busy wait right now */
  482. initio_do_pause(seconds * HZ);
  483. inb(host->addr + TUL_SInt);
  484. return SCSI_RESET_SUCCESS;
  485. }
  486. /**
  487. * initio_init - set up an InitIO host adapter
  488. * @host: InitIO host adapter
  489. * @num_scbs: Number of SCBS
  490. * @bios_addr: BIOS address
  491. *
  492. * Set up the host adapter and devices according to the configuration
  493. * retrieved from the E2PROM.
  494. *
  495. * Locking: Calls E2PROM layer code which is not re-enterable so must
  496. * run single threaded for now.
  497. */
  498. static void initio_init(struct initio_host * host, u8 *bios_addr)
  499. {
  500. int i;
  501. u8 *flags;
  502. u8 *heads;
  503. /* Get E2Prom configuration */
  504. initio_read_eeprom(host->addr);
  505. if (i91unvramp->NVM_SCSIInfo[0].NVM_NumOfTarg == 8)
  506. host->max_tar = 8;
  507. else
  508. host->max_tar = 16;
  509. host->config = i91unvramp->NVM_SCSIInfo[0].NVM_ChConfig1;
  510. host->scsi_id = i91unvramp->NVM_SCSIInfo[0].NVM_ChSCSIID;
  511. host->idmask = ~(1 << host->scsi_id);
  512. #ifdef CHK_PARITY
  513. /* Enable parity error response */
  514. outb(inb(host->addr + TUL_PCMD) | 0x40, host->addr + TUL_PCMD);
  515. #endif
  516. /* Mask all the interrupt */
  517. outb(0x1F, host->addr + TUL_Mask);
  518. initio_stop_bm(host);
  519. /* --- Initialize the tulip --- */
  520. outb(TSC_RST_CHIP, host->addr + TUL_SCtrl0);
  521. /* program HBA's SCSI ID */
  522. outb(host->scsi_id << 4, host->addr + TUL_SScsiId);
  523. /* Enable Initiator Mode ,phase latch,alternate sync period mode,
  524. disable SCSI reset */
  525. if (host->config & HCC_EN_PAR)
  526. host->sconf1 = (TSC_INITDEFAULT | TSC_EN_SCSI_PAR);
  527. else
  528. host->sconf1 = (TSC_INITDEFAULT);
  529. outb(host->sconf1, host->addr + TUL_SConfig);
  530. /* Enable HW reselect */
  531. outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
  532. outb(0, host->addr + TUL_SPeriod);
  533. /* selection time out = 250 ms */
  534. outb(153, host->addr + TUL_STimeOut);
  535. /* Enable SCSI terminator */
  536. outb((host->config & (HCC_ACT_TERM1 | HCC_ACT_TERM2)),
  537. host->addr + TUL_XCtrl);
  538. outb(((host->config & HCC_AUTO_TERM) >> 4) |
  539. (inb(host->addr + TUL_GCTRL1) & 0xFE),
  540. host->addr + TUL_GCTRL1);
  541. for (i = 0,
  542. flags = & (i91unvramp->NVM_SCSIInfo[0].NVM_Targ0Config),
  543. heads = bios_addr + 0x180;
  544. i < host->max_tar;
  545. i++, flags++) {
  546. host->targets[i].flags = *flags & ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
  547. if (host->targets[i].flags & TCF_EN_255)
  548. host->targets[i].drv_flags = TCF_DRV_255_63;
  549. else
  550. host->targets[i].drv_flags = 0;
  551. host->targets[i].js_period = 0;
  552. host->targets[i].sconfig0 = host->sconf1;
  553. host->targets[i].heads = *heads++;
  554. if (host->targets[i].heads == 255)
  555. host->targets[i].drv_flags = TCF_DRV_255_63;
  556. else
  557. host->targets[i].drv_flags = 0;
  558. host->targets[i].sectors = *heads++;
  559. host->targets[i].flags &= ~TCF_BUSY;
  560. host->act_tags[i] = 0;
  561. host->max_tags[i] = 0xFF;
  562. } /* for */
  563. printk("i91u: PCI Base=0x%04X, IRQ=%d, BIOS=0x%04X0, SCSI ID=%d\n",
  564. host->addr, host->pci_dev->irq,
  565. host->bios_addr, host->scsi_id);
  566. /* Reset SCSI Bus */
  567. if (host->config & HCC_SCSI_RESET) {
  568. printk(KERN_INFO "i91u: Reset SCSI Bus ... \n");
  569. initio_reset_scsi(host, 10);
  570. }
  571. outb(0x17, host->addr + TUL_SCFG1);
  572. outb(0xE9, host->addr + TUL_SIntEnable);
  573. }
  574. /**
  575. * initio_alloc_scb - Allocate an SCB
  576. * @host: InitIO host we are allocating for
  577. *
  578. * Walk the SCB list for the controller and allocate a free SCB if
  579. * one exists.
  580. */
  581. static struct scsi_ctrl_blk *initio_alloc_scb(struct initio_host *host)
  582. {
  583. struct scsi_ctrl_blk *scb;
  584. unsigned long flags;
  585. spin_lock_irqsave(&host->avail_lock, flags);
  586. if ((scb = host->first_avail) != NULL) {
  587. #if DEBUG_QUEUE
  588. printk("find scb at %p\n", scb);
  589. #endif
  590. if ((host->first_avail = scb->next) == NULL)
  591. host->last_avail = NULL;
  592. scb->next = NULL;
  593. scb->status = SCB_RENT;
  594. }
  595. spin_unlock_irqrestore(&host->avail_lock, flags);
  596. return scb;
  597. }
  598. /**
  599. * initio_release_scb - Release an SCB
  600. * @host: InitIO host that owns the SCB
  601. * @cmnd: SCB command block being returned
  602. *
  603. * Return an allocated SCB to the host free list
  604. */
  605. static void initio_release_scb(struct initio_host * host, struct scsi_ctrl_blk * cmnd)
  606. {
  607. unsigned long flags;
  608. #if DEBUG_QUEUE
  609. printk("Release SCB %p; ", cmnd);
  610. #endif
  611. spin_lock_irqsave(&(host->avail_lock), flags);
  612. cmnd->srb = NULL;
  613. cmnd->status = 0;
  614. cmnd->next = NULL;
  615. if (host->last_avail != NULL) {
  616. host->last_avail->next = cmnd;
  617. host->last_avail = cmnd;
  618. } else {
  619. host->first_avail = cmnd;
  620. host->last_avail = cmnd;
  621. }
  622. spin_unlock_irqrestore(&(host->avail_lock), flags);
  623. }
  624. /***************************************************************************/
  625. static void initio_append_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
  626. {
  627. #if DEBUG_QUEUE
  628. printk("Append pend SCB %p; ", scbp);
  629. #endif
  630. scbp->status = SCB_PEND;
  631. scbp->next = NULL;
  632. if (host->last_pending != NULL) {
  633. host->last_pending->next = scbp;
  634. host->last_pending = scbp;
  635. } else {
  636. host->first_pending = scbp;
  637. host->last_pending = scbp;
  638. }
  639. }
  640. /***************************************************************************/
  641. static void initio_push_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
  642. {
  643. #if DEBUG_QUEUE
  644. printk("Push pend SCB %p; ", scbp);
  645. #endif
  646. scbp->status = SCB_PEND;
  647. if ((scbp->next = host->first_pending) != NULL) {
  648. host->first_pending = scbp;
  649. } else {
  650. host->first_pending = scbp;
  651. host->last_pending = scbp;
  652. }
  653. }
  654. static struct scsi_ctrl_blk *initio_find_first_pend_scb(struct initio_host * host)
  655. {
  656. struct scsi_ctrl_blk *first;
  657. first = host->first_pending;
  658. while (first != NULL) {
  659. if (first->opcode != ExecSCSI)
  660. return first;
  661. if (first->tagmsg == 0) {
  662. if ((host->act_tags[first->target] == 0) &&
  663. !(host->targets[first->target].flags & TCF_BUSY))
  664. return first;
  665. } else {
  666. if ((host->act_tags[first->target] >=
  667. host->max_tags[first->target]) |
  668. (host->targets[first->target].flags & TCF_BUSY)) {
  669. first = first->next;
  670. continue;
  671. }
  672. return first;
  673. }
  674. first = first->next;
  675. }
  676. return first;
  677. }
  678. static void initio_unlink_pend_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
  679. {
  680. struct scsi_ctrl_blk *tmp, *prev;
  681. #if DEBUG_QUEUE
  682. printk("unlink pend SCB %p; ", scb);
  683. #endif
  684. prev = tmp = host->first_pending;
  685. while (tmp != NULL) {
  686. if (scb == tmp) { /* Unlink this SCB */
  687. if (tmp == host->first_pending) {
  688. if ((host->first_pending = tmp->next) == NULL)
  689. host->last_pending = NULL;
  690. } else {
  691. prev->next = tmp->next;
  692. if (tmp == host->last_pending)
  693. host->last_pending = prev;
  694. }
  695. tmp->next = NULL;
  696. break;
  697. }
  698. prev = tmp;
  699. tmp = tmp->next;
  700. }
  701. }
  702. static void initio_append_busy_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
  703. {
  704. #if DEBUG_QUEUE
  705. printk("append busy SCB %p; ", scbp);
  706. #endif
  707. if (scbp->tagmsg)
  708. host->act_tags[scbp->target]++;
  709. else
  710. host->targets[scbp->target].flags |= TCF_BUSY;
  711. scbp->status = SCB_BUSY;
  712. scbp->next = NULL;
  713. if (host->last_busy != NULL) {
  714. host->last_busy->next = scbp;
  715. host->last_busy = scbp;
  716. } else {
  717. host->first_busy = scbp;
  718. host->last_busy = scbp;
  719. }
  720. }
  721. /***************************************************************************/
  722. static struct scsi_ctrl_blk *initio_pop_busy_scb(struct initio_host * host)
  723. {
  724. struct scsi_ctrl_blk *tmp;
  725. if ((tmp = host->first_busy) != NULL) {
  726. if ((host->first_busy = tmp->next) == NULL)
  727. host->last_busy = NULL;
  728. tmp->next = NULL;
  729. if (tmp->tagmsg)
  730. host->act_tags[tmp->target]--;
  731. else
  732. host->targets[tmp->target].flags &= ~TCF_BUSY;
  733. }
  734. #if DEBUG_QUEUE
  735. printk("Pop busy SCB %p; ", tmp);
  736. #endif
  737. return tmp;
  738. }
  739. /***************************************************************************/
  740. static void initio_unlink_busy_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
  741. {
  742. struct scsi_ctrl_blk *tmp, *prev;
  743. #if DEBUG_QUEUE
  744. printk("unlink busy SCB %p; ", scb);
  745. #endif
  746. prev = tmp = host->first_busy;
  747. while (tmp != NULL) {
  748. if (scb == tmp) { /* Unlink this SCB */
  749. if (tmp == host->first_busy) {
  750. if ((host->first_busy = tmp->next) == NULL)
  751. host->last_busy = NULL;
  752. } else {
  753. prev->next = tmp->next;
  754. if (tmp == host->last_busy)
  755. host->last_busy = prev;
  756. }
  757. tmp->next = NULL;
  758. if (tmp->tagmsg)
  759. host->act_tags[tmp->target]--;
  760. else
  761. host->targets[tmp->target].flags &= ~TCF_BUSY;
  762. break;
  763. }
  764. prev = tmp;
  765. tmp = tmp->next;
  766. }
  767. return;
  768. }
  769. struct scsi_ctrl_blk *initio_find_busy_scb(struct initio_host * host, u16 tarlun)
  770. {
  771. struct scsi_ctrl_blk *tmp, *prev;
  772. u16 scbp_tarlun;
  773. prev = tmp = host->first_busy;
  774. while (tmp != NULL) {
  775. scbp_tarlun = (tmp->lun << 8) | (tmp->target);
  776. if (scbp_tarlun == tarlun) { /* Unlink this SCB */
  777. break;
  778. }
  779. prev = tmp;
  780. tmp = tmp->next;
  781. }
  782. #if DEBUG_QUEUE
  783. printk("find busy SCB %p; ", tmp);
  784. #endif
  785. return tmp;
  786. }
  787. static void initio_append_done_scb(struct initio_host * host, struct scsi_ctrl_blk * scbp)
  788. {
  789. #if DEBUG_QUEUE
  790. printk("append done SCB %p; ", scbp);
  791. #endif
  792. scbp->status = SCB_DONE;
  793. scbp->next = NULL;
  794. if (host->last_done != NULL) {
  795. host->last_done->next = scbp;
  796. host->last_done = scbp;
  797. } else {
  798. host->first_done = scbp;
  799. host->last_done = scbp;
  800. }
  801. }
  802. struct scsi_ctrl_blk *initio_find_done_scb(struct initio_host * host)
  803. {
  804. struct scsi_ctrl_blk *tmp;
  805. if ((tmp = host->first_done) != NULL) {
  806. if ((host->first_done = tmp->next) == NULL)
  807. host->last_done = NULL;
  808. tmp->next = NULL;
  809. }
  810. #if DEBUG_QUEUE
  811. printk("find done SCB %p; ",tmp);
  812. #endif
  813. return tmp;
  814. }
  815. static int initio_abort_srb(struct initio_host * host, struct scsi_cmnd *srbp)
  816. {
  817. unsigned long flags;
  818. struct scsi_ctrl_blk *tmp, *prev;
  819. spin_lock_irqsave(&host->semaph_lock, flags);
  820. if ((host->semaph == 0) && (host->active == NULL)) {
  821. /* disable Jasmin SCSI Int */
  822. outb(0x1F, host->addr + TUL_Mask);
  823. spin_unlock_irqrestore(&host->semaph_lock, flags);
  824. /* FIXME: synchronize_irq needed ? */
  825. tulip_main(host);
  826. spin_lock_irqsave(&host->semaph_lock, flags);
  827. host->semaph = 1;
  828. outb(0x0F, host->addr + TUL_Mask);
  829. spin_unlock_irqrestore(&host->semaph_lock, flags);
  830. return SCSI_ABORT_SNOOZE;
  831. }
  832. prev = tmp = host->first_pending; /* Check Pend queue */
  833. while (tmp != NULL) {
  834. /* 07/27/98 */
  835. if (tmp->srb == srbp) {
  836. if (tmp == host->active) {
  837. spin_unlock_irqrestore(&host->semaph_lock, flags);
  838. return SCSI_ABORT_BUSY;
  839. } else if (tmp == host->first_pending) {
  840. if ((host->first_pending = tmp->next) == NULL)
  841. host->last_pending = NULL;
  842. } else {
  843. prev->next = tmp->next;
  844. if (tmp == host->last_pending)
  845. host->last_pending = prev;
  846. }
  847. tmp->hastat = HOST_ABORTED;
  848. tmp->flags |= SCF_DONE;
  849. if (tmp->flags & SCF_POST)
  850. (*tmp->post) ((u8 *) host, (u8 *) tmp);
  851. spin_unlock_irqrestore(&host->semaph_lock, flags);
  852. return SCSI_ABORT_SUCCESS;
  853. }
  854. prev = tmp;
  855. tmp = tmp->next;
  856. }
  857. prev = tmp = host->first_busy; /* Check Busy queue */
  858. while (tmp != NULL) {
  859. if (tmp->srb == srbp) {
  860. if (tmp == host->active) {
  861. spin_unlock_irqrestore(&host->semaph_lock, flags);
  862. return SCSI_ABORT_BUSY;
  863. } else if (tmp->tagmsg == 0) {
  864. spin_unlock_irqrestore(&host->semaph_lock, flags);
  865. return SCSI_ABORT_BUSY;
  866. } else {
  867. host->act_tags[tmp->target]--;
  868. if (tmp == host->first_busy) {
  869. if ((host->first_busy = tmp->next) == NULL)
  870. host->last_busy = NULL;
  871. } else {
  872. prev->next = tmp->next;
  873. if (tmp == host->last_busy)
  874. host->last_busy = prev;
  875. }
  876. tmp->next = NULL;
  877. tmp->hastat = HOST_ABORTED;
  878. tmp->flags |= SCF_DONE;
  879. if (tmp->flags & SCF_POST)
  880. (*tmp->post) ((u8 *) host, (u8 *) tmp);
  881. spin_unlock_irqrestore(&host->semaph_lock, flags);
  882. return SCSI_ABORT_SUCCESS;
  883. }
  884. }
  885. prev = tmp;
  886. tmp = tmp->next;
  887. }
  888. spin_unlock_irqrestore(&host->semaph_lock, flags);
  889. return SCSI_ABORT_NOT_RUNNING;
  890. }
  891. /***************************************************************************/
  892. static int initio_bad_seq(struct initio_host * host)
  893. {
  894. struct scsi_ctrl_blk *scb;
  895. printk("initio_bad_seg c=%d\n", host->index);
  896. if ((scb = host->active) != NULL) {
  897. initio_unlink_busy_scb(host, scb);
  898. scb->hastat = HOST_BAD_PHAS;
  899. scb->tastat = 0;
  900. initio_append_done_scb(host, scb);
  901. }
  902. initio_stop_bm(host);
  903. initio_reset_scsi(host, 8); /* 7/29/98 */
  904. return initio_post_scsi_rst(host);
  905. }
  906. /************************************************************************/
  907. static void initio_exec_scb(struct initio_host * host, struct scsi_ctrl_blk * scb)
  908. {
  909. unsigned long flags;
  910. scb->mode = 0;
  911. scb->sgidx = 0;
  912. scb->sgmax = scb->sglen;
  913. spin_lock_irqsave(&host->semaph_lock, flags);
  914. initio_append_pend_scb(host, scb); /* Append this SCB to Pending queue */
  915. /* VVVVV 07/21/98 */
  916. if (host->semaph == 1) {
  917. /* Disable Jasmin SCSI Int */
  918. outb(0x1F, host->addr + TUL_Mask);
  919. host->semaph = 0;
  920. spin_unlock_irqrestore(&host->semaph_lock, flags);
  921. tulip_main(host);
  922. spin_lock_irqsave(&host->semaph_lock, flags);
  923. host->semaph = 1;
  924. outb(0x0F, host->addr + TUL_Mask);
  925. }
  926. spin_unlock_irqrestore(&host->semaph_lock, flags);
  927. return;
  928. }
  929. /***************************************************************************/
  930. static int initio_isr(struct initio_host * host)
  931. {
  932. if (inb(host->addr + TUL_Int) & TSS_INT_PENDING) {
  933. if (host->semaph == 1) {
  934. outb(0x1F, host->addr + TUL_Mask);
  935. /* Disable Tulip SCSI Int */
  936. host->semaph = 0;
  937. tulip_main(host);
  938. host->semaph = 1;
  939. outb(0x0F, host->addr + TUL_Mask);
  940. return 1;
  941. }
  942. }
  943. return 0;
  944. }
  945. static int tulip_main(struct initio_host * host)
  946. {
  947. struct scsi_ctrl_blk *scb;
  948. for (;;) {
  949. tulip_scsi(host); /* Call tulip_scsi */
  950. /* Walk the list of completed SCBs */
  951. while ((scb = initio_find_done_scb(host)) != NULL) { /* find done entry */
  952. if (scb->tastat == INI_QUEUE_FULL) {
  953. host->max_tags[scb->target] =
  954. host->act_tags[scb->target] - 1;
  955. scb->tastat = 0;
  956. initio_append_pend_scb(host, scb);
  957. continue;
  958. }
  959. if (!(scb->mode & SCM_RSENS)) { /* not in auto req. sense mode */
  960. if (scb->tastat == 2) {
  961. /* clr sync. nego flag */
  962. if (scb->flags & SCF_SENSE) {
  963. u8 len;
  964. len = scb->senselen;
  965. if (len == 0)
  966. len = 1;
  967. scb->buflen = scb->senselen;
  968. scb->bufptr = scb->senseptr;
  969. scb->flags &= ~(SCF_SG | SCF_DIR); /* for xfer_data_in */
  970. /* so, we won't report wrong direction in xfer_data_in,
  971. and won't report HOST_DO_DU in state_6 */
  972. scb->mode = SCM_RSENS;
  973. scb->ident &= 0xBF; /* Disable Disconnect */
  974. scb->tagmsg = 0;
  975. scb->tastat = 0;
  976. scb->cdblen = 6;
  977. scb->cdb[0] = SCSICMD_RequestSense;
  978. scb->cdb[1] = 0;
  979. scb->cdb[2] = 0;
  980. scb->cdb[3] = 0;
  981. scb->cdb[4] = len;
  982. scb->cdb[5] = 0;
  983. initio_push_pend_scb(host, scb);
  984. break;
  985. }
  986. }
  987. } else { /* in request sense mode */
  988. if (scb->tastat == 2) { /* check contition status again after sending
  989. requset sense cmd 0x3 */
  990. scb->hastat = HOST_BAD_PHAS;
  991. }
  992. scb->tastat = 2;
  993. }
  994. scb->flags |= SCF_DONE;
  995. if (scb->flags & SCF_POST) {
  996. /* FIXME: only one post method and lose casts */
  997. (*scb->post) ((u8 *) host, (u8 *) scb);
  998. }
  999. } /* while */
  1000. /* find_active: */
  1001. if (inb(host->addr + TUL_SStatus0) & TSS_INT_PENDING)
  1002. continue;
  1003. if (host->active) /* return to OS and wait for xfer_done_ISR/Selected_ISR */
  1004. return 1; /* return to OS, enable interrupt */
  1005. /* Check pending SCB */
  1006. if (initio_find_first_pend_scb(host) == NULL)
  1007. return 1; /* return to OS, enable interrupt */
  1008. } /* End of for loop */
  1009. /* statement won't reach here */
  1010. }
  1011. static void tulip_scsi(struct initio_host * host)
  1012. {
  1013. struct scsi_ctrl_blk *scb;
  1014. struct target_control *active_tc;
  1015. /* make sure to service interrupt asap */
  1016. if ((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING) {
  1017. host->phase = host->jsstatus0 & TSS_PH_MASK;
  1018. host->jsstatus1 = inb(host->addr + TUL_SStatus1);
  1019. host->jsint = inb(host->addr + TUL_SInt);
  1020. if (host->jsint & TSS_SCSIRST_INT) { /* SCSI bus reset detected */
  1021. int_initio_scsi_rst(host);
  1022. return;
  1023. }
  1024. if (host->jsint & TSS_RESEL_INT) { /* if selected/reselected interrupt */
  1025. if (int_initio_resel(host) == 0)
  1026. initio_next_state(host);
  1027. return;
  1028. }
  1029. if (host->jsint & TSS_SEL_TIMEOUT) {
  1030. int_initio_busfree(host);
  1031. return;
  1032. }
  1033. if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */
  1034. int_initio_busfree(host); /* unexpected bus free or sel timeout */
  1035. return;
  1036. }
  1037. if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV)) { /* func complete or Bus service */
  1038. if ((scb = host->active) != NULL)
  1039. initio_next_state(host);
  1040. return;
  1041. }
  1042. }
  1043. if (host->active != NULL)
  1044. return;
  1045. if ((scb = initio_find_first_pend_scb(host)) == NULL)
  1046. return;
  1047. /* program HBA's SCSI ID & target SCSI ID */
  1048. outb((host->scsi_id << 4) | (scb->target & 0x0F),
  1049. host->addr + TUL_SScsiId);
  1050. if (scb->opcode == ExecSCSI) {
  1051. active_tc = &host->targets[scb->target];
  1052. if (scb->tagmsg)
  1053. active_tc->drv_flags |= TCF_DRV_EN_TAG;
  1054. else
  1055. active_tc->drv_flags &= ~TCF_DRV_EN_TAG;
  1056. outb(active_tc->js_period, host->addr + TUL_SPeriod);
  1057. if ((active_tc->flags & (TCF_WDTR_DONE | TCF_NO_WDTR)) == 0) { /* do wdtr negotiation */
  1058. initio_select_atn_stop(host, scb);
  1059. } else {
  1060. if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) { /* do sync negotiation */
  1061. initio_select_atn_stop(host, scb);
  1062. } else {
  1063. if (scb->tagmsg)
  1064. initio_select_atn3(host, scb);
  1065. else
  1066. initio_select_atn(host, scb);
  1067. }
  1068. }
  1069. if (scb->flags & SCF_POLL) {
  1070. while (wait_tulip(host) != -1) {
  1071. if (initio_next_state(host) == -1)
  1072. break;
  1073. }
  1074. }
  1075. } else if (scb->opcode == BusDevRst) {
  1076. initio_select_atn_stop(host, scb);
  1077. scb->next_state = 8;
  1078. if (scb->flags & SCF_POLL) {
  1079. while (wait_tulip(host) != -1) {
  1080. if (initio_next_state(host) == -1)
  1081. break;
  1082. }
  1083. }
  1084. } else if (scb->opcode == AbortCmd) {
  1085. if (initio_abort_srb(host, scb->srb) != 0) {
  1086. initio_unlink_pend_scb(host, scb);
  1087. initio_release_scb(host, scb);
  1088. } else {
  1089. scb->opcode = BusDevRst;
  1090. initio_select_atn_stop(host, scb);
  1091. scb->next_state = 8;
  1092. }
  1093. } else {
  1094. initio_unlink_pend_scb(host, scb);
  1095. scb->hastat = 0x16; /* bad command */
  1096. initio_append_done_scb(host, scb);
  1097. }
  1098. return;
  1099. }
  1100. /**
  1101. * initio_next_state - Next SCSI state
  1102. * @host: InitIO host we are processing
  1103. *
  1104. * Progress the active command block along the state machine
  1105. * until we hit a state which we must wait for activity to occur.
  1106. *
  1107. * Returns zero or a negative code.
  1108. */
  1109. static int initio_next_state(struct initio_host * host)
  1110. {
  1111. int next;
  1112. next = host->active->next_state;
  1113. for (;;) {
  1114. switch (next) {
  1115. case 1:
  1116. next = initio_state_1(host);
  1117. break;
  1118. case 2:
  1119. next = initio_state_2(host);
  1120. break;
  1121. case 3:
  1122. next = initio_state_3(host);
  1123. break;
  1124. case 4:
  1125. next = initio_state_4(host);
  1126. break;
  1127. case 5:
  1128. next = initio_state_5(host);
  1129. break;
  1130. case 6:
  1131. next = initio_state_6(host);
  1132. break;
  1133. case 7:
  1134. next = initio_state_7(host);
  1135. break;
  1136. case 8:
  1137. return initio_bus_device_reset(host);
  1138. default:
  1139. return initio_bad_seq(host);
  1140. }
  1141. if (next <= 0)
  1142. return next;
  1143. }
  1144. }
  1145. /**
  1146. * initio_state_1 - SCSI state machine
  1147. * @host: InitIO host we are controlling
  1148. *
  1149. * Perform SCSI state processing for Select/Attention/Stop
  1150. */
  1151. static int initio_state_1(struct initio_host * host)
  1152. {
  1153. struct scsi_ctrl_blk *scb = host->active;
  1154. struct target_control *active_tc = host->active_tc;
  1155. #if DEBUG_STATE
  1156. printk("-s1-");
  1157. #endif
  1158. /* Move the SCB from pending to busy */
  1159. initio_unlink_pend_scb(host, scb);
  1160. initio_append_busy_scb(host, scb);
  1161. outb(active_tc->sconfig0, host->addr + TUL_SConfig );
  1162. /* ATN on */
  1163. if (host->phase == MSG_OUT) {
  1164. outb(TSC_EN_BUS_IN | TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
  1165. outb(scb->ident, host->addr + TUL_SFifo);
  1166. if (scb->tagmsg) {
  1167. outb(scb->tagmsg, host->addr + TUL_SFifo);
  1168. outb(scb->tagid, host->addr + TUL_SFifo);
  1169. }
  1170. if ((active_tc->flags & (TCF_WDTR_DONE | TCF_NO_WDTR)) == 0) {
  1171. active_tc->flags |= TCF_WDTR_DONE;
  1172. outb(MSG_EXTEND, host->addr + TUL_SFifo);
  1173. outb(2, host->addr + TUL_SFifo); /* Extended msg length */
  1174. outb(3, host->addr + TUL_SFifo); /* Sync request */
  1175. outb(1, host->addr + TUL_SFifo); /* Start from 16 bits */
  1176. } else if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) {
  1177. active_tc->flags |= TCF_SYNC_DONE;
  1178. outb(MSG_EXTEND, host->addr + TUL_SFifo);
  1179. outb(3, host->addr + TUL_SFifo); /* extended msg length */
  1180. outb(1, host->addr + TUL_SFifo); /* sync request */
  1181. outb(initio_rate_tbl[active_tc->flags & TCF_SCSI_RATE], host->addr + TUL_SFifo);
  1182. outb(MAX_OFFSET, host->addr + TUL_SFifo); /* REQ/ACK offset */
  1183. }
  1184. outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
  1185. if (wait_tulip(host) == -1)
  1186. return -1;
  1187. }
  1188. outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
  1189. outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), host->addr + TUL_SSignal);
  1190. /* Into before CDB xfer */
  1191. return 3;
  1192. }
  1193. /**
  1194. * initio_state_2 - SCSI state machine
  1195. * @host: InitIO host we are controlling
  1196. *
  1197. * state after selection with attention
  1198. * state after selection with attention3
  1199. */
  1200. static int initio_state_2(struct initio_host * host)
  1201. {
  1202. struct scsi_ctrl_blk *scb = host->active;
  1203. struct target_control *active_tc = host->active_tc;
  1204. #if DEBUG_STATE
  1205. printk("-s2-");
  1206. #endif
  1207. initio_unlink_pend_scb(host, scb);
  1208. initio_append_busy_scb(host, scb);
  1209. outb(active_tc->sconfig0, host->addr + TUL_SConfig);
  1210. if (host->jsstatus1 & TSS_CMD_PH_CMP)
  1211. return 4;
  1212. outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
  1213. outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)), host->addr + TUL_SSignal);
  1214. /* Into before CDB xfer */
  1215. return 3;
  1216. }
  1217. /**
  1218. * initio_state_3 - SCSI state machine
  1219. * @host: InitIO host we are controlling
  1220. *
  1221. * state before CDB xfer is done
  1222. */
  1223. static int initio_state_3(struct initio_host * host)
  1224. {
  1225. struct scsi_ctrl_blk *scb = host->active;
  1226. struct target_control *active_tc = host->active_tc;
  1227. int i;
  1228. #if DEBUG_STATE
  1229. printk("-s3-");
  1230. #endif
  1231. for (;;) {
  1232. switch (host->phase) {
  1233. case CMD_OUT: /* Command out phase */
  1234. for (i = 0; i < (int) scb->cdblen; i++)
  1235. outb(scb->cdb[i], host->addr + TUL_SFifo);
  1236. outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
  1237. if (wait_tulip(host) == -1)
  1238. return -1;
  1239. if (host->phase == CMD_OUT)
  1240. return initio_bad_seq(host);
  1241. return 4;
  1242. case MSG_IN: /* Message in phase */
  1243. scb->next_state = 3;
  1244. if (initio_msgin(host) == -1)
  1245. return -1;
  1246. break;
  1247. case STATUS_IN: /* Status phase */
  1248. if (initio_status_msg(host) == -1)
  1249. return -1;
  1250. break;
  1251. case MSG_OUT: /* Message out phase */
  1252. if (active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) {
  1253. outb(MSG_NOP, host->addr + TUL_SFifo); /* msg nop */
  1254. outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
  1255. if (wait_tulip(host) == -1)
  1256. return -1;
  1257. } else {
  1258. active_tc->flags |= TCF_SYNC_DONE;
  1259. outb(MSG_EXTEND, host->addr + TUL_SFifo);
  1260. outb(3, host->addr + TUL_SFifo); /* ext. msg len */
  1261. outb(1, host->addr + TUL_SFifo); /* sync request */
  1262. outb(initio_rate_tbl[active_tc->flags & TCF_SCSI_RATE], host->addr + TUL_SFifo);
  1263. outb(MAX_OFFSET, host->addr + TUL_SFifo); /* REQ/ACK offset */
  1264. outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
  1265. if (wait_tulip(host) == -1)
  1266. return -1;
  1267. outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
  1268. outb(inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7), host->addr + TUL_SSignal);
  1269. }
  1270. break;
  1271. default:
  1272. return initio_bad_seq(host);
  1273. }
  1274. }
  1275. }
  1276. /**
  1277. * initio_state_4 - SCSI state machine
  1278. * @host: InitIO host we are controlling
  1279. *
  1280. * SCSI state machine. State 4
  1281. */
  1282. static int initio_state_4(struct initio_host * host)
  1283. {
  1284. struct scsi_ctrl_blk *scb = host->active;
  1285. #if DEBUG_STATE
  1286. printk("-s4-");
  1287. #endif
  1288. if ((scb->flags & SCF_DIR) == SCF_NO_XF) {
  1289. return 6; /* Go to state 6 (After data) */
  1290. }
  1291. for (;;) {
  1292. if (scb->buflen == 0)
  1293. return 6;
  1294. switch (host->phase) {
  1295. case STATUS_IN: /* Status phase */
  1296. if ((scb->flags & SCF_DIR) != 0) /* if direction bit set then report data underrun */
  1297. scb->hastat = HOST_DO_DU;
  1298. if ((initio_status_msg(host)) == -1)
  1299. return -1;
  1300. break;
  1301. case MSG_IN: /* Message in phase */
  1302. scb->next_state = 0x4;
  1303. if (initio_msgin(host) == -1)
  1304. return -1;
  1305. break;
  1306. case MSG_OUT: /* Message out phase */
  1307. if (host->jsstatus0 & TSS_PAR_ERROR) {
  1308. scb->buflen = 0;
  1309. scb->hastat = HOST_DO_DU;
  1310. if (initio_msgout_ide(host) == -1)
  1311. return -1;
  1312. return 6;
  1313. } else {
  1314. outb(MSG_NOP, host->addr + TUL_SFifo); /* msg nop */
  1315. outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
  1316. if (wait_tulip(host) == -1)
  1317. return -1;
  1318. }
  1319. break;
  1320. case DATA_IN: /* Data in phase */
  1321. return initio_xfer_data_in(host);
  1322. case DATA_OUT: /* Data out phase */
  1323. return initio_xfer_data_out(host);
  1324. default:
  1325. return initio_bad_seq(host);
  1326. }
  1327. }
  1328. }
  1329. /**
  1330. * initio_state_5 - SCSI state machine
  1331. * @host: InitIO host we are controlling
  1332. *
  1333. * State after dma xfer done or phase change before xfer done
  1334. */
  1335. static int initio_state_5(struct initio_host * host)
  1336. {
  1337. struct scsi_ctrl_blk *scb = host->active;
  1338. long cnt, xcnt; /* cannot use unsigned !! code: if (xcnt < 0) */
  1339. #if DEBUG_STATE
  1340. printk("-s5-");
  1341. #endif
  1342. /*------ get remaining count -------*/
  1343. cnt = inl(host->addr + TUL_SCnt0) & 0x0FFFFFF;
  1344. if (inb(host->addr + TUL_XCmd) & 0x20) {
  1345. /* ----------------------- DATA_IN ----------------------------- */
  1346. /* check scsi parity error */
  1347. if (host->jsstatus0 & TSS_PAR_ERROR)
  1348. scb->hastat = HOST_DO_DU;
  1349. if (inb(host->addr + TUL_XStatus) & XPEND) { /* DMA xfer pending, Send STOP */
  1350. /* tell Hardware scsi xfer has been terminated */
  1351. outb(inb(host->addr + TUL_XCtrl) | 0x80, host->addr + TUL_XCtrl);
  1352. /* wait until DMA xfer not pending */
  1353. while (inb(host->addr + TUL_XStatus) & XPEND)
  1354. cpu_relax();
  1355. }
  1356. } else {
  1357. /*-------- DATA OUT -----------*/
  1358. if ((inb(host->addr + TUL_SStatus1) & TSS_XFER_CMP) == 0) {
  1359. if (host->active_tc->js_period & TSC_WIDE_SCSI)
  1360. cnt += (inb(host->addr + TUL_SFifoCnt) & 0x1F) << 1;
  1361. else
  1362. cnt += (inb(host->addr + TUL_SFifoCnt) & 0x1F);
  1363. }
  1364. if (inb(host->addr + TUL_XStatus) & XPEND) { /* if DMA xfer is pending, abort DMA xfer */
  1365. outb(TAX_X_ABT, host->addr + TUL_XCmd);
  1366. /* wait Abort DMA xfer done */
  1367. while ((inb(host->addr + TUL_Int) & XABT) == 0)
  1368. cpu_relax();
  1369. }
  1370. if ((cnt == 1) && (host->phase == DATA_OUT)) {
  1371. outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
  1372. if (wait_tulip(host) == -1)
  1373. return -1;
  1374. cnt = 0;
  1375. } else {
  1376. if ((inb(host->addr + TUL_SStatus1) & TSS_XFER_CMP) == 0)
  1377. outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
  1378. }
  1379. }
  1380. if (cnt == 0) {
  1381. scb->buflen = 0;
  1382. return 6; /* After Data */
  1383. }
  1384. /* Update active data pointer */
  1385. xcnt = (long) scb->buflen - cnt; /* xcnt== bytes already xferred */
  1386. scb->buflen = (u32) cnt; /* cnt == bytes left to be xferred */
  1387. if (scb->flags & SCF_SG) {
  1388. struct sg_entry *sgp;
  1389. unsigned long i;
  1390. sgp = &scb->sglist[scb->sgidx];
  1391. for (i = scb->sgidx; i < scb->sgmax; sgp++, i++) {
  1392. xcnt -= (long) sgp->len;
  1393. if (xcnt < 0) { /* this sgp xfer half done */
  1394. xcnt += (long) sgp->len; /* xcnt == bytes xferred in this sgp */
  1395. sgp->data += (u32) xcnt; /* new ptr to be xfer */
  1396. sgp->len -= (u32) xcnt; /* new len to be xfer */
  1397. scb->bufptr += ((u32) (i - scb->sgidx) << 3);
  1398. /* new SG table ptr */
  1399. scb->sglen = (u8) (scb->sgmax - i);
  1400. /* new SG table len */
  1401. scb->sgidx = (u16) i;
  1402. /* for next disc and come in this loop */
  1403. return 4; /* Go to state 4 */
  1404. }
  1405. /* else (xcnt >= 0 , i.e. this sgp already xferred */
  1406. } /* for */
  1407. return 6; /* Go to state 6 */
  1408. } else {
  1409. scb->bufptr += (u32) xcnt;
  1410. }
  1411. return 4; /* Go to state 4 */
  1412. }
  1413. /**
  1414. * initio_state_6 - SCSI state machine
  1415. * @host: InitIO host we are controlling
  1416. *
  1417. * State after Data phase
  1418. */
  1419. static int initio_state_6(struct initio_host * host)
  1420. {
  1421. struct scsi_ctrl_blk *scb = host->active;
  1422. #if DEBUG_STATE
  1423. printk("-s6-");
  1424. #endif
  1425. for (;;) {
  1426. switch (host->phase) {
  1427. case STATUS_IN: /* Status phase */
  1428. if ((initio_status_msg(host)) == -1)
  1429. return -1;
  1430. break;
  1431. case MSG_IN: /* Message in phase */
  1432. scb->next_state = 6;
  1433. if ((initio_msgin(host)) == -1)
  1434. return -1;
  1435. break;
  1436. case MSG_OUT: /* Message out phase */
  1437. outb(MSG_NOP, host->addr + TUL_SFifo); /* msg nop */
  1438. outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
  1439. if (wait_tulip(host) == -1)
  1440. return -1;
  1441. break;
  1442. case DATA_IN: /* Data in phase */
  1443. return initio_xpad_in(host);
  1444. case DATA_OUT: /* Data out phase */
  1445. return initio_xpad_out(host);
  1446. default:
  1447. return initio_bad_seq(host);
  1448. }
  1449. }
  1450. }
  1451. /**
  1452. * initio_state_7 - SCSI state machine
  1453. * @host: InitIO host we are controlling
  1454. *
  1455. */
  1456. int initio_state_7(struct initio_host * host)
  1457. {
  1458. int cnt, i;
  1459. #if DEBUG_STATE
  1460. printk("-s7-");
  1461. #endif
  1462. /* flush SCSI FIFO */
  1463. cnt = inb(host->addr + TUL_SFifoCnt) & 0x1F;
  1464. if (cnt) {
  1465. for (i = 0; i < cnt; i++)
  1466. inb(host->addr + TUL_SFifo);
  1467. }
  1468. switch (host->phase) {
  1469. case DATA_IN: /* Data in phase */
  1470. case DATA_OUT: /* Data out phase */
  1471. return initio_bad_seq(host);
  1472. default:
  1473. return 6; /* Go to state 6 */
  1474. }
  1475. }
  1476. /**
  1477. * initio_xfer_data_in - Commence data input
  1478. * @host: InitIO host in use
  1479. *
  1480. * Commence a block of data transfer. The transfer itself will
  1481. * be managed by the controller and we will get a completion (or
  1482. * failure) interrupt.
  1483. */
  1484. static int initio_xfer_data_in(struct initio_host * host)
  1485. {
  1486. struct scsi_ctrl_blk *scb = host->active;
  1487. if ((scb->flags & SCF_DIR) == SCF_DOUT)
  1488. return 6; /* wrong direction */
  1489. outl(scb->buflen, host->addr + TUL_SCnt0);
  1490. outb(TSC_XF_DMA_IN, host->addr + TUL_SCmd); /* 7/25/95 */
  1491. if (scb->flags & SCF_SG) { /* S/G xfer */
  1492. outl(((u32) scb->sglen) << 3, host->addr + TUL_XCntH);
  1493. outl(scb->bufptr, host->addr + TUL_XAddH);
  1494. outb(TAX_SG_IN, host->addr + TUL_XCmd);
  1495. } else {
  1496. outl(scb->buflen, host->addr + TUL_XCntH);
  1497. outl(scb->bufptr, host->addr + TUL_XAddH);
  1498. outb(TAX_X_IN, host->addr + TUL_XCmd);
  1499. }
  1500. scb->next_state = 0x5;
  1501. return 0; /* return to OS, wait xfer done , let jas_isr come in */
  1502. }
  1503. /**
  1504. * initio_xfer_data_out - Commence data output
  1505. * @host: InitIO host in use
  1506. *
  1507. * Commence a block of data transfer. The transfer itself will
  1508. * be managed by the controller and we will get a completion (or
  1509. * failure) interrupt.
  1510. */
  1511. static int initio_xfer_data_out(struct initio_host * host)
  1512. {
  1513. struct scsi_ctrl_blk *scb = host->active;
  1514. if ((scb->flags & SCF_DIR) == SCF_DIN)
  1515. return 6; /* wrong direction */
  1516. outl(scb->buflen, host->addr + TUL_SCnt0);
  1517. outb(TSC_XF_DMA_OUT, host->addr + TUL_SCmd);
  1518. if (scb->flags & SCF_SG) { /* S/G xfer */
  1519. outl(((u32) scb->sglen) << 3, host->addr + TUL_XCntH);
  1520. outl(scb->bufptr, host->addr + TUL_XAddH);
  1521. outb(TAX_SG_OUT, host->addr + TUL_XCmd);
  1522. } else {
  1523. outl(scb->buflen, host->addr + TUL_XCntH);
  1524. outl(scb->bufptr, host->addr + TUL_XAddH);
  1525. outb(TAX_X_OUT, host->addr + TUL_XCmd);
  1526. }
  1527. scb->next_state = 0x5;
  1528. return 0; /* return to OS, wait xfer done , let jas_isr come in */
  1529. }
  1530. int initio_xpad_in(struct initio_host * host)
  1531. {
  1532. struct scsi_ctrl_blk *scb = host->active;
  1533. struct target_control *active_tc = host->active_tc;
  1534. if ((scb->flags & SCF_DIR) != SCF_NO_DCHK)
  1535. scb->hastat = HOST_DO_DU; /* over run */
  1536. for (;;) {
  1537. if (active_tc->js_period & TSC_WIDE_SCSI)
  1538. outl(2, host->addr + TUL_SCnt0);
  1539. else
  1540. outl(1, host->addr + TUL_SCnt0);
  1541. outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
  1542. if (wait_tulip(host) == -1)
  1543. return -1;
  1544. if (host->phase != DATA_IN) {
  1545. outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
  1546. return 6;
  1547. }
  1548. inb(host->addr + TUL_SFifo);
  1549. }
  1550. }
  1551. int initio_xpad_out(struct initio_host * host)
  1552. {
  1553. struct scsi_ctrl_blk *scb = host->active;
  1554. struct target_control *active_tc = host->active_tc;
  1555. if ((scb->flags & SCF_DIR) != SCF_NO_DCHK)
  1556. scb->hastat = HOST_DO_DU; /* over run */
  1557. for (;;) {
  1558. if (active_tc->js_period & TSC_WIDE_SCSI)
  1559. outl(2, host->addr + TUL_SCnt0);
  1560. else
  1561. outl(1, host->addr + TUL_SCnt0);
  1562. outb(0, host->addr + TUL_SFifo);
  1563. outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
  1564. if ((wait_tulip(host)) == -1)
  1565. return -1;
  1566. if (host->phase != DATA_OUT) { /* Disable wide CPU to allow read 16 bits */
  1567. outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1);
  1568. outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
  1569. return 6;
  1570. }
  1571. }
  1572. }
  1573. int initio_status_msg(struct initio_host * host)
  1574. { /* status & MSG_IN */
  1575. struct scsi_ctrl_blk *scb = host->active;
  1576. u8 msg;
  1577. outb(TSC_CMD_COMP, host->addr + TUL_SCmd);
  1578. if (wait_tulip(host) == -1)
  1579. return -1;
  1580. /* get status */
  1581. scb->tastat = inb(host->addr + TUL_SFifo);
  1582. if (host->phase == MSG_OUT) {
  1583. if (host->jsstatus0 & TSS_PAR_ERROR)
  1584. outb(MSG_PARITY, host->addr + TUL_SFifo);
  1585. else
  1586. outb(MSG_NOP, host->addr + TUL_SFifo);
  1587. outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
  1588. return wait_tulip(host);
  1589. }
  1590. if (host->phase == MSG_IN) {
  1591. msg = inb(host->addr + TUL_SFifo);
  1592. if (host->jsstatus0 & TSS_PAR_ERROR) { /* Parity error */
  1593. if ((initio_msgin_accept(host)) == -1)
  1594. return -1;
  1595. if (host->phase != MSG_OUT)
  1596. return initio_bad_seq(host);
  1597. outb(MSG_PARITY, host->addr + TUL_SFifo);
  1598. outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
  1599. return wait_tulip(host);
  1600. }
  1601. if (msg == 0) { /* Command complete */
  1602. if ((scb->tastat & 0x18) == 0x10) /* No link support */
  1603. return initio_bad_seq(host);
  1604. outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
  1605. outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
  1606. return initio_wait_done_disc(host);
  1607. }
  1608. if (msg == MSG_LINK_COMP || msg == MSG_LINK_FLAG) {
  1609. if ((scb->tastat & 0x18) == 0x10)
  1610. return initio_msgin_accept(host);
  1611. }
  1612. }
  1613. return initio_bad_seq(host);
  1614. }
  1615. /* scsi bus free */
  1616. int int_initio_busfree(struct initio_host * host)
  1617. {
  1618. struct scsi_ctrl_blk *scb = host->active;
  1619. if (scb != NULL) {
  1620. if (scb->status & SCB_SELECT) { /* selection timeout */
  1621. initio_unlink_pend_scb(host, scb);
  1622. scb->hastat = HOST_SEL_TOUT;
  1623. initio_append_done_scb(host, scb);
  1624. } else { /* Unexpected bus free */
  1625. initio_unlink_busy_scb(host, scb);
  1626. scb->hastat = HOST_BUS_FREE;
  1627. initio_append_done_scb(host, scb);
  1628. }
  1629. host->active = NULL;
  1630. host->active_tc = NULL;
  1631. }
  1632. outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
  1633. outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
  1634. outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
  1635. return -1;
  1636. }
  1637. /**
  1638. * int_initio_scsi_rst - SCSI reset occurred
  1639. * @host: Host seeing the reset
  1640. *
  1641. * A SCSI bus reset has occurred. Clean up any pending transfer
  1642. * the hardware is doing by DMA and then abort all active and
  1643. * disconnected commands. The mid layer should sort the rest out
  1644. * for us
  1645. */
  1646. static int int_initio_scsi_rst(struct initio_host * host)
  1647. {
  1648. struct scsi_ctrl_blk *scb;
  1649. int i;
  1650. /* if DMA xfer is pending, abort DMA xfer */
  1651. if (inb(host->addr + TUL_XStatus) & 0x01) {
  1652. outb(TAX_X_ABT | TAX_X_CLR_FIFO, host->addr + TUL_XCmd);
  1653. /* wait Abort DMA xfer done */
  1654. while ((inb(host->addr + TUL_Int) & 0x04) == 0)
  1655. cpu_relax();
  1656. outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
  1657. }
  1658. /* Abort all active & disconnected scb */
  1659. while ((scb = initio_pop_busy_scb(host)) != NULL) {
  1660. scb->hastat = HOST_BAD_PHAS;
  1661. initio_append_done_scb(host, scb);
  1662. }
  1663. host->active = NULL;
  1664. host->active_tc = NULL;
  1665. /* clr sync nego. done flag */
  1666. for (i = 0; i < host->max_tar; i++)
  1667. host->targets[i].flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
  1668. return -1;
  1669. }
  1670. /**
  1671. * int_initio_scsi_resel - Reselection occurred
  1672. * @host: InitIO host adapter
  1673. *
  1674. * A SCSI reselection event has been signalled and the interrupt
  1675. * is now being processed. Work out which command block needs attention
  1676. * and continue processing that command.
  1677. */
  1678. int int_initio_resel(struct initio_host * host)
  1679. {
  1680. struct scsi_ctrl_blk *scb;
  1681. struct target_control *active_tc;
  1682. u8 tag, msg = 0;
  1683. u8 tar, lun;
  1684. if ((scb = host->active) != NULL) {
  1685. /* FIXME: Why check and not just clear ? */
  1686. if (scb->status & SCB_SELECT) /* if waiting for selection complete */
  1687. scb->status &= ~SCB_SELECT;
  1688. host->active = NULL;
  1689. }
  1690. /* --------- get target id---------------------- */
  1691. tar = inb(host->addr + TUL_SBusId);
  1692. /* ------ get LUN from Identify message----------- */
  1693. lun = inb(host->addr + TUL_SIdent) & 0x0F;
  1694. /* 07/22/98 from 0x1F -> 0x0F */
  1695. active_tc = &host->targets[tar];
  1696. host->active_tc = active_tc;
  1697. outb(active_tc->sconfig0, host->addr + TUL_SConfig);
  1698. outb(active_tc->js_period, host->addr + TUL_SPeriod);
  1699. /* ------------- tag queueing ? ------------------- */
  1700. if (active_tc->drv_flags & TCF_DRV_EN_TAG) {
  1701. if ((initio_msgin_accept(host)) == -1)
  1702. return -1;
  1703. if (host->phase != MSG_IN)
  1704. goto no_tag;
  1705. outl(1, host->addr + TUL_SCnt0);
  1706. outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
  1707. if (wait_tulip(host) == -1)
  1708. return -1;
  1709. msg = inb(host->addr + TUL_SFifo); /* Read Tag Message */
  1710. if (msg < MSG_STAG || msg > MSG_OTAG) /* Is simple Tag */
  1711. goto no_tag;
  1712. if (initio_msgin_accept(host) == -1)
  1713. return -1;
  1714. if (host->phase != MSG_IN)
  1715. goto no_tag;
  1716. outl(1, host->addr + TUL_SCnt0);
  1717. outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
  1718. if (wait_tulip(host) == -1)
  1719. return -1;
  1720. tag = inb(host->addr + TUL_SFifo); /* Read Tag ID */
  1721. scb = host->scb + tag;
  1722. if (scb->target != tar || scb->lun != lun) {
  1723. return initio_msgout_abort_tag(host);
  1724. }
  1725. if (scb->status != SCB_BUSY) { /* 03/24/95 */
  1726. return initio_msgout_abort_tag(host);
  1727. }
  1728. host->active = scb;
  1729. if ((initio_msgin_accept(host)) == -1)
  1730. return -1;
  1731. } else { /* No tag */
  1732. no_tag:
  1733. if ((scb = initio_find_busy_scb(host, tar | (lun << 8))) == NULL) {
  1734. return initio_msgout_abort_targ(host);
  1735. }
  1736. host->active = scb;
  1737. if (!(active_tc->drv_flags & TCF_DRV_EN_TAG)) {
  1738. if ((initio_msgin_accept(host)) == -1)
  1739. return -1;
  1740. }
  1741. }
  1742. return 0;
  1743. }
  1744. /**
  1745. * int_initio_bad_seq - out of phase
  1746. * @host: InitIO host flagging event
  1747. *
  1748. * We have ended up out of phase somehow. Reset the host controller
  1749. * and throw all our toys out of the pram. Let the midlayer clean up
  1750. */
  1751. static int int_initio_bad_seq(struct initio_host * host)
  1752. { /* target wrong phase */
  1753. struct scsi_ctrl_blk *scb;
  1754. int i;
  1755. initio_reset_scsi(host, 10);
  1756. while ((scb = initio_pop_busy_scb(host)) != NULL) {
  1757. scb->hastat = HOST_BAD_PHAS;
  1758. initio_append_done_scb(host, scb);
  1759. }
  1760. for (i = 0; i < host->max_tar; i++)
  1761. host->targets[i].flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
  1762. return -1;
  1763. }
  1764. /**
  1765. * initio_msgout_abort_targ - abort a tag
  1766. * @host: InitIO host
  1767. *
  1768. * Abort when the target/lun does not match or when our SCB is not
  1769. * busy. Used by untagged commands.
  1770. */
  1771. static int initio_msgout_abort_targ(struct initio_host * host)
  1772. {
  1773. outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
  1774. if (initio_msgin_accept(host) == -1)
  1775. return -1;
  1776. if (host->phase != MSG_OUT)
  1777. return initio_bad_seq(host);
  1778. outb(MSG_ABORT, host->addr + TUL_SFifo);
  1779. outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
  1780. return initio_wait_disc(host);
  1781. }
  1782. /**
  1783. * initio_msgout_abort_tag - abort a tag
  1784. * @host: InitIO host
  1785. *
  1786. * Abort when the target/lun does not match or when our SCB is not
  1787. * busy. Used for tagged commands.
  1788. */
  1789. static int initio_msgout_abort_tag(struct initio_host * host)
  1790. {
  1791. outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
  1792. if (initio_msgin_accept(host) == -1)
  1793. return -1;
  1794. if (host->phase != MSG_OUT)
  1795. return initio_bad_seq(host);
  1796. outb(MSG_ABORT_TAG, host->addr + TUL_SFifo);
  1797. outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
  1798. return initio_wait_disc(host);
  1799. }
  1800. /**
  1801. * initio_msgin - Message in
  1802. * @host: InitIO Host
  1803. *
  1804. * Process incoming message
  1805. */
  1806. static int initio_msgin(struct initio_host * host)
  1807. {
  1808. struct target_control *active_tc;
  1809. for (;;) {
  1810. outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
  1811. outl(1, host->addr + TUL_SCnt0);
  1812. outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
  1813. if (wait_tulip(host) == -1)
  1814. return -1;
  1815. switch (inb(host->addr + TUL_SFifo)) {
  1816. case MSG_DISC: /* Disconnect msg */
  1817. outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
  1818. return initio_wait_disc(host);
  1819. case MSG_SDP:
  1820. case MSG_RESTORE:
  1821. case MSG_NOP:
  1822. initio_msgin_accept(host);
  1823. break;
  1824. case MSG_REJ: /* Clear ATN first */
  1825. outb((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)),
  1826. host->addr + TUL_SSignal);
  1827. active_tc = host->active_tc;
  1828. if ((active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0) /* do sync nego */
  1829. outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN),
  1830. host->addr + TUL_SSignal);
  1831. initio_msgin_accept(host);
  1832. break;
  1833. case MSG_EXTEND: /* extended msg */
  1834. initio_msgin_extend(host);
  1835. break;
  1836. case MSG_IGNOREWIDE:
  1837. initio_msgin_accept(host);
  1838. break;
  1839. case MSG_COMP:
  1840. outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
  1841. outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
  1842. return initio_wait_done_disc(host);
  1843. default:
  1844. initio_msgout_reject(host);
  1845. break;
  1846. }
  1847. if (host->phase != MSG_IN)
  1848. return host->phase;
  1849. }
  1850. /* statement won't reach here */
  1851. }
  1852. static int initio_msgout_reject(struct initio_host * host)
  1853. {
  1854. outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
  1855. if (initio_msgin_accept(host) == -1)
  1856. return -1;
  1857. if (host->phase == MSG_OUT) {
  1858. outb(MSG_REJ, host->addr + TUL_SFifo); /* Msg reject */
  1859. outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
  1860. return wait_tulip(host);
  1861. }
  1862. return host->phase;
  1863. }
  1864. static int initio_msgout_ide(struct initio_host * host)
  1865. {
  1866. outb(MSG_IDE, host->addr + TUL_SFifo); /* Initiator Detected Error */
  1867. outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
  1868. return wait_tulip(host);
  1869. }
  1870. static int initio_msgin_extend(struct initio_host * host)
  1871. {
  1872. u8 len, idx;
  1873. if (initio_msgin_accept(host) != MSG_IN)
  1874. return host->phase;
  1875. /* Get extended msg length */
  1876. outl(1, host->addr + TUL_SCnt0);
  1877. outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
  1878. if (wait_tulip(host) == -1)
  1879. return -1;
  1880. len = inb(host->addr + TUL_SFifo);
  1881. host->msg[0] = len;
  1882. for (idx = 1; len != 0; len--) {
  1883. if ((initio_msgin_accept(host)) != MSG_IN)
  1884. return host->phase;
  1885. outl(1, host->addr + TUL_SCnt0);
  1886. outb(TSC_XF_FIFO_IN, host->addr + TUL_SCmd);
  1887. if (wait_tulip(host) == -1)
  1888. return -1;
  1889. host->msg[idx++] = inb(host->addr + TUL_SFifo);
  1890. }
  1891. if (host->msg[1] == 1) { /* if it's synchronous data transfer request */
  1892. u8 r;
  1893. if (host->msg[0] != 3) /* if length is not right */
  1894. return initio_msgout_reject(host);
  1895. if (host->active_tc->flags & TCF_NO_SYNC_NEGO) { /* Set OFFSET=0 to do async, nego back */
  1896. host->msg[3] = 0;
  1897. } else {
  1898. if (initio_msgin_sync(host) == 0 &&
  1899. (host->active_tc->flags & TCF_SYNC_DONE)) {
  1900. initio_sync_done(host);
  1901. return initio_msgin_accept(host);
  1902. }
  1903. }
  1904. r = inb(host->addr + TUL_SSignal);
  1905. outb((r & (TSC_SET_ACK | 7)) | TSC_SET_ATN,
  1906. host->addr + TUL_SSignal);
  1907. if (initio_msgin_accept(host) != MSG_OUT)
  1908. return host->phase;
  1909. /* sync msg out */
  1910. outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0);
  1911. initio_sync_done(host);
  1912. outb(MSG_EXTEND, host->addr + TUL_SFifo);
  1913. outb(3, host->addr + TUL_SFifo);
  1914. outb(1, host->addr + TUL_SFifo);
  1915. outb(host->msg[2], host->addr + TUL_SFifo);
  1916. outb(host->msg[3], host->addr + TUL_SFifo);
  1917. outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
  1918. return wait_tulip(host);
  1919. }
  1920. if (host->msg[0] != 2 || host->msg[1] != 3)
  1921. return initio_msgout_reject(host);
  1922. /* if it's WIDE DATA XFER REQ */
  1923. if (host->active_tc->flags & TCF_NO_WDTR) {
  1924. host->msg[2] = 0;
  1925. } else {
  1926. if (host->msg[2] > 2) /* > 32 bits */
  1927. return initio_msgout_reject(host);
  1928. if (host->msg[2] == 2) { /* == 32 */
  1929. host->msg[2] = 1;
  1930. } else {
  1931. if ((host->active_tc->flags & TCF_NO_WDTR) == 0) {
  1932. wdtr_done(host);
  1933. if ((host->active_tc->flags & (TCF_SYNC_DONE | TCF_NO_SYNC_NEGO)) == 0)
  1934. outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
  1935. return initio_msgin_accept(host);
  1936. }
  1937. }
  1938. }
  1939. outb(((inb(host->addr + TUL_SSignal) & (TSC_SET_ACK | 7)) | TSC_SET_ATN), host->addr + TUL_SSignal);
  1940. if (initio_msgin_accept(host) != MSG_OUT)
  1941. return host->phase;
  1942. /* WDTR msg out */
  1943. outb(MSG_EXTEND, host->addr + TUL_SFifo);
  1944. outb(2, host->addr + TUL_SFifo);
  1945. outb(3, host->addr + TUL_SFifo);
  1946. outb(host->msg[2], host->addr + TUL_SFifo);
  1947. outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
  1948. return wait_tulip(host);
  1949. }
  1950. static int initio_msgin_sync(struct initio_host * host)
  1951. {
  1952. char default_period;
  1953. default_period = initio_rate_tbl[host->active_tc->flags & TCF_SCSI_RATE];
  1954. if (host->msg[3] > MAX_OFFSET) {
  1955. host->msg[3] = MAX_OFFSET;
  1956. if (host->msg[2] < default_period) {
  1957. host->msg[2] = default_period;
  1958. return 1;
  1959. }
  1960. if (host->msg[2] >= 59) /* Change to async */
  1961. host->msg[3] = 0;
  1962. return 1;
  1963. }
  1964. /* offset requests asynchronous transfers ? */
  1965. if (host->msg[3] == 0) {
  1966. return 0;
  1967. }
  1968. if (host->msg[2] < default_period) {
  1969. host->msg[2] = default_period;
  1970. return 1;
  1971. }
  1972. if (host->msg[2] >= 59) {
  1973. host->msg[3] = 0;
  1974. return 1;
  1975. }
  1976. return 0;
  1977. }
  1978. static int wdtr_done(struct initio_host * host)
  1979. {
  1980. host->active_tc->flags &= ~TCF_SYNC_DONE;
  1981. host->active_tc->flags |= TCF_WDTR_DONE;
  1982. host->active_tc->js_period = 0;
  1983. if (host->msg[2]) /* if 16 bit */
  1984. host->active_tc->js_period |= TSC_WIDE_SCSI;
  1985. host->active_tc->sconfig0 &= ~TSC_ALT_PERIOD;
  1986. outb(host->active_tc->sconfig0, host->addr + TUL_SConfig);
  1987. outb(host->active_tc->js_period, host->addr + TUL_SPeriod);
  1988. return 1;
  1989. }
  1990. static int initio_sync_done(struct initio_host * host)
  1991. {
  1992. int i;
  1993. host->active_tc->flags |= TCF_SYNC_DONE;
  1994. if (host->msg[3]) {
  1995. host->active_tc->js_period |= host->msg[3];
  1996. for (i = 0; i < 8; i++) {
  1997. if (initio_rate_tbl[i] >= host->msg[2]) /* pick the big one */
  1998. break;
  1999. }
  2000. host->active_tc->js_period |= (i << 4);
  2001. host->active_tc->sconfig0 |= TSC_ALT_PERIOD;
  2002. }
  2003. outb(host->active_tc->sconfig0, host->addr + TUL_SConfig);
  2004. outb(host->active_tc->js_period, host->addr + TUL_SPeriod);
  2005. return -1;
  2006. }
  2007. static int initio_post_scsi_rst(struct initio_host * host)
  2008. {
  2009. struct scsi_ctrl_blk *scb;
  2010. struct target_control *active_tc;
  2011. int i;
  2012. host->active = NULL;
  2013. host->active_tc = NULL;
  2014. host->flags = 0;
  2015. while ((scb = initio_pop_busy_scb(host)) != NULL) {
  2016. scb->hastat = HOST_BAD_PHAS;
  2017. initio_append_done_scb(host, scb);
  2018. }
  2019. /* clear sync done flag */
  2020. active_tc = &host->targets[0];
  2021. for (i = 0; i < host->max_tar; active_tc++, i++) {
  2022. active_tc->flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE);
  2023. /* Initialize the sync. xfer register values to an asyn xfer */
  2024. active_tc->js_period = 0;
  2025. active_tc->sconfig0 = host->sconf1;
  2026. host->act_tags[0] = 0; /* 07/22/98 */
  2027. host->targets[i].flags &= ~TCF_BUSY; /* 07/22/98 */
  2028. } /* for */
  2029. return -1;
  2030. }
  2031. static void initio_select_atn_stop(struct initio_host * host, struct scsi_ctrl_blk * scb)
  2032. {
  2033. scb->status |= SCB_SELECT;
  2034. scb->next_state = 0x1;
  2035. host->active = scb;
  2036. host->active_tc = &host->targets[scb->target];
  2037. outb(TSC_SELATNSTOP, host->addr + TUL_SCmd);
  2038. }
  2039. static void initio_select_atn(struct initio_host * host, struct scsi_ctrl_blk * scb)
  2040. {
  2041. int i;
  2042. scb->status |= SCB_SELECT;
  2043. scb->next_state = 0x2;
  2044. outb(scb->ident, host->addr + TUL_SFifo);
  2045. for (i = 0; i < (int) scb->cdblen; i++)
  2046. outb(scb->cdb[i], host->addr + TUL_SFifo);
  2047. host->active_tc = &host->targets[scb->target];
  2048. host->active = scb;
  2049. outb(TSC_SEL_ATN, host->addr + TUL_SCmd);
  2050. }
  2051. static void initio_select_atn3(struct initio_host * host, struct scsi_ctrl_blk * scb)
  2052. {
  2053. int i;
  2054. scb->status |= SCB_SELECT;
  2055. scb->next_state = 0x2;
  2056. outb(scb->ident, host->addr + TUL_SFifo);
  2057. outb(scb->tagmsg, host->addr + TUL_SFifo);
  2058. outb(scb->tagid, host->addr + TUL_SFifo);
  2059. for (i = 0; i < scb->cdblen; i++)
  2060. outb(scb->cdb[i], host->addr + TUL_SFifo);
  2061. host->active_tc = &host->targets[scb->target];
  2062. host->active = scb;
  2063. outb(TSC_SEL_ATN3, host->addr + TUL_SCmd);
  2064. }
  2065. /**
  2066. * initio_bus_device_reset - SCSI Bus Device Reset
  2067. * @host: InitIO host to reset
  2068. *
  2069. * Perform a device reset and abort all pending SCBs for the
  2070. * victim device
  2071. */
  2072. int initio_bus_device_reset(struct initio_host * host)
  2073. {
  2074. struct scsi_ctrl_blk *scb = host->active;
  2075. struct target_control *active_tc = host->active_tc;
  2076. struct scsi_ctrl_blk *tmp, *prev;
  2077. u8 tar;
  2078. if (host->phase != MSG_OUT)
  2079. return int_initio_bad_seq(host); /* Unexpected phase */
  2080. initio_unlink_pend_scb(host, scb);
  2081. initio_release_scb(host, scb);
  2082. tar = scb->target; /* target */
  2083. active_tc->flags &= ~(TCF_SYNC_DONE | TCF_WDTR_DONE | TCF_BUSY);
  2084. /* clr sync. nego & WDTR flags 07/22/98 */
  2085. /* abort all SCB with same target */
  2086. prev = tmp = host->first_busy; /* Check Busy queue */
  2087. while (tmp != NULL) {
  2088. if (tmp->target == tar) {
  2089. /* unlink it */
  2090. if (tmp == host->first_busy) {
  2091. if ((host->first_busy = tmp->next) == NULL)
  2092. host->last_busy = NULL;
  2093. } else {
  2094. prev->next = tmp->next;
  2095. if (tmp == host->last_busy)
  2096. host->last_busy = prev;
  2097. }
  2098. tmp->hastat = HOST_ABORTED;
  2099. initio_append_done_scb(host, tmp);
  2100. }
  2101. /* Previous haven't change */
  2102. else {
  2103. prev = tmp;
  2104. }
  2105. tmp = tmp->next;
  2106. }
  2107. outb(MSG_DEVRST, host->addr + TUL_SFifo);
  2108. outb(TSC_XF_FIFO_OUT, host->addr + TUL_SCmd);
  2109. return initio_wait_disc(host);
  2110. }
  2111. static int initio_msgin_accept(struct initio_host * host)
  2112. {
  2113. outb(TSC_MSG_ACCEPT, host->addr + TUL_SCmd);
  2114. return wait_tulip(host);
  2115. }
  2116. static int wait_tulip(struct initio_host * host)
  2117. {
  2118. while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0))
  2119. & TSS_INT_PENDING))
  2120. cpu_relax();
  2121. host->jsint = inb(host->addr + TUL_SInt);
  2122. host->phase = host->jsstatus0 & TSS_PH_MASK;
  2123. host->jsstatus1 = inb(host->addr + TUL_SStatus1);
  2124. if (host->jsint & TSS_RESEL_INT) /* if SCSI bus reset detected */
  2125. return int_initio_resel(host);
  2126. if (host->jsint & TSS_SEL_TIMEOUT) /* if selected/reselected timeout interrupt */
  2127. return int_initio_busfree(host);
  2128. if (host->jsint & TSS_SCSIRST_INT) /* if SCSI bus reset detected */
  2129. return int_initio_scsi_rst(host);
  2130. if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */
  2131. if (host->flags & HCF_EXPECT_DONE_DISC) {
  2132. outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
  2133. initio_unlink_busy_scb(host, host->active);
  2134. host->active->hastat = 0;
  2135. initio_append_done_scb(host, host->active);
  2136. host->active = NULL;
  2137. host->active_tc = NULL;
  2138. host->flags &= ~HCF_EXPECT_DONE_DISC;
  2139. outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
  2140. outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
  2141. return -1;
  2142. }
  2143. if (host->flags & HCF_EXPECT_DISC) {
  2144. outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
  2145. host->active = NULL;
  2146. host->active_tc = NULL;
  2147. host->flags &= ~HCF_EXPECT_DISC;
  2148. outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
  2149. outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
  2150. return -1;
  2151. }
  2152. return int_initio_busfree(host);
  2153. }
  2154. /* The old code really does the below. Can probably be removed */
  2155. if (host->jsint & (TSS_FUNC_COMP | TSS_BUS_SERV))
  2156. return host->phase;
  2157. return host->phase;
  2158. }
  2159. static int initio_wait_disc(struct initio_host * host)
  2160. {
  2161. while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0)) & TSS_INT_PENDING))
  2162. cpu_relax();
  2163. host->jsint = inb(host->addr + TUL_SInt);
  2164. if (host->jsint & TSS_SCSIRST_INT) /* if SCSI bus reset detected */
  2165. return int_initio_scsi_rst(host);
  2166. if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */
  2167. outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
  2168. outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
  2169. outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
  2170. host->active = NULL;
  2171. return -1;
  2172. }
  2173. return initio_bad_seq(host);
  2174. }
  2175. static int initio_wait_done_disc(struct initio_host * host)
  2176. {
  2177. while (!((host->jsstatus0 = inb(host->addr + TUL_SStatus0))
  2178. & TSS_INT_PENDING))
  2179. cpu_relax();
  2180. host->jsint = inb(host->addr + TUL_SInt);
  2181. if (host->jsint & TSS_SCSIRST_INT) /* if SCSI bus reset detected */
  2182. return int_initio_scsi_rst(host);
  2183. if (host->jsint & TSS_DISC_INT) { /* BUS disconnection */
  2184. outb(TSC_FLUSH_FIFO, host->addr + TUL_SCtrl0); /* Flush SCSI FIFO */
  2185. outb(TSC_INITDEFAULT, host->addr + TUL_SConfig);
  2186. outb(TSC_HW_RESELECT, host->addr + TUL_SCtrl1); /* Enable HW reselect */
  2187. initio_unlink_busy_scb(host, host->active);
  2188. initio_append_done_scb(host, host->active);
  2189. host->active = NULL;
  2190. return -1;
  2191. }
  2192. return initio_bad_seq(host);
  2193. }
  2194. /**
  2195. * i91u_intr - IRQ handler
  2196. * @irqno: IRQ number
  2197. * @dev_id: IRQ identifier
  2198. *
  2199. * Take the relevant locks and then invoke the actual isr processing
  2200. * code under the lock.
  2201. */
  2202. static irqreturn_t i91u_intr(int irqno, void *dev_id)
  2203. {
  2204. struct Scsi_Host *dev = dev_id;
  2205. unsigned long flags;
  2206. int r;
  2207. spin_lock_irqsave(dev->host_lock, flags);
  2208. r = initio_isr((struct initio_host *)dev->hostdata);
  2209. spin_unlock_irqrestore(dev->host_lock, flags);
  2210. if (r)
  2211. return IRQ_HANDLED;
  2212. else
  2213. return IRQ_NONE;
  2214. }
  2215. /**
  2216. * initio_build_scb - Build the mappings and SCB
  2217. * @host: InitIO host taking the command
  2218. * @cblk: Firmware command block
  2219. * @cmnd: SCSI midlayer command block
  2220. *
  2221. * Translate the abstract SCSI command into a firmware command block
  2222. * suitable for feeding to the InitIO host controller. This also requires
  2223. * we build the scatter gather lists and ensure they are mapped properly.
  2224. */
  2225. static void initio_build_scb(struct initio_host * host, struct scsi_ctrl_blk * cblk, struct scsi_cmnd * cmnd)
  2226. { /* Create corresponding SCB */
  2227. struct scatterlist *sglist;
  2228. struct sg_entry *sg; /* Pointer to SG list */
  2229. int i, nseg;
  2230. long total_len;
  2231. dma_addr_t dma_addr;
  2232. /* Fill in the command headers */
  2233. cblk->post = i91uSCBPost; /* i91u's callback routine */
  2234. cblk->srb = cmnd;
  2235. cblk->opcode = ExecSCSI;
  2236. cblk->flags = SCF_POST; /* After SCSI done, call post routine */
  2237. cblk->target = cmnd->device->id;
  2238. cblk->lun = cmnd->device->lun;
  2239. cblk->ident = cmnd->device->lun | DISC_ALLOW;
  2240. cblk->flags |= SCF_SENSE; /* Turn on auto request sense */
  2241. /* Map the sense buffer into bus memory */
  2242. dma_addr = dma_map_single(&host->pci_dev->dev, cmnd->sense_buffer,
  2243. SENSE_SIZE, DMA_FROM_DEVICE);
  2244. cblk->senseptr = (u32)dma_addr;
  2245. cblk->senselen = SENSE_SIZE;
  2246. cmnd->SCp.ptr = (char *)(unsigned long)dma_addr;
  2247. cblk->cdblen = cmnd->cmd_len;
  2248. /* Clear the returned status */
  2249. cblk->hastat = 0;
  2250. cblk->tastat = 0;
  2251. /* Command the command */
  2252. memcpy(cblk->cdb, cmnd->cmnd, cmnd->cmd_len);
  2253. /* Set up tags */
  2254. if (cmnd->device->tagged_supported) { /* Tag Support */
  2255. cblk->tagmsg = SIMPLE_QUEUE_TAG; /* Do simple tag only */
  2256. } else {
  2257. cblk->tagmsg = 0; /* No tag support */
  2258. }
  2259. /* todo handle map_sg error */
  2260. nseg = scsi_dma_map(cmnd);
  2261. BUG_ON(nseg < 0);
  2262. if (nseg) {
  2263. dma_addr = dma_map_single(&host->pci_dev->dev, &cblk->sglist[0],
  2264. sizeof(struct sg_entry) * TOTAL_SG_ENTRY,
  2265. DMA_BIDIRECTIONAL);
  2266. cblk->bufptr = (u32)dma_addr;
  2267. cmnd->SCp.dma_handle = dma_addr;
  2268. cblk->sglen = nseg;
  2269. cblk->flags |= SCF_SG; /* Turn on SG list flag */
  2270. total_len = 0;
  2271. sg = &cblk->sglist[0];
  2272. scsi_for_each_sg(cmnd, sglist, cblk->sglen, i) {
  2273. sg->data = cpu_to_le32((u32)sg_dma_address(sglist));
  2274. sg->len = cpu_to_le32((u32)sg_dma_len(sglist));
  2275. total_len += sg_dma_len(sglist);
  2276. ++sg;
  2277. }
  2278. cblk->buflen = (scsi_bufflen(cmnd) > total_len) ?
  2279. total_len : scsi_bufflen(cmnd);
  2280. } else { /* No data transfer required */
  2281. cblk->buflen = 0;
  2282. cblk->sglen = 0;
  2283. }
  2284. }
  2285. /**
  2286. * i91u_queuecommand - Queue a new command if possible
  2287. * @cmd: SCSI command block from the mid layer
  2288. * @done: Completion handler
  2289. *
  2290. * Attempts to queue a new command with the host adapter. Will return
  2291. * zero if successful or indicate a host busy condition if not (which
  2292. * will cause the mid layer to call us again later with the command)
  2293. */
  2294. static int i91u_queuecommand_lck(struct scsi_cmnd *cmd,
  2295. void (*done)(struct scsi_cmnd *))
  2296. {
  2297. struct initio_host *host = (struct initio_host *) cmd->device->host->hostdata;
  2298. struct scsi_ctrl_blk *cmnd;
  2299. cmd->scsi_done = done;
  2300. cmnd = initio_alloc_scb(host);
  2301. if (!cmnd)
  2302. return SCSI_MLQUEUE_HOST_BUSY;
  2303. initio_build_scb(host, cmnd, cmd);
  2304. initio_exec_scb(host, cmnd);
  2305. return 0;
  2306. }
  2307. static DEF_SCSI_QCMD(i91u_queuecommand)
  2308. /**
  2309. * i91u_bus_reset - reset the SCSI bus
  2310. * @cmnd: Command block we want to trigger the reset for
  2311. *
  2312. * Initiate a SCSI bus reset sequence
  2313. */
  2314. static int i91u_bus_reset(struct scsi_cmnd * cmnd)
  2315. {
  2316. struct initio_host *host;
  2317. host = (struct initio_host *) cmnd->device->host->hostdata;
  2318. spin_lock_irq(cmnd->device->host->host_lock);
  2319. initio_reset_scsi(host, 0);
  2320. spin_unlock_irq(cmnd->device->host->host_lock);
  2321. return SUCCESS;
  2322. }
  2323. /**
  2324. * i91u_biospararm - return the "logical geometry
  2325. * @sdev: SCSI device
  2326. * @dev; Matching block device
  2327. * @capacity: Sector size of drive
  2328. * @info_array: Return space for BIOS geometry
  2329. *
  2330. * Map the device geometry in a manner compatible with the host
  2331. * controller BIOS behaviour.
  2332. *
  2333. * FIXME: limited to 2^32 sector devices.
  2334. */
  2335. static int i91u_biosparam(struct scsi_device *sdev, struct block_device *dev,
  2336. sector_t capacity, int *info_array)
  2337. {
  2338. struct initio_host *host; /* Point to Host adapter control block */
  2339. struct target_control *tc;
  2340. host = (struct initio_host *) sdev->host->hostdata;
  2341. tc = &host->targets[sdev->id];
  2342. if (tc->heads) {
  2343. info_array[0] = tc->heads;
  2344. info_array[1] = tc->sectors;
  2345. info_array[2] = (unsigned long)capacity / tc->heads / tc->sectors;
  2346. } else {
  2347. if (tc->drv_flags & TCF_DRV_255_63) {
  2348. info_array[0] = 255;
  2349. info_array[1] = 63;
  2350. info_array[2] = (unsigned long)capacity / 255 / 63;
  2351. } else {
  2352. info_array[0] = 64;
  2353. info_array[1] = 32;
  2354. info_array[2] = (unsigned long)capacity >> 11;
  2355. }
  2356. }
  2357. #if defined(DEBUG_BIOSPARAM)
  2358. if (i91u_debug & debug_biosparam) {
  2359. printk("bios geometry: head=%d, sec=%d, cyl=%d\n",
  2360. info_array[0], info_array[1], info_array[2]);
  2361. printk("WARNING: check, if the bios geometry is correct.\n");
  2362. }
  2363. #endif
  2364. return 0;
  2365. }
  2366. /**
  2367. * i91u_unmap_scb - Unmap a command
  2368. * @pci_dev: PCI device the command is for
  2369. * @cmnd: The command itself
  2370. *
  2371. * Unmap any PCI mapping/IOMMU resources allocated when the command
  2372. * was mapped originally as part of initio_build_scb
  2373. */
  2374. static void i91u_unmap_scb(struct pci_dev *pci_dev, struct scsi_cmnd *cmnd)
  2375. {
  2376. /* auto sense buffer */
  2377. if (cmnd->SCp.ptr) {
  2378. dma_unmap_single(&pci_dev->dev,
  2379. (dma_addr_t)((unsigned long)cmnd->SCp.ptr),
  2380. SENSE_SIZE, DMA_FROM_DEVICE);
  2381. cmnd->SCp.ptr = NULL;
  2382. }
  2383. /* request buffer */
  2384. if (scsi_sg_count(cmnd)) {
  2385. dma_unmap_single(&pci_dev->dev, cmnd->SCp.dma_handle,
  2386. sizeof(struct sg_entry) * TOTAL_SG_ENTRY,
  2387. DMA_BIDIRECTIONAL);
  2388. scsi_dma_unmap(cmnd);
  2389. }
  2390. }
  2391. /**
  2392. * i91uSCBPost - SCSI callback
  2393. * @host: Pointer to host adapter control block.
  2394. * @cmnd: Pointer to SCSI control block.
  2395. *
  2396. * This is callback routine be called when tulip finish one
  2397. * SCSI command.
  2398. */
  2399. static void i91uSCBPost(u8 * host_mem, u8 * cblk_mem)
  2400. {
  2401. struct scsi_cmnd *cmnd; /* Pointer to SCSI request block */
  2402. struct initio_host *host;
  2403. struct scsi_ctrl_blk *cblk;
  2404. host = (struct initio_host *) host_mem;
  2405. cblk = (struct scsi_ctrl_blk *) cblk_mem;
  2406. if ((cmnd = cblk->srb) == NULL) {
  2407. printk(KERN_ERR "i91uSCBPost: SRB pointer is empty\n");
  2408. WARN_ON(1);
  2409. initio_release_scb(host, cblk); /* Release SCB for current channel */
  2410. return;
  2411. }
  2412. /*
  2413. * Remap the firmware error status into a mid layer one
  2414. */
  2415. switch (cblk->hastat) {
  2416. case 0x0:
  2417. case 0xa: /* Linked command complete without error and linked normally */
  2418. case 0xb: /* Linked command complete without error interrupt generated */
  2419. cblk->hastat = 0;
  2420. break;
  2421. case 0x11: /* Selection time out-The initiator selection or target
  2422. reselection was not complete within the SCSI Time out period */
  2423. cblk->hastat = DID_TIME_OUT;
  2424. break;
  2425. case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus
  2426. phase sequence was requested by the target. The host adapter
  2427. will generate a SCSI Reset Condition, notifying the host with
  2428. a SCRD interrupt */
  2429. cblk->hastat = DID_RESET;
  2430. break;
  2431. case 0x1a: /* SCB Aborted. 07/21/98 */
  2432. cblk->hastat = DID_ABORT;
  2433. break;
  2434. case 0x12: /* Data overrun/underrun-The target attempted to transfer more data
  2435. than was allocated by the Data Length field or the sum of the
  2436. Scatter / Gather Data Length fields. */
  2437. case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
  2438. case 0x16: /* Invalid SCB Operation Code. */
  2439. default:
  2440. printk("ini9100u: %x %x\n", cblk->hastat, cblk->tastat);
  2441. cblk->hastat = DID_ERROR; /* Couldn't find any better */
  2442. break;
  2443. }
  2444. cmnd->result = cblk->tastat | (cblk->hastat << 16);
  2445. i91u_unmap_scb(host->pci_dev, cmnd);
  2446. cmnd->scsi_done(cmnd); /* Notify system DONE */
  2447. initio_release_scb(host, cblk); /* Release SCB for current channel */
  2448. }
  2449. static struct scsi_host_template initio_template = {
  2450. .proc_name = "INI9100U",
  2451. .name = "Initio INI-9X00U/UW SCSI device driver",
  2452. .queuecommand = i91u_queuecommand,
  2453. .eh_bus_reset_handler = i91u_bus_reset,
  2454. .bios_param = i91u_biosparam,
  2455. .can_queue = MAX_TARGETS * i91u_MAXQUEUE,
  2456. .this_id = 1,
  2457. .sg_tablesize = SG_ALL,
  2458. .use_clustering = ENABLE_CLUSTERING,
  2459. };
  2460. static int initio_probe_one(struct pci_dev *pdev,
  2461. const struct pci_device_id *id)
  2462. {
  2463. struct Scsi_Host *shost;
  2464. struct initio_host *host;
  2465. u32 reg;
  2466. u16 bios_seg;
  2467. struct scsi_ctrl_blk *scb, *tmp, *prev = NULL /* silence gcc */;
  2468. int num_scb, i, error;
  2469. error = pci_enable_device(pdev);
  2470. if (error)
  2471. return error;
  2472. pci_read_config_dword(pdev, 0x44, (u32 *) & reg);
  2473. bios_seg = (u16) (reg & 0xFF);
  2474. if (((reg & 0xFF00) >> 8) == 0xFF)
  2475. reg = 0;
  2476. bios_seg = (bios_seg << 8) + ((u16) ((reg & 0xFF00) >> 8));
  2477. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
  2478. printk(KERN_WARNING "i91u: Could not set 32 bit DMA mask\n");
  2479. error = -ENODEV;
  2480. goto out_disable_device;
  2481. }
  2482. shost = scsi_host_alloc(&initio_template, sizeof(struct initio_host));
  2483. if (!shost) {
  2484. printk(KERN_WARNING "initio: Could not allocate host structure.\n");
  2485. error = -ENOMEM;
  2486. goto out_disable_device;
  2487. }
  2488. host = (struct initio_host *)shost->hostdata;
  2489. memset(host, 0, sizeof(struct initio_host));
  2490. host->addr = pci_resource_start(pdev, 0);
  2491. host->bios_addr = bios_seg;
  2492. if (!request_region(host->addr, 256, "i91u")) {
  2493. printk(KERN_WARNING "initio: I/O port range 0x%x is busy.\n", host->addr);
  2494. error = -ENODEV;
  2495. goto out_host_put;
  2496. }
  2497. if (initio_tag_enable) /* 1.01i */
  2498. num_scb = MAX_TARGETS * i91u_MAXQUEUE;
  2499. else
  2500. num_scb = MAX_TARGETS + 3; /* 1-tape, 1-CD_ROM, 1- extra */
  2501. for (; num_scb >= MAX_TARGETS + 3; num_scb--) {
  2502. i = num_scb * sizeof(struct scsi_ctrl_blk);
  2503. if ((scb = kzalloc(i, GFP_DMA)) != NULL)
  2504. break;
  2505. }
  2506. if (!scb) {
  2507. printk(KERN_WARNING "initio: Cannot allocate SCB array.\n");
  2508. error = -ENOMEM;
  2509. goto out_release_region;
  2510. }
  2511. host->pci_dev = pdev;
  2512. host->semaph = 1;
  2513. spin_lock_init(&host->semaph_lock);
  2514. host->num_scbs = num_scb;
  2515. host->scb = scb;
  2516. host->next_pending = scb;
  2517. host->next_avail = scb;
  2518. for (i = 0, tmp = scb; i < num_scb; i++, tmp++) {
  2519. tmp->tagid = i;
  2520. if (i != 0)
  2521. prev->next = tmp;
  2522. prev = tmp;
  2523. }
  2524. prev->next = NULL;
  2525. host->scb_end = tmp;
  2526. host->first_avail = scb;
  2527. host->last_avail = prev;
  2528. spin_lock_init(&host->avail_lock);
  2529. initio_init(host, phys_to_virt(((u32)bios_seg << 4)));
  2530. host->jsstatus0 = 0;
  2531. shost->io_port = host->addr;
  2532. shost->n_io_port = 0xff;
  2533. shost->can_queue = num_scb; /* 03/05/98 */
  2534. shost->unique_id = host->addr;
  2535. shost->max_id = host->max_tar;
  2536. shost->max_lun = 32; /* 10/21/97 */
  2537. shost->irq = pdev->irq;
  2538. shost->this_id = host->scsi_id; /* Assign HCS index */
  2539. shost->base = host->addr;
  2540. shost->sg_tablesize = TOTAL_SG_ENTRY;
  2541. error = request_irq(pdev->irq, i91u_intr, IRQF_SHARED, "i91u", shost);
  2542. if (error < 0) {
  2543. printk(KERN_WARNING "initio: Unable to request IRQ %d\n", pdev->irq);
  2544. goto out_free_scbs;
  2545. }
  2546. pci_set_drvdata(pdev, shost);
  2547. error = scsi_add_host(shost, &pdev->dev);
  2548. if (error)
  2549. goto out_free_irq;
  2550. scsi_scan_host(shost);
  2551. return 0;
  2552. out_free_irq:
  2553. free_irq(pdev->irq, shost);
  2554. out_free_scbs:
  2555. kfree(host->scb);
  2556. out_release_region:
  2557. release_region(host->addr, 256);
  2558. out_host_put:
  2559. scsi_host_put(shost);
  2560. out_disable_device:
  2561. pci_disable_device(pdev);
  2562. return error;
  2563. }
  2564. /**
  2565. * initio_remove_one - control shutdown
  2566. * @pdev: PCI device being released
  2567. *
  2568. * Release the resources assigned to this adapter after it has
  2569. * finished being used.
  2570. */
  2571. static void initio_remove_one(struct pci_dev *pdev)
  2572. {
  2573. struct Scsi_Host *host = pci_get_drvdata(pdev);
  2574. struct initio_host *s = (struct initio_host *)host->hostdata;
  2575. scsi_remove_host(host);
  2576. free_irq(pdev->irq, host);
  2577. release_region(s->addr, 256);
  2578. scsi_host_put(host);
  2579. pci_disable_device(pdev);
  2580. }
  2581. MODULE_LICENSE("GPL");
  2582. static struct pci_device_id initio_pci_tbl[] = {
  2583. {PCI_VENDOR_ID_INIT, 0x9500, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  2584. {PCI_VENDOR_ID_INIT, 0x9400, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  2585. {PCI_VENDOR_ID_INIT, 0x9401, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  2586. {PCI_VENDOR_ID_INIT, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  2587. {PCI_VENDOR_ID_DOMEX, 0x0002, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  2588. {0,}
  2589. };
  2590. MODULE_DEVICE_TABLE(pci, initio_pci_tbl);
  2591. static struct pci_driver initio_pci_driver = {
  2592. .name = "initio",
  2593. .id_table = initio_pci_tbl,
  2594. .probe = initio_probe_one,
  2595. .remove = initio_remove_one,
  2596. };
  2597. static int __init initio_init_driver(void)
  2598. {
  2599. return pci_register_driver(&initio_pci_driver);
  2600. }
  2601. static void __exit initio_exit_driver(void)
  2602. {
  2603. pci_unregister_driver(&initio_pci_driver);
  2604. }
  2605. MODULE_DESCRIPTION("Initio INI-9X00U/UW SCSI device driver");
  2606. MODULE_AUTHOR("Initio Corporation");
  2607. MODULE_LICENSE("GPL");
  2608. module_init(initio_init_driver);
  2609. module_exit(initio_exit_driver);