initio.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997
  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);