53c700.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174
  1. /* -*- mode: c; c-basic-offset: 8 -*- */
  2. /* NCR (or Symbios) 53c700 and 53c700-66 Driver
  3. *
  4. * Copyright (C) 2001 by James.Bottomley@HansenPartnership.com
  5. **-----------------------------------------------------------------------------
  6. **
  7. ** This program is free software; you can redistribute it and/or modify
  8. ** it under the terms of the GNU General Public License as published by
  9. ** the Free Software Foundation; either version 2 of the License, or
  10. ** (at your option) any later version.
  11. **
  12. ** This program is distributed in the hope that it will be useful,
  13. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ** GNU General Public License for more details.
  16. **
  17. ** You should have received a copy of the GNU General Public License
  18. ** along with this program; if not, write to the Free Software
  19. ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. **
  21. **-----------------------------------------------------------------------------
  22. */
  23. /* Notes:
  24. *
  25. * This driver is designed exclusively for these chips (virtually the
  26. * earliest of the scripts engine chips). They need their own drivers
  27. * because they are missing so many of the scripts and snazzy register
  28. * features of their elder brothers (the 710, 720 and 770).
  29. *
  30. * The 700 is the lowliest of the line, it can only do async SCSI.
  31. * The 700-66 can at least do synchronous SCSI up to 10MHz.
  32. *
  33. * The 700 chip has no host bus interface logic of its own. However,
  34. * it is usually mapped to a location with well defined register
  35. * offsets. Therefore, if you can determine the base address and the
  36. * irq your board incorporating this chip uses, you can probably use
  37. * this driver to run it (although you'll probably have to write a
  38. * minimal wrapper for the purpose---see the NCR_D700 driver for
  39. * details about how to do this).
  40. *
  41. *
  42. * TODO List:
  43. *
  44. * 1. Better statistics in the proc fs
  45. *
  46. * 2. Implement message queue (queues SCSI messages like commands) and make
  47. * the abort and device reset functions use them.
  48. * */
  49. /* CHANGELOG
  50. *
  51. * Version 2.8
  52. *
  53. * Fixed bad bug affecting tag starvation processing (previously the
  54. * driver would hang the system if too many tags starved. Also fixed
  55. * bad bug having to do with 10 byte command processing and REQUEST
  56. * SENSE (the command would loop forever getting a transfer length
  57. * mismatch in the CMD phase).
  58. *
  59. * Version 2.7
  60. *
  61. * Fixed scripts problem which caused certain devices (notably CDRWs)
  62. * to hang on initial INQUIRY. Updated NCR_700_readl/writel to use
  63. * __raw_readl/writel for parisc compatibility (Thomas
  64. * Bogendoerfer). Added missing SCp->request_bufflen initialisation
  65. * for sense requests (Ryan Bradetich).
  66. *
  67. * Version 2.6
  68. *
  69. * Following test of the 64 bit parisc kernel by Richard Hirst,
  70. * several problems have now been corrected. Also adds support for
  71. * consistent memory allocation.
  72. *
  73. * Version 2.5
  74. *
  75. * More Compatibility changes for 710 (now actually works). Enhanced
  76. * support for odd clock speeds which constrain SDTR negotiations.
  77. * correct cacheline separation for scsi messages and status for
  78. * incoherent architectures. Use of the pci mapping functions on
  79. * buffers to begin support for 64 bit drivers.
  80. *
  81. * Version 2.4
  82. *
  83. * Added support for the 53c710 chip (in 53c700 emulation mode only---no
  84. * special 53c710 instructions or registers are used).
  85. *
  86. * Version 2.3
  87. *
  88. * More endianness/cache coherency changes.
  89. *
  90. * Better bad device handling (handles devices lying about tag
  91. * queueing support and devices which fail to provide sense data on
  92. * contingent allegiance conditions)
  93. *
  94. * Many thanks to Richard Hirst <rhirst@linuxcare.com> for patiently
  95. * debugging this driver on the parisc architecture and suggesting
  96. * many improvements and bug fixes.
  97. *
  98. * Thanks also go to Linuxcare Inc. for providing several PARISC
  99. * machines for me to debug the driver on.
  100. *
  101. * Version 2.2
  102. *
  103. * Made the driver mem or io mapped; added endian invariance; added
  104. * dma cache flushing operations for architectures which need it;
  105. * added support for more varied clocking speeds.
  106. *
  107. * Version 2.1
  108. *
  109. * Initial modularisation from the D700. See NCR_D700.c for the rest of
  110. * the changelog.
  111. * */
  112. #define NCR_700_VERSION "2.8"
  113. #include <linux/config.h>
  114. #include <linux/kernel.h>
  115. #include <linux/types.h>
  116. #include <linux/string.h>
  117. #include <linux/ioport.h>
  118. #include <linux/delay.h>
  119. #include <linux/spinlock.h>
  120. #include <linux/completion.h>
  121. #include <linux/sched.h>
  122. #include <linux/init.h>
  123. #include <linux/proc_fs.h>
  124. #include <linux/blkdev.h>
  125. #include <linux/module.h>
  126. #include <linux/interrupt.h>
  127. #include <asm/dma.h>
  128. #include <asm/system.h>
  129. #include <asm/io.h>
  130. #include <asm/pgtable.h>
  131. #include <asm/byteorder.h>
  132. #include <scsi/scsi.h>
  133. #include <scsi/scsi_cmnd.h>
  134. #include <scsi/scsi_dbg.h>
  135. #include <scsi/scsi_eh.h>
  136. #include <scsi/scsi_host.h>
  137. #include <scsi/scsi_tcq.h>
  138. #include <scsi/scsi_transport.h>
  139. #include <scsi/scsi_transport_spi.h>
  140. #include "53c700.h"
  141. /* NOTE: For 64 bit drivers there are points in the code where we use
  142. * a non dereferenceable pointer to point to a structure in dma-able
  143. * memory (which is 32 bits) so that we can use all of the structure
  144. * operations but take the address at the end. This macro allows us
  145. * to truncate the 64 bit pointer down to 32 bits without the compiler
  146. * complaining */
  147. #define to32bit(x) ((__u32)((unsigned long)(x)))
  148. #ifdef NCR_700_DEBUG
  149. #define STATIC
  150. #else
  151. #define STATIC static
  152. #endif
  153. MODULE_AUTHOR("James Bottomley");
  154. MODULE_DESCRIPTION("53c700 and 53c700-66 Driver");
  155. MODULE_LICENSE("GPL");
  156. /* This is the script */
  157. #include "53c700_d.h"
  158. STATIC int NCR_700_queuecommand(struct scsi_cmnd *, void (*done)(struct scsi_cmnd *));
  159. STATIC int NCR_700_abort(struct scsi_cmnd * SCpnt);
  160. STATIC int NCR_700_bus_reset(struct scsi_cmnd * SCpnt);
  161. STATIC int NCR_700_host_reset(struct scsi_cmnd * SCpnt);
  162. STATIC void NCR_700_chip_setup(struct Scsi_Host *host);
  163. STATIC void NCR_700_chip_reset(struct Scsi_Host *host);
  164. STATIC int NCR_700_slave_configure(struct scsi_device *SDpnt);
  165. STATIC void NCR_700_slave_destroy(struct scsi_device *SDpnt);
  166. static int NCR_700_change_queue_depth(struct scsi_device *SDpnt, int depth);
  167. static int NCR_700_change_queue_type(struct scsi_device *SDpnt, int depth);
  168. STATIC struct device_attribute *NCR_700_dev_attrs[];
  169. STATIC struct scsi_transport_template *NCR_700_transport_template = NULL;
  170. static char *NCR_700_phase[] = {
  171. "",
  172. "after selection",
  173. "before command phase",
  174. "after command phase",
  175. "after status phase",
  176. "after data in phase",
  177. "after data out phase",
  178. "during data phase",
  179. };
  180. static char *NCR_700_condition[] = {
  181. "",
  182. "NOT MSG_OUT",
  183. "UNEXPECTED PHASE",
  184. "NOT MSG_IN",
  185. "UNEXPECTED MSG",
  186. "MSG_IN",
  187. "SDTR_MSG RECEIVED",
  188. "REJECT_MSG RECEIVED",
  189. "DISCONNECT_MSG RECEIVED",
  190. "MSG_OUT",
  191. "DATA_IN",
  192. };
  193. static char *NCR_700_fatal_messages[] = {
  194. "unexpected message after reselection",
  195. "still MSG_OUT after message injection",
  196. "not MSG_IN after selection",
  197. "Illegal message length received",
  198. };
  199. static char *NCR_700_SBCL_bits[] = {
  200. "IO ",
  201. "CD ",
  202. "MSG ",
  203. "ATN ",
  204. "SEL ",
  205. "BSY ",
  206. "ACK ",
  207. "REQ ",
  208. };
  209. static char *NCR_700_SBCL_to_phase[] = {
  210. "DATA_OUT",
  211. "DATA_IN",
  212. "CMD_OUT",
  213. "STATE",
  214. "ILLEGAL PHASE",
  215. "ILLEGAL PHASE",
  216. "MSG OUT",
  217. "MSG IN",
  218. };
  219. static __u8 NCR_700_SDTR_msg[] = {
  220. 0x01, /* Extended message */
  221. 0x03, /* Extended message Length */
  222. 0x01, /* SDTR Extended message */
  223. NCR_700_MIN_PERIOD,
  224. NCR_700_MAX_OFFSET
  225. };
  226. /* This translates the SDTR message offset and period to a value
  227. * which can be loaded into the SXFER_REG.
  228. *
  229. * NOTE: According to SCSI-2, the true transfer period (in ns) is
  230. * actually four times this period value */
  231. static inline __u8
  232. NCR_700_offset_period_to_sxfer(struct NCR_700_Host_Parameters *hostdata,
  233. __u8 offset, __u8 period)
  234. {
  235. int XFERP;
  236. __u8 min_xferp = (hostdata->chip710
  237. ? NCR_710_MIN_XFERP : NCR_700_MIN_XFERP);
  238. __u8 max_offset = (hostdata->chip710
  239. ? NCR_710_MAX_OFFSET : NCR_700_MAX_OFFSET);
  240. if(offset == 0)
  241. return 0;
  242. if(period < hostdata->min_period) {
  243. printk(KERN_WARNING "53c700: Period %dns is less than this chip's minimum, setting to %d\n", period*4, NCR_700_SDTR_msg[3]*4);
  244. period = hostdata->min_period;
  245. }
  246. XFERP = (period*4 * hostdata->sync_clock)/1000 - 4;
  247. if(offset > max_offset) {
  248. printk(KERN_WARNING "53c700: Offset %d exceeds chip maximum, setting to %d\n",
  249. offset, max_offset);
  250. offset = max_offset;
  251. }
  252. if(XFERP < min_xferp) {
  253. printk(KERN_WARNING "53c700: XFERP %d is less than minium, setting to %d\n",
  254. XFERP, min_xferp);
  255. XFERP = min_xferp;
  256. }
  257. return (offset & 0x0f) | (XFERP & 0x07)<<4;
  258. }
  259. static inline __u8
  260. NCR_700_get_SXFER(struct scsi_device *SDp)
  261. {
  262. struct NCR_700_Host_Parameters *hostdata =
  263. (struct NCR_700_Host_Parameters *)SDp->host->hostdata[0];
  264. return NCR_700_offset_period_to_sxfer(hostdata,
  265. spi_offset(SDp->sdev_target),
  266. spi_period(SDp->sdev_target));
  267. }
  268. struct Scsi_Host *
  269. NCR_700_detect(struct scsi_host_template *tpnt,
  270. struct NCR_700_Host_Parameters *hostdata, struct device *dev)
  271. {
  272. dma_addr_t pScript, pSlots;
  273. __u8 *memory;
  274. __u32 *script;
  275. struct Scsi_Host *host;
  276. static int banner = 0;
  277. int j;
  278. if(tpnt->sdev_attrs == NULL)
  279. tpnt->sdev_attrs = NCR_700_dev_attrs;
  280. memory = dma_alloc_noncoherent(hostdata->dev, TOTAL_MEM_SIZE,
  281. &pScript, GFP_KERNEL);
  282. if(memory == NULL) {
  283. printk(KERN_ERR "53c700: Failed to allocate memory for driver, detatching\n");
  284. return NULL;
  285. }
  286. script = (__u32 *)memory;
  287. hostdata->msgin = memory + MSGIN_OFFSET;
  288. hostdata->msgout = memory + MSGOUT_OFFSET;
  289. hostdata->status = memory + STATUS_OFFSET;
  290. /* all of these offsets are L1_CACHE_BYTES separated. It is fatal
  291. * if this isn't sufficient separation to avoid dma flushing issues */
  292. BUG_ON(!dma_is_consistent(pScript) && L1_CACHE_BYTES < dma_get_cache_alignment());
  293. hostdata->slots = (struct NCR_700_command_slot *)(memory + SLOTS_OFFSET);
  294. hostdata->dev = dev;
  295. pSlots = pScript + SLOTS_OFFSET;
  296. /* Fill in the missing routines from the host template */
  297. tpnt->queuecommand = NCR_700_queuecommand;
  298. tpnt->eh_abort_handler = NCR_700_abort;
  299. tpnt->eh_bus_reset_handler = NCR_700_bus_reset;
  300. tpnt->eh_host_reset_handler = NCR_700_host_reset;
  301. tpnt->can_queue = NCR_700_COMMAND_SLOTS_PER_HOST;
  302. tpnt->sg_tablesize = NCR_700_SG_SEGMENTS;
  303. tpnt->cmd_per_lun = NCR_700_CMD_PER_LUN;
  304. tpnt->use_clustering = ENABLE_CLUSTERING;
  305. tpnt->slave_configure = NCR_700_slave_configure;
  306. tpnt->slave_destroy = NCR_700_slave_destroy;
  307. tpnt->change_queue_depth = NCR_700_change_queue_depth;
  308. tpnt->change_queue_type = NCR_700_change_queue_type;
  309. if(tpnt->name == NULL)
  310. tpnt->name = "53c700";
  311. if(tpnt->proc_name == NULL)
  312. tpnt->proc_name = "53c700";
  313. host = scsi_host_alloc(tpnt, 4);
  314. if (!host)
  315. return NULL;
  316. memset(hostdata->slots, 0, sizeof(struct NCR_700_command_slot)
  317. * NCR_700_COMMAND_SLOTS_PER_HOST);
  318. for(j = 0; j < NCR_700_COMMAND_SLOTS_PER_HOST; j++) {
  319. dma_addr_t offset = (dma_addr_t)((unsigned long)&hostdata->slots[j].SG[0]
  320. - (unsigned long)&hostdata->slots[0].SG[0]);
  321. hostdata->slots[j].pSG = (struct NCR_700_SG_List *)((unsigned long)(pSlots + offset));
  322. if(j == 0)
  323. hostdata->free_list = &hostdata->slots[j];
  324. else
  325. hostdata->slots[j-1].ITL_forw = &hostdata->slots[j];
  326. hostdata->slots[j].state = NCR_700_SLOT_FREE;
  327. }
  328. for(j = 0; j < sizeof(SCRIPT)/sizeof(SCRIPT[0]); j++) {
  329. script[j] = bS_to_host(SCRIPT[j]);
  330. }
  331. /* adjust all labels to be bus physical */
  332. for(j = 0; j < PATCHES; j++) {
  333. script[LABELPATCHES[j]] = bS_to_host(pScript + SCRIPT[LABELPATCHES[j]]);
  334. }
  335. /* now patch up fixed addresses. */
  336. script_patch_32(script, MessageLocation,
  337. pScript + MSGOUT_OFFSET);
  338. script_patch_32(script, StatusAddress,
  339. pScript + STATUS_OFFSET);
  340. script_patch_32(script, ReceiveMsgAddress,
  341. pScript + MSGIN_OFFSET);
  342. hostdata->script = script;
  343. hostdata->pScript = pScript;
  344. dma_sync_single_for_device(hostdata->dev, pScript, sizeof(SCRIPT), DMA_TO_DEVICE);
  345. hostdata->state = NCR_700_HOST_FREE;
  346. hostdata->cmd = NULL;
  347. host->max_id = 7;
  348. host->max_lun = NCR_700_MAX_LUNS;
  349. BUG_ON(NCR_700_transport_template == NULL);
  350. host->transportt = NCR_700_transport_template;
  351. host->unique_id = (unsigned long)hostdata->base;
  352. hostdata->eh_complete = NULL;
  353. host->hostdata[0] = (unsigned long)hostdata;
  354. /* kick the chip */
  355. NCR_700_writeb(0xff, host, CTEST9_REG);
  356. if(hostdata->chip710)
  357. hostdata->rev = (NCR_700_readb(host, CTEST8_REG)>>4) & 0x0f;
  358. else
  359. hostdata->rev = (NCR_700_readb(host, CTEST7_REG)>>4) & 0x0f;
  360. hostdata->fast = (NCR_700_readb(host, CTEST9_REG) == 0);
  361. if(banner == 0) {
  362. printk(KERN_NOTICE "53c700: Version " NCR_700_VERSION " By James.Bottomley@HansenPartnership.com\n");
  363. banner = 1;
  364. }
  365. printk(KERN_NOTICE "scsi%d: %s rev %d %s\n", host->host_no,
  366. hostdata->chip710 ? "53c710" :
  367. (hostdata->fast ? "53c700-66" : "53c700"),
  368. hostdata->rev, hostdata->differential ?
  369. "(Differential)" : "");
  370. /* reset the chip */
  371. NCR_700_chip_reset(host);
  372. if (scsi_add_host(host, dev)) {
  373. dev_printk(KERN_ERR, dev, "53c700: scsi_add_host failed\n");
  374. scsi_host_put(host);
  375. return NULL;
  376. }
  377. spi_signalling(host) = hostdata->differential ? SPI_SIGNAL_HVD :
  378. SPI_SIGNAL_SE;
  379. return host;
  380. }
  381. int
  382. NCR_700_release(struct Scsi_Host *host)
  383. {
  384. struct NCR_700_Host_Parameters *hostdata =
  385. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  386. dma_free_noncoherent(hostdata->dev, TOTAL_MEM_SIZE,
  387. hostdata->script, hostdata->pScript);
  388. return 1;
  389. }
  390. static inline __u8
  391. NCR_700_identify(int can_disconnect, __u8 lun)
  392. {
  393. return IDENTIFY_BASE |
  394. ((can_disconnect) ? 0x40 : 0) |
  395. (lun & NCR_700_LUN_MASK);
  396. }
  397. /*
  398. * Function : static int data_residual (Scsi_Host *host)
  399. *
  400. * Purpose : return residual data count of what's in the chip. If you
  401. * really want to know what this function is doing, it's almost a
  402. * direct transcription of the algorithm described in the 53c710
  403. * guide, except that the DBC and DFIFO registers are only 6 bits
  404. * wide on a 53c700.
  405. *
  406. * Inputs : host - SCSI host */
  407. static inline int
  408. NCR_700_data_residual (struct Scsi_Host *host) {
  409. struct NCR_700_Host_Parameters *hostdata =
  410. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  411. int count, synchronous = 0;
  412. unsigned int ddir;
  413. if(hostdata->chip710) {
  414. count = ((NCR_700_readb(host, DFIFO_REG) & 0x7f) -
  415. (NCR_700_readl(host, DBC_REG) & 0x7f)) & 0x7f;
  416. } else {
  417. count = ((NCR_700_readb(host, DFIFO_REG) & 0x3f) -
  418. (NCR_700_readl(host, DBC_REG) & 0x3f)) & 0x3f;
  419. }
  420. if(hostdata->fast)
  421. synchronous = NCR_700_readb(host, SXFER_REG) & 0x0f;
  422. /* get the data direction */
  423. ddir = NCR_700_readb(host, CTEST0_REG) & 0x01;
  424. if (ddir) {
  425. /* Receive */
  426. if (synchronous)
  427. count += (NCR_700_readb(host, SSTAT2_REG) & 0xf0) >> 4;
  428. else
  429. if (NCR_700_readb(host, SSTAT1_REG) & SIDL_REG_FULL)
  430. ++count;
  431. } else {
  432. /* Send */
  433. __u8 sstat = NCR_700_readb(host, SSTAT1_REG);
  434. if (sstat & SODL_REG_FULL)
  435. ++count;
  436. if (synchronous && (sstat & SODR_REG_FULL))
  437. ++count;
  438. }
  439. #ifdef NCR_700_DEBUG
  440. if(count)
  441. printk("RESIDUAL IS %d (ddir %d)\n", count, ddir);
  442. #endif
  443. return count;
  444. }
  445. /* print out the SCSI wires and corresponding phase from the SBCL register
  446. * in the chip */
  447. static inline char *
  448. sbcl_to_string(__u8 sbcl)
  449. {
  450. int i;
  451. static char ret[256];
  452. ret[0]='\0';
  453. for(i=0; i<8; i++) {
  454. if((1<<i) & sbcl)
  455. strcat(ret, NCR_700_SBCL_bits[i]);
  456. }
  457. strcat(ret, NCR_700_SBCL_to_phase[sbcl & 0x07]);
  458. return ret;
  459. }
  460. static inline __u8
  461. bitmap_to_number(__u8 bitmap)
  462. {
  463. __u8 i;
  464. for(i=0; i<8 && !(bitmap &(1<<i)); i++)
  465. ;
  466. return i;
  467. }
  468. /* Pull a slot off the free list */
  469. STATIC struct NCR_700_command_slot *
  470. find_empty_slot(struct NCR_700_Host_Parameters *hostdata)
  471. {
  472. struct NCR_700_command_slot *slot = hostdata->free_list;
  473. if(slot == NULL) {
  474. /* sanity check */
  475. if(hostdata->command_slot_count != NCR_700_COMMAND_SLOTS_PER_HOST)
  476. printk(KERN_ERR "SLOTS FULL, but count is %d, should be %d\n", hostdata->command_slot_count, NCR_700_COMMAND_SLOTS_PER_HOST);
  477. return NULL;
  478. }
  479. if(slot->state != NCR_700_SLOT_FREE)
  480. /* should panic! */
  481. printk(KERN_ERR "BUSY SLOT ON FREE LIST!!!\n");
  482. hostdata->free_list = slot->ITL_forw;
  483. slot->ITL_forw = NULL;
  484. /* NOTE: set the state to busy here, not queued, since this
  485. * indicates the slot is in use and cannot be run by the IRQ
  486. * finish routine. If we cannot queue the command when it
  487. * is properly build, we then change to NCR_700_SLOT_QUEUED */
  488. slot->state = NCR_700_SLOT_BUSY;
  489. hostdata->command_slot_count++;
  490. return slot;
  491. }
  492. STATIC void
  493. free_slot(struct NCR_700_command_slot *slot,
  494. struct NCR_700_Host_Parameters *hostdata)
  495. {
  496. if((slot->state & NCR_700_SLOT_MASK) != NCR_700_SLOT_MAGIC) {
  497. printk(KERN_ERR "53c700: SLOT %p is not MAGIC!!!\n", slot);
  498. }
  499. if(slot->state == NCR_700_SLOT_FREE) {
  500. printk(KERN_ERR "53c700: SLOT %p is FREE!!!\n", slot);
  501. }
  502. slot->resume_offset = 0;
  503. slot->cmnd = NULL;
  504. slot->state = NCR_700_SLOT_FREE;
  505. slot->ITL_forw = hostdata->free_list;
  506. hostdata->free_list = slot;
  507. hostdata->command_slot_count--;
  508. }
  509. /* This routine really does very little. The command is indexed on
  510. the ITL and (if tagged) the ITLQ lists in _queuecommand */
  511. STATIC void
  512. save_for_reselection(struct NCR_700_Host_Parameters *hostdata,
  513. struct scsi_cmnd *SCp, __u32 dsp)
  514. {
  515. /* Its just possible that this gets executed twice */
  516. if(SCp != NULL) {
  517. struct NCR_700_command_slot *slot =
  518. (struct NCR_700_command_slot *)SCp->host_scribble;
  519. slot->resume_offset = dsp;
  520. }
  521. hostdata->state = NCR_700_HOST_FREE;
  522. hostdata->cmd = NULL;
  523. }
  524. STATIC inline void
  525. NCR_700_unmap(struct NCR_700_Host_Parameters *hostdata, struct scsi_cmnd *SCp,
  526. struct NCR_700_command_slot *slot)
  527. {
  528. if(SCp->sc_data_direction != DMA_NONE &&
  529. SCp->sc_data_direction != DMA_BIDIRECTIONAL) {
  530. if(SCp->use_sg) {
  531. dma_unmap_sg(hostdata->dev, SCp->buffer,
  532. SCp->use_sg, SCp->sc_data_direction);
  533. } else {
  534. dma_unmap_single(hostdata->dev, slot->dma_handle,
  535. SCp->request_bufflen,
  536. SCp->sc_data_direction);
  537. }
  538. }
  539. }
  540. STATIC inline void
  541. NCR_700_scsi_done(struct NCR_700_Host_Parameters *hostdata,
  542. struct scsi_cmnd *SCp, int result)
  543. {
  544. hostdata->state = NCR_700_HOST_FREE;
  545. hostdata->cmd = NULL;
  546. if(SCp != NULL) {
  547. struct NCR_700_command_slot *slot =
  548. (struct NCR_700_command_slot *)SCp->host_scribble;
  549. NCR_700_unmap(hostdata, SCp, slot);
  550. dma_unmap_single(hostdata->dev, slot->pCmd,
  551. sizeof(SCp->cmnd), DMA_TO_DEVICE);
  552. if(SCp->cmnd[0] == REQUEST_SENSE && SCp->cmnd[6] == NCR_700_INTERNAL_SENSE_MAGIC) {
  553. #ifdef NCR_700_DEBUG
  554. printk(" ORIGINAL CMD %p RETURNED %d, new return is %d sense is\n",
  555. SCp, SCp->cmnd[7], result);
  556. scsi_print_sense("53c700", SCp);
  557. #endif
  558. /* restore the old result if the request sense was
  559. * successful */
  560. if(result == 0)
  561. result = SCp->cmnd[7];
  562. /* now restore the original command */
  563. memcpy((void *) SCp->cmnd, (void *) SCp->data_cmnd,
  564. sizeof(SCp->data_cmnd));
  565. SCp->request_buffer = SCp->buffer;
  566. SCp->request_bufflen = SCp->bufflen;
  567. SCp->use_sg = SCp->old_use_sg;
  568. SCp->cmd_len = SCp->old_cmd_len;
  569. SCp->sc_data_direction = SCp->sc_old_data_direction;
  570. SCp->underflow = SCp->old_underflow;
  571. }
  572. free_slot(slot, hostdata);
  573. #ifdef NCR_700_DEBUG
  574. if(NCR_700_get_depth(SCp->device) == 0 ||
  575. NCR_700_get_depth(SCp->device) > SCp->device->queue_depth)
  576. printk(KERN_ERR "Invalid depth in NCR_700_scsi_done(): %d\n",
  577. NCR_700_get_depth(SCp->device));
  578. #endif /* NCR_700_DEBUG */
  579. NCR_700_set_depth(SCp->device, NCR_700_get_depth(SCp->device) - 1);
  580. SCp->host_scribble = NULL;
  581. SCp->result = result;
  582. SCp->scsi_done(SCp);
  583. } else {
  584. printk(KERN_ERR "53c700: SCSI DONE HAS NULL SCp\n");
  585. }
  586. }
  587. STATIC void
  588. NCR_700_internal_bus_reset(struct Scsi_Host *host)
  589. {
  590. /* Bus reset */
  591. NCR_700_writeb(ASSERT_RST, host, SCNTL1_REG);
  592. udelay(50);
  593. NCR_700_writeb(0, host, SCNTL1_REG);
  594. }
  595. STATIC void
  596. NCR_700_chip_setup(struct Scsi_Host *host)
  597. {
  598. struct NCR_700_Host_Parameters *hostdata =
  599. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  600. __u32 dcntl_extra = 0;
  601. __u8 min_period;
  602. __u8 min_xferp = (hostdata->chip710 ? NCR_710_MIN_XFERP : NCR_700_MIN_XFERP);
  603. if(hostdata->chip710) {
  604. __u8 burst_disable = hostdata->burst_disable
  605. ? BURST_DISABLE : 0;
  606. dcntl_extra = COMPAT_700_MODE;
  607. NCR_700_writeb(dcntl_extra, host, DCNTL_REG);
  608. NCR_700_writeb(BURST_LENGTH_8 | hostdata->dmode_extra,
  609. host, DMODE_710_REG);
  610. NCR_700_writeb(burst_disable | (hostdata->differential ?
  611. DIFF : 0), host, CTEST7_REG);
  612. NCR_700_writeb(BTB_TIMER_DISABLE, host, CTEST0_REG);
  613. NCR_700_writeb(FULL_ARBITRATION | ENABLE_PARITY | PARITY
  614. | AUTO_ATN, host, SCNTL0_REG);
  615. } else {
  616. NCR_700_writeb(BURST_LENGTH_8 | hostdata->dmode_extra,
  617. host, DMODE_700_REG);
  618. NCR_700_writeb(hostdata->differential ?
  619. DIFF : 0, host, CTEST7_REG);
  620. if(hostdata->fast) {
  621. /* this is for 700-66, does nothing on 700 */
  622. NCR_700_writeb(LAST_DIS_ENBL | ENABLE_ACTIVE_NEGATION
  623. | GENERATE_RECEIVE_PARITY, host,
  624. CTEST8_REG);
  625. } else {
  626. NCR_700_writeb(FULL_ARBITRATION | ENABLE_PARITY
  627. | PARITY | AUTO_ATN, host, SCNTL0_REG);
  628. }
  629. }
  630. NCR_700_writeb(1 << host->this_id, host, SCID_REG);
  631. NCR_700_writeb(0, host, SBCL_REG);
  632. NCR_700_writeb(ASYNC_OPERATION, host, SXFER_REG);
  633. NCR_700_writeb(PHASE_MM_INT | SEL_TIMEOUT_INT | GROSS_ERR_INT | UX_DISC_INT
  634. | RST_INT | PAR_ERR_INT | SELECT_INT, host, SIEN_REG);
  635. NCR_700_writeb(ABORT_INT | INT_INST_INT | ILGL_INST_INT, host, DIEN_REG);
  636. NCR_700_writeb(ENABLE_SELECT, host, SCNTL1_REG);
  637. if(hostdata->clock > 75) {
  638. printk(KERN_ERR "53c700: Clock speed %dMHz is too high: 75Mhz is the maximum this chip can be driven at\n", hostdata->clock);
  639. /* do the best we can, but the async clock will be out
  640. * of spec: sync divider 2, async divider 3 */
  641. DEBUG(("53c700: sync 2 async 3\n"));
  642. NCR_700_writeb(SYNC_DIV_2_0, host, SBCL_REG);
  643. NCR_700_writeb(ASYNC_DIV_3_0 | dcntl_extra, host, DCNTL_REG);
  644. hostdata->sync_clock = hostdata->clock/2;
  645. } else if(hostdata->clock > 50 && hostdata->clock <= 75) {
  646. /* sync divider 1.5, async divider 3 */
  647. DEBUG(("53c700: sync 1.5 async 3\n"));
  648. NCR_700_writeb(SYNC_DIV_1_5, host, SBCL_REG);
  649. NCR_700_writeb(ASYNC_DIV_3_0 | dcntl_extra, host, DCNTL_REG);
  650. hostdata->sync_clock = hostdata->clock*2;
  651. hostdata->sync_clock /= 3;
  652. } else if(hostdata->clock > 37 && hostdata->clock <= 50) {
  653. /* sync divider 1, async divider 2 */
  654. DEBUG(("53c700: sync 1 async 2\n"));
  655. NCR_700_writeb(SYNC_DIV_1_0, host, SBCL_REG);
  656. NCR_700_writeb(ASYNC_DIV_2_0 | dcntl_extra, host, DCNTL_REG);
  657. hostdata->sync_clock = hostdata->clock;
  658. } else if(hostdata->clock > 25 && hostdata->clock <=37) {
  659. /* sync divider 1, async divider 1.5 */
  660. DEBUG(("53c700: sync 1 async 1.5\n"));
  661. NCR_700_writeb(SYNC_DIV_1_0, host, SBCL_REG);
  662. NCR_700_writeb(ASYNC_DIV_1_5 | dcntl_extra, host, DCNTL_REG);
  663. hostdata->sync_clock = hostdata->clock;
  664. } else {
  665. DEBUG(("53c700: sync 1 async 1\n"));
  666. NCR_700_writeb(SYNC_DIV_1_0, host, SBCL_REG);
  667. NCR_700_writeb(ASYNC_DIV_1_0 | dcntl_extra, host, DCNTL_REG);
  668. /* sync divider 1, async divider 1 */
  669. hostdata->sync_clock = hostdata->clock;
  670. }
  671. /* Calculate the actual minimum period that can be supported
  672. * by our synchronous clock speed. See the 710 manual for
  673. * exact details of this calculation which is based on a
  674. * setting of the SXFER register */
  675. min_period = 1000*(4+min_xferp)/(4*hostdata->sync_clock);
  676. hostdata->min_period = NCR_700_MIN_PERIOD;
  677. if(min_period > NCR_700_MIN_PERIOD)
  678. hostdata->min_period = min_period;
  679. }
  680. STATIC void
  681. NCR_700_chip_reset(struct Scsi_Host *host)
  682. {
  683. struct NCR_700_Host_Parameters *hostdata =
  684. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  685. if(hostdata->chip710) {
  686. NCR_700_writeb(SOFTWARE_RESET_710, host, ISTAT_REG);
  687. udelay(100);
  688. NCR_700_writeb(0, host, ISTAT_REG);
  689. } else {
  690. NCR_700_writeb(SOFTWARE_RESET, host, DCNTL_REG);
  691. udelay(100);
  692. NCR_700_writeb(0, host, DCNTL_REG);
  693. }
  694. mdelay(1000);
  695. NCR_700_chip_setup(host);
  696. }
  697. /* The heart of the message processing engine is that the instruction
  698. * immediately after the INT is the normal case (and so must be CLEAR
  699. * ACK). If we want to do something else, we call that routine in
  700. * scripts and set temp to be the normal case + 8 (skipping the CLEAR
  701. * ACK) so that the routine returns correctly to resume its activity
  702. * */
  703. STATIC __u32
  704. process_extended_message(struct Scsi_Host *host,
  705. struct NCR_700_Host_Parameters *hostdata,
  706. struct scsi_cmnd *SCp, __u32 dsp, __u32 dsps)
  707. {
  708. __u32 resume_offset = dsp, temp = dsp + 8;
  709. __u8 pun = 0xff, lun = 0xff;
  710. if(SCp != NULL) {
  711. pun = SCp->device->id;
  712. lun = SCp->device->lun;
  713. }
  714. switch(hostdata->msgin[2]) {
  715. case A_SDTR_MSG:
  716. if(SCp != NULL && NCR_700_is_flag_set(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION)) {
  717. struct scsi_target *starget = SCp->device->sdev_target;
  718. __u8 period = hostdata->msgin[3];
  719. __u8 offset = hostdata->msgin[4];
  720. if(offset == 0 || period == 0) {
  721. offset = 0;
  722. period = 0;
  723. }
  724. spi_offset(starget) = offset;
  725. spi_period(starget) = period;
  726. if(NCR_700_is_flag_set(SCp->device, NCR_700_DEV_PRINT_SYNC_NEGOTIATION)) {
  727. spi_display_xfer_agreement(starget);
  728. NCR_700_clear_flag(SCp->device, NCR_700_DEV_PRINT_SYNC_NEGOTIATION);
  729. }
  730. NCR_700_set_flag(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC);
  731. NCR_700_clear_flag(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
  732. NCR_700_writeb(NCR_700_get_SXFER(SCp->device),
  733. host, SXFER_REG);
  734. } else {
  735. /* SDTR message out of the blue, reject it */
  736. printk(KERN_WARNING "scsi%d Unexpected SDTR msg\n",
  737. host->host_no);
  738. hostdata->msgout[0] = A_REJECT_MSG;
  739. dma_cache_sync(hostdata->msgout, 1, DMA_TO_DEVICE);
  740. script_patch_16(hostdata->script, MessageCount, 1);
  741. /* SendMsgOut returns, so set up the return
  742. * address */
  743. resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
  744. }
  745. break;
  746. case A_WDTR_MSG:
  747. printk(KERN_INFO "scsi%d: (%d:%d), Unsolicited WDTR after CMD, Rejecting\n",
  748. host->host_no, pun, lun);
  749. hostdata->msgout[0] = A_REJECT_MSG;
  750. dma_cache_sync(hostdata->msgout, 1, DMA_TO_DEVICE);
  751. script_patch_16(hostdata->script, MessageCount, 1);
  752. resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
  753. break;
  754. default:
  755. printk(KERN_INFO "scsi%d (%d:%d): Unexpected message %s: ",
  756. host->host_no, pun, lun,
  757. NCR_700_phase[(dsps & 0xf00) >> 8]);
  758. scsi_print_msg(hostdata->msgin);
  759. printk("\n");
  760. /* just reject it */
  761. hostdata->msgout[0] = A_REJECT_MSG;
  762. dma_cache_sync(hostdata->msgout, 1, DMA_TO_DEVICE);
  763. script_patch_16(hostdata->script, MessageCount, 1);
  764. /* SendMsgOut returns, so set up the return
  765. * address */
  766. resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
  767. }
  768. NCR_700_writel(temp, host, TEMP_REG);
  769. return resume_offset;
  770. }
  771. STATIC __u32
  772. process_message(struct Scsi_Host *host, struct NCR_700_Host_Parameters *hostdata,
  773. struct scsi_cmnd *SCp, __u32 dsp, __u32 dsps)
  774. {
  775. /* work out where to return to */
  776. __u32 temp = dsp + 8, resume_offset = dsp;
  777. __u8 pun = 0xff, lun = 0xff;
  778. if(SCp != NULL) {
  779. pun = SCp->device->id;
  780. lun = SCp->device->lun;
  781. }
  782. #ifdef NCR_700_DEBUG
  783. printk("scsi%d (%d:%d): message %s: ", host->host_no, pun, lun,
  784. NCR_700_phase[(dsps & 0xf00) >> 8]);
  785. scsi_print_msg(hostdata->msgin);
  786. printk("\n");
  787. #endif
  788. switch(hostdata->msgin[0]) {
  789. case A_EXTENDED_MSG:
  790. resume_offset = process_extended_message(host, hostdata, SCp,
  791. dsp, dsps);
  792. break;
  793. case A_REJECT_MSG:
  794. if(SCp != NULL && NCR_700_is_flag_set(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION)) {
  795. /* Rejected our sync negotiation attempt */
  796. spi_period(SCp->device->sdev_target) =
  797. spi_offset(SCp->device->sdev_target) = 0;
  798. NCR_700_set_flag(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC);
  799. NCR_700_clear_flag(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
  800. } else if(SCp != NULL && NCR_700_get_tag_neg_state(SCp->device) == NCR_700_DURING_TAG_NEGOTIATION) {
  801. /* rejected our first simple tag message */
  802. printk(KERN_WARNING "scsi%d (%d:%d) Rejected first tag queue attempt, turning off tag queueing\n", host->host_no, pun, lun);
  803. /* we're done negotiating */
  804. NCR_700_set_tag_neg_state(SCp->device, NCR_700_FINISHED_TAG_NEGOTIATION);
  805. hostdata->tag_negotiated &= ~(1<<SCp->device->id);
  806. SCp->device->tagged_supported = 0;
  807. scsi_deactivate_tcq(SCp->device, host->cmd_per_lun);
  808. } else {
  809. printk(KERN_WARNING "scsi%d (%d:%d) Unexpected REJECT Message %s\n",
  810. host->host_no, pun, lun,
  811. NCR_700_phase[(dsps & 0xf00) >> 8]);
  812. /* however, just ignore it */
  813. }
  814. break;
  815. case A_PARITY_ERROR_MSG:
  816. printk(KERN_ERR "scsi%d (%d:%d) Parity Error!\n", host->host_no,
  817. pun, lun);
  818. NCR_700_internal_bus_reset(host);
  819. break;
  820. case A_SIMPLE_TAG_MSG:
  821. printk(KERN_INFO "scsi%d (%d:%d) SIMPLE TAG %d %s\n", host->host_no,
  822. pun, lun, hostdata->msgin[1],
  823. NCR_700_phase[(dsps & 0xf00) >> 8]);
  824. /* just ignore it */
  825. break;
  826. default:
  827. printk(KERN_INFO "scsi%d (%d:%d): Unexpected message %s: ",
  828. host->host_no, pun, lun,
  829. NCR_700_phase[(dsps & 0xf00) >> 8]);
  830. scsi_print_msg(hostdata->msgin);
  831. printk("\n");
  832. /* just reject it */
  833. hostdata->msgout[0] = A_REJECT_MSG;
  834. dma_cache_sync(hostdata->msgout, 1, DMA_TO_DEVICE);
  835. script_patch_16(hostdata->script, MessageCount, 1);
  836. /* SendMsgOut returns, so set up the return
  837. * address */
  838. resume_offset = hostdata->pScript + Ent_SendMessageWithATN;
  839. break;
  840. }
  841. NCR_700_writel(temp, host, TEMP_REG);
  842. /* set us up to receive another message */
  843. dma_cache_sync(hostdata->msgin, MSG_ARRAY_SIZE, DMA_FROM_DEVICE);
  844. return resume_offset;
  845. }
  846. STATIC __u32
  847. process_script_interrupt(__u32 dsps, __u32 dsp, struct scsi_cmnd *SCp,
  848. struct Scsi_Host *host,
  849. struct NCR_700_Host_Parameters *hostdata)
  850. {
  851. __u32 resume_offset = 0;
  852. __u8 pun = 0xff, lun=0xff;
  853. if(SCp != NULL) {
  854. pun = SCp->device->id;
  855. lun = SCp->device->lun;
  856. }
  857. if(dsps == A_GOOD_STATUS_AFTER_STATUS) {
  858. DEBUG((" COMMAND COMPLETE, status=%02x\n",
  859. hostdata->status[0]));
  860. /* OK, if TCQ still under negotiation, we now know it works */
  861. if (NCR_700_get_tag_neg_state(SCp->device) == NCR_700_DURING_TAG_NEGOTIATION)
  862. NCR_700_set_tag_neg_state(SCp->device,
  863. NCR_700_FINISHED_TAG_NEGOTIATION);
  864. /* check for contingent allegiance contitions */
  865. if(status_byte(hostdata->status[0]) == CHECK_CONDITION ||
  866. status_byte(hostdata->status[0]) == COMMAND_TERMINATED) {
  867. struct NCR_700_command_slot *slot =
  868. (struct NCR_700_command_slot *)SCp->host_scribble;
  869. if(SCp->cmnd[0] == REQUEST_SENSE) {
  870. /* OOPS: bad device, returning another
  871. * contingent allegiance condition */
  872. printk(KERN_ERR "scsi%d (%d:%d) broken device is looping in contingent allegiance: ignoring\n", host->host_no, pun, lun);
  873. NCR_700_scsi_done(hostdata, SCp, hostdata->status[0]);
  874. } else {
  875. #ifdef NCR_DEBUG
  876. scsi_print_command(SCp);
  877. printk(" cmd %p has status %d, requesting sense\n",
  878. SCp, hostdata->status[0]);
  879. #endif
  880. /* we can destroy the command here
  881. * because the contingent allegiance
  882. * condition will cause a retry which
  883. * will re-copy the command from the
  884. * saved data_cmnd. We also unmap any
  885. * data associated with the command
  886. * here */
  887. NCR_700_unmap(hostdata, SCp, slot);
  888. SCp->cmnd[0] = REQUEST_SENSE;
  889. SCp->cmnd[1] = (SCp->device->lun & 0x7) << 5;
  890. SCp->cmnd[2] = 0;
  891. SCp->cmnd[3] = 0;
  892. SCp->cmnd[4] = sizeof(SCp->sense_buffer);
  893. SCp->cmnd[5] = 0;
  894. SCp->cmd_len = 6;
  895. /* Here's a quiet hack: the
  896. * REQUEST_SENSE command is six bytes,
  897. * so store a flag indicating that
  898. * this was an internal sense request
  899. * and the original status at the end
  900. * of the command */
  901. SCp->cmnd[6] = NCR_700_INTERNAL_SENSE_MAGIC;
  902. SCp->cmnd[7] = hostdata->status[0];
  903. SCp->use_sg = 0;
  904. SCp->sc_data_direction = DMA_FROM_DEVICE;
  905. dma_sync_single_for_device(hostdata->dev, slot->pCmd,
  906. SCp->cmd_len, DMA_TO_DEVICE);
  907. SCp->request_bufflen = sizeof(SCp->sense_buffer);
  908. slot->dma_handle = dma_map_single(hostdata->dev, SCp->sense_buffer, sizeof(SCp->sense_buffer), DMA_FROM_DEVICE);
  909. slot->SG[0].ins = bS_to_host(SCRIPT_MOVE_DATA_IN | sizeof(SCp->sense_buffer));
  910. slot->SG[0].pAddr = bS_to_host(slot->dma_handle);
  911. slot->SG[1].ins = bS_to_host(SCRIPT_RETURN);
  912. slot->SG[1].pAddr = 0;
  913. slot->resume_offset = hostdata->pScript;
  914. dma_cache_sync(slot->SG, sizeof(slot->SG[0])*2, DMA_TO_DEVICE);
  915. dma_cache_sync(SCp->sense_buffer, sizeof(SCp->sense_buffer), DMA_FROM_DEVICE);
  916. /* queue the command for reissue */
  917. slot->state = NCR_700_SLOT_QUEUED;
  918. hostdata->state = NCR_700_HOST_FREE;
  919. hostdata->cmd = NULL;
  920. }
  921. } else {
  922. // Currently rely on the mid layer evaluation
  923. // of the tag queuing capability
  924. //
  925. //if(status_byte(hostdata->status[0]) == GOOD &&
  926. // SCp->cmnd[0] == INQUIRY && SCp->use_sg == 0) {
  927. // /* Piggy back the tag queueing support
  928. // * on this command */
  929. // dma_sync_single_for_cpu(hostdata->dev,
  930. // slot->dma_handle,
  931. // SCp->request_bufflen,
  932. // DMA_FROM_DEVICE);
  933. // if(((char *)SCp->request_buffer)[7] & 0x02) {
  934. // printk(KERN_INFO "scsi%d: (%d:%d) Enabling Tag Command Queuing\n", host->host_no, pun, lun);
  935. // hostdata->tag_negotiated |= (1<<SCp->device->id);
  936. // NCR_700_set_flag(SCp->device, NCR_700_DEV_BEGIN_TAG_QUEUEING);
  937. // } else {
  938. // NCR_700_clear_flag(SCp->device, NCR_700_DEV_BEGIN_TAG_QUEUEING);
  939. // hostdata->tag_negotiated &= ~(1<<SCp->device->id);
  940. // }
  941. //}
  942. NCR_700_scsi_done(hostdata, SCp, hostdata->status[0]);
  943. }
  944. } else if((dsps & 0xfffff0f0) == A_UNEXPECTED_PHASE) {
  945. __u8 i = (dsps & 0xf00) >> 8;
  946. printk(KERN_ERR "scsi%d: (%d:%d), UNEXPECTED PHASE %s (%s)\n",
  947. host->host_no, pun, lun,
  948. NCR_700_phase[i],
  949. sbcl_to_string(NCR_700_readb(host, SBCL_REG)));
  950. printk(KERN_ERR " len = %d, cmd =", SCp->cmd_len);
  951. scsi_print_command(SCp);
  952. NCR_700_internal_bus_reset(host);
  953. } else if((dsps & 0xfffff000) == A_FATAL) {
  954. int i = (dsps & 0xfff);
  955. printk(KERN_ERR "scsi%d: (%d:%d) FATAL ERROR: %s\n",
  956. host->host_no, pun, lun, NCR_700_fatal_messages[i]);
  957. if(dsps == A_FATAL_ILLEGAL_MSG_LENGTH) {
  958. printk(KERN_ERR " msg begins %02x %02x\n",
  959. hostdata->msgin[0], hostdata->msgin[1]);
  960. }
  961. NCR_700_internal_bus_reset(host);
  962. } else if((dsps & 0xfffff0f0) == A_DISCONNECT) {
  963. #ifdef NCR_700_DEBUG
  964. __u8 i = (dsps & 0xf00) >> 8;
  965. printk("scsi%d: (%d:%d), DISCONNECTED (%d) %s\n",
  966. host->host_no, pun, lun,
  967. i, NCR_700_phase[i]);
  968. #endif
  969. save_for_reselection(hostdata, SCp, dsp);
  970. } else if(dsps == A_RESELECTION_IDENTIFIED) {
  971. __u8 lun;
  972. struct NCR_700_command_slot *slot;
  973. __u8 reselection_id = hostdata->reselection_id;
  974. struct scsi_device *SDp;
  975. lun = hostdata->msgin[0] & 0x1f;
  976. hostdata->reselection_id = 0xff;
  977. DEBUG(("scsi%d: (%d:%d) RESELECTED!\n",
  978. host->host_no, reselection_id, lun));
  979. /* clear the reselection indicator */
  980. SDp = __scsi_device_lookup(host, 0, reselection_id, lun);
  981. if(unlikely(SDp == NULL)) {
  982. printk(KERN_ERR "scsi%d: (%d:%d) HAS NO device\n",
  983. host->host_no, reselection_id, lun);
  984. BUG();
  985. }
  986. if(hostdata->msgin[1] == A_SIMPLE_TAG_MSG) {
  987. struct scsi_cmnd *SCp = scsi_find_tag(SDp, hostdata->msgin[2]);
  988. if(unlikely(SCp == NULL)) {
  989. printk(KERN_ERR "scsi%d: (%d:%d) no saved request for tag %d\n",
  990. host->host_no, reselection_id, lun, hostdata->msgin[2]);
  991. BUG();
  992. }
  993. slot = (struct NCR_700_command_slot *)SCp->host_scribble;
  994. DEBUG(("53c700: %d:%d:%d, reselection is tag %d, slot %p(%d)\n",
  995. host->host_no, SDp->id, SDp->lun,
  996. hostdata->msgin[2], slot, slot->tag));
  997. } else {
  998. struct scsi_cmnd *SCp = scsi_find_tag(SDp, SCSI_NO_TAG);
  999. if(unlikely(SCp == NULL)) {
  1000. printk(KERN_ERR "scsi%d: (%d:%d) no saved request for untagged cmd\n",
  1001. host->host_no, reselection_id, lun);
  1002. BUG();
  1003. }
  1004. slot = (struct NCR_700_command_slot *)SCp->host_scribble;
  1005. }
  1006. if(slot == NULL) {
  1007. printk(KERN_ERR "scsi%d: (%d:%d) RESELECTED but no saved command (MSG = %02x %02x %02x)!!\n",
  1008. host->host_no, reselection_id, lun,
  1009. hostdata->msgin[0], hostdata->msgin[1],
  1010. hostdata->msgin[2]);
  1011. } else {
  1012. if(hostdata->state != NCR_700_HOST_BUSY)
  1013. printk(KERN_ERR "scsi%d: FATAL, host not busy during valid reselection!\n",
  1014. host->host_no);
  1015. resume_offset = slot->resume_offset;
  1016. hostdata->cmd = slot->cmnd;
  1017. /* re-patch for this command */
  1018. script_patch_32_abs(hostdata->script, CommandAddress,
  1019. slot->pCmd);
  1020. script_patch_16(hostdata->script,
  1021. CommandCount, slot->cmnd->cmd_len);
  1022. script_patch_32_abs(hostdata->script, SGScriptStartAddress,
  1023. to32bit(&slot->pSG[0].ins));
  1024. /* Note: setting SXFER only works if we're
  1025. * still in the MESSAGE phase, so it is vital
  1026. * that ACK is still asserted when we process
  1027. * the reselection message. The resume offset
  1028. * should therefore always clear ACK */
  1029. NCR_700_writeb(NCR_700_get_SXFER(hostdata->cmd->device),
  1030. host, SXFER_REG);
  1031. dma_cache_sync(hostdata->msgin,
  1032. MSG_ARRAY_SIZE, DMA_FROM_DEVICE);
  1033. dma_cache_sync(hostdata->msgout,
  1034. MSG_ARRAY_SIZE, DMA_TO_DEVICE);
  1035. /* I'm just being paranoid here, the command should
  1036. * already have been flushed from the cache */
  1037. dma_cache_sync(slot->cmnd->cmnd,
  1038. slot->cmnd->cmd_len, DMA_TO_DEVICE);
  1039. }
  1040. } else if(dsps == A_RESELECTED_DURING_SELECTION) {
  1041. /* This section is full of debugging code because I've
  1042. * never managed to reach it. I think what happens is
  1043. * that, because the 700 runs with selection
  1044. * interrupts enabled the whole time that we take a
  1045. * selection interrupt before we manage to get to the
  1046. * reselected script interrupt */
  1047. __u8 reselection_id = NCR_700_readb(host, SFBR_REG);
  1048. struct NCR_700_command_slot *slot;
  1049. /* Take out our own ID */
  1050. reselection_id &= ~(1<<host->this_id);
  1051. /* I've never seen this happen, so keep this as a printk rather
  1052. * than a debug */
  1053. printk(KERN_INFO "scsi%d: (%d:%d) RESELECTION DURING SELECTION, dsp=%08x[%04x] state=%d, count=%d\n",
  1054. host->host_no, reselection_id, lun, dsp, dsp - hostdata->pScript, hostdata->state, hostdata->command_slot_count);
  1055. {
  1056. /* FIXME: DEBUGGING CODE */
  1057. __u32 SG = (__u32)bS_to_cpu(hostdata->script[A_SGScriptStartAddress_used[0]]);
  1058. int i;
  1059. for(i=0; i< NCR_700_COMMAND_SLOTS_PER_HOST; i++) {
  1060. if(SG >= to32bit(&hostdata->slots[i].pSG[0])
  1061. && SG <= to32bit(&hostdata->slots[i].pSG[NCR_700_SG_SEGMENTS]))
  1062. break;
  1063. }
  1064. printk(KERN_INFO "IDENTIFIED SG segment as being %08x in slot %p, cmd %p, slot->resume_offset=%08x\n", SG, &hostdata->slots[i], hostdata->slots[i].cmnd, hostdata->slots[i].resume_offset);
  1065. SCp = hostdata->slots[i].cmnd;
  1066. }
  1067. if(SCp != NULL) {
  1068. slot = (struct NCR_700_command_slot *)SCp->host_scribble;
  1069. /* change slot from busy to queued to redo command */
  1070. slot->state = NCR_700_SLOT_QUEUED;
  1071. }
  1072. hostdata->cmd = NULL;
  1073. if(reselection_id == 0) {
  1074. if(hostdata->reselection_id == 0xff) {
  1075. printk(KERN_ERR "scsi%d: Invalid reselection during selection!!\n", host->host_no);
  1076. return 0;
  1077. } else {
  1078. printk(KERN_ERR "scsi%d: script reselected and we took a selection interrupt\n",
  1079. host->host_no);
  1080. reselection_id = hostdata->reselection_id;
  1081. }
  1082. } else {
  1083. /* convert to real ID */
  1084. reselection_id = bitmap_to_number(reselection_id);
  1085. }
  1086. hostdata->reselection_id = reselection_id;
  1087. /* just in case we have a stale simple tag message, clear it */
  1088. hostdata->msgin[1] = 0;
  1089. dma_cache_sync(hostdata->msgin,
  1090. MSG_ARRAY_SIZE, DMA_BIDIRECTIONAL);
  1091. if(hostdata->tag_negotiated & (1<<reselection_id)) {
  1092. resume_offset = hostdata->pScript + Ent_GetReselectionWithTag;
  1093. } else {
  1094. resume_offset = hostdata->pScript + Ent_GetReselectionData;
  1095. }
  1096. } else if(dsps == A_COMPLETED_SELECTION_AS_TARGET) {
  1097. /* we've just disconnected from the bus, do nothing since
  1098. * a return here will re-run the queued command slot
  1099. * that may have been interrupted by the initial selection */
  1100. DEBUG((" SELECTION COMPLETED\n"));
  1101. } else if((dsps & 0xfffff0f0) == A_MSG_IN) {
  1102. resume_offset = process_message(host, hostdata, SCp,
  1103. dsp, dsps);
  1104. } else if((dsps & 0xfffff000) == 0) {
  1105. __u8 i = (dsps & 0xf0) >> 4, j = (dsps & 0xf00) >> 8;
  1106. printk(KERN_ERR "scsi%d: (%d:%d), unhandled script condition %s %s at %04x\n",
  1107. host->host_no, pun, lun, NCR_700_condition[i],
  1108. NCR_700_phase[j], dsp - hostdata->pScript);
  1109. if(SCp != NULL) {
  1110. scsi_print_command(SCp);
  1111. if(SCp->use_sg) {
  1112. for(i = 0; i < SCp->use_sg + 1; i++) {
  1113. printk(KERN_INFO " SG[%d].length = %d, move_insn=%08x, addr %08x\n", i, ((struct scatterlist *)SCp->buffer)[i].length, ((struct NCR_700_command_slot *)SCp->host_scribble)->SG[i].ins, ((struct NCR_700_command_slot *)SCp->host_scribble)->SG[i].pAddr);
  1114. }
  1115. }
  1116. }
  1117. NCR_700_internal_bus_reset(host);
  1118. } else if((dsps & 0xfffff000) == A_DEBUG_INTERRUPT) {
  1119. printk(KERN_NOTICE "scsi%d (%d:%d) DEBUG INTERRUPT %d AT %08x[%04x], continuing\n",
  1120. host->host_no, pun, lun, dsps & 0xfff, dsp, dsp - hostdata->pScript);
  1121. resume_offset = dsp;
  1122. } else {
  1123. printk(KERN_ERR "scsi%d: (%d:%d), unidentified script interrupt 0x%x at %04x\n",
  1124. host->host_no, pun, lun, dsps, dsp - hostdata->pScript);
  1125. NCR_700_internal_bus_reset(host);
  1126. }
  1127. return resume_offset;
  1128. }
  1129. /* We run the 53c700 with selection interrupts always enabled. This
  1130. * means that the chip may be selected as soon as the bus frees. On a
  1131. * busy bus, this can be before the scripts engine finishes its
  1132. * processing. Therefore, part of the selection processing has to be
  1133. * to find out what the scripts engine is doing and complete the
  1134. * function if necessary (i.e. process the pending disconnect or save
  1135. * the interrupted initial selection */
  1136. STATIC inline __u32
  1137. process_selection(struct Scsi_Host *host, __u32 dsp)
  1138. {
  1139. __u8 id = 0; /* Squash compiler warning */
  1140. int count = 0;
  1141. __u32 resume_offset = 0;
  1142. struct NCR_700_Host_Parameters *hostdata =
  1143. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  1144. struct scsi_cmnd *SCp = hostdata->cmd;
  1145. __u8 sbcl;
  1146. for(count = 0; count < 5; count++) {
  1147. id = NCR_700_readb(host, hostdata->chip710 ?
  1148. CTEST9_REG : SFBR_REG);
  1149. /* Take out our own ID */
  1150. id &= ~(1<<host->this_id);
  1151. if(id != 0)
  1152. break;
  1153. udelay(5);
  1154. }
  1155. sbcl = NCR_700_readb(host, SBCL_REG);
  1156. if((sbcl & SBCL_IO) == 0) {
  1157. /* mark as having been selected rather than reselected */
  1158. id = 0xff;
  1159. } else {
  1160. /* convert to real ID */
  1161. hostdata->reselection_id = id = bitmap_to_number(id);
  1162. DEBUG(("scsi%d: Reselected by %d\n",
  1163. host->host_no, id));
  1164. }
  1165. if(hostdata->state == NCR_700_HOST_BUSY && SCp != NULL) {
  1166. struct NCR_700_command_slot *slot =
  1167. (struct NCR_700_command_slot *)SCp->host_scribble;
  1168. DEBUG((" ID %d WARNING: RESELECTION OF BUSY HOST, saving cmd %p, slot %p, addr %x [%04x], resume %x!\n", id, hostdata->cmd, slot, dsp, dsp - hostdata->pScript, resume_offset));
  1169. switch(dsp - hostdata->pScript) {
  1170. case Ent_Disconnect1:
  1171. case Ent_Disconnect2:
  1172. save_for_reselection(hostdata, SCp, Ent_Disconnect2 + hostdata->pScript);
  1173. break;
  1174. case Ent_Disconnect3:
  1175. case Ent_Disconnect4:
  1176. save_for_reselection(hostdata, SCp, Ent_Disconnect4 + hostdata->pScript);
  1177. break;
  1178. case Ent_Disconnect5:
  1179. case Ent_Disconnect6:
  1180. save_for_reselection(hostdata, SCp, Ent_Disconnect6 + hostdata->pScript);
  1181. break;
  1182. case Ent_Disconnect7:
  1183. case Ent_Disconnect8:
  1184. save_for_reselection(hostdata, SCp, Ent_Disconnect8 + hostdata->pScript);
  1185. break;
  1186. case Ent_Finish1:
  1187. case Ent_Finish2:
  1188. process_script_interrupt(A_GOOD_STATUS_AFTER_STATUS, dsp, SCp, host, hostdata);
  1189. break;
  1190. default:
  1191. slot->state = NCR_700_SLOT_QUEUED;
  1192. break;
  1193. }
  1194. }
  1195. hostdata->state = NCR_700_HOST_BUSY;
  1196. hostdata->cmd = NULL;
  1197. /* clear any stale simple tag message */
  1198. hostdata->msgin[1] = 0;
  1199. dma_cache_sync(hostdata->msgin, MSG_ARRAY_SIZE,
  1200. DMA_BIDIRECTIONAL);
  1201. if(id == 0xff) {
  1202. /* Selected as target, Ignore */
  1203. resume_offset = hostdata->pScript + Ent_SelectedAsTarget;
  1204. } else if(hostdata->tag_negotiated & (1<<id)) {
  1205. resume_offset = hostdata->pScript + Ent_GetReselectionWithTag;
  1206. } else {
  1207. resume_offset = hostdata->pScript + Ent_GetReselectionData;
  1208. }
  1209. return resume_offset;
  1210. }
  1211. static inline void
  1212. NCR_700_clear_fifo(struct Scsi_Host *host) {
  1213. const struct NCR_700_Host_Parameters *hostdata
  1214. = (struct NCR_700_Host_Parameters *)host->hostdata[0];
  1215. if(hostdata->chip710) {
  1216. NCR_700_writeb(CLR_FIFO_710, host, CTEST8_REG);
  1217. } else {
  1218. NCR_700_writeb(CLR_FIFO, host, DFIFO_REG);
  1219. }
  1220. }
  1221. static inline void
  1222. NCR_700_flush_fifo(struct Scsi_Host *host) {
  1223. const struct NCR_700_Host_Parameters *hostdata
  1224. = (struct NCR_700_Host_Parameters *)host->hostdata[0];
  1225. if(hostdata->chip710) {
  1226. NCR_700_writeb(FLUSH_DMA_FIFO_710, host, CTEST8_REG);
  1227. udelay(10);
  1228. NCR_700_writeb(0, host, CTEST8_REG);
  1229. } else {
  1230. NCR_700_writeb(FLUSH_DMA_FIFO, host, DFIFO_REG);
  1231. udelay(10);
  1232. NCR_700_writeb(0, host, DFIFO_REG);
  1233. }
  1234. }
  1235. /* The queue lock with interrupts disabled must be held on entry to
  1236. * this function */
  1237. STATIC int
  1238. NCR_700_start_command(struct scsi_cmnd *SCp)
  1239. {
  1240. struct NCR_700_command_slot *slot =
  1241. (struct NCR_700_command_slot *)SCp->host_scribble;
  1242. struct NCR_700_Host_Parameters *hostdata =
  1243. (struct NCR_700_Host_Parameters *)SCp->device->host->hostdata[0];
  1244. __u16 count = 1; /* for IDENTIFY message */
  1245. if(hostdata->state != NCR_700_HOST_FREE) {
  1246. /* keep this inside the lock to close the race window where
  1247. * the running command finishes on another CPU while we don't
  1248. * change the state to queued on this one */
  1249. slot->state = NCR_700_SLOT_QUEUED;
  1250. DEBUG(("scsi%d: host busy, queueing command %p, slot %p\n",
  1251. SCp->device->host->host_no, slot->cmnd, slot));
  1252. return 0;
  1253. }
  1254. hostdata->state = NCR_700_HOST_BUSY;
  1255. hostdata->cmd = SCp;
  1256. slot->state = NCR_700_SLOT_BUSY;
  1257. /* keep interrupts disabled until we have the command correctly
  1258. * set up so we cannot take a selection interrupt */
  1259. hostdata->msgout[0] = NCR_700_identify(SCp->cmnd[0] != REQUEST_SENSE,
  1260. SCp->device->lun);
  1261. /* for INQUIRY or REQUEST_SENSE commands, we cannot be sure
  1262. * if the negotiated transfer parameters still hold, so
  1263. * always renegotiate them */
  1264. if(SCp->cmnd[0] == INQUIRY || SCp->cmnd[0] == REQUEST_SENSE) {
  1265. NCR_700_clear_flag(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC);
  1266. }
  1267. /* REQUEST_SENSE is asking for contingent I_T_L(_Q) status.
  1268. * If a contingent allegiance condition exists, the device
  1269. * will refuse all tags, so send the request sense as untagged
  1270. * */
  1271. if((hostdata->tag_negotiated & (1<<SCp->device->id))
  1272. && (slot->tag != SCSI_NO_TAG && SCp->cmnd[0] != REQUEST_SENSE)) {
  1273. count += scsi_populate_tag_msg(SCp, &hostdata->msgout[count]);
  1274. }
  1275. if(hostdata->fast &&
  1276. NCR_700_is_flag_clear(SCp->device, NCR_700_DEV_NEGOTIATED_SYNC)) {
  1277. memcpy(&hostdata->msgout[count], NCR_700_SDTR_msg,
  1278. sizeof(NCR_700_SDTR_msg));
  1279. hostdata->msgout[count+3] = spi_period(SCp->device->sdev_target);
  1280. hostdata->msgout[count+4] = spi_offset(SCp->device->sdev_target);
  1281. count += sizeof(NCR_700_SDTR_msg);
  1282. NCR_700_set_flag(SCp->device, NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
  1283. }
  1284. script_patch_16(hostdata->script, MessageCount, count);
  1285. script_patch_ID(hostdata->script,
  1286. Device_ID, 1<<SCp->device->id);
  1287. script_patch_32_abs(hostdata->script, CommandAddress,
  1288. slot->pCmd);
  1289. script_patch_16(hostdata->script, CommandCount, SCp->cmd_len);
  1290. /* finally plumb the beginning of the SG list into the script
  1291. * */
  1292. script_patch_32_abs(hostdata->script, SGScriptStartAddress,
  1293. to32bit(&slot->pSG[0].ins));
  1294. NCR_700_clear_fifo(SCp->device->host);
  1295. if(slot->resume_offset == 0)
  1296. slot->resume_offset = hostdata->pScript;
  1297. /* now perform all the writebacks and invalidates */
  1298. dma_cache_sync(hostdata->msgout, count, DMA_TO_DEVICE);
  1299. dma_cache_sync(hostdata->msgin, MSG_ARRAY_SIZE,
  1300. DMA_FROM_DEVICE);
  1301. dma_cache_sync(SCp->cmnd, SCp->cmd_len, DMA_TO_DEVICE);
  1302. dma_cache_sync(hostdata->status, 1, DMA_FROM_DEVICE);
  1303. /* set the synchronous period/offset */
  1304. NCR_700_writeb(NCR_700_get_SXFER(SCp->device),
  1305. SCp->device->host, SXFER_REG);
  1306. NCR_700_writel(slot->temp, SCp->device->host, TEMP_REG);
  1307. NCR_700_writel(slot->resume_offset, SCp->device->host, DSP_REG);
  1308. return 1;
  1309. }
  1310. irqreturn_t
  1311. NCR_700_intr(int irq, void *dev_id, struct pt_regs *regs)
  1312. {
  1313. struct Scsi_Host *host = (struct Scsi_Host *)dev_id;
  1314. struct NCR_700_Host_Parameters *hostdata =
  1315. (struct NCR_700_Host_Parameters *)host->hostdata[0];
  1316. __u8 istat;
  1317. __u32 resume_offset = 0;
  1318. __u8 pun = 0xff, lun = 0xff;
  1319. unsigned long flags;
  1320. int handled = 0;
  1321. /* Use the host lock to serialise acess to the 53c700
  1322. * hardware. Note: In future, we may need to take the queue
  1323. * lock to enter the done routines. When that happens, we
  1324. * need to ensure that for this driver, the host lock and the
  1325. * queue lock point to the same thing. */
  1326. spin_lock_irqsave(host->host_lock, flags);
  1327. if((istat = NCR_700_readb(host, ISTAT_REG))
  1328. & (SCSI_INT_PENDING | DMA_INT_PENDING)) {
  1329. __u32 dsps;
  1330. __u8 sstat0 = 0, dstat = 0;
  1331. __u32 dsp;
  1332. struct scsi_cmnd *SCp = hostdata->cmd;
  1333. enum NCR_700_Host_State state;
  1334. handled = 1;
  1335. state = hostdata->state;
  1336. SCp = hostdata->cmd;
  1337. if(istat & SCSI_INT_PENDING) {
  1338. udelay(10);
  1339. sstat0 = NCR_700_readb(host, SSTAT0_REG);
  1340. }
  1341. if(istat & DMA_INT_PENDING) {
  1342. udelay(10);
  1343. dstat = NCR_700_readb(host, DSTAT_REG);
  1344. }
  1345. dsps = NCR_700_readl(host, DSPS_REG);
  1346. dsp = NCR_700_readl(host, DSP_REG);
  1347. DEBUG(("scsi%d: istat %02x sstat0 %02x dstat %02x dsp %04x[%08x] dsps 0x%x\n",
  1348. host->host_no, istat, sstat0, dstat,
  1349. (dsp - (__u32)(hostdata->pScript))/4,
  1350. dsp, dsps));
  1351. if(SCp != NULL) {
  1352. pun = SCp->device->id;
  1353. lun = SCp->device->lun;
  1354. }
  1355. if(sstat0 & SCSI_RESET_DETECTED) {
  1356. struct scsi_device *SDp;
  1357. int i;
  1358. hostdata->state = NCR_700_HOST_BUSY;
  1359. printk(KERN_ERR "scsi%d: Bus Reset detected, executing command %p, slot %p, dsp %08x[%04x]\n",
  1360. host->host_no, SCp, SCp == NULL ? NULL : SCp->host_scribble, dsp, dsp - hostdata->pScript);
  1361. scsi_report_bus_reset(host, 0);
  1362. /* clear all the negotiated parameters */
  1363. __shost_for_each_device(SDp, host)
  1364. SDp->hostdata = NULL;
  1365. /* clear all the slots and their pending commands */
  1366. for(i = 0; i < NCR_700_COMMAND_SLOTS_PER_HOST; i++) {
  1367. struct scsi_cmnd *SCp;
  1368. struct NCR_700_command_slot *slot =
  1369. &hostdata->slots[i];
  1370. if(slot->state == NCR_700_SLOT_FREE)
  1371. continue;
  1372. SCp = slot->cmnd;
  1373. printk(KERN_ERR " failing command because of reset, slot %p, cmnd %p\n",
  1374. slot, SCp);
  1375. free_slot(slot, hostdata);
  1376. SCp->host_scribble = NULL;
  1377. NCR_700_set_depth(SCp->device, 0);
  1378. /* NOTE: deadlock potential here: we
  1379. * rely on mid-layer guarantees that
  1380. * scsi_done won't try to issue the
  1381. * command again otherwise we'll
  1382. * deadlock on the
  1383. * hostdata->state_lock */
  1384. SCp->result = DID_RESET << 16;
  1385. SCp->scsi_done(SCp);
  1386. }
  1387. mdelay(25);
  1388. NCR_700_chip_setup(host);
  1389. hostdata->state = NCR_700_HOST_FREE;
  1390. hostdata->cmd = NULL;
  1391. /* signal back if this was an eh induced reset */
  1392. if(hostdata->eh_complete != NULL)
  1393. complete(hostdata->eh_complete);
  1394. goto out_unlock;
  1395. } else if(sstat0 & SELECTION_TIMEOUT) {
  1396. DEBUG(("scsi%d: (%d:%d) selection timeout\n",
  1397. host->host_no, pun, lun));
  1398. NCR_700_scsi_done(hostdata, SCp, DID_NO_CONNECT<<16);
  1399. } else if(sstat0 & PHASE_MISMATCH) {
  1400. struct NCR_700_command_slot *slot = (SCp == NULL) ? NULL :
  1401. (struct NCR_700_command_slot *)SCp->host_scribble;
  1402. if(dsp == Ent_SendMessage + 8 + hostdata->pScript) {
  1403. /* It wants to reply to some part of
  1404. * our message */
  1405. #ifdef NCR_700_DEBUG
  1406. __u32 temp = NCR_700_readl(host, TEMP_REG);
  1407. int count = (hostdata->script[Ent_SendMessage/4] & 0xffffff) - ((NCR_700_readl(host, DBC_REG) & 0xffffff) + NCR_700_data_residual(host));
  1408. printk("scsi%d (%d:%d) PHASE MISMATCH IN SEND MESSAGE %d remain, return %p[%04x], phase %s\n", host->host_no, pun, lun, count, (void *)temp, temp - hostdata->pScript, sbcl_to_string(NCR_700_readb(host, SBCL_REG)));
  1409. #endif
  1410. resume_offset = hostdata->pScript + Ent_SendMessagePhaseMismatch;
  1411. } else if(dsp >= to32bit(&slot->pSG[0].ins) &&
  1412. dsp <= to32bit(&slot->pSG[NCR_700_SG_SEGMENTS].ins)) {
  1413. int data_transfer = NCR_700_readl(host, DBC_REG) & 0xffffff;
  1414. int SGcount = (dsp - to32bit(&slot->pSG[0].ins))/sizeof(struct NCR_700_SG_List);
  1415. int residual = NCR_700_data_residual(host);
  1416. int i;
  1417. #ifdef NCR_700_DEBUG
  1418. __u32 naddr = NCR_700_readl(host, DNAD_REG);
  1419. printk("scsi%d: (%d:%d) Expected phase mismatch in slot->SG[%d], transferred 0x%x\n",
  1420. host->host_no, pun, lun,
  1421. SGcount, data_transfer);
  1422. scsi_print_command(SCp);
  1423. if(residual) {
  1424. printk("scsi%d: (%d:%d) Expected phase mismatch in slot->SG[%d], transferred 0x%x, residual %d\n",
  1425. host->host_no, pun, lun,
  1426. SGcount, data_transfer, residual);
  1427. }
  1428. #endif
  1429. data_transfer += residual;
  1430. if(data_transfer != 0) {
  1431. int count;
  1432. __u32 pAddr;
  1433. SGcount--;
  1434. count = (bS_to_cpu(slot->SG[SGcount].ins) & 0x00ffffff);
  1435. DEBUG(("DATA TRANSFER MISMATCH, count = %d, transferred %d\n", count, count-data_transfer));
  1436. slot->SG[SGcount].ins &= bS_to_host(0xff000000);
  1437. slot->SG[SGcount].ins |= bS_to_host(data_transfer);
  1438. pAddr = bS_to_cpu(slot->SG[SGcount].pAddr);
  1439. pAddr += (count - data_transfer);
  1440. #ifdef NCR_700_DEBUG
  1441. if(pAddr != naddr) {
  1442. printk("scsi%d (%d:%d) transfer mismatch pAddr=%lx, naddr=%lx, data_transfer=%d, residual=%d\n", host->host_no, pun, lun, (unsigned long)pAddr, (unsigned long)naddr, data_transfer, residual);
  1443. }
  1444. #endif
  1445. slot->SG[SGcount].pAddr = bS_to_host(pAddr);
  1446. }
  1447. /* set the executed moves to nops */
  1448. for(i=0; i<SGcount; i++) {
  1449. slot->SG[i].ins = bS_to_host(SCRIPT_NOP);
  1450. slot->SG[i].pAddr = 0;
  1451. }
  1452. dma_cache_sync(slot->SG, sizeof(slot->SG), DMA_TO_DEVICE);
  1453. /* and pretend we disconnected after
  1454. * the command phase */
  1455. resume_offset = hostdata->pScript + Ent_MsgInDuringData;
  1456. /* make sure all the data is flushed */
  1457. NCR_700_flush_fifo(host);
  1458. } else {
  1459. __u8 sbcl = NCR_700_readb(host, SBCL_REG);
  1460. printk(KERN_ERR "scsi%d: (%d:%d) phase mismatch at %04x, phase %s\n",
  1461. host->host_no, pun, lun, dsp - hostdata->pScript, sbcl_to_string(sbcl));
  1462. NCR_700_internal_bus_reset(host);
  1463. }
  1464. } else if(sstat0 & SCSI_GROSS_ERROR) {
  1465. printk(KERN_ERR "scsi%d: (%d:%d) GROSS ERROR\n",
  1466. host->host_no, pun, lun);
  1467. NCR_700_scsi_done(hostdata, SCp, DID_ERROR<<16);
  1468. } else if(sstat0 & PARITY_ERROR) {
  1469. printk(KERN_ERR "scsi%d: (%d:%d) PARITY ERROR\n",
  1470. host->host_no, pun, lun);
  1471. NCR_700_scsi_done(hostdata, SCp, DID_ERROR<<16);
  1472. } else if(dstat & SCRIPT_INT_RECEIVED) {
  1473. DEBUG(("scsi%d: (%d:%d) ====>SCRIPT INTERRUPT<====\n",
  1474. host->host_no, pun, lun));
  1475. resume_offset = process_script_interrupt(dsps, dsp, SCp, host, hostdata);
  1476. } else if(dstat & (ILGL_INST_DETECTED)) {
  1477. printk(KERN_ERR "scsi%d: (%d:%d) Illegal Instruction detected at 0x%08x[0x%x]!!!\n"
  1478. " Please email James.Bottomley@HansenPartnership.com with the details\n",
  1479. host->host_no, pun, lun,
  1480. dsp, dsp - hostdata->pScript);
  1481. NCR_700_scsi_done(hostdata, SCp, DID_ERROR<<16);
  1482. } else if(dstat & (WATCH_DOG_INTERRUPT|ABORTED)) {
  1483. printk(KERN_ERR "scsi%d: (%d:%d) serious DMA problem, dstat=%02x\n",
  1484. host->host_no, pun, lun, dstat);
  1485. NCR_700_scsi_done(hostdata, SCp, DID_ERROR<<16);
  1486. }
  1487. /* NOTE: selection interrupt processing MUST occur
  1488. * after script interrupt processing to correctly cope
  1489. * with the case where we process a disconnect and
  1490. * then get reselected before we process the
  1491. * disconnection */
  1492. if(sstat0 & SELECTED) {
  1493. /* FIXME: It currently takes at least FOUR
  1494. * interrupts to complete a command that
  1495. * disconnects: one for the disconnect, one
  1496. * for the reselection, one to get the
  1497. * reselection data and one to complete the
  1498. * command. If we guess the reselected
  1499. * command here and prepare it, we only need
  1500. * to get a reselection data interrupt if we
  1501. * guessed wrongly. Since the interrupt
  1502. * overhead is much greater than the command
  1503. * setup, this would be an efficient
  1504. * optimisation particularly as we probably
  1505. * only have one outstanding command on a
  1506. * target most of the time */
  1507. resume_offset = process_selection(host, dsp);
  1508. }
  1509. }
  1510. if(resume_offset) {
  1511. if(hostdata->state != NCR_700_HOST_BUSY) {
  1512. printk(KERN_ERR "scsi%d: Driver error: resume at 0x%08x [0x%04x] with non busy host!\n",
  1513. host->host_no, resume_offset, resume_offset - hostdata->pScript);
  1514. hostdata->state = NCR_700_HOST_BUSY;
  1515. }
  1516. DEBUG(("Attempting to resume at %x\n", resume_offset));
  1517. NCR_700_clear_fifo(host);
  1518. NCR_700_writel(resume_offset, host, DSP_REG);
  1519. }
  1520. /* There is probably a technical no-no about this: If we're a
  1521. * shared interrupt and we got this interrupt because the
  1522. * other device needs servicing not us, we're still going to
  1523. * check our queued commands here---of course, there shouldn't
  1524. * be any outstanding.... */
  1525. if(hostdata->state == NCR_700_HOST_FREE) {
  1526. int i;
  1527. for(i = 0; i < NCR_700_COMMAND_SLOTS_PER_HOST; i++) {
  1528. /* fairness: always run the queue from the last
  1529. * position we left off */
  1530. int j = (i + hostdata->saved_slot_position)
  1531. % NCR_700_COMMAND_SLOTS_PER_HOST;
  1532. if(hostdata->slots[j].state != NCR_700_SLOT_QUEUED)
  1533. continue;
  1534. if(NCR_700_start_command(hostdata->slots[j].cmnd)) {
  1535. DEBUG(("scsi%d: Issuing saved command slot %p, cmd %p\t\n",
  1536. host->host_no, &hostdata->slots[j],
  1537. hostdata->slots[j].cmnd));
  1538. hostdata->saved_slot_position = j + 1;
  1539. }
  1540. break;
  1541. }
  1542. }
  1543. out_unlock:
  1544. spin_unlock_irqrestore(host->host_lock, flags);
  1545. return IRQ_RETVAL(handled);
  1546. }
  1547. STATIC int
  1548. NCR_700_queuecommand(struct scsi_cmnd *SCp, void (*done)(struct scsi_cmnd *))
  1549. {
  1550. struct NCR_700_Host_Parameters *hostdata =
  1551. (struct NCR_700_Host_Parameters *)SCp->device->host->hostdata[0];
  1552. __u32 move_ins;
  1553. enum dma_data_direction direction;
  1554. struct NCR_700_command_slot *slot;
  1555. if(hostdata->command_slot_count >= NCR_700_COMMAND_SLOTS_PER_HOST) {
  1556. /* We're over our allocation, this should never happen
  1557. * since we report the max allocation to the mid layer */
  1558. printk(KERN_WARNING "scsi%d: Command depth has gone over queue depth\n", SCp->device->host->host_no);
  1559. return 1;
  1560. }
  1561. /* check for untagged commands. We cannot have any outstanding
  1562. * commands if we accept them. Commands could be untagged because:
  1563. *
  1564. * - The tag negotiated bitmap is clear
  1565. * - The blk layer sent and untagged command
  1566. */
  1567. if(NCR_700_get_depth(SCp->device) != 0
  1568. && (!(hostdata->tag_negotiated & (1<<SCp->device->id))
  1569. || !blk_rq_tagged(SCp->request))) {
  1570. DEBUG((KERN_ERR "scsi%d (%d:%d) has non zero depth %d\n",
  1571. SCp->device->host->host_no, SCp->device->id, SCp->device->lun,
  1572. NCR_700_get_depth(SCp->device)));
  1573. return SCSI_MLQUEUE_DEVICE_BUSY;
  1574. }
  1575. if(NCR_700_get_depth(SCp->device) >= SCp->device->queue_depth) {
  1576. DEBUG((KERN_ERR "scsi%d (%d:%d) has max tag depth %d\n",
  1577. SCp->device->host->host_no, SCp->device->id, SCp->device->lun,
  1578. NCR_700_get_depth(SCp->device)));
  1579. return SCSI_MLQUEUE_DEVICE_BUSY;
  1580. }
  1581. NCR_700_set_depth(SCp->device, NCR_700_get_depth(SCp->device) + 1);
  1582. /* begin the command here */
  1583. /* no need to check for NULL, test for command_slot_count above
  1584. * ensures a slot is free */
  1585. slot = find_empty_slot(hostdata);
  1586. slot->cmnd = SCp;
  1587. SCp->scsi_done = done;
  1588. SCp->host_scribble = (unsigned char *)slot;
  1589. SCp->SCp.ptr = NULL;
  1590. SCp->SCp.buffer = NULL;
  1591. #ifdef NCR_700_DEBUG
  1592. printk("53c700: scsi%d, command ", SCp->device->host->host_no);
  1593. scsi_print_command(SCp);
  1594. #endif
  1595. if(blk_rq_tagged(SCp->request)
  1596. && (hostdata->tag_negotiated &(1<<SCp->device->id)) == 0
  1597. && NCR_700_get_tag_neg_state(SCp->device) == NCR_700_START_TAG_NEGOTIATION) {
  1598. printk(KERN_ERR "scsi%d: (%d:%d) Enabling Tag Command Queuing\n", SCp->device->host->host_no, SCp->device->id, SCp->device->lun);
  1599. hostdata->tag_negotiated |= (1<<SCp->device->id);
  1600. NCR_700_set_tag_neg_state(SCp->device, NCR_700_DURING_TAG_NEGOTIATION);
  1601. }
  1602. /* here we may have to process an untagged command. The gate
  1603. * above ensures that this will be the only one outstanding,
  1604. * so clear the tag negotiated bit.
  1605. *
  1606. * FIXME: This will royally screw up on multiple LUN devices
  1607. * */
  1608. if(!blk_rq_tagged(SCp->request)
  1609. && (hostdata->tag_negotiated &(1<<SCp->device->id))) {
  1610. printk(KERN_INFO "scsi%d: (%d:%d) Disabling Tag Command Queuing\n", SCp->device->host->host_no, SCp->device->id, SCp->device->lun);
  1611. hostdata->tag_negotiated &= ~(1<<SCp->device->id);
  1612. }
  1613. if((hostdata->tag_negotiated &(1<<SCp->device->id))
  1614. && scsi_get_tag_type(SCp->device)) {
  1615. slot->tag = SCp->request->tag;
  1616. DEBUG(("53c700 %d:%d:%d, sending out tag %d, slot %p\n",
  1617. SCp->device->host->host_no, SCp->device->id, SCp->device->lun, slot->tag,
  1618. slot));
  1619. } else {
  1620. slot->tag = SCSI_NO_TAG;
  1621. /* must populate current_cmnd for scsi_find_tag to work */
  1622. SCp->device->current_cmnd = SCp;
  1623. }
  1624. /* sanity check: some of the commands generated by the mid-layer
  1625. * have an eccentric idea of their sc_data_direction */
  1626. if(!SCp->use_sg && !SCp->request_bufflen
  1627. && SCp->sc_data_direction != DMA_NONE) {
  1628. #ifdef NCR_700_DEBUG
  1629. printk("53c700: Command");
  1630. scsi_print_command(SCp);
  1631. printk("Has wrong data direction %d\n", SCp->sc_data_direction);
  1632. #endif
  1633. SCp->sc_data_direction = DMA_NONE;
  1634. }
  1635. switch (SCp->cmnd[0]) {
  1636. case REQUEST_SENSE:
  1637. /* clear the internal sense magic */
  1638. SCp->cmnd[6] = 0;
  1639. /* fall through */
  1640. default:
  1641. /* OK, get it from the command */
  1642. switch(SCp->sc_data_direction) {
  1643. case DMA_BIDIRECTIONAL:
  1644. default:
  1645. printk(KERN_ERR "53c700: Unknown command for data direction ");
  1646. scsi_print_command(SCp);
  1647. move_ins = 0;
  1648. break;
  1649. case DMA_NONE:
  1650. move_ins = 0;
  1651. break;
  1652. case DMA_FROM_DEVICE:
  1653. move_ins = SCRIPT_MOVE_DATA_IN;
  1654. break;
  1655. case DMA_TO_DEVICE:
  1656. move_ins = SCRIPT_MOVE_DATA_OUT;
  1657. break;
  1658. }
  1659. }
  1660. /* now build the scatter gather list */
  1661. direction = SCp->sc_data_direction;
  1662. if(move_ins != 0) {
  1663. int i;
  1664. int sg_count;
  1665. dma_addr_t vPtr = 0;
  1666. __u32 count = 0;
  1667. if(SCp->use_sg) {
  1668. sg_count = dma_map_sg(hostdata->dev, SCp->buffer,
  1669. SCp->use_sg, direction);
  1670. } else {
  1671. vPtr = dma_map_single(hostdata->dev,
  1672. SCp->request_buffer,
  1673. SCp->request_bufflen,
  1674. direction);
  1675. count = SCp->request_bufflen;
  1676. slot->dma_handle = vPtr;
  1677. sg_count = 1;
  1678. }
  1679. for(i = 0; i < sg_count; i++) {
  1680. if(SCp->use_sg) {
  1681. struct scatterlist *sg = SCp->buffer;
  1682. vPtr = sg_dma_address(&sg[i]);
  1683. count = sg_dma_len(&sg[i]);
  1684. }
  1685. slot->SG[i].ins = bS_to_host(move_ins | count);
  1686. DEBUG((" scatter block %d: move %d[%08x] from 0x%lx\n",
  1687. i, count, slot->SG[i].ins, (unsigned long)vPtr));
  1688. slot->SG[i].pAddr = bS_to_host(vPtr);
  1689. }
  1690. slot->SG[i].ins = bS_to_host(SCRIPT_RETURN);
  1691. slot->SG[i].pAddr = 0;
  1692. dma_cache_sync(slot->SG, sizeof(slot->SG), DMA_TO_DEVICE);
  1693. DEBUG((" SETTING %08lx to %x\n",
  1694. (&slot->pSG[i].ins),
  1695. slot->SG[i].ins));
  1696. }
  1697. slot->resume_offset = 0;
  1698. slot->pCmd = dma_map_single(hostdata->dev, SCp->cmnd,
  1699. sizeof(SCp->cmnd), DMA_TO_DEVICE);
  1700. NCR_700_start_command(SCp);
  1701. return 0;
  1702. }
  1703. STATIC int
  1704. NCR_700_abort(struct scsi_cmnd * SCp)
  1705. {
  1706. struct NCR_700_command_slot *slot;
  1707. printk(KERN_INFO "scsi%d (%d:%d) New error handler wants to abort command\n\t",
  1708. SCp->device->host->host_no, SCp->device->id, SCp->device->lun);
  1709. scsi_print_command(SCp);
  1710. slot = (struct NCR_700_command_slot *)SCp->host_scribble;
  1711. if(slot == NULL)
  1712. /* no outstanding command to abort */
  1713. return SUCCESS;
  1714. if(SCp->cmnd[0] == TEST_UNIT_READY) {
  1715. /* FIXME: This is because of a problem in the new
  1716. * error handler. When it is in error recovery, it
  1717. * will send a TUR to a device it thinks may still be
  1718. * showing a problem. If the TUR isn't responded to,
  1719. * it will abort it and mark the device off line.
  1720. * Unfortunately, it does no other error recovery, so
  1721. * this would leave us with an outstanding command
  1722. * occupying a slot. Rather than allow this to
  1723. * happen, we issue a bus reset to force all
  1724. * outstanding commands to terminate here. */
  1725. NCR_700_internal_bus_reset(SCp->device->host);
  1726. /* still drop through and return failed */
  1727. }
  1728. return FAILED;
  1729. }
  1730. STATIC int
  1731. NCR_700_bus_reset(struct scsi_cmnd * SCp)
  1732. {
  1733. DECLARE_COMPLETION(complete);
  1734. struct NCR_700_Host_Parameters *hostdata =
  1735. (struct NCR_700_Host_Parameters *)SCp->device->host->hostdata[0];
  1736. printk(KERN_INFO "scsi%d (%d:%d) New error handler wants BUS reset, cmd %p\n\t",
  1737. SCp->device->host->host_no, SCp->device->id, SCp->device->lun, SCp);
  1738. scsi_print_command(SCp);
  1739. /* In theory, eh_complete should always be null because the
  1740. * eh is single threaded, but just in case we're handling a
  1741. * reset via sg or something */
  1742. spin_lock_irq(SCp->device->host->host_lock);
  1743. while (hostdata->eh_complete != NULL) {
  1744. spin_unlock_irq(SCp->device->host->host_lock);
  1745. msleep_interruptible(100);
  1746. spin_lock_irq(SCp->device->host->host_lock);
  1747. }
  1748. hostdata->eh_complete = &complete;
  1749. NCR_700_internal_bus_reset(SCp->device->host);
  1750. spin_unlock_irq(SCp->device->host->host_lock);
  1751. wait_for_completion(&complete);
  1752. spin_lock_irq(SCp->device->host->host_lock);
  1753. hostdata->eh_complete = NULL;
  1754. /* Revalidate the transport parameters of the failing device */
  1755. if(hostdata->fast)
  1756. spi_schedule_dv_device(SCp->device);
  1757. spin_unlock_irq(SCp->device->host->host_lock);
  1758. return SUCCESS;
  1759. }
  1760. STATIC int
  1761. NCR_700_host_reset(struct scsi_cmnd * SCp)
  1762. {
  1763. printk(KERN_INFO "scsi%d (%d:%d) New error handler wants HOST reset\n\t",
  1764. SCp->device->host->host_no, SCp->device->id, SCp->device->lun);
  1765. scsi_print_command(SCp);
  1766. spin_lock_irq(SCp->device->host->host_lock);
  1767. NCR_700_internal_bus_reset(SCp->device->host);
  1768. NCR_700_chip_reset(SCp->device->host);
  1769. spin_unlock_irq(SCp->device->host->host_lock);
  1770. return SUCCESS;
  1771. }
  1772. STATIC void
  1773. NCR_700_set_period(struct scsi_target *STp, int period)
  1774. {
  1775. struct Scsi_Host *SHp = dev_to_shost(STp->dev.parent);
  1776. struct NCR_700_Host_Parameters *hostdata =
  1777. (struct NCR_700_Host_Parameters *)SHp->hostdata[0];
  1778. if(!hostdata->fast)
  1779. return;
  1780. if(period < hostdata->min_period)
  1781. period = hostdata->min_period;
  1782. spi_period(STp) = period;
  1783. spi_flags(STp) &= ~(NCR_700_DEV_NEGOTIATED_SYNC |
  1784. NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
  1785. spi_flags(STp) |= NCR_700_DEV_PRINT_SYNC_NEGOTIATION;
  1786. }
  1787. STATIC void
  1788. NCR_700_set_offset(struct scsi_target *STp, int offset)
  1789. {
  1790. struct Scsi_Host *SHp = dev_to_shost(STp->dev.parent);
  1791. struct NCR_700_Host_Parameters *hostdata =
  1792. (struct NCR_700_Host_Parameters *)SHp->hostdata[0];
  1793. int max_offset = hostdata->chip710
  1794. ? NCR_710_MAX_OFFSET : NCR_700_MAX_OFFSET;
  1795. if(!hostdata->fast)
  1796. return;
  1797. if(offset > max_offset)
  1798. offset = max_offset;
  1799. /* if we're currently async, make sure the period is reasonable */
  1800. if(spi_offset(STp) == 0 && (spi_period(STp) < hostdata->min_period ||
  1801. spi_period(STp) > 0xff))
  1802. spi_period(STp) = hostdata->min_period;
  1803. spi_offset(STp) = offset;
  1804. spi_flags(STp) &= ~(NCR_700_DEV_NEGOTIATED_SYNC |
  1805. NCR_700_DEV_BEGIN_SYNC_NEGOTIATION);
  1806. spi_flags(STp) |= NCR_700_DEV_PRINT_SYNC_NEGOTIATION;
  1807. }
  1808. STATIC int
  1809. NCR_700_slave_configure(struct scsi_device *SDp)
  1810. {
  1811. struct NCR_700_Host_Parameters *hostdata =
  1812. (struct NCR_700_Host_Parameters *)SDp->host->hostdata[0];
  1813. /* to do here: allocate memory; build a queue_full list */
  1814. if(SDp->tagged_supported) {
  1815. scsi_set_tag_type(SDp, MSG_ORDERED_TAG);
  1816. scsi_activate_tcq(SDp, NCR_700_DEFAULT_TAGS);
  1817. NCR_700_set_tag_neg_state(SDp, NCR_700_START_TAG_NEGOTIATION);
  1818. } else {
  1819. /* initialise to default depth */
  1820. scsi_adjust_queue_depth(SDp, 0, SDp->host->cmd_per_lun);
  1821. }
  1822. if(hostdata->fast) {
  1823. /* Find the correct offset and period via domain validation */
  1824. if (!spi_initial_dv(SDp->sdev_target))
  1825. spi_dv_device(SDp);
  1826. } else {
  1827. spi_offset(SDp->sdev_target) = 0;
  1828. spi_period(SDp->sdev_target) = 0;
  1829. }
  1830. return 0;
  1831. }
  1832. STATIC void
  1833. NCR_700_slave_destroy(struct scsi_device *SDp)
  1834. {
  1835. /* to do here: deallocate memory */
  1836. }
  1837. static int
  1838. NCR_700_change_queue_depth(struct scsi_device *SDp, int depth)
  1839. {
  1840. if (depth > NCR_700_MAX_TAGS)
  1841. depth = NCR_700_MAX_TAGS;
  1842. scsi_adjust_queue_depth(SDp, scsi_get_tag_type(SDp), depth);
  1843. return depth;
  1844. }
  1845. static int NCR_700_change_queue_type(struct scsi_device *SDp, int tag_type)
  1846. {
  1847. int change_tag = ((tag_type ==0 && scsi_get_tag_type(SDp) != 0)
  1848. || (tag_type != 0 && scsi_get_tag_type(SDp) == 0));
  1849. struct NCR_700_Host_Parameters *hostdata =
  1850. (struct NCR_700_Host_Parameters *)SDp->host->hostdata[0];
  1851. scsi_set_tag_type(SDp, tag_type);
  1852. /* We have a global (per target) flag to track whether TCQ is
  1853. * enabled, so we'll be turning it off for the entire target here.
  1854. * our tag algorithm will fail if we mix tagged and untagged commands,
  1855. * so quiesce the device before doing this */
  1856. if (change_tag)
  1857. scsi_target_quiesce(SDp->sdev_target);
  1858. if (!tag_type) {
  1859. /* shift back to the default unqueued number of commands
  1860. * (the user can still raise this) */
  1861. scsi_deactivate_tcq(SDp, SDp->host->cmd_per_lun);
  1862. hostdata->tag_negotiated &= ~(1 << SDp->id);
  1863. } else {
  1864. /* Here, we cleared the negotiation flag above, so this
  1865. * will force the driver to renegotiate */
  1866. scsi_activate_tcq(SDp, SDp->queue_depth);
  1867. if (change_tag)
  1868. NCR_700_set_tag_neg_state(SDp, NCR_700_START_TAG_NEGOTIATION);
  1869. }
  1870. if (change_tag)
  1871. scsi_target_resume(SDp->sdev_target);
  1872. return tag_type;
  1873. }
  1874. static ssize_t
  1875. NCR_700_show_active_tags(struct device *dev, struct device_attribute *attr, char *buf)
  1876. {
  1877. struct scsi_device *SDp = to_scsi_device(dev);
  1878. return snprintf(buf, 20, "%d\n", NCR_700_get_depth(SDp));
  1879. }
  1880. static struct device_attribute NCR_700_active_tags_attr = {
  1881. .attr = {
  1882. .name = "active_tags",
  1883. .mode = S_IRUGO,
  1884. },
  1885. .show = NCR_700_show_active_tags,
  1886. };
  1887. STATIC struct device_attribute *NCR_700_dev_attrs[] = {
  1888. &NCR_700_active_tags_attr,
  1889. NULL,
  1890. };
  1891. EXPORT_SYMBOL(NCR_700_detect);
  1892. EXPORT_SYMBOL(NCR_700_release);
  1893. EXPORT_SYMBOL(NCR_700_intr);
  1894. static struct spi_function_template NCR_700_transport_functions = {
  1895. .set_period = NCR_700_set_period,
  1896. .show_period = 1,
  1897. .set_offset = NCR_700_set_offset,
  1898. .show_offset = 1,
  1899. };
  1900. static int __init NCR_700_init(void)
  1901. {
  1902. NCR_700_transport_template = spi_attach_transport(&NCR_700_transport_functions);
  1903. if(!NCR_700_transport_template)
  1904. return -ENODEV;
  1905. return 0;
  1906. }
  1907. static void __exit NCR_700_exit(void)
  1908. {
  1909. spi_release_transport(NCR_700_transport_template);
  1910. }
  1911. module_init(NCR_700_init);
  1912. module_exit(NCR_700_exit);