NCR5380.c 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847
  1. /*
  2. * NCR 5380 generic driver routines. These should make it *trivial*
  3. * to implement 5380 SCSI drivers under Linux with a non-trantor
  4. * architecture.
  5. *
  6. * Note that these routines also work with NR53c400 family chips.
  7. *
  8. * Copyright 1993, Drew Eckhardt
  9. * Visionary Computing
  10. * (Unix and Linux consulting and custom programming)
  11. * drew@colorado.edu
  12. * +1 (303) 666-5836
  13. *
  14. * DISTRIBUTION RELEASE 6.
  15. *
  16. * For more information, please consult
  17. *
  18. * NCR 5380 Family
  19. * SCSI Protocol Controller
  20. * Databook
  21. *
  22. * NCR Microelectronics
  23. * 1635 Aeroplaza Drive
  24. * Colorado Springs, CO 80916
  25. * 1+ (719) 578-3400
  26. * 1+ (800) 334-5454
  27. */
  28. /*
  29. * $Log: NCR5380.c,v $
  30. * Revision 1.10 1998/9/2 Alan Cox
  31. * (alan@lxorguk.ukuu.org.uk)
  32. * Fixed up the timer lockups reported so far. Things still suck. Looking
  33. * forward to 2.3 and per device request queues. Then it'll be possible to
  34. * SMP thread this beast and improve life no end.
  35. * Revision 1.9 1997/7/27 Ronald van Cuijlenborg
  36. * (ronald.van.cuijlenborg@tip.nl or nutty@dds.nl)
  37. * (hopefully) fixed and enhanced USLEEP
  38. * added support for DTC3181E card (for Mustek scanner)
  39. *
  40. * Revision 1.8 Ingmar Baumgart
  41. * (ingmar@gonzo.schwaben.de)
  42. * added support for NCR53C400a card
  43. *
  44. * Revision 1.7 1996/3/2 Ray Van Tassle (rayvt@comm.mot.com)
  45. * added proc_info
  46. * added support needed for DTC 3180/3280
  47. * fixed a couple of bugs
  48. *
  49. * Revision 1.5 1994/01/19 09:14:57 drew
  50. * Fixed udelay() hack that was being used on DATAOUT phases
  51. * instead of a proper wait for the final handshake.
  52. *
  53. * Revision 1.4 1994/01/19 06:44:25 drew
  54. * *** empty log message ***
  55. *
  56. * Revision 1.3 1994/01/19 05:24:40 drew
  57. * Added support for TCR LAST_BYTE_SENT bit.
  58. *
  59. * Revision 1.2 1994/01/15 06:14:11 drew
  60. * REAL DMA support, bug fixes.
  61. *
  62. * Revision 1.1 1994/01/15 06:00:54 drew
  63. * Initial revision
  64. *
  65. */
  66. /*
  67. * Further development / testing that should be done :
  68. * 1. Cleanup the NCR5380_transfer_dma function and DMA operation complete
  69. * code so that everything does the same thing that's done at the
  70. * end of a pseudo-DMA read operation.
  71. *
  72. * 2. Fix REAL_DMA (interrupt driven, polled works fine) -
  73. * basically, transfer size needs to be reduced by one
  74. * and the last byte read as is done with PSEUDO_DMA.
  75. *
  76. * 4. Test SCSI-II tagged queueing (I have no devices which support
  77. * tagged queueing)
  78. *
  79. * 5. Test linked command handling code after Eric is ready with
  80. * the high level code.
  81. */
  82. #include <scsi/scsi_dbg.h>
  83. #include <scsi/scsi_transport_spi.h>
  84. #ifndef NDEBUG
  85. #define NDEBUG 0
  86. #endif
  87. #ifndef NDEBUG_ABORT
  88. #define NDEBUG_ABORT 0
  89. #endif
  90. #if (NDEBUG & NDEBUG_LISTS)
  91. #define LIST(x,y) {printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); }
  92. #define REMOVE(w,x,y,z) {printk("LINE:%d Removing: %p->%p %p->%p \n", __LINE__, (void*)(w), (void*)(x), (void*)(y), (void*)(z)); if ((x)==(y)) udelay(5); }
  93. #else
  94. #define LIST(x,y)
  95. #define REMOVE(w,x,y,z)
  96. #endif
  97. #ifndef notyet
  98. #undef LINKED
  99. #undef REAL_DMA
  100. #endif
  101. #ifdef REAL_DMA_POLL
  102. #undef READ_OVERRUNS
  103. #define READ_OVERRUNS
  104. #endif
  105. #ifdef BOARD_REQUIRES_NO_DELAY
  106. #define io_recovery_delay(x)
  107. #else
  108. #define io_recovery_delay(x) udelay(x)
  109. #endif
  110. /*
  111. * Design
  112. *
  113. * This is a generic 5380 driver. To use it on a different platform,
  114. * one simply writes appropriate system specific macros (ie, data
  115. * transfer - some PC's will use the I/O bus, 68K's must use
  116. * memory mapped) and drops this file in their 'C' wrapper.
  117. *
  118. * (Note from hch: unfortunately it was not enough for the different
  119. * m68k folks and instead of improving this driver they copied it
  120. * and hacked it up for their needs. As a consequence they lost
  121. * most updates to this driver. Maybe someone will fix all these
  122. * drivers to use a common core one day..)
  123. *
  124. * As far as command queueing, two queues are maintained for
  125. * each 5380 in the system - commands that haven't been issued yet,
  126. * and commands that are currently executing. This means that an
  127. * unlimited number of commands may be queued, letting
  128. * more commands propagate from the higher driver levels giving higher
  129. * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported,
  130. * allowing multiple commands to propagate all the way to a SCSI-II device
  131. * while a command is already executing.
  132. *
  133. *
  134. * Issues specific to the NCR5380 :
  135. *
  136. * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead
  137. * piece of hardware that requires you to sit in a loop polling for
  138. * the REQ signal as long as you are connected. Some devices are
  139. * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect
  140. * while doing long seek operations.
  141. *
  142. * The workaround for this is to keep track of devices that have
  143. * disconnected. If the device hasn't disconnected, for commands that
  144. * should disconnect, we do something like
  145. *
  146. * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
  147. *
  148. * Some tweaking of N and M needs to be done. An algorithm based
  149. * on "time to data" would give the best results as long as short time
  150. * to datas (ie, on the same track) were considered, however these
  151. * broken devices are the exception rather than the rule and I'd rather
  152. * spend my time optimizing for the normal case.
  153. *
  154. * Architecture :
  155. *
  156. * At the heart of the design is a coroutine, NCR5380_main,
  157. * which is started from a workqueue for each NCR5380 host in the
  158. * system. It attempts to establish I_T_L or I_T_L_Q nexuses by
  159. * removing the commands from the issue queue and calling
  160. * NCR5380_select() if a nexus is not established.
  161. *
  162. * Once a nexus is established, the NCR5380_information_transfer()
  163. * phase goes through the various phases as instructed by the target.
  164. * if the target goes into MSG IN and sends a DISCONNECT message,
  165. * the command structure is placed into the per instance disconnected
  166. * queue, and NCR5380_main tries to find more work. If the target is
  167. * idle for too long, the system will try to sleep.
  168. *
  169. * If a command has disconnected, eventually an interrupt will trigger,
  170. * calling NCR5380_intr() which will in turn call NCR5380_reselect
  171. * to reestablish a nexus. This will run main if necessary.
  172. *
  173. * On command termination, the done function will be called as
  174. * appropriate.
  175. *
  176. * SCSI pointers are maintained in the SCp field of SCSI command
  177. * structures, being initialized after the command is connected
  178. * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
  179. * Note that in violation of the standard, an implicit SAVE POINTERS operation
  180. * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
  181. */
  182. /*
  183. * Using this file :
  184. * This file a skeleton Linux SCSI driver for the NCR 5380 series
  185. * of chips. To use it, you write an architecture specific functions
  186. * and macros and include this file in your driver.
  187. *
  188. * These macros control options :
  189. * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be
  190. * defined.
  191. *
  192. * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
  193. * for commands that return with a CHECK CONDITION status.
  194. *
  195. * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential
  196. * transceivers.
  197. *
  198. * DONT_USE_INTR - if defined, never use interrupts, even if we probe or
  199. * override-configure an IRQ.
  200. *
  201. * LIMIT_TRANSFERSIZE - if defined, limit the pseudo-dma transfers to 512
  202. * bytes at a time. Since interrupts are disabled by default during
  203. * these transfers, we might need this to give reasonable interrupt
  204. * service time if the transfer size gets too large.
  205. *
  206. * LINKED - if defined, linked commands are supported.
  207. *
  208. * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases.
  209. *
  210. * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
  211. *
  212. * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't
  213. * rely on phase mismatch and EOP interrupts to determine end
  214. * of phase.
  215. *
  216. * UNSAFE - leave interrupts enabled during pseudo-DMA transfers. You
  217. * only really want to use this if you're having a problem with
  218. * dropped characters during high speed communications, and even
  219. * then, you're going to be better off twiddling with transfersize
  220. * in the high level code.
  221. *
  222. * Defaults for these will be provided although the user may want to adjust
  223. * these to allocate CPU resources to the SCSI driver or "real" code.
  224. *
  225. * USLEEP_SLEEP - amount of time, in jiffies, to sleep
  226. *
  227. * USLEEP_POLL - amount of time, in jiffies, to poll
  228. *
  229. * These macros MUST be defined :
  230. * NCR5380_local_declare() - declare any local variables needed for your
  231. * transfer routines.
  232. *
  233. * NCR5380_setup(instance) - initialize any local variables needed from a given
  234. * instance of the host adapter for NCR5380_{read,write,pread,pwrite}
  235. *
  236. * NCR5380_read(register) - read from the specified register
  237. *
  238. * NCR5380_write(register, value) - write to the specific register
  239. *
  240. * NCR5380_implementation_fields - additional fields needed for this
  241. * specific implementation of the NCR5380
  242. *
  243. * Either real DMA *or* pseudo DMA may be implemented
  244. * REAL functions :
  245. * NCR5380_REAL_DMA should be defined if real DMA is to be used.
  246. * Note that the DMA setup functions should return the number of bytes
  247. * that they were able to program the controller for.
  248. *
  249. * Also note that generic i386/PC versions of these macros are
  250. * available as NCR5380_i386_dma_write_setup,
  251. * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
  252. *
  253. * NCR5380_dma_write_setup(instance, src, count) - initialize
  254. * NCR5380_dma_read_setup(instance, dst, count) - initialize
  255. * NCR5380_dma_residual(instance); - residual count
  256. *
  257. * PSEUDO functions :
  258. * NCR5380_pwrite(instance, src, count)
  259. * NCR5380_pread(instance, dst, count);
  260. *
  261. * The generic driver is initialized by calling NCR5380_init(instance),
  262. * after setting the appropriate host specific fields and ID. If the
  263. * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
  264. * possible) function may be used.
  265. */
  266. static int do_abort(struct Scsi_Host *host);
  267. static void do_reset(struct Scsi_Host *host);
  268. /*
  269. * initialize_SCp - init the scsi pointer field
  270. * @cmd: command block to set up
  271. *
  272. * Set up the internal fields in the SCSI command.
  273. */
  274. static __inline__ void initialize_SCp(Scsi_Cmnd * cmd)
  275. {
  276. /*
  277. * Initialize the Scsi Pointer field so that all of the commands in the
  278. * various queues are valid.
  279. */
  280. if (scsi_bufflen(cmd)) {
  281. cmd->SCp.buffer = scsi_sglist(cmd);
  282. cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
  283. cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
  284. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  285. } else {
  286. cmd->SCp.buffer = NULL;
  287. cmd->SCp.buffers_residual = 0;
  288. cmd->SCp.ptr = NULL;
  289. cmd->SCp.this_residual = 0;
  290. }
  291. }
  292. /**
  293. * NCR5380_poll_politely - wait for NCR5380 status bits
  294. * @instance: controller to poll
  295. * @reg: 5380 register to poll
  296. * @bit: Bitmask to check
  297. * @val: Value required to exit
  298. *
  299. * Polls the NCR5380 in a reasonably efficient manner waiting for
  300. * an event to occur, after a short quick poll we begin giving the
  301. * CPU back in non IRQ contexts
  302. *
  303. * Returns the value of the register or a negative error code.
  304. */
  305. static int NCR5380_poll_politely(struct Scsi_Host *instance, int reg, int bit, int val, int t)
  306. {
  307. NCR5380_local_declare();
  308. int n = 500; /* At about 8uS a cycle for the cpu access */
  309. unsigned long end = jiffies + t;
  310. int r;
  311. NCR5380_setup(instance);
  312. while( n-- > 0)
  313. {
  314. r = NCR5380_read(reg);
  315. if((r & bit) == val)
  316. return 0;
  317. cpu_relax();
  318. }
  319. /* t time yet ? */
  320. while(time_before(jiffies, end))
  321. {
  322. r = NCR5380_read(reg);
  323. if((r & bit) == val)
  324. return 0;
  325. if(!in_interrupt())
  326. cond_resched();
  327. else
  328. cpu_relax();
  329. }
  330. return -ETIMEDOUT;
  331. }
  332. static struct {
  333. unsigned char value;
  334. const char *name;
  335. } phases[] __maybe_unused = {
  336. {PHASE_DATAOUT, "DATAOUT"},
  337. {PHASE_DATAIN, "DATAIN"},
  338. {PHASE_CMDOUT, "CMDOUT"},
  339. {PHASE_STATIN, "STATIN"},
  340. {PHASE_MSGOUT, "MSGOUT"},
  341. {PHASE_MSGIN, "MSGIN"},
  342. {PHASE_UNKNOWN, "UNKNOWN"}
  343. };
  344. #if NDEBUG
  345. static struct {
  346. unsigned char mask;
  347. const char *name;
  348. } signals[] = {
  349. {SR_DBP, "PARITY"},
  350. {SR_RST, "RST"},
  351. {SR_BSY, "BSY"},
  352. {SR_REQ, "REQ"},
  353. {SR_MSG, "MSG"},
  354. {SR_CD, "CD"},
  355. {SR_IO, "IO"},
  356. {SR_SEL, "SEL"},
  357. {0, NULL}
  358. },
  359. basrs[] = {
  360. {BASR_ATN, "ATN"},
  361. {BASR_ACK, "ACK"},
  362. {0, NULL}
  363. },
  364. icrs[] = {
  365. {ICR_ASSERT_RST, "ASSERT RST"},
  366. {ICR_ASSERT_ACK, "ASSERT ACK"},
  367. {ICR_ASSERT_BSY, "ASSERT BSY"},
  368. {ICR_ASSERT_SEL, "ASSERT SEL"},
  369. {ICR_ASSERT_ATN, "ASSERT ATN"},
  370. {ICR_ASSERT_DATA, "ASSERT DATA"},
  371. {0, NULL}
  372. },
  373. mrs[] = {
  374. {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"},
  375. {MR_TARGET, "MODE TARGET"},
  376. {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"},
  377. {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"},
  378. {MR_MONITOR_BSY, "MODE MONITOR BSY"},
  379. {MR_DMA_MODE, "MODE DMA"},
  380. {MR_ARBITRATE, "MODE ARBITRATION"},
  381. {0, NULL}
  382. };
  383. /**
  384. * NCR5380_print - print scsi bus signals
  385. * @instance: adapter state to dump
  386. *
  387. * Print the SCSI bus signals for debugging purposes
  388. *
  389. * Locks: caller holds hostdata lock (not essential)
  390. */
  391. static void NCR5380_print(struct Scsi_Host *instance)
  392. {
  393. NCR5380_local_declare();
  394. unsigned char status, data, basr, mr, icr, i;
  395. NCR5380_setup(instance);
  396. data = NCR5380_read(CURRENT_SCSI_DATA_REG);
  397. status = NCR5380_read(STATUS_REG);
  398. mr = NCR5380_read(MODE_REG);
  399. icr = NCR5380_read(INITIATOR_COMMAND_REG);
  400. basr = NCR5380_read(BUS_AND_STATUS_REG);
  401. printk("STATUS_REG: %02x ", status);
  402. for (i = 0; signals[i].mask; ++i)
  403. if (status & signals[i].mask)
  404. printk(",%s", signals[i].name);
  405. printk("\nBASR: %02x ", basr);
  406. for (i = 0; basrs[i].mask; ++i)
  407. if (basr & basrs[i].mask)
  408. printk(",%s", basrs[i].name);
  409. printk("\nICR: %02x ", icr);
  410. for (i = 0; icrs[i].mask; ++i)
  411. if (icr & icrs[i].mask)
  412. printk(",%s", icrs[i].name);
  413. printk("\nMODE: %02x ", mr);
  414. for (i = 0; mrs[i].mask; ++i)
  415. if (mr & mrs[i].mask)
  416. printk(",%s", mrs[i].name);
  417. printk("\n");
  418. }
  419. /*
  420. * NCR5380_print_phase - show SCSI phase
  421. * @instance: adapter to dump
  422. *
  423. * Print the current SCSI phase for debugging purposes
  424. *
  425. * Locks: none
  426. */
  427. static void NCR5380_print_phase(struct Scsi_Host *instance)
  428. {
  429. NCR5380_local_declare();
  430. unsigned char status;
  431. int i;
  432. NCR5380_setup(instance);
  433. status = NCR5380_read(STATUS_REG);
  434. if (!(status & SR_REQ))
  435. printk("scsi%d : REQ not asserted, phase unknown.\n", instance->host_no);
  436. else {
  437. for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i);
  438. printk("scsi%d : phase %s\n", instance->host_no, phases[i].name);
  439. }
  440. }
  441. #endif
  442. /*
  443. * These need tweaking, and would probably work best as per-device
  444. * flags initialized differently for disk, tape, cd, etc devices.
  445. * People with broken devices are free to experiment as to what gives
  446. * the best results for them.
  447. *
  448. * USLEEP_SLEEP should be a minimum seek time.
  449. *
  450. * USLEEP_POLL should be a maximum rotational latency.
  451. */
  452. #ifndef USLEEP_SLEEP
  453. /* 20 ms (reasonable hard disk speed) */
  454. #define USLEEP_SLEEP (20*HZ/1000)
  455. #endif
  456. /* 300 RPM (floppy speed) */
  457. #ifndef USLEEP_POLL
  458. #define USLEEP_POLL (200*HZ/1000)
  459. #endif
  460. #ifndef USLEEP_WAITLONG
  461. /* RvC: (reasonable time to wait on select error) */
  462. #define USLEEP_WAITLONG USLEEP_SLEEP
  463. #endif
  464. /*
  465. * Function : int should_disconnect (unsigned char cmd)
  466. *
  467. * Purpose : decide whether a command would normally disconnect or
  468. * not, since if it won't disconnect we should go to sleep.
  469. *
  470. * Input : cmd - opcode of SCSI command
  471. *
  472. * Returns : DISCONNECT_LONG if we should disconnect for a really long
  473. * time (ie always, sleep, look for REQ active, sleep),
  474. * DISCONNECT_TIME_TO_DATA if we would only disconnect for a normal
  475. * time-to-data delay, DISCONNECT_NONE if this command would return
  476. * immediately.
  477. *
  478. * Future sleep algorithms based on time to data can exploit
  479. * something like this so they can differentiate between "normal"
  480. * (ie, read, write, seek) and unusual commands (ie, * format).
  481. *
  482. * Note : We don't deal with commands that handle an immediate disconnect,
  483. *
  484. */
  485. static int should_disconnect(unsigned char cmd)
  486. {
  487. switch (cmd) {
  488. case READ_6:
  489. case WRITE_6:
  490. case SEEK_6:
  491. case READ_10:
  492. case WRITE_10:
  493. case SEEK_10:
  494. return DISCONNECT_TIME_TO_DATA;
  495. case FORMAT_UNIT:
  496. case SEARCH_HIGH:
  497. case SEARCH_LOW:
  498. case SEARCH_EQUAL:
  499. return DISCONNECT_LONG;
  500. default:
  501. return DISCONNECT_NONE;
  502. }
  503. }
  504. static void NCR5380_set_timer(struct NCR5380_hostdata *hostdata, unsigned long timeout)
  505. {
  506. hostdata->time_expires = jiffies + timeout;
  507. schedule_delayed_work(&hostdata->coroutine, timeout);
  508. }
  509. static int probe_irq __initdata = 0;
  510. /**
  511. * probe_intr - helper for IRQ autoprobe
  512. * @irq: interrupt number
  513. * @dev_id: unused
  514. * @regs: unused
  515. *
  516. * Set a flag to indicate the IRQ in question was received. This is
  517. * used by the IRQ probe code.
  518. */
  519. static irqreturn_t __init probe_intr(int irq, void *dev_id)
  520. {
  521. probe_irq = irq;
  522. return IRQ_HANDLED;
  523. }
  524. /**
  525. * NCR5380_probe_irq - find the IRQ of an NCR5380
  526. * @instance: NCR5380 controller
  527. * @possible: bitmask of ISA IRQ lines
  528. *
  529. * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ
  530. * and then looking to see what interrupt actually turned up.
  531. *
  532. * Locks: none, irqs must be enabled on entry
  533. */
  534. static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance,
  535. int possible)
  536. {
  537. NCR5380_local_declare();
  538. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  539. unsigned long timeout;
  540. int trying_irqs, i, mask;
  541. NCR5380_setup(instance);
  542. for (trying_irqs = i = 0, mask = 1; i < 16; ++i, mask <<= 1)
  543. if ((mask & possible) && (request_irq(i, &probe_intr, IRQF_DISABLED, "NCR-probe", NULL) == 0))
  544. trying_irqs |= mask;
  545. timeout = jiffies + (250 * HZ / 1000);
  546. probe_irq = SCSI_IRQ_NONE;
  547. /*
  548. * A interrupt is triggered whenever BSY = false, SEL = true
  549. * and a bit set in the SELECT_ENABLE_REG is asserted on the
  550. * SCSI bus.
  551. *
  552. * Note that the bus is only driven when the phase control signals
  553. * (I/O, C/D, and MSG) match those in the TCR, so we must reset that
  554. * to zero.
  555. */
  556. NCR5380_write(TARGET_COMMAND_REG, 0);
  557. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  558. NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
  559. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL);
  560. while (probe_irq == SCSI_IRQ_NONE && time_before(jiffies, timeout))
  561. schedule_timeout_uninterruptible(1);
  562. NCR5380_write(SELECT_ENABLE_REG, 0);
  563. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  564. for (i = 0, mask = 1; i < 16; ++i, mask <<= 1)
  565. if (trying_irqs & mask)
  566. free_irq(i, NULL);
  567. return probe_irq;
  568. }
  569. /**
  570. * NCR58380_print_options - show options
  571. * @instance: unused for now
  572. *
  573. * Called by probe code indicating the NCR5380 driver options that
  574. * were selected. At some point this will switch to runtime options
  575. * read from the adapter in question
  576. *
  577. * Locks: none
  578. */
  579. static void __init __maybe_unused
  580. NCR5380_print_options(struct Scsi_Host *instance)
  581. {
  582. printk(" generic options"
  583. #ifdef AUTOPROBE_IRQ
  584. " AUTOPROBE_IRQ"
  585. #endif
  586. #ifdef AUTOSENSE
  587. " AUTOSENSE"
  588. #endif
  589. #ifdef DIFFERENTIAL
  590. " DIFFERENTIAL"
  591. #endif
  592. #ifdef REAL_DMA
  593. " REAL DMA"
  594. #endif
  595. #ifdef REAL_DMA_POLL
  596. " REAL DMA POLL"
  597. #endif
  598. #ifdef PARITY
  599. " PARITY"
  600. #endif
  601. #ifdef PSEUDO_DMA
  602. " PSEUDO DMA"
  603. #endif
  604. #ifdef UNSAFE
  605. " UNSAFE "
  606. #endif
  607. );
  608. printk(" USLEEP, USLEEP_POLL=%d USLEEP_SLEEP=%d", USLEEP_POLL, USLEEP_SLEEP);
  609. printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
  610. if (((struct NCR5380_hostdata *) instance->hostdata)->flags & FLAG_NCR53C400) {
  611. printk(" ncr53c400 release=%d", NCR53C400_PUBLIC_RELEASE);
  612. }
  613. }
  614. /**
  615. * NCR5380_print_status - dump controller info
  616. * @instance: controller to dump
  617. *
  618. * Print commands in the various queues, called from NCR5380_abort
  619. * and NCR5380_debug to aid debugging.
  620. *
  621. * Locks: called functions disable irqs
  622. */
  623. static void NCR5380_print_status(struct Scsi_Host *instance)
  624. {
  625. NCR5380_dprint(NDEBUG_ANY, instance);
  626. NCR5380_dprint_phase(NDEBUG_ANY, instance);
  627. }
  628. /******************************************/
  629. /*
  630. * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
  631. *
  632. * *buffer: I/O buffer
  633. * **start: if inout == FALSE pointer into buffer where user read should start
  634. * offset: current offset
  635. * length: length of buffer
  636. * hostno: Scsi_Host host_no
  637. * inout: TRUE - user is writing; FALSE - user is reading
  638. *
  639. * Return the number of bytes read from or written
  640. */
  641. #undef SPRINTF
  642. #define SPRINTF(args...) do { if(pos < buffer + length-80) pos += sprintf(pos, ## args); } while(0)
  643. static
  644. char *lprint_Scsi_Cmnd(Scsi_Cmnd * cmd, char *pos, char *buffer, int length);
  645. static
  646. char *lprint_command(unsigned char *cmd, char *pos, char *buffer, int len);
  647. static
  648. char *lprint_opcode(int opcode, char *pos, char *buffer, int length);
  649. static int __maybe_unused NCR5380_proc_info(struct Scsi_Host *instance,
  650. char *buffer, char **start, off_t offset, int length, int inout)
  651. {
  652. char *pos = buffer;
  653. struct NCR5380_hostdata *hostdata;
  654. Scsi_Cmnd *ptr;
  655. hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  656. if (inout) { /* Has data been written to the file ? */
  657. #ifdef DTC_PUBLIC_RELEASE
  658. dtc_wmaxi = dtc_maxi = 0;
  659. #endif
  660. #ifdef PAS16_PUBLIC_RELEASE
  661. pas_wmaxi = pas_maxi = 0;
  662. #endif
  663. return (-ENOSYS); /* Currently this is a no-op */
  664. }
  665. SPRINTF("NCR5380 core release=%d. ", NCR5380_PUBLIC_RELEASE);
  666. if (((struct NCR5380_hostdata *) instance->hostdata)->flags & FLAG_NCR53C400)
  667. SPRINTF("ncr53c400 release=%d. ", NCR53C400_PUBLIC_RELEASE);
  668. #ifdef DTC_PUBLIC_RELEASE
  669. SPRINTF("DTC 3180/3280 release %d", DTC_PUBLIC_RELEASE);
  670. #endif
  671. #ifdef T128_PUBLIC_RELEASE
  672. SPRINTF("T128 release %d", T128_PUBLIC_RELEASE);
  673. #endif
  674. #ifdef GENERIC_NCR5380_PUBLIC_RELEASE
  675. SPRINTF("Generic5380 release %d", GENERIC_NCR5380_PUBLIC_RELEASE);
  676. #endif
  677. #ifdef PAS16_PUBLIC_RELEASE
  678. SPRINTF("PAS16 release=%d", PAS16_PUBLIC_RELEASE);
  679. #endif
  680. SPRINTF("\nBase Addr: 0x%05lX ", (long) instance->base);
  681. SPRINTF("io_port: %04x ", (int) instance->io_port);
  682. if (instance->irq == SCSI_IRQ_NONE)
  683. SPRINTF("IRQ: None.\n");
  684. else
  685. SPRINTF("IRQ: %d.\n", instance->irq);
  686. #ifdef DTC_PUBLIC_RELEASE
  687. SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n", dtc_wmaxi, dtc_maxi);
  688. #endif
  689. #ifdef PAS16_PUBLIC_RELEASE
  690. SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n", pas_wmaxi, pas_maxi);
  691. #endif
  692. spin_lock_irq(instance->host_lock);
  693. if (!hostdata->connected)
  694. SPRINTF("scsi%d: no currently connected command\n", instance->host_no);
  695. else
  696. pos = lprint_Scsi_Cmnd((Scsi_Cmnd *) hostdata->connected, pos, buffer, length);
  697. SPRINTF("scsi%d: issue_queue\n", instance->host_no);
  698. for (ptr = (Scsi_Cmnd *) hostdata->issue_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
  699. pos = lprint_Scsi_Cmnd(ptr, pos, buffer, length);
  700. SPRINTF("scsi%d: disconnected_queue\n", instance->host_no);
  701. for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble)
  702. pos = lprint_Scsi_Cmnd(ptr, pos, buffer, length);
  703. spin_unlock_irq(instance->host_lock);
  704. *start = buffer;
  705. if (pos - buffer < offset)
  706. return 0;
  707. else if (pos - buffer - offset < length)
  708. return pos - buffer - offset;
  709. return length;
  710. }
  711. static char *lprint_Scsi_Cmnd(Scsi_Cmnd * cmd, char *pos, char *buffer, int length)
  712. {
  713. SPRINTF("scsi%d : destination target %d, lun %d\n", cmd->device->host->host_no, cmd->device->id, cmd->device->lun);
  714. SPRINTF(" command = ");
  715. pos = lprint_command(cmd->cmnd, pos, buffer, length);
  716. return (pos);
  717. }
  718. static char *lprint_command(unsigned char *command, char *pos, char *buffer, int length)
  719. {
  720. int i, s;
  721. pos = lprint_opcode(command[0], pos, buffer, length);
  722. for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
  723. SPRINTF("%02x ", command[i]);
  724. SPRINTF("\n");
  725. return (pos);
  726. }
  727. static char *lprint_opcode(int opcode, char *pos, char *buffer, int length)
  728. {
  729. SPRINTF("%2d (0x%02x)", opcode, opcode);
  730. return (pos);
  731. }
  732. /**
  733. * NCR5380_init - initialise an NCR5380
  734. * @instance: adapter to configure
  735. * @flags: control flags
  736. *
  737. * Initializes *instance and corresponding 5380 chip,
  738. * with flags OR'd into the initial flags value.
  739. *
  740. * Notes : I assume that the host, hostno, and id bits have been
  741. * set correctly. I don't care about the irq and other fields.
  742. *
  743. * Returns 0 for success
  744. *
  745. * Locks: interrupts must be enabled when we are called
  746. */
  747. static int __devinit NCR5380_init(struct Scsi_Host *instance, int flags)
  748. {
  749. NCR5380_local_declare();
  750. int i, pass;
  751. unsigned long timeout;
  752. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  753. if(in_interrupt())
  754. printk(KERN_ERR "NCR5380_init called with interrupts off!\n");
  755. /*
  756. * On NCR53C400 boards, NCR5380 registers are mapped 8 past
  757. * the base address.
  758. */
  759. #ifdef NCR53C400
  760. if (flags & FLAG_NCR53C400)
  761. instance->NCR5380_instance_name += NCR53C400_address_adjust;
  762. #endif
  763. NCR5380_setup(instance);
  764. hostdata->aborted = 0;
  765. hostdata->id_mask = 1 << instance->this_id;
  766. for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
  767. if (i > hostdata->id_mask)
  768. hostdata->id_higher_mask |= i;
  769. for (i = 0; i < 8; ++i)
  770. hostdata->busy[i] = 0;
  771. #ifdef REAL_DMA
  772. hostdata->dmalen = 0;
  773. #endif
  774. hostdata->targets_present = 0;
  775. hostdata->connected = NULL;
  776. hostdata->issue_queue = NULL;
  777. hostdata->disconnected_queue = NULL;
  778. INIT_DELAYED_WORK(&hostdata->coroutine, NCR5380_main);
  779. #ifdef NCR5380_STATS
  780. for (i = 0; i < 8; ++i) {
  781. hostdata->time_read[i] = 0;
  782. hostdata->time_write[i] = 0;
  783. hostdata->bytes_read[i] = 0;
  784. hostdata->bytes_write[i] = 0;
  785. }
  786. hostdata->timebase = 0;
  787. hostdata->pendingw = 0;
  788. hostdata->pendingr = 0;
  789. #endif
  790. /* The CHECK code seems to break the 53C400. Will check it later maybe */
  791. if (flags & FLAG_NCR53C400)
  792. hostdata->flags = FLAG_HAS_LAST_BYTE_SENT | flags;
  793. else
  794. hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT | flags;
  795. hostdata->host = instance;
  796. hostdata->time_expires = 0;
  797. #ifndef AUTOSENSE
  798. if ((instance->cmd_per_lun > 1) || instance->can_queue > 1)
  799. printk(KERN_WARNING "scsi%d : WARNING : support for multiple outstanding commands enabled\n" " without AUTOSENSE option, contingent allegiance conditions may\n"
  800. " be incorrectly cleared.\n", instance->host_no);
  801. #endif /* def AUTOSENSE */
  802. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  803. NCR5380_write(MODE_REG, MR_BASE);
  804. NCR5380_write(TARGET_COMMAND_REG, 0);
  805. NCR5380_write(SELECT_ENABLE_REG, 0);
  806. #ifdef NCR53C400
  807. if (hostdata->flags & FLAG_NCR53C400) {
  808. NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE);
  809. }
  810. #endif
  811. /*
  812. * Detect and correct bus wedge problems.
  813. *
  814. * If the system crashed, it may have crashed in a state
  815. * where a SCSI command was still executing, and the
  816. * SCSI bus is not in a BUS FREE STATE.
  817. *
  818. * If this is the case, we'll try to abort the currently
  819. * established nexus which we know nothing about, and that
  820. * failing, do a hard reset of the SCSI bus
  821. */
  822. for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) {
  823. switch (pass) {
  824. case 1:
  825. case 3:
  826. case 5:
  827. printk(KERN_INFO "scsi%d: SCSI bus busy, waiting up to five seconds\n", instance->host_no);
  828. timeout = jiffies + 5 * HZ;
  829. NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, 0, 5*HZ);
  830. break;
  831. case 2:
  832. printk(KERN_WARNING "scsi%d: bus busy, attempting abort\n", instance->host_no);
  833. do_abort(instance);
  834. break;
  835. case 4:
  836. printk(KERN_WARNING "scsi%d: bus busy, attempting reset\n", instance->host_no);
  837. do_reset(instance);
  838. break;
  839. case 6:
  840. printk(KERN_ERR "scsi%d: bus locked solid or invalid override\n", instance->host_no);
  841. return -ENXIO;
  842. }
  843. }
  844. return 0;
  845. }
  846. /**
  847. * NCR5380_exit - remove an NCR5380
  848. * @instance: adapter to remove
  849. */
  850. static void NCR5380_exit(struct Scsi_Host *instance)
  851. {
  852. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  853. cancel_delayed_work_sync(&hostdata->coroutine);
  854. }
  855. /**
  856. * NCR5380_queue_command - queue a command
  857. * @cmd: SCSI command
  858. * @done: completion handler
  859. *
  860. * cmd is added to the per instance issue_queue, with minor
  861. * twiddling done to the host specific fields of cmd. If the
  862. * main coroutine is not running, it is restarted.
  863. *
  864. * Locks: host lock taken by caller
  865. */
  866. static int NCR5380_queue_command_lck(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
  867. {
  868. struct Scsi_Host *instance = cmd->device->host;
  869. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  870. Scsi_Cmnd *tmp;
  871. #if (NDEBUG & NDEBUG_NO_WRITE)
  872. switch (cmd->cmnd[0]) {
  873. case WRITE_6:
  874. case WRITE_10:
  875. printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n", instance->host_no);
  876. cmd->result = (DID_ERROR << 16);
  877. done(cmd);
  878. return 0;
  879. }
  880. #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
  881. #ifdef NCR5380_STATS
  882. switch (cmd->cmnd[0]) {
  883. case WRITE:
  884. case WRITE_6:
  885. case WRITE_10:
  886. hostdata->time_write[cmd->device->id] -= (jiffies - hostdata->timebase);
  887. hostdata->bytes_write[cmd->device->id] += scsi_bufflen(cmd);
  888. hostdata->pendingw++;
  889. break;
  890. case READ:
  891. case READ_6:
  892. case READ_10:
  893. hostdata->time_read[cmd->device->id] -= (jiffies - hostdata->timebase);
  894. hostdata->bytes_read[cmd->device->id] += scsi_bufflen(cmd);
  895. hostdata->pendingr++;
  896. break;
  897. }
  898. #endif
  899. /*
  900. * We use the host_scribble field as a pointer to the next command
  901. * in a queue
  902. */
  903. cmd->host_scribble = NULL;
  904. cmd->scsi_done = done;
  905. cmd->result = 0;
  906. /*
  907. * Insert the cmd into the issue queue. Note that REQUEST SENSE
  908. * commands are added to the head of the queue since any command will
  909. * clear the contingent allegiance condition that exists and the
  910. * sense data is only guaranteed to be valid while the condition exists.
  911. */
  912. if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
  913. LIST(cmd, hostdata->issue_queue);
  914. cmd->host_scribble = (unsigned char *) hostdata->issue_queue;
  915. hostdata->issue_queue = cmd;
  916. } else {
  917. for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->host_scribble; tmp = (Scsi_Cmnd *) tmp->host_scribble);
  918. LIST(cmd, tmp);
  919. tmp->host_scribble = (unsigned char *) cmd;
  920. }
  921. dprintk(NDEBUG_QUEUES, ("scsi%d : command added to %s of queue\n", instance->host_no, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail"));
  922. /* Run the coroutine if it isn't already running. */
  923. /* Kick off command processing */
  924. schedule_delayed_work(&hostdata->coroutine, 0);
  925. return 0;
  926. }
  927. static DEF_SCSI_QCMD(NCR5380_queue_command)
  928. /**
  929. * NCR5380_main - NCR state machines
  930. *
  931. * NCR5380_main is a coroutine that runs as long as more work can
  932. * be done on the NCR5380 host adapters in a system. Both
  933. * NCR5380_queue_command() and NCR5380_intr() will try to start it
  934. * in case it is not running.
  935. *
  936. * Locks: called as its own thread with no locks held. Takes the
  937. * host lock and called routines may take the isa dma lock.
  938. */
  939. static void NCR5380_main(struct work_struct *work)
  940. {
  941. struct NCR5380_hostdata *hostdata =
  942. container_of(work, struct NCR5380_hostdata, coroutine.work);
  943. struct Scsi_Host *instance = hostdata->host;
  944. Scsi_Cmnd *tmp, *prev;
  945. int done;
  946. spin_lock_irq(instance->host_lock);
  947. do {
  948. /* Lock held here */
  949. done = 1;
  950. if (!hostdata->connected && !hostdata->selecting) {
  951. dprintk(NDEBUG_MAIN, ("scsi%d : not connected\n", instance->host_no));
  952. /*
  953. * Search through the issue_queue for a command destined
  954. * for a target that's not busy.
  955. */
  956. for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
  957. {
  958. if (prev != tmp)
  959. dprintk(NDEBUG_LISTS, ("MAIN tmp=%p target=%d busy=%d lun=%d\n", tmp, tmp->target, hostdata->busy[tmp->target], tmp->lun));
  960. /* When we find one, remove it from the issue queue. */
  961. if (!(hostdata->busy[tmp->device->id] & (1 << tmp->device->lun))) {
  962. if (prev) {
  963. REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
  964. prev->host_scribble = tmp->host_scribble;
  965. } else {
  966. REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble);
  967. hostdata->issue_queue = (Scsi_Cmnd *) tmp->host_scribble;
  968. }
  969. tmp->host_scribble = NULL;
  970. /*
  971. * Attempt to establish an I_T_L nexus here.
  972. * On success, instance->hostdata->connected is set.
  973. * On failure, we must add the command back to the
  974. * issue queue so we can keep trying.
  975. */
  976. dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, ("scsi%d : main() : command for target %d lun %d removed from issue_queue\n", instance->host_no, tmp->target, tmp->lun));
  977. /*
  978. * A successful selection is defined as one that
  979. * leaves us with the command connected and
  980. * in hostdata->connected, OR has terminated the
  981. * command.
  982. *
  983. * With successful commands, we fall through
  984. * and see if we can do an information transfer,
  985. * with failures we will restart.
  986. */
  987. hostdata->selecting = NULL;
  988. /* RvC: have to preset this to indicate a new command is being performed */
  989. if (!NCR5380_select(instance, tmp,
  990. /*
  991. * REQUEST SENSE commands are issued without tagged
  992. * queueing, even on SCSI-II devices because the
  993. * contingent allegiance condition exists for the
  994. * entire unit.
  995. */
  996. (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : TAG_NEXT)) {
  997. break;
  998. } else {
  999. LIST(tmp, hostdata->issue_queue);
  1000. tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
  1001. hostdata->issue_queue = tmp;
  1002. done = 0;
  1003. dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, ("scsi%d : main(): select() failed, returned to issue_queue\n", instance->host_no));
  1004. }
  1005. /* lock held here still */
  1006. } /* if target/lun is not busy */
  1007. } /* for */
  1008. /* exited locked */
  1009. } /* if (!hostdata->connected) */
  1010. if (hostdata->selecting) {
  1011. tmp = (Scsi_Cmnd *) hostdata->selecting;
  1012. /* Selection will drop and retake the lock */
  1013. if (!NCR5380_select(instance, tmp, (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : TAG_NEXT)) {
  1014. /* Ok ?? */
  1015. } else {
  1016. /* RvC: device failed, so we wait a long time
  1017. this is needed for Mustek scanners, that
  1018. do not respond to commands immediately
  1019. after a scan */
  1020. printk(KERN_DEBUG "scsi%d: device %d did not respond in time\n", instance->host_no, tmp->device->id);
  1021. LIST(tmp, hostdata->issue_queue);
  1022. tmp->host_scribble = (unsigned char *) hostdata->issue_queue;
  1023. hostdata->issue_queue = tmp;
  1024. NCR5380_set_timer(hostdata, USLEEP_WAITLONG);
  1025. }
  1026. } /* if hostdata->selecting */
  1027. if (hostdata->connected
  1028. #ifdef REAL_DMA
  1029. && !hostdata->dmalen
  1030. #endif
  1031. && (!hostdata->time_expires || time_before_eq(hostdata->time_expires, jiffies))
  1032. ) {
  1033. dprintk(NDEBUG_MAIN, ("scsi%d : main() : performing information transfer\n", instance->host_no));
  1034. NCR5380_information_transfer(instance);
  1035. dprintk(NDEBUG_MAIN, ("scsi%d : main() : done set false\n", instance->host_no));
  1036. done = 0;
  1037. } else
  1038. break;
  1039. } while (!done);
  1040. spin_unlock_irq(instance->host_lock);
  1041. }
  1042. #ifndef DONT_USE_INTR
  1043. /**
  1044. * NCR5380_intr - generic NCR5380 irq handler
  1045. * @irq: interrupt number
  1046. * @dev_id: device info
  1047. *
  1048. * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
  1049. * from the disconnected queue, and restarting NCR5380_main()
  1050. * as required.
  1051. *
  1052. * Locks: takes the needed instance locks
  1053. */
  1054. static irqreturn_t NCR5380_intr(int dummy, void *dev_id)
  1055. {
  1056. NCR5380_local_declare();
  1057. struct Scsi_Host *instance = dev_id;
  1058. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  1059. int done;
  1060. unsigned char basr;
  1061. unsigned long flags;
  1062. dprintk(NDEBUG_INTR, ("scsi : NCR5380 irq %d triggered\n",
  1063. instance->irq));
  1064. do {
  1065. done = 1;
  1066. spin_lock_irqsave(instance->host_lock, flags);
  1067. /* Look for pending interrupts */
  1068. NCR5380_setup(instance);
  1069. basr = NCR5380_read(BUS_AND_STATUS_REG);
  1070. /* XXX dispatch to appropriate routine if found and done=0 */
  1071. if (basr & BASR_IRQ) {
  1072. NCR5380_dprint(NDEBUG_INTR, instance);
  1073. if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
  1074. done = 0;
  1075. dprintk(NDEBUG_INTR, ("scsi%d : SEL interrupt\n", instance->host_no));
  1076. NCR5380_reselect(instance);
  1077. (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1078. } else if (basr & BASR_PARITY_ERROR) {
  1079. dprintk(NDEBUG_INTR, ("scsi%d : PARITY interrupt\n", instance->host_no));
  1080. (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1081. } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
  1082. dprintk(NDEBUG_INTR, ("scsi%d : RESET interrupt\n", instance->host_no));
  1083. (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1084. } else {
  1085. #if defined(REAL_DMA)
  1086. /*
  1087. * We should only get PHASE MISMATCH and EOP interrupts
  1088. * if we have DMA enabled, so do a sanity check based on
  1089. * the current setting of the MODE register.
  1090. */
  1091. if ((NCR5380_read(MODE_REG) & MR_DMA) && ((basr & BASR_END_DMA_TRANSFER) || !(basr & BASR_PHASE_MATCH))) {
  1092. int transferred;
  1093. if (!hostdata->connected)
  1094. panic("scsi%d : received end of DMA interrupt with no connected cmd\n", instance->hostno);
  1095. transferred = (hostdata->dmalen - NCR5380_dma_residual(instance));
  1096. hostdata->connected->SCp.this_residual -= transferred;
  1097. hostdata->connected->SCp.ptr += transferred;
  1098. hostdata->dmalen = 0;
  1099. (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1100. /* FIXME: we need to poll briefly then defer a workqueue task ! */
  1101. NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2*HZ);
  1102. NCR5380_write(MODE_REG, MR_BASE);
  1103. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1104. }
  1105. #else
  1106. dprintk(NDEBUG_INTR, ("scsi : unknown interrupt, BASR 0x%X, MR 0x%X, SR 0x%x\n", basr, NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG)));
  1107. (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1108. #endif
  1109. }
  1110. } /* if BASR_IRQ */
  1111. spin_unlock_irqrestore(instance->host_lock, flags);
  1112. if(!done)
  1113. schedule_delayed_work(&hostdata->coroutine, 0);
  1114. } while (!done);
  1115. return IRQ_HANDLED;
  1116. }
  1117. #endif
  1118. /**
  1119. * collect_stats - collect stats on a scsi command
  1120. * @hostdata: adapter
  1121. * @cmd: command being issued
  1122. *
  1123. * Update the statistical data by parsing the command in question
  1124. */
  1125. static void collect_stats(struct NCR5380_hostdata *hostdata, Scsi_Cmnd * cmd)
  1126. {
  1127. #ifdef NCR5380_STATS
  1128. switch (cmd->cmnd[0]) {
  1129. case WRITE:
  1130. case WRITE_6:
  1131. case WRITE_10:
  1132. hostdata->time_write[scmd_id(cmd)] += (jiffies - hostdata->timebase);
  1133. hostdata->pendingw--;
  1134. break;
  1135. case READ:
  1136. case READ_6:
  1137. case READ_10:
  1138. hostdata->time_read[scmd_id(cmd)] += (jiffies - hostdata->timebase);
  1139. hostdata->pendingr--;
  1140. break;
  1141. }
  1142. #endif
  1143. }
  1144. /*
  1145. * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd,
  1146. * int tag);
  1147. *
  1148. * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
  1149. * including ARBITRATION, SELECTION, and initial message out for
  1150. * IDENTIFY and queue messages.
  1151. *
  1152. * Inputs : instance - instantiation of the 5380 driver on which this
  1153. * target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for
  1154. * new tag, TAG_NONE for untagged queueing, otherwise set to the tag for
  1155. * the command that is presently connected.
  1156. *
  1157. * Returns : -1 if selection could not execute for some reason,
  1158. * 0 if selection succeeded or failed because the target
  1159. * did not respond.
  1160. *
  1161. * Side effects :
  1162. * If bus busy, arbitration failed, etc, NCR5380_select() will exit
  1163. * with registers as they should have been on entry - ie
  1164. * SELECT_ENABLE will be set appropriately, the NCR5380
  1165. * will cease to drive any SCSI bus signals.
  1166. *
  1167. * If successful : I_T_L or I_T_L_Q nexus will be established,
  1168. * instance->connected will be set to cmd.
  1169. * SELECT interrupt will be disabled.
  1170. *
  1171. * If failed (no target) : cmd->scsi_done() will be called, and the
  1172. * cmd->result host byte set to DID_BAD_TARGET.
  1173. *
  1174. * Locks: caller holds hostdata lock in IRQ mode
  1175. */
  1176. static int NCR5380_select(struct Scsi_Host *instance, Scsi_Cmnd * cmd, int tag)
  1177. {
  1178. NCR5380_local_declare();
  1179. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  1180. unsigned char tmp[3], phase;
  1181. unsigned char *data;
  1182. int len;
  1183. unsigned long timeout;
  1184. unsigned char value;
  1185. int err;
  1186. NCR5380_setup(instance);
  1187. if (hostdata->selecting)
  1188. goto part2;
  1189. hostdata->restart_select = 0;
  1190. NCR5380_dprint(NDEBUG_ARBITRATION, instance);
  1191. dprintk(NDEBUG_ARBITRATION, ("scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id));
  1192. /*
  1193. * Set the phase bits to 0, otherwise the NCR5380 won't drive the
  1194. * data bus during SELECTION.
  1195. */
  1196. NCR5380_write(TARGET_COMMAND_REG, 0);
  1197. /*
  1198. * Start arbitration.
  1199. */
  1200. NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
  1201. NCR5380_write(MODE_REG, MR_ARBITRATE);
  1202. /* We can be relaxed here, interrupts are on, we are
  1203. in workqueue context, the birds are singing in the trees */
  1204. spin_unlock_irq(instance->host_lock);
  1205. err = NCR5380_poll_politely(instance, INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS, ICR_ARBITRATION_PROGRESS, 5*HZ);
  1206. spin_lock_irq(instance->host_lock);
  1207. if (err < 0) {
  1208. printk(KERN_DEBUG "scsi: arbitration timeout at %d\n", __LINE__);
  1209. NCR5380_write(MODE_REG, MR_BASE);
  1210. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1211. goto failed;
  1212. }
  1213. dprintk(NDEBUG_ARBITRATION, ("scsi%d : arbitration complete\n", instance->host_no));
  1214. /*
  1215. * The arbitration delay is 2.2us, but this is a minimum and there is
  1216. * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
  1217. * the integral nature of udelay().
  1218. *
  1219. */
  1220. udelay(3);
  1221. /* Check for lost arbitration */
  1222. if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) || (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) || (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
  1223. NCR5380_write(MODE_REG, MR_BASE);
  1224. dprintk(NDEBUG_ARBITRATION, ("scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no));
  1225. goto failed;
  1226. }
  1227. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL);
  1228. if (!(hostdata->flags & FLAG_DTC3181E) &&
  1229. /* RvC: DTC3181E has some trouble with this
  1230. * so we simply removed it. Seems to work with
  1231. * only Mustek scanner attached
  1232. */
  1233. (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) {
  1234. NCR5380_write(MODE_REG, MR_BASE);
  1235. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1236. dprintk(NDEBUG_ARBITRATION, ("scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n", instance->host_no));
  1237. goto failed;
  1238. }
  1239. /*
  1240. * Again, bus clear + bus settle time is 1.2us, however, this is
  1241. * a minimum so we'll udelay ceil(1.2)
  1242. */
  1243. udelay(2);
  1244. dprintk(NDEBUG_ARBITRATION, ("scsi%d : won arbitration\n", instance->host_no));
  1245. /*
  1246. * Now that we have won arbitration, start Selection process, asserting
  1247. * the host and target ID's on the SCSI bus.
  1248. */
  1249. NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << scmd_id(cmd))));
  1250. /*
  1251. * Raise ATN while SEL is true before BSY goes false from arbitration,
  1252. * since this is the only way to guarantee that we'll get a MESSAGE OUT
  1253. * phase immediately after selection.
  1254. */
  1255. NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
  1256. NCR5380_write(MODE_REG, MR_BASE);
  1257. /*
  1258. * Reselect interrupts must be turned off prior to the dropping of BSY,
  1259. * otherwise we will trigger an interrupt.
  1260. */
  1261. NCR5380_write(SELECT_ENABLE_REG, 0);
  1262. /*
  1263. * The initiator shall then wait at least two deskew delays and release
  1264. * the BSY signal.
  1265. */
  1266. udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */
  1267. /* Reset BSY */
  1268. NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL));
  1269. /*
  1270. * Something weird happens when we cease to drive BSY - looks
  1271. * like the board/chip is letting us do another read before the
  1272. * appropriate propagation delay has expired, and we're confusing
  1273. * a BSY signal from ourselves as the target's response to SELECTION.
  1274. *
  1275. * A small delay (the 'C++' frontend breaks the pipeline with an
  1276. * unnecessary jump, making it work on my 386-33/Trantor T128, the
  1277. * tighter 'C' code breaks and requires this) solves the problem -
  1278. * the 1 us delay is arbitrary, and only used because this delay will
  1279. * be the same on other platforms and since it works here, it should
  1280. * work there.
  1281. *
  1282. * wingel suggests that this could be due to failing to wait
  1283. * one deskew delay.
  1284. */
  1285. udelay(1);
  1286. dprintk(NDEBUG_SELECTION, ("scsi%d : selecting target %d\n", instance->host_no, scmd_id(cmd)));
  1287. /*
  1288. * The SCSI specification calls for a 250 ms timeout for the actual
  1289. * selection.
  1290. */
  1291. timeout = jiffies + (250 * HZ / 1000);
  1292. /*
  1293. * XXX very interesting - we're seeing a bounce where the BSY we
  1294. * asserted is being reflected / still asserted (propagation delay?)
  1295. * and it's detecting as true. Sigh.
  1296. */
  1297. hostdata->select_time = 0; /* we count the clock ticks at which we polled */
  1298. hostdata->selecting = cmd;
  1299. part2:
  1300. /* RvC: here we enter after a sleeping period, or immediately after
  1301. execution of part 1
  1302. we poll only once ech clock tick */
  1303. value = NCR5380_read(STATUS_REG) & (SR_BSY | SR_IO);
  1304. if (!value && (hostdata->select_time < HZ/4)) {
  1305. /* RvC: we still must wait for a device response */
  1306. hostdata->select_time++; /* after 25 ticks the device has failed */
  1307. NCR5380_set_timer(hostdata, 1);
  1308. return 0; /* RvC: we return here with hostdata->selecting set,
  1309. to go to sleep */
  1310. }
  1311. hostdata->selecting = NULL;/* clear this pointer, because we passed the
  1312. waiting period */
  1313. if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) {
  1314. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1315. NCR5380_reselect(instance);
  1316. printk("scsi%d : reselection after won arbitration?\n", instance->host_no);
  1317. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1318. return -1;
  1319. }
  1320. /*
  1321. * No less than two deskew delays after the initiator detects the
  1322. * BSY signal is true, it shall release the SEL signal and may
  1323. * change the DATA BUS. -wingel
  1324. */
  1325. udelay(1);
  1326. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1327. if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
  1328. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1329. if (hostdata->targets_present & (1 << scmd_id(cmd))) {
  1330. printk(KERN_DEBUG "scsi%d : weirdness\n", instance->host_no);
  1331. if (hostdata->restart_select)
  1332. printk(KERN_DEBUG "\trestart select\n");
  1333. NCR5380_dprint(NDEBUG_SELECTION, instance);
  1334. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1335. return -1;
  1336. }
  1337. cmd->result = DID_BAD_TARGET << 16;
  1338. collect_stats(hostdata, cmd);
  1339. cmd->scsi_done(cmd);
  1340. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1341. dprintk(NDEBUG_SELECTION, ("scsi%d : target did not respond within 250ms\n", instance->host_no));
  1342. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1343. return 0;
  1344. }
  1345. hostdata->targets_present |= (1 << scmd_id(cmd));
  1346. /*
  1347. * Since we followed the SCSI spec, and raised ATN while SEL
  1348. * was true but before BSY was false during selection, the information
  1349. * transfer phase should be a MESSAGE OUT phase so that we can send the
  1350. * IDENTIFY message.
  1351. *
  1352. * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
  1353. * message (2 bytes) with a tag ID that we increment with every command
  1354. * until it wraps back to 0.
  1355. *
  1356. * XXX - it turns out that there are some broken SCSI-II devices,
  1357. * which claim to support tagged queuing but fail when more than
  1358. * some number of commands are issued at once.
  1359. */
  1360. /* Wait for start of REQ/ACK handshake */
  1361. spin_unlock_irq(instance->host_lock);
  1362. err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ);
  1363. spin_lock_irq(instance->host_lock);
  1364. if(err) {
  1365. printk(KERN_ERR "scsi%d: timeout at NCR5380.c:%d\n", instance->host_no, __LINE__);
  1366. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1367. goto failed;
  1368. }
  1369. dprintk(NDEBUG_SELECTION, ("scsi%d : target %d selected, going into MESSAGE OUT phase.\n", instance->host_no, cmd->device->id));
  1370. tmp[0] = IDENTIFY(((instance->irq == SCSI_IRQ_NONE) ? 0 : 1), cmd->device->lun);
  1371. len = 1;
  1372. cmd->tag = 0;
  1373. /* Send message(s) */
  1374. data = tmp;
  1375. phase = PHASE_MSGOUT;
  1376. NCR5380_transfer_pio(instance, &phase, &len, &data);
  1377. dprintk(NDEBUG_SELECTION, ("scsi%d : nexus established.\n", instance->host_no));
  1378. /* XXX need to handle errors here */
  1379. hostdata->connected = cmd;
  1380. hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
  1381. initialize_SCp(cmd);
  1382. return 0;
  1383. /* Selection failed */
  1384. failed:
  1385. return -1;
  1386. }
  1387. /*
  1388. * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance,
  1389. * unsigned char *phase, int *count, unsigned char **data)
  1390. *
  1391. * Purpose : transfers data in given phase using polled I/O
  1392. *
  1393. * Inputs : instance - instance of driver, *phase - pointer to
  1394. * what phase is expected, *count - pointer to number of
  1395. * bytes to transfer, **data - pointer to data pointer.
  1396. *
  1397. * Returns : -1 when different phase is entered without transferring
  1398. * maximum number of bytes, 0 if all bytes or transferred or exit
  1399. * is in same phase.
  1400. *
  1401. * Also, *phase, *count, *data are modified in place.
  1402. *
  1403. * XXX Note : handling for bus free may be useful.
  1404. */
  1405. /*
  1406. * Note : this code is not as quick as it could be, however it
  1407. * IS 100% reliable, and for the actual data transfer where speed
  1408. * counts, we will always do a pseudo DMA or DMA transfer.
  1409. */
  1410. static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
  1411. NCR5380_local_declare();
  1412. unsigned char p = *phase, tmp;
  1413. int c = *count;
  1414. unsigned char *d = *data;
  1415. /*
  1416. * RvC: some administrative data to process polling time
  1417. */
  1418. int break_allowed = 0;
  1419. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  1420. NCR5380_setup(instance);
  1421. if (!(p & SR_IO))
  1422. dprintk(NDEBUG_PIO, ("scsi%d : pio write %d bytes\n", instance->host_no, c));
  1423. else
  1424. dprintk(NDEBUG_PIO, ("scsi%d : pio read %d bytes\n", instance->host_no, c));
  1425. /*
  1426. * The NCR5380 chip will only drive the SCSI bus when the
  1427. * phase specified in the appropriate bits of the TARGET COMMAND
  1428. * REGISTER match the STATUS REGISTER
  1429. */
  1430. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
  1431. /* RvC: don't know if this is necessary, but other SCSI I/O is short
  1432. * so breaks are not necessary there
  1433. */
  1434. if ((p == PHASE_DATAIN) || (p == PHASE_DATAOUT)) {
  1435. break_allowed = 1;
  1436. }
  1437. do {
  1438. /*
  1439. * Wait for assertion of REQ, after which the phase bits will be
  1440. * valid
  1441. */
  1442. /* RvC: we simply poll once, after that we stop temporarily
  1443. * and let the device buffer fill up
  1444. * if breaking is not allowed, we keep polling as long as needed
  1445. */
  1446. /* FIXME */
  1447. while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ) && !break_allowed);
  1448. if (!(tmp & SR_REQ)) {
  1449. /* timeout condition */
  1450. NCR5380_set_timer(hostdata, USLEEP_SLEEP);
  1451. break;
  1452. }
  1453. dprintk(NDEBUG_HANDSHAKE, ("scsi%d : REQ detected\n", instance->host_no));
  1454. /* Check for phase mismatch */
  1455. if ((tmp & PHASE_MASK) != p) {
  1456. dprintk(NDEBUG_HANDSHAKE, ("scsi%d : phase mismatch\n", instance->host_no));
  1457. NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance);
  1458. break;
  1459. }
  1460. /* Do actual transfer from SCSI bus to / from memory */
  1461. if (!(p & SR_IO))
  1462. NCR5380_write(OUTPUT_DATA_REG, *d);
  1463. else
  1464. *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
  1465. ++d;
  1466. /*
  1467. * The SCSI standard suggests that in MSGOUT phase, the initiator
  1468. * should drop ATN on the last byte of the message phase
  1469. * after REQ has been asserted for the handshake but before
  1470. * the initiator raises ACK.
  1471. */
  1472. if (!(p & SR_IO)) {
  1473. if (!((p & SR_MSG) && c > 1)) {
  1474. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
  1475. NCR5380_dprint(NDEBUG_PIO, instance);
  1476. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK);
  1477. } else {
  1478. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN);
  1479. NCR5380_dprint(NDEBUG_PIO, instance);
  1480. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
  1481. }
  1482. } else {
  1483. NCR5380_dprint(NDEBUG_PIO, instance);
  1484. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
  1485. }
  1486. /* FIXME - if this fails bus reset ?? */
  1487. NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 5*HZ);
  1488. dprintk(NDEBUG_HANDSHAKE, ("scsi%d : req false, handshake complete\n", instance->host_no));
  1489. /*
  1490. * We have several special cases to consider during REQ/ACK handshaking :
  1491. * 1. We were in MSGOUT phase, and we are on the last byte of the
  1492. * message. ATN must be dropped as ACK is dropped.
  1493. *
  1494. * 2. We are in a MSGIN phase, and we are on the last byte of the
  1495. * message. We must exit with ACK asserted, so that the calling
  1496. * code may raise ATN before dropping ACK to reject the message.
  1497. *
  1498. * 3. ACK and ATN are clear and the target may proceed as normal.
  1499. */
  1500. if (!(p == PHASE_MSGIN && c == 1)) {
  1501. if (p == PHASE_MSGOUT && c > 1)
  1502. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1503. else
  1504. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1505. }
  1506. } while (--c);
  1507. dprintk(NDEBUG_PIO, ("scsi%d : residual %d\n", instance->host_no, c));
  1508. *count = c;
  1509. *data = d;
  1510. tmp = NCR5380_read(STATUS_REG);
  1511. if (tmp & SR_REQ)
  1512. *phase = tmp & PHASE_MASK;
  1513. else
  1514. *phase = PHASE_UNKNOWN;
  1515. if (!c || (*phase == p))
  1516. return 0;
  1517. else
  1518. return -1;
  1519. }
  1520. /**
  1521. * do_reset - issue a reset command
  1522. * @host: adapter to reset
  1523. *
  1524. * Issue a reset sequence to the NCR5380 and try and get the bus
  1525. * back into sane shape.
  1526. *
  1527. * Locks: caller holds queue lock
  1528. */
  1529. static void do_reset(struct Scsi_Host *host) {
  1530. NCR5380_local_declare();
  1531. NCR5380_setup(host);
  1532. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK));
  1533. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
  1534. udelay(25);
  1535. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1536. }
  1537. /*
  1538. * Function : do_abort (Scsi_Host *host)
  1539. *
  1540. * Purpose : abort the currently established nexus. Should only be
  1541. * called from a routine which can drop into a
  1542. *
  1543. * Returns : 0 on success, -1 on failure.
  1544. *
  1545. * Locks: queue lock held by caller
  1546. * FIXME: sort this out and get new_eh running
  1547. */
  1548. static int do_abort(struct Scsi_Host *host) {
  1549. NCR5380_local_declare();
  1550. unsigned char *msgptr, phase, tmp;
  1551. int len;
  1552. int rc;
  1553. NCR5380_setup(host);
  1554. /* Request message out phase */
  1555. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1556. /*
  1557. * Wait for the target to indicate a valid phase by asserting
  1558. * REQ. Once this happens, we'll have either a MSGOUT phase
  1559. * and can immediately send the ABORT message, or we'll have some
  1560. * other phase and will have to source/sink data.
  1561. *
  1562. * We really don't care what value was on the bus or what value
  1563. * the target sees, so we just handshake.
  1564. */
  1565. rc = NCR5380_poll_politely(host, STATUS_REG, SR_REQ, SR_REQ, 60 * HZ);
  1566. if(rc < 0)
  1567. return -1;
  1568. tmp = (unsigned char)rc;
  1569. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
  1570. if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
  1571. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
  1572. rc = NCR5380_poll_politely(host, STATUS_REG, SR_REQ, 0, 3*HZ);
  1573. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1574. if(rc == -1)
  1575. return -1;
  1576. }
  1577. tmp = ABORT;
  1578. msgptr = &tmp;
  1579. len = 1;
  1580. phase = PHASE_MSGOUT;
  1581. NCR5380_transfer_pio(host, &phase, &len, &msgptr);
  1582. /*
  1583. * If we got here, and the command completed successfully,
  1584. * we're about to go into bus free state.
  1585. */
  1586. return len ? -1 : 0;
  1587. }
  1588. #if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL)
  1589. /*
  1590. * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance,
  1591. * unsigned char *phase, int *count, unsigned char **data)
  1592. *
  1593. * Purpose : transfers data in given phase using either real
  1594. * or pseudo DMA.
  1595. *
  1596. * Inputs : instance - instance of driver, *phase - pointer to
  1597. * what phase is expected, *count - pointer to number of
  1598. * bytes to transfer, **data - pointer to data pointer.
  1599. *
  1600. * Returns : -1 when different phase is entered without transferring
  1601. * maximum number of bytes, 0 if all bytes or transferred or exit
  1602. * is in same phase.
  1603. *
  1604. * Also, *phase, *count, *data are modified in place.
  1605. *
  1606. * Locks: io_request lock held by caller
  1607. */
  1608. static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) {
  1609. NCR5380_local_declare();
  1610. register int c = *count;
  1611. register unsigned char p = *phase;
  1612. register unsigned char *d = *data;
  1613. unsigned char tmp;
  1614. int foo;
  1615. #if defined(REAL_DMA_POLL)
  1616. int cnt, toPIO;
  1617. unsigned char saved_data = 0, overrun = 0, residue;
  1618. #endif
  1619. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  1620. NCR5380_setup(instance);
  1621. if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
  1622. *phase = tmp;
  1623. return -1;
  1624. }
  1625. #if defined(REAL_DMA) || defined(REAL_DMA_POLL)
  1626. #ifdef READ_OVERRUNS
  1627. if (p & SR_IO) {
  1628. c -= 2;
  1629. }
  1630. #endif
  1631. dprintk(NDEBUG_DMA, ("scsi%d : initializing DMA channel %d for %s, %d bytes %s %0x\n", instance->host_no, instance->dma_channel, (p & SR_IO) ? "reading" : "writing", c, (p & SR_IO) ? "to" : "from", (unsigned) d));
  1632. hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c);
  1633. #endif
  1634. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
  1635. #ifdef REAL_DMA
  1636. NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
  1637. #elif defined(REAL_DMA_POLL)
  1638. NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
  1639. #else
  1640. /*
  1641. * Note : on my sample board, watch-dog timeouts occurred when interrupts
  1642. * were not disabled for the duration of a single DMA transfer, from
  1643. * before the setting of DMA mode to after transfer of the last byte.
  1644. */
  1645. #if defined(PSEUDO_DMA) && defined(UNSAFE)
  1646. spin_unlock_irq(instance->host_lock);
  1647. #endif
  1648. /* KLL May need eop and parity in 53c400 */
  1649. if (hostdata->flags & FLAG_NCR53C400)
  1650. NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE |
  1651. MR_ENABLE_PAR_CHECK | MR_ENABLE_PAR_INTR |
  1652. MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
  1653. else
  1654. NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE);
  1655. #endif /* def REAL_DMA */
  1656. dprintk(NDEBUG_DMA, ("scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG)));
  1657. /*
  1658. * On the PAS16 at least I/O recovery delays are not needed here.
  1659. * Everyone else seems to want them.
  1660. */
  1661. if (p & SR_IO) {
  1662. io_recovery_delay(1);
  1663. NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
  1664. } else {
  1665. io_recovery_delay(1);
  1666. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
  1667. io_recovery_delay(1);
  1668. NCR5380_write(START_DMA_SEND_REG, 0);
  1669. io_recovery_delay(1);
  1670. }
  1671. #if defined(REAL_DMA_POLL)
  1672. do {
  1673. tmp = NCR5380_read(BUS_AND_STATUS_REG);
  1674. } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER)));
  1675. /*
  1676. At this point, either we've completed DMA, or we have a phase mismatch,
  1677. or we've unexpectedly lost BUSY (which is a real error).
  1678. For write DMAs, we want to wait until the last byte has been
  1679. transferred out over the bus before we turn off DMA mode. Alas, there
  1680. seems to be no terribly good way of doing this on a 5380 under all
  1681. conditions. For non-scatter-gather operations, we can wait until REQ
  1682. and ACK both go false, or until a phase mismatch occurs. Gather-writes
  1683. are nastier, since the device will be expecting more data than we
  1684. are prepared to send it, and REQ will remain asserted. On a 53C8[01] we
  1685. could test LAST BIT SENT to assure transfer (I imagine this is precisely
  1686. why this signal was added to the newer chips) but on the older 538[01]
  1687. this signal does not exist. The workaround for this lack is a watchdog;
  1688. we bail out of the wait-loop after a modest amount of wait-time if
  1689. the usual exit conditions are not met. Not a terribly clean or
  1690. correct solution :-%
  1691. Reads are equally tricky due to a nasty characteristic of the NCR5380.
  1692. If the chip is in DMA mode for an READ, it will respond to a target's
  1693. REQ by latching the SCSI data into the INPUT DATA register and asserting
  1694. ACK, even if it has _already_ been notified by the DMA controller that
  1695. the current DMA transfer has completed! If the NCR5380 is then taken
  1696. out of DMA mode, this already-acknowledged byte is lost.
  1697. This is not a problem for "one DMA transfer per command" reads, because
  1698. the situation will never arise... either all of the data is DMA'ed
  1699. properly, or the target switches to MESSAGE IN phase to signal a
  1700. disconnection (either operation bringing the DMA to a clean halt).
  1701. However, in order to handle scatter-reads, we must work around the
  1702. problem. The chosen fix is to DMA N-2 bytes, then check for the
  1703. condition before taking the NCR5380 out of DMA mode. One or two extra
  1704. bytes are transferred via PIO as necessary to fill out the original
  1705. request.
  1706. */
  1707. if (p & SR_IO) {
  1708. #ifdef READ_OVERRUNS
  1709. udelay(10);
  1710. if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) == (BASR_PHASE_MATCH | BASR_ACK))) {
  1711. saved_data = NCR5380_read(INPUT_DATA_REGISTER);
  1712. overrun = 1;
  1713. }
  1714. #endif
  1715. } else {
  1716. int limit = 100;
  1717. while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) {
  1718. if (!(tmp & BASR_PHASE_MATCH))
  1719. break;
  1720. if (--limit < 0)
  1721. break;
  1722. }
  1723. }
  1724. dprintk(NDEBUG_DMA, ("scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n", instance->host_no, tmp, NCR5380_read(STATUS_REG)));
  1725. NCR5380_write(MODE_REG, MR_BASE);
  1726. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1727. residue = NCR5380_dma_residual(instance);
  1728. c -= residue;
  1729. *count -= c;
  1730. *data += c;
  1731. *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
  1732. #ifdef READ_OVERRUNS
  1733. if (*phase == p && (p & SR_IO) && residue == 0) {
  1734. if (overrun) {
  1735. dprintk(NDEBUG_DMA, ("Got an input overrun, using saved byte\n"));
  1736. **data = saved_data;
  1737. *data += 1;
  1738. *count -= 1;
  1739. cnt = toPIO = 1;
  1740. } else {
  1741. printk("No overrun??\n");
  1742. cnt = toPIO = 2;
  1743. }
  1744. dprintk(NDEBUG_DMA, ("Doing %d-byte PIO to 0x%X\n", cnt, *data));
  1745. NCR5380_transfer_pio(instance, phase, &cnt, data);
  1746. *count -= toPIO - cnt;
  1747. }
  1748. #endif
  1749. dprintk(NDEBUG_DMA, ("Return with data ptr = 0x%X, count %d, last 0x%X, next 0x%X\n", *data, *count, *(*data + *count - 1), *(*data + *count)));
  1750. return 0;
  1751. #elif defined(REAL_DMA)
  1752. return 0;
  1753. #else /* defined(REAL_DMA_POLL) */
  1754. if (p & SR_IO) {
  1755. #ifdef DMA_WORKS_RIGHT
  1756. foo = NCR5380_pread(instance, d, c);
  1757. #else
  1758. int diff = 1;
  1759. if (hostdata->flags & FLAG_NCR53C400) {
  1760. diff = 0;
  1761. }
  1762. if (!(foo = NCR5380_pread(instance, d, c - diff))) {
  1763. /*
  1764. * We can't disable DMA mode after successfully transferring
  1765. * what we plan to be the last byte, since that would open up
  1766. * a race condition where if the target asserted REQ before
  1767. * we got the DMA mode reset, the NCR5380 would have latched
  1768. * an additional byte into the INPUT DATA register and we'd
  1769. * have dropped it.
  1770. *
  1771. * The workaround was to transfer one fewer bytes than we
  1772. * intended to with the pseudo-DMA read function, wait for
  1773. * the chip to latch the last byte, read it, and then disable
  1774. * pseudo-DMA mode.
  1775. *
  1776. * After REQ is asserted, the NCR5380 asserts DRQ and ACK.
  1777. * REQ is deasserted when ACK is asserted, and not reasserted
  1778. * until ACK goes false. Since the NCR5380 won't lower ACK
  1779. * until DACK is asserted, which won't happen unless we twiddle
  1780. * the DMA port or we take the NCR5380 out of DMA mode, we
  1781. * can guarantee that we won't handshake another extra
  1782. * byte.
  1783. */
  1784. if (!(hostdata->flags & FLAG_NCR53C400)) {
  1785. while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ));
  1786. /* Wait for clean handshake */
  1787. while (NCR5380_read(STATUS_REG) & SR_REQ);
  1788. d[c - 1] = NCR5380_read(INPUT_DATA_REG);
  1789. }
  1790. }
  1791. #endif
  1792. } else {
  1793. #ifdef DMA_WORKS_RIGHT
  1794. foo = NCR5380_pwrite(instance, d, c);
  1795. #else
  1796. int timeout;
  1797. dprintk(NDEBUG_C400_PWRITE, ("About to pwrite %d bytes\n", c));
  1798. if (!(foo = NCR5380_pwrite(instance, d, c))) {
  1799. /*
  1800. * Wait for the last byte to be sent. If REQ is being asserted for
  1801. * the byte we're interested, we'll ACK it and it will go false.
  1802. */
  1803. if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) {
  1804. timeout = 20000;
  1805. while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH));
  1806. if (!timeout)
  1807. dprintk(NDEBUG_LAST_BYTE_SENT, ("scsi%d : timed out on last byte\n", instance->host_no));
  1808. if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) {
  1809. hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT;
  1810. if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) {
  1811. hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT;
  1812. dprintk(NDEBUG_LAST_WRITE_SENT, ("scsi%d : last bit sent works\n", instance->host_no));
  1813. }
  1814. }
  1815. } else {
  1816. dprintk(NDEBUG_C400_PWRITE, ("Waiting for LASTBYTE\n"));
  1817. while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT));
  1818. dprintk(NDEBUG_C400_PWRITE, ("Got LASTBYTE\n"));
  1819. }
  1820. }
  1821. #endif
  1822. }
  1823. NCR5380_write(MODE_REG, MR_BASE);
  1824. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1825. if ((!(p & SR_IO)) && (hostdata->flags & FLAG_NCR53C400)) {
  1826. dprintk(NDEBUG_C400_PWRITE, ("53C400w: Checking for IRQ\n"));
  1827. if (NCR5380_read(BUS_AND_STATUS_REG) & BASR_IRQ) {
  1828. dprintk(NDEBUG_C400_PWRITE, ("53C400w: got it, reading reset interrupt reg\n"));
  1829. NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1830. } else {
  1831. printk("53C400w: IRQ NOT THERE!\n");
  1832. }
  1833. }
  1834. *data = d + c;
  1835. *count = 0;
  1836. *phase = NCR5380_read(STATUS_REG) & PHASE_MASK;
  1837. #if defined(PSEUDO_DMA) && defined(UNSAFE)
  1838. spin_lock_irq(instance->host_lock);
  1839. #endif /* defined(REAL_DMA_POLL) */
  1840. return foo;
  1841. #endif /* def REAL_DMA */
  1842. }
  1843. #endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */
  1844. /*
  1845. * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
  1846. *
  1847. * Purpose : run through the various SCSI phases and do as the target
  1848. * directs us to. Operates on the currently connected command,
  1849. * instance->connected.
  1850. *
  1851. * Inputs : instance, instance for which we are doing commands
  1852. *
  1853. * Side effects : SCSI things happen, the disconnected queue will be
  1854. * modified if a command disconnects, *instance->connected will
  1855. * change.
  1856. *
  1857. * XXX Note : we need to watch for bus free or a reset condition here
  1858. * to recover from an unexpected bus free condition.
  1859. *
  1860. * Locks: io_request_lock held by caller in IRQ mode
  1861. */
  1862. static void NCR5380_information_transfer(struct Scsi_Host *instance) {
  1863. NCR5380_local_declare();
  1864. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)instance->hostdata;
  1865. unsigned char msgout = NOP;
  1866. int sink = 0;
  1867. int len;
  1868. #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
  1869. int transfersize;
  1870. #endif
  1871. unsigned char *data;
  1872. unsigned char phase, tmp, extended_msg[10], old_phase = 0xff;
  1873. Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
  1874. /* RvC: we need to set the end of the polling time */
  1875. unsigned long poll_time = jiffies + USLEEP_POLL;
  1876. NCR5380_setup(instance);
  1877. while (1) {
  1878. tmp = NCR5380_read(STATUS_REG);
  1879. /* We only have a valid SCSI phase when REQ is asserted */
  1880. if (tmp & SR_REQ) {
  1881. phase = (tmp & PHASE_MASK);
  1882. if (phase != old_phase) {
  1883. old_phase = phase;
  1884. NCR5380_dprint_phase(NDEBUG_INFORMATION, instance);
  1885. }
  1886. if (sink && (phase != PHASE_MSGOUT)) {
  1887. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
  1888. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
  1889. while (NCR5380_read(STATUS_REG) & SR_REQ);
  1890. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1891. sink = 0;
  1892. continue;
  1893. }
  1894. switch (phase) {
  1895. case PHASE_DATAIN:
  1896. case PHASE_DATAOUT:
  1897. #if (NDEBUG & NDEBUG_NO_DATAOUT)
  1898. printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance->host_no);
  1899. sink = 1;
  1900. do_abort(instance);
  1901. cmd->result = DID_ERROR << 16;
  1902. cmd->scsi_done(cmd);
  1903. return;
  1904. #endif
  1905. /*
  1906. * If there is no room left in the current buffer in the
  1907. * scatter-gather list, move onto the next one.
  1908. */
  1909. if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
  1910. ++cmd->SCp.buffer;
  1911. --cmd->SCp.buffers_residual;
  1912. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  1913. cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
  1914. dprintk(NDEBUG_INFORMATION, ("scsi%d : %d bytes and %d buffers left\n", instance->host_no, cmd->SCp.this_residual, cmd->SCp.buffers_residual));
  1915. }
  1916. /*
  1917. * The preferred transfer method is going to be
  1918. * PSEUDO-DMA for systems that are strictly PIO,
  1919. * since we can let the hardware do the handshaking.
  1920. *
  1921. * For this to work, we need to know the transfersize
  1922. * ahead of time, since the pseudo-DMA code will sit
  1923. * in an unconditional loop.
  1924. */
  1925. #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL)
  1926. /* KLL
  1927. * PSEUDO_DMA is defined here. If this is the g_NCR5380
  1928. * driver then it will always be defined, so the
  1929. * FLAG_NO_PSEUDO_DMA is used to inhibit PDMA in the base
  1930. * NCR5380 case. I think this is a fairly clean solution.
  1931. * We supplement these 2 if's with the flag.
  1932. */
  1933. #ifdef NCR5380_dma_xfer_len
  1934. if (!cmd->device->borken && !(hostdata->flags & FLAG_NO_PSEUDO_DMA) && (transfersize = NCR5380_dma_xfer_len(instance, cmd)) != 0) {
  1935. #else
  1936. transfersize = cmd->transfersize;
  1937. #ifdef LIMIT_TRANSFERSIZE /* If we have problems with interrupt service */
  1938. if (transfersize > 512)
  1939. transfersize = 512;
  1940. #endif /* LIMIT_TRANSFERSIZE */
  1941. if (!cmd->device->borken && transfersize && !(hostdata->flags & FLAG_NO_PSEUDO_DMA) && cmd->SCp.this_residual && !(cmd->SCp.this_residual % transfersize)) {
  1942. /* Limit transfers to 32K, for xx400 & xx406
  1943. * pseudoDMA that transfers in 128 bytes blocks. */
  1944. if (transfersize > 32 * 1024)
  1945. transfersize = 32 * 1024;
  1946. #endif
  1947. len = transfersize;
  1948. if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) {
  1949. /*
  1950. * If the watchdog timer fires, all future accesses to this
  1951. * device will use the polled-IO.
  1952. */
  1953. scmd_printk(KERN_INFO, cmd,
  1954. "switching to slow handshake\n");
  1955. cmd->device->borken = 1;
  1956. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1957. sink = 1;
  1958. do_abort(instance);
  1959. cmd->result = DID_ERROR << 16;
  1960. cmd->scsi_done(cmd);
  1961. /* XXX - need to source or sink data here, as appropriate */
  1962. } else
  1963. cmd->SCp.this_residual -= transfersize - len;
  1964. } else
  1965. #endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */
  1966. NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **)
  1967. &cmd->SCp.ptr);
  1968. break;
  1969. case PHASE_MSGIN:
  1970. len = 1;
  1971. data = &tmp;
  1972. NCR5380_transfer_pio(instance, &phase, &len, &data);
  1973. cmd->SCp.Message = tmp;
  1974. switch (tmp) {
  1975. /*
  1976. * Linking lets us reduce the time required to get the
  1977. * next command out to the device, hopefully this will
  1978. * mean we don't waste another revolution due to the delays
  1979. * required by ARBITRATION and another SELECTION.
  1980. *
  1981. * In the current implementation proposal, low level drivers
  1982. * merely have to start the next command, pointed to by
  1983. * next_link, done() is called as with unlinked commands.
  1984. */
  1985. #ifdef LINKED
  1986. case LINKED_CMD_COMPLETE:
  1987. case LINKED_FLG_CMD_COMPLETE:
  1988. /* Accept message by clearing ACK */
  1989. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1990. dprintk(NDEBUG_LINKED, ("scsi%d : target %d lun %d linked command complete.\n", instance->host_no, cmd->device->id, cmd->device->lun));
  1991. /*
  1992. * Sanity check : A linked command should only terminate with
  1993. * one of these messages if there are more linked commands
  1994. * available.
  1995. */
  1996. if (!cmd->next_link) {
  1997. printk("scsi%d : target %d lun %d linked command complete, no next_link\n" instance->host_no, cmd->device->id, cmd->device->lun);
  1998. sink = 1;
  1999. do_abort(instance);
  2000. return;
  2001. }
  2002. initialize_SCp(cmd->next_link);
  2003. /* The next command is still part of this process */
  2004. cmd->next_link->tag = cmd->tag;
  2005. cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
  2006. dprintk(NDEBUG_LINKED, ("scsi%d : target %d lun %d linked request done, calling scsi_done().\n", instance->host_no, cmd->device->id, cmd->device->lun));
  2007. collect_stats(hostdata, cmd);
  2008. cmd->scsi_done(cmd);
  2009. cmd = hostdata->connected;
  2010. break;
  2011. #endif /* def LINKED */
  2012. case ABORT:
  2013. case COMMAND_COMPLETE:
  2014. /* Accept message by clearing ACK */
  2015. sink = 1;
  2016. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2017. hostdata->connected = NULL;
  2018. dprintk(NDEBUG_QUEUES, ("scsi%d : command for target %d, lun %d completed\n", instance->host_no, cmd->device->id, cmd->device->lun));
  2019. hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
  2020. /*
  2021. * I'm not sure what the correct thing to do here is :
  2022. *
  2023. * If the command that just executed is NOT a request
  2024. * sense, the obvious thing to do is to set the result
  2025. * code to the values of the stored parameters.
  2026. *
  2027. * If it was a REQUEST SENSE command, we need some way
  2028. * to differentiate between the failure code of the original
  2029. * and the failure code of the REQUEST sense - the obvious
  2030. * case is success, where we fall through and leave the result
  2031. * code unchanged.
  2032. *
  2033. * The non-obvious place is where the REQUEST SENSE failed
  2034. */
  2035. if (cmd->cmnd[0] != REQUEST_SENSE)
  2036. cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
  2037. else if (status_byte(cmd->SCp.Status) != GOOD)
  2038. cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
  2039. #ifdef AUTOSENSE
  2040. if ((cmd->cmnd[0] == REQUEST_SENSE) &&
  2041. hostdata->ses.cmd_len) {
  2042. scsi_eh_restore_cmnd(cmd, &hostdata->ses);
  2043. hostdata->ses.cmd_len = 0 ;
  2044. }
  2045. if ((cmd->cmnd[0] != REQUEST_SENSE) && (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
  2046. scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0);
  2047. dprintk(NDEBUG_AUTOSENSE, ("scsi%d : performing request sense\n", instance->host_no));
  2048. LIST(cmd, hostdata->issue_queue);
  2049. cmd->host_scribble = (unsigned char *)
  2050. hostdata->issue_queue;
  2051. hostdata->issue_queue = (Scsi_Cmnd *) cmd;
  2052. dprintk(NDEBUG_QUEUES, ("scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no));
  2053. } else
  2054. #endif /* def AUTOSENSE */
  2055. {
  2056. collect_stats(hostdata, cmd);
  2057. cmd->scsi_done(cmd);
  2058. }
  2059. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2060. /*
  2061. * Restore phase bits to 0 so an interrupted selection,
  2062. * arbitration can resume.
  2063. */
  2064. NCR5380_write(TARGET_COMMAND_REG, 0);
  2065. while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
  2066. barrier();
  2067. return;
  2068. case MESSAGE_REJECT:
  2069. /* Accept message by clearing ACK */
  2070. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2071. switch (hostdata->last_message) {
  2072. case HEAD_OF_QUEUE_TAG:
  2073. case ORDERED_QUEUE_TAG:
  2074. case SIMPLE_QUEUE_TAG:
  2075. cmd->device->simple_tags = 0;
  2076. hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
  2077. break;
  2078. default:
  2079. break;
  2080. }
  2081. case DISCONNECT:{
  2082. /* Accept message by clearing ACK */
  2083. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2084. cmd->device->disconnect = 1;
  2085. LIST(cmd, hostdata->disconnected_queue);
  2086. cmd->host_scribble = (unsigned char *)
  2087. hostdata->disconnected_queue;
  2088. hostdata->connected = NULL;
  2089. hostdata->disconnected_queue = cmd;
  2090. dprintk(NDEBUG_QUEUES, ("scsi%d : command for target %d lun %d was moved from connected to" " the disconnected_queue\n", instance->host_no, cmd->device->id, cmd->device->lun));
  2091. /*
  2092. * Restore phase bits to 0 so an interrupted selection,
  2093. * arbitration can resume.
  2094. */
  2095. NCR5380_write(TARGET_COMMAND_REG, 0);
  2096. /* Enable reselect interrupts */
  2097. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2098. /* Wait for bus free to avoid nasty timeouts - FIXME timeout !*/
  2099. /* NCR538_poll_politely(instance, STATUS_REG, SR_BSY, 0, 30 * HZ); */
  2100. while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
  2101. barrier();
  2102. return;
  2103. }
  2104. /*
  2105. * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
  2106. * operation, in violation of the SCSI spec so we can safely
  2107. * ignore SAVE/RESTORE pointers calls.
  2108. *
  2109. * Unfortunately, some disks violate the SCSI spec and
  2110. * don't issue the required SAVE_POINTERS message before
  2111. * disconnecting, and we have to break spec to remain
  2112. * compatible.
  2113. */
  2114. case SAVE_POINTERS:
  2115. case RESTORE_POINTERS:
  2116. /* Accept message by clearing ACK */
  2117. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2118. break;
  2119. case EXTENDED_MESSAGE:
  2120. /*
  2121. * Extended messages are sent in the following format :
  2122. * Byte
  2123. * 0 EXTENDED_MESSAGE == 1
  2124. * 1 length (includes one byte for code, doesn't
  2125. * include first two bytes)
  2126. * 2 code
  2127. * 3..length+1 arguments
  2128. *
  2129. * Start the extended message buffer with the EXTENDED_MESSAGE
  2130. * byte, since spi_print_msg() wants the whole thing.
  2131. */
  2132. extended_msg[0] = EXTENDED_MESSAGE;
  2133. /* Accept first byte by clearing ACK */
  2134. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2135. dprintk(NDEBUG_EXTENDED, ("scsi%d : receiving extended message\n", instance->host_no));
  2136. len = 2;
  2137. data = extended_msg + 1;
  2138. phase = PHASE_MSGIN;
  2139. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2140. dprintk(NDEBUG_EXTENDED, ("scsi%d : length=%d, code=0x%02x\n", instance->host_no, (int) extended_msg[1], (int) extended_msg[2]));
  2141. if (!len && extended_msg[1] <= (sizeof(extended_msg) - 1)) {
  2142. /* Accept third byte by clearing ACK */
  2143. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2144. len = extended_msg[1] - 1;
  2145. data = extended_msg + 3;
  2146. phase = PHASE_MSGIN;
  2147. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2148. dprintk(NDEBUG_EXTENDED, ("scsi%d : message received, residual %d\n", instance->host_no, len));
  2149. switch (extended_msg[2]) {
  2150. case EXTENDED_SDTR:
  2151. case EXTENDED_WDTR:
  2152. case EXTENDED_MODIFY_DATA_POINTER:
  2153. case EXTENDED_EXTENDED_IDENTIFY:
  2154. tmp = 0;
  2155. }
  2156. } else if (len) {
  2157. printk("scsi%d: error receiving extended message\n", instance->host_no);
  2158. tmp = 0;
  2159. } else {
  2160. printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]);
  2161. tmp = 0;
  2162. }
  2163. /* Fall through to reject message */
  2164. /*
  2165. * If we get something weird that we aren't expecting,
  2166. * reject it.
  2167. */
  2168. default:
  2169. if (!tmp) {
  2170. printk("scsi%d: rejecting message ", instance->host_no);
  2171. spi_print_msg(extended_msg);
  2172. printk("\n");
  2173. } else if (tmp != EXTENDED_MESSAGE)
  2174. scmd_printk(KERN_INFO, cmd,
  2175. "rejecting unknown message %02x\n",tmp);
  2176. else
  2177. scmd_printk(KERN_INFO, cmd,
  2178. "rejecting unknown extended message code %02x, length %d\n", extended_msg[1], extended_msg[0]);
  2179. msgout = MESSAGE_REJECT;
  2180. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  2181. break;
  2182. } /* switch (tmp) */
  2183. break;
  2184. case PHASE_MSGOUT:
  2185. len = 1;
  2186. data = &msgout;
  2187. hostdata->last_message = msgout;
  2188. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2189. if (msgout == ABORT) {
  2190. hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
  2191. hostdata->connected = NULL;
  2192. cmd->result = DID_ERROR << 16;
  2193. collect_stats(hostdata, cmd);
  2194. cmd->scsi_done(cmd);
  2195. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2196. return;
  2197. }
  2198. msgout = NOP;
  2199. break;
  2200. case PHASE_CMDOUT:
  2201. len = cmd->cmd_len;
  2202. data = cmd->cmnd;
  2203. /*
  2204. * XXX for performance reasons, on machines with a
  2205. * PSEUDO-DMA architecture we should probably
  2206. * use the dma transfer function.
  2207. */
  2208. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2209. if (!cmd->device->disconnect && should_disconnect(cmd->cmnd[0])) {
  2210. NCR5380_set_timer(hostdata, USLEEP_SLEEP);
  2211. dprintk(NDEBUG_USLEEP, ("scsi%d : issued command, sleeping until %ul\n", instance->host_no, hostdata->time_expires));
  2212. return;
  2213. }
  2214. break;
  2215. case PHASE_STATIN:
  2216. len = 1;
  2217. data = &tmp;
  2218. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2219. cmd->SCp.Status = tmp;
  2220. break;
  2221. default:
  2222. printk("scsi%d : unknown phase\n", instance->host_no);
  2223. NCR5380_dprint(NDEBUG_ALL, instance);
  2224. } /* switch(phase) */
  2225. } /* if (tmp * SR_REQ) */
  2226. else {
  2227. /* RvC: go to sleep if polling time expired
  2228. */
  2229. if (!cmd->device->disconnect && time_after_eq(jiffies, poll_time)) {
  2230. NCR5380_set_timer(hostdata, USLEEP_SLEEP);
  2231. dprintk(NDEBUG_USLEEP, ("scsi%d : poll timed out, sleeping until %ul\n", instance->host_no, hostdata->time_expires));
  2232. return;
  2233. }
  2234. }
  2235. } /* while (1) */
  2236. }
  2237. /*
  2238. * Function : void NCR5380_reselect (struct Scsi_Host *instance)
  2239. *
  2240. * Purpose : does reselection, initializing the instance->connected
  2241. * field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q
  2242. * nexus has been reestablished,
  2243. *
  2244. * Inputs : instance - this instance of the NCR5380.
  2245. *
  2246. * Locks: io_request_lock held by caller if IRQ driven
  2247. */
  2248. static void NCR5380_reselect(struct Scsi_Host *instance) {
  2249. NCR5380_local_declare();
  2250. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)
  2251. instance->hostdata;
  2252. unsigned char target_mask;
  2253. unsigned char lun, phase;
  2254. int len;
  2255. unsigned char msg[3];
  2256. unsigned char *data;
  2257. Scsi_Cmnd *tmp = NULL, *prev;
  2258. int abort = 0;
  2259. NCR5380_setup(instance);
  2260. /*
  2261. * Disable arbitration, etc. since the host adapter obviously
  2262. * lost, and tell an interrupted NCR5380_select() to restart.
  2263. */
  2264. NCR5380_write(MODE_REG, MR_BASE);
  2265. hostdata->restart_select = 1;
  2266. target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
  2267. dprintk(NDEBUG_SELECTION, ("scsi%d : reselect\n", instance->host_no));
  2268. /*
  2269. * At this point, we have detected that our SCSI ID is on the bus,
  2270. * SEL is true and BSY was false for at least one bus settle delay
  2271. * (400 ns).
  2272. *
  2273. * We must assert BSY ourselves, until the target drops the SEL
  2274. * signal.
  2275. */
  2276. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
  2277. /* FIXME: timeout too long, must fail to workqueue */
  2278. if(NCR5380_poll_politely(instance, STATUS_REG, SR_SEL, 0, 2*HZ)<0)
  2279. abort = 1;
  2280. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2281. /*
  2282. * Wait for target to go into MSGIN.
  2283. * FIXME: timeout needed and fail to work queeu
  2284. */
  2285. if(NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 2*HZ))
  2286. abort = 1;
  2287. len = 1;
  2288. data = msg;
  2289. phase = PHASE_MSGIN;
  2290. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2291. if (!(msg[0] & 0x80)) {
  2292. printk(KERN_ERR "scsi%d : expecting IDENTIFY message, got ", instance->host_no);
  2293. spi_print_msg(msg);
  2294. abort = 1;
  2295. } else {
  2296. /* Accept message by clearing ACK */
  2297. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2298. lun = (msg[0] & 0x07);
  2299. /*
  2300. * We need to add code for SCSI-II to track which devices have
  2301. * I_T_L_Q nexuses established, and which have simple I_T_L
  2302. * nexuses so we can chose to do additional data transfer.
  2303. */
  2304. /*
  2305. * Find the command corresponding to the I_T_L or I_T_L_Q nexus we
  2306. * just reestablished, and remove it from the disconnected queue.
  2307. */
  2308. for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble)
  2309. if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun)
  2310. ) {
  2311. if (prev) {
  2312. REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble);
  2313. prev->host_scribble = tmp->host_scribble;
  2314. } else {
  2315. REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble);
  2316. hostdata->disconnected_queue = (Scsi_Cmnd *) tmp->host_scribble;
  2317. }
  2318. tmp->host_scribble = NULL;
  2319. break;
  2320. }
  2321. if (!tmp) {
  2322. printk(KERN_ERR "scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n", instance->host_no, target_mask, lun);
  2323. /*
  2324. * Since we have an established nexus that we can't do anything with,
  2325. * we must abort it.
  2326. */
  2327. abort = 1;
  2328. }
  2329. }
  2330. if (abort) {
  2331. do_abort(instance);
  2332. } else {
  2333. hostdata->connected = tmp;
  2334. dprintk(NDEBUG_RESELECTION, ("scsi%d : nexus established, target = %d, lun = %d, tag = %d\n", instance->host_no, tmp->target, tmp->lun, tmp->tag));
  2335. }
  2336. }
  2337. /*
  2338. * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
  2339. *
  2340. * Purpose : called by interrupt handler when DMA finishes or a phase
  2341. * mismatch occurs (which would finish the DMA transfer).
  2342. *
  2343. * Inputs : instance - this instance of the NCR5380.
  2344. *
  2345. * Returns : pointer to the Scsi_Cmnd structure for which the I_T_L
  2346. * nexus has been reestablished, on failure NULL is returned.
  2347. */
  2348. #ifdef REAL_DMA
  2349. static void NCR5380_dma_complete(NCR5380_instance * instance) {
  2350. NCR5380_local_declare();
  2351. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  2352. int transferred;
  2353. NCR5380_setup(instance);
  2354. /*
  2355. * XXX this might not be right.
  2356. *
  2357. * Wait for final byte to transfer, ie wait for ACK to go false.
  2358. *
  2359. * We should use the Last Byte Sent bit, unfortunately this is
  2360. * not available on the 5380/5381 (only the various CMOS chips)
  2361. *
  2362. * FIXME: timeout, and need to handle long timeout/irq case
  2363. */
  2364. NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ);
  2365. NCR5380_write(MODE_REG, MR_BASE);
  2366. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2367. /*
  2368. * The only places we should see a phase mismatch and have to send
  2369. * data from the same set of pointers will be the data transfer
  2370. * phases. So, residual, requested length are only important here.
  2371. */
  2372. if (!(hostdata->connected->SCp.phase & SR_CD)) {
  2373. transferred = instance->dmalen - NCR5380_dma_residual();
  2374. hostdata->connected->SCp.this_residual -= transferred;
  2375. hostdata->connected->SCp.ptr += transferred;
  2376. }
  2377. }
  2378. #endif /* def REAL_DMA */
  2379. /*
  2380. * Function : int NCR5380_abort (Scsi_Cmnd *cmd)
  2381. *
  2382. * Purpose : abort a command
  2383. *
  2384. * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the
  2385. * host byte of the result field to, if zero DID_ABORTED is
  2386. * used.
  2387. *
  2388. * Returns : SUCCESS - success, FAILED on failure.
  2389. *
  2390. * XXX - there is no way to abort the command that is currently
  2391. * connected, you have to wait for it to complete. If this is
  2392. * a problem, we could implement longjmp() / setjmp(), setjmp()
  2393. * called where the loop started in NCR5380_main().
  2394. *
  2395. * Locks: host lock taken by caller
  2396. */
  2397. static int NCR5380_abort(Scsi_Cmnd * cmd) {
  2398. NCR5380_local_declare();
  2399. struct Scsi_Host *instance = cmd->device->host;
  2400. struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata;
  2401. Scsi_Cmnd *tmp, **prev;
  2402. printk(KERN_WARNING "scsi%d : aborting command\n", instance->host_no);
  2403. scsi_print_command(cmd);
  2404. NCR5380_print_status(instance);
  2405. NCR5380_setup(instance);
  2406. dprintk(NDEBUG_ABORT, ("scsi%d : abort called\n", instance->host_no));
  2407. dprintk(NDEBUG_ABORT, (" basr 0x%X, sr 0x%X\n", NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG)));
  2408. #if 0
  2409. /*
  2410. * Case 1 : If the command is the currently executing command,
  2411. * we'll set the aborted flag and return control so that
  2412. * information transfer routine can exit cleanly.
  2413. */
  2414. if (hostdata->connected == cmd) {
  2415. dprintk(NDEBUG_ABORT, ("scsi%d : aborting connected command\n", instance->host_no));
  2416. hostdata->aborted = 1;
  2417. /*
  2418. * We should perform BSY checking, and make sure we haven't slipped
  2419. * into BUS FREE.
  2420. */
  2421. NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN);
  2422. /*
  2423. * Since we can't change phases until we've completed the current
  2424. * handshake, we have to source or sink a byte of data if the current
  2425. * phase is not MSGOUT.
  2426. */
  2427. /*
  2428. * Return control to the executing NCR drive so we can clear the
  2429. * aborted flag and get back into our main loop.
  2430. */
  2431. return SUCCESS;
  2432. }
  2433. #endif
  2434. /*
  2435. * Case 2 : If the command hasn't been issued yet, we simply remove it
  2436. * from the issue queue.
  2437. */
  2438. dprintk(NDEBUG_ABORT, ("scsi%d : abort going into loop.\n", instance->host_no));
  2439. for (prev = (Scsi_Cmnd **) & (hostdata->issue_queue), tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp; prev = (Scsi_Cmnd **) & (tmp->host_scribble), tmp = (Scsi_Cmnd *) tmp->host_scribble)
  2440. if (cmd == tmp) {
  2441. REMOVE(5, *prev, tmp, tmp->host_scribble);
  2442. (*prev) = (Scsi_Cmnd *) tmp->host_scribble;
  2443. tmp->host_scribble = NULL;
  2444. tmp->result = DID_ABORT << 16;
  2445. dprintk(NDEBUG_ABORT, ("scsi%d : abort removed command from issue queue.\n", instance->host_no));
  2446. tmp->scsi_done(tmp);
  2447. return SUCCESS;
  2448. }
  2449. #if (NDEBUG & NDEBUG_ABORT)
  2450. /* KLL */
  2451. else if (prev == tmp)
  2452. printk(KERN_ERR "scsi%d : LOOP\n", instance->host_no);
  2453. #endif
  2454. /*
  2455. * Case 3 : If any commands are connected, we're going to fail the abort
  2456. * and let the high level SCSI driver retry at a later time or
  2457. * issue a reset.
  2458. *
  2459. * Timeouts, and therefore aborted commands, will be highly unlikely
  2460. * and handling them cleanly in this situation would make the common
  2461. * case of noresets less efficient, and would pollute our code. So,
  2462. * we fail.
  2463. */
  2464. if (hostdata->connected) {
  2465. dprintk(NDEBUG_ABORT, ("scsi%d : abort failed, command connected.\n", instance->host_no));
  2466. return FAILED;
  2467. }
  2468. /*
  2469. * Case 4: If the command is currently disconnected from the bus, and
  2470. * there are no connected commands, we reconnect the I_T_L or
  2471. * I_T_L_Q nexus associated with it, go into message out, and send
  2472. * an abort message.
  2473. *
  2474. * This case is especially ugly. In order to reestablish the nexus, we
  2475. * need to call NCR5380_select(). The easiest way to implement this
  2476. * function was to abort if the bus was busy, and let the interrupt
  2477. * handler triggered on the SEL for reselect take care of lost arbitrations
  2478. * where necessary, meaning interrupts need to be enabled.
  2479. *
  2480. * When interrupts are enabled, the queues may change - so we
  2481. * can't remove it from the disconnected queue before selecting it
  2482. * because that could cause a failure in hashing the nexus if that
  2483. * device reselected.
  2484. *
  2485. * Since the queues may change, we can't use the pointers from when we
  2486. * first locate it.
  2487. *
  2488. * So, we must first locate the command, and if NCR5380_select()
  2489. * succeeds, then issue the abort, relocate the command and remove
  2490. * it from the disconnected queue.
  2491. */
  2492. for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp; tmp = (Scsi_Cmnd *) tmp->host_scribble)
  2493. if (cmd == tmp) {
  2494. dprintk(NDEBUG_ABORT, ("scsi%d : aborting disconnected command.\n", instance->host_no));
  2495. if (NCR5380_select(instance, cmd, (int) cmd->tag))
  2496. return FAILED;
  2497. dprintk(NDEBUG_ABORT, ("scsi%d : nexus reestablished.\n", instance->host_no));
  2498. do_abort(instance);
  2499. for (prev = (Scsi_Cmnd **) & (hostdata->disconnected_queue), tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp; prev = (Scsi_Cmnd **) & (tmp->host_scribble), tmp = (Scsi_Cmnd *) tmp->host_scribble)
  2500. if (cmd == tmp) {
  2501. REMOVE(5, *prev, tmp, tmp->host_scribble);
  2502. *prev = (Scsi_Cmnd *) tmp->host_scribble;
  2503. tmp->host_scribble = NULL;
  2504. tmp->result = DID_ABORT << 16;
  2505. tmp->scsi_done(tmp);
  2506. return SUCCESS;
  2507. }
  2508. }
  2509. /*
  2510. * Case 5 : If we reached this point, the command was not found in any of
  2511. * the queues.
  2512. *
  2513. * We probably reached this point because of an unlikely race condition
  2514. * between the command completing successfully and the abortion code,
  2515. * so we won't panic, but we will notify the user in case something really
  2516. * broke.
  2517. */
  2518. printk(KERN_WARNING "scsi%d : warning : SCSI command probably completed successfully\n"
  2519. " before abortion\n", instance->host_no);
  2520. return FAILED;
  2521. }
  2522. /*
  2523. * Function : int NCR5380_bus_reset (Scsi_Cmnd *cmd)
  2524. *
  2525. * Purpose : reset the SCSI bus.
  2526. *
  2527. * Returns : SUCCESS
  2528. *
  2529. * Locks: host lock taken by caller
  2530. */
  2531. static int NCR5380_bus_reset(Scsi_Cmnd * cmd)
  2532. {
  2533. struct Scsi_Host *instance = cmd->device->host;
  2534. NCR5380_local_declare();
  2535. NCR5380_setup(instance);
  2536. NCR5380_print_status(instance);
  2537. spin_lock_irq(instance->host_lock);
  2538. do_reset(instance);
  2539. spin_unlock_irq(instance->host_lock);
  2540. return SUCCESS;
  2541. }