initio.c 80 KB

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