initio.c 81 KB

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