esp_scsi.c 69 KB

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