NCR5380.c 91 KB

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