esp_scsi.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794
  1. /* esp_scsi.c: ESP SCSI driver.
  2. *
  3. * Copyright (C) 2007 David S. Miller (davem@davemloft.net)
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/types.h>
  7. #include <linux/slab.h>
  8. #include <linux/delay.h>
  9. #include <linux/list.h>
  10. #include <linux/completion.h>
  11. #include <linux/kallsyms.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/init.h>
  15. #include <linux/irqreturn.h>
  16. #include <asm/irq.h>
  17. #include <asm/io.h>
  18. #include <asm/dma.h>
  19. #include <scsi/scsi.h>
  20. #include <scsi/scsi_host.h>
  21. #include <scsi/scsi_cmnd.h>
  22. #include <scsi/scsi_device.h>
  23. #include <scsi/scsi_tcq.h>
  24. #include <scsi/scsi_dbg.h>
  25. #include <scsi/scsi_transport_spi.h>
  26. #include "esp_scsi.h"
  27. #define DRV_MODULE_NAME "esp"
  28. #define PFX DRV_MODULE_NAME ": "
  29. #define DRV_VERSION "2.000"
  30. #define DRV_MODULE_RELDATE "April 19, 2007"
  31. /* SCSI bus reset settle time in seconds. */
  32. static int esp_bus_reset_settle = 3;
  33. static u32 esp_debug;
  34. #define ESP_DEBUG_INTR 0x00000001
  35. #define ESP_DEBUG_SCSICMD 0x00000002
  36. #define ESP_DEBUG_RESET 0x00000004
  37. #define ESP_DEBUG_MSGIN 0x00000008
  38. #define ESP_DEBUG_MSGOUT 0x00000010
  39. #define ESP_DEBUG_CMDDONE 0x00000020
  40. #define ESP_DEBUG_DISCONNECT 0x00000040
  41. #define ESP_DEBUG_DATASTART 0x00000080
  42. #define ESP_DEBUG_DATADONE 0x00000100
  43. #define ESP_DEBUG_RECONNECT 0x00000200
  44. #define ESP_DEBUG_AUTOSENSE 0x00000400
  45. #define ESP_DEBUG_EVENT 0x00000800
  46. #define ESP_DEBUG_COMMAND 0x00001000
  47. #define esp_log_intr(f, a...) \
  48. do { if (esp_debug & ESP_DEBUG_INTR) \
  49. shost_printk(KERN_DEBUG, esp->host, f, ## a); \
  50. } while (0)
  51. #define esp_log_reset(f, a...) \
  52. do { if (esp_debug & ESP_DEBUG_RESET) \
  53. shost_printk(KERN_DEBUG, esp->host, f, ## a); \
  54. } while (0)
  55. #define esp_log_msgin(f, a...) \
  56. do { if (esp_debug & ESP_DEBUG_MSGIN) \
  57. shost_printk(KERN_DEBUG, esp->host, f, ## a); \
  58. } while (0)
  59. #define esp_log_msgout(f, a...) \
  60. do { if (esp_debug & ESP_DEBUG_MSGOUT) \
  61. shost_printk(KERN_DEBUG, esp->host, f, ## a); \
  62. } while (0)
  63. #define esp_log_cmddone(f, a...) \
  64. do { if (esp_debug & ESP_DEBUG_CMDDONE) \
  65. shost_printk(KERN_DEBUG, esp->host, f, ## a); \
  66. } while (0)
  67. #define esp_log_disconnect(f, a...) \
  68. do { if (esp_debug & ESP_DEBUG_DISCONNECT) \
  69. shost_printk(KERN_DEBUG, esp->host, f, ## a); \
  70. } while (0)
  71. #define esp_log_datastart(f, a...) \
  72. do { if (esp_debug & ESP_DEBUG_DATASTART) \
  73. shost_printk(KERN_DEBUG, esp->host, f, ## a); \
  74. } while (0)
  75. #define esp_log_datadone(f, a...) \
  76. do { if (esp_debug & ESP_DEBUG_DATADONE) \
  77. shost_printk(KERN_DEBUG, esp->host, f, ## a); \
  78. } while (0)
  79. #define esp_log_reconnect(f, a...) \
  80. do { if (esp_debug & ESP_DEBUG_RECONNECT) \
  81. shost_printk(KERN_DEBUG, esp->host, f, ## a); \
  82. } while (0)
  83. #define esp_log_autosense(f, a...) \
  84. do { if (esp_debug & ESP_DEBUG_AUTOSENSE) \
  85. shost_printk(KERN_DEBUG, esp->host, f, ## a); \
  86. } while (0)
  87. #define esp_log_event(f, a...) \
  88. do { if (esp_debug & ESP_DEBUG_EVENT) \
  89. shost_printk(KERN_DEBUG, esp->host, f, ## a); \
  90. } while (0)
  91. #define esp_log_command(f, a...) \
  92. do { if (esp_debug & ESP_DEBUG_COMMAND) \
  93. shost_printk(KERN_DEBUG, esp->host, f, ## a); \
  94. } while (0)
  95. #define esp_read8(REG) esp->ops->esp_read8(esp, REG)
  96. #define esp_write8(VAL,REG) esp->ops->esp_write8(esp, VAL, REG)
  97. static void esp_log_fill_regs(struct esp *esp,
  98. struct esp_event_ent *p)
  99. {
  100. p->sreg = esp->sreg;
  101. p->seqreg = esp->seqreg;
  102. p->sreg2 = esp->sreg2;
  103. p->ireg = esp->ireg;
  104. p->select_state = esp->select_state;
  105. p->event = esp->event;
  106. }
  107. void scsi_esp_cmd(struct esp *esp, u8 val)
  108. {
  109. struct esp_event_ent *p;
  110. int idx = esp->esp_event_cur;
  111. p = &esp->esp_event_log[idx];
  112. p->type = ESP_EVENT_TYPE_CMD;
  113. p->val = val;
  114. esp_log_fill_regs(esp, p);
  115. esp->esp_event_cur = (idx + 1) & (ESP_EVENT_LOG_SZ - 1);
  116. esp_log_command("cmd[%02x]\n", val);
  117. esp_write8(val, ESP_CMD);
  118. }
  119. EXPORT_SYMBOL(scsi_esp_cmd);
  120. static void esp_send_dma_cmd(struct esp *esp, int len, int max_len, int cmd)
  121. {
  122. if (esp->flags & ESP_FLAG_USE_FIFO) {
  123. int i;
  124. scsi_esp_cmd(esp, ESP_CMD_FLUSH);
  125. for (i = 0; i < len; i++)
  126. esp_write8(esp->command_block[i], ESP_FDATA);
  127. scsi_esp_cmd(esp, cmd);
  128. } else {
  129. if (esp->rev == FASHME)
  130. scsi_esp_cmd(esp, ESP_CMD_FLUSH);
  131. cmd |= ESP_CMD_DMA;
  132. esp->ops->send_dma_cmd(esp, esp->command_block_dma,
  133. len, max_len, 0, cmd);
  134. }
  135. }
  136. static void esp_event(struct esp *esp, u8 val)
  137. {
  138. struct esp_event_ent *p;
  139. int idx = esp->esp_event_cur;
  140. p = &esp->esp_event_log[idx];
  141. p->type = ESP_EVENT_TYPE_EVENT;
  142. p->val = val;
  143. esp_log_fill_regs(esp, p);
  144. esp->esp_event_cur = (idx + 1) & (ESP_EVENT_LOG_SZ - 1);
  145. esp->event = val;
  146. }
  147. static void esp_dump_cmd_log(struct esp *esp)
  148. {
  149. int idx = esp->esp_event_cur;
  150. int stop = idx;
  151. shost_printk(KERN_INFO, esp->host, "Dumping command log\n");
  152. do {
  153. struct esp_event_ent *p = &esp->esp_event_log[idx];
  154. shost_printk(KERN_INFO, esp->host,
  155. "ent[%d] %s val[%02x] sreg[%02x] seqreg[%02x] "
  156. "sreg2[%02x] ireg[%02x] ss[%02x] event[%02x]\n",
  157. idx,
  158. p->type == ESP_EVENT_TYPE_CMD ? "CMD" : "EVENT",
  159. p->val, p->sreg, p->seqreg,
  160. p->sreg2, p->ireg, p->select_state, p->event);
  161. idx = (idx + 1) & (ESP_EVENT_LOG_SZ - 1);
  162. } while (idx != stop);
  163. }
  164. static void esp_flush_fifo(struct esp *esp)
  165. {
  166. scsi_esp_cmd(esp, ESP_CMD_FLUSH);
  167. if (esp->rev == ESP236) {
  168. int lim = 1000;
  169. while (esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES) {
  170. if (--lim == 0) {
  171. shost_printk(KERN_ALERT, esp->host,
  172. "ESP_FF_BYTES will not clear!\n");
  173. break;
  174. }
  175. udelay(1);
  176. }
  177. }
  178. }
  179. static void hme_read_fifo(struct esp *esp)
  180. {
  181. int fcnt = esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES;
  182. int idx = 0;
  183. while (fcnt--) {
  184. esp->fifo[idx++] = esp_read8(ESP_FDATA);
  185. esp->fifo[idx++] = esp_read8(ESP_FDATA);
  186. }
  187. if (esp->sreg2 & ESP_STAT2_F1BYTE) {
  188. esp_write8(0, ESP_FDATA);
  189. esp->fifo[idx++] = esp_read8(ESP_FDATA);
  190. scsi_esp_cmd(esp, ESP_CMD_FLUSH);
  191. }
  192. esp->fifo_cnt = idx;
  193. }
  194. static void esp_set_all_config3(struct esp *esp, u8 val)
  195. {
  196. int i;
  197. for (i = 0; i < ESP_MAX_TARGET; i++)
  198. esp->target[i].esp_config3 = val;
  199. }
  200. /* Reset the ESP chip, _not_ the SCSI bus. */
  201. static void esp_reset_esp(struct esp *esp)
  202. {
  203. u8 family_code, version;
  204. /* Now reset the ESP chip */
  205. scsi_esp_cmd(esp, ESP_CMD_RC);
  206. scsi_esp_cmd(esp, ESP_CMD_NULL | ESP_CMD_DMA);
  207. if (esp->rev == FAST)
  208. esp_write8(ESP_CONFIG2_FENAB, ESP_CFG2);
  209. scsi_esp_cmd(esp, ESP_CMD_NULL | ESP_CMD_DMA);
  210. /* This is the only point at which it is reliable to read
  211. * the ID-code for a fast ESP chip variants.
  212. */
  213. esp->max_period = ((35 * esp->ccycle) / 1000);
  214. if (esp->rev == FAST) {
  215. version = esp_read8(ESP_UID);
  216. family_code = (version & 0xf8) >> 3;
  217. if (family_code == 0x02)
  218. esp->rev = FAS236;
  219. else if (family_code == 0x0a)
  220. esp->rev = FASHME; /* Version is usually '5'. */
  221. else
  222. esp->rev = FAS100A;
  223. esp->min_period = ((4 * esp->ccycle) / 1000);
  224. } else {
  225. esp->min_period = ((5 * esp->ccycle) / 1000);
  226. }
  227. if (esp->rev == FAS236) {
  228. /*
  229. * The AM53c974 chip returns the same ID as FAS236;
  230. * try to configure glitch eater.
  231. */
  232. u8 config4 = ESP_CONFIG4_GE1;
  233. esp_write8(config4, ESP_CFG4);
  234. config4 = esp_read8(ESP_CFG4);
  235. if (config4 & ESP_CONFIG4_GE1) {
  236. esp->rev = PCSCSI;
  237. esp_write8(esp->config4, ESP_CFG4);
  238. }
  239. }
  240. esp->max_period = (esp->max_period + 3)>>2;
  241. esp->min_period = (esp->min_period + 3)>>2;
  242. esp_write8(esp->config1, ESP_CFG1);
  243. switch (esp->rev) {
  244. case ESP100:
  245. /* nothing to do */
  246. break;
  247. case ESP100A:
  248. esp_write8(esp->config2, ESP_CFG2);
  249. break;
  250. case ESP236:
  251. /* Slow 236 */
  252. esp_write8(esp->config2, ESP_CFG2);
  253. esp->prev_cfg3 = esp->target[0].esp_config3;
  254. esp_write8(esp->prev_cfg3, ESP_CFG3);
  255. break;
  256. case FASHME:
  257. esp->config2 |= (ESP_CONFIG2_HME32 | ESP_CONFIG2_HMEFENAB);
  258. /* fallthrough... */
  259. case FAS236:
  260. case PCSCSI:
  261. /* Fast 236, AM53c974 or HME */
  262. esp_write8(esp->config2, ESP_CFG2);
  263. if (esp->rev == FASHME) {
  264. u8 cfg3 = esp->target[0].esp_config3;
  265. cfg3 |= ESP_CONFIG3_FCLOCK | ESP_CONFIG3_OBPUSH;
  266. if (esp->scsi_id >= 8)
  267. cfg3 |= ESP_CONFIG3_IDBIT3;
  268. esp_set_all_config3(esp, cfg3);
  269. } else {
  270. u32 cfg3 = esp->target[0].esp_config3;
  271. cfg3 |= ESP_CONFIG3_FCLK;
  272. esp_set_all_config3(esp, cfg3);
  273. }
  274. esp->prev_cfg3 = esp->target[0].esp_config3;
  275. esp_write8(esp->prev_cfg3, ESP_CFG3);
  276. if (esp->rev == FASHME) {
  277. esp->radelay = 80;
  278. } else {
  279. if (esp->flags & ESP_FLAG_DIFFERENTIAL)
  280. esp->radelay = 0;
  281. else
  282. esp->radelay = 96;
  283. }
  284. break;
  285. case FAS100A:
  286. /* Fast 100a */
  287. esp_write8(esp->config2, ESP_CFG2);
  288. esp_set_all_config3(esp,
  289. (esp->target[0].esp_config3 |
  290. ESP_CONFIG3_FCLOCK));
  291. esp->prev_cfg3 = esp->target[0].esp_config3;
  292. esp_write8(esp->prev_cfg3, ESP_CFG3);
  293. esp->radelay = 32;
  294. break;
  295. default:
  296. break;
  297. }
  298. /* Reload the configuration registers */
  299. esp_write8(esp->cfact, ESP_CFACT);
  300. esp->prev_stp = 0;
  301. esp_write8(esp->prev_stp, ESP_STP);
  302. esp->prev_soff = 0;
  303. esp_write8(esp->prev_soff, ESP_SOFF);
  304. esp_write8(esp->neg_defp, ESP_TIMEO);
  305. /* Eat any bitrot in the chip */
  306. esp_read8(ESP_INTRPT);
  307. udelay(100);
  308. }
  309. static void esp_map_dma(struct esp *esp, struct scsi_cmnd *cmd)
  310. {
  311. struct esp_cmd_priv *spriv = ESP_CMD_PRIV(cmd);
  312. struct scatterlist *sg = scsi_sglist(cmd);
  313. int dir = cmd->sc_data_direction;
  314. int total, i;
  315. if (dir == DMA_NONE)
  316. return;
  317. spriv->u.num_sg = esp->ops->map_sg(esp, sg, scsi_sg_count(cmd), dir);
  318. spriv->cur_residue = sg_dma_len(sg);
  319. spriv->cur_sg = sg;
  320. total = 0;
  321. for (i = 0; i < spriv->u.num_sg; i++)
  322. total += sg_dma_len(&sg[i]);
  323. spriv->tot_residue = total;
  324. }
  325. static dma_addr_t esp_cur_dma_addr(struct esp_cmd_entry *ent,
  326. struct scsi_cmnd *cmd)
  327. {
  328. struct esp_cmd_priv *p = ESP_CMD_PRIV(cmd);
  329. if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
  330. return ent->sense_dma +
  331. (ent->sense_ptr - cmd->sense_buffer);
  332. }
  333. return sg_dma_address(p->cur_sg) +
  334. (sg_dma_len(p->cur_sg) -
  335. p->cur_residue);
  336. }
  337. static unsigned int esp_cur_dma_len(struct esp_cmd_entry *ent,
  338. struct scsi_cmnd *cmd)
  339. {
  340. struct esp_cmd_priv *p = ESP_CMD_PRIV(cmd);
  341. if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
  342. return SCSI_SENSE_BUFFERSIZE -
  343. (ent->sense_ptr - cmd->sense_buffer);
  344. }
  345. return p->cur_residue;
  346. }
  347. static void esp_advance_dma(struct esp *esp, struct esp_cmd_entry *ent,
  348. struct scsi_cmnd *cmd, unsigned int len)
  349. {
  350. struct esp_cmd_priv *p = ESP_CMD_PRIV(cmd);
  351. if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
  352. ent->sense_ptr += len;
  353. return;
  354. }
  355. p->cur_residue -= len;
  356. p->tot_residue -= len;
  357. if (p->cur_residue < 0 || p->tot_residue < 0) {
  358. shost_printk(KERN_ERR, esp->host,
  359. "Data transfer overflow.\n");
  360. shost_printk(KERN_ERR, esp->host,
  361. "cur_residue[%d] tot_residue[%d] len[%u]\n",
  362. p->cur_residue, p->tot_residue, len);
  363. p->cur_residue = 0;
  364. p->tot_residue = 0;
  365. }
  366. if (!p->cur_residue && p->tot_residue) {
  367. p->cur_sg++;
  368. p->cur_residue = sg_dma_len(p->cur_sg);
  369. }
  370. }
  371. static void esp_unmap_dma(struct esp *esp, struct scsi_cmnd *cmd)
  372. {
  373. struct esp_cmd_priv *spriv = ESP_CMD_PRIV(cmd);
  374. int dir = cmd->sc_data_direction;
  375. if (dir == DMA_NONE)
  376. return;
  377. esp->ops->unmap_sg(esp, scsi_sglist(cmd), spriv->u.num_sg, dir);
  378. }
  379. static void esp_save_pointers(struct esp *esp, struct esp_cmd_entry *ent)
  380. {
  381. struct scsi_cmnd *cmd = ent->cmd;
  382. struct esp_cmd_priv *spriv = ESP_CMD_PRIV(cmd);
  383. if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
  384. ent->saved_sense_ptr = ent->sense_ptr;
  385. return;
  386. }
  387. ent->saved_cur_residue = spriv->cur_residue;
  388. ent->saved_cur_sg = spriv->cur_sg;
  389. ent->saved_tot_residue = spriv->tot_residue;
  390. }
  391. static void esp_restore_pointers(struct esp *esp, struct esp_cmd_entry *ent)
  392. {
  393. struct scsi_cmnd *cmd = ent->cmd;
  394. struct esp_cmd_priv *spriv = ESP_CMD_PRIV(cmd);
  395. if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
  396. ent->sense_ptr = ent->saved_sense_ptr;
  397. return;
  398. }
  399. spriv->cur_residue = ent->saved_cur_residue;
  400. spriv->cur_sg = ent->saved_cur_sg;
  401. spriv->tot_residue = ent->saved_tot_residue;
  402. }
  403. static void esp_check_command_len(struct esp *esp, struct scsi_cmnd *cmd)
  404. {
  405. if (cmd->cmd_len == 6 ||
  406. cmd->cmd_len == 10 ||
  407. cmd->cmd_len == 12) {
  408. esp->flags &= ~ESP_FLAG_DOING_SLOWCMD;
  409. } else {
  410. esp->flags |= ESP_FLAG_DOING_SLOWCMD;
  411. }
  412. }
  413. static void esp_write_tgt_config3(struct esp *esp, int tgt)
  414. {
  415. if (esp->rev > ESP100A) {
  416. u8 val = esp->target[tgt].esp_config3;
  417. if (val != esp->prev_cfg3) {
  418. esp->prev_cfg3 = val;
  419. esp_write8(val, ESP_CFG3);
  420. }
  421. }
  422. }
  423. static void esp_write_tgt_sync(struct esp *esp, int tgt)
  424. {
  425. u8 off = esp->target[tgt].esp_offset;
  426. u8 per = esp->target[tgt].esp_period;
  427. if (off != esp->prev_soff) {
  428. esp->prev_soff = off;
  429. esp_write8(off, ESP_SOFF);
  430. }
  431. if (per != esp->prev_stp) {
  432. esp->prev_stp = per;
  433. esp_write8(per, ESP_STP);
  434. }
  435. }
  436. static u32 esp_dma_length_limit(struct esp *esp, u32 dma_addr, u32 dma_len)
  437. {
  438. if (esp->rev == FASHME) {
  439. /* Arbitrary segment boundaries, 24-bit counts. */
  440. if (dma_len > (1U << 24))
  441. dma_len = (1U << 24);
  442. } else {
  443. u32 base, end;
  444. /* ESP chip limits other variants by 16-bits of transfer
  445. * count. Actually on FAS100A and FAS236 we could get
  446. * 24-bits of transfer count by enabling ESP_CONFIG2_FENAB
  447. * in the ESP_CFG2 register but that causes other unwanted
  448. * changes so we don't use it currently.
  449. */
  450. if (dma_len > (1U << 16))
  451. dma_len = (1U << 16);
  452. /* All of the DMA variants hooked up to these chips
  453. * cannot handle crossing a 24-bit address boundary.
  454. */
  455. base = dma_addr & ((1U << 24) - 1U);
  456. end = base + dma_len;
  457. if (end > (1U << 24))
  458. end = (1U <<24);
  459. dma_len = end - base;
  460. }
  461. return dma_len;
  462. }
  463. static int esp_need_to_nego_wide(struct esp_target_data *tp)
  464. {
  465. struct scsi_target *target = tp->starget;
  466. return spi_width(target) != tp->nego_goal_width;
  467. }
  468. static int esp_need_to_nego_sync(struct esp_target_data *tp)
  469. {
  470. struct scsi_target *target = tp->starget;
  471. /* When offset is zero, period is "don't care". */
  472. if (!spi_offset(target) && !tp->nego_goal_offset)
  473. return 0;
  474. if (spi_offset(target) == tp->nego_goal_offset &&
  475. spi_period(target) == tp->nego_goal_period)
  476. return 0;
  477. return 1;
  478. }
  479. static int esp_alloc_lun_tag(struct esp_cmd_entry *ent,
  480. struct esp_lun_data *lp)
  481. {
  482. if (!ent->orig_tag[0]) {
  483. /* Non-tagged, slot already taken? */
  484. if (lp->non_tagged_cmd)
  485. return -EBUSY;
  486. if (lp->hold) {
  487. /* We are being held by active tagged
  488. * commands.
  489. */
  490. if (lp->num_tagged)
  491. return -EBUSY;
  492. /* Tagged commands completed, we can unplug
  493. * the queue and run this untagged command.
  494. */
  495. lp->hold = 0;
  496. } else if (lp->num_tagged) {
  497. /* Plug the queue until num_tagged decreases
  498. * to zero in esp_free_lun_tag.
  499. */
  500. lp->hold = 1;
  501. return -EBUSY;
  502. }
  503. lp->non_tagged_cmd = ent;
  504. return 0;
  505. }
  506. /* Tagged command. Check that it isn't blocked by a non-tagged one. */
  507. if (lp->non_tagged_cmd || lp->hold)
  508. return -EBUSY;
  509. BUG_ON(lp->tagged_cmds[ent->orig_tag[1]]);
  510. lp->tagged_cmds[ent->orig_tag[1]] = ent;
  511. lp->num_tagged++;
  512. return 0;
  513. }
  514. static void esp_free_lun_tag(struct esp_cmd_entry *ent,
  515. struct esp_lun_data *lp)
  516. {
  517. if (ent->orig_tag[0]) {
  518. BUG_ON(lp->tagged_cmds[ent->orig_tag[1]] != ent);
  519. lp->tagged_cmds[ent->orig_tag[1]] = NULL;
  520. lp->num_tagged--;
  521. } else {
  522. BUG_ON(lp->non_tagged_cmd != ent);
  523. lp->non_tagged_cmd = NULL;
  524. }
  525. }
  526. /* When a contingent allegiance conditon is created, we force feed a
  527. * REQUEST_SENSE command to the device to fetch the sense data. I
  528. * tried many other schemes, relying on the scsi error handling layer
  529. * to send out the REQUEST_SENSE automatically, but this was difficult
  530. * to get right especially in the presence of applications like smartd
  531. * which use SG_IO to send out their own REQUEST_SENSE commands.
  532. */
  533. static void esp_autosense(struct esp *esp, struct esp_cmd_entry *ent)
  534. {
  535. struct scsi_cmnd *cmd = ent->cmd;
  536. struct scsi_device *dev = cmd->device;
  537. int tgt, lun;
  538. u8 *p, val;
  539. tgt = dev->id;
  540. lun = dev->lun;
  541. if (!ent->sense_ptr) {
  542. esp_log_autosense("Doing auto-sense for tgt[%d] lun[%d]\n",
  543. tgt, lun);
  544. ent->sense_ptr = cmd->sense_buffer;
  545. ent->sense_dma = esp->ops->map_single(esp,
  546. ent->sense_ptr,
  547. SCSI_SENSE_BUFFERSIZE,
  548. DMA_FROM_DEVICE);
  549. }
  550. ent->saved_sense_ptr = ent->sense_ptr;
  551. esp->active_cmd = ent;
  552. p = esp->command_block;
  553. esp->msg_out_len = 0;
  554. *p++ = IDENTIFY(0, lun);
  555. *p++ = REQUEST_SENSE;
  556. *p++ = ((dev->scsi_level <= SCSI_2) ?
  557. (lun << 5) : 0);
  558. *p++ = 0;
  559. *p++ = 0;
  560. *p++ = SCSI_SENSE_BUFFERSIZE;
  561. *p++ = 0;
  562. esp->select_state = ESP_SELECT_BASIC;
  563. val = tgt;
  564. if (esp->rev == FASHME)
  565. val |= ESP_BUSID_RESELID | ESP_BUSID_CTR32BIT;
  566. esp_write8(val, ESP_BUSID);
  567. esp_write_tgt_sync(esp, tgt);
  568. esp_write_tgt_config3(esp, tgt);
  569. val = (p - esp->command_block);
  570. esp_send_dma_cmd(esp, val, 16, ESP_CMD_SELA);
  571. }
  572. static struct esp_cmd_entry *find_and_prep_issuable_command(struct esp *esp)
  573. {
  574. struct esp_cmd_entry *ent;
  575. list_for_each_entry(ent, &esp->queued_cmds, list) {
  576. struct scsi_cmnd *cmd = ent->cmd;
  577. struct scsi_device *dev = cmd->device;
  578. struct esp_lun_data *lp = dev->hostdata;
  579. if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
  580. ent->tag[0] = 0;
  581. ent->tag[1] = 0;
  582. return ent;
  583. }
  584. if (!spi_populate_tag_msg(&ent->tag[0], cmd)) {
  585. ent->tag[0] = 0;
  586. ent->tag[1] = 0;
  587. }
  588. ent->orig_tag[0] = ent->tag[0];
  589. ent->orig_tag[1] = ent->tag[1];
  590. if (esp_alloc_lun_tag(ent, lp) < 0)
  591. continue;
  592. return ent;
  593. }
  594. return NULL;
  595. }
  596. static void esp_maybe_execute_command(struct esp *esp)
  597. {
  598. struct esp_target_data *tp;
  599. struct esp_lun_data *lp;
  600. struct scsi_device *dev;
  601. struct scsi_cmnd *cmd;
  602. struct esp_cmd_entry *ent;
  603. int tgt, lun, i;
  604. u32 val, start_cmd;
  605. u8 *p;
  606. if (esp->active_cmd ||
  607. (esp->flags & ESP_FLAG_RESETTING))
  608. return;
  609. ent = find_and_prep_issuable_command(esp);
  610. if (!ent)
  611. return;
  612. if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
  613. esp_autosense(esp, ent);
  614. return;
  615. }
  616. cmd = ent->cmd;
  617. dev = cmd->device;
  618. tgt = dev->id;
  619. lun = dev->lun;
  620. tp = &esp->target[tgt];
  621. lp = dev->hostdata;
  622. list_move(&ent->list, &esp->active_cmds);
  623. esp->active_cmd = ent;
  624. esp_map_dma(esp, cmd);
  625. esp_save_pointers(esp, ent);
  626. esp_check_command_len(esp, cmd);
  627. p = esp->command_block;
  628. esp->msg_out_len = 0;
  629. if (tp->flags & ESP_TGT_CHECK_NEGO) {
  630. /* Need to negotiate. If the target is broken
  631. * go for synchronous transfers and non-wide.
  632. */
  633. if (tp->flags & ESP_TGT_BROKEN) {
  634. tp->flags &= ~ESP_TGT_DISCONNECT;
  635. tp->nego_goal_period = 0;
  636. tp->nego_goal_offset = 0;
  637. tp->nego_goal_width = 0;
  638. tp->nego_goal_tags = 0;
  639. }
  640. /* If the settings are not changing, skip this. */
  641. if (spi_width(tp->starget) == tp->nego_goal_width &&
  642. spi_period(tp->starget) == tp->nego_goal_period &&
  643. spi_offset(tp->starget) == tp->nego_goal_offset) {
  644. tp->flags &= ~ESP_TGT_CHECK_NEGO;
  645. goto build_identify;
  646. }
  647. if (esp->rev == FASHME && esp_need_to_nego_wide(tp)) {
  648. esp->msg_out_len =
  649. spi_populate_width_msg(&esp->msg_out[0],
  650. (tp->nego_goal_width ?
  651. 1 : 0));
  652. tp->flags |= ESP_TGT_NEGO_WIDE;
  653. } else if (esp_need_to_nego_sync(tp)) {
  654. esp->msg_out_len =
  655. spi_populate_sync_msg(&esp->msg_out[0],
  656. tp->nego_goal_period,
  657. tp->nego_goal_offset);
  658. tp->flags |= ESP_TGT_NEGO_SYNC;
  659. } else {
  660. tp->flags &= ~ESP_TGT_CHECK_NEGO;
  661. }
  662. /* Process it like a slow command. */
  663. if (tp->flags & (ESP_TGT_NEGO_WIDE | ESP_TGT_NEGO_SYNC))
  664. esp->flags |= ESP_FLAG_DOING_SLOWCMD;
  665. }
  666. build_identify:
  667. /* If we don't have a lun-data struct yet, we're probing
  668. * so do not disconnect. Also, do not disconnect unless
  669. * we have a tag on this command.
  670. */
  671. if (lp && (tp->flags & ESP_TGT_DISCONNECT) && ent->tag[0])
  672. *p++ = IDENTIFY(1, lun);
  673. else
  674. *p++ = IDENTIFY(0, lun);
  675. if (ent->tag[0] && esp->rev == ESP100) {
  676. /* ESP100 lacks select w/atn3 command, use select
  677. * and stop instead.
  678. */
  679. esp->flags |= ESP_FLAG_DOING_SLOWCMD;
  680. }
  681. if (!(esp->flags & ESP_FLAG_DOING_SLOWCMD)) {
  682. start_cmd = ESP_CMD_SELA;
  683. if (ent->tag[0]) {
  684. *p++ = ent->tag[0];
  685. *p++ = ent->tag[1];
  686. start_cmd = ESP_CMD_SA3;
  687. }
  688. for (i = 0; i < cmd->cmd_len; i++)
  689. *p++ = cmd->cmnd[i];
  690. esp->select_state = ESP_SELECT_BASIC;
  691. } else {
  692. esp->cmd_bytes_left = cmd->cmd_len;
  693. esp->cmd_bytes_ptr = &cmd->cmnd[0];
  694. if (ent->tag[0]) {
  695. for (i = esp->msg_out_len - 1;
  696. i >= 0; i--)
  697. esp->msg_out[i + 2] = esp->msg_out[i];
  698. esp->msg_out[0] = ent->tag[0];
  699. esp->msg_out[1] = ent->tag[1];
  700. esp->msg_out_len += 2;
  701. }
  702. start_cmd = ESP_CMD_SELAS;
  703. esp->select_state = ESP_SELECT_MSGOUT;
  704. }
  705. val = tgt;
  706. if (esp->rev == FASHME)
  707. val |= ESP_BUSID_RESELID | ESP_BUSID_CTR32BIT;
  708. esp_write8(val, ESP_BUSID);
  709. esp_write_tgt_sync(esp, tgt);
  710. esp_write_tgt_config3(esp, tgt);
  711. val = (p - esp->command_block);
  712. if (esp_debug & ESP_DEBUG_SCSICMD) {
  713. printk("ESP: tgt[%d] lun[%d] scsi_cmd [ ", tgt, lun);
  714. for (i = 0; i < cmd->cmd_len; i++)
  715. printk("%02x ", cmd->cmnd[i]);
  716. printk("]\n");
  717. }
  718. esp_send_dma_cmd(esp, val, 16, start_cmd);
  719. }
  720. static struct esp_cmd_entry *esp_get_ent(struct esp *esp)
  721. {
  722. struct list_head *head = &esp->esp_cmd_pool;
  723. struct esp_cmd_entry *ret;
  724. if (list_empty(head)) {
  725. ret = kzalloc(sizeof(struct esp_cmd_entry), GFP_ATOMIC);
  726. } else {
  727. ret = list_entry(head->next, struct esp_cmd_entry, list);
  728. list_del(&ret->list);
  729. memset(ret, 0, sizeof(*ret));
  730. }
  731. return ret;
  732. }
  733. static void esp_put_ent(struct esp *esp, struct esp_cmd_entry *ent)
  734. {
  735. list_add(&ent->list, &esp->esp_cmd_pool);
  736. }
  737. static void esp_cmd_is_done(struct esp *esp, struct esp_cmd_entry *ent,
  738. struct scsi_cmnd *cmd, unsigned int result)
  739. {
  740. struct scsi_device *dev = cmd->device;
  741. int tgt = dev->id;
  742. int lun = dev->lun;
  743. esp->active_cmd = NULL;
  744. esp_unmap_dma(esp, cmd);
  745. esp_free_lun_tag(ent, dev->hostdata);
  746. cmd->result = result;
  747. if (ent->eh_done) {
  748. complete(ent->eh_done);
  749. ent->eh_done = NULL;
  750. }
  751. if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
  752. esp->ops->unmap_single(esp, ent->sense_dma,
  753. SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
  754. ent->sense_ptr = NULL;
  755. /* Restore the message/status bytes to what we actually
  756. * saw originally. Also, report that we are providing
  757. * the sense data.
  758. */
  759. cmd->result = ((DRIVER_SENSE << 24) |
  760. (DID_OK << 16) |
  761. (COMMAND_COMPLETE << 8) |
  762. (SAM_STAT_CHECK_CONDITION << 0));
  763. ent->flags &= ~ESP_CMD_FLAG_AUTOSENSE;
  764. if (esp_debug & ESP_DEBUG_AUTOSENSE) {
  765. int i;
  766. printk("esp%d: tgt[%d] lun[%d] AUTO SENSE[ ",
  767. esp->host->unique_id, tgt, lun);
  768. for (i = 0; i < 18; i++)
  769. printk("%02x ", cmd->sense_buffer[i]);
  770. printk("]\n");
  771. }
  772. }
  773. cmd->scsi_done(cmd);
  774. list_del(&ent->list);
  775. esp_put_ent(esp, ent);
  776. esp_maybe_execute_command(esp);
  777. }
  778. static unsigned int compose_result(unsigned int status, unsigned int message,
  779. unsigned int driver_code)
  780. {
  781. return (status | (message << 8) | (driver_code << 16));
  782. }
  783. static void esp_event_queue_full(struct esp *esp, struct esp_cmd_entry *ent)
  784. {
  785. struct scsi_device *dev = ent->cmd->device;
  786. struct esp_lun_data *lp = dev->hostdata;
  787. scsi_track_queue_full(dev, lp->num_tagged - 1);
  788. }
  789. static int esp_queuecommand_lck(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
  790. {
  791. struct scsi_device *dev = cmd->device;
  792. struct esp *esp = shost_priv(dev->host);
  793. struct esp_cmd_priv *spriv;
  794. struct esp_cmd_entry *ent;
  795. ent = esp_get_ent(esp);
  796. if (!ent)
  797. return SCSI_MLQUEUE_HOST_BUSY;
  798. ent->cmd = cmd;
  799. cmd->scsi_done = done;
  800. spriv = ESP_CMD_PRIV(cmd);
  801. spriv->u.dma_addr = ~(dma_addr_t)0x0;
  802. list_add_tail(&ent->list, &esp->queued_cmds);
  803. esp_maybe_execute_command(esp);
  804. return 0;
  805. }
  806. static DEF_SCSI_QCMD(esp_queuecommand)
  807. static int esp_check_gross_error(struct esp *esp)
  808. {
  809. if (esp->sreg & ESP_STAT_SPAM) {
  810. /* Gross Error, could be one of:
  811. * - top of fifo overwritten
  812. * - top of command register overwritten
  813. * - DMA programmed with wrong direction
  814. * - improper phase change
  815. */
  816. shost_printk(KERN_ERR, esp->host,
  817. "Gross error sreg[%02x]\n", esp->sreg);
  818. /* XXX Reset the chip. XXX */
  819. return 1;
  820. }
  821. return 0;
  822. }
  823. static int esp_check_spur_intr(struct esp *esp)
  824. {
  825. switch (esp->rev) {
  826. case ESP100:
  827. case ESP100A:
  828. /* The interrupt pending bit of the status register cannot
  829. * be trusted on these revisions.
  830. */
  831. esp->sreg &= ~ESP_STAT_INTR;
  832. break;
  833. default:
  834. if (!(esp->sreg & ESP_STAT_INTR)) {
  835. if (esp->ireg & ESP_INTR_SR)
  836. return 1;
  837. /* If the DMA is indicating interrupt pending and the
  838. * ESP is not, the only possibility is a DMA error.
  839. */
  840. if (!esp->ops->dma_error(esp)) {
  841. shost_printk(KERN_ERR, esp->host,
  842. "Spurious irq, sreg=%02x.\n",
  843. esp->sreg);
  844. return -1;
  845. }
  846. shost_printk(KERN_ERR, esp->host, "DMA error\n");
  847. /* XXX Reset the chip. XXX */
  848. return -1;
  849. }
  850. break;
  851. }
  852. return 0;
  853. }
  854. static void esp_schedule_reset(struct esp *esp)
  855. {
  856. esp_log_reset("esp_schedule_reset() from %pf\n",
  857. __builtin_return_address(0));
  858. esp->flags |= ESP_FLAG_RESETTING;
  859. esp_event(esp, ESP_EVENT_RESET);
  860. }
  861. /* In order to avoid having to add a special half-reconnected state
  862. * into the driver we just sit here and poll through the rest of
  863. * the reselection process to get the tag message bytes.
  864. */
  865. static struct esp_cmd_entry *esp_reconnect_with_tag(struct esp *esp,
  866. struct esp_lun_data *lp)
  867. {
  868. struct esp_cmd_entry *ent;
  869. int i;
  870. if (!lp->num_tagged) {
  871. shost_printk(KERN_ERR, esp->host,
  872. "Reconnect w/num_tagged==0\n");
  873. return NULL;
  874. }
  875. esp_log_reconnect("reconnect tag, ");
  876. for (i = 0; i < ESP_QUICKIRQ_LIMIT; i++) {
  877. if (esp->ops->irq_pending(esp))
  878. break;
  879. }
  880. if (i == ESP_QUICKIRQ_LIMIT) {
  881. shost_printk(KERN_ERR, esp->host,
  882. "Reconnect IRQ1 timeout\n");
  883. return NULL;
  884. }
  885. esp->sreg = esp_read8(ESP_STATUS);
  886. esp->ireg = esp_read8(ESP_INTRPT);
  887. esp_log_reconnect("IRQ(%d:%x:%x), ",
  888. i, esp->ireg, esp->sreg);
  889. if (esp->ireg & ESP_INTR_DC) {
  890. shost_printk(KERN_ERR, esp->host,
  891. "Reconnect, got disconnect.\n");
  892. return NULL;
  893. }
  894. if ((esp->sreg & ESP_STAT_PMASK) != ESP_MIP) {
  895. shost_printk(KERN_ERR, esp->host,
  896. "Reconnect, not MIP sreg[%02x].\n", esp->sreg);
  897. return NULL;
  898. }
  899. /* DMA in the tag bytes... */
  900. esp->command_block[0] = 0xff;
  901. esp->command_block[1] = 0xff;
  902. esp->ops->send_dma_cmd(esp, esp->command_block_dma,
  903. 2, 2, 1, ESP_CMD_DMA | ESP_CMD_TI);
  904. /* ACK the message. */
  905. scsi_esp_cmd(esp, ESP_CMD_MOK);
  906. for (i = 0; i < ESP_RESELECT_TAG_LIMIT; i++) {
  907. if (esp->ops->irq_pending(esp)) {
  908. esp->sreg = esp_read8(ESP_STATUS);
  909. esp->ireg = esp_read8(ESP_INTRPT);
  910. if (esp->ireg & ESP_INTR_FDONE)
  911. break;
  912. }
  913. udelay(1);
  914. }
  915. if (i == ESP_RESELECT_TAG_LIMIT) {
  916. shost_printk(KERN_ERR, esp->host, "Reconnect IRQ2 timeout\n");
  917. return NULL;
  918. }
  919. esp->ops->dma_drain(esp);
  920. esp->ops->dma_invalidate(esp);
  921. esp_log_reconnect("IRQ2(%d:%x:%x) tag[%x:%x]\n",
  922. i, esp->ireg, esp->sreg,
  923. esp->command_block[0],
  924. esp->command_block[1]);
  925. if (esp->command_block[0] < SIMPLE_QUEUE_TAG ||
  926. esp->command_block[0] > ORDERED_QUEUE_TAG) {
  927. shost_printk(KERN_ERR, esp->host,
  928. "Reconnect, bad tag type %02x.\n",
  929. esp->command_block[0]);
  930. return NULL;
  931. }
  932. ent = lp->tagged_cmds[esp->command_block[1]];
  933. if (!ent) {
  934. shost_printk(KERN_ERR, esp->host,
  935. "Reconnect, no entry for tag %02x.\n",
  936. esp->command_block[1]);
  937. return NULL;
  938. }
  939. return ent;
  940. }
  941. static int esp_reconnect(struct esp *esp)
  942. {
  943. struct esp_cmd_entry *ent;
  944. struct esp_target_data *tp;
  945. struct esp_lun_data *lp;
  946. struct scsi_device *dev;
  947. int target, lun;
  948. BUG_ON(esp->active_cmd);
  949. if (esp->rev == FASHME) {
  950. /* FASHME puts the target and lun numbers directly
  951. * into the fifo.
  952. */
  953. target = esp->fifo[0];
  954. lun = esp->fifo[1] & 0x7;
  955. } else {
  956. u8 bits = esp_read8(ESP_FDATA);
  957. /* Older chips put the lun directly into the fifo, but
  958. * the target is given as a sample of the arbitration
  959. * lines on the bus at reselection time. So we should
  960. * see the ID of the ESP and the one reconnecting target
  961. * set in the bitmap.
  962. */
  963. if (!(bits & esp->scsi_id_mask))
  964. goto do_reset;
  965. bits &= ~esp->scsi_id_mask;
  966. if (!bits || (bits & (bits - 1)))
  967. goto do_reset;
  968. target = ffs(bits) - 1;
  969. lun = (esp_read8(ESP_FDATA) & 0x7);
  970. scsi_esp_cmd(esp, ESP_CMD_FLUSH);
  971. if (esp->rev == ESP100) {
  972. u8 ireg = esp_read8(ESP_INTRPT);
  973. /* This chip has a bug during reselection that can
  974. * cause a spurious illegal-command interrupt, which
  975. * we simply ACK here. Another possibility is a bus
  976. * reset so we must check for that.
  977. */
  978. if (ireg & ESP_INTR_SR)
  979. goto do_reset;
  980. }
  981. scsi_esp_cmd(esp, ESP_CMD_NULL);
  982. }
  983. esp_write_tgt_sync(esp, target);
  984. esp_write_tgt_config3(esp, target);
  985. scsi_esp_cmd(esp, ESP_CMD_MOK);
  986. if (esp->rev == FASHME)
  987. esp_write8(target | ESP_BUSID_RESELID | ESP_BUSID_CTR32BIT,
  988. ESP_BUSID);
  989. tp = &esp->target[target];
  990. dev = __scsi_device_lookup_by_target(tp->starget, lun);
  991. if (!dev) {
  992. shost_printk(KERN_ERR, esp->host,
  993. "Reconnect, no lp tgt[%u] lun[%u]\n",
  994. target, lun);
  995. goto do_reset;
  996. }
  997. lp = dev->hostdata;
  998. ent = lp->non_tagged_cmd;
  999. if (!ent) {
  1000. ent = esp_reconnect_with_tag(esp, lp);
  1001. if (!ent)
  1002. goto do_reset;
  1003. }
  1004. esp->active_cmd = ent;
  1005. esp_event(esp, ESP_EVENT_CHECK_PHASE);
  1006. esp_restore_pointers(esp, ent);
  1007. esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
  1008. return 1;
  1009. do_reset:
  1010. esp_schedule_reset(esp);
  1011. return 0;
  1012. }
  1013. static int esp_finish_select(struct esp *esp)
  1014. {
  1015. struct esp_cmd_entry *ent;
  1016. struct scsi_cmnd *cmd;
  1017. /* No longer selecting. */
  1018. esp->select_state = ESP_SELECT_NONE;
  1019. esp->seqreg = esp_read8(ESP_SSTEP) & ESP_STEP_VBITS;
  1020. ent = esp->active_cmd;
  1021. cmd = ent->cmd;
  1022. if (esp->ops->dma_error(esp)) {
  1023. /* If we see a DMA error during or as a result of selection,
  1024. * all bets are off.
  1025. */
  1026. esp_schedule_reset(esp);
  1027. esp_cmd_is_done(esp, ent, cmd, (DID_ERROR << 16));
  1028. return 0;
  1029. }
  1030. esp->ops->dma_invalidate(esp);
  1031. if (esp->ireg == (ESP_INTR_RSEL | ESP_INTR_FDONE)) {
  1032. struct esp_target_data *tp = &esp->target[cmd->device->id];
  1033. /* Carefully back out of the selection attempt. Release
  1034. * resources (such as DMA mapping & TAG) and reset state (such
  1035. * as message out and command delivery variables).
  1036. */
  1037. if (!(ent->flags & ESP_CMD_FLAG_AUTOSENSE)) {
  1038. esp_unmap_dma(esp, cmd);
  1039. esp_free_lun_tag(ent, cmd->device->hostdata);
  1040. tp->flags &= ~(ESP_TGT_NEGO_SYNC | ESP_TGT_NEGO_WIDE);
  1041. esp->flags &= ~ESP_FLAG_DOING_SLOWCMD;
  1042. esp->cmd_bytes_ptr = NULL;
  1043. esp->cmd_bytes_left = 0;
  1044. } else {
  1045. esp->ops->unmap_single(esp, ent->sense_dma,
  1046. SCSI_SENSE_BUFFERSIZE,
  1047. DMA_FROM_DEVICE);
  1048. ent->sense_ptr = NULL;
  1049. }
  1050. /* Now that the state is unwound properly, put back onto
  1051. * the issue queue. This command is no longer active.
  1052. */
  1053. list_move(&ent->list, &esp->queued_cmds);
  1054. esp->active_cmd = NULL;
  1055. /* Return value ignored by caller, it directly invokes
  1056. * esp_reconnect().
  1057. */
  1058. return 0;
  1059. }
  1060. if (esp->ireg == ESP_INTR_DC) {
  1061. struct scsi_device *dev = cmd->device;
  1062. /* Disconnect. Make sure we re-negotiate sync and
  1063. * wide parameters if this target starts responding
  1064. * again in the future.
  1065. */
  1066. esp->target[dev->id].flags |= ESP_TGT_CHECK_NEGO;
  1067. scsi_esp_cmd(esp, ESP_CMD_ESEL);
  1068. esp_cmd_is_done(esp, ent, cmd, (DID_BAD_TARGET << 16));
  1069. return 1;
  1070. }
  1071. if (esp->ireg == (ESP_INTR_FDONE | ESP_INTR_BSERV)) {
  1072. /* Selection successful. On pre-FAST chips we have
  1073. * to do a NOP and possibly clean out the FIFO.
  1074. */
  1075. if (esp->rev <= ESP236) {
  1076. int fcnt = esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES;
  1077. scsi_esp_cmd(esp, ESP_CMD_NULL);
  1078. if (!fcnt &&
  1079. (!esp->prev_soff ||
  1080. ((esp->sreg & ESP_STAT_PMASK) != ESP_DIP)))
  1081. esp_flush_fifo(esp);
  1082. }
  1083. /* If we are doing a slow command, negotiation, etc.
  1084. * we'll do the right thing as we transition to the
  1085. * next phase.
  1086. */
  1087. esp_event(esp, ESP_EVENT_CHECK_PHASE);
  1088. return 0;
  1089. }
  1090. shost_printk(KERN_INFO, esp->host,
  1091. "Unexpected selection completion ireg[%x]\n", esp->ireg);
  1092. esp_schedule_reset(esp);
  1093. return 0;
  1094. }
  1095. static int esp_data_bytes_sent(struct esp *esp, struct esp_cmd_entry *ent,
  1096. struct scsi_cmnd *cmd)
  1097. {
  1098. int fifo_cnt, ecount, bytes_sent, flush_fifo;
  1099. fifo_cnt = esp_read8(ESP_FFLAGS) & ESP_FF_FBYTES;
  1100. if (esp->prev_cfg3 & ESP_CONFIG3_EWIDE)
  1101. fifo_cnt <<= 1;
  1102. ecount = 0;
  1103. if (!(esp->sreg & ESP_STAT_TCNT)) {
  1104. ecount = ((unsigned int)esp_read8(ESP_TCLOW) |
  1105. (((unsigned int)esp_read8(ESP_TCMED)) << 8));
  1106. if (esp->rev == FASHME)
  1107. ecount |= ((unsigned int)esp_read8(FAS_RLO)) << 16;
  1108. if (esp->rev == PCSCSI && (esp->config2 & ESP_CONFIG2_FENAB))
  1109. ecount |= ((unsigned int)esp_read8(ESP_TCHI)) << 16;
  1110. }
  1111. bytes_sent = esp->data_dma_len;
  1112. bytes_sent -= ecount;
  1113. bytes_sent -= esp->send_cmd_residual;
  1114. /*
  1115. * The am53c974 has a DMA 'pecularity'. The doc states:
  1116. * In some odd byte conditions, one residual byte will
  1117. * be left in the SCSI FIFO, and the FIFO Flags will
  1118. * never count to '0 '. When this happens, the residual
  1119. * byte should be retrieved via PIO following completion
  1120. * of the BLAST operation.
  1121. */
  1122. if (fifo_cnt == 1 && ent->flags & ESP_CMD_FLAG_RESIDUAL) {
  1123. size_t count = 1;
  1124. size_t offset = bytes_sent;
  1125. u8 bval = esp_read8(ESP_FDATA);
  1126. if (ent->flags & ESP_CMD_FLAG_AUTOSENSE)
  1127. ent->sense_ptr[bytes_sent] = bval;
  1128. else {
  1129. struct esp_cmd_priv *p = ESP_CMD_PRIV(cmd);
  1130. u8 *ptr;
  1131. ptr = scsi_kmap_atomic_sg(p->cur_sg, p->u.num_sg,
  1132. &offset, &count);
  1133. if (likely(ptr)) {
  1134. *(ptr + offset) = bval;
  1135. scsi_kunmap_atomic_sg(ptr);
  1136. }
  1137. }
  1138. bytes_sent += fifo_cnt;
  1139. ent->flags &= ~ESP_CMD_FLAG_RESIDUAL;
  1140. }
  1141. if (!(ent->flags & ESP_CMD_FLAG_WRITE))
  1142. bytes_sent -= fifo_cnt;
  1143. flush_fifo = 0;
  1144. if (!esp->prev_soff) {
  1145. /* Synchronous data transfer, always flush fifo. */
  1146. flush_fifo = 1;
  1147. } else {
  1148. if (esp->rev == ESP100) {
  1149. u32 fflags, phase;
  1150. /* ESP100 has a chip bug where in the synchronous data
  1151. * phase it can mistake a final long REQ pulse from the
  1152. * target as an extra data byte. Fun.
  1153. *
  1154. * To detect this case we resample the status register
  1155. * and fifo flags. If we're still in a data phase and
  1156. * we see spurious chunks in the fifo, we return error
  1157. * to the caller which should reset and set things up
  1158. * such that we only try future transfers to this
  1159. * target in synchronous mode.
  1160. */
  1161. esp->sreg = esp_read8(ESP_STATUS);
  1162. phase = esp->sreg & ESP_STAT_PMASK;
  1163. fflags = esp_read8(ESP_FFLAGS);
  1164. if ((phase == ESP_DOP &&
  1165. (fflags & ESP_FF_ONOTZERO)) ||
  1166. (phase == ESP_DIP &&
  1167. (fflags & ESP_FF_FBYTES)))
  1168. return -1;
  1169. }
  1170. if (!(ent->flags & ESP_CMD_FLAG_WRITE))
  1171. flush_fifo = 1;
  1172. }
  1173. if (flush_fifo)
  1174. esp_flush_fifo(esp);
  1175. return bytes_sent;
  1176. }
  1177. static void esp_setsync(struct esp *esp, struct esp_target_data *tp,
  1178. u8 scsi_period, u8 scsi_offset,
  1179. u8 esp_stp, u8 esp_soff)
  1180. {
  1181. spi_period(tp->starget) = scsi_period;
  1182. spi_offset(tp->starget) = scsi_offset;
  1183. spi_width(tp->starget) = (tp->flags & ESP_TGT_WIDE) ? 1 : 0;
  1184. if (esp_soff) {
  1185. esp_stp &= 0x1f;
  1186. esp_soff |= esp->radelay;
  1187. if (esp->rev >= FAS236) {
  1188. u8 bit = ESP_CONFIG3_FSCSI;
  1189. if (esp->rev >= FAS100A)
  1190. bit = ESP_CONFIG3_FAST;
  1191. if (scsi_period < 50) {
  1192. if (esp->rev == FASHME)
  1193. esp_soff &= ~esp->radelay;
  1194. tp->esp_config3 |= bit;
  1195. } else {
  1196. tp->esp_config3 &= ~bit;
  1197. }
  1198. esp->prev_cfg3 = tp->esp_config3;
  1199. esp_write8(esp->prev_cfg3, ESP_CFG3);
  1200. }
  1201. }
  1202. tp->esp_period = esp->prev_stp = esp_stp;
  1203. tp->esp_offset = esp->prev_soff = esp_soff;
  1204. esp_write8(esp_soff, ESP_SOFF);
  1205. esp_write8(esp_stp, ESP_STP);
  1206. tp->flags &= ~(ESP_TGT_NEGO_SYNC | ESP_TGT_CHECK_NEGO);
  1207. spi_display_xfer_agreement(tp->starget);
  1208. }
  1209. static void esp_msgin_reject(struct esp *esp)
  1210. {
  1211. struct esp_cmd_entry *ent = esp->active_cmd;
  1212. struct scsi_cmnd *cmd = ent->cmd;
  1213. struct esp_target_data *tp;
  1214. int tgt;
  1215. tgt = cmd->device->id;
  1216. tp = &esp->target[tgt];
  1217. if (tp->flags & ESP_TGT_NEGO_WIDE) {
  1218. tp->flags &= ~(ESP_TGT_NEGO_WIDE | ESP_TGT_WIDE);
  1219. if (!esp_need_to_nego_sync(tp)) {
  1220. tp->flags &= ~ESP_TGT_CHECK_NEGO;
  1221. scsi_esp_cmd(esp, ESP_CMD_RATN);
  1222. } else {
  1223. esp->msg_out_len =
  1224. spi_populate_sync_msg(&esp->msg_out[0],
  1225. tp->nego_goal_period,
  1226. tp->nego_goal_offset);
  1227. tp->flags |= ESP_TGT_NEGO_SYNC;
  1228. scsi_esp_cmd(esp, ESP_CMD_SATN);
  1229. }
  1230. return;
  1231. }
  1232. if (tp->flags & ESP_TGT_NEGO_SYNC) {
  1233. tp->flags &= ~(ESP_TGT_NEGO_SYNC | ESP_TGT_CHECK_NEGO);
  1234. tp->esp_period = 0;
  1235. tp->esp_offset = 0;
  1236. esp_setsync(esp, tp, 0, 0, 0, 0);
  1237. scsi_esp_cmd(esp, ESP_CMD_RATN);
  1238. return;
  1239. }
  1240. shost_printk(KERN_INFO, esp->host, "Unexpected MESSAGE REJECT\n");
  1241. esp_schedule_reset(esp);
  1242. }
  1243. static void esp_msgin_sdtr(struct esp *esp, struct esp_target_data *tp)
  1244. {
  1245. u8 period = esp->msg_in[3];
  1246. u8 offset = esp->msg_in[4];
  1247. u8 stp;
  1248. if (!(tp->flags & ESP_TGT_NEGO_SYNC))
  1249. goto do_reject;
  1250. if (offset > 15)
  1251. goto do_reject;
  1252. if (offset) {
  1253. int one_clock;
  1254. if (period > esp->max_period) {
  1255. period = offset = 0;
  1256. goto do_sdtr;
  1257. }
  1258. if (period < esp->min_period)
  1259. goto do_reject;
  1260. one_clock = esp->ccycle / 1000;
  1261. stp = DIV_ROUND_UP(period << 2, one_clock);
  1262. if (stp && esp->rev >= FAS236) {
  1263. if (stp >= 50)
  1264. stp--;
  1265. }
  1266. } else {
  1267. stp = 0;
  1268. }
  1269. esp_setsync(esp, tp, period, offset, stp, offset);
  1270. return;
  1271. do_reject:
  1272. esp->msg_out[0] = MESSAGE_REJECT;
  1273. esp->msg_out_len = 1;
  1274. scsi_esp_cmd(esp, ESP_CMD_SATN);
  1275. return;
  1276. do_sdtr:
  1277. tp->nego_goal_period = period;
  1278. tp->nego_goal_offset = offset;
  1279. esp->msg_out_len =
  1280. spi_populate_sync_msg(&esp->msg_out[0],
  1281. tp->nego_goal_period,
  1282. tp->nego_goal_offset);
  1283. scsi_esp_cmd(esp, ESP_CMD_SATN);
  1284. }
  1285. static void esp_msgin_wdtr(struct esp *esp, struct esp_target_data *tp)
  1286. {
  1287. int size = 8 << esp->msg_in[3];
  1288. u8 cfg3;
  1289. if (esp->rev != FASHME)
  1290. goto do_reject;
  1291. if (size != 8 && size != 16)
  1292. goto do_reject;
  1293. if (!(tp->flags & ESP_TGT_NEGO_WIDE))
  1294. goto do_reject;
  1295. cfg3 = tp->esp_config3;
  1296. if (size == 16) {
  1297. tp->flags |= ESP_TGT_WIDE;
  1298. cfg3 |= ESP_CONFIG3_EWIDE;
  1299. } else {
  1300. tp->flags &= ~ESP_TGT_WIDE;
  1301. cfg3 &= ~ESP_CONFIG3_EWIDE;
  1302. }
  1303. tp->esp_config3 = cfg3;
  1304. esp->prev_cfg3 = cfg3;
  1305. esp_write8(cfg3, ESP_CFG3);
  1306. tp->flags &= ~ESP_TGT_NEGO_WIDE;
  1307. spi_period(tp->starget) = 0;
  1308. spi_offset(tp->starget) = 0;
  1309. if (!esp_need_to_nego_sync(tp)) {
  1310. tp->flags &= ~ESP_TGT_CHECK_NEGO;
  1311. scsi_esp_cmd(esp, ESP_CMD_RATN);
  1312. } else {
  1313. esp->msg_out_len =
  1314. spi_populate_sync_msg(&esp->msg_out[0],
  1315. tp->nego_goal_period,
  1316. tp->nego_goal_offset);
  1317. tp->flags |= ESP_TGT_NEGO_SYNC;
  1318. scsi_esp_cmd(esp, ESP_CMD_SATN);
  1319. }
  1320. return;
  1321. do_reject:
  1322. esp->msg_out[0] = MESSAGE_REJECT;
  1323. esp->msg_out_len = 1;
  1324. scsi_esp_cmd(esp, ESP_CMD_SATN);
  1325. }
  1326. static void esp_msgin_extended(struct esp *esp)
  1327. {
  1328. struct esp_cmd_entry *ent = esp->active_cmd;
  1329. struct scsi_cmnd *cmd = ent->cmd;
  1330. struct esp_target_data *tp;
  1331. int tgt = cmd->device->id;
  1332. tp = &esp->target[tgt];
  1333. if (esp->msg_in[2] == EXTENDED_SDTR) {
  1334. esp_msgin_sdtr(esp, tp);
  1335. return;
  1336. }
  1337. if (esp->msg_in[2] == EXTENDED_WDTR) {
  1338. esp_msgin_wdtr(esp, tp);
  1339. return;
  1340. }
  1341. shost_printk(KERN_INFO, esp->host,
  1342. "Unexpected extended msg type %x\n", esp->msg_in[2]);
  1343. esp->msg_out[0] = MESSAGE_REJECT;
  1344. esp->msg_out_len = 1;
  1345. scsi_esp_cmd(esp, ESP_CMD_SATN);
  1346. }
  1347. /* Analyze msgin bytes received from target so far. Return non-zero
  1348. * if there are more bytes needed to complete the message.
  1349. */
  1350. static int esp_msgin_process(struct esp *esp)
  1351. {
  1352. u8 msg0 = esp->msg_in[0];
  1353. int len = esp->msg_in_len;
  1354. if (msg0 & 0x80) {
  1355. /* Identify */
  1356. shost_printk(KERN_INFO, esp->host,
  1357. "Unexpected msgin identify\n");
  1358. return 0;
  1359. }
  1360. switch (msg0) {
  1361. case EXTENDED_MESSAGE:
  1362. if (len == 1)
  1363. return 1;
  1364. if (len < esp->msg_in[1] + 2)
  1365. return 1;
  1366. esp_msgin_extended(esp);
  1367. return 0;
  1368. case IGNORE_WIDE_RESIDUE: {
  1369. struct esp_cmd_entry *ent;
  1370. struct esp_cmd_priv *spriv;
  1371. if (len == 1)
  1372. return 1;
  1373. if (esp->msg_in[1] != 1)
  1374. goto do_reject;
  1375. ent = esp->active_cmd;
  1376. spriv = ESP_CMD_PRIV(ent->cmd);
  1377. if (spriv->cur_residue == sg_dma_len(spriv->cur_sg)) {
  1378. spriv->cur_sg--;
  1379. spriv->cur_residue = 1;
  1380. } else
  1381. spriv->cur_residue++;
  1382. spriv->tot_residue++;
  1383. return 0;
  1384. }
  1385. case NOP:
  1386. return 0;
  1387. case RESTORE_POINTERS:
  1388. esp_restore_pointers(esp, esp->active_cmd);
  1389. return 0;
  1390. case SAVE_POINTERS:
  1391. esp_save_pointers(esp, esp->active_cmd);
  1392. return 0;
  1393. case COMMAND_COMPLETE:
  1394. case DISCONNECT: {
  1395. struct esp_cmd_entry *ent = esp->active_cmd;
  1396. ent->message = msg0;
  1397. esp_event(esp, ESP_EVENT_FREE_BUS);
  1398. esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
  1399. return 0;
  1400. }
  1401. case MESSAGE_REJECT:
  1402. esp_msgin_reject(esp);
  1403. return 0;
  1404. default:
  1405. do_reject:
  1406. esp->msg_out[0] = MESSAGE_REJECT;
  1407. esp->msg_out_len = 1;
  1408. scsi_esp_cmd(esp, ESP_CMD_SATN);
  1409. return 0;
  1410. }
  1411. }
  1412. static int esp_process_event(struct esp *esp)
  1413. {
  1414. int write, i;
  1415. again:
  1416. write = 0;
  1417. esp_log_event("process event %d phase %x\n",
  1418. esp->event, esp->sreg & ESP_STAT_PMASK);
  1419. switch (esp->event) {
  1420. case ESP_EVENT_CHECK_PHASE:
  1421. switch (esp->sreg & ESP_STAT_PMASK) {
  1422. case ESP_DOP:
  1423. esp_event(esp, ESP_EVENT_DATA_OUT);
  1424. break;
  1425. case ESP_DIP:
  1426. esp_event(esp, ESP_EVENT_DATA_IN);
  1427. break;
  1428. case ESP_STATP:
  1429. esp_flush_fifo(esp);
  1430. scsi_esp_cmd(esp, ESP_CMD_ICCSEQ);
  1431. esp_event(esp, ESP_EVENT_STATUS);
  1432. esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
  1433. return 1;
  1434. case ESP_MOP:
  1435. esp_event(esp, ESP_EVENT_MSGOUT);
  1436. break;
  1437. case ESP_MIP:
  1438. esp_event(esp, ESP_EVENT_MSGIN);
  1439. break;
  1440. case ESP_CMDP:
  1441. esp_event(esp, ESP_EVENT_CMD_START);
  1442. break;
  1443. default:
  1444. shost_printk(KERN_INFO, esp->host,
  1445. "Unexpected phase, sreg=%02x\n",
  1446. esp->sreg);
  1447. esp_schedule_reset(esp);
  1448. return 0;
  1449. }
  1450. goto again;
  1451. case ESP_EVENT_DATA_IN:
  1452. write = 1;
  1453. /* fallthru */
  1454. case ESP_EVENT_DATA_OUT: {
  1455. struct esp_cmd_entry *ent = esp->active_cmd;
  1456. struct scsi_cmnd *cmd = ent->cmd;
  1457. dma_addr_t dma_addr = esp_cur_dma_addr(ent, cmd);
  1458. unsigned int dma_len = esp_cur_dma_len(ent, cmd);
  1459. if (esp->rev == ESP100)
  1460. scsi_esp_cmd(esp, ESP_CMD_NULL);
  1461. if (write)
  1462. ent->flags |= ESP_CMD_FLAG_WRITE;
  1463. else
  1464. ent->flags &= ~ESP_CMD_FLAG_WRITE;
  1465. if (esp->ops->dma_length_limit)
  1466. dma_len = esp->ops->dma_length_limit(esp, dma_addr,
  1467. dma_len);
  1468. else
  1469. dma_len = esp_dma_length_limit(esp, dma_addr, dma_len);
  1470. esp->data_dma_len = dma_len;
  1471. if (!dma_len) {
  1472. shost_printk(KERN_ERR, esp->host,
  1473. "DMA length is zero!\n");
  1474. shost_printk(KERN_ERR, esp->host,
  1475. "cur adr[%08llx] len[%08x]\n",
  1476. (unsigned long long)esp_cur_dma_addr(ent, cmd),
  1477. esp_cur_dma_len(ent, cmd));
  1478. esp_schedule_reset(esp);
  1479. return 0;
  1480. }
  1481. esp_log_datastart("start data addr[%08llx] len[%u] write(%d)\n",
  1482. (unsigned long long)dma_addr, dma_len, write);
  1483. esp->ops->send_dma_cmd(esp, dma_addr, dma_len, dma_len,
  1484. write, ESP_CMD_DMA | ESP_CMD_TI);
  1485. esp_event(esp, ESP_EVENT_DATA_DONE);
  1486. break;
  1487. }
  1488. case ESP_EVENT_DATA_DONE: {
  1489. struct esp_cmd_entry *ent = esp->active_cmd;
  1490. struct scsi_cmnd *cmd = ent->cmd;
  1491. int bytes_sent;
  1492. if (esp->ops->dma_error(esp)) {
  1493. shost_printk(KERN_INFO, esp->host,
  1494. "data done, DMA error, resetting\n");
  1495. esp_schedule_reset(esp);
  1496. return 0;
  1497. }
  1498. if (ent->flags & ESP_CMD_FLAG_WRITE) {
  1499. /* XXX parity errors, etc. XXX */
  1500. esp->ops->dma_drain(esp);
  1501. }
  1502. esp->ops->dma_invalidate(esp);
  1503. if (esp->ireg != ESP_INTR_BSERV) {
  1504. /* We should always see exactly a bus-service
  1505. * interrupt at the end of a successful transfer.
  1506. */
  1507. shost_printk(KERN_INFO, esp->host,
  1508. "data done, not BSERV, resetting\n");
  1509. esp_schedule_reset(esp);
  1510. return 0;
  1511. }
  1512. bytes_sent = esp_data_bytes_sent(esp, ent, cmd);
  1513. esp_log_datadone("data done flgs[%x] sent[%d]\n",
  1514. ent->flags, bytes_sent);
  1515. if (bytes_sent < 0) {
  1516. /* XXX force sync mode for this target XXX */
  1517. esp_schedule_reset(esp);
  1518. return 0;
  1519. }
  1520. esp_advance_dma(esp, ent, cmd, bytes_sent);
  1521. esp_event(esp, ESP_EVENT_CHECK_PHASE);
  1522. goto again;
  1523. }
  1524. case ESP_EVENT_STATUS: {
  1525. struct esp_cmd_entry *ent = esp->active_cmd;
  1526. if (esp->ireg & ESP_INTR_FDONE) {
  1527. ent->status = esp_read8(ESP_FDATA);
  1528. ent->message = esp_read8(ESP_FDATA);
  1529. scsi_esp_cmd(esp, ESP_CMD_MOK);
  1530. } else if (esp->ireg == ESP_INTR_BSERV) {
  1531. ent->status = esp_read8(ESP_FDATA);
  1532. ent->message = 0xff;
  1533. esp_event(esp, ESP_EVENT_MSGIN);
  1534. return 0;
  1535. }
  1536. if (ent->message != COMMAND_COMPLETE) {
  1537. shost_printk(KERN_INFO, esp->host,
  1538. "Unexpected message %x in status\n",
  1539. ent->message);
  1540. esp_schedule_reset(esp);
  1541. return 0;
  1542. }
  1543. esp_event(esp, ESP_EVENT_FREE_BUS);
  1544. esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
  1545. break;
  1546. }
  1547. case ESP_EVENT_FREE_BUS: {
  1548. struct esp_cmd_entry *ent = esp->active_cmd;
  1549. struct scsi_cmnd *cmd = ent->cmd;
  1550. if (ent->message == COMMAND_COMPLETE ||
  1551. ent->message == DISCONNECT)
  1552. scsi_esp_cmd(esp, ESP_CMD_ESEL);
  1553. if (ent->message == COMMAND_COMPLETE) {
  1554. esp_log_cmddone("Command done status[%x] message[%x]\n",
  1555. ent->status, ent->message);
  1556. if (ent->status == SAM_STAT_TASK_SET_FULL)
  1557. esp_event_queue_full(esp, ent);
  1558. if (ent->status == SAM_STAT_CHECK_CONDITION &&
  1559. !(ent->flags & ESP_CMD_FLAG_AUTOSENSE)) {
  1560. ent->flags |= ESP_CMD_FLAG_AUTOSENSE;
  1561. esp_autosense(esp, ent);
  1562. } else {
  1563. esp_cmd_is_done(esp, ent, cmd,
  1564. compose_result(ent->status,
  1565. ent->message,
  1566. DID_OK));
  1567. }
  1568. } else if (ent->message == DISCONNECT) {
  1569. esp_log_disconnect("Disconnecting tgt[%d] tag[%x:%x]\n",
  1570. cmd->device->id,
  1571. ent->tag[0], ent->tag[1]);
  1572. esp->active_cmd = NULL;
  1573. esp_maybe_execute_command(esp);
  1574. } else {
  1575. shost_printk(KERN_INFO, esp->host,
  1576. "Unexpected message %x in freebus\n",
  1577. ent->message);
  1578. esp_schedule_reset(esp);
  1579. return 0;
  1580. }
  1581. if (esp->active_cmd)
  1582. esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
  1583. break;
  1584. }
  1585. case ESP_EVENT_MSGOUT: {
  1586. scsi_esp_cmd(esp, ESP_CMD_FLUSH);
  1587. if (esp_debug & ESP_DEBUG_MSGOUT) {
  1588. int i;
  1589. printk("ESP: Sending message [ ");
  1590. for (i = 0; i < esp->msg_out_len; i++)
  1591. printk("%02x ", esp->msg_out[i]);
  1592. printk("]\n");
  1593. }
  1594. if (esp->rev == FASHME) {
  1595. int i;
  1596. /* Always use the fifo. */
  1597. for (i = 0; i < esp->msg_out_len; i++) {
  1598. esp_write8(esp->msg_out[i], ESP_FDATA);
  1599. esp_write8(0, ESP_FDATA);
  1600. }
  1601. scsi_esp_cmd(esp, ESP_CMD_TI);
  1602. } else {
  1603. if (esp->msg_out_len == 1) {
  1604. esp_write8(esp->msg_out[0], ESP_FDATA);
  1605. scsi_esp_cmd(esp, ESP_CMD_TI);
  1606. } else if (esp->flags & ESP_FLAG_USE_FIFO) {
  1607. for (i = 0; i < esp->msg_out_len; i++)
  1608. esp_write8(esp->msg_out[i], ESP_FDATA);
  1609. scsi_esp_cmd(esp, ESP_CMD_TI);
  1610. } else {
  1611. /* Use DMA. */
  1612. memcpy(esp->command_block,
  1613. esp->msg_out,
  1614. esp->msg_out_len);
  1615. esp->ops->send_dma_cmd(esp,
  1616. esp->command_block_dma,
  1617. esp->msg_out_len,
  1618. esp->msg_out_len,
  1619. 0,
  1620. ESP_CMD_DMA|ESP_CMD_TI);
  1621. }
  1622. }
  1623. esp_event(esp, ESP_EVENT_MSGOUT_DONE);
  1624. break;
  1625. }
  1626. case ESP_EVENT_MSGOUT_DONE:
  1627. if (esp->rev == FASHME) {
  1628. scsi_esp_cmd(esp, ESP_CMD_FLUSH);
  1629. } else {
  1630. if (esp->msg_out_len > 1)
  1631. esp->ops->dma_invalidate(esp);
  1632. /* XXX if the chip went into disconnected mode,
  1633. * we can't run the phase state machine anyway.
  1634. */
  1635. if (!(esp->ireg & ESP_INTR_DC))
  1636. scsi_esp_cmd(esp, ESP_CMD_NULL);
  1637. }
  1638. esp->msg_out_len = 0;
  1639. esp_event(esp, ESP_EVENT_CHECK_PHASE);
  1640. goto again;
  1641. case ESP_EVENT_MSGIN:
  1642. if (esp->ireg & ESP_INTR_BSERV) {
  1643. if (esp->rev == FASHME) {
  1644. if (!(esp_read8(ESP_STATUS2) &
  1645. ESP_STAT2_FEMPTY))
  1646. scsi_esp_cmd(esp, ESP_CMD_FLUSH);
  1647. } else {
  1648. scsi_esp_cmd(esp, ESP_CMD_FLUSH);
  1649. if (esp->rev == ESP100)
  1650. scsi_esp_cmd(esp, ESP_CMD_NULL);
  1651. }
  1652. scsi_esp_cmd(esp, ESP_CMD_TI);
  1653. esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
  1654. return 1;
  1655. }
  1656. if (esp->ireg & ESP_INTR_FDONE) {
  1657. u8 val;
  1658. if (esp->rev == FASHME)
  1659. val = esp->fifo[0];
  1660. else
  1661. val = esp_read8(ESP_FDATA);
  1662. esp->msg_in[esp->msg_in_len++] = val;
  1663. esp_log_msgin("Got msgin byte %x\n", val);
  1664. if (!esp_msgin_process(esp))
  1665. esp->msg_in_len = 0;
  1666. if (esp->rev == FASHME)
  1667. scsi_esp_cmd(esp, ESP_CMD_FLUSH);
  1668. scsi_esp_cmd(esp, ESP_CMD_MOK);
  1669. /* Check whether a bus reset is to be done next */
  1670. if (esp->event == ESP_EVENT_RESET)
  1671. return 0;
  1672. if (esp->event != ESP_EVENT_FREE_BUS)
  1673. esp_event(esp, ESP_EVENT_CHECK_PHASE);
  1674. } else {
  1675. shost_printk(KERN_INFO, esp->host,
  1676. "MSGIN neither BSERV not FDON, resetting");
  1677. esp_schedule_reset(esp);
  1678. return 0;
  1679. }
  1680. break;
  1681. case ESP_EVENT_CMD_START:
  1682. memcpy(esp->command_block, esp->cmd_bytes_ptr,
  1683. esp->cmd_bytes_left);
  1684. esp_send_dma_cmd(esp, esp->cmd_bytes_left, 16, ESP_CMD_TI);
  1685. esp_event(esp, ESP_EVENT_CMD_DONE);
  1686. esp->flags |= ESP_FLAG_QUICKIRQ_CHECK;
  1687. break;
  1688. case ESP_EVENT_CMD_DONE:
  1689. esp->ops->dma_invalidate(esp);
  1690. if (esp->ireg & ESP_INTR_BSERV) {
  1691. esp_event(esp, ESP_EVENT_CHECK_PHASE);
  1692. goto again;
  1693. }
  1694. esp_schedule_reset(esp);
  1695. return 0;
  1696. case ESP_EVENT_RESET:
  1697. scsi_esp_cmd(esp, ESP_CMD_RS);
  1698. break;
  1699. default:
  1700. shost_printk(KERN_INFO, esp->host,
  1701. "Unexpected event %x, resetting\n", esp->event);
  1702. esp_schedule_reset(esp);
  1703. return 0;
  1704. }
  1705. return 1;
  1706. }
  1707. static void esp_reset_cleanup_one(struct esp *esp, struct esp_cmd_entry *ent)
  1708. {
  1709. struct scsi_cmnd *cmd = ent->cmd;
  1710. esp_unmap_dma(esp, cmd);
  1711. esp_free_lun_tag(ent, cmd->device->hostdata);
  1712. cmd->result = DID_RESET << 16;
  1713. if (ent->flags & ESP_CMD_FLAG_AUTOSENSE) {
  1714. esp->ops->unmap_single(esp, ent->sense_dma,
  1715. SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
  1716. ent->sense_ptr = NULL;
  1717. }
  1718. cmd->scsi_done(cmd);
  1719. list_del(&ent->list);
  1720. esp_put_ent(esp, ent);
  1721. }
  1722. static void esp_clear_hold(struct scsi_device *dev, void *data)
  1723. {
  1724. struct esp_lun_data *lp = dev->hostdata;
  1725. BUG_ON(lp->num_tagged);
  1726. lp->hold = 0;
  1727. }
  1728. static void esp_reset_cleanup(struct esp *esp)
  1729. {
  1730. struct esp_cmd_entry *ent, *tmp;
  1731. int i;
  1732. list_for_each_entry_safe(ent, tmp, &esp->queued_cmds, list) {
  1733. struct scsi_cmnd *cmd = ent->cmd;
  1734. list_del(&ent->list);
  1735. cmd->result = DID_RESET << 16;
  1736. cmd->scsi_done(cmd);
  1737. esp_put_ent(esp, ent);
  1738. }
  1739. list_for_each_entry_safe(ent, tmp, &esp->active_cmds, list) {
  1740. if (ent == esp->active_cmd)
  1741. esp->active_cmd = NULL;
  1742. esp_reset_cleanup_one(esp, ent);
  1743. }
  1744. BUG_ON(esp->active_cmd != NULL);
  1745. /* Force renegotiation of sync/wide transfers. */
  1746. for (i = 0; i < ESP_MAX_TARGET; i++) {
  1747. struct esp_target_data *tp = &esp->target[i];
  1748. tp->esp_period = 0;
  1749. tp->esp_offset = 0;
  1750. tp->esp_config3 &= ~(ESP_CONFIG3_EWIDE |
  1751. ESP_CONFIG3_FSCSI |
  1752. ESP_CONFIG3_FAST);
  1753. tp->flags &= ~ESP_TGT_WIDE;
  1754. tp->flags |= ESP_TGT_CHECK_NEGO;
  1755. if (tp->starget)
  1756. __starget_for_each_device(tp->starget, NULL,
  1757. esp_clear_hold);
  1758. }
  1759. esp->flags &= ~ESP_FLAG_RESETTING;
  1760. }
  1761. /* Runs under host->lock */
  1762. static void __esp_interrupt(struct esp *esp)
  1763. {
  1764. int finish_reset, intr_done;
  1765. u8 phase;
  1766. /*
  1767. * Once INTRPT is read STATUS and SSTEP are cleared.
  1768. */
  1769. esp->sreg = esp_read8(ESP_STATUS);
  1770. esp->seqreg = esp_read8(ESP_SSTEP);
  1771. esp->ireg = esp_read8(ESP_INTRPT);
  1772. if (esp->flags & ESP_FLAG_RESETTING) {
  1773. finish_reset = 1;
  1774. } else {
  1775. if (esp_check_gross_error(esp))
  1776. return;
  1777. finish_reset = esp_check_spur_intr(esp);
  1778. if (finish_reset < 0)
  1779. return;
  1780. }
  1781. if (esp->ireg & ESP_INTR_SR)
  1782. finish_reset = 1;
  1783. if (finish_reset) {
  1784. esp_reset_cleanup(esp);
  1785. if (esp->eh_reset) {
  1786. complete(esp->eh_reset);
  1787. esp->eh_reset = NULL;
  1788. }
  1789. return;
  1790. }
  1791. phase = (esp->sreg & ESP_STAT_PMASK);
  1792. if (esp->rev == FASHME) {
  1793. if (((phase != ESP_DIP && phase != ESP_DOP) &&
  1794. esp->select_state == ESP_SELECT_NONE &&
  1795. esp->event != ESP_EVENT_STATUS &&
  1796. esp->event != ESP_EVENT_DATA_DONE) ||
  1797. (esp->ireg & ESP_INTR_RSEL)) {
  1798. esp->sreg2 = esp_read8(ESP_STATUS2);
  1799. if (!(esp->sreg2 & ESP_STAT2_FEMPTY) ||
  1800. (esp->sreg2 & ESP_STAT2_F1BYTE))
  1801. hme_read_fifo(esp);
  1802. }
  1803. }
  1804. esp_log_intr("intr sreg[%02x] seqreg[%02x] "
  1805. "sreg2[%02x] ireg[%02x]\n",
  1806. esp->sreg, esp->seqreg, esp->sreg2, esp->ireg);
  1807. intr_done = 0;
  1808. if (esp->ireg & (ESP_INTR_S | ESP_INTR_SATN | ESP_INTR_IC)) {
  1809. shost_printk(KERN_INFO, esp->host,
  1810. "unexpected IREG %02x\n", esp->ireg);
  1811. if (esp->ireg & ESP_INTR_IC)
  1812. esp_dump_cmd_log(esp);
  1813. esp_schedule_reset(esp);
  1814. } else {
  1815. if (esp->ireg & ESP_INTR_RSEL) {
  1816. if (esp->active_cmd)
  1817. (void) esp_finish_select(esp);
  1818. intr_done = esp_reconnect(esp);
  1819. } else {
  1820. /* Some combination of FDONE, BSERV, DC. */
  1821. if (esp->select_state != ESP_SELECT_NONE)
  1822. intr_done = esp_finish_select(esp);
  1823. }
  1824. }
  1825. while (!intr_done)
  1826. intr_done = esp_process_event(esp);
  1827. }
  1828. irqreturn_t scsi_esp_intr(int irq, void *dev_id)
  1829. {
  1830. struct esp *esp = dev_id;
  1831. unsigned long flags;
  1832. irqreturn_t ret;
  1833. spin_lock_irqsave(esp->host->host_lock, flags);
  1834. ret = IRQ_NONE;
  1835. if (esp->ops->irq_pending(esp)) {
  1836. ret = IRQ_HANDLED;
  1837. for (;;) {
  1838. int i;
  1839. __esp_interrupt(esp);
  1840. if (!(esp->flags & ESP_FLAG_QUICKIRQ_CHECK))
  1841. break;
  1842. esp->flags &= ~ESP_FLAG_QUICKIRQ_CHECK;
  1843. for (i = 0; i < ESP_QUICKIRQ_LIMIT; i++) {
  1844. if (esp->ops->irq_pending(esp))
  1845. break;
  1846. }
  1847. if (i == ESP_QUICKIRQ_LIMIT)
  1848. break;
  1849. }
  1850. }
  1851. spin_unlock_irqrestore(esp->host->host_lock, flags);
  1852. return ret;
  1853. }
  1854. EXPORT_SYMBOL(scsi_esp_intr);
  1855. static void esp_get_revision(struct esp *esp)
  1856. {
  1857. u8 val;
  1858. esp->config1 = (ESP_CONFIG1_PENABLE | (esp->scsi_id & 7));
  1859. if (esp->config2 == 0) {
  1860. esp->config2 = (ESP_CONFIG2_SCSI2ENAB | ESP_CONFIG2_REGPARITY);
  1861. esp_write8(esp->config2, ESP_CFG2);
  1862. val = esp_read8(ESP_CFG2);
  1863. val &= ~ESP_CONFIG2_MAGIC;
  1864. esp->config2 = 0;
  1865. if (val != (ESP_CONFIG2_SCSI2ENAB | ESP_CONFIG2_REGPARITY)) {
  1866. /*
  1867. * If what we write to cfg2 does not come back,
  1868. * cfg2 is not implemented.
  1869. * Therefore this must be a plain esp100.
  1870. */
  1871. esp->rev = ESP100;
  1872. return;
  1873. }
  1874. }
  1875. esp_set_all_config3(esp, 5);
  1876. esp->prev_cfg3 = 5;
  1877. esp_write8(esp->config2, ESP_CFG2);
  1878. esp_write8(0, ESP_CFG3);
  1879. esp_write8(esp->prev_cfg3, ESP_CFG3);
  1880. val = esp_read8(ESP_CFG3);
  1881. if (val != 5) {
  1882. /* The cfg2 register is implemented, however
  1883. * cfg3 is not, must be esp100a.
  1884. */
  1885. esp->rev = ESP100A;
  1886. } else {
  1887. esp_set_all_config3(esp, 0);
  1888. esp->prev_cfg3 = 0;
  1889. esp_write8(esp->prev_cfg3, ESP_CFG3);
  1890. /* All of cfg{1,2,3} implemented, must be one of
  1891. * the fas variants, figure out which one.
  1892. */
  1893. if (esp->cfact == 0 || esp->cfact > ESP_CCF_F5) {
  1894. esp->rev = FAST;
  1895. esp->sync_defp = SYNC_DEFP_FAST;
  1896. } else {
  1897. esp->rev = ESP236;
  1898. }
  1899. }
  1900. }
  1901. static void esp_init_swstate(struct esp *esp)
  1902. {
  1903. int i;
  1904. INIT_LIST_HEAD(&esp->queued_cmds);
  1905. INIT_LIST_HEAD(&esp->active_cmds);
  1906. INIT_LIST_HEAD(&esp->esp_cmd_pool);
  1907. /* Start with a clear state, domain validation (via ->slave_configure,
  1908. * spi_dv_device()) will attempt to enable SYNC, WIDE, and tagged
  1909. * commands.
  1910. */
  1911. for (i = 0 ; i < ESP_MAX_TARGET; i++) {
  1912. esp->target[i].flags = 0;
  1913. esp->target[i].nego_goal_period = 0;
  1914. esp->target[i].nego_goal_offset = 0;
  1915. esp->target[i].nego_goal_width = 0;
  1916. esp->target[i].nego_goal_tags = 0;
  1917. }
  1918. }
  1919. /* This places the ESP into a known state at boot time. */
  1920. static void esp_bootup_reset(struct esp *esp)
  1921. {
  1922. u8 val;
  1923. /* Reset the DMA */
  1924. esp->ops->reset_dma(esp);
  1925. /* Reset the ESP */
  1926. esp_reset_esp(esp);
  1927. /* Reset the SCSI bus, but tell ESP not to generate an irq */
  1928. val = esp_read8(ESP_CFG1);
  1929. val |= ESP_CONFIG1_SRRDISAB;
  1930. esp_write8(val, ESP_CFG1);
  1931. scsi_esp_cmd(esp, ESP_CMD_RS);
  1932. udelay(400);
  1933. esp_write8(esp->config1, ESP_CFG1);
  1934. /* Eat any bitrot in the chip and we are done... */
  1935. esp_read8(ESP_INTRPT);
  1936. }
  1937. static void esp_set_clock_params(struct esp *esp)
  1938. {
  1939. int fhz;
  1940. u8 ccf;
  1941. /* This is getting messy but it has to be done correctly or else
  1942. * you get weird behavior all over the place. We are trying to
  1943. * basically figure out three pieces of information.
  1944. *
  1945. * a) Clock Conversion Factor
  1946. *
  1947. * This is a representation of the input crystal clock frequency
  1948. * going into the ESP on this machine. Any operation whose timing
  1949. * is longer than 400ns depends on this value being correct. For
  1950. * example, you'll get blips for arbitration/selection during high
  1951. * load or with multiple targets if this is not set correctly.
  1952. *
  1953. * b) Selection Time-Out
  1954. *
  1955. * The ESP isn't very bright and will arbitrate for the bus and try
  1956. * to select a target forever if you let it. This value tells the
  1957. * ESP when it has taken too long to negotiate and that it should
  1958. * interrupt the CPU so we can see what happened. The value is
  1959. * computed as follows (from NCR/Symbios chip docs).
  1960. *
  1961. * (Time Out Period) * (Input Clock)
  1962. * STO = ----------------------------------
  1963. * (8192) * (Clock Conversion Factor)
  1964. *
  1965. * We use a time out period of 250ms (ESP_BUS_TIMEOUT).
  1966. *
  1967. * c) Imperical constants for synchronous offset and transfer period
  1968. * register values
  1969. *
  1970. * This entails the smallest and largest sync period we could ever
  1971. * handle on this ESP.
  1972. */
  1973. fhz = esp->cfreq;
  1974. ccf = ((fhz / 1000000) + 4) / 5;
  1975. if (ccf == 1)
  1976. ccf = 2;
  1977. /* If we can't find anything reasonable, just assume 20MHZ.
  1978. * This is the clock frequency of the older sun4c's where I've
  1979. * been unable to find the clock-frequency PROM property. All
  1980. * other machines provide useful values it seems.
  1981. */
  1982. if (fhz <= 5000000 || ccf < 1 || ccf > 8) {
  1983. fhz = 20000000;
  1984. ccf = 4;
  1985. }
  1986. esp->cfact = (ccf == 8 ? 0 : ccf);
  1987. esp->cfreq = fhz;
  1988. esp->ccycle = ESP_HZ_TO_CYCLE(fhz);
  1989. esp->ctick = ESP_TICK(ccf, esp->ccycle);
  1990. esp->neg_defp = ESP_NEG_DEFP(fhz, ccf);
  1991. esp->sync_defp = SYNC_DEFP_SLOW;
  1992. }
  1993. static const char *esp_chip_names[] = {
  1994. "ESP100",
  1995. "ESP100A",
  1996. "ESP236",
  1997. "FAS236",
  1998. "FAS100A",
  1999. "FAST",
  2000. "FASHME",
  2001. "AM53C974",
  2002. };
  2003. static struct scsi_transport_template *esp_transport_template;
  2004. int scsi_esp_register(struct esp *esp, struct device *dev)
  2005. {
  2006. static int instance;
  2007. int err;
  2008. if (!esp->num_tags)
  2009. esp->num_tags = ESP_DEFAULT_TAGS;
  2010. esp->host->transportt = esp_transport_template;
  2011. esp->host->max_lun = ESP_MAX_LUN;
  2012. esp->host->cmd_per_lun = 2;
  2013. esp->host->unique_id = instance;
  2014. esp_set_clock_params(esp);
  2015. esp_get_revision(esp);
  2016. esp_init_swstate(esp);
  2017. esp_bootup_reset(esp);
  2018. dev_printk(KERN_INFO, dev, "esp%u: regs[%1p:%1p] irq[%u]\n",
  2019. esp->host->unique_id, esp->regs, esp->dma_regs,
  2020. esp->host->irq);
  2021. dev_printk(KERN_INFO, dev,
  2022. "esp%u: is a %s, %u MHz (ccf=%u), SCSI ID %u\n",
  2023. esp->host->unique_id, esp_chip_names[esp->rev],
  2024. esp->cfreq / 1000000, esp->cfact, esp->scsi_id);
  2025. /* Let the SCSI bus reset settle. */
  2026. ssleep(esp_bus_reset_settle);
  2027. err = scsi_add_host(esp->host, dev);
  2028. if (err)
  2029. return err;
  2030. instance++;
  2031. scsi_scan_host(esp->host);
  2032. return 0;
  2033. }
  2034. EXPORT_SYMBOL(scsi_esp_register);
  2035. void scsi_esp_unregister(struct esp *esp)
  2036. {
  2037. scsi_remove_host(esp->host);
  2038. }
  2039. EXPORT_SYMBOL(scsi_esp_unregister);
  2040. static int esp_target_alloc(struct scsi_target *starget)
  2041. {
  2042. struct esp *esp = shost_priv(dev_to_shost(&starget->dev));
  2043. struct esp_target_data *tp = &esp->target[starget->id];
  2044. tp->starget = starget;
  2045. return 0;
  2046. }
  2047. static void esp_target_destroy(struct scsi_target *starget)
  2048. {
  2049. struct esp *esp = shost_priv(dev_to_shost(&starget->dev));
  2050. struct esp_target_data *tp = &esp->target[starget->id];
  2051. tp->starget = NULL;
  2052. }
  2053. static int esp_slave_alloc(struct scsi_device *dev)
  2054. {
  2055. struct esp *esp = shost_priv(dev->host);
  2056. struct esp_target_data *tp = &esp->target[dev->id];
  2057. struct esp_lun_data *lp;
  2058. lp = kzalloc(sizeof(*lp), GFP_KERNEL);
  2059. if (!lp)
  2060. return -ENOMEM;
  2061. dev->hostdata = lp;
  2062. spi_min_period(tp->starget) = esp->min_period;
  2063. spi_max_offset(tp->starget) = 15;
  2064. if (esp->flags & ESP_FLAG_WIDE_CAPABLE)
  2065. spi_max_width(tp->starget) = 1;
  2066. else
  2067. spi_max_width(tp->starget) = 0;
  2068. return 0;
  2069. }
  2070. static int esp_slave_configure(struct scsi_device *dev)
  2071. {
  2072. struct esp *esp = shost_priv(dev->host);
  2073. struct esp_target_data *tp = &esp->target[dev->id];
  2074. if (dev->tagged_supported)
  2075. scsi_change_queue_depth(dev, esp->num_tags);
  2076. tp->flags |= ESP_TGT_DISCONNECT;
  2077. if (!spi_initial_dv(dev->sdev_target))
  2078. spi_dv_device(dev);
  2079. return 0;
  2080. }
  2081. static void esp_slave_destroy(struct scsi_device *dev)
  2082. {
  2083. struct esp_lun_data *lp = dev->hostdata;
  2084. kfree(lp);
  2085. dev->hostdata = NULL;
  2086. }
  2087. static int esp_eh_abort_handler(struct scsi_cmnd *cmd)
  2088. {
  2089. struct esp *esp = shost_priv(cmd->device->host);
  2090. struct esp_cmd_entry *ent, *tmp;
  2091. struct completion eh_done;
  2092. unsigned long flags;
  2093. /* XXX This helps a lot with debugging but might be a bit
  2094. * XXX much for the final driver.
  2095. */
  2096. spin_lock_irqsave(esp->host->host_lock, flags);
  2097. shost_printk(KERN_ERR, esp->host, "Aborting command [%p:%02x]\n",
  2098. cmd, cmd->cmnd[0]);
  2099. ent = esp->active_cmd;
  2100. if (ent)
  2101. shost_printk(KERN_ERR, esp->host,
  2102. "Current command [%p:%02x]\n",
  2103. ent->cmd, ent->cmd->cmnd[0]);
  2104. list_for_each_entry(ent, &esp->queued_cmds, list) {
  2105. shost_printk(KERN_ERR, esp->host, "Queued command [%p:%02x]\n",
  2106. ent->cmd, ent->cmd->cmnd[0]);
  2107. }
  2108. list_for_each_entry(ent, &esp->active_cmds, list) {
  2109. shost_printk(KERN_ERR, esp->host, " Active command [%p:%02x]\n",
  2110. ent->cmd, ent->cmd->cmnd[0]);
  2111. }
  2112. esp_dump_cmd_log(esp);
  2113. spin_unlock_irqrestore(esp->host->host_lock, flags);
  2114. spin_lock_irqsave(esp->host->host_lock, flags);
  2115. ent = NULL;
  2116. list_for_each_entry(tmp, &esp->queued_cmds, list) {
  2117. if (tmp->cmd == cmd) {
  2118. ent = tmp;
  2119. break;
  2120. }
  2121. }
  2122. if (ent) {
  2123. /* Easiest case, we didn't even issue the command
  2124. * yet so it is trivial to abort.
  2125. */
  2126. list_del(&ent->list);
  2127. cmd->result = DID_ABORT << 16;
  2128. cmd->scsi_done(cmd);
  2129. esp_put_ent(esp, ent);
  2130. goto out_success;
  2131. }
  2132. init_completion(&eh_done);
  2133. ent = esp->active_cmd;
  2134. if (ent && ent->cmd == cmd) {
  2135. /* Command is the currently active command on
  2136. * the bus. If we already have an output message
  2137. * pending, no dice.
  2138. */
  2139. if (esp->msg_out_len)
  2140. goto out_failure;
  2141. /* Send out an abort, encouraging the target to
  2142. * go to MSGOUT phase by asserting ATN.
  2143. */
  2144. esp->msg_out[0] = ABORT_TASK_SET;
  2145. esp->msg_out_len = 1;
  2146. ent->eh_done = &eh_done;
  2147. scsi_esp_cmd(esp, ESP_CMD_SATN);
  2148. } else {
  2149. /* The command is disconnected. This is not easy to
  2150. * abort. For now we fail and let the scsi error
  2151. * handling layer go try a scsi bus reset or host
  2152. * reset.
  2153. *
  2154. * What we could do is put together a scsi command
  2155. * solely for the purpose of sending an abort message
  2156. * to the target. Coming up with all the code to
  2157. * cook up scsi commands, special case them everywhere,
  2158. * etc. is for questionable gain and it would be better
  2159. * if the generic scsi error handling layer could do at
  2160. * least some of that for us.
  2161. *
  2162. * Anyways this is an area for potential future improvement
  2163. * in this driver.
  2164. */
  2165. goto out_failure;
  2166. }
  2167. spin_unlock_irqrestore(esp->host->host_lock, flags);
  2168. if (!wait_for_completion_timeout(&eh_done, 5 * HZ)) {
  2169. spin_lock_irqsave(esp->host->host_lock, flags);
  2170. ent->eh_done = NULL;
  2171. spin_unlock_irqrestore(esp->host->host_lock, flags);
  2172. return FAILED;
  2173. }
  2174. return SUCCESS;
  2175. out_success:
  2176. spin_unlock_irqrestore(esp->host->host_lock, flags);
  2177. return SUCCESS;
  2178. out_failure:
  2179. /* XXX This might be a good location to set ESP_TGT_BROKEN
  2180. * XXX since we know which target/lun in particular is
  2181. * XXX causing trouble.
  2182. */
  2183. spin_unlock_irqrestore(esp->host->host_lock, flags);
  2184. return FAILED;
  2185. }
  2186. static int esp_eh_bus_reset_handler(struct scsi_cmnd *cmd)
  2187. {
  2188. struct esp *esp = shost_priv(cmd->device->host);
  2189. struct completion eh_reset;
  2190. unsigned long flags;
  2191. init_completion(&eh_reset);
  2192. spin_lock_irqsave(esp->host->host_lock, flags);
  2193. esp->eh_reset = &eh_reset;
  2194. /* XXX This is too simple... We should add lots of
  2195. * XXX checks here so that if we find that the chip is
  2196. * XXX very wedged we return failure immediately so
  2197. * XXX that we can perform a full chip reset.
  2198. */
  2199. esp->flags |= ESP_FLAG_RESETTING;
  2200. scsi_esp_cmd(esp, ESP_CMD_RS);
  2201. spin_unlock_irqrestore(esp->host->host_lock, flags);
  2202. ssleep(esp_bus_reset_settle);
  2203. if (!wait_for_completion_timeout(&eh_reset, 5 * HZ)) {
  2204. spin_lock_irqsave(esp->host->host_lock, flags);
  2205. esp->eh_reset = NULL;
  2206. spin_unlock_irqrestore(esp->host->host_lock, flags);
  2207. return FAILED;
  2208. }
  2209. return SUCCESS;
  2210. }
  2211. /* All bets are off, reset the entire device. */
  2212. static int esp_eh_host_reset_handler(struct scsi_cmnd *cmd)
  2213. {
  2214. struct esp *esp = shost_priv(cmd->device->host);
  2215. unsigned long flags;
  2216. spin_lock_irqsave(esp->host->host_lock, flags);
  2217. esp_bootup_reset(esp);
  2218. esp_reset_cleanup(esp);
  2219. spin_unlock_irqrestore(esp->host->host_lock, flags);
  2220. ssleep(esp_bus_reset_settle);
  2221. return SUCCESS;
  2222. }
  2223. static const char *esp_info(struct Scsi_Host *host)
  2224. {
  2225. return "esp";
  2226. }
  2227. struct scsi_host_template scsi_esp_template = {
  2228. .module = THIS_MODULE,
  2229. .name = "esp",
  2230. .info = esp_info,
  2231. .queuecommand = esp_queuecommand,
  2232. .target_alloc = esp_target_alloc,
  2233. .target_destroy = esp_target_destroy,
  2234. .slave_alloc = esp_slave_alloc,
  2235. .slave_configure = esp_slave_configure,
  2236. .slave_destroy = esp_slave_destroy,
  2237. .eh_abort_handler = esp_eh_abort_handler,
  2238. .eh_bus_reset_handler = esp_eh_bus_reset_handler,
  2239. .eh_host_reset_handler = esp_eh_host_reset_handler,
  2240. .can_queue = 7,
  2241. .this_id = 7,
  2242. .sg_tablesize = SG_ALL,
  2243. .use_clustering = ENABLE_CLUSTERING,
  2244. .max_sectors = 0xffff,
  2245. .skip_settle_delay = 1,
  2246. };
  2247. EXPORT_SYMBOL(scsi_esp_template);
  2248. static void esp_get_signalling(struct Scsi_Host *host)
  2249. {
  2250. struct esp *esp = shost_priv(host);
  2251. enum spi_signal_type type;
  2252. if (esp->flags & ESP_FLAG_DIFFERENTIAL)
  2253. type = SPI_SIGNAL_HVD;
  2254. else
  2255. type = SPI_SIGNAL_SE;
  2256. spi_signalling(host) = type;
  2257. }
  2258. static void esp_set_offset(struct scsi_target *target, int offset)
  2259. {
  2260. struct Scsi_Host *host = dev_to_shost(target->dev.parent);
  2261. struct esp *esp = shost_priv(host);
  2262. struct esp_target_data *tp = &esp->target[target->id];
  2263. if (esp->flags & ESP_FLAG_DISABLE_SYNC)
  2264. tp->nego_goal_offset = 0;
  2265. else
  2266. tp->nego_goal_offset = offset;
  2267. tp->flags |= ESP_TGT_CHECK_NEGO;
  2268. }
  2269. static void esp_set_period(struct scsi_target *target, int period)
  2270. {
  2271. struct Scsi_Host *host = dev_to_shost(target->dev.parent);
  2272. struct esp *esp = shost_priv(host);
  2273. struct esp_target_data *tp = &esp->target[target->id];
  2274. tp->nego_goal_period = period;
  2275. tp->flags |= ESP_TGT_CHECK_NEGO;
  2276. }
  2277. static void esp_set_width(struct scsi_target *target, int width)
  2278. {
  2279. struct Scsi_Host *host = dev_to_shost(target->dev.parent);
  2280. struct esp *esp = shost_priv(host);
  2281. struct esp_target_data *tp = &esp->target[target->id];
  2282. tp->nego_goal_width = (width ? 1 : 0);
  2283. tp->flags |= ESP_TGT_CHECK_NEGO;
  2284. }
  2285. static struct spi_function_template esp_transport_ops = {
  2286. .set_offset = esp_set_offset,
  2287. .show_offset = 1,
  2288. .set_period = esp_set_period,
  2289. .show_period = 1,
  2290. .set_width = esp_set_width,
  2291. .show_width = 1,
  2292. .get_signalling = esp_get_signalling,
  2293. };
  2294. static int __init esp_init(void)
  2295. {
  2296. BUILD_BUG_ON(sizeof(struct scsi_pointer) <
  2297. sizeof(struct esp_cmd_priv));
  2298. esp_transport_template = spi_attach_transport(&esp_transport_ops);
  2299. if (!esp_transport_template)
  2300. return -ENODEV;
  2301. return 0;
  2302. }
  2303. static void __exit esp_exit(void)
  2304. {
  2305. spi_release_transport(esp_transport_template);
  2306. }
  2307. MODULE_DESCRIPTION("ESP SCSI driver core");
  2308. MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
  2309. MODULE_LICENSE("GPL");
  2310. MODULE_VERSION(DRV_VERSION);
  2311. module_param(esp_bus_reset_settle, int, 0);
  2312. MODULE_PARM_DESC(esp_bus_reset_settle,
  2313. "ESP scsi bus reset delay in seconds");
  2314. module_param(esp_debug, int, 0);
  2315. MODULE_PARM_DESC(esp_debug,
  2316. "ESP bitmapped debugging message enable value:\n"
  2317. " 0x00000001 Log interrupt events\n"
  2318. " 0x00000002 Log scsi commands\n"
  2319. " 0x00000004 Log resets\n"
  2320. " 0x00000008 Log message in events\n"
  2321. " 0x00000010 Log message out events\n"
  2322. " 0x00000020 Log command completion\n"
  2323. " 0x00000040 Log disconnects\n"
  2324. " 0x00000080 Log data start\n"
  2325. " 0x00000100 Log data done\n"
  2326. " 0x00000200 Log reconnects\n"
  2327. " 0x00000400 Log auto-sense data\n"
  2328. );
  2329. module_init(esp_init);
  2330. module_exit(esp_exit);