53c700.c 70 KB

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