initio.c 81 KB

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