53c700.c 69 KB

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