fas216.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046
  1. /*
  2. * linux/drivers/acorn/scsi/fas216.c
  3. *
  4. * Copyright (C) 1997-2003 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Based on information in qlogicfas.c by Tom Zerucha, Michael Griffith, and
  11. * other sources, including:
  12. * the AMD Am53CF94 data sheet
  13. * the AMD Am53C94 data sheet
  14. *
  15. * This is a generic driver. To use it, have a look at cumana_2.c. You
  16. * should define your own structure that overlays FAS216_Info, eg:
  17. * struct my_host_data {
  18. * FAS216_Info info;
  19. * ... my host specific data ...
  20. * };
  21. *
  22. * Changelog:
  23. * 30-08-1997 RMK Created
  24. * 14-09-1997 RMK Started disconnect support
  25. * 08-02-1998 RMK Corrected real DMA support
  26. * 15-02-1998 RMK Started sync xfer support
  27. * 06-04-1998 RMK Tightened conditions for printing incomplete
  28. * transfers
  29. * 02-05-1998 RMK Added extra checks in fas216_reset
  30. * 24-05-1998 RMK Fixed synchronous transfers with period >= 200ns
  31. * 27-06-1998 RMK Changed asm/delay.h to linux/delay.h
  32. * 26-08-1998 RMK Improved message support wrt MESSAGE_REJECT
  33. * 02-04-2000 RMK Converted to use the new error handling, and
  34. * automatically request sense data upon check
  35. * condition status from targets.
  36. */
  37. #include <linux/module.h>
  38. #include <linux/blkdev.h>
  39. #include <linux/kernel.h>
  40. #include <linux/string.h>
  41. #include <linux/ioport.h>
  42. #include <linux/proc_fs.h>
  43. #include <linux/delay.h>
  44. #include <linux/bitops.h>
  45. #include <linux/init.h>
  46. #include <linux/interrupt.h>
  47. #include <asm/dma.h>
  48. #include <asm/io.h>
  49. #include <asm/irq.h>
  50. #include <asm/ecard.h>
  51. #include "../scsi.h"
  52. #include <scsi/scsi_dbg.h>
  53. #include <scsi/scsi_host.h>
  54. #include "fas216.h"
  55. #include "scsi.h"
  56. /* NOTE: SCSI2 Synchronous transfers *require* DMA according to
  57. * the data sheet. This restriction is crazy, especially when
  58. * you only want to send 16 bytes! What were the guys who
  59. * designed this chip on at that time? Did they read the SCSI2
  60. * spec at all? The following sections are taken from the SCSI2
  61. * standard (s2r10) concerning this:
  62. *
  63. * > IMPLEMENTORS NOTES:
  64. * > (1) Re-negotiation at every selection is not recommended, since a
  65. * > significant performance impact is likely.
  66. *
  67. * > The implied synchronous agreement shall remain in effect until a BUS DEVICE
  68. * > RESET message is received, until a hard reset condition occurs, or until one
  69. * > of the two SCSI devices elects to modify the agreement. The default data
  70. * > transfer mode is asynchronous data transfer mode. The default data transfer
  71. * > mode is entered at power on, after a BUS DEVICE RESET message, or after a hard
  72. * > reset condition.
  73. *
  74. * In total, this means that once you have elected to use synchronous
  75. * transfers, you must always use DMA.
  76. *
  77. * I was thinking that this was a good chip until I found this restriction ;(
  78. */
  79. #define SCSI2_SYNC
  80. #undef SCSI2_TAG
  81. #undef DEBUG_CONNECT
  82. #undef DEBUG_MESSAGES
  83. #undef CHECK_STRUCTURE
  84. #define LOG_CONNECT (1 << 0)
  85. #define LOG_BUSSERVICE (1 << 1)
  86. #define LOG_FUNCTIONDONE (1 << 2)
  87. #define LOG_MESSAGES (1 << 3)
  88. #define LOG_BUFFER (1 << 4)
  89. #define LOG_ERROR (1 << 8)
  90. static int level_mask = LOG_ERROR;
  91. module_param(level_mask, int, 0644);
  92. static int __init fas216_log_setup(char *str)
  93. {
  94. char *s;
  95. level_mask = 0;
  96. while ((s = strsep(&str, ",")) != NULL) {
  97. switch (s[0]) {
  98. case 'a':
  99. if (strcmp(s, "all") == 0)
  100. level_mask |= -1;
  101. break;
  102. case 'b':
  103. if (strncmp(s, "bus", 3) == 0)
  104. level_mask |= LOG_BUSSERVICE;
  105. if (strncmp(s, "buf", 3) == 0)
  106. level_mask |= LOG_BUFFER;
  107. break;
  108. case 'c':
  109. level_mask |= LOG_CONNECT;
  110. break;
  111. case 'e':
  112. level_mask |= LOG_ERROR;
  113. break;
  114. case 'm':
  115. level_mask |= LOG_MESSAGES;
  116. break;
  117. case 'n':
  118. if (strcmp(s, "none") == 0)
  119. level_mask = 0;
  120. break;
  121. case 's':
  122. level_mask |= LOG_FUNCTIONDONE;
  123. break;
  124. }
  125. }
  126. return 1;
  127. }
  128. __setup("fas216_logging=", fas216_log_setup);
  129. static inline unsigned char fas216_readb(FAS216_Info *info, unsigned int reg)
  130. {
  131. unsigned int off = reg << info->scsi.io_shift;
  132. return readb(info->scsi.io_base + off);
  133. }
  134. static inline void fas216_writeb(FAS216_Info *info, unsigned int reg, unsigned int val)
  135. {
  136. unsigned int off = reg << info->scsi.io_shift;
  137. writeb(val, info->scsi.io_base + off);
  138. }
  139. static void fas216_dumpstate(FAS216_Info *info)
  140. {
  141. unsigned char is, stat, inst;
  142. is = fas216_readb(info, REG_IS);
  143. stat = fas216_readb(info, REG_STAT);
  144. inst = fas216_readb(info, REG_INST);
  145. printk("FAS216: CTCL=%02X CTCM=%02X CMD=%02X STAT=%02X"
  146. " INST=%02X IS=%02X CFIS=%02X",
  147. fas216_readb(info, REG_CTCL),
  148. fas216_readb(info, REG_CTCM),
  149. fas216_readb(info, REG_CMD), stat, inst, is,
  150. fas216_readb(info, REG_CFIS));
  151. printk(" CNTL1=%02X CNTL2=%02X CNTL3=%02X CTCH=%02X\n",
  152. fas216_readb(info, REG_CNTL1),
  153. fas216_readb(info, REG_CNTL2),
  154. fas216_readb(info, REG_CNTL3),
  155. fas216_readb(info, REG_CTCH));
  156. }
  157. static void print_SCp(struct scsi_pointer *SCp, const char *prefix, const char *suffix)
  158. {
  159. printk("%sptr %p this_residual 0x%x buffer %p buffers_residual 0x%x%s",
  160. prefix, SCp->ptr, SCp->this_residual, SCp->buffer,
  161. SCp->buffers_residual, suffix);
  162. }
  163. static void fas216_dumpinfo(FAS216_Info *info)
  164. {
  165. static int used = 0;
  166. int i;
  167. if (used++)
  168. return;
  169. printk("FAS216_Info=\n");
  170. printk(" { magic_start=%lX host=%p SCpnt=%p origSCpnt=%p\n",
  171. info->magic_start, info->host, info->SCpnt,
  172. info->origSCpnt);
  173. printk(" scsi={ io_shift=%X irq=%X cfg={ %X %X %X %X }\n",
  174. info->scsi.io_shift, info->scsi.irq,
  175. info->scsi.cfg[0], info->scsi.cfg[1], info->scsi.cfg[2],
  176. info->scsi.cfg[3]);
  177. printk(" type=%p phase=%X\n",
  178. info->scsi.type, info->scsi.phase);
  179. print_SCp(&info->scsi.SCp, " SCp={ ", " }\n");
  180. printk(" msgs async_stp=%X disconnectable=%d aborting=%d }\n",
  181. info->scsi.async_stp,
  182. info->scsi.disconnectable, info->scsi.aborting);
  183. printk(" stats={ queues=%X removes=%X fins=%X reads=%X writes=%X miscs=%X\n"
  184. " disconnects=%X aborts=%X bus_resets=%X host_resets=%X}\n",
  185. info->stats.queues, info->stats.removes, info->stats.fins,
  186. info->stats.reads, info->stats.writes, info->stats.miscs,
  187. info->stats.disconnects, info->stats.aborts, info->stats.bus_resets,
  188. info->stats.host_resets);
  189. printk(" ifcfg={ clockrate=%X select_timeout=%X asyncperiod=%X sync_max_depth=%X }\n",
  190. info->ifcfg.clockrate, info->ifcfg.select_timeout,
  191. info->ifcfg.asyncperiod, info->ifcfg.sync_max_depth);
  192. for (i = 0; i < 8; i++) {
  193. printk(" busyluns[%d]=%08lx dev[%d]={ disconnect_ok=%d stp=%X sof=%X sync_state=%X }\n",
  194. i, info->busyluns[i], i,
  195. info->device[i].disconnect_ok, info->device[i].stp,
  196. info->device[i].sof, info->device[i].sync_state);
  197. }
  198. printk(" dma={ transfer_type=%X setup=%p pseudo=%p stop=%p }\n",
  199. info->dma.transfer_type, info->dma.setup,
  200. info->dma.pseudo, info->dma.stop);
  201. printk(" internal_done=%X magic_end=%lX }\n",
  202. info->internal_done, info->magic_end);
  203. }
  204. #ifdef CHECK_STRUCTURE
  205. static void __fas216_checkmagic(FAS216_Info *info, const char *func)
  206. {
  207. int corruption = 0;
  208. if (info->magic_start != MAGIC) {
  209. printk(KERN_CRIT "FAS216 Error: magic at start corrupted\n");
  210. corruption++;
  211. }
  212. if (info->magic_end != MAGIC) {
  213. printk(KERN_CRIT "FAS216 Error: magic at end corrupted\n");
  214. corruption++;
  215. }
  216. if (corruption) {
  217. fas216_dumpinfo(info);
  218. panic("scsi memory space corrupted in %s", func);
  219. }
  220. }
  221. #define fas216_checkmagic(info) __fas216_checkmagic((info), __func__)
  222. #else
  223. #define fas216_checkmagic(info)
  224. #endif
  225. static const char *fas216_bus_phase(int stat)
  226. {
  227. static const char *phases[] = {
  228. "DATA OUT", "DATA IN",
  229. "COMMAND", "STATUS",
  230. "MISC OUT", "MISC IN",
  231. "MESG OUT", "MESG IN"
  232. };
  233. return phases[stat & STAT_BUSMASK];
  234. }
  235. static const char *fas216_drv_phase(FAS216_Info *info)
  236. {
  237. static const char *phases[] = {
  238. [PHASE_IDLE] = "idle",
  239. [PHASE_SELECTION] = "selection",
  240. [PHASE_COMMAND] = "command",
  241. [PHASE_DATAOUT] = "data out",
  242. [PHASE_DATAIN] = "data in",
  243. [PHASE_MSGIN] = "message in",
  244. [PHASE_MSGIN_DISCONNECT]= "disconnect",
  245. [PHASE_MSGOUT_EXPECT] = "expect message out",
  246. [PHASE_MSGOUT] = "message out",
  247. [PHASE_STATUS] = "status",
  248. [PHASE_DONE] = "done",
  249. };
  250. if (info->scsi.phase < ARRAY_SIZE(phases) &&
  251. phases[info->scsi.phase])
  252. return phases[info->scsi.phase];
  253. return "???";
  254. }
  255. static char fas216_target(FAS216_Info *info)
  256. {
  257. if (info->SCpnt)
  258. return '0' + info->SCpnt->device->id;
  259. else
  260. return 'H';
  261. }
  262. static void
  263. fas216_do_log(FAS216_Info *info, char target, char *fmt, va_list ap)
  264. {
  265. static char buf[1024];
  266. vsnprintf(buf, sizeof(buf), fmt, ap);
  267. printk("scsi%d.%c: %s", info->host->host_no, target, buf);
  268. }
  269. static void fas216_log_command(FAS216_Info *info, int level,
  270. struct scsi_cmnd *SCpnt, char *fmt, ...)
  271. {
  272. va_list args;
  273. if (level != 0 && !(level & level_mask))
  274. return;
  275. va_start(args, fmt);
  276. fas216_do_log(info, '0' + SCpnt->device->id, fmt, args);
  277. va_end(args);
  278. printk(" CDB: ");
  279. __scsi_print_command(SCpnt->cmnd);
  280. }
  281. static void
  282. fas216_log_target(FAS216_Info *info, int level, int target, char *fmt, ...)
  283. {
  284. va_list args;
  285. if (level != 0 && !(level & level_mask))
  286. return;
  287. if (target < 0)
  288. target = 'H';
  289. else
  290. target += '0';
  291. va_start(args, fmt);
  292. fas216_do_log(info, target, fmt, args);
  293. va_end(args);
  294. printk("\n");
  295. }
  296. static void fas216_log(FAS216_Info *info, int level, char *fmt, ...)
  297. {
  298. va_list args;
  299. if (level != 0 && !(level & level_mask))
  300. return;
  301. va_start(args, fmt);
  302. fas216_do_log(info, fas216_target(info), fmt, args);
  303. va_end(args);
  304. printk("\n");
  305. }
  306. #define PH_SIZE 32
  307. static struct { int stat, ssr, isr, ph; } ph_list[PH_SIZE];
  308. static int ph_ptr;
  309. static void add_debug_list(int stat, int ssr, int isr, int ph)
  310. {
  311. ph_list[ph_ptr].stat = stat;
  312. ph_list[ph_ptr].ssr = ssr;
  313. ph_list[ph_ptr].isr = isr;
  314. ph_list[ph_ptr].ph = ph;
  315. ph_ptr = (ph_ptr + 1) & (PH_SIZE-1);
  316. }
  317. static struct { int command; void *from; } cmd_list[8];
  318. static int cmd_ptr;
  319. static void fas216_cmd(FAS216_Info *info, unsigned int command)
  320. {
  321. cmd_list[cmd_ptr].command = command;
  322. cmd_list[cmd_ptr].from = __builtin_return_address(0);
  323. cmd_ptr = (cmd_ptr + 1) & 7;
  324. fas216_writeb(info, REG_CMD, command);
  325. }
  326. static void print_debug_list(void)
  327. {
  328. int i;
  329. i = ph_ptr;
  330. printk(KERN_ERR "SCSI IRQ trail\n");
  331. do {
  332. printk(" %02x:%02x:%02x:%1x",
  333. ph_list[i].stat, ph_list[i].ssr,
  334. ph_list[i].isr, ph_list[i].ph);
  335. i = (i + 1) & (PH_SIZE - 1);
  336. if (((i ^ ph_ptr) & 7) == 0)
  337. printk("\n");
  338. } while (i != ph_ptr);
  339. if ((i ^ ph_ptr) & 7)
  340. printk("\n");
  341. i = cmd_ptr;
  342. printk(KERN_ERR "FAS216 commands: ");
  343. do {
  344. printk("%02x:%p ", cmd_list[i].command, cmd_list[i].from);
  345. i = (i + 1) & 7;
  346. } while (i != cmd_ptr);
  347. printk("\n");
  348. }
  349. static void fas216_done(FAS216_Info *info, unsigned int result);
  350. /**
  351. * fas216_get_last_msg - retrive last message from the list
  352. * @info: interface to search
  353. * @pos: current fifo position
  354. *
  355. * Retrieve a last message from the list, using position in fifo.
  356. */
  357. static inline unsigned short
  358. fas216_get_last_msg(FAS216_Info *info, int pos)
  359. {
  360. unsigned short packed_msg = NOP;
  361. struct message *msg;
  362. int msgnr = 0;
  363. while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
  364. if (pos >= msg->fifo)
  365. break;
  366. }
  367. if (msg) {
  368. if (msg->msg[0] == EXTENDED_MESSAGE)
  369. packed_msg = EXTENDED_MESSAGE | msg->msg[2] << 8;
  370. else
  371. packed_msg = msg->msg[0];
  372. }
  373. fas216_log(info, LOG_MESSAGES,
  374. "Message: %04x found at position %02x\n", packed_msg, pos);
  375. return packed_msg;
  376. }
  377. /**
  378. * fas216_syncperiod - calculate STP register value
  379. * @info: state structure for interface connected to device
  380. * @ns: period in ns (between subsequent bytes)
  381. *
  382. * Calculate value to be loaded into the STP register for a given period
  383. * in ns. Returns a value suitable for REG_STP.
  384. */
  385. static int fas216_syncperiod(FAS216_Info *info, int ns)
  386. {
  387. int value = (info->ifcfg.clockrate * ns) / 1000;
  388. fas216_checkmagic(info);
  389. if (value < 4)
  390. value = 4;
  391. else if (value > 35)
  392. value = 35;
  393. return value & 31;
  394. }
  395. /**
  396. * fas216_set_sync - setup FAS216 chip for specified transfer period.
  397. * @info: state structure for interface connected to device
  398. * @target: target
  399. *
  400. * Correctly setup FAS216 chip for specified transfer period.
  401. * Notes : we need to switch the chip out of FASTSCSI mode if we have
  402. * a transfer period >= 200ns - otherwise the chip will violate
  403. * the SCSI timings.
  404. */
  405. static void fas216_set_sync(FAS216_Info *info, int target)
  406. {
  407. unsigned int cntl3;
  408. fas216_writeb(info, REG_SOF, info->device[target].sof);
  409. fas216_writeb(info, REG_STP, info->device[target].stp);
  410. cntl3 = info->scsi.cfg[2];
  411. if (info->device[target].period >= (200 / 4))
  412. cntl3 = cntl3 & ~CNTL3_FASTSCSI;
  413. fas216_writeb(info, REG_CNTL3, cntl3);
  414. }
  415. /* Synchronous transfer support
  416. *
  417. * Note: The SCSI II r10 spec says (5.6.12):
  418. *
  419. * (2) Due to historical problems with early host adapters that could
  420. * not accept an SDTR message, some targets may not initiate synchronous
  421. * negotiation after a power cycle as required by this standard. Host
  422. * adapters that support synchronous mode may avoid the ensuing failure
  423. * modes when the target is independently power cycled by initiating a
  424. * synchronous negotiation on each REQUEST SENSE and INQUIRY command.
  425. * This approach increases the SCSI bus overhead and is not recommended
  426. * for new implementations. The correct method is to respond to an
  427. * SDTR message with a MESSAGE REJECT message if the either the
  428. * initiator or target devices does not support synchronous transfers
  429. * or does not want to negotiate for synchronous transfers at the time.
  430. * Using the correct method assures compatibility with wide data
  431. * transfers and future enhancements.
  432. *
  433. * We will always initiate a synchronous transfer negotiation request on
  434. * every INQUIRY or REQUEST SENSE message, unless the target itself has
  435. * at some point performed a synchronous transfer negotiation request, or
  436. * we have synchronous transfers disabled for this device.
  437. */
  438. /**
  439. * fas216_handlesync - Handle a synchronous transfer message
  440. * @info: state structure for interface
  441. * @msg: message from target
  442. *
  443. * Handle a synchronous transfer message from the target
  444. */
  445. static void fas216_handlesync(FAS216_Info *info, char *msg)
  446. {
  447. struct fas216_device *dev = &info->device[info->SCpnt->device->id];
  448. enum { sync, async, none, reject } res = none;
  449. #ifdef SCSI2_SYNC
  450. switch (msg[0]) {
  451. case MESSAGE_REJECT:
  452. /* Synchronous transfer request failed.
  453. * Note: SCSI II r10:
  454. *
  455. * SCSI devices that are capable of synchronous
  456. * data transfers shall not respond to an SDTR
  457. * message with a MESSAGE REJECT message.
  458. *
  459. * Hence, if we get this condition, we disable
  460. * negotiation for this device.
  461. */
  462. if (dev->sync_state == neg_inprogress) {
  463. dev->sync_state = neg_invalid;
  464. res = async;
  465. }
  466. break;
  467. case EXTENDED_MESSAGE:
  468. switch (dev->sync_state) {
  469. /* We don't accept synchronous transfer requests.
  470. * Respond with a MESSAGE_REJECT to prevent a
  471. * synchronous transfer agreement from being reached.
  472. */
  473. case neg_invalid:
  474. res = reject;
  475. break;
  476. /* We were not negotiating a synchronous transfer,
  477. * but the device sent us a negotiation request.
  478. * Honour the request by sending back a SDTR
  479. * message containing our capability, limited by
  480. * the targets capability.
  481. */
  482. default:
  483. fas216_cmd(info, CMD_SETATN);
  484. if (msg[4] > info->ifcfg.sync_max_depth)
  485. msg[4] = info->ifcfg.sync_max_depth;
  486. if (msg[3] < 1000 / info->ifcfg.clockrate)
  487. msg[3] = 1000 / info->ifcfg.clockrate;
  488. msgqueue_flush(&info->scsi.msgs);
  489. msgqueue_addmsg(&info->scsi.msgs, 5,
  490. EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
  491. msg[3], msg[4]);
  492. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  493. /* This is wrong. The agreement is not in effect
  494. * until this message is accepted by the device
  495. */
  496. dev->sync_state = neg_targcomplete;
  497. res = sync;
  498. break;
  499. /* We initiated the synchronous transfer negotiation,
  500. * and have successfully received a response from the
  501. * target. The synchronous transfer agreement has been
  502. * reached. Note: if the values returned are out of our
  503. * bounds, we must reject the message.
  504. */
  505. case neg_inprogress:
  506. res = reject;
  507. if (msg[4] <= info->ifcfg.sync_max_depth &&
  508. msg[3] >= 1000 / info->ifcfg.clockrate) {
  509. dev->sync_state = neg_complete;
  510. res = sync;
  511. }
  512. break;
  513. }
  514. }
  515. #else
  516. res = reject;
  517. #endif
  518. switch (res) {
  519. case sync:
  520. dev->period = msg[3];
  521. dev->sof = msg[4];
  522. dev->stp = fas216_syncperiod(info, msg[3] * 4);
  523. fas216_set_sync(info, info->SCpnt->device->id);
  524. break;
  525. case reject:
  526. fas216_cmd(info, CMD_SETATN);
  527. msgqueue_flush(&info->scsi.msgs);
  528. msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
  529. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  530. case async:
  531. dev->period = info->ifcfg.asyncperiod / 4;
  532. dev->sof = 0;
  533. dev->stp = info->scsi.async_stp;
  534. fas216_set_sync(info, info->SCpnt->device->id);
  535. break;
  536. case none:
  537. break;
  538. }
  539. }
  540. /**
  541. * fas216_updateptrs - update data pointers after transfer suspended/paused
  542. * @info: interface's local pointer to update
  543. * @bytes_transferred: number of bytes transferred
  544. *
  545. * Update data pointers after transfer suspended/paused
  546. */
  547. static void fas216_updateptrs(FAS216_Info *info, int bytes_transferred)
  548. {
  549. struct scsi_pointer *SCp = &info->scsi.SCp;
  550. fas216_checkmagic(info);
  551. BUG_ON(bytes_transferred < 0);
  552. SCp->phase -= bytes_transferred;
  553. while (bytes_transferred != 0) {
  554. if (SCp->this_residual > bytes_transferred)
  555. break;
  556. /*
  557. * We have used up this buffer. Move on to the
  558. * next buffer.
  559. */
  560. bytes_transferred -= SCp->this_residual;
  561. if (!next_SCp(SCp) && bytes_transferred) {
  562. printk(KERN_WARNING "scsi%d.%c: out of buffers\n",
  563. info->host->host_no, '0' + info->SCpnt->device->id);
  564. return;
  565. }
  566. }
  567. SCp->this_residual -= bytes_transferred;
  568. if (SCp->this_residual)
  569. SCp->ptr += bytes_transferred;
  570. else
  571. SCp->ptr = NULL;
  572. }
  573. /**
  574. * fas216_pio - transfer data off of/on to card using programmed IO
  575. * @info: interface to transfer data to/from
  576. * @direction: direction to transfer data (DMA_OUT/DMA_IN)
  577. *
  578. * Transfer data off of/on to card using programmed IO.
  579. * Notes: this is incredibly slow.
  580. */
  581. static void fas216_pio(FAS216_Info *info, fasdmadir_t direction)
  582. {
  583. struct scsi_pointer *SCp = &info->scsi.SCp;
  584. fas216_checkmagic(info);
  585. if (direction == DMA_OUT)
  586. fas216_writeb(info, REG_FF, get_next_SCp_byte(SCp));
  587. else
  588. put_next_SCp_byte(SCp, fas216_readb(info, REG_FF));
  589. if (SCp->this_residual == 0)
  590. next_SCp(SCp);
  591. }
  592. static void fas216_set_stc(FAS216_Info *info, unsigned int length)
  593. {
  594. fas216_writeb(info, REG_STCL, length);
  595. fas216_writeb(info, REG_STCM, length >> 8);
  596. fas216_writeb(info, REG_STCH, length >> 16);
  597. }
  598. static unsigned int fas216_get_ctc(FAS216_Info *info)
  599. {
  600. return fas216_readb(info, REG_CTCL) +
  601. (fas216_readb(info, REG_CTCM) << 8) +
  602. (fas216_readb(info, REG_CTCH) << 16);
  603. }
  604. /**
  605. * fas216_cleanuptransfer - clean up after a transfer has completed.
  606. * @info: interface to clean up
  607. *
  608. * Update the data pointers according to the number of bytes transferred
  609. * on the SCSI bus.
  610. */
  611. static void fas216_cleanuptransfer(FAS216_Info *info)
  612. {
  613. unsigned long total, residual, fifo;
  614. fasdmatype_t dmatype = info->dma.transfer_type;
  615. info->dma.transfer_type = fasdma_none;
  616. /*
  617. * PIO transfers do not need to be cleaned up.
  618. */
  619. if (dmatype == fasdma_pio || dmatype == fasdma_none)
  620. return;
  621. if (dmatype == fasdma_real_all)
  622. total = info->scsi.SCp.phase;
  623. else
  624. total = info->scsi.SCp.this_residual;
  625. residual = fas216_get_ctc(info);
  626. fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
  627. fas216_log(info, LOG_BUFFER, "cleaning up from previous "
  628. "transfer: length 0x%06x, residual 0x%x, fifo %d",
  629. total, residual, fifo);
  630. /*
  631. * If we were performing Data-Out, the transfer counter
  632. * counts down each time a byte is transferred by the
  633. * host to the FIFO. This means we must include the
  634. * bytes left in the FIFO from the transfer counter.
  635. */
  636. if (info->scsi.phase == PHASE_DATAOUT)
  637. residual += fifo;
  638. fas216_updateptrs(info, total - residual);
  639. }
  640. /**
  641. * fas216_transfer - Perform a DMA/PIO transfer off of/on to card
  642. * @info: interface from which device disconnected from
  643. *
  644. * Start a DMA/PIO transfer off of/on to card
  645. */
  646. static void fas216_transfer(FAS216_Info *info)
  647. {
  648. fasdmadir_t direction;
  649. fasdmatype_t dmatype;
  650. fas216_log(info, LOG_BUFFER,
  651. "starttransfer: buffer %p length 0x%06x reqlen 0x%06x",
  652. info->scsi.SCp.ptr, info->scsi.SCp.this_residual,
  653. info->scsi.SCp.phase);
  654. if (!info->scsi.SCp.ptr) {
  655. fas216_log(info, LOG_ERROR, "null buffer passed to "
  656. "fas216_starttransfer");
  657. print_SCp(&info->scsi.SCp, "SCp: ", "\n");
  658. print_SCp(&info->SCpnt->SCp, "Cmnd SCp: ", "\n");
  659. return;
  660. }
  661. /*
  662. * If we have a synchronous transfer agreement in effect, we must
  663. * use DMA mode. If we are using asynchronous transfers, we may
  664. * use DMA mode or PIO mode.
  665. */
  666. if (info->device[info->SCpnt->device->id].sof)
  667. dmatype = fasdma_real_all;
  668. else
  669. dmatype = fasdma_pio;
  670. if (info->scsi.phase == PHASE_DATAOUT)
  671. direction = DMA_OUT;
  672. else
  673. direction = DMA_IN;
  674. if (info->dma.setup)
  675. dmatype = info->dma.setup(info->host, &info->scsi.SCp,
  676. direction, dmatype);
  677. info->dma.transfer_type = dmatype;
  678. if (dmatype == fasdma_real_all)
  679. fas216_set_stc(info, info->scsi.SCp.phase);
  680. else
  681. fas216_set_stc(info, info->scsi.SCp.this_residual);
  682. switch (dmatype) {
  683. case fasdma_pio:
  684. fas216_log(info, LOG_BUFFER, "PIO transfer");
  685. fas216_writeb(info, REG_SOF, 0);
  686. fas216_writeb(info, REG_STP, info->scsi.async_stp);
  687. fas216_cmd(info, CMD_TRANSFERINFO);
  688. fas216_pio(info, direction);
  689. break;
  690. case fasdma_pseudo:
  691. fas216_log(info, LOG_BUFFER, "pseudo transfer");
  692. fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
  693. info->dma.pseudo(info->host, &info->scsi.SCp,
  694. direction, info->SCpnt->transfersize);
  695. break;
  696. case fasdma_real_block:
  697. fas216_log(info, LOG_BUFFER, "block dma transfer");
  698. fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
  699. break;
  700. case fasdma_real_all:
  701. fas216_log(info, LOG_BUFFER, "total dma transfer");
  702. fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
  703. break;
  704. default:
  705. fas216_log(info, LOG_BUFFER | LOG_ERROR,
  706. "invalid FAS216 DMA type");
  707. break;
  708. }
  709. }
  710. /**
  711. * fas216_stoptransfer - Stop a DMA transfer onto / off of the card
  712. * @info: interface from which device disconnected from
  713. *
  714. * Called when we switch away from DATA IN or DATA OUT phases.
  715. */
  716. static void fas216_stoptransfer(FAS216_Info *info)
  717. {
  718. fas216_checkmagic(info);
  719. if (info->dma.transfer_type == fasdma_real_all ||
  720. info->dma.transfer_type == fasdma_real_block)
  721. info->dma.stop(info->host, &info->scsi.SCp);
  722. fas216_cleanuptransfer(info);
  723. if (info->scsi.phase == PHASE_DATAIN) {
  724. unsigned int fifo;
  725. /*
  726. * If we were performing Data-In, then the FIFO counter
  727. * contains the number of bytes not transferred via DMA
  728. * from the on-board FIFO. Read them manually.
  729. */
  730. fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
  731. while (fifo && info->scsi.SCp.ptr) {
  732. *info->scsi.SCp.ptr = fas216_readb(info, REG_FF);
  733. fas216_updateptrs(info, 1);
  734. fifo--;
  735. }
  736. } else {
  737. /*
  738. * After a Data-Out phase, there may be unsent
  739. * bytes left in the FIFO. Flush them out.
  740. */
  741. fas216_cmd(info, CMD_FLUSHFIFO);
  742. }
  743. }
  744. static void fas216_aborttransfer(FAS216_Info *info)
  745. {
  746. fas216_checkmagic(info);
  747. if (info->dma.transfer_type == fasdma_real_all ||
  748. info->dma.transfer_type == fasdma_real_block)
  749. info->dma.stop(info->host, &info->scsi.SCp);
  750. info->dma.transfer_type = fasdma_none;
  751. fas216_cmd(info, CMD_FLUSHFIFO);
  752. }
  753. static void fas216_kick(FAS216_Info *info);
  754. /**
  755. * fas216_disconnected_intr - handle device disconnection
  756. * @info: interface from which device disconnected from
  757. *
  758. * Handle device disconnection
  759. */
  760. static void fas216_disconnect_intr(FAS216_Info *info)
  761. {
  762. unsigned long flags;
  763. fas216_checkmagic(info);
  764. fas216_log(info, LOG_CONNECT, "disconnect phase=%02x",
  765. info->scsi.phase);
  766. msgqueue_flush(&info->scsi.msgs);
  767. switch (info->scsi.phase) {
  768. case PHASE_SELECTION: /* while selecting - no target */
  769. case PHASE_SELSTEPS:
  770. fas216_done(info, DID_NO_CONNECT);
  771. break;
  772. case PHASE_MSGIN_DISCONNECT: /* message in - disconnecting */
  773. info->scsi.disconnectable = 1;
  774. info->scsi.phase = PHASE_IDLE;
  775. info->stats.disconnects += 1;
  776. spin_lock_irqsave(&info->host_lock, flags);
  777. if (info->scsi.phase == PHASE_IDLE)
  778. fas216_kick(info);
  779. spin_unlock_irqrestore(&info->host_lock, flags);
  780. break;
  781. case PHASE_DONE: /* at end of command - complete */
  782. fas216_done(info, DID_OK);
  783. break;
  784. case PHASE_MSGOUT: /* message out - possible ABORT message */
  785. if (fas216_get_last_msg(info, info->scsi.msgin_fifo) == ABORT) {
  786. info->scsi.aborting = 0;
  787. fas216_done(info, DID_ABORT);
  788. break;
  789. }
  790. default: /* huh? */
  791. printk(KERN_ERR "scsi%d.%c: unexpected disconnect in phase %s\n",
  792. info->host->host_no, fas216_target(info), fas216_drv_phase(info));
  793. print_debug_list();
  794. fas216_stoptransfer(info);
  795. fas216_done(info, DID_ERROR);
  796. break;
  797. }
  798. }
  799. /**
  800. * fas216_reselected_intr - start reconnection of a device
  801. * @info: interface which was reselected
  802. *
  803. * Start reconnection of a device
  804. */
  805. static void
  806. fas216_reselected_intr(FAS216_Info *info)
  807. {
  808. unsigned int cfis, i;
  809. unsigned char msg[4];
  810. unsigned char target, lun, tag;
  811. fas216_checkmagic(info);
  812. WARN_ON(info->scsi.phase == PHASE_SELECTION ||
  813. info->scsi.phase == PHASE_SELSTEPS);
  814. cfis = fas216_readb(info, REG_CFIS);
  815. fas216_log(info, LOG_CONNECT, "reconnect phase=%02x cfis=%02x",
  816. info->scsi.phase, cfis);
  817. cfis &= CFIS_CF;
  818. if (cfis < 2 || cfis > 4) {
  819. printk(KERN_ERR "scsi%d.H: incorrect number of bytes after reselect\n",
  820. info->host->host_no);
  821. goto bad_message;
  822. }
  823. for (i = 0; i < cfis; i++)
  824. msg[i] = fas216_readb(info, REG_FF);
  825. if (!(msg[0] & (1 << info->host->this_id)) ||
  826. !(msg[1] & 0x80))
  827. goto initiator_error;
  828. target = msg[0] & ~(1 << info->host->this_id);
  829. target = ffs(target) - 1;
  830. lun = msg[1] & 7;
  831. tag = 0;
  832. if (cfis >= 3) {
  833. if (msg[2] != SIMPLE_QUEUE_TAG)
  834. goto initiator_error;
  835. tag = msg[3];
  836. }
  837. /* set up for synchronous transfers */
  838. fas216_writeb(info, REG_SDID, target);
  839. fas216_set_sync(info, target);
  840. msgqueue_flush(&info->scsi.msgs);
  841. fas216_log(info, LOG_CONNECT, "Reconnected: target %1x lun %1x tag %02x",
  842. target, lun, tag);
  843. if (info->scsi.disconnectable && info->SCpnt) {
  844. info->scsi.disconnectable = 0;
  845. if (info->SCpnt->device->id == target &&
  846. info->SCpnt->device->lun == lun &&
  847. info->SCpnt->tag == tag) {
  848. fas216_log(info, LOG_CONNECT, "reconnected previously executing command");
  849. } else {
  850. queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
  851. fas216_log(info, LOG_CONNECT, "had to move command to disconnected queue");
  852. info->SCpnt = NULL;
  853. }
  854. }
  855. if (!info->SCpnt) {
  856. info->SCpnt = queue_remove_tgtluntag(&info->queues.disconnected,
  857. target, lun, tag);
  858. fas216_log(info, LOG_CONNECT, "had to get command");
  859. }
  860. if (info->SCpnt) {
  861. /*
  862. * Restore data pointer from SAVED data pointer
  863. */
  864. info->scsi.SCp = info->SCpnt->SCp;
  865. fas216_log(info, LOG_CONNECT, "data pointers: [%p, %X]",
  866. info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
  867. info->scsi.phase = PHASE_MSGIN;
  868. } else {
  869. /*
  870. * Our command structure not found - abort the
  871. * command on the target. Since we have no
  872. * record of this command, we can't send
  873. * an INITIATOR DETECTED ERROR message.
  874. */
  875. fas216_cmd(info, CMD_SETATN);
  876. #if 0
  877. if (tag)
  878. msgqueue_addmsg(&info->scsi.msgs, 2, ABORT_TAG, tag);
  879. else
  880. #endif
  881. msgqueue_addmsg(&info->scsi.msgs, 1, ABORT);
  882. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  883. info->scsi.aborting = 1;
  884. }
  885. fas216_cmd(info, CMD_MSGACCEPTED);
  886. return;
  887. initiator_error:
  888. printk(KERN_ERR "scsi%d.H: error during reselection: bytes",
  889. info->host->host_no);
  890. for (i = 0; i < cfis; i++)
  891. printk(" %02x", msg[i]);
  892. printk("\n");
  893. bad_message:
  894. fas216_cmd(info, CMD_SETATN);
  895. msgqueue_flush(&info->scsi.msgs);
  896. msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
  897. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  898. fas216_cmd(info, CMD_MSGACCEPTED);
  899. }
  900. static void fas216_parse_message(FAS216_Info *info, unsigned char *message, int msglen)
  901. {
  902. int i;
  903. switch (message[0]) {
  904. case COMMAND_COMPLETE:
  905. if (msglen != 1)
  906. goto unrecognised;
  907. printk(KERN_ERR "scsi%d.%c: command complete with no "
  908. "status in MESSAGE_IN?\n",
  909. info->host->host_no, fas216_target(info));
  910. break;
  911. case SAVE_POINTERS:
  912. if (msglen != 1)
  913. goto unrecognised;
  914. /*
  915. * Save current data pointer to SAVED data pointer
  916. * SCSI II standard says that we must not acknowledge
  917. * this until we have really saved pointers.
  918. * NOTE: we DO NOT save the command nor status pointers
  919. * as required by the SCSI II standard. These always
  920. * point to the start of their respective areas.
  921. */
  922. info->SCpnt->SCp = info->scsi.SCp;
  923. info->SCpnt->SCp.sent_command = 0;
  924. fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
  925. "save data pointers: [%p, %X]",
  926. info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
  927. break;
  928. case RESTORE_POINTERS:
  929. if (msglen != 1)
  930. goto unrecognised;
  931. /*
  932. * Restore current data pointer from SAVED data pointer
  933. */
  934. info->scsi.SCp = info->SCpnt->SCp;
  935. fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
  936. "restore data pointers: [%p, 0x%x]",
  937. info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
  938. break;
  939. case DISCONNECT:
  940. if (msglen != 1)
  941. goto unrecognised;
  942. info->scsi.phase = PHASE_MSGIN_DISCONNECT;
  943. break;
  944. case MESSAGE_REJECT:
  945. if (msglen != 1)
  946. goto unrecognised;
  947. switch (fas216_get_last_msg(info, info->scsi.msgin_fifo)) {
  948. case EXTENDED_MESSAGE | EXTENDED_SDTR << 8:
  949. fas216_handlesync(info, message);
  950. break;
  951. default:
  952. fas216_log(info, 0, "reject, last message 0x%04x",
  953. fas216_get_last_msg(info, info->scsi.msgin_fifo));
  954. }
  955. break;
  956. case NOP:
  957. break;
  958. case EXTENDED_MESSAGE:
  959. if (msglen < 3)
  960. goto unrecognised;
  961. switch (message[2]) {
  962. case EXTENDED_SDTR: /* Sync transfer negotiation request/reply */
  963. fas216_handlesync(info, message);
  964. break;
  965. default:
  966. goto unrecognised;
  967. }
  968. break;
  969. default:
  970. goto unrecognised;
  971. }
  972. return;
  973. unrecognised:
  974. fas216_log(info, 0, "unrecognised message, rejecting");
  975. printk("scsi%d.%c: message was", info->host->host_no, fas216_target(info));
  976. for (i = 0; i < msglen; i++)
  977. printk("%s%02X", i & 31 ? " " : "\n ", message[i]);
  978. printk("\n");
  979. /*
  980. * Something strange seems to be happening here -
  981. * I can't use SETATN since the chip gives me an
  982. * invalid command interrupt when I do. Weird.
  983. */
  984. fas216_cmd(info, CMD_NOP);
  985. fas216_dumpstate(info);
  986. fas216_cmd(info, CMD_SETATN);
  987. msgqueue_flush(&info->scsi.msgs);
  988. msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
  989. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  990. fas216_dumpstate(info);
  991. }
  992. static int fas216_wait_cmd(FAS216_Info *info, int cmd)
  993. {
  994. int tout;
  995. int stat;
  996. fas216_cmd(info, cmd);
  997. for (tout = 1000; tout; tout -= 1) {
  998. stat = fas216_readb(info, REG_STAT);
  999. if (stat & (STAT_INT|STAT_PARITYERROR))
  1000. break;
  1001. udelay(1);
  1002. }
  1003. return stat;
  1004. }
  1005. static int fas216_get_msg_byte(FAS216_Info *info)
  1006. {
  1007. unsigned int stat = fas216_wait_cmd(info, CMD_MSGACCEPTED);
  1008. if ((stat & STAT_INT) == 0)
  1009. goto timedout;
  1010. if ((stat & STAT_BUSMASK) != STAT_MESGIN)
  1011. goto unexpected_phase_change;
  1012. fas216_readb(info, REG_INST);
  1013. stat = fas216_wait_cmd(info, CMD_TRANSFERINFO);
  1014. if ((stat & STAT_INT) == 0)
  1015. goto timedout;
  1016. if (stat & STAT_PARITYERROR)
  1017. goto parity_error;
  1018. if ((stat & STAT_BUSMASK) != STAT_MESGIN)
  1019. goto unexpected_phase_change;
  1020. fas216_readb(info, REG_INST);
  1021. return fas216_readb(info, REG_FF);
  1022. timedout:
  1023. fas216_log(info, LOG_ERROR, "timed out waiting for message byte");
  1024. return -1;
  1025. unexpected_phase_change:
  1026. fas216_log(info, LOG_ERROR, "unexpected phase change: status = %02x", stat);
  1027. return -2;
  1028. parity_error:
  1029. fas216_log(info, LOG_ERROR, "parity error during message in phase");
  1030. return -3;
  1031. }
  1032. /**
  1033. * fas216_message - handle a function done interrupt from FAS216 chip
  1034. * @info: interface which caused function done interrupt
  1035. *
  1036. * Handle a function done interrupt from FAS216 chip
  1037. */
  1038. static void fas216_message(FAS216_Info *info)
  1039. {
  1040. unsigned char *message = info->scsi.message;
  1041. unsigned int msglen = 1;
  1042. int msgbyte = 0;
  1043. fas216_checkmagic(info);
  1044. message[0] = fas216_readb(info, REG_FF);
  1045. if (message[0] == EXTENDED_MESSAGE) {
  1046. msgbyte = fas216_get_msg_byte(info);
  1047. if (msgbyte >= 0) {
  1048. message[1] = msgbyte;
  1049. for (msglen = 2; msglen < message[1] + 2; msglen++) {
  1050. msgbyte = fas216_get_msg_byte(info);
  1051. if (msgbyte >= 0)
  1052. message[msglen] = msgbyte;
  1053. else
  1054. break;
  1055. }
  1056. }
  1057. }
  1058. if (msgbyte == -3)
  1059. goto parity_error;
  1060. #ifdef DEBUG_MESSAGES
  1061. {
  1062. int i;
  1063. printk("scsi%d.%c: message in: ",
  1064. info->host->host_no, fas216_target(info));
  1065. for (i = 0; i < msglen; i++)
  1066. printk("%02X ", message[i]);
  1067. printk("\n");
  1068. }
  1069. #endif
  1070. fas216_parse_message(info, message, msglen);
  1071. fas216_cmd(info, CMD_MSGACCEPTED);
  1072. return;
  1073. parity_error:
  1074. fas216_cmd(info, CMD_SETATN);
  1075. msgqueue_flush(&info->scsi.msgs);
  1076. msgqueue_addmsg(&info->scsi.msgs, 1, MSG_PARITY_ERROR);
  1077. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  1078. fas216_cmd(info, CMD_MSGACCEPTED);
  1079. return;
  1080. }
  1081. /**
  1082. * fas216_send_command - send command after all message bytes have been sent
  1083. * @info: interface which caused bus service
  1084. *
  1085. * Send a command to a target after all message bytes have been sent
  1086. */
  1087. static void fas216_send_command(FAS216_Info *info)
  1088. {
  1089. int i;
  1090. fas216_checkmagic(info);
  1091. fas216_cmd(info, CMD_NOP|CMD_WITHDMA);
  1092. fas216_cmd(info, CMD_FLUSHFIFO);
  1093. /* load command */
  1094. for (i = info->scsi.SCp.sent_command; i < info->SCpnt->cmd_len; i++)
  1095. fas216_writeb(info, REG_FF, info->SCpnt->cmnd[i]);
  1096. fas216_cmd(info, CMD_TRANSFERINFO);
  1097. info->scsi.phase = PHASE_COMMAND;
  1098. }
  1099. /**
  1100. * fas216_send_messageout - handle bus service to send a message
  1101. * @info: interface which caused bus service
  1102. *
  1103. * Handle bus service to send a message.
  1104. * Note: We do not allow the device to change the data direction!
  1105. */
  1106. static void fas216_send_messageout(FAS216_Info *info, int start)
  1107. {
  1108. unsigned int tot_msglen = msgqueue_msglength(&info->scsi.msgs);
  1109. fas216_checkmagic(info);
  1110. fas216_cmd(info, CMD_FLUSHFIFO);
  1111. if (tot_msglen) {
  1112. struct message *msg;
  1113. int msgnr = 0;
  1114. while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
  1115. int i;
  1116. for (i = start; i < msg->length; i++)
  1117. fas216_writeb(info, REG_FF, msg->msg[i]);
  1118. msg->fifo = tot_msglen - (fas216_readb(info, REG_CFIS) & CFIS_CF);
  1119. start = 0;
  1120. }
  1121. } else
  1122. fas216_writeb(info, REG_FF, NOP);
  1123. fas216_cmd(info, CMD_TRANSFERINFO);
  1124. info->scsi.phase = PHASE_MSGOUT;
  1125. }
  1126. /**
  1127. * fas216_busservice_intr - handle bus service interrupt from FAS216 chip
  1128. * @info: interface which caused bus service interrupt
  1129. * @stat: Status register contents
  1130. * @is: SCSI Status register contents
  1131. *
  1132. * Handle a bus service interrupt from FAS216 chip
  1133. */
  1134. static void fas216_busservice_intr(FAS216_Info *info, unsigned int stat, unsigned int is)
  1135. {
  1136. fas216_checkmagic(info);
  1137. fas216_log(info, LOG_BUSSERVICE,
  1138. "bus service: stat=%02x is=%02x phase=%02x",
  1139. stat, is, info->scsi.phase);
  1140. switch (info->scsi.phase) {
  1141. case PHASE_SELECTION:
  1142. if ((is & IS_BITS) != IS_MSGBYTESENT)
  1143. goto bad_is;
  1144. break;
  1145. case PHASE_SELSTEPS:
  1146. switch (is & IS_BITS) {
  1147. case IS_SELARB:
  1148. case IS_MSGBYTESENT:
  1149. goto bad_is;
  1150. case IS_NOTCOMMAND:
  1151. case IS_EARLYPHASE:
  1152. if ((stat & STAT_BUSMASK) == STAT_MESGIN)
  1153. break;
  1154. goto bad_is;
  1155. case IS_COMPLETE:
  1156. break;
  1157. }
  1158. default:
  1159. break;
  1160. }
  1161. fas216_cmd(info, CMD_NOP);
  1162. #define STATE(st,ph) ((ph) << 3 | (st))
  1163. /* This table describes the legal SCSI state transitions,
  1164. * as described by the SCSI II spec.
  1165. */
  1166. switch (STATE(stat & STAT_BUSMASK, info->scsi.phase)) {
  1167. case STATE(STAT_DATAIN, PHASE_SELSTEPS):/* Sel w/ steps -> Data In */
  1168. case STATE(STAT_DATAIN, PHASE_MSGOUT): /* Message Out -> Data In */
  1169. case STATE(STAT_DATAIN, PHASE_COMMAND): /* Command -> Data In */
  1170. case STATE(STAT_DATAIN, PHASE_MSGIN): /* Message In -> Data In */
  1171. info->scsi.phase = PHASE_DATAIN;
  1172. fas216_transfer(info);
  1173. return;
  1174. case STATE(STAT_DATAIN, PHASE_DATAIN): /* Data In -> Data In */
  1175. case STATE(STAT_DATAOUT, PHASE_DATAOUT):/* Data Out -> Data Out */
  1176. fas216_cleanuptransfer(info);
  1177. fas216_transfer(info);
  1178. return;
  1179. case STATE(STAT_DATAOUT, PHASE_SELSTEPS):/* Sel w/ steps-> Data Out */
  1180. case STATE(STAT_DATAOUT, PHASE_MSGOUT): /* Message Out -> Data Out */
  1181. case STATE(STAT_DATAOUT, PHASE_COMMAND):/* Command -> Data Out */
  1182. case STATE(STAT_DATAOUT, PHASE_MSGIN): /* Message In -> Data Out */
  1183. fas216_cmd(info, CMD_FLUSHFIFO);
  1184. info->scsi.phase = PHASE_DATAOUT;
  1185. fas216_transfer(info);
  1186. return;
  1187. case STATE(STAT_STATUS, PHASE_DATAOUT): /* Data Out -> Status */
  1188. case STATE(STAT_STATUS, PHASE_DATAIN): /* Data In -> Status */
  1189. fas216_stoptransfer(info);
  1190. case STATE(STAT_STATUS, PHASE_SELSTEPS):/* Sel w/ steps -> Status */
  1191. case STATE(STAT_STATUS, PHASE_MSGOUT): /* Message Out -> Status */
  1192. case STATE(STAT_STATUS, PHASE_COMMAND): /* Command -> Status */
  1193. case STATE(STAT_STATUS, PHASE_MSGIN): /* Message In -> Status */
  1194. fas216_cmd(info, CMD_INITCMDCOMPLETE);
  1195. info->scsi.phase = PHASE_STATUS;
  1196. return;
  1197. case STATE(STAT_MESGIN, PHASE_DATAOUT): /* Data Out -> Message In */
  1198. case STATE(STAT_MESGIN, PHASE_DATAIN): /* Data In -> Message In */
  1199. fas216_stoptransfer(info);
  1200. case STATE(STAT_MESGIN, PHASE_COMMAND): /* Command -> Message In */
  1201. case STATE(STAT_MESGIN, PHASE_SELSTEPS):/* Sel w/ steps -> Message In */
  1202. case STATE(STAT_MESGIN, PHASE_MSGOUT): /* Message Out -> Message In */
  1203. info->scsi.msgin_fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
  1204. fas216_cmd(info, CMD_FLUSHFIFO);
  1205. fas216_cmd(info, CMD_TRANSFERINFO);
  1206. info->scsi.phase = PHASE_MSGIN;
  1207. return;
  1208. case STATE(STAT_MESGIN, PHASE_MSGIN):
  1209. info->scsi.msgin_fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
  1210. fas216_cmd(info, CMD_TRANSFERINFO);
  1211. return;
  1212. case STATE(STAT_COMMAND, PHASE_MSGOUT): /* Message Out -> Command */
  1213. case STATE(STAT_COMMAND, PHASE_MSGIN): /* Message In -> Command */
  1214. fas216_send_command(info);
  1215. info->scsi.phase = PHASE_COMMAND;
  1216. return;
  1217. /*
  1218. * Selection -> Message Out
  1219. */
  1220. case STATE(STAT_MESGOUT, PHASE_SELECTION):
  1221. fas216_send_messageout(info, 1);
  1222. return;
  1223. /*
  1224. * Message Out -> Message Out
  1225. */
  1226. case STATE(STAT_MESGOUT, PHASE_SELSTEPS):
  1227. case STATE(STAT_MESGOUT, PHASE_MSGOUT):
  1228. /*
  1229. * If we get another message out phase, this usually
  1230. * means some parity error occurred. Resend complete
  1231. * set of messages. If we have more than one byte to
  1232. * send, we need to assert ATN again.
  1233. */
  1234. if (info->device[info->SCpnt->device->id].parity_check) {
  1235. /*
  1236. * We were testing... good, the device
  1237. * supports parity checking.
  1238. */
  1239. info->device[info->SCpnt->device->id].parity_check = 0;
  1240. info->device[info->SCpnt->device->id].parity_enabled = 1;
  1241. fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
  1242. }
  1243. if (msgqueue_msglength(&info->scsi.msgs) > 1)
  1244. fas216_cmd(info, CMD_SETATN);
  1245. /*FALLTHROUGH*/
  1246. /*
  1247. * Any -> Message Out
  1248. */
  1249. case STATE(STAT_MESGOUT, PHASE_MSGOUT_EXPECT):
  1250. fas216_send_messageout(info, 0);
  1251. return;
  1252. /* Error recovery rules.
  1253. * These either attempt to abort or retry the operation.
  1254. * TODO: we need more of these
  1255. */
  1256. case STATE(STAT_COMMAND, PHASE_COMMAND):/* Command -> Command */
  1257. /* error - we've sent out all the command bytes
  1258. * we have.
  1259. * NOTE: we need SAVE DATA POINTERS/RESTORE DATA POINTERS
  1260. * to include the command bytes sent for this to work
  1261. * correctly.
  1262. */
  1263. printk(KERN_ERR "scsi%d.%c: "
  1264. "target trying to receive more command bytes\n",
  1265. info->host->host_no, fas216_target(info));
  1266. fas216_cmd(info, CMD_SETATN);
  1267. fas216_set_stc(info, 15);
  1268. fas216_cmd(info, CMD_PADBYTES | CMD_WITHDMA);
  1269. msgqueue_flush(&info->scsi.msgs);
  1270. msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
  1271. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  1272. return;
  1273. }
  1274. if (info->scsi.phase == PHASE_MSGIN_DISCONNECT) {
  1275. printk(KERN_ERR "scsi%d.%c: disconnect message received, but bus service %s?\n",
  1276. info->host->host_no, fas216_target(info),
  1277. fas216_bus_phase(stat));
  1278. msgqueue_flush(&info->scsi.msgs);
  1279. fas216_cmd(info, CMD_SETATN);
  1280. msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
  1281. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  1282. info->scsi.aborting = 1;
  1283. fas216_cmd(info, CMD_TRANSFERINFO);
  1284. return;
  1285. }
  1286. printk(KERN_ERR "scsi%d.%c: bus phase %s after %s?\n",
  1287. info->host->host_no, fas216_target(info),
  1288. fas216_bus_phase(stat),
  1289. fas216_drv_phase(info));
  1290. print_debug_list();
  1291. return;
  1292. bad_is:
  1293. fas216_log(info, 0, "bus service at step %d?", is & IS_BITS);
  1294. fas216_dumpstate(info);
  1295. print_debug_list();
  1296. fas216_done(info, DID_ERROR);
  1297. }
  1298. /**
  1299. * fas216_funcdone_intr - handle a function done interrupt from FAS216 chip
  1300. * @info: interface which caused function done interrupt
  1301. * @stat: Status register contents
  1302. * @is: SCSI Status register contents
  1303. *
  1304. * Handle a function done interrupt from FAS216 chip
  1305. */
  1306. static void fas216_funcdone_intr(FAS216_Info *info, unsigned int stat, unsigned int is)
  1307. {
  1308. unsigned int fifo_len = fas216_readb(info, REG_CFIS) & CFIS_CF;
  1309. fas216_checkmagic(info);
  1310. fas216_log(info, LOG_FUNCTIONDONE,
  1311. "function done: stat=%02x is=%02x phase=%02x",
  1312. stat, is, info->scsi.phase);
  1313. switch (info->scsi.phase) {
  1314. case PHASE_STATUS: /* status phase - read status and msg */
  1315. if (fifo_len != 2) {
  1316. fas216_log(info, 0, "odd number of bytes in FIFO: %d", fifo_len);
  1317. }
  1318. /*
  1319. * Read status then message byte.
  1320. */
  1321. info->scsi.SCp.Status = fas216_readb(info, REG_FF);
  1322. info->scsi.SCp.Message = fas216_readb(info, REG_FF);
  1323. info->scsi.phase = PHASE_DONE;
  1324. fas216_cmd(info, CMD_MSGACCEPTED);
  1325. break;
  1326. case PHASE_IDLE:
  1327. case PHASE_SELECTION:
  1328. case PHASE_SELSTEPS:
  1329. break;
  1330. case PHASE_MSGIN: /* message in phase */
  1331. if ((stat & STAT_BUSMASK) == STAT_MESGIN) {
  1332. info->scsi.msgin_fifo = fifo_len;
  1333. fas216_message(info);
  1334. break;
  1335. }
  1336. default:
  1337. fas216_log(info, 0, "internal phase %s for function done?"
  1338. " What do I do with this?",
  1339. fas216_target(info), fas216_drv_phase(info));
  1340. }
  1341. }
  1342. static void fas216_bus_reset(FAS216_Info *info)
  1343. {
  1344. neg_t sync_state;
  1345. int i;
  1346. msgqueue_flush(&info->scsi.msgs);
  1347. sync_state = neg_invalid;
  1348. #ifdef SCSI2_SYNC
  1349. if (info->ifcfg.capabilities & (FASCAP_DMA|FASCAP_PSEUDODMA))
  1350. sync_state = neg_wait;
  1351. #endif
  1352. info->scsi.phase = PHASE_IDLE;
  1353. info->SCpnt = NULL; /* bug! */
  1354. memset(&info->scsi.SCp, 0, sizeof(info->scsi.SCp));
  1355. for (i = 0; i < 8; i++) {
  1356. info->device[i].disconnect_ok = info->ifcfg.disconnect_ok;
  1357. info->device[i].sync_state = sync_state;
  1358. info->device[i].period = info->ifcfg.asyncperiod / 4;
  1359. info->device[i].stp = info->scsi.async_stp;
  1360. info->device[i].sof = 0;
  1361. info->device[i].wide_xfer = 0;
  1362. }
  1363. info->rst_bus_status = 1;
  1364. wake_up(&info->eh_wait);
  1365. }
  1366. /**
  1367. * fas216_intr - handle interrupts to progress a command
  1368. * @info: interface to service
  1369. *
  1370. * Handle interrupts from the interface to progress a command
  1371. */
  1372. irqreturn_t fas216_intr(FAS216_Info *info)
  1373. {
  1374. unsigned char inst, is, stat;
  1375. int handled = IRQ_NONE;
  1376. fas216_checkmagic(info);
  1377. stat = fas216_readb(info, REG_STAT);
  1378. is = fas216_readb(info, REG_IS);
  1379. inst = fas216_readb(info, REG_INST);
  1380. add_debug_list(stat, is, inst, info->scsi.phase);
  1381. if (stat & STAT_INT) {
  1382. if (inst & INST_BUSRESET) {
  1383. fas216_log(info, 0, "bus reset detected");
  1384. fas216_bus_reset(info);
  1385. scsi_report_bus_reset(info->host, 0);
  1386. } else if (inst & INST_ILLEGALCMD) {
  1387. fas216_log(info, LOG_ERROR, "illegal command given\n");
  1388. fas216_dumpstate(info);
  1389. print_debug_list();
  1390. } else if (inst & INST_DISCONNECT)
  1391. fas216_disconnect_intr(info);
  1392. else if (inst & INST_RESELECTED) /* reselected */
  1393. fas216_reselected_intr(info);
  1394. else if (inst & INST_BUSSERVICE) /* bus service request */
  1395. fas216_busservice_intr(info, stat, is);
  1396. else if (inst & INST_FUNCDONE) /* function done */
  1397. fas216_funcdone_intr(info, stat, is);
  1398. else
  1399. fas216_log(info, 0, "unknown interrupt received:"
  1400. " phase %s inst %02X is %02X stat %02X",
  1401. fas216_drv_phase(info), inst, is, stat);
  1402. handled = IRQ_HANDLED;
  1403. }
  1404. return handled;
  1405. }
  1406. static void __fas216_start_command(FAS216_Info *info, struct scsi_cmnd *SCpnt)
  1407. {
  1408. int tot_msglen;
  1409. /* following what the ESP driver says */
  1410. fas216_set_stc(info, 0);
  1411. fas216_cmd(info, CMD_NOP | CMD_WITHDMA);
  1412. /* flush FIFO */
  1413. fas216_cmd(info, CMD_FLUSHFIFO);
  1414. /* load bus-id and timeout */
  1415. fas216_writeb(info, REG_SDID, BUSID(SCpnt->device->id));
  1416. fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
  1417. /* synchronous transfers */
  1418. fas216_set_sync(info, SCpnt->device->id);
  1419. tot_msglen = msgqueue_msglength(&info->scsi.msgs);
  1420. #ifdef DEBUG_MESSAGES
  1421. {
  1422. struct message *msg;
  1423. int msgnr = 0, i;
  1424. printk("scsi%d.%c: message out: ",
  1425. info->host->host_no, '0' + SCpnt->device->id);
  1426. while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
  1427. printk("{ ");
  1428. for (i = 0; i < msg->length; i++)
  1429. printk("%02x ", msg->msg[i]);
  1430. printk("} ");
  1431. }
  1432. printk("\n");
  1433. }
  1434. #endif
  1435. if (tot_msglen == 1 || tot_msglen == 3) {
  1436. /*
  1437. * We have an easy message length to send...
  1438. */
  1439. struct message *msg;
  1440. int msgnr = 0, i;
  1441. info->scsi.phase = PHASE_SELSTEPS;
  1442. /* load message bytes */
  1443. while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
  1444. for (i = 0; i < msg->length; i++)
  1445. fas216_writeb(info, REG_FF, msg->msg[i]);
  1446. msg->fifo = tot_msglen - (fas216_readb(info, REG_CFIS) & CFIS_CF);
  1447. }
  1448. /* load command */
  1449. for (i = 0; i < SCpnt->cmd_len; i++)
  1450. fas216_writeb(info, REG_FF, SCpnt->cmnd[i]);
  1451. if (tot_msglen == 1)
  1452. fas216_cmd(info, CMD_SELECTATN);
  1453. else
  1454. fas216_cmd(info, CMD_SELECTATN3);
  1455. } else {
  1456. /*
  1457. * We have an unusual number of message bytes to send.
  1458. * Load first byte into fifo, and issue SELECT with ATN and
  1459. * stop steps.
  1460. */
  1461. struct message *msg = msgqueue_getmsg(&info->scsi.msgs, 0);
  1462. fas216_writeb(info, REG_FF, msg->msg[0]);
  1463. msg->fifo = 1;
  1464. fas216_cmd(info, CMD_SELECTATNSTOP);
  1465. }
  1466. }
  1467. /*
  1468. * Decide whether we need to perform a parity test on this device.
  1469. * Can also be used to force parity error conditions during initial
  1470. * information transfer phase (message out) for test purposes.
  1471. */
  1472. static int parity_test(FAS216_Info *info, int target)
  1473. {
  1474. #if 0
  1475. if (target == 3) {
  1476. info->device[target].parity_check = 0;
  1477. return 1;
  1478. }
  1479. #endif
  1480. return info->device[target].parity_check;
  1481. }
  1482. static void fas216_start_command(FAS216_Info *info, struct scsi_cmnd *SCpnt)
  1483. {
  1484. int disconnect_ok;
  1485. /*
  1486. * claim host busy
  1487. */
  1488. info->scsi.phase = PHASE_SELECTION;
  1489. info->scsi.SCp = SCpnt->SCp;
  1490. info->SCpnt = SCpnt;
  1491. info->dma.transfer_type = fasdma_none;
  1492. if (parity_test(info, SCpnt->device->id))
  1493. fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0] | CNTL1_PTE);
  1494. else
  1495. fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
  1496. /*
  1497. * Don't allow request sense commands to disconnect.
  1498. */
  1499. disconnect_ok = SCpnt->cmnd[0] != REQUEST_SENSE &&
  1500. info->device[SCpnt->device->id].disconnect_ok;
  1501. /*
  1502. * build outgoing message bytes
  1503. */
  1504. msgqueue_flush(&info->scsi.msgs);
  1505. msgqueue_addmsg(&info->scsi.msgs, 1, IDENTIFY(disconnect_ok, SCpnt->device->lun));
  1506. /*
  1507. * add tag message if required
  1508. */
  1509. if (SCpnt->tag)
  1510. msgqueue_addmsg(&info->scsi.msgs, 2, SIMPLE_QUEUE_TAG, SCpnt->tag);
  1511. do {
  1512. #ifdef SCSI2_SYNC
  1513. if ((info->device[SCpnt->device->id].sync_state == neg_wait ||
  1514. info->device[SCpnt->device->id].sync_state == neg_complete) &&
  1515. (SCpnt->cmnd[0] == REQUEST_SENSE ||
  1516. SCpnt->cmnd[0] == INQUIRY)) {
  1517. info->device[SCpnt->device->id].sync_state = neg_inprogress;
  1518. msgqueue_addmsg(&info->scsi.msgs, 5,
  1519. EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
  1520. 1000 / info->ifcfg.clockrate,
  1521. info->ifcfg.sync_max_depth);
  1522. break;
  1523. }
  1524. #endif
  1525. } while (0);
  1526. __fas216_start_command(info, SCpnt);
  1527. }
  1528. static void fas216_allocate_tag(FAS216_Info *info, struct scsi_cmnd *SCpnt)
  1529. {
  1530. #ifdef SCSI2_TAG
  1531. /*
  1532. * tagged queuing - allocate a new tag to this command
  1533. */
  1534. if (SCpnt->device->simple_tags && SCpnt->cmnd[0] != REQUEST_SENSE &&
  1535. SCpnt->cmnd[0] != INQUIRY) {
  1536. SCpnt->device->current_tag += 1;
  1537. if (SCpnt->device->current_tag == 0)
  1538. SCpnt->device->current_tag = 1;
  1539. SCpnt->tag = SCpnt->device->current_tag;
  1540. } else
  1541. #endif
  1542. set_bit(SCpnt->device->id * 8 + SCpnt->device->lun, info->busyluns);
  1543. info->stats.removes += 1;
  1544. switch (SCpnt->cmnd[0]) {
  1545. case WRITE_6:
  1546. case WRITE_10:
  1547. case WRITE_12:
  1548. info->stats.writes += 1;
  1549. break;
  1550. case READ_6:
  1551. case READ_10:
  1552. case READ_12:
  1553. info->stats.reads += 1;
  1554. break;
  1555. default:
  1556. info->stats.miscs += 1;
  1557. break;
  1558. }
  1559. }
  1560. static void fas216_do_bus_device_reset(FAS216_Info *info,
  1561. struct scsi_cmnd *SCpnt)
  1562. {
  1563. struct message *msg;
  1564. /*
  1565. * claim host busy
  1566. */
  1567. info->scsi.phase = PHASE_SELECTION;
  1568. info->scsi.SCp = SCpnt->SCp;
  1569. info->SCpnt = SCpnt;
  1570. info->dma.transfer_type = fasdma_none;
  1571. fas216_log(info, LOG_ERROR, "sending bus device reset");
  1572. msgqueue_flush(&info->scsi.msgs);
  1573. msgqueue_addmsg(&info->scsi.msgs, 1, BUS_DEVICE_RESET);
  1574. /* following what the ESP driver says */
  1575. fas216_set_stc(info, 0);
  1576. fas216_cmd(info, CMD_NOP | CMD_WITHDMA);
  1577. /* flush FIFO */
  1578. fas216_cmd(info, CMD_FLUSHFIFO);
  1579. /* load bus-id and timeout */
  1580. fas216_writeb(info, REG_SDID, BUSID(SCpnt->device->id));
  1581. fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
  1582. /* synchronous transfers */
  1583. fas216_set_sync(info, SCpnt->device->id);
  1584. msg = msgqueue_getmsg(&info->scsi.msgs, 0);
  1585. fas216_writeb(info, REG_FF, BUS_DEVICE_RESET);
  1586. msg->fifo = 1;
  1587. fas216_cmd(info, CMD_SELECTATNSTOP);
  1588. }
  1589. /**
  1590. * fas216_kick - kick a command to the interface
  1591. * @info: our host interface to kick
  1592. *
  1593. * Kick a command to the interface, interface should be idle.
  1594. * Notes: Interrupts are always disabled!
  1595. */
  1596. static void fas216_kick(FAS216_Info *info)
  1597. {
  1598. struct scsi_cmnd *SCpnt = NULL;
  1599. #define TYPE_OTHER 0
  1600. #define TYPE_RESET 1
  1601. #define TYPE_QUEUE 2
  1602. int where_from = TYPE_OTHER;
  1603. fas216_checkmagic(info);
  1604. /*
  1605. * Obtain the next command to process.
  1606. */
  1607. do {
  1608. if (info->rstSCpnt) {
  1609. SCpnt = info->rstSCpnt;
  1610. /* don't remove it */
  1611. where_from = TYPE_RESET;
  1612. break;
  1613. }
  1614. if (info->reqSCpnt) {
  1615. SCpnt = info->reqSCpnt;
  1616. info->reqSCpnt = NULL;
  1617. break;
  1618. }
  1619. if (info->origSCpnt) {
  1620. SCpnt = info->origSCpnt;
  1621. info->origSCpnt = NULL;
  1622. break;
  1623. }
  1624. /* retrieve next command */
  1625. if (!SCpnt) {
  1626. SCpnt = queue_remove_exclude(&info->queues.issue,
  1627. info->busyluns);
  1628. where_from = TYPE_QUEUE;
  1629. break;
  1630. }
  1631. } while (0);
  1632. if (!SCpnt) {
  1633. /*
  1634. * no command pending, so enable reselection.
  1635. */
  1636. fas216_cmd(info, CMD_ENABLESEL);
  1637. return;
  1638. }
  1639. /*
  1640. * We're going to start a command, so disable reselection
  1641. */
  1642. fas216_cmd(info, CMD_DISABLESEL);
  1643. if (info->scsi.disconnectable && info->SCpnt) {
  1644. fas216_log(info, LOG_CONNECT,
  1645. "moved command for %d to disconnected queue",
  1646. info->SCpnt->device->id);
  1647. queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
  1648. info->scsi.disconnectable = 0;
  1649. info->SCpnt = NULL;
  1650. }
  1651. fas216_log_command(info, LOG_CONNECT | LOG_MESSAGES, SCpnt,
  1652. "starting");
  1653. switch (where_from) {
  1654. case TYPE_QUEUE:
  1655. fas216_allocate_tag(info, SCpnt);
  1656. case TYPE_OTHER:
  1657. fas216_start_command(info, SCpnt);
  1658. break;
  1659. case TYPE_RESET:
  1660. fas216_do_bus_device_reset(info, SCpnt);
  1661. break;
  1662. }
  1663. fas216_log(info, LOG_CONNECT, "select: data pointers [%p, %X]",
  1664. info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
  1665. /*
  1666. * should now get either DISCONNECT or
  1667. * (FUNCTION DONE with BUS SERVICE) interrupt
  1668. */
  1669. }
  1670. /*
  1671. * Clean up from issuing a BUS DEVICE RESET message to a device.
  1672. */
  1673. static void fas216_devicereset_done(FAS216_Info *info, struct scsi_cmnd *SCpnt,
  1674. unsigned int result)
  1675. {
  1676. fas216_log(info, LOG_ERROR, "fas216 device reset complete");
  1677. info->rstSCpnt = NULL;
  1678. info->rst_dev_status = 1;
  1679. wake_up(&info->eh_wait);
  1680. }
  1681. /**
  1682. * fas216_rq_sns_done - Finish processing automatic request sense command
  1683. * @info: interface that completed
  1684. * @SCpnt: command that completed
  1685. * @result: driver byte of result
  1686. *
  1687. * Finish processing automatic request sense command
  1688. */
  1689. static void fas216_rq_sns_done(FAS216_Info *info, struct scsi_cmnd *SCpnt,
  1690. unsigned int result)
  1691. {
  1692. fas216_log_target(info, LOG_CONNECT, SCpnt->device->id,
  1693. "request sense complete, result=0x%04x%02x%02x",
  1694. result, SCpnt->SCp.Message, SCpnt->SCp.Status);
  1695. if (result != DID_OK || SCpnt->SCp.Status != GOOD)
  1696. /*
  1697. * Something went wrong. Make sure that we don't
  1698. * have valid data in the sense buffer that could
  1699. * confuse the higher levels.
  1700. */
  1701. memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
  1702. //printk("scsi%d.%c: sense buffer: ", info->host->host_no, '0' + SCpnt->device->id);
  1703. //{ int i; for (i = 0; i < 32; i++) printk("%02x ", SCpnt->sense_buffer[i]); printk("\n"); }
  1704. /*
  1705. * Note that we don't set SCpnt->result, since that should
  1706. * reflect the status of the command that we were asked by
  1707. * the upper layers to process. This would have been set
  1708. * correctly by fas216_std_done.
  1709. */
  1710. scsi_eh_restore_cmnd(SCpnt, &info->ses);
  1711. SCpnt->scsi_done(SCpnt);
  1712. }
  1713. /**
  1714. * fas216_std_done - finish processing of standard command
  1715. * @info: interface that completed
  1716. * @SCpnt: command that completed
  1717. * @result: driver byte of result
  1718. *
  1719. * Finish processing of standard command
  1720. */
  1721. static void
  1722. fas216_std_done(FAS216_Info *info, struct scsi_cmnd *SCpnt, unsigned int result)
  1723. {
  1724. info->stats.fins += 1;
  1725. SCpnt->result = result << 16 | info->scsi.SCp.Message << 8 |
  1726. info->scsi.SCp.Status;
  1727. fas216_log_command(info, LOG_CONNECT, SCpnt,
  1728. "command complete, result=0x%08x", SCpnt->result);
  1729. /*
  1730. * If the driver detected an error, we're all done.
  1731. */
  1732. if (host_byte(SCpnt->result) != DID_OK ||
  1733. msg_byte(SCpnt->result) != COMMAND_COMPLETE)
  1734. goto done;
  1735. /*
  1736. * If the command returned CHECK_CONDITION or COMMAND_TERMINATED
  1737. * status, request the sense information.
  1738. */
  1739. if (status_byte(SCpnt->result) == CHECK_CONDITION ||
  1740. status_byte(SCpnt->result) == COMMAND_TERMINATED)
  1741. goto request_sense;
  1742. /*
  1743. * If the command did not complete with GOOD status,
  1744. * we are all done here.
  1745. */
  1746. if (status_byte(SCpnt->result) != GOOD)
  1747. goto done;
  1748. /*
  1749. * We have successfully completed a command. Make sure that
  1750. * we do not have any buffers left to transfer. The world
  1751. * is not perfect, and we seem to occasionally hit this.
  1752. * It can be indicative of a buggy driver, target or the upper
  1753. * levels of the SCSI code.
  1754. */
  1755. if (info->scsi.SCp.ptr) {
  1756. switch (SCpnt->cmnd[0]) {
  1757. case INQUIRY:
  1758. case START_STOP:
  1759. case MODE_SENSE:
  1760. break;
  1761. default:
  1762. printk(KERN_ERR "scsi%d.%c: incomplete data transfer "
  1763. "detected: res=%08X ptr=%p len=%X CDB: ",
  1764. info->host->host_no, '0' + SCpnt->device->id,
  1765. SCpnt->result, info->scsi.SCp.ptr,
  1766. info->scsi.SCp.this_residual);
  1767. __scsi_print_command(SCpnt->cmnd);
  1768. SCpnt->result &= ~(255 << 16);
  1769. SCpnt->result |= DID_BAD_TARGET << 16;
  1770. goto request_sense;
  1771. }
  1772. }
  1773. done:
  1774. if (SCpnt->scsi_done) {
  1775. SCpnt->scsi_done(SCpnt);
  1776. return;
  1777. }
  1778. panic("scsi%d.H: null scsi_done function in fas216_done",
  1779. info->host->host_no);
  1780. request_sense:
  1781. if (SCpnt->cmnd[0] == REQUEST_SENSE)
  1782. goto done;
  1783. scsi_eh_prep_cmnd(SCpnt, &info->ses, NULL, 0, ~0);
  1784. fas216_log_target(info, LOG_CONNECT, SCpnt->device->id,
  1785. "requesting sense");
  1786. init_SCp(SCpnt);
  1787. SCpnt->SCp.Message = 0;
  1788. SCpnt->SCp.Status = 0;
  1789. SCpnt->tag = 0;
  1790. SCpnt->host_scribble = (void *)fas216_rq_sns_done;
  1791. /*
  1792. * Place this command into the high priority "request
  1793. * sense" slot. This will be the very next command
  1794. * executed, unless a target connects to us.
  1795. */
  1796. if (info->reqSCpnt)
  1797. printk(KERN_WARNING "scsi%d.%c: losing request command\n",
  1798. info->host->host_no, '0' + SCpnt->device->id);
  1799. info->reqSCpnt = SCpnt;
  1800. }
  1801. /**
  1802. * fas216_done - complete processing for current command
  1803. * @info: interface that completed
  1804. * @result: driver byte of result
  1805. *
  1806. * Complete processing for current command
  1807. */
  1808. static void fas216_done(FAS216_Info *info, unsigned int result)
  1809. {
  1810. void (*fn)(FAS216_Info *, struct scsi_cmnd *, unsigned int);
  1811. struct scsi_cmnd *SCpnt;
  1812. unsigned long flags;
  1813. fas216_checkmagic(info);
  1814. if (!info->SCpnt)
  1815. goto no_command;
  1816. SCpnt = info->SCpnt;
  1817. info->SCpnt = NULL;
  1818. info->scsi.phase = PHASE_IDLE;
  1819. if (info->scsi.aborting) {
  1820. fas216_log(info, 0, "uncaught abort - returning DID_ABORT");
  1821. result = DID_ABORT;
  1822. info->scsi.aborting = 0;
  1823. }
  1824. /*
  1825. * Sanity check the completion - if we have zero bytes left
  1826. * to transfer, we should not have a valid pointer.
  1827. */
  1828. if (info->scsi.SCp.ptr && info->scsi.SCp.this_residual == 0) {
  1829. printk("scsi%d.%c: zero bytes left to transfer, but "
  1830. "buffer pointer still valid: ptr=%p len=%08x CDB: ",
  1831. info->host->host_no, '0' + SCpnt->device->id,
  1832. info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
  1833. info->scsi.SCp.ptr = NULL;
  1834. __scsi_print_command(SCpnt->cmnd);
  1835. }
  1836. /*
  1837. * Clear down this command as completed. If we need to request
  1838. * the sense information, fas216_kick will re-assert the busy
  1839. * status.
  1840. */
  1841. info->device[SCpnt->device->id].parity_check = 0;
  1842. clear_bit(SCpnt->device->id * 8 + SCpnt->device->lun, info->busyluns);
  1843. fn = (void (*)(FAS216_Info *, struct scsi_cmnd *, unsigned int))SCpnt->host_scribble;
  1844. fn(info, SCpnt, result);
  1845. if (info->scsi.irq) {
  1846. spin_lock_irqsave(&info->host_lock, flags);
  1847. if (info->scsi.phase == PHASE_IDLE)
  1848. fas216_kick(info);
  1849. spin_unlock_irqrestore(&info->host_lock, flags);
  1850. }
  1851. return;
  1852. no_command:
  1853. panic("scsi%d.H: null command in fas216_done",
  1854. info->host->host_no);
  1855. }
  1856. /**
  1857. * fas216_queue_command - queue a command for adapter to process.
  1858. * @SCpnt: Command to queue
  1859. * @done: done function to call once command is complete
  1860. *
  1861. * Queue a command for adapter to process.
  1862. * Returns: 0 on success, else error.
  1863. * Notes: io_request_lock is held, interrupts are disabled.
  1864. */
  1865. static int fas216_queue_command_lck(struct scsi_cmnd *SCpnt,
  1866. void (*done)(struct scsi_cmnd *))
  1867. {
  1868. FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
  1869. int result;
  1870. fas216_checkmagic(info);
  1871. fas216_log_command(info, LOG_CONNECT, SCpnt,
  1872. "received command (%p)", SCpnt);
  1873. SCpnt->scsi_done = done;
  1874. SCpnt->host_scribble = (void *)fas216_std_done;
  1875. SCpnt->result = 0;
  1876. init_SCp(SCpnt);
  1877. info->stats.queues += 1;
  1878. SCpnt->tag = 0;
  1879. spin_lock(&info->host_lock);
  1880. /*
  1881. * Add command into execute queue and let it complete under
  1882. * whatever scheme we're using.
  1883. */
  1884. result = !queue_add_cmd_ordered(&info->queues.issue, SCpnt);
  1885. /*
  1886. * If we successfully added the command,
  1887. * kick the interface to get it moving.
  1888. */
  1889. if (result == 0 && info->scsi.phase == PHASE_IDLE)
  1890. fas216_kick(info);
  1891. spin_unlock(&info->host_lock);
  1892. fas216_log_target(info, LOG_CONNECT, -1, "queue %s",
  1893. result ? "failure" : "success");
  1894. return result;
  1895. }
  1896. DEF_SCSI_QCMD(fas216_queue_command)
  1897. /**
  1898. * fas216_internal_done - trigger restart of a waiting thread in fas216_noqueue_command
  1899. * @SCpnt: Command to wake
  1900. *
  1901. * Trigger restart of a waiting thread in fas216_command
  1902. */
  1903. static void fas216_internal_done(struct scsi_cmnd *SCpnt)
  1904. {
  1905. FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
  1906. fas216_checkmagic(info);
  1907. info->internal_done = 1;
  1908. }
  1909. /**
  1910. * fas216_noqueue_command - process a command for the adapter.
  1911. * @SCpnt: Command to queue
  1912. *
  1913. * Queue a command for adapter to process.
  1914. * Returns: scsi result code.
  1915. * Notes: io_request_lock is held, interrupts are disabled.
  1916. */
  1917. static int fas216_noqueue_command_lck(struct scsi_cmnd *SCpnt,
  1918. void (*done)(struct scsi_cmnd *))
  1919. {
  1920. FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
  1921. fas216_checkmagic(info);
  1922. /*
  1923. * We should only be using this if we don't have an interrupt.
  1924. * Provide some "incentive" to use the queueing code.
  1925. */
  1926. BUG_ON(info->scsi.irq);
  1927. info->internal_done = 0;
  1928. fas216_queue_command_lck(SCpnt, fas216_internal_done);
  1929. /*
  1930. * This wastes time, since we can't return until the command is
  1931. * complete. We can't sleep either since we may get re-entered!
  1932. * However, we must re-enable interrupts, or else we'll be
  1933. * waiting forever.
  1934. */
  1935. spin_unlock_irq(info->host->host_lock);
  1936. while (!info->internal_done) {
  1937. /*
  1938. * If we don't have an IRQ, then we must poll the card for
  1939. * it's interrupt, and use that to call this driver's
  1940. * interrupt routine. That way, we keep the command
  1941. * progressing. Maybe we can add some intelligence here
  1942. * and go to sleep if we know that the device is going
  1943. * to be some time (eg, disconnected).
  1944. */
  1945. if (fas216_readb(info, REG_STAT) & STAT_INT) {
  1946. spin_lock_irq(info->host->host_lock);
  1947. fas216_intr(info);
  1948. spin_unlock_irq(info->host->host_lock);
  1949. }
  1950. }
  1951. spin_lock_irq(info->host->host_lock);
  1952. done(SCpnt);
  1953. return 0;
  1954. }
  1955. DEF_SCSI_QCMD(fas216_noqueue_command)
  1956. /*
  1957. * Error handler timeout function. Indicate that we timed out,
  1958. * and wake up any error handler process so it can continue.
  1959. */
  1960. static void fas216_eh_timer(unsigned long data)
  1961. {
  1962. FAS216_Info *info = (FAS216_Info *)data;
  1963. fas216_log(info, LOG_ERROR, "error handling timed out\n");
  1964. del_timer(&info->eh_timer);
  1965. if (info->rst_bus_status == 0)
  1966. info->rst_bus_status = -1;
  1967. if (info->rst_dev_status == 0)
  1968. info->rst_dev_status = -1;
  1969. wake_up(&info->eh_wait);
  1970. }
  1971. enum res_find {
  1972. res_failed, /* not found */
  1973. res_success, /* command on issue queue */
  1974. res_hw_abort /* command on disconnected dev */
  1975. };
  1976. /**
  1977. * fas216_do_abort - decide how to abort a command
  1978. * @SCpnt: command to abort
  1979. *
  1980. * Decide how to abort a command.
  1981. * Returns: abort status
  1982. */
  1983. static enum res_find fas216_find_command(FAS216_Info *info,
  1984. struct scsi_cmnd *SCpnt)
  1985. {
  1986. enum res_find res = res_failed;
  1987. if (queue_remove_cmd(&info->queues.issue, SCpnt)) {
  1988. /*
  1989. * The command was on the issue queue, and has not been
  1990. * issued yet. We can remove the command from the queue,
  1991. * and acknowledge the abort. Neither the device nor the
  1992. * interface know about the command.
  1993. */
  1994. printk("on issue queue ");
  1995. res = res_success;
  1996. } else if (queue_remove_cmd(&info->queues.disconnected, SCpnt)) {
  1997. /*
  1998. * The command was on the disconnected queue. We must
  1999. * reconnect with the device if possible, and send it
  2000. * an abort message.
  2001. */
  2002. printk("on disconnected queue ");
  2003. res = res_hw_abort;
  2004. } else if (info->SCpnt == SCpnt) {
  2005. printk("executing ");
  2006. switch (info->scsi.phase) {
  2007. /*
  2008. * If the interface is idle, and the command is 'disconnectable',
  2009. * then it is the same as on the disconnected queue.
  2010. */
  2011. case PHASE_IDLE:
  2012. if (info->scsi.disconnectable) {
  2013. info->scsi.disconnectable = 0;
  2014. info->SCpnt = NULL;
  2015. res = res_hw_abort;
  2016. }
  2017. break;
  2018. default:
  2019. break;
  2020. }
  2021. } else if (info->origSCpnt == SCpnt) {
  2022. /*
  2023. * The command will be executed next, but a command
  2024. * is currently using the interface. This is similar to
  2025. * being on the issue queue, except the busylun bit has
  2026. * been set.
  2027. */
  2028. info->origSCpnt = NULL;
  2029. clear_bit(SCpnt->device->id * 8 + SCpnt->device->lun, info->busyluns);
  2030. printk("waiting for execution ");
  2031. res = res_success;
  2032. } else
  2033. printk("unknown ");
  2034. return res;
  2035. }
  2036. /**
  2037. * fas216_eh_abort - abort this command
  2038. * @SCpnt: command to abort
  2039. *
  2040. * Abort this command.
  2041. * Returns: FAILED if unable to abort
  2042. * Notes: io_request_lock is taken, and irqs are disabled
  2043. */
  2044. int fas216_eh_abort(struct scsi_cmnd *SCpnt)
  2045. {
  2046. FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
  2047. int result = FAILED;
  2048. fas216_checkmagic(info);
  2049. info->stats.aborts += 1;
  2050. printk(KERN_WARNING "scsi%d: abort command ", info->host->host_no);
  2051. __scsi_print_command(SCpnt->cmnd);
  2052. print_debug_list();
  2053. fas216_dumpstate(info);
  2054. printk(KERN_WARNING "scsi%d: abort %p ", info->host->host_no, SCpnt);
  2055. switch (fas216_find_command(info, SCpnt)) {
  2056. /*
  2057. * We found the command, and cleared it out. Either
  2058. * the command is still known to be executing on the
  2059. * target, or the busylun bit is not set.
  2060. */
  2061. case res_success:
  2062. printk("success\n");
  2063. result = SUCCESS;
  2064. break;
  2065. /*
  2066. * We need to reconnect to the target and send it an
  2067. * ABORT or ABORT_TAG message. We can only do this
  2068. * if the bus is free.
  2069. */
  2070. case res_hw_abort:
  2071. /*
  2072. * We are unable to abort the command for some reason.
  2073. */
  2074. default:
  2075. case res_failed:
  2076. printk("failed\n");
  2077. break;
  2078. }
  2079. return result;
  2080. }
  2081. /**
  2082. * fas216_eh_device_reset - Reset the device associated with this command
  2083. * @SCpnt: command specifing device to reset
  2084. *
  2085. * Reset the device associated with this command.
  2086. * Returns: FAILED if unable to reset.
  2087. * Notes: We won't be re-entered, so we'll only have one device
  2088. * reset on the go at one time.
  2089. */
  2090. int fas216_eh_device_reset(struct scsi_cmnd *SCpnt)
  2091. {
  2092. FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
  2093. unsigned long flags;
  2094. int i, res = FAILED, target = SCpnt->device->id;
  2095. fas216_log(info, LOG_ERROR, "device reset for target %d", target);
  2096. spin_lock_irqsave(&info->host_lock, flags);
  2097. do {
  2098. /*
  2099. * If we are currently connected to a device, and
  2100. * it is the device we want to reset, there is
  2101. * nothing we can do here. Chances are it is stuck,
  2102. * and we need a bus reset.
  2103. */
  2104. if (info->SCpnt && !info->scsi.disconnectable &&
  2105. info->SCpnt->device->id == SCpnt->device->id)
  2106. break;
  2107. /*
  2108. * We're going to be resetting this device. Remove
  2109. * all pending commands from the driver. By doing
  2110. * so, we guarantee that we won't touch the command
  2111. * structures except to process the reset request.
  2112. */
  2113. queue_remove_all_target(&info->queues.issue, target);
  2114. queue_remove_all_target(&info->queues.disconnected, target);
  2115. if (info->origSCpnt && info->origSCpnt->device->id == target)
  2116. info->origSCpnt = NULL;
  2117. if (info->reqSCpnt && info->reqSCpnt->device->id == target)
  2118. info->reqSCpnt = NULL;
  2119. for (i = 0; i < 8; i++)
  2120. clear_bit(target * 8 + i, info->busyluns);
  2121. /*
  2122. * Hijack this SCSI command structure to send
  2123. * a bus device reset message to this device.
  2124. */
  2125. SCpnt->host_scribble = (void *)fas216_devicereset_done;
  2126. info->rst_dev_status = 0;
  2127. info->rstSCpnt = SCpnt;
  2128. if (info->scsi.phase == PHASE_IDLE)
  2129. fas216_kick(info);
  2130. mod_timer(&info->eh_timer, jiffies + 30 * HZ);
  2131. spin_unlock_irqrestore(&info->host_lock, flags);
  2132. /*
  2133. * Wait up to 30 seconds for the reset to complete.
  2134. */
  2135. wait_event(info->eh_wait, info->rst_dev_status);
  2136. del_timer_sync(&info->eh_timer);
  2137. spin_lock_irqsave(&info->host_lock, flags);
  2138. info->rstSCpnt = NULL;
  2139. if (info->rst_dev_status == 1)
  2140. res = SUCCESS;
  2141. } while (0);
  2142. SCpnt->host_scribble = NULL;
  2143. spin_unlock_irqrestore(&info->host_lock, flags);
  2144. fas216_log(info, LOG_ERROR, "device reset complete: %s\n",
  2145. res == SUCCESS ? "success" : "failed");
  2146. return res;
  2147. }
  2148. /**
  2149. * fas216_eh_bus_reset - Reset the bus associated with the command
  2150. * @SCpnt: command specifing bus to reset
  2151. *
  2152. * Reset the bus associated with the command.
  2153. * Returns: FAILED if unable to reset.
  2154. * Notes: Further commands are blocked.
  2155. */
  2156. int fas216_eh_bus_reset(struct scsi_cmnd *SCpnt)
  2157. {
  2158. FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
  2159. unsigned long flags;
  2160. struct scsi_device *SDpnt;
  2161. fas216_checkmagic(info);
  2162. fas216_log(info, LOG_ERROR, "resetting bus");
  2163. info->stats.bus_resets += 1;
  2164. spin_lock_irqsave(&info->host_lock, flags);
  2165. /*
  2166. * Stop all activity on this interface.
  2167. */
  2168. fas216_aborttransfer(info);
  2169. fas216_writeb(info, REG_CNTL3, info->scsi.cfg[2]);
  2170. /*
  2171. * Clear any pending interrupts.
  2172. */
  2173. while (fas216_readb(info, REG_STAT) & STAT_INT)
  2174. fas216_readb(info, REG_INST);
  2175. info->rst_bus_status = 0;
  2176. /*
  2177. * For each attached hard-reset device, clear out
  2178. * all command structures. Leave the running
  2179. * command in place.
  2180. */
  2181. shost_for_each_device(SDpnt, info->host) {
  2182. int i;
  2183. if (SDpnt->soft_reset)
  2184. continue;
  2185. queue_remove_all_target(&info->queues.issue, SDpnt->id);
  2186. queue_remove_all_target(&info->queues.disconnected, SDpnt->id);
  2187. if (info->origSCpnt && info->origSCpnt->device->id == SDpnt->id)
  2188. info->origSCpnt = NULL;
  2189. if (info->reqSCpnt && info->reqSCpnt->device->id == SDpnt->id)
  2190. info->reqSCpnt = NULL;
  2191. info->SCpnt = NULL;
  2192. for (i = 0; i < 8; i++)
  2193. clear_bit(SDpnt->id * 8 + i, info->busyluns);
  2194. }
  2195. info->scsi.phase = PHASE_IDLE;
  2196. /*
  2197. * Reset the SCSI bus. Device cleanup happens in
  2198. * the interrupt handler.
  2199. */
  2200. fas216_cmd(info, CMD_RESETSCSI);
  2201. mod_timer(&info->eh_timer, jiffies + HZ);
  2202. spin_unlock_irqrestore(&info->host_lock, flags);
  2203. /*
  2204. * Wait one second for the interrupt.
  2205. */
  2206. wait_event(info->eh_wait, info->rst_bus_status);
  2207. del_timer_sync(&info->eh_timer);
  2208. fas216_log(info, LOG_ERROR, "bus reset complete: %s\n",
  2209. info->rst_bus_status == 1 ? "success" : "failed");
  2210. return info->rst_bus_status == 1 ? SUCCESS : FAILED;
  2211. }
  2212. /**
  2213. * fas216_init_chip - Initialise FAS216 state after reset
  2214. * @info: state structure for interface
  2215. *
  2216. * Initialise FAS216 state after reset
  2217. */
  2218. static void fas216_init_chip(FAS216_Info *info)
  2219. {
  2220. unsigned int clock = ((info->ifcfg.clockrate - 1) / 5 + 1) & 7;
  2221. fas216_writeb(info, REG_CLKF, clock);
  2222. fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
  2223. fas216_writeb(info, REG_CNTL2, info->scsi.cfg[1]);
  2224. fas216_writeb(info, REG_CNTL3, info->scsi.cfg[2]);
  2225. fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
  2226. fas216_writeb(info, REG_SOF, 0);
  2227. fas216_writeb(info, REG_STP, info->scsi.async_stp);
  2228. fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
  2229. }
  2230. /**
  2231. * fas216_eh_host_reset - Reset the host associated with this command
  2232. * @SCpnt: command specifing host to reset
  2233. *
  2234. * Reset the host associated with this command.
  2235. * Returns: FAILED if unable to reset.
  2236. * Notes: io_request_lock is taken, and irqs are disabled
  2237. */
  2238. int fas216_eh_host_reset(struct scsi_cmnd *SCpnt)
  2239. {
  2240. FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
  2241. spin_lock_irq(info->host->host_lock);
  2242. fas216_checkmagic(info);
  2243. printk("scsi%d.%c: %s: resetting host\n",
  2244. info->host->host_no, '0' + SCpnt->device->id, __func__);
  2245. /*
  2246. * Reset the SCSI chip.
  2247. */
  2248. fas216_cmd(info, CMD_RESETCHIP);
  2249. /*
  2250. * Ugly ugly ugly!
  2251. * We need to release the host_lock and enable
  2252. * IRQs if we sleep, but we must relock and disable
  2253. * IRQs after the sleep.
  2254. */
  2255. spin_unlock_irq(info->host->host_lock);
  2256. msleep(50 * 1000/100);
  2257. spin_lock_irq(info->host->host_lock);
  2258. /*
  2259. * Release the SCSI reset.
  2260. */
  2261. fas216_cmd(info, CMD_NOP);
  2262. fas216_init_chip(info);
  2263. spin_unlock_irq(info->host->host_lock);
  2264. return SUCCESS;
  2265. }
  2266. #define TYPE_UNKNOWN 0
  2267. #define TYPE_NCR53C90 1
  2268. #define TYPE_NCR53C90A 2
  2269. #define TYPE_NCR53C9x 3
  2270. #define TYPE_Am53CF94 4
  2271. #define TYPE_EmFAS216 5
  2272. #define TYPE_QLFAS216 6
  2273. static char *chip_types[] = {
  2274. "unknown",
  2275. "NS NCR53C90",
  2276. "NS NCR53C90A",
  2277. "NS NCR53C9x",
  2278. "AMD Am53CF94",
  2279. "Emulex FAS216",
  2280. "QLogic FAS216"
  2281. };
  2282. static int fas216_detect_type(FAS216_Info *info)
  2283. {
  2284. int family, rev;
  2285. /*
  2286. * Reset the chip.
  2287. */
  2288. fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
  2289. udelay(50);
  2290. fas216_writeb(info, REG_CMD, CMD_NOP);
  2291. /*
  2292. * Check to see if control reg 2 is present.
  2293. */
  2294. fas216_writeb(info, REG_CNTL3, 0);
  2295. fas216_writeb(info, REG_CNTL2, CNTL2_S2FE);
  2296. /*
  2297. * If we are unable to read back control reg 2
  2298. * correctly, it is not present, and we have a
  2299. * NCR53C90.
  2300. */
  2301. if ((fas216_readb(info, REG_CNTL2) & (~0xe0)) != CNTL2_S2FE)
  2302. return TYPE_NCR53C90;
  2303. /*
  2304. * Now, check control register 3
  2305. */
  2306. fas216_writeb(info, REG_CNTL2, 0);
  2307. fas216_writeb(info, REG_CNTL3, 0);
  2308. fas216_writeb(info, REG_CNTL3, 5);
  2309. /*
  2310. * If we are unable to read the register back
  2311. * correctly, we have a NCR53C90A
  2312. */
  2313. if (fas216_readb(info, REG_CNTL3) != 5)
  2314. return TYPE_NCR53C90A;
  2315. /*
  2316. * Now read the ID from the chip.
  2317. */
  2318. fas216_writeb(info, REG_CNTL3, 0);
  2319. fas216_writeb(info, REG_CNTL3, CNTL3_ADIDCHK);
  2320. fas216_writeb(info, REG_CNTL3, 0);
  2321. fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
  2322. udelay(50);
  2323. fas216_writeb(info, REG_CMD, CMD_WITHDMA | CMD_NOP);
  2324. fas216_writeb(info, REG_CNTL2, CNTL2_ENF);
  2325. fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
  2326. udelay(50);
  2327. fas216_writeb(info, REG_CMD, CMD_NOP);
  2328. rev = fas216_readb(info, REG_ID);
  2329. family = rev >> 3;
  2330. rev &= 7;
  2331. switch (family) {
  2332. case 0x01:
  2333. if (rev == 4)
  2334. return TYPE_Am53CF94;
  2335. break;
  2336. case 0x02:
  2337. switch (rev) {
  2338. case 2:
  2339. return TYPE_EmFAS216;
  2340. case 3:
  2341. return TYPE_QLFAS216;
  2342. }
  2343. break;
  2344. default:
  2345. break;
  2346. }
  2347. printk("family %x rev %x\n", family, rev);
  2348. return TYPE_NCR53C9x;
  2349. }
  2350. /**
  2351. * fas216_reset_state - Initialise driver internal state
  2352. * @info: state to initialise
  2353. *
  2354. * Initialise driver internal state
  2355. */
  2356. static void fas216_reset_state(FAS216_Info *info)
  2357. {
  2358. int i;
  2359. fas216_checkmagic(info);
  2360. fas216_bus_reset(info);
  2361. /*
  2362. * Clear out all stale info in our state structure
  2363. */
  2364. memset(info->busyluns, 0, sizeof(info->busyluns));
  2365. info->scsi.disconnectable = 0;
  2366. info->scsi.aborting = 0;
  2367. for (i = 0; i < 8; i++) {
  2368. info->device[i].parity_enabled = 0;
  2369. info->device[i].parity_check = 1;
  2370. }
  2371. /*
  2372. * Drain all commands on disconnected queue
  2373. */
  2374. while (queue_remove(&info->queues.disconnected) != NULL);
  2375. /*
  2376. * Remove executing commands.
  2377. */
  2378. info->SCpnt = NULL;
  2379. info->reqSCpnt = NULL;
  2380. info->rstSCpnt = NULL;
  2381. info->origSCpnt = NULL;
  2382. }
  2383. /**
  2384. * fas216_init - initialise FAS/NCR/AMD SCSI structures.
  2385. * @host: a driver-specific filled-out structure
  2386. *
  2387. * Initialise FAS/NCR/AMD SCSI structures.
  2388. * Returns: 0 on success
  2389. */
  2390. int fas216_init(struct Scsi_Host *host)
  2391. {
  2392. FAS216_Info *info = (FAS216_Info *)host->hostdata;
  2393. info->magic_start = MAGIC;
  2394. info->magic_end = MAGIC;
  2395. info->host = host;
  2396. info->scsi.cfg[0] = host->this_id | CNTL1_PERE;
  2397. info->scsi.cfg[1] = CNTL2_ENF | CNTL2_S2FE;
  2398. info->scsi.cfg[2] = info->ifcfg.cntl3 |
  2399. CNTL3_ADIDCHK | CNTL3_QTAG | CNTL3_G2CB | CNTL3_LBTM;
  2400. info->scsi.async_stp = fas216_syncperiod(info, info->ifcfg.asyncperiod);
  2401. info->rst_dev_status = -1;
  2402. info->rst_bus_status = -1;
  2403. init_waitqueue_head(&info->eh_wait);
  2404. init_timer(&info->eh_timer);
  2405. info->eh_timer.data = (unsigned long)info;
  2406. info->eh_timer.function = fas216_eh_timer;
  2407. spin_lock_init(&info->host_lock);
  2408. memset(&info->stats, 0, sizeof(info->stats));
  2409. msgqueue_initialise(&info->scsi.msgs);
  2410. if (!queue_initialise(&info->queues.issue))
  2411. return -ENOMEM;
  2412. if (!queue_initialise(&info->queues.disconnected)) {
  2413. queue_free(&info->queues.issue);
  2414. return -ENOMEM;
  2415. }
  2416. return 0;
  2417. }
  2418. /**
  2419. * fas216_add - initialise FAS/NCR/AMD SCSI ic.
  2420. * @host: a driver-specific filled-out structure
  2421. * @dev: parent device
  2422. *
  2423. * Initialise FAS/NCR/AMD SCSI ic.
  2424. * Returns: 0 on success
  2425. */
  2426. int fas216_add(struct Scsi_Host *host, struct device *dev)
  2427. {
  2428. FAS216_Info *info = (FAS216_Info *)host->hostdata;
  2429. int type, ret;
  2430. if (info->ifcfg.clockrate <= 10 || info->ifcfg.clockrate > 40) {
  2431. printk(KERN_CRIT "fas216: invalid clock rate %u MHz\n",
  2432. info->ifcfg.clockrate);
  2433. return -EINVAL;
  2434. }
  2435. fas216_reset_state(info);
  2436. type = fas216_detect_type(info);
  2437. info->scsi.type = chip_types[type];
  2438. udelay(300);
  2439. /*
  2440. * Initialise the chip correctly.
  2441. */
  2442. fas216_init_chip(info);
  2443. /*
  2444. * Reset the SCSI bus. We don't want to see
  2445. * the resulting reset interrupt, so mask it
  2446. * out.
  2447. */
  2448. fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0] | CNTL1_DISR);
  2449. fas216_writeb(info, REG_CMD, CMD_RESETSCSI);
  2450. /*
  2451. * scsi standard says wait 250ms
  2452. */
  2453. spin_unlock_irq(info->host->host_lock);
  2454. msleep(100*1000/100);
  2455. spin_lock_irq(info->host->host_lock);
  2456. fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
  2457. fas216_readb(info, REG_INST);
  2458. fas216_checkmagic(info);
  2459. ret = scsi_add_host(host, dev);
  2460. if (ret)
  2461. fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
  2462. else
  2463. scsi_scan_host(host);
  2464. return ret;
  2465. }
  2466. void fas216_remove(struct Scsi_Host *host)
  2467. {
  2468. FAS216_Info *info = (FAS216_Info *)host->hostdata;
  2469. fas216_checkmagic(info);
  2470. scsi_remove_host(host);
  2471. fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
  2472. scsi_host_put(host);
  2473. }
  2474. /**
  2475. * fas216_release - release all resources for FAS/NCR/AMD SCSI ic.
  2476. * @host: a driver-specific filled-out structure
  2477. *
  2478. * release all resources and put everything to bed for FAS/NCR/AMD SCSI ic.
  2479. */
  2480. void fas216_release(struct Scsi_Host *host)
  2481. {
  2482. FAS216_Info *info = (FAS216_Info *)host->hostdata;
  2483. queue_free(&info->queues.disconnected);
  2484. queue_free(&info->queues.issue);
  2485. }
  2486. int fas216_print_host(FAS216_Info *info, char *buffer)
  2487. {
  2488. return sprintf(buffer,
  2489. "\n"
  2490. "Chip : %s\n"
  2491. " Address: 0x%p\n"
  2492. " IRQ : %d\n"
  2493. " DMA : %d\n",
  2494. info->scsi.type, info->scsi.io_base,
  2495. info->scsi.irq, info->scsi.dma);
  2496. }
  2497. int fas216_print_stats(FAS216_Info *info, char *buffer)
  2498. {
  2499. char *p = buffer;
  2500. p += sprintf(p, "\n"
  2501. "Command Statistics:\n"
  2502. " Queued : %u\n"
  2503. " Issued : %u\n"
  2504. " Completed : %u\n"
  2505. " Reads : %u\n"
  2506. " Writes : %u\n"
  2507. " Others : %u\n"
  2508. " Disconnects: %u\n"
  2509. " Aborts : %u\n"
  2510. " Bus resets : %u\n"
  2511. " Host resets: %u\n",
  2512. info->stats.queues, info->stats.removes,
  2513. info->stats.fins, info->stats.reads,
  2514. info->stats.writes, info->stats.miscs,
  2515. info->stats.disconnects, info->stats.aborts,
  2516. info->stats.bus_resets, info->stats.host_resets);
  2517. return p - buffer;
  2518. }
  2519. int fas216_print_devices(FAS216_Info *info, char *buffer)
  2520. {
  2521. struct fas216_device *dev;
  2522. struct scsi_device *scd;
  2523. char *p = buffer;
  2524. p += sprintf(p, "Device/Lun TaggedQ Parity Sync\n");
  2525. shost_for_each_device(scd, info->host) {
  2526. dev = &info->device[scd->id];
  2527. p += sprintf(p, " %d/%d ", scd->id, scd->lun);
  2528. if (scd->tagged_supported)
  2529. p += sprintf(p, "%3sabled(%3d) ",
  2530. scd->simple_tags ? "en" : "dis",
  2531. scd->current_tag);
  2532. else
  2533. p += sprintf(p, "unsupported ");
  2534. p += sprintf(p, "%3sabled ", dev->parity_enabled ? "en" : "dis");
  2535. if (dev->sof)
  2536. p += sprintf(p, "offset %d, %d ns\n",
  2537. dev->sof, dev->period * 4);
  2538. else
  2539. p += sprintf(p, "async\n");
  2540. }
  2541. return p - buffer;
  2542. }
  2543. EXPORT_SYMBOL(fas216_init);
  2544. EXPORT_SYMBOL(fas216_add);
  2545. EXPORT_SYMBOL(fas216_queue_command);
  2546. EXPORT_SYMBOL(fas216_noqueue_command);
  2547. EXPORT_SYMBOL(fas216_intr);
  2548. EXPORT_SYMBOL(fas216_remove);
  2549. EXPORT_SYMBOL(fas216_release);
  2550. EXPORT_SYMBOL(fas216_eh_abort);
  2551. EXPORT_SYMBOL(fas216_eh_device_reset);
  2552. EXPORT_SYMBOL(fas216_eh_bus_reset);
  2553. EXPORT_SYMBOL(fas216_eh_host_reset);
  2554. EXPORT_SYMBOL(fas216_print_host);
  2555. EXPORT_SYMBOL(fas216_print_stats);
  2556. EXPORT_SYMBOL(fas216_print_devices);
  2557. MODULE_AUTHOR("Russell King");
  2558. MODULE_DESCRIPTION("Generic FAS216/NCR53C9x driver core");
  2559. MODULE_LICENSE("GPL");