NCR5380.c 88 KB

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