53c700.c 69 KB

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