initio.c 80 KB

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