53c700.c 67 KB

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