esp_scsi.c 65 KB

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