53c700.c 69 KB

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